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