1efb6dfba833ce0a288cb5382e2a160974d0f3f7
[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         ulong  lock_flags2;
4716         char *vaddr;
4717         u16 head, tail;
4718
4719         if (!bd || (bd->magic != DGAP_BOARD_MAGIC))
4720                 return;
4721
4722         if (bd->inhibit_poller)
4723                 return;
4724
4725         DGAP_LOCK(bd->bd_lock, lock_flags);
4726
4727         vaddr = bd->re_map_membase;
4728
4729         /*
4730          * If board is ready, parse deeper to see if there is anything to do.
4731          */
4732         if (bd->state == BOARD_READY) {
4733
4734                 struct ev_t *eaddr = NULL;
4735
4736                 if (!bd->re_map_membase) {
4737                         DGAP_UNLOCK(bd->bd_lock, lock_flags);
4738                         return;
4739                 }
4740                 if (!bd->re_map_port) {
4741                         DGAP_UNLOCK(bd->bd_lock, lock_flags);
4742                         return;
4743                 }
4744
4745                 if (!bd->nasync)
4746                         goto out;
4747
4748                 eaddr = (struct ev_t *) (vaddr + EVBUF);
4749
4750                 /* Get our head and tail */
4751                 head = readw(&(eaddr->ev_head));
4752                 tail = readw(&(eaddr->ev_tail));
4753
4754                 /*
4755                  * If there is an event pending. Go service it.
4756                  */
4757                 if (head != tail) {
4758                         DGAP_UNLOCK(bd->bd_lock, lock_flags);
4759                         dgap_event(bd);
4760                         DGAP_LOCK(bd->bd_lock, lock_flags);
4761                 }
4762
4763 out:
4764                 /*
4765                  * If board is doing interrupts, ACK the interrupt.
4766                  */
4767                 if (bd && bd->intr_running)
4768                         readb(bd->re_map_port + 2);
4769
4770                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
4771                 return;
4772         }
4773
4774         /* Our state machine to get the board up and running */
4775
4776         /* Reset board */
4777         if (bd->state == NEED_RESET) {
4778
4779                 /* Get VPD info */
4780                 dgap_get_vpd(bd);
4781
4782                 dgap_do_reset_board(bd);
4783         }
4784
4785         /* Move to next state */
4786         if (bd->state == FINISHED_RESET)
4787                 bd->state = NEED_CONFIG;
4788
4789         if (bd->state == NEED_CONFIG) {
4790                 /*
4791                  * Match this board to a config the user created for us.
4792                  */
4793                 bd->bd_config = dgap_find_config(bd->type, bd->pci_bus,
4794                                                         bd->pci_slot);
4795
4796                 /*
4797                  * Because the 4 port Xr products share the same PCI ID
4798                  * as the 8 port Xr products, if we receive a NULL config
4799                  * back, and this is a PAPORT8 board, retry with a
4800                  * PAPORT4 attempt as well.
4801                  */
4802                 if (bd->type == PAPORT8 && !bd->bd_config)
4803                         bd->bd_config = dgap_find_config(PAPORT4, bd->pci_bus,
4804                                                                 bd->pci_slot);
4805
4806                 /*
4807                  * Register the ttys (if any) into the kernel.
4808                  */
4809                 if (bd->bd_config)
4810                         bd->state = FINISHED_CONFIG;
4811                 else
4812                         bd->state = CONFIG_NOT_FOUND;
4813         }
4814
4815         /* Move to next state */
4816         if (bd->state == FINISHED_CONFIG)
4817                 bd->state = NEED_DEVICE_CREATION;
4818
4819         /* Move to next state */
4820         if (bd->state == NEED_DEVICE_CREATION) {
4821                 /*
4822                  * Signal downloader, its got some work to do.
4823                  */
4824                 DGAP_LOCK(dgap_dl_lock, lock_flags2);
4825                 if (dgap_dl_action != 1) {
4826                         dgap_dl_action = 1;
4827                         wake_up_interruptible(&dgap_dl_wait);
4828                 }
4829                 DGAP_UNLOCK(dgap_dl_lock, lock_flags2);
4830         }
4831
4832         /* Move to next state */
4833         if (bd->state == FINISHED_DEVICE_CREATION)
4834                 bd->state = NEED_BIOS_LOAD;
4835
4836         /* Move to next state */
4837         if (bd->state == NEED_BIOS_LOAD) {
4838                 /*
4839                  * Signal downloader, its got some work to do.
4840                  */
4841                 DGAP_LOCK(dgap_dl_lock, lock_flags2);
4842                 if (dgap_dl_action != 1) {
4843                         dgap_dl_action = 1;
4844                         wake_up_interruptible(&dgap_dl_wait);
4845                 }
4846                 DGAP_UNLOCK(dgap_dl_lock, lock_flags2);
4847         }
4848
4849         /* Wait for BIOS to test board... */
4850         if (bd->state == WAIT_BIOS_LOAD)
4851                 dgap_do_wait_for_bios(bd);
4852
4853         /* Move to next state */
4854         if (bd->state == FINISHED_BIOS_LOAD) {
4855                 bd->state = NEED_FEP_LOAD;
4856
4857                 /*
4858                  * Signal downloader, its got some work to do.
4859                  */
4860                 DGAP_LOCK(dgap_dl_lock, lock_flags2);
4861                 if (dgap_dl_action != 1) {
4862                         dgap_dl_action = 1;
4863                         wake_up_interruptible(&dgap_dl_wait);
4864                 }
4865                 DGAP_UNLOCK(dgap_dl_lock, lock_flags2);
4866         }
4867
4868         /* Wait for FEP to load on board... */
4869         if (bd->state == WAIT_FEP_LOAD)
4870                 dgap_do_wait_for_fep(bd);
4871
4872         /* Move to next state */
4873         if (bd->state == FINISHED_FEP_LOAD) {
4874
4875                 /*
4876                  * Do tty device initialization.
4877                  */
4878                 int rc = dgap_tty_init(bd);
4879
4880                 if (rc < 0) {
4881                         dgap_tty_uninit(bd);
4882                         bd->state = BOARD_FAILED;
4883                         bd->dpastatus = BD_NOFEP;
4884                 } else {
4885                         bd->state = NEED_PROC_CREATION;
4886
4887                         /*
4888                          * Signal downloader, its got some work to do.
4889                          */
4890                         DGAP_LOCK(dgap_dl_lock, lock_flags2);
4891                         if (dgap_dl_action != 1) {
4892                                 dgap_dl_action = 1;
4893                                 wake_up_interruptible(&dgap_dl_wait);
4894                         }
4895                         DGAP_UNLOCK(dgap_dl_lock, lock_flags2);
4896                 }
4897         }
4898
4899         /* Move to next state */
4900         if (bd->state == FINISHED_PROC_CREATION) {
4901
4902                 bd->state = BOARD_READY;
4903                 bd->dpastatus = BD_RUNNING;
4904
4905                 /*
4906                  * If user requested the board to run in interrupt mode,
4907                  * go and set it up on the board.
4908                  */
4909                 if (bd->intr_used) {
4910                         writew(1, (bd->re_map_membase + ENABLE_INTR));
4911                         /*
4912                          * Tell the board to poll the UARTS as fast as possible.
4913                          */
4914                         writew(FEPPOLL_MIN, (bd->re_map_membase + FEPPOLL));
4915                         bd->intr_running = 1;
4916                 }
4917
4918                 /* Wake up anyone waiting for board state to change to ready */
4919                 wake_up_interruptible(&bd->state_wait);
4920         }
4921
4922         DGAP_UNLOCK(bd->bd_lock, lock_flags);
4923 }
4924
4925 /*=======================================================================
4926  *
4927  *      dgap_cmdb - Sends a 2 byte command to the FEP.
4928  *
4929  *              ch      - Pointer to channel structure.
4930  *              cmd     - Command to be sent.
4931  *              byte1   - Integer containing first byte to be sent.
4932  *              byte2   - Integer containing second byte to be sent.
4933  *              ncmds   - Wait until ncmds or fewer cmds are left
4934  *                        in the cmd buffer before returning.
4935  *
4936  *=======================================================================*/
4937 static void dgap_cmdb(struct channel_t *ch, uchar cmd, uchar byte1,
4938                         uchar byte2, uint ncmds)
4939 {
4940         char            *vaddr = NULL;
4941         struct cm_t     *cm_addr = NULL;
4942         uint            count;
4943         uint            n;
4944         u16             head;
4945         u16             tail;
4946
4947         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
4948                 return;
4949
4950         /*
4951          * Check if board is still alive.
4952          */
4953         if (ch->ch_bd->state == BOARD_FAILED)
4954                 return;
4955
4956         /*
4957          * Make sure the pointers are in range before
4958          * writing to the FEP memory.
4959          */
4960         vaddr = ch->ch_bd->re_map_membase;
4961
4962         if (!vaddr)
4963                 return;
4964
4965         cm_addr = (struct cm_t *) (vaddr + CMDBUF);
4966         head = readw(&(cm_addr->cm_head));
4967
4968         /*
4969          * Forget it if pointers out of range.
4970          */
4971         if (head >= (CMDMAX - CMDSTART) || (head & 03)) {
4972                 ch->ch_bd->state = BOARD_FAILED;
4973                 return;
4974         }
4975
4976         /*
4977          * Put the data in the circular command buffer.
4978          */
4979         writeb(cmd, (char *) (vaddr + head + CMDSTART + 0));
4980         writeb((uchar) ch->ch_portnum, (char *) (vaddr + head + CMDSTART + 1));
4981         writeb(byte1, (char *) (vaddr + head + CMDSTART + 2));
4982         writeb(byte2, (char *) (vaddr + head + CMDSTART + 3));
4983
4984         head = (head + 4) & (CMDMAX - CMDSTART - 4);
4985
4986         writew(head, &(cm_addr->cm_head));
4987
4988         /*
4989          * Wait if necessary before updating the head
4990          * pointer to limit the number of outstanding
4991          * commands to the FEP.   If the time spent waiting
4992          * is outlandish, declare the FEP dead.
4993          */
4994         for (count = dgap_count ;;) {
4995
4996                 head = readw(&(cm_addr->cm_head));
4997                 tail = readw(&(cm_addr->cm_tail));
4998
4999                 n = (head - tail) & (CMDMAX - CMDSTART - 4);
5000
5001                 if (n <= ncmds * sizeof(struct cm_t))
5002                         break;
5003
5004                 if (--count == 0) {
5005                         ch->ch_bd->state = BOARD_FAILED;
5006                         return;
5007                 }
5008                 udelay(10);
5009         }
5010 }
5011
5012 /*=======================================================================
5013  *
5014  *      dgap_cmdw - Sends a 1 word command to the FEP.
5015  *
5016  *              ch      - Pointer to channel structure.
5017  *              cmd     - Command to be sent.
5018  *              word    - Integer containing word to be sent.
5019  *              ncmds   - Wait until ncmds or fewer cmds are left
5020  *                        in the cmd buffer before returning.
5021  *
5022  *=======================================================================*/
5023 static void dgap_cmdw(struct channel_t *ch, uchar cmd, u16 word, uint ncmds)
5024 {
5025         char            *vaddr = NULL;
5026         struct cm_t     *cm_addr = NULL;
5027         uint            count;
5028         uint            n;
5029         u16             head;
5030         u16             tail;
5031
5032         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
5033                 return;
5034
5035         /*
5036          * Check if board is still alive.
5037          */
5038         if (ch->ch_bd->state == BOARD_FAILED)
5039                 return;
5040
5041         /*
5042          * Make sure the pointers are in range before
5043          * writing to the FEP memory.
5044          */
5045         vaddr = ch->ch_bd->re_map_membase;
5046         if (!vaddr)
5047                 return;
5048
5049         cm_addr = (struct cm_t *) (vaddr + CMDBUF);
5050         head = readw(&(cm_addr->cm_head));
5051
5052         /*
5053          * Forget it if pointers out of range.
5054          */
5055         if (head >= (CMDMAX - CMDSTART) || (head & 03)) {
5056                 ch->ch_bd->state = BOARD_FAILED;
5057                 return;
5058         }
5059
5060         /*
5061          * Put the data in the circular command buffer.
5062          */
5063         writeb(cmd, (char *) (vaddr + head + CMDSTART + 0));
5064         writeb((uchar) ch->ch_portnum, (char *) (vaddr + head + CMDSTART + 1));
5065         writew((u16) word, (char *) (vaddr + head + CMDSTART + 2));
5066
5067         head = (head + 4) & (CMDMAX - CMDSTART - 4);
5068
5069         writew(head, &(cm_addr->cm_head));
5070
5071         /*
5072          * Wait if necessary before updating the head
5073          * pointer to limit the number of outstanding
5074          * commands to the FEP.   If the time spent waiting
5075          * is outlandish, declare the FEP dead.
5076          */
5077         for (count = dgap_count ;;) {
5078
5079                 head = readw(&(cm_addr->cm_head));
5080                 tail = readw(&(cm_addr->cm_tail));
5081
5082                 n = (head - tail) & (CMDMAX - CMDSTART - 4);
5083
5084                 if (n <= ncmds * sizeof(struct cm_t))
5085                         break;
5086
5087                 if (--count == 0) {
5088                         ch->ch_bd->state = BOARD_FAILED;
5089                         return;
5090                 }
5091                 udelay(10);
5092         }
5093 }
5094
5095 /*=======================================================================
5096  *
5097  *      dgap_cmdw_ext - Sends a extended word command to the FEP.
5098  *
5099  *              ch      - Pointer to channel structure.
5100  *              cmd     - Command to be sent.
5101  *              word    - Integer containing word to be sent.
5102  *              ncmds   - Wait until ncmds or fewer cmds are left
5103  *                        in the cmd buffer before returning.
5104  *
5105  *=======================================================================*/
5106 static void dgap_cmdw_ext(struct channel_t *ch, u16 cmd, u16 word, uint ncmds)
5107 {
5108         char            *vaddr = NULL;
5109         struct cm_t     *cm_addr = NULL;
5110         uint            count;
5111         uint            n;
5112         u16             head;
5113         u16             tail;
5114
5115         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
5116                 return;
5117
5118         /*
5119          * Check if board is still alive.
5120          */
5121         if (ch->ch_bd->state == BOARD_FAILED)
5122                 return;
5123
5124         /*
5125          * Make sure the pointers are in range before
5126          * writing to the FEP memory.
5127          */
5128         vaddr = ch->ch_bd->re_map_membase;
5129         if (!vaddr)
5130                 return;
5131
5132         cm_addr = (struct cm_t *) (vaddr + CMDBUF);
5133         head = readw(&(cm_addr->cm_head));
5134
5135         /*
5136          * Forget it if pointers out of range.
5137          */
5138         if (head >= (CMDMAX - CMDSTART) || (head & 03)) {
5139                 ch->ch_bd->state = BOARD_FAILED;
5140                 return;
5141         }
5142
5143         /*
5144          * Put the data in the circular command buffer.
5145          */
5146
5147         /* Write an FF to tell the FEP that we want an extended command */
5148         writeb((uchar) 0xff, (char *) (vaddr + head + CMDSTART + 0));
5149
5150         writeb((uchar) ch->ch_portnum, (uchar *) (vaddr + head + CMDSTART + 1));
5151         writew((u16) cmd, (char *) (vaddr + head + CMDSTART + 2));
5152
5153         /*
5154          * If the second part of the command won't fit,
5155          * put it at the beginning of the circular buffer.
5156          */
5157         if (((head + 4) >= ((CMDMAX - CMDSTART)) || (head & 03)))
5158                 writew((u16) word, (char *) (vaddr + CMDSTART));
5159         else
5160                 writew((u16) word, (char *) (vaddr + head + CMDSTART + 4));
5161
5162         head = (head + 8) & (CMDMAX - CMDSTART - 4);
5163
5164         writew(head, &(cm_addr->cm_head));
5165
5166         /*
5167          * Wait if necessary before updating the head
5168          * pointer to limit the number of outstanding
5169          * commands to the FEP.   If the time spent waiting
5170          * is outlandish, declare the FEP dead.
5171          */
5172         for (count = dgap_count ;;) {
5173
5174                 head = readw(&(cm_addr->cm_head));
5175                 tail = readw(&(cm_addr->cm_tail));
5176
5177                 n = (head - tail) & (CMDMAX - CMDSTART - 4);
5178
5179                 if (n <= ncmds * sizeof(struct cm_t))
5180                         break;
5181
5182                 if (--count == 0) {
5183                         ch->ch_bd->state = BOARD_FAILED;
5184                         return;
5185                 }
5186                 udelay(10);
5187         }
5188 }
5189
5190 /*=======================================================================
5191  *
5192  *      dgap_wmove - Write data to FEP buffer.
5193  *
5194  *              ch      - Pointer to channel structure.
5195  *              buf     - Poiter to characters to be moved.
5196  *              cnt     - Number of characters to move.
5197  *
5198  *=======================================================================*/
5199 static void dgap_wmove(struct channel_t *ch, char *buf, uint cnt)
5200 {
5201         int    n;
5202         char   *taddr;
5203         struct bs_t    *bs;
5204         u16    head;
5205
5206         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
5207                 return;
5208
5209         /*
5210          * Check parameters.
5211          */
5212         bs   = ch->ch_bs;
5213         head = readw(&(bs->tx_head));
5214
5215         /*
5216          * If pointers are out of range, just return.
5217          */
5218         if ((cnt > ch->ch_tsize) ||
5219             (unsigned)(head - ch->ch_tstart) >= ch->ch_tsize)
5220                 return;
5221
5222         /*
5223          * If the write wraps over the top of the circular buffer,
5224          * move the portion up to the wrap point, and reset the
5225          * pointers to the bottom.
5226          */
5227         n = ch->ch_tstart + ch->ch_tsize - head;
5228
5229         if (cnt >= n) {
5230                 cnt -= n;
5231                 taddr = ch->ch_taddr + head;
5232                 memcpy_toio(taddr, buf, n);
5233                 head = ch->ch_tstart;
5234                 buf += n;
5235         }
5236
5237         /*
5238          * Move rest of data.
5239          */
5240         taddr = ch->ch_taddr + head;
5241         n = cnt;
5242         memcpy_toio(taddr, buf, n);
5243         head += cnt;
5244
5245         writew(head, &(bs->tx_head));
5246 }
5247
5248 /*
5249  * Retrives the current custom baud rate from FEP memory,
5250  * and returns it back to the user.
5251  * Returns 0 on error.
5252  */
5253 static uint dgap_get_custom_baud(struct channel_t *ch)
5254 {
5255         uchar *vaddr;
5256         ulong offset = 0;
5257         uint value = 0;
5258
5259         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
5260                 return 0;
5261
5262         if (!ch->ch_bd || ch->ch_bd->magic != DGAP_BOARD_MAGIC)
5263                 return 0;
5264
5265         if (!(ch->ch_bd->bd_flags & BD_FEP5PLUS))
5266                 return 0;
5267
5268         vaddr = ch->ch_bd->re_map_membase;
5269
5270         if (!vaddr)
5271                 return 0;
5272
5273         /*
5274          * Go get from fep mem, what the fep
5275          * believes the custom baud rate is.
5276          */
5277         offset = ((((*(unsigned short *)(vaddr + ECS_SEG)) << 4) +
5278                 (ch->ch_portnum * 0x28) + LINE_SPEED));
5279
5280         value = readw(vaddr + offset);
5281         return value;
5282 }
5283
5284 /*
5285  * Calls the firmware to reset this channel.
5286  */
5287 static void dgap_firmware_reset_port(struct channel_t *ch)
5288 {
5289         dgap_cmdb(ch, CHRESET, 0, 0, 0);
5290
5291         /*
5292          * Now that the channel is reset, we need to make sure
5293          * all the current settings get reapplied to the port
5294          * in the firmware.
5295          *
5296          * So we will set the driver's cache of firmware
5297          * settings all to 0, and then call param.
5298          */
5299         ch->ch_fepiflag = 0;
5300         ch->ch_fepcflag = 0;
5301         ch->ch_fepoflag = 0;
5302         ch->ch_fepstartc = 0;
5303         ch->ch_fepstopc = 0;
5304         ch->ch_fepastartc = 0;
5305         ch->ch_fepastopc = 0;
5306         ch->ch_mostat = 0;
5307         ch->ch_hflow = 0;
5308 }
5309
5310 /*=======================================================================
5311  *
5312  *      dgap_param - Set Digi parameters.
5313  *
5314  *              struct tty_struct *     - TTY for port.
5315  *
5316  *=======================================================================*/
5317 static int dgap_param(struct tty_struct *tty)
5318 {
5319         struct ktermios *ts;
5320         struct board_t *bd;
5321         struct channel_t *ch;
5322         struct bs_t   *bs;
5323         struct un_t   *un;
5324         u16     head;
5325         u16     cflag;
5326         u16     iflag;
5327         uchar   mval;
5328         uchar   hflow;
5329
5330         if (!tty || tty->magic != TTY_MAGIC)
5331                 return -ENXIO;
5332
5333         un = (struct un_t *) tty->driver_data;
5334         if (!un || un->magic != DGAP_UNIT_MAGIC)
5335                 return -ENXIO;
5336
5337         ch = un->un_ch;
5338         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
5339                 return -ENXIO;
5340
5341         bd = ch->ch_bd;
5342         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
5343                 return -ENXIO;
5344
5345         bs = ch->ch_bs;
5346         if (!bs)
5347                 return -ENXIO;
5348
5349         ts = &tty->termios;
5350
5351         /*
5352          * If baud rate is zero, flush queues, and set mval to drop DTR.
5353          */
5354         if ((ch->ch_c_cflag & (CBAUD)) == 0) {
5355
5356                 /* flush rx */
5357                 head = readw(&(ch->ch_bs->rx_head));
5358                 writew(head, &(ch->ch_bs->rx_tail));
5359
5360                 /* flush tx */
5361                 head = readw(&(ch->ch_bs->tx_head));
5362                 writew(head, &(ch->ch_bs->tx_tail));
5363
5364                 ch->ch_flags |= (CH_BAUD0);
5365
5366                 /* Drop RTS and DTR */
5367                 ch->ch_mval &= ~(D_RTS(ch)|D_DTR(ch));
5368                 mval = D_DTR(ch) | D_RTS(ch);
5369                 ch->ch_baud_info = 0;
5370
5371         } else if (ch->ch_custom_speed && (bd->bd_flags & BD_FEP5PLUS)) {
5372                 /*
5373                  * Tell the fep to do the command
5374                  */
5375
5376                 dgap_cmdw_ext(ch, 0xff01, ch->ch_custom_speed, 0);
5377
5378                 /*
5379                  * Now go get from fep mem, what the fep
5380                  * believes the custom baud rate is.
5381                  */
5382                 ch->ch_custom_speed = dgap_get_custom_baud(ch);
5383                 ch->ch_baud_info = ch->ch_custom_speed;
5384
5385                 /* Handle transition from B0 */
5386                 if (ch->ch_flags & CH_BAUD0) {
5387                         ch->ch_flags &= ~(CH_BAUD0);
5388                         ch->ch_mval |= (D_RTS(ch)|D_DTR(ch));
5389                 }
5390                 mval = D_DTR(ch) | D_RTS(ch);
5391
5392         } else {
5393                 /*
5394                  * Set baud rate, character size, and parity.
5395                  */
5396
5397
5398                 int iindex = 0;
5399                 int jindex = 0;
5400                 int baud = 0;
5401
5402                 ulong bauds[4][16] = {
5403                         { /* slowbaud */
5404                                 0,      50,     75,     110,
5405                                 134,    150,    200,    300,
5406                                 600,    1200,   1800,   2400,
5407                                 4800,   9600,   19200,  38400 },
5408                         { /* slowbaud & CBAUDEX */
5409                                 0,      57600,  115200, 230400,
5410                                 460800, 150,    200,    921600,
5411                                 600,    1200,   1800,   2400,
5412                                 4800,   9600,   19200,  38400 },
5413                         { /* fastbaud */
5414                                 0,      57600,  76800,  115200,
5415                                 14400,  57600,  230400, 76800,
5416                                 115200, 230400, 28800,  460800,
5417                                 921600, 9600,   19200,  38400 },
5418                         { /* fastbaud & CBAUDEX */
5419                                 0,      57600,  115200, 230400,
5420                                 460800, 150,    200,    921600,
5421                                 600,    1200,   1800,   2400,
5422                                 4800,   9600,   19200,  38400 }
5423                 };
5424
5425                 /*
5426                  * Only use the TXPrint baud rate if the
5427                  * terminal unit is NOT open
5428                  */
5429                 if (!(ch->ch_tun.un_flags & UN_ISOPEN) &&
5430                      (un->un_type == DGAP_PRINT))
5431                         baud = C_BAUD(ch->ch_pun.un_tty) & 0xff;
5432                 else
5433                         baud = C_BAUD(ch->ch_tun.un_tty) & 0xff;
5434
5435                 if (ch->ch_c_cflag & CBAUDEX)
5436                         iindex = 1;
5437
5438                 if (ch->ch_digi.digi_flags & DIGI_FAST)
5439                         iindex += 2;
5440
5441                 jindex = baud;
5442
5443                 if ((iindex >= 0) && (iindex < 4) &&
5444                     (jindex >= 0) && (jindex < 16))
5445                         baud = bauds[iindex][jindex];
5446                 else
5447                         baud = 0;
5448
5449                 if (baud == 0)
5450                         baud = 9600;
5451
5452                 ch->ch_baud_info = baud;
5453
5454                 /*
5455                  * CBAUD has bit position 0x1000 set these days to
5456                  * indicate Linux baud rate remap.
5457                  * We use a different bit assignment for high speed.
5458                  * Clear this bit out while grabbing the parts of
5459                  * "cflag" we want.
5460                  */
5461                 cflag = ch->ch_c_cflag & ((CBAUD ^ CBAUDEX) | PARODD | PARENB |
5462                                                    CSTOPB | CSIZE);
5463
5464                 /*
5465                  * HUPCL bit is used by FEP to indicate fast baud
5466                  * table is to be used.
5467                  */
5468                 if ((ch->ch_digi.digi_flags & DIGI_FAST) ||
5469                     (ch->ch_c_cflag & CBAUDEX))
5470                         cflag |= HUPCL;
5471
5472                 if ((ch->ch_c_cflag & CBAUDEX) &&
5473                     !(ch->ch_digi.digi_flags & DIGI_FAST)) {
5474                         /*
5475                          * The below code is trying to guarantee that only
5476                          * baud rates 115200, 230400, 460800, 921600 are
5477                          * remapped. We use exclusive or  because the various
5478                          * baud rates share common bit positions and therefore
5479                          * can't be tested for easily.
5480                          */
5481                         tcflag_t tcflag = (ch->ch_c_cflag & CBAUD) | CBAUDEX;
5482                         int baudpart = 0;
5483
5484                         /*
5485                          * Map high speed requests to index
5486                          * into FEP's baud table
5487                          */
5488                         switch (tcflag) {
5489                         case B57600:
5490                                 baudpart = 1;
5491                                 break;
5492 #ifdef B76800
5493                         case B76800:
5494                                 baudpart = 2;
5495                                 break;
5496 #endif
5497                         case B115200:
5498                                 baudpart = 3;
5499                                 break;
5500                         case B230400:
5501                                 baudpart = 9;
5502                                 break;
5503                         case B460800:
5504                                 baudpart = 11;
5505                                 break;
5506 #ifdef B921600
5507                         case B921600:
5508                                 baudpart = 12;
5509                                 break;
5510 #endif
5511                         default:
5512                                 baudpart = 0;
5513                         }
5514
5515                         if (baudpart)
5516                                 cflag = (cflag & ~(CBAUD | CBAUDEX)) | baudpart;
5517                 }
5518
5519                 cflag &= 0xffff;
5520
5521                 if (cflag != ch->ch_fepcflag) {
5522                         ch->ch_fepcflag = (u16) (cflag & 0xffff);
5523
5524                         /*
5525                          * Okay to have channel and board
5526                          * locks held calling this
5527                          */
5528                         dgap_cmdw(ch, SCFLAG, (u16) cflag, 0);
5529                 }
5530
5531                 /* Handle transition from B0 */
5532                 if (ch->ch_flags & CH_BAUD0) {
5533                         ch->ch_flags &= ~(CH_BAUD0);
5534                         ch->ch_mval |= (D_RTS(ch)|D_DTR(ch));
5535                 }
5536                 mval = D_DTR(ch) | D_RTS(ch);
5537         }
5538
5539         /*
5540          * Get input flags.
5541          */
5542         iflag = ch->ch_c_iflag & (IGNBRK | BRKINT | IGNPAR | PARMRK |
5543                                   INPCK | ISTRIP | IXON | IXANY | IXOFF);
5544
5545         if ((ch->ch_startc == _POSIX_VDISABLE) ||
5546             (ch->ch_stopc == _POSIX_VDISABLE)) {
5547                 iflag &= ~(IXON | IXOFF);
5548                 ch->ch_c_iflag &= ~(IXON | IXOFF);
5549         }
5550
5551         /*
5552          * Only the IBM Xr card can switch between
5553          * 232 and 422 modes on the fly
5554          */
5555         if (bd->device == PCI_DEV_XR_IBM_DID) {
5556                 if (ch->ch_digi.digi_flags & DIGI_422)
5557                         dgap_cmdb(ch, SCOMMODE, MODE_422, 0, 0);
5558                 else
5559                         dgap_cmdb(ch, SCOMMODE, MODE_232, 0, 0);
5560         }
5561
5562         if (ch->ch_digi.digi_flags & DIGI_ALTPIN)
5563                 iflag |= IALTPIN;
5564
5565         if (iflag != ch->ch_fepiflag) {
5566                 ch->ch_fepiflag = iflag;
5567
5568                 /* Okay to have channel and board locks held calling this */
5569                 dgap_cmdw(ch, SIFLAG, (u16) ch->ch_fepiflag, 0);
5570         }
5571
5572         /*
5573          * Select hardware handshaking.
5574          */
5575         hflow = 0;
5576
5577         if (ch->ch_c_cflag & CRTSCTS)
5578                 hflow |= (D_RTS(ch) | D_CTS(ch));
5579         if (ch->ch_digi.digi_flags & RTSPACE)
5580                 hflow |= D_RTS(ch);
5581         if (ch->ch_digi.digi_flags & DTRPACE)
5582                 hflow |= D_DTR(ch);
5583         if (ch->ch_digi.digi_flags & CTSPACE)
5584                 hflow |= D_CTS(ch);
5585         if (ch->ch_digi.digi_flags & DSRPACE)
5586                 hflow |= D_DSR(ch);
5587         if (ch->ch_digi.digi_flags & DCDPACE)
5588                 hflow |= D_CD(ch);
5589
5590         if (hflow != ch->ch_hflow) {
5591                 ch->ch_hflow = hflow;
5592
5593                 /* Okay to have channel and board locks held calling this */
5594                 dgap_cmdb(ch, SHFLOW, (uchar) hflow, 0xff, 0);
5595         }
5596
5597
5598         /*
5599          * Set RTS and/or DTR Toggle if needed,
5600          * but only if product is FEP5+ based.
5601          */
5602         if (bd->bd_flags & BD_FEP5PLUS) {
5603                 u16 hflow2 = 0;
5604                 if (ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE)
5605                         hflow2 |= (D_RTS(ch));
5606                 if (ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE)
5607                         hflow2 |= (D_DTR(ch));
5608
5609                 dgap_cmdw_ext(ch, 0xff03, hflow2, 0);
5610         }
5611
5612         /*
5613          * Set modem control lines.
5614          */
5615
5616         mval ^= ch->ch_mforce & (mval ^ ch->ch_mval);
5617
5618         if (ch->ch_mostat ^ mval) {
5619                 ch->ch_mostat = mval;
5620
5621                 /* Okay to have channel and board locks held calling this */
5622                 dgap_cmdb(ch, SMODEM, (uchar) mval, D_RTS(ch)|D_DTR(ch), 0);
5623         }
5624
5625         /*
5626          * Read modem signals, and then call carrier function.
5627          */
5628         ch->ch_mistat = readb(&(bs->m_stat));
5629         dgap_carrier(ch);
5630
5631         /*
5632          * Set the start and stop characters.
5633          */
5634         if (ch->ch_startc != ch->ch_fepstartc ||
5635             ch->ch_stopc != ch->ch_fepstopc) {
5636                 ch->ch_fepstartc = ch->ch_startc;
5637                 ch->ch_fepstopc =  ch->ch_stopc;
5638
5639                 /* Okay to have channel and board locks held calling this */
5640                 dgap_cmdb(ch, SFLOWC, ch->ch_fepstartc, ch->ch_fepstopc, 0);
5641         }
5642
5643         /*
5644          * Set the Auxiliary start and stop characters.
5645          */
5646         if (ch->ch_astartc != ch->ch_fepastartc ||
5647             ch->ch_astopc != ch->ch_fepastopc) {
5648                 ch->ch_fepastartc = ch->ch_astartc;
5649                 ch->ch_fepastopc = ch->ch_astopc;
5650
5651                 /* Okay to have channel and board locks held calling this */
5652                 dgap_cmdb(ch, SAFLOWC, ch->ch_fepastartc, ch->ch_fepastopc, 0);
5653         }
5654
5655         return 0;
5656 }
5657
5658 /*
5659  * dgap_parity_scan()
5660  *
5661  * Convert the FEP5 way of reporting parity errors and breaks into
5662  * the Linux line discipline way.
5663  */
5664 static void dgap_parity_scan(struct channel_t *ch, unsigned char *cbuf,
5665                                 unsigned char *fbuf, int *len)
5666 {
5667         int l = *len;
5668         int count = 0;
5669         unsigned char *in, *cout, *fout;
5670         unsigned char c;
5671
5672         in = cbuf;
5673         cout = cbuf;
5674         fout = fbuf;
5675
5676         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
5677                 return;
5678
5679         while (l--) {
5680                 c = *in++;
5681                 switch (ch->pscan_state) {
5682                 default:
5683                         /* reset to sanity and fall through */
5684                         ch->pscan_state = 0;
5685
5686                 case 0:
5687                         /* No FF seen yet */
5688                         if (c == (unsigned char) '\377')
5689                                 /* delete this character from stream */
5690                                 ch->pscan_state = 1;
5691                         else {
5692                                 *cout++ = c;
5693                                 *fout++ = TTY_NORMAL;
5694                                 count += 1;
5695                         }
5696                         break;
5697
5698                 case 1:
5699                         /* first FF seen */
5700                         if (c == (unsigned char) '\377') {
5701                                 /* doubled ff, transform to single ff */
5702                                 *cout++ = c;
5703                                 *fout++ = TTY_NORMAL;
5704                                 count += 1;
5705                                 ch->pscan_state = 0;
5706                         } else {
5707                                 /* save value examination in next state */
5708                                 ch->pscan_savechar = c;
5709                                 ch->pscan_state = 2;
5710                         }
5711                         break;
5712
5713                 case 2:
5714                         /* third character of ff sequence */
5715
5716                         *cout++ = c;
5717
5718                         if (ch->pscan_savechar == 0x0) {
5719
5720                                 if (c == 0x0) {
5721                                         ch->ch_err_break++;
5722                                         *fout++ = TTY_BREAK;
5723                                 } else {
5724                                         ch->ch_err_parity++;
5725                                         *fout++ = TTY_PARITY;
5726                                 }
5727                         }
5728
5729                         count += 1;
5730                         ch->pscan_state = 0;
5731                 }
5732         }
5733         *len = count;
5734 }
5735
5736 /*=======================================================================
5737  *
5738  *      dgap_event - FEP to host event processing routine.
5739  *
5740  *              bd     - Board of current event.
5741  *
5742  *=======================================================================*/
5743 static int dgap_event(struct board_t *bd)
5744 {
5745         struct channel_t *ch;
5746         ulong           lock_flags;
5747         ulong           lock_flags2;
5748         struct bs_t     *bs;
5749         uchar           *event;
5750         uchar           *vaddr = NULL;
5751         struct ev_t     *eaddr = NULL;
5752         uint            head;
5753         uint            tail;
5754         int             port;
5755         int             reason;
5756         int             modem;
5757         int             b1;
5758
5759         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
5760                 return -ENXIO;
5761
5762         DGAP_LOCK(bd->bd_lock, lock_flags);
5763
5764         vaddr = bd->re_map_membase;
5765
5766         if (!vaddr) {
5767                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
5768                 return -ENXIO;
5769         }
5770
5771         eaddr = (struct ev_t *) (vaddr + EVBUF);
5772
5773         /* Get our head and tail */
5774         head = readw(&(eaddr->ev_head));
5775         tail = readw(&(eaddr->ev_tail));
5776
5777         /*
5778          * Forget it if pointers out of range.
5779          */
5780
5781         if (head >= EVMAX - EVSTART || tail >= EVMAX - EVSTART ||
5782             (head | tail) & 03) {
5783                 /* Let go of board lock */
5784                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
5785                 return -ENXIO;
5786         }
5787
5788         /*
5789          * Loop to process all the events in the buffer.
5790          */
5791         while (tail != head) {
5792
5793                 /*
5794                  * Get interrupt information.
5795                  */
5796
5797                 event = bd->re_map_membase + tail + EVSTART;
5798
5799                 port   = event[0];
5800                 reason = event[1];
5801                 modem  = event[2];
5802                 b1     = event[3];
5803
5804                 /*
5805                  * Make sure the interrupt is valid.
5806                  */
5807                 if (port >= bd->nasync)
5808                         goto next;
5809
5810                 if (!(reason & (IFMODEM | IFBREAK | IFTLW | IFTEM | IFDATA)))
5811                         goto next;
5812
5813                 ch = bd->channels[port];
5814
5815                 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
5816                         goto next;
5817
5818                 /*
5819                  * If we have made it here, the event was valid.
5820                  * Lock down the channel.
5821                  */
5822                 DGAP_LOCK(ch->ch_lock, lock_flags2);
5823
5824                 bs = ch->ch_bs;
5825
5826                 if (!bs) {
5827                         DGAP_UNLOCK(ch->ch_lock, lock_flags2);
5828                         goto next;
5829                 }
5830
5831                 /*
5832                  * Process received data.
5833                  */
5834                 if (reason & IFDATA) {
5835
5836                         /*
5837                          * ALL LOCKS *MUST* BE DROPPED BEFORE CALLING INPUT!
5838                          * input could send some data to ld, which in turn
5839                          * could do a callback to one of our other functions.
5840                          */
5841                         DGAP_UNLOCK(ch->ch_lock, lock_flags2);
5842                         DGAP_UNLOCK(bd->bd_lock, lock_flags);
5843
5844                         dgap_input(ch);
5845
5846                         DGAP_LOCK(bd->bd_lock, lock_flags);
5847                         DGAP_LOCK(ch->ch_lock, lock_flags2);
5848
5849                         if (ch->ch_flags & CH_RACTIVE)
5850                                 ch->ch_flags |= CH_RENABLE;
5851                         else
5852                                 writeb(1, &(bs->idata));
5853
5854                         if (ch->ch_flags & CH_RWAIT) {
5855                                 ch->ch_flags &= ~CH_RWAIT;
5856
5857                                 wake_up_interruptible
5858                                         (&ch->ch_tun.un_flags_wait);
5859                         }
5860                 }
5861
5862                 /*
5863                  * Process Modem change signals.
5864                  */
5865                 if (reason & IFMODEM) {
5866                         ch->ch_mistat = modem;
5867                         dgap_carrier(ch);
5868                 }
5869
5870                 /*
5871                  * Process break.
5872                  */
5873                 if (reason & IFBREAK) {
5874
5875                         if (ch->ch_tun.un_tty) {
5876                                 /* A break has been indicated */
5877                                 ch->ch_err_break++;
5878                                 tty_buffer_request_room
5879                                         (ch->ch_tun.un_tty->port, 1);
5880                                 tty_insert_flip_char(ch->ch_tun.un_tty->port,
5881                                                      0, TTY_BREAK);
5882                                 tty_flip_buffer_push(ch->ch_tun.un_tty->port);
5883                         }
5884                 }
5885
5886                 /*
5887                  * Process Transmit low.
5888                  */
5889                 if (reason & IFTLW) {
5890
5891                         if (ch->ch_tun.un_flags & UN_LOW) {
5892                                 ch->ch_tun.un_flags &= ~UN_LOW;
5893
5894                                 if (ch->ch_tun.un_flags & UN_ISOPEN) {
5895                                         if ((ch->ch_tun.un_tty->flags &
5896                                            (1 << TTY_DO_WRITE_WAKEUP)) &&
5897                                                 ch->ch_tun.un_tty->ldisc->ops->write_wakeup) {
5898                                                 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
5899                                                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
5900                                                 (ch->ch_tun.un_tty->ldisc->ops->write_wakeup)(ch->ch_tun.un_tty);
5901                                                 DGAP_LOCK(bd->bd_lock, lock_flags);
5902                                                 DGAP_LOCK(ch->ch_lock, lock_flags2);
5903                                         }
5904                                         wake_up_interruptible(&ch->ch_tun.un_tty->write_wait);
5905                                         wake_up_interruptible(&ch->ch_tun.un_flags_wait);
5906                                 }
5907                         }
5908
5909                         if (ch->ch_pun.un_flags & UN_LOW) {
5910                                 ch->ch_pun.un_flags &= ~UN_LOW;
5911                                 if (ch->ch_pun.un_flags & UN_ISOPEN) {
5912                                         if ((ch->ch_pun.un_tty->flags &
5913                                            (1 << TTY_DO_WRITE_WAKEUP)) &&
5914                                                 ch->ch_pun.un_tty->ldisc->ops->write_wakeup) {
5915                                                 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
5916                                                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
5917                                                 (ch->ch_pun.un_tty->ldisc->ops->write_wakeup)(ch->ch_pun.un_tty);
5918                                                 DGAP_LOCK(bd->bd_lock, lock_flags);
5919                                                 DGAP_LOCK(ch->ch_lock, lock_flags2);
5920                                         }
5921                                         wake_up_interruptible(&ch->ch_pun.un_tty->write_wait);
5922                                         wake_up_interruptible(&ch->ch_pun.un_flags_wait);
5923                                 }
5924                         }
5925
5926                         if (ch->ch_flags & CH_WLOW) {
5927                                 ch->ch_flags &= ~CH_WLOW;
5928                                 wake_up_interruptible(&ch->ch_flags_wait);
5929                         }
5930                 }
5931
5932                 /*
5933                  * Process Transmit empty.
5934                  */
5935                 if (reason & IFTEM) {
5936                         if (ch->ch_tun.un_flags & UN_EMPTY) {
5937                                 ch->ch_tun.un_flags &= ~UN_EMPTY;
5938                                 if (ch->ch_tun.un_flags & UN_ISOPEN) {
5939                                         if ((ch->ch_tun.un_tty->flags &
5940                                            (1 << TTY_DO_WRITE_WAKEUP)) &&
5941                                                 ch->ch_tun.un_tty->ldisc->ops->write_wakeup) {
5942                                                 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
5943                                                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
5944
5945                                                 (ch->ch_tun.un_tty->ldisc->ops->write_wakeup)(ch->ch_tun.un_tty);
5946                                                 DGAP_LOCK(bd->bd_lock, lock_flags);
5947                                                 DGAP_LOCK(ch->ch_lock, lock_flags2);
5948                                         }
5949                                         wake_up_interruptible(&ch->ch_tun.un_tty->write_wait);
5950                                         wake_up_interruptible(&ch->ch_tun.un_flags_wait);
5951                                 }
5952                         }
5953
5954                         if (ch->ch_pun.un_flags & UN_EMPTY) {
5955                                 ch->ch_pun.un_flags &= ~UN_EMPTY;
5956                                 if (ch->ch_pun.un_flags & UN_ISOPEN) {
5957                                         if ((ch->ch_pun.un_tty->flags &
5958                                            (1 << TTY_DO_WRITE_WAKEUP)) &&
5959                                                 ch->ch_pun.un_tty->ldisc->ops->write_wakeup) {
5960                                                 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
5961                                                 DGAP_UNLOCK(bd->bd_lock, lock_flags);
5962                                                 (ch->ch_pun.un_tty->ldisc->ops->write_wakeup)(ch->ch_pun.un_tty);
5963                                                 DGAP_LOCK(bd->bd_lock, lock_flags);
5964                                                 DGAP_LOCK(ch->ch_lock, lock_flags2);
5965                                         }
5966                                         wake_up_interruptible(&ch->ch_pun.un_tty->write_wait);
5967                                         wake_up_interruptible(&ch->ch_pun.un_flags_wait);
5968                                 }
5969                         }
5970
5971
5972                         if (ch->ch_flags & CH_WEMPTY) {
5973                                 ch->ch_flags &= ~CH_WEMPTY;
5974                                 wake_up_interruptible(&ch->ch_flags_wait);
5975                         }
5976                 }
5977
5978                 DGAP_UNLOCK(ch->ch_lock, lock_flags2);
5979
5980 next:
5981                 tail = (tail + 4) & (EVMAX - EVSTART - 4);
5982         }
5983
5984         writew(tail, &(eaddr->ev_tail));
5985         DGAP_UNLOCK(bd->bd_lock, lock_flags);
5986
5987         return 0;
5988 }
5989
5990 static ssize_t dgap_driver_version_show(struct device_driver *ddp, char *buf)
5991 {
5992         return snprintf(buf, PAGE_SIZE, "%s\n", DG_PART);
5993 }
5994 static DRIVER_ATTR(version, S_IRUSR, dgap_driver_version_show, NULL);
5995
5996
5997 static ssize_t dgap_driver_boards_show(struct device_driver *ddp, char *buf)
5998 {
5999         return snprintf(buf, PAGE_SIZE, "%d\n", dgap_NumBoards);
6000 }
6001 static DRIVER_ATTR(boards, S_IRUSR, dgap_driver_boards_show, NULL);
6002
6003
6004 static ssize_t dgap_driver_maxboards_show(struct device_driver *ddp, char *buf)
6005 {
6006         return snprintf(buf, PAGE_SIZE, "%d\n", MAXBOARDS);
6007 }
6008 static DRIVER_ATTR(maxboards, S_IRUSR, dgap_driver_maxboards_show, NULL);
6009
6010
6011 static ssize_t dgap_driver_pollcounter_show(struct device_driver *ddp, char *buf)
6012 {
6013         return snprintf(buf, PAGE_SIZE, "%ld\n", dgap_poll_counter);
6014 }
6015 static DRIVER_ATTR(pollcounter, S_IRUSR, dgap_driver_pollcounter_show, NULL);
6016
6017
6018 static ssize_t dgap_driver_state_show(struct device_driver *ddp, char *buf)
6019 {
6020         return snprintf(buf, PAGE_SIZE, "%s\n", dgap_driver_state_text[dgap_driver_state]);
6021 }
6022 static DRIVER_ATTR(state, S_IRUSR, dgap_driver_state_show, NULL);
6023
6024 static ssize_t dgap_driver_pollrate_show(struct device_driver *ddp, char *buf)
6025 {
6026         return snprintf(buf, PAGE_SIZE, "%dms\n", dgap_poll_tick);
6027 }
6028
6029 static ssize_t dgap_driver_pollrate_store(struct device_driver *ddp, const char *buf, size_t count)
6030 {
6031         if (sscanf(buf, "%d\n", &dgap_poll_tick) != 1)
6032                 return -EINVAL;
6033         return count;
6034 }
6035 static DRIVER_ATTR(pollrate, (S_IRUSR | S_IWUSR), dgap_driver_pollrate_show, dgap_driver_pollrate_store);
6036
6037 static int dgap_create_driver_sysfiles(struct pci_driver *dgap_driver)
6038 {
6039         int rc = 0;
6040         struct device_driver *driverfs = &dgap_driver->driver;
6041
6042         rc |= driver_create_file(driverfs, &driver_attr_version);
6043         rc |= driver_create_file(driverfs, &driver_attr_boards);
6044         rc |= driver_create_file(driverfs, &driver_attr_maxboards);
6045         rc |= driver_create_file(driverfs, &driver_attr_pollrate);
6046         rc |= driver_create_file(driverfs, &driver_attr_pollcounter);
6047         rc |= driver_create_file(driverfs, &driver_attr_state);
6048
6049         return rc;
6050 }
6051
6052 static void dgap_remove_driver_sysfiles(struct pci_driver *dgap_driver)
6053 {
6054         struct device_driver *driverfs = &dgap_driver->driver;
6055         driver_remove_file(driverfs, &driver_attr_version);
6056         driver_remove_file(driverfs, &driver_attr_boards);
6057         driver_remove_file(driverfs, &driver_attr_maxboards);
6058         driver_remove_file(driverfs, &driver_attr_pollrate);
6059         driver_remove_file(driverfs, &driver_attr_pollcounter);
6060         driver_remove_file(driverfs, &driver_attr_state);
6061 }
6062
6063 static struct board_t *dgap_verify_board(struct device *p)
6064 {
6065         struct board_t *bd;
6066
6067         if (!p)
6068                 return NULL;
6069
6070         bd = dev_get_drvdata(p);
6071         if (!bd || bd->magic != DGAP_BOARD_MAGIC || bd->state != BOARD_READY)
6072                 return NULL;
6073
6074         return bd;
6075 }
6076
6077 static ssize_t dgap_ports_state_show(struct device *p, struct device_attribute *attr, char *buf)
6078 {
6079         struct board_t *bd;
6080         int count = 0;
6081         int i = 0;
6082
6083         bd = dgap_verify_board(p);
6084         if (!bd)
6085                 return 0;
6086
6087         for (i = 0; i < bd->nasync; i++) {
6088                 count += snprintf(buf + count, PAGE_SIZE - count,
6089                         "%d %s\n", bd->channels[i]->ch_portnum,
6090                         bd->channels[i]->ch_open_count ? "Open" : "Closed");
6091         }
6092         return count;
6093 }
6094 static DEVICE_ATTR(ports_state, S_IRUSR, dgap_ports_state_show, NULL);
6095
6096 static ssize_t dgap_ports_baud_show(struct device *p, struct device_attribute *attr, char *buf)
6097 {
6098         struct board_t *bd;
6099         int count = 0;
6100         int i = 0;
6101
6102         bd = dgap_verify_board(p);
6103         if (!bd)
6104                 return 0;
6105
6106         for (i = 0; i < bd->nasync; i++) {
6107                 count +=  snprintf(buf + count, PAGE_SIZE - count,
6108                         "%d %d\n", bd->channels[i]->ch_portnum, bd->channels[i]->ch_baud_info);
6109         }
6110         return count;
6111 }
6112 static DEVICE_ATTR(ports_baud, S_IRUSR, dgap_ports_baud_show, NULL);
6113
6114 static ssize_t dgap_ports_msignals_show(struct device *p, struct device_attribute *attr, char *buf)
6115 {
6116         struct board_t *bd;
6117         int count = 0;
6118         int i = 0;
6119
6120         bd = dgap_verify_board(p);
6121         if (!bd)
6122                 return 0;
6123
6124         for (i = 0; i < bd->nasync; i++) {
6125                 if (bd->channels[i]->ch_open_count)
6126                         count += snprintf(buf + count, PAGE_SIZE - count,
6127                                 "%d %s %s %s %s %s %s\n", bd->channels[i]->ch_portnum,
6128                                 (bd->channels[i]->ch_mostat & UART_MCR_RTS) ? "RTS" : "",
6129                                 (bd->channels[i]->ch_mistat & UART_MSR_CTS) ? "CTS" : "",
6130                                 (bd->channels[i]->ch_mostat & UART_MCR_DTR) ? "DTR" : "",
6131                                 (bd->channels[i]->ch_mistat & UART_MSR_DSR) ? "DSR" : "",
6132                                 (bd->channels[i]->ch_mistat & UART_MSR_DCD) ? "DCD" : "",
6133                                 (bd->channels[i]->ch_mistat & UART_MSR_RI)  ? "RI"  : "");
6134                 else
6135                         count += snprintf(buf + count, PAGE_SIZE - count,
6136                                 "%d\n", bd->channels[i]->ch_portnum);
6137         }
6138         return count;
6139 }
6140 static DEVICE_ATTR(ports_msignals, S_IRUSR, dgap_ports_msignals_show, NULL);
6141
6142 static ssize_t dgap_ports_iflag_show(struct device *p, struct device_attribute *attr, char *buf)
6143 {
6144         struct board_t *bd;
6145         int count = 0;
6146         int i = 0;
6147
6148         bd = dgap_verify_board(p);
6149         if (!bd)
6150                 return 0;
6151
6152         for (i = 0; i < bd->nasync; i++)
6153                 count += snprintf(buf + count, PAGE_SIZE - count, "%d %x\n",
6154                         bd->channels[i]->ch_portnum, bd->channels[i]->ch_c_iflag);
6155         return count;
6156 }
6157 static DEVICE_ATTR(ports_iflag, S_IRUSR, dgap_ports_iflag_show, NULL);
6158
6159 static ssize_t dgap_ports_cflag_show(struct device *p, struct device_attribute *attr, char *buf)
6160 {
6161         struct board_t *bd;
6162         int count = 0;
6163         int i = 0;
6164
6165         bd = dgap_verify_board(p);
6166         if (!bd)
6167                 return 0;
6168
6169         for (i = 0; i < bd->nasync; i++)
6170                 count += snprintf(buf + count, PAGE_SIZE - count, "%d %x\n",
6171                         bd->channels[i]->ch_portnum, bd->channels[i]->ch_c_cflag);
6172         return count;
6173 }
6174 static DEVICE_ATTR(ports_cflag, S_IRUSR, dgap_ports_cflag_show, NULL);
6175
6176 static ssize_t dgap_ports_oflag_show(struct device *p, struct device_attribute *attr, char *buf)
6177 {
6178         struct board_t *bd;
6179         int count = 0;
6180         int i = 0;
6181
6182         bd = dgap_verify_board(p);
6183         if (!bd)
6184                 return 0;
6185
6186         for (i = 0; i < bd->nasync; i++)
6187                 count += snprintf(buf + count, PAGE_SIZE - count, "%d %x\n",
6188                         bd->channels[i]->ch_portnum, bd->channels[i]->ch_c_oflag);
6189         return count;
6190 }
6191 static DEVICE_ATTR(ports_oflag, S_IRUSR, dgap_ports_oflag_show, NULL);
6192
6193 static ssize_t dgap_ports_lflag_show(struct device *p, struct device_attribute *attr, char *buf)
6194 {
6195         struct board_t *bd;
6196         int count = 0;
6197         int i = 0;
6198
6199         bd = dgap_verify_board(p);
6200         if (!bd)
6201                 return 0;
6202
6203         for (i = 0; i < bd->nasync; i++)
6204                 count += snprintf(buf + count, PAGE_SIZE - count, "%d %x\n",
6205                         bd->channels[i]->ch_portnum, bd->channels[i]->ch_c_lflag);
6206         return count;
6207 }
6208 static DEVICE_ATTR(ports_lflag, S_IRUSR, dgap_ports_lflag_show, NULL);
6209
6210 static ssize_t dgap_ports_digi_flag_show(struct device *p, struct device_attribute *attr, char *buf)
6211 {
6212         struct board_t *bd;
6213         int count = 0;
6214         int i = 0;
6215
6216         bd = dgap_verify_board(p);
6217         if (!bd)
6218                 return 0;
6219
6220         for (i = 0; i < bd->nasync; i++)
6221                 count += snprintf(buf + count, PAGE_SIZE - count, "%d %x\n",
6222                         bd->channels[i]->ch_portnum, bd->channels[i]->ch_digi.digi_flags);
6223         return count;
6224 }
6225 static DEVICE_ATTR(ports_digi_flag, S_IRUSR, dgap_ports_digi_flag_show, NULL);
6226
6227 static ssize_t dgap_ports_rxcount_show(struct device *p, struct device_attribute *attr, char *buf)
6228 {
6229         struct board_t *bd;
6230         int count = 0;
6231         int i = 0;
6232
6233         bd = dgap_verify_board(p);
6234         if (!bd)
6235                 return 0;
6236
6237         for (i = 0; i < bd->nasync; i++)
6238                 count += snprintf(buf + count, PAGE_SIZE - count, "%d %ld\n",
6239                         bd->channels[i]->ch_portnum, bd->channels[i]->ch_rxcount);
6240         return count;
6241 }
6242 static DEVICE_ATTR(ports_rxcount, S_IRUSR, dgap_ports_rxcount_show, NULL);
6243
6244 static ssize_t dgap_ports_txcount_show(struct device *p, struct device_attribute *attr, char *buf)
6245 {
6246         struct board_t *bd;
6247         int count = 0;
6248         int i = 0;
6249
6250         bd = dgap_verify_board(p);
6251         if (!bd)
6252                 return 0;
6253
6254         for (i = 0; i < bd->nasync; i++)
6255                 count += snprintf(buf + count, PAGE_SIZE - count, "%d %ld\n",
6256                         bd->channels[i]->ch_portnum, bd->channels[i]->ch_txcount);
6257         return count;
6258 }
6259 static DEVICE_ATTR(ports_txcount, S_IRUSR, dgap_ports_txcount_show, NULL);
6260
6261 /* this function creates the sys files that will export each signal status
6262  * to sysfs each value will be put in a separate filename
6263  */
6264 static void dgap_create_ports_sysfiles(struct board_t *bd)
6265 {
6266         dev_set_drvdata(&bd->pdev->dev, bd);
6267         device_create_file(&(bd->pdev->dev), &dev_attr_ports_state);
6268         device_create_file(&(bd->pdev->dev), &dev_attr_ports_baud);
6269         device_create_file(&(bd->pdev->dev), &dev_attr_ports_msignals);
6270         device_create_file(&(bd->pdev->dev), &dev_attr_ports_iflag);
6271         device_create_file(&(bd->pdev->dev), &dev_attr_ports_cflag);
6272         device_create_file(&(bd->pdev->dev), &dev_attr_ports_oflag);
6273         device_create_file(&(bd->pdev->dev), &dev_attr_ports_lflag);
6274         device_create_file(&(bd->pdev->dev), &dev_attr_ports_digi_flag);
6275         device_create_file(&(bd->pdev->dev), &dev_attr_ports_rxcount);
6276         device_create_file(&(bd->pdev->dev), &dev_attr_ports_txcount);
6277 }
6278
6279 /* removes all the sys files created for that port */
6280 static void dgap_remove_ports_sysfiles(struct board_t *bd)
6281 {
6282         device_remove_file(&(bd->pdev->dev), &dev_attr_ports_state);
6283         device_remove_file(&(bd->pdev->dev), &dev_attr_ports_baud);
6284         device_remove_file(&(bd->pdev->dev), &dev_attr_ports_msignals);
6285         device_remove_file(&(bd->pdev->dev), &dev_attr_ports_iflag);
6286         device_remove_file(&(bd->pdev->dev), &dev_attr_ports_cflag);
6287         device_remove_file(&(bd->pdev->dev), &dev_attr_ports_oflag);
6288         device_remove_file(&(bd->pdev->dev), &dev_attr_ports_lflag);
6289         device_remove_file(&(bd->pdev->dev), &dev_attr_ports_digi_flag);
6290         device_remove_file(&(bd->pdev->dev), &dev_attr_ports_rxcount);
6291         device_remove_file(&(bd->pdev->dev), &dev_attr_ports_txcount);
6292 }
6293
6294 static ssize_t dgap_tty_state_show(struct device *d, struct device_attribute *attr, char *buf)
6295 {
6296         struct board_t *bd;
6297         struct channel_t *ch;
6298         struct un_t *un;
6299
6300         if (!d)
6301                 return 0;
6302         un = dev_get_drvdata(d);
6303         if (!un || un->magic != DGAP_UNIT_MAGIC)
6304                 return 0;
6305         ch = un->un_ch;
6306         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
6307                 return 0;
6308         bd = ch->ch_bd;
6309         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
6310                 return 0;
6311         if (bd->state != BOARD_READY)
6312                 return 0;
6313
6314         return snprintf(buf, PAGE_SIZE, "%s", un->un_open_count ? "Open" : "Closed");
6315 }
6316 static DEVICE_ATTR(state, S_IRUSR, dgap_tty_state_show, NULL);
6317
6318 static ssize_t dgap_tty_baud_show(struct device *d, struct device_attribute *attr, char *buf)
6319 {
6320         struct board_t *bd;
6321         struct channel_t *ch;
6322         struct un_t *un;
6323
6324         if (!d)
6325                 return 0;
6326         un = dev_get_drvdata(d);
6327         if (!un || un->magic != DGAP_UNIT_MAGIC)
6328                 return 0;
6329         ch = un->un_ch;
6330         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
6331                 return 0;
6332         bd = ch->ch_bd;
6333         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
6334                 return 0;
6335         if (bd->state != BOARD_READY)
6336                 return 0;
6337
6338         return snprintf(buf, PAGE_SIZE, "%d\n", ch->ch_baud_info);
6339 }
6340 static DEVICE_ATTR(baud, S_IRUSR, dgap_tty_baud_show, NULL);
6341
6342 static ssize_t dgap_tty_msignals_show(struct device *d, struct device_attribute *attr, char *buf)
6343 {
6344         struct board_t *bd;
6345         struct channel_t *ch;
6346         struct un_t *un;
6347
6348         if (!d)
6349                 return 0;
6350         un = dev_get_drvdata(d);
6351         if (!un || un->magic != DGAP_UNIT_MAGIC)
6352                 return 0;
6353         ch = un->un_ch;
6354         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
6355                 return 0;
6356         bd = ch->ch_bd;
6357         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
6358                 return 0;
6359         if (bd->state != BOARD_READY)
6360                 return 0;
6361
6362         if (ch->ch_open_count) {
6363                 return snprintf(buf, PAGE_SIZE, "%s %s %s %s %s %s\n",
6364                         (ch->ch_mostat & UART_MCR_RTS) ? "RTS" : "",
6365                         (ch->ch_mistat & UART_MSR_CTS) ? "CTS" : "",
6366                         (ch->ch_mostat & UART_MCR_DTR) ? "DTR" : "",
6367                         (ch->ch_mistat & UART_MSR_DSR) ? "DSR" : "",
6368                         (ch->ch_mistat & UART_MSR_DCD) ? "DCD" : "",
6369                         (ch->ch_mistat & UART_MSR_RI)  ? "RI"  : "");
6370         }
6371         return 0;
6372 }
6373 static DEVICE_ATTR(msignals, S_IRUSR, dgap_tty_msignals_show, NULL);
6374
6375 static ssize_t dgap_tty_iflag_show(struct device *d, struct device_attribute *attr, char *buf)
6376 {
6377         struct board_t *bd;
6378         struct channel_t *ch;
6379         struct un_t *un;
6380
6381         if (!d)
6382                 return 0;
6383         un = dev_get_drvdata(d);
6384         if (!un || un->magic != DGAP_UNIT_MAGIC)
6385                 return 0;
6386         ch = un->un_ch;
6387         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
6388                 return 0;
6389         bd = ch->ch_bd;
6390         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
6391                 return 0;
6392         if (bd->state != BOARD_READY)
6393                 return 0;
6394
6395         return snprintf(buf, PAGE_SIZE, "%x\n", ch->ch_c_iflag);
6396 }
6397 static DEVICE_ATTR(iflag, S_IRUSR, dgap_tty_iflag_show, NULL);
6398
6399 static ssize_t dgap_tty_cflag_show(struct device *d, struct device_attribute *attr, char *buf)
6400 {
6401         struct board_t *bd;
6402         struct channel_t *ch;
6403         struct un_t *un;
6404
6405         if (!d)
6406                 return 0;
6407         un = dev_get_drvdata(d);
6408         if (!un || un->magic != DGAP_UNIT_MAGIC)
6409                 return 0;
6410         ch = un->un_ch;
6411         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
6412                 return 0;
6413         bd = ch->ch_bd;
6414         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
6415                 return 0;
6416         if (bd->state != BOARD_READY)
6417                 return 0;
6418
6419         return snprintf(buf, PAGE_SIZE, "%x\n", ch->ch_c_cflag);
6420 }
6421 static DEVICE_ATTR(cflag, S_IRUSR, dgap_tty_cflag_show, NULL);
6422
6423 static ssize_t dgap_tty_oflag_show(struct device *d, struct device_attribute *attr, char *buf)
6424 {
6425         struct board_t *bd;
6426         struct channel_t *ch;
6427         struct un_t *un;
6428
6429         if (!d)
6430                 return 0;
6431         un = dev_get_drvdata(d);
6432         if (!un || un->magic != DGAP_UNIT_MAGIC)
6433                 return 0;
6434         ch = un->un_ch;
6435         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
6436                 return 0;
6437         bd = ch->ch_bd;
6438         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
6439                 return 0;
6440         if (bd->state != BOARD_READY)
6441                 return 0;
6442
6443         return snprintf(buf, PAGE_SIZE, "%x\n", ch->ch_c_oflag);
6444 }
6445 static DEVICE_ATTR(oflag, S_IRUSR, dgap_tty_oflag_show, NULL);
6446
6447 static ssize_t dgap_tty_lflag_show(struct device *d, struct device_attribute *attr, char *buf)
6448 {
6449         struct board_t *bd;
6450         struct channel_t *ch;
6451         struct un_t *un;
6452
6453         if (!d)
6454                 return 0;
6455         un = dev_get_drvdata(d);
6456         if (!un || un->magic != DGAP_UNIT_MAGIC)
6457                 return 0;
6458         ch = un->un_ch;
6459         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
6460                 return 0;
6461         bd = ch->ch_bd;
6462         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
6463                 return 0;
6464         if (bd->state != BOARD_READY)
6465                 return 0;
6466
6467         return snprintf(buf, PAGE_SIZE, "%x\n", ch->ch_c_lflag);
6468 }
6469 static DEVICE_ATTR(lflag, S_IRUSR, dgap_tty_lflag_show, NULL);
6470
6471 static ssize_t dgap_tty_digi_flag_show(struct device *d, struct device_attribute *attr, char *buf)
6472 {
6473         struct board_t *bd;
6474         struct channel_t *ch;
6475         struct un_t *un;
6476
6477         if (!d)
6478                 return 0;
6479         un = dev_get_drvdata(d);
6480         if (!un || un->magic != DGAP_UNIT_MAGIC)
6481                 return 0;
6482         ch = un->un_ch;
6483         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
6484                 return 0;
6485         bd = ch->ch_bd;
6486         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
6487                 return 0;
6488         if (bd->state != BOARD_READY)
6489                 return 0;
6490
6491         return snprintf(buf, PAGE_SIZE, "%x\n", ch->ch_digi.digi_flags);
6492 }
6493 static DEVICE_ATTR(digi_flag, S_IRUSR, dgap_tty_digi_flag_show, NULL);
6494
6495 static ssize_t dgap_tty_rxcount_show(struct device *d, struct device_attribute *attr, char *buf)
6496 {
6497         struct board_t *bd;
6498         struct channel_t *ch;
6499         struct un_t *un;
6500
6501         if (!d)
6502                 return 0;
6503         un = dev_get_drvdata(d);
6504         if (!un || un->magic != DGAP_UNIT_MAGIC)
6505                 return 0;
6506         ch = un->un_ch;
6507         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
6508                 return 0;
6509         bd = ch->ch_bd;
6510         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
6511                 return 0;
6512         if (bd->state != BOARD_READY)
6513                 return 0;
6514
6515         return snprintf(buf, PAGE_SIZE, "%ld\n", ch->ch_rxcount);
6516 }
6517 static DEVICE_ATTR(rxcount, S_IRUSR, dgap_tty_rxcount_show, NULL);
6518
6519 static ssize_t dgap_tty_txcount_show(struct device *d, struct device_attribute *attr, char *buf)
6520 {
6521         struct board_t *bd;
6522         struct channel_t *ch;
6523         struct un_t *un;
6524
6525         if (!d)
6526                 return 0;
6527         un = dev_get_drvdata(d);
6528         if (!un || un->magic != DGAP_UNIT_MAGIC)
6529                 return 0;
6530         ch = un->un_ch;
6531         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
6532                 return 0;
6533         bd = ch->ch_bd;
6534         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
6535                 return 0;
6536         if (bd->state != BOARD_READY)
6537                 return 0;
6538
6539         return snprintf(buf, PAGE_SIZE, "%ld\n", ch->ch_txcount);
6540 }
6541 static DEVICE_ATTR(txcount, S_IRUSR, dgap_tty_txcount_show, NULL);
6542
6543 static ssize_t dgap_tty_name_show(struct device *d, struct device_attribute *attr, char *buf)
6544 {
6545         struct board_t *bd;
6546         struct channel_t *ch;
6547         struct un_t *un;
6548         int     cn;
6549         int     bn;
6550         struct cnode *cptr = NULL;
6551         int found = FALSE;
6552         int ncount = 0;
6553         int starto = 0;
6554         int i = 0;
6555
6556         if (!d)
6557                 return 0;
6558         un = dev_get_drvdata(d);
6559         if (!un || un->magic != DGAP_UNIT_MAGIC)
6560                 return 0;
6561         ch = un->un_ch;
6562         if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
6563                 return 0;
6564         bd = ch->ch_bd;
6565         if (!bd || bd->magic != DGAP_BOARD_MAGIC)
6566                 return 0;
6567         if (bd->state != BOARD_READY)
6568                 return 0;
6569
6570         bn = bd->boardnum;
6571         cn = ch->ch_portnum;
6572
6573         for (cptr = bd->bd_config; cptr; cptr = cptr->next) {
6574
6575                 if ((cptr->type == BNODE) &&
6576                     ((cptr->u.board.type == APORT2_920P) || (cptr->u.board.type == APORT4_920P) ||
6577                      (cptr->u.board.type == APORT8_920P) || (cptr->u.board.type == PAPORT4) ||
6578                      (cptr->u.board.type == PAPORT8))) {
6579
6580                                 found = TRUE;
6581                                 if (cptr->u.board.v_start)
6582                                         starto = cptr->u.board.start;
6583                                 else
6584                                         starto = 1;
6585                 }
6586
6587                 if (cptr->type == TNODE && found == TRUE) {
6588                         char *ptr1;
6589                         if (strstr(cptr->u.ttyname, "tty")) {
6590                                 ptr1 = cptr->u.ttyname;
6591                                 ptr1 += 3;
6592                         } else
6593                                 ptr1 = cptr->u.ttyname;
6594
6595                         for (i = 0; i < dgap_config_get_number_of_ports(bd); i++) {
6596                                 if (cn == i)
6597                                         return snprintf(buf, PAGE_SIZE, "%s%s%02d\n",
6598                                                 (un->un_type == DGAP_PRINT) ? "pr" : "tty",
6599                                                 ptr1, i + starto);
6600                         }
6601                 }
6602
6603                 if (cptr->type == CNODE) {
6604
6605                         for (i = 0; i < cptr->u.conc.nport; i++) {
6606                                 if (cn == (i + ncount))
6607
6608                                         return snprintf(buf, PAGE_SIZE, "%s%s%02d\n",
6609                                                 (un->un_type == DGAP_PRINT) ? "pr" : "tty",
6610                                                 cptr->u.conc.id,
6611                                                 i + (cptr->u.conc.v_start ? cptr->u.conc.start : 1));
6612                         }
6613
6614                         ncount += cptr->u.conc.nport;
6615                 }
6616
6617                 if (cptr->type == MNODE) {
6618
6619                         for (i = 0; i < cptr->u.module.nport; i++) {
6620                                 if (cn == (i + ncount))
6621                                         return snprintf(buf, PAGE_SIZE, "%s%s%02d\n",
6622                                                 (un->un_type == DGAP_PRINT) ? "pr" : "tty",
6623                                                 cptr->u.module.id,
6624                                                 i + (cptr->u.module.v_start ? cptr->u.module.start : 1));
6625                         }
6626
6627                         ncount += cptr->u.module.nport;
6628
6629                 }
6630         }
6631
6632         return snprintf(buf, PAGE_SIZE, "%s_dgap_%d_%d\n",
6633                 (un->un_type == DGAP_PRINT) ? "pr" : "tty", bn, cn);
6634
6635 }
6636 static DEVICE_ATTR(custom_name, S_IRUSR, dgap_tty_name_show, NULL);
6637
6638 static struct attribute *dgap_sysfs_tty_entries[] = {
6639         &dev_attr_state.attr,
6640         &dev_attr_baud.attr,
6641         &dev_attr_msignals.attr,
6642         &dev_attr_iflag.attr,
6643         &dev_attr_cflag.attr,
6644         &dev_attr_oflag.attr,
6645         &dev_attr_lflag.attr,
6646         &dev_attr_digi_flag.attr,
6647         &dev_attr_rxcount.attr,
6648         &dev_attr_txcount.attr,
6649         &dev_attr_custom_name.attr,
6650         NULL
6651 };
6652
6653 static struct attribute_group dgap_tty_attribute_group = {
6654         .name = NULL,
6655         .attrs = dgap_sysfs_tty_entries,
6656 };
6657
6658 static void dgap_create_tty_sysfs(struct un_t *un, struct device *c)
6659 {
6660         int ret;
6661
6662         ret = sysfs_create_group(&c->kobj, &dgap_tty_attribute_group);
6663         if (ret)
6664                 return;
6665
6666         dev_set_drvdata(c, un);
6667
6668 }
6669
6670 static void dgap_remove_tty_sysfs(struct device *c)
6671 {
6672         sysfs_remove_group(&c->kobj, &dgap_tty_attribute_group);
6673 }
6674
6675 /*
6676  * Parse a configuration file read into memory as a string.
6677  */
6678 static int      dgap_parsefile(char **in, int Remove)
6679 {
6680         struct cnode *p, *brd, *line, *conc;
6681         int     rc;
6682         char    *s = NULL, *s2 = NULL;
6683         int     linecnt = 0;
6684
6685         p = &dgap_head;
6686         brd = line = conc = NULL;
6687
6688         /* perhaps we are adding to an existing list? */
6689         while (p->next != NULL)
6690                 p = p->next;
6691
6692         /* file must start with a BEGIN */
6693         while ((rc = dgap_gettok(in, p)) != BEGIN) {
6694                 if (rc == 0) {
6695                         dgap_err("unexpected EOF");
6696                         return -1;
6697                 }
6698         }
6699
6700         for (; ;) {
6701                 rc = dgap_gettok(in, p);
6702                 if (rc == 0) {
6703                         dgap_err("unexpected EOF");
6704                         return -1;
6705                 }
6706
6707                 switch (rc) {
6708                 case 0:
6709                         dgap_err("unexpected end of file");
6710                         return -1;
6711
6712                 case BEGIN:     /* should only be 1 begin */
6713                         dgap_err("unexpected config_begin\n");
6714                         return -1;
6715
6716                 case END:
6717                         return 0;
6718
6719                 case BOARD:     /* board info */
6720                         if (dgap_checknode(p))
6721                                 return -1;
6722                         p->next = dgap_newnode(BNODE);
6723                         if (!p->next) {
6724                                 dgap_err("out of memory");
6725                                 return -1;
6726                         }
6727                         p = p->next;
6728
6729                         p->u.board.status = dgap_savestring("No");
6730                         line = conc = NULL;
6731                         brd = p;
6732                         linecnt = -1;
6733                         break;
6734
6735                 case APORT2_920P:       /* AccelePort_4 */
6736                         if (p->type != BNODE) {
6737                                 dgap_err("unexpected Digi_2r_920 string");
6738                                 return -1;
6739                         }
6740                         p->u.board.type = APORT2_920P;
6741                         p->u.board.v_type = 1;
6742                         break;
6743
6744                 case APORT4_920P:       /* AccelePort_4 */
6745                         if (p->type != BNODE) {
6746                                 dgap_err("unexpected Digi_4r_920 string");
6747                                 return -1;
6748                         }
6749                         p->u.board.type = APORT4_920P;
6750                         p->u.board.v_type = 1;
6751                         break;
6752
6753                 case APORT8_920P:       /* AccelePort_8 */
6754                         if (p->type != BNODE) {
6755                                 dgap_err("unexpected Digi_8r_920 string");
6756                                 return -1;
6757                         }
6758                         p->u.board.type = APORT8_920P;
6759                         p->u.board.v_type = 1;
6760                         break;
6761
6762                 case PAPORT4:   /* AccelePort_4 PCI */
6763                         if (p->type != BNODE) {
6764                                 dgap_err("unexpected Digi_4r(PCI) string");
6765                                 return -1;
6766                         }
6767                         p->u.board.type = PAPORT4;
6768                         p->u.board.v_type = 1;
6769                         break;
6770
6771                 case PAPORT8:   /* AccelePort_8 PCI */
6772                         if (p->type != BNODE) {
6773                                 dgap_err("unexpected Digi_8r string");
6774                                 return -1;
6775                         }
6776                         p->u.board.type = PAPORT8;
6777                         p->u.board.v_type = 1;
6778                         break;
6779
6780                 case PCX:       /* PCI C/X */
6781                         if (p->type != BNODE) {
6782                                 dgap_err("unexpected Digi_C/X_(PCI) string");
6783                                 return -1;
6784                         }
6785                         p->u.board.type = PCX;
6786                         p->u.board.v_type = 1;
6787                         p->u.board.conc1 = 0;
6788                         p->u.board.conc2 = 0;
6789                         p->u.board.module1 = 0;
6790                         p->u.board.module2 = 0;
6791                         break;
6792
6793                 case PEPC:      /* PCI EPC/X */
6794                         if (p->type != BNODE) {
6795                                 dgap_err("unexpected \"Digi_EPC/X_(PCI)\" string");
6796                                 return -1;
6797                         }
6798                         p->u.board.type = PEPC;
6799                         p->u.board.v_type = 1;
6800                         p->u.board.conc1 = 0;
6801                         p->u.board.conc2 = 0;
6802                         p->u.board.module1 = 0;
6803                         p->u.board.module2 = 0;
6804                         break;
6805
6806                 case PPCM:      /* PCI/Xem */
6807                         if (p->type != BNODE) {
6808                                 dgap_err("unexpected PCI/Xem string");
6809                                 return -1;
6810                         }
6811                         p->u.board.type = PPCM;
6812                         p->u.board.v_type = 1;
6813                         p->u.board.conc1 = 0;
6814                         p->u.board.conc2 = 0;
6815                         break;
6816
6817                 case IO:        /* i/o port */
6818                         if (p->type != BNODE) {
6819                                 dgap_err("IO port only vaild for boards");
6820                                 return -1;
6821                         }
6822                         s = dgap_getword(in);
6823                         if (s == NULL) {
6824                                 dgap_err("unexpected end of file");
6825                                 return -1;
6826                         }
6827                         p->u.board.portstr = dgap_savestring(s);
6828                         p->u.board.port = (short)simple_strtol(s, &s2, 0);
6829                         if ((short)strlen(s) > (short)(s2 - s)) {
6830                                 dgap_err("bad number for IO port");
6831                                 return -1;
6832                         }
6833                         p->u.board.v_port = 1;
6834                         break;
6835
6836                 case MEM:       /* memory address */
6837                         if (p->type != BNODE) {
6838                                 dgap_err("memory address only vaild for boards");
6839                                 return -1;
6840                         }
6841                         s = dgap_getword(in);
6842                         if (s == NULL) {
6843                                 dgap_err("unexpected end of file");
6844                                 return -1;
6845                         }
6846                         p->u.board.addrstr = dgap_savestring(s);
6847                         p->u.board.addr = simple_strtoul(s, &s2, 0);
6848                         if ((int)strlen(s) > (int)(s2 - s)) {
6849                                 dgap_err("bad number for memory address");
6850                                 return -1;
6851                         }
6852                         p->u.board.v_addr = 1;
6853                         break;
6854
6855                 case PCIINFO:   /* pci information */
6856                         if (p->type != BNODE) {
6857                                 dgap_err("memory address only vaild for boards");
6858                                 return -1;
6859                         }
6860                         s = dgap_getword(in);
6861                         if (s == NULL) {
6862                                 dgap_err("unexpected end of file");
6863                                 return -1;
6864                         }
6865                         p->u.board.pcibusstr = dgap_savestring(s);
6866                         p->u.board.pcibus = simple_strtoul(s, &s2, 0);
6867                         if ((int)strlen(s) > (int)(s2 - s)) {
6868                                 dgap_err("bad number for pci bus");
6869                                 return -1;
6870                         }
6871                         p->u.board.v_pcibus = 1;
6872                         s = dgap_getword(in);
6873                         if (s == NULL) {
6874                                 dgap_err("unexpected end of file");
6875                                 return -1;
6876                         }
6877                         p->u.board.pcislotstr = dgap_savestring(s);
6878                         p->u.board.pcislot = simple_strtoul(s, &s2, 0);
6879                         if ((int)strlen(s) > (int)(s2 - s)) {
6880                                 dgap_err("bad number for pci slot");
6881                                 return -1;
6882                         }
6883                         p->u.board.v_pcislot = 1;
6884                         break;
6885
6886                 case METHOD:
6887                         if (p->type != BNODE) {
6888                                 dgap_err("install method only vaild for boards");
6889                                 return -1;
6890                         }
6891                         s = dgap_getword(in);
6892                         if (s == NULL) {
6893                                 dgap_err("unexpected end of file");
6894                                 return -1;
6895                         }
6896                         p->u.board.method = dgap_savestring(s);
6897                         p->u.board.v_method = 1;
6898                         break;
6899
6900                 case STATUS:
6901                         if (p->type != BNODE) {
6902                                 dgap_err("config status only vaild for boards");
6903                                 return -1;
6904                         }
6905                         s = dgap_getword(in);
6906                         if (s == NULL) {
6907                                 dgap_err("unexpected end of file");
6908                                 return -1;
6909                         }
6910                         p->u.board.status = dgap_savestring(s);
6911                         break;
6912
6913                 case NPORTS:    /* number of ports */
6914                         if (p->type == BNODE) {
6915                                 s = dgap_getword(in);
6916                                 if (s == NULL) {
6917                                         dgap_err("unexpected end of file");
6918                                         return -1;
6919                                 }
6920                                 p->u.board.nport = (char)simple_strtol(s, &s2, 0);
6921                                 if ((int)strlen(s) > (int)(s2 - s)) {
6922                                         dgap_err("bad number for number of ports");
6923                                         return -1;
6924                                 }
6925                                 p->u.board.v_nport = 1;
6926                         } else if (p->type == CNODE) {
6927                                 s = dgap_getword(in);
6928                                 if (s == NULL) {
6929                                         dgap_err("unexpected end of file");
6930                                         return -1;
6931                                 }
6932                                 p->u.conc.nport = (char)simple_strtol(s, &s2, 0);
6933                                 if ((int)strlen(s) > (int)(s2 - s)) {
6934                                         dgap_err("bad number for number of ports");
6935                                         return -1;
6936                                 }
6937                                 p->u.conc.v_nport = 1;
6938                         } else if (p->type == MNODE) {
6939                                 s = dgap_getword(in);
6940                                 if (s == NULL) {
6941                                         dgap_err("unexpected end of file");
6942                                         return -1;
6943                                 }
6944                                 p->u.module.nport = (char)simple_strtol(s, &s2, 0);
6945                                 if ((int)strlen(s) > (int)(s2 - s)) {
6946                                         dgap_err("bad number for number of ports");
6947                                         return -1;
6948                                 }
6949                                 p->u.module.v_nport = 1;
6950                         } else {
6951                                 dgap_err("nports only valid for concentrators or modules");
6952                                 return -1;
6953                         }
6954                         break;
6955
6956                 case ID:        /* letter ID used in tty name */
6957                         s = dgap_getword(in);
6958                         if (s == NULL) {
6959                                 dgap_err("unexpected end of file");
6960                                 return -1;
6961                         }
6962
6963                         p->u.board.status = dgap_savestring(s);
6964
6965                         if (p->type == CNODE) {
6966                                 p->u.conc.id = dgap_savestring(s);
6967                                 p->u.conc.v_id = 1;
6968                         } else if (p->type == MNODE) {
6969                                 p->u.module.id = dgap_savestring(s);
6970                                 p->u.module.v_id = 1;
6971                         } else {
6972                                 dgap_err("id only valid for concentrators or modules");
6973                                 return -1;
6974                         }
6975                         break;
6976
6977                 case STARTO:    /* start offset of ID */
6978                         if (p->type == BNODE) {
6979                                 s = dgap_getword(in);
6980                                 if (s == NULL) {
6981                                         dgap_err("unexpected end of file");
6982                                         return -1;
6983                                 }
6984                                 p->u.board.start = simple_strtol(s, &s2, 0);
6985                                 if ((int)strlen(s) > (int)(s2 - s)) {
6986                                         dgap_err("bad number for start of tty count");
6987                                         return -1;
6988                                 }
6989                                 p->u.board.v_start = 1;
6990                         } else if (p->type == CNODE) {
6991                                 s = dgap_getword(in);
6992                                 if (s == NULL) {
6993                                         dgap_err("unexpected end of file");
6994                                         return -1;
6995                                 }
6996                                 p->u.conc.start = simple_strtol(s, &s2, 0);
6997                                 if ((int)strlen(s) > (int)(s2 - s)) {
6998                                         dgap_err("bad number for start of tty count");
6999                                         return -1;
7000                                 }
7001                                 p->u.conc.v_start = 1;
7002                         } else if (p->type == MNODE) {
7003                                 s = dgap_getword(in);
7004                                 if (s == NULL) {
7005                                         dgap_err("unexpected end of file");
7006                                         return -1;
7007                                 }
7008                                 p->u.module.start = simple_strtol(s, &s2, 0);
7009                                 if ((int)strlen(s) > (int)(s2 - s)) {
7010                                         dgap_err("bad number for start of tty count");
7011                                         return -1;
7012                                 }
7013                                 p->u.module.v_start = 1;
7014                         } else {
7015                                 dgap_err("start only valid for concentrators or modules");
7016                                 return -1;
7017                         }
7018                         break;
7019
7020                 case TTYN:      /* tty name prefix */
7021                         if (dgap_checknode(p))
7022                                 return -1;
7023                         p->next = dgap_newnode(TNODE);
7024                         if (!p->next) {
7025                                 dgap_err("out of memory");
7026                                 return -1;
7027                         }
7028                         p = p->next;
7029                         s = dgap_getword(in);
7030                         if (!s) {
7031                                 dgap_err("unexpeced end of file");
7032                                 return -1;
7033                         }
7034                         p->u.ttyname = dgap_savestring(s);
7035                         if (!p->u.ttyname) {
7036                                 dgap_err("out of memory");
7037                                 return -1;
7038                         }
7039                         break;
7040
7041                 case CU:        /* cu name prefix */
7042                         if (dgap_checknode(p))
7043                                 return -1;
7044                         p->next = dgap_newnode(CUNODE);
7045                         if (!p->next) {
7046                                 dgap_err("out of memory");
7047                                 return -1;
7048                         }
7049                         p = p->next;
7050                         s = dgap_getword(in);
7051                         if (!s) {
7052                                 dgap_err("unexpeced end of file");
7053                                 return -1;
7054                         }
7055                         p->u.cuname = dgap_savestring(s);
7056                         if (!p->u.cuname) {
7057                                 dgap_err("out of memory");
7058                                 return -1;
7059                         }
7060                         break;
7061
7062                 case LINE:      /* line information */
7063                         if (dgap_checknode(p))
7064                                 return -1;
7065                         if (brd == NULL) {
7066                                 dgap_err("must specify board before line info");
7067                                 return -1;
7068                         }
7069                         switch (brd->u.board.type) {
7070                         case PPCM:
7071                                 dgap_err("line not vaild for PC/em");
7072                                 return -1;
7073                         }
7074                         p->next = dgap_newnode(LNODE);
7075                         if (!p->next) {
7076                                 dgap_err("out of memory");
7077                                 return -1;
7078                         }
7079                         p = p->next;
7080                         conc = NULL;
7081                         line = p;
7082                         linecnt++;
7083                         break;
7084
7085                 case CONC:      /* concentrator information */
7086                         if (dgap_checknode(p))
7087                                 return -1;
7088                         if (line == NULL) {
7089                                 dgap_err("must specify line info before concentrator");
7090                                 return -1;
7091                         }
7092                         p->next = dgap_newnode(CNODE);
7093                         if (!p->next) {
7094                                 dgap_err("out of memory");
7095                                 return -1;
7096                         }
7097                         p = p->next;
7098                         conc = p;
7099                         if (linecnt)
7100                                 brd->u.board.conc2++;
7101                         else
7102                                 brd->u.board.conc1++;
7103
7104                         break;
7105
7106                 case CX:        /* c/x type concentrator */
7107                         if (p->type != CNODE) {
7108                                 dgap_err("cx only valid for concentrators");
7109                                 return -1;
7110                         }
7111                         p->u.conc.type = CX;
7112                         p->u.conc.v_type = 1;
7113                         break;
7114
7115                 case EPC:       /* epc type concentrator */
7116                         if (p->type != CNODE) {
7117                                 dgap_err("cx only valid for concentrators");
7118                                 return -1;
7119                         }
7120                         p->u.conc.type = EPC;
7121                         p->u.conc.v_type = 1;
7122                         break;
7123
7124                 case MOD:       /* EBI module */
7125                         if (dgap_checknode(p))
7126                                 return -1;
7127                         if (brd == NULL) {
7128                                 dgap_err("must specify board info before EBI modules");
7129                                 return -1;
7130                         }
7131                         switch (brd->u.board.type) {
7132                         case PPCM:
7133                                 linecnt = 0;
7134                                 break;
7135                         default:
7136                                 if (conc == NULL) {
7137                                         dgap_err("must specify concentrator info before EBI module");
7138                                         return -1;
7139                                 }
7140                         }
7141                         p->next = dgap_newnode(MNODE);
7142                         if (!p->next) {
7143                                 dgap_err("out of memory");
7144                                 return -1;
7145                         }
7146                         p = p->next;
7147                         if (linecnt)
7148                                 brd->u.board.module2++;
7149                         else
7150                                 brd->u.board.module1++;
7151
7152                         break;
7153
7154                 case PORTS:     /* ports type EBI module */
7155                         if (p->type != MNODE) {
7156                                 dgap_err("ports only valid for EBI modules");
7157                                 return -1;
7158                         }
7159                         p->u.module.type = PORTS;
7160                         p->u.module.v_type = 1;
7161                         break;
7162
7163                 case MODEM:     /* ports type EBI module */
7164                         if (p->type != MNODE) {
7165                                 dgap_err("modem only valid for modem modules");
7166                                 return -1;
7167                         }
7168                         p->u.module.type = MODEM;
7169                         p->u.module.v_type = 1;
7170                         break;
7171
7172                 case CABLE:
7173                         if (p->type == LNODE) {
7174                                 s = dgap_getword(in);
7175                                 if (!s) {
7176                                         dgap_err("unexpected end of file");
7177                                         return -1;
7178                                 }
7179                                 p->u.line.cable = dgap_savestring(s);
7180                                 p->u.line.v_cable = 1;
7181                         }
7182                         break;
7183
7184                 case SPEED:     /* sync line speed indication */
7185                         if (p->type == LNODE) {
7186                                 s = dgap_getword(in);
7187                                 if (s == NULL) {
7188                                         dgap_err("unexpected end of file");
7189                                         return -1;
7190                                 }
7191                                 p->u.line.speed = (char)simple_strtol(s, &s2, 0);
7192                                 if ((short)strlen(s) > (short)(s2 - s)) {
7193                                         dgap_err("bad number for line speed");
7194                                         return -1;
7195                                 }
7196                                 p->u.line.v_speed = 1;
7197                         } else if (p->type == CNODE) {
7198                                 s = dgap_getword(in);
7199                                 if (s == NULL) {
7200                                         dgap_err("unexpected end of file");
7201                                         return -1;
7202                                 }
7203                                 p->u.conc.speed = (char)simple_strtol(s, &s2, 0);
7204                                 if ((short)strlen(s) > (short)(s2 - s)) {
7205                                         dgap_err("bad number for line speed");
7206                                         return -1;
7207                                 }
7208                                 p->u.conc.v_speed = 1;
7209                         } else {
7210                                 dgap_err("speed valid only for lines or concentrators.");
7211                                 return -1;
7212                         }
7213                         break;
7214
7215                 case CONNECT:
7216                         if (p->type == CNODE) {
7217                                 s = dgap_getword(in);
7218                                 if (!s) {
7219                                         dgap_err("unexpected end of file");
7220                                         return -1;
7221                                 }
7222                                 p->u.conc.connect = dgap_savestring(s);
7223                                 p->u.conc.v_connect = 1;
7224                         }
7225                         break;
7226                 case PRINT:     /* transparent print name prefix */
7227                         if (dgap_checknode(p))
7228                                 return -1;
7229                         p->next = dgap_newnode(PNODE);
7230                         if (!p->next) {
7231                                 dgap_err("out of memory");
7232                                 return -1;
7233                         }
7234                         p = p->next;
7235                         s = dgap_getword(in);
7236                         if (!s) {
7237                                 dgap_err("unexpeced end of file");
7238                                 return -1;
7239                         }
7240                         p->u.printname = dgap_savestring(s);
7241                         if (!p->u.printname) {
7242                                 dgap_err("out of memory");
7243                                 return -1;
7244                         }
7245                         break;
7246
7247                 case CMAJOR:    /* major number */
7248                         if (dgap_checknode(p))
7249                                 return -1;
7250                         p->next = dgap_newnode(JNODE);
7251                         if (!p->next) {
7252                                 dgap_err("out of memory");
7253                                 return -1;
7254                         }
7255                         p = p->next;
7256                         s = dgap_getword(in);
7257                         if (s == NULL) {
7258                                 dgap_err("unexpected end of file");
7259                                 return -1;
7260                         }
7261                         p->u.majornumber = simple_strtol(s, &s2, 0);
7262                         if ((int)strlen(s) > (int)(s2 - s)) {
7263                                 dgap_err("bad number for major number");
7264                                 return -1;
7265                         }
7266                         break;
7267
7268                 case ALTPIN:    /* altpin setting */
7269                         if (dgap_checknode(p))
7270                                 return -1;
7271                         p->next = dgap_newnode(ANODE);
7272                         if (!p->next) {
7273                                 dgap_err("out of memory");
7274                                 return -1;
7275                         }
7276                         p = p->next;
7277                         s = dgap_getword(in);
7278                         if (s == NULL) {
7279                                 dgap_err("unexpected end of file");
7280                                 return -1;
7281                         }
7282                         p->u.altpin = simple_strtol(s, &s2, 0);
7283                         if ((int)strlen(s) > (int)(s2 - s)) {
7284                                 dgap_err("bad number for altpin");
7285                                 return -1;
7286                         }
7287                         break;
7288
7289                 case USEINTR:           /* enable interrupt setting */
7290                         if (dgap_checknode(p))
7291                                 return -1;
7292                         p->next = dgap_newnode(INTRNODE);
7293                         if (!p->next) {
7294                                 dgap_err("out of memory");
7295                                 return -1;
7296                         }
7297                         p = p->next;
7298                         s = dgap_getword(in);
7299                         if (s == NULL) {
7300                                 dgap_err("unexpected end of file");
7301                                 return -1;
7302                         }
7303                         p->u.useintr = simple_strtol(s, &s2, 0);
7304                         if ((int)strlen(s) > (int)(s2 - s)) {
7305                                 dgap_err("bad number for useintr");
7306                                 return -1;
7307                         }
7308                         break;
7309
7310                 case TTSIZ:     /* size of tty structure */
7311                         if (dgap_checknode(p))
7312                                 return -1;
7313                         p->next = dgap_newnode(TSNODE);
7314                         if (!p->next) {
7315                                 dgap_err("out of memory");
7316                                 return -1;
7317                         }
7318                         p = p->next;
7319                         s = dgap_getword(in);
7320                         if (s == NULL) {
7321                                 dgap_err("unexpected end of file");
7322                                 return -1;
7323                         }
7324                         p->u.ttysize = simple_strtol(s, &s2, 0);
7325                         if ((int)strlen(s) > (int)(s2 - s)) {
7326                                 dgap_err("bad number for ttysize");
7327                                 return -1;
7328                         }
7329                         break;
7330
7331                 case CHSIZ:     /* channel structure size */
7332                         if (dgap_checknode(p))
7333                                 return -1;
7334                         p->next = dgap_newnode(CSNODE);
7335                         if (!p->next) {
7336                                 dgap_err("out of memory");
7337                                 return -1;
7338                         }
7339                         p = p->next;
7340                         s = dgap_getword(in);
7341                         if (s == NULL) {
7342                                 dgap_err("unexpected end of file");
7343                                 return -1;
7344                         }
7345                         p->u.chsize = simple_strtol(s, &s2, 0);
7346                         if ((int)strlen(s) > (int)(s2 - s)) {
7347                                 dgap_err("bad number for chsize");
7348                                 return -1;
7349                         }
7350                         break;
7351
7352                 case BSSIZ:     /* board structure size */
7353                         if (dgap_checknode(p))
7354                                 return -1;
7355                         p->next = dgap_newnode(BSNODE);
7356                         if (!p->next) {
7357                                 dgap_err("out of memory");
7358                                 return -1;
7359                         }
7360                         p = p->next;
7361                         s = dgap_getword(in);
7362                         if (s == NULL) {
7363                                 dgap_err("unexpected end of file");
7364                                 return -1;
7365                         }
7366                         p->u.bssize = simple_strtol(s, &s2, 0);
7367                         if ((int)strlen(s) > (int)(s2 - s)) {
7368                                 dgap_err("bad number for bssize");
7369                                 return -1;
7370                         }
7371                         break;
7372
7373                 case UNTSIZ:    /* sched structure size */
7374                         if (dgap_checknode(p))
7375                                 return -1;
7376                         p->next = dgap_newnode(USNODE);
7377                         if (!p->next) {
7378                                 dgap_err("out of memory");
7379                                 return -1;
7380                         }
7381                         p = p->next;
7382                         s = dgap_getword(in);
7383                         if (s == NULL) {
7384                                 dgap_err("unexpected end of file");
7385                                 return -1;
7386                         }
7387                         p->u.unsize = simple_strtol(s, &s2, 0);
7388                         if ((int)strlen(s) > (int)(s2 - s)) {
7389                                 dgap_err("bad number for schedsize");
7390                                 return -1;
7391                         }
7392                         break;
7393
7394                 case F2SIZ:     /* f2200 structure size */
7395                         if (dgap_checknode(p))
7396                                 return -1;
7397                         p->next = dgap_newnode(FSNODE);
7398                         if (!p->next) {
7399                                 dgap_err("out of memory");
7400                                 return -1;
7401                         }
7402                         p = p->next;
7403                         s = dgap_getword(in);
7404                         if (s == NULL) {
7405                                 dgap_err("unexpected end of file");
7406                                 return -1;
7407                         }
7408                         p->u.f2size = simple_strtol(s, &s2, 0);
7409                         if ((int)strlen(s) > (int)(s2 - s)) {
7410                                 dgap_err("bad number for f2200size");
7411                                 return -1;
7412                         }
7413                         break;
7414
7415                 case VPSIZ:     /* vpix structure size */
7416                         if (dgap_checknode(p))
7417                                 return -1;
7418                         p->next = dgap_newnode(VSNODE);
7419                         if (!p->next) {
7420                                 dgap_err("out of memory");
7421                                 return -1;
7422                         }
7423                         p = p->next;
7424                         s = dgap_getword(in);
7425                         if (s == NULL) {
7426                                 dgap_err("unexpected end of file");
7427                                 return -1;
7428                         }
7429                         p->u.vpixsize = simple_strtol(s, &s2, 0);
7430                         if ((int)strlen(s) > (int)(s2 - s)) {
7431                                 dgap_err("bad number for vpixsize");
7432                                 return -1;
7433                         }
7434                         break;
7435                 }
7436         }
7437 }
7438
7439 /*
7440  * dgap_sindex: much like index(), but it looks for a match of any character in
7441  * the group, and returns that position.  If the first character is a ^, then
7442  * this will match the first occurrence not in that group.
7443  */
7444 static char *dgap_sindex(char *string, char *group)
7445 {
7446         char    *ptr;
7447
7448         if (!string || !group)
7449                 return (char *) NULL;
7450
7451         if (*group == '^') {
7452                 group++;
7453                 for (; *string; string++) {
7454                         for (ptr = group; *ptr; ptr++) {
7455                                 if (*ptr == *string)
7456                                         break;
7457                         }
7458                         if (*ptr == '\0')
7459                                 return string;
7460                 }
7461         } else {
7462                 for (; *string; string++) {
7463                         for (ptr = group; *ptr; ptr++) {
7464                                 if (*ptr == *string)
7465                                         return string;
7466                         }
7467                 }
7468         }
7469
7470         return (char *) NULL;
7471 }
7472
7473 /*
7474  * Get a token from the input file; return 0 if end of file is reached
7475  */
7476 static int dgap_gettok(char **in, struct cnode *p)
7477 {
7478         char    *w;
7479         struct toklist *t;
7480
7481         if (strstr(dgap_cword, "boar")) {
7482                 w = dgap_getword(in);
7483                 snprintf(dgap_cword, MAXCWORD, "%s", w);
7484                 for (t = dgap_tlist; t->token != 0; t++) {
7485                         if (!strcmp(w, t->string))
7486                                 return t->token;
7487                 }
7488                 dgap_err("board !!type not specified");
7489                 return 1;
7490         } else {
7491                 while ((w = dgap_getword(in))) {
7492                         snprintf(dgap_cword, MAXCWORD, "%s", w);
7493                         for (t = dgap_tlist; t->token != 0; t++) {
7494                                 if (!strcmp(w, t->string))
7495                                         return t->token;
7496                         }
7497                 }
7498                 return 0;
7499         }
7500 }
7501
7502 /*
7503  * get a word from the input stream, also keep track of current line number.
7504  * words are separated by whitespace.
7505  */
7506 static char *dgap_getword(char **in)
7507 {
7508         char *ret_ptr = *in;
7509
7510         char *ptr = dgap_sindex(*in, " \t\n");
7511
7512         /* If no word found, return null */
7513         if (!ptr)
7514                 return NULL;
7515
7516         /* Mark new location for our buffer */
7517         *ptr = '\0';
7518         *in = ptr + 1;
7519
7520         /* Eat any extra spaces/tabs/newlines that might be present */
7521         while (*in && **in && ((**in == ' ') || (**in == '\t') || (**in == '\n'))) {
7522                 **in = '\0';
7523                 *in = *in + 1;
7524         }
7525
7526         return ret_ptr;
7527 }
7528
7529 /*
7530  * print an error message, giving the line number in the file where
7531  * the error occurred.
7532  */
7533 static void dgap_err(char *s)
7534 {
7535         pr_err("dgap: parse: %s\n", s);
7536 }
7537
7538 /*
7539  * allocate a new configuration node of type t
7540  */
7541 static struct cnode *dgap_newnode(int t)
7542 {
7543         struct cnode *n;
7544
7545         n = kmalloc(sizeof(struct cnode), GFP_ATOMIC);
7546         if (n != NULL) {
7547                 memset((char *)n, 0, sizeof(struct cnode));
7548                 n->type = t;
7549         }
7550         return n;
7551 }
7552
7553 /*
7554  * dgap_checknode: see if all the necessary info has been supplied for a node
7555  * before creating the next node.
7556  */
7557 static int dgap_checknode(struct cnode *p)
7558 {
7559         switch (p->type) {
7560         case BNODE:
7561                 if (p->u.board.v_type == 0) {
7562                         dgap_err("board type !not specified");
7563                         return 1;
7564                 }
7565
7566                 return 0;
7567
7568         case LNODE:
7569                 if (p->u.line.v_speed == 0) {
7570                         dgap_err("line speed not specified");
7571                         return 1;
7572                 }
7573                 return 0;
7574
7575         case CNODE:
7576                 if (p->u.conc.v_type == 0) {
7577                         dgap_err("concentrator type not specified");
7578                         return 1;
7579                 }
7580                 if (p->u.conc.v_speed == 0) {
7581                         dgap_err("concentrator line speed not specified");
7582                         return 1;
7583                 }
7584                 if (p->u.conc.v_nport == 0) {
7585                         dgap_err("number of ports on concentrator not specified");
7586                         return 1;
7587                 }
7588                 if (p->u.conc.v_id == 0) {
7589                         dgap_err("concentrator id letter not specified");
7590                         return 1;
7591                 }
7592                 return 0;
7593
7594         case MNODE:
7595                 if (p->u.module.v_type == 0) {
7596                         dgap_err("EBI module type not specified");
7597                         return 1;
7598                 }
7599                 if (p->u.module.v_nport == 0) {
7600                         dgap_err("number of ports on EBI module not specified");
7601                         return 1;
7602                 }
7603                 if (p->u.module.v_id == 0) {
7604                         dgap_err("EBI module id letter not specified");
7605                         return 1;
7606                 }
7607                 return 0;
7608         }
7609         return 0;
7610 }
7611
7612 /*
7613  * save a string somewhere
7614  */
7615 static char     *dgap_savestring(char *s)
7616 {
7617         char    *p;
7618
7619         p = kmalloc(strlen(s) + 1, GFP_ATOMIC);
7620         if (p)
7621                 strcpy(p, s);
7622         return p;
7623 }
7624
7625 /*
7626  * Given a board pointer, returns whether we should use interrupts or not.
7627  */
7628 static uint dgap_config_get_useintr(struct board_t *bd)
7629 {
7630         struct cnode *p = NULL;
7631
7632         if (!bd)
7633                 return 0;
7634
7635         for (p = bd->bd_config; p; p = p->next) {
7636                 switch (p->type) {
7637                 case INTRNODE:
7638                         /*
7639                          * check for pcxr types.
7640                          */
7641                         return p->u.useintr;
7642                 default:
7643                         break;
7644                 }
7645         }
7646
7647         /* If not found, then don't turn on interrupts. */
7648         return 0;
7649 }
7650
7651 /*
7652  * Given a board pointer, returns whether we turn on altpin or not.
7653  */
7654 static uint dgap_config_get_altpin(struct board_t *bd)
7655 {
7656         struct cnode *p = NULL;
7657
7658         if (!bd)
7659                 return 0;
7660
7661         for (p = bd->bd_config; p; p = p->next) {
7662                 switch (p->type) {
7663                 case ANODE:
7664                         /*
7665                          * check for pcxr types.
7666                          */
7667                         return p->u.altpin;
7668                 default:
7669                         break;
7670                 }
7671         }
7672
7673         /* If not found, then don't turn on interrupts. */
7674         return 0;
7675 }
7676
7677 /*
7678  * Given a specific type of board, if found, detached link and
7679  * returns the first occurrence in the list.
7680  */
7681 static struct cnode *dgap_find_config(int type, int bus, int slot)
7682 {
7683         struct cnode *p, *prev = NULL, *prev2 = NULL, *found = NULL;
7684
7685         p = &dgap_head;
7686
7687         while (p->next != NULL) {
7688                 prev = p;
7689                 p = p->next;
7690
7691                 if (p->type == BNODE) {
7692
7693                         if (p->u.board.type == type) {
7694
7695                                 if (p->u.board.v_pcibus &&
7696                                     p->u.board.pcibus != bus)
7697                                         continue;
7698                                 if (p->u.board.v_pcislot &&
7699                                     p->u.board.pcislot != slot)
7700                                         continue;
7701
7702                                 found = p;
7703                                 /*
7704                                  * Keep walking thru the list till we find the next board.
7705                                  */
7706                                 while (p->next != NULL) {
7707                                         prev2 = p;
7708                                         p = p->next;
7709                                         if (p->type == BNODE) {
7710
7711                                                 /*
7712                                                  * Mark the end of our 1 board chain of configs.
7713                                                  */
7714                                                 prev2->next = NULL;
7715
7716                                                 /*
7717                                                  * Link the "next" board to the previous board,
7718                                                  * effectively "unlinking" our board from the main config.
7719                                                  */
7720                                                 prev->next = p;
7721
7722                                                 return found;
7723                                         }
7724                                 }
7725                                 /*
7726                                  * It must be the last board in the list.
7727                                  */
7728                                 prev->next = NULL;
7729                                 return found;
7730                         }
7731                 }
7732         }
7733         return NULL;
7734 }
7735
7736 /*
7737  * Given a board pointer, walks the config link, counting up
7738  * all ports user specified should be on the board.
7739  * (This does NOT mean they are all actually present right now tho)
7740  */
7741 static uint dgap_config_get_number_of_ports(struct board_t *bd)
7742 {
7743         int count = 0;
7744         struct cnode *p = NULL;
7745
7746         if (!bd)
7747                 return 0;
7748
7749         for (p = bd->bd_config; p; p = p->next) {
7750
7751                 switch (p->type) {
7752                 case BNODE:
7753                         /*
7754                          * check for pcxr types.
7755                          */
7756                         if (p->u.board.type > EPCFE)
7757                                 count += p->u.board.nport;
7758                         break;
7759                 case CNODE:
7760                         count += p->u.conc.nport;
7761                         break;
7762                 case MNODE:
7763                         count += p->u.module.nport;
7764                         break;
7765                 }
7766         }
7767         return count;
7768 }
7769
7770 static char *dgap_create_config_string(struct board_t *bd, char *string)
7771 {
7772         char *ptr = string;
7773         struct cnode *p = NULL;
7774         struct cnode *q = NULL;
7775         int speed;
7776
7777         if (!bd) {
7778                 *ptr = 0xff;
7779                 return string;
7780         }
7781
7782         for (p = bd->bd_config; p; p = p->next) {
7783
7784                 switch (p->type) {
7785                 case LNODE:
7786                         *ptr = '\0';
7787                         ptr++;
7788                         *ptr = p->u.line.speed;
7789                         ptr++;
7790                         break;
7791                 case CNODE:
7792                         /*
7793                          * Because the EPC/con concentrators can have EM modules
7794                          * hanging off of them, we have to walk ahead in the list
7795                          * and keep adding the number of ports on each EM to the config.
7796                          * UGH!
7797                          */
7798                         speed = p->u.conc.speed;
7799                         q = p->next;
7800                         if ((q != NULL) && (q->type == MNODE)) {
7801                                 *ptr = (p->u.conc.nport + 0x80);
7802                                 ptr++;
7803                                 p = q;
7804                                 while ((q->next != NULL) && (q->next->type) == MNODE) {
7805                                         *ptr = (q->u.module.nport + 0x80);
7806                                         ptr++;
7807                                         p = q;
7808                                         q = q->next;
7809                                 }
7810                                 *ptr = q->u.module.nport;
7811                                 ptr++;
7812                         } else {
7813                                 *ptr = p->u.conc.nport;
7814                                 ptr++;
7815                         }
7816
7817                         *ptr = speed;
7818                         ptr++;
7819                         break;
7820                 }
7821         }
7822
7823         *ptr = 0xff;
7824         return string;
7825 }