GNU Linux-libre 4.9-gnu1
[releases.git] / drivers / staging / dgnc / dgnc_neo.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 #include <linux/kernel.h>
17 #include <linux/sched.h>        /* For jiffies, task states */
18 #include <linux/interrupt.h>    /* For tasklet and interrupt structs/defines */
19 #include <linux/delay.h>        /* For udelay */
20 #include <linux/io.h>           /* For read[bwl]/write[bwl] */
21 #include <linux/serial.h>       /* For struct async_serial */
22 #include <linux/serial_reg.h>   /* For the various UART offsets */
23
24 #include "dgnc_driver.h"        /* Driver main header file */
25 #include "dgnc_neo.h"           /* Our header file */
26 #include "dgnc_tty.h"
27
28 static inline void neo_parse_lsr(struct dgnc_board *brd, uint port);
29 static inline void neo_parse_isr(struct dgnc_board *brd, uint port);
30 static void neo_copy_data_from_uart_to_queue(struct channel_t *ch);
31 static inline void neo_clear_break(struct channel_t *ch, int force);
32 static inline void neo_set_cts_flow_control(struct channel_t *ch);
33 static inline void neo_set_rts_flow_control(struct channel_t *ch);
34 static inline void neo_set_ixon_flow_control(struct channel_t *ch);
35 static inline void neo_set_ixoff_flow_control(struct channel_t *ch);
36 static inline void neo_set_no_output_flow_control(struct channel_t *ch);
37 static inline void neo_set_no_input_flow_control(struct channel_t *ch);
38 static inline void neo_set_new_start_stop_chars(struct channel_t *ch);
39 static void neo_parse_modem(struct channel_t *ch, unsigned char signals);
40 static void neo_tasklet(unsigned long data);
41 static void neo_vpd(struct dgnc_board *brd);
42 static void neo_uart_init(struct channel_t *ch);
43 static void neo_uart_off(struct channel_t *ch);
44 static int neo_drain(struct tty_struct *tty, uint seconds);
45 static void neo_param(struct tty_struct *tty);
46 static void neo_assert_modem_signals(struct channel_t *ch);
47 static void neo_flush_uart_write(struct channel_t *ch);
48 static void neo_flush_uart_read(struct channel_t *ch);
49 static void neo_disable_receiver(struct channel_t *ch);
50 static void neo_enable_receiver(struct channel_t *ch);
51 static void neo_send_break(struct channel_t *ch, int msecs);
52 static void neo_send_start_character(struct channel_t *ch);
53 static void neo_send_stop_character(struct channel_t *ch);
54 static void neo_copy_data_from_queue_to_uart(struct channel_t *ch);
55 static uint neo_get_uart_bytes_left(struct channel_t *ch);
56 static void neo_send_immediate_char(struct channel_t *ch, unsigned char c);
57 static irqreturn_t neo_intr(int irq, void *voidbrd);
58
59 struct board_ops dgnc_neo_ops = {
60         .tasklet =                      neo_tasklet,
61         .intr =                         neo_intr,
62         .uart_init =                    neo_uart_init,
63         .uart_off =                     neo_uart_off,
64         .drain =                        neo_drain,
65         .param =                        neo_param,
66         .vpd =                          neo_vpd,
67         .assert_modem_signals =         neo_assert_modem_signals,
68         .flush_uart_write =             neo_flush_uart_write,
69         .flush_uart_read =              neo_flush_uart_read,
70         .disable_receiver =             neo_disable_receiver,
71         .enable_receiver =              neo_enable_receiver,
72         .send_break =                   neo_send_break,
73         .send_start_character =         neo_send_start_character,
74         .send_stop_character =          neo_send_stop_character,
75         .copy_data_from_queue_to_uart = neo_copy_data_from_queue_to_uart,
76         .get_uart_bytes_left =          neo_get_uart_bytes_left,
77         .send_immediate_char =          neo_send_immediate_char
78 };
79
80 /*
81  * This function allows calls to ensure that all outstanding
82  * PCI writes have been completed, by doing a PCI read against
83  * a non-destructive, read-only location on the Neo card.
84  *
85  * In this case, we are reading the DVID (Read-only Device Identification)
86  * value of the Neo card.
87  */
88 static inline void neo_pci_posting_flush(struct dgnc_board *bd)
89 {
90         readb(bd->re_map_membase + 0x8D);
91 }
92
93 static inline void neo_set_cts_flow_control(struct channel_t *ch)
94 {
95         unsigned char ier = readb(&ch->ch_neo_uart->ier);
96         unsigned char efr = readb(&ch->ch_neo_uart->efr);
97
98         /* Turn on auto CTS flow control */
99 #if 1
100         ier |= UART_17158_IER_CTSDSR;
101 #else
102         ier &= ~(UART_17158_IER_CTSDSR);
103 #endif
104
105         efr |= (UART_17158_EFR_ECB | UART_17158_EFR_CTSDSR);
106
107         /* Turn off auto Xon flow control */
108         efr &= ~UART_17158_EFR_IXON;
109
110         /* Why? Because Exar's spec says we have to zero it
111          * out before setting it
112          */
113         writeb(0, &ch->ch_neo_uart->efr);
114
115         /* Turn on UART enhanced bits */
116         writeb(efr, &ch->ch_neo_uart->efr);
117
118         /* Turn on table D, with 8 char hi/low watermarks */
119         writeb(UART_17158_FCTR_TRGD | UART_17158_FCTR_RTS_4DELAY,
120                &ch->ch_neo_uart->fctr);
121
122         /* Feed the UART our trigger levels */
123         writeb(8, &ch->ch_neo_uart->tfifo);
124         ch->ch_t_tlevel = 8;
125
126         writeb(ier, &ch->ch_neo_uart->ier);
127
128         neo_pci_posting_flush(ch->ch_bd);
129 }
130
131 static inline void neo_set_rts_flow_control(struct channel_t *ch)
132 {
133         unsigned char ier = readb(&ch->ch_neo_uart->ier);
134         unsigned char efr = readb(&ch->ch_neo_uart->efr);
135
136         /* Turn on auto RTS flow control */
137 #if 1
138         ier |= UART_17158_IER_RTSDTR;
139 #else
140         ier &= ~(UART_17158_IER_RTSDTR);
141 #endif
142         efr |= (UART_17158_EFR_ECB | UART_17158_EFR_RTSDTR);
143
144         /* Turn off auto Xoff flow control */
145         ier &= ~UART_17158_IER_XOFF;
146         efr &= ~UART_17158_EFR_IXOFF;
147
148         /* Why? Because Exar's spec says we have to zero it
149          * out before setting it
150          */
151         writeb(0, &ch->ch_neo_uart->efr);
152
153         /* Turn on UART enhanced bits */
154         writeb(efr, &ch->ch_neo_uart->efr);
155
156         writeb(UART_17158_FCTR_TRGD | UART_17158_FCTR_RTS_4DELAY,
157                &ch->ch_neo_uart->fctr);
158         ch->ch_r_watermark = 4;
159
160         writeb(32, &ch->ch_neo_uart->rfifo);
161         ch->ch_r_tlevel = 32;
162
163         writeb(ier, &ch->ch_neo_uart->ier);
164
165         /*
166          * From the Neo UART spec sheet:
167          * The auto RTS/DTR function must be started by asserting
168          * RTS/DTR# output pin (MCR bit-0 or 1 to logic 1 after
169          * it is enabled.
170          */
171         ch->ch_mostat |= UART_MCR_RTS;
172
173         neo_pci_posting_flush(ch->ch_bd);
174 }
175
176 static inline void neo_set_ixon_flow_control(struct channel_t *ch)
177 {
178         unsigned char ier = readb(&ch->ch_neo_uart->ier);
179         unsigned char efr = readb(&ch->ch_neo_uart->efr);
180
181         /* Turn off auto CTS flow control */
182         ier &= ~UART_17158_IER_CTSDSR;
183         efr &= ~UART_17158_EFR_CTSDSR;
184
185         /* Turn on auto Xon flow control */
186         efr |= (UART_17158_EFR_ECB | UART_17158_EFR_IXON);
187
188         /* Why? Because Exar's spec says we have to zero it
189          * out before setting it
190          */
191         writeb(0, &ch->ch_neo_uart->efr);
192
193         /* Turn on UART enhanced bits */
194         writeb(efr, &ch->ch_neo_uart->efr);
195
196         writeb(UART_17158_FCTR_TRGD | UART_17158_FCTR_RTS_8DELAY,
197                &ch->ch_neo_uart->fctr);
198         ch->ch_r_watermark = 4;
199
200         writeb(32, &ch->ch_neo_uart->rfifo);
201         ch->ch_r_tlevel = 32;
202
203         /* Tell UART what start/stop chars it should be looking for */
204         writeb(ch->ch_startc, &ch->ch_neo_uart->xonchar1);
205         writeb(0, &ch->ch_neo_uart->xonchar2);
206
207         writeb(ch->ch_stopc, &ch->ch_neo_uart->xoffchar1);
208         writeb(0, &ch->ch_neo_uart->xoffchar2);
209
210         writeb(ier, &ch->ch_neo_uart->ier);
211
212         neo_pci_posting_flush(ch->ch_bd);
213 }
214
215 static inline void neo_set_ixoff_flow_control(struct channel_t *ch)
216 {
217         unsigned char ier = readb(&ch->ch_neo_uart->ier);
218         unsigned char efr = readb(&ch->ch_neo_uart->efr);
219
220         /* Turn off auto RTS flow control */
221         ier &= ~UART_17158_IER_RTSDTR;
222         efr &= ~UART_17158_EFR_RTSDTR;
223
224         /* Turn on auto Xoff flow control */
225         ier |= UART_17158_IER_XOFF;
226         efr |= (UART_17158_EFR_ECB | UART_17158_EFR_IXOFF);
227
228         /* Why? Because Exar's spec says we have to zero it
229          * out before setting it
230          */
231         writeb(0, &ch->ch_neo_uart->efr);
232
233         /* Turn on UART enhanced bits */
234         writeb(efr, &ch->ch_neo_uart->efr);
235
236         /* Turn on table D, with 8 char hi/low watermarks */
237         writeb(UART_17158_FCTR_TRGD | UART_17158_FCTR_RTS_8DELAY,
238                &ch->ch_neo_uart->fctr);
239
240         writeb(8, &ch->ch_neo_uart->tfifo);
241         ch->ch_t_tlevel = 8;
242
243         /* Tell UART what start/stop chars it should be looking for */
244         writeb(ch->ch_startc, &ch->ch_neo_uart->xonchar1);
245         writeb(0, &ch->ch_neo_uart->xonchar2);
246
247         writeb(ch->ch_stopc, &ch->ch_neo_uart->xoffchar1);
248         writeb(0, &ch->ch_neo_uart->xoffchar2);
249
250         writeb(ier, &ch->ch_neo_uart->ier);
251
252         neo_pci_posting_flush(ch->ch_bd);
253 }
254
255 static inline void neo_set_no_input_flow_control(struct channel_t *ch)
256 {
257         unsigned char ier = readb(&ch->ch_neo_uart->ier);
258         unsigned char efr = readb(&ch->ch_neo_uart->efr);
259
260         /* Turn off auto RTS flow control */
261         ier &= ~UART_17158_IER_RTSDTR;
262         efr &= ~UART_17158_EFR_RTSDTR;
263
264         /* Turn off auto Xoff flow control */
265         ier &= ~UART_17158_IER_XOFF;
266         if (ch->ch_c_iflag & IXON)
267                 efr &= ~(UART_17158_EFR_IXOFF);
268         else
269                 efr &= ~(UART_17158_EFR_ECB | UART_17158_EFR_IXOFF);
270
271         /* Why? Because Exar's spec says we have to zero
272          * it out before setting it
273          */
274         writeb(0, &ch->ch_neo_uart->efr);
275
276         /* Turn on UART enhanced bits */
277         writeb(efr, &ch->ch_neo_uart->efr);
278
279         /* Turn on table D, with 8 char hi/low watermarks */
280         writeb(UART_17158_FCTR_TRGD | UART_17158_FCTR_RTS_8DELAY,
281                &ch->ch_neo_uart->fctr);
282
283         ch->ch_r_watermark = 0;
284
285         writeb(16, &ch->ch_neo_uart->tfifo);
286         ch->ch_t_tlevel = 16;
287
288         writeb(16, &ch->ch_neo_uart->rfifo);
289         ch->ch_r_tlevel = 16;
290
291         writeb(ier, &ch->ch_neo_uart->ier);
292
293         neo_pci_posting_flush(ch->ch_bd);
294 }
295
296 static inline void neo_set_no_output_flow_control(struct channel_t *ch)
297 {
298         unsigned char ier = readb(&ch->ch_neo_uart->ier);
299         unsigned char efr = readb(&ch->ch_neo_uart->efr);
300
301         /* Turn off auto CTS flow control */
302         ier &= ~UART_17158_IER_CTSDSR;
303         efr &= ~UART_17158_EFR_CTSDSR;
304
305         /* Turn off auto Xon flow control */
306         if (ch->ch_c_iflag & IXOFF)
307                 efr &= ~UART_17158_EFR_IXON;
308         else
309                 efr &= ~(UART_17158_EFR_ECB | UART_17158_EFR_IXON);
310
311         /* Why? Because Exar's spec says we have to zero it
312          * out before setting it
313          */
314         writeb(0, &ch->ch_neo_uart->efr);
315
316         /* Turn on UART enhanced bits */
317         writeb(efr, &ch->ch_neo_uart->efr);
318
319         /* Turn on table D, with 8 char hi/low watermarks */
320         writeb(UART_17158_FCTR_TRGD | UART_17158_FCTR_RTS_8DELAY,
321                &ch->ch_neo_uart->fctr);
322
323         ch->ch_r_watermark = 0;
324
325         writeb(16, &ch->ch_neo_uart->tfifo);
326         ch->ch_t_tlevel = 16;
327
328         writeb(16, &ch->ch_neo_uart->rfifo);
329         ch->ch_r_tlevel = 16;
330
331         writeb(ier, &ch->ch_neo_uart->ier);
332
333         neo_pci_posting_flush(ch->ch_bd);
334 }
335
336 /* change UARTs start/stop chars */
337 static inline void neo_set_new_start_stop_chars(struct channel_t *ch)
338 {
339         /* if hardware flow control is set, then skip this whole thing */
340         if (ch->ch_digi.digi_flags & (CTSPACE | RTSPACE) ||
341             ch->ch_c_cflag & CRTSCTS)
342                 return;
343
344         /* Tell UART what start/stop chars it should be looking for */
345         writeb(ch->ch_startc, &ch->ch_neo_uart->xonchar1);
346         writeb(0, &ch->ch_neo_uart->xonchar2);
347
348         writeb(ch->ch_stopc, &ch->ch_neo_uart->xoffchar1);
349         writeb(0, &ch->ch_neo_uart->xoffchar2);
350
351         neo_pci_posting_flush(ch->ch_bd);
352 }
353
354 /*
355  * No locks are assumed to be held when calling this function.
356  */
357 static inline void neo_clear_break(struct channel_t *ch, int force)
358 {
359         unsigned long flags;
360
361         spin_lock_irqsave(&ch->ch_lock, flags);
362
363         /* Bail if we aren't currently sending a break. */
364         if (!ch->ch_stop_sending_break) {
365                 spin_unlock_irqrestore(&ch->ch_lock, flags);
366                 return;
367         }
368
369         /* Turn break off, and unset some variables */
370         if (ch->ch_flags & CH_BREAK_SENDING) {
371                 if (force ||
372                     time_after_eq(jiffies, ch->ch_stop_sending_break)) {
373                         unsigned char temp = readb(&ch->ch_neo_uart->lcr);
374
375                         writeb((temp & ~UART_LCR_SBC), &ch->ch_neo_uart->lcr);
376                         neo_pci_posting_flush(ch->ch_bd);
377                         ch->ch_flags &= ~(CH_BREAK_SENDING);
378                         ch->ch_stop_sending_break = 0;
379                 }
380         }
381         spin_unlock_irqrestore(&ch->ch_lock, flags);
382 }
383
384 /*
385  * Parse the ISR register.
386  */
387 static inline void neo_parse_isr(struct dgnc_board *brd, uint port)
388 {
389         struct channel_t *ch;
390         unsigned char isr;
391         unsigned char cause;
392         unsigned long flags;
393
394         ch = brd->channels[port];
395         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
396                 return;
397
398         /* Here we try to figure out what caused the interrupt to happen */
399         while (1) {
400                 isr = readb(&ch->ch_neo_uart->isr_fcr);
401
402                 /* Bail if no pending interrupt */
403                 if (isr & UART_IIR_NO_INT)
404                         break;
405
406                 /*
407                  * Yank off the upper 2 bits,
408                  * which just show that the FIFO's are enabled.
409                  */
410                 isr &= ~(UART_17158_IIR_FIFO_ENABLED);
411
412                 if (isr & (UART_17158_IIR_RDI_TIMEOUT | UART_IIR_RDI)) {
413                         /* Read data from uart -> queue */
414                         neo_copy_data_from_uart_to_queue(ch);
415
416                         /* Call our tty layer to enforce queue
417                          * flow control if needed.
418                          */
419                         spin_lock_irqsave(&ch->ch_lock, flags);
420                         dgnc_check_queue_flow_control(ch);
421                         spin_unlock_irqrestore(&ch->ch_lock, flags);
422                 }
423
424                 if (isr & UART_IIR_THRI) {
425                         /* Transfer data (if any) from Write Queue -> UART. */
426                         spin_lock_irqsave(&ch->ch_lock, flags);
427                         ch->ch_flags |= (CH_TX_FIFO_EMPTY | CH_TX_FIFO_LWM);
428                         spin_unlock_irqrestore(&ch->ch_lock, flags);
429                         neo_copy_data_from_queue_to_uart(ch);
430                 }
431
432                 if (isr & UART_17158_IIR_XONXOFF) {
433                         cause = readb(&ch->ch_neo_uart->xoffchar1);
434
435                         /*
436                          * Since the UART detected either an XON or
437                          * XOFF match, we need to figure out which
438                          * one it was, so we can suspend or resume data flow.
439                          */
440                         if (cause == UART_17158_XON_DETECT) {
441                                 /* Is output stopped right now, if so,
442                                  * resume it
443                                  */
444                                 if (brd->channels[port]->ch_flags & CH_STOP) {
445                                         spin_lock_irqsave(&ch->ch_lock,
446                                                           flags);
447                                         ch->ch_flags &= ~(CH_STOP);
448                                         spin_unlock_irqrestore(&ch->ch_lock,
449                                                                flags);
450                                 }
451                         } else if (cause == UART_17158_XOFF_DETECT) {
452                                 if (!(brd->channels[port]->ch_flags &
453                                       CH_STOP)) {
454                                         spin_lock_irqsave(&ch->ch_lock,
455                                                           flags);
456                                         ch->ch_flags |= CH_STOP;
457                                         spin_unlock_irqrestore(&ch->ch_lock,
458                                                                flags);
459                                 }
460                         }
461                 }
462
463                 if (isr & UART_17158_IIR_HWFLOW_STATE_CHANGE) {
464                         /*
465                          * If we get here, this means the hardware is
466                          * doing auto flow control. Check to see whether
467                          * RTS/DTR or CTS/DSR caused this interrupt.
468                          */
469                         cause = readb(&ch->ch_neo_uart->mcr);
470                         /* Which pin is doing auto flow? RTS or DTR? */
471                         if ((cause & 0x4) == 0) {
472                                 if (cause & UART_MCR_RTS) {
473                                         spin_lock_irqsave(&ch->ch_lock,
474                                                           flags);
475                                         ch->ch_mostat |= UART_MCR_RTS;
476                                         spin_unlock_irqrestore(&ch->ch_lock,
477                                                                flags);
478                                 } else {
479                                         spin_lock_irqsave(&ch->ch_lock,
480                                                           flags);
481                                         ch->ch_mostat &= ~(UART_MCR_RTS);
482                                         spin_unlock_irqrestore(&ch->ch_lock,
483                                                                flags);
484                                 }
485                         } else {
486                                 if (cause & UART_MCR_DTR) {
487                                         spin_lock_irqsave(&ch->ch_lock,
488                                                           flags);
489                                         ch->ch_mostat |= UART_MCR_DTR;
490                                         spin_unlock_irqrestore(&ch->ch_lock,
491                                                                flags);
492                                 } else {
493                                         spin_lock_irqsave(&ch->ch_lock,
494                                                           flags);
495                                         ch->ch_mostat &= ~(UART_MCR_DTR);
496                                         spin_unlock_irqrestore(&ch->ch_lock,
497                                                                flags);
498                                 }
499                         }
500                 }
501
502                 /* Parse any modem signal changes */
503                 neo_parse_modem(ch, readb(&ch->ch_neo_uart->msr));
504         }
505 }
506
507 static inline void neo_parse_lsr(struct dgnc_board *brd, uint port)
508 {
509         struct channel_t *ch;
510         int linestatus;
511         unsigned long flags;
512
513         /*
514          * Check to make sure it didn't receive interrupt with a null board
515          * associated or a board pointer that wasn't ours.
516          */
517         if (!brd || brd->magic != DGNC_BOARD_MAGIC)
518                 return;
519
520         if (port >= brd->maxports)
521                 return;
522
523         ch = brd->channels[port];
524         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
525                 return;
526
527         linestatus = readb(&ch->ch_neo_uart->lsr);
528
529         ch->ch_cached_lsr |= linestatus;
530
531         if (ch->ch_cached_lsr & UART_LSR_DR) {
532                 /* Read data from uart -> queue */
533                 neo_copy_data_from_uart_to_queue(ch);
534                 spin_lock_irqsave(&ch->ch_lock, flags);
535                 dgnc_check_queue_flow_control(ch);
536                 spin_unlock_irqrestore(&ch->ch_lock, flags);
537         }
538
539         /*
540          * The next 3 tests should *NOT* happen, as the above test
541          * should encapsulate all 3... At least, thats what Exar says.
542          */
543
544         if (linestatus & UART_LSR_PE)
545                 ch->ch_err_parity++;
546
547         if (linestatus & UART_LSR_FE)
548                 ch->ch_err_frame++;
549
550         if (linestatus & UART_LSR_BI)
551                 ch->ch_err_break++;
552
553         if (linestatus & UART_LSR_OE) {
554                 /*
555                  * Rx Oruns. Exar says that an orun will NOT corrupt
556                  * the FIFO. It will just replace the holding register
557                  * with this new data byte. So basically just ignore this.
558                  * Probably we should eventually have an orun stat in our
559                  * driver...
560                  */
561                 ch->ch_err_overrun++;
562         }
563
564         if (linestatus & UART_LSR_THRE) {
565                 spin_lock_irqsave(&ch->ch_lock, flags);
566                 ch->ch_flags |= (CH_TX_FIFO_EMPTY | CH_TX_FIFO_LWM);
567                 spin_unlock_irqrestore(&ch->ch_lock, flags);
568
569                 /* Transfer data (if any) from Write Queue -> UART. */
570                 neo_copy_data_from_queue_to_uart(ch);
571         } else if (linestatus & UART_17158_TX_AND_FIFO_CLR) {
572                 spin_lock_irqsave(&ch->ch_lock, flags);
573                 ch->ch_flags |= (CH_TX_FIFO_EMPTY | CH_TX_FIFO_LWM);
574                 spin_unlock_irqrestore(&ch->ch_lock, flags);
575
576                 /* Transfer data (if any) from Write Queue -> UART. */
577                 neo_copy_data_from_queue_to_uart(ch);
578         }
579 }
580
581 /*
582  * neo_param()
583  * Send any/all changes to the line to the UART.
584  */
585 static void neo_param(struct tty_struct *tty)
586 {
587         unsigned char lcr = 0;
588         unsigned char uart_lcr = 0;
589         unsigned char ier = 0;
590         unsigned char uart_ier = 0;
591         uint baud = 9600;
592         int quot = 0;
593         struct dgnc_board *bd;
594         struct channel_t *ch;
595         struct un_t   *un;
596
597         if (!tty || tty->magic != TTY_MAGIC)
598                 return;
599
600         un = (struct un_t *)tty->driver_data;
601         if (!un || un->magic != DGNC_UNIT_MAGIC)
602                 return;
603
604         ch = un->un_ch;
605         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
606                 return;
607
608         bd = ch->ch_bd;
609         if (!bd || bd->magic != DGNC_BOARD_MAGIC)
610                 return;
611
612         /*
613          * If baud rate is zero, flush queues, and set mval to drop DTR.
614          */
615         if ((ch->ch_c_cflag & (CBAUD)) == 0) {
616                 ch->ch_r_head = 0;
617                 ch->ch_r_tail = 0;
618                 ch->ch_e_head = 0;
619                 ch->ch_e_tail = 0;
620                 ch->ch_w_head = 0;
621                 ch->ch_w_tail = 0;
622
623                 neo_flush_uart_write(ch);
624                 neo_flush_uart_read(ch);
625
626                 /* The baudrate is B0 so all modem lines are to be dropped. */
627                 ch->ch_flags |= (CH_BAUD0);
628                 ch->ch_mostat &= ~(UART_MCR_RTS | UART_MCR_DTR);
629                 neo_assert_modem_signals(ch);
630                 ch->ch_old_baud = 0;
631                 return;
632
633         } else if (ch->ch_custom_speed) {
634                 baud = ch->ch_custom_speed;
635                 /* Handle transition from B0 */
636                 if (ch->ch_flags & CH_BAUD0) {
637                         ch->ch_flags &= ~(CH_BAUD0);
638
639                         /*
640                          * Bring back up RTS and DTR...
641                          * Also handle RTS or DTR toggle if set.
642                          */
643                         if (!(ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE))
644                                 ch->ch_mostat |= (UART_MCR_RTS);
645                         if (!(ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE))
646                                 ch->ch_mostat |= (UART_MCR_DTR);
647                 }
648         } else {
649                 int iindex = 0;
650                 int jindex = 0;
651
652                 ulong bauds[4][16] = {
653                         { /* slowbaud */
654                                 0,      50,     75,     110,
655                                 134,    150,    200,    300,
656                                 600,    1200,   1800,   2400,
657                                 4800,   9600,   19200,  38400 },
658                         { /* slowbaud & CBAUDEX */
659                                 0,      57600,  115200, 230400,
660                                 460800, 150,    200,    921600,
661                                 600,    1200,   1800,   2400,
662                                 4800,   9600,   19200,  38400 },
663                         { /* fastbaud */
664                                 0,      57600,   76800, 115200,
665                                 131657, 153600, 230400, 460800,
666                                 921600, 1200,   1800,   2400,
667                                 4800,   9600,   19200,  38400 },
668                         { /* fastbaud & CBAUDEX */
669                                 0,      57600,  115200, 230400,
670                                 460800, 150,    200,    921600,
671                                 600,    1200,   1800,   2400,
672                                 4800,   9600,   19200,  38400 }
673                 };
674
675                 /* Only use the TXPrint baud rate if the terminal unit
676                  * is NOT open
677                  */
678                 if (!(ch->ch_tun.un_flags & UN_ISOPEN) &&
679                     (un->un_type == DGNC_PRINT))
680                         baud = C_BAUD(ch->ch_pun.un_tty) & 0xff;
681                 else
682                         baud = C_BAUD(ch->ch_tun.un_tty) & 0xff;
683
684                 if (ch->ch_c_cflag & CBAUDEX)
685                         iindex = 1;
686
687                 if (ch->ch_digi.digi_flags & DIGI_FAST)
688                         iindex += 2;
689
690                 jindex = baud;
691
692                 if ((iindex >= 0) && (iindex < 4) &&
693                     (jindex >= 0) && (jindex < 16))
694                         baud = bauds[iindex][jindex];
695                 else
696                         baud = 0;
697
698                 if (baud == 0)
699                         baud = 9600;
700
701                 /* Handle transition from B0 */
702                 if (ch->ch_flags & CH_BAUD0) {
703                         ch->ch_flags &= ~(CH_BAUD0);
704
705                         /*
706                          * Bring back up RTS and DTR...
707                          * Also handle RTS or DTR toggle if set.
708                          */
709                         if (!(ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE))
710                                 ch->ch_mostat |= (UART_MCR_RTS);
711                         if (!(ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE))
712                                 ch->ch_mostat |= (UART_MCR_DTR);
713                 }
714         }
715
716         if (ch->ch_c_cflag & PARENB)
717                 lcr |= UART_LCR_PARITY;
718
719         if (!(ch->ch_c_cflag & PARODD))
720                 lcr |= UART_LCR_EPAR;
721
722         /*
723          * Not all platforms support mark/space parity,
724          * so this will hide behind an ifdef.
725          */
726 #ifdef CMSPAR
727         if (ch->ch_c_cflag & CMSPAR)
728                 lcr |= UART_LCR_SPAR;
729 #endif
730
731         if (ch->ch_c_cflag & CSTOPB)
732                 lcr |= UART_LCR_STOP;
733
734         switch (ch->ch_c_cflag & CSIZE) {
735         case CS5:
736                 lcr |= UART_LCR_WLEN5;
737                 break;
738         case CS6:
739                 lcr |= UART_LCR_WLEN6;
740                 break;
741         case CS7:
742                 lcr |= UART_LCR_WLEN7;
743                 break;
744         case CS8:
745         default:
746                 lcr |= UART_LCR_WLEN8;
747                 break;
748         }
749
750         uart_ier = readb(&ch->ch_neo_uart->ier);
751         ier = uart_ier;
752
753         uart_lcr = readb(&ch->ch_neo_uart->lcr);
754
755         if (baud == 0)
756                 baud = 9600;
757
758         quot = ch->ch_bd->bd_dividend / baud;
759
760         if (quot != 0 && ch->ch_old_baud != baud) {
761                 ch->ch_old_baud = baud;
762                 writeb(UART_LCR_DLAB, &ch->ch_neo_uart->lcr);
763                 writeb((quot & 0xff), &ch->ch_neo_uart->txrx);
764                 writeb((quot >> 8), &ch->ch_neo_uart->ier);
765                 writeb(lcr, &ch->ch_neo_uart->lcr);
766         }
767
768         if (uart_lcr != lcr)
769                 writeb(lcr, &ch->ch_neo_uart->lcr);
770
771         if (ch->ch_c_cflag & CREAD)
772                 ier |= (UART_IER_RDI | UART_IER_RLSI);
773         else
774                 ier &= ~(UART_IER_RDI | UART_IER_RLSI);
775
776         /*
777          * Have the UART interrupt on modem signal changes ONLY when
778          * we are in hardware flow control mode, or CLOCAL/FORCEDCD is not set.
779          */
780         if ((ch->ch_digi.digi_flags & CTSPACE) ||
781             (ch->ch_digi.digi_flags & RTSPACE) ||
782             (ch->ch_c_cflag & CRTSCTS) ||
783             !(ch->ch_digi.digi_flags & DIGI_FORCEDCD) ||
784             !(ch->ch_c_cflag & CLOCAL))
785                 ier |= UART_IER_MSI;
786         else
787                 ier &= ~UART_IER_MSI;
788
789         ier |= UART_IER_THRI;
790
791         if (ier != uart_ier)
792                 writeb(ier, &ch->ch_neo_uart->ier);
793
794         /* Set new start/stop chars */
795         neo_set_new_start_stop_chars(ch);
796
797         if (ch->ch_digi.digi_flags & CTSPACE || ch->ch_c_cflag & CRTSCTS) {
798                 neo_set_cts_flow_control(ch);
799         } else if (ch->ch_c_iflag & IXON) {
800                 /* If start/stop is set to disable, then we should
801                  * disable flow control
802                  */
803                 if ((ch->ch_startc == _POSIX_VDISABLE) ||
804                     (ch->ch_stopc == _POSIX_VDISABLE))
805                         neo_set_no_output_flow_control(ch);
806                 else
807                         neo_set_ixon_flow_control(ch);
808         } else {
809                 neo_set_no_output_flow_control(ch);
810         }
811
812         if (ch->ch_digi.digi_flags & RTSPACE || ch->ch_c_cflag & CRTSCTS) {
813                 neo_set_rts_flow_control(ch);
814         } else if (ch->ch_c_iflag & IXOFF) {
815                 /* If start/stop is set to disable, then we should
816                  * disable flow control
817                  */
818                 if ((ch->ch_startc == _POSIX_VDISABLE) ||
819                     (ch->ch_stopc == _POSIX_VDISABLE))
820                         neo_set_no_input_flow_control(ch);
821                 else
822                         neo_set_ixoff_flow_control(ch);
823         } else {
824                 neo_set_no_input_flow_control(ch);
825         }
826
827         /*
828          * Adjust the RX FIFO Trigger level if baud is less than 9600.
829          * Not exactly elegant, but this is needed because of the Exar chip's
830          * delay on firing off the RX FIFO interrupt on slower baud rates.
831          */
832         if (baud < 9600) {
833                 writeb(1, &ch->ch_neo_uart->rfifo);
834                 ch->ch_r_tlevel = 1;
835         }
836
837         neo_assert_modem_signals(ch);
838
839         /* Get current status of the modem signals now */
840         neo_parse_modem(ch, readb(&ch->ch_neo_uart->msr));
841 }
842
843 /*
844  * Our board poller function.
845  */
846 static void neo_tasklet(unsigned long data)
847 {
848         struct dgnc_board *bd = (struct dgnc_board *)data;
849         struct channel_t *ch;
850         unsigned long flags;
851         int i;
852         int state = 0;
853         int ports = 0;
854
855         if (!bd || bd->magic != DGNC_BOARD_MAGIC)
856                 return;
857
858         /* Cache a couple board values */
859         spin_lock_irqsave(&bd->bd_lock, flags);
860         state = bd->state;
861         ports = bd->nasync;
862         spin_unlock_irqrestore(&bd->bd_lock, flags);
863
864         /*
865          * Do NOT allow the interrupt routine to read the intr registers
866          * Until we release this lock.
867          */
868         spin_lock_irqsave(&bd->bd_intr_lock, flags);
869
870         /*
871          * If board is ready, parse deeper to see if there is anything to do.
872          */
873         if ((state == BOARD_READY) && (ports > 0)) {
874                 /* Loop on each port */
875                 for (i = 0; i < ports; i++) {
876                         ch = bd->channels[i];
877
878                         /* Just being careful... */
879                         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
880                                 continue;
881
882                         /*
883                          * NOTE: Remember you CANNOT hold any channel
884                          * locks when calling the input routine.
885                          *
886                          * During input processing, its possible we
887                          * will call the Linux ld, which might in turn,
888                          * do a callback right back into us, resulting
889                          * in us trying to grab the channel lock twice!
890                          */
891                         dgnc_input(ch);
892
893                         /*
894                          * Channel lock is grabbed and then released
895                          * inside both of these routines, but neither
896                          * call anything else that could call back into us.
897                          */
898                         neo_copy_data_from_queue_to_uart(ch);
899                         dgnc_wakeup_writes(ch);
900
901                         /*
902                          * Call carrier carrier function, in case something
903                          * has changed.
904                          */
905                         dgnc_carrier(ch);
906
907                         /*
908                          * Check to see if we need to turn off a sending break.
909                          * The timing check is done inside clear_break()
910                          */
911                         if (ch->ch_stop_sending_break)
912                                 neo_clear_break(ch, 0);
913                 }
914         }
915
916         /* Allow interrupt routine to access the interrupt register again */
917         spin_unlock_irqrestore(&bd->bd_intr_lock, flags);
918 }
919
920 /*
921  * dgnc_neo_intr()
922  *
923  * Neo specific interrupt handler.
924  */
925 static irqreturn_t neo_intr(int irq, void *voidbrd)
926 {
927         struct dgnc_board *brd = voidbrd;
928         struct channel_t *ch;
929         int port = 0;
930         int type;
931         u32 uart_poll;
932         unsigned long flags;
933         unsigned long flags2;
934
935         /*
936          * Check to make sure it didn't receive interrupt with a null board
937          * associated or a board pointer that wasn't ours.
938          */
939         if (!brd || brd->magic != DGNC_BOARD_MAGIC)
940                 return IRQ_NONE;
941
942         /* Lock out the slow poller from running on this board. */
943         spin_lock_irqsave(&brd->bd_intr_lock, flags);
944
945         /*
946          * Read in "extended" IRQ information from the 32bit Neo register.
947          * Bits 0-7: What port triggered the interrupt.
948          * Bits 8-31: Each 3bits indicate what type of interrupt occurred.
949          */
950         uart_poll = readl(brd->re_map_membase + UART_17158_POLL_ADDR_OFFSET);
951
952         /*
953          * If 0, no interrupts pending.
954          * This can happen if the IRQ is shared among a couple Neo/Classic
955          * boards.
956          */
957         if (!uart_poll) {
958                 spin_unlock_irqrestore(&brd->bd_intr_lock, flags);
959                 return IRQ_NONE;
960         }
961
962         /*
963          * At this point, we have at least SOMETHING to service, dig
964          * further...
965          */
966
967         /* Loop on each port */
968         while ((uart_poll & 0xff) != 0) {
969                 type = uart_poll >> (8 + (port * 3));
970                 type &= 0x7;
971
972                 uart_poll &= ~(0x01 << port);
973
974                 /* Switch on type of interrupt we have */
975                 switch (type) {
976                 case UART_17158_RXRDY_TIMEOUT:
977                         /*
978                          * RXRDY Time-out is cleared by reading data in the
979                          * RX FIFO until it falls below the trigger level.
980                          */
981
982                         /* Verify the port is in range. */
983                         if (port >= brd->nasync)
984                                 break;
985
986                         ch = brd->channels[port];
987                         neo_copy_data_from_uart_to_queue(ch);
988
989                         /*
990                          * Call our tty layer to enforce queue flow control if
991                          * needed.
992                          */
993                         spin_lock_irqsave(&ch->ch_lock, flags2);
994                         dgnc_check_queue_flow_control(ch);
995                         spin_unlock_irqrestore(&ch->ch_lock, flags2);
996
997                         break;
998
999                 case UART_17158_RX_LINE_STATUS:
1000                         /*
1001                          * RXRDY and RX LINE Status (logic OR of LSR[4:1])
1002                          */
1003                         neo_parse_lsr(brd, port);
1004                         break;
1005
1006                 case UART_17158_TXRDY:
1007                         /*
1008                          * TXRDY interrupt clears after reading ISR register
1009                          * for the UART channel.
1010                          */
1011
1012                         /*
1013                          * Yes, this is odd...
1014                          * Why would I check EVERY possibility of type of
1015                          * interrupt, when we know its TXRDY???
1016                          * Becuz for some reason, even tho we got triggered for
1017                          * TXRDY, it seems to be occasionally wrong. Instead of
1018                          * TX, which it should be, I was getting things like
1019                          * RXDY too. Weird.
1020                          */
1021                         neo_parse_isr(brd, port);
1022                         break;
1023
1024                 case UART_17158_MSR:
1025                         /*
1026                          * MSR or flow control was seen.
1027                          */
1028                         neo_parse_isr(brd, port);
1029                         break;
1030
1031                 default:
1032                         /*
1033                          * The UART triggered us with a bogus interrupt type.
1034                          * It appears the Exar chip, when REALLY bogged down,
1035                          * will throw these once and awhile.
1036                          * Its harmless, just ignore it and move on.
1037                          */
1038                         break;
1039                 }
1040
1041                 port++;
1042         }
1043
1044         /*
1045          * Schedule tasklet to more in-depth servicing at a better time.
1046          */
1047         tasklet_schedule(&brd->helper_tasklet);
1048
1049         spin_unlock_irqrestore(&brd->bd_intr_lock, flags);
1050
1051         return IRQ_HANDLED;
1052 }
1053
1054 /*
1055  * Neo specific way of turning off the receiver.
1056  * Used as a way to enforce queue flow control when in
1057  * hardware flow control mode.
1058  */
1059 static void neo_disable_receiver(struct channel_t *ch)
1060 {
1061         unsigned char tmp = readb(&ch->ch_neo_uart->ier);
1062
1063         tmp &= ~(UART_IER_RDI);
1064         writeb(tmp, &ch->ch_neo_uart->ier);
1065         neo_pci_posting_flush(ch->ch_bd);
1066 }
1067
1068 /*
1069  * Neo specific way of turning on the receiver.
1070  * Used as a way to un-enforce queue flow control when in
1071  * hardware flow control mode.
1072  */
1073 static void neo_enable_receiver(struct channel_t *ch)
1074 {
1075         unsigned char tmp = readb(&ch->ch_neo_uart->ier);
1076
1077         tmp |= (UART_IER_RDI);
1078         writeb(tmp, &ch->ch_neo_uart->ier);
1079         neo_pci_posting_flush(ch->ch_bd);
1080 }
1081
1082 static void neo_copy_data_from_uart_to_queue(struct channel_t *ch)
1083 {
1084         int qleft = 0;
1085         unsigned char linestatus = 0;
1086         unsigned char error_mask = 0;
1087         int n = 0;
1088         int total = 0;
1089         ushort head;
1090         ushort tail;
1091         unsigned long flags;
1092
1093         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1094                 return;
1095
1096         spin_lock_irqsave(&ch->ch_lock, flags);
1097
1098         /* cache head and tail of queue */
1099         head = ch->ch_r_head & RQUEUEMASK;
1100         tail = ch->ch_r_tail & RQUEUEMASK;
1101
1102         /* Get our cached LSR */
1103         linestatus = ch->ch_cached_lsr;
1104         ch->ch_cached_lsr = 0;
1105
1106         /* Store how much space we have left in the queue */
1107         qleft = tail - head - 1;
1108         if (qleft < 0)
1109                 qleft += RQUEUEMASK + 1;
1110
1111         /*
1112          * If the UART is not in FIFO mode, force the FIFO copy to
1113          * NOT be run, by setting total to 0.
1114          *
1115          * On the other hand, if the UART IS in FIFO mode, then ask
1116          * the UART to give us an approximation of data it has RX'ed.
1117          */
1118         if (!(ch->ch_flags & CH_FIFO_ENABLED)) {
1119                 total = 0;
1120         } else {
1121                 total = readb(&ch->ch_neo_uart->rfifo);
1122
1123                 /*
1124                  * EXAR chip bug - RX FIFO COUNT - Fudge factor.
1125                  *
1126                  * This resolves a problem/bug with the Exar chip that sometimes
1127                  * returns a bogus value in the rfifo register.
1128                  * The count can be any where from 0-3 bytes "off".
1129                  * Bizarre, but true.
1130                  */
1131                 if ((ch->ch_bd->dvid & 0xf0) >= UART_XR17E158_DVID)
1132                         total -= 1;
1133                 else
1134                         total -= 3;
1135         }
1136
1137         /*
1138          * Finally, bound the copy to make sure we don't overflow
1139          * our own queue...
1140          * The byte by byte copy loop below this loop this will
1141          * deal with the queue overflow possibility.
1142          */
1143         total = min(total, qleft);
1144
1145         while (total > 0) {
1146                 /*
1147                  * Grab the linestatus register, we need to check
1148                  * to see if there are any errors in the FIFO.
1149                  */
1150                 linestatus = readb(&ch->ch_neo_uart->lsr);
1151
1152                 /*
1153                  * Break out if there is a FIFO error somewhere.
1154                  * This will allow us to go byte by byte down below,
1155                  * finding the exact location of the error.
1156                  */
1157                 if (linestatus & UART_17158_RX_FIFO_DATA_ERROR)
1158                         break;
1159
1160                 /* Make sure we don't go over the end of our queue */
1161                 n = min(((uint)total), (RQUEUESIZE - (uint)head));
1162
1163                 /*
1164                  * Cut down n even further if needed, this is to fix
1165                  * a problem with memcpy_fromio() with the Neo on the
1166                  * IBM pSeries platform.
1167                  * 15 bytes max appears to be the magic number.
1168                  */
1169                 n = min_t(uint, n, 12);
1170
1171                 /*
1172                  * Since we are grabbing the linestatus register, which
1173                  * will reset some bits after our read, we need to ensure
1174                  * we don't miss our TX FIFO emptys.
1175                  */
1176                 if (linestatus & (UART_LSR_THRE | UART_17158_TX_AND_FIFO_CLR))
1177                         ch->ch_flags |= (CH_TX_FIFO_EMPTY | CH_TX_FIFO_LWM);
1178
1179                 linestatus = 0;
1180
1181                 /* Copy data from uart to the queue */
1182                 memcpy_fromio(ch->ch_rqueue + head,
1183                               &ch->ch_neo_uart->txrxburst, n);
1184
1185                 /*
1186                  * Since RX_FIFO_DATA_ERROR was 0, we are guaranteed
1187                  * that all the data currently in the FIFO is free of
1188                  * breaks and parity/frame/orun errors.
1189                  */
1190                 memset(ch->ch_equeue + head, 0, n);
1191
1192                 /* Add to and flip head if needed */
1193                 head = (head + n) & RQUEUEMASK;
1194                 total -= n;
1195                 qleft -= n;
1196                 ch->ch_rxcount += n;
1197         }
1198
1199         /*
1200          * Create a mask to determine whether we should
1201          * insert the character (if any) into our queue.
1202          */
1203         if (ch->ch_c_iflag & IGNBRK)
1204                 error_mask |= UART_LSR_BI;
1205
1206         /*
1207          * Now cleanup any leftover bytes still in the UART.
1208          * Also deal with any possible queue overflow here as well.
1209          */
1210         while (1) {
1211                 /*
1212                  * Its possible we have a linestatus from the loop above
1213                  * this, so we "OR" on any extra bits.
1214                  */
1215                 linestatus |= readb(&ch->ch_neo_uart->lsr);
1216
1217                 /*
1218                  * If the chip tells us there is no more data pending to
1219                  * be read, we can then leave.
1220                  * But before we do, cache the linestatus, just in case.
1221                  */
1222                 if (!(linestatus & UART_LSR_DR)) {
1223                         ch->ch_cached_lsr = linestatus;
1224                         break;
1225                 }
1226
1227                 /* No need to store this bit */
1228                 linestatus &= ~UART_LSR_DR;
1229
1230                 /*
1231                  * Since we are grabbing the linestatus register, which
1232                  * will reset some bits after our read, we need to ensure
1233                  * we don't miss our TX FIFO emptys.
1234                  */
1235                 if (linestatus & (UART_LSR_THRE | UART_17158_TX_AND_FIFO_CLR)) {
1236                         linestatus &= ~(UART_LSR_THRE |
1237                                         UART_17158_TX_AND_FIFO_CLR);
1238                         ch->ch_flags |= (CH_TX_FIFO_EMPTY | CH_TX_FIFO_LWM);
1239                 }
1240
1241                 /*
1242                  * Discard character if we are ignoring the error mask.
1243                  */
1244                 if (linestatus & error_mask)  {
1245                         unsigned char discard;
1246
1247                         linestatus = 0;
1248                         memcpy_fromio(&discard, &ch->ch_neo_uart->txrxburst, 1);
1249                         continue;
1250                 }
1251
1252                 /*
1253                  * If our queue is full, we have no choice but to drop some
1254                  * data.
1255                  * The assumption is that HWFLOW or SWFLOW should have stopped
1256                  * things way way before we got to this point.
1257                  *
1258                  * I decided that I wanted to ditch the oldest data first,
1259                  * I hope thats okay with everyone? Yes? Good.
1260                  */
1261                 while (qleft < 1) {
1262                         tail = (tail + 1) & RQUEUEMASK;
1263                         ch->ch_r_tail = tail;
1264                         ch->ch_err_overrun++;
1265                         qleft++;
1266                 }
1267
1268                 memcpy_fromio(ch->ch_rqueue + head,
1269                               &ch->ch_neo_uart->txrxburst, 1);
1270                 ch->ch_equeue[head] = (unsigned char)linestatus;
1271
1272                 /* Ditch any remaining linestatus value. */
1273                 linestatus = 0;
1274
1275                 /* Add to and flip head if needed */
1276                 head = (head + 1) & RQUEUEMASK;
1277
1278                 qleft--;
1279                 ch->ch_rxcount++;
1280         }
1281
1282         /*
1283          * Write new final heads to channel structure.
1284          */
1285         ch->ch_r_head = head & RQUEUEMASK;
1286         ch->ch_e_head = head & EQUEUEMASK;
1287
1288         spin_unlock_irqrestore(&ch->ch_lock, flags);
1289 }
1290
1291 /*
1292  * This function basically goes to sleep for secs, or until
1293  * it gets signalled that the port has fully drained.
1294  */
1295 static int neo_drain(struct tty_struct *tty, uint seconds)
1296 {
1297         unsigned long flags;
1298         struct channel_t *ch;
1299         struct un_t *un;
1300         int rc = 0;
1301
1302         if (!tty || tty->magic != TTY_MAGIC)
1303                 return -ENXIO;
1304
1305         un = (struct un_t *)tty->driver_data;
1306         if (!un || un->magic != DGNC_UNIT_MAGIC)
1307                 return -ENXIO;
1308
1309         ch = un->un_ch;
1310         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1311                 return -ENXIO;
1312
1313         spin_lock_irqsave(&ch->ch_lock, flags);
1314         un->un_flags |= UN_EMPTY;
1315         spin_unlock_irqrestore(&ch->ch_lock, flags);
1316
1317         /*
1318          * Go to sleep waiting for the tty layer to wake me back up when
1319          * the empty flag goes away.
1320          */
1321         rc = wait_event_interruptible_timeout(un->un_flags_wait,
1322                                               ((un->un_flags & UN_EMPTY) == 0),
1323                                               msecs_to_jiffies(seconds * 1000));
1324
1325         /* If ret is non-zero, user ctrl-c'ed us */
1326         return rc;
1327 }
1328
1329 /*
1330  * Flush the WRITE FIFO on the Neo.
1331  *
1332  * NOTE: Channel lock MUST be held before calling this function!
1333  */
1334 static void neo_flush_uart_write(struct channel_t *ch)
1335 {
1336         unsigned char tmp = 0;
1337         int i = 0;
1338
1339         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1340                 return;
1341
1342         writeb((UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_XMIT),
1343                &ch->ch_neo_uart->isr_fcr);
1344         neo_pci_posting_flush(ch->ch_bd);
1345
1346         for (i = 0; i < 10; i++) {
1347                 /*
1348                  * Check to see if the UART feels it completely flushed the
1349                  * FIFO.
1350                  */
1351                 tmp = readb(&ch->ch_neo_uart->isr_fcr);
1352                 if (tmp & 4)
1353                         udelay(10);
1354                 else
1355                         break;
1356         }
1357
1358         ch->ch_flags |= (CH_TX_FIFO_EMPTY | CH_TX_FIFO_LWM);
1359 }
1360
1361 /*
1362  * Flush the READ FIFO on the Neo.
1363  *
1364  * NOTE: Channel lock MUST be held before calling this function!
1365  */
1366 static void neo_flush_uart_read(struct channel_t *ch)
1367 {
1368         unsigned char tmp = 0;
1369         int i = 0;
1370
1371         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1372                 return;
1373
1374         writeb(UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR,
1375                &ch->ch_neo_uart->isr_fcr);
1376         neo_pci_posting_flush(ch->ch_bd);
1377
1378         for (i = 0; i < 10; i++) {
1379                 /*
1380                  * Check to see if the UART feels it completely flushed the
1381                  * FIFO.
1382                  */
1383                 tmp = readb(&ch->ch_neo_uart->isr_fcr);
1384                 if (tmp & 2)
1385                         udelay(10);
1386                 else
1387                         break;
1388         }
1389 }
1390
1391 static void neo_copy_data_from_queue_to_uart(struct channel_t *ch)
1392 {
1393         ushort head;
1394         ushort tail;
1395         int n;
1396         int s;
1397         int qlen;
1398         uint len_written = 0;
1399         unsigned long flags;
1400
1401         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1402                 return;
1403
1404         spin_lock_irqsave(&ch->ch_lock, flags);
1405
1406         /* No data to write to the UART */
1407         if (ch->ch_w_tail == ch->ch_w_head)
1408                 goto exit_unlock;
1409
1410         /* If port is "stopped", don't send any data to the UART */
1411         if ((ch->ch_flags & CH_FORCED_STOP) ||
1412             (ch->ch_flags & CH_BREAK_SENDING))
1413                 goto exit_unlock;
1414
1415         /*
1416          * If FIFOs are disabled. Send data directly to txrx register
1417          */
1418         if (!(ch->ch_flags & CH_FIFO_ENABLED)) {
1419                 unsigned char lsrbits = readb(&ch->ch_neo_uart->lsr);
1420
1421                 /* Cache the LSR bits for later parsing */
1422                 ch->ch_cached_lsr |= lsrbits;
1423                 if (ch->ch_cached_lsr & UART_LSR_THRE) {
1424                         ch->ch_cached_lsr &= ~(UART_LSR_THRE);
1425
1426                         /*
1427                          * If RTS Toggle mode is on, turn on RTS now if not
1428                          * already set, and make sure we get an event when the
1429                          * data transfer has completed.
1430                          */
1431                         if (ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE) {
1432                                 if (!(ch->ch_mostat & UART_MCR_RTS)) {
1433                                         ch->ch_mostat |= (UART_MCR_RTS);
1434                                         neo_assert_modem_signals(ch);
1435                                 }
1436                                 ch->ch_tun.un_flags |= (UN_EMPTY);
1437                         }
1438                         /*
1439                          * If DTR Toggle mode is on, turn on DTR now if not
1440                          * already set, and make sure we get an event when the
1441                          * data transfer has completed.
1442                          */
1443                         if (ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE) {
1444                                 if (!(ch->ch_mostat & UART_MCR_DTR)) {
1445                                         ch->ch_mostat |= (UART_MCR_DTR);
1446                                         neo_assert_modem_signals(ch);
1447                                 }
1448                                 ch->ch_tun.un_flags |= (UN_EMPTY);
1449                         }
1450
1451                         writeb(ch->ch_wqueue[ch->ch_w_tail],
1452                                &ch->ch_neo_uart->txrx);
1453                         ch->ch_w_tail++;
1454                         ch->ch_w_tail &= WQUEUEMASK;
1455                         ch->ch_txcount++;
1456                 }
1457
1458                 goto exit_unlock;
1459         }
1460
1461         /*
1462          * We have to do it this way, because of the EXAR TXFIFO count bug.
1463          */
1464         if ((ch->ch_bd->dvid & 0xf0) < UART_XR17E158_DVID) {
1465                 if (!(ch->ch_flags & (CH_TX_FIFO_EMPTY | CH_TX_FIFO_LWM)))
1466                         goto exit_unlock;
1467
1468                 len_written = 0;
1469
1470                 n = readb(&ch->ch_neo_uart->tfifo);
1471
1472                 if ((unsigned int)n > ch->ch_t_tlevel)
1473                         goto exit_unlock;
1474
1475                 n = UART_17158_TX_FIFOSIZE - ch->ch_t_tlevel;
1476         } else {
1477                 n = UART_17158_TX_FIFOSIZE - readb(&ch->ch_neo_uart->tfifo);
1478         }
1479
1480         /* cache head and tail of queue */
1481         head = ch->ch_w_head & WQUEUEMASK;
1482         tail = ch->ch_w_tail & WQUEUEMASK;
1483         qlen = (head - tail) & WQUEUEMASK;
1484
1485         /* Find minimum of the FIFO space, versus queue length */
1486         n = min(n, qlen);
1487
1488         while (n > 0) {
1489                 s = ((head >= tail) ? head : WQUEUESIZE) - tail;
1490                 s = min(s, n);
1491
1492                 if (s <= 0)
1493                         break;
1494
1495                 /*
1496                  * If RTS Toggle mode is on, turn on RTS now if not already set,
1497                  * and make sure we get an event when the data transfer has
1498                  * completed.
1499                  */
1500                 if (ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE) {
1501                         if (!(ch->ch_mostat & UART_MCR_RTS)) {
1502                                 ch->ch_mostat |= (UART_MCR_RTS);
1503                                 neo_assert_modem_signals(ch);
1504                         }
1505                         ch->ch_tun.un_flags |= (UN_EMPTY);
1506                 }
1507
1508                 /*
1509                  * If DTR Toggle mode is on, turn on DTR now if not already set,
1510                  * and make sure we get an event when the data transfer has
1511                  * completed.
1512                  */
1513                 if (ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE) {
1514                         if (!(ch->ch_mostat & UART_MCR_DTR)) {
1515                                 ch->ch_mostat |= (UART_MCR_DTR);
1516                                 neo_assert_modem_signals(ch);
1517                         }
1518                         ch->ch_tun.un_flags |= (UN_EMPTY);
1519                 }
1520
1521                 memcpy_toio(&ch->ch_neo_uart->txrxburst,
1522                             ch->ch_wqueue + tail, s);
1523
1524                 /* Add and flip queue if needed */
1525                 tail = (tail + s) & WQUEUEMASK;
1526                 n -= s;
1527                 ch->ch_txcount += s;
1528                 len_written += s;
1529         }
1530
1531         /* Update the final tail */
1532         ch->ch_w_tail = tail & WQUEUEMASK;
1533
1534         if (len_written > 0) {
1535                 neo_pci_posting_flush(ch->ch_bd);
1536                 ch->ch_flags &= ~(CH_TX_FIFO_EMPTY | CH_TX_FIFO_LWM);
1537         }
1538
1539 exit_unlock:
1540         spin_unlock_irqrestore(&ch->ch_lock, flags);
1541 }
1542
1543 static void neo_parse_modem(struct channel_t *ch, unsigned char signals)
1544 {
1545         unsigned char msignals = signals;
1546
1547         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1548                 return;
1549
1550         /*
1551          * Do altpin switching. Altpin switches DCD and DSR.
1552          * This prolly breaks DSRPACE, so we should be more clever here.
1553          */
1554         if (ch->ch_digi.digi_flags & DIGI_ALTPIN) {
1555                 unsigned char mswap = msignals;
1556
1557                 if (mswap & UART_MSR_DDCD) {
1558                         msignals &= ~UART_MSR_DDCD;
1559                         msignals |= UART_MSR_DDSR;
1560                 }
1561                 if (mswap & UART_MSR_DDSR) {
1562                         msignals &= ~UART_MSR_DDSR;
1563                         msignals |= UART_MSR_DDCD;
1564                 }
1565                 if (mswap & UART_MSR_DCD) {
1566                         msignals &= ~UART_MSR_DCD;
1567                         msignals |= UART_MSR_DSR;
1568                 }
1569                 if (mswap & UART_MSR_DSR) {
1570                         msignals &= ~UART_MSR_DSR;
1571                         msignals |= UART_MSR_DCD;
1572                 }
1573         }
1574
1575         /*
1576          * Scrub off lower bits. They signify delta's, which I don't care
1577          * about
1578          */
1579         msignals &= 0xf0;
1580
1581         if (msignals & UART_MSR_DCD)
1582                 ch->ch_mistat |= UART_MSR_DCD;
1583         else
1584                 ch->ch_mistat &= ~UART_MSR_DCD;
1585
1586         if (msignals & UART_MSR_DSR)
1587                 ch->ch_mistat |= UART_MSR_DSR;
1588         else
1589                 ch->ch_mistat &= ~UART_MSR_DSR;
1590
1591         if (msignals & UART_MSR_RI)
1592                 ch->ch_mistat |= UART_MSR_RI;
1593         else
1594                 ch->ch_mistat &= ~UART_MSR_RI;
1595
1596         if (msignals & UART_MSR_CTS)
1597                 ch->ch_mistat |= UART_MSR_CTS;
1598         else
1599                 ch->ch_mistat &= ~UART_MSR_CTS;
1600 }
1601
1602 /* Make the UART raise any of the output signals we want up */
1603 static void neo_assert_modem_signals(struct channel_t *ch)
1604 {
1605         unsigned char out;
1606
1607         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1608                 return;
1609
1610         out = ch->ch_mostat;
1611
1612         if (ch->ch_flags & CH_LOOPBACK)
1613                 out |= UART_MCR_LOOP;
1614
1615         writeb(out, &ch->ch_neo_uart->mcr);
1616         neo_pci_posting_flush(ch->ch_bd);
1617
1618         /* Give time for the UART to actually raise/drop the signals */
1619         udelay(10);
1620 }
1621
1622 static void neo_send_start_character(struct channel_t *ch)
1623 {
1624         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1625                 return;
1626
1627         if (ch->ch_startc != _POSIX_VDISABLE) {
1628                 ch->ch_xon_sends++;
1629                 writeb(ch->ch_startc, &ch->ch_neo_uart->txrx);
1630                 neo_pci_posting_flush(ch->ch_bd);
1631                 udelay(10);
1632         }
1633 }
1634
1635 static void neo_send_stop_character(struct channel_t *ch)
1636 {
1637         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1638                 return;
1639
1640         if (ch->ch_stopc != _POSIX_VDISABLE) {
1641                 ch->ch_xoff_sends++;
1642                 writeb(ch->ch_stopc, &ch->ch_neo_uart->txrx);
1643                 neo_pci_posting_flush(ch->ch_bd);
1644                 udelay(10);
1645         }
1646 }
1647
1648 /*
1649  * neo_uart_init
1650  */
1651 static void neo_uart_init(struct channel_t *ch)
1652 {
1653         writeb(0, &ch->ch_neo_uart->ier);
1654         writeb(0, &ch->ch_neo_uart->efr);
1655         writeb(UART_EFR_ECB, &ch->ch_neo_uart->efr);
1656
1657         /* Clear out UART and FIFO */
1658         readb(&ch->ch_neo_uart->txrx);
1659         writeb(UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
1660                &ch->ch_neo_uart->isr_fcr);
1661         readb(&ch->ch_neo_uart->lsr);
1662         readb(&ch->ch_neo_uart->msr);
1663
1664         ch->ch_flags |= CH_FIFO_ENABLED;
1665
1666         /* Assert any signals we want up */
1667         writeb(ch->ch_mostat, &ch->ch_neo_uart->mcr);
1668         neo_pci_posting_flush(ch->ch_bd);
1669 }
1670
1671 /*
1672  * Make the UART completely turn off.
1673  */
1674 static void neo_uart_off(struct channel_t *ch)
1675 {
1676         /* Turn off UART enhanced bits */
1677         writeb(0, &ch->ch_neo_uart->efr);
1678
1679         /* Stop all interrupts from occurring. */
1680         writeb(0, &ch->ch_neo_uart->ier);
1681         neo_pci_posting_flush(ch->ch_bd);
1682 }
1683
1684 static uint neo_get_uart_bytes_left(struct channel_t *ch)
1685 {
1686         unsigned char left = 0;
1687         unsigned char lsr = readb(&ch->ch_neo_uart->lsr);
1688
1689         /* We must cache the LSR as some of the bits get reset once read... */
1690         ch->ch_cached_lsr |= lsr;
1691
1692         /* Determine whether the Transmitter is empty or not */
1693         if (!(lsr & UART_LSR_TEMT)) {
1694                 if (ch->ch_flags & CH_TX_FIFO_EMPTY)
1695                         tasklet_schedule(&ch->ch_bd->helper_tasklet);
1696                 left = 1;
1697         } else {
1698                 ch->ch_flags |= (CH_TX_FIFO_EMPTY | CH_TX_FIFO_LWM);
1699                 left = 0;
1700         }
1701
1702         return left;
1703 }
1704
1705 /* Channel lock MUST be held by the calling function! */
1706 static void neo_send_break(struct channel_t *ch, int msecs)
1707 {
1708         /*
1709          * If we receive a time of 0, this means turn off the break.
1710          */
1711         if (msecs == 0) {
1712                 if (ch->ch_flags & CH_BREAK_SENDING) {
1713                         unsigned char temp = readb(&ch->ch_neo_uart->lcr);
1714
1715                         writeb((temp & ~UART_LCR_SBC), &ch->ch_neo_uart->lcr);
1716                         neo_pci_posting_flush(ch->ch_bd);
1717                         ch->ch_flags &= ~(CH_BREAK_SENDING);
1718                         ch->ch_stop_sending_break = 0;
1719                 }
1720                 return;
1721         }
1722
1723         /*
1724          * Set the time we should stop sending the break.
1725          * If we are already sending a break, toss away the existing
1726          * time to stop, and use this new value instead.
1727          */
1728         ch->ch_stop_sending_break = jiffies + dgnc_jiffies_from_ms(msecs);
1729
1730         /* Tell the UART to start sending the break */
1731         if (!(ch->ch_flags & CH_BREAK_SENDING)) {
1732                 unsigned char temp = readb(&ch->ch_neo_uart->lcr);
1733
1734                 writeb((temp | UART_LCR_SBC), &ch->ch_neo_uart->lcr);
1735                 neo_pci_posting_flush(ch->ch_bd);
1736                 ch->ch_flags |= (CH_BREAK_SENDING);
1737         }
1738 }
1739
1740 /*
1741  * neo_send_immediate_char.
1742  *
1743  * Sends a specific character as soon as possible to the UART,
1744  * jumping over any bytes that might be in the write queue.
1745  *
1746  * The channel lock MUST be held by the calling function.
1747  */
1748 static void neo_send_immediate_char(struct channel_t *ch, unsigned char c)
1749 {
1750         if (!ch || ch->magic != DGNC_CHANNEL_MAGIC)
1751                 return;
1752
1753         writeb(c, &ch->ch_neo_uart->txrx);
1754         neo_pci_posting_flush(ch->ch_bd);
1755 }
1756
1757 static unsigned int neo_read_eeprom(unsigned char __iomem *base,
1758                                     unsigned int address)
1759 {
1760         unsigned int enable;
1761         unsigned int bits;
1762         unsigned int databit;
1763         unsigned int val;
1764
1765         /* enable chip select */
1766         writeb(NEO_EECS, base + NEO_EEREG);
1767         /* READ */
1768         enable = address | 0x180;
1769
1770         for (bits = 9; bits--; ) {
1771                 databit = (enable & (1 << bits)) ? NEO_EEDI : 0;
1772                 /* Set read address */
1773                 writeb(databit | NEO_EECS, base + NEO_EEREG);
1774                 writeb(databit | NEO_EECS | NEO_EECK, base + NEO_EEREG);
1775         }
1776
1777         val = 0;
1778
1779         for (bits = 17; bits--; ) {
1780                 /* clock to EEPROM */
1781                 writeb(NEO_EECS, base + NEO_EEREG);
1782                 writeb(NEO_EECS | NEO_EECK, base + NEO_EEREG);
1783                 val <<= 1;
1784                 /* read EEPROM */
1785                 if (readb(base + NEO_EEREG) & NEO_EEDO)
1786                         val |= 1;
1787         }
1788
1789         /* clock falling edge */
1790         writeb(NEO_EECS, base + NEO_EEREG);
1791
1792         /* drop chip select */
1793         writeb(0x00, base + NEO_EEREG);
1794
1795         return val;
1796 }
1797
1798 static void neo_vpd(struct dgnc_board *brd)
1799 {
1800         unsigned int i = 0;
1801         unsigned int a;
1802
1803         if (!brd || brd->magic != DGNC_BOARD_MAGIC)
1804                 return;
1805
1806         if (!brd->re_map_membase)
1807                 return;
1808
1809         /* Store the VPD into our buffer */
1810         for (i = 0; i < NEO_VPD_IMAGESIZE; i++) {
1811                 a = neo_read_eeprom(brd->re_map_membase, i);
1812                 brd->vpd[i * 2] = a & 0xff;
1813                 brd->vpd[(i * 2) + 1] = (a >> 8) & 0xff;
1814         }
1815
1816         /*
1817          * brd->vpd has different name tags by below index.
1818          * 0x08 : long resource name tag
1819          * 0x10 : long resource name tage (PCI-66 files)
1820          * 0x7F : small resource end tag
1821          */
1822         if  (((brd->vpd[0x08] != 0x82) &&
1823               (brd->vpd[0x10] != 0x82)) ||
1824              (brd->vpd[0x7F] != 0x78)) {
1825                 memset(brd->vpd, '\0', NEO_VPD_IMAGESIZE);
1826         } else {
1827                 /* Search for the serial number */
1828                 for (i = 0; i < NEO_VPD_IMAGEBYTES - 3; i++)
1829                         if (brd->vpd[i] == 'S' && brd->vpd[i + 1] == 'N')
1830                                 strncpy(brd->serial_num, &brd->vpd[i + 3], 9);
1831         }
1832 }