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