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