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