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