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