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