epca.c 75.2 KB
Newer Older
L
Linus Torvalds 已提交
1 2
/*
	Copyright (C) 1996  Digi International.
3

L
Linus Torvalds 已提交
4 5 6
	For technical support please email digiLinux@dgii.com or
	call Digi tech support at (612) 912-3456

7 8
	** This driver is no longer supported by Digi **

9 10 11 12 13 14 15 16
	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.
L
Linus Torvalds 已提交
17

18 19 20 21 22 23 24 25 26 27
	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*/
L
Linus Torvalds 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40

#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>
A
Alan Cox 已提交
41 42
#include <linux/uaccess.h>
#include <linux/io.h>
43
#include <linux/spinlock.h>
L
Linus Torvalds 已提交
44 45
#include <linux/pci.h>
#include "digiPCI.h"
46

L
Linus Torvalds 已提交
47 48 49 50 51 52

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

53
#define VERSION            "1.3.0.1-LK2.6"
L
Linus Torvalds 已提交
54 55

/* This major needs to be submitted to Linux to join the majors list */
56
#define DIGIINFOMAJOR       35  /* For Digi specific ioctl */
L
Linus Torvalds 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69


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

#define PFX "epca: "

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

static int setup_error_code;
static int invalid_lilo_config;

70 71
/*
 * The ISA boards do window flipping into the same spaces so its only sane with
A
Alan Cox 已提交
72 73 74
 * a single lock. It's still pretty efficient. This lock guards the hardware
 * and the tty_port lock guards the kernel side stuff like use counts. Take
 * this lock inside the port lock if you must take both.
75
 */
I
Ingo Molnar 已提交
76
static DEFINE_SPINLOCK(epca_lock);
77

A
Alan Cox 已提交
78 79
/* MAXBOARDS is typically 12, but ISA and EISA cards are restricted
   to 7 below. */
L
Linus Torvalds 已提交
80 81 82 83 84 85 86
static struct board_info boards[MAXBOARDS];

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

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

87 88 89 90 91 92 93
/*
 * 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.
 */
L
Linus Torvalds 已提交
94 95
static struct channel digi_channels[MAX_ALLOC];

96 97 98 99 100
/*
 * 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.
 */
L
Linus Torvalds 已提交
101 102 103 104
static struct channel *card_ptr[MAXCARDS];

static struct timer_list epca_timer;

105 106 107 108
/*
 * Begin generic memory functions. These functions will be alias (point at)
 * more specific functions dependent on the board being configured.
 */
109 110 111 112 113 114 115 116
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 已提交
117 118 119

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

120 121 122 123 124 125
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 已提交
126 127 128

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

129 130 131 132 133 134
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 已提交
135 136 137 138

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

139 140 141 142 143 144
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 已提交
145 146 147

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

148 149 150 151 152 153 154 155
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 已提交
156

157 158
static struct channel *verifyChannel(struct tty_struct *);
static void pc_sched_event(struct channel *, int);
L
Linus Torvalds 已提交
159 160
static void epca_error(int, char *);
static void pc_close(struct tty_struct *, struct file *);
A
Alan Cox 已提交
161
static void shutdown(struct channel *, struct tty_struct *tty);
L
Linus Torvalds 已提交
162 163 164 165 166 167
static void pc_hangup(struct tty_struct *);
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 *,
A
Alan Cox 已提交
168
			struct channel *);
L
Linus Torvalds 已提交
169 170 171 172 173 174 175 176 177
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 *);
A
Alan Cox 已提交
178
static void receive_data(struct channel *, struct tty_struct *tty);
L
Linus Torvalds 已提交
179
static int pc_ioctl(struct tty_struct *, struct file *,
A
Alan Cox 已提交
180
			unsigned int, unsigned long);
L
Linus Torvalds 已提交
181
static int info_ioctl(struct tty_struct *, struct file *,
A
Alan Cox 已提交
182
			unsigned int, unsigned long);
A
Alan Cox 已提交
183
static void pc_set_termios(struct tty_struct *, struct ktermios *);
D
David Howells 已提交
184
static void do_softint(struct work_struct *work);
L
Linus Torvalds 已提交
185 186
static void pc_stop(struct tty_struct *);
static void pc_start(struct tty_struct *);
A
Alan Cox 已提交
187
static void pc_throttle(struct tty_struct *tty);
L
Linus Torvalds 已提交
188
static void pc_unthrottle(struct tty_struct *tty);
A
Alan Cox 已提交
189
static int pc_send_break(struct tty_struct *tty, int msec);
L
Linus Torvalds 已提交
190 191 192
static void setup_empty_event(struct tty_struct *tty, struct channel *ch);

static int pc_write(struct tty_struct *, const unsigned char *, int);
193
static int pc_init(void);
L
Linus Torvalds 已提交
194 195
static int init_PCI(void);

196 197 198 199 200 201 202 203 204
/*
 * 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.
 */
205
static void memwinon(struct board_info *b, unsigned int win)
L
Linus Torvalds 已提交
206
{
207
	b->memwinon(b, win);
L
Linus Torvalds 已提交
208 209
}

210
static void memwinoff(struct board_info *b, unsigned int win)
L
Linus Torvalds 已提交
211
{
212
	b->memwinoff(b, win);
L
Linus Torvalds 已提交
213 214
}

215
static void globalwinon(struct channel *ch)
L
Linus Torvalds 已提交
216
{
217
	ch->board->globalwinon(ch);
L
Linus Torvalds 已提交
218 219
}

220
static void rxwinon(struct channel *ch)
L
Linus Torvalds 已提交
221
{
222
	ch->board->rxwinon(ch);
L
Linus Torvalds 已提交
223 224
}

225
static void txwinon(struct channel *ch)
L
Linus Torvalds 已提交
226
{
227
	ch->board->txwinon(ch);
L
Linus Torvalds 已提交
228 229
}

230
static void memoff(struct channel *ch)
L
Linus Torvalds 已提交
231
{
232
	ch->board->memoff(ch);
L
Linus Torvalds 已提交
233
}
234
static void assertgwinon(struct channel *ch)
L
Linus Torvalds 已提交
235
{
236
	ch->board->assertgwinon(ch);
L
Linus Torvalds 已提交
237 238
}

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

244
/* PCXEM windowing is the same as that used in the PCXR and CX series cards. */
245
static void pcxem_memwinon(struct board_info *b, unsigned int win)
L
Linus Torvalds 已提交
246
{
A
Alan Cox 已提交
247
	outb_p(FEPWIN | win, b->port + 1);
L
Linus Torvalds 已提交
248 249
}

250
static void pcxem_memwinoff(struct board_info *b, unsigned int win)
L
Linus Torvalds 已提交
251
{
252
	outb_p(0, b->port + 1);
L
Linus Torvalds 已提交
253 254
}

255
static void pcxem_globalwinon(struct channel *ch)
L
Linus Torvalds 已提交
256
{
A
Alan Cox 已提交
257
	outb_p(FEPWIN, (int)ch->board->port + 1);
L
Linus Torvalds 已提交
258 259
}

260
static void pcxem_rxwinon(struct channel *ch)
L
Linus Torvalds 已提交
261 262 263 264
{
	outb_p(ch->rxwin, (int)ch->board->port + 1);
}

265
static void pcxem_txwinon(struct channel *ch)
L
Linus Torvalds 已提交
266 267 268 269
{
	outb_p(ch->txwin, (int)ch->board->port + 1);
}

270
static void pcxem_memoff(struct channel *ch)
L
Linus Torvalds 已提交
271 272 273 274 275
{
	outb_p(0, (int)ch->board->port + 1);
}

/* ----------------- Begin pcxe memory window stuff ------------------ */
276
static void pcxe_memwinon(struct board_info *b, unsigned int win)
L
Linus Torvalds 已提交
277
{
278
	outb_p(FEPWIN | win, b->port + 1);
L
Linus Torvalds 已提交
279 280
}

281
static void pcxe_memwinoff(struct board_info *b, unsigned int win)
L
Linus Torvalds 已提交
282
{
283
	outb_p(inb(b->port) & ~FEPMEM, b->port + 1);
284
	outb_p(0, b->port + 1);
L
Linus Torvalds 已提交
285 286
}

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

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

297
static void pcxe_txwinon(struct channel *ch)
L
Linus Torvalds 已提交
298
{
299
	outb_p(ch->txwin, (int)ch->board->port + 1);
L
Linus Torvalds 已提交
300 301
}

302
static void pcxe_memoff(struct channel *ch)
L
Linus Torvalds 已提交
303 304 305 306 307 308
{
	outb_p(0, (int)ch->board->port);
	outb_p(0, (int)ch->board->port + 1);
}

/* ------------- Begin pc64xe and pcxi memory window stuff -------------- */
309
static void pcxi_memwinon(struct board_info *b, unsigned int win)
L
Linus Torvalds 已提交
310
{
311
	outb_p(inb(b->port) | FEPMEM, b->port);
L
Linus Torvalds 已提交
312 313
}

314
static void pcxi_memwinoff(struct board_info *b, unsigned int win)
L
Linus Torvalds 已提交
315
{
316
	outb_p(inb(b->port) & ~FEPMEM, b->port);
L
Linus Torvalds 已提交
317 318
}

319
static void pcxi_globalwinon(struct channel *ch)
L
Linus Torvalds 已提交
320
{
321
	outb_p(FEPMEM, ch->board->port);
L
Linus Torvalds 已提交
322 323
}

324
static void pcxi_rxwinon(struct channel *ch)
L
Linus Torvalds 已提交
325
{
326
	outb_p(FEPMEM, ch->board->port);
L
Linus Torvalds 已提交
327 328
}

329
static void pcxi_txwinon(struct channel *ch)
L
Linus Torvalds 已提交
330
{
331
	outb_p(FEPMEM, ch->board->port);
L
Linus Torvalds 已提交
332 333
}

334
static void pcxi_memoff(struct channel *ch)
L
Linus Torvalds 已提交
335
{
336
	outb_p(0, ch->board->port);
L
Linus Torvalds 已提交
337 338
}

339
static void pcxi_assertgwinon(struct channel *ch)
L
Linus Torvalds 已提交
340
{
341
	epcaassert(inb(ch->board->port) & FEPMEM, "Global memory off");
L
Linus Torvalds 已提交
342 343
}

344
static void pcxi_assertmemoff(struct channel *ch)
L
Linus Torvalds 已提交
345
{
346
	epcaassert(!(inb(ch->board->port) & FEPMEM), "Memory on");
L
Linus Torvalds 已提交
347 348
}

349 350 351 352 353 354 355
/*
 * 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.
 */
356
static void dummy_memwinon(struct board_info *b, unsigned int win)
L
Linus Torvalds 已提交
357 358 359
{
}

360
static void dummy_memwinoff(struct board_info *b, unsigned int win)
L
Linus Torvalds 已提交
361 362 363
{
}

364
static void dummy_globalwinon(struct channel *ch)
L
Linus Torvalds 已提交
365 366 367
{
}

368
static void dummy_rxwinon(struct channel *ch)
L
Linus Torvalds 已提交
369 370 371
{
}

372
static void dummy_txwinon(struct channel *ch)
L
Linus Torvalds 已提交
373 374 375
{
}

376
static void dummy_memoff(struct channel *ch)
L
Linus Torvalds 已提交
377 378 379
{
}

380
static void dummy_assertgwinon(struct channel *ch)
L
Linus Torvalds 已提交
381 382 383
{
}

384
static void dummy_assertmemoff(struct channel *ch)
L
Linus Torvalds 已提交
385 386 387
{
}

388
static struct channel *verifyChannel(struct tty_struct *tty)
389 390 391 392 393 394 395
{
	/*
	 * 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.
	 */
396
	if (tty) {
A
Alan Cox 已提交
397
		struct channel *ch = tty->driver_data;
A
Alan Cox 已提交
398
		if (ch >= &digi_channels[0] && ch < &digi_channels[nbdevs]) {
L
Linus Torvalds 已提交
399 400 401
			if (ch->magic == EPCA_MAGIC)
				return ch;
		}
402
	}
L
Linus Torvalds 已提交
403
	return NULL;
404
}
L
Linus Torvalds 已提交
405

406 407
static void pc_sched_event(struct channel *ch, int event)
{
408 409 410 411
	/*
	 * We call this to schedule interrupt processing on some event. The
	 * kernel sees our request and calls the related routine in OUR driver.
	 */
L
Linus Torvalds 已提交
412 413
	ch->event |= 1 << event;
	schedule_work(&ch->tqueue);
414
}
L
Linus Torvalds 已提交
415 416

static void epca_error(int line, char *msg)
417
{
A
Alan Cox 已提交
418
	printk(KERN_ERR "epca_error (Digi): line = %d %s\n", line, msg);
419
}
L
Linus Torvalds 已提交
420

421
static void pc_close(struct tty_struct *tty, struct file *filp)
422
{
L
Linus Torvalds 已提交
423
	struct channel *ch;
A
Alan Cox 已提交
424
	struct tty_port *port;
L
Linus Torvalds 已提交
425
	unsigned long flags;
426 427 428 429
	/*
	 * verifyChannel returns the channel from the tty struct if it is
	 * valid. This serves as a sanity check.
	 */
A
Alan Cox 已提交
430
	ch = verifyChannel(tty);
A
Alan Cox 已提交
431 432 433
	if (ch == NULL)
		return;
	port = &ch->port;
L
Linus Torvalds 已提交
434

A
Alan Cox 已提交
435
	if (tty_port_close_start(port, tty, filp) == 0)
A
Alan Cox 已提交
436
		return;
L
Linus Torvalds 已提交
437

A
Alan Cox 已提交
438 439 440
	pc_flush_buffer(tty);
	shutdown(ch, tty);

A
Alan Cox 已提交
441 442
	tty_port_close_end(port, tty);
	ch->event = 0;	/* FIXME: review ch->event locking */
A
Alan Cox 已提交
443
	tty_port_tty_set(port, NULL);
444
}
L
Linus Torvalds 已提交
445

A
Alan Cox 已提交
446
static void shutdown(struct channel *ch, struct tty_struct *tty)
447
{
L
Linus Torvalds 已提交
448
	unsigned long flags;
449
	struct board_chan __iomem *bc;
A
Alan Cox 已提交
450
	struct tty_port *port = &ch->port;
L
Linus Torvalds 已提交
451

A
Alan Cox 已提交
452
	if (!(port->flags & ASYNC_INITIALIZED))
L
Linus Torvalds 已提交
453 454
		return;

455
	spin_lock_irqsave(&epca_lock, flags);
L
Linus Torvalds 已提交
456

457
	globalwinon(ch);
L
Linus Torvalds 已提交
458 459
	bc = ch->brdchan;

460 461 462 463 464
	/*
	 * 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.
	 */
L
Linus Torvalds 已提交
465
	if (bc)
466
		writeb(0, &bc->idata);
L
Linus Torvalds 已提交
467

468
	/* If we're a modem control device and HUPCL is on, drop RTS & DTR. */
469
	if (tty->termios->c_cflag & HUPCL)  {
L
Linus Torvalds 已提交
470 471 472 473 474
		ch->omodem &= ~(ch->m_rts | ch->m_dtr);
		fepcmd(ch, SETMODEM, 0, ch->m_dtr | ch->m_rts, 10, 1);
	}
	memoff(ch);

475 476 477 478
	/*
	 * The channel has officialy been closed. The next time it is opened it
	 * will have to reinitialized. Set a flag to indicate this.
	 */
L
Linus Torvalds 已提交
479
	/* Prevent future Digi programmed interrupts from coming active */
A
Alan Cox 已提交
480
	port->flags &= ~ASYNC_INITIALIZED;
481
	spin_unlock_irqrestore(&epca_lock, flags);
482
}
L
Linus Torvalds 已提交
483 484

static void pc_hangup(struct tty_struct *tty)
485
{
L
Linus Torvalds 已提交
486
	struct channel *ch;
A
Alan Cox 已提交
487

488 489 490 491
	/*
	 * verifyChannel returns the channel from the tty struct if it is
	 * valid. This serves as a sanity check.
	 */
A
Alan Cox 已提交
492 493
	ch = verifyChannel(tty);
	if (ch != NULL) {
L
Linus Torvalds 已提交
494 495
		unsigned long flags;

496
		pc_flush_buffer(tty);
L
Linus Torvalds 已提交
497
		tty_ldisc_flush(tty);
A
Alan Cox 已提交
498 499 500
		shutdown(ch, tty);

		ch->event = 0;	/* FIXME: review locking of ch->event */
A
Alan Cox 已提交
501
		tty_port_hangup(&ch->port);
502 503
	}
}
L
Linus Torvalds 已提交
504

505
static int pc_write(struct tty_struct *tty,
A
Alan Cox 已提交
506
			const unsigned char *buf, int bytesAvailable)
507
{
508 509 510 511
	unsigned int head, tail;
	int dataLen;
	int size;
	int amountCopied;
L
Linus Torvalds 已提交
512 513 514
	struct channel *ch;
	unsigned long flags;
	int remain;
515
	struct board_chan __iomem *bc;
L
Linus Torvalds 已提交
516

517 518 519 520 521 522 523 524
	/*
	 * 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.
	 */
L
Linus Torvalds 已提交
525

526 527 528 529
	/*
	 * verifyChannel returns the channel from the tty struct if it is
	 * valid. This serves as a sanity check.
	 */
A
Alan Cox 已提交
530 531
	ch = verifyChannel(tty);
	if (ch == NULL)
L
Linus Torvalds 已提交
532 533 534 535 536 537 538
		return 0;

	/* Make a pointer to the channel data structure found on the board. */
	bc   = ch->brdchan;
	size = ch->txbufsize;
	amountCopied = 0;

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

542 543
	head = readw(&bc->tin) & (size - 1);
	tail = readw(&bc->tout);
L
Linus Torvalds 已提交
544

545 546
	if (tail != readw(&bc->tout))
		tail = readw(&bc->tout);
L
Linus Torvalds 已提交
547 548
	tail &= (size - 1);

549 550 551 552 553 554 555 556 557 558
	if (head >= tail) {
		/* head has not wrapped */
		/*
		 * 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.
		 */
L
Linus Torvalds 已提交
559 560
		dataLen = size - head;
		remain = size - (head - tail) - 1;
561 562
	} else {
		/* head has wrapped around */
L
Linus Torvalds 已提交
563 564
		remain = tail - head - 1;
		dataLen = remain;
565 566 567 568 569
	}
	/*
	 * Check the space on the card. If we have more data than space; reduce
	 * the amount of data to fit the space.
	 */
L
Linus Torvalds 已提交
570 571
	bytesAvailable = min(remain, bytesAvailable);
	txwinon(ch);
572 573
	while (bytesAvailable > 0) {
		/* there is data to copy onto card */
L
Linus Torvalds 已提交
574

575 576 577 578
		/*
		 * If head is not wrapped, the below will make sure the first
		 * data copy fills to the end of card buffer.
		 */
L
Linus Torvalds 已提交
579
		dataLen = min(bytesAvailable, dataLen);
580
		memcpy_toio(ch->txptr + head, buf, dataLen);
L
Linus Torvalds 已提交
581 582 583 584 585
		buf += dataLen;
		head += dataLen;
		amountCopied += dataLen;
		bytesAvailable -= dataLen;

586
		if (head >= size) {
L
Linus Torvalds 已提交
587 588 589
			head = 0;
			dataLen = tail;
		}
590
	}
L
Linus Torvalds 已提交
591 592
	ch->statusflags |= TXBUSY;
	globalwinon(ch);
593
	writew(head, &bc->tin);
L
Linus Torvalds 已提交
594

595
	if ((ch->statusflags & LOWWAIT) == 0)  {
L
Linus Torvalds 已提交
596
		ch->statusflags |= LOWWAIT;
597
		writeb(1, &bc->ilow);
L
Linus Torvalds 已提交
598 599
	}
	memoff(ch);
600
	spin_unlock_irqrestore(&epca_lock, flags);
601 602
	return amountCopied;
}
L
Linus Torvalds 已提交
603 604

static int pc_write_room(struct tty_struct *tty)
605
{
A
Alan Cox 已提交
606
	int remain = 0;
L
Linus Torvalds 已提交
607 608 609
	struct channel *ch;
	unsigned long flags;
	unsigned int head, tail;
610
	struct board_chan __iomem *bc;
611 612 613 614
	/*
	 * verifyChannel returns the channel from the tty struct if it is
	 * valid. This serves as a sanity check.
	 */
A
Alan Cox 已提交
615 616
	ch = verifyChannel(tty);
	if (ch != NULL) {
617
		spin_lock_irqsave(&epca_lock, flags);
L
Linus Torvalds 已提交
618 619 620
		globalwinon(ch);

		bc   = ch->brdchan;
621 622
		head = readw(&bc->tin) & (ch->txbufsize - 1);
		tail = readw(&bc->tout);
L
Linus Torvalds 已提交
623

624 625
		if (tail != readw(&bc->tout))
			tail = readw(&bc->tout);
L
Linus Torvalds 已提交
626 627
		/* Wrap tail if necessary */
		tail &= (ch->txbufsize - 1);
A
Alan Cox 已提交
628 629
		remain = tail - head - 1;
		if (remain < 0)
L
Linus Torvalds 已提交
630 631
			remain += ch->txbufsize;

632
		if (remain && (ch->statusflags & LOWWAIT) == 0) {
L
Linus Torvalds 已提交
633
			ch->statusflags |= LOWWAIT;
634
			writeb(1, &bc->ilow);
L
Linus Torvalds 已提交
635 636
		}
		memoff(ch);
637
		spin_unlock_irqrestore(&epca_lock, flags);
L
Linus Torvalds 已提交
638 639 640
	}
	/* Return how much room is left on card */
	return remain;
641
}
L
Linus Torvalds 已提交
642 643

static int pc_chars_in_buffer(struct tty_struct *tty)
644
{
L
Linus Torvalds 已提交
645 646 647 648 649
	int chars;
	unsigned int ctail, head, tail;
	int remain;
	unsigned long flags;
	struct channel *ch;
650
	struct board_chan __iomem *bc;
651 652 653 654
	/*
	 * verifyChannel returns the channel from the tty struct if it is
	 * valid. This serves as a sanity check.
	 */
A
Alan Cox 已提交
655 656
	ch = verifyChannel(tty);
	if (ch == NULL)
657
		return 0;
L
Linus Torvalds 已提交
658

659
	spin_lock_irqsave(&epca_lock, flags);
L
Linus Torvalds 已提交
660 661 662
	globalwinon(ch);

	bc = ch->brdchan;
663 664 665
	tail = readw(&bc->tout);
	head = readw(&bc->tin);
	ctail = readw(&ch->mailbox->cout);
L
Linus Torvalds 已提交
666

A
Alan Cox 已提交
667 668
	if (tail == head && readw(&ch->mailbox->cin) == ctail &&
						readb(&bc->tbusy) == 0)
L
Linus Torvalds 已提交
669
		chars = 0;
670 671
	else  { /* Begin if some space on the card has been used */
		head = readw(&bc->tin) & (ch->txbufsize - 1);
L
Linus Torvalds 已提交
672
		tail &= (ch->txbufsize - 1);
673 674 675 676 677
		/*
		 * 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.
		 */
A
Alan Cox 已提交
678 679
		remain = tail - head - 1;
		if (remain < 0)
L
Linus Torvalds 已提交
680 681
			remain += ch->txbufsize;
		chars = (int)(ch->txbufsize - remain);
682 683 684 685 686 687 688
		/*
		 * 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.
		 */
L
Linus Torvalds 已提交
689
		if (!(ch->statusflags & EMPTYWAIT))
A
Alan Cox 已提交
690
			setup_empty_event(tty, ch);
L
Linus Torvalds 已提交
691 692
	} /* End if some space on the card has been used */
	memoff(ch);
693
	spin_unlock_irqrestore(&epca_lock, flags);
L
Linus Torvalds 已提交
694
	/* Return number of characters residing on card. */
695 696
	return chars;
}
L
Linus Torvalds 已提交
697 698

static void pc_flush_buffer(struct tty_struct *tty)
699
{
L
Linus Torvalds 已提交
700 701 702
	unsigned int tail;
	unsigned long flags;
	struct channel *ch;
703
	struct board_chan __iomem *bc;
704 705 706 707
	/*
	 * verifyChannel returns the channel from the tty struct if it is
	 * valid. This serves as a sanity check.
	 */
A
Alan Cox 已提交
708 709
	ch = verifyChannel(tty);
	if (ch == NULL)
L
Linus Torvalds 已提交
710 711
		return;

712
	spin_lock_irqsave(&epca_lock, flags);
L
Linus Torvalds 已提交
713 714
	globalwinon(ch);
	bc   = ch->brdchan;
715
	tail = readw(&bc->tout);
L
Linus Torvalds 已提交
716 717 718
	/* Have FEP move tout pointer; effectively flushing transmit buffer */
	fepcmd(ch, STOUT, (unsigned) tail, 0, 0, 0);
	memoff(ch);
719
	spin_unlock_irqrestore(&epca_lock, flags);
L
Linus Torvalds 已提交
720
	tty_wakeup(tty);
721
}
L
Linus Torvalds 已提交
722 723

static void pc_flush_chars(struct tty_struct *tty)
724 725 726 727 728 729
{
	struct channel *ch;
	/*
	 * verifyChannel returns the channel from the tty struct if it is
	 * valid. This serves as a sanity check.
	 */
A
Alan Cox 已提交
730 731
	ch = verifyChannel(tty);
	if (ch != NULL) {
L
Linus Torvalds 已提交
732
		unsigned long flags;
733
		spin_lock_irqsave(&epca_lock, flags);
734 735 736 737
		/*
		 * If not already set and the transmitter is busy setup an
		 * event to indicate when the transmit empties.
		 */
A
Alan Cox 已提交
738 739 740
		if ((ch->statusflags & TXBUSY) &&
				!(ch->statusflags & EMPTYWAIT))
			setup_empty_event(tty, ch);
741
		spin_unlock_irqrestore(&epca_lock, flags);
L
Linus Torvalds 已提交
742
	}
743
}
L
Linus Torvalds 已提交
744

A
Alan Cox 已提交
745
static int epca_carrier_raised(struct tty_port *port)
746
{
A
Alan Cox 已提交
747 748 749
	struct channel *ch = container_of(port, struct channel, port);
	if (ch->imodem & ch->dcd)
		return 1;
L
Linus Torvalds 已提交
750
	return 0;
751
}
L
Linus Torvalds 已提交
752

A
Alan Cox 已提交
753 754 755 756
static void epca_raise_dtr_rts(struct tty_port *port0
{
}

A
Alan Cox 已提交
757
static int pc_open(struct tty_struct *tty, struct file *filp)
758
{
L
Linus Torvalds 已提交
759
	struct channel *ch;
A
Alan Cox 已提交
760
	struct tty_port *port;
L
Linus Torvalds 已提交
761 762
	unsigned long flags;
	int line, retval, boardnum;
763
	struct board_chan __iomem *bc;
764
	unsigned int head;
L
Linus Torvalds 已提交
765 766

	line = tty->index;
767 768
	if (line < 0 || line >= nbdevs)
		return -ENODEV;
L
Linus Torvalds 已提交
769 770

	ch = &digi_channels[line];
A
Alan Cox 已提交
771
	port = &ch->port;
L
Linus Torvalds 已提交
772 773 774 775
	boardnum = ch->boardnum;

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

776 777 778 779 780
	/*
	 * 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.
	 */
781
	if (invalid_lilo_config) {
L
Linus Torvalds 已提交
782
		if (setup_error_code & INVALID_BOARD_TYPE)
783
			printk(KERN_ERR "epca: pc_open: Invalid board type specified in kernel options.\n");
L
Linus Torvalds 已提交
784
		if (setup_error_code & INVALID_NUM_PORTS)
785
			printk(KERN_ERR "epca: pc_open: Invalid number of ports specified in kernel options.\n");
L
Linus Torvalds 已提交
786
		if (setup_error_code & INVALID_MEM_BASE)
787
			printk(KERN_ERR "epca: pc_open: Invalid board memory address specified in kernel options.\n");
L
Linus Torvalds 已提交
788
		if (setup_error_code & INVALID_PORT_BASE)
789
			printk(KERN_ERR "epca; pc_open: Invalid board port address specified in kernel options.\n");
L
Linus Torvalds 已提交
790
		if (setup_error_code & INVALID_BOARD_STATUS)
791
			printk(KERN_ERR "epca: pc_open: Invalid board status specified in kernel options.\n");
L
Linus Torvalds 已提交
792
		if (setup_error_code & INVALID_ALTPIN)
793
			printk(KERN_ERR "epca: pc_open: Invalid board altpin specified in kernel options;\n");
L
Linus Torvalds 已提交
794
		tty->driver_data = NULL;   /* Mark this device as 'down' */
795
		return -ENODEV;
L
Linus Torvalds 已提交
796
	}
797
	if (boardnum >= num_cards || boards[boardnum].status == DISABLED)  {
L
Linus Torvalds 已提交
798 799 800
		tty->driver_data = NULL;   /* Mark this device as 'down' */
		return(-ENODEV);
	}
801

802 803
	bc = ch->brdchan;
	if (bc == NULL) {
L
Linus Torvalds 已提交
804
		tty->driver_data = NULL;
805
		return -ENODEV;
L
Linus Torvalds 已提交
806 807
	}

A
Alan Cox 已提交
808
	spin_lock_irqsave(&port->lock, flags);
809 810 811 812 813
	/*
	 * 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.
	 */
A
Alan Cox 已提交
814
	port->count++;
815 816 817 818
	/*
	 * Set a kernel structures pointer to our local channel structure. This
	 * way we can get to it when passed only a tty struct.
	 */
L
Linus Torvalds 已提交
819
	tty->driver_data = ch;
A
Alan Cox 已提交
820
	port->tty = tty;
821 822 823 824
	/*
	 * If this is the first time the channel has been opened, initialize
	 * the tty->termios struct otherwise let pc_close handle it.
	 */
A
Alan Cox 已提交
825
	spin_lock(&epca_lock);
L
Linus Torvalds 已提交
826 827 828 829
	globalwinon(ch);
	ch->statusflags = 0;

	/* Save boards current modem status */
830
	ch->imodem = readb(&bc->mstat);
L
Linus Torvalds 已提交
831

832 833 834 835
	/*
	 * Set receive head and tail ptrs to each other. This indicates no data
	 * available to read.
	 */
836 837
	head = readw(&bc->rin);
	writew(head, &bc->rout);
L
Linus Torvalds 已提交
838 839 840

	/* Set the channels associated tty structure */

841 842 843 844
	/*
	 * The below routine generally sets up parity, baud, flow control
	 * issues, etc.... It effect both control flags and input flags.
	 */
A
Alan Cox 已提交
845
	epcaparam(tty, ch);
L
Linus Torvalds 已提交
846
	memoff(ch);
A
Alan Cox 已提交
847 848 849
	spin_unlock(&epca_lock);
	port->flags |= ASYNC_INITIALIZED;
	spin_unlock_irqrestore(&port->lock, flags);
L
Linus Torvalds 已提交
850

A
Alan Cox 已提交
851
	retval = tty_port_block_til_ready(port, tty, filp);
L
Linus Torvalds 已提交
852 853
	if (retval)
		return retval;
854 855 856 857
	/*
	 * Set this again in case a hangup set it to zero while this open() was
	 * waiting for the line...
	 */
A
Alan Cox 已提交
858 859 860
	spin_lock_irqsave(&port->lock, flags);
	port->tty = tty;
	spin_lock(&epca_lock);
L
Linus Torvalds 已提交
861 862
	globalwinon(ch);
	/* Enable Digi Data events */
863
	writeb(1, &bc->idata);
L
Linus Torvalds 已提交
864
	memoff(ch);
A
Alan Cox 已提交
865 866
	spin_unlock(&epca_lock);
	spin_unlock_irqrestore(&port->lock, flags);
L
Linus Torvalds 已提交
867
	return 0;
868
}
L
Linus Torvalds 已提交
869 870

static int __init epca_module_init(void)
871
{
872
	return pc_init();
L
Linus Torvalds 已提交
873 874 875 876 877 878 879 880 881 882 883 884 885
}
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);

A
Alan Cox 已提交
886 887
	if (tty_unregister_driver(pc_driver) ||
				tty_unregister_driver(pc_info)) {
888
		printk(KERN_WARNING "epca: cleanup_module failed to un-register tty driver\n");
L
Linus Torvalds 已提交
889 890 891 892 893
		return;
	}
	put_tty_driver(pc_driver);
	put_tty_driver(pc_info);

894
	for (crd = 0; crd < num_cards; crd++) {
L
Linus Torvalds 已提交
895
		bd = &boards[crd];
896
		if (!bd) { /* sanity check */
L
Linus Torvalds 已提交
897 898
			printk(KERN_ERR "<Error> - Digi : cleanup_module failed\n");
			return;
899
		}
900
		ch = card_ptr[crd];
901
		for (count = 0; count < bd->numports; count++, ch++) {
A
Alan Cox 已提交
902 903 904 905 906
			struct tty_struct *tty = tty_port_tty_get(&ch->port);
			if (tty) {
				tty_hangup(tty);
				tty_kref_put(tty);
			}
907 908 909
		}
	}
	pci_unregister_driver(&epca_driver);
L
Linus Torvalds 已提交
910 911 912
}
module_exit(epca_module_exit);

J
Jeff Dike 已提交
913
static const struct tty_operations pc_ops = {
L
Linus Torvalds 已提交
914 915 916 917 918 919 920 921 922 923 924 925 926 927
	.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,
	.ioctl = pc_ioctl,
	.set_termios = pc_set_termios,
	.stop = pc_stop,
	.start = pc_start,
	.throttle = pc_throttle,
	.unthrottle = pc_unthrottle,
	.hangup = pc_hangup,
A
Alan Cox 已提交
928
	.break_ctl = pc_send_break
L
Linus Torvalds 已提交
929 930
};

A
Alan Cox 已提交
931 932 933 934 935
static const struct tty_port_operations epca_port_ops = {
	.carrier_raised = epca_carrier_raised,
	.raise_dtr_rts = epca_raise_dtr_rts,
};

A
Alan Cox 已提交
936
static int info_open(struct tty_struct *tty, struct file *filp)
L
Linus Torvalds 已提交
937 938 939 940 941 942 943 944 945
{
	return 0;
}

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

946
static int __init pc_init(void)
947
{
L
Linus Torvalds 已提交
948 949 950
	int crd;
	struct board_info *bd;
	unsigned char board_id = 0;
951
	int err = -ENOMEM;
L
Linus Torvalds 已提交
952 953 954 955 956 957 958

	int pci_boards_found, pci_count;

	pci_count = 0;

	pc_driver = alloc_tty_driver(MAX_ALLOC);
	if (!pc_driver)
959
		goto out1;
L
Linus Torvalds 已提交
960 961

	pc_info = alloc_tty_driver(MAX_ALLOC);
962 963
	if (!pc_info)
		goto out2;
L
Linus Torvalds 已提交
964

965 966 967 968 969 970 971 972 973
	/*
	 * 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.
	 */
	if (!liloconfig) {
		/* driver has been configured via. epcaconfig */
L
Linus Torvalds 已提交
974 975
		nbdevs = NBDEVS;
		num_cards = NUMCARDS;
976 977 978
		memcpy(&boards, &static_boards,
		       sizeof(struct board_info) * NUMCARDS);
	}
L
Linus Torvalds 已提交
979

980 981 982 983 984 985
	/*
	 * 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.
	 */
L
Linus Torvalds 已提交
986

987 988 989 990
	/*
	 * Set up interrupt, we will worry about memory allocation in
	 * post_fep_init.
	 */
A
Alan Cox 已提交
991
	printk(KERN_INFO "DIGI epca driver version %s loaded.\n", VERSION);
L
Linus Torvalds 已提交
992

993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007
	/*
	 * 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 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.)
	 */
L
Linus Torvalds 已提交
1008
	pci_boards_found = 0;
1009
	if (num_cards < MAXBOARDS)
L
Linus Torvalds 已提交
1010 1011 1012 1013
		pci_boards_found += init_PCI();
	num_cards += pci_boards_found;

	pc_driver->owner = THIS_MODULE;
1014 1015
	pc_driver->name = "ttyD";
	pc_driver->major = DIGI_MAJOR;
L
Linus Torvalds 已提交
1016 1017 1018 1019 1020 1021 1022 1023
	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;
A
Alan Cox 已提交
1024 1025
	pc_driver->init_termios.c_ispeed = 9600;
	pc_driver->init_termios.c_ospeed = 9600;
A
Alan Cox 已提交
1026
	pc_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_HARDWARE_BREAK;
L
Linus Torvalds 已提交
1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039
	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;
A
Alan Cox 已提交
1040 1041
	pc_info->init_termios.c_ispeed = 9600;
	pc_info->init_termios.c_ospeed = 9600;
L
Linus Torvalds 已提交
1042 1043 1044 1045
	pc_info->flags = TTY_DRIVER_REAL_RAW;
	tty_set_operations(pc_info, &info_ops);


1046 1047 1048 1049 1050 1051
	for (crd = 0; crd < num_cards; crd++) {
		/*
		 * This is where the appropriate memory handlers for the
		 * hardware is set. Everything at runtime blindly jumps through
		 * these vectors.
		 */
L
Linus Torvalds 已提交
1052 1053 1054 1055

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

1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066
		switch (bd->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;
L
Linus Torvalds 已提交
1067 1068
			break;

1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080
		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;
L
Linus Torvalds 已提交
1081

1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092
		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;
L
Linus Torvalds 已提交
1093

1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104
		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;
L
Linus Torvalds 已提交
1105

1106
		default:
L
Linus Torvalds 已提交
1107
			break;
1108
		}
L
Linus Torvalds 已提交
1109

1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122
		/*
		 * 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) {
		case PCXE:
		case PCXEVE:
		case PC64XE:
			bd->memory_seg = 0xf000;
			break;
L
Linus Torvalds 已提交
1123

1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143
		case PCXI:
			board_id = inb((int)bd->port);
			if ((board_id & 0x1) == 0x1) {
				/* 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;
			} else
A
Alan Cox 已提交
1144
				printk(KERN_ERR "epca: Board at 0x%x doesn't appear to be an XI\n", (int)bd->port);
1145 1146 1147
			break;
		}
	}
L
Linus Torvalds 已提交
1148

1149 1150 1151 1152 1153
	err = tty_register_driver(pc_driver);
	if (err) {
		printk(KERN_ERR "Couldn't register Digi PC/ driver");
		goto out3;
	}
L
Linus Torvalds 已提交
1154

1155 1156 1157 1158 1159
	err = tty_register_driver(pc_info);
	if (err) {
		printk(KERN_ERR "Couldn't register Digi PC/ info ");
		goto out4;
	}
L
Linus Torvalds 已提交
1160

1161
	/* Start up the poller to check for events on all enabled boards */
L
Linus Torvalds 已提交
1162 1163 1164 1165 1166
	init_timer(&epca_timer);
	epca_timer.function = epcapoll;
	mod_timer(&epca_timer, jiffies + HZ/25);
	return 0;

1167 1168 1169 1170 1171 1172 1173 1174
out4:
	tty_unregister_driver(pc_driver);
out3:
	put_tty_driver(pc_info);
out2:
	put_tty_driver(pc_driver);
out1:
	return err;
1175
}
L
Linus Torvalds 已提交
1176 1177

static void post_fep_init(unsigned int crd)
1178
{
L
Linus Torvalds 已提交
1179
	int i;
1180 1181
	void __iomem *memaddr;
	struct global_data __iomem *gd;
L
Linus Torvalds 已提交
1182
	struct board_info *bd;
1183
	struct board_chan __iomem *bc;
1184 1185
	struct channel *ch;
	int shrinkmem = 0, lowwater;
L
Linus Torvalds 已提交
1186

1187 1188 1189 1190 1191
	/*
	 * 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];
L
Linus Torvalds 已提交
1192

1193 1194 1195 1196 1197 1198 1199 1200
	/*
	 * 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.
	 */
1201
	if (bd->type >= PCIXEM) { /* Begin get PCI number of ports */
1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215
		/*
		 * 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)).
		 */
1216 1217
		/* PCI cards are already remapped at this point ISA are not */
		bd->numports = readw(bd->re_map_membase + XEMPORTS);
A
Alan Cox 已提交
1218
		epcaassert(bd->numports <= 64, "PCI returned a invalid number of ports");
L
Linus Torvalds 已提交
1219
		nbdevs += (bd->numports);
1220 1221 1222
	} else {
		/* Fix up the mappings for ISA/EISA etc */
		/* FIXME: 64K - can we be smarter ? */
A
Alan Cox 已提交
1223
		bd->re_map_membase = ioremap_nocache(bd->membase, 0x10000);
1224
	}
L
Linus Torvalds 已提交
1225 1226 1227 1228 1229 1230 1231 1232 1233

	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");

1234
	memaddr = bd->re_map_membase;
L
Linus Torvalds 已提交
1235

1236 1237 1238 1239 1240
	/*
	 * 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.
	 */
1241
	bc = memaddr + CHANSTRUCT;
L
Linus Torvalds 已提交
1242

1243 1244 1245 1246 1247
	/*
	 * 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.
	 */
1248
	gd = memaddr + GLOBAL;
L
Linus Torvalds 已提交
1249

1250 1251 1252 1253
	/*
	 * XEPORTS (address 0xc22) points at the number of channels the card
	 * supports. (For 64XE, XI, XEM, and XR use 0xc02)
	 */
A
Alan Cox 已提交
1254 1255
	if ((bd->type == PCXEVE || bd->type == PCXE) &&
					(readw(memaddr + XEPORTS) < 3))
L
Linus Torvalds 已提交
1256 1257 1258
		shrinkmem = 1;
	if (bd->type < PCIXEM)
		if (!request_region((int)bd->port, 4, board_desc[bd->type]))
1259
			return;
L
Linus Torvalds 已提交
1260 1261
	memwinon(bd, 0);

1262 1263 1264 1265 1266
	/*
	 * Remember ch is the main drivers channels structure, while bc is the
	 * cards channel structure.
	 */
	for (i = 0; i < bd->numports; i++, ch++, bc++) {
1267
		unsigned long flags;
1268
		u16 tseg, rseg;
L
Linus Torvalds 已提交
1269

A
Alan Cox 已提交
1270
		tty_port_init(&ch->port);
A
Alan Cox 已提交
1271
		ch->port.ops - &epca_port_ops;
1272 1273
		ch->brdchan = bc;
		ch->mailbox = gd;
D
David Howells 已提交
1274
		INIT_WORK(&ch->tqueue, do_softint);
1275
		ch->board = &boards[crd];
L
Linus Torvalds 已提交
1276

1277 1278
		spin_lock_irqsave(&epca_lock, flags);
		switch (bd->type) {
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
		/*
		 * 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;
		}
L
Linus Torvalds 已提交
1310

1311
		if (boards[crd].altpin) {
L
Linus Torvalds 已提交
1312 1313 1314
			ch->dsr = ch->m_dcd;
			ch->dcd = ch->m_dsr;
			ch->digiext.digi_flags |= DIGI_ALTPIN;
1315
		} else {
L
Linus Torvalds 已提交
1316 1317 1318
			ch->dcd = ch->m_dcd;
			ch->dsr = ch->m_dsr;
		}
1319

L
Linus Torvalds 已提交
1320 1321 1322
		ch->boardnum   = crd;
		ch->channelnum = i;
		ch->magic      = EPCA_MAGIC;
A
Alan Cox 已提交
1323
		tty_port_tty_set(&ch->port, NULL);
L
Linus Torvalds 已提交
1324

1325
		if (shrinkmem) {
L
Linus Torvalds 已提交
1326 1327 1328 1329
			fepcmd(ch, SETBUFFER, 32, 0, 0, 0);
			shrinkmem = 0;
		}

1330 1331 1332
		tseg = readw(&bc->tseg);
		rseg = readw(&bc->rseg);

1333
		switch (bd->type) {
1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352
		case PCIXEM:
		case PCIXRJ:
		case PCIXR:
			/* Cover all the 2MEG cards */
			ch->txptr = memaddr + ((tseg << 4) & 0x1fffff);
			ch->rxptr = memaddr + ((rseg << 4) & 0x1fffff);
			ch->txwin = FEPWIN | (tseg >> 11);
			ch->rxwin = FEPWIN | (rseg >> 11);
			break;

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

1354 1355
		case PCXEVE:
		case PCXE:
A
Alan Cox 已提交
1356 1357
			ch->txptr = memaddr + (((tseg - bd->memory_seg) << 4)
								& 0x1fff);
1358
			ch->txwin = FEPWIN | ((tseg - bd->memory_seg) >> 9);
A
Alan Cox 已提交
1359 1360 1361
			ch->rxptr = memaddr + (((rseg - bd->memory_seg) << 4)
								& 0x1fff);
			ch->rxwin = FEPWIN | ((rseg - bd->memory_seg) >> 9);
1362 1363 1364 1365 1366 1367 1368 1369 1370
			break;

		case PCXI:
		case PC64XE:
			ch->txptr = memaddr + ((tseg - bd->memory_seg) << 4);
			ch->rxptr = memaddr + ((rseg - bd->memory_seg) << 4);
			ch->txwin = ch->rxwin = 0;
			break;
		}
L
Linus Torvalds 已提交
1371 1372

		ch->txbufhead = 0;
1373
		ch->txbufsize = readw(&bc->tmax) + 1;
1374

L
Linus Torvalds 已提交
1375
		ch->rxbufhead = 0;
1376
		ch->rxbufsize = readw(&bc->rmax) + 1;
1377

L
Linus Torvalds 已提交
1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388
		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);

1389 1390
		writew(100, &bc->edelay);
		writeb(1, &bc->idata);
1391

1392 1393 1394 1395
		ch->startc  = readb(&bc->startc);
		ch->stopc   = readb(&bc->stopc);
		ch->startca = readb(&bc->startca);
		ch->stopca  = readb(&bc->stopca);
1396

L
Linus Torvalds 已提交
1397 1398 1399 1400 1401 1402 1403
		ch->fepcflag = 0;
		ch->fepiflag = 0;
		ch->fepoflag = 0;
		ch->fepstartc = 0;
		ch->fepstopc = 0;
		ch->fepstartca = 0;
		ch->fepstopca = 0;
1404

A
Alan Cox 已提交
1405
		ch->port.close_delay = 50;
1406 1407

		spin_unlock_irqrestore(&epca_lock, flags);
1408
	}
L
Linus Torvalds 已提交
1409

1410
	printk(KERN_INFO
A
Alan Cox 已提交
1411 1412 1413
	"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);
L
Linus Torvalds 已提交
1414
	memwinoff(bd, 0);
1415
}
L
Linus Torvalds 已提交
1416 1417

static void epcapoll(unsigned long ignored)
1418
{
L
Linus Torvalds 已提交
1419 1420
	unsigned long flags;
	int crd;
A
Alan Cox 已提交
1421
	unsigned int head, tail;
L
Linus Torvalds 已提交
1422 1423 1424
	struct channel *ch;
	struct board_info *bd;

1425 1426 1427 1428 1429 1430 1431 1432
	/*
	 * 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++) {
L
Linus Torvalds 已提交
1433 1434 1435 1436
		bd = &boards[crd];
		ch = card_ptr[crd];

		if ((bd->status == DISABLED) || digi_poller_inhibited)
1437
			continue;
L
Linus Torvalds 已提交
1438

1439 1440 1441 1442 1443
		/*
		 * 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.
		 */
1444 1445
		spin_lock_irqsave(&epca_lock, flags);

L
Linus Torvalds 已提交
1446 1447 1448 1449
		assertmemoff(ch);

		globalwinon(ch);

1450 1451 1452 1453
		/*
		 * In this case head and tail actually refer to the event queue
		 * not the transmit or receive queue.
		 */
1454 1455
		head = readw(&ch->mailbox->ein);
		tail = readw(&ch->mailbox->eout);
L
Linus Torvalds 已提交
1456

1457
		/* If head isn't equal to tail we have an event */
L
Linus Torvalds 已提交
1458 1459 1460 1461
		if (head != tail)
			doevent(crd);
		memoff(ch);

1462 1463
		spin_unlock_irqrestore(&epca_lock, flags);
	} /* End for each card */
L
Linus Torvalds 已提交
1464
	mod_timer(&epca_timer, jiffies + (HZ / 25));
1465
}
L
Linus Torvalds 已提交
1466 1467

static void doevent(int crd)
1468
{
1469
	void __iomem *eventbuf;
L
Linus Torvalds 已提交
1470 1471
	struct channel *ch, *chan0;
	static struct tty_struct *tty;
1472
	struct board_info *bd;
1473
	struct board_chan __iomem *bc;
1474 1475 1476
	unsigned int tail, head;
	int event, channel;
	int mstat, lstat;
L
Linus Torvalds 已提交
1477

1478 1479 1480 1481
	/*
	 * This subroutine is called by epcapoll when an event is detected
	 * in the event queue. This routine responds to those events.
	 */
L
Linus Torvalds 已提交
1482 1483 1484 1485 1486
	bd = &boards[crd];

	chan0 = card_ptr[crd];
	epcaassert(chan0 <= &digi_channels[nbdevs - 1], "ch out of range");
	assertgwinon(chan0);
A
Alan Cox 已提交
1487 1488 1489
	while ((tail = readw(&chan0->mailbox->eout)) !=
			(head = readw(&chan0->mailbox->ein))) {
		/* Begin while something in event queue */
L
Linus Torvalds 已提交
1490
		assertgwinon(chan0);
1491
		eventbuf = bd->re_map_membase + tail + ISTART;
L
Linus Torvalds 已提交
1492
		/* Get the channel the event occurred on */
1493
		channel = readb(eventbuf);
L
Linus Torvalds 已提交
1494
		/* Get the actual event code that occurred */
1495
		event = readb(eventbuf + 1);
1496 1497 1498 1499 1500 1501
		/*
		 * 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.
		 */
1502 1503
		mstat = readb(eventbuf + 2);
		lstat = readb(eventbuf + 3);
L
Linus Torvalds 已提交
1504 1505

		ch = chan0 + channel;
1506
		if ((unsigned)channel >= bd->numports || !ch)  {
L
Linus Torvalds 已提交
1507 1508 1509 1510 1511 1512
			if (channel >= bd->numports)
				ch = chan0;
			bc = ch->brdchan;
			goto next;
		}

A
Alan Cox 已提交
1513 1514
		bc = ch->brdchan;
		if (bc == NULL)
L
Linus Torvalds 已提交
1515 1516
			goto next;

A
Alan Cox 已提交
1517
		tty = tty_port_tty_get(&ch->port);
1518
		if (event & DATA_IND)  { /* Begin DATA_IND */
A
Alan Cox 已提交
1519
			receive_data(ch, tty);
L
Linus Torvalds 已提交
1520 1521 1522
			assertgwinon(ch);
		} /* End DATA_IND */
		/* else *//* Fix for DCD transition missed bug */
1523
		if (event & MODEMCHG_IND) {
L
Linus Torvalds 已提交
1524 1525
			/* A modem signal change has been indicated */
			ch->imodem = mstat;
A
Alan Cox 已提交
1526
			if (test_bit(ASYNC_CHECK_CD, &ch->port.flags)) {
A
Alan Cox 已提交
1527 1528
				/* We are now receiving dcd */
				if (mstat & ch->dcd)
A
Alan Cox 已提交
1529
					wake_up_interruptible(&ch->port.open_wait);
A
Alan Cox 已提交
1530 1531
				else	/* No dcd; hangup */
					pc_sched_event(ch, EPCA_EVENT_HANGUP);
L
Linus Torvalds 已提交
1532
			}
1533 1534 1535
		}
		if (tty) {
			if (event & BREAK_IND) {
L
Linus Torvalds 已提交
1536
				/* A break has been indicated */
A
Alan Cox 已提交
1537
				tty_insert_flip_char(tty, 0, TTY_BREAK);
1538 1539 1540
				tty_schedule_flip(tty);
			} else if (event & LOWTX_IND)  {
				if (ch->statusflags & LOWWAIT) {
L
Linus Torvalds 已提交
1541 1542
					ch->statusflags &= ~LOWWAIT;
					tty_wakeup(tty);
1543 1544
				}
			} else if (event & EMPTYTX_IND) {
A
Alan Cox 已提交
1545 1546
				/* This event is generated by
				   setup_empty_event */
L
Linus Torvalds 已提交
1547
				ch->statusflags &= ~TXBUSY;
1548
				if (ch->statusflags & EMPTYWAIT) {
L
Linus Torvalds 已提交
1549 1550
					ch->statusflags &= ~EMPTYWAIT;
					tty_wakeup(tty);
1551 1552
				}
			}
A
Alan Cox 已提交
1553
			tty_kref_put(tty);
1554
		}
A
Alan Cox 已提交
1555
next:
L
Linus Torvalds 已提交
1556
		globalwinon(ch);
1557 1558 1559
		BUG_ON(!bc);
		writew(1, &bc->idata);
		writew((tail + 4) & (IMAX - ISTART - 4), &chan0->mailbox->eout);
L
Linus Torvalds 已提交
1560 1561
		globalwinon(chan0);
	} /* End while something in event queue */
1562
}
L
Linus Torvalds 已提交
1563 1564

static void fepcmd(struct channel *ch, int cmd, int word_or_byte,
A
Alan Cox 已提交
1565
					int byte2, int ncmds, int bytecmd)
1566
{
1567
	unchar __iomem *memaddr;
L
Linus Torvalds 已提交
1568 1569 1570 1571 1572 1573 1574 1575 1576 1577
	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 */
1578
	head = readw(&ch->mailbox->cin);
L
Linus Torvalds 已提交
1579
	/* cmdStart is a base address */
1580
	cmdStart = readw(&ch->mailbox->cstart);
1581 1582 1583 1584 1585
	/*
	 * 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.
	 */
1586
	cmdMax = (cmdStart + 4 + readw(&ch->mailbox->cmax));
L
Linus Torvalds 已提交
1587 1588
	memaddr = ch->board->re_map_membase;

1589
	if (head >= (cmdMax - cmdStart) || (head & 03))  {
A
Alan Cox 已提交
1590 1591 1592 1593
		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 已提交
1594 1595
		return;
	}
1596 1597 1598
	if (bytecmd)  {
		writeb(cmd, memaddr + head + cmdStart + 0);
		writeb(ch->channelnum,  memaddr + head + cmdStart + 1);
L
Linus Torvalds 已提交
1599
		/* Below word_or_byte is bits to set */
1600
		writeb(word_or_byte,  memaddr + head + cmdStart + 2);
L
Linus Torvalds 已提交
1601
		/* Below byte2 is bits to reset */
1602 1603 1604 1605 1606
		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 已提交
1607 1608
	}
	head = (head + 4) & (cmdMax - cmdStart - 4);
1609
	writew(head, &ch->mailbox->cin);
L
Linus Torvalds 已提交
1610 1611
	count = FEPTIMEOUT;

1612
	for (;;) {
L
Linus Torvalds 已提交
1613
		count--;
1614
		if (count == 0)  {
L
Linus Torvalds 已提交
1615 1616 1617
			printk(KERN_ERR "<Error> - Fep not responding in fepcmd()\n");
			return;
		}
1618 1619
		head = readw(&ch->mailbox->cin);
		cmdTail = readw(&ch->mailbox->cout);
L
Linus Torvalds 已提交
1620
		n = (head - cmdTail) & (cmdMax - cmdStart - 4);
1621 1622 1623 1624
		/*
		 * Basically this will break when the FEP acknowledges the
		 * command by incrementing cmdTail (Making it equal to head).
		 */
L
Linus Torvalds 已提交
1625
		if (n <= ncmds * (sizeof(short) * 4))
1626 1627 1628
			break;
	}
}
L
Linus Torvalds 已提交
1629

1630 1631 1632 1633 1634 1635
/*
 * 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.
 */
L
Linus Torvalds 已提交
1636
static unsigned termios2digi_h(struct channel *ch, unsigned cflag)
1637
{
L
Linus Torvalds 已提交
1638 1639
	unsigned res = 0;

1640
	if (cflag & CRTSCTS) {
L
Linus Torvalds 已提交
1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666
		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;
1667
}
L
Linus Torvalds 已提交
1668 1669

static unsigned termios2digi_i(struct channel *ch, unsigned iflag)
1670 1671
{
	unsigned res = iflag & (IGNBRK | BRKINT | IGNPAR | PARMRK |
A
Alan Cox 已提交
1672
					INPCK | ISTRIP | IXON | IXANY | IXOFF);
L
Linus Torvalds 已提交
1673 1674 1675
	if (ch->digiext.digi_flags & DIGI_AIXON)
		res |= IAIXON;
	return res;
1676
}
L
Linus Torvalds 已提交
1677 1678

static unsigned termios2digi_c(struct channel *ch, unsigned cflag)
1679
{
L
Linus Torvalds 已提交
1680
	unsigned res = 0;
1681
	if (cflag & CBAUDEX) {
L
Linus Torvalds 已提交
1682
		ch->digiext.digi_flags |= DIGI_FAST;
1683 1684 1685 1686
		/*
		 * HUPCL bit is used by FEP to indicate fast baud table is to
		 * be used.
		 */
L
Linus Torvalds 已提交
1687
		res |= FEP_HUPCL;
1688 1689 1690 1691 1692 1693 1694 1695
	} 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.
	 */
L
Linus Torvalds 已提交
1696
	res |= cflag & ((CBAUD ^ CBAUDEX) | PARODD | PARENB | CSTOPB | CSIZE);
1697 1698
	/*
	 * This gets a little confusing. The Digi cards have their own
J
Joe Perches 已提交
1699
	 * representation of c_cflags controlling baud rate. For the most part
1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716
	 * 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.
	 */
1717
	if (cflag & CBAUDEX) {
1718 1719 1720 1721 1722 1723 1724
		/*
		 * 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))) ||
L
Linus Torvalds 已提交
1725 1726 1727 1728
		    (!((cflag & 0x7) ^ (B230400 & ~CBAUDEX))))
			res += 1;
	}
	return res;
1729
}
L
Linus Torvalds 已提交
1730

1731
/* Caller must hold the locks */
L
Linus Torvalds 已提交
1732
static void epcaparam(struct tty_struct *tty, struct channel *ch)
1733
{
L
Linus Torvalds 已提交
1734
	unsigned int cmdHead;
A
Alan Cox 已提交
1735
	struct ktermios *ts;
1736
	struct board_chan __iomem *bc;
L
Linus Torvalds 已提交
1737 1738 1739
	unsigned mval, hflow, cflag, iflag;

	bc = ch->brdchan;
1740
	epcaassert(bc != NULL, "bc out of range");
L
Linus Torvalds 已提交
1741 1742 1743

	assertgwinon(ch);
	ts = tty->termios;
1744 1745
	if ((ts->c_cflag & CBAUD) == 0)  { /* Begin CBAUD detected */
		cmdHead = readw(&bc->rin);
1746
		writew(cmdHead, &bc->rout);
1747
		cmdHead = readw(&bc->tin);
L
Linus Torvalds 已提交
1748
		/* Changing baud in mid-stream transmission can be wonderful */
1749 1750 1751 1752 1753
		/*
		 * Flush current transmit buffer by setting cmdTail pointer
		 * (tout) to cmdHead pointer (tin). Hopefully the transmit
		 * buffer is empty.
		 */
L
Linus Torvalds 已提交
1754 1755
		fepcmd(ch, STOUT, (unsigned) cmdHead, 0, 0, 0);
		mval = 0;
1756 1757 1758 1759 1760
	} else { /* Begin CBAUD not detected */
		/*
		 * c_cflags have changed but that change had nothing to do with
		 * BAUD. Propagate the change to the card.
		 */
L
Linus Torvalds 已提交
1761
		cflag = termios2digi_c(ch, ts->c_cflag);
1762
		if (cflag != ch->fepcflag)  {
L
Linus Torvalds 已提交
1763 1764 1765 1766
			ch->fepcflag = cflag;
			/* Set baud rate, char size, stop bits, parity */
			fepcmd(ch, SETCTRLFLAGS, (unsigned) cflag, 0, 0, 0);
		}
1767 1768 1769 1770 1771
		/*
		 * 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.
		 */
L
Linus Torvalds 已提交
1772
		if (ts->c_cflag & CLOCAL)
A
Alan Cox 已提交
1773
			clear_bit(ASYNC_CHECK_CD, &ch->port.flags);
L
Linus Torvalds 已提交
1774
		else
A
Alan Cox 已提交
1775
			set_bit(ASYNC_CHECK_CD, &ch->port.flags);
L
Linus Torvalds 已提交
1776 1777 1778 1779
		mval = ch->m_dtr | ch->m_rts;
	} /* End CBAUD not detected */
	iflag = termios2digi_i(ch, ts->c_iflag);
	/* Check input mode flags */
1780
	if (iflag != ch->fepiflag)  {
L
Linus Torvalds 已提交
1781
		ch->fepiflag = iflag;
1782 1783 1784 1785
		/*
		 * 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.
A
Alan Cox 已提交
1786 1787 1788
		 *
		 * break handling, parity handling, input stripping,
		 * flow control chars
1789
		 */
L
Linus Torvalds 已提交
1790 1791
		fepcmd(ch, SETIFLAGS, (unsigned int) ch->fepiflag, 0, 0, 0);
	}
1792 1793 1794 1795 1796
	/*
	 * 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.
	 */
1797
	writeb(ch->dcd, &bc->mint);
L
Linus Torvalds 已提交
1798 1799
	if ((ts->c_cflag & CLOCAL) || (ch->digiext.digi_flags & DIGI_FORCEDCD))
		if (ch->digiext.digi_flags & DIGI_FORCEDCD)
1800 1801
			writeb(0, &bc->mint);
	ch->imodem = readb(&bc->mstat);
L
Linus Torvalds 已提交
1802
	hflow = termios2digi_h(ch, ts->c_cflag);
1803
	if (hflow != ch->hflow)  {
L
Linus Torvalds 已提交
1804
		ch->hflow = hflow;
1805 1806 1807 1808
		/*
		 * Hard flow control has been selected but the board is not
		 * using it. Activate hard flow control now.
		 */
L
Linus Torvalds 已提交
1809 1810 1811 1812
		fepcmd(ch, SETHFLOW, hflow, 0xff, 0, 1);
	}
	mval ^= ch->modemfake & (mval ^ ch->modem);

1813
	if (ch->omodem ^ mval)  {
L
Linus Torvalds 已提交
1814
		ch->omodem = mval;
1815 1816 1817 1818 1819 1820 1821
		/*
		 * 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.
		 */
L
Linus Torvalds 已提交
1822 1823 1824 1825 1826

		/* 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);
	}
1827
	if (ch->startc != ch->fepstartc || ch->stopc != ch->fepstopc)  {
L
Linus Torvalds 已提交
1828 1829
		ch->fepstartc = ch->startc;
		ch->fepstopc = ch->stopc;
1830 1831 1832 1833
		/*
		 * The XON / XOFF characters have changed; propagate these
		 * changes to the card.
		 */
L
Linus Torvalds 已提交
1834 1835
		fepcmd(ch, SONOFFC, ch->fepstartc, ch->fepstopc, 0, 1);
	}
1836
	if (ch->startca != ch->fepstartca || ch->stopca != ch->fepstopca)  {
L
Linus Torvalds 已提交
1837 1838
		ch->fepstartca = ch->startca;
		ch->fepstopca = ch->stopca;
1839 1840 1841 1842
		/*
		 * Similar to the above, this time the auxilarly XON / XOFF
		 * characters have changed; propagate these changes to the card.
		 */
L
Linus Torvalds 已提交
1843 1844
		fepcmd(ch, SAUXONOFFC, ch->fepstartca, ch->fepstopca, 0, 1);
	}
1845
}
L
Linus Torvalds 已提交
1846

1847
/* Caller holds lock */
A
Alan Cox 已提交
1848
static void receive_data(struct channel *ch, struct tty_struct *tty)
1849
{
L
Linus Torvalds 已提交
1850
	unchar *rptr;
A
Alan Cox 已提交
1851
	struct ktermios *ts = NULL;
1852
	struct board_chan __iomem *bc;
1853 1854
	int dataToRead, wrapgap, bytesAvailable;
	unsigned int tail, head;
L
Linus Torvalds 已提交
1855 1856
	unsigned int wrapmask;

1857 1858 1859 1860
	/*
	 * This routine is called by doint when a receive data event has taken
	 * place.
	 */
L
Linus Torvalds 已提交
1861 1862 1863 1864 1865 1866
	globalwinon(ch);
	if (ch->statusflags & RXSTOPPED)
		return;
	if (tty)
		ts = tty->termios;
	bc = ch->brdchan;
1867
	BUG_ON(!bc);
L
Linus Torvalds 已提交
1868 1869
	wrapmask = ch->rxbufsize - 1;

1870 1871 1872 1873
	/*
	 * Get the head and tail pointers to the receiver queue. Wrap the head
	 * pointer if it has reached the end of the buffer.
	 */
1874
	head = readw(&bc->rin);
L
Linus Torvalds 已提交
1875
	head &= wrapmask;
1876
	tail = readw(&bc->rout) & wrapmask;
L
Linus Torvalds 已提交
1877 1878 1879 1880 1881

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

1882
	/* If CREAD bit is off or device not open, set TX tail to head */
A
Alan Cox 已提交
1883
	if (!tty || !ts || !(ts->c_cflag & CREAD)) {
1884
		writew(head, &bc->rout);
L
Linus Torvalds 已提交
1885 1886 1887
		return;
	}

A
Alan Cox 已提交
1888
	if (tty_buffer_request_room(tty, bytesAvailable + 1) == 0)
L
Linus Torvalds 已提交
1889 1890
		return;

1891 1892
	if (readb(&bc->orun)) {
		writeb(0, &bc->orun);
A
Alan Cox 已提交
1893 1894
		printk(KERN_WARNING "epca; overrun! DigiBoard device %s\n",
								tty->name);
A
Alan Cox 已提交
1895
		tty_insert_flip_char(tty, 0, TTY_OVERRUN);
L
Linus Torvalds 已提交
1896 1897
	}
	rxwinon(ch);
A
Alan Cox 已提交
1898 1899
	while (bytesAvailable > 0) {
		/* Begin while there is data on the card */
L
Linus Torvalds 已提交
1900
		wrapgap = (head >= tail) ? head - tail : ch->rxbufsize - tail;
1901 1902 1903 1904 1905
		/*
		 * 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.
		 */
A
Alan Cox 已提交
1906 1907
		dataToRead = (wrapgap < bytesAvailable) ? wrapgap
							: bytesAvailable;
1908
		/* Make sure we don't overflow the buffer */
A
Alan Cox 已提交
1909
		dataToRead = tty_prepare_flip_string(tty, &rptr, dataToRead);
L
Linus Torvalds 已提交
1910 1911
		if (dataToRead == 0)
			break;
1912 1913 1914 1915
		/*
		 * Move data read from our card into the line disciplines
		 * buffer for translation if necessary.
		 */
1916
		memcpy_fromio(rptr, ch->rxptr + tail, dataToRead);
L
Linus Torvalds 已提交
1917 1918 1919 1920
		tail = (tail + dataToRead) & wrapmask;
		bytesAvailable -= dataToRead;
	} /* End while there is data on the card */
	globalwinon(ch);
1921
	writew(tail, &bc->rout);
L
Linus Torvalds 已提交
1922
	/* Must be called with global data */
A
Alan Cox 已提交
1923
	tty_schedule_flip(tty);
1924
}
L
Linus Torvalds 已提交
1925

1926
static int info_ioctl(struct tty_struct *tty, struct file *file,
L
Linus Torvalds 已提交
1927 1928
		    unsigned int cmd, unsigned long arg)
{
1929 1930 1931 1932
	switch (cmd) {
	case DIGI_GETINFO:
		{
			struct digi_info di;
L
Linus Torvalds 已提交
1933 1934
			int brd;

1935
			if (get_user(brd, (unsigned int __user *)arg))
1936 1937 1938
				return -EFAULT;
			if (brd < 0 || brd >= num_cards || num_cards == 0)
				return -ENODEV;
L
Linus Torvalds 已提交
1939 1940 1941

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

1942
			di.board = brd;
L
Linus Torvalds 已提交
1943 1944 1945
			di.status = boards[brd].status;
			di.type = boards[brd].type ;
			di.numports = boards[brd].numports ;
1946 1947 1948
			/* 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 已提交
1949

1950
			if (copy_to_user((void __user *)arg, &di, sizeof(di)))
L
Linus Torvalds 已提交
1951 1952 1953
				return -EFAULT;
			break;

1954
		}
L
Linus Torvalds 已提交
1955

1956 1957 1958 1959
	case DIGI_POLLER:
		{
			int brd = arg & 0xff000000 >> 16;
			unsigned char state = arg & 0xff;
L
Linus Torvalds 已提交
1960

1961 1962
			if (brd < 0 || brd >= num_cards) {
				printk(KERN_ERR "epca: DIGI POLLER : brd not valid!\n");
1963
				return -ENODEV;
L
Linus Torvalds 已提交
1964
			}
1965 1966 1967 1968 1969 1970 1971 1972
			digi_poller_inhibited = state;
			break;
		}

	case DIGI_INIT:
		{
			/*
			 * This call is made by the apps to complete the
J
Joe Perches 已提交
1973
			 * initialization of the board(s). This routine is
1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986
			 * 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;
		}
	default:
		return -ENOTTY;
	}
	return 0;
L
Linus Torvalds 已提交
1987 1988 1989 1990
}

static int pc_tiocmget(struct tty_struct *tty, struct file *file)
{
A
Alan Cox 已提交
1991
	struct channel *ch = tty->driver_data;
1992
	struct board_chan __iomem *bc;
L
Linus Torvalds 已提交
1993 1994 1995 1996 1997 1998
	unsigned int mstat, mflag = 0;
	unsigned long flags;

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

2001
	spin_lock_irqsave(&epca_lock, flags);
L
Linus Torvalds 已提交
2002
	globalwinon(ch);
2003
	mstat = readb(&bc->mstat);
L
Linus Torvalds 已提交
2004
	memoff(ch);
2005
	spin_unlock_irqrestore(&epca_lock, flags);
L
Linus Torvalds 已提交
2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024

	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)
{
A
Alan Cox 已提交
2025
	struct channel *ch = tty->driver_data;
L
Linus Torvalds 已提交
2026 2027
	unsigned long flags;

2028 2029
	if (!ch)
		return -EINVAL;
L
Linus Torvalds 已提交
2030

2031
	spin_lock_irqsave(&epca_lock, flags);
L
Linus Torvalds 已提交
2032
	/*
2033 2034 2035
	 * I think this modemfake stuff is broken. It doesn't correctly reflect
	 * the behaviour desired by the TIOCM* ioctls. Therefore this is
	 * probably broken.
L
Linus Torvalds 已提交
2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053
	 */
	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);
2054 2055 2056 2057
	/*
	 * The below routine generally sets up parity, baud, flow control
	 * issues, etc.... It effect both control flags and input flags.
	 */
A
Alan Cox 已提交
2058
	epcaparam(tty, ch);
L
Linus Torvalds 已提交
2059
	memoff(ch);
2060
	spin_unlock_irqrestore(&epca_lock, flags);
L
Linus Torvalds 已提交
2061 2062 2063
	return 0;
}

A
Alan Cox 已提交
2064 2065
static int pc_ioctl(struct tty_struct *tty, struct file *file,
					unsigned int cmd, unsigned long arg)
2066
{
L
Linus Torvalds 已提交
2067 2068 2069 2070
	digiflow_t dflow;
	unsigned long flags;
	unsigned int mflag, mstat;
	unsigned char startc, stopc;
2071
	struct board_chan __iomem *bc;
A
Alan Cox 已提交
2072
	struct channel *ch = tty->driver_data;
L
Linus Torvalds 已提交
2073
	void __user *argp = (void __user *)arg;
2074

L
Linus Torvalds 已提交
2075 2076
	if (ch)
		bc = ch->brdchan;
2077
	else
2078
		return -EINVAL;
2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096
	switch (cmd) {
	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:
		spin_lock_irqsave(&epca_lock, flags);
		ch->omodem |= ch->m_dtr;
		globalwinon(ch);
		fepcmd(ch, SETMODEM, ch->m_dtr, 0, 10, 1);
		memoff(ch);
		spin_unlock_irqrestore(&epca_lock, flags);
		break;
L
Linus Torvalds 已提交
2097

2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111
	case TIOCCDTR:
		spin_lock_irqsave(&epca_lock, flags);
		ch->omodem &= ~ch->m_dtr;
		globalwinon(ch);
		fepcmd(ch, SETMODEM, 0, ch->m_dtr, 10, 1);
		memoff(ch);
		spin_unlock_irqrestore(&epca_lock, flags);
		break;
	case DIGI_GETA:
		if (copy_to_user(argp, &ch->digiext, sizeof(digi_t)))
			return -EFAULT;
		break;
	case DIGI_SETAW:
	case DIGI_SETAF:
A
Alan Cox 已提交
2112
		lock_kernel();
2113
		if (cmd == DIGI_SETAW) {
A
Alan Cox 已提交
2114 2115
			/* Setup an event to indicate when the transmit
			   buffer empties */
2116
			spin_lock_irqsave(&epca_lock, flags);
A
Alan Cox 已提交
2117
			setup_empty_event(tty, ch);
2118
			spin_unlock_irqrestore(&epca_lock, flags);
2119 2120 2121
			tty_wait_until_sent(tty, 0);
		} else {
			/* ldisc lock already held in ioctl */
A
Alan Cox 已提交
2122 2123
			if (tty->ldisc.ops->flush_buffer)
				tty->ldisc.ops->flush_buffer(tty);
2124
		}
A
Alan Cox 已提交
2125
		unlock_kernel();
2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136
		/* Fall Thru */
	case DIGI_SETA:
		if (copy_from_user(&ch->digiext, argp, sizeof(digi_t)))
			return -EFAULT;

		if (ch->digiext.digi_flags & DIGI_ALTPIN)  {
			ch->dcd = ch->m_dsr;
			ch->dsr = ch->m_dcd;
		} else {
			ch->dcd = ch->m_dcd;
			ch->dsr = ch->m_dsr;
L
Linus Torvalds 已提交
2137 2138
			}

2139 2140
		spin_lock_irqsave(&epca_lock, flags);
		globalwinon(ch);
L
Linus Torvalds 已提交
2141

2142 2143 2144 2145 2146
		/*
		 * The below routine generally sets up parity, baud, flow
		 * control issues, etc.... It effect both control flags and
		 * input flags.
		 */
A
Alan Cox 已提交
2147
		epcaparam(tty, ch);
2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178
		memoff(ch);
		spin_unlock_irqrestore(&epca_lock, flags);
		break;

	case DIGI_GETFLOW:
	case DIGI_GETAFLOW:
		spin_lock_irqsave(&epca_lock, flags);
		globalwinon(ch);
		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);
		}
		memoff(ch);
		spin_unlock_irqrestore(&epca_lock, flags);

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

	case DIGI_SETAFLOW:
	case DIGI_SETFLOW:
		if (cmd == DIGI_SETFLOW) {
			startc = ch->startc;
			stopc = ch->stopc;
		} else {
			startc = ch->startca;
			stopc = ch->stopca;
		}
L
Linus Torvalds 已提交
2179

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

A
Alan Cox 已提交
2183 2184
		if (dflow.startc != startc || dflow.stopc != stopc) {
			/* Begin  if setflow toggled */
2185
			spin_lock_irqsave(&epca_lock, flags);
L
Linus Torvalds 已提交
2186 2187
			globalwinon(ch);

2188
			if (cmd == DIGI_SETFLOW) {
2189 2190
				ch->fepstartc = ch->startc = dflow.startc;
				ch->fepstopc = ch->stopc = dflow.stopc;
A
Alan Cox 已提交
2191 2192
				fepcmd(ch, SONOFFC, ch->fepstartc,
						ch->fepstopc, 0, 1);
2193
			} else {
2194 2195
				ch->fepstartca = ch->startca = dflow.startc;
				ch->fepstopca  = ch->stopca = dflow.stopc;
A
Alan Cox 已提交
2196 2197
				fepcmd(ch, SAUXONOFFC, ch->fepstartca,
						ch->fepstopca, 0, 1);
L
Linus Torvalds 已提交
2198 2199
			}

2200 2201
			if (ch->statusflags & TXSTOPPED)
				pc_start(tty);
L
Linus Torvalds 已提交
2202

2203 2204 2205 2206 2207 2208 2209
			memoff(ch);
			spin_unlock_irqrestore(&epca_lock, flags);
		} /* End if setflow toggled */
		break;
	default:
		return -ENOIOCTLCMD;
	}
L
Linus Torvalds 已提交
2210
	return 0;
2211
}
L
Linus Torvalds 已提交
2212

A
Alan Cox 已提交
2213
static void pc_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
2214
{
L
Linus Torvalds 已提交
2215 2216
	struct channel *ch;
	unsigned long flags;
2217 2218 2219 2220
	/*
	 * verifyChannel returns the channel from the tty struct if it is
	 * valid. This serves as a sanity check.
	 */
A
Alan Cox 已提交
2221 2222 2223
	ch = verifyChannel(tty);

	if (ch != NULL)  { /* Begin if channel valid */
2224
		spin_lock_irqsave(&epca_lock, flags);
L
Linus Torvalds 已提交
2225 2226 2227
		globalwinon(ch);
		epcaparam(tty, ch);
		memoff(ch);
2228
		spin_unlock_irqrestore(&epca_lock, flags);
L
Linus Torvalds 已提交
2229 2230 2231 2232 2233 2234 2235

		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))
A
Alan Cox 已提交
2236
			wake_up_interruptible(&ch->port.open_wait);
L
Linus Torvalds 已提交
2237 2238

	} /* End if channel valid */
2239
}
L
Linus Torvalds 已提交
2240

D
David Howells 已提交
2241
static void do_softint(struct work_struct *work)
2242
{
D
David Howells 已提交
2243
	struct channel *ch = container_of(work, struct channel, tqueue);
L
Linus Torvalds 已提交
2244
	/* Called in response to a modem change event */
2245
	if (ch && ch->magic == EPCA_MAGIC) {
A
Alan Cox 已提交
2246
		struct tty_struct *tty = tty_port_tty_get(&ch->port);;
L
Linus Torvalds 已提交
2247

2248
		if (tty && tty->driver_data) {
2249
			if (test_and_clear_bit(EPCA_EVENT_HANGUP, &ch->event)) {
A
Alan Cox 已提交
2250
				tty_hangup(tty);
A
Alan Cox 已提交
2251
				wake_up_interruptible(&ch->port.open_wait);
A
Alan Cox 已提交
2252
				clear_bit(ASYNC_NORMAL_ACTIVE, &ch->port.flags);
2253
			}
L
Linus Torvalds 已提交
2254
		}
A
Alan Cox 已提交
2255
		tty_kref_put(tty);
2256 2257
	}
}
L
Linus Torvalds 已提交
2258

2259 2260 2261 2262
/*
 * pc_stop and pc_start provide software flow control to the routine and the
 * pc_ioctl routine.
 */
L
Linus Torvalds 已提交
2263
static void pc_stop(struct tty_struct *tty)
2264
{
L
Linus Torvalds 已提交
2265 2266
	struct channel *ch;
	unsigned long flags;
2267 2268 2269 2270
	/*
	 * verifyChannel returns the channel from the tty struct if it is
	 * valid. This serves as a sanity check.
	 */
A
Alan Cox 已提交
2271 2272
	ch = verifyChannel(tty);
	if (ch != NULL) {
2273
		spin_lock_irqsave(&epca_lock, flags);
A
Alan Cox 已提交
2274 2275
		if ((ch->statusflags & TXSTOPPED) == 0) {
			/* Begin if transmit stop requested */
L
Linus Torvalds 已提交
2276 2277 2278 2279 2280 2281
			globalwinon(ch);
			/* STOP transmitting now !! */
			fepcmd(ch, PAUSETX, 0, 0, 0, 0);
			ch->statusflags |= TXSTOPPED;
			memoff(ch);
		} /* End if transmit stop requested */
2282
		spin_unlock_irqrestore(&epca_lock, flags);
2283 2284
	}
}
L
Linus Torvalds 已提交
2285 2286

static void pc_start(struct tty_struct *tty)
2287
{
L
Linus Torvalds 已提交
2288
	struct channel *ch;
2289 2290 2291 2292
	/*
	 * verifyChannel returns the channel from the tty struct if it is
	 * valid. This serves as a sanity check.
	 */
A
Alan Cox 已提交
2293 2294
	ch = verifyChannel(tty);
	if (ch != NULL) {
L
Linus Torvalds 已提交
2295
		unsigned long flags;
2296
		spin_lock_irqsave(&epca_lock, flags);
A
Alan Cox 已提交
2297 2298 2299 2300
		/* Just in case output was resumed because of a change
		   in Digi-flow */
		if (ch->statusflags & TXSTOPPED)  {
			/* Begin transmit resume requested */
2301
			struct board_chan __iomem *bc;
L
Linus Torvalds 已提交
2302 2303 2304
			globalwinon(ch);
			bc = ch->brdchan;
			if (ch->statusflags & LOWWAIT)
2305
				writeb(1, &bc->ilow);
L
Linus Torvalds 已提交
2306 2307 2308 2309 2310
			/* Okay, you can start transmitting again... */
			fepcmd(ch, RESUMETX, 0, 0, 0, 0);
			ch->statusflags &= ~TXSTOPPED;
			memoff(ch);
		} /* End transmit resume requested */
2311
		spin_unlock_irqrestore(&epca_lock, flags);
2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323
	}
}

/*
 * 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.
 */
static void pc_throttle(struct tty_struct *tty)
{
L
Linus Torvalds 已提交
2324 2325
	struct channel *ch;
	unsigned long flags;
2326 2327 2328 2329
	/*
	 * verifyChannel returns the channel from the tty struct if it is
	 * valid. This serves as a sanity check.
	 */
A
Alan Cox 已提交
2330 2331
	ch = verifyChannel(tty);
	if (ch != NULL) {
2332 2333
		spin_lock_irqsave(&epca_lock, flags);
		if ((ch->statusflags & RXSTOPPED) == 0) {
L
Linus Torvalds 已提交
2334 2335 2336 2337 2338
			globalwinon(ch);
			fepcmd(ch, PAUSERX, 0, 0, 0, 0);
			ch->statusflags |= RXSTOPPED;
			memoff(ch);
		}
2339
		spin_unlock_irqrestore(&epca_lock, flags);
2340 2341
	}
}
L
Linus Torvalds 已提交
2342 2343

static void pc_unthrottle(struct tty_struct *tty)
2344
{
L
Linus Torvalds 已提交
2345 2346
	struct channel *ch;
	unsigned long flags;
2347 2348 2349 2350
	/*
	 * verifyChannel returns the channel from the tty struct if it is
	 * valid. This serves as a sanity check.
	 */
A
Alan Cox 已提交
2351 2352 2353 2354
	ch = verifyChannel(tty);
	if (ch != NULL) {
		/* Just in case output was resumed because of a change
		   in Digi-flow */
2355 2356
		spin_lock_irqsave(&epca_lock, flags);
		if (ch->statusflags & RXSTOPPED) {
L
Linus Torvalds 已提交
2357 2358 2359 2360 2361
			globalwinon(ch);
			fepcmd(ch, RESUMERX, 0, 0, 0, 0);
			ch->statusflags &= ~RXSTOPPED;
			memoff(ch);
		}
2362
		spin_unlock_irqrestore(&epca_lock, flags);
2363 2364
	}
}
L
Linus Torvalds 已提交
2365

A
Alan Cox 已提交
2366
static int pc_send_break(struct tty_struct *tty, int msec)
2367
{
A
Alan Cox 已提交
2368
	struct channel *ch = tty->driver_data;
L
Linus Torvalds 已提交
2369 2370
	unsigned long flags;

A
Alan Cox 已提交
2371
	if (msec == -1)
A
Alan Cox 已提交
2372 2373 2374 2375 2376
		msec = 0xFFFF;
	else if (msec > 0xFFFE)
		msec = 0xFFFE;
	else if (msec < 1)
		msec = 1;
A
Alan Cox 已提交
2377

2378
	spin_lock_irqsave(&epca_lock, flags);
L
Linus Torvalds 已提交
2379
	globalwinon(ch);
2380 2381 2382 2383 2384 2385 2386
	/*
	 * 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...
	 */
L
Linus Torvalds 已提交
2387 2388
	fepcmd(ch, SENDBREAK, msec, 0, 10, 0);
	memoff(ch);
2389
	spin_unlock_irqrestore(&epca_lock, flags);
A
Alan Cox 已提交
2390
	return 0;
2391
}
L
Linus Torvalds 已提交
2392

2393
/* Caller MUST hold the lock */
L
Linus Torvalds 已提交
2394
static void setup_empty_event(struct tty_struct *tty, struct channel *ch)
2395
{
2396
	struct board_chan __iomem *bc = ch->brdchan;
L
Linus Torvalds 已提交
2397 2398 2399

	globalwinon(ch);
	ch->statusflags |= EMPTYWAIT;
2400 2401 2402 2403
	/*
	 * When set the iempty flag request a event to be generated when the
	 * transmit buffer is empty (If there is no BREAK in progress).
	 */
2404
	writeb(1, &bc->iempty);
L
Linus Torvalds 已提交
2405
	memoff(ch);
2406
}
L
Linus Torvalds 已提交
2407

2408 2409
#ifndef MODULE
static void __init epca_setup(char *str, int *ints)
2410
{
L
Linus Torvalds 已提交
2411 2412 2413 2414 2415
	struct board_info board;
	int               index, loop, last;
	char              *temp, *t2;
	unsigned          len;

2416 2417 2418 2419 2420 2421 2422 2423
	/*
	 * 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;
L
Linus Torvalds 已提交
2424 2425 2426 2427 2428 2429

	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++)
2430 2431 2432 2433 2434 2435 2436 2437
		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.
			 */
A
Alan Cox 已提交
2438 2439
			if (board.status == 2) {
			/* Begin ignore epcaconfig as well as lilo cmd line */
2440 2441 2442 2443 2444 2445
				nbdevs = 0;
				num_cards = 0;
				return;
			} /* End ignore epcaconfig as well as lilo cmd line */

			if (board.status > 2) {
A
Alan Cox 已提交
2446 2447
				printk(KERN_ERR "epca_setup: Invalid board status 0x%x\n",
						board.status);
2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485
				invalid_lilo_config = 1;
				setup_error_code |= INVALID_BOARD_STATUS;
				return;
			}
			last = index;
			break;
		case 2:
			board.type = ints[index];
			if (board.type >= PCIXEM)  {
				printk(KERN_ERR "epca_setup: Invalid board type 0x%x\n", board.type);
				invalid_lilo_config = 1;
				setup_error_code |= INVALID_BOARD_TYPE;
				return;
			}
			last = index;
			break;
		case 3:
			board.altpin = ints[index];
			if (board.altpin > 1) {
				printk(KERN_ERR "epca_setup: Invalid board altpin 0x%x\n", board.altpin);
				invalid_lilo_config = 1;
				setup_error_code |= INVALID_ALTPIN;
				return;
			}
			last = index;
			break;

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

2487 2488 2489 2490 2491 2492
		case 5:
			board.port = ints[index];
			if (ints[index] <= 0) {
				printk(KERN_ERR "epca_setup: Invalid io port 0x%x\n", (unsigned int)board.port);
				invalid_lilo_config = 1;
				setup_error_code |= INVALID_PORT_BASE;
L
Linus Torvalds 已提交
2493
				return;
2494 2495 2496 2497 2498 2499 2500
			}
			last = index;
			break;

		case 6:
			board.membase = ints[index];
			if (ints[index] <= 0) {
A
Alan Cox 已提交
2501 2502
				printk(KERN_ERR "epca_setup: Invalid memory base 0x%x\n",
					(unsigned int)board.membase);
2503 2504 2505 2506 2507 2508 2509 2510 2511 2512
				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;
L
Linus Torvalds 已提交
2513 2514 2515

		} /* End parse switch */

2516
	while (str && *str)  { /* Begin while there is a string arg */
L
Linus Torvalds 已提交
2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528
		/* 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;

2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543
		switch (index) {
		case 1:
			len = strlen(str);
			if (strncmp("Disable", str, len) == 0)
				board.status = 0;
			else if (strncmp("Enable", str, len) == 0)
				board.status = 1;
			else {
				printk(KERN_ERR "epca_setup: Invalid status %s\n", str);
				invalid_lilo_config = 1;
				setup_error_code |= INVALID_BOARD_STATUS;
				return;
			}
			last = index;
			break;
L
Linus Torvalds 已提交
2544

2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577
		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;
			else {
				printk(KERN_ERR "epca_setup: Invalid board type: %s\n", str);
				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;
			else if (strncmp("Enable", str, len) == 0)
				board.altpin = 1;
			else {
				printk(KERN_ERR "epca_setup: Invalid altpin %s\n", str);
				invalid_lilo_config = 1;
				setup_error_code |= INVALID_ALTPIN;
				return;
			}
			last = index;
			break;
L
Linus Torvalds 已提交
2578

2579 2580 2581 2582
		case 4:
			t2 = str;
			while (isdigit(*t2))
				t2++;
L
Linus Torvalds 已提交
2583

2584 2585 2586 2587 2588 2589
			if (*t2) {
				printk(KERN_ERR "epca_setup: Invalid port count %s\n", str);
				invalid_lilo_config = 1;
				setup_error_code |= INVALID_NUM_PORTS;
				return;
			}
L
Linus Torvalds 已提交
2590

2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632
			/*
			 * 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++;

			if (*t2) {
				printk(KERN_ERR "epca_setup: Invalid i/o address %s\n", str);
				invalid_lilo_config = 1;
				setup_error_code |= INVALID_PORT_BASE;
				return;
			}

			board.port = simple_strtoul(str, NULL, 16);
			last = index;
			break;

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

			if (*t2) {
A
Alan Cox 已提交
2633
				printk(KERN_ERR "epca_setup: Invalid memory base %s\n", str);
2634 2635
				invalid_lilo_config = 1;
				setup_error_code |= INVALID_MEM_BASE;
L
Linus Torvalds 已提交
2636
				return;
2637 2638 2639 2640 2641 2642 2643
			}
			board.membase = simple_strtoul(str, NULL, 16);
			last = index;
			break;
		default:
			printk(KERN_ERR "epca: Too many string parms\n");
			return;
L
Linus Torvalds 已提交
2644 2645 2646 2647
		}
		str = temp;
	} /* End while there is a string arg */

2648 2649
	if (last < 6) {
		printk(KERN_ERR "epca: Insufficient parms specified\n");
L
Linus Torvalds 已提交
2650 2651
		return;
	}
2652

L
Linus Torvalds 已提交
2653 2654
	/* I should REALLY validate the stuff here */
	/* Copies our local copy of board into boards */
A
Alan Cox 已提交
2655
	memcpy((void *)&boards[num_cards], (void *)&board, sizeof(board));
L
Linus Torvalds 已提交
2656
	/* Does this get called once per lilo arg are what ? */
2657 2658
	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],
L
Linus Torvalds 已提交
2659 2660
		board.numports, (int)board.port, (unsigned int) board.membase);
	num_cards++;
2661
}
L
Linus Torvalds 已提交
2662

2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673
static int __init epca_real_setup(char *str)
{
	int ints[11];

	epca_setup(get_options(str, 11, ints), ints);
	return 1;
}

__setup("digiepca", epca_real_setup);
#endif

L
Linus Torvalds 已提交
2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691
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, },
};

2692
static int __devinit epca_init_one(struct pci_dev *pdev,
L
Linus Torvalds 已提交
2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705
				 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;
2706

A
Alan Cox 已提交
2707
	addr = pci_resource_start(pdev, epca_info_tbl[info_idx].bar_idx);
L
Linus Torvalds 已提交
2708
	if (!addr) {
A
Alan Cox 已提交
2709
		printk(KERN_ERR PFX "PCI region #%d not available (size 0)\n",
L
Linus Torvalds 已提交
2710 2711 2712 2713 2714 2715 2716
			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;
2717 2718
	boards[board_idx].port = addr + PCI_IO_OFFSET;
	boards[board_idx].membase = addr;
L
Linus Torvalds 已提交
2719

A
Alan Cox 已提交
2720 2721
	if (!request_mem_region(addr + PCI_IO_OFFSET, 0x200000, "epca")) {
		printk(KERN_ERR PFX "resource 0x%x @ 0x%lx unavailable\n",
L
Linus Torvalds 已提交
2722 2723 2724 2725
			0x200000, addr + PCI_IO_OFFSET);
		goto err_out;
	}

A
Alan Cox 已提交
2726 2727
	boards[board_idx].re_map_port = ioremap_nocache(addr + PCI_IO_OFFSET,
								0x200000);
L
Linus Torvalds 已提交
2728
	if (!boards[board_idx].re_map_port) {
A
Alan Cox 已提交
2729
		printk(KERN_ERR PFX "cannot map 0x%x @ 0x%lx\n",
L
Linus Torvalds 已提交
2730 2731 2732 2733
			0x200000, addr + PCI_IO_OFFSET);
		goto err_out_free_pciio;
	}

A
Alan Cox 已提交
2734 2735
	if (!request_mem_region(addr, 0x200000, "epca")) {
		printk(KERN_ERR PFX "resource 0x%x @ 0x%lx unavailable\n",
L
Linus Torvalds 已提交
2736 2737 2738 2739
			0x200000, addr);
		goto err_out_free_iounmap;
	}

A
Alan Cox 已提交
2740
	boards[board_idx].re_map_membase = ioremap_nocache(addr, 0x200000);
L
Linus Torvalds 已提交
2741
	if (!boards[board_idx].re_map_membase) {
A
Alan Cox 已提交
2742
		printk(KERN_ERR PFX "cannot map 0x%x @ 0x%lx\n",
L
Linus Torvalds 已提交
2743 2744 2745 2746
			0x200000, addr + PCI_IO_OFFSET);
		goto err_out_free_memregion;
	}

2747 2748 2749 2750
	/*
	 * I don't know what the below does, but the hardware guys say its
	 * required on everything except PLX (In this case XRJ).
	 */
L
Linus Torvalds 已提交
2751
	if (info_idx != brd_xrj) {
2752
		pci_write_config_byte(pdev, 0x40, 0);
L
Linus Torvalds 已提交
2753 2754
		pci_write_config_byte(pdev, 0x46, 0);
	}
2755

L
Linus Torvalds 已提交
2756 2757 2758
	return 0;

err_out_free_memregion:
A
Alan Cox 已提交
2759
	release_mem_region(addr, 0x200000);
L
Linus Torvalds 已提交
2760
err_out_free_iounmap:
A
Alan Cox 已提交
2761
	iounmap(boards[board_idx].re_map_port);
L
Linus Torvalds 已提交
2762
err_out_free_pciio:
A
Alan Cox 已提交
2763
	release_mem_region(addr + PCI_IO_OFFSET, 0x200000);
L
Linus Torvalds 已提交
2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778
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);

2779
static int __init init_PCI(void)
2780
{
A
Alan Cox 已提交
2781
	memset(&epca_driver, 0, sizeof(epca_driver));
L
Linus Torvalds 已提交
2782 2783 2784 2785 2786
	epca_driver.name = "epca";
	epca_driver.id_table = epca_pci_tbl;
	epca_driver.probe = epca_init_one;

	return pci_register_driver(&epca_driver);
2787
}
L
Linus Torvalds 已提交
2788 2789

MODULE_LICENSE("GPL");