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