]> Pileus Git - ~andy/linux/blob - drivers/staging/dgnc/dgnc_tty.c
Merge branch 'stable/for-jens-3.14' of git://git.kernel.org/pub/scm/linux/kernel...
[~andy/linux] / 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                 newrate = 0;
969
970         /*
971          *  Since the divisor is stored in a 16-bit integer, we make sure
972          *  we don't allow any rates smaller than a 16-bit integer would allow.
973          *  And of course, rates above the dividend won't fly.
974          */
975         if (newrate && newrate < ((ch->ch_bd->bd_dividend / 0xFFFF) + 1))
976                 newrate = ((ch->ch_bd->bd_dividend / 0xFFFF) + 1);
977
978         if (newrate && newrate > ch->ch_bd->bd_dividend)
979                 newrate = ch->ch_bd->bd_dividend;
980
981         while (newrate > 0) {
982                 testdiv = ch->ch_bd->bd_dividend / newrate;
983
984                 /*
985                  *  If we try to figure out what rate the board would use
986                  *  with the test divisor, it will be either equal or higher
987                  *  than the requested baud rate.  If we then determine the
988                  *  rate with a divisor one higher, we will get the next lower
989                  *  supported rate below the requested.
990                  */
991                 testrate_high = ch->ch_bd->bd_dividend / testdiv;
992                 testrate_low  = ch->ch_bd->bd_dividend / (testdiv + 1);
993
994                 /*
995                  *  If the rate for the requested divisor is correct, just
996                  *  use it and be done.
997                  */
998                 if (testrate_high == newrate )
999                         break;
1000
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                 break;
1015         }
1016
1017         ch->ch_custom_speed = newrate;
1018
1019         return;
1020 }
1021
1022
1023 void dgnc_check_queue_flow_control(struct channel_t *ch)
1024 {
1025         int qleft = 0;
1026
1027         /* Store how much space we have left in the queue */
1028         if ((qleft = ch->ch_r_tail - ch->ch_r_head - 1) < 0)
1029                 qleft += RQUEUEMASK + 1;
1030
1031         /*
1032          * Check to see if we should enforce flow control on our queue because
1033          * the ld (or user) isn't reading data out of our queue fast enuf.
1034          *
1035          * NOTE: This is done based on what the current flow control of the
1036          * port is set for.
1037          *
1038          * 1) HWFLOW (RTS) - Turn off the UART's Receive interrupt.
1039          *      This will cause the UART's FIFO to back up, and force
1040          *      the RTS signal to be dropped.
1041          * 2) SWFLOW (IXOFF) - Keep trying to send a stop character to
1042          *      the other side, in hopes it will stop sending data to us.
1043          * 3) NONE - Nothing we can do.  We will simply drop any extra data
1044          *      that gets sent into us when the queue fills up.
1045          */
1046         if (qleft < 256) {
1047                 /* HWFLOW */
1048                 if (ch->ch_digi.digi_flags & CTSPACE || ch->ch_c_cflag & CRTSCTS) {
1049                         if(!(ch->ch_flags & CH_RECEIVER_OFF)) {
1050                                 ch->ch_bd->bd_ops->disable_receiver(ch);
1051                                 ch->ch_flags |= (CH_RECEIVER_OFF);
1052                                 DPR_READ(("Internal queue hit hilevel mark (%d)! Turning off interrupts.\n",
1053                                         qleft));
1054                         }
1055                 }
1056                 /* SWFLOW */
1057                 else if (ch->ch_c_iflag & IXOFF) {
1058                         if (ch->ch_stops_sent <= MAX_STOPS_SENT) {
1059                                 ch->ch_bd->bd_ops->send_stop_character(ch);
1060                                 ch->ch_stops_sent++;
1061                                 DPR_READ(("Sending stop char!  Times sent: %x\n", ch->ch_stops_sent));
1062                         }
1063                 }
1064                 /* No FLOW */
1065                 else {
1066                         /* Empty... Can't do anything about the impending overflow... */
1067                 }
1068         }
1069
1070         /*
1071          * Check to see if we should unenforce flow control because
1072          * ld (or user) finally read enuf data out of our queue.
1073          *
1074          * NOTE: This is done based on what the current flow control of the
1075          * port is set for.
1076          *
1077          * 1) HWFLOW (RTS) - Turn back on the UART's Receive interrupt.
1078          *      This will cause the UART's FIFO to raise RTS back up,
1079          *      which will allow the other side to start sending data again.
1080          * 2) SWFLOW (IXOFF) - Send a start character to
1081          *      the other side, so it will start sending data to us again.
1082          * 3) NONE - Do nothing. Since we didn't do anything to turn off the
1083          *      other side, we don't need to do anything now.
1084          */
1085         if (qleft > (RQUEUESIZE / 2)) {
1086                 /* HWFLOW */
1087                 if (ch->ch_digi.digi_flags & RTSPACE || ch->ch_c_cflag & CRTSCTS) {
1088                         if (ch->ch_flags & CH_RECEIVER_OFF) {
1089                                 ch->ch_bd->bd_ops->enable_receiver(ch);
1090                                 ch->ch_flags &= ~(CH_RECEIVER_OFF);
1091                                 DPR_READ(("Internal queue hit lowlevel mark (%d)! Turning on interrupts.\n",
1092                                         qleft));
1093                         }
1094                 }
1095                 /* SWFLOW */
1096                 else if (ch->ch_c_iflag & IXOFF && ch->ch_stops_sent) {
1097                         ch->ch_stops_sent = 0;
1098                         ch->ch_bd->bd_ops->send_start_character(ch);
1099                         DPR_READ(("Sending start char!\n"));
1100                 }
1101                 /* No FLOW */
1102                 else {
1103                         /* Nothing needed. */
1104                 }
1105         }
1106 }
1107
1108
1109 void dgnc_wakeup_writes(struct channel_t *ch)
1110 {
1111         int qlen = 0;
1112         ulong lock_flags;
1113
1114         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1115                 return;
1116
1117         DGNC_LOCK(ch->ch_lock, lock_flags);
1118
1119         /*
1120          * If channel now has space, wake up anyone waiting on the condition.
1121          */
1122         if ((qlen = ch->ch_w_head - ch->ch_w_tail) < 0)
1123                 qlen += WQUEUESIZE;
1124
1125         if (qlen >= (WQUEUESIZE - 256)) {
1126                 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1127                 return;
1128         }
1129
1130         if (ch->ch_tun.un_flags & UN_ISOPEN) {
1131                 if ((ch->ch_tun.un_tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1132                         ch->ch_tun.un_tty->ldisc->ops->write_wakeup)
1133                 {
1134                         DGNC_UNLOCK(ch->ch_lock, lock_flags);
1135                         (ch->ch_tun.un_tty->ldisc->ops->write_wakeup)(ch->ch_tun.un_tty);
1136                         DGNC_LOCK(ch->ch_lock, lock_flags);
1137                 }
1138
1139                 wake_up_interruptible(&ch->ch_tun.un_tty->write_wait);
1140
1141                 /*
1142                  * If unit is set to wait until empty, check to make sure
1143                  * the queue AND FIFO are both empty.
1144                  */
1145                 if (ch->ch_tun.un_flags & UN_EMPTY) {
1146                         if ((qlen == 0) && (ch->ch_bd->bd_ops->get_uart_bytes_left(ch) == 0)) {
1147                                 ch->ch_tun.un_flags &= ~(UN_EMPTY);
1148
1149                                 /*
1150                                  * If RTS Toggle mode is on, whenever
1151                                  * the queue and UART is empty, keep RTS low.
1152                                  */
1153                                 if (ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE) {
1154                                         ch->ch_mostat &= ~(UART_MCR_RTS);
1155                                         ch->ch_bd->bd_ops->assert_modem_signals(ch);
1156                                 }
1157
1158                                 /*
1159                                  * If DTR Toggle mode is on, whenever
1160                                  * the queue and UART is empty, keep DTR low.
1161                                  */
1162                                 if (ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE) {
1163                                         ch->ch_mostat &= ~(UART_MCR_DTR);
1164                                         ch->ch_bd->bd_ops->assert_modem_signals(ch);
1165                                 }
1166                         }
1167                 }
1168
1169                 wake_up_interruptible(&ch->ch_tun.un_flags_wait);
1170         }
1171
1172         if (ch->ch_pun.un_flags & UN_ISOPEN) {
1173                 if ((ch->ch_pun.un_tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1174                         ch->ch_pun.un_tty->ldisc->ops->write_wakeup)
1175                 {
1176                         DGNC_UNLOCK(ch->ch_lock, lock_flags);
1177                         (ch->ch_pun.un_tty->ldisc->ops->write_wakeup)(ch->ch_pun.un_tty);
1178                         DGNC_LOCK(ch->ch_lock, lock_flags);
1179                 }
1180
1181                 wake_up_interruptible(&ch->ch_pun.un_tty->write_wait);
1182
1183                 /*
1184                  * If unit is set to wait until empty, check to make sure
1185                  * the queue AND FIFO are both empty.
1186                  */
1187                 if (ch->ch_pun.un_flags & UN_EMPTY) {
1188                         if ((qlen == 0) && (ch->ch_bd->bd_ops->get_uart_bytes_left(ch) == 0)) {
1189                                 ch->ch_pun.un_flags &= ~(UN_EMPTY);
1190                         }
1191                 }
1192
1193                 wake_up_interruptible(&ch->ch_pun.un_flags_wait);
1194         }
1195
1196         DGNC_UNLOCK(ch->ch_lock, lock_flags);
1197 }
1198
1199
1200
1201 /************************************************************************
1202  *
1203  * TTY Entry points and helper functions
1204  *
1205  ************************************************************************/
1206
1207 /*
1208  * dgnc_tty_open()
1209  *
1210  */
1211 static int dgnc_tty_open(struct tty_struct *tty, struct file *file)
1212 {
1213         struct dgnc_board       *brd;
1214         struct channel_t *ch;
1215         struct un_t     *un;
1216         uint            major = 0;
1217         uint            minor = 0;
1218         int             rc = 0;
1219         ulong           lock_flags;
1220
1221         rc = 0;
1222
1223         major = MAJOR(tty_devnum(tty));
1224         minor = MINOR(tty_devnum(tty));
1225
1226         if (major > 255) {
1227                 return -ENXIO;
1228         }
1229
1230         /* Get board pointer from our array of majors we have allocated */
1231         brd = dgnc_BoardsByMajor[major];
1232         if (!brd) {
1233                 return -ENXIO;
1234         }
1235
1236         /*
1237          * If board is not yet up to a state of READY, go to
1238          * sleep waiting for it to happen or they cancel the open.
1239          */
1240         rc = wait_event_interruptible(brd->state_wait,
1241                 (brd->state & BOARD_READY));
1242
1243         if (rc) {
1244                 return rc;
1245         }
1246
1247         DGNC_LOCK(brd->bd_lock, lock_flags);
1248
1249         /* If opened device is greater than our number of ports, bail. */
1250         if (PORT_NUM(minor) > brd->nasync) {
1251                 DGNC_UNLOCK(brd->bd_lock, lock_flags);
1252                 return -ENXIO;
1253         }
1254
1255         ch = brd->channels[PORT_NUM(minor)];
1256         if (!ch) {
1257                 DGNC_UNLOCK(brd->bd_lock, lock_flags);
1258                 return -ENXIO;
1259         }
1260
1261         /* Drop board lock */
1262         DGNC_UNLOCK(brd->bd_lock, lock_flags);
1263
1264         /* Grab channel lock */
1265         DGNC_LOCK(ch->ch_lock, lock_flags);
1266
1267         /* Figure out our type */
1268         if (!IS_PRINT(minor)) {
1269                 un = &brd->channels[PORT_NUM(minor)]->ch_tun;
1270                 un->un_type = DGNC_SERIAL;
1271         }
1272         else if (IS_PRINT(minor)) {
1273                 un = &brd->channels[PORT_NUM(minor)]->ch_pun;
1274                 un->un_type = DGNC_PRINT;
1275         }
1276         else {
1277                 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1278                 DPR_OPEN(("%d Unknown TYPE!\n", __LINE__));
1279                 return -ENXIO;
1280         }
1281
1282         /*
1283          * If the port is still in a previous open, and in a state
1284          * where we simply cannot safely keep going, wait until the
1285          * state clears.
1286          */
1287         DGNC_UNLOCK(ch->ch_lock, lock_flags);
1288
1289         rc = wait_event_interruptible(ch->ch_flags_wait, ((ch->ch_flags & CH_OPENING) == 0));
1290
1291         /* If ret is non-zero, user ctrl-c'ed us */
1292         if (rc) {
1293                 DPR_OPEN(("%d User ctrl c'ed\n", __LINE__));
1294                 return -EINTR;
1295         }
1296
1297         /*
1298          * If either unit is in the middle of the fragile part of close,
1299          * we just cannot touch the channel safely.
1300          * Go to sleep, knowing that when the channel can be
1301          * touched safely, the close routine will signal the
1302          * ch_flags_wait to wake us back up.
1303          */
1304         rc = wait_event_interruptible(ch->ch_flags_wait,
1305                 (((ch->ch_tun.un_flags | ch->ch_pun.un_flags) & UN_CLOSING) == 0));
1306
1307         /* If ret is non-zero, user ctrl-c'ed us */
1308         if (rc) {
1309                 DPR_OPEN(("%d User ctrl c'ed\n", __LINE__));
1310                 return -EINTR;
1311         }
1312
1313         DGNC_LOCK(ch->ch_lock, lock_flags);
1314
1315
1316         /* Store our unit into driver_data, so we always have it available. */
1317         tty->driver_data = un;
1318
1319         DPR_OPEN(("Open called. MAJOR: %d MINOR:%d PORT_NUM: %x unit: %p NAME: %s\n",
1320                 MAJOR(tty_devnum(tty)), MINOR(tty_devnum(tty)), PORT_NUM(minor), un, brd->name));
1321
1322         DPR_OPEN(("%d: tflag=%x  pflag=%x\n", __LINE__, ch->ch_tun.un_flags, ch->ch_pun.un_flags));
1323
1324         /*
1325          * Initialize tty's
1326          */
1327         if (!(un->un_flags & UN_ISOPEN)) {
1328                 /* Store important variables. */
1329                 un->un_tty     = tty;
1330
1331                 /* Maybe do something here to the TTY struct as well? */
1332         }
1333
1334
1335         /*
1336          * Allocate channel buffers for read/write/error.
1337          * Set flag, so we don't get trounced on.
1338          */
1339         ch->ch_flags |= (CH_OPENING);
1340
1341         /* Drop locks, as malloc with GFP_KERNEL can sleep */
1342         DGNC_UNLOCK(ch->ch_lock, lock_flags);
1343
1344         if (!ch->ch_rqueue)
1345                 ch->ch_rqueue = kzalloc(RQUEUESIZE, GFP_KERNEL);
1346         if (!ch->ch_equeue)
1347                 ch->ch_equeue = kzalloc(EQUEUESIZE, GFP_KERNEL);
1348         if (!ch->ch_wqueue)
1349                 ch->ch_wqueue = kzalloc(WQUEUESIZE, GFP_KERNEL);
1350
1351         DGNC_LOCK(ch->ch_lock, lock_flags);
1352
1353         ch->ch_flags &= ~(CH_OPENING);
1354         wake_up_interruptible(&ch->ch_flags_wait);
1355
1356         /*
1357          * Initialize if neither terminal or printer is open.
1358          */
1359         if (!((ch->ch_tun.un_flags | ch->ch_pun.un_flags) & UN_ISOPEN)) {
1360
1361                 DPR_OPEN(("dgnc_open: initializing channel in open...\n"));
1362
1363                 /*
1364                  * Flush input queues.
1365                  */
1366                 ch->ch_r_head = ch->ch_r_tail = 0;
1367                 ch->ch_e_head = ch->ch_e_tail = 0;
1368                 ch->ch_w_head = ch->ch_w_tail = 0;
1369
1370                 brd->bd_ops->flush_uart_write(ch);
1371                 brd->bd_ops->flush_uart_read(ch);
1372
1373                 ch->ch_flags = 0;
1374                 ch->ch_cached_lsr = 0;
1375                 ch->ch_stop_sending_break = 0;
1376                 ch->ch_stops_sent = 0;
1377
1378                 ch->ch_c_cflag   = tty->termios.c_cflag;
1379                 ch->ch_c_iflag   = tty->termios.c_iflag;
1380                 ch->ch_c_oflag   = tty->termios.c_oflag;
1381                 ch->ch_c_lflag   = tty->termios.c_lflag;
1382                 ch->ch_startc = tty->termios.c_cc[VSTART];
1383                 ch->ch_stopc  = tty->termios.c_cc[VSTOP];
1384
1385                 /*
1386                  * Bring up RTS and DTR...
1387                  * Also handle RTS or DTR toggle if set.
1388                  */
1389                 if (!(ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE))
1390                         ch->ch_mostat |= (UART_MCR_RTS);
1391                 if (!(ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE))
1392                         ch->ch_mostat |= (UART_MCR_DTR);
1393
1394                 /* Tell UART to init itself */
1395                 brd->bd_ops->uart_init(ch);
1396         }
1397
1398         /*
1399          * Run param in case we changed anything
1400          */
1401         brd->bd_ops->param(tty);
1402
1403         dgnc_carrier(ch);
1404
1405         /*
1406          * follow protocol for opening port
1407          */
1408
1409         DGNC_UNLOCK(ch->ch_lock, lock_flags);
1410
1411         rc = dgnc_block_til_ready(tty, file, ch);
1412
1413         if (rc) {
1414                 DPR_OPEN(("dgnc_tty_open returning after dgnc_block_til_ready "
1415                         "with %d\n", rc));
1416         }
1417
1418         /* No going back now, increment our unit and channel counters */
1419         DGNC_LOCK(ch->ch_lock, lock_flags);
1420         ch->ch_open_count++;
1421         un->un_open_count++;
1422         un->un_flags |= (UN_ISOPEN);
1423         DGNC_UNLOCK(ch->ch_lock, lock_flags);
1424
1425         DPR_OPEN(("dgnc_tty_open finished\n"));
1426         return rc;
1427 }
1428
1429
1430 /*
1431  * dgnc_block_til_ready()
1432  *
1433  * Wait for DCD, if needed.
1434  */
1435 static int dgnc_block_til_ready(struct tty_struct *tty, struct file *file, struct channel_t *ch)
1436 {
1437         int retval = 0;
1438         struct un_t *un = NULL;
1439         ulong   lock_flags;
1440         uint    old_flags = 0;
1441         int     sleep_on_un_flags = 0;
1442
1443         if (!tty || tty->magic != TTY_MAGIC || !file || !ch || ch->magic != DGNC_CHANNEL_MAGIC) {
1444                 return -ENXIO;
1445         }
1446
1447         un = tty->driver_data;
1448         if (!un || un->magic != DGNC_UNIT_MAGIC) {
1449                 return -ENXIO;
1450         }
1451
1452         DPR_OPEN(("dgnc_block_til_ready - before block.\n"));
1453
1454         DGNC_LOCK(ch->ch_lock, lock_flags);
1455
1456         ch->ch_wopen++;
1457
1458         /* Loop forever */
1459         while (1) {
1460
1461                 sleep_on_un_flags = 0;
1462
1463                 /*
1464                  * If board has failed somehow during our sleep, bail with error.
1465                  */
1466                 if (ch->ch_bd->state == BOARD_FAILED) {
1467                         retval = -ENXIO;
1468                         break;
1469                 }
1470
1471                 /* If tty was hung up, break out of loop and set error. */
1472                 if (tty_hung_up_p(file)) {
1473                         retval = -EAGAIN;
1474                         break;
1475                 }
1476
1477                 /*
1478                  * If either unit is in the middle of the fragile part of close,
1479                  * we just cannot touch the channel safely.
1480                  * Go back to sleep, knowing that when the channel can be
1481                  * touched safely, the close routine will signal the
1482                  * ch_wait_flags to wake us back up.
1483                  */
1484                 if (!((ch->ch_tun.un_flags | ch->ch_pun.un_flags) & UN_CLOSING)) {
1485
1486                         /*
1487                          * Our conditions to leave cleanly and happily:
1488                          * 1) NONBLOCKING on the tty is set.
1489                          * 2) CLOCAL is set.
1490                          * 3) DCD (fake or real) is active.
1491                          */
1492
1493                         if (file->f_flags & O_NONBLOCK) {
1494                                 break;
1495                         }
1496
1497                         if (tty->flags & (1 << TTY_IO_ERROR)) {
1498                                 retval = -EIO;
1499                                 break;
1500                         }
1501
1502                         if (ch->ch_flags & CH_CD) {
1503                                 DPR_OPEN(("%d: ch_flags: %x\n", __LINE__, ch->ch_flags));
1504                                 break;
1505                         }
1506
1507                         if (ch->ch_flags & CH_FCAR) {
1508                                 DPR_OPEN(("%d: ch_flags: %x\n", __LINE__, ch->ch_flags));
1509                                 break;
1510                         }
1511                 }
1512                 else {
1513                         sleep_on_un_flags = 1;
1514                 }
1515
1516                 /*
1517                  * If there is a signal pending, the user probably
1518                  * interrupted (ctrl-c) us.
1519                  * Leave loop with error set.
1520                  */
1521                 if (signal_pending(current)) {
1522                         DPR_OPEN(("%d: signal pending...\n", __LINE__));
1523                         retval = -ERESTARTSYS;
1524                         break;
1525                 }
1526
1527                 DPR_OPEN(("dgnc_block_til_ready - blocking.\n"));
1528
1529                 /*
1530                  * Store the flags before we let go of channel lock
1531                  */
1532                 if (sleep_on_un_flags)
1533                         old_flags = ch->ch_tun.un_flags | ch->ch_pun.un_flags;
1534                 else
1535                         old_flags = ch->ch_flags;
1536
1537                 /*
1538                  * Let go of channel lock before calling schedule.
1539                  * Our poller will get any FEP events and wake us up when DCD
1540                  * eventually goes active.
1541                  */
1542
1543                 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1544
1545                 DPR_OPEN(("Going to sleep on %s flags...\n",
1546                         (sleep_on_un_flags ? "un" : "ch")));
1547
1548                 /*
1549                  * Wait for something in the flags to change from the current value.
1550                  */
1551                 if (sleep_on_un_flags) {
1552                         retval = wait_event_interruptible(un->un_flags_wait,
1553                                 (old_flags != (ch->ch_tun.un_flags | ch->ch_pun.un_flags)));
1554                 }
1555                 else {
1556                         retval = wait_event_interruptible(ch->ch_flags_wait,
1557                                 (old_flags != ch->ch_flags));
1558                 }
1559
1560                 DPR_OPEN(("After sleep... retval: %x\n", retval));
1561
1562                 /*
1563                  * We got woken up for some reason.
1564                  * Before looping around, grab our channel lock.
1565                  */
1566                 DGNC_LOCK(ch->ch_lock, lock_flags);
1567         }
1568
1569         ch->ch_wopen--;
1570
1571         DGNC_UNLOCK(ch->ch_lock, lock_flags);
1572
1573         DPR_OPEN(("dgnc_block_til_ready - after blocking.\n"));
1574
1575         if (retval) {
1576                 DPR_OPEN(("dgnc_block_til_ready - done. error. retval: %x\n", retval));
1577                 return retval;
1578         }
1579
1580         DPR_OPEN(("dgnc_block_til_ready - done no error. jiffies: %lu\n", jiffies));
1581
1582         return 0;
1583 }
1584
1585
1586 /*
1587  * dgnc_tty_hangup()
1588  *
1589  * Hangup the port.  Like a close, but don't wait for output to drain.
1590  */
1591 static void dgnc_tty_hangup(struct tty_struct *tty)
1592 {
1593         struct un_t     *un;
1594
1595         if (!tty || tty->magic != TTY_MAGIC)
1596                 return;
1597
1598         un = tty->driver_data;
1599         if (!un || un->magic != DGNC_UNIT_MAGIC)
1600                 return;
1601
1602         DPR_CLOSE(("dgnc_hangup called. ch->ch_open_count: %d un->un_open_count: %d\n",
1603                 un->un_ch->ch_open_count, un->un_open_count));
1604
1605         /* flush the transmit queues */
1606         dgnc_tty_flush_buffer(tty);
1607
1608         DPR_CLOSE(("dgnc_hangup finished. ch->ch_open_count: %d un->un_open_count: %d\n",
1609                 un->un_ch->ch_open_count, un->un_open_count));
1610 }
1611
1612
1613 /*
1614  * dgnc_tty_close()
1615  *
1616  */
1617 static void dgnc_tty_close(struct tty_struct *tty, struct file *file)
1618 {
1619         struct ktermios *ts;
1620         struct dgnc_board *bd;
1621         struct channel_t *ch;
1622         struct un_t *un;
1623         ulong lock_flags;
1624         int rc = 0;
1625
1626         if (!tty || tty->magic != TTY_MAGIC)
1627                 return;
1628
1629         un = tty->driver_data;
1630         if (!un || un->magic != DGNC_UNIT_MAGIC)
1631                 return;
1632
1633         ch = un->un_ch;
1634         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1635                 return;
1636
1637         bd = ch->ch_bd;
1638         if (!bd || bd->magic != DGNC_BOARD_MAGIC)
1639                 return;
1640
1641         ts = &tty->termios;
1642
1643         DPR_CLOSE(("Close called\n"));
1644
1645         DGNC_LOCK(ch->ch_lock, lock_flags);
1646
1647         /*
1648          * Determine if this is the last close or not - and if we agree about
1649          * which type of close it is with the Line Discipline
1650          */
1651         if ((tty->count == 1) && (un->un_open_count != 1)) {
1652                 /*
1653                  * Uh, oh.  tty->count is 1, which means that the tty
1654                  * structure will be freed.  un_open_count should always
1655                  * be one in these conditions.  If it's greater than
1656                  * one, we've got real problems, since it means the
1657                  * serial port won't be shutdown.
1658                  */
1659                 APR(("tty->count is 1, un open count is %d\n", un->un_open_count));
1660                 un->un_open_count = 1;
1661         }
1662
1663         if (--un->un_open_count < 0) {
1664                 APR(("bad serial port open count of %d\n", un->un_open_count));
1665                 un->un_open_count = 0;
1666         }
1667
1668         ch->ch_open_count--;
1669
1670         if (ch->ch_open_count && un->un_open_count) {
1671                 DPR_CLOSE(("dgnc_tty_close: not last close ch: %d un:%d\n",
1672                         ch->ch_open_count, un->un_open_count));
1673
1674                 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1675                 return;
1676         }
1677
1678         /* OK, its the last close on the unit */
1679         DPR_CLOSE(("dgnc_tty_close - last close on unit procedures\n"));
1680
1681         un->un_flags |= UN_CLOSING;
1682
1683         tty->closing = 1;
1684
1685
1686         /*
1687          * Only officially close channel if count is 0 and
1688          * DIGI_PRINTER bit is not set.
1689          */
1690         if ((ch->ch_open_count == 0) && !(ch->ch_digi.digi_flags & DIGI_PRINTER)) {
1691
1692                 ch->ch_flags &= ~(CH_STOPI | CH_FORCED_STOPI);
1693
1694                 /*
1695                  * turn off print device when closing print device.
1696                  */
1697                 if ((un->un_type == DGNC_PRINT) && (ch->ch_flags & CH_PRON) ) {
1698                         dgnc_wmove(ch, ch->ch_digi.digi_offstr,
1699                                 (int) ch->ch_digi.digi_offlen);
1700                         ch->ch_flags &= ~CH_PRON;
1701                 }
1702
1703                 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1704                 /* wait for output to drain */
1705                 /* This will also return if we take an interrupt */
1706
1707                 DPR_CLOSE(("Calling wait_for_drain\n"));
1708                 rc = bd->bd_ops->drain(tty, 0);
1709
1710                 DPR_CLOSE(("After calling wait_for_drain\n"));
1711
1712                 if (rc) {
1713                         DPR_BASIC(("dgnc_tty_close - bad return: %d ", rc));
1714                 }
1715
1716                 dgnc_tty_flush_buffer(tty);
1717                 tty_ldisc_flush(tty);
1718
1719                 DGNC_LOCK(ch->ch_lock, lock_flags);
1720
1721                 tty->closing = 0;
1722
1723                 /*
1724                  * If we have HUPCL set, lower DTR and RTS
1725                  */
1726                 if (ch->ch_c_cflag & HUPCL) {
1727                         DPR_CLOSE(("Close. HUPCL set, dropping DTR/RTS\n"));
1728
1729                         /* Drop RTS/DTR */
1730                         ch->ch_mostat &= ~(UART_MCR_DTR | UART_MCR_RTS);
1731                         bd->bd_ops->assert_modem_signals(ch);
1732
1733                         /*
1734                          * Go to sleep to ensure RTS/DTR
1735                          * have been dropped for modems to see it.
1736                          */
1737                         if (ch->ch_close_delay) {
1738                                 DPR_CLOSE(("Close. Sleeping for RTS/DTR drop\n"));
1739
1740                                 DGNC_UNLOCK(ch->ch_lock, lock_flags);
1741                                 dgnc_ms_sleep(ch->ch_close_delay);
1742                                 DGNC_LOCK(ch->ch_lock, lock_flags);
1743
1744                                 DPR_CLOSE(("Close. After sleeping for RTS/DTR drop\n"));
1745                         }
1746                 }
1747
1748                 ch->ch_old_baud = 0;
1749
1750                 /* Turn off UART interrupts for this port */
1751                 ch->ch_bd->bd_ops->uart_off(ch);
1752         }
1753         else {
1754                 /*
1755                  * turn off print device when closing print device.
1756                  */
1757                 if ((un->un_type == DGNC_PRINT) && (ch->ch_flags & CH_PRON) ) {
1758                         dgnc_wmove(ch, ch->ch_digi.digi_offstr,
1759                                 (int) ch->ch_digi.digi_offlen);
1760                         ch->ch_flags &= ~CH_PRON;
1761                 }
1762         }
1763
1764         un->un_tty = NULL;
1765         un->un_flags &= ~(UN_ISOPEN | UN_CLOSING);
1766
1767         DPR_CLOSE(("Close. Doing wakeups\n"));
1768         wake_up_interruptible(&ch->ch_flags_wait);
1769         wake_up_interruptible(&un->un_flags_wait);
1770
1771         DGNC_UNLOCK(ch->ch_lock, lock_flags);
1772
1773         DPR_BASIC(("dgnc_tty_close - complete\n"));
1774 }
1775
1776
1777 /*
1778  * dgnc_tty_chars_in_buffer()
1779  *
1780  * Return number of characters that have not been transmitted yet.
1781  *
1782  * This routine is used by the line discipline to determine if there
1783  * is data waiting to be transmitted/drained/flushed or not.
1784  */
1785 static int dgnc_tty_chars_in_buffer(struct tty_struct *tty)
1786 {
1787         struct channel_t *ch = NULL;
1788         struct un_t *un = NULL;
1789         ushort thead;
1790         ushort ttail;
1791         uint tmask;
1792         uint chars = 0;
1793         ulong   lock_flags = 0;
1794
1795         if (tty == NULL)
1796                 return 0;
1797
1798         un = tty->driver_data;
1799         if (!un || un->magic != DGNC_UNIT_MAGIC)
1800                 return 0;
1801
1802         ch = un->un_ch;
1803         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1804                 return 0;
1805
1806         DGNC_LOCK(ch->ch_lock, lock_flags);
1807
1808         tmask = WQUEUEMASK;
1809         thead = ch->ch_w_head & tmask;
1810         ttail = ch->ch_w_tail & tmask;
1811
1812         DGNC_UNLOCK(ch->ch_lock, lock_flags);
1813
1814         if (ttail == thead) {
1815                 chars = 0;
1816         } else {
1817                 if (thead >= ttail)
1818                         chars = thead - ttail;
1819                 else
1820                         chars = thead - ttail + WQUEUESIZE;
1821         }
1822
1823         DPR_WRITE(("dgnc_tty_chars_in_buffer. Port: %x - %d (head: %d tail: %d)\n",
1824                 ch->ch_portnum, chars, thead, ttail));
1825
1826         return chars;
1827 }
1828
1829
1830 /*
1831  * dgnc_maxcps_room
1832  *
1833  * Reduces bytes_available to the max number of characters
1834  * that can be sent currently given the maxcps value, and
1835  * returns the new bytes_available.  This only affects printer
1836  * output.
1837  */
1838 static int dgnc_maxcps_room(struct tty_struct *tty, int bytes_available)
1839 {
1840         struct channel_t *ch = NULL;
1841         struct un_t *un = NULL;
1842
1843         if (!tty)
1844                 return bytes_available;
1845
1846         un = tty->driver_data;
1847         if (!un || un->magic != DGNC_UNIT_MAGIC)
1848                 return bytes_available;
1849
1850         ch = un->un_ch;
1851         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1852                 return bytes_available;
1853
1854         /*
1855          * If its not the Transparent print device, return
1856          * the full data amount.
1857          */
1858         if (un->un_type != DGNC_PRINT)
1859                 return bytes_available;
1860
1861         if (ch->ch_digi.digi_maxcps > 0 && ch->ch_digi.digi_bufsize > 0 ) {
1862                 int cps_limit = 0;
1863                 unsigned long current_time = jiffies;
1864                 unsigned long buffer_time = current_time +
1865                         (HZ * ch->ch_digi.digi_bufsize) / ch->ch_digi.digi_maxcps;
1866
1867                 if (ch->ch_cpstime < current_time) {
1868                         /* buffer is empty */
1869                         ch->ch_cpstime = current_time;      /* reset ch_cpstime */
1870                         cps_limit = ch->ch_digi.digi_bufsize;
1871                 }
1872                 else if (ch->ch_cpstime < buffer_time) {
1873                         /* still room in the buffer */
1874                         cps_limit = ((buffer_time - ch->ch_cpstime) * ch->ch_digi.digi_maxcps) / HZ;
1875                 }
1876                 else {
1877                         /* no room in the buffer */
1878                         cps_limit = 0;
1879                 }
1880
1881                 bytes_available = min(cps_limit, bytes_available);
1882         }
1883
1884         return bytes_available;
1885 }
1886
1887
1888 /*
1889  * dgnc_tty_write_room()
1890  *
1891  * Return space available in Tx buffer
1892  */
1893 static int dgnc_tty_write_room(struct tty_struct *tty)
1894 {
1895         struct channel_t *ch = NULL;
1896         struct un_t *un = NULL;
1897         ushort head;
1898         ushort tail;
1899         ushort tmask;
1900         int ret = 0;
1901         ulong   lock_flags = 0;
1902
1903         if (tty == NULL || dgnc_TmpWriteBuf == NULL)
1904                 return 0;
1905
1906         un = tty->driver_data;
1907         if (!un || un->magic != DGNC_UNIT_MAGIC)
1908                 return 0;
1909
1910         ch = un->un_ch;
1911         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1912                 return 0;
1913
1914         DGNC_LOCK(ch->ch_lock, lock_flags);
1915
1916         tmask = WQUEUEMASK;
1917         head = (ch->ch_w_head) & tmask;
1918         tail = (ch->ch_w_tail) & tmask;
1919
1920         if ((ret = tail - head - 1) < 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         if ((bufcount = tail - head - 1) < 0)
2021                 bufcount += WQUEUESIZE;
2022
2023         DPR_WRITE(("%d: bufcount: %x count: %x tail: %x head: %x tmask: %x\n",
2024                 __LINE__, bufcount, count, tail, head, tmask));
2025
2026         /*
2027          * Limit printer output to maxcps overall, with bursts allowed
2028          * up to bufsize characters.
2029          */
2030         bufcount = dgnc_maxcps_room(tty, bufcount);
2031
2032         /*
2033          * Take minimum of what the user wants to send, and the
2034          * space available in the FEP buffer.
2035          */
2036         count = min(count, bufcount);
2037
2038         /*
2039          * Bail if no space left.
2040          */
2041         if (count <= 0) {
2042                 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2043                 return 0;
2044         }
2045
2046         /*
2047          * Output the printer ON string, if we are in terminal mode, but
2048          * need to be in printer mode.
2049          */
2050         if ((un->un_type == DGNC_PRINT) && !(ch->ch_flags & CH_PRON)) {
2051                 dgnc_wmove(ch, ch->ch_digi.digi_onstr,
2052                     (int) ch->ch_digi.digi_onlen);
2053                 head = (ch->ch_w_head) & tmask;
2054                 ch->ch_flags |= CH_PRON;
2055         }
2056
2057         /*
2058          * On the other hand, output the printer OFF string, if we are
2059          * currently in printer mode, but need to output to the terminal.
2060          */
2061         if ((un->un_type != DGNC_PRINT) && (ch->ch_flags & CH_PRON)) {
2062                 dgnc_wmove(ch, ch->ch_digi.digi_offstr,
2063                         (int) ch->ch_digi.digi_offlen);
2064                 head = (ch->ch_w_head) & tmask;
2065                 ch->ch_flags &= ~CH_PRON;
2066         }
2067
2068         /*
2069          * If there is nothing left to copy, or I can't handle any more data, leave.
2070          */
2071         if (count <= 0) {
2072                 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2073                 return 0;
2074         }
2075
2076         if (from_user) {
2077
2078                 count = min(count, WRITEBUFLEN);
2079
2080                 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2081
2082                 /*
2083                  * If data is coming from user space, copy it into a temporary
2084                  * buffer so we don't get swapped out while doing the copy to
2085                  * the board.
2086                  */
2087                 /* we're allowed to block if it's from_user */
2088                 if (down_interruptible(&dgnc_TmpWriteSem)) {
2089                         return -EINTR;
2090                 }
2091
2092                 /*
2093                  * copy_from_user() returns the number
2094                  * of bytes that could *NOT* be copied.
2095                  */
2096                 count -= copy_from_user(dgnc_TmpWriteBuf, (const uchar __user *) buf, count);
2097
2098                 if (!count) {
2099                         up(&dgnc_TmpWriteSem);
2100                         return -EFAULT;
2101                 }
2102
2103                 DGNC_LOCK(ch->ch_lock, lock_flags);
2104
2105                 buf = dgnc_TmpWriteBuf;
2106
2107         }
2108
2109         n = count;
2110
2111         /*
2112          * If the write wraps over the top of the circular buffer,
2113          * move the portion up to the wrap point, and reset the
2114          * pointers to the bottom.
2115          */
2116         remain = WQUEUESIZE - head;
2117
2118         if (n >= remain) {
2119                 n -= remain;
2120                 memcpy(ch->ch_wqueue + head, buf, remain);
2121                 dgnc_sniff_nowait_nolock(ch, "USER WRITE", ch->ch_wqueue + head, remain);
2122                 head = 0;
2123                 buf += remain;
2124         }
2125
2126         if (n > 0) {
2127                 /*
2128                  * Move rest of data.
2129                  */
2130                 remain = n;
2131                 memcpy(ch->ch_wqueue + head, buf, remain);
2132                 dgnc_sniff_nowait_nolock(ch, "USER WRITE", ch->ch_wqueue + head, remain);
2133                 head += remain;
2134         }
2135
2136         if (count) {
2137                 head &= tmask;
2138                 ch->ch_w_head = head;
2139         }
2140
2141 #if 0
2142         /*
2143          * If this is the print device, and the
2144          * printer is still on, we need to turn it
2145          * off before going idle.
2146          */
2147         if (count == orig_count) {
2148                 if ((un->un_type == DGNC_PRINT) && (ch->ch_flags & CH_PRON)) {
2149                         head &= tmask;
2150                         ch->ch_w_head = head;
2151                         dgnc_wmove(ch, ch->ch_digi.digi_offstr,
2152                                 (int) ch->ch_digi.digi_offlen);
2153                         head = (ch->ch_w_head) & tmask;
2154                         ch->ch_flags &= ~CH_PRON;
2155                 }
2156         }
2157 #endif
2158
2159         /* Update printer buffer empty time. */
2160         if ((un->un_type == DGNC_PRINT) && (ch->ch_digi.digi_maxcps > 0)
2161             && (ch->ch_digi.digi_bufsize > 0)) {
2162                 ch->ch_cpstime += (HZ * count) / ch->ch_digi.digi_maxcps;
2163         }
2164
2165         if (from_user) {
2166                 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2167                 up(&dgnc_TmpWriteSem);
2168         } else {
2169                 DGNC_UNLOCK(ch->ch_lock, lock_flags);
2170         }
2171
2172         DPR_WRITE(("Write finished - Write %d bytes of %d.\n", count, orig_count));
2173
2174         if (count) {
2175                 /*
2176                  * Channel lock is grabbed and then released
2177                  * inside this routine.
2178                  */
2179                 ch->ch_bd->bd_ops->copy_data_from_queue_to_uart(ch);
2180         }
2181
2182         return count;
2183 }
2184
2185
2186 /*
2187  * Return modem signals to ld.
2188  */
2189
2190 static int dgnc_tty_tiocmget(struct tty_struct *tty)
2191 {
2192         struct channel_t *ch;
2193         struct un_t *un;
2194         int result = -EIO;
2195         uchar mstat = 0;
2196         ulong   lock_flags;
2197
2198         if (!tty || tty->magic != TTY_MAGIC)
2199                 return result;
2200
2201         un = tty->driver_data;
2202         if (!un || un->magic != DGNC_UNIT_MAGIC)
2203                 return result;
2204
2205         ch = un->un_ch;
2206         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2207                 return result;
2208
2209         DPR_IOCTL(("dgnc_tty_tiocmget start\n"));
2210
2211         DGNC_LOCK(ch->ch_lock, lock_flags);
2212
2213         mstat = (ch->ch_mostat | ch->ch_mistat);
2214
2215         DGNC_UNLOCK(ch->ch_lock, lock_flags);
2216
2217         result = 0;
2218
2219         if (mstat & UART_MCR_DTR)
2220                 result |= TIOCM_DTR;
2221         if (mstat & UART_MCR_RTS)
2222                 result |= TIOCM_RTS;
2223         if (mstat & UART_MSR_CTS)
2224                 result |= TIOCM_CTS;
2225         if (mstat & UART_MSR_DSR)
2226                 result |= TIOCM_DSR;
2227         if (mstat & UART_MSR_RI)
2228                 result |= TIOCM_RI;
2229         if (mstat & UART_MSR_DCD)
2230                 result |= TIOCM_CD;
2231
2232         DPR_IOCTL(("dgnc_tty_tiocmget finish\n"));
2233
2234         return result;
2235 }
2236
2237
2238 /*
2239  * dgnc_tty_tiocmset()
2240  *
2241  * Set modem signals, called by ld.
2242  */
2243
2244 static int dgnc_tty_tiocmset(struct tty_struct *tty,
2245                 unsigned int set, unsigned int clear)
2246 {
2247         struct dgnc_board *bd;
2248         struct channel_t *ch;
2249         struct un_t *un;
2250         int ret = -EIO;
2251         ulong   lock_flags;
2252
2253         if (!tty || tty->magic != TTY_MAGIC)
2254                 return ret;
2255
2256         un = tty->driver_data;
2257         if (!un || un->magic != DGNC_UNIT_MAGIC)
2258                 return ret;
2259
2260         ch = un->un_ch;
2261         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2262                 return ret;
2263
2264         bd = ch->ch_bd;
2265         if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2266                 return ret;
2267
2268         DPR_IOCTL(("dgnc_tty_tiocmset start\n"));
2269
2270
2271         DGNC_LOCK(ch->ch_lock, lock_flags);
2272
2273         if (set & TIOCM_RTS) {
2274                 ch->ch_mostat |= UART_MCR_RTS;
2275         }
2276
2277         if (set & TIOCM_DTR) {
2278                 ch->ch_mostat |= UART_MCR_DTR;
2279         }
2280
2281         if (clear & TIOCM_RTS) {
2282                 ch->ch_mostat &= ~(UART_MCR_RTS);
2283         }
2284
2285         if (clear & TIOCM_DTR) {
2286                 ch->ch_mostat &= ~(UART_MCR_DTR);
2287         }
2288
2289         ch->ch_bd->bd_ops->assert_modem_signals(ch);
2290
2291         DGNC_UNLOCK(ch->ch_lock, lock_flags);
2292
2293         DPR_IOCTL(("dgnc_tty_tiocmset finish\n"));
2294
2295         return 0;
2296 }
2297
2298
2299 /*
2300  * dgnc_tty_send_break()
2301  *
2302  * Send a Break, called by ld.
2303  */
2304 static int dgnc_tty_send_break(struct tty_struct *tty, int msec)
2305 {
2306         struct dgnc_board *bd;
2307         struct channel_t *ch;
2308         struct un_t *un;
2309         int ret = -EIO;
2310         ulong   lock_flags;
2311
2312         if (!tty || tty->magic != TTY_MAGIC)
2313                 return ret;
2314
2315         un = tty->driver_data;
2316         if (!un || un->magic != DGNC_UNIT_MAGIC)
2317                 return ret;
2318
2319         ch = un->un_ch;
2320         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2321                 return ret;
2322
2323         bd = ch->ch_bd;
2324         if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2325                 return ret;
2326
2327         switch (msec) {
2328         case -1:
2329                 msec = 0xFFFF;
2330                 break;
2331         case 0:
2332                 msec = 0;
2333                 break;
2334         default:
2335                 break;
2336         }
2337
2338         DPR_IOCTL(("dgnc_tty_send_break start 1.  %lx\n", jiffies));
2339
2340         DGNC_LOCK(ch->ch_lock, lock_flags);
2341
2342         ch->ch_bd->bd_ops->send_break(ch, msec);
2343
2344         DGNC_UNLOCK(ch->ch_lock, lock_flags);
2345
2346         DPR_IOCTL(("dgnc_tty_send_break finish\n"));
2347
2348         return 0;
2349
2350 }
2351
2352
2353 /*
2354  * dgnc_tty_wait_until_sent()
2355  *
2356  * wait until data has been transmitted, called by ld.
2357  */
2358 static void dgnc_tty_wait_until_sent(struct tty_struct *tty, int timeout)
2359 {
2360         struct dgnc_board *bd;
2361         struct channel_t *ch;
2362         struct un_t *un;
2363         int rc;
2364
2365         if (!tty || tty->magic != TTY_MAGIC)
2366                 return;
2367
2368         un = tty->driver_data;
2369         if (!un || un->magic != DGNC_UNIT_MAGIC)
2370                 return;
2371
2372         ch = un->un_ch;
2373         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2374                 return;
2375
2376         bd = ch->ch_bd;
2377         if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2378                 return;
2379
2380         rc = bd->bd_ops->drain(tty, 0);
2381         if (rc) {
2382                 DPR_IOCTL(("dgnc_tty_ioctl - bad return: %d ", rc));
2383                 return;
2384         }
2385         return;
2386 }
2387
2388
2389 /*
2390  * dgnc_send_xchar()
2391  *
2392  * send a high priority character, called by ld.
2393  */
2394 static void dgnc_tty_send_xchar(struct tty_struct *tty, char c)
2395 {
2396         struct dgnc_board *bd;
2397         struct channel_t *ch;
2398         struct un_t *un;
2399         ulong   lock_flags;
2400
2401         if (!tty || tty->magic != TTY_MAGIC)
2402                 return;
2403
2404         un = tty->driver_data;
2405         if (!un || un->magic != DGNC_UNIT_MAGIC)
2406                 return;
2407
2408         ch = un->un_ch;
2409         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2410                 return;
2411
2412         bd = ch->ch_bd;
2413         if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2414                 return;
2415
2416         DPR_IOCTL(("dgnc_tty_send_xchar start\n"));
2417         printk("dgnc_tty_send_xchar start\n");
2418
2419         DGNC_LOCK(ch->ch_lock, lock_flags);
2420         bd->bd_ops->send_immediate_char(ch, c);
2421         DGNC_UNLOCK(ch->ch_lock, lock_flags);
2422
2423         DPR_IOCTL(("dgnc_tty_send_xchar finish\n"));
2424         printk("dgnc_tty_send_xchar finish\n");
2425         return;
2426 }
2427
2428
2429
2430
2431 /*
2432  * Return modem signals to ld.
2433  */
2434 static inline int dgnc_get_mstat(struct channel_t *ch)
2435 {
2436         unsigned char mstat;
2437         int result = -EIO;
2438         ulong   lock_flags;
2439
2440         DPR_IOCTL(("dgnc_getmstat start\n"));
2441
2442         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2443                 return -ENXIO;
2444
2445         DGNC_LOCK(ch->ch_lock, lock_flags);
2446
2447         mstat = (ch->ch_mostat | ch->ch_mistat);
2448
2449         DGNC_UNLOCK(ch->ch_lock, lock_flags);
2450
2451         result = 0;
2452
2453         if (mstat & UART_MCR_DTR)
2454                 result |= TIOCM_DTR;
2455         if (mstat & UART_MCR_RTS)
2456                 result |= TIOCM_RTS;
2457         if (mstat & UART_MSR_CTS)
2458                 result |= TIOCM_CTS;
2459         if (mstat & UART_MSR_DSR)
2460                 result |= TIOCM_DSR;
2461         if (mstat & UART_MSR_RI)
2462                 result |= TIOCM_RI;
2463         if (mstat & UART_MSR_DCD)
2464                 result |= TIOCM_CD;
2465
2466         DPR_IOCTL(("dgnc_getmstat finish\n"));
2467
2468         return result;
2469 }
2470
2471
2472
2473 /*
2474  * Return modem signals to ld.
2475  */
2476 static int dgnc_get_modem_info(struct channel_t *ch, unsigned int  __user *value)
2477 {
2478         int result;
2479         int rc;
2480
2481         DPR_IOCTL(("dgnc_get_modem_info start\n"));
2482
2483         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2484                 return -ENXIO;
2485
2486         result = dgnc_get_mstat(ch);
2487
2488         if (result < 0)
2489                 return -ENXIO;
2490
2491         rc = put_user(result, value);
2492
2493         DPR_IOCTL(("dgnc_get_modem_info finish\n"));
2494         return rc;
2495 }
2496
2497
2498 /*
2499  * dgnc_set_modem_info()
2500  *
2501  * Set modem signals, called by ld.
2502  */
2503 static int dgnc_set_modem_info(struct tty_struct *tty, unsigned int command, unsigned int __user *value)
2504 {
2505         struct dgnc_board *bd;
2506         struct channel_t *ch;
2507         struct un_t *un;
2508         int ret = -ENXIO;
2509         unsigned int arg = 0;
2510         ulong   lock_flags;
2511
2512         if (!tty || tty->magic != TTY_MAGIC)
2513                 return ret;
2514
2515         un = tty->driver_data;
2516         if (!un || un->magic != DGNC_UNIT_MAGIC)
2517                 return ret;
2518
2519         ch = un->un_ch;
2520         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2521                 return ret;
2522
2523         bd = ch->ch_bd;
2524         if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2525                 return ret;
2526
2527         ret = 0;
2528
2529         DPR_IOCTL(("dgnc_set_modem_info() start\n"));
2530
2531         ret = get_user(arg, value);
2532         if (ret)
2533                 return ret;
2534
2535         switch (command) {
2536         case TIOCMBIS:
2537                 if (arg & TIOCM_RTS) {
2538                         ch->ch_mostat |= UART_MCR_RTS;
2539                 }
2540
2541                 if (arg & TIOCM_DTR) {
2542                         ch->ch_mostat |= UART_MCR_DTR;
2543                 }
2544
2545                 break;
2546
2547         case TIOCMBIC:
2548                 if (arg & TIOCM_RTS) {
2549                         ch->ch_mostat &= ~(UART_MCR_RTS);
2550                 }
2551
2552                 if (arg & TIOCM_DTR) {
2553                         ch->ch_mostat &= ~(UART_MCR_DTR);
2554                 }
2555
2556                 break;
2557
2558         case TIOCMSET:
2559
2560                 if (arg & TIOCM_RTS) {
2561                         ch->ch_mostat |= UART_MCR_RTS;
2562                 }
2563                 else {
2564                         ch->ch_mostat &= ~(UART_MCR_RTS);
2565                 }
2566
2567                 if (arg & TIOCM_DTR) {
2568                         ch->ch_mostat |= UART_MCR_DTR;
2569                 }
2570                 else {
2571                         ch->ch_mostat &= ~(UART_MCR_DTR);
2572                 }
2573
2574                 break;
2575
2576         default:
2577                 return -EINVAL;
2578         }
2579
2580         DGNC_LOCK(ch->ch_lock, lock_flags);
2581
2582         ch->ch_bd->bd_ops->assert_modem_signals(ch);
2583
2584         DGNC_UNLOCK(ch->ch_lock, lock_flags);
2585
2586         DPR_IOCTL(("dgnc_set_modem_info finish\n"));
2587
2588         return 0;
2589 }
2590
2591
2592 /*
2593  * dgnc_tty_digigeta()
2594  *
2595  * Ioctl to get the information for ditty.
2596  *
2597  *
2598  *
2599  */
2600 static int dgnc_tty_digigeta(struct tty_struct *tty, struct digi_t __user *retinfo)
2601 {
2602         struct channel_t *ch;
2603         struct un_t *un;
2604         struct digi_t tmp;
2605         ulong   lock_flags;
2606
2607         if (!retinfo)
2608                 return -EFAULT;
2609
2610         if (!tty || tty->magic != TTY_MAGIC)
2611                 return -EFAULT;
2612
2613         un = tty->driver_data;
2614         if (!un || un->magic != DGNC_UNIT_MAGIC)
2615                 return -EFAULT;
2616
2617         ch = un->un_ch;
2618         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2619                 return -EFAULT;
2620
2621         memset(&tmp, 0, sizeof(tmp));
2622
2623         DGNC_LOCK(ch->ch_lock, lock_flags);
2624         memcpy(&tmp, &ch->ch_digi, sizeof(tmp));
2625         DGNC_UNLOCK(ch->ch_lock, lock_flags);
2626
2627         if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2628                 return -EFAULT;
2629
2630         return 0;
2631 }
2632
2633
2634 /*
2635  * dgnc_tty_digiseta()
2636  *
2637  * Ioctl to set the information for ditty.
2638  *
2639  *
2640  *
2641  */
2642 static int dgnc_tty_digiseta(struct tty_struct *tty, struct digi_t __user *new_info)
2643 {
2644         struct dgnc_board *bd;
2645         struct channel_t *ch;
2646         struct un_t *un;
2647         struct digi_t new_digi;
2648         ulong lock_flags;
2649
2650         DPR_IOCTL(("DIGI_SETA start\n"));
2651
2652         if (!tty || tty->magic != TTY_MAGIC)
2653                 return -EFAULT;
2654
2655         un = tty->driver_data;
2656         if (!un || un->magic != DGNC_UNIT_MAGIC)
2657                 return -EFAULT;
2658
2659         ch = un->un_ch;
2660         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2661                 return -EFAULT;
2662
2663         bd = ch->ch_bd;
2664         if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2665                 return -EFAULT;
2666
2667         if (copy_from_user(&new_digi, new_info, sizeof(new_digi))) {
2668                 DPR_IOCTL(("DIGI_SETA failed copy_from_user\n"));
2669                 return -EFAULT;
2670         }
2671
2672         DGNC_LOCK(ch->ch_lock, lock_flags);
2673
2674         /*
2675          * Handle transistions to and from RTS Toggle.
2676          */
2677         if (!(ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE) && (new_digi.digi_flags & DIGI_RTS_TOGGLE))
2678                 ch->ch_mostat &= ~(UART_MCR_RTS);
2679         if ((ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE) && !(new_digi.digi_flags & DIGI_RTS_TOGGLE))
2680                 ch->ch_mostat |= (UART_MCR_RTS);
2681
2682         /*
2683          * Handle transistions to and from DTR Toggle.
2684          */
2685         if (!(ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE) && (new_digi.digi_flags & DIGI_DTR_TOGGLE))
2686                 ch->ch_mostat &= ~(UART_MCR_DTR);
2687         if ((ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE) && !(new_digi.digi_flags & DIGI_DTR_TOGGLE))
2688                 ch->ch_mostat |= (UART_MCR_DTR);
2689
2690         memcpy(&ch->ch_digi, &new_digi, sizeof(new_digi));
2691
2692         if (ch->ch_digi.digi_maxcps < 1)
2693                 ch->ch_digi.digi_maxcps = 1;
2694
2695         if (ch->ch_digi.digi_maxcps > 10000)
2696                 ch->ch_digi.digi_maxcps = 10000;
2697
2698         if (ch->ch_digi.digi_bufsize < 10)
2699                 ch->ch_digi.digi_bufsize = 10;
2700
2701         if (ch->ch_digi.digi_maxchar < 1)
2702                 ch->ch_digi.digi_maxchar = 1;
2703
2704         if (ch->ch_digi.digi_maxchar > ch->ch_digi.digi_bufsize)
2705                 ch->ch_digi.digi_maxchar = ch->ch_digi.digi_bufsize;
2706
2707         if (ch->ch_digi.digi_onlen > DIGI_PLEN)
2708                 ch->ch_digi.digi_onlen = DIGI_PLEN;
2709
2710         if (ch->ch_digi.digi_offlen > DIGI_PLEN)
2711                 ch->ch_digi.digi_offlen = DIGI_PLEN;
2712
2713         ch->ch_bd->bd_ops->param(tty);
2714
2715         DGNC_UNLOCK(ch->ch_lock, lock_flags);
2716
2717         DPR_IOCTL(("DIGI_SETA finish\n"));
2718
2719         return 0;
2720 }
2721
2722
2723 /*
2724  * dgnc_set_termios()
2725  */
2726 static void dgnc_tty_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
2727 {
2728         struct dgnc_board *bd;
2729         struct channel_t *ch;
2730         struct un_t *un;
2731         unsigned long lock_flags;
2732
2733         if (!tty || tty->magic != TTY_MAGIC)
2734                 return;
2735
2736         un = tty->driver_data;
2737         if (!un || un->magic != DGNC_UNIT_MAGIC)
2738                 return;
2739
2740         ch = un->un_ch;
2741         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2742                 return;
2743
2744         bd = ch->ch_bd;
2745         if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2746                 return;
2747
2748         DGNC_LOCK(ch->ch_lock, lock_flags);
2749
2750         ch->ch_c_cflag   = tty->termios.c_cflag;
2751         ch->ch_c_iflag   = tty->termios.c_iflag;
2752         ch->ch_c_oflag   = tty->termios.c_oflag;
2753         ch->ch_c_lflag   = tty->termios.c_lflag;
2754         ch->ch_startc = tty->termios.c_cc[VSTART];
2755         ch->ch_stopc  = tty->termios.c_cc[VSTOP];
2756
2757         ch->ch_bd->bd_ops->param(tty);
2758         dgnc_carrier(ch);
2759
2760         DGNC_UNLOCK(ch->ch_lock, lock_flags);
2761 }
2762
2763
2764 static void dgnc_tty_throttle(struct tty_struct *tty)
2765 {
2766         struct channel_t *ch;
2767         struct un_t *un;
2768         ulong   lock_flags = 0;
2769
2770         if (!tty || tty->magic != TTY_MAGIC)
2771                 return;
2772
2773         un = tty->driver_data;
2774         if (!un || un->magic != DGNC_UNIT_MAGIC)
2775                 return;
2776
2777         ch = un->un_ch;
2778         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2779                 return;
2780
2781         DPR_IOCTL(("dgnc_tty_throttle start\n"));
2782
2783         DGNC_LOCK(ch->ch_lock, lock_flags);
2784
2785         ch->ch_flags |= (CH_FORCED_STOPI);
2786
2787         DGNC_UNLOCK(ch->ch_lock, lock_flags);
2788
2789         DPR_IOCTL(("dgnc_tty_throttle finish\n"));
2790 }
2791
2792
2793 static void dgnc_tty_unthrottle(struct tty_struct *tty)
2794 {
2795         struct channel_t *ch;
2796         struct un_t *un;
2797         ulong   lock_flags;
2798
2799         if (!tty || tty->magic != TTY_MAGIC)
2800                 return;
2801
2802         un = tty->driver_data;
2803         if (!un || un->magic != DGNC_UNIT_MAGIC)
2804                 return;
2805
2806         ch = un->un_ch;
2807         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2808                 return;
2809
2810         DPR_IOCTL(("dgnc_tty_unthrottle start\n"));
2811
2812         DGNC_LOCK(ch->ch_lock, lock_flags);
2813
2814         ch->ch_flags &= ~(CH_FORCED_STOPI);
2815
2816         DGNC_UNLOCK(ch->ch_lock, lock_flags);
2817
2818         DPR_IOCTL(("dgnc_tty_unthrottle finish\n"));
2819 }
2820
2821
2822 static void dgnc_tty_start(struct tty_struct *tty)
2823 {
2824         struct dgnc_board *bd;
2825         struct channel_t *ch;
2826         struct un_t *un;
2827         ulong lock_flags;
2828
2829         if (!tty || tty->magic != TTY_MAGIC)
2830                 return;
2831
2832         un = tty->driver_data;
2833         if (!un || un->magic != DGNC_UNIT_MAGIC)
2834                 return;
2835
2836         ch = un->un_ch;
2837         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2838                 return;
2839
2840         bd = ch->ch_bd;
2841         if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2842                 return;
2843
2844         DPR_IOCTL(("dgcn_tty_start start\n"));
2845
2846         DGNC_LOCK(ch->ch_lock, lock_flags);
2847
2848         ch->ch_flags &= ~(CH_FORCED_STOP);
2849
2850         DGNC_UNLOCK(ch->ch_lock, lock_flags);
2851
2852         DPR_IOCTL(("dgnc_tty_start finish\n"));
2853 }
2854
2855
2856 static void dgnc_tty_stop(struct tty_struct *tty)
2857 {
2858         struct dgnc_board *bd;
2859         struct channel_t *ch;
2860         struct un_t *un;
2861         ulong lock_flags;
2862
2863         if (!tty || tty->magic != TTY_MAGIC)
2864                 return;
2865
2866         un = tty->driver_data;
2867         if (!un || un->magic != DGNC_UNIT_MAGIC)
2868                 return;
2869
2870         ch = un->un_ch;
2871         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2872                 return;
2873
2874         bd = ch->ch_bd;
2875         if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2876                 return;
2877
2878         DPR_IOCTL(("dgnc_tty_stop start\n"));
2879
2880         DGNC_LOCK(ch->ch_lock, lock_flags);
2881
2882         ch->ch_flags |= (CH_FORCED_STOP);
2883
2884         DGNC_UNLOCK(ch->ch_lock, lock_flags);
2885
2886         DPR_IOCTL(("dgnc_tty_stop finish\n"));
2887 }
2888
2889
2890 /*
2891  * dgnc_tty_flush_chars()
2892  *
2893  * Flush the cook buffer
2894  *
2895  * Note to self, and any other poor souls who venture here:
2896  *
2897  * flush in this case DOES NOT mean dispose of the data.
2898  * instead, it means "stop buffering and send it if you
2899  * haven't already."  Just guess how I figured that out...   SRW 2-Jun-98
2900  *
2901  * It is also always called in interrupt context - JAR 8-Sept-99
2902  */
2903 static void dgnc_tty_flush_chars(struct tty_struct *tty)
2904 {
2905         struct dgnc_board *bd;
2906         struct channel_t *ch;
2907         struct un_t *un;
2908         ulong lock_flags;
2909
2910         if (!tty || tty->magic != TTY_MAGIC)
2911                 return;
2912
2913         un = tty->driver_data;
2914         if (!un || un->magic != DGNC_UNIT_MAGIC)
2915                 return;
2916
2917         ch = un->un_ch;
2918         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2919                 return;
2920
2921         bd = ch->ch_bd;
2922         if (!bd || bd->magic != DGNC_BOARD_MAGIC)
2923                 return;
2924
2925         DPR_IOCTL(("dgnc_tty_flush_chars start\n"));
2926
2927         DGNC_LOCK(ch->ch_lock, lock_flags);
2928
2929         /* Do something maybe here */
2930
2931         DGNC_UNLOCK(ch->ch_lock, lock_flags);
2932
2933         DPR_IOCTL(("dgnc_tty_flush_chars finish\n"));
2934 }
2935
2936
2937
2938 /*
2939  * dgnc_tty_flush_buffer()
2940  *
2941  * Flush Tx buffer (make in == out)
2942  */
2943 static void dgnc_tty_flush_buffer(struct tty_struct *tty)
2944 {
2945         struct channel_t *ch;
2946         struct un_t *un;
2947         ulong lock_flags;
2948
2949         if (!tty || tty->magic != TTY_MAGIC)
2950                 return;
2951
2952         un = tty->driver_data;
2953         if (!un || un->magic != DGNC_UNIT_MAGIC)
2954                 return;
2955
2956         ch = un->un_ch;
2957         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
2958                 return;
2959
2960         DPR_IOCTL(("dgnc_tty_flush_buffer on port: %d start\n", ch->ch_portnum));
2961
2962         DGNC_LOCK(ch->ch_lock, lock_flags);
2963
2964         ch->ch_flags &= ~CH_STOP;
2965
2966         /* Flush our write queue */
2967         ch->ch_w_head = ch->ch_w_tail;
2968
2969         /* Flush UARTs transmit FIFO */
2970         ch->ch_bd->bd_ops->flush_uart_write(ch);
2971
2972         if (ch->ch_tun.un_flags & (UN_LOW|UN_EMPTY)) {
2973                 ch->ch_tun.un_flags &= ~(UN_LOW|UN_EMPTY);
2974                 wake_up_interruptible(&ch->ch_tun.un_flags_wait);
2975         }
2976         if (ch->ch_pun.un_flags & (UN_LOW|UN_EMPTY)) {
2977                 ch->ch_pun.un_flags &= ~(UN_LOW|UN_EMPTY);
2978                 wake_up_interruptible(&ch->ch_pun.un_flags_wait);
2979         }
2980
2981         DGNC_UNLOCK(ch->ch_lock, lock_flags);
2982
2983         DPR_IOCTL(("dgnc_tty_flush_buffer finish\n"));
2984 }
2985
2986
2987
2988 /*****************************************************************************
2989  *
2990  * The IOCTL function and all of its helpers
2991  *
2992  *****************************************************************************/
2993
2994 /*
2995  * dgnc_tty_ioctl()
2996  *
2997  * The usual assortment of ioctl's
2998  */
2999 static int dgnc_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
3000                 unsigned long arg)
3001 {
3002         struct dgnc_board *bd;
3003         struct channel_t *ch;
3004         struct un_t *un;
3005         int rc;
3006         ulong lock_flags;
3007         void __user *uarg = (void __user *) arg;
3008
3009         if (!tty || tty->magic != TTY_MAGIC)
3010                 return -ENODEV;
3011
3012         un = tty->driver_data;
3013         if (!un || un->magic != DGNC_UNIT_MAGIC)
3014                 return -ENODEV;
3015
3016         ch = un->un_ch;
3017         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
3018                 return -ENODEV;
3019
3020         bd = ch->ch_bd;
3021         if (!bd || bd->magic != DGNC_BOARD_MAGIC)
3022                 return -ENODEV;
3023
3024         DPR_IOCTL(("dgnc_tty_ioctl start on port %d - cmd %s (%x), arg %lx\n",
3025                 ch->ch_portnum, dgnc_ioctl_name(cmd), cmd, arg));
3026
3027         DGNC_LOCK(ch->ch_lock, lock_flags);
3028
3029         if (un->un_open_count <= 0) {
3030                 DPR_BASIC(("dgnc_tty_ioctl - unit not open.\n"));
3031                 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3032                 return -EIO;
3033         }
3034
3035         switch (cmd) {
3036
3037         /* Here are all the standard ioctl's that we MUST implement */
3038
3039         case TCSBRK:
3040                 /*
3041                  * TCSBRK is SVID version: non-zero arg --> no break
3042                  * this behaviour is exploited by tcdrain().
3043                  *
3044                  * According to POSIX.1 spec (7.2.2.1.2) breaks should be
3045                  * between 0.25 and 0.5 seconds so we'll ask for something
3046                  * in the middle: 0.375 seconds.
3047                  */
3048                 rc = tty_check_change(tty);
3049                 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3050                 if (rc) {
3051                         return rc;
3052                 }
3053
3054                 rc = ch->ch_bd->bd_ops->drain(tty, 0);
3055
3056                 if (rc) {
3057                         DPR_IOCTL(("dgnc_tty_ioctl - bad return: %d ", rc));
3058                         return -EINTR;
3059                 }
3060
3061                 DGNC_LOCK(ch->ch_lock, lock_flags);
3062
3063                 if(((cmd == TCSBRK) && (!arg)) || (cmd == TCSBRKP)) {
3064                         ch->ch_bd->bd_ops->send_break(ch, 250);
3065                 }
3066
3067                 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3068
3069                 DPR_IOCTL(("dgnc_tty_ioctl finish on port %d - cmd %s (%x), arg %lx\n",
3070                         ch->ch_portnum, dgnc_ioctl_name(cmd), cmd, arg));
3071
3072                 return 0;
3073
3074
3075         case TCSBRKP:
3076                 /* support for POSIX tcsendbreak()
3077                  * According to POSIX.1 spec (7.2.2.1.2) breaks should be
3078                  * between 0.25 and 0.5 seconds so we'll ask for something
3079                  * in the middle: 0.375 seconds.
3080                  */
3081                 rc = tty_check_change(tty);
3082                 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3083                 if (rc) {
3084                         return rc;
3085                 }
3086
3087                 rc = ch->ch_bd->bd_ops->drain(tty, 0);
3088                 if (rc) {
3089                         DPR_IOCTL(("dgnc_tty_ioctl - bad return: %d ", rc));
3090                         return -EINTR;
3091                 }
3092
3093                 DGNC_LOCK(ch->ch_lock, lock_flags);
3094
3095                 ch->ch_bd->bd_ops->send_break(ch, 250);
3096
3097                 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3098
3099                 DPR_IOCTL(("dgnc_tty_ioctl finish on port %d - cmd %s (%x), arg %lx\n",
3100                         ch->ch_portnum, dgnc_ioctl_name(cmd), cmd, arg));
3101
3102                 return 0;
3103
3104         case TIOCSBRK:
3105                 rc = tty_check_change(tty);
3106                 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3107                 if (rc) {
3108                         return rc;
3109                 }
3110
3111                 rc = ch->ch_bd->bd_ops->drain(tty, 0);
3112                 if (rc) {
3113                         DPR_IOCTL(("dgnc_tty_ioctl - bad return: %d ", rc));
3114                         return -EINTR;
3115                 }
3116
3117                 DGNC_LOCK(ch->ch_lock, lock_flags);
3118
3119                 ch->ch_bd->bd_ops->send_break(ch, 250);
3120
3121                 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3122
3123                 DPR_IOCTL(("dgnc_tty_ioctl finish on port %d - cmd %s (%x), arg %lx\n",
3124                         ch->ch_portnum, dgnc_ioctl_name(cmd), cmd, arg));
3125
3126                 return 0;
3127
3128         case TIOCCBRK:
3129                 /* Do Nothing */
3130                 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3131                 return 0;
3132
3133         case TIOCGSOFTCAR:
3134
3135                 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3136
3137                 rc = put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *) arg);
3138                 return rc;
3139
3140         case TIOCSSOFTCAR:
3141
3142                 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3143                 rc = get_user(arg, (unsigned long __user *) arg);
3144                 if (rc)
3145                         return rc;
3146
3147                 DGNC_LOCK(ch->ch_lock, lock_flags);
3148                 tty->termios.c_cflag = ((tty->termios.c_cflag & ~CLOCAL) | (arg ? CLOCAL : 0));
3149                 ch->ch_bd->bd_ops->param(tty);
3150                 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3151
3152                 return 0;
3153
3154         case TIOCMGET:
3155                 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3156                 return dgnc_get_modem_info(ch, uarg);
3157
3158         case TIOCMBIS:
3159         case TIOCMBIC:
3160         case TIOCMSET:
3161                 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3162                 return dgnc_set_modem_info(tty, cmd, uarg);
3163
3164                 /*
3165                  * Here are any additional ioctl's that we want to implement
3166                  */
3167
3168         case TCFLSH:
3169                 /*
3170                  * The linux tty driver doesn't have a flush
3171                  * input routine for the driver, assuming all backed
3172                  * up data is in the line disc. buffers.  However,
3173                  * we all know that's not the case.  Here, we
3174                  * act on the ioctl, but then lie and say we didn't
3175                  * so the line discipline will process the flush
3176                  * also.
3177                  */
3178                 rc = tty_check_change(tty);
3179                 if (rc) {
3180                         DGNC_UNLOCK(ch->ch_lock, lock_flags);
3181                         return rc;
3182                 }
3183
3184                 if ((arg == TCIFLUSH) || (arg == TCIOFLUSH)) {
3185                         ch->ch_r_head = ch->ch_r_tail;
3186                         ch->ch_bd->bd_ops->flush_uart_read(ch);
3187                         /* Force queue flow control to be released, if needed */
3188                         dgnc_check_queue_flow_control(ch);
3189                 }
3190
3191                 if ((arg == TCOFLUSH) || (arg == TCIOFLUSH)) {
3192                         if (!(un->un_type == DGNC_PRINT)) {
3193                                 ch->ch_w_head = ch->ch_w_tail;
3194                                 ch->ch_bd->bd_ops->flush_uart_write(ch);
3195
3196                                 if (ch->ch_tun.un_flags & (UN_LOW|UN_EMPTY)) {
3197                                         ch->ch_tun.un_flags &= ~(UN_LOW|UN_EMPTY);
3198                                         wake_up_interruptible(&ch->ch_tun.un_flags_wait);
3199                                 }
3200
3201                                 if (ch->ch_pun.un_flags & (UN_LOW|UN_EMPTY)) {
3202                                         ch->ch_pun.un_flags &= ~(UN_LOW|UN_EMPTY);
3203                                         wake_up_interruptible(&ch->ch_pun.un_flags_wait);
3204                                 }
3205
3206                         }
3207                 }
3208
3209                 /* pretend we didn't recognize this IOCTL */
3210                 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3211                 return -ENOIOCTLCMD;
3212         case TCSETSF:
3213         case TCSETSW:
3214                 /*
3215                  * The linux tty driver doesn't have a flush
3216                  * input routine for the driver, assuming all backed
3217                  * up data is in the line disc. buffers.  However,
3218                  * we all know that's not the case.  Here, we
3219                  * act on the ioctl, but then lie and say we didn't
3220                  * so the line discipline will process the flush
3221                  * also.
3222                  */
3223                 if (cmd == TCSETSF) {
3224                         /* flush rx */
3225                         ch->ch_flags &= ~CH_STOP;
3226                         ch->ch_r_head = ch->ch_r_tail;
3227                         ch->ch_bd->bd_ops->flush_uart_read(ch);
3228                         /* Force queue flow control to be released, if needed */
3229                         dgnc_check_queue_flow_control(ch);
3230                 }
3231
3232                 /* now wait for all the output to drain */
3233                 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3234                 rc = ch->ch_bd->bd_ops->drain(tty, 0);
3235                 if (rc) {
3236                         DPR_IOCTL(("dgnc_tty_ioctl - bad return: %d\n", rc));
3237                         return -EINTR;
3238                 }
3239
3240                 DPR_IOCTL(("dgnc_tty_ioctl finish on port %d - cmd %s (%x), arg %lx\n",
3241                         ch->ch_portnum, dgnc_ioctl_name(cmd), cmd, arg));
3242
3243                 /* pretend we didn't recognize this */
3244                 return -ENOIOCTLCMD;
3245
3246         case TCSETAW:
3247
3248                 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3249                 rc = ch->ch_bd->bd_ops->drain(tty, 0);
3250                 if (rc) {
3251                         DPR_IOCTL(("dgnc_tty_ioctl - bad return: %d ", rc));
3252                         return -EINTR;
3253                 }
3254
3255                 /* pretend we didn't recognize this */
3256                 return -ENOIOCTLCMD;
3257
3258         case TCXONC:
3259                 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3260                 /* Make the ld do it */
3261                 return -ENOIOCTLCMD;
3262
3263         case DIGI_GETA:
3264                 /* get information for ditty */
3265                 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3266                 return dgnc_tty_digigeta(tty, uarg);
3267
3268         case DIGI_SETAW:
3269         case DIGI_SETAF:
3270
3271                 /* set information for ditty */
3272                 if (cmd == (DIGI_SETAW)) {
3273
3274                         DGNC_UNLOCK(ch->ch_lock, lock_flags);
3275                         rc = ch->ch_bd->bd_ops->drain(tty, 0);
3276                         if (rc) {
3277                                 DPR_IOCTL(("dgnc_tty_ioctl - bad return: %d ", rc));
3278                                 return -EINTR;
3279                         }
3280                         DGNC_LOCK(ch->ch_lock, lock_flags);
3281                 }
3282                 else {
3283                         tty_ldisc_flush(tty);
3284                 }
3285                 /* fall thru */
3286
3287         case DIGI_SETA:
3288                 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3289                 return dgnc_tty_digiseta(tty, uarg);
3290
3291         case DIGI_LOOPBACK:
3292                 {
3293                         uint loopback = 0;
3294                         /* Let go of locks when accessing user space, could sleep */
3295                         DGNC_UNLOCK(ch->ch_lock, lock_flags);
3296                         rc = get_user(loopback, (unsigned int __user *) arg);
3297                         if (rc)
3298                                 return rc;
3299                         DGNC_LOCK(ch->ch_lock, lock_flags);
3300
3301                         /* Enable/disable internal loopback for this port */
3302                         if (loopback)
3303                                 ch->ch_flags |= CH_LOOPBACK;
3304                         else
3305                                 ch->ch_flags &= ~(CH_LOOPBACK);
3306
3307                         ch->ch_bd->bd_ops->param(tty);
3308                         DGNC_UNLOCK(ch->ch_lock, lock_flags);
3309                         return 0;
3310                 }
3311
3312         case DIGI_GETCUSTOMBAUD:
3313                 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3314                 rc = put_user(ch->ch_custom_speed, (unsigned int __user *) arg);
3315                 return rc;
3316
3317         case DIGI_SETCUSTOMBAUD:
3318         {
3319                 uint new_rate;
3320                 /* Let go of locks when accessing user space, could sleep */
3321                 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3322                 rc = get_user(new_rate, (unsigned int __user *) arg);
3323                 if (rc)
3324                         return rc;
3325                 DGNC_LOCK(ch->ch_lock, lock_flags);
3326                 dgnc_set_custom_speed(ch, new_rate);
3327                 ch->ch_bd->bd_ops->param(tty);
3328                 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3329                 return 0;
3330         }
3331
3332         /*
3333          * This ioctl allows insertion of a character into the front
3334          * of any pending data to be transmitted.
3335          *
3336          * This ioctl is to satify the "Send Character Immediate"
3337          * call that the RealPort protocol spec requires.
3338          */
3339         case DIGI_REALPORT_SENDIMMEDIATE:
3340         {
3341                 unsigned char c;
3342                 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3343                 rc = get_user(c, (unsigned char __user *) arg);
3344                 if (rc)
3345                         return rc;
3346                 DGNC_LOCK(ch->ch_lock, lock_flags);
3347                 ch->ch_bd->bd_ops->send_immediate_char(ch, c);
3348                 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3349                 return 0;
3350         }
3351
3352         /*
3353          * This ioctl returns all the current counts for the port.
3354          *
3355          * This ioctl is to satify the "Line Error Counters"
3356          * call that the RealPort protocol spec requires.
3357          */
3358         case DIGI_REALPORT_GETCOUNTERS:
3359         {
3360                 struct digi_getcounter buf;
3361
3362                 buf.norun = ch->ch_err_overrun;
3363                 buf.noflow = 0;         /* The driver doesn't keep this stat */
3364                 buf.nframe = ch->ch_err_frame;
3365                 buf.nparity = ch->ch_err_parity;
3366                 buf.nbreak = ch->ch_err_break;
3367                 buf.rbytes = ch->ch_rxcount;
3368                 buf.tbytes = ch->ch_txcount;
3369
3370                 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3371
3372                 if (copy_to_user(uarg, &buf, sizeof(buf))) {
3373                         return -EFAULT;
3374                 }
3375                 return 0;
3376         }
3377
3378         /*
3379          * This ioctl returns all current events.
3380          *
3381          * This ioctl is to satify the "Event Reporting"
3382          * call that the RealPort protocol spec requires.
3383          */
3384         case DIGI_REALPORT_GETEVENTS:
3385         {
3386                 unsigned int events = 0;
3387
3388                 /* NOTE: MORE EVENTS NEEDS TO BE ADDED HERE */
3389                 if (ch->ch_flags & CH_BREAK_SENDING)
3390                         events |= EV_TXB;
3391                 if ((ch->ch_flags & CH_STOP) || (ch->ch_flags & CH_FORCED_STOP)) {
3392                         events |= (EV_OPU | EV_OPS);
3393                 }
3394                 if ((ch->ch_flags & CH_STOPI) || (ch->ch_flags & CH_FORCED_STOPI)) {
3395                         events |= (EV_IPU | EV_IPS);
3396                 }
3397
3398                 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3399                 rc = put_user(events, (unsigned int __user *) arg);
3400                 return rc;
3401         }
3402
3403         /*
3404          * This ioctl returns TOUT and TIN counters based
3405          * upon the values passed in by the RealPort Server.
3406          * It also passes back whether the UART Transmitter is
3407          * empty as well.
3408          */
3409         case DIGI_REALPORT_GETBUFFERS:
3410         {
3411                 struct digi_getbuffer buf;
3412                 int tdist;
3413                 int count;
3414
3415                 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3416
3417                 /*
3418                  * Get data from user first.
3419                  */
3420                 if (copy_from_user(&buf, uarg, sizeof(buf))) {
3421                         return -EFAULT;
3422                 }
3423
3424                 DGNC_LOCK(ch->ch_lock, lock_flags);
3425
3426                 /*
3427                  * Figure out how much data is in our RX and TX queues.
3428                  */
3429                 buf.rxbuf = (ch->ch_r_head - ch->ch_r_tail) & RQUEUEMASK;
3430                 buf.txbuf = (ch->ch_w_head - ch->ch_w_tail) & WQUEUEMASK;
3431
3432                 /*
3433                  * Is the UART empty? Add that value to whats in our TX queue.
3434                  */
3435                 count = buf.txbuf + ch->ch_bd->bd_ops->get_uart_bytes_left(ch);
3436
3437                 /*
3438                  * Figure out how much data the RealPort Server believes should
3439                  * be in our TX queue.
3440                  */
3441                 tdist = (buf.tIn - buf.tOut) & 0xffff;
3442
3443                 /*
3444                  * If we have more data than the RealPort Server believes we
3445                  * should have, reduce our count to its amount.
3446                  *
3447                  * This count difference CAN happen because the Linux LD can
3448                  * insert more characters into our queue for OPOST processing
3449                  * that the RealPort Server doesn't know about.
3450                  */
3451                 if (buf.txbuf > tdist) {
3452                         buf.txbuf = tdist;
3453                 }
3454
3455                 /*
3456                  * Report whether our queue and UART TX are completely empty.
3457                  */
3458                 if (count) {
3459                         buf.txdone = 0;
3460                 } else {
3461                         buf.txdone = 1;
3462                 }
3463
3464                 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3465
3466                 if (copy_to_user(uarg, &buf, sizeof(buf))) {
3467                         return -EFAULT;
3468                 }
3469                 return 0;
3470         }
3471         default:
3472                 DGNC_UNLOCK(ch->ch_lock, lock_flags);
3473
3474                 DPR_IOCTL(("dgnc_tty_ioctl - in default\n"));
3475                 DPR_IOCTL(("dgnc_tty_ioctl end - cmd %s (%x), arg %lx\n",
3476                         dgnc_ioctl_name(cmd), cmd, arg));
3477
3478                 return -ENOIOCTLCMD;
3479         }
3480
3481         DGNC_UNLOCK(ch->ch_lock, lock_flags);
3482
3483         DPR_IOCTL(("dgnc_tty_ioctl end - cmd %s (%x), arg %lx\n",
3484                 dgnc_ioctl_name(cmd), cmd, arg));
3485
3486         return 0;
3487 }