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