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