]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/dgnc/dgnc_tty.c
ffbe5a22169f5bef4ba95075cc3f1d29b3eb2e67
[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/signal.h> /* For jiffies, task states, etc. */
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 const 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         if (IS_ERR(brd->serial_driver))
147                 return PTR_ERR(brd->serial_driver);
148
149         snprintf(brd->serial_name, MAXTTYNAMELEN, "tty_dgnc_%d_",
150                  brd->boardnum);
151
152         brd->serial_driver->name = brd->serial_name;
153         brd->serial_driver->name_base = 0;
154         brd->serial_driver->major = 0;
155         brd->serial_driver->minor_start = 0;
156         brd->serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
157         brd->serial_driver->subtype = SERIAL_TYPE_NORMAL;
158         brd->serial_driver->init_termios = default_termios;
159         brd->serial_driver->driver_name = DRVSTR;
160
161         /*
162          * Entry points for driver.  Called by the kernel from
163          * tty_io.c and n_tty.c.
164          */
165         tty_set_operations(brd->serial_driver, &dgnc_tty_ops);
166
167         rc = tty_register_driver(brd->serial_driver);
168         if (rc < 0) {
169                 dev_dbg(&brd->pdev->dev,
170                         "Can't register tty device (%d)\n", rc);
171                 goto free_serial_driver;
172         }
173
174         /*
175          * If we're doing transparent print, we have to do all of the above
176          * again, separately so we don't get the LD confused about what major
177          * we are when we get into the dgnc_tty_open() routine.
178          */
179         brd->print_driver = tty_alloc_driver(brd->maxports,
180                                              TTY_DRIVER_REAL_RAW |
181                                              TTY_DRIVER_DYNAMIC_DEV |
182                                              TTY_DRIVER_HARDWARE_BREAK);
183         if (IS_ERR(brd->print_driver)) {
184                 rc = PTR_ERR(brd->print_driver);
185                 goto unregister_serial_driver;
186         }
187
188         snprintf(brd->print_name, MAXTTYNAMELEN, "pr_dgnc_%d_", brd->boardnum);
189
190         brd->print_driver->name = brd->print_name;
191         brd->print_driver->name_base = 0;
192         brd->print_driver->major = brd->serial_driver->major;
193         brd->print_driver->minor_start = 0x80;
194         brd->print_driver->type = TTY_DRIVER_TYPE_SERIAL;
195         brd->print_driver->subtype = SERIAL_TYPE_NORMAL;
196         brd->print_driver->init_termios = default_termios;
197         brd->print_driver->driver_name = DRVSTR;
198
199         /*
200          * Entry points for driver.  Called by the kernel from
201          * tty_io.c and n_tty.c.
202          */
203         tty_set_operations(brd->print_driver, &dgnc_tty_ops);
204
205         rc = tty_register_driver(brd->print_driver);
206         if (rc < 0) {
207                 dev_dbg(&brd->pdev->dev,
208                         "Can't register Transparent Print device(%d)\n",
209                         rc);
210                 goto free_print_driver;
211         }
212
213         return 0;
214
215 free_print_driver:
216         put_tty_driver(brd->print_driver);
217 unregister_serial_driver:
218         tty_unregister_driver(brd->serial_driver);
219 free_serial_driver:
220         put_tty_driver(brd->serial_driver);
221         return rc;
222 }
223
224 void dgnc_tty_unregister(struct dgnc_board *brd)
225 {
226         tty_unregister_driver(brd->print_driver);
227         tty_unregister_driver(brd->serial_driver);
228         put_tty_driver(brd->print_driver);
229         put_tty_driver(brd->serial_driver);
230 }
231
232 /*
233  * dgnc_tty_init()
234  *
235  * Init the tty subsystem.  Called once per board after board has been
236  * downloaded and init'ed.
237  */
238 int dgnc_tty_init(struct dgnc_board *brd)
239 {
240         int i;
241         int rc;
242         void __iomem *vaddr;
243         struct channel_t *ch;
244
245         if (!brd)
246                 return -ENXIO;
247
248         /* Initialize board structure elements. */
249
250         vaddr = brd->re_map_membase;
251
252         brd->nasync = brd->maxports;
253
254         for (i = 0; i < brd->nasync; i++) {
255                 /*
256                  * Okay to malloc with GFP_KERNEL, we are not at
257                  * interrupt context, and there are no locks held.
258                  */
259                 brd->channels[i] = kzalloc(sizeof(*brd->channels[i]),
260                                            GFP_KERNEL);
261                 if (!brd->channels[i]) {
262                         rc = -ENOMEM;
263                         goto err_free_channels;
264                 }
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 rc;
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         if (rc)
918                 return rc;
919
920         spin_lock_irqsave(&brd->bd_lock, flags);
921
922         /* If opened device is greater than our number of ports, bail. */
923         if (PORT_NUM(minor) >= brd->nasync) {
924                 rc = -ENXIO;
925                 goto err_brd_unlock;
926         }
927
928         ch = brd->channels[PORT_NUM(minor)];
929         if (!ch) {
930                 rc = -ENXIO;
931                 goto err_brd_unlock;
932         }
933
934         /* Drop board lock */
935         spin_unlock_irqrestore(&brd->bd_lock, flags);
936
937         /* Grab channel lock */
938         spin_lock_irqsave(&ch->ch_lock, flags);
939
940         /* Figure out our type */
941         if (!IS_PRINT(minor)) {
942                 un = &brd->channels[PORT_NUM(minor)]->ch_tun;
943                 un->un_type = DGNC_SERIAL;
944         } else if (IS_PRINT(minor)) {
945                 un = &brd->channels[PORT_NUM(minor)]->ch_pun;
946                 un->un_type = DGNC_PRINT;
947         } else {
948                 rc = -ENXIO;
949                 goto err_ch_unlock;
950         }
951
952         /*
953          * If the port is still in a previous open, and in a state
954          * where we simply cannot safely keep going, wait until the
955          * state clears.
956          */
957         spin_unlock_irqrestore(&ch->ch_lock, flags);
958
959         rc = wait_event_interruptible(ch->ch_flags_wait,
960                                       ((ch->ch_flags & CH_OPENING) == 0));
961         /* If ret is non-zero, user ctrl-c'ed us */
962         if (rc)
963                 return -EINTR;
964
965         /*
966          * If either unit is in the middle of the fragile part of close,
967          * we just cannot touch the channel safely.
968          * Go to sleep, knowing that when the channel can be
969          * touched safely, the close routine will signal the
970          * ch_flags_wait to wake us back up.
971          */
972         rc = wait_event_interruptible(
973                                 ch->ch_flags_wait,
974                                 (((ch->ch_tun.un_flags |
975                                 ch->ch_pun.un_flags) & UN_CLOSING) == 0));
976         /* If ret is non-zero, user ctrl-c'ed us */
977         if (rc)
978                 return -EINTR;
979
980         spin_lock_irqsave(&ch->ch_lock, flags);
981
982         /* Store our unit into driver_data, so we always have it available. */
983         tty->driver_data = un;
984
985         /* Initialize tty's */
986
987         if (!(un->un_flags & UN_ISOPEN)) {
988                 /* Store important variables. */
989                 un->un_tty     = tty;
990
991                 /* Maybe do something here to the TTY struct as well? */
992         }
993
994         /*
995          * Allocate channel buffers for read/write/error.
996          * Set flag, so we don't get trounced on.
997          */
998         ch->ch_flags |= (CH_OPENING);
999
1000         /* Drop locks, as malloc with GFP_KERNEL can sleep */
1001         spin_unlock_irqrestore(&ch->ch_lock, flags);
1002
1003         if (!ch->ch_rqueue)
1004                 ch->ch_rqueue = kzalloc(RQUEUESIZE, GFP_KERNEL);
1005         if (!ch->ch_equeue)
1006                 ch->ch_equeue = kzalloc(EQUEUESIZE, GFP_KERNEL);
1007         if (!ch->ch_wqueue)
1008                 ch->ch_wqueue = kzalloc(WQUEUESIZE, GFP_KERNEL);
1009
1010         if (!ch->ch_rqueue || !ch->ch_equeue || !ch->ch_wqueue) {
1011                 kfree(ch->ch_rqueue);
1012                 kfree(ch->ch_equeue);
1013                 kfree(ch->ch_wqueue);
1014                 return -ENOMEM;
1015         }
1016
1017         spin_lock_irqsave(&ch->ch_lock, flags);
1018
1019         ch->ch_flags &= ~(CH_OPENING);
1020         wake_up_interruptible(&ch->ch_flags_wait);
1021
1022         /* Initialize if neither terminal or printer is open. */
1023
1024         if (!((ch->ch_tun.un_flags | ch->ch_pun.un_flags) & UN_ISOPEN)) {
1025                 /* Flush input queues. */
1026                 ch->ch_r_head = 0;
1027                 ch->ch_r_tail = 0;
1028                 ch->ch_e_head = 0;
1029                 ch->ch_e_tail = 0;
1030                 ch->ch_w_head = 0;
1031                 ch->ch_w_tail = 0;
1032
1033                 brd->bd_ops->flush_uart_write(ch);
1034                 brd->bd_ops->flush_uart_read(ch);
1035
1036                 ch->ch_flags = 0;
1037                 ch->ch_cached_lsr = 0;
1038                 ch->ch_stop_sending_break = 0;
1039                 ch->ch_stops_sent = 0;
1040
1041                 ch->ch_c_cflag   = tty->termios.c_cflag;
1042                 ch->ch_c_iflag   = tty->termios.c_iflag;
1043                 ch->ch_c_oflag   = tty->termios.c_oflag;
1044                 ch->ch_c_lflag   = tty->termios.c_lflag;
1045                 ch->ch_startc = tty->termios.c_cc[VSTART];
1046                 ch->ch_stopc  = tty->termios.c_cc[VSTOP];
1047
1048                 /*
1049                  * Bring up RTS and DTR...
1050                  * Also handle RTS or DTR toggle if set.
1051                  */
1052                 if (!(ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE))
1053                         ch->ch_mostat |= (UART_MCR_RTS);
1054                 if (!(ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE))
1055                         ch->ch_mostat |= (UART_MCR_DTR);
1056
1057                 /* Tell UART to init itself */
1058                 brd->bd_ops->uart_init(ch);
1059         }
1060
1061         /* Run param in case we changed anything */
1062
1063         brd->bd_ops->param(tty);
1064
1065         dgnc_carrier(ch);
1066
1067         /* follow protocol for opening port */
1068
1069         spin_unlock_irqrestore(&ch->ch_lock, flags);
1070
1071         rc = dgnc_block_til_ready(tty, file, ch);
1072
1073         /* No going back now, increment our unit and channel counters */
1074         spin_lock_irqsave(&ch->ch_lock, flags);
1075         ch->ch_open_count++;
1076         un->un_open_count++;
1077         un->un_flags |= (UN_ISOPEN);
1078         spin_unlock_irqrestore(&ch->ch_lock, flags);
1079
1080         return rc;
1081
1082 err_brd_unlock:
1083         spin_unlock_irqrestore(&brd->bd_lock, flags);
1084         return rc;
1085 err_ch_unlock:
1086         spin_unlock_irqrestore(&ch->ch_lock, flags);
1087         return rc;
1088
1089 }
1090
1091 /*
1092  * dgnc_block_til_ready()
1093  *
1094  * Wait for DCD, if needed.
1095  */
1096 static int dgnc_block_til_ready(struct tty_struct *tty,
1097                                 struct file *file,
1098                                 struct channel_t *ch)
1099 {
1100         int rc = 0;
1101         struct un_t *un = tty->driver_data;
1102         unsigned long flags;
1103         uint    old_flags = 0;
1104         int     sleep_on_un_flags = 0;
1105
1106         if (!file)
1107                 return -ENXIO;
1108
1109         spin_lock_irqsave(&ch->ch_lock, flags);
1110
1111         ch->ch_wopen++;
1112
1113         /* Loop forever */
1114         while (1) {
1115                 sleep_on_un_flags = 0;
1116
1117                 /*
1118                  * If board has failed somehow during our sleep,
1119                  * bail with error.
1120                  */
1121                 if (ch->ch_bd->state == BOARD_FAILED) {
1122                         rc = -ENXIO;
1123                         break;
1124                 }
1125
1126                 /* If tty was hung up, break out of loop and set error. */
1127                 if (tty_hung_up_p(file)) {
1128                         rc = -EAGAIN;
1129                         break;
1130                 }
1131
1132                 /*
1133                  * If either unit is in the middle of the fragile part of close,
1134                  * we just cannot touch the channel safely.
1135                  * Go back to sleep, knowing that when the channel can be
1136                  * touched safely, the close routine will signal the
1137                  * ch_wait_flags to wake us back up.
1138                  */
1139                 if (!((ch->ch_tun.un_flags |
1140                     ch->ch_pun.un_flags) &
1141                     UN_CLOSING)) {
1142                         /*
1143                          * Our conditions to leave cleanly and happily:
1144                          * 1) NONBLOCKING on the tty is set.
1145                          * 2) CLOCAL is set.
1146                          * 3) DCD (fake or real) is active.
1147                          */
1148
1149                         if (file->f_flags & O_NONBLOCK)
1150                                 break;
1151
1152                         if (tty_io_error(tty)) {
1153                                 rc = -EIO;
1154                                 break;
1155                         }
1156
1157                         if (ch->ch_flags & CH_CD)
1158                                 break;
1159
1160                         if (ch->ch_flags & CH_FCAR)
1161                                 break;
1162                 } else {
1163                         sleep_on_un_flags = 1;
1164                 }
1165
1166                 /*
1167                  * If there is a signal pending, the user probably
1168                  * interrupted (ctrl-c) us.
1169                  * Leave loop with error set.
1170                  */
1171                 if (signal_pending(current)) {
1172                         rc = -ERESTARTSYS;
1173                         break;
1174                 }
1175
1176                 /* Store the flags before we let go of channel lock */
1177
1178                 if (sleep_on_un_flags)
1179                         old_flags = ch->ch_tun.un_flags | ch->ch_pun.un_flags;
1180                 else
1181                         old_flags = ch->ch_flags;
1182
1183                 /*
1184                  * Let go of channel lock before calling schedule.
1185                  * Our poller will get any FEP events and wake us up when DCD
1186                  * eventually goes active.
1187                  */
1188
1189                 spin_unlock_irqrestore(&ch->ch_lock, flags);
1190
1191                 /*
1192                  * Wait for something in the flags to change
1193                  * from the current value.
1194                  */
1195                 if (sleep_on_un_flags)
1196                         rc = wait_event_interruptible
1197                                 (un->un_flags_wait,
1198                                  (old_flags != (ch->ch_tun.un_flags |
1199                                                 ch->ch_pun.un_flags)));
1200                 else
1201                         rc = wait_event_interruptible(
1202                                         ch->ch_flags_wait,
1203                                         (old_flags != ch->ch_flags));
1204
1205                 /*
1206                  * We got woken up for some reason.
1207                  * Before looping around, grab our channel lock.
1208                  */
1209                 spin_lock_irqsave(&ch->ch_lock, flags);
1210         }
1211
1212         ch->ch_wopen--;
1213
1214         spin_unlock_irqrestore(&ch->ch_lock, flags);
1215         return rc;
1216 }
1217
1218 /*
1219  * dgnc_tty_hangup()
1220  *
1221  * Hangup the port.  Like a close, but don't wait for output to drain.
1222  */
1223 static void dgnc_tty_hangup(struct tty_struct *tty)
1224 {
1225         if (!tty || tty->magic != TTY_MAGIC)
1226                 return;
1227
1228         /* flush the transmit queues */
1229         dgnc_tty_flush_buffer(tty);
1230 }
1231
1232 /* dgnc_tty_close() */
1233
1234 static void dgnc_tty_close(struct tty_struct *tty, struct file *file)
1235 {
1236         struct dgnc_board *bd;
1237         struct channel_t *ch;
1238         struct un_t *un;
1239         unsigned long flags;
1240
1241         if (!tty || tty->magic != TTY_MAGIC)
1242                 return;
1243
1244         un = tty->driver_data;
1245         if (!un || un->magic != DGNC_UNIT_MAGIC)
1246                 return;
1247
1248         ch = un->un_ch;
1249         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1250                 return;
1251
1252         bd = ch->ch_bd;
1253         if (!bd || bd->magic != DGNC_BOARD_MAGIC)
1254                 return;
1255
1256         spin_lock_irqsave(&ch->ch_lock, flags);
1257
1258         /*
1259          * Determine if this is the last close or not - and if we agree about
1260          * which type of close it is with the Line Discipline
1261          */
1262         if ((tty->count == 1) && (un->un_open_count != 1)) {
1263                 /*
1264                  * Uh, oh.  tty->count is 1, which means that the tty
1265                  * structure will be freed.  un_open_count should always
1266                  * be one in these conditions.  If it's greater than
1267                  * one, we've got real problems, since it means the
1268                  * serial port won't be shutdown.
1269                  */
1270                 dev_dbg(tty->dev,
1271                         "tty->count is 1, un open count is %d\n",
1272                         un->un_open_count);
1273                 un->un_open_count = 1;
1274         }
1275
1276         if (un->un_open_count)
1277                 un->un_open_count--;
1278         else
1279                 dev_dbg(tty->dev,
1280                         "bad serial port open count of %d\n",
1281                         un->un_open_count);
1282
1283         ch->ch_open_count--;
1284
1285         if (ch->ch_open_count && un->un_open_count) {
1286                 spin_unlock_irqrestore(&ch->ch_lock, flags);
1287                 return;
1288         }
1289
1290         /* OK, its the last close on the unit */
1291         un->un_flags |= UN_CLOSING;
1292
1293         tty->closing = 1;
1294
1295         /*
1296          * Only officially close channel if count is 0 and
1297          * DIGI_PRINTER bit is not set.
1298          */
1299         if ((ch->ch_open_count == 0) &&
1300             !(ch->ch_digi.digi_flags & DIGI_PRINTER)) {
1301                 ch->ch_flags &= ~(CH_STOPI | CH_FORCED_STOPI);
1302
1303                 /* turn off print device when closing print device. */
1304
1305                 if ((un->un_type == DGNC_PRINT) && (ch->ch_flags & CH_PRON)) {
1306                         dgnc_wmove(ch, ch->ch_digi.digi_offstr,
1307                                    (int)ch->ch_digi.digi_offlen);
1308                         ch->ch_flags &= ~CH_PRON;
1309                 }
1310
1311                 spin_unlock_irqrestore(&ch->ch_lock, flags);
1312                 /* wait for output to drain */
1313                 /* This will also return if we take an interrupt */
1314
1315                 bd->bd_ops->drain(tty, 0);
1316
1317                 dgnc_tty_flush_buffer(tty);
1318                 tty_ldisc_flush(tty);
1319
1320                 spin_lock_irqsave(&ch->ch_lock, flags);
1321
1322                 tty->closing = 0;
1323
1324                 /* If we have HUPCL set, lower DTR and RTS */
1325
1326                 if (ch->ch_c_cflag & HUPCL) {
1327                         /* Drop RTS/DTR */
1328                         ch->ch_mostat &= ~(UART_MCR_DTR | UART_MCR_RTS);
1329                         bd->bd_ops->assert_modem_signals(ch);
1330
1331                         /*
1332                          * Go to sleep to ensure RTS/DTR
1333                          * have been dropped for modems to see it.
1334                          */
1335                         if (ch->ch_close_delay) {
1336                                 spin_unlock_irqrestore(&ch->ch_lock,
1337                                                        flags);
1338                                 dgnc_ms_sleep(ch->ch_close_delay);
1339                                 spin_lock_irqsave(&ch->ch_lock, flags);
1340                         }
1341                 }
1342
1343                 ch->ch_old_baud = 0;
1344
1345                 /* Turn off UART interrupts for this port */
1346                 ch->ch_bd->bd_ops->uart_off(ch);
1347         } else {
1348                 /* turn off print device when closing print device. */
1349
1350                 if ((un->un_type == DGNC_PRINT) && (ch->ch_flags & CH_PRON)) {
1351                         dgnc_wmove(ch, ch->ch_digi.digi_offstr,
1352                                    (int)ch->ch_digi.digi_offlen);
1353                         ch->ch_flags &= ~CH_PRON;
1354                 }
1355         }
1356
1357         un->un_tty = NULL;
1358         un->un_flags &= ~(UN_ISOPEN | UN_CLOSING);
1359
1360         wake_up_interruptible(&ch->ch_flags_wait);
1361         wake_up_interruptible(&un->un_flags_wait);
1362
1363         spin_unlock_irqrestore(&ch->ch_lock, flags);
1364 }
1365
1366 /*
1367  * dgnc_tty_chars_in_buffer()
1368  *
1369  * Return number of characters that have not been transmitted yet.
1370  *
1371  * This routine is used by the line discipline to determine if there
1372  * is data waiting to be transmitted/drained/flushed or not.
1373  */
1374 static int dgnc_tty_chars_in_buffer(struct tty_struct *tty)
1375 {
1376         struct channel_t *ch = NULL;
1377         struct un_t *un = NULL;
1378         ushort thead;
1379         ushort ttail;
1380         uint tmask;
1381         uint chars;
1382         unsigned long flags;
1383
1384         if (!tty)
1385                 return 0;
1386
1387         un = tty->driver_data;
1388         if (!un || un->magic != DGNC_UNIT_MAGIC)
1389                 return 0;
1390
1391         ch = un->un_ch;
1392         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1393                 return 0;
1394
1395         spin_lock_irqsave(&ch->ch_lock, flags);
1396
1397         tmask = WQUEUEMASK;
1398         thead = ch->ch_w_head & tmask;
1399         ttail = ch->ch_w_tail & tmask;
1400
1401         spin_unlock_irqrestore(&ch->ch_lock, flags);
1402
1403         if (ttail == thead)
1404                 chars = 0;
1405         else if (thead > ttail)
1406                 chars = thead - ttail;
1407         else
1408                 chars = thead - ttail + WQUEUESIZE;
1409
1410         return (int)chars;
1411 }
1412
1413 /*
1414  * dgnc_maxcps_room
1415  *
1416  * Reduces bytes_available to the max number of characters
1417  * that can be sent currently given the maxcps value, and
1418  * returns the new bytes_available.  This only affects printer
1419  * output.
1420  */
1421 static int dgnc_maxcps_room(struct channel_t *ch, int bytes_available)
1422 {
1423         int rc = bytes_available;
1424
1425         if (ch->ch_digi.digi_maxcps > 0 && ch->ch_digi.digi_bufsize > 0) {
1426                 int cps_limit = 0;
1427                 unsigned long current_time = jiffies;
1428                 unsigned long buffer_time = current_time +
1429                         (HZ * ch->ch_digi.digi_bufsize) /
1430                         ch->ch_digi.digi_maxcps;
1431
1432                 if (ch->ch_cpstime < current_time) {
1433                         /* buffer is empty */
1434                         ch->ch_cpstime = current_time;  /* reset ch_cpstime */
1435                         cps_limit = ch->ch_digi.digi_bufsize;
1436                 } else if (ch->ch_cpstime < buffer_time) {
1437                         /* still room in the buffer */
1438                         cps_limit = ((buffer_time - ch->ch_cpstime) *
1439                                         ch->ch_digi.digi_maxcps) / HZ;
1440                 } else {
1441                         /* no room in the buffer */
1442                         cps_limit = 0;
1443                 }
1444
1445                 rc = min(cps_limit, bytes_available);
1446         }
1447
1448         return rc;
1449 }
1450
1451 /*
1452  * dgnc_tty_write_room()
1453  *
1454  * Return room available in Tx buffer
1455  */
1456 static int dgnc_tty_write_room(struct tty_struct *tty)
1457 {
1458         struct channel_t *ch = NULL;
1459         struct un_t *un = NULL;
1460         ushort head;
1461         ushort tail;
1462         ushort tmask;
1463         int room = 0;
1464         unsigned long flags;
1465
1466         if (!tty)
1467                 return 0;
1468
1469         un = tty->driver_data;
1470         if (!un || un->magic != DGNC_UNIT_MAGIC)
1471                 return 0;
1472
1473         ch = un->un_ch;
1474         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1475                 return 0;
1476
1477         spin_lock_irqsave(&ch->ch_lock, flags);
1478
1479         tmask = WQUEUEMASK;
1480         head = (ch->ch_w_head) & tmask;
1481         tail = (ch->ch_w_tail) & tmask;
1482
1483         room = tail - head - 1;
1484         if (room < 0)
1485                 room += WQUEUESIZE;
1486
1487         /* Limit printer to maxcps */
1488         if (un->un_type != DGNC_PRINT)
1489                 room = dgnc_maxcps_room(ch, room);
1490
1491         /*
1492          * If we are printer device, leave room for
1493          * possibly both the on and off strings.
1494          */
1495         if (un->un_type == DGNC_PRINT) {
1496                 if (!(ch->ch_flags & CH_PRON))
1497                         room -= ch->ch_digi.digi_onlen;
1498                 room -= ch->ch_digi.digi_offlen;
1499         } else {
1500                 if (ch->ch_flags & CH_PRON)
1501                         room -= ch->ch_digi.digi_offlen;
1502         }
1503
1504         if (room < 0)
1505                 room = 0;
1506
1507         spin_unlock_irqrestore(&ch->ch_lock, flags);
1508         return room;
1509 }
1510
1511 /*
1512  * dgnc_tty_put_char()
1513  *
1514  * Put a character into ch->ch_buf
1515  *
1516  *      - used by the line discipline for OPOST processing
1517  */
1518 static int dgnc_tty_put_char(struct tty_struct *tty, unsigned char c)
1519 {
1520         /* Simply call tty_write. */
1521
1522         dgnc_tty_write(tty, &c, 1);
1523         return 1;
1524 }
1525
1526 /*
1527  * dgnc_tty_write()
1528  *
1529  * Take data from the user or kernel and send it out to the FEP.
1530  * In here exists all the Transparent Print magic as well.
1531  */
1532 static int dgnc_tty_write(struct tty_struct *tty,
1533                           const unsigned char *buf, int count)
1534 {
1535         struct channel_t *ch = NULL;
1536         struct un_t *un = NULL;
1537         int bufcount = 0, n = 0;
1538         unsigned long flags;
1539         ushort head;
1540         ushort tail;
1541         ushort tmask;
1542         uint remain;
1543
1544         if (!tty)
1545                 return 0;
1546
1547         un = tty->driver_data;
1548         if (!un || un->magic != DGNC_UNIT_MAGIC)
1549                 return 0;
1550
1551         ch = un->un_ch;
1552         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1553                 return 0;
1554
1555         if (!count)
1556                 return 0;
1557
1558         /*
1559          * Store original amount of characters passed in.
1560          * This helps to figure out if we should ask the FEP
1561          * to send us an event when it has more space available.
1562          */
1563
1564         spin_lock_irqsave(&ch->ch_lock, flags);
1565
1566         /* Get our space available for the channel from the board */
1567         tmask = WQUEUEMASK;
1568         head = (ch->ch_w_head) & tmask;
1569         tail = (ch->ch_w_tail) & tmask;
1570
1571         bufcount = tail - head - 1;
1572         if (bufcount < 0)
1573                 bufcount += WQUEUESIZE;
1574
1575         /*
1576          * Limit printer output to maxcps overall, with bursts allowed
1577          * up to bufsize characters.
1578          */
1579         if (un->un_type != DGNC_PRINT)
1580                 bufcount = dgnc_maxcps_room(ch, bufcount);
1581
1582         /*
1583          * Take minimum of what the user wants to send, and the
1584          * space available in the FEP buffer.
1585          */
1586         count = min(count, bufcount);
1587
1588         /* Bail if no space left. */
1589
1590         if (count <= 0)
1591                 goto exit_retry;
1592
1593         /*
1594          * Output the printer ON string, if we are in terminal mode, but
1595          * need to be in printer mode.
1596          */
1597         if ((un->un_type == DGNC_PRINT) && !(ch->ch_flags & CH_PRON)) {
1598                 dgnc_wmove(ch, ch->ch_digi.digi_onstr,
1599                            (int)ch->ch_digi.digi_onlen);
1600                 head = (ch->ch_w_head) & tmask;
1601                 ch->ch_flags |= CH_PRON;
1602         }
1603
1604         /*
1605          * On the other hand, output the printer OFF string, if we are
1606          * currently in printer mode, but need to output to the terminal.
1607          */
1608         if ((un->un_type != DGNC_PRINT) && (ch->ch_flags & CH_PRON)) {
1609                 dgnc_wmove(ch, ch->ch_digi.digi_offstr,
1610                            (int)ch->ch_digi.digi_offlen);
1611                 head = (ch->ch_w_head) & tmask;
1612                 ch->ch_flags &= ~CH_PRON;
1613         }
1614
1615         n = count;
1616
1617         /*
1618          * If the write wraps over the top of the circular buffer,
1619          * move the portion up to the wrap point, and reset the
1620          * pointers to the bottom.
1621          */
1622         remain = WQUEUESIZE - head;
1623
1624         if (n >= remain) {
1625                 n -= remain;
1626                 memcpy(ch->ch_wqueue + head, buf, remain);
1627                 head = 0;
1628                 buf += remain;
1629         }
1630
1631         if (n > 0) {
1632                 /* Move rest of data. */
1633                 remain = n;
1634                 memcpy(ch->ch_wqueue + head, buf, remain);
1635                 head += remain;
1636         }
1637
1638         if (count) {
1639                 head &= tmask;
1640                 ch->ch_w_head = head;
1641         }
1642
1643         /* Update printer buffer empty time. */
1644         if ((un->un_type == DGNC_PRINT) && (ch->ch_digi.digi_maxcps > 0) &&
1645             (ch->ch_digi.digi_bufsize > 0)) {
1646                 ch->ch_cpstime += (HZ * count) / ch->ch_digi.digi_maxcps;
1647         }
1648
1649         spin_unlock_irqrestore(&ch->ch_lock, flags);
1650
1651         if (count) {
1652                 /*
1653                  * Channel lock is grabbed and then released
1654                  * inside this routine.
1655                  */
1656                 ch->ch_bd->bd_ops->copy_data_from_queue_to_uart(ch);
1657         }
1658
1659         return count;
1660
1661 exit_retry:
1662         spin_unlock_irqrestore(&ch->ch_lock, flags);
1663         return 0;
1664 }
1665
1666 /* Return modem signals to ld. */
1667
1668 static int dgnc_tty_tiocmget(struct tty_struct *tty)
1669 {
1670         struct channel_t *ch;
1671         struct un_t *un;
1672         int rc = -EIO;
1673         unsigned char mstat = 0;
1674         unsigned long flags;
1675
1676         if (!tty || tty->magic != TTY_MAGIC)
1677                 return rc;
1678
1679         un = tty->driver_data;
1680         if (!un || un->magic != DGNC_UNIT_MAGIC)
1681                 return rc;
1682
1683         ch = un->un_ch;
1684         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1685                 return rc;
1686
1687         spin_lock_irqsave(&ch->ch_lock, flags);
1688
1689         mstat = ch->ch_mostat | ch->ch_mistat;
1690
1691         spin_unlock_irqrestore(&ch->ch_lock, flags);
1692
1693         rc = 0;
1694
1695         if (mstat & UART_MCR_DTR)
1696                 rc |= TIOCM_DTR;
1697         if (mstat & UART_MCR_RTS)
1698                 rc |= TIOCM_RTS;
1699         if (mstat & UART_MSR_CTS)
1700                 rc |= TIOCM_CTS;
1701         if (mstat & UART_MSR_DSR)
1702                 rc |= TIOCM_DSR;
1703         if (mstat & UART_MSR_RI)
1704                 rc |= TIOCM_RI;
1705         if (mstat & UART_MSR_DCD)
1706                 rc |= TIOCM_CD;
1707
1708         return rc;
1709 }
1710
1711 /*
1712  * dgnc_tty_tiocmset()
1713  *
1714  * Set modem signals, called by ld.
1715  */
1716
1717 static int dgnc_tty_tiocmset(struct tty_struct *tty,
1718                              unsigned int set, unsigned int clear)
1719 {
1720         struct dgnc_board *bd;
1721         struct channel_t *ch;
1722         struct un_t *un;
1723         int rc = -EIO;
1724         unsigned long flags;
1725
1726         if (!tty || tty->magic != TTY_MAGIC)
1727                 return rc;
1728
1729         un = tty->driver_data;
1730         if (!un || un->magic != DGNC_UNIT_MAGIC)
1731                 return rc;
1732
1733         ch = un->un_ch;
1734         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1735                 return rc;
1736
1737         bd = ch->ch_bd;
1738         if (!bd || bd->magic != DGNC_BOARD_MAGIC)
1739                 return rc;
1740
1741         spin_lock_irqsave(&ch->ch_lock, flags);
1742
1743         if (set & TIOCM_RTS)
1744                 ch->ch_mostat |= UART_MCR_RTS;
1745
1746         if (set & TIOCM_DTR)
1747                 ch->ch_mostat |= UART_MCR_DTR;
1748
1749         if (clear & TIOCM_RTS)
1750                 ch->ch_mostat &= ~(UART_MCR_RTS);
1751
1752         if (clear & TIOCM_DTR)
1753                 ch->ch_mostat &= ~(UART_MCR_DTR);
1754
1755         ch->ch_bd->bd_ops->assert_modem_signals(ch);
1756
1757         spin_unlock_irqrestore(&ch->ch_lock, flags);
1758
1759         return 0;
1760 }
1761
1762 /*
1763  * dgnc_tty_send_break()
1764  *
1765  * Send a Break, called by ld.
1766  */
1767 static int dgnc_tty_send_break(struct tty_struct *tty, int msec)
1768 {
1769         struct dgnc_board *bd;
1770         struct channel_t *ch;
1771         struct un_t *un;
1772         int rc = -EIO;
1773         unsigned long flags;
1774
1775         if (!tty || tty->magic != TTY_MAGIC)
1776                 return rc;
1777
1778         un = tty->driver_data;
1779         if (!un || un->magic != DGNC_UNIT_MAGIC)
1780                 return rc;
1781
1782         ch = un->un_ch;
1783         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1784                 return rc;
1785
1786         bd = ch->ch_bd;
1787         if (!bd || bd->magic != DGNC_BOARD_MAGIC)
1788                 return rc;
1789
1790         switch (msec) {
1791         case -1:
1792                 msec = 0xFFFF;
1793                 break;
1794         case 0:
1795                 msec = 0;
1796                 break;
1797         default:
1798                 break;
1799         }
1800
1801         spin_lock_irqsave(&ch->ch_lock, flags);
1802
1803         ch->ch_bd->bd_ops->send_break(ch, msec);
1804
1805         spin_unlock_irqrestore(&ch->ch_lock, flags);
1806
1807         return 0;
1808 }
1809
1810 /*
1811  * dgnc_tty_wait_until_sent()
1812  *
1813  * wait until data has been transmitted, called by ld.
1814  */
1815 static void dgnc_tty_wait_until_sent(struct tty_struct *tty, int timeout)
1816 {
1817         struct dgnc_board *bd;
1818         struct channel_t *ch;
1819         struct un_t *un;
1820
1821         if (!tty || tty->magic != TTY_MAGIC)
1822                 return;
1823
1824         un = tty->driver_data;
1825         if (!un || un->magic != DGNC_UNIT_MAGIC)
1826                 return;
1827
1828         ch = un->un_ch;
1829         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1830                 return;
1831
1832         bd = ch->ch_bd;
1833         if (!bd || bd->magic != DGNC_BOARD_MAGIC)
1834                 return;
1835
1836         bd->bd_ops->drain(tty, 0);
1837 }
1838
1839 /*
1840  * dgnc_send_xchar()
1841  *
1842  * send a high priority character, called by ld.
1843  */
1844 static void dgnc_tty_send_xchar(struct tty_struct *tty, char c)
1845 {
1846         struct dgnc_board *bd;
1847         struct channel_t *ch;
1848         struct un_t *un;
1849         unsigned long flags;
1850
1851         if (!tty || tty->magic != TTY_MAGIC)
1852                 return;
1853
1854         un = tty->driver_data;
1855         if (!un || un->magic != DGNC_UNIT_MAGIC)
1856                 return;
1857
1858         ch = un->un_ch;
1859         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1860                 return;
1861
1862         bd = ch->ch_bd;
1863         if (!bd || bd->magic != DGNC_BOARD_MAGIC)
1864                 return;
1865
1866         dev_dbg(tty->dev, "dgnc_tty_send_xchar start\n");
1867
1868         spin_lock_irqsave(&ch->ch_lock, flags);
1869         bd->bd_ops->send_immediate_char(ch, c);
1870         spin_unlock_irqrestore(&ch->ch_lock, flags);
1871
1872         dev_dbg(tty->dev, "dgnc_tty_send_xchar finish\n");
1873 }
1874
1875 /* Return modem signals to ld. */
1876
1877 static inline int dgnc_get_mstat(struct channel_t *ch)
1878 {
1879         unsigned char mstat;
1880         int rc = -ENXIO;
1881         unsigned long flags;
1882
1883         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1884                 return rc;
1885
1886         spin_lock_irqsave(&ch->ch_lock, flags);
1887
1888         mstat = ch->ch_mostat | ch->ch_mistat;
1889
1890         spin_unlock_irqrestore(&ch->ch_lock, flags);
1891
1892         rc = 0;
1893
1894         if (mstat & UART_MCR_DTR)
1895                 rc |= TIOCM_DTR;
1896         if (mstat & UART_MCR_RTS)
1897                 rc |= TIOCM_RTS;
1898         if (mstat & UART_MSR_CTS)
1899                 rc |= TIOCM_CTS;
1900         if (mstat & UART_MSR_DSR)
1901                 rc |= TIOCM_DSR;
1902         if (mstat & UART_MSR_RI)
1903                 rc |= TIOCM_RI;
1904         if (mstat & UART_MSR_DCD)
1905                 rc |= TIOCM_CD;
1906
1907         return rc;
1908 }
1909
1910 /* Return modem signals to ld. */
1911
1912 static int dgnc_get_modem_info(struct channel_t *ch,
1913                                unsigned int  __user *value)
1914 {
1915         return put_user(dgnc_get_mstat(ch), value);
1916 }
1917
1918 /*
1919  * dgnc_set_modem_info()
1920  *
1921  * Set modem signals, called by ld.
1922  */
1923 static int dgnc_set_modem_info(struct channel_t *ch,
1924                                unsigned int command,
1925                                unsigned int __user *value)
1926 {
1927         int rc;
1928         unsigned int arg = 0;
1929         unsigned long flags;
1930
1931         rc = get_user(arg, value);
1932         if (rc)
1933                 return rc;
1934
1935         switch (command) {
1936         case TIOCMBIS:
1937                 if (arg & TIOCM_RTS)
1938                         ch->ch_mostat |= UART_MCR_RTS;
1939
1940                 if (arg & TIOCM_DTR)
1941                         ch->ch_mostat |= UART_MCR_DTR;
1942
1943                 break;
1944
1945         case TIOCMBIC:
1946                 if (arg & TIOCM_RTS)
1947                         ch->ch_mostat &= ~(UART_MCR_RTS);
1948
1949                 if (arg & TIOCM_DTR)
1950                         ch->ch_mostat &= ~(UART_MCR_DTR);
1951
1952                 break;
1953
1954         case TIOCMSET:
1955
1956                 if (arg & TIOCM_RTS)
1957                         ch->ch_mostat |= UART_MCR_RTS;
1958                 else
1959                         ch->ch_mostat &= ~(UART_MCR_RTS);
1960
1961                 if (arg & TIOCM_DTR)
1962                         ch->ch_mostat |= UART_MCR_DTR;
1963                 else
1964                         ch->ch_mostat &= ~(UART_MCR_DTR);
1965
1966                 break;
1967
1968         default:
1969                 return -EINVAL;
1970         }
1971
1972         spin_lock_irqsave(&ch->ch_lock, flags);
1973
1974         ch->ch_bd->bd_ops->assert_modem_signals(ch);
1975
1976         spin_unlock_irqrestore(&ch->ch_lock, flags);
1977
1978         return 0;
1979 }
1980
1981 /*
1982  * dgnc_tty_digigeta()
1983  *
1984  * Ioctl to get the information for ditty.
1985  */
1986 static int dgnc_tty_digigeta(struct tty_struct *tty,
1987                              struct digi_t __user *retinfo)
1988 {
1989         struct channel_t *ch;
1990         struct un_t *un;
1991         struct digi_t tmp;
1992         unsigned long flags;
1993         int rc = -EFAULT;
1994
1995         if (!retinfo)
1996                 return rc;
1997
1998         if (!tty || tty->magic != TTY_MAGIC)
1999                 return rc;
2000
2001         un = tty->driver_data;
2002         if (!un || un->magic != DGNC_UNIT_MAGIC)
2003                 return rc;
2004
2005         ch = un->un_ch;
2006         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2007                 return rc;
2008
2009         memset(&tmp, 0, sizeof(tmp));
2010
2011         spin_lock_irqsave(&ch->ch_lock, flags);
2012         memcpy(&tmp, &ch->ch_digi, sizeof(tmp));
2013         spin_unlock_irqrestore(&ch->ch_lock, flags);
2014
2015         if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2016                 return rc;
2017
2018         return 0;
2019 }
2020
2021 /*
2022  * dgnc_tty_digiseta()
2023  *
2024  * Ioctl to set the information for ditty.
2025  */
2026 static int dgnc_tty_digiseta(struct tty_struct *tty,
2027                              struct digi_t __user *new_info)
2028 {
2029         struct dgnc_board *bd;
2030         struct channel_t *ch;
2031         struct un_t *un;
2032         struct digi_t new_digi;
2033         unsigned long flags;
2034         int rc = -EFAULT;
2035
2036         if (!tty || tty->magic != TTY_MAGIC)
2037                 return rc;
2038
2039         un = tty->driver_data;
2040         if (!un || un->magic != DGNC_UNIT_MAGIC)
2041                 return rc;
2042
2043         ch = un->un_ch;
2044         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2045                 return rc;
2046
2047         bd = ch->ch_bd;
2048         if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2049                 return rc;
2050
2051         if (copy_from_user(&new_digi, new_info, sizeof(new_digi)))
2052                 return rc;
2053
2054         spin_lock_irqsave(&ch->ch_lock, flags);
2055
2056         /* Handle transitions to and from RTS Toggle. */
2057
2058         if (!(ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE) &&
2059             (new_digi.digi_flags & DIGI_RTS_TOGGLE))
2060                 ch->ch_mostat &= ~(UART_MCR_RTS);
2061         if ((ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE) &&
2062             !(new_digi.digi_flags & DIGI_RTS_TOGGLE))
2063                 ch->ch_mostat |= (UART_MCR_RTS);
2064
2065         /* Handle transitions to and from DTR Toggle. */
2066
2067         if (!(ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE) &&
2068             (new_digi.digi_flags & DIGI_DTR_TOGGLE))
2069                 ch->ch_mostat &= ~(UART_MCR_DTR);
2070         if ((ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE) &&
2071             !(new_digi.digi_flags & DIGI_DTR_TOGGLE))
2072                 ch->ch_mostat |= (UART_MCR_DTR);
2073
2074         memcpy(&ch->ch_digi, &new_digi, sizeof(new_digi));
2075
2076         if (ch->ch_digi.digi_maxcps < 1)
2077                 ch->ch_digi.digi_maxcps = 1;
2078
2079         if (ch->ch_digi.digi_maxcps > 10000)
2080                 ch->ch_digi.digi_maxcps = 10000;
2081
2082         if (ch->ch_digi.digi_bufsize < 10)
2083                 ch->ch_digi.digi_bufsize = 10;
2084
2085         if (ch->ch_digi.digi_maxchar < 1)
2086                 ch->ch_digi.digi_maxchar = 1;
2087
2088         if (ch->ch_digi.digi_maxchar > ch->ch_digi.digi_bufsize)
2089                 ch->ch_digi.digi_maxchar = ch->ch_digi.digi_bufsize;
2090
2091         if (ch->ch_digi.digi_onlen > DIGI_PLEN)
2092                 ch->ch_digi.digi_onlen = DIGI_PLEN;
2093
2094         if (ch->ch_digi.digi_offlen > DIGI_PLEN)
2095                 ch->ch_digi.digi_offlen = DIGI_PLEN;
2096
2097         ch->ch_bd->bd_ops->param(tty);
2098
2099         spin_unlock_irqrestore(&ch->ch_lock, flags);
2100
2101         return 0;
2102 }
2103
2104 /* dgnc_set_termios() */
2105
2106 static void dgnc_tty_set_termios(struct tty_struct *tty,
2107                                  struct ktermios *old_termios)
2108 {
2109         struct dgnc_board *bd;
2110         struct channel_t *ch;
2111         struct un_t *un;
2112         unsigned long flags;
2113
2114         if (!tty || tty->magic != TTY_MAGIC)
2115                 return;
2116
2117         un = tty->driver_data;
2118         if (!un || un->magic != DGNC_UNIT_MAGIC)
2119                 return;
2120
2121         ch = un->un_ch;
2122         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2123                 return;
2124
2125         bd = ch->ch_bd;
2126         if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2127                 return;
2128
2129         spin_lock_irqsave(&ch->ch_lock, flags);
2130
2131         ch->ch_c_cflag   = tty->termios.c_cflag;
2132         ch->ch_c_iflag   = tty->termios.c_iflag;
2133         ch->ch_c_oflag   = tty->termios.c_oflag;
2134         ch->ch_c_lflag   = tty->termios.c_lflag;
2135         ch->ch_startc = tty->termios.c_cc[VSTART];
2136         ch->ch_stopc  = tty->termios.c_cc[VSTOP];
2137
2138         ch->ch_bd->bd_ops->param(tty);
2139         dgnc_carrier(ch);
2140
2141         spin_unlock_irqrestore(&ch->ch_lock, flags);
2142 }
2143
2144 static void dgnc_tty_throttle(struct tty_struct *tty)
2145 {
2146         struct channel_t *ch;
2147         struct un_t *un;
2148         unsigned long flags;
2149
2150         if (!tty || tty->magic != TTY_MAGIC)
2151                 return;
2152
2153         un = tty->driver_data;
2154         if (!un || un->magic != DGNC_UNIT_MAGIC)
2155                 return;
2156
2157         ch = un->un_ch;
2158         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2159                 return;
2160
2161         spin_lock_irqsave(&ch->ch_lock, flags);
2162
2163         ch->ch_flags |= (CH_FORCED_STOPI);
2164
2165         spin_unlock_irqrestore(&ch->ch_lock, flags);
2166 }
2167
2168 static void dgnc_tty_unthrottle(struct tty_struct *tty)
2169 {
2170         struct channel_t *ch;
2171         struct un_t *un;
2172         unsigned long flags;
2173
2174         if (!tty || tty->magic != TTY_MAGIC)
2175                 return;
2176
2177         un = tty->driver_data;
2178         if (!un || un->magic != DGNC_UNIT_MAGIC)
2179                 return;
2180
2181         ch = un->un_ch;
2182         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2183                 return;
2184
2185         spin_lock_irqsave(&ch->ch_lock, flags);
2186
2187         ch->ch_flags &= ~(CH_FORCED_STOPI);
2188
2189         spin_unlock_irqrestore(&ch->ch_lock, flags);
2190 }
2191
2192 static void dgnc_tty_start(struct tty_struct *tty)
2193 {
2194         struct dgnc_board *bd;
2195         struct channel_t *ch;
2196         struct un_t *un;
2197         unsigned long flags;
2198
2199         if (!tty || tty->magic != TTY_MAGIC)
2200                 return;
2201
2202         un = tty->driver_data;
2203         if (!un || un->magic != DGNC_UNIT_MAGIC)
2204                 return;
2205
2206         ch = un->un_ch;
2207         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2208                 return;
2209
2210         bd = ch->ch_bd;
2211         if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2212                 return;
2213
2214         spin_lock_irqsave(&ch->ch_lock, flags);
2215
2216         ch->ch_flags &= ~(CH_FORCED_STOP);
2217
2218         spin_unlock_irqrestore(&ch->ch_lock, flags);
2219 }
2220
2221 static void dgnc_tty_stop(struct tty_struct *tty)
2222 {
2223         struct dgnc_board *bd;
2224         struct channel_t *ch;
2225         struct un_t *un;
2226         unsigned long flags;
2227
2228         if (!tty || tty->magic != TTY_MAGIC)
2229                 return;
2230
2231         un = tty->driver_data;
2232         if (!un || un->magic != DGNC_UNIT_MAGIC)
2233                 return;
2234
2235         ch = un->un_ch;
2236         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2237                 return;
2238
2239         bd = ch->ch_bd;
2240         if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2241                 return;
2242
2243         spin_lock_irqsave(&ch->ch_lock, flags);
2244
2245         ch->ch_flags |= (CH_FORCED_STOP);
2246
2247         spin_unlock_irqrestore(&ch->ch_lock, flags);
2248 }
2249
2250 /*
2251  * dgnc_tty_flush_chars()
2252  *
2253  * Flush the cook buffer
2254  *
2255  * Note to self, and any other poor souls who venture here:
2256  *
2257  * flush in this case DOES NOT mean dispose of the data.
2258  * instead, it means "stop buffering and send it if you
2259  * haven't already."  Just guess how I figured that out...   SRW 2-Jun-98
2260  *
2261  * It is also always called in interrupt context - JAR 8-Sept-99
2262  */
2263 static void dgnc_tty_flush_chars(struct tty_struct *tty)
2264 {
2265         struct dgnc_board *bd;
2266         struct channel_t *ch;
2267         struct un_t *un;
2268         unsigned long flags;
2269
2270         if (!tty || tty->magic != TTY_MAGIC)
2271                 return;
2272
2273         un = tty->driver_data;
2274         if (!un || un->magic != DGNC_UNIT_MAGIC)
2275                 return;
2276
2277         ch = un->un_ch;
2278         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2279                 return;
2280
2281         bd = ch->ch_bd;
2282         if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2283                 return;
2284
2285         spin_lock_irqsave(&ch->ch_lock, flags);
2286
2287         /* Do something maybe here */
2288
2289         spin_unlock_irqrestore(&ch->ch_lock, flags);
2290 }
2291
2292 /*
2293  * dgnc_tty_flush_buffer()
2294  *
2295  * Flush Tx buffer (make in == out)
2296  */
2297 static void dgnc_tty_flush_buffer(struct tty_struct *tty)
2298 {
2299         struct channel_t *ch;
2300         struct un_t *un;
2301         unsigned long flags;
2302
2303         if (!tty || tty->magic != TTY_MAGIC)
2304                 return;
2305
2306         un = tty->driver_data;
2307         if (!un || un->magic != DGNC_UNIT_MAGIC)
2308                 return;
2309
2310         ch = un->un_ch;
2311         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2312                 return;
2313
2314         spin_lock_irqsave(&ch->ch_lock, flags);
2315
2316         ch->ch_flags &= ~CH_STOP;
2317
2318         /* Flush our write queue */
2319         ch->ch_w_head = ch->ch_w_tail;
2320
2321         /* Flush UARTs transmit FIFO */
2322         ch->ch_bd->bd_ops->flush_uart_write(ch);
2323
2324         if (ch->ch_tun.un_flags & (UN_LOW | UN_EMPTY)) {
2325                 ch->ch_tun.un_flags &= ~(UN_LOW | UN_EMPTY);
2326                 wake_up_interruptible(&ch->ch_tun.un_flags_wait);
2327         }
2328         if (ch->ch_pun.un_flags & (UN_LOW | UN_EMPTY)) {
2329                 ch->ch_pun.un_flags &= ~(UN_LOW | UN_EMPTY);
2330                 wake_up_interruptible(&ch->ch_pun.un_flags_wait);
2331         }
2332
2333         spin_unlock_irqrestore(&ch->ch_lock, flags);
2334 }
2335
2336 /*
2337  * dgnc_wake_up_unit()
2338  *
2339  * Wakes up processes waiting in the unit's (teminal/printer) wait queue
2340  */
2341 static void dgnc_wake_up_unit(struct un_t *unit)
2342 {
2343         unit->un_flags &= ~(UN_LOW | UN_EMPTY);
2344         wake_up_interruptible(&unit->un_flags_wait);
2345 }
2346
2347 /* The IOCTL function and all of its helpers */
2348
2349 /*
2350  * dgnc_tty_ioctl()
2351  *
2352  * The usual assortment of ioctl's
2353  */
2354 static int dgnc_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
2355                           unsigned long arg)
2356 {
2357         struct dgnc_board *bd;
2358         struct board_ops *ch_bd_ops;
2359         struct channel_t *ch;
2360         struct un_t *un;
2361         int rc = -ENODEV;
2362         unsigned long flags;
2363         void __user *uarg = (void __user *)arg;
2364
2365         if (!tty || tty->magic != TTY_MAGIC)
2366                 return rc;
2367
2368         un = tty->driver_data;
2369         if (!un || un->magic != DGNC_UNIT_MAGIC)
2370                 return rc;
2371
2372         ch = un->un_ch;
2373         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2374                 return rc;
2375
2376         bd = ch->ch_bd;
2377         if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2378                 return rc;
2379
2380         ch_bd_ops = bd->bd_ops;
2381
2382         spin_lock_irqsave(&ch->ch_lock, flags);
2383
2384         if (un->un_open_count <= 0) {
2385                 rc = -EIO;
2386                 goto err_unlock;
2387         }
2388
2389         switch (cmd) {
2390         /* Here are all the standard ioctl's that we MUST implement */
2391
2392         case TCSBRK:
2393                 /*
2394                  * TCSBRK is SVID version: non-zero arg --> no break
2395                  * this behaviour is exploited by tcdrain().
2396                  *
2397                  * According to POSIX.1 spec (7.2.2.1.2) breaks should be
2398                  * between 0.25 and 0.5 seconds so we'll ask for something
2399                  * in the middle: 0.375 seconds.
2400                  */
2401                 rc = tty_check_change(tty);
2402                 spin_unlock_irqrestore(&ch->ch_lock, flags);
2403                 if (rc)
2404                         return rc;
2405
2406                 rc = ch_bd_ops->drain(tty, 0);
2407
2408                 if (rc)
2409                         return -EINTR;
2410
2411                 spin_lock_irqsave(&ch->ch_lock, flags);
2412
2413                 if (((cmd == TCSBRK) && (!arg)) || (cmd == TCSBRKP))
2414                         ch_bd_ops->send_break(ch, 250);
2415
2416                 spin_unlock_irqrestore(&ch->ch_lock, flags);
2417
2418                 return 0;
2419
2420         case TCSBRKP:
2421                 /*
2422                  * support for POSIX tcsendbreak()
2423                  * According to POSIX.1 spec (7.2.2.1.2) breaks should be
2424                  * between 0.25 and 0.5 seconds so we'll ask for something
2425                  * in the middle: 0.375 seconds.
2426                  */
2427                 rc = tty_check_change(tty);
2428                 spin_unlock_irqrestore(&ch->ch_lock, flags);
2429                 if (rc)
2430                         return rc;
2431
2432                 rc = ch_bd_ops->drain(tty, 0);
2433                 if (rc)
2434                         return -EINTR;
2435
2436                 spin_lock_irqsave(&ch->ch_lock, flags);
2437
2438                 ch_bd_ops->send_break(ch, 250);
2439
2440                 spin_unlock_irqrestore(&ch->ch_lock, flags);
2441
2442                 return 0;
2443
2444         case TIOCSBRK:
2445                 rc = tty_check_change(tty);
2446                 spin_unlock_irqrestore(&ch->ch_lock, flags);
2447                 if (rc)
2448                         return rc;
2449
2450                 rc = ch_bd_ops->drain(tty, 0);
2451                 if (rc)
2452                         return -EINTR;
2453
2454                 spin_lock_irqsave(&ch->ch_lock, flags);
2455
2456                 ch_bd_ops->send_break(ch, 250);
2457
2458                 spin_unlock_irqrestore(&ch->ch_lock, flags);
2459
2460                 return 0;
2461
2462         case TIOCCBRK:
2463                 /* Do Nothing */
2464                 spin_unlock_irqrestore(&ch->ch_lock, flags);
2465                 return 0;
2466
2467         case TIOCGSOFTCAR:
2468
2469                 spin_unlock_irqrestore(&ch->ch_lock, flags);
2470
2471                 return put_user(C_CLOCAL(tty) ? 1 : 0,
2472                                 (unsigned long __user *)arg);
2473
2474         case TIOCSSOFTCAR:
2475
2476                 spin_unlock_irqrestore(&ch->ch_lock, flags);
2477                 rc = get_user(arg, (unsigned long __user *)arg);
2478                 if (rc)
2479                         return rc;
2480
2481                 spin_lock_irqsave(&ch->ch_lock, flags);
2482                 tty->termios.c_cflag = ((tty->termios.c_cflag & ~CLOCAL) |
2483                                        (arg ? CLOCAL : 0));
2484                 ch_bd_ops->param(tty);
2485                 spin_unlock_irqrestore(&ch->ch_lock, flags);
2486
2487                 return 0;
2488
2489         case TIOCMGET:
2490                 spin_unlock_irqrestore(&ch->ch_lock, flags);
2491                 return dgnc_get_modem_info(ch, uarg);
2492
2493         case TIOCMBIS:
2494         case TIOCMBIC:
2495         case TIOCMSET:
2496                 spin_unlock_irqrestore(&ch->ch_lock, flags);
2497                 return dgnc_set_modem_info(ch, cmd, uarg);
2498
2499                 /* Here are any additional ioctl's that we want to implement */
2500
2501         case TCFLSH:
2502                 /*
2503                  * The linux tty driver doesn't have a flush
2504                  * input routine for the driver, assuming all backed
2505                  * up data is in the line disc. buffers.  However,
2506                  * we all know that's not the case.  Here, we
2507                  * act on the ioctl, but then lie and say we didn't
2508                  * so the line discipline will process the flush
2509                  * also.
2510                  */
2511                 rc = tty_check_change(tty);
2512                 if (rc)
2513                         goto err_unlock;
2514
2515                 if ((arg == TCIFLUSH) || (arg == TCIOFLUSH)) {
2516                         ch->ch_r_head = ch->ch_r_tail;
2517                         ch_bd_ops->flush_uart_read(ch);
2518                         /* Force queue flow control to be released, if needed */
2519                         dgnc_check_queue_flow_control(ch);
2520                 }
2521
2522                 if ((arg == TCOFLUSH) || (arg == TCIOFLUSH)) {
2523                         if (!(un->un_type == DGNC_PRINT)) {
2524                                 ch->ch_w_head = ch->ch_w_tail;
2525                                 ch_bd_ops->flush_uart_write(ch);
2526
2527                                 if (ch->ch_tun.un_flags & (UN_LOW | UN_EMPTY))
2528                                         dgnc_wake_up_unit(&ch->ch_tun);
2529
2530                                 if (ch->ch_pun.un_flags & (UN_LOW | UN_EMPTY))
2531                                         dgnc_wake_up_unit(&ch->ch_pun);
2532                         }
2533                 }
2534
2535                 /* pretend we didn't recognize this IOCTL */
2536                 spin_unlock_irqrestore(&ch->ch_lock, flags);
2537                 return -ENOIOCTLCMD;
2538         case TCSETSF:
2539         case TCSETSW:
2540                 /*
2541                  * The linux tty driver doesn't have a flush
2542                  * input routine for the driver, assuming all backed
2543                  * up data is in the line disc. buffers.  However,
2544                  * we all know that's not the case.  Here, we
2545                  * act on the ioctl, but then lie and say we didn't
2546                  * so the line discipline will process the flush
2547                  * also.
2548                  */
2549                 if (cmd == TCSETSF) {
2550                         /* flush rx */
2551                         ch->ch_flags &= ~CH_STOP;
2552                         ch->ch_r_head = ch->ch_r_tail;
2553                         ch_bd_ops->flush_uart_read(ch);
2554                         /* Force queue flow control to be released, if needed */
2555                         dgnc_check_queue_flow_control(ch);
2556                 }
2557
2558                 /* now wait for all the output to drain */
2559                 spin_unlock_irqrestore(&ch->ch_lock, flags);
2560                 rc = ch_bd_ops->drain(tty, 0);
2561                 if (rc)
2562                         return -EINTR;
2563
2564                 /* pretend we didn't recognize this */
2565                 return -ENOIOCTLCMD;
2566
2567         case TCSETAW:
2568
2569                 spin_unlock_irqrestore(&ch->ch_lock, flags);
2570                 rc = ch_bd_ops->drain(tty, 0);
2571                 if (rc)
2572                         return -EINTR;
2573
2574                 /* pretend we didn't recognize this */
2575                 return -ENOIOCTLCMD;
2576
2577         case TCXONC:
2578                 spin_unlock_irqrestore(&ch->ch_lock, flags);
2579                 /* Make the ld do it */
2580                 return -ENOIOCTLCMD;
2581
2582         case DIGI_GETA:
2583                 /* get information for ditty */
2584                 spin_unlock_irqrestore(&ch->ch_lock, flags);
2585                 return dgnc_tty_digigeta(tty, uarg);
2586
2587         case DIGI_SETAW:
2588         case DIGI_SETAF:
2589
2590                 /* set information for ditty */
2591                 if (cmd == (DIGI_SETAW)) {
2592                         spin_unlock_irqrestore(&ch->ch_lock, flags);
2593                         rc = ch_bd_ops->drain(tty, 0);
2594                         if (rc)
2595                                 return -EINTR;
2596
2597                         spin_lock_irqsave(&ch->ch_lock, flags);
2598                 } else {
2599                         tty_ldisc_flush(tty);
2600                 }
2601                 /* fall thru */
2602
2603         case DIGI_SETA:
2604                 spin_unlock_irqrestore(&ch->ch_lock, flags);
2605                 return dgnc_tty_digiseta(tty, uarg);
2606
2607         case DIGI_LOOPBACK:
2608                 {
2609                         uint loopback = 0;
2610                         /*
2611                          * Let go of locks when accessing user space,
2612                          * could sleep
2613                          */
2614                         spin_unlock_irqrestore(&ch->ch_lock, flags);
2615                         rc = get_user(loopback, (unsigned int __user *)arg);
2616                         if (rc)
2617                                 return rc;
2618                         spin_lock_irqsave(&ch->ch_lock, flags);
2619
2620                         /* Enable/disable internal loopback for this port */
2621                         if (loopback)
2622                                 ch->ch_flags |= CH_LOOPBACK;
2623                         else
2624                                 ch->ch_flags &= ~(CH_LOOPBACK);
2625
2626                         ch_bd_ops->param(tty);
2627                         spin_unlock_irqrestore(&ch->ch_lock, flags);
2628                         return 0;
2629                 }
2630
2631         case DIGI_GETCUSTOMBAUD:
2632                 spin_unlock_irqrestore(&ch->ch_lock, flags);
2633                 return put_user(ch->ch_custom_speed,
2634                                 (unsigned int __user *)arg);
2635
2636         case DIGI_SETCUSTOMBAUD:
2637         {
2638                 int new_rate;
2639                 /* Let go of locks when accessing user space, could sleep */
2640                 spin_unlock_irqrestore(&ch->ch_lock, flags);
2641                 rc = get_user(new_rate, (int __user *)arg);
2642                 if (rc)
2643                         return rc;
2644                 spin_lock_irqsave(&ch->ch_lock, flags);
2645                 dgnc_set_custom_speed(ch, new_rate);
2646                 ch_bd_ops->param(tty);
2647                 spin_unlock_irqrestore(&ch->ch_lock, flags);
2648                 return 0;
2649         }
2650
2651         /*
2652          * This ioctl allows insertion of a character into the front
2653          * of any pending data to be transmitted.
2654          *
2655          * This ioctl is to satisfy the "Send Character Immediate"
2656          * call that the RealPort protocol spec requires.
2657          */
2658         case DIGI_REALPORT_SENDIMMEDIATE:
2659         {
2660                 unsigned char c;
2661
2662                 spin_unlock_irqrestore(&ch->ch_lock, flags);
2663                 rc = get_user(c, (unsigned char __user *)arg);
2664                 if (rc)
2665                         return rc;
2666                 spin_lock_irqsave(&ch->ch_lock, flags);
2667                 ch_bd_ops->send_immediate_char(ch, c);
2668                 spin_unlock_irqrestore(&ch->ch_lock, flags);
2669                 return 0;
2670         }
2671
2672         /*
2673          * This ioctl returns all the current counts for the port.
2674          *
2675          * This ioctl is to satisfy the "Line Error Counters"
2676          * call that the RealPort protocol spec requires.
2677          */
2678         case DIGI_REALPORT_GETCOUNTERS:
2679         {
2680                 struct digi_getcounter buf;
2681
2682                 buf.norun = ch->ch_err_overrun;
2683                 buf.noflow = 0;         /* The driver doesn't keep this stat */
2684                 buf.nframe = ch->ch_err_frame;
2685                 buf.nparity = ch->ch_err_parity;
2686                 buf.nbreak = ch->ch_err_break;
2687                 buf.rbytes = ch->ch_rxcount;
2688                 buf.tbytes = ch->ch_txcount;
2689
2690                 spin_unlock_irqrestore(&ch->ch_lock, flags);
2691
2692                 if (copy_to_user(uarg, &buf, sizeof(buf)))
2693                         return -EFAULT;
2694
2695                 return 0;
2696         }
2697
2698         /*
2699          * This ioctl returns all current events.
2700          *
2701          * This ioctl is to satisfy the "Event Reporting"
2702          * call that the RealPort protocol spec requires.
2703          */
2704         case DIGI_REALPORT_GETEVENTS:
2705         {
2706                 unsigned int events = 0;
2707
2708                 /* NOTE: MORE EVENTS NEEDS TO BE ADDED HERE */
2709                 if (ch->ch_flags & CH_BREAK_SENDING)
2710                         events |= EV_TXB;
2711                 if ((ch->ch_flags & CH_STOP) ||
2712                     (ch->ch_flags & CH_FORCED_STOP))
2713                         events |= (EV_OPU | EV_OPS);
2714
2715                 if ((ch->ch_flags & CH_STOPI) ||
2716                     (ch->ch_flags & CH_FORCED_STOPI))
2717                         events |= (EV_IPU | EV_IPS);
2718
2719                 spin_unlock_irqrestore(&ch->ch_lock, flags);
2720                 return put_user(events, (unsigned int __user *)arg);
2721         }
2722
2723         /*
2724          * This ioctl returns TOUT and TIN counters based
2725          * upon the values passed in by the RealPort Server.
2726          * It also passes back whether the UART Transmitter is
2727          * empty as well.
2728          */
2729         case DIGI_REALPORT_GETBUFFERS:
2730         {
2731                 struct digi_getbuffer buf;
2732                 int tdist;
2733                 int count;
2734
2735                 spin_unlock_irqrestore(&ch->ch_lock, flags);
2736
2737                 /* Get data from user first. */
2738
2739                 if (copy_from_user(&buf, uarg, sizeof(buf)))
2740                         return -EFAULT;
2741
2742                 spin_lock_irqsave(&ch->ch_lock, flags);
2743
2744                 /* Figure out how much data is in our RX and TX queues. */
2745
2746                 buf.rxbuf = (ch->ch_r_head - ch->ch_r_tail) & RQUEUEMASK;
2747                 buf.txbuf = (ch->ch_w_head - ch->ch_w_tail) & WQUEUEMASK;
2748
2749                 /*
2750                  * Is the UART empty?
2751                  * Add that value to whats in our TX queue.
2752                  */
2753
2754                 count = buf.txbuf + ch_bd_ops->get_uart_bytes_left(ch);
2755
2756                 /*
2757                  * Figure out how much data the RealPort Server believes should
2758                  * be in our TX queue.
2759                  */
2760                 tdist = (buf.tx_in - buf.tx_out) & 0xffff;
2761
2762                 /*
2763                  * If we have more data than the RealPort Server believes we
2764                  * should have, reduce our count to its amount.
2765                  *
2766                  * This count difference CAN happen because the Linux LD can
2767                  * insert more characters into our queue for OPOST processing
2768                  * that the RealPort Server doesn't know about.
2769                  */
2770                 if (buf.txbuf > tdist)
2771                         buf.txbuf = tdist;
2772
2773                 /* Report whether our queue and UART TX are completely empty. */
2774
2775                 if (count)
2776                         buf.txdone = 0;
2777                 else
2778                         buf.txdone = 1;
2779
2780                 spin_unlock_irqrestore(&ch->ch_lock, flags);
2781
2782                 if (copy_to_user(uarg, &buf, sizeof(buf)))
2783                         return -EFAULT;
2784
2785                 return 0;
2786         }
2787         default:
2788                 spin_unlock_irqrestore(&ch->ch_lock, flags);
2789
2790                 return -ENOIOCTLCMD;
2791         }
2792 err_unlock:
2793         spin_unlock_irqrestore(&ch->ch_lock, flags);
2794         return rc;
2795 }