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