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