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