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