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