2 * Copyright 2003 Digi International (www.digi.com)
3 * Scott H Kilau <Scott_Kilau at digi dot com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED; without even the
12 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
13 * PURPOSE. See the GNU General Public License for more details.
16 /************************************************************************
18 * This file implements the tty driver functionality for the
19 * Neo and ClassicBoard PCI based product lines.
21 ************************************************************************
25 #include <linux/kernel.h>
26 #include <linux/sched.h> /* For jiffies, task states */
27 #include <linux/interrupt.h> /* For tasklet and interrupt structs/defines */
28 #include <linux/module.h>
29 #include <linux/ctype.h>
30 #include <linux/tty.h>
31 #include <linux/tty_flip.h>
32 #include <linux/types.h>
33 #include <linux/serial_reg.h>
34 #include <linux/slab.h>
35 #include <linux/delay.h> /* For udelay */
36 #include <linux/uaccess.h> /* For copy_from_user/copy_to_user */
37 #include <linux/pci.h>
38 #include "dgnc_driver.h"
42 #include "dgnc_sysfs.h"
43 #include "dgnc_utils.h"
48 static struct dgnc_board *dgnc_BoardsByMajor[256];
49 static unsigned char *dgnc_TmpWriteBuf;
52 * Default transparent print information.
54 static struct digi_t dgnc_digi_init = {
55 .digi_flags = DIGI_COOK, /* Flags */
56 .digi_maxcps = 100, /* Max CPS */
57 .digi_maxchar = 50, /* Max chars in print queue */
58 .digi_bufsize = 100, /* Printer buffer size */
59 .digi_onlen = 4, /* size of printer on string */
60 .digi_offlen = 4, /* size of printer off string */
61 .digi_onstr = "\033[5i", /* ANSI printer on string ] */
62 .digi_offstr = "\033[4i", /* ANSI printer off string ] */
63 .digi_term = "ansi" /* default terminal type */
67 * Define a local default termios struct. All ports will be created
68 * with this termios initially.
70 * This defines a raw port at 9600 baud, 8 data bits, no parity,
73 static struct ktermios DgncDefaultTermios = {
74 .c_iflag = (DEFAULT_IFLAGS), /* iflags */
75 .c_oflag = (DEFAULT_OFLAGS), /* oflags */
76 .c_cflag = (DEFAULT_CFLAGS), /* cflags */
77 .c_lflag = (DEFAULT_LFLAGS), /* lflags */
82 /* Our function prototypes */
83 static int dgnc_tty_open(struct tty_struct *tty, struct file *file);
84 static void dgnc_tty_close(struct tty_struct *tty, struct file *file);
85 static int dgnc_block_til_ready(struct tty_struct *tty, struct file *file,
86 struct channel_t *ch);
87 static int dgnc_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
89 static int dgnc_tty_digigeta(struct tty_struct *tty,
90 struct digi_t __user *retinfo);
91 static int dgnc_tty_digiseta(struct tty_struct *tty,
92 struct digi_t __user *new_info);
93 static int dgnc_tty_write_room(struct tty_struct *tty);
94 static int dgnc_tty_put_char(struct tty_struct *tty, unsigned char c);
95 static int dgnc_tty_chars_in_buffer(struct tty_struct *tty);
96 static void dgnc_tty_start(struct tty_struct *tty);
97 static void dgnc_tty_stop(struct tty_struct *tty);
98 static void dgnc_tty_throttle(struct tty_struct *tty);
99 static void dgnc_tty_unthrottle(struct tty_struct *tty);
100 static void dgnc_tty_flush_chars(struct tty_struct *tty);
101 static void dgnc_tty_flush_buffer(struct tty_struct *tty);
102 static void dgnc_tty_hangup(struct tty_struct *tty);
103 static int dgnc_set_modem_info(struct tty_struct *tty, unsigned int command,
104 unsigned int __user *value);
105 static int dgnc_get_modem_info(struct channel_t *ch,
106 unsigned int __user *value);
107 static int dgnc_tty_tiocmget(struct tty_struct *tty);
108 static int dgnc_tty_tiocmset(struct tty_struct *tty, unsigned int set,
110 static int dgnc_tty_send_break(struct tty_struct *tty, int msec);
111 static void dgnc_tty_wait_until_sent(struct tty_struct *tty, int timeout);
112 static int dgnc_tty_write(struct tty_struct *tty, const unsigned char *buf,
114 static void dgnc_tty_set_termios(struct tty_struct *tty,
115 struct ktermios *old_termios);
116 static void dgnc_tty_send_xchar(struct tty_struct *tty, char ch);
118 static const struct tty_operations dgnc_tty_ops = {
119 .open = dgnc_tty_open,
120 .close = dgnc_tty_close,
121 .write = dgnc_tty_write,
122 .write_room = dgnc_tty_write_room,
123 .flush_buffer = dgnc_tty_flush_buffer,
124 .chars_in_buffer = dgnc_tty_chars_in_buffer,
125 .flush_chars = dgnc_tty_flush_chars,
126 .ioctl = dgnc_tty_ioctl,
127 .set_termios = dgnc_tty_set_termios,
128 .stop = dgnc_tty_stop,
129 .start = dgnc_tty_start,
130 .throttle = dgnc_tty_throttle,
131 .unthrottle = dgnc_tty_unthrottle,
132 .hangup = dgnc_tty_hangup,
133 .put_char = dgnc_tty_put_char,
134 .tiocmget = dgnc_tty_tiocmget,
135 .tiocmset = dgnc_tty_tiocmset,
136 .break_ctl = dgnc_tty_send_break,
137 .wait_until_sent = dgnc_tty_wait_until_sent,
138 .send_xchar = dgnc_tty_send_xchar
141 /************************************************************************
143 * TTY Initialization/Cleanup Functions
145 ************************************************************************/
150 * Initialize any global tty related data before we download any boards.
152 int dgnc_tty_preinit(void)
155 * Allocate a buffer for doing the copy from user space to
156 * kernel space in dgnc_write(). We only use one buffer and
157 * control access to it with a semaphore. If we are paging, we
158 * are already in trouble so one buffer won't hurt much anyway.
160 * We are okay to sleep in the malloc, as this routine
161 * is only called during module load, (not in interrupt context),
162 * and with no locks held.
164 dgnc_TmpWriteBuf = kmalloc(WRITEBUFLEN, GFP_KERNEL);
166 if (!dgnc_TmpWriteBuf)
173 * dgnc_tty_register()
175 * Init the tty subsystem for this board.
177 int dgnc_tty_register(struct dgnc_board *brd)
181 brd->SerialDriver.magic = TTY_DRIVER_MAGIC;
183 snprintf(brd->SerialName, MAXTTYNAMELEN, "tty_dgnc_%d_", brd->boardnum);
185 brd->SerialDriver.name = brd->SerialName;
186 brd->SerialDriver.name_base = 0;
187 brd->SerialDriver.major = 0;
188 brd->SerialDriver.minor_start = 0;
189 brd->SerialDriver.num = brd->maxports;
190 brd->SerialDriver.type = TTY_DRIVER_TYPE_SERIAL;
191 brd->SerialDriver.subtype = SERIAL_TYPE_NORMAL;
192 brd->SerialDriver.init_termios = DgncDefaultTermios;
193 brd->SerialDriver.driver_name = DRVSTR;
194 brd->SerialDriver.flags = (TTY_DRIVER_REAL_RAW |
195 TTY_DRIVER_DYNAMIC_DEV |
196 TTY_DRIVER_HARDWARE_BREAK);
199 * The kernel wants space to store pointers to
200 * tty_struct's and termios's.
202 brd->SerialDriver.ttys = kcalloc(brd->maxports,
203 sizeof(*brd->SerialDriver.ttys),
205 if (!brd->SerialDriver.ttys)
208 kref_init(&brd->SerialDriver.kref);
209 brd->SerialDriver.termios = kcalloc(brd->maxports,
210 sizeof(*brd->SerialDriver.termios),
212 if (!brd->SerialDriver.termios)
216 * Entry points for driver. Called by the kernel from
217 * tty_io.c and n_tty.c.
219 tty_set_operations(&brd->SerialDriver, &dgnc_tty_ops);
221 if (!brd->dgnc_Major_Serial_Registered) {
222 /* Register tty devices */
223 rc = tty_register_driver(&brd->SerialDriver);
225 dev_dbg(&brd->pdev->dev,
226 "Can't register tty device (%d)\n", rc);
229 brd->dgnc_Major_Serial_Registered = true;
233 * If we're doing transparent print, we have to do all of the above
234 * again, separately so we don't get the LD confused about what major
235 * we are when we get into the dgnc_tty_open() routine.
237 brd->PrintDriver.magic = TTY_DRIVER_MAGIC;
238 snprintf(brd->PrintName, MAXTTYNAMELEN, "pr_dgnc_%d_", brd->boardnum);
240 brd->PrintDriver.name = brd->PrintName;
241 brd->PrintDriver.name_base = 0;
242 brd->PrintDriver.major = brd->SerialDriver.major;
243 brd->PrintDriver.minor_start = 0x80;
244 brd->PrintDriver.num = brd->maxports;
245 brd->PrintDriver.type = TTY_DRIVER_TYPE_SERIAL;
246 brd->PrintDriver.subtype = SERIAL_TYPE_NORMAL;
247 brd->PrintDriver.init_termios = DgncDefaultTermios;
248 brd->PrintDriver.driver_name = DRVSTR;
249 brd->PrintDriver.flags = (TTY_DRIVER_REAL_RAW |
250 TTY_DRIVER_DYNAMIC_DEV |
251 TTY_DRIVER_HARDWARE_BREAK);
254 * The kernel wants space to store pointers to
255 * tty_struct's and termios's. Must be separated from
256 * the Serial Driver so we don't get confused
258 brd->PrintDriver.ttys = kcalloc(brd->maxports,
259 sizeof(*brd->PrintDriver.ttys),
261 if (!brd->PrintDriver.ttys)
263 kref_init(&brd->PrintDriver.kref);
264 brd->PrintDriver.termios = kcalloc(brd->maxports,
265 sizeof(*brd->PrintDriver.termios),
267 if (!brd->PrintDriver.termios)
271 * Entry points for driver. Called by the kernel from
272 * tty_io.c and n_tty.c.
274 tty_set_operations(&brd->PrintDriver, &dgnc_tty_ops);
276 if (!brd->dgnc_Major_TransparentPrint_Registered) {
277 /* Register Transparent Print devices */
278 rc = tty_register_driver(&brd->PrintDriver);
280 dev_dbg(&brd->pdev->dev,
281 "Can't register Transparent Print device(%d)\n",
285 brd->dgnc_Major_TransparentPrint_Registered = true;
288 dgnc_BoardsByMajor[brd->SerialDriver.major] = brd;
289 brd->dgnc_Serial_Major = brd->SerialDriver.major;
290 brd->dgnc_TransparentPrint_Major = brd->PrintDriver.major;
298 * Init the tty subsystem. Called once per board after board has been
299 * downloaded and init'ed.
301 int dgnc_tty_init(struct dgnc_board *brd)
305 struct channel_t *ch;
311 * Initialize board structure elements.
314 vaddr = brd->re_map_membase;
316 brd->nasync = brd->maxports;
318 for (i = 0; i < brd->nasync; i++) {
320 * Okay to malloc with GFP_KERNEL, we are not at
321 * interrupt context, and there are no locks held.
323 brd->channels[i] = kzalloc(sizeof(*brd->channels[i]),
325 if (!brd->channels[i])
326 goto err_free_channels;
329 ch = brd->channels[0];
330 vaddr = brd->re_map_membase;
332 /* Set up channel variables */
333 for (i = 0; i < brd->nasync; i++, ch = brd->channels[i]) {
334 spin_lock_init(&ch->ch_lock);
336 /* Store all our magic numbers */
337 ch->magic = DGNC_CHANNEL_MAGIC;
338 ch->ch_tun.magic = DGNC_UNIT_MAGIC;
339 ch->ch_tun.un_ch = ch;
340 ch->ch_tun.un_type = DGNC_SERIAL;
341 ch->ch_tun.un_dev = i;
343 ch->ch_pun.magic = DGNC_UNIT_MAGIC;
344 ch->ch_pun.un_ch = ch;
345 ch->ch_pun.un_type = DGNC_PRINT;
346 ch->ch_pun.un_dev = i + 128;
348 if (brd->bd_uart_offset == 0x200)
349 ch->ch_neo_uart = vaddr + (brd->bd_uart_offset * i);
351 ch->ch_cls_uart = vaddr + (brd->bd_uart_offset * i);
355 ch->ch_digi = dgnc_digi_init;
357 /* .25 second delay */
358 ch->ch_close_delay = 250;
360 init_waitqueue_head(&ch->ch_flags_wait);
361 init_waitqueue_head(&ch->ch_tun.un_flags_wait);
362 init_waitqueue_head(&ch->ch_pun.un_flags_wait);
365 struct device *classp;
367 classp = tty_register_device(&brd->SerialDriver, i,
368 &ch->ch_bd->pdev->dev);
369 ch->ch_tun.un_sysfs = classp;
370 dgnc_create_tty_sysfs(&ch->ch_tun, classp);
372 classp = tty_register_device(&brd->PrintDriver, i,
373 &ch->ch_bd->pdev->dev);
374 ch->ch_pun.un_sysfs = classp;
375 dgnc_create_tty_sysfs(&ch->ch_pun, classp);
382 for (i = i - 1; i >= 0; --i) {
383 kfree(brd->channels[i]);
384 brd->channels[i] = NULL;
390 * dgnc_tty_post_uninit()
392 * UnInitialize any global tty related data.
394 void dgnc_tty_post_uninit(void)
396 kfree(dgnc_TmpWriteBuf);
397 dgnc_TmpWriteBuf = NULL;
403 * Uninitialize the TTY portion of this driver. Free all memory and
406 void dgnc_tty_uninit(struct dgnc_board *brd)
410 if (brd->dgnc_Major_Serial_Registered) {
411 dgnc_BoardsByMajor[brd->SerialDriver.major] = NULL;
412 brd->dgnc_Serial_Major = 0;
413 for (i = 0; i < brd->nasync; i++) {
414 if (brd->channels[i])
415 dgnc_remove_tty_sysfs(brd->channels[i]->
417 tty_unregister_device(&brd->SerialDriver, i);
419 tty_unregister_driver(&brd->SerialDriver);
420 brd->dgnc_Major_Serial_Registered = false;
423 if (brd->dgnc_Major_TransparentPrint_Registered) {
424 dgnc_BoardsByMajor[brd->PrintDriver.major] = NULL;
425 brd->dgnc_TransparentPrint_Major = 0;
426 for (i = 0; i < brd->nasync; i++) {
427 if (brd->channels[i])
428 dgnc_remove_tty_sysfs(brd->channels[i]->
430 tty_unregister_device(&brd->PrintDriver, i);
432 tty_unregister_driver(&brd->PrintDriver);
433 brd->dgnc_Major_TransparentPrint_Registered = false;
436 kfree(brd->SerialDriver.ttys);
437 brd->SerialDriver.ttys = NULL;
438 kfree(brd->SerialDriver.termios);
439 brd->SerialDriver.termios = NULL;
440 kfree(brd->PrintDriver.ttys);
441 brd->PrintDriver.ttys = NULL;
442 kfree(brd->PrintDriver.termios);
443 brd->PrintDriver.termios = NULL;
447 * dgnc_wmove - Write data to transmit queue.
449 * ch - Pointer to channel structure.
450 * buf - Pointer to characters to be moved.
451 * n - Number of characters to move.
453 static void dgnc_wmove(struct channel_t *ch, char *buf, uint n)
458 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
461 head = ch->ch_w_head & WQUEUEMASK;
464 * If the write wraps over the top of the circular buffer,
465 * move the portion up to the wrap point, and reset the
466 * pointers to the bottom.
468 remain = WQUEUESIZE - head;
472 memcpy(ch->ch_wqueue + head, buf, remain);
482 memcpy(ch->ch_wqueue + head, buf, remain);
487 ch->ch_w_head = head;
491 * dgnc_input - Process received data.
493 * ch - Pointer to channel structure.
495 void dgnc_input(struct channel_t *ch)
497 struct dgnc_board *bd;
498 struct tty_struct *tp;
499 struct tty_ldisc *ld = NULL;
511 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
514 tp = ch->ch_tun.un_tty;
517 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
520 spin_lock_irqsave(&ch->ch_lock, flags);
523 * Figure the number of characters in the buffer.
524 * Exit immediately if none.
527 head = ch->ch_r_head & rmask;
528 tail = ch->ch_r_tail & rmask;
529 data_len = (head - tail) & rmask;
535 * If the device is not open, or CREAD is off,
536 * flush input data and return immediately.
538 if (!tp || (tp->magic != TTY_MAGIC) ||
539 !(ch->ch_tun.un_flags & UN_ISOPEN) ||
541 (ch->ch_tun.un_flags & UN_CLOSING)) {
542 ch->ch_r_head = tail;
544 /* Force queue flow control to be released, if needed */
545 dgnc_check_queue_flow_control(ch);
551 * If we are throttled, simply don't read any data.
553 if (ch->ch_flags & CH_FORCED_STOPI)
556 flip_len = TTY_FLIPBUF_SIZE;
558 /* Chop down the length, if needed */
559 len = min(data_len, flip_len);
560 len = min(len, (N_TTY_BUF_SIZE - 1));
562 ld = tty_ldisc_ref(tp);
565 * If we were unable to get a reference to the ld,
566 * don't flush our buffer, and act like the ld doesn't
567 * have any space to put the data right now.
573 * If ld doesn't have a pointer to a receive_buf function,
574 * flush the data, then act like the ld doesn't have any
575 * space to put the data right now.
577 if (!ld->ops->receive_buf) {
578 ch->ch_r_head = ch->ch_r_tail;
587 * The tty layer in the kernel has changed in 2.6.16+.
589 * The flip buffers in the tty structure are no longer exposed,
590 * and probably will be going away eventually.
592 * If we are completely raw, we don't need to go through a lot
593 * of the tty layers that exist.
594 * In this case, we take the shortest and fastest route we
595 * can to relay the data to the user.
597 * On the other hand, if we are not raw, we need to go through
598 * the new 2.6.16+ tty layer, which has its API more well defined.
600 len = tty_buffer_request_room(tp->port, len);
604 * n now contains the most amount of data we can copy,
605 * bounded either by how much the Linux tty layer can handle,
606 * or the amount of data the card actually has pending...
609 s = ((head >= tail) ? head : RQUEUESIZE) - tail;
616 * If conditions are such that ld needs to see all
617 * UART errors, we will have to walk each character
618 * and error byte and send them to the buffer one at
621 if (I_PARMRK(tp) || I_BRKINT(tp) || I_INPCK(tp)) {
622 for (i = 0; i < s; i++) {
623 if (*(ch->ch_equeue + tail + i) & UART_LSR_BI)
624 tty_insert_flip_char(tp->port,
625 *(ch->ch_rqueue + tail + i),
627 else if (*(ch->ch_equeue + tail + i) &
629 tty_insert_flip_char(tp->port,
630 *(ch->ch_rqueue + tail + i),
632 else if (*(ch->ch_equeue + tail + i) &
634 tty_insert_flip_char(tp->port,
635 *(ch->ch_rqueue + tail + i),
638 tty_insert_flip_char(tp->port,
639 *(ch->ch_rqueue + tail + i),
643 tty_insert_flip_string(tp->port,
644 ch->ch_rqueue + tail,
650 /* Flip queue if needed */
654 ch->ch_r_tail = tail & rmask;
655 ch->ch_e_tail = tail & rmask;
656 dgnc_check_queue_flow_control(ch);
657 spin_unlock_irqrestore(&ch->ch_lock, flags);
659 /* Tell the tty layer its okay to "eat" the data now */
660 tty_flip_buffer_push(tp->port);
667 spin_unlock_irqrestore(&ch->ch_lock, flags);
672 /************************************************************************
673 * Determines when CARRIER changes state and takes appropriate
675 ************************************************************************/
676 void dgnc_carrier(struct channel_t *ch)
678 struct dgnc_board *bd;
680 int virt_carrier = 0;
681 int phys_carrier = 0;
683 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
688 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
691 if (ch->ch_mistat & UART_MSR_DCD)
694 if (ch->ch_digi.digi_flags & DIGI_FORCEDCD)
697 if (ch->ch_c_cflag & CLOCAL)
701 * Test for a VIRTUAL carrier transition to HIGH.
703 if (((ch->ch_flags & CH_FCAR) == 0) && (virt_carrier == 1)) {
705 * When carrier rises, wake any threads waiting
706 * for carrier in the open routine.
709 if (waitqueue_active(&ch->ch_flags_wait))
710 wake_up_interruptible(&ch->ch_flags_wait);
714 * Test for a PHYSICAL carrier transition to HIGH.
716 if (((ch->ch_flags & CH_CD) == 0) && (phys_carrier == 1)) {
718 * When carrier rises, wake any threads waiting
719 * for carrier in the open routine.
722 if (waitqueue_active(&ch->ch_flags_wait))
723 wake_up_interruptible(&ch->ch_flags_wait);
727 * Test for a PHYSICAL transition to low, so long as we aren't
728 * currently ignoring physical transitions (which is what "virtual
729 * carrier" indicates).
731 * The transition of the virtual carrier to low really doesn't
732 * matter... it really only means "ignore carrier state", not
733 * "make pretend that carrier is there".
735 if ((virt_carrier == 0) && ((ch->ch_flags & CH_CD) != 0) &&
736 (phys_carrier == 0)) {
738 * When carrier drops:
740 * Drop carrier on all open units.
742 * Flush queues, waking up any task waiting in the
745 * Send a hangup to the control terminal.
747 * Enable all select calls.
749 if (waitqueue_active(&ch->ch_flags_wait))
750 wake_up_interruptible(&ch->ch_flags_wait);
752 if (ch->ch_tun.un_open_count > 0)
753 tty_hangup(ch->ch_tun.un_tty);
755 if (ch->ch_pun.un_open_count > 0)
756 tty_hangup(ch->ch_pun.un_tty);
760 * Make sure that our cached values reflect the current reality.
762 if (virt_carrier == 1)
763 ch->ch_flags |= CH_FCAR;
765 ch->ch_flags &= ~CH_FCAR;
767 if (phys_carrier == 1)
768 ch->ch_flags |= CH_CD;
770 ch->ch_flags &= ~CH_CD;
774 * Assign the custom baud rate to the channel structure
776 static void dgnc_set_custom_speed(struct channel_t *ch, uint newrate)
785 ch->ch_custom_speed = 0;
790 * Since the divisor is stored in a 16-bit integer, we make sure
791 * we don't allow any rates smaller than a 16-bit integer would allow.
792 * And of course, rates above the dividend won't fly.
794 if (newrate && newrate < ((ch->ch_bd->bd_dividend / 0xFFFF) + 1))
795 newrate = (ch->ch_bd->bd_dividend / 0xFFFF) + 1;
797 if (newrate && newrate > ch->ch_bd->bd_dividend)
798 newrate = ch->ch_bd->bd_dividend;
801 testdiv = ch->ch_bd->bd_dividend / newrate;
804 * If we try to figure out what rate the board would use
805 * with the test divisor, it will be either equal or higher
806 * than the requested baud rate. If we then determine the
807 * rate with a divisor one higher, we will get the next lower
808 * supported rate below the requested.
810 testrate_high = ch->ch_bd->bd_dividend / testdiv;
811 testrate_low = ch->ch_bd->bd_dividend / (testdiv + 1);
814 * If the rate for the requested divisor is correct, just
815 * use it and be done.
817 if (testrate_high != newrate) {
819 * Otherwise, pick the rate that is closer
820 * (i.e. whichever rate has a smaller delta).
822 deltahigh = testrate_high - newrate;
823 deltalow = newrate - testrate_low;
825 if (deltahigh < deltalow)
826 newrate = testrate_high;
828 newrate = testrate_low;
832 ch->ch_custom_speed = newrate;
835 void dgnc_check_queue_flow_control(struct channel_t *ch)
839 /* Store how much space we have left in the queue */
840 qleft = ch->ch_r_tail - ch->ch_r_head - 1;
842 qleft += RQUEUEMASK + 1;
845 * Check to see if we should enforce flow control on our queue because
846 * the ld (or user) isn't reading data out of our queue fast enuf.
848 * NOTE: This is done based on what the current flow control of the
851 * 1) HWFLOW (RTS) - Turn off the UART's Receive interrupt.
852 * This will cause the UART's FIFO to back up, and force
853 * the RTS signal to be dropped.
854 * 2) SWFLOW (IXOFF) - Keep trying to send a stop character to
855 * the other side, in hopes it will stop sending data to us.
856 * 3) NONE - Nothing we can do. We will simply drop any extra data
857 * that gets sent into us when the queue fills up.
861 if (ch->ch_digi.digi_flags & CTSPACE ||
862 ch->ch_c_cflag & CRTSCTS) {
863 if (!(ch->ch_flags & CH_RECEIVER_OFF)) {
864 ch->ch_bd->bd_ops->disable_receiver(ch);
865 ch->ch_flags |= (CH_RECEIVER_OFF);
869 else if (ch->ch_c_iflag & IXOFF) {
870 if (ch->ch_stops_sent <= MAX_STOPS_SENT) {
871 ch->ch_bd->bd_ops->send_stop_character(ch);
878 * Check to see if we should unenforce flow control because
879 * ld (or user) finally read enuf data out of our queue.
881 * NOTE: This is done based on what the current flow control of the
884 * 1) HWFLOW (RTS) - Turn back on the UART's Receive interrupt.
885 * This will cause the UART's FIFO to raise RTS back up,
886 * which will allow the other side to start sending data again.
887 * 2) SWFLOW (IXOFF) - Send a start character to
888 * the other side, so it will start sending data to us again.
889 * 3) NONE - Do nothing. Since we didn't do anything to turn off the
890 * other side, we don't need to do anything now.
892 if (qleft > (RQUEUESIZE / 2)) {
894 if (ch->ch_digi.digi_flags & RTSPACE ||
895 ch->ch_c_cflag & CRTSCTS) {
896 if (ch->ch_flags & CH_RECEIVER_OFF) {
897 ch->ch_bd->bd_ops->enable_receiver(ch);
898 ch->ch_flags &= ~(CH_RECEIVER_OFF);
902 else if (ch->ch_c_iflag & IXOFF && ch->ch_stops_sent) {
903 ch->ch_stops_sent = 0;
904 ch->ch_bd->bd_ops->send_start_character(ch);
909 void dgnc_wakeup_writes(struct channel_t *ch)
914 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
917 spin_lock_irqsave(&ch->ch_lock, flags);
920 * If channel now has space, wake up anyone waiting on the condition.
922 qlen = ch->ch_w_head - ch->ch_w_tail;
926 if (qlen >= (WQUEUESIZE - 256)) {
927 spin_unlock_irqrestore(&ch->ch_lock, flags);
931 if (ch->ch_tun.un_flags & UN_ISOPEN) {
932 tty_wakeup(ch->ch_tun.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_tun.un_flags & UN_EMPTY) {
940 (ch->ch_bd->bd_ops->get_uart_bytes_left(ch) == 0)) {
941 ch->ch_tun.un_flags &= ~(UN_EMPTY);
944 * If RTS Toggle mode is on, whenever
945 * the queue and UART is empty, keep RTS low.
947 if (ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE) {
948 ch->ch_mostat &= ~(UART_MCR_RTS);
949 ch->ch_bd->bd_ops->assert_modem_signals(ch);
953 * If DTR Toggle mode is on, whenever
954 * the queue and UART is empty, keep DTR low.
956 if (ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE) {
957 ch->ch_mostat &= ~(UART_MCR_DTR);
958 ch->ch_bd->bd_ops->assert_modem_signals(ch);
963 wake_up_interruptible(&ch->ch_tun.un_flags_wait);
966 if (ch->ch_pun.un_flags & UN_ISOPEN) {
967 tty_wakeup(ch->ch_pun.un_tty);
970 * If unit is set to wait until empty, check to make sure
971 * the queue AND FIFO are both empty.
973 if (ch->ch_pun.un_flags & UN_EMPTY) {
975 (ch->ch_bd->bd_ops->get_uart_bytes_left(ch) == 0))
976 ch->ch_pun.un_flags &= ~(UN_EMPTY);
979 wake_up_interruptible(&ch->ch_pun.un_flags_wait);
982 spin_unlock_irqrestore(&ch->ch_lock, flags);
985 /************************************************************************
987 * TTY Entry points and helper functions
989 ************************************************************************/
995 static int dgnc_tty_open(struct tty_struct *tty, struct file *file)
997 struct dgnc_board *brd;
998 struct channel_t *ch;
1003 unsigned long flags;
1007 major = MAJOR(tty_devnum(tty));
1008 minor = MINOR(tty_devnum(tty));
1013 /* Get board pointer from our array of majors we have allocated */
1014 brd = dgnc_BoardsByMajor[major];
1019 * If board is not yet up to a state of READY, go to
1020 * sleep waiting for it to happen or they cancel the open.
1022 rc = wait_event_interruptible(brd->state_wait,
1023 (brd->state & BOARD_READY));
1028 spin_lock_irqsave(&brd->bd_lock, flags);
1030 /* If opened device is greater than our number of ports, bail. */
1031 if (PORT_NUM(minor) >= brd->nasync) {
1032 spin_unlock_irqrestore(&brd->bd_lock, flags);
1036 ch = brd->channels[PORT_NUM(minor)];
1038 spin_unlock_irqrestore(&brd->bd_lock, flags);
1042 /* Drop board lock */
1043 spin_unlock_irqrestore(&brd->bd_lock, flags);
1045 /* Grab channel lock */
1046 spin_lock_irqsave(&ch->ch_lock, flags);
1048 /* Figure out our type */
1049 if (!IS_PRINT(minor)) {
1050 un = &brd->channels[PORT_NUM(minor)]->ch_tun;
1051 un->un_type = DGNC_SERIAL;
1052 } else if (IS_PRINT(minor)) {
1053 un = &brd->channels[PORT_NUM(minor)]->ch_pun;
1054 un->un_type = DGNC_PRINT;
1056 spin_unlock_irqrestore(&ch->ch_lock, flags);
1061 * If the port is still in a previous open, and in a state
1062 * where we simply cannot safely keep going, wait until the
1065 spin_unlock_irqrestore(&ch->ch_lock, flags);
1067 rc = wait_event_interruptible(ch->ch_flags_wait,
1068 ((ch->ch_flags & CH_OPENING) == 0));
1070 /* If ret is non-zero, user ctrl-c'ed us */
1075 * If either unit is in the middle of the fragile part of close,
1076 * we just cannot touch the channel safely.
1077 * Go to sleep, knowing that when the channel can be
1078 * touched safely, the close routine will signal the
1079 * ch_flags_wait to wake us back up.
1081 rc = wait_event_interruptible(ch->ch_flags_wait,
1082 (((ch->ch_tun.un_flags | ch->ch_pun.un_flags) &
1085 /* If ret is non-zero, user ctrl-c'ed us */
1089 spin_lock_irqsave(&ch->ch_lock, flags);
1091 /* Store our unit into driver_data, so we always have it available. */
1092 tty->driver_data = un;
1097 if (!(un->un_flags & UN_ISOPEN)) {
1098 /* Store important variables. */
1101 /* Maybe do something here to the TTY struct as well? */
1105 * Allocate channel buffers for read/write/error.
1106 * Set flag, so we don't get trounced on.
1108 ch->ch_flags |= (CH_OPENING);
1110 /* Drop locks, as malloc with GFP_KERNEL can sleep */
1111 spin_unlock_irqrestore(&ch->ch_lock, flags);
1114 ch->ch_rqueue = kzalloc(RQUEUESIZE, GFP_KERNEL);
1116 ch->ch_equeue = kzalloc(EQUEUESIZE, GFP_KERNEL);
1118 ch->ch_wqueue = kzalloc(WQUEUESIZE, GFP_KERNEL);
1120 spin_lock_irqsave(&ch->ch_lock, flags);
1122 ch->ch_flags &= ~(CH_OPENING);
1123 wake_up_interruptible(&ch->ch_flags_wait);
1126 * Initialize if neither terminal or printer is open.
1128 if (!((ch->ch_tun.un_flags | ch->ch_pun.un_flags) & UN_ISOPEN)) {
1130 * Flush input queues.
1139 brd->bd_ops->flush_uart_write(ch);
1140 brd->bd_ops->flush_uart_read(ch);
1143 ch->ch_cached_lsr = 0;
1144 ch->ch_stop_sending_break = 0;
1145 ch->ch_stops_sent = 0;
1147 ch->ch_c_cflag = tty->termios.c_cflag;
1148 ch->ch_c_iflag = tty->termios.c_iflag;
1149 ch->ch_c_oflag = tty->termios.c_oflag;
1150 ch->ch_c_lflag = tty->termios.c_lflag;
1151 ch->ch_startc = tty->termios.c_cc[VSTART];
1152 ch->ch_stopc = tty->termios.c_cc[VSTOP];
1155 * Bring up RTS and DTR...
1156 * Also handle RTS or DTR toggle if set.
1158 if (!(ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE))
1159 ch->ch_mostat |= (UART_MCR_RTS);
1160 if (!(ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE))
1161 ch->ch_mostat |= (UART_MCR_DTR);
1163 /* Tell UART to init itself */
1164 brd->bd_ops->uart_init(ch);
1168 * Run param in case we changed anything
1170 brd->bd_ops->param(tty);
1175 * follow protocol for opening port
1178 spin_unlock_irqrestore(&ch->ch_lock, flags);
1180 rc = dgnc_block_til_ready(tty, file, ch);
1182 /* No going back now, increment our unit and channel counters */
1183 spin_lock_irqsave(&ch->ch_lock, flags);
1184 ch->ch_open_count++;
1185 un->un_open_count++;
1186 un->un_flags |= (UN_ISOPEN);
1187 spin_unlock_irqrestore(&ch->ch_lock, flags);
1193 * dgnc_block_til_ready()
1195 * Wait for DCD, if needed.
1197 static int dgnc_block_til_ready(struct tty_struct *tty,
1199 struct channel_t *ch)
1202 struct un_t *un = NULL;
1203 unsigned long flags;
1205 int sleep_on_un_flags = 0;
1207 if (!tty || tty->magic != TTY_MAGIC || !file || !ch ||
1208 ch->magic != DGNC_CHANNEL_MAGIC)
1211 un = tty->driver_data;
1212 if (!un || un->magic != DGNC_UNIT_MAGIC)
1215 spin_lock_irqsave(&ch->ch_lock, flags);
1221 sleep_on_un_flags = 0;
1224 * If board has failed somehow during our sleep,
1227 if (ch->ch_bd->state == BOARD_FAILED) {
1232 /* If tty was hung up, break out of loop and set error. */
1233 if (tty_hung_up_p(file)) {
1239 * If either unit is in the middle of the fragile part of close,
1240 * we just cannot touch the channel safely.
1241 * Go back to sleep, knowing that when the channel can be
1242 * touched safely, the close routine will signal the
1243 * ch_wait_flags to wake us back up.
1245 if (!((ch->ch_tun.un_flags |
1246 ch->ch_pun.un_flags) &
1249 * Our conditions to leave cleanly and happily:
1250 * 1) NONBLOCKING on the tty is set.
1252 * 3) DCD (fake or real) is active.
1255 if (file->f_flags & O_NONBLOCK)
1258 if (tty->flags & (1 << TTY_IO_ERROR)) {
1263 if (ch->ch_flags & CH_CD)
1266 if (ch->ch_flags & CH_FCAR)
1269 sleep_on_un_flags = 1;
1273 * If there is a signal pending, the user probably
1274 * interrupted (ctrl-c) us.
1275 * Leave loop with error set.
1277 if (signal_pending(current)) {
1278 retval = -ERESTARTSYS;
1283 * Store the flags before we let go of channel lock
1285 if (sleep_on_un_flags)
1286 old_flags = ch->ch_tun.un_flags | ch->ch_pun.un_flags;
1288 old_flags = ch->ch_flags;
1291 * Let go of channel lock before calling schedule.
1292 * Our poller will get any FEP events and wake us up when DCD
1293 * eventually goes active.
1296 spin_unlock_irqrestore(&ch->ch_lock, flags);
1299 * Wait for something in the flags to change
1300 * from the current value.
1302 if (sleep_on_un_flags)
1303 retval = wait_event_interruptible(un->un_flags_wait,
1304 (old_flags != (ch->ch_tun.un_flags |
1305 ch->ch_pun.un_flags)));
1307 retval = wait_event_interruptible(ch->ch_flags_wait,
1308 (old_flags != ch->ch_flags));
1311 * We got woken up for some reason.
1312 * Before looping around, grab our channel lock.
1314 spin_lock_irqsave(&ch->ch_lock, flags);
1319 spin_unlock_irqrestore(&ch->ch_lock, flags);
1327 * Hangup the port. Like a close, but don't wait for output to drain.
1329 static void dgnc_tty_hangup(struct tty_struct *tty)
1333 if (!tty || tty->magic != TTY_MAGIC)
1336 un = tty->driver_data;
1337 if (!un || un->magic != DGNC_UNIT_MAGIC)
1340 /* flush the transmit queues */
1341 dgnc_tty_flush_buffer(tty);
1348 static void dgnc_tty_close(struct tty_struct *tty, struct file *file)
1350 struct dgnc_board *bd;
1351 struct channel_t *ch;
1353 unsigned long flags;
1355 if (!tty || tty->magic != TTY_MAGIC)
1358 un = tty->driver_data;
1359 if (!un || un->magic != DGNC_UNIT_MAGIC)
1363 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1367 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
1370 spin_lock_irqsave(&ch->ch_lock, flags);
1373 * Determine if this is the last close or not - and if we agree about
1374 * which type of close it is with the Line Discipline
1376 if ((tty->count == 1) && (un->un_open_count != 1)) {
1378 * Uh, oh. tty->count is 1, which means that the tty
1379 * structure will be freed. un_open_count should always
1380 * be one in these conditions. If it's greater than
1381 * one, we've got real problems, since it means the
1382 * serial port won't be shutdown.
1385 "tty->count is 1, un open count is %d\n",
1387 un->un_open_count = 1;
1390 if (un->un_open_count)
1391 un->un_open_count--;
1394 "bad serial port open count of %d\n",
1397 ch->ch_open_count--;
1399 if (ch->ch_open_count && un->un_open_count) {
1400 spin_unlock_irqrestore(&ch->ch_lock, flags);
1404 /* OK, its the last close on the unit */
1405 un->un_flags |= UN_CLOSING;
1410 * Only officially close channel if count is 0 and
1411 * DIGI_PRINTER bit is not set.
1413 if ((ch->ch_open_count == 0) &&
1414 !(ch->ch_digi.digi_flags & DIGI_PRINTER)) {
1415 ch->ch_flags &= ~(CH_STOPI | CH_FORCED_STOPI);
1418 * turn off print device when closing print device.
1420 if ((un->un_type == DGNC_PRINT) && (ch->ch_flags & CH_PRON)) {
1421 dgnc_wmove(ch, ch->ch_digi.digi_offstr,
1422 (int)ch->ch_digi.digi_offlen);
1423 ch->ch_flags &= ~CH_PRON;
1426 spin_unlock_irqrestore(&ch->ch_lock, flags);
1427 /* wait for output to drain */
1428 /* This will also return if we take an interrupt */
1430 bd->bd_ops->drain(tty, 0);
1432 dgnc_tty_flush_buffer(tty);
1433 tty_ldisc_flush(tty);
1435 spin_lock_irqsave(&ch->ch_lock, flags);
1440 * If we have HUPCL set, lower DTR and RTS
1442 if (ch->ch_c_cflag & HUPCL) {
1444 ch->ch_mostat &= ~(UART_MCR_DTR | UART_MCR_RTS);
1445 bd->bd_ops->assert_modem_signals(ch);
1448 * Go to sleep to ensure RTS/DTR
1449 * have been dropped for modems to see it.
1451 if (ch->ch_close_delay) {
1452 spin_unlock_irqrestore(&ch->ch_lock,
1454 dgnc_ms_sleep(ch->ch_close_delay);
1455 spin_lock_irqsave(&ch->ch_lock, flags);
1459 ch->ch_old_baud = 0;
1461 /* Turn off UART interrupts for this port */
1462 ch->ch_bd->bd_ops->uart_off(ch);
1465 * turn off print device when closing print device.
1467 if ((un->un_type == DGNC_PRINT) && (ch->ch_flags & CH_PRON)) {
1468 dgnc_wmove(ch, ch->ch_digi.digi_offstr,
1469 (int)ch->ch_digi.digi_offlen);
1470 ch->ch_flags &= ~CH_PRON;
1475 un->un_flags &= ~(UN_ISOPEN | UN_CLOSING);
1477 wake_up_interruptible(&ch->ch_flags_wait);
1478 wake_up_interruptible(&un->un_flags_wait);
1480 spin_unlock_irqrestore(&ch->ch_lock, flags);
1484 * dgnc_tty_chars_in_buffer()
1486 * Return number of characters that have not been transmitted yet.
1488 * This routine is used by the line discipline to determine if there
1489 * is data waiting to be transmitted/drained/flushed or not.
1491 static int dgnc_tty_chars_in_buffer(struct tty_struct *tty)
1493 struct channel_t *ch = NULL;
1494 struct un_t *un = NULL;
1499 unsigned long flags;
1504 un = tty->driver_data;
1505 if (!un || un->magic != DGNC_UNIT_MAGIC)
1509 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1512 spin_lock_irqsave(&ch->ch_lock, flags);
1515 thead = ch->ch_w_head & tmask;
1516 ttail = ch->ch_w_tail & tmask;
1518 spin_unlock_irqrestore(&ch->ch_lock, flags);
1520 if (ttail == thead) {
1524 chars = thead - ttail;
1526 chars = thead - ttail + WQUEUESIZE;
1535 * Reduces bytes_available to the max number of characters
1536 * that can be sent currently given the maxcps value, and
1537 * returns the new bytes_available. This only affects printer
1540 static int dgnc_maxcps_room(struct tty_struct *tty, int bytes_available)
1542 struct channel_t *ch = NULL;
1543 struct un_t *un = NULL;
1546 return bytes_available;
1548 un = tty->driver_data;
1549 if (!un || un->magic != DGNC_UNIT_MAGIC)
1550 return bytes_available;
1553 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1554 return bytes_available;
1557 * If its not the Transparent print device, return
1558 * the full data amount.
1560 if (un->un_type != DGNC_PRINT)
1561 return bytes_available;
1563 if (ch->ch_digi.digi_maxcps > 0 && ch->ch_digi.digi_bufsize > 0) {
1565 unsigned long current_time = jiffies;
1566 unsigned long buffer_time = current_time +
1567 (HZ * ch->ch_digi.digi_bufsize) /
1568 ch->ch_digi.digi_maxcps;
1570 if (ch->ch_cpstime < current_time) {
1571 /* buffer is empty */
1572 ch->ch_cpstime = current_time; /* reset ch_cpstime */
1573 cps_limit = ch->ch_digi.digi_bufsize;
1574 } else if (ch->ch_cpstime < buffer_time) {
1575 /* still room in the buffer */
1576 cps_limit = ((buffer_time - ch->ch_cpstime) *
1577 ch->ch_digi.digi_maxcps) / HZ;
1579 /* no room in the buffer */
1583 bytes_available = min(cps_limit, bytes_available);
1586 return bytes_available;
1590 * dgnc_tty_write_room()
1592 * Return space available in Tx buffer
1594 static int dgnc_tty_write_room(struct tty_struct *tty)
1596 struct channel_t *ch = NULL;
1597 struct un_t *un = NULL;
1602 unsigned long flags;
1604 if (!tty || !dgnc_TmpWriteBuf)
1607 un = tty->driver_data;
1608 if (!un || un->magic != DGNC_UNIT_MAGIC)
1612 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1615 spin_lock_irqsave(&ch->ch_lock, flags);
1618 head = (ch->ch_w_head) & tmask;
1619 tail = (ch->ch_w_tail) & tmask;
1621 ret = tail - head - 1;
1625 /* Limit printer to maxcps */
1626 ret = dgnc_maxcps_room(tty, ret);
1629 * If we are printer device, leave space for
1630 * possibly both the on and off strings.
1632 if (un->un_type == DGNC_PRINT) {
1633 if (!(ch->ch_flags & CH_PRON))
1634 ret -= ch->ch_digi.digi_onlen;
1635 ret -= ch->ch_digi.digi_offlen;
1637 if (ch->ch_flags & CH_PRON)
1638 ret -= ch->ch_digi.digi_offlen;
1644 spin_unlock_irqrestore(&ch->ch_lock, flags);
1650 * dgnc_tty_put_char()
1652 * Put a character into ch->ch_buf
1654 * - used by the line discipline for OPOST processing
1656 static int dgnc_tty_put_char(struct tty_struct *tty, unsigned char c)
1659 * Simply call tty_write.
1661 dgnc_tty_write(tty, &c, 1);
1668 * Take data from the user or kernel and send it out to the FEP.
1669 * In here exists all the Transparent Print magic as well.
1671 static int dgnc_tty_write(struct tty_struct *tty,
1672 const unsigned char *buf, int count)
1674 struct channel_t *ch = NULL;
1675 struct un_t *un = NULL;
1676 int bufcount = 0, n = 0;
1677 unsigned long flags;
1683 if (!tty || !dgnc_TmpWriteBuf)
1686 un = tty->driver_data;
1687 if (!un || un->magic != DGNC_UNIT_MAGIC)
1691 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1698 * Store original amount of characters passed in.
1699 * This helps to figure out if we should ask the FEP
1700 * to send us an event when it has more space available.
1703 spin_lock_irqsave(&ch->ch_lock, flags);
1705 /* Get our space available for the channel from the board */
1707 head = (ch->ch_w_head) & tmask;
1708 tail = (ch->ch_w_tail) & tmask;
1710 bufcount = tail - head - 1;
1712 bufcount += WQUEUESIZE;
1715 * Limit printer output to maxcps overall, with bursts allowed
1716 * up to bufsize characters.
1718 bufcount = dgnc_maxcps_room(tty, bufcount);
1721 * Take minimum of what the user wants to send, and the
1722 * space available in the FEP buffer.
1724 count = min(count, bufcount);
1727 * Bail if no space left.
1733 * Output the printer ON string, if we are in terminal mode, but
1734 * need to be in printer mode.
1736 if ((un->un_type == DGNC_PRINT) && !(ch->ch_flags & CH_PRON)) {
1737 dgnc_wmove(ch, ch->ch_digi.digi_onstr,
1738 (int)ch->ch_digi.digi_onlen);
1739 head = (ch->ch_w_head) & tmask;
1740 ch->ch_flags |= CH_PRON;
1744 * On the other hand, output the printer OFF string, if we are
1745 * currently in printer mode, but need to output to the terminal.
1747 if ((un->un_type != DGNC_PRINT) && (ch->ch_flags & CH_PRON)) {
1748 dgnc_wmove(ch, ch->ch_digi.digi_offstr,
1749 (int)ch->ch_digi.digi_offlen);
1750 head = (ch->ch_w_head) & tmask;
1751 ch->ch_flags &= ~CH_PRON;
1757 * If the write wraps over the top of the circular buffer,
1758 * move the portion up to the wrap point, and reset the
1759 * pointers to the bottom.
1761 remain = WQUEUESIZE - head;
1765 memcpy(ch->ch_wqueue + head, buf, remain);
1772 * Move rest of data.
1775 memcpy(ch->ch_wqueue + head, buf, remain);
1781 ch->ch_w_head = head;
1784 /* Update printer buffer empty time. */
1785 if ((un->un_type == DGNC_PRINT) && (ch->ch_digi.digi_maxcps > 0) &&
1786 (ch->ch_digi.digi_bufsize > 0)) {
1787 ch->ch_cpstime += (HZ * count) / ch->ch_digi.digi_maxcps;
1790 spin_unlock_irqrestore(&ch->ch_lock, flags);
1794 * Channel lock is grabbed and then released
1795 * inside this routine.
1797 ch->ch_bd->bd_ops->copy_data_from_queue_to_uart(ch);
1804 spin_unlock_irqrestore(&ch->ch_lock, flags);
1809 * Return modem signals to ld.
1812 static int dgnc_tty_tiocmget(struct tty_struct *tty)
1814 struct channel_t *ch;
1817 unsigned char mstat = 0;
1818 unsigned long flags;
1820 if (!tty || tty->magic != TTY_MAGIC)
1823 un = tty->driver_data;
1824 if (!un || un->magic != DGNC_UNIT_MAGIC)
1828 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1831 spin_lock_irqsave(&ch->ch_lock, flags);
1833 mstat = ch->ch_mostat | ch->ch_mistat;
1835 spin_unlock_irqrestore(&ch->ch_lock, flags);
1839 if (mstat & UART_MCR_DTR)
1840 result |= TIOCM_DTR;
1841 if (mstat & UART_MCR_RTS)
1842 result |= TIOCM_RTS;
1843 if (mstat & UART_MSR_CTS)
1844 result |= TIOCM_CTS;
1845 if (mstat & UART_MSR_DSR)
1846 result |= TIOCM_DSR;
1847 if (mstat & UART_MSR_RI)
1849 if (mstat & UART_MSR_DCD)
1856 * dgnc_tty_tiocmset()
1858 * Set modem signals, called by ld.
1861 static int dgnc_tty_tiocmset(struct tty_struct *tty,
1862 unsigned int set, unsigned int clear)
1864 struct dgnc_board *bd;
1865 struct channel_t *ch;
1868 unsigned long flags;
1870 if (!tty || tty->magic != TTY_MAGIC)
1873 un = tty->driver_data;
1874 if (!un || un->magic != DGNC_UNIT_MAGIC)
1878 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1882 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
1885 spin_lock_irqsave(&ch->ch_lock, flags);
1887 if (set & TIOCM_RTS)
1888 ch->ch_mostat |= UART_MCR_RTS;
1890 if (set & TIOCM_DTR)
1891 ch->ch_mostat |= UART_MCR_DTR;
1893 if (clear & TIOCM_RTS)
1894 ch->ch_mostat &= ~(UART_MCR_RTS);
1896 if (clear & TIOCM_DTR)
1897 ch->ch_mostat &= ~(UART_MCR_DTR);
1899 ch->ch_bd->bd_ops->assert_modem_signals(ch);
1901 spin_unlock_irqrestore(&ch->ch_lock, flags);
1907 * dgnc_tty_send_break()
1909 * Send a Break, called by ld.
1911 static int dgnc_tty_send_break(struct tty_struct *tty, int msec)
1913 struct dgnc_board *bd;
1914 struct channel_t *ch;
1917 unsigned long flags;
1919 if (!tty || tty->magic != TTY_MAGIC)
1922 un = tty->driver_data;
1923 if (!un || un->magic != DGNC_UNIT_MAGIC)
1927 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1931 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
1945 spin_lock_irqsave(&ch->ch_lock, flags);
1947 ch->ch_bd->bd_ops->send_break(ch, msec);
1949 spin_unlock_irqrestore(&ch->ch_lock, flags);
1955 * dgnc_tty_wait_until_sent()
1957 * wait until data has been transmitted, called by ld.
1959 static void dgnc_tty_wait_until_sent(struct tty_struct *tty, int timeout)
1961 struct dgnc_board *bd;
1962 struct channel_t *ch;
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 bd->bd_ops->drain(tty, 0);
1986 * send a high priority character, called by ld.
1988 static void dgnc_tty_send_xchar(struct tty_struct *tty, char c)
1990 struct dgnc_board *bd;
1991 struct channel_t *ch;
1993 unsigned long flags;
1995 if (!tty || tty->magic != TTY_MAGIC)
1998 un = tty->driver_data;
1999 if (!un || un->magic != DGNC_UNIT_MAGIC)
2003 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2007 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2010 dev_dbg(tty->dev, "dgnc_tty_send_xchar start\n");
2012 spin_lock_irqsave(&ch->ch_lock, flags);
2013 bd->bd_ops->send_immediate_char(ch, c);
2014 spin_unlock_irqrestore(&ch->ch_lock, flags);
2016 dev_dbg(tty->dev, "dgnc_tty_send_xchar finish\n");
2020 * Return modem signals to ld.
2022 static inline int dgnc_get_mstat(struct channel_t *ch)
2024 unsigned char mstat;
2026 unsigned long flags;
2028 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2031 spin_lock_irqsave(&ch->ch_lock, flags);
2033 mstat = ch->ch_mostat | ch->ch_mistat;
2035 spin_unlock_irqrestore(&ch->ch_lock, flags);
2039 if (mstat & UART_MCR_DTR)
2040 result |= TIOCM_DTR;
2041 if (mstat & UART_MCR_RTS)
2042 result |= TIOCM_RTS;
2043 if (mstat & UART_MSR_CTS)
2044 result |= TIOCM_CTS;
2045 if (mstat & UART_MSR_DSR)
2046 result |= TIOCM_DSR;
2047 if (mstat & UART_MSR_RI)
2049 if (mstat & UART_MSR_DCD)
2056 * Return modem signals to ld.
2058 static int dgnc_get_modem_info(struct channel_t *ch,
2059 unsigned int __user *value)
2063 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2066 result = dgnc_get_mstat(ch);
2071 return put_user(result, value);
2075 * dgnc_set_modem_info()
2077 * Set modem signals, called by ld.
2079 static int dgnc_set_modem_info(struct tty_struct *tty,
2080 unsigned int command,
2081 unsigned int __user *value)
2083 struct dgnc_board *bd;
2084 struct channel_t *ch;
2087 unsigned int arg = 0;
2088 unsigned long flags;
2090 if (!tty || tty->magic != TTY_MAGIC)
2093 un = tty->driver_data;
2094 if (!un || un->magic != DGNC_UNIT_MAGIC)
2098 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2102 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2105 ret = get_user(arg, value);
2111 if (arg & TIOCM_RTS)
2112 ch->ch_mostat |= UART_MCR_RTS;
2114 if (arg & TIOCM_DTR)
2115 ch->ch_mostat |= UART_MCR_DTR;
2120 if (arg & TIOCM_RTS)
2121 ch->ch_mostat &= ~(UART_MCR_RTS);
2123 if (arg & TIOCM_DTR)
2124 ch->ch_mostat &= ~(UART_MCR_DTR);
2130 if (arg & TIOCM_RTS)
2131 ch->ch_mostat |= UART_MCR_RTS;
2133 ch->ch_mostat &= ~(UART_MCR_RTS);
2135 if (arg & TIOCM_DTR)
2136 ch->ch_mostat |= UART_MCR_DTR;
2138 ch->ch_mostat &= ~(UART_MCR_DTR);
2146 spin_lock_irqsave(&ch->ch_lock, flags);
2148 ch->ch_bd->bd_ops->assert_modem_signals(ch);
2150 spin_unlock_irqrestore(&ch->ch_lock, flags);
2156 * dgnc_tty_digigeta()
2158 * Ioctl to get the information for ditty.
2163 static int dgnc_tty_digigeta(struct tty_struct *tty,
2164 struct digi_t __user *retinfo)
2166 struct channel_t *ch;
2169 unsigned long flags;
2174 if (!tty || tty->magic != TTY_MAGIC)
2177 un = tty->driver_data;
2178 if (!un || un->magic != DGNC_UNIT_MAGIC)
2182 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2185 memset(&tmp, 0, sizeof(tmp));
2187 spin_lock_irqsave(&ch->ch_lock, flags);
2188 memcpy(&tmp, &ch->ch_digi, sizeof(tmp));
2189 spin_unlock_irqrestore(&ch->ch_lock, flags);
2191 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2198 * dgnc_tty_digiseta()
2200 * Ioctl to set the information for ditty.
2205 static int dgnc_tty_digiseta(struct tty_struct *tty,
2206 struct digi_t __user *new_info)
2208 struct dgnc_board *bd;
2209 struct channel_t *ch;
2211 struct digi_t new_digi;
2212 unsigned long flags;
2214 if (!tty || tty->magic != TTY_MAGIC)
2217 un = tty->driver_data;
2218 if (!un || un->magic != DGNC_UNIT_MAGIC)
2222 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2226 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2229 if (copy_from_user(&new_digi, new_info, sizeof(new_digi)))
2232 spin_lock_irqsave(&ch->ch_lock, flags);
2235 * Handle transistions to and from RTS Toggle.
2237 if (!(ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE) &&
2238 (new_digi.digi_flags & DIGI_RTS_TOGGLE))
2239 ch->ch_mostat &= ~(UART_MCR_RTS);
2240 if ((ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE) &&
2241 !(new_digi.digi_flags & DIGI_RTS_TOGGLE))
2242 ch->ch_mostat |= (UART_MCR_RTS);
2245 * Handle transistions to and from DTR Toggle.
2247 if (!(ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE) &&
2248 (new_digi.digi_flags & DIGI_DTR_TOGGLE))
2249 ch->ch_mostat &= ~(UART_MCR_DTR);
2250 if ((ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE) &&
2251 !(new_digi.digi_flags & DIGI_DTR_TOGGLE))
2252 ch->ch_mostat |= (UART_MCR_DTR);
2254 memcpy(&ch->ch_digi, &new_digi, sizeof(new_digi));
2256 if (ch->ch_digi.digi_maxcps < 1)
2257 ch->ch_digi.digi_maxcps = 1;
2259 if (ch->ch_digi.digi_maxcps > 10000)
2260 ch->ch_digi.digi_maxcps = 10000;
2262 if (ch->ch_digi.digi_bufsize < 10)
2263 ch->ch_digi.digi_bufsize = 10;
2265 if (ch->ch_digi.digi_maxchar < 1)
2266 ch->ch_digi.digi_maxchar = 1;
2268 if (ch->ch_digi.digi_maxchar > ch->ch_digi.digi_bufsize)
2269 ch->ch_digi.digi_maxchar = ch->ch_digi.digi_bufsize;
2271 if (ch->ch_digi.digi_onlen > DIGI_PLEN)
2272 ch->ch_digi.digi_onlen = DIGI_PLEN;
2274 if (ch->ch_digi.digi_offlen > DIGI_PLEN)
2275 ch->ch_digi.digi_offlen = DIGI_PLEN;
2277 ch->ch_bd->bd_ops->param(tty);
2279 spin_unlock_irqrestore(&ch->ch_lock, flags);
2285 * dgnc_set_termios()
2287 static void dgnc_tty_set_termios(struct tty_struct *tty,
2288 struct ktermios *old_termios)
2290 struct dgnc_board *bd;
2291 struct channel_t *ch;
2293 unsigned long flags;
2295 if (!tty || tty->magic != TTY_MAGIC)
2298 un = tty->driver_data;
2299 if (!un || un->magic != DGNC_UNIT_MAGIC)
2303 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2307 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2310 spin_lock_irqsave(&ch->ch_lock, flags);
2312 ch->ch_c_cflag = tty->termios.c_cflag;
2313 ch->ch_c_iflag = tty->termios.c_iflag;
2314 ch->ch_c_oflag = tty->termios.c_oflag;
2315 ch->ch_c_lflag = tty->termios.c_lflag;
2316 ch->ch_startc = tty->termios.c_cc[VSTART];
2317 ch->ch_stopc = tty->termios.c_cc[VSTOP];
2319 ch->ch_bd->bd_ops->param(tty);
2322 spin_unlock_irqrestore(&ch->ch_lock, flags);
2325 static void dgnc_tty_throttle(struct tty_struct *tty)
2327 struct channel_t *ch;
2329 unsigned long flags;
2331 if (!tty || tty->magic != TTY_MAGIC)
2334 un = tty->driver_data;
2335 if (!un || un->magic != DGNC_UNIT_MAGIC)
2339 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2342 spin_lock_irqsave(&ch->ch_lock, flags);
2344 ch->ch_flags |= (CH_FORCED_STOPI);
2346 spin_unlock_irqrestore(&ch->ch_lock, flags);
2349 static void dgnc_tty_unthrottle(struct tty_struct *tty)
2351 struct channel_t *ch;
2353 unsigned long flags;
2355 if (!tty || tty->magic != TTY_MAGIC)
2358 un = tty->driver_data;
2359 if (!un || un->magic != DGNC_UNIT_MAGIC)
2363 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2366 spin_lock_irqsave(&ch->ch_lock, flags);
2368 ch->ch_flags &= ~(CH_FORCED_STOPI);
2370 spin_unlock_irqrestore(&ch->ch_lock, flags);
2373 static void dgnc_tty_start(struct tty_struct *tty)
2375 struct dgnc_board *bd;
2376 struct channel_t *ch;
2378 unsigned long flags;
2380 if (!tty || tty->magic != TTY_MAGIC)
2383 un = tty->driver_data;
2384 if (!un || un->magic != DGNC_UNIT_MAGIC)
2388 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2392 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2395 spin_lock_irqsave(&ch->ch_lock, flags);
2397 ch->ch_flags &= ~(CH_FORCED_STOP);
2399 spin_unlock_irqrestore(&ch->ch_lock, flags);
2402 static void dgnc_tty_stop(struct tty_struct *tty)
2404 struct dgnc_board *bd;
2405 struct channel_t *ch;
2407 unsigned long flags;
2409 if (!tty || tty->magic != TTY_MAGIC)
2412 un = tty->driver_data;
2413 if (!un || un->magic != DGNC_UNIT_MAGIC)
2417 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2421 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2424 spin_lock_irqsave(&ch->ch_lock, flags);
2426 ch->ch_flags |= (CH_FORCED_STOP);
2428 spin_unlock_irqrestore(&ch->ch_lock, flags);
2432 * dgnc_tty_flush_chars()
2434 * Flush the cook buffer
2436 * Note to self, and any other poor souls who venture here:
2438 * flush in this case DOES NOT mean dispose of the data.
2439 * instead, it means "stop buffering and send it if you
2440 * haven't already." Just guess how I figured that out... SRW 2-Jun-98
2442 * It is also always called in interrupt context - JAR 8-Sept-99
2444 static void dgnc_tty_flush_chars(struct tty_struct *tty)
2446 struct dgnc_board *bd;
2447 struct channel_t *ch;
2449 unsigned long flags;
2451 if (!tty || tty->magic != TTY_MAGIC)
2454 un = tty->driver_data;
2455 if (!un || un->magic != DGNC_UNIT_MAGIC)
2459 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2463 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2466 spin_lock_irqsave(&ch->ch_lock, flags);
2468 /* Do something maybe here */
2470 spin_unlock_irqrestore(&ch->ch_lock, flags);
2474 * dgnc_tty_flush_buffer()
2476 * Flush Tx buffer (make in == out)
2478 static void dgnc_tty_flush_buffer(struct tty_struct *tty)
2480 struct channel_t *ch;
2482 unsigned long flags;
2484 if (!tty || tty->magic != TTY_MAGIC)
2487 un = tty->driver_data;
2488 if (!un || un->magic != DGNC_UNIT_MAGIC)
2492 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2495 spin_lock_irqsave(&ch->ch_lock, flags);
2497 ch->ch_flags &= ~CH_STOP;
2499 /* Flush our write queue */
2500 ch->ch_w_head = ch->ch_w_tail;
2502 /* Flush UARTs transmit FIFO */
2503 ch->ch_bd->bd_ops->flush_uart_write(ch);
2505 if (ch->ch_tun.un_flags & (UN_LOW | UN_EMPTY)) {
2506 ch->ch_tun.un_flags &= ~(UN_LOW | UN_EMPTY);
2507 wake_up_interruptible(&ch->ch_tun.un_flags_wait);
2509 if (ch->ch_pun.un_flags & (UN_LOW | UN_EMPTY)) {
2510 ch->ch_pun.un_flags &= ~(UN_LOW | UN_EMPTY);
2511 wake_up_interruptible(&ch->ch_pun.un_flags_wait);
2514 spin_unlock_irqrestore(&ch->ch_lock, flags);
2517 /*****************************************************************************
2519 * The IOCTL function and all of its helpers
2521 *****************************************************************************/
2526 * The usual assortment of ioctl's
2528 static int dgnc_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
2531 struct dgnc_board *bd;
2532 struct channel_t *ch;
2535 unsigned long flags;
2536 void __user *uarg = (void __user *)arg;
2538 if (!tty || tty->magic != TTY_MAGIC)
2541 un = tty->driver_data;
2542 if (!un || un->magic != DGNC_UNIT_MAGIC)
2546 if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2550 if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2553 spin_lock_irqsave(&ch->ch_lock, flags);
2555 if (un->un_open_count <= 0) {
2556 spin_unlock_irqrestore(&ch->ch_lock, flags);
2561 /* Here are all the standard ioctl's that we MUST implement */
2565 * TCSBRK is SVID version: non-zero arg --> no break
2566 * this behaviour is exploited by tcdrain().
2568 * According to POSIX.1 spec (7.2.2.1.2) breaks should be
2569 * between 0.25 and 0.5 seconds so we'll ask for something
2570 * in the middle: 0.375 seconds.
2572 rc = tty_check_change(tty);
2573 spin_unlock_irqrestore(&ch->ch_lock, flags);
2577 rc = ch->ch_bd->bd_ops->drain(tty, 0);
2582 spin_lock_irqsave(&ch->ch_lock, flags);
2584 if (((cmd == TCSBRK) && (!arg)) || (cmd == TCSBRKP))
2585 ch->ch_bd->bd_ops->send_break(ch, 250);
2587 spin_unlock_irqrestore(&ch->ch_lock, flags);
2592 /* support for POSIX tcsendbreak()
2593 * According to POSIX.1 spec (7.2.2.1.2) breaks should be
2594 * between 0.25 and 0.5 seconds so we'll ask for something
2595 * in the middle: 0.375 seconds.
2597 rc = tty_check_change(tty);
2598 spin_unlock_irqrestore(&ch->ch_lock, flags);
2602 rc = ch->ch_bd->bd_ops->drain(tty, 0);
2606 spin_lock_irqsave(&ch->ch_lock, flags);
2608 ch->ch_bd->bd_ops->send_break(ch, 250);
2610 spin_unlock_irqrestore(&ch->ch_lock, flags);
2615 rc = tty_check_change(tty);
2616 spin_unlock_irqrestore(&ch->ch_lock, flags);
2620 rc = ch->ch_bd->bd_ops->drain(tty, 0);
2624 spin_lock_irqsave(&ch->ch_lock, flags);
2626 ch->ch_bd->bd_ops->send_break(ch, 250);
2628 spin_unlock_irqrestore(&ch->ch_lock, flags);
2634 spin_unlock_irqrestore(&ch->ch_lock, flags);
2639 spin_unlock_irqrestore(&ch->ch_lock, flags);
2641 rc = put_user(C_CLOCAL(tty) ? 1 : 0,
2642 (unsigned long __user *)arg);
2647 spin_unlock_irqrestore(&ch->ch_lock, flags);
2648 rc = get_user(arg, (unsigned long __user *)arg);
2652 spin_lock_irqsave(&ch->ch_lock, flags);
2653 tty->termios.c_cflag = ((tty->termios.c_cflag & ~CLOCAL) |
2654 (arg ? CLOCAL : 0));
2655 ch->ch_bd->bd_ops->param(tty);
2656 spin_unlock_irqrestore(&ch->ch_lock, flags);
2661 spin_unlock_irqrestore(&ch->ch_lock, flags);
2662 return dgnc_get_modem_info(ch, uarg);
2667 spin_unlock_irqrestore(&ch->ch_lock, flags);
2668 return dgnc_set_modem_info(tty, cmd, uarg);
2671 * Here are any additional ioctl's that we want to implement
2676 * The linux tty driver doesn't have a flush
2677 * input routine for the driver, assuming all backed
2678 * up data is in the line disc. buffers. However,
2679 * we all know that's not the case. Here, we
2680 * act on the ioctl, but then lie and say we didn't
2681 * so the line discipline will process the flush
2684 rc = tty_check_change(tty);
2686 spin_unlock_irqrestore(&ch->ch_lock, flags);
2690 if ((arg == TCIFLUSH) || (arg == TCIOFLUSH)) {
2691 ch->ch_r_head = ch->ch_r_tail;
2692 ch->ch_bd->bd_ops->flush_uart_read(ch);
2693 /* Force queue flow control to be released, if needed */
2694 dgnc_check_queue_flow_control(ch);
2697 if ((arg == TCOFLUSH) || (arg == TCIOFLUSH)) {
2698 if (!(un->un_type == DGNC_PRINT)) {
2699 ch->ch_w_head = ch->ch_w_tail;
2700 ch->ch_bd->bd_ops->flush_uart_write(ch);
2702 if (ch->ch_tun.un_flags & (UN_LOW|UN_EMPTY)) {
2703 ch->ch_tun.un_flags &=
2704 ~(UN_LOW | UN_EMPTY);
2705 wake_up_interruptible(&ch->ch_tun.un_flags_wait);
2708 if (ch->ch_pun.un_flags & (UN_LOW|UN_EMPTY)) {
2709 ch->ch_pun.un_flags &=
2710 ~(UN_LOW | UN_EMPTY);
2711 wake_up_interruptible(&ch->ch_pun.un_flags_wait);
2716 /* pretend we didn't recognize this IOCTL */
2717 spin_unlock_irqrestore(&ch->ch_lock, flags);
2718 return -ENOIOCTLCMD;
2722 * The linux tty driver doesn't have a flush
2723 * input routine for the driver, assuming all backed
2724 * up data is in the line disc. buffers. However,
2725 * we all know that's not the case. Here, we
2726 * act on the ioctl, but then lie and say we didn't
2727 * so the line discipline will process the flush
2730 if (cmd == TCSETSF) {
2732 ch->ch_flags &= ~CH_STOP;
2733 ch->ch_r_head = ch->ch_r_tail;
2734 ch->ch_bd->bd_ops->flush_uart_read(ch);
2735 /* Force queue flow control to be released, if needed */
2736 dgnc_check_queue_flow_control(ch);
2739 /* now wait for all the output to drain */
2740 spin_unlock_irqrestore(&ch->ch_lock, flags);
2741 rc = ch->ch_bd->bd_ops->drain(tty, 0);
2745 /* pretend we didn't recognize this */
2746 return -ENOIOCTLCMD;
2750 spin_unlock_irqrestore(&ch->ch_lock, flags);
2751 rc = ch->ch_bd->bd_ops->drain(tty, 0);
2755 /* pretend we didn't recognize this */
2756 return -ENOIOCTLCMD;
2759 spin_unlock_irqrestore(&ch->ch_lock, flags);
2760 /* Make the ld do it */
2761 return -ENOIOCTLCMD;
2764 /* get information for ditty */
2765 spin_unlock_irqrestore(&ch->ch_lock, flags);
2766 return dgnc_tty_digigeta(tty, uarg);
2771 /* set information for ditty */
2772 if (cmd == (DIGI_SETAW)) {
2773 spin_unlock_irqrestore(&ch->ch_lock, flags);
2774 rc = ch->ch_bd->bd_ops->drain(tty, 0);
2779 spin_lock_irqsave(&ch->ch_lock, flags);
2781 tty_ldisc_flush(tty);
2786 spin_unlock_irqrestore(&ch->ch_lock, flags);
2787 return dgnc_tty_digiseta(tty, uarg);
2792 /* Let go of locks when accessing user space,
2795 spin_unlock_irqrestore(&ch->ch_lock, flags);
2796 rc = get_user(loopback, (unsigned int __user *)arg);
2799 spin_lock_irqsave(&ch->ch_lock, flags);
2801 /* Enable/disable internal loopback for this port */
2803 ch->ch_flags |= CH_LOOPBACK;
2805 ch->ch_flags &= ~(CH_LOOPBACK);
2807 ch->ch_bd->bd_ops->param(tty);
2808 spin_unlock_irqrestore(&ch->ch_lock, flags);
2812 case DIGI_GETCUSTOMBAUD:
2813 spin_unlock_irqrestore(&ch->ch_lock, flags);
2814 rc = put_user(ch->ch_custom_speed, (unsigned int __user *)arg);
2817 case DIGI_SETCUSTOMBAUD:
2820 /* Let go of locks when accessing user space, could sleep */
2821 spin_unlock_irqrestore(&ch->ch_lock, flags);
2822 rc = get_user(new_rate, (int __user *)arg);
2825 spin_lock_irqsave(&ch->ch_lock, flags);
2826 dgnc_set_custom_speed(ch, new_rate);
2827 ch->ch_bd->bd_ops->param(tty);
2828 spin_unlock_irqrestore(&ch->ch_lock, flags);
2833 * This ioctl allows insertion of a character into the front
2834 * of any pending data to be transmitted.
2836 * This ioctl is to satify the "Send Character Immediate"
2837 * call that the RealPort protocol spec requires.
2839 case DIGI_REALPORT_SENDIMMEDIATE:
2843 spin_unlock_irqrestore(&ch->ch_lock, flags);
2844 rc = get_user(c, (unsigned char __user *)arg);
2847 spin_lock_irqsave(&ch->ch_lock, flags);
2848 ch->ch_bd->bd_ops->send_immediate_char(ch, c);
2849 spin_unlock_irqrestore(&ch->ch_lock, flags);
2854 * This ioctl returns all the current counts for the port.
2856 * This ioctl is to satify the "Line Error Counters"
2857 * call that the RealPort protocol spec requires.
2859 case DIGI_REALPORT_GETCOUNTERS:
2861 struct digi_getcounter buf;
2863 buf.norun = ch->ch_err_overrun;
2864 buf.noflow = 0; /* The driver doesn't keep this stat */
2865 buf.nframe = ch->ch_err_frame;
2866 buf.nparity = ch->ch_err_parity;
2867 buf.nbreak = ch->ch_err_break;
2868 buf.rbytes = ch->ch_rxcount;
2869 buf.tbytes = ch->ch_txcount;
2871 spin_unlock_irqrestore(&ch->ch_lock, flags);
2873 if (copy_to_user(uarg, &buf, sizeof(buf)))
2880 * This ioctl returns all current events.
2882 * This ioctl is to satify the "Event Reporting"
2883 * call that the RealPort protocol spec requires.
2885 case DIGI_REALPORT_GETEVENTS:
2887 unsigned int events = 0;
2889 /* NOTE: MORE EVENTS NEEDS TO BE ADDED HERE */
2890 if (ch->ch_flags & CH_BREAK_SENDING)
2892 if ((ch->ch_flags & CH_STOP) ||
2893 (ch->ch_flags & CH_FORCED_STOP))
2894 events |= (EV_OPU | EV_OPS);
2896 if ((ch->ch_flags & CH_STOPI) ||
2897 (ch->ch_flags & CH_FORCED_STOPI))
2898 events |= (EV_IPU | EV_IPS);
2900 spin_unlock_irqrestore(&ch->ch_lock, flags);
2901 rc = put_user(events, (unsigned int __user *)arg);
2906 * This ioctl returns TOUT and TIN counters based
2907 * upon the values passed in by the RealPort Server.
2908 * It also passes back whether the UART Transmitter is
2911 case DIGI_REALPORT_GETBUFFERS:
2913 struct digi_getbuffer buf;
2917 spin_unlock_irqrestore(&ch->ch_lock, flags);
2920 * Get data from user first.
2922 if (copy_from_user(&buf, uarg, sizeof(buf)))
2925 spin_lock_irqsave(&ch->ch_lock, flags);
2928 * Figure out how much data is in our RX and TX queues.
2930 buf.rxbuf = (ch->ch_r_head - ch->ch_r_tail) & RQUEUEMASK;
2931 buf.txbuf = (ch->ch_w_head - ch->ch_w_tail) & WQUEUEMASK;
2934 * Is the UART empty? Add that value to whats in our TX queue.
2936 count = buf.txbuf + ch->ch_bd->bd_ops->get_uart_bytes_left(ch);
2939 * Figure out how much data the RealPort Server believes should
2940 * be in our TX queue.
2942 tdist = (buf.tIn - buf.tOut) & 0xffff;
2945 * If we have more data than the RealPort Server believes we
2946 * should have, reduce our count to its amount.
2948 * This count difference CAN happen because the Linux LD can
2949 * insert more characters into our queue for OPOST processing
2950 * that the RealPort Server doesn't know about.
2952 if (buf.txbuf > tdist)
2956 * Report whether our queue and UART TX are completely empty.
2963 spin_unlock_irqrestore(&ch->ch_lock, flags);
2965 if (copy_to_user(uarg, &buf, sizeof(buf)))
2971 spin_unlock_irqrestore(&ch->ch_lock, flags);
2973 return -ENOIOCTLCMD;