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 */
68 * Define a local default termios struct. All ports will be created
69 * with this termios initially.
71 * This defines a raw port at 9600 baud, 8 data bits, no parity,
74 static struct ktermios DgncDefaultTermios = {
75 .c_iflag = (DEFAULT_IFLAGS), /* iflags */
76 .c_oflag = (DEFAULT_OFLAGS), /* oflags */
77 .c_cflag = (DEFAULT_CFLAGS), /* cflags */
78 .c_lflag = (DEFAULT_LFLAGS), /* lflags */
84 /* Our function prototypes */
85 static int dgnc_tty_open(struct tty_struct *tty, struct file *file);
86 static void dgnc_tty_close(struct tty_struct *tty, struct file *file);
87 static int dgnc_block_til_ready(struct tty_struct *tty, struct file *file, struct channel_t *ch);
88 static int dgnc_tty_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg);
89 static int dgnc_tty_digigeta(struct tty_struct *tty, struct digi_t __user *retinfo);
90 static int dgnc_tty_digiseta(struct tty_struct *tty, struct digi_t __user *new_info);
91 static int dgnc_tty_write_room(struct tty_struct *tty);
92 static int dgnc_tty_put_char(struct tty_struct *tty, unsigned char c);
93 static int dgnc_tty_chars_in_buffer(struct tty_struct *tty);
94 static void dgnc_tty_start(struct tty_struct *tty);
95 static void dgnc_tty_stop(struct tty_struct *tty);
96 static void dgnc_tty_throttle(struct tty_struct *tty);
97 static void dgnc_tty_unthrottle(struct tty_struct *tty);
98 static void dgnc_tty_flush_chars(struct tty_struct *tty);
99 static void dgnc_tty_flush_buffer(struct tty_struct *tty);
100 static void dgnc_tty_hangup(struct tty_struct *tty);
101 static int dgnc_set_modem_info(struct tty_struct *tty, unsigned int command, unsigned int __user *value);
102 static int dgnc_get_modem_info(struct channel_t *ch, unsigned int __user *value);
103 static int dgnc_tty_tiocmget(struct tty_struct *tty);
104 static int dgnc_tty_tiocmset(struct tty_struct *tty, unsigned int set, unsigned int clear);
105 static int dgnc_tty_send_break(struct tty_struct *tty, int msec);
106 static void dgnc_tty_wait_until_sent(struct tty_struct *tty, int timeout);
107 static int dgnc_tty_write(struct tty_struct *tty, const unsigned char *buf, int count);
108 static void dgnc_tty_set_termios(struct tty_struct *tty, struct ktermios *old_termios);
109 static void dgnc_tty_send_xchar(struct tty_struct *tty, char ch);
112 static const struct tty_operations dgnc_tty_ops = {
113 .open = dgnc_tty_open,
114 .close = dgnc_tty_close,
115 .write = dgnc_tty_write,
116 .write_room = dgnc_tty_write_room,
117 .flush_buffer = dgnc_tty_flush_buffer,
118 .chars_in_buffer = dgnc_tty_chars_in_buffer,
119 .flush_chars = dgnc_tty_flush_chars,
120 .ioctl = dgnc_tty_ioctl,
121 .set_termios = dgnc_tty_set_termios,
122 .stop = dgnc_tty_stop,
123 .start = dgnc_tty_start,
124 .throttle = dgnc_tty_throttle,
125 .unthrottle = dgnc_tty_unthrottle,
126 .hangup = dgnc_tty_hangup,
127 .put_char = dgnc_tty_put_char,
128 .tiocmget = dgnc_tty_tiocmget,
129 .tiocmset = dgnc_tty_tiocmset,
130 .break_ctl = dgnc_tty_send_break,
131 .wait_until_sent = dgnc_tty_wait_until_sent,
132 .send_xchar = dgnc_tty_send_xchar
135 /************************************************************************
137 * TTY Initialization/Cleanup Functions
139 ************************************************************************/
144 * Initialize any global tty related data before we download any boards.
146 int dgnc_tty_preinit(void)
149 * Allocate a buffer for doing the copy from user space to
150 * kernel space in dgnc_write(). We only use one buffer and
151 * control access to it with a semaphore. If we are paging, we
152 * are already in trouble so one buffer won't hurt much anyway.
154 * We are okay to sleep in the malloc, as this routine
155 * is only called during module load, (not in interrupt context),
156 * and with no locks held.
158 dgnc_TmpWriteBuf = kmalloc(WRITEBUFLEN, GFP_KERNEL);
160 if (!dgnc_TmpWriteBuf)
168 * dgnc_tty_register()
170 * Init the tty subsystem for this board.
172 int dgnc_tty_register(struct dgnc_board *brd)
176 brd->SerialDriver.magic = TTY_DRIVER_MAGIC;
178 snprintf(brd->SerialName, MAXTTYNAMELEN, "tty_dgnc_%d_", brd->boardnum);
180 brd->SerialDriver.name = brd->SerialName;
181 brd->SerialDriver.name_base = 0;
182 brd->SerialDriver.major = 0;
183 brd->SerialDriver.minor_start = 0;
184 brd->SerialDriver.num = brd->maxports;
185 brd->SerialDriver.type = TTY_DRIVER_TYPE_SERIAL;
186 brd->SerialDriver.subtype = SERIAL_TYPE_NORMAL;
187 brd->SerialDriver.init_termios = DgncDefaultTermios;
188 brd->SerialDriver.driver_name = DRVSTR;
189 brd->SerialDriver.flags = (TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_HARDWARE_BREAK);
192 * The kernel wants space to store pointers to
193 * tty_struct's and termios's.
195 brd->SerialDriver.ttys = kcalloc(brd->maxports, sizeof(*brd->SerialDriver.ttys), GFP_KERNEL);
196 if (!brd->SerialDriver.ttys)
199 kref_init(&brd->SerialDriver.kref);
200 brd->SerialDriver.termios = kcalloc(brd->maxports, sizeof(*brd->SerialDriver.termios), GFP_KERNEL);
201 if (!brd->SerialDriver.termios)
205 * Entry points for driver. Called by the kernel from
206 * tty_io.c and n_tty.c.
208 tty_set_operations(&brd->SerialDriver, &dgnc_tty_ops);
210 if (!brd->dgnc_Major_Serial_Registered) {
211 /* Register tty devices */
212 rc = tty_register_driver(&brd->SerialDriver);
214 dev_dbg(&brd->pdev->dev,
215 "Can't register tty device (%d)\n", rc);
218 brd->dgnc_Major_Serial_Registered = true;
222 * If we're doing transparent print, we have to do all of the above
223 * again, separately so we don't get the LD confused about what major
224 * we are when we get into the dgnc_tty_open() routine.
226 brd->PrintDriver.magic = TTY_DRIVER_MAGIC;
227 snprintf(brd->PrintName, MAXTTYNAMELEN, "pr_dgnc_%d_", brd->boardnum);
229 brd->PrintDriver.name = brd->PrintName;
230 brd->PrintDriver.name_base = 0;
231 brd->PrintDriver.major = brd->SerialDriver.major;
232 brd->PrintDriver.minor_start = 0x80;
233 brd->PrintDriver.num = brd->maxports;
234 brd->PrintDriver.type = TTY_DRIVER_TYPE_SERIAL;
235 brd->PrintDriver.subtype = SERIAL_TYPE_NORMAL;
236 brd->PrintDriver.init_termios = DgncDefaultTermios;
237 brd->PrintDriver.driver_name = DRVSTR;
238 brd->PrintDriver.flags = (TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_HARDWARE_BREAK);
241 * The kernel wants space to store pointers to
242 * tty_struct's and termios's. Must be separated from
243 * the Serial Driver so we don't get confused
245 brd->PrintDriver.ttys = kcalloc(brd->maxports, sizeof(*brd->PrintDriver.ttys), GFP_KERNEL);
246 if (!brd->PrintDriver.ttys)
248 kref_init(&brd->PrintDriver.kref);
249 brd->PrintDriver.termios = kcalloc(brd->maxports, sizeof(*brd->PrintDriver.termios), GFP_KERNEL);
250 if (!brd->PrintDriver.termios)
254 * Entry points for driver. Called by the kernel from
255 * tty_io.c and n_tty.c.
257 tty_set_operations(&brd->PrintDriver, &dgnc_tty_ops);
259 if (!brd->dgnc_Major_TransparentPrint_Registered) {
260 /* Register Transparent Print devices */
261 rc = tty_register_driver(&brd->PrintDriver);
263 dev_dbg(&brd->pdev->dev,
264 "Can't register Transparent Print device(%d)\n",
268 brd->dgnc_Major_TransparentPrint_Registered = true;
271 dgnc_BoardsByMajor[brd->SerialDriver.major] = brd;
272 brd->dgnc_Serial_Major = brd->SerialDriver.major;
273 brd->dgnc_TransparentPrint_Major = brd->PrintDriver.major;
282 * Init the tty subsystem. Called once per board after board has been
283 * downloaded and init'ed.
285 int dgnc_tty_init(struct dgnc_board *brd)
289 struct channel_t *ch;
295 * Initialize board structure elements.
298 vaddr = brd->re_map_membase;
300 brd->nasync = brd->maxports;
302 for (i = 0; i < brd->nasync; i++) {
304 * Okay to malloc with GFP_KERNEL, we are not at
305 * interrupt context, and there are no locks held.
307 brd->channels[i] = kzalloc(sizeof(*brd->channels[i]),
309 if (!brd->channels[i])
310 goto err_free_channels;
313 ch = brd->channels[0];
314 vaddr = brd->re_map_membase;
316 /* Set up channel variables */
317 for (i = 0; i < brd->nasync; i++, ch = brd->channels[i]) {
318 spin_lock_init(&ch->ch_lock);
320 /* Store all our magic numbers */
321 ch->magic = DGNC_CHANNEL_MAGIC;
322 ch->ch_tun.magic = DGNC_UNIT_MAGIC;
323 ch->ch_tun.un_ch = ch;
324 ch->ch_tun.un_type = DGNC_SERIAL;
325 ch->ch_tun.un_dev = i;
327 ch->ch_pun.magic = DGNC_UNIT_MAGIC;
328 ch->ch_pun.un_ch = ch;
329 ch->ch_pun.un_type = DGNC_PRINT;
330 ch->ch_pun.un_dev = i + 128;
332 if (brd->bd_uart_offset == 0x200)
333 ch->ch_neo_uart = vaddr + (brd->bd_uart_offset * i);
335 ch->ch_cls_uart = vaddr + (brd->bd_uart_offset * i);
339 ch->ch_digi = dgnc_digi_init;
341 /* .25 second delay */
342 ch->ch_close_delay = 250;
344 init_waitqueue_head(&ch->ch_flags_wait);
345 init_waitqueue_head(&ch->ch_tun.un_flags_wait);
346 init_waitqueue_head(&ch->ch_pun.un_flags_wait);
349 struct device *classp;
351 classp = tty_register_device(&brd->SerialDriver, i,
352 &(ch->ch_bd->pdev->dev));
353 ch->ch_tun.un_sysfs = classp;
354 dgnc_create_tty_sysfs(&ch->ch_tun, classp);
356 classp = tty_register_device(&brd->PrintDriver, i,
357 &(ch->ch_bd->pdev->dev));
358 ch->ch_pun.un_sysfs = classp;
359 dgnc_create_tty_sysfs(&ch->ch_pun, classp);
367 for (i = i - 1; i >= 0; --i) {
368 kfree(brd->channels[i]);
369 brd->channels[i] = NULL;
376 * dgnc_tty_post_uninit()
378 * UnInitialize any global tty related data.
380 void dgnc_tty_post_uninit(void)
382 kfree(dgnc_TmpWriteBuf);
383 dgnc_TmpWriteBuf = NULL;
390 * Uninitialize the TTY portion of this driver. Free all memory and
393 void dgnc_tty_uninit(struct dgnc_board *brd)
397 if (brd->dgnc_Major_Serial_Registered) {
398 dgnc_BoardsByMajor[brd->SerialDriver.major] = NULL;
399 brd->dgnc_Serial_Major = 0;
400 for (i = 0; i < brd->nasync; i++) {
401 if (brd->channels[i])
402 dgnc_remove_tty_sysfs(brd->channels[i]->
404 tty_unregister_device(&brd->SerialDriver, i);
406 tty_unregister_driver(&brd->SerialDriver);
407 brd->dgnc_Major_Serial_Registered = false;
410 if (brd->dgnc_Major_TransparentPrint_Registered) {
411 dgnc_BoardsByMajor[brd->PrintDriver.major] = NULL;
412 brd->dgnc_TransparentPrint_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->PrintDriver, i);
419 tty_unregister_driver(&brd->PrintDriver);
420 brd->dgnc_Major_TransparentPrint_Registered = false;
423 kfree(brd->SerialDriver.ttys);
424 brd->SerialDriver.ttys = NULL;
425 kfree(brd->SerialDriver.termios);
426 brd->SerialDriver.termios = NULL;
427 kfree(brd->PrintDriver.ttys);
428 brd->PrintDriver.ttys = NULL;
429 kfree(brd->PrintDriver.termios);
430 brd->PrintDriver.termios = NULL;
433 /*=======================================================================
435 * dgnc_wmove - Write data to transmit queue.
437 * ch - Pointer to channel structure.
438 * buf - Poiter to characters to be moved.
439 * n - Number of characters to move.
441 *=======================================================================*/
442 static void dgnc_wmove(struct channel_t *ch, char *buf, uint n)
447 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
450 head = ch->ch_w_head & WQUEUEMASK;
453 * If the write wraps over the top of the circular buffer,
454 * move the portion up to the wrap point, and reset the
455 * pointers to the bottom.
457 remain = WQUEUESIZE - head;
461 memcpy(ch->ch_wqueue + head, buf, remain);
471 memcpy(ch->ch_wqueue + head, buf, remain);
476 ch->ch_w_head = head;
482 /*=======================================================================
484 * dgnc_input - Process received data.
486 * ch - Pointer to channel structure.
488 *=======================================================================*/
489 void dgnc_input(struct channel_t *ch)
491 struct dgnc_board *bd;
492 struct tty_struct *tp;
493 struct tty_ldisc *ld = NULL;
505 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
508 tp = ch->ch_tun.un_tty;
511 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
514 spin_lock_irqsave(&ch->ch_lock, flags);
517 * Figure the number of characters in the buffer.
518 * Exit immediately if none.
521 head = ch->ch_r_head & rmask;
522 tail = ch->ch_r_tail & rmask;
523 data_len = (head - tail) & rmask;
529 * If the device is not open, or CREAD is off,
530 * flush input data and return immediately.
532 if (!tp || (tp->magic != TTY_MAGIC) || !(ch->ch_tun.un_flags & UN_ISOPEN) ||
533 !(tp->termios.c_cflag & CREAD) || (ch->ch_tun.un_flags & UN_CLOSING)) {
535 ch->ch_r_head = tail;
537 /* Force queue flow control to be released, if needed */
538 dgnc_check_queue_flow_control(ch);
544 * If we are throttled, simply don't read any data.
546 if (ch->ch_flags & CH_FORCED_STOPI)
549 flip_len = TTY_FLIPBUF_SIZE;
551 /* Chop down the length, if needed */
552 len = min(data_len, flip_len);
553 len = min(len, (N_TTY_BUF_SIZE - 1));
555 ld = tty_ldisc_ref(tp);
558 * If we were unable to get a reference to the ld,
559 * don't flush our buffer, and act like the ld doesn't
560 * have any space to put the data right now.
566 * If ld doesn't have a pointer to a receive_buf function,
567 * flush the data, then act like the ld doesn't have any
568 * space to put the data right now.
570 if (!ld->ops->receive_buf) {
571 ch->ch_r_head = ch->ch_r_tail;
580 * The tty layer in the kernel has changed in 2.6.16+.
582 * The flip buffers in the tty structure are no longer exposed,
583 * and probably will be going away eventually.
585 * If we are completely raw, we don't need to go through a lot
586 * of the tty layers that exist.
587 * In this case, we take the shortest and fastest route we
588 * can to relay the data to the user.
590 * On the other hand, if we are not raw, we need to go through
591 * the new 2.6.16+ tty layer, which has its API more well defined.
593 len = tty_buffer_request_room(tp->port, len);
597 * n now contains the most amount of data we can copy,
598 * bounded either by how much the Linux tty layer can handle,
599 * or the amount of data the card actually has pending...
602 s = ((head >= tail) ? head : RQUEUESIZE) - tail;
609 * If conditions are such that ld needs to see all
610 * UART errors, we will have to walk each character
611 * and error byte and send them to the buffer one at
614 if (I_PARMRK(tp) || I_BRKINT(tp) || I_INPCK(tp)) {
615 for (i = 0; i < s; i++) {
616 if (*(ch->ch_equeue + tail + i) & UART_LSR_BI)
617 tty_insert_flip_char(tp->port, *(ch->ch_rqueue + tail + i), TTY_BREAK);
618 else if (*(ch->ch_equeue + tail + i) & UART_LSR_PE)
619 tty_insert_flip_char(tp->port, *(ch->ch_rqueue + tail + i), TTY_PARITY);
620 else if (*(ch->ch_equeue + tail + i) & UART_LSR_FE)
621 tty_insert_flip_char(tp->port, *(ch->ch_rqueue + tail + i), TTY_FRAME);
623 tty_insert_flip_char(tp->port, *(ch->ch_rqueue + tail + i), TTY_NORMAL);
626 tty_insert_flip_string(tp->port, ch->ch_rqueue + tail, s);
631 /* Flip queue if needed */
635 ch->ch_r_tail = tail & rmask;
636 ch->ch_e_tail = tail & rmask;
637 dgnc_check_queue_flow_control(ch);
638 spin_unlock_irqrestore(&ch->ch_lock, flags);
640 /* Tell the tty layer its okay to "eat" the data now */
641 tty_flip_buffer_push(tp->port);
648 spin_unlock_irqrestore(&ch->ch_lock, flags);
654 /************************************************************************
655 * Determines when CARRIER changes state and takes appropriate
657 ************************************************************************/
658 void dgnc_carrier(struct channel_t *ch)
660 struct dgnc_board *bd;
662 int virt_carrier = 0;
663 int phys_carrier = 0;
665 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
670 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
673 if (ch->ch_mistat & UART_MSR_DCD)
676 if (ch->ch_digi.digi_flags & DIGI_FORCEDCD)
679 if (ch->ch_c_cflag & CLOCAL)
683 * Test for a VIRTUAL carrier transition to HIGH.
685 if (((ch->ch_flags & CH_FCAR) == 0) && (virt_carrier == 1)) {
688 * When carrier rises, wake any threads waiting
689 * for carrier in the open routine.
692 if (waitqueue_active(&(ch->ch_flags_wait)))
693 wake_up_interruptible(&ch->ch_flags_wait);
697 * Test for a PHYSICAL carrier transition to HIGH.
699 if (((ch->ch_flags & CH_CD) == 0) && (phys_carrier == 1)) {
702 * When carrier rises, wake any threads waiting
703 * for carrier in the open routine.
706 if (waitqueue_active(&(ch->ch_flags_wait)))
707 wake_up_interruptible(&ch->ch_flags_wait);
711 * Test for a PHYSICAL transition to low, so long as we aren't
712 * currently ignoring physical transitions (which is what "virtual
713 * carrier" indicates).
715 * The transition of the virtual carrier to low really doesn't
716 * matter... it really only means "ignore carrier state", not
717 * "make pretend that carrier is there".
719 if ((virt_carrier == 0) && ((ch->ch_flags & CH_CD) != 0) &&
720 (phys_carrier == 0)) {
723 * When carrier drops:
725 * Drop carrier on all open units.
727 * Flush queues, waking up any task waiting in the
730 * Send a hangup to the control terminal.
732 * Enable all select calls.
734 if (waitqueue_active(&(ch->ch_flags_wait)))
735 wake_up_interruptible(&ch->ch_flags_wait);
737 if (ch->ch_tun.un_open_count > 0)
738 tty_hangup(ch->ch_tun.un_tty);
740 if (ch->ch_pun.un_open_count > 0)
741 tty_hangup(ch->ch_pun.un_tty);
745 * Make sure that our cached values reflect the current reality.
747 if (virt_carrier == 1)
748 ch->ch_flags |= CH_FCAR;
750 ch->ch_flags &= ~CH_FCAR;
752 if (phys_carrier == 1)
753 ch->ch_flags |= CH_CD;
755 ch->ch_flags &= ~CH_CD;
759 * Assign the custom baud rate to the channel structure
761 static void dgnc_set_custom_speed(struct channel_t *ch, uint newrate)
770 ch->ch_custom_speed = 0;
775 * Since the divisor is stored in a 16-bit integer, we make sure
776 * we don't allow any rates smaller than a 16-bit integer would allow.
777 * And of course, rates above the dividend won't fly.
779 if (newrate && newrate < ((ch->ch_bd->bd_dividend / 0xFFFF) + 1))
780 newrate = ((ch->ch_bd->bd_dividend / 0xFFFF) + 1);
782 if (newrate && newrate > ch->ch_bd->bd_dividend)
783 newrate = ch->ch_bd->bd_dividend;
786 testdiv = ch->ch_bd->bd_dividend / newrate;
789 * If we try to figure out what rate the board would use
790 * with the test divisor, it will be either equal or higher
791 * than the requested baud rate. If we then determine the
792 * rate with a divisor one higher, we will get the next lower
793 * supported rate below the requested.
795 testrate_high = ch->ch_bd->bd_dividend / testdiv;
796 testrate_low = ch->ch_bd->bd_dividend / (testdiv + 1);
799 * If the rate for the requested divisor is correct, just
800 * use it and be done.
802 if (testrate_high != newrate) {
804 * Otherwise, pick the rate that is closer (i.e. whichever rate
805 * has a smaller delta).
807 deltahigh = testrate_high - newrate;
808 deltalow = newrate - testrate_low;
810 if (deltahigh < deltalow)
811 newrate = testrate_high;
813 newrate = testrate_low;
817 ch->ch_custom_speed = newrate;
821 void dgnc_check_queue_flow_control(struct channel_t *ch)
825 /* Store how much space we have left in the queue */
826 qleft = ch->ch_r_tail - ch->ch_r_head - 1;
828 qleft += RQUEUEMASK + 1;
831 * Check to see if we should enforce flow control on our queue because
832 * the ld (or user) isn't reading data out of our queue fast enuf.
834 * NOTE: This is done based on what the current flow control of the
837 * 1) HWFLOW (RTS) - Turn off the UART's Receive interrupt.
838 * This will cause the UART's FIFO to back up, and force
839 * the RTS signal to be dropped.
840 * 2) SWFLOW (IXOFF) - Keep trying to send a stop character to
841 * the other side, in hopes it will stop sending data to us.
842 * 3) NONE - Nothing we can do. We will simply drop any extra data
843 * that gets sent into us when the queue fills up.
847 if (ch->ch_digi.digi_flags & CTSPACE || ch->ch_c_cflag & CRTSCTS) {
848 if (!(ch->ch_flags & CH_RECEIVER_OFF)) {
849 ch->ch_bd->bd_ops->disable_receiver(ch);
850 ch->ch_flags |= (CH_RECEIVER_OFF);
854 else if (ch->ch_c_iflag & IXOFF) {
855 if (ch->ch_stops_sent <= MAX_STOPS_SENT) {
856 ch->ch_bd->bd_ops->send_stop_character(ch);
863 * Check to see if we should unenforce flow control because
864 * ld (or user) finally read enuf data out of our queue.
866 * NOTE: This is done based on what the current flow control of the
869 * 1) HWFLOW (RTS) - Turn back on the UART's Receive interrupt.
870 * This will cause the UART's FIFO to raise RTS back up,
871 * which will allow the other side to start sending data again.
872 * 2) SWFLOW (IXOFF) - Send a start character to
873 * the other side, so it will start sending data to us again.
874 * 3) NONE - Do nothing. Since we didn't do anything to turn off the
875 * other side, we don't need to do anything now.
877 if (qleft > (RQUEUESIZE / 2)) {
879 if (ch->ch_digi.digi_flags & RTSPACE || ch->ch_c_cflag & CRTSCTS) {
880 if (ch->ch_flags & CH_RECEIVER_OFF) {
881 ch->ch_bd->bd_ops->enable_receiver(ch);
882 ch->ch_flags &= ~(CH_RECEIVER_OFF);
886 else if (ch->ch_c_iflag & IXOFF && ch->ch_stops_sent) {
887 ch->ch_stops_sent = 0;
888 ch->ch_bd->bd_ops->send_start_character(ch);
894 void dgnc_wakeup_writes(struct channel_t *ch)
899 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
902 spin_lock_irqsave(&ch->ch_lock, flags);
905 * If channel now has space, wake up anyone waiting on the condition.
907 qlen = ch->ch_w_head - ch->ch_w_tail;
911 if (qlen >= (WQUEUESIZE - 256)) {
912 spin_unlock_irqrestore(&ch->ch_lock, flags);
916 if (ch->ch_tun.un_flags & UN_ISOPEN) {
917 if ((ch->ch_tun.un_tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
918 ch->ch_tun.un_tty->ldisc->ops->write_wakeup) {
919 spin_unlock_irqrestore(&ch->ch_lock, flags);
920 (ch->ch_tun.un_tty->ldisc->ops->write_wakeup)(ch->ch_tun.un_tty);
921 spin_lock_irqsave(&ch->ch_lock, flags);
924 wake_up_interruptible(&ch->ch_tun.un_tty->write_wait);
927 * If unit is set to wait until empty, check to make sure
928 * the queue AND FIFO are both empty.
930 if (ch->ch_tun.un_flags & UN_EMPTY) {
931 if ((qlen == 0) && (ch->ch_bd->bd_ops->get_uart_bytes_left(ch) == 0)) {
932 ch->ch_tun.un_flags &= ~(UN_EMPTY);
935 * If RTS Toggle mode is on, whenever
936 * the queue and UART is empty, keep RTS low.
938 if (ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE) {
939 ch->ch_mostat &= ~(UART_MCR_RTS);
940 ch->ch_bd->bd_ops->assert_modem_signals(ch);
944 * If DTR Toggle mode is on, whenever
945 * the queue and UART is empty, keep DTR low.
947 if (ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE) {
948 ch->ch_mostat &= ~(UART_MCR_DTR);
949 ch->ch_bd->bd_ops->assert_modem_signals(ch);
954 wake_up_interruptible(&ch->ch_tun.un_flags_wait);
957 if (ch->ch_pun.un_flags & UN_ISOPEN) {
958 if ((ch->ch_pun.un_tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
959 ch->ch_pun.un_tty->ldisc->ops->write_wakeup) {
960 spin_unlock_irqrestore(&ch->ch_lock, flags);
961 (ch->ch_pun.un_tty->ldisc->ops->write_wakeup)(ch->ch_pun.un_tty);
962 spin_lock_irqsave(&ch->ch_lock, flags);
965 wake_up_interruptible(&ch->ch_pun.un_tty->write_wait);
968 * If unit is set to wait until empty, check to make sure
969 * the queue AND FIFO are both empty.
971 if (ch->ch_pun.un_flags & UN_EMPTY) {
972 if ((qlen == 0) && (ch->ch_bd->bd_ops->get_uart_bytes_left(ch) == 0))
973 ch->ch_pun.un_flags &= ~(UN_EMPTY);
976 wake_up_interruptible(&ch->ch_pun.un_flags_wait);
979 spin_unlock_irqrestore(&ch->ch_lock, flags);
984 /************************************************************************
986 * TTY Entry points and helper functions
988 ************************************************************************/
994 static int dgnc_tty_open(struct tty_struct *tty, struct file *file)
996 struct dgnc_board *brd;
997 struct channel_t *ch;
1002 unsigned long flags;
1006 major = MAJOR(tty_devnum(tty));
1007 minor = MINOR(tty_devnum(tty));
1012 /* Get board pointer from our array of majors we have allocated */
1013 brd = dgnc_BoardsByMajor[major];
1018 * If board is not yet up to a state of READY, go to
1019 * sleep waiting for it to happen or they cancel the open.
1021 rc = wait_event_interruptible(brd->state_wait,
1022 (brd->state & BOARD_READY));
1027 spin_lock_irqsave(&brd->bd_lock, flags);
1029 /* If opened device is greater than our number of ports, bail. */
1030 if (PORT_NUM(minor) >= brd->nasync) {
1031 spin_unlock_irqrestore(&brd->bd_lock, flags);
1035 ch = brd->channels[PORT_NUM(minor)];
1037 spin_unlock_irqrestore(&brd->bd_lock, flags);
1041 /* Drop board lock */
1042 spin_unlock_irqrestore(&brd->bd_lock, flags);
1044 /* Grab channel lock */
1045 spin_lock_irqsave(&ch->ch_lock, flags);
1047 /* Figure out our type */
1048 if (!IS_PRINT(minor)) {
1049 un = &brd->channels[PORT_NUM(minor)]->ch_tun;
1050 un->un_type = DGNC_SERIAL;
1051 } else if (IS_PRINT(minor)) {
1052 un = &brd->channels[PORT_NUM(minor)]->ch_pun;
1053 un->un_type = DGNC_PRINT;
1055 spin_unlock_irqrestore(&ch->ch_lock, flags);
1060 * If the port is still in a previous open, and in a state
1061 * where we simply cannot safely keep going, wait until the
1064 spin_unlock_irqrestore(&ch->ch_lock, flags);
1066 rc = wait_event_interruptible(ch->ch_flags_wait, ((ch->ch_flags & CH_OPENING) == 0));
1068 /* If ret is non-zero, user ctrl-c'ed us */
1073 * If either unit is in the middle of the fragile part of close,
1074 * we just cannot touch the channel safely.
1075 * Go to sleep, knowing that when the channel can be
1076 * touched safely, the close routine will signal the
1077 * ch_flags_wait to wake us back up.
1079 rc = wait_event_interruptible(ch->ch_flags_wait,
1080 (((ch->ch_tun.un_flags | ch->ch_pun.un_flags) & UN_CLOSING) == 0));
1082 /* If ret is non-zero, user ctrl-c'ed us */
1086 spin_lock_irqsave(&ch->ch_lock, flags);
1089 /* Store our unit into driver_data, so we always have it available. */
1090 tty->driver_data = un;
1096 if (!(un->un_flags & UN_ISOPEN)) {
1097 /* Store important variables. */
1100 /* Maybe do something here to the TTY struct as well? */
1105 * Allocate channel buffers for read/write/error.
1106 * Set flag, so we don't get trounced on.
1108 ch->ch_flags |= (CH_OPENING);
1110 /* Drop locks, as malloc with GFP_KERNEL can sleep */
1111 spin_unlock_irqrestore(&ch->ch_lock, flags);
1114 ch->ch_rqueue = kzalloc(RQUEUESIZE, GFP_KERNEL);
1116 ch->ch_equeue = kzalloc(EQUEUESIZE, GFP_KERNEL);
1118 ch->ch_wqueue = kzalloc(WQUEUESIZE, GFP_KERNEL);
1120 spin_lock_irqsave(&ch->ch_lock, flags);
1122 ch->ch_flags &= ~(CH_OPENING);
1123 wake_up_interruptible(&ch->ch_flags_wait);
1126 * Initialize if neither terminal or printer is open.
1128 if (!((ch->ch_tun.un_flags | ch->ch_pun.un_flags) & UN_ISOPEN)) {
1131 * Flush input queues.
1140 brd->bd_ops->flush_uart_write(ch);
1141 brd->bd_ops->flush_uart_read(ch);
1144 ch->ch_cached_lsr = 0;
1145 ch->ch_stop_sending_break = 0;
1146 ch->ch_stops_sent = 0;
1148 ch->ch_c_cflag = tty->termios.c_cflag;
1149 ch->ch_c_iflag = tty->termios.c_iflag;
1150 ch->ch_c_oflag = tty->termios.c_oflag;
1151 ch->ch_c_lflag = tty->termios.c_lflag;
1152 ch->ch_startc = tty->termios.c_cc[VSTART];
1153 ch->ch_stopc = tty->termios.c_cc[VSTOP];
1156 * Bring up RTS and DTR...
1157 * Also handle RTS or DTR toggle if set.
1159 if (!(ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE))
1160 ch->ch_mostat |= (UART_MCR_RTS);
1161 if (!(ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE))
1162 ch->ch_mostat |= (UART_MCR_DTR);
1164 /* Tell UART to init itself */
1165 brd->bd_ops->uart_init(ch);
1169 * Run param in case we changed anything
1171 brd->bd_ops->param(tty);
1176 * follow protocol for opening port
1179 spin_unlock_irqrestore(&ch->ch_lock, flags);
1181 rc = dgnc_block_til_ready(tty, file, ch);
1183 /* No going back now, increment our unit and channel counters */
1184 spin_lock_irqsave(&ch->ch_lock, flags);
1185 ch->ch_open_count++;
1186 un->un_open_count++;
1187 un->un_flags |= (UN_ISOPEN);
1188 spin_unlock_irqrestore(&ch->ch_lock, flags);
1195 * dgnc_block_til_ready()
1197 * Wait for DCD, if needed.
1199 static int dgnc_block_til_ready(struct tty_struct *tty, struct file *file, struct channel_t *ch)
1202 struct un_t *un = NULL;
1203 unsigned long flags;
1205 int sleep_on_un_flags = 0;
1207 if (!tty || tty->magic != TTY_MAGIC || !file || !ch || ch->magic != DGNC_CHANNEL_MAGIC)
1210 un = tty->driver_data;
1211 if (!un || un->magic != DGNC_UNIT_MAGIC)
1214 spin_lock_irqsave(&ch->ch_lock, flags);
1221 sleep_on_un_flags = 0;
1224 * If board has failed somehow during our sleep, bail with error.
1226 if (ch->ch_bd->state == BOARD_FAILED) {
1231 /* If tty was hung up, break out of loop and set error. */
1232 if (tty_hung_up_p(file)) {
1238 * If either unit is in the middle of the fragile part of close,
1239 * we just cannot touch the channel safely.
1240 * Go back to sleep, knowing that when the channel can be
1241 * touched safely, the close routine will signal the
1242 * ch_wait_flags to wake us back up.
1244 if (!((ch->ch_tun.un_flags | ch->ch_pun.un_flags) & UN_CLOSING)) {
1247 * Our conditions to leave cleanly and happily:
1248 * 1) NONBLOCKING on the tty is set.
1250 * 3) DCD (fake or real) is active.
1253 if (file->f_flags & O_NONBLOCK)
1256 if (tty->flags & (1 << TTY_IO_ERROR)) {
1261 if (ch->ch_flags & CH_CD)
1264 if (ch->ch_flags & CH_FCAR)
1267 sleep_on_un_flags = 1;
1271 * If there is a signal pending, the user probably
1272 * interrupted (ctrl-c) us.
1273 * Leave loop with error set.
1275 if (signal_pending(current)) {
1276 retval = -ERESTARTSYS;
1281 * Store the flags before we let go of channel lock
1283 if (sleep_on_un_flags)
1284 old_flags = ch->ch_tun.un_flags | ch->ch_pun.un_flags;
1286 old_flags = ch->ch_flags;
1289 * Let go of channel lock before calling schedule.
1290 * Our poller will get any FEP events and wake us up when DCD
1291 * eventually goes active.
1294 spin_unlock_irqrestore(&ch->ch_lock, flags);
1297 * Wait for something in the flags to change from the current value.
1299 if (sleep_on_un_flags)
1300 retval = wait_event_interruptible(un->un_flags_wait,
1301 (old_flags != (ch->ch_tun.un_flags | ch->ch_pun.un_flags)));
1303 retval = wait_event_interruptible(ch->ch_flags_wait,
1304 (old_flags != ch->ch_flags));
1307 * We got woken up for some reason.
1308 * Before looping around, grab our channel lock.
1310 spin_lock_irqsave(&ch->ch_lock, flags);
1315 spin_unlock_irqrestore(&ch->ch_lock, flags);
1327 * Hangup the port. Like a close, but don't wait for output to drain.
1329 static void dgnc_tty_hangup(struct tty_struct *tty)
1333 if (!tty || tty->magic != TTY_MAGIC)
1336 un = tty->driver_data;
1337 if (!un || un->magic != DGNC_UNIT_MAGIC)
1340 /* flush the transmit queues */
1341 dgnc_tty_flush_buffer(tty);
1350 static void dgnc_tty_close(struct tty_struct *tty, struct file *file)
1352 struct ktermios *ts;
1353 struct dgnc_board *bd;
1354 struct channel_t *ch;
1356 unsigned long flags;
1359 if (!tty || tty->magic != TTY_MAGIC)
1362 un = tty->driver_data;
1363 if (!un || un->magic != DGNC_UNIT_MAGIC)
1367 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1371 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
1376 spin_lock_irqsave(&ch->ch_lock, flags);
1379 * Determine if this is the last close or not - and if we agree about
1380 * which type of close it is with the Line Discipline
1382 if ((tty->count == 1) && (un->un_open_count != 1)) {
1384 * Uh, oh. tty->count is 1, which means that the tty
1385 * structure will be freed. un_open_count should always
1386 * be one in these conditions. If it's greater than
1387 * one, we've got real problems, since it means the
1388 * serial port won't be shutdown.
1391 "tty->count is 1, un open count is %d\n",
1393 un->un_open_count = 1;
1396 if (un->un_open_count)
1397 un->un_open_count--;
1400 "bad serial port open count of %d\n",
1403 ch->ch_open_count--;
1405 if (ch->ch_open_count && un->un_open_count) {
1406 spin_unlock_irqrestore(&ch->ch_lock, flags);
1410 /* OK, its the last close on the unit */
1411 un->un_flags |= UN_CLOSING;
1417 * Only officially close channel if count is 0 and
1418 * DIGI_PRINTER bit is not set.
1420 if ((ch->ch_open_count == 0) && !(ch->ch_digi.digi_flags & DIGI_PRINTER)) {
1422 ch->ch_flags &= ~(CH_STOPI | CH_FORCED_STOPI);
1425 * turn off print device when closing print device.
1427 if ((un->un_type == DGNC_PRINT) && (ch->ch_flags & CH_PRON)) {
1428 dgnc_wmove(ch, ch->ch_digi.digi_offstr,
1429 (int) ch->ch_digi.digi_offlen);
1430 ch->ch_flags &= ~CH_PRON;
1433 spin_unlock_irqrestore(&ch->ch_lock, flags);
1434 /* wait for output to drain */
1435 /* This will also return if we take an interrupt */
1437 rc = bd->bd_ops->drain(tty, 0);
1439 dgnc_tty_flush_buffer(tty);
1440 tty_ldisc_flush(tty);
1442 spin_lock_irqsave(&ch->ch_lock, flags);
1447 * If we have HUPCL set, lower DTR and RTS
1449 if (ch->ch_c_cflag & HUPCL) {
1452 ch->ch_mostat &= ~(UART_MCR_DTR | UART_MCR_RTS);
1453 bd->bd_ops->assert_modem_signals(ch);
1456 * Go to sleep to ensure RTS/DTR
1457 * have been dropped for modems to see it.
1459 if (ch->ch_close_delay) {
1460 spin_unlock_irqrestore(&ch->ch_lock,
1462 dgnc_ms_sleep(ch->ch_close_delay);
1463 spin_lock_irqsave(&ch->ch_lock, flags);
1467 ch->ch_old_baud = 0;
1469 /* Turn off UART interrupts for this port */
1470 ch->ch_bd->bd_ops->uart_off(ch);
1473 * turn off print device when closing print device.
1475 if ((un->un_type == DGNC_PRINT) && (ch->ch_flags & CH_PRON)) {
1476 dgnc_wmove(ch, ch->ch_digi.digi_offstr,
1477 (int) ch->ch_digi.digi_offlen);
1478 ch->ch_flags &= ~CH_PRON;
1483 un->un_flags &= ~(UN_ISOPEN | UN_CLOSING);
1485 wake_up_interruptible(&ch->ch_flags_wait);
1486 wake_up_interruptible(&un->un_flags_wait);
1488 spin_unlock_irqrestore(&ch->ch_lock, flags);
1493 * dgnc_tty_chars_in_buffer()
1495 * Return number of characters that have not been transmitted yet.
1497 * This routine is used by the line discipline to determine if there
1498 * is data waiting to be transmitted/drained/flushed or not.
1500 static int dgnc_tty_chars_in_buffer(struct tty_struct *tty)
1502 struct channel_t *ch = NULL;
1503 struct un_t *un = NULL;
1508 unsigned long flags;
1513 un = tty->driver_data;
1514 if (!un || un->magic != DGNC_UNIT_MAGIC)
1518 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1521 spin_lock_irqsave(&ch->ch_lock, flags);
1524 thead = ch->ch_w_head & tmask;
1525 ttail = ch->ch_w_tail & tmask;
1527 spin_unlock_irqrestore(&ch->ch_lock, flags);
1529 if (ttail == thead) {
1533 chars = thead - ttail;
1535 chars = thead - ttail + WQUEUESIZE;
1545 * Reduces bytes_available to the max number of characters
1546 * that can be sent currently given the maxcps value, and
1547 * returns the new bytes_available. This only affects printer
1550 static int dgnc_maxcps_room(struct tty_struct *tty, int bytes_available)
1552 struct channel_t *ch = NULL;
1553 struct un_t *un = NULL;
1556 return bytes_available;
1558 un = tty->driver_data;
1559 if (!un || un->magic != DGNC_UNIT_MAGIC)
1560 return bytes_available;
1563 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1564 return bytes_available;
1567 * If its not the Transparent print device, return
1568 * the full data amount.
1570 if (un->un_type != DGNC_PRINT)
1571 return bytes_available;
1573 if (ch->ch_digi.digi_maxcps > 0 && ch->ch_digi.digi_bufsize > 0) {
1575 unsigned long current_time = jiffies;
1576 unsigned long buffer_time = current_time +
1577 (HZ * ch->ch_digi.digi_bufsize) / ch->ch_digi.digi_maxcps;
1579 if (ch->ch_cpstime < current_time) {
1580 /* buffer is empty */
1581 ch->ch_cpstime = current_time; /* reset ch_cpstime */
1582 cps_limit = ch->ch_digi.digi_bufsize;
1583 } else if (ch->ch_cpstime < buffer_time) {
1584 /* still room in the buffer */
1585 cps_limit = ((buffer_time - ch->ch_cpstime) * ch->ch_digi.digi_maxcps) / HZ;
1587 /* no room in the buffer */
1591 bytes_available = min(cps_limit, bytes_available);
1594 return bytes_available;
1599 * dgnc_tty_write_room()
1601 * Return space available in Tx buffer
1603 static int dgnc_tty_write_room(struct tty_struct *tty)
1605 struct channel_t *ch = NULL;
1606 struct un_t *un = NULL;
1611 unsigned long flags;
1613 if (tty == NULL || dgnc_TmpWriteBuf == NULL)
1616 un = tty->driver_data;
1617 if (!un || un->magic != DGNC_UNIT_MAGIC)
1621 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1624 spin_lock_irqsave(&ch->ch_lock, flags);
1627 head = (ch->ch_w_head) & tmask;
1628 tail = (ch->ch_w_tail) & tmask;
1630 ret = tail - head - 1;
1634 /* Limit printer to maxcps */
1635 ret = dgnc_maxcps_room(tty, ret);
1638 * If we are printer device, leave space for
1639 * possibly both the on and off strings.
1641 if (un->un_type == DGNC_PRINT) {
1642 if (!(ch->ch_flags & CH_PRON))
1643 ret -= ch->ch_digi.digi_onlen;
1644 ret -= ch->ch_digi.digi_offlen;
1646 if (ch->ch_flags & CH_PRON)
1647 ret -= ch->ch_digi.digi_offlen;
1653 spin_unlock_irqrestore(&ch->ch_lock, flags);
1660 * dgnc_tty_put_char()
1662 * Put a character into ch->ch_buf
1664 * - used by the line discipline for OPOST processing
1666 static int dgnc_tty_put_char(struct tty_struct *tty, unsigned char c)
1669 * Simply call tty_write.
1671 dgnc_tty_write(tty, &c, 1);
1679 * Take data from the user or kernel and send it out to the FEP.
1680 * In here exists all the Transparent Print magic as well.
1682 static int dgnc_tty_write(struct tty_struct *tty,
1683 const unsigned char *buf, int count)
1685 struct channel_t *ch = NULL;
1686 struct un_t *un = NULL;
1687 int bufcount = 0, n = 0;
1689 unsigned long flags;
1695 if (tty == NULL || dgnc_TmpWriteBuf == NULL)
1698 un = tty->driver_data;
1699 if (!un || un->magic != DGNC_UNIT_MAGIC)
1703 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1710 * Store original amount of characters passed in.
1711 * This helps to figure out if we should ask the FEP
1712 * to send us an event when it has more space available.
1716 spin_lock_irqsave(&ch->ch_lock, flags);
1718 /* Get our space available for the channel from the board */
1720 head = (ch->ch_w_head) & tmask;
1721 tail = (ch->ch_w_tail) & tmask;
1723 bufcount = tail - head - 1;
1725 bufcount += WQUEUESIZE;
1728 * Limit printer output to maxcps overall, with bursts allowed
1729 * up to bufsize characters.
1731 bufcount = dgnc_maxcps_room(tty, bufcount);
1734 * Take minimum of what the user wants to send, and the
1735 * space available in the FEP buffer.
1737 count = min(count, bufcount);
1740 * Bail if no space left.
1746 * Output the printer ON string, if we are in terminal mode, but
1747 * need to be in printer mode.
1749 if ((un->un_type == DGNC_PRINT) && !(ch->ch_flags & CH_PRON)) {
1750 dgnc_wmove(ch, ch->ch_digi.digi_onstr,
1751 (int) ch->ch_digi.digi_onlen);
1752 head = (ch->ch_w_head) & tmask;
1753 ch->ch_flags |= CH_PRON;
1757 * On the other hand, output the printer OFF string, if we are
1758 * currently in printer mode, but need to output to the terminal.
1760 if ((un->un_type != DGNC_PRINT) && (ch->ch_flags & CH_PRON)) {
1761 dgnc_wmove(ch, ch->ch_digi.digi_offstr,
1762 (int) ch->ch_digi.digi_offlen);
1763 head = (ch->ch_w_head) & tmask;
1764 ch->ch_flags &= ~CH_PRON;
1770 * If the write wraps over the top of the circular buffer,
1771 * move the portion up to the wrap point, and reset the
1772 * pointers to the bottom.
1774 remain = WQUEUESIZE - head;
1778 memcpy(ch->ch_wqueue + head, buf, remain);
1785 * Move rest of data.
1788 memcpy(ch->ch_wqueue + head, buf, remain);
1794 ch->ch_w_head = head;
1797 /* Update printer buffer empty time. */
1798 if ((un->un_type == DGNC_PRINT) && (ch->ch_digi.digi_maxcps > 0)
1799 && (ch->ch_digi.digi_bufsize > 0)) {
1800 ch->ch_cpstime += (HZ * count) / ch->ch_digi.digi_maxcps;
1803 spin_unlock_irqrestore(&ch->ch_lock, flags);
1807 * Channel lock is grabbed and then released
1808 * inside this routine.
1810 ch->ch_bd->bd_ops->copy_data_from_queue_to_uart(ch);
1817 spin_unlock_irqrestore(&ch->ch_lock, flags);
1823 * Return modem signals to ld.
1826 static int dgnc_tty_tiocmget(struct tty_struct *tty)
1828 struct channel_t *ch;
1831 unsigned char mstat = 0;
1832 unsigned long flags;
1834 if (!tty || tty->magic != TTY_MAGIC)
1837 un = tty->driver_data;
1838 if (!un || un->magic != DGNC_UNIT_MAGIC)
1842 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1845 spin_lock_irqsave(&ch->ch_lock, flags);
1847 mstat = (ch->ch_mostat | ch->ch_mistat);
1849 spin_unlock_irqrestore(&ch->ch_lock, flags);
1853 if (mstat & UART_MCR_DTR)
1854 result |= TIOCM_DTR;
1855 if (mstat & UART_MCR_RTS)
1856 result |= TIOCM_RTS;
1857 if (mstat & UART_MSR_CTS)
1858 result |= TIOCM_CTS;
1859 if (mstat & UART_MSR_DSR)
1860 result |= TIOCM_DSR;
1861 if (mstat & UART_MSR_RI)
1863 if (mstat & UART_MSR_DCD)
1871 * dgnc_tty_tiocmset()
1873 * Set modem signals, called by ld.
1876 static int dgnc_tty_tiocmset(struct tty_struct *tty,
1877 unsigned int set, unsigned int clear)
1879 struct dgnc_board *bd;
1880 struct channel_t *ch;
1883 unsigned long flags;
1885 if (!tty || tty->magic != TTY_MAGIC)
1888 un = tty->driver_data;
1889 if (!un || un->magic != DGNC_UNIT_MAGIC)
1893 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1897 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
1900 spin_lock_irqsave(&ch->ch_lock, flags);
1902 if (set & TIOCM_RTS)
1903 ch->ch_mostat |= UART_MCR_RTS;
1905 if (set & TIOCM_DTR)
1906 ch->ch_mostat |= UART_MCR_DTR;
1908 if (clear & TIOCM_RTS)
1909 ch->ch_mostat &= ~(UART_MCR_RTS);
1911 if (clear & TIOCM_DTR)
1912 ch->ch_mostat &= ~(UART_MCR_DTR);
1914 ch->ch_bd->bd_ops->assert_modem_signals(ch);
1916 spin_unlock_irqrestore(&ch->ch_lock, flags);
1923 * dgnc_tty_send_break()
1925 * Send a Break, called by ld.
1927 static int dgnc_tty_send_break(struct tty_struct *tty, int msec)
1929 struct dgnc_board *bd;
1930 struct channel_t *ch;
1933 unsigned long flags;
1935 if (!tty || tty->magic != TTY_MAGIC)
1938 un = tty->driver_data;
1939 if (!un || un->magic != DGNC_UNIT_MAGIC)
1943 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1947 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
1961 spin_lock_irqsave(&ch->ch_lock, flags);
1963 ch->ch_bd->bd_ops->send_break(ch, msec);
1965 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;
1984 if (!tty || tty->magic != TTY_MAGIC)
1987 un = tty->driver_data;
1988 if (!un || un->magic != DGNC_UNIT_MAGIC)
1992 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1996 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
1999 rc = bd->bd_ops->drain(tty, 0);
2006 * send a high priority character, called by ld.
2008 static void dgnc_tty_send_xchar(struct tty_struct *tty, char c)
2010 struct dgnc_board *bd;
2011 struct channel_t *ch;
2013 unsigned long flags;
2015 if (!tty || tty->magic != TTY_MAGIC)
2018 un = tty->driver_data;
2019 if (!un || un->magic != DGNC_UNIT_MAGIC)
2023 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2027 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2030 dev_dbg(tty->dev, "dgnc_tty_send_xchar start\n");
2032 spin_lock_irqsave(&ch->ch_lock, flags);
2033 bd->bd_ops->send_immediate_char(ch, c);
2034 spin_unlock_irqrestore(&ch->ch_lock, flags);
2036 dev_dbg(tty->dev, "dgnc_tty_send_xchar finish\n");
2043 * Return modem signals to ld.
2045 static inline int dgnc_get_mstat(struct channel_t *ch)
2047 unsigned char mstat;
2049 unsigned long flags;
2051 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2054 spin_lock_irqsave(&ch->ch_lock, flags);
2056 mstat = (ch->ch_mostat | ch->ch_mistat);
2058 spin_unlock_irqrestore(&ch->ch_lock, flags);
2062 if (mstat & UART_MCR_DTR)
2063 result |= TIOCM_DTR;
2064 if (mstat & UART_MCR_RTS)
2065 result |= TIOCM_RTS;
2066 if (mstat & UART_MSR_CTS)
2067 result |= TIOCM_CTS;
2068 if (mstat & UART_MSR_DSR)
2069 result |= TIOCM_DSR;
2070 if (mstat & UART_MSR_RI)
2072 if (mstat & UART_MSR_DCD)
2081 * Return modem signals to ld.
2083 static int dgnc_get_modem_info(struct channel_t *ch, unsigned int __user *value)
2087 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2090 result = dgnc_get_mstat(ch);
2095 return put_user(result, value);
2100 * dgnc_set_modem_info()
2102 * Set modem signals, called by ld.
2104 static int dgnc_set_modem_info(struct tty_struct *tty, unsigned int command, unsigned int __user *value)
2106 struct dgnc_board *bd;
2107 struct channel_t *ch;
2110 unsigned int arg = 0;
2111 unsigned long flags;
2113 if (!tty || tty->magic != TTY_MAGIC)
2116 un = tty->driver_data;
2117 if (!un || un->magic != DGNC_UNIT_MAGIC)
2121 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2125 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2128 ret = get_user(arg, value);
2134 if (arg & TIOCM_RTS)
2135 ch->ch_mostat |= UART_MCR_RTS;
2137 if (arg & TIOCM_DTR)
2138 ch->ch_mostat |= UART_MCR_DTR;
2143 if (arg & TIOCM_RTS)
2144 ch->ch_mostat &= ~(UART_MCR_RTS);
2146 if (arg & TIOCM_DTR)
2147 ch->ch_mostat &= ~(UART_MCR_DTR);
2153 if (arg & TIOCM_RTS)
2154 ch->ch_mostat |= UART_MCR_RTS;
2156 ch->ch_mostat &= ~(UART_MCR_RTS);
2158 if (arg & TIOCM_DTR)
2159 ch->ch_mostat |= UART_MCR_DTR;
2161 ch->ch_mostat &= ~(UART_MCR_DTR);
2169 spin_lock_irqsave(&ch->ch_lock, flags);
2171 ch->ch_bd->bd_ops->assert_modem_signals(ch);
2173 spin_unlock_irqrestore(&ch->ch_lock, flags);
2180 * dgnc_tty_digigeta()
2182 * Ioctl to get the information for ditty.
2187 static int dgnc_tty_digigeta(struct tty_struct *tty, struct digi_t __user *retinfo)
2189 struct channel_t *ch;
2192 unsigned long flags;
2197 if (!tty || tty->magic != TTY_MAGIC)
2200 un = tty->driver_data;
2201 if (!un || un->magic != DGNC_UNIT_MAGIC)
2205 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2208 memset(&tmp, 0, sizeof(tmp));
2210 spin_lock_irqsave(&ch->ch_lock, flags);
2211 memcpy(&tmp, &ch->ch_digi, sizeof(tmp));
2212 spin_unlock_irqrestore(&ch->ch_lock, flags);
2214 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2222 * dgnc_tty_digiseta()
2224 * Ioctl to set the information for ditty.
2229 static int dgnc_tty_digiseta(struct tty_struct *tty, struct digi_t __user *new_info)
2231 struct dgnc_board *bd;
2232 struct channel_t *ch;
2234 struct digi_t new_digi;
2235 unsigned long flags;
2237 if (!tty || tty->magic != TTY_MAGIC)
2240 un = tty->driver_data;
2241 if (!un || un->magic != DGNC_UNIT_MAGIC)
2245 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2249 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2252 if (copy_from_user(&new_digi, new_info, sizeof(new_digi)))
2255 spin_lock_irqsave(&ch->ch_lock, flags);
2258 * Handle transistions to and from RTS Toggle.
2260 if (!(ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE) && (new_digi.digi_flags & DIGI_RTS_TOGGLE))
2261 ch->ch_mostat &= ~(UART_MCR_RTS);
2262 if ((ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE) && !(new_digi.digi_flags & DIGI_RTS_TOGGLE))
2263 ch->ch_mostat |= (UART_MCR_RTS);
2266 * Handle transistions to and from DTR Toggle.
2268 if (!(ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE) && (new_digi.digi_flags & DIGI_DTR_TOGGLE))
2269 ch->ch_mostat &= ~(UART_MCR_DTR);
2270 if ((ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE) && !(new_digi.digi_flags & DIGI_DTR_TOGGLE))
2271 ch->ch_mostat |= (UART_MCR_DTR);
2273 memcpy(&ch->ch_digi, &new_digi, sizeof(new_digi));
2275 if (ch->ch_digi.digi_maxcps < 1)
2276 ch->ch_digi.digi_maxcps = 1;
2278 if (ch->ch_digi.digi_maxcps > 10000)
2279 ch->ch_digi.digi_maxcps = 10000;
2281 if (ch->ch_digi.digi_bufsize < 10)
2282 ch->ch_digi.digi_bufsize = 10;
2284 if (ch->ch_digi.digi_maxchar < 1)
2285 ch->ch_digi.digi_maxchar = 1;
2287 if (ch->ch_digi.digi_maxchar > ch->ch_digi.digi_bufsize)
2288 ch->ch_digi.digi_maxchar = ch->ch_digi.digi_bufsize;
2290 if (ch->ch_digi.digi_onlen > DIGI_PLEN)
2291 ch->ch_digi.digi_onlen = DIGI_PLEN;
2293 if (ch->ch_digi.digi_offlen > DIGI_PLEN)
2294 ch->ch_digi.digi_offlen = DIGI_PLEN;
2296 ch->ch_bd->bd_ops->param(tty);
2298 spin_unlock_irqrestore(&ch->ch_lock, flags);
2305 * dgnc_set_termios()
2307 static void dgnc_tty_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
2309 struct dgnc_board *bd;
2310 struct channel_t *ch;
2312 unsigned long flags;
2314 if (!tty || tty->magic != TTY_MAGIC)
2317 un = tty->driver_data;
2318 if (!un || un->magic != DGNC_UNIT_MAGIC)
2322 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2326 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2329 spin_lock_irqsave(&ch->ch_lock, flags);
2331 ch->ch_c_cflag = tty->termios.c_cflag;
2332 ch->ch_c_iflag = tty->termios.c_iflag;
2333 ch->ch_c_oflag = tty->termios.c_oflag;
2334 ch->ch_c_lflag = tty->termios.c_lflag;
2335 ch->ch_startc = tty->termios.c_cc[VSTART];
2336 ch->ch_stopc = tty->termios.c_cc[VSTOP];
2338 ch->ch_bd->bd_ops->param(tty);
2341 spin_unlock_irqrestore(&ch->ch_lock, flags);
2345 static void dgnc_tty_throttle(struct tty_struct *tty)
2347 struct channel_t *ch;
2349 unsigned long flags;
2351 if (!tty || tty->magic != TTY_MAGIC)
2354 un = tty->driver_data;
2355 if (!un || un->magic != DGNC_UNIT_MAGIC)
2359 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2362 spin_lock_irqsave(&ch->ch_lock, flags);
2364 ch->ch_flags |= (CH_FORCED_STOPI);
2366 spin_unlock_irqrestore(&ch->ch_lock, flags);
2370 static void dgnc_tty_unthrottle(struct tty_struct *tty)
2372 struct channel_t *ch;
2374 unsigned long flags;
2376 if (!tty || tty->magic != TTY_MAGIC)
2379 un = tty->driver_data;
2380 if (!un || un->magic != DGNC_UNIT_MAGIC)
2384 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2387 spin_lock_irqsave(&ch->ch_lock, flags);
2389 ch->ch_flags &= ~(CH_FORCED_STOPI);
2391 spin_unlock_irqrestore(&ch->ch_lock, flags);
2395 static void dgnc_tty_start(struct tty_struct *tty)
2397 struct dgnc_board *bd;
2398 struct channel_t *ch;
2400 unsigned long flags;
2402 if (!tty || tty->magic != TTY_MAGIC)
2405 un = tty->driver_data;
2406 if (!un || un->magic != DGNC_UNIT_MAGIC)
2410 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2414 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2417 spin_lock_irqsave(&ch->ch_lock, flags);
2419 ch->ch_flags &= ~(CH_FORCED_STOP);
2421 spin_unlock_irqrestore(&ch->ch_lock, flags);
2425 static void dgnc_tty_stop(struct tty_struct *tty)
2427 struct dgnc_board *bd;
2428 struct channel_t *ch;
2430 unsigned long flags;
2432 if (!tty || tty->magic != TTY_MAGIC)
2435 un = tty->driver_data;
2436 if (!un || un->magic != DGNC_UNIT_MAGIC)
2440 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2444 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2447 spin_lock_irqsave(&ch->ch_lock, flags);
2449 ch->ch_flags |= (CH_FORCED_STOP);
2451 spin_unlock_irqrestore(&ch->ch_lock, flags);
2456 * dgnc_tty_flush_chars()
2458 * Flush the cook buffer
2460 * Note to self, and any other poor souls who venture here:
2462 * flush in this case DOES NOT mean dispose of the data.
2463 * instead, it means "stop buffering and send it if you
2464 * haven't already." Just guess how I figured that out... SRW 2-Jun-98
2466 * It is also always called in interrupt context - JAR 8-Sept-99
2468 static void dgnc_tty_flush_chars(struct tty_struct *tty)
2470 struct dgnc_board *bd;
2471 struct channel_t *ch;
2473 unsigned long flags;
2475 if (!tty || tty->magic != TTY_MAGIC)
2478 un = tty->driver_data;
2479 if (!un || un->magic != DGNC_UNIT_MAGIC)
2483 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2487 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2490 spin_lock_irqsave(&ch->ch_lock, flags);
2492 /* Do something maybe here */
2494 spin_unlock_irqrestore(&ch->ch_lock, flags);
2500 * dgnc_tty_flush_buffer()
2502 * Flush Tx buffer (make in == out)
2504 static void dgnc_tty_flush_buffer(struct tty_struct *tty)
2506 struct channel_t *ch;
2508 unsigned long flags;
2510 if (!tty || tty->magic != TTY_MAGIC)
2513 un = tty->driver_data;
2514 if (!un || un->magic != DGNC_UNIT_MAGIC)
2518 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2521 spin_lock_irqsave(&ch->ch_lock, flags);
2523 ch->ch_flags &= ~CH_STOP;
2525 /* Flush our write queue */
2526 ch->ch_w_head = ch->ch_w_tail;
2528 /* Flush UARTs transmit FIFO */
2529 ch->ch_bd->bd_ops->flush_uart_write(ch);
2531 if (ch->ch_tun.un_flags & (UN_LOW|UN_EMPTY)) {
2532 ch->ch_tun.un_flags &= ~(UN_LOW|UN_EMPTY);
2533 wake_up_interruptible(&ch->ch_tun.un_flags_wait);
2535 if (ch->ch_pun.un_flags & (UN_LOW|UN_EMPTY)) {
2536 ch->ch_pun.un_flags &= ~(UN_LOW|UN_EMPTY);
2537 wake_up_interruptible(&ch->ch_pun.un_flags_wait);
2540 spin_unlock_irqrestore(&ch->ch_lock, flags);
2545 /*****************************************************************************
2547 * The IOCTL function and all of its helpers
2549 *****************************************************************************/
2554 * The usual assortment of ioctl's
2556 static int dgnc_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
2559 struct dgnc_board *bd;
2560 struct channel_t *ch;
2563 unsigned long flags;
2564 void __user *uarg = (void __user *) arg;
2566 if (!tty || tty->magic != TTY_MAGIC)
2569 un = tty->driver_data;
2570 if (!un || un->magic != DGNC_UNIT_MAGIC)
2574 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2578 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2581 spin_lock_irqsave(&ch->ch_lock, flags);
2583 if (un->un_open_count <= 0) {
2584 spin_unlock_irqrestore(&ch->ch_lock, flags);
2590 /* Here are all the standard ioctl's that we MUST implement */
2594 * TCSBRK is SVID version: non-zero arg --> no break
2595 * this behaviour is exploited by tcdrain().
2597 * According to POSIX.1 spec (7.2.2.1.2) breaks should be
2598 * between 0.25 and 0.5 seconds so we'll ask for something
2599 * in the middle: 0.375 seconds.
2601 rc = tty_check_change(tty);
2602 spin_unlock_irqrestore(&ch->ch_lock, flags);
2606 rc = ch->ch_bd->bd_ops->drain(tty, 0);
2611 spin_lock_irqsave(&ch->ch_lock, flags);
2613 if (((cmd == TCSBRK) && (!arg)) || (cmd == TCSBRKP))
2614 ch->ch_bd->bd_ops->send_break(ch, 250);
2616 spin_unlock_irqrestore(&ch->ch_lock, flags);
2622 /* support for POSIX tcsendbreak()
2623 * According to POSIX.1 spec (7.2.2.1.2) breaks should be
2624 * between 0.25 and 0.5 seconds so we'll ask for something
2625 * in the middle: 0.375 seconds.
2627 rc = tty_check_change(tty);
2628 spin_unlock_irqrestore(&ch->ch_lock, flags);
2632 rc = ch->ch_bd->bd_ops->drain(tty, 0);
2636 spin_lock_irqsave(&ch->ch_lock, flags);
2638 ch->ch_bd->bd_ops->send_break(ch, 250);
2640 spin_unlock_irqrestore(&ch->ch_lock, flags);
2645 rc = tty_check_change(tty);
2646 spin_unlock_irqrestore(&ch->ch_lock, flags);
2650 rc = ch->ch_bd->bd_ops->drain(tty, 0);
2654 spin_lock_irqsave(&ch->ch_lock, flags);
2656 ch->ch_bd->bd_ops->send_break(ch, 250);
2658 spin_unlock_irqrestore(&ch->ch_lock, flags);
2664 spin_unlock_irqrestore(&ch->ch_lock, flags);
2669 spin_unlock_irqrestore(&ch->ch_lock, flags);
2671 rc = put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *) arg);
2676 spin_unlock_irqrestore(&ch->ch_lock, flags);
2677 rc = get_user(arg, (unsigned long __user *) arg);
2681 spin_lock_irqsave(&ch->ch_lock, flags);
2682 tty->termios.c_cflag = ((tty->termios.c_cflag & ~CLOCAL) | (arg ? CLOCAL : 0));
2683 ch->ch_bd->bd_ops->param(tty);
2684 spin_unlock_irqrestore(&ch->ch_lock, flags);
2689 spin_unlock_irqrestore(&ch->ch_lock, flags);
2690 return dgnc_get_modem_info(ch, uarg);
2695 spin_unlock_irqrestore(&ch->ch_lock, flags);
2696 return dgnc_set_modem_info(tty, cmd, uarg);
2699 * Here are any additional ioctl's that we want to implement
2704 * The linux tty driver doesn't have a flush
2705 * input routine for the driver, assuming all backed
2706 * up data is in the line disc. buffers. However,
2707 * we all know that's not the case. Here, we
2708 * act on the ioctl, but then lie and say we didn't
2709 * so the line discipline will process the flush
2712 rc = tty_check_change(tty);
2714 spin_unlock_irqrestore(&ch->ch_lock, flags);
2718 if ((arg == TCIFLUSH) || (arg == TCIOFLUSH)) {
2719 ch->ch_r_head = ch->ch_r_tail;
2720 ch->ch_bd->bd_ops->flush_uart_read(ch);
2721 /* Force queue flow control to be released, if needed */
2722 dgnc_check_queue_flow_control(ch);
2725 if ((arg == TCOFLUSH) || (arg == TCIOFLUSH)) {
2726 if (!(un->un_type == DGNC_PRINT)) {
2727 ch->ch_w_head = ch->ch_w_tail;
2728 ch->ch_bd->bd_ops->flush_uart_write(ch);
2730 if (ch->ch_tun.un_flags & (UN_LOW|UN_EMPTY)) {
2731 ch->ch_tun.un_flags &= ~(UN_LOW|UN_EMPTY);
2732 wake_up_interruptible(&ch->ch_tun.un_flags_wait);
2735 if (ch->ch_pun.un_flags & (UN_LOW|UN_EMPTY)) {
2736 ch->ch_pun.un_flags &= ~(UN_LOW|UN_EMPTY);
2737 wake_up_interruptible(&ch->ch_pun.un_flags_wait);
2743 /* pretend we didn't recognize this IOCTL */
2744 spin_unlock_irqrestore(&ch->ch_lock, flags);
2745 return -ENOIOCTLCMD;
2749 * The linux tty driver doesn't have a flush
2750 * input routine for the driver, assuming all backed
2751 * up data is in the line disc. buffers. However,
2752 * we all know that's not the case. Here, we
2753 * act on the ioctl, but then lie and say we didn't
2754 * so the line discipline will process the flush
2757 if (cmd == TCSETSF) {
2759 ch->ch_flags &= ~CH_STOP;
2760 ch->ch_r_head = ch->ch_r_tail;
2761 ch->ch_bd->bd_ops->flush_uart_read(ch);
2762 /* Force queue flow control to be released, if needed */
2763 dgnc_check_queue_flow_control(ch);
2766 /* now wait for all the output to drain */
2767 spin_unlock_irqrestore(&ch->ch_lock, flags);
2768 rc = ch->ch_bd->bd_ops->drain(tty, 0);
2772 /* pretend we didn't recognize this */
2773 return -ENOIOCTLCMD;
2777 spin_unlock_irqrestore(&ch->ch_lock, flags);
2778 rc = ch->ch_bd->bd_ops->drain(tty, 0);
2782 /* pretend we didn't recognize this */
2783 return -ENOIOCTLCMD;
2786 spin_unlock_irqrestore(&ch->ch_lock, flags);
2787 /* Make the ld do it */
2788 return -ENOIOCTLCMD;
2791 /* get information for ditty */
2792 spin_unlock_irqrestore(&ch->ch_lock, flags);
2793 return dgnc_tty_digigeta(tty, uarg);
2798 /* set information for ditty */
2799 if (cmd == (DIGI_SETAW)) {
2801 spin_unlock_irqrestore(&ch->ch_lock, flags);
2802 rc = ch->ch_bd->bd_ops->drain(tty, 0);
2807 spin_lock_irqsave(&ch->ch_lock, flags);
2809 tty_ldisc_flush(tty);
2814 spin_unlock_irqrestore(&ch->ch_lock, flags);
2815 return dgnc_tty_digiseta(tty, uarg);
2820 /* Let go of locks when accessing user space, could sleep */
2821 spin_unlock_irqrestore(&ch->ch_lock, flags);
2822 rc = get_user(loopback, (unsigned int __user *) arg);
2825 spin_lock_irqsave(&ch->ch_lock, flags);
2827 /* Enable/disable internal loopback for this port */
2829 ch->ch_flags |= CH_LOOPBACK;
2831 ch->ch_flags &= ~(CH_LOOPBACK);
2833 ch->ch_bd->bd_ops->param(tty);
2834 spin_unlock_irqrestore(&ch->ch_lock, flags);
2838 case DIGI_GETCUSTOMBAUD:
2839 spin_unlock_irqrestore(&ch->ch_lock, flags);
2840 rc = put_user(ch->ch_custom_speed, (unsigned int __user *) arg);
2843 case DIGI_SETCUSTOMBAUD:
2846 /* Let go of locks when accessing user space, could sleep */
2847 spin_unlock_irqrestore(&ch->ch_lock, flags);
2848 rc = get_user(new_rate, (int __user *) arg);
2851 spin_lock_irqsave(&ch->ch_lock, flags);
2852 dgnc_set_custom_speed(ch, new_rate);
2853 ch->ch_bd->bd_ops->param(tty);
2854 spin_unlock_irqrestore(&ch->ch_lock, flags);
2859 * This ioctl allows insertion of a character into the front
2860 * of any pending data to be transmitted.
2862 * This ioctl is to satify the "Send Character Immediate"
2863 * call that the RealPort protocol spec requires.
2865 case DIGI_REALPORT_SENDIMMEDIATE:
2869 spin_unlock_irqrestore(&ch->ch_lock, flags);
2870 rc = get_user(c, (unsigned char __user *) arg);
2873 spin_lock_irqsave(&ch->ch_lock, flags);
2874 ch->ch_bd->bd_ops->send_immediate_char(ch, c);
2875 spin_unlock_irqrestore(&ch->ch_lock, flags);
2880 * This ioctl returns all the current counts for the port.
2882 * This ioctl is to satify the "Line Error Counters"
2883 * call that the RealPort protocol spec requires.
2885 case DIGI_REALPORT_GETCOUNTERS:
2887 struct digi_getcounter buf;
2889 buf.norun = ch->ch_err_overrun;
2890 buf.noflow = 0; /* The driver doesn't keep this stat */
2891 buf.nframe = ch->ch_err_frame;
2892 buf.nparity = ch->ch_err_parity;
2893 buf.nbreak = ch->ch_err_break;
2894 buf.rbytes = ch->ch_rxcount;
2895 buf.tbytes = ch->ch_txcount;
2897 spin_unlock_irqrestore(&ch->ch_lock, flags);
2899 if (copy_to_user(uarg, &buf, sizeof(buf)))
2906 * This ioctl returns all current events.
2908 * This ioctl is to satify the "Event Reporting"
2909 * call that the RealPort protocol spec requires.
2911 case DIGI_REALPORT_GETEVENTS:
2913 unsigned int events = 0;
2915 /* NOTE: MORE EVENTS NEEDS TO BE ADDED HERE */
2916 if (ch->ch_flags & CH_BREAK_SENDING)
2918 if ((ch->ch_flags & CH_STOP) || (ch->ch_flags & CH_FORCED_STOP))
2919 events |= (EV_OPU | EV_OPS);
2921 if ((ch->ch_flags & CH_STOPI) || (ch->ch_flags & CH_FORCED_STOPI))
2922 events |= (EV_IPU | EV_IPS);
2924 spin_unlock_irqrestore(&ch->ch_lock, flags);
2925 rc = put_user(events, (unsigned int __user *) arg);
2930 * This ioctl returns TOUT and TIN counters based
2931 * upon the values passed in by the RealPort Server.
2932 * It also passes back whether the UART Transmitter is
2935 case DIGI_REALPORT_GETBUFFERS:
2937 struct digi_getbuffer buf;
2941 spin_unlock_irqrestore(&ch->ch_lock, flags);
2944 * Get data from user first.
2946 if (copy_from_user(&buf, uarg, sizeof(buf)))
2949 spin_lock_irqsave(&ch->ch_lock, flags);
2952 * Figure out how much data is in our RX and TX queues.
2954 buf.rxbuf = (ch->ch_r_head - ch->ch_r_tail) & RQUEUEMASK;
2955 buf.txbuf = (ch->ch_w_head - ch->ch_w_tail) & WQUEUEMASK;
2958 * Is the UART empty? Add that value to whats in our TX queue.
2960 count = buf.txbuf + ch->ch_bd->bd_ops->get_uart_bytes_left(ch);
2963 * Figure out how much data the RealPort Server believes should
2964 * be in our TX queue.
2966 tdist = (buf.tIn - buf.tOut) & 0xffff;
2969 * If we have more data than the RealPort Server believes we
2970 * should have, reduce our count to its amount.
2972 * This count difference CAN happen because the Linux LD can
2973 * insert more characters into our queue for OPOST processing
2974 * that the RealPort Server doesn't know about.
2976 if (buf.txbuf > tdist)
2980 * Report whether our queue and UART TX are completely empty.
2987 spin_unlock_irqrestore(&ch->ch_lock, flags);
2989 if (copy_to_user(uarg, &buf, sizeof(buf)))
2995 spin_unlock_irqrestore(&ch->ch_lock, flags);
2997 return -ENOIOCTLCMD;