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