]> Pileus Git - ~andy/linux/blob - drivers/staging/dgap/dgap_fep5.c
Merge tag 'nfs-for-3.13-2' of git://git.linux-nfs.org/projects/trondmy/linux-nfs
[~andy/linux] / drivers / staging / dgap / dgap_fep5.c
1 /*
2  * Copyright 2003 Digi International (www.digi.com)
3  *      Scott H Kilau <Scott_Kilau at digi dot com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2, or (at your option)
8  * any later version.
9  * 
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED; without even the 
12  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
13  * PURPOSE.  See the GNU General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  *
19  *
20  *      NOTE TO LINUX KERNEL HACKERS:  DO NOT REFORMAT THIS CODE!
21  *
22  *      This is shared code between Digi's CVS archive and the
23  *      Linux Kernel sources.
24  *      Changing the source just for reformatting needlessly breaks
25  *      our CVS diff history.
26  *
27  *      Send any bug fixes/changes to:  Eng.Linux at digi dot com.
28  *      Thank you.
29  *
30  * $Id: dgap_fep5.c,v 1.2 2011/06/21 10:35:40 markh Exp $
31  */
32
33
34 #include <linux/kernel.h>
35 #include <linux/version.h>
36 #include <linux/module.h>
37 #include <linux/pci.h>
38 #include <linux/delay.h>        /* For udelay */
39 #include <asm/uaccess.h>        /* For copy_from_user/copy_to_user */
40 #include <linux/tty.h>
41 #include <linux/tty_flip.h>     /* For tty_schedule_flip */
42
43 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,39)
44 #include <linux/sched.h>
45 #endif
46
47 #include "dgap_driver.h"
48 #include "dgap_pci.h"
49 #include "dgap_fep5.h"
50 #include "dgap_tty.h"
51 #include "dgap_conf.h"
52 #include "dgap_parse.h"
53 #include "dgap_trace.h"
54
55 /*
56  * Our function prototypes
57  */
58 static void dgap_cmdw_ext(struct channel_t *ch, u16 cmd, u16 word, uint ncmds);
59 static int dgap_event(struct board_t *bd);
60
61 /*
62  * internal variables
63  */
64 static uint dgap_count = 500;
65
66
67 /*
68  * Loads the dgap.conf config file from the user.
69  */
70 void dgap_do_config_load(uchar __user *uaddr, int len)
71 {
72         int orig_len = len;
73         char *to_addr;
74         uchar __user *from_addr = uaddr;
75         char buf[U2BSIZE];
76         int n;
77
78         to_addr = dgap_config_buf = dgap_driver_kzmalloc(len + 1, GFP_ATOMIC);
79         if (!dgap_config_buf) {
80                 DPR_INIT(("dgap_do_config_load - unable to allocate memory for file\n"));
81                 dgap_driver_state = DRIVER_NEED_CONFIG_LOAD;
82                 return;
83         }
84
85         n = U2BSIZE;
86         while (len) {
87
88                 if (n > len)
89                         n = len;
90
91                 if (copy_from_user((char *) &buf, from_addr, n) == -1 )
92                         return;
93
94                 /* Copy data from buffer to kernel memory */
95                 memcpy(to_addr, buf, n);
96
97                 /* increment counts */
98                 len -= n;
99                 to_addr += n;
100                 from_addr += n;
101                 n = U2BSIZE;
102         }
103
104         dgap_config_buf[orig_len] = '\0';
105
106         to_addr = dgap_config_buf;
107         dgap_parsefile(&to_addr, TRUE);
108
109         DPR_INIT(("dgap_config_load() finish\n"));
110
111         return;
112 }
113
114
115 int dgap_after_config_loaded(void)
116 {
117         int i = 0;
118         int rc = 0;
119
120         /*
121          * Register our ttys, now that we have the config loaded.
122          */
123         for (i = 0; i < dgap_NumBoards; ++i) {
124
125                 /*
126                  * Initialize KME waitqueues...
127                  */
128                 init_waitqueue_head(&(dgap_Board[i]->kme_wait));
129
130                 /*
131                  * allocate flip buffer for board.
132                  */
133                 dgap_Board[i]->flipbuf = dgap_driver_kzmalloc(MYFLIPLEN, GFP_ATOMIC);
134                 dgap_Board[i]->flipflagbuf = dgap_driver_kzmalloc(MYFLIPLEN, GFP_ATOMIC);
135         }
136
137         return rc;
138 }
139
140
141
142 /*=======================================================================
143  *
144  *      usertoboard - copy from user space to board space.
145  *
146  *=======================================================================*/
147 static int dgap_usertoboard(struct board_t *brd, char *to_addr, char __user *from_addr, int len)
148 {
149         char buf[U2BSIZE];
150         int n = U2BSIZE;
151
152         if (!brd || brd->magic != DGAP_BOARD_MAGIC)
153                 return -EFAULT;
154
155         while (len) {
156                 if (n > len)
157                         n = len;
158
159                 if (copy_from_user((char *) &buf, from_addr, n) == -1 ) {
160                         return -EFAULT;
161                 }
162
163                 /* Copy data from buffer to card memory */
164                 memcpy_toio(to_addr, buf, n);
165
166                 /* increment counts */
167                 len -= n;
168                 to_addr += n;
169                 from_addr += n;   
170                 n = U2BSIZE;
171         }
172         return 0;
173 }
174
175
176 /*
177  * Copies the BIOS code from the user to the board,
178  * and starts the BIOS running.
179  */
180 void dgap_do_bios_load(struct board_t *brd, uchar __user *ubios, int len)
181 {
182         uchar *addr;
183         uint offset;
184         int i;
185
186         if (!brd || (brd->magic != DGAP_BOARD_MAGIC) || !brd->re_map_membase)
187                 return;
188
189         DPR_INIT(("dgap_do_bios_load() start\n"));
190
191         addr = brd->re_map_membase;
192
193         /*
194          * clear POST area
195          */
196         for (i = 0; i < 16; i++)
197                 writeb(0, addr + POSTAREA + i);
198                                 
199         /*
200          * Download bios
201          */
202         offset = 0x1000;
203         if (dgap_usertoboard(brd, addr + offset, ubios, len) == -1 ) {
204                 brd->state = BOARD_FAILED;
205                 brd->dpastatus = BD_NOFEP;
206                 return;
207         }
208
209         writel(0x0bf00401, addr);
210         writel(0, (addr + 4));
211
212         /* Clear the reset, and change states. */
213         writeb(FEPCLR, brd->re_map_port);
214         brd->state = WAIT_BIOS_LOAD;
215 }
216
217
218 /*
219  * Checks to see if the BIOS completed running on the card.
220  */
221 static void dgap_do_wait_for_bios(struct board_t *brd)
222 {
223         uchar *addr;
224         u16 word;
225
226         if (!brd || (brd->magic != DGAP_BOARD_MAGIC) || !brd->re_map_membase)
227                 return;
228
229         addr = brd->re_map_membase;
230         word = readw(addr + POSTAREA);
231
232         /* Check to see if BIOS thinks board is good. (GD). */
233         if (word == *(u16 *) "GD") {
234                 DPR_INIT(("GOT GD in memory, moving states.\n"));
235                 brd->state = FINISHED_BIOS_LOAD;
236                 return;
237         }
238
239         /* Give up on board after too long of time taken */
240         if (brd->wait_for_bios++ > 5000) {
241                 u16 err1 = readw(addr + SEQUENCE);
242                 u16 err2 = readw(addr + ERROR);
243                 APR(("***WARNING*** %s failed diagnostics.  Error #(%x,%x).\n",
244                         brd->name, err1, err2));
245                 brd->state = BOARD_FAILED;
246                 brd->dpastatus = BD_NOFEP;
247         }
248 }
249
250
251 /*
252  * Copies the FEP code from the user to the board,
253  * and starts the FEP running.
254  */
255 void dgap_do_fep_load(struct board_t *brd, uchar __user *ufep, int len)
256 {
257         uchar *addr;
258         uint offset;
259
260         if (!brd || (brd->magic != DGAP_BOARD_MAGIC) || !brd->re_map_membase)
261                 return;
262
263         addr = brd->re_map_membase;
264
265         DPR_INIT(("dgap_do_fep_load() for board %s : start\n", brd->name));
266
267         /*
268          * Download FEP
269          */
270         offset = 0x1000;
271         if (dgap_usertoboard(brd, addr + offset, ufep, len) == -1 ) {
272                 brd->state = BOARD_FAILED;
273                 brd->dpastatus = BD_NOFEP;
274                 return;
275         }
276
277         /*
278          * If board is a concentrator product, we need to give
279          * it its config string describing how the concentrators look.
280          */
281         if ((brd->type == PCX) || (brd->type == PEPC)) {
282                 uchar string[100];
283                 uchar *config, *xconfig;
284                 int i = 0;
285
286                 xconfig = dgap_create_config_string(brd, string);
287
288                 /* Write string to board memory */
289                 config = addr + CONFIG;
290                 for (; i < CONFIGSIZE; i++, config++, xconfig++) {
291                         writeb(*xconfig, config);
292                         if ((*xconfig & 0xff) == 0xff)
293                                 break;
294                 }
295         }
296
297         writel(0xbfc01004, (addr + 0xc34));
298         writel(0x3, (addr + 0xc30));
299
300         /* change states. */
301         brd->state = WAIT_FEP_LOAD;
302
303         DPR_INIT(("dgap_do_fep_load() for board %s : finish\n", brd->name));
304
305 }
306
307
308 /*
309  * Waits for the FEP to report thats its ready for us to use.
310  */
311 static void dgap_do_wait_for_fep(struct board_t *brd)
312 {
313         uchar *addr;
314         u16 word;
315
316         if (!brd || (brd->magic != DGAP_BOARD_MAGIC) || !brd->re_map_membase)
317                 return;
318
319         addr = brd->re_map_membase;
320
321         DPR_INIT(("dgap_do_wait_for_fep() for board %s : start. addr: %p\n", brd->name, addr));
322
323         word = readw(addr + FEPSTAT);
324
325         /* Check to see if FEP is up and running now. */
326         if (word == *(u16 *) "OS") {
327                 DPR_INIT(("GOT OS in memory for board %s, moving states.\n", brd->name));
328                 brd->state = FINISHED_FEP_LOAD;
329
330                 /*
331                  * Check to see if the board can support FEP5+ commands.
332                  */
333                 word = readw(addr + FEP5_PLUS);
334                 if (word == *(u16 *) "5A") {
335                         DPR_INIT(("GOT 5A in memory for board %s, board supports extended FEP5 commands.\n", brd->name));
336                         brd->bd_flags |= BD_FEP5PLUS;
337                 }
338
339                 return;
340         }
341
342         /* Give up on board after too long of time taken */
343         if (brd->wait_for_fep++ > 5000) {
344                 u16 err1 = readw(addr + SEQUENCE);
345                 u16 err2 = readw(addr + ERROR);
346                 APR(("***WARNING*** FEPOS for %s not functioning.  Error #(%x,%x).\n",
347                         brd->name, err1, err2));
348                 brd->state = BOARD_FAILED;
349                 brd->dpastatus = BD_NOFEP;
350         }
351
352         DPR_INIT(("dgap_do_wait_for_fep() for board %s : finish\n", brd->name));
353 }
354
355
356 /*
357  * Physically forces the FEP5 card to reset itself.
358  */
359 static void dgap_do_reset_board(struct board_t *brd)
360 {
361         uchar check;
362         u32 check1;
363         u32 check2;
364         int i = 0;
365
366         if (!brd || (brd->magic != DGAP_BOARD_MAGIC) || !brd->re_map_membase || !brd->re_map_port) {
367                 DPR_INIT(("dgap_do_reset_board() start. bad values. brd: %p mem: %p io: %p\n", 
368                         brd, brd ? brd->re_map_membase : 0, brd ? brd->re_map_port : 0));
369                 return;
370         }
371
372         DPR_INIT(("dgap_do_reset_board() start. io: %p\n", brd->re_map_port));
373
374         /* FEPRST does not vary among supported boards */
375         writeb(FEPRST, brd->re_map_port);
376
377         for (i = 0; i <= 1000; i++) {
378                 check = readb(brd->re_map_port) & 0xe;
379                 if (check == FEPRST)
380                         break;
381                 udelay(10);
382
383         }
384         if (i > 1000) {
385                 APR(("*** WARNING *** Board not resetting...  Failing board.\n"));
386                 brd->state = BOARD_FAILED;
387                 brd->dpastatus = BD_NOFEP;
388                 goto failed;
389         }
390
391         /*
392          * Make sure there really is memory out there.
393          */
394         writel(0xa55a3cc3, (brd->re_map_membase + LOWMEM));
395         writel(0x5aa5c33c, (brd->re_map_membase + HIGHMEM));
396         check1 = readl(brd->re_map_membase + LOWMEM);
397         check2 = readl(brd->re_map_membase + HIGHMEM);
398
399         if ((check1 != 0xa55a3cc3) || (check2 != 0x5aa5c33c)) {
400                 APR(("*** Warning *** No memory at %p for board.\n", brd->re_map_membase));
401                 brd->state = BOARD_FAILED;
402                 brd->dpastatus = BD_NOFEP;
403                 goto failed;
404         }
405
406         if (brd->state != BOARD_FAILED)
407                 brd->state = FINISHED_RESET;
408
409 failed:
410         DPR_INIT(("dgap_do_reset_board() finish\n"));
411 }
412
413
414 /*
415  * Sends a concentrator image into the FEP5 board.
416  */
417 void dgap_do_conc_load(struct board_t *brd, uchar *uaddr, int len)
418 {
419         char *vaddr;
420         u16 offset = 0;
421         struct downld_t *to_dp;
422
423         if (!brd || (brd->magic != DGAP_BOARD_MAGIC) || !brd->re_map_membase)
424                 return;
425
426         vaddr = brd->re_map_membase;
427
428         offset = readw((u16 *) (vaddr + DOWNREQ));
429         to_dp = (struct downld_t *) (vaddr + (int) offset);
430
431         /*
432          * The image was already read into kernel space,
433          * we do NOT need a user space read here
434          */
435         memcpy_toio((char *) to_dp, uaddr, sizeof(struct downld_t));
436
437         /* Tell card we have data for it */
438         writew(0, vaddr + (DOWNREQ));
439
440         brd->conc_dl_status = NO_PENDING_CONCENTRATOR_REQUESTS;
441 }
442
443
444 #define EXPANSION_ROM_SIZE      (64 * 1024)
445 #define FEP5_ROM_MAGIC          (0xFEFFFFFF)
446
447 static void dgap_get_vpd(struct board_t *brd)
448 {
449         u32 magic;
450         u32 base_offset;
451         u16 rom_offset;
452         u16 vpd_offset;
453         u16 image_length;
454         u16 i;
455         uchar byte1;
456         uchar byte2;
457
458         /*
459          * Poke the magic number at the PCI Rom Address location.
460          * If VPD is supported, the value read from that address
461          * will be non-zero.
462          */
463         magic = FEP5_ROM_MAGIC;
464         pci_write_config_dword(brd->pdev, PCI_ROM_ADDRESS, magic);
465         pci_read_config_dword(brd->pdev, PCI_ROM_ADDRESS, &magic);
466
467         /* VPD not supported, bail */
468         if (!magic)
469                 return;
470
471         /*
472          * To get to the OTPROM memory, we have to send the boards base
473          * address or'ed with 1 into the PCI Rom Address location.
474          */
475         magic = brd->membase | 0x01;
476         pci_write_config_dword(brd->pdev, PCI_ROM_ADDRESS, magic);
477         pci_read_config_dword(brd->pdev, PCI_ROM_ADDRESS, &magic);
478
479         byte1 = readb(brd->re_map_membase);
480         byte2 = readb(brd->re_map_membase + 1);
481
482         /*
483          * If the board correctly swapped to the OTPROM memory,
484          * the first 2 bytes (header) should be 0x55, 0xAA
485          */
486         if (byte1 == 0x55 && byte2 == 0xAA) {
487
488                 base_offset = 0;
489
490                 /*
491                  * We have to run through all the OTPROM memory looking
492                  * for the VPD offset.
493                  */
494                 while (base_offset <= EXPANSION_ROM_SIZE) {
495                 
496                         /*
497                          * Lots of magic numbers here.
498                          *
499                          * The VPD offset is located inside the ROM Data Structure.
500                          * We also have to remember the length of each
501                          * ROM Data Structure, so we can "hop" to the next
502                          * entry if the VPD isn't in the current
503                          * ROM Data Structure.
504                          */
505                         rom_offset = readw(brd->re_map_membase + base_offset + 0x18);
506                         image_length = readw(brd->re_map_membase + rom_offset + 0x10) * 512;
507                         vpd_offset = readw(brd->re_map_membase + rom_offset + 0x08);
508
509                         /* Found the VPD entry */
510                         if (vpd_offset)
511                                 break;
512
513                         /* We didn't find a VPD entry, go to next ROM entry. */
514                         base_offset += image_length;
515
516                         byte1 = readb(brd->re_map_membase + base_offset);
517                         byte2 = readb(brd->re_map_membase + base_offset + 1);
518
519                         /*
520                          * If the new ROM offset doesn't have 0x55, 0xAA
521                          * as its header, we have run out of ROM.
522                          */
523                         if (byte1 != 0x55 || byte2 != 0xAA)
524                                 break;
525                 }
526
527                 /*
528                  * If we have a VPD offset, then mark the board
529                  * as having a valid VPD, and copy VPDSIZE (512) bytes of
530                  * that VPD to the buffer we have in our board structure.
531                  */
532                 if (vpd_offset) {
533                         brd->bd_flags |= BD_HAS_VPD;
534                         for (i = 0; i < VPDSIZE; i++)
535                                 brd->vpd[i] = readb(brd->re_map_membase + vpd_offset + i);
536                 }
537         }
538
539         /*
540          * We MUST poke the magic number at the PCI Rom Address location again.
541          * This makes the card report the regular board memory back to us,
542          * rather than the OTPROM memory.
543          */
544         magic = FEP5_ROM_MAGIC;
545         pci_write_config_dword(brd->pdev, PCI_ROM_ADDRESS, magic);
546 }
547
548
549 /*
550  * Our board poller function.
551  */
552 void dgap_poll_tasklet(unsigned long data)
553 {
554         struct board_t *bd = (struct board_t *) data;
555         ulong  lock_flags;
556         ulong  lock_flags2;
557         char *vaddr;
558         u16 head, tail;
559         u16 *chk_addr;
560         u16 check = 0;
561
562         if (!bd || (bd->magic != DGAP_BOARD_MAGIC)) {
563                 APR(("dgap_poll_tasklet() - NULL or bad bd.\n"));
564                 return;
565         }
566
567         if (bd->inhibit_poller)
568                 return;
569
570         DGAP_LOCK(bd->bd_lock, lock_flags);
571
572         vaddr = bd->re_map_membase;
573
574         /*
575          * If board is ready, parse deeper to see if there is anything to do.
576          */
577         if (bd->state == BOARD_READY) {
578
579                 struct ev_t *eaddr = NULL;
580
581                 if (!bd->re_map_membase) {
582                         DGAP_UNLOCK(bd->bd_lock, lock_flags);
583                         return;
584                 }
585                 if (!bd->re_map_port) {
586                         DGAP_UNLOCK(bd->bd_lock, lock_flags);
587                         return;
588                 }
589
590                 if (!bd->nasync) {
591                         goto out;
592                 }
593
594                 /*
595                  * If this is a CX or EPCX, we need to see if the firmware
596                  * is requesting a concentrator image from us.
597                  */
598                 if ((bd->type == PCX) || (bd->type == PEPC)) {
599                         chk_addr = (u16 *) (vaddr + DOWNREQ);
600                         check = readw(chk_addr);
601                         /* Nonzero if FEP is requesting concentrator image. */
602                         if (check) {
603                                 if (bd->conc_dl_status == NO_PENDING_CONCENTRATOR_REQUESTS)
604                                         bd->conc_dl_status = NEED_CONCENTRATOR;
605                                 /*
606                                  * Signal downloader, its got some work to do.
607                                  */
608                                 DGAP_LOCK(dgap_dl_lock, lock_flags2);
609                                 if (dgap_dl_action != 1) {
610                                         dgap_dl_action = 1;
611                                         wake_up_interruptible(&dgap_dl_wait);
612                                 }
613                                 DGAP_UNLOCK(dgap_dl_lock, lock_flags2);
614
615                         }
616                 }
617
618                 eaddr = (struct ev_t *) (vaddr + EVBUF);
619
620                 /* Get our head and tail */
621                 head = readw(&(eaddr->ev_head));
622                 tail = readw(&(eaddr->ev_tail));
623
624                 /*
625                  * If there is an event pending. Go service it.
626                  */
627                 if (head != tail) {
628                         DGAP_UNLOCK(bd->bd_lock, lock_flags);
629                         dgap_event(bd);
630                         DGAP_LOCK(bd->bd_lock, lock_flags);
631                 }
632
633 out:
634                 /*
635                  * If board is doing interrupts, ACK the interrupt.
636                  */
637                 if (bd && bd->intr_running) {
638                         readb(bd->re_map_port + 2);
639                 }
640
641                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
642                 return;
643         }
644
645         /* Our state machine to get the board up and running */
646
647         /* Reset board */
648         if (bd->state == NEED_RESET) {
649
650                 /* Get VPD info */
651                 dgap_get_vpd(bd);
652
653                 dgap_do_reset_board(bd);
654         }
655
656         /* Move to next state */
657         if (bd->state == FINISHED_RESET) {
658                 bd->state = NEED_CONFIG;
659         }
660
661         if (bd->state == NEED_CONFIG) {
662                 /*
663                  * Match this board to a config the user created for us.
664                  */
665                 bd->bd_config = dgap_find_config(bd->type, bd->pci_bus, bd->pci_slot);
666
667                 /*
668                  * Because the 4 port Xr products share the same PCI ID
669                  * as the 8 port Xr products, if we receive a NULL config
670                  * back, and this is a PAPORT8 board, retry with a
671                  * PAPORT4 attempt as well.
672                  */
673                 if (bd->type == PAPORT8 && !bd->bd_config) {
674                         bd->bd_config = dgap_find_config(PAPORT4, bd->pci_bus, bd->pci_slot);
675                 }
676
677                 /*
678                  * Register the ttys (if any) into the kernel.
679                  */
680                 if (bd->bd_config) {
681                         bd->state = FINISHED_CONFIG;
682                 }
683                 else {
684                         bd->state = CONFIG_NOT_FOUND;
685                 }
686         }
687
688         /* Move to next state */
689         if (bd->state == FINISHED_CONFIG) {
690                 bd->state = NEED_DEVICE_CREATION;
691         }
692
693         /* Move to next state */
694         if (bd->state == NEED_DEVICE_CREATION) {
695                 /*
696                  * Signal downloader, its got some work to do.
697                  */
698                 DGAP_LOCK(dgap_dl_lock, lock_flags2);
699                 if (dgap_dl_action != 1) {
700                         dgap_dl_action = 1;
701                         wake_up_interruptible(&dgap_dl_wait);
702                 }
703                 DGAP_UNLOCK(dgap_dl_lock, lock_flags2);
704         }
705
706         /* Move to next state */
707         if (bd->state == FINISHED_DEVICE_CREATION) {
708                 bd->state = NEED_BIOS_LOAD;
709         }
710
711         /* Move to next state */
712         if (bd->state == NEED_BIOS_LOAD) {
713                 /*
714                  * Signal downloader, its got some work to do.
715                  */
716                 DGAP_LOCK(dgap_dl_lock, lock_flags2);
717                 if (dgap_dl_action != 1) {
718                         dgap_dl_action = 1;
719                         wake_up_interruptible(&dgap_dl_wait);
720                 }
721                 DGAP_UNLOCK(dgap_dl_lock, lock_flags2);
722         }
723
724         /* Wait for BIOS to test board... */
725         if (bd->state == WAIT_BIOS_LOAD) {
726                 dgap_do_wait_for_bios(bd);
727         }
728
729         /* Move to next state */
730         if (bd->state == FINISHED_BIOS_LOAD) {
731                 bd->state = NEED_FEP_LOAD;
732
733                 /*
734                  * Signal downloader, its got some work to do.
735                  */
736                 DGAP_LOCK(dgap_dl_lock, lock_flags2);
737                 if (dgap_dl_action != 1) {
738                         dgap_dl_action = 1;
739                         wake_up_interruptible(&dgap_dl_wait);
740                 }
741                 DGAP_UNLOCK(dgap_dl_lock, lock_flags2);
742         }
743
744         /* Wait for FEP to load on board... */
745         if (bd->state == WAIT_FEP_LOAD) {
746                 dgap_do_wait_for_fep(bd);
747         }
748
749
750         /* Move to next state */
751         if (bd->state == FINISHED_FEP_LOAD) {
752
753                 /*
754                  * Do tty device initialization.
755                  */
756                 int rc = dgap_tty_init(bd);
757
758                 if (rc < 0) {
759                         dgap_tty_uninit(bd);
760                         APR(("Can't init tty devices (%d)\n", rc));
761                         bd->state = BOARD_FAILED;
762                         bd->dpastatus = BD_NOFEP;
763                 }
764                 else {
765                         bd->state = NEED_PROC_CREATION;
766
767                         /*
768                          * Signal downloader, its got some work to do.
769                          */
770                         DGAP_LOCK(dgap_dl_lock, lock_flags2);
771                         if (dgap_dl_action != 1) {
772                                 dgap_dl_action = 1;
773                                 wake_up_interruptible(&dgap_dl_wait);
774                         }
775                         DGAP_UNLOCK(dgap_dl_lock, lock_flags2);
776                 }
777         }
778
779         /* Move to next state */
780         if (bd->state == FINISHED_PROC_CREATION) {
781
782                 bd->state = BOARD_READY;
783                 bd->dpastatus = BD_RUNNING;
784
785                 /*
786                  * If user requested the board to run in interrupt mode,
787                  * go and set it up on the board.
788                  */
789                 if (bd->intr_used) {
790                         writew(1, (bd->re_map_membase + ENABLE_INTR));
791                         /*
792                          * Tell the board to poll the UARTS as fast as possible.
793                          */
794                         writew(FEPPOLL_MIN, (bd->re_map_membase + FEPPOLL));
795                         bd->intr_running = 1;
796                 }
797
798                 /* Wake up anyone waiting for board state to change to ready */
799                 wake_up_interruptible(&bd->state_wait);
800         }
801
802         DGAP_UNLOCK(bd->bd_lock, lock_flags);
803 }
804
805
806 /*=======================================================================
807  *
808  *      dgap_cmdb - Sends a 2 byte command to the FEP.
809  *
810  *              ch      - Pointer to channel structure.
811  *              cmd     - Command to be sent.
812  *              byte1   - Integer containing first byte to be sent.
813  *              byte2   - Integer containing second byte to be sent.
814  *              ncmds   - Wait until ncmds or fewer cmds are left
815  *                        in the cmd buffer before returning.
816  *
817  *=======================================================================*/
818 void dgap_cmdb(struct channel_t *ch, uchar cmd, uchar byte1, uchar byte2, uint ncmds)
819 {                       
820         char            *vaddr = NULL;
821         struct cm_t     *cm_addr = NULL;
822         uint            count;
823         uint            n;
824         u16             head;
825         u16             tail;
826
827         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
828                 return;
829
830         /*
831          * Check if board is still alive.
832          */
833         if (ch->ch_bd->state == BOARD_FAILED) {
834                 DPR_CORE(("%s:%d board is in failed state.\n", __FILE__, __LINE__));
835                 return;
836         }               
837
838         /*
839          * Make sure the pointers are in range before
840          * writing to the FEP memory.
841          */
842         vaddr = ch->ch_bd->re_map_membase;
843
844         if (!vaddr)
845                 return;
846
847         cm_addr = (struct cm_t *) (vaddr + CMDBUF);
848         head = readw(&(cm_addr->cm_head));
849
850         /* 
851          * Forget it if pointers out of range.
852          */
853         if (head >= (CMDMAX - CMDSTART) || (head & 03)) {
854                 DPR_CORE(("%s:%d pointers out of range, failing board!\n", __FILE__, __LINE__));
855                 ch->ch_bd->state = BOARD_FAILED;
856                 return; 
857         }
858
859         /*
860          * Put the data in the circular command buffer.
861          */
862         writeb(cmd, (char *) (vaddr + head + CMDSTART + 0));
863         writeb((uchar) ch->ch_portnum, (char *) (vaddr + head + CMDSTART + 1));
864         writeb(byte1, (char *) (vaddr + head + CMDSTART + 2));
865         writeb(byte2, (char *) (vaddr + head + CMDSTART + 3));
866
867         head = (head + 4) & (CMDMAX - CMDSTART - 4);
868
869         writew(head, &(cm_addr->cm_head));
870
871         /*
872          * Wait if necessary before updating the head  
873          * pointer to limit the number of outstanding
874          * commands to the FEP.   If the time spent waiting
875          * is outlandish, declare the FEP dead.
876          */
877         for (count = dgap_count ;;) {
878
879                 head = readw(&(cm_addr->cm_head));
880                 tail = readw(&(cm_addr->cm_tail));
881
882                 n = (head - tail) & (CMDMAX - CMDSTART - 4);
883
884                 if (n <= ncmds * sizeof(struct cm_t))
885                         break;
886
887                 if (--count == 0) {
888                         DPR_CORE(("%s:%d failing board.\n",__FILE__, __LINE__));
889                         ch->ch_bd->state = BOARD_FAILED;
890                         return;
891                 }
892                 udelay(10);
893         }  
894 }
895
896
897 /*=======================================================================
898  *
899  *      dgap_cmdw - Sends a 1 word command to the FEP.
900  *      
901  *              ch      - Pointer to channel structure.
902  *              cmd     - Command to be sent.
903  *              word    - Integer containing word to be sent.
904  *              ncmds   - Wait until ncmds or fewer cmds are left
905  *                        in the cmd buffer before returning.
906  *
907  *=======================================================================*/
908 void dgap_cmdw(struct channel_t *ch, uchar cmd, u16 word, uint ncmds)
909 {
910         char            *vaddr = NULL;
911         struct cm_t     *cm_addr = NULL;
912         uint            count;
913         uint            n;
914         u16             head;
915         u16             tail;
916
917         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
918                 return;
919
920         /*
921          * Check if board is still alive.
922          */
923         if (ch->ch_bd->state == BOARD_FAILED) {
924                 DPR_CORE(("%s:%d board is failed!\n", __FILE__, __LINE__));
925                 return;
926         }
927
928         /*
929          * Make sure the pointers are in range before
930          * writing to the FEP memory.
931          */
932         vaddr = ch->ch_bd->re_map_membase;
933         if (!vaddr)
934                 return;
935
936         cm_addr = (struct cm_t *) (vaddr + CMDBUF);
937         head = readw(&(cm_addr->cm_head));
938
939         /* 
940          * Forget it if pointers out of range.
941          */
942         if (head >= (CMDMAX - CMDSTART) || (head & 03)) {
943                 DPR_CORE(("%s:%d Pointers out of range.  Failing board.\n",__FILE__, __LINE__));
944                 ch->ch_bd->state = BOARD_FAILED;
945                 return;
946         }
947
948         /*
949          * Put the data in the circular command buffer.
950          */
951         writeb(cmd, (char *) (vaddr + head + CMDSTART + 0));
952         writeb((uchar) ch->ch_portnum, (char *) (vaddr + head + CMDSTART + 1));
953         writew((u16) word, (char *) (vaddr + head + CMDSTART + 2));
954
955         head = (head + 4) & (CMDMAX - CMDSTART - 4);
956
957         writew(head, &(cm_addr->cm_head));
958
959         /*
960          * Wait if necessary before updating the head
961          * pointer to limit the number of outstanding  
962          * commands to the FEP.   If the time spent waiting
963          * is outlandish, declare the FEP dead.
964          */
965         for (count = dgap_count ;;) {
966
967                 head = readw(&(cm_addr->cm_head));
968                 tail = readw(&(cm_addr->cm_tail));
969
970                 n = (head - tail) & (CMDMAX - CMDSTART - 4);
971
972                 if (n <= ncmds * sizeof(struct cm_t))
973                         break;
974
975                 if (--count == 0) {
976                         DPR_CORE(("%s:%d Failing board.\n",__FILE__, __LINE__));
977                         ch->ch_bd->state = BOARD_FAILED;
978                         return;
979                 }
980                 udelay(10);
981         }  
982 }
983
984
985
986 /*=======================================================================
987  *
988  *      dgap_cmdw_ext - Sends a extended word command to the FEP.
989  *      
990  *              ch      - Pointer to channel structure.
991  *              cmd     - Command to be sent.
992  *              word    - Integer containing word to be sent.
993  *              ncmds   - Wait until ncmds or fewer cmds are left
994  *                        in the cmd buffer before returning.
995  *
996  *=======================================================================*/
997 static void dgap_cmdw_ext(struct channel_t *ch, u16 cmd, u16 word, uint ncmds)
998 {
999         char            *vaddr = NULL;
1000         struct cm_t     *cm_addr = NULL;
1001         uint            count;
1002         uint            n;
1003         u16             head;
1004         u16             tail;
1005
1006         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
1007                 return;
1008
1009         /*
1010          * Check if board is still alive.
1011          */
1012         if (ch->ch_bd->state == BOARD_FAILED) {
1013                 DPR_CORE(("%s:%d board is failed!\n", __FILE__, __LINE__));
1014                 return;
1015         }
1016
1017         /*
1018          * Make sure the pointers are in range before
1019          * writing to the FEP memory.
1020          */
1021         vaddr = ch->ch_bd->re_map_membase;
1022         if (!vaddr)
1023                 return;
1024
1025         cm_addr = (struct cm_t *) (vaddr + CMDBUF);
1026         head = readw(&(cm_addr->cm_head));
1027
1028         /* 
1029          * Forget it if pointers out of range.
1030          */
1031         if (head >= (CMDMAX - CMDSTART) || (head & 03)) {
1032                 DPR_CORE(("%s:%d Pointers out of range.  Failing board.\n",__FILE__, __LINE__));
1033                 ch->ch_bd->state = BOARD_FAILED;
1034                 return;
1035         }
1036
1037         /*
1038          * Put the data in the circular command buffer.
1039          */
1040
1041         /* Write an FF to tell the FEP that we want an extended command */
1042         writeb((uchar) 0xff, (char *) (vaddr + head + CMDSTART + 0));
1043
1044         writeb((uchar) ch->ch_portnum, (uchar *) (vaddr + head + CMDSTART + 1));
1045         writew((u16) cmd, (char *) (vaddr + head + CMDSTART + 2));
1046
1047         /*
1048          * If the second part of the command won't fit,
1049          * put it at the beginning of the circular buffer.
1050          */
1051         if (((head + 4) >= ((CMDMAX - CMDSTART)) || (head & 03))) {
1052                 writew((u16) word, (char *) (vaddr + CMDSTART));
1053         } else {
1054                 writew((u16) word, (char *) (vaddr + head + CMDSTART + 4));
1055         }
1056
1057         head = (head + 8) & (CMDMAX - CMDSTART - 4);
1058
1059         writew(head, &(cm_addr->cm_head));
1060
1061         /*
1062          * Wait if necessary before updating the head
1063          * pointer to limit the number of outstanding  
1064          * commands to the FEP.   If the time spent waiting
1065          * is outlandish, declare the FEP dead.
1066          */
1067         for (count = dgap_count ;;) {
1068
1069                 head = readw(&(cm_addr->cm_head));
1070                 tail = readw(&(cm_addr->cm_tail));
1071
1072                 n = (head - tail) & (CMDMAX - CMDSTART - 4);
1073
1074                 if (n <= ncmds * sizeof(struct cm_t))
1075                         break;
1076
1077                 if (--count == 0) {
1078                         DPR_CORE(("%s:%d Failing board.\n",__FILE__, __LINE__));
1079                         ch->ch_bd->state = BOARD_FAILED;
1080                         return;
1081                 }
1082                 udelay(10);
1083         }  
1084 }
1085
1086
1087 /*=======================================================================
1088  *
1089  *      dgap_wmove - Write data to FEP buffer.
1090  *
1091  *              ch      - Pointer to channel structure.
1092  *              buf     - Poiter to characters to be moved.
1093  *              cnt     - Number of characters to move.
1094  *
1095  *=======================================================================*/
1096 void dgap_wmove(struct channel_t *ch, char *buf, uint cnt)
1097 {
1098         int    n;
1099         char   *taddr;
1100         struct bs_t    *bs;
1101         u16    head;
1102
1103         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
1104                 return;
1105  
1106         /*
1107          * Check parameters.
1108          */
1109         bs   = ch->ch_bs;
1110         head = readw(&(bs->tx_head));
1111
1112         /*
1113          * If pointers are out of range, just return.
1114          */
1115         if ((cnt > ch->ch_tsize) || (unsigned)(head - ch->ch_tstart) >= ch->ch_tsize) {
1116                 DPR_CORE(("%s:%d pointer out of range", __FILE__, __LINE__));
1117                 return;
1118         }
1119
1120         /*
1121          * If the write wraps over the top of the circular buffer,
1122          * move the portion up to the wrap point, and reset the
1123          * pointers to the bottom.
1124          */
1125         n = ch->ch_tstart + ch->ch_tsize - head;
1126
1127         if (cnt >= n) {
1128                 cnt -= n;
1129                 taddr = ch->ch_taddr + head;
1130                 memcpy_toio(taddr, buf, n);
1131                 head = ch->ch_tstart;
1132                 buf += n;
1133         }
1134
1135         /*
1136          * Move rest of data.
1137          */
1138         taddr = ch->ch_taddr + head;
1139         n = cnt;
1140         memcpy_toio(taddr, buf, n);
1141         head += cnt;
1142
1143         writew(head, &(bs->tx_head));
1144 }
1145
1146 /*
1147  * Retrives the current custom baud rate from FEP memory,
1148  * and returns it back to the user.
1149  * Returns 0 on error.
1150  */
1151 uint dgap_get_custom_baud(struct channel_t *ch)
1152 {
1153         uchar *vaddr;
1154         ulong offset = 0;
1155         uint value = 0;
1156
1157         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC) {
1158                 return 0;
1159         }
1160
1161         if (!ch->ch_bd || ch->ch_bd->magic != DGAP_BOARD_MAGIC) {
1162                 return 0;
1163         }
1164
1165         if (!(ch->ch_bd->bd_flags & BD_FEP5PLUS))
1166                 return 0;
1167
1168         vaddr = ch->ch_bd->re_map_membase;
1169
1170         if (!vaddr)
1171                 return 0;
1172
1173         /*
1174          * Go get from fep mem, what the fep
1175          * believes the custom baud rate is. 
1176          */
1177         offset = ((((*(unsigned short *)(vaddr + ECS_SEG)) << 4) +  
1178                 (ch->ch_portnum * 0x28) + LINE_SPEED));
1179
1180         value = readw(vaddr + offset);
1181         return value;
1182 }
1183
1184
1185 /*
1186  * Calls the firmware to reset this channel.
1187  */
1188 void dgap_firmware_reset_port(struct channel_t *ch)
1189 {
1190         dgap_cmdb(ch, CHRESET, 0, 0, 0);
1191
1192         /*
1193          * Now that the channel is reset, we need to make sure
1194          * all the current settings get reapplied to the port
1195          * in the firmware.
1196          *
1197          * So we will set the driver's cache of firmware
1198          * settings all to 0, and then call param.
1199          */
1200         ch->ch_fepiflag = 0;
1201         ch->ch_fepcflag = 0;
1202         ch->ch_fepoflag = 0;
1203         ch->ch_fepstartc = 0;
1204         ch->ch_fepstopc = 0;
1205         ch->ch_fepastartc = 0;
1206         ch->ch_fepastopc = 0;
1207         ch->ch_mostat = 0;
1208         ch->ch_hflow = 0;
1209 }
1210
1211
1212 /*=======================================================================
1213  *      
1214  *      dgap_param - Set Digi parameters.
1215  *
1216  *              struct tty_struct *     - TTY for port.
1217  *
1218  *=======================================================================*/
1219 int dgap_param(struct tty_struct *tty)
1220 {
1221         struct ktermios *ts;
1222         struct board_t *bd;
1223         struct channel_t *ch;
1224         struct bs_t   *bs;
1225         struct un_t   *un;
1226         u16     head;
1227         u16     cflag;
1228         u16     iflag;
1229         uchar   mval;
1230         uchar   hflow;
1231
1232         if (!tty || tty->magic != TTY_MAGIC)
1233                 return -ENXIO;
1234
1235         un = (struct un_t *) tty->driver_data;
1236         if (!un || un->magic != DGAP_UNIT_MAGIC)
1237                 return -ENXIO;
1238
1239         ch = un->un_ch;
1240         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
1241                 return -ENXIO;
1242
1243         bd = ch->ch_bd;
1244         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
1245                 return -ENXIO;
1246
1247         bs = ch->ch_bs;
1248         if (!bs)
1249                 return -ENXIO;
1250
1251         DPR_PARAM(("param start: tdev: %x cflags: %x oflags: %x iflags: %x\n",
1252                 ch->ch_tun.un_dev, ch->ch_c_cflag, ch->ch_c_oflag, ch->ch_c_iflag));
1253
1254         ts = &tty->termios;
1255
1256         /*
1257          * If baud rate is zero, flush queues, and set mval to drop DTR.
1258          */
1259         if ((ch->ch_c_cflag & (CBAUD)) == 0) {
1260
1261                 /* flush rx */
1262                 head = readw(&(ch->ch_bs->rx_head));
1263                 writew(head, &(ch->ch_bs->rx_tail));
1264
1265                 /* flush tx */
1266                 head = readw(&(ch->ch_bs->tx_head));
1267                 writew(head, &(ch->ch_bs->tx_tail));
1268
1269                 ch->ch_flags |= (CH_BAUD0);
1270
1271                 /* Drop RTS and DTR */
1272                 ch->ch_mval &= ~(D_RTS(ch)|D_DTR(ch));
1273                 mval = D_DTR(ch) | D_RTS(ch);
1274                 ch->ch_baud_info = 0;
1275
1276         } else if (ch->ch_custom_speed && (bd->bd_flags & BD_FEP5PLUS)) {
1277                 /*
1278                  * Tell the fep to do the command
1279                  */
1280
1281                 DPR_PARAM(("param: Want %d speed\n", ch->ch_custom_speed));
1282
1283                 dgap_cmdw_ext(ch, 0xff01, ch->ch_custom_speed, 0);
1284
1285                 /*
1286                  * Now go get from fep mem, what the fep
1287                  * believes the custom baud rate is. 
1288                  */
1289                 ch->ch_baud_info = ch->ch_custom_speed = dgap_get_custom_baud(ch);
1290
1291                 DPR_PARAM(("param: Got %d speed\n", ch->ch_custom_speed));
1292
1293                 /* Handle transition from B0 */   
1294                 if (ch->ch_flags & CH_BAUD0) {
1295                         ch->ch_flags &= ~(CH_BAUD0);
1296                         ch->ch_mval |= (D_RTS(ch)|D_DTR(ch));
1297                 }
1298                 mval = D_DTR(ch) | D_RTS(ch);
1299
1300         } else {
1301                 /*
1302                  * Set baud rate, character size, and parity.
1303                  */
1304
1305
1306                 int iindex = 0;
1307                 int jindex = 0;
1308                 int baud = 0;
1309
1310                 ulong bauds[4][16] = {
1311                         { /* slowbaud */
1312                                 0,      50,     75,     110,
1313                                 134,    150,    200,    300,
1314                                 600,    1200,   1800,   2400,
1315                                 4800,   9600,   19200,  38400 },
1316                         { /* slowbaud & CBAUDEX */
1317                                 0,      57600,  115200, 230400,
1318                                 460800, 150,    200,    921600,
1319                                 600,    1200,   1800,   2400,
1320                                 4800,   9600,   19200,  38400 },
1321                         { /* fastbaud */
1322                                 0,      57600,  76800,  115200,
1323                                 14400,  57600,  230400, 76800,
1324                                 115200, 230400, 28800,  460800,
1325                                 921600, 9600,   19200,  38400 },
1326                         { /* fastbaud & CBAUDEX */
1327                                 0,      57600,  115200, 230400,
1328                                 460800, 150,    200,    921600,
1329                                 600,    1200,   1800,   2400,
1330                                 4800,   9600,   19200,  38400 }
1331                 };
1332
1333                 /* Only use the TXPrint baud rate if the terminal unit is NOT open */
1334                 if (!(ch->ch_tun.un_flags & UN_ISOPEN) && (un->un_type == DGAP_PRINT))
1335                         baud = C_BAUD(ch->ch_pun.un_tty) & 0xff;
1336                 else
1337                         baud = C_BAUD(ch->ch_tun.un_tty) & 0xff;
1338
1339                 if (ch->ch_c_cflag & CBAUDEX)
1340                         iindex = 1;
1341
1342                 if (ch->ch_digi.digi_flags & DIGI_FAST)
1343                         iindex += 2;
1344
1345                 jindex = baud;
1346
1347                 if ((iindex >= 0) && (iindex < 4) && (jindex >= 0) && (jindex < 16)) {
1348                         baud = bauds[iindex][jindex];
1349                 } else {
1350                         DPR_IOCTL(("baud indices were out of range (%d)(%d)",
1351                                 iindex, jindex));
1352                         baud = 0;
1353                 }
1354
1355                 if (baud == 0)  
1356                         baud = 9600;
1357
1358                 ch->ch_baud_info = baud;
1359
1360
1361                 /*
1362                  * CBAUD has bit position 0x1000 set these days to indicate Linux
1363                  * baud rate remap.
1364                  * We use a different bit assignment for high speed.  Clear this
1365                  * bit out while grabbing the parts of "cflag" we want.
1366                  */
1367                 cflag = ch->ch_c_cflag & ((CBAUD ^ CBAUDEX) | PARODD | PARENB | CSTOPB | CSIZE);
1368
1369                 /*
1370                  * HUPCL bit is used by FEP to indicate fast baud
1371                  * table is to be used.
1372                  */
1373                 if ((ch->ch_digi.digi_flags & DIGI_FAST) || (ch->ch_c_cflag & CBAUDEX))
1374                         cflag |= HUPCL;
1375
1376
1377                 if ((ch->ch_c_cflag & CBAUDEX) && !(ch->ch_digi.digi_flags & DIGI_FAST)) {
1378                 /*
1379                  * The below code is trying to guarantee that only baud rates
1380                  * 115200, 230400, 460800, 921600 are remapped.  We use exclusive or
1381                  * because the various baud rates share common bit positions
1382                  * and therefore can't be tested for easily.
1383                  */
1384                         tcflag_t tcflag = (ch->ch_c_cflag & CBAUD) | CBAUDEX;
1385                         int baudpart = 0;
1386
1387                         /* Map high speed requests to index into FEP's baud table */
1388                         switch (tcflag) {
1389                         case B57600 :
1390                                 baudpart = 1;
1391                                 break;
1392 #ifdef B76800
1393                         case B76800 :
1394                                 baudpart = 2;
1395                                 break;
1396 #endif
1397                         case B115200 :
1398                                 baudpart = 3;
1399                                 break;
1400                         case B230400 :
1401                                 baudpart = 9;
1402                                 break;
1403                         case B460800 :
1404                                 baudpart = 11;
1405                                 break;
1406 #ifdef B921600
1407                         case B921600 :
1408                                 baudpart = 12;
1409                                 break;
1410 #endif
1411                         default:
1412                                 baudpart = 0;
1413                         }
1414
1415                         if (baudpart)
1416                                 cflag = (cflag & ~(CBAUD | CBAUDEX)) | baudpart;
1417                 }
1418
1419                 cflag &= 0xffff;
1420
1421                 if (cflag != ch->ch_fepcflag) {
1422                         ch->ch_fepcflag = (u16) (cflag & 0xffff);
1423
1424                         /* Okay to have channel and board locks held calling this */
1425                         dgap_cmdw(ch, SCFLAG, (u16) cflag, 0);
1426                 }
1427
1428                 /* Handle transition from B0 */   
1429                 if (ch->ch_flags & CH_BAUD0) {
1430                         ch->ch_flags &= ~(CH_BAUD0);
1431                         ch->ch_mval |= (D_RTS(ch)|D_DTR(ch));
1432                 }
1433                 mval = D_DTR(ch) | D_RTS(ch);
1434         }
1435
1436         /*
1437          * Get input flags.
1438          */
1439         iflag = ch->ch_c_iflag & (IGNBRK | BRKINT | IGNPAR | PARMRK | INPCK | ISTRIP | IXON | IXANY | IXOFF);
1440
1441         if ((ch->ch_startc == _POSIX_VDISABLE) || (ch->ch_stopc == _POSIX_VDISABLE)) {
1442                 iflag &= ~(IXON | IXOFF);
1443                 ch->ch_c_iflag &= ~(IXON | IXOFF);
1444         }
1445
1446         /*
1447          * Only the IBM Xr card can switch between
1448          * 232 and 422 modes on the fly
1449          */
1450         if (bd->device == PCI_DEVICE_XR_IBM_DID) {
1451                 if (ch->ch_digi.digi_flags & DIGI_422)
1452                         dgap_cmdb(ch, SCOMMODE, MODE_422, 0, 0);
1453                 else
1454                         dgap_cmdb(ch, SCOMMODE, MODE_232, 0, 0);
1455         }
1456
1457         if (ch->ch_digi.digi_flags & DIGI_ALTPIN)
1458                 iflag |= IALTPIN ;
1459
1460         if (iflag != ch->ch_fepiflag) {
1461                 ch->ch_fepiflag = iflag;
1462
1463                 /* Okay to have channel and board locks held calling this */
1464                 dgap_cmdw(ch, SIFLAG, (u16) ch->ch_fepiflag, 0);
1465         }
1466
1467         /*
1468          * Select hardware handshaking.
1469          */
1470         hflow = 0;
1471
1472         if (ch->ch_c_cflag & CRTSCTS) {
1473                 hflow |= (D_RTS(ch) | D_CTS(ch));
1474         }
1475         if (ch->ch_digi.digi_flags & RTSPACE)
1476                 hflow |= D_RTS(ch);
1477         if (ch->ch_digi.digi_flags & DTRPACE)
1478                 hflow |= D_DTR(ch);  
1479         if (ch->ch_digi.digi_flags & CTSPACE)
1480                 hflow |= D_CTS(ch);
1481         if (ch->ch_digi.digi_flags & DSRPACE)
1482                 hflow |= D_DSR(ch);
1483         if (ch->ch_digi.digi_flags & DCDPACE)
1484                 hflow |= D_CD(ch);
1485
1486         if (hflow != ch->ch_hflow) {
1487                 ch->ch_hflow = hflow;
1488
1489                 /* Okay to have channel and board locks held calling this */
1490                 dgap_cmdb(ch, SHFLOW, (uchar) hflow, 0xff, 0);
1491         }
1492
1493
1494         /*
1495          * Set RTS and/or DTR Toggle if needed, but only if product is FEP5+ based.
1496          */
1497         if (bd->bd_flags & BD_FEP5PLUS) {
1498                 u16 hflow2 = 0;
1499                 if (ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE) {
1500                         hflow2 |= (D_RTS(ch));
1501                 }
1502                 if (ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE) {
1503                         hflow2 |= (D_DTR(ch));
1504                 }
1505
1506                 dgap_cmdw_ext(ch, 0xff03, hflow2, 0);
1507         }
1508
1509         /*
1510          * Set modem control lines.  
1511          */
1512
1513         mval ^= ch->ch_mforce & (mval ^ ch->ch_mval);
1514
1515         DPR_PARAM(("dgap_param: mval: %x ch_mforce: %x ch_mval: %x ch_mostat: %x\n",
1516                 mval, ch->ch_mforce, ch->ch_mval, ch->ch_mostat));
1517
1518         if (ch->ch_mostat ^ mval) {
1519                 ch->ch_mostat = mval;
1520
1521                 /* Okay to have channel and board locks held calling this */
1522                 DPR_PARAM(("dgap_param: Sending SMODEM mval: %x\n", mval));
1523                 dgap_cmdb(ch, SMODEM, (uchar) mval, D_RTS(ch)|D_DTR(ch), 0);
1524         }
1525
1526         /*
1527          * Read modem signals, and then call carrier function.             
1528          */
1529         ch->ch_mistat = readb(&(bs->m_stat));
1530         dgap_carrier(ch);
1531
1532         /*      
1533          * Set the start and stop characters.
1534          */
1535         if (ch->ch_startc != ch->ch_fepstartc || ch->ch_stopc != ch->ch_fepstopc) {
1536                 ch->ch_fepstartc = ch->ch_startc;
1537                 ch->ch_fepstopc =  ch->ch_stopc;
1538
1539                 /* Okay to have channel and board locks held calling this */
1540                 dgap_cmdb(ch, SFLOWC, ch->ch_fepstartc, ch->ch_fepstopc, 0);
1541         }
1542
1543         /*
1544          * Set the Auxiliary start and stop characters.
1545          */     
1546         if (ch->ch_astartc != ch->ch_fepastartc || ch->ch_astopc != ch->ch_fepastopc) {
1547                 ch->ch_fepastartc = ch->ch_astartc;
1548                 ch->ch_fepastopc = ch->ch_astopc;
1549
1550                 /* Okay to have channel and board locks held calling this */
1551                 dgap_cmdb(ch, SAFLOWC, ch->ch_fepastartc, ch->ch_fepastopc, 0);
1552         }
1553
1554         DPR_PARAM(("param finish\n"));
1555
1556         return 0;
1557 }
1558
1559
1560 /*
1561  * dgap_parity_scan()
1562  *
1563  * Convert the FEP5 way of reporting parity errors and breaks into
1564  * the Linux line discipline way.
1565  */
1566 void dgap_parity_scan(struct channel_t *ch, unsigned char *cbuf, unsigned char *fbuf, int *len)
1567 {
1568         int l = *len;
1569         int count = 0;
1570         unsigned char *in, *cout, *fout;
1571         unsigned char c;
1572
1573         in = cbuf;
1574         cout = cbuf;
1575         fout = fbuf;
1576
1577         DPR_PSCAN(("dgap_parity_scan start\n"));
1578
1579         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
1580                 return;
1581
1582         while (l--) {
1583                 c = *in++;
1584                 switch (ch->pscan_state) {
1585                 default:
1586                         /* reset to sanity and fall through */
1587                         ch->pscan_state = 0;
1588
1589                 case 0:
1590                         /* No FF seen yet */
1591                         if (c == (unsigned char) '\377') {
1592                                 /* delete this character from stream */
1593                                 ch->pscan_state = 1;
1594                         } else {
1595                                 *cout++ = c;
1596                                 *fout++ = TTY_NORMAL;
1597                                 count += 1;
1598                         }
1599                         break;
1600
1601                 case 1:
1602                         /* first FF seen */
1603                         if (c == (unsigned char) '\377') {
1604                                 /* doubled ff, transform to single ff */
1605                                 *cout++ = c;
1606                                 *fout++ = TTY_NORMAL;
1607                                 count += 1;
1608                                 ch->pscan_state = 0;
1609                         } else {
1610                                 /* save value examination in next state */
1611                                 ch->pscan_savechar = c;
1612                                 ch->pscan_state = 2; 
1613                         }
1614                         break;
1615
1616                 case 2:
1617                         /* third character of ff sequence */
1618
1619                         *cout++ = c;
1620
1621                         if (ch->pscan_savechar == 0x0) {
1622
1623                                 if (c == 0x0) {
1624                                         DPR_PSCAN(("dgap_parity_scan in 3rd char of ff seq. c: %x setting break.\n", c));
1625                                         ch->ch_err_break++;
1626                                         *fout++ = TTY_BREAK;
1627                                 }
1628                                 else {
1629                                         DPR_PSCAN(("dgap_parity_scan in 3rd char of ff seq. c: %x setting parity.\n", c));
1630                                         ch->ch_err_parity++;
1631                                         *fout++ = TTY_PARITY;
1632                                 }
1633                         }
1634                         else {
1635                                 DPR_PSCAN(("%s:%d Logic Error.\n", __FILE__, __LINE__));
1636                         }
1637
1638                         count += 1;
1639                         ch->pscan_state = 0;
1640                 }       
1641         }
1642         *len = count;
1643         DPR_PSCAN(("dgap_parity_scan finish\n"));
1644 }
1645
1646
1647
1648
1649 /*=======================================================================
1650  *
1651  *      dgap_event - FEP to host event processing routine.
1652  *
1653  *              bd     - Board of current event.
1654  *
1655  *=======================================================================*/
1656 static int dgap_event(struct board_t *bd)
1657 {
1658         struct channel_t *ch;
1659         ulong           lock_flags;
1660         ulong           lock_flags2;
1661         struct bs_t     *bs;
1662         uchar           *event;
1663         uchar           *vaddr = NULL;
1664         struct ev_t     *eaddr = NULL;
1665         uint            head;
1666         uint            tail;
1667         int             port;
1668         int             reason;
1669         int             modem;
1670         int             b1;
1671
1672         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
1673                 return -ENXIO;
1674
1675         DGAP_LOCK(bd->bd_lock, lock_flags);
1676
1677         vaddr = bd->re_map_membase;
1678
1679         if (!vaddr) {
1680                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
1681                 return -ENXIO;
1682         }
1683
1684         eaddr = (struct ev_t *) (vaddr + EVBUF);
1685
1686         /* Get our head and tail */
1687         head = readw(&(eaddr->ev_head));
1688         tail = readw(&(eaddr->ev_tail));
1689
1690         /*
1691          * Forget it if pointers out of range.
1692          */
1693
1694         if (head >= EVMAX - EVSTART || tail >= EVMAX - EVSTART ||
1695             (head | tail) & 03) {
1696                 DPR_EVENT(("should be calling xxfail %d\n", __LINE__));
1697                 /* Let go of board lock */
1698                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
1699                 return -ENXIO;
1700         }
1701
1702         /*
1703          * Loop to process all the events in the buffer.
1704          */
1705         while (tail != head) {
1706
1707                 /*
1708                  * Get interrupt information.
1709                  */
1710
1711                 event = bd->re_map_membase + tail + EVSTART;
1712
1713                 port   = event[0];
1714                 reason = event[1];
1715                 modem  = event[2];
1716                 b1     = event[3];
1717
1718                 DPR_EVENT(("event: jiffies: %ld port: %d reason: %x modem: %x\n",
1719                         jiffies, port, reason, modem));
1720
1721                 /*
1722                  * Make sure the interrupt is valid.
1723                  */
1724                 if ( port >= bd->nasync) {
1725                         goto next;
1726                 }
1727
1728                 if (!(reason & (IFMODEM | IFBREAK | IFTLW | IFTEM | IFDATA))) {
1729                         goto next;
1730                 }
1731
1732                 ch = bd->channels[port];
1733
1734                 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC) {
1735                         goto next;
1736                 }
1737
1738                 /*
1739                  * If we have made it here, the event was valid.
1740                  * Lock down the channel.
1741                  */
1742                 DGAP_LOCK(ch->ch_lock, lock_flags2);
1743
1744                 bs = ch->ch_bs;
1745
1746                 if (!bs) {
1747                         DGAP_UNLOCK(ch->ch_lock, lock_flags2);
1748                         goto next;
1749                 }
1750
1751                 /*
1752                  * Process received data.
1753                  */
1754                 if (reason & IFDATA) {
1755
1756                         /*
1757                          * ALL LOCKS *MUST* BE DROPPED BEFORE CALLING INPUT!
1758                          * input could send some data to ld, which in turn
1759                          * could do a callback to one of our other functions.
1760                          */
1761                         DGAP_UNLOCK(ch->ch_lock, lock_flags2);
1762                         DGAP_UNLOCK(bd->bd_lock, lock_flags);
1763
1764                         dgap_input(ch);
1765
1766                         DGAP_LOCK(bd->bd_lock, lock_flags);
1767                         DGAP_LOCK(ch->ch_lock, lock_flags2);
1768
1769                         if (ch->ch_flags & CH_RACTIVE)
1770                                 ch->ch_flags |= CH_RENABLE;
1771                         else
1772                                 writeb(1, &(bs->idata));
1773
1774                         if (ch->ch_flags & CH_RWAIT) {
1775                                 ch->ch_flags &= ~CH_RWAIT;
1776
1777                                 wake_up_interruptible(&ch->ch_tun.un_flags_wait);
1778                         }
1779                 }
1780
1781                 /*
1782                  * Process Modem change signals. 
1783                  */
1784                 if (reason & IFMODEM) {
1785                         ch->ch_mistat = modem;
1786                         dgap_carrier(ch);
1787                 }
1788
1789                 /*
1790                  * Process break.
1791                  */
1792                 if (reason & IFBREAK) {
1793
1794                         DPR_EVENT(("got IFBREAK\n"));
1795
1796                         if (ch->ch_tun.un_tty) {
1797                                 /* A break has been indicated */
1798                                 ch->ch_err_break++;
1799                                 tty_buffer_request_room(ch->ch_tun.un_tty->port, 1);
1800                                 tty_insert_flip_char(ch->ch_tun.un_tty->port, 0, TTY_BREAK);
1801                                 tty_flip_buffer_push(ch->ch_tun.un_tty->port);
1802                         }
1803                 }
1804
1805                 /*
1806                  * Process Transmit low.
1807                  */
1808                 if (reason & IFTLW) {
1809
1810                         DPR_EVENT(("event: got low event\n"));
1811
1812                         if (ch->ch_tun.un_flags & UN_LOW) {
1813                                 ch->ch_tun.un_flags &= ~UN_LOW;
1814
1815                                 if (ch->ch_tun.un_flags & UN_ISOPEN) {
1816                                         if ((ch->ch_tun.un_tty->flags & 
1817                                            (1 << TTY_DO_WRITE_WAKEUP)) &&
1818 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,31)
1819                                                 ch->ch_tun.un_tty->ldisc->ops->write_wakeup)
1820 #else
1821                                                 ch->ch_tun.un_tty->ldisc.ops->write_wakeup)
1822 #endif
1823                                         {
1824                                                 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
1825                                                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
1826 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,31)
1827                                                 (ch->ch_tun.un_tty->ldisc->ops->write_wakeup)(ch->ch_tun.un_tty);
1828 #else
1829                                                 (ch->ch_tun.un_tty->ldisc.ops->write_wakeup)(ch->ch_tun.un_tty);
1830 #endif
1831                                                 DGAP_LOCK(bd->bd_lock, lock_flags);
1832                                                 DGAP_LOCK(ch->ch_lock, lock_flags2);
1833                                         }
1834                                         wake_up_interruptible(&ch->ch_tun.un_tty->write_wait);
1835                                         wake_up_interruptible(&ch->ch_tun.un_flags_wait);
1836
1837                                         DPR_EVENT(("event: Got low event. jiffies: %lu\n", jiffies));
1838                                 }
1839                         }
1840
1841                         if (ch->ch_pun.un_flags & UN_LOW) {
1842                                 ch->ch_pun.un_flags &= ~UN_LOW;
1843                                 if (ch->ch_pun.un_flags & UN_ISOPEN) {
1844                                         if ((ch->ch_pun.un_tty->flags & 
1845                                            (1 << TTY_DO_WRITE_WAKEUP)) &&
1846 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,31)
1847                                                 ch->ch_pun.un_tty->ldisc->ops->write_wakeup)
1848 #else
1849                                                 ch->ch_pun.un_tty->ldisc.ops->write_wakeup)
1850 #endif
1851                                         {
1852                                                 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
1853                                                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
1854 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,31)
1855                                                 (ch->ch_pun.un_tty->ldisc->ops->write_wakeup)(ch->ch_pun.un_tty);
1856 #else
1857                                                 (ch->ch_pun.un_tty->ldisc.ops->write_wakeup)(ch->ch_pun.un_tty);
1858 #endif
1859                                                 DGAP_LOCK(bd->bd_lock, lock_flags);
1860                                                 DGAP_LOCK(ch->ch_lock, lock_flags2);
1861                                         }
1862                                         wake_up_interruptible(&ch->ch_pun.un_tty->write_wait);
1863                                         wake_up_interruptible(&ch->ch_pun.un_flags_wait);
1864                                 }
1865                         }
1866
1867                         if (ch->ch_flags & CH_WLOW) {
1868                                 ch->ch_flags &= ~CH_WLOW;
1869                                 wake_up_interruptible(&ch->ch_flags_wait);
1870                         }
1871                 }
1872
1873                 /*
1874                  * Process Transmit empty.
1875                  */
1876                 if (reason & IFTEM) {
1877                         DPR_EVENT(("event: got empty event\n"));
1878
1879                         if (ch->ch_tun.un_flags & UN_EMPTY) {
1880                                 ch->ch_tun.un_flags &= ~UN_EMPTY;
1881                                 if (ch->ch_tun.un_flags & UN_ISOPEN) {
1882                                         if ((ch->ch_tun.un_tty->flags & 
1883                                            (1 << TTY_DO_WRITE_WAKEUP)) &&
1884 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,31)
1885                                                 ch->ch_tun.un_tty->ldisc->ops->write_wakeup)
1886 #else
1887                                                 ch->ch_tun.un_tty->ldisc.ops->write_wakeup)
1888 #endif
1889                                         {
1890                                                 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
1891                                                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
1892 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,31)
1893                                                 (ch->ch_tun.un_tty->ldisc->ops->write_wakeup)(ch->ch_tun.un_tty);
1894 #else
1895                                                 (ch->ch_tun.un_tty->ldisc.ops->write_wakeup)(ch->ch_tun.un_tty);
1896 #endif
1897                                                 DGAP_LOCK(bd->bd_lock, lock_flags);
1898                                                 DGAP_LOCK(ch->ch_lock, lock_flags2);
1899                                         }
1900                                         wake_up_interruptible(&ch->ch_tun.un_tty->write_wait);
1901                                         wake_up_interruptible(&ch->ch_tun.un_flags_wait);
1902                                 }
1903                         }
1904
1905                         if (ch->ch_pun.un_flags & UN_EMPTY) {
1906                                 ch->ch_pun.un_flags &= ~UN_EMPTY;
1907                                 if (ch->ch_pun.un_flags & UN_ISOPEN) {
1908                                         if ((ch->ch_pun.un_tty->flags & 
1909                                            (1 << TTY_DO_WRITE_WAKEUP)) &&
1910 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,31)
1911                                                 ch->ch_pun.un_tty->ldisc->ops->write_wakeup)
1912 #else
1913                                                 ch->ch_pun.un_tty->ldisc.ops->write_wakeup)
1914 #endif
1915                                         {
1916                                                 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
1917                                                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
1918 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,31)
1919                                                 (ch->ch_pun.un_tty->ldisc->ops->write_wakeup)(ch->ch_pun.un_tty);
1920 #else
1921                                                 (ch->ch_pun.un_tty->ldisc.ops->write_wakeup)(ch->ch_pun.un_tty);
1922 #endif
1923                                                 DGAP_LOCK(bd->bd_lock, lock_flags);
1924                                                 DGAP_LOCK(ch->ch_lock, lock_flags2);
1925                                         }
1926                                         wake_up_interruptible(&ch->ch_pun.un_tty->write_wait);
1927                                         wake_up_interruptible(&ch->ch_pun.un_flags_wait);
1928                                 }
1929                         }
1930
1931
1932                         if (ch->ch_flags & CH_WEMPTY) {
1933                                 ch->ch_flags &= ~CH_WEMPTY;
1934                                 wake_up_interruptible(&ch->ch_flags_wait);
1935                         }
1936                 }
1937
1938                 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
1939
1940 next:
1941                 tail = (tail + 4) & (EVMAX - EVSTART - 4);
1942         }
1943
1944         writew(tail, &(eaddr->ev_tail));
1945         DGAP_UNLOCK(bd->bd_lock, lock_flags);
1946
1947         return 0;
1948 }