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