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