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