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