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