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