]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/dgnc/dgnc_tty.c
Merge branch 'kconfig' of git://git.kernel.org/pub/scm/linux/kernel/git/mmarek/kbuild
[karo-tx-linux.git] / drivers / staging / dgnc / dgnc_tty.c
1 /*
2  * Copyright 2003 Digi International (www.digi.com)
3  *      Scott H Kilau <Scott_Kilau at digi dot com>
4  *
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)
8  * any later version.
9  *
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.
14  */
15
16 /*
17  * This file implements the tty driver functionality for the
18  * Neo and ClassicBoard PCI based product lines.
19  */
20
21 #include <linux/kernel.h>
22 #include <linux/sched.h>        /* For jiffies, task states */
23 #include <linux/interrupt.h>    /* For tasklet and interrupt structs/defines */
24 #include <linux/module.h>
25 #include <linux/ctype.h>
26 #include <linux/tty.h>
27 #include <linux/tty_flip.h>
28 #include <linux/types.h>
29 #include <linux/serial_reg.h>
30 #include <linux/slab.h>
31 #include <linux/delay.h>        /* For udelay */
32 #include <linux/uaccess.h>      /* For copy_from_user/copy_to_user */
33 #include <linux/pci.h>
34 #include "dgnc_driver.h"
35 #include "dgnc_tty.h"
36 #include "dgnc_neo.h"
37 #include "dgnc_cls.h"
38 #include "dgnc_utils.h"
39
40 /* Default transparent print information. */
41
42 static const struct digi_t dgnc_digi_init = {
43         .digi_flags =   DIGI_COOK,      /* Flags */
44         .digi_maxcps =  100,            /* Max CPS */
45         .digi_maxchar = 50,             /* Max chars in print queue */
46         .digi_bufsize = 100,            /* Printer buffer size */
47         .digi_onlen =   4,              /* size of printer on string */
48         .digi_offlen =  4,              /* size of printer off string */
49         .digi_onstr =   "\033[5i",      /* ANSI printer on string ] */
50         .digi_offstr =  "\033[4i",      /* ANSI printer off string ] */
51         .digi_term =    "ansi"          /* default terminal type */
52 };
53
54 /*
55  * Define a local default termios struct. All ports will be created
56  * with this termios initially.
57  *
58  * This defines a raw port at 9600 baud, 8 data bits, no parity,
59  * 1 stop bit.
60  */
61 static struct ktermios default_termios = {
62         .c_iflag =      (DEFAULT_IFLAGS),       /* iflags */
63         .c_oflag =      (DEFAULT_OFLAGS),       /* oflags */
64         .c_cflag =      (DEFAULT_CFLAGS),       /* cflags */
65         .c_lflag =      (DEFAULT_LFLAGS),       /* lflags */
66         .c_cc =         INIT_C_CC,
67         .c_line =       0,
68 };
69
70 /* Our function prototypes */
71 static int dgnc_tty_open(struct tty_struct *tty, struct file *file);
72 static void dgnc_tty_close(struct tty_struct *tty, struct file *file);
73 static int dgnc_block_til_ready(struct tty_struct *tty, struct file *file,
74                                 struct channel_t *ch);
75 static int dgnc_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
76                           unsigned long arg);
77 static int dgnc_tty_digigeta(struct tty_struct *tty,
78                              struct digi_t __user *retinfo);
79 static int dgnc_tty_digiseta(struct tty_struct *tty,
80                              struct digi_t __user *new_info);
81 static int dgnc_tty_write_room(struct tty_struct *tty);
82 static int dgnc_tty_put_char(struct tty_struct *tty, unsigned char c);
83 static int dgnc_tty_chars_in_buffer(struct tty_struct *tty);
84 static void dgnc_tty_start(struct tty_struct *tty);
85 static void dgnc_tty_stop(struct tty_struct *tty);
86 static void dgnc_tty_throttle(struct tty_struct *tty);
87 static void dgnc_tty_unthrottle(struct tty_struct *tty);
88 static void dgnc_tty_flush_chars(struct tty_struct *tty);
89 static void dgnc_tty_flush_buffer(struct tty_struct *tty);
90 static void dgnc_tty_hangup(struct tty_struct *tty);
91 static int dgnc_set_modem_info(struct channel_t *ch, unsigned int command,
92                                unsigned int __user *value);
93 static int dgnc_get_modem_info(struct channel_t *ch,
94                                unsigned int __user *value);
95 static int dgnc_tty_tiocmget(struct tty_struct *tty);
96 static int dgnc_tty_tiocmset(struct tty_struct *tty, unsigned int set,
97                              unsigned int clear);
98 static int dgnc_tty_send_break(struct tty_struct *tty, int msec);
99 static void dgnc_tty_wait_until_sent(struct tty_struct *tty, int timeout);
100 static int dgnc_tty_write(struct tty_struct *tty, const unsigned char *buf,
101                           int count);
102 static void dgnc_tty_set_termios(struct tty_struct *tty,
103                                  struct ktermios *old_termios);
104 static void dgnc_tty_send_xchar(struct tty_struct *tty, char ch);
105 static void dgnc_set_signal_low(struct channel_t *ch, const unsigned char line);
106 static void dgnc_wake_up_unit(struct un_t *unit);
107
108 static const struct tty_operations dgnc_tty_ops = {
109         .open = dgnc_tty_open,
110         .close = dgnc_tty_close,
111         .write = dgnc_tty_write,
112         .write_room = dgnc_tty_write_room,
113         .flush_buffer = dgnc_tty_flush_buffer,
114         .chars_in_buffer = dgnc_tty_chars_in_buffer,
115         .flush_chars = dgnc_tty_flush_chars,
116         .ioctl = dgnc_tty_ioctl,
117         .set_termios = dgnc_tty_set_termios,
118         .stop = dgnc_tty_stop,
119         .start = dgnc_tty_start,
120         .throttle = dgnc_tty_throttle,
121         .unthrottle = dgnc_tty_unthrottle,
122         .hangup = dgnc_tty_hangup,
123         .put_char = dgnc_tty_put_char,
124         .tiocmget = dgnc_tty_tiocmget,
125         .tiocmset = dgnc_tty_tiocmset,
126         .break_ctl = dgnc_tty_send_break,
127         .wait_until_sent = dgnc_tty_wait_until_sent,
128         .send_xchar = dgnc_tty_send_xchar
129 };
130
131 /* TTY Initialization/Cleanup Functions */
132
133 /*
134  * dgnc_tty_register()
135  *
136  * Init the tty subsystem for this board.
137  */
138 int dgnc_tty_register(struct dgnc_board *brd)
139 {
140         int rc;
141
142         brd->serial_driver = tty_alloc_driver(brd->maxports,
143                                               TTY_DRIVER_REAL_RAW |
144                                               TTY_DRIVER_DYNAMIC_DEV |
145                                               TTY_DRIVER_HARDWARE_BREAK);
146
147         if (IS_ERR(brd->serial_driver))
148                 return PTR_ERR(brd->serial_driver);
149
150         snprintf(brd->serial_name, MAXTTYNAMELEN, "tty_dgnc_%d_",
151                  brd->boardnum);
152
153         brd->serial_driver->name = brd->serial_name;
154         brd->serial_driver->name_base = 0;
155         brd->serial_driver->major = 0;
156         brd->serial_driver->minor_start = 0;
157         brd->serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
158         brd->serial_driver->subtype = SERIAL_TYPE_NORMAL;
159         brd->serial_driver->init_termios = default_termios;
160         brd->serial_driver->driver_name = DRVSTR;
161
162         /*
163          * Entry points for driver.  Called by the kernel from
164          * tty_io.c and n_tty.c.
165          */
166         tty_set_operations(brd->serial_driver, &dgnc_tty_ops);
167
168         rc = tty_register_driver(brd->serial_driver);
169         if (rc < 0) {
170                 dev_dbg(&brd->pdev->dev,
171                         "Can't register tty device (%d)\n", rc);
172                 goto free_serial_driver;
173         }
174
175         /*
176          * If we're doing transparent print, we have to do all of the above
177          * again, separately so we don't get the LD confused about what major
178          * we are when we get into the dgnc_tty_open() routine.
179          */
180         brd->print_driver = tty_alloc_driver(brd->maxports,
181                                              TTY_DRIVER_REAL_RAW |
182                                              TTY_DRIVER_DYNAMIC_DEV |
183                                              TTY_DRIVER_HARDWARE_BREAK);
184
185         if (IS_ERR(brd->print_driver)) {
186                 rc = PTR_ERR(brd->print_driver);
187                 goto unregister_serial_driver;
188         }
189
190         snprintf(brd->print_name, MAXTTYNAMELEN, "pr_dgnc_%d_", brd->boardnum);
191
192         brd->print_driver->name = brd->print_name;
193         brd->print_driver->name_base = 0;
194         brd->print_driver->major = brd->serial_driver->major;
195         brd->print_driver->minor_start = 0x80;
196         brd->print_driver->type = TTY_DRIVER_TYPE_SERIAL;
197         brd->print_driver->subtype = SERIAL_TYPE_NORMAL;
198         brd->print_driver->init_termios = default_termios;
199         brd->print_driver->driver_name = DRVSTR;
200
201         /*
202          * Entry points for driver.  Called by the kernel from
203          * tty_io.c and n_tty.c.
204          */
205         tty_set_operations(brd->print_driver, &dgnc_tty_ops);
206
207         rc = tty_register_driver(brd->print_driver);
208         if (rc < 0) {
209                 dev_dbg(&brd->pdev->dev,
210                         "Can't register Transparent Print device(%d)\n",
211                         rc);
212                 goto free_print_driver;
213         }
214
215         return 0;
216
217 free_print_driver:
218         put_tty_driver(brd->print_driver);
219 unregister_serial_driver:
220         tty_unregister_driver(brd->serial_driver);
221 free_serial_driver:
222         put_tty_driver(brd->serial_driver);
223
224         return rc;
225 }
226
227 void dgnc_tty_unregister(struct dgnc_board *brd)
228 {
229         tty_unregister_driver(brd->print_driver);
230         tty_unregister_driver(brd->serial_driver);
231         put_tty_driver(brd->print_driver);
232         put_tty_driver(brd->serial_driver);
233 }
234
235 /*
236  * dgnc_tty_init()
237  *
238  * Init the tty subsystem.  Called once per board after board has been
239  * downloaded and init'ed.
240  */
241 int dgnc_tty_init(struct dgnc_board *brd)
242 {
243         int i;
244         void __iomem *vaddr;
245         struct channel_t *ch;
246
247         if (!brd)
248                 return -ENXIO;
249
250         /* Initialize board structure elements. */
251
252         vaddr = brd->re_map_membase;
253
254         brd->nasync = brd->maxports;
255
256         for (i = 0; i < brd->nasync; i++) {
257                 /*
258                  * Okay to malloc with GFP_KERNEL, we are not at
259                  * interrupt context, and there are no locks held.
260                  */
261                 brd->channels[i] = kzalloc(sizeof(*brd->channels[i]),
262                                            GFP_KERNEL);
263                 if (!brd->channels[i])
264                         goto err_free_channels;
265         }
266
267         ch = brd->channels[0];
268         vaddr = brd->re_map_membase;
269
270         /* Set up channel variables */
271         for (i = 0; i < brd->nasync; i++, ch = brd->channels[i]) {
272                 spin_lock_init(&ch->ch_lock);
273
274                 /* Store all our magic numbers */
275                 ch->magic = DGNC_CHANNEL_MAGIC;
276                 ch->ch_tun.magic = DGNC_UNIT_MAGIC;
277                 ch->ch_tun.un_ch = ch;
278                 ch->ch_tun.un_type = DGNC_SERIAL;
279                 ch->ch_tun.un_dev = i;
280
281                 ch->ch_pun.magic = DGNC_UNIT_MAGIC;
282                 ch->ch_pun.un_ch = ch;
283                 ch->ch_pun.un_type = DGNC_PRINT;
284                 ch->ch_pun.un_dev = i + 128;
285
286                 if (brd->bd_uart_offset == 0x200)
287                         ch->ch_neo_uart = vaddr + (brd->bd_uart_offset * i);
288                 else
289                         ch->ch_cls_uart = vaddr + (brd->bd_uart_offset * i);
290
291                 ch->ch_bd = brd;
292                 ch->ch_portnum = i;
293                 ch->ch_digi = dgnc_digi_init;
294
295                 /* .25 second delay */
296                 ch->ch_close_delay = 250;
297
298                 init_waitqueue_head(&ch->ch_flags_wait);
299                 init_waitqueue_head(&ch->ch_tun.un_flags_wait);
300                 init_waitqueue_head(&ch->ch_pun.un_flags_wait);
301
302                 {
303                         struct device *classp;
304
305                         classp = tty_register_device(brd->serial_driver, i,
306                                                      &ch->ch_bd->pdev->dev);
307                         ch->ch_tun.un_sysfs = classp;
308
309                         classp = tty_register_device(brd->print_driver, i,
310                                                      &ch->ch_bd->pdev->dev);
311                         ch->ch_pun.un_sysfs = classp;
312                 }
313         }
314
315         return 0;
316
317 err_free_channels:
318         for (i = i - 1; i >= 0; --i) {
319                 kfree(brd->channels[i]);
320                 brd->channels[i] = NULL;
321         }
322         return -ENOMEM;
323 }
324
325 /*
326  * dgnc_cleanup_tty()
327  *
328  * Uninitialize the TTY portion of this driver.  Free all memory and
329  * resources.
330  */
331 void dgnc_cleanup_tty(struct dgnc_board *brd)
332 {
333         int i = 0;
334
335         for (i = 0; i < brd->nasync; i++)
336                 tty_unregister_device(brd->serial_driver, i);
337
338         tty_unregister_driver(brd->serial_driver);
339
340         for (i = 0; i < brd->nasync; i++)
341                 tty_unregister_device(brd->print_driver, i);
342
343         tty_unregister_driver(brd->print_driver);
344
345         put_tty_driver(brd->serial_driver);
346         put_tty_driver(brd->print_driver);
347 }
348
349 /*
350  *      dgnc_wmove - Write data to transmit queue.
351  *
352  *              ch      - Pointer to channel structure.
353  *              buf     - Pointer to characters to be moved.
354  *              n       - Number of characters to move.
355  */
356 static void dgnc_wmove(struct channel_t *ch, char *buf, uint n)
357 {
358         int     remain;
359         uint    head;
360
361         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
362                 return;
363
364         head = ch->ch_w_head & WQUEUEMASK;
365
366         /*
367          * If the write wraps over the top of the circular buffer,
368          * move the portion up to the wrap point, and reset the
369          * pointers to the bottom.
370          */
371         remain = WQUEUESIZE - head;
372
373         if (n >= remain) {
374                 n -= remain;
375                 memcpy(ch->ch_wqueue + head, buf, remain);
376                 head = 0;
377                 buf += remain;
378         }
379
380         if (n > 0) {
381                 /* Move rest of data. */
382                 remain = n;
383                 memcpy(ch->ch_wqueue + head, buf, remain);
384                 head += remain;
385         }
386
387         head &= WQUEUEMASK;
388         ch->ch_w_head = head;
389 }
390
391 /*
392  *      dgnc_input - Process received data.
393  *
394  *            ch      - Pointer to channel structure.
395  */
396 void dgnc_input(struct channel_t *ch)
397 {
398         struct dgnc_board *bd;
399         struct tty_struct *tp;
400         struct tty_ldisc *ld = NULL;
401         uint    rmask;
402         ushort  head;
403         ushort  tail;
404         int     data_len;
405         unsigned long flags;
406         int flip_len;
407         int len = 0;
408         int n = 0;
409         int s = 0;
410         int i = 0;
411
412         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
413                 return;
414
415         tp = ch->ch_tun.un_tty;
416
417         bd = ch->ch_bd;
418         if (!bd || bd->magic != DGNC_BOARD_MAGIC)
419                 return;
420
421         spin_lock_irqsave(&ch->ch_lock, flags);
422
423         /*
424          *      Figure the number of characters in the buffer.
425          *      Exit immediately if none.
426          */
427         rmask = RQUEUEMASK;
428         head = ch->ch_r_head & rmask;
429         tail = ch->ch_r_tail & rmask;
430         data_len = (head - tail) & rmask;
431
432         if (data_len == 0)
433                 goto exit_unlock;
434
435         /*
436          * If the device is not open, or CREAD is off,
437          * flush input data and return immediately.
438          */
439         if (!tp || (tp->magic != TTY_MAGIC) ||
440             !(ch->ch_tun.un_flags & UN_ISOPEN) ||
441             !C_CREAD(tp) ||
442             (ch->ch_tun.un_flags & UN_CLOSING)) {
443                 ch->ch_r_head = tail;
444
445                 /* Force queue flow control to be released, if needed */
446                 dgnc_check_queue_flow_control(ch);
447
448                 goto exit_unlock;
449         }
450
451         /* If we are throttled, simply don't read any data. */
452
453         if (ch->ch_flags & CH_FORCED_STOPI)
454                 goto exit_unlock;
455
456         flip_len = TTY_FLIPBUF_SIZE;
457
458         /* Chop down the length, if needed */
459         len = min(data_len, flip_len);
460         len = min(len, (N_TTY_BUF_SIZE - 1));
461
462         ld = tty_ldisc_ref(tp);
463
464         /*
465          * If we were unable to get a reference to the ld,
466          * don't flush our buffer, and act like the ld doesn't
467          * have any space to put the data right now.
468          */
469         if (!ld) {
470                 len = 0;
471         } else {
472                 /*
473                  * If ld doesn't have a pointer to a receive_buf function,
474                  * flush the data, then act like the ld doesn't have any
475                  * space to put the data right now.
476                  */
477                 if (!ld->ops->receive_buf) {
478                         ch->ch_r_head = ch->ch_r_tail;
479                         len = 0;
480                 }
481         }
482
483         if (len <= 0)
484                 goto exit_unlock;
485
486         /*
487          * The tty layer in the kernel has changed in 2.6.16+.
488          *
489          * The flip buffers in the tty structure are no longer exposed,
490          * and probably will be going away eventually.
491          *
492          * If we are completely raw, we don't need to go through a lot
493          * of the tty layers that exist.
494          * In this case, we take the shortest and fastest route we
495          * can to relay the data to the user.
496          *
497          * On the other hand, if we are not raw, we need to go through
498          * the new 2.6.16+ tty layer, which has its API more well defined.
499          */
500         len = tty_buffer_request_room(tp->port, len);
501         n = len;
502
503         /*
504          * n now contains the most amount of data we can copy,
505          * bounded either by how much the Linux tty layer can handle,
506          * or the amount of data the card actually has pending...
507          */
508         while (n) {
509                 unsigned char *ch_pos = ch->ch_equeue + tail;
510
511                 s = ((head >= tail) ? head : RQUEUESIZE) - tail;
512                 s = min(s, n);
513
514                 if (s <= 0)
515                         break;
516
517                 /*
518                  * If conditions are such that ld needs to see all
519                  * UART errors, we will have to walk each character
520                  * and error byte and send them to the buffer one at
521                  * a time.
522                  */
523                 if (I_PARMRK(tp) || I_BRKINT(tp) || I_INPCK(tp)) {
524                         for (i = 0; i < s; i++) {
525                                 unsigned char ch = *(ch_pos + i);
526                                 char flag = TTY_NORMAL;
527
528                                 if (ch & UART_LSR_BI)
529                                         flag = TTY_BREAK;
530                                 else if (ch & UART_LSR_PE)
531                                         flag = TTY_PARITY;
532                                 else if (ch & UART_LSR_FE)
533                                         flag = TTY_FRAME;
534
535                                 tty_insert_flip_char(tp->port, ch, flag);
536                         }
537                 } else {
538                         tty_insert_flip_string(tp->port, ch_pos, s);
539                 }
540
541                 tail += s;
542                 n -= s;
543                 /* Flip queue if needed */
544                 tail &= rmask;
545         }
546
547         ch->ch_r_tail = tail & rmask;
548         ch->ch_e_tail = tail & rmask;
549         dgnc_check_queue_flow_control(ch);
550         spin_unlock_irqrestore(&ch->ch_lock, flags);
551
552         /* Tell the tty layer its okay to "eat" the data now */
553         tty_flip_buffer_push(tp->port);
554
555         if (ld)
556                 tty_ldisc_deref(ld);
557         return;
558
559 exit_unlock:
560         spin_unlock_irqrestore(&ch->ch_lock, flags);
561         if (ld)
562                 tty_ldisc_deref(ld);
563 }
564
565 /*
566  * Determines when CARRIER changes state and takes appropriate
567  * action.
568  */
569 void dgnc_carrier(struct channel_t *ch)
570 {
571         int virt_carrier = 0;
572         int phys_carrier = 0;
573
574         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
575                 return;
576
577         if (ch->ch_mistat & UART_MSR_DCD)
578                 phys_carrier = 1;
579
580         if (ch->ch_digi.digi_flags & DIGI_FORCEDCD)
581                 virt_carrier = 1;
582
583         if (ch->ch_c_cflag & CLOCAL)
584                 virt_carrier = 1;
585
586         /* Test for a VIRTUAL carrier transition to HIGH. */
587
588         if (((ch->ch_flags & CH_FCAR) == 0) && (virt_carrier == 1)) {
589                 /*
590                  * When carrier rises, wake any threads waiting
591                  * for carrier in the open routine.
592                  */
593                 if (waitqueue_active(&ch->ch_flags_wait))
594                         wake_up_interruptible(&ch->ch_flags_wait);
595         }
596
597         /* Test for a PHYSICAL carrier transition to HIGH. */
598
599         if (((ch->ch_flags & CH_CD) == 0) && (phys_carrier == 1)) {
600                 /*
601                  * When carrier rises, wake any threads waiting
602                  * for carrier in the open routine.
603                  */
604                 if (waitqueue_active(&ch->ch_flags_wait))
605                         wake_up_interruptible(&ch->ch_flags_wait);
606         }
607
608         /*
609          *  Test for a PHYSICAL transition to low, so long as we aren't
610          *  currently ignoring physical transitions (which is what "virtual
611          *  carrier" indicates).
612          *
613          *  The transition of the virtual carrier to low really doesn't
614          *  matter... it really only means "ignore carrier state", not
615          *  "make pretend that carrier is there".
616          */
617         if ((virt_carrier == 0) && ((ch->ch_flags & CH_CD) != 0) &&
618             (phys_carrier == 0)) {
619                 /*
620                  *   When carrier drops:
621                  *
622                  *   Drop carrier on all open units.
623                  *
624                  *   Flush queues, waking up any task waiting in the
625                  *   line discipline.
626                  *
627                  *   Send a hangup to the control terminal.
628                  *
629                  *   Enable all select calls.
630                  */
631                 if (waitqueue_active(&ch->ch_flags_wait))
632                         wake_up_interruptible(&ch->ch_flags_wait);
633
634                 if (ch->ch_tun.un_open_count > 0)
635                         tty_hangup(ch->ch_tun.un_tty);
636
637                 if (ch->ch_pun.un_open_count > 0)
638                         tty_hangup(ch->ch_pun.un_tty);
639         }
640
641         /*  Make sure that our cached values reflect the current reality. */
642
643         if (virt_carrier == 1)
644                 ch->ch_flags |= CH_FCAR;
645         else
646                 ch->ch_flags &= ~CH_FCAR;
647
648         if (phys_carrier == 1)
649                 ch->ch_flags |= CH_CD;
650         else
651                 ch->ch_flags &= ~CH_CD;
652 }
653
654 /*  Assign the custom baud rate to the channel structure */
655
656 static void dgnc_set_custom_speed(struct channel_t *ch, uint newrate)
657 {
658         int testdiv;
659         int testrate_high;
660         int testrate_low;
661         int deltahigh;
662         int deltalow;
663
664         if (newrate <= 0) {
665                 ch->ch_custom_speed = 0;
666                 return;
667         }
668
669         /*
670          *  Since the divisor is stored in a 16-bit integer, we make sure
671          *  we don't allow any rates smaller than a 16-bit integer would allow.
672          *  And of course, rates above the dividend won't fly.
673          */
674         if (newrate && newrate < ((ch->ch_bd->bd_dividend / 0xFFFF) + 1))
675                 newrate = (ch->ch_bd->bd_dividend / 0xFFFF) + 1;
676
677         if (newrate && newrate > ch->ch_bd->bd_dividend)
678                 newrate = ch->ch_bd->bd_dividend;
679
680         if (newrate > 0) {
681                 testdiv = ch->ch_bd->bd_dividend / newrate;
682
683                 /*
684                  *  If we try to figure out what rate the board would use
685                  *  with the test divisor, it will be either equal or higher
686                  *  than the requested baud rate.  If we then determine the
687                  *  rate with a divisor one higher, we will get the next lower
688                  *  supported rate below the requested.
689                  */
690                 testrate_high = ch->ch_bd->bd_dividend / testdiv;
691                 testrate_low  = ch->ch_bd->bd_dividend / (testdiv + 1);
692
693                 /*
694                  *  If the rate for the requested divisor is correct, just
695                  *  use it and be done.
696                  */
697                 if (testrate_high != newrate) {
698                         /*
699                          *  Otherwise, pick the rate that is closer
700                          *  (i.e. whichever rate has a smaller delta).
701                          */
702                         deltahigh = testrate_high - newrate;
703                         deltalow = newrate - testrate_low;
704
705                         if (deltahigh < deltalow)
706                                 newrate = testrate_high;
707                         else
708                                 newrate = testrate_low;
709                 }
710         }
711
712         ch->ch_custom_speed = newrate;
713 }
714
715 void dgnc_check_queue_flow_control(struct channel_t *ch)
716 {
717         int qleft;
718
719         /* Store how much space we have left in the queue */
720         qleft = ch->ch_r_tail - ch->ch_r_head - 1;
721         if (qleft < 0)
722                 qleft += RQUEUEMASK + 1;
723
724         /*
725          * Check to see if we should enforce flow control on our queue because
726          * the ld (or user) isn't reading data out of our queue fast enuf.
727          *
728          * NOTE: This is done based on what the current flow control of the
729          * port is set for.
730          *
731          * 1) HWFLOW (RTS) - Turn off the UART's Receive interrupt.
732          *      This will cause the UART's FIFO to back up, and force
733          *      the RTS signal to be dropped.
734          * 2) SWFLOW (IXOFF) - Keep trying to send a stop character to
735          *      the other side, in hopes it will stop sending data to us.
736          * 3) NONE - Nothing we can do.  We will simply drop any extra data
737          *      that gets sent into us when the queue fills up.
738          */
739         if (qleft < 256) {
740                 /* HWFLOW */
741                 if (ch->ch_digi.digi_flags & CTSPACE ||
742                     ch->ch_c_cflag & CRTSCTS) {
743                         if (!(ch->ch_flags & CH_RECEIVER_OFF)) {
744                                 ch->ch_bd->bd_ops->disable_receiver(ch);
745                                 ch->ch_flags |= (CH_RECEIVER_OFF);
746                         }
747                 }
748                 /* SWFLOW */
749                 else if (ch->ch_c_iflag & IXOFF) {
750                         if (ch->ch_stops_sent <= MAX_STOPS_SENT) {
751                                 ch->ch_bd->bd_ops->send_stop_character(ch);
752                                 ch->ch_stops_sent++;
753                         }
754                 }
755         }
756
757         /*
758          * Check to see if we should unenforce flow control because
759          * ld (or user) finally read enuf data out of our queue.
760          *
761          * NOTE: This is done based on what the current flow control of the
762          * port is set for.
763          *
764          * 1) HWFLOW (RTS) - Turn back on the UART's Receive interrupt.
765          *      This will cause the UART's FIFO to raise RTS back up,
766          *      which will allow the other side to start sending data again.
767          * 2) SWFLOW (IXOFF) - Send a start character to
768          *      the other side, so it will start sending data to us again.
769          * 3) NONE - Do nothing. Since we didn't do anything to turn off the
770          *      other side, we don't need to do anything now.
771          */
772         if (qleft > (RQUEUESIZE / 2)) {
773                 /* HWFLOW */
774                 if (ch->ch_digi.digi_flags & RTSPACE ||
775                     ch->ch_c_cflag & CRTSCTS) {
776                         if (ch->ch_flags & CH_RECEIVER_OFF) {
777                                 ch->ch_bd->bd_ops->enable_receiver(ch);
778                                 ch->ch_flags &= ~(CH_RECEIVER_OFF);
779                         }
780                 }
781                 /* SWFLOW */
782                 else if (ch->ch_c_iflag & IXOFF && ch->ch_stops_sent) {
783                         ch->ch_stops_sent = 0;
784                         ch->ch_bd->bd_ops->send_start_character(ch);
785                 }
786         }
787 }
788
789 static void dgnc_set_signal_low(struct channel_t *ch, const unsigned char sig)
790 {
791         ch->ch_mostat &= ~(sig);
792         ch->ch_bd->bd_ops->assert_modem_signals(ch);
793 }
794
795 void dgnc_wakeup_writes(struct channel_t *ch)
796 {
797         int qlen = 0;
798         unsigned long flags;
799
800         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
801                 return;
802
803         spin_lock_irqsave(&ch->ch_lock, flags);
804
805         /* If channel now has space, wake up anyone waiting on the condition. */
806
807         qlen = ch->ch_w_head - ch->ch_w_tail;
808         if (qlen < 0)
809                 qlen += WQUEUESIZE;
810
811         if (qlen >= (WQUEUESIZE - 256)) {
812                 spin_unlock_irqrestore(&ch->ch_lock, flags);
813                 return;
814         }
815
816         if (ch->ch_tun.un_flags & UN_ISOPEN) {
817                 tty_wakeup(ch->ch_tun.un_tty);
818
819                 /*
820                  * If unit is set to wait until empty, check to make sure
821                  * the queue AND FIFO are both empty.
822                  */
823                 if (ch->ch_tun.un_flags & UN_EMPTY) {
824                         if ((qlen == 0) &&
825                             (ch->ch_bd->bd_ops->get_uart_bytes_left(ch) == 0)) {
826                                 ch->ch_tun.un_flags &= ~(UN_EMPTY);
827
828                                 /*
829                                  * If RTS Toggle mode is on, whenever
830                                  * the queue and UART is empty, keep RTS low.
831                                  */
832                                 if (ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE)
833                                         dgnc_set_signal_low(ch, UART_MCR_RTS);
834
835                                 /*
836                                  * If DTR Toggle mode is on, whenever
837                                  * the queue and UART is empty, keep DTR low.
838                                  */
839                                 if (ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE)
840                                         dgnc_set_signal_low(ch, UART_MCR_DTR);
841                         }
842                 }
843
844                 wake_up_interruptible(&ch->ch_tun.un_flags_wait);
845         }
846
847         if (ch->ch_pun.un_flags & UN_ISOPEN) {
848                 tty_wakeup(ch->ch_pun.un_tty);
849
850                 /*
851                  * If unit is set to wait until empty, check to make sure
852                  * the queue AND FIFO are both empty.
853                  */
854                 if (ch->ch_pun.un_flags & UN_EMPTY) {
855                         if ((qlen == 0) &&
856                             (ch->ch_bd->bd_ops->get_uart_bytes_left(ch) == 0))
857                                 ch->ch_pun.un_flags &= ~(UN_EMPTY);
858                 }
859
860                 wake_up_interruptible(&ch->ch_pun.un_flags_wait);
861         }
862
863         spin_unlock_irqrestore(&ch->ch_lock, flags);
864 }
865
866 static struct dgnc_board *find_board_by_major(unsigned int major)
867 {
868         int i;
869
870         for (i = 0; i < MAXBOARDS; i++) {
871                 struct dgnc_board *brd = dgnc_board[i];
872
873                 if (!brd)
874                         return NULL;
875
876                 if (major == brd->serial_driver->major ||
877                     major == brd->print_driver->major)
878                         return brd;
879         }
880
881         return NULL;
882 }
883
884 /* TTY Entry points and helper functions */
885
886 /* dgnc_tty_open() */
887
888 static int dgnc_tty_open(struct tty_struct *tty, struct file *file)
889 {
890         struct dgnc_board       *brd;
891         struct channel_t *ch;
892         struct un_t     *un;
893         uint            major = 0;
894         uint            minor = 0;
895         int             rc = 0;
896         unsigned long flags;
897
898         rc = 0;
899
900         major = MAJOR(tty_devnum(tty));
901         minor = MINOR(tty_devnum(tty));
902
903         if (major > 255)
904                 return -ENXIO;
905
906         /* Get board pointer from our array of majors we have allocated */
907         brd = find_board_by_major(major);
908         if (!brd)
909                 return -ENXIO;
910
911         /*
912          * If board is not yet up to a state of READY, go to
913          * sleep waiting for it to happen or they cancel the open.
914          */
915         rc = wait_event_interruptible(brd->state_wait,
916                                       (brd->state & BOARD_READY));
917
918         if (rc)
919                 return rc;
920
921         spin_lock_irqsave(&brd->bd_lock, flags);
922
923         /* If opened device is greater than our number of ports, bail. */
924         if (PORT_NUM(minor) >= brd->nasync) {
925                 spin_unlock_irqrestore(&brd->bd_lock, flags);
926                 return -ENXIO;
927         }
928
929         ch = brd->channels[PORT_NUM(minor)];
930         if (!ch) {
931                 spin_unlock_irqrestore(&brd->bd_lock, flags);
932                 return -ENXIO;
933         }
934
935         /* Drop board lock */
936         spin_unlock_irqrestore(&brd->bd_lock, flags);
937
938         /* Grab channel lock */
939         spin_lock_irqsave(&ch->ch_lock, flags);
940
941         /* Figure out our type */
942         if (!IS_PRINT(minor)) {
943                 un = &brd->channels[PORT_NUM(minor)]->ch_tun;
944                 un->un_type = DGNC_SERIAL;
945         } else if (IS_PRINT(minor)) {
946                 un = &brd->channels[PORT_NUM(minor)]->ch_pun;
947                 un->un_type = DGNC_PRINT;
948         } else {
949                 spin_unlock_irqrestore(&ch->ch_lock, flags);
950                 return -ENXIO;
951         }
952
953         /*
954          * If the port is still in a previous open, and in a state
955          * where we simply cannot safely keep going, wait until the
956          * state clears.
957          */
958         spin_unlock_irqrestore(&ch->ch_lock, flags);
959
960         rc = wait_event_interruptible(ch->ch_flags_wait,
961                                       ((ch->ch_flags & CH_OPENING) == 0));
962
963         /* If ret is non-zero, user ctrl-c'ed us */
964         if (rc)
965                 return -EINTR;
966
967         /*
968          * If either unit is in the middle of the fragile part of close,
969          * we just cannot touch the channel safely.
970          * Go to sleep, knowing that when the channel can be
971          * touched safely, the close routine will signal the
972          * ch_flags_wait to wake us back up.
973          */
974         rc = wait_event_interruptible(ch->ch_flags_wait,
975                         (((ch->ch_tun.un_flags |
976                            ch->ch_pun.un_flags) & UN_CLOSING) == 0));
977
978         /* If ret is non-zero, user ctrl-c'ed us */
979         if (rc)
980                 return -EINTR;
981
982         spin_lock_irqsave(&ch->ch_lock, flags);
983
984         /* Store our unit into driver_data, so we always have it available. */
985         tty->driver_data = un;
986
987         /* Initialize tty's */
988
989         if (!(un->un_flags & UN_ISOPEN)) {
990                 /* Store important variables. */
991                 un->un_tty     = tty;
992
993                 /* Maybe do something here to the TTY struct as well? */
994         }
995
996         /*
997          * Allocate channel buffers for read/write/error.
998          * Set flag, so we don't get trounced on.
999          */
1000         ch->ch_flags |= (CH_OPENING);
1001
1002         /* Drop locks, as malloc with GFP_KERNEL can sleep */
1003         spin_unlock_irqrestore(&ch->ch_lock, flags);
1004
1005         if (!ch->ch_rqueue)
1006                 ch->ch_rqueue = kzalloc(RQUEUESIZE, GFP_KERNEL);
1007         if (!ch->ch_equeue)
1008                 ch->ch_equeue = kzalloc(EQUEUESIZE, GFP_KERNEL);
1009         if (!ch->ch_wqueue)
1010                 ch->ch_wqueue = kzalloc(WQUEUESIZE, GFP_KERNEL);
1011
1012         if (!ch->ch_rqueue || !ch->ch_equeue || !ch->ch_wqueue) {
1013                 kfree(ch->ch_rqueue);
1014                 kfree(ch->ch_equeue);
1015                 kfree(ch->ch_wqueue);
1016
1017                 return -ENOMEM;
1018         }
1019
1020         spin_lock_irqsave(&ch->ch_lock, flags);
1021
1022         ch->ch_flags &= ~(CH_OPENING);
1023         wake_up_interruptible(&ch->ch_flags_wait);
1024
1025         /* Initialize if neither terminal or printer is open. */
1026
1027         if (!((ch->ch_tun.un_flags | ch->ch_pun.un_flags) & UN_ISOPEN)) {
1028                 /* Flush input queues. */
1029                 ch->ch_r_head = 0;
1030                 ch->ch_r_tail = 0;
1031                 ch->ch_e_head = 0;
1032                 ch->ch_e_tail = 0;
1033                 ch->ch_w_head = 0;
1034                 ch->ch_w_tail = 0;
1035
1036                 brd->bd_ops->flush_uart_write(ch);
1037                 brd->bd_ops->flush_uart_read(ch);
1038
1039                 ch->ch_flags = 0;
1040                 ch->ch_cached_lsr = 0;
1041                 ch->ch_stop_sending_break = 0;
1042                 ch->ch_stops_sent = 0;
1043
1044                 ch->ch_c_cflag   = tty->termios.c_cflag;
1045                 ch->ch_c_iflag   = tty->termios.c_iflag;
1046                 ch->ch_c_oflag   = tty->termios.c_oflag;
1047                 ch->ch_c_lflag   = tty->termios.c_lflag;
1048                 ch->ch_startc = tty->termios.c_cc[VSTART];
1049                 ch->ch_stopc  = tty->termios.c_cc[VSTOP];
1050
1051                 /*
1052                  * Bring up RTS and DTR...
1053                  * Also handle RTS or DTR toggle if set.
1054                  */
1055                 if (!(ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE))
1056                         ch->ch_mostat |= (UART_MCR_RTS);
1057                 if (!(ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE))
1058                         ch->ch_mostat |= (UART_MCR_DTR);
1059
1060                 /* Tell UART to init itself */
1061                 brd->bd_ops->uart_init(ch);
1062         }
1063
1064         /* Run param in case we changed anything */
1065
1066         brd->bd_ops->param(tty);
1067
1068         dgnc_carrier(ch);
1069
1070         /* follow protocol for opening port */
1071
1072         spin_unlock_irqrestore(&ch->ch_lock, flags);
1073
1074         rc = dgnc_block_til_ready(tty, file, ch);
1075
1076         /* No going back now, increment our unit and channel counters */
1077         spin_lock_irqsave(&ch->ch_lock, flags);
1078         ch->ch_open_count++;
1079         un->un_open_count++;
1080         un->un_flags |= (UN_ISOPEN);
1081         spin_unlock_irqrestore(&ch->ch_lock, flags);
1082
1083         return rc;
1084 }
1085
1086 /*
1087  * dgnc_block_til_ready()
1088  *
1089  * Wait for DCD, if needed.
1090  */
1091 static int dgnc_block_til_ready(struct tty_struct *tty,
1092                                 struct file *file,
1093                                 struct channel_t *ch)
1094 {
1095         int retval = 0;
1096         struct un_t *un = tty->driver_data;
1097         unsigned long flags;
1098         uint    old_flags = 0;
1099         int     sleep_on_un_flags = 0;
1100
1101         if (!file)
1102                 return -ENXIO;
1103
1104         spin_lock_irqsave(&ch->ch_lock, flags);
1105
1106         ch->ch_wopen++;
1107
1108         /* Loop forever */
1109         while (1) {
1110                 sleep_on_un_flags = 0;
1111
1112                 /*
1113                  * If board has failed somehow during our sleep,
1114                  * bail with error.
1115                  */
1116                 if (ch->ch_bd->state == BOARD_FAILED) {
1117                         retval = -ENXIO;
1118                         break;
1119                 }
1120
1121                 /* If tty was hung up, break out of loop and set error. */
1122                 if (tty_hung_up_p(file)) {
1123                         retval = -EAGAIN;
1124                         break;
1125                 }
1126
1127                 /*
1128                  * If either unit is in the middle of the fragile part of close,
1129                  * we just cannot touch the channel safely.
1130                  * Go back to sleep, knowing that when the channel can be
1131                  * touched safely, the close routine will signal the
1132                  * ch_wait_flags to wake us back up.
1133                  */
1134                 if (!((ch->ch_tun.un_flags |
1135                     ch->ch_pun.un_flags) &
1136                     UN_CLOSING)) {
1137                         /*
1138                          * Our conditions to leave cleanly and happily:
1139                          * 1) NONBLOCKING on the tty is set.
1140                          * 2) CLOCAL is set.
1141                          * 3) DCD (fake or real) is active.
1142                          */
1143
1144                         if (file->f_flags & O_NONBLOCK)
1145                                 break;
1146
1147                         if (tty_io_error(tty)) {
1148                                 retval = -EIO;
1149                                 break;
1150                         }
1151
1152                         if (ch->ch_flags & CH_CD)
1153                                 break;
1154
1155                         if (ch->ch_flags & CH_FCAR)
1156                                 break;
1157                 } else {
1158                         sleep_on_un_flags = 1;
1159                 }
1160
1161                 /*
1162                  * If there is a signal pending, the user probably
1163                  * interrupted (ctrl-c) us.
1164                  * Leave loop with error set.
1165                  */
1166                 if (signal_pending(current)) {
1167                         retval = -ERESTARTSYS;
1168                         break;
1169                 }
1170
1171                 /* Store the flags before we let go of channel lock */
1172
1173                 if (sleep_on_un_flags)
1174                         old_flags = ch->ch_tun.un_flags | ch->ch_pun.un_flags;
1175                 else
1176                         old_flags = ch->ch_flags;
1177
1178                 /*
1179                  * Let go of channel lock before calling schedule.
1180                  * Our poller will get any FEP events and wake us up when DCD
1181                  * eventually goes active.
1182                  */
1183
1184                 spin_unlock_irqrestore(&ch->ch_lock, flags);
1185
1186                 /*
1187                  * Wait for something in the flags to change
1188                  * from the current value.
1189                  */
1190                 if (sleep_on_un_flags)
1191                         retval = wait_event_interruptible
1192                                 (un->un_flags_wait,
1193                                  (old_flags != (ch->ch_tun.un_flags |
1194                                                 ch->ch_pun.un_flags)));
1195                 else
1196                         retval = wait_event_interruptible(ch->ch_flags_wait,
1197                                         (old_flags != ch->ch_flags));
1198
1199                 /*
1200                  * We got woken up for some reason.
1201                  * Before looping around, grab our channel lock.
1202                  */
1203                 spin_lock_irqsave(&ch->ch_lock, flags);
1204         }
1205
1206         ch->ch_wopen--;
1207
1208         spin_unlock_irqrestore(&ch->ch_lock, flags);
1209
1210         return retval;
1211 }
1212
1213 /*
1214  * dgnc_tty_hangup()
1215  *
1216  * Hangup the port.  Like a close, but don't wait for output to drain.
1217  */
1218 static void dgnc_tty_hangup(struct tty_struct *tty)
1219 {
1220         if (!tty || tty->magic != TTY_MAGIC)
1221                 return;
1222
1223         /* flush the transmit queues */
1224         dgnc_tty_flush_buffer(tty);
1225 }
1226
1227 /* dgnc_tty_close() */
1228
1229 static void dgnc_tty_close(struct tty_struct *tty, struct file *file)
1230 {
1231         struct dgnc_board *bd;
1232         struct channel_t *ch;
1233         struct un_t *un;
1234         unsigned long flags;
1235
1236         if (!tty || tty->magic != TTY_MAGIC)
1237                 return;
1238
1239         un = tty->driver_data;
1240         if (!un || un->magic != DGNC_UNIT_MAGIC)
1241                 return;
1242
1243         ch = un->un_ch;
1244         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1245                 return;
1246
1247         bd = ch->ch_bd;
1248         if (!bd || bd->magic != DGNC_BOARD_MAGIC)
1249                 return;
1250
1251         spin_lock_irqsave(&ch->ch_lock, flags);
1252
1253         /*
1254          * Determine if this is the last close or not - and if we agree about
1255          * which type of close it is with the Line Discipline
1256          */
1257         if ((tty->count == 1) && (un->un_open_count != 1)) {
1258                 /*
1259                  * Uh, oh.  tty->count is 1, which means that the tty
1260                  * structure will be freed.  un_open_count should always
1261                  * be one in these conditions.  If it's greater than
1262                  * one, we've got real problems, since it means the
1263                  * serial port won't be shutdown.
1264                  */
1265                 dev_dbg(tty->dev,
1266                         "tty->count is 1, un open count is %d\n",
1267                         un->un_open_count);
1268                 un->un_open_count = 1;
1269         }
1270
1271         if (un->un_open_count)
1272                 un->un_open_count--;
1273         else
1274                 dev_dbg(tty->dev,
1275                         "bad serial port open count of %d\n",
1276                         un->un_open_count);
1277
1278         ch->ch_open_count--;
1279
1280         if (ch->ch_open_count && un->un_open_count) {
1281                 spin_unlock_irqrestore(&ch->ch_lock, flags);
1282                 return;
1283         }
1284
1285         /* OK, its the last close on the unit */
1286         un->un_flags |= UN_CLOSING;
1287
1288         tty->closing = 1;
1289
1290         /*
1291          * Only officially close channel if count is 0 and
1292          * DIGI_PRINTER bit is not set.
1293          */
1294         if ((ch->ch_open_count == 0) &&
1295             !(ch->ch_digi.digi_flags & DIGI_PRINTER)) {
1296                 ch->ch_flags &= ~(CH_STOPI | CH_FORCED_STOPI);
1297
1298                 /* turn off print device when closing print device. */
1299
1300                 if ((un->un_type == DGNC_PRINT) && (ch->ch_flags & CH_PRON)) {
1301                         dgnc_wmove(ch, ch->ch_digi.digi_offstr,
1302                                    (int)ch->ch_digi.digi_offlen);
1303                         ch->ch_flags &= ~CH_PRON;
1304                 }
1305
1306                 spin_unlock_irqrestore(&ch->ch_lock, flags);
1307                 /* wait for output to drain */
1308                 /* This will also return if we take an interrupt */
1309
1310                 bd->bd_ops->drain(tty, 0);
1311
1312                 dgnc_tty_flush_buffer(tty);
1313                 tty_ldisc_flush(tty);
1314
1315                 spin_lock_irqsave(&ch->ch_lock, flags);
1316
1317                 tty->closing = 0;
1318
1319                 /* If we have HUPCL set, lower DTR and RTS */
1320
1321                 if (ch->ch_c_cflag & HUPCL) {
1322                         /* Drop RTS/DTR */
1323                         ch->ch_mostat &= ~(UART_MCR_DTR | UART_MCR_RTS);
1324                         bd->bd_ops->assert_modem_signals(ch);
1325
1326                         /*
1327                          * Go to sleep to ensure RTS/DTR
1328                          * have been dropped for modems to see it.
1329                          */
1330                         if (ch->ch_close_delay) {
1331                                 spin_unlock_irqrestore(&ch->ch_lock,
1332                                                        flags);
1333                                 dgnc_ms_sleep(ch->ch_close_delay);
1334                                 spin_lock_irqsave(&ch->ch_lock, flags);
1335                         }
1336                 }
1337
1338                 ch->ch_old_baud = 0;
1339
1340                 /* Turn off UART interrupts for this port */
1341                 ch->ch_bd->bd_ops->uart_off(ch);
1342         } else {
1343                 /* turn off print device when closing print device. */
1344
1345                 if ((un->un_type == DGNC_PRINT) && (ch->ch_flags & CH_PRON)) {
1346                         dgnc_wmove(ch, ch->ch_digi.digi_offstr,
1347                                    (int)ch->ch_digi.digi_offlen);
1348                         ch->ch_flags &= ~CH_PRON;
1349                 }
1350         }
1351
1352         un->un_tty = NULL;
1353         un->un_flags &= ~(UN_ISOPEN | UN_CLOSING);
1354
1355         wake_up_interruptible(&ch->ch_flags_wait);
1356         wake_up_interruptible(&un->un_flags_wait);
1357
1358         spin_unlock_irqrestore(&ch->ch_lock, flags);
1359 }
1360
1361 /*
1362  * dgnc_tty_chars_in_buffer()
1363  *
1364  * Return number of characters that have not been transmitted yet.
1365  *
1366  * This routine is used by the line discipline to determine if there
1367  * is data waiting to be transmitted/drained/flushed or not.
1368  */
1369 static int dgnc_tty_chars_in_buffer(struct tty_struct *tty)
1370 {
1371         struct channel_t *ch = NULL;
1372         struct un_t *un = NULL;
1373         ushort thead;
1374         ushort ttail;
1375         uint tmask;
1376         uint chars = 0;
1377         unsigned long flags;
1378
1379         if (!tty)
1380                 return 0;
1381
1382         un = tty->driver_data;
1383         if (!un || un->magic != DGNC_UNIT_MAGIC)
1384                 return 0;
1385
1386         ch = un->un_ch;
1387         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1388                 return 0;
1389
1390         spin_lock_irqsave(&ch->ch_lock, flags);
1391
1392         tmask = WQUEUEMASK;
1393         thead = ch->ch_w_head & tmask;
1394         ttail = ch->ch_w_tail & tmask;
1395
1396         spin_unlock_irqrestore(&ch->ch_lock, flags);
1397
1398         if (ttail == thead) {
1399                 chars = 0;
1400         } else {
1401                 if (thead >= ttail)
1402                         chars = thead - ttail;
1403                 else
1404                         chars = thead - ttail + WQUEUESIZE;
1405         }
1406
1407         return chars;
1408 }
1409
1410 /*
1411  * dgnc_maxcps_room
1412  *
1413  * Reduces bytes_available to the max number of characters
1414  * that can be sent currently given the maxcps value, and
1415  * returns the new bytes_available.  This only affects printer
1416  * output.
1417  */
1418 static int dgnc_maxcps_room(struct channel_t *ch, int bytes_available)
1419 {
1420         if (ch->ch_digi.digi_maxcps > 0 && ch->ch_digi.digi_bufsize > 0) {
1421                 int cps_limit = 0;
1422                 unsigned long current_time = jiffies;
1423                 unsigned long buffer_time = current_time +
1424                         (HZ * ch->ch_digi.digi_bufsize) /
1425                         ch->ch_digi.digi_maxcps;
1426
1427                 if (ch->ch_cpstime < current_time) {
1428                         /* buffer is empty */
1429                         ch->ch_cpstime = current_time;  /* reset ch_cpstime */
1430                         cps_limit = ch->ch_digi.digi_bufsize;
1431                 } else if (ch->ch_cpstime < buffer_time) {
1432                         /* still room in the buffer */
1433                         cps_limit = ((buffer_time - ch->ch_cpstime) *
1434                                         ch->ch_digi.digi_maxcps) / HZ;
1435                 } else {
1436                         /* no room in the buffer */
1437                         cps_limit = 0;
1438                 }
1439
1440                 bytes_available = min(cps_limit, bytes_available);
1441         }
1442
1443         return bytes_available;
1444 }
1445
1446 /*
1447  * dgnc_tty_write_room()
1448  *
1449  * Return space available in Tx buffer
1450  */
1451 static int dgnc_tty_write_room(struct tty_struct *tty)
1452 {
1453         struct channel_t *ch = NULL;
1454         struct un_t *un = NULL;
1455         ushort head;
1456         ushort tail;
1457         ushort tmask;
1458         int ret = 0;
1459         unsigned long flags;
1460
1461         if (!tty)
1462                 return 0;
1463
1464         un = tty->driver_data;
1465         if (!un || un->magic != DGNC_UNIT_MAGIC)
1466                 return 0;
1467
1468         ch = un->un_ch;
1469         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1470                 return 0;
1471
1472         spin_lock_irqsave(&ch->ch_lock, flags);
1473
1474         tmask = WQUEUEMASK;
1475         head = (ch->ch_w_head) & tmask;
1476         tail = (ch->ch_w_tail) & tmask;
1477
1478         ret = tail - head - 1;
1479         if (ret < 0)
1480                 ret += WQUEUESIZE;
1481
1482         /* Limit printer to maxcps */
1483         if (un->un_type != DGNC_PRINT)
1484                 ret = dgnc_maxcps_room(ch, ret);
1485
1486         /*
1487          * If we are printer device, leave space for
1488          * possibly both the on and off strings.
1489          */
1490         if (un->un_type == DGNC_PRINT) {
1491                 if (!(ch->ch_flags & CH_PRON))
1492                         ret -= ch->ch_digi.digi_onlen;
1493                 ret -= ch->ch_digi.digi_offlen;
1494         } else {
1495                 if (ch->ch_flags & CH_PRON)
1496                         ret -= ch->ch_digi.digi_offlen;
1497         }
1498
1499         if (ret < 0)
1500                 ret = 0;
1501
1502         spin_unlock_irqrestore(&ch->ch_lock, flags);
1503
1504         return ret;
1505 }
1506
1507 /*
1508  * dgnc_tty_put_char()
1509  *
1510  * Put a character into ch->ch_buf
1511  *
1512  *      - used by the line discipline for OPOST processing
1513  */
1514 static int dgnc_tty_put_char(struct tty_struct *tty, unsigned char c)
1515 {
1516         /* Simply call tty_write. */
1517
1518         dgnc_tty_write(tty, &c, 1);
1519         return 1;
1520 }
1521
1522 /*
1523  * dgnc_tty_write()
1524  *
1525  * Take data from the user or kernel and send it out to the FEP.
1526  * In here exists all the Transparent Print magic as well.
1527  */
1528 static int dgnc_tty_write(struct tty_struct *tty,
1529                           const unsigned char *buf, int count)
1530 {
1531         struct channel_t *ch = NULL;
1532         struct un_t *un = NULL;
1533         int bufcount = 0, n = 0;
1534         unsigned long flags;
1535         ushort head;
1536         ushort tail;
1537         ushort tmask;
1538         uint remain;
1539
1540         if (!tty)
1541                 return 0;
1542
1543         un = tty->driver_data;
1544         if (!un || un->magic != DGNC_UNIT_MAGIC)
1545                 return 0;
1546
1547         ch = un->un_ch;
1548         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1549                 return 0;
1550
1551         if (!count)
1552                 return 0;
1553
1554         /*
1555          * Store original amount of characters passed in.
1556          * This helps to figure out if we should ask the FEP
1557          * to send us an event when it has more space available.
1558          */
1559
1560         spin_lock_irqsave(&ch->ch_lock, flags);
1561
1562         /* Get our space available for the channel from the board */
1563         tmask = WQUEUEMASK;
1564         head = (ch->ch_w_head) & tmask;
1565         tail = (ch->ch_w_tail) & tmask;
1566
1567         bufcount = tail - head - 1;
1568         if (bufcount < 0)
1569                 bufcount += WQUEUESIZE;
1570
1571         /*
1572          * Limit printer output to maxcps overall, with bursts allowed
1573          * up to bufsize characters.
1574          */
1575         if (un->un_type != DGNC_PRINT)
1576                 bufcount = dgnc_maxcps_room(ch, bufcount);
1577
1578         /*
1579          * Take minimum of what the user wants to send, and the
1580          * space available in the FEP buffer.
1581          */
1582         count = min(count, bufcount);
1583
1584         /* Bail if no space left. */
1585
1586         if (count <= 0)
1587                 goto exit_retry;
1588
1589         /*
1590          * Output the printer ON string, if we are in terminal mode, but
1591          * need to be in printer mode.
1592          */
1593         if ((un->un_type == DGNC_PRINT) && !(ch->ch_flags & CH_PRON)) {
1594                 dgnc_wmove(ch, ch->ch_digi.digi_onstr,
1595                            (int)ch->ch_digi.digi_onlen);
1596                 head = (ch->ch_w_head) & tmask;
1597                 ch->ch_flags |= CH_PRON;
1598         }
1599
1600         /*
1601          * On the other hand, output the printer OFF string, if we are
1602          * currently in printer mode, but need to output to the terminal.
1603          */
1604         if ((un->un_type != DGNC_PRINT) && (ch->ch_flags & CH_PRON)) {
1605                 dgnc_wmove(ch, ch->ch_digi.digi_offstr,
1606                            (int)ch->ch_digi.digi_offlen);
1607                 head = (ch->ch_w_head) & tmask;
1608                 ch->ch_flags &= ~CH_PRON;
1609         }
1610
1611         n = count;
1612
1613         /*
1614          * If the write wraps over the top of the circular buffer,
1615          * move the portion up to the wrap point, and reset the
1616          * pointers to the bottom.
1617          */
1618         remain = WQUEUESIZE - head;
1619
1620         if (n >= remain) {
1621                 n -= remain;
1622                 memcpy(ch->ch_wqueue + head, buf, remain);
1623                 head = 0;
1624                 buf += remain;
1625         }
1626
1627         if (n > 0) {
1628                 /* Move rest of data. */
1629                 remain = n;
1630                 memcpy(ch->ch_wqueue + head, buf, remain);
1631                 head += remain;
1632         }
1633
1634         if (count) {
1635                 head &= tmask;
1636                 ch->ch_w_head = head;
1637         }
1638
1639         /* Update printer buffer empty time. */
1640         if ((un->un_type == DGNC_PRINT) && (ch->ch_digi.digi_maxcps > 0) &&
1641             (ch->ch_digi.digi_bufsize > 0)) {
1642                 ch->ch_cpstime += (HZ * count) / ch->ch_digi.digi_maxcps;
1643         }
1644
1645         spin_unlock_irqrestore(&ch->ch_lock, flags);
1646
1647         if (count) {
1648                 /*
1649                  * Channel lock is grabbed and then released
1650                  * inside this routine.
1651                  */
1652                 ch->ch_bd->bd_ops->copy_data_from_queue_to_uart(ch);
1653         }
1654
1655         return count;
1656
1657 exit_retry:
1658
1659         spin_unlock_irqrestore(&ch->ch_lock, flags);
1660         return 0;
1661 }
1662
1663 /* Return modem signals to ld. */
1664
1665 static int dgnc_tty_tiocmget(struct tty_struct *tty)
1666 {
1667         struct channel_t *ch;
1668         struct un_t *un;
1669         int result = -EIO;
1670         unsigned char mstat = 0;
1671         unsigned long flags;
1672
1673         if (!tty || tty->magic != TTY_MAGIC)
1674                 return result;
1675
1676         un = tty->driver_data;
1677         if (!un || un->magic != DGNC_UNIT_MAGIC)
1678                 return result;
1679
1680         ch = un->un_ch;
1681         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1682                 return result;
1683
1684         spin_lock_irqsave(&ch->ch_lock, flags);
1685
1686         mstat = ch->ch_mostat | ch->ch_mistat;
1687
1688         spin_unlock_irqrestore(&ch->ch_lock, flags);
1689
1690         result = 0;
1691
1692         if (mstat & UART_MCR_DTR)
1693                 result |= TIOCM_DTR;
1694         if (mstat & UART_MCR_RTS)
1695                 result |= TIOCM_RTS;
1696         if (mstat & UART_MSR_CTS)
1697                 result |= TIOCM_CTS;
1698         if (mstat & UART_MSR_DSR)
1699                 result |= TIOCM_DSR;
1700         if (mstat & UART_MSR_RI)
1701                 result |= TIOCM_RI;
1702         if (mstat & UART_MSR_DCD)
1703                 result |= TIOCM_CD;
1704
1705         return result;
1706 }
1707
1708 /*
1709  * dgnc_tty_tiocmset()
1710  *
1711  * Set modem signals, called by ld.
1712  */
1713
1714 static int dgnc_tty_tiocmset(struct tty_struct *tty,
1715                              unsigned int set, unsigned int clear)
1716 {
1717         struct dgnc_board *bd;
1718         struct channel_t *ch;
1719         struct un_t *un;
1720         int ret = -EIO;
1721         unsigned long flags;
1722
1723         if (!tty || tty->magic != TTY_MAGIC)
1724                 return ret;
1725
1726         un = tty->driver_data;
1727         if (!un || un->magic != DGNC_UNIT_MAGIC)
1728                 return ret;
1729
1730         ch = un->un_ch;
1731         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1732                 return ret;
1733
1734         bd = ch->ch_bd;
1735         if (!bd || bd->magic != DGNC_BOARD_MAGIC)
1736                 return ret;
1737
1738         spin_lock_irqsave(&ch->ch_lock, flags);
1739
1740         if (set & TIOCM_RTS)
1741                 ch->ch_mostat |= UART_MCR_RTS;
1742
1743         if (set & TIOCM_DTR)
1744                 ch->ch_mostat |= UART_MCR_DTR;
1745
1746         if (clear & TIOCM_RTS)
1747                 ch->ch_mostat &= ~(UART_MCR_RTS);
1748
1749         if (clear & TIOCM_DTR)
1750                 ch->ch_mostat &= ~(UART_MCR_DTR);
1751
1752         ch->ch_bd->bd_ops->assert_modem_signals(ch);
1753
1754         spin_unlock_irqrestore(&ch->ch_lock, flags);
1755
1756         return 0;
1757 }
1758
1759 /*
1760  * dgnc_tty_send_break()
1761  *
1762  * Send a Break, called by ld.
1763  */
1764 static int dgnc_tty_send_break(struct tty_struct *tty, int msec)
1765 {
1766         struct dgnc_board *bd;
1767         struct channel_t *ch;
1768         struct un_t *un;
1769         int ret = -EIO;
1770         unsigned long flags;
1771
1772         if (!tty || tty->magic != TTY_MAGIC)
1773                 return ret;
1774
1775         un = tty->driver_data;
1776         if (!un || un->magic != DGNC_UNIT_MAGIC)
1777                 return ret;
1778
1779         ch = un->un_ch;
1780         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1781                 return ret;
1782
1783         bd = ch->ch_bd;
1784         if (!bd || bd->magic != DGNC_BOARD_MAGIC)
1785                 return ret;
1786
1787         switch (msec) {
1788         case -1:
1789                 msec = 0xFFFF;
1790                 break;
1791         case 0:
1792                 msec = 0;
1793                 break;
1794         default:
1795                 break;
1796         }
1797
1798         spin_lock_irqsave(&ch->ch_lock, flags);
1799
1800         ch->ch_bd->bd_ops->send_break(ch, msec);
1801
1802         spin_unlock_irqrestore(&ch->ch_lock, flags);
1803
1804         return 0;
1805 }
1806
1807 /*
1808  * dgnc_tty_wait_until_sent()
1809  *
1810  * wait until data has been transmitted, called by ld.
1811  */
1812 static void dgnc_tty_wait_until_sent(struct tty_struct *tty, int timeout)
1813 {
1814         struct dgnc_board *bd;
1815         struct channel_t *ch;
1816         struct un_t *un;
1817
1818         if (!tty || tty->magic != TTY_MAGIC)
1819                 return;
1820
1821         un = tty->driver_data;
1822         if (!un || un->magic != DGNC_UNIT_MAGIC)
1823                 return;
1824
1825         ch = un->un_ch;
1826         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1827                 return;
1828
1829         bd = ch->ch_bd;
1830         if (!bd || bd->magic != DGNC_BOARD_MAGIC)
1831                 return;
1832
1833         bd->bd_ops->drain(tty, 0);
1834 }
1835
1836 /*
1837  * dgnc_send_xchar()
1838  *
1839  * send a high priority character, called by ld.
1840  */
1841 static void dgnc_tty_send_xchar(struct tty_struct *tty, char c)
1842 {
1843         struct dgnc_board *bd;
1844         struct channel_t *ch;
1845         struct un_t *un;
1846         unsigned long flags;
1847
1848         if (!tty || tty->magic != TTY_MAGIC)
1849                 return;
1850
1851         un = tty->driver_data;
1852         if (!un || un->magic != DGNC_UNIT_MAGIC)
1853                 return;
1854
1855         ch = un->un_ch;
1856         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1857                 return;
1858
1859         bd = ch->ch_bd;
1860         if (!bd || bd->magic != DGNC_BOARD_MAGIC)
1861                 return;
1862
1863         dev_dbg(tty->dev, "dgnc_tty_send_xchar start\n");
1864
1865         spin_lock_irqsave(&ch->ch_lock, flags);
1866         bd->bd_ops->send_immediate_char(ch, c);
1867         spin_unlock_irqrestore(&ch->ch_lock, flags);
1868
1869         dev_dbg(tty->dev, "dgnc_tty_send_xchar finish\n");
1870 }
1871
1872 /* Return modem signals to ld. */
1873
1874 static inline int dgnc_get_mstat(struct channel_t *ch)
1875 {
1876         unsigned char mstat;
1877         int result = 0;
1878         unsigned long flags;
1879
1880         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1881                 return -ENXIO;
1882
1883         spin_lock_irqsave(&ch->ch_lock, flags);
1884
1885         mstat = ch->ch_mostat | ch->ch_mistat;
1886
1887         spin_unlock_irqrestore(&ch->ch_lock, flags);
1888
1889         if (mstat & UART_MCR_DTR)
1890                 result |= TIOCM_DTR;
1891         if (mstat & UART_MCR_RTS)
1892                 result |= TIOCM_RTS;
1893         if (mstat & UART_MSR_CTS)
1894                 result |= TIOCM_CTS;
1895         if (mstat & UART_MSR_DSR)
1896                 result |= TIOCM_DSR;
1897         if (mstat & UART_MSR_RI)
1898                 result |= TIOCM_RI;
1899         if (mstat & UART_MSR_DCD)
1900                 result |= TIOCM_CD;
1901
1902         return result;
1903 }
1904
1905 /* Return modem signals to ld. */
1906
1907 static int dgnc_get_modem_info(struct channel_t *ch,
1908                                unsigned int  __user *value)
1909 {
1910         return put_user(dgnc_get_mstat(ch), value);
1911 }
1912
1913 /*
1914  * dgnc_set_modem_info()
1915  *
1916  * Set modem signals, called by ld.
1917  */
1918 static int dgnc_set_modem_info(struct channel_t *ch,
1919                                unsigned int command,
1920                                unsigned int __user *value)
1921 {
1922         int ret = -ENXIO;
1923         unsigned int arg = 0;
1924         unsigned long flags;
1925
1926         ret = get_user(arg, value);
1927         if (ret)
1928                 return ret;
1929
1930         switch (command) {
1931         case TIOCMBIS:
1932                 if (arg & TIOCM_RTS)
1933                         ch->ch_mostat |= UART_MCR_RTS;
1934
1935                 if (arg & TIOCM_DTR)
1936                         ch->ch_mostat |= UART_MCR_DTR;
1937
1938                 break;
1939
1940         case TIOCMBIC:
1941                 if (arg & TIOCM_RTS)
1942                         ch->ch_mostat &= ~(UART_MCR_RTS);
1943
1944                 if (arg & TIOCM_DTR)
1945                         ch->ch_mostat &= ~(UART_MCR_DTR);
1946
1947                 break;
1948
1949         case TIOCMSET:
1950
1951                 if (arg & TIOCM_RTS)
1952                         ch->ch_mostat |= UART_MCR_RTS;
1953                 else
1954                         ch->ch_mostat &= ~(UART_MCR_RTS);
1955
1956                 if (arg & TIOCM_DTR)
1957                         ch->ch_mostat |= UART_MCR_DTR;
1958                 else
1959                         ch->ch_mostat &= ~(UART_MCR_DTR);
1960
1961                 break;
1962
1963         default:
1964                 return -EINVAL;
1965         }
1966
1967         spin_lock_irqsave(&ch->ch_lock, flags);
1968
1969         ch->ch_bd->bd_ops->assert_modem_signals(ch);
1970
1971         spin_unlock_irqrestore(&ch->ch_lock, flags);
1972
1973         return 0;
1974 }
1975
1976 /*
1977  * dgnc_tty_digigeta()
1978  *
1979  * Ioctl to get the information for ditty.
1980  */
1981 static int dgnc_tty_digigeta(struct tty_struct *tty,
1982                              struct digi_t __user *retinfo)
1983 {
1984         struct channel_t *ch;
1985         struct un_t *un;
1986         struct digi_t tmp;
1987         unsigned long flags;
1988
1989         if (!retinfo)
1990                 return -EFAULT;
1991
1992         if (!tty || tty->magic != TTY_MAGIC)
1993                 return -EFAULT;
1994
1995         un = tty->driver_data;
1996         if (!un || un->magic != DGNC_UNIT_MAGIC)
1997                 return -EFAULT;
1998
1999         ch = un->un_ch;
2000         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2001                 return -EFAULT;
2002
2003         memset(&tmp, 0, sizeof(tmp));
2004
2005         spin_lock_irqsave(&ch->ch_lock, flags);
2006         memcpy(&tmp, &ch->ch_digi, sizeof(tmp));
2007         spin_unlock_irqrestore(&ch->ch_lock, flags);
2008
2009         if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2010                 return -EFAULT;
2011
2012         return 0;
2013 }
2014
2015 /*
2016  * dgnc_tty_digiseta()
2017  *
2018  * Ioctl to set the information for ditty.
2019  */
2020 static int dgnc_tty_digiseta(struct tty_struct *tty,
2021                              struct digi_t __user *new_info)
2022 {
2023         struct dgnc_board *bd;
2024         struct channel_t *ch;
2025         struct un_t *un;
2026         struct digi_t new_digi;
2027         unsigned long flags;
2028
2029         if (!tty || tty->magic != TTY_MAGIC)
2030                 return -EFAULT;
2031
2032         un = tty->driver_data;
2033         if (!un || un->magic != DGNC_UNIT_MAGIC)
2034                 return -EFAULT;
2035
2036         ch = un->un_ch;
2037         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2038                 return -EFAULT;
2039
2040         bd = ch->ch_bd;
2041         if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2042                 return -EFAULT;
2043
2044         if (copy_from_user(&new_digi, new_info, sizeof(new_digi)))
2045                 return -EFAULT;
2046
2047         spin_lock_irqsave(&ch->ch_lock, flags);
2048
2049         /* Handle transitions to and from RTS Toggle. */
2050
2051         if (!(ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE) &&
2052             (new_digi.digi_flags & DIGI_RTS_TOGGLE))
2053                 ch->ch_mostat &= ~(UART_MCR_RTS);
2054         if ((ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE) &&
2055             !(new_digi.digi_flags & DIGI_RTS_TOGGLE))
2056                 ch->ch_mostat |= (UART_MCR_RTS);
2057
2058         /* Handle transitions to and from DTR Toggle. */
2059
2060         if (!(ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE) &&
2061             (new_digi.digi_flags & DIGI_DTR_TOGGLE))
2062                 ch->ch_mostat &= ~(UART_MCR_DTR);
2063         if ((ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE) &&
2064             !(new_digi.digi_flags & DIGI_DTR_TOGGLE))
2065                 ch->ch_mostat |= (UART_MCR_DTR);
2066
2067         memcpy(&ch->ch_digi, &new_digi, sizeof(new_digi));
2068
2069         if (ch->ch_digi.digi_maxcps < 1)
2070                 ch->ch_digi.digi_maxcps = 1;
2071
2072         if (ch->ch_digi.digi_maxcps > 10000)
2073                 ch->ch_digi.digi_maxcps = 10000;
2074
2075         if (ch->ch_digi.digi_bufsize < 10)
2076                 ch->ch_digi.digi_bufsize = 10;
2077
2078         if (ch->ch_digi.digi_maxchar < 1)
2079                 ch->ch_digi.digi_maxchar = 1;
2080
2081         if (ch->ch_digi.digi_maxchar > ch->ch_digi.digi_bufsize)
2082                 ch->ch_digi.digi_maxchar = ch->ch_digi.digi_bufsize;
2083
2084         if (ch->ch_digi.digi_onlen > DIGI_PLEN)
2085                 ch->ch_digi.digi_onlen = DIGI_PLEN;
2086
2087         if (ch->ch_digi.digi_offlen > DIGI_PLEN)
2088                 ch->ch_digi.digi_offlen = DIGI_PLEN;
2089
2090         ch->ch_bd->bd_ops->param(tty);
2091
2092         spin_unlock_irqrestore(&ch->ch_lock, flags);
2093
2094         return 0;
2095 }
2096
2097 /* dgnc_set_termios() */
2098
2099 static void dgnc_tty_set_termios(struct tty_struct *tty,
2100                                  struct ktermios *old_termios)
2101 {
2102         struct dgnc_board *bd;
2103         struct channel_t *ch;
2104         struct un_t *un;
2105         unsigned long flags;
2106
2107         if (!tty || tty->magic != TTY_MAGIC)
2108                 return;
2109
2110         un = tty->driver_data;
2111         if (!un || un->magic != DGNC_UNIT_MAGIC)
2112                 return;
2113
2114         ch = un->un_ch;
2115         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2116                 return;
2117
2118         bd = ch->ch_bd;
2119         if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2120                 return;
2121
2122         spin_lock_irqsave(&ch->ch_lock, flags);
2123
2124         ch->ch_c_cflag   = tty->termios.c_cflag;
2125         ch->ch_c_iflag   = tty->termios.c_iflag;
2126         ch->ch_c_oflag   = tty->termios.c_oflag;
2127         ch->ch_c_lflag   = tty->termios.c_lflag;
2128         ch->ch_startc = tty->termios.c_cc[VSTART];
2129         ch->ch_stopc  = tty->termios.c_cc[VSTOP];
2130
2131         ch->ch_bd->bd_ops->param(tty);
2132         dgnc_carrier(ch);
2133
2134         spin_unlock_irqrestore(&ch->ch_lock, flags);
2135 }
2136
2137 static void dgnc_tty_throttle(struct tty_struct *tty)
2138 {
2139         struct channel_t *ch;
2140         struct un_t *un;
2141         unsigned long flags;
2142
2143         if (!tty || tty->magic != TTY_MAGIC)
2144                 return;
2145
2146         un = tty->driver_data;
2147         if (!un || un->magic != DGNC_UNIT_MAGIC)
2148                 return;
2149
2150         ch = un->un_ch;
2151         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2152                 return;
2153
2154         spin_lock_irqsave(&ch->ch_lock, flags);
2155
2156         ch->ch_flags |= (CH_FORCED_STOPI);
2157
2158         spin_unlock_irqrestore(&ch->ch_lock, flags);
2159 }
2160
2161 static void dgnc_tty_unthrottle(struct tty_struct *tty)
2162 {
2163         struct channel_t *ch;
2164         struct un_t *un;
2165         unsigned long flags;
2166
2167         if (!tty || tty->magic != TTY_MAGIC)
2168                 return;
2169
2170         un = tty->driver_data;
2171         if (!un || un->magic != DGNC_UNIT_MAGIC)
2172                 return;
2173
2174         ch = un->un_ch;
2175         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2176                 return;
2177
2178         spin_lock_irqsave(&ch->ch_lock, flags);
2179
2180         ch->ch_flags &= ~(CH_FORCED_STOPI);
2181
2182         spin_unlock_irqrestore(&ch->ch_lock, flags);
2183 }
2184
2185 static void dgnc_tty_start(struct tty_struct *tty)
2186 {
2187         struct dgnc_board *bd;
2188         struct channel_t *ch;
2189         struct un_t *un;
2190         unsigned long flags;
2191
2192         if (!tty || tty->magic != TTY_MAGIC)
2193                 return;
2194
2195         un = tty->driver_data;
2196         if (!un || un->magic != DGNC_UNIT_MAGIC)
2197                 return;
2198
2199         ch = un->un_ch;
2200         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2201                 return;
2202
2203         bd = ch->ch_bd;
2204         if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2205                 return;
2206
2207         spin_lock_irqsave(&ch->ch_lock, flags);
2208
2209         ch->ch_flags &= ~(CH_FORCED_STOP);
2210
2211         spin_unlock_irqrestore(&ch->ch_lock, flags);
2212 }
2213
2214 static void dgnc_tty_stop(struct tty_struct *tty)
2215 {
2216         struct dgnc_board *bd;
2217         struct channel_t *ch;
2218         struct un_t *un;
2219         unsigned long flags;
2220
2221         if (!tty || tty->magic != TTY_MAGIC)
2222                 return;
2223
2224         un = tty->driver_data;
2225         if (!un || un->magic != DGNC_UNIT_MAGIC)
2226                 return;
2227
2228         ch = un->un_ch;
2229         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2230                 return;
2231
2232         bd = ch->ch_bd;
2233         if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2234                 return;
2235
2236         spin_lock_irqsave(&ch->ch_lock, flags);
2237
2238         ch->ch_flags |= (CH_FORCED_STOP);
2239
2240         spin_unlock_irqrestore(&ch->ch_lock, flags);
2241 }
2242
2243 /*
2244  * dgnc_tty_flush_chars()
2245  *
2246  * Flush the cook buffer
2247  *
2248  * Note to self, and any other poor souls who venture here:
2249  *
2250  * flush in this case DOES NOT mean dispose of the data.
2251  * instead, it means "stop buffering and send it if you
2252  * haven't already."  Just guess how I figured that out...   SRW 2-Jun-98
2253  *
2254  * It is also always called in interrupt context - JAR 8-Sept-99
2255  */
2256 static void dgnc_tty_flush_chars(struct tty_struct *tty)
2257 {
2258         struct dgnc_board *bd;
2259         struct channel_t *ch;
2260         struct un_t *un;
2261         unsigned long flags;
2262
2263         if (!tty || tty->magic != TTY_MAGIC)
2264                 return;
2265
2266         un = tty->driver_data;
2267         if (!un || un->magic != DGNC_UNIT_MAGIC)
2268                 return;
2269
2270         ch = un->un_ch;
2271         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2272                 return;
2273
2274         bd = ch->ch_bd;
2275         if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2276                 return;
2277
2278         spin_lock_irqsave(&ch->ch_lock, flags);
2279
2280         /* Do something maybe here */
2281
2282         spin_unlock_irqrestore(&ch->ch_lock, flags);
2283 }
2284
2285 /*
2286  * dgnc_tty_flush_buffer()
2287  *
2288  * Flush Tx buffer (make in == out)
2289  */
2290 static void dgnc_tty_flush_buffer(struct tty_struct *tty)
2291 {
2292         struct channel_t *ch;
2293         struct un_t *un;
2294         unsigned long flags;
2295
2296         if (!tty || tty->magic != TTY_MAGIC)
2297                 return;
2298
2299         un = tty->driver_data;
2300         if (!un || un->magic != DGNC_UNIT_MAGIC)
2301                 return;
2302
2303         ch = un->un_ch;
2304         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2305                 return;
2306
2307         spin_lock_irqsave(&ch->ch_lock, flags);
2308
2309         ch->ch_flags &= ~CH_STOP;
2310
2311         /* Flush our write queue */
2312         ch->ch_w_head = ch->ch_w_tail;
2313
2314         /* Flush UARTs transmit FIFO */
2315         ch->ch_bd->bd_ops->flush_uart_write(ch);
2316
2317         if (ch->ch_tun.un_flags & (UN_LOW | UN_EMPTY)) {
2318                 ch->ch_tun.un_flags &= ~(UN_LOW | UN_EMPTY);
2319                 wake_up_interruptible(&ch->ch_tun.un_flags_wait);
2320         }
2321         if (ch->ch_pun.un_flags & (UN_LOW | UN_EMPTY)) {
2322                 ch->ch_pun.un_flags &= ~(UN_LOW | UN_EMPTY);
2323                 wake_up_interruptible(&ch->ch_pun.un_flags_wait);
2324         }
2325
2326         spin_unlock_irqrestore(&ch->ch_lock, flags);
2327 }
2328
2329 /*
2330  * dgnc_wake_up_unit()
2331  *
2332  * Wakes up processes waiting in the unit's (teminal/printer) wait queue
2333  */
2334 static void dgnc_wake_up_unit(struct un_t *unit)
2335 {
2336         unit->un_flags &= ~(UN_LOW | UN_EMPTY);
2337         wake_up_interruptible(&unit->un_flags_wait);
2338 }
2339
2340 /* The IOCTL function and all of its helpers */
2341
2342 /*
2343  * dgnc_tty_ioctl()
2344  *
2345  * The usual assortment of ioctl's
2346  */
2347 static int dgnc_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
2348                           unsigned long arg)
2349 {
2350         struct dgnc_board *bd;
2351         struct board_ops *ch_bd_ops;
2352         struct channel_t *ch;
2353         struct un_t *un;
2354         int rc;
2355         unsigned long flags;
2356         void __user *uarg = (void __user *)arg;
2357
2358         if (!tty || tty->magic != TTY_MAGIC)
2359                 return -ENODEV;
2360
2361         un = tty->driver_data;
2362         if (!un || un->magic != DGNC_UNIT_MAGIC)
2363                 return -ENODEV;
2364
2365         ch = un->un_ch;
2366         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2367                 return -ENODEV;
2368
2369         bd = ch->ch_bd;
2370         if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2371                 return -ENODEV;
2372
2373         ch_bd_ops = bd->bd_ops;
2374
2375         spin_lock_irqsave(&ch->ch_lock, flags);
2376
2377         if (un->un_open_count <= 0) {
2378                 spin_unlock_irqrestore(&ch->ch_lock, flags);
2379                 return -EIO;
2380         }
2381
2382         switch (cmd) {
2383         /* Here are all the standard ioctl's that we MUST implement */
2384
2385         case TCSBRK:
2386                 /*
2387                  * TCSBRK is SVID version: non-zero arg --> no break
2388                  * this behaviour is exploited by tcdrain().
2389                  *
2390                  * According to POSIX.1 spec (7.2.2.1.2) breaks should be
2391                  * between 0.25 and 0.5 seconds so we'll ask for something
2392                  * in the middle: 0.375 seconds.
2393                  */
2394                 rc = tty_check_change(tty);
2395                 spin_unlock_irqrestore(&ch->ch_lock, flags);
2396                 if (rc)
2397                         return rc;
2398
2399                 rc = ch_bd_ops->drain(tty, 0);
2400
2401                 if (rc)
2402                         return -EINTR;
2403
2404                 spin_lock_irqsave(&ch->ch_lock, flags);
2405
2406                 if (((cmd == TCSBRK) && (!arg)) || (cmd == TCSBRKP))
2407                         ch_bd_ops->send_break(ch, 250);
2408
2409                 spin_unlock_irqrestore(&ch->ch_lock, flags);
2410
2411                 return 0;
2412
2413         case TCSBRKP:
2414                 /*
2415                  * support for POSIX tcsendbreak()
2416                  * According to POSIX.1 spec (7.2.2.1.2) breaks should be
2417                  * between 0.25 and 0.5 seconds so we'll ask for something
2418                  * in the middle: 0.375 seconds.
2419                  */
2420                 rc = tty_check_change(tty);
2421                 spin_unlock_irqrestore(&ch->ch_lock, flags);
2422                 if (rc)
2423                         return rc;
2424
2425                 rc = ch_bd_ops->drain(tty, 0);
2426                 if (rc)
2427                         return -EINTR;
2428
2429                 spin_lock_irqsave(&ch->ch_lock, flags);
2430
2431                 ch_bd_ops->send_break(ch, 250);
2432
2433                 spin_unlock_irqrestore(&ch->ch_lock, flags);
2434
2435                 return 0;
2436
2437         case TIOCSBRK:
2438                 rc = tty_check_change(tty);
2439                 spin_unlock_irqrestore(&ch->ch_lock, flags);
2440                 if (rc)
2441                         return rc;
2442
2443                 rc = ch_bd_ops->drain(tty, 0);
2444                 if (rc)
2445                         return -EINTR;
2446
2447                 spin_lock_irqsave(&ch->ch_lock, flags);
2448
2449                 ch_bd_ops->send_break(ch, 250);
2450
2451                 spin_unlock_irqrestore(&ch->ch_lock, flags);
2452
2453                 return 0;
2454
2455         case TIOCCBRK:
2456                 /* Do Nothing */
2457                 spin_unlock_irqrestore(&ch->ch_lock, flags);
2458                 return 0;
2459
2460         case TIOCGSOFTCAR:
2461
2462                 spin_unlock_irqrestore(&ch->ch_lock, flags);
2463
2464                 return put_user(C_CLOCAL(tty) ? 1 : 0,
2465                                 (unsigned long __user *)arg);
2466
2467         case TIOCSSOFTCAR:
2468
2469                 spin_unlock_irqrestore(&ch->ch_lock, flags);
2470                 rc = get_user(arg, (unsigned long __user *)arg);
2471                 if (rc)
2472                         return rc;
2473
2474                 spin_lock_irqsave(&ch->ch_lock, flags);
2475                 tty->termios.c_cflag = ((tty->termios.c_cflag & ~CLOCAL) |
2476                                        (arg ? CLOCAL : 0));
2477                 ch_bd_ops->param(tty);
2478                 spin_unlock_irqrestore(&ch->ch_lock, flags);
2479
2480                 return 0;
2481
2482         case TIOCMGET:
2483                 spin_unlock_irqrestore(&ch->ch_lock, flags);
2484                 return dgnc_get_modem_info(ch, uarg);
2485
2486         case TIOCMBIS:
2487         case TIOCMBIC:
2488         case TIOCMSET:
2489                 spin_unlock_irqrestore(&ch->ch_lock, flags);
2490                 return dgnc_set_modem_info(ch, cmd, uarg);
2491
2492                 /* Here are any additional ioctl's that we want to implement */
2493
2494         case TCFLSH:
2495                 /*
2496                  * The linux tty driver doesn't have a flush
2497                  * input routine for the driver, assuming all backed
2498                  * up data is in the line disc. buffers.  However,
2499                  * we all know that's not the case.  Here, we
2500                  * act on the ioctl, but then lie and say we didn't
2501                  * so the line discipline will process the flush
2502                  * also.
2503                  */
2504                 rc = tty_check_change(tty);
2505                 if (rc) {
2506                         spin_unlock_irqrestore(&ch->ch_lock, flags);
2507                         return rc;
2508                 }
2509
2510                 if ((arg == TCIFLUSH) || (arg == TCIOFLUSH)) {
2511                         ch->ch_r_head = ch->ch_r_tail;
2512                         ch_bd_ops->flush_uart_read(ch);
2513                         /* Force queue flow control to be released, if needed */
2514                         dgnc_check_queue_flow_control(ch);
2515                 }
2516
2517                 if ((arg == TCOFLUSH) || (arg == TCIOFLUSH)) {
2518                         if (!(un->un_type == DGNC_PRINT)) {
2519                                 ch->ch_w_head = ch->ch_w_tail;
2520                                 ch_bd_ops->flush_uart_write(ch);
2521
2522                                 if (ch->ch_tun.un_flags & (UN_LOW | UN_EMPTY))
2523                                         dgnc_wake_up_unit(&ch->ch_tun);
2524
2525                                 if (ch->ch_pun.un_flags & (UN_LOW | UN_EMPTY))
2526                                         dgnc_wake_up_unit(&ch->ch_pun);
2527                         }
2528                 }
2529
2530                 /* pretend we didn't recognize this IOCTL */
2531                 spin_unlock_irqrestore(&ch->ch_lock, flags);
2532                 return -ENOIOCTLCMD;
2533         case TCSETSF:
2534         case TCSETSW:
2535                 /*
2536                  * The linux tty driver doesn't have a flush
2537                  * input routine for the driver, assuming all backed
2538                  * up data is in the line disc. buffers.  However,
2539                  * we all know that's not the case.  Here, we
2540                  * act on the ioctl, but then lie and say we didn't
2541                  * so the line discipline will process the flush
2542                  * also.
2543                  */
2544                 if (cmd == TCSETSF) {
2545                         /* flush rx */
2546                         ch->ch_flags &= ~CH_STOP;
2547                         ch->ch_r_head = ch->ch_r_tail;
2548                         ch_bd_ops->flush_uart_read(ch);
2549                         /* Force queue flow control to be released, if needed */
2550                         dgnc_check_queue_flow_control(ch);
2551                 }
2552
2553                 /* now wait for all the output to drain */
2554                 spin_unlock_irqrestore(&ch->ch_lock, flags);
2555                 rc = ch_bd_ops->drain(tty, 0);
2556                 if (rc)
2557                         return -EINTR;
2558
2559                 /* pretend we didn't recognize this */
2560                 return -ENOIOCTLCMD;
2561
2562         case TCSETAW:
2563
2564                 spin_unlock_irqrestore(&ch->ch_lock, flags);
2565                 rc = ch_bd_ops->drain(tty, 0);
2566                 if (rc)
2567                         return -EINTR;
2568
2569                 /* pretend we didn't recognize this */
2570                 return -ENOIOCTLCMD;
2571
2572         case TCXONC:
2573                 spin_unlock_irqrestore(&ch->ch_lock, flags);
2574                 /* Make the ld do it */
2575                 return -ENOIOCTLCMD;
2576
2577         case DIGI_GETA:
2578                 /* get information for ditty */
2579                 spin_unlock_irqrestore(&ch->ch_lock, flags);
2580                 return dgnc_tty_digigeta(tty, uarg);
2581
2582         case DIGI_SETAW:
2583         case DIGI_SETAF:
2584
2585                 /* set information for ditty */
2586                 if (cmd == (DIGI_SETAW)) {
2587                         spin_unlock_irqrestore(&ch->ch_lock, flags);
2588                         rc = ch_bd_ops->drain(tty, 0);
2589
2590                         if (rc)
2591                                 return -EINTR;
2592
2593                         spin_lock_irqsave(&ch->ch_lock, flags);
2594                 } else {
2595                         tty_ldisc_flush(tty);
2596                 }
2597                 /* fall thru */
2598
2599         case DIGI_SETA:
2600                 spin_unlock_irqrestore(&ch->ch_lock, flags);
2601                 return dgnc_tty_digiseta(tty, uarg);
2602
2603         case DIGI_LOOPBACK:
2604                 {
2605                         uint loopback = 0;
2606                         /*
2607                          * Let go of locks when accessing user space,
2608                          * could sleep
2609                          */
2610                         spin_unlock_irqrestore(&ch->ch_lock, flags);
2611                         rc = get_user(loopback, (unsigned int __user *)arg);
2612                         if (rc)
2613                                 return rc;
2614                         spin_lock_irqsave(&ch->ch_lock, flags);
2615
2616                         /* Enable/disable internal loopback for this port */
2617                         if (loopback)
2618                                 ch->ch_flags |= CH_LOOPBACK;
2619                         else
2620                                 ch->ch_flags &= ~(CH_LOOPBACK);
2621
2622                         ch_bd_ops->param(tty);
2623                         spin_unlock_irqrestore(&ch->ch_lock, flags);
2624                         return 0;
2625                 }
2626
2627         case DIGI_GETCUSTOMBAUD:
2628                 spin_unlock_irqrestore(&ch->ch_lock, flags);
2629                 return put_user(ch->ch_custom_speed,
2630                                 (unsigned int __user *)arg);
2631
2632         case DIGI_SETCUSTOMBAUD:
2633         {
2634                 int new_rate;
2635                 /* Let go of locks when accessing user space, could sleep */
2636                 spin_unlock_irqrestore(&ch->ch_lock, flags);
2637                 rc = get_user(new_rate, (int __user *)arg);
2638                 if (rc)
2639                         return rc;
2640                 spin_lock_irqsave(&ch->ch_lock, flags);
2641                 dgnc_set_custom_speed(ch, new_rate);
2642                 ch_bd_ops->param(tty);
2643                 spin_unlock_irqrestore(&ch->ch_lock, flags);
2644                 return 0;
2645         }
2646
2647         /*
2648          * This ioctl allows insertion of a character into the front
2649          * of any pending data to be transmitted.
2650          *
2651          * This ioctl is to satisfy the "Send Character Immediate"
2652          * call that the RealPort protocol spec requires.
2653          */
2654         case DIGI_REALPORT_SENDIMMEDIATE:
2655         {
2656                 unsigned char c;
2657
2658                 spin_unlock_irqrestore(&ch->ch_lock, flags);
2659                 rc = get_user(c, (unsigned char __user *)arg);
2660                 if (rc)
2661                         return rc;
2662                 spin_lock_irqsave(&ch->ch_lock, flags);
2663                 ch_bd_ops->send_immediate_char(ch, c);
2664                 spin_unlock_irqrestore(&ch->ch_lock, flags);
2665                 return 0;
2666         }
2667
2668         /*
2669          * This ioctl returns all the current counts for the port.
2670          *
2671          * This ioctl is to satisfy the "Line Error Counters"
2672          * call that the RealPort protocol spec requires.
2673          */
2674         case DIGI_REALPORT_GETCOUNTERS:
2675         {
2676                 struct digi_getcounter buf;
2677
2678                 buf.norun = ch->ch_err_overrun;
2679                 buf.noflow = 0;         /* The driver doesn't keep this stat */
2680                 buf.nframe = ch->ch_err_frame;
2681                 buf.nparity = ch->ch_err_parity;
2682                 buf.nbreak = ch->ch_err_break;
2683                 buf.rbytes = ch->ch_rxcount;
2684                 buf.tbytes = ch->ch_txcount;
2685
2686                 spin_unlock_irqrestore(&ch->ch_lock, flags);
2687
2688                 if (copy_to_user(uarg, &buf, sizeof(buf)))
2689                         return -EFAULT;
2690
2691                 return 0;
2692         }
2693
2694         /*
2695          * This ioctl returns all current events.
2696          *
2697          * This ioctl is to satisfy the "Event Reporting"
2698          * call that the RealPort protocol spec requires.
2699          */
2700         case DIGI_REALPORT_GETEVENTS:
2701         {
2702                 unsigned int events = 0;
2703
2704                 /* NOTE: MORE EVENTS NEEDS TO BE ADDED HERE */
2705                 if (ch->ch_flags & CH_BREAK_SENDING)
2706                         events |= EV_TXB;
2707                 if ((ch->ch_flags & CH_STOP) ||
2708                     (ch->ch_flags & CH_FORCED_STOP))
2709                         events |= (EV_OPU | EV_OPS);
2710
2711                 if ((ch->ch_flags & CH_STOPI) ||
2712                     (ch->ch_flags & CH_FORCED_STOPI))
2713                         events |= (EV_IPU | EV_IPS);
2714
2715                 spin_unlock_irqrestore(&ch->ch_lock, flags);
2716                 return put_user(events, (unsigned int __user *)arg);
2717         }
2718
2719         /*
2720          * This ioctl returns TOUT and TIN counters based
2721          * upon the values passed in by the RealPort Server.
2722          * It also passes back whether the UART Transmitter is
2723          * empty as well.
2724          */
2725         case DIGI_REALPORT_GETBUFFERS:
2726         {
2727                 struct digi_getbuffer buf;
2728                 int tdist;
2729                 int count;
2730
2731                 spin_unlock_irqrestore(&ch->ch_lock, flags);
2732
2733                 /* Get data from user first. */
2734
2735                 if (copy_from_user(&buf, uarg, sizeof(buf)))
2736                         return -EFAULT;
2737
2738                 spin_lock_irqsave(&ch->ch_lock, flags);
2739
2740                 /* Figure out how much data is in our RX and TX queues. */
2741
2742                 buf.rxbuf = (ch->ch_r_head - ch->ch_r_tail) & RQUEUEMASK;
2743                 buf.txbuf = (ch->ch_w_head - ch->ch_w_tail) & WQUEUEMASK;
2744
2745                 /*
2746                  * Is the UART empty?
2747                  * Add that value to whats in our TX queue.
2748                  */
2749
2750                 count = buf.txbuf + ch_bd_ops->get_uart_bytes_left(ch);
2751
2752                 /*
2753                  * Figure out how much data the RealPort Server believes should
2754                  * be in our TX queue.
2755                  */
2756                 tdist = (buf.tx_in - buf.tx_out) & 0xffff;
2757
2758                 /*
2759                  * If we have more data than the RealPort Server believes we
2760                  * should have, reduce our count to its amount.
2761                  *
2762                  * This count difference CAN happen because the Linux LD can
2763                  * insert more characters into our queue for OPOST processing
2764                  * that the RealPort Server doesn't know about.
2765                  */
2766                 if (buf.txbuf > tdist)
2767                         buf.txbuf = tdist;
2768
2769                 /* Report whether our queue and UART TX are completely empty. */
2770
2771                 if (count)
2772                         buf.txdone = 0;
2773                 else
2774                         buf.txdone = 1;
2775
2776                 spin_unlock_irqrestore(&ch->ch_lock, flags);
2777
2778                 if (copy_to_user(uarg, &buf, sizeof(buf)))
2779                         return -EFAULT;
2780
2781                 return 0;
2782         }
2783         default:
2784                 spin_unlock_irqrestore(&ch->ch_lock, flags);
2785
2786                 return -ENOIOCTLCMD;
2787         }
2788 }