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