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