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