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