Import 2.3.50pre1
[davej-history.git] / drivers / char / istallion.c
blobad945239027b3c4cf3aa1ffa016bff40ea16db1d
1 /*****************************************************************************/
3 /*
4 * istallion.c -- stallion intelligent multiport serial driver.
6 * Copyright (C) 1996-1999 Stallion Technologies (support@stallion.oz.au).
7 * Copyright (C) 1994-1996 Greg Ungerer.
9 * This code is loosely based on the Linux serial driver, written by
10 * Linus Torvalds, Theodore T'so and others.
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.
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.
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.
27 /*****************************************************************************/
29 #include <linux/config.h>
30 #include <linux/module.h>
31 #include <linux/malloc.h>
32 #include <linux/interrupt.h>
33 #include <linux/tty_flip.h>
34 #include <linux/serial.h>
35 #include <linux/cdk.h>
36 #include <linux/comstats.h>
37 #include <linux/version.h>
38 #include <linux/istallion.h>
39 #include <linux/ioport.h>
40 #include <linux/delay.h>
41 #include <linux/init.h>
42 #include <linux/devfs_fs_kernel.h>
44 #include <asm/io.h>
45 #include <asm/uaccess.h>
47 #ifdef CONFIG_PCI
48 #include <linux/pci.h>
49 #endif
51 /*****************************************************************************/
54 * Define different board types. Not all of the following board types
55 * are supported by this driver. But I will use the standard "assigned"
56 * board numbers. Currently supported boards are abbreviated as:
57 * ECP = EasyConnection 8/64, ONB = ONboard, BBY = Brumby and
58 * STAL = Stallion.
60 #define BRD_UNKNOWN 0
61 #define BRD_STALLION 1
62 #define BRD_BRUMBY4 2
63 #define BRD_ONBOARD2 3
64 #define BRD_ONBOARD 4
65 #define BRD_BRUMBY8 5
66 #define BRD_BRUMBY16 6
67 #define BRD_ONBOARDE 7
68 #define BRD_ONBOARD32 9
69 #define BRD_ONBOARD2_32 10
70 #define BRD_ONBOARDRS 11
71 #define BRD_EASYIO 20
72 #define BRD_ECH 21
73 #define BRD_ECHMC 22
74 #define BRD_ECP 23
75 #define BRD_ECPE 24
76 #define BRD_ECPMC 25
77 #define BRD_ECHPCI 26
78 #define BRD_ECH64PCI 27
79 #define BRD_EASYIOPCI 28
80 #define BRD_ECPPCI 29
82 #define BRD_BRUMBY BRD_BRUMBY4
85 * Define a configuration structure to hold the board configuration.
86 * Need to set this up in the code (for now) with the boards that are
87 * to be configured into the system. This is what needs to be modified
88 * when adding/removing/modifying boards. Each line entry in the
89 * stli_brdconf[] array is a board. Each line contains io/irq/memory
90 * ranges for that board (as well as what type of board it is).
91 * Some examples:
92 * { BRD_ECP, 0x2a0, 0, 0xcc000, 0, 0 },
93 * This line will configure an EasyConnection 8/64 at io address 2a0,
94 * and shared memory address of cc000. Multiple EasyConnection 8/64
95 * boards can share the same shared memory address space. No interrupt
96 * is required for this board type.
97 * Another example:
98 * { BRD_ECPE, 0x5000, 0, 0x80000000, 0, 0 },
99 * This line will configure an EasyConnection 8/64 EISA in slot 5 and
100 * shared memory address of 0x80000000 (2 GByte). Multiple
101 * EasyConnection 8/64 EISA boards can share the same shared memory
102 * address space. No interrupt is required for this board type.
103 * Another example:
104 * { BRD_ONBOARD, 0x240, 0, 0xd0000, 0, 0 },
105 * This line will configure an ONboard (ISA type) at io address 240,
106 * and shared memory address of d0000. Multiple ONboards can share
107 * the same shared memory address space. No interrupt required.
108 * Another example:
109 * { BRD_BRUMBY4, 0x360, 0, 0xc8000, 0, 0 },
110 * This line will configure a Brumby board (any number of ports!) at
111 * io address 360 and shared memory address of c8000. All Brumby boards
112 * configured into a system must have their own separate io and memory
113 * addresses. No interrupt is required.
114 * Another example:
115 * { BRD_STALLION, 0x330, 0, 0xd0000, 0, 0 },
116 * This line will configure an original Stallion board at io address 330
117 * and shared memory address d0000 (this would only be valid for a "V4.0"
118 * or Rev.O Stallion board). All Stallion boards configured into the
119 * system must have their own separate io and memory addresses. No
120 * interrupt is required.
123 typedef struct {
124 int brdtype;
125 int ioaddr1;
126 int ioaddr2;
127 unsigned long memaddr;
128 int irq;
129 int irqtype;
130 } stlconf_t;
132 static stlconf_t stli_brdconf[] = {
133 /*{ BRD_ECP, 0x2a0, 0, 0xcc000, 0, 0 },*/
136 static int stli_nrbrds = sizeof(stli_brdconf) / sizeof(stlconf_t);
139 * There is some experimental EISA board detection code in this driver.
140 * By default it is disabled, but for those that want to try it out,
141 * then set the define below to be 1.
143 #define STLI_EISAPROBE 0
145 static devfs_handle_t devfs_handle = NULL;
147 /*****************************************************************************/
150 * Define some important driver characteristics. Device major numbers
151 * allocated as per Linux Device Registry.
153 #ifndef STL_SIOMEMMAJOR
154 #define STL_SIOMEMMAJOR 28
155 #endif
156 #ifndef STL_SERIALMAJOR
157 #define STL_SERIALMAJOR 24
158 #endif
159 #ifndef STL_CALLOUTMAJOR
160 #define STL_CALLOUTMAJOR 25
161 #endif
163 #define STL_DRVTYPSERIAL 1
164 #define STL_DRVTYPCALLOUT 2
166 /*****************************************************************************/
169 * Define our local driver identity first. Set up stuff to deal with
170 * all the local structures required by a serial tty driver.
172 static char *stli_drvtitle = "Stallion Intelligent Multiport Serial Driver";
173 static char *stli_drvname = "istallion";
174 static char *stli_drvversion = "5.6.0";
175 static char *stli_serialname = "ttyE";
176 static char *stli_calloutname = "cue";
178 static struct tty_driver stli_serial;
179 static struct tty_driver stli_callout;
180 static struct tty_struct *stli_ttys[STL_MAXDEVS];
181 static struct termios *stli_termios[STL_MAXDEVS];
182 static struct termios *stli_termioslocked[STL_MAXDEVS];
183 static int stli_refcount;
186 * We will need to allocate a temporary write buffer for chars that
187 * come direct from user space. The problem is that a copy from user
188 * space might cause a page fault (typically on a system that is
189 * swapping!). All ports will share one buffer - since if the system
190 * is already swapping a shared buffer won't make things any worse.
192 static char *stli_tmpwritebuf = (char *) NULL;
193 static DECLARE_MUTEX(stli_tmpwritesem);
195 #define STLI_TXBUFSIZE 4096
198 * Use a fast local buffer for cooked characters. Typically a whole
199 * bunch of cooked characters come in for a port, 1 at a time. So we
200 * save those up into a local buffer, then write out the whole lot
201 * with a large memcpy. Just use 1 buffer for all ports, since its
202 * use it is only need for short periods of time by each port.
204 static char *stli_txcookbuf = (char *) NULL;
205 static int stli_txcooksize = 0;
206 static int stli_txcookrealsize = 0;
207 static struct tty_struct *stli_txcooktty = (struct tty_struct *) NULL;
210 * Define a local default termios struct. All ports will be created
211 * with this termios initially. Basically all it defines is a raw port
212 * at 9600 baud, 8 data bits, no parity, 1 stop bit.
214 static struct termios stli_deftermios = {
217 (B9600 | CS8 | CREAD | HUPCL | CLOCAL),
220 INIT_C_CC
224 * Define global stats structures. Not used often, and can be
225 * re-used for each stats call.
227 static comstats_t stli_comstats;
228 static combrd_t stli_brdstats;
229 static asystats_t stli_cdkstats;
230 static stlibrd_t stli_dummybrd;
231 static stliport_t stli_dummyport;
233 /*****************************************************************************/
235 static stlibrd_t *stli_brds[STL_MAXBRDS];
237 static int stli_shared = 0;
240 * Per board state flags. Used with the state field of the board struct.
241 * Not really much here... All we need to do is keep track of whether
242 * the board has been detected, and whether it is actually running a slave
243 * or not.
245 #define BST_FOUND 0x1
246 #define BST_STARTED 0x2
249 * Define the set of port state flags. These are marked for internal
250 * state purposes only, usually to do with the state of communications
251 * with the slave. Most of them need to be updated atomically, so always
252 * use the bit setting operations (unless protected by cli/sti).
254 #define ST_INITIALIZING 1
255 #define ST_OPENING 2
256 #define ST_CLOSING 3
257 #define ST_CMDING 4
258 #define ST_TXBUSY 5
259 #define ST_RXING 6
260 #define ST_DOFLUSHRX 7
261 #define ST_DOFLUSHTX 8
262 #define ST_DOSIGS 9
263 #define ST_RXSTOP 10
264 #define ST_GETSIGS 11
267 * Define an array of board names as printable strings. Handy for
268 * referencing boards when printing trace and stuff.
270 static char *stli_brdnames[] = {
271 "Unknown",
272 "Stallion",
273 "Brumby",
274 "ONboard-MC",
275 "ONboard",
276 "Brumby",
277 "Brumby",
278 "ONboard-EI",
279 (char *) NULL,
280 "ONboard",
281 "ONboard-MC",
282 "ONboard-MC",
283 (char *) NULL,
284 (char *) NULL,
285 (char *) NULL,
286 (char *) NULL,
287 (char *) NULL,
288 (char *) NULL,
289 (char *) NULL,
290 (char *) NULL,
291 "EasyIO",
292 "EC8/32-AT",
293 "EC8/32-MC",
294 "EC8/64-AT",
295 "EC8/64-EI",
296 "EC8/64-MC",
297 "EC8/32-PCI",
298 "EC8/64-PCI",
299 "EasyIO-PCI",
300 "EC/RA-PCI",
303 /*****************************************************************************/
305 #ifdef MODULE
307 * Define some string labels for arguments passed from the module
308 * load line. These allow for easy board definitions, and easy
309 * modification of the io, memory and irq resoucres.
312 static char *board0[8];
313 static char *board1[8];
314 static char *board2[8];
315 static char *board3[8];
317 static char **stli_brdsp[] = {
318 (char **) &board0,
319 (char **) &board1,
320 (char **) &board2,
321 (char **) &board3
325 * Define a set of common board names, and types. This is used to
326 * parse any module arguments.
329 typedef struct stlibrdtype {
330 char *name;
331 int type;
332 } stlibrdtype_t;
334 static stlibrdtype_t stli_brdstr[] = {
335 { "stallion", BRD_STALLION },
336 { "1", BRD_STALLION },
337 { "brumby", BRD_BRUMBY },
338 { "brumby4", BRD_BRUMBY },
339 { "brumby/4", BRD_BRUMBY },
340 { "brumby-4", BRD_BRUMBY },
341 { "brumby8", BRD_BRUMBY },
342 { "brumby/8", BRD_BRUMBY },
343 { "brumby-8", BRD_BRUMBY },
344 { "brumby16", BRD_BRUMBY },
345 { "brumby/16", BRD_BRUMBY },
346 { "brumby-16", BRD_BRUMBY },
347 { "2", BRD_BRUMBY },
348 { "onboard2", BRD_ONBOARD2 },
349 { "onboard-2", BRD_ONBOARD2 },
350 { "onboard/2", BRD_ONBOARD2 },
351 { "onboard-mc", BRD_ONBOARD2 },
352 { "onboard/mc", BRD_ONBOARD2 },
353 { "onboard-mca", BRD_ONBOARD2 },
354 { "onboard/mca", BRD_ONBOARD2 },
355 { "3", BRD_ONBOARD2 },
356 { "onboard", BRD_ONBOARD },
357 { "onboardat", BRD_ONBOARD },
358 { "4", BRD_ONBOARD },
359 { "onboarde", BRD_ONBOARDE },
360 { "onboard-e", BRD_ONBOARDE },
361 { "onboard/e", BRD_ONBOARDE },
362 { "onboard-ei", BRD_ONBOARDE },
363 { "onboard/ei", BRD_ONBOARDE },
364 { "7", BRD_ONBOARDE },
365 { "ecp", BRD_ECP },
366 { "ecpat", BRD_ECP },
367 { "ec8/64", BRD_ECP },
368 { "ec8/64-at", BRD_ECP },
369 { "ec8/64-isa", BRD_ECP },
370 { "23", BRD_ECP },
371 { "ecpe", BRD_ECPE },
372 { "ecpei", BRD_ECPE },
373 { "ec8/64-e", BRD_ECPE },
374 { "ec8/64-ei", BRD_ECPE },
375 { "24", BRD_ECPE },
376 { "ecpmc", BRD_ECPMC },
377 { "ec8/64-mc", BRD_ECPMC },
378 { "ec8/64-mca", BRD_ECPMC },
379 { "25", BRD_ECPMC },
380 { "ecppci", BRD_ECPPCI },
381 { "ec/ra", BRD_ECPPCI },
382 { "ec/ra-pc", BRD_ECPPCI },
383 { "ec/ra-pci", BRD_ECPPCI },
384 { "29", BRD_ECPPCI },
388 * Define the module agruments.
390 MODULE_AUTHOR("Greg Ungerer");
391 MODULE_DESCRIPTION("Stallion Intelligent Multiport Serial Driver");
393 MODULE_PARM(board0, "1-3s");
394 MODULE_PARM_DESC(board0, "Board 0 config -> name[,ioaddr[,memaddr]");
395 MODULE_PARM(board1, "1-3s");
396 MODULE_PARM_DESC(board1, "Board 1 config -> name[,ioaddr[,memaddr]");
397 MODULE_PARM(board2, "1-3s");
398 MODULE_PARM_DESC(board2, "Board 2 config -> name[,ioaddr[,memaddr]");
399 MODULE_PARM(board3, "1-3s");
400 MODULE_PARM_DESC(board3, "Board 3 config -> name[,ioaddr[,memaddr]");
402 #endif
405 * Set up a default memory address table for EISA board probing.
406 * The default addresses are all bellow 1Mbyte, which has to be the
407 * case anyway. They should be safe, since we only read values from
408 * them, and interrupts are disabled while we do it. If the higher
409 * memory support is compiled in then we also try probing around
410 * the 1Gb, 2Gb and 3Gb areas as well...
412 static unsigned long stli_eisamemprobeaddrs[] = {
413 0xc0000, 0xd0000, 0xe0000, 0xf0000,
414 0x80000000, 0x80010000, 0x80020000, 0x80030000,
415 0x40000000, 0x40010000, 0x40020000, 0x40030000,
416 0xc0000000, 0xc0010000, 0xc0020000, 0xc0030000,
417 0xff000000, 0xff010000, 0xff020000, 0xff030000,
420 static int stli_eisamempsize = sizeof(stli_eisamemprobeaddrs) / sizeof(unsigned long);
421 int stli_eisaprobe = STLI_EISAPROBE;
424 * Define the Stallion PCI vendor and device IDs.
426 #ifdef CONFIG_PCI
427 #ifndef PCI_VENDOR_ID_STALLION
428 #define PCI_VENDOR_ID_STALLION 0x124d
429 #endif
430 #ifndef PCI_DEVICE_ID_ECRA
431 #define PCI_DEVICE_ID_ECRA 0x0004
432 #endif
433 #endif
435 /*****************************************************************************/
438 * Hardware configuration info for ECP boards. These defines apply
439 * to the directly accessible io ports of the ECP. There is a set of
440 * defines for each ECP board type, ISA, EISA, MCA and PCI.
442 #define ECP_IOSIZE 4
444 #define ECP_MEMSIZE (128 * 1024)
445 #define ECP_PCIMEMSIZE (256 * 1024)
447 #define ECP_ATPAGESIZE (4 * 1024)
448 #define ECP_MCPAGESIZE (4 * 1024)
449 #define ECP_EIPAGESIZE (64 * 1024)
450 #define ECP_PCIPAGESIZE (64 * 1024)
452 #define STL_EISAID 0x8c4e
455 * Important defines for the ISA class of ECP board.
457 #define ECP_ATIREG 0
458 #define ECP_ATCONFR 1
459 #define ECP_ATMEMAR 2
460 #define ECP_ATMEMPR 3
461 #define ECP_ATSTOP 0x1
462 #define ECP_ATINTENAB 0x10
463 #define ECP_ATENABLE 0x20
464 #define ECP_ATDISABLE 0x00
465 #define ECP_ATADDRMASK 0x3f000
466 #define ECP_ATADDRSHFT 12
469 * Important defines for the EISA class of ECP board.
471 #define ECP_EIIREG 0
472 #define ECP_EIMEMARL 1
473 #define ECP_EICONFR 2
474 #define ECP_EIMEMARH 3
475 #define ECP_EIENABLE 0x1
476 #define ECP_EIDISABLE 0x0
477 #define ECP_EISTOP 0x4
478 #define ECP_EIEDGE 0x00
479 #define ECP_EILEVEL 0x80
480 #define ECP_EIADDRMASKL 0x00ff0000
481 #define ECP_EIADDRSHFTL 16
482 #define ECP_EIADDRMASKH 0xff000000
483 #define ECP_EIADDRSHFTH 24
484 #define ECP_EIBRDENAB 0xc84
486 #define ECP_EISAID 0x4
489 * Important defines for the Micro-channel class of ECP board.
490 * (It has a lot in common with the ISA boards.)
492 #define ECP_MCIREG 0
493 #define ECP_MCCONFR 1
494 #define ECP_MCSTOP 0x20
495 #define ECP_MCENABLE 0x80
496 #define ECP_MCDISABLE 0x00
499 * Important defines for the PCI class of ECP board.
500 * (It has a lot in common with the other ECP boards.)
502 #define ECP_PCIIREG 0
503 #define ECP_PCICONFR 1
504 #define ECP_PCISTOP 0x01
507 * Hardware configuration info for ONboard and Brumby boards. These
508 * defines apply to the directly accessible io ports of these boards.
510 #define ONB_IOSIZE 16
511 #define ONB_MEMSIZE (64 * 1024)
512 #define ONB_ATPAGESIZE (64 * 1024)
513 #define ONB_MCPAGESIZE (64 * 1024)
514 #define ONB_EIMEMSIZE (128 * 1024)
515 #define ONB_EIPAGESIZE (64 * 1024)
518 * Important defines for the ISA class of ONboard board.
520 #define ONB_ATIREG 0
521 #define ONB_ATMEMAR 1
522 #define ONB_ATCONFR 2
523 #define ONB_ATSTOP 0x4
524 #define ONB_ATENABLE 0x01
525 #define ONB_ATDISABLE 0x00
526 #define ONB_ATADDRMASK 0xff0000
527 #define ONB_ATADDRSHFT 16
529 #define ONB_MEMENABLO 0
530 #define ONB_MEMENABHI 0x02
533 * Important defines for the EISA class of ONboard board.
535 #define ONB_EIIREG 0
536 #define ONB_EIMEMARL 1
537 #define ONB_EICONFR 2
538 #define ONB_EIMEMARH 3
539 #define ONB_EIENABLE 0x1
540 #define ONB_EIDISABLE 0x0
541 #define ONB_EISTOP 0x4
542 #define ONB_EIEDGE 0x00
543 #define ONB_EILEVEL 0x80
544 #define ONB_EIADDRMASKL 0x00ff0000
545 #define ONB_EIADDRSHFTL 16
546 #define ONB_EIADDRMASKH 0xff000000
547 #define ONB_EIADDRSHFTH 24
548 #define ONB_EIBRDENAB 0xc84
550 #define ONB_EISAID 0x1
553 * Important defines for the Brumby boards. They are pretty simple,
554 * there is not much that is programmably configurable.
556 #define BBY_IOSIZE 16
557 #define BBY_MEMSIZE (64 * 1024)
558 #define BBY_PAGESIZE (16 * 1024)
560 #define BBY_ATIREG 0
561 #define BBY_ATCONFR 1
562 #define BBY_ATSTOP 0x4
565 * Important defines for the Stallion boards. They are pretty simple,
566 * there is not much that is programmably configurable.
568 #define STAL_IOSIZE 16
569 #define STAL_MEMSIZE (64 * 1024)
570 #define STAL_PAGESIZE (64 * 1024)
573 * Define the set of status register values for EasyConnection panels.
574 * The signature will return with the status value for each panel. From
575 * this we can determine what is attached to the board - before we have
576 * actually down loaded any code to it.
578 #define ECH_PNLSTATUS 2
579 #define ECH_PNL16PORT 0x20
580 #define ECH_PNLIDMASK 0x07
581 #define ECH_PNLXPID 0x40
582 #define ECH_PNLINTRPEND 0x80
585 * Define some macros to do things to the board. Even those these boards
586 * are somewhat related there is often significantly different ways of
587 * doing some operation on it (like enable, paging, reset, etc). So each
588 * board class has a set of functions which do the commonly required
589 * operations. The macros below basically just call these functions,
590 * generally checking for a NULL function - which means that the board
591 * needs nothing done to it to achieve this operation!
593 #define EBRDINIT(brdp) \
594 if (brdp->init != NULL) \
595 (* brdp->init)(brdp)
597 #define EBRDENABLE(brdp) \
598 if (brdp->enable != NULL) \
599 (* brdp->enable)(brdp);
601 #define EBRDDISABLE(brdp) \
602 if (brdp->disable != NULL) \
603 (* brdp->disable)(brdp);
605 #define EBRDINTR(brdp) \
606 if (brdp->intr != NULL) \
607 (* brdp->intr)(brdp);
609 #define EBRDRESET(brdp) \
610 if (brdp->reset != NULL) \
611 (* brdp->reset)(brdp);
613 #define EBRDGETMEMPTR(brdp,offset) \
614 (* brdp->getmemptr)(brdp, offset, __LINE__)
617 * Define the maximal baud rate, and the default baud base for ports.
619 #define STL_MAXBAUD 460800
620 #define STL_BAUDBASE 115200
621 #define STL_CLOSEDELAY (5 * HZ / 10)
623 /*****************************************************************************/
626 * Define macros to extract a brd or port number from a minor number.
628 #define MINOR2BRD(min) (((min) & 0xc0) >> 6)
629 #define MINOR2PORT(min) ((min) & 0x3f)
632 * Define a baud rate table that converts termios baud rate selector
633 * into the actual baud rate value. All baud rate calculations are based
634 * on the actual baud rate required.
636 static unsigned int stli_baudrates[] = {
637 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
638 9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600
641 /*****************************************************************************/
644 * Define some handy local macros...
646 #undef MIN
647 #define MIN(a,b) (((a) <= (b)) ? (a) : (b))
649 #undef TOLOWER
650 #define TOLOWER(x) ((((x) >= 'A') && ((x) <= 'Z')) ? ((x) + 0x20) : (x))
652 /*****************************************************************************/
655 * Prototype all functions in this driver!
658 #ifdef MODULE
659 int init_module(void);
660 void cleanup_module(void);
661 static void stli_argbrds(void);
662 static int stli_parsebrd(stlconf_t *confp, char **argp);
664 static unsigned long stli_atol(char *str);
665 #endif
667 int stli_init(void);
668 static int stli_open(struct tty_struct *tty, struct file *filp);
669 static void stli_close(struct tty_struct *tty, struct file *filp);
670 static int stli_write(struct tty_struct *tty, int from_user, const unsigned char *buf, int count);
671 static void stli_putchar(struct tty_struct *tty, unsigned char ch);
672 static void stli_flushchars(struct tty_struct *tty);
673 static int stli_writeroom(struct tty_struct *tty);
674 static int stli_charsinbuffer(struct tty_struct *tty);
675 static int stli_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg);
676 static void stli_settermios(struct tty_struct *tty, struct termios *old);
677 static void stli_throttle(struct tty_struct *tty);
678 static void stli_unthrottle(struct tty_struct *tty);
679 static void stli_stop(struct tty_struct *tty);
680 static void stli_start(struct tty_struct *tty);
681 static void stli_flushbuffer(struct tty_struct *tty);
682 static void stli_breakctl(struct tty_struct *tty, int state);
683 static void stli_waituntilsent(struct tty_struct *tty, int timeout);
684 static void stli_sendxchar(struct tty_struct *tty, char ch);
685 static void stli_hangup(struct tty_struct *tty);
686 static int stli_portinfo(stlibrd_t *brdp, stliport_t *portp, int portnr, char *pos);
688 static int stli_brdinit(stlibrd_t *brdp);
689 static int stli_startbrd(stlibrd_t *brdp);
690 static int stli_memopen(struct inode *ip, struct file *fp);
691 static int stli_memclose(struct inode *ip, struct file *fp);
692 static ssize_t stli_memread(struct file *fp, char *buf, size_t count, loff_t *offp);
693 static ssize_t stli_memwrite(struct file *fp, const char *buf, size_t count, loff_t *offp);
694 static int stli_memioctl(struct inode *ip, struct file *fp, unsigned int cmd, unsigned long arg);
695 static void stli_brdpoll(stlibrd_t *brdp, volatile cdkhdr_t *hdrp);
696 static void stli_poll(unsigned long arg);
697 static int stli_hostcmd(stlibrd_t *brdp, stliport_t *portp);
698 static int stli_initopen(stlibrd_t *brdp, stliport_t *portp);
699 static int stli_rawopen(stlibrd_t *brdp, stliport_t *portp, unsigned long arg, int wait);
700 static int stli_rawclose(stlibrd_t *brdp, stliport_t *portp, unsigned long arg, int wait);
701 static int stli_waitcarrier(stlibrd_t *brdp, stliport_t *portp, struct file *filp);
702 static void stli_dohangup(void *arg);
703 static void stli_delay(int len);
704 static int stli_setport(stliport_t *portp);
705 static int stli_cmdwait(stlibrd_t *brdp, stliport_t *portp, unsigned long cmd, void *arg, int size, int copyback);
706 static void stli_sendcmd(stlibrd_t *brdp, stliport_t *portp, unsigned long cmd, void *arg, int size, int copyback);
707 static void stli_dodelaycmd(stliport_t *portp, volatile cdkctrl_t *cp);
708 static void stli_mkasyport(stliport_t *portp, asyport_t *pp, struct termios *tiosp);
709 static void stli_mkasysigs(asysigs_t *sp, int dtr, int rts);
710 static long stli_mktiocm(unsigned long sigvalue);
711 static void stli_read(stlibrd_t *brdp, stliport_t *portp);
712 static void stli_getserial(stliport_t *portp, struct serial_struct *sp);
713 static int stli_setserial(stliport_t *portp, struct serial_struct *sp);
714 static int stli_getbrdstats(combrd_t *bp);
715 static int stli_getportstats(stliport_t *portp, comstats_t *cp);
716 static int stli_portcmdstats(stliport_t *portp);
717 static int stli_clrportstats(stliport_t *portp, comstats_t *cp);
718 static int stli_getportstruct(unsigned long arg);
719 static int stli_getbrdstruct(unsigned long arg);
720 static void *stli_memalloc(int len);
721 static stlibrd_t *stli_allocbrd(void);
723 static void stli_ecpinit(stlibrd_t *brdp);
724 static void stli_ecpenable(stlibrd_t *brdp);
725 static void stli_ecpdisable(stlibrd_t *brdp);
726 static char *stli_ecpgetmemptr(stlibrd_t *brdp, unsigned long offset, int line);
727 static void stli_ecpreset(stlibrd_t *brdp);
728 static void stli_ecpintr(stlibrd_t *brdp);
729 static void stli_ecpeiinit(stlibrd_t *brdp);
730 static void stli_ecpeienable(stlibrd_t *brdp);
731 static void stli_ecpeidisable(stlibrd_t *brdp);
732 static char *stli_ecpeigetmemptr(stlibrd_t *brdp, unsigned long offset, int line);
733 static void stli_ecpeireset(stlibrd_t *brdp);
734 static void stli_ecpmcenable(stlibrd_t *brdp);
735 static void stli_ecpmcdisable(stlibrd_t *brdp);
736 static char *stli_ecpmcgetmemptr(stlibrd_t *brdp, unsigned long offset, int line);
737 static void stli_ecpmcreset(stlibrd_t *brdp);
738 static void stli_ecppciinit(stlibrd_t *brdp);
739 static char *stli_ecppcigetmemptr(stlibrd_t *brdp, unsigned long offset, int line);
740 static void stli_ecppcireset(stlibrd_t *brdp);
742 static void stli_onbinit(stlibrd_t *brdp);
743 static void stli_onbenable(stlibrd_t *brdp);
744 static void stli_onbdisable(stlibrd_t *brdp);
745 static char *stli_onbgetmemptr(stlibrd_t *brdp, unsigned long offset, int line);
746 static void stli_onbreset(stlibrd_t *brdp);
747 static void stli_onbeinit(stlibrd_t *brdp);
748 static void stli_onbeenable(stlibrd_t *brdp);
749 static void stli_onbedisable(stlibrd_t *brdp);
750 static char *stli_onbegetmemptr(stlibrd_t *brdp, unsigned long offset, int line);
751 static void stli_onbereset(stlibrd_t *brdp);
752 static void stli_bbyinit(stlibrd_t *brdp);
753 static char *stli_bbygetmemptr(stlibrd_t *brdp, unsigned long offset, int line);
754 static void stli_bbyreset(stlibrd_t *brdp);
755 static void stli_stalinit(stlibrd_t *brdp);
756 static char *stli_stalgetmemptr(stlibrd_t *brdp, unsigned long offset, int line);
757 static void stli_stalreset(stlibrd_t *brdp);
759 static stliport_t *stli_getport(int brdnr, int panelnr, int portnr);
761 static inline int stli_initbrds(void);
762 static inline int stli_initecp(stlibrd_t *brdp);
763 static inline int stli_initonb(stlibrd_t *brdp);
764 static inline int stli_findeisabrds(void);
765 static inline int stli_eisamemprobe(stlibrd_t *brdp);
766 static inline int stli_initports(stlibrd_t *brdp);
767 static inline int stli_getbrdnr(void);
769 #ifdef CONFIG_PCI
770 static inline int stli_findpcibrds(void);
771 static inline int stli_initpcibrd(int brdtype, struct pci_dev *devp);
772 #endif
774 /*****************************************************************************/
777 * Define the driver info for a user level shared memory device. This
778 * device will work sort of like the /dev/kmem device - except that it
779 * will give access to the shared memory on the Stallion intelligent
780 * board. This is also a very useful debugging tool.
782 static struct file_operations stli_fsiomem = {
783 read: stli_memread,
784 write: stli_memwrite,
785 ioctl: stli_memioctl,
786 open: stli_memopen,
787 release: stli_memclose,
790 /*****************************************************************************/
793 * Define a timer_list entry for our poll routine. The slave board
794 * is polled every so often to see if anything needs doing. This is
795 * much cheaper on host cpu than using interrupts. It turns out to
796 * not increase character latency by much either...
798 static struct timer_list stli_timerlist = {
799 NULL, NULL, 0, 0, stli_poll
802 static int stli_timeron = 0;
805 * Define the calculation for the timeout routine.
807 #define STLI_TIMEOUT (jiffies + 1)
809 /*****************************************************************************/
811 #ifdef MODULE
814 * Loadable module initialization stuff.
817 int init_module()
819 unsigned long flags;
821 #if DEBUG
822 printk("init_module()\n");
823 #endif
825 save_flags(flags);
826 cli();
827 stli_init();
828 restore_flags(flags);
830 return(0);
833 /*****************************************************************************/
835 void cleanup_module()
837 stlibrd_t *brdp;
838 stliport_t *portp;
839 unsigned long flags;
840 int i, j, k;
842 #if DEBUG
843 printk("cleanup_module()\n");
844 #endif
846 printk(KERN_INFO "Unloading %s: version %s\n", stli_drvtitle,
847 stli_drvversion);
849 save_flags(flags);
850 cli();
853 * Free up all allocated resources used by the ports. This includes
854 * memory and interrupts.
856 if (stli_timeron) {
857 stli_timeron = 0;
858 del_timer(&stli_timerlist);
861 i = tty_unregister_driver(&stli_serial);
862 j = tty_unregister_driver(&stli_callout);
863 if (i || j) {
864 printk("STALLION: failed to un-register tty driver, "
865 "errno=%d,%d\n", -i, -j);
866 restore_flags(flags);
867 return;
869 devfs_unregister (devfs_handle);
870 if ((i = devfs_unregister_chrdev(STL_SIOMEMMAJOR, "staliomem")))
871 printk("STALLION: failed to un-register serial memory device, "
872 "errno=%d\n", -i);
873 if (stli_tmpwritebuf != (char *) NULL)
874 kfree_s(stli_tmpwritebuf, STLI_TXBUFSIZE);
875 if (stli_txcookbuf != (char *) NULL)
876 kfree_s(stli_txcookbuf, STLI_TXBUFSIZE);
878 for (i = 0; (i < stli_nrbrds); i++) {
879 if ((brdp = stli_brds[i]) == (stlibrd_t *) NULL)
880 continue;
881 for (j = 0; (j < STL_MAXPORTS); j++) {
882 portp = brdp->ports[j];
883 if (portp != (stliport_t *) NULL) {
884 if (portp->tty != (struct tty_struct *) NULL)
885 tty_hangup(portp->tty);
886 kfree_s(portp, sizeof(stliport_t));
890 iounmap(brdp->membase);
891 if (brdp->iosize > 0)
892 release_region(brdp->iobase, brdp->iosize);
893 kfree_s(brdp, sizeof(stlibrd_t));
894 stli_brds[i] = (stlibrd_t *) NULL;
897 restore_flags(flags);
900 /*****************************************************************************/
903 * Check for any arguments passed in on the module load command line.
906 static void stli_argbrds()
908 stlconf_t conf;
909 stlibrd_t *brdp;
910 int nrargs, i;
912 #if DEBUG
913 printk("stli_argbrds()\n");
914 #endif
916 nrargs = sizeof(stli_brdsp) / sizeof(char **);
918 for (i = stli_nrbrds; (i < nrargs); i++) {
919 memset(&conf, 0, sizeof(conf));
920 if (stli_parsebrd(&conf, stli_brdsp[i]) == 0)
921 continue;
922 if ((brdp = stli_allocbrd()) == (stlibrd_t *) NULL)
923 continue;
924 stli_nrbrds = i + 1;
925 brdp->brdnr = i;
926 brdp->brdtype = conf.brdtype;
927 brdp->iobase = conf.ioaddr1;
928 brdp->memaddr = conf.memaddr;
929 stli_brdinit(brdp);
933 /*****************************************************************************/
936 * Convert an ascii string number into an unsigned long.
939 static unsigned long stli_atol(char *str)
941 unsigned long val;
942 int base, c;
943 char *sp;
945 val = 0;
946 sp = str;
947 if ((*sp == '0') && (*(sp+1) == 'x')) {
948 base = 16;
949 sp += 2;
950 } else if (*sp == '0') {
951 base = 8;
952 sp++;
953 } else {
954 base = 10;
957 for (; (*sp != 0); sp++) {
958 c = (*sp > '9') ? (TOLOWER(*sp) - 'a' + 10) : (*sp - '0');
959 if ((c < 0) || (c >= base)) {
960 printk("STALLION: invalid argument %s\n", str);
961 val = 0;
962 break;
964 val = (val * base) + c;
966 return(val);
969 /*****************************************************************************/
972 * Parse the supplied argument string, into the board conf struct.
975 static int stli_parsebrd(stlconf_t *confp, char **argp)
977 char *sp;
978 int nrbrdnames, i;
980 #if DEBUG
981 printk("stli_parsebrd(confp=%x,argp=%x)\n", (int) confp, (int) argp);
982 #endif
984 if ((argp[0] == (char *) NULL) || (*argp[0] == 0))
985 return(0);
987 for (sp = argp[0], i = 0; ((*sp != 0) && (i < 25)); sp++, i++)
988 *sp = TOLOWER(*sp);
990 nrbrdnames = sizeof(stli_brdstr) / sizeof(stlibrdtype_t);
991 for (i = 0; (i < nrbrdnames); i++) {
992 if (strcmp(stli_brdstr[i].name, argp[0]) == 0)
993 break;
995 if (i >= nrbrdnames) {
996 printk("STALLION: unknown board name, %s?\n", argp[0]);
997 return(0);
1000 confp->brdtype = stli_brdstr[i].type;
1001 if ((argp[1] != (char *) NULL) && (*argp[1] != 0))
1002 confp->ioaddr1 = stli_atol(argp[1]);
1003 if ((argp[2] != (char *) NULL) && (*argp[2] != 0))
1004 confp->memaddr = stli_atol(argp[2]);
1005 return(1);
1008 #endif
1010 /*****************************************************************************/
1013 * Local driver kernel malloc routine.
1016 static void *stli_memalloc(int len)
1018 return((void *) kmalloc(len, GFP_KERNEL));
1021 /*****************************************************************************/
1023 static int stli_open(struct tty_struct *tty, struct file *filp)
1025 stlibrd_t *brdp;
1026 stliport_t *portp;
1027 unsigned int minordev;
1028 int brdnr, portnr, rc;
1030 #if DEBUG
1031 printk("stli_open(tty=%x,filp=%x): device=%x\n", (int) tty,
1032 (int) filp, tty->device);
1033 #endif
1035 minordev = MINOR(tty->device);
1036 brdnr = MINOR2BRD(minordev);
1037 if (brdnr >= stli_nrbrds)
1038 return(-ENODEV);
1039 brdp = stli_brds[brdnr];
1040 if (brdp == (stlibrd_t *) NULL)
1041 return(-ENODEV);
1042 if ((brdp->state & BST_STARTED) == 0)
1043 return(-ENODEV);
1044 portnr = MINOR2PORT(minordev);
1045 if ((portnr < 0) || (portnr > brdp->nrports))
1046 return(-ENODEV);
1048 portp = brdp->ports[portnr];
1049 if (portp == (stliport_t *) NULL)
1050 return(-ENODEV);
1051 if (portp->devnr < 1)
1052 return(-ENODEV);
1054 MOD_INC_USE_COUNT;
1057 * Check if this port is in the middle of closing. If so then wait
1058 * until it is closed then return error status based on flag settings.
1059 * The sleep here does not need interrupt protection since the wakeup
1060 * for it is done with the same context.
1062 if (portp->flags & ASYNC_CLOSING) {
1063 interruptible_sleep_on(&portp->close_wait);
1064 if (portp->flags & ASYNC_HUP_NOTIFY)
1065 return(-EAGAIN);
1066 return(-ERESTARTSYS);
1070 * On the first open of the device setup the port hardware, and
1071 * initialize the per port data structure. Since initializing the port
1072 * requires several commands to the board we will need to wait for any
1073 * other open that is already initializing the port.
1075 portp->tty = tty;
1076 tty->driver_data = portp;
1077 portp->refcount++;
1079 while (test_bit(ST_INITIALIZING, &portp->state)) {
1080 if (signal_pending(current))
1081 return(-ERESTARTSYS);
1082 interruptible_sleep_on(&portp->raw_wait);
1085 if ((portp->flags & ASYNC_INITIALIZED) == 0) {
1086 set_bit(ST_INITIALIZING, &portp->state);
1087 if ((rc = stli_initopen(brdp, portp)) >= 0) {
1088 portp->flags |= ASYNC_INITIALIZED;
1089 clear_bit(TTY_IO_ERROR, &tty->flags);
1091 clear_bit(ST_INITIALIZING, &portp->state);
1092 wake_up_interruptible(&portp->raw_wait);
1093 if (rc < 0)
1094 return(rc);
1098 * Check if this port is in the middle of closing. If so then wait
1099 * until it is closed then return error status, based on flag settings.
1100 * The sleep here does not need interrupt protection since the wakeup
1101 * for it is done with the same context.
1103 if (portp->flags & ASYNC_CLOSING) {
1104 interruptible_sleep_on(&portp->close_wait);
1105 if (portp->flags & ASYNC_HUP_NOTIFY)
1106 return(-EAGAIN);
1107 return(-ERESTARTSYS);
1111 * Based on type of open being done check if it can overlap with any
1112 * previous opens still in effect. If we are a normal serial device
1113 * then also we might have to wait for carrier.
1115 if (tty->driver.subtype == STL_DRVTYPCALLOUT) {
1116 if (portp->flags & ASYNC_NORMAL_ACTIVE)
1117 return(-EBUSY);
1118 if (portp->flags & ASYNC_CALLOUT_ACTIVE) {
1119 if ((portp->flags & ASYNC_SESSION_LOCKOUT) &&
1120 (portp->session != current->session))
1121 return(-EBUSY);
1122 if ((portp->flags & ASYNC_PGRP_LOCKOUT) &&
1123 (portp->pgrp != current->pgrp))
1124 return(-EBUSY);
1126 portp->flags |= ASYNC_CALLOUT_ACTIVE;
1127 } else {
1128 if (filp->f_flags & O_NONBLOCK) {
1129 if (portp->flags & ASYNC_CALLOUT_ACTIVE)
1130 return(-EBUSY);
1131 } else {
1132 if ((rc = stli_waitcarrier(brdp, portp, filp)) != 0)
1133 return(rc);
1135 portp->flags |= ASYNC_NORMAL_ACTIVE;
1138 if ((portp->refcount == 1) && (portp->flags & ASYNC_SPLIT_TERMIOS)) {
1139 if (tty->driver.subtype == STL_DRVTYPSERIAL)
1140 *tty->termios = portp->normaltermios;
1141 else
1142 *tty->termios = portp->callouttermios;
1143 stli_setport(portp);
1146 portp->session = current->session;
1147 portp->pgrp = current->pgrp;
1148 return(0);
1151 /*****************************************************************************/
1153 static void stli_close(struct tty_struct *tty, struct file *filp)
1155 stlibrd_t *brdp;
1156 stliport_t *portp;
1157 unsigned long flags;
1159 #if DEBUG
1160 printk("stli_close(tty=%x,filp=%x)\n", (int) tty, (int) filp);
1161 #endif
1163 portp = tty->driver_data;
1164 if (portp == (stliport_t *) NULL)
1165 return;
1167 save_flags(flags);
1168 cli();
1169 if (tty_hung_up_p(filp)) {
1170 MOD_DEC_USE_COUNT;
1171 restore_flags(flags);
1172 return;
1174 if ((tty->count == 1) && (portp->refcount != 1))
1175 portp->refcount = 1;
1176 if (portp->refcount-- > 1) {
1177 MOD_DEC_USE_COUNT;
1178 restore_flags(flags);
1179 return;
1182 portp->flags |= ASYNC_CLOSING;
1184 if (portp->flags & ASYNC_NORMAL_ACTIVE)
1185 portp->normaltermios = *tty->termios;
1186 if (portp->flags & ASYNC_CALLOUT_ACTIVE)
1187 portp->callouttermios = *tty->termios;
1190 * May want to wait for data to drain before closing. The BUSY flag
1191 * keeps track of whether we are still transmitting or not. It is
1192 * updated by messages from the slave - indicating when all chars
1193 * really have drained.
1195 if (tty == stli_txcooktty)
1196 stli_flushchars(tty);
1197 tty->closing = 1;
1198 if (portp->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1199 tty_wait_until_sent(tty, portp->closing_wait);
1201 portp->flags &= ~ASYNC_INITIALIZED;
1202 brdp = stli_brds[portp->brdnr];
1203 stli_rawclose(brdp, portp, 0, 0);
1204 if (tty->termios->c_cflag & HUPCL) {
1205 stli_mkasysigs(&portp->asig, 0, 0);
1206 if (test_bit(ST_CMDING, &portp->state))
1207 set_bit(ST_DOSIGS, &portp->state);
1208 else
1209 stli_sendcmd(brdp, portp, A_SETSIGNALS, &portp->asig,
1210 sizeof(asysigs_t), 0);
1212 clear_bit(ST_TXBUSY, &portp->state);
1213 clear_bit(ST_RXSTOP, &portp->state);
1214 set_bit(TTY_IO_ERROR, &tty->flags);
1215 if (tty->ldisc.flush_buffer)
1216 (tty->ldisc.flush_buffer)(tty);
1217 set_bit(ST_DOFLUSHRX, &portp->state);
1218 stli_flushbuffer(tty);
1220 tty->closing = 0;
1221 portp->tty = (struct tty_struct *) NULL;
1223 if (portp->openwaitcnt) {
1224 if (portp->close_delay)
1225 stli_delay(portp->close_delay);
1226 wake_up_interruptible(&portp->open_wait);
1229 portp->flags &= ~(ASYNC_CALLOUT_ACTIVE | ASYNC_NORMAL_ACTIVE |
1230 ASYNC_CLOSING);
1231 wake_up_interruptible(&portp->close_wait);
1232 MOD_DEC_USE_COUNT;
1233 restore_flags(flags);
1236 /*****************************************************************************/
1239 * Carry out first open operations on a port. This involves a number of
1240 * commands to be sent to the slave. We need to open the port, set the
1241 * notification events, set the initial port settings, get and set the
1242 * initial signal values. We sleep and wait in between each one. But
1243 * this still all happens pretty quickly.
1246 static int stli_initopen(stlibrd_t *brdp, stliport_t *portp)
1248 struct tty_struct *tty;
1249 asynotify_t nt;
1250 asyport_t aport;
1251 int rc;
1253 #if DEBUG
1254 printk("stli_initopen(brdp=%x,portp=%x)\n", (int) brdp, (int) portp);
1255 #endif
1257 if ((rc = stli_rawopen(brdp, portp, 0, 1)) < 0)
1258 return(rc);
1260 memset(&nt, 0, sizeof(asynotify_t));
1261 nt.data = (DT_TXLOW | DT_TXEMPTY | DT_RXBUSY | DT_RXBREAK);
1262 nt.signal = SG_DCD;
1263 if ((rc = stli_cmdwait(brdp, portp, A_SETNOTIFY, &nt,
1264 sizeof(asynotify_t), 0)) < 0)
1265 return(rc);
1267 tty = portp->tty;
1268 if (tty == (struct tty_struct *) NULL)
1269 return(-ENODEV);
1270 stli_mkasyport(portp, &aport, tty->termios);
1271 if ((rc = stli_cmdwait(brdp, portp, A_SETPORT, &aport,
1272 sizeof(asyport_t), 0)) < 0)
1273 return(rc);
1275 set_bit(ST_GETSIGS, &portp->state);
1276 if ((rc = stli_cmdwait(brdp, portp, A_GETSIGNALS, &portp->asig,
1277 sizeof(asysigs_t), 1)) < 0)
1278 return(rc);
1279 if (test_and_clear_bit(ST_GETSIGS, &portp->state))
1280 portp->sigs = stli_mktiocm(portp->asig.sigvalue);
1281 stli_mkasysigs(&portp->asig, 1, 1);
1282 if ((rc = stli_cmdwait(brdp, portp, A_SETSIGNALS, &portp->asig,
1283 sizeof(asysigs_t), 0)) < 0)
1284 return(rc);
1286 return(0);
1289 /*****************************************************************************/
1292 * Send an open message to the slave. This will sleep waiting for the
1293 * acknowledgement, so must have user context. We need to co-ordinate
1294 * with close events here, since we don't want open and close events
1295 * to overlap.
1298 static int stli_rawopen(stlibrd_t *brdp, stliport_t *portp, unsigned long arg, int wait)
1300 volatile cdkhdr_t *hdrp;
1301 volatile cdkctrl_t *cp;
1302 volatile unsigned char *bits;
1303 unsigned long flags;
1304 int rc;
1306 #if DEBUG
1307 printk("stli_rawopen(brdp=%x,portp=%x,arg=%x,wait=%d)\n",
1308 (int) brdp, (int) portp, (int) arg, wait);
1309 #endif
1312 * Send a message to the slave to open this port.
1314 save_flags(flags);
1315 cli();
1318 * Slave is already closing this port. This can happen if a hangup
1319 * occurs on this port. So we must wait until it is complete. The
1320 * order of opens and closes may not be preserved across shared
1321 * memory, so we must wait until it is complete.
1323 while (test_bit(ST_CLOSING, &portp->state)) {
1324 if (signal_pending(current)) {
1325 restore_flags(flags);
1326 return(-ERESTARTSYS);
1328 interruptible_sleep_on(&portp->raw_wait);
1332 * Everything is ready now, so write the open message into shared
1333 * memory. Once the message is in set the service bits to say that
1334 * this port wants service.
1336 EBRDENABLE(brdp);
1337 cp = &((volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr))->ctrl;
1338 cp->openarg = arg;
1339 cp->open = 1;
1340 hdrp = (volatile cdkhdr_t *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
1341 bits = ((volatile unsigned char *) hdrp) + brdp->slaveoffset +
1342 portp->portidx;
1343 *bits |= portp->portbit;
1344 EBRDDISABLE(brdp);
1346 if (wait == 0) {
1347 restore_flags(flags);
1348 return(0);
1352 * Slave is in action, so now we must wait for the open acknowledgment
1353 * to come back.
1355 rc = 0;
1356 set_bit(ST_OPENING, &portp->state);
1357 while (test_bit(ST_OPENING, &portp->state)) {
1358 if (signal_pending(current)) {
1359 rc = -ERESTARTSYS;
1360 break;
1362 interruptible_sleep_on(&portp->raw_wait);
1364 restore_flags(flags);
1366 if ((rc == 0) && (portp->rc != 0))
1367 rc = -EIO;
1368 return(rc);
1371 /*****************************************************************************/
1374 * Send a close message to the slave. Normally this will sleep waiting
1375 * for the acknowledgement, but if wait parameter is 0 it will not. If
1376 * wait is true then must have user context (to sleep).
1379 static int stli_rawclose(stlibrd_t *brdp, stliport_t *portp, unsigned long arg, int wait)
1381 volatile cdkhdr_t *hdrp;
1382 volatile cdkctrl_t *cp;
1383 volatile unsigned char *bits;
1384 unsigned long flags;
1385 int rc;
1387 #if DEBUG
1388 printk("stli_rawclose(brdp=%x,portp=%x,arg=%x,wait=%d)\n",
1389 (int) brdp, (int) portp, (int) arg, wait);
1390 #endif
1392 save_flags(flags);
1393 cli();
1396 * Slave is already closing this port. This can happen if a hangup
1397 * occurs on this port.
1399 if (wait) {
1400 while (test_bit(ST_CLOSING, &portp->state)) {
1401 if (signal_pending(current)) {
1402 restore_flags(flags);
1403 return(-ERESTARTSYS);
1405 interruptible_sleep_on(&portp->raw_wait);
1410 * Write the close command into shared memory.
1412 EBRDENABLE(brdp);
1413 cp = &((volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr))->ctrl;
1414 cp->closearg = arg;
1415 cp->close = 1;
1416 hdrp = (volatile cdkhdr_t *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
1417 bits = ((volatile unsigned char *) hdrp) + brdp->slaveoffset +
1418 portp->portidx;
1419 *bits |= portp->portbit;
1420 EBRDDISABLE(brdp);
1422 set_bit(ST_CLOSING, &portp->state);
1423 if (wait == 0) {
1424 restore_flags(flags);
1425 return(0);
1429 * Slave is in action, so now we must wait for the open acknowledgment
1430 * to come back.
1432 rc = 0;
1433 while (test_bit(ST_CLOSING, &portp->state)) {
1434 if (signal_pending(current)) {
1435 rc = -ERESTARTSYS;
1436 break;
1438 interruptible_sleep_on(&portp->raw_wait);
1440 restore_flags(flags);
1442 if ((rc == 0) && (portp->rc != 0))
1443 rc = -EIO;
1444 return(rc);
1447 /*****************************************************************************/
1450 * Send a command to the slave and wait for the response. This must
1451 * have user context (it sleeps). This routine is generic in that it
1452 * can send any type of command. Its purpose is to wait for that command
1453 * to complete (as opposed to initiating the command then returning).
1456 static int stli_cmdwait(stlibrd_t *brdp, stliport_t *portp, unsigned long cmd, void *arg, int size, int copyback)
1458 unsigned long flags;
1460 #if DEBUG
1461 printk("stli_cmdwait(brdp=%x,portp=%x,cmd=%x,arg=%x,size=%d,"
1462 "copyback=%d)\n", (int) brdp, (int) portp, (int) cmd,
1463 (int) arg, size, copyback);
1464 #endif
1466 save_flags(flags);
1467 cli();
1468 while (test_bit(ST_CMDING, &portp->state)) {
1469 if (signal_pending(current)) {
1470 restore_flags(flags);
1471 return(-ERESTARTSYS);
1473 interruptible_sleep_on(&portp->raw_wait);
1476 stli_sendcmd(brdp, portp, cmd, arg, size, copyback);
1478 while (test_bit(ST_CMDING, &portp->state)) {
1479 if (signal_pending(current)) {
1480 restore_flags(flags);
1481 return(-ERESTARTSYS);
1483 interruptible_sleep_on(&portp->raw_wait);
1485 restore_flags(flags);
1487 if (portp->rc != 0)
1488 return(-EIO);
1489 return(0);
1492 /*****************************************************************************/
1495 * Send the termios settings for this port to the slave. This sleeps
1496 * waiting for the command to complete - so must have user context.
1499 static int stli_setport(stliport_t *portp)
1501 stlibrd_t *brdp;
1502 asyport_t aport;
1504 #if DEBUG
1505 printk("stli_setport(portp=%x)\n", (int) portp);
1506 #endif
1508 if (portp == (stliport_t *) NULL)
1509 return(-ENODEV);
1510 if (portp->tty == (struct tty_struct *) NULL)
1511 return(-ENODEV);
1512 if ((portp->brdnr < 0) && (portp->brdnr >= stli_nrbrds))
1513 return(-ENODEV);
1514 brdp = stli_brds[portp->brdnr];
1515 if (brdp == (stlibrd_t *) NULL)
1516 return(-ENODEV);
1518 stli_mkasyport(portp, &aport, portp->tty->termios);
1519 return(stli_cmdwait(brdp, portp, A_SETPORT, &aport, sizeof(asyport_t), 0));
1522 /*****************************************************************************/
1525 * Wait for a specified delay period, this is not a busy-loop. It will
1526 * give up the processor while waiting. Unfortunately this has some
1527 * rather intimate knowledge of the process management stuff.
1530 static void stli_delay(int len)
1532 #if DEBUG
1533 printk("stli_delay(len=%d)\n", len);
1534 #endif
1535 if (len > 0) {
1536 current->state = TASK_INTERRUPTIBLE;
1537 schedule_timeout(len);
1538 current->state = TASK_RUNNING;
1542 /*****************************************************************************/
1545 * Possibly need to wait for carrier (DCD signal) to come high. Say
1546 * maybe because if we are clocal then we don't need to wait...
1549 static int stli_waitcarrier(stlibrd_t *brdp, stliport_t *portp, struct file *filp)
1551 unsigned long flags;
1552 int rc, doclocal;
1554 #if DEBUG
1555 printk("stli_waitcarrier(brdp=%x,portp=%x,filp=%x)\n",
1556 (int) brdp, (int) portp, (int) filp);
1557 #endif
1559 rc = 0;
1560 doclocal = 0;
1562 if (portp->flags & ASYNC_CALLOUT_ACTIVE) {
1563 if (portp->normaltermios.c_cflag & CLOCAL)
1564 doclocal++;
1565 } else {
1566 if (portp->tty->termios->c_cflag & CLOCAL)
1567 doclocal++;
1570 save_flags(flags);
1571 cli();
1572 portp->openwaitcnt++;
1573 if (! tty_hung_up_p(filp))
1574 portp->refcount--;
1576 for (;;) {
1577 if ((portp->flags & ASYNC_CALLOUT_ACTIVE) == 0) {
1578 stli_mkasysigs(&portp->asig, 1, 1);
1579 if ((rc = stli_cmdwait(brdp, portp, A_SETSIGNALS,
1580 &portp->asig, sizeof(asysigs_t), 0)) < 0)
1581 break;
1583 if (tty_hung_up_p(filp) ||
1584 ((portp->flags & ASYNC_INITIALIZED) == 0)) {
1585 if (portp->flags & ASYNC_HUP_NOTIFY)
1586 rc = -EBUSY;
1587 else
1588 rc = -ERESTARTSYS;
1589 break;
1591 if (((portp->flags & ASYNC_CALLOUT_ACTIVE) == 0) &&
1592 ((portp->flags & ASYNC_CLOSING) == 0) &&
1593 (doclocal || (portp->sigs & TIOCM_CD))) {
1594 break;
1596 if (signal_pending(current)) {
1597 rc = -ERESTARTSYS;
1598 break;
1600 interruptible_sleep_on(&portp->open_wait);
1603 if (! tty_hung_up_p(filp))
1604 portp->refcount++;
1605 portp->openwaitcnt--;
1606 restore_flags(flags);
1608 return(rc);
1611 /*****************************************************************************/
1614 * Write routine. Take the data and put it in the shared memory ring
1615 * queue. If port is not already sending chars then need to mark the
1616 * service bits for this port.
1619 static int stli_write(struct tty_struct *tty, int from_user, const unsigned char *buf, int count)
1621 volatile cdkasy_t *ap;
1622 volatile cdkhdr_t *hdrp;
1623 volatile unsigned char *bits;
1624 unsigned char *shbuf, *chbuf;
1625 stliport_t *portp;
1626 stlibrd_t *brdp;
1627 unsigned int len, stlen, head, tail, size;
1628 unsigned long flags;
1630 #if DEBUG
1631 printk("stli_write(tty=%x,from_user=%d,buf=%x,count=%d)\n",
1632 (int) tty, from_user, (int) buf, count);
1633 #endif
1635 if ((tty == (struct tty_struct *) NULL) ||
1636 (stli_tmpwritebuf == (char *) NULL))
1637 return(0);
1638 if (tty == stli_txcooktty)
1639 stli_flushchars(tty);
1640 portp = tty->driver_data;
1641 if (portp == (stliport_t *) NULL)
1642 return(0);
1643 if ((portp->brdnr < 0) || (portp->brdnr >= stli_nrbrds))
1644 return(0);
1645 brdp = stli_brds[portp->brdnr];
1646 if (brdp == (stlibrd_t *) NULL)
1647 return(0);
1648 chbuf = (unsigned char *) buf;
1651 * If copying direct from user space we need to be able to handle page
1652 * faults while we are copying. To do this copy as much as we can now
1653 * into a kernel buffer. From there we copy it into shared memory. The
1654 * big problem is that we do not want shared memory enabled when we are
1655 * sleeping (other boards may be serviced while asleep). Something else
1656 * to note here is the reading of the tail twice. Since the boards
1657 * shared memory can be on an 8-bit bus then we need to be very careful
1658 * reading 16 bit quantities - since both the board (slave) and host
1659 * could be writing and reading at the same time.
1661 if (from_user) {
1662 save_flags(flags);
1663 cli();
1664 EBRDENABLE(brdp);
1665 ap = (volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr);
1666 head = (unsigned int) ap->txq.head;
1667 tail = (unsigned int) ap->txq.tail;
1668 if (tail != ((unsigned int) ap->txq.tail))
1669 tail = (unsigned int) ap->txq.tail;
1670 len = (head >= tail) ? (portp->txsize - (head - tail) - 1) :
1671 (tail - head - 1);
1672 count = MIN(len, count);
1673 EBRDDISABLE(brdp);
1674 restore_flags(flags);
1676 down(&stli_tmpwritesem);
1677 copy_from_user(stli_tmpwritebuf, chbuf, count);
1678 chbuf = &stli_tmpwritebuf[0];
1682 * All data is now local, shove as much as possible into shared memory.
1684 save_flags(flags);
1685 cli();
1686 EBRDENABLE(brdp);
1687 ap = (volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr);
1688 head = (unsigned int) ap->txq.head;
1689 tail = (unsigned int) ap->txq.tail;
1690 if (tail != ((unsigned int) ap->txq.tail))
1691 tail = (unsigned int) ap->txq.tail;
1692 size = portp->txsize;
1693 if (head >= tail) {
1694 len = size - (head - tail) - 1;
1695 stlen = size - head;
1696 } else {
1697 len = tail - head - 1;
1698 stlen = len;
1701 len = MIN(len, count);
1702 count = 0;
1703 shbuf = (char *) EBRDGETMEMPTR(brdp, portp->txoffset);
1705 while (len > 0) {
1706 stlen = MIN(len, stlen);
1707 memcpy((shbuf + head), chbuf, stlen);
1708 chbuf += stlen;
1709 len -= stlen;
1710 count += stlen;
1711 head += stlen;
1712 if (head >= size) {
1713 head = 0;
1714 stlen = tail;
1718 ap = (volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr);
1719 ap->txq.head = head;
1720 if (test_bit(ST_TXBUSY, &portp->state)) {
1721 if (ap->changed.data & DT_TXEMPTY)
1722 ap->changed.data &= ~DT_TXEMPTY;
1724 hdrp = (volatile cdkhdr_t *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
1725 bits = ((volatile unsigned char *) hdrp) + brdp->slaveoffset +
1726 portp->portidx;
1727 *bits |= portp->portbit;
1728 set_bit(ST_TXBUSY, &portp->state);
1729 EBRDDISABLE(brdp);
1731 if (from_user)
1732 up(&stli_tmpwritesem);
1733 restore_flags(flags);
1735 return(count);
1738 /*****************************************************************************/
1741 * Output a single character. We put it into a temporary local buffer
1742 * (for speed) then write out that buffer when the flushchars routine
1743 * is called. There is a safety catch here so that if some other port
1744 * writes chars before the current buffer has been, then we write them
1745 * first them do the new ports.
1748 static void stli_putchar(struct tty_struct *tty, unsigned char ch)
1750 #if DEBUG
1751 printk("stli_putchar(tty=%x,ch=%x)\n", (int) tty, (int) ch);
1752 #endif
1754 if (tty == (struct tty_struct *) NULL)
1755 return;
1756 if (tty != stli_txcooktty) {
1757 if (stli_txcooktty != (struct tty_struct *) NULL)
1758 stli_flushchars(stli_txcooktty);
1759 stli_txcooktty = tty;
1762 stli_txcookbuf[stli_txcooksize++] = ch;
1765 /*****************************************************************************/
1768 * Transfer characters from the local TX cooking buffer to the board.
1769 * We sort of ignore the tty that gets passed in here. We rely on the
1770 * info stored with the TX cook buffer to tell us which port to flush
1771 * the data on. In any case we clean out the TX cook buffer, for re-use
1772 * by someone else.
1775 static void stli_flushchars(struct tty_struct *tty)
1777 volatile cdkhdr_t *hdrp;
1778 volatile unsigned char *bits;
1779 volatile cdkasy_t *ap;
1780 struct tty_struct *cooktty;
1781 stliport_t *portp;
1782 stlibrd_t *brdp;
1783 unsigned int len, stlen, head, tail, size, count, cooksize;
1784 unsigned char *buf, *shbuf;
1785 unsigned long flags;
1787 #if DEBUG
1788 printk("stli_flushchars(tty=%x)\n", (int) tty);
1789 #endif
1791 cooksize = stli_txcooksize;
1792 cooktty = stli_txcooktty;
1793 stli_txcooksize = 0;
1794 stli_txcookrealsize = 0;
1795 stli_txcooktty = (struct tty_struct *) NULL;
1797 if (tty == (struct tty_struct *) NULL)
1798 return;
1799 if (cooktty == (struct tty_struct *) NULL)
1800 return;
1801 if (tty != cooktty)
1802 tty = cooktty;
1803 if (cooksize == 0)
1804 return;
1806 portp = tty->driver_data;
1807 if (portp == (stliport_t *) NULL)
1808 return;
1809 if ((portp->brdnr < 0) || (portp->brdnr >= stli_nrbrds))
1810 return;
1811 brdp = stli_brds[portp->brdnr];
1812 if (brdp == (stlibrd_t *) NULL)
1813 return;
1815 save_flags(flags);
1816 cli();
1817 EBRDENABLE(brdp);
1819 ap = (volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr);
1820 head = (unsigned int) ap->txq.head;
1821 tail = (unsigned int) ap->txq.tail;
1822 if (tail != ((unsigned int) ap->txq.tail))
1823 tail = (unsigned int) ap->txq.tail;
1824 size = portp->txsize;
1825 if (head >= tail) {
1826 len = size - (head - tail) - 1;
1827 stlen = size - head;
1828 } else {
1829 len = tail - head - 1;
1830 stlen = len;
1833 len = MIN(len, cooksize);
1834 count = 0;
1835 shbuf = (char *) EBRDGETMEMPTR(brdp, portp->txoffset);
1836 buf = stli_txcookbuf;
1838 while (len > 0) {
1839 stlen = MIN(len, stlen);
1840 memcpy((shbuf + head), buf, stlen);
1841 buf += stlen;
1842 len -= stlen;
1843 count += stlen;
1844 head += stlen;
1845 if (head >= size) {
1846 head = 0;
1847 stlen = tail;
1851 ap = (volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr);
1852 ap->txq.head = head;
1854 if (test_bit(ST_TXBUSY, &portp->state)) {
1855 if (ap->changed.data & DT_TXEMPTY)
1856 ap->changed.data &= ~DT_TXEMPTY;
1858 hdrp = (volatile cdkhdr_t *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
1859 bits = ((volatile unsigned char *) hdrp) + brdp->slaveoffset +
1860 portp->portidx;
1861 *bits |= portp->portbit;
1862 set_bit(ST_TXBUSY, &portp->state);
1864 EBRDDISABLE(brdp);
1865 restore_flags(flags);
1868 /*****************************************************************************/
1870 static int stli_writeroom(struct tty_struct *tty)
1872 volatile cdkasyrq_t *rp;
1873 stliport_t *portp;
1874 stlibrd_t *brdp;
1875 unsigned int head, tail, len;
1876 unsigned long flags;
1878 #if DEBUG
1879 printk("stli_writeroom(tty=%x)\n", (int) tty);
1880 #endif
1882 if (tty == (struct tty_struct *) NULL)
1883 return(0);
1884 if (tty == stli_txcooktty) {
1885 if (stli_txcookrealsize != 0) {
1886 len = stli_txcookrealsize - stli_txcooksize;
1887 return(len);
1891 portp = tty->driver_data;
1892 if (portp == (stliport_t *) NULL)
1893 return(0);
1894 if ((portp->brdnr < 0) || (portp->brdnr >= stli_nrbrds))
1895 return(0);
1896 brdp = stli_brds[portp->brdnr];
1897 if (brdp == (stlibrd_t *) NULL)
1898 return(0);
1900 save_flags(flags);
1901 cli();
1902 EBRDENABLE(brdp);
1903 rp = &((volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr))->txq;
1904 head = (unsigned int) rp->head;
1905 tail = (unsigned int) rp->tail;
1906 if (tail != ((unsigned int) rp->tail))
1907 tail = (unsigned int) rp->tail;
1908 len = (head >= tail) ? (portp->txsize - (head - tail)) : (tail - head);
1909 len--;
1910 EBRDDISABLE(brdp);
1911 restore_flags(flags);
1913 if (tty == stli_txcooktty) {
1914 stli_txcookrealsize = len;
1915 len -= stli_txcooksize;
1917 return(len);
1920 /*****************************************************************************/
1923 * Return the number of characters in the transmit buffer. Normally we
1924 * will return the number of chars in the shared memory ring queue.
1925 * We need to kludge around the case where the shared memory buffer is
1926 * empty but not all characters have drained yet, for this case just
1927 * return that there is 1 character in the buffer!
1930 static int stli_charsinbuffer(struct tty_struct *tty)
1932 volatile cdkasyrq_t *rp;
1933 stliport_t *portp;
1934 stlibrd_t *brdp;
1935 unsigned int head, tail, len;
1936 unsigned long flags;
1938 #if DEBUG
1939 printk("stli_charsinbuffer(tty=%x)\n", (int) tty);
1940 #endif
1942 if (tty == (struct tty_struct *) NULL)
1943 return(0);
1944 if (tty == stli_txcooktty)
1945 stli_flushchars(tty);
1946 portp = tty->driver_data;
1947 if (portp == (stliport_t *) NULL)
1948 return(0);
1949 if ((portp->brdnr < 0) || (portp->brdnr >= stli_nrbrds))
1950 return(0);
1951 brdp = stli_brds[portp->brdnr];
1952 if (brdp == (stlibrd_t *) NULL)
1953 return(0);
1955 save_flags(flags);
1956 cli();
1957 EBRDENABLE(brdp);
1958 rp = &((volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr))->txq;
1959 head = (unsigned int) rp->head;
1960 tail = (unsigned int) rp->tail;
1961 if (tail != ((unsigned int) rp->tail))
1962 tail = (unsigned int) rp->tail;
1963 len = (head >= tail) ? (head - tail) : (portp->txsize - (tail - head));
1964 if ((len == 0) && test_bit(ST_TXBUSY, &portp->state))
1965 len = 1;
1966 EBRDDISABLE(brdp);
1967 restore_flags(flags);
1969 return(len);
1972 /*****************************************************************************/
1975 * Generate the serial struct info.
1978 static void stli_getserial(stliport_t *portp, struct serial_struct *sp)
1980 struct serial_struct sio;
1981 stlibrd_t *brdp;
1983 #if DEBUG
1984 printk("stli_getserial(portp=%x,sp=%x)\n", (int) portp, (int) sp);
1985 #endif
1987 memset(&sio, 0, sizeof(struct serial_struct));
1988 sio.type = PORT_UNKNOWN;
1989 sio.line = portp->portnr;
1990 sio.irq = 0;
1991 sio.flags = portp->flags;
1992 sio.baud_base = portp->baud_base;
1993 sio.close_delay = portp->close_delay;
1994 sio.closing_wait = portp->closing_wait;
1995 sio.custom_divisor = portp->custom_divisor;
1996 sio.xmit_fifo_size = 0;
1997 sio.hub6 = 0;
1999 brdp = stli_brds[portp->brdnr];
2000 if (brdp != (stlibrd_t *) NULL)
2001 sio.port = brdp->iobase;
2003 copy_to_user(sp, &sio, sizeof(struct serial_struct));
2006 /*****************************************************************************/
2009 * Set port according to the serial struct info.
2010 * At this point we do not do any auto-configure stuff, so we will
2011 * just quietly ignore any requests to change irq, etc.
2014 static int stli_setserial(stliport_t *portp, struct serial_struct *sp)
2016 struct serial_struct sio;
2017 int rc;
2019 #if DEBUG
2020 printk("stli_setserial(portp=%x,sp=%x)\n", (int) portp, (int) sp);
2021 #endif
2023 copy_from_user(&sio, sp, sizeof(struct serial_struct));
2024 if (!capable(CAP_SYS_ADMIN)) {
2025 if ((sio.baud_base != portp->baud_base) ||
2026 (sio.close_delay != portp->close_delay) ||
2027 ((sio.flags & ~ASYNC_USR_MASK) !=
2028 (portp->flags & ~ASYNC_USR_MASK)))
2029 return(-EPERM);
2032 portp->flags = (portp->flags & ~ASYNC_USR_MASK) |
2033 (sio.flags & ASYNC_USR_MASK);
2034 portp->baud_base = sio.baud_base;
2035 portp->close_delay = sio.close_delay;
2036 portp->closing_wait = sio.closing_wait;
2037 portp->custom_divisor = sio.custom_divisor;
2039 if ((rc = stli_setport(portp)) < 0)
2040 return(rc);
2041 return(0);
2044 /*****************************************************************************/
2046 static int stli_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
2048 stliport_t *portp;
2049 stlibrd_t *brdp;
2050 unsigned long lval;
2051 unsigned int ival;
2052 int rc;
2054 #if DEBUG
2055 printk("stli_ioctl(tty=%x,file=%x,cmd=%x,arg=%x)\n",
2056 (int) tty, (int) file, cmd, (int) arg);
2057 #endif
2059 if (tty == (struct tty_struct *) NULL)
2060 return(-ENODEV);
2061 portp = tty->driver_data;
2062 if (portp == (stliport_t *) NULL)
2063 return(-ENODEV);
2064 if ((portp->brdnr < 0) || (portp->brdnr >= stli_nrbrds))
2065 return(0);
2066 brdp = stli_brds[portp->brdnr];
2067 if (brdp == (stlibrd_t *) NULL)
2068 return(0);
2070 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
2071 (cmd != COM_GETPORTSTATS) && (cmd != COM_CLRPORTSTATS)) {
2072 if (tty->flags & (1 << TTY_IO_ERROR))
2073 return(-EIO);
2076 rc = 0;
2078 switch (cmd) {
2079 case TIOCGSOFTCAR:
2080 rc = put_user(((tty->termios->c_cflag & CLOCAL) ? 1 : 0),
2081 (unsigned int *) arg);
2082 break;
2083 case TIOCSSOFTCAR:
2084 if ((rc = verify_area(VERIFY_READ, (void *) arg,
2085 sizeof(unsigned int))) == 0) {
2086 get_user(ival, (unsigned int *) arg);
2087 tty->termios->c_cflag =
2088 (tty->termios->c_cflag & ~CLOCAL) |
2089 (ival ? CLOCAL : 0);
2091 break;
2092 case TIOCMGET:
2093 if ((rc = verify_area(VERIFY_WRITE, (void *) arg,
2094 sizeof(unsigned int))) == 0) {
2095 if ((rc = stli_cmdwait(brdp, portp, A_GETSIGNALS,
2096 &portp->asig, sizeof(asysigs_t), 1)) < 0)
2097 return(rc);
2098 lval = stli_mktiocm(portp->asig.sigvalue);
2099 put_user(lval, (unsigned int *) arg);
2101 break;
2102 case TIOCMBIS:
2103 if ((rc = verify_area(VERIFY_READ, (void *) arg,
2104 sizeof(unsigned int))) == 0) {
2105 get_user(ival, (unsigned int *) arg);
2106 stli_mkasysigs(&portp->asig,
2107 ((ival & TIOCM_DTR) ? 1 : -1),
2108 ((ival & TIOCM_RTS) ? 1 : -1));
2109 rc = stli_cmdwait(brdp, portp, A_SETSIGNALS,
2110 &portp->asig, sizeof(asysigs_t), 0);
2112 break;
2113 case TIOCMBIC:
2114 if ((rc = verify_area(VERIFY_READ, (void *) arg,
2115 sizeof(unsigned int))) == 0) {
2116 get_user(ival, (unsigned int *) arg);
2117 stli_mkasysigs(&portp->asig,
2118 ((ival & TIOCM_DTR) ? 0 : -1),
2119 ((ival & TIOCM_RTS) ? 0 : -1));
2120 rc = stli_cmdwait(brdp, portp, A_SETSIGNALS,
2121 &portp->asig, sizeof(asysigs_t), 0);
2123 break;
2124 case TIOCMSET:
2125 if ((rc = verify_area(VERIFY_READ, (void *) arg,
2126 sizeof(unsigned int))) == 0) {
2127 get_user(ival, (unsigned int *) arg);
2128 stli_mkasysigs(&portp->asig,
2129 ((ival & TIOCM_DTR) ? 1 : 0),
2130 ((ival & TIOCM_RTS) ? 1 : 0));
2131 rc = stli_cmdwait(brdp, portp, A_SETSIGNALS,
2132 &portp->asig, sizeof(asysigs_t), 0);
2134 break;
2135 case TIOCGSERIAL:
2136 if ((rc = verify_area(VERIFY_WRITE, (void *) arg,
2137 sizeof(struct serial_struct))) == 0)
2138 stli_getserial(portp, (struct serial_struct *) arg);
2139 break;
2140 case TIOCSSERIAL:
2141 if ((rc = verify_area(VERIFY_READ, (void *) arg,
2142 sizeof(struct serial_struct))) == 0)
2143 rc = stli_setserial(portp, (struct serial_struct *)arg);
2144 break;
2145 case STL_GETPFLAG:
2146 if ((rc = verify_area(VERIFY_WRITE, (void *) arg,
2147 sizeof(unsigned long))) == 0)
2148 put_user(portp->pflag, (unsigned int *) arg);
2149 break;
2150 case STL_SETPFLAG:
2151 if ((rc = verify_area(VERIFY_READ, (void *) arg,
2152 sizeof(unsigned long))) == 0) {
2153 get_user(portp->pflag, (unsigned int *) arg);
2154 stli_setport(portp);
2156 break;
2157 case COM_GETPORTSTATS:
2158 if ((rc = verify_area(VERIFY_WRITE, (void *) arg,
2159 sizeof(comstats_t))) == 0)
2160 rc = stli_getportstats(portp, (comstats_t *) arg);
2161 break;
2162 case COM_CLRPORTSTATS:
2163 if ((rc = verify_area(VERIFY_WRITE, (void *) arg,
2164 sizeof(comstats_t))) == 0)
2165 rc = stli_clrportstats(portp, (comstats_t *) arg);
2166 break;
2167 case TIOCSERCONFIG:
2168 case TIOCSERGWILD:
2169 case TIOCSERSWILD:
2170 case TIOCSERGETLSR:
2171 case TIOCSERGSTRUCT:
2172 case TIOCSERGETMULTI:
2173 case TIOCSERSETMULTI:
2174 default:
2175 rc = -ENOIOCTLCMD;
2176 break;
2179 return(rc);
2182 /*****************************************************************************/
2185 * This routine assumes that we have user context and can sleep.
2186 * Looks like it is true for the current ttys implementation..!!
2189 static void stli_settermios(struct tty_struct *tty, struct termios *old)
2191 stliport_t *portp;
2192 stlibrd_t *brdp;
2193 struct termios *tiosp;
2194 asyport_t aport;
2196 #if DEBUG
2197 printk("stli_settermios(tty=%x,old=%x)\n", (int) tty, (int) old);
2198 #endif
2200 if (tty == (struct tty_struct *) NULL)
2201 return;
2202 portp = tty->driver_data;
2203 if (portp == (stliport_t *) NULL)
2204 return;
2205 if ((portp->brdnr < 0) || (portp->brdnr >= stli_nrbrds))
2206 return;
2207 brdp = stli_brds[portp->brdnr];
2208 if (brdp == (stlibrd_t *) NULL)
2209 return;
2211 tiosp = tty->termios;
2212 if ((tiosp->c_cflag == old->c_cflag) &&
2213 (tiosp->c_iflag == old->c_iflag))
2214 return;
2216 stli_mkasyport(portp, &aport, tiosp);
2217 stli_cmdwait(brdp, portp, A_SETPORT, &aport, sizeof(asyport_t), 0);
2218 stli_mkasysigs(&portp->asig, ((tiosp->c_cflag & CBAUD) ? 1 : 0), -1);
2219 stli_cmdwait(brdp, portp, A_SETSIGNALS, &portp->asig,
2220 sizeof(asysigs_t), 0);
2221 if ((old->c_cflag & CRTSCTS) && ((tiosp->c_cflag & CRTSCTS) == 0))
2222 tty->hw_stopped = 0;
2223 if (((old->c_cflag & CLOCAL) == 0) && (tiosp->c_cflag & CLOCAL))
2224 wake_up_interruptible(&portp->open_wait);
2227 /*****************************************************************************/
2230 * Attempt to flow control who ever is sending us data. We won't really
2231 * do any flow control action here. We can't directly, and even if we
2232 * wanted to we would have to send a command to the slave. The slave
2233 * knows how to flow control, and will do so when its buffers reach its
2234 * internal high water marks. So what we will do is set a local state
2235 * bit that will stop us sending any RX data up from the poll routine
2236 * (which is the place where RX data from the slave is handled).
2239 static void stli_throttle(struct tty_struct *tty)
2241 stliport_t *portp;
2243 #if DEBUG
2244 printk("stli_throttle(tty=%x)\n", (int) tty);
2245 #endif
2247 if (tty == (struct tty_struct *) NULL)
2248 return;
2249 portp = tty->driver_data;
2250 if (portp == (stliport_t *) NULL)
2251 return;
2253 set_bit(ST_RXSTOP, &portp->state);
2256 /*****************************************************************************/
2259 * Unflow control the device sending us data... That means that all
2260 * we have to do is clear the RXSTOP state bit. The next poll call
2261 * will then be able to pass the RX data back up.
2264 static void stli_unthrottle(struct tty_struct *tty)
2266 stliport_t *portp;
2268 #if DEBUG
2269 printk("stli_unthrottle(tty=%x)\n", (int) tty);
2270 #endif
2272 if (tty == (struct tty_struct *) NULL)
2273 return;
2274 portp = tty->driver_data;
2275 if (portp == (stliport_t *) NULL)
2276 return;
2278 clear_bit(ST_RXSTOP, &portp->state);
2281 /*****************************************************************************/
2284 * Stop the transmitter. Basically to do this we will just turn TX
2285 * interrupts off.
2288 static void stli_stop(struct tty_struct *tty)
2290 stlibrd_t *brdp;
2291 stliport_t *portp;
2292 asyctrl_t actrl;
2294 #if DEBUG
2295 printk("stli_stop(tty=%x)\n", (int) tty);
2296 #endif
2298 if (tty == (struct tty_struct *) NULL)
2299 return;
2300 portp = tty->driver_data;
2301 if (portp == (stliport_t *) NULL)
2302 return;
2303 if ((portp->brdnr < 0) || (portp->brdnr >= stli_nrbrds))
2304 return;
2305 brdp = stli_brds[portp->brdnr];
2306 if (brdp == (stlibrd_t *) NULL)
2307 return;
2309 memset(&actrl, 0, sizeof(asyctrl_t));
2310 actrl.txctrl = CT_STOPFLOW;
2311 #if 0
2312 stli_cmdwait(brdp, portp, A_PORTCTRL, &actrl, sizeof(asyctrl_t), 0);
2313 #endif
2316 /*****************************************************************************/
2319 * Start the transmitter again. Just turn TX interrupts back on.
2322 static void stli_start(struct tty_struct *tty)
2324 stliport_t *portp;
2325 stlibrd_t *brdp;
2326 asyctrl_t actrl;
2328 #if DEBUG
2329 printk("stli_start(tty=%x)\n", (int) tty);
2330 #endif
2332 if (tty == (struct tty_struct *) NULL)
2333 return;
2334 portp = tty->driver_data;
2335 if (portp == (stliport_t *) NULL)
2336 return;
2337 if ((portp->brdnr < 0) || (portp->brdnr >= stli_nrbrds))
2338 return;
2339 brdp = stli_brds[portp->brdnr];
2340 if (brdp == (stlibrd_t *) NULL)
2341 return;
2343 memset(&actrl, 0, sizeof(asyctrl_t));
2344 actrl.txctrl = CT_STARTFLOW;
2345 #if 0
2346 stli_cmdwait(brdp, portp, A_PORTCTRL, &actrl, sizeof(asyctrl_t), 0);
2347 #endif
2350 /*****************************************************************************/
2353 * Scheduler called hang up routine. This is called from the scheduler,
2354 * not direct from the driver "poll" routine. We can't call it there
2355 * since the real local hangup code will enable/disable the board and
2356 * other things that we can't do while handling the poll. Much easier
2357 * to deal with it some time later (don't really care when, hangups
2358 * aren't that time critical).
2361 static void stli_dohangup(void *arg)
2363 stliport_t *portp;
2365 #if DEBUG
2366 printk("stli_dohangup(portp=%x)\n", (int) arg);
2367 #endif
2369 portp = (stliport_t *) arg;
2370 if (portp == (stliport_t *) NULL)
2371 return;
2372 if (portp->tty == (struct tty_struct *) NULL)
2373 return;
2374 tty_hangup(portp->tty);
2377 /*****************************************************************************/
2380 * Hangup this port. This is pretty much like closing the port, only
2381 * a little more brutal. No waiting for data to drain. Shutdown the
2382 * port and maybe drop signals. This is rather tricky really. We want
2383 * to close the port as well.
2386 static void stli_hangup(struct tty_struct *tty)
2388 stliport_t *portp;
2389 stlibrd_t *brdp;
2390 unsigned long flags;
2392 #if DEBUG
2393 printk("stli_hangup(tty=%x)\n", (int) tty);
2394 #endif
2396 if (tty == (struct tty_struct *) NULL)
2397 return;
2398 portp = tty->driver_data;
2399 if (portp == (stliport_t *) NULL)
2400 return;
2401 if ((portp->brdnr < 0) || (portp->brdnr >= stli_nrbrds))
2402 return;
2403 brdp = stli_brds[portp->brdnr];
2404 if (brdp == (stlibrd_t *) NULL)
2405 return;
2407 portp->flags &= ~ASYNC_INITIALIZED;
2409 save_flags(flags);
2410 cli();
2411 if (! test_bit(ST_CLOSING, &portp->state))
2412 stli_rawclose(brdp, portp, 0, 0);
2413 if (tty->termios->c_cflag & HUPCL) {
2414 stli_mkasysigs(&portp->asig, 0, 0);
2415 if (test_bit(ST_CMDING, &portp->state)) {
2416 set_bit(ST_DOSIGS, &portp->state);
2417 set_bit(ST_DOFLUSHTX, &portp->state);
2418 set_bit(ST_DOFLUSHRX, &portp->state);
2419 } else {
2420 stli_sendcmd(brdp, portp, A_SETSIGNALSF,
2421 &portp->asig, sizeof(asysigs_t), 0);
2424 restore_flags(flags);
2426 clear_bit(ST_TXBUSY, &portp->state);
2427 clear_bit(ST_RXSTOP, &portp->state);
2428 set_bit(TTY_IO_ERROR, &tty->flags);
2429 portp->tty = (struct tty_struct *) NULL;
2430 portp->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CALLOUT_ACTIVE);
2431 portp->refcount = 0;
2432 wake_up_interruptible(&portp->open_wait);
2435 /*****************************************************************************/
2438 * Flush characters from the lower buffer. We may not have user context
2439 * so we cannot sleep waiting for it to complete. Also we need to check
2440 * if there is chars for this port in the TX cook buffer, and flush them
2441 * as well.
2444 static void stli_flushbuffer(struct tty_struct *tty)
2446 stliport_t *portp;
2447 stlibrd_t *brdp;
2448 unsigned long ftype, flags;
2450 #if DEBUG
2451 printk("stli_flushbuffer(tty=%x)\n", (int) tty);
2452 #endif
2454 if (tty == (struct tty_struct *) NULL)
2455 return;
2456 portp = tty->driver_data;
2457 if (portp == (stliport_t *) NULL)
2458 return;
2459 if ((portp->brdnr < 0) || (portp->brdnr >= stli_nrbrds))
2460 return;
2461 brdp = stli_brds[portp->brdnr];
2462 if (brdp == (stlibrd_t *) NULL)
2463 return;
2465 save_flags(flags);
2466 cli();
2467 if (tty == stli_txcooktty) {
2468 stli_txcooktty = (struct tty_struct *) NULL;
2469 stli_txcooksize = 0;
2470 stli_txcookrealsize = 0;
2472 if (test_bit(ST_CMDING, &portp->state)) {
2473 set_bit(ST_DOFLUSHTX, &portp->state);
2474 } else {
2475 ftype = FLUSHTX;
2476 if (test_bit(ST_DOFLUSHRX, &portp->state)) {
2477 ftype |= FLUSHRX;
2478 clear_bit(ST_DOFLUSHRX, &portp->state);
2480 stli_sendcmd(brdp, portp, A_FLUSH, &ftype,
2481 sizeof(unsigned long), 0);
2483 restore_flags(flags);
2485 wake_up_interruptible(&tty->write_wait);
2486 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
2487 tty->ldisc.write_wakeup)
2488 (tty->ldisc.write_wakeup)(tty);
2491 /*****************************************************************************/
2493 static void stli_breakctl(struct tty_struct *tty, int state)
2495 stlibrd_t *brdp;
2496 stliport_t *portp;
2497 long arg;
2498 /* long savestate, savetime; */
2500 #if DEBUG
2501 printk("stli_breakctl(tty=%x,state=%d)\n", (int) tty, state);
2502 #endif
2504 if (tty == (struct tty_struct *) NULL)
2505 return;
2506 portp = tty->driver_data;
2507 if (portp == (stliport_t *) NULL)
2508 return;
2509 if ((portp->brdnr < 0) || (portp->brdnr >= stli_nrbrds))
2510 return;
2511 brdp = stli_brds[portp->brdnr];
2512 if (brdp == (stlibrd_t *) NULL)
2513 return;
2516 * Due to a bug in the tty send_break() code we need to preserve
2517 * the current process state and timeout...
2518 savetime = current->timeout;
2519 savestate = current->state;
2522 arg = (state == -1) ? BREAKON : BREAKOFF;
2523 stli_cmdwait(brdp, portp, A_BREAK, &arg, sizeof(long), 0);
2527 current->timeout = savetime;
2528 current->state = savestate;
2532 /*****************************************************************************/
2534 static void stli_waituntilsent(struct tty_struct *tty, int timeout)
2536 stliport_t *portp;
2537 unsigned long tend;
2539 #if DEBUG
2540 printk("stli_waituntilsent(tty=%x,timeout=%x)\n", (int) tty, timeout);
2541 #endif
2543 if (tty == (struct tty_struct *) NULL)
2544 return;
2545 portp = tty->driver_data;
2546 if (portp == (stliport_t *) NULL)
2547 return;
2549 if (timeout == 0)
2550 timeout = HZ;
2551 tend = jiffies + timeout;
2553 while (test_bit(ST_TXBUSY, &portp->state)) {
2554 if (signal_pending(current))
2555 break;
2556 stli_delay(2);
2557 if (time_after_eq(jiffies, tend))
2558 break;
2562 /*****************************************************************************/
2564 static void stli_sendxchar(struct tty_struct *tty, char ch)
2566 stlibrd_t *brdp;
2567 stliport_t *portp;
2568 asyctrl_t actrl;
2570 #if DEBUG
2571 printk("stli_sendxchar(tty=%x,ch=%x)\n", (int) tty, ch);
2572 #endif
2574 if (tty == (struct tty_struct *) NULL)
2575 return;
2576 portp = tty->driver_data;
2577 if (portp == (stliport_t *) NULL)
2578 return;
2579 if ((portp->brdnr < 0) || (portp->brdnr >= stli_nrbrds))
2580 return;
2581 brdp = stli_brds[portp->brdnr];
2582 if (brdp == (stlibrd_t *) NULL)
2583 return;
2585 memset(&actrl, 0, sizeof(asyctrl_t));
2586 if (ch == STOP_CHAR(tty)) {
2587 actrl.rxctrl = CT_STOPFLOW;
2588 } else if (ch == START_CHAR(tty)) {
2589 actrl.rxctrl = CT_STARTFLOW;
2590 } else {
2591 actrl.txctrl = CT_SENDCHR;
2592 actrl.tximdch = ch;
2595 stli_cmdwait(brdp, portp, A_PORTCTRL, &actrl, sizeof(asyctrl_t), 0);
2598 /*****************************************************************************/
2600 #define MAXLINE 80
2603 * Format info for a specified port. The line is deliberately limited
2604 * to 80 characters. (If it is too long it will be truncated, if too
2605 * short then padded with spaces).
2608 static int stli_portinfo(stlibrd_t *brdp, stliport_t *portp, int portnr, char *pos)
2610 char *sp, *uart;
2611 int rc, cnt;
2613 rc = stli_portcmdstats(portp);
2615 uart = "UNKNOWN";
2616 if (brdp->state & BST_STARTED) {
2617 switch (stli_comstats.hwid) {
2618 case 0: uart = "2681"; break;
2619 case 1: uart = "SC26198"; break;
2620 default: uart = "CD1400"; break;
2624 sp = pos;
2625 sp += sprintf(sp, "%d: uart:%s ", portnr, uart);
2627 if ((brdp->state & BST_STARTED) && (rc >= 0)) {
2628 sp += sprintf(sp, "tx:%d rx:%d", (int) stli_comstats.txtotal,
2629 (int) stli_comstats.rxtotal);
2631 if (stli_comstats.rxframing)
2632 sp += sprintf(sp, " fe:%d",
2633 (int) stli_comstats.rxframing);
2634 if (stli_comstats.rxparity)
2635 sp += sprintf(sp, " pe:%d",
2636 (int) stli_comstats.rxparity);
2637 if (stli_comstats.rxbreaks)
2638 sp += sprintf(sp, " brk:%d",
2639 (int) stli_comstats.rxbreaks);
2640 if (stli_comstats.rxoverrun)
2641 sp += sprintf(sp, " oe:%d",
2642 (int) stli_comstats.rxoverrun);
2644 cnt = sprintf(sp, "%s%s%s%s%s ",
2645 (stli_comstats.signals & TIOCM_RTS) ? "|RTS" : "",
2646 (stli_comstats.signals & TIOCM_CTS) ? "|CTS" : "",
2647 (stli_comstats.signals & TIOCM_DTR) ? "|DTR" : "",
2648 (stli_comstats.signals & TIOCM_CD) ? "|DCD" : "",
2649 (stli_comstats.signals & TIOCM_DSR) ? "|DSR" : "");
2650 *sp = ' ';
2651 sp += cnt;
2654 for (cnt = (sp - pos); (cnt < (MAXLINE - 1)); cnt++)
2655 *sp++ = ' ';
2656 if (cnt >= MAXLINE)
2657 pos[(MAXLINE - 2)] = '+';
2658 pos[(MAXLINE - 1)] = '\n';
2660 return(MAXLINE);
2663 /*****************************************************************************/
2666 * Port info, read from the /proc file system.
2669 static int stli_readproc(char *page, char **start, off_t off, int count, int *eof, void *data)
2671 stlibrd_t *brdp;
2672 stliport_t *portp;
2673 int brdnr, portnr, totalport;
2674 int curoff, maxoff;
2675 char *pos;
2677 #if DEBUG
2678 printk("stli_readproc(page=%x,start=%x,off=%x,count=%d,eof=%x,"
2679 "data=%x\n", (int) page, (int) start, (int) off, count,
2680 (int) eof, (int) data);
2681 #endif
2683 pos = page;
2684 totalport = 0;
2685 curoff = 0;
2687 if (off == 0) {
2688 pos += sprintf(pos, "%s: version %s", stli_drvtitle,
2689 stli_drvversion);
2690 while (pos < (page + MAXLINE - 1))
2691 *pos++ = ' ';
2692 *pos++ = '\n';
2694 curoff = MAXLINE;
2697 * We scan through for each board, panel and port. The offset is
2698 * calculated on the fly, and irrelevant ports are skipped.
2700 for (brdnr = 0; (brdnr < stli_nrbrds); brdnr++) {
2701 brdp = stli_brds[brdnr];
2702 if (brdp == (stlibrd_t *) NULL)
2703 continue;
2704 if (brdp->state == 0)
2705 continue;
2707 maxoff = curoff + (brdp->nrports * MAXLINE);
2708 if (off >= maxoff) {
2709 curoff = maxoff;
2710 continue;
2713 totalport = brdnr * STL_MAXPORTS;
2714 for (portnr = 0; (portnr < brdp->nrports); portnr++,
2715 totalport++) {
2716 portp = brdp->ports[portnr];
2717 if (portp == (stliport_t *) NULL)
2718 continue;
2719 if (off >= (curoff += MAXLINE))
2720 continue;
2721 if ((pos - page + MAXLINE) > count)
2722 goto stli_readdone;
2723 pos += stli_portinfo(brdp, portp, totalport, pos);
2727 *eof = 1;
2729 stli_readdone:
2730 *start = page;
2731 return(pos - page);
2734 /*****************************************************************************/
2737 * Generic send command routine. This will send a message to the slave,
2738 * of the specified type with the specified argument. Must be very
2739 * careful of data that will be copied out from shared memory -
2740 * containing command results. The command completion is all done from
2741 * a poll routine that does not have user context. Therefore you cannot
2742 * copy back directly into user space, or to the kernel stack of a
2743 * process. This routine does not sleep, so can be called from anywhere.
2746 static void stli_sendcmd(stlibrd_t *brdp, stliport_t *portp, unsigned long cmd, void *arg, int size, int copyback)
2748 volatile cdkhdr_t *hdrp;
2749 volatile cdkctrl_t *cp;
2750 volatile unsigned char *bits;
2751 unsigned long flags;
2753 #if DEBUG
2754 printk("stli_sendcmd(brdp=%x,portp=%x,cmd=%x,arg=%x,size=%d,"
2755 "copyback=%d)\n", (int) brdp, (int) portp, (int) cmd,
2756 (int) arg, size, copyback);
2757 #endif
2759 save_flags(flags);
2760 cli();
2762 if (test_bit(ST_CMDING, &portp->state)) {
2763 printk("STALLION: command already busy, cmd=%x!\n", (int) cmd);
2764 restore_flags(flags);
2765 return;
2768 EBRDENABLE(brdp);
2769 cp = &((volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr))->ctrl;
2770 if (size > 0) {
2771 memcpy((void *) &(cp->args[0]), arg, size);
2772 if (copyback) {
2773 portp->argp = arg;
2774 portp->argsize = size;
2777 cp->status = 0;
2778 cp->cmd = cmd;
2779 hdrp = (volatile cdkhdr_t *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
2780 bits = ((volatile unsigned char *) hdrp) + brdp->slaveoffset +
2781 portp->portidx;
2782 *bits |= portp->portbit;
2783 set_bit(ST_CMDING, &portp->state);
2784 EBRDDISABLE(brdp);
2785 restore_flags(flags);
2788 /*****************************************************************************/
2791 * Read data from shared memory. This assumes that the shared memory
2792 * is enabled and that interrupts are off. Basically we just empty out
2793 * the shared memory buffer into the tty buffer. Must be careful to
2794 * handle the case where we fill up the tty buffer, but still have
2795 * more chars to unload.
2798 static inline void stli_read(stlibrd_t *brdp, stliport_t *portp)
2800 volatile cdkasyrq_t *rp;
2801 volatile char *shbuf;
2802 struct tty_struct *tty;
2803 unsigned int head, tail, size;
2804 unsigned int len, stlen;
2806 #if DEBUG
2807 printk("stli_read(brdp=%x,portp=%d)\n", (int) brdp, (int) portp);
2808 #endif
2810 if (test_bit(ST_RXSTOP, &portp->state))
2811 return;
2812 tty = portp->tty;
2813 if (tty == (struct tty_struct *) NULL)
2814 return;
2816 rp = &((volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr))->rxq;
2817 head = (unsigned int) rp->head;
2818 if (head != ((unsigned int) rp->head))
2819 head = (unsigned int) rp->head;
2820 tail = (unsigned int) rp->tail;
2821 size = portp->rxsize;
2822 if (head >= tail) {
2823 len = head - tail;
2824 stlen = len;
2825 } else {
2826 len = size - (tail - head);
2827 stlen = size - tail;
2830 len = MIN(len, (TTY_FLIPBUF_SIZE - tty->flip.count));
2831 shbuf = (volatile char *) EBRDGETMEMPTR(brdp, portp->rxoffset);
2833 while (len > 0) {
2834 stlen = MIN(len, stlen);
2835 memcpy(tty->flip.char_buf_ptr, (char *) (shbuf + tail), stlen);
2836 memset(tty->flip.flag_buf_ptr, 0, stlen);
2837 tty->flip.char_buf_ptr += stlen;
2838 tty->flip.flag_buf_ptr += stlen;
2839 tty->flip.count += stlen;
2841 len -= stlen;
2842 tail += stlen;
2843 if (tail >= size) {
2844 tail = 0;
2845 stlen = head;
2848 rp = &((volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr))->rxq;
2849 rp->tail = tail;
2851 if (head != tail)
2852 set_bit(ST_RXING, &portp->state);
2854 tty_schedule_flip(tty);
2857 /*****************************************************************************/
2860 * Set up and carry out any delayed commands. There is only a small set
2861 * of slave commands that can be done "off-level". So it is not too
2862 * difficult to deal with them here.
2865 static inline void stli_dodelaycmd(stliport_t *portp, volatile cdkctrl_t *cp)
2867 int cmd;
2869 if (test_bit(ST_DOSIGS, &portp->state)) {
2870 if (test_bit(ST_DOFLUSHTX, &portp->state) &&
2871 test_bit(ST_DOFLUSHRX, &portp->state))
2872 cmd = A_SETSIGNALSF;
2873 else if (test_bit(ST_DOFLUSHTX, &portp->state))
2874 cmd = A_SETSIGNALSFTX;
2875 else if (test_bit(ST_DOFLUSHRX, &portp->state))
2876 cmd = A_SETSIGNALSFRX;
2877 else
2878 cmd = A_SETSIGNALS;
2879 clear_bit(ST_DOFLUSHTX, &portp->state);
2880 clear_bit(ST_DOFLUSHRX, &portp->state);
2881 clear_bit(ST_DOSIGS, &portp->state);
2882 memcpy((void *) &(cp->args[0]), (void *) &portp->asig,
2883 sizeof(asysigs_t));
2884 cp->status = 0;
2885 cp->cmd = cmd;
2886 set_bit(ST_CMDING, &portp->state);
2887 } else if (test_bit(ST_DOFLUSHTX, &portp->state) ||
2888 test_bit(ST_DOFLUSHRX, &portp->state)) {
2889 cmd = ((test_bit(ST_DOFLUSHTX, &portp->state)) ? FLUSHTX : 0);
2890 cmd |= ((test_bit(ST_DOFLUSHRX, &portp->state)) ? FLUSHRX : 0);
2891 clear_bit(ST_DOFLUSHTX, &portp->state);
2892 clear_bit(ST_DOFLUSHRX, &portp->state);
2893 memcpy((void *) &(cp->args[0]), (void *) &cmd, sizeof(int));
2894 cp->status = 0;
2895 cp->cmd = A_FLUSH;
2896 set_bit(ST_CMDING, &portp->state);
2900 /*****************************************************************************/
2903 * Host command service checking. This handles commands or messages
2904 * coming from the slave to the host. Must have board shared memory
2905 * enabled and interrupts off when called. Notice that by servicing the
2906 * read data last we don't need to change the shared memory pointer
2907 * during processing (which is a slow IO operation).
2908 * Return value indicates if this port is still awaiting actions from
2909 * the slave (like open, command, or even TX data being sent). If 0
2910 * then port is still busy, otherwise no longer busy.
2913 static inline int stli_hostcmd(stlibrd_t *brdp, stliport_t *portp)
2915 volatile cdkasy_t *ap;
2916 volatile cdkctrl_t *cp;
2917 struct tty_struct *tty;
2918 asynotify_t nt;
2919 unsigned long oldsigs;
2920 int rc, donerx;
2922 #if DEBUG
2923 printk("stli_hostcmd(brdp=%x,channr=%d)\n", (int) brdp, channr);
2924 #endif
2926 ap = (volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr);
2927 cp = &ap->ctrl;
2930 * Check if we are waiting for an open completion message.
2932 if (test_bit(ST_OPENING, &portp->state)) {
2933 rc = (int) cp->openarg;
2934 if ((cp->open == 0) && (rc != 0)) {
2935 if (rc > 0)
2936 rc--;
2937 cp->openarg = 0;
2938 portp->rc = rc;
2939 clear_bit(ST_OPENING, &portp->state);
2940 wake_up_interruptible(&portp->raw_wait);
2945 * Check if we are waiting for a close completion message.
2947 if (test_bit(ST_CLOSING, &portp->state)) {
2948 rc = (int) cp->closearg;
2949 if ((cp->close == 0) && (rc != 0)) {
2950 if (rc > 0)
2951 rc--;
2952 cp->closearg = 0;
2953 portp->rc = rc;
2954 clear_bit(ST_CLOSING, &portp->state);
2955 wake_up_interruptible(&portp->raw_wait);
2960 * Check if we are waiting for a command completion message. We may
2961 * need to copy out the command results associated with this command.
2963 if (test_bit(ST_CMDING, &portp->state)) {
2964 rc = cp->status;
2965 if ((cp->cmd == 0) && (rc != 0)) {
2966 if (rc > 0)
2967 rc--;
2968 if (portp->argp != (void *) NULL) {
2969 memcpy(portp->argp, (void *) &(cp->args[0]),
2970 portp->argsize);
2971 portp->argp = (void *) NULL;
2973 cp->status = 0;
2974 portp->rc = rc;
2975 clear_bit(ST_CMDING, &portp->state);
2976 stli_dodelaycmd(portp, cp);
2977 wake_up_interruptible(&portp->raw_wait);
2982 * Check for any notification messages ready. This includes lots of
2983 * different types of events - RX chars ready, RX break received,
2984 * TX data low or empty in the slave, modem signals changed state.
2986 donerx = 0;
2988 if (ap->notify) {
2989 nt = ap->changed;
2990 ap->notify = 0;
2991 tty = portp->tty;
2993 if (nt.signal & SG_DCD) {
2994 oldsigs = portp->sigs;
2995 portp->sigs = stli_mktiocm(nt.sigvalue);
2996 clear_bit(ST_GETSIGS, &portp->state);
2997 if ((portp->sigs & TIOCM_CD) &&
2998 ((oldsigs & TIOCM_CD) == 0))
2999 wake_up_interruptible(&portp->open_wait);
3000 if ((oldsigs & TIOCM_CD) &&
3001 ((portp->sigs & TIOCM_CD) == 0)) {
3002 if (portp->flags & ASYNC_CHECK_CD) {
3003 if (! ((portp->flags & ASYNC_CALLOUT_ACTIVE) &&
3004 (portp->flags & ASYNC_CALLOUT_NOHUP))) {
3005 if (tty != (struct tty_struct *) NULL)
3006 queue_task(&portp->tqhangup, &tq_scheduler);
3012 if (nt.data & DT_TXEMPTY)
3013 clear_bit(ST_TXBUSY, &portp->state);
3014 if (nt.data & (DT_TXEMPTY | DT_TXLOW)) {
3015 if (tty != (struct tty_struct *) NULL) {
3016 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
3017 tty->ldisc.write_wakeup) {
3018 (tty->ldisc.write_wakeup)(tty);
3019 EBRDENABLE(brdp);
3021 wake_up_interruptible(&tty->write_wait);
3025 if ((nt.data & DT_RXBREAK) && (portp->rxmarkmsk & BRKINT)) {
3026 if (tty != (struct tty_struct *) NULL) {
3027 if (tty->flip.count < TTY_FLIPBUF_SIZE) {
3028 tty->flip.count++;
3029 *tty->flip.flag_buf_ptr++ = TTY_BREAK;
3030 *tty->flip.char_buf_ptr++ = 0;
3031 if (portp->flags & ASYNC_SAK) {
3032 do_SAK(tty);
3033 EBRDENABLE(brdp);
3035 tty_schedule_flip(tty);
3040 if (nt.data & DT_RXBUSY) {
3041 donerx++;
3042 stli_read(brdp, portp);
3047 * It might seem odd that we are checking for more RX chars here.
3048 * But, we need to handle the case where the tty buffer was previously
3049 * filled, but we had more characters to pass up. The slave will not
3050 * send any more RX notify messages until the RX buffer has been emptied.
3051 * But it will leave the service bits on (since the buffer is not empty).
3052 * So from here we can try to process more RX chars.
3054 if ((!donerx) && test_bit(ST_RXING, &portp->state)) {
3055 clear_bit(ST_RXING, &portp->state);
3056 stli_read(brdp, portp);
3059 return((test_bit(ST_OPENING, &portp->state) ||
3060 test_bit(ST_CLOSING, &portp->state) ||
3061 test_bit(ST_CMDING, &portp->state) ||
3062 test_bit(ST_TXBUSY, &portp->state) ||
3063 test_bit(ST_RXING, &portp->state)) ? 0 : 1);
3066 /*****************************************************************************/
3069 * Service all ports on a particular board. Assumes that the boards
3070 * shared memory is enabled, and that the page pointer is pointed
3071 * at the cdk header structure.
3074 static inline void stli_brdpoll(stlibrd_t *brdp, volatile cdkhdr_t *hdrp)
3076 stliport_t *portp;
3077 unsigned char hostbits[(STL_MAXCHANS / 8) + 1];
3078 unsigned char slavebits[(STL_MAXCHANS / 8) + 1];
3079 unsigned char *slavep;
3080 int bitpos, bitat, bitsize;
3081 int channr, nrdevs, slavebitchange;
3083 bitsize = brdp->bitsize;
3084 nrdevs = brdp->nrdevs;
3087 * Check if slave wants any service. Basically we try to do as
3088 * little work as possible here. There are 2 levels of service
3089 * bits. So if there is nothing to do we bail early. We check
3090 * 8 service bits at a time in the inner loop, so we can bypass
3091 * the lot if none of them want service.
3093 memcpy(&hostbits[0], (((unsigned char *) hdrp) + brdp->hostoffset),
3094 bitsize);
3096 memset(&slavebits[0], 0, bitsize);
3097 slavebitchange = 0;
3099 for (bitpos = 0; (bitpos < bitsize); bitpos++) {
3100 if (hostbits[bitpos] == 0)
3101 continue;
3102 channr = bitpos * 8;
3103 for (bitat = 0x1; (channr < nrdevs); channr++, bitat <<= 1) {
3104 if (hostbits[bitpos] & bitat) {
3105 portp = brdp->ports[(channr - 1)];
3106 if (stli_hostcmd(brdp, portp)) {
3107 slavebitchange++;
3108 slavebits[bitpos] |= bitat;
3115 * If any of the ports are no longer busy then update them in the
3116 * slave request bits. We need to do this after, since a host port
3117 * service may initiate more slave requests.
3119 if (slavebitchange) {
3120 hdrp = (volatile cdkhdr_t *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
3121 slavep = ((unsigned char *) hdrp) + brdp->slaveoffset;
3122 for (bitpos = 0; (bitpos < bitsize); bitpos++) {
3123 if (slavebits[bitpos])
3124 slavep[bitpos] &= ~slavebits[bitpos];
3129 /*****************************************************************************/
3132 * Driver poll routine. This routine polls the boards in use and passes
3133 * messages back up to host when necessary. This is actually very
3134 * CPU efficient, since we will always have the kernel poll clock, it
3135 * adds only a few cycles when idle (since board service can be
3136 * determined very easily), but when loaded generates no interrupts
3137 * (with their expensive associated context change).
3140 static void stli_poll(unsigned long arg)
3142 volatile cdkhdr_t *hdrp;
3143 stlibrd_t *brdp;
3144 int brdnr;
3146 stli_timerlist.expires = STLI_TIMEOUT;
3147 add_timer(&stli_timerlist);
3150 * Check each board and do any servicing required.
3152 for (brdnr = 0; (brdnr < stli_nrbrds); brdnr++) {
3153 brdp = stli_brds[brdnr];
3154 if (brdp == (stlibrd_t *) NULL)
3155 continue;
3156 if ((brdp->state & BST_STARTED) == 0)
3157 continue;
3159 EBRDENABLE(brdp);
3160 hdrp = (volatile cdkhdr_t *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
3161 if (hdrp->hostreq)
3162 stli_brdpoll(brdp, hdrp);
3163 EBRDDISABLE(brdp);
3167 /*****************************************************************************/
3170 * Translate the termios settings into the port setting structure of
3171 * the slave.
3174 static void stli_mkasyport(stliport_t *portp, asyport_t *pp, struct termios *tiosp)
3176 #if DEBUG
3177 printk("stli_mkasyport(portp=%x,pp=%x,tiosp=%d)\n",
3178 (int) portp, (int) pp, (int) tiosp);
3179 #endif
3181 memset(pp, 0, sizeof(asyport_t));
3184 * Start of by setting the baud, char size, parity and stop bit info.
3186 pp->baudout = tiosp->c_cflag & CBAUD;
3187 if (pp->baudout & CBAUDEX) {
3188 pp->baudout &= ~CBAUDEX;
3189 if ((pp->baudout < 1) || (pp->baudout > 4))
3190 tiosp->c_cflag &= ~CBAUDEX;
3191 else
3192 pp->baudout += 15;
3194 pp->baudout = stli_baudrates[pp->baudout];
3195 if ((tiosp->c_cflag & CBAUD) == B38400) {
3196 if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
3197 pp->baudout = 57600;
3198 else if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
3199 pp->baudout = 115200;
3200 else if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
3201 pp->baudout = 230400;
3202 else if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
3203 pp->baudout = 460800;
3204 else if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)
3205 pp->baudout = (portp->baud_base / portp->custom_divisor);
3207 if (pp->baudout > STL_MAXBAUD)
3208 pp->baudout = STL_MAXBAUD;
3209 pp->baudin = pp->baudout;
3211 switch (tiosp->c_cflag & CSIZE) {
3212 case CS5:
3213 pp->csize = 5;
3214 break;
3215 case CS6:
3216 pp->csize = 6;
3217 break;
3218 case CS7:
3219 pp->csize = 7;
3220 break;
3221 default:
3222 pp->csize = 8;
3223 break;
3226 if (tiosp->c_cflag & CSTOPB)
3227 pp->stopbs = PT_STOP2;
3228 else
3229 pp->stopbs = PT_STOP1;
3231 if (tiosp->c_cflag & PARENB) {
3232 if (tiosp->c_cflag & PARODD)
3233 pp->parity = PT_ODDPARITY;
3234 else
3235 pp->parity = PT_EVENPARITY;
3236 } else {
3237 pp->parity = PT_NOPARITY;
3241 * Set up any flow control options enabled.
3243 if (tiosp->c_iflag & IXON) {
3244 pp->flow |= F_IXON;
3245 if (tiosp->c_iflag & IXANY)
3246 pp->flow |= F_IXANY;
3248 if (tiosp->c_cflag & CRTSCTS)
3249 pp->flow |= (F_RTSFLOW | F_CTSFLOW);
3251 pp->startin = tiosp->c_cc[VSTART];
3252 pp->stopin = tiosp->c_cc[VSTOP];
3253 pp->startout = tiosp->c_cc[VSTART];
3254 pp->stopout = tiosp->c_cc[VSTOP];
3257 * Set up the RX char marking mask with those RX error types we must
3258 * catch. We can get the slave to help us out a little here, it will
3259 * ignore parity errors and breaks for us, and mark parity errors in
3260 * the data stream.
3262 if (tiosp->c_iflag & IGNPAR)
3263 pp->iflag |= FI_IGNRXERRS;
3264 if (tiosp->c_iflag & IGNBRK)
3265 pp->iflag |= FI_IGNBREAK;
3267 portp->rxmarkmsk = 0;
3268 if (tiosp->c_iflag & (INPCK | PARMRK))
3269 pp->iflag |= FI_1MARKRXERRS;
3270 if (tiosp->c_iflag & BRKINT)
3271 portp->rxmarkmsk |= BRKINT;
3274 * Set up clocal processing as required.
3276 if (tiosp->c_cflag & CLOCAL)
3277 portp->flags &= ~ASYNC_CHECK_CD;
3278 else
3279 portp->flags |= ASYNC_CHECK_CD;
3282 * Transfer any persistent flags into the asyport structure.
3284 pp->pflag = (portp->pflag & 0xffff);
3285 pp->vmin = (portp->pflag & P_RXIMIN) ? 1 : 0;
3286 pp->vtime = (portp->pflag & P_RXITIME) ? 1 : 0;
3287 pp->cc[1] = (portp->pflag & P_RXTHOLD) ? 1 : 0;
3290 /*****************************************************************************/
3293 * Construct a slave signals structure for setting the DTR and RTS
3294 * signals as specified.
3297 static void stli_mkasysigs(asysigs_t *sp, int dtr, int rts)
3299 #if DEBUG
3300 printk("stli_mkasysigs(sp=%x,dtr=%d,rts=%d)\n", (int) sp, dtr, rts);
3301 #endif
3303 memset(sp, 0, sizeof(asysigs_t));
3304 if (dtr >= 0) {
3305 sp->signal |= SG_DTR;
3306 sp->sigvalue |= ((dtr > 0) ? SG_DTR : 0);
3308 if (rts >= 0) {
3309 sp->signal |= SG_RTS;
3310 sp->sigvalue |= ((rts > 0) ? SG_RTS : 0);
3314 /*****************************************************************************/
3317 * Convert the signals returned from the slave into a local TIOCM type
3318 * signals value. We keep them locally in TIOCM format.
3321 static long stli_mktiocm(unsigned long sigvalue)
3323 long tiocm;
3325 #if DEBUG
3326 printk("stli_mktiocm(sigvalue=%x)\n", (int) sigvalue);
3327 #endif
3329 tiocm = 0;
3330 tiocm |= ((sigvalue & SG_DCD) ? TIOCM_CD : 0);
3331 tiocm |= ((sigvalue & SG_CTS) ? TIOCM_CTS : 0);
3332 tiocm |= ((sigvalue & SG_RI) ? TIOCM_RI : 0);
3333 tiocm |= ((sigvalue & SG_DSR) ? TIOCM_DSR : 0);
3334 tiocm |= ((sigvalue & SG_DTR) ? TIOCM_DTR : 0);
3335 tiocm |= ((sigvalue & SG_RTS) ? TIOCM_RTS : 0);
3336 return(tiocm);
3339 /*****************************************************************************/
3342 * All panels and ports actually attached have been worked out. All
3343 * we need to do here is set up the appropriate per port data structures.
3346 static inline int stli_initports(stlibrd_t *brdp)
3348 stliport_t *portp;
3349 int i, panelnr, panelport;
3351 #if DEBUG
3352 printk("stli_initports(brdp=%x)\n", (int) brdp);
3353 #endif
3355 for (i = 0, panelnr = 0, panelport = 0; (i < brdp->nrports); i++) {
3356 portp = (stliport_t *) stli_memalloc(sizeof(stliport_t));
3357 if (portp == (stliport_t *) NULL) {
3358 printk("STALLION: failed to allocate port structure\n");
3359 continue;
3362 memset(portp, 0, sizeof(stliport_t));
3363 portp->magic = STLI_PORTMAGIC;
3364 portp->portnr = i;
3365 portp->brdnr = brdp->brdnr;
3366 portp->panelnr = panelnr;
3367 portp->baud_base = STL_BAUDBASE;
3368 portp->close_delay = STL_CLOSEDELAY;
3369 portp->closing_wait = 30 * HZ;
3370 portp->tqhangup.routine = stli_dohangup;
3371 portp->tqhangup.data = portp;
3372 init_waitqueue_head(&portp->open_wait);
3373 init_waitqueue_head(&portp->close_wait);
3374 init_waitqueue_head(&portp->raw_wait);
3375 portp->normaltermios = stli_deftermios;
3376 portp->callouttermios = stli_deftermios;
3377 panelport++;
3378 if (panelport >= brdp->panels[panelnr]) {
3379 panelport = 0;
3380 panelnr++;
3382 brdp->ports[i] = portp;
3385 return(0);
3388 /*****************************************************************************/
3391 * All the following routines are board specific hardware operations.
3394 static void stli_ecpinit(stlibrd_t *brdp)
3396 unsigned long memconf;
3398 #if DEBUG
3399 printk("stli_ecpinit(brdp=%d)\n", (int) brdp);
3400 #endif
3402 outb(ECP_ATSTOP, (brdp->iobase + ECP_ATCONFR));
3403 udelay(10);
3404 outb(ECP_ATDISABLE, (brdp->iobase + ECP_ATCONFR));
3405 udelay(100);
3407 memconf = (brdp->memaddr & ECP_ATADDRMASK) >> ECP_ATADDRSHFT;
3408 outb(memconf, (brdp->iobase + ECP_ATMEMAR));
3411 /*****************************************************************************/
3413 static void stli_ecpenable(stlibrd_t *brdp)
3415 #if DEBUG
3416 printk("stli_ecpenable(brdp=%x)\n", (int) brdp);
3417 #endif
3418 outb(ECP_ATENABLE, (brdp->iobase + ECP_ATCONFR));
3421 /*****************************************************************************/
3423 static void stli_ecpdisable(stlibrd_t *brdp)
3425 #if DEBUG
3426 printk("stli_ecpdisable(brdp=%x)\n", (int) brdp);
3427 #endif
3428 outb(ECP_ATDISABLE, (brdp->iobase + ECP_ATCONFR));
3431 /*****************************************************************************/
3433 static char *stli_ecpgetmemptr(stlibrd_t *brdp, unsigned long offset, int line)
3435 void *ptr;
3436 unsigned char val;
3438 #if DEBUG
3439 printk("stli_ecpgetmemptr(brdp=%x,offset=%x)\n", (int) brdp,
3440 (int) offset);
3441 #endif
3443 if (offset > brdp->memsize) {
3444 printk("STALLION: shared memory pointer=%x out of range at "
3445 "line=%d(%d), brd=%d\n", (int) offset, line,
3446 __LINE__, brdp->brdnr);
3447 ptr = 0;
3448 val = 0;
3449 } else {
3450 ptr = brdp->membase + (offset % ECP_ATPAGESIZE);
3451 val = (unsigned char) (offset / ECP_ATPAGESIZE);
3453 outb(val, (brdp->iobase + ECP_ATMEMPR));
3454 return(ptr);
3457 /*****************************************************************************/
3459 static void stli_ecpreset(stlibrd_t *brdp)
3461 #if DEBUG
3462 printk("stli_ecpreset(brdp=%x)\n", (int) brdp);
3463 #endif
3465 outb(ECP_ATSTOP, (brdp->iobase + ECP_ATCONFR));
3466 udelay(10);
3467 outb(ECP_ATDISABLE, (brdp->iobase + ECP_ATCONFR));
3468 udelay(500);
3471 /*****************************************************************************/
3473 static void stli_ecpintr(stlibrd_t *brdp)
3475 #if DEBUG
3476 printk("stli_ecpintr(brdp=%x)\n", (int) brdp);
3477 #endif
3478 outb(0x1, brdp->iobase);
3481 /*****************************************************************************/
3484 * The following set of functions act on ECP EISA boards.
3487 static void stli_ecpeiinit(stlibrd_t *brdp)
3489 unsigned long memconf;
3491 #if DEBUG
3492 printk("stli_ecpeiinit(brdp=%x)\n", (int) brdp);
3493 #endif
3495 outb(0x1, (brdp->iobase + ECP_EIBRDENAB));
3496 outb(ECP_EISTOP, (brdp->iobase + ECP_EICONFR));
3497 udelay(10);
3498 outb(ECP_EIDISABLE, (brdp->iobase + ECP_EICONFR));
3499 udelay(500);
3501 memconf = (brdp->memaddr & ECP_EIADDRMASKL) >> ECP_EIADDRSHFTL;
3502 outb(memconf, (brdp->iobase + ECP_EIMEMARL));
3503 memconf = (brdp->memaddr & ECP_EIADDRMASKH) >> ECP_EIADDRSHFTH;
3504 outb(memconf, (brdp->iobase + ECP_EIMEMARH));
3507 /*****************************************************************************/
3509 static void stli_ecpeienable(stlibrd_t *brdp)
3511 outb(ECP_EIENABLE, (brdp->iobase + ECP_EICONFR));
3514 /*****************************************************************************/
3516 static void stli_ecpeidisable(stlibrd_t *brdp)
3518 outb(ECP_EIDISABLE, (brdp->iobase + ECP_EICONFR));
3521 /*****************************************************************************/
3523 static char *stli_ecpeigetmemptr(stlibrd_t *brdp, unsigned long offset, int line)
3525 void *ptr;
3526 unsigned char val;
3528 #if DEBUG
3529 printk("stli_ecpeigetmemptr(brdp=%x,offset=%x,line=%d)\n",
3530 (int) brdp, (int) offset, line);
3531 #endif
3533 if (offset > brdp->memsize) {
3534 printk("STALLION: shared memory pointer=%x out of range at "
3535 "line=%d(%d), brd=%d\n", (int) offset, line,
3536 __LINE__, brdp->brdnr);
3537 ptr = 0;
3538 val = 0;
3539 } else {
3540 ptr = brdp->membase + (offset % ECP_EIPAGESIZE);
3541 if (offset < ECP_EIPAGESIZE)
3542 val = ECP_EIENABLE;
3543 else
3544 val = ECP_EIENABLE | 0x40;
3546 outb(val, (brdp->iobase + ECP_EICONFR));
3547 return(ptr);
3550 /*****************************************************************************/
3552 static void stli_ecpeireset(stlibrd_t *brdp)
3554 outb(ECP_EISTOP, (brdp->iobase + ECP_EICONFR));
3555 udelay(10);
3556 outb(ECP_EIDISABLE, (brdp->iobase + ECP_EICONFR));
3557 udelay(500);
3560 /*****************************************************************************/
3563 * The following set of functions act on ECP MCA boards.
3566 static void stli_ecpmcenable(stlibrd_t *brdp)
3568 outb(ECP_MCENABLE, (brdp->iobase + ECP_MCCONFR));
3571 /*****************************************************************************/
3573 static void stli_ecpmcdisable(stlibrd_t *brdp)
3575 outb(ECP_MCDISABLE, (brdp->iobase + ECP_MCCONFR));
3578 /*****************************************************************************/
3580 static char *stli_ecpmcgetmemptr(stlibrd_t *brdp, unsigned long offset, int line)
3582 void *ptr;
3583 unsigned char val;
3585 if (offset > brdp->memsize) {
3586 printk("STALLION: shared memory pointer=%x out of range at "
3587 "line=%d(%d), brd=%d\n", (int) offset, line,
3588 __LINE__, brdp->brdnr);
3589 ptr = 0;
3590 val = 0;
3591 } else {
3592 ptr = brdp->membase + (offset % ECP_MCPAGESIZE);
3593 val = ((unsigned char) (offset / ECP_MCPAGESIZE)) | ECP_MCENABLE;
3595 outb(val, (brdp->iobase + ECP_MCCONFR));
3596 return(ptr);
3599 /*****************************************************************************/
3601 static void stli_ecpmcreset(stlibrd_t *brdp)
3603 outb(ECP_MCSTOP, (brdp->iobase + ECP_MCCONFR));
3604 udelay(10);
3605 outb(ECP_MCDISABLE, (brdp->iobase + ECP_MCCONFR));
3606 udelay(500);
3609 /*****************************************************************************/
3612 * The following set of functions act on ECP PCI boards.
3615 static void stli_ecppciinit(stlibrd_t *brdp)
3617 #if DEBUG
3618 printk("stli_ecppciinit(brdp=%x)\n", (int) brdp);
3619 #endif
3621 outb(ECP_PCISTOP, (brdp->iobase + ECP_PCICONFR));
3622 udelay(10);
3623 outb(0, (brdp->iobase + ECP_PCICONFR));
3624 udelay(500);
3627 /*****************************************************************************/
3629 static char *stli_ecppcigetmemptr(stlibrd_t *brdp, unsigned long offset, int line)
3631 void *ptr;
3632 unsigned char val;
3634 #if DEBUG
3635 printk("stli_ecppcigetmemptr(brdp=%x,offset=%x,line=%d)\n",
3636 (int) brdp, (int) offset, line);
3637 #endif
3639 if (offset > brdp->memsize) {
3640 printk("STALLION: shared memory pointer=%x out of range at "
3641 "line=%d(%d), board=%d\n", (int) offset, line,
3642 __LINE__, brdp->brdnr);
3643 ptr = 0;
3644 val = 0;
3645 } else {
3646 ptr = brdp->membase + (offset % ECP_PCIPAGESIZE);
3647 val = (offset / ECP_PCIPAGESIZE) << 1;
3649 outb(val, (brdp->iobase + ECP_PCICONFR));
3650 return(ptr);
3653 /*****************************************************************************/
3655 static void stli_ecppcireset(stlibrd_t *brdp)
3657 outb(ECP_PCISTOP, (brdp->iobase + ECP_PCICONFR));
3658 udelay(10);
3659 outb(0, (brdp->iobase + ECP_PCICONFR));
3660 udelay(500);
3663 /*****************************************************************************/
3666 * The following routines act on ONboards.
3669 static void stli_onbinit(stlibrd_t *brdp)
3671 unsigned long memconf;
3673 #if DEBUG
3674 printk("stli_onbinit(brdp=%d)\n", (int) brdp);
3675 #endif
3677 outb(ONB_ATSTOP, (brdp->iobase + ONB_ATCONFR));
3678 udelay(10);
3679 outb(ONB_ATDISABLE, (brdp->iobase + ONB_ATCONFR));
3680 mdelay(1000);
3682 memconf = (brdp->memaddr & ONB_ATADDRMASK) >> ONB_ATADDRSHFT;
3683 outb(memconf, (brdp->iobase + ONB_ATMEMAR));
3684 outb(0x1, brdp->iobase);
3685 mdelay(1);
3688 /*****************************************************************************/
3690 static void stli_onbenable(stlibrd_t *brdp)
3692 #if DEBUG
3693 printk("stli_onbenable(brdp=%x)\n", (int) brdp);
3694 #endif
3695 outb((brdp->enabval | ONB_ATENABLE), (brdp->iobase + ONB_ATCONFR));
3698 /*****************************************************************************/
3700 static void stli_onbdisable(stlibrd_t *brdp)
3702 #if DEBUG
3703 printk("stli_onbdisable(brdp=%x)\n", (int) brdp);
3704 #endif
3705 outb((brdp->enabval | ONB_ATDISABLE), (brdp->iobase + ONB_ATCONFR));
3708 /*****************************************************************************/
3710 static char *stli_onbgetmemptr(stlibrd_t *brdp, unsigned long offset, int line)
3712 void *ptr;
3714 #if DEBUG
3715 printk("stli_onbgetmemptr(brdp=%x,offset=%x)\n", (int) brdp,
3716 (int) offset);
3717 #endif
3719 if (offset > brdp->memsize) {
3720 printk("STALLION: shared memory pointer=%x out of range at "
3721 "line=%d(%d), brd=%d\n", (int) offset, line,
3722 __LINE__, brdp->brdnr);
3723 ptr = 0;
3724 } else {
3725 ptr = brdp->membase + (offset % ONB_ATPAGESIZE);
3727 return(ptr);
3730 /*****************************************************************************/
3732 static void stli_onbreset(stlibrd_t *brdp)
3735 #if DEBUG
3736 printk("stli_onbreset(brdp=%x)\n", (int) brdp);
3737 #endif
3739 outb(ONB_ATSTOP, (brdp->iobase + ONB_ATCONFR));
3740 udelay(10);
3741 outb(ONB_ATDISABLE, (brdp->iobase + ONB_ATCONFR));
3742 mdelay(1000);
3745 /*****************************************************************************/
3748 * The following routines act on ONboard EISA.
3751 static void stli_onbeinit(stlibrd_t *brdp)
3753 unsigned long memconf;
3755 #if DEBUG
3756 printk("stli_onbeinit(brdp=%d)\n", (int) brdp);
3757 #endif
3759 outb(0x1, (brdp->iobase + ONB_EIBRDENAB));
3760 outb(ONB_EISTOP, (brdp->iobase + ONB_EICONFR));
3761 udelay(10);
3762 outb(ONB_EIDISABLE, (brdp->iobase + ONB_EICONFR));
3763 mdelay(1000);
3765 memconf = (brdp->memaddr & ONB_EIADDRMASKL) >> ONB_EIADDRSHFTL;
3766 outb(memconf, (brdp->iobase + ONB_EIMEMARL));
3767 memconf = (brdp->memaddr & ONB_EIADDRMASKH) >> ONB_EIADDRSHFTH;
3768 outb(memconf, (brdp->iobase + ONB_EIMEMARH));
3769 outb(0x1, brdp->iobase);
3770 mdelay(1);
3773 /*****************************************************************************/
3775 static void stli_onbeenable(stlibrd_t *brdp)
3777 #if DEBUG
3778 printk("stli_onbeenable(brdp=%x)\n", (int) brdp);
3779 #endif
3780 outb(ONB_EIENABLE, (brdp->iobase + ONB_EICONFR));
3783 /*****************************************************************************/
3785 static void stli_onbedisable(stlibrd_t *brdp)
3787 #if DEBUG
3788 printk("stli_onbedisable(brdp=%x)\n", (int) brdp);
3789 #endif
3790 outb(ONB_EIDISABLE, (brdp->iobase + ONB_EICONFR));
3793 /*****************************************************************************/
3795 static char *stli_onbegetmemptr(stlibrd_t *brdp, unsigned long offset, int line)
3797 void *ptr;
3798 unsigned char val;
3800 #if DEBUG
3801 printk("stli_onbegetmemptr(brdp=%x,offset=%x,line=%d)\n",
3802 (int) brdp, (int) offset, line);
3803 #endif
3805 if (offset > brdp->memsize) {
3806 printk("STALLION: shared memory pointer=%x out of range at "
3807 "line=%d(%d), brd=%d\n", (int) offset, line,
3808 __LINE__, brdp->brdnr);
3809 ptr = 0;
3810 val = 0;
3811 } else {
3812 ptr = brdp->membase + (offset % ONB_EIPAGESIZE);
3813 if (offset < ONB_EIPAGESIZE)
3814 val = ONB_EIENABLE;
3815 else
3816 val = ONB_EIENABLE | 0x40;
3818 outb(val, (brdp->iobase + ONB_EICONFR));
3819 return(ptr);
3822 /*****************************************************************************/
3824 static void stli_onbereset(stlibrd_t *brdp)
3827 #if DEBUG
3828 printk("stli_onbereset(brdp=%x)\n", (int) brdp);
3829 #endif
3831 outb(ONB_EISTOP, (brdp->iobase + ONB_EICONFR));
3832 udelay(10);
3833 outb(ONB_EIDISABLE, (brdp->iobase + ONB_EICONFR));
3834 mdelay(1000);
3837 /*****************************************************************************/
3840 * The following routines act on Brumby boards.
3843 static void stli_bbyinit(stlibrd_t *brdp)
3846 #if DEBUG
3847 printk("stli_bbyinit(brdp=%d)\n", (int) brdp);
3848 #endif
3850 outb(BBY_ATSTOP, (brdp->iobase + BBY_ATCONFR));
3851 udelay(10);
3852 outb(0, (brdp->iobase + BBY_ATCONFR));
3853 mdelay(1000);
3854 outb(0x1, brdp->iobase);
3855 mdelay(1);
3858 /*****************************************************************************/
3860 static char *stli_bbygetmemptr(stlibrd_t *brdp, unsigned long offset, int line)
3862 void *ptr;
3863 unsigned char val;
3865 #if DEBUG
3866 printk("stli_bbygetmemptr(brdp=%x,offset=%x)\n", (int) brdp,
3867 (int) offset);
3868 #endif
3870 if (offset > brdp->memsize) {
3871 printk("STALLION: shared memory pointer=%x out of range at "
3872 "line=%d(%d), brd=%d\n", (int) offset, line,
3873 __LINE__, brdp->brdnr);
3874 ptr = 0;
3875 val = 0;
3876 } else {
3877 ptr = brdp->membase + (offset % BBY_PAGESIZE);
3878 val = (unsigned char) (offset / BBY_PAGESIZE);
3880 outb(val, (brdp->iobase + BBY_ATCONFR));
3881 return(ptr);
3884 /*****************************************************************************/
3886 static void stli_bbyreset(stlibrd_t *brdp)
3889 #if DEBUG
3890 printk("stli_bbyreset(brdp=%x)\n", (int) brdp);
3891 #endif
3893 outb(BBY_ATSTOP, (brdp->iobase + BBY_ATCONFR));
3894 udelay(10);
3895 outb(0, (brdp->iobase + BBY_ATCONFR));
3896 mdelay(1000);
3899 /*****************************************************************************/
3902 * The following routines act on original old Stallion boards.
3905 static void stli_stalinit(stlibrd_t *brdp)
3908 #if DEBUG
3909 printk("stli_stalinit(brdp=%d)\n", (int) brdp);
3910 #endif
3912 outb(0x1, brdp->iobase);
3913 mdelay(1000);
3916 /*****************************************************************************/
3918 static char *stli_stalgetmemptr(stlibrd_t *brdp, unsigned long offset, int line)
3920 void *ptr;
3922 #if DEBUG
3923 printk("stli_stalgetmemptr(brdp=%x,offset=%x)\n", (int) brdp,
3924 (int) offset);
3925 #endif
3927 if (offset > brdp->memsize) {
3928 printk("STALLION: shared memory pointer=%x out of range at "
3929 "line=%d(%d), brd=%d\n", (int) offset, line,
3930 __LINE__, brdp->brdnr);
3931 ptr = 0;
3932 } else {
3933 ptr = brdp->membase + (offset % STAL_PAGESIZE);
3935 return(ptr);
3938 /*****************************************************************************/
3940 static void stli_stalreset(stlibrd_t *brdp)
3942 volatile unsigned long *vecp;
3944 #if DEBUG
3945 printk("stli_stalreset(brdp=%x)\n", (int) brdp);
3946 #endif
3948 vecp = (volatile unsigned long *) (brdp->membase + 0x30);
3949 *vecp = 0xffff0000;
3950 outb(0, brdp->iobase);
3951 mdelay(1000);
3954 /*****************************************************************************/
3957 * Try to find an ECP board and initialize it. This handles only ECP
3958 * board types.
3961 static inline int stli_initecp(stlibrd_t *brdp)
3963 cdkecpsig_t sig;
3964 cdkecpsig_t *sigsp;
3965 unsigned int status, nxtid;
3966 char *name;
3967 int panelnr, nrports;
3969 #if DEBUG
3970 printk("stli_initecp(brdp=%x)\n", (int) brdp);
3971 #endif
3974 * Do a basic sanity check on the IO and memory addresses.
3976 if ((brdp->iobase == 0) || (brdp->memaddr == 0))
3977 return(-ENODEV);
3979 brdp->iosize = ECP_IOSIZE;
3980 if (check_region(brdp->iobase, brdp->iosize))
3981 printk("STALLION: Warning, board %d I/O address %x conflicts "
3982 "with another device\n", brdp->brdnr, brdp->iobase);
3985 * Based on the specific board type setup the common vars to access
3986 * and enable shared memory. Set all board specific information now
3987 * as well.
3989 switch (brdp->brdtype) {
3990 case BRD_ECP:
3991 brdp->membase = (void *) brdp->memaddr;
3992 brdp->memsize = ECP_MEMSIZE;
3993 brdp->pagesize = ECP_ATPAGESIZE;
3994 brdp->init = stli_ecpinit;
3995 brdp->enable = stli_ecpenable;
3996 brdp->reenable = stli_ecpenable;
3997 brdp->disable = stli_ecpdisable;
3998 brdp->getmemptr = stli_ecpgetmemptr;
3999 brdp->intr = stli_ecpintr;
4000 brdp->reset = stli_ecpreset;
4001 name = "serial(EC8/64)";
4002 break;
4004 case BRD_ECPE:
4005 brdp->membase = (void *) brdp->memaddr;
4006 brdp->memsize = ECP_MEMSIZE;
4007 brdp->pagesize = ECP_EIPAGESIZE;
4008 brdp->init = stli_ecpeiinit;
4009 brdp->enable = stli_ecpeienable;
4010 brdp->reenable = stli_ecpeienable;
4011 brdp->disable = stli_ecpeidisable;
4012 brdp->getmemptr = stli_ecpeigetmemptr;
4013 brdp->intr = stli_ecpintr;
4014 brdp->reset = stli_ecpeireset;
4015 name = "serial(EC8/64-EI)";
4016 break;
4018 case BRD_ECPMC:
4019 brdp->membase = (void *) brdp->memaddr;
4020 brdp->memsize = ECP_MEMSIZE;
4021 brdp->pagesize = ECP_MCPAGESIZE;
4022 brdp->init = NULL;
4023 brdp->enable = stli_ecpmcenable;
4024 brdp->reenable = stli_ecpmcenable;
4025 brdp->disable = stli_ecpmcdisable;
4026 brdp->getmemptr = stli_ecpmcgetmemptr;
4027 brdp->intr = stli_ecpintr;
4028 brdp->reset = stli_ecpmcreset;
4029 name = "serial(EC8/64-MCA)";
4030 break;
4032 case BRD_ECPPCI:
4033 brdp->membase = (void *) brdp->memaddr;
4034 brdp->memsize = ECP_PCIMEMSIZE;
4035 brdp->pagesize = ECP_PCIPAGESIZE;
4036 brdp->init = stli_ecppciinit;
4037 brdp->enable = NULL;
4038 brdp->reenable = NULL;
4039 brdp->disable = NULL;
4040 brdp->getmemptr = stli_ecppcigetmemptr;
4041 brdp->intr = stli_ecpintr;
4042 brdp->reset = stli_ecppcireset;
4043 name = "serial(EC/RA-PCI)";
4044 break;
4046 default:
4047 return(-EINVAL);
4051 * The per-board operations structure is all set up, so now let's go
4052 * and get the board operational. Firstly initialize board configuration
4053 * registers. Set the memory mapping info so we can get at the boards
4054 * shared memory.
4056 EBRDINIT(brdp);
4058 brdp->membase = ioremap(brdp->memaddr, brdp->memsize);
4059 if (brdp->membase == (void *) NULL)
4060 return(-ENOMEM);
4063 * Now that all specific code is set up, enable the shared memory and
4064 * look for the a signature area that will tell us exactly what board
4065 * this is, and what it is connected to it.
4067 EBRDENABLE(brdp);
4068 sigsp = (cdkecpsig_t *) EBRDGETMEMPTR(brdp, CDK_SIGADDR);
4069 memcpy(&sig, sigsp, sizeof(cdkecpsig_t));
4070 EBRDDISABLE(brdp);
4072 #if 0
4073 printk("%s(%d): sig-> magic=%x rom=%x panel=%x,%x,%x,%x,%x,%x,%x,%x\n",
4074 __FILE__, __LINE__, (int) sig.magic, sig.romver, sig.panelid[0],
4075 (int) sig.panelid[1], (int) sig.panelid[2],
4076 (int) sig.panelid[3], (int) sig.panelid[4],
4077 (int) sig.panelid[5], (int) sig.panelid[6],
4078 (int) sig.panelid[7]);
4079 #endif
4081 if (sig.magic != ECP_MAGIC)
4082 return(-ENODEV);
4085 * Scan through the signature looking at the panels connected to the
4086 * board. Calculate the total number of ports as we go.
4088 for (panelnr = 0, nxtid = 0; (panelnr < STL_MAXPANELS); panelnr++) {
4089 status = sig.panelid[nxtid];
4090 if ((status & ECH_PNLIDMASK) != nxtid)
4091 break;
4093 brdp->panelids[panelnr] = status;
4094 nrports = (status & ECH_PNL16PORT) ? 16 : 8;
4095 if ((nrports == 16) && ((status & ECH_PNLXPID) == 0))
4096 nxtid++;
4097 brdp->panels[panelnr] = nrports;
4098 brdp->nrports += nrports;
4099 nxtid++;
4100 brdp->nrpanels++;
4103 request_region(brdp->iobase, brdp->iosize, name);
4104 brdp->state |= BST_FOUND;
4105 return(0);
4108 /*****************************************************************************/
4111 * Try to find an ONboard, Brumby or Stallion board and initialize it.
4112 * This handles only these board types.
4115 static inline int stli_initonb(stlibrd_t *brdp)
4117 cdkonbsig_t sig;
4118 cdkonbsig_t *sigsp;
4119 char *name;
4120 int i;
4122 #if DEBUG
4123 printk("stli_initonb(brdp=%x)\n", (int) brdp);
4124 #endif
4127 * Do a basic sanity check on the IO and memory addresses.
4129 if ((brdp->iobase == 0) || (brdp->memaddr == 0))
4130 return(-ENODEV);
4132 brdp->iosize = ONB_IOSIZE;
4133 if (check_region(brdp->iobase, brdp->iosize))
4134 printk("STALLION: Warning, board %d I/O address %x conflicts "
4135 "with another device\n", brdp->brdnr, brdp->iobase);
4138 * Based on the specific board type setup the common vars to access
4139 * and enable shared memory. Set all board specific information now
4140 * as well.
4142 switch (brdp->brdtype) {
4143 case BRD_ONBOARD:
4144 case BRD_ONBOARD32:
4145 case BRD_ONBOARD2:
4146 case BRD_ONBOARD2_32:
4147 case BRD_ONBOARDRS:
4148 brdp->membase = (void *) brdp->memaddr;
4149 brdp->memsize = ONB_MEMSIZE;
4150 brdp->pagesize = ONB_ATPAGESIZE;
4151 brdp->init = stli_onbinit;
4152 brdp->enable = stli_onbenable;
4153 brdp->reenable = stli_onbenable;
4154 brdp->disable = stli_onbdisable;
4155 brdp->getmemptr = stli_onbgetmemptr;
4156 brdp->intr = stli_ecpintr;
4157 brdp->reset = stli_onbreset;
4158 if (brdp->memaddr > 0x100000)
4159 brdp->enabval = ONB_MEMENABHI;
4160 else
4161 brdp->enabval = ONB_MEMENABLO;
4162 name = "serial(ONBoard)";
4163 break;
4165 case BRD_ONBOARDE:
4166 brdp->membase = (void *) brdp->memaddr;
4167 brdp->memsize = ONB_EIMEMSIZE;
4168 brdp->pagesize = ONB_EIPAGESIZE;
4169 brdp->init = stli_onbeinit;
4170 brdp->enable = stli_onbeenable;
4171 brdp->reenable = stli_onbeenable;
4172 brdp->disable = stli_onbedisable;
4173 brdp->getmemptr = stli_onbegetmemptr;
4174 brdp->intr = stli_ecpintr;
4175 brdp->reset = stli_onbereset;
4176 name = "serial(ONBoard/E)";
4177 break;
4179 case BRD_BRUMBY4:
4180 case BRD_BRUMBY8:
4181 case BRD_BRUMBY16:
4182 brdp->membase = (void *) brdp->memaddr;
4183 brdp->memsize = BBY_MEMSIZE;
4184 brdp->pagesize = BBY_PAGESIZE;
4185 brdp->init = stli_bbyinit;
4186 brdp->enable = NULL;
4187 brdp->reenable = NULL;
4188 brdp->disable = NULL;
4189 brdp->getmemptr = stli_bbygetmemptr;
4190 brdp->intr = stli_ecpintr;
4191 brdp->reset = stli_bbyreset;
4192 name = "serial(Brumby)";
4193 break;
4195 case BRD_STALLION:
4196 brdp->membase = (void *) brdp->memaddr;
4197 brdp->memsize = STAL_MEMSIZE;
4198 brdp->pagesize = STAL_PAGESIZE;
4199 brdp->init = stli_stalinit;
4200 brdp->enable = NULL;
4201 brdp->reenable = NULL;
4202 brdp->disable = NULL;
4203 brdp->getmemptr = stli_stalgetmemptr;
4204 brdp->intr = stli_ecpintr;
4205 brdp->reset = stli_stalreset;
4206 name = "serial(Stallion)";
4207 break;
4209 default:
4210 return(-EINVAL);
4214 * The per-board operations structure is all set up, so now let's go
4215 * and get the board operational. Firstly initialize board configuration
4216 * registers. Set the memory mapping info so we can get at the boards
4217 * shared memory.
4219 EBRDINIT(brdp);
4221 brdp->membase = ioremap(brdp->memaddr, brdp->memsize);
4222 if (brdp->membase == (void *) NULL)
4223 return(-ENOMEM);
4226 * Now that all specific code is set up, enable the shared memory and
4227 * look for the a signature area that will tell us exactly what board
4228 * this is, and how many ports.
4230 EBRDENABLE(brdp);
4231 sigsp = (cdkonbsig_t *) EBRDGETMEMPTR(brdp, CDK_SIGADDR);
4232 memcpy(&sig, sigsp, sizeof(cdkonbsig_t));
4233 EBRDDISABLE(brdp);
4235 #if 0
4236 printk("%s(%d): sig-> magic=%x:%x:%x:%x romver=%x amask=%x:%x:%x\n",
4237 __FILE__, __LINE__, sig.magic0, sig.magic1, sig.magic2,
4238 sig.magic3, sig.romver, sig.amask0, sig.amask1, sig.amask2);
4239 #endif
4241 if ((sig.magic0 != ONB_MAGIC0) || (sig.magic1 != ONB_MAGIC1) ||
4242 (sig.magic2 != ONB_MAGIC2) || (sig.magic3 != ONB_MAGIC3))
4243 return(-ENODEV);
4246 * Scan through the signature alive mask and calculate how many ports
4247 * there are on this board.
4249 brdp->nrpanels = 1;
4250 if (sig.amask1) {
4251 brdp->nrports = 32;
4252 } else {
4253 for (i = 0; (i < 16); i++) {
4254 if (((sig.amask0 << i) & 0x8000) == 0)
4255 break;
4257 brdp->nrports = i;
4259 brdp->panels[0] = brdp->nrports;
4261 request_region(brdp->iobase, brdp->iosize, name);
4262 brdp->state |= BST_FOUND;
4263 return(0);
4266 /*****************************************************************************/
4269 * Start up a running board. This routine is only called after the
4270 * code has been down loaded to the board and is operational. It will
4271 * read in the memory map, and get the show on the road...
4274 static int stli_startbrd(stlibrd_t *brdp)
4276 volatile cdkhdr_t *hdrp;
4277 volatile cdkmem_t *memp;
4278 volatile cdkasy_t *ap;
4279 unsigned long flags;
4280 stliport_t *portp;
4281 int portnr, nrdevs, i, rc;
4283 #if DEBUG
4284 printk("stli_startbrd(brdp=%x)\n", (int) brdp);
4285 #endif
4287 rc = 0;
4289 save_flags(flags);
4290 cli();
4291 EBRDENABLE(brdp);
4292 hdrp = (volatile cdkhdr_t *) EBRDGETMEMPTR(brdp, CDK_CDKADDR);
4293 nrdevs = hdrp->nrdevs;
4295 #if 0
4296 printk("%s(%d): CDK version %d.%d.%d --> "
4297 "nrdevs=%d memp=%x hostp=%x slavep=%x\n",
4298 __FILE__, __LINE__, hdrp->ver_release, hdrp->ver_modification,
4299 hdrp->ver_fix, nrdevs, (int) hdrp->memp, (int) hdrp->hostp,
4300 (int) hdrp->slavep);
4301 #endif
4303 if (nrdevs < (brdp->nrports + 1)) {
4304 printk("STALLION: slave failed to allocate memory for all "
4305 "devices, devices=%d\n", nrdevs);
4306 brdp->nrports = nrdevs - 1;
4308 brdp->nrdevs = nrdevs;
4309 brdp->hostoffset = hdrp->hostp - CDK_CDKADDR;
4310 brdp->slaveoffset = hdrp->slavep - CDK_CDKADDR;
4311 brdp->bitsize = (nrdevs + 7) / 8;
4312 memp = (volatile cdkmem_t *) hdrp->memp;
4313 if (((unsigned long) memp) > brdp->memsize) {
4314 printk("STALLION: corrupted shared memory region?\n");
4315 rc = -EIO;
4316 goto stli_donestartup;
4318 memp = (volatile cdkmem_t *) EBRDGETMEMPTR(brdp, (unsigned long) memp);
4319 if (memp->dtype != TYP_ASYNCTRL) {
4320 printk("STALLION: no slave control device found\n");
4321 goto stli_donestartup;
4323 memp++;
4326 * Cycle through memory allocation of each port. We are guaranteed to
4327 * have all ports inside the first page of slave window, so no need to
4328 * change pages while reading memory map.
4330 for (i = 1, portnr = 0; (i < nrdevs); i++, portnr++, memp++) {
4331 if (memp->dtype != TYP_ASYNC)
4332 break;
4333 portp = brdp->ports[portnr];
4334 if (portp == (stliport_t *) NULL)
4335 break;
4336 portp->devnr = i;
4337 portp->addr = memp->offset;
4338 portp->reqbit = (unsigned char) (0x1 << (i * 8 / nrdevs));
4339 portp->portidx = (unsigned char) (i / 8);
4340 portp->portbit = (unsigned char) (0x1 << (i % 8));
4343 hdrp->slavereq = 0xff;
4346 * For each port setup a local copy of the RX and TX buffer offsets
4347 * and sizes. We do this separate from the above, because we need to
4348 * move the shared memory page...
4350 for (i = 1, portnr = 0; (i < nrdevs); i++, portnr++) {
4351 portp = brdp->ports[portnr];
4352 if (portp == (stliport_t *) NULL)
4353 break;
4354 if (portp->addr == 0)
4355 break;
4356 ap = (volatile cdkasy_t *) EBRDGETMEMPTR(brdp, portp->addr);
4357 if (ap != (volatile cdkasy_t *) NULL) {
4358 portp->rxsize = ap->rxq.size;
4359 portp->txsize = ap->txq.size;
4360 portp->rxoffset = ap->rxq.offset;
4361 portp->txoffset = ap->txq.offset;
4365 stli_donestartup:
4366 EBRDDISABLE(brdp);
4367 restore_flags(flags);
4369 if (rc == 0)
4370 brdp->state |= BST_STARTED;
4372 if (! stli_timeron) {
4373 stli_timeron++;
4374 stli_timerlist.expires = STLI_TIMEOUT;
4375 add_timer(&stli_timerlist);
4378 return(rc);
4381 /*****************************************************************************/
4384 * Probe and initialize the specified board.
4387 static int __init stli_brdinit(stlibrd_t *brdp)
4389 #if DEBUG
4390 printk("stli_brdinit(brdp=%x)\n", (int) brdp);
4391 #endif
4393 stli_brds[brdp->brdnr] = brdp;
4395 switch (brdp->brdtype) {
4396 case BRD_ECP:
4397 case BRD_ECPE:
4398 case BRD_ECPMC:
4399 case BRD_ECPPCI:
4400 stli_initecp(brdp);
4401 break;
4402 case BRD_ONBOARD:
4403 case BRD_ONBOARDE:
4404 case BRD_ONBOARD2:
4405 case BRD_ONBOARD32:
4406 case BRD_ONBOARD2_32:
4407 case BRD_ONBOARDRS:
4408 case BRD_BRUMBY4:
4409 case BRD_BRUMBY8:
4410 case BRD_BRUMBY16:
4411 case BRD_STALLION:
4412 stli_initonb(brdp);
4413 break;
4414 case BRD_EASYIO:
4415 case BRD_ECH:
4416 case BRD_ECHMC:
4417 case BRD_ECHPCI:
4418 printk("STALLION: %s board type not supported in this driver\n",
4419 stli_brdnames[brdp->brdtype]);
4420 return(ENODEV);
4421 default:
4422 printk("STALLION: board=%d is unknown board type=%d\n",
4423 brdp->brdnr, brdp->brdtype);
4424 return(ENODEV);
4427 if ((brdp->state & BST_FOUND) == 0) {
4428 printk("STALLION: %s board not found, board=%d io=%x mem=%x\n",
4429 stli_brdnames[brdp->brdtype], brdp->brdnr,
4430 brdp->iobase, (int) brdp->memaddr);
4431 return(ENODEV);
4434 stli_initports(brdp);
4435 printk("STALLION: %s found, board=%d io=%x mem=%x "
4436 "nrpanels=%d nrports=%d\n", stli_brdnames[brdp->brdtype],
4437 brdp->brdnr, brdp->iobase, (int) brdp->memaddr,
4438 brdp->nrpanels, brdp->nrports);
4439 return(0);
4442 /*****************************************************************************/
4445 * Probe around trying to find where the EISA boards shared memory
4446 * might be. This is a bit if hack, but it is the best we can do.
4449 static inline int stli_eisamemprobe(stlibrd_t *brdp)
4451 cdkecpsig_t ecpsig, *ecpsigp;
4452 cdkonbsig_t onbsig, *onbsigp;
4453 int i, foundit;
4455 #if DEBUG
4456 printk("stli_eisamemprobe(brdp=%x)\n", (int) brdp);
4457 #endif
4460 * First up we reset the board, to get it into a known state. There
4461 * is only 2 board types here we need to worry about. Don;t use the
4462 * standard board init routine here, it programs up the shared
4463 * memory address, and we don't know it yet...
4465 if (brdp->brdtype == BRD_ECPE) {
4466 outb(0x1, (brdp->iobase + ECP_EIBRDENAB));
4467 outb(ECP_EISTOP, (brdp->iobase + ECP_EICONFR));
4468 udelay(10);
4469 outb(ECP_EIDISABLE, (brdp->iobase + ECP_EICONFR));
4470 udelay(500);
4471 stli_ecpeienable(brdp);
4472 } else if (brdp->brdtype == BRD_ONBOARDE) {
4473 outb(0x1, (brdp->iobase + ONB_EIBRDENAB));
4474 outb(ONB_EISTOP, (brdp->iobase + ONB_EICONFR));
4475 udelay(10);
4476 outb(ONB_EIDISABLE, (brdp->iobase + ONB_EICONFR));
4477 mdelay(100);
4478 outb(0x1, brdp->iobase);
4479 mdelay(1);
4480 stli_onbeenable(brdp);
4481 } else {
4482 return(-ENODEV);
4485 foundit = 0;
4486 brdp->memsize = ECP_MEMSIZE;
4489 * Board shared memory is enabled, so now we have a poke around and
4490 * see if we can find it.
4492 for (i = 0; (i < stli_eisamempsize); i++) {
4493 brdp->memaddr = stli_eisamemprobeaddrs[i];
4494 brdp->membase = (void *) brdp->memaddr;
4495 brdp->membase = ioremap(brdp->memaddr, brdp->memsize);
4496 if (brdp->membase == (void *) NULL)
4497 continue;
4499 if (brdp->brdtype == BRD_ECPE) {
4500 ecpsigp = (cdkecpsig_t *) stli_ecpeigetmemptr(brdp,
4501 CDK_SIGADDR, __LINE__);
4502 memcpy(&ecpsig, ecpsigp, sizeof(cdkecpsig_t));
4503 if (ecpsig.magic == ECP_MAGIC)
4504 foundit = 1;
4505 } else {
4506 onbsigp = (cdkonbsig_t *) stli_onbegetmemptr(brdp,
4507 CDK_SIGADDR, __LINE__);
4508 memcpy(&onbsig, onbsigp, sizeof(cdkonbsig_t));
4509 if ((onbsig.magic0 == ONB_MAGIC0) &&
4510 (onbsig.magic1 == ONB_MAGIC1) &&
4511 (onbsig.magic2 == ONB_MAGIC2) &&
4512 (onbsig.magic3 == ONB_MAGIC3))
4513 foundit = 1;
4516 iounmap(brdp->membase);
4517 if (foundit)
4518 break;
4522 * Regardless of whether we found the shared memory or not we must
4523 * disable the region. After that return success or failure.
4525 if (brdp->brdtype == BRD_ECPE)
4526 stli_ecpeidisable(brdp);
4527 else
4528 stli_onbedisable(brdp);
4530 if (! foundit) {
4531 brdp->memaddr = 0;
4532 brdp->membase = 0;
4533 printk("STALLION: failed to probe shared memory region for "
4534 "%s in EISA slot=%d\n", stli_brdnames[brdp->brdtype],
4535 (brdp->iobase >> 12));
4536 return(-ENODEV);
4538 return(0);
4541 /*****************************************************************************/
4544 * Probe around and try to find any EISA boards in system. The biggest
4545 * problem here is finding out what memory address is associated with
4546 * an EISA board after it is found. The registers of the ECPE and
4547 * ONboardE are not readable - so we can't read them from there. We
4548 * don't have access to the EISA CMOS (or EISA BIOS) so we don't
4549 * actually have any way to find out the real value. The best we can
4550 * do is go probing around in the usual places hoping we can find it.
4553 static inline int stli_findeisabrds()
4555 stlibrd_t *brdp;
4556 unsigned int iobase, eid;
4557 int i;
4559 #if DEBUG
4560 printk("stli_findeisabrds()\n");
4561 #endif
4564 * Firstly check if this is an EISA system. Do this by probing for
4565 * the system board EISA ID. If this is not an EISA system then
4566 * don't bother going any further!
4568 outb(0xff, 0xc80);
4569 if (inb(0xc80) == 0xff)
4570 return(0);
4573 * Looks like an EISA system, so go searching for EISA boards.
4575 for (iobase = 0x1000; (iobase <= 0xc000); iobase += 0x1000) {
4576 outb(0xff, (iobase + 0xc80));
4577 eid = inb(iobase + 0xc80);
4578 eid |= inb(iobase + 0xc81) << 8;
4579 if (eid != STL_EISAID)
4580 continue;
4583 * We have found a board. Need to check if this board was
4584 * statically configured already (just in case!).
4586 for (i = 0; (i < STL_MAXBRDS); i++) {
4587 brdp = stli_brds[i];
4588 if (brdp == (stlibrd_t *) NULL)
4589 continue;
4590 if (brdp->iobase == iobase)
4591 break;
4593 if (i < STL_MAXBRDS)
4594 continue;
4597 * We have found a Stallion board and it is not configured already.
4598 * Allocate a board structure and initialize it.
4600 if ((brdp = stli_allocbrd()) == (stlibrd_t *) NULL)
4601 return(-ENOMEM);
4602 if ((brdp->brdnr = stli_getbrdnr()) < 0)
4603 return(-ENOMEM);
4604 eid = inb(iobase + 0xc82);
4605 if (eid == ECP_EISAID)
4606 brdp->brdtype = BRD_ECPE;
4607 else if (eid == ONB_EISAID)
4608 brdp->brdtype = BRD_ONBOARDE;
4609 else
4610 brdp->brdtype = BRD_UNKNOWN;
4611 brdp->iobase = iobase;
4612 outb(0x1, (iobase + 0xc84));
4613 if (stli_eisamemprobe(brdp))
4614 outb(0, (iobase + 0xc84));
4615 stli_brdinit(brdp);
4618 return(0);
4621 /*****************************************************************************/
4624 * Find the next available board number that is free.
4627 static inline int stli_getbrdnr()
4629 int i;
4631 for (i = 0; (i < STL_MAXBRDS); i++) {
4632 if (stli_brds[i] == (stlibrd_t *) NULL) {
4633 if (i >= stli_nrbrds)
4634 stli_nrbrds = i + 1;
4635 return(i);
4638 return(-1);
4641 /*****************************************************************************/
4643 #ifdef CONFIG_PCI
4646 * We have a Stallion board. Allocate a board structure and
4647 * initialize it. Read its IO and MEMORY resources from PCI
4648 * configuration space.
4651 static inline int stli_initpcibrd(int brdtype, struct pci_dev *devp)
4653 stlibrd_t *brdp;
4655 #if DEBUG
4656 printk("stli_initpcibrd(brdtype=%d,busnr=%x,devnr=%x)\n", brdtype,
4657 dev->bus->number, dev->devfn);
4658 #endif
4660 if ((brdp = stli_allocbrd()) == (stlibrd_t *) NULL)
4661 return(-ENOMEM);
4662 if ((brdp->brdnr = stli_getbrdnr()) < 0) {
4663 printk("STALLION: too many boards found, "
4664 "maximum supported %d\n", STL_MAXBRDS);
4665 return(0);
4667 brdp->brdtype = brdtype;
4669 #if DEBUG
4670 printk("%s(%d): BAR[]=%x,%x,%x,%x\n", __FILE__, __LINE__,
4671 devp->resource[0].start, devp->resource[1].start,
4672 devp->resource[2].start, devp->resource[3].start);
4673 #endif
4676 * We have all resources from the board, so lets setup the actual
4677 * board structure now.
4679 brdp->iobase = (devp->resource[3].start & PCI_BASE_ADDRESS_IO_MASK);
4680 brdp->memaddr = (devp->resource[2].start & PCI_BASE_ADDRESS_MEM_MASK);
4681 stli_brdinit(brdp);
4683 return(0);
4686 /*****************************************************************************/
4689 * Find all Stallion PCI boards that might be installed. Initialize each
4690 * one as it is found.
4693 static inline int stli_findpcibrds()
4695 struct pci_dev *dev = NULL;
4696 int rc;
4698 #if DEBUG
4699 printk("stli_findpcibrds()\n");
4700 #endif
4702 if (! pci_present())
4703 return(0);
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);
4711 return(0);
4714 #endif
4716 /*****************************************************************************/
4719 * Allocate a new board structure. Fill out the basic info in it.
4722 static stlibrd_t *stli_allocbrd()
4724 stlibrd_t *brdp;
4726 brdp = (stlibrd_t *) stli_memalloc(sizeof(stlibrd_t));
4727 if (brdp == (stlibrd_t *) NULL) {
4728 printk("STALLION: failed to allocate memory (size=%d)\n",
4729 sizeof(stlibrd_t));
4730 return((stlibrd_t *) NULL);
4733 memset(brdp, 0, sizeof(stlibrd_t));
4734 brdp->magic = STLI_BOARDMAGIC;
4735 return(brdp);
4738 /*****************************************************************************/
4741 * Scan through all the boards in the configuration and see what we
4742 * can find.
4745 static inline int stli_initbrds()
4747 stlibrd_t *brdp, *nxtbrdp;
4748 stlconf_t *confp;
4749 int i, j;
4751 #if DEBUG
4752 printk("stli_initbrds()\n");
4753 #endif
4755 if (stli_nrbrds > STL_MAXBRDS) {
4756 printk("STALLION: too many boards in configuration table, "
4757 "truncating to %d\n", STL_MAXBRDS);
4758 stli_nrbrds = STL_MAXBRDS;
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.
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);
4781 * Static configuration table done, so now use dynamic methods to
4782 * see if any more boards should be configured.
4784 #ifdef MODULE
4785 stli_argbrds();
4786 #endif
4787 if (stli_eisaprobe)
4788 stli_findeisabrds();
4789 #ifdef CONFIG_PCI
4790 stli_findpcibrds();
4791 #endif
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.
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;
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;
4831 return(0);
4834 /*****************************************************************************/
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 :-)
4842 static ssize_t stli_memread(struct file *fp, char *buf, size_t count, loff_t *offp)
4844 unsigned long flags;
4845 void *memptr;
4846 stlibrd_t *brdp;
4847 int brdnr, size, n;
4849 #if DEBUG
4850 printk("stli_memread(fp=%x,buf=%x,count=%x,offp=%x)\n", (int) fp,
4851 (int) buf, count, (int) offp);
4852 #endif
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);
4865 size = MIN(count, (brdp->memsize - fp->f_pos));
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;
4878 EBRDDISABLE(brdp);
4879 restore_flags(flags);
4881 return(count);
4884 /*****************************************************************************/
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 :-)
4892 static ssize_t stli_memwrite(struct file *fp, const char *buf, size_t count, loff_t *offp)
4894 unsigned long flags;
4895 void *memptr;
4896 stlibrd_t *brdp;
4897 char *chbuf;
4898 int brdnr, size, n;
4900 #if DEBUG
4901 printk("stli_memwrite(fp=%x,buf=%x,count=%x,offp=%x)\n", (int) fp,
4902 (int) buf, count, (int) offp);
4903 #endif
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);
4916 chbuf = (char *) buf;
4917 size = MIN(count, (brdp->memsize - fp->f_pos));
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;
4930 EBRDDISABLE(brdp);
4931 restore_flags(flags);
4933 return(count);
4936 /*****************************************************************************/
4939 * Return the board stats structure to user app.
4942 static int stli_getbrdstats(combrd_t *bp)
4944 stlibrd_t *brdp;
4945 int i;
4947 copy_from_user(&stli_brdstats, bp, sizeof(combrd_t));
4948 if (stli_brdstats.brd >= STL_MAXBRDS)
4949 return(-ENODEV);
4950 brdp = stli_brds[stli_brdstats.brd];
4951 if (brdp == (stlibrd_t *) NULL)
4952 return(-ENODEV);
4954 memset(&stli_brdstats, 0, sizeof(combrd_t));
4955 stli_brdstats.brd = brdp->brdnr;
4956 stli_brdstats.type = brdp->brdtype;
4957 stli_brdstats.hwid = 0;
4958 stli_brdstats.state = brdp->state;
4959 stli_brdstats.ioaddr = brdp->iobase;
4960 stli_brdstats.memaddr = brdp->memaddr;
4961 stli_brdstats.nrpanels = brdp->nrpanels;
4962 stli_brdstats.nrports = brdp->nrports;
4963 for (i = 0; (i < brdp->nrpanels); i++) {
4964 stli_brdstats.panels[i].panel = i;
4965 stli_brdstats.panels[i].hwid = brdp->panelids[i];
4966 stli_brdstats.panels[i].nrports = brdp->panels[i];
4969 copy_to_user(bp, &stli_brdstats, sizeof(combrd_t));
4970 return(0);
4973 /*****************************************************************************/
4976 * Resolve the referenced port number into a port struct pointer.
4979 static stliport_t *stli_getport(int brdnr, int panelnr, int portnr)
4981 stlibrd_t *brdp;
4982 int i;
4984 if ((brdnr < 0) || (brdnr >= STL_MAXBRDS))
4985 return((stliport_t *) NULL);
4986 brdp = stli_brds[brdnr];
4987 if (brdp == (stlibrd_t *) NULL)
4988 return((stliport_t *) NULL);
4989 for (i = 0; (i < panelnr); i++)
4990 portnr += brdp->panels[i];
4991 if ((portnr < 0) || (portnr >= brdp->nrports))
4992 return((stliport_t *) NULL);
4993 return(brdp->ports[portnr]);
4996 /*****************************************************************************/
4999 * Return the port stats structure to user app. A NULL port struct
5000 * pointer passed in means that we need to find out from the app
5001 * what port to get stats for (used through board control device).
5004 static int stli_portcmdstats(stliport_t *portp)
5006 unsigned long flags;
5007 stlibrd_t *brdp;
5008 int rc;
5010 memset(&stli_comstats, 0, sizeof(comstats_t));
5012 if (portp == (stliport_t *) NULL)
5013 return(-ENODEV);
5014 brdp = stli_brds[portp->brdnr];
5015 if (brdp == (stlibrd_t *) NULL)
5016 return(-ENODEV);
5018 if (brdp->state & BST_STARTED) {
5019 if ((rc = stli_cmdwait(brdp, portp, A_GETSTATS,
5020 &stli_cdkstats, sizeof(asystats_t), 1)) < 0)
5021 return(rc);
5022 } else {
5023 memset(&stli_cdkstats, 0, sizeof(asystats_t));
5026 stli_comstats.brd = portp->brdnr;
5027 stli_comstats.panel = portp->panelnr;
5028 stli_comstats.port = portp->portnr;
5029 stli_comstats.state = portp->state;
5030 stli_comstats.flags = portp->flags;
5032 save_flags(flags);
5033 cli();
5034 if (portp->tty != (struct tty_struct *) NULL) {
5035 if (portp->tty->driver_data == portp) {
5036 stli_comstats.ttystate = portp->tty->flags;
5037 stli_comstats.rxbuffered = portp->tty->flip.count;
5038 if (portp->tty->termios != (struct termios *) NULL) {
5039 stli_comstats.cflags = portp->tty->termios->c_cflag;
5040 stli_comstats.iflags = portp->tty->termios->c_iflag;
5041 stli_comstats.oflags = portp->tty->termios->c_oflag;
5042 stli_comstats.lflags = portp->tty->termios->c_lflag;
5046 restore_flags(flags);
5048 stli_comstats.txtotal = stli_cdkstats.txchars;
5049 stli_comstats.rxtotal = stli_cdkstats.rxchars + stli_cdkstats.ringover;
5050 stli_comstats.txbuffered = stli_cdkstats.txringq;
5051 stli_comstats.rxbuffered += stli_cdkstats.rxringq;
5052 stli_comstats.rxoverrun = stli_cdkstats.overruns;
5053 stli_comstats.rxparity = stli_cdkstats.parity;
5054 stli_comstats.rxframing = stli_cdkstats.framing;
5055 stli_comstats.rxlost = stli_cdkstats.ringover;
5056 stli_comstats.rxbreaks = stli_cdkstats.rxbreaks;
5057 stli_comstats.txbreaks = stli_cdkstats.txbreaks;
5058 stli_comstats.txxon = stli_cdkstats.txstart;
5059 stli_comstats.txxoff = stli_cdkstats.txstop;
5060 stli_comstats.rxxon = stli_cdkstats.rxstart;
5061 stli_comstats.rxxoff = stli_cdkstats.rxstop;
5062 stli_comstats.rxrtsoff = stli_cdkstats.rtscnt / 2;
5063 stli_comstats.rxrtson = stli_cdkstats.rtscnt - stli_comstats.rxrtsoff;
5064 stli_comstats.modem = stli_cdkstats.dcdcnt;
5065 stli_comstats.hwid = stli_cdkstats.hwid;
5066 stli_comstats.signals = stli_mktiocm(stli_cdkstats.signals);
5068 return(0);
5071 /*****************************************************************************/
5074 * Return the port stats structure to user app. A NULL port struct
5075 * pointer passed in means that we need to find out from the app
5076 * what port to get stats for (used through board control device).
5079 static int stli_getportstats(stliport_t *portp, comstats_t *cp)
5081 stlibrd_t *brdp;
5082 int rc;
5084 if (portp == (stliport_t *) NULL) {
5085 copy_from_user(&stli_comstats, cp, sizeof(comstats_t));
5086 portp = stli_getport(stli_comstats.brd, stli_comstats.panel,
5087 stli_comstats.port);
5088 if (portp == (stliport_t *) NULL)
5089 return(-ENODEV);
5092 brdp = stli_brds[portp->brdnr];
5093 if (brdp == (stlibrd_t *) NULL)
5094 return(-ENODEV);
5096 if ((rc = stli_portcmdstats(portp)) < 0)
5097 return(rc);
5099 copy_to_user(cp, &stli_comstats, sizeof(comstats_t));
5100 return(0);
5103 /*****************************************************************************/
5106 * Clear the port stats structure. We also return it zeroed out...
5109 static int stli_clrportstats(stliport_t *portp, comstats_t *cp)
5111 stlibrd_t *brdp;
5112 int rc;
5114 if (portp == (stliport_t *) NULL) {
5115 copy_from_user(&stli_comstats, cp, sizeof(comstats_t));
5116 portp = stli_getport(stli_comstats.brd, stli_comstats.panel,
5117 stli_comstats.port);
5118 if (portp == (stliport_t *) NULL)
5119 return(-ENODEV);
5122 brdp = stli_brds[portp->brdnr];
5123 if (brdp == (stlibrd_t *) NULL)
5124 return(-ENODEV);
5126 if (brdp->state & BST_STARTED) {
5127 if ((rc = stli_cmdwait(brdp, portp, A_CLEARSTATS, 0, 0, 0)) < 0)
5128 return(rc);
5131 memset(&stli_comstats, 0, sizeof(comstats_t));
5132 stli_comstats.brd = portp->brdnr;
5133 stli_comstats.panel = portp->panelnr;
5134 stli_comstats.port = portp->portnr;
5136 copy_to_user(cp, &stli_comstats, sizeof(comstats_t));
5137 return(0);
5140 /*****************************************************************************/
5143 * Return the entire driver ports structure to a user app.
5146 static int stli_getportstruct(unsigned long arg)
5148 stliport_t *portp;
5150 copy_from_user(&stli_dummyport, (void *) arg, sizeof(stliport_t));
5151 portp = stli_getport(stli_dummyport.brdnr, stli_dummyport.panelnr,
5152 stli_dummyport.portnr);
5153 if (portp == (stliport_t *) NULL)
5154 return(-ENODEV);
5155 copy_to_user((void *) arg, portp, sizeof(stliport_t));
5156 return(0);
5159 /*****************************************************************************/
5162 * Return the entire driver board structure to a user app.
5165 static int stli_getbrdstruct(unsigned long arg)
5167 stlibrd_t *brdp;
5169 copy_from_user(&stli_dummybrd, (void *) arg, sizeof(stlibrd_t));
5170 if ((stli_dummybrd.brdnr < 0) || (stli_dummybrd.brdnr >= STL_MAXBRDS))
5171 return(-ENODEV);
5172 brdp = stli_brds[stli_dummybrd.brdnr];
5173 if (brdp == (stlibrd_t *) NULL)
5174 return(-ENODEV);
5175 copy_to_user((void *) arg, brdp, sizeof(stlibrd_t));
5176 return(0);
5179 /*****************************************************************************/
5182 * Memory device open code. Need to keep track of opens and close
5183 * for module handling.
5186 static int stli_memopen(struct inode *ip, struct file *fp)
5188 MOD_INC_USE_COUNT;
5189 return(0);
5192 /*****************************************************************************/
5194 static int stli_memclose(struct inode *ip, struct file *fp)
5196 MOD_DEC_USE_COUNT;
5197 return(0);
5200 /*****************************************************************************/
5203 * The "staliomem" device is also required to do some special operations on
5204 * the board. We need to be able to send an interrupt to the board,
5205 * reset it, and start/stop it.
5208 static int stli_memioctl(struct inode *ip, struct file *fp, unsigned int cmd, unsigned long arg)
5210 stlibrd_t *brdp;
5211 int brdnr, rc, done;
5213 #if DEBUG
5214 printk("stli_memioctl(ip=%x,fp=%x,cmd=%x,arg=%x)\n", (int) ip,
5215 (int) fp, cmd, (int) arg);
5216 #endif
5219 * First up handle the board independent ioctls.
5221 done = 0;
5222 rc = 0;
5224 switch (cmd) {
5225 case COM_GETPORTSTATS:
5226 if ((rc = verify_area(VERIFY_WRITE, (void *) arg,
5227 sizeof(comstats_t))) == 0)
5228 rc = stli_getportstats((stliport_t *) NULL,
5229 (comstats_t *) arg);
5230 done++;
5231 break;
5232 case COM_CLRPORTSTATS:
5233 if ((rc = verify_area(VERIFY_WRITE, (void *) arg,
5234 sizeof(comstats_t))) == 0)
5235 rc = stli_clrportstats((stliport_t *) NULL,
5236 (comstats_t *) arg);
5237 done++;
5238 break;
5239 case COM_GETBRDSTATS:
5240 if ((rc = verify_area(VERIFY_WRITE, (void *) arg,
5241 sizeof(combrd_t))) == 0)
5242 rc = stli_getbrdstats((combrd_t *) arg);
5243 done++;
5244 break;
5245 case COM_READPORT:
5246 if ((rc = verify_area(VERIFY_WRITE, (void *) arg,
5247 sizeof(stliport_t))) == 0)
5248 rc = stli_getportstruct(arg);
5249 done++;
5250 break;
5251 case COM_READBOARD:
5252 if ((rc = verify_area(VERIFY_WRITE, (void *) arg,
5253 sizeof(stlibrd_t))) == 0)
5254 rc = stli_getbrdstruct(arg);
5255 done++;
5256 break;
5257 default:
5258 break;
5261 if (done)
5262 return(rc);
5265 * Now handle the board specific ioctls. These all depend on the
5266 * minor number of the device they were called from.
5268 brdnr = MINOR(ip->i_rdev);
5269 if (brdnr >= STL_MAXBRDS)
5270 return(-ENODEV);
5271 brdp = stli_brds[brdnr];
5272 if (brdp == (stlibrd_t *) NULL)
5273 return(-ENODEV);
5274 if (brdp->state == 0)
5275 return(-ENODEV);
5277 switch (cmd) {
5278 case STL_BINTR:
5279 EBRDINTR(brdp);
5280 break;
5281 case STL_BSTART:
5282 rc = stli_startbrd(brdp);
5283 break;
5284 case STL_BSTOP:
5285 brdp->state &= ~BST_STARTED;
5286 break;
5287 case STL_BRESET:
5288 brdp->state &= ~BST_STARTED;
5289 EBRDRESET(brdp);
5290 if (stli_shared == 0) {
5291 if (brdp->reenable != NULL)
5292 (* brdp->reenable)(brdp);
5294 break;
5295 default:
5296 rc = -ENOIOCTLCMD;
5297 break;
5300 return(rc);
5303 /*****************************************************************************/
5305 int __init stli_init(void)
5307 printk(KERN_INFO "%s: version %s\n", stli_drvtitle, stli_drvversion);
5309 stli_initbrds();
5312 * Allocate a temporary write buffer.
5314 stli_tmpwritebuf = (char *) stli_memalloc(STLI_TXBUFSIZE);
5315 if (stli_tmpwritebuf == (char *) NULL)
5316 printk("STALLION: failed to allocate memory (size=%d)\n",
5317 STLI_TXBUFSIZE);
5318 stli_txcookbuf = (char *) stli_memalloc(STLI_TXBUFSIZE);
5319 if (stli_txcookbuf == (char *) NULL)
5320 printk("STALLION: failed to allocate memory (size=%d)\n",
5321 STLI_TXBUFSIZE);
5324 * Set up a character driver for the shared memory region. We need this
5325 * to down load the slave code image. Also it is a useful debugging tool.
5327 if (devfs_register_chrdev(STL_SIOMEMMAJOR, "staliomem", &stli_fsiomem))
5328 printk("STALLION: failed to register serial memory device\n");
5330 devfs_handle = devfs_mk_dir (NULL, "staliomem", 9, NULL);
5331 devfs_register_series (devfs_handle, "%u", 4, DEVFS_FL_DEFAULT,
5332 STL_SIOMEMMAJOR, 0,
5333 S_IFCHR | S_IRUSR | S_IWUSR, 0, 0,
5334 &stli_fsiomem, NULL);
5337 * Set up the tty driver structure and register us as a driver.
5338 * Also setup the callout tty device.
5340 memset(&stli_serial, 0, sizeof(struct tty_driver));
5341 stli_serial.magic = TTY_DRIVER_MAGIC;
5342 stli_serial.driver_name = stli_drvname;
5343 stli_serial.name = stli_serialname;
5344 stli_serial.major = STL_SERIALMAJOR;
5345 stli_serial.minor_start = 0;
5346 stli_serial.num = STL_MAXBRDS * STL_MAXPORTS;
5347 stli_serial.type = TTY_DRIVER_TYPE_SERIAL;
5348 stli_serial.subtype = STL_DRVTYPSERIAL;
5349 stli_serial.init_termios = stli_deftermios;
5350 stli_serial.flags = TTY_DRIVER_REAL_RAW;
5351 stli_serial.refcount = &stli_refcount;
5352 stli_serial.table = stli_ttys;
5353 stli_serial.termios = stli_termios;
5354 stli_serial.termios_locked = stli_termioslocked;
5356 stli_serial.open = stli_open;
5357 stli_serial.close = stli_close;
5358 stli_serial.write = stli_write;
5359 stli_serial.put_char = stli_putchar;
5360 stli_serial.flush_chars = stli_flushchars;
5361 stli_serial.write_room = stli_writeroom;
5362 stli_serial.chars_in_buffer = stli_charsinbuffer;
5363 stli_serial.ioctl = stli_ioctl;
5364 stli_serial.set_termios = stli_settermios;
5365 stli_serial.throttle = stli_throttle;
5366 stli_serial.unthrottle = stli_unthrottle;
5367 stli_serial.stop = stli_stop;
5368 stli_serial.start = stli_start;
5369 stli_serial.hangup = stli_hangup;
5370 stli_serial.flush_buffer = stli_flushbuffer;
5371 stli_serial.break_ctl = stli_breakctl;
5372 stli_serial.wait_until_sent = stli_waituntilsent;
5373 stli_serial.send_xchar = stli_sendxchar;
5374 stli_serial.read_proc = stli_readproc;
5376 stli_callout = stli_serial;
5377 stli_callout.name = stli_calloutname;
5378 stli_callout.major = STL_CALLOUTMAJOR;
5379 stli_callout.subtype = STL_DRVTYPCALLOUT;
5380 stli_callout.read_proc = 0;
5382 if (tty_register_driver(&stli_serial))
5383 printk("STALLION: failed to register serial driver\n");
5384 if (tty_register_driver(&stli_callout))
5385 printk("STALLION: failed to register callout driver\n");
5387 return(0);
5390 /*****************************************************************************/