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