staging: dgap: Merge dgap_tty.h into dgap_driver.c
[firefly-linux-kernel-4.4.55.git] / drivers / staging / dgap / dgap_driver.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  */
31
32
33 #include <linux/kernel.h>
34 #include <linux/module.h>
35 #include <linux/pci.h>
36 #include <linux/delay.h>        /* For udelay */
37 #include <linux/slab.h>
38 #include <asm/uaccess.h>        /* For copy_from_user/copy_to_user */
39 #include <linux/sched.h>
40
41 #include <linux/interrupt.h>    /* For tasklet and interrupt structs/defines */
42 #include <linux/ctype.h>
43 #include <linux/tty.h>
44 #include <linux/tty_flip.h>
45 #include <linux/serial_reg.h>
46 #include <asm/io.h>             /* For read[bwl]/write[bwl] */
47
48 #include <linux/string.h>
49 #include <linux/device.h>
50 #include <linux/kdev_t.h>
51
52 #include "dgap_driver.h"
53 #include "dgap_pci.h"
54 #include "dgap_fep5.h"
55 #include "dgap_conf.h"
56 #include "dgap_parse.h"
57 #include "dgap_sysfs.h"
58 #include "dgap_types.h"
59
60 #define init_MUTEX(sem)         sema_init(sem, 1)
61 #define DECLARE_MUTEX(name)     \
62         struct semaphore name = __SEMAPHORE_INITIALIZER(name, 1)
63
64 MODULE_LICENSE("GPL");
65 MODULE_AUTHOR("Digi International, http://www.digi.com");
66 MODULE_DESCRIPTION("Driver for the Digi International EPCA PCI based product line");
67 MODULE_SUPPORTED_DEVICE("dgap");
68
69 /*
70  * insmod command line overrideable parameters
71  *
72  * NOTE: we use a set of macros to create the variables, which allows
73  * us to specify the variable type, name, initial value, and description.
74  */
75 PARM_INT(debug,         0x00,           0644,   "Driver debugging level");
76 PARM_INT(rawreadok,     1,              0644,   "Bypass flip buffers on input");
77 PARM_INT(trcbuf_size,   0x100000,       0644,   "Debugging trace buffer size.");
78
79
80 /**************************************************************************
81  *
82  * protos for this file
83  *
84  */
85
86 static int              dgap_start(void);
87 static void             dgap_init_globals(void);
88 static int              dgap_found_board(struct pci_dev *pdev, int id);
89 static void             dgap_cleanup_board(struct board_t *brd);
90 static void             dgap_poll_handler(ulong dummy);
91 static int              dgap_init_pci(void);
92 static int              dgap_init_one(struct pci_dev *pdev, const struct pci_device_id *ent);
93 static void             dgap_remove_one(struct pci_dev *dev);
94 static int              dgap_probe1(struct pci_dev *pdev, int card_type);
95 static void             dgap_mbuf(struct board_t *brd, const char *fmt, ...);
96 static int              dgap_do_remap(struct board_t *brd);
97 static irqreturn_t      dgap_intr(int irq, void *voidbrd);
98
99 /* Our function prototypes */
100 static int dgap_tty_open(struct tty_struct *tty, struct file *file);
101 static void dgap_tty_close(struct tty_struct *tty, struct file *file);
102 static int dgap_block_til_ready(struct tty_struct *tty, struct file *file, struct channel_t *ch);
103 static int dgap_tty_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg);
104 static int dgap_tty_digigeta(struct tty_struct *tty, struct digi_t __user *retinfo);
105 static int dgap_tty_digiseta(struct tty_struct *tty, struct digi_t __user *new_info);
106 static int dgap_tty_digigetedelay(struct tty_struct *tty, int __user *retinfo);
107 static int dgap_tty_digisetedelay(struct tty_struct *tty, int __user *new_info);
108 static int dgap_tty_write_room(struct tty_struct* tty);
109 static int dgap_tty_chars_in_buffer(struct tty_struct* tty);
110 static void dgap_tty_start(struct tty_struct *tty);
111 static void dgap_tty_stop(struct tty_struct *tty);
112 static void dgap_tty_throttle(struct tty_struct *tty);
113 static void dgap_tty_unthrottle(struct tty_struct *tty);
114 static void dgap_tty_flush_chars(struct tty_struct *tty);
115 static void dgap_tty_flush_buffer(struct tty_struct *tty);
116 static void dgap_tty_hangup(struct tty_struct *tty);
117 static int dgap_wait_for_drain(struct tty_struct *tty);
118 static int dgap_set_modem_info(struct tty_struct *tty, unsigned int command, unsigned int __user *value);
119 static int dgap_get_modem_info(struct channel_t *ch, unsigned int __user *value);
120 static int dgap_tty_digisetcustombaud(struct tty_struct *tty, int __user *new_info);
121 static int dgap_tty_digigetcustombaud(struct tty_struct *tty, int __user *retinfo);
122 static int dgap_tty_tiocmget(struct tty_struct *tty);
123 static int dgap_tty_tiocmset(struct tty_struct *tty, unsigned int set, unsigned int clear);
124 static int dgap_tty_send_break(struct tty_struct *tty, int msec);
125 static void dgap_tty_wait_until_sent(struct tty_struct *tty, int timeout);
126 static int dgap_tty_write(struct tty_struct *tty, const unsigned char *buf, int count);
127 static void dgap_tty_set_termios(struct tty_struct *tty, struct ktermios *old_termios);
128 static int dgap_tty_put_char(struct tty_struct *tty, unsigned char c);
129 static void dgap_tty_send_xchar(struct tty_struct *tty, char ch);
130
131 int     dgap_tty_register(struct board_t *brd);
132 int     dgap_tty_preinit(void);
133 int     dgap_tty_init(struct board_t *);
134 void    dgap_tty_post_uninit(void);
135 void    dgap_tty_uninit(struct board_t *);
136 void    dgap_carrier(struct channel_t *ch);
137 void    dgap_input(struct channel_t *ch);
138
139 /*
140  * Our function prototypes from dgap_fep5
141  */
142 static void dgap_cmdw_ext(struct channel_t *ch, u16 cmd, u16 word, uint ncmds);
143 static int dgap_event(struct board_t *bd);
144
145 /*
146  * Function prototypes from dgap_parse.c.
147  */
148 static int dgap_gettok(char **in, struct cnode *p);
149 static char *dgap_getword(char **in);
150 static char *dgap_savestring(char *s);
151 static struct cnode *dgap_newnode(int t);
152 static int dgap_checknode(struct cnode *p);
153 static void dgap_err(char *s);
154
155 /* Driver load/unload functions */
156 int                     dgap_init_module(void);
157 void                    dgap_cleanup_module(void);
158
159 module_init(dgap_init_module);
160 module_exit(dgap_cleanup_module);
161
162
163 /*
164  * File operations permitted on Control/Management major.
165  */
166 static struct file_operations DgapBoardFops =
167 {
168         .owner          =       THIS_MODULE,
169 };
170
171
172 /*
173  * Globals
174  */
175 uint                    dgap_NumBoards;
176 struct board_t          *dgap_Board[MAXBOARDS];
177 DEFINE_SPINLOCK(dgap_global_lock);
178 ulong                   dgap_poll_counter;
179 char                    *dgap_config_buf;
180 int                     dgap_driver_state = DRIVER_INITIALIZED;
181 DEFINE_SPINLOCK(dgap_dl_lock);
182 wait_queue_head_t       dgap_dl_wait;
183 int                     dgap_dl_action;
184 int                     dgap_poll_tick = 20;    /* Poll interval - 20 ms */
185
186 /*
187  * Static vars.
188  */
189 static int              dgap_Major_Control_Registered = FALSE;
190 static uint             dgap_driver_start = FALSE;
191
192 static struct class *   dgap_class;
193
194 static struct board_t   *dgap_BoardsByMajor[256];
195 static uchar            *dgap_TmpWriteBuf = NULL;
196 static DECLARE_MUTEX(dgap_TmpWriteSem);
197 static uint dgap_count = 500;
198
199 /*
200  * Poller stuff
201  */
202 static                  DEFINE_SPINLOCK(dgap_poll_lock);        /* Poll scheduling lock */
203 static ulong            dgap_poll_time;                         /* Time of next poll */
204 static uint             dgap_poll_stop;                         /* Used to tell poller to stop */
205 static struct timer_list dgap_poll_timer;
206
207
208 static struct pci_device_id dgap_pci_tbl[] = {
209         {       DIGI_VID, PCI_DEVICE_XEM_DID,   PCI_ANY_ID, PCI_ANY_ID, 0, 0,   0 },
210         {       DIGI_VID, PCI_DEVICE_CX_DID,    PCI_ANY_ID, PCI_ANY_ID, 0, 0,   1 },
211         {       DIGI_VID, PCI_DEVICE_CX_IBM_DID, PCI_ANY_ID, PCI_ANY_ID, 0, 0,  2 },
212         {       DIGI_VID, PCI_DEVICE_EPCJ_DID,  PCI_ANY_ID, PCI_ANY_ID, 0, 0,   3 },
213         {       DIGI_VID, PCI_DEVICE_920_2_DID, PCI_ANY_ID, PCI_ANY_ID, 0, 0,   4 },
214         {       DIGI_VID, PCI_DEVICE_920_4_DID, PCI_ANY_ID, PCI_ANY_ID, 0, 0,   5 },
215         {       DIGI_VID, PCI_DEVICE_920_8_DID, PCI_ANY_ID, PCI_ANY_ID, 0, 0,   6 },
216         {       DIGI_VID, PCI_DEVICE_XR_DID,    PCI_ANY_ID, PCI_ANY_ID, 0, 0,   7 },
217         {       DIGI_VID, PCI_DEVICE_XRJ_DID,   PCI_ANY_ID, PCI_ANY_ID, 0, 0,   8 },
218         {       DIGI_VID, PCI_DEVICE_XR_422_DID, PCI_ANY_ID, PCI_ANY_ID, 0, 0,  9 },
219         {       DIGI_VID, PCI_DEVICE_XR_IBM_DID, PCI_ANY_ID, PCI_ANY_ID, 0, 0,  10 },
220         {       DIGI_VID, PCI_DEVICE_XR_SAIP_DID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 11 },
221         {       DIGI_VID, PCI_DEVICE_XR_BULL_DID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 12 },
222         {       DIGI_VID, PCI_DEVICE_920_8_HP_DID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 13 },
223         {       DIGI_VID, PCI_DEVICE_XEM_HP_DID, PCI_ANY_ID, PCI_ANY_ID, 0, 0,  14 },
224         {0,}                                    /* 0 terminated list. */
225 };
226 MODULE_DEVICE_TABLE(pci, dgap_pci_tbl);
227
228
229 /*
230  * A generic list of Product names, PCI Vendor ID, and PCI Device ID.
231  */
232 struct board_id {
233         uint config_type;
234         uchar *name;
235         uint maxports;
236         uint dpatype;
237 };
238
239 static struct board_id dgap_Ids[] =
240 {
241         {       PPCM,           PCI_DEVICE_XEM_NAME,    64,     (T_PCXM | T_PCLITE | T_PCIBUS)  },
242         {       PCX,            PCI_DEVICE_CX_NAME,     128,    (T_CX | T_PCIBUS)               },
243         {       PCX,            PCI_DEVICE_CX_IBM_NAME, 128,    (T_CX | T_PCIBUS)               },
244         {       PEPC,           PCI_DEVICE_EPCJ_NAME,   224,    (T_EPC  | T_PCIBUS)             },
245         {       APORT2_920P,    PCI_DEVICE_920_2_NAME,  2,      (T_PCXR | T_PCLITE | T_PCIBUS)  },
246         {       APORT4_920P,    PCI_DEVICE_920_4_NAME,  4,      (T_PCXR | T_PCLITE | T_PCIBUS)  },
247         {       APORT8_920P,    PCI_DEVICE_920_8_NAME,  8,      (T_PCXR | T_PCLITE | T_PCIBUS)  },
248         {       PAPORT8,        PCI_DEVICE_XR_NAME,     8,      (T_PCXR | T_PCLITE | T_PCIBUS)  },
249         {       PAPORT8,        PCI_DEVICE_XRJ_NAME,    8,      (T_PCXR | T_PCLITE | T_PCIBUS)  },
250         {       PAPORT8,        PCI_DEVICE_XR_422_NAME, 8,      (T_PCXR | T_PCLITE | T_PCIBUS)  },
251         {       PAPORT8,        PCI_DEVICE_XR_IBM_NAME, 8,      (T_PCXR | T_PCLITE | T_PCIBUS)  },
252         {       PAPORT8,        PCI_DEVICE_XR_SAIP_NAME, 8,     (T_PCXR | T_PCLITE | T_PCIBUS)  },
253         {       PAPORT8,        PCI_DEVICE_XR_BULL_NAME, 8,     (T_PCXR | T_PCLITE | T_PCIBUS)  },
254         {       APORT8_920P,    PCI_DEVICE_920_8_HP_NAME, 8,    (T_PCXR | T_PCLITE | T_PCIBUS)  },
255         {       PPCM,           PCI_DEVICE_XEM_HP_NAME, 64,     (T_PCXM | T_PCLITE | T_PCIBUS)  },
256         {0,}                                            /* 0 terminated list. */
257 };
258
259 static struct pci_driver dgap_driver = {
260         .name           = "dgap",
261         .probe          = dgap_init_one,
262         .id_table       = dgap_pci_tbl,
263         .remove         = dgap_remove_one,
264 };
265
266
267 char *dgap_state_text[] = {
268         "Board Failed",
269         "Configuration for board not found.\n\t\t\tRun mpi to configure board.",
270         "Board Found",
271         "Need Reset",
272         "Finished Reset",
273         "Need Config",
274         "Finished Config",
275         "Need Device Creation",
276         "Requested Device Creation",
277         "Finished Device Creation",
278         "Need BIOS Load",
279         "Requested BIOS",
280         "Doing BIOS Load",
281         "Finished BIOS Load",
282         "Need FEP Load",
283         "Requested FEP",
284         "Doing FEP Load",
285         "Finished FEP Load",
286         "Requested PROC creation",
287         "Finished PROC creation",
288         "Board READY",
289 };
290
291 char *dgap_driver_state_text[] = {
292         "Driver Initialized",
293         "Driver needs configuration load.",
294         "Driver requested configuration from download daemon.",
295         "Driver Ready."
296 };
297
298 /*
299  * Default transparent print information.
300  */
301 static struct digi_t dgap_digi_init = {
302         .digi_flags =   DIGI_COOK,      /* Flags                        */
303         .digi_maxcps =  100,            /* Max CPS                      */
304         .digi_maxchar = 50,             /* Max chars in print queue     */
305         .digi_bufsize = 100,            /* Printer buffer size          */
306         .digi_onlen =   4,              /* size of printer on string    */
307         .digi_offlen =  4,              /* size of printer off string   */
308         .digi_onstr =   "\033[5i",      /* ANSI printer on string ]     */
309         .digi_offstr =  "\033[4i",      /* ANSI printer off string ]    */
310         .digi_term =    "ansi"          /* default terminal type        */
311 };
312
313
314 /*
315  * Define a local default termios struct. All ports will be created
316  * with this termios initially.
317  *
318  * This defines a raw port at 9600 baud, 8 data bits, no parity,
319  * 1 stop bit.
320  */
321
322 static struct ktermios DgapDefaultTermios =
323 {
324         .c_iflag =      (DEFAULT_IFLAGS),       /* iflags */
325         .c_oflag =      (DEFAULT_OFLAGS),       /* oflags */
326         .c_cflag =      (DEFAULT_CFLAGS),       /* cflags */
327         .c_lflag =      (DEFAULT_LFLAGS),       /* lflags */
328         .c_cc =         INIT_C_CC,
329         .c_line =       0,
330 };
331
332 static const struct tty_operations dgap_tty_ops = {
333         .open = dgap_tty_open,
334         .close = dgap_tty_close,
335         .write = dgap_tty_write,
336         .write_room = dgap_tty_write_room,
337         .flush_buffer = dgap_tty_flush_buffer,
338         .chars_in_buffer = dgap_tty_chars_in_buffer,
339         .flush_chars = dgap_tty_flush_chars,
340         .ioctl = dgap_tty_ioctl,
341         .set_termios = dgap_tty_set_termios,
342         .stop = dgap_tty_stop,
343         .start = dgap_tty_start,
344         .throttle = dgap_tty_throttle,
345         .unthrottle = dgap_tty_unthrottle,
346         .hangup = dgap_tty_hangup,
347         .put_char = dgap_tty_put_char,
348         .tiocmget = dgap_tty_tiocmget,
349         .tiocmset = dgap_tty_tiocmset,
350         .break_ctl = dgap_tty_send_break,
351         .wait_until_sent = dgap_tty_wait_until_sent,
352         .send_xchar = dgap_tty_send_xchar
353 };
354
355 /*
356  * Our needed internal static variables from dgap_parse.c
357  */
358 static struct cnode dgap_head;
359 #define MAXCWORD 200
360 static char dgap_cword[MAXCWORD];
361
362 struct toklist {
363         int     token;
364         char    *string;
365 };
366
367 static struct toklist dgap_tlist[] = {
368         {       BEGIN,          "config_begin"                  },
369         {       END,            "config_end"                    },
370         {       BOARD,          "board"                         },
371         {       PCX,            "Digi_AccelePort_C/X_PCI"       },      /* C/X_PCI */
372         {       PEPC,           "Digi_AccelePort_EPC/X_PCI"     },      /* EPC/X_PCI */
373         {       PPCM,           "Digi_AccelePort_Xem_PCI"       },      /* PCI/Xem */
374         {       APORT2_920P,    "Digi_AccelePort_2r_920_PCI"    },
375         {       APORT4_920P,    "Digi_AccelePort_4r_920_PCI"    },
376         {       APORT8_920P,    "Digi_AccelePort_8r_920_PCI"    },
377         {       PAPORT4,        "Digi_AccelePort_4r_PCI(EIA-232/RS-422)" },
378         {       PAPORT8,        "Digi_AccelePort_8r_PCI(EIA-232/RS-422)" },
379         {       IO,             "io"                            },
380         {       PCIINFO,        "pciinfo"                       },
381         {       LINE,           "line"                          },
382         {       CONC,           "conc"                          },
383         {       CONC,           "concentrator"                  },
384         {       CX,             "cx"                            },
385         {       CX,             "ccon"                          },
386         {       EPC,            "epccon"                        },
387         {       EPC,            "epc"                           },
388         {       MOD,            "module"                        },
389         {       ID,             "id"                            },
390         {       STARTO,         "start"                         },
391         {       SPEED,          "speed"                         },
392         {       CABLE,          "cable"                         },
393         {       CONNECT,        "connect"                       },
394         {       METHOD,         "method"                        },
395         {       STATUS,         "status"                        },
396         {       CUSTOM,         "Custom"                        },
397         {       BASIC,          "Basic"                         },
398         {       MEM,            "mem"                           },
399         {       MEM,            "memory"                        },
400         {       PORTS,          "ports"                         },
401         {       MODEM,          "modem"                         },
402         {       NPORTS,         "nports"                        },
403         {       TTYN,           "ttyname"                       },
404         {       CU,             "cuname"                        },
405         {       PRINT,          "prname"                        },
406         {       CMAJOR,         "major"                         },
407         {       ALTPIN,         "altpin"                        },
408         {       USEINTR,        "useintr"                       },
409         {       TTSIZ,          "ttysize"                       },
410         {       CHSIZ,          "chsize"                        },
411         {       BSSIZ,          "boardsize"                     },
412         {       UNTSIZ,         "schedsize"                     },
413         {       F2SIZ,          "f2200size"                     },
414         {       VPSIZ,          "vpixsize"                      },
415         {       0,              NULL                            }
416 };
417
418 /************************************************************************
419  *
420  * Driver load/unload functions
421  *
422  ************************************************************************/
423
424 /*
425  * init_module()
426  *
427  * Module load.  This is where it all starts.
428  */
429 int dgap_init_module(void)
430 {
431         int rc = 0;
432
433         APR(("%s, Digi International Part Number %s\n", DG_NAME, DG_PART));
434
435         /*
436          * Initialize global stuff
437          */
438         rc = dgap_start();
439
440         if (rc < 0) {
441                 return(rc);
442         }
443
444         /*
445          * Find and configure all the cards
446          */
447         rc = dgap_init_pci();
448
449         /*
450          * If something went wrong in the scan, bail out of driver.
451          */
452         if (rc < 0) {
453                 /* Only unregister the pci driver if it was actually registered. */
454                 if (dgap_NumBoards)
455                         pci_unregister_driver(&dgap_driver);
456                 else
457                         printk("WARNING: dgap driver load failed.  No DGAP boards found.\n");
458
459                 dgap_cleanup_module();
460         }
461         else {
462                 dgap_create_driver_sysfiles(&dgap_driver);
463         }
464
465         DPR_INIT(("Finished init_module. Returning %d\n", rc));
466         return (rc);
467 }
468
469
470 /*
471  * Start of driver.
472  */
473 static int dgap_start(void)
474 {
475         int rc = 0;
476         unsigned long flags;
477
478         if (dgap_driver_start == FALSE) {
479
480                 dgap_driver_start = TRUE;
481
482                 /* make sure that the globals are init'd before we do anything else */
483                 dgap_init_globals();
484
485                 dgap_NumBoards = 0;
486
487                 APR(("For the tools package or updated drivers please visit http://www.digi.com\n"));
488
489                 /*
490                  * Register our base character device into the kernel.
491                  * This allows the download daemon to connect to the downld device
492                  * before any of the boards are init'ed.
493                  */
494                 if (!dgap_Major_Control_Registered) {
495                         /*
496                          * Register management/dpa devices
497                          */
498                         rc = register_chrdev(DIGI_DGAP_MAJOR, "dgap", &DgapBoardFops);
499                         if (rc < 0) {
500                                 APR(("Can't register dgap driver device (%d)\n", rc));
501                                 return (rc);
502                         }
503
504                         dgap_class = class_create(THIS_MODULE, "dgap_mgmt");
505                         device_create(dgap_class, NULL,
506                                 MKDEV(DIGI_DGAP_MAJOR, 0),
507                                 NULL, "dgap_mgmt");
508                         device_create(dgap_class, NULL,
509                                 MKDEV(DIGI_DGAP_MAJOR, 1),
510                                 NULL, "dgap_downld");
511                         dgap_Major_Control_Registered = TRUE;
512                 }
513
514                 /*
515                  * Init any global tty stuff.
516                  */
517                 rc = dgap_tty_preinit();
518
519                 if (rc < 0) {
520                         APR(("tty preinit - not enough memory (%d)\n", rc));
521                         return(rc);
522                 }
523
524                 /* Start the poller */
525                 DGAP_LOCK(dgap_poll_lock, flags);
526                 init_timer(&dgap_poll_timer);
527                 dgap_poll_timer.function = dgap_poll_handler;
528                 dgap_poll_timer.data = 0;
529                 dgap_poll_time = jiffies + dgap_jiffies_from_ms(dgap_poll_tick);
530                 dgap_poll_timer.expires = dgap_poll_time;
531                 DGAP_UNLOCK(dgap_poll_lock, flags);
532
533                 add_timer(&dgap_poll_timer);
534
535                 dgap_driver_state = DRIVER_NEED_CONFIG_LOAD;
536         }
537
538         return (rc);
539 }
540
541
542 /*
543  * Register pci driver, and return how many boards we have.
544  */
545 static int dgap_init_pci(void)
546 {
547         return pci_register_driver(&dgap_driver);
548 }
549
550
551 /* returns count (>= 0), or negative on error */
552 static int dgap_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
553 {
554         int rc;
555
556         /* wake up and enable device */
557         rc = pci_enable_device(pdev);
558
559         if (rc < 0) {
560                 rc = -EIO;
561         } else {
562                 rc = dgap_probe1(pdev, ent->driver_data);
563                 if (rc == 0) {
564                         dgap_NumBoards++;
565                         DPR_INIT(("Incrementing numboards to %d\n", dgap_NumBoards));
566                 }
567         }
568         return rc;
569 }
570
571
572 static int dgap_probe1(struct pci_dev *pdev, int card_type)
573 {
574         return dgap_found_board(pdev, card_type);
575 }
576
577
578 static void dgap_remove_one(struct pci_dev *dev)
579 {
580         /* Do Nothing */
581 }
582
583
584 /*
585  * dgap_cleanup_module()
586  *
587  * Module unload.  This is where it all ends.
588  */
589 void dgap_cleanup_module(void)
590 {
591         int i;
592         ulong lock_flags;
593
594         DGAP_LOCK(dgap_poll_lock, lock_flags);
595         dgap_poll_stop = 1;
596         DGAP_UNLOCK(dgap_poll_lock, lock_flags);
597
598         /* Turn off poller right away. */
599         del_timer_sync( &dgap_poll_timer);
600
601         dgap_remove_driver_sysfiles(&dgap_driver);
602
603
604         if (dgap_Major_Control_Registered) {
605                 device_destroy(dgap_class, MKDEV(DIGI_DGAP_MAJOR, 0));
606                 device_destroy(dgap_class, MKDEV(DIGI_DGAP_MAJOR, 1));
607                 class_destroy(dgap_class);
608                 unregister_chrdev(DIGI_DGAP_MAJOR, "dgap");
609         }
610
611         kfree(dgap_config_buf);
612
613         for (i = 0; i < dgap_NumBoards; ++i) {
614                 dgap_remove_ports_sysfiles(dgap_Board[i]);
615                 dgap_tty_uninit(dgap_Board[i]);
616                 dgap_cleanup_board(dgap_Board[i]);
617         }
618
619         dgap_tty_post_uninit();
620
621 #if defined(DGAP_TRACER)
622         /* last thing, make sure we release the tracebuffer */
623         dgap_tracer_free();
624 #endif
625         if (dgap_NumBoards)
626                 pci_unregister_driver(&dgap_driver);
627 }
628
629
630 /*
631  * dgap_cleanup_board()
632  *
633  * Free all the memory associated with a board
634  */
635 static void dgap_cleanup_board(struct board_t *brd)
636 {
637         int i = 0;
638
639         if(!brd || brd->magic != DGAP_BOARD_MAGIC)
640                 return;
641
642         if (brd->intr_used && brd->irq)
643                 free_irq(brd->irq, brd);
644
645         tasklet_kill(&brd->helper_tasklet);
646
647         if (brd->re_map_port) {
648                 release_mem_region(brd->membase + 0x200000, 0x200000);
649                 iounmap(brd->re_map_port);
650                 brd->re_map_port = NULL;
651         }
652
653         if (brd->re_map_membase) {
654                 release_mem_region(brd->membase, 0x200000);
655                 iounmap(brd->re_map_membase);
656                 brd->re_map_membase = NULL;
657         }
658
659         if (brd->msgbuf_head) {
660                 unsigned long flags;
661
662                 DGAP_LOCK(dgap_global_lock, flags);
663                 brd->msgbuf = NULL;
664                 printk("%s", brd->msgbuf_head);
665                 kfree(brd->msgbuf_head);
666                 brd->msgbuf_head = NULL;
667                 DGAP_UNLOCK(dgap_global_lock, flags);
668         }
669
670         /* Free all allocated channels structs */
671         for (i = 0; i < MAXPORTS ; i++) {
672                 if (brd->channels[i]) {
673                         kfree(brd->channels[i]);
674                         brd->channels[i] = NULL;
675                 }
676         }
677
678         kfree(brd->flipbuf);
679         kfree(brd->flipflagbuf);
680
681         dgap_Board[brd->boardnum] = NULL;
682
683         kfree(brd);
684 }
685
686
687 /*
688  * dgap_found_board()
689  *
690  * A board has been found, init it.
691  */
692 static int dgap_found_board(struct pci_dev *pdev, int id)
693 {
694         struct board_t *brd;
695         unsigned int pci_irq;
696         int i = 0;
697         unsigned long flags;
698
699         /* get the board structure and prep it */
700         brd = dgap_Board[dgap_NumBoards] =
701         (struct board_t *) kzalloc(sizeof(struct board_t), GFP_KERNEL);
702         if (!brd) {
703                 APR(("memory allocation for board structure failed\n"));
704                 return(-ENOMEM);
705         }
706
707         /* make a temporary message buffer for the boot messages */
708         brd->msgbuf = brd->msgbuf_head =
709                 (char *) kzalloc(sizeof(char) * 8192, GFP_KERNEL);
710         if(!brd->msgbuf) {
711                 kfree(brd);
712                 APR(("memory allocation for board msgbuf failed\n"));
713                 return(-ENOMEM);
714         }
715
716         /* store the info for the board we've found */
717         brd->magic = DGAP_BOARD_MAGIC;
718         brd->boardnum = dgap_NumBoards;
719         brd->firstminor = 0;
720         brd->vendor = dgap_pci_tbl[id].vendor;
721         brd->device = dgap_pci_tbl[id].device;
722         brd->pdev = pdev;
723         brd->pci_bus = pdev->bus->number;
724         brd->pci_slot = PCI_SLOT(pdev->devfn);
725         brd->name = dgap_Ids[id].name;
726         brd->maxports = dgap_Ids[id].maxports;
727         brd->type = dgap_Ids[id].config_type;
728         brd->dpatype = dgap_Ids[id].dpatype;
729         brd->dpastatus = BD_NOFEP;
730         init_waitqueue_head(&brd->state_wait);
731
732         DGAP_SPINLOCK_INIT(brd->bd_lock);
733
734         brd->state              = BOARD_FOUND;
735         brd->runwait            = 0;
736         brd->inhibit_poller     = FALSE;
737         brd->wait_for_bios      = 0;
738         brd->wait_for_fep       = 0;
739
740         for (i = 0; i < MAXPORTS; i++) {
741                 brd->channels[i] = NULL;
742         }
743
744         /* store which card & revision we have */
745         pci_read_config_word(pdev, PCI_SUBSYSTEM_VENDOR_ID, &brd->subvendor);
746         pci_read_config_word(pdev, PCI_SUBSYSTEM_ID, &brd->subdevice);
747         pci_read_config_byte(pdev, PCI_REVISION_ID, &brd->rev);
748
749         pci_irq = pdev->irq;
750         brd->irq = pci_irq;
751
752         /* get the PCI Base Address Registers */
753
754         /* Xr Jupiter and EPC use BAR 2 */
755         if (brd->device == PCI_DEVICE_XRJ_DID || brd->device == PCI_DEVICE_EPCJ_DID) {
756                 brd->membase     = pci_resource_start(pdev, 2);
757                 brd->membase_end = pci_resource_end(pdev, 2);
758         }
759         /* Everyone else uses BAR 0 */
760         else {
761                 brd->membase     = pci_resource_start(pdev, 0);
762                 brd->membase_end = pci_resource_end(pdev, 0);
763         }
764
765         if (!brd->membase) {
766                 APR(("card has no PCI IO resources, failing board.\n"));
767                 return -ENODEV;
768         }
769
770         if (brd->membase & 1)
771                 brd->membase &= ~3;
772         else
773                 brd->membase &= ~15;
774
775         /*
776          * On the PCI boards, there is no IO space allocated
777          * The I/O registers will be in the first 3 bytes of the
778          * upper 2MB of the 4MB memory space.  The board memory
779          * will be mapped into the low 2MB of the 4MB memory space
780          */
781         brd->port = brd->membase + PCI_IO_OFFSET;
782         brd->port_end = brd->port + PCI_IO_SIZE;
783
784
785         /*
786          * Special initialization for non-PLX boards
787          */
788         if (brd->device != PCI_DEVICE_XRJ_DID && brd->device != PCI_DEVICE_EPCJ_DID) {
789                 unsigned short cmd;
790
791                 pci_write_config_byte(pdev, 0x40, 0);
792                 pci_write_config_byte(pdev, 0x46, 0);
793
794                 /* Limit burst length to 2 doubleword transactions */
795                 pci_write_config_byte(pdev, 0x42, 1);
796
797                 /*
798                  * Enable IO and mem if not already done.
799                  * This was needed for support on Itanium.
800                  */
801                 pci_read_config_word(pdev, PCI_COMMAND, &cmd);
802                 cmd |= (PCI_COMMAND_IO | PCI_COMMAND_MEMORY);
803                 pci_write_config_word(pdev, PCI_COMMAND, cmd);
804         }
805
806         /* init our poll helper tasklet */
807         tasklet_init(&brd->helper_tasklet, dgap_poll_tasklet, (unsigned long) brd);
808
809          /* Log the information about the board */
810         dgap_mbuf(brd, DRVSTR": board %d: %s (rev %d), irq %d\n",
811                 dgap_NumBoards, brd->name, brd->rev, brd->irq);
812
813         DPR_INIT(("dgap_scan(%d) - printing out the msgbuf\n", i));
814         DGAP_LOCK(dgap_global_lock, flags);
815         brd->msgbuf = NULL;
816         printk("%s", brd->msgbuf_head);
817         kfree(brd->msgbuf_head);
818         brd->msgbuf_head = NULL;
819         DGAP_UNLOCK(dgap_global_lock, flags);
820
821         i = dgap_do_remap(brd);
822         if (i)
823                 brd->state = BOARD_FAILED;
824         else
825                 brd->state = NEED_RESET;
826
827         return(0);
828 }
829
830
831 int dgap_finalize_board_init(struct board_t *brd) {
832
833         int rc;
834
835         DPR_INIT(("dgap_finalize_board_init() - start\n"));
836
837         if (!brd || brd->magic != DGAP_BOARD_MAGIC)
838                 return(-ENODEV);
839
840         DPR_INIT(("dgap_finalize_board_init() - start #2\n"));
841
842         brd->use_interrupts = dgap_config_get_useintr(brd);
843
844         /*
845          * Set up our interrupt handler if we are set to do interrupts.
846          */
847         if (brd->use_interrupts && brd->irq) {
848
849                 rc = request_irq(brd->irq, dgap_intr, IRQF_SHARED, "DGAP", brd);
850
851                 if (rc) {
852                         dgap_mbuf(brd, DRVSTR": Failed to hook IRQ %d. Board will work in poll mode.\n",
853                                   brd->irq);
854                         brd->intr_used = 0;
855                 }
856                 else
857                         brd->intr_used = 1;
858         } else {
859                 brd->intr_used = 0;
860         }
861
862         return(0);
863 }
864
865
866 /*
867  * Remap PCI memory.
868  */
869 static int dgap_do_remap(struct board_t *brd)
870 {
871         if (!brd || brd->magic != DGAP_BOARD_MAGIC)
872                 return -ENXIO;
873
874         if (!request_mem_region(brd->membase, 0x200000, "dgap")) {
875                 APR(("dgap: mem_region %lx already in use.\n", brd->membase));
876                 return -ENOMEM;
877         }
878
879         if (!request_mem_region(brd->membase + PCI_IO_OFFSET, 0x200000, "dgap")) {
880                 APR(("dgap: mem_region IO %lx already in use.\n",
881                         brd->membase + PCI_IO_OFFSET));
882                 release_mem_region(brd->membase, 0x200000);
883                 return -ENOMEM;
884         }
885
886         brd->re_map_membase = ioremap(brd->membase, 0x200000);
887         if (!brd->re_map_membase) {
888                 APR(("dgap: ioremap mem %lx cannot be mapped.\n", brd->membase));
889                 release_mem_region(brd->membase, 0x200000);
890                 release_mem_region(brd->membase + PCI_IO_OFFSET, 0x200000);
891                 return -ENOMEM;
892         }
893
894         brd->re_map_port = ioremap((brd->membase + PCI_IO_OFFSET), 0x200000);
895         if (!brd->re_map_port) {
896                 release_mem_region(brd->membase, 0x200000);
897                 release_mem_region(brd->membase + PCI_IO_OFFSET, 0x200000);
898                 iounmap(brd->re_map_membase);
899                 APR(("dgap: ioremap IO mem %lx cannot be mapped.\n",
900                         brd->membase + PCI_IO_OFFSET));
901                 return -ENOMEM;
902         }
903
904         DPR_INIT(("remapped io: 0x%p  remapped mem: 0x%p\n",
905                 brd->re_map_port, brd->re_map_membase));
906         return 0;
907 }
908
909
910 /*****************************************************************************
911 *
912 * Function:
913 *
914 *    dgap_poll_handler
915 *
916 * Author:
917 *
918 *    Scott H Kilau
919 *
920 * Parameters:
921 *
922 *    dummy -- ignored
923 *
924 * Return Values:
925 *
926 *    none
927 *
928 * Description:
929 *
930 *    As each timer expires, it determines (a) whether the "transmit"
931 *    waiter needs to be woken up, and (b) whether the poller needs to
932 *    be rescheduled.
933 *
934 ******************************************************************************/
935
936 static void dgap_poll_handler(ulong dummy)
937 {
938         int i;
939         struct board_t *brd;
940         unsigned long lock_flags;
941         unsigned long lock_flags2;
942         ulong new_time;
943
944         dgap_poll_counter++;
945
946
947         /*
948          * If driver needs the config file still,
949          * keep trying to wake up the downloader to
950          * send us the file.
951          */
952         if (dgap_driver_state == DRIVER_NEED_CONFIG_LOAD) {
953                 /*
954                  * Signal downloader, its got some work to do.
955                  */
956                 DGAP_LOCK(dgap_dl_lock, lock_flags2);
957                 if (dgap_dl_action != 1) {
958                         dgap_dl_action = 1;
959                         wake_up_interruptible(&dgap_dl_wait);
960                 }
961                 DGAP_UNLOCK(dgap_dl_lock, lock_flags2);
962                 goto schedule_poller;
963         }
964         /*
965          * Do not start the board state machine until
966          * driver tells us its up and running, and has
967          * everything it needs.
968          */
969         else if (dgap_driver_state != DRIVER_READY) {
970                 goto schedule_poller;
971         }
972
973         /*
974          * If we have just 1 board, or the system is not SMP,
975          * then use the typical old style poller.
976          * Otherwise, use our new tasklet based poller, which should
977          * speed things up for multiple boards.
978          */
979         if ( (dgap_NumBoards == 1) || (num_online_cpus() <= 1) ) {
980                 for (i = 0; i < dgap_NumBoards; i++) {
981
982                         brd = dgap_Board[i];
983
984                         if (brd->state == BOARD_FAILED) {
985                                 continue;
986                         }
987                         if (!brd->intr_running) {
988                                 /* Call the real board poller directly */
989                                 dgap_poll_tasklet((unsigned long) brd);
990                         }
991                 }
992         }
993         else {
994                 /* Go thru each board, kicking off a tasklet for each if needed */
995                 for (i = 0; i < dgap_NumBoards; i++) {
996                         brd = dgap_Board[i];
997
998                         /*
999                          * Attempt to grab the board lock.
1000                          *
1001                          * If we can't get it, no big deal, the next poll will get it.
1002                          * Basically, I just really don't want to spin in here, because I want
1003                          * to kick off my tasklets as fast as I can, and then get out the poller.
1004                          */
1005                         if (!spin_trylock(&brd->bd_lock)) {
1006                                 continue;
1007                         }
1008
1009                         /* If board is in a failed state, don't bother scheduling a tasklet */
1010                         if (brd->state == BOARD_FAILED) {
1011                                 spin_unlock(&brd->bd_lock);
1012                                 continue;
1013                         }
1014
1015                         /* Schedule a poll helper task */
1016                         if (!brd->intr_running) {
1017                                 tasklet_schedule(&brd->helper_tasklet);
1018                         }
1019
1020                         /*
1021                          * Can't do DGAP_UNLOCK here, as we don't have
1022                          * lock_flags because we did a trylock above.
1023                          */
1024                         spin_unlock(&brd->bd_lock);
1025                 }
1026         }
1027
1028 schedule_poller:
1029
1030         /*
1031          * Schedule ourself back at the nominal wakeup interval.
1032          */
1033         DGAP_LOCK(dgap_poll_lock, lock_flags );
1034         dgap_poll_time +=  dgap_jiffies_from_ms(dgap_poll_tick);
1035
1036         new_time = dgap_poll_time - jiffies;
1037
1038         if ((ulong) new_time >= 2 * dgap_poll_tick) {
1039                 dgap_poll_time = jiffies +  dgap_jiffies_from_ms(dgap_poll_tick);
1040         }
1041
1042         dgap_poll_timer.function = dgap_poll_handler;
1043         dgap_poll_timer.data = 0;
1044         dgap_poll_timer.expires = dgap_poll_time;
1045         DGAP_UNLOCK(dgap_poll_lock, lock_flags );
1046
1047         if (!dgap_poll_stop)
1048                 add_timer(&dgap_poll_timer);
1049 }
1050
1051
1052
1053
1054 /*
1055  * dgap_intr()
1056  *
1057  * Driver interrupt handler.
1058  */
1059 static irqreturn_t dgap_intr(int irq, void *voidbrd)
1060 {
1061         struct board_t *brd = (struct board_t *) voidbrd;
1062
1063         if (!brd) {
1064                 APR(("Received interrupt (%d) with null board associated\n", irq));
1065                 return IRQ_NONE;
1066         }
1067
1068         /*
1069          * Check to make sure its for us.
1070          */
1071         if (brd->magic != DGAP_BOARD_MAGIC) {
1072                 APR(("Received interrupt (%d) with a board pointer that wasn't ours!\n", irq));
1073                 return IRQ_NONE;
1074         }
1075
1076         brd->intr_count++;
1077
1078         /*
1079          * Schedule tasklet to run at a better time.
1080          */
1081         tasklet_schedule(&brd->helper_tasklet);
1082         return IRQ_HANDLED;
1083 }
1084
1085
1086 /*
1087  * dgap_init_globals()
1088  *
1089  * This is where we initialize the globals from the static insmod
1090  * configuration variables.  These are declared near the head of
1091  * this file.
1092  */
1093 static void dgap_init_globals(void)
1094 {
1095         int i = 0;
1096
1097         dgap_rawreadok          = rawreadok;
1098         dgap_trcbuf_size        = trcbuf_size;
1099         dgap_debug              = debug;
1100
1101         for (i = 0; i < MAXBOARDS; i++) {
1102                 dgap_Board[i] = NULL;
1103         }
1104
1105         init_timer( &dgap_poll_timer );
1106
1107         init_waitqueue_head(&dgap_dl_wait);
1108         dgap_dl_action = 0;
1109 }
1110
1111
1112 /************************************************************************
1113  *
1114  * Utility functions
1115  *
1116  ************************************************************************/
1117
1118
1119 /*
1120  * dgap_mbuf()
1121  *
1122  * Used to print to the message buffer during board init.
1123  */
1124 static void dgap_mbuf(struct board_t *brd, const char *fmt, ...) {
1125         va_list         ap;
1126         char            buf[1024];
1127         int             i;
1128         unsigned long   flags;
1129         size_t          length;
1130
1131         DGAP_LOCK(dgap_global_lock, flags);
1132
1133         /* Format buf using fmt and arguments contained in ap. */
1134         va_start(ap, fmt);
1135         i = vsnprintf(buf, sizeof(buf), fmt,  ap);
1136         va_end(ap);
1137
1138         DPR((buf));
1139
1140         if (!brd || !brd->msgbuf) {
1141                 printk("%s", buf);
1142                 DGAP_UNLOCK(dgap_global_lock, flags);
1143                 return;
1144         }
1145
1146         length = strlen(buf) + 1;
1147         if (brd->msgbuf - brd->msgbuf_head < length)
1148                 length = brd->msgbuf - brd->msgbuf_head;
1149         memcpy(brd->msgbuf, buf, length);
1150         brd->msgbuf += length;
1151
1152         DGAP_UNLOCK(dgap_global_lock, flags);
1153 }
1154
1155
1156 /*
1157  * dgap_ms_sleep()
1158  *
1159  * Put the driver to sleep for x ms's
1160  *
1161  * Returns 0 if timed out, !0 (showing signal) if interrupted by a signal.
1162  */
1163 int dgap_ms_sleep(ulong ms)
1164 {
1165         current->state = TASK_INTERRUPTIBLE;
1166         schedule_timeout((ms * HZ) / 1000);
1167         return (signal_pending(current));
1168 }
1169
1170
1171
1172 /*
1173  *      dgap_ioctl_name() : Returns a text version of each ioctl value.
1174  */
1175 char *dgap_ioctl_name(int cmd)
1176 {
1177         switch(cmd) {
1178
1179         case TCGETA:            return("TCGETA");
1180         case TCGETS:            return("TCGETS");
1181         case TCSETA:            return("TCSETA");
1182         case TCSETS:            return("TCSETS");
1183         case TCSETAW:           return("TCSETAW");
1184         case TCSETSW:           return("TCSETSW");
1185         case TCSETAF:           return("TCSETAF");
1186         case TCSETSF:           return("TCSETSF");
1187         case TCSBRK:            return("TCSBRK");
1188         case TCXONC:            return("TCXONC");
1189         case TCFLSH:            return("TCFLSH");
1190         case TIOCGSID:          return("TIOCGSID");
1191
1192         case TIOCGETD:          return("TIOCGETD");
1193         case TIOCSETD:          return("TIOCSETD");
1194         case TIOCGWINSZ:        return("TIOCGWINSZ");
1195         case TIOCSWINSZ:        return("TIOCSWINSZ");
1196
1197         case TIOCMGET:          return("TIOCMGET");
1198         case TIOCMSET:          return("TIOCMSET");
1199         case TIOCMBIS:          return("TIOCMBIS");
1200         case TIOCMBIC:          return("TIOCMBIC");
1201
1202         /* from digi.h */
1203         case DIGI_SETA:         return("DIGI_SETA");
1204         case DIGI_SETAW:        return("DIGI_SETAW");
1205         case DIGI_SETAF:        return("DIGI_SETAF");
1206         case DIGI_SETFLOW:      return("DIGI_SETFLOW");
1207         case DIGI_SETAFLOW:     return("DIGI_SETAFLOW");
1208         case DIGI_GETFLOW:      return("DIGI_GETFLOW");
1209         case DIGI_GETAFLOW:     return("DIGI_GETAFLOW");
1210         case DIGI_GETA:         return("DIGI_GETA");
1211         case DIGI_GEDELAY:      return("DIGI_GEDELAY");
1212         case DIGI_SEDELAY:      return("DIGI_SEDELAY");
1213         case DIGI_GETCUSTOMBAUD: return("DIGI_GETCUSTOMBAUD");
1214         case DIGI_SETCUSTOMBAUD: return("DIGI_SETCUSTOMBAUD");
1215         case TIOCMODG:          return("TIOCMODG");
1216         case TIOCMODS:          return("TIOCMODS");
1217         case TIOCSDTR:          return("TIOCSDTR");
1218         case TIOCCDTR:          return("TIOCCDTR");
1219
1220         default:                return("unknown");
1221         }
1222 }
1223
1224 /************************************************************************
1225  *
1226  * TTY Initialization/Cleanup Functions
1227  *
1228  ************************************************************************/
1229
1230 /*
1231  * dgap_tty_preinit()
1232  *
1233  * Initialize any global tty related data before we download any boards.
1234  */
1235 int dgap_tty_preinit(void)
1236 {
1237         unsigned long flags;
1238
1239         DGAP_LOCK(dgap_global_lock, flags);
1240
1241         /*
1242          * Allocate a buffer for doing the copy from user space to
1243          * kernel space in dgap_input().  We only use one buffer and
1244          * control access to it with a semaphore.  If we are paging, we
1245          * are already in trouble so one buffer won't hurt much anyway.
1246          */
1247         dgap_TmpWriteBuf = kmalloc(WRITEBUFLEN, GFP_ATOMIC);
1248
1249         if (!dgap_TmpWriteBuf) {
1250                 DGAP_UNLOCK(dgap_global_lock, flags);
1251                 DPR_INIT(("unable to allocate tmp write buf"));
1252                 return (-ENOMEM);
1253         }
1254
1255         DGAP_UNLOCK(dgap_global_lock, flags);
1256         return(0);
1257 }
1258
1259
1260 /*
1261  * dgap_tty_register()
1262  *
1263  * Init the tty subsystem for this board.
1264  */
1265 int dgap_tty_register(struct board_t *brd)
1266 {
1267         int rc = 0;
1268
1269         DPR_INIT(("tty_register start"));
1270
1271         brd->SerialDriver = alloc_tty_driver(MAXPORTS);
1272
1273         snprintf(brd->SerialName, MAXTTYNAMELEN, "tty_dgap_%d_", brd->boardnum);
1274         brd->SerialDriver->name = brd->SerialName;
1275         brd->SerialDriver->name_base = 0;
1276         brd->SerialDriver->major = 0;
1277         brd->SerialDriver->minor_start = 0;
1278         brd->SerialDriver->type = TTY_DRIVER_TYPE_SERIAL;
1279         brd->SerialDriver->subtype = SERIAL_TYPE_NORMAL;
1280         brd->SerialDriver->init_termios = DgapDefaultTermios;
1281         brd->SerialDriver->driver_name = DRVSTR;
1282         brd->SerialDriver->flags = (TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_HARDWARE_BREAK);
1283
1284         /* The kernel wants space to store pointers to tty_structs */
1285         brd->SerialDriver->ttys = kzalloc(MAXPORTS * sizeof(struct tty_struct *), GFP_KERNEL);
1286         if (!brd->SerialDriver->ttys)
1287                 return(-ENOMEM);
1288
1289         /*
1290          * Entry points for driver.  Called by the kernel from
1291          * tty_io.c and n_tty.c.
1292          */
1293         tty_set_operations(brd->SerialDriver, &dgap_tty_ops);
1294
1295         /*
1296          * If we're doing transparent print, we have to do all of the above
1297          * again, separately so we don't get the LD confused about what major
1298          * we are when we get into the dgap_tty_open() routine.
1299          */
1300         brd->PrintDriver = alloc_tty_driver(MAXPORTS);
1301
1302         snprintf(brd->PrintName, MAXTTYNAMELEN, "pr_dgap_%d_", brd->boardnum);
1303         brd->PrintDriver->name = brd->PrintName;
1304         brd->PrintDriver->name_base = 0;
1305         brd->PrintDriver->major = 0;
1306         brd->PrintDriver->minor_start = 0;
1307         brd->PrintDriver->type = TTY_DRIVER_TYPE_SERIAL;
1308         brd->PrintDriver->subtype = SERIAL_TYPE_NORMAL;
1309         brd->PrintDriver->init_termios = DgapDefaultTermios;
1310         brd->PrintDriver->driver_name = DRVSTR;
1311         brd->PrintDriver->flags = (TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_HARDWARE_BREAK);
1312
1313         /* The kernel wants space to store pointers to tty_structs */
1314         brd->PrintDriver->ttys = kzalloc(MAXPORTS * sizeof(struct tty_struct *), GFP_KERNEL);
1315         if (!brd->PrintDriver->ttys)
1316                 return(-ENOMEM);
1317
1318         /*
1319          * Entry points for driver.  Called by the kernel from
1320          * tty_io.c and n_tty.c.
1321          */
1322         tty_set_operations(brd->PrintDriver, &dgap_tty_ops);
1323
1324         if (!brd->dgap_Major_Serial_Registered) {
1325                 /* Register tty devices */
1326                 rc = tty_register_driver(brd->SerialDriver);
1327                 if (rc < 0) {
1328                         APR(("Can't register tty device (%d)\n", rc));
1329                         return(rc);
1330                 }
1331                 brd->dgap_Major_Serial_Registered = TRUE;
1332                 dgap_BoardsByMajor[brd->SerialDriver->major] = brd;
1333                 brd->dgap_Serial_Major = brd->SerialDriver->major;
1334         }
1335
1336         if (!brd->dgap_Major_TransparentPrint_Registered) {
1337                 /* Register Transparent Print devices */
1338                 rc = tty_register_driver(brd->PrintDriver);
1339                 if (rc < 0) {
1340                         APR(("Can't register Transparent Print device (%d)\n", rc));
1341                         return(rc);
1342                 }
1343                 brd->dgap_Major_TransparentPrint_Registered = TRUE;
1344                 dgap_BoardsByMajor[brd->PrintDriver->major] = brd;
1345                 brd->dgap_TransparentPrint_Major = brd->PrintDriver->major;
1346         }
1347
1348         DPR_INIT(("DGAP REGISTER TTY: MAJORS: %d %d\n", brd->SerialDriver->major,
1349                 brd->PrintDriver->major));
1350
1351         return (rc);
1352 }
1353
1354
1355 /*
1356  * dgap_tty_init()
1357  *
1358  * Init the tty subsystem.  Called once per board after board has been
1359  * downloaded and init'ed.
1360  */
1361 int dgap_tty_init(struct board_t *brd)
1362 {
1363         int i;
1364         int tlw;
1365         uint true_count = 0;
1366         uchar *vaddr;
1367         uchar modem = 0;
1368         struct channel_t *ch;
1369         struct bs_t *bs;
1370         struct cm_t *cm;
1371
1372         if (!brd)
1373                 return (-ENXIO);
1374
1375         DPR_INIT(("dgap_tty_init start\n"));
1376
1377         /*
1378          * Initialize board structure elements.
1379          */
1380
1381         vaddr = brd->re_map_membase;
1382         true_count = readw((vaddr + NCHAN));
1383
1384         brd->nasync = dgap_config_get_number_of_ports(brd);
1385
1386         if (!brd->nasync) {
1387                 brd->nasync = brd->maxports;
1388         }
1389
1390         if (brd->nasync > brd->maxports) {
1391                 brd->nasync = brd->maxports;
1392         }
1393
1394         if (true_count != brd->nasync) {
1395                 if ((brd->type == PPCM) && (true_count == 64)) {
1396                         APR(("***WARNING**** %s configured for %d ports, has %d ports.\nPlease make SURE the EBI cable running from the card\nto each EM module is plugged into EBI IN!\n",
1397                                 brd->name, brd->nasync, true_count));
1398                 }
1399                 else if ((brd->type == PPCM) && (true_count == 0)) {
1400                         APR(("***WARNING**** %s configured for %d ports, has %d ports.\nPlease make SURE the EBI cable running from the card\nto each EM module is plugged into EBI IN!\n",
1401                                 brd->name, brd->nasync, true_count));
1402                 }
1403                 else {
1404                         APR(("***WARNING**** %s configured for %d ports, has %d ports.\n",
1405                                 brd->name, brd->nasync, true_count));
1406                 }
1407
1408                 brd->nasync = true_count;
1409
1410                 /* If no ports, don't bother going any further */
1411                 if (!brd->nasync) {
1412                         brd->state = BOARD_FAILED;
1413                         brd->dpastatus = BD_NOFEP;
1414                         return(-ENXIO);
1415                 }
1416         }
1417
1418         /*
1419          * Allocate channel memory that might not have been allocated
1420          * when the driver was first loaded.
1421          */
1422         for (i = 0; i < brd->nasync; i++) {
1423                 if (!brd->channels[i]) {
1424                         brd->channels[i] = kzalloc(sizeof(struct channel_t), GFP_ATOMIC);
1425                         if (!brd->channels[i]) {
1426                                 DPR_CORE(("%s:%d Unable to allocate memory for channel struct\n",
1427                                     __FILE__, __LINE__));
1428                         }
1429                 }
1430         }
1431
1432         ch = brd->channels[0];
1433         vaddr = brd->re_map_membase;
1434
1435         bs = (struct bs_t *) ((ulong) vaddr + CHANBUF);
1436         cm = (struct cm_t *) ((ulong) vaddr + CMDBUF);
1437
1438         brd->bd_bs = bs;
1439
1440         /* Set up channel variables */
1441         for (i = 0; i < brd->nasync; i++, ch = brd->channels[i], bs++) {
1442
1443                 if (!brd->channels[i])
1444                         continue;
1445
1446                 DGAP_SPINLOCK_INIT(ch->ch_lock);
1447
1448                 /* Store all our magic numbers */
1449                 ch->magic = DGAP_CHANNEL_MAGIC;
1450                 ch->ch_tun.magic = DGAP_UNIT_MAGIC;
1451                 ch->ch_tun.un_type = DGAP_SERIAL;
1452                 ch->ch_tun.un_ch = ch;
1453                 ch->ch_tun.un_dev = i;
1454
1455                 ch->ch_pun.magic = DGAP_UNIT_MAGIC;
1456                 ch->ch_pun.un_type = DGAP_PRINT;
1457                 ch->ch_pun.un_ch = ch;
1458                 ch->ch_pun.un_dev = i;
1459
1460                 ch->ch_vaddr = vaddr;
1461                 ch->ch_bs = bs;
1462                 ch->ch_cm = cm;
1463                 ch->ch_bd = brd;
1464                 ch->ch_portnum = i;
1465                 ch->ch_digi = dgap_digi_init;
1466
1467                 /*
1468                  * Set up digi dsr and dcd bits based on altpin flag.
1469                  */
1470                 if (dgap_config_get_altpin(brd)) {
1471                         ch->ch_dsr      = DM_CD;
1472                         ch->ch_cd       = DM_DSR;
1473                         ch->ch_digi.digi_flags |= DIGI_ALTPIN;
1474                 }
1475                 else {
1476                         ch->ch_cd       = DM_CD;
1477                         ch->ch_dsr      = DM_DSR;
1478                 }
1479
1480                 ch->ch_taddr = vaddr + ((ch->ch_bs->tx_seg) << 4);
1481                 ch->ch_raddr = vaddr + ((ch->ch_bs->rx_seg) << 4);
1482                 ch->ch_tx_win = 0;
1483                 ch->ch_rx_win = 0;
1484                 ch->ch_tsize = readw(&(ch->ch_bs->tx_max)) + 1;
1485                 ch->ch_rsize = readw(&(ch->ch_bs->rx_max)) + 1;
1486                 ch->ch_tstart = 0;
1487                 ch->ch_rstart = 0;
1488
1489                 /* .25 second delay */
1490                 ch->ch_close_delay = 250;
1491
1492                 /*
1493                  * Set queue water marks, interrupt mask,
1494                  * and general tty parameters.
1495                  */
1496                 ch->ch_tlw = tlw = ch->ch_tsize >= 2000 ? ((ch->ch_tsize * 5) / 8) : ch->ch_tsize / 2;
1497
1498                 dgap_cmdw(ch, STLOW, tlw, 0);
1499
1500                 dgap_cmdw(ch, SRLOW, ch->ch_rsize / 2, 0);
1501
1502                 dgap_cmdw(ch, SRHIGH, 7 * ch->ch_rsize / 8, 0);
1503
1504                 ch->ch_mistat = readb(&(ch->ch_bs->m_stat));
1505
1506                 init_waitqueue_head(&ch->ch_flags_wait);
1507                 init_waitqueue_head(&ch->ch_tun.un_flags_wait);
1508                 init_waitqueue_head(&ch->ch_pun.un_flags_wait);
1509                 init_waitqueue_head(&ch->ch_sniff_wait);
1510
1511                 /* Turn on all modem interrupts for now */
1512                 modem = (DM_CD | DM_DSR | DM_CTS | DM_RI);
1513                 writeb(modem, &(ch->ch_bs->m_int));
1514
1515                 /*
1516                  * Set edelay to 0 if interrupts are turned on,
1517                  * otherwise set edelay to the usual 100.
1518                  */
1519                 if (brd->intr_used)
1520                         writew(0, &(ch->ch_bs->edelay));
1521                 else
1522                         writew(100, &(ch->ch_bs->edelay));
1523
1524                 writeb(1, &(ch->ch_bs->idata));
1525         }
1526
1527
1528         DPR_INIT(("dgap_tty_init finish\n"));
1529
1530         return (0);
1531 }
1532
1533
1534 /*
1535  * dgap_tty_post_uninit()
1536  *
1537  * UnInitialize any global tty related data.
1538  */
1539 void dgap_tty_post_uninit(void)
1540 {
1541         kfree(dgap_TmpWriteBuf);
1542         dgap_TmpWriteBuf = NULL;
1543 }
1544
1545
1546 /*
1547  * dgap_tty_uninit()
1548  *
1549  * Uninitialize the TTY portion of this driver.  Free all memory and
1550  * resources.
1551  */
1552 void dgap_tty_uninit(struct board_t *brd)
1553 {
1554         int i = 0;
1555
1556         if (brd->dgap_Major_Serial_Registered) {
1557                 dgap_BoardsByMajor[brd->SerialDriver->major] = NULL;
1558                 brd->dgap_Serial_Major = 0;
1559                 for (i = 0; i < brd->nasync; i++) {
1560                         dgap_remove_tty_sysfs(brd->channels[i]->ch_tun.un_sysfs);
1561                         tty_unregister_device(brd->SerialDriver, i);
1562                 }
1563                 tty_unregister_driver(brd->SerialDriver);
1564                 kfree(brd->SerialDriver->ttys);
1565                 brd->SerialDriver->ttys = NULL;
1566                 put_tty_driver(brd->SerialDriver);
1567                 brd->dgap_Major_Serial_Registered = FALSE;
1568         }
1569
1570         if (brd->dgap_Major_TransparentPrint_Registered) {
1571                 dgap_BoardsByMajor[brd->PrintDriver->major] = NULL;
1572                 brd->dgap_TransparentPrint_Major = 0;
1573                 for (i = 0; i < brd->nasync; i++) {
1574                         dgap_remove_tty_sysfs(brd->channels[i]->ch_pun.un_sysfs);
1575                         tty_unregister_device(brd->PrintDriver, i);
1576                 }
1577                 tty_unregister_driver(brd->PrintDriver);
1578                 kfree(brd->PrintDriver->ttys);
1579                 brd->PrintDriver->ttys = NULL;
1580                 put_tty_driver(brd->PrintDriver);
1581                 brd->dgap_Major_TransparentPrint_Registered = FALSE;
1582         }
1583 }
1584
1585
1586 #define TMPBUFLEN (1024)
1587
1588 /*
1589  * dgap_sniff - Dump data out to the "sniff" buffer if the
1590  * proc sniff file is opened...
1591  */
1592 static void dgap_sniff_nowait_nolock(struct channel_t *ch, uchar *text, uchar *buf, int len)
1593 {
1594         struct timeval tv;
1595         int n;
1596         int r;
1597         int nbuf;
1598         int i;
1599         int tmpbuflen;
1600         char tmpbuf[TMPBUFLEN];
1601         char *p = tmpbuf;
1602         int too_much_data;
1603
1604         /* Leave if sniff not open */
1605         if (!(ch->ch_sniff_flags & SNIFF_OPEN))
1606                 return;
1607
1608         do_gettimeofday(&tv);
1609
1610         /* Create our header for data dump */
1611         p += sprintf(p, "<%ld %ld><%s><", tv.tv_sec, tv.tv_usec, text);
1612         tmpbuflen = p - tmpbuf;
1613
1614         do {
1615                 too_much_data = 0;
1616
1617                 for (i = 0; i < len && tmpbuflen < (TMPBUFLEN - 4); i++) {
1618                         p += sprintf(p, "%02x ", *buf);
1619                         buf++;
1620                         tmpbuflen = p - tmpbuf;
1621                 }
1622
1623                 if (tmpbuflen < (TMPBUFLEN - 4)) {
1624                         if (i > 0)
1625                                 p += sprintf(p - 1, "%s\n", ">");
1626                         else
1627                                 p += sprintf(p, "%s\n", ">");
1628                 } else {
1629                         too_much_data = 1;
1630                         len -= i;
1631                 }
1632
1633                 nbuf = strlen(tmpbuf);
1634                 p = tmpbuf;
1635
1636                 /*
1637                  *  Loop while data remains.
1638                  */
1639                 while (nbuf > 0 && ch->ch_sniff_buf) {
1640                         /*
1641                          *  Determine the amount of available space left in the
1642                          *  buffer.  If there's none, wait until some appears.
1643                          */
1644                         n = (ch->ch_sniff_out - ch->ch_sniff_in - 1) & SNIFF_MASK;
1645
1646                         /*
1647                          * If there is no space left to write to in our sniff buffer,
1648                          * we have no choice but to drop the data.
1649                          * We *cannot* sleep here waiting for space, because this
1650                          * function was probably called by the interrupt/timer routines!
1651                          */
1652                         if (n == 0) {
1653                                 return;
1654                         }
1655
1656                         /*
1657                          * Copy as much data as will fit.
1658                          */
1659
1660                         if (n > nbuf)
1661                                 n = nbuf;
1662
1663                         r = SNIFF_MAX - ch->ch_sniff_in;
1664
1665                         if (r <= n) {
1666                                 memcpy(ch->ch_sniff_buf + ch->ch_sniff_in, p, r);
1667
1668                                 n -= r;
1669                                 ch->ch_sniff_in = 0;
1670                                 p += r;
1671                                 nbuf -= r;
1672                         }
1673
1674                         memcpy(ch->ch_sniff_buf + ch->ch_sniff_in, p, n);
1675
1676                         ch->ch_sniff_in += n;
1677                         p += n;
1678                         nbuf -= n;
1679
1680                         /*
1681                          *  Wakeup any thread waiting for data
1682                          */
1683                         if (ch->ch_sniff_flags & SNIFF_WAIT_DATA) {
1684                                 ch->ch_sniff_flags &= ~SNIFF_WAIT_DATA;
1685                                 wake_up_interruptible(&ch->ch_sniff_wait);
1686                         }
1687                 }
1688
1689                 /*
1690                  * If the user sent us too much data to push into our tmpbuf,
1691                  * we need to keep looping around on all the data.
1692                  */
1693                 if (too_much_data) {
1694                         p = tmpbuf;
1695                         tmpbuflen = 0;
1696                 }
1697
1698         } while (too_much_data);
1699 }
1700
1701
1702 /*=======================================================================
1703  *
1704  *      dgap_input - Process received data.
1705  *
1706  *              ch      - Pointer to channel structure.
1707  *
1708  *=======================================================================*/
1709
1710 void dgap_input(struct channel_t *ch)
1711 {
1712         struct board_t *bd;
1713         struct bs_t     *bs;
1714         struct tty_struct *tp;
1715         struct tty_ldisc *ld;
1716         uint    rmask;
1717         uint    head;
1718         uint    tail;
1719         int     data_len;
1720         ulong   lock_flags;
1721         ulong   lock_flags2;
1722         int flip_len;
1723         int len = 0;
1724         int n = 0;
1725         uchar *buf;
1726         uchar tmpchar;
1727         int s = 0;
1728
1729         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
1730                 return;
1731
1732         tp = ch->ch_tun.un_tty;
1733
1734         bs  = ch->ch_bs;
1735         if (!bs) {
1736                 return;
1737         }
1738
1739         bd = ch->ch_bd;
1740         if(!bd || bd->magic != DGAP_BOARD_MAGIC)
1741                 return;
1742
1743         DPR_READ(("dgap_input start\n"));
1744
1745         DGAP_LOCK(bd->bd_lock, lock_flags);
1746         DGAP_LOCK(ch->ch_lock, lock_flags2);
1747
1748         /*
1749          *      Figure the number of characters in the buffer.
1750          *      Exit immediately if none.
1751          */
1752
1753         rmask = ch->ch_rsize - 1;
1754
1755         head = readw(&(bs->rx_head));
1756         head &= rmask;
1757         tail = readw(&(bs->rx_tail));
1758         tail &= rmask;
1759
1760         data_len = (head - tail) & rmask;
1761
1762         if (data_len == 0) {
1763                 writeb(1, &(bs->idata));
1764                 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
1765                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
1766                 DPR_READ(("No data on port %d\n", ch->ch_portnum));
1767                 return;
1768         }
1769
1770         /*
1771          * If the device is not open, or CREAD is off, flush
1772          * input data and return immediately.
1773          */
1774         if ((bd->state != BOARD_READY) || !tp  || (tp->magic != TTY_MAGIC) ||
1775             !(ch->ch_tun.un_flags & UN_ISOPEN) || !(tp->termios.c_cflag & CREAD) ||
1776             (ch->ch_tun.un_flags & UN_CLOSING)) {
1777
1778                 DPR_READ(("input. dropping %d bytes on port %d...\n", data_len, ch->ch_portnum));
1779                 DPR_READ(("input. tp: %p tp->magic: %x MAGIC:%x ch flags: %x\n",
1780                         tp, tp ? tp->magic : 0, TTY_MAGIC, ch->ch_tun.un_flags));
1781                 writew(head, &(bs->rx_tail));
1782                 writeb(1, &(bs->idata));
1783                 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
1784                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
1785                 return;
1786         }
1787
1788         /*
1789          * If we are throttled, simply don't read any data.
1790          */
1791         if (ch->ch_flags & CH_RXBLOCK) {
1792                 writeb(1, &(bs->idata));
1793                 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
1794                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
1795                 DPR_READ(("Port %d throttled, not reading any data. head: %x tail: %x\n",
1796                         ch->ch_portnum, head, tail));
1797                 return;
1798         }
1799
1800         /*
1801          *      Ignore oruns.
1802          */
1803         tmpchar = readb(&(bs->orun));
1804         if (tmpchar) {
1805                 ch->ch_err_overrun++;
1806                 writeb(0, &(bs->orun));
1807         }
1808
1809         DPR_READ(("dgap_input start 2\n"));
1810
1811         /* Decide how much data we can send into the tty layer */
1812         flip_len = TTY_FLIPBUF_SIZE;
1813
1814         /* Chop down the length, if needed */
1815         len = min(data_len, flip_len);
1816         len = min(len, (N_TTY_BUF_SIZE - 1));
1817
1818         ld = tty_ldisc_ref(tp);
1819
1820 #ifdef TTY_DONT_FLIP
1821         /*
1822          * If the DONT_FLIP flag is on, don't flush our buffer, and act
1823          * like the ld doesn't have any space to put the data right now.
1824          */
1825         if (test_bit(TTY_DONT_FLIP, &tp->flags))
1826                 len = 0;
1827 #endif
1828
1829         /*
1830          * If we were unable to get a reference to the ld,
1831          * don't flush our buffer, and act like the ld doesn't
1832          * have any space to put the data right now.
1833          */
1834         if (!ld) {
1835                 len = 0;
1836         } else {
1837                 /*
1838                  * If ld doesn't have a pointer to a receive_buf function,
1839                  * flush the data, then act like the ld doesn't have any
1840                  * space to put the data right now.
1841                  */
1842                 if (!ld->ops->receive_buf) {
1843                         writew(head, &(bs->rx_tail));
1844                         len = 0;
1845                 }
1846         }
1847
1848         if (len <= 0) {
1849                 writeb(1, &(bs->idata));
1850                 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
1851                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
1852                 DPR_READ(("dgap_input 1 - finish\n"));
1853                 if (ld)
1854                         tty_ldisc_deref(ld);
1855                 return;
1856         }
1857
1858         buf = ch->ch_bd->flipbuf;
1859         n = len;
1860
1861         /*
1862          * n now contains the most amount of data we can copy,
1863          * bounded either by our buffer size or the amount
1864          * of data the card actually has pending...
1865          */
1866         while (n) {
1867
1868                 s = ((head >= tail) ? head : ch->ch_rsize) - tail;
1869                 s = min(s, n);
1870
1871                 if (s <= 0)
1872                         break;
1873
1874                 memcpy_fromio(buf, (char *) ch->ch_raddr + tail, s);
1875                 dgap_sniff_nowait_nolock(ch, "USER READ", buf, s);
1876
1877                 tail += s;
1878                 buf += s;
1879
1880                 n -= s;
1881                 /* Flip queue if needed */
1882                 tail &= rmask;
1883         }
1884
1885         writew(tail, &(bs->rx_tail));
1886         writeb(1, &(bs->idata));
1887         ch->ch_rxcount += len;
1888
1889         /*
1890          * If we are completely raw, we don't need to go through a lot
1891          * of the tty layers that exist.
1892          * In this case, we take the shortest and fastest route we
1893          * can to relay the data to the user.
1894          *
1895          * On the other hand, if we are not raw, we need to go through
1896          * the tty layer, which has its API more well defined.
1897          */
1898         if (I_PARMRK(tp) || I_BRKINT(tp) || I_INPCK(tp)) {
1899                 dgap_parity_scan(ch, ch->ch_bd->flipbuf, ch->ch_bd->flipflagbuf, &len);
1900
1901                 len = tty_buffer_request_room(tp->port, len);
1902                 tty_insert_flip_string_flags(tp->port, ch->ch_bd->flipbuf,
1903                         ch->ch_bd->flipflagbuf, len);
1904         }
1905         else {
1906                 len = tty_buffer_request_room(tp->port, len);
1907                 tty_insert_flip_string(tp->port, ch->ch_bd->flipbuf, len);
1908         }
1909
1910         DGAP_UNLOCK(ch->ch_lock, lock_flags2);
1911         DGAP_UNLOCK(bd->bd_lock, lock_flags);
1912
1913         /* Tell the tty layer its okay to "eat" the data now */
1914         tty_flip_buffer_push(tp->port);
1915
1916         if (ld)
1917                 tty_ldisc_deref(ld);
1918
1919         DPR_READ(("dgap_input - finish\n"));
1920 }
1921
1922
1923 /************************************************************************
1924  * Determines when CARRIER changes state and takes appropriate
1925  * action.
1926  ************************************************************************/
1927 void dgap_carrier(struct channel_t *ch)
1928 {
1929         struct board_t *bd;
1930
1931         int virt_carrier = 0;
1932         int phys_carrier = 0;
1933
1934         DPR_CARR(("dgap_carrier called...\n"));
1935
1936         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
1937                 return;
1938
1939         bd = ch->ch_bd;
1940
1941         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
1942                 return;
1943
1944         /* Make sure altpin is always set correctly */
1945         if (ch->ch_digi.digi_flags & DIGI_ALTPIN) {
1946                 ch->ch_dsr      = DM_CD;
1947                 ch->ch_cd       = DM_DSR;
1948         }
1949         else {
1950                 ch->ch_dsr      = DM_DSR;
1951                 ch->ch_cd       = DM_CD;
1952         }
1953
1954         if (ch->ch_mistat & D_CD(ch)) {
1955                 DPR_CARR(("mistat: %x  D_CD: %x\n", ch->ch_mistat, D_CD(ch)));
1956                 phys_carrier = 1;
1957         }
1958
1959         if (ch->ch_digi.digi_flags & DIGI_FORCEDCD) {
1960                 virt_carrier = 1;
1961         }
1962
1963         if (ch->ch_c_cflag & CLOCAL) {
1964                 virt_carrier = 1;
1965         }
1966
1967
1968         DPR_CARR(("DCD: physical: %d virt: %d\n", phys_carrier, virt_carrier));
1969
1970         /*
1971          * Test for a VIRTUAL carrier transition to HIGH.
1972          */
1973         if (((ch->ch_flags & CH_FCAR) == 0) && (virt_carrier == 1)) {
1974
1975                 /*
1976                  * When carrier rises, wake any threads waiting
1977                  * for carrier in the open routine.
1978                  */
1979
1980                 DPR_CARR(("carrier: virt DCD rose\n"));
1981
1982                 if (waitqueue_active(&(ch->ch_flags_wait)))
1983                         wake_up_interruptible(&ch->ch_flags_wait);
1984         }
1985
1986         /*
1987          * Test for a PHYSICAL carrier transition to HIGH.
1988          */
1989         if (((ch->ch_flags & CH_CD) == 0) && (phys_carrier == 1)) {
1990
1991                 /*
1992                  * When carrier rises, wake any threads waiting
1993                  * for carrier in the open routine.
1994                  */
1995
1996                 DPR_CARR(("carrier: physical DCD rose\n"));
1997
1998                 if (waitqueue_active(&(ch->ch_flags_wait)))
1999                         wake_up_interruptible(&ch->ch_flags_wait);
2000         }
2001
2002         /*
2003          *  Test for a PHYSICAL transition to low, so long as we aren't
2004          *  currently ignoring physical transitions (which is what "virtual
2005          *  carrier" indicates).
2006          *
2007          *  The transition of the virtual carrier to low really doesn't
2008          *  matter... it really only means "ignore carrier state", not
2009          *  "make pretend that carrier is there".
2010          */
2011         if ((virt_carrier == 0) && ((ch->ch_flags & CH_CD) != 0) &&
2012             (phys_carrier == 0))
2013         {
2014
2015                 /*
2016                  *   When carrier drops:
2017                  *
2018                  *   Drop carrier on all open units.
2019                  *
2020                  *   Flush queues, waking up any task waiting in the
2021                  *   line discipline.
2022                  *
2023                  *   Send a hangup to the control terminal.
2024                  *
2025                  *   Enable all select calls.
2026                  */
2027                 if (waitqueue_active(&(ch->ch_flags_wait)))
2028                         wake_up_interruptible(&ch->ch_flags_wait);
2029
2030                 if (ch->ch_tun.un_open_count > 0) {
2031                         DPR_CARR(("Sending tty hangup\n"));
2032                         tty_hangup(ch->ch_tun.un_tty);
2033                 }
2034
2035                 if (ch->ch_pun.un_open_count > 0) {
2036                         DPR_CARR(("Sending pr hangup\n"));
2037                         tty_hangup(ch->ch_pun.un_tty);
2038                 }
2039         }
2040
2041         /*
2042          *  Make sure that our cached values reflect the current reality.
2043          */
2044         if (virt_carrier == 1)
2045                 ch->ch_flags |= CH_FCAR;
2046         else
2047                 ch->ch_flags &= ~CH_FCAR;
2048
2049         if (phys_carrier == 1)
2050                 ch->ch_flags |= CH_CD;
2051         else
2052                 ch->ch_flags &= ~CH_CD;
2053 }
2054
2055
2056 /************************************************************************
2057  *
2058  * TTY Entry points and helper functions
2059  *
2060  ************************************************************************/
2061
2062 /*
2063  * dgap_tty_open()
2064  *
2065  */
2066 static int dgap_tty_open(struct tty_struct *tty, struct file *file)
2067 {
2068         struct board_t  *brd;
2069         struct channel_t *ch;
2070         struct un_t     *un;
2071         struct bs_t     *bs;
2072         uint            major = 0;
2073         uint            minor = 0;
2074         int             rc = 0;
2075         ulong           lock_flags;
2076         ulong           lock_flags2;
2077         u16             head;
2078
2079         rc = 0;
2080
2081         major = MAJOR(tty_devnum(tty));
2082         minor = MINOR(tty_devnum(tty));
2083
2084         if (major > 255) {
2085                 return -ENXIO;
2086         }
2087
2088         /* Get board pointer from our array of majors we have allocated */
2089         brd = dgap_BoardsByMajor[major];
2090         if (!brd) {
2091                 return -ENXIO;
2092         }
2093
2094         /*
2095          * If board is not yet up to a state of READY, go to
2096          * sleep waiting for it to happen or they cancel the open.
2097          */
2098         rc = wait_event_interruptible(brd->state_wait,
2099                 (brd->state & BOARD_READY));
2100
2101         if (rc) {
2102                 return rc;
2103         }
2104
2105         DGAP_LOCK(brd->bd_lock, lock_flags);
2106
2107         /* The wait above should guarantee this cannot happen */
2108         if (brd->state != BOARD_READY) {
2109                 DGAP_UNLOCK(brd->bd_lock, lock_flags);
2110                 return -ENXIO;
2111         }
2112
2113         /* If opened device is greater than our number of ports, bail. */
2114         if (MINOR(tty_devnum(tty)) > brd->nasync) {
2115                 DGAP_UNLOCK(brd->bd_lock, lock_flags);
2116                 return -ENXIO;
2117         }
2118
2119         ch = brd->channels[minor];
2120         if (!ch) {
2121                 DGAP_UNLOCK(brd->bd_lock, lock_flags);
2122                 return -ENXIO;
2123         }
2124
2125         /* Grab channel lock */
2126         DGAP_LOCK(ch->ch_lock, lock_flags2);
2127
2128         /* Figure out our type */
2129         if (major == brd->dgap_Serial_Major) {
2130                 un = &brd->channels[minor]->ch_tun;
2131                 un->un_type = DGAP_SERIAL;
2132         }
2133         else if (major == brd->dgap_TransparentPrint_Major) {
2134                 un = &brd->channels[minor]->ch_pun;
2135                 un->un_type = DGAP_PRINT;
2136         }
2137         else {
2138                 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
2139                 DGAP_UNLOCK(brd->bd_lock, lock_flags);
2140                 DPR_OPEN(("%d Unknown TYPE!\n", __LINE__));
2141                 return -ENXIO;
2142         }
2143
2144         /* Store our unit into driver_data, so we always have it available. */
2145         tty->driver_data = un;
2146
2147         DPR_OPEN(("Open called. MAJOR: %d MINOR:%d unit: %p NAME: %s\n",
2148                 MAJOR(tty_devnum(tty)), MINOR(tty_devnum(tty)), un, brd->name));
2149
2150         /*
2151          * Error if channel info pointer is NULL.
2152          */
2153         bs = ch->ch_bs;
2154         if (!bs) {
2155                 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
2156                 DGAP_UNLOCK(brd->bd_lock, lock_flags);
2157                 DPR_OPEN(("%d BS is 0!\n", __LINE__));
2158                 return -ENXIO;
2159         }
2160
2161         DPR_OPEN(("%d: tflag=%x  pflag=%x\n", __LINE__, ch->ch_tun.un_flags, ch->ch_pun.un_flags));
2162
2163         /*
2164          * Initialize tty's
2165          */
2166         if (!(un->un_flags & UN_ISOPEN)) {
2167                 /* Store important variables. */
2168                 un->un_tty     = tty;
2169
2170                 /* Maybe do something here to the TTY struct as well? */
2171         }
2172
2173         /*
2174          * Initialize if neither terminal or printer is open.
2175          */
2176         if (!((ch->ch_tun.un_flags | ch->ch_pun.un_flags) & UN_ISOPEN)) {
2177
2178                 DPR_OPEN(("dgap_open: initializing channel in open...\n"));
2179
2180                 ch->ch_mforce = 0;
2181                 ch->ch_mval = 0;
2182
2183                 /*
2184                  * Flush input queue.
2185                  */
2186                 head = readw(&(bs->rx_head));
2187                 writew(head, &(bs->rx_tail));
2188
2189                 ch->ch_flags = 0;
2190                 ch->pscan_state = 0;
2191                 ch->pscan_savechar = 0;
2192
2193                 ch->ch_c_cflag   = tty->termios.c_cflag;
2194                 ch->ch_c_iflag   = tty->termios.c_iflag;
2195                 ch->ch_c_oflag   = tty->termios.c_oflag;
2196                 ch->ch_c_lflag   = tty->termios.c_lflag;
2197                 ch->ch_startc = tty->termios.c_cc[VSTART];
2198                 ch->ch_stopc  = tty->termios.c_cc[VSTOP];
2199
2200                 /* TODO: flush our TTY struct here? */
2201         }
2202
2203         dgap_carrier(ch);
2204         /*
2205          * Run param in case we changed anything
2206          */
2207         dgap_param(tty);
2208
2209         /*
2210          * follow protocol for opening port
2211          */
2212
2213         DGAP_UNLOCK(ch->ch_lock, lock_flags2);
2214         DGAP_UNLOCK(brd->bd_lock, lock_flags);
2215
2216         rc = dgap_block_til_ready(tty, file, ch);
2217
2218         if (!un->un_tty) {
2219                 return -ENODEV;
2220         }
2221
2222         if (rc) {
2223                 DPR_OPEN(("dgap_tty_open returning after dgap_block_til_ready "
2224                         "with %d\n", rc));
2225         }
2226
2227         /* No going back now, increment our unit and channel counters */
2228         DGAP_LOCK(ch->ch_lock, lock_flags);
2229         ch->ch_open_count++;
2230         un->un_open_count++;
2231         un->un_flags |= (UN_ISOPEN);
2232         DGAP_UNLOCK(ch->ch_lock, lock_flags);
2233
2234         DPR_OPEN(("dgap_tty_open finished\n"));
2235         return (rc);
2236 }
2237
2238
2239 /*
2240  * dgap_block_til_ready()
2241  *
2242  * Wait for DCD, if needed.
2243  */
2244 static int dgap_block_til_ready(struct tty_struct *tty, struct file *file, struct channel_t *ch)
2245 {
2246         int retval = 0;
2247         struct un_t *un = NULL;
2248         ulong   lock_flags;
2249         uint    old_flags = 0;
2250         int sleep_on_un_flags = 0;
2251
2252         if (!tty || tty->magic != TTY_MAGIC || !file || !ch || ch->magic != DGAP_CHANNEL_MAGIC) {
2253                 return (-ENXIO);
2254         }
2255
2256         un = tty->driver_data;
2257         if (!un || un->magic != DGAP_UNIT_MAGIC) {
2258                 return (-ENXIO);
2259         }
2260
2261         DPR_OPEN(("dgap_block_til_ready - before block.\n"));
2262
2263         DGAP_LOCK(ch->ch_lock, lock_flags);
2264
2265         ch->ch_wopen++;
2266
2267         /* Loop forever */
2268         while (1) {
2269
2270                 sleep_on_un_flags = 0;
2271
2272                 /*
2273                  * If board has failed somehow during our sleep, bail with error.
2274                  */
2275                 if (ch->ch_bd->state == BOARD_FAILED) {
2276                         retval = -ENXIO;
2277                         break;
2278                 }
2279
2280                 /* If tty was hung up, break out of loop and set error. */
2281                 if (tty_hung_up_p(file)) {
2282                         retval = -EAGAIN;
2283                         break;
2284                 }
2285
2286                 /*
2287                  * If either unit is in the middle of the fragile part of close,
2288                  * we just cannot touch the channel safely.
2289                  * Go back to sleep, knowing that when the channel can be
2290                  * touched safely, the close routine will signal the
2291                  * ch_wait_flags to wake us back up.
2292                  */
2293                 if (!((ch->ch_tun.un_flags | ch->ch_pun.un_flags) & UN_CLOSING)) {
2294
2295                         /*
2296                          * Our conditions to leave cleanly and happily:
2297                          * 1) NONBLOCKING on the tty is set.
2298                          * 2) CLOCAL is set.
2299                          * 3) DCD (fake or real) is active.
2300                          */
2301
2302                         if (file->f_flags & O_NONBLOCK) {
2303                                 break;
2304                         }
2305
2306                         if (tty->flags & (1 << TTY_IO_ERROR)) {
2307                                 break;
2308                         }
2309
2310                         if (ch->ch_flags & CH_CD) {
2311                                 DPR_OPEN(("%d: ch_flags: %x\n", __LINE__, ch->ch_flags));
2312                                 break;
2313                         }
2314
2315                         if (ch->ch_flags & CH_FCAR) {
2316                                 DPR_OPEN(("%d: ch_flags: %x\n", __LINE__, ch->ch_flags));
2317                                 break;
2318                         }
2319                 }
2320                 else {
2321                         sleep_on_un_flags = 1;
2322                 }
2323
2324                 /*
2325                  * If there is a signal pending, the user probably
2326                  * interrupted (ctrl-c) us.
2327                  * Leave loop with error set.
2328                  */
2329                 if (signal_pending(current)) {
2330                         DPR_OPEN(("%d: signal pending...\n", __LINE__));
2331                         retval = -ERESTARTSYS;
2332                         break;
2333                 }
2334
2335                 DPR_OPEN(("dgap_block_til_ready - blocking.\n"));
2336
2337                 /*
2338                  * Store the flags before we let go of channel lock
2339                  */
2340                 if (sleep_on_un_flags)
2341                         old_flags = ch->ch_tun.un_flags | ch->ch_pun.un_flags;
2342                 else
2343                         old_flags = ch->ch_flags;
2344
2345                 /*
2346                  * Let go of channel lock before calling schedule.
2347                  * Our poller will get any FEP events and wake us up when DCD
2348                  * eventually goes active.
2349                  */
2350
2351                 DGAP_UNLOCK(ch->ch_lock, lock_flags);
2352
2353                 DPR_OPEN(("Going to sleep on %s flags...\n",
2354                         (sleep_on_un_flags ? "un" : "ch")));
2355
2356                 /*
2357                  * Wait for something in the flags to change from the current value.
2358                  */
2359                 if (sleep_on_un_flags) {
2360                         retval = wait_event_interruptible(un->un_flags_wait,
2361                                 (old_flags != (ch->ch_tun.un_flags | ch->ch_pun.un_flags)));
2362                 }
2363                 else {
2364                         retval = wait_event_interruptible(ch->ch_flags_wait,
2365                                 (old_flags != ch->ch_flags));
2366                 }
2367
2368                 DPR_OPEN(("After sleep... retval: %x\n", retval));
2369
2370                 /*
2371                  * We got woken up for some reason.
2372                  * Before looping around, grab our channel lock.
2373                  */
2374                 DGAP_LOCK(ch->ch_lock, lock_flags);
2375         }
2376
2377         ch->ch_wopen--;
2378
2379         DGAP_UNLOCK(ch->ch_lock, lock_flags);
2380
2381         DPR_OPEN(("dgap_block_til_ready - after blocking.\n"));
2382
2383         if (retval) {
2384                 DPR_OPEN(("dgap_block_til_ready - done. error. retval: %x\n", retval));
2385                 return(retval);
2386         }
2387
2388         DPR_OPEN(("dgap_block_til_ready - done no error. jiffies: %lu\n", jiffies));
2389
2390         return(0);
2391 }
2392
2393
2394 /*
2395  * dgap_tty_hangup()
2396  *
2397  * Hangup the port.  Like a close, but don't wait for output to drain.
2398  */
2399 static void dgap_tty_hangup(struct tty_struct *tty)
2400 {
2401         struct board_t  *bd;
2402         struct channel_t *ch;
2403         struct un_t     *un;
2404
2405         if (!tty || tty->magic != TTY_MAGIC)
2406                 return;
2407
2408         un = tty->driver_data;
2409         if (!un || un->magic != DGAP_UNIT_MAGIC)
2410                 return;
2411
2412         ch = un->un_ch;
2413         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
2414                 return;
2415
2416         bd = ch->ch_bd;
2417         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
2418                 return;
2419
2420         DPR_CLOSE(("dgap_hangup called. ch->ch_open_count: %d un->un_open_count: %d\n",
2421                 ch->ch_open_count, un->un_open_count));
2422
2423         /* flush the transmit queues */
2424         dgap_tty_flush_buffer(tty);
2425
2426         DPR_CLOSE(("dgap_hangup finished. ch->ch_open_count: %d un->un_open_count: %d\n",
2427                 ch->ch_open_count, un->un_open_count));
2428 }
2429
2430
2431
2432 /*
2433  * dgap_tty_close()
2434  *
2435  */
2436 static void dgap_tty_close(struct tty_struct *tty, struct file *file)
2437 {
2438         struct ktermios *ts;
2439         struct board_t *bd;
2440         struct channel_t *ch;
2441         struct un_t *un;
2442         ulong lock_flags;
2443         int rc = 0;
2444
2445         if (!tty || tty->magic != TTY_MAGIC)
2446                 return;
2447
2448         un = tty->driver_data;
2449         if (!un || un->magic != DGAP_UNIT_MAGIC)
2450                 return;
2451
2452         ch = un->un_ch;
2453         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
2454                 return;
2455
2456         bd = ch->ch_bd;
2457         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
2458                 return;
2459
2460         ts = &tty->termios;
2461
2462         DPR_CLOSE(("Close called\n"));
2463
2464         DGAP_LOCK(ch->ch_lock, lock_flags);
2465
2466         /*
2467          * Determine if this is the last close or not - and if we agree about
2468          * which type of close it is with the Line Discipline
2469          */
2470         if ((tty->count == 1) && (un->un_open_count != 1)) {
2471                 /*
2472                  * Uh, oh.  tty->count is 1, which means that the tty
2473                  * structure will be freed.  un_open_count should always
2474                  * be one in these conditions.  If it's greater than
2475                  * one, we've got real problems, since it means the
2476                  * serial port won't be shutdown.
2477                  */
2478                 APR(("tty->count is 1, un open count is %d\n", un->un_open_count));
2479                 un->un_open_count = 1;
2480         }
2481
2482         if (--un->un_open_count < 0) {
2483                 APR(("bad serial port open count of %d\n", un->un_open_count));
2484                 un->un_open_count = 0;
2485         }
2486
2487         ch->ch_open_count--;
2488
2489         if (ch->ch_open_count && un->un_open_count) {
2490                 DPR_CLOSE(("dgap_tty_close: not last close ch: %d un:%d\n",
2491                         ch->ch_open_count, un->un_open_count));
2492
2493                 DGAP_UNLOCK(ch->ch_lock, lock_flags);
2494                 return;
2495         }
2496
2497         /* OK, its the last close on the unit */
2498         DPR_CLOSE(("dgap_tty_close - last close on unit procedures\n"));
2499
2500         un->un_flags |= UN_CLOSING;
2501
2502         tty->closing = 1;
2503
2504         /*
2505          * Only officially close channel if count is 0 and
2506          * DIGI_PRINTER bit is not set.
2507          */
2508         if ((ch->ch_open_count == 0) && !(ch->ch_digi.digi_flags & DIGI_PRINTER)) {
2509
2510                 ch->ch_flags &= ~(CH_RXBLOCK);
2511
2512                 DGAP_UNLOCK(ch->ch_lock, lock_flags);
2513
2514                 /* wait for output to drain */
2515                 /* This will also return if we take an interrupt */
2516
2517                 DPR_CLOSE(("Calling wait_for_drain\n"));
2518                 rc = dgap_wait_for_drain(tty);
2519                 DPR_CLOSE(("After calling wait_for_drain\n"));
2520
2521                 if (rc) {
2522                         DPR_BASIC(("dgap_tty_close - bad return: %d ", rc));
2523                 }
2524
2525                 dgap_tty_flush_buffer(tty);
2526                 tty_ldisc_flush(tty);
2527
2528                 DGAP_LOCK(ch->ch_lock, lock_flags);
2529
2530                 tty->closing = 0;
2531
2532                 /*
2533                  * If we have HUPCL set, lower DTR and RTS
2534                  */
2535                 if (ch->ch_c_cflag & HUPCL ) {
2536                         DPR_CLOSE(("Close. HUPCL set, dropping DTR/RTS\n"));
2537                         ch->ch_mostat &= ~(D_RTS(ch)|D_DTR(ch));
2538                         dgap_cmdb( ch, SMODEM, 0, D_DTR(ch)|D_RTS(ch), 0 );
2539
2540                         /*
2541                          * Go to sleep to ensure RTS/DTR
2542                          * have been dropped for modems to see it.
2543                          */
2544                         if (ch->ch_close_delay) {
2545                                 DPR_CLOSE(("Close. Sleeping for RTS/DTR drop\n"));
2546
2547                                 DGAP_UNLOCK(ch->ch_lock, lock_flags);
2548                                 dgap_ms_sleep(ch->ch_close_delay);
2549                                 DGAP_LOCK(ch->ch_lock, lock_flags);
2550
2551                                 DPR_CLOSE(("Close. After sleeping for RTS/DTR drop\n"));
2552                         }
2553                 }
2554
2555                 ch->pscan_state = 0;
2556                 ch->pscan_savechar = 0;
2557                 ch->ch_baud_info = 0;
2558
2559         }
2560
2561         /*
2562          * turn off print device when closing print device.
2563          */
2564         if ((un->un_type == DGAP_PRINT)  && (ch->ch_flags & CH_PRON) ) {
2565                 dgap_wmove(ch, ch->ch_digi.digi_offstr,
2566                         (int) ch->ch_digi.digi_offlen);
2567                 ch->ch_flags &= ~CH_PRON;
2568         }
2569
2570         un->un_tty = NULL;
2571         un->un_flags &= ~(UN_ISOPEN | UN_CLOSING);
2572         tty->driver_data = NULL;
2573
2574         DPR_CLOSE(("Close. Doing wakeups\n"));
2575         wake_up_interruptible(&ch->ch_flags_wait);
2576         wake_up_interruptible(&un->un_flags_wait);
2577
2578         DGAP_UNLOCK(ch->ch_lock, lock_flags);
2579
2580         DPR_BASIC(("dgap_tty_close - complete\n"));
2581 }
2582
2583
2584 /*
2585  * dgap_tty_chars_in_buffer()
2586  *
2587  * Return number of characters that have not been transmitted yet.
2588  *
2589  * This routine is used by the line discipline to determine if there
2590  * is data waiting to be transmitted/drained/flushed or not.
2591  */
2592 static int dgap_tty_chars_in_buffer(struct tty_struct *tty)
2593 {
2594         struct board_t *bd = NULL;
2595         struct channel_t *ch = NULL;
2596         struct un_t *un = NULL;
2597         struct bs_t *bs = NULL;
2598         uchar tbusy;
2599         uint chars = 0;
2600         u16 thead, ttail, tmask, chead, ctail;
2601         ulong   lock_flags = 0;
2602         ulong   lock_flags2 = 0;
2603
2604         if (tty == NULL)
2605                 return(0);
2606
2607         un = tty->driver_data;
2608         if (!un || un->magic != DGAP_UNIT_MAGIC)
2609                 return (0);
2610
2611         ch = un->un_ch;
2612         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
2613                 return (0);
2614
2615         bd = ch->ch_bd;
2616         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
2617                 return (0);
2618
2619         bs = ch->ch_bs;
2620         if (!bs)
2621                 return (0);
2622
2623         DGAP_LOCK(bd->bd_lock, lock_flags);
2624         DGAP_LOCK(ch->ch_lock, lock_flags2);
2625
2626         tmask = (ch->ch_tsize - 1);
2627
2628         /* Get Transmit queue pointers */
2629         thead = readw(&(bs->tx_head)) & tmask;
2630         ttail = readw(&(bs->tx_tail)) & tmask;
2631
2632         /* Get tbusy flag */
2633         tbusy = readb(&(bs->tbusy));
2634
2635         /* Get Command queue pointers */
2636         chead = readw(&(ch->ch_cm->cm_head));
2637         ctail = readw(&(ch->ch_cm->cm_tail));
2638
2639         DGAP_UNLOCK(ch->ch_lock, lock_flags2);
2640         DGAP_UNLOCK(bd->bd_lock, lock_flags);
2641
2642         /*
2643          * The only way we know for sure if there is no pending
2644          * data left to be transferred, is if:
2645          * 1) Transmit head and tail are equal (empty).
2646          * 2) Command queue head and tail are equal (empty).
2647          * 3) The "TBUSY" flag is 0. (Transmitter not busy).
2648          */
2649
2650         if ((ttail == thead) && (tbusy == 0) && (chead == ctail)) {
2651                 chars = 0;
2652         }
2653         else {
2654                 if (thead >= ttail)
2655                         chars = thead - ttail;
2656                 else
2657                         chars = thead - ttail + ch->ch_tsize;
2658                 /*
2659                  * Fudge factor here.
2660                  * If chars is zero, we know that the command queue had
2661                  * something in it or tbusy was set.  Because we cannot
2662                  * be sure if there is still some data to be transmitted,
2663                  * lets lie, and tell ld we have 1 byte left.
2664                  */
2665                 if (chars == 0) {
2666                         /*
2667                          * If TBUSY is still set, and our tx buffers are empty,
2668                          * force the firmware to send me another wakeup after
2669                          * TBUSY has been cleared.
2670                          */
2671                         if (tbusy != 0) {
2672                                 DGAP_LOCK(ch->ch_lock, lock_flags);
2673                                 un->un_flags |= UN_EMPTY;
2674                                 writeb(1, &(bs->iempty));
2675                                 DGAP_UNLOCK(ch->ch_lock, lock_flags);
2676                         }
2677                         chars = 1;
2678                 }
2679         }
2680
2681         DPR_WRITE(("dgap_tty_chars_in_buffer. Port: %x - %d (head: %d tail: %d tsize: %d)\n",
2682                 ch->ch_portnum, chars, thead, ttail, ch->ch_tsize));
2683         return(chars);
2684 }
2685
2686
2687 static int dgap_wait_for_drain(struct tty_struct *tty)
2688 {
2689         struct channel_t *ch;
2690         struct un_t *un;
2691         struct bs_t *bs;
2692         int ret = -EIO;
2693         uint count = 1;
2694         ulong   lock_flags = 0;
2695
2696         if (!tty || tty->magic != TTY_MAGIC)
2697                 return ret;
2698
2699         un = tty->driver_data;
2700         if (!un || un->magic != DGAP_UNIT_MAGIC)
2701                 return ret;
2702
2703         ch = un->un_ch;
2704         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
2705                 return ret;
2706
2707         bs = ch->ch_bs;
2708         if (!bs)
2709                 return ret;
2710
2711         ret = 0;
2712
2713         DPR_DRAIN(("dgap_wait_for_drain start\n"));
2714
2715         /* Loop until data is drained */
2716         while (count != 0) {
2717
2718                 count = dgap_tty_chars_in_buffer(tty);
2719
2720                 if (count == 0)
2721                         break;
2722
2723                 /* Set flag waiting for drain */
2724                 DGAP_LOCK(ch->ch_lock, lock_flags);
2725                 un->un_flags |= UN_EMPTY;
2726                 writeb(1, &(bs->iempty));
2727                 DGAP_UNLOCK(ch->ch_lock, lock_flags);
2728
2729                 /* Go to sleep till we get woken up */
2730                 ret = wait_event_interruptible(un->un_flags_wait, ((un->un_flags & UN_EMPTY) == 0));
2731                 /* If ret is non-zero, user ctrl-c'ed us */
2732                 if (ret) {
2733                         break;
2734                 }
2735         }
2736
2737         DGAP_LOCK(ch->ch_lock, lock_flags);
2738         un->un_flags &= ~(UN_EMPTY);
2739         DGAP_UNLOCK(ch->ch_lock, lock_flags);
2740
2741         DPR_DRAIN(("dgap_wait_for_drain finish\n"));
2742         return (ret);
2743 }
2744
2745
2746 /*
2747  * dgap_maxcps_room
2748  *
2749  * Reduces bytes_available to the max number of characters
2750  * that can be sent currently given the maxcps value, and
2751  * returns the new bytes_available.  This only affects printer
2752  * output.
2753  */
2754 static int dgap_maxcps_room(struct tty_struct *tty, int bytes_available)
2755 {
2756         struct channel_t *ch = NULL;
2757         struct un_t *un = NULL;
2758
2759         if (tty == NULL)
2760                 return (bytes_available);
2761
2762         un = tty->driver_data;
2763         if (!un || un->magic != DGAP_UNIT_MAGIC)
2764                 return (bytes_available);
2765
2766         ch = un->un_ch;
2767         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
2768                 return (bytes_available);
2769
2770         /*
2771          * If its not the Transparent print device, return
2772          * the full data amount.
2773          */
2774         if (un->un_type != DGAP_PRINT)
2775                 return (bytes_available);
2776
2777         if (ch->ch_digi.digi_maxcps > 0 && ch->ch_digi.digi_bufsize > 0 ) {
2778                 int cps_limit = 0;
2779                 unsigned long current_time = jiffies;
2780                 unsigned long buffer_time = current_time +
2781                         (HZ * ch->ch_digi.digi_bufsize) / ch->ch_digi.digi_maxcps;
2782
2783                 if (ch->ch_cpstime < current_time) {
2784                         /* buffer is empty */
2785                         ch->ch_cpstime = current_time;            /* reset ch_cpstime */
2786                         cps_limit = ch->ch_digi.digi_bufsize;
2787                 }
2788                 else if (ch->ch_cpstime < buffer_time) {
2789                         /* still room in the buffer */
2790                         cps_limit = ((buffer_time - ch->ch_cpstime) * ch->ch_digi.digi_maxcps) / HZ;
2791                 }
2792                 else {
2793                         /* no room in the buffer */
2794                         cps_limit = 0;
2795                 }
2796
2797                 bytes_available = min(cps_limit, bytes_available);
2798         }
2799
2800         return (bytes_available);
2801 }
2802
2803
2804 static inline void dgap_set_firmware_event(struct un_t *un, unsigned int event)
2805 {
2806         struct channel_t *ch = NULL;
2807         struct bs_t *bs = NULL;
2808
2809         if (!un || un->magic != DGAP_UNIT_MAGIC)
2810                 return;
2811         ch = un->un_ch;
2812         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
2813                 return;
2814         bs = ch->ch_bs;
2815         if (!bs)
2816                 return;
2817
2818         if ((event & UN_LOW) != 0) {
2819                 if ((un->un_flags & UN_LOW) == 0) {
2820                         un->un_flags |= UN_LOW;
2821                         writeb(1, &(bs->ilow));
2822                 }
2823         }
2824         if ((event & UN_LOW) != 0) {
2825                 if ((un->un_flags & UN_EMPTY) == 0) {
2826                         un->un_flags |= UN_EMPTY;
2827                         writeb(1, &(bs->iempty));
2828                 }
2829         }
2830 }
2831
2832
2833 /*
2834  * dgap_tty_write_room()
2835  *
2836  * Return space available in Tx buffer
2837  */
2838 static int dgap_tty_write_room(struct tty_struct *tty)
2839 {
2840         struct channel_t *ch = NULL;
2841         struct un_t *un = NULL;
2842         struct bs_t *bs = NULL;
2843         u16 head, tail, tmask;
2844         int ret = 0;
2845         ulong   lock_flags = 0;
2846
2847         if (tty == NULL || dgap_TmpWriteBuf == NULL)
2848                 return(0);
2849
2850         un = tty->driver_data;
2851         if (!un || un->magic != DGAP_UNIT_MAGIC)
2852                 return (0);
2853
2854         ch = un->un_ch;
2855         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
2856                 return (0);
2857
2858         bs = ch->ch_bs;
2859         if (!bs)
2860                 return (0);
2861
2862         DGAP_LOCK(ch->ch_lock, lock_flags);
2863
2864         tmask = ch->ch_tsize - 1;
2865         head = readw(&(bs->tx_head)) & tmask;
2866         tail = readw(&(bs->tx_tail)) & tmask;
2867
2868         if ((ret = tail - head - 1) < 0)
2869                 ret += ch->ch_tsize;
2870
2871         /* Limit printer to maxcps */
2872         ret = dgap_maxcps_room(tty, ret);
2873
2874         /*
2875          * If we are printer device, leave space for
2876          * possibly both the on and off strings.
2877          */
2878         if (un->un_type == DGAP_PRINT) {
2879                 if (!(ch->ch_flags & CH_PRON))
2880                         ret -= ch->ch_digi.digi_onlen;
2881                 ret -= ch->ch_digi.digi_offlen;
2882         }
2883         else {
2884                 if (ch->ch_flags & CH_PRON)
2885                         ret -= ch->ch_digi.digi_offlen;
2886         }
2887
2888         if (ret < 0)
2889                 ret = 0;
2890
2891         /*
2892          * Schedule FEP to wake us up if needed.
2893          *
2894          * TODO:  This might be overkill...
2895          * Do we really need to schedule callbacks from the FEP
2896          * in every case?  Can we get smarter based on ret?
2897          */
2898         dgap_set_firmware_event(un, UN_LOW | UN_EMPTY);
2899         DGAP_UNLOCK(ch->ch_lock, lock_flags);
2900
2901         DPR_WRITE(("dgap_tty_write_room - %d tail: %d head: %d\n", ret, tail, head));
2902
2903         return(ret);
2904 }
2905
2906
2907 /*
2908  * dgap_tty_put_char()
2909  *
2910  * Put a character into ch->ch_buf
2911  *
2912  *      - used by the line discipline for OPOST processing
2913  */
2914 static int dgap_tty_put_char(struct tty_struct *tty, unsigned char c)
2915 {
2916         /*
2917          * Simply call tty_write.
2918          */
2919         DPR_WRITE(("dgap_tty_put_char called\n"));
2920         dgap_tty_write(tty, &c, 1);
2921         return 1;
2922 }
2923
2924
2925 /*
2926  * dgap_tty_write()
2927  *
2928  * Take data from the user or kernel and send it out to the FEP.
2929  * In here exists all the Transparent Print magic as well.
2930  */
2931 static int dgap_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
2932 {
2933         struct channel_t *ch = NULL;
2934         struct un_t *un = NULL;
2935         struct bs_t *bs = NULL;
2936         char *vaddr = NULL;
2937         u16 head, tail, tmask, remain;
2938         int bufcount = 0, n = 0;
2939         int orig_count = 0;
2940         ulong lock_flags;
2941         int from_user = 0;
2942
2943         if (tty == NULL || dgap_TmpWriteBuf == NULL)
2944                 return(0);
2945
2946         un = tty->driver_data;
2947         if (!un || un->magic != DGAP_UNIT_MAGIC)
2948                 return (0);
2949
2950         ch = un->un_ch;
2951         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
2952                 return(0);
2953
2954         bs = ch->ch_bs;
2955         if (!bs)
2956                 return(0);
2957
2958         if (!count)
2959                 return(0);
2960
2961         DPR_WRITE(("dgap_tty_write: Port: %x tty=%p user=%d len=%d\n",
2962                 ch->ch_portnum, tty, from_user, count));
2963
2964         /*
2965          * Store original amount of characters passed in.
2966          * This helps to figure out if we should ask the FEP
2967          * to send us an event when it has more space available.
2968          */
2969         orig_count = count;
2970
2971         DGAP_LOCK(ch->ch_lock, lock_flags);
2972
2973         /* Get our space available for the channel from the board */
2974         tmask = ch->ch_tsize - 1;
2975         head = readw(&(bs->tx_head)) & tmask;
2976         tail = readw(&(bs->tx_tail)) & tmask;
2977
2978         if ((bufcount = tail - head - 1) < 0)
2979                 bufcount += ch->ch_tsize;
2980
2981         DPR_WRITE(("%d: bufcount: %x count: %x tail: %x head: %x tmask: %x\n",
2982                 __LINE__, bufcount, count, tail, head, tmask));
2983
2984         /*
2985          * Limit printer output to maxcps overall, with bursts allowed
2986          * up to bufsize characters.
2987          */
2988         bufcount = dgap_maxcps_room(tty, bufcount);
2989
2990         /*
2991          * Take minimum of what the user wants to send, and the
2992          * space available in the FEP buffer.
2993          */
2994         count = min(count, bufcount);
2995
2996         /*
2997          * Bail if no space left.
2998          */
2999         if (count <= 0) {
3000                 dgap_set_firmware_event(un, UN_LOW | UN_EMPTY);
3001                 DGAP_UNLOCK(ch->ch_lock, lock_flags);
3002                 return(0);
3003         }
3004
3005         /*
3006          * Output the printer ON string, if we are in terminal mode, but
3007          * need to be in printer mode.
3008          */
3009         if ((un->un_type == DGAP_PRINT) && !(ch->ch_flags & CH_PRON)) {
3010                 dgap_wmove(ch, ch->ch_digi.digi_onstr,
3011                     (int) ch->ch_digi.digi_onlen);
3012                 head = readw(&(bs->tx_head)) & tmask;
3013                 ch->ch_flags |= CH_PRON;
3014         }
3015
3016         /*
3017          * On the other hand, output the printer OFF string, if we are
3018          * currently in printer mode, but need to output to the terminal.
3019          */
3020         if ((un->un_type != DGAP_PRINT) && (ch->ch_flags & CH_PRON)) {
3021                 dgap_wmove(ch, ch->ch_digi.digi_offstr,
3022                         (int) ch->ch_digi.digi_offlen);
3023                 head = readw(&(bs->tx_head)) & tmask;
3024                 ch->ch_flags &= ~CH_PRON;
3025         }
3026
3027         /*
3028          * If there is nothing left to copy, or I can't handle any more data, leave.
3029          */
3030         if (count <= 0) {
3031                 dgap_set_firmware_event(un, UN_LOW | UN_EMPTY);
3032                 DGAP_UNLOCK(ch->ch_lock, lock_flags);
3033                 return(0);
3034         }
3035
3036         if (from_user) {
3037
3038                 count = min(count, WRITEBUFLEN);
3039
3040                 DGAP_UNLOCK(ch->ch_lock, lock_flags);
3041
3042                 /*
3043                  * If data is coming from user space, copy it into a temporary
3044                  * buffer so we don't get swapped out while doing the copy to
3045                  * the board.
3046                  */
3047                 /* we're allowed to block if it's from_user */
3048                 if (down_interruptible(&dgap_TmpWriteSem)) {
3049                         return (-EINTR);
3050                 }
3051
3052                 if (copy_from_user(dgap_TmpWriteBuf, (const uchar __user *) buf, count)) {
3053                         up(&dgap_TmpWriteSem);
3054                         printk("Write: Copy from user failed!\n");
3055                         return -EFAULT;
3056                 }
3057
3058                 DGAP_LOCK(ch->ch_lock, lock_flags);
3059
3060                 buf = dgap_TmpWriteBuf;
3061         }
3062
3063         n = count;
3064
3065         /*
3066          * If the write wraps over the top of the circular buffer,
3067          * move the portion up to the wrap point, and reset the
3068          * pointers to the bottom.
3069          */
3070         remain = ch->ch_tstart + ch->ch_tsize - head;
3071
3072         if (n >= remain) {
3073                 n -= remain;
3074                 vaddr = ch->ch_taddr + head;
3075
3076                 memcpy_toio(vaddr, (uchar *) buf, remain);
3077                 dgap_sniff_nowait_nolock(ch, "USER WRITE", (uchar *) buf, remain);
3078
3079                 head = ch->ch_tstart;
3080                 buf += remain;
3081         }
3082
3083         if (n > 0) {
3084
3085                 /*
3086                  * Move rest of data.
3087                  */
3088                 vaddr = ch->ch_taddr + head;
3089                 remain = n;
3090
3091                 memcpy_toio(vaddr, (uchar *) buf, remain);
3092                 dgap_sniff_nowait_nolock(ch, "USER WRITE", (uchar *) buf, remain);
3093
3094                 head += remain;
3095
3096         }
3097
3098         if (count) {
3099                 ch->ch_txcount += count;
3100                 head &= tmask;
3101                 writew(head, &(bs->tx_head));
3102         }
3103
3104
3105         dgap_set_firmware_event(un, UN_LOW | UN_EMPTY);
3106
3107         /*
3108          * If this is the print device, and the
3109          * printer is still on, we need to turn it
3110          * off before going idle.  If the buffer is
3111          * non-empty, wait until it goes empty.
3112          * Otherwise turn it off right now.
3113          */
3114         if ((un->un_type == DGAP_PRINT) && (ch->ch_flags & CH_PRON)) {
3115                 tail = readw(&(bs->tx_tail)) & tmask;
3116
3117                 if (tail != head) {
3118                         un->un_flags |= UN_EMPTY;
3119                         writeb(1, &(bs->iempty));
3120                 }
3121                 else {
3122                         dgap_wmove(ch, ch->ch_digi.digi_offstr,
3123                                 (int) ch->ch_digi.digi_offlen);
3124                         head = readw(&(bs->tx_head)) & tmask;
3125                         ch->ch_flags &= ~CH_PRON;
3126                 }
3127         }
3128
3129         /* Update printer buffer empty time. */
3130         if ((un->un_type == DGAP_PRINT) && (ch->ch_digi.digi_maxcps > 0)
3131             && (ch->ch_digi.digi_bufsize > 0)) {
3132                 ch->ch_cpstime += (HZ * count) / ch->ch_digi.digi_maxcps;
3133         }
3134
3135         if (from_user) {
3136                 DGAP_UNLOCK(ch->ch_lock, lock_flags);
3137                 up(&dgap_TmpWriteSem);
3138         }
3139         else {
3140                 DGAP_UNLOCK(ch->ch_lock, lock_flags);
3141         }
3142
3143         DPR_WRITE(("Write finished - Write %d bytes of %d.\n", count, orig_count));
3144
3145         return (count);
3146 }
3147
3148
3149
3150 /*
3151  * Return modem signals to ld.
3152  */
3153 static int dgap_tty_tiocmget(struct tty_struct *tty)
3154 {
3155         struct channel_t *ch;
3156         struct un_t *un;
3157         int result = -EIO;
3158         uchar mstat = 0;
3159         ulong lock_flags;
3160
3161         if (!tty || tty->magic != TTY_MAGIC)
3162                 return result;
3163
3164         un = tty->driver_data;
3165         if (!un || un->magic != DGAP_UNIT_MAGIC)
3166                 return result;
3167
3168         ch = un->un_ch;
3169         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
3170                 return result;
3171
3172         DPR_IOCTL(("dgap_tty_tiocmget start\n"));
3173
3174         DGAP_LOCK(ch->ch_lock, lock_flags);
3175
3176         mstat = readb(&(ch->ch_bs->m_stat));
3177         /* Append any outbound signals that might be pending... */
3178         mstat |= ch->ch_mostat;
3179
3180         DGAP_UNLOCK(ch->ch_lock, lock_flags);
3181
3182         result = 0;
3183
3184         if (mstat & D_DTR(ch))
3185                 result |= TIOCM_DTR;
3186         if (mstat & D_RTS(ch))
3187                 result |= TIOCM_RTS;
3188         if (mstat & D_CTS(ch))
3189                 result |= TIOCM_CTS;
3190         if (mstat & D_DSR(ch))
3191                 result |= TIOCM_DSR;
3192         if (mstat & D_RI(ch))
3193                 result |= TIOCM_RI;
3194         if (mstat & D_CD(ch))
3195                 result |= TIOCM_CD;
3196
3197         DPR_IOCTL(("dgap_tty_tiocmget finish\n"));
3198
3199         return result;
3200 }
3201
3202
3203 /*
3204  * dgap_tty_tiocmset()
3205  *
3206  * Set modem signals, called by ld.
3207  */
3208
3209 static int dgap_tty_tiocmset(struct tty_struct *tty,
3210                 unsigned int set, unsigned int clear)
3211 {
3212         struct board_t *bd;
3213         struct channel_t *ch;
3214         struct un_t *un;
3215         int ret = -EIO;
3216         ulong lock_flags;
3217         ulong lock_flags2;
3218
3219         if (!tty || tty->magic != TTY_MAGIC)
3220                 return ret;
3221
3222         un = tty->driver_data;
3223         if (!un || un->magic != DGAP_UNIT_MAGIC)
3224                 return ret;
3225
3226         ch = un->un_ch;
3227         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
3228                 return ret;
3229
3230         bd = ch->ch_bd;
3231         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
3232                 return ret;
3233
3234         DPR_IOCTL(("dgap_tty_tiocmset start\n"));
3235
3236         DGAP_LOCK(bd->bd_lock, lock_flags);
3237         DGAP_LOCK(ch->ch_lock, lock_flags2);
3238
3239         if (set & TIOCM_RTS) {
3240                 ch->ch_mforce |= D_RTS(ch);
3241                 ch->ch_mval   |= D_RTS(ch);
3242         }
3243
3244         if (set & TIOCM_DTR) {
3245                 ch->ch_mforce |= D_DTR(ch);
3246                 ch->ch_mval   |= D_DTR(ch);
3247         }
3248
3249         if (clear & TIOCM_RTS) {
3250                 ch->ch_mforce |= D_RTS(ch);
3251                 ch->ch_mval   &= ~(D_RTS(ch));
3252         }
3253
3254         if (clear & TIOCM_DTR) {
3255                 ch->ch_mforce |= D_DTR(ch);
3256                 ch->ch_mval   &= ~(D_DTR(ch));
3257         }
3258
3259         dgap_param(tty);
3260
3261         DGAP_UNLOCK(ch->ch_lock, lock_flags2);
3262         DGAP_UNLOCK(bd->bd_lock, lock_flags);
3263
3264         DPR_IOCTL(("dgap_tty_tiocmset finish\n"));
3265
3266         return (0);
3267 }
3268
3269
3270
3271 /*
3272  * dgap_tty_send_break()
3273  *
3274  * Send a Break, called by ld.
3275  */
3276 static int dgap_tty_send_break(struct tty_struct *tty, int msec)
3277 {
3278         struct board_t *bd;
3279         struct channel_t *ch;
3280         struct un_t *un;
3281         int ret = -EIO;
3282         ulong lock_flags;
3283         ulong lock_flags2;
3284
3285         if (!tty || tty->magic != TTY_MAGIC)
3286                 return ret;
3287
3288         un = tty->driver_data;
3289         if (!un || un->magic != DGAP_UNIT_MAGIC)
3290                 return ret;
3291
3292         ch = un->un_ch;
3293         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
3294                 return ret;
3295
3296         bd = ch->ch_bd;
3297         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
3298                 return ret;
3299
3300         switch (msec) {
3301         case -1:
3302                 msec = 0xFFFF;
3303                 break;
3304         case 0:
3305                 msec = 1;
3306                 break;
3307         default:
3308                 msec /= 10;
3309                 break;
3310         }
3311
3312         DPR_IOCTL(("dgap_tty_send_break start 1.  %lx\n", jiffies));
3313
3314         DGAP_LOCK(bd->bd_lock, lock_flags);
3315         DGAP_LOCK(ch->ch_lock, lock_flags2);
3316 #if 0
3317         dgap_cmdw(ch, SBREAK, (u16) SBREAK_TIME, 0);
3318 #endif
3319         dgap_cmdw(ch, SBREAK, (u16) msec, 0);
3320
3321         DGAP_UNLOCK(ch->ch_lock, lock_flags2);
3322         DGAP_UNLOCK(bd->bd_lock, lock_flags);
3323
3324         DPR_IOCTL(("dgap_tty_send_break finish\n"));
3325
3326         return (0);
3327 }
3328
3329
3330
3331
3332 /*
3333  * dgap_tty_wait_until_sent()
3334  *
3335  * wait until data has been transmitted, called by ld.
3336  */
3337 static void dgap_tty_wait_until_sent(struct tty_struct *tty, int timeout)
3338 {
3339         int rc;
3340         rc = dgap_wait_for_drain(tty);
3341         if (rc) {
3342                 DPR_IOCTL(("dgap_tty_ioctl - bad return: %d ", rc));
3343                 return;
3344         }
3345         return;
3346 }
3347
3348
3349
3350 /*
3351  * dgap_send_xchar()
3352  *
3353  * send a high priority character, called by ld.
3354  */
3355 static void dgap_tty_send_xchar(struct tty_struct *tty, char c)
3356 {
3357         struct board_t *bd;
3358         struct channel_t *ch;
3359         struct un_t *un;
3360         ulong lock_flags;
3361         ulong lock_flags2;
3362
3363         if (!tty || tty->magic != TTY_MAGIC)
3364                 return;
3365
3366         un = tty->driver_data;
3367         if (!un || un->magic != DGAP_UNIT_MAGIC)
3368                 return;
3369
3370         ch = un->un_ch;
3371         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
3372                 return;
3373
3374         bd = ch->ch_bd;
3375         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
3376                 return;
3377
3378         DPR_IOCTL(("dgap_tty_send_xchar start 1.  %lx\n", jiffies));
3379
3380         DGAP_LOCK(bd->bd_lock, lock_flags);
3381         DGAP_LOCK(ch->ch_lock, lock_flags2);
3382
3383         /*
3384          * This is technically what we should do.
3385          * However, the NIST tests specifically want
3386          * to see each XON or XOFF character that it
3387          * sends, so lets just send each character
3388          * by hand...
3389          */
3390 #if 0
3391         if (c == STOP_CHAR(tty)) {
3392                 dgap_cmdw(ch, RPAUSE, 0, 0);
3393         }
3394         else if (c == START_CHAR(tty)) {
3395                 dgap_cmdw(ch, RRESUME, 0, 0);
3396         }
3397         else {
3398                 dgap_wmove(ch, &c, 1);
3399         }
3400 #else
3401         dgap_wmove(ch, &c, 1);
3402 #endif
3403
3404         DGAP_UNLOCK(ch->ch_lock, lock_flags2);
3405         DGAP_UNLOCK(bd->bd_lock, lock_flags);
3406
3407         DPR_IOCTL(("dgap_tty_send_xchar finish\n"));
3408
3409         return;
3410 }
3411
3412
3413
3414
3415 /*
3416  * Return modem signals to ld.
3417  */
3418 static int dgap_get_modem_info(struct channel_t *ch, unsigned int __user *value)
3419 {
3420         int result = 0;
3421         uchar mstat = 0;
3422         ulong lock_flags;
3423         int rc = 0;
3424
3425         DPR_IOCTL(("dgap_get_modem_info start\n"));
3426
3427         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
3428                 return(-ENXIO);
3429
3430         DGAP_LOCK(ch->ch_lock, lock_flags);
3431
3432         mstat = readb(&(ch->ch_bs->m_stat));
3433         /* Append any outbound signals that might be pending... */
3434         mstat |= ch->ch_mostat;
3435
3436         DGAP_UNLOCK(ch->ch_lock, lock_flags);
3437
3438         result = 0;
3439
3440         if (mstat & D_DTR(ch))
3441                 result |= TIOCM_DTR;
3442         if (mstat & D_RTS(ch))
3443                 result |= TIOCM_RTS;
3444         if (mstat & D_CTS(ch))
3445                 result |= TIOCM_CTS;
3446         if (mstat & D_DSR(ch))
3447                 result |= TIOCM_DSR;
3448         if (mstat & D_RI(ch))
3449                 result |= TIOCM_RI;
3450         if (mstat & D_CD(ch))
3451                 result |= TIOCM_CD;
3452
3453         rc = put_user(result, value);
3454
3455         DPR_IOCTL(("dgap_get_modem_info finish\n"));
3456         return(rc);
3457 }
3458
3459
3460 /*
3461  * dgap_set_modem_info()
3462  *
3463  * Set modem signals, called by ld.
3464  */
3465 static int dgap_set_modem_info(struct tty_struct *tty, unsigned int command, unsigned int __user *value)
3466 {
3467         struct board_t *bd;
3468         struct channel_t *ch;
3469         struct un_t *un;
3470         int ret = -ENXIO;
3471         unsigned int arg = 0;
3472         ulong lock_flags;
3473         ulong lock_flags2;
3474
3475         if (!tty || tty->magic != TTY_MAGIC)
3476                 return ret;
3477
3478         un = tty->driver_data;
3479         if (!un || un->magic != DGAP_UNIT_MAGIC)
3480                 return ret;
3481
3482         ch = un->un_ch;
3483         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
3484                 return ret;
3485
3486         bd = ch->ch_bd;
3487         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
3488                 return ret;
3489
3490         DPR_IOCTL(("dgap_set_modem_info() start\n"));
3491
3492         ret = get_user(arg, value);
3493         if (ret) {
3494                 DPR_IOCTL(("dgap_set_modem_info %d ret: %x. finished.\n", __LINE__, ret));
3495                 return(ret);
3496         }
3497
3498         DPR_IOCTL(("dgap_set_modem_info: command: %x arg: %x\n", command, arg));
3499
3500         switch (command) {
3501         case TIOCMBIS:
3502                 if (arg & TIOCM_RTS) {
3503                         ch->ch_mforce |= D_RTS(ch);
3504                         ch->ch_mval   |= D_RTS(ch);
3505                 }
3506
3507                 if (arg & TIOCM_DTR) {
3508                         ch->ch_mforce |= D_DTR(ch);
3509                         ch->ch_mval   |= D_DTR(ch);
3510                 }
3511
3512                 break;
3513
3514         case TIOCMBIC:
3515                 if (arg & TIOCM_RTS) {
3516                         ch->ch_mforce |= D_RTS(ch);
3517                         ch->ch_mval   &= ~(D_RTS(ch));
3518                 }
3519
3520                 if (arg & TIOCM_DTR) {
3521                         ch->ch_mforce |= D_DTR(ch);
3522                         ch->ch_mval   &= ~(D_DTR(ch));
3523                 }
3524
3525                 break;
3526
3527         case TIOCMSET:
3528                 ch->ch_mforce = D_DTR(ch)|D_RTS(ch);
3529
3530                 if (arg & TIOCM_RTS) {
3531                         ch->ch_mval |= D_RTS(ch);
3532                 }
3533                 else {
3534                         ch->ch_mval &= ~(D_RTS(ch));
3535                 }
3536
3537                 if (arg & TIOCM_DTR) {
3538                         ch->ch_mval |= (D_DTR(ch));
3539                 }
3540                 else {
3541                         ch->ch_mval &= ~(D_DTR(ch));
3542                 }
3543
3544                 break;
3545
3546         default:
3547                 return(-EINVAL);
3548         }
3549
3550         DGAP_LOCK(bd->bd_lock, lock_flags);
3551         DGAP_LOCK(ch->ch_lock, lock_flags2);
3552
3553         dgap_param(tty);
3554
3555         DGAP_UNLOCK(ch->ch_lock, lock_flags2);
3556         DGAP_UNLOCK(bd->bd_lock, lock_flags);
3557
3558         DPR_IOCTL(("dgap_set_modem_info finish\n"));
3559
3560         return (0);
3561 }
3562
3563
3564 /*
3565  * dgap_tty_digigeta()
3566  *
3567  * Ioctl to get the information for ditty.
3568  *
3569  *
3570  *
3571  */
3572 static int dgap_tty_digigeta(struct tty_struct *tty, struct digi_t __user *retinfo)
3573 {
3574         struct channel_t *ch;
3575         struct un_t *un;
3576         struct digi_t tmp;
3577         ulong lock_flags;
3578
3579         if (!retinfo)
3580                 return (-EFAULT);
3581
3582         if (!tty || tty->magic != TTY_MAGIC)
3583                 return (-EFAULT);
3584
3585         un = tty->driver_data;
3586         if (!un || un->magic != DGAP_UNIT_MAGIC)
3587                 return (-EFAULT);
3588
3589         ch = un->un_ch;
3590         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
3591                 return (-EFAULT);
3592
3593         memset(&tmp, 0, sizeof(tmp));
3594
3595         DGAP_LOCK(ch->ch_lock, lock_flags);
3596         memcpy(&tmp, &ch->ch_digi, sizeof(tmp));
3597         DGAP_UNLOCK(ch->ch_lock, lock_flags);
3598
3599         if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
3600                 return (-EFAULT);
3601
3602         return (0);
3603 }
3604
3605
3606 /*
3607  * dgap_tty_digiseta()
3608  *
3609  * Ioctl to set the information for ditty.
3610  *
3611  *
3612  *
3613  */
3614 static int dgap_tty_digiseta(struct tty_struct *tty, struct digi_t __user *new_info)
3615 {
3616         struct board_t *bd;
3617         struct channel_t *ch;
3618         struct un_t *un;
3619         struct digi_t new_digi;
3620         ulong   lock_flags = 0;
3621         unsigned long lock_flags2;
3622
3623         DPR_IOCTL(("DIGI_SETA start\n"));
3624
3625         if (!tty || tty->magic != TTY_MAGIC)
3626                 return (-EFAULT);
3627
3628         un = tty->driver_data;
3629         if (!un || un->magic != DGAP_UNIT_MAGIC)
3630                 return (-EFAULT);
3631
3632         ch = un->un_ch;
3633         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
3634                 return (-EFAULT);
3635
3636         bd = ch->ch_bd;
3637         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
3638                 return (-EFAULT);
3639
3640         if (copy_from_user(&new_digi, new_info, sizeof(struct digi_t))) {
3641                 DPR_IOCTL(("DIGI_SETA failed copy_from_user\n"));
3642                 return(-EFAULT);
3643         }
3644
3645         DGAP_LOCK(bd->bd_lock, lock_flags);
3646         DGAP_LOCK(ch->ch_lock, lock_flags2);
3647
3648         memcpy(&ch->ch_digi, &new_digi, sizeof(struct digi_t));
3649
3650         if (ch->ch_digi.digi_maxcps < 1)
3651                 ch->ch_digi.digi_maxcps = 1;
3652
3653         if (ch->ch_digi.digi_maxcps > 10000)
3654                 ch->ch_digi.digi_maxcps = 10000;
3655
3656         if (ch->ch_digi.digi_bufsize < 10)
3657                 ch->ch_digi.digi_bufsize = 10;
3658
3659         if (ch->ch_digi.digi_maxchar < 1)
3660                 ch->ch_digi.digi_maxchar = 1;
3661
3662         if (ch->ch_digi.digi_maxchar > ch->ch_digi.digi_bufsize)
3663                 ch->ch_digi.digi_maxchar = ch->ch_digi.digi_bufsize;
3664
3665         if (ch->ch_digi.digi_onlen > DIGI_PLEN)
3666                 ch->ch_digi.digi_onlen = DIGI_PLEN;
3667
3668         if (ch->ch_digi.digi_offlen > DIGI_PLEN)
3669                 ch->ch_digi.digi_offlen = DIGI_PLEN;
3670
3671         dgap_param(tty);
3672
3673         DGAP_UNLOCK(ch->ch_lock, lock_flags2);
3674         DGAP_UNLOCK(bd->bd_lock, lock_flags);
3675
3676         DPR_IOCTL(("DIGI_SETA finish\n"));
3677
3678         return(0);
3679 }
3680
3681
3682 /*
3683  * dgap_tty_digigetedelay()
3684  *
3685  * Ioctl to get the current edelay setting.
3686  *
3687  *
3688  *
3689  */
3690 static int dgap_tty_digigetedelay(struct tty_struct *tty, int __user *retinfo)
3691 {
3692         struct channel_t *ch;
3693         struct un_t *un;
3694         int tmp;
3695         ulong lock_flags;
3696
3697         if (!retinfo)
3698                 return (-EFAULT);
3699
3700         if (!tty || tty->magic != TTY_MAGIC)
3701                 return (-EFAULT);
3702
3703         un = tty->driver_data;
3704         if (!un || un->magic != DGAP_UNIT_MAGIC)
3705                 return (-EFAULT);
3706
3707         ch = un->un_ch;
3708         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
3709                 return (-EFAULT);
3710
3711         memset(&tmp, 0, sizeof(tmp));
3712
3713         DGAP_LOCK(ch->ch_lock, lock_flags);
3714         tmp = readw(&(ch->ch_bs->edelay));
3715         DGAP_UNLOCK(ch->ch_lock, lock_flags);
3716
3717         if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
3718                 return (-EFAULT);
3719
3720         return (0);
3721 }
3722
3723
3724 /*
3725  * dgap_tty_digisetedelay()
3726  *
3727  * Ioctl to set the EDELAY setting
3728  *
3729  */
3730 static int dgap_tty_digisetedelay(struct tty_struct *tty, int __user *new_info)
3731 {
3732         struct board_t *bd;
3733         struct channel_t *ch;
3734         struct un_t *un;
3735         int new_digi;
3736         ulong lock_flags;
3737         ulong lock_flags2;
3738
3739         DPR_IOCTL(("DIGI_SETA start\n"));
3740
3741         if (!tty || tty->magic != TTY_MAGIC)
3742                 return (-EFAULT);
3743
3744         un = tty->driver_data;
3745         if (!un || un->magic != DGAP_UNIT_MAGIC)
3746                 return (-EFAULT);
3747
3748         ch = un->un_ch;
3749         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
3750                 return (-EFAULT);
3751
3752         bd = ch->ch_bd;
3753         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
3754                 return (-EFAULT);
3755
3756         if (copy_from_user(&new_digi, new_info, sizeof(int))) {
3757                 DPR_IOCTL(("DIGI_SETEDELAY failed copy_from_user\n"));
3758                 return(-EFAULT);
3759         }
3760
3761         DGAP_LOCK(bd->bd_lock, lock_flags);
3762         DGAP_LOCK(ch->ch_lock, lock_flags2);
3763
3764         writew((u16) new_digi, &(ch->ch_bs->edelay));
3765
3766         dgap_param(tty);
3767
3768         DGAP_UNLOCK(ch->ch_lock, lock_flags2);
3769         DGAP_UNLOCK(bd->bd_lock, lock_flags);
3770
3771         DPR_IOCTL(("DIGI_SETA finish\n"));
3772
3773         return(0);
3774 }
3775
3776
3777 /*
3778  * dgap_tty_digigetcustombaud()
3779  *
3780  * Ioctl to get the current custom baud rate setting.
3781  */
3782 static int dgap_tty_digigetcustombaud(struct tty_struct *tty, int __user *retinfo)
3783 {
3784         struct channel_t *ch;
3785         struct un_t *un;
3786         int tmp;
3787         ulong lock_flags;
3788
3789         if (!retinfo)
3790                 return (-EFAULT);
3791
3792         if (!tty || tty->magic != TTY_MAGIC)
3793                 return (-EFAULT);
3794
3795         un = tty->driver_data;
3796         if (!un || un->magic != DGAP_UNIT_MAGIC)
3797                 return (-EFAULT);
3798
3799         ch = un->un_ch;
3800         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
3801                 return (-EFAULT);
3802
3803         memset(&tmp, 0, sizeof(tmp));
3804
3805         DGAP_LOCK(ch->ch_lock, lock_flags);
3806         tmp = dgap_get_custom_baud(ch);
3807         DGAP_UNLOCK(ch->ch_lock, lock_flags);
3808
3809         DPR_IOCTL(("DIGI_GETCUSTOMBAUD. Returning %d\n", tmp));
3810
3811         if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
3812                 return (-EFAULT);
3813
3814         return (0);
3815 }
3816
3817
3818 /*
3819  * dgap_tty_digisetcustombaud()
3820  *
3821  * Ioctl to set the custom baud rate setting
3822  */
3823 static int dgap_tty_digisetcustombaud(struct tty_struct *tty, int __user *new_info)
3824 {
3825         struct board_t *bd;
3826         struct channel_t *ch;
3827         struct un_t *un;
3828         uint new_rate;
3829         ulong lock_flags;
3830         ulong lock_flags2;
3831
3832         DPR_IOCTL(("DIGI_SETCUSTOMBAUD start\n"));
3833
3834         if (!tty || tty->magic != TTY_MAGIC)
3835                 return (-EFAULT);
3836
3837         un = tty->driver_data;
3838         if (!un || un->magic != DGAP_UNIT_MAGIC)
3839                 return (-EFAULT);
3840
3841         ch = un->un_ch;
3842         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
3843                 return (-EFAULT);
3844
3845         bd = ch->ch_bd;
3846         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
3847                 return (-EFAULT);
3848
3849
3850         if (copy_from_user(&new_rate, new_info, sizeof(unsigned int))) {
3851                 DPR_IOCTL(("DIGI_SETCUSTOMBAUD failed copy_from_user\n"));
3852                 return(-EFAULT);
3853         }
3854
3855         if (bd->bd_flags & BD_FEP5PLUS) {
3856
3857                 DPR_IOCTL(("DIGI_SETCUSTOMBAUD. Setting %d\n", new_rate));
3858
3859                 DGAP_LOCK(bd->bd_lock, lock_flags);
3860                 DGAP_LOCK(ch->ch_lock, lock_flags2);
3861
3862                 ch->ch_custom_speed = new_rate;
3863
3864                 dgap_param(tty);
3865
3866                 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
3867                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
3868         }
3869
3870         DPR_IOCTL(("DIGI_SETCUSTOMBAUD finish\n"));
3871
3872         return(0);
3873 }
3874
3875
3876 /*
3877  * dgap_set_termios()
3878  */
3879 static void dgap_tty_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
3880 {
3881         struct board_t *bd;
3882         struct channel_t *ch;
3883         struct un_t *un;
3884         unsigned long lock_flags;
3885         unsigned long lock_flags2;
3886
3887         if (!tty || tty->magic != TTY_MAGIC)
3888                 return;
3889
3890         un = tty->driver_data;
3891         if (!un || un->magic != DGAP_UNIT_MAGIC)
3892                 return;
3893
3894         ch = un->un_ch;
3895         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
3896                 return;
3897
3898         bd = ch->ch_bd;
3899         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
3900                 return;
3901
3902         DGAP_LOCK(bd->bd_lock, lock_flags);
3903         DGAP_LOCK(ch->ch_lock, lock_flags2);
3904
3905         ch->ch_c_cflag   = tty->termios.c_cflag;
3906         ch->ch_c_iflag   = tty->termios.c_iflag;
3907         ch->ch_c_oflag   = tty->termios.c_oflag;
3908         ch->ch_c_lflag   = tty->termios.c_lflag;
3909         ch->ch_startc    = tty->termios.c_cc[VSTART];
3910         ch->ch_stopc     = tty->termios.c_cc[VSTOP];
3911
3912         dgap_carrier(ch);
3913         dgap_param(tty);
3914
3915         DGAP_UNLOCK(ch->ch_lock, lock_flags2);
3916         DGAP_UNLOCK(bd->bd_lock, lock_flags);
3917 }
3918
3919
3920 static void dgap_tty_throttle(struct tty_struct *tty)
3921 {
3922         struct board_t *bd;
3923         struct channel_t *ch;
3924         struct un_t *un;
3925         ulong   lock_flags;
3926         ulong   lock_flags2;
3927
3928         if (!tty || tty->magic != TTY_MAGIC)
3929                 return;
3930
3931         un = tty->driver_data;
3932         if (!un || un->magic != DGAP_UNIT_MAGIC)
3933                 return;
3934
3935         ch = un->un_ch;
3936         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
3937                 return;
3938
3939         bd = ch->ch_bd;
3940         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
3941                 return;
3942
3943         DPR_IOCTL(("dgap_tty_throttle start\n"));
3944
3945         DGAP_LOCK(bd->bd_lock, lock_flags);
3946         DGAP_LOCK(ch->ch_lock, lock_flags2);
3947
3948         ch->ch_flags |= (CH_RXBLOCK);
3949 #if 1
3950         dgap_cmdw(ch, RPAUSE, 0, 0);
3951 #endif
3952
3953         DGAP_UNLOCK(ch->ch_lock, lock_flags2);
3954         DGAP_UNLOCK(bd->bd_lock, lock_flags);
3955
3956         DPR_IOCTL(("dgap_tty_throttle finish\n"));
3957 }
3958
3959
3960 static void dgap_tty_unthrottle(struct tty_struct *tty)
3961 {
3962         struct board_t *bd;
3963         struct channel_t *ch;
3964         struct un_t *un;
3965         ulong   lock_flags;
3966         ulong   lock_flags2;
3967
3968         if (!tty || tty->magic != TTY_MAGIC)
3969                 return;
3970
3971         un = tty->driver_data;
3972         if (!un || un->magic != DGAP_UNIT_MAGIC)
3973                 return;
3974
3975         ch = un->un_ch;
3976         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
3977                 return;
3978
3979         bd = ch->ch_bd;
3980         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
3981                 return;
3982
3983         DPR_IOCTL(("dgap_tty_unthrottle start\n"));
3984
3985         DGAP_LOCK(bd->bd_lock, lock_flags);
3986         DGAP_LOCK(ch->ch_lock, lock_flags2);
3987
3988         ch->ch_flags &= ~(CH_RXBLOCK);
3989
3990 #if 1
3991         dgap_cmdw(ch, RRESUME, 0, 0);
3992 #endif
3993
3994         DGAP_UNLOCK(ch->ch_lock, lock_flags2);
3995         DGAP_UNLOCK(bd->bd_lock, lock_flags);
3996
3997         DPR_IOCTL(("dgap_tty_unthrottle finish\n"));
3998 }
3999
4000
4001 static void dgap_tty_start(struct tty_struct *tty)
4002 {
4003         struct board_t *bd;
4004         struct channel_t *ch;
4005         struct un_t *un;
4006         ulong   lock_flags;
4007         ulong   lock_flags2;
4008
4009         if (!tty || tty->magic != TTY_MAGIC)
4010                 return;
4011
4012         un = tty->driver_data;
4013         if (!un || un->magic != DGAP_UNIT_MAGIC)
4014                 return;
4015
4016         ch = un->un_ch;
4017         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
4018                 return;
4019
4020         bd = ch->ch_bd;
4021         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
4022                 return;
4023
4024         DPR_IOCTL(("dgap_tty_start start\n"));
4025
4026         DGAP_LOCK(bd->bd_lock, lock_flags);
4027         DGAP_LOCK(ch->ch_lock, lock_flags2);
4028
4029         dgap_cmdw(ch, RESUMETX, 0, 0);
4030
4031         DGAP_UNLOCK(ch->ch_lock, lock_flags2);
4032         DGAP_UNLOCK(bd->bd_lock, lock_flags);
4033
4034         DPR_IOCTL(("dgap_tty_start finish\n"));
4035 }
4036
4037
4038 static void dgap_tty_stop(struct tty_struct *tty)
4039 {
4040         struct board_t *bd;
4041         struct channel_t *ch;
4042         struct un_t *un;
4043         ulong   lock_flags;
4044         ulong   lock_flags2;
4045
4046         if (!tty || tty->magic != TTY_MAGIC)
4047                 return;
4048
4049         un = tty->driver_data;
4050         if (!un || un->magic != DGAP_UNIT_MAGIC)
4051                 return;
4052
4053         ch = un->un_ch;
4054         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
4055                 return;
4056
4057         bd = ch->ch_bd;
4058         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
4059                 return;
4060
4061         DPR_IOCTL(("dgap_tty_stop start\n"));
4062
4063         DGAP_LOCK(bd->bd_lock, lock_flags);
4064         DGAP_LOCK(ch->ch_lock, lock_flags2);
4065
4066         dgap_cmdw(ch, PAUSETX, 0, 0);
4067
4068         DGAP_UNLOCK(ch->ch_lock, lock_flags2);
4069         DGAP_UNLOCK(bd->bd_lock, lock_flags);
4070
4071         DPR_IOCTL(("dgap_tty_stop finish\n"));
4072 }
4073
4074
4075 /*
4076  * dgap_tty_flush_chars()
4077  *
4078  * Flush the cook buffer
4079  *
4080  * Note to self, and any other poor souls who venture here:
4081  *
4082  * flush in this case DOES NOT mean dispose of the data.
4083  * instead, it means "stop buffering and send it if you
4084  * haven't already."  Just guess how I figured that out...   SRW 2-Jun-98
4085  *
4086  * It is also always called in interrupt context - JAR 8-Sept-99
4087  */
4088 static void dgap_tty_flush_chars(struct tty_struct *tty)
4089 {
4090         struct board_t *bd;
4091         struct channel_t *ch;
4092         struct un_t *un;
4093         ulong   lock_flags;
4094         ulong   lock_flags2;
4095
4096         if (!tty || tty->magic != TTY_MAGIC)
4097                 return;
4098
4099         un = tty->driver_data;
4100         if (!un || un->magic != DGAP_UNIT_MAGIC)
4101                 return;
4102
4103         ch = un->un_ch;
4104         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
4105                 return;
4106
4107         bd = ch->ch_bd;
4108         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
4109                 return;
4110
4111         DPR_IOCTL(("dgap_tty_flush_chars start\n"));
4112
4113         DGAP_LOCK(bd->bd_lock, lock_flags);
4114         DGAP_LOCK(ch->ch_lock, lock_flags2);
4115
4116         /* TODO: Do something here */
4117
4118         DGAP_UNLOCK(ch->ch_lock, lock_flags2);
4119         DGAP_UNLOCK(bd->bd_lock, lock_flags);
4120
4121         DPR_IOCTL(("dgap_tty_flush_chars finish\n"));
4122 }
4123
4124
4125
4126 /*
4127  * dgap_tty_flush_buffer()
4128  *
4129  * Flush Tx buffer (make in == out)
4130  */
4131 static void dgap_tty_flush_buffer(struct tty_struct *tty)
4132 {
4133         struct board_t *bd;
4134         struct channel_t *ch;
4135         struct un_t *un;
4136         ulong   lock_flags;
4137         ulong   lock_flags2;
4138         u16     head = 0;
4139
4140         if (!tty || tty->magic != TTY_MAGIC)
4141                 return;
4142
4143         un = tty->driver_data;
4144         if (!un || un->magic != DGAP_UNIT_MAGIC)
4145                 return;
4146
4147         ch = un->un_ch;
4148         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
4149                 return;
4150
4151         bd = ch->ch_bd;
4152         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
4153                 return;
4154
4155         DPR_IOCTL(("dgap_tty_flush_buffer on port: %d start\n", ch->ch_portnum));
4156
4157         DGAP_LOCK(bd->bd_lock, lock_flags);
4158         DGAP_LOCK(ch->ch_lock, lock_flags2);
4159
4160         ch->ch_flags &= ~CH_STOP;
4161         head = readw(&(ch->ch_bs->tx_head));
4162         dgap_cmdw(ch, FLUSHTX, (u16) head, 0);
4163         dgap_cmdw(ch, RESUMETX, 0, 0);
4164         if (ch->ch_tun.un_flags & (UN_LOW|UN_EMPTY)) {
4165                 ch->ch_tun.un_flags &= ~(UN_LOW|UN_EMPTY);
4166                 wake_up_interruptible(&ch->ch_tun.un_flags_wait);
4167         }
4168         if (ch->ch_pun.un_flags & (UN_LOW|UN_EMPTY)) {
4169                 ch->ch_pun.un_flags &= ~(UN_LOW|UN_EMPTY);
4170                 wake_up_interruptible(&ch->ch_pun.un_flags_wait);
4171         }
4172
4173         DGAP_UNLOCK(ch->ch_lock, lock_flags2);
4174         DGAP_UNLOCK(bd->bd_lock, lock_flags);
4175         if (waitqueue_active(&tty->write_wait))
4176                 wake_up_interruptible(&tty->write_wait);
4177         tty_wakeup(tty);
4178
4179         DPR_IOCTL(("dgap_tty_flush_buffer finish\n"));
4180 }
4181
4182
4183
4184 /*****************************************************************************
4185  *
4186  * The IOCTL function and all of its helpers
4187  *
4188  *****************************************************************************/
4189
4190 /*
4191  * dgap_tty_ioctl()
4192  *
4193  * The usual assortment of ioctl's
4194  */
4195 static int dgap_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
4196                 unsigned long arg)
4197 {
4198         struct board_t *bd;
4199         struct channel_t *ch;
4200         struct un_t *un;
4201         int rc;
4202         u16     head = 0;
4203         ulong   lock_flags = 0;
4204         ulong   lock_flags2 = 0;
4205         void __user *uarg = (void __user *) arg;
4206
4207         if (!tty || tty->magic != TTY_MAGIC)
4208                 return (-ENODEV);
4209
4210         un = tty->driver_data;
4211         if (!un || un->magic != DGAP_UNIT_MAGIC)
4212                 return (-ENODEV);
4213
4214         ch = un->un_ch;
4215         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
4216                 return (-ENODEV);
4217
4218         bd = ch->ch_bd;
4219         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
4220                 return (-ENODEV);
4221
4222         DPR_IOCTL(("dgap_tty_ioctl start on port %d - cmd %s (%x), arg %lx\n",
4223                 ch->ch_portnum, dgap_ioctl_name(cmd), cmd, arg));
4224
4225         DGAP_LOCK(bd->bd_lock, lock_flags);
4226         DGAP_LOCK(ch->ch_lock, lock_flags2);
4227
4228         if (un->un_open_count <= 0) {
4229                 DPR_BASIC(("dgap_tty_ioctl - unit not open.\n"));
4230                 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
4231                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
4232                 return(-EIO);
4233         }
4234
4235         switch (cmd) {
4236
4237         /* Here are all the standard ioctl's that we MUST implement */
4238
4239         case TCSBRK:
4240                 /*
4241                  * TCSBRK is SVID version: non-zero arg --> no break
4242                  * this behaviour is exploited by tcdrain().
4243                  *
4244                  * According to POSIX.1 spec (7.2.2.1.2) breaks should be
4245                  * between 0.25 and 0.5 seconds so we'll ask for something
4246                  * in the middle: 0.375 seconds.
4247                  */
4248                 rc = tty_check_change(tty);
4249                 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
4250                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
4251                 if (rc) {
4252                         return(rc);
4253                 }
4254
4255                 rc = dgap_wait_for_drain(tty);
4256
4257                 if (rc) {
4258                         DPR_IOCTL(("dgap_tty_ioctl - bad return: %d ", rc));
4259                         return(-EINTR);
4260                 }
4261
4262                 DGAP_LOCK(bd->bd_lock, lock_flags);
4263                 DGAP_LOCK(ch->ch_lock, lock_flags2);
4264
4265                 if(((cmd == TCSBRK) && (!arg)) || (cmd == TCSBRKP)) {
4266                         dgap_cmdw(ch, SBREAK, (u16) SBREAK_TIME, 0);
4267                 }
4268
4269                 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
4270                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
4271
4272                 DPR_IOCTL(("dgap_tty_ioctl finish on port %d - cmd %s (%x), arg %lx\n",
4273                         ch->ch_portnum, dgap_ioctl_name(cmd), cmd, arg));
4274
4275                 return(0);
4276
4277
4278         case TCSBRKP:
4279                 /* support for POSIX tcsendbreak()
4280
4281                  * According to POSIX.1 spec (7.2.2.1.2) breaks should be
4282                  * between 0.25 and 0.5 seconds so we'll ask for something
4283                  * in the middle: 0.375 seconds.
4284                  */
4285                 rc = tty_check_change(tty);
4286                 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
4287                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
4288                 if (rc) {
4289                         return(rc);
4290                 }
4291
4292                 rc = dgap_wait_for_drain(tty);
4293                 if (rc) {
4294                         DPR_IOCTL(("dgap_tty_ioctl - bad return: %d ", rc));
4295                         return(-EINTR);
4296                 }
4297
4298                 DGAP_LOCK(bd->bd_lock, lock_flags);
4299                 DGAP_LOCK(ch->ch_lock, lock_flags2);
4300
4301                 dgap_cmdw(ch, SBREAK, (u16) SBREAK_TIME, 0);
4302
4303                 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
4304                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
4305
4306                 DPR_IOCTL(("dgap_tty_ioctl finish on port %d - cmd %s (%x), arg %lx\n",
4307                         ch->ch_portnum, dgap_ioctl_name(cmd), cmd, arg));
4308
4309                 return(0);
4310
4311         case TIOCSBRK:
4312                 /*
4313                  * FEP5 doesn't support turning on a break unconditionally.
4314                  * The FEP5 device will stop sending a break automatically
4315                  * after the specified time value that was sent when turning on
4316                  * the break.
4317                  */
4318                 rc = tty_check_change(tty);
4319                 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
4320                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
4321                 if (rc) {
4322                         return(rc);
4323                 }
4324
4325                 rc = dgap_wait_for_drain(tty);
4326                 if (rc) {
4327                         DPR_IOCTL(("dgap_tty_ioctl - bad return: %d ", rc));
4328                         return(-EINTR);
4329                 }
4330
4331                 DGAP_LOCK(bd->bd_lock, lock_flags);
4332                 DGAP_LOCK(ch->ch_lock, lock_flags2);
4333
4334                 dgap_cmdw(ch, SBREAK, (u16) SBREAK_TIME, 0);
4335
4336                 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
4337                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
4338
4339                 DPR_IOCTL(("dgap_tty_ioctl finish on port %d - cmd %s (%x), arg %lx\n",
4340                         ch->ch_portnum, dgap_ioctl_name(cmd), cmd, arg));
4341
4342                 return 0;
4343
4344         case TIOCCBRK:
4345                 /*
4346                  * FEP5 doesn't support turning off a break unconditionally.
4347                  * The FEP5 device will stop sending a break automatically
4348                  * after the specified time value that was sent when turning on
4349                  * the break.
4350                  */
4351                 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
4352                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
4353                 return 0;
4354
4355         case TIOCGSOFTCAR:
4356
4357                 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
4358                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
4359
4360                 rc = put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *) arg);
4361                 return(rc);
4362
4363         case TIOCSSOFTCAR:
4364                 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
4365                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
4366
4367                 rc = get_user(arg, (unsigned long __user *) arg);
4368                 if (rc)
4369                         return(rc);
4370
4371                 DGAP_LOCK(bd->bd_lock, lock_flags);
4372                 DGAP_LOCK(ch->ch_lock, lock_flags2);
4373                 tty->termios.c_cflag = ((tty->termios.c_cflag & ~CLOCAL) | (arg ? CLOCAL : 0));
4374                 dgap_param(tty);
4375                 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
4376                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
4377
4378                 return(0);
4379
4380         case TIOCMGET:
4381                 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
4382                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
4383                 return(dgap_get_modem_info(ch, uarg));
4384
4385         case TIOCMBIS:
4386         case TIOCMBIC:
4387         case TIOCMSET:
4388                 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
4389                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
4390                 return(dgap_set_modem_info(tty, cmd, uarg));
4391
4392                 /*
4393                  * Here are any additional ioctl's that we want to implement
4394                  */
4395
4396         case TCFLSH:
4397                 /*
4398                  * The linux tty driver doesn't have a flush
4399                  * input routine for the driver, assuming all backed
4400                  * up data is in the line disc. buffers.  However,
4401                  * we all know that's not the case.  Here, we
4402                  * act on the ioctl, but then lie and say we didn't
4403                  * so the line discipline will process the flush
4404                  * also.
4405                  */
4406                 rc = tty_check_change(tty);
4407                 if (rc) {
4408                         DGAP_UNLOCK(ch->ch_lock, lock_flags2);
4409                         DGAP_UNLOCK(bd->bd_lock, lock_flags);
4410                         return(rc);
4411                 }
4412
4413                 if ((arg == TCIFLUSH) || (arg == TCIOFLUSH)) {
4414                         if (!(un->un_type == DGAP_PRINT)) {
4415                                 head = readw(&(ch->ch_bs->rx_head));
4416                                 writew(head, &(ch->ch_bs->rx_tail));
4417                                 writeb(0, &(ch->ch_bs->orun));
4418                         }
4419                 }
4420
4421                 if ((arg == TCOFLUSH) || (arg == TCIOFLUSH)) {
4422                         ch->ch_flags &= ~CH_STOP;
4423                         head = readw(&(ch->ch_bs->tx_head));
4424                         dgap_cmdw(ch, FLUSHTX, (u16) head, 0 );
4425                         dgap_cmdw(ch, RESUMETX, 0, 0);
4426                         if (ch->ch_tun.un_flags & (UN_LOW|UN_EMPTY)) {
4427                                 ch->ch_tun.un_flags &= ~(UN_LOW|UN_EMPTY);
4428                                 wake_up_interruptible(&ch->ch_tun.un_flags_wait);
4429                         }
4430                         if (ch->ch_pun.un_flags & (UN_LOW|UN_EMPTY)) {
4431                                 ch->ch_pun.un_flags &= ~(UN_LOW|UN_EMPTY);
4432                                 wake_up_interruptible(&ch->ch_pun.un_flags_wait);
4433                         }
4434                         if (waitqueue_active(&tty->write_wait))
4435                                 wake_up_interruptible(&tty->write_wait);
4436
4437                         /* Can't hold any locks when calling tty_wakeup! */
4438                         DGAP_UNLOCK(ch->ch_lock, lock_flags2);
4439                         DGAP_UNLOCK(bd->bd_lock, lock_flags);
4440                         tty_wakeup(tty);
4441                         DGAP_LOCK(bd->bd_lock, lock_flags);
4442                         DGAP_LOCK(ch->ch_lock, lock_flags2);
4443                 }
4444
4445                 /* pretend we didn't recognize this IOCTL */
4446                 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
4447                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
4448
4449                 DPR_IOCTL(("dgap_tty_ioctl (LINE:%d) finish on port %d - cmd %s (%x), arg %lx\n",
4450                         __LINE__, ch->ch_portnum, dgap_ioctl_name(cmd), cmd, arg));
4451
4452                 return(-ENOIOCTLCMD);
4453
4454         case TCSETSF:
4455         case TCSETSW:
4456                 /*
4457                  * The linux tty driver doesn't have a flush
4458                  * input routine for the driver, assuming all backed
4459                  * up data is in the line disc. buffers.  However,
4460                  * we all know that's not the case.  Here, we
4461                  * act on the ioctl, but then lie and say we didn't
4462                  * so the line discipline will process the flush
4463                  * also.
4464                  */
4465                 if (cmd == TCSETSF) {
4466                         /* flush rx */
4467                         ch->ch_flags &= ~CH_STOP;
4468                         head = readw(&(ch->ch_bs->rx_head));
4469                         writew(head, &(ch->ch_bs->rx_tail));
4470                 }
4471
4472                 /* now wait for all the output to drain */
4473                 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
4474                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
4475                 rc = dgap_wait_for_drain(tty);
4476                 if (rc) {
4477                         DPR_IOCTL(("dgap_tty_ioctl - bad return: %d ", rc));
4478                         return(-EINTR);
4479                 }
4480
4481                 DPR_IOCTL(("dgap_tty_ioctl finish on port %d - cmd %s (%x), arg %lx\n",
4482                         ch->ch_portnum, dgap_ioctl_name(cmd), cmd, arg));
4483
4484                 /* pretend we didn't recognize this */
4485                 return(-ENOIOCTLCMD);
4486
4487         case TCSETAW:
4488
4489                 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
4490                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
4491                 rc = dgap_wait_for_drain(tty);
4492                 if (rc) {
4493                         DPR_IOCTL(("dgap_tty_ioctl - bad return: %d ", rc));
4494                         return(-EINTR);
4495                 }
4496
4497                 /* pretend we didn't recognize this */
4498                 return(-ENOIOCTLCMD);
4499
4500         case TCXONC:
4501                 /*
4502                  * The Linux Line Discipline (LD) would do this for us if we
4503                  * let it, but we have the special firmware options to do this
4504                  * the "right way" regardless of hardware or software flow
4505                  * control so we'll do it outselves instead of letting the LD
4506                  * do it.
4507                  */
4508                 rc = tty_check_change(tty);
4509                 if (rc) {
4510                         DGAP_UNLOCK(ch->ch_lock, lock_flags2);
4511                         DGAP_UNLOCK(bd->bd_lock, lock_flags);
4512                         return(rc);
4513                 }
4514
4515                 DPR_IOCTL(("dgap_ioctl - in TCXONC - %d\n", cmd));
4516                 switch (arg) {
4517
4518                 case TCOON:
4519                         DGAP_UNLOCK(ch->ch_lock, lock_flags2);
4520                         DGAP_UNLOCK(bd->bd_lock, lock_flags);
4521                         dgap_tty_start(tty);
4522                         return(0);
4523                 case TCOOFF:
4524                         DGAP_UNLOCK(ch->ch_lock, lock_flags2);
4525                         DGAP_UNLOCK(bd->bd_lock, lock_flags);
4526                         dgap_tty_stop(tty);
4527                         return(0);
4528                 case TCION:
4529                         DGAP_UNLOCK(ch->ch_lock, lock_flags2);
4530                         DGAP_UNLOCK(bd->bd_lock, lock_flags);
4531                         /* Make the ld do it */
4532                         return(-ENOIOCTLCMD);
4533                 case TCIOFF:
4534                         DGAP_UNLOCK(ch->ch_lock, lock_flags2);
4535                         DGAP_UNLOCK(bd->bd_lock, lock_flags);
4536                         /* Make the ld do it */
4537                         return(-ENOIOCTLCMD);
4538                 default:
4539                         DGAP_UNLOCK(ch->ch_lock, lock_flags2);
4540                         DGAP_UNLOCK(bd->bd_lock, lock_flags);
4541                         return(-EINVAL);
4542                 }
4543
4544         case DIGI_GETA:
4545                 /* get information for ditty */
4546                 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
4547                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
4548                 return(dgap_tty_digigeta(tty, uarg));
4549
4550         case DIGI_SETAW:
4551         case DIGI_SETAF:
4552
4553                 /* set information for ditty */
4554                 if (cmd == (DIGI_SETAW)) {
4555
4556                         DGAP_UNLOCK(ch->ch_lock, lock_flags2);
4557                         DGAP_UNLOCK(bd->bd_lock, lock_flags);
4558                         rc = dgap_wait_for_drain(tty);
4559                         if (rc) {
4560                                 DPR_IOCTL(("dgap_tty_ioctl - bad return: %d ", rc));
4561                                 return(-EINTR);
4562                         }
4563                         DGAP_LOCK(bd->bd_lock, lock_flags);
4564                         DGAP_LOCK(ch->ch_lock, lock_flags2);
4565                 }
4566                 else {
4567                         tty_ldisc_flush(tty);
4568                 }
4569                 /* fall thru */
4570
4571         case DIGI_SETA:
4572                 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
4573                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
4574                 return(dgap_tty_digiseta(tty, uarg));
4575
4576         case DIGI_GEDELAY:
4577                 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
4578                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
4579                 return(dgap_tty_digigetedelay(tty, uarg));
4580
4581         case DIGI_SEDELAY:
4582                 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
4583                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
4584                 return(dgap_tty_digisetedelay(tty, uarg));
4585
4586         case DIGI_GETCUSTOMBAUD:
4587                 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
4588                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
4589                 return(dgap_tty_digigetcustombaud(tty, uarg));
4590
4591         case DIGI_SETCUSTOMBAUD:
4592                 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
4593                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
4594                 return(dgap_tty_digisetcustombaud(tty, uarg));
4595
4596         case DIGI_RESET_PORT:
4597                 dgap_firmware_reset_port(ch);
4598                 dgap_param(tty);
4599                 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
4600                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
4601                 return 0;
4602
4603         default:
4604                 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
4605                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
4606
4607                 DPR_IOCTL(("dgap_tty_ioctl - in default\n"));
4608                 DPR_IOCTL(("dgap_tty_ioctl end - cmd %s (%x), arg %lx\n",
4609                         dgap_ioctl_name(cmd), cmd, arg));
4610
4611                 return(-ENOIOCTLCMD);
4612         }
4613 }
4614 /*
4615  * Loads the dgap.conf config file from the user.
4616  */
4617 void dgap_do_config_load(uchar __user *uaddr, int len)
4618 {
4619         int orig_len = len;
4620         char *to_addr;
4621         uchar __user *from_addr = uaddr;
4622         char buf[U2BSIZE];
4623         int n;
4624
4625         to_addr = dgap_config_buf = kzalloc(len + 1, GFP_ATOMIC);
4626         if (!dgap_config_buf) {
4627                 DPR_INIT(("dgap_do_config_load - unable to allocate memory for file\n"));
4628                 dgap_driver_state = DRIVER_NEED_CONFIG_LOAD;
4629                 return;
4630         }
4631
4632         n = U2BSIZE;
4633         while (len) {
4634
4635                 if (n > len)
4636                         n = len;
4637
4638                 if (copy_from_user((char *) &buf, from_addr, n) == -1 )
4639                         return;
4640
4641                 /* Copy data from buffer to kernel memory */
4642                 memcpy(to_addr, buf, n);
4643
4644                 /* increment counts */
4645                 len -= n;
4646                 to_addr += n;
4647                 from_addr += n;
4648                 n = U2BSIZE;
4649         }
4650
4651         dgap_config_buf[orig_len] = '\0';
4652
4653         to_addr = dgap_config_buf;
4654         dgap_parsefile(&to_addr, TRUE);
4655
4656         DPR_INIT(("dgap_config_load() finish\n"));
4657
4658         return;
4659 }
4660
4661
4662 int dgap_after_config_loaded(void)
4663 {
4664         int i = 0;
4665         int rc = 0;
4666
4667         /*
4668          * Register our ttys, now that we have the config loaded.
4669          */
4670         for (i = 0; i < dgap_NumBoards; ++i) {
4671
4672                 /*
4673                  * Initialize KME waitqueues...
4674                  */
4675                 init_waitqueue_head(&(dgap_Board[i]->kme_wait));
4676
4677                 /*
4678                  * allocate flip buffer for board.
4679                  */
4680                 dgap_Board[i]->flipbuf = kzalloc(MYFLIPLEN, GFP_ATOMIC);
4681                 dgap_Board[i]->flipflagbuf = kzalloc(MYFLIPLEN, GFP_ATOMIC);
4682         }
4683
4684         return rc;
4685 }
4686
4687
4688
4689 /*=======================================================================
4690  *
4691  *      usertoboard - copy from user space to board space.
4692  *
4693  *=======================================================================*/
4694 static int dgap_usertoboard(struct board_t *brd, char *to_addr, char __user *from_addr, int len)
4695 {
4696         char buf[U2BSIZE];
4697         int n = U2BSIZE;
4698
4699         if (!brd || brd->magic != DGAP_BOARD_MAGIC)
4700                 return -EFAULT;
4701
4702         while (len) {
4703                 if (n > len)
4704                         n = len;
4705
4706                 if (copy_from_user((char *) &buf, from_addr, n) == -1 ) {
4707                         return -EFAULT;
4708                 }
4709
4710                 /* Copy data from buffer to card memory */
4711                 memcpy_toio(to_addr, buf, n);
4712
4713                 /* increment counts */
4714                 len -= n;
4715                 to_addr += n;
4716                 from_addr += n;
4717                 n = U2BSIZE;
4718         }
4719         return 0;
4720 }
4721
4722
4723 /*
4724  * Copies the BIOS code from the user to the board,
4725  * and starts the BIOS running.
4726  */
4727 void dgap_do_bios_load(struct board_t *brd, uchar __user *ubios, int len)
4728 {
4729         uchar *addr;
4730         uint offset;
4731         int i;
4732
4733         if (!brd || (brd->magic != DGAP_BOARD_MAGIC) || !brd->re_map_membase)
4734                 return;
4735
4736         DPR_INIT(("dgap_do_bios_load() start\n"));
4737
4738         addr = brd->re_map_membase;
4739
4740         /*
4741          * clear POST area
4742          */
4743         for (i = 0; i < 16; i++)
4744                 writeb(0, addr + POSTAREA + i);
4745
4746         /*
4747          * Download bios
4748          */
4749         offset = 0x1000;
4750         if (dgap_usertoboard(brd, addr + offset, ubios, len) == -1 ) {
4751                 brd->state = BOARD_FAILED;
4752                 brd->dpastatus = BD_NOFEP;
4753                 return;
4754         }
4755
4756         writel(0x0bf00401, addr);
4757         writel(0, (addr + 4));
4758
4759         /* Clear the reset, and change states. */
4760         writeb(FEPCLR, brd->re_map_port);
4761         brd->state = WAIT_BIOS_LOAD;
4762 }
4763
4764
4765 /*
4766  * Checks to see if the BIOS completed running on the card.
4767  */
4768 static void dgap_do_wait_for_bios(struct board_t *brd)
4769 {
4770         uchar *addr;
4771         u16 word;
4772
4773         if (!brd || (brd->magic != DGAP_BOARD_MAGIC) || !brd->re_map_membase)
4774                 return;
4775
4776         addr = brd->re_map_membase;
4777         word = readw(addr + POSTAREA);
4778
4779         /* Check to see if BIOS thinks board is good. (GD). */
4780         if (word == *(u16 *) "GD") {
4781                 DPR_INIT(("GOT GD in memory, moving states.\n"));
4782                 brd->state = FINISHED_BIOS_LOAD;
4783                 return;
4784         }
4785
4786         /* Give up on board after too long of time taken */
4787         if (brd->wait_for_bios++ > 5000) {
4788                 u16 err1 = readw(addr + SEQUENCE);
4789                 u16 err2 = readw(addr + ERROR);
4790                 APR(("***WARNING*** %s failed diagnostics.  Error #(%x,%x).\n",
4791                         brd->name, err1, err2));
4792                 brd->state = BOARD_FAILED;
4793                 brd->dpastatus = BD_NOFEP;
4794         }
4795 }
4796
4797
4798 /*
4799  * Copies the FEP code from the user to the board,
4800  * and starts the FEP running.
4801  */
4802 void dgap_do_fep_load(struct board_t *brd, uchar __user *ufep, int len)
4803 {
4804         uchar *addr;
4805         uint offset;
4806
4807         if (!brd || (brd->magic != DGAP_BOARD_MAGIC) || !brd->re_map_membase)
4808                 return;
4809
4810         addr = brd->re_map_membase;
4811
4812         DPR_INIT(("dgap_do_fep_load() for board %s : start\n", brd->name));
4813
4814         /*
4815          * Download FEP
4816          */
4817         offset = 0x1000;
4818         if (dgap_usertoboard(brd, addr + offset, ufep, len) == -1 ) {
4819                 brd->state = BOARD_FAILED;
4820                 brd->dpastatus = BD_NOFEP;
4821                 return;
4822         }
4823
4824         /*
4825          * If board is a concentrator product, we need to give
4826          * it its config string describing how the concentrators look.
4827          */
4828         if ((brd->type == PCX) || (brd->type == PEPC)) {
4829                 uchar string[100];
4830                 uchar *config, *xconfig;
4831                 int i = 0;
4832
4833                 xconfig = dgap_create_config_string(brd, string);
4834
4835                 /* Write string to board memory */
4836                 config = addr + CONFIG;
4837                 for (; i < CONFIGSIZE; i++, config++, xconfig++) {
4838                         writeb(*xconfig, config);
4839                         if ((*xconfig & 0xff) == 0xff)
4840                                 break;
4841                 }
4842         }
4843
4844         writel(0xbfc01004, (addr + 0xc34));
4845         writel(0x3, (addr + 0xc30));
4846
4847         /* change states. */
4848         brd->state = WAIT_FEP_LOAD;
4849
4850         DPR_INIT(("dgap_do_fep_load() for board %s : finish\n", brd->name));
4851
4852 }
4853
4854
4855 /*
4856  * Waits for the FEP to report thats its ready for us to use.
4857  */
4858 static void dgap_do_wait_for_fep(struct board_t *brd)
4859 {
4860         uchar *addr;
4861         u16 word;
4862
4863         if (!brd || (brd->magic != DGAP_BOARD_MAGIC) || !brd->re_map_membase)
4864                 return;
4865
4866         addr = brd->re_map_membase;
4867
4868         DPR_INIT(("dgap_do_wait_for_fep() for board %s : start. addr: %p\n", brd->name, addr));
4869
4870         word = readw(addr + FEPSTAT);
4871
4872         /* Check to see if FEP is up and running now. */
4873         if (word == *(u16 *) "OS") {
4874                 DPR_INIT(("GOT OS in memory for board %s, moving states.\n", brd->name));
4875                 brd->state = FINISHED_FEP_LOAD;
4876
4877                 /*
4878                  * Check to see if the board can support FEP5+ commands.
4879                  */
4880                 word = readw(addr + FEP5_PLUS);
4881                 if (word == *(u16 *) "5A") {
4882                         DPR_INIT(("GOT 5A in memory for board %s, board supports extended FEP5 commands.\n", brd->name));
4883                         brd->bd_flags |= BD_FEP5PLUS;
4884                 }
4885
4886                 return;
4887         }
4888
4889         /* Give up on board after too long of time taken */
4890         if (brd->wait_for_fep++ > 5000) {
4891                 u16 err1 = readw(addr + SEQUENCE);
4892                 u16 err2 = readw(addr + ERROR);
4893                 APR(("***WARNING*** FEPOS for %s not functioning.  Error #(%x,%x).\n",
4894                         brd->name, err1, err2));
4895                 brd->state = BOARD_FAILED;
4896                 brd->dpastatus = BD_NOFEP;
4897         }
4898
4899         DPR_INIT(("dgap_do_wait_for_fep() for board %s : finish\n", brd->name));
4900 }
4901
4902
4903 /*
4904  * Physically forces the FEP5 card to reset itself.
4905  */
4906 static void dgap_do_reset_board(struct board_t *brd)
4907 {
4908         uchar check;
4909         u32 check1;
4910         u32 check2;
4911         int i = 0;
4912
4913         if (!brd || (brd->magic != DGAP_BOARD_MAGIC) || !brd->re_map_membase || !brd->re_map_port) {
4914                 DPR_INIT(("dgap_do_reset_board() start. bad values. brd: %p mem: %p io: %p\n",
4915                         brd, brd ? brd->re_map_membase : 0, brd ? brd->re_map_port : 0));
4916                 return;
4917         }
4918
4919         DPR_INIT(("dgap_do_reset_board() start. io: %p\n", brd->re_map_port));
4920
4921         /* FEPRST does not vary among supported boards */
4922         writeb(FEPRST, brd->re_map_port);
4923
4924         for (i = 0; i <= 1000; i++) {
4925                 check = readb(brd->re_map_port) & 0xe;
4926                 if (check == FEPRST)
4927                         break;
4928                 udelay(10);
4929
4930         }
4931         if (i > 1000) {
4932                 APR(("*** WARNING *** Board not resetting...  Failing board.\n"));
4933                 brd->state = BOARD_FAILED;
4934                 brd->dpastatus = BD_NOFEP;
4935                 goto failed;
4936         }
4937
4938         /*
4939          * Make sure there really is memory out there.
4940          */
4941         writel(0xa55a3cc3, (brd->re_map_membase + LOWMEM));
4942         writel(0x5aa5c33c, (brd->re_map_membase + HIGHMEM));
4943         check1 = readl(brd->re_map_membase + LOWMEM);
4944         check2 = readl(brd->re_map_membase + HIGHMEM);
4945
4946         if ((check1 != 0xa55a3cc3) || (check2 != 0x5aa5c33c)) {
4947                 APR(("*** Warning *** No memory at %p for board.\n", brd->re_map_membase));
4948                 brd->state = BOARD_FAILED;
4949                 brd->dpastatus = BD_NOFEP;
4950                 goto failed;
4951         }
4952
4953         if (brd->state != BOARD_FAILED)
4954                 brd->state = FINISHED_RESET;
4955
4956 failed:
4957         DPR_INIT(("dgap_do_reset_board() finish\n"));
4958 }
4959
4960
4961 /*
4962  * Sends a concentrator image into the FEP5 board.
4963  */
4964 void dgap_do_conc_load(struct board_t *brd, uchar *uaddr, int len)
4965 {
4966         char *vaddr;
4967         u16 offset = 0;
4968         struct downld_t *to_dp;
4969
4970         if (!brd || (brd->magic != DGAP_BOARD_MAGIC) || !brd->re_map_membase)
4971                 return;
4972
4973         vaddr = brd->re_map_membase;
4974
4975         offset = readw((u16 *) (vaddr + DOWNREQ));
4976         to_dp = (struct downld_t *) (vaddr + (int) offset);
4977
4978         /*
4979          * The image was already read into kernel space,
4980          * we do NOT need a user space read here
4981          */
4982         memcpy_toio((char *) to_dp, uaddr, sizeof(struct downld_t));
4983
4984         /* Tell card we have data for it */
4985         writew(0, vaddr + (DOWNREQ));
4986
4987         brd->conc_dl_status = NO_PENDING_CONCENTRATOR_REQUESTS;
4988 }
4989
4990
4991 #define EXPANSION_ROM_SIZE      (64 * 1024)
4992 #define FEP5_ROM_MAGIC          (0xFEFFFFFF)
4993
4994 static void dgap_get_vpd(struct board_t *brd)
4995 {
4996         u32 magic;
4997         u32 base_offset;
4998         u16 rom_offset;
4999         u16 vpd_offset;
5000         u16 image_length;
5001         u16 i;
5002         uchar byte1;
5003         uchar byte2;
5004
5005         /*
5006          * Poke the magic number at the PCI Rom Address location.
5007          * If VPD is supported, the value read from that address
5008          * will be non-zero.
5009          */
5010         magic = FEP5_ROM_MAGIC;
5011         pci_write_config_dword(brd->pdev, PCI_ROM_ADDRESS, magic);
5012         pci_read_config_dword(brd->pdev, PCI_ROM_ADDRESS, &magic);
5013
5014         /* VPD not supported, bail */
5015         if (!magic)
5016                 return;
5017
5018         /*
5019          * To get to the OTPROM memory, we have to send the boards base
5020          * address or'ed with 1 into the PCI Rom Address location.
5021          */
5022         magic = brd->membase | 0x01;
5023         pci_write_config_dword(brd->pdev, PCI_ROM_ADDRESS, magic);
5024         pci_read_config_dword(brd->pdev, PCI_ROM_ADDRESS, &magic);
5025
5026         byte1 = readb(brd->re_map_membase);
5027         byte2 = readb(brd->re_map_membase + 1);
5028
5029         /*
5030          * If the board correctly swapped to the OTPROM memory,
5031          * the first 2 bytes (header) should be 0x55, 0xAA
5032          */
5033         if (byte1 == 0x55 && byte2 == 0xAA) {
5034
5035                 base_offset = 0;
5036
5037                 /*
5038                  * We have to run through all the OTPROM memory looking
5039                  * for the VPD offset.
5040                  */
5041                 while (base_offset <= EXPANSION_ROM_SIZE) {
5042
5043                         /*
5044                          * Lots of magic numbers here.
5045                          *
5046                          * The VPD offset is located inside the ROM Data Structure.
5047                          * We also have to remember the length of each
5048                          * ROM Data Structure, so we can "hop" to the next
5049                          * entry if the VPD isn't in the current
5050                          * ROM Data Structure.
5051                          */
5052                         rom_offset = readw(brd->re_map_membase + base_offset + 0x18);
5053                         image_length = readw(brd->re_map_membase + rom_offset + 0x10) * 512;
5054                         vpd_offset = readw(brd->re_map_membase + rom_offset + 0x08);
5055
5056                         /* Found the VPD entry */
5057                         if (vpd_offset)
5058                                 break;
5059
5060                         /* We didn't find a VPD entry, go to next ROM entry. */
5061                         base_offset += image_length;
5062
5063                         byte1 = readb(brd->re_map_membase + base_offset);
5064                         byte2 = readb(brd->re_map_membase + base_offset + 1);
5065
5066                         /*
5067                          * If the new ROM offset doesn't have 0x55, 0xAA
5068                          * as its header, we have run out of ROM.
5069                          */
5070                         if (byte1 != 0x55 || byte2 != 0xAA)
5071                                 break;
5072                 }
5073
5074                 /*
5075                  * If we have a VPD offset, then mark the board
5076                  * as having a valid VPD, and copy VPDSIZE (512) bytes of
5077                  * that VPD to the buffer we have in our board structure.
5078                  */
5079                 if (vpd_offset) {
5080                         brd->bd_flags |= BD_HAS_VPD;
5081                         for (i = 0; i < VPDSIZE; i++)
5082                                 brd->vpd[i] = readb(brd->re_map_membase + vpd_offset + i);
5083                 }
5084         }
5085
5086         /*
5087          * We MUST poke the magic number at the PCI Rom Address location again.
5088          * This makes the card report the regular board memory back to us,
5089          * rather than the OTPROM memory.
5090          */
5091         magic = FEP5_ROM_MAGIC;
5092         pci_write_config_dword(brd->pdev, PCI_ROM_ADDRESS, magic);
5093 }
5094
5095
5096 /*
5097  * Our board poller function.
5098  */
5099 void dgap_poll_tasklet(unsigned long data)
5100 {
5101         struct board_t *bd = (struct board_t *) data;
5102         ulong  lock_flags;
5103         ulong  lock_flags2;
5104         char *vaddr;
5105         u16 head, tail;
5106         u16 *chk_addr;
5107         u16 check = 0;
5108
5109         if (!bd || (bd->magic != DGAP_BOARD_MAGIC)) {
5110                 APR(("dgap_poll_tasklet() - NULL or bad bd.\n"));
5111                 return;
5112         }
5113
5114         if (bd->inhibit_poller)
5115                 return;
5116
5117         DGAP_LOCK(bd->bd_lock, lock_flags);
5118
5119         vaddr = bd->re_map_membase;
5120
5121         /*
5122          * If board is ready, parse deeper to see if there is anything to do.
5123          */
5124         if (bd->state == BOARD_READY) {
5125
5126                 struct ev_t *eaddr = NULL;
5127
5128                 if (!bd->re_map_membase) {
5129                         DGAP_UNLOCK(bd->bd_lock, lock_flags);
5130                         return;
5131                 }
5132                 if (!bd->re_map_port) {
5133                         DGAP_UNLOCK(bd->bd_lock, lock_flags);
5134                         return;
5135                 }
5136
5137                 if (!bd->nasync) {
5138                         goto out;
5139                 }
5140
5141                 /*
5142                  * If this is a CX or EPCX, we need to see if the firmware
5143                  * is requesting a concentrator image from us.
5144                  */
5145                 if ((bd->type == PCX) || (bd->type == PEPC)) {
5146                         chk_addr = (u16 *) (vaddr + DOWNREQ);
5147                         check = readw(chk_addr);
5148                         /* Nonzero if FEP is requesting concentrator image. */
5149                         if (check) {
5150                                 if (bd->conc_dl_status == NO_PENDING_CONCENTRATOR_REQUESTS)
5151                                         bd->conc_dl_status = NEED_CONCENTRATOR;
5152                                 /*
5153                                  * Signal downloader, its got some work to do.
5154                                  */
5155                                 DGAP_LOCK(dgap_dl_lock, lock_flags2);
5156                                 if (dgap_dl_action != 1) {
5157                                         dgap_dl_action = 1;
5158                                         wake_up_interruptible(&dgap_dl_wait);
5159                                 }
5160                                 DGAP_UNLOCK(dgap_dl_lock, lock_flags2);
5161
5162                         }
5163                 }
5164
5165                 eaddr = (struct ev_t *) (vaddr + EVBUF);
5166
5167                 /* Get our head and tail */
5168                 head = readw(&(eaddr->ev_head));
5169                 tail = readw(&(eaddr->ev_tail));
5170
5171                 /*
5172                  * If there is an event pending. Go service it.
5173                  */
5174                 if (head != tail) {
5175                         DGAP_UNLOCK(bd->bd_lock, lock_flags);
5176                         dgap_event(bd);
5177                         DGAP_LOCK(bd->bd_lock, lock_flags);
5178                 }
5179
5180 out:
5181                 /*
5182                  * If board is doing interrupts, ACK the interrupt.
5183                  */
5184                 if (bd && bd->intr_running) {
5185                         readb(bd->re_map_port + 2);
5186                 }
5187
5188                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
5189                 return;
5190         }
5191
5192         /* Our state machine to get the board up and running */
5193
5194         /* Reset board */
5195         if (bd->state == NEED_RESET) {
5196
5197                 /* Get VPD info */
5198                 dgap_get_vpd(bd);
5199
5200                 dgap_do_reset_board(bd);
5201         }
5202
5203         /* Move to next state */
5204         if (bd->state == FINISHED_RESET) {
5205                 bd->state = NEED_CONFIG;
5206         }
5207
5208         if (bd->state == NEED_CONFIG) {
5209                 /*
5210                  * Match this board to a config the user created for us.
5211                  */
5212                 bd->bd_config = dgap_find_config(bd->type, bd->pci_bus, bd->pci_slot);
5213
5214                 /*
5215                  * Because the 4 port Xr products share the same PCI ID
5216                  * as the 8 port Xr products, if we receive a NULL config
5217                  * back, and this is a PAPORT8 board, retry with a
5218                  * PAPORT4 attempt as well.
5219                  */
5220                 if (bd->type == PAPORT8 && !bd->bd_config) {
5221                         bd->bd_config = dgap_find_config(PAPORT4, bd->pci_bus, bd->pci_slot);
5222                 }
5223
5224                 /*
5225                  * Register the ttys (if any) into the kernel.
5226                  */
5227                 if (bd->bd_config) {
5228                         bd->state = FINISHED_CONFIG;
5229                 }
5230                 else {
5231                         bd->state = CONFIG_NOT_FOUND;
5232                 }
5233         }
5234
5235         /* Move to next state */
5236         if (bd->state == FINISHED_CONFIG) {
5237                 bd->state = NEED_DEVICE_CREATION;
5238         }
5239
5240         /* Move to next state */
5241         if (bd->state == NEED_DEVICE_CREATION) {
5242                 /*
5243                  * Signal downloader, its got some work to do.
5244                  */
5245                 DGAP_LOCK(dgap_dl_lock, lock_flags2);
5246                 if (dgap_dl_action != 1) {
5247                         dgap_dl_action = 1;
5248                         wake_up_interruptible(&dgap_dl_wait);
5249                 }
5250                 DGAP_UNLOCK(dgap_dl_lock, lock_flags2);
5251         }
5252
5253         /* Move to next state */
5254         if (bd->state == FINISHED_DEVICE_CREATION) {
5255                 bd->state = NEED_BIOS_LOAD;
5256         }
5257
5258         /* Move to next state */
5259         if (bd->state == NEED_BIOS_LOAD) {
5260                 /*
5261                  * Signal downloader, its got some work to do.
5262                  */
5263                 DGAP_LOCK(dgap_dl_lock, lock_flags2);
5264                 if (dgap_dl_action != 1) {
5265                         dgap_dl_action = 1;
5266                         wake_up_interruptible(&dgap_dl_wait);
5267                 }
5268                 DGAP_UNLOCK(dgap_dl_lock, lock_flags2);
5269         }
5270
5271         /* Wait for BIOS to test board... */
5272         if (bd->state == WAIT_BIOS_LOAD) {
5273                 dgap_do_wait_for_bios(bd);
5274         }
5275
5276         /* Move to next state */
5277         if (bd->state == FINISHED_BIOS_LOAD) {
5278                 bd->state = NEED_FEP_LOAD;
5279
5280                 /*
5281                  * Signal downloader, its got some work to do.
5282                  */
5283                 DGAP_LOCK(dgap_dl_lock, lock_flags2);
5284                 if (dgap_dl_action != 1) {
5285                         dgap_dl_action = 1;
5286                         wake_up_interruptible(&dgap_dl_wait);
5287                 }
5288                 DGAP_UNLOCK(dgap_dl_lock, lock_flags2);
5289         }
5290
5291         /* Wait for FEP to load on board... */
5292         if (bd->state == WAIT_FEP_LOAD) {
5293                 dgap_do_wait_for_fep(bd);
5294         }
5295
5296
5297         /* Move to next state */
5298         if (bd->state == FINISHED_FEP_LOAD) {
5299
5300                 /*
5301                  * Do tty device initialization.
5302                  */
5303                 int rc = dgap_tty_init(bd);
5304
5305                 if (rc < 0) {
5306                         dgap_tty_uninit(bd);
5307                         APR(("Can't init tty devices (%d)\n", rc));
5308                         bd->state = BOARD_FAILED;
5309                         bd->dpastatus = BD_NOFEP;
5310                 }
5311                 else {
5312                         bd->state = NEED_PROC_CREATION;
5313
5314                         /*
5315                          * Signal downloader, its got some work to do.
5316                          */
5317                         DGAP_LOCK(dgap_dl_lock, lock_flags2);
5318                         if (dgap_dl_action != 1) {
5319                                 dgap_dl_action = 1;
5320                                 wake_up_interruptible(&dgap_dl_wait);
5321                         }
5322                         DGAP_UNLOCK(dgap_dl_lock, lock_flags2);
5323                 }
5324         }
5325
5326         /* Move to next state */
5327         if (bd->state == FINISHED_PROC_CREATION) {
5328
5329                 bd->state = BOARD_READY;
5330                 bd->dpastatus = BD_RUNNING;
5331
5332                 /*
5333                  * If user requested the board to run in interrupt mode,
5334                  * go and set it up on the board.
5335                  */
5336                 if (bd->intr_used) {
5337                         writew(1, (bd->re_map_membase + ENABLE_INTR));
5338                         /*
5339                          * Tell the board to poll the UARTS as fast as possible.
5340                          */
5341                         writew(FEPPOLL_MIN, (bd->re_map_membase + FEPPOLL));
5342                         bd->intr_running = 1;
5343                 }
5344
5345                 /* Wake up anyone waiting for board state to change to ready */
5346                 wake_up_interruptible(&bd->state_wait);
5347         }
5348
5349         DGAP_UNLOCK(bd->bd_lock, lock_flags);
5350 }
5351
5352
5353 /*=======================================================================
5354  *
5355  *      dgap_cmdb - Sends a 2 byte command to the FEP.
5356  *
5357  *              ch      - Pointer to channel structure.
5358  *              cmd     - Command to be sent.
5359  *              byte1   - Integer containing first byte to be sent.
5360  *              byte2   - Integer containing second byte to be sent.
5361  *              ncmds   - Wait until ncmds or fewer cmds are left
5362  *                        in the cmd buffer before returning.
5363  *
5364  *=======================================================================*/
5365 void dgap_cmdb(struct channel_t *ch, uchar cmd, uchar byte1, uchar byte2, uint ncmds)
5366 {
5367         char            *vaddr = NULL;
5368         struct cm_t     *cm_addr = NULL;
5369         uint            count;
5370         uint            n;
5371         u16             head;
5372         u16             tail;
5373
5374         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
5375                 return;
5376
5377         /*
5378          * Check if board is still alive.
5379          */
5380         if (ch->ch_bd->state == BOARD_FAILED) {
5381                 DPR_CORE(("%s:%d board is in failed state.\n", __FILE__, __LINE__));
5382                 return;
5383         }
5384
5385         /*
5386          * Make sure the pointers are in range before
5387          * writing to the FEP memory.
5388          */
5389         vaddr = ch->ch_bd->re_map_membase;
5390
5391         if (!vaddr)
5392                 return;
5393
5394         cm_addr = (struct cm_t *) (vaddr + CMDBUF);
5395         head = readw(&(cm_addr->cm_head));
5396
5397         /*
5398          * Forget it if pointers out of range.
5399          */
5400         if (head >= (CMDMAX - CMDSTART) || (head & 03)) {
5401                 DPR_CORE(("%s:%d pointers out of range, failing board!\n", __FILE__, __LINE__));
5402                 ch->ch_bd->state = BOARD_FAILED;
5403                 return;
5404         }
5405
5406         /*
5407          * Put the data in the circular command buffer.
5408          */
5409         writeb(cmd, (char *) (vaddr + head + CMDSTART + 0));
5410         writeb((uchar) ch->ch_portnum, (char *) (vaddr + head + CMDSTART + 1));
5411         writeb(byte1, (char *) (vaddr + head + CMDSTART + 2));
5412         writeb(byte2, (char *) (vaddr + head + CMDSTART + 3));
5413
5414         head = (head + 4) & (CMDMAX - CMDSTART - 4);
5415
5416         writew(head, &(cm_addr->cm_head));
5417
5418         /*
5419          * Wait if necessary before updating the head
5420          * pointer to limit the number of outstanding
5421          * commands to the FEP.   If the time spent waiting
5422          * is outlandish, declare the FEP dead.
5423          */
5424         for (count = dgap_count ;;) {
5425
5426                 head = readw(&(cm_addr->cm_head));
5427                 tail = readw(&(cm_addr->cm_tail));
5428
5429                 n = (head - tail) & (CMDMAX - CMDSTART - 4);
5430
5431                 if (n <= ncmds * sizeof(struct cm_t))
5432                         break;
5433
5434                 if (--count == 0) {
5435                         DPR_CORE(("%s:%d failing board.\n",__FILE__, __LINE__));
5436                         ch->ch_bd->state = BOARD_FAILED;
5437                         return;
5438                 }
5439                 udelay(10);
5440         }
5441 }
5442
5443
5444 /*=======================================================================
5445  *
5446  *      dgap_cmdw - Sends a 1 word command to the FEP.
5447  *
5448  *              ch      - Pointer to channel structure.
5449  *              cmd     - Command to be sent.
5450  *              word    - Integer containing word to be sent.
5451  *              ncmds   - Wait until ncmds or fewer cmds are left
5452  *                        in the cmd buffer before returning.
5453  *
5454  *=======================================================================*/
5455 void dgap_cmdw(struct channel_t *ch, uchar cmd, u16 word, uint ncmds)
5456 {
5457         char            *vaddr = NULL;
5458         struct cm_t     *cm_addr = NULL;
5459         uint            count;
5460         uint            n;
5461         u16             head;
5462         u16             tail;
5463
5464         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
5465                 return;
5466
5467         /*
5468          * Check if board is still alive.
5469          */
5470         if (ch->ch_bd->state == BOARD_FAILED) {
5471                 DPR_CORE(("%s:%d board is failed!\n", __FILE__, __LINE__));
5472                 return;
5473         }
5474
5475         /*
5476          * Make sure the pointers are in range before
5477          * writing to the FEP memory.
5478          */
5479         vaddr = ch->ch_bd->re_map_membase;
5480         if (!vaddr)
5481                 return;
5482
5483         cm_addr = (struct cm_t *) (vaddr + CMDBUF);
5484         head = readw(&(cm_addr->cm_head));
5485
5486         /*
5487          * Forget it if pointers out of range.
5488          */
5489         if (head >= (CMDMAX - CMDSTART) || (head & 03)) {
5490                 DPR_CORE(("%s:%d Pointers out of range.  Failing board.\n",__FILE__, __LINE__));
5491                 ch->ch_bd->state = BOARD_FAILED;
5492                 return;
5493         }
5494
5495         /*
5496          * Put the data in the circular command buffer.
5497          */
5498         writeb(cmd, (char *) (vaddr + head + CMDSTART + 0));
5499         writeb((uchar) ch->ch_portnum, (char *) (vaddr + head + CMDSTART + 1));
5500         writew((u16) word, (char *) (vaddr + head + CMDSTART + 2));
5501
5502         head = (head + 4) & (CMDMAX - CMDSTART - 4);
5503
5504         writew(head, &(cm_addr->cm_head));
5505
5506         /*
5507          * Wait if necessary before updating the head
5508          * pointer to limit the number of outstanding
5509          * commands to the FEP.   If the time spent waiting
5510          * is outlandish, declare the FEP dead.
5511          */
5512         for (count = dgap_count ;;) {
5513
5514                 head = readw(&(cm_addr->cm_head));
5515                 tail = readw(&(cm_addr->cm_tail));
5516
5517                 n = (head - tail) & (CMDMAX - CMDSTART - 4);
5518
5519                 if (n <= ncmds * sizeof(struct cm_t))
5520                         break;
5521
5522                 if (--count == 0) {
5523                         DPR_CORE(("%s:%d Failing board.\n",__FILE__, __LINE__));
5524                         ch->ch_bd->state = BOARD_FAILED;
5525                         return;
5526                 }
5527                 udelay(10);
5528         }
5529 }
5530
5531
5532
5533 /*=======================================================================
5534  *
5535  *      dgap_cmdw_ext - Sends a extended word command to the FEP.
5536  *
5537  *              ch      - Pointer to channel structure.
5538  *              cmd     - Command to be sent.
5539  *              word    - Integer containing word to be sent.
5540  *              ncmds   - Wait until ncmds or fewer cmds are left
5541  *                        in the cmd buffer before returning.
5542  *
5543  *=======================================================================*/
5544 static void dgap_cmdw_ext(struct channel_t *ch, u16 cmd, u16 word, uint ncmds)
5545 {
5546         char            *vaddr = NULL;
5547         struct cm_t     *cm_addr = NULL;
5548         uint            count;
5549         uint            n;
5550         u16             head;
5551         u16             tail;
5552
5553         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
5554                 return;
5555
5556         /*
5557          * Check if board is still alive.
5558          */
5559         if (ch->ch_bd->state == BOARD_FAILED) {
5560                 DPR_CORE(("%s:%d board is failed!\n", __FILE__, __LINE__));
5561                 return;
5562         }
5563
5564         /*
5565          * Make sure the pointers are in range before
5566          * writing to the FEP memory.
5567          */
5568         vaddr = ch->ch_bd->re_map_membase;
5569         if (!vaddr)
5570                 return;
5571
5572         cm_addr = (struct cm_t *) (vaddr + CMDBUF);
5573         head = readw(&(cm_addr->cm_head));
5574
5575         /*
5576          * Forget it if pointers out of range.
5577          */
5578         if (head >= (CMDMAX - CMDSTART) || (head & 03)) {
5579                 DPR_CORE(("%s:%d Pointers out of range.  Failing board.\n",__FILE__, __LINE__));
5580                 ch->ch_bd->state = BOARD_FAILED;
5581                 return;
5582         }
5583
5584         /*
5585          * Put the data in the circular command buffer.
5586          */
5587
5588         /* Write an FF to tell the FEP that we want an extended command */
5589         writeb((uchar) 0xff, (char *) (vaddr + head + CMDSTART + 0));
5590
5591         writeb((uchar) ch->ch_portnum, (uchar *) (vaddr + head + CMDSTART + 1));
5592         writew((u16) cmd, (char *) (vaddr + head + CMDSTART + 2));
5593
5594         /*
5595          * If the second part of the command won't fit,
5596          * put it at the beginning of the circular buffer.
5597          */
5598         if (((head + 4) >= ((CMDMAX - CMDSTART)) || (head & 03))) {
5599                 writew((u16) word, (char *) (vaddr + CMDSTART));
5600         } else {
5601                 writew((u16) word, (char *) (vaddr + head + CMDSTART + 4));
5602         }
5603
5604         head = (head + 8) & (CMDMAX - CMDSTART - 4);
5605
5606         writew(head, &(cm_addr->cm_head));
5607
5608         /*
5609          * Wait if necessary before updating the head
5610          * pointer to limit the number of outstanding
5611          * commands to the FEP.   If the time spent waiting
5612          * is outlandish, declare the FEP dead.
5613          */
5614         for (count = dgap_count ;;) {
5615
5616                 head = readw(&(cm_addr->cm_head));
5617                 tail = readw(&(cm_addr->cm_tail));
5618
5619                 n = (head - tail) & (CMDMAX - CMDSTART - 4);
5620
5621                 if (n <= ncmds * sizeof(struct cm_t))
5622                         break;
5623
5624                 if (--count == 0) {
5625                         DPR_CORE(("%s:%d Failing board.\n",__FILE__, __LINE__));
5626                         ch->ch_bd->state = BOARD_FAILED;
5627                         return;
5628                 }
5629                 udelay(10);
5630         }
5631 }
5632
5633
5634 /*=======================================================================
5635  *
5636  *      dgap_wmove - Write data to FEP buffer.
5637  *
5638  *              ch      - Pointer to channel structure.
5639  *              buf     - Poiter to characters to be moved.
5640  *              cnt     - Number of characters to move.
5641  *
5642  *=======================================================================*/
5643 void dgap_wmove(struct channel_t *ch, char *buf, uint cnt)
5644 {
5645         int    n;
5646         char   *taddr;
5647         struct bs_t    *bs;
5648         u16    head;
5649
5650         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
5651                 return;
5652
5653         /*
5654          * Check parameters.
5655          */
5656         bs   = ch->ch_bs;
5657         head = readw(&(bs->tx_head));
5658
5659         /*
5660          * If pointers are out of range, just return.
5661          */
5662         if ((cnt > ch->ch_tsize) || (unsigned)(head - ch->ch_tstart) >= ch->ch_tsize) {
5663                 DPR_CORE(("%s:%d pointer out of range", __FILE__, __LINE__));
5664                 return;
5665         }
5666
5667         /*
5668          * If the write wraps over the top of the circular buffer,
5669          * move the portion up to the wrap point, and reset the
5670          * pointers to the bottom.
5671          */
5672         n = ch->ch_tstart + ch->ch_tsize - head;
5673
5674         if (cnt >= n) {
5675                 cnt -= n;
5676                 taddr = ch->ch_taddr + head;
5677                 memcpy_toio(taddr, buf, n);
5678                 head = ch->ch_tstart;
5679                 buf += n;
5680         }
5681
5682         /*
5683          * Move rest of data.
5684          */
5685         taddr = ch->ch_taddr + head;
5686         n = cnt;
5687         memcpy_toio(taddr, buf, n);
5688         head += cnt;
5689
5690         writew(head, &(bs->tx_head));
5691 }
5692
5693 /*
5694  * Retrives the current custom baud rate from FEP memory,
5695  * and returns it back to the user.
5696  * Returns 0 on error.
5697  */
5698 uint dgap_get_custom_baud(struct channel_t *ch)
5699 {
5700         uchar *vaddr;
5701         ulong offset = 0;
5702         uint value = 0;
5703
5704         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC) {
5705                 return 0;
5706         }
5707
5708         if (!ch->ch_bd || ch->ch_bd->magic != DGAP_BOARD_MAGIC) {
5709                 return 0;
5710         }
5711
5712         if (!(ch->ch_bd->bd_flags & BD_FEP5PLUS))
5713                 return 0;
5714
5715         vaddr = ch->ch_bd->re_map_membase;
5716
5717         if (!vaddr)
5718                 return 0;
5719
5720         /*
5721          * Go get from fep mem, what the fep
5722          * believes the custom baud rate is.
5723          */
5724         offset = ((((*(unsigned short *)(vaddr + ECS_SEG)) << 4) +
5725                 (ch->ch_portnum * 0x28) + LINE_SPEED));
5726
5727         value = readw(vaddr + offset);
5728         return value;
5729 }
5730
5731
5732 /*
5733  * Calls the firmware to reset this channel.
5734  */
5735 void dgap_firmware_reset_port(struct channel_t *ch)
5736 {
5737         dgap_cmdb(ch, CHRESET, 0, 0, 0);
5738
5739         /*
5740          * Now that the channel is reset, we need to make sure
5741          * all the current settings get reapplied to the port
5742          * in the firmware.
5743          *
5744          * So we will set the driver's cache of firmware
5745          * settings all to 0, and then call param.
5746          */
5747         ch->ch_fepiflag = 0;
5748         ch->ch_fepcflag = 0;
5749         ch->ch_fepoflag = 0;
5750         ch->ch_fepstartc = 0;
5751         ch->ch_fepstopc = 0;
5752         ch->ch_fepastartc = 0;
5753         ch->ch_fepastopc = 0;
5754         ch->ch_mostat = 0;
5755         ch->ch_hflow = 0;
5756 }
5757
5758
5759 /*=======================================================================
5760  *
5761  *      dgap_param - Set Digi parameters.
5762  *
5763  *              struct tty_struct *     - TTY for port.
5764  *
5765  *=======================================================================*/
5766 int dgap_param(struct tty_struct *tty)
5767 {
5768         struct ktermios *ts;
5769         struct board_t *bd;
5770         struct channel_t *ch;
5771         struct bs_t   *bs;
5772         struct un_t   *un;
5773         u16     head;
5774         u16     cflag;
5775         u16     iflag;
5776         uchar   mval;
5777         uchar   hflow;
5778
5779         if (!tty || tty->magic != TTY_MAGIC)
5780                 return -ENXIO;
5781
5782         un = (struct un_t *) tty->driver_data;
5783         if (!un || un->magic != DGAP_UNIT_MAGIC)
5784                 return -ENXIO;
5785
5786         ch = un->un_ch;
5787         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
5788                 return -ENXIO;
5789
5790         bd = ch->ch_bd;
5791         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
5792                 return -ENXIO;
5793
5794         bs = ch->ch_bs;
5795         if (!bs)
5796                 return -ENXIO;
5797
5798         DPR_PARAM(("param start: tdev: %x cflags: %x oflags: %x iflags: %x\n",
5799                 ch->ch_tun.un_dev, ch->ch_c_cflag, ch->ch_c_oflag, ch->ch_c_iflag));
5800
5801         ts = &tty->termios;
5802
5803         /*
5804          * If baud rate is zero, flush queues, and set mval to drop DTR.
5805          */
5806         if ((ch->ch_c_cflag & (CBAUD)) == 0) {
5807
5808                 /* flush rx */
5809                 head = readw(&(ch->ch_bs->rx_head));
5810                 writew(head, &(ch->ch_bs->rx_tail));
5811
5812                 /* flush tx */
5813                 head = readw(&(ch->ch_bs->tx_head));
5814                 writew(head, &(ch->ch_bs->tx_tail));
5815
5816                 ch->ch_flags |= (CH_BAUD0);
5817
5818                 /* Drop RTS and DTR */
5819                 ch->ch_mval &= ~(D_RTS(ch)|D_DTR(ch));
5820                 mval = D_DTR(ch) | D_RTS(ch);
5821                 ch->ch_baud_info = 0;
5822
5823         } else if (ch->ch_custom_speed && (bd->bd_flags & BD_FEP5PLUS)) {
5824                 /*
5825                  * Tell the fep to do the command
5826                  */
5827
5828                 DPR_PARAM(("param: Want %d speed\n", ch->ch_custom_speed));
5829
5830                 dgap_cmdw_ext(ch, 0xff01, ch->ch_custom_speed, 0);
5831
5832                 /*
5833                  * Now go get from fep mem, what the fep
5834                  * believes the custom baud rate is.
5835                  */
5836                 ch->ch_baud_info = ch->ch_custom_speed = dgap_get_custom_baud(ch);
5837
5838                 DPR_PARAM(("param: Got %d speed\n", ch->ch_custom_speed));
5839
5840                 /* Handle transition from B0 */
5841                 if (ch->ch_flags & CH_BAUD0) {
5842                         ch->ch_flags &= ~(CH_BAUD0);
5843                         ch->ch_mval |= (D_RTS(ch)|D_DTR(ch));
5844                 }
5845                 mval = D_DTR(ch) | D_RTS(ch);
5846
5847         } else {
5848                 /*
5849                  * Set baud rate, character size, and parity.
5850                  */
5851
5852
5853                 int iindex = 0;
5854                 int jindex = 0;
5855                 int baud = 0;
5856
5857                 ulong bauds[4][16] = {
5858                         { /* slowbaud */
5859                                 0,      50,     75,     110,
5860                                 134,    150,    200,    300,
5861                                 600,    1200,   1800,   2400,
5862                                 4800,   9600,   19200,  38400 },
5863                         { /* slowbaud & CBAUDEX */
5864                                 0,      57600,  115200, 230400,
5865                                 460800, 150,    200,    921600,
5866                                 600,    1200,   1800,   2400,
5867                                 4800,   9600,   19200,  38400 },
5868                         { /* fastbaud */
5869                                 0,      57600,  76800,  115200,
5870                                 14400,  57600,  230400, 76800,
5871                                 115200, 230400, 28800,  460800,
5872                                 921600, 9600,   19200,  38400 },
5873                         { /* fastbaud & CBAUDEX */
5874                                 0,      57600,  115200, 230400,
5875                                 460800, 150,    200,    921600,
5876                                 600,    1200,   1800,   2400,
5877                                 4800,   9600,   19200,  38400 }
5878                 };
5879
5880                 /* Only use the TXPrint baud rate if the terminal unit is NOT open */
5881                 if (!(ch->ch_tun.un_flags & UN_ISOPEN) && (un->un_type == DGAP_PRINT))
5882                         baud = C_BAUD(ch->ch_pun.un_tty) & 0xff;
5883                 else
5884                         baud = C_BAUD(ch->ch_tun.un_tty) & 0xff;
5885
5886                 if (ch->ch_c_cflag & CBAUDEX)
5887                         iindex = 1;
5888
5889                 if (ch->ch_digi.digi_flags & DIGI_FAST)
5890                         iindex += 2;
5891
5892                 jindex = baud;
5893
5894                 if ((iindex >= 0) && (iindex < 4) && (jindex >= 0) && (jindex < 16)) {
5895                         baud = bauds[iindex][jindex];
5896                 } else {
5897                         DPR_IOCTL(("baud indices were out of range (%d)(%d)",
5898                                 iindex, jindex));
5899                         baud = 0;
5900                 }
5901
5902                 if (baud == 0)
5903                         baud = 9600;
5904
5905                 ch->ch_baud_info = baud;
5906
5907
5908                 /*
5909                  * CBAUD has bit position 0x1000 set these days to indicate Linux
5910                  * baud rate remap.
5911                  * We use a different bit assignment for high speed.  Clear this
5912                  * bit out while grabbing the parts of "cflag" we want.
5913                  */
5914                 cflag = ch->ch_c_cflag & ((CBAUD ^ CBAUDEX) | PARODD | PARENB | CSTOPB | CSIZE);
5915
5916                 /*
5917                  * HUPCL bit is used by FEP to indicate fast baud
5918                  * table is to be used.
5919                  */
5920                 if ((ch->ch_digi.digi_flags & DIGI_FAST) || (ch->ch_c_cflag & CBAUDEX))
5921                         cflag |= HUPCL;
5922
5923
5924                 if ((ch->ch_c_cflag & CBAUDEX) && !(ch->ch_digi.digi_flags & DIGI_FAST)) {
5925                 /*
5926                  * The below code is trying to guarantee that only baud rates
5927                  * 115200, 230400, 460800, 921600 are remapped.  We use exclusive or
5928                  * because the various baud rates share common bit positions
5929                  * and therefore can't be tested for easily.
5930                  */
5931                         tcflag_t tcflag = (ch->ch_c_cflag & CBAUD) | CBAUDEX;
5932                         int baudpart = 0;
5933
5934                         /* Map high speed requests to index into FEP's baud table */
5935                         switch (tcflag) {
5936                         case B57600 :
5937                                 baudpart = 1;
5938                                 break;
5939 #ifdef B76800
5940                         case B76800 :
5941                                 baudpart = 2;
5942                                 break;
5943 #endif
5944                         case B115200 :
5945                                 baudpart = 3;
5946                                 break;
5947                         case B230400 :
5948                                 baudpart = 9;
5949                                 break;
5950                         case B460800 :
5951                                 baudpart = 11;
5952                                 break;
5953 #ifdef B921600
5954                         case B921600 :
5955                                 baudpart = 12;
5956                                 break;
5957 #endif
5958                         default:
5959                                 baudpart = 0;
5960                         }
5961
5962                         if (baudpart)
5963                                 cflag = (cflag & ~(CBAUD | CBAUDEX)) | baudpart;
5964                 }
5965
5966                 cflag &= 0xffff;
5967
5968                 if (cflag != ch->ch_fepcflag) {
5969                         ch->ch_fepcflag = (u16) (cflag & 0xffff);
5970
5971                         /* Okay to have channel and board locks held calling this */
5972                         dgap_cmdw(ch, SCFLAG, (u16) cflag, 0);
5973                 }
5974
5975                 /* Handle transition from B0 */
5976                 if (ch->ch_flags & CH_BAUD0) {
5977                         ch->ch_flags &= ~(CH_BAUD0);
5978                         ch->ch_mval |= (D_RTS(ch)|D_DTR(ch));
5979                 }
5980                 mval = D_DTR(ch) | D_RTS(ch);
5981         }
5982
5983         /*
5984          * Get input flags.
5985          */
5986         iflag = ch->ch_c_iflag & (IGNBRK | BRKINT | IGNPAR | PARMRK | INPCK | ISTRIP | IXON | IXANY | IXOFF);
5987
5988         if ((ch->ch_startc == _POSIX_VDISABLE) || (ch->ch_stopc == _POSIX_VDISABLE)) {
5989                 iflag &= ~(IXON | IXOFF);
5990                 ch->ch_c_iflag &= ~(IXON | IXOFF);
5991         }
5992
5993         /*
5994          * Only the IBM Xr card can switch between
5995          * 232 and 422 modes on the fly
5996          */
5997         if (bd->device == PCI_DEVICE_XR_IBM_DID) {
5998                 if (ch->ch_digi.digi_flags & DIGI_422)
5999                         dgap_cmdb(ch, SCOMMODE, MODE_422, 0, 0);
6000                 else
6001                         dgap_cmdb(ch, SCOMMODE, MODE_232, 0, 0);
6002         }
6003
6004         if (ch->ch_digi.digi_flags & DIGI_ALTPIN)
6005                 iflag |= IALTPIN ;
6006
6007         if (iflag != ch->ch_fepiflag) {
6008                 ch->ch_fepiflag = iflag;
6009
6010                 /* Okay to have channel and board locks held calling this */
6011                 dgap_cmdw(ch, SIFLAG, (u16) ch->ch_fepiflag, 0);
6012         }
6013
6014         /*
6015          * Select hardware handshaking.
6016          */
6017         hflow = 0;
6018
6019         if (ch->ch_c_cflag & CRTSCTS) {
6020                 hflow |= (D_RTS(ch) | D_CTS(ch));
6021         }
6022         if (ch->ch_digi.digi_flags & RTSPACE)
6023                 hflow |= D_RTS(ch);
6024         if (ch->ch_digi.digi_flags & DTRPACE)
6025                 hflow |= D_DTR(ch);
6026         if (ch->ch_digi.digi_flags & CTSPACE)
6027                 hflow |= D_CTS(ch);
6028         if (ch->ch_digi.digi_flags & DSRPACE)
6029                 hflow |= D_DSR(ch);
6030         if (ch->ch_digi.digi_flags & DCDPACE)
6031                 hflow |= D_CD(ch);
6032
6033         if (hflow != ch->ch_hflow) {
6034                 ch->ch_hflow = hflow;
6035
6036                 /* Okay to have channel and board locks held calling this */
6037                 dgap_cmdb(ch, SHFLOW, (uchar) hflow, 0xff, 0);
6038         }
6039
6040
6041         /*
6042          * Set RTS and/or DTR Toggle if needed, but only if product is FEP5+ based.
6043          */
6044         if (bd->bd_flags & BD_FEP5PLUS) {
6045                 u16 hflow2 = 0;
6046                 if (ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE) {
6047                         hflow2 |= (D_RTS(ch));
6048                 }
6049                 if (ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE) {
6050                         hflow2 |= (D_DTR(ch));
6051                 }
6052
6053                 dgap_cmdw_ext(ch, 0xff03, hflow2, 0);
6054         }
6055
6056         /*
6057          * Set modem control lines.
6058          */
6059
6060         mval ^= ch->ch_mforce & (mval ^ ch->ch_mval);
6061
6062         DPR_PARAM(("dgap_param: mval: %x ch_mforce: %x ch_mval: %x ch_mostat: %x\n",
6063                 mval, ch->ch_mforce, ch->ch_mval, ch->ch_mostat));
6064
6065         if (ch->ch_mostat ^ mval) {
6066                 ch->ch_mostat = mval;
6067
6068                 /* Okay to have channel and board locks held calling this */
6069                 DPR_PARAM(("dgap_param: Sending SMODEM mval: %x\n", mval));
6070                 dgap_cmdb(ch, SMODEM, (uchar) mval, D_RTS(ch)|D_DTR(ch), 0);
6071         }
6072
6073         /*
6074          * Read modem signals, and then call carrier function.
6075          */
6076         ch->ch_mistat = readb(&(bs->m_stat));
6077         dgap_carrier(ch);
6078
6079         /*
6080          * Set the start and stop characters.
6081          */
6082         if (ch->ch_startc != ch->ch_fepstartc || ch->ch_stopc != ch->ch_fepstopc) {
6083                 ch->ch_fepstartc = ch->ch_startc;
6084                 ch->ch_fepstopc =  ch->ch_stopc;
6085
6086                 /* Okay to have channel and board locks held calling this */
6087                 dgap_cmdb(ch, SFLOWC, ch->ch_fepstartc, ch->ch_fepstopc, 0);
6088         }
6089
6090         /*
6091          * Set the Auxiliary start and stop characters.
6092          */
6093         if (ch->ch_astartc != ch->ch_fepastartc || ch->ch_astopc != ch->ch_fepastopc) {
6094                 ch->ch_fepastartc = ch->ch_astartc;
6095                 ch->ch_fepastopc = ch->ch_astopc;
6096
6097                 /* Okay to have channel and board locks held calling this */
6098                 dgap_cmdb(ch, SAFLOWC, ch->ch_fepastartc, ch->ch_fepastopc, 0);
6099         }
6100
6101         DPR_PARAM(("param finish\n"));
6102
6103         return 0;
6104 }
6105
6106
6107 /*
6108  * dgap_parity_scan()
6109  *
6110  * Convert the FEP5 way of reporting parity errors and breaks into
6111  * the Linux line discipline way.
6112  */
6113 void dgap_parity_scan(struct channel_t *ch, unsigned char *cbuf, unsigned char *fbuf, int *len)
6114 {
6115         int l = *len;
6116         int count = 0;
6117         unsigned char *in, *cout, *fout;
6118         unsigned char c;
6119
6120         in = cbuf;
6121         cout = cbuf;
6122         fout = fbuf;
6123
6124         DPR_PSCAN(("dgap_parity_scan start\n"));
6125
6126         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
6127                 return;
6128
6129         while (l--) {
6130                 c = *in++;
6131                 switch (ch->pscan_state) {
6132                 default:
6133                         /* reset to sanity and fall through */
6134                         ch->pscan_state = 0;
6135
6136                 case 0:
6137                         /* No FF seen yet */
6138                         if (c == (unsigned char) '\377') {
6139                                 /* delete this character from stream */
6140                                 ch->pscan_state = 1;
6141                         } else {
6142                                 *cout++ = c;
6143                                 *fout++ = TTY_NORMAL;
6144                                 count += 1;
6145                         }
6146                         break;
6147
6148                 case 1:
6149                         /* first FF seen */
6150                         if (c == (unsigned char) '\377') {
6151                                 /* doubled ff, transform to single ff */
6152                                 *cout++ = c;
6153                                 *fout++ = TTY_NORMAL;
6154                                 count += 1;
6155                                 ch->pscan_state = 0;
6156                         } else {
6157                                 /* save value examination in next state */
6158                                 ch->pscan_savechar = c;
6159                                 ch->pscan_state = 2;
6160                         }
6161                         break;
6162
6163                 case 2:
6164                         /* third character of ff sequence */
6165
6166                         *cout++ = c;
6167
6168                         if (ch->pscan_savechar == 0x0) {
6169
6170                                 if (c == 0x0) {
6171                                         DPR_PSCAN(("dgap_parity_scan in 3rd char of ff seq. c: %x setting break.\n", c));
6172                                         ch->ch_err_break++;
6173                                         *fout++ = TTY_BREAK;
6174                                 }
6175                                 else {
6176                                         DPR_PSCAN(("dgap_parity_scan in 3rd char of ff seq. c: %x setting parity.\n", c));
6177                                         ch->ch_err_parity++;
6178                                         *fout++ = TTY_PARITY;
6179                                 }
6180                         }
6181                         else {
6182                                 DPR_PSCAN(("%s:%d Logic Error.\n", __FILE__, __LINE__));
6183                         }
6184
6185                         count += 1;
6186                         ch->pscan_state = 0;
6187                 }
6188         }
6189         *len = count;
6190         DPR_PSCAN(("dgap_parity_scan finish\n"));
6191 }
6192
6193
6194
6195
6196 /*=======================================================================
6197  *
6198  *      dgap_event - FEP to host event processing routine.
6199  *
6200  *              bd     - Board of current event.
6201  *
6202  *=======================================================================*/
6203 static int dgap_event(struct board_t *bd)
6204 {
6205         struct channel_t *ch;
6206         ulong           lock_flags;
6207         ulong           lock_flags2;
6208         struct bs_t     *bs;
6209         uchar           *event;
6210         uchar           *vaddr = NULL;
6211         struct ev_t     *eaddr = NULL;
6212         uint            head;
6213         uint            tail;
6214         int             port;
6215         int             reason;
6216         int             modem;
6217         int             b1;
6218
6219         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
6220                 return -ENXIO;
6221
6222         DGAP_LOCK(bd->bd_lock, lock_flags);
6223
6224         vaddr = bd->re_map_membase;
6225
6226         if (!vaddr) {
6227                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
6228                 return -ENXIO;
6229         }
6230
6231         eaddr = (struct ev_t *) (vaddr + EVBUF);
6232
6233         /* Get our head and tail */
6234         head = readw(&(eaddr->ev_head));
6235         tail = readw(&(eaddr->ev_tail));
6236
6237         /*
6238          * Forget it if pointers out of range.
6239          */
6240
6241         if (head >= EVMAX - EVSTART || tail >= EVMAX - EVSTART ||
6242             (head | tail) & 03) {
6243                 DPR_EVENT(("should be calling xxfail %d\n", __LINE__));
6244                 /* Let go of board lock */
6245                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
6246                 return -ENXIO;
6247         }
6248
6249         /*
6250          * Loop to process all the events in the buffer.
6251          */
6252         while (tail != head) {
6253
6254                 /*
6255                  * Get interrupt information.
6256                  */
6257
6258                 event = bd->re_map_membase + tail + EVSTART;
6259
6260                 port   = event[0];
6261                 reason = event[1];
6262                 modem  = event[2];
6263                 b1     = event[3];
6264
6265                 DPR_EVENT(("event: jiffies: %ld port: %d reason: %x modem: %x\n",
6266                         jiffies, port, reason, modem));
6267
6268                 /*
6269                  * Make sure the interrupt is valid.
6270                  */
6271                 if (port >= bd->nasync)
6272                         goto next;
6273
6274                 if (!(reason & (IFMODEM | IFBREAK | IFTLW | IFTEM | IFDATA))) {
6275                         goto next;
6276                 }
6277
6278                 ch = bd->channels[port];
6279
6280                 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC) {
6281                         goto next;
6282                 }
6283
6284                 /*
6285                  * If we have made it here, the event was valid.
6286                  * Lock down the channel.
6287                  */
6288                 DGAP_LOCK(ch->ch_lock, lock_flags2);
6289
6290                 bs = ch->ch_bs;
6291
6292                 if (!bs) {
6293                         DGAP_UNLOCK(ch->ch_lock, lock_flags2);
6294                         goto next;
6295                 }
6296
6297                 /*
6298                  * Process received data.
6299                  */
6300                 if (reason & IFDATA) {
6301
6302                         /*
6303                          * ALL LOCKS *MUST* BE DROPPED BEFORE CALLING INPUT!
6304                          * input could send some data to ld, which in turn
6305                          * could do a callback to one of our other functions.
6306                          */
6307                         DGAP_UNLOCK(ch->ch_lock, lock_flags2);
6308                         DGAP_UNLOCK(bd->bd_lock, lock_flags);
6309
6310                         dgap_input(ch);
6311
6312                         DGAP_LOCK(bd->bd_lock, lock_flags);
6313                         DGAP_LOCK(ch->ch_lock, lock_flags2);
6314
6315                         if (ch->ch_flags & CH_RACTIVE)
6316                                 ch->ch_flags |= CH_RENABLE;
6317                         else
6318                                 writeb(1, &(bs->idata));
6319
6320                         if (ch->ch_flags & CH_RWAIT) {
6321                                 ch->ch_flags &= ~CH_RWAIT;
6322
6323                                 wake_up_interruptible(&ch->ch_tun.un_flags_wait);
6324                         }
6325                 }
6326
6327                 /*
6328                  * Process Modem change signals.
6329                  */
6330                 if (reason & IFMODEM) {
6331                         ch->ch_mistat = modem;
6332                         dgap_carrier(ch);
6333                 }
6334
6335                 /*
6336                  * Process break.
6337                  */
6338                 if (reason & IFBREAK) {
6339
6340                         DPR_EVENT(("got IFBREAK\n"));
6341
6342                         if (ch->ch_tun.un_tty) {
6343                                 /* A break has been indicated */
6344                                 ch->ch_err_break++;
6345                                 tty_buffer_request_room(ch->ch_tun.un_tty->port, 1);
6346                                 tty_insert_flip_char(ch->ch_tun.un_tty->port, 0, TTY_BREAK);
6347                                 tty_flip_buffer_push(ch->ch_tun.un_tty->port);
6348                         }
6349                 }
6350
6351                 /*
6352                  * Process Transmit low.
6353                  */
6354                 if (reason & IFTLW) {
6355
6356                         DPR_EVENT(("event: got low event\n"));
6357
6358                         if (ch->ch_tun.un_flags & UN_LOW) {
6359                                 ch->ch_tun.un_flags &= ~UN_LOW;
6360
6361                                 if (ch->ch_tun.un_flags & UN_ISOPEN) {
6362                                         if ((ch->ch_tun.un_tty->flags &
6363                                            (1 << TTY_DO_WRITE_WAKEUP)) &&
6364                                                 ch->ch_tun.un_tty->ldisc->ops->write_wakeup)
6365                                         {
6366                                                 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
6367                                                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
6368                                                 (ch->ch_tun.un_tty->ldisc->ops->write_wakeup)(ch->ch_tun.un_tty);
6369                                                 DGAP_LOCK(bd->bd_lock, lock_flags);
6370                                                 DGAP_LOCK(ch->ch_lock, lock_flags2);
6371                                         }
6372                                         wake_up_interruptible(&ch->ch_tun.un_tty->write_wait);
6373                                         wake_up_interruptible(&ch->ch_tun.un_flags_wait);
6374
6375                                         DPR_EVENT(("event: Got low event. jiffies: %lu\n", jiffies));
6376                                 }
6377                         }
6378
6379                         if (ch->ch_pun.un_flags & UN_LOW) {
6380                                 ch->ch_pun.un_flags &= ~UN_LOW;
6381                                 if (ch->ch_pun.un_flags & UN_ISOPEN) {
6382                                         if ((ch->ch_pun.un_tty->flags &
6383                                            (1 << TTY_DO_WRITE_WAKEUP)) &&
6384                                                 ch->ch_pun.un_tty->ldisc->ops->write_wakeup)
6385                                         {
6386                                                 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
6387                                                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
6388                                                 (ch->ch_pun.un_tty->ldisc->ops->write_wakeup)(ch->ch_pun.un_tty);
6389                                                 DGAP_LOCK(bd->bd_lock, lock_flags);
6390                                                 DGAP_LOCK(ch->ch_lock, lock_flags2);
6391                                         }
6392                                         wake_up_interruptible(&ch->ch_pun.un_tty->write_wait);
6393                                         wake_up_interruptible(&ch->ch_pun.un_flags_wait);
6394                                 }
6395                         }
6396
6397                         if (ch->ch_flags & CH_WLOW) {
6398                                 ch->ch_flags &= ~CH_WLOW;
6399                                 wake_up_interruptible(&ch->ch_flags_wait);
6400                         }
6401                 }
6402
6403                 /*
6404                  * Process Transmit empty.
6405                  */
6406                 if (reason & IFTEM) {
6407                         DPR_EVENT(("event: got empty event\n"));
6408
6409                         if (ch->ch_tun.un_flags & UN_EMPTY) {
6410                                 ch->ch_tun.un_flags &= ~UN_EMPTY;
6411                                 if (ch->ch_tun.un_flags & UN_ISOPEN) {
6412                                         if ((ch->ch_tun.un_tty->flags &
6413                                            (1 << TTY_DO_WRITE_WAKEUP)) &&
6414                                                 ch->ch_tun.un_tty->ldisc->ops->write_wakeup)
6415                                         {
6416                                                 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
6417                                                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
6418
6419                                                 (ch->ch_tun.un_tty->ldisc->ops->write_wakeup)(ch->ch_tun.un_tty);
6420                                                 DGAP_LOCK(bd->bd_lock, lock_flags);
6421                                                 DGAP_LOCK(ch->ch_lock, lock_flags2);
6422                                         }
6423                                         wake_up_interruptible(&ch->ch_tun.un_tty->write_wait);
6424                                         wake_up_interruptible(&ch->ch_tun.un_flags_wait);
6425                                 }
6426                         }
6427
6428                         if (ch->ch_pun.un_flags & UN_EMPTY) {
6429                                 ch->ch_pun.un_flags &= ~UN_EMPTY;
6430                                 if (ch->ch_pun.un_flags & UN_ISOPEN) {
6431                                         if ((ch->ch_pun.un_tty->flags &
6432                                            (1 << TTY_DO_WRITE_WAKEUP)) &&
6433                                                 ch->ch_pun.un_tty->ldisc->ops->write_wakeup)
6434                                         {
6435                                                 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
6436                                                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
6437                                                 (ch->ch_pun.un_tty->ldisc->ops->write_wakeup)(ch->ch_pun.un_tty);
6438                                                 DGAP_LOCK(bd->bd_lock, lock_flags);
6439                                                 DGAP_LOCK(ch->ch_lock, lock_flags2);
6440                                         }
6441                                         wake_up_interruptible(&ch->ch_pun.un_tty->write_wait);
6442                                         wake_up_interruptible(&ch->ch_pun.un_flags_wait);
6443                                 }
6444                         }
6445
6446
6447                         if (ch->ch_flags & CH_WEMPTY) {
6448                                 ch->ch_flags &= ~CH_WEMPTY;
6449                                 wake_up_interruptible(&ch->ch_flags_wait);
6450                         }
6451                 }
6452
6453                 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
6454
6455 next:
6456                 tail = (tail + 4) & (EVMAX - EVSTART - 4);
6457         }
6458
6459         writew(tail, &(eaddr->ev_tail));
6460         DGAP_UNLOCK(bd->bd_lock, lock_flags);
6461
6462         return 0;
6463 }
6464
6465 static ssize_t dgap_driver_version_show(struct device_driver *ddp, char *buf)
6466 {
6467         return snprintf(buf, PAGE_SIZE, "%s\n", DG_PART);
6468 }
6469 static DRIVER_ATTR(version, S_IRUSR, dgap_driver_version_show, NULL);
6470
6471
6472 static ssize_t dgap_driver_boards_show(struct device_driver *ddp, char *buf)
6473 {
6474         return snprintf(buf, PAGE_SIZE, "%d\n", dgap_NumBoards);
6475 }
6476 static DRIVER_ATTR(boards, S_IRUSR, dgap_driver_boards_show, NULL);
6477
6478
6479 static ssize_t dgap_driver_maxboards_show(struct device_driver *ddp, char *buf)
6480 {
6481         return snprintf(buf, PAGE_SIZE, "%d\n", MAXBOARDS);
6482 }
6483 static DRIVER_ATTR(maxboards, S_IRUSR, dgap_driver_maxboards_show, NULL);
6484
6485
6486 static ssize_t dgap_driver_pollcounter_show(struct device_driver *ddp, char *buf)
6487 {
6488         return snprintf(buf, PAGE_SIZE, "%ld\n", dgap_poll_counter);
6489 }
6490 static DRIVER_ATTR(pollcounter, S_IRUSR, dgap_driver_pollcounter_show, NULL);
6491
6492
6493 static ssize_t dgap_driver_state_show(struct device_driver *ddp, char *buf)
6494 {
6495         return snprintf(buf, PAGE_SIZE, "%s\n", dgap_driver_state_text[dgap_driver_state]);
6496 }
6497 static DRIVER_ATTR(state, S_IRUSR, dgap_driver_state_show, NULL);
6498
6499
6500 static ssize_t dgap_driver_debug_show(struct device_driver *ddp, char *buf)
6501 {
6502         return snprintf(buf, PAGE_SIZE, "0x%x\n", dgap_debug);
6503 }
6504
6505 static ssize_t dgap_driver_debug_store(struct device_driver *ddp, const char *buf, size_t count)
6506 {
6507         sscanf(buf, "0x%x\n", &dgap_debug);
6508         return count;
6509 }
6510 static DRIVER_ATTR(debug, (S_IRUSR | S_IWUSR), dgap_driver_debug_show, dgap_driver_debug_store);
6511
6512
6513 static ssize_t dgap_driver_rawreadok_show(struct device_driver *ddp, char *buf)
6514 {
6515         return snprintf(buf, PAGE_SIZE, "0x%x\n", dgap_rawreadok);
6516 }
6517
6518 static ssize_t dgap_driver_rawreadok_store(struct device_driver *ddp, const char *buf, size_t count)
6519 {
6520         sscanf(buf, "0x%x\n", &dgap_rawreadok);
6521         return count;
6522 }
6523 static DRIVER_ATTR(rawreadok, (S_IRUSR | S_IWUSR), dgap_driver_rawreadok_show, dgap_driver_rawreadok_store);
6524
6525
6526 static ssize_t dgap_driver_pollrate_show(struct device_driver *ddp, char *buf)
6527 {
6528         return snprintf(buf, PAGE_SIZE, "%dms\n", dgap_poll_tick);
6529 }
6530
6531 static ssize_t dgap_driver_pollrate_store(struct device_driver *ddp, const char *buf, size_t count)
6532 {
6533         sscanf(buf, "%d\n", &dgap_poll_tick);
6534         return count;
6535 }
6536 static DRIVER_ATTR(pollrate, (S_IRUSR | S_IWUSR), dgap_driver_pollrate_show, dgap_driver_pollrate_store);
6537
6538
6539 void dgap_create_driver_sysfiles(struct pci_driver *dgap_driver)
6540 {
6541         int rc = 0;
6542         struct device_driver *driverfs = &dgap_driver->driver;
6543
6544         rc |= driver_create_file(driverfs, &driver_attr_version);
6545         rc |= driver_create_file(driverfs, &driver_attr_boards);
6546         rc |= driver_create_file(driverfs, &driver_attr_maxboards);
6547         rc |= driver_create_file(driverfs, &driver_attr_debug);
6548         rc |= driver_create_file(driverfs, &driver_attr_rawreadok);
6549         rc |= driver_create_file(driverfs, &driver_attr_pollrate);
6550         rc |= driver_create_file(driverfs, &driver_attr_pollcounter);
6551         rc |= driver_create_file(driverfs, &driver_attr_state);
6552         if (rc) {
6553                 printk(KERN_ERR "DGAP: sysfs driver_create_file failed!\n");
6554         }
6555 }
6556
6557
6558 void dgap_remove_driver_sysfiles(struct pci_driver *dgap_driver)
6559 {
6560         struct device_driver *driverfs = &dgap_driver->driver;
6561         driver_remove_file(driverfs, &driver_attr_version);
6562         driver_remove_file(driverfs, &driver_attr_boards);
6563         driver_remove_file(driverfs, &driver_attr_maxboards);
6564         driver_remove_file(driverfs, &driver_attr_debug);
6565         driver_remove_file(driverfs, &driver_attr_rawreadok);
6566         driver_remove_file(driverfs, &driver_attr_pollrate);
6567         driver_remove_file(driverfs, &driver_attr_pollcounter);
6568         driver_remove_file(driverfs, &driver_attr_state);
6569 }
6570
6571
6572 #define DGAP_VERIFY_BOARD(p, bd)                        \
6573         if (!p)                                         \
6574                 return (0);                             \
6575                                                         \
6576         bd = dev_get_drvdata(p);                        \
6577         if (!bd || bd->magic != DGAP_BOARD_MAGIC)       \
6578                 return (0);                             \
6579         if (bd->state != BOARD_READY)                   \
6580                 return (0);                             \
6581
6582
6583 static ssize_t dgap_ports_state_show(struct device *p, struct device_attribute *attr, char *buf)
6584 {
6585         struct board_t *bd;
6586         int count = 0;
6587         int i = 0;
6588
6589         DGAP_VERIFY_BOARD(p, bd);
6590
6591         for (i = 0; i < bd->nasync; i++) {
6592                 count += snprintf(buf + count, PAGE_SIZE - count,
6593                         "%d %s\n", bd->channels[i]->ch_portnum,
6594                         bd->channels[i]->ch_open_count ? "Open" : "Closed");
6595         }
6596         return count;
6597 }
6598 static DEVICE_ATTR(ports_state, S_IRUSR, dgap_ports_state_show, NULL);
6599
6600
6601 static ssize_t dgap_ports_baud_show(struct device *p, struct device_attribute *attr, char *buf)
6602 {
6603         struct board_t *bd;
6604         int count = 0;
6605         int i = 0;
6606
6607         DGAP_VERIFY_BOARD(p, bd);
6608
6609         for (i = 0; i < bd->nasync; i++) {
6610                 count +=  snprintf(buf + count, PAGE_SIZE - count,
6611                         "%d %d\n", bd->channels[i]->ch_portnum, bd->channels[i]->ch_baud_info);
6612         }
6613         return count;
6614 }
6615 static DEVICE_ATTR(ports_baud, S_IRUSR, dgap_ports_baud_show, NULL);
6616
6617
6618 static ssize_t dgap_ports_msignals_show(struct device *p, struct device_attribute *attr, char *buf)
6619 {
6620         struct board_t *bd;
6621         int count = 0;
6622         int i = 0;
6623
6624         DGAP_VERIFY_BOARD(p, bd);
6625
6626         for (i = 0; i < bd->nasync; i++) {
6627                 if (bd->channels[i]->ch_open_count) {
6628                         count += snprintf(buf + count, PAGE_SIZE - count,
6629                                 "%d %s %s %s %s %s %s\n", bd->channels[i]->ch_portnum,
6630                                 (bd->channels[i]->ch_mostat & UART_MCR_RTS) ? "RTS" : "",
6631                                 (bd->channels[i]->ch_mistat & UART_MSR_CTS) ? "CTS" : "",
6632                                 (bd->channels[i]->ch_mostat & UART_MCR_DTR) ? "DTR" : "",
6633                                 (bd->channels[i]->ch_mistat & UART_MSR_DSR) ? "DSR" : "",
6634                                 (bd->channels[i]->ch_mistat & UART_MSR_DCD) ? "DCD" : "",
6635                                 (bd->channels[i]->ch_mistat & UART_MSR_RI)  ? "RI"  : "");
6636                 } else {
6637                         count += snprintf(buf + count, PAGE_SIZE - count,
6638                                 "%d\n", bd->channels[i]->ch_portnum);
6639                 }
6640         }
6641         return count;
6642 }
6643 static DEVICE_ATTR(ports_msignals, S_IRUSR, dgap_ports_msignals_show, NULL);
6644
6645
6646 static ssize_t dgap_ports_iflag_show(struct device *p, struct device_attribute *attr, char *buf)
6647 {
6648         struct board_t *bd;
6649         int count = 0;
6650         int i = 0;
6651
6652         DGAP_VERIFY_BOARD(p, bd);
6653
6654         for (i = 0; i < bd->nasync; i++) {
6655                 count += snprintf(buf + count, PAGE_SIZE - count, "%d %x\n",
6656                         bd->channels[i]->ch_portnum, bd->channels[i]->ch_c_iflag);
6657         }
6658         return count;
6659 }
6660 static DEVICE_ATTR(ports_iflag, S_IRUSR, dgap_ports_iflag_show, NULL);
6661
6662
6663 static ssize_t dgap_ports_cflag_show(struct device *p, struct device_attribute *attr, char *buf)
6664 {
6665         struct board_t *bd;
6666         int count = 0;
6667         int i = 0;
6668
6669         DGAP_VERIFY_BOARD(p, bd);
6670
6671         for (i = 0; i < bd->nasync; i++) {
6672                 count += snprintf(buf + count, PAGE_SIZE - count, "%d %x\n",
6673                         bd->channels[i]->ch_portnum, bd->channels[i]->ch_c_cflag);
6674         }
6675         return count;
6676 }
6677 static DEVICE_ATTR(ports_cflag, S_IRUSR, dgap_ports_cflag_show, NULL);
6678
6679
6680 static ssize_t dgap_ports_oflag_show(struct device *p, struct device_attribute *attr, char *buf)
6681 {
6682         struct board_t *bd;
6683         int count = 0;
6684         int i = 0;
6685
6686         DGAP_VERIFY_BOARD(p, bd);
6687
6688         for (i = 0; i < bd->nasync; i++) {
6689                 count += snprintf(buf + count, PAGE_SIZE - count, "%d %x\n",
6690                         bd->channels[i]->ch_portnum, bd->channels[i]->ch_c_oflag);
6691         }
6692         return count;
6693 }
6694 static DEVICE_ATTR(ports_oflag, S_IRUSR, dgap_ports_oflag_show, NULL);
6695
6696
6697 static ssize_t dgap_ports_lflag_show(struct device *p, struct device_attribute *attr, char *buf)
6698 {
6699         struct board_t *bd;
6700         int count = 0;
6701         int i = 0;
6702
6703         DGAP_VERIFY_BOARD(p, bd);
6704
6705         for (i = 0; i < bd->nasync; i++) {
6706                 count += snprintf(buf + count, PAGE_SIZE - count, "%d %x\n",
6707                         bd->channels[i]->ch_portnum, bd->channels[i]->ch_c_lflag);
6708         }
6709         return count;
6710 }
6711 static DEVICE_ATTR(ports_lflag, S_IRUSR, dgap_ports_lflag_show, NULL);
6712
6713
6714 static ssize_t dgap_ports_digi_flag_show(struct device *p, struct device_attribute *attr, char *buf)
6715 {
6716         struct board_t *bd;
6717         int count = 0;
6718         int i = 0;
6719
6720         DGAP_VERIFY_BOARD(p, bd);
6721
6722         for (i = 0; i < bd->nasync; i++) {
6723                 count += snprintf(buf + count, PAGE_SIZE - count, "%d %x\n",
6724                         bd->channels[i]->ch_portnum, bd->channels[i]->ch_digi.digi_flags);
6725         }
6726         return count;
6727 }
6728 static DEVICE_ATTR(ports_digi_flag, S_IRUSR, dgap_ports_digi_flag_show, NULL);
6729
6730
6731 static ssize_t dgap_ports_rxcount_show(struct device *p, struct device_attribute *attr, char *buf)
6732 {
6733         struct board_t *bd;
6734         int count = 0;
6735         int i = 0;
6736
6737         DGAP_VERIFY_BOARD(p, bd);
6738
6739         for (i = 0; i < bd->nasync; i++) {
6740                 count += snprintf(buf + count, PAGE_SIZE - count, "%d %ld\n",
6741                         bd->channels[i]->ch_portnum, bd->channels[i]->ch_rxcount);
6742         }
6743         return count;
6744 }
6745 static DEVICE_ATTR(ports_rxcount, S_IRUSR, dgap_ports_rxcount_show, NULL);
6746
6747
6748 static ssize_t dgap_ports_txcount_show(struct device *p, struct device_attribute *attr, char *buf)
6749 {
6750         struct board_t *bd;
6751         int count = 0;
6752         int i = 0;
6753
6754         DGAP_VERIFY_BOARD(p, bd);
6755
6756         for (i = 0; i < bd->nasync; i++) {
6757                 count += snprintf(buf + count, PAGE_SIZE - count, "%d %ld\n",
6758                         bd->channels[i]->ch_portnum, bd->channels[i]->ch_txcount);
6759         }
6760         return count;
6761 }
6762 static DEVICE_ATTR(ports_txcount, S_IRUSR, dgap_ports_txcount_show, NULL);
6763
6764
6765 /* this function creates the sys files that will export each signal status
6766  * to sysfs each value will be put in a separate filename
6767  */
6768 void dgap_create_ports_sysfiles(struct board_t *bd)
6769 {
6770         int rc = 0;
6771
6772         dev_set_drvdata(&bd->pdev->dev, bd);
6773         rc |= device_create_file(&(bd->pdev->dev), &dev_attr_ports_state);
6774         rc |= device_create_file(&(bd->pdev->dev), &dev_attr_ports_baud);
6775         rc |= device_create_file(&(bd->pdev->dev), &dev_attr_ports_msignals);
6776         rc |= device_create_file(&(bd->pdev->dev), &dev_attr_ports_iflag);
6777         rc |= device_create_file(&(bd->pdev->dev), &dev_attr_ports_cflag);
6778         rc |= device_create_file(&(bd->pdev->dev), &dev_attr_ports_oflag);
6779         rc |= device_create_file(&(bd->pdev->dev), &dev_attr_ports_lflag);
6780         rc |= device_create_file(&(bd->pdev->dev), &dev_attr_ports_digi_flag);
6781         rc |= device_create_file(&(bd->pdev->dev), &dev_attr_ports_rxcount);
6782         rc |= device_create_file(&(bd->pdev->dev), &dev_attr_ports_txcount);
6783         if (rc) {
6784                 printk(KERN_ERR "DGAP: sysfs device_create_file failed!\n");
6785         }
6786 }
6787
6788
6789 /* removes all the sys files created for that port */
6790 void dgap_remove_ports_sysfiles(struct board_t *bd)
6791 {
6792         device_remove_file(&(bd->pdev->dev), &dev_attr_ports_state);
6793         device_remove_file(&(bd->pdev->dev), &dev_attr_ports_baud);
6794         device_remove_file(&(bd->pdev->dev), &dev_attr_ports_msignals);
6795         device_remove_file(&(bd->pdev->dev), &dev_attr_ports_iflag);
6796         device_remove_file(&(bd->pdev->dev), &dev_attr_ports_cflag);
6797         device_remove_file(&(bd->pdev->dev), &dev_attr_ports_oflag);
6798         device_remove_file(&(bd->pdev->dev), &dev_attr_ports_lflag);
6799         device_remove_file(&(bd->pdev->dev), &dev_attr_ports_digi_flag);
6800         device_remove_file(&(bd->pdev->dev), &dev_attr_ports_rxcount);
6801         device_remove_file(&(bd->pdev->dev), &dev_attr_ports_txcount);
6802 }
6803
6804
6805 static ssize_t dgap_tty_state_show(struct device *d, struct device_attribute *attr, char *buf)
6806 {
6807         struct board_t *bd;
6808         struct channel_t *ch;
6809         struct un_t *un;
6810
6811         if (!d)
6812                 return (0);
6813         un = dev_get_drvdata(d);
6814         if (!un || un->magic != DGAP_UNIT_MAGIC)
6815                 return (0);
6816         ch = un->un_ch;
6817         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
6818                 return (0);
6819         bd = ch->ch_bd;
6820         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
6821                 return (0);
6822         if (bd->state != BOARD_READY)
6823                 return (0);
6824
6825         return snprintf(buf, PAGE_SIZE, "%s", un->un_open_count ? "Open" : "Closed");
6826 }
6827 static DEVICE_ATTR(state, S_IRUSR, dgap_tty_state_show, NULL);
6828
6829
6830 static ssize_t dgap_tty_baud_show(struct device *d, struct device_attribute *attr, char *buf)
6831 {
6832         struct board_t *bd;
6833         struct channel_t *ch;
6834         struct un_t *un;
6835
6836         if (!d)
6837                 return (0);
6838         un = dev_get_drvdata(d);
6839         if (!un || un->magic != DGAP_UNIT_MAGIC)
6840                 return (0);
6841         ch = un->un_ch;
6842         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
6843                 return (0);
6844         bd = ch->ch_bd;
6845         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
6846                 return (0);
6847         if (bd->state != BOARD_READY)
6848                 return (0);
6849
6850         return snprintf(buf, PAGE_SIZE, "%d\n", ch->ch_baud_info);
6851 }
6852 static DEVICE_ATTR(baud, S_IRUSR, dgap_tty_baud_show, NULL);
6853
6854
6855 static ssize_t dgap_tty_msignals_show(struct device *d, struct device_attribute *attr, char *buf)
6856 {
6857         struct board_t *bd;
6858         struct channel_t *ch;
6859         struct un_t *un;
6860
6861         if (!d)
6862                 return (0);
6863         un = dev_get_drvdata(d);
6864         if (!un || un->magic != DGAP_UNIT_MAGIC)
6865                 return (0);
6866         ch = un->un_ch;
6867         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
6868                 return (0);
6869         bd = ch->ch_bd;
6870         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
6871                 return (0);
6872         if (bd->state != BOARD_READY)
6873                 return (0);
6874
6875         if (ch->ch_open_count) {
6876                 return snprintf(buf, PAGE_SIZE, "%s %s %s %s %s %s\n",
6877                         (ch->ch_mostat & UART_MCR_RTS) ? "RTS" : "",
6878                         (ch->ch_mistat & UART_MSR_CTS) ? "CTS" : "",
6879                         (ch->ch_mostat & UART_MCR_DTR) ? "DTR" : "",
6880                         (ch->ch_mistat & UART_MSR_DSR) ? "DSR" : "",
6881                         (ch->ch_mistat & UART_MSR_DCD) ? "DCD" : "",
6882                         (ch->ch_mistat & UART_MSR_RI)  ? "RI"  : "");
6883         }
6884         return 0;
6885 }
6886 static DEVICE_ATTR(msignals, S_IRUSR, dgap_tty_msignals_show, NULL);
6887
6888
6889 static ssize_t dgap_tty_iflag_show(struct device *d, struct device_attribute *attr, char *buf)
6890 {
6891         struct board_t *bd;
6892         struct channel_t *ch;
6893         struct un_t *un;
6894
6895         if (!d)
6896                 return (0);
6897         un = dev_get_drvdata(d);
6898         if (!un || un->magic != DGAP_UNIT_MAGIC)
6899                 return (0);
6900         ch = un->un_ch;
6901         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
6902                 return (0);
6903         bd = ch->ch_bd;
6904         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
6905                 return (0);
6906         if (bd->state != BOARD_READY)
6907                 return (0);
6908
6909         return snprintf(buf, PAGE_SIZE, "%x\n", ch->ch_c_iflag);
6910 }
6911 static DEVICE_ATTR(iflag, S_IRUSR, dgap_tty_iflag_show, NULL);
6912
6913
6914 static ssize_t dgap_tty_cflag_show(struct device *d, struct device_attribute *attr, char *buf)
6915 {
6916         struct board_t *bd;
6917         struct channel_t *ch;
6918         struct un_t *un;
6919
6920         if (!d)
6921                 return (0);
6922         un = dev_get_drvdata(d);
6923         if (!un || un->magic != DGAP_UNIT_MAGIC)
6924                 return (0);
6925         ch = un->un_ch;
6926         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
6927                 return (0);
6928         bd = ch->ch_bd;
6929         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
6930                 return (0);
6931         if (bd->state != BOARD_READY)
6932                 return (0);
6933
6934         return snprintf(buf, PAGE_SIZE, "%x\n", ch->ch_c_cflag);
6935 }
6936 static DEVICE_ATTR(cflag, S_IRUSR, dgap_tty_cflag_show, NULL);
6937
6938
6939 static ssize_t dgap_tty_oflag_show(struct device *d, struct device_attribute *attr, char *buf)
6940 {
6941         struct board_t *bd;
6942         struct channel_t *ch;
6943         struct un_t *un;
6944
6945         if (!d)
6946                 return (0);
6947         un = dev_get_drvdata(d);
6948         if (!un || un->magic != DGAP_UNIT_MAGIC)
6949                 return (0);
6950         ch = un->un_ch;
6951         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
6952                 return (0);
6953         bd = ch->ch_bd;
6954         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
6955                 return (0);
6956         if (bd->state != BOARD_READY)
6957                 return (0);
6958
6959         return snprintf(buf, PAGE_SIZE, "%x\n", ch->ch_c_oflag);
6960 }
6961 static DEVICE_ATTR(oflag, S_IRUSR, dgap_tty_oflag_show, NULL);
6962
6963
6964 static ssize_t dgap_tty_lflag_show(struct device *d, struct device_attribute *attr, char *buf)
6965 {
6966         struct board_t *bd;
6967         struct channel_t *ch;
6968         struct un_t *un;
6969
6970         if (!d)
6971                 return (0);
6972         un = dev_get_drvdata(d);
6973         if (!un || un->magic != DGAP_UNIT_MAGIC)
6974                 return (0);
6975         ch = un->un_ch;
6976         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
6977                 return (0);
6978         bd = ch->ch_bd;
6979         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
6980                 return (0);
6981         if (bd->state != BOARD_READY)
6982                 return (0);
6983
6984         return snprintf(buf, PAGE_SIZE, "%x\n", ch->ch_c_lflag);
6985 }
6986 static DEVICE_ATTR(lflag, S_IRUSR, dgap_tty_lflag_show, NULL);
6987
6988
6989 static ssize_t dgap_tty_digi_flag_show(struct device *d, struct device_attribute *attr, char *buf)
6990 {
6991         struct board_t *bd;
6992         struct channel_t *ch;
6993         struct un_t *un;
6994
6995         if (!d)
6996                 return (0);
6997         un = dev_get_drvdata(d);
6998         if (!un || un->magic != DGAP_UNIT_MAGIC)
6999                 return (0);
7000         ch = un->un_ch;
7001         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
7002                 return (0);
7003         bd = ch->ch_bd;
7004         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
7005                 return (0);
7006         if (bd->state != BOARD_READY)
7007                 return (0);
7008
7009         return snprintf(buf, PAGE_SIZE, "%x\n", ch->ch_digi.digi_flags);
7010 }
7011 static DEVICE_ATTR(digi_flag, S_IRUSR, dgap_tty_digi_flag_show, NULL);
7012
7013
7014 static ssize_t dgap_tty_rxcount_show(struct device *d, struct device_attribute *attr, char *buf)
7015 {
7016         struct board_t *bd;
7017         struct channel_t *ch;
7018         struct un_t *un;
7019
7020         if (!d)
7021                 return (0);
7022         un = dev_get_drvdata(d);
7023         if (!un || un->magic != DGAP_UNIT_MAGIC)
7024                 return (0);
7025         ch = un->un_ch;
7026         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
7027                 return (0);
7028         bd = ch->ch_bd;
7029         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
7030                 return (0);
7031         if (bd->state != BOARD_READY)
7032                 return (0);
7033
7034         return snprintf(buf, PAGE_SIZE, "%ld\n", ch->ch_rxcount);
7035 }
7036 static DEVICE_ATTR(rxcount, S_IRUSR, dgap_tty_rxcount_show, NULL);
7037
7038
7039 static ssize_t dgap_tty_txcount_show(struct device *d, struct device_attribute *attr, char *buf)
7040 {
7041         struct board_t *bd;
7042         struct channel_t *ch;
7043         struct un_t *un;
7044
7045         if (!d)
7046                 return (0);
7047         un = dev_get_drvdata(d);
7048         if (!un || un->magic != DGAP_UNIT_MAGIC)
7049                 return (0);
7050         ch = un->un_ch;
7051         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
7052                 return (0);
7053         bd = ch->ch_bd;
7054         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
7055                 return (0);
7056         if (bd->state != BOARD_READY)
7057                 return (0);
7058
7059         return snprintf(buf, PAGE_SIZE, "%ld\n", ch->ch_txcount);
7060 }
7061 static DEVICE_ATTR(txcount, S_IRUSR, dgap_tty_txcount_show, NULL);
7062
7063
7064 static ssize_t dgap_tty_name_show(struct device *d, struct device_attribute *attr, char *buf)
7065 {
7066         struct board_t *bd;
7067         struct channel_t *ch;
7068         struct un_t *un;
7069         int     cn;
7070         int     bn;
7071         struct cnode *cptr = NULL;
7072         int found = FALSE;
7073         int ncount = 0;
7074         int starto = 0;
7075         int i = 0;
7076
7077         if (!d)
7078                 return (0);
7079         un = dev_get_drvdata(d);
7080         if (!un || un->magic != DGAP_UNIT_MAGIC)
7081                 return (0);
7082         ch = un->un_ch;
7083         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
7084                 return (0);
7085         bd = ch->ch_bd;
7086         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
7087                 return (0);
7088         if (bd->state != BOARD_READY)
7089                 return (0);
7090
7091         bn = bd->boardnum;
7092         cn = ch->ch_portnum;
7093
7094         for (cptr = bd->bd_config; cptr; cptr = cptr->next) {
7095
7096                 if ((cptr->type == BNODE) &&
7097                     ((cptr->u.board.type == APORT2_920P) || (cptr->u.board.type == APORT4_920P) ||
7098                      (cptr->u.board.type == APORT8_920P) || (cptr->u.board.type == PAPORT4) ||
7099                      (cptr->u.board.type == PAPORT8))) {
7100
7101                                 found = TRUE;
7102                                 if (cptr->u.board.v_start)
7103                                         starto = cptr->u.board.start;
7104                                 else
7105                                         starto = 1;
7106                 }
7107
7108                 if (cptr->type == TNODE && found == TRUE) {
7109                         char *ptr1;
7110                         if (strstr(cptr->u.ttyname, "tty")) {
7111                                 ptr1 = cptr->u.ttyname;
7112                                 ptr1 += 3;
7113                         }
7114                         else {
7115                                 ptr1 = cptr->u.ttyname;
7116                         }
7117
7118                         for (i = 0; i < dgap_config_get_number_of_ports(bd); i++) {
7119                                 if (cn == i) {
7120                                         return snprintf(buf, PAGE_SIZE, "%s%s%02d\n",
7121                                                 (un->un_type == DGAP_PRINT) ? "pr" : "tty",
7122                                                 ptr1, i + starto);
7123                                 }
7124                         }
7125                 }
7126
7127                 if (cptr->type == CNODE) {
7128
7129                         for (i = 0; i < cptr->u.conc.nport; i++) {
7130                                 if (cn == (i + ncount)) {
7131
7132                                         return snprintf(buf, PAGE_SIZE, "%s%s%02d\n",
7133                                                 (un->un_type == DGAP_PRINT) ? "pr" : "tty",
7134                                                 cptr->u.conc.id,
7135                                                 i + (cptr->u.conc.v_start ? cptr->u.conc.start : 1));
7136                                 }
7137                         }
7138
7139                         ncount += cptr->u.conc.nport;
7140                 }
7141
7142                 if (cptr->type == MNODE) {
7143
7144                         for (i = 0; i < cptr->u.module.nport; i++) {
7145                                 if (cn == (i + ncount)) {
7146                                         return snprintf(buf, PAGE_SIZE, "%s%s%02d\n",
7147                                                 (un->un_type == DGAP_PRINT) ? "pr" : "tty",
7148                                                 cptr->u.module.id,
7149                                                 i + (cptr->u.module.v_start ? cptr->u.module.start : 1));
7150                                 }
7151                         }
7152
7153                         ncount += cptr->u.module.nport;
7154
7155                 }
7156         }
7157
7158         return snprintf(buf, PAGE_SIZE, "%s_dgap_%d_%d\n",
7159                 (un->un_type == DGAP_PRINT) ? "pr" : "tty", bn, cn);
7160
7161 }
7162 static DEVICE_ATTR(custom_name, S_IRUSR, dgap_tty_name_show, NULL);
7163
7164
7165 static struct attribute *dgap_sysfs_tty_entries[] = {
7166         &dev_attr_state.attr,
7167         &dev_attr_baud.attr,
7168         &dev_attr_msignals.attr,
7169         &dev_attr_iflag.attr,
7170         &dev_attr_cflag.attr,
7171         &dev_attr_oflag.attr,
7172         &dev_attr_lflag.attr,
7173         &dev_attr_digi_flag.attr,
7174         &dev_attr_rxcount.attr,
7175         &dev_attr_txcount.attr,
7176         &dev_attr_custom_name.attr,
7177         NULL
7178 };
7179
7180
7181 static struct attribute_group dgap_tty_attribute_group = {
7182         .name = NULL,
7183         .attrs = dgap_sysfs_tty_entries,
7184 };
7185
7186
7187
7188
7189 void dgap_create_tty_sysfs(struct un_t *un, struct device *c)
7190 {
7191         int ret;
7192
7193         ret = sysfs_create_group(&c->kobj, &dgap_tty_attribute_group);
7194         if (ret) {
7195                 printk(KERN_ERR "dgap: failed to create sysfs tty device attributes.\n");
7196                 sysfs_remove_group(&c->kobj, &dgap_tty_attribute_group);
7197                 return;
7198         }
7199
7200         dev_set_drvdata(c, un);
7201
7202 }
7203
7204
7205 void dgap_remove_tty_sysfs(struct device *c)
7206 {
7207         sysfs_remove_group(&c->kobj, &dgap_tty_attribute_group);
7208 }
7209
7210 /*
7211  * Parse a configuration file read into memory as a string.
7212  */
7213 int     dgap_parsefile(char **in, int Remove)
7214 {
7215         struct cnode *p, *brd, *line, *conc;
7216         int     rc;
7217         char    *s = NULL, *s2 = NULL;
7218         int     linecnt = 0;
7219
7220         p = &dgap_head;
7221         brd = line = conc = NULL;
7222
7223         /* perhaps we are adding to an existing list? */
7224         while (p->next != NULL) {
7225                 p = p->next;
7226         }
7227
7228         /* file must start with a BEGIN */
7229         while ( (rc = dgap_gettok(in,p)) != BEGIN ) {
7230                 if (rc == 0) {
7231                         dgap_err("unexpected EOF");
7232                         return(-1);
7233                 }
7234         }
7235
7236         for (; ; ) {
7237                 rc = dgap_gettok(in,p);
7238                 if (rc == 0) {
7239                         dgap_err("unexpected EOF");
7240                         return(-1);
7241                 }
7242
7243                 switch (rc) {
7244                 case 0:
7245                         dgap_err("unexpected end of file");
7246                         return(-1);
7247
7248                 case BEGIN:     /* should only be 1 begin */
7249                         dgap_err("unexpected config_begin\n");
7250                         return(-1);
7251
7252                 case END:
7253                         return(0);
7254
7255                 case BOARD:     /* board info */
7256                         if (dgap_checknode(p))
7257                                 return(-1);
7258                         if ( (p->next = dgap_newnode(BNODE)) == NULL ) {
7259                                 dgap_err("out of memory");
7260                                 return(-1);
7261                         }
7262                         p = p->next;
7263
7264                         p->u.board.status = dgap_savestring("No");
7265                         line = conc = NULL;
7266                         brd = p;
7267                         linecnt = -1;
7268                         break;
7269
7270                 case APORT2_920P:       /* AccelePort_4 */
7271                         if (p->type != BNODE) {
7272                                 dgap_err("unexpected Digi_2r_920 string");
7273                                 return(-1);
7274                         }
7275                         p->u.board.type = APORT2_920P;
7276                         p->u.board.v_type = 1;
7277                         DPR_INIT(("Adding Digi_2r_920 PCI to config...\n"));
7278                         break;
7279
7280                 case APORT4_920P:       /* AccelePort_4 */
7281                         if (p->type != BNODE) {
7282                                 dgap_err("unexpected Digi_4r_920 string");
7283                                 return(-1);
7284                         }
7285                         p->u.board.type = APORT4_920P;
7286                         p->u.board.v_type = 1;
7287                         DPR_INIT(("Adding Digi_4r_920 PCI to config...\n"));
7288                         break;
7289
7290                 case APORT8_920P:       /* AccelePort_8 */
7291                         if (p->type != BNODE) {
7292                                 dgap_err("unexpected Digi_8r_920 string");
7293                                 return(-1);
7294                         }
7295                         p->u.board.type = APORT8_920P;
7296                         p->u.board.v_type = 1;
7297                         DPR_INIT(("Adding Digi_8r_920 PCI to config...\n"));
7298                         break;
7299
7300                 case PAPORT4:   /* AccelePort_4 PCI */
7301                         if (p->type != BNODE) {
7302                                 dgap_err("unexpected Digi_4r(PCI) string");
7303                                 return(-1);
7304                         }
7305                         p->u.board.type = PAPORT4;
7306                         p->u.board.v_type = 1;
7307                         DPR_INIT(("Adding Digi_4r PCI to config...\n"));
7308                         break;
7309
7310                 case PAPORT8:   /* AccelePort_8 PCI */
7311                         if (p->type != BNODE) {
7312                                 dgap_err("unexpected Digi_8r string");
7313                                 return(-1);
7314                         }
7315                         p->u.board.type = PAPORT8;
7316                         p->u.board.v_type = 1;
7317                         DPR_INIT(("Adding Digi_8r PCI to config...\n"));
7318                         break;
7319
7320                 case PCX:       /* PCI C/X */
7321                         if (p->type != BNODE) {
7322                                 dgap_err("unexpected Digi_C/X_(PCI) string");
7323                                 return(-1);
7324                         }
7325                         p->u.board.type = PCX;
7326                         p->u.board.v_type = 1;
7327                         p->u.board.conc1 = 0;
7328                         p->u.board.conc2 = 0;
7329                         p->u.board.module1 = 0;
7330                         p->u.board.module2 = 0;
7331                         DPR_INIT(("Adding PCI C/X to config...\n"));
7332                         break;
7333
7334                 case PEPC:      /* PCI EPC/X */
7335                         if (p->type != BNODE) {
7336                                 dgap_err("unexpected \"Digi_EPC/X_(PCI)\" string");
7337                                 return(-1);
7338                         }
7339                         p->u.board.type = PEPC;
7340                         p->u.board.v_type = 1;
7341                         p->u.board.conc1 = 0;
7342                         p->u.board.conc2 = 0;
7343                         p->u.board.module1 = 0;
7344                         p->u.board.module2 = 0;
7345                         DPR_INIT(("Adding PCI EPC/X to config...\n"));
7346                         break;
7347
7348                 case PPCM:      /* PCI/Xem */
7349                         if (p->type != BNODE) {
7350                                 dgap_err("unexpected PCI/Xem string");
7351                                 return(-1);
7352                         }
7353                         p->u.board.type = PPCM;
7354                         p->u.board.v_type = 1;
7355                         p->u.board.conc1 = 0;
7356                         p->u.board.conc2 = 0;
7357                         DPR_INIT(("Adding PCI XEM to config...\n"));
7358                         break;
7359
7360                 case IO:        /* i/o port */
7361                         if (p->type != BNODE) {
7362                                 dgap_err("IO port only vaild for boards");
7363                                 return(-1);
7364                         }
7365                         s = dgap_getword(in);
7366                         if (s == NULL) {
7367                                 dgap_err("unexpected end of file");
7368                                 return(-1);
7369                         }
7370                         p->u.board.portstr = dgap_savestring(s);
7371                         p->u.board.port = (short)simple_strtol(s, &s2, 0);
7372                         if ((short)strlen(s) > (short)(s2 - s)) {
7373                                 dgap_err("bad number for IO port");
7374                                 return(-1);
7375                         }
7376                         p->u.board.v_port = 1;
7377                         DPR_INIT(("Adding IO (%s) to config...\n", s));
7378                         break;
7379
7380                 case MEM:       /* memory address */
7381                         if (p->type != BNODE) {
7382                                 dgap_err("memory address only vaild for boards");
7383                                 return(-1);
7384                         }
7385                         s = dgap_getword(in);
7386                         if (s == NULL) {
7387                                 dgap_err("unexpected end of file");
7388                                 return(-1);
7389                         }
7390                         p->u.board.addrstr = dgap_savestring(s);
7391                         p->u.board.addr = simple_strtoul(s, &s2, 0);
7392                         if ((int)strlen(s) > (int)(s2 - s)) {
7393                                 dgap_err("bad number for memory address");
7394                                 return(-1);
7395                         }
7396                         p->u.board.v_addr = 1;
7397                         DPR_INIT(("Adding MEM (%s) to config...\n", s));
7398                         break;
7399
7400                 case PCIINFO:   /* pci information */
7401                         if (p->type != BNODE) {
7402                                 dgap_err("memory address only vaild for boards");
7403                                 return(-1);
7404                         }
7405                         s = dgap_getword(in);
7406                         if (s == NULL) {
7407                                 dgap_err("unexpected end of file");
7408                                 return(-1);
7409                         }
7410                         p->u.board.pcibusstr = dgap_savestring(s);
7411                         p->u.board.pcibus = simple_strtoul(s, &s2, 0);
7412                         if ((int)strlen(s) > (int)(s2 - s)) {
7413                                 dgap_err("bad number for pci bus");
7414                                 return(-1);
7415                         }
7416                         p->u.board.v_pcibus = 1;
7417                         s = dgap_getword(in);
7418                         if (s == NULL) {
7419                                 dgap_err("unexpected end of file");
7420                                 return(-1);
7421                         }
7422                         p->u.board.pcislotstr = dgap_savestring(s);
7423                         p->u.board.pcislot = simple_strtoul(s, &s2, 0);
7424                         if ((int)strlen(s) > (int)(s2 - s)) {
7425                                 dgap_err("bad number for pci slot");
7426                                 return(-1);
7427                         }
7428                         p->u.board.v_pcislot = 1;
7429
7430                         DPR_INIT(("Adding PCIINFO (%s %s) to config...\n", p->u.board.pcibusstr,
7431                                 p->u.board.pcislotstr));
7432                         break;
7433
7434                 case METHOD:
7435                         if (p->type != BNODE) {
7436                                 dgap_err("install method only vaild for boards");
7437                                 return(-1);
7438                         }
7439                         s = dgap_getword(in);
7440                         if (s == NULL) {
7441                                 dgap_err("unexpected end of file");
7442                                 return(-1);
7443                         }
7444                         p->u.board.method = dgap_savestring(s);
7445                         p->u.board.v_method = 1;
7446                         DPR_INIT(("Adding METHOD (%s) to config...\n", s));
7447                         break;
7448
7449                 case STATUS:
7450                         if (p->type != BNODE) {
7451                                 dgap_err("config status only vaild for boards");
7452                                 return(-1);
7453                         }
7454                         s = dgap_getword(in);
7455                         if (s == NULL) {
7456                                 dgap_err("unexpected end of file");
7457                                 return(-1);
7458                         }
7459                         p->u.board.status = dgap_savestring(s);
7460                         DPR_INIT(("Adding STATUS (%s) to config...\n", s));
7461                         break;
7462
7463                 case NPORTS:    /* number of ports */
7464                         if (p->type == BNODE) {
7465                                 s = dgap_getword(in);
7466                                 if (s == NULL) {
7467                                         dgap_err("unexpected end of file");
7468                                         return(-1);
7469                                 }
7470                                 p->u.board.nport = (char)simple_strtol(s, &s2, 0);
7471                                 if ((int)strlen(s) > (int)(s2 - s)) {
7472                                         dgap_err("bad number for number of ports");
7473                                         return(-1);
7474                                 }
7475                                 p->u.board.v_nport = 1;
7476                         } else if (p->type == CNODE) {
7477                                 s = dgap_getword(in);
7478                                 if (s == NULL) {
7479                                         dgap_err("unexpected end of file");
7480                                         return(-1);
7481                                 }
7482                                 p->u.conc.nport = (char)simple_strtol(s, &s2, 0);
7483                                 if ((int)strlen(s) > (int)(s2 - s)) {
7484                                         dgap_err("bad number for number of ports");
7485                                         return(-1);
7486                                 }
7487                                 p->u.conc.v_nport = 1;
7488                         } else if (p->type == MNODE) {
7489                                 s = dgap_getword(in);
7490                                 if (s == NULL) {
7491                                         dgap_err("unexpected end of file");
7492                                         return(-1);
7493                                 }
7494                                 p->u.module.nport = (char)simple_strtol(s, &s2, 0);
7495                                 if ((int)strlen(s) > (int)(s2 - s)) {
7496                                         dgap_err("bad number for number of ports");
7497                                         return(-1);
7498                                 }
7499                                 p->u.module.v_nport = 1;
7500                         } else {
7501                                 dgap_err("nports only valid for concentrators or modules");
7502                                 return(-1);
7503                         }
7504                         DPR_INIT(("Adding NPORTS (%s) to config...\n", s));
7505                         break;
7506
7507                 case ID:        /* letter ID used in tty name */
7508                         s = dgap_getword(in);
7509                         if (s == NULL) {
7510                                 dgap_err("unexpected end of file");
7511                                 return(-1);
7512                         }
7513
7514                         p->u.board.status = dgap_savestring(s);
7515
7516                         if (p->type == CNODE) {
7517                                 p->u.conc.id = dgap_savestring(s);
7518                                 p->u.conc.v_id = 1;
7519                         } else if (p->type == MNODE) {
7520                                 p->u.module.id = dgap_savestring(s);
7521                                 p->u.module.v_id = 1;
7522                         } else {
7523                                 dgap_err("id only valid for concentrators or modules");
7524                                 return(-1);
7525                         }
7526                         DPR_INIT(("Adding ID (%s) to config...\n", s));
7527                         break;
7528
7529                 case STARTO:    /* start offset of ID */
7530                         if (p->type == BNODE) {
7531                                 s = dgap_getword(in);
7532                                 if (s == NULL) {
7533                                         dgap_err("unexpected end of file");
7534                                         return(-1);
7535                                 }
7536                                 p->u.board.start = simple_strtol(s, &s2, 0);
7537                                 if ((int)strlen(s) > (int)(s2 - s)) {
7538                                         dgap_err("bad number for start of tty count");
7539                                         return(-1);
7540                                 }
7541                                 p->u.board.v_start = 1;
7542                         } else if (p->type == CNODE) {
7543                                 s = dgap_getword(in);
7544                                 if (s == NULL) {
7545                                         dgap_err("unexpected end of file");
7546                                         return(-1);
7547                                 }
7548                                 p->u.conc.start = simple_strtol(s, &s2, 0);
7549                                 if ((int)strlen(s) > (int)(s2 - s)) {
7550                                         dgap_err("bad number for start of tty count");
7551                                         return(-1);
7552                                 }
7553                                 p->u.conc.v_start = 1;
7554                         } else if (p->type == MNODE) {
7555                                 s = dgap_getword(in);
7556                                 if (s == NULL) {
7557                                         dgap_err("unexpected end of file");
7558                                         return(-1);
7559                                 }
7560                                 p->u.module.start = simple_strtol(s, &s2, 0);
7561                                 if ((int)strlen(s) > (int)(s2 - s)) {
7562                                         dgap_err("bad number for start of tty count");
7563                                         return(-1);
7564                                 }
7565                                 p->u.module.v_start = 1;
7566                         } else {
7567                                 dgap_err("start only valid for concentrators or modules");
7568                                 return(-1);
7569                         }
7570                         DPR_INIT(("Adding START (%s) to config...\n", s));
7571                         break;
7572
7573                 case TTYN:      /* tty name prefix */
7574                         if (dgap_checknode(p))
7575                                 return(-1);
7576                         if ( (p->next = dgap_newnode(TNODE)) == NULL ) {
7577                                 dgap_err("out of memory");
7578                                 return(-1);
7579                         }
7580                         p = p->next;
7581                         if ( (s = dgap_getword(in)) == NULL ) {
7582                                 dgap_err("unexpeced end of file");
7583                                 return(-1);
7584                         }
7585                         if ( (p->u.ttyname = dgap_savestring(s)) == NULL ) {
7586                                 dgap_err("out of memory");
7587                                 return(-1);
7588                         }
7589                         DPR_INIT(("Adding TTY (%s) to config...\n", s));
7590                         break;
7591
7592                 case CU:        /* cu name prefix */
7593                         if (dgap_checknode(p))
7594                                 return(-1);
7595                         if ( (p->next = dgap_newnode(CUNODE)) == NULL ) {
7596                                 dgap_err("out of memory");
7597                                 return(-1);
7598                         }
7599                         p = p->next;
7600                         if ( (s = dgap_getword(in)) == NULL ) {
7601                                 dgap_err("unexpeced end of file");
7602                                 return(-1);
7603                         }
7604                         if ( (p->u.cuname = dgap_savestring(s)) == NULL ) {
7605                                 dgap_err("out of memory");
7606                                 return(-1);
7607                         }
7608                         DPR_INIT(("Adding CU (%s) to config...\n", s));
7609                         break;
7610
7611                 case LINE:      /* line information */
7612                         if (dgap_checknode(p))
7613                                 return(-1);
7614                         if (brd == NULL) {
7615                                 dgap_err("must specify board before line info");
7616                                 return(-1);
7617                         }
7618                         switch (brd->u.board.type) {
7619                         case PPCM:
7620                                 dgap_err("line not vaild for PC/em");
7621                                 return(-1);
7622                         }
7623                         if ( (p->next = dgap_newnode(LNODE)) == NULL ) {
7624                                 dgap_err("out of memory");
7625                                 return(-1);
7626                         }
7627                         p = p->next;
7628                         conc = NULL;
7629                         line = p;
7630                         linecnt++;
7631                         DPR_INIT(("Adding LINE to config...\n"));
7632                         break;
7633
7634                 case CONC:      /* concentrator information */
7635                         if (dgap_checknode(p))
7636                                 return(-1);
7637                         if (line == NULL) {
7638                                 dgap_err("must specify line info before concentrator");
7639                                 return(-1);
7640                         }
7641                         if ( (p->next = dgap_newnode(CNODE)) == NULL ) {
7642                                 dgap_err("out of memory");
7643                                 return(-1);
7644                         }
7645                         p = p->next;
7646                         conc = p;
7647                         if (linecnt)
7648                                 brd->u.board.conc2++;
7649                         else
7650                                 brd->u.board.conc1++;
7651
7652                         DPR_INIT(("Adding CONC to config...\n"));
7653                         break;
7654
7655                 case CX:        /* c/x type concentrator */
7656                         if (p->type != CNODE) {
7657                                 dgap_err("cx only valid for concentrators");
7658                                 return(-1);
7659                         }
7660                         p->u.conc.type = CX;
7661                         p->u.conc.v_type = 1;
7662                         DPR_INIT(("Adding CX to config...\n"));
7663                         break;
7664
7665                 case EPC:       /* epc type concentrator */
7666                         if (p->type != CNODE) {
7667                                 dgap_err("cx only valid for concentrators");
7668                                 return(-1);
7669                         }
7670                         p->u.conc.type = EPC;
7671                         p->u.conc.v_type = 1;
7672                         DPR_INIT(("Adding EPC to config...\n"));
7673                         break;
7674
7675                 case MOD:       /* EBI module */
7676                         if (dgap_checknode(p))
7677                                 return(-1);
7678                         if (brd == NULL) {
7679                                 dgap_err("must specify board info before EBI modules");
7680                                 return(-1);
7681                         }
7682                         switch (brd->u.board.type) {
7683                         case PPCM:
7684                                 linecnt = 0;
7685                                 break;
7686                         default:
7687                                 if (conc == NULL) {
7688                                         dgap_err("must specify concentrator info before EBI module");
7689                                         return(-1);
7690                                 }
7691                         }
7692                         if ( (p->next = dgap_newnode(MNODE)) == NULL ) {
7693                                 dgap_err("out of memory");
7694                                 return(-1);
7695                         }
7696                         p = p->next;
7697                         if (linecnt)
7698                                 brd->u.board.module2++;
7699                         else
7700                                 brd->u.board.module1++;
7701
7702                         DPR_INIT(("Adding MOD to config...\n"));
7703                         break;
7704
7705                 case PORTS:     /* ports type EBI module */
7706                         if (p->type != MNODE) {
7707                                 dgap_err("ports only valid for EBI modules");
7708                                 return(-1);
7709                         }
7710                         p->u.module.type = PORTS;
7711                         p->u.module.v_type = 1;
7712                         DPR_INIT(("Adding PORTS to config...\n"));
7713                         break;
7714
7715                 case MODEM:     /* ports type EBI module */
7716                         if (p->type != MNODE) {
7717                                 dgap_err("modem only valid for modem modules");
7718                                 return(-1);
7719                         }
7720                         p->u.module.type = MODEM;
7721                         p->u.module.v_type = 1;
7722                         DPR_INIT(("Adding MODEM to config...\n"));
7723                         break;
7724
7725                 case CABLE:
7726                         if (p->type == LNODE) {
7727                                 if ((s = dgap_getword(in)) == NULL) {
7728                                         dgap_err("unexpected end of file");
7729                                         return(-1);
7730                                 }
7731                                 p->u.line.cable = dgap_savestring(s);
7732                                 p->u.line.v_cable = 1;
7733                         }
7734                         DPR_INIT(("Adding CABLE (%s) to config...\n", s));
7735                         break;
7736
7737                 case SPEED:     /* sync line speed indication */
7738                         if (p->type == LNODE) {
7739                                 s = dgap_getword(in);
7740                                 if (s == NULL) {
7741                                         dgap_err("unexpected end of file");
7742                                         return(-1);
7743                                 }
7744                                 p->u.line.speed = (char)simple_strtol(s, &s2, 0);
7745                                 if ((short)strlen(s) > (short)(s2 - s)) {
7746                                         dgap_err("bad number for line speed");
7747                                         return(-1);
7748                                 }
7749                                 p->u.line.v_speed = 1;
7750                         } else if (p->type == CNODE) {
7751                                 s = dgap_getword(in);
7752                                 if (s == NULL) {
7753                                         dgap_err("unexpected end of file");
7754                                         return(-1);
7755                                 }
7756                                 p->u.conc.speed = (char)simple_strtol(s, &s2, 0);
7757                                 if ((short)strlen(s) > (short)(s2 - s)) {
7758                                         dgap_err("bad number for line speed");
7759                                         return(-1);
7760                                 }
7761                                 p->u.conc.v_speed = 1;
7762                         } else {
7763                                 dgap_err("speed valid only for lines or concentrators.");
7764                                 return(-1);
7765                         }
7766                         DPR_INIT(("Adding SPEED (%s) to config...\n", s));
7767                         break;
7768
7769                 case CONNECT:
7770                         if (p->type == CNODE) {
7771                                 if ((s = dgap_getword(in)) == NULL) {
7772                                         dgap_err("unexpected end of file");
7773                                         return(-1);
7774                                 }
7775                                 p->u.conc.connect = dgap_savestring(s);
7776                                 p->u.conc.v_connect = 1;
7777                         }
7778                         DPR_INIT(("Adding CONNECT (%s) to config...\n", s));
7779                         break;
7780                 case PRINT:     /* transparent print name prefix */
7781                         if (dgap_checknode(p))
7782                                 return(-1);
7783                         if ( (p->next = dgap_newnode(PNODE)) == NULL ) {
7784                                 dgap_err("out of memory");
7785                                 return(-1);
7786                         }
7787                         p = p->next;
7788                         if ( (s = dgap_getword(in)) == NULL ) {
7789                                 dgap_err("unexpeced end of file");
7790                                 return(-1);
7791                         }
7792                         if ( (p->u.printname = dgap_savestring(s)) == NULL ) {
7793                                 dgap_err("out of memory");
7794                                 return(-1);
7795                         }
7796                         DPR_INIT(("Adding PRINT (%s) to config...\n", s));
7797                         break;
7798
7799                 case CMAJOR:    /* major number */
7800                         if (dgap_checknode(p))
7801                                 return(-1);
7802                         if ( (p->next = dgap_newnode(JNODE)) == NULL ) {
7803                                 dgap_err("out of memory");
7804                                 return(-1);
7805                         }
7806                         p = p->next;
7807                         s = dgap_getword(in);
7808                         if (s == NULL) {
7809                                 dgap_err("unexpected end of file");
7810                                 return(-1);
7811                         }
7812                         p->u.majornumber = simple_strtol(s, &s2, 0);
7813                         if ((int)strlen(s) > (int)(s2 - s)) {
7814                                 dgap_err("bad number for major number");
7815                                 return(-1);
7816                         }
7817                         DPR_INIT(("Adding CMAJOR (%s) to config...\n", s));
7818                         break;
7819
7820                 case ALTPIN:    /* altpin setting */
7821                         if (dgap_checknode(p))
7822                                 return(-1);
7823                         if ( (p->next = dgap_newnode(ANODE)) == NULL ) {
7824                                 dgap_err("out of memory");
7825                                 return(-1);
7826                         }
7827                         p = p->next;
7828                         s = dgap_getword(in);
7829                         if (s == NULL) {
7830                                 dgap_err("unexpected end of file");
7831                                 return(-1);
7832                         }
7833                         p->u.altpin = simple_strtol(s, &s2, 0);
7834                         if ((int)strlen(s) > (int)(s2 - s)) {
7835                                 dgap_err("bad number for altpin");
7836                                 return(-1);
7837                         }
7838                         DPR_INIT(("Adding ALTPIN (%s) to config...\n", s));
7839                         break;
7840
7841                 case USEINTR:           /* enable interrupt setting */
7842                         if (dgap_checknode(p))
7843                                 return(-1);
7844                         if ( (p->next = dgap_newnode(INTRNODE)) == NULL ) {
7845                                 dgap_err("out of memory");
7846                                 return(-1);
7847                         }
7848                         p = p->next;
7849                         s = dgap_getword(in);
7850                         if (s == NULL) {
7851                                 dgap_err("unexpected end of file");
7852                                 return(-1);
7853                         }
7854                         p->u.useintr = simple_strtol(s, &s2, 0);
7855                         if ((int)strlen(s) > (int)(s2 - s)) {
7856                                 dgap_err("bad number for useintr");
7857                                 return(-1);
7858                         }
7859                         DPR_INIT(("Adding USEINTR (%s) to config...\n", s));
7860                         break;
7861
7862                 case TTSIZ:     /* size of tty structure */
7863                         if (dgap_checknode(p))
7864                                 return(-1);
7865                         if ( (p->next = dgap_newnode(TSNODE)) == NULL ) {
7866                                 dgap_err("out of memory");
7867                                 return(-1);
7868                         }
7869                         p = p->next;
7870                         s = dgap_getword(in);
7871                         if (s == NULL) {
7872                                 dgap_err("unexpected end of file");
7873                                 return(-1);
7874                         }
7875                         p->u.ttysize = simple_strtol(s, &s2, 0);
7876                         if ((int)strlen(s) > (int)(s2 - s)) {
7877                                 dgap_err("bad number for ttysize");
7878                                 return(-1);
7879                         }
7880                         DPR_INIT(("Adding TTSIZ (%s) to config...\n", s));
7881                         break;
7882
7883                 case CHSIZ:     /* channel structure size */
7884                         if (dgap_checknode(p))
7885                                 return(-1);
7886                         if ( (p->next = dgap_newnode(CSNODE)) == NULL ) {
7887                                 dgap_err("out of memory");
7888                                 return(-1);
7889                         }
7890                         p = p->next;
7891                         s = dgap_getword(in);
7892                         if (s == NULL) {
7893                                 dgap_err("unexpected end of file");
7894                                 return(-1);
7895                         }
7896                         p->u.chsize = simple_strtol(s, &s2, 0);
7897                         if ((int)strlen(s) > (int)(s2 - s)) {
7898                                 dgap_err("bad number for chsize");
7899                                 return(-1);
7900                         }
7901                         DPR_INIT(("Adding CHSIZE (%s) to config...\n", s));
7902                         break;
7903
7904                 case BSSIZ:     /* board structure size */
7905                         if (dgap_checknode(p))
7906                                 return(-1);
7907                         if ( (p->next = dgap_newnode(BSNODE)) == NULL ) {
7908                                 dgap_err("out of memory");
7909                                 return(-1);
7910                         }
7911                         p = p->next;
7912                         s = dgap_getword(in);
7913                         if (s == NULL) {
7914                                 dgap_err("unexpected end of file");
7915                                 return(-1);
7916                         }
7917                         p->u.bssize = simple_strtol(s, &s2, 0);
7918                         if ((int)strlen(s) > (int)(s2 - s)) {
7919                                 dgap_err("bad number for bssize");
7920                                 return(-1);
7921                         }
7922                         DPR_INIT(("Adding BSSIZ (%s) to config...\n", s));
7923                         break;
7924
7925                 case UNTSIZ:    /* sched structure size */
7926                         if (dgap_checknode(p))
7927                                 return(-1);
7928                         if ( (p->next = dgap_newnode(USNODE)) == NULL ) {
7929                                 dgap_err("out of memory");
7930                                 return(-1);
7931                         }
7932                         p = p->next;
7933                         s = dgap_getword(in);
7934                         if (s == NULL) {
7935                                 dgap_err("unexpected end of file");
7936                                 return(-1);
7937                         }
7938                         p->u.unsize = simple_strtol(s, &s2, 0);
7939                         if ((int)strlen(s) > (int)(s2 - s)) {
7940                                 dgap_err("bad number for schedsize");
7941                                 return(-1);
7942                         }
7943                         DPR_INIT(("Adding UNTSIZ (%s) to config...\n", s));
7944                         break;
7945
7946                 case F2SIZ:     /* f2200 structure size */
7947                         if (dgap_checknode(p))
7948                                 return(-1);
7949                         if ( (p->next = dgap_newnode(FSNODE)) == NULL ) {
7950                                 dgap_err("out of memory");
7951                                 return(-1);
7952                         }
7953                         p = p->next;
7954                         s = dgap_getword(in);
7955                         if (s == NULL) {
7956                                 dgap_err("unexpected end of file");
7957                                 return(-1);
7958                         }
7959                         p->u.f2size = simple_strtol(s, &s2, 0);
7960                         if ((int)strlen(s) > (int)(s2 - s)) {
7961                                 dgap_err("bad number for f2200size");
7962                                 return(-1);
7963                         }
7964                         DPR_INIT(("Adding F2SIZ (%s) to config...\n", s));
7965                         break;
7966
7967                 case VPSIZ:     /* vpix structure size */
7968                         if (dgap_checknode(p))
7969                                 return(-1);
7970                         if ( (p->next = dgap_newnode(VSNODE)) == NULL ) {
7971                                 dgap_err("out of memory");
7972                                 return(-1);
7973                         }
7974                         p = p->next;
7975                         s = dgap_getword(in);
7976                         if (s == NULL) {
7977                                 dgap_err("unexpected end of file");
7978                                 return(-1);
7979                         }
7980                         p->u.vpixsize = simple_strtol(s, &s2, 0);
7981                         if ((int)strlen(s) > (int)(s2 - s)) {
7982                                 dgap_err("bad number for vpixsize");
7983                                 return(-1);
7984                         }
7985                         DPR_INIT(("Adding VPSIZ (%s) to config...\n", s));
7986                         break;
7987                 }
7988         }
7989 }
7990
7991
7992 /*
7993  * dgap_sindex: much like index(), but it looks for a match of any character in
7994  * the group, and returns that position.  If the first character is a ^, then
7995  * this will match the first occurrence not in that group.
7996  */
7997 static char *dgap_sindex (char *string, char *group)
7998 {
7999         char    *ptr;
8000
8001         if (!string || !group)
8002                 return (char *) NULL;
8003
8004         if (*group == '^') {
8005                 group++;
8006                 for (; *string; string++) {
8007                         for (ptr = group; *ptr; ptr++) {
8008                                 if (*ptr == *string)
8009                                         break;
8010                         }
8011                         if (*ptr == '\0')
8012                                 return string;
8013                 }
8014         }
8015         else {
8016                 for (; *string; string++) {
8017                         for (ptr = group; *ptr; ptr++) {
8018                                 if (*ptr == *string)
8019                                         return string;
8020                         }
8021                 }
8022         }
8023
8024         return (char *) NULL;
8025 }
8026
8027
8028 /*
8029  * Get a token from the input file; return 0 if end of file is reached
8030  */
8031 static int dgap_gettok(char **in, struct cnode *p)
8032 {
8033         char    *w;
8034         struct toklist *t;
8035
8036         if (strstr(dgap_cword, "boar")) {
8037                 w = dgap_getword(in);
8038                 snprintf(dgap_cword, MAXCWORD, "%s", w);
8039                 for (t = dgap_tlist; t->token != 0; t++) {
8040                         if ( !strcmp(w, t->string)) {
8041                                 return(t->token);
8042                         }
8043                 }
8044                 dgap_err("board !!type not specified");
8045                 return(1);
8046         }
8047         else {
8048                 while ( (w = dgap_getword(in)) != NULL ) {
8049                         snprintf(dgap_cword, MAXCWORD, "%s", w);
8050                         for (t = dgap_tlist; t->token != 0; t++) {
8051                                 if ( !strcmp(w, t->string) )
8052                                         return(t->token);
8053                         }
8054                 }
8055                 return(0);
8056         }
8057 }
8058
8059
8060 /*
8061  * get a word from the input stream, also keep track of current line number.
8062  * words are separated by whitespace.
8063  */
8064 static char *dgap_getword(char **in)
8065 {
8066         char *ret_ptr = *in;
8067
8068         char *ptr = dgap_sindex(*in, " \t\n");
8069
8070         /* If no word found, return null */
8071         if (!ptr)
8072                 return NULL;
8073
8074         /* Mark new location for our buffer */
8075         *ptr = '\0';
8076         *in = ptr + 1;
8077
8078         /* Eat any extra spaces/tabs/newlines that might be present */
8079         while (*in && **in && ((**in == ' ') || (**in == '\t') || (**in == '\n'))) {
8080                 **in = '\0';
8081                 *in = *in + 1;
8082         }
8083
8084         return ret_ptr;
8085 }
8086
8087
8088 /*
8089  * print an error message, giving the line number in the file where
8090  * the error occurred.
8091  */
8092 static void dgap_err(char *s)
8093 {
8094         printk("DGAP: parse: %s\n", s);
8095 }
8096
8097
8098 /*
8099  * allocate a new configuration node of type t
8100  */
8101 static struct cnode *dgap_newnode(int t)
8102 {
8103         struct cnode *n;
8104
8105         n = kmalloc(sizeof(struct cnode), GFP_ATOMIC);
8106         if (n != NULL) {
8107                 memset((char *)n, 0, sizeof(struct cnode));
8108                 n->type = t;
8109         }
8110         return(n);
8111 }
8112
8113
8114 /*
8115  * dgap_checknode: see if all the necessary info has been supplied for a node
8116  * before creating the next node.
8117  */
8118 static int dgap_checknode(struct cnode *p)
8119 {
8120         switch (p->type) {
8121         case BNODE:
8122                 if (p->u.board.v_type == 0) {
8123                         dgap_err("board type !not specified");
8124                         return(1);
8125                 }
8126
8127                 return(0);
8128
8129         case LNODE:
8130                 if (p->u.line.v_speed == 0) {
8131                         dgap_err("line speed not specified");
8132                         return(1);
8133                 }
8134                 return(0);
8135
8136         case CNODE:
8137                 if (p->u.conc.v_type == 0) {
8138                         dgap_err("concentrator type not specified");
8139                         return(1);
8140                 }
8141                 if (p->u.conc.v_speed == 0) {
8142                         dgap_err("concentrator line speed not specified");
8143                         return(1);
8144                 }
8145                 if (p->u.conc.v_nport == 0) {
8146                         dgap_err("number of ports on concentrator not specified");
8147                         return(1);
8148                 }
8149                 if (p->u.conc.v_id == 0) {
8150                         dgap_err("concentrator id letter not specified");
8151                         return(1);
8152                 }
8153                 return(0);
8154
8155         case MNODE:
8156                 if (p->u.module.v_type == 0) {
8157                         dgap_err("EBI module type not specified");
8158                         return(1);
8159                 }
8160                 if (p->u.module.v_nport == 0) {
8161                         dgap_err("number of ports on EBI module not specified");
8162                         return(1);
8163                 }
8164                 if (p->u.module.v_id == 0) {
8165                         dgap_err("EBI module id letter not specified");
8166                         return(1);
8167                 }
8168                 return(0);
8169         }
8170         return(0);
8171 }
8172
8173 /*
8174  * save a string somewhere
8175  */
8176 static char     *dgap_savestring(char *s)
8177 {
8178         char    *p;
8179         if ( (p = kmalloc(strlen(s) + 1, GFP_ATOMIC) ) != NULL) {
8180                 strcpy(p, s);
8181         }
8182         return(p);
8183 }
8184
8185
8186 /*
8187  * Given a board pointer, returns whether we should use interrupts or not.
8188  */
8189 uint dgap_config_get_useintr(struct board_t *bd)
8190 {
8191         struct cnode *p = NULL;
8192
8193         if (!bd)
8194                 return(0);
8195
8196         for (p = bd->bd_config; p; p = p->next) {
8197                 switch (p->type) {
8198                 case INTRNODE:
8199                         /*
8200                          * check for pcxr types.
8201                          */
8202                         return p->u.useintr;
8203                 default:
8204                         break;
8205                 }
8206         }
8207
8208         /* If not found, then don't turn on interrupts. */
8209         return 0;
8210 }
8211
8212
8213 /*
8214  * Given a board pointer, returns whether we turn on altpin or not.
8215  */
8216 uint dgap_config_get_altpin(struct board_t *bd)
8217 {
8218         struct cnode *p = NULL;
8219
8220         if (!bd)
8221                 return(0);
8222
8223         for (p = bd->bd_config; p; p = p->next) {
8224                 switch (p->type) {
8225                 case ANODE:
8226                         /*
8227                          * check for pcxr types.
8228                          */
8229                         return p->u.altpin;
8230                 default:
8231                         break;
8232                 }
8233         }
8234
8235         /* If not found, then don't turn on interrupts. */
8236         return 0;
8237 }
8238
8239
8240
8241 /*
8242  * Given a specific type of board, if found, detached link and
8243  * returns the first occurrence in the list.
8244  */
8245 struct cnode *dgap_find_config(int type, int bus, int slot)
8246 {
8247         struct cnode *p, *prev = NULL, *prev2 = NULL, *found = NULL;
8248
8249         p = &dgap_head;
8250
8251         while (p->next != NULL) {
8252                 prev = p;
8253                 p = p->next;
8254
8255                 if (p->type == BNODE) {
8256
8257                         if (p->u.board.type == type) {
8258
8259                                 if (p->u.board.v_pcibus && p->u.board.pcibus != bus) {
8260                                         DPR(("Found matching board, but wrong bus position. System says bus %d, we want bus %ld\n",
8261                                                 bus, p->u.board.pcibus));
8262                                         continue;
8263                                 }
8264                                 if (p->u.board.v_pcislot && p->u.board.pcislot != slot) {
8265                                         DPR_INIT(("Found matching board, but wrong slot position. System says slot %d, we want slot %ld\n",
8266                                                 slot, p->u.board.pcislot));
8267                                         continue;
8268                                 }
8269
8270                                 DPR_INIT(("Matched type in config file\n"));
8271
8272                                 found = p;
8273                                 /*
8274                                  * Keep walking thru the list till we find the next board.
8275                                  */
8276                                 while (p->next != NULL) {
8277                                         prev2 = p;
8278                                         p = p->next;
8279                                         if (p->type == BNODE) {
8280
8281                                                 /*
8282                                                  * Mark the end of our 1 board chain of configs.
8283                                                  */
8284                                                 prev2->next = NULL;
8285
8286                                                 /*
8287                                                  * Link the "next" board to the previous board,
8288                                                  * effectively "unlinking" our board from the main config.
8289                                                  */
8290                                                 prev->next = p;
8291
8292                                                 return found;
8293                                         }
8294                                 }
8295                                 /*
8296                                  * It must be the last board in the list.
8297                                  */
8298                                 prev->next = NULL;
8299                                 return found;
8300                         }
8301                 }
8302         }
8303         return NULL;
8304 }
8305
8306 /*
8307  * Given a board pointer, walks the config link, counting up
8308  * all ports user specified should be on the board.
8309  * (This does NOT mean they are all actually present right now tho)
8310  */
8311 uint dgap_config_get_number_of_ports(struct board_t *bd)
8312 {
8313         int count = 0;
8314         struct cnode *p = NULL;
8315
8316         if (!bd)
8317                 return(0);
8318
8319         for (p = bd->bd_config; p; p = p->next) {
8320
8321                 switch (p->type) {
8322                 case BNODE:
8323                         /*
8324                          * check for pcxr types.
8325                          */
8326                         if (p->u.board.type > EPCFE)
8327                                 count += p->u.board.nport;
8328                         break;
8329                 case CNODE:
8330                         count += p->u.conc.nport;
8331                         break;
8332                 case MNODE:
8333                         count += p->u.module.nport;
8334                         break;
8335                 }
8336         }
8337         return (count);
8338 }
8339
8340 char *dgap_create_config_string(struct board_t *bd, char *string)
8341 {
8342         char *ptr = string;
8343         struct cnode *p = NULL;
8344         struct cnode *q = NULL;
8345         int speed;
8346
8347         if (!bd) {
8348                 *ptr = 0xff;
8349                 return string;
8350         }
8351
8352         for (p = bd->bd_config; p; p = p->next) {
8353
8354                 switch (p->type) {
8355                 case LNODE:
8356                         *ptr = '\0';
8357                         ptr++;
8358                         *ptr = p->u.line.speed;
8359                         ptr++;
8360                         break;
8361                 case CNODE:
8362                         /*
8363                          * Because the EPC/con concentrators can have EM modules
8364                          * hanging off of them, we have to walk ahead in the list
8365                          * and keep adding the number of ports on each EM to the config.
8366                          * UGH!
8367                          */
8368                         speed = p->u.conc.speed;
8369                         q = p->next;
8370                         if ((q != NULL) && (q->type == MNODE) ) {
8371                                 *ptr = (p->u.conc.nport + 0x80);
8372                                 ptr++;
8373                                 p = q;
8374                                 while ((q->next != NULL) && (q->next->type) == MNODE) {
8375                                         *ptr = (q->u.module.nport + 0x80);
8376                                         ptr++;
8377                                         p = q;
8378                                         q = q->next;
8379                                 }
8380                                 *ptr = q->u.module.nport;
8381                                 ptr++;
8382                         } else {
8383                                 *ptr = p->u.conc.nport;
8384                                 ptr++;
8385                         }
8386
8387                         *ptr = speed;
8388                         ptr++;
8389                         break;
8390                 }
8391         }
8392
8393         *ptr = 0xff;
8394         return string;
8395 }
8396
8397
8398
8399 char *dgap_get_config_letters(struct board_t *bd, char *string)
8400 {
8401         int found = FALSE;
8402         char *ptr = string;
8403         struct cnode *cptr = NULL;
8404         int len = 0;
8405         int left = MAXTTYNAMELEN;
8406
8407         if (!bd) {
8408                 return "<NULL>";
8409         }
8410
8411         for (cptr = bd->bd_config; cptr; cptr = cptr->next) {
8412
8413                 if ((cptr->type == BNODE) &&
8414                      ((cptr->u.board.type == APORT2_920P) || (cptr->u.board.type == APORT4_920P) ||
8415                      (cptr->u.board.type == APORT8_920P) || (cptr->u.board.type == PAPORT4) ||
8416                      (cptr->u.board.type == PAPORT8))) {
8417
8418                         found = TRUE;
8419                 }
8420
8421                 if (cptr->type == TNODE && found == TRUE) {
8422                         char *ptr1;
8423                         if (strstr(cptr->u.ttyname, "tty")) {
8424                                 ptr1 = cptr->u.ttyname;
8425                                 ptr1 += 3;
8426                         }
8427                         else {
8428                                 ptr1 = cptr->u.ttyname;
8429                         }
8430                         if (ptr1) {
8431                                 len = snprintf(ptr, left, "%s", ptr1);
8432                                 left -= len;
8433                                 ptr  += len;
8434                                 if (left <= 0)
8435                                         break;
8436                         }
8437                 }
8438
8439                 if (cptr->type == CNODE) {
8440                         if (cptr->u.conc.id) {
8441                                 len = snprintf(ptr, left, "%s", cptr->u.conc.id);
8442                                 left -= len;
8443                                 ptr  += len;
8444                                 if (left <= 0)
8445                                         break;
8446                         }
8447                 }
8448
8449                 if (cptr->type == MNODE) {
8450                         if (cptr->u.module.id) {
8451                                 len = snprintf(ptr, left, "%s", cptr->u.module.id);
8452                                 left -= len;
8453                                 ptr  += len;
8454                                 if (left <= 0)
8455                                         break;
8456                         }
8457                 }
8458         }
8459
8460         return string;
8461 }