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