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