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