xfs: XFS_IS_REALTIME_INODE() should be false if no rt device present
[linux/fpc-iii.git] / drivers / staging / dgap / dgap.c
blobbad355100825d569834065a8b6e41aa3d0a2a44b
1 /*
2 * Copyright 2003 Digi International (www.digi.com)
3 * Scott H Kilau <Scott_Kilau at digi dot com>
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.
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.
18 * In the original out of kernel Digi dgap driver, firmware
19 * loading was done via user land to driver handshaking.
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.
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.
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.
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.
42 #undef DIGI_CONCENTRATORS_SUPPORTED
44 #define pr_fmt(fmt) "dgap: " fmt
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>
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] */
61 #include <linux/string.h>
62 #include <linux/device.h>
63 #include <linux/kdev_t.h>
64 #include <linux/firmware.h>
66 #include "dgap.h"
69 * File operations permitted on Control/Management major.
71 static const struct file_operations dgap_board_fops = {
72 .owner = THIS_MODULE,
75 static uint dgap_numboards;
76 static struct board_t *dgap_board[MAXBOARDS];
77 static ulong dgap_poll_counter;
78 static int dgap_driver_state = DRIVER_INITIALIZED;
79 static int dgap_poll_tick = 20; /* Poll interval - 20 ms */
81 static struct class *dgap_class;
83 static uint dgap_count = 500;
86 * Poller stuff
88 static DEFINE_SPINLOCK(dgap_poll_lock); /* Poll scheduling lock */
89 static ulong dgap_poll_time; /* Time of next poll */
90 static uint dgap_poll_stop; /* Used to tell poller to stop */
91 static struct timer_list dgap_poll_timer;
94 SUPPORTED PRODUCTS
96 Card Model Number of Ports Interface
97 ----------------------------------------------------------------
98 Acceleport Xem 4 - 64 (EIA232 & EIA422)
99 Acceleport Xr 4 & 8 (EIA232)
100 Acceleport Xr 920 4 & 8 (EIA232)
101 Acceleport C/X 8 - 128 (EIA232)
102 Acceleport EPC/X 8 - 224 (EIA232)
103 Acceleport Xr/422 4 & 8 (EIA422)
104 Acceleport 2r/920 2 (EIA232)
105 Acceleport 4r/920 4 (EIA232)
106 Acceleport 8r/920 8 (EIA232)
108 IBM 8-Port Asynchronous PCI Adapter (EIA232)
109 IBM 128-Port Asynchronous PCI Adapter (EIA232 & EIA422)
112 static struct pci_device_id dgap_pci_tbl[] = {
113 { DIGI_VID, PCI_DEV_XEM_DID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
114 { DIGI_VID, PCI_DEV_CX_DID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 1 },
115 { DIGI_VID, PCI_DEV_CX_IBM_DID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 2 },
116 { DIGI_VID, PCI_DEV_EPCJ_DID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 3 },
117 { DIGI_VID, PCI_DEV_920_2_DID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 4 },
118 { DIGI_VID, PCI_DEV_920_4_DID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 5 },
119 { DIGI_VID, PCI_DEV_920_8_DID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 6 },
120 { DIGI_VID, PCI_DEV_XR_DID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 7 },
121 { DIGI_VID, PCI_DEV_XRJ_DID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 8 },
122 { DIGI_VID, PCI_DEV_XR_422_DID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 9 },
123 { DIGI_VID, PCI_DEV_XR_IBM_DID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 10 },
124 { DIGI_VID, PCI_DEV_XR_SAIP_DID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 11 },
125 { DIGI_VID, PCI_DEV_XR_BULL_DID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 12 },
126 { DIGI_VID, PCI_DEV_920_8_HP_DID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 13 },
127 { DIGI_VID, PCI_DEV_XEM_HP_DID, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 14 },
128 {0,} /* 0 terminated list. */
130 MODULE_DEVICE_TABLE(pci, dgap_pci_tbl);
133 * A generic list of Product names, PCI Vendor ID, and PCI Device ID.
135 struct board_id {
136 uint config_type;
137 u8 *name;
138 uint maxports;
139 uint dpatype;
142 static struct board_id dgap_ids[] = {
143 {PPCM, PCI_DEV_XEM_NAME, 64, (T_PCXM | T_PCLITE | T_PCIBUS)},
144 {PCX, PCI_DEV_CX_NAME, 128, (T_CX | T_PCIBUS) },
145 {PCX, PCI_DEV_CX_IBM_NAME, 128, (T_CX | T_PCIBUS) },
146 {PEPC, PCI_DEV_EPCJ_NAME, 224, (T_EPC | T_PCIBUS) },
147 {APORT2_920P, PCI_DEV_920_2_NAME, 2, (T_PCXR | T_PCLITE | T_PCIBUS)},
148 {APORT4_920P, PCI_DEV_920_4_NAME, 4, (T_PCXR | T_PCLITE | T_PCIBUS)},
149 {APORT8_920P, PCI_DEV_920_8_NAME, 8, (T_PCXR | T_PCLITE | T_PCIBUS)},
150 {PAPORT8, PCI_DEV_XR_NAME, 8, (T_PCXR | T_PCLITE | T_PCIBUS)},
151 {PAPORT8, PCI_DEV_XRJ_NAME, 8, (T_PCXR | T_PCLITE | T_PCIBUS)},
152 {PAPORT8, PCI_DEV_XR_422_NAME, 8, (T_PCXR | T_PCLITE | T_PCIBUS)},
153 {PAPORT8, PCI_DEV_XR_IBM_NAME, 8, (T_PCXR | T_PCLITE | T_PCIBUS)},
154 {PAPORT8, PCI_DEV_XR_SAIP_NAME, 8, (T_PCXR | T_PCLITE | T_PCIBUS)},
155 {PAPORT8, PCI_DEV_XR_BULL_NAME, 8, (T_PCXR | T_PCLITE | T_PCIBUS)},
156 {APORT8_920P, PCI_DEV_920_8_HP_NAME, 8, (T_PCXR | T_PCLITE | T_PCIBUS)},
157 {PPCM, PCI_DEV_XEM_HP_NAME, 64, (T_PCXM | T_PCLITE | T_PCIBUS)},
158 {0,} /* 0 terminated list. */
161 struct firmware_info {
162 u8 *conf_name; /* dgap.conf */
163 u8 *bios_name; /* BIOS filename */
164 u8 *fep_name; /* FEP filename */
165 u8 *con_name; /* Concentrator filename FIXME*/
166 int num; /* sequence number */
170 * Firmware - BIOS, FEP, and CONC filenames
172 static struct firmware_info fw_info[] = {
173 { "dgap/dgap.conf", "dgap/sxbios.bin", "dgap/sxfep.bin", NULL, 0 },
174 { "dgap/dgap.conf", "dgap/cxpbios.bin", "dgap/cxpfep.bin", NULL, 1 },
175 { "dgap/dgap.conf", "dgap/cxpbios.bin", "dgap/cxpfep.bin", NULL, 2 },
176 { "dgap/dgap.conf", "dgap/pcibios.bin", "dgap/pcifep.bin", NULL, 3 },
177 { "dgap/dgap.conf", "dgap/xrbios.bin", "dgap/xrfep.bin", NULL, 4 },
178 { "dgap/dgap.conf", "dgap/xrbios.bin", "dgap/xrfep.bin", NULL, 5 },
179 { "dgap/dgap.conf", "dgap/xrbios.bin", "dgap/xrfep.bin", NULL, 6 },
180 { "dgap/dgap.conf", "dgap/xrbios.bin", "dgap/xrfep.bin", NULL, 7 },
181 { "dgap/dgap.conf", "dgap/xrbios.bin", "dgap/xrfep.bin", NULL, 8 },
182 { "dgap/dgap.conf", "dgap/xrbios.bin", "dgap/xrfep.bin", NULL, 9 },
183 { "dgap/dgap.conf", "dgap/xrbios.bin", "dgap/xrfep.bin", NULL, 10 },
184 { "dgap/dgap.conf", "dgap/xrbios.bin", "dgap/xrfep.bin", NULL, 11 },
185 { "dgap/dgap.conf", "dgap/xrbios.bin", "dgap/xrfep.bin", NULL, 12 },
186 { "dgap/dgap.conf", "dgap/xrbios.bin", "dgap/xrfep.bin", NULL, 13 },
187 { "dgap/dgap.conf", "dgap/sxbios.bin", "dgap/sxfep.bin", NULL, 14 },
188 {NULL,}
192 * Default transparent print information.
194 static struct digi_t dgap_digi_init = {
195 .digi_flags = DIGI_COOK, /* Flags */
196 .digi_maxcps = 100, /* Max CPS */
197 .digi_maxchar = 50, /* Max chars in print queue */
198 .digi_bufsize = 100, /* Printer buffer size */
199 .digi_onlen = 4, /* size of printer on string */
200 .digi_offlen = 4, /* size of printer off string */
201 .digi_onstr = "\033[5i", /* ANSI printer on string ] */
202 .digi_offstr = "\033[4i", /* ANSI printer off string ] */
203 .digi_term = "ansi" /* default terminal type */
207 * Define a local default termios struct. All ports will be created
208 * with this termios initially.
210 * This defines a raw port at 9600 baud, 8 data bits, no parity,
211 * 1 stop bit.
214 static struct ktermios dgap_default_termios = {
215 .c_iflag = (DEFAULT_IFLAGS), /* iflags */
216 .c_oflag = (DEFAULT_OFLAGS), /* oflags */
217 .c_cflag = (DEFAULT_CFLAGS), /* cflags */
218 .c_lflag = (DEFAULT_LFLAGS), /* lflags */
219 .c_cc = INIT_C_CC,
220 .c_line = 0,
224 * Our needed internal static variables from dgap_parse.c
226 static struct cnode dgap_head;
227 #define MAXCWORD 200
228 static char dgap_cword[MAXCWORD];
230 struct toklist {
231 int token;
232 char *string;
235 static struct toklist dgap_brdtype[] = {
236 { PCX, "Digi_AccelePort_C/X_PCI" },
237 { PEPC, "Digi_AccelePort_EPC/X_PCI" },
238 { PPCM, "Digi_AccelePort_Xem_PCI" },
239 { APORT2_920P, "Digi_AccelePort_2r_920_PCI" },
240 { APORT4_920P, "Digi_AccelePort_4r_920_PCI" },
241 { APORT8_920P, "Digi_AccelePort_8r_920_PCI" },
242 { PAPORT4, "Digi_AccelePort_4r_PCI(EIA-232/RS-422)" },
243 { PAPORT8, "Digi_AccelePort_8r_PCI(EIA-232/RS-422)" },
244 { 0, NULL }
247 static struct toklist dgap_tlist[] = {
248 { BEGIN, "config_begin" },
249 { END, "config_end" },
250 { BOARD, "board" },
251 { PCIINFO, "pciinfo" },
252 { LINE, "line" },
253 { CONC, "conc" },
254 { CONC, "concentrator" },
255 { CX, "cx" },
256 { CX, "ccon" },
257 { EPC, "epccon" },
258 { EPC, "epc" },
259 { MOD, "module" },
260 { ID, "id" },
261 { STARTO, "start" },
262 { SPEED, "speed" },
263 { CABLE, "cable" },
264 { CONNECT, "connect" },
265 { METHOD, "method" },
266 { STATUS, "status" },
267 { CUSTOM, "Custom" },
268 { BASIC, "Basic" },
269 { MEM, "mem" },
270 { MEM, "memory" },
271 { PORTS, "ports" },
272 { MODEM, "modem" },
273 { NPORTS, "nports" },
274 { TTYN, "ttyname" },
275 { CU, "cuname" },
276 { PRINT, "prname" },
277 { CMAJOR, "major" },
278 { ALTPIN, "altpin" },
279 { USEINTR, "useintr" },
280 { TTSIZ, "ttysize" },
281 { CHSIZ, "chsize" },
282 { BSSIZ, "boardsize" },
283 { UNTSIZ, "schedsize" },
284 { F2SIZ, "f2200size" },
285 { VPSIZ, "vpixsize" },
286 { 0, NULL }
290 * get a word from the input stream, also keep track of current line number.
291 * words are separated by whitespace.
293 static char *dgap_getword(char **in)
295 char *ret_ptr = *in;
297 char *ptr = strpbrk(*in, " \t\n");
299 /* If no word found, return null */
300 if (!ptr)
301 return NULL;
303 /* Mark new location for our buffer */
304 *ptr = '\0';
305 *in = ptr + 1;
307 /* Eat any extra spaces/tabs/newlines that might be present */
308 while (*in && **in && ((**in == ' ') ||
309 (**in == '\t') ||
310 (**in == '\n'))) {
311 **in = '\0';
312 *in = *in + 1;
315 return ret_ptr;
320 * Get a token from the input file; return 0 if end of file is reached
322 static int dgap_gettok(char **in)
324 char *w;
325 struct toklist *t;
327 if (strstr(dgap_cword, "board")) {
328 w = dgap_getword(in);
329 if (!w)
330 return 0;
331 snprintf(dgap_cword, MAXCWORD, "%s", w);
332 for (t = dgap_brdtype; t->token != 0; t++) {
333 if (!strcmp(w, t->string))
334 return t->token;
336 } else {
337 while ((w = dgap_getword(in))) {
338 snprintf(dgap_cword, MAXCWORD, "%s", w);
339 for (t = dgap_tlist; t->token != 0; t++) {
340 if (!strcmp(w, t->string))
341 return t->token;
346 return 0;
350 * dgap_checknode: see if all the necessary info has been supplied for a node
351 * before creating the next node.
353 static int dgap_checknode(struct cnode *p)
355 switch (p->type) {
356 case LNODE:
357 if (p->u.line.v_speed == 0) {
358 pr_err("line speed not specified");
359 return 1;
361 return 0;
363 case CNODE:
364 if (p->u.conc.v_speed == 0) {
365 pr_err("concentrator line speed not specified");
366 return 1;
368 if (p->u.conc.v_nport == 0) {
369 pr_err("number of ports on concentrator not specified");
370 return 1;
372 if (p->u.conc.v_id == 0) {
373 pr_err("concentrator id letter not specified");
374 return 1;
376 return 0;
378 case MNODE:
379 if (p->u.module.v_nport == 0) {
380 pr_err("number of ports on EBI module not specified");
381 return 1;
383 if (p->u.module.v_id == 0) {
384 pr_err("EBI module id letter not specified");
385 return 1;
387 return 0;
389 return 0;
393 * Given a board pointer, returns whether we should use interrupts or not.
395 static uint dgap_config_get_useintr(struct board_t *bd)
397 struct cnode *p;
399 if (!bd)
400 return 0;
402 for (p = bd->bd_config; p; p = p->next) {
403 if (p->type == INTRNODE) {
405 * check for pcxr types.
407 return p->u.useintr;
411 /* If not found, then don't turn on interrupts. */
412 return 0;
416 * Given a board pointer, returns whether we turn on altpin or not.
418 static uint dgap_config_get_altpin(struct board_t *bd)
420 struct cnode *p;
422 if (!bd)
423 return 0;
425 for (p = bd->bd_config; p; p = p->next) {
426 if (p->type == ANODE) {
428 * check for pcxr types.
430 return p->u.altpin;
434 /* If not found, then don't turn on interrupts. */
435 return 0;
439 * Given a specific type of board, if found, detached link and
440 * returns the first occurrence in the list.
442 static struct cnode *dgap_find_config(int type, int bus, int slot)
444 struct cnode *p, *prev, *prev2, *found;
446 p = &dgap_head;
448 while (p->next) {
449 prev = p;
450 p = p->next;
452 if (p->type != BNODE)
453 continue;
455 if (p->u.board.type != type)
456 continue;
458 if (p->u.board.v_pcibus &&
459 p->u.board.pcibus != bus)
460 continue;
462 if (p->u.board.v_pcislot &&
463 p->u.board.pcislot != slot)
464 continue;
466 found = p;
468 * Keep walking thru the list till we
469 * find the next board.
471 while (p->next) {
472 prev2 = p;
473 p = p->next;
475 if (p->type != BNODE)
476 continue;
479 * Mark the end of our 1 board
480 * chain of configs.
482 prev2->next = NULL;
485 * Link the "next" board to the
486 * previous board, effectively
487 * "unlinking" our board from
488 * the main config.
490 prev->next = p;
492 return found;
495 * It must be the last board in the list.
497 prev->next = NULL;
498 return found;
500 return NULL;
504 * Given a board pointer, walks the config link, counting up
505 * all ports user specified should be on the board.
506 * (This does NOT mean they are all actually present right now tho)
508 static uint dgap_config_get_num_prts(struct board_t *bd)
510 int count = 0;
511 struct cnode *p;
513 if (!bd)
514 return 0;
516 for (p = bd->bd_config; p; p = p->next) {
517 switch (p->type) {
518 case BNODE:
520 * check for pcxr types.
522 if (p->u.board.type > EPCFE)
523 count += p->u.board.nport;
524 break;
525 case CNODE:
526 count += p->u.conc.nport;
527 break;
528 case MNODE:
529 count += p->u.module.nport;
530 break;
533 return count;
536 static char *dgap_create_config_string(struct board_t *bd, char *string)
538 char *ptr = string;
539 struct cnode *p;
540 struct cnode *q;
541 int speed;
543 if (!bd) {
544 *ptr = 0xff;
545 return string;
548 for (p = bd->bd_config; p; p = p->next) {
549 switch (p->type) {
550 case LNODE:
551 *ptr = '\0';
552 ptr++;
553 *ptr = p->u.line.speed;
554 ptr++;
555 break;
556 case CNODE:
558 * Because the EPC/con concentrators can have EM modules
559 * hanging off of them, we have to walk ahead in the
560 * list and keep adding the number of ports on each EM
561 * to the config. UGH!
563 speed = p->u.conc.speed;
564 q = p->next;
565 if (q && (q->type == MNODE)) {
566 *ptr = (p->u.conc.nport + 0x80);
567 ptr++;
568 p = q;
569 while (q->next && (q->next->type) == MNODE) {
570 *ptr = (q->u.module.nport + 0x80);
571 ptr++;
572 p = q;
573 q = q->next;
575 *ptr = q->u.module.nport;
576 ptr++;
577 } else {
578 *ptr = p->u.conc.nport;
579 ptr++;
582 *ptr = speed;
583 ptr++;
584 break;
588 *ptr = 0xff;
589 return string;
593 * Parse a configuration file read into memory as a string.
595 static int dgap_parsefile(char **in)
597 struct cnode *p, *brd, *line, *conc;
598 int rc;
599 char *s;
600 int linecnt = 0;
602 p = &dgap_head;
603 brd = line = conc = NULL;
605 /* perhaps we are adding to an existing list? */
606 while (p->next)
607 p = p->next;
609 /* file must start with a BEGIN */
610 while ((rc = dgap_gettok(in)) != BEGIN) {
611 if (rc == 0) {
612 pr_err("unexpected EOF");
613 return -1;
617 for (; ;) {
618 int board_type = 0;
619 int conc_type = 0;
620 int module_type = 0;
622 rc = dgap_gettok(in);
623 if (rc == 0) {
624 pr_err("unexpected EOF");
625 return -1;
628 switch (rc) {
629 case BEGIN: /* should only be 1 begin */
630 pr_err("unexpected config_begin\n");
631 return -1;
633 case END:
634 return 0;
636 case BOARD: /* board info */
637 if (dgap_checknode(p))
638 return -1;
640 p->next = kzalloc(sizeof(struct cnode), GFP_KERNEL);
641 if (!p->next)
642 return -ENOMEM;
644 p = p->next;
646 p->type = BNODE;
647 p->u.board.status = kstrdup("No", GFP_KERNEL);
648 line = conc = NULL;
649 brd = p;
650 linecnt = -1;
652 board_type = dgap_gettok(in);
653 if (board_type == 0) {
654 pr_err("board !!type not specified");
655 return -1;
658 p->u.board.type = board_type;
660 break;
662 case MEM: /* memory address */
663 if (p->type != BNODE) {
664 pr_err("memory address only valid for boards");
665 return -1;
667 s = dgap_getword(in);
668 if (!s) {
669 pr_err("unexpected end of file");
670 return -1;
672 kfree(p->u.board.addrstr);
673 p->u.board.addrstr = kstrdup(s, GFP_KERNEL);
674 if (kstrtoul(s, 0, &p->u.board.addr)) {
675 pr_err("bad number for memory address");
676 return -1;
678 p->u.board.v_addr = 1;
679 break;
681 case PCIINFO: /* pci information */
682 if (p->type != BNODE) {
683 pr_err("memory address only valid for boards");
684 return -1;
686 s = dgap_getword(in);
687 if (!s) {
688 pr_err("unexpected end of file");
689 return -1;
691 kfree(p->u.board.pcibusstr);
692 p->u.board.pcibusstr = kstrdup(s, GFP_KERNEL);
693 if (kstrtoul(s, 0, &p->u.board.pcibus)) {
694 pr_err("bad number for pci bus");
695 return -1;
697 p->u.board.v_pcibus = 1;
698 s = dgap_getword(in);
699 if (!s) {
700 pr_err("unexpected end of file");
701 return -1;
703 kfree(p->u.board.pcislotstr);
704 p->u.board.pcislotstr = kstrdup(s, GFP_KERNEL);
705 if (kstrtoul(s, 0, &p->u.board.pcislot)) {
706 pr_err("bad number for pci slot");
707 return -1;
709 p->u.board.v_pcislot = 1;
710 break;
712 case METHOD:
713 if (p->type != BNODE) {
714 pr_err("install method only valid for boards");
715 return -1;
717 s = dgap_getword(in);
718 if (!s) {
719 pr_err("unexpected end of file");
720 return -1;
722 kfree(p->u.board.method);
723 p->u.board.method = kstrdup(s, GFP_KERNEL);
724 p->u.board.v_method = 1;
725 break;
727 case STATUS:
728 if (p->type != BNODE) {
729 pr_err("config status only valid for boards");
730 return -1;
732 s = dgap_getword(in);
733 if (!s) {
734 pr_err("unexpected end of file");
735 return -1;
737 kfree(p->u.board.status);
738 p->u.board.status = kstrdup(s, GFP_KERNEL);
739 break;
741 case NPORTS: /* number of ports */
742 if (p->type == BNODE) {
743 s = dgap_getword(in);
744 if (!s) {
745 pr_err("unexpected end of file");
746 return -1;
748 if (kstrtol(s, 0, &p->u.board.nport)) {
749 pr_err("bad number for number of ports");
750 return -1;
752 p->u.board.v_nport = 1;
753 } else if (p->type == CNODE) {
754 s = dgap_getword(in);
755 if (!s) {
756 pr_err("unexpected end of file");
757 return -1;
759 if (kstrtol(s, 0, &p->u.conc.nport)) {
760 pr_err("bad number for number of ports");
761 return -1;
763 p->u.conc.v_nport = 1;
764 } else if (p->type == MNODE) {
765 s = dgap_getword(in);
766 if (!s) {
767 pr_err("unexpected end of file");
768 return -1;
770 if (kstrtol(s, 0, &p->u.module.nport)) {
771 pr_err("bad number for number of ports");
772 return -1;
774 p->u.module.v_nport = 1;
775 } else {
776 pr_err("nports only valid for concentrators or modules");
777 return -1;
779 break;
781 case ID: /* letter ID used in tty name */
782 s = dgap_getword(in);
783 if (!s) {
784 pr_err("unexpected end of file");
785 return -1;
787 kfree(p->u.board.status);
788 p->u.board.status = kstrdup(s, GFP_KERNEL);
790 if (p->type == CNODE) {
791 kfree(p->u.conc.id);
792 p->u.conc.id = kstrdup(s, GFP_KERNEL);
793 p->u.conc.v_id = 1;
794 } else if (p->type == MNODE) {
795 kfree(p->u.module.id);
796 p->u.module.id = kstrdup(s, GFP_KERNEL);
797 p->u.module.v_id = 1;
798 } else {
799 pr_err("id only valid for concentrators or modules");
800 return -1;
802 break;
804 case STARTO: /* start offset of ID */
805 if (p->type == BNODE) {
806 s = dgap_getword(in);
807 if (!s) {
808 pr_err("unexpected end of file");
809 return -1;
811 if (kstrtol(s, 0, &p->u.board.start)) {
812 pr_err("bad number for start of tty count");
813 return -1;
815 p->u.board.v_start = 1;
816 } else if (p->type == CNODE) {
817 s = dgap_getword(in);
818 if (!s) {
819 pr_err("unexpected end of file");
820 return -1;
822 if (kstrtol(s, 0, &p->u.conc.start)) {
823 pr_err("bad number for start of tty count");
824 return -1;
826 p->u.conc.v_start = 1;
827 } else if (p->type == MNODE) {
828 s = dgap_getword(in);
829 if (!s) {
830 pr_err("unexpected end of file");
831 return -1;
833 if (kstrtol(s, 0, &p->u.module.start)) {
834 pr_err("bad number for start of tty count");
835 return -1;
837 p->u.module.v_start = 1;
838 } else {
839 pr_err("start only valid for concentrators or modules");
840 return -1;
842 break;
844 case TTYN: /* tty name prefix */
845 if (dgap_checknode(p))
846 return -1;
848 p->next = kzalloc(sizeof(struct cnode), GFP_KERNEL);
849 if (!p->next)
850 return -ENOMEM;
852 p = p->next;
853 p->type = TNODE;
855 s = dgap_getword(in);
856 if (!s) {
857 pr_err("unexpeced end of file");
858 return -1;
860 p->u.ttyname = kstrdup(s, GFP_KERNEL);
861 if (!p->u.ttyname)
862 return -1;
864 break;
866 case CU: /* cu name prefix */
867 if (dgap_checknode(p))
868 return -1;
870 p->next = kzalloc(sizeof(struct cnode), GFP_KERNEL);
871 if (!p->next)
872 return -ENOMEM;
874 p = p->next;
875 p->type = CUNODE;
877 s = dgap_getword(in);
878 if (!s) {
879 pr_err("unexpeced end of file");
880 return -1;
882 p->u.cuname = kstrdup(s, GFP_KERNEL);
883 if (!p->u.cuname)
884 return -1;
886 break;
888 case LINE: /* line information */
889 if (dgap_checknode(p))
890 return -1;
891 if (!brd) {
892 pr_err("must specify board before line info");
893 return -1;
895 switch (brd->u.board.type) {
896 case PPCM:
897 pr_err("line not valid for PC/em");
898 return -1;
901 p->next = kzalloc(sizeof(struct cnode), GFP_KERNEL);
902 if (!p->next)
903 return -ENOMEM;
905 p = p->next;
906 p->type = LNODE;
907 conc = NULL;
908 line = p;
909 linecnt++;
910 break;
912 case CONC: /* concentrator information */
913 if (dgap_checknode(p))
914 return -1;
915 if (!line) {
916 pr_err("must specify line info before concentrator");
917 return -1;
920 p->next = kzalloc(sizeof(struct cnode), GFP_KERNEL);
921 if (!p->next)
922 return -ENOMEM;
924 p = p->next;
925 p->type = CNODE;
926 conc = p;
928 if (linecnt)
929 brd->u.board.conc2++;
930 else
931 brd->u.board.conc1++;
933 conc_type = dgap_gettok(in);
934 if (conc_type == 0 ||
935 (conc_type != CX && conc_type != EPC)) {
936 pr_err("failed to set a type of concentratros");
937 return -1;
940 p->u.conc.type = conc_type;
942 break;
944 case MOD: /* EBI module */
945 if (dgap_checknode(p))
946 return -1;
947 if (!brd) {
948 pr_err("must specify board info before EBI modules");
949 return -1;
951 switch (brd->u.board.type) {
952 case PPCM:
953 linecnt = 0;
954 break;
955 default:
956 if (!conc) {
957 pr_err("must specify concentrator info before EBI module");
958 return -1;
962 p->next = kzalloc(sizeof(struct cnode), GFP_KERNEL);
963 if (!p->next)
964 return -ENOMEM;
966 p = p->next;
967 p->type = MNODE;
969 if (linecnt)
970 brd->u.board.module2++;
971 else
972 brd->u.board.module1++;
974 module_type = dgap_gettok(in);
975 if (module_type == 0 ||
976 (module_type != PORTS && module_type != MODEM)) {
977 pr_err("failed to set a type of module");
978 return -1;
981 p->u.module.type = module_type;
983 break;
985 case CABLE:
986 if (p->type == LNODE) {
987 s = dgap_getword(in);
988 if (!s) {
989 pr_err("unexpected end of file");
990 return -1;
992 kfree(p->u.line.cable);
993 p->u.line.cable = kstrdup(s, GFP_KERNEL);
994 p->u.line.v_cable = 1;
996 break;
998 case SPEED: /* sync line speed indication */
999 if (p->type == LNODE) {
1000 s = dgap_getword(in);
1001 if (!s) {
1002 pr_err("unexpected end of file");
1003 return -1;
1005 if (kstrtol(s, 0, &p->u.line.speed)) {
1006 pr_err("bad number for line speed");
1007 return -1;
1009 p->u.line.v_speed = 1;
1010 } else if (p->type == CNODE) {
1011 s = dgap_getword(in);
1012 if (!s) {
1013 pr_err("unexpected end of file");
1014 return -1;
1016 if (kstrtol(s, 0, &p->u.conc.speed)) {
1017 pr_err("bad number for line speed");
1018 return -1;
1020 p->u.conc.v_speed = 1;
1021 } else {
1022 pr_err("speed valid only for lines or concentrators.");
1023 return -1;
1025 break;
1027 case CONNECT:
1028 if (p->type == CNODE) {
1029 s = dgap_getword(in);
1030 if (!s) {
1031 pr_err("unexpected end of file");
1032 return -1;
1034 kfree(p->u.conc.connect);
1035 p->u.conc.connect = kstrdup(s, GFP_KERNEL);
1036 p->u.conc.v_connect = 1;
1038 break;
1039 case PRINT: /* transparent print name prefix */
1040 if (dgap_checknode(p))
1041 return -1;
1043 p->next = kzalloc(sizeof(struct cnode), GFP_KERNEL);
1044 if (!p->next)
1045 return -ENOMEM;
1047 p = p->next;
1048 p->type = PNODE;
1050 s = dgap_getword(in);
1051 if (!s) {
1052 pr_err("unexpeced end of file");
1053 return -1;
1055 p->u.printname = kstrdup(s, GFP_KERNEL);
1056 if (!p->u.printname)
1057 return -1;
1059 break;
1061 case CMAJOR: /* major number */
1062 if (dgap_checknode(p))
1063 return -1;
1065 p->next = kzalloc(sizeof(struct cnode), GFP_KERNEL);
1066 if (!p->next)
1067 return -ENOMEM;
1069 p = p->next;
1070 p->type = JNODE;
1072 s = dgap_getword(in);
1073 if (!s) {
1074 pr_err("unexpected end of file");
1075 return -1;
1077 if (kstrtol(s, 0, &p->u.majornumber)) {
1078 pr_err("bad number for major number");
1079 return -1;
1081 break;
1083 case ALTPIN: /* altpin setting */
1084 if (dgap_checknode(p))
1085 return -1;
1087 p->next = kzalloc(sizeof(struct cnode), GFP_KERNEL);
1088 if (!p->next)
1089 return -ENOMEM;
1091 p = p->next;
1092 p->type = ANODE;
1094 s = dgap_getword(in);
1095 if (!s) {
1096 pr_err("unexpected end of file");
1097 return -1;
1099 if (kstrtol(s, 0, &p->u.altpin)) {
1100 pr_err("bad number for altpin");
1101 return -1;
1103 break;
1105 case USEINTR: /* enable interrupt setting */
1106 if (dgap_checknode(p))
1107 return -1;
1109 p->next = kzalloc(sizeof(struct cnode), GFP_KERNEL);
1110 if (!p->next)
1111 return -ENOMEM;
1113 p = p->next;
1114 p->type = INTRNODE;
1115 s = dgap_getword(in);
1116 if (!s) {
1117 pr_err("unexpected end of file");
1118 return -1;
1120 if (kstrtol(s, 0, &p->u.useintr)) {
1121 pr_err("bad number for useintr");
1122 return -1;
1124 break;
1126 case TTSIZ: /* size of tty structure */
1127 if (dgap_checknode(p))
1128 return -1;
1130 p->next = kzalloc(sizeof(struct cnode), GFP_KERNEL);
1131 if (!p->next)
1132 return -ENOMEM;
1134 p = p->next;
1135 p->type = TSNODE;
1137 s = dgap_getword(in);
1138 if (!s) {
1139 pr_err("unexpected end of file");
1140 return -1;
1142 if (kstrtol(s, 0, &p->u.ttysize)) {
1143 pr_err("bad number for ttysize");
1144 return -1;
1146 break;
1148 case CHSIZ: /* channel structure size */
1149 if (dgap_checknode(p))
1150 return -1;
1152 p->next = kzalloc(sizeof(struct cnode), GFP_KERNEL);
1153 if (!p->next)
1154 return -ENOMEM;
1156 p = p->next;
1157 p->type = CSNODE;
1159 s = dgap_getword(in);
1160 if (!s) {
1161 pr_err("unexpected end of file");
1162 return -1;
1164 if (kstrtol(s, 0, &p->u.chsize)) {
1165 pr_err("bad number for chsize");
1166 return -1;
1168 break;
1170 case BSSIZ: /* board structure size */
1171 if (dgap_checknode(p))
1172 return -1;
1174 p->next = kzalloc(sizeof(struct cnode), GFP_KERNEL);
1175 if (!p->next)
1176 return -ENOMEM;
1178 p = p->next;
1179 p->type = BSNODE;
1181 s = dgap_getword(in);
1182 if (!s) {
1183 pr_err("unexpected end of file");
1184 return -1;
1186 if (kstrtol(s, 0, &p->u.bssize)) {
1187 pr_err("bad number for bssize");
1188 return -1;
1190 break;
1192 case UNTSIZ: /* sched structure size */
1193 if (dgap_checknode(p))
1194 return -1;
1196 p->next = kzalloc(sizeof(struct cnode), GFP_KERNEL);
1197 if (!p->next)
1198 return -ENOMEM;
1200 p = p->next;
1201 p->type = USNODE;
1203 s = dgap_getword(in);
1204 if (!s) {
1205 pr_err("unexpected end of file");
1206 return -1;
1208 if (kstrtol(s, 0, &p->u.unsize)) {
1209 pr_err("bad number for schedsize");
1210 return -1;
1212 break;
1214 case F2SIZ: /* f2200 structure size */
1215 if (dgap_checknode(p))
1216 return -1;
1218 p->next = kzalloc(sizeof(struct cnode), GFP_KERNEL);
1219 if (!p->next)
1220 return -ENOMEM;
1222 p = p->next;
1223 p->type = FSNODE;
1225 s = dgap_getword(in);
1226 if (!s) {
1227 pr_err("unexpected end of file");
1228 return -1;
1230 if (kstrtol(s, 0, &p->u.f2size)) {
1231 pr_err("bad number for f2200size");
1232 return -1;
1234 break;
1236 case VPSIZ: /* vpix structure size */
1237 if (dgap_checknode(p))
1238 return -1;
1240 p->next = kzalloc(sizeof(struct cnode), GFP_KERNEL);
1241 if (!p->next)
1242 return -ENOMEM;
1244 p = p->next;
1245 p->type = VSNODE;
1247 s = dgap_getword(in);
1248 if (!s) {
1249 pr_err("unexpected end of file");
1250 return -1;
1252 if (kstrtol(s, 0, &p->u.vpixsize)) {
1253 pr_err("bad number for vpixsize");
1254 return -1;
1256 break;
1261 static void dgap_cleanup_nodes(void)
1263 struct cnode *p;
1265 p = &dgap_head;
1267 while (p) {
1268 struct cnode *tmp = p->next;
1270 if (p->type == NULLNODE) {
1271 p = tmp;
1272 continue;
1275 switch (p->type) {
1276 case BNODE:
1277 kfree(p->u.board.addrstr);
1278 kfree(p->u.board.pcibusstr);
1279 kfree(p->u.board.pcislotstr);
1280 kfree(p->u.board.method);
1281 break;
1282 case CNODE:
1283 kfree(p->u.conc.id);
1284 kfree(p->u.conc.connect);
1285 break;
1286 case MNODE:
1287 kfree(p->u.module.id);
1288 break;
1289 case TNODE:
1290 kfree(p->u.ttyname);
1291 break;
1292 case CUNODE:
1293 kfree(p->u.cuname);
1294 break;
1295 case LNODE:
1296 kfree(p->u.line.cable);
1297 break;
1298 case PNODE:
1299 kfree(p->u.printname);
1300 break;
1303 kfree(p->u.board.status);
1304 kfree(p);
1305 p = tmp;
1310 * Retrives the current custom baud rate from FEP memory,
1311 * and returns it back to the user.
1312 * Returns 0 on error.
1314 static uint dgap_get_custom_baud(struct channel_t *ch)
1316 u8 __iomem *vaddr;
1317 ulong offset;
1319 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
1320 return 0;
1322 if (!ch->ch_bd || ch->ch_bd->magic != DGAP_BOARD_MAGIC)
1323 return 0;
1325 if (!(ch->ch_bd->bd_flags & BD_FEP5PLUS))
1326 return 0;
1328 vaddr = ch->ch_bd->re_map_membase;
1330 if (!vaddr)
1331 return 0;
1334 * Go get from fep mem, what the fep
1335 * believes the custom baud rate is.
1337 offset = (ioread16(vaddr + ECS_SEG) << 4) + (ch->ch_portnum * 0x28)
1338 + LINE_SPEED;
1340 return readw(vaddr + offset);
1344 * Remap PCI memory.
1346 static int dgap_remap(struct board_t *brd)
1348 if (!brd || brd->magic != DGAP_BOARD_MAGIC)
1349 return -EIO;
1351 if (!request_mem_region(brd->membase, 0x200000, "dgap"))
1352 return -ENOMEM;
1354 if (!request_mem_region(brd->membase + PCI_IO_OFFSET, 0x200000, "dgap"))
1355 goto err_req_mem;
1357 brd->re_map_membase = ioremap(brd->membase, 0x200000);
1358 if (!brd->re_map_membase)
1359 goto err_remap_mem;
1361 brd->re_map_port = ioremap((brd->membase + PCI_IO_OFFSET), 0x200000);
1362 if (!brd->re_map_port)
1363 goto err_remap_port;
1365 return 0;
1367 err_remap_port:
1368 iounmap(brd->re_map_membase);
1369 err_remap_mem:
1370 release_mem_region(brd->membase + PCI_IO_OFFSET, 0x200000);
1371 err_req_mem:
1372 release_mem_region(brd->membase, 0x200000);
1374 return -ENOMEM;
1377 static void dgap_unmap(struct board_t *brd)
1379 iounmap(brd->re_map_port);
1380 iounmap(brd->re_map_membase);
1381 release_mem_region(brd->membase + PCI_IO_OFFSET, 0x200000);
1382 release_mem_region(brd->membase, 0x200000);
1386 * dgap_parity_scan()
1388 * Convert the FEP5 way of reporting parity errors and breaks into
1389 * the Linux line discipline way.
1391 static void dgap_parity_scan(struct channel_t *ch, unsigned char *cbuf,
1392 unsigned char *fbuf, int *len)
1394 int l = *len;
1395 int count = 0;
1396 unsigned char *in, *cout, *fout;
1397 unsigned char c;
1399 in = cbuf;
1400 cout = cbuf;
1401 fout = fbuf;
1403 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
1404 return;
1406 while (l--) {
1407 c = *in++;
1408 switch (ch->pscan_state) {
1409 default:
1410 /* reset to sanity and fall through */
1411 ch->pscan_state = 0;
1413 case 0:
1414 /* No FF seen yet */
1415 if (c == (unsigned char)'\377')
1416 /* delete this character from stream */
1417 ch->pscan_state = 1;
1418 else {
1419 *cout++ = c;
1420 *fout++ = TTY_NORMAL;
1421 count += 1;
1423 break;
1425 case 1:
1426 /* first FF seen */
1427 if (c == (unsigned char)'\377') {
1428 /* doubled ff, transform to single ff */
1429 *cout++ = c;
1430 *fout++ = TTY_NORMAL;
1431 count += 1;
1432 ch->pscan_state = 0;
1433 } else {
1434 /* save value examination in next state */
1435 ch->pscan_savechar = c;
1436 ch->pscan_state = 2;
1438 break;
1440 case 2:
1441 /* third character of ff sequence */
1443 *cout++ = c;
1445 if (ch->pscan_savechar == 0x0) {
1446 if (c == 0x0) {
1447 ch->ch_err_break++;
1448 *fout++ = TTY_BREAK;
1449 } else {
1450 ch->ch_err_parity++;
1451 *fout++ = TTY_PARITY;
1455 count += 1;
1456 ch->pscan_state = 0;
1459 *len = count;
1462 /*=======================================================================
1464 * dgap_input - Process received data.
1466 * ch - Pointer to channel structure.
1468 *=======================================================================*/
1470 static void dgap_input(struct channel_t *ch)
1472 struct board_t *bd;
1473 struct bs_t __iomem *bs;
1474 struct tty_struct *tp;
1475 struct tty_ldisc *ld;
1476 uint rmask;
1477 uint head;
1478 uint tail;
1479 int data_len;
1480 ulong lock_flags;
1481 ulong lock_flags2;
1482 int flip_len;
1483 int len;
1484 int n;
1485 u8 *buf;
1486 u8 tmpchar;
1487 int s;
1489 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
1490 return;
1492 tp = ch->ch_tun.un_tty;
1494 bs = ch->ch_bs;
1495 if (!bs)
1496 return;
1498 bd = ch->ch_bd;
1499 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
1500 return;
1502 spin_lock_irqsave(&bd->bd_lock, lock_flags);
1503 spin_lock_irqsave(&ch->ch_lock, lock_flags2);
1506 * Figure the number of characters in the buffer.
1507 * Exit immediately if none.
1510 rmask = ch->ch_rsize - 1;
1512 head = readw(&bs->rx_head);
1513 head &= rmask;
1514 tail = readw(&bs->rx_tail);
1515 tail &= rmask;
1517 data_len = (head - tail) & rmask;
1519 if (data_len == 0) {
1520 writeb(1, &bs->idata);
1521 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
1522 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
1523 return;
1527 * If the device is not open, or CREAD is off, flush
1528 * input data and return immediately.
1530 if ((bd->state != BOARD_READY) || !tp ||
1531 (tp->magic != TTY_MAGIC) ||
1532 !(ch->ch_tun.un_flags & UN_ISOPEN) ||
1533 !(tp->termios.c_cflag & CREAD) ||
1534 (ch->ch_tun.un_flags & UN_CLOSING)) {
1535 writew(head, &bs->rx_tail);
1536 writeb(1, &bs->idata);
1537 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
1538 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
1539 return;
1543 * If we are throttled, simply don't read any data.
1545 if (ch->ch_flags & CH_RXBLOCK) {
1546 writeb(1, &bs->idata);
1547 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
1548 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
1549 return;
1553 * Ignore oruns.
1555 tmpchar = readb(&bs->orun);
1556 if (tmpchar) {
1557 ch->ch_err_overrun++;
1558 writeb(0, &bs->orun);
1561 /* Decide how much data we can send into the tty layer */
1562 flip_len = TTY_FLIPBUF_SIZE;
1564 /* Chop down the length, if needed */
1565 len = min(data_len, flip_len);
1566 len = min(len, (N_TTY_BUF_SIZE - 1));
1568 ld = tty_ldisc_ref(tp);
1570 #ifdef TTY_DONT_FLIP
1572 * If the DONT_FLIP flag is on, don't flush our buffer, and act
1573 * like the ld doesn't have any space to put the data right now.
1575 if (test_bit(TTY_DONT_FLIP, &tp->flags))
1576 len = 0;
1577 #endif
1580 * If we were unable to get a reference to the ld,
1581 * don't flush our buffer, and act like the ld doesn't
1582 * have any space to put the data right now.
1584 if (!ld) {
1585 len = 0;
1586 } else {
1588 * If ld doesn't have a pointer to a receive_buf function,
1589 * flush the data, then act like the ld doesn't have any
1590 * space to put the data right now.
1592 if (!ld->ops->receive_buf) {
1593 writew(head, &bs->rx_tail);
1594 len = 0;
1598 if (len <= 0) {
1599 writeb(1, &bs->idata);
1600 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
1601 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
1602 if (ld)
1603 tty_ldisc_deref(ld);
1604 return;
1607 buf = ch->ch_bd->flipbuf;
1608 n = len;
1611 * n now contains the most amount of data we can copy,
1612 * bounded either by our buffer size or the amount
1613 * of data the card actually has pending...
1615 while (n) {
1616 s = ((head >= tail) ? head : ch->ch_rsize) - tail;
1617 s = min(s, n);
1619 if (s <= 0)
1620 break;
1622 memcpy_fromio(buf, ch->ch_raddr + tail, s);
1624 tail += s;
1625 buf += s;
1627 n -= s;
1628 /* Flip queue if needed */
1629 tail &= rmask;
1632 writew(tail, &bs->rx_tail);
1633 writeb(1, &bs->idata);
1634 ch->ch_rxcount += len;
1637 * If we are completely raw, we don't need to go through a lot
1638 * of the tty layers that exist.
1639 * In this case, we take the shortest and fastest route we
1640 * can to relay the data to the user.
1642 * On the other hand, if we are not raw, we need to go through
1643 * the tty layer, which has its API more well defined.
1645 if (I_PARMRK(tp) || I_BRKINT(tp) || I_INPCK(tp)) {
1646 dgap_parity_scan(ch, ch->ch_bd->flipbuf,
1647 ch->ch_bd->flipflagbuf, &len);
1649 len = tty_buffer_request_room(tp->port, len);
1650 tty_insert_flip_string_flags(tp->port, ch->ch_bd->flipbuf,
1651 ch->ch_bd->flipflagbuf, len);
1652 } else {
1653 len = tty_buffer_request_room(tp->port, len);
1654 tty_insert_flip_string(tp->port, ch->ch_bd->flipbuf, len);
1657 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
1658 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
1660 /* Tell the tty layer its okay to "eat" the data now */
1661 tty_flip_buffer_push(tp->port);
1663 if (ld)
1664 tty_ldisc_deref(ld);
1667 static void dgap_write_wakeup(struct board_t *bd, struct channel_t *ch,
1668 struct un_t *un, u32 mask,
1669 unsigned long *irq_flags1,
1670 unsigned long *irq_flags2)
1672 if (!(un->un_flags & mask))
1673 return;
1675 un->un_flags &= ~mask;
1677 if (!(un->un_flags & UN_ISOPEN))
1678 return;
1680 if ((un->un_tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1681 un->un_tty->ldisc->ops->write_wakeup) {
1682 spin_unlock_irqrestore(&ch->ch_lock, *irq_flags2);
1683 spin_unlock_irqrestore(&bd->bd_lock, *irq_flags1);
1685 (un->un_tty->ldisc->ops->write_wakeup)(un->un_tty);
1687 spin_lock_irqsave(&bd->bd_lock, *irq_flags1);
1688 spin_lock_irqsave(&ch->ch_lock, *irq_flags2);
1690 wake_up_interruptible(&un->un_tty->write_wait);
1691 wake_up_interruptible(&un->un_flags_wait);
1694 /************************************************************************
1695 * Determines when CARRIER changes state and takes appropriate
1696 * action.
1697 ************************************************************************/
1698 static void dgap_carrier(struct channel_t *ch)
1700 struct board_t *bd;
1702 int virt_carrier = 0;
1703 int phys_carrier = 0;
1705 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
1706 return;
1708 bd = ch->ch_bd;
1710 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
1711 return;
1713 /* Make sure altpin is always set correctly */
1714 if (ch->ch_digi.digi_flags & DIGI_ALTPIN) {
1715 ch->ch_dsr = DM_CD;
1716 ch->ch_cd = DM_DSR;
1717 } else {
1718 ch->ch_dsr = DM_DSR;
1719 ch->ch_cd = DM_CD;
1722 if (ch->ch_mistat & D_CD(ch))
1723 phys_carrier = 1;
1725 if (ch->ch_digi.digi_flags & DIGI_FORCEDCD)
1726 virt_carrier = 1;
1728 if (ch->ch_c_cflag & CLOCAL)
1729 virt_carrier = 1;
1732 * Test for a VIRTUAL carrier transition to HIGH.
1734 if (((ch->ch_flags & CH_FCAR) == 0) && (virt_carrier == 1)) {
1736 * When carrier rises, wake any threads waiting
1737 * for carrier in the open routine.
1740 if (waitqueue_active(&(ch->ch_flags_wait)))
1741 wake_up_interruptible(&ch->ch_flags_wait);
1745 * Test for a PHYSICAL carrier transition to HIGH.
1747 if (((ch->ch_flags & CH_CD) == 0) && (phys_carrier == 1)) {
1749 * When carrier rises, wake any threads waiting
1750 * for carrier in the open routine.
1753 if (waitqueue_active(&(ch->ch_flags_wait)))
1754 wake_up_interruptible(&ch->ch_flags_wait);
1758 * Test for a PHYSICAL transition to low, so long as we aren't
1759 * currently ignoring physical transitions (which is what "virtual
1760 * carrier" indicates).
1762 * The transition of the virtual carrier to low really doesn't
1763 * matter... it really only means "ignore carrier state", not
1764 * "make pretend that carrier is there".
1766 if ((virt_carrier == 0) &&
1767 ((ch->ch_flags & CH_CD) != 0) &&
1768 (phys_carrier == 0)) {
1770 * When carrier drops:
1772 * Drop carrier on all open units.
1774 * Flush queues, waking up any task waiting in the
1775 * line discipline.
1777 * Send a hangup to the control terminal.
1779 * Enable all select calls.
1781 if (waitqueue_active(&(ch->ch_flags_wait)))
1782 wake_up_interruptible(&ch->ch_flags_wait);
1784 if (ch->ch_tun.un_open_count > 0)
1785 tty_hangup(ch->ch_tun.un_tty);
1787 if (ch->ch_pun.un_open_count > 0)
1788 tty_hangup(ch->ch_pun.un_tty);
1792 * Make sure that our cached values reflect the current reality.
1794 if (virt_carrier == 1)
1795 ch->ch_flags |= CH_FCAR;
1796 else
1797 ch->ch_flags &= ~CH_FCAR;
1799 if (phys_carrier == 1)
1800 ch->ch_flags |= CH_CD;
1801 else
1802 ch->ch_flags &= ~CH_CD;
1805 /*=======================================================================
1807 * dgap_event - FEP to host event processing routine.
1809 * bd - Board of current event.
1811 *=======================================================================*/
1812 static int dgap_event(struct board_t *bd)
1814 struct channel_t *ch;
1815 ulong lock_flags;
1816 ulong lock_flags2;
1817 struct bs_t __iomem *bs;
1818 u8 __iomem *event;
1819 u8 __iomem *vaddr;
1820 struct ev_t __iomem *eaddr;
1821 uint head;
1822 uint tail;
1823 int port;
1824 int reason;
1825 int modem;
1827 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
1828 return -EIO;
1830 spin_lock_irqsave(&bd->bd_lock, lock_flags);
1832 vaddr = bd->re_map_membase;
1834 if (!vaddr) {
1835 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
1836 return -EIO;
1839 eaddr = (struct ev_t __iomem *)(vaddr + EVBUF);
1841 /* Get our head and tail */
1842 head = readw(&eaddr->ev_head);
1843 tail = readw(&eaddr->ev_tail);
1846 * Forget it if pointers out of range.
1849 if (head >= EVMAX - EVSTART || tail >= EVMAX - EVSTART ||
1850 (head | tail) & 03) {
1851 /* Let go of board lock */
1852 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
1853 return -EIO;
1857 * Loop to process all the events in the buffer.
1859 while (tail != head) {
1861 * Get interrupt information.
1864 event = bd->re_map_membase + tail + EVSTART;
1866 port = ioread8(event);
1867 reason = ioread8(event + 1);
1868 modem = ioread8(event + 2);
1869 ioread8(event + 3);
1872 * Make sure the interrupt is valid.
1874 if (port >= bd->nasync)
1875 goto next;
1877 if (!(reason & (IFMODEM | IFBREAK | IFTLW | IFTEM | IFDATA)))
1878 goto next;
1880 ch = bd->channels[port];
1882 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
1883 goto next;
1886 * If we have made it here, the event was valid.
1887 * Lock down the channel.
1889 spin_lock_irqsave(&ch->ch_lock, lock_flags2);
1891 bs = ch->ch_bs;
1893 if (!bs) {
1894 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
1895 goto next;
1899 * Process received data.
1901 if (reason & IFDATA) {
1903 * ALL LOCKS *MUST* BE DROPPED BEFORE CALLING INPUT!
1904 * input could send some data to ld, which in turn
1905 * could do a callback to one of our other functions.
1907 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
1908 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
1910 dgap_input(ch);
1912 spin_lock_irqsave(&bd->bd_lock, lock_flags);
1913 spin_lock_irqsave(&ch->ch_lock, lock_flags2);
1915 if (ch->ch_flags & CH_RACTIVE)
1916 ch->ch_flags |= CH_RENABLE;
1917 else
1918 writeb(1, &bs->idata);
1920 if (ch->ch_flags & CH_RWAIT) {
1921 ch->ch_flags &= ~CH_RWAIT;
1923 wake_up_interruptible
1924 (&ch->ch_tun.un_flags_wait);
1929 * Process Modem change signals.
1931 if (reason & IFMODEM) {
1932 ch->ch_mistat = modem;
1933 dgap_carrier(ch);
1937 * Process break.
1939 if (reason & IFBREAK) {
1940 if (ch->ch_tun.un_tty) {
1941 /* A break has been indicated */
1942 ch->ch_err_break++;
1943 tty_buffer_request_room
1944 (ch->ch_tun.un_tty->port, 1);
1945 tty_insert_flip_char(ch->ch_tun.un_tty->port,
1946 0, TTY_BREAK);
1947 tty_flip_buffer_push(ch->ch_tun.un_tty->port);
1952 * Process Transmit low.
1954 if (reason & IFTLW) {
1955 dgap_write_wakeup(bd, ch, &ch->ch_tun, UN_LOW,
1956 &lock_flags, &lock_flags2);
1957 dgap_write_wakeup(bd, ch, &ch->ch_pun, UN_LOW,
1958 &lock_flags, &lock_flags2);
1959 if (ch->ch_flags & CH_WLOW) {
1960 ch->ch_flags &= ~CH_WLOW;
1961 wake_up_interruptible(&ch->ch_flags_wait);
1966 * Process Transmit empty.
1968 if (reason & IFTEM) {
1969 dgap_write_wakeup(bd, ch, &ch->ch_tun, UN_EMPTY,
1970 &lock_flags, &lock_flags2);
1971 dgap_write_wakeup(bd, ch, &ch->ch_pun, UN_EMPTY,
1972 &lock_flags, &lock_flags2);
1973 if (ch->ch_flags & CH_WEMPTY) {
1974 ch->ch_flags &= ~CH_WEMPTY;
1975 wake_up_interruptible(&ch->ch_flags_wait);
1979 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
1981 next:
1982 tail = (tail + 4) & (EVMAX - EVSTART - 4);
1985 writew(tail, &eaddr->ev_tail);
1986 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
1988 return 0;
1992 * Our board poller function.
1994 static void dgap_poll_tasklet(unsigned long data)
1996 struct board_t *bd = (struct board_t *)data;
1997 ulong lock_flags;
1998 char __iomem *vaddr;
1999 u16 head, tail;
2001 if (!bd || (bd->magic != DGAP_BOARD_MAGIC))
2002 return;
2004 if (bd->inhibit_poller)
2005 return;
2007 spin_lock_irqsave(&bd->bd_lock, lock_flags);
2009 vaddr = bd->re_map_membase;
2012 * If board is ready, parse deeper to see if there is anything to do.
2014 if (bd->state == BOARD_READY) {
2015 struct ev_t __iomem *eaddr;
2017 if (!bd->re_map_membase) {
2018 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
2019 return;
2021 if (!bd->re_map_port) {
2022 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
2023 return;
2026 if (!bd->nasync)
2027 goto out;
2029 eaddr = (struct ev_t __iomem *)(vaddr + EVBUF);
2031 /* Get our head and tail */
2032 head = readw(&eaddr->ev_head);
2033 tail = readw(&eaddr->ev_tail);
2036 * If there is an event pending. Go service it.
2038 if (head != tail) {
2039 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
2040 dgap_event(bd);
2041 spin_lock_irqsave(&bd->bd_lock, lock_flags);
2044 out:
2046 * If board is doing interrupts, ACK the interrupt.
2048 if (bd->intr_running)
2049 readb(bd->re_map_port + 2);
2051 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
2052 return;
2055 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
2059 * dgap_found_board()
2061 * A board has been found, init it.
2063 static struct board_t *dgap_found_board(struct pci_dev *pdev, int id,
2064 int boardnum)
2066 struct board_t *brd;
2067 unsigned int pci_irq;
2068 int i;
2069 int ret;
2071 /* get the board structure and prep it */
2072 brd = kzalloc(sizeof(struct board_t), GFP_KERNEL);
2073 if (!brd)
2074 return ERR_PTR(-ENOMEM);
2076 /* store the info for the board we've found */
2077 brd->magic = DGAP_BOARD_MAGIC;
2078 brd->boardnum = boardnum;
2079 brd->vendor = dgap_pci_tbl[id].vendor;
2080 brd->device = dgap_pci_tbl[id].device;
2081 brd->pdev = pdev;
2082 brd->pci_bus = pdev->bus->number;
2083 brd->pci_slot = PCI_SLOT(pdev->devfn);
2084 brd->name = dgap_ids[id].name;
2085 brd->maxports = dgap_ids[id].maxports;
2086 brd->type = dgap_ids[id].config_type;
2087 brd->dpatype = dgap_ids[id].dpatype;
2088 brd->dpastatus = BD_NOFEP;
2089 init_waitqueue_head(&brd->state_wait);
2091 spin_lock_init(&brd->bd_lock);
2093 brd->inhibit_poller = FALSE;
2094 brd->wait_for_bios = 0;
2095 brd->wait_for_fep = 0;
2097 for (i = 0; i < MAXPORTS; i++)
2098 brd->channels[i] = NULL;
2100 /* store which card & revision we have */
2101 pci_read_config_word(pdev, PCI_SUBSYSTEM_VENDOR_ID, &brd->subvendor);
2102 pci_read_config_word(pdev, PCI_SUBSYSTEM_ID, &brd->subdevice);
2103 pci_read_config_byte(pdev, PCI_REVISION_ID, &brd->rev);
2105 pci_irq = pdev->irq;
2106 brd->irq = pci_irq;
2108 /* get the PCI Base Address Registers */
2110 /* Xr Jupiter and EPC use BAR 2 */
2111 if (brd->device == PCI_DEV_XRJ_DID || brd->device == PCI_DEV_EPCJ_DID) {
2112 brd->membase = pci_resource_start(pdev, 2);
2113 brd->membase_end = pci_resource_end(pdev, 2);
2115 /* Everyone else uses BAR 0 */
2116 else {
2117 brd->membase = pci_resource_start(pdev, 0);
2118 brd->membase_end = pci_resource_end(pdev, 0);
2121 if (!brd->membase) {
2122 ret = -ENODEV;
2123 goto free_brd;
2126 if (brd->membase & 1)
2127 brd->membase &= ~3;
2128 else
2129 brd->membase &= ~15;
2132 * On the PCI boards, there is no IO space allocated
2133 * The I/O registers will be in the first 3 bytes of the
2134 * upper 2MB of the 4MB memory space. The board memory
2135 * will be mapped into the low 2MB of the 4MB memory space
2137 brd->port = brd->membase + PCI_IO_OFFSET;
2138 brd->port_end = brd->port + PCI_IO_SIZE_DGAP;
2141 * Special initialization for non-PLX boards
2143 if (brd->device != PCI_DEV_XRJ_DID && brd->device != PCI_DEV_EPCJ_DID) {
2144 unsigned short cmd;
2146 pci_write_config_byte(pdev, 0x40, 0);
2147 pci_write_config_byte(pdev, 0x46, 0);
2149 /* Limit burst length to 2 doubleword transactions */
2150 pci_write_config_byte(pdev, 0x42, 1);
2153 * Enable IO and mem if not already done.
2154 * This was needed for support on Itanium.
2156 pci_read_config_word(pdev, PCI_COMMAND, &cmd);
2157 cmd |= (PCI_COMMAND_IO | PCI_COMMAND_MEMORY);
2158 pci_write_config_word(pdev, PCI_COMMAND, cmd);
2161 /* init our poll helper tasklet */
2162 tasklet_init(&brd->helper_tasklet, dgap_poll_tasklet,
2163 (unsigned long)brd);
2165 ret = dgap_remap(brd);
2166 if (ret)
2167 goto free_brd;
2169 pr_info("dgap: board %d: %s (rev %d), irq %ld\n",
2170 boardnum, brd->name, brd->rev, brd->irq);
2172 return brd;
2174 free_brd:
2175 kfree(brd);
2177 return ERR_PTR(ret);
2181 * dgap_intr()
2183 * Driver interrupt handler.
2185 static irqreturn_t dgap_intr(int irq, void *voidbrd)
2187 struct board_t *brd = voidbrd;
2189 if (!brd)
2190 return IRQ_NONE;
2193 * Check to make sure its for us.
2195 if (brd->magic != DGAP_BOARD_MAGIC)
2196 return IRQ_NONE;
2198 brd->intr_count++;
2201 * Schedule tasklet to run at a better time.
2203 tasklet_schedule(&brd->helper_tasklet);
2204 return IRQ_HANDLED;
2207 /*****************************************************************************
2209 * Function:
2211 * dgap_poll_handler
2213 * Author:
2215 * Scott H Kilau
2217 * Parameters:
2219 * dummy -- ignored
2221 * Return Values:
2223 * none
2225 * Description:
2227 * As each timer expires, it determines (a) whether the "transmit"
2228 * waiter needs to be woken up, and (b) whether the poller needs to
2229 * be rescheduled.
2231 ******************************************************************************/
2233 static void dgap_poll_handler(ulong dummy)
2235 unsigned int i;
2236 struct board_t *brd;
2237 unsigned long lock_flags;
2238 ulong new_time;
2240 dgap_poll_counter++;
2243 * Do not start the board state machine until
2244 * driver tells us its up and running, and has
2245 * everything it needs.
2247 if (dgap_driver_state != DRIVER_READY)
2248 goto schedule_poller;
2251 * If we have just 1 board, or the system is not SMP,
2252 * then use the typical old style poller.
2253 * Otherwise, use our new tasklet based poller, which should
2254 * speed things up for multiple boards.
2256 if ((dgap_numboards == 1) || (num_online_cpus() <= 1)) {
2257 for (i = 0; i < dgap_numboards; i++) {
2258 brd = dgap_board[i];
2260 if (brd->state == BOARD_FAILED)
2261 continue;
2262 if (!brd->intr_running)
2263 /* Call the real board poller directly */
2264 dgap_poll_tasklet((unsigned long)brd);
2266 } else {
2268 * Go thru each board, kicking off a
2269 * tasklet for each if needed
2271 for (i = 0; i < dgap_numboards; i++) {
2272 brd = dgap_board[i];
2275 * Attempt to grab the board lock.
2277 * If we can't get it, no big deal, the next poll
2278 * will get it. Basically, I just really don't want
2279 * to spin in here, because I want to kick off my
2280 * tasklets as fast as I can, and then get out the
2281 * poller.
2283 if (!spin_trylock(&brd->bd_lock))
2284 continue;
2287 * If board is in a failed state, don't bother
2288 * scheduling a tasklet
2290 if (brd->state == BOARD_FAILED) {
2291 spin_unlock(&brd->bd_lock);
2292 continue;
2295 /* Schedule a poll helper task */
2296 if (!brd->intr_running)
2297 tasklet_schedule(&brd->helper_tasklet);
2300 * Can't do DGAP_UNLOCK here, as we don't have
2301 * lock_flags because we did a trylock above.
2303 spin_unlock(&brd->bd_lock);
2307 schedule_poller:
2310 * Schedule ourself back at the nominal wakeup interval.
2312 spin_lock_irqsave(&dgap_poll_lock, lock_flags);
2313 dgap_poll_time += dgap_jiffies_from_ms(dgap_poll_tick);
2315 new_time = dgap_poll_time - jiffies;
2317 if ((ulong)new_time >= 2 * dgap_poll_tick) {
2318 dgap_poll_time =
2319 jiffies + dgap_jiffies_from_ms(dgap_poll_tick);
2322 dgap_poll_timer.function = dgap_poll_handler;
2323 dgap_poll_timer.data = 0;
2324 dgap_poll_timer.expires = dgap_poll_time;
2325 spin_unlock_irqrestore(&dgap_poll_lock, lock_flags);
2327 if (!dgap_poll_stop)
2328 add_timer(&dgap_poll_timer);
2331 /*=======================================================================
2333 * dgap_cmdb - Sends a 2 byte command to the FEP.
2335 * ch - Pointer to channel structure.
2336 * cmd - Command to be sent.
2337 * byte1 - Integer containing first byte to be sent.
2338 * byte2 - Integer containing second byte to be sent.
2339 * ncmds - Wait until ncmds or fewer cmds are left
2340 * in the cmd buffer before returning.
2342 *=======================================================================*/
2343 static void dgap_cmdb(struct channel_t *ch, u8 cmd, u8 byte1,
2344 u8 byte2, uint ncmds)
2346 char __iomem *vaddr;
2347 struct __iomem cm_t *cm_addr;
2348 uint count;
2349 uint n;
2350 u16 head;
2351 u16 tail;
2353 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
2354 return;
2357 * Check if board is still alive.
2359 if (ch->ch_bd->state == BOARD_FAILED)
2360 return;
2363 * Make sure the pointers are in range before
2364 * writing to the FEP memory.
2366 vaddr = ch->ch_bd->re_map_membase;
2368 if (!vaddr)
2369 return;
2371 cm_addr = (struct cm_t __iomem *)(vaddr + CMDBUF);
2372 head = readw(&cm_addr->cm_head);
2375 * Forget it if pointers out of range.
2377 if (head >= (CMDMAX - CMDSTART) || (head & 03)) {
2378 ch->ch_bd->state = BOARD_FAILED;
2379 return;
2383 * Put the data in the circular command buffer.
2385 writeb(cmd, (vaddr + head + CMDSTART + 0));
2386 writeb((u8)ch->ch_portnum, (vaddr + head + CMDSTART + 1));
2387 writeb(byte1, (vaddr + head + CMDSTART + 2));
2388 writeb(byte2, (vaddr + head + CMDSTART + 3));
2390 head = (head + 4) & (CMDMAX - CMDSTART - 4);
2392 writew(head, &cm_addr->cm_head);
2395 * Wait if necessary before updating the head
2396 * pointer to limit the number of outstanding
2397 * commands to the FEP. If the time spent waiting
2398 * is outlandish, declare the FEP dead.
2400 for (count = dgap_count ;;) {
2401 head = readw(&cm_addr->cm_head);
2402 tail = readw(&cm_addr->cm_tail);
2404 n = (head - tail) & (CMDMAX - CMDSTART - 4);
2406 if (n <= ncmds * sizeof(struct cm_t))
2407 break;
2409 if (--count == 0) {
2410 ch->ch_bd->state = BOARD_FAILED;
2411 return;
2413 udelay(10);
2417 /*=======================================================================
2419 * dgap_cmdw - Sends a 1 word command to the FEP.
2421 * ch - Pointer to channel structure.
2422 * cmd - Command to be sent.
2423 * word - Integer containing word to be sent.
2424 * ncmds - Wait until ncmds or fewer cmds are left
2425 * in the cmd buffer before returning.
2427 *=======================================================================*/
2428 static void dgap_cmdw(struct channel_t *ch, u8 cmd, u16 word, uint ncmds)
2430 char __iomem *vaddr;
2431 struct __iomem cm_t *cm_addr;
2432 uint count;
2433 uint n;
2434 u16 head;
2435 u16 tail;
2437 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
2438 return;
2441 * Check if board is still alive.
2443 if (ch->ch_bd->state == BOARD_FAILED)
2444 return;
2447 * Make sure the pointers are in range before
2448 * writing to the FEP memory.
2450 vaddr = ch->ch_bd->re_map_membase;
2451 if (!vaddr)
2452 return;
2454 cm_addr = (struct cm_t __iomem *)(vaddr + CMDBUF);
2455 head = readw(&cm_addr->cm_head);
2458 * Forget it if pointers out of range.
2460 if (head >= (CMDMAX - CMDSTART) || (head & 03)) {
2461 ch->ch_bd->state = BOARD_FAILED;
2462 return;
2466 * Put the data in the circular command buffer.
2468 writeb(cmd, (vaddr + head + CMDSTART + 0));
2469 writeb((u8)ch->ch_portnum, (vaddr + head + CMDSTART + 1));
2470 writew((u16)word, (vaddr + head + CMDSTART + 2));
2472 head = (head + 4) & (CMDMAX - CMDSTART - 4);
2474 writew(head, &cm_addr->cm_head);
2477 * Wait if necessary before updating the head
2478 * pointer to limit the number of outstanding
2479 * commands to the FEP. If the time spent waiting
2480 * is outlandish, declare the FEP dead.
2482 for (count = dgap_count ;;) {
2483 head = readw(&cm_addr->cm_head);
2484 tail = readw(&cm_addr->cm_tail);
2486 n = (head - tail) & (CMDMAX - CMDSTART - 4);
2488 if (n <= ncmds * sizeof(struct cm_t))
2489 break;
2491 if (--count == 0) {
2492 ch->ch_bd->state = BOARD_FAILED;
2493 return;
2495 udelay(10);
2499 /*=======================================================================
2501 * dgap_cmdw_ext - Sends a extended word command to the FEP.
2503 * ch - Pointer to channel structure.
2504 * cmd - Command to be sent.
2505 * word - Integer containing word to be sent.
2506 * ncmds - Wait until ncmds or fewer cmds are left
2507 * in the cmd buffer before returning.
2509 *=======================================================================*/
2510 static void dgap_cmdw_ext(struct channel_t *ch, u16 cmd, u16 word, uint ncmds)
2512 char __iomem *vaddr;
2513 struct __iomem cm_t *cm_addr;
2514 uint count;
2515 uint n;
2516 u16 head;
2517 u16 tail;
2519 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
2520 return;
2523 * Check if board is still alive.
2525 if (ch->ch_bd->state == BOARD_FAILED)
2526 return;
2529 * Make sure the pointers are in range before
2530 * writing to the FEP memory.
2532 vaddr = ch->ch_bd->re_map_membase;
2533 if (!vaddr)
2534 return;
2536 cm_addr = (struct cm_t __iomem *)(vaddr + CMDBUF);
2537 head = readw(&cm_addr->cm_head);
2540 * Forget it if pointers out of range.
2542 if (head >= (CMDMAX - CMDSTART) || (head & 03)) {
2543 ch->ch_bd->state = BOARD_FAILED;
2544 return;
2548 * Put the data in the circular command buffer.
2551 /* Write an FF to tell the FEP that we want an extended command */
2552 writeb((u8)0xff, (vaddr + head + CMDSTART + 0));
2554 writeb((u8)ch->ch_portnum, (vaddr + head + CMDSTART + 1));
2555 writew((u16)cmd, (vaddr + head + CMDSTART + 2));
2558 * If the second part of the command won't fit,
2559 * put it at the beginning of the circular buffer.
2561 if (((head + 4) >= ((CMDMAX - CMDSTART)) || (head & 03)))
2562 writew((u16)word, (vaddr + CMDSTART));
2563 else
2564 writew((u16)word, (vaddr + head + CMDSTART + 4));
2566 head = (head + 8) & (CMDMAX - CMDSTART - 4);
2568 writew(head, &cm_addr->cm_head);
2571 * Wait if necessary before updating the head
2572 * pointer to limit the number of outstanding
2573 * commands to the FEP. If the time spent waiting
2574 * is outlandish, declare the FEP dead.
2576 for (count = dgap_count ;;) {
2577 head = readw(&cm_addr->cm_head);
2578 tail = readw(&cm_addr->cm_tail);
2580 n = (head - tail) & (CMDMAX - CMDSTART - 4);
2582 if (n <= ncmds * sizeof(struct cm_t))
2583 break;
2585 if (--count == 0) {
2586 ch->ch_bd->state = BOARD_FAILED;
2587 return;
2589 udelay(10);
2593 /*=======================================================================
2595 * dgap_wmove - Write data to FEP buffer.
2597 * ch - Pointer to channel structure.
2598 * buf - Pointer to characters to be moved.
2599 * cnt - Number of characters to move.
2601 *=======================================================================*/
2602 static void dgap_wmove(struct channel_t *ch, char *buf, uint cnt)
2604 int n;
2605 char __iomem *taddr;
2606 struct bs_t __iomem *bs;
2607 u16 head;
2609 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
2610 return;
2613 * Check parameters.
2615 bs = ch->ch_bs;
2616 head = readw(&bs->tx_head);
2619 * If pointers are out of range, just return.
2621 if ((cnt > ch->ch_tsize) ||
2622 (unsigned)(head - ch->ch_tstart) >= ch->ch_tsize)
2623 return;
2626 * If the write wraps over the top of the circular buffer,
2627 * move the portion up to the wrap point, and reset the
2628 * pointers to the bottom.
2630 n = ch->ch_tstart + ch->ch_tsize - head;
2632 if (cnt >= n) {
2633 cnt -= n;
2634 taddr = ch->ch_taddr + head;
2635 memcpy_toio(taddr, buf, n);
2636 head = ch->ch_tstart;
2637 buf += n;
2641 * Move rest of data.
2643 taddr = ch->ch_taddr + head;
2644 n = cnt;
2645 memcpy_toio(taddr, buf, n);
2646 head += cnt;
2648 writew(head, &bs->tx_head);
2652 * Calls the firmware to reset this channel.
2654 static void dgap_firmware_reset_port(struct channel_t *ch)
2656 dgap_cmdb(ch, CHRESET, 0, 0, 0);
2659 * Now that the channel is reset, we need to make sure
2660 * all the current settings get reapplied to the port
2661 * in the firmware.
2663 * So we will set the driver's cache of firmware
2664 * settings all to 0, and then call param.
2666 ch->ch_fepiflag = 0;
2667 ch->ch_fepcflag = 0;
2668 ch->ch_fepoflag = 0;
2669 ch->ch_fepstartc = 0;
2670 ch->ch_fepstopc = 0;
2671 ch->ch_fepastartc = 0;
2672 ch->ch_fepastopc = 0;
2673 ch->ch_mostat = 0;
2674 ch->ch_hflow = 0;
2677 /*=======================================================================
2679 * dgap_param - Set Digi parameters.
2681 * struct tty_struct * - TTY for port.
2683 *=======================================================================*/
2684 static int dgap_param(struct channel_t *ch, struct board_t *bd, u32 un_type)
2686 u16 head;
2687 u16 cflag;
2688 u16 iflag;
2689 u8 mval;
2690 u8 hflow;
2693 * If baud rate is zero, flush queues, and set mval to drop DTR.
2695 if ((ch->ch_c_cflag & (CBAUD)) == 0) {
2696 /* flush rx */
2697 head = readw(&ch->ch_bs->rx_head);
2698 writew(head, &ch->ch_bs->rx_tail);
2700 /* flush tx */
2701 head = readw(&ch->ch_bs->tx_head);
2702 writew(head, &ch->ch_bs->tx_tail);
2704 ch->ch_flags |= (CH_BAUD0);
2706 /* Drop RTS and DTR */
2707 ch->ch_mval &= ~(D_RTS(ch) | D_DTR(ch));
2708 mval = D_DTR(ch) | D_RTS(ch);
2709 ch->ch_baud_info = 0;
2711 } else if (ch->ch_custom_speed && (bd->bd_flags & BD_FEP5PLUS)) {
2713 * Tell the fep to do the command
2716 dgap_cmdw_ext(ch, 0xff01, ch->ch_custom_speed, 0);
2719 * Now go get from fep mem, what the fep
2720 * believes the custom baud rate is.
2722 ch->ch_custom_speed = dgap_get_custom_baud(ch);
2723 ch->ch_baud_info = ch->ch_custom_speed;
2725 /* Handle transition from B0 */
2726 if (ch->ch_flags & CH_BAUD0) {
2727 ch->ch_flags &= ~(CH_BAUD0);
2728 ch->ch_mval |= (D_RTS(ch) | D_DTR(ch));
2730 mval = D_DTR(ch) | D_RTS(ch);
2732 } else {
2734 * Set baud rate, character size, and parity.
2738 int iindex = 0;
2739 int jindex = 0;
2740 int baud = 0;
2742 ulong bauds[4][16] = {
2743 { /* slowbaud */
2744 0, 50, 75, 110,
2745 134, 150, 200, 300,
2746 600, 1200, 1800, 2400,
2747 4800, 9600, 19200, 38400 },
2748 { /* slowbaud & CBAUDEX */
2749 0, 57600, 115200, 230400,
2750 460800, 150, 200, 921600,
2751 600, 1200, 1800, 2400,
2752 4800, 9600, 19200, 38400 },
2753 { /* fastbaud */
2754 0, 57600, 76800, 115200,
2755 14400, 57600, 230400, 76800,
2756 115200, 230400, 28800, 460800,
2757 921600, 9600, 19200, 38400 },
2758 { /* fastbaud & CBAUDEX */
2759 0, 57600, 115200, 230400,
2760 460800, 150, 200, 921600,
2761 600, 1200, 1800, 2400,
2762 4800, 9600, 19200, 38400 }
2766 * Only use the TXPrint baud rate if the
2767 * terminal unit is NOT open
2769 if (!(ch->ch_tun.un_flags & UN_ISOPEN) &&
2770 un_type == DGAP_PRINT)
2771 baud = C_BAUD(ch->ch_pun.un_tty) & 0xff;
2772 else
2773 baud = C_BAUD(ch->ch_tun.un_tty) & 0xff;
2775 if (ch->ch_c_cflag & CBAUDEX)
2776 iindex = 1;
2778 if (ch->ch_digi.digi_flags & DIGI_FAST)
2779 iindex += 2;
2781 jindex = baud;
2783 if ((iindex >= 0) && (iindex < 4) &&
2784 (jindex >= 0) && (jindex < 16))
2785 baud = bauds[iindex][jindex];
2786 else
2787 baud = 0;
2789 if (baud == 0)
2790 baud = 9600;
2792 ch->ch_baud_info = baud;
2795 * CBAUD has bit position 0x1000 set these days to
2796 * indicate Linux baud rate remap.
2797 * We use a different bit assignment for high speed.
2798 * Clear this bit out while grabbing the parts of
2799 * "cflag" we want.
2801 cflag = ch->ch_c_cflag & ((CBAUD ^ CBAUDEX) | PARODD | PARENB |
2802 CSTOPB | CSIZE);
2805 * HUPCL bit is used by FEP to indicate fast baud
2806 * table is to be used.
2808 if ((ch->ch_digi.digi_flags & DIGI_FAST) ||
2809 (ch->ch_c_cflag & CBAUDEX))
2810 cflag |= HUPCL;
2812 if ((ch->ch_c_cflag & CBAUDEX) &&
2813 !(ch->ch_digi.digi_flags & DIGI_FAST)) {
2815 * The below code is trying to guarantee that only
2816 * baud rates 115200, 230400, 460800, 921600 are
2817 * remapped. We use exclusive or because the various
2818 * baud rates share common bit positions and therefore
2819 * can't be tested for easily.
2821 tcflag_t tcflag = (ch->ch_c_cflag & CBAUD) | CBAUDEX;
2822 int baudpart = 0;
2825 * Map high speed requests to index
2826 * into FEP's baud table
2828 switch (tcflag) {
2829 case B57600:
2830 baudpart = 1;
2831 break;
2832 #ifdef B76800
2833 case B76800:
2834 baudpart = 2;
2835 break;
2836 #endif
2837 case B115200:
2838 baudpart = 3;
2839 break;
2840 case B230400:
2841 baudpart = 9;
2842 break;
2843 case B460800:
2844 baudpart = 11;
2845 break;
2846 #ifdef B921600
2847 case B921600:
2848 baudpart = 12;
2849 break;
2850 #endif
2851 default:
2852 baudpart = 0;
2855 if (baudpart)
2856 cflag = (cflag & ~(CBAUD | CBAUDEX)) | baudpart;
2859 cflag &= 0xffff;
2861 if (cflag != ch->ch_fepcflag) {
2862 ch->ch_fepcflag = (u16)(cflag & 0xffff);
2865 * Okay to have channel and board
2866 * locks held calling this
2868 dgap_cmdw(ch, SCFLAG, (u16)cflag, 0);
2871 /* Handle transition from B0 */
2872 if (ch->ch_flags & CH_BAUD0) {
2873 ch->ch_flags &= ~(CH_BAUD0);
2874 ch->ch_mval |= (D_RTS(ch) | D_DTR(ch));
2876 mval = D_DTR(ch) | D_RTS(ch);
2880 * Get input flags.
2882 iflag = ch->ch_c_iflag & (IGNBRK | BRKINT | IGNPAR | PARMRK |
2883 INPCK | ISTRIP | IXON | IXANY | IXOFF);
2885 if ((ch->ch_startc == _POSIX_VDISABLE) ||
2886 (ch->ch_stopc == _POSIX_VDISABLE)) {
2887 iflag &= ~(IXON | IXOFF);
2888 ch->ch_c_iflag &= ~(IXON | IXOFF);
2892 * Only the IBM Xr card can switch between
2893 * 232 and 422 modes on the fly
2895 if (bd->device == PCI_DEV_XR_IBM_DID) {
2896 if (ch->ch_digi.digi_flags & DIGI_422)
2897 dgap_cmdb(ch, SCOMMODE, MODE_422, 0, 0);
2898 else
2899 dgap_cmdb(ch, SCOMMODE, MODE_232, 0, 0);
2902 if (ch->ch_digi.digi_flags & DIGI_ALTPIN)
2903 iflag |= IALTPIN;
2905 if (iflag != ch->ch_fepiflag) {
2906 ch->ch_fepiflag = iflag;
2908 /* Okay to have channel and board locks held calling this */
2909 dgap_cmdw(ch, SIFLAG, (u16)ch->ch_fepiflag, 0);
2913 * Select hardware handshaking.
2915 hflow = 0;
2917 if (ch->ch_c_cflag & CRTSCTS)
2918 hflow |= (D_RTS(ch) | D_CTS(ch));
2919 if (ch->ch_digi.digi_flags & RTSPACE)
2920 hflow |= D_RTS(ch);
2921 if (ch->ch_digi.digi_flags & DTRPACE)
2922 hflow |= D_DTR(ch);
2923 if (ch->ch_digi.digi_flags & CTSPACE)
2924 hflow |= D_CTS(ch);
2925 if (ch->ch_digi.digi_flags & DSRPACE)
2926 hflow |= D_DSR(ch);
2927 if (ch->ch_digi.digi_flags & DCDPACE)
2928 hflow |= D_CD(ch);
2930 if (hflow != ch->ch_hflow) {
2931 ch->ch_hflow = hflow;
2933 /* Okay to have channel and board locks held calling this */
2934 dgap_cmdb(ch, SHFLOW, (u8)hflow, 0xff, 0);
2938 * Set RTS and/or DTR Toggle if needed,
2939 * but only if product is FEP5+ based.
2941 if (bd->bd_flags & BD_FEP5PLUS) {
2942 u16 hflow2 = 0;
2944 if (ch->ch_digi.digi_flags & DIGI_RTS_TOGGLE)
2945 hflow2 |= (D_RTS(ch));
2946 if (ch->ch_digi.digi_flags & DIGI_DTR_TOGGLE)
2947 hflow2 |= (D_DTR(ch));
2949 dgap_cmdw_ext(ch, 0xff03, hflow2, 0);
2953 * Set modem control lines.
2956 mval ^= ch->ch_mforce & (mval ^ ch->ch_mval);
2958 if (ch->ch_mostat ^ mval) {
2959 ch->ch_mostat = mval;
2961 /* Okay to have channel and board locks held calling this */
2962 dgap_cmdb(ch, SMODEM, (u8)mval, D_RTS(ch) | D_DTR(ch), 0);
2966 * Read modem signals, and then call carrier function.
2968 ch->ch_mistat = readb(&ch->ch_bs->m_stat);
2969 dgap_carrier(ch);
2972 * Set the start and stop characters.
2974 if (ch->ch_startc != ch->ch_fepstartc ||
2975 ch->ch_stopc != ch->ch_fepstopc) {
2976 ch->ch_fepstartc = ch->ch_startc;
2977 ch->ch_fepstopc = ch->ch_stopc;
2979 /* Okay to have channel and board locks held calling this */
2980 dgap_cmdb(ch, SFLOWC, ch->ch_fepstartc, ch->ch_fepstopc, 0);
2984 * Set the Auxiliary start and stop characters.
2986 if (ch->ch_astartc != ch->ch_fepastartc ||
2987 ch->ch_astopc != ch->ch_fepastopc) {
2988 ch->ch_fepastartc = ch->ch_astartc;
2989 ch->ch_fepastopc = ch->ch_astopc;
2991 /* Okay to have channel and board locks held calling this */
2992 dgap_cmdb(ch, SAFLOWC, ch->ch_fepastartc, ch->ch_fepastopc, 0);
2995 return 0;
2999 * dgap_block_til_ready()
3001 * Wait for DCD, if needed.
3003 static int dgap_block_til_ready(struct tty_struct *tty, struct file *file,
3004 struct channel_t *ch)
3006 int retval = 0;
3007 struct un_t *un;
3008 ulong lock_flags;
3009 uint old_flags;
3010 int sleep_on_un_flags;
3012 if (!tty || tty->magic != TTY_MAGIC || !file || !ch ||
3013 ch->magic != DGAP_CHANNEL_MAGIC)
3014 return -EIO;
3016 un = tty->driver_data;
3017 if (!un || un->magic != DGAP_UNIT_MAGIC)
3018 return -EIO;
3020 spin_lock_irqsave(&ch->ch_lock, lock_flags);
3022 ch->ch_wopen++;
3024 /* Loop forever */
3025 while (1) {
3026 sleep_on_un_flags = 0;
3029 * If board has failed somehow during our sleep,
3030 * bail with error.
3032 if (ch->ch_bd->state == BOARD_FAILED) {
3033 retval = -EIO;
3034 break;
3037 /* If tty was hung up, break out of loop and set error. */
3038 if (tty_hung_up_p(file)) {
3039 retval = -EAGAIN;
3040 break;
3044 * If either unit is in the middle of the fragile part of close,
3045 * we just cannot touch the channel safely.
3046 * Go back to sleep, knowing that when the channel can be
3047 * touched safely, the close routine will signal the
3048 * ch_wait_flags to wake us back up.
3050 if (!((ch->ch_tun.un_flags | ch->ch_pun.un_flags) &
3051 UN_CLOSING)) {
3053 * Our conditions to leave cleanly and happily:
3054 * 1) NONBLOCKING on the tty is set.
3055 * 2) CLOCAL is set.
3056 * 3) DCD (fake or real) is active.
3059 if (file->f_flags & O_NONBLOCK)
3060 break;
3062 if (tty->flags & (1 << TTY_IO_ERROR))
3063 break;
3065 if (ch->ch_flags & CH_CD)
3066 break;
3068 if (ch->ch_flags & CH_FCAR)
3069 break;
3070 } else {
3071 sleep_on_un_flags = 1;
3075 * If there is a signal pending, the user probably
3076 * interrupted (ctrl-c) us.
3077 * Leave loop with error set.
3079 if (signal_pending(current)) {
3080 retval = -ERESTARTSYS;
3081 break;
3085 * Store the flags before we let go of channel lock
3087 if (sleep_on_un_flags)
3088 old_flags = ch->ch_tun.un_flags | ch->ch_pun.un_flags;
3089 else
3090 old_flags = ch->ch_flags;
3093 * Let go of channel lock before calling schedule.
3094 * Our poller will get any FEP events and wake us up when DCD
3095 * eventually goes active.
3098 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
3101 * Wait for something in the flags to change
3102 * from the current value.
3104 if (sleep_on_un_flags) {
3105 retval = wait_event_interruptible(un->un_flags_wait,
3106 (old_flags != (ch->ch_tun.un_flags |
3107 ch->ch_pun.un_flags)));
3108 } else {
3109 retval = wait_event_interruptible(ch->ch_flags_wait,
3110 (old_flags != ch->ch_flags));
3114 * We got woken up for some reason.
3115 * Before looping around, grab our channel lock.
3117 spin_lock_irqsave(&ch->ch_lock, lock_flags);
3120 ch->ch_wopen--;
3122 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
3124 return retval;
3128 * dgap_tty_flush_buffer()
3130 * Flush Tx buffer (make in == out)
3132 static void dgap_tty_flush_buffer(struct tty_struct *tty)
3134 struct board_t *bd;
3135 struct channel_t *ch;
3136 struct un_t *un;
3137 ulong lock_flags;
3138 ulong lock_flags2;
3139 u16 head;
3141 if (!tty || tty->magic != TTY_MAGIC)
3142 return;
3144 un = tty->driver_data;
3145 if (!un || un->magic != DGAP_UNIT_MAGIC)
3146 return;
3148 ch = un->un_ch;
3149 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
3150 return;
3152 bd = ch->ch_bd;
3153 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
3154 return;
3156 spin_lock_irqsave(&bd->bd_lock, lock_flags);
3157 spin_lock_irqsave(&ch->ch_lock, lock_flags2);
3159 ch->ch_flags &= ~CH_STOP;
3160 head = readw(&ch->ch_bs->tx_head);
3161 dgap_cmdw(ch, FLUSHTX, (u16)head, 0);
3162 dgap_cmdw(ch, RESUMETX, 0, 0);
3163 if (ch->ch_tun.un_flags & (UN_LOW | UN_EMPTY)) {
3164 ch->ch_tun.un_flags &= ~(UN_LOW | UN_EMPTY);
3165 wake_up_interruptible(&ch->ch_tun.un_flags_wait);
3167 if (ch->ch_pun.un_flags & (UN_LOW | UN_EMPTY)) {
3168 ch->ch_pun.un_flags &= ~(UN_LOW | UN_EMPTY);
3169 wake_up_interruptible(&ch->ch_pun.un_flags_wait);
3172 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
3173 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
3174 if (waitqueue_active(&tty->write_wait))
3175 wake_up_interruptible(&tty->write_wait);
3176 tty_wakeup(tty);
3180 * dgap_tty_hangup()
3182 * Hangup the port. Like a close, but don't wait for output to drain.
3184 static void dgap_tty_hangup(struct tty_struct *tty)
3186 struct board_t *bd;
3187 struct channel_t *ch;
3188 struct un_t *un;
3190 if (!tty || tty->magic != TTY_MAGIC)
3191 return;
3193 un = tty->driver_data;
3194 if (!un || un->magic != DGAP_UNIT_MAGIC)
3195 return;
3197 ch = un->un_ch;
3198 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
3199 return;
3201 bd = ch->ch_bd;
3202 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
3203 return;
3205 /* flush the transmit queues */
3206 dgap_tty_flush_buffer(tty);
3210 * dgap_tty_chars_in_buffer()
3212 * Return number of characters that have not been transmitted yet.
3214 * This routine is used by the line discipline to determine if there
3215 * is data waiting to be transmitted/drained/flushed or not.
3217 static int dgap_tty_chars_in_buffer(struct tty_struct *tty)
3219 struct board_t *bd;
3220 struct channel_t *ch;
3221 struct un_t *un;
3222 struct bs_t __iomem *bs;
3223 u8 tbusy;
3224 uint chars;
3225 u16 thead, ttail, tmask, chead, ctail;
3226 ulong lock_flags = 0;
3227 ulong lock_flags2 = 0;
3229 if (!tty)
3230 return 0;
3232 un = tty->driver_data;
3233 if (!un || un->magic != DGAP_UNIT_MAGIC)
3234 return 0;
3236 ch = un->un_ch;
3237 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
3238 return 0;
3240 bd = ch->ch_bd;
3241 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
3242 return 0;
3244 bs = ch->ch_bs;
3245 if (!bs)
3246 return 0;
3248 spin_lock_irqsave(&bd->bd_lock, lock_flags);
3249 spin_lock_irqsave(&ch->ch_lock, lock_flags2);
3251 tmask = (ch->ch_tsize - 1);
3253 /* Get Transmit queue pointers */
3254 thead = readw(&bs->tx_head) & tmask;
3255 ttail = readw(&bs->tx_tail) & tmask;
3257 /* Get tbusy flag */
3258 tbusy = readb(&bs->tbusy);
3260 /* Get Command queue pointers */
3261 chead = readw(&ch->ch_cm->cm_head);
3262 ctail = readw(&ch->ch_cm->cm_tail);
3264 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
3265 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
3268 * The only way we know for sure if there is no pending
3269 * data left to be transferred, is if:
3270 * 1) Transmit head and tail are equal (empty).
3271 * 2) Command queue head and tail are equal (empty).
3272 * 3) The "TBUSY" flag is 0. (Transmitter not busy).
3275 if ((ttail == thead) && (tbusy == 0) && (chead == ctail)) {
3276 chars = 0;
3277 } else {
3278 if (thead >= ttail)
3279 chars = thead - ttail;
3280 else
3281 chars = thead - ttail + ch->ch_tsize;
3283 * Fudge factor here.
3284 * If chars is zero, we know that the command queue had
3285 * something in it or tbusy was set. Because we cannot
3286 * be sure if there is still some data to be transmitted,
3287 * lets lie, and tell ld we have 1 byte left.
3289 if (chars == 0) {
3291 * If TBUSY is still set, and our tx buffers are empty,
3292 * force the firmware to send me another wakeup after
3293 * TBUSY has been cleared.
3295 if (tbusy != 0) {
3296 spin_lock_irqsave(&ch->ch_lock, lock_flags);
3297 un->un_flags |= UN_EMPTY;
3298 writeb(1, &bs->iempty);
3299 spin_unlock_irqrestore(&ch->ch_lock,
3300 lock_flags);
3302 chars = 1;
3306 return chars;
3309 static int dgap_wait_for_drain(struct tty_struct *tty)
3311 struct channel_t *ch;
3312 struct un_t *un;
3313 struct bs_t __iomem *bs;
3314 int ret = 0;
3315 uint count = 1;
3316 ulong lock_flags = 0;
3318 if (!tty || tty->magic != TTY_MAGIC)
3319 return -EIO;
3321 un = tty->driver_data;
3322 if (!un || un->magic != DGAP_UNIT_MAGIC)
3323 return -EIO;
3325 ch = un->un_ch;
3326 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
3327 return -EIO;
3329 bs = ch->ch_bs;
3330 if (!bs)
3331 return -EIO;
3333 /* Loop until data is drained */
3334 while (count != 0) {
3335 count = dgap_tty_chars_in_buffer(tty);
3337 if (count == 0)
3338 break;
3340 /* Set flag waiting for drain */
3341 spin_lock_irqsave(&ch->ch_lock, lock_flags);
3342 un->un_flags |= UN_EMPTY;
3343 writeb(1, &bs->iempty);
3344 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
3346 /* Go to sleep till we get woken up */
3347 ret = wait_event_interruptible(un->un_flags_wait,
3348 ((un->un_flags & UN_EMPTY) == 0));
3349 /* If ret is non-zero, user ctrl-c'ed us */
3350 if (ret)
3351 break;
3354 spin_lock_irqsave(&ch->ch_lock, lock_flags);
3355 un->un_flags &= ~(UN_EMPTY);
3356 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
3358 return ret;
3362 * dgap_maxcps_room
3364 * Reduces bytes_available to the max number of characters
3365 * that can be sent currently given the maxcps value, and
3366 * returns the new bytes_available. This only affects printer
3367 * output.
3369 static int dgap_maxcps_room(struct channel_t *ch, struct un_t *un,
3370 int bytes_available)
3373 * If its not the Transparent print device, return
3374 * the full data amount.
3376 if (un->un_type != DGAP_PRINT)
3377 return bytes_available;
3379 if (ch->ch_digi.digi_maxcps > 0 && ch->ch_digi.digi_bufsize > 0) {
3380 int cps_limit = 0;
3381 unsigned long current_time = jiffies;
3382 unsigned long buffer_time = current_time +
3383 (HZ * ch->ch_digi.digi_bufsize) /
3384 ch->ch_digi.digi_maxcps;
3386 if (ch->ch_cpstime < current_time) {
3387 /* buffer is empty */
3388 ch->ch_cpstime = current_time; /* reset ch_cpstime */
3389 cps_limit = ch->ch_digi.digi_bufsize;
3390 } else if (ch->ch_cpstime < buffer_time) {
3391 /* still room in the buffer */
3392 cps_limit = ((buffer_time - ch->ch_cpstime) *
3393 ch->ch_digi.digi_maxcps) / HZ;
3394 } else {
3395 /* no room in the buffer */
3396 cps_limit = 0;
3399 bytes_available = min(cps_limit, bytes_available);
3402 return bytes_available;
3405 static inline void dgap_set_firmware_event(struct un_t *un, unsigned int event)
3407 struct channel_t *ch;
3408 struct bs_t __iomem *bs;
3410 if (!un || un->magic != DGAP_UNIT_MAGIC)
3411 return;
3412 ch = un->un_ch;
3413 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
3414 return;
3415 bs = ch->ch_bs;
3416 if (!bs)
3417 return;
3419 if ((event & UN_LOW) != 0) {
3420 if ((un->un_flags & UN_LOW) == 0) {
3421 un->un_flags |= UN_LOW;
3422 writeb(1, &bs->ilow);
3425 if ((event & UN_LOW) != 0) {
3426 if ((un->un_flags & UN_EMPTY) == 0) {
3427 un->un_flags |= UN_EMPTY;
3428 writeb(1, &bs->iempty);
3434 * dgap_tty_write_room()
3436 * Return space available in Tx buffer
3438 static int dgap_tty_write_room(struct tty_struct *tty)
3440 struct channel_t *ch;
3441 struct un_t *un;
3442 struct bs_t __iomem *bs;
3443 u16 head, tail, tmask;
3444 int ret;
3445 ulong lock_flags = 0;
3447 if (!tty)
3448 return 0;
3450 un = tty->driver_data;
3451 if (!un || un->magic != DGAP_UNIT_MAGIC)
3452 return 0;
3454 ch = un->un_ch;
3455 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
3456 return 0;
3458 bs = ch->ch_bs;
3459 if (!bs)
3460 return 0;
3462 spin_lock_irqsave(&ch->ch_lock, lock_flags);
3464 tmask = ch->ch_tsize - 1;
3465 head = readw(&bs->tx_head) & tmask;
3466 tail = readw(&bs->tx_tail) & tmask;
3468 ret = tail - head - 1;
3469 if (ret < 0)
3470 ret += ch->ch_tsize;
3472 /* Limit printer to maxcps */
3473 ret = dgap_maxcps_room(ch, un, ret);
3476 * If we are printer device, leave space for
3477 * possibly both the on and off strings.
3479 if (un->un_type == DGAP_PRINT) {
3480 if (!(ch->ch_flags & CH_PRON))
3481 ret -= ch->ch_digi.digi_onlen;
3482 ret -= ch->ch_digi.digi_offlen;
3483 } else {
3484 if (ch->ch_flags & CH_PRON)
3485 ret -= ch->ch_digi.digi_offlen;
3488 if (ret < 0)
3489 ret = 0;
3492 * Schedule FEP to wake us up if needed.
3494 * TODO: This might be overkill...
3495 * Do we really need to schedule callbacks from the FEP
3496 * in every case? Can we get smarter based on ret?
3498 dgap_set_firmware_event(un, UN_LOW | UN_EMPTY);
3499 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
3501 return ret;
3505 * dgap_tty_write()
3507 * Take data from the user or kernel and send it out to the FEP.
3508 * In here exists all the Transparent Print magic as well.
3510 static int dgap_tty_write(struct tty_struct *tty, const unsigned char *buf,
3511 int count)
3513 struct channel_t *ch;
3514 struct un_t *un;
3515 struct bs_t __iomem *bs;
3516 char __iomem *vaddr;
3517 u16 head, tail, tmask, remain;
3518 int bufcount, n;
3519 ulong lock_flags;
3521 if (!tty)
3522 return 0;
3524 un = tty->driver_data;
3525 if (!un || un->magic != DGAP_UNIT_MAGIC)
3526 return 0;
3528 ch = un->un_ch;
3529 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
3530 return 0;
3532 bs = ch->ch_bs;
3533 if (!bs)
3534 return 0;
3536 if (!count)
3537 return 0;
3539 spin_lock_irqsave(&ch->ch_lock, lock_flags);
3541 /* Get our space available for the channel from the board */
3542 tmask = ch->ch_tsize - 1;
3543 head = readw(&(bs->tx_head)) & tmask;
3544 tail = readw(&(bs->tx_tail)) & tmask;
3546 bufcount = tail - head - 1;
3547 if (bufcount < 0)
3548 bufcount += ch->ch_tsize;
3551 * Limit printer output to maxcps overall, with bursts allowed
3552 * up to bufsize characters.
3554 bufcount = dgap_maxcps_room(ch, un, bufcount);
3557 * Take minimum of what the user wants to send, and the
3558 * space available in the FEP buffer.
3560 count = min(count, bufcount);
3563 * Bail if no space left.
3565 if (count <= 0) {
3566 dgap_set_firmware_event(un, UN_LOW | UN_EMPTY);
3567 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
3568 return 0;
3572 * Output the printer ON string, if we are in terminal mode, but
3573 * need to be in printer mode.
3575 if ((un->un_type == DGAP_PRINT) && !(ch->ch_flags & CH_PRON)) {
3576 dgap_wmove(ch, ch->ch_digi.digi_onstr,
3577 (int)ch->ch_digi.digi_onlen);
3578 head = readw(&bs->tx_head) & tmask;
3579 ch->ch_flags |= CH_PRON;
3583 * On the other hand, output the printer OFF string, if we are
3584 * currently in printer mode, but need to output to the terminal.
3586 if ((un->un_type != DGAP_PRINT) && (ch->ch_flags & CH_PRON)) {
3587 dgap_wmove(ch, ch->ch_digi.digi_offstr,
3588 (int)ch->ch_digi.digi_offlen);
3589 head = readw(&bs->tx_head) & tmask;
3590 ch->ch_flags &= ~CH_PRON;
3593 n = count;
3596 * If the write wraps over the top of the circular buffer,
3597 * move the portion up to the wrap point, and reset the
3598 * pointers to the bottom.
3600 remain = ch->ch_tstart + ch->ch_tsize - head;
3602 if (n >= remain) {
3603 n -= remain;
3604 vaddr = ch->ch_taddr + head;
3606 memcpy_toio(vaddr, (u8 *)buf, remain);
3608 head = ch->ch_tstart;
3609 buf += remain;
3612 if (n > 0) {
3614 * Move rest of data.
3616 vaddr = ch->ch_taddr + head;
3617 remain = n;
3619 memcpy_toio(vaddr, (u8 *)buf, remain);
3620 head += remain;
3623 if (count) {
3624 ch->ch_txcount += count;
3625 head &= tmask;
3626 writew(head, &bs->tx_head);
3629 dgap_set_firmware_event(un, UN_LOW | UN_EMPTY);
3632 * If this is the print device, and the
3633 * printer is still on, we need to turn it
3634 * off before going idle. If the buffer is
3635 * non-empty, wait until it goes empty.
3636 * Otherwise turn it off right now.
3638 if ((un->un_type == DGAP_PRINT) && (ch->ch_flags & CH_PRON)) {
3639 tail = readw(&bs->tx_tail) & tmask;
3641 if (tail != head) {
3642 un->un_flags |= UN_EMPTY;
3643 writeb(1, &bs->iempty);
3644 } else {
3645 dgap_wmove(ch, ch->ch_digi.digi_offstr,
3646 (int)ch->ch_digi.digi_offlen);
3647 head = readw(&bs->tx_head) & tmask;
3648 ch->ch_flags &= ~CH_PRON;
3652 /* Update printer buffer empty time. */
3653 if ((un->un_type == DGAP_PRINT) && (ch->ch_digi.digi_maxcps > 0)
3654 && (ch->ch_digi.digi_bufsize > 0)) {
3655 ch->ch_cpstime += (HZ * count) / ch->ch_digi.digi_maxcps;
3658 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
3660 return count;
3664 * dgap_tty_put_char()
3666 * Put a character into ch->ch_buf
3668 * - used by the line discipline for OPOST processing
3670 static int dgap_tty_put_char(struct tty_struct *tty, unsigned char c)
3673 * Simply call tty_write.
3675 dgap_tty_write(tty, &c, 1);
3676 return 1;
3680 * Return modem signals to ld.
3682 static int dgap_tty_tiocmget(struct tty_struct *tty)
3684 struct channel_t *ch;
3685 struct un_t *un;
3686 int result;
3687 u8 mstat;
3688 ulong lock_flags;
3690 if (!tty || tty->magic != TTY_MAGIC)
3691 return -EIO;
3693 un = tty->driver_data;
3694 if (!un || un->magic != DGAP_UNIT_MAGIC)
3695 return -EIO;
3697 ch = un->un_ch;
3698 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
3699 return -EIO;
3701 spin_lock_irqsave(&ch->ch_lock, lock_flags);
3703 mstat = readb(&ch->ch_bs->m_stat);
3704 /* Append any outbound signals that might be pending... */
3705 mstat |= ch->ch_mostat;
3707 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
3709 result = 0;
3711 if (mstat & D_DTR(ch))
3712 result |= TIOCM_DTR;
3713 if (mstat & D_RTS(ch))
3714 result |= TIOCM_RTS;
3715 if (mstat & D_CTS(ch))
3716 result |= TIOCM_CTS;
3717 if (mstat & D_DSR(ch))
3718 result |= TIOCM_DSR;
3719 if (mstat & D_RI(ch))
3720 result |= TIOCM_RI;
3721 if (mstat & D_CD(ch))
3722 result |= TIOCM_CD;
3724 return result;
3728 * dgap_tty_tiocmset()
3730 * Set modem signals, called by ld.
3732 static int dgap_tty_tiocmset(struct tty_struct *tty,
3733 unsigned int set, unsigned int clear)
3735 struct board_t *bd;
3736 struct channel_t *ch;
3737 struct un_t *un;
3738 ulong lock_flags;
3739 ulong lock_flags2;
3741 if (!tty || tty->magic != TTY_MAGIC)
3742 return -EIO;
3744 un = tty->driver_data;
3745 if (!un || un->magic != DGAP_UNIT_MAGIC)
3746 return -EIO;
3748 ch = un->un_ch;
3749 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
3750 return -EIO;
3752 bd = ch->ch_bd;
3753 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
3754 return -EIO;
3756 spin_lock_irqsave(&bd->bd_lock, lock_flags);
3757 spin_lock_irqsave(&ch->ch_lock, lock_flags2);
3759 if (set & TIOCM_RTS) {
3760 ch->ch_mforce |= D_RTS(ch);
3761 ch->ch_mval |= D_RTS(ch);
3764 if (set & TIOCM_DTR) {
3765 ch->ch_mforce |= D_DTR(ch);
3766 ch->ch_mval |= D_DTR(ch);
3769 if (clear & TIOCM_RTS) {
3770 ch->ch_mforce |= D_RTS(ch);
3771 ch->ch_mval &= ~(D_RTS(ch));
3774 if (clear & TIOCM_DTR) {
3775 ch->ch_mforce |= D_DTR(ch);
3776 ch->ch_mval &= ~(D_DTR(ch));
3779 dgap_param(ch, bd, un->un_type);
3781 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
3782 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
3784 return 0;
3788 * dgap_tty_send_break()
3790 * Send a Break, called by ld.
3792 static int dgap_tty_send_break(struct tty_struct *tty, int msec)
3794 struct board_t *bd;
3795 struct channel_t *ch;
3796 struct un_t *un;
3797 ulong lock_flags;
3798 ulong lock_flags2;
3800 if (!tty || tty->magic != TTY_MAGIC)
3801 return -EIO;
3803 un = tty->driver_data;
3804 if (!un || un->magic != DGAP_UNIT_MAGIC)
3805 return -EIO;
3807 ch = un->un_ch;
3808 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
3809 return -EIO;
3811 bd = ch->ch_bd;
3812 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
3813 return -EIO;
3815 switch (msec) {
3816 case -1:
3817 msec = 0xFFFF;
3818 break;
3819 case 0:
3820 msec = 1;
3821 break;
3822 default:
3823 msec /= 10;
3824 break;
3827 spin_lock_irqsave(&bd->bd_lock, lock_flags);
3828 spin_lock_irqsave(&ch->ch_lock, lock_flags2);
3829 #if 0
3830 dgap_cmdw(ch, SBREAK, (u16) SBREAK_TIME, 0);
3831 #endif
3832 dgap_cmdw(ch, SBREAK, (u16)msec, 0);
3834 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
3835 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
3837 return 0;
3841 * dgap_tty_wait_until_sent()
3843 * wait until data has been transmitted, called by ld.
3845 static void dgap_tty_wait_until_sent(struct tty_struct *tty, int timeout)
3847 dgap_wait_for_drain(tty);
3851 * dgap_send_xchar()
3853 * send a high priority character, called by ld.
3855 static void dgap_tty_send_xchar(struct tty_struct *tty, char c)
3857 struct board_t *bd;
3858 struct channel_t *ch;
3859 struct un_t *un;
3860 ulong lock_flags;
3861 ulong lock_flags2;
3863 if (!tty || tty->magic != TTY_MAGIC)
3864 return;
3866 un = tty->driver_data;
3867 if (!un || un->magic != DGAP_UNIT_MAGIC)
3868 return;
3870 ch = un->un_ch;
3871 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
3872 return;
3874 bd = ch->ch_bd;
3875 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
3876 return;
3878 spin_lock_irqsave(&bd->bd_lock, lock_flags);
3879 spin_lock_irqsave(&ch->ch_lock, lock_flags2);
3882 * This is technically what we should do.
3883 * However, the NIST tests specifically want
3884 * to see each XON or XOFF character that it
3885 * sends, so lets just send each character
3886 * by hand...
3888 #if 0
3889 if (c == STOP_CHAR(tty))
3890 dgap_cmdw(ch, RPAUSE, 0, 0);
3891 else if (c == START_CHAR(tty))
3892 dgap_cmdw(ch, RRESUME, 0, 0);
3893 else
3894 dgap_wmove(ch, &c, 1);
3895 #else
3896 dgap_wmove(ch, &c, 1);
3897 #endif
3899 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
3900 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
3904 * Return modem signals to ld.
3906 static int dgap_get_modem_info(struct channel_t *ch, unsigned int __user *value)
3908 int result;
3909 u8 mstat;
3910 ulong lock_flags;
3912 spin_lock_irqsave(&ch->ch_lock, lock_flags);
3914 mstat = readb(&ch->ch_bs->m_stat);
3915 /* Append any outbound signals that might be pending... */
3916 mstat |= ch->ch_mostat;
3918 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
3920 result = 0;
3922 if (mstat & D_DTR(ch))
3923 result |= TIOCM_DTR;
3924 if (mstat & D_RTS(ch))
3925 result |= TIOCM_RTS;
3926 if (mstat & D_CTS(ch))
3927 result |= TIOCM_CTS;
3928 if (mstat & D_DSR(ch))
3929 result |= TIOCM_DSR;
3930 if (mstat & D_RI(ch))
3931 result |= TIOCM_RI;
3932 if (mstat & D_CD(ch))
3933 result |= TIOCM_CD;
3935 return put_user(result, value);
3939 * dgap_set_modem_info()
3941 * Set modem signals, called by ld.
3943 static int dgap_set_modem_info(struct channel_t *ch, struct board_t *bd,
3944 struct un_t *un, unsigned int command,
3945 unsigned int __user *value)
3947 int ret;
3948 unsigned int arg;
3949 ulong lock_flags;
3950 ulong lock_flags2;
3952 ret = get_user(arg, value);
3953 if (ret)
3954 return ret;
3956 switch (command) {
3957 case TIOCMBIS:
3958 if (arg & TIOCM_RTS) {
3959 ch->ch_mforce |= D_RTS(ch);
3960 ch->ch_mval |= D_RTS(ch);
3963 if (arg & TIOCM_DTR) {
3964 ch->ch_mforce |= D_DTR(ch);
3965 ch->ch_mval |= D_DTR(ch);
3968 break;
3970 case TIOCMBIC:
3971 if (arg & TIOCM_RTS) {
3972 ch->ch_mforce |= D_RTS(ch);
3973 ch->ch_mval &= ~(D_RTS(ch));
3976 if (arg & TIOCM_DTR) {
3977 ch->ch_mforce |= D_DTR(ch);
3978 ch->ch_mval &= ~(D_DTR(ch));
3981 break;
3983 case TIOCMSET:
3984 ch->ch_mforce = D_DTR(ch) | D_RTS(ch);
3986 if (arg & TIOCM_RTS)
3987 ch->ch_mval |= D_RTS(ch);
3988 else
3989 ch->ch_mval &= ~(D_RTS(ch));
3991 if (arg & TIOCM_DTR)
3992 ch->ch_mval |= (D_DTR(ch));
3993 else
3994 ch->ch_mval &= ~(D_DTR(ch));
3996 break;
3998 default:
3999 return -EINVAL;
4002 spin_lock_irqsave(&bd->bd_lock, lock_flags);
4003 spin_lock_irqsave(&ch->ch_lock, lock_flags2);
4005 dgap_param(ch, bd, un->un_type);
4007 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4008 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
4010 return 0;
4014 * dgap_tty_digigeta()
4016 * Ioctl to get the information for ditty.
4021 static int dgap_tty_digigeta(struct channel_t *ch,
4022 struct digi_t __user *retinfo)
4024 struct digi_t tmp;
4025 ulong lock_flags;
4027 if (!retinfo)
4028 return -EFAULT;
4030 memset(&tmp, 0, sizeof(tmp));
4032 spin_lock_irqsave(&ch->ch_lock, lock_flags);
4033 memcpy(&tmp, &ch->ch_digi, sizeof(tmp));
4034 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
4036 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
4037 return -EFAULT;
4039 return 0;
4043 * dgap_tty_digiseta()
4045 * Ioctl to set the information for ditty.
4050 static int dgap_tty_digiseta(struct channel_t *ch, struct board_t *bd,
4051 struct un_t *un, struct digi_t __user *new_info)
4053 struct digi_t new_digi;
4054 ulong lock_flags = 0;
4055 unsigned long lock_flags2;
4057 if (copy_from_user(&new_digi, new_info, sizeof(struct digi_t)))
4058 return -EFAULT;
4060 spin_lock_irqsave(&bd->bd_lock, lock_flags);
4061 spin_lock_irqsave(&ch->ch_lock, lock_flags2);
4063 memcpy(&ch->ch_digi, &new_digi, sizeof(struct digi_t));
4065 if (ch->ch_digi.digi_maxcps < 1)
4066 ch->ch_digi.digi_maxcps = 1;
4068 if (ch->ch_digi.digi_maxcps > 10000)
4069 ch->ch_digi.digi_maxcps = 10000;
4071 if (ch->ch_digi.digi_bufsize < 10)
4072 ch->ch_digi.digi_bufsize = 10;
4074 if (ch->ch_digi.digi_maxchar < 1)
4075 ch->ch_digi.digi_maxchar = 1;
4077 if (ch->ch_digi.digi_maxchar > ch->ch_digi.digi_bufsize)
4078 ch->ch_digi.digi_maxchar = ch->ch_digi.digi_bufsize;
4080 if (ch->ch_digi.digi_onlen > DIGI_PLEN)
4081 ch->ch_digi.digi_onlen = DIGI_PLEN;
4083 if (ch->ch_digi.digi_offlen > DIGI_PLEN)
4084 ch->ch_digi.digi_offlen = DIGI_PLEN;
4086 dgap_param(ch, bd, un->un_type);
4088 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4089 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
4091 return 0;
4095 * dgap_tty_digigetedelay()
4097 * Ioctl to get the current edelay setting.
4102 static int dgap_tty_digigetedelay(struct tty_struct *tty, int __user *retinfo)
4104 struct channel_t *ch;
4105 struct un_t *un;
4106 int tmp;
4107 ulong lock_flags;
4109 if (!retinfo)
4110 return -EFAULT;
4112 if (!tty || tty->magic != TTY_MAGIC)
4113 return -EFAULT;
4115 un = tty->driver_data;
4116 if (!un || un->magic != DGAP_UNIT_MAGIC)
4117 return -EFAULT;
4119 ch = un->un_ch;
4120 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
4121 return -EFAULT;
4123 memset(&tmp, 0, sizeof(tmp));
4125 spin_lock_irqsave(&ch->ch_lock, lock_flags);
4126 tmp = readw(&ch->ch_bs->edelay);
4127 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
4129 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
4130 return -EFAULT;
4132 return 0;
4136 * dgap_tty_digisetedelay()
4138 * Ioctl to set the EDELAY setting
4141 static int dgap_tty_digisetedelay(struct channel_t *ch, struct board_t *bd,
4142 struct un_t *un, int __user *new_info)
4144 int new_digi;
4145 ulong lock_flags;
4146 ulong lock_flags2;
4148 if (copy_from_user(&new_digi, new_info, sizeof(int)))
4149 return -EFAULT;
4151 spin_lock_irqsave(&bd->bd_lock, lock_flags);
4152 spin_lock_irqsave(&ch->ch_lock, lock_flags2);
4154 writew((u16)new_digi, &ch->ch_bs->edelay);
4156 dgap_param(ch, bd, un->un_type);
4158 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4159 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
4161 return 0;
4165 * dgap_tty_digigetcustombaud()
4167 * Ioctl to get the current custom baud rate setting.
4169 static int dgap_tty_digigetcustombaud(struct channel_t *ch, struct un_t *un,
4170 int __user *retinfo)
4172 int tmp;
4173 ulong lock_flags;
4175 if (!retinfo)
4176 return -EFAULT;
4178 memset(&tmp, 0, sizeof(tmp));
4180 spin_lock_irqsave(&ch->ch_lock, lock_flags);
4181 tmp = dgap_get_custom_baud(ch);
4182 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
4184 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
4185 return -EFAULT;
4187 return 0;
4191 * dgap_tty_digisetcustombaud()
4193 * Ioctl to set the custom baud rate setting
4195 static int dgap_tty_digisetcustombaud(struct channel_t *ch, struct board_t *bd,
4196 struct un_t *un, int __user *new_info)
4198 uint new_rate;
4199 ulong lock_flags;
4200 ulong lock_flags2;
4202 if (copy_from_user(&new_rate, new_info, sizeof(unsigned int)))
4203 return -EFAULT;
4205 if (bd->bd_flags & BD_FEP5PLUS) {
4206 spin_lock_irqsave(&bd->bd_lock, lock_flags);
4207 spin_lock_irqsave(&ch->ch_lock, lock_flags2);
4209 ch->ch_custom_speed = new_rate;
4211 dgap_param(ch, bd, un->un_type);
4213 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4214 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
4217 return 0;
4221 * dgap_set_termios()
4223 static void dgap_tty_set_termios(struct tty_struct *tty,
4224 struct ktermios *old_termios)
4226 struct board_t *bd;
4227 struct channel_t *ch;
4228 struct un_t *un;
4229 unsigned long lock_flags;
4230 unsigned long lock_flags2;
4232 if (!tty || tty->magic != TTY_MAGIC)
4233 return;
4235 un = tty->driver_data;
4236 if (!un || un->magic != DGAP_UNIT_MAGIC)
4237 return;
4239 ch = un->un_ch;
4240 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
4241 return;
4243 bd = ch->ch_bd;
4244 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
4245 return;
4247 spin_lock_irqsave(&bd->bd_lock, lock_flags);
4248 spin_lock_irqsave(&ch->ch_lock, lock_flags2);
4250 ch->ch_c_cflag = tty->termios.c_cflag;
4251 ch->ch_c_iflag = tty->termios.c_iflag;
4252 ch->ch_c_oflag = tty->termios.c_oflag;
4253 ch->ch_c_lflag = tty->termios.c_lflag;
4254 ch->ch_startc = tty->termios.c_cc[VSTART];
4255 ch->ch_stopc = tty->termios.c_cc[VSTOP];
4257 dgap_carrier(ch);
4258 dgap_param(ch, bd, un->un_type);
4260 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4261 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
4264 static void dgap_tty_throttle(struct tty_struct *tty)
4266 struct board_t *bd;
4267 struct channel_t *ch;
4268 struct un_t *un;
4269 ulong lock_flags;
4270 ulong lock_flags2;
4272 if (!tty || tty->magic != TTY_MAGIC)
4273 return;
4275 un = tty->driver_data;
4276 if (!un || un->magic != DGAP_UNIT_MAGIC)
4277 return;
4279 ch = un->un_ch;
4280 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
4281 return;
4283 bd = ch->ch_bd;
4284 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
4285 return;
4287 spin_lock_irqsave(&bd->bd_lock, lock_flags);
4288 spin_lock_irqsave(&ch->ch_lock, lock_flags2);
4290 ch->ch_flags |= (CH_RXBLOCK);
4291 #if 1
4292 dgap_cmdw(ch, RPAUSE, 0, 0);
4293 #endif
4295 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4296 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
4299 static void dgap_tty_unthrottle(struct tty_struct *tty)
4301 struct board_t *bd;
4302 struct channel_t *ch;
4303 struct un_t *un;
4304 ulong lock_flags;
4305 ulong lock_flags2;
4307 if (!tty || tty->magic != TTY_MAGIC)
4308 return;
4310 un = tty->driver_data;
4311 if (!un || un->magic != DGAP_UNIT_MAGIC)
4312 return;
4314 ch = un->un_ch;
4315 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
4316 return;
4318 bd = ch->ch_bd;
4319 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
4320 return;
4322 spin_lock_irqsave(&bd->bd_lock, lock_flags);
4323 spin_lock_irqsave(&ch->ch_lock, lock_flags2);
4325 ch->ch_flags &= ~(CH_RXBLOCK);
4327 #if 1
4328 dgap_cmdw(ch, RRESUME, 0, 0);
4329 #endif
4331 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4332 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
4335 static struct board_t *find_board_by_major(unsigned int major)
4337 unsigned int i;
4339 for (i = 0; i < MAXBOARDS; i++) {
4340 struct board_t *brd = dgap_board[i];
4342 if (!brd)
4343 return NULL;
4344 if (major == brd->serial_driver->major ||
4345 major == brd->print_driver->major)
4346 return brd;
4349 return NULL;
4352 /************************************************************************
4354 * TTY Entry points and helper functions
4356 ************************************************************************/
4359 * dgap_tty_open()
4362 static int dgap_tty_open(struct tty_struct *tty, struct file *file)
4364 struct board_t *brd;
4365 struct channel_t *ch;
4366 struct un_t *un;
4367 struct bs_t __iomem *bs;
4368 uint major;
4369 uint minor;
4370 int rc;
4371 ulong lock_flags;
4372 ulong lock_flags2;
4373 u16 head;
4375 major = MAJOR(tty_devnum(tty));
4376 minor = MINOR(tty_devnum(tty));
4378 brd = find_board_by_major(major);
4379 if (!brd)
4380 return -EIO;
4383 * If board is not yet up to a state of READY, go to
4384 * sleep waiting for it to happen or they cancel the open.
4386 rc = wait_event_interruptible(brd->state_wait,
4387 (brd->state & BOARD_READY));
4389 if (rc)
4390 return rc;
4392 spin_lock_irqsave(&brd->bd_lock, lock_flags);
4394 /* The wait above should guarantee this cannot happen */
4395 if (brd->state != BOARD_READY) {
4396 spin_unlock_irqrestore(&brd->bd_lock, lock_flags);
4397 return -EIO;
4400 /* If opened device is greater than our number of ports, bail. */
4401 if (MINOR(tty_devnum(tty)) > brd->nasync) {
4402 spin_unlock_irqrestore(&brd->bd_lock, lock_flags);
4403 return -EIO;
4406 ch = brd->channels[minor];
4407 if (!ch) {
4408 spin_unlock_irqrestore(&brd->bd_lock, lock_flags);
4409 return -EIO;
4412 /* Grab channel lock */
4413 spin_lock_irqsave(&ch->ch_lock, lock_flags2);
4415 /* Figure out our type */
4416 if (major == brd->serial_driver->major) {
4417 un = &brd->channels[minor]->ch_tun;
4418 un->un_type = DGAP_SERIAL;
4419 } else if (major == brd->print_driver->major) {
4420 un = &brd->channels[minor]->ch_pun;
4421 un->un_type = DGAP_PRINT;
4422 } else {
4423 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4424 spin_unlock_irqrestore(&brd->bd_lock, lock_flags);
4425 return -EIO;
4428 /* Store our unit into driver_data, so we always have it available. */
4429 tty->driver_data = un;
4432 * Error if channel info pointer is NULL.
4434 bs = ch->ch_bs;
4435 if (!bs) {
4436 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4437 spin_unlock_irqrestore(&brd->bd_lock, lock_flags);
4438 return -EIO;
4442 * Initialize tty's
4444 if (!(un->un_flags & UN_ISOPEN)) {
4445 /* Store important variables. */
4446 un->un_tty = tty;
4448 /* Maybe do something here to the TTY struct as well? */
4452 * Initialize if neither terminal or printer is open.
4454 if (!((ch->ch_tun.un_flags | ch->ch_pun.un_flags) & UN_ISOPEN)) {
4455 ch->ch_mforce = 0;
4456 ch->ch_mval = 0;
4459 * Flush input queue.
4461 head = readw(&bs->rx_head);
4462 writew(head, &bs->rx_tail);
4464 ch->ch_flags = 0;
4465 ch->pscan_state = 0;
4466 ch->pscan_savechar = 0;
4468 ch->ch_c_cflag = tty->termios.c_cflag;
4469 ch->ch_c_iflag = tty->termios.c_iflag;
4470 ch->ch_c_oflag = tty->termios.c_oflag;
4471 ch->ch_c_lflag = tty->termios.c_lflag;
4472 ch->ch_startc = tty->termios.c_cc[VSTART];
4473 ch->ch_stopc = tty->termios.c_cc[VSTOP];
4475 /* TODO: flush our TTY struct here? */
4478 dgap_carrier(ch);
4480 * Run param in case we changed anything
4482 dgap_param(ch, brd, un->un_type);
4485 * follow protocol for opening port
4488 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4489 spin_unlock_irqrestore(&brd->bd_lock, lock_flags);
4491 rc = dgap_block_til_ready(tty, file, ch);
4493 if (!un->un_tty)
4494 return -ENODEV;
4496 /* No going back now, increment our unit and channel counters */
4497 spin_lock_irqsave(&ch->ch_lock, lock_flags);
4498 ch->ch_open_count++;
4499 un->un_open_count++;
4500 un->un_flags |= (UN_ISOPEN);
4501 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
4503 return rc;
4507 * dgap_tty_close()
4510 static void dgap_tty_close(struct tty_struct *tty, struct file *file)
4512 struct board_t *bd;
4513 struct channel_t *ch;
4514 struct un_t *un;
4515 ulong lock_flags;
4517 if (!tty || tty->magic != TTY_MAGIC)
4518 return;
4520 un = tty->driver_data;
4521 if (!un || un->magic != DGAP_UNIT_MAGIC)
4522 return;
4524 ch = un->un_ch;
4525 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
4526 return;
4528 bd = ch->ch_bd;
4529 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
4530 return;
4532 spin_lock_irqsave(&ch->ch_lock, lock_flags);
4535 * Determine if this is the last close or not - and if we agree about
4536 * which type of close it is with the Line Discipline
4538 if ((tty->count == 1) && (un->un_open_count != 1)) {
4540 * Uh, oh. tty->count is 1, which means that the tty
4541 * structure will be freed. un_open_count should always
4542 * be one in these conditions. If it's greater than
4543 * one, we've got real problems, since it means the
4544 * serial port won't be shutdown.
4546 un->un_open_count = 1;
4549 if (--un->un_open_count < 0)
4550 un->un_open_count = 0;
4552 ch->ch_open_count--;
4554 if (ch->ch_open_count && un->un_open_count) {
4555 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
4556 return;
4559 /* OK, its the last close on the unit */
4561 un->un_flags |= UN_CLOSING;
4563 tty->closing = 1;
4566 * Only officially close channel if count is 0 and
4567 * DIGI_PRINTER bit is not set.
4569 if ((ch->ch_open_count == 0) &&
4570 !(ch->ch_digi.digi_flags & DIGI_PRINTER)) {
4571 ch->ch_flags &= ~(CH_RXBLOCK);
4573 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
4575 /* wait for output to drain */
4576 /* This will also return if we take an interrupt */
4578 dgap_wait_for_drain(tty);
4580 dgap_tty_flush_buffer(tty);
4581 tty_ldisc_flush(tty);
4583 spin_lock_irqsave(&ch->ch_lock, lock_flags);
4585 tty->closing = 0;
4588 * If we have HUPCL set, lower DTR and RTS
4590 if (ch->ch_c_cflag & HUPCL) {
4591 ch->ch_mostat &= ~(D_RTS(ch) | D_DTR(ch));
4592 dgap_cmdb(ch, SMODEM, 0, D_DTR(ch) | D_RTS(ch), 0);
4595 * Go to sleep to ensure RTS/DTR
4596 * have been dropped for modems to see it.
4598 spin_unlock_irqrestore(&ch->ch_lock,
4599 lock_flags);
4601 /* .25 second delay for dropping RTS/DTR */
4602 schedule_timeout_interruptible(msecs_to_jiffies(250));
4604 spin_lock_irqsave(&ch->ch_lock, lock_flags);
4607 ch->pscan_state = 0;
4608 ch->pscan_savechar = 0;
4609 ch->ch_baud_info = 0;
4613 * turn off print device when closing print device.
4615 if ((un->un_type == DGAP_PRINT) && (ch->ch_flags & CH_PRON)) {
4616 dgap_wmove(ch, ch->ch_digi.digi_offstr,
4617 (int)ch->ch_digi.digi_offlen);
4618 ch->ch_flags &= ~CH_PRON;
4621 un->un_tty = NULL;
4622 un->un_flags &= ~(UN_ISOPEN | UN_CLOSING);
4623 tty->driver_data = NULL;
4625 wake_up_interruptible(&ch->ch_flags_wait);
4626 wake_up_interruptible(&un->un_flags_wait);
4628 spin_unlock_irqrestore(&ch->ch_lock, lock_flags);
4631 static void dgap_tty_start(struct tty_struct *tty)
4633 struct board_t *bd;
4634 struct channel_t *ch;
4635 struct un_t *un;
4636 ulong lock_flags;
4637 ulong lock_flags2;
4639 if (!tty || tty->magic != TTY_MAGIC)
4640 return;
4642 un = tty->driver_data;
4643 if (!un || un->magic != DGAP_UNIT_MAGIC)
4644 return;
4646 ch = un->un_ch;
4647 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
4648 return;
4650 bd = ch->ch_bd;
4651 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
4652 return;
4654 spin_lock_irqsave(&bd->bd_lock, lock_flags);
4655 spin_lock_irqsave(&ch->ch_lock, lock_flags2);
4657 dgap_cmdw(ch, RESUMETX, 0, 0);
4659 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4660 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
4663 static void dgap_tty_stop(struct tty_struct *tty)
4665 struct board_t *bd;
4666 struct channel_t *ch;
4667 struct un_t *un;
4668 ulong lock_flags;
4669 ulong lock_flags2;
4671 if (!tty || tty->magic != TTY_MAGIC)
4672 return;
4674 un = tty->driver_data;
4675 if (!un || un->magic != DGAP_UNIT_MAGIC)
4676 return;
4678 ch = un->un_ch;
4679 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
4680 return;
4682 bd = ch->ch_bd;
4683 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
4684 return;
4686 spin_lock_irqsave(&bd->bd_lock, lock_flags);
4687 spin_lock_irqsave(&ch->ch_lock, lock_flags2);
4689 dgap_cmdw(ch, PAUSETX, 0, 0);
4691 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4692 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
4696 * dgap_tty_flush_chars()
4698 * Flush the cook buffer
4700 * Note to self, and any other poor souls who venture here:
4702 * flush in this case DOES NOT mean dispose of the data.
4703 * instead, it means "stop buffering and send it if you
4704 * haven't already." Just guess how I figured that out... SRW 2-Jun-98
4706 * It is also always called in interrupt context - JAR 8-Sept-99
4708 static void dgap_tty_flush_chars(struct tty_struct *tty)
4710 struct board_t *bd;
4711 struct channel_t *ch;
4712 struct un_t *un;
4713 ulong lock_flags;
4714 ulong lock_flags2;
4716 if (!tty || tty->magic != TTY_MAGIC)
4717 return;
4719 un = tty->driver_data;
4720 if (!un || un->magic != DGAP_UNIT_MAGIC)
4721 return;
4723 ch = un->un_ch;
4724 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
4725 return;
4727 bd = ch->ch_bd;
4728 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
4729 return;
4731 spin_lock_irqsave(&bd->bd_lock, lock_flags);
4732 spin_lock_irqsave(&ch->ch_lock, lock_flags2);
4734 /* TODO: Do something here */
4736 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4737 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
4740 /*****************************************************************************
4742 * The IOCTL function and all of its helpers
4744 *****************************************************************************/
4747 * dgap_tty_ioctl()
4749 * The usual assortment of ioctl's
4751 static int dgap_tty_ioctl(struct tty_struct *tty, unsigned int cmd,
4752 unsigned long arg)
4754 struct board_t *bd;
4755 struct channel_t *ch;
4756 struct un_t *un;
4757 int rc;
4758 u16 head;
4759 ulong lock_flags = 0;
4760 ulong lock_flags2 = 0;
4761 void __user *uarg = (void __user *)arg;
4763 if (!tty || tty->magic != TTY_MAGIC)
4764 return -ENODEV;
4766 un = tty->driver_data;
4767 if (!un || un->magic != DGAP_UNIT_MAGIC)
4768 return -ENODEV;
4770 ch = un->un_ch;
4771 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
4772 return -ENODEV;
4774 bd = ch->ch_bd;
4775 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
4776 return -ENODEV;
4778 spin_lock_irqsave(&bd->bd_lock, lock_flags);
4779 spin_lock_irqsave(&ch->ch_lock, lock_flags2);
4781 if (un->un_open_count <= 0) {
4782 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4783 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
4784 return -EIO;
4787 switch (cmd) {
4788 /* Here are all the standard ioctl's that we MUST implement */
4789 case TCSBRK:
4791 * TCSBRK is SVID version: non-zero arg --> no break
4792 * this behaviour is exploited by tcdrain().
4794 * According to POSIX.1 spec (7.2.2.1.2) breaks should be
4795 * between 0.25 and 0.5 seconds so we'll ask for something
4796 * in the middle: 0.375 seconds.
4798 rc = tty_check_change(tty);
4799 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4800 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
4801 if (rc)
4802 return rc;
4804 rc = dgap_wait_for_drain(tty);
4806 if (rc)
4807 return -EINTR;
4809 spin_lock_irqsave(&bd->bd_lock, lock_flags);
4810 spin_lock_irqsave(&ch->ch_lock, lock_flags2);
4812 if (((cmd == TCSBRK) && (!arg)) || (cmd == TCSBRKP))
4813 dgap_cmdw(ch, SBREAK, (u16)SBREAK_TIME, 0);
4815 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4816 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
4818 return 0;
4820 case TCSBRKP:
4821 /* support for POSIX tcsendbreak()
4823 * According to POSIX.1 spec (7.2.2.1.2) breaks should be
4824 * between 0.25 and 0.5 seconds so we'll ask for something
4825 * in the middle: 0.375 seconds.
4827 rc = tty_check_change(tty);
4828 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4829 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
4830 if (rc)
4831 return rc;
4833 rc = dgap_wait_for_drain(tty);
4834 if (rc)
4835 return -EINTR;
4837 spin_lock_irqsave(&bd->bd_lock, lock_flags);
4838 spin_lock_irqsave(&ch->ch_lock, lock_flags2);
4840 dgap_cmdw(ch, SBREAK, (u16)SBREAK_TIME, 0);
4842 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4843 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
4845 return 0;
4847 case TIOCSBRK:
4849 * FEP5 doesn't support turning on a break unconditionally.
4850 * The FEP5 device will stop sending a break automatically
4851 * after the specified time value that was sent when turning on
4852 * the break.
4854 rc = tty_check_change(tty);
4855 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4856 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
4857 if (rc)
4858 return rc;
4860 rc = dgap_wait_for_drain(tty);
4861 if (rc)
4862 return -EINTR;
4864 spin_lock_irqsave(&bd->bd_lock, lock_flags);
4865 spin_lock_irqsave(&ch->ch_lock, lock_flags2);
4867 dgap_cmdw(ch, SBREAK, (u16)SBREAK_TIME, 0);
4869 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4870 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
4872 return 0;
4874 case TIOCCBRK:
4876 * FEP5 doesn't support turning off a break unconditionally.
4877 * The FEP5 device will stop sending a break automatically
4878 * after the specified time value that was sent when turning on
4879 * the break.
4881 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4882 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
4883 return 0;
4885 case TIOCGSOFTCAR:
4887 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4888 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
4890 return put_user(C_CLOCAL(tty) ? 1 : 0,
4891 (unsigned long __user *)arg);
4893 case TIOCSSOFTCAR:
4894 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4895 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
4897 rc = get_user(arg, (unsigned long __user *)arg);
4898 if (rc)
4899 return rc;
4901 spin_lock_irqsave(&bd->bd_lock, lock_flags);
4902 spin_lock_irqsave(&ch->ch_lock, lock_flags2);
4903 tty->termios.c_cflag = ((tty->termios.c_cflag & ~CLOCAL) |
4904 (arg ? CLOCAL : 0));
4905 dgap_param(ch, bd, un->un_type);
4906 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4907 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
4909 return 0;
4911 case TIOCMGET:
4912 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4913 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
4914 return dgap_get_modem_info(ch, uarg);
4916 case TIOCMBIS:
4917 case TIOCMBIC:
4918 case TIOCMSET:
4919 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4920 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
4921 return dgap_set_modem_info(ch, bd, un, cmd, uarg);
4924 * Here are any additional ioctl's that we want to implement
4927 case TCFLSH:
4929 * The linux tty driver doesn't have a flush
4930 * input routine for the driver, assuming all backed
4931 * up data is in the line disc. buffers. However,
4932 * we all know that's not the case. Here, we
4933 * act on the ioctl, but then lie and say we didn't
4934 * so the line discipline will process the flush
4935 * also.
4937 rc = tty_check_change(tty);
4938 if (rc) {
4939 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4940 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
4941 return rc;
4944 if ((arg == TCIFLUSH) || (arg == TCIOFLUSH)) {
4945 if (!(un->un_type == DGAP_PRINT)) {
4946 head = readw(&ch->ch_bs->rx_head);
4947 writew(head, &ch->ch_bs->rx_tail);
4948 writeb(0, &ch->ch_bs->orun);
4952 if ((arg != TCOFLUSH) && (arg != TCIOFLUSH)) {
4953 /* pretend we didn't recognize this IOCTL */
4954 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4955 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
4957 return -ENOIOCTLCMD;
4960 ch->ch_flags &= ~CH_STOP;
4961 head = readw(&ch->ch_bs->tx_head);
4962 dgap_cmdw(ch, FLUSHTX, (u16)head, 0);
4963 dgap_cmdw(ch, RESUMETX, 0, 0);
4964 if (ch->ch_tun.un_flags & (UN_LOW | UN_EMPTY)) {
4965 ch->ch_tun.un_flags &= ~(UN_LOW | UN_EMPTY);
4966 wake_up_interruptible(&ch->ch_tun.un_flags_wait);
4968 if (ch->ch_pun.un_flags & (UN_LOW | UN_EMPTY)) {
4969 ch->ch_pun.un_flags &= ~(UN_LOW | UN_EMPTY);
4970 wake_up_interruptible(&ch->ch_pun.un_flags_wait);
4972 if (waitqueue_active(&tty->write_wait))
4973 wake_up_interruptible(&tty->write_wait);
4975 /* Can't hold any locks when calling tty_wakeup! */
4976 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
4977 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
4978 tty_wakeup(tty);
4980 /* pretend we didn't recognize this IOCTL */
4981 return -ENOIOCTLCMD;
4983 case TCSETSF:
4984 case TCSETSW:
4986 * The linux tty driver doesn't have a flush
4987 * input routine for the driver, assuming all backed
4988 * up data is in the line disc. buffers. However,
4989 * we all know that's not the case. Here, we
4990 * act on the ioctl, but then lie and say we didn't
4991 * so the line discipline will process the flush
4992 * also.
4994 if (cmd == TCSETSF) {
4995 /* flush rx */
4996 ch->ch_flags &= ~CH_STOP;
4997 head = readw(&ch->ch_bs->rx_head);
4998 writew(head, &ch->ch_bs->rx_tail);
5001 /* now wait for all the output to drain */
5002 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
5003 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
5004 rc = dgap_wait_for_drain(tty);
5005 if (rc)
5006 return -EINTR;
5008 /* pretend we didn't recognize this */
5009 return -ENOIOCTLCMD;
5011 case TCSETAW:
5013 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
5014 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
5015 rc = dgap_wait_for_drain(tty);
5016 if (rc)
5017 return -EINTR;
5019 /* pretend we didn't recognize this */
5020 return -ENOIOCTLCMD;
5022 case TCXONC:
5024 * The Linux Line Discipline (LD) would do this for us if we
5025 * let it, but we have the special firmware options to do this
5026 * the "right way" regardless of hardware or software flow
5027 * control so we'll do it outselves instead of letting the LD
5028 * do it.
5030 rc = tty_check_change(tty);
5031 if (rc) {
5032 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
5033 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
5034 return rc;
5037 switch (arg) {
5038 case TCOON:
5039 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
5040 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
5041 dgap_tty_start(tty);
5042 return 0;
5043 case TCOOFF:
5044 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
5045 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
5046 dgap_tty_stop(tty);
5047 return 0;
5048 case TCION:
5049 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
5050 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
5051 /* Make the ld do it */
5052 return -ENOIOCTLCMD;
5053 case TCIOFF:
5054 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
5055 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
5056 /* Make the ld do it */
5057 return -ENOIOCTLCMD;
5058 default:
5059 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
5060 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
5061 return -EINVAL;
5064 case DIGI_GETA:
5065 /* get information for ditty */
5066 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
5067 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
5068 return dgap_tty_digigeta(ch, uarg);
5070 case DIGI_SETAW:
5071 case DIGI_SETAF:
5073 /* set information for ditty */
5074 if (cmd == (DIGI_SETAW)) {
5075 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
5076 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
5077 rc = dgap_wait_for_drain(tty);
5078 if (rc)
5079 return -EINTR;
5080 spin_lock_irqsave(&bd->bd_lock, lock_flags);
5081 spin_lock_irqsave(&ch->ch_lock, lock_flags2);
5082 } else
5083 tty_ldisc_flush(tty);
5084 /* fall thru */
5086 case DIGI_SETA:
5087 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
5088 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
5089 return dgap_tty_digiseta(ch, bd, un, uarg);
5091 case DIGI_GEDELAY:
5092 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
5093 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
5094 return dgap_tty_digigetedelay(tty, uarg);
5096 case DIGI_SEDELAY:
5097 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
5098 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
5099 return dgap_tty_digisetedelay(ch, bd, un, uarg);
5101 case DIGI_GETCUSTOMBAUD:
5102 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
5103 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
5104 return dgap_tty_digigetcustombaud(ch, un, uarg);
5106 case DIGI_SETCUSTOMBAUD:
5107 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
5108 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
5109 return dgap_tty_digisetcustombaud(ch, bd, un, uarg);
5111 case DIGI_RESET_PORT:
5112 dgap_firmware_reset_port(ch);
5113 dgap_param(ch, bd, un->un_type);
5114 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
5115 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
5116 return 0;
5118 default:
5119 spin_unlock_irqrestore(&ch->ch_lock, lock_flags2);
5120 spin_unlock_irqrestore(&bd->bd_lock, lock_flags);
5122 return -ENOIOCTLCMD;
5126 static const struct tty_operations dgap_tty_ops = {
5127 .open = dgap_tty_open,
5128 .close = dgap_tty_close,
5129 .write = dgap_tty_write,
5130 .write_room = dgap_tty_write_room,
5131 .flush_buffer = dgap_tty_flush_buffer,
5132 .chars_in_buffer = dgap_tty_chars_in_buffer,
5133 .flush_chars = dgap_tty_flush_chars,
5134 .ioctl = dgap_tty_ioctl,
5135 .set_termios = dgap_tty_set_termios,
5136 .stop = dgap_tty_stop,
5137 .start = dgap_tty_start,
5138 .throttle = dgap_tty_throttle,
5139 .unthrottle = dgap_tty_unthrottle,
5140 .hangup = dgap_tty_hangup,
5141 .put_char = dgap_tty_put_char,
5142 .tiocmget = dgap_tty_tiocmget,
5143 .tiocmset = dgap_tty_tiocmset,
5144 .break_ctl = dgap_tty_send_break,
5145 .wait_until_sent = dgap_tty_wait_until_sent,
5146 .send_xchar = dgap_tty_send_xchar
5149 /************************************************************************
5151 * TTY Initialization/Cleanup Functions
5153 ************************************************************************/
5156 * dgap_tty_register()
5158 * Init the tty subsystem for this board.
5160 static int dgap_tty_register(struct board_t *brd)
5162 int rc;
5164 brd->serial_driver = tty_alloc_driver(MAXPORTS,
5165 TTY_DRIVER_REAL_RAW |
5166 TTY_DRIVER_DYNAMIC_DEV |
5167 TTY_DRIVER_HARDWARE_BREAK);
5168 if (IS_ERR(brd->serial_driver))
5169 return PTR_ERR(brd->serial_driver);
5171 snprintf(brd->serial_name, MAXTTYNAMELEN, "tty_dgap_%d_",
5172 brd->boardnum);
5173 brd->serial_driver->name = brd->serial_name;
5174 brd->serial_driver->name_base = 0;
5175 brd->serial_driver->major = 0;
5176 brd->serial_driver->minor_start = 0;
5177 brd->serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
5178 brd->serial_driver->subtype = SERIAL_TYPE_NORMAL;
5179 brd->serial_driver->init_termios = dgap_default_termios;
5180 brd->serial_driver->driver_name = DRVSTR;
5183 * Entry points for driver. Called by the kernel from
5184 * tty_io.c and n_tty.c.
5186 tty_set_operations(brd->serial_driver, &dgap_tty_ops);
5189 * If we're doing transparent print, we have to do all of the above
5190 * again, separately so we don't get the LD confused about what major
5191 * we are when we get into the dgap_tty_open() routine.
5193 brd->print_driver = tty_alloc_driver(MAXPORTS,
5194 TTY_DRIVER_REAL_RAW |
5195 TTY_DRIVER_DYNAMIC_DEV |
5196 TTY_DRIVER_HARDWARE_BREAK);
5197 if (IS_ERR(brd->print_driver)) {
5198 rc = PTR_ERR(brd->print_driver);
5199 goto free_serial_drv;
5202 snprintf(brd->print_name, MAXTTYNAMELEN, "pr_dgap_%d_",
5203 brd->boardnum);
5204 brd->print_driver->name = brd->print_name;
5205 brd->print_driver->name_base = 0;
5206 brd->print_driver->major = 0;
5207 brd->print_driver->minor_start = 0;
5208 brd->print_driver->type = TTY_DRIVER_TYPE_SERIAL;
5209 brd->print_driver->subtype = SERIAL_TYPE_NORMAL;
5210 brd->print_driver->init_termios = dgap_default_termios;
5211 brd->print_driver->driver_name = DRVSTR;
5214 * Entry points for driver. Called by the kernel from
5215 * tty_io.c and n_tty.c.
5217 tty_set_operations(brd->print_driver, &dgap_tty_ops);
5219 /* Register tty devices */
5220 rc = tty_register_driver(brd->serial_driver);
5221 if (rc < 0)
5222 goto free_print_drv;
5224 /* Register Transparent Print devices */
5225 rc = tty_register_driver(brd->print_driver);
5226 if (rc < 0)
5227 goto unregister_serial_drv;
5229 return 0;
5231 unregister_serial_drv:
5232 tty_unregister_driver(brd->serial_driver);
5233 free_print_drv:
5234 put_tty_driver(brd->print_driver);
5235 free_serial_drv:
5236 put_tty_driver(brd->serial_driver);
5238 return rc;
5241 static void dgap_tty_unregister(struct board_t *brd)
5243 tty_unregister_driver(brd->print_driver);
5244 tty_unregister_driver(brd->serial_driver);
5245 put_tty_driver(brd->print_driver);
5246 put_tty_driver(brd->serial_driver);
5249 static int dgap_alloc_flipbuf(struct board_t *brd)
5252 * allocate flip buffer for board.
5254 brd->flipbuf = kmalloc(MYFLIPLEN, GFP_KERNEL);
5255 if (!brd->flipbuf)
5256 return -ENOMEM;
5258 brd->flipflagbuf = kmalloc(MYFLIPLEN, GFP_KERNEL);
5259 if (!brd->flipflagbuf) {
5260 kfree(brd->flipbuf);
5261 return -ENOMEM;
5264 return 0;
5267 static void dgap_free_flipbuf(struct board_t *brd)
5269 kfree(brd->flipbuf);
5270 kfree(brd->flipflagbuf);
5273 static struct board_t *dgap_verify_board(struct device *p)
5275 struct board_t *bd;
5277 if (!p)
5278 return NULL;
5280 bd = dev_get_drvdata(p);
5281 if (!bd || bd->magic != DGAP_BOARD_MAGIC || bd->state != BOARD_READY)
5282 return NULL;
5284 return bd;
5287 static ssize_t dgap_ports_state_show(struct device *p,
5288 struct device_attribute *attr,
5289 char *buf)
5291 struct board_t *bd;
5292 int count = 0;
5293 unsigned int i;
5295 bd = dgap_verify_board(p);
5296 if (!bd)
5297 return 0;
5299 for (i = 0; i < bd->nasync; i++) {
5300 count += snprintf(buf + count, PAGE_SIZE - count,
5301 "%d %s\n", bd->channels[i]->ch_portnum,
5302 bd->channels[i]->ch_open_count ? "Open" : "Closed");
5304 return count;
5306 static DEVICE_ATTR(ports_state, S_IRUSR, dgap_ports_state_show, NULL);
5308 static ssize_t dgap_ports_baud_show(struct device *p,
5309 struct device_attribute *attr,
5310 char *buf)
5312 struct board_t *bd;
5313 int count = 0;
5314 unsigned int i;
5316 bd = dgap_verify_board(p);
5317 if (!bd)
5318 return 0;
5320 for (i = 0; i < bd->nasync; i++) {
5321 count += snprintf(buf + count, PAGE_SIZE - count, "%d %d\n",
5322 bd->channels[i]->ch_portnum,
5323 bd->channels[i]->ch_baud_info);
5325 return count;
5327 static DEVICE_ATTR(ports_baud, S_IRUSR, dgap_ports_baud_show, NULL);
5329 static ssize_t dgap_ports_msignals_show(struct device *p,
5330 struct device_attribute *attr,
5331 char *buf)
5333 struct board_t *bd;
5334 int count = 0;
5335 unsigned int i;
5337 bd = dgap_verify_board(p);
5338 if (!bd)
5339 return 0;
5341 for (i = 0; i < bd->nasync; i++) {
5342 if (bd->channels[i]->ch_open_count)
5343 count += snprintf(buf + count, PAGE_SIZE - count,
5344 "%d %s %s %s %s %s %s\n",
5345 bd->channels[i]->ch_portnum,
5346 (bd->channels[i]->ch_mostat &
5347 UART_MCR_RTS) ? "RTS" : "",
5348 (bd->channels[i]->ch_mistat &
5349 UART_MSR_CTS) ? "CTS" : "",
5350 (bd->channels[i]->ch_mostat &
5351 UART_MCR_DTR) ? "DTR" : "",
5352 (bd->channels[i]->ch_mistat &
5353 UART_MSR_DSR) ? "DSR" : "",
5354 (bd->channels[i]->ch_mistat &
5355 UART_MSR_DCD) ? "DCD" : "",
5356 (bd->channels[i]->ch_mistat &
5357 UART_MSR_RI) ? "RI" : "");
5358 else
5359 count += snprintf(buf + count, PAGE_SIZE - count,
5360 "%d\n", bd->channels[i]->ch_portnum);
5362 return count;
5364 static DEVICE_ATTR(ports_msignals, S_IRUSR, dgap_ports_msignals_show, NULL);
5366 static ssize_t dgap_ports_iflag_show(struct device *p,
5367 struct device_attribute *attr,
5368 char *buf)
5370 struct board_t *bd;
5371 int count = 0;
5372 unsigned int i;
5374 bd = dgap_verify_board(p);
5375 if (!bd)
5376 return 0;
5378 for (i = 0; i < bd->nasync; i++)
5379 count += snprintf(buf + count, PAGE_SIZE - count, "%d %x\n",
5380 bd->channels[i]->ch_portnum,
5381 bd->channels[i]->ch_c_iflag);
5382 return count;
5384 static DEVICE_ATTR(ports_iflag, S_IRUSR, dgap_ports_iflag_show, NULL);
5386 static ssize_t dgap_ports_cflag_show(struct device *p,
5387 struct device_attribute *attr,
5388 char *buf)
5390 struct board_t *bd;
5391 int count = 0;
5392 unsigned int i;
5394 bd = dgap_verify_board(p);
5395 if (!bd)
5396 return 0;
5398 for (i = 0; i < bd->nasync; i++)
5399 count += snprintf(buf + count, PAGE_SIZE - count, "%d %x\n",
5400 bd->channels[i]->ch_portnum,
5401 bd->channels[i]->ch_c_cflag);
5402 return count;
5404 static DEVICE_ATTR(ports_cflag, S_IRUSR, dgap_ports_cflag_show, NULL);
5406 static ssize_t dgap_ports_oflag_show(struct device *p,
5407 struct device_attribute *attr,
5408 char *buf)
5410 struct board_t *bd;
5411 int count = 0;
5412 unsigned int i;
5414 bd = dgap_verify_board(p);
5415 if (!bd)
5416 return 0;
5418 for (i = 0; i < bd->nasync; i++)
5419 count += snprintf(buf + count, PAGE_SIZE - count, "%d %x\n",
5420 bd->channels[i]->ch_portnum,
5421 bd->channels[i]->ch_c_oflag);
5422 return count;
5424 static DEVICE_ATTR(ports_oflag, S_IRUSR, dgap_ports_oflag_show, NULL);
5426 static ssize_t dgap_ports_lflag_show(struct device *p,
5427 struct device_attribute *attr,
5428 char *buf)
5430 struct board_t *bd;
5431 int count = 0;
5432 unsigned int i;
5434 bd = dgap_verify_board(p);
5435 if (!bd)
5436 return 0;
5438 for (i = 0; i < bd->nasync; i++)
5439 count += snprintf(buf + count, PAGE_SIZE - count, "%d %x\n",
5440 bd->channels[i]->ch_portnum,
5441 bd->channels[i]->ch_c_lflag);
5442 return count;
5444 static DEVICE_ATTR(ports_lflag, S_IRUSR, dgap_ports_lflag_show, NULL);
5446 static ssize_t dgap_ports_digi_flag_show(struct device *p,
5447 struct device_attribute *attr,
5448 char *buf)
5450 struct board_t *bd;
5451 int count = 0;
5452 unsigned int i;
5454 bd = dgap_verify_board(p);
5455 if (!bd)
5456 return 0;
5458 for (i = 0; i < bd->nasync; i++)
5459 count += snprintf(buf + count, PAGE_SIZE - count, "%d %x\n",
5460 bd->channels[i]->ch_portnum,
5461 bd->channels[i]->ch_digi.digi_flags);
5462 return count;
5464 static DEVICE_ATTR(ports_digi_flag, S_IRUSR, dgap_ports_digi_flag_show, NULL);
5466 static ssize_t dgap_ports_rxcount_show(struct device *p,
5467 struct device_attribute *attr,
5468 char *buf)
5470 struct board_t *bd;
5471 int count = 0;
5472 unsigned int i;
5474 bd = dgap_verify_board(p);
5475 if (!bd)
5476 return 0;
5478 for (i = 0; i < bd->nasync; i++)
5479 count += snprintf(buf + count, PAGE_SIZE - count, "%d %ld\n",
5480 bd->channels[i]->ch_portnum,
5481 bd->channels[i]->ch_rxcount);
5482 return count;
5484 static DEVICE_ATTR(ports_rxcount, S_IRUSR, dgap_ports_rxcount_show, NULL);
5486 static ssize_t dgap_ports_txcount_show(struct device *p,
5487 struct device_attribute *attr,
5488 char *buf)
5490 struct board_t *bd;
5491 int count = 0;
5492 unsigned int i;
5494 bd = dgap_verify_board(p);
5495 if (!bd)
5496 return 0;
5498 for (i = 0; i < bd->nasync; i++)
5499 count += snprintf(buf + count, PAGE_SIZE - count, "%d %ld\n",
5500 bd->channels[i]->ch_portnum,
5501 bd->channels[i]->ch_txcount);
5502 return count;
5504 static DEVICE_ATTR(ports_txcount, S_IRUSR, dgap_ports_txcount_show, NULL);
5506 static ssize_t dgap_tty_state_show(struct device *d,
5507 struct device_attribute *attr,
5508 char *buf)
5510 struct board_t *bd;
5511 struct channel_t *ch;
5512 struct un_t *un;
5514 if (!d)
5515 return 0;
5516 un = dev_get_drvdata(d);
5517 if (!un || un->magic != DGAP_UNIT_MAGIC)
5518 return 0;
5519 ch = un->un_ch;
5520 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
5521 return 0;
5522 bd = ch->ch_bd;
5523 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
5524 return 0;
5525 if (bd->state != BOARD_READY)
5526 return 0;
5528 return snprintf(buf, PAGE_SIZE, "%s", un->un_open_count ?
5529 "Open" : "Closed");
5531 static DEVICE_ATTR(state, S_IRUSR, dgap_tty_state_show, NULL);
5533 static ssize_t dgap_tty_baud_show(struct device *d,
5534 struct device_attribute *attr,
5535 char *buf)
5537 struct board_t *bd;
5538 struct channel_t *ch;
5539 struct un_t *un;
5541 if (!d)
5542 return 0;
5543 un = dev_get_drvdata(d);
5544 if (!un || un->magic != DGAP_UNIT_MAGIC)
5545 return 0;
5546 ch = un->un_ch;
5547 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
5548 return 0;
5549 bd = ch->ch_bd;
5550 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
5551 return 0;
5552 if (bd->state != BOARD_READY)
5553 return 0;
5555 return snprintf(buf, PAGE_SIZE, "%d\n", ch->ch_baud_info);
5557 static DEVICE_ATTR(baud, S_IRUSR, dgap_tty_baud_show, NULL);
5559 static ssize_t dgap_tty_msignals_show(struct device *d,
5560 struct device_attribute *attr,
5561 char *buf)
5563 struct board_t *bd;
5564 struct channel_t *ch;
5565 struct un_t *un;
5567 if (!d)
5568 return 0;
5569 un = dev_get_drvdata(d);
5570 if (!un || un->magic != DGAP_UNIT_MAGIC)
5571 return 0;
5572 ch = un->un_ch;
5573 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
5574 return 0;
5575 bd = ch->ch_bd;
5576 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
5577 return 0;
5578 if (bd->state != BOARD_READY)
5579 return 0;
5581 if (ch->ch_open_count) {
5582 return snprintf(buf, PAGE_SIZE, "%s %s %s %s %s %s\n",
5583 (ch->ch_mostat & UART_MCR_RTS) ? "RTS" : "",
5584 (ch->ch_mistat & UART_MSR_CTS) ? "CTS" : "",
5585 (ch->ch_mostat & UART_MCR_DTR) ? "DTR" : "",
5586 (ch->ch_mistat & UART_MSR_DSR) ? "DSR" : "",
5587 (ch->ch_mistat & UART_MSR_DCD) ? "DCD" : "",
5588 (ch->ch_mistat & UART_MSR_RI) ? "RI" : "");
5590 return 0;
5592 static DEVICE_ATTR(msignals, S_IRUSR, dgap_tty_msignals_show, NULL);
5594 static ssize_t dgap_tty_iflag_show(struct device *d,
5595 struct device_attribute *attr,
5596 char *buf)
5598 struct board_t *bd;
5599 struct channel_t *ch;
5600 struct un_t *un;
5602 if (!d)
5603 return 0;
5604 un = dev_get_drvdata(d);
5605 if (!un || un->magic != DGAP_UNIT_MAGIC)
5606 return 0;
5607 ch = un->un_ch;
5608 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
5609 return 0;
5610 bd = ch->ch_bd;
5611 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
5612 return 0;
5613 if (bd->state != BOARD_READY)
5614 return 0;
5616 return snprintf(buf, PAGE_SIZE, "%x\n", ch->ch_c_iflag);
5618 static DEVICE_ATTR(iflag, S_IRUSR, dgap_tty_iflag_show, NULL);
5620 static ssize_t dgap_tty_cflag_show(struct device *d,
5621 struct device_attribute *attr,
5622 char *buf)
5624 struct board_t *bd;
5625 struct channel_t *ch;
5626 struct un_t *un;
5628 if (!d)
5629 return 0;
5630 un = dev_get_drvdata(d);
5631 if (!un || un->magic != DGAP_UNIT_MAGIC)
5632 return 0;
5633 ch = un->un_ch;
5634 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
5635 return 0;
5636 bd = ch->ch_bd;
5637 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
5638 return 0;
5639 if (bd->state != BOARD_READY)
5640 return 0;
5642 return snprintf(buf, PAGE_SIZE, "%x\n", ch->ch_c_cflag);
5644 static DEVICE_ATTR(cflag, S_IRUSR, dgap_tty_cflag_show, NULL);
5646 static ssize_t dgap_tty_oflag_show(struct device *d,
5647 struct device_attribute *attr,
5648 char *buf)
5650 struct board_t *bd;
5651 struct channel_t *ch;
5652 struct un_t *un;
5654 if (!d)
5655 return 0;
5656 un = dev_get_drvdata(d);
5657 if (!un || un->magic != DGAP_UNIT_MAGIC)
5658 return 0;
5659 ch = un->un_ch;
5660 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
5661 return 0;
5662 bd = ch->ch_bd;
5663 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
5664 return 0;
5665 if (bd->state != BOARD_READY)
5666 return 0;
5668 return snprintf(buf, PAGE_SIZE, "%x\n", ch->ch_c_oflag);
5670 static DEVICE_ATTR(oflag, S_IRUSR, dgap_tty_oflag_show, NULL);
5672 static ssize_t dgap_tty_lflag_show(struct device *d,
5673 struct device_attribute *attr,
5674 char *buf)
5676 struct board_t *bd;
5677 struct channel_t *ch;
5678 struct un_t *un;
5680 if (!d)
5681 return 0;
5682 un = dev_get_drvdata(d);
5683 if (!un || un->magic != DGAP_UNIT_MAGIC)
5684 return 0;
5685 ch = un->un_ch;
5686 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
5687 return 0;
5688 bd = ch->ch_bd;
5689 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
5690 return 0;
5691 if (bd->state != BOARD_READY)
5692 return 0;
5694 return snprintf(buf, PAGE_SIZE, "%x\n", ch->ch_c_lflag);
5696 static DEVICE_ATTR(lflag, S_IRUSR, dgap_tty_lflag_show, NULL);
5698 static ssize_t dgap_tty_digi_flag_show(struct device *d,
5699 struct device_attribute *attr,
5700 char *buf)
5702 struct board_t *bd;
5703 struct channel_t *ch;
5704 struct un_t *un;
5706 if (!d)
5707 return 0;
5708 un = dev_get_drvdata(d);
5709 if (!un || un->magic != DGAP_UNIT_MAGIC)
5710 return 0;
5711 ch = un->un_ch;
5712 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
5713 return 0;
5714 bd = ch->ch_bd;
5715 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
5716 return 0;
5717 if (bd->state != BOARD_READY)
5718 return 0;
5720 return snprintf(buf, PAGE_SIZE, "%x\n", ch->ch_digi.digi_flags);
5722 static DEVICE_ATTR(digi_flag, S_IRUSR, dgap_tty_digi_flag_show, NULL);
5724 static ssize_t dgap_tty_rxcount_show(struct device *d,
5725 struct device_attribute *attr,
5726 char *buf)
5728 struct board_t *bd;
5729 struct channel_t *ch;
5730 struct un_t *un;
5732 if (!d)
5733 return 0;
5734 un = dev_get_drvdata(d);
5735 if (!un || un->magic != DGAP_UNIT_MAGIC)
5736 return 0;
5737 ch = un->un_ch;
5738 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
5739 return 0;
5740 bd = ch->ch_bd;
5741 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
5742 return 0;
5743 if (bd->state != BOARD_READY)
5744 return 0;
5746 return snprintf(buf, PAGE_SIZE, "%ld\n", ch->ch_rxcount);
5748 static DEVICE_ATTR(rxcount, S_IRUSR, dgap_tty_rxcount_show, NULL);
5750 static ssize_t dgap_tty_txcount_show(struct device *d,
5751 struct device_attribute *attr,
5752 char *buf)
5754 struct board_t *bd;
5755 struct channel_t *ch;
5756 struct un_t *un;
5758 if (!d)
5759 return 0;
5760 un = dev_get_drvdata(d);
5761 if (!un || un->magic != DGAP_UNIT_MAGIC)
5762 return 0;
5763 ch = un->un_ch;
5764 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
5765 return 0;
5766 bd = ch->ch_bd;
5767 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
5768 return 0;
5769 if (bd->state != BOARD_READY)
5770 return 0;
5772 return snprintf(buf, PAGE_SIZE, "%ld\n", ch->ch_txcount);
5774 static DEVICE_ATTR(txcount, S_IRUSR, dgap_tty_txcount_show, NULL);
5776 static ssize_t dgap_tty_name_show(struct device *d,
5777 struct device_attribute *attr,
5778 char *buf)
5780 struct board_t *bd;
5781 struct channel_t *ch;
5782 struct un_t *un;
5783 int cn;
5784 int bn;
5785 struct cnode *cptr;
5786 int found = FALSE;
5787 int ncount = 0;
5788 int starto = 0;
5789 int i;
5791 if (!d)
5792 return 0;
5793 un = dev_get_drvdata(d);
5794 if (!un || un->magic != DGAP_UNIT_MAGIC)
5795 return 0;
5796 ch = un->un_ch;
5797 if (!ch || ch->magic != DGAP_CHANNEL_MAGIC)
5798 return 0;
5799 bd = ch->ch_bd;
5800 if (!bd || bd->magic != DGAP_BOARD_MAGIC)
5801 return 0;
5802 if (bd->state != BOARD_READY)
5803 return 0;
5805 bn = bd->boardnum;
5806 cn = ch->ch_portnum;
5808 for (cptr = bd->bd_config; cptr; cptr = cptr->next) {
5809 if ((cptr->type == BNODE) &&
5810 ((cptr->u.board.type == APORT2_920P) ||
5811 (cptr->u.board.type == APORT4_920P) ||
5812 (cptr->u.board.type == APORT8_920P) ||
5813 (cptr->u.board.type == PAPORT4) ||
5814 (cptr->u.board.type == PAPORT8))) {
5815 found = TRUE;
5816 if (cptr->u.board.v_start)
5817 starto = cptr->u.board.start;
5818 else
5819 starto = 1;
5822 if (cptr->type == TNODE && found == TRUE) {
5823 char *ptr1;
5825 if (strstr(cptr->u.ttyname, "tty")) {
5826 ptr1 = cptr->u.ttyname;
5827 ptr1 += 3;
5828 } else
5829 ptr1 = cptr->u.ttyname;
5831 for (i = 0; i < dgap_config_get_num_prts(bd); i++) {
5832 if (cn != i)
5833 continue;
5835 return snprintf(buf, PAGE_SIZE, "%s%s%02d\n",
5836 (un->un_type == DGAP_PRINT) ?
5837 "pr" : "tty",
5838 ptr1, i + starto);
5842 if (cptr->type == CNODE) {
5843 for (i = 0; i < cptr->u.conc.nport; i++) {
5844 if (cn != (i + ncount))
5845 continue;
5847 return snprintf(buf, PAGE_SIZE, "%s%s%02ld\n",
5848 (un->un_type == DGAP_PRINT) ?
5849 "pr" : "tty",
5850 cptr->u.conc.id,
5851 i + (cptr->u.conc.v_start ?
5852 cptr->u.conc.start : 1));
5855 ncount += cptr->u.conc.nport;
5858 if (cptr->type == MNODE) {
5859 for (i = 0; i < cptr->u.module.nport; i++) {
5860 if (cn != (i + ncount))
5861 continue;
5863 return snprintf(buf, PAGE_SIZE, "%s%s%02ld\n",
5864 (un->un_type == DGAP_PRINT) ?
5865 "pr" : "tty",
5866 cptr->u.module.id,
5867 i + (cptr->u.module.v_start ?
5868 cptr->u.module.start : 1));
5871 ncount += cptr->u.module.nport;
5875 return snprintf(buf, PAGE_SIZE, "%s_dgap_%d_%d\n",
5876 (un->un_type == DGAP_PRINT) ? "pr" : "tty", bn, cn);
5878 static DEVICE_ATTR(custom_name, S_IRUSR, dgap_tty_name_show, NULL);
5880 static struct attribute *dgap_sysfs_tty_entries[] = {
5881 &dev_attr_state.attr,
5882 &dev_attr_baud.attr,
5883 &dev_attr_msignals.attr,
5884 &dev_attr_iflag.attr,
5885 &dev_attr_cflag.attr,
5886 &dev_attr_oflag.attr,
5887 &dev_attr_lflag.attr,
5888 &dev_attr_digi_flag.attr,
5889 &dev_attr_rxcount.attr,
5890 &dev_attr_txcount.attr,
5891 &dev_attr_custom_name.attr,
5892 NULL
5896 /* this function creates the sys files that will export each signal status
5897 * to sysfs each value will be put in a separate filename
5899 static void dgap_create_ports_sysfiles(struct board_t *bd)
5901 dev_set_drvdata(&bd->pdev->dev, bd);
5902 device_create_file(&bd->pdev->dev, &dev_attr_ports_state);
5903 device_create_file(&bd->pdev->dev, &dev_attr_ports_baud);
5904 device_create_file(&bd->pdev->dev, &dev_attr_ports_msignals);
5905 device_create_file(&bd->pdev->dev, &dev_attr_ports_iflag);
5906 device_create_file(&bd->pdev->dev, &dev_attr_ports_cflag);
5907 device_create_file(&bd->pdev->dev, &dev_attr_ports_oflag);
5908 device_create_file(&bd->pdev->dev, &dev_attr_ports_lflag);
5909 device_create_file(&bd->pdev->dev, &dev_attr_ports_digi_flag);
5910 device_create_file(&bd->pdev->dev, &dev_attr_ports_rxcount);
5911 device_create_file(&bd->pdev->dev, &dev_attr_ports_txcount);
5914 /* removes all the sys files created for that port */
5915 static void dgap_remove_ports_sysfiles(struct board_t *bd)
5917 device_remove_file(&bd->pdev->dev, &dev_attr_ports_state);
5918 device_remove_file(&bd->pdev->dev, &dev_attr_ports_baud);
5919 device_remove_file(&bd->pdev->dev, &dev_attr_ports_msignals);
5920 device_remove_file(&bd->pdev->dev, &dev_attr_ports_iflag);
5921 device_remove_file(&bd->pdev->dev, &dev_attr_ports_cflag);
5922 device_remove_file(&bd->pdev->dev, &dev_attr_ports_oflag);
5923 device_remove_file(&bd->pdev->dev, &dev_attr_ports_lflag);
5924 device_remove_file(&bd->pdev->dev, &dev_attr_ports_digi_flag);
5925 device_remove_file(&bd->pdev->dev, &dev_attr_ports_rxcount);
5926 device_remove_file(&bd->pdev->dev, &dev_attr_ports_txcount);
5930 * Copies the BIOS code from the user to the board,
5931 * and starts the BIOS running.
5933 static void dgap_do_bios_load(struct board_t *brd, const u8 *ubios, int len)
5935 u8 __iomem *addr;
5936 uint offset;
5937 unsigned int i;
5939 if (!brd || (brd->magic != DGAP_BOARD_MAGIC) || !brd->re_map_membase)
5940 return;
5942 addr = brd->re_map_membase;
5945 * clear POST area
5947 for (i = 0; i < 16; i++)
5948 writeb(0, addr + POSTAREA + i);
5951 * Download bios
5953 offset = 0x1000;
5954 memcpy_toio(addr + offset, ubios, len);
5956 writel(0x0bf00401, addr);
5957 writel(0, (addr + 4));
5959 /* Clear the reset, and change states. */
5960 writeb(FEPCLR, brd->re_map_port);
5964 * Checks to see if the BIOS completed running on the card.
5966 static int dgap_test_bios(struct board_t *brd)
5968 u8 __iomem *addr;
5969 u16 word;
5970 u16 err1;
5971 u16 err2;
5973 if (!brd || (brd->magic != DGAP_BOARD_MAGIC) || !brd->re_map_membase)
5974 return -EINVAL;
5976 addr = brd->re_map_membase;
5977 word = readw(addr + POSTAREA);
5980 * It can take 5-6 seconds for a board to
5981 * pass the bios self test and post results.
5982 * Give it 10 seconds.
5984 brd->wait_for_bios = 0;
5985 while (brd->wait_for_bios < 1000) {
5986 /* Check to see if BIOS thinks board is good. (GD). */
5987 if (word == *(u16 *)"GD")
5988 return 0;
5989 msleep_interruptible(10);
5990 brd->wait_for_bios++;
5991 word = readw(addr + POSTAREA);
5994 /* Gave up on board after too long of time taken */
5995 err1 = readw(addr + SEQUENCE);
5996 err2 = readw(addr + ERROR);
5997 dev_warn(&brd->pdev->dev, "%s failed diagnostics. Error #(%x,%x).\n",
5998 brd->name, err1, err2);
5999 brd->state = BOARD_FAILED;
6000 brd->dpastatus = BD_NOBIOS;
6002 return -EIO;
6006 * Copies the FEP code from the user to the board,
6007 * and starts the FEP running.
6009 static void dgap_do_fep_load(struct board_t *brd, const u8 *ufep, int len)
6011 u8 __iomem *addr;
6012 uint offset;
6014 if (!brd || (brd->magic != DGAP_BOARD_MAGIC) || !brd->re_map_membase)
6015 return;
6017 addr = brd->re_map_membase;
6020 * Download FEP
6022 offset = 0x1000;
6023 memcpy_toio(addr + offset, ufep, len);
6026 * If board is a concentrator product, we need to give
6027 * it its config string describing how the concentrators look.
6029 if ((brd->type == PCX) || (brd->type == PEPC)) {
6030 u8 string[100];
6031 u8 __iomem *config;
6032 u8 *xconfig;
6033 unsigned int i = 0;
6035 xconfig = dgap_create_config_string(brd, string);
6037 /* Write string to board memory */
6038 config = addr + CONFIG;
6039 for (; i < CONFIGSIZE; i++, config++, xconfig++) {
6040 writeb(*xconfig, config);
6041 if ((*xconfig & 0xff) == 0xff)
6042 break;
6046 writel(0xbfc01004, (addr + 0xc34));
6047 writel(0x3, (addr + 0xc30));
6051 * Waits for the FEP to report thats its ready for us to use.
6053 static int dgap_test_fep(struct board_t *brd)
6055 u8 __iomem *addr;
6056 u16 word;
6057 u16 err1;
6058 u16 err2;
6060 if (!brd || (brd->magic != DGAP_BOARD_MAGIC) || !brd->re_map_membase)
6061 return -EINVAL;
6063 addr = brd->re_map_membase;
6064 word = readw(addr + FEPSTAT);
6067 * It can take 2-3 seconds for the FEP to
6068 * be up and running. Give it 5 secs.
6070 brd->wait_for_fep = 0;
6071 while (brd->wait_for_fep < 500) {
6072 /* Check to see if FEP is up and running now. */
6073 if (word == *(u16 *)"OS") {
6075 * Check to see if the board can support FEP5+ commands.
6077 word = readw(addr + FEP5_PLUS);
6078 if (word == *(u16 *)"5A")
6079 brd->bd_flags |= BD_FEP5PLUS;
6081 return 0;
6083 msleep_interruptible(10);
6084 brd->wait_for_fep++;
6085 word = readw(addr + FEPSTAT);
6088 /* Gave up on board after too long of time taken */
6089 err1 = readw(addr + SEQUENCE);
6090 err2 = readw(addr + ERROR);
6091 dev_warn(&brd->pdev->dev,
6092 "FEPOS for %s not functioning. Error #(%x,%x).\n",
6093 brd->name, err1, err2);
6094 brd->state = BOARD_FAILED;
6095 brd->dpastatus = BD_NOFEP;
6097 return -EIO;
6101 * Physically forces the FEP5 card to reset itself.
6103 static void dgap_do_reset_board(struct board_t *brd)
6105 u8 check;
6106 u32 check1;
6107 u32 check2;
6108 unsigned int i;
6110 if (!brd || (brd->magic != DGAP_BOARD_MAGIC) ||
6111 !brd->re_map_membase || !brd->re_map_port)
6112 return;
6114 /* FEPRST does not vary among supported boards */
6115 writeb(FEPRST, brd->re_map_port);
6117 for (i = 0; i <= 1000; i++) {
6118 check = readb(brd->re_map_port) & 0xe;
6119 if (check == FEPRST)
6120 break;
6121 udelay(10);
6123 if (i > 1000) {
6124 dev_warn(&brd->pdev->dev,
6125 "dgap: Board not resetting... Failing board.\n");
6126 brd->state = BOARD_FAILED;
6127 brd->dpastatus = BD_NOFEP;
6128 return;
6132 * Make sure there really is memory out there.
6134 writel(0xa55a3cc3, (brd->re_map_membase + LOWMEM));
6135 writel(0x5aa5c33c, (brd->re_map_membase + HIGHMEM));
6136 check1 = readl(brd->re_map_membase + LOWMEM);
6137 check2 = readl(brd->re_map_membase + HIGHMEM);
6139 if ((check1 != 0xa55a3cc3) || (check2 != 0x5aa5c33c)) {
6140 dev_warn(&brd->pdev->dev,
6141 "No memory at %p for board.\n",
6142 brd->re_map_membase);
6143 brd->state = BOARD_FAILED;
6144 brd->dpastatus = BD_NOFEP;
6145 return;
6149 #ifdef DIGI_CONCENTRATORS_SUPPORTED
6151 * Sends a concentrator image into the FEP5 board.
6153 static void dgap_do_conc_load(struct board_t *brd, u8 *uaddr, int len)
6155 char __iomem *vaddr;
6156 u16 offset;
6157 struct downld_t *to_dp;
6159 if (!brd || (brd->magic != DGAP_BOARD_MAGIC) || !brd->re_map_membase)
6160 return;
6162 vaddr = brd->re_map_membase;
6164 offset = readw((u16 *)(vaddr + DOWNREQ));
6165 to_dp = (struct downld_t *)(vaddr + (int)offset);
6166 memcpy_toio(to_dp, uaddr, len);
6168 /* Tell card we have data for it */
6169 writew(0, vaddr + (DOWNREQ));
6171 brd->conc_dl_status = NO_PENDING_CONCENTRATOR_REQUESTS;
6173 #endif
6175 #define EXPANSION_ROM_SIZE (64 * 1024)
6176 #define FEP5_ROM_MAGIC (0xFEFFFFFF)
6178 static void dgap_get_vpd(struct board_t *brd)
6180 u32 magic;
6181 u32 base_offset;
6182 u16 rom_offset;
6183 u16 vpd_offset;
6184 u16 image_length;
6185 u16 i;
6186 u8 byte1;
6187 u8 byte2;
6190 * Poke the magic number at the PCI Rom Address location.
6191 * If VPD is supported, the value read from that address
6192 * will be non-zero.
6194 magic = FEP5_ROM_MAGIC;
6195 pci_write_config_dword(brd->pdev, PCI_ROM_ADDRESS, magic);
6196 pci_read_config_dword(brd->pdev, PCI_ROM_ADDRESS, &magic);
6198 /* VPD not supported, bail */
6199 if (!magic)
6200 return;
6203 * To get to the OTPROM memory, we have to send the boards base
6204 * address or'ed with 1 into the PCI Rom Address location.
6206 magic = brd->membase | 0x01;
6207 pci_write_config_dword(brd->pdev, PCI_ROM_ADDRESS, magic);
6208 pci_read_config_dword(brd->pdev, PCI_ROM_ADDRESS, &magic);
6210 byte1 = readb(brd->re_map_membase);
6211 byte2 = readb(brd->re_map_membase + 1);
6214 * If the board correctly swapped to the OTPROM memory,
6215 * the first 2 bytes (header) should be 0x55, 0xAA
6217 if (byte1 == 0x55 && byte2 == 0xAA) {
6218 base_offset = 0;
6221 * We have to run through all the OTPROM memory looking
6222 * for the VPD offset.
6224 while (base_offset <= EXPANSION_ROM_SIZE) {
6226 * Lots of magic numbers here.
6228 * The VPD offset is located inside the ROM Data
6229 * Structure.
6231 * We also have to remember the length of each
6232 * ROM Data Structure, so we can "hop" to the next
6233 * entry if the VPD isn't in the current
6234 * ROM Data Structure.
6236 rom_offset = readw(brd->re_map_membase +
6237 base_offset + 0x18);
6238 image_length = readw(brd->re_map_membase +
6239 rom_offset + 0x10) * 512;
6240 vpd_offset = readw(brd->re_map_membase +
6241 rom_offset + 0x08);
6243 /* Found the VPD entry */
6244 if (vpd_offset)
6245 break;
6247 /* We didn't find a VPD entry, go to next ROM entry. */
6248 base_offset += image_length;
6250 byte1 = readb(brd->re_map_membase + base_offset);
6251 byte2 = readb(brd->re_map_membase + base_offset + 1);
6254 * If the new ROM offset doesn't have 0x55, 0xAA
6255 * as its header, we have run out of ROM.
6257 if (byte1 != 0x55 || byte2 != 0xAA)
6258 break;
6262 * If we have a VPD offset, then mark the board
6263 * as having a valid VPD, and copy VPDSIZE (512) bytes of
6264 * that VPD to the buffer we have in our board structure.
6266 if (vpd_offset) {
6267 brd->bd_flags |= BD_HAS_VPD;
6268 for (i = 0; i < VPDSIZE; i++) {
6269 brd->vpd[i] = readb(brd->re_map_membase +
6270 vpd_offset + i);
6276 * We MUST poke the magic number at the PCI Rom Address location again.
6277 * This makes the card report the regular board memory back to us,
6278 * rather than the OTPROM memory.
6280 magic = FEP5_ROM_MAGIC;
6281 pci_write_config_dword(brd->pdev, PCI_ROM_ADDRESS, magic);
6285 static ssize_t dgap_driver_version_show(struct device_driver *ddp, char *buf)
6287 return snprintf(buf, PAGE_SIZE, "%s\n", DG_PART);
6289 static DRIVER_ATTR(version, S_IRUSR, dgap_driver_version_show, NULL);
6292 static ssize_t dgap_driver_boards_show(struct device_driver *ddp, char *buf)
6294 return snprintf(buf, PAGE_SIZE, "%d\n", dgap_numboards);
6296 static DRIVER_ATTR(boards, S_IRUSR, dgap_driver_boards_show, NULL);
6299 static ssize_t dgap_driver_maxboards_show(struct device_driver *ddp, char *buf)
6301 return snprintf(buf, PAGE_SIZE, "%d\n", MAXBOARDS);
6303 static DRIVER_ATTR(maxboards, S_IRUSR, dgap_driver_maxboards_show, NULL);
6306 static ssize_t dgap_driver_pollcounter_show(struct device_driver *ddp,
6307 char *buf)
6309 return snprintf(buf, PAGE_SIZE, "%ld\n", dgap_poll_counter);
6311 static DRIVER_ATTR(pollcounter, S_IRUSR, dgap_driver_pollcounter_show, NULL);
6313 static ssize_t dgap_driver_pollrate_show(struct device_driver *ddp, char *buf)
6315 return snprintf(buf, PAGE_SIZE, "%dms\n", dgap_poll_tick);
6318 static ssize_t dgap_driver_pollrate_store(struct device_driver *ddp,
6319 const char *buf, size_t count)
6321 if (sscanf(buf, "%d\n", &dgap_poll_tick) != 1)
6322 return -EINVAL;
6323 return count;
6325 static DRIVER_ATTR(pollrate, (S_IRUSR | S_IWUSR), dgap_driver_pollrate_show,
6326 dgap_driver_pollrate_store);
6329 static int dgap_create_driver_sysfiles(struct pci_driver *dgap_driver)
6331 int rc = 0;
6332 struct device_driver *driverfs = &dgap_driver->driver;
6334 rc |= driver_create_file(driverfs, &driver_attr_version);
6335 rc |= driver_create_file(driverfs, &driver_attr_boards);
6336 rc |= driver_create_file(driverfs, &driver_attr_maxboards);
6337 rc |= driver_create_file(driverfs, &driver_attr_pollrate);
6338 rc |= driver_create_file(driverfs, &driver_attr_pollcounter);
6340 return rc;
6343 static void dgap_remove_driver_sysfiles(struct pci_driver *dgap_driver)
6345 struct device_driver *driverfs = &dgap_driver->driver;
6347 driver_remove_file(driverfs, &driver_attr_version);
6348 driver_remove_file(driverfs, &driver_attr_boards);
6349 driver_remove_file(driverfs, &driver_attr_maxboards);
6350 driver_remove_file(driverfs, &driver_attr_pollrate);
6351 driver_remove_file(driverfs, &driver_attr_pollcounter);
6354 static struct attribute_group dgap_tty_attribute_group = {
6355 .name = NULL,
6356 .attrs = dgap_sysfs_tty_entries,
6359 static void dgap_create_tty_sysfs(struct un_t *un, struct device *c)
6361 int ret;
6363 ret = sysfs_create_group(&c->kobj, &dgap_tty_attribute_group);
6364 if (ret)
6365 return;
6367 dev_set_drvdata(c, un);
6370 static void dgap_remove_tty_sysfs(struct device *c)
6372 sysfs_remove_group(&c->kobj, &dgap_tty_attribute_group);
6376 * Create pr and tty device entries
6378 static int dgap_tty_register_ports(struct board_t *brd)
6380 struct channel_t *ch;
6381 int i;
6382 int ret;
6384 brd->serial_ports = kcalloc(brd->nasync, sizeof(*brd->serial_ports),
6385 GFP_KERNEL);
6386 if (!brd->serial_ports)
6387 return -ENOMEM;
6389 brd->printer_ports = kcalloc(brd->nasync, sizeof(*brd->printer_ports),
6390 GFP_KERNEL);
6391 if (!brd->printer_ports) {
6392 ret = -ENOMEM;
6393 goto free_serial_ports;
6396 for (i = 0; i < brd->nasync; i++) {
6397 tty_port_init(&brd->serial_ports[i]);
6398 tty_port_init(&brd->printer_ports[i]);
6401 ch = brd->channels[0];
6402 for (i = 0; i < brd->nasync; i++, ch = brd->channels[i]) {
6403 struct device *classp;
6405 classp = tty_port_register_device(&brd->serial_ports[i],
6406 brd->serial_driver,
6407 i, NULL);
6409 if (IS_ERR(classp)) {
6410 ret = PTR_ERR(classp);
6411 goto unregister_ttys;
6414 dgap_create_tty_sysfs(&ch->ch_tun, classp);
6415 ch->ch_tun.un_sysfs = classp;
6417 classp = tty_port_register_device(&brd->printer_ports[i],
6418 brd->print_driver,
6419 i, NULL);
6421 if (IS_ERR(classp)) {
6422 ret = PTR_ERR(classp);
6423 goto unregister_ttys;
6426 dgap_create_tty_sysfs(&ch->ch_pun, classp);
6427 ch->ch_pun.un_sysfs = classp;
6429 dgap_create_ports_sysfiles(brd);
6431 return 0;
6433 unregister_ttys:
6434 while (i >= 0) {
6435 ch = brd->channels[i];
6436 if (ch->ch_tun.un_sysfs) {
6437 dgap_remove_tty_sysfs(ch->ch_tun.un_sysfs);
6438 tty_unregister_device(brd->serial_driver, i);
6441 if (ch->ch_pun.un_sysfs) {
6442 dgap_remove_tty_sysfs(ch->ch_pun.un_sysfs);
6443 tty_unregister_device(brd->print_driver, i);
6445 i--;
6448 for (i = 0; i < brd->nasync; i++) {
6449 tty_port_destroy(&brd->serial_ports[i]);
6450 tty_port_destroy(&brd->printer_ports[i]);
6453 kfree(brd->printer_ports);
6454 brd->printer_ports = NULL;
6456 free_serial_ports:
6457 kfree(brd->serial_ports);
6458 brd->serial_ports = NULL;
6460 return ret;
6464 * dgap_cleanup_tty()
6466 * Uninitialize the TTY portion of this driver. Free all memory and
6467 * resources.
6469 static void dgap_cleanup_tty(struct board_t *brd)
6471 struct device *dev;
6472 unsigned int i;
6474 for (i = 0; i < brd->nasync; i++) {
6475 tty_port_destroy(&brd->serial_ports[i]);
6476 dev = brd->channels[i]->ch_tun.un_sysfs;
6477 dgap_remove_tty_sysfs(dev);
6478 tty_unregister_device(brd->serial_driver, i);
6480 tty_unregister_driver(brd->serial_driver);
6481 put_tty_driver(brd->serial_driver);
6482 kfree(brd->serial_ports);
6484 for (i = 0; i < brd->nasync; i++) {
6485 tty_port_destroy(&brd->printer_ports[i]);
6486 dev = brd->channels[i]->ch_pun.un_sysfs;
6487 dgap_remove_tty_sysfs(dev);
6488 tty_unregister_device(brd->print_driver, i);
6490 tty_unregister_driver(brd->print_driver);
6491 put_tty_driver(brd->print_driver);
6492 kfree(brd->printer_ports);
6495 static int dgap_request_irq(struct board_t *brd)
6497 int rc;
6499 if (!brd || brd->magic != DGAP_BOARD_MAGIC)
6500 return -ENODEV;
6503 * Set up our interrupt handler if we are set to do interrupts.
6505 if (dgap_config_get_useintr(brd) && brd->irq) {
6506 rc = request_irq(brd->irq, dgap_intr, IRQF_SHARED, "DGAP", brd);
6508 if (!rc)
6509 brd->intr_used = 1;
6511 return 0;
6514 static void dgap_free_irq(struct board_t *brd)
6516 if (brd->intr_used && brd->irq)
6517 free_irq(brd->irq, brd);
6520 static int dgap_firmware_load(struct pci_dev *pdev, int card_type,
6521 struct board_t *brd)
6523 const struct firmware *fw;
6524 char *tmp_ptr;
6525 int ret;
6526 char *dgap_config_buf;
6528 dgap_get_vpd(brd);
6529 dgap_do_reset_board(brd);
6531 if (fw_info[card_type].conf_name) {
6532 ret = request_firmware(&fw, fw_info[card_type].conf_name,
6533 &pdev->dev);
6534 if (ret) {
6535 dev_err(&pdev->dev, "config file %s not found\n",
6536 fw_info[card_type].conf_name);
6537 return ret;
6540 dgap_config_buf = kzalloc(fw->size + 1, GFP_KERNEL);
6541 if (!dgap_config_buf) {
6542 release_firmware(fw);
6543 return -ENOMEM;
6546 memcpy(dgap_config_buf, fw->data, fw->size);
6547 release_firmware(fw);
6550 * preserve dgap_config_buf
6551 * as dgap_parsefile would
6552 * otherwise alter it.
6554 tmp_ptr = dgap_config_buf;
6556 if (dgap_parsefile(&tmp_ptr) != 0) {
6557 kfree(dgap_config_buf);
6558 return -EINVAL;
6560 kfree(dgap_config_buf);
6564 * Match this board to a config the user created for us.
6566 brd->bd_config =
6567 dgap_find_config(brd->type, brd->pci_bus, brd->pci_slot);
6570 * Because the 4 port Xr products share the same PCI ID
6571 * as the 8 port Xr products, if we receive a NULL config
6572 * back, and this is a PAPORT8 board, retry with a
6573 * PAPORT4 attempt as well.
6575 if (brd->type == PAPORT8 && !brd->bd_config)
6576 brd->bd_config =
6577 dgap_find_config(PAPORT4, brd->pci_bus, brd->pci_slot);
6579 if (!brd->bd_config) {
6580 dev_err(&pdev->dev, "No valid configuration found\n");
6581 return -EINVAL;
6584 if (fw_info[card_type].bios_name) {
6585 ret = request_firmware(&fw, fw_info[card_type].bios_name,
6586 &pdev->dev);
6587 if (ret) {
6588 dev_err(&pdev->dev, "bios file %s not found\n",
6589 fw_info[card_type].bios_name);
6590 return ret;
6592 dgap_do_bios_load(brd, fw->data, fw->size);
6593 release_firmware(fw);
6595 /* Wait for BIOS to test board... */
6596 ret = dgap_test_bios(brd);
6597 if (ret)
6598 return ret;
6601 if (fw_info[card_type].fep_name) {
6602 ret = request_firmware(&fw, fw_info[card_type].fep_name,
6603 &pdev->dev);
6604 if (ret) {
6605 dev_err(&pdev->dev, "dgap: fep file %s not found\n",
6606 fw_info[card_type].fep_name);
6607 return ret;
6609 dgap_do_fep_load(brd, fw->data, fw->size);
6610 release_firmware(fw);
6612 /* Wait for FEP to load on board... */
6613 ret = dgap_test_fep(brd);
6614 if (ret)
6615 return ret;
6618 #ifdef DIGI_CONCENTRATORS_SUPPORTED
6620 * If this is a CX or EPCX, we need to see if the firmware
6621 * is requesting a concentrator image from us.
6623 if ((bd->type == PCX) || (bd->type == PEPC)) {
6624 chk_addr = (u16 *)(vaddr + DOWNREQ);
6625 /* Nonzero if FEP is requesting concentrator image. */
6626 check = readw(chk_addr);
6627 vaddr = brd->re_map_membase;
6630 if (fw_info[card_type].con_name && check && vaddr) {
6631 ret = request_firmware(&fw, fw_info[card_type].con_name,
6632 &pdev->dev);
6633 if (ret) {
6634 dev_err(&pdev->dev, "conc file %s not found\n",
6635 fw_info[card_type].con_name);
6636 return ret;
6638 /* Put concentrator firmware loading code here */
6639 offset = readw((u16 *)(vaddr + DOWNREQ));
6640 memcpy_toio(offset, fw->data, fw->size);
6642 dgap_do_conc_load(brd, (char *)fw->data, fw->size)
6643 release_firmware(fw);
6645 #endif
6647 return 0;
6651 * dgap_tty_init()
6653 * Init the tty subsystem. Called once per board after board has been
6654 * downloaded and init'ed.
6656 static int dgap_tty_init(struct board_t *brd)
6658 int i;
6659 int tlw;
6660 uint true_count;
6661 u8 __iomem *vaddr;
6662 u8 modem;
6663 struct channel_t *ch;
6664 struct bs_t __iomem *bs;
6665 struct cm_t __iomem *cm;
6666 int ret;
6669 * Initialize board structure elements.
6672 vaddr = brd->re_map_membase;
6673 true_count = readw((vaddr + NCHAN));
6675 brd->nasync = dgap_config_get_num_prts(brd);
6677 if (!brd->nasync)
6678 brd->nasync = brd->maxports;
6680 if (brd->nasync > brd->maxports)
6681 brd->nasync = brd->maxports;
6683 if (true_count != brd->nasync) {
6684 dev_warn(&brd->pdev->dev,
6685 "%s configured for %d ports, has %d ports.\n",
6686 brd->name, brd->nasync, true_count);
6688 if ((brd->type == PPCM) &&
6689 (true_count == 64 || true_count == 0)) {
6690 dev_warn(&brd->pdev->dev,
6691 "Please make SURE the EBI cable running from the card\n");
6692 dev_warn(&brd->pdev->dev,
6693 "to each EM module is plugged into EBI IN!\n");
6696 brd->nasync = true_count;
6698 /* If no ports, don't bother going any further */
6699 if (!brd->nasync) {
6700 brd->state = BOARD_FAILED;
6701 brd->dpastatus = BD_NOFEP;
6702 return -EIO;
6707 * Allocate channel memory that might not have been allocated
6708 * when the driver was first loaded.
6710 for (i = 0; i < brd->nasync; i++) {
6711 brd->channels[i] =
6712 kzalloc(sizeof(struct channel_t), GFP_KERNEL);
6713 if (!brd->channels[i]) {
6714 ret = -ENOMEM;
6715 goto free_chan;
6719 ch = brd->channels[0];
6720 vaddr = brd->re_map_membase;
6722 bs = (struct bs_t __iomem *)((ulong)vaddr + CHANBUF);
6723 cm = (struct cm_t __iomem *)((ulong)vaddr + CMDBUF);
6725 brd->bd_bs = bs;
6727 /* Set up channel variables */
6728 for (i = 0; i < brd->nasync; i++, ch = brd->channels[i], bs++) {
6729 spin_lock_init(&ch->ch_lock);
6731 /* Store all our magic numbers */
6732 ch->magic = DGAP_CHANNEL_MAGIC;
6733 ch->ch_tun.magic = DGAP_UNIT_MAGIC;
6734 ch->ch_tun.un_type = DGAP_SERIAL;
6735 ch->ch_tun.un_ch = ch;
6736 ch->ch_tun.un_dev = i;
6738 ch->ch_pun.magic = DGAP_UNIT_MAGIC;
6739 ch->ch_pun.un_type = DGAP_PRINT;
6740 ch->ch_pun.un_ch = ch;
6741 ch->ch_pun.un_dev = i;
6743 ch->ch_vaddr = vaddr;
6744 ch->ch_bs = bs;
6745 ch->ch_cm = cm;
6746 ch->ch_bd = brd;
6747 ch->ch_portnum = i;
6748 ch->ch_digi = dgap_digi_init;
6751 * Set up digi dsr and dcd bits based on altpin flag.
6753 if (dgap_config_get_altpin(brd)) {
6754 ch->ch_dsr = DM_CD;
6755 ch->ch_cd = DM_DSR;
6756 ch->ch_digi.digi_flags |= DIGI_ALTPIN;
6757 } else {
6758 ch->ch_cd = DM_CD;
6759 ch->ch_dsr = DM_DSR;
6762 ch->ch_taddr = vaddr + (ioread16(&ch->ch_bs->tx_seg) << 4);
6763 ch->ch_raddr = vaddr + (ioread16(&ch->ch_bs->rx_seg) << 4);
6764 ch->ch_tx_win = 0;
6765 ch->ch_rx_win = 0;
6766 ch->ch_tsize = readw(&ch->ch_bs->tx_max) + 1;
6767 ch->ch_rsize = readw(&ch->ch_bs->rx_max) + 1;
6768 ch->ch_tstart = 0;
6769 ch->ch_rstart = 0;
6772 * Set queue water marks, interrupt mask,
6773 * and general tty parameters.
6775 tlw = ch->ch_tsize >= 2000 ? ((ch->ch_tsize * 5) / 8) :
6776 ch->ch_tsize / 2;
6777 ch->ch_tlw = tlw;
6779 dgap_cmdw(ch, STLOW, tlw, 0);
6781 dgap_cmdw(ch, SRLOW, ch->ch_rsize / 2, 0);
6783 dgap_cmdw(ch, SRHIGH, 7 * ch->ch_rsize / 8, 0);
6785 ch->ch_mistat = readb(&ch->ch_bs->m_stat);
6787 init_waitqueue_head(&ch->ch_flags_wait);
6788 init_waitqueue_head(&ch->ch_tun.un_flags_wait);
6789 init_waitqueue_head(&ch->ch_pun.un_flags_wait);
6791 /* Turn on all modem interrupts for now */
6792 modem = (DM_CD | DM_DSR | DM_CTS | DM_RI);
6793 writeb(modem, &ch->ch_bs->m_int);
6796 * Set edelay to 0 if interrupts are turned on,
6797 * otherwise set edelay to the usual 100.
6799 if (brd->intr_used)
6800 writew(0, &ch->ch_bs->edelay);
6801 else
6802 writew(100, &ch->ch_bs->edelay);
6804 writeb(1, &ch->ch_bs->idata);
6807 return 0;
6809 free_chan:
6810 while (--i >= 0) {
6811 kfree(brd->channels[i]);
6812 brd->channels[i] = NULL;
6814 return ret;
6818 * dgap_tty_free()
6820 * Free the channles which are allocated in dgap_tty_init().
6822 static void dgap_tty_free(struct board_t *brd)
6824 int i;
6826 for (i = 0; i < brd->nasync; i++)
6827 kfree(brd->channels[i]);
6830 static int dgap_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
6832 int rc;
6833 struct board_t *brd;
6835 if (dgap_numboards >= MAXBOARDS)
6836 return -EPERM;
6838 rc = pci_enable_device(pdev);
6839 if (rc)
6840 return -EIO;
6842 brd = dgap_found_board(pdev, ent->driver_data, dgap_numboards);
6843 if (IS_ERR(brd))
6844 return PTR_ERR(brd);
6846 rc = dgap_firmware_load(pdev, ent->driver_data, brd);
6847 if (rc)
6848 goto cleanup_brd;
6850 rc = dgap_alloc_flipbuf(brd);
6851 if (rc)
6852 goto cleanup_brd;
6854 rc = dgap_tty_register(brd);
6855 if (rc)
6856 goto free_flipbuf;
6858 rc = dgap_request_irq(brd);
6859 if (rc)
6860 goto unregister_tty;
6863 * Do tty device initialization.
6865 rc = dgap_tty_init(brd);
6866 if (rc < 0)
6867 goto free_irq;
6869 rc = dgap_tty_register_ports(brd);
6870 if (rc)
6871 goto tty_free;
6873 brd->state = BOARD_READY;
6874 brd->dpastatus = BD_RUNNING;
6876 dgap_board[dgap_numboards++] = brd;
6878 return 0;
6880 tty_free:
6881 dgap_tty_free(brd);
6882 free_irq:
6883 dgap_free_irq(brd);
6884 unregister_tty:
6885 dgap_tty_unregister(brd);
6886 free_flipbuf:
6887 dgap_free_flipbuf(brd);
6888 cleanup_brd:
6889 dgap_cleanup_nodes();
6890 dgap_unmap(brd);
6891 kfree(brd);
6893 return rc;
6897 * dgap_cleanup_board()
6899 * Free all the memory associated with a board
6901 static void dgap_cleanup_board(struct board_t *brd)
6903 unsigned int i;
6905 if (!brd || brd->magic != DGAP_BOARD_MAGIC)
6906 return;
6908 dgap_free_irq(brd);
6910 tasklet_kill(&brd->helper_tasklet);
6912 dgap_unmap(brd);
6914 /* Free all allocated channels structs */
6915 for (i = 0; i < MAXPORTS ; i++)
6916 kfree(brd->channels[i]);
6918 kfree(brd->flipbuf);
6919 kfree(brd->flipflagbuf);
6921 dgap_board[brd->boardnum] = NULL;
6923 kfree(brd);
6926 static void dgap_stop(bool removesys, struct pci_driver *drv)
6928 unsigned long lock_flags;
6930 spin_lock_irqsave(&dgap_poll_lock, lock_flags);
6931 dgap_poll_stop = 1;
6932 spin_unlock_irqrestore(&dgap_poll_lock, lock_flags);
6934 del_timer_sync(&dgap_poll_timer);
6935 if (removesys)
6936 dgap_remove_driver_sysfiles(drv);
6938 device_destroy(dgap_class, MKDEV(DIGI_DGAP_MAJOR, 0));
6939 class_destroy(dgap_class);
6940 unregister_chrdev(DIGI_DGAP_MAJOR, "dgap");
6943 static void dgap_remove_one(struct pci_dev *dev)
6945 unsigned int i;
6946 struct pci_driver *drv = to_pci_driver(dev->dev.driver);
6948 dgap_stop(true, drv);
6949 for (i = 0; i < dgap_numboards; ++i) {
6950 dgap_remove_ports_sysfiles(dgap_board[i]);
6951 dgap_cleanup_tty(dgap_board[i]);
6952 dgap_cleanup_board(dgap_board[i]);
6955 dgap_cleanup_nodes();
6958 static struct pci_driver dgap_driver = {
6959 .name = "dgap",
6960 .probe = dgap_init_one,
6961 .id_table = dgap_pci_tbl,
6962 .remove = dgap_remove_one,
6966 * Start of driver.
6968 static int dgap_start(void)
6970 int rc;
6971 unsigned long flags;
6972 struct device *device;
6974 dgap_numboards = 0;
6976 pr_info("For the tools package please visit http://www.digi.com\n");
6979 * Register our base character device into the kernel.
6983 * Register management/dpa devices
6985 rc = register_chrdev(DIGI_DGAP_MAJOR, "dgap", &dgap_board_fops);
6986 if (rc < 0)
6987 return rc;
6989 dgap_class = class_create(THIS_MODULE, "dgap_mgmt");
6990 if (IS_ERR(dgap_class)) {
6991 rc = PTR_ERR(dgap_class);
6992 goto failed_class;
6995 device = device_create(dgap_class, NULL,
6996 MKDEV(DIGI_DGAP_MAJOR, 0),
6997 NULL, "dgap_mgmt");
6998 if (IS_ERR(device)) {
6999 rc = PTR_ERR(device);
7000 goto failed_device;
7003 /* Start the poller */
7004 spin_lock_irqsave(&dgap_poll_lock, flags);
7005 setup_timer(&dgap_poll_timer, dgap_poll_handler, 0);
7006 dgap_poll_timer.data = 0;
7007 dgap_poll_time = jiffies + dgap_jiffies_from_ms(dgap_poll_tick);
7008 dgap_poll_timer.expires = dgap_poll_time;
7009 spin_unlock_irqrestore(&dgap_poll_lock, flags);
7011 add_timer(&dgap_poll_timer);
7013 return rc;
7015 failed_device:
7016 class_destroy(dgap_class);
7017 failed_class:
7018 unregister_chrdev(DIGI_DGAP_MAJOR, "dgap");
7019 return rc;
7022 /************************************************************************
7024 * Driver load/unload functions
7026 ************************************************************************/
7029 * init_module()
7031 * Module load. This is where it all starts.
7033 static int dgap_init_module(void)
7035 int rc;
7037 pr_info("%s, Digi International Part Number %s\n", DG_NAME, DG_PART);
7039 rc = dgap_start();
7040 if (rc)
7041 return rc;
7043 rc = pci_register_driver(&dgap_driver);
7044 if (rc) {
7045 dgap_stop(false, NULL);
7046 return rc;
7049 rc = dgap_create_driver_sysfiles(&dgap_driver);
7050 if (rc)
7051 goto err_unregister;
7053 dgap_driver_state = DRIVER_READY;
7055 return 0;
7057 err_unregister:
7058 pci_unregister_driver(&dgap_driver);
7059 return rc;
7063 * dgap_cleanup_module()
7065 * Module unload. This is where it all ends.
7067 static void dgap_cleanup_module(void)
7069 if (dgap_numboards)
7070 pci_unregister_driver(&dgap_driver);
7073 module_init(dgap_init_module);
7074 module_exit(dgap_cleanup_module);
7076 MODULE_LICENSE("GPL");
7077 MODULE_AUTHOR("Digi International, http://www.digi.com");
7078 MODULE_DESCRIPTION("Driver for the Digi International EPCA PCI based product line");
7079 MODULE_SUPPORTED_DEVICE("dgap");