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