staging: dgap: Fix BUG in processing config file
[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 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",  0, 0 },
322         { "dgap/dgap.conf", "dgap/cxpbios.bin", "dgap/cxpfep.bin", 0, 1 },
323         { "dgap/dgap.conf", "dgap/cxpbios.bin", "dgap/cxpfep.bin", 0, 2 },
324         { "dgap/dgap.conf", "dgap/pcibios.bin", "dgap/pcifep.bin", 0, 3 },
325         { "dgap/dgap.conf", "dgap/xrbios.bin",  "dgap/xrfep.bin",  0, 4 },
326         { "dgap/dgap.conf", "dgap/xrbios.bin",  "dgap/xrfep.bin",  0, 5 },
327         { "dgap/dgap.conf", "dgap/xrbios.bin",  "dgap/xrfep.bin",  0, 6 },
328         { "dgap/dgap.conf", "dgap/xrbios.bin",  "dgap/xrfep.bin",  0, 7 },
329         { "dgap/dgap.conf", "dgap/xrbios.bin",  "dgap/xrfep.bin",  0, 8 },
330         { "dgap/dgap.conf", "dgap/xrbios.bin",  "dgap/xrfep.bin",  0, 9 },
331         { "dgap/dgap.conf", "dgap/xrbios.bin",  "dgap/xrfep.bin",  0, 10 },
332         { "dgap/dgap.conf", "dgap/xrbios.bin",  "dgap/xrfep.bin",  0, 11 },
333         { "dgap/dgap.conf", "dgap/xrbios.bin",  "dgap/xrfep.bin",  0, 12 },
334         { "dgap/dgap.conf", "dgap/xrbios.bin",  "dgap/xrfep.bin",  0, 13 },
335         { "dgap/dgap.conf", "dgap/sxbios.bin",  "dgap/sxfep.bin",  0, 14 },
336         {0,}
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 *vaddr;
1304         u8 modem = 0;
1305         struct channel_t *ch;
1306         struct bs_t *bs;
1307         struct cm_t *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 *) ((ulong) vaddr + CHANBUF);
1369         cm = (struct cm_t *) ((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     *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, (char *) 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     *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 *bs = NULL;
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 *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 *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 *bs = NULL;
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 *bs = NULL;
2745         char *vaddr = NULL;
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 *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 *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 *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 *config, *xconfig;
4410                 int i = 0;
4411
4412                 xconfig = dgap_create_config_string(brd, string);
4413
4414                 /* Write string to board memory */
4415                 config = addr + CONFIG;
4416                 for (; i < CONFIGSIZE; i++, config++, xconfig++) {
4417                         writeb(*xconfig, config);
4418                         if ((*xconfig & 0xff) == 0xff)
4419                                 break;
4420                 }
4421         }
4422
4423         writel(0xbfc01004, (addr + 0xc34));
4424         writel(0x3, (addr + 0xc30));
4425
4426 }
4427
4428 /*
4429  * Waits for the FEP to report thats its ready for us to use.
4430  */
4431 static int dgap_test_fep(struct board_t *brd)
4432 {
4433         u8 *addr;
4434         u16 word;
4435         u16 err1;
4436         u16 err2;
4437
4438         if (!brd || (brd->magic != DGAP_BOARD_MAGIC) || !brd->re_map_membase)
4439                 return -EINVAL;
4440
4441         addr = brd->re_map_membase;
4442         word = readw(addr + FEPSTAT);
4443
4444         /*
4445          * It can take 2-3 seconds for the FEP to
4446          * be up and running. Give it 5 secs.
4447          */
4448         brd->wait_for_fep = 0;
4449         while (brd->wait_for_fep < 500) {
4450                 /* Check to see if FEP is up and running now. */
4451                 if (word == *(u16 *) "OS") {
4452                         /*
4453                          * Check to see if the board can support FEP5+ commands.
4454                         */
4455                         word = readw(addr + FEP5_PLUS);
4456                         if (word == *(u16 *) "5A")
4457                                 brd->bd_flags |= BD_FEP5PLUS;
4458
4459                         return 0;
4460                 }
4461                 msleep_interruptible(10);
4462                 brd->wait_for_fep++;
4463                 word = readw(addr + FEPSTAT);
4464         }
4465
4466         /* Gave up on board after too long of time taken */
4467         err1 = readw(addr + SEQUENCE);
4468         err2 = readw(addr + ERROR);
4469         pr_warn("dgap: FEPOS for %s not functioning.  Error #(%x,%x).\n",
4470                 brd->name, err1, err2);
4471         brd->state = BOARD_FAILED;
4472         brd->dpastatus = BD_NOFEP;
4473
4474         return -EIO;
4475 }
4476
4477 /*
4478  * Physically forces the FEP5 card to reset itself.
4479  */
4480 static void dgap_do_reset_board(struct board_t *brd)
4481 {
4482         u8 check;
4483         u32 check1;
4484         u32 check2;
4485         int i = 0;
4486
4487         if (!brd || (brd->magic != DGAP_BOARD_MAGIC) ||
4488             !brd->re_map_membase || !brd->re_map_port)
4489                 return;
4490
4491         /* FEPRST does not vary among supported boards */
4492         writeb(FEPRST, brd->re_map_port);
4493
4494         for (i = 0; i <= 1000; i++) {
4495                 check = readb(brd->re_map_port) & 0xe;
4496                 if (check == FEPRST)
4497                         break;
4498                 udelay(10);
4499
4500         }
4501         if (i > 1000) {
4502                 pr_warn("dgap: Board not resetting...  Failing board.\n");
4503                 brd->state = BOARD_FAILED;
4504                 brd->dpastatus = BD_NOFEP;
4505                 return;
4506         }
4507
4508         /*
4509          * Make sure there really is memory out there.
4510          */
4511         writel(0xa55a3cc3, (brd->re_map_membase + LOWMEM));
4512         writel(0x5aa5c33c, (brd->re_map_membase + HIGHMEM));
4513         check1 = readl(brd->re_map_membase + LOWMEM);
4514         check2 = readl(brd->re_map_membase + HIGHMEM);
4515
4516         if ((check1 != 0xa55a3cc3) || (check2 != 0x5aa5c33c)) {
4517                 pr_warn("dgap: No memory at %p for board.\n",
4518                         brd->re_map_membase);
4519                 brd->state = BOARD_FAILED;
4520                 brd->dpastatus = BD_NOFEP;
4521                 return;
4522         }
4523
4524 }
4525
4526 #ifdef DIGI_CONCENTRATORS_SUPPORTED
4527 /*
4528  * Sends a concentrator image into the FEP5 board.
4529  */
4530 static void dgap_do_conc_load(struct board_t *brd, u8 *uaddr, int len)
4531 {
4532         char *vaddr;
4533         u16 offset = 0;
4534         struct downld_t *to_dp;
4535
4536         if (!brd || (brd->magic != DGAP_BOARD_MAGIC) || !brd->re_map_membase)
4537                 return;
4538
4539         vaddr = brd->re_map_membase;
4540
4541         offset = readw((u16 *) (vaddr + DOWNREQ));
4542         to_dp = (struct downld_t *) (vaddr + (int) offset);
4543         memcpy_toio(to_dp, uaddr, len);
4544
4545         /* Tell card we have data for it */
4546         writew(0, vaddr + (DOWNREQ));
4547
4548         brd->conc_dl_status = NO_PENDING_CONCENTRATOR_REQUESTS;
4549 }
4550 #endif
4551
4552 #define EXPANSION_ROM_SIZE      (64 * 1024)
4553 #define FEP5_ROM_MAGIC          (0xFEFFFFFF)
4554
4555 static void dgap_get_vpd(struct board_t *brd)
4556 {
4557         u32 magic;
4558         u32 base_offset;
4559         u16 rom_offset;
4560         u16 vpd_offset;
4561         u16 image_length;
4562         u16 i;
4563         u8 byte1;
4564         u8 byte2;
4565
4566         /*
4567          * Poke the magic number at the PCI Rom Address location.
4568          * If VPD is supported, the value read from that address
4569          * will be non-zero.
4570          */
4571         magic = FEP5_ROM_MAGIC;
4572         pci_write_config_dword(brd->pdev, PCI_ROM_ADDRESS, magic);
4573         pci_read_config_dword(brd->pdev, PCI_ROM_ADDRESS, &magic);
4574
4575         /* VPD not supported, bail */
4576         if (!magic)
4577                 return;
4578
4579         /*
4580          * To get to the OTPROM memory, we have to send the boards base
4581          * address or'ed with 1 into the PCI Rom Address location.
4582          */
4583         magic = brd->membase | 0x01;
4584         pci_write_config_dword(brd->pdev, PCI_ROM_ADDRESS, magic);
4585         pci_read_config_dword(brd->pdev, PCI_ROM_ADDRESS, &magic);
4586
4587         byte1 = readb(brd->re_map_membase);
4588         byte2 = readb(brd->re_map_membase + 1);
4589
4590         /*
4591          * If the board correctly swapped to the OTPROM memory,
4592          * the first 2 bytes (header) should be 0x55, 0xAA
4593          */
4594         if (byte1 == 0x55 && byte2 == 0xAA) {
4595
4596                 base_offset = 0;
4597
4598                 /*
4599                  * We have to run through all the OTPROM memory looking
4600                  * for the VPD offset.
4601                  */
4602                 while (base_offset <= EXPANSION_ROM_SIZE) {
4603
4604                         /*
4605                          * Lots of magic numbers here.
4606                          *
4607                          * The VPD offset is located inside the ROM Data
4608                          * Structure.
4609                          *
4610                          * We also have to remember the length of each
4611                          * ROM Data Structure, so we can "hop" to the next
4612                          * entry if the VPD isn't in the current
4613                          * ROM Data Structure.
4614                          */
4615                         rom_offset = readw(brd->re_map_membase +
4616                                                 base_offset + 0x18);
4617                         image_length = readw(brd->re_map_membase +
4618                                                 rom_offset + 0x10) * 512;
4619                         vpd_offset = readw(brd->re_map_membase +
4620                                                 rom_offset + 0x08);
4621
4622                         /* Found the VPD entry */
4623                         if (vpd_offset)
4624                                 break;
4625
4626                         /* We didn't find a VPD entry, go to next ROM entry. */
4627                         base_offset += image_length;
4628
4629                         byte1 = readb(brd->re_map_membase + base_offset);
4630                         byte2 = readb(brd->re_map_membase + base_offset + 1);
4631
4632                         /*
4633                          * If the new ROM offset doesn't have 0x55, 0xAA
4634                          * as its header, we have run out of ROM.
4635                          */
4636                         if (byte1 != 0x55 || byte2 != 0xAA)
4637                                 break;
4638                 }
4639
4640                 /*
4641                  * If we have a VPD offset, then mark the board
4642                  * as having a valid VPD, and copy VPDSIZE (512) bytes of
4643                  * that VPD to the buffer we have in our board structure.
4644                  */
4645                 if (vpd_offset) {
4646                         brd->bd_flags |= BD_HAS_VPD;
4647                         for (i = 0; i < VPDSIZE; i++) {
4648                                 brd->vpd[i] = readb(brd->re_map_membase +
4649                                                         vpd_offset + i);
4650                         }
4651                 }
4652         }
4653
4654         /*
4655          * We MUST poke the magic number at the PCI Rom Address location again.
4656          * This makes the card report the regular board memory back to us,
4657          * rather than the OTPROM memory.
4658          */
4659         magic = FEP5_ROM_MAGIC;
4660         pci_write_config_dword(brd->pdev, PCI_ROM_ADDRESS, magic);
4661 }
4662
4663 /*
4664  * Our board poller function.
4665  */
4666 static void dgap_poll_tasklet(unsigned long data)
4667 {
4668         struct board_t *bd = (struct board_t *) data;
4669         ulong  lock_flags;
4670         char *vaddr;
4671         u16 head, tail;
4672
4673         if (!bd || (bd->magic != DGAP_BOARD_MAGIC))
4674                 return;
4675
4676         if (bd->inhibit_poller)
4677                 return;
4678
4679         spin_lock_irqsave(&bd->bd_lock, lock_flags);
4680
4681         vaddr = bd->re_map_membase;
4682
4683         /*
4684          * If board is ready, parse deeper to see if there is anything to do.
4685          */
4686         if (bd->state == BOARD_READY) {
4687
4688                 struct ev_t *eaddr = NULL;
4689
4690                 if (!bd->re_map_membase) {
4691                         spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
4692                         return;
4693                 }
4694                 if (!bd->re_map_port) {
4695                         spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
4696                         return;
4697                 }
4698
4699                 if (!bd->nasync)
4700                         goto out;
4701
4702                 eaddr = (struct ev_t *) (vaddr + EVBUF);
4703
4704                 /* Get our head and tail */
4705                 head = readw(&(eaddr->ev_head));
4706                 tail = readw(&(eaddr->ev_tail));
4707
4708                 /*
4709                  * If there is an event pending. Go service it.
4710                  */
4711                 if (head != tail) {
4712                         spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
4713                         dgap_event(bd);
4714                         spin_lock_irqsave(&bd->bd_lock, lock_flags);
4715                 }
4716
4717 out:
4718                 /*
4719                  * If board is doing interrupts, ACK the interrupt.
4720                  */
4721                 if (bd && bd->intr_running)
4722                         readb(bd->re_map_port + 2);
4723
4724                 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
4725                 return;
4726         }
4727
4728         spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
4729 }
4730
4731 /*=======================================================================
4732  *
4733  *      dgap_cmdb - Sends a 2 byte command to the FEP.
4734  *
4735  *              ch      - Pointer to channel structure.
4736  *              cmd     - Command to be sent.
4737  *              byte1   - Integer containing first byte to be sent.
4738  *              byte2   - Integer containing second byte to be sent.
4739  *              ncmds   - Wait until ncmds or fewer cmds are left
4740  *                        in the cmd buffer before returning.
4741  *
4742  *=======================================================================*/
4743 static void dgap_cmdb(struct channel_t *ch, u8 cmd, u8 byte1,
4744                         u8 byte2, uint ncmds)
4745 {
4746         char            *vaddr = NULL;
4747         struct cm_t     *cm_addr = NULL;
4748         uint            count;
4749         uint            n;
4750         u16             head;
4751         u16             tail;
4752
4753         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
4754                 return;
4755
4756         /*
4757          * Check if board is still alive.
4758          */
4759         if (ch->ch_bd->state == BOARD_FAILED)
4760                 return;
4761
4762         /*
4763          * Make sure the pointers are in range before
4764          * writing to the FEP memory.
4765          */
4766         vaddr = ch->ch_bd->re_map_membase;
4767
4768         if (!vaddr)
4769                 return;
4770
4771         cm_addr = (struct cm_t *) (vaddr + CMDBUF);
4772         head = readw(&(cm_addr->cm_head));
4773
4774         /*
4775          * Forget it if pointers out of range.
4776          */
4777         if (head >= (CMDMAX - CMDSTART) || (head & 03)) {
4778                 ch->ch_bd->state = BOARD_FAILED;
4779                 return;
4780         }
4781
4782         /*
4783          * Put the data in the circular command buffer.
4784          */
4785         writeb(cmd, (char *) (vaddr + head + CMDSTART + 0));
4786         writeb((u8) ch->ch_portnum, (char *) (vaddr + head + CMDSTART + 1));
4787         writeb(byte1, (char *) (vaddr + head + CMDSTART + 2));
4788         writeb(byte2, (char *) (vaddr + head + CMDSTART + 3));
4789
4790         head = (head + 4) & (CMDMAX - CMDSTART - 4);
4791
4792         writew(head, &(cm_addr->cm_head));
4793
4794         /*
4795          * Wait if necessary before updating the head
4796          * pointer to limit the number of outstanding
4797          * commands to the FEP.   If the time spent waiting
4798          * is outlandish, declare the FEP dead.
4799          */
4800         for (count = dgap_count ;;) {
4801
4802                 head = readw(&(cm_addr->cm_head));
4803                 tail = readw(&(cm_addr->cm_tail));
4804
4805                 n = (head - tail) & (CMDMAX - CMDSTART - 4);
4806
4807                 if (n <= ncmds * sizeof(struct cm_t))
4808                         break;
4809
4810                 if (--count == 0) {
4811                         ch->ch_bd->state = BOARD_FAILED;
4812                         return;
4813                 }
4814                 udelay(10);
4815         }
4816 }
4817
4818 /*=======================================================================
4819  *
4820  *      dgap_cmdw - Sends a 1 word command to the FEP.
4821  *
4822  *              ch      - Pointer to channel structure.
4823  *              cmd     - Command to be sent.
4824  *              word    - Integer containing word to be sent.
4825  *              ncmds   - Wait until ncmds or fewer cmds are left
4826  *                        in the cmd buffer before returning.
4827  *
4828  *=======================================================================*/
4829 static void dgap_cmdw(struct channel_t *ch, u8 cmd, u16 word, uint ncmds)
4830 {
4831         char            *vaddr = NULL;
4832         struct cm_t     *cm_addr = NULL;
4833         uint            count;
4834         uint            n;
4835         u16             head;
4836         u16             tail;
4837
4838         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
4839                 return;
4840
4841         /*
4842          * Check if board is still alive.
4843          */
4844         if (ch->ch_bd->state == BOARD_FAILED)
4845                 return;
4846
4847         /*
4848          * Make sure the pointers are in range before
4849          * writing to the FEP memory.
4850          */
4851         vaddr = ch->ch_bd->re_map_membase;
4852         if (!vaddr)
4853                 return;
4854
4855         cm_addr = (struct cm_t *) (vaddr + CMDBUF);
4856         head = readw(&(cm_addr->cm_head));
4857
4858         /*
4859          * Forget it if pointers out of range.
4860          */
4861         if (head >= (CMDMAX - CMDSTART) || (head & 03)) {
4862                 ch->ch_bd->state = BOARD_FAILED;
4863                 return;
4864         }
4865
4866         /*
4867          * Put the data in the circular command buffer.
4868          */
4869         writeb(cmd, (char *) (vaddr + head + CMDSTART + 0));
4870         writeb((u8) ch->ch_portnum, (char *) (vaddr + head + CMDSTART + 1));
4871         writew((u16) word, (char *) (vaddr + head + CMDSTART + 2));
4872
4873         head = (head + 4) & (CMDMAX - CMDSTART - 4);
4874
4875         writew(head, &(cm_addr->cm_head));
4876
4877         /*
4878          * Wait if necessary before updating the head
4879          * pointer to limit the number of outstanding
4880          * commands to the FEP.   If the time spent waiting
4881          * is outlandish, declare the FEP dead.
4882          */
4883         for (count = dgap_count ;;) {
4884
4885                 head = readw(&(cm_addr->cm_head));
4886                 tail = readw(&(cm_addr->cm_tail));
4887
4888                 n = (head - tail) & (CMDMAX - CMDSTART - 4);
4889
4890                 if (n <= ncmds * sizeof(struct cm_t))
4891                         break;
4892
4893                 if (--count == 0) {
4894                         ch->ch_bd->state = BOARD_FAILED;
4895                         return;
4896                 }
4897                 udelay(10);
4898         }
4899 }
4900
4901 /*=======================================================================
4902  *
4903  *      dgap_cmdw_ext - Sends a extended word command to the FEP.
4904  *
4905  *              ch      - Pointer to channel structure.
4906  *              cmd     - Command to be sent.
4907  *              word    - Integer containing word to be sent.
4908  *              ncmds   - Wait until ncmds or fewer cmds are left
4909  *                        in the cmd buffer before returning.
4910  *
4911  *=======================================================================*/
4912 static void dgap_cmdw_ext(struct channel_t *ch, u16 cmd, u16 word, uint ncmds)
4913 {
4914         char            *vaddr = NULL;
4915         struct cm_t     *cm_addr = NULL;
4916         uint            count;
4917         uint            n;
4918         u16             head;
4919         u16             tail;
4920
4921         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
4922                 return;
4923
4924         /*
4925          * Check if board is still alive.
4926          */
4927         if (ch->ch_bd->state == BOARD_FAILED)
4928                 return;
4929
4930         /*
4931          * Make sure the pointers are in range before
4932          * writing to the FEP memory.
4933          */
4934         vaddr = ch->ch_bd->re_map_membase;
4935         if (!vaddr)
4936                 return;
4937
4938         cm_addr = (struct cm_t *) (vaddr + CMDBUF);
4939         head = readw(&(cm_addr->cm_head));
4940
4941         /*
4942          * Forget it if pointers out of range.
4943          */
4944         if (head >= (CMDMAX - CMDSTART) || (head & 03)) {
4945                 ch->ch_bd->state = BOARD_FAILED;
4946                 return;
4947         }
4948
4949         /*
4950          * Put the data in the circular command buffer.
4951          */
4952
4953         /* Write an FF to tell the FEP that we want an extended command */
4954         writeb((u8) 0xff, (char *) (vaddr + head + CMDSTART + 0));
4955
4956         writeb((u8) ch->ch_portnum, (u8 *) (vaddr + head + CMDSTART + 1));
4957         writew((u16) cmd, (char *) (vaddr + head + CMDSTART + 2));
4958
4959         /*
4960          * If the second part of the command won't fit,
4961          * put it at the beginning of the circular buffer.
4962          */
4963         if (((head + 4) >= ((CMDMAX - CMDSTART)) || (head & 03)))
4964                 writew((u16) word, (char *) (vaddr + CMDSTART));
4965         else
4966                 writew((u16) word, (char *) (vaddr + head + CMDSTART + 4));
4967
4968         head = (head + 8) & (CMDMAX - CMDSTART - 4);
4969
4970         writew(head, &(cm_addr->cm_head));
4971
4972         /*
4973          * Wait if necessary before updating the head
4974          * pointer to limit the number of outstanding
4975          * commands to the FEP.   If the time spent waiting
4976          * is outlandish, declare the FEP dead.
4977          */
4978         for (count = dgap_count ;;) {
4979
4980                 head = readw(&(cm_addr->cm_head));
4981                 tail = readw(&(cm_addr->cm_tail));
4982
4983                 n = (head - tail) & (CMDMAX - CMDSTART - 4);
4984
4985                 if (n <= ncmds * sizeof(struct cm_t))
4986                         break;
4987
4988                 if (--count == 0) {
4989                         ch->ch_bd->state = BOARD_FAILED;
4990                         return;
4991                 }
4992                 udelay(10);
4993         }
4994 }
4995
4996 /*=======================================================================
4997  *
4998  *      dgap_wmove - Write data to FEP buffer.
4999  *
5000  *              ch      - Pointer to channel structure.
5001  *              buf     - Poiter to characters to be moved.
5002  *              cnt     - Number of characters to move.
5003  *
5004  *=======================================================================*/
5005 static void dgap_wmove(struct channel_t *ch, char *buf, uint cnt)
5006 {
5007         int    n;
5008         char   *taddr;
5009         struct bs_t    *bs;
5010         u16    head;
5011
5012         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
5013                 return;
5014
5015         /*
5016          * Check parameters.
5017          */
5018         bs   = ch->ch_bs;
5019         head = readw(&(bs->tx_head));
5020
5021         /*
5022          * If pointers are out of range, just return.
5023          */
5024         if ((cnt > ch->ch_tsize) ||
5025             (unsigned)(head - ch->ch_tstart) >= ch->ch_tsize)
5026                 return;
5027
5028         /*
5029          * If the write wraps over the top of the circular buffer,
5030          * move the portion up to the wrap point, and reset the
5031          * pointers to the bottom.
5032          */
5033         n = ch->ch_tstart + ch->ch_tsize - head;
5034
5035         if (cnt >= n) {
5036                 cnt -= n;
5037                 taddr = ch->ch_taddr + head;
5038                 memcpy_toio(taddr, buf, n);
5039                 head = ch->ch_tstart;
5040                 buf += n;
5041         }
5042
5043         /*
5044          * Move rest of data.
5045          */
5046         taddr = ch->ch_taddr + head;
5047         n = cnt;
5048         memcpy_toio(taddr, buf, n);
5049         head += cnt;
5050
5051         writew(head, &(bs->tx_head));
5052 }
5053
5054 /*
5055  * Retrives the current custom baud rate from FEP memory,
5056  * and returns it back to the user.
5057  * Returns 0 on error.
5058  */
5059 static uint dgap_get_custom_baud(struct channel_t *ch)
5060 {
5061         u8 *vaddr;
5062         ulong offset = 0;
5063         uint value = 0;
5064
5065         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
5066                 return 0;
5067
5068         if (!ch->ch_bd || ch->ch_bd->magic != DGAP_BOARD_MAGIC)
5069                 return 0;
5070
5071         if (!(ch->ch_bd->bd_flags & BD_FEP5PLUS))
5072                 return 0;
5073
5074         vaddr = ch->ch_bd->re_map_membase;
5075
5076         if (!vaddr)
5077                 return 0;
5078
5079         /*
5080          * Go get from fep mem, what the fep
5081          * believes the custom baud rate is.
5082          */
5083         offset = ((((*(unsigned short *)(vaddr + ECS_SEG)) << 4) +
5084                 (ch->ch_portnum * 0x28) + LINE_SPEED));
5085
5086         value = readw(vaddr + offset);
5087         return value;
5088 }
5089
5090 /*
5091  * Calls the firmware to reset this channel.
5092  */
5093 static void dgap_firmware_reset_port(struct channel_t *ch)
5094 {
5095         dgap_cmdb(ch, CHRESET, 0, 0, 0);
5096
5097         /*
5098          * Now that the channel is reset, we need to make sure
5099          * all the current settings get reapplied to the port
5100          * in the firmware.
5101          *
5102          * So we will set the driver's cache of firmware
5103          * settings all to 0, and then call param.
5104          */
5105         ch->ch_fepiflag = 0;
5106         ch->ch_fepcflag = 0;
5107         ch->ch_fepoflag = 0;
5108         ch->ch_fepstartc = 0;
5109         ch->ch_fepstopc = 0;
5110         ch->ch_fepastartc = 0;
5111         ch->ch_fepastopc = 0;
5112         ch->ch_mostat = 0;
5113         ch->ch_hflow = 0;
5114 }
5115
5116 /*=======================================================================
5117  *
5118  *      dgap_param - Set Digi parameters.
5119  *
5120  *              struct tty_struct *     - TTY for port.
5121  *
5122  *=======================================================================*/
5123 static int dgap_param(struct tty_struct *tty)
5124 {
5125         struct ktermios *ts;
5126         struct board_t *bd;
5127         struct channel_t *ch;
5128         struct bs_t   *bs;
5129         struct un_t   *un;
5130         u16     head;
5131         u16     cflag;
5132         u16     iflag;
5133         u8      mval;
5134         u8      hflow;
5135
5136         if (!tty || tty->magic != TTY_MAGIC)
5137                 return -ENXIO;
5138
5139         un = (struct un_t *) tty->driver_data;
5140         if (!un || un->magic != DGAP_UNIT_MAGIC)
5141                 return -ENXIO;
5142
5143         ch = un->un_ch;
5144         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
5145                 return -ENXIO;
5146
5147         bd = ch->ch_bd;
5148         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
5149                 return -ENXIO;
5150
5151         bs = ch->ch_bs;
5152         if (!bs)
5153                 return -ENXIO;
5154
5155         ts = &tty->termios;
5156
5157         /*
5158          * If baud rate is zero, flush queues, and set mval to drop DTR.
5159          */
5160         if ((ch->ch_c_cflag & (CBAUD)) == 0) {
5161
5162                 /* flush rx */
5163                 head = readw(&(ch->ch_bs->rx_head));
5164                 writew(head, &(ch->ch_bs->rx_tail));
5165
5166                 /* flush tx */
5167                 head = readw(&(ch->ch_bs->tx_head));
5168                 writew(head, &(ch->ch_bs->tx_tail));
5169
5170                 ch->ch_flags |= (CH_BAUD0);
5171
5172                 /* Drop RTS and DTR */
5173                 ch->ch_mval &= ~(D_RTS(ch)|D_DTR(ch));
5174                 mval = D_DTR(ch) | D_RTS(ch);
5175                 ch->ch_baud_info = 0;
5176
5177         } else if (ch->ch_custom_speed && (bd->bd_flags & BD_FEP5PLUS)) {
5178                 /*
5179                  * Tell the fep to do the command
5180                  */
5181
5182                 dgap_cmdw_ext(ch, 0xff01, ch->ch_custom_speed, 0);
5183
5184                 /*
5185                  * Now go get from fep mem, what the fep
5186                  * believes the custom baud rate is.
5187                  */
5188                 ch->ch_custom_speed = dgap_get_custom_baud(ch);
5189                 ch->ch_baud_info = ch->ch_custom_speed;
5190
5191                 /* Handle transition from B0 */
5192                 if (ch->ch_flags & CH_BAUD0) {
5193                         ch->ch_flags &= ~(CH_BAUD0);
5194                         ch->ch_mval |= (D_RTS(ch)|D_DTR(ch));
5195                 }
5196                 mval = D_DTR(ch) | D_RTS(ch);
5197
5198         } else {
5199                 /*
5200                  * Set baud rate, character size, and parity.
5201                  */
5202
5203
5204                 int iindex = 0;
5205                 int jindex = 0;
5206                 int baud = 0;
5207
5208                 ulong bauds[4][16] = {
5209                         { /* slowbaud */
5210                                 0,      50,     75,     110,
5211                                 134,    150,    200,    300,
5212                                 600,    1200,   1800,   2400,
5213                                 4800,   9600,   19200,  38400 },
5214                         { /* slowbaud & CBAUDEX */
5215                                 0,      57600,  115200, 230400,
5216                                 460800, 150,    200,    921600,
5217                                 600,    1200,   1800,   2400,
5218                                 4800,   9600,   19200,  38400 },
5219                         { /* fastbaud */
5220                                 0,      57600,  76800,  115200,
5221                                 14400,  57600,  230400, 76800,
5222                                 115200, 230400, 28800,  460800,
5223                                 921600, 9600,   19200,  38400 },
5224                         { /* fastbaud & CBAUDEX */
5225                                 0,      57600,  115200, 230400,
5226                                 460800, 150,    200,    921600,
5227                                 600,    1200,   1800,   2400,
5228                                 4800,   9600,   19200,  38400 }
5229                 };
5230
5231                 /*
5232                  * Only use the TXPrint baud rate if the
5233                  * terminal unit is NOT open
5234                  */
5235                 if (!(ch->ch_tun.un_flags & UN_ISOPEN) &&
5236                      (un->un_type == DGAP_PRINT))
5237                         baud = C_BAUD(ch->ch_pun.un_tty) & 0xff;
5238                 else
5239                         baud = C_BAUD(ch->ch_tun.un_tty) & 0xff;
5240
5241                 if (ch->ch_c_cflag & CBAUDEX)
5242                         iindex = 1;
5243
5244                 if (ch->ch_digi.digi_flags & DIGI_FAST)
5245                         iindex += 2;
5246
5247                 jindex = baud;
5248
5249                 if ((iindex >= 0) && (iindex < 4) &&
5250                     (jindex >= 0) && (jindex < 16))
5251                         baud = bauds[iindex][jindex];
5252                 else
5253                         baud = 0;
5254
5255                 if (baud == 0)
5256                         baud = 9600;
5257
5258                 ch->ch_baud_info = baud;
5259
5260                 /*
5261                  * CBAUD has bit position 0x1000 set these days to
5262                  * indicate Linux baud rate remap.
5263                  * We use a different bit assignment for high speed.
5264                  * Clear this bit out while grabbing the parts of
5265                  * "cflag" we want.
5266                  */
5267                 cflag = ch->ch_c_cflag & ((CBAUD ^ CBAUDEX) | PARODD | PARENB |
5268                                                    CSTOPB | CSIZE);
5269
5270                 /*
5271                  * HUPCL bit is used by FEP to indicate fast baud
5272                  * table is to be used.
5273                  */
5274                 if ((ch->ch_digi.digi_flags & DIGI_FAST) ||
5275                     (ch->ch_c_cflag & CBAUDEX))
5276                         cflag |= HUPCL;
5277
5278                 if ((ch->ch_c_cflag & CBAUDEX) &&
5279                     !(ch->ch_digi.digi_flags & DIGI_FAST)) {
5280                         /*
5281                          * The below code is trying to guarantee that only
5282                          * baud rates 115200, 230400, 460800, 921600 are
5283                          * remapped. We use exclusive or  because the various
5284                          * baud rates share common bit positions and therefore
5285                          * can't be tested for easily.
5286                          */
5287                         tcflag_t tcflag = (ch->ch_c_cflag & CBAUD) | CBAUDEX;
5288                         int baudpart = 0;
5289
5290                         /*
5291                          * Map high speed requests to index
5292                          * into FEP's baud table
5293                          */
5294                         switch (tcflag) {
5295                         case B57600:
5296                                 baudpart = 1;
5297                                 break;
5298 #ifdef B76800
5299                         case B76800:
5300                                 baudpart = 2;
5301                                 break;
5302 #endif
5303                         case B115200:
5304                                 baudpart = 3;
5305                                 break;
5306                         case B230400:
5307                                 baudpart = 9;
5308                                 break;
5309                         case B460800:
5310                                 baudpart = 11;
5311                                 break;
5312 #ifdef B921600
5313                         case B921600:
5314                                 baudpart = 12;
5315                                 break;
5316 #endif
5317                         default:
5318                                 baudpart = 0;
5319                         }
5320
5321                         if (baudpart)
5322                                 cflag = (cflag & ~(CBAUD | CBAUDEX)) | baudpart;
5323                 }
5324
5325                 cflag &= 0xffff;
5326
5327                 if (cflag != ch->ch_fepcflag) {
5328                         ch->ch_fepcflag = (u16) (cflag & 0xffff);
5329
5330                         /*
5331                          * Okay to have channel and board
5332                          * locks held calling this
5333                          */
5334                         dgap_cmdw(ch, SCFLAG, (u16) cflag, 0);
5335                 }
5336
5337                 /* Handle transition from B0 */
5338                 if (ch->ch_flags & CH_BAUD0) {
5339                         ch->ch_flags &= ~(CH_BAUD0);
5340                         ch->ch_mval |= (D_RTS(ch)|D_DTR(ch));
5341                 }
5342                 mval = D_DTR(ch) | D_RTS(ch);
5343         }
5344
5345         /*
5346          * Get input flags.
5347          */
5348         iflag = ch->ch_c_iflag & (IGNBRK | BRKINT | IGNPAR | PARMRK |
5349                                   INPCK | ISTRIP | IXON | IXANY | IXOFF);
5350
5351         if ((ch->ch_startc == _POSIX_VDISABLE) ||
5352             (ch->ch_stopc == _POSIX_VDISABLE)) {
5353                 iflag &= ~(IXON | IXOFF);
5354                 ch->ch_c_iflag &= ~(IXON | IXOFF);
5355         }
5356
5357         /*
5358          * Only the IBM Xr card can switch between
5359          * 232 and 422 modes on the fly
5360          */
5361         if (bd->device == PCI_DEV_XR_IBM_DID) {
5362                 if (ch->ch_digi.digi_flags & DIGI_422)
5363                         dgap_cmdb(ch, SCOMMODE, MODE_422, 0, 0);
5364                 else
5365                         dgap_cmdb(ch, SCOMMODE, MODE_232, 0, 0);
5366         }
5367
5368         if (ch->ch_digi.digi_flags & DIGI_ALTPIN)
5369                 iflag |= IALTPIN;
5370
5371         if (iflag != ch->ch_fepiflag) {
5372                 ch->ch_fepiflag = iflag;
5373
5374                 /* Okay to have channel and board locks held calling this */
5375                 dgap_cmdw(ch, SIFLAG, (u16) ch->ch_fepiflag, 0);
5376         }
5377
5378         /*
5379          * Select hardware handshaking.
5380          */
5381         hflow = 0;
5382
5383         if (ch->ch_c_cflag & CRTSCTS)
5384                 hflow |= (D_RTS(ch) | D_CTS(ch));
5385         if (ch->ch_digi.digi_flags & RTSPACE)
5386                 hflow |= D_RTS(ch);
5387         if (ch->ch_digi.digi_flags & DTRPACE)
5388                 hflow |= D_DTR(ch);
5389         if (ch->ch_digi.digi_flags & CTSPACE)
5390                 hflow |= D_CTS(ch);
5391         if (ch->ch_digi.digi_flags & DSRPACE)
5392                 hflow |= D_DSR(ch);
5393         if (ch->ch_digi.digi_flags & DCDPACE)
5394                 hflow |= D_CD(ch);
5395
5396         if (hflow != ch->ch_hflow) {
5397                 ch->ch_hflow = hflow;
5398
5399                 /* Okay to have channel and board locks held calling this */
5400                 dgap_cmdb(ch, SHFLOW, (u8) hflow, 0xff, 0);
5401         }
5402
5403
5404         /*
5405          * Set RTS and/or DTR Toggle if needed,
5406          * but only if product is FEP5+ based.
5407          */
5408         if (bd->bd_flags & BD_FEP5PLUS) {
5409                 u16 hflow2 = 0;
5410                 if (ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE)
5411                         hflow2 |= (D_RTS(ch));
5412                 if (ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE)
5413                         hflow2 |= (D_DTR(ch));
5414
5415                 dgap_cmdw_ext(ch, 0xff03, hflow2, 0);
5416         }
5417
5418         /*
5419          * Set modem control lines.
5420          */
5421
5422         mval ^= ch->ch_mforce & (mval ^ ch->ch_mval);
5423
5424         if (ch->ch_mostat ^ mval) {
5425                 ch->ch_mostat = mval;
5426
5427                 /* Okay to have channel and board locks held calling this */
5428                 dgap_cmdb(ch, SMODEM, (u8) mval, D_RTS(ch)|D_DTR(ch), 0);
5429         }
5430
5431         /*
5432          * Read modem signals, and then call carrier function.
5433          */
5434         ch->ch_mistat = readb(&(bs->m_stat));
5435         dgap_carrier(ch);
5436
5437         /*
5438          * Set the start and stop characters.
5439          */
5440         if (ch->ch_startc != ch->ch_fepstartc ||
5441             ch->ch_stopc != ch->ch_fepstopc) {
5442                 ch->ch_fepstartc = ch->ch_startc;
5443                 ch->ch_fepstopc =  ch->ch_stopc;
5444
5445                 /* Okay to have channel and board locks held calling this */
5446                 dgap_cmdb(ch, SFLOWC, ch->ch_fepstartc, ch->ch_fepstopc, 0);
5447         }
5448
5449         /*
5450          * Set the Auxiliary start and stop characters.
5451          */
5452         if (ch->ch_astartc != ch->ch_fepastartc ||
5453             ch->ch_astopc != ch->ch_fepastopc) {
5454                 ch->ch_fepastartc = ch->ch_astartc;
5455                 ch->ch_fepastopc = ch->ch_astopc;
5456
5457                 /* Okay to have channel and board locks held calling this */
5458                 dgap_cmdb(ch, SAFLOWC, ch->ch_fepastartc, ch->ch_fepastopc, 0);
5459         }
5460
5461         return 0;
5462 }
5463
5464 /*
5465  * dgap_parity_scan()
5466  *
5467  * Convert the FEP5 way of reporting parity errors and breaks into
5468  * the Linux line discipline way.
5469  */
5470 static void dgap_parity_scan(struct channel_t *ch, unsigned char *cbuf,
5471                                 unsigned char *fbuf, int *len)
5472 {
5473         int l = *len;
5474         int count = 0;
5475         unsigned char *in, *cout, *fout;
5476         unsigned char c;
5477
5478         in = cbuf;
5479         cout = cbuf;
5480         fout = fbuf;
5481
5482         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
5483                 return;
5484
5485         while (l--) {
5486                 c = *in++;
5487                 switch (ch->pscan_state) {
5488                 default:
5489                         /* reset to sanity and fall through */
5490                         ch->pscan_state = 0;
5491
5492                 case 0:
5493                         /* No FF seen yet */
5494                         if (c == (unsigned char) '\377')
5495                                 /* delete this character from stream */
5496                                 ch->pscan_state = 1;
5497                         else {
5498                                 *cout++ = c;
5499                                 *fout++ = TTY_NORMAL;
5500                                 count += 1;
5501                         }
5502                         break;
5503
5504                 case 1:
5505                         /* first FF seen */
5506                         if (c == (unsigned char) '\377') {
5507                                 /* doubled ff, transform to single ff */
5508                                 *cout++ = c;
5509                                 *fout++ = TTY_NORMAL;
5510                                 count += 1;
5511                                 ch->pscan_state = 0;
5512                         } else {
5513                                 /* save value examination in next state */
5514                                 ch->pscan_savechar = c;
5515                                 ch->pscan_state = 2;
5516                         }
5517                         break;
5518
5519                 case 2:
5520                         /* third character of ff sequence */
5521
5522                         *cout++ = c;
5523
5524                         if (ch->pscan_savechar == 0x0) {
5525
5526                                 if (c == 0x0) {
5527                                         ch->ch_err_break++;
5528                                         *fout++ = TTY_BREAK;
5529                                 } else {
5530                                         ch->ch_err_parity++;
5531                                         *fout++ = TTY_PARITY;
5532                                 }
5533                         }
5534
5535                         count += 1;
5536                         ch->pscan_state = 0;
5537                 }
5538         }
5539         *len = count;
5540 }
5541
5542 static void dgap_write_wakeup(struct board_t *bd, struct channel_t *ch,
5543                               struct un_t *un, u32 mask,
5544                               unsigned long *irq_flags1,
5545                               unsigned long *irq_flags2)
5546 {
5547         if (!(un->un_flags & mask))
5548                 return;
5549
5550         un->un_flags &= ~mask;
5551
5552         if (!(un->un_flags & UN_ISOPEN))
5553                 return;
5554
5555         if ((un->un_tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
5556             un->un_tty->ldisc->ops->write_wakeup) {
5557                 spin_unlock_irqrestore(&ch->ch_lock, *irq_flags2);
5558                 spin_unlock_irqrestore(&bd->bd_lock, *irq_flags1);
5559
5560                 (un->un_tty->ldisc->ops->write_wakeup)(un->un_tty);
5561
5562                 spin_lock_irqsave(&bd->bd_lock, *irq_flags1);
5563                 spin_lock_irqsave(&ch->ch_lock, *irq_flags2);
5564         }
5565         wake_up_interruptible(&un->un_tty->write_wait);
5566         wake_up_interruptible(&un->un_flags_wait);
5567 }
5568
5569 /*=======================================================================
5570  *
5571  *      dgap_event - FEP to host event processing routine.
5572  *
5573  *              bd     - Board of current event.
5574  *
5575  *=======================================================================*/
5576 static int dgap_event(struct board_t *bd)
5577 {
5578         struct channel_t *ch;
5579         ulong           lock_flags;
5580         ulong           lock_flags2;
5581         struct bs_t     *bs;
5582         u8              *event;
5583         u8              *vaddr = NULL;
5584         struct ev_t     *eaddr = NULL;
5585         uint            head;
5586         uint            tail;
5587         int             port;
5588         int             reason;
5589         int             modem;
5590         int             b1;
5591
5592         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
5593                 return -ENXIO;
5594
5595         spin_lock_irqsave(&bd->bd_lock, lock_flags);
5596
5597         vaddr = bd->re_map_membase;
5598
5599         if (!vaddr) {
5600                 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
5601                 return -ENXIO;
5602         }
5603
5604         eaddr = (struct ev_t *) (vaddr + EVBUF);
5605
5606         /* Get our head and tail */
5607         head = readw(&(eaddr->ev_head));
5608         tail = readw(&(eaddr->ev_tail));
5609
5610         /*
5611          * Forget it if pointers out of range.
5612          */
5613
5614         if (head >= EVMAX - EVSTART || tail >= EVMAX - EVSTART ||
5615             (head | tail) & 03) {
5616                 /* Let go of board lock */
5617                 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
5618                 return -ENXIO;
5619         }
5620
5621         /*
5622          * Loop to process all the events in the buffer.
5623          */
5624         while (tail != head) {
5625
5626                 /*
5627                  * Get interrupt information.
5628                  */
5629
5630                 event = bd->re_map_membase + tail + EVSTART;
5631
5632                 port   = event[0];
5633                 reason = event[1];
5634                 modem  = event[2];
5635                 b1     = event[3];
5636
5637                 /*
5638                  * Make sure the interrupt is valid.
5639                  */
5640                 if (port >= bd->nasync)
5641                         goto next;
5642
5643                 if (!(reason & (IFMODEM | IFBREAK | IFTLW | IFTEM | IFDATA)))
5644                         goto next;
5645
5646                 ch = bd->channels[port];
5647
5648                 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
5649                         goto next;
5650
5651                 /*
5652                  * If we have made it here, the event was valid.
5653                  * Lock down the channel.
5654                  */
5655                 spin_lock_irqsave(&ch->ch_lock, lock_flags2);
5656
5657                 bs = ch->ch_bs;
5658
5659                 if (!bs) {
5660                         spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
5661                         goto next;
5662                 }
5663
5664                 /*
5665                  * Process received data.
5666                  */
5667                 if (reason & IFDATA) {
5668
5669                         /*
5670                          * ALL LOCKS *MUST* BE DROPPED BEFORE CALLING INPUT!
5671                          * input could send some data to ld, which in turn
5672                          * could do a callback to one of our other functions.
5673                          */
5674                         spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
5675                         spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
5676
5677                         dgap_input(ch);
5678
5679                         spin_lock_irqsave(&bd->bd_lock, lock_flags);
5680                         spin_lock_irqsave(&ch->ch_lock, lock_flags2);
5681
5682                         if (ch->ch_flags & CH_RACTIVE)
5683                                 ch->ch_flags |= CH_RENABLE;
5684                         else
5685                                 writeb(1, &(bs->idata));
5686
5687                         if (ch->ch_flags & CH_RWAIT) {
5688                                 ch->ch_flags &= ~CH_RWAIT;
5689
5690                                 wake_up_interruptible
5691                                         (&ch->ch_tun.un_flags_wait);
5692                         }
5693                 }
5694
5695                 /*
5696                  * Process Modem change signals.
5697                  */
5698                 if (reason & IFMODEM) {
5699                         ch->ch_mistat = modem;
5700                         dgap_carrier(ch);
5701                 }
5702
5703                 /*
5704                  * Process break.
5705                  */
5706                 if (reason & IFBREAK) {
5707
5708                         if (ch->ch_tun.un_tty) {
5709                                 /* A break has been indicated */
5710                                 ch->ch_err_break++;
5711                                 tty_buffer_request_room
5712                                         (ch->ch_tun.un_tty->port, 1);
5713                                 tty_insert_flip_char(ch->ch_tun.un_tty->port,
5714                                                      0, TTY_BREAK);
5715                                 tty_flip_buffer_push(ch->ch_tun.un_tty->port);
5716                         }
5717                 }
5718
5719                 /*
5720                  * Process Transmit low.
5721                  */
5722                 if (reason & IFTLW) {
5723                         dgap_write_wakeup(bd, ch, &ch->ch_tun, UN_LOW,
5724                                           &lock_flags, &lock_flags2);
5725                         dgap_write_wakeup(bd, ch, &ch->ch_pun, UN_LOW,
5726                                           &lock_flags, &lock_flags2);
5727                         if (ch->ch_flags & CH_WLOW) {
5728                                 ch->ch_flags &= ~CH_WLOW;
5729                                 wake_up_interruptible(&ch->ch_flags_wait);
5730                         }
5731                 }
5732
5733                 /*
5734                  * Process Transmit empty.
5735                  */
5736                 if (reason & IFTEM) {
5737                         dgap_write_wakeup(bd, ch, &ch->ch_tun, UN_EMPTY,
5738                                           &lock_flags, &lock_flags2);
5739                         dgap_write_wakeup(bd, ch, &ch->ch_pun, UN_EMPTY,
5740                                           &lock_flags, &lock_flags2);
5741                         if (ch->ch_flags & CH_WEMPTY) {
5742                                 ch->ch_flags &= ~CH_WEMPTY;
5743                                 wake_up_interruptible(&ch->ch_flags_wait);
5744                         }
5745                 }
5746
5747                 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
5748
5749 next:
5750                 tail = (tail + 4) & (EVMAX - EVSTART - 4);
5751         }
5752
5753         writew(tail, &(eaddr->ev_tail));
5754         spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
5755
5756         return 0;
5757 }
5758
5759 static ssize_t dgap_driver_version_show(struct device_driver *ddp, char *buf)
5760 {
5761         return snprintf(buf, PAGE_SIZE, "%s\n", DG_PART);
5762 }
5763 static DRIVER_ATTR(version, S_IRUSR, dgap_driver_version_show, NULL);
5764
5765
5766 static ssize_t dgap_driver_boards_show(struct device_driver *ddp, char *buf)
5767 {
5768         return snprintf(buf, PAGE_SIZE, "%d\n", dgap_NumBoards);
5769 }
5770 static DRIVER_ATTR(boards, S_IRUSR, dgap_driver_boards_show, NULL);
5771
5772
5773 static ssize_t dgap_driver_maxboards_show(struct device_driver *ddp, char *buf)
5774 {
5775         return snprintf(buf, PAGE_SIZE, "%d\n", MAXBOARDS);
5776 }
5777 static DRIVER_ATTR(maxboards, S_IRUSR, dgap_driver_maxboards_show, NULL);
5778
5779
5780 static ssize_t dgap_driver_pollcounter_show(struct device_driver *ddp,
5781                                             char *buf)
5782 {
5783         return snprintf(buf, PAGE_SIZE, "%ld\n", dgap_poll_counter);
5784 }
5785 static DRIVER_ATTR(pollcounter, S_IRUSR, dgap_driver_pollcounter_show, NULL);
5786
5787 static ssize_t dgap_driver_pollrate_show(struct device_driver *ddp, char *buf)
5788 {
5789         return snprintf(buf, PAGE_SIZE, "%dms\n", dgap_poll_tick);
5790 }
5791
5792 static ssize_t dgap_driver_pollrate_store(struct device_driver *ddp,
5793                                           const char *buf, size_t count)
5794 {
5795         if (sscanf(buf, "%d\n", &dgap_poll_tick) != 1)
5796                 return -EINVAL;
5797         return count;
5798 }
5799 static DRIVER_ATTR(pollrate, (S_IRUSR | S_IWUSR), dgap_driver_pollrate_show,
5800                    dgap_driver_pollrate_store);
5801
5802 static int dgap_create_driver_sysfiles(struct pci_driver *dgap_driver)
5803 {
5804         int rc = 0;
5805         struct device_driver *driverfs = &dgap_driver->driver;
5806
5807         rc |= driver_create_file(driverfs, &driver_attr_version);
5808         rc |= driver_create_file(driverfs, &driver_attr_boards);
5809         rc |= driver_create_file(driverfs, &driver_attr_maxboards);
5810         rc |= driver_create_file(driverfs, &driver_attr_pollrate);
5811         rc |= driver_create_file(driverfs, &driver_attr_pollcounter);
5812
5813         return rc;
5814 }
5815
5816 static void dgap_remove_driver_sysfiles(struct pci_driver *dgap_driver)
5817 {
5818         struct device_driver *driverfs = &dgap_driver->driver;
5819         driver_remove_file(driverfs, &driver_attr_version);
5820         driver_remove_file(driverfs, &driver_attr_boards);
5821         driver_remove_file(driverfs, &driver_attr_maxboards);
5822         driver_remove_file(driverfs, &driver_attr_pollrate);
5823         driver_remove_file(driverfs, &driver_attr_pollcounter);
5824 }
5825
5826 static struct board_t *dgap_verify_board(struct device *p)
5827 {
5828         struct board_t *bd;
5829
5830         if (!p)
5831                 return NULL;
5832
5833         bd = dev_get_drvdata(p);
5834         if (!bd || bd->magic != DGAP_BOARD_MAGIC || bd->state != BOARD_READY)
5835                 return NULL;
5836
5837         return bd;
5838 }
5839
5840 static ssize_t dgap_ports_state_show(struct device *p,
5841                                      struct device_attribute *attr,
5842                                      char *buf)
5843 {
5844         struct board_t *bd;
5845         int count = 0;
5846         int i = 0;
5847
5848         bd = dgap_verify_board(p);
5849         if (!bd)
5850                 return 0;
5851
5852         for (i = 0; i < bd->nasync; i++) {
5853                 count += snprintf(buf + count, PAGE_SIZE - count,
5854                         "%d %s\n", bd->channels[i]->ch_portnum,
5855                         bd->channels[i]->ch_open_count ? "Open" : "Closed");
5856         }
5857         return count;
5858 }
5859 static DEVICE_ATTR(ports_state, S_IRUSR, dgap_ports_state_show, NULL);
5860
5861 static ssize_t dgap_ports_baud_show(struct device *p,
5862                                     struct device_attribute *attr,
5863                                     char *buf)
5864 {
5865         struct board_t *bd;
5866         int count = 0;
5867         int i = 0;
5868
5869         bd = dgap_verify_board(p);
5870         if (!bd)
5871                 return 0;
5872
5873         for (i = 0; i < bd->nasync; i++) {
5874                 count +=  snprintf(buf + count, PAGE_SIZE - count, "%d %d\n",
5875                                    bd->channels[i]->ch_portnum,
5876                                    bd->channels[i]->ch_baud_info);
5877         }
5878         return count;
5879 }
5880 static DEVICE_ATTR(ports_baud, S_IRUSR, dgap_ports_baud_show, NULL);
5881
5882 static ssize_t dgap_ports_msignals_show(struct device *p,
5883                                         struct device_attribute *attr,
5884                                         char *buf)
5885 {
5886         struct board_t *bd;
5887         int count = 0;
5888         int i = 0;
5889
5890         bd = dgap_verify_board(p);
5891         if (!bd)
5892                 return 0;
5893
5894         for (i = 0; i < bd->nasync; i++) {
5895                 if (bd->channels[i]->ch_open_count)
5896                         count += snprintf(buf + count, PAGE_SIZE - count,
5897                                 "%d %s %s %s %s %s %s\n",
5898                                 bd->channels[i]->ch_portnum,
5899                                 (bd->channels[i]->ch_mostat &
5900                                  UART_MCR_RTS) ? "RTS" : "",
5901                                 (bd->channels[i]->ch_mistat &
5902                                  UART_MSR_CTS) ? "CTS" : "",
5903                                 (bd->channels[i]->ch_mostat &
5904                                  UART_MCR_DTR) ? "DTR" : "",
5905                                 (bd->channels[i]->ch_mistat &
5906                                  UART_MSR_DSR) ? "DSR" : "",
5907                                 (bd->channels[i]->ch_mistat &
5908                                  UART_MSR_DCD) ? "DCD" : "",
5909                                 (bd->channels[i]->ch_mistat &
5910                                  UART_MSR_RI)  ? "RI"  : "");
5911                 else
5912                         count += snprintf(buf + count, PAGE_SIZE - count,
5913                                 "%d\n", bd->channels[i]->ch_portnum);
5914         }
5915         return count;
5916 }
5917 static DEVICE_ATTR(ports_msignals, S_IRUSR, dgap_ports_msignals_show, NULL);
5918
5919 static ssize_t dgap_ports_iflag_show(struct device *p,
5920                                      struct device_attribute *attr,
5921                                      char *buf)
5922 {
5923         struct board_t *bd;
5924         int count = 0;
5925         int i = 0;
5926
5927         bd = dgap_verify_board(p);
5928         if (!bd)
5929                 return 0;
5930
5931         for (i = 0; i < bd->nasync; i++)
5932                 count += snprintf(buf + count, PAGE_SIZE - count, "%d %x\n",
5933                                   bd->channels[i]->ch_portnum,
5934                                   bd->channels[i]->ch_c_iflag);
5935         return count;
5936 }
5937 static DEVICE_ATTR(ports_iflag, S_IRUSR, dgap_ports_iflag_show, NULL);
5938
5939 static ssize_t dgap_ports_cflag_show(struct device *p,
5940                                      struct device_attribute *attr,
5941                                      char *buf)
5942 {
5943         struct board_t *bd;
5944         int count = 0;
5945         int i = 0;
5946
5947         bd = dgap_verify_board(p);
5948         if (!bd)
5949                 return 0;
5950
5951         for (i = 0; i < bd->nasync; i++)
5952                 count += snprintf(buf + count, PAGE_SIZE - count, "%d %x\n",
5953                                   bd->channels[i]->ch_portnum,
5954                                   bd->channels[i]->ch_c_cflag);
5955         return count;
5956 }
5957 static DEVICE_ATTR(ports_cflag, S_IRUSR, dgap_ports_cflag_show, NULL);
5958
5959 static ssize_t dgap_ports_oflag_show(struct device *p,
5960                                      struct device_attribute *attr,
5961                                      char *buf)
5962 {
5963         struct board_t *bd;
5964         int count = 0;
5965         int i = 0;
5966
5967         bd = dgap_verify_board(p);
5968         if (!bd)
5969                 return 0;
5970
5971         for (i = 0; i < bd->nasync; i++)
5972                 count += snprintf(buf + count, PAGE_SIZE - count, "%d %x\n",
5973                                   bd->channels[i]->ch_portnum,
5974                                   bd->channels[i]->ch_c_oflag);
5975         return count;
5976 }
5977 static DEVICE_ATTR(ports_oflag, S_IRUSR, dgap_ports_oflag_show, NULL);
5978
5979 static ssize_t dgap_ports_lflag_show(struct device *p,
5980                                      struct device_attribute *attr,
5981                                      char *buf)
5982 {
5983         struct board_t *bd;
5984         int count = 0;
5985         int i = 0;
5986
5987         bd = dgap_verify_board(p);
5988         if (!bd)
5989                 return 0;
5990
5991         for (i = 0; i < bd->nasync; i++)
5992                 count += snprintf(buf + count, PAGE_SIZE - count, "%d %x\n",
5993                                   bd->channels[i]->ch_portnum,
5994                                   bd->channels[i]->ch_c_lflag);
5995         return count;
5996 }
5997 static DEVICE_ATTR(ports_lflag, S_IRUSR, dgap_ports_lflag_show, NULL);
5998
5999 static ssize_t dgap_ports_digi_flag_show(struct device *p,
6000                                          struct device_attribute *attr,
6001                                          char *buf)
6002 {
6003         struct board_t *bd;
6004         int count = 0;
6005         int i = 0;
6006
6007         bd = dgap_verify_board(p);
6008         if (!bd)
6009                 return 0;
6010
6011         for (i = 0; i < bd->nasync; i++)
6012                 count += snprintf(buf + count, PAGE_SIZE - count, "%d %x\n",
6013                                   bd->channels[i]->ch_portnum,
6014                                   bd->channels[i]->ch_digi.digi_flags);
6015         return count;
6016 }
6017 static DEVICE_ATTR(ports_digi_flag, S_IRUSR, dgap_ports_digi_flag_show, NULL);
6018
6019 static ssize_t dgap_ports_rxcount_show(struct device *p,
6020                                        struct device_attribute *attr,
6021                                        char *buf)
6022 {
6023         struct board_t *bd;
6024         int count = 0;
6025         int i = 0;
6026
6027         bd = dgap_verify_board(p);
6028         if (!bd)
6029                 return 0;
6030
6031         for (i = 0; i < bd->nasync; i++)
6032                 count += snprintf(buf + count, PAGE_SIZE - count, "%d %ld\n",
6033                                   bd->channels[i]->ch_portnum,
6034                                   bd->channels[i]->ch_rxcount);
6035         return count;
6036 }
6037 static DEVICE_ATTR(ports_rxcount, S_IRUSR, dgap_ports_rxcount_show, NULL);
6038
6039 static ssize_t dgap_ports_txcount_show(struct device *p,
6040                                        struct device_attribute *attr,
6041                                        char *buf)
6042 {
6043         struct board_t *bd;
6044         int count = 0;
6045         int i = 0;
6046
6047         bd = dgap_verify_board(p);
6048         if (!bd)
6049                 return 0;
6050
6051         for (i = 0; i < bd->nasync; i++)
6052                 count += snprintf(buf + count, PAGE_SIZE - count, "%d %ld\n",
6053                                   bd->channels[i]->ch_portnum,
6054                                   bd->channels[i]->ch_txcount);
6055         return count;
6056 }
6057 static DEVICE_ATTR(ports_txcount, S_IRUSR, dgap_ports_txcount_show, NULL);
6058
6059 /* this function creates the sys files that will export each signal status
6060  * to sysfs each value will be put in a separate filename
6061  */
6062 static void dgap_create_ports_sysfiles(struct board_t *bd)
6063 {
6064         dev_set_drvdata(&bd->pdev->dev, bd);
6065         device_create_file(&(bd->pdev->dev), &dev_attr_ports_state);
6066         device_create_file(&(bd->pdev->dev), &dev_attr_ports_baud);
6067         device_create_file(&(bd->pdev->dev), &dev_attr_ports_msignals);
6068         device_create_file(&(bd->pdev->dev), &dev_attr_ports_iflag);
6069         device_create_file(&(bd->pdev->dev), &dev_attr_ports_cflag);
6070         device_create_file(&(bd->pdev->dev), &dev_attr_ports_oflag);
6071         device_create_file(&(bd->pdev->dev), &dev_attr_ports_lflag);
6072         device_create_file(&(bd->pdev->dev), &dev_attr_ports_digi_flag);
6073         device_create_file(&(bd->pdev->dev), &dev_attr_ports_rxcount);
6074         device_create_file(&(bd->pdev->dev), &dev_attr_ports_txcount);
6075 }
6076
6077 /* removes all the sys files created for that port */
6078 static void dgap_remove_ports_sysfiles(struct board_t *bd)
6079 {
6080         device_remove_file(&(bd->pdev->dev), &dev_attr_ports_state);
6081         device_remove_file(&(bd->pdev->dev), &dev_attr_ports_baud);
6082         device_remove_file(&(bd->pdev->dev), &dev_attr_ports_msignals);
6083         device_remove_file(&(bd->pdev->dev), &dev_attr_ports_iflag);
6084         device_remove_file(&(bd->pdev->dev), &dev_attr_ports_cflag);
6085         device_remove_file(&(bd->pdev->dev), &dev_attr_ports_oflag);
6086         device_remove_file(&(bd->pdev->dev), &dev_attr_ports_lflag);
6087         device_remove_file(&(bd->pdev->dev), &dev_attr_ports_digi_flag);
6088         device_remove_file(&(bd->pdev->dev), &dev_attr_ports_rxcount);
6089         device_remove_file(&(bd->pdev->dev), &dev_attr_ports_txcount);
6090 }
6091
6092 static ssize_t dgap_tty_state_show(struct device *d,
6093                                    struct device_attribute *attr,
6094                                    char *buf)
6095 {
6096         struct board_t *bd;
6097         struct channel_t *ch;
6098         struct un_t *un;
6099
6100         if (!d)
6101                 return 0;
6102         un = dev_get_drvdata(d);
6103         if (!un || un->magic != DGAP_UNIT_MAGIC)
6104                 return 0;
6105         ch = un->un_ch;
6106         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
6107                 return 0;
6108         bd = ch->ch_bd;
6109         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
6110                 return 0;
6111         if (bd->state != BOARD_READY)
6112                 return 0;
6113
6114         return snprintf(buf, PAGE_SIZE, "%s", un->un_open_count ?
6115                         "Open" : "Closed");
6116 }
6117 static DEVICE_ATTR(state, S_IRUSR, dgap_tty_state_show, NULL);
6118
6119 static ssize_t dgap_tty_baud_show(struct device *d,
6120                                   struct device_attribute *attr,
6121                                   char *buf)
6122 {
6123         struct board_t *bd;
6124         struct channel_t *ch;
6125         struct un_t *un;
6126
6127         if (!d)
6128                 return 0;
6129         un = dev_get_drvdata(d);
6130         if (!un || un->magic != DGAP_UNIT_MAGIC)
6131                 return 0;
6132         ch = un->un_ch;
6133         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
6134                 return 0;
6135         bd = ch->ch_bd;
6136         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
6137                 return 0;
6138         if (bd->state != BOARD_READY)
6139                 return 0;
6140
6141         return snprintf(buf, PAGE_SIZE, "%d\n", ch->ch_baud_info);
6142 }
6143 static DEVICE_ATTR(baud, S_IRUSR, dgap_tty_baud_show, NULL);
6144
6145 static ssize_t dgap_tty_msignals_show(struct device *d,
6146                                       struct device_attribute *attr,
6147                                       char *buf)
6148 {
6149         struct board_t *bd;
6150         struct channel_t *ch;
6151         struct un_t *un;
6152
6153         if (!d)
6154                 return 0;
6155         un = dev_get_drvdata(d);
6156         if (!un || un->magic != DGAP_UNIT_MAGIC)
6157                 return 0;
6158         ch = un->un_ch;
6159         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
6160                 return 0;
6161         bd = ch->ch_bd;
6162         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
6163                 return 0;
6164         if (bd->state != BOARD_READY)
6165                 return 0;
6166
6167         if (ch->ch_open_count) {
6168                 return snprintf(buf, PAGE_SIZE, "%s %s %s %s %s %s\n",
6169                         (ch->ch_mostat & UART_MCR_RTS) ? "RTS" : "",
6170                         (ch->ch_mistat & UART_MSR_CTS) ? "CTS" : "",
6171                         (ch->ch_mostat & UART_MCR_DTR) ? "DTR" : "",
6172                         (ch->ch_mistat & UART_MSR_DSR) ? "DSR" : "",
6173                         (ch->ch_mistat & UART_MSR_DCD) ? "DCD" : "",
6174                         (ch->ch_mistat & UART_MSR_RI)  ? "RI"  : "");
6175         }
6176         return 0;
6177 }
6178 static DEVICE_ATTR(msignals, S_IRUSR, dgap_tty_msignals_show, NULL);
6179
6180 static ssize_t dgap_tty_iflag_show(struct device *d,
6181                                    struct device_attribute *attr,
6182                                    char *buf)
6183 {
6184         struct board_t *bd;
6185         struct channel_t *ch;
6186         struct un_t *un;
6187
6188         if (!d)
6189                 return 0;
6190         un = dev_get_drvdata(d);
6191         if (!un || un->magic != DGAP_UNIT_MAGIC)
6192                 return 0;
6193         ch = un->un_ch;
6194         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
6195                 return 0;
6196         bd = ch->ch_bd;
6197         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
6198                 return 0;
6199         if (bd->state != BOARD_READY)
6200                 return 0;
6201
6202         return snprintf(buf, PAGE_SIZE, "%x\n", ch->ch_c_iflag);
6203 }
6204 static DEVICE_ATTR(iflag, S_IRUSR, dgap_tty_iflag_show, NULL);
6205
6206 static ssize_t dgap_tty_cflag_show(struct device *d,
6207                                    struct device_attribute *attr,
6208                                    char *buf)
6209 {
6210         struct board_t *bd;
6211         struct channel_t *ch;
6212         struct un_t *un;
6213
6214         if (!d)
6215                 return 0;
6216         un = dev_get_drvdata(d);
6217         if (!un || un->magic != DGAP_UNIT_MAGIC)
6218                 return 0;
6219         ch = un->un_ch;
6220         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
6221                 return 0;
6222         bd = ch->ch_bd;
6223         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
6224                 return 0;
6225         if (bd->state != BOARD_READY)
6226                 return 0;
6227
6228         return snprintf(buf, PAGE_SIZE, "%x\n", ch->ch_c_cflag);
6229 }
6230 static DEVICE_ATTR(cflag, S_IRUSR, dgap_tty_cflag_show, NULL);
6231
6232 static ssize_t dgap_tty_oflag_show(struct device *d,
6233                                    struct device_attribute *attr,
6234                                    char *buf)
6235 {
6236         struct board_t *bd;
6237         struct channel_t *ch;
6238         struct un_t *un;
6239
6240         if (!d)
6241                 return 0;
6242         un = dev_get_drvdata(d);
6243         if (!un || un->magic != DGAP_UNIT_MAGIC)
6244                 return 0;
6245         ch = un->un_ch;
6246         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
6247                 return 0;
6248         bd = ch->ch_bd;
6249         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
6250                 return 0;
6251         if (bd->state != BOARD_READY)
6252                 return 0;
6253
6254         return snprintf(buf, PAGE_SIZE, "%x\n", ch->ch_c_oflag);
6255 }
6256 static DEVICE_ATTR(oflag, S_IRUSR, dgap_tty_oflag_show, NULL);
6257
6258 static ssize_t dgap_tty_lflag_show(struct device *d,
6259                                    struct device_attribute *attr,
6260                                    char *buf)
6261 {
6262         struct board_t *bd;
6263         struct channel_t *ch;
6264         struct un_t *un;
6265
6266         if (!d)
6267                 return 0;
6268         un = dev_get_drvdata(d);
6269         if (!un || un->magic != DGAP_UNIT_MAGIC)
6270                 return 0;
6271         ch = un->un_ch;
6272         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
6273                 return 0;
6274         bd = ch->ch_bd;
6275         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
6276                 return 0;
6277         if (bd->state != BOARD_READY)
6278                 return 0;
6279
6280         return snprintf(buf, PAGE_SIZE, "%x\n", ch->ch_c_lflag);
6281 }
6282 static DEVICE_ATTR(lflag, S_IRUSR, dgap_tty_lflag_show, NULL);
6283
6284 static ssize_t dgap_tty_digi_flag_show(struct device *d,
6285                                        struct device_attribute *attr,
6286                                        char *buf)
6287 {
6288         struct board_t *bd;
6289         struct channel_t *ch;
6290         struct un_t *un;
6291
6292         if (!d)
6293                 return 0;
6294         un = dev_get_drvdata(d);
6295         if (!un || un->magic != DGAP_UNIT_MAGIC)
6296                 return 0;
6297         ch = un->un_ch;
6298         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
6299                 return 0;
6300         bd = ch->ch_bd;
6301         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
6302                 return 0;
6303         if (bd->state != BOARD_READY)
6304                 return 0;
6305
6306         return snprintf(buf, PAGE_SIZE, "%x\n", ch->ch_digi.digi_flags);
6307 }
6308 static DEVICE_ATTR(digi_flag, S_IRUSR, dgap_tty_digi_flag_show, NULL);
6309
6310 static ssize_t dgap_tty_rxcount_show(struct device *d,
6311                                      struct device_attribute *attr,
6312                                      char *buf)
6313 {
6314         struct board_t *bd;
6315         struct channel_t *ch;
6316         struct un_t *un;
6317
6318         if (!d)
6319                 return 0;
6320         un = dev_get_drvdata(d);
6321         if (!un || un->magic != DGAP_UNIT_MAGIC)
6322                 return 0;
6323         ch = un->un_ch;
6324         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
6325                 return 0;
6326         bd = ch->ch_bd;
6327         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
6328                 return 0;
6329         if (bd->state != BOARD_READY)
6330                 return 0;
6331
6332         return snprintf(buf, PAGE_SIZE, "%ld\n", ch->ch_rxcount);
6333 }
6334 static DEVICE_ATTR(rxcount, S_IRUSR, dgap_tty_rxcount_show, NULL);
6335
6336 static ssize_t dgap_tty_txcount_show(struct device *d,
6337                                      struct device_attribute *attr,
6338                                      char *buf)
6339 {
6340         struct board_t *bd;
6341         struct channel_t *ch;
6342         struct un_t *un;
6343
6344         if (!d)
6345                 return 0;
6346         un = dev_get_drvdata(d);
6347         if (!un || un->magic != DGAP_UNIT_MAGIC)
6348                 return 0;
6349         ch = un->un_ch;
6350         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
6351                 return 0;
6352         bd = ch->ch_bd;
6353         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
6354                 return 0;
6355         if (bd->state != BOARD_READY)
6356                 return 0;
6357
6358         return snprintf(buf, PAGE_SIZE, "%ld\n", ch->ch_txcount);
6359 }
6360 static DEVICE_ATTR(txcount, S_IRUSR, dgap_tty_txcount_show, NULL);
6361
6362 static ssize_t dgap_tty_name_show(struct device *d,
6363                                   struct device_attribute *attr,
6364                                   char *buf)
6365 {
6366         struct board_t *bd;
6367         struct channel_t *ch;
6368         struct un_t *un;
6369         int     cn;
6370         int     bn;
6371         struct cnode *cptr = NULL;
6372         int found = FALSE;
6373         int ncount = 0;
6374         int starto = 0;
6375         int i = 0;
6376
6377         if (!d)
6378                 return 0;
6379         un = dev_get_drvdata(d);
6380         if (!un || un->magic != DGAP_UNIT_MAGIC)
6381                 return 0;
6382         ch = un->un_ch;
6383         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
6384                 return 0;
6385         bd = ch->ch_bd;
6386         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
6387                 return 0;
6388         if (bd->state != BOARD_READY)
6389                 return 0;
6390
6391         bn = bd->boardnum;
6392         cn = ch->ch_portnum;
6393
6394         for (cptr = bd->bd_config; cptr; cptr = cptr->next) {
6395
6396                 if ((cptr->type == BNODE) &&
6397                     ((cptr->u.board.type == APORT2_920P) ||
6398                      (cptr->u.board.type == APORT4_920P) ||
6399                      (cptr->u.board.type == APORT8_920P) ||
6400                      (cptr->u.board.type == PAPORT4) ||
6401                      (cptr->u.board.type == PAPORT8))) {
6402
6403                         found = TRUE;
6404                         if (cptr->u.board.v_start)
6405                                 starto = cptr->u.board.start;
6406                         else
6407                                 starto = 1;
6408                 }
6409
6410                 if (cptr->type == TNODE && found == TRUE) {
6411                         char *ptr1;
6412                         if (strstr(cptr->u.ttyname, "tty")) {
6413                                 ptr1 = cptr->u.ttyname;
6414                                 ptr1 += 3;
6415                         } else
6416                                 ptr1 = cptr->u.ttyname;
6417
6418                         for (i = 0; i < dgap_config_get_num_prts(bd); i++) {
6419                                 if (cn != i)
6420                                         continue;
6421
6422                                 return snprintf(buf, PAGE_SIZE, "%s%s%02d\n",
6423                                                 (un->un_type == DGAP_PRINT) ?
6424                                                  "pr" : "tty",
6425                                                 ptr1, i + starto);
6426                         }
6427                 }
6428
6429                 if (cptr->type == CNODE) {
6430
6431                         for (i = 0; i < cptr->u.conc.nport; i++) {
6432                                 if (cn != (i + ncount))
6433                                         continue;
6434
6435                                 return snprintf(buf, PAGE_SIZE, "%s%s%02ld\n",
6436                                                 (un->un_type == DGAP_PRINT) ?
6437                                                  "pr" : "tty",
6438                                                 cptr->u.conc.id,
6439                                                 i + (cptr->u.conc.v_start ?
6440                                                      cptr->u.conc.start : 1));
6441                         }
6442
6443                         ncount += cptr->u.conc.nport;
6444                 }
6445
6446                 if (cptr->type == MNODE) {
6447
6448                         for (i = 0; i < cptr->u.module.nport; i++) {
6449                                 if (cn != (i + ncount))
6450                                         continue;
6451
6452                                 return snprintf(buf, PAGE_SIZE, "%s%s%02ld\n",
6453                                                 (un->un_type == DGAP_PRINT) ?
6454                                                  "pr" : "tty",
6455                                                 cptr->u.module.id,
6456                                                 i + (cptr->u.module.v_start ?
6457                                                      cptr->u.module.start : 1));
6458                         }
6459
6460                         ncount += cptr->u.module.nport;
6461
6462                 }
6463         }
6464
6465         return snprintf(buf, PAGE_SIZE, "%s_dgap_%d_%d\n",
6466                 (un->un_type == DGAP_PRINT) ? "pr" : "tty", bn, cn);
6467
6468 }
6469 static DEVICE_ATTR(custom_name, S_IRUSR, dgap_tty_name_show, NULL);
6470
6471 static struct attribute *dgap_sysfs_tty_entries[] = {
6472         &dev_attr_state.attr,
6473         &dev_attr_baud.attr,
6474         &dev_attr_msignals.attr,
6475         &dev_attr_iflag.attr,
6476         &dev_attr_cflag.attr,
6477         &dev_attr_oflag.attr,
6478         &dev_attr_lflag.attr,
6479         &dev_attr_digi_flag.attr,
6480         &dev_attr_rxcount.attr,
6481         &dev_attr_txcount.attr,
6482         &dev_attr_custom_name.attr,
6483         NULL
6484 };
6485
6486 static struct attribute_group dgap_tty_attribute_group = {
6487         .name = NULL,
6488         .attrs = dgap_sysfs_tty_entries,
6489 };
6490
6491 static void dgap_create_tty_sysfs(struct un_t *un, struct device *c)
6492 {
6493         int ret;
6494
6495         ret = sysfs_create_group(&c->kobj, &dgap_tty_attribute_group);
6496         if (ret)
6497                 return;
6498
6499         dev_set_drvdata(c, un);
6500
6501 }
6502
6503 static void dgap_remove_tty_sysfs(struct device *c)
6504 {
6505         sysfs_remove_group(&c->kobj, &dgap_tty_attribute_group);
6506 }
6507
6508 /*
6509  * Parse a configuration file read into memory as a string.
6510  */
6511 static int      dgap_parsefile(char **in, int Remove)
6512 {
6513         struct cnode *p, *brd, *line, *conc;
6514         int     rc;
6515         char    *s = NULL;
6516         int     linecnt = 0;
6517
6518         p = &dgap_head;
6519         brd = line = conc = NULL;
6520
6521         /* perhaps we are adding to an existing list? */
6522         while (p->next != NULL)
6523                 p = p->next;
6524
6525         /* file must start with a BEGIN */
6526         while ((rc = dgap_gettok(in, p)) != BEGIN) {
6527                 if (rc == 0) {
6528                         dgap_err("unexpected EOF");
6529                         return -1;
6530                 }
6531         }
6532
6533         for (; ;) {
6534                 rc = dgap_gettok(in, p);
6535                 if (rc == 0) {
6536                         dgap_err("unexpected EOF");
6537                         return -1;
6538                 }
6539
6540                 switch (rc) {
6541                 case 0:
6542                         dgap_err("unexpected end of file");
6543                         return -1;
6544
6545                 case BEGIN:     /* should only be 1 begin */
6546                         dgap_err("unexpected config_begin\n");
6547                         return -1;
6548
6549                 case END:
6550                         return 0;
6551
6552                 case BOARD:     /* board info */
6553                         if (dgap_checknode(p))
6554                                 return -1;
6555                         p->next = dgap_newnode(BNODE);
6556                         if (!p->next) {
6557                                 dgap_err("out of memory");
6558                                 return -1;
6559                         }
6560                         p = p->next;
6561
6562                         p->u.board.status = dgap_savestring("No");
6563                         line = conc = NULL;
6564                         brd = p;
6565                         linecnt = -1;
6566                         break;
6567
6568                 case APORT2_920P:       /* AccelePort_4 */
6569                         if (p->type != BNODE) {
6570                                 dgap_err("unexpected Digi_2r_920 string");
6571                                 return -1;
6572                         }
6573                         p->u.board.type = APORT2_920P;
6574                         p->u.board.v_type = 1;
6575                         break;
6576
6577                 case APORT4_920P:       /* AccelePort_4 */
6578                         if (p->type != BNODE) {
6579                                 dgap_err("unexpected Digi_4r_920 string");
6580                                 return -1;
6581                         }
6582                         p->u.board.type = APORT4_920P;
6583                         p->u.board.v_type = 1;
6584                         break;
6585
6586                 case APORT8_920P:       /* AccelePort_8 */
6587                         if (p->type != BNODE) {
6588                                 dgap_err("unexpected Digi_8r_920 string");
6589                                 return -1;
6590                         }
6591                         p->u.board.type = APORT8_920P;
6592                         p->u.board.v_type = 1;
6593                         break;
6594
6595                 case PAPORT4:   /* AccelePort_4 PCI */
6596                         if (p->type != BNODE) {
6597                                 dgap_err("unexpected Digi_4r(PCI) string");
6598                                 return -1;
6599                         }
6600                         p->u.board.type = PAPORT4;
6601                         p->u.board.v_type = 1;
6602                         break;
6603
6604                 case PAPORT8:   /* AccelePort_8 PCI */
6605                         if (p->type != BNODE) {
6606                                 dgap_err("unexpected Digi_8r string");
6607                                 return -1;
6608                         }
6609                         p->u.board.type = PAPORT8;
6610                         p->u.board.v_type = 1;
6611                         break;
6612
6613                 case PCX:       /* PCI C/X */
6614                         if (p->type != BNODE) {
6615                                 dgap_err("unexpected Digi_C/X_(PCI) string");
6616                                 return -1;
6617                         }
6618                         p->u.board.type = PCX;
6619                         p->u.board.v_type = 1;
6620                         p->u.board.conc1 = 0;
6621                         p->u.board.conc2 = 0;
6622                         p->u.board.module1 = 0;
6623                         p->u.board.module2 = 0;
6624                         break;
6625
6626                 case PEPC:      /* PCI EPC/X */
6627                         if (p->type != BNODE) {
6628                                 dgap_err("unexpected \"Digi_EPC/X_(PCI)\" string");
6629                                 return -1;
6630                         }
6631                         p->u.board.type = PEPC;
6632                         p->u.board.v_type = 1;
6633                         p->u.board.conc1 = 0;
6634                         p->u.board.conc2 = 0;
6635                         p->u.board.module1 = 0;
6636                         p->u.board.module2 = 0;
6637                         break;
6638
6639                 case PPCM:      /* PCI/Xem */
6640                         if (p->type != BNODE) {
6641                                 dgap_err("unexpected PCI/Xem string");
6642                                 return -1;
6643                         }
6644                         p->u.board.type = PPCM;
6645                         p->u.board.v_type = 1;
6646                         p->u.board.conc1 = 0;
6647                         p->u.board.conc2 = 0;
6648                         break;
6649
6650                 case IO:        /* i/o port */
6651                         if (p->type != BNODE) {
6652                                 dgap_err("IO port only vaild for boards");
6653                                 return -1;
6654                         }
6655                         s = dgap_getword(in);
6656                         if (s == NULL) {
6657                                 dgap_err("unexpected end of file");
6658                                 return -1;
6659                         }
6660                         p->u.board.portstr = dgap_savestring(s);
6661                         if (kstrtol(s, 0, &p->u.board.port)) {
6662                                 dgap_err("bad number for IO port");
6663                                 return -1;
6664                         }
6665                         p->u.board.v_port = 1;
6666                         break;
6667
6668                 case MEM:       /* memory address */
6669                         if (p->type != BNODE) {
6670                                 dgap_err("memory address only vaild for boards");
6671                                 return -1;
6672                         }
6673                         s = dgap_getword(in);
6674                         if (s == NULL) {
6675                                 dgap_err("unexpected end of file");
6676                                 return -1;
6677                         }
6678                         p->u.board.addrstr = dgap_savestring(s);
6679                         if (kstrtoul(s, 0, &p->u.board.addr)) {
6680                                 dgap_err("bad number for memory address");
6681                                 return -1;
6682                         }
6683                         p->u.board.v_addr = 1;
6684                         break;
6685
6686                 case PCIINFO:   /* pci information */
6687                         if (p->type != BNODE) {
6688                                 dgap_err("memory address only vaild for boards");
6689                                 return -1;
6690                         }
6691                         s = dgap_getword(in);
6692                         if (s == NULL) {
6693                                 dgap_err("unexpected end of file");
6694                                 return -1;
6695                         }
6696                         p->u.board.pcibusstr = dgap_savestring(s);
6697                         if (kstrtoul(s, 0, &p->u.board.pcibus)) {
6698                                 dgap_err("bad number for pci bus");
6699                                 return -1;
6700                         }
6701                         p->u.board.v_pcibus = 1;
6702                         s = dgap_getword(in);
6703                         if (s == NULL) {
6704                                 dgap_err("unexpected end of file");
6705                                 return -1;
6706                         }
6707                         p->u.board.pcislotstr = dgap_savestring(s);
6708                         if (kstrtoul(s, 0, &p->u.board.pcislot)) {
6709                                 dgap_err("bad number for pci slot");
6710                                 return -1;
6711                         }
6712                         p->u.board.v_pcislot = 1;
6713                         break;
6714
6715                 case METHOD:
6716                         if (p->type != BNODE) {
6717                                 dgap_err("install method only vaild for boards");
6718                                 return -1;
6719                         }
6720                         s = dgap_getword(in);
6721                         if (s == NULL) {
6722                                 dgap_err("unexpected end of file");
6723                                 return -1;
6724                         }
6725                         p->u.board.method = dgap_savestring(s);
6726                         p->u.board.v_method = 1;
6727                         break;
6728
6729                 case STATUS:
6730                         if (p->type != BNODE) {
6731                                 dgap_err("config status only vaild for boards");
6732                                 return -1;
6733                         }
6734                         s = dgap_getword(in);
6735                         if (s == NULL) {
6736                                 dgap_err("unexpected end of file");
6737                                 return -1;
6738                         }
6739                         p->u.board.status = dgap_savestring(s);
6740                         break;
6741
6742                 case NPORTS:    /* number of ports */
6743                         if (p->type == BNODE) {
6744                                 s = dgap_getword(in);
6745                                 if (s == NULL) {
6746                                         dgap_err("unexpected end of file");
6747                                         return -1;
6748                                 }
6749                                 if (kstrtol(s, 0, &p->u.board.nport)) {
6750                                         dgap_err("bad number for number of ports");
6751                                         return -1;
6752                                 }
6753                                 p->u.board.v_nport = 1;
6754                         } else if (p->type == CNODE) {
6755                                 s = dgap_getword(in);
6756                                 if (s == NULL) {
6757                                         dgap_err("unexpected end of file");
6758                                         return -1;
6759                                 }
6760                                 if (kstrtol(s, 0, &p->u.conc.nport)) {
6761                                         dgap_err("bad number for number of ports");
6762                                         return -1;
6763                                 }
6764                                 p->u.conc.v_nport = 1;
6765                         } else if (p->type == MNODE) {
6766                                 s = dgap_getword(in);
6767                                 if (s == NULL) {
6768                                         dgap_err("unexpected end of file");
6769                                         return -1;
6770                                 }
6771                                 if (kstrtol(s, 0, &p->u.module.nport)) {
6772                                         dgap_err("bad number for number of ports");
6773                                         return -1;
6774                                 }
6775                                 p->u.module.v_nport = 1;
6776                         } else {
6777                                 dgap_err("nports only valid for concentrators or modules");
6778                                 return -1;
6779                         }
6780                         break;
6781
6782                 case ID:        /* letter ID used in tty name */
6783                         s = dgap_getword(in);
6784                         if (s == NULL) {
6785                                 dgap_err("unexpected end of file");
6786                                 return -1;
6787                         }
6788
6789                         p->u.board.status = dgap_savestring(s);
6790
6791                         if (p->type == CNODE) {
6792                                 p->u.conc.id = dgap_savestring(s);
6793                                 p->u.conc.v_id = 1;
6794                         } else if (p->type == MNODE) {
6795                                 p->u.module.id = dgap_savestring(s);
6796                                 p->u.module.v_id = 1;
6797                         } else {
6798                                 dgap_err("id only valid for concentrators or modules");
6799                                 return -1;
6800                         }
6801                         break;
6802
6803                 case STARTO:    /* start offset of ID */
6804                         if (p->type == BNODE) {
6805                                 s = dgap_getword(in);
6806                                 if (s == NULL) {
6807                                         dgap_err("unexpected end of file");
6808                                         return -1;
6809                                 }
6810                                 if (kstrtol(s, 0, &p->u.board.start)) {
6811                                         dgap_err("bad number for start of tty count");
6812                                         return -1;
6813                                 }
6814                                 p->u.board.v_start = 1;
6815                         } else if (p->type == CNODE) {
6816                                 s = dgap_getword(in);
6817                                 if (s == NULL) {
6818                                         dgap_err("unexpected end of file");
6819                                         return -1;
6820                                 }
6821                                 if (kstrtol(s, 0, &p->u.conc.start)) {
6822                                         dgap_err("bad number for start of tty count");
6823                                         return -1;
6824                                 }
6825                                 p->u.conc.v_start = 1;
6826                         } else if (p->type == MNODE) {
6827                                 s = dgap_getword(in);
6828                                 if (s == NULL) {
6829                                         dgap_err("unexpected end of file");
6830                                         return -1;
6831                                 }
6832                                 if (kstrtol(s, 0, &p->u.module.start)) {
6833                                         dgap_err("bad number for start of tty count");
6834                                         return -1;
6835                                 }
6836                                 p->u.module.v_start = 1;
6837                         } else {
6838                                 dgap_err("start only valid for concentrators or modules");
6839                                 return -1;
6840                         }
6841                         break;
6842
6843                 case TTYN:      /* tty name prefix */
6844                         if (dgap_checknode(p))
6845                                 return -1;
6846                         p->next = dgap_newnode(TNODE);
6847                         if (!p->next) {
6848                                 dgap_err("out of memory");
6849                                 return -1;
6850                         }
6851                         p = p->next;
6852                         s = dgap_getword(in);
6853                         if (!s) {
6854                                 dgap_err("unexpeced end of file");
6855                                 return -1;
6856                         }
6857                         p->u.ttyname = dgap_savestring(s);
6858                         if (!p->u.ttyname) {
6859                                 dgap_err("out of memory");
6860                                 return -1;
6861                         }
6862                         break;
6863
6864                 case CU:        /* cu name prefix */
6865                         if (dgap_checknode(p))
6866                                 return -1;
6867                         p->next = dgap_newnode(CUNODE);
6868                         if (!p->next) {
6869                                 dgap_err("out of memory");
6870                                 return -1;
6871                         }
6872                         p = p->next;
6873                         s = dgap_getword(in);
6874                         if (!s) {
6875                                 dgap_err("unexpeced end of file");
6876                                 return -1;
6877                         }
6878                         p->u.cuname = dgap_savestring(s);
6879                         if (!p->u.cuname) {
6880                                 dgap_err("out of memory");
6881                                 return -1;
6882                         }
6883                         break;
6884
6885                 case LINE:      /* line information */
6886                         if (dgap_checknode(p))
6887                                 return -1;
6888                         if (brd == NULL) {
6889                                 dgap_err("must specify board before line info");
6890                                 return -1;
6891                         }
6892                         switch (brd->u.board.type) {
6893                         case PPCM:
6894                                 dgap_err("line not vaild for PC/em");
6895                                 return -1;
6896                         }
6897                         p->next = dgap_newnode(LNODE);
6898                         if (!p->next) {
6899                                 dgap_err("out of memory");
6900                                 return -1;
6901                         }
6902                         p = p->next;
6903                         conc = NULL;
6904                         line = p;
6905                         linecnt++;
6906                         break;
6907
6908                 case CONC:      /* concentrator information */
6909                         if (dgap_checknode(p))
6910                                 return -1;
6911                         if (line == NULL) {
6912                                 dgap_err("must specify line info before concentrator");
6913                                 return -1;
6914                         }
6915                         p->next = dgap_newnode(CNODE);
6916                         if (!p->next) {
6917                                 dgap_err("out of memory");
6918                                 return -1;
6919                         }
6920                         p = p->next;
6921                         conc = p;
6922                         if (linecnt)
6923                                 brd->u.board.conc2++;
6924                         else
6925                                 brd->u.board.conc1++;
6926
6927                         break;
6928
6929                 case CX:        /* c/x type concentrator */
6930                         if (p->type != CNODE) {
6931                                 dgap_err("cx only valid for concentrators");
6932                                 return -1;
6933                         }
6934                         p->u.conc.type = CX;
6935                         p->u.conc.v_type = 1;
6936                         break;
6937
6938                 case EPC:       /* epc type concentrator */
6939                         if (p->type != CNODE) {
6940                                 dgap_err("cx only valid for concentrators");
6941                                 return -1;
6942                         }
6943                         p->u.conc.type = EPC;
6944                         p->u.conc.v_type = 1;
6945                         break;
6946
6947                 case MOD:       /* EBI module */
6948                         if (dgap_checknode(p))
6949                                 return -1;
6950                         if (brd == NULL) {
6951                                 dgap_err("must specify board info before EBI modules");
6952                                 return -1;
6953                         }
6954                         switch (brd->u.board.type) {
6955                         case PPCM:
6956                                 linecnt = 0;
6957                                 break;
6958                         default:
6959                                 if (conc == NULL) {
6960                                         dgap_err("must specify concentrator info before EBI module");
6961                                         return -1;
6962                                 }
6963                         }
6964                         p->next = dgap_newnode(MNODE);
6965                         if (!p->next) {
6966                                 dgap_err("out of memory");
6967                                 return -1;
6968                         }
6969                         p = p->next;
6970                         if (linecnt)
6971                                 brd->u.board.module2++;
6972                         else
6973                                 brd->u.board.module1++;
6974
6975                         break;
6976
6977                 case PORTS:     /* ports type EBI module */
6978                         if (p->type != MNODE) {
6979                                 dgap_err("ports only valid for EBI modules");
6980                                 return -1;
6981                         }
6982                         p->u.module.type = PORTS;
6983                         p->u.module.v_type = 1;
6984                         break;
6985
6986                 case MODEM:     /* ports type EBI module */
6987                         if (p->type != MNODE) {
6988                                 dgap_err("modem only valid for modem modules");
6989                                 return -1;
6990                         }
6991                         p->u.module.type = MODEM;
6992                         p->u.module.v_type = 1;
6993                         break;
6994
6995                 case CABLE:
6996                         if (p->type == LNODE) {
6997                                 s = dgap_getword(in);
6998                                 if (!s) {
6999                                         dgap_err("unexpected end of file");
7000                                         return -1;
7001                                 }
7002                                 p->u.line.cable = dgap_savestring(s);
7003                                 p->u.line.v_cable = 1;
7004                         }
7005                         break;
7006
7007                 case SPEED:     /* sync line speed indication */
7008                         if (p->type == LNODE) {
7009                                 s = dgap_getword(in);
7010                                 if (s == NULL) {
7011                                         dgap_err("unexpected end of file");
7012                                         return -1;
7013                                 }
7014                                 if (kstrtol(s, 0, &p->u.line.speed)) {
7015                                         dgap_err("bad number for line speed");
7016                                         return -1;
7017                                 }
7018                                 p->u.line.v_speed = 1;
7019                         } else if (p->type == CNODE) {
7020                                 s = dgap_getword(in);
7021                                 if (s == NULL) {
7022                                         dgap_err("unexpected end of file");
7023                                         return -1;
7024                                 }
7025                                 if (kstrtol(s, 0, &p->u.conc.speed)) {
7026                                         dgap_err("bad number for line speed");
7027                                         return -1;
7028                                 }
7029                                 p->u.conc.v_speed = 1;
7030                         } else {
7031                                 dgap_err("speed valid only for lines or concentrators.");
7032                                 return -1;
7033                         }
7034                         break;
7035
7036                 case CONNECT:
7037                         if (p->type == CNODE) {
7038                                 s = dgap_getword(in);
7039                                 if (!s) {
7040                                         dgap_err("unexpected end of file");
7041                                         return -1;
7042                                 }
7043                                 p->u.conc.connect = dgap_savestring(s);
7044                                 p->u.conc.v_connect = 1;
7045                         }
7046                         break;
7047                 case PRINT:     /* transparent print name prefix */
7048                         if (dgap_checknode(p))
7049                                 return -1;
7050                         p->next = dgap_newnode(PNODE);
7051                         if (!p->next) {
7052                                 dgap_err("out of memory");
7053                                 return -1;
7054                         }
7055                         p = p->next;
7056                         s = dgap_getword(in);
7057                         if (!s) {
7058                                 dgap_err("unexpeced end of file");
7059                                 return -1;
7060                         }
7061                         p->u.printname = dgap_savestring(s);
7062                         if (!p->u.printname) {
7063                                 dgap_err("out of memory");
7064                                 return -1;
7065                         }
7066                         break;
7067
7068                 case CMAJOR:    /* major number */
7069                         if (dgap_checknode(p))
7070                                 return -1;
7071                         p->next = dgap_newnode(JNODE);
7072                         if (!p->next) {
7073                                 dgap_err("out of memory");
7074                                 return -1;
7075                         }
7076                         p = p->next;
7077                         s = dgap_getword(in);
7078                         if (s == NULL) {
7079                                 dgap_err("unexpected end of file");
7080                                 return -1;
7081                         }
7082                         if (kstrtol(s, 0, &p->u.majornumber)) {
7083                                 dgap_err("bad number for major number");
7084                                 return -1;
7085                         }
7086                         break;
7087
7088                 case ALTPIN:    /* altpin setting */
7089                         if (dgap_checknode(p))
7090                                 return -1;
7091                         p->next = dgap_newnode(ANODE);
7092                         if (!p->next) {
7093                                 dgap_err("out of memory");
7094                                 return -1;
7095                         }
7096                         p = p->next;
7097                         s = dgap_getword(in);
7098                         if (s == NULL) {
7099                                 dgap_err("unexpected end of file");
7100                                 return -1;
7101                         }
7102                         if (kstrtol(s, 0, &p->u.altpin)) {
7103                                 dgap_err("bad number for altpin");
7104                                 return -1;
7105                         }
7106                         break;
7107
7108                 case USEINTR:           /* enable interrupt setting */
7109                         if (dgap_checknode(p))
7110                                 return -1;
7111                         p->next = dgap_newnode(INTRNODE);
7112                         if (!p->next) {
7113                                 dgap_err("out of memory");
7114                                 return -1;
7115                         }
7116                         p = p->next;
7117                         s = dgap_getword(in);
7118                         if (s == NULL) {
7119                                 dgap_err("unexpected end of file");
7120                                 return -1;
7121                         }
7122                         if (kstrtol(s, 0, &p->u.useintr)) {
7123                                 dgap_err("bad number for useintr");
7124                                 return -1;
7125                         }
7126                         break;
7127
7128                 case TTSIZ:     /* size of tty structure */
7129                         if (dgap_checknode(p))
7130                                 return -1;
7131                         p->next = dgap_newnode(TSNODE);
7132                         if (!p->next) {
7133                                 dgap_err("out of memory");
7134                                 return -1;
7135                         }
7136                         p = p->next;
7137                         s = dgap_getword(in);
7138                         if (s == NULL) {
7139                                 dgap_err("unexpected end of file");
7140                                 return -1;
7141                         }
7142                         if (kstrtol(s, 0, &p->u.ttysize)) {
7143                                 dgap_err("bad number for ttysize");
7144                                 return -1;
7145                         }
7146                         break;
7147
7148                 case CHSIZ:     /* channel structure size */
7149                         if (dgap_checknode(p))
7150                                 return -1;
7151                         p->next = dgap_newnode(CSNODE);
7152                         if (!p->next) {
7153                                 dgap_err("out of memory");
7154                                 return -1;
7155                         }
7156                         p = p->next;
7157                         s = dgap_getword(in);
7158                         if (s == NULL) {
7159                                 dgap_err("unexpected end of file");
7160                                 return -1;
7161                         }
7162                         if (kstrtol(s, 0, &p->u.chsize)) {
7163                                 dgap_err("bad number for chsize");
7164                                 return -1;
7165                         }
7166                         break;
7167
7168                 case BSSIZ:     /* board structure size */
7169                         if (dgap_checknode(p))
7170                                 return -1;
7171                         p->next = dgap_newnode(BSNODE);
7172                         if (!p->next) {
7173                                 dgap_err("out of memory");
7174                                 return -1;
7175                         }
7176                         p = p->next;
7177                         s = dgap_getword(in);
7178                         if (s == NULL) {
7179                                 dgap_err("unexpected end of file");
7180                                 return -1;
7181                         }
7182                         if (kstrtol(s, 0, &p->u.bssize)) {
7183                                 dgap_err("bad number for bssize");
7184                                 return -1;
7185                         }
7186                         break;
7187
7188                 case UNTSIZ:    /* sched structure size */
7189                         if (dgap_checknode(p))
7190                                 return -1;
7191                         p->next = dgap_newnode(USNODE);
7192                         if (!p->next) {
7193                                 dgap_err("out of memory");
7194                                 return -1;
7195                         }
7196                         p = p->next;
7197                         s = dgap_getword(in);
7198                         if (s == NULL) {
7199                                 dgap_err("unexpected end of file");
7200                                 return -1;
7201                         }
7202                         if (kstrtol(s, 0, &p->u.unsize)) {
7203                                 dgap_err("bad number for schedsize");
7204                                 return -1;
7205                         }
7206                         break;
7207
7208                 case F2SIZ:     /* f2200 structure size */
7209                         if (dgap_checknode(p))
7210                                 return -1;
7211                         p->next = dgap_newnode(FSNODE);
7212                         if (!p->next) {
7213                                 dgap_err("out of memory");
7214                                 return -1;
7215                         }
7216                         p = p->next;
7217                         s = dgap_getword(in);
7218                         if (s == NULL) {
7219                                 dgap_err("unexpected end of file");
7220                                 return -1;
7221                         }
7222                         if (kstrtol(s, 0, &p->u.f2size)) {
7223                                 dgap_err("bad number for f2200size");
7224                                 return -1;
7225                         }
7226                         break;
7227
7228                 case VPSIZ:     /* vpix structure size */
7229                         if (dgap_checknode(p))
7230                                 return -1;
7231                         p->next = dgap_newnode(VSNODE);
7232                         if (!p->next) {
7233                                 dgap_err("out of memory");
7234                                 return -1;
7235                         }
7236                         p = p->next;
7237                         s = dgap_getword(in);
7238                         if (s == NULL) {
7239                                 dgap_err("unexpected end of file");
7240                                 return -1;
7241                         }
7242                         if (kstrtol(s, 0, &p->u.vpixsize)) {
7243                                 dgap_err("bad number for vpixsize");
7244                                 return -1;
7245                         }
7246                         break;
7247                 }
7248         }
7249 }
7250
7251 /*
7252  * dgap_sindex: much like index(), but it looks for a match of any character in
7253  * the group, and returns that position.  If the first character is a ^, then
7254  * this will match the first occurrence not in that group.
7255  */
7256 static char *dgap_sindex(char *string, char *group)
7257 {
7258         char    *ptr;
7259
7260         if (!string || !group)
7261                 return (char *) NULL;
7262
7263         if (*group == '^') {
7264                 group++;
7265                 for (; *string; string++) {
7266                         for (ptr = group; *ptr; ptr++) {
7267                                 if (*ptr == *string)
7268                                         break;
7269                         }
7270                         if (*ptr == '\0')
7271                                 return string;
7272                 }
7273         } else {
7274                 for (; *string; string++) {
7275                         for (ptr = group; *ptr; ptr++) {
7276                                 if (*ptr == *string)
7277                                         return string;
7278                         }
7279                 }
7280         }
7281
7282         return (char *) NULL;
7283 }
7284
7285 /*
7286  * Get a token from the input file; return 0 if end of file is reached
7287  */
7288 static int dgap_gettok(char **in, struct cnode *p)
7289 {
7290         char    *w;
7291         struct toklist *t;
7292
7293         if (strstr(dgap_cword, "boar")) {
7294                 w = dgap_getword(in);
7295                 snprintf(dgap_cword, MAXCWORD, "%s", w);
7296                 for (t = dgap_tlist; t->token != 0; t++) {
7297                         if (!strcmp(w, t->string))
7298                                 return t->token;
7299                 }
7300                 dgap_err("board !!type not specified");
7301                 return 1;
7302         } else {
7303                 while ((w = dgap_getword(in))) {
7304                         snprintf(dgap_cword, MAXCWORD, "%s", w);
7305                         for (t = dgap_tlist; t->token != 0; t++) {
7306                                 if (!strcmp(w, t->string))
7307                                         return t->token;
7308                         }
7309                 }
7310                 return 0;
7311         }
7312 }
7313
7314 /*
7315  * get a word from the input stream, also keep track of current line number.
7316  * words are separated by whitespace.
7317  */
7318 static char *dgap_getword(char **in)
7319 {
7320         char *ret_ptr = *in;
7321
7322         char *ptr = dgap_sindex(*in, " \t\n");
7323
7324         /* If no word found, return null */
7325         if (!ptr)
7326                 return NULL;
7327
7328         /* Mark new location for our buffer */
7329         *ptr = '\0';
7330         *in = ptr + 1;
7331
7332         /* Eat any extra spaces/tabs/newlines that might be present */
7333         while (*in && **in && ((**in == ' ') ||
7334                                (**in == '\t') ||
7335                                (**in == '\n'))) {
7336                 **in = '\0';
7337                 *in = *in + 1;
7338         }
7339
7340         return ret_ptr;
7341 }
7342
7343 /*
7344  * print an error message, giving the line number in the file where
7345  * the error occurred.
7346  */
7347 static void dgap_err(char *s)
7348 {
7349         pr_err("dgap: parse: %s\n", s);
7350 }
7351
7352 /*
7353  * allocate a new configuration node of type t
7354  */
7355 static struct cnode *dgap_newnode(int t)
7356 {
7357         struct cnode *n;
7358
7359         n = kmalloc(sizeof(struct cnode), GFP_ATOMIC);
7360         if (n != NULL) {
7361                 memset((char *)n, 0, sizeof(struct cnode));
7362                 n->type = t;
7363         }
7364         return n;
7365 }
7366
7367 /*
7368  * dgap_checknode: see if all the necessary info has been supplied for a node
7369  * before creating the next node.
7370  */
7371 static int dgap_checknode(struct cnode *p)
7372 {
7373         switch (p->type) {
7374         case BNODE:
7375                 if (p->u.board.v_type == 0) {
7376                         dgap_err("board type !not specified");
7377                         return 1;
7378                 }
7379
7380                 return 0;
7381
7382         case LNODE:
7383                 if (p->u.line.v_speed == 0) {
7384                         dgap_err("line speed not specified");
7385                         return 1;
7386                 }
7387                 return 0;
7388
7389         case CNODE:
7390                 if (p->u.conc.v_type == 0) {
7391                         dgap_err("concentrator type not specified");
7392                         return 1;
7393                 }
7394                 if (p->u.conc.v_speed == 0) {
7395                         dgap_err("concentrator line speed not specified");
7396                         return 1;
7397                 }
7398                 if (p->u.conc.v_nport == 0) {
7399                         dgap_err("number of ports on concentrator not specified");
7400                         return 1;
7401                 }
7402                 if (p->u.conc.v_id == 0) {
7403                         dgap_err("concentrator id letter not specified");
7404                         return 1;
7405                 }
7406                 return 0;
7407
7408         case MNODE:
7409                 if (p->u.module.v_type == 0) {
7410                         dgap_err("EBI module type not specified");
7411                         return 1;
7412                 }
7413                 if (p->u.module.v_nport == 0) {
7414                         dgap_err("number of ports on EBI module not specified");
7415                         return 1;
7416                 }
7417                 if (p->u.module.v_id == 0) {
7418                         dgap_err("EBI module id letter not specified");
7419                         return 1;
7420                 }
7421                 return 0;
7422         }
7423         return 0;
7424 }
7425
7426 /*
7427  * save a string somewhere
7428  */
7429 static char     *dgap_savestring(char *s)
7430 {
7431         char    *p;
7432
7433         p = kmalloc(strlen(s) + 1, GFP_ATOMIC);
7434         if (p)
7435                 strcpy(p, s);
7436         return p;
7437 }
7438
7439 /*
7440  * Given a board pointer, returns whether we should use interrupts or not.
7441  */
7442 static uint dgap_config_get_useintr(struct board_t *bd)
7443 {
7444         struct cnode *p = NULL;
7445
7446         if (!bd)
7447                 return 0;
7448
7449         for (p = bd->bd_config; p; p = p->next) {
7450                 switch (p->type) {
7451                 case INTRNODE:
7452                         /*
7453                          * check for pcxr types.
7454                          */
7455                         return p->u.useintr;
7456                 default:
7457                         break;
7458                 }
7459         }
7460
7461         /* If not found, then don't turn on interrupts. */
7462         return 0;
7463 }
7464
7465 /*
7466  * Given a board pointer, returns whether we turn on altpin or not.
7467  */
7468 static uint dgap_config_get_altpin(struct board_t *bd)
7469 {
7470         struct cnode *p = NULL;
7471
7472         if (!bd)
7473                 return 0;
7474
7475         for (p = bd->bd_config; p; p = p->next) {
7476                 switch (p->type) {
7477                 case ANODE:
7478                         /*
7479                          * check for pcxr types.
7480                          */
7481                         return p->u.altpin;
7482                 default:
7483                         break;
7484                 }
7485         }
7486
7487         /* If not found, then don't turn on interrupts. */
7488         return 0;
7489 }
7490
7491 /*
7492  * Given a specific type of board, if found, detached link and
7493  * returns the first occurrence in the list.
7494  */
7495 static struct cnode *dgap_find_config(int type, int bus, int slot)
7496 {
7497         struct cnode *p, *prev = NULL, *prev2 = NULL, *found = NULL;
7498
7499         p = &dgap_head;
7500
7501         while (p->next != NULL) {
7502                 prev = p;
7503                 p = p->next;
7504
7505                 if (p->type == BNODE) {
7506
7507                         if (p->u.board.type == type) {
7508
7509                                 if (p->u.board.v_pcibus &&
7510                                     p->u.board.pcibus != bus)
7511                                         continue;
7512                                 if (p->u.board.v_pcislot &&
7513                                     p->u.board.pcislot != slot)
7514                                         continue;
7515
7516                                 found = p;
7517                                 /*
7518                                  * Keep walking thru the list till we
7519                                  * find the next board.
7520                                  */
7521                                 while (p->next != NULL) {
7522                                         prev2 = p;
7523                                         p = p->next;
7524                                         if (p->type == BNODE) {
7525
7526                                                 /*
7527                                                  * Mark the end of our 1 board
7528                                                  * chain of configs.
7529                                                  */
7530                                                 prev2->next = NULL;
7531
7532                                                 /*
7533                                                  * Link the "next" board to the
7534                                                  * previous board, effectively
7535                                                  * "unlinking" our board from
7536                                                  * the main config.
7537                                                  */
7538                                                 prev->next = p;
7539
7540                                                 return found;
7541                                         }
7542                                 }
7543                                 /*
7544                                  * It must be the last board in the list.
7545                                  */
7546                                 prev->next = NULL;
7547                                 return found;
7548                         }
7549                 }
7550         }
7551         return NULL;
7552 }
7553
7554 /*
7555  * Given a board pointer, walks the config link, counting up
7556  * all ports user specified should be on the board.
7557  * (This does NOT mean they are all actually present right now tho)
7558  */
7559 static uint dgap_config_get_num_prts(struct board_t *bd)
7560 {
7561         int count = 0;
7562         struct cnode *p = NULL;
7563
7564         if (!bd)
7565                 return 0;
7566
7567         for (p = bd->bd_config; p; p = p->next) {
7568
7569                 switch (p->type) {
7570                 case BNODE:
7571                         /*
7572                          * check for pcxr types.
7573                          */
7574                         if (p->u.board.type > EPCFE)
7575                                 count += p->u.board.nport;
7576                         break;
7577                 case CNODE:
7578                         count += p->u.conc.nport;
7579                         break;
7580                 case MNODE:
7581                         count += p->u.module.nport;
7582                         break;
7583                 }
7584         }
7585         return count;
7586 }
7587
7588 static char *dgap_create_config_string(struct board_t *bd, char *string)
7589 {
7590         char *ptr = string;
7591         struct cnode *p = NULL;
7592         struct cnode *q = NULL;
7593         int speed;
7594
7595         if (!bd) {
7596                 *ptr = 0xff;
7597                 return string;
7598         }
7599
7600         for (p = bd->bd_config; p; p = p->next) {
7601
7602                 switch (p->type) {
7603                 case LNODE:
7604                         *ptr = '\0';
7605                         ptr++;
7606                         *ptr = p->u.line.speed;
7607                         ptr++;
7608                         break;
7609                 case CNODE:
7610                         /*
7611                          * Because the EPC/con concentrators can have EM modules
7612                          * hanging off of them, we have to walk ahead in the
7613                          * list and keep adding the number of ports on each EM
7614                          * to the config. UGH!
7615                          */
7616                         speed = p->u.conc.speed;
7617                         q = p->next;
7618                         if ((q != NULL) && (q->type == MNODE)) {
7619                                 *ptr = (p->u.conc.nport + 0x80);
7620                                 ptr++;
7621                                 p = q;
7622                                 while ((q->next != NULL) &&
7623                                        (q->next->type) == MNODE) {
7624
7625                                         *ptr = (q->u.module.nport + 0x80);
7626                                         ptr++;
7627                                         p = q;
7628                                         q = q->next;
7629                                 }
7630                                 *ptr = q->u.module.nport;
7631                                 ptr++;
7632                         } else {
7633                                 *ptr = p->u.conc.nport;
7634                                 ptr++;
7635                         }
7636
7637                         *ptr = speed;
7638                         ptr++;
7639                         break;
7640                 }
7641         }
7642
7643         *ptr = 0xff;
7644         return string;
7645 }