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