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