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