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