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