2 * Copyright 2003 Digi International (www.digi.com)
3 * Scott H Kilau <Scott_Kilau at digi dot com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED; without even the
12 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
13 * PURPOSE. See the GNU General Public License for more details.
16 /************************************************************************
18 * This file implements the tty driver functionality for the
19 * Neo and ClassicBoard PCI based product lines.
21 ************************************************************************
25 #include <linux/kernel.h>
26 #include <linux/sched.h> /* For jiffies, task states */
27 #include <linux/interrupt.h> /* For tasklet and interrupt structs/defines */
28 #include <linux/module.h>
29 #include <linux/ctype.h>
30 #include <linux/tty.h>
31 #include <linux/tty_flip.h>
32 #include <linux/types.h>
33 #include <linux/serial_reg.h>
34 #include <linux/slab.h>
35 #include <linux/delay.h> /* For udelay */
36 #include <linux/uaccess.h> /* For copy_from_user/copy_to_user */
37 #include <linux/pci.h>
38 #include "dgnc_driver.h"
42 #include "dgnc_sysfs.h"
43 #include "dgnc_utils.h"
48 static struct dgnc_board *dgnc_BoardsByMajor[256];
49 static unsigned char *dgnc_TmpWriteBuf;
52 * Default transparent print information.
54 static struct digi_t dgnc_digi_init = {
55 .digi_flags = DIGI_COOK, /* Flags */
56 .digi_maxcps = 100, /* Max CPS */
57 .digi_maxchar = 50, /* Max chars in print queue */
58 .digi_bufsize = 100, /* Printer buffer size */
59 .digi_onlen = 4, /* size of printer on string */
60 .digi_offlen = 4, /* size of printer off string */
61 .digi_onstr = "\033[5i", /* ANSI printer on string ] */
62 .digi_offstr = "\033[4i", /* ANSI printer off string ] */
63 .digi_term = "ansi" /* default terminal type */
67 * Define a local default termios struct. All ports will be created
68 * with this termios initially.
70 * This defines a raw port at 9600 baud, 8 data bits, no parity,
73 static struct ktermios DgncDefaultTermios = {
74 .c_iflag = (DEFAULT_IFLAGS), /* iflags */
75 .c_oflag = (DEFAULT_OFLAGS), /* oflags */
76 .c_cflag = (DEFAULT_CFLAGS), /* cflags */
77 .c_lflag = (DEFAULT_LFLAGS), /* lflags */
82 /* Our function prototypes */
83 static int dgnc_tty_open(struct tty_struct *tty, struct file *file);
84 static void dgnc_tty_close(struct tty_struct *tty, struct file *file);
85 static int dgnc_block_til_ready(struct tty_struct *tty, struct file *file,
86 struct channel_t *ch);
87 static int dgnc_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
89 static int dgnc_tty_digigeta(struct tty_struct *tty,
90 struct digi_t __user *retinfo);
91 static int dgnc_tty_digiseta(struct tty_struct *tty,
92 struct digi_t __user *new_info);
93 static int dgnc_tty_write_room(struct tty_struct *tty);
94 static int dgnc_tty_put_char(struct tty_struct *tty, unsigned char c);
95 static int dgnc_tty_chars_in_buffer(struct tty_struct *tty);
96 static void dgnc_tty_start(struct tty_struct *tty);
97 static void dgnc_tty_stop(struct tty_struct *tty);
98 static void dgnc_tty_throttle(struct tty_struct *tty);
99 static void dgnc_tty_unthrottle(struct tty_struct *tty);
100 static void dgnc_tty_flush_chars(struct tty_struct *tty);
101 static void dgnc_tty_flush_buffer(struct tty_struct *tty);
102 static void dgnc_tty_hangup(struct tty_struct *tty);
103 static int dgnc_set_modem_info(struct tty_struct *tty, unsigned int command,
104 unsigned int __user *value);
105 static int dgnc_get_modem_info(struct channel_t *ch,
106 unsigned int __user *value);
107 static int dgnc_tty_tiocmget(struct tty_struct *tty);
108 static int dgnc_tty_tiocmset(struct tty_struct *tty, unsigned int set,
110 static int dgnc_tty_send_break(struct tty_struct *tty, int msec);
111 static void dgnc_tty_wait_until_sent(struct tty_struct *tty, int timeout);
112 static int dgnc_tty_write(struct tty_struct *tty, const unsigned char *buf,
114 static void dgnc_tty_set_termios(struct tty_struct *tty,
115 struct ktermios *old_termios);
116 static void dgnc_tty_send_xchar(struct tty_struct *tty, char ch);
118 static const struct tty_operations dgnc_tty_ops = {
119 .open = dgnc_tty_open,
120 .close = dgnc_tty_close,
121 .write = dgnc_tty_write,
122 .write_room = dgnc_tty_write_room,
123 .flush_buffer = dgnc_tty_flush_buffer,
124 .chars_in_buffer = dgnc_tty_chars_in_buffer,
125 .flush_chars = dgnc_tty_flush_chars,
126 .ioctl = dgnc_tty_ioctl,
127 .set_termios = dgnc_tty_set_termios,
128 .stop = dgnc_tty_stop,
129 .start = dgnc_tty_start,
130 .throttle = dgnc_tty_throttle,
131 .unthrottle = dgnc_tty_unthrottle,
132 .hangup = dgnc_tty_hangup,
133 .put_char = dgnc_tty_put_char,
134 .tiocmget = dgnc_tty_tiocmget,
135 .tiocmset = dgnc_tty_tiocmset,
136 .break_ctl = dgnc_tty_send_break,
137 .wait_until_sent = dgnc_tty_wait_until_sent,
138 .send_xchar = dgnc_tty_send_xchar
141 /************************************************************************
143 * TTY Initialization/Cleanup Functions
145 ************************************************************************/
150 * Initialize any global tty related data before we download any boards.
152 int dgnc_tty_preinit(void)
155 * Allocate a buffer for doing the copy from user space to
156 * kernel space in dgnc_write(). We only use one buffer and
157 * control access to it with a semaphore. If we are paging, we
158 * are already in trouble so one buffer won't hurt much anyway.
160 * We are okay to sleep in the malloc, as this routine
161 * is only called during module load, (not in interrupt context),
162 * and with no locks held.
164 dgnc_TmpWriteBuf = kmalloc(WRITEBUFLEN, GFP_KERNEL);
166 if (!dgnc_TmpWriteBuf)
173 * dgnc_tty_register()
175 * Init the tty subsystem for this board.
177 int dgnc_tty_register(struct dgnc_board *brd)
181 brd->serial_driver = tty_alloc_driver(brd->maxports,
182 TTY_DRIVER_REAL_RAW |
183 TTY_DRIVER_DYNAMIC_DEV |
184 TTY_DRIVER_HARDWARE_BREAK);
186 if (IS_ERR(brd->serial_driver))
187 return PTR_ERR(brd->serial_driver);
189 snprintf(brd->serial_name, MAXTTYNAMELEN, "tty_dgnc_%d_", brd->boardnum);
191 brd->serial_driver->name = brd->serial_name;
192 brd->serial_driver->name_base = 0;
193 brd->serial_driver->major = 0;
194 brd->serial_driver->minor_start = 0;
195 brd->serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
196 brd->serial_driver->subtype = SERIAL_TYPE_NORMAL;
197 brd->serial_driver->init_termios = DgncDefaultTermios;
198 brd->serial_driver->driver_name = DRVSTR;
201 * Entry points for driver. Called by the kernel from
202 * tty_io.c and n_tty.c.
204 tty_set_operations(brd->serial_driver, &dgnc_tty_ops);
206 if (!brd->dgnc_major_serial_registered) {
207 /* Register tty devices */
208 rc = tty_register_driver(brd->serial_driver);
210 dev_dbg(&brd->pdev->dev,
211 "Can't register tty device (%d)\n", rc);
212 goto free_serial_driver;
214 brd->dgnc_major_serial_registered = true;
218 * If we're doing transparent print, we have to do all of the above
219 * again, separately so we don't get the LD confused about what major
220 * we are when we get into the dgnc_tty_open() routine.
222 brd->print_driver = tty_alloc_driver(brd->maxports,
223 TTY_DRIVER_REAL_RAW |
224 TTY_DRIVER_DYNAMIC_DEV |
225 TTY_DRIVER_HARDWARE_BREAK);
227 if (IS_ERR(brd->print_driver)) {
228 rc = PTR_ERR(brd->print_driver);
229 goto unregister_serial_driver;
232 snprintf(brd->print_name, MAXTTYNAMELEN, "pr_dgnc_%d_", brd->boardnum);
234 brd->print_driver->name = brd->print_name;
235 brd->print_driver->name_base = 0;
236 brd->print_driver->major = brd->serial_driver->major;
237 brd->print_driver->minor_start = 0x80;
238 brd->print_driver->type = TTY_DRIVER_TYPE_SERIAL;
239 brd->print_driver->subtype = SERIAL_TYPE_NORMAL;
240 brd->print_driver->init_termios = DgncDefaultTermios;
241 brd->print_driver->driver_name = DRVSTR;
244 * Entry points for driver. Called by the kernel from
245 * tty_io.c and n_tty.c.
247 tty_set_operations(brd->print_driver, &dgnc_tty_ops);
249 if (!brd->dgnc_major_transparent_print_registered) {
250 /* Register Transparent Print devices */
251 rc = tty_register_driver(brd->print_driver);
253 dev_dbg(&brd->pdev->dev,
254 "Can't register Transparent Print device(%d)\n",
256 goto free_print_driver;
258 brd->dgnc_major_transparent_print_registered = true;
261 dgnc_BoardsByMajor[brd->serial_driver->major] = brd;
266 put_tty_driver(brd->print_driver);
267 unregister_serial_driver:
268 tty_unregister_driver(brd->serial_driver);
270 put_tty_driver(brd->serial_driver);
278 * Init the tty subsystem. Called once per board after board has been
279 * downloaded and init'ed.
281 int dgnc_tty_init(struct dgnc_board *brd)
285 struct channel_t *ch;
291 * Initialize board structure elements.
294 vaddr = brd->re_map_membase;
296 brd->nasync = brd->maxports;
298 for (i = 0; i < brd->nasync; i++) {
300 * Okay to malloc with GFP_KERNEL, we are not at
301 * interrupt context, and there are no locks held.
303 brd->channels[i] = kzalloc(sizeof(*brd->channels[i]),
305 if (!brd->channels[i])
306 goto err_free_channels;
309 ch = brd->channels[0];
310 vaddr = brd->re_map_membase;
312 /* Set up channel variables */
313 for (i = 0; i < brd->nasync; i++, ch = brd->channels[i]) {
314 spin_lock_init(&ch->ch_lock);
316 /* Store all our magic numbers */
317 ch->magic = DGNC_CHANNEL_MAGIC;
318 ch->ch_tun.magic = DGNC_UNIT_MAGIC;
319 ch->ch_tun.un_ch = ch;
320 ch->ch_tun.un_type = DGNC_SERIAL;
321 ch->ch_tun.un_dev = i;
323 ch->ch_pun.magic = DGNC_UNIT_MAGIC;
324 ch->ch_pun.un_ch = ch;
325 ch->ch_pun.un_type = DGNC_PRINT;
326 ch->ch_pun.un_dev = i + 128;
328 if (brd->bd_uart_offset == 0x200)
329 ch->ch_neo_uart = vaddr + (brd->bd_uart_offset * i);
331 ch->ch_cls_uart = vaddr + (brd->bd_uart_offset * i);
335 ch->ch_digi = dgnc_digi_init;
337 /* .25 second delay */
338 ch->ch_close_delay = 250;
340 init_waitqueue_head(&ch->ch_flags_wait);
341 init_waitqueue_head(&ch->ch_tun.un_flags_wait);
342 init_waitqueue_head(&ch->ch_pun.un_flags_wait);
345 struct device *classp;
347 classp = tty_register_device(brd->serial_driver, i,
348 &ch->ch_bd->pdev->dev);
349 ch->ch_tun.un_sysfs = classp;
350 dgnc_create_tty_sysfs(&ch->ch_tun, classp);
352 classp = tty_register_device(brd->print_driver, i,
353 &ch->ch_bd->pdev->dev);
354 ch->ch_pun.un_sysfs = classp;
355 dgnc_create_tty_sysfs(&ch->ch_pun, classp);
362 for (i = i - 1; i >= 0; --i) {
363 kfree(brd->channels[i]);
364 brd->channels[i] = NULL;
370 * dgnc_tty_post_uninit()
372 * UnInitialize any global tty related data.
374 void dgnc_tty_post_uninit(void)
376 kfree(dgnc_TmpWriteBuf);
377 dgnc_TmpWriteBuf = NULL;
383 * Uninitialize the TTY portion of this driver. Free all memory and
386 void dgnc_tty_uninit(struct dgnc_board *brd)
390 if (brd->dgnc_major_serial_registered) {
391 dgnc_BoardsByMajor[brd->serial_driver->major] = NULL;
392 for (i = 0; i < brd->nasync; i++) {
393 if (brd->channels[i])
394 dgnc_remove_tty_sysfs(brd->channels[i]->
396 tty_unregister_device(brd->serial_driver, i);
398 tty_unregister_driver(brd->serial_driver);
399 brd->dgnc_major_serial_registered = false;
402 if (brd->dgnc_major_transparent_print_registered) {
403 dgnc_BoardsByMajor[brd->print_driver->major] = NULL;
404 for (i = 0; i < brd->nasync; i++) {
405 if (brd->channels[i])
406 dgnc_remove_tty_sysfs(brd->channels[i]->
408 tty_unregister_device(brd->print_driver, i);
410 tty_unregister_driver(brd->print_driver);
411 brd->dgnc_major_transparent_print_registered = false;
414 put_tty_driver(brd->serial_driver);
415 put_tty_driver(brd->print_driver);
419 * dgnc_wmove - Write data to transmit queue.
421 * ch - Pointer to channel structure.
422 * buf - Pointer to characters to be moved.
423 * n - Number of characters to move.
425 static void dgnc_wmove(struct channel_t *ch, char *buf, uint n)
430 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
433 head = ch->ch_w_head & WQUEUEMASK;
436 * If the write wraps over the top of the circular buffer,
437 * move the portion up to the wrap point, and reset the
438 * pointers to the bottom.
440 remain = WQUEUESIZE - head;
444 memcpy(ch->ch_wqueue + head, buf, remain);
454 memcpy(ch->ch_wqueue + head, buf, remain);
459 ch->ch_w_head = head;
463 * dgnc_input - Process received data.
465 * ch - Pointer to channel structure.
467 void dgnc_input(struct channel_t *ch)
469 struct dgnc_board *bd;
470 struct tty_struct *tp;
471 struct tty_ldisc *ld = NULL;
483 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
486 tp = ch->ch_tun.un_tty;
489 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
492 spin_lock_irqsave(&ch->ch_lock, flags);
495 * Figure the number of characters in the buffer.
496 * Exit immediately if none.
499 head = ch->ch_r_head & rmask;
500 tail = ch->ch_r_tail & rmask;
501 data_len = (head - tail) & rmask;
507 * If the device is not open, or CREAD is off,
508 * flush input data and return immediately.
510 if (!tp || (tp->magic != TTY_MAGIC) ||
511 !(ch->ch_tun.un_flags & UN_ISOPEN) ||
513 (ch->ch_tun.un_flags & UN_CLOSING)) {
514 ch->ch_r_head = tail;
516 /* Force queue flow control to be released, if needed */
517 dgnc_check_queue_flow_control(ch);
523 * If we are throttled, simply don't read any data.
525 if (ch->ch_flags & CH_FORCED_STOPI)
528 flip_len = TTY_FLIPBUF_SIZE;
530 /* Chop down the length, if needed */
531 len = min(data_len, flip_len);
532 len = min(len, (N_TTY_BUF_SIZE - 1));
534 ld = tty_ldisc_ref(tp);
537 * If we were unable to get a reference to the ld,
538 * don't flush our buffer, and act like the ld doesn't
539 * have any space to put the data right now.
545 * If ld doesn't have a pointer to a receive_buf function,
546 * flush the data, then act like the ld doesn't have any
547 * space to put the data right now.
549 if (!ld->ops->receive_buf) {
550 ch->ch_r_head = ch->ch_r_tail;
559 * The tty layer in the kernel has changed in 2.6.16+.
561 * The flip buffers in the tty structure are no longer exposed,
562 * and probably will be going away eventually.
564 * If we are completely raw, we don't need to go through a lot
565 * of the tty layers that exist.
566 * In this case, we take the shortest and fastest route we
567 * can to relay the data to the user.
569 * On the other hand, if we are not raw, we need to go through
570 * the new 2.6.16+ tty layer, which has its API more well defined.
572 len = tty_buffer_request_room(tp->port, len);
576 * n now contains the most amount of data we can copy,
577 * bounded either by how much the Linux tty layer can handle,
578 * or the amount of data the card actually has pending...
581 unsigned char *ch_pos = ch->ch_equeue + tail;
583 s = ((head >= tail) ? head : RQUEUESIZE) - tail;
590 * If conditions are such that ld needs to see all
591 * UART errors, we will have to walk each character
592 * and error byte and send them to the buffer one at
595 if (I_PARMRK(tp) || I_BRKINT(tp) || I_INPCK(tp)) {
596 for (i = 0; i < s; i++) {
597 unsigned char ch = *(ch_pos + i);
598 char flag = TTY_NORMAL;
600 if (ch & UART_LSR_BI)
602 else if (ch & UART_LSR_PE)
604 else if (ch & UART_LSR_FE)
607 tty_insert_flip_char(tp->port, ch, flag);
610 tty_insert_flip_string(tp->port, ch_pos, s);
615 /* Flip queue if needed */
619 ch->ch_r_tail = tail & rmask;
620 ch->ch_e_tail = tail & rmask;
621 dgnc_check_queue_flow_control(ch);
622 spin_unlock_irqrestore(&ch->ch_lock, flags);
624 /* Tell the tty layer its okay to "eat" the data now */
625 tty_flip_buffer_push(tp->port);
632 spin_unlock_irqrestore(&ch->ch_lock, flags);
637 /************************************************************************
638 * Determines when CARRIER changes state and takes appropriate
640 ************************************************************************/
641 void dgnc_carrier(struct channel_t *ch)
643 struct dgnc_board *bd;
645 int virt_carrier = 0;
646 int phys_carrier = 0;
648 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
653 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
656 if (ch->ch_mistat & UART_MSR_DCD)
659 if (ch->ch_digi.digi_flags & DIGI_FORCEDCD)
662 if (ch->ch_c_cflag & CLOCAL)
666 * Test for a VIRTUAL carrier transition to HIGH.
668 if (((ch->ch_flags & CH_FCAR) == 0) && (virt_carrier == 1)) {
670 * When carrier rises, wake any threads waiting
671 * for carrier in the open routine.
674 if (waitqueue_active(&ch->ch_flags_wait))
675 wake_up_interruptible(&ch->ch_flags_wait);
679 * Test for a PHYSICAL carrier transition to HIGH.
681 if (((ch->ch_flags & CH_CD) == 0) && (phys_carrier == 1)) {
683 * When carrier rises, wake any threads waiting
684 * for carrier in the open routine.
687 if (waitqueue_active(&ch->ch_flags_wait))
688 wake_up_interruptible(&ch->ch_flags_wait);
692 * Test for a PHYSICAL transition to low, so long as we aren't
693 * currently ignoring physical transitions (which is what "virtual
694 * carrier" indicates).
696 * The transition of the virtual carrier to low really doesn't
697 * matter... it really only means "ignore carrier state", not
698 * "make pretend that carrier is there".
700 if ((virt_carrier == 0) && ((ch->ch_flags & CH_CD) != 0) &&
701 (phys_carrier == 0)) {
703 * When carrier drops:
705 * Drop carrier on all open units.
707 * Flush queues, waking up any task waiting in the
710 * Send a hangup to the control terminal.
712 * Enable all select calls.
714 if (waitqueue_active(&ch->ch_flags_wait))
715 wake_up_interruptible(&ch->ch_flags_wait);
717 if (ch->ch_tun.un_open_count > 0)
718 tty_hangup(ch->ch_tun.un_tty);
720 if (ch->ch_pun.un_open_count > 0)
721 tty_hangup(ch->ch_pun.un_tty);
725 * Make sure that our cached values reflect the current reality.
727 if (virt_carrier == 1)
728 ch->ch_flags |= CH_FCAR;
730 ch->ch_flags &= ~CH_FCAR;
732 if (phys_carrier == 1)
733 ch->ch_flags |= CH_CD;
735 ch->ch_flags &= ~CH_CD;
739 * Assign the custom baud rate to the channel structure
741 static void dgnc_set_custom_speed(struct channel_t *ch, uint newrate)
750 ch->ch_custom_speed = 0;
755 * Since the divisor is stored in a 16-bit integer, we make sure
756 * we don't allow any rates smaller than a 16-bit integer would allow.
757 * And of course, rates above the dividend won't fly.
759 if (newrate && newrate < ((ch->ch_bd->bd_dividend / 0xFFFF) + 1))
760 newrate = (ch->ch_bd->bd_dividend / 0xFFFF) + 1;
762 if (newrate && newrate > ch->ch_bd->bd_dividend)
763 newrate = ch->ch_bd->bd_dividend;
766 testdiv = ch->ch_bd->bd_dividend / newrate;
769 * If we try to figure out what rate the board would use
770 * with the test divisor, it will be either equal or higher
771 * than the requested baud rate. If we then determine the
772 * rate with a divisor one higher, we will get the next lower
773 * supported rate below the requested.
775 testrate_high = ch->ch_bd->bd_dividend / testdiv;
776 testrate_low = ch->ch_bd->bd_dividend / (testdiv + 1);
779 * If the rate for the requested divisor is correct, just
780 * use it and be done.
782 if (testrate_high != newrate) {
784 * Otherwise, pick the rate that is closer
785 * (i.e. whichever rate has a smaller delta).
787 deltahigh = testrate_high - newrate;
788 deltalow = newrate - testrate_low;
790 if (deltahigh < deltalow)
791 newrate = testrate_high;
793 newrate = testrate_low;
797 ch->ch_custom_speed = newrate;
800 void dgnc_check_queue_flow_control(struct channel_t *ch)
804 /* Store how much space we have left in the queue */
805 qleft = ch->ch_r_tail - ch->ch_r_head - 1;
807 qleft += RQUEUEMASK + 1;
810 * Check to see if we should enforce flow control on our queue because
811 * the ld (or user) isn't reading data out of our queue fast enuf.
813 * NOTE: This is done based on what the current flow control of the
816 * 1) HWFLOW (RTS) - Turn off the UART's Receive interrupt.
817 * This will cause the UART's FIFO to back up, and force
818 * the RTS signal to be dropped.
819 * 2) SWFLOW (IXOFF) - Keep trying to send a stop character to
820 * the other side, in hopes it will stop sending data to us.
821 * 3) NONE - Nothing we can do. We will simply drop any extra data
822 * that gets sent into us when the queue fills up.
826 if (ch->ch_digi.digi_flags & CTSPACE ||
827 ch->ch_c_cflag & CRTSCTS) {
828 if (!(ch->ch_flags & CH_RECEIVER_OFF)) {
829 ch->ch_bd->bd_ops->disable_receiver(ch);
830 ch->ch_flags |= (CH_RECEIVER_OFF);
834 else if (ch->ch_c_iflag & IXOFF) {
835 if (ch->ch_stops_sent <= MAX_STOPS_SENT) {
836 ch->ch_bd->bd_ops->send_stop_character(ch);
843 * Check to see if we should unenforce flow control because
844 * ld (or user) finally read enuf data out of our queue.
846 * NOTE: This is done based on what the current flow control of the
849 * 1) HWFLOW (RTS) - Turn back on the UART's Receive interrupt.
850 * This will cause the UART's FIFO to raise RTS back up,
851 * which will allow the other side to start sending data again.
852 * 2) SWFLOW (IXOFF) - Send a start character to
853 * the other side, so it will start sending data to us again.
854 * 3) NONE - Do nothing. Since we didn't do anything to turn off the
855 * other side, we don't need to do anything now.
857 if (qleft > (RQUEUESIZE / 2)) {
859 if (ch->ch_digi.digi_flags & RTSPACE ||
860 ch->ch_c_cflag & CRTSCTS) {
861 if (ch->ch_flags & CH_RECEIVER_OFF) {
862 ch->ch_bd->bd_ops->enable_receiver(ch);
863 ch->ch_flags &= ~(CH_RECEIVER_OFF);
867 else if (ch->ch_c_iflag & IXOFF && ch->ch_stops_sent) {
868 ch->ch_stops_sent = 0;
869 ch->ch_bd->bd_ops->send_start_character(ch);
874 void dgnc_wakeup_writes(struct channel_t *ch)
879 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
882 spin_lock_irqsave(&ch->ch_lock, flags);
885 * If channel now has space, wake up anyone waiting on the condition.
887 qlen = ch->ch_w_head - ch->ch_w_tail;
891 if (qlen >= (WQUEUESIZE - 256)) {
892 spin_unlock_irqrestore(&ch->ch_lock, flags);
896 if (ch->ch_tun.un_flags & UN_ISOPEN) {
897 tty_wakeup(ch->ch_tun.un_tty);
900 * If unit is set to wait until empty, check to make sure
901 * the queue AND FIFO are both empty.
903 if (ch->ch_tun.un_flags & UN_EMPTY) {
905 (ch->ch_bd->bd_ops->get_uart_bytes_left(ch) == 0)) {
906 ch->ch_tun.un_flags &= ~(UN_EMPTY);
909 * If RTS Toggle mode is on, whenever
910 * the queue and UART is empty, keep RTS low.
912 if (ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE) {
913 ch->ch_mostat &= ~(UART_MCR_RTS);
914 ch->ch_bd->bd_ops->assert_modem_signals(ch);
918 * If DTR Toggle mode is on, whenever
919 * the queue and UART is empty, keep DTR low.
921 if (ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE) {
922 ch->ch_mostat &= ~(UART_MCR_DTR);
923 ch->ch_bd->bd_ops->assert_modem_signals(ch);
928 wake_up_interruptible(&ch->ch_tun.un_flags_wait);
931 if (ch->ch_pun.un_flags & UN_ISOPEN) {
932 tty_wakeup(ch->ch_pun.un_tty);
935 * If unit is set to wait until empty, check to make sure
936 * the queue AND FIFO are both empty.
938 if (ch->ch_pun.un_flags & UN_EMPTY) {
940 (ch->ch_bd->bd_ops->get_uart_bytes_left(ch) == 0))
941 ch->ch_pun.un_flags &= ~(UN_EMPTY);
944 wake_up_interruptible(&ch->ch_pun.un_flags_wait);
947 spin_unlock_irqrestore(&ch->ch_lock, flags);
950 /************************************************************************
952 * TTY Entry points and helper functions
954 ************************************************************************/
960 static int dgnc_tty_open(struct tty_struct *tty, struct file *file)
962 struct dgnc_board *brd;
963 struct channel_t *ch;
972 major = MAJOR(tty_devnum(tty));
973 minor = MINOR(tty_devnum(tty));
978 /* Get board pointer from our array of majors we have allocated */
979 brd = dgnc_BoardsByMajor[major];
984 * If board is not yet up to a state of READY, go to
985 * sleep waiting for it to happen or they cancel the open.
987 rc = wait_event_interruptible(brd->state_wait,
988 (brd->state & BOARD_READY));
993 spin_lock_irqsave(&brd->bd_lock, flags);
995 /* If opened device is greater than our number of ports, bail. */
996 if (PORT_NUM(minor) >= brd->nasync) {
997 spin_unlock_irqrestore(&brd->bd_lock, flags);
1001 ch = brd->channels[PORT_NUM(minor)];
1003 spin_unlock_irqrestore(&brd->bd_lock, flags);
1007 /* Drop board lock */
1008 spin_unlock_irqrestore(&brd->bd_lock, flags);
1010 /* Grab channel lock */
1011 spin_lock_irqsave(&ch->ch_lock, flags);
1013 /* Figure out our type */
1014 if (!IS_PRINT(minor)) {
1015 un = &brd->channels[PORT_NUM(minor)]->ch_tun;
1016 un->un_type = DGNC_SERIAL;
1017 } else if (IS_PRINT(minor)) {
1018 un = &brd->channels[PORT_NUM(minor)]->ch_pun;
1019 un->un_type = DGNC_PRINT;
1021 spin_unlock_irqrestore(&ch->ch_lock, flags);
1026 * If the port is still in a previous open, and in a state
1027 * where we simply cannot safely keep going, wait until the
1030 spin_unlock_irqrestore(&ch->ch_lock, flags);
1032 rc = wait_event_interruptible(ch->ch_flags_wait,
1033 ((ch->ch_flags & CH_OPENING) == 0));
1035 /* If ret is non-zero, user ctrl-c'ed us */
1040 * If either unit is in the middle of the fragile part of close,
1041 * we just cannot touch the channel safely.
1042 * Go to sleep, knowing that when the channel can be
1043 * touched safely, the close routine will signal the
1044 * ch_flags_wait to wake us back up.
1046 rc = wait_event_interruptible(ch->ch_flags_wait,
1047 (((ch->ch_tun.un_flags | ch->ch_pun.un_flags) &
1050 /* If ret is non-zero, user ctrl-c'ed us */
1054 spin_lock_irqsave(&ch->ch_lock, flags);
1056 /* Store our unit into driver_data, so we always have it available. */
1057 tty->driver_data = un;
1062 if (!(un->un_flags & UN_ISOPEN)) {
1063 /* Store important variables. */
1066 /* Maybe do something here to the TTY struct as well? */
1070 * Allocate channel buffers for read/write/error.
1071 * Set flag, so we don't get trounced on.
1073 ch->ch_flags |= (CH_OPENING);
1075 /* Drop locks, as malloc with GFP_KERNEL can sleep */
1076 spin_unlock_irqrestore(&ch->ch_lock, flags);
1079 ch->ch_rqueue = kzalloc(RQUEUESIZE, GFP_KERNEL);
1081 ch->ch_equeue = kzalloc(EQUEUESIZE, GFP_KERNEL);
1083 ch->ch_wqueue = kzalloc(WQUEUESIZE, GFP_KERNEL);
1085 if (!ch->ch_rqueue || !ch->ch_equeue || !ch->ch_wqueue) {
1086 kfree(ch->ch_rqueue);
1087 kfree(ch->ch_equeue);
1088 kfree(ch->ch_wqueue);
1093 spin_lock_irqsave(&ch->ch_lock, flags);
1095 ch->ch_flags &= ~(CH_OPENING);
1096 wake_up_interruptible(&ch->ch_flags_wait);
1099 * Initialize if neither terminal or printer is open.
1101 if (!((ch->ch_tun.un_flags | ch->ch_pun.un_flags) & UN_ISOPEN)) {
1103 * Flush input queues.
1112 brd->bd_ops->flush_uart_write(ch);
1113 brd->bd_ops->flush_uart_read(ch);
1116 ch->ch_cached_lsr = 0;
1117 ch->ch_stop_sending_break = 0;
1118 ch->ch_stops_sent = 0;
1120 ch->ch_c_cflag = tty->termios.c_cflag;
1121 ch->ch_c_iflag = tty->termios.c_iflag;
1122 ch->ch_c_oflag = tty->termios.c_oflag;
1123 ch->ch_c_lflag = tty->termios.c_lflag;
1124 ch->ch_startc = tty->termios.c_cc[VSTART];
1125 ch->ch_stopc = tty->termios.c_cc[VSTOP];
1128 * Bring up RTS and DTR...
1129 * Also handle RTS or DTR toggle if set.
1131 if (!(ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE))
1132 ch->ch_mostat |= (UART_MCR_RTS);
1133 if (!(ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE))
1134 ch->ch_mostat |= (UART_MCR_DTR);
1136 /* Tell UART to init itself */
1137 brd->bd_ops->uart_init(ch);
1141 * Run param in case we changed anything
1143 brd->bd_ops->param(tty);
1148 * follow protocol for opening port
1151 spin_unlock_irqrestore(&ch->ch_lock, flags);
1153 rc = dgnc_block_til_ready(tty, file, ch);
1155 /* No going back now, increment our unit and channel counters */
1156 spin_lock_irqsave(&ch->ch_lock, flags);
1157 ch->ch_open_count++;
1158 un->un_open_count++;
1159 un->un_flags |= (UN_ISOPEN);
1160 spin_unlock_irqrestore(&ch->ch_lock, flags);
1166 * dgnc_block_til_ready()
1168 * Wait for DCD, if needed.
1170 static int dgnc_block_til_ready(struct tty_struct *tty,
1172 struct channel_t *ch)
1175 struct un_t *un = NULL;
1176 unsigned long flags;
1178 int sleep_on_un_flags = 0;
1180 if (!tty || tty->magic != TTY_MAGIC || !file || !ch ||
1181 ch->magic != DGNC_CHANNEL_MAGIC)
1184 un = tty->driver_data;
1185 if (!un || un->magic != DGNC_UNIT_MAGIC)
1188 spin_lock_irqsave(&ch->ch_lock, flags);
1194 sleep_on_un_flags = 0;
1197 * If board has failed somehow during our sleep,
1200 if (ch->ch_bd->state == BOARD_FAILED) {
1205 /* If tty was hung up, break out of loop and set error. */
1206 if (tty_hung_up_p(file)) {
1212 * If either unit is in the middle of the fragile part of close,
1213 * we just cannot touch the channel safely.
1214 * Go back to sleep, knowing that when the channel can be
1215 * touched safely, the close routine will signal the
1216 * ch_wait_flags to wake us back up.
1218 if (!((ch->ch_tun.un_flags |
1219 ch->ch_pun.un_flags) &
1222 * Our conditions to leave cleanly and happily:
1223 * 1) NONBLOCKING on the tty is set.
1225 * 3) DCD (fake or real) is active.
1228 if (file->f_flags & O_NONBLOCK)
1231 if (tty_io_error(tty)) {
1236 if (ch->ch_flags & CH_CD)
1239 if (ch->ch_flags & CH_FCAR)
1242 sleep_on_un_flags = 1;
1246 * If there is a signal pending, the user probably
1247 * interrupted (ctrl-c) us.
1248 * Leave loop with error set.
1250 if (signal_pending(current)) {
1251 retval = -ERESTARTSYS;
1256 * Store the flags before we let go of channel lock
1258 if (sleep_on_un_flags)
1259 old_flags = ch->ch_tun.un_flags | ch->ch_pun.un_flags;
1261 old_flags = ch->ch_flags;
1264 * Let go of channel lock before calling schedule.
1265 * Our poller will get any FEP events and wake us up when DCD
1266 * eventually goes active.
1269 spin_unlock_irqrestore(&ch->ch_lock, flags);
1272 * Wait for something in the flags to change
1273 * from the current value.
1275 if (sleep_on_un_flags)
1276 retval = wait_event_interruptible(un->un_flags_wait,
1277 (old_flags != (ch->ch_tun.un_flags |
1278 ch->ch_pun.un_flags)));
1280 retval = wait_event_interruptible(ch->ch_flags_wait,
1281 (old_flags != ch->ch_flags));
1284 * We got woken up for some reason.
1285 * Before looping around, grab our channel lock.
1287 spin_lock_irqsave(&ch->ch_lock, flags);
1292 spin_unlock_irqrestore(&ch->ch_lock, flags);
1300 * Hangup the port. Like a close, but don't wait for output to drain.
1302 static void dgnc_tty_hangup(struct tty_struct *tty)
1306 if (!tty || tty->magic != TTY_MAGIC)
1309 un = tty->driver_data;
1310 if (!un || un->magic != DGNC_UNIT_MAGIC)
1313 /* flush the transmit queues */
1314 dgnc_tty_flush_buffer(tty);
1321 static void dgnc_tty_close(struct tty_struct *tty, struct file *file)
1323 struct dgnc_board *bd;
1324 struct channel_t *ch;
1326 unsigned long flags;
1328 if (!tty || tty->magic != TTY_MAGIC)
1331 un = tty->driver_data;
1332 if (!un || un->magic != DGNC_UNIT_MAGIC)
1336 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1340 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
1343 spin_lock_irqsave(&ch->ch_lock, flags);
1346 * Determine if this is the last close or not - and if we agree about
1347 * which type of close it is with the Line Discipline
1349 if ((tty->count == 1) && (un->un_open_count != 1)) {
1351 * Uh, oh. tty->count is 1, which means that the tty
1352 * structure will be freed. un_open_count should always
1353 * be one in these conditions. If it's greater than
1354 * one, we've got real problems, since it means the
1355 * serial port won't be shutdown.
1358 "tty->count is 1, un open count is %d\n",
1360 un->un_open_count = 1;
1363 if (un->un_open_count)
1364 un->un_open_count--;
1367 "bad serial port open count of %d\n",
1370 ch->ch_open_count--;
1372 if (ch->ch_open_count && un->un_open_count) {
1373 spin_unlock_irqrestore(&ch->ch_lock, flags);
1377 /* OK, its the last close on the unit */
1378 un->un_flags |= UN_CLOSING;
1383 * Only officially close channel if count is 0 and
1384 * DIGI_PRINTER bit is not set.
1386 if ((ch->ch_open_count == 0) &&
1387 !(ch->ch_digi.digi_flags & DIGI_PRINTER)) {
1388 ch->ch_flags &= ~(CH_STOPI | CH_FORCED_STOPI);
1391 * turn off print device when closing print device.
1393 if ((un->un_type == DGNC_PRINT) && (ch->ch_flags & CH_PRON)) {
1394 dgnc_wmove(ch, ch->ch_digi.digi_offstr,
1395 (int)ch->ch_digi.digi_offlen);
1396 ch->ch_flags &= ~CH_PRON;
1399 spin_unlock_irqrestore(&ch->ch_lock, flags);
1400 /* wait for output to drain */
1401 /* This will also return if we take an interrupt */
1403 bd->bd_ops->drain(tty, 0);
1405 dgnc_tty_flush_buffer(tty);
1406 tty_ldisc_flush(tty);
1408 spin_lock_irqsave(&ch->ch_lock, flags);
1413 * If we have HUPCL set, lower DTR and RTS
1415 if (ch->ch_c_cflag & HUPCL) {
1417 ch->ch_mostat &= ~(UART_MCR_DTR | UART_MCR_RTS);
1418 bd->bd_ops->assert_modem_signals(ch);
1421 * Go to sleep to ensure RTS/DTR
1422 * have been dropped for modems to see it.
1424 if (ch->ch_close_delay) {
1425 spin_unlock_irqrestore(&ch->ch_lock,
1427 dgnc_ms_sleep(ch->ch_close_delay);
1428 spin_lock_irqsave(&ch->ch_lock, flags);
1432 ch->ch_old_baud = 0;
1434 /* Turn off UART interrupts for this port */
1435 ch->ch_bd->bd_ops->uart_off(ch);
1438 * turn off print device when closing print device.
1440 if ((un->un_type == DGNC_PRINT) && (ch->ch_flags & CH_PRON)) {
1441 dgnc_wmove(ch, ch->ch_digi.digi_offstr,
1442 (int)ch->ch_digi.digi_offlen);
1443 ch->ch_flags &= ~CH_PRON;
1448 un->un_flags &= ~(UN_ISOPEN | UN_CLOSING);
1450 wake_up_interruptible(&ch->ch_flags_wait);
1451 wake_up_interruptible(&un->un_flags_wait);
1453 spin_unlock_irqrestore(&ch->ch_lock, flags);
1457 * dgnc_tty_chars_in_buffer()
1459 * Return number of characters that have not been transmitted yet.
1461 * This routine is used by the line discipline to determine if there
1462 * is data waiting to be transmitted/drained/flushed or not.
1464 static int dgnc_tty_chars_in_buffer(struct tty_struct *tty)
1466 struct channel_t *ch = NULL;
1467 struct un_t *un = NULL;
1472 unsigned long flags;
1477 un = tty->driver_data;
1478 if (!un || un->magic != DGNC_UNIT_MAGIC)
1482 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1485 spin_lock_irqsave(&ch->ch_lock, flags);
1488 thead = ch->ch_w_head & tmask;
1489 ttail = ch->ch_w_tail & tmask;
1491 spin_unlock_irqrestore(&ch->ch_lock, flags);
1493 if (ttail == thead) {
1497 chars = thead - ttail;
1499 chars = thead - ttail + WQUEUESIZE;
1508 * Reduces bytes_available to the max number of characters
1509 * that can be sent currently given the maxcps value, and
1510 * returns the new bytes_available. This only affects printer
1513 static int dgnc_maxcps_room(struct tty_struct *tty, int bytes_available)
1515 struct un_t *un = tty->driver_data;
1516 struct channel_t *ch = un->un_ch;
1519 * If its not the Transparent print device, return
1520 * the full data amount.
1522 if (un->un_type != DGNC_PRINT)
1523 return bytes_available;
1525 if (ch->ch_digi.digi_maxcps > 0 && ch->ch_digi.digi_bufsize > 0) {
1527 unsigned long current_time = jiffies;
1528 unsigned long buffer_time = current_time +
1529 (HZ * ch->ch_digi.digi_bufsize) /
1530 ch->ch_digi.digi_maxcps;
1532 if (ch->ch_cpstime < current_time) {
1533 /* buffer is empty */
1534 ch->ch_cpstime = current_time; /* reset ch_cpstime */
1535 cps_limit = ch->ch_digi.digi_bufsize;
1536 } else if (ch->ch_cpstime < buffer_time) {
1537 /* still room in the buffer */
1538 cps_limit = ((buffer_time - ch->ch_cpstime) *
1539 ch->ch_digi.digi_maxcps) / HZ;
1541 /* no room in the buffer */
1545 bytes_available = min(cps_limit, bytes_available);
1548 return bytes_available;
1552 * dgnc_tty_write_room()
1554 * Return space available in Tx buffer
1556 static int dgnc_tty_write_room(struct tty_struct *tty)
1558 struct channel_t *ch = NULL;
1559 struct un_t *un = NULL;
1564 unsigned long flags;
1566 if (!tty || !dgnc_TmpWriteBuf)
1569 un = tty->driver_data;
1570 if (!un || un->magic != DGNC_UNIT_MAGIC)
1574 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1577 spin_lock_irqsave(&ch->ch_lock, flags);
1580 head = (ch->ch_w_head) & tmask;
1581 tail = (ch->ch_w_tail) & tmask;
1583 ret = tail - head - 1;
1587 /* Limit printer to maxcps */
1588 ret = dgnc_maxcps_room(tty, ret);
1591 * If we are printer device, leave space for
1592 * possibly both the on and off strings.
1594 if (un->un_type == DGNC_PRINT) {
1595 if (!(ch->ch_flags & CH_PRON))
1596 ret -= ch->ch_digi.digi_onlen;
1597 ret -= ch->ch_digi.digi_offlen;
1599 if (ch->ch_flags & CH_PRON)
1600 ret -= ch->ch_digi.digi_offlen;
1606 spin_unlock_irqrestore(&ch->ch_lock, flags);
1612 * dgnc_tty_put_char()
1614 * Put a character into ch->ch_buf
1616 * - used by the line discipline for OPOST processing
1618 static int dgnc_tty_put_char(struct tty_struct *tty, unsigned char c)
1621 * Simply call tty_write.
1623 dgnc_tty_write(tty, &c, 1);
1630 * Take data from the user or kernel and send it out to the FEP.
1631 * In here exists all the Transparent Print magic as well.
1633 static int dgnc_tty_write(struct tty_struct *tty,
1634 const unsigned char *buf, int count)
1636 struct channel_t *ch = NULL;
1637 struct un_t *un = NULL;
1638 int bufcount = 0, n = 0;
1639 unsigned long flags;
1645 if (!tty || !dgnc_TmpWriteBuf)
1648 un = tty->driver_data;
1649 if (!un || un->magic != DGNC_UNIT_MAGIC)
1653 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1660 * Store original amount of characters passed in.
1661 * This helps to figure out if we should ask the FEP
1662 * to send us an event when it has more space available.
1665 spin_lock_irqsave(&ch->ch_lock, flags);
1667 /* Get our space available for the channel from the board */
1669 head = (ch->ch_w_head) & tmask;
1670 tail = (ch->ch_w_tail) & tmask;
1672 bufcount = tail - head - 1;
1674 bufcount += WQUEUESIZE;
1677 * Limit printer output to maxcps overall, with bursts allowed
1678 * up to bufsize characters.
1680 bufcount = dgnc_maxcps_room(tty, bufcount);
1683 * Take minimum of what the user wants to send, and the
1684 * space available in the FEP buffer.
1686 count = min(count, bufcount);
1689 * Bail if no space left.
1695 * Output the printer ON string, if we are in terminal mode, but
1696 * need to be in printer mode.
1698 if ((un->un_type == DGNC_PRINT) && !(ch->ch_flags & CH_PRON)) {
1699 dgnc_wmove(ch, ch->ch_digi.digi_onstr,
1700 (int)ch->ch_digi.digi_onlen);
1701 head = (ch->ch_w_head) & tmask;
1702 ch->ch_flags |= CH_PRON;
1706 * On the other hand, output the printer OFF string, if we are
1707 * currently in printer mode, but need to output to the terminal.
1709 if ((un->un_type != DGNC_PRINT) && (ch->ch_flags & CH_PRON)) {
1710 dgnc_wmove(ch, ch->ch_digi.digi_offstr,
1711 (int)ch->ch_digi.digi_offlen);
1712 head = (ch->ch_w_head) & tmask;
1713 ch->ch_flags &= ~CH_PRON;
1719 * If the write wraps over the top of the circular buffer,
1720 * move the portion up to the wrap point, and reset the
1721 * pointers to the bottom.
1723 remain = WQUEUESIZE - head;
1727 memcpy(ch->ch_wqueue + head, buf, remain);
1734 * Move rest of data.
1737 memcpy(ch->ch_wqueue + head, buf, remain);
1743 ch->ch_w_head = head;
1746 /* Update printer buffer empty time. */
1747 if ((un->un_type == DGNC_PRINT) && (ch->ch_digi.digi_maxcps > 0) &&
1748 (ch->ch_digi.digi_bufsize > 0)) {
1749 ch->ch_cpstime += (HZ * count) / ch->ch_digi.digi_maxcps;
1752 spin_unlock_irqrestore(&ch->ch_lock, flags);
1756 * Channel lock is grabbed and then released
1757 * inside this routine.
1759 ch->ch_bd->bd_ops->copy_data_from_queue_to_uart(ch);
1766 spin_unlock_irqrestore(&ch->ch_lock, flags);
1771 * Return modem signals to ld.
1774 static int dgnc_tty_tiocmget(struct tty_struct *tty)
1776 struct channel_t *ch;
1779 unsigned char mstat = 0;
1780 unsigned long flags;
1782 if (!tty || tty->magic != TTY_MAGIC)
1785 un = tty->driver_data;
1786 if (!un || un->magic != DGNC_UNIT_MAGIC)
1790 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1793 spin_lock_irqsave(&ch->ch_lock, flags);
1795 mstat = ch->ch_mostat | ch->ch_mistat;
1797 spin_unlock_irqrestore(&ch->ch_lock, flags);
1801 if (mstat & UART_MCR_DTR)
1802 result |= TIOCM_DTR;
1803 if (mstat & UART_MCR_RTS)
1804 result |= TIOCM_RTS;
1805 if (mstat & UART_MSR_CTS)
1806 result |= TIOCM_CTS;
1807 if (mstat & UART_MSR_DSR)
1808 result |= TIOCM_DSR;
1809 if (mstat & UART_MSR_RI)
1811 if (mstat & UART_MSR_DCD)
1818 * dgnc_tty_tiocmset()
1820 * Set modem signals, called by ld.
1823 static int dgnc_tty_tiocmset(struct tty_struct *tty,
1824 unsigned int set, unsigned int clear)
1826 struct dgnc_board *bd;
1827 struct channel_t *ch;
1830 unsigned long flags;
1832 if (!tty || tty->magic != TTY_MAGIC)
1835 un = tty->driver_data;
1836 if (!un || un->magic != DGNC_UNIT_MAGIC)
1840 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1844 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
1847 spin_lock_irqsave(&ch->ch_lock, flags);
1849 if (set & TIOCM_RTS)
1850 ch->ch_mostat |= UART_MCR_RTS;
1852 if (set & TIOCM_DTR)
1853 ch->ch_mostat |= UART_MCR_DTR;
1855 if (clear & TIOCM_RTS)
1856 ch->ch_mostat &= ~(UART_MCR_RTS);
1858 if (clear & TIOCM_DTR)
1859 ch->ch_mostat &= ~(UART_MCR_DTR);
1861 ch->ch_bd->bd_ops->assert_modem_signals(ch);
1863 spin_unlock_irqrestore(&ch->ch_lock, flags);
1869 * dgnc_tty_send_break()
1871 * Send a Break, called by ld.
1873 static int dgnc_tty_send_break(struct tty_struct *tty, int msec)
1875 struct dgnc_board *bd;
1876 struct channel_t *ch;
1879 unsigned long flags;
1881 if (!tty || tty->magic != TTY_MAGIC)
1884 un = tty->driver_data;
1885 if (!un || un->magic != DGNC_UNIT_MAGIC)
1889 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1893 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
1907 spin_lock_irqsave(&ch->ch_lock, flags);
1909 ch->ch_bd->bd_ops->send_break(ch, msec);
1911 spin_unlock_irqrestore(&ch->ch_lock, flags);
1917 * dgnc_tty_wait_until_sent()
1919 * wait until data has been transmitted, called by ld.
1921 static void dgnc_tty_wait_until_sent(struct tty_struct *tty, int timeout)
1923 struct dgnc_board *bd;
1924 struct channel_t *ch;
1927 if (!tty || tty->magic != TTY_MAGIC)
1930 un = tty->driver_data;
1931 if (!un || un->magic != DGNC_UNIT_MAGIC)
1935 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1939 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
1942 bd->bd_ops->drain(tty, 0);
1948 * send a high priority character, called by ld.
1950 static void dgnc_tty_send_xchar(struct tty_struct *tty, char c)
1952 struct dgnc_board *bd;
1953 struct channel_t *ch;
1955 unsigned long flags;
1957 if (!tty || tty->magic != TTY_MAGIC)
1960 un = tty->driver_data;
1961 if (!un || un->magic != DGNC_UNIT_MAGIC)
1965 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1969 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
1972 dev_dbg(tty->dev, "dgnc_tty_send_xchar start\n");
1974 spin_lock_irqsave(&ch->ch_lock, flags);
1975 bd->bd_ops->send_immediate_char(ch, c);
1976 spin_unlock_irqrestore(&ch->ch_lock, flags);
1978 dev_dbg(tty->dev, "dgnc_tty_send_xchar finish\n");
1982 * Return modem signals to ld.
1984 static inline int dgnc_get_mstat(struct channel_t *ch)
1986 unsigned char mstat;
1988 unsigned long flags;
1990 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1993 spin_lock_irqsave(&ch->ch_lock, flags);
1995 mstat = ch->ch_mostat | ch->ch_mistat;
1997 spin_unlock_irqrestore(&ch->ch_lock, flags);
2001 if (mstat & UART_MCR_DTR)
2002 result |= TIOCM_DTR;
2003 if (mstat & UART_MCR_RTS)
2004 result |= TIOCM_RTS;
2005 if (mstat & UART_MSR_CTS)
2006 result |= TIOCM_CTS;
2007 if (mstat & UART_MSR_DSR)
2008 result |= TIOCM_DSR;
2009 if (mstat & UART_MSR_RI)
2011 if (mstat & UART_MSR_DCD)
2018 * Return modem signals to ld.
2020 static int dgnc_get_modem_info(struct channel_t *ch,
2021 unsigned int __user *value)
2023 return put_user(dgnc_get_mstat(ch), value);
2027 * dgnc_set_modem_info()
2029 * Set modem signals, called by ld.
2031 static int dgnc_set_modem_info(struct tty_struct *tty,
2032 unsigned int command,
2033 unsigned int __user *value)
2035 struct dgnc_board *bd;
2036 struct channel_t *ch;
2039 unsigned int arg = 0;
2040 unsigned long flags;
2042 if (!tty || tty->magic != TTY_MAGIC)
2045 un = tty->driver_data;
2046 if (!un || un->magic != DGNC_UNIT_MAGIC)
2050 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2054 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2057 ret = get_user(arg, value);
2063 if (arg & TIOCM_RTS)
2064 ch->ch_mostat |= UART_MCR_RTS;
2066 if (arg & TIOCM_DTR)
2067 ch->ch_mostat |= UART_MCR_DTR;
2072 if (arg & TIOCM_RTS)
2073 ch->ch_mostat &= ~(UART_MCR_RTS);
2075 if (arg & TIOCM_DTR)
2076 ch->ch_mostat &= ~(UART_MCR_DTR);
2082 if (arg & TIOCM_RTS)
2083 ch->ch_mostat |= UART_MCR_RTS;
2085 ch->ch_mostat &= ~(UART_MCR_RTS);
2087 if (arg & TIOCM_DTR)
2088 ch->ch_mostat |= UART_MCR_DTR;
2090 ch->ch_mostat &= ~(UART_MCR_DTR);
2098 spin_lock_irqsave(&ch->ch_lock, flags);
2100 ch->ch_bd->bd_ops->assert_modem_signals(ch);
2102 spin_unlock_irqrestore(&ch->ch_lock, flags);
2108 * dgnc_tty_digigeta()
2110 * Ioctl to get the information for ditty.
2115 static int dgnc_tty_digigeta(struct tty_struct *tty,
2116 struct digi_t __user *retinfo)
2118 struct channel_t *ch;
2121 unsigned long flags;
2126 if (!tty || tty->magic != TTY_MAGIC)
2129 un = tty->driver_data;
2130 if (!un || un->magic != DGNC_UNIT_MAGIC)
2134 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2137 memset(&tmp, 0, sizeof(tmp));
2139 spin_lock_irqsave(&ch->ch_lock, flags);
2140 memcpy(&tmp, &ch->ch_digi, sizeof(tmp));
2141 spin_unlock_irqrestore(&ch->ch_lock, flags);
2143 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2150 * dgnc_tty_digiseta()
2152 * Ioctl to set the information for ditty.
2157 static int dgnc_tty_digiseta(struct tty_struct *tty,
2158 struct digi_t __user *new_info)
2160 struct dgnc_board *bd;
2161 struct channel_t *ch;
2163 struct digi_t new_digi;
2164 unsigned long flags;
2166 if (!tty || tty->magic != TTY_MAGIC)
2169 un = tty->driver_data;
2170 if (!un || un->magic != DGNC_UNIT_MAGIC)
2174 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2178 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2181 if (copy_from_user(&new_digi, new_info, sizeof(new_digi)))
2184 spin_lock_irqsave(&ch->ch_lock, flags);
2187 * Handle transistions to and from RTS Toggle.
2189 if (!(ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE) &&
2190 (new_digi.digi_flags & DIGI_RTS_TOGGLE))
2191 ch->ch_mostat &= ~(UART_MCR_RTS);
2192 if ((ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE) &&
2193 !(new_digi.digi_flags & DIGI_RTS_TOGGLE))
2194 ch->ch_mostat |= (UART_MCR_RTS);
2197 * Handle transistions to and from DTR Toggle.
2199 if (!(ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE) &&
2200 (new_digi.digi_flags & DIGI_DTR_TOGGLE))
2201 ch->ch_mostat &= ~(UART_MCR_DTR);
2202 if ((ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE) &&
2203 !(new_digi.digi_flags & DIGI_DTR_TOGGLE))
2204 ch->ch_mostat |= (UART_MCR_DTR);
2206 memcpy(&ch->ch_digi, &new_digi, sizeof(new_digi));
2208 if (ch->ch_digi.digi_maxcps < 1)
2209 ch->ch_digi.digi_maxcps = 1;
2211 if (ch->ch_digi.digi_maxcps > 10000)
2212 ch->ch_digi.digi_maxcps = 10000;
2214 if (ch->ch_digi.digi_bufsize < 10)
2215 ch->ch_digi.digi_bufsize = 10;
2217 if (ch->ch_digi.digi_maxchar < 1)
2218 ch->ch_digi.digi_maxchar = 1;
2220 if (ch->ch_digi.digi_maxchar > ch->ch_digi.digi_bufsize)
2221 ch->ch_digi.digi_maxchar = ch->ch_digi.digi_bufsize;
2223 if (ch->ch_digi.digi_onlen > DIGI_PLEN)
2224 ch->ch_digi.digi_onlen = DIGI_PLEN;
2226 if (ch->ch_digi.digi_offlen > DIGI_PLEN)
2227 ch->ch_digi.digi_offlen = DIGI_PLEN;
2229 ch->ch_bd->bd_ops->param(tty);
2231 spin_unlock_irqrestore(&ch->ch_lock, flags);
2237 * dgnc_set_termios()
2239 static void dgnc_tty_set_termios(struct tty_struct *tty,
2240 struct ktermios *old_termios)
2242 struct dgnc_board *bd;
2243 struct channel_t *ch;
2245 unsigned long flags;
2247 if (!tty || tty->magic != TTY_MAGIC)
2250 un = tty->driver_data;
2251 if (!un || un->magic != DGNC_UNIT_MAGIC)
2255 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2259 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2262 spin_lock_irqsave(&ch->ch_lock, flags);
2264 ch->ch_c_cflag = tty->termios.c_cflag;
2265 ch->ch_c_iflag = tty->termios.c_iflag;
2266 ch->ch_c_oflag = tty->termios.c_oflag;
2267 ch->ch_c_lflag = tty->termios.c_lflag;
2268 ch->ch_startc = tty->termios.c_cc[VSTART];
2269 ch->ch_stopc = tty->termios.c_cc[VSTOP];
2271 ch->ch_bd->bd_ops->param(tty);
2274 spin_unlock_irqrestore(&ch->ch_lock, flags);
2277 static void dgnc_tty_throttle(struct tty_struct *tty)
2279 struct channel_t *ch;
2281 unsigned long flags;
2283 if (!tty || tty->magic != TTY_MAGIC)
2286 un = tty->driver_data;
2287 if (!un || un->magic != DGNC_UNIT_MAGIC)
2291 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2294 spin_lock_irqsave(&ch->ch_lock, flags);
2296 ch->ch_flags |= (CH_FORCED_STOPI);
2298 spin_unlock_irqrestore(&ch->ch_lock, flags);
2301 static void dgnc_tty_unthrottle(struct tty_struct *tty)
2303 struct channel_t *ch;
2305 unsigned long flags;
2307 if (!tty || tty->magic != TTY_MAGIC)
2310 un = tty->driver_data;
2311 if (!un || un->magic != DGNC_UNIT_MAGIC)
2315 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2318 spin_lock_irqsave(&ch->ch_lock, flags);
2320 ch->ch_flags &= ~(CH_FORCED_STOPI);
2322 spin_unlock_irqrestore(&ch->ch_lock, flags);
2325 static void dgnc_tty_start(struct tty_struct *tty)
2327 struct dgnc_board *bd;
2328 struct channel_t *ch;
2330 unsigned long flags;
2332 if (!tty || tty->magic != TTY_MAGIC)
2335 un = tty->driver_data;
2336 if (!un || un->magic != DGNC_UNIT_MAGIC)
2340 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2344 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2347 spin_lock_irqsave(&ch->ch_lock, flags);
2349 ch->ch_flags &= ~(CH_FORCED_STOP);
2351 spin_unlock_irqrestore(&ch->ch_lock, flags);
2354 static void dgnc_tty_stop(struct tty_struct *tty)
2356 struct dgnc_board *bd;
2357 struct channel_t *ch;
2359 unsigned long flags;
2361 if (!tty || tty->magic != TTY_MAGIC)
2364 un = tty->driver_data;
2365 if (!un || un->magic != DGNC_UNIT_MAGIC)
2369 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2373 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2376 spin_lock_irqsave(&ch->ch_lock, flags);
2378 ch->ch_flags |= (CH_FORCED_STOP);
2380 spin_unlock_irqrestore(&ch->ch_lock, flags);
2384 * dgnc_tty_flush_chars()
2386 * Flush the cook buffer
2388 * Note to self, and any other poor souls who venture here:
2390 * flush in this case DOES NOT mean dispose of the data.
2391 * instead, it means "stop buffering and send it if you
2392 * haven't already." Just guess how I figured that out... SRW 2-Jun-98
2394 * It is also always called in interrupt context - JAR 8-Sept-99
2396 static void dgnc_tty_flush_chars(struct tty_struct *tty)
2398 struct dgnc_board *bd;
2399 struct channel_t *ch;
2401 unsigned long flags;
2403 if (!tty || tty->magic != TTY_MAGIC)
2406 un = tty->driver_data;
2407 if (!un || un->magic != DGNC_UNIT_MAGIC)
2411 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2415 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2418 spin_lock_irqsave(&ch->ch_lock, flags);
2420 /* Do something maybe here */
2422 spin_unlock_irqrestore(&ch->ch_lock, flags);
2426 * dgnc_tty_flush_buffer()
2428 * Flush Tx buffer (make in == out)
2430 static void dgnc_tty_flush_buffer(struct tty_struct *tty)
2432 struct channel_t *ch;
2434 unsigned long flags;
2436 if (!tty || tty->magic != TTY_MAGIC)
2439 un = tty->driver_data;
2440 if (!un || un->magic != DGNC_UNIT_MAGIC)
2444 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2447 spin_lock_irqsave(&ch->ch_lock, flags);
2449 ch->ch_flags &= ~CH_STOP;
2451 /* Flush our write queue */
2452 ch->ch_w_head = ch->ch_w_tail;
2454 /* Flush UARTs transmit FIFO */
2455 ch->ch_bd->bd_ops->flush_uart_write(ch);
2457 if (ch->ch_tun.un_flags & (UN_LOW | UN_EMPTY)) {
2458 ch->ch_tun.un_flags &= ~(UN_LOW | UN_EMPTY);
2459 wake_up_interruptible(&ch->ch_tun.un_flags_wait);
2461 if (ch->ch_pun.un_flags & (UN_LOW | UN_EMPTY)) {
2462 ch->ch_pun.un_flags &= ~(UN_LOW | UN_EMPTY);
2463 wake_up_interruptible(&ch->ch_pun.un_flags_wait);
2466 spin_unlock_irqrestore(&ch->ch_lock, flags);
2469 /*****************************************************************************
2471 * The IOCTL function and all of its helpers
2473 *****************************************************************************/
2478 * The usual assortment of ioctl's
2480 static int dgnc_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
2483 struct dgnc_board *bd;
2484 struct board_ops *ch_bd_ops;
2485 struct channel_t *ch;
2488 unsigned long flags;
2489 void __user *uarg = (void __user *)arg;
2491 if (!tty || tty->magic != TTY_MAGIC)
2494 un = tty->driver_data;
2495 if (!un || un->magic != DGNC_UNIT_MAGIC)
2499 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2503 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2506 ch_bd_ops = bd->bd_ops;
2508 spin_lock_irqsave(&ch->ch_lock, flags);
2510 if (un->un_open_count <= 0) {
2511 spin_unlock_irqrestore(&ch->ch_lock, flags);
2516 /* Here are all the standard ioctl's that we MUST implement */
2520 * TCSBRK is SVID version: non-zero arg --> no break
2521 * this behaviour is exploited by tcdrain().
2523 * According to POSIX.1 spec (7.2.2.1.2) breaks should be
2524 * between 0.25 and 0.5 seconds so we'll ask for something
2525 * in the middle: 0.375 seconds.
2527 rc = tty_check_change(tty);
2528 spin_unlock_irqrestore(&ch->ch_lock, flags);
2532 rc = ch_bd_ops->drain(tty, 0);
2537 spin_lock_irqsave(&ch->ch_lock, flags);
2539 if (((cmd == TCSBRK) && (!arg)) || (cmd == TCSBRKP))
2540 ch_bd_ops->send_break(ch, 250);
2542 spin_unlock_irqrestore(&ch->ch_lock, flags);
2547 /* support for POSIX tcsendbreak()
2548 * According to POSIX.1 spec (7.2.2.1.2) breaks should be
2549 * between 0.25 and 0.5 seconds so we'll ask for something
2550 * in the middle: 0.375 seconds.
2552 rc = tty_check_change(tty);
2553 spin_unlock_irqrestore(&ch->ch_lock, flags);
2557 rc = ch_bd_ops->drain(tty, 0);
2561 spin_lock_irqsave(&ch->ch_lock, flags);
2563 ch_bd_ops->send_break(ch, 250);
2565 spin_unlock_irqrestore(&ch->ch_lock, flags);
2570 rc = tty_check_change(tty);
2571 spin_unlock_irqrestore(&ch->ch_lock, flags);
2575 rc = ch_bd_ops->drain(tty, 0);
2579 spin_lock_irqsave(&ch->ch_lock, flags);
2581 ch_bd_ops->send_break(ch, 250);
2583 spin_unlock_irqrestore(&ch->ch_lock, flags);
2589 spin_unlock_irqrestore(&ch->ch_lock, flags);
2594 spin_unlock_irqrestore(&ch->ch_lock, flags);
2596 rc = put_user(C_CLOCAL(tty) ? 1 : 0,
2597 (unsigned long __user *)arg);
2602 spin_unlock_irqrestore(&ch->ch_lock, flags);
2603 rc = get_user(arg, (unsigned long __user *)arg);
2607 spin_lock_irqsave(&ch->ch_lock, flags);
2608 tty->termios.c_cflag = ((tty->termios.c_cflag & ~CLOCAL) |
2609 (arg ? CLOCAL : 0));
2610 ch_bd_ops->param(tty);
2611 spin_unlock_irqrestore(&ch->ch_lock, flags);
2616 spin_unlock_irqrestore(&ch->ch_lock, flags);
2617 return dgnc_get_modem_info(ch, uarg);
2622 spin_unlock_irqrestore(&ch->ch_lock, flags);
2623 return dgnc_set_modem_info(tty, cmd, uarg);
2626 * Here are any additional ioctl's that we want to implement
2631 * The linux tty driver doesn't have a flush
2632 * input routine for the driver, assuming all backed
2633 * up data is in the line disc. buffers. However,
2634 * we all know that's not the case. Here, we
2635 * act on the ioctl, but then lie and say we didn't
2636 * so the line discipline will process the flush
2639 rc = tty_check_change(tty);
2641 spin_unlock_irqrestore(&ch->ch_lock, flags);
2645 if ((arg == TCIFLUSH) || (arg == TCIOFLUSH)) {
2646 ch->ch_r_head = ch->ch_r_tail;
2647 ch_bd_ops->flush_uart_read(ch);
2648 /* Force queue flow control to be released, if needed */
2649 dgnc_check_queue_flow_control(ch);
2652 if ((arg == TCOFLUSH) || (arg == TCIOFLUSH)) {
2653 if (!(un->un_type == DGNC_PRINT)) {
2654 ch->ch_w_head = ch->ch_w_tail;
2655 ch_bd_ops->flush_uart_write(ch);
2657 if (ch->ch_tun.un_flags & (UN_LOW | UN_EMPTY)) {
2658 ch->ch_tun.un_flags &=
2659 ~(UN_LOW | UN_EMPTY);
2660 wake_up_interruptible(&ch->ch_tun.un_flags_wait);
2663 if (ch->ch_pun.un_flags & (UN_LOW|UN_EMPTY)) {
2664 ch->ch_pun.un_flags &=
2665 ~(UN_LOW | UN_EMPTY);
2666 wake_up_interruptible(&ch->ch_pun.un_flags_wait);
2671 /* pretend we didn't recognize this IOCTL */
2672 spin_unlock_irqrestore(&ch->ch_lock, flags);
2673 return -ENOIOCTLCMD;
2677 * The linux tty driver doesn't have a flush
2678 * input routine for the driver, assuming all backed
2679 * up data is in the line disc. buffers. However,
2680 * we all know that's not the case. Here, we
2681 * act on the ioctl, but then lie and say we didn't
2682 * so the line discipline will process the flush
2685 if (cmd == TCSETSF) {
2687 ch->ch_flags &= ~CH_STOP;
2688 ch->ch_r_head = ch->ch_r_tail;
2689 ch_bd_ops->flush_uart_read(ch);
2690 /* Force queue flow control to be released, if needed */
2691 dgnc_check_queue_flow_control(ch);
2694 /* now wait for all the output to drain */
2695 spin_unlock_irqrestore(&ch->ch_lock, flags);
2696 rc = ch_bd_ops->drain(tty, 0);
2700 /* pretend we didn't recognize this */
2701 return -ENOIOCTLCMD;
2705 spin_unlock_irqrestore(&ch->ch_lock, flags);
2706 rc = ch_bd_ops->drain(tty, 0);
2710 /* pretend we didn't recognize this */
2711 return -ENOIOCTLCMD;
2714 spin_unlock_irqrestore(&ch->ch_lock, flags);
2715 /* Make the ld do it */
2716 return -ENOIOCTLCMD;
2719 /* get information for ditty */
2720 spin_unlock_irqrestore(&ch->ch_lock, flags);
2721 return dgnc_tty_digigeta(tty, uarg);
2726 /* set information for ditty */
2727 if (cmd == (DIGI_SETAW)) {
2728 spin_unlock_irqrestore(&ch->ch_lock, flags);
2729 rc = ch_bd_ops->drain(tty, 0);
2734 spin_lock_irqsave(&ch->ch_lock, flags);
2736 tty_ldisc_flush(tty);
2741 spin_unlock_irqrestore(&ch->ch_lock, flags);
2742 return dgnc_tty_digiseta(tty, uarg);
2747 /* Let go of locks when accessing user space,
2750 spin_unlock_irqrestore(&ch->ch_lock, flags);
2751 rc = get_user(loopback, (unsigned int __user *)arg);
2754 spin_lock_irqsave(&ch->ch_lock, flags);
2756 /* Enable/disable internal loopback for this port */
2758 ch->ch_flags |= CH_LOOPBACK;
2760 ch->ch_flags &= ~(CH_LOOPBACK);
2762 ch_bd_ops->param(tty);
2763 spin_unlock_irqrestore(&ch->ch_lock, flags);
2767 case DIGI_GETCUSTOMBAUD:
2768 spin_unlock_irqrestore(&ch->ch_lock, flags);
2769 rc = put_user(ch->ch_custom_speed, (unsigned int __user *)arg);
2772 case DIGI_SETCUSTOMBAUD:
2775 /* Let go of locks when accessing user space, could sleep */
2776 spin_unlock_irqrestore(&ch->ch_lock, flags);
2777 rc = get_user(new_rate, (int __user *)arg);
2780 spin_lock_irqsave(&ch->ch_lock, flags);
2781 dgnc_set_custom_speed(ch, new_rate);
2782 ch_bd_ops->param(tty);
2783 spin_unlock_irqrestore(&ch->ch_lock, flags);
2788 * This ioctl allows insertion of a character into the front
2789 * of any pending data to be transmitted.
2791 * This ioctl is to satify the "Send Character Immediate"
2792 * call that the RealPort protocol spec requires.
2794 case DIGI_REALPORT_SENDIMMEDIATE:
2798 spin_unlock_irqrestore(&ch->ch_lock, flags);
2799 rc = get_user(c, (unsigned char __user *)arg);
2802 spin_lock_irqsave(&ch->ch_lock, flags);
2803 ch_bd_ops->send_immediate_char(ch, c);
2804 spin_unlock_irqrestore(&ch->ch_lock, flags);
2809 * This ioctl returns all the current counts for the port.
2811 * This ioctl is to satify the "Line Error Counters"
2812 * call that the RealPort protocol spec requires.
2814 case DIGI_REALPORT_GETCOUNTERS:
2816 struct digi_getcounter buf;
2818 buf.norun = ch->ch_err_overrun;
2819 buf.noflow = 0; /* The driver doesn't keep this stat */
2820 buf.nframe = ch->ch_err_frame;
2821 buf.nparity = ch->ch_err_parity;
2822 buf.nbreak = ch->ch_err_break;
2823 buf.rbytes = ch->ch_rxcount;
2824 buf.tbytes = ch->ch_txcount;
2826 spin_unlock_irqrestore(&ch->ch_lock, flags);
2828 if (copy_to_user(uarg, &buf, sizeof(buf)))
2835 * This ioctl returns all current events.
2837 * This ioctl is to satify the "Event Reporting"
2838 * call that the RealPort protocol spec requires.
2840 case DIGI_REALPORT_GETEVENTS:
2842 unsigned int events = 0;
2844 /* NOTE: MORE EVENTS NEEDS TO BE ADDED HERE */
2845 if (ch->ch_flags & CH_BREAK_SENDING)
2847 if ((ch->ch_flags & CH_STOP) ||
2848 (ch->ch_flags & CH_FORCED_STOP))
2849 events |= (EV_OPU | EV_OPS);
2851 if ((ch->ch_flags & CH_STOPI) ||
2852 (ch->ch_flags & CH_FORCED_STOPI))
2853 events |= (EV_IPU | EV_IPS);
2855 spin_unlock_irqrestore(&ch->ch_lock, flags);
2856 rc = put_user(events, (unsigned int __user *)arg);
2861 * This ioctl returns TOUT and TIN counters based
2862 * upon the values passed in by the RealPort Server.
2863 * It also passes back whether the UART Transmitter is
2866 case DIGI_REALPORT_GETBUFFERS:
2868 struct digi_getbuffer buf;
2872 spin_unlock_irqrestore(&ch->ch_lock, flags);
2875 * Get data from user first.
2877 if (copy_from_user(&buf, uarg, sizeof(buf)))
2880 spin_lock_irqsave(&ch->ch_lock, flags);
2883 * Figure out how much data is in our RX and TX queues.
2885 buf.rxbuf = (ch->ch_r_head - ch->ch_r_tail) & RQUEUEMASK;
2886 buf.txbuf = (ch->ch_w_head - ch->ch_w_tail) & WQUEUEMASK;
2889 * Is the UART empty? Add that value to whats in our TX queue.
2891 count = buf.txbuf + ch_bd_ops->get_uart_bytes_left(ch);
2894 * Figure out how much data the RealPort Server believes should
2895 * be in our TX queue.
2897 tdist = (buf.tx_in - buf.tx_out) & 0xffff;
2900 * If we have more data than the RealPort Server believes we
2901 * should have, reduce our count to its amount.
2903 * This count difference CAN happen because the Linux LD can
2904 * insert more characters into our queue for OPOST processing
2905 * that the RealPort Server doesn't know about.
2907 if (buf.txbuf > tdist)
2911 * Report whether our queue and UART TX are completely empty.
2918 spin_unlock_irqrestore(&ch->ch_lock, flags);
2920 if (copy_to_user(uarg, &buf, sizeof(buf)))
2926 spin_unlock_irqrestore(&ch->ch_lock, flags);
2928 return -ENOIOCTLCMD;