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