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