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