staging: dgap: Remove unnecessary test on count
[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         n = count;
2735
2736         /*
2737          * If the write wraps over the top of the circular buffer,
2738          * move the portion up to the wrap point, and reset the
2739          * pointers to the bottom.
2740          */
2741         remain = ch->ch_tstart + ch->ch_tsize - head;
2742
2743         if (n >= remain) {
2744                 n -= remain;
2745                 vaddr = ch->ch_taddr + head;
2746
2747                 memcpy_toio(vaddr, (u8 *) buf, remain);
2748
2749                 head = ch->ch_tstart;
2750                 buf += remain;
2751         }
2752
2753         if (n > 0) {
2754
2755                 /*
2756                  * Move rest of data.
2757                  */
2758                 vaddr = ch->ch_taddr + head;
2759                 remain = n;
2760
2761                 memcpy_toio(vaddr, (u8 *) buf, remain);
2762                 head += remain;
2763
2764         }
2765
2766         if (count) {
2767                 ch->ch_txcount += count;
2768                 head &= tmask;
2769                 writew(head, &(bs->tx_head));
2770         }
2771
2772         dgap_set_firmware_event(un, UN_LOW | UN_EMPTY);
2773
2774         /*
2775          * If this is the print device, and the
2776          * printer is still on, we need to turn it
2777          * off before going idle.  If the buffer is
2778          * non-empty, wait until it goes empty.
2779          * Otherwise turn it off right now.
2780          */
2781         if ((un->un_type == DGAP_PRINT) && (ch->ch_flags & CH_PRON)) {
2782                 tail = readw(&(bs->tx_tail)) & tmask;
2783
2784                 if (tail != head) {
2785                         un->un_flags |= UN_EMPTY;
2786                         writeb(1, &(bs->iempty));
2787                 } else {
2788                         dgap_wmove(ch, ch->ch_digi.digi_offstr,
2789                                 (int) ch->ch_digi.digi_offlen);
2790                         head = readw(&(bs->tx_head)) & tmask;
2791                         ch->ch_flags &= ~CH_PRON;
2792                 }
2793         }
2794
2795         /* Update printer buffer empty time. */
2796         if ((un->un_type == DGAP_PRINT) && (ch->ch_digi.digi_maxcps > 0)
2797             && (ch->ch_digi.digi_bufsize > 0)) {
2798                 ch->ch_cpstime += (HZ * count) / ch->ch_digi.digi_maxcps;
2799         }
2800
2801         spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
2802
2803         return count;
2804 }
2805
2806 /*
2807  * Return modem signals to ld.
2808  */
2809 static int dgap_tty_tiocmget(struct tty_struct *tty)
2810 {
2811         struct channel_t *ch;
2812         struct un_t *un;
2813         int result = -EIO;
2814         u8 mstat = 0;
2815         ulong lock_flags;
2816
2817         if (!tty || tty->magic != TTY_MAGIC)
2818                 return result;
2819
2820         un = tty->driver_data;
2821         if (!un || un->magic != DGAP_UNIT_MAGIC)
2822                 return result;
2823
2824         ch = un->un_ch;
2825         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
2826                 return result;
2827
2828         spin_lock_irqsave(&ch->ch_lock, lock_flags);
2829
2830         mstat = readb(&(ch->ch_bs->m_stat));
2831         /* Append any outbound signals that might be pending... */
2832         mstat |= ch->ch_mostat;
2833
2834         spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
2835
2836         result = 0;
2837
2838         if (mstat & D_DTR(ch))
2839                 result |= TIOCM_DTR;
2840         if (mstat & D_RTS(ch))
2841                 result |= TIOCM_RTS;
2842         if (mstat & D_CTS(ch))
2843                 result |= TIOCM_CTS;
2844         if (mstat & D_DSR(ch))
2845                 result |= TIOCM_DSR;
2846         if (mstat & D_RI(ch))
2847                 result |= TIOCM_RI;
2848         if (mstat & D_CD(ch))
2849                 result |= TIOCM_CD;
2850
2851         return result;
2852 }
2853
2854 /*
2855  * dgap_tty_tiocmset()
2856  *
2857  * Set modem signals, called by ld.
2858  */
2859 static int dgap_tty_tiocmset(struct tty_struct *tty,
2860                 unsigned int set, unsigned int clear)
2861 {
2862         struct board_t *bd;
2863         struct channel_t *ch;
2864         struct un_t *un;
2865         int ret = -EIO;
2866         ulong lock_flags;
2867         ulong lock_flags2;
2868
2869         if (!tty || tty->magic != TTY_MAGIC)
2870                 return ret;
2871
2872         un = tty->driver_data;
2873         if (!un || un->magic != DGAP_UNIT_MAGIC)
2874                 return ret;
2875
2876         ch = un->un_ch;
2877         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
2878                 return ret;
2879
2880         bd = ch->ch_bd;
2881         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
2882                 return ret;
2883
2884         spin_lock_irqsave(&bd->bd_lock, lock_flags);
2885         spin_lock_irqsave(&ch->ch_lock, lock_flags2);
2886
2887         if (set & TIOCM_RTS) {
2888                 ch->ch_mforce |= D_RTS(ch);
2889                 ch->ch_mval   |= D_RTS(ch);
2890         }
2891
2892         if (set & TIOCM_DTR) {
2893                 ch->ch_mforce |= D_DTR(ch);
2894                 ch->ch_mval   |= D_DTR(ch);
2895         }
2896
2897         if (clear & TIOCM_RTS) {
2898                 ch->ch_mforce |= D_RTS(ch);
2899                 ch->ch_mval   &= ~(D_RTS(ch));
2900         }
2901
2902         if (clear & TIOCM_DTR) {
2903                 ch->ch_mforce |= D_DTR(ch);
2904                 ch->ch_mval   &= ~(D_DTR(ch));
2905         }
2906
2907         dgap_param(tty);
2908
2909         spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
2910         spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
2911
2912         return 0;
2913 }
2914
2915 /*
2916  * dgap_tty_send_break()
2917  *
2918  * Send a Break, called by ld.
2919  */
2920 static int dgap_tty_send_break(struct tty_struct *tty, int msec)
2921 {
2922         struct board_t *bd;
2923         struct channel_t *ch;
2924         struct un_t *un;
2925         int ret = -EIO;
2926         ulong lock_flags;
2927         ulong lock_flags2;
2928
2929         if (!tty || tty->magic != TTY_MAGIC)
2930                 return ret;
2931
2932         un = tty->driver_data;
2933         if (!un || un->magic != DGAP_UNIT_MAGIC)
2934                 return ret;
2935
2936         ch = un->un_ch;
2937         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
2938                 return ret;
2939
2940         bd = ch->ch_bd;
2941         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
2942                 return ret;
2943
2944         switch (msec) {
2945         case -1:
2946                 msec = 0xFFFF;
2947                 break;
2948         case 0:
2949                 msec = 1;
2950                 break;
2951         default:
2952                 msec /= 10;
2953                 break;
2954         }
2955
2956         spin_lock_irqsave(&bd->bd_lock, lock_flags);
2957         spin_lock_irqsave(&ch->ch_lock, lock_flags2);
2958 #if 0
2959         dgap_cmdw(ch, SBREAK, (u16) SBREAK_TIME, 0);
2960 #endif
2961         dgap_cmdw(ch, SBREAK, (u16) msec, 0);
2962
2963         spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
2964         spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
2965
2966         return 0;
2967 }
2968
2969 /*
2970  * dgap_tty_wait_until_sent()
2971  *
2972  * wait until data has been transmitted, called by ld.
2973  */
2974 static void dgap_tty_wait_until_sent(struct tty_struct *tty, int timeout)
2975 {
2976         dgap_wait_for_drain(tty);
2977 }
2978
2979 /*
2980  * dgap_send_xchar()
2981  *
2982  * send a high priority character, called by ld.
2983  */
2984 static void dgap_tty_send_xchar(struct tty_struct *tty, char c)
2985 {
2986         struct board_t *bd;
2987         struct channel_t *ch;
2988         struct un_t *un;
2989         ulong lock_flags;
2990         ulong lock_flags2;
2991
2992         if (!tty || tty->magic != TTY_MAGIC)
2993                 return;
2994
2995         un = tty->driver_data;
2996         if (!un || un->magic != DGAP_UNIT_MAGIC)
2997                 return;
2998
2999         ch = un->un_ch;
3000         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
3001                 return;
3002
3003         bd = ch->ch_bd;
3004         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
3005                 return;
3006
3007         spin_lock_irqsave(&bd->bd_lock, lock_flags);
3008         spin_lock_irqsave(&ch->ch_lock, lock_flags2);
3009
3010         /*
3011          * This is technically what we should do.
3012          * However, the NIST tests specifically want
3013          * to see each XON or XOFF character that it
3014          * sends, so lets just send each character
3015          * by hand...
3016          */
3017 #if 0
3018         if (c == STOP_CHAR(tty))
3019                 dgap_cmdw(ch, RPAUSE, 0, 0);
3020         else if (c == START_CHAR(tty))
3021                 dgap_cmdw(ch, RRESUME, 0, 0);
3022         else
3023                 dgap_wmove(ch, &c, 1);
3024 #else
3025         dgap_wmove(ch, &c, 1);
3026 #endif
3027
3028         spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
3029         spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
3030
3031         return;
3032 }
3033
3034 /*
3035  * Return modem signals to ld.
3036  */
3037 static int dgap_get_modem_info(struct channel_t *ch, unsigned int __user *value)
3038 {
3039         int result = 0;
3040         u8 mstat = 0;
3041         ulong lock_flags;
3042         int rc = 0;
3043
3044         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
3045                 return -ENXIO;
3046
3047         spin_lock_irqsave(&ch->ch_lock, lock_flags);
3048
3049         mstat = readb(&(ch->ch_bs->m_stat));
3050         /* Append any outbound signals that might be pending... */
3051         mstat |= ch->ch_mostat;
3052
3053         spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
3054
3055         result = 0;
3056
3057         if (mstat & D_DTR(ch))
3058                 result |= TIOCM_DTR;
3059         if (mstat & D_RTS(ch))
3060                 result |= TIOCM_RTS;
3061         if (mstat & D_CTS(ch))
3062                 result |= TIOCM_CTS;
3063         if (mstat & D_DSR(ch))
3064                 result |= TIOCM_DSR;
3065         if (mstat & D_RI(ch))
3066                 result |= TIOCM_RI;
3067         if (mstat & D_CD(ch))
3068                 result |= TIOCM_CD;
3069
3070         rc = put_user(result, value);
3071
3072         return rc;
3073 }
3074
3075 /*
3076  * dgap_set_modem_info()
3077  *
3078  * Set modem signals, called by ld.
3079  */
3080 static int dgap_set_modem_info(struct tty_struct *tty, unsigned int command,
3081                                 unsigned int __user *value)
3082 {
3083         struct board_t *bd;
3084         struct channel_t *ch;
3085         struct un_t *un;
3086         int ret = -ENXIO;
3087         unsigned int arg = 0;
3088         ulong lock_flags;
3089         ulong lock_flags2;
3090
3091         if (!tty || tty->magic != TTY_MAGIC)
3092                 return ret;
3093
3094         un = tty->driver_data;
3095         if (!un || un->magic != DGAP_UNIT_MAGIC)
3096                 return ret;
3097
3098         ch = un->un_ch;
3099         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
3100                 return ret;
3101
3102         bd = ch->ch_bd;
3103         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
3104                 return ret;
3105
3106         ret = get_user(arg, value);
3107         if (ret)
3108                 return ret;
3109
3110         switch (command) {
3111         case TIOCMBIS:
3112                 if (arg & TIOCM_RTS) {
3113                         ch->ch_mforce |= D_RTS(ch);
3114                         ch->ch_mval   |= D_RTS(ch);
3115                 }
3116
3117                 if (arg & TIOCM_DTR) {
3118                         ch->ch_mforce |= D_DTR(ch);
3119                         ch->ch_mval   |= D_DTR(ch);
3120                 }
3121
3122                 break;
3123
3124         case TIOCMBIC:
3125                 if (arg & TIOCM_RTS) {
3126                         ch->ch_mforce |= D_RTS(ch);
3127                         ch->ch_mval   &= ~(D_RTS(ch));
3128                 }
3129
3130                 if (arg & TIOCM_DTR) {
3131                         ch->ch_mforce |= D_DTR(ch);
3132                         ch->ch_mval   &= ~(D_DTR(ch));
3133                 }
3134
3135                 break;
3136
3137         case TIOCMSET:
3138                 ch->ch_mforce = D_DTR(ch)|D_RTS(ch);
3139
3140                 if (arg & TIOCM_RTS)
3141                         ch->ch_mval |= D_RTS(ch);
3142                 else
3143                         ch->ch_mval &= ~(D_RTS(ch));
3144
3145                 if (arg & TIOCM_DTR)
3146                         ch->ch_mval |= (D_DTR(ch));
3147                 else
3148                         ch->ch_mval &= ~(D_DTR(ch));
3149
3150                 break;
3151
3152         default:
3153                 return -EINVAL;
3154         }
3155
3156         spin_lock_irqsave(&bd->bd_lock, lock_flags);
3157         spin_lock_irqsave(&ch->ch_lock, lock_flags2);
3158
3159         dgap_param(tty);
3160
3161         spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
3162         spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
3163
3164         return 0;
3165 }
3166
3167 /*
3168  * dgap_tty_digigeta()
3169  *
3170  * Ioctl to get the information for ditty.
3171  *
3172  *
3173  *
3174  */
3175 static int dgap_tty_digigeta(struct tty_struct *tty,
3176                                 struct digi_t __user *retinfo)
3177 {
3178         struct channel_t *ch;
3179         struct un_t *un;
3180         struct digi_t tmp;
3181         ulong lock_flags;
3182
3183         if (!retinfo)
3184                 return -EFAULT;
3185
3186         if (!tty || tty->magic != TTY_MAGIC)
3187                 return -EFAULT;
3188
3189         un = tty->driver_data;
3190         if (!un || un->magic != DGAP_UNIT_MAGIC)
3191                 return -EFAULT;
3192
3193         ch = un->un_ch;
3194         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
3195                 return -EFAULT;
3196
3197         memset(&tmp, 0, sizeof(tmp));
3198
3199         spin_lock_irqsave(&ch->ch_lock, lock_flags);
3200         memcpy(&tmp, &ch->ch_digi, sizeof(tmp));
3201         spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
3202
3203         if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
3204                 return -EFAULT;
3205
3206         return 0;
3207 }
3208
3209 /*
3210  * dgap_tty_digiseta()
3211  *
3212  * Ioctl to set the information for ditty.
3213  *
3214  *
3215  *
3216  */
3217 static int dgap_tty_digiseta(struct tty_struct *tty,
3218                                 struct digi_t __user *new_info)
3219 {
3220         struct board_t *bd;
3221         struct channel_t *ch;
3222         struct un_t *un;
3223         struct digi_t new_digi;
3224         ulong   lock_flags = 0;
3225         unsigned long lock_flags2;
3226
3227         if (!tty || tty->magic != TTY_MAGIC)
3228                 return -EFAULT;
3229
3230         un = tty->driver_data;
3231         if (!un || un->magic != DGAP_UNIT_MAGIC)
3232                 return -EFAULT;
3233
3234         ch = un->un_ch;
3235         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
3236                 return -EFAULT;
3237
3238         bd = ch->ch_bd;
3239         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
3240                 return -EFAULT;
3241
3242         if (copy_from_user(&new_digi, new_info, sizeof(struct digi_t)))
3243                 return -EFAULT;
3244
3245         spin_lock_irqsave(&bd->bd_lock, lock_flags);
3246         spin_lock_irqsave(&ch->ch_lock, lock_flags2);
3247
3248         memcpy(&ch->ch_digi, &new_digi, sizeof(struct digi_t));
3249
3250         if (ch->ch_digi.digi_maxcps < 1)
3251                 ch->ch_digi.digi_maxcps = 1;
3252
3253         if (ch->ch_digi.digi_maxcps > 10000)
3254                 ch->ch_digi.digi_maxcps = 10000;
3255
3256         if (ch->ch_digi.digi_bufsize < 10)
3257                 ch->ch_digi.digi_bufsize = 10;
3258
3259         if (ch->ch_digi.digi_maxchar < 1)
3260                 ch->ch_digi.digi_maxchar = 1;
3261
3262         if (ch->ch_digi.digi_maxchar > ch->ch_digi.digi_bufsize)
3263                 ch->ch_digi.digi_maxchar = ch->ch_digi.digi_bufsize;
3264
3265         if (ch->ch_digi.digi_onlen > DIGI_PLEN)
3266                 ch->ch_digi.digi_onlen = DIGI_PLEN;
3267
3268         if (ch->ch_digi.digi_offlen > DIGI_PLEN)
3269                 ch->ch_digi.digi_offlen = DIGI_PLEN;
3270
3271         dgap_param(tty);
3272
3273         spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
3274         spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
3275
3276         return 0;
3277 }
3278
3279 /*
3280  * dgap_tty_digigetedelay()
3281  *
3282  * Ioctl to get the current edelay setting.
3283  *
3284  *
3285  *
3286  */
3287 static int dgap_tty_digigetedelay(struct tty_struct *tty, int __user *retinfo)
3288 {
3289         struct channel_t *ch;
3290         struct un_t *un;
3291         int tmp;
3292         ulong lock_flags;
3293
3294         if (!retinfo)
3295                 return -EFAULT;
3296
3297         if (!tty || tty->magic != TTY_MAGIC)
3298                 return -EFAULT;
3299
3300         un = tty->driver_data;
3301         if (!un || un->magic != DGAP_UNIT_MAGIC)
3302                 return -EFAULT;
3303
3304         ch = un->un_ch;
3305         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
3306                 return -EFAULT;
3307
3308         memset(&tmp, 0, sizeof(tmp));
3309
3310         spin_lock_irqsave(&ch->ch_lock, lock_flags);
3311         tmp = readw(&(ch->ch_bs->edelay));
3312         spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
3313
3314         if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
3315                 return -EFAULT;
3316
3317         return 0;
3318 }
3319
3320 /*
3321  * dgap_tty_digisetedelay()
3322  *
3323  * Ioctl to set the EDELAY setting
3324  *
3325  */
3326 static int dgap_tty_digisetedelay(struct tty_struct *tty, int __user *new_info)
3327 {
3328         struct board_t *bd;
3329         struct channel_t *ch;
3330         struct un_t *un;
3331         int new_digi;
3332         ulong lock_flags;
3333         ulong lock_flags2;
3334
3335         if (!tty || tty->magic != TTY_MAGIC)
3336                 return -EFAULT;
3337
3338         un = tty->driver_data;
3339         if (!un || un->magic != DGAP_UNIT_MAGIC)
3340                 return -EFAULT;
3341
3342         ch = un->un_ch;
3343         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
3344                 return -EFAULT;
3345
3346         bd = ch->ch_bd;
3347         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
3348                 return -EFAULT;
3349
3350         if (copy_from_user(&new_digi, new_info, sizeof(int)))
3351                 return -EFAULT;
3352
3353         spin_lock_irqsave(&bd->bd_lock, lock_flags);
3354         spin_lock_irqsave(&ch->ch_lock, lock_flags2);
3355
3356         writew((u16) new_digi, &(ch->ch_bs->edelay));
3357
3358         dgap_param(tty);
3359
3360         spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
3361         spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
3362
3363         return 0;
3364 }
3365
3366 /*
3367  * dgap_tty_digigetcustombaud()
3368  *
3369  * Ioctl to get the current custom baud rate setting.
3370  */
3371 static int dgap_tty_digigetcustombaud(struct tty_struct *tty,
3372                                         int __user *retinfo)
3373 {
3374         struct channel_t *ch;
3375         struct un_t *un;
3376         int tmp;
3377         ulong lock_flags;
3378
3379         if (!retinfo)
3380                 return -EFAULT;
3381
3382         if (!tty || tty->magic != TTY_MAGIC)
3383                 return -EFAULT;
3384
3385         un = tty->driver_data;
3386         if (!un || un->magic != DGAP_UNIT_MAGIC)
3387                 return -EFAULT;
3388
3389         ch = un->un_ch;
3390         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
3391                 return -EFAULT;
3392
3393         memset(&tmp, 0, sizeof(tmp));
3394
3395         spin_lock_irqsave(&ch->ch_lock, lock_flags);
3396         tmp = dgap_get_custom_baud(ch);
3397         spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
3398
3399         if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
3400                 return -EFAULT;
3401
3402         return 0;
3403 }
3404
3405 /*
3406  * dgap_tty_digisetcustombaud()
3407  *
3408  * Ioctl to set the custom baud rate setting
3409  */
3410 static int dgap_tty_digisetcustombaud(struct tty_struct *tty,
3411                                         int __user *new_info)
3412 {
3413         struct board_t *bd;
3414         struct channel_t *ch;
3415         struct un_t *un;
3416         uint new_rate;
3417         ulong lock_flags;
3418         ulong lock_flags2;
3419
3420         if (!tty || tty->magic != TTY_MAGIC)
3421                 return -EFAULT;
3422
3423         un = tty->driver_data;
3424         if (!un || un->magic != DGAP_UNIT_MAGIC)
3425                 return -EFAULT;
3426
3427         ch = un->un_ch;
3428         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
3429                 return -EFAULT;
3430
3431         bd = ch->ch_bd;
3432         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
3433                 return -EFAULT;
3434
3435
3436         if (copy_from_user(&new_rate, new_info, sizeof(unsigned int)))
3437                 return -EFAULT;
3438
3439         if (bd->bd_flags & BD_FEP5PLUS) {
3440
3441                 spin_lock_irqsave(&bd->bd_lock, lock_flags);
3442                 spin_lock_irqsave(&ch->ch_lock, lock_flags2);
3443
3444                 ch->ch_custom_speed = new_rate;
3445
3446                 dgap_param(tty);
3447
3448                 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
3449                 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
3450         }
3451
3452         return 0;
3453 }
3454
3455 /*
3456  * dgap_set_termios()
3457  */
3458 static void dgap_tty_set_termios(struct tty_struct *tty,
3459                                 struct ktermios *old_termios)
3460 {
3461         struct board_t *bd;
3462         struct channel_t *ch;
3463         struct un_t *un;
3464         unsigned long lock_flags;
3465         unsigned long lock_flags2;
3466
3467         if (!tty || tty->magic != TTY_MAGIC)
3468                 return;
3469
3470         un = tty->driver_data;
3471         if (!un || un->magic != DGAP_UNIT_MAGIC)
3472                 return;
3473
3474         ch = un->un_ch;
3475         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
3476                 return;
3477
3478         bd = ch->ch_bd;
3479         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
3480                 return;
3481
3482         spin_lock_irqsave(&bd->bd_lock, lock_flags);
3483         spin_lock_irqsave(&ch->ch_lock, lock_flags2);
3484
3485         ch->ch_c_cflag   = tty->termios.c_cflag;
3486         ch->ch_c_iflag   = tty->termios.c_iflag;
3487         ch->ch_c_oflag   = tty->termios.c_oflag;
3488         ch->ch_c_lflag   = tty->termios.c_lflag;
3489         ch->ch_startc    = tty->termios.c_cc[VSTART];
3490         ch->ch_stopc     = tty->termios.c_cc[VSTOP];
3491
3492         dgap_carrier(ch);
3493         dgap_param(tty);
3494
3495         spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
3496         spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
3497 }
3498
3499 static void dgap_tty_throttle(struct tty_struct *tty)
3500 {
3501         struct board_t *bd;
3502         struct channel_t *ch;
3503         struct un_t *un;
3504         ulong   lock_flags;
3505         ulong   lock_flags2;
3506
3507         if (!tty || tty->magic != TTY_MAGIC)
3508                 return;
3509
3510         un = tty->driver_data;
3511         if (!un || un->magic != DGAP_UNIT_MAGIC)
3512                 return;
3513
3514         ch = un->un_ch;
3515         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
3516                 return;
3517
3518         bd = ch->ch_bd;
3519         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
3520                 return;
3521
3522         spin_lock_irqsave(&bd->bd_lock, lock_flags);
3523         spin_lock_irqsave(&ch->ch_lock, lock_flags2);
3524
3525         ch->ch_flags |= (CH_RXBLOCK);
3526 #if 1
3527         dgap_cmdw(ch, RPAUSE, 0, 0);
3528 #endif
3529
3530         spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
3531         spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
3532
3533 }
3534
3535 static void dgap_tty_unthrottle(struct tty_struct *tty)
3536 {
3537         struct board_t *bd;
3538         struct channel_t *ch;
3539         struct un_t *un;
3540         ulong   lock_flags;
3541         ulong   lock_flags2;
3542
3543         if (!tty || tty->magic != TTY_MAGIC)
3544                 return;
3545
3546         un = tty->driver_data;
3547         if (!un || un->magic != DGAP_UNIT_MAGIC)
3548                 return;
3549
3550         ch = un->un_ch;
3551         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
3552                 return;
3553
3554         bd = ch->ch_bd;
3555         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
3556                 return;
3557
3558         spin_lock_irqsave(&bd->bd_lock, lock_flags);
3559         spin_lock_irqsave(&ch->ch_lock, lock_flags2);
3560
3561         ch->ch_flags &= ~(CH_RXBLOCK);
3562
3563 #if 1
3564         dgap_cmdw(ch, RRESUME, 0, 0);
3565 #endif
3566
3567         spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
3568         spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
3569 }
3570
3571 static void dgap_tty_start(struct tty_struct *tty)
3572 {
3573         struct board_t *bd;
3574         struct channel_t *ch;
3575         struct un_t *un;
3576         ulong   lock_flags;
3577         ulong   lock_flags2;
3578
3579         if (!tty || tty->magic != TTY_MAGIC)
3580                 return;
3581
3582         un = tty->driver_data;
3583         if (!un || un->magic != DGAP_UNIT_MAGIC)
3584                 return;
3585
3586         ch = un->un_ch;
3587         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
3588                 return;
3589
3590         bd = ch->ch_bd;
3591         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
3592                 return;
3593
3594         spin_lock_irqsave(&bd->bd_lock, lock_flags);
3595         spin_lock_irqsave(&ch->ch_lock, lock_flags2);
3596
3597         dgap_cmdw(ch, RESUMETX, 0, 0);
3598
3599         spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
3600         spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
3601
3602 }
3603
3604 static void dgap_tty_stop(struct tty_struct *tty)
3605 {
3606         struct board_t *bd;
3607         struct channel_t *ch;
3608         struct un_t *un;
3609         ulong   lock_flags;
3610         ulong   lock_flags2;
3611
3612         if (!tty || tty->magic != TTY_MAGIC)
3613                 return;
3614
3615         un = tty->driver_data;
3616         if (!un || un->magic != DGAP_UNIT_MAGIC)
3617                 return;
3618
3619         ch = un->un_ch;
3620         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
3621                 return;
3622
3623         bd = ch->ch_bd;
3624         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
3625                 return;
3626
3627         spin_lock_irqsave(&bd->bd_lock, lock_flags);
3628         spin_lock_irqsave(&ch->ch_lock, lock_flags2);
3629
3630         dgap_cmdw(ch, PAUSETX, 0, 0);
3631
3632         spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
3633         spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
3634
3635 }
3636
3637 /*
3638  * dgap_tty_flush_chars()
3639  *
3640  * Flush the cook buffer
3641  *
3642  * Note to self, and any other poor souls who venture here:
3643  *
3644  * flush in this case DOES NOT mean dispose of the data.
3645  * instead, it means "stop buffering and send it if you
3646  * haven't already."  Just guess how I figured that out...   SRW 2-Jun-98
3647  *
3648  * It is also always called in interrupt context - JAR 8-Sept-99
3649  */
3650 static void dgap_tty_flush_chars(struct tty_struct *tty)
3651 {
3652         struct board_t *bd;
3653         struct channel_t *ch;
3654         struct un_t *un;
3655         ulong   lock_flags;
3656         ulong   lock_flags2;
3657
3658         if (!tty || tty->magic != TTY_MAGIC)
3659                 return;
3660
3661         un = tty->driver_data;
3662         if (!un || un->magic != DGAP_UNIT_MAGIC)
3663                 return;
3664
3665         ch = un->un_ch;
3666         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
3667                 return;
3668
3669         bd = ch->ch_bd;
3670         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
3671                 return;
3672
3673         spin_lock_irqsave(&bd->bd_lock, lock_flags);
3674         spin_lock_irqsave(&ch->ch_lock, lock_flags2);
3675
3676         /* TODO: Do something here */
3677
3678         spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
3679         spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
3680 }
3681
3682 /*
3683  * dgap_tty_flush_buffer()
3684  *
3685  * Flush Tx buffer (make in == out)
3686  */
3687 static void dgap_tty_flush_buffer(struct tty_struct *tty)
3688 {
3689         struct board_t *bd;
3690         struct channel_t *ch;
3691         struct un_t *un;
3692         ulong   lock_flags;
3693         ulong   lock_flags2;
3694         u16     head = 0;
3695
3696         if (!tty || tty->magic != TTY_MAGIC)
3697                 return;
3698
3699         un = tty->driver_data;
3700         if (!un || un->magic != DGAP_UNIT_MAGIC)
3701                 return;
3702
3703         ch = un->un_ch;
3704         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
3705                 return;
3706
3707         bd = ch->ch_bd;
3708         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
3709                 return;
3710
3711         spin_lock_irqsave(&bd->bd_lock, lock_flags);
3712         spin_lock_irqsave(&ch->ch_lock, lock_flags2);
3713
3714         ch->ch_flags &= ~CH_STOP;
3715         head = readw(&(ch->ch_bs->tx_head));
3716         dgap_cmdw(ch, FLUSHTX, (u16) head, 0);
3717         dgap_cmdw(ch, RESUMETX, 0, 0);
3718         if (ch->ch_tun.un_flags & (UN_LOW|UN_EMPTY)) {
3719                 ch->ch_tun.un_flags &= ~(UN_LOW|UN_EMPTY);
3720                 wake_up_interruptible(&ch->ch_tun.un_flags_wait);
3721         }
3722         if (ch->ch_pun.un_flags & (UN_LOW|UN_EMPTY)) {
3723                 ch->ch_pun.un_flags &= ~(UN_LOW|UN_EMPTY);
3724                 wake_up_interruptible(&ch->ch_pun.un_flags_wait);
3725         }
3726
3727         spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
3728         spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
3729         if (waitqueue_active(&tty->write_wait))
3730                 wake_up_interruptible(&tty->write_wait);
3731         tty_wakeup(tty);
3732 }
3733
3734 /*****************************************************************************
3735  *
3736  * The IOCTL function and all of its helpers
3737  *
3738  *****************************************************************************/
3739
3740 /*
3741  * dgap_tty_ioctl()
3742  *
3743  * The usual assortment of ioctl's
3744  */
3745 static int dgap_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
3746                 unsigned long arg)
3747 {
3748         struct board_t *bd;
3749         struct channel_t *ch;
3750         struct un_t *un;
3751         int rc;
3752         u16     head = 0;
3753         ulong   lock_flags = 0;
3754         ulong   lock_flags2 = 0;
3755         void __user *uarg = (void __user *) arg;
3756
3757         if (!tty || tty->magic != TTY_MAGIC)
3758                 return -ENODEV;
3759
3760         un = tty->driver_data;
3761         if (!un || un->magic != DGAP_UNIT_MAGIC)
3762                 return -ENODEV;
3763
3764         ch = un->un_ch;
3765         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
3766                 return -ENODEV;
3767
3768         bd = ch->ch_bd;
3769         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
3770                 return -ENODEV;
3771
3772         spin_lock_irqsave(&bd->bd_lock, lock_flags);
3773         spin_lock_irqsave(&ch->ch_lock, lock_flags2);
3774
3775         if (un->un_open_count <= 0) {
3776                 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
3777                 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
3778                 return -EIO;
3779         }
3780
3781         switch (cmd) {
3782
3783         /* Here are all the standard ioctl's that we MUST implement */
3784
3785         case TCSBRK:
3786                 /*
3787                  * TCSBRK is SVID version: non-zero arg --> no break
3788                  * this behaviour is exploited by tcdrain().
3789                  *
3790                  * According to POSIX.1 spec (7.2.2.1.2) breaks should be
3791                  * between 0.25 and 0.5 seconds so we'll ask for something
3792                  * in the middle: 0.375 seconds.
3793                  */
3794                 rc = tty_check_change(tty);
3795                 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
3796                 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
3797                 if (rc)
3798                         return rc;
3799
3800                 rc = dgap_wait_for_drain(tty);
3801
3802                 if (rc)
3803                         return -EINTR;
3804
3805                 spin_lock_irqsave(&bd->bd_lock, lock_flags);
3806                 spin_lock_irqsave(&ch->ch_lock, lock_flags2);
3807
3808                 if (((cmd == TCSBRK) && (!arg)) || (cmd == TCSBRKP))
3809                         dgap_cmdw(ch, SBREAK, (u16) SBREAK_TIME, 0);
3810
3811                 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
3812                 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
3813
3814                 return 0;
3815
3816         case TCSBRKP:
3817                 /* support for POSIX tcsendbreak()
3818
3819                  * According to POSIX.1 spec (7.2.2.1.2) breaks should be
3820                  * between 0.25 and 0.5 seconds so we'll ask for something
3821                  * in the middle: 0.375 seconds.
3822                  */
3823                 rc = tty_check_change(tty);
3824                 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
3825                 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
3826                 if (rc)
3827                         return rc;
3828
3829                 rc = dgap_wait_for_drain(tty);
3830                 if (rc)
3831                         return -EINTR;
3832
3833                 spin_lock_irqsave(&bd->bd_lock, lock_flags);
3834                 spin_lock_irqsave(&ch->ch_lock, lock_flags2);
3835
3836                 dgap_cmdw(ch, SBREAK, (u16) SBREAK_TIME, 0);
3837
3838                 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
3839                 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
3840
3841                 return 0;
3842
3843         case TIOCSBRK:
3844                 /*
3845                  * FEP5 doesn't support turning on a break unconditionally.
3846                  * The FEP5 device will stop sending a break automatically
3847                  * after the specified time value that was sent when turning on
3848                  * the break.
3849                  */
3850                 rc = tty_check_change(tty);
3851                 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
3852                 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
3853                 if (rc)
3854                         return rc;
3855
3856                 rc = dgap_wait_for_drain(tty);
3857                 if (rc)
3858                         return -EINTR;
3859
3860                 spin_lock_irqsave(&bd->bd_lock, lock_flags);
3861                 spin_lock_irqsave(&ch->ch_lock, lock_flags2);
3862
3863                 dgap_cmdw(ch, SBREAK, (u16) SBREAK_TIME, 0);
3864
3865                 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
3866                 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
3867
3868                 return 0;
3869
3870         case TIOCCBRK:
3871                 /*
3872                  * FEP5 doesn't support turning off a break unconditionally.
3873                  * The FEP5 device will stop sending a break automatically
3874                  * after the specified time value that was sent when turning on
3875                  * the break.
3876                  */
3877                 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
3878                 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
3879                 return 0;
3880
3881         case TIOCGSOFTCAR:
3882
3883                 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
3884                 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
3885
3886                 rc = put_user(C_CLOCAL(tty) ? 1 : 0,
3887                                 (unsigned long __user *) arg);
3888                 return rc;
3889
3890         case TIOCSSOFTCAR:
3891                 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
3892                 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
3893
3894                 rc = get_user(arg, (unsigned long __user *) arg);
3895                 if (rc)
3896                         return rc;
3897
3898                 spin_lock_irqsave(&bd->bd_lock, lock_flags);
3899                 spin_lock_irqsave(&ch->ch_lock, lock_flags2);
3900                 tty->termios.c_cflag = ((tty->termios.c_cflag & ~CLOCAL) |
3901                                                 (arg ? CLOCAL : 0));
3902                 dgap_param(tty);
3903                 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
3904                 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
3905
3906                 return 0;
3907
3908         case TIOCMGET:
3909                 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
3910                 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
3911                 return dgap_get_modem_info(ch, uarg);
3912
3913         case TIOCMBIS:
3914         case TIOCMBIC:
3915         case TIOCMSET:
3916                 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
3917                 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
3918                 return dgap_set_modem_info(tty, cmd, uarg);
3919
3920                 /*
3921                  * Here are any additional ioctl's that we want to implement
3922                  */
3923
3924         case TCFLSH:
3925                 /*
3926                  * The linux tty driver doesn't have a flush
3927                  * input routine for the driver, assuming all backed
3928                  * up data is in the line disc. buffers.  However,
3929                  * we all know that's not the case.  Here, we
3930                  * act on the ioctl, but then lie and say we didn't
3931                  * so the line discipline will process the flush
3932                  * also.
3933                  */
3934                 rc = tty_check_change(tty);
3935                 if (rc) {
3936                         spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
3937                         spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
3938                         return rc;
3939                 }
3940
3941                 if ((arg == TCIFLUSH) || (arg == TCIOFLUSH)) {
3942                         if (!(un->un_type == DGAP_PRINT)) {
3943                                 head = readw(&(ch->ch_bs->rx_head));
3944                                 writew(head, &(ch->ch_bs->rx_tail));
3945                                 writeb(0, &(ch->ch_bs->orun));
3946                         }
3947                 }
3948
3949                 if ((arg != TCOFLUSH) && (arg != TCIOFLUSH)) {
3950                         /* pretend we didn't recognize this IOCTL */
3951                         spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
3952                         spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
3953
3954                         return -ENOIOCTLCMD;
3955                 }
3956
3957                 ch->ch_flags &= ~CH_STOP;
3958                 head = readw(&(ch->ch_bs->tx_head));
3959                 dgap_cmdw(ch, FLUSHTX, (u16) head, 0);
3960                 dgap_cmdw(ch, RESUMETX, 0, 0);
3961                 if (ch->ch_tun.un_flags & (UN_LOW|UN_EMPTY)) {
3962                         ch->ch_tun.un_flags &= ~(UN_LOW|UN_EMPTY);
3963                         wake_up_interruptible(&ch->ch_tun.un_flags_wait);
3964                 }
3965                 if (ch->ch_pun.un_flags & (UN_LOW|UN_EMPTY)) {
3966                         ch->ch_pun.un_flags &= ~(UN_LOW|UN_EMPTY);
3967                         wake_up_interruptible(&ch->ch_pun.un_flags_wait);
3968                 }
3969                 if (waitqueue_active(&tty->write_wait))
3970                         wake_up_interruptible(&tty->write_wait);
3971
3972                 /* Can't hold any locks when calling tty_wakeup! */
3973                 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
3974                 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
3975                 tty_wakeup(tty);
3976
3977                 /* pretend we didn't recognize this IOCTL */
3978                 return -ENOIOCTLCMD;
3979
3980         case TCSETSF:
3981         case TCSETSW:
3982                 /*
3983                  * The linux tty driver doesn't have a flush
3984                  * input routine for the driver, assuming all backed
3985                  * up data is in the line disc. buffers.  However,
3986                  * we all know that's not the case.  Here, we
3987                  * act on the ioctl, but then lie and say we didn't
3988                  * so the line discipline will process the flush
3989                  * also.
3990                  */
3991                 if (cmd == TCSETSF) {
3992                         /* flush rx */
3993                         ch->ch_flags &= ~CH_STOP;
3994                         head = readw(&(ch->ch_bs->rx_head));
3995                         writew(head, &(ch->ch_bs->rx_tail));
3996                 }
3997
3998                 /* now wait for all the output to drain */
3999                 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4000                 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
4001                 rc = dgap_wait_for_drain(tty);
4002                 if (rc)
4003                         return -EINTR;
4004
4005                 /* pretend we didn't recognize this */
4006                 return -ENOIOCTLCMD;
4007
4008         case TCSETAW:
4009
4010                 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4011                 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
4012                 rc = dgap_wait_for_drain(tty);
4013                 if (rc)
4014                         return -EINTR;
4015
4016                 /* pretend we didn't recognize this */
4017                 return -ENOIOCTLCMD;
4018
4019         case TCXONC:
4020                 /*
4021                  * The Linux Line Discipline (LD) would do this for us if we
4022                  * let it, but we have the special firmware options to do this
4023                  * the "right way" regardless of hardware or software flow
4024                  * control so we'll do it outselves instead of letting the LD
4025                  * do it.
4026                  */
4027                 rc = tty_check_change(tty);
4028                 if (rc) {
4029                         spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4030                         spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
4031                         return rc;
4032                 }
4033
4034                 switch (arg) {
4035
4036                 case TCOON:
4037                         spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4038                         spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
4039                         dgap_tty_start(tty);
4040                         return 0;
4041                 case TCOOFF:
4042                         spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4043                         spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
4044                         dgap_tty_stop(tty);
4045                         return 0;
4046                 case TCION:
4047                         spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4048                         spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
4049                         /* Make the ld do it */
4050                         return -ENOIOCTLCMD;
4051                 case TCIOFF:
4052                         spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4053                         spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
4054                         /* Make the ld do it */
4055                         return -ENOIOCTLCMD;
4056                 default:
4057                         spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4058                         spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
4059                         return -EINVAL;
4060                 }
4061
4062         case DIGI_GETA:
4063                 /* get information for ditty */
4064                 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4065                 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
4066                 return dgap_tty_digigeta(tty, uarg);
4067
4068         case DIGI_SETAW:
4069         case DIGI_SETAF:
4070
4071                 /* set information for ditty */
4072                 if (cmd == (DIGI_SETAW)) {
4073
4074                         spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4075                         spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
4076                         rc = dgap_wait_for_drain(tty);
4077                         if (rc)
4078                                 return -EINTR;
4079                         spin_lock_irqsave(&bd->bd_lock, lock_flags);
4080                         spin_lock_irqsave(&ch->ch_lock, lock_flags2);
4081                 } else
4082                         tty_ldisc_flush(tty);
4083                 /* fall thru */
4084
4085         case DIGI_SETA:
4086                 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4087                 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
4088                 return dgap_tty_digiseta(tty, uarg);
4089
4090         case DIGI_GEDELAY:
4091                 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4092                 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
4093                 return dgap_tty_digigetedelay(tty, uarg);
4094
4095         case DIGI_SEDELAY:
4096                 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4097                 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
4098                 return dgap_tty_digisetedelay(tty, uarg);
4099
4100         case DIGI_GETCUSTOMBAUD:
4101                 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4102                 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
4103                 return dgap_tty_digigetcustombaud(tty, uarg);
4104
4105         case DIGI_SETCUSTOMBAUD:
4106                 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4107                 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
4108                 return dgap_tty_digisetcustombaud(tty, uarg);
4109
4110         case DIGI_RESET_PORT:
4111                 dgap_firmware_reset_port(ch);
4112                 dgap_param(tty);
4113                 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4114                 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
4115                 return 0;
4116
4117         default:
4118                 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4119                 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
4120
4121                 return -ENOIOCTLCMD;
4122         }
4123 }
4124
4125 static int dgap_after_config_loaded(int board)
4126 {
4127         /*
4128          * Initialize KME waitqueues...
4129          */
4130         init_waitqueue_head(&(dgap_board[board]->kme_wait));
4131
4132         /*
4133          * allocate flip buffer for board.
4134          */
4135         dgap_board[board]->flipbuf = kmalloc(MYFLIPLEN, GFP_ATOMIC);
4136         if (!dgap_board[board]->flipbuf)
4137                 return -ENOMEM;
4138
4139         dgap_board[board]->flipflagbuf = kmalloc(MYFLIPLEN, GFP_ATOMIC);
4140         if (!dgap_board[board]->flipflagbuf) {
4141                 kfree(dgap_board[board]->flipbuf);
4142                 return -ENOMEM;
4143         }
4144
4145         return 0;
4146 }
4147
4148 /*
4149  * Create pr and tty device entries
4150  */
4151 static int dgap_tty_register_ports(struct board_t *brd)
4152 {
4153         struct channel_t *ch;
4154         int i;
4155
4156         brd->serial_ports = kcalloc(brd->nasync, sizeof(*brd->serial_ports),
4157                                         GFP_KERNEL);
4158         if (brd->serial_ports == NULL)
4159                 return -ENOMEM;
4160         for (i = 0; i < brd->nasync; i++)
4161                 tty_port_init(&brd->serial_ports[i]);
4162
4163         brd->printer_ports = kcalloc(brd->nasync, sizeof(*brd->printer_ports),
4164                                         GFP_KERNEL);
4165         if (brd->printer_ports == NULL) {
4166                 kfree(brd->serial_ports);
4167                 return -ENOMEM;
4168         }
4169         for (i = 0; i < brd->nasync; i++)
4170                 tty_port_init(&brd->printer_ports[i]);
4171
4172         ch = brd->channels[0];
4173         for (i = 0; i < brd->nasync; i++, ch = brd->channels[i]) {
4174
4175                 struct device *classp;
4176
4177                 classp = tty_port_register_device(&brd->serial_ports[i],
4178                                         brd->serial_driver,
4179                                         brd->firstminor + i, NULL);
4180
4181                 dgap_create_tty_sysfs(&ch->ch_tun, classp);
4182                 ch->ch_tun.un_sysfs = classp;
4183
4184                 classp = tty_port_register_device(&brd->printer_ports[i],
4185                                         brd->print_driver,
4186                                         brd->firstminor + i, NULL);
4187
4188                 dgap_create_tty_sysfs(&ch->ch_pun, classp);
4189                 ch->ch_pun.un_sysfs = classp;
4190         }
4191         dgap_create_ports_sysfiles(brd);
4192
4193         return 0;
4194 }
4195
4196 /*
4197  * Copies the BIOS code from the user to the board,
4198  * and starts the BIOS running.
4199  */
4200 static void dgap_do_bios_load(struct board_t *brd, const u8 *ubios, int len)
4201 {
4202         u8 __iomem *addr;
4203         uint offset;
4204         int i;
4205
4206         if (!brd || (brd->magic != DGAP_BOARD_MAGIC) || !brd->re_map_membase)
4207                 return;
4208
4209         addr = brd->re_map_membase;
4210
4211         /*
4212          * clear POST area
4213          */
4214         for (i = 0; i < 16; i++)
4215                 writeb(0, addr + POSTAREA + i);
4216
4217         /*
4218          * Download bios
4219          */
4220         offset = 0x1000;
4221         memcpy_toio(addr + offset, ubios, len);
4222
4223         writel(0x0bf00401, addr);
4224         writel(0, (addr + 4));
4225
4226         /* Clear the reset, and change states. */
4227         writeb(FEPCLR, brd->re_map_port);
4228 }
4229
4230 /*
4231  * Checks to see if the BIOS completed running on the card.
4232  */
4233 static int dgap_test_bios(struct board_t *brd)
4234 {
4235         u8 __iomem *addr;
4236         u16 word;
4237         u16 err1;
4238         u16 err2;
4239
4240         if (!brd || (brd->magic != DGAP_BOARD_MAGIC) || !brd->re_map_membase)
4241                 return -EINVAL;
4242
4243         addr = brd->re_map_membase;
4244         word = readw(addr + POSTAREA);
4245
4246         /*
4247          * It can take 5-6 seconds for a board to
4248          * pass the bios self test and post results.
4249          * Give it 10 seconds.
4250          */
4251         brd->wait_for_bios = 0;
4252         while (brd->wait_for_bios < 1000) {
4253                 /* Check to see if BIOS thinks board is good. (GD). */
4254                 if (word == *(u16 *) "GD")
4255                         return 0;
4256                 msleep_interruptible(10);
4257                 brd->wait_for_bios++;
4258                 word = readw(addr + POSTAREA);
4259         }
4260
4261         /* Gave up on board after too long of time taken */
4262         err1 = readw(addr + SEQUENCE);
4263         err2 = readw(addr + ERROR);
4264         pr_warn("dgap: %s failed diagnostics.  Error #(%x,%x).\n",
4265                 brd->name, err1, err2);
4266         brd->state = BOARD_FAILED;
4267         brd->dpastatus = BD_NOBIOS;
4268
4269         return -EIO;
4270 }
4271
4272 /*
4273  * Copies the FEP code from the user to the board,
4274  * and starts the FEP running.
4275  */
4276 static void dgap_do_fep_load(struct board_t *brd, const u8 *ufep, int len)
4277 {
4278         u8 __iomem *addr;
4279         uint offset;
4280
4281         if (!brd || (brd->magic != DGAP_BOARD_MAGIC) || !brd->re_map_membase)
4282                 return;
4283
4284         addr = brd->re_map_membase;
4285
4286         /*
4287          * Download FEP
4288          */
4289         offset = 0x1000;
4290         memcpy_toio(addr + offset, ufep, len);
4291
4292         /*
4293          * If board is a concentrator product, we need to give
4294          * it its config string describing how the concentrators look.
4295          */
4296         if ((brd->type == PCX) || (brd->type == PEPC)) {
4297                 u8 string[100];
4298                 u8 __iomem *config;
4299                 u8 *xconfig;
4300                 int i = 0;
4301
4302                 xconfig = dgap_create_config_string(brd, string);
4303
4304                 /* Write string to board memory */
4305                 config = addr + CONFIG;
4306                 for (; i < CONFIGSIZE; i++, config++, xconfig++) {
4307                         writeb(*xconfig, config);
4308                         if ((*xconfig & 0xff) == 0xff)
4309                                 break;
4310                 }
4311         }
4312
4313         writel(0xbfc01004, (addr + 0xc34));
4314         writel(0x3, (addr + 0xc30));
4315
4316 }
4317
4318 /*
4319  * Waits for the FEP to report thats its ready for us to use.
4320  */
4321 static int dgap_test_fep(struct board_t *brd)
4322 {
4323         u8 __iomem *addr;
4324         u16 word;
4325         u16 err1;
4326         u16 err2;
4327
4328         if (!brd || (brd->magic != DGAP_BOARD_MAGIC) || !brd->re_map_membase)
4329                 return -EINVAL;
4330
4331         addr = brd->re_map_membase;
4332         word = readw(addr + FEPSTAT);
4333
4334         /*
4335          * It can take 2-3 seconds for the FEP to
4336          * be up and running. Give it 5 secs.
4337          */
4338         brd->wait_for_fep = 0;
4339         while (brd->wait_for_fep < 500) {
4340                 /* Check to see if FEP is up and running now. */
4341                 if (word == *(u16 *) "OS") {
4342                         /*
4343                          * Check to see if the board can support FEP5+ commands.
4344                         */
4345                         word = readw(addr + FEP5_PLUS);
4346                         if (word == *(u16 *) "5A")
4347                                 brd->bd_flags |= BD_FEP5PLUS;
4348
4349                         return 0;
4350                 }
4351                 msleep_interruptible(10);
4352                 brd->wait_for_fep++;
4353                 word = readw(addr + FEPSTAT);
4354         }
4355
4356         /* Gave up on board after too long of time taken */
4357         err1 = readw(addr + SEQUENCE);
4358         err2 = readw(addr + ERROR);
4359         pr_warn("dgap: FEPOS for %s not functioning.  Error #(%x,%x).\n",
4360                 brd->name, err1, err2);
4361         brd->state = BOARD_FAILED;
4362         brd->dpastatus = BD_NOFEP;
4363
4364         return -EIO;
4365 }
4366
4367 /*
4368  * Physically forces the FEP5 card to reset itself.
4369  */
4370 static void dgap_do_reset_board(struct board_t *brd)
4371 {
4372         u8 check;
4373         u32 check1;
4374         u32 check2;
4375         int i = 0;
4376
4377         if (!brd || (brd->magic != DGAP_BOARD_MAGIC) ||
4378             !brd->re_map_membase || !brd->re_map_port)
4379                 return;
4380
4381         /* FEPRST does not vary among supported boards */
4382         writeb(FEPRST, brd->re_map_port);
4383
4384         for (i = 0; i <= 1000; i++) {
4385                 check = readb(brd->re_map_port) & 0xe;
4386                 if (check == FEPRST)
4387                         break;
4388                 udelay(10);
4389
4390         }
4391         if (i > 1000) {
4392                 pr_warn("dgap: Board not resetting...  Failing board.\n");
4393                 brd->state = BOARD_FAILED;
4394                 brd->dpastatus = BD_NOFEP;
4395                 return;
4396         }
4397
4398         /*
4399          * Make sure there really is memory out there.
4400          */
4401         writel(0xa55a3cc3, (brd->re_map_membase + LOWMEM));
4402         writel(0x5aa5c33c, (brd->re_map_membase + HIGHMEM));
4403         check1 = readl(brd->re_map_membase + LOWMEM);
4404         check2 = readl(brd->re_map_membase + HIGHMEM);
4405
4406         if ((check1 != 0xa55a3cc3) || (check2 != 0x5aa5c33c)) {
4407                 pr_warn("dgap: No memory at %p for board.\n",
4408                         brd->re_map_membase);
4409                 brd->state = BOARD_FAILED;
4410                 brd->dpastatus = BD_NOFEP;
4411                 return;
4412         }
4413
4414 }
4415
4416 #ifdef DIGI_CONCENTRATORS_SUPPORTED
4417 /*
4418  * Sends a concentrator image into the FEP5 board.
4419  */
4420 static void dgap_do_conc_load(struct board_t *brd, u8 *uaddr, int len)
4421 {
4422         char __iomem *vaddr;
4423         u16 offset = 0;
4424         struct downld_t *to_dp;
4425
4426         if (!brd || (brd->magic != DGAP_BOARD_MAGIC) || !brd->re_map_membase)
4427                 return;
4428
4429         vaddr = brd->re_map_membase;
4430
4431         offset = readw((u16 *) (vaddr + DOWNREQ));
4432         to_dp = (struct downld_t *) (vaddr + (int) offset);
4433         memcpy_toio(to_dp, uaddr, len);
4434
4435         /* Tell card we have data for it */
4436         writew(0, vaddr + (DOWNREQ));
4437
4438         brd->conc_dl_status = NO_PENDING_CONCENTRATOR_REQUESTS;
4439 }
4440 #endif
4441
4442 #define EXPANSION_ROM_SIZE      (64 * 1024)
4443 #define FEP5_ROM_MAGIC          (0xFEFFFFFF)
4444
4445 static void dgap_get_vpd(struct board_t *brd)
4446 {
4447         u32 magic;
4448         u32 base_offset;
4449         u16 rom_offset;
4450         u16 vpd_offset;
4451         u16 image_length;
4452         u16 i;
4453         u8 byte1;
4454         u8 byte2;
4455
4456         /*
4457          * Poke the magic number at the PCI Rom Address location.
4458          * If VPD is supported, the value read from that address
4459          * will be non-zero.
4460          */
4461         magic = FEP5_ROM_MAGIC;
4462         pci_write_config_dword(brd->pdev, PCI_ROM_ADDRESS, magic);
4463         pci_read_config_dword(brd->pdev, PCI_ROM_ADDRESS, &magic);
4464
4465         /* VPD not supported, bail */
4466         if (!magic)
4467                 return;
4468
4469         /*
4470          * To get to the OTPROM memory, we have to send the boards base
4471          * address or'ed with 1 into the PCI Rom Address location.
4472          */
4473         magic = brd->membase | 0x01;
4474         pci_write_config_dword(brd->pdev, PCI_ROM_ADDRESS, magic);
4475         pci_read_config_dword(brd->pdev, PCI_ROM_ADDRESS, &magic);
4476
4477         byte1 = readb(brd->re_map_membase);
4478         byte2 = readb(brd->re_map_membase + 1);
4479
4480         /*
4481          * If the board correctly swapped to the OTPROM memory,
4482          * the first 2 bytes (header) should be 0x55, 0xAA
4483          */
4484         if (byte1 == 0x55 && byte2 == 0xAA) {
4485
4486                 base_offset = 0;
4487
4488                 /*
4489                  * We have to run through all the OTPROM memory looking
4490                  * for the VPD offset.
4491                  */
4492                 while (base_offset <= EXPANSION_ROM_SIZE) {
4493
4494                         /*
4495                          * Lots of magic numbers here.
4496                          *
4497                          * The VPD offset is located inside the ROM Data
4498                          * Structure.
4499                          *
4500                          * We also have to remember the length of each
4501                          * ROM Data Structure, so we can "hop" to the next
4502                          * entry if the VPD isn't in the current
4503                          * ROM Data Structure.
4504                          */
4505                         rom_offset = readw(brd->re_map_membase +
4506                                                 base_offset + 0x18);
4507                         image_length = readw(brd->re_map_membase +
4508                                                 rom_offset + 0x10) * 512;
4509                         vpd_offset = readw(brd->re_map_membase +
4510                                                 rom_offset + 0x08);
4511
4512                         /* Found the VPD entry */
4513                         if (vpd_offset)
4514                                 break;
4515
4516                         /* We didn't find a VPD entry, go to next ROM entry. */
4517                         base_offset += image_length;
4518
4519                         byte1 = readb(brd->re_map_membase + base_offset);
4520                         byte2 = readb(brd->re_map_membase + base_offset + 1);
4521
4522                         /*
4523                          * If the new ROM offset doesn't have 0x55, 0xAA
4524                          * as its header, we have run out of ROM.
4525                          */
4526                         if (byte1 != 0x55 || byte2 != 0xAA)
4527                                 break;
4528                 }
4529
4530                 /*
4531                  * If we have a VPD offset, then mark the board
4532                  * as having a valid VPD, and copy VPDSIZE (512) bytes of
4533                  * that VPD to the buffer we have in our board structure.
4534                  */
4535                 if (vpd_offset) {
4536                         brd->bd_flags |= BD_HAS_VPD;
4537                         for (i = 0; i < VPDSIZE; i++) {
4538                                 brd->vpd[i] = readb(brd->re_map_membase +
4539                                                         vpd_offset + i);
4540                         }
4541                 }
4542         }
4543
4544         /*
4545          * We MUST poke the magic number at the PCI Rom Address location again.
4546          * This makes the card report the regular board memory back to us,
4547          * rather than the OTPROM memory.
4548          */
4549         magic = FEP5_ROM_MAGIC;
4550         pci_write_config_dword(brd->pdev, PCI_ROM_ADDRESS, magic);
4551 }
4552
4553 /*
4554  * Our board poller function.
4555  */
4556 static void dgap_poll_tasklet(unsigned long data)
4557 {
4558         struct board_t *bd = (struct board_t *) data;
4559         ulong  lock_flags;
4560         char __iomem *vaddr;
4561         u16 head, tail;
4562
4563         if (!bd || (bd->magic != DGAP_BOARD_MAGIC))
4564                 return;
4565
4566         if (bd->inhibit_poller)
4567                 return;
4568
4569         spin_lock_irqsave(&bd->bd_lock, lock_flags);
4570
4571         vaddr = bd->re_map_membase;
4572
4573         /*
4574          * If board is ready, parse deeper to see if there is anything to do.
4575          */
4576         if (bd->state == BOARD_READY) {
4577
4578                 struct ev_t __iomem *eaddr;
4579
4580                 if (!bd->re_map_membase) {
4581                         spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
4582                         return;
4583                 }
4584                 if (!bd->re_map_port) {
4585                         spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
4586                         return;
4587                 }
4588
4589                 if (!bd->nasync)
4590                         goto out;
4591
4592                 eaddr = (struct ev_t __iomem *) (vaddr + EVBUF);
4593
4594                 /* Get our head and tail */
4595                 head = readw(&(eaddr->ev_head));
4596                 tail = readw(&(eaddr->ev_tail));
4597
4598                 /*
4599                  * If there is an event pending. Go service it.
4600                  */
4601                 if (head != tail) {
4602                         spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
4603                         dgap_event(bd);
4604                         spin_lock_irqsave(&bd->bd_lock, lock_flags);
4605                 }
4606
4607 out:
4608                 /*
4609                  * If board is doing interrupts, ACK the interrupt.
4610                  */
4611                 if (bd && bd->intr_running)
4612                         readb(bd->re_map_port + 2);
4613
4614                 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
4615                 return;
4616         }
4617
4618         spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
4619 }
4620
4621 /*=======================================================================
4622  *
4623  *      dgap_cmdb - Sends a 2 byte command to the FEP.
4624  *
4625  *              ch      - Pointer to channel structure.
4626  *              cmd     - Command to be sent.
4627  *              byte1   - Integer containing first byte to be sent.
4628  *              byte2   - Integer containing second byte to be sent.
4629  *              ncmds   - Wait until ncmds or fewer cmds are left
4630  *                        in the cmd buffer before returning.
4631  *
4632  *=======================================================================*/
4633 static void dgap_cmdb(struct channel_t *ch, u8 cmd, u8 byte1,
4634                         u8 byte2, uint ncmds)
4635 {
4636         char __iomem    *vaddr;
4637         struct __iomem cm_t *cm_addr;
4638         uint            count;
4639         uint            n;
4640         u16             head;
4641         u16             tail;
4642
4643         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
4644                 return;
4645
4646         /*
4647          * Check if board is still alive.
4648          */
4649         if (ch->ch_bd->state == BOARD_FAILED)
4650                 return;
4651
4652         /*
4653          * Make sure the pointers are in range before
4654          * writing to the FEP memory.
4655          */
4656         vaddr = ch->ch_bd->re_map_membase;
4657
4658         if (!vaddr)
4659                 return;
4660
4661         cm_addr = (struct cm_t __iomem *) (vaddr + CMDBUF);
4662         head = readw(&(cm_addr->cm_head));
4663
4664         /*
4665          * Forget it if pointers out of range.
4666          */
4667         if (head >= (CMDMAX - CMDSTART) || (head & 03)) {
4668                 ch->ch_bd->state = BOARD_FAILED;
4669                 return;
4670         }
4671
4672         /*
4673          * Put the data in the circular command buffer.
4674          */
4675         writeb(cmd, (vaddr + head + CMDSTART + 0));
4676         writeb((u8) ch->ch_portnum, (vaddr + head + CMDSTART + 1));
4677         writeb(byte1, (vaddr + head + CMDSTART + 2));
4678         writeb(byte2, (vaddr + head + CMDSTART + 3));
4679
4680         head = (head + 4) & (CMDMAX - CMDSTART - 4);
4681
4682         writew(head, &(cm_addr->cm_head));
4683
4684         /*
4685          * Wait if necessary before updating the head
4686          * pointer to limit the number of outstanding
4687          * commands to the FEP.   If the time spent waiting
4688          * is outlandish, declare the FEP dead.
4689          */
4690         for (count = dgap_count ;;) {
4691
4692                 head = readw(&(cm_addr->cm_head));
4693                 tail = readw(&(cm_addr->cm_tail));
4694
4695                 n = (head - tail) & (CMDMAX - CMDSTART - 4);
4696
4697                 if (n <= ncmds * sizeof(struct cm_t))
4698                         break;
4699
4700                 if (--count == 0) {
4701                         ch->ch_bd->state = BOARD_FAILED;
4702                         return;
4703                 }
4704                 udelay(10);
4705         }
4706 }
4707
4708 /*=======================================================================
4709  *
4710  *      dgap_cmdw - Sends a 1 word command to the FEP.
4711  *
4712  *              ch      - Pointer to channel structure.
4713  *              cmd     - Command to be sent.
4714  *              word    - Integer containing word to be sent.
4715  *              ncmds   - Wait until ncmds or fewer cmds are left
4716  *                        in the cmd buffer before returning.
4717  *
4718  *=======================================================================*/
4719 static void dgap_cmdw(struct channel_t *ch, u8 cmd, u16 word, uint ncmds)
4720 {
4721         char __iomem    *vaddr;
4722         struct __iomem cm_t *cm_addr;
4723         uint            count;
4724         uint            n;
4725         u16             head;
4726         u16             tail;
4727
4728         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
4729                 return;
4730
4731         /*
4732          * Check if board is still alive.
4733          */
4734         if (ch->ch_bd->state == BOARD_FAILED)
4735                 return;
4736
4737         /*
4738          * Make sure the pointers are in range before
4739          * writing to the FEP memory.
4740          */
4741         vaddr = ch->ch_bd->re_map_membase;
4742         if (!vaddr)
4743                 return;
4744
4745         cm_addr = (struct cm_t __iomem *) (vaddr + CMDBUF);
4746         head = readw(&(cm_addr->cm_head));
4747
4748         /*
4749          * Forget it if pointers out of range.
4750          */
4751         if (head >= (CMDMAX - CMDSTART) || (head & 03)) {
4752                 ch->ch_bd->state = BOARD_FAILED;
4753                 return;
4754         }
4755
4756         /*
4757          * Put the data in the circular command buffer.
4758          */
4759         writeb(cmd, (vaddr + head + CMDSTART + 0));
4760         writeb((u8) ch->ch_portnum, (vaddr + head + CMDSTART + 1));
4761         writew((u16) word, (vaddr + head + CMDSTART + 2));
4762
4763         head = (head + 4) & (CMDMAX - CMDSTART - 4);
4764
4765         writew(head, &(cm_addr->cm_head));
4766
4767         /*
4768          * Wait if necessary before updating the head
4769          * pointer to limit the number of outstanding
4770          * commands to the FEP.   If the time spent waiting
4771          * is outlandish, declare the FEP dead.
4772          */
4773         for (count = dgap_count ;;) {
4774
4775                 head = readw(&(cm_addr->cm_head));
4776                 tail = readw(&(cm_addr->cm_tail));
4777
4778                 n = (head - tail) & (CMDMAX - CMDSTART - 4);
4779
4780                 if (n <= ncmds * sizeof(struct cm_t))
4781                         break;
4782
4783                 if (--count == 0) {
4784                         ch->ch_bd->state = BOARD_FAILED;
4785                         return;
4786                 }
4787                 udelay(10);
4788         }
4789 }
4790
4791 /*=======================================================================
4792  *
4793  *      dgap_cmdw_ext - Sends a extended word command to the FEP.
4794  *
4795  *              ch      - Pointer to channel structure.
4796  *              cmd     - Command to be sent.
4797  *              word    - Integer containing word to be sent.
4798  *              ncmds   - Wait until ncmds or fewer cmds are left
4799  *                        in the cmd buffer before returning.
4800  *
4801  *=======================================================================*/
4802 static void dgap_cmdw_ext(struct channel_t *ch, u16 cmd, u16 word, uint ncmds)
4803 {
4804         char __iomem    *vaddr;
4805         struct __iomem cm_t *cm_addr;
4806         uint            count;
4807         uint            n;
4808         u16             head;
4809         u16             tail;
4810
4811         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
4812                 return;
4813
4814         /*
4815          * Check if board is still alive.
4816          */
4817         if (ch->ch_bd->state == BOARD_FAILED)
4818                 return;
4819
4820         /*
4821          * Make sure the pointers are in range before
4822          * writing to the FEP memory.
4823          */
4824         vaddr = ch->ch_bd->re_map_membase;
4825         if (!vaddr)
4826                 return;
4827
4828         cm_addr = (struct cm_t __iomem *) (vaddr + CMDBUF);
4829         head = readw(&(cm_addr->cm_head));
4830
4831         /*
4832          * Forget it if pointers out of range.
4833          */
4834         if (head >= (CMDMAX - CMDSTART) || (head & 03)) {
4835                 ch->ch_bd->state = BOARD_FAILED;
4836                 return;
4837         }
4838
4839         /*
4840          * Put the data in the circular command buffer.
4841          */
4842
4843         /* Write an FF to tell the FEP that we want an extended command */
4844         writeb((u8) 0xff, (vaddr + head + CMDSTART + 0));
4845
4846         writeb((u8) ch->ch_portnum, (vaddr + head + CMDSTART + 1));
4847         writew((u16) cmd, (vaddr + head + CMDSTART + 2));
4848
4849         /*
4850          * If the second part of the command won't fit,
4851          * put it at the beginning of the circular buffer.
4852          */
4853         if (((head + 4) >= ((CMDMAX - CMDSTART)) || (head & 03)))
4854                 writew((u16) word, (vaddr + CMDSTART));
4855         else
4856                 writew((u16) word, (vaddr + head + CMDSTART + 4));
4857
4858         head = (head + 8) & (CMDMAX - CMDSTART - 4);
4859
4860         writew(head, &(cm_addr->cm_head));
4861
4862         /*
4863          * Wait if necessary before updating the head
4864          * pointer to limit the number of outstanding
4865          * commands to the FEP.   If the time spent waiting
4866          * is outlandish, declare the FEP dead.
4867          */
4868         for (count = dgap_count ;;) {
4869
4870                 head = readw(&(cm_addr->cm_head));
4871                 tail = readw(&(cm_addr->cm_tail));
4872
4873                 n = (head - tail) & (CMDMAX - CMDSTART - 4);
4874
4875                 if (n <= ncmds * sizeof(struct cm_t))
4876                         break;
4877
4878                 if (--count == 0) {
4879                         ch->ch_bd->state = BOARD_FAILED;
4880                         return;
4881                 }
4882                 udelay(10);
4883         }
4884 }
4885
4886 /*=======================================================================
4887  *
4888  *      dgap_wmove - Write data to FEP buffer.
4889  *
4890  *              ch      - Pointer to channel structure.
4891  *              buf     - Poiter to characters to be moved.
4892  *              cnt     - Number of characters to move.
4893  *
4894  *=======================================================================*/
4895 static void dgap_wmove(struct channel_t *ch, char *buf, uint cnt)
4896 {
4897         int    n;
4898         char   __iomem *taddr;
4899         struct bs_t __iomem *bs;
4900         u16    head;
4901
4902         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
4903                 return;
4904
4905         /*
4906          * Check parameters.
4907          */
4908         bs   = ch->ch_bs;
4909         head = readw(&(bs->tx_head));
4910
4911         /*
4912          * If pointers are out of range, just return.
4913          */
4914         if ((cnt > ch->ch_tsize) ||
4915             (unsigned)(head - ch->ch_tstart) >= ch->ch_tsize)
4916                 return;
4917
4918         /*
4919          * If the write wraps over the top of the circular buffer,
4920          * move the portion up to the wrap point, and reset the
4921          * pointers to the bottom.
4922          */
4923         n = ch->ch_tstart + ch->ch_tsize - head;
4924
4925         if (cnt >= n) {
4926                 cnt -= n;
4927                 taddr = ch->ch_taddr + head;
4928                 memcpy_toio(taddr, buf, n);
4929                 head = ch->ch_tstart;
4930                 buf += n;
4931         }
4932
4933         /*
4934          * Move rest of data.
4935          */
4936         taddr = ch->ch_taddr + head;
4937         n = cnt;
4938         memcpy_toio(taddr, buf, n);
4939         head += cnt;
4940
4941         writew(head, &(bs->tx_head));
4942 }
4943
4944 /*
4945  * Retrives the current custom baud rate from FEP memory,
4946  * and returns it back to the user.
4947  * Returns 0 on error.
4948  */
4949 static uint dgap_get_custom_baud(struct channel_t *ch)
4950 {
4951         u8 __iomem *vaddr;
4952         ulong offset;
4953         uint value = 0;
4954
4955         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
4956                 return 0;
4957
4958         if (!ch->ch_bd || ch->ch_bd->magic != DGAP_BOARD_MAGIC)
4959                 return 0;
4960
4961         if (!(ch->ch_bd->bd_flags & BD_FEP5PLUS))
4962                 return 0;
4963
4964         vaddr = ch->ch_bd->re_map_membase;
4965
4966         if (!vaddr)
4967                 return 0;
4968
4969         /*
4970          * Go get from fep mem, what the fep
4971          * believes the custom baud rate is.
4972          */
4973         offset = ((((*(unsigned short __iomem *)(vaddr + ECS_SEG)) << 4) +
4974                 (ch->ch_portnum * 0x28) + LINE_SPEED));
4975
4976         value = readw(vaddr + offset);
4977         return value;
4978 }
4979
4980 /*
4981  * Calls the firmware to reset this channel.
4982  */
4983 static void dgap_firmware_reset_port(struct channel_t *ch)
4984 {
4985         dgap_cmdb(ch, CHRESET, 0, 0, 0);
4986
4987         /*
4988          * Now that the channel is reset, we need to make sure
4989          * all the current settings get reapplied to the port
4990          * in the firmware.
4991          *
4992          * So we will set the driver's cache of firmware
4993          * settings all to 0, and then call param.
4994          */
4995         ch->ch_fepiflag = 0;
4996         ch->ch_fepcflag = 0;
4997         ch->ch_fepoflag = 0;
4998         ch->ch_fepstartc = 0;
4999         ch->ch_fepstopc = 0;
5000         ch->ch_fepastartc = 0;
5001         ch->ch_fepastopc = 0;
5002         ch->ch_mostat = 0;
5003         ch->ch_hflow = 0;
5004 }
5005
5006 /*=======================================================================
5007  *
5008  *      dgap_param - Set Digi parameters.
5009  *
5010  *              struct tty_struct *     - TTY for port.
5011  *
5012  *=======================================================================*/
5013 static int dgap_param(struct tty_struct *tty)
5014 {
5015         struct ktermios *ts;
5016         struct board_t *bd;
5017         struct channel_t *ch;
5018         struct bs_t __iomem *bs;
5019         struct un_t   *un;
5020         u16     head;
5021         u16     cflag;
5022         u16     iflag;
5023         u8      mval;
5024         u8      hflow;
5025
5026         if (!tty || tty->magic != TTY_MAGIC)
5027                 return -ENXIO;
5028
5029         un = (struct un_t *) tty->driver_data;
5030         if (!un || un->magic != DGAP_UNIT_MAGIC)
5031                 return -ENXIO;
5032
5033         ch = un->un_ch;
5034         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
5035                 return -ENXIO;
5036
5037         bd = ch->ch_bd;
5038         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
5039                 return -ENXIO;
5040
5041         bs = ch->ch_bs;
5042         if (!bs)
5043                 return -ENXIO;
5044
5045         ts = &tty->termios;
5046
5047         /*
5048          * If baud rate is zero, flush queues, and set mval to drop DTR.
5049          */
5050         if ((ch->ch_c_cflag & (CBAUD)) == 0) {
5051
5052                 /* flush rx */
5053                 head = readw(&(ch->ch_bs->rx_head));
5054                 writew(head, &(ch->ch_bs->rx_tail));
5055
5056                 /* flush tx */
5057                 head = readw(&(ch->ch_bs->tx_head));
5058                 writew(head, &(ch->ch_bs->tx_tail));
5059
5060                 ch->ch_flags |= (CH_BAUD0);
5061
5062                 /* Drop RTS and DTR */
5063                 ch->ch_mval &= ~(D_RTS(ch)|D_DTR(ch));
5064                 mval = D_DTR(ch) | D_RTS(ch);
5065                 ch->ch_baud_info = 0;
5066
5067         } else if (ch->ch_custom_speed && (bd->bd_flags & BD_FEP5PLUS)) {
5068                 /*
5069                  * Tell the fep to do the command
5070                  */
5071
5072                 dgap_cmdw_ext(ch, 0xff01, ch->ch_custom_speed, 0);
5073
5074                 /*
5075                  * Now go get from fep mem, what the fep
5076                  * believes the custom baud rate is.
5077                  */
5078                 ch->ch_custom_speed = dgap_get_custom_baud(ch);
5079                 ch->ch_baud_info = ch->ch_custom_speed;
5080
5081                 /* Handle transition from B0 */
5082                 if (ch->ch_flags & CH_BAUD0) {
5083                         ch->ch_flags &= ~(CH_BAUD0);
5084                         ch->ch_mval |= (D_RTS(ch)|D_DTR(ch));
5085                 }
5086                 mval = D_DTR(ch) | D_RTS(ch);
5087
5088         } else {
5089                 /*
5090                  * Set baud rate, character size, and parity.
5091                  */
5092
5093
5094                 int iindex = 0;
5095                 int jindex = 0;
5096                 int baud = 0;
5097
5098                 ulong bauds[4][16] = {
5099                         { /* slowbaud */
5100                                 0,      50,     75,     110,
5101                                 134,    150,    200,    300,
5102                                 600,    1200,   1800,   2400,
5103                                 4800,   9600,   19200,  38400 },
5104                         { /* slowbaud & CBAUDEX */
5105                                 0,      57600,  115200, 230400,
5106                                 460800, 150,    200,    921600,
5107                                 600,    1200,   1800,   2400,
5108                                 4800,   9600,   19200,  38400 },
5109                         { /* fastbaud */
5110                                 0,      57600,  76800,  115200,
5111                                 14400,  57600,  230400, 76800,
5112                                 115200, 230400, 28800,  460800,
5113                                 921600, 9600,   19200,  38400 },
5114                         { /* fastbaud & CBAUDEX */
5115                                 0,      57600,  115200, 230400,
5116                                 460800, 150,    200,    921600,
5117                                 600,    1200,   1800,   2400,
5118                                 4800,   9600,   19200,  38400 }
5119                 };
5120
5121                 /*
5122                  * Only use the TXPrint baud rate if the
5123                  * terminal unit is NOT open
5124                  */
5125                 if (!(ch->ch_tun.un_flags & UN_ISOPEN) &&
5126                      (un->un_type == DGAP_PRINT))
5127                         baud = C_BAUD(ch->ch_pun.un_tty) & 0xff;
5128                 else
5129                         baud = C_BAUD(ch->ch_tun.un_tty) & 0xff;
5130
5131                 if (ch->ch_c_cflag & CBAUDEX)
5132                         iindex = 1;
5133
5134                 if (ch->ch_digi.digi_flags & DIGI_FAST)
5135                         iindex += 2;
5136
5137                 jindex = baud;
5138
5139                 if ((iindex >= 0) && (iindex < 4) &&
5140                     (jindex >= 0) && (jindex < 16))
5141                         baud = bauds[iindex][jindex];
5142                 else
5143                         baud = 0;
5144
5145                 if (baud == 0)
5146                         baud = 9600;
5147
5148                 ch->ch_baud_info = baud;
5149
5150                 /*
5151                  * CBAUD has bit position 0x1000 set these days to
5152                  * indicate Linux baud rate remap.
5153                  * We use a different bit assignment for high speed.
5154                  * Clear this bit out while grabbing the parts of
5155                  * "cflag" we want.
5156                  */
5157                 cflag = ch->ch_c_cflag & ((CBAUD ^ CBAUDEX) | PARODD | PARENB |
5158                                                    CSTOPB | CSIZE);
5159
5160                 /*
5161                  * HUPCL bit is used by FEP to indicate fast baud
5162                  * table is to be used.
5163                  */
5164                 if ((ch->ch_digi.digi_flags & DIGI_FAST) ||
5165                     (ch->ch_c_cflag & CBAUDEX))
5166                         cflag |= HUPCL;
5167
5168                 if ((ch->ch_c_cflag & CBAUDEX) &&
5169                     !(ch->ch_digi.digi_flags & DIGI_FAST)) {
5170                         /*
5171                          * The below code is trying to guarantee that only
5172                          * baud rates 115200, 230400, 460800, 921600 are
5173                          * remapped. We use exclusive or  because the various
5174                          * baud rates share common bit positions and therefore
5175                          * can't be tested for easily.
5176                          */
5177                         tcflag_t tcflag = (ch->ch_c_cflag & CBAUD) | CBAUDEX;
5178                         int baudpart = 0;
5179
5180                         /*
5181                          * Map high speed requests to index
5182                          * into FEP's baud table
5183                          */
5184                         switch (tcflag) {
5185                         case B57600:
5186                                 baudpart = 1;
5187                                 break;
5188 #ifdef B76800
5189                         case B76800:
5190                                 baudpart = 2;
5191                                 break;
5192 #endif
5193                         case B115200:
5194                                 baudpart = 3;
5195                                 break;
5196                         case B230400:
5197                                 baudpart = 9;
5198                                 break;
5199                         case B460800:
5200                                 baudpart = 11;
5201                                 break;
5202 #ifdef B921600
5203                         case B921600:
5204                                 baudpart = 12;
5205                                 break;
5206 #endif
5207                         default:
5208                                 baudpart = 0;
5209                         }
5210
5211                         if (baudpart)
5212                                 cflag = (cflag & ~(CBAUD | CBAUDEX)) | baudpart;
5213                 }
5214
5215                 cflag &= 0xffff;
5216
5217                 if (cflag != ch->ch_fepcflag) {
5218                         ch->ch_fepcflag = (u16) (cflag & 0xffff);
5219
5220                         /*
5221                          * Okay to have channel and board
5222                          * locks held calling this
5223                          */
5224                         dgap_cmdw(ch, SCFLAG, (u16) cflag, 0);
5225                 }
5226
5227                 /* Handle transition from B0 */
5228                 if (ch->ch_flags & CH_BAUD0) {
5229                         ch->ch_flags &= ~(CH_BAUD0);
5230                         ch->ch_mval |= (D_RTS(ch)|D_DTR(ch));
5231                 }
5232                 mval = D_DTR(ch) | D_RTS(ch);
5233         }
5234
5235         /*
5236          * Get input flags.
5237          */
5238         iflag = ch->ch_c_iflag & (IGNBRK | BRKINT | IGNPAR | PARMRK |
5239                                   INPCK | ISTRIP | IXON | IXANY | IXOFF);
5240
5241         if ((ch->ch_startc == _POSIX_VDISABLE) ||
5242             (ch->ch_stopc == _POSIX_VDISABLE)) {
5243                 iflag &= ~(IXON | IXOFF);
5244                 ch->ch_c_iflag &= ~(IXON | IXOFF);
5245         }
5246
5247         /*
5248          * Only the IBM Xr card can switch between
5249          * 232 and 422 modes on the fly
5250          */
5251         if (bd->device == PCI_DEV_XR_IBM_DID) {
5252                 if (ch->ch_digi.digi_flags & DIGI_422)
5253                         dgap_cmdb(ch, SCOMMODE, MODE_422, 0, 0);
5254                 else
5255                         dgap_cmdb(ch, SCOMMODE, MODE_232, 0, 0);
5256         }
5257
5258         if (ch->ch_digi.digi_flags & DIGI_ALTPIN)
5259                 iflag |= IALTPIN;
5260
5261         if (iflag != ch->ch_fepiflag) {
5262                 ch->ch_fepiflag = iflag;
5263
5264                 /* Okay to have channel and board locks held calling this */
5265                 dgap_cmdw(ch, SIFLAG, (u16) ch->ch_fepiflag, 0);
5266         }
5267
5268         /*
5269          * Select hardware handshaking.
5270          */
5271         hflow = 0;
5272
5273         if (ch->ch_c_cflag & CRTSCTS)
5274                 hflow |= (D_RTS(ch) | D_CTS(ch));
5275         if (ch->ch_digi.digi_flags & RTSPACE)
5276                 hflow |= D_RTS(ch);
5277         if (ch->ch_digi.digi_flags & DTRPACE)
5278                 hflow |= D_DTR(ch);
5279         if (ch->ch_digi.digi_flags & CTSPACE)
5280                 hflow |= D_CTS(ch);
5281         if (ch->ch_digi.digi_flags & DSRPACE)
5282                 hflow |= D_DSR(ch);
5283         if (ch->ch_digi.digi_flags & DCDPACE)
5284                 hflow |= D_CD(ch);
5285
5286         if (hflow != ch->ch_hflow) {
5287                 ch->ch_hflow = hflow;
5288
5289                 /* Okay to have channel and board locks held calling this */
5290                 dgap_cmdb(ch, SHFLOW, (u8) hflow, 0xff, 0);
5291         }
5292
5293
5294         /*
5295          * Set RTS and/or DTR Toggle if needed,
5296          * but only if product is FEP5+ based.
5297          */
5298         if (bd->bd_flags & BD_FEP5PLUS) {
5299                 u16 hflow2 = 0;
5300                 if (ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE)
5301                         hflow2 |= (D_RTS(ch));
5302                 if (ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE)
5303                         hflow2 |= (D_DTR(ch));
5304
5305                 dgap_cmdw_ext(ch, 0xff03, hflow2, 0);
5306         }
5307
5308         /*
5309          * Set modem control lines.
5310          */
5311
5312         mval ^= ch->ch_mforce & (mval ^ ch->ch_mval);
5313
5314         if (ch->ch_mostat ^ mval) {
5315                 ch->ch_mostat = mval;
5316
5317                 /* Okay to have channel and board locks held calling this */
5318                 dgap_cmdb(ch, SMODEM, (u8) mval, D_RTS(ch)|D_DTR(ch), 0);
5319         }
5320
5321         /*
5322          * Read modem signals, and then call carrier function.
5323          */
5324         ch->ch_mistat = readb(&(bs->m_stat));
5325         dgap_carrier(ch);
5326
5327         /*
5328          * Set the start and stop characters.
5329          */
5330         if (ch->ch_startc != ch->ch_fepstartc ||
5331             ch->ch_stopc != ch->ch_fepstopc) {
5332                 ch->ch_fepstartc = ch->ch_startc;
5333                 ch->ch_fepstopc =  ch->ch_stopc;
5334
5335                 /* Okay to have channel and board locks held calling this */
5336                 dgap_cmdb(ch, SFLOWC, ch->ch_fepstartc, ch->ch_fepstopc, 0);
5337         }
5338
5339         /*
5340          * Set the Auxiliary start and stop characters.
5341          */
5342         if (ch->ch_astartc != ch->ch_fepastartc ||
5343             ch->ch_astopc != ch->ch_fepastopc) {
5344                 ch->ch_fepastartc = ch->ch_astartc;
5345                 ch->ch_fepastopc = ch->ch_astopc;
5346
5347                 /* Okay to have channel and board locks held calling this */
5348                 dgap_cmdb(ch, SAFLOWC, ch->ch_fepastartc, ch->ch_fepastopc, 0);
5349         }
5350
5351         return 0;
5352 }
5353
5354 /*
5355  * dgap_parity_scan()
5356  *
5357  * Convert the FEP5 way of reporting parity errors and breaks into
5358  * the Linux line discipline way.
5359  */
5360 static void dgap_parity_scan(struct channel_t *ch, unsigned char *cbuf,
5361                                 unsigned char *fbuf, int *len)
5362 {
5363         int l = *len;
5364         int count = 0;
5365         unsigned char *in, *cout, *fout;
5366         unsigned char c;
5367
5368         in = cbuf;
5369         cout = cbuf;
5370         fout = fbuf;
5371
5372         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
5373                 return;
5374
5375         while (l--) {
5376                 c = *in++;
5377                 switch (ch->pscan_state) {
5378                 default:
5379                         /* reset to sanity and fall through */
5380                         ch->pscan_state = 0;
5381
5382                 case 0:
5383                         /* No FF seen yet */
5384                         if (c == (unsigned char) '\377')
5385                                 /* delete this character from stream */
5386                                 ch->pscan_state = 1;
5387                         else {
5388                                 *cout++ = c;
5389                                 *fout++ = TTY_NORMAL;
5390                                 count += 1;
5391                         }
5392                         break;
5393
5394                 case 1:
5395                         /* first FF seen */
5396                         if (c == (unsigned char) '\377') {
5397                                 /* doubled ff, transform to single ff */
5398                                 *cout++ = c;
5399                                 *fout++ = TTY_NORMAL;
5400                                 count += 1;
5401                                 ch->pscan_state = 0;
5402                         } else {
5403                                 /* save value examination in next state */
5404                                 ch->pscan_savechar = c;
5405                                 ch->pscan_state = 2;
5406                         }
5407                         break;
5408
5409                 case 2:
5410                         /* third character of ff sequence */
5411
5412                         *cout++ = c;
5413
5414                         if (ch->pscan_savechar == 0x0) {
5415
5416                                 if (c == 0x0) {
5417                                         ch->ch_err_break++;
5418                                         *fout++ = TTY_BREAK;
5419                                 } else {
5420                                         ch->ch_err_parity++;
5421                                         *fout++ = TTY_PARITY;
5422                                 }
5423                         }
5424
5425                         count += 1;
5426                         ch->pscan_state = 0;
5427                 }
5428         }
5429         *len = count;
5430 }
5431
5432 static void dgap_write_wakeup(struct board_t *bd, struct channel_t *ch,
5433                               struct un_t *un, u32 mask,
5434                               unsigned long *irq_flags1,
5435                               unsigned long *irq_flags2)
5436 {
5437         if (!(un->un_flags & mask))
5438                 return;
5439
5440         un->un_flags &= ~mask;
5441
5442         if (!(un->un_flags & UN_ISOPEN))
5443                 return;
5444
5445         if ((un->un_tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
5446             un->un_tty->ldisc->ops->write_wakeup) {
5447                 spin_unlock_irqrestore(&ch->ch_lock, *irq_flags2);
5448                 spin_unlock_irqrestore(&bd->bd_lock, *irq_flags1);
5449
5450                 (un->un_tty->ldisc->ops->write_wakeup)(un->un_tty);
5451
5452                 spin_lock_irqsave(&bd->bd_lock, *irq_flags1);
5453                 spin_lock_irqsave(&ch->ch_lock, *irq_flags2);
5454         }
5455         wake_up_interruptible(&un->un_tty->write_wait);
5456         wake_up_interruptible(&un->un_flags_wait);
5457 }
5458
5459 /*=======================================================================
5460  *
5461  *      dgap_event - FEP to host event processing routine.
5462  *
5463  *              bd     - Board of current event.
5464  *
5465  *=======================================================================*/
5466 static int dgap_event(struct board_t *bd)
5467 {
5468         struct channel_t *ch;
5469         ulong           lock_flags;
5470         ulong           lock_flags2;
5471         struct bs_t __iomem *bs;
5472         u8 __iomem      *event;
5473         u8 __iomem      *vaddr;
5474         struct ev_t __iomem *eaddr;
5475         uint            head;
5476         uint            tail;
5477         int             port;
5478         int             reason;
5479         int             modem;
5480         int             b1;
5481
5482         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
5483                 return -ENXIO;
5484
5485         spin_lock_irqsave(&bd->bd_lock, lock_flags);
5486
5487         vaddr = bd->re_map_membase;
5488
5489         if (!vaddr) {
5490                 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
5491                 return -ENXIO;
5492         }
5493
5494         eaddr = (struct ev_t __iomem *) (vaddr + EVBUF);
5495
5496         /* Get our head and tail */
5497         head = readw(&(eaddr->ev_head));
5498         tail = readw(&(eaddr->ev_tail));
5499
5500         /*
5501          * Forget it if pointers out of range.
5502          */
5503
5504         if (head >= EVMAX - EVSTART || tail >= EVMAX - EVSTART ||
5505             (head | tail) & 03) {
5506                 /* Let go of board lock */
5507                 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
5508                 return -ENXIO;
5509         }
5510
5511         /*
5512          * Loop to process all the events in the buffer.
5513          */
5514         while (tail != head) {
5515
5516                 /*
5517                  * Get interrupt information.
5518                  */
5519
5520                 event = bd->re_map_membase + tail + EVSTART;
5521
5522                 port   = event[0];
5523                 reason = event[1];
5524                 modem  = event[2];
5525                 b1     = event[3];
5526
5527                 /*
5528                  * Make sure the interrupt is valid.
5529                  */
5530                 if (port >= bd->nasync)
5531                         goto next;
5532
5533                 if (!(reason & (IFMODEM | IFBREAK | IFTLW | IFTEM | IFDATA)))
5534                         goto next;
5535
5536                 ch = bd->channels[port];
5537
5538                 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
5539                         goto next;
5540
5541                 /*
5542                  * If we have made it here, the event was valid.
5543                  * Lock down the channel.
5544                  */
5545                 spin_lock_irqsave(&ch->ch_lock, lock_flags2);
5546
5547                 bs = ch->ch_bs;
5548
5549                 if (!bs) {
5550                         spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
5551                         goto next;
5552                 }
5553
5554                 /*
5555                  * Process received data.
5556                  */
5557                 if (reason & IFDATA) {
5558
5559                         /*
5560                          * ALL LOCKS *MUST* BE DROPPED BEFORE CALLING INPUT!
5561                          * input could send some data to ld, which in turn
5562                          * could do a callback to one of our other functions.
5563                          */
5564                         spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
5565                         spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
5566
5567                         dgap_input(ch);
5568
5569                         spin_lock_irqsave(&bd->bd_lock, lock_flags);
5570                         spin_lock_irqsave(&ch->ch_lock, lock_flags2);
5571
5572                         if (ch->ch_flags & CH_RACTIVE)
5573                                 ch->ch_flags |= CH_RENABLE;
5574                         else
5575                                 writeb(1, &(bs->idata));
5576
5577                         if (ch->ch_flags & CH_RWAIT) {
5578                                 ch->ch_flags &= ~CH_RWAIT;
5579
5580                                 wake_up_interruptible
5581                                         (&ch->ch_tun.un_flags_wait);
5582                         }
5583                 }
5584
5585                 /*
5586                  * Process Modem change signals.
5587                  */
5588                 if (reason & IFMODEM) {
5589                         ch->ch_mistat = modem;
5590                         dgap_carrier(ch);
5591                 }
5592
5593                 /*
5594                  * Process break.
5595                  */
5596                 if (reason & IFBREAK) {
5597
5598                         if (ch->ch_tun.un_tty) {
5599                                 /* A break has been indicated */
5600                                 ch->ch_err_break++;
5601                                 tty_buffer_request_room
5602                                         (ch->ch_tun.un_tty->port, 1);
5603                                 tty_insert_flip_char(ch->ch_tun.un_tty->port,
5604                                                      0, TTY_BREAK);
5605                                 tty_flip_buffer_push(ch->ch_tun.un_tty->port);
5606                         }
5607                 }
5608
5609                 /*
5610                  * Process Transmit low.
5611                  */
5612                 if (reason & IFTLW) {
5613                         dgap_write_wakeup(bd, ch, &ch->ch_tun, UN_LOW,
5614                                           &lock_flags, &lock_flags2);
5615                         dgap_write_wakeup(bd, ch, &ch->ch_pun, UN_LOW,
5616                                           &lock_flags, &lock_flags2);
5617                         if (ch->ch_flags & CH_WLOW) {
5618                                 ch->ch_flags &= ~CH_WLOW;
5619                                 wake_up_interruptible(&ch->ch_flags_wait);
5620                         }
5621                 }
5622
5623                 /*
5624                  * Process Transmit empty.
5625                  */
5626                 if (reason & IFTEM) {
5627                         dgap_write_wakeup(bd, ch, &ch->ch_tun, UN_EMPTY,
5628                                           &lock_flags, &lock_flags2);
5629                         dgap_write_wakeup(bd, ch, &ch->ch_pun, UN_EMPTY,
5630                                           &lock_flags, &lock_flags2);
5631                         if (ch->ch_flags & CH_WEMPTY) {
5632                                 ch->ch_flags &= ~CH_WEMPTY;
5633                                 wake_up_interruptible(&ch->ch_flags_wait);
5634                         }
5635                 }
5636
5637                 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
5638
5639 next:
5640                 tail = (tail + 4) & (EVMAX - EVSTART - 4);
5641         }
5642
5643         writew(tail, &(eaddr->ev_tail));
5644         spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
5645
5646         return 0;
5647 }
5648
5649 static ssize_t dgap_driver_version_show(struct device_driver *ddp, char *buf)
5650 {
5651         return snprintf(buf, PAGE_SIZE, "%s\n", DG_PART);
5652 }
5653 static DRIVER_ATTR(version, S_IRUSR, dgap_driver_version_show, NULL);
5654
5655
5656 static ssize_t dgap_driver_boards_show(struct device_driver *ddp, char *buf)
5657 {
5658         return snprintf(buf, PAGE_SIZE, "%d\n", dgap_numboards);
5659 }
5660 static DRIVER_ATTR(boards, S_IRUSR, dgap_driver_boards_show, NULL);
5661
5662
5663 static ssize_t dgap_driver_maxboards_show(struct device_driver *ddp, char *buf)
5664 {
5665         return snprintf(buf, PAGE_SIZE, "%d\n", MAXBOARDS);
5666 }
5667 static DRIVER_ATTR(maxboards, S_IRUSR, dgap_driver_maxboards_show, NULL);
5668
5669
5670 static ssize_t dgap_driver_pollcounter_show(struct device_driver *ddp,
5671                                             char *buf)
5672 {
5673         return snprintf(buf, PAGE_SIZE, "%ld\n", dgap_poll_counter);
5674 }
5675 static DRIVER_ATTR(pollcounter, S_IRUSR, dgap_driver_pollcounter_show, NULL);
5676
5677 static ssize_t dgap_driver_pollrate_show(struct device_driver *ddp, char *buf)
5678 {
5679         return snprintf(buf, PAGE_SIZE, "%dms\n", dgap_poll_tick);
5680 }
5681
5682 static ssize_t dgap_driver_pollrate_store(struct device_driver *ddp,
5683                                           const char *buf, size_t count)
5684 {
5685         if (sscanf(buf, "%d\n", &dgap_poll_tick) != 1)
5686                 return -EINVAL;
5687         return count;
5688 }
5689 static DRIVER_ATTR(pollrate, (S_IRUSR | S_IWUSR), dgap_driver_pollrate_show,
5690                    dgap_driver_pollrate_store);
5691
5692 static int dgap_create_driver_sysfiles(struct pci_driver *dgap_driver)
5693 {
5694         int rc = 0;
5695         struct device_driver *driverfs = &dgap_driver->driver;
5696
5697         rc |= driver_create_file(driverfs, &driver_attr_version);
5698         rc |= driver_create_file(driverfs, &driver_attr_boards);
5699         rc |= driver_create_file(driverfs, &driver_attr_maxboards);
5700         rc |= driver_create_file(driverfs, &driver_attr_pollrate);
5701         rc |= driver_create_file(driverfs, &driver_attr_pollcounter);
5702
5703         return rc;
5704 }
5705
5706 static void dgap_remove_driver_sysfiles(struct pci_driver *dgap_driver)
5707 {
5708         struct device_driver *driverfs = &dgap_driver->driver;
5709         driver_remove_file(driverfs, &driver_attr_version);
5710         driver_remove_file(driverfs, &driver_attr_boards);
5711         driver_remove_file(driverfs, &driver_attr_maxboards);
5712         driver_remove_file(driverfs, &driver_attr_pollrate);
5713         driver_remove_file(driverfs, &driver_attr_pollcounter);
5714 }
5715
5716 static struct board_t *dgap_verify_board(struct device *p)
5717 {
5718         struct board_t *bd;
5719
5720         if (!p)
5721                 return NULL;
5722
5723         bd = dev_get_drvdata(p);
5724         if (!bd || bd->magic != DGAP_BOARD_MAGIC || bd->state != BOARD_READY)
5725                 return NULL;
5726
5727         return bd;
5728 }
5729
5730 static ssize_t dgap_ports_state_show(struct device *p,
5731                                      struct device_attribute *attr,
5732                                      char *buf)
5733 {
5734         struct board_t *bd;
5735         int count = 0;
5736         int i = 0;
5737
5738         bd = dgap_verify_board(p);
5739         if (!bd)
5740                 return 0;
5741
5742         for (i = 0; i < bd->nasync; i++) {
5743                 count += snprintf(buf + count, PAGE_SIZE - count,
5744                         "%d %s\n", bd->channels[i]->ch_portnum,
5745                         bd->channels[i]->ch_open_count ? "Open" : "Closed");
5746         }
5747         return count;
5748 }
5749 static DEVICE_ATTR(ports_state, S_IRUSR, dgap_ports_state_show, NULL);
5750
5751 static ssize_t dgap_ports_baud_show(struct device *p,
5752                                     struct device_attribute *attr,
5753                                     char *buf)
5754 {
5755         struct board_t *bd;
5756         int count = 0;
5757         int i = 0;
5758
5759         bd = dgap_verify_board(p);
5760         if (!bd)
5761                 return 0;
5762
5763         for (i = 0; i < bd->nasync; i++) {
5764                 count +=  snprintf(buf + count, PAGE_SIZE - count, "%d %d\n",
5765                                    bd->channels[i]->ch_portnum,
5766                                    bd->channels[i]->ch_baud_info);
5767         }
5768         return count;
5769 }
5770 static DEVICE_ATTR(ports_baud, S_IRUSR, dgap_ports_baud_show, NULL);
5771
5772 static ssize_t dgap_ports_msignals_show(struct device *p,
5773                                         struct device_attribute *attr,
5774                                         char *buf)
5775 {
5776         struct board_t *bd;
5777         int count = 0;
5778         int i = 0;
5779
5780         bd = dgap_verify_board(p);
5781         if (!bd)
5782                 return 0;
5783
5784         for (i = 0; i < bd->nasync; i++) {
5785                 if (bd->channels[i]->ch_open_count)
5786                         count += snprintf(buf + count, PAGE_SIZE - count,
5787                                 "%d %s %s %s %s %s %s\n",
5788                                 bd->channels[i]->ch_portnum,
5789                                 (bd->channels[i]->ch_mostat &
5790                                  UART_MCR_RTS) ? "RTS" : "",
5791                                 (bd->channels[i]->ch_mistat &
5792                                  UART_MSR_CTS) ? "CTS" : "",
5793                                 (bd->channels[i]->ch_mostat &
5794                                  UART_MCR_DTR) ? "DTR" : "",
5795                                 (bd->channels[i]->ch_mistat &
5796                                  UART_MSR_DSR) ? "DSR" : "",
5797                                 (bd->channels[i]->ch_mistat &
5798                                  UART_MSR_DCD) ? "DCD" : "",
5799                                 (bd->channels[i]->ch_mistat &
5800                                  UART_MSR_RI)  ? "RI"  : "");
5801                 else
5802                         count += snprintf(buf + count, PAGE_SIZE - count,
5803                                 "%d\n", bd->channels[i]->ch_portnum);
5804         }
5805         return count;
5806 }
5807 static DEVICE_ATTR(ports_msignals, S_IRUSR, dgap_ports_msignals_show, NULL);
5808
5809 static ssize_t dgap_ports_iflag_show(struct device *p,
5810                                      struct device_attribute *attr,
5811                                      char *buf)
5812 {
5813         struct board_t *bd;
5814         int count = 0;
5815         int i = 0;
5816
5817         bd = dgap_verify_board(p);
5818         if (!bd)
5819                 return 0;
5820
5821         for (i = 0; i < bd->nasync; i++)
5822                 count += snprintf(buf + count, PAGE_SIZE - count, "%d %x\n",
5823                                   bd->channels[i]->ch_portnum,
5824                                   bd->channels[i]->ch_c_iflag);
5825         return count;
5826 }
5827 static DEVICE_ATTR(ports_iflag, S_IRUSR, dgap_ports_iflag_show, NULL);
5828
5829 static ssize_t dgap_ports_cflag_show(struct device *p,
5830                                      struct device_attribute *attr,
5831                                      char *buf)
5832 {
5833         struct board_t *bd;
5834         int count = 0;
5835         int i = 0;
5836
5837         bd = dgap_verify_board(p);
5838         if (!bd)
5839                 return 0;
5840
5841         for (i = 0; i < bd->nasync; i++)
5842                 count += snprintf(buf + count, PAGE_SIZE - count, "%d %x\n",
5843                                   bd->channels[i]->ch_portnum,
5844                                   bd->channels[i]->ch_c_cflag);
5845         return count;
5846 }
5847 static DEVICE_ATTR(ports_cflag, S_IRUSR, dgap_ports_cflag_show, NULL);
5848
5849 static ssize_t dgap_ports_oflag_show(struct device *p,
5850                                      struct device_attribute *attr,
5851                                      char *buf)
5852 {
5853         struct board_t *bd;
5854         int count = 0;
5855         int i = 0;
5856
5857         bd = dgap_verify_board(p);
5858         if (!bd)
5859                 return 0;
5860
5861         for (i = 0; i < bd->nasync; i++)
5862                 count += snprintf(buf + count, PAGE_SIZE - count, "%d %x\n",
5863                                   bd->channels[i]->ch_portnum,
5864                                   bd->channels[i]->ch_c_oflag);
5865         return count;
5866 }
5867 static DEVICE_ATTR(ports_oflag, S_IRUSR, dgap_ports_oflag_show, NULL);
5868
5869 static ssize_t dgap_ports_lflag_show(struct device *p,
5870                                      struct device_attribute *attr,
5871                                      char *buf)
5872 {
5873         struct board_t *bd;
5874         int count = 0;
5875         int i = 0;
5876
5877         bd = dgap_verify_board(p);
5878         if (!bd)
5879                 return 0;
5880
5881         for (i = 0; i < bd->nasync; i++)
5882                 count += snprintf(buf + count, PAGE_SIZE - count, "%d %x\n",
5883                                   bd->channels[i]->ch_portnum,
5884                                   bd->channels[i]->ch_c_lflag);
5885         return count;
5886 }
5887 static DEVICE_ATTR(ports_lflag, S_IRUSR, dgap_ports_lflag_show, NULL);
5888
5889 static ssize_t dgap_ports_digi_flag_show(struct device *p,
5890                                          struct device_attribute *attr,
5891                                          char *buf)
5892 {
5893         struct board_t *bd;
5894         int count = 0;
5895         int i = 0;
5896
5897         bd = dgap_verify_board(p);
5898         if (!bd)
5899                 return 0;
5900
5901         for (i = 0; i < bd->nasync; i++)
5902                 count += snprintf(buf + count, PAGE_SIZE - count, "%d %x\n",
5903                                   bd->channels[i]->ch_portnum,
5904                                   bd->channels[i]->ch_digi.digi_flags);
5905         return count;
5906 }
5907 static DEVICE_ATTR(ports_digi_flag, S_IRUSR, dgap_ports_digi_flag_show, NULL);
5908
5909 static ssize_t dgap_ports_rxcount_show(struct device *p,
5910                                        struct device_attribute *attr,
5911                                        char *buf)
5912 {
5913         struct board_t *bd;
5914         int count = 0;
5915         int i = 0;
5916
5917         bd = dgap_verify_board(p);
5918         if (!bd)
5919                 return 0;
5920
5921         for (i = 0; i < bd->nasync; i++)
5922                 count += snprintf(buf + count, PAGE_SIZE - count, "%d %ld\n",
5923                                   bd->channels[i]->ch_portnum,
5924                                   bd->channels[i]->ch_rxcount);
5925         return count;
5926 }
5927 static DEVICE_ATTR(ports_rxcount, S_IRUSR, dgap_ports_rxcount_show, NULL);
5928
5929 static ssize_t dgap_ports_txcount_show(struct device *p,
5930                                        struct device_attribute *attr,
5931                                        char *buf)
5932 {
5933         struct board_t *bd;
5934         int count = 0;
5935         int i = 0;
5936
5937         bd = dgap_verify_board(p);
5938         if (!bd)
5939                 return 0;
5940
5941         for (i = 0; i < bd->nasync; i++)
5942                 count += snprintf(buf + count, PAGE_SIZE - count, "%d %ld\n",
5943                                   bd->channels[i]->ch_portnum,
5944                                   bd->channels[i]->ch_txcount);
5945         return count;
5946 }
5947 static DEVICE_ATTR(ports_txcount, S_IRUSR, dgap_ports_txcount_show, NULL);
5948
5949 /* this function creates the sys files that will export each signal status
5950  * to sysfs each value will be put in a separate filename
5951  */
5952 static void dgap_create_ports_sysfiles(struct board_t *bd)
5953 {
5954         dev_set_drvdata(&bd->pdev->dev, bd);
5955         device_create_file(&(bd->pdev->dev), &dev_attr_ports_state);
5956         device_create_file(&(bd->pdev->dev), &dev_attr_ports_baud);
5957         device_create_file(&(bd->pdev->dev), &dev_attr_ports_msignals);
5958         device_create_file(&(bd->pdev->dev), &dev_attr_ports_iflag);
5959         device_create_file(&(bd->pdev->dev), &dev_attr_ports_cflag);
5960         device_create_file(&(bd->pdev->dev), &dev_attr_ports_oflag);
5961         device_create_file(&(bd->pdev->dev), &dev_attr_ports_lflag);
5962         device_create_file(&(bd->pdev->dev), &dev_attr_ports_digi_flag);
5963         device_create_file(&(bd->pdev->dev), &dev_attr_ports_rxcount);
5964         device_create_file(&(bd->pdev->dev), &dev_attr_ports_txcount);
5965 }
5966
5967 /* removes all the sys files created for that port */
5968 static void dgap_remove_ports_sysfiles(struct board_t *bd)
5969 {
5970         device_remove_file(&(bd->pdev->dev), &dev_attr_ports_state);
5971         device_remove_file(&(bd->pdev->dev), &dev_attr_ports_baud);
5972         device_remove_file(&(bd->pdev->dev), &dev_attr_ports_msignals);
5973         device_remove_file(&(bd->pdev->dev), &dev_attr_ports_iflag);
5974         device_remove_file(&(bd->pdev->dev), &dev_attr_ports_cflag);
5975         device_remove_file(&(bd->pdev->dev), &dev_attr_ports_oflag);
5976         device_remove_file(&(bd->pdev->dev), &dev_attr_ports_lflag);
5977         device_remove_file(&(bd->pdev->dev), &dev_attr_ports_digi_flag);
5978         device_remove_file(&(bd->pdev->dev), &dev_attr_ports_rxcount);
5979         device_remove_file(&(bd->pdev->dev), &dev_attr_ports_txcount);
5980 }
5981
5982 static ssize_t dgap_tty_state_show(struct device *d,
5983                                    struct device_attribute *attr,
5984                                    char *buf)
5985 {
5986         struct board_t *bd;
5987         struct channel_t *ch;
5988         struct un_t *un;
5989
5990         if (!d)
5991                 return 0;
5992         un = dev_get_drvdata(d);
5993         if (!un || un->magic != DGAP_UNIT_MAGIC)
5994                 return 0;
5995         ch = un->un_ch;
5996         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
5997                 return 0;
5998         bd = ch->ch_bd;
5999         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
6000                 return 0;
6001         if (bd->state != BOARD_READY)
6002                 return 0;
6003
6004         return snprintf(buf, PAGE_SIZE, "%s", un->un_open_count ?
6005                         "Open" : "Closed");
6006 }
6007 static DEVICE_ATTR(state, S_IRUSR, dgap_tty_state_show, NULL);
6008
6009 static ssize_t dgap_tty_baud_show(struct device *d,
6010                                   struct device_attribute *attr,
6011                                   char *buf)
6012 {
6013         struct board_t *bd;
6014         struct channel_t *ch;
6015         struct un_t *un;
6016
6017         if (!d)
6018                 return 0;
6019         un = dev_get_drvdata(d);
6020         if (!un || un->magic != DGAP_UNIT_MAGIC)
6021                 return 0;
6022         ch = un->un_ch;
6023         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
6024                 return 0;
6025         bd = ch->ch_bd;
6026         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
6027                 return 0;
6028         if (bd->state != BOARD_READY)
6029                 return 0;
6030
6031         return snprintf(buf, PAGE_SIZE, "%d\n", ch->ch_baud_info);
6032 }
6033 static DEVICE_ATTR(baud, S_IRUSR, dgap_tty_baud_show, NULL);
6034
6035 static ssize_t dgap_tty_msignals_show(struct device *d,
6036                                       struct device_attribute *attr,
6037                                       char *buf)
6038 {
6039         struct board_t *bd;
6040         struct channel_t *ch;
6041         struct un_t *un;
6042
6043         if (!d)
6044                 return 0;
6045         un = dev_get_drvdata(d);
6046         if (!un || un->magic != DGAP_UNIT_MAGIC)
6047                 return 0;
6048         ch = un->un_ch;
6049         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
6050                 return 0;
6051         bd = ch->ch_bd;
6052         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
6053                 return 0;
6054         if (bd->state != BOARD_READY)
6055                 return 0;
6056
6057         if (ch->ch_open_count) {
6058                 return snprintf(buf, PAGE_SIZE, "%s %s %s %s %s %s\n",
6059                         (ch->ch_mostat & UART_MCR_RTS) ? "RTS" : "",
6060                         (ch->ch_mistat & UART_MSR_CTS) ? "CTS" : "",
6061                         (ch->ch_mostat & UART_MCR_DTR) ? "DTR" : "",
6062                         (ch->ch_mistat & UART_MSR_DSR) ? "DSR" : "",
6063                         (ch->ch_mistat & UART_MSR_DCD) ? "DCD" : "",
6064                         (ch->ch_mistat & UART_MSR_RI)  ? "RI"  : "");
6065         }
6066         return 0;
6067 }
6068 static DEVICE_ATTR(msignals, S_IRUSR, dgap_tty_msignals_show, NULL);
6069
6070 static ssize_t dgap_tty_iflag_show(struct device *d,
6071                                    struct device_attribute *attr,
6072                                    char *buf)
6073 {
6074         struct board_t *bd;
6075         struct channel_t *ch;
6076         struct un_t *un;
6077
6078         if (!d)
6079                 return 0;
6080         un = dev_get_drvdata(d);
6081         if (!un || un->magic != DGAP_UNIT_MAGIC)
6082                 return 0;
6083         ch = un->un_ch;
6084         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
6085                 return 0;
6086         bd = ch->ch_bd;
6087         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
6088                 return 0;
6089         if (bd->state != BOARD_READY)
6090                 return 0;
6091
6092         return snprintf(buf, PAGE_SIZE, "%x\n", ch->ch_c_iflag);
6093 }
6094 static DEVICE_ATTR(iflag, S_IRUSR, dgap_tty_iflag_show, NULL);
6095
6096 static ssize_t dgap_tty_cflag_show(struct device *d,
6097                                    struct device_attribute *attr,
6098                                    char *buf)
6099 {
6100         struct board_t *bd;
6101         struct channel_t *ch;
6102         struct un_t *un;
6103
6104         if (!d)
6105                 return 0;
6106         un = dev_get_drvdata(d);
6107         if (!un || un->magic != DGAP_UNIT_MAGIC)
6108                 return 0;
6109         ch = un->un_ch;
6110         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
6111                 return 0;
6112         bd = ch->ch_bd;
6113         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
6114                 return 0;
6115         if (bd->state != BOARD_READY)
6116                 return 0;
6117
6118         return snprintf(buf, PAGE_SIZE, "%x\n", ch->ch_c_cflag);
6119 }
6120 static DEVICE_ATTR(cflag, S_IRUSR, dgap_tty_cflag_show, NULL);
6121
6122 static ssize_t dgap_tty_oflag_show(struct device *d,
6123                                    struct device_attribute *attr,
6124                                    char *buf)
6125 {
6126         struct board_t *bd;
6127         struct channel_t *ch;
6128         struct un_t *un;
6129
6130         if (!d)
6131                 return 0;
6132         un = dev_get_drvdata(d);
6133         if (!un || un->magic != DGAP_UNIT_MAGIC)
6134                 return 0;
6135         ch = un->un_ch;
6136         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
6137                 return 0;
6138         bd = ch->ch_bd;
6139         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
6140                 return 0;
6141         if (bd->state != BOARD_READY)
6142                 return 0;
6143
6144         return snprintf(buf, PAGE_SIZE, "%x\n", ch->ch_c_oflag);
6145 }
6146 static DEVICE_ATTR(oflag, S_IRUSR, dgap_tty_oflag_show, NULL);
6147
6148 static ssize_t dgap_tty_lflag_show(struct device *d,
6149                                    struct device_attribute *attr,
6150                                    char *buf)
6151 {
6152         struct board_t *bd;
6153         struct channel_t *ch;
6154         struct un_t *un;
6155
6156         if (!d)
6157                 return 0;
6158         un = dev_get_drvdata(d);
6159         if (!un || un->magic != DGAP_UNIT_MAGIC)
6160                 return 0;
6161         ch = un->un_ch;
6162         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
6163                 return 0;
6164         bd = ch->ch_bd;
6165         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
6166                 return 0;
6167         if (bd->state != BOARD_READY)
6168                 return 0;
6169
6170         return snprintf(buf, PAGE_SIZE, "%x\n", ch->ch_c_lflag);
6171 }
6172 static DEVICE_ATTR(lflag, S_IRUSR, dgap_tty_lflag_show, NULL);
6173
6174 static ssize_t dgap_tty_digi_flag_show(struct device *d,
6175                                        struct device_attribute *attr,
6176                                        char *buf)
6177 {
6178         struct board_t *bd;
6179         struct channel_t *ch;
6180         struct un_t *un;
6181
6182         if (!d)
6183                 return 0;
6184         un = dev_get_drvdata(d);
6185         if (!un || un->magic != DGAP_UNIT_MAGIC)
6186                 return 0;
6187         ch = un->un_ch;
6188         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
6189                 return 0;
6190         bd = ch->ch_bd;
6191         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
6192                 return 0;
6193         if (bd->state != BOARD_READY)
6194                 return 0;
6195
6196         return snprintf(buf, PAGE_SIZE, "%x\n", ch->ch_digi.digi_flags);
6197 }
6198 static DEVICE_ATTR(digi_flag, S_IRUSR, dgap_tty_digi_flag_show, NULL);
6199
6200 static ssize_t dgap_tty_rxcount_show(struct device *d,
6201                                      struct device_attribute *attr,
6202                                      char *buf)
6203 {
6204         struct board_t *bd;
6205         struct channel_t *ch;
6206         struct un_t *un;
6207
6208         if (!d)
6209                 return 0;
6210         un = dev_get_drvdata(d);
6211         if (!un || un->magic != DGAP_UNIT_MAGIC)
6212                 return 0;
6213         ch = un->un_ch;
6214         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
6215                 return 0;
6216         bd = ch->ch_bd;
6217         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
6218                 return 0;
6219         if (bd->state != BOARD_READY)
6220                 return 0;
6221
6222         return snprintf(buf, PAGE_SIZE, "%ld\n", ch->ch_rxcount);
6223 }
6224 static DEVICE_ATTR(rxcount, S_IRUSR, dgap_tty_rxcount_show, NULL);
6225
6226 static ssize_t dgap_tty_txcount_show(struct device *d,
6227                                      struct device_attribute *attr,
6228                                      char *buf)
6229 {
6230         struct board_t *bd;
6231         struct channel_t *ch;
6232         struct un_t *un;
6233
6234         if (!d)
6235                 return 0;
6236         un = dev_get_drvdata(d);
6237         if (!un || un->magic != DGAP_UNIT_MAGIC)
6238                 return 0;
6239         ch = un->un_ch;
6240         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
6241                 return 0;
6242         bd = ch->ch_bd;
6243         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
6244                 return 0;
6245         if (bd->state != BOARD_READY)
6246                 return 0;
6247
6248         return snprintf(buf, PAGE_SIZE, "%ld\n", ch->ch_txcount);
6249 }
6250 static DEVICE_ATTR(txcount, S_IRUSR, dgap_tty_txcount_show, NULL);
6251
6252 static ssize_t dgap_tty_name_show(struct device *d,
6253                                   struct device_attribute *attr,
6254                                   char *buf)
6255 {
6256         struct board_t *bd;
6257         struct channel_t *ch;
6258         struct un_t *un;
6259         int     cn;
6260         int     bn;
6261         struct cnode *cptr = NULL;
6262         int found = FALSE;
6263         int ncount = 0;
6264         int starto = 0;
6265         int i = 0;
6266
6267         if (!d)
6268                 return 0;
6269         un = dev_get_drvdata(d);
6270         if (!un || un->magic != DGAP_UNIT_MAGIC)
6271                 return 0;
6272         ch = un->un_ch;
6273         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
6274                 return 0;
6275         bd = ch->ch_bd;
6276         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
6277                 return 0;
6278         if (bd->state != BOARD_READY)
6279                 return 0;
6280
6281         bn = bd->boardnum;
6282         cn = ch->ch_portnum;
6283
6284         for (cptr = bd->bd_config; cptr; cptr = cptr->next) {
6285
6286                 if ((cptr->type == BNODE) &&
6287                     ((cptr->u.board.type == APORT2_920P) ||
6288                      (cptr->u.board.type == APORT4_920P) ||
6289                      (cptr->u.board.type == APORT8_920P) ||
6290                      (cptr->u.board.type == PAPORT4) ||
6291                      (cptr->u.board.type == PAPORT8))) {
6292
6293                         found = TRUE;
6294                         if (cptr->u.board.v_start)
6295                                 starto = cptr->u.board.start;
6296                         else
6297                                 starto = 1;
6298                 }
6299
6300                 if (cptr->type == TNODE && found == TRUE) {
6301                         char *ptr1;
6302                         if (strstr(cptr->u.ttyname, "tty")) {
6303                                 ptr1 = cptr->u.ttyname;
6304                                 ptr1 += 3;
6305                         } else
6306                                 ptr1 = cptr->u.ttyname;
6307
6308                         for (i = 0; i < dgap_config_get_num_prts(bd); i++) {
6309                                 if (cn != i)
6310                                         continue;
6311
6312                                 return snprintf(buf, PAGE_SIZE, "%s%s%02d\n",
6313                                                 (un->un_type == DGAP_PRINT) ?
6314                                                  "pr" : "tty",
6315                                                 ptr1, i + starto);
6316                         }
6317                 }
6318
6319                 if (cptr->type == CNODE) {
6320
6321                         for (i = 0; i < cptr->u.conc.nport; i++) {
6322                                 if (cn != (i + ncount))
6323                                         continue;
6324
6325                                 return snprintf(buf, PAGE_SIZE, "%s%s%02ld\n",
6326                                                 (un->un_type == DGAP_PRINT) ?
6327                                                  "pr" : "tty",
6328                                                 cptr->u.conc.id,
6329                                                 i + (cptr->u.conc.v_start ?
6330                                                      cptr->u.conc.start : 1));
6331                         }
6332
6333                         ncount += cptr->u.conc.nport;
6334                 }
6335
6336                 if (cptr->type == MNODE) {
6337
6338                         for (i = 0; i < cptr->u.module.nport; i++) {
6339                                 if (cn != (i + ncount))
6340                                         continue;
6341
6342                                 return snprintf(buf, PAGE_SIZE, "%s%s%02ld\n",
6343                                                 (un->un_type == DGAP_PRINT) ?
6344                                                  "pr" : "tty",
6345                                                 cptr->u.module.id,
6346                                                 i + (cptr->u.module.v_start ?
6347                                                      cptr->u.module.start : 1));
6348                         }
6349
6350                         ncount += cptr->u.module.nport;
6351
6352                 }
6353         }
6354
6355         return snprintf(buf, PAGE_SIZE, "%s_dgap_%d_%d\n",
6356                 (un->un_type == DGAP_PRINT) ? "pr" : "tty", bn, cn);
6357
6358 }
6359 static DEVICE_ATTR(custom_name, S_IRUSR, dgap_tty_name_show, NULL);
6360
6361 static struct attribute *dgap_sysfs_tty_entries[] = {
6362         &dev_attr_state.attr,
6363         &dev_attr_baud.attr,
6364         &dev_attr_msignals.attr,
6365         &dev_attr_iflag.attr,
6366         &dev_attr_cflag.attr,
6367         &dev_attr_oflag.attr,
6368         &dev_attr_lflag.attr,
6369         &dev_attr_digi_flag.attr,
6370         &dev_attr_rxcount.attr,
6371         &dev_attr_txcount.attr,
6372         &dev_attr_custom_name.attr,
6373         NULL
6374 };
6375
6376 static struct attribute_group dgap_tty_attribute_group = {
6377         .name = NULL,
6378         .attrs = dgap_sysfs_tty_entries,
6379 };
6380
6381 static void dgap_create_tty_sysfs(struct un_t *un, struct device *c)
6382 {
6383         int ret;
6384
6385         ret = sysfs_create_group(&c->kobj, &dgap_tty_attribute_group);
6386         if (ret)
6387                 return;
6388
6389         dev_set_drvdata(c, un);
6390
6391 }
6392
6393 static void dgap_remove_tty_sysfs(struct device *c)
6394 {
6395         sysfs_remove_group(&c->kobj, &dgap_tty_attribute_group);
6396 }
6397
6398 /*
6399  * Parse a configuration file read into memory as a string.
6400  */
6401 static int      dgap_parsefile(char **in, int remove)
6402 {
6403         struct cnode *p, *brd, *line, *conc;
6404         int     rc;
6405         char    *s = NULL;
6406         int     linecnt = 0;
6407
6408         p = &dgap_head;
6409         brd = line = conc = NULL;
6410
6411         /* perhaps we are adding to an existing list? */
6412         while (p->next != NULL)
6413                 p = p->next;
6414
6415         /* file must start with a BEGIN */
6416         while ((rc = dgap_gettok(in, p)) != BEGIN) {
6417                 if (rc == 0) {
6418                         dgap_err("unexpected EOF");
6419                         return -1;
6420                 }
6421         }
6422
6423         for (; ;) {
6424                 rc = dgap_gettok(in, p);
6425                 if (rc == 0) {
6426                         dgap_err("unexpected EOF");
6427                         return -1;
6428                 }
6429
6430                 switch (rc) {
6431                 case 0:
6432                         dgap_err("unexpected end of file");
6433                         return -1;
6434
6435                 case BEGIN:     /* should only be 1 begin */
6436                         dgap_err("unexpected config_begin\n");
6437                         return -1;
6438
6439                 case END:
6440                         return 0;
6441
6442                 case BOARD:     /* board info */
6443                         if (dgap_checknode(p))
6444                                 return -1;
6445                         p->next = dgap_newnode(BNODE);
6446                         if (!p->next) {
6447                                 dgap_err("out of memory");
6448                                 return -1;
6449                         }
6450                         p = p->next;
6451
6452                         p->u.board.status = dgap_savestring("No");
6453                         line = conc = NULL;
6454                         brd = p;
6455                         linecnt = -1;
6456                         break;
6457
6458                 case APORT2_920P:       /* AccelePort_4 */
6459                         if (p->type != BNODE) {
6460                                 dgap_err("unexpected Digi_2r_920 string");
6461                                 return -1;
6462                         }
6463                         p->u.board.type = APORT2_920P;
6464                         p->u.board.v_type = 1;
6465                         break;
6466
6467                 case APORT4_920P:       /* AccelePort_4 */
6468                         if (p->type != BNODE) {
6469                                 dgap_err("unexpected Digi_4r_920 string");
6470                                 return -1;
6471                         }
6472                         p->u.board.type = APORT4_920P;
6473                         p->u.board.v_type = 1;
6474                         break;
6475
6476                 case APORT8_920P:       /* AccelePort_8 */
6477                         if (p->type != BNODE) {
6478                                 dgap_err("unexpected Digi_8r_920 string");
6479                                 return -1;
6480                         }
6481                         p->u.board.type = APORT8_920P;
6482                         p->u.board.v_type = 1;
6483                         break;
6484
6485                 case PAPORT4:   /* AccelePort_4 PCI */
6486                         if (p->type != BNODE) {
6487                                 dgap_err("unexpected Digi_4r(PCI) string");
6488                                 return -1;
6489                         }
6490                         p->u.board.type = PAPORT4;
6491                         p->u.board.v_type = 1;
6492                         break;
6493
6494                 case PAPORT8:   /* AccelePort_8 PCI */
6495                         if (p->type != BNODE) {
6496                                 dgap_err("unexpected Digi_8r string");
6497                                 return -1;
6498                         }
6499                         p->u.board.type = PAPORT8;
6500                         p->u.board.v_type = 1;
6501                         break;
6502
6503                 case PCX:       /* PCI C/X */
6504                         if (p->type != BNODE) {
6505                                 dgap_err("unexpected Digi_C/X_(PCI) string");
6506                                 return -1;
6507                         }
6508                         p->u.board.type = PCX;
6509                         p->u.board.v_type = 1;
6510                         p->u.board.conc1 = 0;
6511                         p->u.board.conc2 = 0;
6512                         p->u.board.module1 = 0;
6513                         p->u.board.module2 = 0;
6514                         break;
6515
6516                 case PEPC:      /* PCI EPC/X */
6517                         if (p->type != BNODE) {
6518                                 dgap_err("unexpected \"Digi_EPC/X_(PCI)\" string");
6519                                 return -1;
6520                         }
6521                         p->u.board.type = PEPC;
6522                         p->u.board.v_type = 1;
6523                         p->u.board.conc1 = 0;
6524                         p->u.board.conc2 = 0;
6525                         p->u.board.module1 = 0;
6526                         p->u.board.module2 = 0;
6527                         break;
6528
6529                 case PPCM:      /* PCI/Xem */
6530                         if (p->type != BNODE) {
6531                                 dgap_err("unexpected PCI/Xem string");
6532                                 return -1;
6533                         }
6534                         p->u.board.type = PPCM;
6535                         p->u.board.v_type = 1;
6536                         p->u.board.conc1 = 0;
6537                         p->u.board.conc2 = 0;
6538                         break;
6539
6540                 case IO:        /* i/o port */
6541                         if (p->type != BNODE) {
6542                                 dgap_err("IO port only vaild for boards");
6543                                 return -1;
6544                         }
6545                         s = dgap_getword(in);
6546                         if (s == NULL) {
6547                                 dgap_err("unexpected end of file");
6548                                 return -1;
6549                         }
6550                         p->u.board.portstr = dgap_savestring(s);
6551                         if (kstrtol(s, 0, &p->u.board.port)) {
6552                                 dgap_err("bad number for IO port");
6553                                 return -1;
6554                         }
6555                         p->u.board.v_port = 1;
6556                         break;
6557
6558                 case MEM:       /* memory address */
6559                         if (p->type != BNODE) {
6560                                 dgap_err("memory address only vaild for boards");
6561                                 return -1;
6562                         }
6563                         s = dgap_getword(in);
6564                         if (s == NULL) {
6565                                 dgap_err("unexpected end of file");
6566                                 return -1;
6567                         }
6568                         p->u.board.addrstr = dgap_savestring(s);
6569                         if (kstrtoul(s, 0, &p->u.board.addr)) {
6570                                 dgap_err("bad number for memory address");
6571                                 return -1;
6572                         }
6573                         p->u.board.v_addr = 1;
6574                         break;
6575
6576                 case PCIINFO:   /* pci information */
6577                         if (p->type != BNODE) {
6578                                 dgap_err("memory address only vaild for boards");
6579                                 return -1;
6580                         }
6581                         s = dgap_getword(in);
6582                         if (s == NULL) {
6583                                 dgap_err("unexpected end of file");
6584                                 return -1;
6585                         }
6586                         p->u.board.pcibusstr = dgap_savestring(s);
6587                         if (kstrtoul(s, 0, &p->u.board.pcibus)) {
6588                                 dgap_err("bad number for pci bus");
6589                                 return -1;
6590                         }
6591                         p->u.board.v_pcibus = 1;
6592                         s = dgap_getword(in);
6593                         if (s == NULL) {
6594                                 dgap_err("unexpected end of file");
6595                                 return -1;
6596                         }
6597                         p->u.board.pcislotstr = dgap_savestring(s);
6598                         if (kstrtoul(s, 0, &p->u.board.pcislot)) {
6599                                 dgap_err("bad number for pci slot");
6600                                 return -1;
6601                         }
6602                         p->u.board.v_pcislot = 1;
6603                         break;
6604
6605                 case METHOD:
6606                         if (p->type != BNODE) {
6607                                 dgap_err("install method only vaild for boards");
6608                                 return -1;
6609                         }
6610                         s = dgap_getword(in);
6611                         if (s == NULL) {
6612                                 dgap_err("unexpected end of file");
6613                                 return -1;
6614                         }
6615                         p->u.board.method = dgap_savestring(s);
6616                         p->u.board.v_method = 1;
6617                         break;
6618
6619                 case STATUS:
6620                         if (p->type != BNODE) {
6621                                 dgap_err("config status only vaild for boards");
6622                                 return -1;
6623                         }
6624                         s = dgap_getword(in);
6625                         if (s == NULL) {
6626                                 dgap_err("unexpected end of file");
6627                                 return -1;
6628                         }
6629                         p->u.board.status = dgap_savestring(s);
6630                         break;
6631
6632                 case NPORTS:    /* number of ports */
6633                         if (p->type == BNODE) {
6634                                 s = dgap_getword(in);
6635                                 if (s == NULL) {
6636                                         dgap_err("unexpected end of file");
6637                                         return -1;
6638                                 }
6639                                 if (kstrtol(s, 0, &p->u.board.nport)) {
6640                                         dgap_err("bad number for number of ports");
6641                                         return -1;
6642                                 }
6643                                 p->u.board.v_nport = 1;
6644                         } else if (p->type == CNODE) {
6645                                 s = dgap_getword(in);
6646                                 if (s == NULL) {
6647                                         dgap_err("unexpected end of file");
6648                                         return -1;
6649                                 }
6650                                 if (kstrtol(s, 0, &p->u.conc.nport)) {
6651                                         dgap_err("bad number for number of ports");
6652                                         return -1;
6653                                 }
6654                                 p->u.conc.v_nport = 1;
6655                         } else if (p->type == MNODE) {
6656                                 s = dgap_getword(in);
6657                                 if (s == NULL) {
6658                                         dgap_err("unexpected end of file");
6659                                         return -1;
6660                                 }
6661                                 if (kstrtol(s, 0, &p->u.module.nport)) {
6662                                         dgap_err("bad number for number of ports");
6663                                         return -1;
6664                                 }
6665                                 p->u.module.v_nport = 1;
6666                         } else {
6667                                 dgap_err("nports only valid for concentrators or modules");
6668                                 return -1;
6669                         }
6670                         break;
6671
6672                 case ID:        /* letter ID used in tty name */
6673                         s = dgap_getword(in);
6674                         if (s == NULL) {
6675                                 dgap_err("unexpected end of file");
6676                                 return -1;
6677                         }
6678
6679                         p->u.board.status = dgap_savestring(s);
6680
6681                         if (p->type == CNODE) {
6682                                 p->u.conc.id = dgap_savestring(s);
6683                                 p->u.conc.v_id = 1;
6684                         } else if (p->type == MNODE) {
6685                                 p->u.module.id = dgap_savestring(s);
6686                                 p->u.module.v_id = 1;
6687                         } else {
6688                                 dgap_err("id only valid for concentrators or modules");
6689                                 return -1;
6690                         }
6691                         break;
6692
6693                 case STARTO:    /* start offset of ID */
6694                         if (p->type == BNODE) {
6695                                 s = dgap_getword(in);
6696                                 if (s == NULL) {
6697                                         dgap_err("unexpected end of file");
6698                                         return -1;
6699                                 }
6700                                 if (kstrtol(s, 0, &p->u.board.start)) {
6701                                         dgap_err("bad number for start of tty count");
6702                                         return -1;
6703                                 }
6704                                 p->u.board.v_start = 1;
6705                         } else if (p->type == CNODE) {
6706                                 s = dgap_getword(in);
6707                                 if (s == NULL) {
6708                                         dgap_err("unexpected end of file");
6709                                         return -1;
6710                                 }
6711                                 if (kstrtol(s, 0, &p->u.conc.start)) {
6712                                         dgap_err("bad number for start of tty count");
6713                                         return -1;
6714                                 }
6715                                 p->u.conc.v_start = 1;
6716                         } else if (p->type == MNODE) {
6717                                 s = dgap_getword(in);
6718                                 if (s == NULL) {
6719                                         dgap_err("unexpected end of file");
6720                                         return -1;
6721                                 }
6722                                 if (kstrtol(s, 0, &p->u.module.start)) {
6723                                         dgap_err("bad number for start of tty count");
6724                                         return -1;
6725                                 }
6726                                 p->u.module.v_start = 1;
6727                         } else {
6728                                 dgap_err("start only valid for concentrators or modules");
6729                                 return -1;
6730                         }
6731                         break;
6732
6733                 case TTYN:      /* tty name prefix */
6734                         if (dgap_checknode(p))
6735                                 return -1;
6736                         p->next = dgap_newnode(TNODE);
6737                         if (!p->next) {
6738                                 dgap_err("out of memory");
6739                                 return -1;
6740                         }
6741                         p = p->next;
6742                         s = dgap_getword(in);
6743                         if (!s) {
6744                                 dgap_err("unexpeced end of file");
6745                                 return -1;
6746                         }
6747                         p->u.ttyname = dgap_savestring(s);
6748                         if (!p->u.ttyname) {
6749                                 dgap_err("out of memory");
6750                                 return -1;
6751                         }
6752                         break;
6753
6754                 case CU:        /* cu name prefix */
6755                         if (dgap_checknode(p))
6756                                 return -1;
6757                         p->next = dgap_newnode(CUNODE);
6758                         if (!p->next) {
6759                                 dgap_err("out of memory");
6760                                 return -1;
6761                         }
6762                         p = p->next;
6763                         s = dgap_getword(in);
6764                         if (!s) {
6765                                 dgap_err("unexpeced end of file");
6766                                 return -1;
6767                         }
6768                         p->u.cuname = dgap_savestring(s);
6769                         if (!p->u.cuname) {
6770                                 dgap_err("out of memory");
6771                                 return -1;
6772                         }
6773                         break;
6774
6775                 case LINE:      /* line information */
6776                         if (dgap_checknode(p))
6777                                 return -1;
6778                         if (brd == NULL) {
6779                                 dgap_err("must specify board before line info");
6780                                 return -1;
6781                         }
6782                         switch (brd->u.board.type) {
6783                         case PPCM:
6784                                 dgap_err("line not vaild for PC/em");
6785                                 return -1;
6786                         }
6787                         p->next = dgap_newnode(LNODE);
6788                         if (!p->next) {
6789                                 dgap_err("out of memory");
6790                                 return -1;
6791                         }
6792                         p = p->next;
6793                         conc = NULL;
6794                         line = p;
6795                         linecnt++;
6796                         break;
6797
6798                 case CONC:      /* concentrator information */
6799                         if (dgap_checknode(p))
6800                                 return -1;
6801                         if (line == NULL) {
6802                                 dgap_err("must specify line info before concentrator");
6803                                 return -1;
6804                         }
6805                         p->next = dgap_newnode(CNODE);
6806                         if (!p->next) {
6807                                 dgap_err("out of memory");
6808                                 return -1;
6809                         }
6810                         p = p->next;
6811                         conc = p;
6812                         if (linecnt)
6813                                 brd->u.board.conc2++;
6814                         else
6815                                 brd->u.board.conc1++;
6816
6817                         break;
6818
6819                 case CX:        /* c/x type concentrator */
6820                         if (p->type != CNODE) {
6821                                 dgap_err("cx only valid for concentrators");
6822                                 return -1;
6823                         }
6824                         p->u.conc.type = CX;
6825                         p->u.conc.v_type = 1;
6826                         break;
6827
6828                 case EPC:       /* epc type concentrator */
6829                         if (p->type != CNODE) {
6830                                 dgap_err("cx only valid for concentrators");
6831                                 return -1;
6832                         }
6833                         p->u.conc.type = EPC;
6834                         p->u.conc.v_type = 1;
6835                         break;
6836
6837                 case MOD:       /* EBI module */
6838                         if (dgap_checknode(p))
6839                                 return -1;
6840                         if (brd == NULL) {
6841                                 dgap_err("must specify board info before EBI modules");
6842                                 return -1;
6843                         }
6844                         switch (brd->u.board.type) {
6845                         case PPCM:
6846                                 linecnt = 0;
6847                                 break;
6848                         default:
6849                                 if (conc == NULL) {
6850                                         dgap_err("must specify concentrator info before EBI module");
6851                                         return -1;
6852                                 }
6853                         }
6854                         p->next = dgap_newnode(MNODE);
6855                         if (!p->next) {
6856                                 dgap_err("out of memory");
6857                                 return -1;
6858                         }
6859                         p = p->next;
6860                         if (linecnt)
6861                                 brd->u.board.module2++;
6862                         else
6863                                 brd->u.board.module1++;
6864
6865                         break;
6866
6867                 case PORTS:     /* ports type EBI module */
6868                         if (p->type != MNODE) {
6869                                 dgap_err("ports only valid for EBI modules");
6870                                 return -1;
6871                         }
6872                         p->u.module.type = PORTS;
6873                         p->u.module.v_type = 1;
6874                         break;
6875
6876                 case MODEM:     /* ports type EBI module */
6877                         if (p->type != MNODE) {
6878                                 dgap_err("modem only valid for modem modules");
6879                                 return -1;
6880                         }
6881                         p->u.module.type = MODEM;
6882                         p->u.module.v_type = 1;
6883                         break;
6884
6885                 case CABLE:
6886                         if (p->type == LNODE) {
6887                                 s = dgap_getword(in);
6888                                 if (!s) {
6889                                         dgap_err("unexpected end of file");
6890                                         return -1;
6891                                 }
6892                                 p->u.line.cable = dgap_savestring(s);
6893                                 p->u.line.v_cable = 1;
6894                         }
6895                         break;
6896
6897                 case SPEED:     /* sync line speed indication */
6898                         if (p->type == LNODE) {
6899                                 s = dgap_getword(in);
6900                                 if (s == NULL) {
6901                                         dgap_err("unexpected end of file");
6902                                         return -1;
6903                                 }
6904                                 if (kstrtol(s, 0, &p->u.line.speed)) {
6905                                         dgap_err("bad number for line speed");
6906                                         return -1;
6907                                 }
6908                                 p->u.line.v_speed = 1;
6909                         } else if (p->type == CNODE) {
6910                                 s = dgap_getword(in);
6911                                 if (s == NULL) {
6912                                         dgap_err("unexpected end of file");
6913                                         return -1;
6914                                 }
6915                                 if (kstrtol(s, 0, &p->u.conc.speed)) {
6916                                         dgap_err("bad number for line speed");
6917                                         return -1;
6918                                 }
6919                                 p->u.conc.v_speed = 1;
6920                         } else {
6921                                 dgap_err("speed valid only for lines or concentrators.");
6922                                 return -1;
6923                         }
6924                         break;
6925
6926                 case CONNECT:
6927                         if (p->type == CNODE) {
6928                                 s = dgap_getword(in);
6929                                 if (!s) {
6930                                         dgap_err("unexpected end of file");
6931                                         return -1;
6932                                 }
6933                                 p->u.conc.connect = dgap_savestring(s);
6934                                 p->u.conc.v_connect = 1;
6935                         }
6936                         break;
6937                 case PRINT:     /* transparent print name prefix */
6938                         if (dgap_checknode(p))
6939                                 return -1;
6940                         p->next = dgap_newnode(PNODE);
6941                         if (!p->next) {
6942                                 dgap_err("out of memory");
6943                                 return -1;
6944                         }
6945                         p = p->next;
6946                         s = dgap_getword(in);
6947                         if (!s) {
6948                                 dgap_err("unexpeced end of file");
6949                                 return -1;
6950                         }
6951                         p->u.printname = dgap_savestring(s);
6952                         if (!p->u.printname) {
6953                                 dgap_err("out of memory");
6954                                 return -1;
6955                         }
6956                         break;
6957
6958                 case CMAJOR:    /* major number */
6959                         if (dgap_checknode(p))
6960                                 return -1;
6961                         p->next = dgap_newnode(JNODE);
6962                         if (!p->next) {
6963                                 dgap_err("out of memory");
6964                                 return -1;
6965                         }
6966                         p = p->next;
6967                         s = dgap_getword(in);
6968                         if (s == NULL) {
6969                                 dgap_err("unexpected end of file");
6970                                 return -1;
6971                         }
6972                         if (kstrtol(s, 0, &p->u.majornumber)) {
6973                                 dgap_err("bad number for major number");
6974                                 return -1;
6975                         }
6976                         break;
6977
6978                 case ALTPIN:    /* altpin setting */
6979                         if (dgap_checknode(p))
6980                                 return -1;
6981                         p->next = dgap_newnode(ANODE);
6982                         if (!p->next) {
6983                                 dgap_err("out of memory");
6984                                 return -1;
6985                         }
6986                         p = p->next;
6987                         s = dgap_getword(in);
6988                         if (s == NULL) {
6989                                 dgap_err("unexpected end of file");
6990                                 return -1;
6991                         }
6992                         if (kstrtol(s, 0, &p->u.altpin)) {
6993                                 dgap_err("bad number for altpin");
6994                                 return -1;
6995                         }
6996                         break;
6997
6998                 case USEINTR:           /* enable interrupt setting */
6999                         if (dgap_checknode(p))
7000                                 return -1;
7001                         p->next = dgap_newnode(INTRNODE);
7002                         if (!p->next) {
7003                                 dgap_err("out of memory");
7004                                 return -1;
7005                         }
7006                         p = p->next;
7007                         s = dgap_getword(in);
7008                         if (s == NULL) {
7009                                 dgap_err("unexpected end of file");
7010                                 return -1;
7011                         }
7012                         if (kstrtol(s, 0, &p->u.useintr)) {
7013                                 dgap_err("bad number for useintr");
7014                                 return -1;
7015                         }
7016                         break;
7017
7018                 case TTSIZ:     /* size of tty structure */
7019                         if (dgap_checknode(p))
7020                                 return -1;
7021                         p->next = dgap_newnode(TSNODE);
7022                         if (!p->next) {
7023                                 dgap_err("out of memory");
7024                                 return -1;
7025                         }
7026                         p = p->next;
7027                         s = dgap_getword(in);
7028                         if (s == NULL) {
7029                                 dgap_err("unexpected end of file");
7030                                 return -1;
7031                         }
7032                         if (kstrtol(s, 0, &p->u.ttysize)) {
7033                                 dgap_err("bad number for ttysize");
7034                                 return -1;
7035                         }
7036                         break;
7037
7038                 case CHSIZ:     /* channel structure size */
7039                         if (dgap_checknode(p))
7040                                 return -1;
7041                         p->next = dgap_newnode(CSNODE);
7042                         if (!p->next) {
7043                                 dgap_err("out of memory");
7044                                 return -1;
7045                         }
7046                         p = p->next;
7047                         s = dgap_getword(in);
7048                         if (s == NULL) {
7049                                 dgap_err("unexpected end of file");
7050                                 return -1;
7051                         }
7052                         if (kstrtol(s, 0, &p->u.chsize)) {
7053                                 dgap_err("bad number for chsize");
7054                                 return -1;
7055                         }
7056                         break;
7057
7058                 case BSSIZ:     /* board structure size */
7059                         if (dgap_checknode(p))
7060                                 return -1;
7061                         p->next = dgap_newnode(BSNODE);
7062                         if (!p->next) {
7063                                 dgap_err("out of memory");
7064                                 return -1;
7065                         }
7066                         p = p->next;
7067                         s = dgap_getword(in);
7068                         if (s == NULL) {
7069                                 dgap_err("unexpected end of file");
7070                                 return -1;
7071                         }
7072                         if (kstrtol(s, 0, &p->u.bssize)) {
7073                                 dgap_err("bad number for bssize");
7074                                 return -1;
7075                         }
7076                         break;
7077
7078                 case UNTSIZ:    /* sched structure size */
7079                         if (dgap_checknode(p))
7080                                 return -1;
7081                         p->next = dgap_newnode(USNODE);
7082                         if (!p->next) {
7083                                 dgap_err("out of memory");
7084                                 return -1;
7085                         }
7086                         p = p->next;
7087                         s = dgap_getword(in);
7088                         if (s == NULL) {
7089                                 dgap_err("unexpected end of file");
7090                                 return -1;
7091                         }
7092                         if (kstrtol(s, 0, &p->u.unsize)) {
7093                                 dgap_err("bad number for schedsize");
7094                                 return -1;
7095                         }
7096                         break;
7097
7098                 case F2SIZ:     /* f2200 structure size */
7099                         if (dgap_checknode(p))
7100                                 return -1;
7101                         p->next = dgap_newnode(FSNODE);
7102                         if (!p->next) {
7103                                 dgap_err("out of memory");
7104                                 return -1;
7105                         }
7106                         p = p->next;
7107                         s = dgap_getword(in);
7108                         if (s == NULL) {
7109                                 dgap_err("unexpected end of file");
7110                                 return -1;
7111                         }
7112                         if (kstrtol(s, 0, &p->u.f2size)) {
7113                                 dgap_err("bad number for f2200size");
7114                                 return -1;
7115                         }
7116                         break;
7117
7118                 case VPSIZ:     /* vpix structure size */
7119                         if (dgap_checknode(p))
7120                                 return -1;
7121                         p->next = dgap_newnode(VSNODE);
7122                         if (!p->next) {
7123                                 dgap_err("out of memory");
7124                                 return -1;
7125                         }
7126                         p = p->next;
7127                         s = dgap_getword(in);
7128                         if (s == NULL) {
7129                                 dgap_err("unexpected end of file");
7130                                 return -1;
7131                         }
7132                         if (kstrtol(s, 0, &p->u.vpixsize)) {
7133                                 dgap_err("bad number for vpixsize");
7134                                 return -1;
7135                         }
7136                         break;
7137                 }
7138         }
7139 }
7140
7141 /*
7142  * dgap_sindex: much like index(), but it looks for a match of any character in
7143  * the group, and returns that position.  If the first character is a ^, then
7144  * this will match the first occurrence not in that group.
7145  */
7146 static char *dgap_sindex(char *string, char *group)
7147 {
7148         char    *ptr;
7149
7150         if (!string || !group)
7151                 return (char *) NULL;
7152
7153         if (*group == '^') {
7154                 group++;
7155                 for (; *string; string++) {
7156                         for (ptr = group; *ptr; ptr++) {
7157                                 if (*ptr == *string)
7158                                         break;
7159                         }
7160                         if (*ptr == '\0')
7161                                 return string;
7162                 }
7163         } else {
7164                 for (; *string; string++) {
7165                         for (ptr = group; *ptr; ptr++) {
7166                                 if (*ptr == *string)
7167                                         return string;
7168                         }
7169                 }
7170         }
7171
7172         return (char *) NULL;
7173 }
7174
7175 /*
7176  * Get a token from the input file; return 0 if end of file is reached
7177  */
7178 static int dgap_gettok(char **in, struct cnode *p)
7179 {
7180         char    *w;
7181         struct toklist *t;
7182
7183         if (strstr(dgap_cword, "boar")) {
7184                 w = dgap_getword(in);
7185                 snprintf(dgap_cword, MAXCWORD, "%s", w);
7186                 for (t = dgap_tlist; t->token != 0; t++) {
7187                         if (!strcmp(w, t->string))
7188                                 return t->token;
7189                 }
7190                 dgap_err("board !!type not specified");
7191                 return 1;
7192         } else {
7193                 while ((w = dgap_getword(in))) {
7194                         snprintf(dgap_cword, MAXCWORD, "%s", w);
7195                         for (t = dgap_tlist; t->token != 0; t++) {
7196                                 if (!strcmp(w, t->string))
7197                                         return t->token;
7198                         }
7199                 }
7200                 return 0;
7201         }
7202 }
7203
7204 /*
7205  * get a word from the input stream, also keep track of current line number.
7206  * words are separated by whitespace.
7207  */
7208 static char *dgap_getword(char **in)
7209 {
7210         char *ret_ptr = *in;
7211
7212         char *ptr = dgap_sindex(*in, " \t\n");
7213
7214         /* If no word found, return null */
7215         if (!ptr)
7216                 return NULL;
7217
7218         /* Mark new location for our buffer */
7219         *ptr = '\0';
7220         *in = ptr + 1;
7221
7222         /* Eat any extra spaces/tabs/newlines that might be present */
7223         while (*in && **in && ((**in == ' ') ||
7224                                (**in == '\t') ||
7225                                (**in == '\n'))) {
7226                 **in = '\0';
7227                 *in = *in + 1;
7228         }
7229
7230         return ret_ptr;
7231 }
7232
7233 /*
7234  * print an error message, giving the line number in the file where
7235  * the error occurred.
7236  */
7237 static void dgap_err(char *s)
7238 {
7239         pr_err("dgap: parse: %s\n", s);
7240 }
7241
7242 /*
7243  * allocate a new configuration node of type t
7244  */
7245 static struct cnode *dgap_newnode(int t)
7246 {
7247         struct cnode *n;
7248
7249         n = kmalloc(sizeof(struct cnode), GFP_ATOMIC);
7250         if (n != NULL) {
7251                 memset((char *)n, 0, sizeof(struct cnode));
7252                 n->type = t;
7253         }
7254         return n;
7255 }
7256
7257 /*
7258  * dgap_checknode: see if all the necessary info has been supplied for a node
7259  * before creating the next node.
7260  */
7261 static int dgap_checknode(struct cnode *p)
7262 {
7263         switch (p->type) {
7264         case BNODE:
7265                 if (p->u.board.v_type == 0) {
7266                         dgap_err("board type !not specified");
7267                         return 1;
7268                 }
7269
7270                 return 0;
7271
7272         case LNODE:
7273                 if (p->u.line.v_speed == 0) {
7274                         dgap_err("line speed not specified");
7275                         return 1;
7276                 }
7277                 return 0;
7278
7279         case CNODE:
7280                 if (p->u.conc.v_type == 0) {
7281                         dgap_err("concentrator type not specified");
7282                         return 1;
7283                 }
7284                 if (p->u.conc.v_speed == 0) {
7285                         dgap_err("concentrator line speed not specified");
7286                         return 1;
7287                 }
7288                 if (p->u.conc.v_nport == 0) {
7289                         dgap_err("number of ports on concentrator not specified");
7290                         return 1;
7291                 }
7292                 if (p->u.conc.v_id == 0) {
7293                         dgap_err("concentrator id letter not specified");
7294                         return 1;
7295                 }
7296                 return 0;
7297
7298         case MNODE:
7299                 if (p->u.module.v_type == 0) {
7300                         dgap_err("EBI module type not specified");
7301                         return 1;
7302                 }
7303                 if (p->u.module.v_nport == 0) {
7304                         dgap_err("number of ports on EBI module not specified");
7305                         return 1;
7306                 }
7307                 if (p->u.module.v_id == 0) {
7308                         dgap_err("EBI module id letter not specified");
7309                         return 1;
7310                 }
7311                 return 0;
7312         }
7313         return 0;
7314 }
7315
7316 /*
7317  * save a string somewhere
7318  */
7319 static char     *dgap_savestring(char *s)
7320 {
7321         char    *p;
7322
7323         p = kmalloc(strlen(s) + 1, GFP_ATOMIC);
7324         if (p)
7325                 strcpy(p, s);
7326         return p;
7327 }
7328
7329 /*
7330  * Given a board pointer, returns whether we should use interrupts or not.
7331  */
7332 static uint dgap_config_get_useintr(struct board_t *bd)
7333 {
7334         struct cnode *p = NULL;
7335
7336         if (!bd)
7337                 return 0;
7338
7339         for (p = bd->bd_config; p; p = p->next) {
7340                 switch (p->type) {
7341                 case INTRNODE:
7342                         /*
7343                          * check for pcxr types.
7344                          */
7345                         return p->u.useintr;
7346                 default:
7347                         break;
7348                 }
7349         }
7350
7351         /* If not found, then don't turn on interrupts. */
7352         return 0;
7353 }
7354
7355 /*
7356  * Given a board pointer, returns whether we turn on altpin or not.
7357  */
7358 static uint dgap_config_get_altpin(struct board_t *bd)
7359 {
7360         struct cnode *p = NULL;
7361
7362         if (!bd)
7363                 return 0;
7364
7365         for (p = bd->bd_config; p; p = p->next) {
7366                 switch (p->type) {
7367                 case ANODE:
7368                         /*
7369                          * check for pcxr types.
7370                          */
7371                         return p->u.altpin;
7372                 default:
7373                         break;
7374                 }
7375         }
7376
7377         /* If not found, then don't turn on interrupts. */
7378         return 0;
7379 }
7380
7381 /*
7382  * Given a specific type of board, if found, detached link and
7383  * returns the first occurrence in the list.
7384  */
7385 static struct cnode *dgap_find_config(int type, int bus, int slot)
7386 {
7387         struct cnode *p, *prev = NULL, *prev2 = NULL, *found = NULL;
7388
7389         p = &dgap_head;
7390
7391         while (p->next != NULL) {
7392                 prev = p;
7393                 p = p->next;
7394
7395                 if (p->type == BNODE) {
7396
7397                         if (p->u.board.type == type) {
7398
7399                                 if (p->u.board.v_pcibus &&
7400                                     p->u.board.pcibus != bus)
7401                                         continue;
7402                                 if (p->u.board.v_pcislot &&
7403                                     p->u.board.pcislot != slot)
7404                                         continue;
7405
7406                                 found = p;
7407                                 /*
7408                                  * Keep walking thru the list till we
7409                                  * find the next board.
7410                                  */
7411                                 while (p->next != NULL) {
7412                                         prev2 = p;
7413                                         p = p->next;
7414                                         if (p->type == BNODE) {
7415
7416                                                 /*
7417                                                  * Mark the end of our 1 board
7418                                                  * chain of configs.
7419                                                  */
7420                                                 prev2->next = NULL;
7421
7422                                                 /*
7423                                                  * Link the "next" board to the
7424                                                  * previous board, effectively
7425                                                  * "unlinking" our board from
7426                                                  * the main config.
7427                                                  */
7428                                                 prev->next = p;
7429
7430                                                 return found;
7431                                         }
7432                                 }
7433                                 /*
7434                                  * It must be the last board in the list.
7435                                  */
7436                                 prev->next = NULL;
7437                                 return found;
7438                         }
7439                 }
7440         }
7441         return NULL;
7442 }
7443
7444 /*
7445  * Given a board pointer, walks the config link, counting up
7446  * all ports user specified should be on the board.
7447  * (This does NOT mean they are all actually present right now tho)
7448  */
7449 static uint dgap_config_get_num_prts(struct board_t *bd)
7450 {
7451         int count = 0;
7452         struct cnode *p = NULL;
7453
7454         if (!bd)
7455                 return 0;
7456
7457         for (p = bd->bd_config; p; p = p->next) {
7458
7459                 switch (p->type) {
7460                 case BNODE:
7461                         /*
7462                          * check for pcxr types.
7463                          */
7464                         if (p->u.board.type > EPCFE)
7465                                 count += p->u.board.nport;
7466                         break;
7467                 case CNODE:
7468                         count += p->u.conc.nport;
7469                         break;
7470                 case MNODE:
7471                         count += p->u.module.nport;
7472                         break;
7473                 }
7474         }
7475         return count;
7476 }
7477
7478 static char *dgap_create_config_string(struct board_t *bd, char *string)
7479 {
7480         char *ptr = string;
7481         struct cnode *p = NULL;
7482         struct cnode *q = NULL;
7483         int speed;
7484
7485         if (!bd) {
7486                 *ptr = 0xff;
7487                 return string;
7488         }
7489
7490         for (p = bd->bd_config; p; p = p->next) {
7491
7492                 switch (p->type) {
7493                 case LNODE:
7494                         *ptr = '\0';
7495                         ptr++;
7496                         *ptr = p->u.line.speed;
7497                         ptr++;
7498                         break;
7499                 case CNODE:
7500                         /*
7501                          * Because the EPC/con concentrators can have EM modules
7502                          * hanging off of them, we have to walk ahead in the
7503                          * list and keep adding the number of ports on each EM
7504                          * to the config. UGH!
7505                          */
7506                         speed = p->u.conc.speed;
7507                         q = p->next;
7508                         if ((q != NULL) && (q->type == MNODE)) {
7509                                 *ptr = (p->u.conc.nport + 0x80);
7510                                 ptr++;
7511                                 p = q;
7512                                 while ((q->next != NULL) &&
7513                                        (q->next->type) == MNODE) {
7514
7515                                         *ptr = (q->u.module.nport + 0x80);
7516                                         ptr++;
7517                                         p = q;
7518                                         q = q->next;
7519                                 }
7520                                 *ptr = q->u.module.nport;
7521                                 ptr++;
7522                         } else {
7523                                 *ptr = p->u.conc.nport;
7524                                 ptr++;
7525                         }
7526
7527                         *ptr = speed;
7528                         ptr++;
7529                         break;
7530                 }
7531         }
7532
7533         *ptr = 0xff;
7534         return string;
7535 }