Merge remote-tracking branch 'spi/fix/pxa' into spi-linus
[cascardo/linux.git] / 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
1236         un = (struct un_t *) tty->driver_data;
1237         if (!un || un->magic != DGAP_UNIT_MAGIC) {
1238                 return (-ENXIO);
1239         }
1240
1241         ch = un->un_ch;
1242         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC) {
1243                 return (-ENXIO);
1244         }
1245
1246         bd = ch->ch_bd;
1247         if (!bd || bd->magic != DGAP_BOARD_MAGIC) {
1248                 return (-ENXIO);
1249         }
1250
1251         bs = ch->ch_bs;
1252         if (bs == 0) {
1253                 return (-ENXIO);
1254         }
1255
1256         DPR_PARAM(("param start: tdev: %x cflags: %x oflags: %x iflags: %x\n",
1257                 ch->ch_tun.un_dev, ch->ch_c_cflag, ch->ch_c_oflag, ch->ch_c_iflag));
1258
1259         ts = &tty->termios;
1260
1261         /*
1262          * If baud rate is zero, flush queues, and set mval to drop DTR.
1263          */
1264         if ((ch->ch_c_cflag & (CBAUD)) == 0) {
1265
1266                 /* flush rx */
1267                 head = readw(&(ch->ch_bs->rx_head));
1268                 writew(head, &(ch->ch_bs->rx_tail));
1269
1270                 /* flush tx */
1271                 head = readw(&(ch->ch_bs->tx_head));
1272                 writew(head, &(ch->ch_bs->tx_tail));
1273
1274                 ch->ch_flags |= (CH_BAUD0);
1275
1276                 /* Drop RTS and DTR */
1277                 ch->ch_mval &= ~(D_RTS(ch)|D_DTR(ch));
1278                 mval = D_DTR(ch) | D_RTS(ch);
1279                 ch->ch_baud_info = 0;
1280
1281         } else if (ch->ch_custom_speed && (bd->bd_flags & BD_FEP5PLUS)) {
1282                 /*
1283                  * Tell the fep to do the command
1284                  */
1285
1286                 DPR_PARAM(("param: Want %d speed\n", ch->ch_custom_speed));
1287
1288                 dgap_cmdw_ext(ch, 0xff01, ch->ch_custom_speed, 0);
1289
1290                 /*
1291                  * Now go get from fep mem, what the fep
1292                  * believes the custom baud rate is. 
1293                  */
1294                 ch->ch_baud_info = ch->ch_custom_speed = dgap_get_custom_baud(ch);
1295
1296                 DPR_PARAM(("param: Got %d speed\n", ch->ch_custom_speed));
1297
1298                 /* Handle transition from B0 */   
1299                 if (ch->ch_flags & CH_BAUD0) {
1300                         ch->ch_flags &= ~(CH_BAUD0);
1301                         ch->ch_mval |= (D_RTS(ch)|D_DTR(ch));
1302                 }
1303                 mval = D_DTR(ch) | D_RTS(ch);
1304
1305         } else {
1306                 /*
1307                  * Set baud rate, character size, and parity.
1308                  */
1309
1310
1311                 int iindex = 0;
1312                 int jindex = 0;
1313                 int baud = 0;
1314
1315                 ulong bauds[4][16] = {
1316                         { /* slowbaud */
1317                                 0,      50,     75,     110,
1318                                 134,    150,    200,    300,
1319                                 600,    1200,   1800,   2400,
1320                                 4800,   9600,   19200,  38400 },
1321                         { /* slowbaud & CBAUDEX */
1322                                 0,      57600,  115200, 230400,
1323                                 460800, 150,    200,    921600,
1324                                 600,    1200,   1800,   2400,
1325                                 4800,   9600,   19200,  38400 },
1326                         { /* fastbaud */
1327                                 0,      57600,  76800,  115200,
1328                                 14400,  57600,  230400, 76800,
1329                                 115200, 230400, 28800,  460800,
1330                                 921600, 9600,   19200,  38400 },
1331                         { /* fastbaud & CBAUDEX */
1332                                 0,      57600,  115200, 230400,
1333                                 460800, 150,    200,    921600,
1334                                 600,    1200,   1800,   2400,
1335                                 4800,   9600,   19200,  38400 }
1336                 };
1337
1338                 /* Only use the TXPrint baud rate if the terminal unit is NOT open */
1339                 if (!(ch->ch_tun.un_flags & UN_ISOPEN) && (un->un_type == DGAP_PRINT))
1340                         baud = C_BAUD(ch->ch_pun.un_tty) & 0xff;
1341                 else
1342                         baud = C_BAUD(ch->ch_tun.un_tty) & 0xff;
1343
1344                 if (ch->ch_c_cflag & CBAUDEX)
1345                         iindex = 1;
1346
1347                 if (ch->ch_digi.digi_flags & DIGI_FAST)
1348                         iindex += 2;
1349
1350                 jindex = baud;
1351
1352                 if ((iindex >= 0) && (iindex < 4) && (jindex >= 0) && (jindex < 16)) {
1353                         baud = bauds[iindex][jindex];
1354                 } else {
1355                         DPR_IOCTL(("baud indices were out of range (%d)(%d)",
1356                                 iindex, jindex));
1357                         baud = 0;
1358                 }
1359
1360                 if (baud == 0)  
1361                         baud = 9600;
1362
1363                 ch->ch_baud_info = baud;
1364
1365
1366                 /*
1367                  * CBAUD has bit position 0x1000 set these days to indicate Linux
1368                  * baud rate remap.
1369                  * We use a different bit assignment for high speed.  Clear this
1370                  * bit out while grabbing the parts of "cflag" we want.
1371                  */
1372                 cflag = ch->ch_c_cflag & ((CBAUD ^ CBAUDEX) | PARODD | PARENB | CSTOPB | CSIZE);
1373
1374                 /*
1375                  * HUPCL bit is used by FEP to indicate fast baud
1376                  * table is to be used.
1377                  */
1378                 if ((ch->ch_digi.digi_flags & DIGI_FAST) || (ch->ch_c_cflag & CBAUDEX))
1379                         cflag |= HUPCL;
1380
1381
1382                 if ((ch->ch_c_cflag & CBAUDEX) && !(ch->ch_digi.digi_flags & DIGI_FAST)) {
1383                 /*
1384                  * The below code is trying to guarantee that only baud rates
1385                  * 115200, 230400, 460800, 921600 are remapped.  We use exclusive or
1386                  * because the various baud rates share common bit positions
1387                  * and therefore can't be tested for easily.
1388                  */
1389                         tcflag_t tcflag = (ch->ch_c_cflag & CBAUD) | CBAUDEX;
1390                         int baudpart = 0;
1391
1392                         /* Map high speed requests to index into FEP's baud table */
1393                         switch (tcflag) {
1394                         case B57600 :
1395                                 baudpart = 1;
1396                                 break;
1397 #ifdef B76800
1398                         case B76800 :
1399                                 baudpart = 2;
1400                                 break;
1401 #endif
1402                         case B115200 :
1403                                 baudpart = 3;
1404                                 break;
1405                         case B230400 :
1406                                 baudpart = 9;
1407                                 break;
1408                         case B460800 :
1409                                 baudpart = 11;
1410                                 break;
1411 #ifdef B921600
1412                         case B921600 :
1413                                 baudpart = 12;
1414                                 break;
1415 #endif
1416                         default:
1417                                 baudpart = 0;
1418                         }
1419
1420                         if (baudpart)
1421                                 cflag = (cflag & ~(CBAUD | CBAUDEX)) | baudpart;
1422                 }
1423
1424                 cflag &= 0xffff;
1425
1426                 if (cflag != ch->ch_fepcflag) {
1427                         ch->ch_fepcflag = (u16) (cflag & 0xffff);
1428
1429                         /* Okay to have channel and board locks held calling this */
1430                         dgap_cmdw(ch, SCFLAG, (u16) cflag, 0);
1431                 }
1432
1433                 /* Handle transition from B0 */   
1434                 if (ch->ch_flags & CH_BAUD0) {
1435                         ch->ch_flags &= ~(CH_BAUD0);
1436                         ch->ch_mval |= (D_RTS(ch)|D_DTR(ch));
1437                 }
1438                 mval = D_DTR(ch) | D_RTS(ch);
1439         }
1440
1441         /*
1442          * Get input flags.
1443          */
1444         iflag = ch->ch_c_iflag & (IGNBRK | BRKINT | IGNPAR | PARMRK | INPCK | ISTRIP | IXON | IXANY | IXOFF);
1445
1446         if ((ch->ch_startc == _POSIX_VDISABLE) || (ch->ch_stopc == _POSIX_VDISABLE)) {
1447                 iflag &= ~(IXON | IXOFF);
1448                 ch->ch_c_iflag &= ~(IXON | IXOFF);
1449         }
1450
1451         /*
1452          * Only the IBM Xr card can switch between
1453          * 232 and 422 modes on the fly
1454          */
1455         if (bd->device == PCI_DEVICE_XR_IBM_DID) {
1456                 if (ch->ch_digi.digi_flags & DIGI_422)
1457                         dgap_cmdb(ch, SCOMMODE, MODE_422, 0, 0);
1458                 else
1459                         dgap_cmdb(ch, SCOMMODE, MODE_232, 0, 0);
1460         }
1461
1462         if (ch->ch_digi.digi_flags & DIGI_ALTPIN)
1463                 iflag |= IALTPIN ;
1464
1465         if (iflag != ch->ch_fepiflag) {
1466                 ch->ch_fepiflag = iflag;
1467
1468                 /* Okay to have channel and board locks held calling this */
1469                 dgap_cmdw(ch, SIFLAG, (u16) ch->ch_fepiflag, 0);
1470         }
1471
1472         /*
1473          * Select hardware handshaking.
1474          */
1475         hflow = 0;
1476
1477         if (ch->ch_c_cflag & CRTSCTS) {
1478                 hflow |= (D_RTS(ch) | D_CTS(ch));
1479         }
1480         if (ch->ch_digi.digi_flags & RTSPACE)
1481                 hflow |= D_RTS(ch);
1482         if (ch->ch_digi.digi_flags & DTRPACE)
1483                 hflow |= D_DTR(ch);  
1484         if (ch->ch_digi.digi_flags & CTSPACE)
1485                 hflow |= D_CTS(ch);
1486         if (ch->ch_digi.digi_flags & DSRPACE)
1487                 hflow |= D_DSR(ch);
1488         if (ch->ch_digi.digi_flags & DCDPACE)
1489                 hflow |= D_CD(ch);
1490
1491         if (hflow != ch->ch_hflow) {
1492                 ch->ch_hflow = hflow;
1493
1494                 /* Okay to have channel and board locks held calling this */
1495                 dgap_cmdb(ch, SHFLOW, (uchar) hflow, 0xff, 0);
1496         }
1497
1498
1499         /*
1500          * Set RTS and/or DTR Toggle if needed, but only if product is FEP5+ based.
1501          */
1502         if (bd->bd_flags & BD_FEP5PLUS) {
1503                 u16 hflow2 = 0;
1504                 if (ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE) {
1505                         hflow2 |= (D_RTS(ch));
1506                 }
1507                 if (ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE) {
1508                         hflow2 |= (D_DTR(ch));
1509                 }
1510
1511                 dgap_cmdw_ext(ch, 0xff03, hflow2, 0);
1512         }
1513
1514         /*
1515          * Set modem control lines.  
1516          */
1517
1518         mval ^= ch->ch_mforce & (mval ^ ch->ch_mval);
1519
1520         DPR_PARAM(("dgap_param: mval: %x ch_mforce: %x ch_mval: %x ch_mostat: %x\n",
1521                 mval, ch->ch_mforce, ch->ch_mval, ch->ch_mostat));
1522
1523         if (ch->ch_mostat ^ mval) {
1524                 ch->ch_mostat = mval;
1525
1526                 /* Okay to have channel and board locks held calling this */
1527                 DPR_PARAM(("dgap_param: Sending SMODEM mval: %x\n", mval));
1528                 dgap_cmdb(ch, SMODEM, (uchar) mval, D_RTS(ch)|D_DTR(ch), 0);
1529         }
1530
1531         /*
1532          * Read modem signals, and then call carrier function.             
1533          */
1534         ch->ch_mistat = readb(&(bs->m_stat));
1535         dgap_carrier(ch);
1536
1537         /*      
1538          * Set the start and stop characters.
1539          */
1540         if (ch->ch_startc != ch->ch_fepstartc || ch->ch_stopc != ch->ch_fepstopc) {
1541                 ch->ch_fepstartc = ch->ch_startc;
1542                 ch->ch_fepstopc =  ch->ch_stopc;
1543
1544                 /* Okay to have channel and board locks held calling this */
1545                 dgap_cmdb(ch, SFLOWC, ch->ch_fepstartc, ch->ch_fepstopc, 0);
1546         }
1547
1548         /*
1549          * Set the Auxiliary start and stop characters.
1550          */     
1551         if (ch->ch_astartc != ch->ch_fepastartc || ch->ch_astopc != ch->ch_fepastopc) {
1552                 ch->ch_fepastartc = ch->ch_astartc;
1553                 ch->ch_fepastopc = ch->ch_astopc;
1554
1555                 /* Okay to have channel and board locks held calling this */
1556                 dgap_cmdb(ch, SAFLOWC, ch->ch_fepastartc, ch->ch_fepastopc, 0);
1557         }
1558
1559         DPR_PARAM(("param finish\n"));
1560
1561         return (0);
1562 }
1563
1564
1565 /*
1566  * dgap_parity_scan()
1567  *
1568  * Convert the FEP5 way of reporting parity errors and breaks into
1569  * the Linux line discipline way.
1570  */
1571 void dgap_parity_scan(struct channel_t *ch, unsigned char *cbuf, unsigned char *fbuf, int *len)
1572 {
1573         int l = *len;
1574         int count = 0;
1575         unsigned char *in, *cout, *fout;
1576         unsigned char c;
1577
1578         in = cbuf;
1579         cout = cbuf;
1580         fout = fbuf;
1581
1582         DPR_PSCAN(("dgap_parity_scan start\n"));
1583
1584         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
1585                 return;
1586
1587         while (l--) {
1588                 c = *in++;
1589                 switch (ch->pscan_state) {
1590                 default:
1591                         /* reset to sanity and fall through */
1592                         ch->pscan_state = 0;
1593
1594                 case 0:
1595                         /* No FF seen yet */
1596                         if (c == (unsigned char) '\377') {
1597                                 /* delete this character from stream */
1598                                 ch->pscan_state = 1;
1599                         } else {
1600                                 *cout++ = c;
1601                                 *fout++ = TTY_NORMAL;
1602                                 count += 1;
1603                         }
1604                         break;
1605
1606                 case 1:
1607                         /* first FF seen */
1608                         if (c == (unsigned char) '\377') {
1609                                 /* doubled ff, transform to single ff */
1610                                 *cout++ = c;
1611                                 *fout++ = TTY_NORMAL;
1612                                 count += 1;
1613                                 ch->pscan_state = 0;
1614                         } else {
1615                                 /* save value examination in next state */
1616                                 ch->pscan_savechar = c;
1617                                 ch->pscan_state = 2; 
1618                         }
1619                         break;
1620
1621                 case 2:
1622                         /* third character of ff sequence */
1623
1624                         *cout++ = c;
1625
1626                         if (ch->pscan_savechar == 0x0) {
1627
1628                                 if (c == 0x0) {
1629                                         DPR_PSCAN(("dgap_parity_scan in 3rd char of ff seq. c: %x setting break.\n", c));
1630                                         ch->ch_err_break++;
1631                                         *fout++ = TTY_BREAK;
1632                                 }
1633                                 else {
1634                                         DPR_PSCAN(("dgap_parity_scan in 3rd char of ff seq. c: %x setting parity.\n", c));
1635                                         ch->ch_err_parity++;
1636                                         *fout++ = TTY_PARITY;
1637                                 }
1638                         }
1639                         else {
1640                                 DPR_PSCAN(("%s:%d Logic Error.\n", __FILE__, __LINE__));
1641                         }
1642
1643                         count += 1;
1644                         ch->pscan_state = 0;
1645                 }       
1646         }
1647         *len = count;
1648         DPR_PSCAN(("dgap_parity_scan finish\n"));
1649 }
1650
1651
1652
1653
1654 /*=======================================================================
1655  *
1656  *      dgap_event - FEP to host event processing routine.
1657  *
1658  *              bd     - Board of current event.
1659  *
1660  *=======================================================================*/
1661 static int dgap_event(struct board_t *bd)
1662 {
1663         struct channel_t *ch;
1664         ulong           lock_flags;
1665         ulong           lock_flags2;
1666         struct bs_t     *bs;
1667         uchar           *event;
1668         uchar           *vaddr = NULL;
1669         struct ev_t     *eaddr = NULL;
1670         uint            head;
1671         uint            tail;
1672         int             port;
1673         int             reason;
1674         int             modem;
1675         int             b1;
1676
1677         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
1678                 return (-ENXIO);
1679
1680         DGAP_LOCK(bd->bd_lock, lock_flags);
1681
1682         vaddr = bd->re_map_membase;
1683
1684         if (!vaddr) {
1685                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
1686                 return (-ENXIO);
1687         }
1688
1689         eaddr = (struct ev_t *) (vaddr + EVBUF);
1690
1691         /* Get our head and tail */
1692         head = readw(&(eaddr->ev_head));
1693         tail = readw(&(eaddr->ev_tail));
1694
1695         /*
1696          * Forget it if pointers out of range.
1697          */
1698
1699         if (head >= EVMAX - EVSTART || tail >= EVMAX - EVSTART ||
1700             (head | tail) & 03) {
1701                 DPR_EVENT(("should be calling xxfail %d\n", __LINE__));
1702                 /* Let go of board lock */
1703                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
1704                 return (-ENXIO);
1705         }
1706
1707         /*
1708          * Loop to process all the events in the buffer.
1709          */
1710         while (tail != head) {
1711
1712                 /*
1713                  * Get interrupt information.
1714                  */
1715
1716                 event = bd->re_map_membase + tail + EVSTART;
1717
1718                 port   = event[0];
1719                 reason = event[1];
1720                 modem  = event[2];
1721                 b1     = event[3];
1722
1723                 DPR_EVENT(("event: jiffies: %ld port: %d reason: %x modem: %x\n",
1724                         jiffies, port, reason, modem));
1725
1726                 /*
1727                  * Make sure the interrupt is valid.
1728                  */
1729                 if ( port >= bd->nasync) {
1730                         goto next;
1731                 }
1732
1733                 if (!(reason & (IFMODEM | IFBREAK | IFTLW | IFTEM | IFDATA))) {
1734                         goto next;
1735                 }
1736
1737                 ch = bd->channels[port];
1738
1739                 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC) {
1740                         goto next;
1741                 }
1742
1743                 /*
1744                  * If we have made it here, the event was valid.
1745                  * Lock down the channel.
1746                  */
1747                 DGAP_LOCK(ch->ch_lock, lock_flags2);
1748
1749                 bs = ch->ch_bs;
1750
1751                 if (!bs) {
1752                         DGAP_UNLOCK(ch->ch_lock, lock_flags2);
1753                         goto next;
1754                 }
1755
1756                 /*
1757                  * Process received data.
1758                  */
1759                 if (reason & IFDATA) {
1760
1761                         /*
1762                          * ALL LOCKS *MUST* BE DROPPED BEFORE CALLING INPUT!
1763                          * input could send some data to ld, which in turn
1764                          * could do a callback to one of our other functions.
1765                          */
1766                         DGAP_UNLOCK(ch->ch_lock, lock_flags2);
1767                         DGAP_UNLOCK(bd->bd_lock, lock_flags);
1768
1769                         dgap_input(ch);
1770
1771                         DGAP_LOCK(bd->bd_lock, lock_flags);
1772                         DGAP_LOCK(ch->ch_lock, lock_flags2);
1773
1774                         if (ch->ch_flags & CH_RACTIVE)
1775                                 ch->ch_flags |= CH_RENABLE;
1776                         else
1777                                 writeb(1, &(bs->idata));
1778
1779                         if (ch->ch_flags & CH_RWAIT) {
1780                                 ch->ch_flags &= ~CH_RWAIT;
1781
1782                                 wake_up_interruptible(&ch->ch_tun.un_flags_wait);
1783                         }
1784                 }
1785
1786                 /*
1787                  * Process Modem change signals. 
1788                  */
1789                 if (reason & IFMODEM) {
1790                         ch->ch_mistat = modem;
1791                         dgap_carrier(ch);
1792                 }
1793
1794                 /*
1795                  * Process break.
1796                  */
1797                 if (reason & IFBREAK) {
1798
1799                         DPR_EVENT(("got IFBREAK\n"));
1800
1801                         if (ch->ch_tun.un_tty) {
1802                                 /* A break has been indicated */
1803                                 ch->ch_err_break++;
1804                                 tty_buffer_request_room(ch->ch_tun.un_tty->port, 1);
1805                                 tty_insert_flip_char(ch->ch_tun.un_tty->port, 0, TTY_BREAK);
1806                                 tty_flip_buffer_push(ch->ch_tun.un_tty->port);
1807                         }
1808                 }
1809
1810                 /*
1811                  * Process Transmit low.
1812                  */
1813                 if (reason & IFTLW) {
1814
1815                         DPR_EVENT(("event: got low event\n"));
1816
1817                         if (ch->ch_tun.un_flags & UN_LOW) {
1818                                 ch->ch_tun.un_flags &= ~UN_LOW;
1819
1820                                 if (ch->ch_tun.un_flags & UN_ISOPEN) {
1821                                         if ((ch->ch_tun.un_tty->flags & 
1822                                            (1 << TTY_DO_WRITE_WAKEUP)) &&
1823 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,31)
1824                                                 ch->ch_tun.un_tty->ldisc->ops->write_wakeup)
1825 #else
1826                                                 ch->ch_tun.un_tty->ldisc.ops->write_wakeup)
1827 #endif
1828                                         {
1829                                                 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
1830                                                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
1831 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,31)
1832                                                 (ch->ch_tun.un_tty->ldisc->ops->write_wakeup)(ch->ch_tun.un_tty);
1833 #else
1834                                                 (ch->ch_tun.un_tty->ldisc.ops->write_wakeup)(ch->ch_tun.un_tty);
1835 #endif
1836                                                 DGAP_LOCK(bd->bd_lock, lock_flags);
1837                                                 DGAP_LOCK(ch->ch_lock, lock_flags2);
1838                                         }
1839                                         wake_up_interruptible(&ch->ch_tun.un_tty->write_wait);
1840                                         wake_up_interruptible(&ch->ch_tun.un_flags_wait);
1841
1842                                         DPR_EVENT(("event: Got low event. jiffies: %lu\n", jiffies));
1843                                 }
1844                         }
1845
1846                         if (ch->ch_pun.un_flags & UN_LOW) {
1847                                 ch->ch_pun.un_flags &= ~UN_LOW;
1848                                 if (ch->ch_pun.un_flags & UN_ISOPEN) {
1849                                         if ((ch->ch_pun.un_tty->flags & 
1850                                            (1 << TTY_DO_WRITE_WAKEUP)) &&
1851 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,31)
1852                                                 ch->ch_pun.un_tty->ldisc->ops->write_wakeup)
1853 #else
1854                                                 ch->ch_pun.un_tty->ldisc.ops->write_wakeup)
1855 #endif
1856                                         {
1857                                                 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
1858                                                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
1859 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,31)
1860                                                 (ch->ch_pun.un_tty->ldisc->ops->write_wakeup)(ch->ch_pun.un_tty);
1861 #else
1862                                                 (ch->ch_pun.un_tty->ldisc.ops->write_wakeup)(ch->ch_pun.un_tty);
1863 #endif
1864                                                 DGAP_LOCK(bd->bd_lock, lock_flags);
1865                                                 DGAP_LOCK(ch->ch_lock, lock_flags2);
1866                                         }
1867                                         wake_up_interruptible(&ch->ch_pun.un_tty->write_wait);
1868                                         wake_up_interruptible(&ch->ch_pun.un_flags_wait);
1869                                 }
1870                         }
1871
1872                         if (ch->ch_flags & CH_WLOW) {
1873                                 ch->ch_flags &= ~CH_WLOW;
1874                                 wake_up_interruptible(&ch->ch_flags_wait);
1875                         }
1876                 }
1877
1878                 /*
1879                  * Process Transmit empty.
1880                  */
1881                 if (reason & IFTEM) {
1882                         DPR_EVENT(("event: got empty event\n"));
1883
1884                         if (ch->ch_tun.un_flags & UN_EMPTY) {
1885                                 ch->ch_tun.un_flags &= ~UN_EMPTY;
1886                                 if (ch->ch_tun.un_flags & UN_ISOPEN) {
1887                                         if ((ch->ch_tun.un_tty->flags & 
1888                                            (1 << TTY_DO_WRITE_WAKEUP)) &&
1889 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,31)
1890                                                 ch->ch_tun.un_tty->ldisc->ops->write_wakeup)
1891 #else
1892                                                 ch->ch_tun.un_tty->ldisc.ops->write_wakeup)
1893 #endif
1894                                         {
1895                                                 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
1896                                                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
1897 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,31)
1898                                                 (ch->ch_tun.un_tty->ldisc->ops->write_wakeup)(ch->ch_tun.un_tty);
1899 #else
1900                                                 (ch->ch_tun.un_tty->ldisc.ops->write_wakeup)(ch->ch_tun.un_tty);
1901 #endif
1902                                                 DGAP_LOCK(bd->bd_lock, lock_flags);
1903                                                 DGAP_LOCK(ch->ch_lock, lock_flags2);
1904                                         }
1905                                         wake_up_interruptible(&ch->ch_tun.un_tty->write_wait);
1906                                         wake_up_interruptible(&ch->ch_tun.un_flags_wait);
1907                                 }
1908                         }
1909
1910                         if (ch->ch_pun.un_flags & UN_EMPTY) {
1911                                 ch->ch_pun.un_flags &= ~UN_EMPTY;
1912                                 if (ch->ch_pun.un_flags & UN_ISOPEN) {
1913                                         if ((ch->ch_pun.un_tty->flags & 
1914                                            (1 << TTY_DO_WRITE_WAKEUP)) &&
1915 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,31)
1916                                                 ch->ch_pun.un_tty->ldisc->ops->write_wakeup)
1917 #else
1918                                                 ch->ch_pun.un_tty->ldisc.ops->write_wakeup)
1919 #endif
1920                                         {
1921                                                 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
1922                                                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
1923 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,31)
1924                                                 (ch->ch_pun.un_tty->ldisc->ops->write_wakeup)(ch->ch_pun.un_tty);
1925 #else
1926                                                 (ch->ch_pun.un_tty->ldisc.ops->write_wakeup)(ch->ch_pun.un_tty);
1927 #endif
1928                                                 DGAP_LOCK(bd->bd_lock, lock_flags);
1929                                                 DGAP_LOCK(ch->ch_lock, lock_flags2);
1930                                         }
1931                                         wake_up_interruptible(&ch->ch_pun.un_tty->write_wait);
1932                                         wake_up_interruptible(&ch->ch_pun.un_flags_wait);
1933                                 }
1934                         }
1935
1936
1937                         if (ch->ch_flags & CH_WEMPTY) {
1938                                 ch->ch_flags &= ~CH_WEMPTY;
1939                                 wake_up_interruptible(&ch->ch_flags_wait);
1940                         }
1941                 }
1942
1943                 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
1944
1945 next:
1946                 tail = (tail + 4) & (EVMAX - EVSTART - 4);
1947         }
1948
1949         writew(tail, &(eaddr->ev_tail));
1950         DGAP_UNLOCK(bd->bd_lock, lock_flags);
1951
1952         return (0);
1953 }