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