File: /usr/src/linux/drivers/char/istallion.c

1     /*****************************************************************************/
2     
3     /*
4      *	istallion.c  -- stallion intelligent multiport serial driver.
5      *
6      *	Copyright (C) 1996-1999  Stallion Technologies (support@stallion.oz.au).
7      *	Copyright (C) 1994-1996  Greg Ungerer.
8      *
9      *	This code is loosely based on the Linux serial driver, written by
10      *	Linus Torvalds, Theodore T'so and others.
11      *
12      *	This program is free software; you can redistribute it and/or modify
13      *	it under the terms of the GNU General Public License as published by
14      *	the Free Software Foundation; either version 2 of the License, or
15      *	(at your option) any later version.
16      *
17      *	This program is distributed in the hope that it will be useful,
18      *	but WITHOUT ANY WARRANTY; without even the implied warranty of
19      *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20      *	GNU General Public License for more details.
21      *
22      *	You should have received a copy of the GNU General Public License
23      *	along with this program; if not, write to the Free Software
24      *	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25      */
26     
27     /*****************************************************************************/
28     
29     #include <linux/config.h>
30     #include <linux/module.h>
31     #include <linux/slab.h>
32     #include <linux/interrupt.h>
33     #include <linux/tty.h>
34     #include <linux/tty_flip.h>
35     #include <linux/serial.h>
36     #include <linux/cdk.h>
37     #include <linux/comstats.h>
38     #include <linux/version.h>
39     #include <linux/istallion.h>
40     #include <linux/ioport.h>
41     #include <linux/delay.h>
42     #include <linux/init.h>
43     #include <linux/devfs_fs_kernel.h>
44     
45     #include <asm/io.h>
46     #include <asm/uaccess.h>
47     
48     #ifdef CONFIG_PCI
49     #include <linux/pci.h>
50     #endif
51     
52     /*****************************************************************************/
53     
54     /*
55      *	Define different board types. Not all of the following board types
56      *	are supported by this driver. But I will use the standard "assigned"
57      *	board numbers. Currently supported boards are abbreviated as:
58      *	ECP = EasyConnection 8/64, ONB = ONboard, BBY = Brumby and
59      *	STAL = Stallion.
60      */
61     #define	BRD_UNKNOWN	0
62     #define	BRD_STALLION	1
63     #define	BRD_BRUMBY4	2
64     #define	BRD_ONBOARD2	3
65     #define	BRD_ONBOARD	4
66     #define	BRD_BRUMBY8	5
67     #define	BRD_BRUMBY16	6
68     #define	BRD_ONBOARDE	7
69     #define	BRD_ONBOARD32	9
70     #define	BRD_ONBOARD2_32	10
71     #define	BRD_ONBOARDRS	11
72     #define	BRD_EASYIO	20
73     #define	BRD_ECH		21
74     #define	BRD_ECHMC	22
75     #define	BRD_ECP		23
76     #define BRD_ECPE	24
77     #define	BRD_ECPMC	25
78     #define	BRD_ECHPCI	26
79     #define	BRD_ECH64PCI	27
80     #define	BRD_EASYIOPCI	28
81     #define	BRD_ECPPCI	29
82     
83     #define	BRD_BRUMBY	BRD_BRUMBY4
84     
85     /*
86      *	Define a configuration structure to hold the board configuration.
87      *	Need to set this up in the code (for now) with the boards that are
88      *	to be configured into the system. This is what needs to be modified
89      *	when adding/removing/modifying boards. Each line entry in the
90      *	stli_brdconf[] array is a board. Each line contains io/irq/memory
91      *	ranges for that board (as well as what type of board it is).
92      *	Some examples:
93      *		{ BRD_ECP, 0x2a0, 0, 0xcc000, 0, 0 },
94      *	This line will configure an EasyConnection 8/64 at io address 2a0,
95      *	and shared memory address of cc000. Multiple EasyConnection 8/64
96      *	boards can share the same shared memory address space. No interrupt
97      *	is required for this board type.
98      *	Another example:
99      *		{ BRD_ECPE, 0x5000, 0, 0x80000000, 0, 0 },
100      *	This line will configure an EasyConnection 8/64 EISA in slot 5 and
101      *	shared memory address of 0x80000000 (2 GByte). Multiple
102      *	EasyConnection 8/64 EISA boards can share the same shared memory
103      *	address space. No interrupt is required for this board type.
104      *	Another example:
105      *		{ BRD_ONBOARD, 0x240, 0, 0xd0000, 0, 0 },
106      *	This line will configure an ONboard (ISA type) at io address 240,
107      *	and shared memory address of d0000. Multiple ONboards can share
108      *	the same shared memory address space. No interrupt required.
109      *	Another example:
110      *		{ BRD_BRUMBY4, 0x360, 0, 0xc8000, 0, 0 },
111      *	This line will configure a Brumby board (any number of ports!) at
112      *	io address 360 and shared memory address of c8000. All Brumby boards
113      *	configured into a system must have their own separate io and memory
114      *	addresses. No interrupt is required.
115      *	Another example:
116      *		{ BRD_STALLION, 0x330, 0, 0xd0000, 0, 0 },
117      *	This line will configure an original Stallion board at io address 330
118      *	and shared memory address d0000 (this would only be valid for a "V4.0"
119      *	or Rev.O Stallion board). All Stallion boards configured into the
120      *	system must have their own separate io and memory addresses. No
121      *	interrupt is required.
122      */
123     
124     typedef struct {
125     	int		brdtype;
126     	int		ioaddr1;
127     	int		ioaddr2;
128     	unsigned long	memaddr;
129     	int		irq;
130     	int		irqtype;
131     } stlconf_t;
132     
133     static stlconf_t	stli_brdconf[] = {
134     	/*{ BRD_ECP, 0x2a0, 0, 0xcc000, 0, 0 },*/
135     };
136     
137     static int	stli_nrbrds = sizeof(stli_brdconf) / sizeof(stlconf_t);
138     
139     /*
140      *	There is some experimental EISA board detection code in this driver.
141      *	By default it is disabled, but for those that want to try it out,
142      *	then set the define below to be 1.
143      */
144     #define	STLI_EISAPROBE	0
145     
146     static devfs_handle_t devfs_handle;
147     
148     /*****************************************************************************/
149     
150     /*
151      *	Define some important driver characteristics. Device major numbers
152      *	allocated as per Linux Device Registry.
153      */
154     #ifndef	STL_SIOMEMMAJOR
155     #define	STL_SIOMEMMAJOR		28
156     #endif
157     #ifndef	STL_SERIALMAJOR
158     #define	STL_SERIALMAJOR		24
159     #endif
160     #ifndef	STL_CALLOUTMAJOR
161     #define	STL_CALLOUTMAJOR	25
162     #endif
163     
164     #define	STL_DRVTYPSERIAL	1
165     #define	STL_DRVTYPCALLOUT	2
166     
167     /*****************************************************************************/
168     
169     /*
170      *	Define our local driver identity first. Set up stuff to deal with
171      *	all the local structures required by a serial tty driver.
172      */
173     static char	*stli_drvtitle = "Stallion Intelligent Multiport Serial Driver";
174     static char	*stli_drvname = "istallion";
175     static char	*stli_drvversion = "5.6.0";
176     static char	*stli_serialname = "ttyE";
177     static char	*stli_calloutname = "cue";
178     
179     static struct tty_driver	stli_serial;
180     static struct tty_driver	stli_callout;
181     static struct tty_struct	*stli_ttys[STL_MAXDEVS];
182     static struct termios		*stli_termios[STL_MAXDEVS];
183     static struct termios		*stli_termioslocked[STL_MAXDEVS];
184     static int			stli_refcount;
185     
186     /*
187      *	We will need to allocate a temporary write buffer for chars that
188      *	come direct from user space. The problem is that a copy from user
189      *	space might cause a page fault (typically on a system that is
190      *	swapping!). All ports will share one buffer - since if the system
191      *	is already swapping a shared buffer won't make things any worse.
192      */
193     static char			*stli_tmpwritebuf;
194     static DECLARE_MUTEX(stli_tmpwritesem);
195     
196     #define	STLI_TXBUFSIZE		4096
197     
198     /*
199      *	Use a fast local buffer for cooked characters. Typically a whole
200      *	bunch of cooked characters come in for a port, 1 at a time. So we
201      *	save those up into a local buffer, then write out the whole lot
202      *	with a large memcpy. Just use 1 buffer for all ports, since its
203      *	use it is only need for short periods of time by each port.
204      */
205     static char			*stli_txcookbuf;
206     static int			stli_txcooksize;
207     static int			stli_txcookrealsize;
208     static struct tty_struct	*stli_txcooktty;
209     
210     /*
211      *	Define a local default termios struct. All ports will be created
212      *	with this termios initially. Basically all it defines is a raw port
213      *	at 9600 baud, 8 data bits, no parity, 1 stop bit.
214      */
215     static struct termios		stli_deftermios = {
216     	c_cflag:	(B9600 | CS8 | CREAD | HUPCL | CLOCAL),
217     	c_cc:		INIT_C_CC,
218     };
219     
220     /*
221      *	Define global stats structures. Not used often, and can be
222      *	re-used for each stats call.
223      */
224     static comstats_t	stli_comstats;
225     static combrd_t		stli_brdstats;
226     static asystats_t	stli_cdkstats;
227     static stlibrd_t	stli_dummybrd;
228     static stliport_t	stli_dummyport;
229     
230     /*****************************************************************************/
231     
232     static stlibrd_t	*stli_brds[STL_MAXBRDS];
233     
234     static int		stli_shared;
235     
236     /*
237      *	Per board state flags. Used with the state field of the board struct.
238      *	Not really much here... All we need to do is keep track of whether
239      *	the board has been detected, and whether it is actually running a slave
240      *	or not.
241      */
242     #define	BST_FOUND	0x1
243     #define	BST_STARTED	0x2
244     
245     /*
246      *	Define the set of port state flags. These are marked for internal
247      *	state purposes only, usually to do with the state of communications
248      *	with the slave. Most of them need to be updated atomically, so always
249      *	use the bit setting operations (unless protected by cli/sti).
250      */
251     #define	ST_INITIALIZING	1
252     #define	ST_OPENING	2
253     #define	ST_CLOSING	3
254     #define	ST_CMDING	4
255     #define	ST_TXBUSY	5
256     #define	ST_RXING	6
257     #define	ST_DOFLUSHRX	7
258     #define	ST_DOFLUSHTX	8
259     #define	ST_DOSIGS	9
260     #define	ST_RXSTOP	10
261     #define	ST_GETSIGS	11
262     
263     /*
264      *	Define an array of board names as printable strings. Handy for
265      *	referencing boards when printing trace and stuff.
266      */
267     static char	*stli_brdnames[] = {
268     	"Unknown",
269     	"Stallion",
270     	"Brumby",
271     	"ONboard-MC",
272     	"ONboard",
273     	"Brumby",
274     	"Brumby",
275     	"ONboard-EI",
276     	(char *) NULL,
277     	"ONboard",
278     	"ONboard-MC",
279     	"ONboard-MC",
280     	(char *) NULL,
281     	(char *) NULL,
282     	(char *) NULL,
283     	(char *) NULL,
284     	(char *) NULL,
285     	(char *) NULL,
286     	(char *) NULL,
287     	(char *) NULL,
288     	"EasyIO",
289     	"EC8/32-AT",
290     	"EC8/32-MC",
291     	"EC8/64-AT",
292     	"EC8/64-EI",
293     	"EC8/64-MC",
294     	"EC8/32-PCI",
295     	"EC8/64-PCI",
296     	"EasyIO-PCI",
297     	"EC/RA-PCI",
298     };
299     
300     /*****************************************************************************/
301     
302     #ifdef MODULE
303     /*
304      *	Define some string labels for arguments passed from the module
305      *	load line. These allow for easy board definitions, and easy
306      *	modification of the io, memory and irq resoucres.
307      */
308     
309     static char	*board0[8];
310     static char	*board1[8];
311     static char	*board2[8];
312     static char	*board3[8];
313     
314     static char	**stli_brdsp[] = {
315     	(char **) &board0,
316     	(char **) &board1,
317     	(char **) &board2,
318     	(char **) &board3
319     };
320     
321     /*
322      *	Define a set of common board names, and types. This is used to
323      *	parse any module arguments.
324      */
325     
326     typedef struct stlibrdtype {
327     	char	*name;
328     	int	type;
329     } stlibrdtype_t;
330     
331     static stlibrdtype_t	stli_brdstr[] = {
332     	{ "stallion", BRD_STALLION },
333     	{ "1", BRD_STALLION },
334     	{ "brumby", BRD_BRUMBY },
335     	{ "brumby4", BRD_BRUMBY },
336     	{ "brumby/4", BRD_BRUMBY },
337     	{ "brumby-4", BRD_BRUMBY },
338     	{ "brumby8", BRD_BRUMBY },
339     	{ "brumby/8", BRD_BRUMBY },
340     	{ "brumby-8", BRD_BRUMBY },
341     	{ "brumby16", BRD_BRUMBY },
342     	{ "brumby/16", BRD_BRUMBY },
343     	{ "brumby-16", BRD_BRUMBY },
344     	{ "2", BRD_BRUMBY },
345     	{ "onboard2", BRD_ONBOARD2 },
346     	{ "onboard-2", BRD_ONBOARD2 },
347     	{ "onboard/2", BRD_ONBOARD2 },
348     	{ "onboard-mc", BRD_ONBOARD2 },
349     	{ "onboard/mc", BRD_ONBOARD2 },
350     	{ "onboard-mca", BRD_ONBOARD2 },
351     	{ "onboard/mca", BRD_ONBOARD2 },
352     	{ "3", BRD_ONBOARD2 },
353     	{ "onboard", BRD_ONBOARD },
354     	{ "onboardat", BRD_ONBOARD },
355     	{ "4", BRD_ONBOARD },
356     	{ "onboarde", BRD_ONBOARDE },
357     	{ "onboard-e", BRD_ONBOARDE },
358     	{ "onboard/e", BRD_ONBOARDE },
359     	{ "onboard-ei", BRD_ONBOARDE },
360     	{ "onboard/ei", BRD_ONBOARDE },
361     	{ "7", BRD_ONBOARDE },
362     	{ "ecp", BRD_ECP },
363     	{ "ecpat", BRD_ECP },
364     	{ "ec8/64", BRD_ECP },
365     	{ "ec8/64-at", BRD_ECP },
366     	{ "ec8/64-isa", BRD_ECP },
367     	{ "23", BRD_ECP },
368     	{ "ecpe", BRD_ECPE },
369     	{ "ecpei", BRD_ECPE },
370     	{ "ec8/64-e", BRD_ECPE },
371     	{ "ec8/64-ei", BRD_ECPE },
372     	{ "24", BRD_ECPE },
373     	{ "ecpmc", BRD_ECPMC },
374     	{ "ec8/64-mc", BRD_ECPMC },
375     	{ "ec8/64-mca", BRD_ECPMC },
376     	{ "25", BRD_ECPMC },
377     	{ "ecppci", BRD_ECPPCI },
378     	{ "ec/ra", BRD_ECPPCI },
379     	{ "ec/ra-pc", BRD_ECPPCI },
380     	{ "ec/ra-pci", BRD_ECPPCI },
381     	{ "29", BRD_ECPPCI },
382     };
383     
384     /*
385      *	Define the module agruments.
386      */
387     MODULE_AUTHOR("Greg Ungerer");
388     MODULE_DESCRIPTION("Stallion Intelligent Multiport Serial Driver");
389     
390     MODULE_PARM(board0, "1-3s");
391     MODULE_PARM_DESC(board0, "Board 0 config -> name[,ioaddr[,memaddr]");
392     MODULE_PARM(board1, "1-3s");
393     MODULE_PARM_DESC(board1, "Board 1 config -> name[,ioaddr[,memaddr]");
394     MODULE_PARM(board2, "1-3s");
395     MODULE_PARM_DESC(board2, "Board 2 config -> name[,ioaddr[,memaddr]");
396     MODULE_PARM(board3, "1-3s");
397     MODULE_PARM_DESC(board3, "Board 3 config -> name[,ioaddr[,memaddr]");
398     
399     #endif
400     
401     /*
402      *	Set up a default memory address table for EISA board probing.
403      *	The default addresses are all bellow 1Mbyte, which has to be the
404      *	case anyway. They should be safe, since we only read values from
405      *	them, and interrupts are disabled while we do it. If the higher
406      *	memory support is compiled in then we also try probing around
407      *	the 1Gb, 2Gb and 3Gb areas as well...
408      */
409     static unsigned long	stli_eisamemprobeaddrs[] = {
410     	0xc0000,    0xd0000,    0xe0000,    0xf0000,
411     	0x80000000, 0x80010000, 0x80020000, 0x80030000,
412     	0x40000000, 0x40010000, 0x40020000, 0x40030000,
413     	0xc0000000, 0xc0010000, 0xc0020000, 0xc0030000,
414     	0xff000000, 0xff010000, 0xff020000, 0xff030000,
415     };
416     
417     static int	stli_eisamempsize = sizeof(stli_eisamemprobeaddrs) / sizeof(unsigned long);
418     int		stli_eisaprobe = STLI_EISAPROBE;
419     
420     /*
421      *	Define the Stallion PCI vendor and device IDs.
422      */
423     #ifdef CONFIG_PCI
424     #ifndef	PCI_VENDOR_ID_STALLION
425     #define	PCI_VENDOR_ID_STALLION		0x124d
426     #endif
427     #ifndef PCI_DEVICE_ID_ECRA
428     #define	PCI_DEVICE_ID_ECRA		0x0004
429     #endif
430     #endif
431     
432     /*****************************************************************************/
433     
434     /*
435      *	Hardware configuration info for ECP boards. These defines apply
436      *	to the directly accessible io ports of the ECP. There is a set of
437      *	defines for each ECP board type, ISA, EISA, MCA and PCI.
438      */
439     #define	ECP_IOSIZE	4
440     
441     #define	ECP_MEMSIZE	(128 * 1024)
442     #define	ECP_PCIMEMSIZE	(256 * 1024)
443     
444     #define	ECP_ATPAGESIZE	(4 * 1024)
445     #define	ECP_MCPAGESIZE	(4 * 1024)
446     #define	ECP_EIPAGESIZE	(64 * 1024)
447     #define	ECP_PCIPAGESIZE	(64 * 1024)
448     
449     #define	STL_EISAID	0x8c4e
450     
451     /*
452      *	Important defines for the ISA class of ECP board.
453      */
454     #define	ECP_ATIREG	0
455     #define	ECP_ATCONFR	1
456     #define	ECP_ATMEMAR	2
457     #define	ECP_ATMEMPR	3
458     #define	ECP_ATSTOP	0x1
459     #define	ECP_ATINTENAB	0x10
460     #define	ECP_ATENABLE	0x20
461     #define	ECP_ATDISABLE	0x00
462     #define	ECP_ATADDRMASK	0x3f000
463     #define	ECP_ATADDRSHFT	12
464     
465     /*
466      *	Important defines for the EISA class of ECP board.
467      */
468     #define	ECP_EIIREG	0
469     #define	ECP_EIMEMARL	1
470     #define	ECP_EICONFR	2
471     #define	ECP_EIMEMARH	3
472     #define	ECP_EIENABLE	0x1
473     #define	ECP_EIDISABLE	0x0
474     #define	ECP_EISTOP	0x4
475     #define	ECP_EIEDGE	0x00
476     #define	ECP_EILEVEL	0x80
477     #define	ECP_EIADDRMASKL	0x00ff0000
478     #define	ECP_EIADDRSHFTL	16
479     #define	ECP_EIADDRMASKH	0xff000000
480     #define	ECP_EIADDRSHFTH	24
481     #define	ECP_EIBRDENAB	0xc84
482     
483     #define	ECP_EISAID	0x4
484     
485     /*
486      *	Important defines for the Micro-channel class of ECP board.
487      *	(It has a lot in common with the ISA boards.)
488      */
489     #define	ECP_MCIREG	0
490     #define	ECP_MCCONFR	1
491     #define	ECP_MCSTOP	0x20
492     #define	ECP_MCENABLE	0x80
493     #define	ECP_MCDISABLE	0x00
494     
495     /*
496      *	Important defines for the PCI class of ECP board.
497      *	(It has a lot in common with the other ECP boards.)
498      */
499     #define	ECP_PCIIREG	0
500     #define	ECP_PCICONFR	1
501     #define	ECP_PCISTOP	0x01
502     
503     /*
504      *	Hardware configuration info for ONboard and Brumby boards. These
505      *	defines apply to the directly accessible io ports of these boards.
506      */
507     #define	ONB_IOSIZE	16
508     #define	ONB_MEMSIZE	(64 * 1024)
509     #define	ONB_ATPAGESIZE	(64 * 1024)
510     #define	ONB_MCPAGESIZE	(64 * 1024)
511     #define	ONB_EIMEMSIZE	(128 * 1024)
512     #define	ONB_EIPAGESIZE	(64 * 1024)
513     
514     /*
515      *	Important defines for the ISA class of ONboard board.
516      */
517     #define	ONB_ATIREG	0
518     #define	ONB_ATMEMAR	1
519     #define	ONB_ATCONFR	2
520     #define	ONB_ATSTOP	0x4
521     #define	ONB_ATENABLE	0x01
522     #define	ONB_ATDISABLE	0x00
523     #define	ONB_ATADDRMASK	0xff0000
524     #define	ONB_ATADDRSHFT	16
525     
526     #define	ONB_MEMENABLO	0
527     #define	ONB_MEMENABHI	0x02
528     
529     /*
530      *	Important defines for the EISA class of ONboard board.
531      */
532     #define	ONB_EIIREG	0
533     #define	ONB_EIMEMARL	1
534     #define	ONB_EICONFR	2
535     #define	ONB_EIMEMARH	3
536     #define	ONB_EIENABLE	0x1
537     #define	ONB_EIDISABLE	0x0
538     #define	ONB_EISTOP	0x4
539     #define	ONB_EIEDGE	0x00
540     #define	ONB_EILEVEL	0x80
541     #define	ONB_EIADDRMASKL	0x00ff0000
542     #define	ONB_EIADDRSHFTL	16
543     #define	ONB_EIADDRMASKH	0xff000000
544     #define	ONB_EIADDRSHFTH	24
545     #define	ONB_EIBRDENAB	0xc84
546     
547     #define	ONB_EISAID	0x1
548     
549     /*
550      *	Important defines for the Brumby boards. They are pretty simple,
551      *	there is not much that is programmably configurable.
552      */
553     #define	BBY_IOSIZE	16
554     #define	BBY_MEMSIZE	(64 * 1024)
555     #define	BBY_PAGESIZE	(16 * 1024)
556     
557     #define	BBY_ATIREG	0
558     #define	BBY_ATCONFR	1
559     #define	BBY_ATSTOP	0x4
560     
561     /*
562      *	Important defines for the Stallion boards. They are pretty simple,
563      *	there is not much that is programmably configurable.
564      */
565     #define	STAL_IOSIZE	16
566     #define	STAL_MEMSIZE	(64 * 1024)
567     #define	STAL_PAGESIZE	(64 * 1024)
568     
569     /*
570      *	Define the set of status register values for EasyConnection panels.
571      *	The signature will return with the status value for each panel. From
572      *	this we can determine what is attached to the board - before we have
573      *	actually down loaded any code to it.
574      */
575     #define	ECH_PNLSTATUS	2
576     #define	ECH_PNL16PORT	0x20
577     #define	ECH_PNLIDMASK	0x07
578     #define	ECH_PNLXPID	0x40
579     #define	ECH_PNLINTRPEND	0x80
580     
581     /*
582      *	Define some macros to do things to the board. Even those these boards
583      *	are somewhat related there is often significantly different ways of
584      *	doing some operation on it (like enable, paging, reset, etc). So each
585      *	board class has a set of functions which do the commonly required
586      *	operations. The macros below basically just call these functions,
587      *	generally checking for a NULL function - which means that the board
588      *	needs nothing done to it to achieve this operation!
589      */
590     #define	EBRDINIT(brdp)						\
591     	if (brdp->init != NULL)					\
592     		(* brdp->init)(brdp)
593     
594     #define	EBRDENABLE(brdp)					\
595     	if (brdp->enable != NULL)				\
596     		(* brdp->enable)(brdp);
597     
598     #define	EBRDDISABLE(brdp)					\
599     	if (brdp->disable != NULL)				\
600     		(* brdp->disable)(brdp);
601     
602     #define	EBRDINTR(brdp)						\
603     	if (brdp->intr != NULL)					\
604     		(* brdp->intr)(brdp);
605     
606     #define	EBRDRESET(brdp)						\
607     	if (brdp->reset != NULL)				\
608     		(* brdp->reset)(brdp);
609     
610     #define	EBRDGETMEMPTR(brdp,offset)				\
611     	(* brdp->getmemptr)(brdp, offset, __LINE__)
612     
613     /*
614      *	Define the maximal baud rate, and the default baud base for ports.
615      */
616     #define	STL_MAXBAUD	460800
617     #define	STL_BAUDBASE	115200
618     #define	STL_CLOSEDELAY	(5 * HZ / 10)
619     
620     /*****************************************************************************/
621     
622     /*
623      *	Define macros to extract a brd or port number from a minor number.
624      */
625     #define	MINOR2BRD(min)		(((min) & 0xc0) >> 6)
626     #define	MINOR2PORT(min)		((min) & 0x3f)
627     
628     /*
629      *	Define a baud rate table that converts termios baud rate selector
630      *	into the actual baud rate value. All baud rate calculations are based
631      *	on the actual baud rate required.
632      */
633     static unsigned int	stli_baudrates[] = {
634     	0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
635     	9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600
636     };
637     
638     /*****************************************************************************/
639     
640     /*
641      *	Define some handy local macros...
642      */
643     #undef MIN
644     #define	MIN(a,b)	(((a) <= (b)) ? (a) : (b))
645     
646     #undef	TOLOWER
647     #define	TOLOWER(x)	((((x) >= 'A') && ((x) <= 'Z')) ? ((x) + 0x20) : (x))
648     
649     /*****************************************************************************/
650     
651     /*
652      *	Prototype all functions in this driver!
653      */
654     
655     #ifdef MODULE
656     int		init_module(void);
657     void		cleanup_module(void);
658     static void	stli_argbrds(void);
659     static int	stli_parsebrd(stlconf_t *confp, char **argp);
660     
661     static unsigned long	stli_atol(char *str);
662     #endif
663     
664     int		stli_init(void);
665     static int	stli_open(struct tty_struct *tty, struct file *filp);
666     static void	stli_close(struct tty_struct *tty, struct file *filp);
667     static int	stli_write(struct tty_struct *tty, int from_user, const unsigned char *buf, int count);
668     static void	stli_putchar(struct tty_struct *tty, unsigned char ch);
669     static void	stli_flushchars(struct tty_struct *tty);
670     static int	stli_writeroom(struct tty_struct *tty);
671     static int	stli_charsinbuffer(struct tty_struct *tty);
672     static int	stli_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg);
673     static void	stli_settermios(struct tty_struct *tty, struct termios *old);
674     static void	stli_throttle(struct tty_struct *tty);
675     static void	stli_unthrottle(struct tty_struct *tty);
676     static void	stli_stop(struct tty_struct *tty);
677     static void	stli_start(struct tty_struct *tty);
678     static void	stli_flushbuffer(struct tty_struct *tty);
679     static void	stli_breakctl(struct tty_struct *tty, int state);
680     static void	stli_waituntilsent(struct tty_struct *tty, int timeout);
681     static void	stli_sendxchar(struct tty_struct *tty, char ch);
682     static void	stli_hangup(struct tty_struct *tty);
683     static int	stli_portinfo(stlibrd_t *brdp, stliport_t *portp, int portnr, char *pos);
684     
685     static int	stli_brdinit(stlibrd_t *brdp);
686     static int	stli_startbrd(stlibrd_t *brdp);
687     static ssize_t	stli_memread(struct file *fp, char *buf, size_t count, loff_t *offp);
688     static ssize_t	stli_memwrite(struct file *fp, const char *buf, size_t count, loff_t *offp);
689     static int	stli_memioctl(struct inode *ip, struct file *fp, unsigned int cmd, unsigned long arg);
690     static void	stli_brdpoll(stlibrd_t *brdp, volatile cdkhdr_t *hdrp);
691     static void	stli_poll(unsigned long arg);
692     static int	stli_hostcmd(stlibrd_t *brdp, stliport_t *portp);
693     static int	stli_initopen(stlibrd_t *brdp, stliport_t *portp);
694     static int	stli_rawopen(stlibrd_t *brdp, stliport_t *portp, unsigned long arg, int wait);
695     static int	stli_rawclose(stlibrd_t *brdp, stliport_t *portp, unsigned long arg, int wait);
696     static int	stli_waitcarrier(stlibrd_t *brdp, stliport_t *portp, struct file *filp);
697     static void	stli_dohangup(void *arg);
698     static void	stli_delay(int len);
699     static int	stli_setport(stliport_t *portp);
700     static int	stli_cmdwait(stlibrd_t *brdp, stliport_t *portp, unsigned long cmd, void *arg, int size, int copyback);
701     static void	stli_sendcmd(stlibrd_t *brdp, stliport_t *portp, unsigned long cmd, void *arg, int size, int copyback);
702     static void	stli_dodelaycmd(stliport_t *portp, volatile cdkctrl_t *cp);
703     static void	stli_mkasyport(stliport_t *portp, asyport_t *pp, struct termios *tiosp);
704     static void	stli_mkasysigs(asysigs_t *sp, int dtr, int rts);
705     static long	stli_mktiocm(unsigned long sigvalue);
706     static void	stli_read(stlibrd_t *brdp, stliport_t *portp);
707     static void	stli_getserial(stliport_t *portp, struct serial_struct *sp);
708     static int	stli_setserial(stliport_t *portp, struct serial_struct *sp);
709     static int	stli_getbrdstats(combrd_t *bp);
710     static int	stli_getportstats(stliport_t *portp, comstats_t *cp);
711     static int	stli_portcmdstats(stliport_t *portp);
712     static int	stli_clrportstats(stliport_t *portp, comstats_t *cp);
713     static int	stli_getportstruct(unsigned long arg);
714     static int	stli_getbrdstruct(unsigned long arg);
715     static void	*stli_memalloc(int len);
716     static stlibrd_t *stli_allocbrd(void);
717     
718     static void	stli_ecpinit(stlibrd_t *brdp);
719     static void	stli_ecpenable(stlibrd_t *brdp);
720     static void	stli_ecpdisable(stlibrd_t *brdp);
721     static char	*stli_ecpgetmemptr(stlibrd_t *brdp, unsigned long offset, int line);
722     static void	stli_ecpreset(stlibrd_t *brdp);
723     static void	stli_ecpintr(stlibrd_t *brdp);
724     static void	stli_ecpeiinit(stlibrd_t *brdp);
725     static void	stli_ecpeienable(stlibrd_t *brdp);
726     static void	stli_ecpeidisable(stlibrd_t *brdp);
727     static char	*stli_ecpeigetmemptr(stlibrd_t *brdp, unsigned long offset, int line);
728     static void	stli_ecpeireset(stlibrd_t *brdp);
729     static void	stli_ecpmcenable(stlibrd_t *brdp);
730     static void	stli_ecpmcdisable(stlibrd_t *brdp);
731     static char	*stli_ecpmcgetmemptr(stlibrd_t *brdp, unsigned long offset, int line);
732     static void	stli_ecpmcreset(stlibrd_t *brdp);
733     static void	stli_ecppciinit(stlibrd_t *brdp);
734     static char	*stli_ecppcigetmemptr(stlibrd_t *brdp, unsigned long offset, int line);
735     static void	stli_ecppcireset(stlibrd_t *brdp);
736     
737     static void	stli_onbinit(stlibrd_t *brdp);
738     static void	stli_onbenable(stlibrd_t *brdp);
739     static void	stli_onbdisable(stlibrd_t *brdp);
740     static char	*stli_onbgetmemptr(stlibrd_t *brdp, unsigned long offset, int line);
741     static void	stli_onbreset(stlibrd_t *brdp);
742     static void	stli_onbeinit(stlibrd_t *brdp);
743     static void	stli_onbeenable(stlibrd_t *brdp);
744     static void	stli_onbedisable(stlibrd_t *brdp);
745     static char	*stli_onbegetmemptr(stlibrd_t *brdp, unsigned long offset, int line);
746     static void	stli_onbereset(stlibrd_t *brdp);
747     static void	stli_bbyinit(stlibrd_t *brdp);
748     static char	*stli_bbygetmemptr(stlibrd_t *brdp, unsigned long offset, int line);
749     static void	stli_bbyreset(stlibrd_t *brdp);
750     static void	stli_stalinit(stlibrd_t *brdp);
751     static char	*stli_stalgetmemptr(stlibrd_t *brdp, unsigned long offset, int line);
752     static void	stli_stalreset(stlibrd_t *brdp);
753     
754     static stliport_t *stli_getport(int brdnr, int panelnr, int portnr);
755     
756     static inline int	stli_initbrds(void);
757     static inline int	stli_initecp(stlibrd_t *brdp);
758     static inline int	stli_initonb(stlibrd_t *brdp);
759     static inline int	stli_findeisabrds(void);
760     static inline int	stli_eisamemprobe(stlibrd_t *brdp);
761     static inline int	stli_initports(stlibrd_t *brdp);
762     static inline int	stli_getbrdnr(void);
763     
764     #ifdef	CONFIG_PCI
765     static inline int	stli_findpcibrds(void);
766     static inline int	stli_initpcibrd(int brdtype, struct pci_dev *devp);
767     #endif
768     
769     /*****************************************************************************/
770     
771     /*
772      *	Define the driver info for a user level shared memory device. This
773      *	device will work sort of like the /dev/kmem device - except that it
774      *	will give access to the shared memory on the Stallion intelligent
775      *	board. This is also a very useful debugging tool.
776      */
777     static struct file_operations	stli_fsiomem = {
778     	owner:		THIS_MODULE,
779     	read:		stli_memread,
780     	write:		stli_memwrite,
781     	ioctl:		stli_memioctl,
782     };
783     
784     /*****************************************************************************/
785     
786     /*
787      *	Define a timer_list entry for our poll routine. The slave board
788      *	is polled every so often to see if anything needs doing. This is
789      *	much cheaper on host cpu than using interrupts. It turns out to
790      *	not increase character latency by much either...
791      */
792     static struct timer_list	stli_timerlist = {
793     	function: stli_poll
794     };
795     
796     static int	stli_timeron;
797     
798     /*
799      *	Define the calculation for the timeout routine.
800      */
801     #define	STLI_TIMEOUT	(jiffies + 1)
802     
803     /*****************************************************************************/
804     
805     #ifdef MODULE
806     
807     /*
808      *	Loadable module initialization stuff.
809      */
810     
811     int init_module()
812     {
813     	unsigned long	flags;
814     
815     #if DEBUG
816     	printk("init_module()\n");
817     #endif
818     
819     	save_flags(flags);
820     	cli();
821     	stli_init();
822     	restore_flags(flags);
823     
824     	return(0);
825     }
826     
827     /*****************************************************************************/
828     
829     void cleanup_module()
830     {
831     	stlibrd_t	*brdp;
832     	stliport_t	*portp;
833     	unsigned long	flags;
834     	int		i, j;
835     
836     #if DEBUG
837     	printk("cleanup_module()\n");
838     #endif
839     
840     	printk(KERN_INFO "Unloading %s: version %s\n", stli_drvtitle,
841     		stli_drvversion);
842     
843     	save_flags(flags);
844     	cli();
845     
846     /*
847      *	Free up all allocated resources used by the ports. This includes
848      *	memory and interrupts.
849      */
850     	if (stli_timeron) {
851     		stli_timeron = 0;
852     		del_timer(&stli_timerlist);
853     	}
854     
855     	i = tty_unregister_driver(&stli_serial);
856     	j = tty_unregister_driver(&stli_callout);
857     	if (i || j) {
858     		printk("STALLION: failed to un-register tty driver, "
859     			"errno=%d,%d\n", -i, -j);
860     		restore_flags(flags);
861     		return;
862     	}
863     	devfs_unregister (devfs_handle);
864     	if ((i = devfs_unregister_chrdev(STL_SIOMEMMAJOR, "staliomem")))
865     		printk("STALLION: failed to un-register serial memory device, "
866     			"errno=%d\n", -i);
867     	if (stli_tmpwritebuf != (char *) NULL)
868     		kfree(stli_tmpwritebuf);
869     	if (stli_txcookbuf != (char *) NULL)
870     		kfree(stli_txcookbuf);
871     
872     	for (i = 0; (i < stli_nrbrds); i++) {
873     		if ((brdp = stli_brds[i]) == (stlibrd_t *) NULL)
874     			continue;
875     		for (j = 0; (j < STL_MAXPORTS); j++) {
876     			portp = brdp->ports[j];
877     			if (portp != (stliport_t *) NULL) {
878     				if (portp->tty != (struct tty_struct *) NULL)
879     					tty_hangup(portp->tty);
880     				kfree(portp);
881     			}
882     		}
883     
884     		iounmap(brdp->membase);
885     		if (brdp->iosize > 0)
886     			release_region(brdp->iobase, brdp->iosize);
887     		kfree(brdp);
888     		stli_brds[i] = (stlibrd_t *) NULL;
889     	}
890     
891     	restore_flags(flags);
892     }
893     
894     /*****************************************************************************/
895     
896     /*
897      *	Check for any arguments passed in on the module load command line.
898      */
899     
900     static void stli_argbrds()
901     {
902     	stlconf_t	conf;
903     	stlibrd_t	*brdp;
904     	int		nrargs, i;
905     
906     #if DEBUG
907     	printk("stli_argbrds()\n");
908     #endif
909     
910     	nrargs = sizeof(stli_brdsp) / sizeof(char **);
911     
912     	for (i = stli_nrbrds; (i < nrargs); i++) {
913     		memset(&conf, 0, sizeof(conf));
914     		if (stli_parsebrd(&conf, stli_brdsp[i]) == 0)
915     			continue;
916     		if ((brdp = stli_allocbrd()) == (stlibrd_t *) NULL)
917     			continue;
918     		stli_nrbrds = i + 1;
919     		brdp->brdnr = i;
920     		brdp->brdtype = conf.brdtype;
921     		brdp->iobase = conf.ioaddr1;
922     		brdp->memaddr = conf.memaddr;
923     		stli_brdinit(brdp);
924     	}
925     }
926     
927     /*****************************************************************************/
928     
929     /*
930      *	Convert an ascii string number into an unsigned long.
931      */
932     
933     static unsigned long stli_atol(char *str)
934     {
935     	unsigned long	val;
936     	int		base, c;
937     	char		*sp;
938     
939     	val = 0;
940     	sp = str;
941     	if ((*sp == '0') && (*(sp+1) == 'x')) {
942     		base = 16;
943     		sp += 2;
944     	} else if (*sp == '0') {
945     		base = 8;
946     		sp++;
947     	} else {
948     		base = 10;
949     	}
950     
951     	for (; (*sp != 0); sp++) {
952     		c = (*sp > '9') ? (TOLOWER(*sp) - 'a' + 10) : (*sp - '0');
953     		if ((c < 0) || (c >= base)) {
954     			printk("STALLION: invalid argument %s\n", str);
955     			val = 0;
956     			break;
957     		}
958     		val = (val * base) + c;
959     	}
960     	return(val);
961     }
962     
963     /*****************************************************************************/
964     
965     /*
966      *	Parse the supplied argument string, into the board conf struct.
967      */
968     
969     static int stli_parsebrd(stlconf_t *confp, char **argp)
970     {
971     	char	*sp;
972     	int	nrbrdnames, i;
973     
974     #if DEBUG
975     	printk("stli_parsebrd(confp=%x,argp=%x)\n", (int) confp, (int) argp);
976     #endif
977     
978     	if ((argp[0] == (char *) NULL) || (*argp[0] == 0))
979     		return(0);
980     
981     	for (sp = argp[0], i = 0; ((*sp != 0) && (i < 25)); sp++, i++)
982     		*sp = TOLOWER(*sp);
983     
984     	nrbrdnames = sizeof(stli_brdstr) / sizeof(stlibrdtype_t);
985     	for (i = 0; (i < nrbrdnames); i++) {
986     		if (strcmp(stli_brdstr[i].name, argp[0]) == 0)
987     			break;
988     	}
989     	if (i >= nrbrdnames) {
990     		printk("STALLION: unknown board name, %s?\n", argp[0]);
991     		return(0);
992     	}
993     
994     	confp->brdtype = stli_brdstr[i].type;
995     	if ((argp[1] != (char *) NULL) && (*argp[1] != 0))
996     		confp->ioaddr1 = stli_atol(argp[1]);
997     	if ((argp[2] != (char *) NULL) && (*argp[2] != 0))
998     		confp->memaddr = stli_atol(argp[2]);
999     	return(1);
1000     }
1001     
1002     #endif
1003     
1004     /*****************************************************************************/
1005     
1006     /*
1007      *	Local driver kernel malloc routine.
1008      */
1009     
1010     static void *stli_memalloc(int len)
1011     {
1012     	return((void *) kmalloc(len, GFP_KERNEL));
1013     }
1014     
1015     /*****************************************************************************/
1016     
1017     static int stli_open(struct tty_struct *tty, struct file *filp)
1018     {
1019     	stlibrd_t	*brdp;
1020     	stliport_t	*portp;
1021     	unsigned int	minordev;
1022     	int		brdnr, portnr, rc;
1023     
1024     #if DEBUG
1025     	printk("stli_open(tty=%x,filp=%x): device=%x\n", (int) tty,
1026     		(int) filp, tty->device);
1027     #endif
1028     
1029     	minordev = MINOR(tty->device);
1030     	brdnr = MINOR2BRD(minordev);
1031     	if (brdnr >= stli_nrbrds)
1032     		return(-ENODEV);
1033     	brdp = stli_brds[brdnr];
1034     	if (brdp == (stlibrd_t *) NULL)
1035     		return(-ENODEV);
1036     	if ((brdp->state & BST_STARTED) == 0)
1037     		return(-ENODEV);
1038     	portnr = MINOR2PORT(minordev);
1039     	if ((portnr < 0) || (portnr > brdp->nrports))
1040     		return(-ENODEV);
1041     
1042     	portp = brdp->ports[portnr];
1043     	if (portp == (stliport_t *) NULL)
1044     		return(-ENODEV);
1045     	if (portp->devnr < 1)
1046     		return(-ENODEV);
1047     
1048     	MOD_INC_USE_COUNT;
1049     
1050     /*
1051      *	Check if this port is in the middle of closing. If so then wait
1052      *	until it is closed then return error status based on flag settings.
1053      *	The sleep here does not need interrupt protection since the wakeup
1054      *	for it is done with the same context.
1055      */
1056     	if (portp->flags & ASYNC_CLOSING) {
1057     		interruptible_sleep_on(&portp->close_wait);
1058     		if (portp->flags & ASYNC_HUP_NOTIFY)
1059     			return(-EAGAIN);
1060     		return(-ERESTARTSYS);
1061     	}
1062     
1063     /*
1064      *	On the first open of the device setup the port hardware, and
1065      *	initialize the per port data structure. Since initializing the port
1066      *	requires several commands to the board we will need to wait for any
1067      *	other open that is already initializing the port.
1068      */
1069     	portp->tty = tty;
1070     	tty->driver_data = portp;
1071     	portp->refcount++;
1072     
1073     	while (test_bit(ST_INITIALIZING, &portp->state)) {
1074     		if (signal_pending(current))
1075     			return(-ERESTARTSYS);
1076     		interruptible_sleep_on(&portp->raw_wait);
1077     	}
1078     
1079     	if ((portp->flags & ASYNC_INITIALIZED) == 0) {
1080     		set_bit(ST_INITIALIZING, &portp->state);
1081     		if ((rc = stli_initopen(brdp, portp)) >= 0) {
1082     			portp->flags |= ASYNC_INITIALIZED;
1083     			clear_bit(TTY_IO_ERROR, &tty->flags);
1084     		}
1085     		clear_bit(ST_INITIALIZING, &portp->state);
1086     		wake_up_interruptible(&portp->raw_wait);
1087     		if (rc < 0)
1088     			return(rc);
1089     	}
1090     
1091     /*
1092      *	Check if this port is in the middle of closing. If so then wait
1093      *	until it is closed then return error status, based on flag settings.
1094      *	The sleep here does not need interrupt protection since the wakeup
1095      *	for it is done with the same context.
1096      */
1097     	if (portp->flags & ASYNC_CLOSING) {
1098     		interruptible_sleep_on(&portp->close_wait);
1099     		if (portp->flags & ASYNC_HUP_NOTIFY)
1100     			return(-EAGAIN);
1101     		return(-ERESTARTSYS);
1102     	}
1103     
1104     /*
1105      *	Based on type of open being done check if it can overlap with any
1106      *	previous opens still in effect. If we are a normal serial device
1107      *	then also we might have to wait for carrier.
1108      */
1109     	if (tty->driver.subtype == STL_DRVTYPCALLOUT) {
1110     		if (portp->flags & ASYNC_NORMAL_ACTIVE)
1111     			return(-EBUSY);
1112     		if (portp->flags & ASYNC_CALLOUT_ACTIVE) {
1113     			if ((portp->flags & ASYNC_SESSION_LOCKOUT) &&
1114     			    (portp->session != current->session))
1115     				return(-EBUSY);
1116     			if ((portp->flags & ASYNC_PGRP_LOCKOUT) &&
1117     			    (portp->pgrp != current->pgrp))
1118     				return(-EBUSY);
1119     		}
1120     		portp->flags |= ASYNC_CALLOUT_ACTIVE;
1121     	} else {
1122     		if (filp->f_flags & O_NONBLOCK) {
1123     			if (portp->flags & ASYNC_CALLOUT_ACTIVE)
1124     				return(-EBUSY);
1125     		} else {
1126     			if ((rc = stli_waitcarrier(brdp, portp, filp)) != 0)
1127     				return(rc);
1128     		}
1129     		portp->flags |= ASYNC_NORMAL_ACTIVE;
1130     	}
1131     
1132     	if ((portp->refcount == 1) && (portp->flags & ASYNC_SPLIT_TERMIOS)) {
1133     		if (tty->driver.subtype == STL_DRVTYPSERIAL)
1134     			*tty->termios = portp->normaltermios;
1135     		else
1136     			*tty->termios = portp->callouttermios;
1137     		stli_setport(portp);
1138     	}
1139     
1140     	portp->session = current->session;
1141     	portp->pgrp = current->pgrp;
1142     	return(0);
1143     }
1144     
1145     /*****************************************************************************/
1146     
1147     static void stli_close(struct tty_struct *tty, struct file *filp)
1148     {
1149     	stlibrd_t	*brdp;
1150     	stliport_t	*portp;
1151     	unsigned long	flags;
1152     
1153     #if DEBUG
1154     	printk("stli_close(tty=%x,filp=%x)\n", (int) tty, (int) filp);
1155     #endif
1156     
1157     	portp = tty->driver_data;
1158     	if (portp == (stliport_t *) NULL)
1159     		return;
1160     
1161     	save_flags(flags);
1162     	cli();
1163     	if (tty_hung_up_p(filp)) {
1164     		MOD_DEC_USE_COUNT;
1165     		restore_flags(flags);
1166     		return;
1167     	}
1168     	if ((tty->count == 1) && (portp->refcount != 1))
1169     		portp->refcount = 1;
1170     	if (portp->refcount-- > 1) {
1171     		MOD_DEC_USE_COUNT;
1172     		restore_flags(flags);
1173     		return;
1174     	}
1175     
1176     	portp->flags |= ASYNC_CLOSING;
1177     
1178     	if (portp->flags & ASYNC_NORMAL_ACTIVE)
1179     		portp->normaltermios = *tty->termios;
1180     	if (portp->flags & ASYNC_CALLOUT_ACTIVE)
1181     		portp->callouttermios = *tty->termios;
1182     
1183     /*
1184      *	May want to wait for data to drain before closing. The BUSY flag
1185      *	keeps track of whether we are still transmitting or not. It is
1186      *	updated by messages from the slave - indicating when all chars
1187      *	really have drained.
1188      */
1189     	if (tty == stli_txcooktty)
1190     		stli_flushchars(tty);
1191     	tty->closing = 1;
1192     	if (portp->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1193     		tty_wait_until_sent(tty, portp->closing_wait);
1194     
1195     	portp->flags &= ~ASYNC_INITIALIZED;
1196     	brdp = stli_brds[portp->brdnr];
1197     	stli_rawclose(brdp, portp, 0, 0);
1198     	if (tty->termios->c_cflag & HUPCL) {
1199     		stli_mkasysigs(&portp->asig, 0, 0);
1200     		if (test_bit(ST_CMDING, &portp->state))
1201     			set_bit(ST_DOSIGS, &portp->state);
1202     		else
1203     			stli_sendcmd(brdp, portp, A_SETSIGNALS, &portp->asig,
1204     				sizeof(asysigs_t), 0);
1205     	}
1206     	clear_bit(ST_TXBUSY, &portp->state);
1207     	clear_bit(ST_RXSTOP, &portp->state);
1208     	set_bit(TTY_IO_ERROR, &tty->flags);
1209     	if (tty->ldisc.flush_buffer)
1210     		(tty->ldisc.flush_buffer)(tty);
1211     	set_bit(ST_DOFLUSHRX, &portp->state);
1212     	stli_flushbuffer(tty);
1213     
1214     	tty->closing = 0;
1215     	portp->tty = (struct tty_struct *) NULL;
1216     
1217     	if (portp->openwaitcnt) {
1218     		if (portp->close_delay)
1219     			stli_delay(portp->close_delay);
1220     		wake_up_interruptible(&portp->open_wait);
1221     	}
1222     
1223     	portp->flags &= ~(ASYNC_CALLOUT_ACTIVE | ASYNC_NORMAL_ACTIVE |
1224     		ASYNC_CLOSING);
1225     	wake_up_interruptible(&portp->close_wait);
1226     	MOD_DEC_USE_COUNT;
1227     	restore_flags(flags);
1228     }
1229     
1230     /*****************************************************************************/
1231     
1232     /*
1233      *	Carry out first open operations on a port. This involves a number of
1234      *	commands to be sent to the slave. We need to open the port, set the
1235      *	notification events, set the initial port settings, get and set the
1236      *	initial signal values. We sleep and wait in between each one. But
1237      *	this still all happens pretty quickly.
1238      */
1239     
1240     static int stli_initopen(stlibrd_t *brdp, stliport_t *portp)
1241     {
1242     	struct tty_struct	*tty;
1243     	asynotify_t		nt;
1244     	asyport_t		aport;
1245     	int			rc;
1246     
1247     #if DEBUG
1248     	printk("stli_initopen(brdp=%x,portp=%x)\n", (int) brdp, (int) portp);
1249     #endif
1250     
1251     	if ((rc = stli_rawopen(brdp, portp, 0, 1)) < 0)
1252     		return(rc);
1253     
1254     	memset(&nt, 0, sizeof(asynotify_t));
1255     	nt.data = (DT_TXLOW | DT_TXEMPTY | DT_RXBUSY | DT_RXBREAK);
1256     	nt.signal = SG_DCD;
1257     	if ((rc = stli_cmdwait(brdp, portp, A_SETNOTIFY, &nt,
1258     	    sizeof(asynotify_t), 0)) < 0)
1259     		return(rc);
1260     
1261     	tty = portp->tty;
1262     	if (tty == (struct tty_struct *) NULL)
1263     		return(-ENODEV);
1264     	stli_mkasyport(portp, &aport, tty->termios);
1265     	if ((rc = stli_cmdwait(brdp, portp, A_SETPORT, &aport,
1266     	    sizeof(asyport_t), 0)) < 0)
1267     		return(rc);
1268     
1269     	set_bit(ST_GETSIGS, &portp->state);
1270     	if ((rc = stli_cmdwait(brdp, portp, A_GETSIGNALS, &portp->asig,
1271     	    sizeof(asysigs_t), 1)) < 0)
1272     		return(rc);
1273     	if (test_and_clear_bit(ST_GETSIGS, &portp->state))
1274     		portp->sigs = stli_mktiocm(portp->asig.sigvalue);
1275     	stli_mkasysigs(&portp->asig, 1, 1);
1276     	if ((rc = stli_cmdwait(brdp, portp, A_SETSIGNALS, &portp->asig,
1277     	    sizeof(asysigs_t), 0)) < 0)
1278     		return(rc);
1279     
1280     	return(0);
1281     }
1282     
1283     /*****************************************************************************/
1284     
1285     /*
1286      *	Send an open message to the slave. This will sleep waiting for the
1287      *	acknowledgement, so must have user context. We need to co-ordinate
1288      *	with close events here, since we don't want open and close events
1289      *	to overlap.
1290      */
1291     
1292     static int stli_rawopen(stlibrd_t *brdp, stliport_t *portp, unsigned long arg, int wait)
1293     {
1294     	volatile cdkhdr_t	*hdrp;
1295     	volatile cdkctrl_t	*cp;
1296     	volatile unsigned char	*bits;
1297     	unsigned long		flags;
1298     	int			rc;
1299     
1300     #if DEBUG
1301     	printk("stli_rawopen(brdp=%x,portp=%x,arg=%x,wait=%d)\n",
1302     		(int) brdp, (int) portp, (int) arg, wait);
1303     #endif
1304     
1305     /*
1306      *	Send a message to the slave to open this port.
1307      */
1308     	save_flags(flags);
1309     	cli();
1310     
1311     /*
1312      *	Slave is already closing this port. This can happen if a hangup
1313      *	occurs on this port. So we must wait until it is complete. The
1314      *	order of opens and closes may not be preserved across shared
1315      *	memory, so we must wait until it is complete.
1316      */
1317     	while (test_bit(ST_CLOSING, &portp->state)) {
1318     		if (signal_pending(current)) {
1319     			restore_flags(flags);
1320     			return(-ERESTARTSYS);
1321     		}
1322     		interruptible_sleep_on(&portp->raw_wait);
1323     	}
1324     
1325     /*
1326      *	Everything is ready now, so write the open message into shared
1327      *	memory. Once the message is in set the service bits to say that
1328      *	this port wants service.
1329      */
1330     	EBRDENABLE(brdp);
1331     	cp = &((volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr))->ctrl;
1332     	cp->openarg = arg;
1333     	cp->open = 1;
1334     	hdrp = (volatile cdkhdr_t *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
1335     	bits = ((volatile unsigned char *) hdrp) + brdp->slaveoffset +
1336     		portp->portidx;
1337     	*bits |= portp->portbit;
1338     	EBRDDISABLE(brdp);
1339     
1340     	if (wait == 0) {
1341     		restore_flags(flags);
1342     		return(0);
1343     	}
1344     
1345     /*
1346      *	Slave is in action, so now we must wait for the open acknowledgment
1347      *	to come back.
1348      */
1349     	rc = 0;
1350     	set_bit(ST_OPENING, &portp->state);
1351     	while (test_bit(ST_OPENING, &portp->state)) {
1352     		if (signal_pending(current)) {
1353     			rc = -ERESTARTSYS;
1354     			break;
1355     		}
1356     		interruptible_sleep_on(&portp->raw_wait);
1357     	}
1358     	restore_flags(flags);
1359     
1360     	if ((rc == 0) && (portp->rc != 0))
1361     		rc = -EIO;
1362     	return(rc);
1363     }
1364     
1365     /*****************************************************************************/
1366     
1367     /*
1368      *	Send a close message to the slave. Normally this will sleep waiting
1369      *	for the acknowledgement, but if wait parameter is 0 it will not. If
1370      *	wait is true then must have user context (to sleep).
1371      */
1372     
1373     static int stli_rawclose(stlibrd_t *brdp, stliport_t *portp, unsigned long arg, int wait)
1374     {
1375     	volatile cdkhdr_t	*hdrp;
1376     	volatile cdkctrl_t	*cp;
1377     	volatile unsigned char	*bits;
1378     	unsigned long		flags;
1379     	int			rc;
1380     
1381     #if DEBUG
1382     	printk("stli_rawclose(brdp=%x,portp=%x,arg=%x,wait=%d)\n",
1383     		(int) brdp, (int) portp, (int) arg, wait);
1384     #endif
1385     
1386     	save_flags(flags);
1387     	cli();
1388     
1389     /*
1390      *	Slave is already closing this port. This can happen if a hangup
1391      *	occurs on this port.
1392      */
1393     	if (wait) {
1394     		while (test_bit(ST_CLOSING, &portp->state)) {
1395     			if (signal_pending(current)) {
1396     				restore_flags(flags);
1397     				return(-ERESTARTSYS);
1398     			}
1399     			interruptible_sleep_on(&portp->raw_wait);
1400     		}
1401     	}
1402     
1403     /*
1404      *	Write the close command into shared memory.
1405      */
1406     	EBRDENABLE(brdp);
1407     	cp = &((volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr))->ctrl;
1408     	cp->closearg = arg;
1409     	cp->close = 1;
1410     	hdrp = (volatile cdkhdr_t *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
1411     	bits = ((volatile unsigned char *) hdrp) + brdp->slaveoffset +
1412     		portp->portidx;
1413     	*bits |= portp->portbit;
1414     	EBRDDISABLE(brdp);
1415     
1416     	set_bit(ST_CLOSING, &portp->state);
1417     	if (wait == 0) {
1418     		restore_flags(flags);
1419     		return(0);
1420     	}
1421     
1422     /*
1423      *	Slave is in action, so now we must wait for the open acknowledgment
1424      *	to come back.
1425      */
1426     	rc = 0;
1427     	while (test_bit(ST_CLOSING, &portp->state)) {
1428     		if (signal_pending(current)) {
1429     			rc = -ERESTARTSYS;
1430     			break;
1431     		}
1432     		interruptible_sleep_on(&portp->raw_wait);
1433     	}
1434     	restore_flags(flags);
1435     
1436     	if ((rc == 0) && (portp->rc != 0))
1437     		rc = -EIO;
1438     	return(rc);
1439     }
1440     
1441     /*****************************************************************************/
1442     
1443     /*
1444      *	Send a command to the slave and wait for the response. This must
1445      *	have user context (it sleeps). This routine is generic in that it
1446      *	can send any type of command. Its purpose is to wait for that command
1447      *	to complete (as opposed to initiating the command then returning).
1448      */
1449     
1450     static int stli_cmdwait(stlibrd_t *brdp, stliport_t *portp, unsigned long cmd, void *arg, int size, int copyback)
1451     {
1452     	unsigned long	flags;
1453     
1454     #if DEBUG
1455     	printk("stli_cmdwait(brdp=%x,portp=%x,cmd=%x,arg=%x,size=%d,"
1456     		"copyback=%d)\n", (int) brdp, (int) portp, (int) cmd,
1457     		(int) arg, size, copyback);
1458     #endif
1459     
1460     	save_flags(flags);
1461     	cli();
1462     	while (test_bit(ST_CMDING, &portp->state)) {
1463     		if (signal_pending(current)) {
1464     			restore_flags(flags);
1465     			return(-ERESTARTSYS);
1466     		}
1467     		interruptible_sleep_on(&portp->raw_wait);
1468     	}
1469     
1470     	stli_sendcmd(brdp, portp, cmd, arg, size, copyback);
1471     
1472     	while (test_bit(ST_CMDING, &portp->state)) {
1473     		if (signal_pending(current)) {
1474     			restore_flags(flags);
1475     			return(-ERESTARTSYS);
1476     		}
1477     		interruptible_sleep_on(&portp->raw_wait);
1478     	}
1479     	restore_flags(flags);
1480     
1481     	if (portp->rc != 0)
1482     		return(-EIO);
1483     	return(0);
1484     }
1485     
1486     /*****************************************************************************/
1487     
1488     /*
1489      *	Send the termios settings for this port to the slave. This sleeps
1490      *	waiting for the command to complete - so must have user context.
1491      */
1492     
1493     static int stli_setport(stliport_t *portp)
1494     {
1495     	stlibrd_t	*brdp;
1496     	asyport_t	aport;
1497     
1498     #if DEBUG
1499     	printk("stli_setport(portp=%x)\n", (int) portp);
1500     #endif
1501     
1502     	if (portp == (stliport_t *) NULL)
1503     		return(-ENODEV);
1504     	if (portp->tty == (struct tty_struct *) NULL)
1505     		return(-ENODEV);
1506     	if ((portp->brdnr < 0) && (portp->brdnr >= stli_nrbrds))
1507     		return(-ENODEV);
1508     	brdp = stli_brds[portp->brdnr];
1509     	if (brdp == (stlibrd_t *) NULL)
1510     		return(-ENODEV);
1511     
1512     	stli_mkasyport(portp, &aport, portp->tty->termios);
1513     	return(stli_cmdwait(brdp, portp, A_SETPORT, &aport, sizeof(asyport_t), 0));
1514     }
1515     
1516     /*****************************************************************************/
1517     
1518     /*
1519      *	Wait for a specified delay period, this is not a busy-loop. It will
1520      *	give up the processor while waiting. Unfortunately this has some
1521      *	rather intimate knowledge of the process management stuff.
1522      */
1523     
1524     static void stli_delay(int len)
1525     {
1526     #if DEBUG
1527     	printk("stli_delay(len=%d)\n", len);
1528     #endif
1529     	if (len > 0) {
1530     		current->state = TASK_INTERRUPTIBLE;
1531     		schedule_timeout(len);
1532     		current->state = TASK_RUNNING;
1533     	}
1534     }
1535     
1536     /*****************************************************************************/
1537     
1538     /*
1539      *	Possibly need to wait for carrier (DCD signal) to come high. Say
1540      *	maybe because if we are clocal then we don't need to wait...
1541      */
1542     
1543     static int stli_waitcarrier(stlibrd_t *brdp, stliport_t *portp, struct file *filp)
1544     {
1545     	unsigned long	flags;
1546     	int		rc, doclocal;
1547     
1548     #if DEBUG
1549     	printk("stli_waitcarrier(brdp=%x,portp=%x,filp=%x)\n",
1550     		(int) brdp, (int) portp, (int) filp);
1551     #endif
1552     
1553     	rc = 0;
1554     	doclocal = 0;
1555     
1556     	if (portp->flags & ASYNC_CALLOUT_ACTIVE) {
1557     		if (portp->normaltermios.c_cflag & CLOCAL)
1558     			doclocal++;
1559     	} else {
1560     		if (portp->tty->termios->c_cflag & CLOCAL)
1561     			doclocal++;
1562     	}
1563     
1564     	save_flags(flags);
1565     	cli();
1566     	portp->openwaitcnt++;
1567     	if (! tty_hung_up_p(filp))
1568     		portp->refcount--;
1569     
1570     	for (;;) {
1571     		if ((portp->flags & ASYNC_CALLOUT_ACTIVE) == 0) {
1572     			stli_mkasysigs(&portp->asig, 1, 1);
1573     			if ((rc = stli_cmdwait(brdp, portp, A_SETSIGNALS,
1574     			    &portp->asig, sizeof(asysigs_t), 0)) < 0)
1575     				break;
1576     		}
1577     		if (tty_hung_up_p(filp) ||
1578     		    ((portp->flags & ASYNC_INITIALIZED) == 0)) {
1579     			if (portp->flags & ASYNC_HUP_NOTIFY)
1580     				rc = -EBUSY;
1581     			else
1582     				rc = -ERESTARTSYS;
1583     			break;
1584     		}
1585     		if (((portp->flags & ASYNC_CALLOUT_ACTIVE) == 0) &&
1586     		    ((portp->flags & ASYNC_CLOSING) == 0) &&
1587     		    (doclocal || (portp->sigs & TIOCM_CD))) {
1588     			break;
1589     		}
1590     		if (signal_pending(current)) {
1591     			rc = -ERESTARTSYS;
1592     			break;
1593     		}
1594     		interruptible_sleep_on(&portp->open_wait);
1595     	}
1596     
1597     	if (! tty_hung_up_p(filp))
1598     		portp->refcount++;
1599     	portp->openwaitcnt--;
1600     	restore_flags(flags);
1601     
1602     	return(rc);
1603     }
1604     
1605     /*****************************************************************************/
1606     
1607     /*
1608      *	Write routine. Take the data and put it in the shared memory ring
1609      *	queue. If port is not already sending chars then need to mark the
1610      *	service bits for this port.
1611      */
1612     
1613     static int stli_write(struct tty_struct *tty, int from_user, const unsigned char *buf, int count)
1614     {
1615     	volatile cdkasy_t	*ap;
1616     	volatile cdkhdr_t	*hdrp;
1617     	volatile unsigned char	*bits;
1618     	unsigned char		*shbuf, *chbuf;
1619     	stliport_t		*portp;
1620     	stlibrd_t		*brdp;
1621     	unsigned int		len, stlen, head, tail, size;
1622     	unsigned long		flags;
1623     
1624     #if DEBUG
1625     	printk("stli_write(tty=%x,from_user=%d,buf=%x,count=%d)\n",
1626     		(int) tty, from_user, (int) buf, count);
1627     #endif
1628     
1629     	if ((tty == (struct tty_struct *) NULL) ||
1630     	    (stli_tmpwritebuf == (char *) NULL))
1631     		return(0);
1632     	if (tty == stli_txcooktty)
1633     		stli_flushchars(tty);
1634     	portp = tty->driver_data;
1635     	if (portp == (stliport_t *) NULL)
1636     		return(0);
1637     	if ((portp->brdnr < 0) || (portp->brdnr >= stli_nrbrds))
1638     		return(0);
1639     	brdp = stli_brds[portp->brdnr];
1640     	if (brdp == (stlibrd_t *) NULL)
1641     		return(0);
1642     	chbuf = (unsigned char *) buf;
1643     
1644     /*
1645      *	If copying direct from user space we need to be able to handle page
1646      *	faults while we are copying. To do this copy as much as we can now
1647      *	into a kernel buffer. From there we copy it into shared memory. The
1648      *	big problem is that we do not want shared memory enabled when we are
1649      *	sleeping (other boards may be serviced while asleep). Something else
1650      *	to note here is the reading of the tail twice. Since the boards
1651      *	shared memory can be on an 8-bit bus then we need to be very careful
1652      *	reading 16 bit quantities - since both the board (slave) and host
1653      *	could be writing and reading at the same time.
1654      */
1655     	if (from_user) {
1656     		save_flags(flags);
1657     		cli();
1658     		EBRDENABLE(brdp);
1659     		ap = (volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr);
1660     		head = (unsigned int) ap->txq.head;
1661     		tail = (unsigned int) ap->txq.tail;
1662     		if (tail != ((unsigned int) ap->txq.tail))
1663     			tail = (unsigned int) ap->txq.tail;
1664     		len = (head >= tail) ? (portp->txsize - (head - tail) - 1) :
1665     			(tail - head - 1);
1666     		count = MIN(len, count);
1667     		EBRDDISABLE(brdp);
1668     		restore_flags(flags);
1669     
1670     		down(&stli_tmpwritesem);
1671     		copy_from_user(stli_tmpwritebuf, chbuf, count);
1672     		chbuf = &stli_tmpwritebuf[0];
1673     	}
1674     
1675     /*
1676      *	All data is now local, shove as much as possible into shared memory.
1677      */
1678     	save_flags(flags);
1679     	cli();
1680     	EBRDENABLE(brdp);
1681     	ap = (volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr);
1682     	head = (unsigned int) ap->txq.head;
1683     	tail = (unsigned int) ap->txq.tail;
1684     	if (tail != ((unsigned int) ap->txq.tail))
1685     		tail = (unsigned int) ap->txq.tail;
1686     	size = portp->txsize;
1687     	if (head >= tail) {
1688     		len = size - (head - tail) - 1;
1689     		stlen = size - head;
1690     	} else {
1691     		len = tail - head - 1;
1692     		stlen = len;
1693     	}
1694     
1695     	len = MIN(len, count);
1696     	count = 0;
1697     	shbuf = (char *) EBRDGETMEMPTR(brdp, portp->txoffset);
1698     
1699     	while (len > 0) {
1700     		stlen = MIN(len, stlen);
1701     		memcpy((shbuf + head), chbuf, stlen);
1702     		chbuf += stlen;
1703     		len -= stlen;
1704     		count += stlen;
1705     		head += stlen;
1706     		if (head >= size) {
1707     			head = 0;
1708     			stlen = tail;
1709     		}
1710     	}
1711     
1712     	ap = (volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr);
1713     	ap->txq.head = head;
1714     	if (test_bit(ST_TXBUSY, &portp->state)) {
1715     		if (ap->changed.data & DT_TXEMPTY)
1716     			ap->changed.data &= ~DT_TXEMPTY;
1717     	}
1718     	hdrp = (volatile cdkhdr_t *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
1719     	bits = ((volatile unsigned char *) hdrp) + brdp->slaveoffset +
1720     		portp->portidx;
1721     	*bits |= portp->portbit;
1722     	set_bit(ST_TXBUSY, &portp->state);
1723     	EBRDDISABLE(brdp);
1724     
1725     	if (from_user)
1726     		up(&stli_tmpwritesem);
1727     	restore_flags(flags);
1728     
1729     	return(count);
1730     }
1731     
1732     /*****************************************************************************/
1733     
1734     /*
1735      *	Output a single character. We put it into a temporary local buffer
1736      *	(for speed) then write out that buffer when the flushchars routine
1737      *	is called. There is a safety catch here so that if some other port
1738      *	writes chars before the current buffer has been, then we write them
1739      *	first them do the new ports.
1740      */
1741     
1742     static void stli_putchar(struct tty_struct *tty, unsigned char ch)
1743     {
1744     #if DEBUG
1745     	printk("stli_putchar(tty=%x,ch=%x)\n", (int) tty, (int) ch);
1746     #endif
1747     
1748     	if (tty == (struct tty_struct *) NULL)
1749     		return;
1750     	if (tty != stli_txcooktty) {
1751     		if (stli_txcooktty != (struct tty_struct *) NULL)
1752     			stli_flushchars(stli_txcooktty);
1753     		stli_txcooktty = tty;
1754     	}
1755     
1756     	stli_txcookbuf[stli_txcooksize++] = ch;
1757     }
1758     
1759     /*****************************************************************************/
1760     
1761     /*
1762      *	Transfer characters from the local TX cooking buffer to the board.
1763      *	We sort of ignore the tty that gets passed in here. We rely on the
1764      *	info stored with the TX cook buffer to tell us which port to flush
1765      *	the data on. In any case we clean out the TX cook buffer, for re-use
1766      *	by someone else.
1767      */
1768     
1769     static void stli_flushchars(struct tty_struct *tty)
1770     {
1771     	volatile cdkhdr_t	*hdrp;
1772     	volatile unsigned char	*bits;
1773     	volatile cdkasy_t	*ap;
1774     	struct tty_struct	*cooktty;
1775     	stliport_t		*portp;
1776     	stlibrd_t		*brdp;
1777     	unsigned int		len, stlen, head, tail, size, count, cooksize;
1778     	unsigned char		*buf, *shbuf;
1779     	unsigned long		flags;
1780     
1781     #if DEBUG
1782     	printk("stli_flushchars(tty=%x)\n", (int) tty);
1783     #endif
1784     
1785     	cooksize = stli_txcooksize;
1786     	cooktty = stli_txcooktty;
1787     	stli_txcooksize = 0;
1788     	stli_txcookrealsize = 0;
1789     	stli_txcooktty = (struct tty_struct *) NULL;
1790     
1791     	if (tty == (struct tty_struct *) NULL)
1792     		return;
1793     	if (cooktty == (struct tty_struct *) NULL)
1794     		return;
1795     	if (tty != cooktty)
1796     		tty = cooktty;
1797     	if (cooksize == 0)
1798     		return;
1799     
1800     	portp = tty->driver_data;
1801     	if (portp == (stliport_t *) NULL)
1802     		return;
1803     	if ((portp->brdnr < 0) || (portp->brdnr >= stli_nrbrds))
1804     		return;
1805     	brdp = stli_brds[portp->brdnr];
1806     	if (brdp == (stlibrd_t *) NULL)
1807     		return;
1808     
1809     	save_flags(flags);
1810     	cli();
1811     	EBRDENABLE(brdp);
1812     
1813     	ap = (volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr);
1814     	head = (unsigned int) ap->txq.head;
1815     	tail = (unsigned int) ap->txq.tail;
1816     	if (tail != ((unsigned int) ap->txq.tail))
1817     		tail = (unsigned int) ap->txq.tail;
1818     	size = portp->txsize;
1819     	if (head >= tail) {
1820     		len = size - (head - tail) - 1;
1821     		stlen = size - head;
1822     	} else {
1823     		len = tail - head - 1;
1824     		stlen = len;
1825     	}
1826     
1827     	len = MIN(len, cooksize);
1828     	count = 0;
1829     	shbuf = (char *) EBRDGETMEMPTR(brdp, portp->txoffset);
1830     	buf = stli_txcookbuf;
1831     
1832     	while (len > 0) {
1833     		stlen = MIN(len, stlen);
1834     		memcpy((shbuf + head), buf, stlen);
1835     		buf += stlen;
1836     		len -= stlen;
1837     		count += stlen;
1838     		head += stlen;
1839     		if (head >= size) {
1840     			head = 0;
1841     			stlen = tail;
1842     		}
1843     	}
1844     
1845     	ap = (volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr);
1846     	ap->txq.head = head;
1847     
1848     	if (test_bit(ST_TXBUSY, &portp->state)) {
1849     		if (ap->changed.data & DT_TXEMPTY)
1850     			ap->changed.data &= ~DT_TXEMPTY;
1851     	}
1852     	hdrp = (volatile cdkhdr_t *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
1853     	bits = ((volatile unsigned char *) hdrp) + brdp->slaveoffset +
1854     		portp->portidx;
1855     	*bits |= portp->portbit;
1856     	set_bit(ST_TXBUSY, &portp->state);
1857     
1858     	EBRDDISABLE(brdp);
1859     	restore_flags(flags);
1860     }
1861     
1862     /*****************************************************************************/
1863     
1864     static int stli_writeroom(struct tty_struct *tty)
1865     {
1866     	volatile cdkasyrq_t	*rp;
1867     	stliport_t		*portp;
1868     	stlibrd_t		*brdp;
1869     	unsigned int		head, tail, len;
1870     	unsigned long		flags;
1871     
1872     #if DEBUG
1873     	printk("stli_writeroom(tty=%x)\n", (int) tty);
1874     #endif
1875     
1876     	if (tty == (struct tty_struct *) NULL)
1877     		return(0);
1878     	if (tty == stli_txcooktty) {
1879     		if (stli_txcookrealsize != 0) {
1880     			len = stli_txcookrealsize - stli_txcooksize;
1881     			return(len);
1882     		}
1883     	}
1884     
1885     	portp = tty->driver_data;
1886     	if (portp == (stliport_t *) NULL)
1887     		return(0);
1888     	if ((portp->brdnr < 0) || (portp->brdnr >= stli_nrbrds))
1889     		return(0);
1890     	brdp = stli_brds[portp->brdnr];
1891     	if (brdp == (stlibrd_t *) NULL)
1892     		return(0);
1893     
1894     	save_flags(flags);
1895     	cli();
1896     	EBRDENABLE(brdp);
1897     	rp = &((volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr))->txq;
1898     	head = (unsigned int) rp->head;
1899     	tail = (unsigned int) rp->tail;
1900     	if (tail != ((unsigned int) rp->tail))
1901     		tail = (unsigned int) rp->tail;
1902     	len = (head >= tail) ? (portp->txsize - (head - tail)) : (tail - head);
1903     	len--;
1904     	EBRDDISABLE(brdp);
1905     	restore_flags(flags);
1906     
1907     	if (tty == stli_txcooktty) {
1908     		stli_txcookrealsize = len;
1909     		len -= stli_txcooksize;
1910     	}
1911     	return(len);
1912     }
1913     
1914     /*****************************************************************************/
1915     
1916     /*
1917      *	Return the number of characters in the transmit buffer. Normally we
1918      *	will return the number of chars in the shared memory ring queue.
1919      *	We need to kludge around the case where the shared memory buffer is
1920      *	empty but not all characters have drained yet, for this case just
1921      *	return that there is 1 character in the buffer!
1922      */
1923     
1924     static int stli_charsinbuffer(struct tty_struct *tty)
1925     {
1926     	volatile cdkasyrq_t	*rp;
1927     	stliport_t		*portp;
1928     	stlibrd_t		*brdp;
1929     	unsigned int		head, tail, len;
1930     	unsigned long		flags;
1931     
1932     #if DEBUG
1933     	printk("stli_charsinbuffer(tty=%x)\n", (int) tty);
1934     #endif
1935     
1936     	if (tty == (struct tty_struct *) NULL)
1937     		return(0);
1938     	if (tty == stli_txcooktty)
1939     		stli_flushchars(tty);
1940     	portp = tty->driver_data;
1941     	if (portp == (stliport_t *) NULL)
1942     		return(0);
1943     	if ((portp->brdnr < 0) || (portp->brdnr >= stli_nrbrds))
1944     		return(0);
1945     	brdp = stli_brds[portp->brdnr];
1946     	if (brdp == (stlibrd_t *) NULL)
1947     		return(0);
1948     
1949     	save_flags(flags);
1950     	cli();
1951     	EBRDENABLE(brdp);
1952     	rp = &((volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr))->txq;
1953     	head = (unsigned int) rp->head;
1954     	tail = (unsigned int) rp->tail;
1955     	if (tail != ((unsigned int) rp->tail))
1956     		tail = (unsigned int) rp->tail;
1957     	len = (head >= tail) ? (head - tail) : (portp->txsize - (tail - head));
1958     	if ((len == 0) && test_bit(ST_TXBUSY, &portp->state))
1959     		len = 1;
1960     	EBRDDISABLE(brdp);
1961     	restore_flags(flags);
1962     
1963     	return(len);
1964     }
1965     
1966     /*****************************************************************************/
1967     
1968     /*
1969      *	Generate the serial struct info.
1970      */
1971     
1972     static void stli_getserial(stliport_t *portp, struct serial_struct *sp)
1973     {
1974     	struct serial_struct	sio;
1975     	stlibrd_t		*brdp;
1976     
1977     #if DEBUG
1978     	printk("stli_getserial(portp=%x,sp=%x)\n", (int) portp, (int) sp);
1979     #endif
1980     
1981     	memset(&sio, 0, sizeof(struct serial_struct));
1982     	sio.type = PORT_UNKNOWN;
1983     	sio.line = portp->portnr;
1984     	sio.irq = 0;
1985     	sio.flags = portp->flags;
1986     	sio.baud_base = portp->baud_base;
1987     	sio.close_delay = portp->close_delay;
1988     	sio.closing_wait = portp->closing_wait;
1989     	sio.custom_divisor = portp->custom_divisor;
1990     	sio.xmit_fifo_size = 0;
1991     	sio.hub6 = 0;
1992     
1993     	brdp = stli_brds[portp->brdnr];
1994     	if (brdp != (stlibrd_t *) NULL)
1995     		sio.port = brdp->iobase;
1996     		
1997     	copy_to_user(sp, &sio, sizeof(struct serial_struct));
1998     }
1999     
2000     /*****************************************************************************/
2001     
2002     /*
2003      *	Set port according to the serial struct info.
2004      *	At this point we do not do any auto-configure stuff, so we will
2005      *	just quietly ignore any requests to change irq, etc.
2006      */
2007     
2008     static int stli_setserial(stliport_t *portp, struct serial_struct *sp)
2009     {
2010     	struct serial_struct	sio;
2011     	int			rc;
2012     
2013     #if DEBUG
2014     	printk("stli_setserial(portp=%x,sp=%x)\n", (int) portp, (int) sp);
2015     #endif
2016     
2017     	copy_from_user(&sio, sp, sizeof(struct serial_struct));
2018     	if (!capable(CAP_SYS_ADMIN)) {
2019     		if ((sio.baud_base != portp->baud_base) ||
2020     		    (sio.close_delay != portp->close_delay) ||
2021     		    ((sio.flags & ~ASYNC_USR_MASK) !=
2022     		    (portp->flags & ~ASYNC_USR_MASK)))
2023     			return(-EPERM);
2024     	} 
2025     
2026     	portp->flags = (portp->flags & ~ASYNC_USR_MASK) |
2027     		(sio.flags & ASYNC_USR_MASK);
2028     	portp->baud_base = sio.baud_base;
2029     	portp->close_delay = sio.close_delay;
2030     	portp->closing_wait = sio.closing_wait;
2031     	portp->custom_divisor = sio.custom_divisor;
2032     
2033     	if ((rc = stli_setport(portp)) < 0)
2034     		return(rc);
2035     	return(0);
2036     }
2037     
2038     /*****************************************************************************/
2039     
2040     static int stli_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
2041     {
2042     	stliport_t	*portp;
2043     	stlibrd_t	*brdp;
2044     	unsigned long	lval;
2045     	unsigned int	ival;
2046     	int		rc;
2047     
2048     #if DEBUG
2049     	printk("stli_ioctl(tty=%x,file=%x,cmd=%x,arg=%x)\n",
2050     		(int) tty, (int) file, cmd, (int) arg);
2051     #endif
2052     
2053     	if (tty == (struct tty_struct *) NULL)
2054     		return(-ENODEV);
2055     	portp = tty->driver_data;
2056     	if (portp == (stliport_t *) NULL)
2057     		return(-ENODEV);
2058     	if ((portp->brdnr < 0) || (portp->brdnr >= stli_nrbrds))
2059     		return(0);
2060     	brdp = stli_brds[portp->brdnr];
2061     	if (brdp == (stlibrd_t *) NULL)
2062     		return(0);
2063     
2064     	if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
2065      	    (cmd != COM_GETPORTSTATS) && (cmd != COM_CLRPORTSTATS)) {
2066     		if (tty->flags & (1 << TTY_IO_ERROR))
2067     			return(-EIO);
2068     	}
2069     
2070     	rc = 0;
2071     
2072     	switch (cmd) {
2073     	case TIOCGSOFTCAR:
2074     		rc = put_user(((tty->termios->c_cflag & CLOCAL) ? 1 : 0),
2075     			(unsigned int *) arg);
2076     		break;
2077     	case TIOCSSOFTCAR:
2078     		if ((rc = get_user(ival, (unsigned int *) arg)) == 0)
2079     			tty->termios->c_cflag =
2080     				(tty->termios->c_cflag & ~CLOCAL) |
2081     				(ival ? CLOCAL : 0);
2082     		break;
2083     	case TIOCMGET:
2084     		if ((rc = verify_area(VERIFY_WRITE, (void *) arg,
2085     		    sizeof(unsigned int))) == 0) {
2086     			if ((rc = stli_cmdwait(brdp, portp, A_GETSIGNALS,
2087     			    &portp->asig, sizeof(asysigs_t), 1)) < 0)
2088     				return(rc);
2089     			lval = stli_mktiocm(portp->asig.sigvalue);
2090     			put_user(lval, (unsigned int *) arg);
2091     		}
2092     		break;
2093     	case TIOCMBIS:
2094     		if ((rc = get_user(ival, (unsigned int *) arg)) == 0) {
2095     			stli_mkasysigs(&portp->asig,
2096     				((ival & TIOCM_DTR) ? 1 : -1),
2097     				((ival & TIOCM_RTS) ? 1 : -1));
2098     			rc = stli_cmdwait(brdp, portp, A_SETSIGNALS,
2099     				&portp->asig, sizeof(asysigs_t), 0);
2100     		}
2101     		break;
2102     	case TIOCMBIC:
2103     		if ((rc = get_user(ival, (unsigned int *) arg)) == 0) {
2104     			stli_mkasysigs(&portp->asig,
2105     				((ival & TIOCM_DTR) ? 0 : -1),
2106     				((ival & TIOCM_RTS) ? 0 : -1));
2107     			rc = stli_cmdwait(brdp, portp, A_SETSIGNALS,
2108     				&portp->asig, sizeof(asysigs_t), 0);
2109     		}
2110     		break;
2111     	case TIOCMSET:
2112     		if ((rc = get_user(ival, (unsigned int *) arg)) == 0) {
2113     			stli_mkasysigs(&portp->asig,
2114     				((ival & TIOCM_DTR) ? 1 : 0),
2115     				((ival & TIOCM_RTS) ? 1 : 0));
2116     			rc = stli_cmdwait(brdp, portp, A_SETSIGNALS,
2117     				&portp->asig, sizeof(asysigs_t), 0);
2118     		}
2119     		break;
2120     	case TIOCGSERIAL:
2121     		if ((rc = verify_area(VERIFY_WRITE, (void *) arg,
2122     		    sizeof(struct serial_struct))) == 0)
2123     			stli_getserial(portp, (struct serial_struct *) arg);
2124     		break;
2125     	case TIOCSSERIAL:
2126     		if ((rc = verify_area(VERIFY_READ, (void *) arg,
2127     		    sizeof(struct serial_struct))) == 0)
2128     			rc = stli_setserial(portp, (struct serial_struct *)arg);
2129     		break;
2130     	case STL_GETPFLAG:
2131     		rc = put_user(portp->pflag, (unsigned int *) arg);
2132     		break;
2133     	case STL_SETPFLAG:
2134     		if ((rc = get_user(portp->pflag, (unsigned int *) arg)) == 0)
2135     			stli_setport(portp);
2136     		break;
2137     	case COM_GETPORTSTATS:
2138     		if ((rc = verify_area(VERIFY_WRITE, (void *) arg,
2139     		    sizeof(comstats_t))) == 0)
2140     			rc = stli_getportstats(portp, (comstats_t *) arg);
2141     		break;
2142     	case COM_CLRPORTSTATS:
2143     		if ((rc = verify_area(VERIFY_WRITE, (void *) arg,
2144     		    sizeof(comstats_t))) == 0)
2145     			rc = stli_clrportstats(portp, (comstats_t *) arg);
2146     		break;
2147     	case TIOCSERCONFIG:
2148     	case TIOCSERGWILD:
2149     	case TIOCSERSWILD:
2150     	case TIOCSERGETLSR:
2151     	case TIOCSERGSTRUCT:
2152     	case TIOCSERGETMULTI:
2153     	case TIOCSERSETMULTI:
2154     	default:
2155     		rc = -ENOIOCTLCMD;
2156     		break;
2157     	}
2158     
2159     	return(rc);
2160     }
2161     
2162     /*****************************************************************************/
2163     
2164     /*
2165      *	This routine assumes that we have user context and can sleep.
2166      *	Looks like it is true for the current ttys implementation..!!
2167      */
2168     
2169     static void stli_settermios(struct tty_struct *tty, struct termios *old)
2170     {
2171     	stliport_t	*portp;
2172     	stlibrd_t	*brdp;
2173     	struct termios	*tiosp;
2174     	asyport_t	aport;
2175     
2176     #if DEBUG
2177     	printk("stli_settermios(tty=%x,old=%x)\n", (int) tty, (int) old);
2178     #endif
2179     
2180     	if (tty == (struct tty_struct *) NULL)
2181     		return;
2182     	portp = tty->driver_data;
2183     	if (portp == (stliport_t *) NULL)
2184     		return;
2185     	if ((portp->brdnr < 0) || (portp->brdnr >= stli_nrbrds))
2186     		return;
2187     	brdp = stli_brds[portp->brdnr];
2188     	if (brdp == (stlibrd_t *) NULL)
2189     		return;
2190     
2191     	tiosp = tty->termios;
2192     	if ((tiosp->c_cflag == old->c_cflag) &&
2193     	    (tiosp->c_iflag == old->c_iflag))
2194     		return;
2195     
2196     	stli_mkasyport(portp, &aport, tiosp);
2197     	stli_cmdwait(brdp, portp, A_SETPORT, &aport, sizeof(asyport_t), 0);
2198     	stli_mkasysigs(&portp->asig, ((tiosp->c_cflag & CBAUD) ? 1 : 0), -1);
2199     	stli_cmdwait(brdp, portp, A_SETSIGNALS, &portp->asig,
2200     		sizeof(asysigs_t), 0);
2201     	if ((old->c_cflag & CRTSCTS) && ((tiosp->c_cflag & CRTSCTS) == 0))
2202     		tty->hw_stopped = 0;
2203     	if (((old->c_cflag & CLOCAL) == 0) && (tiosp->c_cflag & CLOCAL))
2204     		wake_up_interruptible(&portp->open_wait);
2205     }
2206     
2207     /*****************************************************************************/
2208     
2209     /*
2210      *	Attempt to flow control who ever is sending us data. We won't really
2211      *	do any flow control action here. We can't directly, and even if we
2212      *	wanted to we would have to send a command to the slave. The slave
2213      *	knows how to flow control, and will do so when its buffers reach its
2214      *	internal high water marks. So what we will do is set a local state
2215      *	bit that will stop us sending any RX data up from the poll routine
2216      *	(which is the place where RX data from the slave is handled).
2217      */
2218     
2219     static void stli_throttle(struct tty_struct *tty)
2220     {
2221     	stliport_t	*portp;
2222     
2223     #if DEBUG
2224     	printk("stli_throttle(tty=%x)\n", (int) tty);
2225     #endif
2226     
2227     	if (tty == (struct tty_struct *) NULL)
2228     		return;
2229     	portp = tty->driver_data;
2230     	if (portp == (stliport_t *) NULL)
2231     		return;
2232     
2233     	set_bit(ST_RXSTOP, &portp->state);
2234     }
2235     
2236     /*****************************************************************************/
2237     
2238     /*
2239      *	Unflow control the device sending us data... That means that all
2240      *	we have to do is clear the RXSTOP state bit. The next poll call
2241      *	will then be able to pass the RX data back up.
2242      */
2243     
2244     static void stli_unthrottle(struct tty_struct *tty)
2245     {
2246     	stliport_t	*portp;
2247     
2248     #if DEBUG
2249     	printk("stli_unthrottle(tty=%x)\n", (int) tty);
2250     #endif
2251     
2252     	if (tty == (struct tty_struct *) NULL)
2253     		return;
2254     	portp = tty->driver_data;
2255     	if (portp == (stliport_t *) NULL)
2256     		return;
2257     
2258     	clear_bit(ST_RXSTOP, &portp->state);
2259     }
2260     
2261     /*****************************************************************************/
2262     
2263     /*
2264      *	Stop the transmitter. Basically to do this we will just turn TX
2265      *	interrupts off.
2266      */
2267     
2268     static void stli_stop(struct tty_struct *tty)
2269     {
2270     	stlibrd_t	*brdp;
2271     	stliport_t	*portp;
2272     	asyctrl_t	actrl;
2273     
2274     #if DEBUG
2275     	printk("stli_stop(tty=%x)\n", (int) tty);
2276     #endif
2277     
2278     	if (tty == (struct tty_struct *) NULL)
2279     		return;
2280     	portp = tty->driver_data;
2281     	if (portp == (stliport_t *) NULL)
2282     		return;
2283     	if ((portp->brdnr < 0) || (portp->brdnr >= stli_nrbrds))
2284     		return;
2285     	brdp = stli_brds[portp->brdnr];
2286     	if (brdp == (stlibrd_t *) NULL)
2287     		return;
2288     
2289     	memset(&actrl, 0, sizeof(asyctrl_t));
2290     	actrl.txctrl = CT_STOPFLOW;
2291     #if 0
2292     	stli_cmdwait(brdp, portp, A_PORTCTRL, &actrl, sizeof(asyctrl_t), 0);
2293     #endif
2294     }
2295     
2296     /*****************************************************************************/
2297     
2298     /*
2299      *	Start the transmitter again. Just turn TX interrupts back on.
2300      */
2301     
2302     static void stli_start(struct tty_struct *tty)
2303     {
2304     	stliport_t	*portp;
2305     	stlibrd_t	*brdp;
2306     	asyctrl_t	actrl;
2307     
2308     #if DEBUG
2309     	printk("stli_start(tty=%x)\n", (int) tty);
2310     #endif
2311     
2312     	if (tty == (struct tty_struct *) NULL)
2313     		return;
2314     	portp = tty->driver_data;
2315     	if (portp == (stliport_t *) NULL)
2316     		return;
2317     	if ((portp->brdnr < 0) || (portp->brdnr >= stli_nrbrds))
2318     		return;
2319     	brdp = stli_brds[portp->brdnr];
2320     	if (brdp == (stlibrd_t *) NULL)
2321     		return;
2322     
2323     	memset(&actrl, 0, sizeof(asyctrl_t));
2324     	actrl.txctrl = CT_STARTFLOW;
2325     #if 0
2326     	stli_cmdwait(brdp, portp, A_PORTCTRL, &actrl, sizeof(asyctrl_t), 0);
2327     #endif
2328     }
2329     
2330     /*****************************************************************************/
2331     
2332     /*
2333      *	Scheduler called hang up routine. This is called from the scheduler,
2334      *	not direct from the driver "poll" routine. We can't call it there
2335      *	since the real local hangup code will enable/disable the board and
2336      *	other things that we can't do while handling the poll. Much easier
2337      *	to deal with it some time later (don't really care when, hangups
2338      *	aren't that time critical).
2339      */
2340     
2341     static void stli_dohangup(void *arg)
2342     {
2343     	stliport_t	*portp;
2344     
2345     #if DEBUG
2346     	printk(KERN_DEBUG "stli_dohangup(portp=%x)\n", (int) arg);
2347     #endif
2348     
2349     	/*
2350     	 * FIXME: There's a module removal race here: tty_hangup
2351     	 * calls schedule_task which will call into this
2352     	 * driver later.
2353     	 */
2354     	portp = (stliport_t *) arg;
2355     	if (portp != (stliport_t *) NULL) {
2356     		if (portp->tty != (struct tty_struct *) NULL) {
2357     			tty_hangup(portp->tty);
2358     		}
2359     	}
2360     	MOD_DEC_USE_COUNT;
2361     }
2362     
2363     /*****************************************************************************/
2364     
2365     /*
2366      *	Hangup this port. This is pretty much like closing the port, only
2367      *	a little more brutal. No waiting for data to drain. Shutdown the
2368      *	port and maybe drop signals. This is rather tricky really. We want
2369      *	to close the port as well.
2370      */
2371     
2372     static void stli_hangup(struct tty_struct *tty)
2373     {
2374     	stliport_t	*portp;
2375     	stlibrd_t	*brdp;
2376     	unsigned long	flags;
2377     
2378     #if DEBUG
2379     	printk(KERN_DEBUG "stli_hangup(tty=%x)\n", (int) tty);
2380     #endif
2381     
2382     	if (tty == (struct tty_struct *) NULL)
2383     		return;
2384     	portp = tty->driver_data;
2385     	if (portp == (stliport_t *) NULL)
2386     		return;
2387     	if ((portp->brdnr < 0) || (portp->brdnr >= stli_nrbrds))
2388     		return;
2389     	brdp = stli_brds[portp->brdnr];
2390     	if (brdp == (stlibrd_t *) NULL)
2391     		return;
2392     
2393     	portp->flags &= ~ASYNC_INITIALIZED;
2394     
2395     	save_flags(flags);
2396     	cli();
2397     	if (! test_bit(ST_CLOSING, &portp->state))
2398     		stli_rawclose(brdp, portp, 0, 0);
2399     	if (tty->termios->c_cflag & HUPCL) {
2400     		stli_mkasysigs(&portp->asig, 0, 0);
2401     		if (test_bit(ST_CMDING, &portp->state)) {
2402     			set_bit(ST_DOSIGS, &portp->state);
2403     			set_bit(ST_DOFLUSHTX, &portp->state);
2404     			set_bit(ST_DOFLUSHRX, &portp->state);
2405     		} else {
2406     			stli_sendcmd(brdp, portp, A_SETSIGNALSF,
2407     				&portp->asig, sizeof(asysigs_t), 0);
2408     		}
2409     	}
2410     	restore_flags(flags);
2411     
2412     	clear_bit(ST_TXBUSY, &portp->state);
2413     	clear_bit(ST_RXSTOP, &portp->state);
2414     	set_bit(TTY_IO_ERROR, &tty->flags);
2415     	portp->tty = (struct tty_struct *) NULL;
2416     	portp->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CALLOUT_ACTIVE);
2417     	portp->refcount = 0;
2418     	wake_up_interruptible(&portp->open_wait);
2419     }
2420     
2421     /*****************************************************************************/
2422     
2423     /*
2424      *	Flush characters from the lower buffer. We may not have user context
2425      *	so we cannot sleep waiting for it to complete. Also we need to check
2426      *	if there is chars for this port in the TX cook buffer, and flush them
2427      *	as well.
2428      */
2429     
2430     static void stli_flushbuffer(struct tty_struct *tty)
2431     {
2432     	stliport_t	*portp;
2433     	stlibrd_t	*brdp;
2434     	unsigned long	ftype, flags;
2435     
2436     #if DEBUG
2437     	printk(KERN_DEBUG "stli_flushbuffer(tty=%x)\n", (int) tty);
2438     #endif
2439     
2440     	if (tty == (struct tty_struct *) NULL)
2441     		return;
2442     	portp = tty->driver_data;
2443     	if (portp == (stliport_t *) NULL)
2444     		return;
2445     	if ((portp->brdnr < 0) || (portp->brdnr >= stli_nrbrds))
2446     		return;
2447     	brdp = stli_brds[portp->brdnr];
2448     	if (brdp == (stlibrd_t *) NULL)
2449     		return;
2450     
2451     	save_flags(flags);
2452     	cli();
2453     	if (tty == stli_txcooktty) {
2454     		stli_txcooktty = (struct tty_struct *) NULL;
2455     		stli_txcooksize = 0;
2456     		stli_txcookrealsize = 0;
2457     	}
2458     	if (test_bit(ST_CMDING, &portp->state)) {
2459     		set_bit(ST_DOFLUSHTX, &portp->state);
2460     	} else {
2461     		ftype = FLUSHTX;
2462     		if (test_bit(ST_DOFLUSHRX, &portp->state)) {
2463     			ftype |= FLUSHRX;
2464     			clear_bit(ST_DOFLUSHRX, &portp->state);
2465     		}
2466     		stli_sendcmd(brdp, portp, A_FLUSH, &ftype,
2467     			sizeof(unsigned long), 0);
2468     	}
2469     	restore_flags(flags);
2470     
2471     	wake_up_interruptible(&tty->write_wait);
2472     	if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
2473     	    tty->ldisc.write_wakeup)
2474     		(tty->ldisc.write_wakeup)(tty);
2475     }
2476     
2477     /*****************************************************************************/
2478     
2479     static void stli_breakctl(struct tty_struct *tty, int state)
2480     {
2481     	stlibrd_t	*brdp;
2482     	stliport_t	*portp;
2483     	long		arg;
2484     	/* long savestate, savetime; */
2485     
2486     #if DEBUG
2487     	printk(KERN_DEBUG "stli_breakctl(tty=%x,state=%d)\n", (int) tty, state);
2488     #endif
2489     
2490     	if (tty == (struct tty_struct *) NULL)
2491     		return;
2492     	portp = tty->driver_data;
2493     	if (portp == (stliport_t *) NULL)
2494     		return;
2495     	if ((portp->brdnr < 0) || (portp->brdnr >= stli_nrbrds))
2496     		return;
2497     	brdp = stli_brds[portp->brdnr];
2498     	if (brdp == (stlibrd_t *) NULL)
2499     		return;
2500     
2501     /*
2502      *	Due to a bug in the tty send_break() code we need to preserve
2503      *	the current process state and timeout...
2504     	savetime = current->timeout;
2505     	savestate = current->state;
2506      */
2507     
2508     	arg = (state == -1) ? BREAKON : BREAKOFF;
2509     	stli_cmdwait(brdp, portp, A_BREAK, &arg, sizeof(long), 0);
2510     
2511     /*
2512      *
2513     	current->timeout = savetime;
2514     	current->state = savestate;
2515      */
2516     }
2517     
2518     /*****************************************************************************/
2519     
2520     static void stli_waituntilsent(struct tty_struct *tty, int timeout)
2521     {
2522     	stliport_t	*portp;
2523     	unsigned long	tend;
2524     
2525     #if DEBUG
2526     	printk(KERN_DEBUG "stli_waituntilsent(tty=%x,timeout=%x)\n", (int) tty, timeout);
2527     #endif
2528     
2529     	if (tty == (struct tty_struct *) NULL)
2530     		return;
2531     	portp = tty->driver_data;
2532     	if (portp == (stliport_t *) NULL)
2533     		return;
2534     
2535     	if (timeout == 0)
2536     		timeout = HZ;
2537     	tend = jiffies + timeout;
2538     
2539     	while (test_bit(ST_TXBUSY, &portp->state)) {
2540     		if (signal_pending(current))
2541     			break;
2542     		stli_delay(2);
2543     		if (time_after_eq(jiffies, tend))
2544     			break;
2545     	}
2546     }
2547     
2548     /*****************************************************************************/
2549     
2550     static void stli_sendxchar(struct tty_struct *tty, char ch)
2551     {
2552     	stlibrd_t	*brdp;
2553     	stliport_t	*portp;
2554     	asyctrl_t	actrl;
2555     
2556     #if DEBUG
2557     	printk(KERN_DEBUG "stli_sendxchar(tty=%x,ch=%x)\n", (int) tty, ch);
2558     #endif
2559     
2560     	if (tty == (struct tty_struct *) NULL)
2561     		return;
2562     	portp = tty->driver_data;
2563     	if (portp == (stliport_t *) NULL)
2564     		return;
2565     	if ((portp->brdnr < 0) || (portp->brdnr >= stli_nrbrds))
2566     		return;
2567     	brdp = stli_brds[portp->brdnr];
2568     	if (brdp == (stlibrd_t *) NULL)
2569     		return;
2570     
2571     	memset(&actrl, 0, sizeof(asyctrl_t));
2572     	if (ch == STOP_CHAR(tty)) {
2573     		actrl.rxctrl = CT_STOPFLOW;
2574     	} else if (ch == START_CHAR(tty)) {
2575     		actrl.rxctrl = CT_STARTFLOW;
2576     	} else {
2577     		actrl.txctrl = CT_SENDCHR;
2578     		actrl.tximdch = ch;
2579     	}
2580     
2581     	stli_cmdwait(brdp, portp, A_PORTCTRL, &actrl, sizeof(asyctrl_t), 0);
2582     }
2583     
2584     /*****************************************************************************/
2585     
2586     #define	MAXLINE		80
2587     
2588     /*
2589      *	Format info for a specified port. The line is deliberately limited
2590      *	to 80 characters. (If it is too long it will be truncated, if too
2591      *	short then padded with spaces).
2592      */
2593     
2594     static int stli_portinfo(stlibrd_t *brdp, stliport_t *portp, int portnr, char *pos)
2595     {
2596     	char	*sp, *uart;
2597     	int	rc, cnt;
2598     
2599     	rc = stli_portcmdstats(portp);
2600     
2601     	uart = "UNKNOWN";
2602     	if (brdp->state & BST_STARTED) {
2603     		switch (stli_comstats.hwid) {
2604     		case 0:		uart = "2681"; break;
2605     		case 1:		uart = "SC26198"; break;
2606     		default:	uart = "CD1400"; break;
2607     		}
2608     	}
2609     
2610     	sp = pos;
2611     	sp += sprintf(sp, "%d: uart:%s ", portnr, uart);
2612     
2613     	if ((brdp->state & BST_STARTED) && (rc >= 0)) {
2614     		sp += sprintf(sp, "tx:%d rx:%d", (int) stli_comstats.txtotal,
2615     			(int) stli_comstats.rxtotal);
2616     
2617     		if (stli_comstats.rxframing)
2618     			sp += sprintf(sp, " fe:%d",
2619     				(int) stli_comstats.rxframing);
2620     		if (stli_comstats.rxparity)
2621     			sp += sprintf(sp, " pe:%d",
2622     				(int) stli_comstats.rxparity);
2623     		if (stli_comstats.rxbreaks)
2624     			sp += sprintf(sp, " brk:%d",
2625     				(int) stli_comstats.rxbreaks);
2626     		if (stli_comstats.rxoverrun)
2627     			sp += sprintf(sp, " oe:%d",
2628     				(int) stli_comstats.rxoverrun);
2629     
2630     		cnt = sprintf(sp, "%s%s%s%s%s ",
2631     			(stli_comstats.signals & TIOCM_RTS) ? "|RTS" : "",
2632     			(stli_comstats.signals & TIOCM_CTS) ? "|CTS" : "",
2633     			(stli_comstats.signals & TIOCM_DTR) ? "|DTR" : "",
2634     			(stli_comstats.signals & TIOCM_CD) ? "|DCD" : "",
2635     			(stli_comstats.signals & TIOCM_DSR) ? "|DSR" : "");
2636     		*sp = ' ';
2637     		sp += cnt;
2638     	}
2639     
2640     	for (cnt = (sp - pos); (cnt < (MAXLINE - 1)); cnt++)
2641     		*sp++ = ' ';
2642     	if (cnt >= MAXLINE)
2643     		pos[(MAXLINE - 2)] = '+';
2644     	pos[(MAXLINE - 1)] = '\n';
2645     
2646     	return(MAXLINE);
2647     }
2648     
2649     /*****************************************************************************/
2650     
2651     /*
2652      *	Port info, read from the /proc file system.
2653      */
2654     
2655     static int stli_readproc(char *page, char **start, off_t off, int count, int *eof, void *data)
2656     {
2657     	stlibrd_t	*brdp;
2658     	stliport_t	*portp;
2659     	int		brdnr, portnr, totalport;
2660     	int		curoff, maxoff;
2661     	char		*pos;
2662     
2663     #if DEBUG
2664     	printk(KERN_DEBUG "stli_readproc(page=%x,start=%x,off=%x,count=%d,eof=%x,"
2665     		"data=%x\n", (int) page, (int) start, (int) off, count,
2666     		(int) eof, (int) data);
2667     #endif
2668     
2669     	pos = page;
2670     	totalport = 0;
2671     	curoff = 0;
2672     
2673     	if (off == 0) {
2674     		pos += sprintf(pos, "%s: version %s", stli_drvtitle,
2675     			stli_drvversion);
2676     		while (pos < (page + MAXLINE - 1))
2677     			*pos++ = ' ';
2678     		*pos++ = '\n';
2679     	}
2680     	curoff =  MAXLINE;
2681     
2682     /*
2683      *	We scan through for each board, panel and port. The offset is
2684      *	calculated on the fly, and irrelevant ports are skipped.
2685      */
2686     	for (brdnr = 0; (brdnr < stli_nrbrds); brdnr++) {
2687     		brdp = stli_brds[brdnr];
2688     		if (brdp == (stlibrd_t *) NULL)
2689     			continue;
2690     		if (brdp->state == 0)
2691     			continue;
2692     
2693     		maxoff = curoff + (brdp->nrports * MAXLINE);
2694     		if (off >= maxoff) {
2695     			curoff = maxoff;
2696     			continue;
2697     		}
2698     
2699     		totalport = brdnr * STL_MAXPORTS;
2700     		for (portnr = 0; (portnr < brdp->nrports); portnr++,
2701     		    totalport++) {
2702     			portp = brdp->ports[portnr];
2703     			if (portp == (stliport_t *) NULL)
2704     				continue;
2705     			if (off >= (curoff += MAXLINE))
2706     				continue;
2707     			if ((pos - page + MAXLINE) > count)
2708     				goto stli_readdone;
2709     			pos += stli_portinfo(brdp, portp, totalport, pos);
2710     		}
2711     	}
2712     
2713     	*eof = 1;
2714     
2715     stli_readdone:
2716     	*start = page;
2717     	return(pos - page);
2718     }
2719     
2720     /*****************************************************************************/
2721     
2722     /*
2723      *	Generic send command routine. This will send a message to the slave,
2724      *	of the specified type with the specified argument. Must be very
2725      *	careful of data that will be copied out from shared memory -
2726      *	containing command results. The command completion is all done from
2727      *	a poll routine that does not have user context. Therefore you cannot
2728      *	copy back directly into user space, or to the kernel stack of a
2729      *	process. This routine does not sleep, so can be called from anywhere.
2730      */
2731     
2732     static void stli_sendcmd(stlibrd_t *brdp, stliport_t *portp, unsigned long cmd, void *arg, int size, int copyback)
2733     {
2734     	volatile cdkhdr_t	*hdrp;
2735     	volatile cdkctrl_t	*cp;
2736     	volatile unsigned char	*bits;
2737     	unsigned long		flags;
2738     
2739     #if DEBUG
2740     	printk(KERN_DEBUG "stli_sendcmd(brdp=%x,portp=%x,cmd=%x,arg=%x,size=%d,"
2741     		"copyback=%d)\n", (int) brdp, (int) portp, (int) cmd,
2742     		(int) arg, size, copyback);
2743     #endif
2744     
2745     	save_flags(flags);
2746     	cli();
2747     
2748     	if (test_bit(ST_CMDING, &portp->state)) {
2749     		printk(KERN_ERR "STALLION: command already busy, cmd=%x!\n",
2750     				(int) cmd);
2751     		restore_flags(flags);
2752     		return;
2753     	}
2754     
2755     	EBRDENABLE(brdp);
2756     	cp = &((volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr))->ctrl;
2757     	if (size > 0) {
2758     		memcpy((void *) &(cp->args[0]), arg, size);
2759     		if (copyback) {
2760     			portp->argp = arg;
2761     			portp->argsize = size;
2762     		}
2763     	}
2764     	cp->status = 0;
2765     	cp->cmd = cmd;
2766     	hdrp = (volatile cdkhdr_t *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
2767     	bits = ((volatile unsigned char *) hdrp) + brdp->slaveoffset +
2768     		portp->portidx;
2769     	*bits |= portp->portbit;
2770     	set_bit(ST_CMDING, &portp->state);
2771     	EBRDDISABLE(brdp);
2772     	restore_flags(flags);
2773     }
2774     
2775     /*****************************************************************************/
2776     
2777     /*
2778      *	Read data from shared memory. This assumes that the shared memory
2779      *	is enabled and that interrupts are off. Basically we just empty out
2780      *	the shared memory buffer into the tty buffer. Must be careful to
2781      *	handle the case where we fill up the tty buffer, but still have
2782      *	more chars to unload.
2783      */
2784     
2785     static inline void stli_read(stlibrd_t *brdp, stliport_t *portp)
2786     {
2787     	volatile cdkasyrq_t	*rp;
2788     	volatile char		*shbuf;
2789     	struct tty_struct	*tty;
2790     	unsigned int		head, tail, size;
2791     	unsigned int		len, stlen;
2792     
2793     #if DEBUG
2794     	printk(KERN_DEBUG "stli_read(brdp=%x,portp=%d)\n",
2795     			(int) brdp, (int) portp);
2796     #endif
2797     
2798     	if (test_bit(ST_RXSTOP, &portp->state))
2799     		return;
2800     	tty = portp->tty;
2801     	if (tty == (struct tty_struct *) NULL)
2802     		return;
2803     
2804     	rp = &((volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr))->rxq;
2805     	head = (unsigned int) rp->head;
2806     	if (head != ((unsigned int) rp->head))
2807     		head = (unsigned int) rp->head;
2808     	tail = (unsigned int) rp->tail;
2809     	size = portp->rxsize;
2810     	if (head >= tail) {
2811     		len = head - tail;
2812     		stlen = len;
2813     	} else {
2814     		len = size - (tail - head);
2815     		stlen = size - tail;
2816     	}
2817     
2818     	len = MIN(len, (TTY_FLIPBUF_SIZE - tty->flip.count));
2819     	shbuf = (volatile char *) EBRDGETMEMPTR(brdp, portp->rxoffset);
2820     
2821     	while (len > 0) {
2822     		stlen = MIN(len, stlen);
2823     		memcpy(tty->flip.char_buf_ptr, (char *) (shbuf + tail), stlen);
2824     		memset(tty->flip.flag_buf_ptr, 0, stlen);
2825     		tty->flip.char_buf_ptr += stlen;
2826     		tty->flip.flag_buf_ptr += stlen;
2827     		tty->flip.count += stlen;
2828     
2829     		len -= stlen;
2830     		tail += stlen;
2831     		if (tail >= size) {
2832     			tail = 0;
2833     			stlen = head;
2834     		}
2835     	}
2836     	rp = &((volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr))->rxq;
2837     	rp->tail = tail;
2838     
2839     	if (head != tail)
2840     		set_bit(ST_RXING, &portp->state);
2841     
2842     	tty_schedule_flip(tty);
2843     }
2844     
2845     /*****************************************************************************/
2846     
2847     /*
2848      *	Set up and carry out any delayed commands. There is only a small set
2849      *	of slave commands that can be done "off-level". So it is not too
2850      *	difficult to deal with them here.
2851      */
2852     
2853     static inline void stli_dodelaycmd(stliport_t *portp, volatile cdkctrl_t *cp)
2854     {
2855     	int	cmd;
2856     
2857     	if (test_bit(ST_DOSIGS, &portp->state)) {
2858     		if (test_bit(ST_DOFLUSHTX, &portp->state) &&
2859     		    test_bit(ST_DOFLUSHRX, &portp->state))
2860     			cmd = A_SETSIGNALSF;
2861     		else if (test_bit(ST_DOFLUSHTX, &portp->state))
2862     			cmd = A_SETSIGNALSFTX;
2863     		else if (test_bit(ST_DOFLUSHRX, &portp->state))
2864     			cmd = A_SETSIGNALSFRX;
2865     		else
2866     			cmd = A_SETSIGNALS;
2867     		clear_bit(ST_DOFLUSHTX, &portp->state);
2868     		clear_bit(ST_DOFLUSHRX, &portp->state);
2869     		clear_bit(ST_DOSIGS, &portp->state);
2870     		memcpy((void *) &(cp->args[0]), (void *) &portp->asig,
2871     			sizeof(asysigs_t));
2872     		cp->status = 0;
2873     		cp->cmd = cmd;
2874     		set_bit(ST_CMDING, &portp->state);
2875     	} else if (test_bit(ST_DOFLUSHTX, &portp->state) ||
2876     	    test_bit(ST_DOFLUSHRX, &portp->state)) {
2877     		cmd = ((test_bit(ST_DOFLUSHTX, &portp->state)) ? FLUSHTX : 0);
2878     		cmd |= ((test_bit(ST_DOFLUSHRX, &portp->state)) ? FLUSHRX : 0);
2879     		clear_bit(ST_DOFLUSHTX, &portp->state);
2880     		clear_bit(ST_DOFLUSHRX, &portp->state);
2881     		memcpy((void *) &(cp->args[0]), (void *) &cmd, sizeof(int));
2882     		cp->status = 0;
2883     		cp->cmd = A_FLUSH;
2884     		set_bit(ST_CMDING, &portp->state);
2885     	}
2886     }
2887     
2888     /*****************************************************************************/
2889     
2890     /*
2891      *	Host command service checking. This handles commands or messages
2892      *	coming from the slave to the host. Must have board shared memory
2893      *	enabled and interrupts off when called. Notice that by servicing the
2894      *	read data last we don't need to change the shared memory pointer
2895      *	during processing (which is a slow IO operation).
2896      *	Return value indicates if this port is still awaiting actions from
2897      *	the slave (like open, command, or even TX data being sent). If 0
2898      *	then port is still busy, otherwise no longer busy.
2899      */
2900     
2901     static inline int stli_hostcmd(stlibrd_t *brdp, stliport_t *portp)
2902     {
2903     	volatile cdkasy_t	*ap;
2904     	volatile cdkctrl_t	*cp;
2905     	struct tty_struct	*tty;
2906     	asynotify_t		nt;
2907     	unsigned long		oldsigs;
2908     	int			rc, donerx;
2909     
2910     #if DEBUG
2911     	printk(KERN_DEBUG "stli_hostcmd(brdp=%x,channr=%d)\n",
2912     			(int) brdp, channr);
2913     #endif
2914     
2915     	ap = (volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr);
2916     	cp = &ap->ctrl;
2917     
2918     /*
2919      *	Check if we are waiting for an open completion message.
2920      */
2921     	if (test_bit(ST_OPENING, &portp->state)) {
2922     		rc = (int) cp->openarg;
2923     		if ((cp->open == 0) && (rc != 0)) {
2924     			if (rc > 0)
2925     				rc--;
2926     			cp->openarg = 0;
2927     			portp->rc = rc;
2928     			clear_bit(ST_OPENING, &portp->state);
2929     			wake_up_interruptible(&portp->raw_wait);
2930     		}
2931     	}
2932     
2933     /*
2934      *	Check if we are waiting for a close completion message.
2935      */
2936     	if (test_bit(ST_CLOSING, &portp->state)) {
2937     		rc = (int) cp->closearg;
2938     		if ((cp->close == 0) && (rc != 0)) {
2939     			if (rc > 0)
2940     				rc--;
2941     			cp->closearg = 0;
2942     			portp->rc = rc;
2943     			clear_bit(ST_CLOSING, &portp->state);
2944     			wake_up_interruptible(&portp->raw_wait);
2945     		}
2946     	}
2947     
2948     /*
2949      *	Check if we are waiting for a command completion message. We may
2950      *	need to copy out the command results associated with this command.
2951      */
2952     	if (test_bit(ST_CMDING, &portp->state)) {
2953     		rc = cp->status;
2954     		if ((cp->cmd == 0) && (rc != 0)) {
2955     			if (rc > 0)
2956     				rc--;
2957     			if (portp->argp != (void *) NULL) {
2958     				memcpy(portp->argp, (void *) &(cp->args[0]),
2959     					portp->argsize);
2960     				portp->argp = (void *) NULL;
2961     			}
2962     			cp->status = 0;
2963     			portp->rc = rc;
2964     			clear_bit(ST_CMDING, &portp->state);
2965     			stli_dodelaycmd(portp, cp);
2966     			wake_up_interruptible(&portp->raw_wait);
2967     		}
2968     	}
2969     
2970     /*
2971      *	Check for any notification messages ready. This includes lots of
2972      *	different types of events - RX chars ready, RX break received,
2973      *	TX data low or empty in the slave, modem signals changed state.
2974      */
2975     	donerx = 0;
2976     
2977     	if (ap->notify) {
2978     		nt = ap->changed;
2979     		ap->notify = 0;
2980     		tty = portp->tty;
2981     
2982     		if (nt.signal & SG_DCD) {
2983     			oldsigs = portp->sigs;
2984     			portp->sigs = stli_mktiocm(nt.sigvalue);
2985     			clear_bit(ST_GETSIGS, &portp->state);
2986     			if ((portp->sigs & TIOCM_CD) &&
2987     			    ((oldsigs & TIOCM_CD) == 0))
2988     				wake_up_interruptible(&portp->open_wait);
2989     			if ((oldsigs & TIOCM_CD) &&
2990     			    ((portp->sigs & TIOCM_CD) == 0)) {
2991     				if (portp->flags & ASYNC_CHECK_CD) {
2992     					if (! ((portp->flags & ASYNC_CALLOUT_ACTIVE) &&
2993     					    (portp->flags & ASYNC_CALLOUT_NOHUP))) {
2994     						if (tty != (struct tty_struct *) NULL) {
2995     							MOD_INC_USE_COUNT;
2996     							if (schedule_task(&portp->tqhangup) == 0)
2997     								MOD_DEC_USE_COUNT;
2998     						}
2999     					}
3000     				}
3001     			}
3002     		}
3003     
3004     		if (nt.data & DT_TXEMPTY)
3005     			clear_bit(ST_TXBUSY, &portp->state);
3006     		if (nt.data & (DT_TXEMPTY | DT_TXLOW)) {
3007     			if (tty != (struct tty_struct *) NULL) {
3008     				if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
3009     				    tty->ldisc.write_wakeup) {
3010     					(tty->ldisc.write_wakeup)(tty);
3011     					EBRDENABLE(brdp);
3012     				}
3013     				wake_up_interruptible(&tty->write_wait);
3014     			}
3015     		}
3016     
3017     		if ((nt.data & DT_RXBREAK) && (portp->rxmarkmsk & BRKINT)) {
3018     			if (tty != (struct tty_struct *) NULL) {
3019     				if (tty->flip.count < TTY_FLIPBUF_SIZE) {
3020     					tty->flip.count++;
3021     					*tty->flip.flag_buf_ptr++ = TTY_BREAK;
3022     					*tty->flip.char_buf_ptr++ = 0;
3023     					if (portp->flags & ASYNC_SAK) {
3024     						do_SAK(tty);
3025     						EBRDENABLE(brdp);
3026     					}
3027     					tty_schedule_flip(tty);
3028     				}
3029     			}
3030     		}
3031     
3032     		if (nt.data & DT_RXBUSY) {
3033     			donerx++;
3034     			stli_read(brdp, portp);
3035     		}
3036     	}
3037     
3038     /*
3039      *	It might seem odd that we are checking for more RX chars here.
3040      *	But, we need to handle the case where the tty buffer was previously
3041      *	filled, but we had more characters to pass up. The slave will not
3042      *	send any more RX notify messages until the RX buffer has been emptied.
3043      *	But it will leave the service bits on (since the buffer is not empty).
3044      *	So from here we can try to process more RX chars.
3045      */
3046     	if ((!donerx) && test_bit(ST_RXING, &portp->state)) {
3047     		clear_bit(ST_RXING, &portp->state);
3048     		stli_read(brdp, portp);
3049     	}
3050     
3051     	return((test_bit(ST_OPENING, &portp->state) ||
3052     		test_bit(ST_CLOSING, &portp->state) ||
3053     		test_bit(ST_CMDING, &portp->state) ||
3054     		test_bit(ST_TXBUSY, &portp->state) ||
3055     		test_bit(ST_RXING, &portp->state)) ? 0 : 1);
3056     }
3057     
3058     /*****************************************************************************/
3059     
3060     /*
3061      *	Service all ports on a particular board. Assumes that the boards
3062      *	shared memory is enabled, and that the page pointer is pointed
3063      *	at the cdk header structure.
3064      */
3065     
3066     static inline void stli_brdpoll(stlibrd_t *brdp, volatile cdkhdr_t *hdrp)
3067     {
3068     	stliport_t	*portp;
3069     	unsigned char	hostbits[(STL_MAXCHANS / 8) + 1];
3070     	unsigned char	slavebits[(STL_MAXCHANS / 8) + 1];
3071     	unsigned char	*slavep;
3072     	int		bitpos, bitat, bitsize;
3073     	int 		channr, nrdevs, slavebitchange;
3074     
3075     	bitsize = brdp->bitsize;
3076     	nrdevs = brdp->nrdevs;
3077     
3078     /*
3079      *	Check if slave wants any service. Basically we try to do as
3080      *	little work as possible here. There are 2 levels of service
3081      *	bits. So if there is nothing to do we bail early. We check
3082      *	8 service bits at a time in the inner loop, so we can bypass
3083      *	the lot if none of them want service.
3084      */
3085     	memcpy(&hostbits[0], (((unsigned char *) hdrp) + brdp->hostoffset),
3086     		bitsize);
3087     
3088     	memset(&slavebits[0], 0, bitsize);
3089     	slavebitchange = 0;
3090     
3091     	for (bitpos = 0; (bitpos < bitsize); bitpos++) {
3092     		if (hostbits[bitpos] == 0)
3093     			continue;
3094     		channr = bitpos * 8;
3095     		for (bitat = 0x1; (channr < nrdevs); channr++, bitat <<= 1) {
3096     			if (hostbits[bitpos] & bitat) {
3097     				portp = brdp->ports[(channr - 1)];
3098     				if (stli_hostcmd(brdp, portp)) {
3099     					slavebitchange++;
3100     					slavebits[bitpos] |= bitat;
3101     				}
3102     			}
3103     		}
3104     	}
3105     
3106     /*
3107      *	If any of the ports are no longer busy then update them in the
3108      *	slave request bits. We need to do this after, since a host port
3109      *	service may initiate more slave requests.
3110      */
3111     	if (slavebitchange) {
3112     		hdrp = (volatile cdkhdr_t *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
3113     		slavep = ((unsigned char *) hdrp) + brdp->slaveoffset;
3114     		for (bitpos = 0; (bitpos < bitsize); bitpos++) {
3115     			if (slavebits[bitpos])
3116     				slavep[bitpos] &= ~slavebits[bitpos];
3117     		}
3118     	}
3119     }
3120     
3121     /*****************************************************************************/
3122     
3123     /*
3124      *	Driver poll routine. This routine polls the boards in use and passes
3125      *	messages back up to host when necessary. This is actually very
3126      *	CPU efficient, since we will always have the kernel poll clock, it
3127      *	adds only a few cycles when idle (since board service can be
3128      *	determined very easily), but when loaded generates no interrupts
3129      *	(with their expensive associated context change).
3130      */
3131     
3132     static void stli_poll(unsigned long arg)
3133     {
3134     	volatile cdkhdr_t	*hdrp;
3135     	stlibrd_t		*brdp;
3136     	int 			brdnr;
3137     
3138     	stli_timerlist.expires = STLI_TIMEOUT;
3139     	add_timer(&stli_timerlist);
3140     
3141     /*
3142      *	Check each board and do any servicing required.
3143      */
3144     	for (brdnr = 0; (brdnr < stli_nrbrds); brdnr++) {
3145     		brdp = stli_brds[brdnr];
3146     		if (brdp == (stlibrd_t *) NULL)
3147     			continue;
3148     		if ((brdp->state & BST_STARTED) == 0)
3149     			continue;
3150     
3151     		EBRDENABLE(brdp);
3152     		hdrp = (volatile cdkhdr_t *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
3153     		if (hdrp->hostreq)
3154     			stli_brdpoll(brdp, hdrp);
3155     		EBRDDISABLE(brdp);
3156     	}
3157     }
3158     
3159     /*****************************************************************************/
3160     
3161     /*
3162      *	Translate the termios settings into the port setting structure of
3163      *	the slave.
3164      */
3165     
3166     static void stli_mkasyport(stliport_t *portp, asyport_t *pp, struct termios *tiosp)
3167     {
3168     #if DEBUG
3169     	printk(KERN_DEBUG "stli_mkasyport(portp=%x,pp=%x,tiosp=%d)\n",
3170     		(int) portp, (int) pp, (int) tiosp);
3171     #endif
3172     
3173     	memset(pp, 0, sizeof(asyport_t));
3174     
3175     /*
3176      *	Start of by setting the baud, char size, parity and stop bit info.
3177      */
3178     	pp->baudout = tiosp->c_cflag & CBAUD;
3179     	if (pp->baudout & CBAUDEX) {
3180     		pp->baudout &= ~CBAUDEX;
3181     		if ((pp->baudout < 1) || (pp->baudout > 4))
3182     			tiosp->c_cflag &= ~CBAUDEX;
3183     		else
3184     			pp->baudout += 15;
3185     	}
3186     	pp->baudout = stli_baudrates[pp->baudout];
3187     	if ((tiosp->c_cflag & CBAUD) == B38400) {
3188     		if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
3189     			pp->baudout = 57600;
3190     		else if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
3191     			pp->baudout = 115200;
3192     		else if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
3193     			pp->baudout = 230400;
3194     		else if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
3195     			pp->baudout = 460800;
3196     		else if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)
3197     			pp->baudout = (portp->baud_base / portp->custom_divisor);
3198     	}
3199     	if (pp->baudout > STL_MAXBAUD)
3200     		pp->baudout = STL_MAXBAUD;
3201     	pp->baudin = pp->baudout;
3202     
3203     	switch (tiosp->c_cflag & CSIZE) {
3204     	case CS5:
3205     		pp->csize = 5;
3206     		break;
3207     	case CS6:
3208     		pp->csize = 6;
3209     		break;
3210     	case CS7:
3211     		pp->csize = 7;
3212     		break;
3213     	default:
3214     		pp->csize = 8;
3215     		break;
3216     	}
3217     
3218     	if (tiosp->c_cflag & CSTOPB)
3219     		pp->stopbs = PT_STOP2;
3220     	else
3221     		pp->stopbs = PT_STOP1;
3222     
3223     	if (tiosp->c_cflag & PARENB) {
3224     		if (tiosp->c_cflag & PARODD)
3225     			pp->parity = PT_ODDPARITY;
3226     		else
3227     			pp->parity = PT_EVENPARITY;
3228     	} else {
3229     		pp->parity = PT_NOPARITY;
3230     	}
3231     
3232     /*
3233      *	Set up any flow control options enabled.
3234      */
3235     	if (tiosp->c_iflag & IXON) {
3236     		pp->flow |= F_IXON;
3237     		if (tiosp->c_iflag & IXANY)
3238     			pp->flow |= F_IXANY;
3239     	}
3240     	if (tiosp->c_cflag & CRTSCTS)
3241     		pp->flow |= (F_RTSFLOW | F_CTSFLOW);
3242     
3243     	pp->startin = tiosp->c_cc[VSTART];
3244     	pp->stopin = tiosp->c_cc[VSTOP];
3245     	pp->startout = tiosp->c_cc[VSTART];
3246     	pp->stopout = tiosp->c_cc[VSTOP];
3247     
3248     /*
3249      *	Set up the RX char marking mask with those RX error types we must
3250      *	catch. We can get the slave to help us out a little here, it will
3251      *	ignore parity errors and breaks for us, and mark parity errors in
3252      *	the data stream.
3253      */
3254     	if (tiosp->c_iflag & IGNPAR)
3255     		pp->iflag |= FI_IGNRXERRS;
3256     	if (tiosp->c_iflag & IGNBRK)
3257     		pp->iflag |= FI_IGNBREAK;
3258     
3259     	portp->rxmarkmsk = 0;
3260     	if (tiosp->c_iflag & (INPCK | PARMRK))
3261     		pp->iflag |= FI_1MARKRXERRS;
3262     	if (tiosp->c_iflag & BRKINT)
3263     		portp->rxmarkmsk |= BRKINT;
3264     
3265     /*
3266      *	Set up clocal processing as required.
3267      */
3268     	if (tiosp->c_cflag & CLOCAL)
3269     		portp->flags &= ~ASYNC_CHECK_CD;
3270     	else
3271     		portp->flags |= ASYNC_CHECK_CD;
3272     
3273     /*
3274      *	Transfer any persistent flags into the asyport structure.
3275      */
3276     	pp->pflag = (portp->pflag & 0xffff);
3277     	pp->vmin = (portp->pflag & P_RXIMIN) ? 1 : 0;
3278     	pp->vtime = (portp->pflag & P_RXITIME) ? 1 : 0;
3279     	pp->cc[1] = (portp->pflag & P_RXTHOLD) ? 1 : 0;
3280     }
3281     
3282     /*****************************************************************************/
3283     
3284     /*
3285      *	Construct a slave signals structure for setting the DTR and RTS
3286      *	signals as specified.
3287      */
3288     
3289     static void stli_mkasysigs(asysigs_t *sp, int dtr, int rts)
3290     {
3291     #if DEBUG
3292     	printk(KERN_DEBUG "stli_mkasysigs(sp=%x,dtr=%d,rts=%d)\n",
3293     			(int) sp, dtr, rts);
3294     #endif
3295     
3296     	memset(sp, 0, sizeof(asysigs_t));
3297     	if (dtr >= 0) {
3298     		sp->signal |= SG_DTR;
3299     		sp->sigvalue |= ((dtr > 0) ? SG_DTR : 0);
3300     	}
3301     	if (rts >= 0) {
3302     		sp->signal |= SG_RTS;
3303     		sp->sigvalue |= ((rts > 0) ? SG_RTS : 0);
3304     	}
3305     }
3306     
3307     /*****************************************************************************/
3308     
3309     /*
3310      *	Convert the signals returned from the slave into a local TIOCM type
3311      *	signals value. We keep them locally in TIOCM format.
3312      */
3313     
3314     static long stli_mktiocm(unsigned long sigvalue)
3315     {
3316     	long	tiocm;
3317     
3318     #if DEBUG
3319     	printk(KERN_DEBUG "stli_mktiocm(sigvalue=%x)\n", (int) sigvalue);
3320     #endif
3321     
3322     	tiocm = 0;
3323     	tiocm |= ((sigvalue & SG_DCD) ? TIOCM_CD : 0);
3324     	tiocm |= ((sigvalue & SG_CTS) ? TIOCM_CTS : 0);
3325     	tiocm |= ((sigvalue & SG_RI) ? TIOCM_RI : 0);
3326     	tiocm |= ((sigvalue & SG_DSR) ? TIOCM_DSR : 0);
3327     	tiocm |= ((sigvalue & SG_DTR) ? TIOCM_DTR : 0);
3328     	tiocm |= ((sigvalue & SG_RTS) ? TIOCM_RTS : 0);
3329     	return(tiocm);
3330     }
3331     
3332     /*****************************************************************************/
3333     
3334     /*
3335      *	All panels and ports actually attached have been worked out. All
3336      *	we need to do here is set up the appropriate per port data structures.
3337      */
3338     
3339     static inline int stli_initports(stlibrd_t *brdp)
3340     {
3341     	stliport_t	*portp;
3342     	int		i, panelnr, panelport;
3343     
3344     #if DEBUG
3345     	printk(KERN_DEBUG "stli_initports(brdp=%x)\n", (int) brdp);
3346     #endif
3347     
3348     	for (i = 0, panelnr = 0, panelport = 0; (i < brdp->nrports); i++) {
3349     		portp = (stliport_t *) stli_memalloc(sizeof(stliport_t));
3350     		if (portp == (stliport_t *) NULL) {
3351     			printk("STALLION: failed to allocate port structure\n");
3352     			continue;
3353     		}
3354     
3355     		memset(portp, 0, sizeof(stliport_t));
3356     		portp->magic = STLI_PORTMAGIC;
3357     		portp->portnr = i;
3358     		portp->brdnr = brdp->brdnr;
3359     		portp->panelnr = panelnr;
3360     		portp->baud_base = STL_BAUDBASE;
3361     		portp->close_delay = STL_CLOSEDELAY;
3362     		portp->closing_wait = 30 * HZ;
3363     		portp->tqhangup.routine = stli_dohangup;
3364     		portp->tqhangup.data = portp;
3365     		init_waitqueue_head(&portp->open_wait);
3366     		init_waitqueue_head(&portp->close_wait);
3367     		init_waitqueue_head(&portp->raw_wait);
3368     		portp->normaltermios = stli_deftermios;
3369     		portp->callouttermios = stli_deftermios;
3370     		panelport++;
3371     		if (panelport >= brdp->panels[panelnr]) {
3372     			panelport = 0;
3373     			panelnr++;
3374     		}
3375     		brdp->ports[i] = portp;
3376     	}
3377     
3378     	return(0);
3379     }
3380     
3381     /*****************************************************************************/
3382     
3383     /*
3384      *	All the following routines are board specific hardware operations.
3385      */
3386     
3387     static void stli_ecpinit(stlibrd_t *brdp)
3388     {
3389     	unsigned long	memconf;
3390     
3391     #if DEBUG
3392     	printk(KERN_DEBUG "stli_ecpinit(brdp=%d)\n", (int) brdp);
3393     #endif
3394     
3395     	outb(ECP_ATSTOP, (brdp->iobase + ECP_ATCONFR));
3396     	udelay(10);
3397     	outb(ECP_ATDISABLE, (brdp->iobase + ECP_ATCONFR));
3398     	udelay(100);
3399     
3400     	memconf = (brdp->memaddr & ECP_ATADDRMASK) >> ECP_ATADDRSHFT;
3401     	outb(memconf, (brdp->iobase + ECP_ATMEMAR));
3402     }
3403     
3404     /*****************************************************************************/
3405     
3406     static void stli_ecpenable(stlibrd_t *brdp)
3407     {	
3408     #if DEBUG
3409     	printk(KERN_DEBUG "stli_ecpenable(brdp=%x)\n", (int) brdp);
3410     #endif
3411     	outb(ECP_ATENABLE, (brdp->iobase + ECP_ATCONFR));
3412     }
3413     
3414     /*****************************************************************************/
3415     
3416     static void stli_ecpdisable(stlibrd_t *brdp)
3417     {	
3418     #if DEBUG
3419     	printk(KERN_DEBUG "stli_ecpdisable(brdp=%x)\n", (int) brdp);
3420     #endif
3421     	outb(ECP_ATDISABLE, (brdp->iobase + ECP_ATCONFR));
3422     }
3423     
3424     /*****************************************************************************/
3425     
3426     static char *stli_ecpgetmemptr(stlibrd_t *brdp, unsigned long offset, int line)
3427     {	
3428     	void		*ptr;
3429     	unsigned char	val;
3430     
3431     #if DEBUG
3432     	printk(KERN_DEBUG "stli_ecpgetmemptr(brdp=%x,offset=%x)\n", (int) brdp,
3433     		(int) offset);
3434     #endif
3435     
3436     	if (offset > brdp->memsize) {
3437     		printk(KERN_ERR "STALLION: shared memory pointer=%x out of "
3438     				"range at line=%d(%d), brd=%d\n",
3439     			(int) offset, line, __LINE__, brdp->brdnr);
3440     		ptr = 0;
3441     		val = 0;
3442     	} else {
3443     		ptr = brdp->membase + (offset % ECP_ATPAGESIZE);
3444     		val = (unsigned char) (offset / ECP_ATPAGESIZE);
3445     	}
3446     	outb(val, (brdp->iobase + ECP_ATMEMPR));
3447     	return(ptr);
3448     }
3449     
3450     /*****************************************************************************/
3451     
3452     static void stli_ecpreset(stlibrd_t *brdp)
3453     {	
3454     #if DEBUG
3455     	printk(KERN_DEBUG "stli_ecpreset(brdp=%x)\n", (int) brdp);
3456     #endif
3457     
3458     	outb(ECP_ATSTOP, (brdp->iobase + ECP_ATCONFR));
3459     	udelay(10);
3460     	outb(ECP_ATDISABLE, (brdp->iobase + ECP_ATCONFR));
3461     	udelay(500);
3462     }
3463     
3464     /*****************************************************************************/
3465     
3466     static void stli_ecpintr(stlibrd_t *brdp)
3467     {	
3468     #if DEBUG
3469     	printk(KERN_DEBUG "stli_ecpintr(brdp=%x)\n", (int) brdp);
3470     #endif
3471     	outb(0x1, brdp->iobase);
3472     }
3473     
3474     /*****************************************************************************/
3475     
3476     /*
3477      *	The following set of functions act on ECP EISA boards.
3478      */
3479     
3480     static void stli_ecpeiinit(stlibrd_t *brdp)
3481     {
3482     	unsigned long	memconf;
3483     
3484     #if DEBUG
3485     	printk(KERN_DEBUG "stli_ecpeiinit(brdp=%x)\n", (int) brdp);
3486     #endif
3487     
3488     	outb(0x1, (brdp->iobase + ECP_EIBRDENAB));
3489     	outb(ECP_EISTOP, (brdp->iobase + ECP_EICONFR));
3490     	udelay(10);
3491     	outb(ECP_EIDISABLE, (brdp->iobase + ECP_EICONFR));
3492     	udelay(500);
3493     
3494     	memconf = (brdp->memaddr & ECP_EIADDRMASKL) >> ECP_EIADDRSHFTL;
3495     	outb(memconf, (brdp->iobase + ECP_EIMEMARL));
3496     	memconf = (brdp->memaddr & ECP_EIADDRMASKH) >> ECP_EIADDRSHFTH;
3497     	outb(memconf, (brdp->iobase + ECP_EIMEMARH));
3498     }
3499     
3500     /*****************************************************************************/
3501     
3502     static void stli_ecpeienable(stlibrd_t *brdp)
3503     {	
3504     	outb(ECP_EIENABLE, (brdp->iobase + ECP_EICONFR));
3505     }
3506     
3507     /*****************************************************************************/
3508     
3509     static void stli_ecpeidisable(stlibrd_t *brdp)
3510     {	
3511     	outb(ECP_EIDISABLE, (brdp->iobase + ECP_EICONFR));
3512     }
3513     
3514     /*****************************************************************************/
3515     
3516     static char *stli_ecpeigetmemptr(stlibrd_t *brdp, unsigned long offset, int line)
3517     {	
3518     	void		*ptr;
3519     	unsigned char	val;
3520     
3521     #if DEBUG
3522     	printk(KERN_DEBUG "stli_ecpeigetmemptr(brdp=%x,offset=%x,line=%d)\n",
3523     		(int) brdp, (int) offset, line);
3524     #endif
3525     
3526     	if (offset > brdp->memsize) {
3527     		printk(KERN_ERR "STALLION: shared memory pointer=%x out of "
3528     				"range at line=%d(%d), brd=%d\n",
3529     			(int) offset, line, __LINE__, brdp->brdnr);
3530     		ptr = 0;
3531     		val = 0;
3532     	} else {
3533     		ptr = brdp->membase + (offset % ECP_EIPAGESIZE);
3534     		if (offset < ECP_EIPAGESIZE)
3535     			val = ECP_EIENABLE;
3536     		else
3537     			val = ECP_EIENABLE | 0x40;
3538     	}
3539     	outb(val, (brdp->iobase + ECP_EICONFR));
3540     	return(ptr);
3541     }
3542     
3543     /*****************************************************************************/
3544     
3545     static void stli_ecpeireset(stlibrd_t *brdp)
3546     {	
3547     	outb(ECP_EISTOP, (brdp->iobase + ECP_EICONFR));
3548     	udelay(10);
3549     	outb(ECP_EIDISABLE, (brdp->iobase + ECP_EICONFR));
3550     	udelay(500);
3551     }
3552     
3553     /*****************************************************************************/
3554     
3555     /*
3556      *	The following set of functions act on ECP MCA boards.
3557      */
3558     
3559     static void stli_ecpmcenable(stlibrd_t *brdp)
3560     {	
3561     	outb(ECP_MCENABLE, (brdp->iobase + ECP_MCCONFR));
3562     }
3563     
3564     /*****************************************************************************/
3565     
3566     static void stli_ecpmcdisable(stlibrd_t *brdp)
3567     {	
3568     	outb(ECP_MCDISABLE, (brdp->iobase + ECP_MCCONFR));
3569     }
3570     
3571     /*****************************************************************************/
3572     
3573     static char *stli_ecpmcgetmemptr(stlibrd_t *brdp, unsigned long offset, int line)
3574     {	
3575     	void		*ptr;
3576     	unsigned char	val;
3577     
3578     	if (offset > brdp->memsize) {
3579     		printk(KERN_ERR "STALLION: shared memory pointer=%x out of "
3580     				"range at line=%d(%d), brd=%d\n",
3581     			(int) offset, line, __LINE__, brdp->brdnr);
3582     		ptr = 0;
3583     		val = 0;
3584     	} else {
3585     		ptr = brdp->membase + (offset % ECP_MCPAGESIZE);
3586     		val = ((unsigned char) (offset / ECP_MCPAGESIZE)) | ECP_MCENABLE;
3587     	}
3588     	outb(val, (brdp->iobase + ECP_MCCONFR));
3589     	return(ptr);
3590     }
3591     
3592     /*****************************************************************************/
3593     
3594     static void stli_ecpmcreset(stlibrd_t *brdp)
3595     {	
3596     	outb(ECP_MCSTOP, (brdp->iobase + ECP_MCCONFR));
3597     	udelay(10);
3598     	outb(ECP_MCDISABLE, (brdp->iobase + ECP_MCCONFR));
3599     	udelay(500);
3600     }
3601     
3602     /*****************************************************************************/
3603     
3604     /*
3605      *	The following set of functions act on ECP PCI boards.
3606      */
3607     
3608     static void stli_ecppciinit(stlibrd_t *brdp)
3609     {
3610     #if DEBUG
3611     	printk(KERN_DEBUG "stli_ecppciinit(brdp=%x)\n", (int) brdp);
3612     #endif
3613     
3614     	outb(ECP_PCISTOP, (brdp->iobase + ECP_PCICONFR));
3615     	udelay(10);
3616     	outb(0, (brdp->iobase + ECP_PCICONFR));
3617     	udelay(500);
3618     }
3619     
3620     /*****************************************************************************/
3621     
3622     static char *stli_ecppcigetmemptr(stlibrd_t *brdp, unsigned long offset, int line)
3623     {	
3624     	void		*ptr;
3625     	unsigned char	val;
3626     
3627     #if DEBUG
3628     	printk(KERN_DEBUG "stli_ecppcigetmemptr(brdp=%x,offset=%x,line=%d)\n",
3629     		(int) brdp, (int) offset, line);
3630     #endif
3631     
3632     	if (offset > brdp->memsize) {
3633     		printk(KERN_ERR "STALLION: shared memory pointer=%x out of "
3634     				"range at line=%d(%d), board=%d\n",
3635     				(int) offset, line, __LINE__, brdp->brdnr);
3636     		ptr = 0;
3637     		val = 0;
3638     	} else {
3639     		ptr = brdp->membase + (offset % ECP_PCIPAGESIZE);
3640     		val = (offset / ECP_PCIPAGESIZE) << 1;
3641     	}
3642     	outb(val, (brdp->iobase + ECP_PCICONFR));
3643     	return(ptr);
3644     }
3645     
3646     /*****************************************************************************/
3647     
3648     static void stli_ecppcireset(stlibrd_t *brdp)
3649     {	
3650     	outb(ECP_PCISTOP, (brdp->iobase + ECP_PCICONFR));
3651     	udelay(10);
3652     	outb(0, (brdp->iobase + ECP_PCICONFR));
3653     	udelay(500);
3654     }
3655     
3656     /*****************************************************************************/
3657     
3658     /*
3659      *	The following routines act on ONboards.
3660      */
3661     
3662     static void stli_onbinit(stlibrd_t *brdp)
3663     {
3664     	unsigned long	memconf;
3665     
3666     #if DEBUG
3667     	printk(KERN_DEBUG "stli_onbinit(brdp=%d)\n", (int) brdp);
3668     #endif
3669     
3670     	outb(ONB_ATSTOP, (brdp->iobase + ONB_ATCONFR));
3671     	udelay(10);
3672     	outb(ONB_ATDISABLE, (brdp->iobase + ONB_ATCONFR));
3673     	mdelay(1000);
3674     
3675     	memconf = (brdp->memaddr & ONB_ATADDRMASK) >> ONB_ATADDRSHFT;
3676     	outb(memconf, (brdp->iobase + ONB_ATMEMAR));
3677     	outb(0x1, brdp->iobase);
3678     	mdelay(1);
3679     }
3680     
3681     /*****************************************************************************/
3682     
3683     static void stli_onbenable(stlibrd_t *brdp)
3684     {	
3685     #if DEBUG
3686     	printk(KERN_DEBUG "stli_onbenable(brdp=%x)\n", (int) brdp);
3687     #endif
3688     	outb((brdp->enabval | ONB_ATENABLE), (brdp->iobase + ONB_ATCONFR));
3689     }
3690     
3691     /*****************************************************************************/
3692     
3693     static void stli_onbdisable(stlibrd_t *brdp)
3694     {	
3695     #if DEBUG
3696     	printk(KERN_DEBUG "stli_onbdisable(brdp=%x)\n", (int) brdp);
3697     #endif
3698     	outb((brdp->enabval | ONB_ATDISABLE), (brdp->iobase + ONB_ATCONFR));
3699     }
3700     
3701     /*****************************************************************************/
3702     
3703     static char *stli_onbgetmemptr(stlibrd_t *brdp, unsigned long offset, int line)
3704     {	
3705     	void	*ptr;
3706     
3707     #if DEBUG
3708     	printk(KERN_DEBUG "stli_onbgetmemptr(brdp=%x,offset=%x)\n", (int) brdp,
3709     		(int) offset);
3710     #endif
3711     
3712     	if (offset > brdp->memsize) {
3713     		printk(KERN_ERR "STALLION: shared memory pointer=%x out of "
3714     				"range at line=%d(%d), brd=%d\n",
3715     				(int) offset, line, __LINE__, brdp->brdnr);
3716     		ptr = 0;
3717     	} else {
3718     		ptr = brdp->membase + (offset % ONB_ATPAGESIZE);
3719     	}
3720     	return(ptr);
3721     }
3722     
3723     /*****************************************************************************/
3724     
3725     static void stli_onbreset(stlibrd_t *brdp)
3726     {	
3727     
3728     #if DEBUG
3729     	printk(KERN_DEBUG "stli_onbreset(brdp=%x)\n", (int) brdp);
3730     #endif
3731     
3732     	outb(ONB_ATSTOP, (brdp->iobase + ONB_ATCONFR));
3733     	udelay(10);
3734     	outb(ONB_ATDISABLE, (brdp->iobase + ONB_ATCONFR));
3735     	mdelay(1000);
3736     }
3737     
3738     /*****************************************************************************/
3739     
3740     /*
3741      *	The following routines act on ONboard EISA.
3742      */
3743     
3744     static void stli_onbeinit(stlibrd_t *brdp)
3745     {
3746     	unsigned long	memconf;
3747     
3748     #if DEBUG
3749     	printk(KERN_DEBUG "stli_onbeinit(brdp=%d)\n", (int) brdp);
3750     #endif
3751     
3752     	outb(0x1, (brdp->iobase + ONB_EIBRDENAB));
3753     	outb(ONB_EISTOP, (brdp->iobase + ONB_EICONFR));
3754     	udelay(10);
3755     	outb(ONB_EIDISABLE, (brdp->iobase + ONB_EICONFR));
3756     	mdelay(1000);
3757     
3758     	memconf = (brdp->memaddr & ONB_EIADDRMASKL) >> ONB_EIADDRSHFTL;
3759     	outb(memconf, (brdp->iobase + ONB_EIMEMARL));
3760     	memconf = (brdp->memaddr & ONB_EIADDRMASKH) >> ONB_EIADDRSHFTH;
3761     	outb(memconf, (brdp->iobase + ONB_EIMEMARH));
3762     	outb(0x1, brdp->iobase);
3763     	mdelay(1);
3764     }
3765     
3766     /*****************************************************************************/
3767     
3768     static void stli_onbeenable(stlibrd_t *brdp)
3769     {	
3770     #if DEBUG
3771     	printk(KERN_DEBUG "stli_onbeenable(brdp=%x)\n", (int) brdp);
3772     #endif
3773     	outb(ONB_EIENABLE, (brdp->iobase + ONB_EICONFR));
3774     }
3775     
3776     /*****************************************************************************/
3777     
3778     static void stli_onbedisable(stlibrd_t *brdp)
3779     {	
3780     #if DEBUG
3781     	printk(KERN_DEBUG "stli_onbedisable(brdp=%x)\n", (int) brdp);
3782     #endif
3783     	outb(ONB_EIDISABLE, (brdp->iobase + ONB_EICONFR));
3784     }
3785     
3786     /*****************************************************************************/
3787     
3788     static char *stli_onbegetmemptr(stlibrd_t *brdp, unsigned long offset, int line)
3789     {	
3790     	void		*ptr;
3791     	unsigned char	val;
3792     
3793     #if DEBUG
3794     	printk(KERN_DEBUG "stli_onbegetmemptr(brdp=%x,offset=%x,line=%d)\n",
3795     		(int) brdp, (int) offset, line);
3796     #endif
3797     
3798     	if (offset > brdp->memsize) {
3799     		printk(KERN_ERR "STALLION: shared memory pointer=%x out of "
3800     				"range at line=%d(%d), brd=%d\n",
3801     			(int) offset, line, __LINE__, brdp->brdnr);
3802     		ptr = 0;
3803     		val = 0;
3804     	} else {
3805     		ptr = brdp->membase + (offset % ONB_EIPAGESIZE);
3806     		if (offset < ONB_EIPAGESIZE)
3807     			val = ONB_EIENABLE;
3808     		else
3809     			val = ONB_EIENABLE | 0x40;
3810     	}
3811     	outb(val, (brdp->iobase + ONB_EICONFR));
3812     	return(ptr);
3813     }
3814     
3815     /*****************************************************************************/
3816     
3817     static void stli_onbereset(stlibrd_t *brdp)
3818     {	
3819     
3820     #if DEBUG
3821     	printk(KERN_ERR "stli_onbereset(brdp=%x)\n", (int) brdp);
3822     #endif
3823     
3824     	outb(ONB_EISTOP, (brdp->iobase + ONB_EICONFR));
3825     	udelay(10);
3826     	outb(ONB_EIDISABLE, (brdp->iobase + ONB_EICONFR));
3827     	mdelay(1000);
3828     }
3829     
3830     /*****************************************************************************/
3831     
3832     /*
3833      *	The following routines act on Brumby boards.
3834      */
3835     
3836     static void stli_bbyinit(stlibrd_t *brdp)
3837     {
3838     
3839     #if DEBUG
3840     	printk(KERN_ERR "stli_bbyinit(brdp=%d)\n", (int) brdp);
3841     #endif
3842     
3843     	outb(BBY_ATSTOP, (brdp->iobase + BBY_ATCONFR));
3844     	udelay(10);
3845     	outb(0, (brdp->iobase + BBY_ATCONFR));
3846     	mdelay(1000);
3847     	outb(0x1, brdp->iobase);
3848     	mdelay(1);
3849     }
3850     
3851     /*****************************************************************************/
3852     
3853     static char *stli_bbygetmemptr(stlibrd_t *brdp, unsigned long offset, int line)
3854     {	
3855     	void		*ptr;
3856     	unsigned char	val;
3857     
3858     #if DEBUG
3859     	printk(KERN_ERR "stli_bbygetmemptr(brdp=%x,offset=%x)\n", (int) brdp,
3860     		(int) offset);
3861     #endif
3862     
3863     	if (offset > brdp->memsize) {
3864     		printk(KERN_ERR "STALLION: shared memory pointer=%x out of "
3865     				"range at line=%d(%d), brd=%d\n",
3866     				(int) offset, line, __LINE__, brdp->brdnr);
3867     		ptr = 0;
3868     		val = 0;
3869     	} else {
3870     		ptr = brdp->membase + (offset % BBY_PAGESIZE);
3871     		val = (unsigned char) (offset / BBY_PAGESIZE);
3872     	}
3873     	outb(val, (brdp->iobase + BBY_ATCONFR));
3874     	return(ptr);
3875     }
3876     
3877     /*****************************************************************************/
3878     
3879     static void stli_bbyreset(stlibrd_t *brdp)
3880     {	
3881     
3882     #if DEBUG
3883     	printk(KERN_DEBUG "stli_bbyreset(brdp=%x)\n", (int) brdp);
3884     #endif
3885     
3886     	outb(BBY_ATSTOP, (brdp->iobase + BBY_ATCONFR));
3887     	udelay(10);
3888     	outb(0, (brdp->iobase + BBY_ATCONFR));
3889     	mdelay(1000);
3890     }
3891     
3892     /*****************************************************************************/
3893     
3894     /*
3895      *	The following routines act on original old Stallion boards.
3896      */
3897     
3898     static void stli_stalinit(stlibrd_t *brdp)
3899     {
3900     
3901     #if DEBUG
3902     	printk(KERN_DEBUG "stli_stalinit(brdp=%d)\n", (int) brdp);
3903     #endif
3904     
3905     	outb(0x1, brdp->iobase);
3906     	mdelay(1000);
3907     }
3908     
3909     /*****************************************************************************/
3910     
3911     static char *stli_stalgetmemptr(stlibrd_t *brdp, unsigned long offset, int line)
3912     {	
3913     	void	*ptr;
3914     
3915     #if DEBUG
3916     	printk(KERN_DEBUG "stli_stalgetmemptr(brdp=%x,offset=%x)\n", (int) brdp,
3917     		(int) offset);
3918     #endif
3919     
3920     	if (offset > brdp->memsize) {
3921     		printk(KERN_ERR "STALLION: shared memory pointer=%x out of "
3922     				"range at line=%d(%d), brd=%d\n",
3923     				(int) offset, line, __LINE__, brdp->brdnr);
3924     		ptr = 0;
3925     	} else {
3926     		ptr = brdp->membase + (offset % STAL_PAGESIZE);
3927     	}
3928     	return(ptr);
3929     }
3930     
3931     /*****************************************************************************/
3932     
3933     static void stli_stalreset(stlibrd_t *brdp)
3934     {	
3935     	volatile unsigned long	*vecp;
3936     
3937     #if DEBUG
3938     	printk(KERN_DEBUG "stli_stalreset(brdp=%x)\n", (int) brdp);
3939     #endif
3940     
3941     	vecp = (volatile unsigned long *) (brdp->membase + 0x30);
3942     	*vecp = 0xffff0000;
3943     	outb(0, brdp->iobase);
3944     	mdelay(1000);
3945     }
3946     
3947     /*****************************************************************************/
3948     
3949     /*
3950      *	Try to find an ECP board and initialize it. This handles only ECP
3951      *	board types.
3952      */
3953     
3954     static inline int stli_initecp(stlibrd_t *brdp)
3955     {
3956     	cdkecpsig_t	sig;
3957     	cdkecpsig_t	*sigsp;
3958     	unsigned int	status, nxtid;
3959     	char		*name;
3960     	int		panelnr, nrports;
3961     
3962     #if DEBUG
3963     	printk(KERN_DEBUG "stli_initecp(brdp=%x)\n", (int) brdp);
3964     #endif
3965     
3966     /*
3967      *	Do a basic sanity check on the IO and memory addresses.
3968      */
3969     	if ((brdp->iobase == 0) || (brdp->memaddr == 0))
3970     		return(-ENODEV);
3971     
3972     	brdp->iosize = ECP_IOSIZE;
3973     	if (check_region(brdp->iobase, brdp->iosize))
3974     		printk(KERN_ERR "STALLION: Warning, board %d I/O address %x "
3975     				"conflicts with another device\n",
3976     				brdp->brdnr, brdp->iobase);
3977     
3978     /*
3979      *	Based on the specific board type setup the common vars to access
3980      *	and enable shared memory. Set all board specific information now
3981      *	as well.
3982      */
3983     	switch (brdp->brdtype) {
3984     	case BRD_ECP:
3985     		brdp->membase = (void *) brdp->memaddr;
3986     		brdp->memsize = ECP_MEMSIZE;
3987     		brdp->pagesize = ECP_ATPAGESIZE;
3988     		brdp->init = stli_ecpinit;
3989     		brdp->enable = stli_ecpenable;
3990     		brdp->reenable = stli_ecpenable;
3991     		brdp->disable = stli_ecpdisable;
3992     		brdp->getmemptr = stli_ecpgetmemptr;
3993     		brdp->intr = stli_ecpintr;
3994     		brdp->reset = stli_ecpreset;
3995     		name = "serial(EC8/64)";
3996     		break;
3997     
3998     	case BRD_ECPE:
3999     		brdp->membase = (void *) brdp->memaddr;
4000     		brdp->memsize = ECP_MEMSIZE;
4001     		brdp->pagesize = ECP_EIPAGESIZE;
4002     		brdp->init = stli_ecpeiinit;
4003     		brdp->enable = stli_ecpeienable;
4004     		brdp->reenable = stli_ecpeienable;
4005     		brdp->disable = stli_ecpeidisable;
4006     		brdp->getmemptr = stli_ecpeigetmemptr;
4007     		brdp->intr = stli_ecpintr;
4008     		brdp->reset = stli_ecpeireset;
4009     		name = "serial(EC8/64-EI)";
4010     		break;
4011     
4012     	case BRD_ECPMC:
4013     		brdp->membase = (void *) brdp->memaddr;
4014     		brdp->memsize = ECP_MEMSIZE;
4015     		brdp->pagesize = ECP_MCPAGESIZE;
4016     		brdp->init = NULL;
4017     		brdp->enable = stli_ecpmcenable;
4018     		brdp->reenable = stli_ecpmcenable;
4019     		brdp->disable = stli_ecpmcdisable;
4020     		brdp->getmemptr = stli_ecpmcgetmemptr;
4021     		brdp->intr = stli_ecpintr;
4022     		brdp->reset = stli_ecpmcreset;
4023     		name = "serial(EC8/64-MCA)";
4024     		break;
4025     
4026     	case BRD_ECPPCI:
4027     		brdp->membase = (void *) brdp->memaddr;
4028     		brdp->memsize = ECP_PCIMEMSIZE;
4029     		brdp->pagesize = ECP_PCIPAGESIZE;
4030     		brdp->init = stli_ecppciinit;
4031     		brdp->enable = NULL;
4032     		brdp->reenable = NULL;
4033     		brdp->disable = NULL;
4034     		brdp->getmemptr = stli_ecppcigetmemptr;
4035     		brdp->intr = stli_ecpintr;
4036     		brdp->reset = stli_ecppcireset;
4037     		name = "serial(EC/RA-PCI)";
4038     		break;
4039     
4040     	default:
4041     		return(-EINVAL);
4042     	}
4043     
4044     /*
4045      *	The per-board operations structure is all set up, so now let's go
4046      *	and get the board operational. Firstly initialize board configuration
4047      *	registers. Set the memory mapping info so we can get at the boards
4048      *	shared memory.
4049      */
4050     	EBRDINIT(brdp);
4051     
4052     	brdp->membase = ioremap(brdp->memaddr, brdp->memsize);
4053     	if (brdp->membase == (void *) NULL)
4054     		return(-ENOMEM);
4055     
4056     /*
4057      *	Now that all specific code is set up, enable the shared memory and
4058      *	look for the a signature area that will tell us exactly what board
4059      *	this is, and what it is connected to it.
4060      */
4061     	EBRDENABLE(brdp);
4062     	sigsp = (cdkecpsig_t *) EBRDGETMEMPTR(brdp, CDK_SIGADDR);
4063     	memcpy(&sig, sigsp, sizeof(cdkecpsig_t));
4064     	EBRDDISABLE(brdp);
4065     
4066     #if 0
4067     	printk("%s(%d): sig-> magic=%x rom=%x panel=%x,%x,%x,%x,%x,%x,%x,%x\n",
4068     		__FILE__, __LINE__, (int) sig.magic, sig.romver, sig.panelid[0],
4069     		(int) sig.panelid[1], (int) sig.panelid[2],
4070     		(int) sig.panelid[3], (int) sig.panelid[4],
4071     		(int) sig.panelid[5], (int) sig.panelid[6],
4072     		(int) sig.panelid[7]);
4073     #endif
4074     
4075     	if (sig.magic != ECP_MAGIC)
4076     		return(-ENODEV);
4077     
4078     /*
4079      *	Scan through the signature looking at the panels connected to the
4080      *	board. Calculate the total number of ports as we go.
4081      */
4082     	for (panelnr = 0, nxtid = 0; (panelnr < STL_MAXPANELS); panelnr++) {
4083     		status = sig.panelid[nxtid];
4084     		if ((status & ECH_PNLIDMASK) != nxtid)
4085     			break;
4086     
4087     		brdp->panelids[panelnr] = status;
4088     		nrports = (status & ECH_PNL16PORT) ? 16 : 8;
4089     		if ((nrports == 16) && ((status & ECH_PNLXPID) == 0))
4090     			nxtid++;
4091     		brdp->panels[panelnr] = nrports;
4092     		brdp->nrports += nrports;
4093     		nxtid++;
4094     		brdp->nrpanels++;
4095     	}
4096     
4097     	request_region(brdp->iobase, brdp->iosize, name);
4098     	brdp->state |= BST_FOUND;
4099     	return(0);
4100     }
4101     
4102     /*****************************************************************************/
4103     
4104     /*
4105      *	Try to find an ONboard, Brumby or Stallion board and initialize it.
4106      *	This handles only these board types.
4107      */
4108     
4109     static inline int stli_initonb(stlibrd_t *brdp)
4110     {
4111     	cdkonbsig_t	sig;
4112     	cdkonbsig_t	*sigsp;
4113     	char		*name;
4114     	int		i;
4115     
4116     #if DEBUG
4117     	printk(KERN_DEBUG "stli_initonb(brdp=%x)\n", (int) brdp);
4118     #endif
4119     
4120     /*
4121      *	Do a basic sanity check on the IO and memory addresses.
4122      */
4123     	if ((brdp->iobase == 0) || (brdp->memaddr == 0))
4124     		return(-ENODEV);
4125     
4126     	brdp->iosize = ONB_IOSIZE;
4127     	if (check_region(brdp->iobase, brdp->iosize))
4128     		printk(KERN_ERR "STALLION: Warning, board %d I/O address %x "
4129     				"conflicts with another device\n",
4130     				brdp->brdnr, brdp->iobase);
4131     
4132     /*
4133      *	Based on the specific board type setup the common vars to access
4134      *	and enable shared memory. Set all board specific information now
4135      *	as well.
4136      */
4137     	switch (brdp->brdtype) {
4138     	case BRD_ONBOARD:
4139     	case BRD_ONBOARD32:
4140     	case BRD_ONBOARD2:
4141     	case BRD_ONBOARD2_32:
4142     	case BRD_ONBOARDRS:
4143     		brdp->membase = (void *) brdp->memaddr;
4144     		brdp->memsize = ONB_MEMSIZE;
4145     		brdp->pagesize = ONB_ATPAGESIZE;
4146     		brdp->init = stli_onbinit;
4147     		brdp->enable = stli_onbenable;
4148     		brdp->reenable = stli_onbenable;
4149     		brdp->disable = stli_onbdisable;
4150     		brdp->getmemptr = stli_onbgetmemptr;
4151     		brdp->intr = stli_ecpintr;
4152     		brdp->reset = stli_onbreset;
4153     		if (brdp->memaddr > 0x100000)
4154     			brdp->enabval = ONB_MEMENABHI;
4155     		else
4156     			brdp->enabval = ONB_MEMENABLO;
4157     		name = "serial(ONBoard)";
4158     		break;
4159     
4160     	case BRD_ONBOARDE:
4161     		brdp->membase = (void *) brdp->memaddr;
4162     		brdp->memsize = ONB_EIMEMSIZE;
4163     		brdp->pagesize = ONB_EIPAGESIZE;
4164     		brdp->init = stli_onbeinit;
4165     		brdp->enable = stli_onbeenable;
4166     		brdp->reenable = stli_onbeenable;
4167     		brdp->disable = stli_onbedisable;
4168     		brdp->getmemptr = stli_onbegetmemptr;
4169     		brdp->intr = stli_ecpintr;
4170     		brdp->reset = stli_onbereset;
4171     		name = "serial(ONBoard/E)";
4172     		break;
4173     
4174     	case BRD_BRUMBY4:
4175     	case BRD_BRUMBY8:
4176     	case BRD_BRUMBY16:
4177     		brdp->membase = (void *) brdp->memaddr;
4178     		brdp->memsize = BBY_MEMSIZE;
4179     		brdp->pagesize = BBY_PAGESIZE;
4180     		brdp->init = stli_bbyinit;
4181     		brdp->enable = NULL;
4182     		brdp->reenable = NULL;
4183     		brdp->disable = NULL;
4184     		brdp->getmemptr = stli_bbygetmemptr;
4185     		brdp->intr = stli_ecpintr;
4186     		brdp->reset = stli_bbyreset;
4187     		name = "serial(Brumby)";
4188     		break;
4189     
4190     	case BRD_STALLION:
4191     		brdp->membase = (void *) brdp->memaddr;
4192     		brdp->memsize = STAL_MEMSIZE;
4193     		brdp->pagesize = STAL_PAGESIZE;
4194     		brdp->init = stli_stalinit;
4195     		brdp->enable = NULL;
4196     		brdp->reenable = NULL;
4197     		brdp->disable = NULL;
4198     		brdp->getmemptr = stli_stalgetmemptr;
4199     		brdp->intr = stli_ecpintr;
4200     		brdp->reset = stli_stalreset;
4201     		name = "serial(Stallion)";
4202     		break;
4203     
4204     	default:
4205     		return(-EINVAL);
4206     	}
4207     
4208     /*
4209      *	The per-board operations structure is all set up, so now let's go
4210      *	and get the board operational. Firstly initialize board configuration
4211      *	registers. Set the memory mapping info so we can get at the boards
4212      *	shared memory.
4213      */
4214     	EBRDINIT(brdp);
4215     
4216     	brdp->membase = ioremap(brdp->memaddr, brdp->memsize);
4217     	if (brdp->membase == (void *) NULL)
4218     		return(-ENOMEM);
4219     
4220     /*
4221      *	Now that all specific code is set up, enable the shared memory and
4222      *	look for the a signature area that will tell us exactly what board
4223      *	this is, and how many ports.
4224      */
4225     	EBRDENABLE(brdp);
4226     	sigsp = (cdkonbsig_t *) EBRDGETMEMPTR(brdp, CDK_SIGADDR);
4227     	memcpy(&sig, sigsp, sizeof(cdkonbsig_t));
4228     	EBRDDISABLE(brdp);
4229     
4230     #if 0
4231     	printk("%s(%d): sig-> magic=%x:%x:%x:%x romver=%x amask=%x:%x:%x\n",
4232     		__FILE__, __LINE__, sig.magic0, sig.magic1, sig.magic2,
4233     		sig.magic3, sig.romver, sig.amask0, sig.amask1, sig.amask2);
4234     #endif
4235     
4236     	if ((sig.magic0 != ONB_MAGIC0) || (sig.magic1 != ONB_MAGIC1) ||
4237     	    (sig.magic2 != ONB_MAGIC2) || (sig.magic3 != ONB_MAGIC3))
4238     		return(-ENODEV);
4239     
4240     /*
4241      *	Scan through the signature alive mask and calculate how many ports
4242      *	there are on this board.
4243      */
4244     	brdp->nrpanels = 1;
4245     	if (sig.amask1) {
4246     		brdp->nrports = 32;
4247     	} else {
4248     		for (i = 0; (i < 16); i++) {
4249     			if (((sig.amask0 << i) & 0x8000) == 0)
4250     				break;
4251     		}
4252     		brdp->nrports = i;
4253     	}
4254     	brdp->panels[0] = brdp->nrports;
4255     
4256     	request_region(brdp->iobase, brdp->iosize, name);
4257     	brdp->state |= BST_FOUND;
4258     	return(0);
4259     }
4260     
4261     /*****************************************************************************/
4262     
4263     /*
4264      *	Start up a running board. This routine is only called after the
4265      *	code has been down loaded to the board and is operational. It will
4266      *	read in the memory map, and get the show on the road...
4267      */
4268     
4269     static int stli_startbrd(stlibrd_t *brdp)
4270     {
4271     	volatile cdkhdr_t	*hdrp;
4272     	volatile cdkmem_t	*memp;
4273     	volatile cdkasy_t	*ap;
4274     	unsigned long		flags;
4275     	stliport_t		*portp;
4276     	int			portnr, nrdevs, i, rc;
4277     
4278     #if DEBUG
4279     	printk(KERN_DEBUG "stli_startbrd(brdp=%x)\n", (int) brdp);
4280     #endif
4281     
4282     	rc = 0;
4283     
4284     	save_flags(flags);
4285     	cli();
4286     	EBRDENABLE(brdp);
4287     	hdrp = (volatile cdkhdr_t *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
4288     	nrdevs = hdrp->nrdevs;
4289     
4290     #if 0
4291     	printk("%s(%d): CDK version %d.%d.%d --> "
4292     		"nrdevs=%d memp=%x hostp=%x slavep=%x\n",
4293     		 __FILE__, __LINE__, hdrp->ver_release, hdrp->ver_modification,
4294     		 hdrp->ver_fix, nrdevs, (int) hdrp->memp, (int) hdrp->hostp,
4295     		 (int) hdrp->slavep);
4296     #endif
4297     
4298     	if (nrdevs < (brdp->nrports + 1)) {
4299     		printk(KERN_ERR "STALLION: slave failed to allocate memory for "
4300     				"all devices, devices=%d\n", nrdevs);
4301     		brdp->nrports = nrdevs - 1;
4302     	}
4303     	brdp->nrdevs = nrdevs;
4304     	brdp->hostoffset = hdrp->hostp - CDK_CDKADDR;
4305     	brdp->slaveoffset = hdrp->slavep - CDK_CDKADDR;
4306     	brdp->bitsize = (nrdevs + 7) / 8;
4307     	memp = (volatile cdkmem_t *) hdrp->memp;
4308     	if (((unsigned long) memp) > brdp->memsize) {
4309     		printk(KERN_ERR "STALLION: corrupted shared memory region?\n");
4310     		rc = -EIO;
4311     		goto stli_donestartup;
4312     	}
4313     	memp = (volatile cdkmem_t *) EBRDGETMEMPTR(brdp, (unsigned long) memp);
4314     	if (memp->dtype != TYP_ASYNCTRL) {
4315     		printk(KERN_ERR "STALLION: no slave control device found\n");
4316     		goto stli_donestartup;
4317     	}
4318     	memp++;
4319     
4320     /*
4321      *	Cycle through memory allocation of each port. We are guaranteed to
4322      *	have all ports inside the first page of slave window, so no need to
4323      *	change pages while reading memory map.
4324      */
4325     	for (i = 1, portnr = 0; (i < nrdevs); i++, portnr++, memp++) {
4326     		if (memp->dtype != TYP_ASYNC)
4327     			break;
4328     		portp = brdp->ports[portnr];
4329     		if (portp == (stliport_t *) NULL)
4330     			break;
4331     		portp->devnr = i;
4332     		portp->addr = memp->offset;
4333     		portp->reqbit = (unsigned char) (0x1 << (i * 8 / nrdevs));
4334     		portp->portidx = (unsigned char) (i / 8);
4335     		portp->portbit = (unsigned char) (0x1 << (i % 8));
4336     	}
4337     
4338     	hdrp->slavereq = 0xff;
4339     
4340     /*
4341      *	For each port setup a local copy of the RX and TX buffer offsets
4342      *	and sizes. We do this separate from the above, because we need to
4343      *	move the shared memory page...
4344      */
4345     	for (i = 1, portnr = 0; (i < nrdevs); i++, portnr++) {
4346     		portp = brdp->ports[portnr];
4347     		if (portp == (stliport_t *) NULL)
4348     			break;
4349     		if (portp->addr == 0)
4350     			break;
4351     		ap = (volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr);
4352     		if (ap != (volatile cdkasy_t *) NULL) {
4353     			portp->rxsize = ap->rxq.size;
4354     			portp->txsize = ap->txq.size;
4355     			portp->rxoffset = ap->rxq.offset;
4356     			portp->txoffset = ap->txq.offset;
4357     		}
4358     	}
4359     
4360     stli_donestartup:
4361     	EBRDDISABLE(brdp);
4362     	restore_flags(flags);
4363     
4364     	if (rc == 0)
4365     		brdp->state |= BST_STARTED;
4366     
4367     	if (! stli_timeron) {
4368     		stli_timeron++;
4369     		stli_timerlist.expires = STLI_TIMEOUT;
4370     		add_timer(&stli_timerlist);
4371     	}
4372     
4373     	return(rc);
4374     }
4375     
4376     /*****************************************************************************/
4377     
4378     /*
4379      *	Probe and initialize the specified board.
4380      */
4381     
4382     static int __init stli_brdinit(stlibrd_t *brdp)
4383     {
4384     #if DEBUG
4385     	printk(KERN_DEBUG "stli_brdinit(brdp=%x)\n", (int) brdp);
4386     #endif
4387     
4388     	stli_brds[brdp->brdnr] = brdp;
4389     
4390     	switch (brdp->brdtype) {
4391     	case BRD_ECP:
4392     	case BRD_ECPE:
4393     	case BRD_ECPMC:
4394     	case BRD_ECPPCI:
4395     		stli_initecp(brdp);
4396     		break;
4397     	case BRD_ONBOARD:
4398     	case BRD_ONBOARDE:
4399     	case BRD_ONBOARD2:
4400     	case BRD_ONBOARD32:
4401     	case BRD_ONBOARD2_32:
4402     	case BRD_ONBOARDRS:
4403     	case BRD_BRUMBY4:
4404     	case BRD_BRUMBY8:
4405     	case BRD_BRUMBY16:
4406     	case BRD_STALLION:
4407     		stli_initonb(brdp);
4408     		break;
4409     	case BRD_EASYIO:
4410     	case BRD_ECH:
4411     	case BRD_ECHMC:
4412     	case BRD_ECHPCI:
4413     		printk(KERN_ERR "STALLION: %s board type not supported in "
4414     				"this driver\n", stli_brdnames[brdp->brdtype]);
4415     		return(ENODEV);
4416     	default:
4417     		printk(KERN_ERR "STALLION: board=%d is unknown board "
4418     				"type=%d\n", brdp->brdnr, brdp->brdtype);
4419     		return(ENODEV);
4420     	}
4421     
4422     	if ((brdp->state & BST_FOUND) == 0) {
4423     		printk(KERN_ERR "STALLION: %s board not found, board=%d "
4424     				"io=%x mem=%x\n",
4425     			stli_brdnames[brdp->brdtype], brdp->brdnr,
4426     			brdp->iobase, (int) brdp->memaddr);
4427     		return(ENODEV);
4428     	}
4429     
4430     	stli_initports(brdp);
4431     	printk(KERN_INFO "STALLION: %s found, board=%d io=%x mem=%x "
4432     		"nrpanels=%d nrports=%d\n", stli_brdnames[brdp->brdtype],
4433     		brdp->brdnr, brdp->iobase, (int) brdp->memaddr,
4434     		brdp->nrpanels, brdp->nrports);
4435     	return(0);
4436     }
4437     
4438     /*****************************************************************************/
4439     
4440     /*
4441      *	Probe around trying to find where the EISA boards shared memory
4442      *	might be. This is a bit if hack, but it is the best we can do.
4443      */
4444     
4445     static inline int stli_eisamemprobe(stlibrd_t *brdp)
4446     {
4447     	cdkecpsig_t	ecpsig, *ecpsigp;
4448     	cdkonbsig_t	onbsig, *onbsigp;
4449     	int		i, foundit;
4450     
4451     #if DEBUG
4452     	printk(KERN_DEBUG "stli_eisamemprobe(brdp=%x)\n", (int) brdp);
4453     #endif
4454     
4455     /*
4456      *	First up we reset the board, to get it into a known state. There
4457      *	is only 2 board types here we need to worry about. Don;t use the
4458      *	standard board init routine here, it programs up the shared
4459      *	memory address, and we don't know it yet...
4460      */
4461     	if (brdp->brdtype == BRD_ECPE) {
4462     		outb(0x1, (brdp->iobase + ECP_EIBRDENAB));
4463     		outb(ECP_EISTOP, (brdp->iobase + ECP_EICONFR));
4464     		udelay(10);
4465     		outb(ECP_EIDISABLE, (brdp->iobase + ECP_EICONFR));
4466     		udelay(500);
4467     		stli_ecpeienable(brdp);
4468     	} else if (brdp->brdtype == BRD_ONBOARDE) {
4469     		outb(0x1, (brdp->iobase + ONB_EIBRDENAB));
4470     		outb(ONB_EISTOP, (brdp->iobase + ONB_EICONFR));
4471     		udelay(10);
4472     		outb(ONB_EIDISABLE, (brdp->iobase + ONB_EICONFR));
4473     		mdelay(100);
4474     		outb(0x1, brdp->iobase);
4475     		mdelay(1);
4476     		stli_onbeenable(brdp);
4477     	} else {
4478     		return(-ENODEV);
4479     	}
4480     
4481     	foundit = 0;
4482     	brdp->memsize = ECP_MEMSIZE;
4483     
4484     /*
4485      *	Board shared memory is enabled, so now we have a poke around and
4486      *	see if we can find it.
4487      */
4488     	for (i = 0; (i < stli_eisamempsize); i++) {
4489     		brdp->memaddr = stli_eisamemprobeaddrs[i];
4490     		brdp->membase = (void *) brdp->memaddr;
4491     		brdp->membase = ioremap(brdp->memaddr, brdp->memsize);
4492     		if (brdp->membase == (void *) NULL)
4493     			continue;
4494     
4495     		if (brdp->brdtype == BRD_ECPE) {
4496     			ecpsigp = (cdkecpsig_t *) stli_ecpeigetmemptr(brdp,
4497     				CDK_SIGADDR, __LINE__);
4498     			memcpy(&ecpsig, ecpsigp, sizeof(cdkecpsig_t));
4499     			if (ecpsig.magic == ECP_MAGIC)
4500     				foundit = 1;
4501     		} else {
4502     			onbsigp = (cdkonbsig_t *) stli_onbegetmemptr(brdp,
4503     				CDK_SIGADDR, __LINE__);
4504     			memcpy(&onbsig, onbsigp, sizeof(cdkonbsig_t));
4505     			if ((onbsig.magic0 == ONB_MAGIC0) &&
4506     			    (onbsig.magic1 == ONB_MAGIC1) &&
4507     			    (onbsig.magic2 == ONB_MAGIC2) &&
4508     			    (onbsig.magic3 == ONB_MAGIC3))
4509     				foundit = 1;
4510     		}
4511     
4512     		iounmap(brdp->membase);
4513     		if (foundit)
4514     			break;
4515     	}
4516     
4517     /*
4518      *	Regardless of whether we found the shared memory or not we must
4519      *	disable the region. After that return success or failure.
4520      */
4521     	if (brdp->brdtype == BRD_ECPE)
4522     		stli_ecpeidisable(brdp);
4523     	else
4524     		stli_onbedisable(brdp);
4525     
4526     	if (! foundit) {
4527     		brdp->memaddr = 0;
4528     		brdp->membase = 0;
4529     		printk(KERN_ERR "STALLION: failed to probe shared memory "
4530     				"region for %s in EISA slot=%d\n",
4531     			stli_brdnames[brdp->brdtype], (brdp->iobase >> 12));
4532     		return(-ENODEV);
4533     	}
4534     	return(0);
4535     }
4536     
4537     /*****************************************************************************/
4538     
4539     /*
4540      *	Probe around and try to find any EISA boards in system. The biggest
4541      *	problem here is finding out what memory address is associated with
4542      *	an EISA board after it is found. The registers of the ECPE and
4543      *	ONboardE are not readable - so we can't read them from there. We
4544      *	don't have access to the EISA CMOS (or EISA BIOS) so we don't
4545      *	actually have any way to find out the real value. The best we can
4546      *	do is go probing around in the usual places hoping we can find it.
4547      */
4548     
4549     static inline int stli_findeisabrds()
4550     {
4551     	stlibrd_t	*brdp;
4552     	unsigned int	iobase, eid;
4553     	int		i;
4554     
4555     #if DEBUG
4556     	printk(KERN_DEBUG "stli_findeisabrds()\n");
4557     #endif
4558     
4559     /*
4560      *	Firstly check if this is an EISA system. Do this by probing for
4561      *	the system board EISA ID. If this is not an EISA system then
4562      *	don't bother going any further!
4563      */
4564     	outb(0xff, 0xc80);
4565     	if (inb(0xc80) == 0xff)
4566     		return(0);
4567     
4568     /*
4569      *	Looks like an EISA system, so go searching for EISA boards.
4570      */
4571     	for (iobase = 0x1000; (iobase <= 0xc000); iobase += 0x1000) {
4572     		outb(0xff, (iobase + 0xc80));
4573     		eid = inb(iobase + 0xc80);
4574     		eid |= inb(iobase + 0xc81) << 8;
4575     		if (eid != STL_EISAID)
4576     			continue;
4577     
4578     /*
4579      *		We have found a board. Need to check if this board was
4580      *		statically configured already (just in case!).
4581      */
4582     		for (i = 0; (i < STL_MAXBRDS); i++) {
4583     			brdp = stli_brds[i];
4584     			if (brdp == (stlibrd_t *) NULL)
4585     				continue;
4586     			if (brdp->iobase == iobase)
4587     				break;
4588     		}
4589     		if (i < STL_MAXBRDS)
4590     			continue;
4591     
4592     /*
4593      *		We have found a Stallion board and it is not configured already.
4594      *		Allocate a board structure and initialize it.
4595      */
4596     		if ((brdp = stli_allocbrd()) == (stlibrd_t *) NULL)
4597     			return(-ENOMEM);
4598     		if ((brdp->brdnr = stli_getbrdnr()) < 0)
4599     			return(-ENOMEM);
4600     		eid = inb(iobase + 0xc82);
4601     		if (eid == ECP_EISAID)
4602     			brdp->brdtype = BRD_ECPE;
4603     		else if (eid == ONB_EISAID)
4604     			brdp->brdtype = BRD_ONBOARDE;
4605     		else
4606     			brdp->brdtype = BRD_UNKNOWN;
4607     		brdp->iobase = iobase;
4608     		outb(0x1, (iobase + 0xc84));
4609     		if (stli_eisamemprobe(brdp))
4610     			outb(0, (iobase + 0xc84));
4611     		stli_brdinit(brdp);
4612     	}
4613     
4614     	return(0);
4615     }
4616     
4617     /*****************************************************************************/
4618     
4619     /*
4620      *	Find the next available board number that is free.
4621      */
4622     
4623     static inline int stli_getbrdnr()
4624     {
4625     	int	i;
4626     
4627     	for (i = 0; (i < STL_MAXBRDS); i++) {
4628     		if (stli_brds[i] == (stlibrd_t *) NULL) {
4629     			if (i >= stli_nrbrds)
4630     				stli_nrbrds = i + 1;
4631     			return(i);
4632     		}
4633     	}
4634     	return(-1);
4635     }
4636     
4637     /*****************************************************************************/
4638     
4639     #ifdef	CONFIG_PCI
4640     
4641     /*
4642      *	We have a Stallion board. Allocate a board structure and
4643      *	initialize it. Read its IO and MEMORY resources from PCI
4644      *	configuration space.
4645      */
4646     
4647     static inline int stli_initpcibrd(int brdtype, struct pci_dev *devp)
4648     {
4649     	stlibrd_t	*brdp;
4650     
4651     #if DEBUG
4652     	printk(KERN_DEBUG "stli_initpcibrd(brdtype=%d,busnr=%x,devnr=%x)\n",
4653     		brdtype, dev->bus->number, dev->devfn);
4654     #endif
4655     
4656     	if (pci_enable_device(devp))
4657     		return(-EIO);
4658     	if ((brdp = stli_allocbrd()) == (stlibrd_t *) NULL)
4659     		return(-ENOMEM);
4660     	if ((brdp->brdnr = stli_getbrdnr()) < 0) {
4661     		printk(KERN_INFO "STALLION: too many boards found, "
4662     			"maximum supported %d\n", STL_MAXBRDS);
4663     		return(0);
4664     	}
4665     	brdp->brdtype = brdtype;
4666     
4667     #if DEBUG
4668     	printk(KERN_DEBUG "%s(%d): BAR[]=%lx,%lx,%lx,%lx\n", __FILE__, __LINE__,
4669     		pci_resource_start(devp, 0),
4670     		pci_resource_start(devp, 1),
4671     		pci_resource_start(devp, 2),
4672     		pci_resource_start(devp, 3));
4673     #endif
4674     
4675     /*
4676      *	We have all resources from the board, so lets setup the actual
4677      *	board structure now.
4678      */
4679     	brdp->iobase = pci_resource_start(devp, 3);
4680     	brdp->memaddr = pci_resource_start(devp, 2);
4681     	stli_brdinit(brdp);
4682     
4683     	return(0);
4684     }
4685     
4686     /*****************************************************************************/
4687     
4688     /*
4689      *	Find all Stallion PCI boards that might be installed. Initialize each
4690      *	one as it is found.
4691      */
4692     
4693     static inline int stli_findpcibrds()
4694     {
4695     	struct pci_dev	*dev = NULL;
4696     	int		rc;
4697     
4698     #if DEBUG
4699     	printk("stli_findpcibrds()\n");
4700     #endif
4701     
4702     	if (! pci_present())
4703     		return(0);
4704     
4705     	while ((dev = pci_find_device(PCI_VENDOR_ID_STALLION,
4706     	    PCI_DEVICE_ID_ECRA, dev))) {
4707     		if ((rc = stli_initpcibrd(BRD_ECPPCI, dev)))
4708     			return(rc);
4709     	}
4710     
4711     	return(0);
4712     }
4713     
4714     #endif
4715     
4716     /*****************************************************************************/
4717     
4718     /*
4719      *	Allocate a new board structure. Fill out the basic info in it.
4720      */
4721     
4722     static stlibrd_t *stli_allocbrd()
4723     {
4724     	stlibrd_t	*brdp;
4725     
4726     	brdp = (stlibrd_t *) stli_memalloc(sizeof(stlibrd_t));
4727     	if (brdp == (stlibrd_t *) NULL) {
4728     		printk(KERN_ERR "STALLION: failed to allocate memory "
4729     				"(size=%d)\n", sizeof(stlibrd_t));
4730     		return((stlibrd_t *) NULL);
4731     	}
4732     
4733     	memset(brdp, 0, sizeof(stlibrd_t));
4734     	brdp->magic = STLI_BOARDMAGIC;
4735     	return(brdp);
4736     }
4737     
4738     /*****************************************************************************/
4739     
4740     /*
4741      *	Scan through all the boards in the configuration and see what we
4742      *	can find.
4743      */
4744     
4745     static inline int stli_initbrds()
4746     {
4747     	stlibrd_t	*brdp, *nxtbrdp;
4748     	stlconf_t	*confp;
4749     	int		i, j;
4750     
4751     #if DEBUG
4752     	printk(KERN_DEBUG "stli_initbrds()\n");
4753     #endif
4754     
4755     	if (stli_nrbrds > STL_MAXBRDS) {
4756     		printk(KERN_INFO "STALLION: too many boards in configuration "
4757     			"table, truncating to %d\n", STL_MAXBRDS);
4758     		stli_nrbrds = STL_MAXBRDS;
4759     	}
4760     
4761     /*
4762      *	Firstly scan the list of static boards configured. Allocate
4763      *	resources and initialize the boards as found. If this is a
4764      *	module then let the module args override static configuration.
4765      */
4766     	for (i = 0; (i < stli_nrbrds); i++) {
4767     		confp = &stli_brdconf[i];
4768     #ifdef MODULE
4769     		stli_parsebrd(confp, stli_brdsp[i]);
4770     #endif
4771     		if ((brdp = stli_allocbrd()) == (stlibrd_t *) NULL)
4772     			return(-ENOMEM);
4773     		brdp->brdnr = i;
4774     		brdp->brdtype = confp->brdtype;
4775     		brdp->iobase = confp->ioaddr1;
4776     		brdp->memaddr = confp->memaddr;
4777     		stli_brdinit(brdp);
4778     	}
4779     
4780     /*
4781      *	Static configuration table done, so now use dynamic methods to
4782      *	see if any more boards should be configured.
4783      */
4784     #ifdef MODULE
4785     	stli_argbrds();
4786     #endif
4787     	if (stli_eisaprobe)
4788     		stli_findeisabrds();
4789     #ifdef CONFIG_PCI
4790     	stli_findpcibrds();
4791     #endif
4792     
4793     /*
4794      *	All found boards are initialized. Now for a little optimization, if
4795      *	no boards are sharing the "shared memory" regions then we can just
4796      *	leave them all enabled. This is in fact the usual case.
4797      */
4798     	stli_shared = 0;
4799     	if (stli_nrbrds > 1) {
4800     		for (i = 0; (i < stli_nrbrds); i++) {
4801     			brdp = stli_brds[i];
4802     			if (brdp == (stlibrd_t *) NULL)
4803     				continue;
4804     			for (j = i + 1; (j < stli_nrbrds); j++) {
4805     				nxtbrdp = stli_brds[j];
4806     				if (nxtbrdp == (stlibrd_t *) NULL)
4807     					continue;
4808     				if ((brdp->membase >= nxtbrdp->membase) &&
4809     				    (brdp->membase <= (nxtbrdp->membase +
4810     				    nxtbrdp->memsize - 1))) {
4811     					stli_shared++;
4812     					break;
4813     				}
4814     			}
4815     		}
4816     	}
4817     
4818     	if (stli_shared == 0) {
4819     		for (i = 0; (i < stli_nrbrds); i++) {
4820     			brdp = stli_brds[i];
4821     			if (brdp == (stlibrd_t *) NULL)
4822     				continue;
4823     			if (brdp->state & BST_FOUND) {
4824     				EBRDENABLE(brdp);
4825     				brdp->enable = NULL;
4826     				brdp->disable = NULL;
4827     			}
4828     		}
4829     	}
4830     
4831     	return(0);
4832     }
4833     
4834     /*****************************************************************************/
4835     
4836     /*
4837      *	Code to handle an "staliomem" read operation. This device is the 
4838      *	contents of the board shared memory. It is used for down loading
4839      *	the slave image (and debugging :-)
4840      */
4841     
4842     static ssize_t stli_memread(struct file *fp, char *buf, size_t count, loff_t *offp)
4843     {
4844     	unsigned long	flags;
4845     	void		*memptr;
4846     	stlibrd_t	*brdp;
4847     	int		brdnr, size, n;
4848     
4849     #if DEBUG
4850     	printk(KERN_DEBUG "stli_memread(fp=%x,buf=%x,count=%x,offp=%x)\n",
4851     			(int) fp, (int) buf, count, (int) offp);
4852     #endif
4853     
4854     	brdnr = MINOR(fp->f_dentry->d_inode->i_rdev);
4855     	if (brdnr >= stli_nrbrds)
4856     		return(-ENODEV);
4857     	brdp = stli_brds[brdnr];
4858     	if (brdp == (stlibrd_t *) NULL)
4859     		return(-ENODEV);
4860     	if (brdp->state == 0)
4861     		return(-ENODEV);
4862     	if (fp->f_pos >= brdp->memsize)
4863     		return(0);
4864     
4865     	size = MIN(count, (brdp->memsize - fp->f_pos));
4866     
4867     	save_flags(flags);
4868     	cli();
4869     	EBRDENABLE(brdp);
4870     	while (size > 0) {
4871     		memptr = (void *) EBRDGETMEMPTR(brdp, fp->f_pos);
4872     		n = MIN(size, (brdp->pagesize - (((unsigned long) fp->f_pos) % brdp->pagesize)));
4873     		copy_to_user(buf, memptr, n);
4874     		fp->f_pos += n;
4875     		buf += n;
4876     		size -= n;
4877     	}
4878     	EBRDDISABLE(brdp);
4879     	restore_flags(flags);
4880     
4881     	return(count);
4882     }
4883     
4884     /*****************************************************************************/
4885     
4886     /*
4887      *	Code to handle an "staliomem" write operation. This device is the 
4888      *	contents of the board shared memory. It is used for down loading
4889      *	the slave image (and debugging :-)
4890      */
4891     
4892     static ssize_t stli_memwrite(struct file *fp, const char *buf, size_t count, loff_t *offp)
4893     {
4894     	unsigned long	flags;
4895     	void		*memptr;
4896     	stlibrd_t	*brdp;
4897     	char		*chbuf;
4898     	int		brdnr, size, n;
4899     
4900     #if DEBUG
4901     	printk(KERN_DEBUG "stli_memwrite(fp=%x,buf=%x,count=%x,offp=%x)\n",
4902     			(int) fp, (int) buf, count, (int) offp);
4903     #endif
4904     
4905     	brdnr = MINOR(fp->f_dentry->d_inode->i_rdev);
4906     	if (brdnr >= stli_nrbrds)
4907     		return(-ENODEV);
4908     	brdp = stli_brds[brdnr];
4909     	if (brdp == (stlibrd_t *) NULL)
4910     		return(-ENODEV);
4911     	if (brdp->state == 0)
4912     		return(-ENODEV);
4913     	if (fp->f_pos >= brdp->memsize)
4914     		return(0);
4915     
4916     	chbuf = (char *) buf;
4917     	size = MIN(count, (brdp->memsize - fp->f_pos));
4918     
4919     	save_flags(flags);
4920     	cli();
4921     	EBRDENABLE(brdp);
4922     	while (size > 0) {
4923     		memptr = (void *) EBRDGETMEMPTR(brdp, fp->f_pos);
4924     		n = MIN(size, (brdp->pagesize - (((unsigned long) fp->f_pos) % brdp->pagesize)));
4925     		copy_from_user(memptr, chbuf, n);
4926     		fp->f_pos += n;
4927     		chbuf += n;
4928     		size -= n;
4929     	}
4930     	EBRDDISABLE(brdp);
4931     	restore_flags(flags);
4932     
4933     	return(count);
4934     }
4935     
4936     /*****************************************************************************/
4937     
4938     /*
4939      *	Return the board stats structure to user app.
4940      */
4941     
4942     static int stli_getbrdstats(combrd_t *bp)
4943     {
4944     	stlibrd_t	*brdp;
4945     	int		i;
4946     
4947     	if (copy_from_user(&stli_brdstats, bp, sizeof(combrd_t)))
4948     		return -EFAULT;
4949     	if (stli_brdstats.brd >= STL_MAXBRDS)
4950     		return(-ENODEV);
4951     	brdp = stli_brds[stli_brdstats.brd];
4952     	if (brdp == (stlibrd_t *) NULL)
4953     		return(-ENODEV);
4954     
4955     	memset(&stli_brdstats, 0, sizeof(combrd_t));
4956     	stli_brdstats.brd = brdp->brdnr;
4957     	stli_brdstats.type = brdp->brdtype;
4958     	stli_brdstats.hwid = 0;
4959     	stli_brdstats.state = brdp->state;
4960     	stli_brdstats.ioaddr = brdp->iobase;
4961     	stli_brdstats.memaddr = brdp->memaddr;
4962     	stli_brdstats.nrpanels = brdp->nrpanels;
4963     	stli_brdstats.nrports = brdp->nrports;
4964     	for (i = 0; (i < brdp->nrpanels); i++) {
4965     		stli_brdstats.panels[i].panel = i;
4966     		stli_brdstats.panels[i].hwid = brdp->panelids[i];
4967     		stli_brdstats.panels[i].nrports = brdp->panels[i];
4968     	}
4969     
4970     	if (copy_to_user(bp, &stli_brdstats, sizeof(combrd_t)))
4971     		return -EFAULT;
4972     	return(0);
4973     }
4974     
4975     /*****************************************************************************/
4976     
4977     /*
4978      *	Resolve the referenced port number into a port struct pointer.
4979      */
4980     
4981     static stliport_t *stli_getport(int brdnr, int panelnr, int portnr)
4982     {
4983     	stlibrd_t	*brdp;
4984     	int		i;
4985     
4986     	if ((brdnr < 0) || (brdnr >= STL_MAXBRDS))
4987     		return((stliport_t *) NULL);
4988     	brdp = stli_brds[brdnr];
4989     	if (brdp == (stlibrd_t *) NULL)
4990     		return((stliport_t *) NULL);
4991     	for (i = 0; (i < panelnr); i++)
4992     		portnr += brdp->panels[i];
4993     	if ((portnr < 0) || (portnr >= brdp->nrports))
4994     		return((stliport_t *) NULL);
4995     	return(brdp->ports[portnr]);
4996     }
4997     
4998     /*****************************************************************************/
4999     
5000     /*
5001      *	Return the port stats structure to user app. A NULL port struct
5002      *	pointer passed in means that we need to find out from the app
5003      *	what port to get stats for (used through board control device).
5004      */
5005     
5006     static int stli_portcmdstats(stliport_t *portp)
5007     {
5008     	unsigned long	flags;
5009     	stlibrd_t	*brdp;
5010     	int		rc;
5011     
5012     	memset(&stli_comstats, 0, sizeof(comstats_t));
5013     
5014     	if (portp == (stliport_t *) NULL)
5015     		return(-ENODEV);
5016     	brdp = stli_brds[portp->brdnr];
5017     	if (brdp == (stlibrd_t *) NULL)
5018     		return(-ENODEV);
5019     
5020     	if (brdp->state & BST_STARTED) {
5021     		if ((rc = stli_cmdwait(brdp, portp, A_GETSTATS,
5022     		    &stli_cdkstats, sizeof(asystats_t), 1)) < 0)
5023     			return(rc);
5024     	} else {
5025     		memset(&stli_cdkstats, 0, sizeof(asystats_t));
5026     	}
5027     
5028     	stli_comstats.brd = portp->brdnr;
5029     	stli_comstats.panel = portp->panelnr;
5030     	stli_comstats.port = portp->portnr;
5031     	stli_comstats.state = portp->state;
5032     	stli_comstats.flags = portp->flags;
5033     
5034     	save_flags(flags);
5035     	cli();
5036     	if (portp->tty != (struct tty_struct *) NULL) {
5037     		if (portp->tty->driver_data == portp) {
5038     			stli_comstats.ttystate = portp->tty->flags;
5039     			stli_comstats.rxbuffered = portp->tty->flip.count;
5040     			if (portp->tty->termios != (struct termios *) NULL) {
5041     				stli_comstats.cflags = portp->tty->termios->c_cflag;
5042     				stli_comstats.iflags = portp->tty->termios->c_iflag;
5043     				stli_comstats.oflags = portp->tty->termios->c_oflag;
5044     				stli_comstats.lflags = portp->tty->termios->c_lflag;
5045     			}
5046     		}
5047     	}
5048     	restore_flags(flags);
5049     
5050     	stli_comstats.txtotal = stli_cdkstats.txchars;
5051     	stli_comstats.rxtotal = stli_cdkstats.rxchars + stli_cdkstats.ringover;
5052     	stli_comstats.txbuffered = stli_cdkstats.txringq;
5053     	stli_comstats.rxbuffered += stli_cdkstats.rxringq;
5054     	stli_comstats.rxoverrun = stli_cdkstats.overruns;
5055     	stli_comstats.rxparity = stli_cdkstats.parity;
5056     	stli_comstats.rxframing = stli_cdkstats.framing;
5057     	stli_comstats.rxlost = stli_cdkstats.ringover;
5058     	stli_comstats.rxbreaks = stli_cdkstats.rxbreaks;
5059     	stli_comstats.txbreaks = stli_cdkstats.txbreaks;
5060     	stli_comstats.txxon = stli_cdkstats.txstart;
5061     	stli_comstats.txxoff = stli_cdkstats.txstop;
5062     	stli_comstats.rxxon = stli_cdkstats.rxstart;
5063     	stli_comstats.rxxoff = stli_cdkstats.rxstop;
5064     	stli_comstats.rxrtsoff = stli_cdkstats.rtscnt / 2;
5065     	stli_comstats.rxrtson = stli_cdkstats.rtscnt - stli_comstats.rxrtsoff;
5066     	stli_comstats.modem = stli_cdkstats.dcdcnt;
5067     	stli_comstats.hwid = stli_cdkstats.hwid;
5068     	stli_comstats.signals = stli_mktiocm(stli_cdkstats.signals);
5069     
5070     	return(0);
5071     }
5072     
5073     /*****************************************************************************/
5074     
5075     /*
5076      *	Return the port stats structure to user app. A NULL port struct
5077      *	pointer passed in means that we need to find out from the app
5078      *	what port to get stats for (used through board control device).
5079      */
5080     
5081     static int stli_getportstats(stliport_t *portp, comstats_t *cp)
5082     {
5083     	stlibrd_t	*brdp;
5084     	int		rc;
5085     
5086     	if (portp == (stliport_t *) NULL) {
5087     		if (copy_from_user(&stli_comstats, cp, sizeof(comstats_t)))
5088     			return -EFAULT;
5089     		portp = stli_getport(stli_comstats.brd, stli_comstats.panel,
5090     			stli_comstats.port);
5091     		if (portp == (stliport_t *) NULL)
5092     			return(-ENODEV);
5093     	}
5094     
5095     	brdp = stli_brds[portp->brdnr];
5096     	if (brdp == (stlibrd_t *) NULL)
5097     		return(-ENODEV);
5098     
5099     	if ((rc = stli_portcmdstats(portp)) < 0)
5100     		return(rc);
5101     
5102     	return copy_to_user(cp, &stli_comstats, sizeof(comstats_t)) ?
5103     			-EFAULT : 0;
5104     }
5105     
5106     /*****************************************************************************/
5107     
5108     /*
5109      *	Clear the port stats structure. We also return it zeroed out...
5110      */
5111     
5112     static int stli_clrportstats(stliport_t *portp, comstats_t *cp)
5113     {
5114     	stlibrd_t	*brdp;
5115     	int		rc;
5116     
5117     	if (portp == (stliport_t *) NULL) {
5118     		if (copy_from_user(&stli_comstats, cp, sizeof(comstats_t)))
5119     			return -EFAULT;
5120     		portp = stli_getport(stli_comstats.brd, stli_comstats.panel,
5121     			stli_comstats.port);
5122     		if (portp == (stliport_t *) NULL)
5123     			return(-ENODEV);
5124     	}
5125     
5126     	brdp = stli_brds[portp->brdnr];
5127     	if (brdp == (stlibrd_t *) NULL)
5128     		return(-ENODEV);
5129     
5130     	if (brdp->state & BST_STARTED) {
5131     		if ((rc = stli_cmdwait(brdp, portp, A_CLEARSTATS, 0, 0, 0)) < 0)
5132     			return(rc);
5133     	}
5134     
5135     	memset(&stli_comstats, 0, sizeof(comstats_t));
5136     	stli_comstats.brd = portp->brdnr;
5137     	stli_comstats.panel = portp->panelnr;
5138     	stli_comstats.port = portp->portnr;
5139     
5140     	if (copy_to_user(cp, &stli_comstats, sizeof(comstats_t)))
5141     		return -EFAULT;
5142     	return(0);
5143     }
5144     
5145     /*****************************************************************************/
5146     
5147     /*
5148      *	Return the entire driver ports structure to a user app.
5149      */
5150     
5151     static int stli_getportstruct(unsigned long arg)
5152     {
5153     	stliport_t	*portp;
5154     
5155     	if (copy_from_user(&stli_dummyport, (void *)arg, sizeof(stliport_t)))
5156     		return -EFAULT;
5157     	portp = stli_getport(stli_dummyport.brdnr, stli_dummyport.panelnr,
5158     		 stli_dummyport.portnr);
5159     	if (portp == (stliport_t *) NULL)
5160     		return(-ENODEV);
5161     	if (copy_to_user((void *) arg, portp, sizeof(stliport_t)))
5162     		return -EFAULT;
5163     	return(0);
5164     }
5165     
5166     /*****************************************************************************/
5167     
5168     /*
5169      *	Return the entire driver board structure to a user app.
5170      */
5171     
5172     static int stli_getbrdstruct(unsigned long arg)
5173     {
5174     	stlibrd_t	*brdp;
5175     
5176     	if (copy_from_user(&stli_dummybrd, (void *)arg, sizeof(stlibrd_t)))
5177     		return -EFAULT;
5178     	if ((stli_dummybrd.brdnr < 0) || (stli_dummybrd.brdnr >= STL_MAXBRDS))
5179     		return(-ENODEV);
5180     	brdp = stli_brds[stli_dummybrd.brdnr];
5181     	if (brdp == (stlibrd_t *) NULL)
5182     		return(-ENODEV);
5183     	if (copy_to_user((void *) arg, brdp, sizeof(stlibrd_t)))
5184     		return -EFAULT;
5185     	return(0);
5186     }
5187     
5188     /*****************************************************************************/
5189     
5190     /*
5191      *	The "staliomem" device is also required to do some special operations on
5192      *	the board. We need to be able to send an interrupt to the board,
5193      *	reset it, and start/stop it.
5194      */
5195     
5196     static int stli_memioctl(struct inode *ip, struct file *fp, unsigned int cmd, unsigned long arg)
5197     {
5198     	stlibrd_t	*brdp;
5199     	int		brdnr, rc, done;
5200     
5201     #if DEBUG
5202     	printk(KERN_DEBUG "stli_memioctl(ip=%x,fp=%x,cmd=%x,arg=%x)\n",
5203     			(int) ip, (int) fp, cmd, (int) arg);
5204     #endif
5205     
5206     /*
5207      *	First up handle the board independent ioctls.
5208      */
5209     	done = 0;
5210     	rc = 0;
5211     
5212     	switch (cmd) {
5213     	case COM_GETPORTSTATS:
5214     		rc = stli_getportstats((stliport_t *)NULL, (comstats_t *)arg);
5215     		done++;
5216     		break;
5217     	case COM_CLRPORTSTATS:
5218     		rc = stli_clrportstats((stliport_t *)NULL, (comstats_t *)arg);
5219     		done++;
5220     		break;
5221     	case COM_GETBRDSTATS:
5222     		rc = stli_getbrdstats((combrd_t *) arg);
5223     		done++;
5224     		break;
5225     	case COM_READPORT:
5226     		rc = stli_getportstruct(arg);
5227     		done++;
5228     		break;
5229     	case COM_READBOARD:
5230     		rc = stli_getbrdstruct(arg);
5231     		done++;
5232     		break;
5233     	}
5234     
5235     	if (done)
5236     		return(rc);
5237     
5238     /*
5239      *	Now handle the board specific ioctls. These all depend on the
5240      *	minor number of the device they were called from.
5241      */
5242     	brdnr = MINOR(ip->i_rdev);
5243     	if (brdnr >= STL_MAXBRDS)
5244     		return(-ENODEV);
5245     	brdp = stli_brds[brdnr];
5246     	if (brdp == (stlibrd_t *) NULL)
5247     		return(-ENODEV);
5248     	if (brdp->state == 0)
5249     		return(-ENODEV);
5250     
5251     	switch (cmd) {
5252     	case STL_BINTR:
5253     		EBRDINTR(brdp);
5254     		break;
5255     	case STL_BSTART:
5256     		rc = stli_startbrd(brdp);
5257     		break;
5258     	case STL_BSTOP:
5259     		brdp->state &= ~BST_STARTED;
5260     		break;
5261     	case STL_BRESET:
5262     		brdp->state &= ~BST_STARTED;
5263     		EBRDRESET(brdp);
5264     		if (stli_shared == 0) {
5265     			if (brdp->reenable != NULL)
5266     				(* brdp->reenable)(brdp);
5267     		}
5268     		break;
5269     	default:
5270     		rc = -ENOIOCTLCMD;
5271     		break;
5272     	}
5273     
5274     	return(rc);
5275     }
5276     
5277     /*****************************************************************************/
5278     
5279     int __init stli_init(void)
5280     {
5281     	printk(KERN_INFO "%s: version %s\n", stli_drvtitle, stli_drvversion);
5282     
5283     	stli_initbrds();
5284     
5285     /*
5286      *	Allocate a temporary write buffer.
5287      */
5288     	stli_tmpwritebuf = (char *) stli_memalloc(STLI_TXBUFSIZE);
5289     	if (stli_tmpwritebuf == (char *) NULL)
5290     		printk(KERN_ERR "STALLION: failed to allocate memory "
5291     				"(size=%d)\n", STLI_TXBUFSIZE);
5292     	stli_txcookbuf = stli_memalloc(STLI_TXBUFSIZE);
5293     	if (stli_txcookbuf == (char *) NULL)
5294     		printk(KERN_ERR "STALLION: failed to allocate memory "
5295     				"(size=%d)\n", STLI_TXBUFSIZE);
5296     
5297     /*
5298      *	Set up a character driver for the shared memory region. We need this
5299      *	to down load the slave code image. Also it is a useful debugging tool.
5300      */
5301     	if (devfs_register_chrdev(STL_SIOMEMMAJOR, "staliomem", &stli_fsiomem))
5302     		printk(KERN_ERR "STALLION: failed to register serial memory "
5303     				"device\n");
5304     
5305     	devfs_handle = devfs_mk_dir (NULL, "staliomem", NULL);
5306     	devfs_register_series (devfs_handle, "%u", 4, DEVFS_FL_DEFAULT,
5307     			       STL_SIOMEMMAJOR, 0,
5308     			       S_IFCHR | S_IRUSR | S_IWUSR,
5309     			       &stli_fsiomem, NULL);
5310     
5311     /*
5312      *	Set up the tty driver structure and register us as a driver.
5313      *	Also setup the callout tty device.
5314      */
5315     	memset(&stli_serial, 0, sizeof(struct tty_driver));
5316     	stli_serial.magic = TTY_DRIVER_MAGIC;
5317     	stli_serial.driver_name = stli_drvname;
5318     	stli_serial.name = stli_serialname;
5319     	stli_serial.major = STL_SERIALMAJOR;
5320     	stli_serial.minor_start = 0;
5321     	stli_serial.num = STL_MAXBRDS * STL_MAXPORTS;
5322     	stli_serial.type = TTY_DRIVER_TYPE_SERIAL;
5323     	stli_serial.subtype = STL_DRVTYPSERIAL;
5324     	stli_serial.init_termios = stli_deftermios;
5325     	stli_serial.flags = TTY_DRIVER_REAL_RAW;
5326     	stli_serial.refcount = &stli_refcount;
5327     	stli_serial.table = stli_ttys;
5328     	stli_serial.termios = stli_termios;
5329     	stli_serial.termios_locked = stli_termioslocked;
5330     	
5331     	stli_serial.open = stli_open;
5332     	stli_serial.close = stli_close;
5333     	stli_serial.write = stli_write;
5334     	stli_serial.put_char = stli_putchar;
5335     	stli_serial.flush_chars = stli_flushchars;
5336     	stli_serial.write_room = stli_writeroom;
5337     	stli_serial.chars_in_buffer = stli_charsinbuffer;
5338     	stli_serial.ioctl = stli_ioctl;
5339     	stli_serial.set_termios = stli_settermios;
5340     	stli_serial.throttle = stli_throttle;
5341     	stli_serial.unthrottle = stli_unthrottle;
5342     	stli_serial.stop = stli_stop;
5343     	stli_serial.start = stli_start;
5344     	stli_serial.hangup = stli_hangup;
5345     	stli_serial.flush_buffer = stli_flushbuffer;
5346     	stli_serial.break_ctl = stli_breakctl;
5347     	stli_serial.wait_until_sent = stli_waituntilsent;
5348     	stli_serial.send_xchar = stli_sendxchar;
5349     	stli_serial.read_proc = stli_readproc;
5350     
5351     	stli_callout = stli_serial;
5352     	stli_callout.name = stli_calloutname;
5353     	stli_callout.major = STL_CALLOUTMAJOR;
5354     	stli_callout.subtype = STL_DRVTYPCALLOUT;
5355     	stli_callout.read_proc = 0;
5356     
5357     	if (tty_register_driver(&stli_serial))
5358     		printk(KERN_ERR "STALLION: failed to register serial driver\n");
5359     	if (tty_register_driver(&stli_callout))
5360     		printk(KERN_ERR "STALLION: failed to register callout driver\n");
5361     
5362     	return(0);
5363     }
5364     
5365     /*****************************************************************************/
5366