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