epca.c 98.9 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8
/*

 
	Copyright (C) 1996  Digi International.
 
	For technical support please email digiLinux@dgii.com or
	call Digi tech support at (612) 912-3456

9 10
	** This driver is no longer supported by Digi **

L
Linus Torvalds 已提交
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
	Much of this design and code came from epca.c which was 
	copyright (C) 1994, 1995 Troy De Jongh, and subsquently 
	modified by David Nugent, Christoph Lameter, Mike McLagan. 
 
 	This program is free software; you can redistribute it and/or modify
 	it under the terms of the GNU General Public License as published by
 	the Free Software Foundation; either version 2 of the License, or
 	(at your option) any later version.

 	This program is distributed in the hope that it will be useful,
 	but WITHOUT ANY WARRANTY; without even the implied warranty of
 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 	GNU General Public License for more details.

 	You should have received a copy of the GNU General Public License
 	along with this program; if not, write to the Free Software
 	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

--------------------------------------------------------------------------- */
/* See README.epca for change history --DAT*/


#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/init.h>
#include <linux/serial.h>
#include <linux/delay.h>
#include <linux/ctype.h>
#include <linux/tty.h>
#include <linux/tty_flip.h>
#include <linux/slab.h>
#include <linux/ioport.h>
#include <linux/interrupt.h>
#include <asm/uaccess.h>
#include <asm/io.h>
47
#include <linux/spinlock.h>
L
Linus Torvalds 已提交
48 49
#include <linux/pci.h>
#include "digiPCI.h"
50

L
Linus Torvalds 已提交
51 52 53 54 55 56 57 58

#include "digi1.h"
#include "digiFep1.h"
#include "epca.h"
#include "epcaconfig.h"

/* ---------------------- Begin defines ------------------------ */

59
#define VERSION            "1.3.0.1-LK2.6"
L
Linus Torvalds 已提交
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78

/* This major needs to be submitted to Linux to join the majors list */

#define DIGIINFOMAJOR       35  /* For Digi specific ioctl */ 


#define MAXCARDS 7
#define epcaassert(x, msg)  if (!(x)) epca_error(__LINE__, msg)

#define PFX "epca: "

/* ----------------- Begin global definitions ------------------- */

static int nbdevs, num_cards, liloconfig;
static int digi_poller_inhibited = 1 ;

static int setup_error_code;
static int invalid_lilo_config;

79 80 81
/* The ISA boards do window flipping into the same spaces so its only sane
   with a single lock. It's still pretty efficient */

I
Ingo Molnar 已提交
82
static DEFINE_SPINLOCK(epca_lock);
83

L
Linus Torvalds 已提交
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
/* -----------------------------------------------------------------------
	MAXBOARDS is typically 12, but ISA and EISA cards are restricted to 
	7 below.
--------------------------------------------------------------------------*/
static struct board_info boards[MAXBOARDS];


/* ------------- Begin structures used for driver registeration ---------- */

static struct tty_driver *pc_driver;
static struct tty_driver *pc_info;

/* ------------------ Begin Digi specific structures -------------------- */

/* ------------------------------------------------------------------------
	digi_channels represents an array of structures that keep track of
	each channel of the Digi product.  Information such as transmit and
	receive pointers, termio data, and signal definitions (DTR, CTS, etc ...)
	are stored here.  This structure is NOT used to overlay the cards 
	physical channel structure.
-------------------------------------------------------------------------- */
  
static struct channel digi_channels[MAX_ALLOC];

/* ------------------------------------------------------------------------
	card_ptr is an array used to hold the address of the
	first channel structure of each card.  This array will hold
	the addresses of various channels located in digi_channels.
-------------------------------------------------------------------------- */
static struct channel *card_ptr[MAXCARDS];

static struct timer_list epca_timer;

/* ---------------------- Begin function prototypes --------------------- */

/* ----------------------------------------------------------------------
	Begin generic memory functions.  These functions will be alias
	(point at) more specific functions dependent on the board being
	configured.
----------------------------------------------------------------------- */
	
125 126 127 128 129 130 131 132
static void memwinon(struct board_info *b, unsigned int win);
static void memwinoff(struct board_info *b, unsigned int win);
static void globalwinon(struct channel *ch);
static void rxwinon(struct channel *ch);
static void txwinon(struct channel *ch);
static void memoff(struct channel *ch);
static void assertgwinon(struct channel *ch);
static void assertmemoff(struct channel *ch);
L
Linus Torvalds 已提交
133 134 135

/* ---- Begin more 'specific' memory functions for cx_like products --- */

136 137 138 139 140 141
static void pcxem_memwinon(struct board_info *b, unsigned int win);
static void pcxem_memwinoff(struct board_info *b, unsigned int win);
static void pcxem_globalwinon(struct channel *ch);
static void pcxem_rxwinon(struct channel *ch);
static void pcxem_txwinon(struct channel *ch);
static void pcxem_memoff(struct channel *ch);
L
Linus Torvalds 已提交
142 143 144

/* ------ Begin more 'specific' memory functions for the pcxe ------- */

145 146 147 148 149 150
static void pcxe_memwinon(struct board_info *b, unsigned int win);
static void pcxe_memwinoff(struct board_info *b, unsigned int win);
static void pcxe_globalwinon(struct channel *ch);
static void pcxe_rxwinon(struct channel *ch);
static void pcxe_txwinon(struct channel *ch);
static void pcxe_memoff(struct channel *ch);
L
Linus Torvalds 已提交
151 152 153 154

/* ---- Begin more 'specific' memory functions for the pc64xe and pcxi ---- */
/* Note : pc64xe and pcxi share the same windowing routines */

155 156 157 158 159 160
static void pcxi_memwinon(struct board_info *b, unsigned int win);
static void pcxi_memwinoff(struct board_info *b, unsigned int win);
static void pcxi_globalwinon(struct channel *ch);
static void pcxi_rxwinon(struct channel *ch);
static void pcxi_txwinon(struct channel *ch);
static void pcxi_memoff(struct channel *ch);
L
Linus Torvalds 已提交
161 162 163

/* - Begin 'specific' do nothing memory functions needed for some cards - */

164 165 166 167 168 169 170 171
static void dummy_memwinon(struct board_info *b, unsigned int win);
static void dummy_memwinoff(struct board_info *b, unsigned int win);
static void dummy_globalwinon(struct channel *ch);
static void dummy_rxwinon(struct channel *ch);
static void dummy_txwinon(struct channel *ch);
static void dummy_memoff(struct channel *ch);
static void dummy_assertgwinon(struct channel *ch);
static void dummy_assertmemoff(struct channel *ch);
L
Linus Torvalds 已提交
172 173 174

/* ------------------- Begin declare functions ----------------------- */

175 176
static struct channel *verifyChannel(struct tty_struct *);
static void pc_sched_event(struct channel *, int);
L
Linus Torvalds 已提交
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
static void epca_error(int, char *);
static void pc_close(struct tty_struct *, struct file *);
static void shutdown(struct channel *);
static void pc_hangup(struct tty_struct *);
static void pc_put_char(struct tty_struct *, unsigned char);
static int pc_write_room(struct tty_struct *);
static int pc_chars_in_buffer(struct tty_struct *);
static void pc_flush_buffer(struct tty_struct *);
static void pc_flush_chars(struct tty_struct *);
static int block_til_ready(struct tty_struct *, struct file *,
                           struct channel *);
static int pc_open(struct tty_struct *, struct file *);
static void post_fep_init(unsigned int crd);
static void epcapoll(unsigned long);
static void doevent(int);
static void fepcmd(struct channel *, int, int, int, int, int);
static unsigned termios2digi_h(struct channel *ch, unsigned);
static unsigned termios2digi_i(struct channel *ch, unsigned);
static unsigned termios2digi_c(struct channel *ch, unsigned);
static void epcaparam(struct tty_struct *, struct channel *);
static void receive_data(struct channel *);
static int pc_ioctl(struct tty_struct *, struct file *,
                    unsigned int, unsigned long);
static int info_ioctl(struct tty_struct *, struct file *,
                    unsigned int, unsigned long);
static void pc_set_termios(struct tty_struct *, struct termios *);
D
David Howells 已提交
203
static void do_softint(struct work_struct *work);
L
Linus Torvalds 已提交
204 205 206 207 208 209 210 211 212 213
static void pc_stop(struct tty_struct *);
static void pc_start(struct tty_struct *);
static void pc_throttle(struct tty_struct * tty);
static void pc_unthrottle(struct tty_struct *tty);
static void digi_send_break(struct channel *ch, int msec);
static void setup_empty_event(struct tty_struct *tty, struct channel *ch);
void epca_setup(char *, int *);

static int get_termio(struct tty_struct *, struct termio __user *);
static int pc_write(struct tty_struct *, const unsigned char *, int);
214
static int pc_init(void);
L
Linus Torvalds 已提交
215 216 217 218 219 220 221 222 223 224 225 226 227 228
static int init_PCI(void);


/* ------------------------------------------------------------------
	Table of functions for each board to handle memory.  Mantaining 
	parallelism is a *very* good idea here.  The idea is for the 
	runtime code to blindly call these functions, not knowing/caring    
	about the underlying hardware.  This stuff should contain no
	conditionals; if more functionality is needed a different entry
	should be established.  These calls are the interface calls and 
	are the only functions that should be accessed.  Anyone caught
	making direct calls deserves what they get.
-------------------------------------------------------------------- */

229
static void memwinon(struct board_info *b, unsigned int win)
L
Linus Torvalds 已提交
230 231 232 233
{
	(b->memwinon)(b, win);
}

234
static void memwinoff(struct board_info *b, unsigned int win)
L
Linus Torvalds 已提交
235 236 237 238
{
	(b->memwinoff)(b, win);
}

239
static void globalwinon(struct channel *ch)
L
Linus Torvalds 已提交
240 241 242 243
{
	(ch->board->globalwinon)(ch);
}

244
static void rxwinon(struct channel *ch)
L
Linus Torvalds 已提交
245 246 247 248
{
	(ch->board->rxwinon)(ch);
}

249
static void txwinon(struct channel *ch)
L
Linus Torvalds 已提交
250 251 252 253
{
	(ch->board->txwinon)(ch);
}

254
static void memoff(struct channel *ch)
L
Linus Torvalds 已提交
255 256 257
{
	(ch->board->memoff)(ch);
}
258
static void assertgwinon(struct channel *ch)
L
Linus Torvalds 已提交
259 260 261 262
{
	(ch->board->assertgwinon)(ch);
}

263
static void assertmemoff(struct channel *ch)
L
Linus Torvalds 已提交
264 265 266 267 268 269 270 271 272
{
	(ch->board->assertmemoff)(ch);
}

/* ---------------------------------------------------------
	PCXEM windowing is the same as that used in the PCXR 
	and CX series cards.
------------------------------------------------------------ */

273
static void pcxem_memwinon(struct board_info *b, unsigned int win)
L
Linus Torvalds 已提交
274
{
275
        outb_p(FEPWIN|win, b->port + 1);
L
Linus Torvalds 已提交
276 277
}

278
static void pcxem_memwinoff(struct board_info *b, unsigned int win)
L
Linus Torvalds 已提交
279
{
280
	outb_p(0, b->port + 1);
L
Linus Torvalds 已提交
281 282
}

283
static void pcxem_globalwinon(struct channel *ch)
L
Linus Torvalds 已提交
284 285 286 287
{
	outb_p( FEPWIN, (int)ch->board->port + 1);
}

288
static void pcxem_rxwinon(struct channel *ch)
L
Linus Torvalds 已提交
289 290 291 292
{
	outb_p(ch->rxwin, (int)ch->board->port + 1);
}

293
static void pcxem_txwinon(struct channel *ch)
L
Linus Torvalds 已提交
294 295 296 297
{
	outb_p(ch->txwin, (int)ch->board->port + 1);
}

298
static void pcxem_memoff(struct channel *ch)
L
Linus Torvalds 已提交
299 300 301 302 303 304
{
	outb_p(0, (int)ch->board->port + 1);
}

/* ----------------- Begin pcxe memory window stuff ------------------ */

305
static void pcxe_memwinon(struct board_info *b, unsigned int win)
L
Linus Torvalds 已提交
306
{
307
               outb_p(FEPWIN | win, b->port + 1);
L
Linus Torvalds 已提交
308 309
}

310
static void pcxe_memwinoff(struct board_info *b, unsigned int win)
L
Linus Torvalds 已提交
311
{
312 313 314
	outb_p(inb(b->port) & ~FEPMEM,
	           b->port + 1);
	outb_p(0, b->port + 1);
L
Linus Torvalds 已提交
315 316
}

317
static void pcxe_globalwinon(struct channel *ch)
L
Linus Torvalds 已提交
318 319 320 321
{
	outb_p( FEPWIN, (int)ch->board->port + 1);
}

322
static void pcxe_rxwinon(struct channel *ch)
L
Linus Torvalds 已提交
323 324 325 326
{
		outb_p(ch->rxwin, (int)ch->board->port + 1);
}

327
static void pcxe_txwinon(struct channel *ch)
L
Linus Torvalds 已提交
328 329 330 331
{
		outb_p(ch->txwin, (int)ch->board->port + 1);
}

332
static void pcxe_memoff(struct channel *ch)
L
Linus Torvalds 已提交
333 334 335 336 337 338 339
{
	outb_p(0, (int)ch->board->port);
	outb_p(0, (int)ch->board->port + 1);
}

/* ------------- Begin pc64xe and pcxi memory window stuff -------------- */

340
static void pcxi_memwinon(struct board_info *b, unsigned int win)
L
Linus Torvalds 已提交
341
{
342
               outb_p(inb(b->port) | FEPMEM, b->port);
L
Linus Torvalds 已提交
343 344
}

345
static void pcxi_memwinoff(struct board_info *b, unsigned int win)
L
Linus Torvalds 已提交
346
{
347
	outb_p(inb(b->port) & ~FEPMEM, b->port);
L
Linus Torvalds 已提交
348 349
}

350
static void pcxi_globalwinon(struct channel *ch)
L
Linus Torvalds 已提交
351
{
352
	outb_p(FEPMEM, ch->board->port);
L
Linus Torvalds 已提交
353 354
}

355
static void pcxi_rxwinon(struct channel *ch)
L
Linus Torvalds 已提交
356
{
357
		outb_p(FEPMEM, ch->board->port);
L
Linus Torvalds 已提交
358 359
}

360
static void pcxi_txwinon(struct channel *ch)
L
Linus Torvalds 已提交
361
{
362
		outb_p(FEPMEM, ch->board->port);
L
Linus Torvalds 已提交
363 364
}

365
static void pcxi_memoff(struct channel *ch)
L
Linus Torvalds 已提交
366
{
367
	outb_p(0, ch->board->port);
L
Linus Torvalds 已提交
368 369
}

370
static void pcxi_assertgwinon(struct channel *ch)
L
Linus Torvalds 已提交
371
{
372
	epcaassert(inb(ch->board->port) & FEPMEM, "Global memory off");
L
Linus Torvalds 已提交
373 374
}

375
static void pcxi_assertmemoff(struct channel *ch)
L
Linus Torvalds 已提交
376
{
377
	epcaassert(!(inb(ch->board->port) & FEPMEM), "Memory on");
L
Linus Torvalds 已提交
378 379 380 381 382 383 384 385 386 387 388 389
}


/* ----------------------------------------------------------------------
	Not all of the cards need specific memory windowing routines.  Some
	cards (Such as PCI) needs no windowing routines at all.  We provide
	these do nothing routines so that the same code base can be used.
	The driver will ALWAYS call a windowing routine if it thinks it needs
	to; regardless of the card.  However, dependent on the card the routine
	may or may not do anything.
---------------------------------------------------------------------------*/

390
static void dummy_memwinon(struct board_info *b, unsigned int win)
L
Linus Torvalds 已提交
391 392 393
{
}

394
static void dummy_memwinoff(struct board_info *b, unsigned int win)
L
Linus Torvalds 已提交
395 396 397
{
}

398
static void dummy_globalwinon(struct channel *ch)
L
Linus Torvalds 已提交
399 400 401
{
}

402
static void dummy_rxwinon(struct channel *ch)
L
Linus Torvalds 已提交
403 404 405
{
}

406
static void dummy_txwinon(struct channel *ch)
L
Linus Torvalds 已提交
407 408 409
{
}

410
static void dummy_memoff(struct channel *ch)
L
Linus Torvalds 已提交
411 412 413
{
}

414
static void dummy_assertgwinon(struct channel *ch)
L
Linus Torvalds 已提交
415 416 417
{
}

418
static void dummy_assertmemoff(struct channel *ch)
L
Linus Torvalds 已提交
419 420 421 422
{
}

/* ----------------- Begin verifyChannel function ----------------------- */
423
static struct channel *verifyChannel(struct tty_struct *tty)
L
Linus Torvalds 已提交
424 425 426 427 428 429
{ /* Begin verifyChannel */
	/* --------------------------------------------------------------------
		This routine basically provides a sanity check.  It insures that
		the channel returned is within the proper range of addresses as
		well as properly initialized.  If some bogus info gets passed in
		through tty->driver_data this should catch it.
430 431 432 433
		--------------------------------------------------------------------- */
	if (tty) {
		struct channel *ch = (struct channel *)tty->driver_data;
		if ((ch >= &digi_channels[0]) && (ch < &digi_channels[nbdevs])) {
L
Linus Torvalds 已提交
434 435 436
			if (ch->magic == EPCA_MAGIC)
				return ch;
		}
437
	}
L
Linus Torvalds 已提交
438 439 440 441 442 443
	return NULL;

} /* End verifyChannel */

/* ------------------ Begin pc_sched_event ------------------------- */

444 445
static void pc_sched_event(struct channel *ch, int event)
{
L
Linus Torvalds 已提交
446 447 448 449 450 451 452 453 454 455 456
	/* ----------------------------------------------------------------------
		We call this to schedule interrupt processing on some event.  The 
		kernel sees our request and calls the related routine in OUR driver.
	-------------------------------------------------------------------------*/
	ch->event |= 1 << event;
	schedule_work(&ch->tqueue);
} /* End pc_sched_event */

/* ------------------ Begin epca_error ------------------------- */

static void epca_error(int line, char *msg)
457
{
L
Linus Torvalds 已提交
458
	printk(KERN_ERR "epca_error (Digi): line = %d %s\n",line,msg);
459
}
L
Linus Torvalds 已提交
460 461 462

/* ------------------ Begin pc_close ------------------------- */
static void pc_close(struct tty_struct * tty, struct file * filp)
463
{
L
Linus Torvalds 已提交
464 465 466 467 468 469
	struct channel *ch;
	unsigned long flags;
	/* ---------------------------------------------------------
		verifyChannel returns the channel from the tty struct
		if it is valid.  This serves as a sanity check.
	------------------------------------------------------------- */
470 471 472 473
	if ((ch = verifyChannel(tty)) != NULL)  { /* Begin if ch != NULL */
		spin_lock_irqsave(&epca_lock, flags);
		if (tty_hung_up_p(filp)) {
			spin_unlock_irqrestore(&epca_lock, flags);
L
Linus Torvalds 已提交
474 475 476
			return;
		}
		/* Check to see if the channel is open more than once */
477 478
		if (ch->count-- > 1)  {
			/* Begin channel is open more than once */
L
Linus Torvalds 已提交
479 480 481 482
			/* -------------------------------------------------------------
				Return without doing anything.  Someone might still be using
				the channel.
			---------------------------------------------------------------- */
483
			spin_unlock_irqrestore(&epca_lock, flags);
L
Linus Torvalds 已提交
484 485 486 487
			return;
		} /* End channel is open more than once */

		/* Port open only once go ahead with shutdown & reset */
488
		BUG_ON(ch->count < 0);
L
Linus Torvalds 已提交
489 490 491 492 493 494 495 496 497

		/* ---------------------------------------------------------------
			Let the rest of the driver know the channel is being closed.
			This becomes important if an open is attempted before close 
			is finished.
		------------------------------------------------------------------ */
		ch->asyncflags |= ASYNC_CLOSING;
		tty->closing = 1;

498 499 500
		spin_unlock_irqrestore(&epca_lock, flags);

		if (ch->asyncflags & ASYNC_INITIALIZED)  {
L
Linus Torvalds 已提交
501 502 503 504 505 506 507 508 509
			/* Setup an event to indicate when the transmit buffer empties */
			setup_empty_event(tty, ch);		
			tty_wait_until_sent(tty, 3000); /* 30 seconds timeout */
		}
		if (tty->driver->flush_buffer)
			tty->driver->flush_buffer(tty);

		tty_ldisc_flush(tty);
		shutdown(ch);
510 511

		spin_lock_irqsave(&epca_lock, flags);
L
Linus Torvalds 已提交
512 513 514
		tty->closing = 0;
		ch->event = 0;
		ch->tty = NULL;
515
		spin_unlock_irqrestore(&epca_lock, flags);
L
Linus Torvalds 已提交
516

517
		if (ch->blocked_open)  { /* Begin if blocked_open */
L
Linus Torvalds 已提交
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
			if (ch->close_delay) 
				msleep_interruptible(jiffies_to_msecs(ch->close_delay));
			wake_up_interruptible(&ch->open_wait);
		} /* End if blocked_open */
		ch->asyncflags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_INITIALIZED | 
		                      ASYNC_CLOSING);
		wake_up_interruptible(&ch->close_wait);
	} /* End if ch != NULL */
} /* End pc_close */ 

/* ------------------ Begin shutdown  ------------------------- */

static void shutdown(struct channel *ch)
{ /* Begin shutdown */

	unsigned long flags;
	struct tty_struct *tty;
535
	struct board_chan __iomem *bc;
L
Linus Torvalds 已提交
536 537 538 539

	if (!(ch->asyncflags & ASYNC_INITIALIZED)) 
		return;

540
	spin_lock_irqsave(&epca_lock, flags);
L
Linus Torvalds 已提交
541

542
	globalwinon(ch);
L
Linus Torvalds 已提交
543 544 545 546 547 548 549 550 551
	bc = ch->brdchan;

	/* ------------------------------------------------------------------
		In order for an event to be generated on the receipt of data the
		idata flag must be set. Since we are shutting down, this is not 
		necessary clear this flag.
	--------------------------------------------------------------------- */ 

	if (bc)
552
		writeb(0, &bc->idata);
L
Linus Torvalds 已提交
553 554 555 556 557 558
	tty = ch->tty;

	/* ----------------------------------------------------------------
	   If we're a modem control device and HUPCL is on, drop RTS & DTR.
 	------------------------------------------------------------------ */

559
	if (tty->termios->c_cflag & HUPCL)  {
L
Linus Torvalds 已提交
560 561 562 563 564 565 566 567 568 569 570 571 572
		ch->omodem &= ~(ch->m_rts | ch->m_dtr);
		fepcmd(ch, SETMODEM, 0, ch->m_dtr | ch->m_rts, 10, 1);
	}
	memoff(ch);

	/* ------------------------------------------------------------------
		The channel has officialy been closed.  The next time it is opened
		it will have to reinitialized.  Set a flag to indicate this.
	---------------------------------------------------------------------- */

	/* Prevent future Digi programmed interrupts from coming active */

	ch->asyncflags &= ~ASYNC_INITIALIZED;
573
	spin_unlock_irqrestore(&epca_lock, flags);
L
Linus Torvalds 已提交
574 575 576 577 578 579 580 581 582 583 584 585 586 587

} /* End shutdown */

/* ------------------ Begin pc_hangup  ------------------------- */

static void pc_hangup(struct tty_struct *tty)
{ /* Begin pc_hangup */
	struct channel *ch;
	
	/* ---------------------------------------------------------
		verifyChannel returns the channel from the tty struct
		if it is valid.  This serves as a sanity check.
	------------------------------------------------------------- */

588
	if ((ch = verifyChannel(tty)) != NULL)  { /* Begin if ch != NULL */
L
Linus Torvalds 已提交
589 590 591 592 593 594 595
		unsigned long flags;

		if (tty->driver->flush_buffer)
			tty->driver->flush_buffer(tty);
		tty_ldisc_flush(tty);
		shutdown(ch);

596
		spin_lock_irqsave(&epca_lock, flags);
L
Linus Torvalds 已提交
597 598 599 600
		ch->tty   = NULL;
		ch->event = 0;
		ch->count = 0;
		ch->asyncflags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_INITIALIZED);
601
		spin_unlock_irqrestore(&epca_lock, flags);
L
Linus Torvalds 已提交
602 603 604 605 606 607 608 609 610 611
		wake_up_interruptible(&ch->open_wait);
	} /* End if ch != NULL */

} /* End pc_hangup */

/* ------------------ Begin pc_write  ------------------------- */

static int pc_write(struct tty_struct * tty,
                    const unsigned char *buf, int bytesAvailable)
{ /* Begin pc_write */
612 613 614 615
	unsigned int head, tail;
	int dataLen;
	int size;
	int amountCopied;
L
Linus Torvalds 已提交
616 617 618
	struct channel *ch;
	unsigned long flags;
	int remain;
619
	struct board_chan __iomem *bc;
L
Linus Torvalds 已提交
620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643

	/* ----------------------------------------------------------------
		pc_write is primarily called directly by the kernel routine
		tty_write (Though it can also be called by put_char) found in
		tty_io.c.  pc_write is passed a line discipline buffer where 
		the data to be written out is stored.  The line discipline 
		implementation itself is done at the kernel level and is not 
		brought into the driver.  
	------------------------------------------------------------------- */

	/* ---------------------------------------------------------
		verifyChannel returns the channel from the tty struct
		if it is valid.  This serves as a sanity check.
	------------------------------------------------------------- */

	if ((ch = verifyChannel(tty)) == NULL)
		return 0;

	/* Make a pointer to the channel data structure found on the board. */

	bc   = ch->brdchan;
	size = ch->txbufsize;
	amountCopied = 0;

644
	spin_lock_irqsave(&epca_lock, flags);
L
Linus Torvalds 已提交
645 646
	globalwinon(ch);

647 648
	head = readw(&bc->tin) & (size - 1);
	tail = readw(&bc->tout);
L
Linus Torvalds 已提交
649

650 651
	if (tail != readw(&bc->tout))
		tail = readw(&bc->tout);
L
Linus Torvalds 已提交
652 653 654
	tail &= (size - 1);

	/*	If head >= tail, head has not wrapped around. */ 
655
	if (head >= tail)  { /* Begin head has not wrapped */
L
Linus Torvalds 已提交
656 657 658 659 660 661 662 663 664
		/* ---------------------------------------------------------------
			remain (much like dataLen above) represents the total amount of
			space available on the card for data.  Here dataLen represents
			the space existing between the head pointer and the end of 
			buffer.  This is important because a memcpy cannot be told to
			automatically wrap around when it hits the buffer end.
		------------------------------------------------------------------ */ 
		dataLen = size - head;
		remain = size - (head - tail) - 1;
665
	} else { /* Begin head has wrapped around */
L
Linus Torvalds 已提交
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685

		remain = tail - head - 1;
		dataLen = remain;

	} /* End head has wrapped around */
	/* -------------------------------------------------------------------
			Check the space on the card.  If we have more data than 
			space; reduce the amount of data to fit the space.
	---------------------------------------------------------------------- */
	bytesAvailable = min(remain, bytesAvailable);
	txwinon(ch);
	while (bytesAvailable > 0) 
	{ /* Begin while there is data to copy onto card */

		/* -----------------------------------------------------------------
			If head is not wrapped, the below will make sure the first 
			data copy fills to the end of card buffer.
		------------------------------------------------------------------- */

		dataLen = min(bytesAvailable, dataLen);
686
		memcpy_toio(ch->txptr + head, buf, dataLen);
L
Linus Torvalds 已提交
687 688 689 690 691
		buf += dataLen;
		head += dataLen;
		amountCopied += dataLen;
		bytesAvailable -= dataLen;

692
		if (head >= size) {
L
Linus Torvalds 已提交
693 694 695 696 697 698
			head = 0;
			dataLen = tail;
		}
	} /* End while there is data to copy onto card */
	ch->statusflags |= TXBUSY;
	globalwinon(ch);
699
	writew(head, &bc->tin);
L
Linus Torvalds 已提交
700

701
	if ((ch->statusflags & LOWWAIT) == 0)  {
L
Linus Torvalds 已提交
702
		ch->statusflags |= LOWWAIT;
703
		writeb(1, &bc->ilow);
L
Linus Torvalds 已提交
704 705
	}
	memoff(ch);
706
	spin_unlock_irqrestore(&epca_lock, flags);
L
Linus Torvalds 已提交
707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726
	return(amountCopied);

} /* End pc_write */

/* ------------------ Begin pc_put_char  ------------------------- */

static void pc_put_char(struct tty_struct *tty, unsigned char c)
{ /* Begin pc_put_char */
	pc_write(tty, &c, 1);
} /* End pc_put_char */

/* ------------------ Begin pc_write_room  ------------------------- */

static int pc_write_room(struct tty_struct *tty)
{ /* Begin pc_write_room */

	int remain;
	struct channel *ch;
	unsigned long flags;
	unsigned int head, tail;
727
	struct board_chan __iomem *bc;
L
Linus Torvalds 已提交
728 729 730 731 732 733 734 735

	remain = 0;

	/* ---------------------------------------------------------
		verifyChannel returns the channel from the tty struct
		if it is valid.  This serves as a sanity check.
	------------------------------------------------------------- */

736 737
	if ((ch = verifyChannel(tty)) != NULL)  {
		spin_lock_irqsave(&epca_lock, flags);
L
Linus Torvalds 已提交
738 739 740
		globalwinon(ch);

		bc   = ch->brdchan;
741 742
		head = readw(&bc->tin) & (ch->txbufsize - 1);
		tail = readw(&bc->tout);
L
Linus Torvalds 已提交
743

744 745
		if (tail != readw(&bc->tout))
			tail = readw(&bc->tout);
L
Linus Torvalds 已提交
746 747 748 749 750 751
		/* Wrap tail if necessary */
		tail &= (ch->txbufsize - 1);

		if ((remain = tail - head - 1) < 0 )
			remain += ch->txbufsize;

752
		if (remain && (ch->statusflags & LOWWAIT) == 0) {
L
Linus Torvalds 已提交
753
			ch->statusflags |= LOWWAIT;
754
			writeb(1, &bc->ilow);
L
Linus Torvalds 已提交
755 756
		}
		memoff(ch);
757
		spin_unlock_irqrestore(&epca_lock, flags);
L
Linus Torvalds 已提交
758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773
	}
	/* Return how much room is left on card */
	return remain;

} /* End pc_write_room */

/* ------------------ Begin pc_chars_in_buffer  ---------------------- */

static int pc_chars_in_buffer(struct tty_struct *tty)
{ /* Begin pc_chars_in_buffer */

	int chars;
	unsigned int ctail, head, tail;
	int remain;
	unsigned long flags;
	struct channel *ch;
774
	struct board_chan __iomem *bc;
L
Linus Torvalds 已提交
775 776 777 778 779 780 781 782 783

	/* ---------------------------------------------------------
		verifyChannel returns the channel from the tty struct
		if it is valid.  This serves as a sanity check.
	------------------------------------------------------------- */

	if ((ch = verifyChannel(tty)) == NULL)
		return(0);

784
	spin_lock_irqsave(&epca_lock, flags);
L
Linus Torvalds 已提交
785 786 787
	globalwinon(ch);

	bc = ch->brdchan;
788 789 790
	tail = readw(&bc->tout);
	head = readw(&bc->tin);
	ctail = readw(&ch->mailbox->cout);
L
Linus Torvalds 已提交
791

792
	if (tail == head && readw(&ch->mailbox->cin) == ctail && readb(&bc->tbusy) == 0)
L
Linus Torvalds 已提交
793
		chars = 0;
794 795
	else  { /* Begin if some space on the card has been used */
		head = readw(&bc->tin) & (ch->txbufsize - 1);
L
Linus Torvalds 已提交
796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816
		tail &= (ch->txbufsize - 1);
		/*  --------------------------------------------------------------
			The logic here is basically opposite of the above pc_write_room
			here we are finding the amount of bytes in the buffer filled.
			Not the amount of bytes empty.
		------------------------------------------------------------------- */
		if ((remain = tail - head - 1) < 0 )
			remain += ch->txbufsize;
		chars = (int)(ch->txbufsize - remain);
		/* -------------------------------------------------------------  
			Make it possible to wakeup anything waiting for output
			in tty_ioctl.c, etc.

			If not already set.  Setup an event to indicate when the
			transmit buffer empties 
		----------------------------------------------------------------- */
		if (!(ch->statusflags & EMPTYWAIT))
			setup_empty_event(tty,ch);

	} /* End if some space on the card has been used */
	memoff(ch);
817
	spin_unlock_irqrestore(&epca_lock, flags);
L
Linus Torvalds 已提交
818 819 820 821 822 823 824 825 826 827 828 829 830
	/* Return number of characters residing on card. */
	return(chars);

} /* End pc_chars_in_buffer */

/* ------------------ Begin pc_flush_buffer  ---------------------- */

static void pc_flush_buffer(struct tty_struct *tty)
{ /* Begin pc_flush_buffer */

	unsigned int tail;
	unsigned long flags;
	struct channel *ch;
831
	struct board_chan __iomem *bc;
L
Linus Torvalds 已提交
832 833 834 835 836 837 838
	/* ---------------------------------------------------------
		verifyChannel returns the channel from the tty struct
		if it is valid.  This serves as a sanity check.
	------------------------------------------------------------- */
	if ((ch = verifyChannel(tty)) == NULL)
		return;

839
	spin_lock_irqsave(&epca_lock, flags);
L
Linus Torvalds 已提交
840 841
	globalwinon(ch);
	bc   = ch->brdchan;
842
	tail = readw(&bc->tout);
L
Linus Torvalds 已提交
843 844 845
	/* Have FEP move tout pointer; effectively flushing transmit buffer */
	fepcmd(ch, STOUT, (unsigned) tail, 0, 0, 0);
	memoff(ch);
846
	spin_unlock_irqrestore(&epca_lock, flags);
L
Linus Torvalds 已提交
847 848 849 850 851 852 853 854 855 856 857 858 859
	wake_up_interruptible(&tty->write_wait);
	tty_wakeup(tty);
} /* End pc_flush_buffer */

/* ------------------ Begin pc_flush_chars  ---------------------- */

static void pc_flush_chars(struct tty_struct *tty)
{ /* Begin pc_flush_chars */
	struct channel * ch;
	/* ---------------------------------------------------------
		verifyChannel returns the channel from the tty struct
		if it is valid.  This serves as a sanity check.
	------------------------------------------------------------- */
860
	if ((ch = verifyChannel(tty)) != NULL) {
L
Linus Torvalds 已提交
861
		unsigned long flags;
862
		spin_lock_irqsave(&epca_lock, flags);
L
Linus Torvalds 已提交
863 864 865 866 867 868
		/* ----------------------------------------------------------------
			If not already set and the transmitter is busy setup an event
			to indicate when the transmit empties.
		------------------------------------------------------------------- */
		if ((ch->statusflags & TXBUSY) && !(ch->statusflags & EMPTYWAIT))
			setup_empty_event(tty,ch);
869
		spin_unlock_irqrestore(&epca_lock, flags);
L
Linus Torvalds 已提交
870 871 872 873 874 875 876 877 878 879 880 881
	}
} /* End pc_flush_chars */

/* ------------------ Begin block_til_ready  ---------------------- */

static int block_til_ready(struct tty_struct *tty, 
                           struct file *filp, struct channel *ch)
{ /* Begin block_til_ready */
	DECLARE_WAITQUEUE(wait,current);
	int	retval, do_clocal = 0;
	unsigned long flags;

882
	if (tty_hung_up_p(filp)) {
L
Linus Torvalds 已提交
883 884 885 886 887 888 889 890 891 892 893
		if (ch->asyncflags & ASYNC_HUP_NOTIFY)
			retval = -EAGAIN;
		else
			retval = -ERESTARTSYS;	
		return(retval);
	}

	/* ----------------------------------------------------------------- 
		If the device is in the middle of being closed, then block
		until it's done, and then try again.
	-------------------------------------------------------------------- */
894
	if (ch->asyncflags & ASYNC_CLOSING) {
L
Linus Torvalds 已提交
895 896 897 898 899 900 901 902
		interruptible_sleep_on(&ch->close_wait);

		if (ch->asyncflags & ASYNC_HUP_NOTIFY)
			return -EAGAIN;
		else
			return -ERESTARTSYS;
	}

903
	if (filp->f_flags & O_NONBLOCK)  {
L
Linus Torvalds 已提交
904 905 906 907 908 909 910 911 912
		/* ----------------------------------------------------------------- 
	  	 If non-blocking mode is set, then make the check up front
	  	 and then exit.
		-------------------------------------------------------------------- */
		ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
		return 0;
	}
	if (tty->termios->c_cflag & CLOCAL)
		do_clocal = 1;
913
	/* Block waiting for the carrier detect and the line to become free */
L
Linus Torvalds 已提交
914 915 916 917
	
	retval = 0;
	add_wait_queue(&ch->open_wait, &wait);

918
	spin_lock_irqsave(&epca_lock, flags);
L
Linus Torvalds 已提交
919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937
	/* We dec count so that pc_close will know when to free things */
	if (!tty_hung_up_p(filp))
		ch->count--;
	ch->blocked_open++;
	while(1) 
	{ /* Begin forever while  */
		set_current_state(TASK_INTERRUPTIBLE);
		if (tty_hung_up_p(filp) ||
		    !(ch->asyncflags & ASYNC_INITIALIZED)) 
		{
			if (ch->asyncflags & ASYNC_HUP_NOTIFY)
				retval = -EAGAIN;
			else
				retval = -ERESTARTSYS;	
			break;
		}
		if (!(ch->asyncflags & ASYNC_CLOSING) && 
			  (do_clocal || (ch->imodem & ch->dcd)))
			break;
938
		if (signal_pending(current)) {
L
Linus Torvalds 已提交
939 940 941
			retval = -ERESTARTSYS;
			break;
		}
942
		spin_unlock_irqrestore(&epca_lock, flags);
L
Linus Torvalds 已提交
943 944 945 946 947 948 949
		/* ---------------------------------------------------------------
			Allow someone else to be scheduled.  We will occasionally go
			through this loop until one of the above conditions change.
			The below schedule call will allow other processes to enter and
			prevent this loop from hogging the cpu.
		------------------------------------------------------------------ */
		schedule();
950
		spin_lock_irqsave(&epca_lock, flags);
L
Linus Torvalds 已提交
951 952 953 954 955 956 957 958 959

	} /* End forever while  */

	current->state = TASK_RUNNING;
	remove_wait_queue(&ch->open_wait, &wait);
	if (!tty_hung_up_p(filp))
		ch->count++;
	ch->blocked_open--;

960 961
	spin_unlock_irqrestore(&epca_lock, flags);

L
Linus Torvalds 已提交
962 963 964 965 966 967 968 969 970 971 972 973 974 975 976
	if (retval)
		return retval;

	ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
	return 0;
} /* End block_til_ready */	

/* ------------------ Begin pc_open  ---------------------- */

static int pc_open(struct tty_struct *tty, struct file * filp)
{ /* Begin pc_open */

	struct channel *ch;
	unsigned long flags;
	int line, retval, boardnum;
977
	struct board_chan __iomem *bc;
978
	unsigned int head;
L
Linus Torvalds 已提交
979 980

	line = tty->index;
981 982
	if (line < 0 || line >= nbdevs)
		return -ENODEV;
L
Linus Torvalds 已提交
983 984 985 986 987 988 989 990 991 992 993 994

	ch = &digi_channels[line];
	boardnum = ch->boardnum;

	/* Check status of board configured in system.  */

	/* -----------------------------------------------------------------
		I check to see if the epca_setup routine detected an user error.  
		It might be better to put this in pc_init, but for the moment it
		goes here.
	---------------------------------------------------------------------- */

995
	if (invalid_lilo_config) {
L
Linus Torvalds 已提交
996
		if (setup_error_code & INVALID_BOARD_TYPE)
997
			printk(KERN_ERR "epca: pc_open: Invalid board type specified in kernel options.\n");
L
Linus Torvalds 已提交
998
		if (setup_error_code & INVALID_NUM_PORTS)
999
			printk(KERN_ERR "epca: pc_open: Invalid number of ports specified in kernel options.\n");
L
Linus Torvalds 已提交
1000
		if (setup_error_code & INVALID_MEM_BASE)
1001
			printk(KERN_ERR "epca: pc_open: Invalid board memory address specified in kernel options.\n");
L
Linus Torvalds 已提交
1002
		if (setup_error_code & INVALID_PORT_BASE)
1003
			printk(KERN_ERR "epca; pc_open: Invalid board port address specified in kernel options.\n");
L
Linus Torvalds 已提交
1004
		if (setup_error_code & INVALID_BOARD_STATUS)
1005
			printk(KERN_ERR "epca: pc_open: Invalid board status specified in kernel options.\n");
L
Linus Torvalds 已提交
1006
		if (setup_error_code & INVALID_ALTPIN)
1007
			printk(KERN_ERR "epca: pc_open: Invalid board altpin specified in kernel options;\n");
L
Linus Torvalds 已提交
1008
		tty->driver_data = NULL;   /* Mark this device as 'down' */
1009
		return -ENODEV;
L
Linus Torvalds 已提交
1010
	}
1011
	if (boardnum >= num_cards || boards[boardnum].status == DISABLED)  {
L
Linus Torvalds 已提交
1012 1013 1014 1015
		tty->driver_data = NULL;   /* Mark this device as 'down' */
		return(-ENODEV);
	}
	
1016
	if ((bc = ch->brdchan) == 0) {
L
Linus Torvalds 已提交
1017
		tty->driver_data = NULL;
1018
		return -ENODEV;
L
Linus Torvalds 已提交
1019 1020
	}

1021
	spin_lock_irqsave(&epca_lock, flags);
L
Linus Torvalds 已提交
1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041
	/* ------------------------------------------------------------------
		Every time a channel is opened, increment a counter.  This is 
		necessary because we do not wish to flush and shutdown the channel
		until the last app holding the channel open, closes it.	 	
	--------------------------------------------------------------------- */
	ch->count++;
	/* ----------------------------------------------------------------
		Set a kernel structures pointer to our local channel 
		structure.  This way we can get to it when passed only
		a tty struct.
	------------------------------------------------------------------ */
	tty->driver_data = ch;
	/* ----------------------------------------------------------------
		If this is the first time the channel has been opened, initialize
		the tty->termios struct otherwise let pc_close handle it.
	-------------------------------------------------------------------- */
	globalwinon(ch);
	ch->statusflags = 0;

	/* Save boards current modem status */
1042
	ch->imodem = readb(&bc->mstat);
L
Linus Torvalds 已提交
1043 1044 1045 1046 1047

	/* ----------------------------------------------------------------
	   Set receive head and tail ptrs to each other.  This indicates
	   no data available to read.
	----------------------------------------------------------------- */
1048 1049
	head = readw(&bc->rin);
	writew(head, &bc->rout);
L
Linus Torvalds 已提交
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060

	/* Set the channels associated tty structure */
	ch->tty = tty;

	/* -----------------------------------------------------------------
		The below routine generally sets up parity, baud, flow control 
		issues, etc.... It effect both control flags and input flags.
	-------------------------------------------------------------------- */
	epcaparam(tty,ch);
	ch->asyncflags |= ASYNC_INITIALIZED;
	memoff(ch);
1061
	spin_unlock_irqrestore(&epca_lock, flags);
L
Linus Torvalds 已提交
1062 1063 1064 1065 1066 1067 1068 1069

	retval = block_til_ready(tty, filp, ch);
	if (retval)
		return retval;
	/* -------------------------------------------------------------
		Set this again in case a hangup set it to zero while this 
		open() was waiting for the line...
	--------------------------------------------------------------- */
1070
	spin_lock_irqsave(&epca_lock, flags);
L
Linus Torvalds 已提交
1071 1072 1073
	ch->tty = tty;
	globalwinon(ch);
	/* Enable Digi Data events */
1074
	writeb(1, &bc->idata);
L
Linus Torvalds 已提交
1075
	memoff(ch);
1076
	spin_unlock_irqrestore(&epca_lock, flags);
L
Linus Torvalds 已提交
1077 1078 1079 1080 1081
	return 0;
} /* End pc_open */

static int __init epca_module_init(void)
{ /* Begin init_module */
1082
	return pc_init();
L
Linus Torvalds 已提交
1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099
}

module_init(epca_module_init);

static struct pci_driver epca_driver;

static void __exit epca_module_exit(void)
{
	int               count, crd;
	struct board_info *bd;
	struct channel    *ch;

	del_timer_sync(&epca_timer);

	if ((tty_unregister_driver(pc_driver)) ||  
	    (tty_unregister_driver(pc_info)))
	{
1100
		printk(KERN_WARNING "epca: cleanup_module failed to un-register tty driver\n");
L
Linus Torvalds 已提交
1101 1102 1103 1104 1105
		return;
	}
	put_tty_driver(pc_driver);
	put_tty_driver(pc_info);

1106
	for (crd = 0; crd < num_cards; crd++)  { /* Begin for each card */
L
Linus Torvalds 已提交
1107 1108 1109 1110 1111 1112
		bd = &boards[crd];
		if (!bd)
		{ /* Begin sanity check */
			printk(KERN_ERR "<Error> - Digi : cleanup_module failed\n");
			return;
		} /* End sanity check */
1113
		ch = card_ptr[crd];
L
Linus Torvalds 已提交
1114 1115
		for (count = 0; count < bd->numports; count++, ch++) 
		{ /* Begin for each port */
J
Jiri Slaby 已提交
1116 1117
			if (ch && ch->tty)
				tty_hangup(ch->tty);
L
Linus Torvalds 已提交
1118 1119 1120 1121
		} /* End for each port */
	} /* End for each card */
	pci_unregister_driver (&epca_driver);
}
1122

L
Linus Torvalds 已提交
1123 1124
module_exit(epca_module_exit);

J
Jeff Dike 已提交
1125
static const struct tty_operations pc_ops = {
L
Linus Torvalds 已提交
1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154
	.open = pc_open,
	.close = pc_close,
	.write = pc_write,
	.write_room = pc_write_room,
	.flush_buffer = pc_flush_buffer,
	.chars_in_buffer = pc_chars_in_buffer,
	.flush_chars = pc_flush_chars,
	.put_char = pc_put_char,
	.ioctl = pc_ioctl,
	.set_termios = pc_set_termios,
	.stop = pc_stop,
	.start = pc_start,
	.throttle = pc_throttle,
	.unthrottle = pc_unthrottle,
	.hangup = pc_hangup,
};

static int info_open(struct tty_struct *tty, struct file * filp)
{
	return 0;
}

static struct tty_operations info_ops = {
	.open = info_open,
	.ioctl = info_ioctl,
};

/* ------------------ Begin pc_init  ---------------------- */

1155
static int __init pc_init(void)
L
Linus Torvalds 已提交
1156 1157 1158 1159
{ /* Begin pc_init */
	int crd;
	struct board_info *bd;
	unsigned char board_id = 0;
1160
	int err = -ENOMEM;
L
Linus Torvalds 已提交
1161 1162 1163 1164 1165 1166 1167

	int pci_boards_found, pci_count;

	pci_count = 0;

	pc_driver = alloc_tty_driver(MAX_ALLOC);
	if (!pc_driver)
1168
		goto out1;
L
Linus Torvalds 已提交
1169 1170

	pc_info = alloc_tty_driver(MAX_ALLOC);
1171 1172
	if (!pc_info)
		goto out2;
L
Linus Torvalds 已提交
1173 1174 1175 1176 1177 1178 1179

	/* -----------------------------------------------------------------------
		If epca_setup has not been ran by LILO set num_cards to defaults; copy
		board structure defined by digiConfig into drivers board structure.
		Note : If LILO has ran epca_setup then epca_setup will handle defining
		num_cards as well as copying the data into the board structure.
	-------------------------------------------------------------------------- */
1180
	if (!liloconfig) { /* Begin driver has been configured via. epcaconfig */
L
Linus Torvalds 已提交
1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364

		nbdevs = NBDEVS;
		num_cards = NUMCARDS;
		memcpy((void *)&boards, (void *)&static_boards,
		       (sizeof(struct board_info) * NUMCARDS));
	} /* End driver has been configured via. epcaconfig */

	/* -----------------------------------------------------------------
		Note : If lilo was used to configure the driver and the 
		ignore epcaconfig option was choosen (digiepca=2) then 
		nbdevs and num_cards will equal 0 at this point.  This is
		okay; PCI cards will still be picked up if detected.
	--------------------------------------------------------------------- */

	/*  -----------------------------------------------------------
		Set up interrupt, we will worry about memory allocation in
		post_fep_init. 
	--------------------------------------------------------------- */


	printk(KERN_INFO "DIGI epca driver version %s loaded.\n",VERSION);

	/* ------------------------------------------------------------------
		NOTE : This code assumes that the number of ports found in 
		       the boards array is correct.  This could be wrong if
		       the card in question is PCI (And therefore has no ports 
		       entry in the boards structure.)  The rest of the 
		       information will be valid for PCI because the beginning
		       of pc_init scans for PCI and determines i/o and base
		       memory addresses.  I am not sure if it is possible to 
		       read the number of ports supported by the card prior to
		       it being booted (Since that is the state it is in when 
		       pc_init is run).  Because it is not possible to query the
		       number of supported ports until after the card has booted;
		       we are required to calculate the card_ptrs as the card is	 
		       is initialized (Inside post_fep_init).  The negative thing
		       about this approach is that digiDload's call to GET_INFO
		       will have a bad port value.  (Since this is called prior
		       to post_fep_init.)

	--------------------------------------------------------------------- */
  
	pci_boards_found = 0;
	if(num_cards < MAXBOARDS)
		pci_boards_found += init_PCI();
	num_cards += pci_boards_found;

	pc_driver->owner = THIS_MODULE;
	pc_driver->name = "ttyD"; 
	pc_driver->major = DIGI_MAJOR; 
	pc_driver->minor_start = 0;
	pc_driver->type = TTY_DRIVER_TYPE_SERIAL;
	pc_driver->subtype = SERIAL_TYPE_NORMAL;
	pc_driver->init_termios = tty_std_termios;
	pc_driver->init_termios.c_iflag = 0;
	pc_driver->init_termios.c_oflag = 0;
	pc_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
	pc_driver->init_termios.c_lflag = 0;
	pc_driver->flags = TTY_DRIVER_REAL_RAW;
	tty_set_operations(pc_driver, &pc_ops);

	pc_info->owner = THIS_MODULE;
	pc_info->name = "digi_ctl";
	pc_info->major = DIGIINFOMAJOR;
	pc_info->minor_start = 0;
	pc_info->type = TTY_DRIVER_TYPE_SERIAL;
	pc_info->subtype = SERIAL_TYPE_INFO;
	pc_info->init_termios = tty_std_termios;
	pc_info->init_termios.c_iflag = 0;
	pc_info->init_termios.c_oflag = 0;
	pc_info->init_termios.c_lflag = 0;
	pc_info->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL;
	pc_info->flags = TTY_DRIVER_REAL_RAW;
	tty_set_operations(pc_info, &info_ops);


	for (crd = 0; crd < num_cards; crd++) 
	{ /* Begin for each card */

		/*  ------------------------------------------------------------------
			This is where the appropriate memory handlers for the hardware is
			set.  Everything at runtime blindly jumps through these vectors.
		---------------------------------------------------------------------- */

		/* defined in epcaconfig.h */
		bd = &boards[crd];

		switch (bd->type)
		{ /* Begin switch on bd->type {board type} */
			case PCXEM:
			case EISAXEM:
				bd->memwinon     = pcxem_memwinon ;
				bd->memwinoff    = pcxem_memwinoff ;
				bd->globalwinon  = pcxem_globalwinon ;
				bd->txwinon      = pcxem_txwinon ;
				bd->rxwinon      = pcxem_rxwinon ;
				bd->memoff       = pcxem_memoff ;
				bd->assertgwinon = dummy_assertgwinon;
				bd->assertmemoff = dummy_assertmemoff;
				break;

			case PCIXEM:
			case PCIXRJ:
			case PCIXR:
				bd->memwinon     = dummy_memwinon;
				bd->memwinoff    = dummy_memwinoff;
				bd->globalwinon  = dummy_globalwinon;
				bd->txwinon      = dummy_txwinon;
				bd->rxwinon      = dummy_rxwinon;
				bd->memoff       = dummy_memoff;
				bd->assertgwinon = dummy_assertgwinon;
				bd->assertmemoff = dummy_assertmemoff;
				break;

			case PCXE:
			case PCXEVE:

				bd->memwinon     = pcxe_memwinon;
				bd->memwinoff    = pcxe_memwinoff;
				bd->globalwinon  = pcxe_globalwinon;
				bd->txwinon      = pcxe_txwinon;
				bd->rxwinon      = pcxe_rxwinon;
				bd->memoff       = pcxe_memoff;
				bd->assertgwinon = dummy_assertgwinon;
				bd->assertmemoff = dummy_assertmemoff;
				break;

			case PCXI:
			case PC64XE:

				bd->memwinon     = pcxi_memwinon;
				bd->memwinoff    = pcxi_memwinoff;
				bd->globalwinon  = pcxi_globalwinon;
				bd->txwinon      = pcxi_txwinon;
				bd->rxwinon      = pcxi_rxwinon;
				bd->memoff       = pcxi_memoff;
				bd->assertgwinon = pcxi_assertgwinon;
				bd->assertmemoff = pcxi_assertmemoff;
				break;

			default:
				break;

		} /* End switch on bd->type */

		/* ---------------------------------------------------------------
			Some cards need a memory segment to be defined for use in 
			transmit and receive windowing operations.  These boards
			are listed in the below switch.  In the case of the XI the
			amount of memory on the board is variable so the memory_seg
			is also variable.  This code determines what they segment 
			should be.
		----------------------------------------------------------------- */

		switch (bd->type)
		{ /* Begin switch on bd->type {board type} */

			case PCXE:
			case PCXEVE:
			case PC64XE:
				bd->memory_seg = 0xf000;
			break;

			case PCXI:
				board_id = inb((int)bd->port);
				if ((board_id & 0x1) == 0x1) 
				{ /* Begin it's an XI card */ 

					/* Is it a 64K board */
					if ((board_id & 0x30) == 0) 
						bd->memory_seg = 0xf000;

					/* Is it a 128K board */
					if ((board_id & 0x30) == 0x10) 
						bd->memory_seg = 0xe000;

					/* Is is a 256K board */	
					if ((board_id & 0x30) == 0x20) 
						bd->memory_seg = 0xc000;

					/* Is it a 512K board */
					if ((board_id & 0x30) == 0x30) 
						bd->memory_seg = 0x8000;

1365
				} else printk(KERN_ERR "epca: Board at 0x%x doesn't appear to be an XI\n",(int)bd->port);
L
Linus Torvalds 已提交
1366 1367 1368 1369 1370 1371
			break;

		} /* End switch on bd->type */

	} /* End for each card */

1372 1373 1374 1375 1376
	err = tty_register_driver(pc_driver);
	if (err) {
		printk(KERN_ERR "Couldn't register Digi PC/ driver");
		goto out3;
	}
L
Linus Torvalds 已提交
1377

1378 1379 1380 1381 1382
	err = tty_register_driver(pc_info);
	if (err) {
		printk(KERN_ERR "Couldn't register Digi PC/ info ");
		goto out4;
	}
L
Linus Torvalds 已提交
1383 1384 1385 1386 1387 1388 1389 1390 1391 1392

	/* -------------------------------------------------------------------
	   Start up the poller to check for events on all enabled boards
	---------------------------------------------------------------------- */

	init_timer(&epca_timer);
	epca_timer.function = epcapoll;
	mod_timer(&epca_timer, jiffies + HZ/25);
	return 0;

1393 1394 1395 1396 1397 1398 1399 1400 1401
out4:
	tty_unregister_driver(pc_driver);
out3:
	put_tty_driver(pc_info);
out2:
	put_tty_driver(pc_driver);
out1:
	return err;

L
Linus Torvalds 已提交
1402 1403 1404 1405 1406 1407 1408 1409
} /* End pc_init */

/* ------------------ Begin post_fep_init  ---------------------- */

static void post_fep_init(unsigned int crd)
{ /* Begin post_fep_init */

	int i;
1410 1411
	void __iomem *memaddr;
	struct global_data __iomem *gd;
L
Linus Torvalds 已提交
1412
	struct board_info *bd;
1413
	struct board_chan __iomem *bc;
L
Linus Torvalds 已提交
1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431
	struct channel *ch; 
	int shrinkmem = 0, lowwater ; 
 
	/*  -------------------------------------------------------------
		This call is made by the user via. the ioctl call DIGI_INIT.
		It is responsible for setting up all the card specific stuff.
	---------------------------------------------------------------- */
	bd = &boards[crd];

	/* -----------------------------------------------------------------
		If this is a PCI board, get the port info.  Remember PCI cards
		do not have entries into the epcaconfig.h file, so we can't get 
		the number of ports from it.  Unfortunetly, this means that anyone
		doing a DIGI_GETINFO before the board has booted will get an invalid
		number of ports returned (It should return 0).  Calls to DIGI_GETINFO
		after DIGI_INIT has been called will return the proper values. 
	------------------------------------------------------------------- */

1432
	if (bd->type >= PCIXEM) { /* Begin get PCI number of ports */
L
Linus Torvalds 已提交
1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445
		/* --------------------------------------------------------------------
			Below we use XEMPORTS as a memory offset regardless of which PCI
			card it is.  This is because all of the supported PCI cards have
			the same memory offset for the channel data.  This will have to be
			changed if we ever develop a PCI/XE card.  NOTE : The FEP manual
			states that the port offset is 0xC22 as opposed to 0xC02.  This is
			only true for PC/XE, and PC/XI cards; not for the XEM, or CX series.
			On the PCI cards the number of ports is determined by reading a 
			ID PROM located in the box attached to the card.  The card can then
			determine the index the id to determine the number of ports available.
			(FYI - The id should be located at 0x1ac (And may use up to 4 bytes
			if the box in question is a XEM or CX)).  
		------------------------------------------------------------------------ */ 
1446 1447
		/* PCI cards are already remapped at this point ISA are not */
		bd->numports = readw(bd->re_map_membase + XEMPORTS);
L
Linus Torvalds 已提交
1448 1449
		epcaassert(bd->numports <= 64,"PCI returned a invalid number of ports");
		nbdevs += (bd->numports);
1450 1451 1452 1453 1454
	} else {
		/* Fix up the mappings for ISA/EISA etc */
		/* FIXME: 64K - can we be smarter ? */
		bd->re_map_membase = ioremap(bd->membase, 0x10000);
	}
L
Linus Torvalds 已提交
1455 1456 1457 1458 1459 1460 1461 1462 1463

	if (crd != 0)
		card_ptr[crd] = card_ptr[crd-1] + boards[crd-1].numports;
	else
		card_ptr[crd] = &digi_channels[crd]; /* <- For card 0 only */

	ch = card_ptr[crd];
	epcaassert(ch <= &digi_channels[nbdevs - 1], "ch out of range");

1464
	memaddr = bd->re_map_membase;
L
Linus Torvalds 已提交
1465 1466 1467 1468 1469 1470 1471

	/* -----------------------------------------------------------------
		The below assignment will set bc to point at the BEGINING of
		the cards channel structures.  For 1 card there will be between
		8 and 64 of these structures.
	-------------------------------------------------------------------- */

1472
	bc = memaddr + CHANSTRUCT;
L
Linus Torvalds 已提交
1473 1474 1475 1476 1477 1478 1479 1480

	/* -------------------------------------------------------------------
		The below assignment will set gd to point at the BEGINING of
		global memory address 0xc00.  The first data in that global
		memory actually starts at address 0xc1a.  The command in 
		pointer begins at 0xd10.
	---------------------------------------------------------------------- */

1481
	gd = memaddr + GLOBAL;
L
Linus Torvalds 已提交
1482 1483 1484 1485 1486 1487

	/* --------------------------------------------------------------------
		XEPORTS (address 0xc22) points at the number of channels the
		card supports. (For 64XE, XI, XEM, and XR use 0xc02)
	----------------------------------------------------------------------- */

1488
	if ((bd->type == PCXEVE || bd->type == PCXE) && (readw(memaddr + XEPORTS) < 3))
L
Linus Torvalds 已提交
1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501
		shrinkmem = 1;
	if (bd->type < PCIXEM)
		if (!request_region((int)bd->port, 4, board_desc[bd->type]))
			return;		
	memwinon(bd, 0);

	/*  --------------------------------------------------------------------
		Remember ch is the main drivers channels structure, while bc is 
	   the cards channel structure.
	------------------------------------------------------------------------ */

	/* For every port on the card do ..... */

1502 1503
	for (i = 0; i < bd->numports; i++, ch++, bc++)  { /* Begin for each port */
		unsigned long flags;
1504
		u16 tseg, rseg;
L
Linus Torvalds 已提交
1505 1506 1507

		ch->brdchan        = bc;
		ch->mailbox        = gd; 
D
David Howells 已提交
1508
		INIT_WORK(&ch->tqueue, do_softint);
L
Linus Torvalds 已提交
1509 1510
		ch->board          = &boards[crd];

1511 1512
		spin_lock_irqsave(&epca_lock, flags);
		switch (bd->type) {
L
Linus Torvalds 已提交
1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544
			/* ----------------------------------------------------------------
				Since some of the boards use different bitmaps for their
				control signals we cannot hard code these values and retain
				portability.  We virtualize this data here.
			------------------------------------------------------------------- */
			case EISAXEM:
			case PCXEM:
			case PCIXEM:
			case PCIXRJ:
			case PCIXR:
				ch->m_rts = 0x02 ;
				ch->m_dcd = 0x80 ; 
				ch->m_dsr = 0x20 ;
				ch->m_cts = 0x10 ;
				ch->m_ri  = 0x40 ;
				ch->m_dtr = 0x01 ;
				break;

			case PCXE:
			case PCXEVE:
			case PCXI:
			case PC64XE:
				ch->m_rts = 0x02 ;
				ch->m_dcd = 0x08 ; 
				ch->m_dsr = 0x10 ;
				ch->m_cts = 0x20 ;
				ch->m_ri  = 0x40 ;
				ch->m_dtr = 0x80 ;
				break;
	
		} /* End switch bd->type */

1545
		if (boards[crd].altpin) {
L
Linus Torvalds 已提交
1546 1547 1548 1549
			ch->dsr = ch->m_dcd;
			ch->dcd = ch->m_dsr;
			ch->digiext.digi_flags |= DIGI_ALTPIN;
		}
1550
		else {
L
Linus Torvalds 已提交
1551 1552 1553 1554 1555 1556 1557 1558 1559
			ch->dcd = ch->m_dcd;
			ch->dsr = ch->m_dsr;
		}
	
		ch->boardnum   = crd;
		ch->channelnum = i;
		ch->magic      = EPCA_MAGIC;
		ch->tty        = NULL;

1560
		if (shrinkmem) {
L
Linus Torvalds 已提交
1561 1562 1563 1564
			fepcmd(ch, SETBUFFER, 32, 0, 0, 0);
			shrinkmem = 0;
		}

1565 1566 1567
		tseg = readw(&bc->tseg);
		rseg = readw(&bc->rseg);

1568
		switch (bd->type) {
L
Linus Torvalds 已提交
1569 1570 1571 1572 1573

			case PCIXEM:
			case PCIXRJ:
			case PCIXR:
				/* Cover all the 2MEG cards */
1574 1575 1576 1577
				ch->txptr = memaddr + ((tseg << 4) & 0x1fffff);
				ch->rxptr = memaddr + ((rseg << 4) & 0x1fffff);
				ch->txwin = FEPWIN | (tseg >> 11);
				ch->rxwin = FEPWIN | (rseg >> 11);
L
Linus Torvalds 已提交
1578 1579 1580 1581 1582 1583
				break;

			case PCXEM:
			case EISAXEM:
				/* Cover all the 32K windowed cards */
				/* Mask equal to window size - 1 */
1584 1585 1586 1587
				ch->txptr = memaddr + ((tseg << 4) & 0x7fff);
				ch->rxptr = memaddr + ((rseg << 4) & 0x7fff);
				ch->txwin = FEPWIN | (tseg >> 11);
				ch->rxwin = FEPWIN | (rseg >> 11);
L
Linus Torvalds 已提交
1588 1589 1590 1591
				break;

			case PCXEVE:
			case PCXE:
1592 1593 1594 1595
				ch->txptr = memaddr + (((tseg - bd->memory_seg) << 4) & 0x1fff);
				ch->txwin = FEPWIN | ((tseg - bd->memory_seg) >> 9);
				ch->rxptr = memaddr + (((rseg - bd->memory_seg) << 4) & 0x1fff);
				ch->rxwin = FEPWIN | ((rseg - bd->memory_seg) >>9 );
L
Linus Torvalds 已提交
1596 1597 1598 1599
				break;

			case PCXI:
			case PC64XE:
1600 1601
				ch->txptr = memaddr + ((tseg - bd->memory_seg) << 4);
				ch->rxptr = memaddr + ((rseg - bd->memory_seg) << 4);
L
Linus Torvalds 已提交
1602 1603 1604 1605 1606 1607
				ch->txwin = ch->rxwin = 0;
				break;

		} /* End switch bd->type */

		ch->txbufhead = 0;
1608
		ch->txbufsize = readw(&bc->tmax) + 1;
L
Linus Torvalds 已提交
1609 1610
	
		ch->rxbufhead = 0;
1611
		ch->rxbufsize = readw(&bc->rmax) + 1;
L
Linus Torvalds 已提交
1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625
	
		lowwater = ch->txbufsize >= 2000 ? 1024 : (ch->txbufsize / 2);

		/* Set transmitter low water mark */
		fepcmd(ch, STXLWATER, lowwater, 0, 10, 0);

		/* Set receiver low water mark */

		fepcmd(ch, SRXLWATER, (ch->rxbufsize / 4), 0, 10, 0);

		/* Set receiver high water mark */

		fepcmd(ch, SRXHWATER, (3 * ch->rxbufsize / 4), 0, 10, 0);

1626 1627
		writew(100, &bc->edelay);
		writeb(1, &bc->idata);
L
Linus Torvalds 已提交
1628
	
1629 1630 1631 1632
		ch->startc  = readb(&bc->startc);
		ch->stopc   = readb(&bc->stopc);
		ch->startca = readb(&bc->startca);
		ch->stopca  = readb(&bc->stopca);
L
Linus Torvalds 已提交
1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646
	
		ch->fepcflag = 0;
		ch->fepiflag = 0;
		ch->fepoflag = 0;
		ch->fepstartc = 0;
		ch->fepstopc = 0;
		ch->fepstartca = 0;
		ch->fepstopca = 0;
	
		ch->close_delay = 50;
		ch->count = 0;
		ch->blocked_open = 0;
		init_waitqueue_head(&ch->open_wait);
		init_waitqueue_head(&ch->close_wait);
1647 1648

		spin_unlock_irqrestore(&epca_lock, flags);
L
Linus Torvalds 已提交
1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691
	} /* End for each port */

	printk(KERN_INFO 
	        "Digi PC/Xx Driver V%s:  %s I/O = 0x%lx Mem = 0x%lx Ports = %d\n", 
	        VERSION, board_desc[bd->type], (long)bd->port, (long)bd->membase, bd->numports);
	memwinoff(bd, 0);

} /* End post_fep_init */

/* --------------------- Begin epcapoll  ------------------------ */

static void epcapoll(unsigned long ignored)
{ /* Begin epcapoll */

	unsigned long flags;
	int crd;
	volatile unsigned int head, tail;
	struct channel *ch;
	struct board_info *bd;

	/* -------------------------------------------------------------------
		This routine is called upon every timer interrupt.  Even though
		the Digi series cards are capable of generating interrupts this 
		method of non-looping polling is more efficient.  This routine
		checks for card generated events (Such as receive data, are transmit
		buffer empty) and acts on those events.
	----------------------------------------------------------------------- */
	
	for (crd = 0; crd < num_cards; crd++) 
	{ /* Begin for each card */

		bd = &boards[crd];
		ch = card_ptr[crd];

		if ((bd->status == DISABLED) || digi_poller_inhibited)
			continue; /* Begin loop next interation */

		/* -----------------------------------------------------------
			assertmemoff is not needed here; indeed it is an empty subroutine.
			It is being kept because future boards may need this as well as
			some legacy boards.
		---------------------------------------------------------------- */

1692 1693
		spin_lock_irqsave(&epca_lock, flags);

L
Linus Torvalds 已提交
1694 1695 1696 1697 1698 1699 1700 1701 1702
		assertmemoff(ch);

		globalwinon(ch);

		/* ---------------------------------------------------------------
			In this case head and tail actually refer to the event queue not
			the transmit or receive queue.
		------------------------------------------------------------------- */

1703 1704
		head = readw(&ch->mailbox->ein);
		tail = readw(&ch->mailbox->eout);
L
Linus Torvalds 已提交
1705 1706 1707 1708 1709 1710 1711
		
		/* If head isn't equal to tail we have an event */

		if (head != tail)
			doevent(crd);
		memoff(ch);

1712
		spin_unlock_irqrestore(&epca_lock, flags);
L
Linus Torvalds 已提交
1713

1714
	} /* End for each card */
L
Linus Torvalds 已提交
1715 1716 1717 1718 1719 1720 1721 1722
	mod_timer(&epca_timer, jiffies + (HZ / 25));
} /* End epcapoll */

/* --------------------- Begin doevent  ------------------------ */

static void doevent(int crd)
{ /* Begin doevent */

1723
	void __iomem *eventbuf;
L
Linus Torvalds 已提交
1724 1725
	struct channel *ch, *chan0;
	static struct tty_struct *tty;
1726
	struct board_info *bd;
1727
	struct board_chan __iomem *bc;
1728 1729 1730
	unsigned int tail, head;
	int event, channel;
	int mstat, lstat;
L
Linus Torvalds 已提交
1731 1732 1733 1734 1735 1736 1737 1738 1739 1740

	/* -------------------------------------------------------------------
		This subroutine is called by epcapoll when an event is detected 
		in the event queue.  This routine responds to those events.
	--------------------------------------------------------------------- */
	bd = &boards[crd];

	chan0 = card_ptr[crd];
	epcaassert(chan0 <= &digi_channels[nbdevs - 1], "ch out of range");
	assertgwinon(chan0);
1741
	while ((tail = readw(&chan0->mailbox->eout)) != (head = readw(&chan0->mailbox->ein)))
L
Linus Torvalds 已提交
1742 1743
	{ /* Begin while something in event queue */
		assertgwinon(chan0);
1744
		eventbuf = bd->re_map_membase + tail + ISTART;
L
Linus Torvalds 已提交
1745
		/* Get the channel the event occurred on */
1746
		channel = readb(eventbuf);
L
Linus Torvalds 已提交
1747
		/* Get the actual event code that occurred */
1748
		event = readb(eventbuf + 1);
L
Linus Torvalds 已提交
1749 1750 1751 1752 1753
		/*  ----------------------------------------------------------------
			The two assignments below get the current modem status (mstat)
			and the previous modem status (lstat).  These are useful becuase
			an event could signal a change in modem signals itself.
		------------------------------------------------------------------- */
1754 1755
		mstat = readb(eventbuf + 2);
		lstat = readb(eventbuf + 3);
L
Linus Torvalds 已提交
1756 1757

		ch = chan0 + channel;
1758
		if ((unsigned)channel >= bd->numports || !ch)  {
L
Linus Torvalds 已提交
1759 1760 1761 1762 1763 1764 1765 1766 1767
			if (channel >= bd->numports)
				ch = chan0;
			bc = ch->brdchan;
			goto next;
		}

		if ((bc = ch->brdchan) == NULL)
			goto next;

1768
		if (event & DATA_IND)  { /* Begin DATA_IND */
L
Linus Torvalds 已提交
1769 1770 1771 1772
			receive_data(ch);
			assertgwinon(ch);
		} /* End DATA_IND */
		/* else *//* Fix for DCD transition missed bug */
1773
		if (event & MODEMCHG_IND)  { /* Begin MODEMCHG_IND */
L
Linus Torvalds 已提交
1774 1775
			/* A modem signal change has been indicated */
			ch->imodem = mstat;
1776
			if (ch->asyncflags & ASYNC_CHECK_CD)  {
L
Linus Torvalds 已提交
1777 1778 1779 1780 1781 1782 1783
				if (mstat & ch->dcd)  /* We are now receiving dcd */
					wake_up_interruptible(&ch->open_wait);
				else
					pc_sched_event(ch, EPCA_EVENT_HANGUP); /* No dcd; hangup */
			}
		} /* End MODEMCHG_IND */
		tty = ch->tty;
1784 1785
		if (tty)  { /* Begin if valid tty */
			if (event & BREAK_IND)  { /* Begin if BREAK_IND */
L
Linus Torvalds 已提交
1786
				/* A break has been indicated */
A
Alan Cox 已提交
1787
				tty_insert_flip_char(tty, 0, TTY_BREAK);
L
Linus Torvalds 已提交
1788
				tty_schedule_flip(tty); 
1789
			} else if (event & LOWTX_IND)  { /* Begin LOWTX_IND */
L
Linus Torvalds 已提交
1790 1791 1792 1793 1794 1795
				if (ch->statusflags & LOWWAIT) 
				{ /* Begin if LOWWAIT */
					ch->statusflags &= ~LOWWAIT;
					tty_wakeup(tty);
					wake_up_interruptible(&tty->write_wait);
				} /* End if LOWWAIT */
1796
			} else if (event & EMPTYTX_IND)  { /* Begin EMPTYTX_IND */
L
Linus Torvalds 已提交
1797 1798
				/* This event is generated by setup_empty_event */
				ch->statusflags &= ~TXBUSY;
1799
				if (ch->statusflags & EMPTYWAIT)  { /* Begin if EMPTYWAIT */
L
Linus Torvalds 已提交
1800 1801 1802 1803 1804 1805 1806 1807
					ch->statusflags &= ~EMPTYWAIT;
					tty_wakeup(tty);
					wake_up_interruptible(&tty->write_wait);
				} /* End if EMPTYWAIT */
			} /* End EMPTYTX_IND */
		} /* End if valid tty */
	next:
		globalwinon(ch);
1808 1809 1810
		BUG_ON(!bc);
		writew(1, &bc->idata);
		writew((tail + 4) & (IMAX - ISTART - 4), &chan0->mailbox->eout);
L
Linus Torvalds 已提交
1811 1812 1813 1814 1815 1816 1817 1818 1819
		globalwinon(chan0);
	} /* End while something in event queue */
} /* End doevent */

/* --------------------- Begin fepcmd  ------------------------ */

static void fepcmd(struct channel *ch, int cmd, int word_or_byte,
                   int byte2, int ncmds, int bytecmd)
{ /* Begin fepcmd */
1820
	unchar __iomem *memaddr;
L
Linus Torvalds 已提交
1821 1822 1823 1824 1825 1826 1827 1828 1829 1830
	unsigned int head, cmdTail, cmdStart, cmdMax;
	long count;
	int n;

	/* This is the routine in which commands may be passed to the card. */

	if (ch->board->status == DISABLED)
		return;
	assertgwinon(ch);
	/* Remember head (As well as max) is just an offset not a base addr */
1831
	head = readw(&ch->mailbox->cin);
L
Linus Torvalds 已提交
1832
	/* cmdStart is a base address */
1833
	cmdStart = readw(&ch->mailbox->cstart);
L
Linus Torvalds 已提交
1834 1835 1836 1837 1838
	/* ------------------------------------------------------------------
		We do the addition below because we do not want a max pointer 
		relative to cmdStart.  We want a max pointer that points at the 
		physical end of the command queue.
	-------------------------------------------------------------------- */
1839
	cmdMax = (cmdStart + 4 + readw(&ch->mailbox->cmax));
L
Linus Torvalds 已提交
1840 1841
	memaddr = ch->board->re_map_membase;

1842 1843 1844
	if (head >= (cmdMax - cmdStart) || (head & 03))  {
		printk(KERN_ERR "line %d: Out of range, cmd = %x, head = %x\n", __LINE__,  cmd, head);
		printk(KERN_ERR "line %d: Out of range, cmdMax = %x, cmdStart = %x\n", __LINE__,  cmdMax, cmdStart);
L
Linus Torvalds 已提交
1845 1846
		return;
	}
1847 1848 1849
	if (bytecmd)  {
		writeb(cmd, memaddr + head + cmdStart + 0);
		writeb(ch->channelnum,  memaddr + head + cmdStart + 1);
L
Linus Torvalds 已提交
1850
		/* Below word_or_byte is bits to set */
1851
		writeb(word_or_byte,  memaddr + head + cmdStart + 2);
L
Linus Torvalds 已提交
1852
		/* Below byte2 is bits to reset */
1853 1854 1855 1856 1857
		writeb(byte2, memaddr + head + cmdStart + 3);
	}  else {
		writeb(cmd, memaddr + head + cmdStart + 0);
		writeb(ch->channelnum,  memaddr + head + cmdStart + 1);
		writeb(word_or_byte,  memaddr + head + cmdStart + 2);
L
Linus Torvalds 已提交
1858 1859
	}
	head = (head + 4) & (cmdMax - cmdStart - 4);
1860
	writew(head, &ch->mailbox->cin);
L
Linus Torvalds 已提交
1861 1862
	count = FEPTIMEOUT;

1863
	for (;;)  { /* Begin forever loop */
L
Linus Torvalds 已提交
1864
		count--;
1865
		if (count == 0)  {
L
Linus Torvalds 已提交
1866 1867 1868
			printk(KERN_ERR "<Error> - Fep not responding in fepcmd()\n");
			return;
		}
1869 1870
		head = readw(&ch->mailbox->cin);
		cmdTail = readw(&ch->mailbox->cout);
L
Linus Torvalds 已提交
1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893
		n = (head - cmdTail) & (cmdMax - cmdStart - 4);
		/* ----------------------------------------------------------
			Basically this will break when the FEP acknowledges the 
			command by incrementing cmdTail (Making it equal to head).
		------------------------------------------------------------- */
		if (n <= ncmds * (sizeof(short) * 4))
			break; /* Well nearly forever :-) */
	} /* End forever loop */
} /* End fepcmd */

/* ---------------------------------------------------------------------
	Digi products use fields in their channels structures that are very
	similar to the c_cflag and c_iflag fields typically found in UNIX
	termios structures.  The below three routines allow mappings 
	between these hardware "flags" and their respective Linux flags.
------------------------------------------------------------------------- */
 
/* --------------------- Begin termios2digi_h -------------------- */

static unsigned termios2digi_h(struct channel *ch, unsigned cflag)
{ /* Begin termios2digi_h */
	unsigned res = 0;

1894
	if (cflag & CRTSCTS) {
L
Linus Torvalds 已提交
1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941
		ch->digiext.digi_flags |= (RTSPACE | CTSPACE);
		res |= ((ch->m_cts) | (ch->m_rts));
	}

	if (ch->digiext.digi_flags & RTSPACE)
		res |= ch->m_rts;

	if (ch->digiext.digi_flags & DTRPACE)
		res |= ch->m_dtr;

	if (ch->digiext.digi_flags & CTSPACE)
		res |= ch->m_cts;

	if (ch->digiext.digi_flags & DSRPACE)
		res |= ch->dsr;

	if (ch->digiext.digi_flags & DCDPACE)
		res |= ch->dcd;

	if (res & (ch->m_rts))
		ch->digiext.digi_flags |= RTSPACE;

	if (res & (ch->m_cts))
		ch->digiext.digi_flags |= CTSPACE;

	return res;

} /* End termios2digi_h */

/* --------------------- Begin termios2digi_i -------------------- */
static unsigned termios2digi_i(struct channel *ch, unsigned iflag)
{ /* Begin termios2digi_i */

	unsigned res = iflag & (IGNBRK | BRKINT | IGNPAR | PARMRK | 
	                        INPCK | ISTRIP|IXON|IXANY|IXOFF);
	if (ch->digiext.digi_flags & DIGI_AIXON)
		res |= IAIXON;
	return res;

} /* End termios2digi_i */

/* --------------------- Begin termios2digi_c -------------------- */

static unsigned termios2digi_c(struct channel *ch, unsigned cflag)
{ /* Begin termios2digi_c */

	unsigned res = 0;
1942
	if (cflag & CBAUDEX) { /* Begin detected CBAUDEX */
L
Linus Torvalds 已提交
1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976
		ch->digiext.digi_flags |= DIGI_FAST;
		/* -------------------------------------------------------------
		   HUPCL bit is used by FEP to indicate fast baud
		   table is to be used.
		----------------------------------------------------------------- */
		res |= FEP_HUPCL;
	} /* End detected CBAUDEX */
	else ch->digiext.digi_flags &= ~DIGI_FAST; 
	/* -------------------------------------------------------------------
		CBAUD has bit position 0x1000 set these days to indicate Linux
		baud rate remap.  Digi hardware can't handle the bit assignment.
		(We use a different bit assignment for high speed.).  Clear this
		bit out.
	---------------------------------------------------------------------- */
	res |= cflag & ((CBAUD ^ CBAUDEX) | PARODD | PARENB | CSTOPB | CSIZE);
	/* -------------------------------------------------------------
		This gets a little confusing.  The Digi cards have their own
		representation of c_cflags controling baud rate.  For the most
		part this is identical to the Linux implementation.  However;
		Digi supports one rate (76800) that Linux doesn't.  This means 
		that the c_cflag entry that would normally mean 76800 for Digi
		actually means 115200 under Linux.  Without the below mapping,
		a stty 115200 would only drive the board at 76800.  Since 
		the rate 230400 is also found after 76800, the same problem afflicts	
		us when we choose a rate of 230400.  Without the below modificiation
		stty 230400 would actually give us 115200.

		There are two additional differences.  The Linux value for CLOCAL
		(0x800; 0004000) has no meaning to the Digi hardware.  Also in 
		later releases of Linux; the CBAUD define has CBAUDEX (0x1000;
		0010000) ored into it (CBAUD = 0x100f as opposed to 0xf). CBAUDEX
		should be checked for a screened out prior to termios2digi_c 
		returning.  Since CLOCAL isn't used by the board this can be
		ignored as long as the returned value is used only by Digi hardware. 
1977 1978
		----------------------------------------------------------------- */
	if (cflag & CBAUDEX) {
L
Linus Torvalds 已提交
1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996
		/* -------------------------------------------------------------
			The below code is trying to guarantee that only baud rates
			115200 and 230400 are remapped.  We use exclusive or because
			the various baud rates share common bit positions and therefore
			can't be tested for easily.
		----------------------------------------------------------------- */

				
		if ((!((cflag & 0x7) ^ (B115200 & ~CBAUDEX))) || 
		    (!((cflag & 0x7) ^ (B230400 & ~CBAUDEX))))
			res += 1;
	}
	return res;

} /* End termios2digi_c */

/* --------------------- Begin epcaparam  ----------------------- */

1997
/* Caller must hold the locks */
L
Linus Torvalds 已提交
1998 1999 2000 2001 2002
static void epcaparam(struct tty_struct *tty, struct channel *ch)
{ /* Begin epcaparam */

	unsigned int cmdHead;
	struct termios *ts;
2003
	struct board_chan __iomem *bc;
L
Linus Torvalds 已提交
2004 2005 2006 2007 2008 2009 2010
	unsigned mval, hflow, cflag, iflag;

	bc = ch->brdchan;
	epcaassert(bc !=0, "bc out of range");

	assertgwinon(ch);
	ts = tty->termios;
2011 2012
	if ((ts->c_cflag & CBAUD) == 0)  { /* Begin CBAUD detected */
		cmdHead = readw(&bc->rin);
2013
		writew(cmdHead, &bc->rout);
2014
		cmdHead = readw(&bc->tin);
L
Linus Torvalds 已提交
2015 2016 2017 2018 2019 2020 2021
		/* Changing baud in mid-stream transmission can be wonderful */
		/* ---------------------------------------------------------------
			Flush current transmit buffer by setting cmdTail pointer (tout)
			to cmdHead pointer (tin).  Hopefully the transmit buffer is empty.
		----------------------------------------------------------------- */
		fepcmd(ch, STOUT, (unsigned) cmdHead, 0, 0, 0);
		mval = 0;
2022
	} else  { /* Begin CBAUD not detected */
L
Linus Torvalds 已提交
2023 2024 2025 2026 2027
		/* -------------------------------------------------------------------
			c_cflags have changed but that change had nothing to do with BAUD.
			Propagate the change to the card.
		---------------------------------------------------------------------- */ 
		cflag = termios2digi_c(ch, ts->c_cflag);
2028
		if (cflag != ch->fepcflag)  {
L
Linus Torvalds 已提交
2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045
			ch->fepcflag = cflag;
			/* Set baud rate, char size, stop bits, parity */
			fepcmd(ch, SETCTRLFLAGS, (unsigned) cflag, 0, 0, 0);
		}
		/* ----------------------------------------------------------------
			If the user has not forced CLOCAL and if the device is not a 
			CALLOUT device (Which is always CLOCAL) we set flags such that
			the driver will wait on carrier detect.
		------------------------------------------------------------------- */
		if (ts->c_cflag & CLOCAL)
			ch->asyncflags &= ~ASYNC_CHECK_CD;
		else
			ch->asyncflags |= ASYNC_CHECK_CD;
		mval = ch->m_dtr | ch->m_rts;
	} /* End CBAUD not detected */
	iflag = termios2digi_i(ch, ts->c_iflag);
	/* Check input mode flags */
2046
	if (iflag != ch->fepiflag)  {
L
Linus Torvalds 已提交
2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060
		ch->fepiflag = iflag;
		/* ---------------------------------------------------------------
			Command sets channels iflag structure on the board. Such things 
			as input soft flow control, handling of parity errors, and
			break handling are all set here.
		------------------------------------------------------------------- */
		/* break handling, parity handling, input stripping, flow control chars */
		fepcmd(ch, SETIFLAGS, (unsigned int) ch->fepiflag, 0, 0, 0);
	}
	/* ---------------------------------------------------------------
		Set the board mint value for this channel.  This will cause hardware
		events to be generated each time the DCD signal (Described in mint) 
		changes.	
	------------------------------------------------------------------- */
2061
	writeb(ch->dcd, &bc->mint);
L
Linus Torvalds 已提交
2062 2063
	if ((ts->c_cflag & CLOCAL) || (ch->digiext.digi_flags & DIGI_FORCEDCD))
		if (ch->digiext.digi_flags & DIGI_FORCEDCD)
2064 2065
			writeb(0, &bc->mint);
	ch->imodem = readb(&bc->mstat);
L
Linus Torvalds 已提交
2066
	hflow = termios2digi_h(ch, ts->c_cflag);
2067
	if (hflow != ch->hflow)  {
L
Linus Torvalds 已提交
2068 2069 2070 2071 2072 2073 2074 2075 2076
		ch->hflow = hflow;
		/* --------------------------------------------------------------
			Hard flow control has been selected but the board is not
			using it.  Activate hard flow control now.
		----------------------------------------------------------------- */
		fepcmd(ch, SETHFLOW, hflow, 0xff, 0, 1);
	}
	mval ^= ch->modemfake & (mval ^ ch->modem);

2077
	if (ch->omodem ^ mval)  {
L
Linus Torvalds 已提交
2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090
		ch->omodem = mval;
		/* --------------------------------------------------------------
			The below command sets the DTR and RTS mstat structure.  If
			hard flow control is NOT active these changes will drive the
			output of the actual DTR and RTS lines.  If hard flow control 
			is active, the changes will be saved in the mstat structure and
			only asserted when hard flow control is turned off. 
		----------------------------------------------------------------- */

		/* First reset DTR & RTS; then set them */
		fepcmd(ch, SETMODEM, 0, ((ch->m_dtr)|(ch->m_rts)), 0, 1);
		fepcmd(ch, SETMODEM, mval, 0, 0, 1);
	}
2091
	if (ch->startc != ch->fepstartc || ch->stopc != ch->fepstopc)  {
L
Linus Torvalds 已提交
2092 2093 2094 2095 2096 2097 2098 2099
		ch->fepstartc = ch->startc;
		ch->fepstopc = ch->stopc;
		/* ------------------------------------------------------------
			The XON / XOFF characters have changed; propagate these
			changes to the card.	
		--------------------------------------------------------------- */
		fepcmd(ch, SONOFFC, ch->fepstartc, ch->fepstopc, 0, 1);
	}
2100
	if (ch->startca != ch->fepstartca || ch->stopca != ch->fepstopca)  {
L
Linus Torvalds 已提交
2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111
		ch->fepstartca = ch->startca;
		ch->fepstopca = ch->stopca;
		/* ---------------------------------------------------------------
			Similar to the above, this time the auxilarly XON / XOFF 
			characters have changed; propagate these changes to the card.
		------------------------------------------------------------------ */
		fepcmd(ch, SAUXONOFFC, ch->fepstartca, ch->fepstopca, 0, 1);
	}
} /* End epcaparam */

/* --------------------- Begin receive_data  ----------------------- */
2112
/* Caller holds lock */
L
Linus Torvalds 已提交
2113 2114 2115 2116 2117 2118
static void receive_data(struct channel *ch)
{ /* Begin receive_data */

	unchar *rptr;
	struct termios *ts = NULL;
	struct tty_struct *tty;
2119
	struct board_chan __iomem *bc;
2120 2121
	int dataToRead, wrapgap, bytesAvailable;
	unsigned int tail, head;
L
Linus Torvalds 已提交
2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135
	unsigned int wrapmask;

	/* ---------------------------------------------------------------
		This routine is called by doint when a receive data event 
		has taken place.
	------------------------------------------------------------------- */

	globalwinon(ch);
	if (ch->statusflags & RXSTOPPED)
		return;
	tty = ch->tty;
	if (tty)
		ts = tty->termios;
	bc = ch->brdchan;
2136
	BUG_ON(!bc);
L
Linus Torvalds 已提交
2137 2138 2139 2140 2141 2142
	wrapmask = ch->rxbufsize - 1;

	/* --------------------------------------------------------------------- 
		Get the head and tail pointers to the receiver queue.  Wrap the 
		head pointer if it has reached the end of the buffer.
	------------------------------------------------------------------------ */
2143
	head = readw(&bc->rin);
L
Linus Torvalds 已提交
2144
	head &= wrapmask;
2145
	tail = readw(&bc->rout) & wrapmask;
L
Linus Torvalds 已提交
2146 2147 2148 2149 2150 2151 2152 2153 2154

	bytesAvailable = (head - tail) & wrapmask;
	if (bytesAvailable == 0)
		return;

	/* ------------------------------------------------------------------
	   If CREAD bit is off or device not open, set TX tail to head
	--------------------------------------------------------------------- */

2155
	if (!tty || !ts || !(ts->c_cflag & CREAD))  {
2156
		writew(head, &bc->rout);
L
Linus Torvalds 已提交
2157 2158 2159
		return;
	}

A
Alan Cox 已提交
2160
	if (tty_buffer_request_room(tty, bytesAvailable + 1) == 0)
L
Linus Torvalds 已提交
2161 2162
		return;

2163 2164 2165
	if (readb(&bc->orun)) {
		writeb(0, &bc->orun);
		printk(KERN_WARNING "epca; overrun! DigiBoard device %s\n",tty->name);
A
Alan Cox 已提交
2166
		tty_insert_flip_char(tty, 0, TTY_OVERRUN);
L
Linus Torvalds 已提交
2167 2168
	}
	rxwinon(ch);
2169
	while (bytesAvailable > 0)  { /* Begin while there is data on the card */
L
Linus Torvalds 已提交
2170 2171 2172 2173 2174 2175 2176 2177 2178 2179
		wrapgap = (head >= tail) ? head - tail : ch->rxbufsize - tail;
		/* ---------------------------------------------------------------
			Even if head has wrapped around only report the amount of
			data to be equal to the size - tail.  Remember memcpy can't
			automaticly wrap around the receive buffer.
		----------------------------------------------------------------- */
		dataToRead = (wrapgap < bytesAvailable) ? wrapgap : bytesAvailable;
		/* --------------------------------------------------------------
		   Make sure we don't overflow the buffer
		----------------------------------------------------------------- */
A
Alan Cox 已提交
2180
		dataToRead = tty_prepare_flip_string(tty, &rptr, dataToRead);
L
Linus Torvalds 已提交
2181 2182 2183 2184 2185 2186
		if (dataToRead == 0)
			break;
		/* ---------------------------------------------------------------
			Move data read from our card into the line disciplines buffer
			for translation if necessary.
		------------------------------------------------------------------ */
2187
		memcpy_fromio(rptr, ch->rxptr + tail, dataToRead);
L
Linus Torvalds 已提交
2188 2189 2190 2191
		tail = (tail + dataToRead) & wrapmask;
		bytesAvailable -= dataToRead;
	} /* End while there is data on the card */
	globalwinon(ch);
2192
	writew(tail, &bc->rout);
L
Linus Torvalds 已提交
2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207
	/* Must be called with global data */
	tty_schedule_flip(ch->tty); 
	return;
} /* End receive_data */

static int info_ioctl(struct tty_struct *tty, struct file * file,
		    unsigned int cmd, unsigned long arg)
{
	switch (cmd) 
	{ /* Begin switch cmd */
		case DIGI_GETINFO:
		{ /* Begin case DIGI_GETINFO */
			struct digi_info di ;
			int brd;

2208 2209 2210 2211
			if(get_user(brd, (unsigned int __user *)arg))
				return -EFAULT;
			if (brd < 0 || brd >= num_cards || num_cards == 0)
				return -ENODEV;
L
Linus Torvalds 已提交
2212 2213 2214 2215 2216 2217 2218

			memset(&di, 0, sizeof(di));

			di.board = brd ; 
			di.status = boards[brd].status;
			di.type = boards[brd].type ;
			di.numports = boards[brd].numports ;
2219 2220 2221
			/* Legacy fixups - just move along nothing to see */
			di.port = (unsigned char *)boards[brd].port ;
			di.membase = (unsigned char *)boards[brd].membase ;
L
Linus Torvalds 已提交
2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234

			if (copy_to_user((void __user *)arg, &di, sizeof (di)))
				return -EFAULT;
			break;

		} /* End case DIGI_GETINFO */

		case DIGI_POLLER:
		{ /* Begin case DIGI_POLLER */

			int brd = arg & 0xff000000 >> 16 ; 
			unsigned char state = arg & 0xff ; 

2235 2236
			if (brd < 0 || brd >= num_cards) {
				printk(KERN_ERR "epca: DIGI POLLER : brd not valid!\n");
L
Linus Torvalds 已提交
2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256
				return (-ENODEV);
			}
			digi_poller_inhibited = state ;
			break ; 
		} /* End case DIGI_POLLER */

		case DIGI_INIT:
		{ /* Begin case DIGI_INIT */
			/* ------------------------------------------------------------
				This call is made by the apps to complete the initilization
				of the board(s).  This routine is responsible for setting
				the card to its initial state and setting the drivers control
				fields to the sutianle settings for the card in question.
			---------------------------------------------------------------- */
			int crd ; 
			for (crd = 0; crd < num_cards; crd++) 
				post_fep_init (crd);
			break ; 
		} /* End case DIGI_INIT */
		default:
2257
			return -ENOTTY;
L
Linus Torvalds 已提交
2258 2259 2260 2261 2262 2263 2264 2265
	} /* End switch cmd */
	return (0) ;
}
/* --------------------- Begin pc_ioctl  ----------------------- */

static int pc_tiocmget(struct tty_struct *tty, struct file *file)
{
	struct channel *ch = (struct channel *) tty->driver_data;
2266
	struct board_chan __iomem *bc;
L
Linus Torvalds 已提交
2267 2268 2269 2270 2271 2272
	unsigned int mstat, mflag = 0;
	unsigned long flags;

	if (ch)
		bc = ch->brdchan;
	else
2273
		return -EINVAL;
L
Linus Torvalds 已提交
2274

2275
	spin_lock_irqsave(&epca_lock, flags);
L
Linus Torvalds 已提交
2276
	globalwinon(ch);
2277
	mstat = readb(&bc->mstat);
L
Linus Torvalds 已提交
2278
	memoff(ch);
2279
	spin_unlock_irqrestore(&epca_lock, flags);
L
Linus Torvalds 已提交
2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301

	if (mstat & ch->m_dtr)
		mflag |= TIOCM_DTR;
	if (mstat & ch->m_rts)
		mflag |= TIOCM_RTS;
	if (mstat & ch->m_cts)
		mflag |= TIOCM_CTS;
	if (mstat & ch->dsr)
		mflag |= TIOCM_DSR;
	if (mstat & ch->m_ri)
		mflag |= TIOCM_RI;
	if (mstat & ch->dcd)
		mflag |= TIOCM_CD;
	return mflag;
}

static int pc_tiocmset(struct tty_struct *tty, struct file *file,
		       unsigned int set, unsigned int clear)
{
	struct channel *ch = (struct channel *) tty->driver_data;
	unsigned long flags;

2302 2303
	if (!ch)
		return -EINVAL;
L
Linus Torvalds 已提交
2304

2305
	spin_lock_irqsave(&epca_lock, flags);
L
Linus Torvalds 已提交
2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333
	/*
	 * I think this modemfake stuff is broken.  It doesn't
	 * correctly reflect the behaviour desired by the TIOCM*
	 * ioctls.  Therefore this is probably broken.
	 */
	if (set & TIOCM_RTS) {
		ch->modemfake |= ch->m_rts;
		ch->modem |= ch->m_rts;
	}
	if (set & TIOCM_DTR) {
		ch->modemfake |= ch->m_dtr;
		ch->modem |= ch->m_dtr;
	}
	if (clear & TIOCM_RTS) {
		ch->modemfake |= ch->m_rts;
		ch->modem &= ~ch->m_rts;
	}
	if (clear & TIOCM_DTR) {
		ch->modemfake |= ch->m_dtr;
		ch->modem &= ~ch->m_dtr;
	}
	globalwinon(ch);
	/*  --------------------------------------------------------------
		The below routine generally sets up parity, baud, flow control
		issues, etc.... It effect both control flags and input flags.
	------------------------------------------------------------------ */
	epcaparam(tty,ch);
	memoff(ch);
2334
	spin_unlock_irqrestore(&epca_lock, flags);
L
Linus Torvalds 已提交
2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346
	return 0;
}

static int pc_ioctl(struct tty_struct *tty, struct file * file,
		    unsigned int cmd, unsigned long arg)
{ /* Begin pc_ioctl */

	digiflow_t dflow;
	int retval;
	unsigned long flags;
	unsigned int mflag, mstat;
	unsigned char startc, stopc;
2347
	struct board_chan __iomem *bc;
L
Linus Torvalds 已提交
2348 2349 2350 2351 2352 2353
	struct channel *ch = (struct channel *) tty->driver_data;
	void __user *argp = (void __user *)arg;
	
	if (ch)
		bc = ch->brdchan;
	else 
2354
		return -EINVAL;
L
Linus Torvalds 已提交
2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365

	/* -------------------------------------------------------------------
		For POSIX compliance we need to add more ioctls.  See tty_ioctl.c
		in /usr/src/linux/drivers/char for a good example.  In particular 
		think about adding TCSETAF, TCSETAW, TCSETA, TCSETSF, TCSETSW, TCSETS.
	---------------------------------------------------------------------- */

	switch (cmd) 
	{ /* Begin switch cmd */

		case TCGETS:
2366
			if (copy_to_user(argp, tty->termios, sizeof(struct termios)))
L
Linus Torvalds 已提交
2367
				return -EFAULT;
2368
			return 0;
L
Linus Torvalds 已提交
2369 2370 2371 2372 2373 2374 2375
		case TCGETA:
			return get_termio(tty, argp);
		case TCSBRK:	/* SVID version: non-zero arg --> no break */
			retval = tty_check_change(tty);
			if (retval)
				return retval;
			/* Setup an event to indicate when the transmit buffer empties */
2376
			spin_lock_irqsave(&epca_lock, flags);
L
Linus Torvalds 已提交
2377
			setup_empty_event(tty,ch);		
2378
			spin_unlock_irqrestore(&epca_lock, flags);
L
Linus Torvalds 已提交
2379 2380 2381 2382 2383 2384 2385 2386 2387 2388
			tty_wait_until_sent(tty, 0);
			if (!arg)
				digi_send_break(ch, HZ/4);    /* 1/4 second */
			return 0;
		case TCSBRKP:	/* support for POSIX tcsendbreak() */
			retval = tty_check_change(tty);
			if (retval)
				return retval;

			/* Setup an event to indicate when the transmit buffer empties */
2389
			spin_lock_irqsave(&epca_lock, flags);
L
Linus Torvalds 已提交
2390
			setup_empty_event(tty,ch);		
2391
			spin_unlock_irqrestore(&epca_lock, flags);
L
Linus Torvalds 已提交
2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419
			tty_wait_until_sent(tty, 0);
			digi_send_break(ch, arg ? arg*(HZ/10) : HZ/4);
			return 0;
		case TIOCGSOFTCAR:
			if (put_user(C_CLOCAL(tty)?1:0, (unsigned long __user *)arg))
				return -EFAULT;
			return 0;
		case TIOCSSOFTCAR:
		{
			unsigned int value;

			if (get_user(value, (unsigned __user *)argp))
				return -EFAULT;
			tty->termios->c_cflag =
				((tty->termios->c_cflag & ~CLOCAL) |
				 (value ? CLOCAL : 0));
			return 0;
		}
		case TIOCMODG:
			mflag = pc_tiocmget(tty, file);
			if (put_user(mflag, (unsigned long __user *)argp))
				return -EFAULT;
			break;
		case TIOCMODS:
			if (get_user(mstat, (unsigned __user *)argp))
				return -EFAULT;
			return pc_tiocmset(tty, file, mstat, ~mstat);
		case TIOCSDTR:
2420
			spin_lock_irqsave(&epca_lock, flags);
L
Linus Torvalds 已提交
2421 2422 2423 2424
			ch->omodem |= ch->m_dtr;
			globalwinon(ch);
			fepcmd(ch, SETMODEM, ch->m_dtr, 0, 10, 1);
			memoff(ch);
2425
			spin_unlock_irqrestore(&epca_lock, flags);
L
Linus Torvalds 已提交
2426 2427 2428
			break;

		case TIOCCDTR:
2429
			spin_lock_irqsave(&epca_lock, flags);
L
Linus Torvalds 已提交
2430 2431 2432 2433
			ch->omodem &= ~ch->m_dtr;
			globalwinon(ch);
			fepcmd(ch, SETMODEM, 0, ch->m_dtr, 10, 1);
			memoff(ch);
2434
			spin_unlock_irqrestore(&epca_lock, flags);
L
Linus Torvalds 已提交
2435 2436 2437 2438 2439 2440 2441
			break;
		case DIGI_GETA:
			if (copy_to_user(argp, &ch->digiext, sizeof(digi_t)))
				return -EFAULT;
			break;
		case DIGI_SETAW:
		case DIGI_SETAF:
2442
			if (cmd == DIGI_SETAW) {
L
Linus Torvalds 已提交
2443
				/* Setup an event to indicate when the transmit buffer empties */
2444
				spin_lock_irqsave(&epca_lock, flags);
L
Linus Torvalds 已提交
2445
				setup_empty_event(tty,ch);		
2446
				spin_unlock_irqrestore(&epca_lock, flags);
L
Linus Torvalds 已提交
2447
				tty_wait_until_sent(tty, 0);
2448
			} else 	{
L
Linus Torvalds 已提交
2449 2450 2451 2452 2453 2454 2455 2456 2457
				/* ldisc lock already held in ioctl */
				if (tty->ldisc.flush_buffer)
					tty->ldisc.flush_buffer(tty);
			}
			/* Fall Thru */
		case DIGI_SETA:
			if (copy_from_user(&ch->digiext, argp, sizeof(digi_t)))
				return -EFAULT;
			
2458
			if (ch->digiext.digi_flags & DIGI_ALTPIN)  {
L
Linus Torvalds 已提交
2459 2460
				ch->dcd = ch->m_dsr;
				ch->dsr = ch->m_dcd;
2461
			} else {
L
Linus Torvalds 已提交
2462 2463 2464 2465
				ch->dcd = ch->m_dcd;
				ch->dsr = ch->m_dsr;
			}
		
2466
			spin_lock_irqsave(&epca_lock, flags);
L
Linus Torvalds 已提交
2467 2468 2469 2470 2471 2472 2473 2474 2475
			globalwinon(ch);

			/* -----------------------------------------------------------------
				The below routine generally sets up parity, baud, flow control 
				issues, etc.... It effect both control flags and input flags.
			------------------------------------------------------------------- */

			epcaparam(tty,ch);
			memoff(ch);
2476
			spin_unlock_irqrestore(&epca_lock, flags);
L
Linus Torvalds 已提交
2477 2478 2479 2480
			break;

		case DIGI_GETFLOW:
		case DIGI_GETAFLOW:
2481
			spin_lock_irqsave(&epca_lock, flags);
L
Linus Torvalds 已提交
2482
			globalwinon(ch);
2483 2484 2485 2486 2487 2488
			if (cmd == DIGI_GETFLOW) {
				dflow.startc = readb(&bc->startc);
				dflow.stopc = readb(&bc->stopc);
			} else {
				dflow.startc = readb(&bc->startca);
				dflow.stopc = readb(&bc->stopca);
L
Linus Torvalds 已提交
2489 2490
			}
			memoff(ch);
2491
			spin_unlock_irqrestore(&epca_lock, flags);
L
Linus Torvalds 已提交
2492 2493 2494 2495 2496 2497 2498

			if (copy_to_user(argp, &dflow, sizeof(dflow)))
				return -EFAULT;
			break;

		case DIGI_SETAFLOW:
		case DIGI_SETFLOW:
2499
			if (cmd == DIGI_SETFLOW) {
L
Linus Torvalds 已提交
2500 2501
				startc = ch->startc;
				stopc = ch->stopc;
2502
			} else {
L
Linus Torvalds 已提交
2503 2504 2505 2506 2507 2508 2509
				startc = ch->startca;
				stopc = ch->stopca;
			}

			if (copy_from_user(&dflow, argp, sizeof(dflow)))
				return -EFAULT;

2510 2511
			if (dflow.startc != startc || dflow.stopc != stopc) { /* Begin  if setflow toggled */
				spin_lock_irqsave(&epca_lock, flags);
L
Linus Torvalds 已提交
2512 2513
				globalwinon(ch);

2514
				if (cmd == DIGI_SETFLOW) {
L
Linus Torvalds 已提交
2515 2516 2517
					ch->fepstartc = ch->startc = dflow.startc;
					ch->fepstopc = ch->stopc = dflow.stopc;
					fepcmd(ch, SONOFFC, ch->fepstartc, ch->fepstopc, 0, 1);
2518
				} else {
L
Linus Torvalds 已提交
2519 2520 2521 2522 2523
					ch->fepstartca = ch->startca = dflow.startc;
					ch->fepstopca  = ch->stopca = dflow.stopc;
					fepcmd(ch, SAUXONOFFC, ch->fepstartca, ch->fepstopca, 0, 1);
				}

2524
				if (ch->statusflags & TXSTOPPED)
L
Linus Torvalds 已提交
2525 2526 2527
					pc_start(tty);

				memoff(ch);
2528
				spin_unlock_irqrestore(&epca_lock, flags);
L
Linus Torvalds 已提交
2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547
			} /* End if setflow toggled */
			break;
		default:
			return -ENOIOCTLCMD;
	} /* End switch cmd */
	return 0;
} /* End pc_ioctl */

/* --------------------- Begin pc_set_termios  ----------------------- */

static void pc_set_termios(struct tty_struct *tty, struct termios *old_termios)
{ /* Begin pc_set_termios */

	struct channel *ch;
	unsigned long flags;
	/* ---------------------------------------------------------
		verifyChannel returns the channel from the tty struct
		if it is valid.  This serves as a sanity check.
	------------------------------------------------------------- */
2548 2549
	if ((ch = verifyChannel(tty)) != NULL)  { /* Begin if channel valid */
		spin_lock_irqsave(&epca_lock, flags);
L
Linus Torvalds 已提交
2550 2551 2552
		globalwinon(ch);
		epcaparam(tty, ch);
		memoff(ch);
2553
		spin_unlock_irqrestore(&epca_lock, flags);
L
Linus Torvalds 已提交
2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568

		if ((old_termios->c_cflag & CRTSCTS) &&
			 ((tty->termios->c_cflag & CRTSCTS) == 0))
			tty->hw_stopped = 0;

		if (!(old_termios->c_cflag & CLOCAL) &&
			 (tty->termios->c_cflag & CLOCAL))
			wake_up_interruptible(&ch->open_wait);

	} /* End if channel valid */

} /* End pc_set_termios */

/* --------------------- Begin do_softint  ----------------------- */

D
David Howells 已提交
2569
static void do_softint(struct work_struct *work)
L
Linus Torvalds 已提交
2570
{ /* Begin do_softint */
D
David Howells 已提交
2571
	struct channel *ch = container_of(work, struct channel, tqueue);
L
Linus Torvalds 已提交
2572
	/* Called in response to a modem change event */
2573
	if (ch && ch->magic == EPCA_MAGIC)  { /* Begin EPCA_MAGIC */
L
Linus Torvalds 已提交
2574 2575
		struct tty_struct *tty = ch->tty;

2576 2577
		if (tty && tty->driver_data) {
			if (test_and_clear_bit(EPCA_EVENT_HANGUP, &ch->event)) { /* Begin if clear_bit */
L
Linus Torvalds 已提交
2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601
				tty_hangup(tty);	/* FIXME: module removal race here - AKPM */
				wake_up_interruptible(&ch->open_wait);
				ch->asyncflags &= ~ASYNC_NORMAL_ACTIVE;
			} /* End if clear_bit */
		}
	} /* End EPCA_MAGIC */
} /* End do_softint */

/* ------------------------------------------------------------
	pc_stop and pc_start provide software flow control to the 
	routine and the pc_ioctl routine.
---------------------------------------------------------------- */

/* --------------------- Begin pc_stop  ----------------------- */

static void pc_stop(struct tty_struct *tty)
{ /* Begin pc_stop */

	struct channel *ch;
	unsigned long flags;
	/* ---------------------------------------------------------
		verifyChannel returns the channel from the tty struct
		if it is valid.  This serves as a sanity check.
	------------------------------------------------------------- */
2602 2603 2604
	if ((ch = verifyChannel(tty)) != NULL)  { /* Begin if valid channel */
		spin_lock_irqsave(&epca_lock, flags);
		if ((ch->statusflags & TXSTOPPED) == 0)  { /* Begin if transmit stop requested */
L
Linus Torvalds 已提交
2605 2606 2607 2608 2609 2610
			globalwinon(ch);
			/* STOP transmitting now !! */
			fepcmd(ch, PAUSETX, 0, 0, 0, 0);
			ch->statusflags |= TXSTOPPED;
			memoff(ch);
		} /* End if transmit stop requested */
2611
		spin_unlock_irqrestore(&epca_lock, flags);
L
Linus Torvalds 已提交
2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623
	} /* End if valid channel */
} /* End pc_stop */

/* --------------------- Begin pc_start  ----------------------- */

static void pc_start(struct tty_struct *tty)
{ /* Begin pc_start */
	struct channel *ch;
	/* ---------------------------------------------------------
		verifyChannel returns the channel from the tty struct
		if it is valid.  This serves as a sanity check.
	------------------------------------------------------------- */
2624
	if ((ch = verifyChannel(tty)) != NULL) { /* Begin if channel valid */
L
Linus Torvalds 已提交
2625
		unsigned long flags;
2626
		spin_lock_irqsave(&epca_lock, flags);
L
Linus Torvalds 已提交
2627
		/* Just in case output was resumed because of a change in Digi-flow */
2628
		if (ch->statusflags & TXSTOPPED)  { /* Begin transmit resume requested */
2629
			struct board_chan __iomem *bc;
L
Linus Torvalds 已提交
2630 2631 2632
			globalwinon(ch);
			bc = ch->brdchan;
			if (ch->statusflags & LOWWAIT)
2633
				writeb(1, &bc->ilow);
L
Linus Torvalds 已提交
2634 2635 2636 2637 2638
			/* Okay, you can start transmitting again... */
			fepcmd(ch, RESUMETX, 0, 0, 0, 0);
			ch->statusflags &= ~TXSTOPPED;
			memoff(ch);
		} /* End transmit resume requested */
2639
		spin_unlock_irqrestore(&epca_lock, flags);
L
Linus Torvalds 已提交
2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660
	} /* End if channel valid */
} /* End pc_start */

/* ------------------------------------------------------------------
	The below routines pc_throttle and pc_unthrottle are used 
	to slow (And resume) the receipt of data into the kernels
	receive buffers.  The exact occurrence of this depends on the
	size of the kernels receive buffer and what the 'watermarks'
	are set to for that buffer.  See the n_ttys.c file for more
	details. 
______________________________________________________________________ */
/* --------------------- Begin throttle  ----------------------- */

static void pc_throttle(struct tty_struct * tty)
{ /* Begin pc_throttle */
	struct channel *ch;
	unsigned long flags;
	/* ---------------------------------------------------------
		verifyChannel returns the channel from the tty struct
		if it is valid.  This serves as a sanity check.
	------------------------------------------------------------- */
2661 2662 2663
	if ((ch = verifyChannel(tty)) != NULL)  { /* Begin if channel valid */
		spin_lock_irqsave(&epca_lock, flags);
		if ((ch->statusflags & RXSTOPPED) == 0) {
L
Linus Torvalds 已提交
2664 2665 2666 2667 2668
			globalwinon(ch);
			fepcmd(ch, PAUSERX, 0, 0, 0, 0);
			ch->statusflags |= RXSTOPPED;
			memoff(ch);
		}
2669
		spin_unlock_irqrestore(&epca_lock, flags);
L
Linus Torvalds 已提交
2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682
	} /* End if channel valid */
} /* End pc_throttle */

/* --------------------- Begin unthrottle  ----------------------- */

static void pc_unthrottle(struct tty_struct *tty)
{ /* Begin pc_unthrottle */
	struct channel *ch;
	unsigned long flags;
	/* ---------------------------------------------------------
		verifyChannel returns the channel from the tty struct
		if it is valid.  This serves as a sanity check.
	------------------------------------------------------------- */
2683
	if ((ch = verifyChannel(tty)) != NULL)  { /* Begin if channel valid */
L
Linus Torvalds 已提交
2684
		/* Just in case output was resumed because of a change in Digi-flow */
2685 2686
		spin_lock_irqsave(&epca_lock, flags);
		if (ch->statusflags & RXSTOPPED) {
L
Linus Torvalds 已提交
2687 2688 2689 2690 2691
			globalwinon(ch);
			fepcmd(ch, RESUMERX, 0, 0, 0, 0);
			ch->statusflags &= ~RXSTOPPED;
			memoff(ch);
		}
2692
		spin_unlock_irqrestore(&epca_lock, flags);
L
Linus Torvalds 已提交
2693 2694 2695 2696 2697 2698 2699 2700 2701
	} /* End if channel valid */
} /* End pc_unthrottle */

/* --------------------- Begin digi_send_break  ----------------------- */

void digi_send_break(struct channel *ch, int msec)
{ /* Begin digi_send_break */
	unsigned long flags;

2702
	spin_lock_irqsave(&epca_lock, flags);
L
Linus Torvalds 已提交
2703 2704 2705 2706 2707 2708 2709 2710 2711 2712
	globalwinon(ch);
	/* -------------------------------------------------------------------- 
	   Maybe I should send an infinite break here, schedule() for
	   msec amount of time, and then stop the break.  This way,
	   the user can't screw up the FEP by causing digi_send_break()
	   to be called (i.e. via an ioctl()) more than once in msec amount 
	   of time.  Try this for now...
	------------------------------------------------------------------------ */
	fepcmd(ch, SENDBREAK, msec, 0, 10, 0);
	memoff(ch);
2713
	spin_unlock_irqrestore(&epca_lock, flags);
L
Linus Torvalds 已提交
2714 2715 2716 2717
} /* End digi_send_break */

/* --------------------- Begin setup_empty_event  ----------------------- */

2718 2719
/* Caller MUST hold the lock */

L
Linus Torvalds 已提交
2720 2721 2722
static void setup_empty_event(struct tty_struct *tty, struct channel *ch)
{ /* Begin setup_empty_event */

2723
	struct board_chan __iomem *bc = ch->brdchan;
L
Linus Torvalds 已提交
2724 2725 2726 2727 2728 2729 2730

	globalwinon(ch);
	ch->statusflags |= EMPTYWAIT;
	/* ------------------------------------------------------------------
		When set the iempty flag request a event to be generated when the 
		transmit buffer is empty (If there is no BREAK in progress).
	--------------------------------------------------------------------- */
2731
	writeb(1, &bc->iempty);
L
Linus Torvalds 已提交
2732 2733 2734 2735 2736 2737 2738 2739 2740
	memoff(ch);
} /* End setup_empty_event */

/* --------------------- Begin get_termio ----------------------- */

static int get_termio(struct tty_struct * tty, struct termio __user * termio)
{ /* Begin get_termio */
	return kernel_termios_to_user_termio(termio, tty->termios);
} /* End get_termio */
2741

L
Linus Torvalds 已提交
2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772
/* ---------------------- Begin epca_setup  -------------------------- */
void epca_setup(char *str, int *ints)
{ /* Begin epca_setup */
	struct board_info board;
	int               index, loop, last;
	char              *temp, *t2;
	unsigned          len;

	/* ----------------------------------------------------------------------
		If this routine looks a little strange it is because it is only called
		if a LILO append command is given to boot the kernel with parameters.  
		In this way, we can provide the user a method of changing his board
		configuration without rebuilding the kernel.
	----------------------------------------------------------------------- */
	if (!liloconfig) 
		liloconfig = 1; 

	memset(&board, 0, sizeof(board));

	/* Assume the data is int first, later we can change it */
	/* I think that array position 0 of ints holds the number of args */
	for (last = 0, index = 1; index <= ints[0]; index++)
		switch(index)
		{ /* Begin parse switch */
			case 1:
				board.status = ints[index];
				/* ---------------------------------------------------------
					We check for 2 (As opposed to 1; because 2 is a flag
					instructing the driver to ignore epcaconfig.)  For this
					reason we check for 2.
				------------------------------------------------------------ */ 
2773
				if (board.status == 2) { /* Begin ignore epcaconfig as well as lilo cmd line */
L
Linus Torvalds 已提交
2774 2775 2776 2777 2778
					nbdevs = 0;
					num_cards = 0;
					return;
				} /* End ignore epcaconfig as well as lilo cmd line */
	
2779 2780
				if (board.status > 2) {
					printk(KERN_ERR "epca_setup: Invalid board status 0x%x\n", board.status);
L
Linus Torvalds 已提交
2781 2782 2783 2784 2785 2786 2787 2788
					invalid_lilo_config = 1;
					setup_error_code |= INVALID_BOARD_STATUS;
					return;
				}
				last = index;
				break;
			case 2:
				board.type = ints[index];
2789 2790
				if (board.type >= PCIXEM)  {
					printk(KERN_ERR "epca_setup: Invalid board type 0x%x\n", board.type);
L
Linus Torvalds 已提交
2791 2792 2793 2794 2795 2796 2797 2798
					invalid_lilo_config = 1;
					setup_error_code |= INVALID_BOARD_TYPE;
					return;
				}
				last = index;
				break;
			case 3:
				board.altpin = ints[index];
2799 2800
				if (board.altpin > 1) {
					printk(KERN_ERR "epca_setup: Invalid board altpin 0x%x\n", board.altpin);
L
Linus Torvalds 已提交
2801 2802 2803 2804 2805 2806 2807 2808 2809
					invalid_lilo_config = 1;
					setup_error_code |= INVALID_ALTPIN;
					return;
				}
				last = index;
				break;

			case 4:
				board.numports = ints[index];
2810 2811
				if (board.numports < 2 || board.numports > 256) {
					printk(KERN_ERR "epca_setup: Invalid board numports 0x%x\n", board.numports);
L
Linus Torvalds 已提交
2812 2813 2814 2815 2816 2817 2818 2819 2820
					invalid_lilo_config = 1;
					setup_error_code |= INVALID_NUM_PORTS;
					return;
				}
				nbdevs += board.numports;
				last = index;
				break;

			case 5:
2821 2822 2823
				board.port = ints[index];
				if (ints[index] <= 0) {
					printk(KERN_ERR "epca_setup: Invalid io port 0x%x\n", (unsigned int)board.port);
L
Linus Torvalds 已提交
2824 2825 2826 2827 2828 2829 2830 2831
					invalid_lilo_config = 1;
					setup_error_code |= INVALID_PORT_BASE;
					return;
				}
				last = index;
				break;

			case 6:
2832 2833 2834
				board.membase = ints[index];
				if (ints[index] <= 0) {
					printk(KERN_ERR "epca_setup: Invalid memory base 0x%x\n",(unsigned int)board.membase);
L
Linus Torvalds 已提交
2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847
					invalid_lilo_config = 1;
					setup_error_code |= INVALID_MEM_BASE;
					return;
				}
				last = index;
				break;

			default:
				printk(KERN_ERR "<Error> - epca_setup: Too many integer parms\n");
				return;

		} /* End parse switch */

2848
	while (str && *str)  { /* Begin while there is a string arg */
L
Linus Torvalds 已提交
2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866
		/* find the next comma or terminator */
		temp = str;
		/* While string is not null, and a comma hasn't been found */
		while (*temp && (*temp != ','))
			temp++;
		if (!*temp)
			temp = NULL;
		else
			*temp++ = 0;
		/* Set index to the number of args + 1 */
		index = last + 1;

		switch(index)
		{
			case 1:
				len = strlen(str);
				if (strncmp("Disable", str, len) == 0) 
					board.status = 0;
2867
				else if (strncmp("Enable", str, len) == 0)
L
Linus Torvalds 已提交
2868
					board.status = 1;
2869 2870
				else {
					printk(KERN_ERR "epca_setup: Invalid status %s\n", str);
L
Linus Torvalds 已提交
2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887
					invalid_lilo_config = 1;
					setup_error_code |= INVALID_BOARD_STATUS;
					return;
				}
				last = index;
				break;

			case 2:
				for(loop = 0; loop < EPCA_NUM_TYPES; loop++)
					if (strcmp(board_desc[loop], str) == 0)
						break;
				/* ---------------------------------------------------------------
					If the index incremented above refers to a legitamate board 
					type set it here. 
				------------------------------------------------------------------*/
				if (index < EPCA_NUM_TYPES) 
					board.type = loop;
2888 2889
				else {
					printk(KERN_ERR "epca_setup: Invalid board type: %s\n", str);
L
Linus Torvalds 已提交
2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900
					invalid_lilo_config = 1;
					setup_error_code |= INVALID_BOARD_TYPE;
					return;
				}
				last = index;
				break;

			case 3:
				len = strlen(str);
				if (strncmp("Disable", str, len) == 0) 
					board.altpin = 0;
2901
				else if (strncmp("Enable", str, len) == 0)
L
Linus Torvalds 已提交
2902
					board.altpin = 1;
2903 2904
				else {
					printk(KERN_ERR "epca_setup: Invalid altpin %s\n", str);
L
Linus Torvalds 已提交
2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916
					invalid_lilo_config = 1;
					setup_error_code |= INVALID_ALTPIN;
					return;
				}
				last = index;
				break;

			case 4:
				t2 = str;
				while (isdigit(*t2))
					t2++;

2917 2918
				if (*t2) {
					printk(KERN_ERR "epca_setup: Invalid port count %s\n", str);
L
Linus Torvalds 已提交
2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946
					invalid_lilo_config = 1;
					setup_error_code |= INVALID_NUM_PORTS;
					return;
				}

				/* ------------------------------------------------------------
					There is not a man page for simple_strtoul but the code can be 
					found in vsprintf.c.  The first argument is the string to 
					translate (To an unsigned long obviously),  the second argument
					can be the address of any character variable or a NULL.  If a
					variable is given, the end pointer of the string will be stored 
					in that variable; if a NULL is given the end pointer will 
					not be returned.  The last argument is the base to use.  If 
					a 0 is indicated, the routine will attempt to determine the 
					proper base by looking at the values prefix (A '0' for octal,
					a 'x' for hex, etc ...  If a value is given it will use that 
					value as the base. 
				---------------------------------------------------------------- */ 
				board.numports = simple_strtoul(str, NULL, 0);
				nbdevs += board.numports;
				last = index;
				break;

			case 5:
				t2 = str;
				while (isxdigit(*t2))
					t2++;

2947 2948
				if (*t2) {
					printk(KERN_ERR "epca_setup: Invalid i/o address %s\n", str);
L
Linus Torvalds 已提交
2949 2950 2951 2952 2953
					invalid_lilo_config = 1;
					setup_error_code |= INVALID_PORT_BASE;
					return;
				}

2954
				board.port = simple_strtoul(str, NULL, 16);
L
Linus Torvalds 已提交
2955 2956 2957 2958 2959 2960 2961 2962
				last = index;
				break;

			case 6:
				t2 = str;
				while (isxdigit(*t2))
					t2++;

2963 2964
				if (*t2) {
					printk(KERN_ERR "epca_setup: Invalid memory base %s\n",str);
L
Linus Torvalds 已提交
2965 2966 2967 2968
					invalid_lilo_config = 1;
					setup_error_code |= INVALID_MEM_BASE;
					return;
				}
2969
				board.membase = simple_strtoul(str, NULL, 16);
L
Linus Torvalds 已提交
2970 2971 2972
				last = index;
				break;
			default:
2973
				printk(KERN_ERR "epca: Too many string parms\n");
L
Linus Torvalds 已提交
2974 2975 2976 2977 2978
				return;
		}
		str = temp;
	} /* End while there is a string arg */

2979 2980
	if (last < 6) {
		printk(KERN_ERR "epca: Insufficient parms specified\n");
L
Linus Torvalds 已提交
2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040
		return;
	}
 
	/* I should REALLY validate the stuff here */
	/* Copies our local copy of board into boards */
	memcpy((void *)&boards[num_cards],(void *)&board, sizeof(board));
	/* Does this get called once per lilo arg are what ? */
	printk(KERN_INFO "PC/Xx: Added board %i, %s %i ports at 0x%4.4X base 0x%6.6X\n", 
		num_cards, board_desc[board.type], 
		board.numports, (int)board.port, (unsigned int) board.membase);
	num_cards++;
} /* End epca_setup */


/* ------------------------ Begin init_PCI  --------------------------- */

enum epic_board_types {
	brd_xr = 0,
	brd_xem,
	brd_cx,
	brd_xrj,
};


/* indexed directly by epic_board_types enum */
static struct {
	unsigned char board_type;
	unsigned bar_idx;		/* PCI base address region */
} epca_info_tbl[] = {
	{ PCIXR, 0, },
	{ PCIXEM, 0, },
	{ PCICX, 0, },
	{ PCIXRJ, 2, },
};

static int __devinit epca_init_one (struct pci_dev *pdev,
				 const struct pci_device_id *ent)
{
	static int board_num = -1;
	int board_idx, info_idx = ent->driver_data;
	unsigned long addr;

	if (pci_enable_device(pdev))
		return -EIO;

	board_num++;
	board_idx = board_num + num_cards;
	if (board_idx >= MAXBOARDS)
		goto err_out;
	
	addr = pci_resource_start (pdev, epca_info_tbl[info_idx].bar_idx);
	if (!addr) {
		printk (KERN_ERR PFX "PCI region #%d not available (size 0)\n",
			epca_info_tbl[info_idx].bar_idx);
		goto err_out;
	}

	boards[board_idx].status = ENABLED;
	boards[board_idx].type = epca_info_tbl[info_idx].board_type;
	boards[board_idx].numports = 0x0;
3041 3042
	boards[board_idx].port = addr + PCI_IO_OFFSET;
	boards[board_idx].membase = addr;
L
Linus Torvalds 已提交
3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102

	if (!request_mem_region (addr + PCI_IO_OFFSET, 0x200000, "epca")) {
		printk (KERN_ERR PFX "resource 0x%x @ 0x%lx unavailable\n",
			0x200000, addr + PCI_IO_OFFSET);
		goto err_out;
	}

	boards[board_idx].re_map_port = ioremap(addr + PCI_IO_OFFSET, 0x200000);
	if (!boards[board_idx].re_map_port) {
		printk (KERN_ERR PFX "cannot map 0x%x @ 0x%lx\n",
			0x200000, addr + PCI_IO_OFFSET);
		goto err_out_free_pciio;
	}

	if (!request_mem_region (addr, 0x200000, "epca")) {
		printk (KERN_ERR PFX "resource 0x%x @ 0x%lx unavailable\n",
			0x200000, addr);
		goto err_out_free_iounmap;
	}

	boards[board_idx].re_map_membase = ioremap(addr, 0x200000);
	if (!boards[board_idx].re_map_membase) {
		printk (KERN_ERR PFX "cannot map 0x%x @ 0x%lx\n",
			0x200000, addr + PCI_IO_OFFSET);
		goto err_out_free_memregion;
	}

	/* --------------------------------------------------------------
		I don't know what the below does, but the hardware guys say
		its required on everything except PLX (In this case XRJ).
	---------------------------------------------------------------- */
	if (info_idx != brd_xrj) {
		pci_write_config_byte(pdev, 0x40, 0);  
		pci_write_config_byte(pdev, 0x46, 0);
	}
	
	return 0;

err_out_free_memregion:
	release_mem_region (addr, 0x200000);
err_out_free_iounmap:
	iounmap (boards[board_idx].re_map_port);
err_out_free_pciio:
	release_mem_region (addr + PCI_IO_OFFSET, 0x200000);
err_out:
	return -ENODEV;
}


static struct pci_device_id epca_pci_tbl[] = {
	{ PCI_VENDOR_DIGI, PCI_DEVICE_XR, PCI_ANY_ID, PCI_ANY_ID, 0, 0, brd_xr },
	{ PCI_VENDOR_DIGI, PCI_DEVICE_XEM, PCI_ANY_ID, PCI_ANY_ID, 0, 0, brd_xem },
	{ PCI_VENDOR_DIGI, PCI_DEVICE_CX, PCI_ANY_ID, PCI_ANY_ID, 0, 0, brd_cx },
	{ PCI_VENDOR_DIGI, PCI_DEVICE_XRJ, PCI_ANY_ID, PCI_ANY_ID, 0, 0, brd_xrj },
	{ 0, }
};

MODULE_DEVICE_TABLE(pci, epca_pci_tbl);

int __init init_PCI (void)
3103
{	/* Begin init_PCI */
L
Linus Torvalds 已提交
3104 3105 3106 3107 3108 3109
	memset (&epca_driver, 0, sizeof (epca_driver));
	epca_driver.name = "epca";
	epca_driver.id_table = epca_pci_tbl;
	epca_driver.probe = epca_init_one;

	return pci_register_driver(&epca_driver);
3110
}
L
Linus Torvalds 已提交
3111 3112

MODULE_LICENSE("GPL");