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