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