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