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