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