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