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