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