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