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