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