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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 * NOTE TO LINUX KERNEL HACKERS: DO NOT REFORMAT THIS CODE!
22 * This is shared code between Digi's CVS archive and the
23 * Linux Kernel sources.
24 * Changing the source just for reformatting needlessly breaks
25 * our CVS diff history.
27 * Send any bug fixes/changes to: Eng.Linux at digi dot com.
31 /************************************************************************
33 * This file implements the tty driver functionality for the
34 * Neo and ClassicBoard PCI based product lines.
36 ************************************************************************
40 #include <linux/kernel.h>
41 #include <linux/sched.h> /* For jiffies, task states */
42 #include <linux/interrupt.h> /* For tasklet and interrupt structs/defines */
43 #include <linux/module.h>
44 #include <linux/ctype.h>
45 #include <linux/tty.h>
46 #include <linux/tty_flip.h>
47 #include <linux/serial_reg.h>
48 #include <linux/slab.h>
49 #include <linux/delay.h> /* For udelay */
50 #include <linux/uaccess.h> /* For copy_from_user/copy_to_user */
51 #include <linux/pci.h>
52 #include "dgnc_driver.h"
54 #include "dgnc_types.h"
57 #include "dpacompat.h"
58 #include "dgnc_sysfs.h"
59 #include "dgnc_utils.h"
61 #define init_MUTEX(sem) sema_init(sem, 1)
62 #define DECLARE_MUTEX(name) \
63 struct semaphore name = __SEMAPHORE_INITIALIZER(name, 1)
68 static struct dgnc_board *dgnc_BoardsByMajor[256];
69 static unsigned char *dgnc_TmpWriteBuf;
70 static DECLARE_MUTEX(dgnc_TmpWriteSem);
73 * Default transparent print information.
75 static struct digi_t dgnc_digi_init = {
76 .digi_flags = DIGI_COOK, /* Flags */
77 .digi_maxcps = 100, /* Max CPS */
78 .digi_maxchar = 50, /* Max chars in print queue */
79 .digi_bufsize = 100, /* Printer buffer size */
80 .digi_onlen = 4, /* size of printer on string */
81 .digi_offlen = 4, /* size of printer off string */
82 .digi_onstr = "\033[5i", /* ANSI printer on string ] */
83 .digi_offstr = "\033[4i", /* ANSI printer off string ] */
84 .digi_term = "ansi" /* default terminal type */
89 * Define a local default termios struct. All ports will be created
90 * with this termios initially.
92 * This defines a raw port at 9600 baud, 8 data bits, no parity,
95 static struct ktermios DgncDefaultTermios = {
96 .c_iflag = (DEFAULT_IFLAGS), /* iflags */
97 .c_oflag = (DEFAULT_OFLAGS), /* oflags */
98 .c_cflag = (DEFAULT_CFLAGS), /* cflags */
99 .c_lflag = (DEFAULT_LFLAGS), /* lflags */
105 /* Our function prototypes */
106 static int dgnc_tty_open(struct tty_struct *tty, struct file *file);
107 static void dgnc_tty_close(struct tty_struct *tty, struct file *file);
108 static int dgnc_block_til_ready(struct tty_struct *tty, struct file *file, struct channel_t *ch);
109 static int dgnc_tty_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg);
110 static int dgnc_tty_digigeta(struct tty_struct *tty, struct digi_t __user *retinfo);
111 static int dgnc_tty_digiseta(struct tty_struct *tty, struct digi_t __user *new_info);
112 static int dgnc_tty_write_room(struct tty_struct *tty);
113 static int dgnc_tty_put_char(struct tty_struct *tty, unsigned char c);
114 static int dgnc_tty_chars_in_buffer(struct tty_struct *tty);
115 static void dgnc_tty_start(struct tty_struct *tty);
116 static void dgnc_tty_stop(struct tty_struct *tty);
117 static void dgnc_tty_throttle(struct tty_struct *tty);
118 static void dgnc_tty_unthrottle(struct tty_struct *tty);
119 static void dgnc_tty_flush_chars(struct tty_struct *tty);
120 static void dgnc_tty_flush_buffer(struct tty_struct *tty);
121 static void dgnc_tty_hangup(struct tty_struct *tty);
122 static int dgnc_set_modem_info(struct tty_struct *tty, unsigned int command, unsigned int __user *value);
123 static int dgnc_get_modem_info(struct channel_t *ch, unsigned int __user *value);
124 static int dgnc_tty_tiocmget(struct tty_struct *tty);
125 static int dgnc_tty_tiocmset(struct tty_struct *tty, unsigned int set, unsigned int clear);
126 static int dgnc_tty_send_break(struct tty_struct *tty, int msec);
127 static void dgnc_tty_wait_until_sent(struct tty_struct *tty, int timeout);
128 static int dgnc_tty_write(struct tty_struct *tty, const unsigned char *buf, int count);
129 static void dgnc_tty_set_termios(struct tty_struct *tty, struct ktermios *old_termios);
130 static void dgnc_tty_send_xchar(struct tty_struct *tty, char ch);
133 static const struct tty_operations dgnc_tty_ops = {
134 .open = dgnc_tty_open,
135 .close = dgnc_tty_close,
136 .write = dgnc_tty_write,
137 .write_room = dgnc_tty_write_room,
138 .flush_buffer = dgnc_tty_flush_buffer,
139 .chars_in_buffer = dgnc_tty_chars_in_buffer,
140 .flush_chars = dgnc_tty_flush_chars,
141 .ioctl = dgnc_tty_ioctl,
142 .set_termios = dgnc_tty_set_termios,
143 .stop = dgnc_tty_stop,
144 .start = dgnc_tty_start,
145 .throttle = dgnc_tty_throttle,
146 .unthrottle = dgnc_tty_unthrottle,
147 .hangup = dgnc_tty_hangup,
148 .put_char = dgnc_tty_put_char,
149 .tiocmget = dgnc_tty_tiocmget,
150 .tiocmset = dgnc_tty_tiocmset,
151 .break_ctl = dgnc_tty_send_break,
152 .wait_until_sent = dgnc_tty_wait_until_sent,
153 .send_xchar = dgnc_tty_send_xchar
156 /************************************************************************
158 * TTY Initialization/Cleanup Functions
160 ************************************************************************/
165 * Initialize any global tty related data before we download any boards.
167 int dgnc_tty_preinit(void)
170 * Allocate a buffer for doing the copy from user space to
171 * kernel space in dgnc_write(). We only use one buffer and
172 * control access to it with a semaphore. If we are paging, we
173 * are already in trouble so one buffer won't hurt much anyway.
175 * We are okay to sleep in the malloc, as this routine
176 * is only called during module load, (not in interrupt context),
177 * and with no locks held.
179 dgnc_TmpWriteBuf = kmalloc(WRITEBUFLEN, GFP_KERNEL);
181 if (!dgnc_TmpWriteBuf)
189 * dgnc_tty_register()
191 * Init the tty subsystem for this board.
193 int dgnc_tty_register(struct dgnc_board *brd)
197 brd->SerialDriver.magic = TTY_DRIVER_MAGIC;
199 snprintf(brd->SerialName, MAXTTYNAMELEN, "tty_dgnc_%d_", brd->boardnum);
201 brd->SerialDriver.name = brd->SerialName;
202 brd->SerialDriver.name_base = 0;
203 brd->SerialDriver.major = 0;
204 brd->SerialDriver.minor_start = 0;
205 brd->SerialDriver.num = brd->maxports;
206 brd->SerialDriver.type = TTY_DRIVER_TYPE_SERIAL;
207 brd->SerialDriver.subtype = SERIAL_TYPE_NORMAL;
208 brd->SerialDriver.init_termios = DgncDefaultTermios;
209 brd->SerialDriver.driver_name = DRVSTR;
210 brd->SerialDriver.flags = (TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_HARDWARE_BREAK);
213 * The kernel wants space to store pointers to
214 * tty_struct's and termios's.
216 brd->SerialDriver.ttys = kcalloc(brd->maxports, sizeof(*brd->SerialDriver.ttys), GFP_KERNEL);
217 if (!brd->SerialDriver.ttys)
220 kref_init(&brd->SerialDriver.kref);
221 brd->SerialDriver.termios = kcalloc(brd->maxports, sizeof(*brd->SerialDriver.termios), GFP_KERNEL);
222 if (!brd->SerialDriver.termios)
226 * Entry points for driver. Called by the kernel from
227 * tty_io.c and n_tty.c.
229 tty_set_operations(&brd->SerialDriver, &dgnc_tty_ops);
231 if (!brd->dgnc_Major_Serial_Registered) {
232 /* Register tty devices */
233 rc = tty_register_driver(&brd->SerialDriver);
235 dev_dbg(&brd->pdev->dev,
236 "Can't register tty device (%d)\n", rc);
239 brd->dgnc_Major_Serial_Registered = TRUE;
243 * If we're doing transparent print, we have to do all of the above
244 * again, separately so we don't get the LD confused about what major
245 * we are when we get into the dgnc_tty_open() routine.
247 brd->PrintDriver.magic = TTY_DRIVER_MAGIC;
248 snprintf(brd->PrintName, MAXTTYNAMELEN, "pr_dgnc_%d_", brd->boardnum);
250 brd->PrintDriver.name = brd->PrintName;
251 brd->PrintDriver.name_base = 0;
252 brd->PrintDriver.major = brd->SerialDriver.major;
253 brd->PrintDriver.minor_start = 0x80;
254 brd->PrintDriver.num = brd->maxports;
255 brd->PrintDriver.type = TTY_DRIVER_TYPE_SERIAL;
256 brd->PrintDriver.subtype = SERIAL_TYPE_NORMAL;
257 brd->PrintDriver.init_termios = DgncDefaultTermios;
258 brd->PrintDriver.driver_name = DRVSTR;
259 brd->PrintDriver.flags = (TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_HARDWARE_BREAK);
262 * The kernel wants space to store pointers to
263 * tty_struct's and termios's. Must be separated from
264 * the Serial Driver so we don't get confused
266 brd->PrintDriver.ttys = kcalloc(brd->maxports, sizeof(*brd->PrintDriver.ttys), GFP_KERNEL);
267 if (!brd->PrintDriver.ttys)
269 kref_init(&brd->PrintDriver.kref);
270 brd->PrintDriver.termios = kcalloc(brd->maxports, sizeof(*brd->PrintDriver.termios), GFP_KERNEL);
271 if (!brd->PrintDriver.termios)
275 * Entry points for driver. Called by the kernel from
276 * tty_io.c and n_tty.c.
278 tty_set_operations(&brd->PrintDriver, &dgnc_tty_ops);
280 if (!brd->dgnc_Major_TransparentPrint_Registered) {
281 /* Register Transparent Print devices */
282 rc = tty_register_driver(&brd->PrintDriver);
284 dev_dbg(&brd->pdev->dev,
285 "Can't register Transparent Print device(%d)\n",
289 brd->dgnc_Major_TransparentPrint_Registered = TRUE;
292 dgnc_BoardsByMajor[brd->SerialDriver.major] = brd;
293 brd->dgnc_Serial_Major = brd->SerialDriver.major;
294 brd->dgnc_TransparentPrint_Major = brd->PrintDriver.major;
303 * Init the tty subsystem. Called once per board after board has been
304 * downloaded and init'ed.
306 int dgnc_tty_init(struct dgnc_board *brd)
310 struct channel_t *ch;
316 * Initialize board structure elements.
319 vaddr = brd->re_map_membase;
321 brd->nasync = brd->maxports;
324 * Allocate channel memory that might not have been allocated
325 * when the driver was first loaded.
327 for (i = 0; i < brd->nasync; i++) {
328 if (!brd->channels[i]) {
331 * Okay to malloc with GFP_KERNEL, we are not at
332 * interrupt context, and there are no locks held.
334 brd->channels[i] = kzalloc(sizeof(*brd->channels[i]), GFP_KERNEL);
338 ch = brd->channels[0];
339 vaddr = brd->re_map_membase;
341 /* Set up channel variables */
342 for (i = 0; i < brd->nasync; i++, ch = brd->channels[i]) {
344 if (!brd->channels[i])
347 spin_lock_init(&ch->ch_lock);
349 /* Store all our magic numbers */
350 ch->magic = DGNC_CHANNEL_MAGIC;
351 ch->ch_tun.magic = DGNC_UNIT_MAGIC;
352 ch->ch_tun.un_ch = ch;
353 ch->ch_tun.un_type = DGNC_SERIAL;
354 ch->ch_tun.un_dev = i;
356 ch->ch_pun.magic = DGNC_UNIT_MAGIC;
357 ch->ch_pun.un_ch = ch;
358 ch->ch_pun.un_type = DGNC_PRINT;
359 ch->ch_pun.un_dev = i + 128;
361 if (brd->bd_uart_offset == 0x200)
362 ch->ch_neo_uart = vaddr + (brd->bd_uart_offset * i);
364 ch->ch_cls_uart = vaddr + (brd->bd_uart_offset * i);
368 ch->ch_digi = dgnc_digi_init;
370 /* .25 second delay */
371 ch->ch_close_delay = 250;
373 init_waitqueue_head(&ch->ch_flags_wait);
374 init_waitqueue_head(&ch->ch_tun.un_flags_wait);
375 init_waitqueue_head(&ch->ch_pun.un_flags_wait);
378 struct device *classp;
380 classp = tty_register_device(&brd->SerialDriver, i,
381 &(ch->ch_bd->pdev->dev));
382 ch->ch_tun.un_sysfs = classp;
383 dgnc_create_tty_sysfs(&ch->ch_tun, classp);
385 classp = tty_register_device(&brd->PrintDriver, i,
386 &(ch->ch_bd->pdev->dev));
387 ch->ch_pun.un_sysfs = classp;
388 dgnc_create_tty_sysfs(&ch->ch_pun, classp);
398 * dgnc_tty_post_uninit()
400 * UnInitialize any global tty related data.
402 void dgnc_tty_post_uninit(void)
404 kfree(dgnc_TmpWriteBuf);
405 dgnc_TmpWriteBuf = NULL;
412 * Uninitialize the TTY portion of this driver. Free all memory and
415 void dgnc_tty_uninit(struct dgnc_board *brd)
419 if (brd->dgnc_Major_Serial_Registered) {
420 dgnc_BoardsByMajor[brd->SerialDriver.major] = NULL;
421 brd->dgnc_Serial_Major = 0;
422 for (i = 0; i < brd->nasync; i++) {
423 dgnc_remove_tty_sysfs(brd->channels[i]->ch_tun.un_sysfs);
424 tty_unregister_device(&brd->SerialDriver, i);
426 tty_unregister_driver(&brd->SerialDriver);
427 brd->dgnc_Major_Serial_Registered = FALSE;
430 if (brd->dgnc_Major_TransparentPrint_Registered) {
431 dgnc_BoardsByMajor[brd->PrintDriver.major] = NULL;
432 brd->dgnc_TransparentPrint_Major = 0;
433 for (i = 0; i < brd->nasync; i++) {
434 dgnc_remove_tty_sysfs(brd->channels[i]->ch_pun.un_sysfs);
435 tty_unregister_device(&brd->PrintDriver, i);
437 tty_unregister_driver(&brd->PrintDriver);
438 brd->dgnc_Major_TransparentPrint_Registered = FALSE;
441 kfree(brd->SerialDriver.ttys);
442 brd->SerialDriver.ttys = NULL;
443 kfree(brd->PrintDriver.ttys);
444 brd->PrintDriver.ttys = NULL;
448 #define TMPBUFLEN (1024)
450 /*=======================================================================
452 * dgnc_wmove - Write data to transmit queue.
454 * ch - Pointer to channel structure.
455 * buf - Poiter to characters to be moved.
456 * n - Number of characters to move.
458 *=======================================================================*/
459 static void dgnc_wmove(struct channel_t *ch, char *buf, uint n)
464 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
467 head = ch->ch_w_head & WQUEUEMASK;
470 * If the write wraps over the top of the circular buffer,
471 * move the portion up to the wrap point, and reset the
472 * pointers to the bottom.
474 remain = WQUEUESIZE - head;
478 memcpy(ch->ch_wqueue + head, buf, remain);
488 memcpy(ch->ch_wqueue + head, buf, remain);
493 ch->ch_w_head = head;
499 /*=======================================================================
501 * dgnc_input - Process received data.
503 * ch - Pointer to channel structure.
505 *=======================================================================*/
506 void dgnc_input(struct channel_t *ch)
508 struct dgnc_board *bd;
509 struct tty_struct *tp;
510 struct tty_ldisc *ld;
522 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
525 tp = ch->ch_tun.un_tty;
528 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
531 spin_lock_irqsave(&ch->ch_lock, flags);
534 * Figure the number of characters in the buffer.
535 * Exit immediately if none.
538 head = ch->ch_r_head & rmask;
539 tail = ch->ch_r_tail & rmask;
540 data_len = (head - tail) & rmask;
543 spin_unlock_irqrestore(&ch->ch_lock, flags);
548 * If the device is not open, or CREAD is off,
549 * flush input data and return immediately.
551 if (!tp || (tp->magic != TTY_MAGIC) || !(ch->ch_tun.un_flags & UN_ISOPEN) ||
552 !(tp->termios.c_cflag & CREAD) || (ch->ch_tun.un_flags & UN_CLOSING)) {
554 ch->ch_r_head = tail;
556 /* Force queue flow control to be released, if needed */
557 dgnc_check_queue_flow_control(ch);
559 spin_unlock_irqrestore(&ch->ch_lock, flags);
564 * If we are throttled, simply don't read any data.
566 if (ch->ch_flags & CH_FORCED_STOPI) {
567 spin_unlock_irqrestore(&ch->ch_lock, flags);
571 flip_len = TTY_FLIPBUF_SIZE;
573 /* Chop down the length, if needed */
574 len = min(data_len, flip_len);
575 len = min(len, (N_TTY_BUF_SIZE - 1));
577 ld = tty_ldisc_ref(tp);
581 * If the DONT_FLIP flag is on, don't flush our buffer, and act
582 * like the ld doesn't have any space to put the data right now.
584 if (test_bit(TTY_DONT_FLIP, &tp->flags))
589 * If we were unable to get a reference to the ld,
590 * don't flush our buffer, and act like the ld doesn't
591 * have any space to put the data right now.
597 * If ld doesn't have a pointer to a receive_buf function,
598 * flush the data, then act like the ld doesn't have any
599 * space to put the data right now.
601 if (!ld->ops->receive_buf) {
602 ch->ch_r_head = ch->ch_r_tail;
608 spin_unlock_irqrestore(&ch->ch_lock, flags);
615 * The tty layer in the kernel has changed in 2.6.16+.
617 * The flip buffers in the tty structure are no longer exposed,
618 * and probably will be going away eventually.
620 * If we are completely raw, we don't need to go through a lot
621 * of the tty layers that exist.
622 * In this case, we take the shortest and fastest route we
623 * can to relay the data to the user.
625 * On the other hand, if we are not raw, we need to go through
626 * the new 2.6.16+ tty layer, which has its API more well defined.
628 len = tty_buffer_request_room(tp->port, len);
632 * n now contains the most amount of data we can copy,
633 * bounded either by how much the Linux tty layer can handle,
634 * or the amount of data the card actually has pending...
637 s = ((head >= tail) ? head : RQUEUESIZE) - tail;
644 * If conditions are such that ld needs to see all
645 * UART errors, we will have to walk each character
646 * and error byte and send them to the buffer one at
649 if (I_PARMRK(tp) || I_BRKINT(tp) || I_INPCK(tp)) {
650 for (i = 0; i < s; i++) {
651 if (*(ch->ch_equeue + tail + i) & UART_LSR_BI)
652 tty_insert_flip_char(tp->port, *(ch->ch_rqueue + tail + i), TTY_BREAK);
653 else if (*(ch->ch_equeue + tail + i) & UART_LSR_PE)
654 tty_insert_flip_char(tp->port, *(ch->ch_rqueue + tail + i), TTY_PARITY);
655 else if (*(ch->ch_equeue + tail + i) & UART_LSR_FE)
656 tty_insert_flip_char(tp->port, *(ch->ch_rqueue + tail + i), TTY_FRAME);
658 tty_insert_flip_char(tp->port, *(ch->ch_rqueue + tail + i), TTY_NORMAL);
661 tty_insert_flip_string(tp->port, ch->ch_rqueue + tail, s);
666 /* Flip queue if needed */
670 ch->ch_r_tail = tail & rmask;
671 ch->ch_e_tail = tail & rmask;
672 dgnc_check_queue_flow_control(ch);
673 spin_unlock_irqrestore(&ch->ch_lock, flags);
675 /* Tell the tty layer its okay to "eat" the data now */
676 tty_flip_buffer_push(tp->port);
683 /************************************************************************
684 * Determines when CARRIER changes state and takes appropriate
686 ************************************************************************/
687 void dgnc_carrier(struct channel_t *ch)
689 struct dgnc_board *bd;
691 int virt_carrier = 0;
692 int phys_carrier = 0;
694 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
699 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
702 if (ch->ch_mistat & UART_MSR_DCD)
705 if (ch->ch_digi.digi_flags & DIGI_FORCEDCD)
708 if (ch->ch_c_cflag & CLOCAL)
712 * Test for a VIRTUAL carrier transition to HIGH.
714 if (((ch->ch_flags & CH_FCAR) == 0) && (virt_carrier == 1)) {
717 * When carrier rises, wake any threads waiting
718 * for carrier in the open routine.
721 if (waitqueue_active(&(ch->ch_flags_wait)))
722 wake_up_interruptible(&ch->ch_flags_wait);
726 * Test for a PHYSICAL carrier transition to HIGH.
728 if (((ch->ch_flags & CH_CD) == 0) && (phys_carrier == 1)) {
731 * When carrier rises, wake any threads waiting
732 * for carrier in the open routine.
735 if (waitqueue_active(&(ch->ch_flags_wait)))
736 wake_up_interruptible(&ch->ch_flags_wait);
740 * Test for a PHYSICAL transition to low, so long as we aren't
741 * currently ignoring physical transitions (which is what "virtual
742 * carrier" indicates).
744 * The transition of the virtual carrier to low really doesn't
745 * matter... it really only means "ignore carrier state", not
746 * "make pretend that carrier is there".
748 if ((virt_carrier == 0) && ((ch->ch_flags & CH_CD) != 0) &&
749 (phys_carrier == 0)) {
752 * When carrier drops:
754 * Drop carrier on all open units.
756 * Flush queues, waking up any task waiting in the
759 * Send a hangup to the control terminal.
761 * Enable all select calls.
763 if (waitqueue_active(&(ch->ch_flags_wait)))
764 wake_up_interruptible(&ch->ch_flags_wait);
766 if (ch->ch_tun.un_open_count > 0)
767 tty_hangup(ch->ch_tun.un_tty);
769 if (ch->ch_pun.un_open_count > 0)
770 tty_hangup(ch->ch_pun.un_tty);
774 * Make sure that our cached values reflect the current reality.
776 if (virt_carrier == 1)
777 ch->ch_flags |= CH_FCAR;
779 ch->ch_flags &= ~CH_FCAR;
781 if (phys_carrier == 1)
782 ch->ch_flags |= CH_CD;
784 ch->ch_flags &= ~CH_CD;
788 * Assign the custom baud rate to the channel structure
790 static void dgnc_set_custom_speed(struct channel_t *ch, uint newrate)
799 ch->ch_custom_speed = 0;
804 * Since the divisor is stored in a 16-bit integer, we make sure
805 * we don't allow any rates smaller than a 16-bit integer would allow.
806 * And of course, rates above the dividend won't fly.
808 if (newrate && newrate < ((ch->ch_bd->bd_dividend / 0xFFFF) + 1))
809 newrate = ((ch->ch_bd->bd_dividend / 0xFFFF) + 1);
811 if (newrate && newrate > ch->ch_bd->bd_dividend)
812 newrate = ch->ch_bd->bd_dividend;
815 testdiv = ch->ch_bd->bd_dividend / newrate;
818 * If we try to figure out what rate the board would use
819 * with the test divisor, it will be either equal or higher
820 * than the requested baud rate. If we then determine the
821 * rate with a divisor one higher, we will get the next lower
822 * supported rate below the requested.
824 testrate_high = ch->ch_bd->bd_dividend / testdiv;
825 testrate_low = ch->ch_bd->bd_dividend / (testdiv + 1);
828 * If the rate for the requested divisor is correct, just
829 * use it and be done.
831 if (testrate_high != newrate) {
833 * Otherwise, pick the rate that is closer (i.e. whichever rate
834 * has a smaller delta).
836 deltahigh = testrate_high - newrate;
837 deltalow = newrate - testrate_low;
839 if (deltahigh < deltalow)
840 newrate = testrate_high;
842 newrate = testrate_low;
846 ch->ch_custom_speed = newrate;
850 void dgnc_check_queue_flow_control(struct channel_t *ch)
854 /* Store how much space we have left in the queue */
855 qleft = ch->ch_r_tail - ch->ch_r_head - 1;
857 qleft += RQUEUEMASK + 1;
860 * Check to see if we should enforce flow control on our queue because
861 * the ld (or user) isn't reading data out of our queue fast enuf.
863 * NOTE: This is done based on what the current flow control of the
866 * 1) HWFLOW (RTS) - Turn off the UART's Receive interrupt.
867 * This will cause the UART's FIFO to back up, and force
868 * the RTS signal to be dropped.
869 * 2) SWFLOW (IXOFF) - Keep trying to send a stop character to
870 * the other side, in hopes it will stop sending data to us.
871 * 3) NONE - Nothing we can do. We will simply drop any extra data
872 * that gets sent into us when the queue fills up.
876 if (ch->ch_digi.digi_flags & CTSPACE || ch->ch_c_cflag & CRTSCTS) {
877 if (!(ch->ch_flags & CH_RECEIVER_OFF)) {
878 ch->ch_bd->bd_ops->disable_receiver(ch);
879 ch->ch_flags |= (CH_RECEIVER_OFF);
883 else if (ch->ch_c_iflag & IXOFF) {
884 if (ch->ch_stops_sent <= MAX_STOPS_SENT) {
885 ch->ch_bd->bd_ops->send_stop_character(ch);
891 /* Empty... Can't do anything about the impending overflow... */
896 * Check to see if we should unenforce flow control because
897 * ld (or user) finally read enuf data out of our queue.
899 * NOTE: This is done based on what the current flow control of the
902 * 1) HWFLOW (RTS) - Turn back on the UART's Receive interrupt.
903 * This will cause the UART's FIFO to raise RTS back up,
904 * which will allow the other side to start sending data again.
905 * 2) SWFLOW (IXOFF) - Send a start character to
906 * the other side, so it will start sending data to us again.
907 * 3) NONE - Do nothing. Since we didn't do anything to turn off the
908 * other side, we don't need to do anything now.
910 if (qleft > (RQUEUESIZE / 2)) {
912 if (ch->ch_digi.digi_flags & RTSPACE || ch->ch_c_cflag & CRTSCTS) {
913 if (ch->ch_flags & CH_RECEIVER_OFF) {
914 ch->ch_bd->bd_ops->enable_receiver(ch);
915 ch->ch_flags &= ~(CH_RECEIVER_OFF);
919 else if (ch->ch_c_iflag & IXOFF && ch->ch_stops_sent) {
920 ch->ch_stops_sent = 0;
921 ch->ch_bd->bd_ops->send_start_character(ch);
925 /* Nothing needed. */
931 void dgnc_wakeup_writes(struct channel_t *ch)
936 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
939 spin_lock_irqsave(&ch->ch_lock, flags);
942 * If channel now has space, wake up anyone waiting on the condition.
944 qlen = ch->ch_w_head - ch->ch_w_tail;
948 if (qlen >= (WQUEUESIZE - 256)) {
949 spin_unlock_irqrestore(&ch->ch_lock, flags);
953 if (ch->ch_tun.un_flags & UN_ISOPEN) {
954 if ((ch->ch_tun.un_tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
955 ch->ch_tun.un_tty->ldisc->ops->write_wakeup) {
956 spin_unlock_irqrestore(&ch->ch_lock, flags);
957 (ch->ch_tun.un_tty->ldisc->ops->write_wakeup)(ch->ch_tun.un_tty);
958 spin_lock_irqsave(&ch->ch_lock, flags);
961 wake_up_interruptible(&ch->ch_tun.un_tty->write_wait);
964 * If unit is set to wait until empty, check to make sure
965 * the queue AND FIFO are both empty.
967 if (ch->ch_tun.un_flags & UN_EMPTY) {
968 if ((qlen == 0) && (ch->ch_bd->bd_ops->get_uart_bytes_left(ch) == 0)) {
969 ch->ch_tun.un_flags &= ~(UN_EMPTY);
972 * If RTS Toggle mode is on, whenever
973 * the queue and UART is empty, keep RTS low.
975 if (ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE) {
976 ch->ch_mostat &= ~(UART_MCR_RTS);
977 ch->ch_bd->bd_ops->assert_modem_signals(ch);
981 * If DTR Toggle mode is on, whenever
982 * the queue and UART is empty, keep DTR low.
984 if (ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE) {
985 ch->ch_mostat &= ~(UART_MCR_DTR);
986 ch->ch_bd->bd_ops->assert_modem_signals(ch);
991 wake_up_interruptible(&ch->ch_tun.un_flags_wait);
994 if (ch->ch_pun.un_flags & UN_ISOPEN) {
995 if ((ch->ch_pun.un_tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
996 ch->ch_pun.un_tty->ldisc->ops->write_wakeup) {
997 spin_unlock_irqrestore(&ch->ch_lock, flags);
998 (ch->ch_pun.un_tty->ldisc->ops->write_wakeup)(ch->ch_pun.un_tty);
999 spin_lock_irqsave(&ch->ch_lock, flags);
1002 wake_up_interruptible(&ch->ch_pun.un_tty->write_wait);
1005 * If unit is set to wait until empty, check to make sure
1006 * the queue AND FIFO are both empty.
1008 if (ch->ch_pun.un_flags & UN_EMPTY) {
1009 if ((qlen == 0) && (ch->ch_bd->bd_ops->get_uart_bytes_left(ch) == 0))
1010 ch->ch_pun.un_flags &= ~(UN_EMPTY);
1013 wake_up_interruptible(&ch->ch_pun.un_flags_wait);
1016 spin_unlock_irqrestore(&ch->ch_lock, flags);
1021 /************************************************************************
1023 * TTY Entry points and helper functions
1025 ************************************************************************/
1031 static int dgnc_tty_open(struct tty_struct *tty, struct file *file)
1033 struct dgnc_board *brd;
1034 struct channel_t *ch;
1039 unsigned long flags;
1043 major = MAJOR(tty_devnum(tty));
1044 minor = MINOR(tty_devnum(tty));
1049 /* Get board pointer from our array of majors we have allocated */
1050 brd = dgnc_BoardsByMajor[major];
1055 * If board is not yet up to a state of READY, go to
1056 * sleep waiting for it to happen or they cancel the open.
1058 rc = wait_event_interruptible(brd->state_wait,
1059 (brd->state & BOARD_READY));
1064 spin_lock_irqsave(&brd->bd_lock, flags);
1066 /* If opened device is greater than our number of ports, bail. */
1067 if (PORT_NUM(minor) > brd->nasync) {
1068 spin_unlock_irqrestore(&brd->bd_lock, flags);
1072 ch = brd->channels[PORT_NUM(minor)];
1074 spin_unlock_irqrestore(&brd->bd_lock, flags);
1078 /* Drop board lock */
1079 spin_unlock_irqrestore(&brd->bd_lock, flags);
1081 /* Grab channel lock */
1082 spin_lock_irqsave(&ch->ch_lock, flags);
1084 /* Figure out our type */
1085 if (!IS_PRINT(minor)) {
1086 un = &brd->channels[PORT_NUM(minor)]->ch_tun;
1087 un->un_type = DGNC_SERIAL;
1088 } else if (IS_PRINT(minor)) {
1089 un = &brd->channels[PORT_NUM(minor)]->ch_pun;
1090 un->un_type = DGNC_PRINT;
1092 spin_unlock_irqrestore(&ch->ch_lock, flags);
1097 * If the port is still in a previous open, and in a state
1098 * where we simply cannot safely keep going, wait until the
1101 spin_unlock_irqrestore(&ch->ch_lock, flags);
1103 rc = wait_event_interruptible(ch->ch_flags_wait, ((ch->ch_flags & CH_OPENING) == 0));
1105 /* If ret is non-zero, user ctrl-c'ed us */
1110 * If either unit is in the middle of the fragile part of close,
1111 * we just cannot touch the channel safely.
1112 * Go to sleep, knowing that when the channel can be
1113 * touched safely, the close routine will signal the
1114 * ch_flags_wait to wake us back up.
1116 rc = wait_event_interruptible(ch->ch_flags_wait,
1117 (((ch->ch_tun.un_flags | ch->ch_pun.un_flags) & UN_CLOSING) == 0));
1119 /* If ret is non-zero, user ctrl-c'ed us */
1123 spin_lock_irqsave(&ch->ch_lock, flags);
1126 /* Store our unit into driver_data, so we always have it available. */
1127 tty->driver_data = un;
1133 if (!(un->un_flags & UN_ISOPEN)) {
1134 /* Store important variables. */
1137 /* Maybe do something here to the TTY struct as well? */
1142 * Allocate channel buffers for read/write/error.
1143 * Set flag, so we don't get trounced on.
1145 ch->ch_flags |= (CH_OPENING);
1147 /* Drop locks, as malloc with GFP_KERNEL can sleep */
1148 spin_unlock_irqrestore(&ch->ch_lock, flags);
1151 ch->ch_rqueue = kzalloc(RQUEUESIZE, GFP_KERNEL);
1153 ch->ch_equeue = kzalloc(EQUEUESIZE, GFP_KERNEL);
1155 ch->ch_wqueue = kzalloc(WQUEUESIZE, GFP_KERNEL);
1157 spin_lock_irqsave(&ch->ch_lock, flags);
1159 ch->ch_flags &= ~(CH_OPENING);
1160 wake_up_interruptible(&ch->ch_flags_wait);
1163 * Initialize if neither terminal or printer is open.
1165 if (!((ch->ch_tun.un_flags | ch->ch_pun.un_flags) & UN_ISOPEN)) {
1168 * Flush input queues.
1177 brd->bd_ops->flush_uart_write(ch);
1178 brd->bd_ops->flush_uart_read(ch);
1181 ch->ch_cached_lsr = 0;
1182 ch->ch_stop_sending_break = 0;
1183 ch->ch_stops_sent = 0;
1185 ch->ch_c_cflag = tty->termios.c_cflag;
1186 ch->ch_c_iflag = tty->termios.c_iflag;
1187 ch->ch_c_oflag = tty->termios.c_oflag;
1188 ch->ch_c_lflag = tty->termios.c_lflag;
1189 ch->ch_startc = tty->termios.c_cc[VSTART];
1190 ch->ch_stopc = tty->termios.c_cc[VSTOP];
1193 * Bring up RTS and DTR...
1194 * Also handle RTS or DTR toggle if set.
1196 if (!(ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE))
1197 ch->ch_mostat |= (UART_MCR_RTS);
1198 if (!(ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE))
1199 ch->ch_mostat |= (UART_MCR_DTR);
1201 /* Tell UART to init itself */
1202 brd->bd_ops->uart_init(ch);
1206 * Run param in case we changed anything
1208 brd->bd_ops->param(tty);
1213 * follow protocol for opening port
1216 spin_unlock_irqrestore(&ch->ch_lock, flags);
1218 rc = dgnc_block_til_ready(tty, file, ch);
1220 /* No going back now, increment our unit and channel counters */
1221 spin_lock_irqsave(&ch->ch_lock, flags);
1222 ch->ch_open_count++;
1223 un->un_open_count++;
1224 un->un_flags |= (UN_ISOPEN);
1225 spin_unlock_irqrestore(&ch->ch_lock, flags);
1232 * dgnc_block_til_ready()
1234 * Wait for DCD, if needed.
1236 static int dgnc_block_til_ready(struct tty_struct *tty, struct file *file, struct channel_t *ch)
1239 struct un_t *un = NULL;
1240 unsigned long flags;
1242 int sleep_on_un_flags = 0;
1244 if (!tty || tty->magic != TTY_MAGIC || !file || !ch || ch->magic != DGNC_CHANNEL_MAGIC)
1247 un = tty->driver_data;
1248 if (!un || un->magic != DGNC_UNIT_MAGIC)
1251 spin_lock_irqsave(&ch->ch_lock, flags);
1258 sleep_on_un_flags = 0;
1261 * If board has failed somehow during our sleep, bail with error.
1263 if (ch->ch_bd->state == BOARD_FAILED) {
1268 /* If tty was hung up, break out of loop and set error. */
1269 if (tty_hung_up_p(file)) {
1275 * If either unit is in the middle of the fragile part of close,
1276 * we just cannot touch the channel safely.
1277 * Go back to sleep, knowing that when the channel can be
1278 * touched safely, the close routine will signal the
1279 * ch_wait_flags to wake us back up.
1281 if (!((ch->ch_tun.un_flags | ch->ch_pun.un_flags) & UN_CLOSING)) {
1284 * Our conditions to leave cleanly and happily:
1285 * 1) NONBLOCKING on the tty is set.
1287 * 3) DCD (fake or real) is active.
1290 if (file->f_flags & O_NONBLOCK)
1293 if (tty->flags & (1 << TTY_IO_ERROR)) {
1298 if (ch->ch_flags & CH_CD)
1301 if (ch->ch_flags & CH_FCAR)
1304 sleep_on_un_flags = 1;
1308 * If there is a signal pending, the user probably
1309 * interrupted (ctrl-c) us.
1310 * Leave loop with error set.
1312 if (signal_pending(current)) {
1313 retval = -ERESTARTSYS;
1318 * Store the flags before we let go of channel lock
1320 if (sleep_on_un_flags)
1321 old_flags = ch->ch_tun.un_flags | ch->ch_pun.un_flags;
1323 old_flags = ch->ch_flags;
1326 * Let go of channel lock before calling schedule.
1327 * Our poller will get any FEP events and wake us up when DCD
1328 * eventually goes active.
1331 spin_unlock_irqrestore(&ch->ch_lock, flags);
1334 * Wait for something in the flags to change from the current value.
1336 if (sleep_on_un_flags)
1337 retval = wait_event_interruptible(un->un_flags_wait,
1338 (old_flags != (ch->ch_tun.un_flags | ch->ch_pun.un_flags)));
1340 retval = wait_event_interruptible(ch->ch_flags_wait,
1341 (old_flags != ch->ch_flags));
1344 * We got woken up for some reason.
1345 * Before looping around, grab our channel lock.
1347 spin_lock_irqsave(&ch->ch_lock, flags);
1352 spin_unlock_irqrestore(&ch->ch_lock, flags);
1364 * Hangup the port. Like a close, but don't wait for output to drain.
1366 static void dgnc_tty_hangup(struct tty_struct *tty)
1370 if (!tty || tty->magic != TTY_MAGIC)
1373 un = tty->driver_data;
1374 if (!un || un->magic != DGNC_UNIT_MAGIC)
1377 /* flush the transmit queues */
1378 dgnc_tty_flush_buffer(tty);
1387 static void dgnc_tty_close(struct tty_struct *tty, struct file *file)
1389 struct ktermios *ts;
1390 struct dgnc_board *bd;
1391 struct channel_t *ch;
1393 unsigned long flags;
1396 if (!tty || tty->magic != TTY_MAGIC)
1399 un = tty->driver_data;
1400 if (!un || un->magic != DGNC_UNIT_MAGIC)
1404 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1408 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
1413 spin_lock_irqsave(&ch->ch_lock, flags);
1416 * Determine if this is the last close or not - and if we agree about
1417 * which type of close it is with the Line Discipline
1419 if ((tty->count == 1) && (un->un_open_count != 1)) {
1421 * Uh, oh. tty->count is 1, which means that the tty
1422 * structure will be freed. un_open_count should always
1423 * be one in these conditions. If it's greater than
1424 * one, we've got real problems, since it means the
1425 * serial port won't be shutdown.
1428 "tty->count is 1, un open count is %d\n",
1430 un->un_open_count = 1;
1433 if (un->un_open_count)
1434 un->un_open_count--;
1437 "bad serial port open count of %d\n",
1440 ch->ch_open_count--;
1442 if (ch->ch_open_count && un->un_open_count) {
1443 spin_unlock_irqrestore(&ch->ch_lock, flags);
1447 /* OK, its the last close on the unit */
1448 un->un_flags |= UN_CLOSING;
1454 * Only officially close channel if count is 0 and
1455 * DIGI_PRINTER bit is not set.
1457 if ((ch->ch_open_count == 0) && !(ch->ch_digi.digi_flags & DIGI_PRINTER)) {
1459 ch->ch_flags &= ~(CH_STOPI | CH_FORCED_STOPI);
1462 * turn off print device when closing print device.
1464 if ((un->un_type == DGNC_PRINT) && (ch->ch_flags & CH_PRON)) {
1465 dgnc_wmove(ch, ch->ch_digi.digi_offstr,
1466 (int) ch->ch_digi.digi_offlen);
1467 ch->ch_flags &= ~CH_PRON;
1470 spin_unlock_irqrestore(&ch->ch_lock, flags);
1471 /* wait for output to drain */
1472 /* This will also return if we take an interrupt */
1474 rc = bd->bd_ops->drain(tty, 0);
1476 dgnc_tty_flush_buffer(tty);
1477 tty_ldisc_flush(tty);
1479 spin_lock_irqsave(&ch->ch_lock, flags);
1484 * If we have HUPCL set, lower DTR and RTS
1486 if (ch->ch_c_cflag & HUPCL) {
1489 ch->ch_mostat &= ~(UART_MCR_DTR | UART_MCR_RTS);
1490 bd->bd_ops->assert_modem_signals(ch);
1493 * Go to sleep to ensure RTS/DTR
1494 * have been dropped for modems to see it.
1496 if (ch->ch_close_delay) {
1497 spin_unlock_irqrestore(&ch->ch_lock,
1499 dgnc_ms_sleep(ch->ch_close_delay);
1500 spin_lock_irqsave(&ch->ch_lock, flags);
1504 ch->ch_old_baud = 0;
1506 /* Turn off UART interrupts for this port */
1507 ch->ch_bd->bd_ops->uart_off(ch);
1510 * turn off print device when closing print device.
1512 if ((un->un_type == DGNC_PRINT) && (ch->ch_flags & CH_PRON)) {
1513 dgnc_wmove(ch, ch->ch_digi.digi_offstr,
1514 (int) ch->ch_digi.digi_offlen);
1515 ch->ch_flags &= ~CH_PRON;
1520 un->un_flags &= ~(UN_ISOPEN | UN_CLOSING);
1522 wake_up_interruptible(&ch->ch_flags_wait);
1523 wake_up_interruptible(&un->un_flags_wait);
1525 spin_unlock_irqrestore(&ch->ch_lock, flags);
1530 * dgnc_tty_chars_in_buffer()
1532 * Return number of characters that have not been transmitted yet.
1534 * This routine is used by the line discipline to determine if there
1535 * is data waiting to be transmitted/drained/flushed or not.
1537 static int dgnc_tty_chars_in_buffer(struct tty_struct *tty)
1539 struct channel_t *ch = NULL;
1540 struct un_t *un = NULL;
1545 unsigned long flags;
1550 un = tty->driver_data;
1551 if (!un || un->magic != DGNC_UNIT_MAGIC)
1555 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1558 spin_lock_irqsave(&ch->ch_lock, flags);
1561 thead = ch->ch_w_head & tmask;
1562 ttail = ch->ch_w_tail & tmask;
1564 spin_unlock_irqrestore(&ch->ch_lock, flags);
1566 if (ttail == thead) {
1570 chars = thead - ttail;
1572 chars = thead - ttail + WQUEUESIZE;
1582 * Reduces bytes_available to the max number of characters
1583 * that can be sent currently given the maxcps value, and
1584 * returns the new bytes_available. This only affects printer
1587 static int dgnc_maxcps_room(struct tty_struct *tty, int bytes_available)
1589 struct channel_t *ch = NULL;
1590 struct un_t *un = NULL;
1593 return bytes_available;
1595 un = tty->driver_data;
1596 if (!un || un->magic != DGNC_UNIT_MAGIC)
1597 return bytes_available;
1600 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1601 return bytes_available;
1604 * If its not the Transparent print device, return
1605 * the full data amount.
1607 if (un->un_type != DGNC_PRINT)
1608 return bytes_available;
1610 if (ch->ch_digi.digi_maxcps > 0 && ch->ch_digi.digi_bufsize > 0) {
1612 unsigned long current_time = jiffies;
1613 unsigned long buffer_time = current_time +
1614 (HZ * ch->ch_digi.digi_bufsize) / ch->ch_digi.digi_maxcps;
1616 if (ch->ch_cpstime < current_time) {
1617 /* buffer is empty */
1618 ch->ch_cpstime = current_time; /* reset ch_cpstime */
1619 cps_limit = ch->ch_digi.digi_bufsize;
1620 } else if (ch->ch_cpstime < buffer_time) {
1621 /* still room in the buffer */
1622 cps_limit = ((buffer_time - ch->ch_cpstime) * ch->ch_digi.digi_maxcps) / HZ;
1624 /* no room in the buffer */
1628 bytes_available = min(cps_limit, bytes_available);
1631 return bytes_available;
1636 * dgnc_tty_write_room()
1638 * Return space available in Tx buffer
1640 static int dgnc_tty_write_room(struct tty_struct *tty)
1642 struct channel_t *ch = NULL;
1643 struct un_t *un = NULL;
1648 unsigned long flags;
1650 if (tty == NULL || dgnc_TmpWriteBuf == NULL)
1653 un = tty->driver_data;
1654 if (!un || un->magic != DGNC_UNIT_MAGIC)
1658 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1661 spin_lock_irqsave(&ch->ch_lock, flags);
1664 head = (ch->ch_w_head) & tmask;
1665 tail = (ch->ch_w_tail) & tmask;
1667 ret = tail - head - 1;
1671 /* Limit printer to maxcps */
1672 ret = dgnc_maxcps_room(tty, ret);
1675 * If we are printer device, leave space for
1676 * possibly both the on and off strings.
1678 if (un->un_type == DGNC_PRINT) {
1679 if (!(ch->ch_flags & CH_PRON))
1680 ret -= ch->ch_digi.digi_onlen;
1681 ret -= ch->ch_digi.digi_offlen;
1683 if (ch->ch_flags & CH_PRON)
1684 ret -= ch->ch_digi.digi_offlen;
1690 spin_unlock_irqrestore(&ch->ch_lock, flags);
1697 * dgnc_tty_put_char()
1699 * Put a character into ch->ch_buf
1701 * - used by the line discipline for OPOST processing
1703 static int dgnc_tty_put_char(struct tty_struct *tty, unsigned char c)
1706 * Simply call tty_write.
1708 dgnc_tty_write(tty, &c, 1);
1716 * Take data from the user or kernel and send it out to the FEP.
1717 * In here exists all the Transparent Print magic as well.
1719 static int dgnc_tty_write(struct tty_struct *tty,
1720 const unsigned char *buf, int count)
1722 struct channel_t *ch = NULL;
1723 struct un_t *un = NULL;
1724 int bufcount = 0, n = 0;
1726 unsigned long flags;
1733 if (tty == NULL || dgnc_TmpWriteBuf == NULL)
1736 un = tty->driver_data;
1737 if (!un || un->magic != DGNC_UNIT_MAGIC)
1741 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1748 * Store original amount of characters passed in.
1749 * This helps to figure out if we should ask the FEP
1750 * to send us an event when it has more space available.
1754 spin_lock_irqsave(&ch->ch_lock, flags);
1756 /* Get our space available for the channel from the board */
1758 head = (ch->ch_w_head) & tmask;
1759 tail = (ch->ch_w_tail) & tmask;
1761 bufcount = tail - head - 1;
1763 bufcount += WQUEUESIZE;
1766 * Limit printer output to maxcps overall, with bursts allowed
1767 * up to bufsize characters.
1769 bufcount = dgnc_maxcps_room(tty, bufcount);
1772 * Take minimum of what the user wants to send, and the
1773 * space available in the FEP buffer.
1775 count = min(count, bufcount);
1778 * Bail if no space left.
1781 spin_unlock_irqrestore(&ch->ch_lock, flags);
1786 * Output the printer ON string, if we are in terminal mode, but
1787 * need to be in printer mode.
1789 if ((un->un_type == DGNC_PRINT) && !(ch->ch_flags & CH_PRON)) {
1790 dgnc_wmove(ch, ch->ch_digi.digi_onstr,
1791 (int) ch->ch_digi.digi_onlen);
1792 head = (ch->ch_w_head) & tmask;
1793 ch->ch_flags |= CH_PRON;
1797 * On the other hand, output the printer OFF string, if we are
1798 * currently in printer mode, but need to output to the terminal.
1800 if ((un->un_type != DGNC_PRINT) && (ch->ch_flags & CH_PRON)) {
1801 dgnc_wmove(ch, ch->ch_digi.digi_offstr,
1802 (int) ch->ch_digi.digi_offlen);
1803 head = (ch->ch_w_head) & tmask;
1804 ch->ch_flags &= ~CH_PRON;
1808 * If there is nothing left to copy, or I can't handle any more data, leave.
1811 spin_unlock_irqrestore(&ch->ch_lock, flags);
1817 count = min(count, WRITEBUFLEN);
1819 spin_unlock_irqrestore(&ch->ch_lock, flags);
1822 * If data is coming from user space, copy it into a temporary
1823 * buffer so we don't get swapped out while doing the copy to
1826 /* we're allowed to block if it's from_user */
1827 if (down_interruptible(&dgnc_TmpWriteSem))
1831 * copy_from_user() returns the number
1832 * of bytes that could *NOT* be copied.
1834 count -= copy_from_user(dgnc_TmpWriteBuf, (const unsigned char __user *) buf, count);
1837 up(&dgnc_TmpWriteSem);
1841 spin_lock_irqsave(&ch->ch_lock, flags);
1843 buf = dgnc_TmpWriteBuf;
1850 * If the write wraps over the top of the circular buffer,
1851 * move the portion up to the wrap point, and reset the
1852 * pointers to the bottom.
1854 remain = WQUEUESIZE - head;
1858 memcpy(ch->ch_wqueue + head, buf, remain);
1865 * Move rest of data.
1868 memcpy(ch->ch_wqueue + head, buf, remain);
1874 ch->ch_w_head = head;
1877 /* Update printer buffer empty time. */
1878 if ((un->un_type == DGNC_PRINT) && (ch->ch_digi.digi_maxcps > 0)
1879 && (ch->ch_digi.digi_bufsize > 0)) {
1880 ch->ch_cpstime += (HZ * count) / ch->ch_digi.digi_maxcps;
1884 spin_unlock_irqrestore(&ch->ch_lock, flags);
1885 up(&dgnc_TmpWriteSem);
1887 spin_unlock_irqrestore(&ch->ch_lock, flags);
1892 * Channel lock is grabbed and then released
1893 * inside this routine.
1895 ch->ch_bd->bd_ops->copy_data_from_queue_to_uart(ch);
1903 * Return modem signals to ld.
1906 static int dgnc_tty_tiocmget(struct tty_struct *tty)
1908 struct channel_t *ch;
1911 unsigned char mstat = 0;
1912 unsigned long flags;
1914 if (!tty || tty->magic != TTY_MAGIC)
1917 un = tty->driver_data;
1918 if (!un || un->magic != DGNC_UNIT_MAGIC)
1922 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1925 spin_lock_irqsave(&ch->ch_lock, flags);
1927 mstat = (ch->ch_mostat | ch->ch_mistat);
1929 spin_unlock_irqrestore(&ch->ch_lock, flags);
1933 if (mstat & UART_MCR_DTR)
1934 result |= TIOCM_DTR;
1935 if (mstat & UART_MCR_RTS)
1936 result |= TIOCM_RTS;
1937 if (mstat & UART_MSR_CTS)
1938 result |= TIOCM_CTS;
1939 if (mstat & UART_MSR_DSR)
1940 result |= TIOCM_DSR;
1941 if (mstat & UART_MSR_RI)
1943 if (mstat & UART_MSR_DCD)
1951 * dgnc_tty_tiocmset()
1953 * Set modem signals, called by ld.
1956 static int dgnc_tty_tiocmset(struct tty_struct *tty,
1957 unsigned int set, unsigned int clear)
1959 struct dgnc_board *bd;
1960 struct channel_t *ch;
1963 unsigned long flags;
1965 if (!tty || tty->magic != TTY_MAGIC)
1968 un = tty->driver_data;
1969 if (!un || un->magic != DGNC_UNIT_MAGIC)
1973 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1977 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
1980 spin_lock_irqsave(&ch->ch_lock, flags);
1982 if (set & TIOCM_RTS)
1983 ch->ch_mostat |= UART_MCR_RTS;
1985 if (set & TIOCM_DTR)
1986 ch->ch_mostat |= UART_MCR_DTR;
1988 if (clear & TIOCM_RTS)
1989 ch->ch_mostat &= ~(UART_MCR_RTS);
1991 if (clear & TIOCM_DTR)
1992 ch->ch_mostat &= ~(UART_MCR_DTR);
1994 ch->ch_bd->bd_ops->assert_modem_signals(ch);
1996 spin_unlock_irqrestore(&ch->ch_lock, flags);
2003 * dgnc_tty_send_break()
2005 * Send a Break, called by ld.
2007 static int dgnc_tty_send_break(struct tty_struct *tty, int msec)
2009 struct dgnc_board *bd;
2010 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)
2041 spin_lock_irqsave(&ch->ch_lock, flags);
2043 ch->ch_bd->bd_ops->send_break(ch, msec);
2045 spin_unlock_irqrestore(&ch->ch_lock, flags);
2053 * dgnc_tty_wait_until_sent()
2055 * wait until data has been transmitted, called by ld.
2057 static void dgnc_tty_wait_until_sent(struct tty_struct *tty, int timeout)
2059 struct dgnc_board *bd;
2060 struct channel_t *ch;
2064 if (!tty || tty->magic != TTY_MAGIC)
2067 un = tty->driver_data;
2068 if (!un || un->magic != DGNC_UNIT_MAGIC)
2072 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2076 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2079 rc = bd->bd_ops->drain(tty, 0);
2086 * send a high priority character, called by ld.
2088 static void dgnc_tty_send_xchar(struct tty_struct *tty, char c)
2090 struct dgnc_board *bd;
2091 struct channel_t *ch;
2093 unsigned long flags;
2095 if (!tty || tty->magic != TTY_MAGIC)
2098 un = tty->driver_data;
2099 if (!un || un->magic != DGNC_UNIT_MAGIC)
2103 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2107 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2110 dev_dbg(tty->dev, "dgnc_tty_send_xchar start\n");
2112 spin_lock_irqsave(&ch->ch_lock, flags);
2113 bd->bd_ops->send_immediate_char(ch, c);
2114 spin_unlock_irqrestore(&ch->ch_lock, flags);
2116 dev_dbg(tty->dev, "dgnc_tty_send_xchar finish\n");
2123 * Return modem signals to ld.
2125 static inline int dgnc_get_mstat(struct channel_t *ch)
2127 unsigned char mstat;
2129 unsigned long flags;
2131 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2134 spin_lock_irqsave(&ch->ch_lock, flags);
2136 mstat = (ch->ch_mostat | ch->ch_mistat);
2138 spin_unlock_irqrestore(&ch->ch_lock, flags);
2142 if (mstat & UART_MCR_DTR)
2143 result |= TIOCM_DTR;
2144 if (mstat & UART_MCR_RTS)
2145 result |= TIOCM_RTS;
2146 if (mstat & UART_MSR_CTS)
2147 result |= TIOCM_CTS;
2148 if (mstat & UART_MSR_DSR)
2149 result |= TIOCM_DSR;
2150 if (mstat & UART_MSR_RI)
2152 if (mstat & UART_MSR_DCD)
2161 * Return modem signals to ld.
2163 static int dgnc_get_modem_info(struct channel_t *ch, unsigned int __user *value)
2167 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2170 result = dgnc_get_mstat(ch);
2175 return put_user(result, value);
2180 * dgnc_set_modem_info()
2182 * Set modem signals, called by ld.
2184 static int dgnc_set_modem_info(struct tty_struct *tty, unsigned int command, unsigned int __user *value)
2186 struct dgnc_board *bd;
2187 struct channel_t *ch;
2190 unsigned int arg = 0;
2191 unsigned long flags;
2193 if (!tty || tty->magic != TTY_MAGIC)
2196 un = tty->driver_data;
2197 if (!un || un->magic != DGNC_UNIT_MAGIC)
2201 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2205 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2208 ret = get_user(arg, value);
2214 if (arg & TIOCM_RTS)
2215 ch->ch_mostat |= UART_MCR_RTS;
2217 if (arg & TIOCM_DTR)
2218 ch->ch_mostat |= UART_MCR_DTR;
2223 if (arg & TIOCM_RTS)
2224 ch->ch_mostat &= ~(UART_MCR_RTS);
2226 if (arg & TIOCM_DTR)
2227 ch->ch_mostat &= ~(UART_MCR_DTR);
2233 if (arg & TIOCM_RTS)
2234 ch->ch_mostat |= UART_MCR_RTS;
2236 ch->ch_mostat &= ~(UART_MCR_RTS);
2238 if (arg & TIOCM_DTR)
2239 ch->ch_mostat |= UART_MCR_DTR;
2241 ch->ch_mostat &= ~(UART_MCR_DTR);
2249 spin_lock_irqsave(&ch->ch_lock, flags);
2251 ch->ch_bd->bd_ops->assert_modem_signals(ch);
2253 spin_unlock_irqrestore(&ch->ch_lock, flags);
2260 * dgnc_tty_digigeta()
2262 * Ioctl to get the information for ditty.
2267 static int dgnc_tty_digigeta(struct tty_struct *tty, struct digi_t __user *retinfo)
2269 struct channel_t *ch;
2272 unsigned long flags;
2277 if (!tty || tty->magic != TTY_MAGIC)
2280 un = tty->driver_data;
2281 if (!un || un->magic != DGNC_UNIT_MAGIC)
2285 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2288 memset(&tmp, 0, sizeof(tmp));
2290 spin_lock_irqsave(&ch->ch_lock, flags);
2291 memcpy(&tmp, &ch->ch_digi, sizeof(tmp));
2292 spin_unlock_irqrestore(&ch->ch_lock, flags);
2294 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2302 * dgnc_tty_digiseta()
2304 * Ioctl to set the information for ditty.
2309 static int dgnc_tty_digiseta(struct tty_struct *tty, struct digi_t __user *new_info)
2311 struct dgnc_board *bd;
2312 struct channel_t *ch;
2314 struct digi_t new_digi;
2315 unsigned long flags;
2317 if (!tty || tty->magic != TTY_MAGIC)
2320 un = tty->driver_data;
2321 if (!un || un->magic != DGNC_UNIT_MAGIC)
2325 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2329 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2332 if (copy_from_user(&new_digi, new_info, sizeof(new_digi)))
2335 spin_lock_irqsave(&ch->ch_lock, flags);
2338 * Handle transistions to and from RTS Toggle.
2340 if (!(ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE) && (new_digi.digi_flags & DIGI_RTS_TOGGLE))
2341 ch->ch_mostat &= ~(UART_MCR_RTS);
2342 if ((ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE) && !(new_digi.digi_flags & DIGI_RTS_TOGGLE))
2343 ch->ch_mostat |= (UART_MCR_RTS);
2346 * Handle transistions to and from DTR Toggle.
2348 if (!(ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE) && (new_digi.digi_flags & DIGI_DTR_TOGGLE))
2349 ch->ch_mostat &= ~(UART_MCR_DTR);
2350 if ((ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE) && !(new_digi.digi_flags & DIGI_DTR_TOGGLE))
2351 ch->ch_mostat |= (UART_MCR_DTR);
2353 memcpy(&ch->ch_digi, &new_digi, sizeof(new_digi));
2355 if (ch->ch_digi.digi_maxcps < 1)
2356 ch->ch_digi.digi_maxcps = 1;
2358 if (ch->ch_digi.digi_maxcps > 10000)
2359 ch->ch_digi.digi_maxcps = 10000;
2361 if (ch->ch_digi.digi_bufsize < 10)
2362 ch->ch_digi.digi_bufsize = 10;
2364 if (ch->ch_digi.digi_maxchar < 1)
2365 ch->ch_digi.digi_maxchar = 1;
2367 if (ch->ch_digi.digi_maxchar > ch->ch_digi.digi_bufsize)
2368 ch->ch_digi.digi_maxchar = ch->ch_digi.digi_bufsize;
2370 if (ch->ch_digi.digi_onlen > DIGI_PLEN)
2371 ch->ch_digi.digi_onlen = DIGI_PLEN;
2373 if (ch->ch_digi.digi_offlen > DIGI_PLEN)
2374 ch->ch_digi.digi_offlen = DIGI_PLEN;
2376 ch->ch_bd->bd_ops->param(tty);
2378 spin_unlock_irqrestore(&ch->ch_lock, flags);
2385 * dgnc_set_termios()
2387 static void dgnc_tty_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
2389 struct dgnc_board *bd;
2390 struct channel_t *ch;
2392 unsigned long flags;
2394 if (!tty || tty->magic != TTY_MAGIC)
2397 un = tty->driver_data;
2398 if (!un || un->magic != DGNC_UNIT_MAGIC)
2402 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2406 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2409 spin_lock_irqsave(&ch->ch_lock, flags);
2411 ch->ch_c_cflag = tty->termios.c_cflag;
2412 ch->ch_c_iflag = tty->termios.c_iflag;
2413 ch->ch_c_oflag = tty->termios.c_oflag;
2414 ch->ch_c_lflag = tty->termios.c_lflag;
2415 ch->ch_startc = tty->termios.c_cc[VSTART];
2416 ch->ch_stopc = tty->termios.c_cc[VSTOP];
2418 ch->ch_bd->bd_ops->param(tty);
2421 spin_unlock_irqrestore(&ch->ch_lock, flags);
2425 static void dgnc_tty_throttle(struct tty_struct *tty)
2427 struct channel_t *ch;
2429 unsigned long flags;
2431 if (!tty || tty->magic != TTY_MAGIC)
2434 un = tty->driver_data;
2435 if (!un || un->magic != DGNC_UNIT_MAGIC)
2439 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2442 spin_lock_irqsave(&ch->ch_lock, flags);
2444 ch->ch_flags |= (CH_FORCED_STOPI);
2446 spin_unlock_irqrestore(&ch->ch_lock, flags);
2450 static void dgnc_tty_unthrottle(struct tty_struct *tty)
2452 struct channel_t *ch;
2454 unsigned long flags;
2456 if (!tty || tty->magic != TTY_MAGIC)
2459 un = tty->driver_data;
2460 if (!un || un->magic != DGNC_UNIT_MAGIC)
2464 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2467 spin_lock_irqsave(&ch->ch_lock, flags);
2469 ch->ch_flags &= ~(CH_FORCED_STOPI);
2471 spin_unlock_irqrestore(&ch->ch_lock, flags);
2475 static void dgnc_tty_start(struct tty_struct *tty)
2477 struct dgnc_board *bd;
2478 struct channel_t *ch;
2480 unsigned long flags;
2482 if (!tty || tty->magic != TTY_MAGIC)
2485 un = tty->driver_data;
2486 if (!un || un->magic != DGNC_UNIT_MAGIC)
2490 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2494 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2497 spin_lock_irqsave(&ch->ch_lock, flags);
2499 ch->ch_flags &= ~(CH_FORCED_STOP);
2501 spin_unlock_irqrestore(&ch->ch_lock, flags);
2505 static void dgnc_tty_stop(struct tty_struct *tty)
2507 struct dgnc_board *bd;
2508 struct channel_t *ch;
2510 unsigned long flags;
2512 if (!tty || tty->magic != TTY_MAGIC)
2515 un = tty->driver_data;
2516 if (!un || un->magic != DGNC_UNIT_MAGIC)
2520 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2524 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2527 spin_lock_irqsave(&ch->ch_lock, flags);
2529 ch->ch_flags |= (CH_FORCED_STOP);
2531 spin_unlock_irqrestore(&ch->ch_lock, flags);
2536 * dgnc_tty_flush_chars()
2538 * Flush the cook buffer
2540 * Note to self, and any other poor souls who venture here:
2542 * flush in this case DOES NOT mean dispose of the data.
2543 * instead, it means "stop buffering and send it if you
2544 * haven't already." Just guess how I figured that out... SRW 2-Jun-98
2546 * It is also always called in interrupt context - JAR 8-Sept-99
2548 static void dgnc_tty_flush_chars(struct tty_struct *tty)
2550 struct dgnc_board *bd;
2551 struct channel_t *ch;
2553 unsigned long flags;
2555 if (!tty || tty->magic != TTY_MAGIC)
2558 un = tty->driver_data;
2559 if (!un || un->magic != DGNC_UNIT_MAGIC)
2563 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2567 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2570 spin_lock_irqsave(&ch->ch_lock, flags);
2572 /* Do something maybe here */
2574 spin_unlock_irqrestore(&ch->ch_lock, flags);
2580 * dgnc_tty_flush_buffer()
2582 * Flush Tx buffer (make in == out)
2584 static void dgnc_tty_flush_buffer(struct tty_struct *tty)
2586 struct channel_t *ch;
2588 unsigned long flags;
2590 if (!tty || tty->magic != TTY_MAGIC)
2593 un = tty->driver_data;
2594 if (!un || un->magic != DGNC_UNIT_MAGIC)
2598 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2601 spin_lock_irqsave(&ch->ch_lock, flags);
2603 ch->ch_flags &= ~CH_STOP;
2605 /* Flush our write queue */
2606 ch->ch_w_head = ch->ch_w_tail;
2608 /* Flush UARTs transmit FIFO */
2609 ch->ch_bd->bd_ops->flush_uart_write(ch);
2611 if (ch->ch_tun.un_flags & (UN_LOW|UN_EMPTY)) {
2612 ch->ch_tun.un_flags &= ~(UN_LOW|UN_EMPTY);
2613 wake_up_interruptible(&ch->ch_tun.un_flags_wait);
2615 if (ch->ch_pun.un_flags & (UN_LOW|UN_EMPTY)) {
2616 ch->ch_pun.un_flags &= ~(UN_LOW|UN_EMPTY);
2617 wake_up_interruptible(&ch->ch_pun.un_flags_wait);
2620 spin_unlock_irqrestore(&ch->ch_lock, flags);
2625 /*****************************************************************************
2627 * The IOCTL function and all of its helpers
2629 *****************************************************************************/
2634 * The usual assortment of ioctl's
2636 static int dgnc_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
2639 struct dgnc_board *bd;
2640 struct channel_t *ch;
2643 unsigned long flags;
2644 void __user *uarg = (void __user *) arg;
2646 if (!tty || tty->magic != TTY_MAGIC)
2649 un = tty->driver_data;
2650 if (!un || un->magic != DGNC_UNIT_MAGIC)
2654 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2658 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2661 spin_lock_irqsave(&ch->ch_lock, flags);
2663 if (un->un_open_count <= 0) {
2664 spin_unlock_irqrestore(&ch->ch_lock, flags);
2670 /* Here are all the standard ioctl's that we MUST implement */
2674 * TCSBRK is SVID version: non-zero arg --> no break
2675 * this behaviour is exploited by tcdrain().
2677 * According to POSIX.1 spec (7.2.2.1.2) breaks should be
2678 * between 0.25 and 0.5 seconds so we'll ask for something
2679 * in the middle: 0.375 seconds.
2681 rc = tty_check_change(tty);
2682 spin_unlock_irqrestore(&ch->ch_lock, flags);
2686 rc = ch->ch_bd->bd_ops->drain(tty, 0);
2691 spin_lock_irqsave(&ch->ch_lock, flags);
2693 if (((cmd == TCSBRK) && (!arg)) || (cmd == TCSBRKP))
2694 ch->ch_bd->bd_ops->send_break(ch, 250);
2696 spin_unlock_irqrestore(&ch->ch_lock, flags);
2702 /* support for POSIX tcsendbreak()
2703 * According to POSIX.1 spec (7.2.2.1.2) breaks should be
2704 * between 0.25 and 0.5 seconds so we'll ask for something
2705 * in the middle: 0.375 seconds.
2707 rc = tty_check_change(tty);
2708 spin_unlock_irqrestore(&ch->ch_lock, flags);
2712 rc = ch->ch_bd->bd_ops->drain(tty, 0);
2716 spin_lock_irqsave(&ch->ch_lock, flags);
2718 ch->ch_bd->bd_ops->send_break(ch, 250);
2720 spin_unlock_irqrestore(&ch->ch_lock, flags);
2725 rc = tty_check_change(tty);
2726 spin_unlock_irqrestore(&ch->ch_lock, flags);
2730 rc = ch->ch_bd->bd_ops->drain(tty, 0);
2734 spin_lock_irqsave(&ch->ch_lock, flags);
2736 ch->ch_bd->bd_ops->send_break(ch, 250);
2738 spin_unlock_irqrestore(&ch->ch_lock, flags);
2744 spin_unlock_irqrestore(&ch->ch_lock, flags);
2749 spin_unlock_irqrestore(&ch->ch_lock, flags);
2751 rc = put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *) arg);
2756 spin_unlock_irqrestore(&ch->ch_lock, flags);
2757 rc = get_user(arg, (unsigned long __user *) arg);
2761 spin_lock_irqsave(&ch->ch_lock, flags);
2762 tty->termios.c_cflag = ((tty->termios.c_cflag & ~CLOCAL) | (arg ? CLOCAL : 0));
2763 ch->ch_bd->bd_ops->param(tty);
2764 spin_unlock_irqrestore(&ch->ch_lock, flags);
2769 spin_unlock_irqrestore(&ch->ch_lock, flags);
2770 return dgnc_get_modem_info(ch, uarg);
2775 spin_unlock_irqrestore(&ch->ch_lock, flags);
2776 return dgnc_set_modem_info(tty, cmd, uarg);
2779 * Here are any additional ioctl's that we want to implement
2784 * The linux tty driver doesn't have a flush
2785 * input routine for the driver, assuming all backed
2786 * up data is in the line disc. buffers. However,
2787 * we all know that's not the case. Here, we
2788 * act on the ioctl, but then lie and say we didn't
2789 * so the line discipline will process the flush
2792 rc = tty_check_change(tty);
2794 spin_unlock_irqrestore(&ch->ch_lock, flags);
2798 if ((arg == TCIFLUSH) || (arg == TCIOFLUSH)) {
2799 ch->ch_r_head = ch->ch_r_tail;
2800 ch->ch_bd->bd_ops->flush_uart_read(ch);
2801 /* Force queue flow control to be released, if needed */
2802 dgnc_check_queue_flow_control(ch);
2805 if ((arg == TCOFLUSH) || (arg == TCIOFLUSH)) {
2806 if (!(un->un_type == DGNC_PRINT)) {
2807 ch->ch_w_head = ch->ch_w_tail;
2808 ch->ch_bd->bd_ops->flush_uart_write(ch);
2810 if (ch->ch_tun.un_flags & (UN_LOW|UN_EMPTY)) {
2811 ch->ch_tun.un_flags &= ~(UN_LOW|UN_EMPTY);
2812 wake_up_interruptible(&ch->ch_tun.un_flags_wait);
2815 if (ch->ch_pun.un_flags & (UN_LOW|UN_EMPTY)) {
2816 ch->ch_pun.un_flags &= ~(UN_LOW|UN_EMPTY);
2817 wake_up_interruptible(&ch->ch_pun.un_flags_wait);
2823 /* pretend we didn't recognize this IOCTL */
2824 spin_unlock_irqrestore(&ch->ch_lock, flags);
2825 return -ENOIOCTLCMD;
2829 * The linux tty driver doesn't have a flush
2830 * input routine for the driver, assuming all backed
2831 * up data is in the line disc. buffers. However,
2832 * we all know that's not the case. Here, we
2833 * act on the ioctl, but then lie and say we didn't
2834 * so the line discipline will process the flush
2837 if (cmd == TCSETSF) {
2839 ch->ch_flags &= ~CH_STOP;
2840 ch->ch_r_head = ch->ch_r_tail;
2841 ch->ch_bd->bd_ops->flush_uart_read(ch);
2842 /* Force queue flow control to be released, if needed */
2843 dgnc_check_queue_flow_control(ch);
2846 /* now wait for all the output to drain */
2847 spin_unlock_irqrestore(&ch->ch_lock, flags);
2848 rc = ch->ch_bd->bd_ops->drain(tty, 0);
2852 /* pretend we didn't recognize this */
2853 return -ENOIOCTLCMD;
2857 spin_unlock_irqrestore(&ch->ch_lock, flags);
2858 rc = ch->ch_bd->bd_ops->drain(tty, 0);
2862 /* pretend we didn't recognize this */
2863 return -ENOIOCTLCMD;
2866 spin_unlock_irqrestore(&ch->ch_lock, flags);
2867 /* Make the ld do it */
2868 return -ENOIOCTLCMD;
2871 /* get information for ditty */
2872 spin_unlock_irqrestore(&ch->ch_lock, flags);
2873 return dgnc_tty_digigeta(tty, uarg);
2878 /* set information for ditty */
2879 if (cmd == (DIGI_SETAW)) {
2881 spin_unlock_irqrestore(&ch->ch_lock, flags);
2882 rc = ch->ch_bd->bd_ops->drain(tty, 0);
2887 spin_lock_irqsave(&ch->ch_lock, flags);
2889 tty_ldisc_flush(tty);
2894 spin_unlock_irqrestore(&ch->ch_lock, flags);
2895 return dgnc_tty_digiseta(tty, uarg);
2900 /* Let go of locks when accessing user space, could sleep */
2901 spin_unlock_irqrestore(&ch->ch_lock, flags);
2902 rc = get_user(loopback, (unsigned int __user *) arg);
2905 spin_lock_irqsave(&ch->ch_lock, flags);
2907 /* Enable/disable internal loopback for this port */
2909 ch->ch_flags |= CH_LOOPBACK;
2911 ch->ch_flags &= ~(CH_LOOPBACK);
2913 ch->ch_bd->bd_ops->param(tty);
2914 spin_unlock_irqrestore(&ch->ch_lock, flags);
2918 case DIGI_GETCUSTOMBAUD:
2919 spin_unlock_irqrestore(&ch->ch_lock, flags);
2920 rc = put_user(ch->ch_custom_speed, (unsigned int __user *) arg);
2923 case DIGI_SETCUSTOMBAUD:
2926 /* Let go of locks when accessing user space, could sleep */
2927 spin_unlock_irqrestore(&ch->ch_lock, flags);
2928 rc = get_user(new_rate, (int __user *) arg);
2931 spin_lock_irqsave(&ch->ch_lock, flags);
2932 dgnc_set_custom_speed(ch, new_rate);
2933 ch->ch_bd->bd_ops->param(tty);
2934 spin_unlock_irqrestore(&ch->ch_lock, flags);
2939 * This ioctl allows insertion of a character into the front
2940 * of any pending data to be transmitted.
2942 * This ioctl is to satify the "Send Character Immediate"
2943 * call that the RealPort protocol spec requires.
2945 case DIGI_REALPORT_SENDIMMEDIATE:
2949 spin_unlock_irqrestore(&ch->ch_lock, flags);
2950 rc = get_user(c, (unsigned char __user *) arg);
2953 spin_lock_irqsave(&ch->ch_lock, flags);
2954 ch->ch_bd->bd_ops->send_immediate_char(ch, c);
2955 spin_unlock_irqrestore(&ch->ch_lock, flags);
2960 * This ioctl returns all the current counts for the port.
2962 * This ioctl is to satify the "Line Error Counters"
2963 * call that the RealPort protocol spec requires.
2965 case DIGI_REALPORT_GETCOUNTERS:
2967 struct digi_getcounter buf;
2969 buf.norun = ch->ch_err_overrun;
2970 buf.noflow = 0; /* The driver doesn't keep this stat */
2971 buf.nframe = ch->ch_err_frame;
2972 buf.nparity = ch->ch_err_parity;
2973 buf.nbreak = ch->ch_err_break;
2974 buf.rbytes = ch->ch_rxcount;
2975 buf.tbytes = ch->ch_txcount;
2977 spin_unlock_irqrestore(&ch->ch_lock, flags);
2979 if (copy_to_user(uarg, &buf, sizeof(buf)))
2986 * This ioctl returns all current events.
2988 * This ioctl is to satify the "Event Reporting"
2989 * call that the RealPort protocol spec requires.
2991 case DIGI_REALPORT_GETEVENTS:
2993 unsigned int events = 0;
2995 /* NOTE: MORE EVENTS NEEDS TO BE ADDED HERE */
2996 if (ch->ch_flags & CH_BREAK_SENDING)
2998 if ((ch->ch_flags & CH_STOP) || (ch->ch_flags & CH_FORCED_STOP))
2999 events |= (EV_OPU | EV_OPS);
3001 if ((ch->ch_flags & CH_STOPI) || (ch->ch_flags & CH_FORCED_STOPI))
3002 events |= (EV_IPU | EV_IPS);
3004 spin_unlock_irqrestore(&ch->ch_lock, flags);
3005 rc = put_user(events, (unsigned int __user *) arg);
3010 * This ioctl returns TOUT and TIN counters based
3011 * upon the values passed in by the RealPort Server.
3012 * It also passes back whether the UART Transmitter is
3015 case DIGI_REALPORT_GETBUFFERS:
3017 struct digi_getbuffer buf;
3021 spin_unlock_irqrestore(&ch->ch_lock, flags);
3024 * Get data from user first.
3026 if (copy_from_user(&buf, uarg, sizeof(buf)))
3029 spin_lock_irqsave(&ch->ch_lock, flags);
3032 * Figure out how much data is in our RX and TX queues.
3034 buf.rxbuf = (ch->ch_r_head - ch->ch_r_tail) & RQUEUEMASK;
3035 buf.txbuf = (ch->ch_w_head - ch->ch_w_tail) & WQUEUEMASK;
3038 * Is the UART empty? Add that value to whats in our TX queue.
3040 count = buf.txbuf + ch->ch_bd->bd_ops->get_uart_bytes_left(ch);
3043 * Figure out how much data the RealPort Server believes should
3044 * be in our TX queue.
3046 tdist = (buf.tIn - buf.tOut) & 0xffff;
3049 * If we have more data than the RealPort Server believes we
3050 * should have, reduce our count to its amount.
3052 * This count difference CAN happen because the Linux LD can
3053 * insert more characters into our queue for OPOST processing
3054 * that the RealPort Server doesn't know about.
3056 if (buf.txbuf > tdist)
3060 * Report whether our queue and UART TX are completely empty.
3067 spin_unlock_irqrestore(&ch->ch_lock, flags);
3069 if (copy_to_user(uarg, &buf, sizeof(buf)))
3075 spin_unlock_irqrestore(&ch->ch_lock, flags);
3077 return -ENOIOCTLCMD;