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