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