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