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