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