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