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