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