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