vserver 1.9.3
[linux-2.6.git] / drivers / char / epca.c
1 /*
2
3  
4         Copyright (C) 1996  Digi International.
5  
6         For technical support please email digiLinux@dgii.com or
7         call Digi tech support at (612) 912-3456
8
9         Much of this design and code came from epca.c which was 
10         copyright (C) 1994, 1995 Troy De Jongh, and subsquently 
11         modified by David Nugent, Christoph Lameter, Mike McLagan. 
12  
13         This program is free software; you can redistribute it and/or modify
14         it under the terms of the GNU General Public License as published by
15         the Free Software Foundation; either version 2 of the License, or
16         (at your option) any later version.
17
18         This program is distributed in the hope that it will be useful,
19         but WITHOUT ANY WARRANTY; without even the implied warranty of
20         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21         GNU General Public License for more details.
22
23         You should have received a copy of the GNU General Public License
24         along with this program; if not, write to the Free Software
25         Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26
27 --------------------------------------------------------------------------- */
28 /* See README.epca for change history --DAT*/
29
30
31 #include <linux/config.h>
32 #include <linux/module.h>
33 #include <linux/kernel.h>
34 #include <linux/types.h>
35 #include <linux/init.h>
36 #include <linux/serial.h>
37 #include <linux/delay.h>
38 #include <linux/ctype.h>
39 #include <linux/tty.h>
40 #include <linux/tty_flip.h>
41 #include <linux/slab.h>
42 #include <linux/ioport.h>
43 #include <linux/interrupt.h>
44 #include <asm/uaccess.h>
45 #include <asm/io.h>
46
47 #ifdef CONFIG_PCI
48 #define ENABLE_PCI
49 #endif /* CONFIG_PCI */
50
51 #define putUser(arg1, arg2) put_user(arg1, (unsigned long __user *)arg2)
52 #define getUser(arg1, arg2) get_user(arg1, (unsigned __user *)arg2)
53
54 #ifdef ENABLE_PCI
55 #include <linux/pci.h>
56 #include "digiPCI.h"
57 #endif /* ENABLE_PCI */
58
59 #include "digi1.h"
60 #include "digiFep1.h"
61 #include "epca.h"
62 #include "epcaconfig.h"
63
64 #if BITS_PER_LONG != 32
65 #  error FIXME: this driver only works on 32-bit platforms
66 #endif
67
68 /* ---------------------- Begin defines ------------------------ */
69
70 #define VERSION            "1.3.0.1-LK"
71
72 /* This major needs to be submitted to Linux to join the majors list */
73
74 #define DIGIINFOMAJOR       35  /* For Digi specific ioctl */ 
75
76
77 #define MAXCARDS 7
78 #define epcaassert(x, msg)  if (!(x)) epca_error(__LINE__, msg)
79
80 #define PFX "epca: "
81
82 /* ----------------- Begin global definitions ------------------- */
83
84 static char mesg[100];
85 static int nbdevs, num_cards, liloconfig;
86 static int digi_poller_inhibited = 1 ;
87
88 static int setup_error_code;
89 static int invalid_lilo_config;
90
91 /* -----------------------------------------------------------------------
92         MAXBOARDS is typically 12, but ISA and EISA cards are restricted to 
93         7 below.
94 --------------------------------------------------------------------------*/
95 static struct board_info boards[MAXBOARDS];
96
97
98 /* ------------- Begin structures used for driver registeration ---------- */
99
100 static struct tty_driver *pc_driver;
101 static struct tty_driver *pc_info;
102
103 /* ------------------ Begin Digi specific structures -------------------- */
104
105 /* ------------------------------------------------------------------------
106         digi_channels represents an array of structures that keep track of
107         each channel of the Digi product.  Information such as transmit and
108         receive pointers, termio data, and signal definitions (DTR, CTS, etc ...)
109         are stored here.  This structure is NOT used to overlay the cards 
110         physical channel structure.
111 -------------------------------------------------------------------------- */
112   
113 static struct channel digi_channels[MAX_ALLOC];
114
115 /* ------------------------------------------------------------------------
116         card_ptr is an array used to hold the address of the
117         first channel structure of each card.  This array will hold
118         the addresses of various channels located in digi_channels.
119 -------------------------------------------------------------------------- */
120 static struct channel *card_ptr[MAXCARDS];
121
122 static struct timer_list epca_timer;
123
124 /* ---------------------- Begin function prototypes --------------------- */
125
126 /* ----------------------------------------------------------------------
127         Begin generic memory functions.  These functions will be alias
128         (point at) more specific functions dependent on the board being
129         configured.
130 ----------------------------------------------------------------------- */
131         
132 static inline void memwinon(struct board_info *b, unsigned int win);
133 static inline void memwinoff(struct board_info *b, unsigned int win);
134 static inline void globalwinon(struct channel *ch);
135 static inline void rxwinon(struct channel *ch);
136 static inline void txwinon(struct channel *ch);
137 static inline void memoff(struct channel *ch);
138 static inline void assertgwinon(struct channel *ch);
139 static inline void assertmemoff(struct channel *ch);
140
141 /* ---- Begin more 'specific' memory functions for cx_like products --- */
142
143 static inline void pcxem_memwinon(struct board_info *b, unsigned int win);
144 static inline void pcxem_memwinoff(struct board_info *b, unsigned int win);
145 static inline void pcxem_globalwinon(struct channel *ch);
146 static inline void pcxem_rxwinon(struct channel *ch);
147 static inline void pcxem_txwinon(struct channel *ch);
148 static inline void pcxem_memoff(struct channel *ch);
149
150 /* ------ Begin more 'specific' memory functions for the pcxe ------- */
151
152 static inline void pcxe_memwinon(struct board_info *b, unsigned int win);
153 static inline void pcxe_memwinoff(struct board_info *b, unsigned int win);
154 static inline void pcxe_globalwinon(struct channel *ch);
155 static inline void pcxe_rxwinon(struct channel *ch);
156 static inline void pcxe_txwinon(struct channel *ch);
157 static inline void pcxe_memoff(struct channel *ch);
158
159 /* ---- Begin more 'specific' memory functions for the pc64xe and pcxi ---- */
160 /* Note : pc64xe and pcxi share the same windowing routines */
161
162 static inline void pcxi_memwinon(struct board_info *b, unsigned int win);
163 static inline void pcxi_memwinoff(struct board_info *b, unsigned int win);
164 static inline void pcxi_globalwinon(struct channel *ch);
165 static inline void pcxi_rxwinon(struct channel *ch);
166 static inline void pcxi_txwinon(struct channel *ch);
167 static inline void pcxi_memoff(struct channel *ch);
168
169 /* - Begin 'specific' do nothing memory functions needed for some cards - */
170
171 static inline void dummy_memwinon(struct board_info *b, unsigned int win);
172 static inline void dummy_memwinoff(struct board_info *b, unsigned int win);
173 static inline void dummy_globalwinon(struct channel *ch);
174 static inline void dummy_rxwinon(struct channel *ch);
175 static inline void dummy_txwinon(struct channel *ch);
176 static inline void dummy_memoff(struct channel *ch);
177 static inline void dummy_assertgwinon(struct channel *ch);
178 static inline void dummy_assertmemoff(struct channel *ch);
179
180 /* ------------------- Begin declare functions ----------------------- */
181
182 static inline struct channel *verifyChannel(register struct tty_struct *);
183 static inline void pc_sched_event(struct channel *, int);
184 static void epca_error(int, char *);
185 static void pc_close(struct tty_struct *, struct file *);
186 static void shutdown(struct channel *);
187 static void pc_hangup(struct tty_struct *);
188 static void pc_put_char(struct tty_struct *, unsigned char);
189 static int pc_write_room(struct tty_struct *);
190 static int pc_chars_in_buffer(struct tty_struct *);
191 static void pc_flush_buffer(struct tty_struct *);
192 static void pc_flush_chars(struct tty_struct *);
193 static int block_til_ready(struct tty_struct *, struct file *,
194                            struct channel *);
195 static int pc_open(struct tty_struct *, struct file *);
196 static void post_fep_init(unsigned int crd);
197 static void epcapoll(unsigned long);
198 static void doevent(int);
199 static void fepcmd(struct channel *, int, int, int, int, int);
200 static unsigned termios2digi_h(struct channel *ch, unsigned);
201 static unsigned termios2digi_i(struct channel *ch, unsigned);
202 static unsigned termios2digi_c(struct channel *ch, unsigned);
203 static void epcaparam(struct tty_struct *, struct channel *);
204 static void receive_data(struct channel *);
205 static int pc_ioctl(struct tty_struct *, struct file *,
206                     unsigned int, unsigned long);
207 static int info_ioctl(struct tty_struct *, struct file *,
208                     unsigned int, unsigned long);
209 static void pc_set_termios(struct tty_struct *, struct termios *);
210 static void do_softint(void *);
211 static void pc_stop(struct tty_struct *);
212 static void pc_start(struct tty_struct *);
213 static void pc_throttle(struct tty_struct * tty);
214 static void pc_unthrottle(struct tty_struct *tty);
215 static void digi_send_break(struct channel *ch, int msec);
216 static void setup_empty_event(struct tty_struct *tty, struct channel *ch);
217 void epca_setup(char *, int *);
218 void console_print(const char *);
219
220 static int get_termio(struct tty_struct *, struct termio __user *);
221 static int pc_write(struct tty_struct *, int, const unsigned char *, int);
222 int pc_init(void);
223
224 #ifdef ENABLE_PCI
225 static int init_PCI(void);
226 #endif /* ENABLE_PCI */
227
228
229 /* ------------------------------------------------------------------
230         Table of functions for each board to handle memory.  Mantaining 
231         parallelism is a *very* good idea here.  The idea is for the 
232         runtime code to blindly call these functions, not knowing/caring    
233         about the underlying hardware.  This stuff should contain no
234         conditionals; if more functionality is needed a different entry
235         should be established.  These calls are the interface calls and 
236         are the only functions that should be accessed.  Anyone caught
237         making direct calls deserves what they get.
238 -------------------------------------------------------------------- */
239
240 static inline void memwinon(struct board_info *b, unsigned int win)
241 {
242         (b->memwinon)(b, win);
243 }
244
245 static inline void memwinoff(struct board_info *b, unsigned int win)
246 {
247         (b->memwinoff)(b, win);
248 }
249
250 static inline void globalwinon(struct channel *ch)
251 {
252         (ch->board->globalwinon)(ch);
253 }
254
255 static inline void rxwinon(struct channel *ch)
256 {
257         (ch->board->rxwinon)(ch);
258 }
259
260 static inline void txwinon(struct channel *ch)
261 {
262         (ch->board->txwinon)(ch);
263 }
264
265 static inline void memoff(struct channel *ch)
266 {
267         (ch->board->memoff)(ch);
268 }
269 static inline void assertgwinon(struct channel *ch)
270 {
271         (ch->board->assertgwinon)(ch);
272 }
273
274 static inline void assertmemoff(struct channel *ch)
275 {
276         (ch->board->assertmemoff)(ch);
277 }
278
279 /* ---------------------------------------------------------
280         PCXEM windowing is the same as that used in the PCXR 
281         and CX series cards.
282 ------------------------------------------------------------ */
283
284 static inline void pcxem_memwinon(struct board_info *b, unsigned int win)
285 {
286         outb_p(FEPWIN|win, (int)b->port + 1);
287 }
288
289 static inline void pcxem_memwinoff(struct board_info *b, unsigned int win)
290 {
291         outb_p(0, (int)b->port + 1);
292 }
293
294 static inline void pcxem_globalwinon(struct channel *ch)
295 {
296         outb_p( FEPWIN, (int)ch->board->port + 1);
297 }
298
299 static inline void pcxem_rxwinon(struct channel *ch)
300 {
301         outb_p(ch->rxwin, (int)ch->board->port + 1);
302 }
303
304 static inline void pcxem_txwinon(struct channel *ch)
305 {
306         outb_p(ch->txwin, (int)ch->board->port + 1);
307 }
308
309 static inline void pcxem_memoff(struct channel *ch)
310 {
311         outb_p(0, (int)ch->board->port + 1);
312 }
313
314 /* ----------------- Begin pcxe memory window stuff ------------------ */
315
316 static inline void pcxe_memwinon(struct board_info *b, unsigned int win)
317 {
318                outb_p(FEPWIN | win, (int)b->port + 1);
319 }
320
321 static inline void pcxe_memwinoff(struct board_info *b, unsigned int win)
322 {
323         outb_p(inb((int)b->port) & ~FEPMEM,
324                    (int)b->port + 1);
325         outb_p(0, (int)b->port + 1);
326 }
327
328 static inline void pcxe_globalwinon(struct channel *ch)
329 {
330         outb_p( FEPWIN, (int)ch->board->port + 1);
331 }
332
333 static inline void pcxe_rxwinon(struct channel *ch)
334 {
335                 outb_p(ch->rxwin, (int)ch->board->port + 1);
336 }
337
338 static inline void pcxe_txwinon(struct channel *ch)
339 {
340                 outb_p(ch->txwin, (int)ch->board->port + 1);
341 }
342
343 static inline void pcxe_memoff(struct channel *ch)
344 {
345         outb_p(0, (int)ch->board->port);
346         outb_p(0, (int)ch->board->port + 1);
347 }
348
349 /* ------------- Begin pc64xe and pcxi memory window stuff -------------- */
350
351 static inline void pcxi_memwinon(struct board_info *b, unsigned int win)
352 {
353                outb_p(inb((int)b->port) | FEPMEM, (int)b->port);
354 }
355
356 static inline void pcxi_memwinoff(struct board_info *b, unsigned int win)
357 {
358         outb_p(inb((int)b->port) & ~FEPMEM, (int)b->port);
359 }
360
361 static inline void pcxi_globalwinon(struct channel *ch)
362 {
363         outb_p(FEPMEM, (int)ch->board->port);
364 }
365
366 static inline void pcxi_rxwinon(struct channel *ch)
367 {
368                 outb_p(FEPMEM, (int)ch->board->port);
369 }
370
371 static inline void pcxi_txwinon(struct channel *ch)
372 {
373                 outb_p(FEPMEM, (int)ch->board->port);
374 }
375
376 static inline void pcxi_memoff(struct channel *ch)
377 {
378         outb_p(0, (int)ch->board->port);
379 }
380
381 static inline void pcxi_assertgwinon(struct channel *ch)
382 {
383         epcaassert(inb((int)ch->board->port) & FEPMEM, "Global memory off");
384 }
385
386 static inline void pcxi_assertmemoff(struct channel *ch)
387 {
388         epcaassert(!(inb((int)ch->board->port) & FEPMEM), "Memory on");
389 }
390
391
392 /* ----------------------------------------------------------------------
393         Not all of the cards need specific memory windowing routines.  Some
394         cards (Such as PCI) needs no windowing routines at all.  We provide
395         these do nothing routines so that the same code base can be used.
396         The driver will ALWAYS call a windowing routine if it thinks it needs
397         to; regardless of the card.  However, dependent on the card the routine
398         may or may not do anything.
399 ---------------------------------------------------------------------------*/
400
401 static inline void dummy_memwinon(struct board_info *b, unsigned int win)
402 {
403 }
404
405 static inline void dummy_memwinoff(struct board_info *b, unsigned int win)
406 {
407 }
408
409 static inline void dummy_globalwinon(struct channel *ch)
410 {
411 }
412
413 static inline void dummy_rxwinon(struct channel *ch)
414 {
415 }
416
417 static inline void dummy_txwinon(struct channel *ch)
418 {
419 }
420
421 static inline void dummy_memoff(struct channel *ch)
422 {
423 }
424
425 static inline void dummy_assertgwinon(struct channel *ch)
426 {
427 }
428
429 static inline void dummy_assertmemoff(struct channel *ch)
430 {
431 }
432
433 /* ----------------- Begin verifyChannel function ----------------------- */
434 static inline struct channel *verifyChannel(register struct tty_struct *tty)
435 { /* Begin verifyChannel */
436
437         /* --------------------------------------------------------------------
438                 This routine basically provides a sanity check.  It insures that
439                 the channel returned is within the proper range of addresses as
440                 well as properly initialized.  If some bogus info gets passed in
441                 through tty->driver_data this should catch it.
442         --------------------------------------------------------------------- */
443
444         if (tty) 
445         { /* Begin if tty */
446
447                 register struct channel *ch = (struct channel *)tty->driver_data;
448
449                 if ((ch >= &digi_channels[0]) && (ch < &digi_channels[nbdevs])) 
450                 {
451                         if (ch->magic == EPCA_MAGIC)
452                                 return ch;
453                 }
454
455         } /* End if tty */
456
457         /* Else return a NULL for invalid */
458         return NULL;
459
460 } /* End verifyChannel */
461
462 /* ------------------ Begin pc_sched_event ------------------------- */
463
464 static inline void pc_sched_event(struct channel *ch, int event)
465 { /* Begin pc_sched_event */
466
467
468         /* ----------------------------------------------------------------------
469                 We call this to schedule interrupt processing on some event.  The 
470                 kernel sees our request and calls the related routine in OUR driver.
471         -------------------------------------------------------------------------*/
472
473         ch->event |= 1 << event;
474         schedule_work(&ch->tqueue);
475
476
477 } /* End pc_sched_event */
478
479 /* ------------------ Begin epca_error ------------------------- */
480
481 static void epca_error(int line, char *msg)
482 { /* Begin epca_error */
483
484         printk(KERN_ERR "epca_error (Digi): line = %d %s\n",line,msg);
485         return;
486
487 } /* End epca_error */
488
489 /* ------------------ Begin pc_close ------------------------- */
490 static void pc_close(struct tty_struct * tty, struct file * filp)
491 { /* Begin pc_close */
492
493         struct channel *ch;
494         unsigned long flags;
495
496         /* ---------------------------------------------------------
497                 verifyChannel returns the channel from the tty struct
498                 if it is valid.  This serves as a sanity check.
499         ------------------------------------------------------------- */
500
501         if ((ch = verifyChannel(tty)) != NULL) 
502         { /* Begin if ch != NULL */
503
504                 save_flags(flags);
505                 cli();
506
507                 if (tty_hung_up_p(filp)) 
508                 {
509                         restore_flags(flags);
510                         return;
511                 }
512
513                 /* Check to see if the channel is open more than once */
514                 if (ch->count-- > 1) 
515                 { /* Begin channel is open more than once */
516
517                         /* -------------------------------------------------------------
518                                 Return without doing anything.  Someone might still be using
519                                 the channel.
520                         ---------------------------------------------------------------- */
521
522                         restore_flags(flags);
523                         return;
524                 } /* End channel is open more than once */
525
526                 /* Port open only once go ahead with shutdown & reset */
527
528                 if (ch->count < 0) 
529                 {
530                         ch->count = 0;
531                 }
532
533                 /* ---------------------------------------------------------------
534                         Let the rest of the driver know the channel is being closed.
535                         This becomes important if an open is attempted before close 
536                         is finished.
537                 ------------------------------------------------------------------ */
538
539                 ch->asyncflags |= ASYNC_CLOSING;
540         
541                 tty->closing = 1;
542
543                 if (ch->asyncflags & ASYNC_INITIALIZED) 
544                 {
545                         /* Setup an event to indicate when the transmit buffer empties */
546                         setup_empty_event(tty, ch);             
547                         tty_wait_until_sent(tty, 3000); /* 30 seconds timeout */
548                 }
549         
550                 if (tty->driver->flush_buffer)
551                         tty->driver->flush_buffer(tty);
552
553                 tty_ldisc_flush(tty);
554                 shutdown(ch);
555                 tty->closing = 0;
556                 ch->event = 0;
557                 ch->tty = NULL;
558
559                 if (ch->blocked_open) 
560                 { /* Begin if blocked_open */
561
562                         if (ch->close_delay) 
563                         {
564                                 current->state = TASK_INTERRUPTIBLE;
565                                 schedule_timeout(ch->close_delay);
566                         }
567
568                         wake_up_interruptible(&ch->open_wait);
569
570                 } /* End if blocked_open */
571
572                 ch->asyncflags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_INITIALIZED | 
573                                       ASYNC_CLOSING);
574                 wake_up_interruptible(&ch->close_wait);
575
576
577                 restore_flags(flags);
578
579         } /* End if ch != NULL */
580
581 } /* End pc_close */ 
582
583 /* ------------------ Begin shutdown  ------------------------- */
584
585 static void shutdown(struct channel *ch)
586 { /* Begin shutdown */
587
588         unsigned long flags;
589         struct tty_struct *tty;
590         volatile struct board_chan *bc;
591
592         if (!(ch->asyncflags & ASYNC_INITIALIZED)) 
593                 return;
594
595         save_flags(flags);
596         cli();
597         globalwinon(ch);
598
599         bc = ch->brdchan;
600
601         /* ------------------------------------------------------------------
602                 In order for an event to be generated on the receipt of data the
603                 idata flag must be set. Since we are shutting down, this is not 
604                 necessary clear this flag.
605         --------------------------------------------------------------------- */ 
606
607         if (bc)
608                 bc->idata = 0;
609
610         tty = ch->tty;
611
612         /* ----------------------------------------------------------------
613            If we're a modem control device and HUPCL is on, drop RTS & DTR.
614         ------------------------------------------------------------------ */
615
616         if (tty->termios->c_cflag & HUPCL) 
617         {
618                 ch->omodem &= ~(ch->m_rts | ch->m_dtr);
619                 fepcmd(ch, SETMODEM, 0, ch->m_dtr | ch->m_rts, 10, 1);
620         }
621
622         memoff(ch);
623
624         /* ------------------------------------------------------------------
625                 The channel has officialy been closed.  The next time it is opened
626                 it will have to reinitialized.  Set a flag to indicate this.
627         ---------------------------------------------------------------------- */
628
629         /* Prevent future Digi programmed interrupts from coming active */
630
631         ch->asyncflags &= ~ASYNC_INITIALIZED;
632         restore_flags(flags);
633
634 } /* End shutdown */
635
636 /* ------------------ Begin pc_hangup  ------------------------- */
637
638 static void pc_hangup(struct tty_struct *tty)
639 { /* Begin pc_hangup */
640
641         struct channel *ch;
642         
643         /* ---------------------------------------------------------
644                 verifyChannel returns the channel from the tty struct
645                 if it is valid.  This serves as a sanity check.
646         ------------------------------------------------------------- */
647
648         if ((ch = verifyChannel(tty)) != NULL) 
649         { /* Begin if ch != NULL */
650
651                 unsigned long flags;
652
653                 save_flags(flags);
654                 cli();
655                 if (tty->driver->flush_buffer)
656                         tty->driver->flush_buffer(tty);
657                 tty_ldisc_flush(tty);
658                 shutdown(ch);
659
660                 ch->tty   = NULL;
661                 ch->event = 0;
662                 ch->count = 0;
663                 restore_flags(flags);
664                 ch->asyncflags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_INITIALIZED);
665                 wake_up_interruptible(&ch->open_wait);
666
667         } /* End if ch != NULL */
668
669 } /* End pc_hangup */
670
671 /* ------------------ Begin pc_write  ------------------------- */
672
673 static int pc_write(struct tty_struct * tty, int from_user,
674                     const unsigned char *buf, int bytesAvailable)
675 { /* Begin pc_write */
676
677         register unsigned int head, tail;
678         register int dataLen;
679         register int size;
680         register int amountCopied;
681
682
683         struct channel *ch;
684         unsigned long flags;
685         int remain;
686         volatile struct board_chan *bc;
687
688
689         /* ----------------------------------------------------------------
690                 pc_write is primarily called directly by the kernel routine
691                 tty_write (Though it can also be called by put_char) found in
692                 tty_io.c.  pc_write is passed a line discipline buffer where 
693                 the data to be written out is stored.  The line discipline 
694                 implementation itself is done at the kernel level and is not 
695                 brought into the driver.  
696         ------------------------------------------------------------------- */
697
698         /* ---------------------------------------------------------
699                 verifyChannel returns the channel from the tty struct
700                 if it is valid.  This serves as a sanity check.
701         ------------------------------------------------------------- */
702
703         if ((ch = verifyChannel(tty)) == NULL)
704                 return 0;
705
706         /* Make a pointer to the channel data structure found on the board. */
707
708         bc   = ch->brdchan;
709         size = ch->txbufsize;
710
711         if (from_user) 
712         { /* Begin from_user */
713
714                 save_flags(flags);
715                 cli();
716
717                 globalwinon(ch);
718
719                 /* -----------------------------------------------------------------    
720                         Anding against size will wrap the pointer back to its beginning 
721                         position if it is necessary.  This will only work if size is
722                         a power of 2 which should always be the case.  Size is determined 
723                         by the cards on board FEP/OS.
724                 -------------------------------------------------------------------- */ 
725
726                 /* head refers to the next empty location in which data may be stored */ 
727
728                 head = bc->tin & (size - 1);
729
730                 /* tail refers to the next data byte to be transmitted */ 
731
732                 tail = bc->tout;
733
734                 /* Consider changing this to a do statement to make sure */
735
736                 if (tail != bc->tout)
737                         tail = bc->tout;
738
739                 /* ------------------------------------------------------------------   
740                         Anding against size will wrap the pointer back to its beginning 
741                         position if it is necessary.  This will only work if size is
742                         a power of 2 which should always be the case.  Size is determined 
743                         by the cards on board FEP/OS.
744                 --------------------------------------------------------------------- */        
745
746                 tail &= (size - 1);
747
748                 /* -----------------------------------------------------------------
749                         Two situations can affect how space in the transmit buffer
750                         is calculated.  You can have a situation where the transmit
751                         in pointer (tin) head has wrapped around and actually has a 
752                         lower address than the transmit out pointer (tout) tail; or
753                         the transmit in pointer (tin) head will not be wrapped around
754                         yet, and have a higher address than the transmit out pointer
755                         (tout) tail.  Obviously space available in the transmit buffer
756                         is calculated differently for each case.
757
758                         Example 1:
759                         
760                         Consider a 10 byte buffer where head is a pointer to the next
761                         empty location in the buffer and tail is a pointer to the next 
762                         byte to transmit.  In this example head will not have wrapped 
763                         around and therefore head > tail.  
764
765                         0      1      2      3      4      5      6      7      8      9   
766                                 tail                               head
767
768                         The above diagram shows that buffer locations 2,3,4,5 and 6 have
769                         data to be transmitted, while head points at the next empty
770                         location.  To calculate how much space is available first we have
771                         to determine if the head pointer (tin) has wrapped.  To do this
772                         compare the head pointer to the tail pointer,  If head is equal
773                         or greater than tail; then it has not wrapped; and the space may
774                         be calculated by subtracting tail from head and then subtracting
775                         that value from the buffers size.  A one is subtracted from the
776                         new value to indicate how much space is available between the 
777                         head pointer and end of buffer; as well as the space between the
778                         beginning of the buffer and the tail.  If the head is not greater
779                         or equal to the tail this indicates that the head has wrapped
780                         around to the beginning of the buffer.  To calculate the space 
781                         available in this case simply subtract head from tail.  This new 
782                         value minus one represents the space available betwwen the head 
783                         and tail pointers.  In this example head (7) is greater than tail (2)
784                         and therefore has not wrapped around.  We find the space by first
785                         subtracting tail from head (7-2=5).  We then subtract this value
786                         from the buffer size of ten and subtract one (10-5-1=4).  The space
787                         remaining is 4 bytes. 
788
789                         Example 2:
790                         
791                         Consider a 10 byte buffer where head is a pointer to the next
792                         empty location in the buffer and tail is a pointer to the next 
793                         byte to transmit.  In this example head will wrapped around and 
794                         therefore head < tail.  
795
796                         0      1      2      3      4      5      6      7      8      9   
797                                 head                               tail
798
799                         The above diagram shows that buffer locations 7,8,9,0 and 1 have
800                         data to be transmitted, while head points at the next empty
801                         location.  To find the space available we compare head to tail.  If
802                         head is not equal to, or greater than tail this indicates that head
803                         has wrapped around. In this case head (2) is not equal to, or
804                         greater than tail (7) and therefore has already wrapped around.  To
805                         calculate the available space between the two pointers we subtract
806                         head from tail (7-2=5).  We then subtract one from this new value
807                         (5-1=4).  We have 5 bytes empty remaining in the buffer.  Unlike the
808                         previous example these five bytes are located between the head and
809                         tail pointers. 
810
811                 ----------------------------------------------------------------------- */
812
813                 dataLen = (head >= tail) ? (size - (head - tail) - 1) : (tail - head - 1);
814
815                 /* ----------------------------------------------------------------------
816                         In this case bytesAvailable has been passed into pc_write and
817                         represents the amount of data that needs to be written.  dataLen
818                         represents the amount of space available on the card.  Whichever
819                         value is smaller will be the amount actually written. 
820                         bytesAvailable will then take on this newly calculated value.
821                 ---------------------------------------------------------------------- */
822
823                 bytesAvailable = min(dataLen, bytesAvailable);
824
825                 /* First we read the data in from the file system into a temp buffer */
826
827                 memoff(ch);
828                 restore_flags(flags);
829
830                 if (bytesAvailable) 
831                 { /* Begin bytesAvailable */
832                         /* ---------------------------------------------------------------
833                                 The below function reads data from user memory.  This routine
834                                 can not be used in an interrupt routine. (Because it may 
835                                 generate a page fault)  It can only be called while we can the
836                                 user context is accessible. 
837
838                                 The prototype is :
839                                 inline void copy_from_user(void * to, const void * from,
840                                                           unsigned long count);
841
842                                 I also think (Check hackers guide) that optimization must
843                                 be turned ON.  (Which sounds strange to me...)
844
845                                 Remember copy_from_user WILL generate a page fault if the
846                                 user memory being accessed has been swapped out.  This can
847                                 cause this routine to temporarily sleep while this page
848                                 fault is occurring.
849                         
850                         ----------------------------------------------------------------- */
851
852                         if (copy_from_user(ch->tmp_buf, buf,
853                                            bytesAvailable))
854                                 return -EFAULT;
855                 } /* End bytesAvailable */
856
857                 /* ------------------------------------------------------------------ 
858                         Set buf to this address for the moment.  tmp_buf was allocated in
859                         post_fep_init.
860                 --------------------------------------------------------------------- */
861                 buf = ch->tmp_buf;
862
863         } /* End from_user */
864
865         /* All data is now local */
866
867         amountCopied = 0;
868         save_flags(flags);
869         cli();
870
871         globalwinon(ch);
872
873         head = bc->tin & (size - 1);
874         tail = bc->tout;
875
876         if (tail != bc->tout)
877                 tail = bc->tout;
878         tail &= (size - 1);
879
880         /*      If head >= tail, head has not wrapped around. */ 
881         if (head >= tail) 
882         { /* Begin head has not wrapped */
883
884                 /* ---------------------------------------------------------------
885                         remain (much like dataLen above) represents the total amount of
886                         space available on the card for data.  Here dataLen represents
887                         the space existing between the head pointer and the end of 
888                         buffer.  This is important because a memcpy cannot be told to
889                         automatically wrap around when it hits the buffer end.
890                 ------------------------------------------------------------------ */ 
891
892                 dataLen = size - head;
893                 remain = size - (head - tail) - 1;
894
895         } /* End head has not wrapped */
896         else 
897         { /* Begin head has wrapped around */
898
899                 remain = tail - head - 1;
900                 dataLen = remain;
901
902         } /* End head has wrapped around */
903
904         /* -------------------------------------------------------------------
905                         Check the space on the card.  If we have more data than 
906                         space; reduce the amount of data to fit the space.
907         ---------------------------------------------------------------------- */
908
909         bytesAvailable = min(remain, bytesAvailable);
910
911         txwinon(ch);
912         while (bytesAvailable > 0) 
913         { /* Begin while there is data to copy onto card */
914
915                 /* -----------------------------------------------------------------
916                         If head is not wrapped, the below will make sure the first 
917                         data copy fills to the end of card buffer.
918                 ------------------------------------------------------------------- */
919
920                 dataLen = min(bytesAvailable, dataLen);
921                 memcpy(ch->txptr + head, buf, dataLen);
922                 buf += dataLen;
923                 head += dataLen;
924                 amountCopied += dataLen;
925                 bytesAvailable -= dataLen;
926
927                 if (head >= size) 
928                 {
929                         head = 0;
930                         dataLen = tail;
931                 }
932
933         } /* End while there is data to copy onto card */
934
935         ch->statusflags |= TXBUSY;
936         globalwinon(ch);
937         bc->tin = head;
938
939         if ((ch->statusflags & LOWWAIT) == 0) 
940         {
941                 ch->statusflags |= LOWWAIT;
942                 bc->ilow = 1;
943         }
944         memoff(ch);
945         restore_flags(flags);
946
947         return(amountCopied);
948
949 } /* End pc_write */
950
951 /* ------------------ Begin pc_put_char  ------------------------- */
952
953 static void pc_put_char(struct tty_struct *tty, unsigned char c)
954 { /* Begin pc_put_char */
955
956    
957         pc_write(tty, 0, &c, 1);
958         return;
959
960 } /* End pc_put_char */
961
962 /* ------------------ Begin pc_write_room  ------------------------- */
963
964 static int pc_write_room(struct tty_struct *tty)
965 { /* Begin pc_write_room */
966
967         int remain;
968         struct channel *ch;
969         unsigned long flags;
970         unsigned int head, tail;
971         volatile struct board_chan *bc;
972
973         remain = 0;
974
975         /* ---------------------------------------------------------
976                 verifyChannel returns the channel from the tty struct
977                 if it is valid.  This serves as a sanity check.
978         ------------------------------------------------------------- */
979
980         if ((ch = verifyChannel(tty)) != NULL) 
981         {
982                 save_flags(flags);
983                 cli();
984                 globalwinon(ch);
985
986                 bc   = ch->brdchan;
987                 head = bc->tin & (ch->txbufsize - 1);
988                 tail = bc->tout;
989
990                 if (tail != bc->tout)
991                         tail = bc->tout;
992                 /* Wrap tail if necessary */
993                 tail &= (ch->txbufsize - 1);
994
995                 if ((remain = tail - head - 1) < 0 )
996                         remain += ch->txbufsize;
997
998                 if (remain && (ch->statusflags & LOWWAIT) == 0) 
999                 {
1000                         ch->statusflags |= LOWWAIT;
1001                         bc->ilow = 1;
1002                 }
1003                 memoff(ch);
1004                 restore_flags(flags);
1005         }
1006
1007         /* Return how much room is left on card */
1008         return remain;
1009
1010 } /* End pc_write_room */
1011
1012 /* ------------------ Begin pc_chars_in_buffer  ---------------------- */
1013
1014 static int pc_chars_in_buffer(struct tty_struct *tty)
1015 { /* Begin pc_chars_in_buffer */
1016
1017         int chars;
1018         unsigned int ctail, head, tail;
1019         int remain;
1020         unsigned long flags;
1021         struct channel *ch;
1022         volatile struct board_chan *bc;
1023
1024
1025         /* ---------------------------------------------------------
1026                 verifyChannel returns the channel from the tty struct
1027                 if it is valid.  This serves as a sanity check.
1028         ------------------------------------------------------------- */
1029
1030         if ((ch = verifyChannel(tty)) == NULL)
1031                 return(0);
1032
1033         save_flags(flags);
1034         cli();
1035         globalwinon(ch);
1036
1037         bc = ch->brdchan;
1038         tail = bc->tout;
1039         head = bc->tin;
1040         ctail = ch->mailbox->cout;
1041
1042         if (tail == head && ch->mailbox->cin == ctail && bc->tbusy == 0)
1043                 chars = 0;
1044         else 
1045         { /* Begin if some space on the card has been used */
1046
1047                 head = bc->tin & (ch->txbufsize - 1);
1048                 tail &= (ch->txbufsize - 1);
1049
1050                 /*  --------------------------------------------------------------
1051                         The logic here is basically opposite of the above pc_write_room
1052                         here we are finding the amount of bytes in the buffer filled.
1053                         Not the amount of bytes empty.
1054                 ------------------------------------------------------------------- */
1055
1056                 if ((remain = tail - head - 1) < 0 )
1057                         remain += ch->txbufsize;
1058
1059                 chars = (int)(ch->txbufsize - remain);
1060
1061                 /* -------------------------------------------------------------  
1062                         Make it possible to wakeup anything waiting for output
1063                         in tty_ioctl.c, etc.
1064
1065                         If not already set.  Setup an event to indicate when the
1066                         transmit buffer empties 
1067                 ----------------------------------------------------------------- */
1068
1069                 if (!(ch->statusflags & EMPTYWAIT))
1070                         setup_empty_event(tty,ch);
1071
1072         } /* End if some space on the card has been used */
1073
1074         memoff(ch);
1075         restore_flags(flags);
1076
1077         /* Return number of characters residing on card. */
1078         return(chars);
1079
1080 } /* End pc_chars_in_buffer */
1081
1082 /* ------------------ Begin pc_flush_buffer  ---------------------- */
1083
1084 static void pc_flush_buffer(struct tty_struct *tty)
1085 { /* Begin pc_flush_buffer */
1086
1087         unsigned int tail;
1088         unsigned long flags;
1089         struct channel *ch;
1090         volatile struct board_chan *bc;
1091
1092
1093         /* ---------------------------------------------------------
1094                 verifyChannel returns the channel from the tty struct
1095                 if it is valid.  This serves as a sanity check.
1096         ------------------------------------------------------------- */
1097
1098         if ((ch = verifyChannel(tty)) == NULL)
1099                 return;
1100
1101         save_flags(flags);
1102         cli();
1103
1104         globalwinon(ch);
1105
1106         bc   = ch->brdchan;
1107         tail = bc->tout;
1108
1109         /* Have FEP move tout pointer; effectively flushing transmit buffer */
1110
1111         fepcmd(ch, STOUT, (unsigned) tail, 0, 0, 0);
1112
1113         memoff(ch);
1114         restore_flags(flags);
1115
1116         wake_up_interruptible(&tty->write_wait);
1117         tty_wakeup(tty);
1118
1119 } /* End pc_flush_buffer */
1120
1121 /* ------------------ Begin pc_flush_chars  ---------------------- */
1122
1123 static void pc_flush_chars(struct tty_struct *tty)
1124 { /* Begin pc_flush_chars */
1125
1126         struct channel * ch;
1127
1128         /* ---------------------------------------------------------
1129                 verifyChannel returns the channel from the tty struct
1130                 if it is valid.  This serves as a sanity check.
1131         ------------------------------------------------------------- */
1132
1133         if ((ch = verifyChannel(tty)) != NULL) 
1134         {
1135                 unsigned long flags;
1136
1137                 save_flags(flags);
1138                 cli();
1139
1140                 /* ----------------------------------------------------------------
1141                         If not already set and the transmitter is busy setup an event
1142                         to indicate when the transmit empties.
1143                 ------------------------------------------------------------------- */
1144
1145                 if ((ch->statusflags & TXBUSY) && !(ch->statusflags & EMPTYWAIT))
1146                         setup_empty_event(tty,ch);
1147
1148                 restore_flags(flags);
1149         }
1150
1151 } /* End pc_flush_chars */
1152
1153 /* ------------------ Begin block_til_ready  ---------------------- */
1154
1155 static int block_til_ready(struct tty_struct *tty, 
1156                            struct file *filp, struct channel *ch)
1157 { /* Begin block_til_ready */
1158
1159         DECLARE_WAITQUEUE(wait,current);
1160         int     retval, do_clocal = 0;
1161         unsigned long flags;
1162
1163
1164         if (tty_hung_up_p(filp))
1165         {
1166                 if (ch->asyncflags & ASYNC_HUP_NOTIFY)
1167                         retval = -EAGAIN;
1168                 else
1169                         retval = -ERESTARTSYS;  
1170                 return(retval);
1171         }
1172
1173         /* ----------------------------------------------------------------- 
1174                 If the device is in the middle of being closed, then block
1175                 until it's done, and then try again.
1176         -------------------------------------------------------------------- */
1177         if (ch->asyncflags & ASYNC_CLOSING) 
1178         {
1179                 interruptible_sleep_on(&ch->close_wait);
1180
1181                 if (ch->asyncflags & ASYNC_HUP_NOTIFY)
1182                         return -EAGAIN;
1183                 else
1184                         return -ERESTARTSYS;
1185         }
1186
1187         if (filp->f_flags & O_NONBLOCK) 
1188         {
1189                 /* ----------------------------------------------------------------- 
1190                  If non-blocking mode is set, then make the check up front
1191                  and then exit.
1192                 -------------------------------------------------------------------- */
1193
1194                 ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
1195
1196                 return 0;
1197         }
1198
1199
1200         if (tty->termios->c_cflag & CLOCAL)
1201                 do_clocal = 1;
1202         
1203    /* Block waiting for the carrier detect and the line to become free */
1204         
1205         retval = 0;
1206         add_wait_queue(&ch->open_wait, &wait);
1207         save_flags(flags);
1208         cli();
1209
1210
1211         /* We dec count so that pc_close will know when to free things */
1212         if (!tty_hung_up_p(filp))
1213                 ch->count--;
1214
1215         restore_flags(flags);
1216
1217         ch->blocked_open++;
1218
1219         while(1) 
1220         { /* Begin forever while  */
1221
1222                 set_current_state(TASK_INTERRUPTIBLE);
1223
1224                 if (tty_hung_up_p(filp) ||
1225                     !(ch->asyncflags & ASYNC_INITIALIZED)) 
1226                 {
1227                         if (ch->asyncflags & ASYNC_HUP_NOTIFY)
1228                                 retval = -EAGAIN;
1229                         else
1230                                 retval = -ERESTARTSYS;  
1231                         break;
1232                 }
1233
1234                 if (!(ch->asyncflags & ASYNC_CLOSING) && 
1235                           (do_clocal || (ch->imodem & ch->dcd)))
1236                         break;
1237
1238                 if (signal_pending(current)) 
1239                 {
1240                         retval = -ERESTARTSYS;
1241                         break;
1242                 }
1243
1244                 /* ---------------------------------------------------------------
1245                         Allow someone else to be scheduled.  We will occasionally go
1246                         through this loop until one of the above conditions change.
1247                         The below schedule call will allow other processes to enter and
1248                         prevent this loop from hogging the cpu.
1249                 ------------------------------------------------------------------ */
1250                 schedule();
1251
1252         } /* End forever while  */
1253
1254         current->state = TASK_RUNNING;
1255         remove_wait_queue(&ch->open_wait, &wait);
1256         cli();
1257         if (!tty_hung_up_p(filp))
1258                 ch->count++;
1259         restore_flags(flags);
1260
1261         ch->blocked_open--;
1262
1263         if (retval)
1264                 return retval;
1265
1266         ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
1267
1268         return 0;
1269
1270 } /* End block_til_ready */     
1271
1272 /* ------------------ Begin pc_open  ---------------------- */
1273
1274 static int pc_open(struct tty_struct *tty, struct file * filp)
1275 { /* Begin pc_open */
1276
1277         struct channel *ch;
1278         unsigned long flags;
1279         int line, retval, boardnum;
1280         volatile struct board_chan *bc;
1281         volatile unsigned int head;
1282
1283         line = tty->index;
1284         if (line < 0 || line >= nbdevs) 
1285         {
1286                 printk(KERN_ERR "<Error> - pc_open : line out of range in pc_open\n");
1287                 tty->driver_data = NULL;
1288                 return(-ENODEV);
1289         }
1290
1291
1292         ch = &digi_channels[line];
1293         boardnum = ch->boardnum;
1294
1295         /* Check status of board configured in system.  */
1296
1297         /* -----------------------------------------------------------------
1298                 I check to see if the epca_setup routine detected an user error.  
1299                 It might be better to put this in pc_init, but for the moment it
1300                 goes here.
1301         ---------------------------------------------------------------------- */
1302
1303         if (invalid_lilo_config)
1304         {
1305                 if (setup_error_code & INVALID_BOARD_TYPE)
1306                         printk(KERN_ERR "<Error> - pc_open: Invalid board type specified in LILO command\n");
1307
1308                 if (setup_error_code & INVALID_NUM_PORTS)
1309                         printk(KERN_ERR "<Error> - pc_open: Invalid number of ports specified in LILO command\n");
1310
1311                 if (setup_error_code & INVALID_MEM_BASE)
1312                         printk(KERN_ERR "<Error> - pc_open: Invalid board memory address specified in LILO command\n");
1313
1314                 if (setup_error_code & INVALID_PORT_BASE)
1315                         printk(KERN_ERR "<Error> - pc_open: Invalid board port address specified in LILO command\n");
1316
1317                 if (setup_error_code & INVALID_BOARD_STATUS)
1318                         printk(KERN_ERR "<Error> - pc_open: Invalid board status specified in LILO command\n");
1319
1320                 if (setup_error_code & INVALID_ALTPIN)
1321                         printk(KERN_ERR "<Error> - pc_open: Invalid board altpin specified in LILO command\n");
1322
1323                 tty->driver_data = NULL;   /* Mark this device as 'down' */
1324                 return(-ENODEV);
1325         }
1326
1327         if ((boardnum >= num_cards) || (boards[boardnum].status == DISABLED)) 
1328         {
1329                 tty->driver_data = NULL;   /* Mark this device as 'down' */
1330                 return(-ENODEV);
1331         }
1332         
1333         if (( bc = ch->brdchan) == 0) 
1334         {
1335                 tty->driver_data = NULL;
1336                 return(-ENODEV);
1337         }
1338
1339         /* ------------------------------------------------------------------
1340                 Every time a channel is opened, increment a counter.  This is 
1341                 necessary because we do not wish to flush and shutdown the channel
1342                 until the last app holding the channel open, closes it.         
1343         --------------------------------------------------------------------- */
1344
1345         ch->count++;
1346
1347         /* ----------------------------------------------------------------
1348                 Set a kernel structures pointer to our local channel 
1349                 structure.  This way we can get to it when passed only
1350                 a tty struct.
1351         ------------------------------------------------------------------ */
1352
1353         tty->driver_data = ch;
1354         
1355         /* ----------------------------------------------------------------
1356                 If this is the first time the channel has been opened, initialize
1357                 the tty->termios struct otherwise let pc_close handle it.
1358         -------------------------------------------------------------------- */
1359
1360         save_flags(flags);
1361         cli();
1362
1363         globalwinon(ch);
1364         ch->statusflags = 0;
1365
1366         /* Save boards current modem status */
1367         ch->imodem = bc->mstat;
1368
1369         /* ----------------------------------------------------------------
1370            Set receive head and tail ptrs to each other.  This indicates
1371            no data available to read.
1372         ----------------------------------------------------------------- */
1373         head = bc->rin;
1374         bc->rout = head;
1375
1376         /* Set the channels associated tty structure */
1377         ch->tty = tty;
1378
1379         /* -----------------------------------------------------------------
1380                 The below routine generally sets up parity, baud, flow control 
1381                 issues, etc.... It effect both control flags and input flags.
1382         -------------------------------------------------------------------- */
1383         epcaparam(tty,ch);
1384
1385         ch->asyncflags |= ASYNC_INITIALIZED;
1386         memoff(ch);
1387
1388         restore_flags(flags);
1389
1390         retval = block_til_ready(tty, filp, ch);
1391         if (retval)
1392         {
1393                 return retval;
1394         }
1395
1396         /* -------------------------------------------------------------
1397                 Set this again in case a hangup set it to zero while this 
1398                 open() was waiting for the line...
1399         --------------------------------------------------------------- */
1400         ch->tty = tty;
1401
1402         save_flags(flags);
1403         cli();
1404         globalwinon(ch);
1405
1406         /* Enable Digi Data events */
1407         bc->idata = 1;
1408
1409         memoff(ch);
1410         restore_flags(flags);
1411
1412         return 0;
1413
1414 } /* End pc_open */
1415
1416 #ifdef MODULE
1417 static int __init epca_module_init(void)
1418 { /* Begin init_module */
1419
1420         unsigned long   flags;
1421
1422         save_flags(flags);
1423         cli();
1424
1425         pc_init();
1426
1427         restore_flags(flags);
1428
1429         return(0);
1430 }
1431
1432 module_init(epca_module_init);
1433 #endif
1434
1435 #ifdef ENABLE_PCI
1436 static struct pci_driver epca_driver;
1437 #endif
1438
1439 #ifdef MODULE
1440 /* -------------------- Begin cleanup_module  ---------------------- */
1441
1442 static void __exit epca_module_exit(void)
1443 {
1444
1445         int               count, crd;
1446         struct board_info *bd;
1447         struct channel    *ch;
1448         unsigned long     flags;
1449
1450         del_timer_sync(&epca_timer);
1451
1452         save_flags(flags);
1453         cli();
1454
1455         if ((tty_unregister_driver(pc_driver)) ||  
1456             (tty_unregister_driver(pc_info)))
1457         {
1458                 printk(KERN_WARNING "<Error> - DIGI : cleanup_module failed to un-register tty driver\n");
1459                 restore_flags(flags);
1460                 return;
1461         }
1462         put_tty_driver(pc_driver);
1463         put_tty_driver(pc_info);
1464
1465         for (crd = 0; crd < num_cards; crd++) 
1466         { /* Begin for each card */
1467
1468                 bd = &boards[crd];
1469
1470                 if (!bd)
1471                 { /* Begin sanity check */
1472                         printk(KERN_ERR "<Error> - Digi : cleanup_module failed\n");
1473                         return;
1474                 } /* End sanity check */
1475
1476                 ch = card_ptr[crd]; 
1477
1478                 for (count = 0; count < bd->numports; count++, ch++) 
1479                 { /* Begin for each port */
1480
1481                         if (ch) 
1482                         {
1483                                 if (ch->tty)
1484                                         tty_hangup(ch->tty);
1485                                 kfree(ch->tmp_buf);
1486                         }
1487
1488                 } /* End for each port */
1489         } /* End for each card */
1490
1491 #ifdef ENABLE_PCI
1492         pci_unregister_driver (&epca_driver);
1493 #endif
1494
1495         restore_flags(flags);
1496
1497 }
1498 module_exit(epca_module_exit);
1499 #endif /* MODULE */
1500
1501 static struct tty_operations pc_ops = {
1502         .open = pc_open,
1503         .close = pc_close,
1504         .write = pc_write,
1505         .write_room = pc_write_room,
1506         .flush_buffer = pc_flush_buffer,
1507         .chars_in_buffer = pc_chars_in_buffer,
1508         .flush_chars = pc_flush_chars,
1509         .put_char = pc_put_char,
1510         .ioctl = pc_ioctl,
1511         .set_termios = pc_set_termios,
1512         .stop = pc_stop,
1513         .start = pc_start,
1514         .throttle = pc_throttle,
1515         .unthrottle = pc_unthrottle,
1516         .hangup = pc_hangup,
1517 };
1518
1519 static int info_open(struct tty_struct *tty, struct file * filp)
1520 {
1521         return 0;
1522 }
1523
1524 static struct tty_operations info_ops = {
1525         .open = info_open,
1526         .ioctl = info_ioctl,
1527 };
1528
1529 /* ------------------ Begin pc_init  ---------------------- */
1530
1531 int __init pc_init(void)
1532 { /* Begin pc_init */
1533
1534         /* ----------------------------------------------------------------
1535                 pc_init is called by the operating system during boot up prior to
1536                 any open calls being made.  In the older versions of Linux (Prior
1537                 to 2.0.0) an entry is made into tty_io.c.  A pointer to the last
1538                 memory location (from kernel space) used (kmem_start) is passed
1539                 to pc_init.  It is pc_inits responsibility to modify this value 
1540                 for any memory that the Digi driver might need and then return
1541                 this value to the operating system.  For example if the driver
1542                 wishes to allocate 1K of kernel memory, pc_init would return 
1543                 (kmem_start + 1024).  This memory (Between kmem_start and kmem_start
1544                 + 1024) would then be available for use exclusively by the driver.  
1545                 In this case our driver does not allocate any of this kernel 
1546                 memory.
1547         ------------------------------------------------------------------*/
1548
1549         ulong flags;
1550         int crd;
1551         struct board_info *bd;
1552         unsigned char board_id = 0;
1553
1554 #ifdef ENABLE_PCI
1555         int pci_boards_found, pci_count;
1556
1557         pci_count = 0;
1558 #endif /* ENABLE_PCI */
1559
1560         pc_driver = alloc_tty_driver(MAX_ALLOC);
1561         if (!pc_driver)
1562                 return -ENOMEM;
1563
1564         pc_info = alloc_tty_driver(MAX_ALLOC);
1565         if (!pc_info) {
1566                 put_tty_driver(pc_driver);
1567                 return -ENOMEM;
1568         }
1569
1570         /* -----------------------------------------------------------------------
1571                 If epca_setup has not been ran by LILO set num_cards to defaults; copy
1572                 board structure defined by digiConfig into drivers board structure.
1573                 Note : If LILO has ran epca_setup then epca_setup will handle defining
1574                 num_cards as well as copying the data into the board structure.
1575         -------------------------------------------------------------------------- */
1576         if (!liloconfig)
1577         { /* Begin driver has been configured via. epcaconfig */
1578
1579                 nbdevs = NBDEVS;
1580                 num_cards = NUMCARDS;
1581                 memcpy((void *)&boards, (void *)&static_boards,
1582                        (sizeof(struct board_info) * NUMCARDS));
1583         } /* End driver has been configured via. epcaconfig */
1584
1585         /* -----------------------------------------------------------------
1586                 Note : If lilo was used to configure the driver and the 
1587                 ignore epcaconfig option was choosen (digiepca=2) then 
1588                 nbdevs and num_cards will equal 0 at this point.  This is
1589                 okay; PCI cards will still be picked up if detected.
1590         --------------------------------------------------------------------- */
1591
1592         /*  -----------------------------------------------------------
1593                 Set up interrupt, we will worry about memory allocation in
1594                 post_fep_init. 
1595         --------------------------------------------------------------- */
1596
1597
1598         printk(KERN_INFO "DIGI epca driver version %s loaded.\n",VERSION);
1599
1600 #ifdef ENABLE_PCI
1601
1602         /* ------------------------------------------------------------------
1603                 NOTE : This code assumes that the number of ports found in 
1604                        the boards array is correct.  This could be wrong if
1605                        the card in question is PCI (And therefore has no ports 
1606                        entry in the boards structure.)  The rest of the 
1607                        information will be valid for PCI because the beginning
1608                        of pc_init scans for PCI and determines i/o and base
1609                        memory addresses.  I am not sure if it is possible to 
1610                        read the number of ports supported by the card prior to
1611                        it being booted (Since that is the state it is in when 
1612                        pc_init is run).  Because it is not possible to query the
1613                        number of supported ports until after the card has booted;
1614                        we are required to calculate the card_ptrs as the card is         
1615                        is initialized (Inside post_fep_init).  The negative thing
1616                        about this approach is that digiDload's call to GET_INFO
1617                        will have a bad port value.  (Since this is called prior
1618                        to post_fep_init.)
1619
1620         --------------------------------------------------------------------- */
1621   
1622         pci_boards_found = 0;
1623         if(num_cards < MAXBOARDS)
1624                 pci_boards_found += init_PCI();
1625         num_cards += pci_boards_found;
1626
1627 #endif /* ENABLE_PCI */
1628
1629         pc_driver->owner = THIS_MODULE;
1630         pc_driver->name = "ttyD"; 
1631         pc_driver->devfs_name = "tts/D";
1632         pc_driver->major = DIGI_MAJOR; 
1633         pc_driver->minor_start = 0;
1634         pc_driver->type = TTY_DRIVER_TYPE_SERIAL;
1635         pc_driver->subtype = SERIAL_TYPE_NORMAL;
1636         pc_driver->init_termios = tty_std_termios;
1637         pc_driver->init_termios.c_iflag = 0;
1638         pc_driver->init_termios.c_oflag = 0;
1639         pc_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
1640         pc_driver->init_termios.c_lflag = 0;
1641         pc_driver->flags = TTY_DRIVER_REAL_RAW;
1642         tty_set_operations(pc_driver, &pc_ops);
1643
1644         pc_info->owner = THIS_MODULE;
1645         pc_info->name = "digi_ctl";
1646         pc_info->major = DIGIINFOMAJOR;
1647         pc_info->minor_start = 0;
1648         pc_info->type = TTY_DRIVER_TYPE_SERIAL;
1649         pc_info->subtype = SERIAL_TYPE_INFO;
1650         pc_info->init_termios = tty_std_termios;
1651         pc_info->init_termios.c_iflag = 0;
1652         pc_info->init_termios.c_oflag = 0;
1653         pc_info->init_termios.c_lflag = 0;
1654         pc_info->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL;
1655         pc_info->flags = TTY_DRIVER_REAL_RAW;
1656         tty_set_operations(pc_info, &info_ops);
1657
1658
1659         save_flags(flags);
1660         cli();
1661
1662         for (crd = 0; crd < num_cards; crd++) 
1663         { /* Begin for each card */
1664
1665                 /*  ------------------------------------------------------------------
1666                         This is where the appropriate memory handlers for the hardware is
1667                         set.  Everything at runtime blindly jumps through these vectors.
1668                 ---------------------------------------------------------------------- */
1669
1670                 /* defined in epcaconfig.h */
1671                 bd = &boards[crd];
1672
1673                 switch (bd->type)
1674                 { /* Begin switch on bd->type {board type} */
1675                         case PCXEM:
1676                         case EISAXEM:
1677                                 bd->memwinon     = pcxem_memwinon ;
1678                                 bd->memwinoff    = pcxem_memwinoff ;
1679                                 bd->globalwinon  = pcxem_globalwinon ;
1680                                 bd->txwinon      = pcxem_txwinon ;
1681                                 bd->rxwinon      = pcxem_rxwinon ;
1682                                 bd->memoff       = pcxem_memoff ;
1683                                 bd->assertgwinon = dummy_assertgwinon;
1684                                 bd->assertmemoff = dummy_assertmemoff;
1685                                 break;
1686
1687                         case PCIXEM:
1688                         case PCIXRJ:
1689                         case PCIXR:
1690                                 bd->memwinon     = dummy_memwinon;
1691                                 bd->memwinoff    = dummy_memwinoff;
1692                                 bd->globalwinon  = dummy_globalwinon;
1693                                 bd->txwinon      = dummy_txwinon;
1694                                 bd->rxwinon      = dummy_rxwinon;
1695                                 bd->memoff       = dummy_memoff;
1696                                 bd->assertgwinon = dummy_assertgwinon;
1697                                 bd->assertmemoff = dummy_assertmemoff;
1698                                 break;
1699
1700                         case PCXE:
1701                         case PCXEVE:
1702
1703                                 bd->memwinon     = pcxe_memwinon;
1704                                 bd->memwinoff    = pcxe_memwinoff;
1705                                 bd->globalwinon  = pcxe_globalwinon;
1706                                 bd->txwinon      = pcxe_txwinon;
1707                                 bd->rxwinon      = pcxe_rxwinon;
1708                                 bd->memoff       = pcxe_memoff;
1709                                 bd->assertgwinon = dummy_assertgwinon;
1710                                 bd->assertmemoff = dummy_assertmemoff;
1711                                 break;
1712
1713                         case PCXI:
1714                         case PC64XE:
1715
1716                                 bd->memwinon     = pcxi_memwinon;
1717                                 bd->memwinoff    = pcxi_memwinoff;
1718                                 bd->globalwinon  = pcxi_globalwinon;
1719                                 bd->txwinon      = pcxi_txwinon;
1720                                 bd->rxwinon      = pcxi_rxwinon;
1721                                 bd->memoff       = pcxi_memoff;
1722                                 bd->assertgwinon = pcxi_assertgwinon;
1723                                 bd->assertmemoff = pcxi_assertmemoff;
1724                                 break;
1725
1726                         default:
1727                                 break;
1728
1729                 } /* End switch on bd->type */
1730
1731                 /* ---------------------------------------------------------------
1732                         Some cards need a memory segment to be defined for use in 
1733                         transmit and receive windowing operations.  These boards
1734                         are listed in the below switch.  In the case of the XI the
1735                         amount of memory on the board is variable so the memory_seg
1736                         is also variable.  This code determines what they segment 
1737                         should be.
1738                 ----------------------------------------------------------------- */
1739
1740                 switch (bd->type)
1741                 { /* Begin switch on bd->type {board type} */
1742
1743                         case PCXE:
1744                         case PCXEVE:
1745                         case PC64XE:
1746                                 bd->memory_seg = 0xf000;
1747                         break;
1748
1749                         case PCXI:
1750                                 board_id = inb((int)bd->port);
1751                                 if ((board_id & 0x1) == 0x1) 
1752                                 { /* Begin it's an XI card */ 
1753
1754                                         /* Is it a 64K board */
1755                                         if ((board_id & 0x30) == 0) 
1756                                                 bd->memory_seg = 0xf000;
1757
1758                                         /* Is it a 128K board */
1759                                         if ((board_id & 0x30) == 0x10) 
1760                                                 bd->memory_seg = 0xe000;
1761
1762                                         /* Is is a 256K board */        
1763                                         if ((board_id & 0x30) == 0x20) 
1764                                                 bd->memory_seg = 0xc000;
1765
1766                                         /* Is it a 512K board */
1767                                         if ((board_id & 0x30) == 0x30) 
1768                                                 bd->memory_seg = 0x8000;
1769
1770                                 } /* End it is an XI card */
1771                                 else
1772                                 {
1773                                         printk(KERN_ERR "<Error> - Board at 0x%x doesn't appear to be an XI\n",(int)bd->port);
1774                                 }
1775                         break;
1776
1777                 } /* End switch on bd->type */
1778
1779         } /* End for each card */
1780
1781         if (tty_register_driver(pc_driver))
1782                 panic("Couldn't register Digi PC/ driver");
1783
1784         if (tty_register_driver(pc_info))
1785                 panic("Couldn't register Digi PC/ info ");
1786
1787         /* -------------------------------------------------------------------
1788            Start up the poller to check for events on all enabled boards
1789         ---------------------------------------------------------------------- */
1790
1791         init_timer(&epca_timer);
1792         epca_timer.function = epcapoll;
1793         mod_timer(&epca_timer, jiffies + HZ/25);
1794
1795         restore_flags(flags);
1796
1797         return 0;
1798
1799 } /* End pc_init */
1800
1801 /* ------------------ Begin post_fep_init  ---------------------- */
1802
1803 static void post_fep_init(unsigned int crd)
1804 { /* Begin post_fep_init */
1805
1806         int i;
1807         unchar *memaddr;
1808         volatile struct global_data *gd;
1809         struct board_info *bd;
1810         volatile struct board_chan *bc;
1811         struct channel *ch; 
1812         int shrinkmem = 0, lowwater ; 
1813  
1814         /*  -------------------------------------------------------------
1815                 This call is made by the user via. the ioctl call DIGI_INIT.
1816                 It is responsible for setting up all the card specific stuff.
1817         ---------------------------------------------------------------- */
1818         bd = &boards[crd];
1819
1820         /* -----------------------------------------------------------------
1821                 If this is a PCI board, get the port info.  Remember PCI cards
1822                 do not have entries into the epcaconfig.h file, so we can't get 
1823                 the number of ports from it.  Unfortunetly, this means that anyone
1824                 doing a DIGI_GETINFO before the board has booted will get an invalid
1825                 number of ports returned (It should return 0).  Calls to DIGI_GETINFO
1826                 after DIGI_INIT has been called will return the proper values. 
1827         ------------------------------------------------------------------- */
1828
1829         if (bd->type >= PCIXEM) /* If the board in question is PCI */
1830         { /* Begin get PCI number of ports */
1831
1832                 /* --------------------------------------------------------------------
1833                         Below we use XEMPORTS as a memory offset regardless of which PCI
1834                         card it is.  This is because all of the supported PCI cards have
1835                         the same memory offset for the channel data.  This will have to be
1836                         changed if we ever develop a PCI/XE card.  NOTE : The FEP manual
1837                         states that the port offset is 0xC22 as opposed to 0xC02.  This is
1838                         only true for PC/XE, and PC/XI cards; not for the XEM, or CX series.
1839                         On the PCI cards the number of ports is determined by reading a 
1840                         ID PROM located in the box attached to the card.  The card can then
1841                         determine the index the id to determine the number of ports available.
1842                         (FYI - The id should be located at 0x1ac (And may use up to 4 bytes
1843                         if the box in question is a XEM or CX)).  
1844                 ------------------------------------------------------------------------ */ 
1845
1846                 bd->numports = (unsigned short)*(unsigned char *)bus_to_virt((unsigned long)
1847                                                        (bd->re_map_membase + XEMPORTS));
1848
1849                 
1850                 epcaassert(bd->numports <= 64,"PCI returned a invalid number of ports");
1851                 nbdevs += (bd->numports);
1852
1853         } /* End get PCI number of ports */
1854
1855         if (crd != 0)
1856                 card_ptr[crd] = card_ptr[crd-1] + boards[crd-1].numports;
1857         else
1858                 card_ptr[crd] = &digi_channels[crd]; /* <- For card 0 only */
1859
1860         ch = card_ptr[crd];
1861
1862
1863         epcaassert(ch <= &digi_channels[nbdevs - 1], "ch out of range");
1864
1865         memaddr = (unchar *)bd->re_map_membase;
1866
1867         /* 
1868            The below command is necessary because newer kernels (2.1.x and
1869            up) do not have a 1:1 virtual to physical mapping.  The below
1870            call adjust for that.
1871         */
1872
1873         memaddr = (unsigned char *)bus_to_virt((unsigned long)memaddr);
1874
1875         /* -----------------------------------------------------------------
1876                 The below assignment will set bc to point at the BEGINING of
1877                 the cards channel structures.  For 1 card there will be between
1878                 8 and 64 of these structures.
1879         -------------------------------------------------------------------- */
1880
1881         bc = (volatile struct board_chan *)((ulong)memaddr + CHANSTRUCT);
1882
1883         /* -------------------------------------------------------------------
1884                 The below assignment will set gd to point at the BEGINING of
1885                 global memory address 0xc00.  The first data in that global
1886                 memory actually starts at address 0xc1a.  The command in 
1887                 pointer begins at 0xd10.
1888         ---------------------------------------------------------------------- */
1889
1890         gd = (volatile struct global_data *)((ulong)memaddr + GLOBAL);
1891
1892         /* --------------------------------------------------------------------
1893                 XEPORTS (address 0xc22) points at the number of channels the
1894                 card supports. (For 64XE, XI, XEM, and XR use 0xc02)
1895         ----------------------------------------------------------------------- */
1896
1897         if (((bd->type == PCXEVE) | (bd->type == PCXE)) &&
1898             (*(ushort *)((ulong)memaddr + XEPORTS) < 3))
1899                 shrinkmem = 1;
1900         if (bd->type < PCIXEM)
1901                 if (!request_region((int)bd->port, 4, board_desc[bd->type]))
1902                         return;         
1903
1904         memwinon(bd, 0);
1905
1906         /*  --------------------------------------------------------------------
1907                 Remember ch is the main drivers channels structure, while bc is 
1908            the cards channel structure.
1909         ------------------------------------------------------------------------ */
1910
1911         /* For every port on the card do ..... */
1912
1913         for (i = 0; i < bd->numports; i++, ch++, bc++) 
1914         { /* Begin for each port */
1915
1916                 ch->brdchan        = bc;
1917                 ch->mailbox        = gd; 
1918                 INIT_WORK(&ch->tqueue, do_softint, ch);
1919                 ch->board          = &boards[crd];
1920
1921                 switch (bd->type)
1922                 { /* Begin switch bd->type */
1923
1924                         /* ----------------------------------------------------------------
1925                                 Since some of the boards use different bitmaps for their
1926                                 control signals we cannot hard code these values and retain
1927                                 portability.  We virtualize this data here.
1928                         ------------------------------------------------------------------- */
1929                         case EISAXEM:
1930                         case PCXEM:
1931                         case PCIXEM:
1932                         case PCIXRJ:
1933                         case PCIXR:
1934                                 ch->m_rts = 0x02 ;
1935                                 ch->m_dcd = 0x80 ; 
1936                                 ch->m_dsr = 0x20 ;
1937                                 ch->m_cts = 0x10 ;
1938                                 ch->m_ri  = 0x40 ;
1939                                 ch->m_dtr = 0x01 ;
1940                                 break;
1941
1942                         case PCXE:
1943                         case PCXEVE:
1944                         case PCXI:
1945                         case PC64XE:
1946                                 ch->m_rts = 0x02 ;
1947                                 ch->m_dcd = 0x08 ; 
1948                                 ch->m_dsr = 0x10 ;
1949                                 ch->m_cts = 0x20 ;
1950                                 ch->m_ri  = 0x40 ;
1951                                 ch->m_dtr = 0x80 ;
1952                                 break;
1953         
1954                 } /* End switch bd->type */
1955
1956                 if (boards[crd].altpin) 
1957                 {
1958                         ch->dsr = ch->m_dcd;
1959                         ch->dcd = ch->m_dsr;
1960                         ch->digiext.digi_flags |= DIGI_ALTPIN;
1961                 }
1962                 else 
1963                 { 
1964                         ch->dcd = ch->m_dcd;
1965                         ch->dsr = ch->m_dsr;
1966                 }
1967         
1968                 ch->boardnum   = crd;
1969                 ch->channelnum = i;
1970                 ch->magic      = EPCA_MAGIC;
1971                 ch->tty        = NULL;
1972
1973                 if (shrinkmem) 
1974                 {
1975                         fepcmd(ch, SETBUFFER, 32, 0, 0, 0);
1976                         shrinkmem = 0;
1977                 }
1978
1979                 switch (bd->type)
1980                 { /* Begin switch bd->type */
1981
1982                         case PCIXEM:
1983                         case PCIXRJ:
1984                         case PCIXR:
1985                                 /* Cover all the 2MEG cards */
1986                                 ch->txptr = memaddr + (((bc->tseg) << 4) & 0x1fffff);
1987                                 ch->rxptr = memaddr + (((bc->rseg) << 4) & 0x1fffff);
1988                                 ch->txwin = FEPWIN | ((bc->tseg) >> 11);
1989                                 ch->rxwin = FEPWIN | ((bc->rseg) >> 11);
1990                                 break;
1991
1992                         case PCXEM:
1993                         case EISAXEM:
1994                                 /* Cover all the 32K windowed cards */
1995                                 /* Mask equal to window size - 1 */
1996                                 ch->txptr = memaddr + (((bc->tseg) << 4) & 0x7fff);
1997                                 ch->rxptr = memaddr + (((bc->rseg) << 4) & 0x7fff);
1998                                 ch->txwin = FEPWIN | ((bc->tseg) >> 11);
1999                                 ch->rxwin = FEPWIN | ((bc->rseg) >> 11);
2000                                 break;
2001
2002                         case PCXEVE:
2003                         case PCXE:
2004                                 ch->txptr = memaddr + (((bc->tseg - bd->memory_seg) << 4) & 0x1fff);
2005                                 ch->txwin = FEPWIN | ((bc->tseg - bd->memory_seg) >> 9);
2006                                 ch->rxptr = memaddr + (((bc->rseg - bd->memory_seg) << 4) & 0x1fff);
2007                                 ch->rxwin = FEPWIN | ((bc->rseg - bd->memory_seg) >>9 );
2008                                 break;
2009
2010                         case PCXI:
2011                         case PC64XE:
2012                                 ch->txptr = memaddr + ((bc->tseg - bd->memory_seg) << 4);
2013                                 ch->rxptr = memaddr + ((bc->rseg - bd->memory_seg) << 4);
2014                                 ch->txwin = ch->rxwin = 0;
2015                                 break;
2016
2017                 } /* End switch bd->type */
2018
2019                 ch->txbufhead = 0;
2020                 ch->txbufsize = bc->tmax + 1;
2021         
2022                 ch->rxbufhead = 0;
2023                 ch->rxbufsize = bc->rmax + 1;
2024         
2025                 lowwater = ch->txbufsize >= 2000 ? 1024 : (ch->txbufsize / 2);
2026
2027                 /* Set transmitter low water mark */
2028                 fepcmd(ch, STXLWATER, lowwater, 0, 10, 0);
2029
2030                 /* Set receiver low water mark */
2031
2032                 fepcmd(ch, SRXLWATER, (ch->rxbufsize / 4), 0, 10, 0);
2033
2034                 /* Set receiver high water mark */
2035
2036                 fepcmd(ch, SRXHWATER, (3 * ch->rxbufsize / 4), 0, 10, 0);
2037
2038                 bc->edelay = 100;
2039                 bc->idata = 1;
2040         
2041                 ch->startc  = bc->startc;
2042                 ch->stopc   = bc->stopc;
2043                 ch->startca = bc->startca;
2044                 ch->stopca  = bc->stopca;
2045         
2046                 ch->fepcflag = 0;
2047                 ch->fepiflag = 0;
2048                 ch->fepoflag = 0;
2049                 ch->fepstartc = 0;
2050                 ch->fepstopc = 0;
2051                 ch->fepstartca = 0;
2052                 ch->fepstopca = 0;
2053         
2054                 ch->close_delay = 50;
2055                 ch->count = 0;
2056                 ch->blocked_open = 0;
2057                 init_waitqueue_head(&ch->open_wait);
2058                 init_waitqueue_head(&ch->close_wait);
2059                 ch->tmp_buf = kmalloc(ch->txbufsize,GFP_KERNEL);
2060                 if (!(ch->tmp_buf))
2061                 {
2062                         printk(KERN_ERR "POST FEP INIT : kmalloc failed for port 0x%x\n",i);
2063                         release_region((int)bd->port, 4);
2064                         while(i-- > 0)
2065                                 kfree((ch--)->tmp_buf);
2066                         return;
2067                 }
2068                 else 
2069                         memset((void *)ch->tmp_buf,0,ch->txbufsize);
2070         } /* End for each port */
2071
2072         printk(KERN_INFO 
2073                 "Digi PC/Xx Driver V%s:  %s I/O = 0x%lx Mem = 0x%lx Ports = %d\n", 
2074                 VERSION, board_desc[bd->type], (long)bd->port, (long)bd->membase, bd->numports);
2075         sprintf(mesg, 
2076                 "Digi PC/Xx Driver V%s:  %s I/O = 0x%lx Mem = 0x%lx Ports = %d\n", 
2077                 VERSION, board_desc[bd->type], (long)bd->port, (long)bd->membase, bd->numports);
2078         console_print(mesg);
2079
2080         memwinoff(bd, 0);
2081
2082 } /* End post_fep_init */
2083
2084 /* --------------------- Begin epcapoll  ------------------------ */
2085
2086 static void epcapoll(unsigned long ignored)
2087 { /* Begin epcapoll */
2088
2089         unsigned long flags;
2090         int crd;
2091         volatile unsigned int head, tail;
2092         struct channel *ch;
2093         struct board_info *bd;
2094
2095         /* -------------------------------------------------------------------
2096                 This routine is called upon every timer interrupt.  Even though
2097                 the Digi series cards are capable of generating interrupts this 
2098                 method of non-looping polling is more efficient.  This routine
2099                 checks for card generated events (Such as receive data, are transmit
2100                 buffer empty) and acts on those events.
2101         ----------------------------------------------------------------------- */
2102         
2103         save_flags(flags);
2104         cli();
2105
2106         for (crd = 0; crd < num_cards; crd++) 
2107         { /* Begin for each card */
2108
2109                 bd = &boards[crd];
2110                 ch = card_ptr[crd];
2111
2112                 if ((bd->status == DISABLED) || digi_poller_inhibited)
2113                         continue; /* Begin loop next interation */
2114
2115                 /* -----------------------------------------------------------
2116                         assertmemoff is not needed here; indeed it is an empty subroutine.
2117                         It is being kept because future boards may need this as well as
2118                         some legacy boards.
2119                 ---------------------------------------------------------------- */
2120
2121                 assertmemoff(ch);
2122
2123                 globalwinon(ch);
2124
2125                 /* ---------------------------------------------------------------
2126                         In this case head and tail actually refer to the event queue not
2127                         the transmit or receive queue.
2128                 ------------------------------------------------------------------- */
2129
2130                 head = ch->mailbox->ein;
2131                 tail = ch->mailbox->eout;
2132                 
2133                 /* If head isn't equal to tail we have an event */
2134
2135                 if (head != tail)
2136                         doevent(crd);
2137
2138                 memoff(ch);
2139
2140         } /* End for each card */
2141
2142         mod_timer(&epca_timer, jiffies + (HZ / 25));
2143
2144         restore_flags(flags);
2145 } /* End epcapoll */
2146
2147 /* --------------------- Begin doevent  ------------------------ */
2148
2149 static void doevent(int crd)
2150 { /* Begin doevent */
2151
2152         volatile unchar *eventbuf;
2153         struct channel *ch, *chan0;
2154         static struct tty_struct *tty;
2155         volatile struct board_info *bd;
2156         volatile struct board_chan *bc;
2157         register volatile unsigned int tail, head;
2158         register int event, channel;
2159         register int mstat, lstat;
2160
2161         /* -------------------------------------------------------------------
2162                 This subroutine is called by epcapoll when an event is detected 
2163                 in the event queue.  This routine responds to those events.
2164         --------------------------------------------------------------------- */
2165
2166         bd = &boards[crd];
2167
2168         chan0 = card_ptr[crd];
2169         epcaassert(chan0 <= &digi_channels[nbdevs - 1], "ch out of range");
2170
2171         assertgwinon(chan0);
2172
2173         while ((tail = chan0->mailbox->eout) != (head = chan0->mailbox->ein)) 
2174         { /* Begin while something in event queue */
2175
2176                 assertgwinon(chan0);
2177
2178                 eventbuf = (volatile unchar *)bus_to_virt((ulong)(bd->re_map_membase + tail + ISTART));
2179
2180                 /* Get the channel the event occurred on */
2181                 channel = eventbuf[0];
2182
2183                 /* Get the actual event code that occurred */
2184                 event = eventbuf[1];
2185
2186                 /*  ----------------------------------------------------------------
2187                         The two assignments below get the current modem status (mstat)
2188                         and the previous modem status (lstat).  These are useful becuase
2189                         an event could signal a change in modem signals itself.
2190                 ------------------------------------------------------------------- */
2191
2192                 mstat = eventbuf[2];
2193                 lstat = eventbuf[3];
2194
2195                 ch = chan0 + channel;
2196
2197                 if ((unsigned)channel >= bd->numports || !ch) 
2198                 { 
2199                         if (channel >= bd->numports)
2200                                 ch = chan0;
2201                         bc = ch->brdchan;
2202                         goto next;
2203                 }
2204
2205                 if ((bc = ch->brdchan) == NULL)
2206                         goto next;
2207
2208                 if (event & DATA_IND) 
2209                 { /* Begin DATA_IND */
2210
2211                         receive_data(ch);
2212                         assertgwinon(ch);
2213
2214                 } /* End DATA_IND */
2215                 /* else *//* Fix for DCD transition missed bug */
2216                 if (event & MODEMCHG_IND) 
2217                 { /* Begin MODEMCHG_IND */
2218
2219                         /* A modem signal change has been indicated */
2220
2221                         ch->imodem = mstat;
2222
2223                         if (ch->asyncflags & ASYNC_CHECK_CD) 
2224                         {
2225                                 if (mstat & ch->dcd)  /* We are now receiving dcd */
2226                                         wake_up_interruptible(&ch->open_wait);
2227                                 else
2228                                         pc_sched_event(ch, EPCA_EVENT_HANGUP); /* No dcd; hangup */
2229                         }
2230
2231                 } /* End MODEMCHG_IND */
2232
2233                 tty = ch->tty;
2234                 if (tty) 
2235                 { /* Begin if valid tty */
2236
2237                         if (event & BREAK_IND) 
2238                         { /* Begin if BREAK_IND */
2239
2240                                 /* A break has been indicated */
2241
2242                                 tty->flip.count++;
2243                                 *tty->flip.flag_buf_ptr++ = TTY_BREAK;
2244
2245                                 *tty->flip.char_buf_ptr++ = 0;
2246
2247                                 tty_schedule_flip(tty); 
2248
2249                         } /* End if BREAK_IND */
2250                         else
2251                         if (event & LOWTX_IND) 
2252                         { /* Begin LOWTX_IND */
2253
2254                                 if (ch->statusflags & LOWWAIT) 
2255                                 { /* Begin if LOWWAIT */
2256
2257                                         ch->statusflags &= ~LOWWAIT;
2258                                         tty_wakeup(tty);
2259                                         wake_up_interruptible(&tty->write_wait);
2260
2261                                 } /* End if LOWWAIT */
2262
2263                         } /* End LOWTX_IND */
2264                         else
2265                         if (event & EMPTYTX_IND) 
2266                         { /* Begin EMPTYTX_IND */
2267
2268                                 /* This event is generated by setup_empty_event */
2269
2270                                 ch->statusflags &= ~TXBUSY;
2271                                 if (ch->statusflags & EMPTYWAIT) 
2272                                 { /* Begin if EMPTYWAIT */
2273
2274                                         ch->statusflags &= ~EMPTYWAIT;
2275                                         tty_wakeup(tty);
2276
2277                                         wake_up_interruptible(&tty->write_wait);
2278
2279                                 } /* End if EMPTYWAIT */
2280
2281                         } /* End EMPTYTX_IND */
2282
2283                 } /* End if valid tty */
2284
2285
2286         next:
2287                 globalwinon(ch);
2288
2289                 if (!bc)
2290                         printk(KERN_ERR "<Error> - bc == NULL in doevent!\n");
2291                 else 
2292                         bc->idata = 1;
2293
2294                 chan0->mailbox->eout = (tail + 4) & (IMAX - ISTART - 4);
2295                 globalwinon(chan0);
2296
2297         } /* End while something in event queue */
2298
2299 } /* End doevent */
2300
2301 /* --------------------- Begin fepcmd  ------------------------ */
2302
2303 static void fepcmd(struct channel *ch, int cmd, int word_or_byte,
2304                    int byte2, int ncmds, int bytecmd)
2305 { /* Begin fepcmd */
2306
2307         unchar *memaddr;
2308         unsigned int head, cmdTail, cmdStart, cmdMax;
2309         long count;
2310         int n;
2311
2312         /* This is the routine in which commands may be passed to the card. */
2313
2314         if (ch->board->status == DISABLED)
2315         {
2316                 return;
2317         }
2318
2319         assertgwinon(ch);
2320
2321         /* Remember head (As well as max) is just an offset not a base addr */
2322         head = ch->mailbox->cin;
2323
2324         /* cmdStart is a base address */
2325         cmdStart = ch->mailbox->cstart;
2326
2327         /* ------------------------------------------------------------------
2328                 We do the addition below because we do not want a max pointer 
2329                 relative to cmdStart.  We want a max pointer that points at the 
2330                 physical end of the command queue.
2331         -------------------------------------------------------------------- */
2332
2333         cmdMax = (cmdStart + 4 + (ch->mailbox->cmax));
2334
2335         memaddr = ch->board->re_map_membase;
2336
2337         /* 
2338            The below command is necessary because newer kernels (2.1.x and
2339            up) do not have a 1:1 virtual to physical mapping.  The below
2340            call adjust for that.
2341         */
2342
2343         memaddr = (unsigned char *)bus_to_virt((unsigned long)memaddr);
2344
2345         if (head >= (cmdMax - cmdStart) || (head & 03)) 
2346         {
2347                 printk(KERN_ERR "line %d: Out of range, cmd = %x, head = %x\n", __LINE__, 
2348               cmd, head);
2349                 printk(KERN_ERR "line %d: Out of range, cmdMax = %x, cmdStart = %x\n", __LINE__, 
2350               cmdMax, cmdStart);
2351                 return;
2352         }
2353
2354         if (bytecmd) 
2355         {
2356                 *(volatile unchar *)(memaddr + head + cmdStart + 0) = (unchar)cmd;
2357
2358                 *(volatile unchar *)(memaddr + head + cmdStart + 1) = (unchar)ch->channelnum;
2359                 /* Below word_or_byte is bits to set */
2360                 *(volatile unchar *)(memaddr + head + cmdStart + 2) = (unchar)word_or_byte;
2361                 /* Below byte2 is bits to reset */
2362                 *(volatile unchar *)(memaddr + head + cmdStart + 3) = (unchar)byte2;
2363
2364         } 
2365         else 
2366         {
2367                 *(volatile unchar *)(memaddr + head + cmdStart + 0) = (unchar)cmd;
2368                 *(volatile unchar *)(memaddr + head + cmdStart + 1) = (unchar)ch->channelnum;
2369                 *(volatile ushort*)(memaddr + head + cmdStart + 2) = (ushort)word_or_byte;
2370         }
2371
2372         head = (head + 4) & (cmdMax - cmdStart - 4);
2373         ch->mailbox->cin = head;
2374
2375         count = FEPTIMEOUT;
2376
2377         for (;;) 
2378         { /* Begin forever loop */
2379
2380                 count--;
2381                 if (count == 0) 
2382                 {
2383                         printk(KERN_ERR "<Error> - Fep not responding in fepcmd()\n");
2384                         return;
2385                 }
2386
2387                 head = ch->mailbox->cin;
2388                 cmdTail = ch->mailbox->cout;
2389
2390                 n = (head - cmdTail) & (cmdMax - cmdStart - 4);
2391
2392                 /* ----------------------------------------------------------
2393                         Basically this will break when the FEP acknowledges the 
2394                         command by incrementing cmdTail (Making it equal to head).
2395                 ------------------------------------------------------------- */
2396
2397                 if (n <= ncmds * (sizeof(short) * 4))
2398                         break; /* Well nearly forever :-) */
2399
2400         } /* End forever loop */
2401
2402 } /* End fepcmd */
2403
2404 /* ---------------------------------------------------------------------
2405         Digi products use fields in their channels structures that are very
2406         similar to the c_cflag and c_iflag fields typically found in UNIX
2407         termios structures.  The below three routines allow mappings 
2408         between these hardware "flags" and their respective Linux flags.
2409 ------------------------------------------------------------------------- */
2410  
2411 /* --------------------- Begin termios2digi_h -------------------- */
2412
2413 static unsigned termios2digi_h(struct channel *ch, unsigned cflag)
2414 { /* Begin termios2digi_h */
2415
2416         unsigned res = 0;
2417
2418         if (cflag & CRTSCTS) 
2419         {
2420                 ch->digiext.digi_flags |= (RTSPACE | CTSPACE);
2421                 res |= ((ch->m_cts) | (ch->m_rts));
2422         }
2423
2424         if (ch->digiext.digi_flags & RTSPACE)
2425                 res |= ch->m_rts;
2426
2427         if (ch->digiext.digi_flags & DTRPACE)
2428                 res |= ch->m_dtr;
2429
2430         if (ch->digiext.digi_flags & CTSPACE)
2431                 res |= ch->m_cts;
2432
2433         if (ch->digiext.digi_flags & DSRPACE)
2434                 res |= ch->dsr;
2435
2436         if (ch->digiext.digi_flags & DCDPACE)
2437                 res |= ch->dcd;
2438
2439         if (res & (ch->m_rts))
2440                 ch->digiext.digi_flags |= RTSPACE;
2441
2442         if (res & (ch->m_cts))
2443                 ch->digiext.digi_flags |= CTSPACE;
2444
2445         return res;
2446
2447 } /* End termios2digi_h */
2448
2449 /* --------------------- Begin termios2digi_i -------------------- */
2450 static unsigned termios2digi_i(struct channel *ch, unsigned iflag)
2451 { /* Begin termios2digi_i */
2452
2453         unsigned res = iflag & (IGNBRK | BRKINT | IGNPAR | PARMRK | 
2454                                 INPCK | ISTRIP|IXON|IXANY|IXOFF);
2455         
2456         if (ch->digiext.digi_flags & DIGI_AIXON)
2457                 res |= IAIXON;
2458         return res;
2459
2460 } /* End termios2digi_i */
2461
2462 /* --------------------- Begin termios2digi_c -------------------- */
2463
2464 static unsigned termios2digi_c(struct channel *ch, unsigned cflag)
2465 { /* Begin termios2digi_c */
2466
2467         unsigned res = 0;
2468
2469 #ifdef SPEED_HACK
2470         /* CL: HACK to force 115200 at 38400 and 57600 at 19200 Baud */
2471         if ((cflag & CBAUD)== B38400) cflag=cflag - B38400 + B115200;
2472         if ((cflag & CBAUD)== B19200) cflag=cflag - B19200 + B57600;
2473 #endif /* SPEED_HACK */
2474
2475         if (cflag & CBAUDEX)
2476         { /* Begin detected CBAUDEX */
2477
2478                 ch->digiext.digi_flags |= DIGI_FAST;
2479
2480                 /* -------------------------------------------------------------
2481                    HUPCL bit is used by FEP to indicate fast baud
2482                    table is to be used.
2483                 ----------------------------------------------------------------- */
2484
2485                 res |= FEP_HUPCL;
2486
2487         } /* End detected CBAUDEX */
2488         else ch->digiext.digi_flags &= ~DIGI_FAST; 
2489
2490         /* -------------------------------------------------------------------
2491                 CBAUD has bit position 0x1000 set these days to indicate Linux
2492                 baud rate remap.  Digi hardware can't handle the bit assignment.
2493                 (We use a different bit assignment for high speed.).  Clear this
2494                 bit out.
2495         ---------------------------------------------------------------------- */
2496         res |= cflag & ((CBAUD ^ CBAUDEX) | PARODD | PARENB | CSTOPB | CSIZE);
2497
2498         /* -------------------------------------------------------------
2499                 This gets a little confusing.  The Digi cards have their own
2500                 representation of c_cflags controling baud rate.  For the most
2501                 part this is identical to the Linux implementation.  However;
2502                 Digi supports one rate (76800) that Linux doesn't.  This means 
2503                 that the c_cflag entry that would normally mean 76800 for Digi
2504                 actually means 115200 under Linux.  Without the below mapping,
2505                 a stty 115200 would only drive the board at 76800.  Since 
2506                 the rate 230400 is also found after 76800, the same problem afflicts    
2507                 us when we choose a rate of 230400.  Without the below modificiation
2508                 stty 230400 would actually give us 115200.
2509
2510                 There are two additional differences.  The Linux value for CLOCAL
2511                 (0x800; 0004000) has no meaning to the Digi hardware.  Also in 
2512                 later releases of Linux; the CBAUD define has CBAUDEX (0x1000;
2513                 0010000) ored into it (CBAUD = 0x100f as opposed to 0xf). CBAUDEX
2514                 should be checked for a screened out prior to termios2digi_c 
2515                 returning.  Since CLOCAL isn't used by the board this can be
2516                 ignored as long as the returned value is used only by Digi hardware. 
2517         ----------------------------------------------------------------- */
2518
2519         if (cflag & CBAUDEX)
2520         {
2521                 /* -------------------------------------------------------------
2522                         The below code is trying to guarantee that only baud rates
2523                         115200 and 230400 are remapped.  We use exclusive or because
2524                         the various baud rates share common bit positions and therefore
2525                         can't be tested for easily.
2526                 ----------------------------------------------------------------- */
2527
2528                                 
2529                 if ((!((cflag & 0x7) ^ (B115200 & ~CBAUDEX))) || 
2530                     (!((cflag & 0x7) ^ (B230400 & ~CBAUDEX))))
2531                 {
2532                         res += 1;
2533                 }
2534         }
2535
2536         return res;
2537
2538 } /* End termios2digi_c */
2539
2540 /* --------------------- Begin epcaparam  ----------------------- */
2541
2542 static void epcaparam(struct tty_struct *tty, struct channel *ch)
2543 { /* Begin epcaparam */
2544
2545         unsigned int cmdHead;
2546         struct termios *ts;
2547         volatile struct board_chan *bc;
2548         unsigned mval, hflow, cflag, iflag;
2549
2550         bc = ch->brdchan;
2551         epcaassert(bc !=0, "bc out of range");
2552
2553         assertgwinon(ch);
2554
2555         ts = tty->termios;
2556
2557         if ((ts->c_cflag & CBAUD) == 0) 
2558         { /* Begin CBAUD detected */
2559
2560                 cmdHead = bc->rin;
2561                 bc->rout = cmdHead;
2562                 cmdHead = bc->tin;
2563
2564                 /* Changing baud in mid-stream transmission can be wonderful */
2565                 /* ---------------------------------------------------------------
2566                         Flush current transmit buffer by setting cmdTail pointer (tout)
2567                         to cmdHead pointer (tin).  Hopefully the transmit buffer is empty.
2568                 ----------------------------------------------------------------- */
2569
2570                 fepcmd(ch, STOUT, (unsigned) cmdHead, 0, 0, 0);
2571                 mval = 0;
2572
2573         } /* End CBAUD detected */
2574         else 
2575         { /* Begin CBAUD not detected */
2576
2577                 /* -------------------------------------------------------------------
2578                         c_cflags have changed but that change had nothing to do with BAUD.
2579                         Propagate the change to the card.
2580                 ---------------------------------------------------------------------- */ 
2581
2582                 cflag = termios2digi_c(ch, ts->c_cflag);
2583
2584                 if (cflag != ch->fepcflag) 
2585                 {
2586                         ch->fepcflag = cflag;
2587                         /* Set baud rate, char size, stop bits, parity */
2588                         fepcmd(ch, SETCTRLFLAGS, (unsigned) cflag, 0, 0, 0);
2589                 }
2590
2591
2592                 /* ----------------------------------------------------------------
2593                         If the user has not forced CLOCAL and if the device is not a 
2594                         CALLOUT device (Which is always CLOCAL) we set flags such that
2595                         the driver will wait on carrier detect.
2596                 ------------------------------------------------------------------- */
2597
2598                 if (ts->c_cflag & CLOCAL)
2599                 { /* Begin it is a cud device or a ttyD device with CLOCAL on */
2600                         ch->asyncflags &= ~ASYNC_CHECK_CD;
2601                 } /* End it is a cud device or a ttyD device with CLOCAL on */
2602                 else
2603                 { /* Begin it is a ttyD device */
2604                         ch->asyncflags |= ASYNC_CHECK_CD;
2605                 } /* End it is a ttyD device */
2606
2607                 mval = ch->m_dtr | ch->m_rts;
2608
2609         } /* End CBAUD not detected */
2610
2611         iflag = termios2digi_i(ch, ts->c_iflag);
2612
2613         /* Check input mode flags */
2614
2615         if (iflag != ch->fepiflag) 
2616         {
2617                 ch->fepiflag = iflag;
2618
2619                 /* ---------------------------------------------------------------
2620                         Command sets channels iflag structure on the board. Such things 
2621                         as input soft flow control, handling of parity errors, and
2622                         break handling are all set here.
2623                 ------------------------------------------------------------------- */
2624
2625                 /* break handling, parity handling, input stripping, flow control chars */
2626                 fepcmd(ch, SETIFLAGS, (unsigned int) ch->fepiflag, 0, 0, 0);
2627         }
2628
2629         /* ---------------------------------------------------------------
2630                 Set the board mint value for this channel.  This will cause hardware
2631                 events to be generated each time the DCD signal (Described in mint) 
2632                 changes.        
2633         ------------------------------------------------------------------- */
2634         bc->mint = ch->dcd;
2635
2636         if ((ts->c_cflag & CLOCAL) || (ch->digiext.digi_flags & DIGI_FORCEDCD))
2637                 if (ch->digiext.digi_flags & DIGI_FORCEDCD)
2638                         bc->mint = 0;
2639
2640         ch->imodem = bc->mstat;
2641
2642         hflow = termios2digi_h(ch, ts->c_cflag);
2643
2644         if (hflow != ch->hflow) 
2645         {
2646                 ch->hflow = hflow;
2647
2648                 /* --------------------------------------------------------------
2649                         Hard flow control has been selected but the board is not
2650                         using it.  Activate hard flow control now.
2651                 ----------------------------------------------------------------- */
2652
2653                 fepcmd(ch, SETHFLOW, hflow, 0xff, 0, 1);
2654         }
2655         
2656
2657         mval ^= ch->modemfake & (mval ^ ch->modem);
2658
2659         if (ch->omodem ^ mval) 
2660         {
2661                 ch->omodem = mval;
2662
2663                 /* --------------------------------------------------------------
2664                         The below command sets the DTR and RTS mstat structure.  If
2665                         hard flow control is NOT active these changes will drive the
2666                         output of the actual DTR and RTS lines.  If hard flow control 
2667                         is active, the changes will be saved in the mstat structure and
2668                         only asserted when hard flow control is turned off. 
2669                 ----------------------------------------------------------------- */
2670
2671                 /* First reset DTR & RTS; then set them */
2672                 fepcmd(ch, SETMODEM, 0, ((ch->m_dtr)|(ch->m_rts)), 0, 1);
2673                 fepcmd(ch, SETMODEM, mval, 0, 0, 1);
2674
2675         }
2676
2677         if (ch->startc != ch->fepstartc || ch->stopc != ch->fepstopc) 
2678         {
2679                 ch->fepstartc = ch->startc;
2680                 ch->fepstopc = ch->stopc;
2681
2682                 /* ------------------------------------------------------------
2683                         The XON / XOFF characters have changed; propagate these
2684                         changes to the card.    
2685                 --------------------------------------------------------------- */
2686
2687                 fepcmd(ch, SONOFFC, ch->fepstartc, ch->fepstopc, 0, 1);
2688         }
2689
2690         if (ch->startca != ch->fepstartca || ch->stopca != ch->fepstopca) 
2691         {
2692                 ch->fepstartca = ch->startca;
2693                 ch->fepstopca = ch->stopca;
2694
2695                 /* ---------------------------------------------------------------
2696                         Similar to the above, this time the auxilarly XON / XOFF 
2697                         characters have changed; propagate these changes to the card.
2698                 ------------------------------------------------------------------ */
2699
2700                 fepcmd(ch, SAUXONOFFC, ch->fepstartca, ch->fepstopca, 0, 1);
2701         }
2702
2703 } /* End epcaparam */
2704
2705 /* --------------------- Begin receive_data  ----------------------- */
2706
2707 static void receive_data(struct channel *ch)
2708 { /* Begin receive_data */
2709
2710         unchar *rptr;
2711         struct termios *ts = NULL;
2712         struct tty_struct *tty;
2713         volatile struct board_chan *bc;
2714         register int dataToRead, wrapgap, bytesAvailable;
2715         register unsigned int tail, head;
2716         unsigned int wrapmask;
2717         int rc;
2718
2719
2720         /* ---------------------------------------------------------------
2721                 This routine is called by doint when a receive data event 
2722                 has taken place.
2723         ------------------------------------------------------------------- */
2724
2725         globalwinon(ch);
2726
2727         if (ch->statusflags & RXSTOPPED)
2728                 return;
2729
2730         tty = ch->tty;
2731         if (tty)
2732                 ts = tty->termios;
2733
2734         bc = ch->brdchan;
2735
2736         if (!bc) 
2737         {
2738                 printk(KERN_ERR "<Error> - bc is NULL in receive_data!\n");
2739                 return;
2740         }
2741
2742         wrapmask = ch->rxbufsize - 1;
2743
2744         /* --------------------------------------------------------------------- 
2745                 Get the head and tail pointers to the receiver queue.  Wrap the 
2746                 head pointer if it has reached the end of the buffer.
2747         ------------------------------------------------------------------------ */
2748
2749         head = bc->rin;
2750         head &= wrapmask;
2751         tail = bc->rout & wrapmask;
2752
2753         bytesAvailable = (head - tail) & wrapmask;
2754
2755         if (bytesAvailable == 0)
2756                 return;
2757
2758         /* ------------------------------------------------------------------
2759            If CREAD bit is off or device not open, set TX tail to head
2760         --------------------------------------------------------------------- */
2761
2762         if (!tty || !ts || !(ts->c_cflag & CREAD)) 
2763         {
2764                 bc->rout = head;
2765                 return;
2766         }
2767
2768         if (tty->flip.count == TTY_FLIPBUF_SIZE) 
2769                 return;
2770
2771         if (bc->orun) 
2772         {
2773                 bc->orun = 0;
2774                 printk(KERN_WARNING "overrun! DigiBoard device %s\n",tty->name);
2775         }
2776
2777         rxwinon(ch);
2778         rptr = tty->flip.char_buf_ptr;
2779         rc = tty->flip.count;
2780
2781         while (bytesAvailable > 0) 
2782         { /* Begin while there is data on the card */
2783
2784                 wrapgap = (head >= tail) ? head - tail : ch->rxbufsize - tail;
2785
2786                 /* ---------------------------------------------------------------
2787                         Even if head has wrapped around only report the amount of
2788                         data to be equal to the size - tail.  Remember memcpy can't
2789                         automaticly wrap around the receive buffer.
2790                 ----------------------------------------------------------------- */
2791
2792                 dataToRead = (wrapgap < bytesAvailable) ? wrapgap : bytesAvailable;
2793
2794                 /* --------------------------------------------------------------
2795                    Make sure we don't overflow the buffer
2796                 ----------------------------------------------------------------- */
2797
2798                 if ((rc + dataToRead) > TTY_FLIPBUF_SIZE)
2799                         dataToRead = TTY_FLIPBUF_SIZE - rc;
2800
2801                 if (dataToRead == 0)
2802                         break;
2803
2804                 /* ---------------------------------------------------------------
2805                         Move data read from our card into the line disciplines buffer
2806                         for translation if necessary.
2807                 ------------------------------------------------------------------ */
2808
2809                 if ((memcpy(rptr, ch->rxptr + tail, dataToRead)) != rptr)
2810                         printk(KERN_ERR "<Error> - receive_data : memcpy failed\n");
2811                         
2812                 rc   += dataToRead;
2813                 rptr += dataToRead;
2814                 tail = (tail + dataToRead) & wrapmask;
2815                 bytesAvailable -= dataToRead;
2816
2817         } /* End while there is data on the card */
2818
2819
2820         tty->flip.count = rc;
2821         tty->flip.char_buf_ptr = rptr;
2822         globalwinon(ch);
2823         bc->rout = tail;
2824
2825         /* Must be called with global data */
2826         tty_schedule_flip(ch->tty); 
2827         return;
2828
2829 } /* End receive_data */
2830
2831 static int info_ioctl(struct tty_struct *tty, struct file * file,
2832                     unsigned int cmd, unsigned long arg)
2833 {
2834         switch (cmd) 
2835         { /* Begin switch cmd */
2836
2837                 case DIGI_GETINFO:
2838                 { /* Begin case DIGI_GETINFO */
2839
2840                         struct digi_info di ;
2841                         int brd;
2842
2843                         getUser(brd, (unsigned int __user *)arg);
2844
2845                         if ((brd < 0) || (brd >= num_cards) || (num_cards == 0))
2846                                 return (-ENODEV);
2847
2848                         memset(&di, 0, sizeof(di));
2849
2850                         di.board = brd ; 
2851                         di.status = boards[brd].status;
2852                         di.type = boards[brd].type ;
2853                         di.numports = boards[brd].numports ;
2854                         di.port = boards[brd].port ;
2855                         di.membase = boards[brd].membase ;
2856
2857                         if (copy_to_user((void __user *)arg, &di, sizeof (di)))
2858                                 return -EFAULT;
2859                         break;
2860
2861                 } /* End case DIGI_GETINFO */
2862
2863                 case DIGI_POLLER:
2864                 { /* Begin case DIGI_POLLER */
2865
2866                         int brd = arg & 0xff000000 >> 16 ; 
2867                         unsigned char state = arg & 0xff ; 
2868
2869                         if ((brd < 0) || (brd >= num_cards))
2870                         {
2871                                 printk(KERN_ERR "<Error> - DIGI POLLER : brd not valid!\n");
2872                                 return (-ENODEV);
2873                         }
2874
2875                         digi_poller_inhibited = state ;
2876                         break ; 
2877
2878                 } /* End case DIGI_POLLER */
2879
2880                 case DIGI_INIT:
2881                 { /* Begin case DIGI_INIT */
2882
2883                         /* ------------------------------------------------------------
2884                                 This call is made by the apps to complete the initilization
2885                                 of the board(s).  This routine is responsible for setting
2886                                 the card to its initial state and setting the drivers control
2887                                 fields to the sutianle settings for the card in question.
2888                         ---------------------------------------------------------------- */
2889                 
2890                         int crd ; 
2891                         for (crd = 0; crd < num_cards; crd++) 
2892                                 post_fep_init (crd);
2893
2894                         break ; 
2895
2896                 } /* End case DIGI_INIT */
2897
2898
2899                 default:
2900                         return -ENOIOCTLCMD;
2901
2902         } /* End switch cmd */
2903         return (0) ;
2904 }
2905 /* --------------------- Begin pc_ioctl  ----------------------- */
2906
2907 static int pc_tiocmget(struct tty_struct *tty, struct file *file)
2908 {
2909         struct channel *ch = (struct channel *) tty->driver_data;
2910         volatile struct board_chan *bc;
2911         unsigned int mstat, mflag = 0;
2912         unsigned long flags;
2913
2914         if (ch)
2915                 bc = ch->brdchan;
2916         else
2917         {
2918                 printk(KERN_ERR "<Error> - ch is NULL in pc_tiocmget!\n");
2919                 return(-EINVAL);
2920         }
2921
2922         save_flags(flags);
2923         cli();
2924         globalwinon(ch);
2925         mstat = bc->mstat;
2926         memoff(ch);
2927         restore_flags(flags);
2928
2929         if (mstat & ch->m_dtr)
2930                 mflag |= TIOCM_DTR;
2931
2932         if (mstat & ch->m_rts)
2933                 mflag |= TIOCM_RTS;
2934
2935         if (mstat & ch->m_cts)
2936                 mflag |= TIOCM_CTS;
2937
2938         if (mstat & ch->dsr)
2939                 mflag |= TIOCM_DSR;
2940
2941         if (mstat & ch->m_ri)
2942                 mflag |= TIOCM_RI;
2943
2944         if (mstat & ch->dcd)
2945                 mflag |= TIOCM_CD;
2946
2947         return mflag;
2948 }
2949
2950 static int pc_tiocmset(struct tty_struct *tty, struct file *file,
2951                        unsigned int set, unsigned int clear)
2952 {
2953         struct channel *ch = (struct channel *) tty->driver_data;
2954         unsigned long flags;
2955
2956         if (!ch) {
2957                 printk(KERN_ERR "<Error> - ch is NULL in pc_tiocmset!\n");
2958                 return(-EINVAL);
2959         }
2960
2961         save_flags(flags);
2962         cli();
2963         /*
2964          * I think this modemfake stuff is broken.  It doesn't
2965          * correctly reflect the behaviour desired by the TIOCM*
2966          * ioctls.  Therefore this is probably broken.
2967          */
2968         if (set & TIOCM_RTS) {
2969                 ch->modemfake |= ch->m_rts;
2970                 ch->modem |= ch->m_rts;
2971         }
2972         if (set & TIOCM_DTR) {
2973                 ch->modemfake |= ch->m_dtr;
2974                 ch->modem |= ch->m_dtr;
2975         }
2976         if (clear & TIOCM_RTS) {
2977                 ch->modemfake |= ch->m_rts;
2978                 ch->modem &= ~ch->m_rts;
2979         }
2980         if (clear & TIOCM_DTR) {
2981                 ch->modemfake |= ch->m_dtr;
2982                 ch->modem &= ~ch->m_dtr;
2983         }
2984
2985         globalwinon(ch);
2986
2987         /*  --------------------------------------------------------------
2988                 The below routine generally sets up parity, baud, flow control
2989                 issues, etc.... It effect both control flags and input flags.
2990         ------------------------------------------------------------------ */
2991
2992         epcaparam(tty,ch);
2993         memoff(ch);
2994         restore_flags(flags);
2995         return 0;
2996 }
2997
2998 static int pc_ioctl(struct tty_struct *tty, struct file * file,
2999                     unsigned int cmd, unsigned long arg)
3000 { /* Begin pc_ioctl */
3001
3002         digiflow_t dflow;
3003         int retval;
3004         unsigned long flags;
3005         unsigned int mflag, mstat;
3006         unsigned char startc, stopc;
3007         volatile struct board_chan *bc;
3008         struct channel *ch = (struct channel *) tty->driver_data;
3009         void __user *argp = (void __user *)arg;
3010         
3011         if (ch)
3012                 bc = ch->brdchan;
3013         else 
3014         {
3015                 printk(KERN_ERR "<Error> - ch is NULL in pc_ioctl!\n");
3016                 return(-EINVAL);
3017         }
3018
3019         save_flags(flags);
3020
3021         /* -------------------------------------------------------------------
3022                 For POSIX compliance we need to add more ioctls.  See tty_ioctl.c
3023                 in /usr/src/linux/drivers/char for a good example.  In particular 
3024                 think about adding TCSETAF, TCSETAW, TCSETA, TCSETSF, TCSETSW, TCSETS.
3025         ---------------------------------------------------------------------- */
3026
3027         switch (cmd) 
3028         { /* Begin switch cmd */
3029
3030                 case TCGETS:
3031                         if (copy_to_user(argp, 
3032                                          tty->termios, sizeof(struct termios)))
3033                                 return -EFAULT;
3034                         return(0);
3035
3036                 case TCGETA:
3037                         return get_termio(tty, argp);
3038
3039                 case TCSBRK:    /* SVID version: non-zero arg --> no break */
3040
3041                         retval = tty_check_change(tty);
3042                         if (retval)
3043                                 return retval;
3044
3045                         /* Setup an event to indicate when the transmit buffer empties */
3046
3047                         setup_empty_event(tty,ch);              
3048                         tty_wait_until_sent(tty, 0);
3049                         if (!arg)
3050                                 digi_send_break(ch, HZ/4);    /* 1/4 second */
3051                         return 0;
3052
3053                 case TCSBRKP:   /* support for POSIX tcsendbreak() */
3054
3055                         retval = tty_check_change(tty);
3056                         if (retval)
3057                                 return retval;
3058
3059                         /* Setup an event to indicate when the transmit buffer empties */
3060
3061                         setup_empty_event(tty,ch);              
3062                         tty_wait_until_sent(tty, 0);
3063                         digi_send_break(ch, arg ? arg*(HZ/10) : HZ/4);
3064                         return 0;
3065
3066                 case TIOCGSOFTCAR:
3067                         if (put_user(C_CLOCAL(tty)?1:0, (unsigned long __user *)arg))
3068                                 return -EFAULT;
3069                         return 0;
3070
3071                 case TIOCSSOFTCAR:
3072                 {
3073                         unsigned int value;
3074
3075                         if (get_user(value, (unsigned __user *)argp))
3076                                 return -EFAULT;
3077                         tty->termios->c_cflag =
3078                                 ((tty->termios->c_cflag & ~CLOCAL) |
3079                                  (value ? CLOCAL : 0));
3080                         return 0;
3081                 }
3082
3083                 case TIOCMODG:
3084                         mflag = pc_tiocmget(tty, file);
3085                         if (put_user(mflag, (unsigned long __user *)argp))
3086                                 return -EFAULT;
3087                         break;
3088
3089                 case TIOCMODS:
3090                         if (get_user(mstat, (unsigned __user *)argp))
3091                                 return -EFAULT;
3092                         return pc_tiocmset(tty, file, mstat, ~mstat);
3093
3094                 case TIOCSDTR:
3095                         ch->omodem |= ch->m_dtr;
3096                         cli();
3097                         globalwinon(ch);
3098                         fepcmd(ch, SETMODEM, ch->m_dtr, 0, 10, 1);
3099                         memoff(ch);
3100                         restore_flags(flags);
3101                         break;
3102
3103                 case TIOCCDTR:
3104                         ch->omodem &= ~ch->m_dtr;
3105                         cli();
3106                         globalwinon(ch);
3107                         fepcmd(ch, SETMODEM, 0, ch->m_dtr, 10, 1);
3108                         memoff(ch);
3109                         restore_flags(flags);
3110                         break;
3111
3112                 case DIGI_GETA:
3113                         if (copy_to_user(argp, &ch->digiext, sizeof(digi_t)))
3114                                 return -EFAULT;
3115                         break;
3116
3117                 case DIGI_SETAW:
3118                 case DIGI_SETAF:
3119                         if ((cmd) == (DIGI_SETAW)) 
3120                         {
3121                                 /* Setup an event to indicate when the transmit buffer empties */
3122
3123                                 setup_empty_event(tty,ch);              
3124                                 tty_wait_until_sent(tty, 0);
3125                         }
3126                         else 
3127                         {
3128                                 /* ldisc lock already held in ioctl */
3129                                 if (tty->ldisc.flush_buffer)
3130                                         tty->ldisc.flush_buffer(tty);
3131                         }
3132
3133                         /* Fall Thru */
3134
3135                 case DIGI_SETA:
3136                         if (copy_from_user(&ch->digiext, argp, sizeof(digi_t)))
3137                                 return -EFAULT;
3138                         
3139                         if (ch->digiext.digi_flags & DIGI_ALTPIN) 
3140                         {
3141                                 ch->dcd = ch->m_dsr;
3142                                 ch->dsr = ch->m_dcd;
3143                         } 
3144                         else 
3145                         {
3146                                 ch->dcd = ch->m_dcd;
3147                                 ch->dsr = ch->m_dsr;
3148                         }
3149                 
3150                         cli();
3151                         globalwinon(ch);
3152
3153                         /* -----------------------------------------------------------------
3154                                 The below routine generally sets up parity, baud, flow control 
3155                                 issues, etc.... It effect both control flags and input flags.
3156                         ------------------------------------------------------------------- */
3157
3158                         epcaparam(tty,ch);
3159                         memoff(ch);
3160                         restore_flags(flags);
3161                         break;
3162
3163                 case DIGI_GETFLOW:
3164                 case DIGI_GETAFLOW:
3165                         cli();  
3166                         globalwinon(ch);
3167                         if ((cmd) == (DIGI_GETFLOW)) 
3168                         {
3169                                 dflow.startc = bc->startc;
3170                                 dflow.stopc = bc->stopc;
3171                         }
3172                         else 
3173                         {
3174                                 dflow.startc = bc->startca;
3175                                 dflow.stopc = bc->stopca;
3176                         }
3177                         memoff(ch);
3178                         restore_flags(flags);
3179
3180                         if (copy_to_user(argp, &dflow, sizeof(dflow)))
3181                                 return -EFAULT;
3182                         break;
3183
3184                 case DIGI_SETAFLOW:
3185                 case DIGI_SETFLOW:
3186                         if ((cmd) == (DIGI_SETFLOW)) 
3187                         {
3188                                 startc = ch->startc;
3189                                 stopc = ch->stopc;
3190                         } 
3191                         else 
3192                         {
3193                                 startc = ch->startca;
3194                                 stopc = ch->stopca;
3195                         }
3196
3197                         if (copy_from_user(&dflow, argp, sizeof(dflow)))
3198                                 return -EFAULT;
3199
3200                         if (dflow.startc != startc || dflow.stopc != stopc) 
3201                         { /* Begin  if setflow toggled */
3202                                 cli();
3203                                 globalwinon(ch);
3204
3205                                 if ((cmd) == (DIGI_SETFLOW)) 
3206                                 {
3207                                         ch->fepstartc = ch->startc = dflow.startc;
3208                                         ch->fepstopc = ch->stopc = dflow.stopc;
3209                                         fepcmd(ch, SONOFFC, ch->fepstartc, ch->fepstopc, 0, 1);
3210                                 } 
3211                                 else 
3212                                 {
3213                                         ch->fepstartca = ch->startca = dflow.startc;
3214                                         ch->fepstopca  = ch->stopca = dflow.stopc;
3215                                         fepcmd(ch, SAUXONOFFC, ch->fepstartca, ch->fepstopca, 0, 1);
3216                                 }
3217
3218                                 if      (ch->statusflags & TXSTOPPED)
3219                                         pc_start(tty);
3220
3221                                 memoff(ch);
3222                                 restore_flags(flags);
3223
3224                         } /* End if setflow toggled */
3225                         break;
3226
3227                 default:
3228                         return -ENOIOCTLCMD;
3229
3230         } /* End switch cmd */
3231
3232         return 0;
3233
3234 } /* End pc_ioctl */
3235
3236 /* --------------------- Begin pc_set_termios  ----------------------- */
3237
3238 static void pc_set_termios(struct tty_struct *tty, struct termios *old_termios)
3239 { /* Begin pc_set_termios */
3240
3241         struct channel *ch;
3242         unsigned long flags;
3243
3244         /* ---------------------------------------------------------
3245                 verifyChannel returns the channel from the tty struct
3246                 if it is valid.  This serves as a sanity check.
3247         ------------------------------------------------------------- */
3248
3249         if ((ch = verifyChannel(tty)) != NULL) 
3250         { /* Begin if channel valid */
3251
3252                 save_flags(flags);
3253                 cli();
3254                 globalwinon(ch);
3255                 epcaparam(tty, ch);
3256                 memoff(ch);
3257
3258                 if ((old_termios->c_cflag & CRTSCTS) &&
3259                          ((tty->termios->c_cflag & CRTSCTS) == 0))
3260                         tty->hw_stopped = 0;
3261
3262                 if (!(old_termios->c_cflag & CLOCAL) &&
3263                          (tty->termios->c_cflag & CLOCAL))
3264                         wake_up_interruptible(&ch->open_wait);
3265
3266                 restore_flags(flags);
3267
3268         } /* End if channel valid */
3269
3270 } /* End pc_set_termios */
3271
3272 /* --------------------- Begin do_softint  ----------------------- */
3273
3274 static void do_softint(void *private_)
3275 { /* Begin do_softint */
3276
3277         struct channel *ch = (struct channel *) private_;
3278         
3279
3280         /* Called in response to a modem change event */
3281
3282         if (ch && ch->magic == EPCA_MAGIC) 
3283         { /* Begin EPCA_MAGIC */
3284
3285                 struct tty_struct *tty = ch->tty;
3286
3287                 if (tty && tty->driver_data) 
3288                 { 
3289                         if (test_and_clear_bit(EPCA_EVENT_HANGUP, &ch->event)) 
3290                         { /* Begin if clear_bit */
3291
3292                                 tty_hangup(tty);        /* FIXME: module removal race here - AKPM */
3293                                 wake_up_interruptible(&ch->open_wait);
3294                                 ch->asyncflags &= ~ASYNC_NORMAL_ACTIVE;
3295
3296                         } /* End if clear_bit */
3297                 }
3298
3299         } /* End EPCA_MAGIC */
3300 } /* End do_softint */
3301
3302 /* ------------------------------------------------------------
3303         pc_stop and pc_start provide software flow control to the 
3304         routine and the pc_ioctl routine.
3305 ---------------------------------------------------------------- */
3306
3307 /* --------------------- Begin pc_stop  ----------------------- */
3308
3309 static void pc_stop(struct tty_struct *tty)
3310 { /* Begin pc_stop */
3311
3312         struct channel *ch;
3313         unsigned long flags;
3314
3315         /* ---------------------------------------------------------
3316                 verifyChannel returns the channel from the tty struct
3317                 if it is valid.  This serves as a sanity check.
3318         ------------------------------------------------------------- */
3319
3320         if ((ch = verifyChannel(tty)) != NULL) 
3321         { /* Begin if valid channel */
3322
3323                 save_flags(flags); 
3324                 cli();
3325
3326                 if ((ch->statusflags & TXSTOPPED) == 0) 
3327                 { /* Begin if transmit stop requested */
3328
3329                         globalwinon(ch);
3330
3331                         /* STOP transmitting now !! */
3332
3333                         fepcmd(ch, PAUSETX, 0, 0, 0, 0);
3334
3335                         ch->statusflags |= TXSTOPPED;
3336                         memoff(ch);
3337
3338                 } /* End if transmit stop requested */
3339
3340                 restore_flags(flags);
3341
3342         } /* End if valid channel */
3343
3344 } /* End pc_stop */
3345
3346 /* --------------------- Begin pc_start  ----------------------- */
3347
3348 static void pc_start(struct tty_struct *tty)
3349 { /* Begin pc_start */
3350
3351         struct channel *ch;
3352
3353         /* ---------------------------------------------------------
3354                 verifyChannel returns the channel from the tty struct
3355                 if it is valid.  This serves as a sanity check.
3356         ------------------------------------------------------------- */
3357
3358         if ((ch = verifyChannel(tty)) != NULL) 
3359         { /* Begin if channel valid */
3360
3361                 unsigned long flags;
3362
3363                 save_flags(flags);
3364                 cli();
3365
3366                 /* Just in case output was resumed because of a change in Digi-flow */
3367                 if (ch->statusflags & TXSTOPPED) 
3368                 { /* Begin transmit resume requested */
3369
3370                         volatile struct board_chan *bc;
3371
3372                         globalwinon(ch);
3373                         bc = ch->brdchan;
3374                         if (ch->statusflags & LOWWAIT)
3375                                 bc->ilow = 1;
3376
3377                         /* Okay, you can start transmitting again... */
3378
3379                         fepcmd(ch, RESUMETX, 0, 0, 0, 0);
3380
3381                         ch->statusflags &= ~TXSTOPPED;
3382                         memoff(ch);
3383
3384                 } /* End transmit resume requested */
3385
3386                 restore_flags(flags);
3387
3388         } /* End if channel valid */
3389
3390 } /* End pc_start */
3391
3392 /* ------------------------------------------------------------------
3393         The below routines pc_throttle and pc_unthrottle are used 
3394         to slow (And resume) the receipt of data into the kernels
3395         receive buffers.  The exact occurrence of this depends on the
3396         size of the kernels receive buffer and what the 'watermarks'
3397         are set to for that buffer.  See the n_ttys.c file for more
3398         details. 
3399 ______________________________________________________________________ */
3400 /* --------------------- Begin throttle  ----------------------- */
3401
3402 static void pc_throttle(struct tty_struct * tty)
3403 { /* Begin pc_throttle */
3404
3405         struct channel *ch;
3406         unsigned long flags;
3407
3408         /* ---------------------------------------------------------
3409                 verifyChannel returns the channel from the tty struct
3410                 if it is valid.  This serves as a sanity check.
3411         ------------------------------------------------------------- */
3412
3413         if ((ch = verifyChannel(tty)) != NULL) 
3414         { /* Begin if channel valid */
3415
3416
3417                 save_flags(flags);
3418                 cli();
3419
3420                 if ((ch->statusflags & RXSTOPPED) == 0)
3421                 {
3422                         globalwinon(ch);
3423                         fepcmd(ch, PAUSERX, 0, 0, 0, 0);
3424
3425                         ch->statusflags |= RXSTOPPED;
3426                         memoff(ch);
3427                 }
3428                 restore_flags(flags);
3429
3430         } /* End if channel valid */
3431
3432 } /* End pc_throttle */
3433
3434 /* --------------------- Begin unthrottle  ----------------------- */
3435
3436 static void pc_unthrottle(struct tty_struct *tty)
3437 { /* Begin pc_unthrottle */
3438
3439         struct channel *ch;
3440         unsigned long flags;
3441         volatile struct board_chan *bc;
3442
3443
3444         /* ---------------------------------------------------------
3445                 verifyChannel returns the channel from the tty struct
3446                 if it is valid.  This serves as a sanity check.
3447         ------------------------------------------------------------- */
3448
3449         if ((ch = verifyChannel(tty)) != NULL) 
3450         { /* Begin if channel valid */
3451
3452
3453                 /* Just in case output was resumed because of a change in Digi-flow */
3454                 save_flags(flags);
3455                 cli();
3456
3457                 if (ch->statusflags & RXSTOPPED) 
3458                 {
3459
3460                         globalwinon(ch);
3461                         bc = ch->brdchan;
3462                         fepcmd(ch, RESUMERX, 0, 0, 0, 0);
3463
3464                         ch->statusflags &= ~RXSTOPPED;
3465                         memoff(ch);
3466                 }
3467                 restore_flags(flags);
3468
3469         } /* End if channel valid */
3470
3471 } /* End pc_unthrottle */
3472
3473 /* --------------------- Begin digi_send_break  ----------------------- */
3474
3475 void digi_send_break(struct channel *ch, int msec)
3476 { /* Begin digi_send_break */
3477
3478         unsigned long flags;
3479
3480         save_flags(flags);
3481         cli();
3482         globalwinon(ch);
3483
3484         /* -------------------------------------------------------------------- 
3485            Maybe I should send an infinite break here, schedule() for
3486            msec amount of time, and then stop the break.  This way,
3487            the user can't screw up the FEP by causing digi_send_break()
3488            to be called (i.e. via an ioctl()) more than once in msec amount 
3489            of time.  Try this for now...
3490         ------------------------------------------------------------------------ */
3491
3492         fepcmd(ch, SENDBREAK, msec, 0, 10, 0);
3493         memoff(ch);
3494
3495         restore_flags(flags);
3496
3497 } /* End digi_send_break */
3498
3499 /* --------------------- Begin setup_empty_event  ----------------------- */
3500
3501 static void setup_empty_event(struct tty_struct *tty, struct channel *ch)
3502 { /* Begin setup_empty_event */
3503
3504         volatile struct board_chan *bc = ch->brdchan;
3505         unsigned long int flags;
3506
3507         save_flags(flags);
3508         cli();
3509         globalwinon(ch);
3510         ch->statusflags |= EMPTYWAIT;
3511         
3512         /* ------------------------------------------------------------------
3513                 When set the iempty flag request a event to be generated when the 
3514                 transmit buffer is empty (If there is no BREAK in progress).
3515         --------------------------------------------------------------------- */
3516
3517         bc->iempty = 1;
3518         memoff(ch);
3519         restore_flags(flags);
3520
3521 } /* End setup_empty_event */
3522
3523 /* --------------------- Begin get_termio ----------------------- */
3524
3525 static int get_termio(struct tty_struct * tty, struct termio __user * termio)
3526 { /* Begin get_termio */
3527         return kernel_termios_to_user_termio(termio, tty->termios);
3528 } /* End get_termio */
3529 /* ---------------------- Begin epca_setup  -------------------------- */
3530 void epca_setup(char *str, int *ints)
3531 { /* Begin epca_setup */
3532
3533         struct board_info board;
3534         int               index, loop, last;
3535         char              *temp, *t2;
3536         unsigned          len;
3537
3538         /* ----------------------------------------------------------------------
3539                 If this routine looks a little strange it is because it is only called
3540                 if a LILO append command is given to boot the kernel with parameters.  
3541                 In this way, we can provide the user a method of changing his board
3542                 configuration without rebuilding the kernel.
3543         ----------------------------------------------------------------------- */
3544         if (!liloconfig) 
3545                 liloconfig = 1; 
3546
3547         memset(&board, 0, sizeof(board));
3548
3549         /* Assume the data is int first, later we can change it */
3550         /* I think that array position 0 of ints holds the number of args */
3551         for (last = 0, index = 1; index <= ints[0]; index++)
3552                 switch(index)
3553                 { /* Begin parse switch */
3554
3555                         case 1:
3556                                 board.status = ints[index];
3557                                 
3558                                 /* ---------------------------------------------------------
3559                                         We check for 2 (As opposed to 1; because 2 is a flag
3560                                         instructing the driver to ignore epcaconfig.)  For this
3561                                         reason we check for 2.
3562                                 ------------------------------------------------------------ */ 
3563                                 if (board.status == 2)
3564                                 { /* Begin ignore epcaconfig as well as lilo cmd line */
3565                                         nbdevs = 0;
3566                                         num_cards = 0;
3567                                         return;
3568                                 } /* End ignore epcaconfig as well as lilo cmd line */
3569         
3570                                 if (board.status > 2)
3571                                 {
3572                                         printk(KERN_ERR "<Error> - epca_setup: Invalid board status 0x%x\n", board.status);
3573                                         invalid_lilo_config = 1;
3574                                         setup_error_code |= INVALID_BOARD_STATUS;
3575                                         return;
3576                                 }
3577                                 last = index;
3578                                 break;
3579
3580                         case 2:
3581                                 board.type = ints[index];
3582                                 if (board.type >= PCIXEM) 
3583                                 {
3584                                         printk(KERN_ERR "<Error> - epca_setup: Invalid board type 0x%x\n", board.type);
3585                                         invalid_lilo_config = 1;
3586                                         setup_error_code |= INVALID_BOARD_TYPE;
3587                                         return;
3588                                 }
3589                                 last = index;
3590                                 break;
3591
3592                         case 3:
3593                                 board.altpin = ints[index];
3594                                 if (board.altpin > 1)
3595                                 {
3596                                         printk(KERN_ERR "<Error> - epca_setup: Invalid board altpin 0x%x\n", board.altpin);
3597                                         invalid_lilo_config = 1;
3598                                         setup_error_code |= INVALID_ALTPIN;
3599                                         return;
3600                                 }
3601                                 last = index;
3602                                 break;
3603
3604                         case 4:
3605                                 board.numports = ints[index];
3606                                 if ((board.numports < 2) || (board.numports > 256))
3607                                 {
3608                                         printk(KERN_ERR "<Error> - epca_setup: Invalid board numports 0x%x\n", board.numports);
3609                                         invalid_lilo_config = 1;
3610                                         setup_error_code |= INVALID_NUM_PORTS;
3611                                         return;
3612                                 }
3613                                 nbdevs += board.numports;
3614                                 last = index;
3615                                 break;
3616
3617                         case 5:
3618                                 board.port = (unsigned char *)ints[index];
3619                                 if (ints[index] <= 0)
3620                                 {
3621                                         printk(KERN_ERR "<Error> - epca_setup: Invalid io port 0x%x\n", (unsigned int)board.port);
3622                                         invalid_lilo_config = 1;
3623                                         setup_error_code |= INVALID_PORT_BASE;
3624                                         return;
3625                                 }
3626                                 last = index;
3627                                 break;
3628
3629                         case 6:
3630                                 board.membase = (unsigned char *)ints[index];
3631                                 if (ints[index] <= 0)
3632                                 {
3633                                         printk(KERN_ERR "<Error> - epca_setup: Invalid memory base 0x%x\n",(unsigned int)board.membase);
3634                                         invalid_lilo_config = 1;
3635                                         setup_error_code |= INVALID_MEM_BASE;
3636                                         return;
3637                                 }
3638                                 last = index;
3639                                 break;
3640
3641                         default:
3642                                 printk(KERN_ERR "<Error> - epca_setup: Too many integer parms\n");
3643                                 return;
3644
3645                 } /* End parse switch */
3646
3647         while (str && *str) 
3648         { /* Begin while there is a string arg */
3649
3650                 /* find the next comma or terminator */
3651                 temp = str;
3652
3653                 /* While string is not null, and a comma hasn't been found */
3654                 while (*temp && (*temp != ','))
3655                         temp++;
3656
3657                 if (!*temp)
3658                         temp = NULL;
3659                 else
3660                         *temp++ = 0;
3661
3662                 /* Set index to the number of args + 1 */
3663                 index = last + 1;
3664
3665                 switch(index)
3666                 {
3667                         case 1:
3668                                 len = strlen(str);
3669                                 if (strncmp("Disable", str, len) == 0) 
3670                                         board.status = 0;
3671                                 else
3672                                 if (strncmp("Enable", str, len) == 0)
3673                                         board.status = 1;
3674                                 else
3675                                 {
3676                                         printk(KERN_ERR "<Error> - epca_setup: Invalid status %s\n", str);
3677                                         invalid_lilo_config = 1;
3678                                         setup_error_code |= INVALID_BOARD_STATUS;
3679                                         return;
3680                                 }
3681                                 last = index;
3682                                 break;
3683
3684                         case 2:
3685
3686                                 for(loop = 0; loop < EPCA_NUM_TYPES; loop++)
3687                                         if (strcmp(board_desc[loop], str) == 0)
3688                                                 break;
3689
3690
3691                                 /* ---------------------------------------------------------------
3692                                         If the index incremented above refers to a legitamate board 
3693                                         type set it here. 
3694                                 ------------------------------------------------------------------*/
3695
3696                                 if (index < EPCA_NUM_TYPES) 
3697                                         board.type = loop;
3698                                 else
3699                                 {
3700                                         printk(KERN_ERR "<Error> - epca_setup: Invalid board type: %s\n", str);
3701                                         invalid_lilo_config = 1;
3702                                         setup_error_code |= INVALID_BOARD_TYPE;
3703                                         return;
3704                                 }
3705                                 last = index;
3706                                 break;
3707
3708                         case 3:
3709                                 len = strlen(str);
3710                                 if (strncmp("Disable", str, len) == 0) 
3711                                         board.altpin = 0;
3712                                 else
3713                                 if (strncmp("Enable", str, len) == 0)
3714                                         board.altpin = 1;
3715                                 else
3716                                 {
3717                                         printk(KERN_ERR "<Error> - epca_setup: Invalid altpin %s\n", str);
3718                                         invalid_lilo_config = 1;
3719                                         setup_error_code |= INVALID_ALTPIN;
3720                                         return;
3721                                 }
3722                                 last = index;
3723                                 break;
3724
3725                         case 4:
3726                                 t2 = str;
3727                                 while (isdigit(*t2))
3728                                         t2++;
3729
3730                                 if (*t2)
3731                                 {
3732                                         printk(KERN_ERR "<Error> - epca_setup: Invalid port count %s\n", str);
3733                                         invalid_lilo_config = 1;
3734                                         setup_error_code |= INVALID_NUM_PORTS;
3735                                         return;
3736                                 }
3737
3738                                 /* ------------------------------------------------------------
3739                                         There is not a man page for simple_strtoul but the code can be 
3740                                         found in vsprintf.c.  The first argument is the string to 
3741                                         translate (To an unsigned long obviously),  the second argument
3742                                         can be the address of any character variable or a NULL.  If a
3743                                         variable is given, the end pointer of the string will be stored 
3744                                         in that variable; if a NULL is given the end pointer will 
3745                                         not be returned.  The last argument is the base to use.  If 
3746                                         a 0 is indicated, the routine will attempt to determine the 
3747                                         proper base by looking at the values prefix (A '0' for octal,
3748                                         a 'x' for hex, etc ...  If a value is given it will use that 
3749                                         value as the base. 
3750                                 ---------------------------------------------------------------- */ 
3751                                 board.numports = simple_strtoul(str, NULL, 0);
3752                                 nbdevs += board.numports;
3753                                 last = index;
3754                                 break;
3755
3756                         case 5:
3757                                 t2 = str;
3758                                 while (isxdigit(*t2))
3759                                         t2++;
3760
3761                                 if (*t2)
3762                                 {
3763                                         printk(KERN_ERR "<Error> - epca_setup: Invalid i/o address %s\n", str);
3764                                         invalid_lilo_config = 1;
3765                                         setup_error_code |= INVALID_PORT_BASE;
3766                                         return;
3767                                 }
3768
3769                                 board.port = (unsigned char *)simple_strtoul(str, NULL, 16);
3770                                 last = index;
3771                                 break;
3772
3773                         case 6:
3774                                 t2 = str;
3775                                 while (isxdigit(*t2))
3776                                         t2++;
3777
3778                                 if (*t2)
3779                                 {
3780                                         printk(KERN_ERR "<Error> - epca_setup: Invalid memory base %s\n",str);
3781                                         invalid_lilo_config = 1;
3782                                         setup_error_code |= INVALID_MEM_BASE;
3783                                         return;
3784                                 }
3785
3786                                 board.membase = (unsigned char *)simple_strtoul(str, NULL, 16);
3787                                 last = index;
3788                                 break;
3789
3790                         default:
3791                                 printk(KERN_ERR "PC/Xx: Too many string parms\n");
3792                                 return;
3793                 }
3794                 str = temp;
3795
3796         } /* End while there is a string arg */
3797
3798
3799         if (last < 6)  
3800         {
3801                 printk(KERN_ERR "PC/Xx: Insufficient parms specified\n");
3802                 return;
3803         }
3804  
3805         /* I should REALLY validate the stuff here */
3806
3807         /* Copies our local copy of board into boards */
3808         memcpy((void *)&boards[num_cards],(void *)&board, sizeof(board));
3809
3810
3811         /* Does this get called once per lilo arg are what ? */
3812
3813         printk(KERN_INFO "PC/Xx: Added board %i, %s %i ports at 0x%4.4X base 0x%6.6X\n", 
3814                 num_cards, board_desc[board.type], 
3815                 board.numports, (int)board.port, (unsigned int) board.membase);
3816
3817         num_cards++;
3818
3819 } /* End epca_setup */
3820
3821
3822
3823 #ifdef ENABLE_PCI
3824 /* ------------------------ Begin init_PCI  --------------------------- */
3825
3826 enum epic_board_types {
3827         brd_xr = 0,
3828         brd_xem,
3829         brd_cx,
3830         brd_xrj,
3831 };
3832
3833
3834 /* indexed directly by epic_board_types enum */
3835 static struct {
3836         unsigned char board_type;
3837         unsigned bar_idx;               /* PCI base address region */
3838 } epca_info_tbl[] = {
3839         { PCIXR, 0, },
3840         { PCIXEM, 0, },
3841         { PCICX, 0, },
3842         { PCIXRJ, 2, },
3843 };
3844
3845
3846 static int __devinit epca_init_one (struct pci_dev *pdev,
3847                                  const struct pci_device_id *ent)
3848 {
3849         static int board_num = -1;
3850         int board_idx, info_idx = ent->driver_data;
3851         unsigned long addr;
3852
3853         if (pci_enable_device(pdev))
3854                 return -EIO;
3855
3856         board_num++;
3857         board_idx = board_num + num_cards;
3858         if (board_idx >= MAXBOARDS)
3859                 goto err_out;
3860         
3861         addr = pci_resource_start (pdev, epca_info_tbl[info_idx].bar_idx);
3862         if (!addr) {
3863                 printk (KERN_ERR PFX "PCI region #%d not available (size 0)\n",
3864                         epca_info_tbl[info_idx].bar_idx);
3865                 goto err_out;
3866         }
3867
3868         boards[board_idx].status = ENABLED;
3869         boards[board_idx].type = epca_info_tbl[info_idx].board_type;
3870         boards[board_idx].numports = 0x0;
3871         boards[board_idx].port =
3872                 (unsigned char *)((char *) addr + PCI_IO_OFFSET);
3873         boards[board_idx].membase =
3874                 (unsigned char *)((char *) addr);
3875
3876         if (!request_mem_region (addr + PCI_IO_OFFSET, 0x200000, "epca")) {
3877                 printk (KERN_ERR PFX "resource 0x%x @ 0x%lx unavailable\n",
3878                         0x200000, addr + PCI_IO_OFFSET);
3879                 goto err_out;
3880         }
3881
3882         boards[board_idx].re_map_port = ioremap(addr + PCI_IO_OFFSET, 0x200000);
3883         if (!boards[board_idx].re_map_port) {
3884                 printk (KERN_ERR PFX "cannot map 0x%x @ 0x%lx\n",
3885                         0x200000, addr + PCI_IO_OFFSET);
3886                 goto err_out_free_pciio;
3887         }
3888
3889         if (!request_mem_region (addr, 0x200000, "epca")) {
3890                 printk (KERN_ERR PFX "resource 0x%x @ 0x%lx unavailable\n",
3891                         0x200000, addr);
3892                 goto err_out_free_iounmap;
3893         }
3894
3895         boards[board_idx].re_map_membase = ioremap(addr, 0x200000);
3896         if (!boards[board_idx].re_map_membase) {
3897                 printk (KERN_ERR PFX "cannot map 0x%x @ 0x%lx\n",
3898                         0x200000, addr + PCI_IO_OFFSET);
3899                 goto err_out_free_memregion;
3900         }
3901
3902         /* --------------------------------------------------------------
3903                 I don't know what the below does, but the hardware guys say
3904                 its required on everything except PLX (In this case XRJ).
3905         ---------------------------------------------------------------- */
3906         if (info_idx != brd_xrj) {
3907                 pci_write_config_byte(pdev, 0x40, 0);  
3908                 pci_write_config_byte(pdev, 0x46, 0);
3909         }
3910         
3911         return 0;
3912
3913 err_out_free_memregion:
3914         release_mem_region (addr, 0x200000);
3915 err_out_free_iounmap:
3916         iounmap (boards[board_idx].re_map_port);
3917 err_out_free_pciio:
3918         release_mem_region (addr + PCI_IO_OFFSET, 0x200000);
3919 err_out:
3920         return -ENODEV;
3921 }
3922
3923
3924 static struct pci_device_id epca_pci_tbl[] = {
3925         { PCI_VENDOR_DIGI, PCI_DEVICE_XR, PCI_ANY_ID, PCI_ANY_ID, 0, 0, brd_xr },
3926         { PCI_VENDOR_DIGI, PCI_DEVICE_XEM, PCI_ANY_ID, PCI_ANY_ID, 0, 0, brd_xem },
3927         { PCI_VENDOR_DIGI, PCI_DEVICE_CX, PCI_ANY_ID, PCI_ANY_ID, 0, 0, brd_cx },
3928         { PCI_VENDOR_DIGI, PCI_DEVICE_XRJ, PCI_ANY_ID, PCI_ANY_ID, 0, 0, brd_xrj },
3929         { 0, }
3930 };
3931
3932 MODULE_DEVICE_TABLE(pci, epca_pci_tbl);
3933
3934 int __init init_PCI (void)
3935 { /* Begin init_PCI */
3936         
3937         int pci_count;
3938         
3939         memset (&epca_driver, 0, sizeof (epca_driver));
3940         epca_driver.name = "epca";
3941         epca_driver.id_table = epca_pci_tbl;
3942         epca_driver.probe = epca_init_one;
3943
3944         pci_count = pci_register_driver (&epca_driver);
3945         
3946         if (pci_count <= 0) {
3947                 pci_unregister_driver (&epca_driver);
3948                 pci_count = 0;
3949         }
3950
3951         return(pci_count);
3952
3953 } /* End init_PCI */
3954
3955 #endif /* ENABLE_PCI */
3956
3957 MODULE_LICENSE("GPL");