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