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