[PATCH] drivers/char: Use ARRAY_SIZE macro
[deliverable/linux.git] / drivers / char / stallion.c
1 /*****************************************************************************/
2
3 /*
4 * stallion.c -- stallion 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/cd1400.h>
37 #include <linux/sc26198.h>
38 #include <linux/comstats.h>
39 #include <linux/stallion.h>
40 #include <linux/ioport.h>
41 #include <linux/init.h>
42 #include <linux/smp_lock.h>
43 #include <linux/devfs_fs_kernel.h>
44 #include <linux/device.h>
45 #include <linux/delay.h>
46
47 #include <asm/io.h>
48 #include <asm/uaccess.h>
49
50 #ifdef CONFIG_PCI
51 #include <linux/pci.h>
52 #endif
53
54 /*****************************************************************************/
55
56 /*
57 * Define different board types. Use the standard Stallion "assigned"
58 * board numbers. Boards supported in this driver are abbreviated as
59 * EIO = EasyIO and ECH = EasyConnection 8/32.
60 */
61 #define BRD_EASYIO 20
62 #define BRD_ECH 21
63 #define BRD_ECHMC 22
64 #define BRD_ECHPCI 26
65 #define BRD_ECH64PCI 27
66 #define BRD_EASYIOPCI 28
67
68 /*
69 * Define a configuration structure to hold the board configuration.
70 * Need to set this up in the code (for now) with the boards that are
71 * to be configured into the system. This is what needs to be modified
72 * when adding/removing/modifying boards. Each line entry in the
73 * stl_brdconf[] array is a board. Each line contains io/irq/memory
74 * ranges for that board (as well as what type of board it is).
75 * Some examples:
76 * { BRD_EASYIO, 0x2a0, 0, 0, 10, 0 },
77 * This line would configure an EasyIO board (4 or 8, no difference),
78 * at io address 2a0 and irq 10.
79 * Another example:
80 * { BRD_ECH, 0x2a8, 0x280, 0, 12, 0 },
81 * This line will configure an EasyConnection 8/32 board at primary io
82 * address 2a8, secondary io address 280 and irq 12.
83 * Enter as many lines into this array as you want (only the first 4
84 * will actually be used!). Any combination of EasyIO and EasyConnection
85 * boards can be specified. EasyConnection 8/32 boards can share their
86 * secondary io addresses between each other.
87 *
88 * NOTE: there is no need to put any entries in this table for PCI
89 * boards. They will be found automatically by the driver - provided
90 * PCI BIOS32 support is compiled into the kernel.
91 */
92
93 typedef struct {
94 int brdtype;
95 int ioaddr1;
96 int ioaddr2;
97 unsigned long memaddr;
98 int irq;
99 int irqtype;
100 } stlconf_t;
101
102 static stlconf_t stl_brdconf[] = {
103 /*{ BRD_EASYIO, 0x2a0, 0, 0, 10, 0 },*/
104 };
105
106 static int stl_nrbrds = ARRAY_SIZE(stl_brdconf);
107
108 /*****************************************************************************/
109
110 /*
111 * Define some important driver characteristics. Device major numbers
112 * allocated as per Linux Device Registry.
113 */
114 #ifndef STL_SIOMEMMAJOR
115 #define STL_SIOMEMMAJOR 28
116 #endif
117 #ifndef STL_SERIALMAJOR
118 #define STL_SERIALMAJOR 24
119 #endif
120 #ifndef STL_CALLOUTMAJOR
121 #define STL_CALLOUTMAJOR 25
122 #endif
123
124 /*
125 * Set the TX buffer size. Bigger is better, but we don't want
126 * to chew too much memory with buffers!
127 */
128 #define STL_TXBUFLOW 512
129 #define STL_TXBUFSIZE 4096
130
131 /*****************************************************************************/
132
133 /*
134 * Define our local driver identity first. Set up stuff to deal with
135 * all the local structures required by a serial tty driver.
136 */
137 static char *stl_drvtitle = "Stallion Multiport Serial Driver";
138 static char *stl_drvname = "stallion";
139 static char *stl_drvversion = "5.6.0";
140
141 static struct tty_driver *stl_serial;
142
143 /*
144 * We will need to allocate a temporary write buffer for chars that
145 * come direct from user space. The problem is that a copy from user
146 * space might cause a page fault (typically on a system that is
147 * swapping!). All ports will share one buffer - since if the system
148 * is already swapping a shared buffer won't make things any worse.
149 */
150 static char *stl_tmpwritebuf;
151 static DECLARE_MUTEX(stl_tmpwritesem);
152
153 /*
154 * Define a local default termios struct. All ports will be created
155 * with this termios initially. Basically all it defines is a raw port
156 * at 9600, 8 data bits, 1 stop bit.
157 */
158 static struct termios stl_deftermios = {
159 .c_cflag = (B9600 | CS8 | CREAD | HUPCL | CLOCAL),
160 .c_cc = INIT_C_CC,
161 };
162
163 /*
164 * Define global stats structures. Not used often, and can be
165 * re-used for each stats call.
166 */
167 static comstats_t stl_comstats;
168 static combrd_t stl_brdstats;
169 static stlbrd_t stl_dummybrd;
170 static stlport_t stl_dummyport;
171
172 /*
173 * Define global place to put buffer overflow characters.
174 */
175 static char stl_unwanted[SC26198_RXFIFOSIZE];
176
177 /*****************************************************************************/
178
179 static stlbrd_t *stl_brds[STL_MAXBRDS];
180
181 /*
182 * Per board state flags. Used with the state field of the board struct.
183 * Not really much here!
184 */
185 #define BRD_FOUND 0x1
186
187 /*
188 * Define the port structure istate flags. These set of flags are
189 * modified at interrupt time - so setting and reseting them needs
190 * to be atomic. Use the bit clear/setting routines for this.
191 */
192 #define ASYI_TXBUSY 1
193 #define ASYI_TXLOW 2
194 #define ASYI_DCDCHANGE 3
195 #define ASYI_TXFLOWED 4
196
197 /*
198 * Define an array of board names as printable strings. Handy for
199 * referencing boards when printing trace and stuff.
200 */
201 static char *stl_brdnames[] = {
202 (char *) NULL,
203 (char *) NULL,
204 (char *) NULL,
205 (char *) NULL,
206 (char *) NULL,
207 (char *) NULL,
208 (char *) NULL,
209 (char *) NULL,
210 (char *) NULL,
211 (char *) NULL,
212 (char *) NULL,
213 (char *) NULL,
214 (char *) NULL,
215 (char *) NULL,
216 (char *) NULL,
217 (char *) NULL,
218 (char *) NULL,
219 (char *) NULL,
220 (char *) NULL,
221 (char *) NULL,
222 "EasyIO",
223 "EC8/32-AT",
224 "EC8/32-MC",
225 (char *) NULL,
226 (char *) NULL,
227 (char *) NULL,
228 "EC8/32-PCI",
229 "EC8/64-PCI",
230 "EasyIO-PCI",
231 };
232
233 /*****************************************************************************/
234
235 /*
236 * Define some string labels for arguments passed from the module
237 * load line. These allow for easy board definitions, and easy
238 * modification of the io, memory and irq resoucres.
239 */
240 static int stl_nargs = 0;
241 static char *board0[4];
242 static char *board1[4];
243 static char *board2[4];
244 static char *board3[4];
245
246 static char **stl_brdsp[] = {
247 (char **) &board0,
248 (char **) &board1,
249 (char **) &board2,
250 (char **) &board3
251 };
252
253 /*
254 * Define a set of common board names, and types. This is used to
255 * parse any module arguments.
256 */
257
258 typedef struct stlbrdtype {
259 char *name;
260 int type;
261 } stlbrdtype_t;
262
263 static stlbrdtype_t stl_brdstr[] = {
264 { "easyio", BRD_EASYIO },
265 { "eio", BRD_EASYIO },
266 { "20", BRD_EASYIO },
267 { "ec8/32", BRD_ECH },
268 { "ec8/32-at", BRD_ECH },
269 { "ec8/32-isa", BRD_ECH },
270 { "ech", BRD_ECH },
271 { "echat", BRD_ECH },
272 { "21", BRD_ECH },
273 { "ec8/32-mc", BRD_ECHMC },
274 { "ec8/32-mca", BRD_ECHMC },
275 { "echmc", BRD_ECHMC },
276 { "echmca", BRD_ECHMC },
277 { "22", BRD_ECHMC },
278 { "ec8/32-pc", BRD_ECHPCI },
279 { "ec8/32-pci", BRD_ECHPCI },
280 { "26", BRD_ECHPCI },
281 { "ec8/64-pc", BRD_ECH64PCI },
282 { "ec8/64-pci", BRD_ECH64PCI },
283 { "ech-pci", BRD_ECH64PCI },
284 { "echpci", BRD_ECH64PCI },
285 { "echpc", BRD_ECH64PCI },
286 { "27", BRD_ECH64PCI },
287 { "easyio-pc", BRD_EASYIOPCI },
288 { "easyio-pci", BRD_EASYIOPCI },
289 { "eio-pci", BRD_EASYIOPCI },
290 { "eiopci", BRD_EASYIOPCI },
291 { "28", BRD_EASYIOPCI },
292 };
293
294 /*
295 * Define the module agruments.
296 */
297 MODULE_AUTHOR("Greg Ungerer");
298 MODULE_DESCRIPTION("Stallion Multiport Serial Driver");
299 MODULE_LICENSE("GPL");
300
301 module_param_array(board0, charp, &stl_nargs, 0);
302 MODULE_PARM_DESC(board0, "Board 0 config -> name[,ioaddr[,ioaddr2][,irq]]");
303 module_param_array(board1, charp, &stl_nargs, 0);
304 MODULE_PARM_DESC(board1, "Board 1 config -> name[,ioaddr[,ioaddr2][,irq]]");
305 module_param_array(board2, charp, &stl_nargs, 0);
306 MODULE_PARM_DESC(board2, "Board 2 config -> name[,ioaddr[,ioaddr2][,irq]]");
307 module_param_array(board3, charp, &stl_nargs, 0);
308 MODULE_PARM_DESC(board3, "Board 3 config -> name[,ioaddr[,ioaddr2][,irq]]");
309
310 /*****************************************************************************/
311
312 /*
313 * Hardware ID bits for the EasyIO and ECH boards. These defines apply
314 * to the directly accessible io ports of these boards (not the uarts -
315 * they are in cd1400.h and sc26198.h).
316 */
317 #define EIO_8PORTRS 0x04
318 #define EIO_4PORTRS 0x05
319 #define EIO_8PORTDI 0x00
320 #define EIO_8PORTM 0x06
321 #define EIO_MK3 0x03
322 #define EIO_IDBITMASK 0x07
323
324 #define EIO_BRDMASK 0xf0
325 #define ID_BRD4 0x10
326 #define ID_BRD8 0x20
327 #define ID_BRD16 0x30
328
329 #define EIO_INTRPEND 0x08
330 #define EIO_INTEDGE 0x00
331 #define EIO_INTLEVEL 0x08
332 #define EIO_0WS 0x10
333
334 #define ECH_ID 0xa0
335 #define ECH_IDBITMASK 0xe0
336 #define ECH_BRDENABLE 0x08
337 #define ECH_BRDDISABLE 0x00
338 #define ECH_INTENABLE 0x01
339 #define ECH_INTDISABLE 0x00
340 #define ECH_INTLEVEL 0x02
341 #define ECH_INTEDGE 0x00
342 #define ECH_INTRPEND 0x01
343 #define ECH_BRDRESET 0x01
344
345 #define ECHMC_INTENABLE 0x01
346 #define ECHMC_BRDRESET 0x02
347
348 #define ECH_PNLSTATUS 2
349 #define ECH_PNL16PORT 0x20
350 #define ECH_PNLIDMASK 0x07
351 #define ECH_PNLXPID 0x40
352 #define ECH_PNLINTRPEND 0x80
353
354 #define ECH_ADDR2MASK 0x1e0
355
356 /*
357 * Define the vector mapping bits for the programmable interrupt board
358 * hardware. These bits encode the interrupt for the board to use - it
359 * is software selectable (except the EIO-8M).
360 */
361 static unsigned char stl_vecmap[] = {
362 0xff, 0xff, 0xff, 0x04, 0x06, 0x05, 0xff, 0x07,
363 0xff, 0xff, 0x00, 0x02, 0x01, 0xff, 0xff, 0x03
364 };
365
366 /*
367 * Set up enable and disable macros for the ECH boards. They require
368 * the secondary io address space to be activated and deactivated.
369 * This way all ECH boards can share their secondary io region.
370 * If this is an ECH-PCI board then also need to set the page pointer
371 * to point to the correct page.
372 */
373 #define BRDENABLE(brdnr,pagenr) \
374 if (stl_brds[(brdnr)]->brdtype == BRD_ECH) \
375 outb((stl_brds[(brdnr)]->ioctrlval | ECH_BRDENABLE), \
376 stl_brds[(brdnr)]->ioctrl); \
377 else if (stl_brds[(brdnr)]->brdtype == BRD_ECHPCI) \
378 outb((pagenr), stl_brds[(brdnr)]->ioctrl);
379
380 #define BRDDISABLE(brdnr) \
381 if (stl_brds[(brdnr)]->brdtype == BRD_ECH) \
382 outb((stl_brds[(brdnr)]->ioctrlval | ECH_BRDDISABLE), \
383 stl_brds[(brdnr)]->ioctrl);
384
385 #define STL_CD1400MAXBAUD 230400
386 #define STL_SC26198MAXBAUD 460800
387
388 #define STL_BAUDBASE 115200
389 #define STL_CLOSEDELAY (5 * HZ / 10)
390
391 /*****************************************************************************/
392
393 #ifdef CONFIG_PCI
394
395 /*
396 * Define the Stallion PCI vendor and device IDs.
397 */
398 #ifndef PCI_VENDOR_ID_STALLION
399 #define PCI_VENDOR_ID_STALLION 0x124d
400 #endif
401 #ifndef PCI_DEVICE_ID_ECHPCI832
402 #define PCI_DEVICE_ID_ECHPCI832 0x0000
403 #endif
404 #ifndef PCI_DEVICE_ID_ECHPCI864
405 #define PCI_DEVICE_ID_ECHPCI864 0x0002
406 #endif
407 #ifndef PCI_DEVICE_ID_EIOPCI
408 #define PCI_DEVICE_ID_EIOPCI 0x0003
409 #endif
410
411 /*
412 * Define structure to hold all Stallion PCI boards.
413 */
414 typedef struct stlpcibrd {
415 unsigned short vendid;
416 unsigned short devid;
417 int brdtype;
418 } stlpcibrd_t;
419
420 static stlpcibrd_t stl_pcibrds[] = {
421 { PCI_VENDOR_ID_STALLION, PCI_DEVICE_ID_ECHPCI864, BRD_ECH64PCI },
422 { PCI_VENDOR_ID_STALLION, PCI_DEVICE_ID_EIOPCI, BRD_EASYIOPCI },
423 { PCI_VENDOR_ID_STALLION, PCI_DEVICE_ID_ECHPCI832, BRD_ECHPCI },
424 { PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_87410, BRD_ECHPCI },
425 };
426
427 static int stl_nrpcibrds = ARRAY_SIZE(stl_pcibrds);
428
429 #endif
430
431 /*****************************************************************************/
432
433 /*
434 * Define macros to extract a brd/port number from a minor number.
435 */
436 #define MINOR2BRD(min) (((min) & 0xc0) >> 6)
437 #define MINOR2PORT(min) ((min) & 0x3f)
438
439 /*
440 * Define a baud rate table that converts termios baud rate selector
441 * into the actual baud rate value. All baud rate calculations are
442 * based on the actual baud rate required.
443 */
444 static unsigned int stl_baudrates[] = {
445 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
446 9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600
447 };
448
449 /*
450 * Define some handy local macros...
451 */
452 #undef MIN
453 #define MIN(a,b) (((a) <= (b)) ? (a) : (b))
454
455 #undef TOLOWER
456 #define TOLOWER(x) ((((x) >= 'A') && ((x) <= 'Z')) ? ((x) + 0x20) : (x))
457
458 /*****************************************************************************/
459
460 /*
461 * Declare all those functions in this driver!
462 */
463
464 static void stl_argbrds(void);
465 static int stl_parsebrd(stlconf_t *confp, char **argp);
466
467 static unsigned long stl_atol(char *str);
468
469 static int stl_init(void);
470 static int stl_open(struct tty_struct *tty, struct file *filp);
471 static void stl_close(struct tty_struct *tty, struct file *filp);
472 static int stl_write(struct tty_struct *tty, const unsigned char *buf, int count);
473 static void stl_putchar(struct tty_struct *tty, unsigned char ch);
474 static void stl_flushchars(struct tty_struct *tty);
475 static int stl_writeroom(struct tty_struct *tty);
476 static int stl_charsinbuffer(struct tty_struct *tty);
477 static int stl_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg);
478 static void stl_settermios(struct tty_struct *tty, struct termios *old);
479 static void stl_throttle(struct tty_struct *tty);
480 static void stl_unthrottle(struct tty_struct *tty);
481 static void stl_stop(struct tty_struct *tty);
482 static void stl_start(struct tty_struct *tty);
483 static void stl_flushbuffer(struct tty_struct *tty);
484 static void stl_breakctl(struct tty_struct *tty, int state);
485 static void stl_waituntilsent(struct tty_struct *tty, int timeout);
486 static void stl_sendxchar(struct tty_struct *tty, char ch);
487 static void stl_hangup(struct tty_struct *tty);
488 static int stl_memioctl(struct inode *ip, struct file *fp, unsigned int cmd, unsigned long arg);
489 static int stl_portinfo(stlport_t *portp, int portnr, char *pos);
490 static int stl_readproc(char *page, char **start, off_t off, int count, int *eof, void *data);
491
492 static int stl_brdinit(stlbrd_t *brdp);
493 static int stl_initports(stlbrd_t *brdp, stlpanel_t *panelp);
494 static int stl_getserial(stlport_t *portp, struct serial_struct __user *sp);
495 static int stl_setserial(stlport_t *portp, struct serial_struct __user *sp);
496 static int stl_getbrdstats(combrd_t __user *bp);
497 static int stl_getportstats(stlport_t *portp, comstats_t __user *cp);
498 static int stl_clrportstats(stlport_t *portp, comstats_t __user *cp);
499 static int stl_getportstruct(stlport_t __user *arg);
500 static int stl_getbrdstruct(stlbrd_t __user *arg);
501 static int stl_waitcarrier(stlport_t *portp, struct file *filp);
502 static int stl_eiointr(stlbrd_t *brdp);
503 static int stl_echatintr(stlbrd_t *brdp);
504 static int stl_echmcaintr(stlbrd_t *brdp);
505 static int stl_echpciintr(stlbrd_t *brdp);
506 static int stl_echpci64intr(stlbrd_t *brdp);
507 static void stl_offintr(void *private);
508 static void *stl_memalloc(int len);
509 static stlbrd_t *stl_allocbrd(void);
510 static stlport_t *stl_getport(int brdnr, int panelnr, int portnr);
511
512 static inline int stl_initbrds(void);
513 static inline int stl_initeio(stlbrd_t *brdp);
514 static inline int stl_initech(stlbrd_t *brdp);
515 static inline int stl_getbrdnr(void);
516
517 #ifdef CONFIG_PCI
518 static inline int stl_findpcibrds(void);
519 static inline int stl_initpcibrd(int brdtype, struct pci_dev *devp);
520 #endif
521
522 /*
523 * CD1400 uart specific handling functions.
524 */
525 static void stl_cd1400setreg(stlport_t *portp, int regnr, int value);
526 static int stl_cd1400getreg(stlport_t *portp, int regnr);
527 static int stl_cd1400updatereg(stlport_t *portp, int regnr, int value);
528 static int stl_cd1400panelinit(stlbrd_t *brdp, stlpanel_t *panelp);
529 static void stl_cd1400portinit(stlbrd_t *brdp, stlpanel_t *panelp, stlport_t *portp);
530 static void stl_cd1400setport(stlport_t *portp, struct termios *tiosp);
531 static int stl_cd1400getsignals(stlport_t *portp);
532 static void stl_cd1400setsignals(stlport_t *portp, int dtr, int rts);
533 static void stl_cd1400ccrwait(stlport_t *portp);
534 static void stl_cd1400enablerxtx(stlport_t *portp, int rx, int tx);
535 static void stl_cd1400startrxtx(stlport_t *portp, int rx, int tx);
536 static void stl_cd1400disableintrs(stlport_t *portp);
537 static void stl_cd1400sendbreak(stlport_t *portp, int len);
538 static void stl_cd1400flowctrl(stlport_t *portp, int state);
539 static void stl_cd1400sendflow(stlport_t *portp, int state);
540 static void stl_cd1400flush(stlport_t *portp);
541 static int stl_cd1400datastate(stlport_t *portp);
542 static void stl_cd1400eiointr(stlpanel_t *panelp, unsigned int iobase);
543 static void stl_cd1400echintr(stlpanel_t *panelp, unsigned int iobase);
544 static void stl_cd1400txisr(stlpanel_t *panelp, int ioaddr);
545 static void stl_cd1400rxisr(stlpanel_t *panelp, int ioaddr);
546 static void stl_cd1400mdmisr(stlpanel_t *panelp, int ioaddr);
547
548 static inline int stl_cd1400breakisr(stlport_t *portp, int ioaddr);
549
550 /*
551 * SC26198 uart specific handling functions.
552 */
553 static void stl_sc26198setreg(stlport_t *portp, int regnr, int value);
554 static int stl_sc26198getreg(stlport_t *portp, int regnr);
555 static int stl_sc26198updatereg(stlport_t *portp, int regnr, int value);
556 static int stl_sc26198getglobreg(stlport_t *portp, int regnr);
557 static int stl_sc26198panelinit(stlbrd_t *brdp, stlpanel_t *panelp);
558 static void stl_sc26198portinit(stlbrd_t *brdp, stlpanel_t *panelp, stlport_t *portp);
559 static void stl_sc26198setport(stlport_t *portp, struct termios *tiosp);
560 static int stl_sc26198getsignals(stlport_t *portp);
561 static void stl_sc26198setsignals(stlport_t *portp, int dtr, int rts);
562 static void stl_sc26198enablerxtx(stlport_t *portp, int rx, int tx);
563 static void stl_sc26198startrxtx(stlport_t *portp, int rx, int tx);
564 static void stl_sc26198disableintrs(stlport_t *portp);
565 static void stl_sc26198sendbreak(stlport_t *portp, int len);
566 static void stl_sc26198flowctrl(stlport_t *portp, int state);
567 static void stl_sc26198sendflow(stlport_t *portp, int state);
568 static void stl_sc26198flush(stlport_t *portp);
569 static int stl_sc26198datastate(stlport_t *portp);
570 static void stl_sc26198wait(stlport_t *portp);
571 static void stl_sc26198txunflow(stlport_t *portp, struct tty_struct *tty);
572 static void stl_sc26198intr(stlpanel_t *panelp, unsigned int iobase);
573 static void stl_sc26198txisr(stlport_t *port);
574 static void stl_sc26198rxisr(stlport_t *port, unsigned int iack);
575 static void stl_sc26198rxbadch(stlport_t *portp, unsigned char status, char ch);
576 static void stl_sc26198rxbadchars(stlport_t *portp);
577 static void stl_sc26198otherisr(stlport_t *port, unsigned int iack);
578
579 /*****************************************************************************/
580
581 /*
582 * Generic UART support structure.
583 */
584 typedef struct uart {
585 int (*panelinit)(stlbrd_t *brdp, stlpanel_t *panelp);
586 void (*portinit)(stlbrd_t *brdp, stlpanel_t *panelp, stlport_t *portp);
587 void (*setport)(stlport_t *portp, struct termios *tiosp);
588 int (*getsignals)(stlport_t *portp);
589 void (*setsignals)(stlport_t *portp, int dtr, int rts);
590 void (*enablerxtx)(stlport_t *portp, int rx, int tx);
591 void (*startrxtx)(stlport_t *portp, int rx, int tx);
592 void (*disableintrs)(stlport_t *portp);
593 void (*sendbreak)(stlport_t *portp, int len);
594 void (*flowctrl)(stlport_t *portp, int state);
595 void (*sendflow)(stlport_t *portp, int state);
596 void (*flush)(stlport_t *portp);
597 int (*datastate)(stlport_t *portp);
598 void (*intr)(stlpanel_t *panelp, unsigned int iobase);
599 } uart_t;
600
601 /*
602 * Define some macros to make calling these functions nice and clean.
603 */
604 #define stl_panelinit (* ((uart_t *) panelp->uartp)->panelinit)
605 #define stl_portinit (* ((uart_t *) portp->uartp)->portinit)
606 #define stl_setport (* ((uart_t *) portp->uartp)->setport)
607 #define stl_getsignals (* ((uart_t *) portp->uartp)->getsignals)
608 #define stl_setsignals (* ((uart_t *) portp->uartp)->setsignals)
609 #define stl_enablerxtx (* ((uart_t *) portp->uartp)->enablerxtx)
610 #define stl_startrxtx (* ((uart_t *) portp->uartp)->startrxtx)
611 #define stl_disableintrs (* ((uart_t *) portp->uartp)->disableintrs)
612 #define stl_sendbreak (* ((uart_t *) portp->uartp)->sendbreak)
613 #define stl_flowctrl (* ((uart_t *) portp->uartp)->flowctrl)
614 #define stl_sendflow (* ((uart_t *) portp->uartp)->sendflow)
615 #define stl_flush (* ((uart_t *) portp->uartp)->flush)
616 #define stl_datastate (* ((uart_t *) portp->uartp)->datastate)
617
618 /*****************************************************************************/
619
620 /*
621 * CD1400 UART specific data initialization.
622 */
623 static uart_t stl_cd1400uart = {
624 stl_cd1400panelinit,
625 stl_cd1400portinit,
626 stl_cd1400setport,
627 stl_cd1400getsignals,
628 stl_cd1400setsignals,
629 stl_cd1400enablerxtx,
630 stl_cd1400startrxtx,
631 stl_cd1400disableintrs,
632 stl_cd1400sendbreak,
633 stl_cd1400flowctrl,
634 stl_cd1400sendflow,
635 stl_cd1400flush,
636 stl_cd1400datastate,
637 stl_cd1400eiointr
638 };
639
640 /*
641 * Define the offsets within the register bank of a cd1400 based panel.
642 * These io address offsets are common to the EasyIO board as well.
643 */
644 #define EREG_ADDR 0
645 #define EREG_DATA 4
646 #define EREG_RXACK 5
647 #define EREG_TXACK 6
648 #define EREG_MDACK 7
649
650 #define EREG_BANKSIZE 8
651
652 #define CD1400_CLK 25000000
653 #define CD1400_CLK8M 20000000
654
655 /*
656 * Define the cd1400 baud rate clocks. These are used when calculating
657 * what clock and divisor to use for the required baud rate. Also
658 * define the maximum baud rate allowed, and the default base baud.
659 */
660 static int stl_cd1400clkdivs[] = {
661 CD1400_CLK0, CD1400_CLK1, CD1400_CLK2, CD1400_CLK3, CD1400_CLK4
662 };
663
664 /*****************************************************************************/
665
666 /*
667 * SC26198 UART specific data initization.
668 */
669 static uart_t stl_sc26198uart = {
670 stl_sc26198panelinit,
671 stl_sc26198portinit,
672 stl_sc26198setport,
673 stl_sc26198getsignals,
674 stl_sc26198setsignals,
675 stl_sc26198enablerxtx,
676 stl_sc26198startrxtx,
677 stl_sc26198disableintrs,
678 stl_sc26198sendbreak,
679 stl_sc26198flowctrl,
680 stl_sc26198sendflow,
681 stl_sc26198flush,
682 stl_sc26198datastate,
683 stl_sc26198intr
684 };
685
686 /*
687 * Define the offsets within the register bank of a sc26198 based panel.
688 */
689 #define XP_DATA 0
690 #define XP_ADDR 1
691 #define XP_MODID 2
692 #define XP_STATUS 2
693 #define XP_IACK 3
694
695 #define XP_BANKSIZE 4
696
697 /*
698 * Define the sc26198 baud rate table. Offsets within the table
699 * represent the actual baud rate selector of sc26198 registers.
700 */
701 static unsigned int sc26198_baudtable[] = {
702 50, 75, 150, 200, 300, 450, 600, 900, 1200, 1800, 2400, 3600,
703 4800, 7200, 9600, 14400, 19200, 28800, 38400, 57600, 115200,
704 230400, 460800, 921600
705 };
706
707 #define SC26198_NRBAUDS ARRAY_SIZE(sc26198_baudtable)
708
709 /*****************************************************************************/
710
711 /*
712 * Define the driver info for a user level control device. Used mainly
713 * to get at port stats - only not using the port device itself.
714 */
715 static struct file_operations stl_fsiomem = {
716 .owner = THIS_MODULE,
717 .ioctl = stl_memioctl,
718 };
719
720 /*****************************************************************************/
721
722 static struct class *stallion_class;
723
724 /*
725 * Loadable module initialization stuff.
726 */
727
728 static int __init stallion_module_init(void)
729 {
730 unsigned long flags;
731
732 #ifdef DEBUG
733 printk("init_module()\n");
734 #endif
735
736 save_flags(flags);
737 cli();
738 stl_init();
739 restore_flags(flags);
740
741 return(0);
742 }
743
744 /*****************************************************************************/
745
746 static void __exit stallion_module_exit(void)
747 {
748 stlbrd_t *brdp;
749 stlpanel_t *panelp;
750 stlport_t *portp;
751 unsigned long flags;
752 int i, j, k;
753
754 #ifdef DEBUG
755 printk("cleanup_module()\n");
756 #endif
757
758 printk(KERN_INFO "Unloading %s: version %s\n", stl_drvtitle,
759 stl_drvversion);
760
761 save_flags(flags);
762 cli();
763
764 /*
765 * Free up all allocated resources used by the ports. This includes
766 * memory and interrupts. As part of this process we will also do
767 * a hangup on every open port - to try to flush out any processes
768 * hanging onto ports.
769 */
770 i = tty_unregister_driver(stl_serial);
771 put_tty_driver(stl_serial);
772 if (i) {
773 printk("STALLION: failed to un-register tty driver, "
774 "errno=%d\n", -i);
775 restore_flags(flags);
776 return;
777 }
778 for (i = 0; i < 4; i++) {
779 devfs_remove("staliomem/%d", i);
780 class_device_destroy(stallion_class, MKDEV(STL_SIOMEMMAJOR, i));
781 }
782 devfs_remove("staliomem");
783 if ((i = unregister_chrdev(STL_SIOMEMMAJOR, "staliomem")))
784 printk("STALLION: failed to un-register serial memory device, "
785 "errno=%d\n", -i);
786 class_destroy(stallion_class);
787
788 kfree(stl_tmpwritebuf);
789
790 for (i = 0; (i < stl_nrbrds); i++) {
791 if ((brdp = stl_brds[i]) == (stlbrd_t *) NULL)
792 continue;
793
794 free_irq(brdp->irq, brdp);
795
796 for (j = 0; (j < STL_MAXPANELS); j++) {
797 panelp = brdp->panels[j];
798 if (panelp == (stlpanel_t *) NULL)
799 continue;
800 for (k = 0; (k < STL_PORTSPERPANEL); k++) {
801 portp = panelp->ports[k];
802 if (portp == (stlport_t *) NULL)
803 continue;
804 if (portp->tty != (struct tty_struct *) NULL)
805 stl_hangup(portp->tty);
806 kfree(portp->tx.buf);
807 kfree(portp);
808 }
809 kfree(panelp);
810 }
811
812 release_region(brdp->ioaddr1, brdp->iosize1);
813 if (brdp->iosize2 > 0)
814 release_region(brdp->ioaddr2, brdp->iosize2);
815
816 kfree(brdp);
817 stl_brds[i] = (stlbrd_t *) NULL;
818 }
819
820 restore_flags(flags);
821 }
822
823 module_init(stallion_module_init);
824 module_exit(stallion_module_exit);
825
826 /*****************************************************************************/
827
828 /*
829 * Check for any arguments passed in on the module load command line.
830 */
831
832 static void stl_argbrds(void)
833 {
834 stlconf_t conf;
835 stlbrd_t *brdp;
836 int i;
837
838 #ifdef DEBUG
839 printk("stl_argbrds()\n");
840 #endif
841
842 for (i = stl_nrbrds; (i < stl_nargs); i++) {
843 memset(&conf, 0, sizeof(conf));
844 if (stl_parsebrd(&conf, stl_brdsp[i]) == 0)
845 continue;
846 if ((brdp = stl_allocbrd()) == (stlbrd_t *) NULL)
847 continue;
848 stl_nrbrds = i + 1;
849 brdp->brdnr = i;
850 brdp->brdtype = conf.brdtype;
851 brdp->ioaddr1 = conf.ioaddr1;
852 brdp->ioaddr2 = conf.ioaddr2;
853 brdp->irq = conf.irq;
854 brdp->irqtype = conf.irqtype;
855 stl_brdinit(brdp);
856 }
857 }
858
859 /*****************************************************************************/
860
861 /*
862 * Convert an ascii string number into an unsigned long.
863 */
864
865 static unsigned long stl_atol(char *str)
866 {
867 unsigned long val;
868 int base, c;
869 char *sp;
870
871 val = 0;
872 sp = str;
873 if ((*sp == '0') && (*(sp+1) == 'x')) {
874 base = 16;
875 sp += 2;
876 } else if (*sp == '0') {
877 base = 8;
878 sp++;
879 } else {
880 base = 10;
881 }
882
883 for (; (*sp != 0); sp++) {
884 c = (*sp > '9') ? (TOLOWER(*sp) - 'a' + 10) : (*sp - '0');
885 if ((c < 0) || (c >= base)) {
886 printk("STALLION: invalid argument %s\n", str);
887 val = 0;
888 break;
889 }
890 val = (val * base) + c;
891 }
892 return(val);
893 }
894
895 /*****************************************************************************/
896
897 /*
898 * Parse the supplied argument string, into the board conf struct.
899 */
900
901 static int stl_parsebrd(stlconf_t *confp, char **argp)
902 {
903 char *sp;
904 int i;
905
906 #ifdef DEBUG
907 printk("stl_parsebrd(confp=%x,argp=%x)\n", (int) confp, (int) argp);
908 #endif
909
910 if ((argp[0] == (char *) NULL) || (*argp[0] == 0))
911 return(0);
912
913 for (sp = argp[0], i = 0; ((*sp != 0) && (i < 25)); sp++, i++)
914 *sp = TOLOWER(*sp);
915
916 for (i = 0; i < ARRAY_SIZE(stl_brdstr); i++) {
917 if (strcmp(stl_brdstr[i].name, argp[0]) == 0)
918 break;
919 }
920 if (i == ARRAY_SIZE(stl_brdstr)) {
921 printk("STALLION: unknown board name, %s?\n", argp[0]);
922 return 0;
923 }
924
925 confp->brdtype = stl_brdstr[i].type;
926
927 i = 1;
928 if ((argp[i] != (char *) NULL) && (*argp[i] != 0))
929 confp->ioaddr1 = stl_atol(argp[i]);
930 i++;
931 if (confp->brdtype == BRD_ECH) {
932 if ((argp[i] != (char *) NULL) && (*argp[i] != 0))
933 confp->ioaddr2 = stl_atol(argp[i]);
934 i++;
935 }
936 if ((argp[i] != (char *) NULL) && (*argp[i] != 0))
937 confp->irq = stl_atol(argp[i]);
938 return(1);
939 }
940
941 /*****************************************************************************/
942
943 /*
944 * Local driver kernel memory allocation routine.
945 */
946
947 static void *stl_memalloc(int len)
948 {
949 return((void *) kmalloc(len, GFP_KERNEL));
950 }
951
952 /*****************************************************************************/
953
954 /*
955 * Allocate a new board structure. Fill out the basic info in it.
956 */
957
958 static stlbrd_t *stl_allocbrd(void)
959 {
960 stlbrd_t *brdp;
961
962 brdp = (stlbrd_t *) stl_memalloc(sizeof(stlbrd_t));
963 if (brdp == (stlbrd_t *) NULL) {
964 printk("STALLION: failed to allocate memory (size=%d)\n",
965 sizeof(stlbrd_t));
966 return((stlbrd_t *) NULL);
967 }
968
969 memset(brdp, 0, sizeof(stlbrd_t));
970 brdp->magic = STL_BOARDMAGIC;
971 return(brdp);
972 }
973
974 /*****************************************************************************/
975
976 static int stl_open(struct tty_struct *tty, struct file *filp)
977 {
978 stlport_t *portp;
979 stlbrd_t *brdp;
980 unsigned int minordev;
981 int brdnr, panelnr, portnr, rc;
982
983 #ifdef DEBUG
984 printk("stl_open(tty=%x,filp=%x): device=%s\n", (int) tty,
985 (int) filp, tty->name);
986 #endif
987
988 minordev = tty->index;
989 brdnr = MINOR2BRD(minordev);
990 if (brdnr >= stl_nrbrds)
991 return(-ENODEV);
992 brdp = stl_brds[brdnr];
993 if (brdp == (stlbrd_t *) NULL)
994 return(-ENODEV);
995 minordev = MINOR2PORT(minordev);
996 for (portnr = -1, panelnr = 0; (panelnr < STL_MAXPANELS); panelnr++) {
997 if (brdp->panels[panelnr] == (stlpanel_t *) NULL)
998 break;
999 if (minordev < brdp->panels[panelnr]->nrports) {
1000 portnr = minordev;
1001 break;
1002 }
1003 minordev -= brdp->panels[panelnr]->nrports;
1004 }
1005 if (portnr < 0)
1006 return(-ENODEV);
1007
1008 portp = brdp->panels[panelnr]->ports[portnr];
1009 if (portp == (stlport_t *) NULL)
1010 return(-ENODEV);
1011
1012 /*
1013 * On the first open of the device setup the port hardware, and
1014 * initialize the per port data structure.
1015 */
1016 portp->tty = tty;
1017 tty->driver_data = portp;
1018 portp->refcount++;
1019
1020 if ((portp->flags & ASYNC_INITIALIZED) == 0) {
1021 if (portp->tx.buf == (char *) NULL) {
1022 portp->tx.buf = (char *) stl_memalloc(STL_TXBUFSIZE);
1023 if (portp->tx.buf == (char *) NULL)
1024 return(-ENOMEM);
1025 portp->tx.head = portp->tx.buf;
1026 portp->tx.tail = portp->tx.buf;
1027 }
1028 stl_setport(portp, tty->termios);
1029 portp->sigs = stl_getsignals(portp);
1030 stl_setsignals(portp, 1, 1);
1031 stl_enablerxtx(portp, 1, 1);
1032 stl_startrxtx(portp, 1, 0);
1033 clear_bit(TTY_IO_ERROR, &tty->flags);
1034 portp->flags |= ASYNC_INITIALIZED;
1035 }
1036
1037 /*
1038 * Check if this port is in the middle of closing. If so then wait
1039 * until it is closed then return error status, based on flag settings.
1040 * The sleep here does not need interrupt protection since the wakeup
1041 * for it is done with the same context.
1042 */
1043 if (portp->flags & ASYNC_CLOSING) {
1044 interruptible_sleep_on(&portp->close_wait);
1045 if (portp->flags & ASYNC_HUP_NOTIFY)
1046 return(-EAGAIN);
1047 return(-ERESTARTSYS);
1048 }
1049
1050 /*
1051 * Based on type of open being done check if it can overlap with any
1052 * previous opens still in effect. If we are a normal serial device
1053 * then also we might have to wait for carrier.
1054 */
1055 if (!(filp->f_flags & O_NONBLOCK)) {
1056 if ((rc = stl_waitcarrier(portp, filp)) != 0)
1057 return(rc);
1058 }
1059 portp->flags |= ASYNC_NORMAL_ACTIVE;
1060
1061 return(0);
1062 }
1063
1064 /*****************************************************************************/
1065
1066 /*
1067 * Possibly need to wait for carrier (DCD signal) to come high. Say
1068 * maybe because if we are clocal then we don't need to wait...
1069 */
1070
1071 static int stl_waitcarrier(stlport_t *portp, struct file *filp)
1072 {
1073 unsigned long flags;
1074 int rc, doclocal;
1075
1076 #ifdef DEBUG
1077 printk("stl_waitcarrier(portp=%x,filp=%x)\n", (int) portp, (int) filp);
1078 #endif
1079
1080 rc = 0;
1081 doclocal = 0;
1082
1083 if (portp->tty->termios->c_cflag & CLOCAL)
1084 doclocal++;
1085
1086 save_flags(flags);
1087 cli();
1088 portp->openwaitcnt++;
1089 if (! tty_hung_up_p(filp))
1090 portp->refcount--;
1091
1092 for (;;) {
1093 stl_setsignals(portp, 1, 1);
1094 if (tty_hung_up_p(filp) ||
1095 ((portp->flags & ASYNC_INITIALIZED) == 0)) {
1096 if (portp->flags & ASYNC_HUP_NOTIFY)
1097 rc = -EBUSY;
1098 else
1099 rc = -ERESTARTSYS;
1100 break;
1101 }
1102 if (((portp->flags & ASYNC_CLOSING) == 0) &&
1103 (doclocal || (portp->sigs & TIOCM_CD))) {
1104 break;
1105 }
1106 if (signal_pending(current)) {
1107 rc = -ERESTARTSYS;
1108 break;
1109 }
1110 interruptible_sleep_on(&portp->open_wait);
1111 }
1112
1113 if (! tty_hung_up_p(filp))
1114 portp->refcount++;
1115 portp->openwaitcnt--;
1116 restore_flags(flags);
1117
1118 return(rc);
1119 }
1120
1121 /*****************************************************************************/
1122
1123 static void stl_close(struct tty_struct *tty, struct file *filp)
1124 {
1125 stlport_t *portp;
1126 unsigned long flags;
1127
1128 #ifdef DEBUG
1129 printk("stl_close(tty=%x,filp=%x)\n", (int) tty, (int) filp);
1130 #endif
1131
1132 portp = tty->driver_data;
1133 if (portp == (stlport_t *) NULL)
1134 return;
1135
1136 save_flags(flags);
1137 cli();
1138 if (tty_hung_up_p(filp)) {
1139 restore_flags(flags);
1140 return;
1141 }
1142 if ((tty->count == 1) && (portp->refcount != 1))
1143 portp->refcount = 1;
1144 if (portp->refcount-- > 1) {
1145 restore_flags(flags);
1146 return;
1147 }
1148
1149 portp->refcount = 0;
1150 portp->flags |= ASYNC_CLOSING;
1151
1152 /*
1153 * May want to wait for any data to drain before closing. The BUSY
1154 * flag keeps track of whether we are still sending or not - it is
1155 * very accurate for the cd1400, not quite so for the sc26198.
1156 * (The sc26198 has no "end-of-data" interrupt only empty FIFO)
1157 */
1158 tty->closing = 1;
1159 if (portp->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1160 tty_wait_until_sent(tty, portp->closing_wait);
1161 stl_waituntilsent(tty, (HZ / 2));
1162
1163 portp->flags &= ~ASYNC_INITIALIZED;
1164 stl_disableintrs(portp);
1165 if (tty->termios->c_cflag & HUPCL)
1166 stl_setsignals(portp, 0, 0);
1167 stl_enablerxtx(portp, 0, 0);
1168 stl_flushbuffer(tty);
1169 portp->istate = 0;
1170 if (portp->tx.buf != (char *) NULL) {
1171 kfree(portp->tx.buf);
1172 portp->tx.buf = (char *) NULL;
1173 portp->tx.head = (char *) NULL;
1174 portp->tx.tail = (char *) NULL;
1175 }
1176 set_bit(TTY_IO_ERROR, &tty->flags);
1177 tty_ldisc_flush(tty);
1178
1179 tty->closing = 0;
1180 portp->tty = (struct tty_struct *) NULL;
1181
1182 if (portp->openwaitcnt) {
1183 if (portp->close_delay)
1184 msleep_interruptible(jiffies_to_msecs(portp->close_delay));
1185 wake_up_interruptible(&portp->open_wait);
1186 }
1187
1188 portp->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
1189 wake_up_interruptible(&portp->close_wait);
1190 restore_flags(flags);
1191 }
1192
1193 /*****************************************************************************/
1194
1195 /*
1196 * Write routine. Take data and stuff it in to the TX ring queue.
1197 * If transmit interrupts are not running then start them.
1198 */
1199
1200 static int stl_write(struct tty_struct *tty, const unsigned char *buf, int count)
1201 {
1202 stlport_t *portp;
1203 unsigned int len, stlen;
1204 unsigned char *chbuf;
1205 char *head, *tail;
1206
1207 #ifdef DEBUG
1208 printk("stl_write(tty=%x,buf=%x,count=%d)\n",
1209 (int) tty, (int) buf, count);
1210 #endif
1211
1212 if ((tty == (struct tty_struct *) NULL) ||
1213 (stl_tmpwritebuf == (char *) NULL))
1214 return(0);
1215 portp = tty->driver_data;
1216 if (portp == (stlport_t *) NULL)
1217 return(0);
1218 if (portp->tx.buf == (char *) NULL)
1219 return(0);
1220
1221 /*
1222 * If copying direct from user space we must cater for page faults,
1223 * causing us to "sleep" here for a while. To handle this copy in all
1224 * the data we need now, into a local buffer. Then when we got it all
1225 * copy it into the TX buffer.
1226 */
1227 chbuf = (unsigned char *) buf;
1228
1229 head = portp->tx.head;
1230 tail = portp->tx.tail;
1231 if (head >= tail) {
1232 len = STL_TXBUFSIZE - (head - tail) - 1;
1233 stlen = STL_TXBUFSIZE - (head - portp->tx.buf);
1234 } else {
1235 len = tail - head - 1;
1236 stlen = len;
1237 }
1238
1239 len = MIN(len, count);
1240 count = 0;
1241 while (len > 0) {
1242 stlen = MIN(len, stlen);
1243 memcpy(head, chbuf, stlen);
1244 len -= stlen;
1245 chbuf += stlen;
1246 count += stlen;
1247 head += stlen;
1248 if (head >= (portp->tx.buf + STL_TXBUFSIZE)) {
1249 head = portp->tx.buf;
1250 stlen = tail - head;
1251 }
1252 }
1253 portp->tx.head = head;
1254
1255 clear_bit(ASYI_TXLOW, &portp->istate);
1256 stl_startrxtx(portp, -1, 1);
1257
1258 return(count);
1259 }
1260
1261 /*****************************************************************************/
1262
1263 static void stl_putchar(struct tty_struct *tty, unsigned char ch)
1264 {
1265 stlport_t *portp;
1266 unsigned int len;
1267 char *head, *tail;
1268
1269 #ifdef DEBUG
1270 printk("stl_putchar(tty=%x,ch=%x)\n", (int) tty, (int) ch);
1271 #endif
1272
1273 if (tty == (struct tty_struct *) NULL)
1274 return;
1275 portp = tty->driver_data;
1276 if (portp == (stlport_t *) NULL)
1277 return;
1278 if (portp->tx.buf == (char *) NULL)
1279 return;
1280
1281 head = portp->tx.head;
1282 tail = portp->tx.tail;
1283
1284 len = (head >= tail) ? (STL_TXBUFSIZE - (head - tail)) : (tail - head);
1285 len--;
1286
1287 if (len > 0) {
1288 *head++ = ch;
1289 if (head >= (portp->tx.buf + STL_TXBUFSIZE))
1290 head = portp->tx.buf;
1291 }
1292 portp->tx.head = head;
1293 }
1294
1295 /*****************************************************************************/
1296
1297 /*
1298 * If there are any characters in the buffer then make sure that TX
1299 * interrupts are on and get'em out. Normally used after the putchar
1300 * routine has been called.
1301 */
1302
1303 static void stl_flushchars(struct tty_struct *tty)
1304 {
1305 stlport_t *portp;
1306
1307 #ifdef DEBUG
1308 printk("stl_flushchars(tty=%x)\n", (int) tty);
1309 #endif
1310
1311 if (tty == (struct tty_struct *) NULL)
1312 return;
1313 portp = tty->driver_data;
1314 if (portp == (stlport_t *) NULL)
1315 return;
1316 if (portp->tx.buf == (char *) NULL)
1317 return;
1318
1319 #if 0
1320 if (tty->stopped || tty->hw_stopped ||
1321 (portp->tx.head == portp->tx.tail))
1322 return;
1323 #endif
1324 stl_startrxtx(portp, -1, 1);
1325 }
1326
1327 /*****************************************************************************/
1328
1329 static int stl_writeroom(struct tty_struct *tty)
1330 {
1331 stlport_t *portp;
1332 char *head, *tail;
1333
1334 #ifdef DEBUG
1335 printk("stl_writeroom(tty=%x)\n", (int) tty);
1336 #endif
1337
1338 if (tty == (struct tty_struct *) NULL)
1339 return(0);
1340 portp = tty->driver_data;
1341 if (portp == (stlport_t *) NULL)
1342 return(0);
1343 if (portp->tx.buf == (char *) NULL)
1344 return(0);
1345
1346 head = portp->tx.head;
1347 tail = portp->tx.tail;
1348 return((head >= tail) ? (STL_TXBUFSIZE - (head - tail) - 1) : (tail - head - 1));
1349 }
1350
1351 /*****************************************************************************/
1352
1353 /*
1354 * Return number of chars in the TX buffer. Normally we would just
1355 * calculate the number of chars in the buffer and return that, but if
1356 * the buffer is empty and TX interrupts are still on then we return
1357 * that the buffer still has 1 char in it. This way whoever called us
1358 * will not think that ALL chars have drained - since the UART still
1359 * must have some chars in it (we are busy after all).
1360 */
1361
1362 static int stl_charsinbuffer(struct tty_struct *tty)
1363 {
1364 stlport_t *portp;
1365 unsigned int size;
1366 char *head, *tail;
1367
1368 #ifdef DEBUG
1369 printk("stl_charsinbuffer(tty=%x)\n", (int) tty);
1370 #endif
1371
1372 if (tty == (struct tty_struct *) NULL)
1373 return(0);
1374 portp = tty->driver_data;
1375 if (portp == (stlport_t *) NULL)
1376 return(0);
1377 if (portp->tx.buf == (char *) NULL)
1378 return(0);
1379
1380 head = portp->tx.head;
1381 tail = portp->tx.tail;
1382 size = (head >= tail) ? (head - tail) : (STL_TXBUFSIZE - (tail - head));
1383 if ((size == 0) && test_bit(ASYI_TXBUSY, &portp->istate))
1384 size = 1;
1385 return(size);
1386 }
1387
1388 /*****************************************************************************/
1389
1390 /*
1391 * Generate the serial struct info.
1392 */
1393
1394 static int stl_getserial(stlport_t *portp, struct serial_struct __user *sp)
1395 {
1396 struct serial_struct sio;
1397 stlbrd_t *brdp;
1398
1399 #ifdef DEBUG
1400 printk("stl_getserial(portp=%x,sp=%x)\n", (int) portp, (int) sp);
1401 #endif
1402
1403 memset(&sio, 0, sizeof(struct serial_struct));
1404 sio.line = portp->portnr;
1405 sio.port = portp->ioaddr;
1406 sio.flags = portp->flags;
1407 sio.baud_base = portp->baud_base;
1408 sio.close_delay = portp->close_delay;
1409 sio.closing_wait = portp->closing_wait;
1410 sio.custom_divisor = portp->custom_divisor;
1411 sio.hub6 = 0;
1412 if (portp->uartp == &stl_cd1400uart) {
1413 sio.type = PORT_CIRRUS;
1414 sio.xmit_fifo_size = CD1400_TXFIFOSIZE;
1415 } else {
1416 sio.type = PORT_UNKNOWN;
1417 sio.xmit_fifo_size = SC26198_TXFIFOSIZE;
1418 }
1419
1420 brdp = stl_brds[portp->brdnr];
1421 if (brdp != (stlbrd_t *) NULL)
1422 sio.irq = brdp->irq;
1423
1424 return copy_to_user(sp, &sio, sizeof(struct serial_struct)) ? -EFAULT : 0;
1425 }
1426
1427 /*****************************************************************************/
1428
1429 /*
1430 * Set port according to the serial struct info.
1431 * At this point we do not do any auto-configure stuff, so we will
1432 * just quietly ignore any requests to change irq, etc.
1433 */
1434
1435 static int stl_setserial(stlport_t *portp, struct serial_struct __user *sp)
1436 {
1437 struct serial_struct sio;
1438
1439 #ifdef DEBUG
1440 printk("stl_setserial(portp=%x,sp=%x)\n", (int) portp, (int) sp);
1441 #endif
1442
1443 if (copy_from_user(&sio, sp, sizeof(struct serial_struct)))
1444 return -EFAULT;
1445 if (!capable(CAP_SYS_ADMIN)) {
1446 if ((sio.baud_base != portp->baud_base) ||
1447 (sio.close_delay != portp->close_delay) ||
1448 ((sio.flags & ~ASYNC_USR_MASK) !=
1449 (portp->flags & ~ASYNC_USR_MASK)))
1450 return(-EPERM);
1451 }
1452
1453 portp->flags = (portp->flags & ~ASYNC_USR_MASK) |
1454 (sio.flags & ASYNC_USR_MASK);
1455 portp->baud_base = sio.baud_base;
1456 portp->close_delay = sio.close_delay;
1457 portp->closing_wait = sio.closing_wait;
1458 portp->custom_divisor = sio.custom_divisor;
1459 stl_setport(portp, portp->tty->termios);
1460 return(0);
1461 }
1462
1463 /*****************************************************************************/
1464
1465 static int stl_tiocmget(struct tty_struct *tty, struct file *file)
1466 {
1467 stlport_t *portp;
1468
1469 if (tty == (struct tty_struct *) NULL)
1470 return(-ENODEV);
1471 portp = tty->driver_data;
1472 if (portp == (stlport_t *) NULL)
1473 return(-ENODEV);
1474 if (tty->flags & (1 << TTY_IO_ERROR))
1475 return(-EIO);
1476
1477 return stl_getsignals(portp);
1478 }
1479
1480 static int stl_tiocmset(struct tty_struct *tty, struct file *file,
1481 unsigned int set, unsigned int clear)
1482 {
1483 stlport_t *portp;
1484 int rts = -1, dtr = -1;
1485
1486 if (tty == (struct tty_struct *) NULL)
1487 return(-ENODEV);
1488 portp = tty->driver_data;
1489 if (portp == (stlport_t *) NULL)
1490 return(-ENODEV);
1491 if (tty->flags & (1 << TTY_IO_ERROR))
1492 return(-EIO);
1493
1494 if (set & TIOCM_RTS)
1495 rts = 1;
1496 if (set & TIOCM_DTR)
1497 dtr = 1;
1498 if (clear & TIOCM_RTS)
1499 rts = 0;
1500 if (clear & TIOCM_DTR)
1501 dtr = 0;
1502
1503 stl_setsignals(portp, dtr, rts);
1504 return 0;
1505 }
1506
1507 static int stl_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
1508 {
1509 stlport_t *portp;
1510 unsigned int ival;
1511 int rc;
1512 void __user *argp = (void __user *)arg;
1513
1514 #ifdef DEBUG
1515 printk("stl_ioctl(tty=%x,file=%x,cmd=%x,arg=%x)\n",
1516 (int) tty, (int) file, cmd, (int) arg);
1517 #endif
1518
1519 if (tty == (struct tty_struct *) NULL)
1520 return(-ENODEV);
1521 portp = tty->driver_data;
1522 if (portp == (stlport_t *) NULL)
1523 return(-ENODEV);
1524
1525 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1526 (cmd != COM_GETPORTSTATS) && (cmd != COM_CLRPORTSTATS)) {
1527 if (tty->flags & (1 << TTY_IO_ERROR))
1528 return(-EIO);
1529 }
1530
1531 rc = 0;
1532
1533 switch (cmd) {
1534 case TIOCGSOFTCAR:
1535 rc = put_user(((tty->termios->c_cflag & CLOCAL) ? 1 : 0),
1536 (unsigned __user *) argp);
1537 break;
1538 case TIOCSSOFTCAR:
1539 if (get_user(ival, (unsigned int __user *) arg))
1540 return -EFAULT;
1541 tty->termios->c_cflag =
1542 (tty->termios->c_cflag & ~CLOCAL) |
1543 (ival ? CLOCAL : 0);
1544 break;
1545 case TIOCGSERIAL:
1546 rc = stl_getserial(portp, argp);
1547 break;
1548 case TIOCSSERIAL:
1549 rc = stl_setserial(portp, argp);
1550 break;
1551 case COM_GETPORTSTATS:
1552 rc = stl_getportstats(portp, argp);
1553 break;
1554 case COM_CLRPORTSTATS:
1555 rc = stl_clrportstats(portp, argp);
1556 break;
1557 case TIOCSERCONFIG:
1558 case TIOCSERGWILD:
1559 case TIOCSERSWILD:
1560 case TIOCSERGETLSR:
1561 case TIOCSERGSTRUCT:
1562 case TIOCSERGETMULTI:
1563 case TIOCSERSETMULTI:
1564 default:
1565 rc = -ENOIOCTLCMD;
1566 break;
1567 }
1568
1569 return(rc);
1570 }
1571
1572 /*****************************************************************************/
1573
1574 static void stl_settermios(struct tty_struct *tty, struct termios *old)
1575 {
1576 stlport_t *portp;
1577 struct termios *tiosp;
1578
1579 #ifdef DEBUG
1580 printk("stl_settermios(tty=%x,old=%x)\n", (int) tty, (int) old);
1581 #endif
1582
1583 if (tty == (struct tty_struct *) NULL)
1584 return;
1585 portp = tty->driver_data;
1586 if (portp == (stlport_t *) NULL)
1587 return;
1588
1589 tiosp = tty->termios;
1590 if ((tiosp->c_cflag == old->c_cflag) &&
1591 (tiosp->c_iflag == old->c_iflag))
1592 return;
1593
1594 stl_setport(portp, tiosp);
1595 stl_setsignals(portp, ((tiosp->c_cflag & (CBAUD & ~CBAUDEX)) ? 1 : 0),
1596 -1);
1597 if ((old->c_cflag & CRTSCTS) && ((tiosp->c_cflag & CRTSCTS) == 0)) {
1598 tty->hw_stopped = 0;
1599 stl_start(tty);
1600 }
1601 if (((old->c_cflag & CLOCAL) == 0) && (tiosp->c_cflag & CLOCAL))
1602 wake_up_interruptible(&portp->open_wait);
1603 }
1604
1605 /*****************************************************************************/
1606
1607 /*
1608 * Attempt to flow control who ever is sending us data. Based on termios
1609 * settings use software or/and hardware flow control.
1610 */
1611
1612 static void stl_throttle(struct tty_struct *tty)
1613 {
1614 stlport_t *portp;
1615
1616 #ifdef DEBUG
1617 printk("stl_throttle(tty=%x)\n", (int) tty);
1618 #endif
1619
1620 if (tty == (struct tty_struct *) NULL)
1621 return;
1622 portp = tty->driver_data;
1623 if (portp == (stlport_t *) NULL)
1624 return;
1625 stl_flowctrl(portp, 0);
1626 }
1627
1628 /*****************************************************************************/
1629
1630 /*
1631 * Unflow control the device sending us data...
1632 */
1633
1634 static void stl_unthrottle(struct tty_struct *tty)
1635 {
1636 stlport_t *portp;
1637
1638 #ifdef DEBUG
1639 printk("stl_unthrottle(tty=%x)\n", (int) tty);
1640 #endif
1641
1642 if (tty == (struct tty_struct *) NULL)
1643 return;
1644 portp = tty->driver_data;
1645 if (portp == (stlport_t *) NULL)
1646 return;
1647 stl_flowctrl(portp, 1);
1648 }
1649
1650 /*****************************************************************************/
1651
1652 /*
1653 * Stop the transmitter. Basically to do this we will just turn TX
1654 * interrupts off.
1655 */
1656
1657 static void stl_stop(struct tty_struct *tty)
1658 {
1659 stlport_t *portp;
1660
1661 #ifdef DEBUG
1662 printk("stl_stop(tty=%x)\n", (int) tty);
1663 #endif
1664
1665 if (tty == (struct tty_struct *) NULL)
1666 return;
1667 portp = tty->driver_data;
1668 if (portp == (stlport_t *) NULL)
1669 return;
1670 stl_startrxtx(portp, -1, 0);
1671 }
1672
1673 /*****************************************************************************/
1674
1675 /*
1676 * Start the transmitter again. Just turn TX interrupts back on.
1677 */
1678
1679 static void stl_start(struct tty_struct *tty)
1680 {
1681 stlport_t *portp;
1682
1683 #ifdef DEBUG
1684 printk("stl_start(tty=%x)\n", (int) tty);
1685 #endif
1686
1687 if (tty == (struct tty_struct *) NULL)
1688 return;
1689 portp = tty->driver_data;
1690 if (portp == (stlport_t *) NULL)
1691 return;
1692 stl_startrxtx(portp, -1, 1);
1693 }
1694
1695 /*****************************************************************************/
1696
1697 /*
1698 * Hangup this port. This is pretty much like closing the port, only
1699 * a little more brutal. No waiting for data to drain. Shutdown the
1700 * port and maybe drop signals.
1701 */
1702
1703 static void stl_hangup(struct tty_struct *tty)
1704 {
1705 stlport_t *portp;
1706
1707 #ifdef DEBUG
1708 printk("stl_hangup(tty=%x)\n", (int) tty);
1709 #endif
1710
1711 if (tty == (struct tty_struct *) NULL)
1712 return;
1713 portp = tty->driver_data;
1714 if (portp == (stlport_t *) NULL)
1715 return;
1716
1717 portp->flags &= ~ASYNC_INITIALIZED;
1718 stl_disableintrs(portp);
1719 if (tty->termios->c_cflag & HUPCL)
1720 stl_setsignals(portp, 0, 0);
1721 stl_enablerxtx(portp, 0, 0);
1722 stl_flushbuffer(tty);
1723 portp->istate = 0;
1724 set_bit(TTY_IO_ERROR, &tty->flags);
1725 if (portp->tx.buf != (char *) NULL) {
1726 kfree(portp->tx.buf);
1727 portp->tx.buf = (char *) NULL;
1728 portp->tx.head = (char *) NULL;
1729 portp->tx.tail = (char *) NULL;
1730 }
1731 portp->tty = (struct tty_struct *) NULL;
1732 portp->flags &= ~ASYNC_NORMAL_ACTIVE;
1733 portp->refcount = 0;
1734 wake_up_interruptible(&portp->open_wait);
1735 }
1736
1737 /*****************************************************************************/
1738
1739 static void stl_flushbuffer(struct tty_struct *tty)
1740 {
1741 stlport_t *portp;
1742
1743 #ifdef DEBUG
1744 printk("stl_flushbuffer(tty=%x)\n", (int) tty);
1745 #endif
1746
1747 if (tty == (struct tty_struct *) NULL)
1748 return;
1749 portp = tty->driver_data;
1750 if (portp == (stlport_t *) NULL)
1751 return;
1752
1753 stl_flush(portp);
1754 tty_wakeup(tty);
1755 }
1756
1757 /*****************************************************************************/
1758
1759 static void stl_breakctl(struct tty_struct *tty, int state)
1760 {
1761 stlport_t *portp;
1762
1763 #ifdef DEBUG
1764 printk("stl_breakctl(tty=%x,state=%d)\n", (int) tty, state);
1765 #endif
1766
1767 if (tty == (struct tty_struct *) NULL)
1768 return;
1769 portp = tty->driver_data;
1770 if (portp == (stlport_t *) NULL)
1771 return;
1772
1773 stl_sendbreak(portp, ((state == -1) ? 1 : 2));
1774 }
1775
1776 /*****************************************************************************/
1777
1778 static void stl_waituntilsent(struct tty_struct *tty, int timeout)
1779 {
1780 stlport_t *portp;
1781 unsigned long tend;
1782
1783 #ifdef DEBUG
1784 printk("stl_waituntilsent(tty=%x,timeout=%d)\n", (int) tty, timeout);
1785 #endif
1786
1787 if (tty == (struct tty_struct *) NULL)
1788 return;
1789 portp = tty->driver_data;
1790 if (portp == (stlport_t *) NULL)
1791 return;
1792
1793 if (timeout == 0)
1794 timeout = HZ;
1795 tend = jiffies + timeout;
1796
1797 while (stl_datastate(portp)) {
1798 if (signal_pending(current))
1799 break;
1800 msleep_interruptible(20);
1801 if (time_after_eq(jiffies, tend))
1802 break;
1803 }
1804 }
1805
1806 /*****************************************************************************/
1807
1808 static void stl_sendxchar(struct tty_struct *tty, char ch)
1809 {
1810 stlport_t *portp;
1811
1812 #ifdef DEBUG
1813 printk("stl_sendxchar(tty=%x,ch=%x)\n", (int) tty, ch);
1814 #endif
1815
1816 if (tty == (struct tty_struct *) NULL)
1817 return;
1818 portp = tty->driver_data;
1819 if (portp == (stlport_t *) NULL)
1820 return;
1821
1822 if (ch == STOP_CHAR(tty))
1823 stl_sendflow(portp, 0);
1824 else if (ch == START_CHAR(tty))
1825 stl_sendflow(portp, 1);
1826 else
1827 stl_putchar(tty, ch);
1828 }
1829
1830 /*****************************************************************************/
1831
1832 #define MAXLINE 80
1833
1834 /*
1835 * Format info for a specified port. The line is deliberately limited
1836 * to 80 characters. (If it is too long it will be truncated, if too
1837 * short then padded with spaces).
1838 */
1839
1840 static int stl_portinfo(stlport_t *portp, int portnr, char *pos)
1841 {
1842 char *sp;
1843 int sigs, cnt;
1844
1845 sp = pos;
1846 sp += sprintf(sp, "%d: uart:%s tx:%d rx:%d",
1847 portnr, (portp->hwid == 1) ? "SC26198" : "CD1400",
1848 (int) portp->stats.txtotal, (int) portp->stats.rxtotal);
1849
1850 if (portp->stats.rxframing)
1851 sp += sprintf(sp, " fe:%d", (int) portp->stats.rxframing);
1852 if (portp->stats.rxparity)
1853 sp += sprintf(sp, " pe:%d", (int) portp->stats.rxparity);
1854 if (portp->stats.rxbreaks)
1855 sp += sprintf(sp, " brk:%d", (int) portp->stats.rxbreaks);
1856 if (portp->stats.rxoverrun)
1857 sp += sprintf(sp, " oe:%d", (int) portp->stats.rxoverrun);
1858
1859 sigs = stl_getsignals(portp);
1860 cnt = sprintf(sp, "%s%s%s%s%s ",
1861 (sigs & TIOCM_RTS) ? "|RTS" : "",
1862 (sigs & TIOCM_CTS) ? "|CTS" : "",
1863 (sigs & TIOCM_DTR) ? "|DTR" : "",
1864 (sigs & TIOCM_CD) ? "|DCD" : "",
1865 (sigs & TIOCM_DSR) ? "|DSR" : "");
1866 *sp = ' ';
1867 sp += cnt;
1868
1869 for (cnt = (sp - pos); (cnt < (MAXLINE - 1)); cnt++)
1870 *sp++ = ' ';
1871 if (cnt >= MAXLINE)
1872 pos[(MAXLINE - 2)] = '+';
1873 pos[(MAXLINE - 1)] = '\n';
1874
1875 return(MAXLINE);
1876 }
1877
1878 /*****************************************************************************/
1879
1880 /*
1881 * Port info, read from the /proc file system.
1882 */
1883
1884 static int stl_readproc(char *page, char **start, off_t off, int count, int *eof, void *data)
1885 {
1886 stlbrd_t *brdp;
1887 stlpanel_t *panelp;
1888 stlport_t *portp;
1889 int brdnr, panelnr, portnr, totalport;
1890 int curoff, maxoff;
1891 char *pos;
1892
1893 #ifdef DEBUG
1894 printk("stl_readproc(page=%x,start=%x,off=%x,count=%d,eof=%x,"
1895 "data=%x\n", (int) page, (int) start, (int) off, count,
1896 (int) eof, (int) data);
1897 #endif
1898
1899 pos = page;
1900 totalport = 0;
1901 curoff = 0;
1902
1903 if (off == 0) {
1904 pos += sprintf(pos, "%s: version %s", stl_drvtitle,
1905 stl_drvversion);
1906 while (pos < (page + MAXLINE - 1))
1907 *pos++ = ' ';
1908 *pos++ = '\n';
1909 }
1910 curoff = MAXLINE;
1911
1912 /*
1913 * We scan through for each board, panel and port. The offset is
1914 * calculated on the fly, and irrelevant ports are skipped.
1915 */
1916 for (brdnr = 0; (brdnr < stl_nrbrds); brdnr++) {
1917 brdp = stl_brds[brdnr];
1918 if (brdp == (stlbrd_t *) NULL)
1919 continue;
1920 if (brdp->state == 0)
1921 continue;
1922
1923 maxoff = curoff + (brdp->nrports * MAXLINE);
1924 if (off >= maxoff) {
1925 curoff = maxoff;
1926 continue;
1927 }
1928
1929 totalport = brdnr * STL_MAXPORTS;
1930 for (panelnr = 0; (panelnr < brdp->nrpanels); panelnr++) {
1931 panelp = brdp->panels[panelnr];
1932 if (panelp == (stlpanel_t *) NULL)
1933 continue;
1934
1935 maxoff = curoff + (panelp->nrports * MAXLINE);
1936 if (off >= maxoff) {
1937 curoff = maxoff;
1938 totalport += panelp->nrports;
1939 continue;
1940 }
1941
1942 for (portnr = 0; (portnr < panelp->nrports); portnr++,
1943 totalport++) {
1944 portp = panelp->ports[portnr];
1945 if (portp == (stlport_t *) NULL)
1946 continue;
1947 if (off >= (curoff += MAXLINE))
1948 continue;
1949 if ((pos - page + MAXLINE) > count)
1950 goto stl_readdone;
1951 pos += stl_portinfo(portp, totalport, pos);
1952 }
1953 }
1954 }
1955
1956 *eof = 1;
1957
1958 stl_readdone:
1959 *start = page;
1960 return(pos - page);
1961 }
1962
1963 /*****************************************************************************/
1964
1965 /*
1966 * All board interrupts are vectored through here first. This code then
1967 * calls off to the approrpriate board interrupt handlers.
1968 */
1969
1970 static irqreturn_t stl_intr(int irq, void *dev_id, struct pt_regs *regs)
1971 {
1972 stlbrd_t *brdp = (stlbrd_t *) dev_id;
1973
1974 #ifdef DEBUG
1975 printk("stl_intr(brdp=%x,irq=%d,regs=%x)\n", (int) brdp, irq,
1976 (int) regs);
1977 #endif
1978
1979 return IRQ_RETVAL((* brdp->isr)(brdp));
1980 }
1981
1982 /*****************************************************************************/
1983
1984 /*
1985 * Interrupt service routine for EasyIO board types.
1986 */
1987
1988 static int stl_eiointr(stlbrd_t *brdp)
1989 {
1990 stlpanel_t *panelp;
1991 unsigned int iobase;
1992 int handled = 0;
1993
1994 panelp = brdp->panels[0];
1995 iobase = panelp->iobase;
1996 while (inb(brdp->iostatus) & EIO_INTRPEND) {
1997 handled = 1;
1998 (* panelp->isr)(panelp, iobase);
1999 }
2000 return handled;
2001 }
2002
2003 /*****************************************************************************/
2004
2005 /*
2006 * Interrupt service routine for ECH-AT board types.
2007 */
2008
2009 static int stl_echatintr(stlbrd_t *brdp)
2010 {
2011 stlpanel_t *panelp;
2012 unsigned int ioaddr;
2013 int bnknr;
2014 int handled = 0;
2015
2016 outb((brdp->ioctrlval | ECH_BRDENABLE), brdp->ioctrl);
2017
2018 while (inb(brdp->iostatus) & ECH_INTRPEND) {
2019 handled = 1;
2020 for (bnknr = 0; (bnknr < brdp->nrbnks); bnknr++) {
2021 ioaddr = brdp->bnkstataddr[bnknr];
2022 if (inb(ioaddr) & ECH_PNLINTRPEND) {
2023 panelp = brdp->bnk2panel[bnknr];
2024 (* panelp->isr)(panelp, (ioaddr & 0xfffc));
2025 }
2026 }
2027 }
2028
2029 outb((brdp->ioctrlval | ECH_BRDDISABLE), brdp->ioctrl);
2030
2031 return handled;
2032 }
2033
2034 /*****************************************************************************/
2035
2036 /*
2037 * Interrupt service routine for ECH-MCA board types.
2038 */
2039
2040 static int stl_echmcaintr(stlbrd_t *brdp)
2041 {
2042 stlpanel_t *panelp;
2043 unsigned int ioaddr;
2044 int bnknr;
2045 int handled = 0;
2046
2047 while (inb(brdp->iostatus) & ECH_INTRPEND) {
2048 handled = 1;
2049 for (bnknr = 0; (bnknr < brdp->nrbnks); bnknr++) {
2050 ioaddr = brdp->bnkstataddr[bnknr];
2051 if (inb(ioaddr) & ECH_PNLINTRPEND) {
2052 panelp = brdp->bnk2panel[bnknr];
2053 (* panelp->isr)(panelp, (ioaddr & 0xfffc));
2054 }
2055 }
2056 }
2057 return handled;
2058 }
2059
2060 /*****************************************************************************/
2061
2062 /*
2063 * Interrupt service routine for ECH-PCI board types.
2064 */
2065
2066 static int stl_echpciintr(stlbrd_t *brdp)
2067 {
2068 stlpanel_t *panelp;
2069 unsigned int ioaddr;
2070 int bnknr, recheck;
2071 int handled = 0;
2072
2073 while (1) {
2074 recheck = 0;
2075 for (bnknr = 0; (bnknr < brdp->nrbnks); bnknr++) {
2076 outb(brdp->bnkpageaddr[bnknr], brdp->ioctrl);
2077 ioaddr = brdp->bnkstataddr[bnknr];
2078 if (inb(ioaddr) & ECH_PNLINTRPEND) {
2079 panelp = brdp->bnk2panel[bnknr];
2080 (* panelp->isr)(panelp, (ioaddr & 0xfffc));
2081 recheck++;
2082 handled = 1;
2083 }
2084 }
2085 if (! recheck)
2086 break;
2087 }
2088 return handled;
2089 }
2090
2091 /*****************************************************************************/
2092
2093 /*
2094 * Interrupt service routine for ECH-8/64-PCI board types.
2095 */
2096
2097 static int stl_echpci64intr(stlbrd_t *brdp)
2098 {
2099 stlpanel_t *panelp;
2100 unsigned int ioaddr;
2101 int bnknr;
2102 int handled = 0;
2103
2104 while (inb(brdp->ioctrl) & 0x1) {
2105 handled = 1;
2106 for (bnknr = 0; (bnknr < brdp->nrbnks); bnknr++) {
2107 ioaddr = brdp->bnkstataddr[bnknr];
2108 if (inb(ioaddr) & ECH_PNLINTRPEND) {
2109 panelp = brdp->bnk2panel[bnknr];
2110 (* panelp->isr)(panelp, (ioaddr & 0xfffc));
2111 }
2112 }
2113 }
2114
2115 return handled;
2116 }
2117
2118 /*****************************************************************************/
2119
2120 /*
2121 * Service an off-level request for some channel.
2122 */
2123 static void stl_offintr(void *private)
2124 {
2125 stlport_t *portp;
2126 struct tty_struct *tty;
2127 unsigned int oldsigs;
2128
2129 portp = private;
2130
2131 #ifdef DEBUG
2132 printk("stl_offintr(portp=%x)\n", (int) portp);
2133 #endif
2134
2135 if (portp == (stlport_t *) NULL)
2136 return;
2137
2138 tty = portp->tty;
2139 if (tty == (struct tty_struct *) NULL)
2140 return;
2141
2142 lock_kernel();
2143 if (test_bit(ASYI_TXLOW, &portp->istate)) {
2144 tty_wakeup(tty);
2145 }
2146 if (test_bit(ASYI_DCDCHANGE, &portp->istate)) {
2147 clear_bit(ASYI_DCDCHANGE, &portp->istate);
2148 oldsigs = portp->sigs;
2149 portp->sigs = stl_getsignals(portp);
2150 if ((portp->sigs & TIOCM_CD) && ((oldsigs & TIOCM_CD) == 0))
2151 wake_up_interruptible(&portp->open_wait);
2152 if ((oldsigs & TIOCM_CD) && ((portp->sigs & TIOCM_CD) == 0)) {
2153 if (portp->flags & ASYNC_CHECK_CD)
2154 tty_hangup(tty); /* FIXME: module removal race here - AKPM */
2155 }
2156 }
2157 unlock_kernel();
2158 }
2159
2160 /*****************************************************************************/
2161
2162 /*
2163 * Initialize all the ports on a panel.
2164 */
2165
2166 static int __init stl_initports(stlbrd_t *brdp, stlpanel_t *panelp)
2167 {
2168 stlport_t *portp;
2169 int chipmask, i;
2170
2171 #ifdef DEBUG
2172 printk("stl_initports(brdp=%x,panelp=%x)\n", (int) brdp, (int) panelp);
2173 #endif
2174
2175 chipmask = stl_panelinit(brdp, panelp);
2176
2177 /*
2178 * All UART's are initialized (if found!). Now go through and setup
2179 * each ports data structures.
2180 */
2181 for (i = 0; (i < panelp->nrports); i++) {
2182 portp = (stlport_t *) stl_memalloc(sizeof(stlport_t));
2183 if (portp == (stlport_t *) NULL) {
2184 printk("STALLION: failed to allocate memory "
2185 "(size=%d)\n", sizeof(stlport_t));
2186 break;
2187 }
2188 memset(portp, 0, sizeof(stlport_t));
2189
2190 portp->magic = STL_PORTMAGIC;
2191 portp->portnr = i;
2192 portp->brdnr = panelp->brdnr;
2193 portp->panelnr = panelp->panelnr;
2194 portp->uartp = panelp->uartp;
2195 portp->clk = brdp->clk;
2196 portp->baud_base = STL_BAUDBASE;
2197 portp->close_delay = STL_CLOSEDELAY;
2198 portp->closing_wait = 30 * HZ;
2199 INIT_WORK(&portp->tqueue, stl_offintr, portp);
2200 init_waitqueue_head(&portp->open_wait);
2201 init_waitqueue_head(&portp->close_wait);
2202 portp->stats.brd = portp->brdnr;
2203 portp->stats.panel = portp->panelnr;
2204 portp->stats.port = portp->portnr;
2205 panelp->ports[i] = portp;
2206 stl_portinit(brdp, panelp, portp);
2207 }
2208
2209 return(0);
2210 }
2211
2212 /*****************************************************************************/
2213
2214 /*
2215 * Try to find and initialize an EasyIO board.
2216 */
2217
2218 static inline int stl_initeio(stlbrd_t *brdp)
2219 {
2220 stlpanel_t *panelp;
2221 unsigned int status;
2222 char *name;
2223 int rc;
2224
2225 #ifdef DEBUG
2226 printk("stl_initeio(brdp=%x)\n", (int) brdp);
2227 #endif
2228
2229 brdp->ioctrl = brdp->ioaddr1 + 1;
2230 brdp->iostatus = brdp->ioaddr1 + 2;
2231
2232 status = inb(brdp->iostatus);
2233 if ((status & EIO_IDBITMASK) == EIO_MK3)
2234 brdp->ioctrl++;
2235
2236 /*
2237 * Handle board specific stuff now. The real difference is PCI
2238 * or not PCI.
2239 */
2240 if (brdp->brdtype == BRD_EASYIOPCI) {
2241 brdp->iosize1 = 0x80;
2242 brdp->iosize2 = 0x80;
2243 name = "serial(EIO-PCI)";
2244 outb(0x41, (brdp->ioaddr2 + 0x4c));
2245 } else {
2246 brdp->iosize1 = 8;
2247 name = "serial(EIO)";
2248 if ((brdp->irq < 0) || (brdp->irq > 15) ||
2249 (stl_vecmap[brdp->irq] == (unsigned char) 0xff)) {
2250 printk("STALLION: invalid irq=%d for brd=%d\n",
2251 brdp->irq, brdp->brdnr);
2252 return(-EINVAL);
2253 }
2254 outb((stl_vecmap[brdp->irq] | EIO_0WS |
2255 ((brdp->irqtype) ? EIO_INTLEVEL : EIO_INTEDGE)),
2256 brdp->ioctrl);
2257 }
2258
2259 if (!request_region(brdp->ioaddr1, brdp->iosize1, name)) {
2260 printk(KERN_WARNING "STALLION: Warning, board %d I/O address "
2261 "%x conflicts with another device\n", brdp->brdnr,
2262 brdp->ioaddr1);
2263 return(-EBUSY);
2264 }
2265
2266 if (brdp->iosize2 > 0)
2267 if (!request_region(brdp->ioaddr2, brdp->iosize2, name)) {
2268 printk(KERN_WARNING "STALLION: Warning, board %d I/O "
2269 "address %x conflicts with another device\n",
2270 brdp->brdnr, brdp->ioaddr2);
2271 printk(KERN_WARNING "STALLION: Warning, also "
2272 "releasing board %d I/O address %x \n",
2273 brdp->brdnr, brdp->ioaddr1);
2274 release_region(brdp->ioaddr1, brdp->iosize1);
2275 return(-EBUSY);
2276 }
2277
2278 /*
2279 * Everything looks OK, so let's go ahead and probe for the hardware.
2280 */
2281 brdp->clk = CD1400_CLK;
2282 brdp->isr = stl_eiointr;
2283
2284 switch (status & EIO_IDBITMASK) {
2285 case EIO_8PORTM:
2286 brdp->clk = CD1400_CLK8M;
2287 /* fall thru */
2288 case EIO_8PORTRS:
2289 case EIO_8PORTDI:
2290 brdp->nrports = 8;
2291 break;
2292 case EIO_4PORTRS:
2293 brdp->nrports = 4;
2294 break;
2295 case EIO_MK3:
2296 switch (status & EIO_BRDMASK) {
2297 case ID_BRD4:
2298 brdp->nrports = 4;
2299 break;
2300 case ID_BRD8:
2301 brdp->nrports = 8;
2302 break;
2303 case ID_BRD16:
2304 brdp->nrports = 16;
2305 break;
2306 default:
2307 return(-ENODEV);
2308 }
2309 break;
2310 default:
2311 return(-ENODEV);
2312 }
2313
2314 /*
2315 * We have verified that the board is actually present, so now we
2316 * can complete the setup.
2317 */
2318
2319 panelp = (stlpanel_t *) stl_memalloc(sizeof(stlpanel_t));
2320 if (panelp == (stlpanel_t *) NULL) {
2321 printk(KERN_WARNING "STALLION: failed to allocate memory "
2322 "(size=%d)\n", sizeof(stlpanel_t));
2323 return(-ENOMEM);
2324 }
2325 memset(panelp, 0, sizeof(stlpanel_t));
2326
2327 panelp->magic = STL_PANELMAGIC;
2328 panelp->brdnr = brdp->brdnr;
2329 panelp->panelnr = 0;
2330 panelp->nrports = brdp->nrports;
2331 panelp->iobase = brdp->ioaddr1;
2332 panelp->hwid = status;
2333 if ((status & EIO_IDBITMASK) == EIO_MK3) {
2334 panelp->uartp = (void *) &stl_sc26198uart;
2335 panelp->isr = stl_sc26198intr;
2336 } else {
2337 panelp->uartp = (void *) &stl_cd1400uart;
2338 panelp->isr = stl_cd1400eiointr;
2339 }
2340
2341 brdp->panels[0] = panelp;
2342 brdp->nrpanels = 1;
2343 brdp->state |= BRD_FOUND;
2344 brdp->hwid = status;
2345 if (request_irq(brdp->irq, stl_intr, SA_SHIRQ, name, brdp) != 0) {
2346 printk("STALLION: failed to register interrupt "
2347 "routine for %s irq=%d\n", name, brdp->irq);
2348 rc = -ENODEV;
2349 } else {
2350 rc = 0;
2351 }
2352 return(rc);
2353 }
2354
2355 /*****************************************************************************/
2356
2357 /*
2358 * Try to find an ECH board and initialize it. This code is capable of
2359 * dealing with all types of ECH board.
2360 */
2361
2362 static inline int stl_initech(stlbrd_t *brdp)
2363 {
2364 stlpanel_t *panelp;
2365 unsigned int status, nxtid, ioaddr, conflict;
2366 int panelnr, banknr, i;
2367 char *name;
2368
2369 #ifdef DEBUG
2370 printk("stl_initech(brdp=%x)\n", (int) brdp);
2371 #endif
2372
2373 status = 0;
2374 conflict = 0;
2375
2376 /*
2377 * Set up the initial board register contents for boards. This varies a
2378 * bit between the different board types. So we need to handle each
2379 * separately. Also do a check that the supplied IRQ is good.
2380 */
2381 switch (brdp->brdtype) {
2382
2383 case BRD_ECH:
2384 brdp->isr = stl_echatintr;
2385 brdp->ioctrl = brdp->ioaddr1 + 1;
2386 brdp->iostatus = brdp->ioaddr1 + 1;
2387 status = inb(brdp->iostatus);
2388 if ((status & ECH_IDBITMASK) != ECH_ID)
2389 return(-ENODEV);
2390 if ((brdp->irq < 0) || (brdp->irq > 15) ||
2391 (stl_vecmap[brdp->irq] == (unsigned char) 0xff)) {
2392 printk("STALLION: invalid irq=%d for brd=%d\n",
2393 brdp->irq, brdp->brdnr);
2394 return(-EINVAL);
2395 }
2396 status = ((brdp->ioaddr2 & ECH_ADDR2MASK) >> 1);
2397 status |= (stl_vecmap[brdp->irq] << 1);
2398 outb((status | ECH_BRDRESET), brdp->ioaddr1);
2399 brdp->ioctrlval = ECH_INTENABLE |
2400 ((brdp->irqtype) ? ECH_INTLEVEL : ECH_INTEDGE);
2401 for (i = 0; (i < 10); i++)
2402 outb((brdp->ioctrlval | ECH_BRDENABLE), brdp->ioctrl);
2403 brdp->iosize1 = 2;
2404 brdp->iosize2 = 32;
2405 name = "serial(EC8/32)";
2406 outb(status, brdp->ioaddr1);
2407 break;
2408
2409 case BRD_ECHMC:
2410 brdp->isr = stl_echmcaintr;
2411 brdp->ioctrl = brdp->ioaddr1 + 0x20;
2412 brdp->iostatus = brdp->ioctrl;
2413 status = inb(brdp->iostatus);
2414 if ((status & ECH_IDBITMASK) != ECH_ID)
2415 return(-ENODEV);
2416 if ((brdp->irq < 0) || (brdp->irq > 15) ||
2417 (stl_vecmap[brdp->irq] == (unsigned char) 0xff)) {
2418 printk("STALLION: invalid irq=%d for brd=%d\n",
2419 brdp->irq, brdp->brdnr);
2420 return(-EINVAL);
2421 }
2422 outb(ECHMC_BRDRESET, brdp->ioctrl);
2423 outb(ECHMC_INTENABLE, brdp->ioctrl);
2424 brdp->iosize1 = 64;
2425 name = "serial(EC8/32-MC)";
2426 break;
2427
2428 case BRD_ECHPCI:
2429 brdp->isr = stl_echpciintr;
2430 brdp->ioctrl = brdp->ioaddr1 + 2;
2431 brdp->iosize1 = 4;
2432 brdp->iosize2 = 8;
2433 name = "serial(EC8/32-PCI)";
2434 break;
2435
2436 case BRD_ECH64PCI:
2437 brdp->isr = stl_echpci64intr;
2438 brdp->ioctrl = brdp->ioaddr2 + 0x40;
2439 outb(0x43, (brdp->ioaddr1 + 0x4c));
2440 brdp->iosize1 = 0x80;
2441 brdp->iosize2 = 0x80;
2442 name = "serial(EC8/64-PCI)";
2443 break;
2444
2445 default:
2446 printk("STALLION: unknown board type=%d\n", brdp->brdtype);
2447 return(-EINVAL);
2448 break;
2449 }
2450
2451 /*
2452 * Check boards for possible IO address conflicts and return fail status
2453 * if an IO conflict found.
2454 */
2455 if (!request_region(brdp->ioaddr1, brdp->iosize1, name)) {
2456 printk(KERN_WARNING "STALLION: Warning, board %d I/O address "
2457 "%x conflicts with another device\n", brdp->brdnr,
2458 brdp->ioaddr1);
2459 return(-EBUSY);
2460 }
2461
2462 if (brdp->iosize2 > 0)
2463 if (!request_region(brdp->ioaddr2, brdp->iosize2, name)) {
2464 printk(KERN_WARNING "STALLION: Warning, board %d I/O "
2465 "address %x conflicts with another device\n",
2466 brdp->brdnr, brdp->ioaddr2);
2467 printk(KERN_WARNING "STALLION: Warning, also "
2468 "releasing board %d I/O address %x \n",
2469 brdp->brdnr, brdp->ioaddr1);
2470 release_region(brdp->ioaddr1, brdp->iosize1);
2471 return(-EBUSY);
2472 }
2473
2474 /*
2475 * Scan through the secondary io address space looking for panels.
2476 * As we find'em allocate and initialize panel structures for each.
2477 */
2478 brdp->clk = CD1400_CLK;
2479 brdp->hwid = status;
2480
2481 ioaddr = brdp->ioaddr2;
2482 banknr = 0;
2483 panelnr = 0;
2484 nxtid = 0;
2485
2486 for (i = 0; (i < STL_MAXPANELS); i++) {
2487 if (brdp->brdtype == BRD_ECHPCI) {
2488 outb(nxtid, brdp->ioctrl);
2489 ioaddr = brdp->ioaddr2;
2490 }
2491 status = inb(ioaddr + ECH_PNLSTATUS);
2492 if ((status & ECH_PNLIDMASK) != nxtid)
2493 break;
2494 panelp = (stlpanel_t *) stl_memalloc(sizeof(stlpanel_t));
2495 if (panelp == (stlpanel_t *) NULL) {
2496 printk("STALLION: failed to allocate memory "
2497 "(size=%d)\n", sizeof(stlpanel_t));
2498 break;
2499 }
2500 memset(panelp, 0, sizeof(stlpanel_t));
2501 panelp->magic = STL_PANELMAGIC;
2502 panelp->brdnr = brdp->brdnr;
2503 panelp->panelnr = panelnr;
2504 panelp->iobase = ioaddr;
2505 panelp->pagenr = nxtid;
2506 panelp->hwid = status;
2507 brdp->bnk2panel[banknr] = panelp;
2508 brdp->bnkpageaddr[banknr] = nxtid;
2509 brdp->bnkstataddr[banknr++] = ioaddr + ECH_PNLSTATUS;
2510
2511 if (status & ECH_PNLXPID) {
2512 panelp->uartp = (void *) &stl_sc26198uart;
2513 panelp->isr = stl_sc26198intr;
2514 if (status & ECH_PNL16PORT) {
2515 panelp->nrports = 16;
2516 brdp->bnk2panel[banknr] = panelp;
2517 brdp->bnkpageaddr[banknr] = nxtid;
2518 brdp->bnkstataddr[banknr++] = ioaddr + 4 +
2519 ECH_PNLSTATUS;
2520 } else {
2521 panelp->nrports = 8;
2522 }
2523 } else {
2524 panelp->uartp = (void *) &stl_cd1400uart;
2525 panelp->isr = stl_cd1400echintr;
2526 if (status & ECH_PNL16PORT) {
2527 panelp->nrports = 16;
2528 panelp->ackmask = 0x80;
2529 if (brdp->brdtype != BRD_ECHPCI)
2530 ioaddr += EREG_BANKSIZE;
2531 brdp->bnk2panel[banknr] = panelp;
2532 brdp->bnkpageaddr[banknr] = ++nxtid;
2533 brdp->bnkstataddr[banknr++] = ioaddr +
2534 ECH_PNLSTATUS;
2535 } else {
2536 panelp->nrports = 8;
2537 panelp->ackmask = 0xc0;
2538 }
2539 }
2540
2541 nxtid++;
2542 ioaddr += EREG_BANKSIZE;
2543 brdp->nrports += panelp->nrports;
2544 brdp->panels[panelnr++] = panelp;
2545 if ((brdp->brdtype != BRD_ECHPCI) &&
2546 (ioaddr >= (brdp->ioaddr2 + brdp->iosize2)))
2547 break;
2548 }
2549
2550 brdp->nrpanels = panelnr;
2551 brdp->nrbnks = banknr;
2552 if (brdp->brdtype == BRD_ECH)
2553 outb((brdp->ioctrlval | ECH_BRDDISABLE), brdp->ioctrl);
2554
2555 brdp->state |= BRD_FOUND;
2556 if (request_irq(brdp->irq, stl_intr, SA_SHIRQ, name, brdp) != 0) {
2557 printk("STALLION: failed to register interrupt "
2558 "routine for %s irq=%d\n", name, brdp->irq);
2559 i = -ENODEV;
2560 } else {
2561 i = 0;
2562 }
2563
2564 return(i);
2565 }
2566
2567 /*****************************************************************************/
2568
2569 /*
2570 * Initialize and configure the specified board.
2571 * Scan through all the boards in the configuration and see what we
2572 * can find. Handle EIO and the ECH boards a little differently here
2573 * since the initial search and setup is very different.
2574 */
2575
2576 static int __init stl_brdinit(stlbrd_t *brdp)
2577 {
2578 int i;
2579
2580 #ifdef DEBUG
2581 printk("stl_brdinit(brdp=%x)\n", (int) brdp);
2582 #endif
2583
2584 switch (brdp->brdtype) {
2585 case BRD_EASYIO:
2586 case BRD_EASYIOPCI:
2587 stl_initeio(brdp);
2588 break;
2589 case BRD_ECH:
2590 case BRD_ECHMC:
2591 case BRD_ECHPCI:
2592 case BRD_ECH64PCI:
2593 stl_initech(brdp);
2594 break;
2595 default:
2596 printk("STALLION: board=%d is unknown board type=%d\n",
2597 brdp->brdnr, brdp->brdtype);
2598 return(ENODEV);
2599 }
2600
2601 stl_brds[brdp->brdnr] = brdp;
2602 if ((brdp->state & BRD_FOUND) == 0) {
2603 printk("STALLION: %s board not found, board=%d io=%x irq=%d\n",
2604 stl_brdnames[brdp->brdtype], brdp->brdnr,
2605 brdp->ioaddr1, brdp->irq);
2606 return(ENODEV);
2607 }
2608
2609 for (i = 0; (i < STL_MAXPANELS); i++)
2610 if (brdp->panels[i] != (stlpanel_t *) NULL)
2611 stl_initports(brdp, brdp->panels[i]);
2612
2613 printk("STALLION: %s found, board=%d io=%x irq=%d "
2614 "nrpanels=%d nrports=%d\n", stl_brdnames[brdp->brdtype],
2615 brdp->brdnr, brdp->ioaddr1, brdp->irq, brdp->nrpanels,
2616 brdp->nrports);
2617 return(0);
2618 }
2619
2620 /*****************************************************************************/
2621
2622 /*
2623 * Find the next available board number that is free.
2624 */
2625
2626 static inline int stl_getbrdnr(void)
2627 {
2628 int i;
2629
2630 for (i = 0; (i < STL_MAXBRDS); i++) {
2631 if (stl_brds[i] == (stlbrd_t *) NULL) {
2632 if (i >= stl_nrbrds)
2633 stl_nrbrds = i + 1;
2634 return(i);
2635 }
2636 }
2637 return(-1);
2638 }
2639
2640 /*****************************************************************************/
2641
2642 #ifdef CONFIG_PCI
2643
2644 /*
2645 * We have a Stallion board. Allocate a board structure and
2646 * initialize it. Read its IO and IRQ resources from PCI
2647 * configuration space.
2648 */
2649
2650 static inline int stl_initpcibrd(int brdtype, struct pci_dev *devp)
2651 {
2652 stlbrd_t *brdp;
2653
2654 #ifdef DEBUG
2655 printk("stl_initpcibrd(brdtype=%d,busnr=%x,devnr=%x)\n", brdtype,
2656 devp->bus->number, devp->devfn);
2657 #endif
2658
2659 if (pci_enable_device(devp))
2660 return(-EIO);
2661 if ((brdp = stl_allocbrd()) == (stlbrd_t *) NULL)
2662 return(-ENOMEM);
2663 if ((brdp->brdnr = stl_getbrdnr()) < 0) {
2664 printk("STALLION: too many boards found, "
2665 "maximum supported %d\n", STL_MAXBRDS);
2666 return(0);
2667 }
2668 brdp->brdtype = brdtype;
2669
2670 /*
2671 * Different Stallion boards use the BAR registers in different ways,
2672 * so set up io addresses based on board type.
2673 */
2674 #ifdef DEBUG
2675 printk("%s(%d): BAR[]=%x,%x,%x,%x IRQ=%x\n", __FILE__, __LINE__,
2676 pci_resource_start(devp, 0), pci_resource_start(devp, 1),
2677 pci_resource_start(devp, 2), pci_resource_start(devp, 3), devp->irq);
2678 #endif
2679
2680 /*
2681 * We have all resources from the board, so let's setup the actual
2682 * board structure now.
2683 */
2684 switch (brdtype) {
2685 case BRD_ECHPCI:
2686 brdp->ioaddr2 = pci_resource_start(devp, 0);
2687 brdp->ioaddr1 = pci_resource_start(devp, 1);
2688 break;
2689 case BRD_ECH64PCI:
2690 brdp->ioaddr2 = pci_resource_start(devp, 2);
2691 brdp->ioaddr1 = pci_resource_start(devp, 1);
2692 break;
2693 case BRD_EASYIOPCI:
2694 brdp->ioaddr1 = pci_resource_start(devp, 2);
2695 brdp->ioaddr2 = pci_resource_start(devp, 1);
2696 break;
2697 default:
2698 printk("STALLION: unknown PCI board type=%d\n", brdtype);
2699 break;
2700 }
2701
2702 brdp->irq = devp->irq;
2703 stl_brdinit(brdp);
2704
2705 return(0);
2706 }
2707
2708 /*****************************************************************************/
2709
2710 /*
2711 * Find all Stallion PCI boards that might be installed. Initialize each
2712 * one as it is found.
2713 */
2714
2715
2716 static inline int stl_findpcibrds(void)
2717 {
2718 struct pci_dev *dev = NULL;
2719 int i, rc;
2720
2721 #ifdef DEBUG
2722 printk("stl_findpcibrds()\n");
2723 #endif
2724
2725 for (i = 0; (i < stl_nrpcibrds); i++)
2726 while ((dev = pci_find_device(stl_pcibrds[i].vendid,
2727 stl_pcibrds[i].devid, dev))) {
2728
2729 /*
2730 * Found a device on the PCI bus that has our vendor and
2731 * device ID. Need to check now that it is really us.
2732 */
2733 if ((dev->class >> 8) == PCI_CLASS_STORAGE_IDE)
2734 continue;
2735
2736 rc = stl_initpcibrd(stl_pcibrds[i].brdtype, dev);
2737 if (rc)
2738 return(rc);
2739 }
2740
2741 return(0);
2742 }
2743
2744 #endif
2745
2746 /*****************************************************************************/
2747
2748 /*
2749 * Scan through all the boards in the configuration and see what we
2750 * can find. Handle EIO and the ECH boards a little differently here
2751 * since the initial search and setup is too different.
2752 */
2753
2754 static inline int stl_initbrds(void)
2755 {
2756 stlbrd_t *brdp;
2757 stlconf_t *confp;
2758 int i;
2759
2760 #ifdef DEBUG
2761 printk("stl_initbrds()\n");
2762 #endif
2763
2764 if (stl_nrbrds > STL_MAXBRDS) {
2765 printk("STALLION: too many boards in configuration table, "
2766 "truncating to %d\n", STL_MAXBRDS);
2767 stl_nrbrds = STL_MAXBRDS;
2768 }
2769
2770 /*
2771 * Firstly scan the list of static boards configured. Allocate
2772 * resources and initialize the boards as found.
2773 */
2774 for (i = 0; (i < stl_nrbrds); i++) {
2775 confp = &stl_brdconf[i];
2776 stl_parsebrd(confp, stl_brdsp[i]);
2777 if ((brdp = stl_allocbrd()) == (stlbrd_t *) NULL)
2778 return(-ENOMEM);
2779 brdp->brdnr = i;
2780 brdp->brdtype = confp->brdtype;
2781 brdp->ioaddr1 = confp->ioaddr1;
2782 brdp->ioaddr2 = confp->ioaddr2;
2783 brdp->irq = confp->irq;
2784 brdp->irqtype = confp->irqtype;
2785 stl_brdinit(brdp);
2786 }
2787
2788 /*
2789 * Find any dynamically supported boards. That is via module load
2790 * line options or auto-detected on the PCI bus.
2791 */
2792 stl_argbrds();
2793 #ifdef CONFIG_PCI
2794 stl_findpcibrds();
2795 #endif
2796
2797 return(0);
2798 }
2799
2800 /*****************************************************************************/
2801
2802 /*
2803 * Return the board stats structure to user app.
2804 */
2805
2806 static int stl_getbrdstats(combrd_t __user *bp)
2807 {
2808 stlbrd_t *brdp;
2809 stlpanel_t *panelp;
2810 int i;
2811
2812 if (copy_from_user(&stl_brdstats, bp, sizeof(combrd_t)))
2813 return -EFAULT;
2814 if (stl_brdstats.brd >= STL_MAXBRDS)
2815 return(-ENODEV);
2816 brdp = stl_brds[stl_brdstats.brd];
2817 if (brdp == (stlbrd_t *) NULL)
2818 return(-ENODEV);
2819
2820 memset(&stl_brdstats, 0, sizeof(combrd_t));
2821 stl_brdstats.brd = brdp->brdnr;
2822 stl_brdstats.type = brdp->brdtype;
2823 stl_brdstats.hwid = brdp->hwid;
2824 stl_brdstats.state = brdp->state;
2825 stl_brdstats.ioaddr = brdp->ioaddr1;
2826 stl_brdstats.ioaddr2 = brdp->ioaddr2;
2827 stl_brdstats.irq = brdp->irq;
2828 stl_brdstats.nrpanels = brdp->nrpanels;
2829 stl_brdstats.nrports = brdp->nrports;
2830 for (i = 0; (i < brdp->nrpanels); i++) {
2831 panelp = brdp->panels[i];
2832 stl_brdstats.panels[i].panel = i;
2833 stl_brdstats.panels[i].hwid = panelp->hwid;
2834 stl_brdstats.panels[i].nrports = panelp->nrports;
2835 }
2836
2837 return copy_to_user(bp, &stl_brdstats, sizeof(combrd_t)) ? -EFAULT : 0;
2838 }
2839
2840 /*****************************************************************************/
2841
2842 /*
2843 * Resolve the referenced port number into a port struct pointer.
2844 */
2845
2846 static stlport_t *stl_getport(int brdnr, int panelnr, int portnr)
2847 {
2848 stlbrd_t *brdp;
2849 stlpanel_t *panelp;
2850
2851 if ((brdnr < 0) || (brdnr >= STL_MAXBRDS))
2852 return((stlport_t *) NULL);
2853 brdp = stl_brds[brdnr];
2854 if (brdp == (stlbrd_t *) NULL)
2855 return((stlport_t *) NULL);
2856 if ((panelnr < 0) || (panelnr >= brdp->nrpanels))
2857 return((stlport_t *) NULL);
2858 panelp = brdp->panels[panelnr];
2859 if (panelp == (stlpanel_t *) NULL)
2860 return((stlport_t *) NULL);
2861 if ((portnr < 0) || (portnr >= panelp->nrports))
2862 return((stlport_t *) NULL);
2863 return(panelp->ports[portnr]);
2864 }
2865
2866 /*****************************************************************************/
2867
2868 /*
2869 * Return the port stats structure to user app. A NULL port struct
2870 * pointer passed in means that we need to find out from the app
2871 * what port to get stats for (used through board control device).
2872 */
2873
2874 static int stl_getportstats(stlport_t *portp, comstats_t __user *cp)
2875 {
2876 unsigned char *head, *tail;
2877 unsigned long flags;
2878
2879 if (!portp) {
2880 if (copy_from_user(&stl_comstats, cp, sizeof(comstats_t)))
2881 return -EFAULT;
2882 portp = stl_getport(stl_comstats.brd, stl_comstats.panel,
2883 stl_comstats.port);
2884 if (portp == (stlport_t *) NULL)
2885 return(-ENODEV);
2886 }
2887
2888 portp->stats.state = portp->istate;
2889 portp->stats.flags = portp->flags;
2890 portp->stats.hwid = portp->hwid;
2891
2892 portp->stats.ttystate = 0;
2893 portp->stats.cflags = 0;
2894 portp->stats.iflags = 0;
2895 portp->stats.oflags = 0;
2896 portp->stats.lflags = 0;
2897 portp->stats.rxbuffered = 0;
2898
2899 save_flags(flags);
2900 cli();
2901 if (portp->tty != (struct tty_struct *) NULL) {
2902 if (portp->tty->driver_data == portp) {
2903 portp->stats.ttystate = portp->tty->flags;
2904 portp->stats.rxbuffered = portp->tty->flip.count;
2905 if (portp->tty->termios != (struct termios *) NULL) {
2906 portp->stats.cflags = portp->tty->termios->c_cflag;
2907 portp->stats.iflags = portp->tty->termios->c_iflag;
2908 portp->stats.oflags = portp->tty->termios->c_oflag;
2909 portp->stats.lflags = portp->tty->termios->c_lflag;
2910 }
2911 }
2912 }
2913 restore_flags(flags);
2914
2915 head = portp->tx.head;
2916 tail = portp->tx.tail;
2917 portp->stats.txbuffered = ((head >= tail) ? (head - tail) :
2918 (STL_TXBUFSIZE - (tail - head)));
2919
2920 portp->stats.signals = (unsigned long) stl_getsignals(portp);
2921
2922 return copy_to_user(cp, &portp->stats,
2923 sizeof(comstats_t)) ? -EFAULT : 0;
2924 }
2925
2926 /*****************************************************************************/
2927
2928 /*
2929 * Clear the port stats structure. We also return it zeroed out...
2930 */
2931
2932 static int stl_clrportstats(stlport_t *portp, comstats_t __user *cp)
2933 {
2934 if (!portp) {
2935 if (copy_from_user(&stl_comstats, cp, sizeof(comstats_t)))
2936 return -EFAULT;
2937 portp = stl_getport(stl_comstats.brd, stl_comstats.panel,
2938 stl_comstats.port);
2939 if (portp == (stlport_t *) NULL)
2940 return(-ENODEV);
2941 }
2942
2943 memset(&portp->stats, 0, sizeof(comstats_t));
2944 portp->stats.brd = portp->brdnr;
2945 portp->stats.panel = portp->panelnr;
2946 portp->stats.port = portp->portnr;
2947 return copy_to_user(cp, &portp->stats,
2948 sizeof(comstats_t)) ? -EFAULT : 0;
2949 }
2950
2951 /*****************************************************************************/
2952
2953 /*
2954 * Return the entire driver ports structure to a user app.
2955 */
2956
2957 static int stl_getportstruct(stlport_t __user *arg)
2958 {
2959 stlport_t *portp;
2960
2961 if (copy_from_user(&stl_dummyport, arg, sizeof(stlport_t)))
2962 return -EFAULT;
2963 portp = stl_getport(stl_dummyport.brdnr, stl_dummyport.panelnr,
2964 stl_dummyport.portnr);
2965 if (!portp)
2966 return -ENODEV;
2967 return copy_to_user(arg, portp, sizeof(stlport_t)) ? -EFAULT : 0;
2968 }
2969
2970 /*****************************************************************************/
2971
2972 /*
2973 * Return the entire driver board structure to a user app.
2974 */
2975
2976 static int stl_getbrdstruct(stlbrd_t __user *arg)
2977 {
2978 stlbrd_t *brdp;
2979
2980 if (copy_from_user(&stl_dummybrd, arg, sizeof(stlbrd_t)))
2981 return -EFAULT;
2982 if ((stl_dummybrd.brdnr < 0) || (stl_dummybrd.brdnr >= STL_MAXBRDS))
2983 return -ENODEV;
2984 brdp = stl_brds[stl_dummybrd.brdnr];
2985 if (!brdp)
2986 return(-ENODEV);
2987 return copy_to_user(arg, brdp, sizeof(stlbrd_t)) ? -EFAULT : 0;
2988 }
2989
2990 /*****************************************************************************/
2991
2992 /*
2993 * The "staliomem" device is also required to do some special operations
2994 * on the board and/or ports. In this driver it is mostly used for stats
2995 * collection.
2996 */
2997
2998 static int stl_memioctl(struct inode *ip, struct file *fp, unsigned int cmd, unsigned long arg)
2999 {
3000 int brdnr, rc;
3001 void __user *argp = (void __user *)arg;
3002
3003 #ifdef DEBUG
3004 printk("stl_memioctl(ip=%x,fp=%x,cmd=%x,arg=%x)\n", (int) ip,
3005 (int) fp, cmd, (int) arg);
3006 #endif
3007
3008 brdnr = iminor(ip);
3009 if (brdnr >= STL_MAXBRDS)
3010 return(-ENODEV);
3011 rc = 0;
3012
3013 switch (cmd) {
3014 case COM_GETPORTSTATS:
3015 rc = stl_getportstats(NULL, argp);
3016 break;
3017 case COM_CLRPORTSTATS:
3018 rc = stl_clrportstats(NULL, argp);
3019 break;
3020 case COM_GETBRDSTATS:
3021 rc = stl_getbrdstats(argp);
3022 break;
3023 case COM_READPORT:
3024 rc = stl_getportstruct(argp);
3025 break;
3026 case COM_READBOARD:
3027 rc = stl_getbrdstruct(argp);
3028 break;
3029 default:
3030 rc = -ENOIOCTLCMD;
3031 break;
3032 }
3033
3034 return(rc);
3035 }
3036
3037 static struct tty_operations stl_ops = {
3038 .open = stl_open,
3039 .close = stl_close,
3040 .write = stl_write,
3041 .put_char = stl_putchar,
3042 .flush_chars = stl_flushchars,
3043 .write_room = stl_writeroom,
3044 .chars_in_buffer = stl_charsinbuffer,
3045 .ioctl = stl_ioctl,
3046 .set_termios = stl_settermios,
3047 .throttle = stl_throttle,
3048 .unthrottle = stl_unthrottle,
3049 .stop = stl_stop,
3050 .start = stl_start,
3051 .hangup = stl_hangup,
3052 .flush_buffer = stl_flushbuffer,
3053 .break_ctl = stl_breakctl,
3054 .wait_until_sent = stl_waituntilsent,
3055 .send_xchar = stl_sendxchar,
3056 .read_proc = stl_readproc,
3057 .tiocmget = stl_tiocmget,
3058 .tiocmset = stl_tiocmset,
3059 };
3060
3061 /*****************************************************************************/
3062
3063 static int __init stl_init(void)
3064 {
3065 int i;
3066 printk(KERN_INFO "%s: version %s\n", stl_drvtitle, stl_drvversion);
3067
3068 stl_initbrds();
3069
3070 stl_serial = alloc_tty_driver(STL_MAXBRDS * STL_MAXPORTS);
3071 if (!stl_serial)
3072 return -1;
3073
3074 /*
3075 * Allocate a temporary write buffer.
3076 */
3077 stl_tmpwritebuf = (char *) stl_memalloc(STL_TXBUFSIZE);
3078 if (stl_tmpwritebuf == (char *) NULL)
3079 printk("STALLION: failed to allocate memory (size=%d)\n",
3080 STL_TXBUFSIZE);
3081
3082 /*
3083 * Set up a character driver for per board stuff. This is mainly used
3084 * to do stats ioctls on the ports.
3085 */
3086 if (register_chrdev(STL_SIOMEMMAJOR, "staliomem", &stl_fsiomem))
3087 printk("STALLION: failed to register serial board device\n");
3088 devfs_mk_dir("staliomem");
3089
3090 stallion_class = class_create(THIS_MODULE, "staliomem");
3091 for (i = 0; i < 4; i++) {
3092 devfs_mk_cdev(MKDEV(STL_SIOMEMMAJOR, i),
3093 S_IFCHR|S_IRUSR|S_IWUSR,
3094 "staliomem/%d", i);
3095 class_device_create(stallion_class, NULL,
3096 MKDEV(STL_SIOMEMMAJOR, i), NULL,
3097 "staliomem%d", i);
3098 }
3099
3100 stl_serial->owner = THIS_MODULE;
3101 stl_serial->driver_name = stl_drvname;
3102 stl_serial->name = "ttyE";
3103 stl_serial->devfs_name = "tts/E";
3104 stl_serial->major = STL_SERIALMAJOR;
3105 stl_serial->minor_start = 0;
3106 stl_serial->type = TTY_DRIVER_TYPE_SERIAL;
3107 stl_serial->subtype = SERIAL_TYPE_NORMAL;
3108 stl_serial->init_termios = stl_deftermios;
3109 stl_serial->flags = TTY_DRIVER_REAL_RAW;
3110 tty_set_operations(stl_serial, &stl_ops);
3111
3112 if (tty_register_driver(stl_serial)) {
3113 put_tty_driver(stl_serial);
3114 printk("STALLION: failed to register serial driver\n");
3115 return -1;
3116 }
3117
3118 return(0);
3119 }
3120
3121 /*****************************************************************************/
3122 /* CD1400 HARDWARE FUNCTIONS */
3123 /*****************************************************************************/
3124
3125 /*
3126 * These functions get/set/update the registers of the cd1400 UARTs.
3127 * Access to the cd1400 registers is via an address/data io port pair.
3128 * (Maybe should make this inline...)
3129 */
3130
3131 static int stl_cd1400getreg(stlport_t *portp, int regnr)
3132 {
3133 outb((regnr + portp->uartaddr), portp->ioaddr);
3134 return(inb(portp->ioaddr + EREG_DATA));
3135 }
3136
3137 static void stl_cd1400setreg(stlport_t *portp, int regnr, int value)
3138 {
3139 outb((regnr + portp->uartaddr), portp->ioaddr);
3140 outb(value, portp->ioaddr + EREG_DATA);
3141 }
3142
3143 static int stl_cd1400updatereg(stlport_t *portp, int regnr, int value)
3144 {
3145 outb((regnr + portp->uartaddr), portp->ioaddr);
3146 if (inb(portp->ioaddr + EREG_DATA) != value) {
3147 outb(value, portp->ioaddr + EREG_DATA);
3148 return(1);
3149 }
3150 return(0);
3151 }
3152
3153 /*****************************************************************************/
3154
3155 /*
3156 * Inbitialize the UARTs in a panel. We don't care what sort of board
3157 * these ports are on - since the port io registers are almost
3158 * identical when dealing with ports.
3159 */
3160
3161 static int stl_cd1400panelinit(stlbrd_t *brdp, stlpanel_t *panelp)
3162 {
3163 unsigned int gfrcr;
3164 int chipmask, i, j;
3165 int nrchips, uartaddr, ioaddr;
3166
3167 #ifdef DEBUG
3168 printk("stl_panelinit(brdp=%x,panelp=%x)\n", (int) brdp, (int) panelp);
3169 #endif
3170
3171 BRDENABLE(panelp->brdnr, panelp->pagenr);
3172
3173 /*
3174 * Check that each chip is present and started up OK.
3175 */
3176 chipmask = 0;
3177 nrchips = panelp->nrports / CD1400_PORTS;
3178 for (i = 0; (i < nrchips); i++) {
3179 if (brdp->brdtype == BRD_ECHPCI) {
3180 outb((panelp->pagenr + (i >> 1)), brdp->ioctrl);
3181 ioaddr = panelp->iobase;
3182 } else {
3183 ioaddr = panelp->iobase + (EREG_BANKSIZE * (i >> 1));
3184 }
3185 uartaddr = (i & 0x01) ? 0x080 : 0;
3186 outb((GFRCR + uartaddr), ioaddr);
3187 outb(0, (ioaddr + EREG_DATA));
3188 outb((CCR + uartaddr), ioaddr);
3189 outb(CCR_RESETFULL, (ioaddr + EREG_DATA));
3190 outb(CCR_RESETFULL, (ioaddr + EREG_DATA));
3191 outb((GFRCR + uartaddr), ioaddr);
3192 for (j = 0; (j < CCR_MAXWAIT); j++) {
3193 if ((gfrcr = inb(ioaddr + EREG_DATA)) != 0)
3194 break;
3195 }
3196 if ((j >= CCR_MAXWAIT) || (gfrcr < 0x40) || (gfrcr > 0x60)) {
3197 printk("STALLION: cd1400 not responding, "
3198 "brd=%d panel=%d chip=%d\n",
3199 panelp->brdnr, panelp->panelnr, i);
3200 continue;
3201 }
3202 chipmask |= (0x1 << i);
3203 outb((PPR + uartaddr), ioaddr);
3204 outb(PPR_SCALAR, (ioaddr + EREG_DATA));
3205 }
3206
3207 BRDDISABLE(panelp->brdnr);
3208 return(chipmask);
3209 }
3210
3211 /*****************************************************************************/
3212
3213 /*
3214 * Initialize hardware specific port registers.
3215 */
3216
3217 static void stl_cd1400portinit(stlbrd_t *brdp, stlpanel_t *panelp, stlport_t *portp)
3218 {
3219 #ifdef DEBUG
3220 printk("stl_cd1400portinit(brdp=%x,panelp=%x,portp=%x)\n",
3221 (int) brdp, (int) panelp, (int) portp);
3222 #endif
3223
3224 if ((brdp == (stlbrd_t *) NULL) || (panelp == (stlpanel_t *) NULL) ||
3225 (portp == (stlport_t *) NULL))
3226 return;
3227
3228 portp->ioaddr = panelp->iobase + (((brdp->brdtype == BRD_ECHPCI) ||
3229 (portp->portnr < 8)) ? 0 : EREG_BANKSIZE);
3230 portp->uartaddr = (portp->portnr & 0x04) << 5;
3231 portp->pagenr = panelp->pagenr + (portp->portnr >> 3);
3232
3233 BRDENABLE(portp->brdnr, portp->pagenr);
3234 stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
3235 stl_cd1400setreg(portp, LIVR, (portp->portnr << 3));
3236 portp->hwid = stl_cd1400getreg(portp, GFRCR);
3237 BRDDISABLE(portp->brdnr);
3238 }
3239
3240 /*****************************************************************************/
3241
3242 /*
3243 * Wait for the command register to be ready. We will poll this,
3244 * since it won't usually take too long to be ready.
3245 */
3246
3247 static void stl_cd1400ccrwait(stlport_t *portp)
3248 {
3249 int i;
3250
3251 for (i = 0; (i < CCR_MAXWAIT); i++) {
3252 if (stl_cd1400getreg(portp, CCR) == 0) {
3253 return;
3254 }
3255 }
3256
3257 printk("STALLION: cd1400 not responding, port=%d panel=%d brd=%d\n",
3258 portp->portnr, portp->panelnr, portp->brdnr);
3259 }
3260
3261 /*****************************************************************************/
3262
3263 /*
3264 * Set up the cd1400 registers for a port based on the termios port
3265 * settings.
3266 */
3267
3268 static void stl_cd1400setport(stlport_t *portp, struct termios *tiosp)
3269 {
3270 stlbrd_t *brdp;
3271 unsigned long flags;
3272 unsigned int clkdiv, baudrate;
3273 unsigned char cor1, cor2, cor3;
3274 unsigned char cor4, cor5, ccr;
3275 unsigned char srer, sreron, sreroff;
3276 unsigned char mcor1, mcor2, rtpr;
3277 unsigned char clk, div;
3278
3279 cor1 = 0;
3280 cor2 = 0;
3281 cor3 = 0;
3282 cor4 = 0;
3283 cor5 = 0;
3284 ccr = 0;
3285 rtpr = 0;
3286 clk = 0;
3287 div = 0;
3288 mcor1 = 0;
3289 mcor2 = 0;
3290 sreron = 0;
3291 sreroff = 0;
3292
3293 brdp = stl_brds[portp->brdnr];
3294 if (brdp == (stlbrd_t *) NULL)
3295 return;
3296
3297 /*
3298 * Set up the RX char ignore mask with those RX error types we
3299 * can ignore. We can get the cd1400 to help us out a little here,
3300 * it will ignore parity errors and breaks for us.
3301 */
3302 portp->rxignoremsk = 0;
3303 if (tiosp->c_iflag & IGNPAR) {
3304 portp->rxignoremsk |= (ST_PARITY | ST_FRAMING | ST_OVERRUN);
3305 cor1 |= COR1_PARIGNORE;
3306 }
3307 if (tiosp->c_iflag & IGNBRK) {
3308 portp->rxignoremsk |= ST_BREAK;
3309 cor4 |= COR4_IGNBRK;
3310 }
3311
3312 portp->rxmarkmsk = ST_OVERRUN;
3313 if (tiosp->c_iflag & (INPCK | PARMRK))
3314 portp->rxmarkmsk |= (ST_PARITY | ST_FRAMING);
3315 if (tiosp->c_iflag & BRKINT)
3316 portp->rxmarkmsk |= ST_BREAK;
3317
3318 /*
3319 * Go through the char size, parity and stop bits and set all the
3320 * option register appropriately.
3321 */
3322 switch (tiosp->c_cflag & CSIZE) {
3323 case CS5:
3324 cor1 |= COR1_CHL5;
3325 break;
3326 case CS6:
3327 cor1 |= COR1_CHL6;
3328 break;
3329 case CS7:
3330 cor1 |= COR1_CHL7;
3331 break;
3332 default:
3333 cor1 |= COR1_CHL8;
3334 break;
3335 }
3336
3337 if (tiosp->c_cflag & CSTOPB)
3338 cor1 |= COR1_STOP2;
3339 else
3340 cor1 |= COR1_STOP1;
3341
3342 if (tiosp->c_cflag & PARENB) {
3343 if (tiosp->c_cflag & PARODD)
3344 cor1 |= (COR1_PARENB | COR1_PARODD);
3345 else
3346 cor1 |= (COR1_PARENB | COR1_PAREVEN);
3347 } else {
3348 cor1 |= COR1_PARNONE;
3349 }
3350
3351 /*
3352 * Set the RX FIFO threshold at 6 chars. This gives a bit of breathing
3353 * space for hardware flow control and the like. This should be set to
3354 * VMIN. Also here we will set the RX data timeout to 10ms - this should
3355 * really be based on VTIME.
3356 */
3357 cor3 |= FIFO_RXTHRESHOLD;
3358 rtpr = 2;
3359
3360 /*
3361 * Calculate the baud rate timers. For now we will just assume that
3362 * the input and output baud are the same. Could have used a baud
3363 * table here, but this way we can generate virtually any baud rate
3364 * we like!
3365 */
3366 baudrate = tiosp->c_cflag & CBAUD;
3367 if (baudrate & CBAUDEX) {
3368 baudrate &= ~CBAUDEX;
3369 if ((baudrate < 1) || (baudrate > 4))
3370 tiosp->c_cflag &= ~CBAUDEX;
3371 else
3372 baudrate += 15;
3373 }
3374 baudrate = stl_baudrates[baudrate];
3375 if ((tiosp->c_cflag & CBAUD) == B38400) {
3376 if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
3377 baudrate = 57600;
3378 else if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
3379 baudrate = 115200;
3380 else if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
3381 baudrate = 230400;
3382 else if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
3383 baudrate = 460800;
3384 else if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)
3385 baudrate = (portp->baud_base / portp->custom_divisor);
3386 }
3387 if (baudrate > STL_CD1400MAXBAUD)
3388 baudrate = STL_CD1400MAXBAUD;
3389
3390 if (baudrate > 0) {
3391 for (clk = 0; (clk < CD1400_NUMCLKS); clk++) {
3392 clkdiv = ((portp->clk / stl_cd1400clkdivs[clk]) / baudrate);
3393 if (clkdiv < 0x100)
3394 break;
3395 }
3396 div = (unsigned char) clkdiv;
3397 }
3398
3399 /*
3400 * Check what form of modem signaling is required and set it up.
3401 */
3402 if ((tiosp->c_cflag & CLOCAL) == 0) {
3403 mcor1 |= MCOR1_DCD;
3404 mcor2 |= MCOR2_DCD;
3405 sreron |= SRER_MODEM;
3406 portp->flags |= ASYNC_CHECK_CD;
3407 } else {
3408 portp->flags &= ~ASYNC_CHECK_CD;
3409 }
3410
3411 /*
3412 * Setup cd1400 enhanced modes if we can. In particular we want to
3413 * handle as much of the flow control as possible automatically. As
3414 * well as saving a few CPU cycles it will also greatly improve flow
3415 * control reliability.
3416 */
3417 if (tiosp->c_iflag & IXON) {
3418 cor2 |= COR2_TXIBE;
3419 cor3 |= COR3_SCD12;
3420 if (tiosp->c_iflag & IXANY)
3421 cor2 |= COR2_IXM;
3422 }
3423
3424 if (tiosp->c_cflag & CRTSCTS) {
3425 cor2 |= COR2_CTSAE;
3426 mcor1 |= FIFO_RTSTHRESHOLD;
3427 }
3428
3429 /*
3430 * All cd1400 register values calculated so go through and set
3431 * them all up.
3432 */
3433
3434 #ifdef DEBUG
3435 printk("SETPORT: portnr=%d panelnr=%d brdnr=%d\n",
3436 portp->portnr, portp->panelnr, portp->brdnr);
3437 printk(" cor1=%x cor2=%x cor3=%x cor4=%x cor5=%x\n",
3438 cor1, cor2, cor3, cor4, cor5);
3439 printk(" mcor1=%x mcor2=%x rtpr=%x sreron=%x sreroff=%x\n",
3440 mcor1, mcor2, rtpr, sreron, sreroff);
3441 printk(" tcor=%x tbpr=%x rcor=%x rbpr=%x\n", clk, div, clk, div);
3442 printk(" schr1=%x schr2=%x schr3=%x schr4=%x\n",
3443 tiosp->c_cc[VSTART], tiosp->c_cc[VSTOP],
3444 tiosp->c_cc[VSTART], tiosp->c_cc[VSTOP]);
3445 #endif
3446
3447 save_flags(flags);
3448 cli();
3449 BRDENABLE(portp->brdnr, portp->pagenr);
3450 stl_cd1400setreg(portp, CAR, (portp->portnr & 0x3));
3451 srer = stl_cd1400getreg(portp, SRER);
3452 stl_cd1400setreg(portp, SRER, 0);
3453 if (stl_cd1400updatereg(portp, COR1, cor1))
3454 ccr = 1;
3455 if (stl_cd1400updatereg(portp, COR2, cor2))
3456 ccr = 1;
3457 if (stl_cd1400updatereg(portp, COR3, cor3))
3458 ccr = 1;
3459 if (ccr) {
3460 stl_cd1400ccrwait(portp);
3461 stl_cd1400setreg(portp, CCR, CCR_CORCHANGE);
3462 }
3463 stl_cd1400setreg(portp, COR4, cor4);
3464 stl_cd1400setreg(portp, COR5, cor5);
3465 stl_cd1400setreg(portp, MCOR1, mcor1);
3466 stl_cd1400setreg(portp, MCOR2, mcor2);
3467 if (baudrate > 0) {
3468 stl_cd1400setreg(portp, TCOR, clk);
3469 stl_cd1400setreg(portp, TBPR, div);
3470 stl_cd1400setreg(portp, RCOR, clk);
3471 stl_cd1400setreg(portp, RBPR, div);
3472 }
3473 stl_cd1400setreg(portp, SCHR1, tiosp->c_cc[VSTART]);
3474 stl_cd1400setreg(portp, SCHR2, tiosp->c_cc[VSTOP]);
3475 stl_cd1400setreg(portp, SCHR3, tiosp->c_cc[VSTART]);
3476 stl_cd1400setreg(portp, SCHR4, tiosp->c_cc[VSTOP]);
3477 stl_cd1400setreg(portp, RTPR, rtpr);
3478 mcor1 = stl_cd1400getreg(portp, MSVR1);
3479 if (mcor1 & MSVR1_DCD)
3480 portp->sigs |= TIOCM_CD;
3481 else
3482 portp->sigs &= ~TIOCM_CD;
3483 stl_cd1400setreg(portp, SRER, ((srer & ~sreroff) | sreron));
3484 BRDDISABLE(portp->brdnr);
3485 restore_flags(flags);
3486 }
3487
3488 /*****************************************************************************/
3489
3490 /*
3491 * Set the state of the DTR and RTS signals.
3492 */
3493
3494 static void stl_cd1400setsignals(stlport_t *portp, int dtr, int rts)
3495 {
3496 unsigned char msvr1, msvr2;
3497 unsigned long flags;
3498
3499 #ifdef DEBUG
3500 printk("stl_cd1400setsignals(portp=%x,dtr=%d,rts=%d)\n",
3501 (int) portp, dtr, rts);
3502 #endif
3503
3504 msvr1 = 0;
3505 msvr2 = 0;
3506 if (dtr > 0)
3507 msvr1 = MSVR1_DTR;
3508 if (rts > 0)
3509 msvr2 = MSVR2_RTS;
3510
3511 save_flags(flags);
3512 cli();
3513 BRDENABLE(portp->brdnr, portp->pagenr);
3514 stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
3515 if (rts >= 0)
3516 stl_cd1400setreg(portp, MSVR2, msvr2);
3517 if (dtr >= 0)
3518 stl_cd1400setreg(portp, MSVR1, msvr1);
3519 BRDDISABLE(portp->brdnr);
3520 restore_flags(flags);
3521 }
3522
3523 /*****************************************************************************/
3524
3525 /*
3526 * Return the state of the signals.
3527 */
3528
3529 static int stl_cd1400getsignals(stlport_t *portp)
3530 {
3531 unsigned char msvr1, msvr2;
3532 unsigned long flags;
3533 int sigs;
3534
3535 #ifdef DEBUG
3536 printk("stl_cd1400getsignals(portp=%x)\n", (int) portp);
3537 #endif
3538
3539 save_flags(flags);
3540 cli();
3541 BRDENABLE(portp->brdnr, portp->pagenr);
3542 stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
3543 msvr1 = stl_cd1400getreg(portp, MSVR1);
3544 msvr2 = stl_cd1400getreg(portp, MSVR2);
3545 BRDDISABLE(portp->brdnr);
3546 restore_flags(flags);
3547
3548 sigs = 0;
3549 sigs |= (msvr1 & MSVR1_DCD) ? TIOCM_CD : 0;
3550 sigs |= (msvr1 & MSVR1_CTS) ? TIOCM_CTS : 0;
3551 sigs |= (msvr1 & MSVR1_DTR) ? TIOCM_DTR : 0;
3552 sigs |= (msvr2 & MSVR2_RTS) ? TIOCM_RTS : 0;
3553 #if 0
3554 sigs |= (msvr1 & MSVR1_RI) ? TIOCM_RI : 0;
3555 sigs |= (msvr1 & MSVR1_DSR) ? TIOCM_DSR : 0;
3556 #else
3557 sigs |= TIOCM_DSR;
3558 #endif
3559 return(sigs);
3560 }
3561
3562 /*****************************************************************************/
3563
3564 /*
3565 * Enable/Disable the Transmitter and/or Receiver.
3566 */
3567
3568 static void stl_cd1400enablerxtx(stlport_t *portp, int rx, int tx)
3569 {
3570 unsigned char ccr;
3571 unsigned long flags;
3572
3573 #ifdef DEBUG
3574 printk("stl_cd1400enablerxtx(portp=%x,rx=%d,tx=%d)\n",
3575 (int) portp, rx, tx);
3576 #endif
3577 ccr = 0;
3578
3579 if (tx == 0)
3580 ccr |= CCR_TXDISABLE;
3581 else if (tx > 0)
3582 ccr |= CCR_TXENABLE;
3583 if (rx == 0)
3584 ccr |= CCR_RXDISABLE;
3585 else if (rx > 0)
3586 ccr |= CCR_RXENABLE;
3587
3588 save_flags(flags);
3589 cli();
3590 BRDENABLE(portp->brdnr, portp->pagenr);
3591 stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
3592 stl_cd1400ccrwait(portp);
3593 stl_cd1400setreg(portp, CCR, ccr);
3594 stl_cd1400ccrwait(portp);
3595 BRDDISABLE(portp->brdnr);
3596 restore_flags(flags);
3597 }
3598
3599 /*****************************************************************************/
3600
3601 /*
3602 * Start/stop the Transmitter and/or Receiver.
3603 */
3604
3605 static void stl_cd1400startrxtx(stlport_t *portp, int rx, int tx)
3606 {
3607 unsigned char sreron, sreroff;
3608 unsigned long flags;
3609
3610 #ifdef DEBUG
3611 printk("stl_cd1400startrxtx(portp=%x,rx=%d,tx=%d)\n",
3612 (int) portp, rx, tx);
3613 #endif
3614
3615 sreron = 0;
3616 sreroff = 0;
3617 if (tx == 0)
3618 sreroff |= (SRER_TXDATA | SRER_TXEMPTY);
3619 else if (tx == 1)
3620 sreron |= SRER_TXDATA;
3621 else if (tx >= 2)
3622 sreron |= SRER_TXEMPTY;
3623 if (rx == 0)
3624 sreroff |= SRER_RXDATA;
3625 else if (rx > 0)
3626 sreron |= SRER_RXDATA;
3627
3628 save_flags(flags);
3629 cli();
3630 BRDENABLE(portp->brdnr, portp->pagenr);
3631 stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
3632 stl_cd1400setreg(portp, SRER,
3633 ((stl_cd1400getreg(portp, SRER) & ~sreroff) | sreron));
3634 BRDDISABLE(portp->brdnr);
3635 if (tx > 0)
3636 set_bit(ASYI_TXBUSY, &portp->istate);
3637 restore_flags(flags);
3638 }
3639
3640 /*****************************************************************************/
3641
3642 /*
3643 * Disable all interrupts from this port.
3644 */
3645
3646 static void stl_cd1400disableintrs(stlport_t *portp)
3647 {
3648 unsigned long flags;
3649
3650 #ifdef DEBUG
3651 printk("stl_cd1400disableintrs(portp=%x)\n", (int) portp);
3652 #endif
3653 save_flags(flags);
3654 cli();
3655 BRDENABLE(portp->brdnr, portp->pagenr);
3656 stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
3657 stl_cd1400setreg(portp, SRER, 0);
3658 BRDDISABLE(portp->brdnr);
3659 restore_flags(flags);
3660 }
3661
3662 /*****************************************************************************/
3663
3664 static void stl_cd1400sendbreak(stlport_t *portp, int len)
3665 {
3666 unsigned long flags;
3667
3668 #ifdef DEBUG
3669 printk("stl_cd1400sendbreak(portp=%x,len=%d)\n", (int) portp, len);
3670 #endif
3671
3672 save_flags(flags);
3673 cli();
3674 BRDENABLE(portp->brdnr, portp->pagenr);
3675 stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
3676 stl_cd1400setreg(portp, SRER,
3677 ((stl_cd1400getreg(portp, SRER) & ~SRER_TXDATA) |
3678 SRER_TXEMPTY));
3679 BRDDISABLE(portp->brdnr);
3680 portp->brklen = len;
3681 if (len == 1)
3682 portp->stats.txbreaks++;
3683 restore_flags(flags);
3684 }
3685
3686 /*****************************************************************************/
3687
3688 /*
3689 * Take flow control actions...
3690 */
3691
3692 static void stl_cd1400flowctrl(stlport_t *portp, int state)
3693 {
3694 struct tty_struct *tty;
3695 unsigned long flags;
3696
3697 #ifdef DEBUG
3698 printk("stl_cd1400flowctrl(portp=%x,state=%x)\n", (int) portp, state);
3699 #endif
3700
3701 if (portp == (stlport_t *) NULL)
3702 return;
3703 tty = portp->tty;
3704 if (tty == (struct tty_struct *) NULL)
3705 return;
3706
3707 save_flags(flags);
3708 cli();
3709 BRDENABLE(portp->brdnr, portp->pagenr);
3710 stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
3711
3712 if (state) {
3713 if (tty->termios->c_iflag & IXOFF) {
3714 stl_cd1400ccrwait(portp);
3715 stl_cd1400setreg(portp, CCR, CCR_SENDSCHR1);
3716 portp->stats.rxxon++;
3717 stl_cd1400ccrwait(portp);
3718 }
3719 /*
3720 * Question: should we return RTS to what it was before? It may
3721 * have been set by an ioctl... Suppose not, since if you have
3722 * hardware flow control set then it is pretty silly to go and
3723 * set the RTS line by hand.
3724 */
3725 if (tty->termios->c_cflag & CRTSCTS) {
3726 stl_cd1400setreg(portp, MCOR1,
3727 (stl_cd1400getreg(portp, MCOR1) |
3728 FIFO_RTSTHRESHOLD));
3729 stl_cd1400setreg(portp, MSVR2, MSVR2_RTS);
3730 portp->stats.rxrtson++;
3731 }
3732 } else {
3733 if (tty->termios->c_iflag & IXOFF) {
3734 stl_cd1400ccrwait(portp);
3735 stl_cd1400setreg(portp, CCR, CCR_SENDSCHR2);
3736 portp->stats.rxxoff++;
3737 stl_cd1400ccrwait(portp);
3738 }
3739 if (tty->termios->c_cflag & CRTSCTS) {
3740 stl_cd1400setreg(portp, MCOR1,
3741 (stl_cd1400getreg(portp, MCOR1) & 0xf0));
3742 stl_cd1400setreg(portp, MSVR2, 0);
3743 portp->stats.rxrtsoff++;
3744 }
3745 }
3746
3747 BRDDISABLE(portp->brdnr);
3748 restore_flags(flags);
3749 }
3750
3751 /*****************************************************************************/
3752
3753 /*
3754 * Send a flow control character...
3755 */
3756
3757 static void stl_cd1400sendflow(stlport_t *portp, int state)
3758 {
3759 struct tty_struct *tty;
3760 unsigned long flags;
3761
3762 #ifdef DEBUG
3763 printk("stl_cd1400sendflow(portp=%x,state=%x)\n", (int) portp, state);
3764 #endif
3765
3766 if (portp == (stlport_t *) NULL)
3767 return;
3768 tty = portp->tty;
3769 if (tty == (struct tty_struct *) NULL)
3770 return;
3771
3772 save_flags(flags);
3773 cli();
3774 BRDENABLE(portp->brdnr, portp->pagenr);
3775 stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
3776 if (state) {
3777 stl_cd1400ccrwait(portp);
3778 stl_cd1400setreg(portp, CCR, CCR_SENDSCHR1);
3779 portp->stats.rxxon++;
3780 stl_cd1400ccrwait(portp);
3781 } else {
3782 stl_cd1400ccrwait(portp);
3783 stl_cd1400setreg(portp, CCR, CCR_SENDSCHR2);
3784 portp->stats.rxxoff++;
3785 stl_cd1400ccrwait(portp);
3786 }
3787 BRDDISABLE(portp->brdnr);
3788 restore_flags(flags);
3789 }
3790
3791 /*****************************************************************************/
3792
3793 static void stl_cd1400flush(stlport_t *portp)
3794 {
3795 unsigned long flags;
3796
3797 #ifdef DEBUG
3798 printk("stl_cd1400flush(portp=%x)\n", (int) portp);
3799 #endif
3800
3801 if (portp == (stlport_t *) NULL)
3802 return;
3803
3804 save_flags(flags);
3805 cli();
3806 BRDENABLE(portp->brdnr, portp->pagenr);
3807 stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
3808 stl_cd1400ccrwait(portp);
3809 stl_cd1400setreg(portp, CCR, CCR_TXFLUSHFIFO);
3810 stl_cd1400ccrwait(portp);
3811 portp->tx.tail = portp->tx.head;
3812 BRDDISABLE(portp->brdnr);
3813 restore_flags(flags);
3814 }
3815
3816 /*****************************************************************************/
3817
3818 /*
3819 * Return the current state of data flow on this port. This is only
3820 * really interresting when determining if data has fully completed
3821 * transmission or not... This is easy for the cd1400, it accurately
3822 * maintains the busy port flag.
3823 */
3824
3825 static int stl_cd1400datastate(stlport_t *portp)
3826 {
3827 #ifdef DEBUG
3828 printk("stl_cd1400datastate(portp=%x)\n", (int) portp);
3829 #endif
3830
3831 if (portp == (stlport_t *) NULL)
3832 return(0);
3833
3834 return(test_bit(ASYI_TXBUSY, &portp->istate) ? 1 : 0);
3835 }
3836
3837 /*****************************************************************************/
3838
3839 /*
3840 * Interrupt service routine for cd1400 EasyIO boards.
3841 */
3842
3843 static void stl_cd1400eiointr(stlpanel_t *panelp, unsigned int iobase)
3844 {
3845 unsigned char svrtype;
3846
3847 #ifdef DEBUG
3848 printk("stl_cd1400eiointr(panelp=%x,iobase=%x)\n",
3849 (int) panelp, iobase);
3850 #endif
3851
3852 outb(SVRR, iobase);
3853 svrtype = inb(iobase + EREG_DATA);
3854 if (panelp->nrports > 4) {
3855 outb((SVRR + 0x80), iobase);
3856 svrtype |= inb(iobase + EREG_DATA);
3857 }
3858
3859 if (svrtype & SVRR_RX)
3860 stl_cd1400rxisr(panelp, iobase);
3861 else if (svrtype & SVRR_TX)
3862 stl_cd1400txisr(panelp, iobase);
3863 else if (svrtype & SVRR_MDM)
3864 stl_cd1400mdmisr(panelp, iobase);
3865 }
3866
3867 /*****************************************************************************/
3868
3869 /*
3870 * Interrupt service routine for cd1400 panels.
3871 */
3872
3873 static void stl_cd1400echintr(stlpanel_t *panelp, unsigned int iobase)
3874 {
3875 unsigned char svrtype;
3876
3877 #ifdef DEBUG
3878 printk("stl_cd1400echintr(panelp=%x,iobase=%x)\n", (int) panelp,
3879 iobase);
3880 #endif
3881
3882 outb(SVRR, iobase);
3883 svrtype = inb(iobase + EREG_DATA);
3884 outb((SVRR + 0x80), iobase);
3885 svrtype |= inb(iobase + EREG_DATA);
3886 if (svrtype & SVRR_RX)
3887 stl_cd1400rxisr(panelp, iobase);
3888 else if (svrtype & SVRR_TX)
3889 stl_cd1400txisr(panelp, iobase);
3890 else if (svrtype & SVRR_MDM)
3891 stl_cd1400mdmisr(panelp, iobase);
3892 }
3893
3894
3895 /*****************************************************************************/
3896
3897 /*
3898 * Unfortunately we need to handle breaks in the TX data stream, since
3899 * this is the only way to generate them on the cd1400.
3900 */
3901
3902 static inline int stl_cd1400breakisr(stlport_t *portp, int ioaddr)
3903 {
3904 if (portp->brklen == 1) {
3905 outb((COR2 + portp->uartaddr), ioaddr);
3906 outb((inb(ioaddr + EREG_DATA) | COR2_ETC),
3907 (ioaddr + EREG_DATA));
3908 outb((TDR + portp->uartaddr), ioaddr);
3909 outb(ETC_CMD, (ioaddr + EREG_DATA));
3910 outb(ETC_STARTBREAK, (ioaddr + EREG_DATA));
3911 outb((SRER + portp->uartaddr), ioaddr);
3912 outb((inb(ioaddr + EREG_DATA) & ~(SRER_TXDATA | SRER_TXEMPTY)),
3913 (ioaddr + EREG_DATA));
3914 return(1);
3915 } else if (portp->brklen > 1) {
3916 outb((TDR + portp->uartaddr), ioaddr);
3917 outb(ETC_CMD, (ioaddr + EREG_DATA));
3918 outb(ETC_STOPBREAK, (ioaddr + EREG_DATA));
3919 portp->brklen = -1;
3920 return(1);
3921 } else {
3922 outb((COR2 + portp->uartaddr), ioaddr);
3923 outb((inb(ioaddr + EREG_DATA) & ~COR2_ETC),
3924 (ioaddr + EREG_DATA));
3925 portp->brklen = 0;
3926 }
3927 return(0);
3928 }
3929
3930 /*****************************************************************************/
3931
3932 /*
3933 * Transmit interrupt handler. This has gotta be fast! Handling TX
3934 * chars is pretty simple, stuff as many as possible from the TX buffer
3935 * into the cd1400 FIFO. Must also handle TX breaks here, since they
3936 * are embedded as commands in the data stream. Oh no, had to use a goto!
3937 * This could be optimized more, will do when I get time...
3938 * In practice it is possible that interrupts are enabled but that the
3939 * port has been hung up. Need to handle not having any TX buffer here,
3940 * this is done by using the side effect that head and tail will also
3941 * be NULL if the buffer has been freed.
3942 */
3943
3944 static void stl_cd1400txisr(stlpanel_t *panelp, int ioaddr)
3945 {
3946 stlport_t *portp;
3947 int len, stlen;
3948 char *head, *tail;
3949 unsigned char ioack, srer;
3950
3951 #ifdef DEBUG
3952 printk("stl_cd1400txisr(panelp=%x,ioaddr=%x)\n", (int) panelp, ioaddr);
3953 #endif
3954
3955 ioack = inb(ioaddr + EREG_TXACK);
3956 if (((ioack & panelp->ackmask) != 0) ||
3957 ((ioack & ACK_TYPMASK) != ACK_TYPTX)) {
3958 printk("STALLION: bad TX interrupt ack value=%x\n", ioack);
3959 return;
3960 }
3961 portp = panelp->ports[(ioack >> 3)];
3962
3963 /*
3964 * Unfortunately we need to handle breaks in the data stream, since
3965 * this is the only way to generate them on the cd1400. Do it now if
3966 * a break is to be sent.
3967 */
3968 if (portp->brklen != 0)
3969 if (stl_cd1400breakisr(portp, ioaddr))
3970 goto stl_txalldone;
3971
3972 head = portp->tx.head;
3973 tail = portp->tx.tail;
3974 len = (head >= tail) ? (head - tail) : (STL_TXBUFSIZE - (tail - head));
3975 if ((len == 0) || ((len < STL_TXBUFLOW) &&
3976 (test_bit(ASYI_TXLOW, &portp->istate) == 0))) {
3977 set_bit(ASYI_TXLOW, &portp->istate);
3978 schedule_work(&portp->tqueue);
3979 }
3980
3981 if (len == 0) {
3982 outb((SRER + portp->uartaddr), ioaddr);
3983 srer = inb(ioaddr + EREG_DATA);
3984 if (srer & SRER_TXDATA) {
3985 srer = (srer & ~SRER_TXDATA) | SRER_TXEMPTY;
3986 } else {
3987 srer &= ~(SRER_TXDATA | SRER_TXEMPTY);
3988 clear_bit(ASYI_TXBUSY, &portp->istate);
3989 }
3990 outb(srer, (ioaddr + EREG_DATA));
3991 } else {
3992 len = MIN(len, CD1400_TXFIFOSIZE);
3993 portp->stats.txtotal += len;
3994 stlen = MIN(len, ((portp->tx.buf + STL_TXBUFSIZE) - tail));
3995 outb((TDR + portp->uartaddr), ioaddr);
3996 outsb((ioaddr + EREG_DATA), tail, stlen);
3997 len -= stlen;
3998 tail += stlen;
3999 if (tail >= (portp->tx.buf + STL_TXBUFSIZE))
4000 tail = portp->tx.buf;
4001 if (len > 0) {
4002 outsb((ioaddr + EREG_DATA), tail, len);
4003 tail += len;
4004 }
4005 portp->tx.tail = tail;
4006 }
4007
4008 stl_txalldone:
4009 outb((EOSRR + portp->uartaddr), ioaddr);
4010 outb(0, (ioaddr + EREG_DATA));
4011 }
4012
4013 /*****************************************************************************/
4014
4015 /*
4016 * Receive character interrupt handler. Determine if we have good chars
4017 * or bad chars and then process appropriately. Good chars are easy
4018 * just shove the lot into the RX buffer and set all status byte to 0.
4019 * If a bad RX char then process as required. This routine needs to be
4020 * fast! In practice it is possible that we get an interrupt on a port
4021 * that is closed. This can happen on hangups - since they completely
4022 * shutdown a port not in user context. Need to handle this case.
4023 */
4024
4025 static void stl_cd1400rxisr(stlpanel_t *panelp, int ioaddr)
4026 {
4027 stlport_t *portp;
4028 struct tty_struct *tty;
4029 unsigned int ioack, len, buflen;
4030 unsigned char status;
4031 char ch;
4032
4033 #ifdef DEBUG
4034 printk("stl_cd1400rxisr(panelp=%x,ioaddr=%x)\n", (int) panelp, ioaddr);
4035 #endif
4036
4037 ioack = inb(ioaddr + EREG_RXACK);
4038 if ((ioack & panelp->ackmask) != 0) {
4039 printk("STALLION: bad RX interrupt ack value=%x\n", ioack);
4040 return;
4041 }
4042 portp = panelp->ports[(ioack >> 3)];
4043 tty = portp->tty;
4044
4045 if ((ioack & ACK_TYPMASK) == ACK_TYPRXGOOD) {
4046 outb((RDCR + portp->uartaddr), ioaddr);
4047 len = inb(ioaddr + EREG_DATA);
4048 if ((tty == (struct tty_struct *) NULL) ||
4049 (tty->flip.char_buf_ptr == (char *) NULL) ||
4050 ((buflen = TTY_FLIPBUF_SIZE - tty->flip.count) == 0)) {
4051 len = MIN(len, sizeof(stl_unwanted));
4052 outb((RDSR + portp->uartaddr), ioaddr);
4053 insb((ioaddr + EREG_DATA), &stl_unwanted[0], len);
4054 portp->stats.rxlost += len;
4055 portp->stats.rxtotal += len;
4056 } else {
4057 len = MIN(len, buflen);
4058 if (len > 0) {
4059 outb((RDSR + portp->uartaddr), ioaddr);
4060 insb((ioaddr + EREG_DATA), tty->flip.char_buf_ptr, len);
4061 memset(tty->flip.flag_buf_ptr, 0, len);
4062 tty->flip.flag_buf_ptr += len;
4063 tty->flip.char_buf_ptr += len;
4064 tty->flip.count += len;
4065 tty_schedule_flip(tty);
4066 portp->stats.rxtotal += len;
4067 }
4068 }
4069 } else if ((ioack & ACK_TYPMASK) == ACK_TYPRXBAD) {
4070 outb((RDSR + portp->uartaddr), ioaddr);
4071 status = inb(ioaddr + EREG_DATA);
4072 ch = inb(ioaddr + EREG_DATA);
4073 if (status & ST_PARITY)
4074 portp->stats.rxparity++;
4075 if (status & ST_FRAMING)
4076 portp->stats.rxframing++;
4077 if (status & ST_OVERRUN)
4078 portp->stats.rxoverrun++;
4079 if (status & ST_BREAK)
4080 portp->stats.rxbreaks++;
4081 if (status & ST_SCHARMASK) {
4082 if ((status & ST_SCHARMASK) == ST_SCHAR1)
4083 portp->stats.txxon++;
4084 if ((status & ST_SCHARMASK) == ST_SCHAR2)
4085 portp->stats.txxoff++;
4086 goto stl_rxalldone;
4087 }
4088 if ((tty != (struct tty_struct *) NULL) &&
4089 ((portp->rxignoremsk & status) == 0)) {
4090 if (portp->rxmarkmsk & status) {
4091 if (status & ST_BREAK) {
4092 status = TTY_BREAK;
4093 if (portp->flags & ASYNC_SAK) {
4094 do_SAK(tty);
4095 BRDENABLE(portp->brdnr, portp->pagenr);
4096 }
4097 } else if (status & ST_PARITY) {
4098 status = TTY_PARITY;
4099 } else if (status & ST_FRAMING) {
4100 status = TTY_FRAME;
4101 } else if(status & ST_OVERRUN) {
4102 status = TTY_OVERRUN;
4103 } else {
4104 status = 0;
4105 }
4106 } else {
4107 status = 0;
4108 }
4109 if (tty->flip.char_buf_ptr != (char *) NULL) {
4110 if (tty->flip.count < TTY_FLIPBUF_SIZE) {
4111 *tty->flip.flag_buf_ptr++ = status;
4112 *tty->flip.char_buf_ptr++ = ch;
4113 tty->flip.count++;
4114 }
4115 tty_schedule_flip(tty);
4116 }
4117 }
4118 } else {
4119 printk("STALLION: bad RX interrupt ack value=%x\n", ioack);
4120 return;
4121 }
4122
4123 stl_rxalldone:
4124 outb((EOSRR + portp->uartaddr), ioaddr);
4125 outb(0, (ioaddr + EREG_DATA));
4126 }
4127
4128 /*****************************************************************************/
4129
4130 /*
4131 * Modem interrupt handler. The is called when the modem signal line
4132 * (DCD) has changed state. Leave most of the work to the off-level
4133 * processing routine.
4134 */
4135
4136 static void stl_cd1400mdmisr(stlpanel_t *panelp, int ioaddr)
4137 {
4138 stlport_t *portp;
4139 unsigned int ioack;
4140 unsigned char misr;
4141
4142 #ifdef DEBUG
4143 printk("stl_cd1400mdmisr(panelp=%x)\n", (int) panelp);
4144 #endif
4145
4146 ioack = inb(ioaddr + EREG_MDACK);
4147 if (((ioack & panelp->ackmask) != 0) ||
4148 ((ioack & ACK_TYPMASK) != ACK_TYPMDM)) {
4149 printk("STALLION: bad MODEM interrupt ack value=%x\n", ioack);
4150 return;
4151 }
4152 portp = panelp->ports[(ioack >> 3)];
4153
4154 outb((MISR + portp->uartaddr), ioaddr);
4155 misr = inb(ioaddr + EREG_DATA);
4156 if (misr & MISR_DCD) {
4157 set_bit(ASYI_DCDCHANGE, &portp->istate);
4158 schedule_work(&portp->tqueue);
4159 portp->stats.modem++;
4160 }
4161
4162 outb((EOSRR + portp->uartaddr), ioaddr);
4163 outb(0, (ioaddr + EREG_DATA));
4164 }
4165
4166 /*****************************************************************************/
4167 /* SC26198 HARDWARE FUNCTIONS */
4168 /*****************************************************************************/
4169
4170 /*
4171 * These functions get/set/update the registers of the sc26198 UARTs.
4172 * Access to the sc26198 registers is via an address/data io port pair.
4173 * (Maybe should make this inline...)
4174 */
4175
4176 static int stl_sc26198getreg(stlport_t *portp, int regnr)
4177 {
4178 outb((regnr | portp->uartaddr), (portp->ioaddr + XP_ADDR));
4179 return(inb(portp->ioaddr + XP_DATA));
4180 }
4181
4182 static void stl_sc26198setreg(stlport_t *portp, int regnr, int value)
4183 {
4184 outb((regnr | portp->uartaddr), (portp->ioaddr + XP_ADDR));
4185 outb(value, (portp->ioaddr + XP_DATA));
4186 }
4187
4188 static int stl_sc26198updatereg(stlport_t *portp, int regnr, int value)
4189 {
4190 outb((regnr | portp->uartaddr), (portp->ioaddr + XP_ADDR));
4191 if (inb(portp->ioaddr + XP_DATA) != value) {
4192 outb(value, (portp->ioaddr + XP_DATA));
4193 return(1);
4194 }
4195 return(0);
4196 }
4197
4198 /*****************************************************************************/
4199
4200 /*
4201 * Functions to get and set the sc26198 global registers.
4202 */
4203
4204 static int stl_sc26198getglobreg(stlport_t *portp, int regnr)
4205 {
4206 outb(regnr, (portp->ioaddr + XP_ADDR));
4207 return(inb(portp->ioaddr + XP_DATA));
4208 }
4209
4210 #if 0
4211 static void stl_sc26198setglobreg(stlport_t *portp, int regnr, int value)
4212 {
4213 outb(regnr, (portp->ioaddr + XP_ADDR));
4214 outb(value, (portp->ioaddr + XP_DATA));
4215 }
4216 #endif
4217
4218 /*****************************************************************************/
4219
4220 /*
4221 * Inbitialize the UARTs in a panel. We don't care what sort of board
4222 * these ports are on - since the port io registers are almost
4223 * identical when dealing with ports.
4224 */
4225
4226 static int stl_sc26198panelinit(stlbrd_t *brdp, stlpanel_t *panelp)
4227 {
4228 int chipmask, i;
4229 int nrchips, ioaddr;
4230
4231 #ifdef DEBUG
4232 printk("stl_sc26198panelinit(brdp=%x,panelp=%x)\n",
4233 (int) brdp, (int) panelp);
4234 #endif
4235
4236 BRDENABLE(panelp->brdnr, panelp->pagenr);
4237
4238 /*
4239 * Check that each chip is present and started up OK.
4240 */
4241 chipmask = 0;
4242 nrchips = (panelp->nrports + 4) / SC26198_PORTS;
4243 if (brdp->brdtype == BRD_ECHPCI)
4244 outb(panelp->pagenr, brdp->ioctrl);
4245
4246 for (i = 0; (i < nrchips); i++) {
4247 ioaddr = panelp->iobase + (i * 4);
4248 outb(SCCR, (ioaddr + XP_ADDR));
4249 outb(CR_RESETALL, (ioaddr + XP_DATA));
4250 outb(TSTR, (ioaddr + XP_ADDR));
4251 if (inb(ioaddr + XP_DATA) != 0) {
4252 printk("STALLION: sc26198 not responding, "
4253 "brd=%d panel=%d chip=%d\n",
4254 panelp->brdnr, panelp->panelnr, i);
4255 continue;
4256 }
4257 chipmask |= (0x1 << i);
4258 outb(GCCR, (ioaddr + XP_ADDR));
4259 outb(GCCR_IVRTYPCHANACK, (ioaddr + XP_DATA));
4260 outb(WDTRCR, (ioaddr + XP_ADDR));
4261 outb(0xff, (ioaddr + XP_DATA));
4262 }
4263
4264 BRDDISABLE(panelp->brdnr);
4265 return(chipmask);
4266 }
4267
4268 /*****************************************************************************/
4269
4270 /*
4271 * Initialize hardware specific port registers.
4272 */
4273
4274 static void stl_sc26198portinit(stlbrd_t *brdp, stlpanel_t *panelp, stlport_t *portp)
4275 {
4276 #ifdef DEBUG
4277 printk("stl_sc26198portinit(brdp=%x,panelp=%x,portp=%x)\n",
4278 (int) brdp, (int) panelp, (int) portp);
4279 #endif
4280
4281 if ((brdp == (stlbrd_t *) NULL) || (panelp == (stlpanel_t *) NULL) ||
4282 (portp == (stlport_t *) NULL))
4283 return;
4284
4285 portp->ioaddr = panelp->iobase + ((portp->portnr < 8) ? 0 : 4);
4286 portp->uartaddr = (portp->portnr & 0x07) << 4;
4287 portp->pagenr = panelp->pagenr;
4288 portp->hwid = 0x1;
4289
4290 BRDENABLE(portp->brdnr, portp->pagenr);
4291 stl_sc26198setreg(portp, IOPCR, IOPCR_SETSIGS);
4292 BRDDISABLE(portp->brdnr);
4293 }
4294
4295 /*****************************************************************************/
4296
4297 /*
4298 * Set up the sc26198 registers for a port based on the termios port
4299 * settings.
4300 */
4301
4302 static void stl_sc26198setport(stlport_t *portp, struct termios *tiosp)
4303 {
4304 stlbrd_t *brdp;
4305 unsigned long flags;
4306 unsigned int baudrate;
4307 unsigned char mr0, mr1, mr2, clk;
4308 unsigned char imron, imroff, iopr, ipr;
4309
4310 mr0 = 0;
4311 mr1 = 0;
4312 mr2 = 0;
4313 clk = 0;
4314 iopr = 0;
4315 imron = 0;
4316 imroff = 0;
4317
4318 brdp = stl_brds[portp->brdnr];
4319 if (brdp == (stlbrd_t *) NULL)
4320 return;
4321
4322 /*
4323 * Set up the RX char ignore mask with those RX error types we
4324 * can ignore.
4325 */
4326 portp->rxignoremsk = 0;
4327 if (tiosp->c_iflag & IGNPAR)
4328 portp->rxignoremsk |= (SR_RXPARITY | SR_RXFRAMING |
4329 SR_RXOVERRUN);
4330 if (tiosp->c_iflag & IGNBRK)
4331 portp->rxignoremsk |= SR_RXBREAK;
4332
4333 portp->rxmarkmsk = SR_RXOVERRUN;
4334 if (tiosp->c_iflag & (INPCK | PARMRK))
4335 portp->rxmarkmsk |= (SR_RXPARITY | SR_RXFRAMING);
4336 if (tiosp->c_iflag & BRKINT)
4337 portp->rxmarkmsk |= SR_RXBREAK;
4338
4339 /*
4340 * Go through the char size, parity and stop bits and set all the
4341 * option register appropriately.
4342 */
4343 switch (tiosp->c_cflag & CSIZE) {
4344 case CS5:
4345 mr1 |= MR1_CS5;
4346 break;
4347 case CS6:
4348 mr1 |= MR1_CS6;
4349 break;
4350 case CS7:
4351 mr1 |= MR1_CS7;
4352 break;
4353 default:
4354 mr1 |= MR1_CS8;
4355 break;
4356 }
4357
4358 if (tiosp->c_cflag & CSTOPB)
4359 mr2 |= MR2_STOP2;
4360 else
4361 mr2 |= MR2_STOP1;
4362
4363 if (tiosp->c_cflag & PARENB) {
4364 if (tiosp->c_cflag & PARODD)
4365 mr1 |= (MR1_PARENB | MR1_PARODD);
4366 else
4367 mr1 |= (MR1_PARENB | MR1_PAREVEN);
4368 } else {
4369 mr1 |= MR1_PARNONE;
4370 }
4371
4372 mr1 |= MR1_ERRBLOCK;
4373
4374 /*
4375 * Set the RX FIFO threshold at 8 chars. This gives a bit of breathing
4376 * space for hardware flow control and the like. This should be set to
4377 * VMIN.
4378 */
4379 mr2 |= MR2_RXFIFOHALF;
4380
4381 /*
4382 * Calculate the baud rate timers. For now we will just assume that
4383 * the input and output baud are the same. The sc26198 has a fixed
4384 * baud rate table, so only discrete baud rates possible.
4385 */
4386 baudrate = tiosp->c_cflag & CBAUD;
4387 if (baudrate & CBAUDEX) {
4388 baudrate &= ~CBAUDEX;
4389 if ((baudrate < 1) || (baudrate > 4))
4390 tiosp->c_cflag &= ~CBAUDEX;
4391 else
4392 baudrate += 15;
4393 }
4394 baudrate = stl_baudrates[baudrate];
4395 if ((tiosp->c_cflag & CBAUD) == B38400) {
4396 if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
4397 baudrate = 57600;
4398 else if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
4399 baudrate = 115200;
4400 else if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
4401 baudrate = 230400;
4402 else if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
4403 baudrate = 460800;
4404 else if ((portp->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)
4405 baudrate = (portp->baud_base / portp->custom_divisor);
4406 }
4407 if (baudrate > STL_SC26198MAXBAUD)
4408 baudrate = STL_SC26198MAXBAUD;
4409
4410 if (baudrate > 0) {
4411 for (clk = 0; (clk < SC26198_NRBAUDS); clk++) {
4412 if (baudrate <= sc26198_baudtable[clk])
4413 break;
4414 }
4415 }
4416
4417 /*
4418 * Check what form of modem signaling is required and set it up.
4419 */
4420 if (tiosp->c_cflag & CLOCAL) {
4421 portp->flags &= ~ASYNC_CHECK_CD;
4422 } else {
4423 iopr |= IOPR_DCDCOS;
4424 imron |= IR_IOPORT;
4425 portp->flags |= ASYNC_CHECK_CD;
4426 }
4427
4428 /*
4429 * Setup sc26198 enhanced modes if we can. In particular we want to
4430 * handle as much of the flow control as possible automatically. As
4431 * well as saving a few CPU cycles it will also greatly improve flow
4432 * control reliability.
4433 */
4434 if (tiosp->c_iflag & IXON) {
4435 mr0 |= MR0_SWFTX | MR0_SWFT;
4436 imron |= IR_XONXOFF;
4437 } else {
4438 imroff |= IR_XONXOFF;
4439 }
4440 if (tiosp->c_iflag & IXOFF)
4441 mr0 |= MR0_SWFRX;
4442
4443 if (tiosp->c_cflag & CRTSCTS) {
4444 mr2 |= MR2_AUTOCTS;
4445 mr1 |= MR1_AUTORTS;
4446 }
4447
4448 /*
4449 * All sc26198 register values calculated so go through and set
4450 * them all up.
4451 */
4452
4453 #ifdef DEBUG
4454 printk("SETPORT: portnr=%d panelnr=%d brdnr=%d\n",
4455 portp->portnr, portp->panelnr, portp->brdnr);
4456 printk(" mr0=%x mr1=%x mr2=%x clk=%x\n", mr0, mr1, mr2, clk);
4457 printk(" iopr=%x imron=%x imroff=%x\n", iopr, imron, imroff);
4458 printk(" schr1=%x schr2=%x schr3=%x schr4=%x\n",
4459 tiosp->c_cc[VSTART], tiosp->c_cc[VSTOP],
4460 tiosp->c_cc[VSTART], tiosp->c_cc[VSTOP]);
4461 #endif
4462
4463 save_flags(flags);
4464 cli();
4465 BRDENABLE(portp->brdnr, portp->pagenr);
4466 stl_sc26198setreg(portp, IMR, 0);
4467 stl_sc26198updatereg(portp, MR0, mr0);
4468 stl_sc26198updatereg(portp, MR1, mr1);
4469 stl_sc26198setreg(portp, SCCR, CR_RXERRBLOCK);
4470 stl_sc26198updatereg(portp, MR2, mr2);
4471 stl_sc26198updatereg(portp, IOPIOR,
4472 ((stl_sc26198getreg(portp, IOPIOR) & ~IPR_CHANGEMASK) | iopr));
4473
4474 if (baudrate > 0) {
4475 stl_sc26198setreg(portp, TXCSR, clk);
4476 stl_sc26198setreg(portp, RXCSR, clk);
4477 }
4478
4479 stl_sc26198setreg(portp, XONCR, tiosp->c_cc[VSTART]);
4480 stl_sc26198setreg(portp, XOFFCR, tiosp->c_cc[VSTOP]);
4481
4482 ipr = stl_sc26198getreg(portp, IPR);
4483 if (ipr & IPR_DCD)
4484 portp->sigs &= ~TIOCM_CD;
4485 else
4486 portp->sigs |= TIOCM_CD;
4487
4488 portp->imr = (portp->imr & ~imroff) | imron;
4489 stl_sc26198setreg(portp, IMR, portp->imr);
4490 BRDDISABLE(portp->brdnr);
4491 restore_flags(flags);
4492 }
4493
4494 /*****************************************************************************/
4495
4496 /*
4497 * Set the state of the DTR and RTS signals.
4498 */
4499
4500 static void stl_sc26198setsignals(stlport_t *portp, int dtr, int rts)
4501 {
4502 unsigned char iopioron, iopioroff;
4503 unsigned long flags;
4504
4505 #ifdef DEBUG
4506 printk("stl_sc26198setsignals(portp=%x,dtr=%d,rts=%d)\n",
4507 (int) portp, dtr, rts);
4508 #endif
4509
4510 iopioron = 0;
4511 iopioroff = 0;
4512 if (dtr == 0)
4513 iopioroff |= IPR_DTR;
4514 else if (dtr > 0)
4515 iopioron |= IPR_DTR;
4516 if (rts == 0)
4517 iopioroff |= IPR_RTS;
4518 else if (rts > 0)
4519 iopioron |= IPR_RTS;
4520
4521 save_flags(flags);
4522 cli();
4523 BRDENABLE(portp->brdnr, portp->pagenr);
4524 stl_sc26198setreg(portp, IOPIOR,
4525 ((stl_sc26198getreg(portp, IOPIOR) & ~iopioroff) | iopioron));
4526 BRDDISABLE(portp->brdnr);
4527 restore_flags(flags);
4528 }
4529
4530 /*****************************************************************************/
4531
4532 /*
4533 * Return the state of the signals.
4534 */
4535
4536 static int stl_sc26198getsignals(stlport_t *portp)
4537 {
4538 unsigned char ipr;
4539 unsigned long flags;
4540 int sigs;
4541
4542 #ifdef DEBUG
4543 printk("stl_sc26198getsignals(portp=%x)\n", (int) portp);
4544 #endif
4545
4546 save_flags(flags);
4547 cli();
4548 BRDENABLE(portp->brdnr, portp->pagenr);
4549 ipr = stl_sc26198getreg(portp, IPR);
4550 BRDDISABLE(portp->brdnr);
4551 restore_flags(flags);
4552
4553 sigs = 0;
4554 sigs |= (ipr & IPR_DCD) ? 0 : TIOCM_CD;
4555 sigs |= (ipr & IPR_CTS) ? 0 : TIOCM_CTS;
4556 sigs |= (ipr & IPR_DTR) ? 0: TIOCM_DTR;
4557 sigs |= (ipr & IPR_RTS) ? 0: TIOCM_RTS;
4558 sigs |= TIOCM_DSR;
4559 return(sigs);
4560 }
4561
4562 /*****************************************************************************/
4563
4564 /*
4565 * Enable/Disable the Transmitter and/or Receiver.
4566 */
4567
4568 static void stl_sc26198enablerxtx(stlport_t *portp, int rx, int tx)
4569 {
4570 unsigned char ccr;
4571 unsigned long flags;
4572
4573 #ifdef DEBUG
4574 printk("stl_sc26198enablerxtx(portp=%x,rx=%d,tx=%d)\n",
4575 (int) portp, rx, tx);
4576 #endif
4577
4578 ccr = portp->crenable;
4579 if (tx == 0)
4580 ccr &= ~CR_TXENABLE;
4581 else if (tx > 0)
4582 ccr |= CR_TXENABLE;
4583 if (rx == 0)
4584 ccr &= ~CR_RXENABLE;
4585 else if (rx > 0)
4586 ccr |= CR_RXENABLE;
4587
4588 save_flags(flags);
4589 cli();
4590 BRDENABLE(portp->brdnr, portp->pagenr);
4591 stl_sc26198setreg(portp, SCCR, ccr);
4592 BRDDISABLE(portp->brdnr);
4593 portp->crenable = ccr;
4594 restore_flags(flags);
4595 }
4596
4597 /*****************************************************************************/
4598
4599 /*
4600 * Start/stop the Transmitter and/or Receiver.
4601 */
4602
4603 static void stl_sc26198startrxtx(stlport_t *portp, int rx, int tx)
4604 {
4605 unsigned char imr;
4606 unsigned long flags;
4607
4608 #ifdef DEBUG
4609 printk("stl_sc26198startrxtx(portp=%x,rx=%d,tx=%d)\n",
4610 (int) portp, rx, tx);
4611 #endif
4612
4613 imr = portp->imr;
4614 if (tx == 0)
4615 imr &= ~IR_TXRDY;
4616 else if (tx == 1)
4617 imr |= IR_TXRDY;
4618 if (rx == 0)
4619 imr &= ~(IR_RXRDY | IR_RXBREAK | IR_RXWATCHDOG);
4620 else if (rx > 0)
4621 imr |= IR_RXRDY | IR_RXBREAK | IR_RXWATCHDOG;
4622
4623 save_flags(flags);
4624 cli();
4625 BRDENABLE(portp->brdnr, portp->pagenr);
4626 stl_sc26198setreg(portp, IMR, imr);
4627 BRDDISABLE(portp->brdnr);
4628 portp->imr = imr;
4629 if (tx > 0)
4630 set_bit(ASYI_TXBUSY, &portp->istate);
4631 restore_flags(flags);
4632 }
4633
4634 /*****************************************************************************/
4635
4636 /*
4637 * Disable all interrupts from this port.
4638 */
4639
4640 static void stl_sc26198disableintrs(stlport_t *portp)
4641 {
4642 unsigned long flags;
4643
4644 #ifdef DEBUG
4645 printk("stl_sc26198disableintrs(portp=%x)\n", (int) portp);
4646 #endif
4647
4648 save_flags(flags);
4649 cli();
4650 BRDENABLE(portp->brdnr, portp->pagenr);
4651 portp->imr = 0;
4652 stl_sc26198setreg(portp, IMR, 0);
4653 BRDDISABLE(portp->brdnr);
4654 restore_flags(flags);
4655 }
4656
4657 /*****************************************************************************/
4658
4659 static void stl_sc26198sendbreak(stlport_t *portp, int len)
4660 {
4661 unsigned long flags;
4662
4663 #ifdef DEBUG
4664 printk("stl_sc26198sendbreak(portp=%x,len=%d)\n", (int) portp, len);
4665 #endif
4666
4667 save_flags(flags);
4668 cli();
4669 BRDENABLE(portp->brdnr, portp->pagenr);
4670 if (len == 1) {
4671 stl_sc26198setreg(portp, SCCR, CR_TXSTARTBREAK);
4672 portp->stats.txbreaks++;
4673 } else {
4674 stl_sc26198setreg(portp, SCCR, CR_TXSTOPBREAK);
4675 }
4676 BRDDISABLE(portp->brdnr);
4677 restore_flags(flags);
4678 }
4679
4680 /*****************************************************************************/
4681
4682 /*
4683 * Take flow control actions...
4684 */
4685
4686 static void stl_sc26198flowctrl(stlport_t *portp, int state)
4687 {
4688 struct tty_struct *tty;
4689 unsigned long flags;
4690 unsigned char mr0;
4691
4692 #ifdef DEBUG
4693 printk("stl_sc26198flowctrl(portp=%x,state=%x)\n", (int) portp, state);
4694 #endif
4695
4696 if (portp == (stlport_t *) NULL)
4697 return;
4698 tty = portp->tty;
4699 if (tty == (struct tty_struct *) NULL)
4700 return;
4701
4702 save_flags(flags);
4703 cli();
4704 BRDENABLE(portp->brdnr, portp->pagenr);
4705
4706 if (state) {
4707 if (tty->termios->c_iflag & IXOFF) {
4708 mr0 = stl_sc26198getreg(portp, MR0);
4709 stl_sc26198setreg(portp, MR0, (mr0 & ~MR0_SWFRXTX));
4710 stl_sc26198setreg(portp, SCCR, CR_TXSENDXON);
4711 mr0 |= MR0_SWFRX;
4712 portp->stats.rxxon++;
4713 stl_sc26198wait(portp);
4714 stl_sc26198setreg(portp, MR0, mr0);
4715 }
4716 /*
4717 * Question: should we return RTS to what it was before? It may
4718 * have been set by an ioctl... Suppose not, since if you have
4719 * hardware flow control set then it is pretty silly to go and
4720 * set the RTS line by hand.
4721 */
4722 if (tty->termios->c_cflag & CRTSCTS) {
4723 stl_sc26198setreg(portp, MR1,
4724 (stl_sc26198getreg(portp, MR1) | MR1_AUTORTS));
4725 stl_sc26198setreg(portp, IOPIOR,
4726 (stl_sc26198getreg(portp, IOPIOR) | IOPR_RTS));
4727 portp->stats.rxrtson++;
4728 }
4729 } else {
4730 if (tty->termios->c_iflag & IXOFF) {
4731 mr0 = stl_sc26198getreg(portp, MR0);
4732 stl_sc26198setreg(portp, MR0, (mr0 & ~MR0_SWFRXTX));
4733 stl_sc26198setreg(portp, SCCR, CR_TXSENDXOFF);
4734 mr0 &= ~MR0_SWFRX;
4735 portp->stats.rxxoff++;
4736 stl_sc26198wait(portp);
4737 stl_sc26198setreg(portp, MR0, mr0);
4738 }
4739 if (tty->termios->c_cflag & CRTSCTS) {
4740 stl_sc26198setreg(portp, MR1,
4741 (stl_sc26198getreg(portp, MR1) & ~MR1_AUTORTS));
4742 stl_sc26198setreg(portp, IOPIOR,
4743 (stl_sc26198getreg(portp, IOPIOR) & ~IOPR_RTS));
4744 portp->stats.rxrtsoff++;
4745 }
4746 }
4747
4748 BRDDISABLE(portp->brdnr);
4749 restore_flags(flags);
4750 }
4751
4752 /*****************************************************************************/
4753
4754 /*
4755 * Send a flow control character.
4756 */
4757
4758 static void stl_sc26198sendflow(stlport_t *portp, int state)
4759 {
4760 struct tty_struct *tty;
4761 unsigned long flags;
4762 unsigned char mr0;
4763
4764 #ifdef DEBUG
4765 printk("stl_sc26198sendflow(portp=%x,state=%x)\n", (int) portp, state);
4766 #endif
4767
4768 if (portp == (stlport_t *) NULL)
4769 return;
4770 tty = portp->tty;
4771 if (tty == (struct tty_struct *) NULL)
4772 return;
4773
4774 save_flags(flags);
4775 cli();
4776 BRDENABLE(portp->brdnr, portp->pagenr);
4777 if (state) {
4778 mr0 = stl_sc26198getreg(portp, MR0);
4779 stl_sc26198setreg(portp, MR0, (mr0 & ~MR0_SWFRXTX));
4780 stl_sc26198setreg(portp, SCCR, CR_TXSENDXON);
4781 mr0 |= MR0_SWFRX;
4782 portp->stats.rxxon++;
4783 stl_sc26198wait(portp);
4784 stl_sc26198setreg(portp, MR0, mr0);
4785 } else {
4786 mr0 = stl_sc26198getreg(portp, MR0);
4787 stl_sc26198setreg(portp, MR0, (mr0 & ~MR0_SWFRXTX));
4788 stl_sc26198setreg(portp, SCCR, CR_TXSENDXOFF);
4789 mr0 &= ~MR0_SWFRX;
4790 portp->stats.rxxoff++;
4791 stl_sc26198wait(portp);
4792 stl_sc26198setreg(portp, MR0, mr0);
4793 }
4794 BRDDISABLE(portp->brdnr);
4795 restore_flags(flags);
4796 }
4797
4798 /*****************************************************************************/
4799
4800 static void stl_sc26198flush(stlport_t *portp)
4801 {
4802 unsigned long flags;
4803
4804 #ifdef DEBUG
4805 printk("stl_sc26198flush(portp=%x)\n", (int) portp);
4806 #endif
4807
4808 if (portp == (stlport_t *) NULL)
4809 return;
4810
4811 save_flags(flags);
4812 cli();
4813 BRDENABLE(portp->brdnr, portp->pagenr);
4814 stl_sc26198setreg(portp, SCCR, CR_TXRESET);
4815 stl_sc26198setreg(portp, SCCR, portp->crenable);
4816 BRDDISABLE(portp->brdnr);
4817 portp->tx.tail = portp->tx.head;
4818 restore_flags(flags);
4819 }
4820
4821 /*****************************************************************************/
4822
4823 /*
4824 * Return the current state of data flow on this port. This is only
4825 * really interresting when determining if data has fully completed
4826 * transmission or not... The sc26198 interrupt scheme cannot
4827 * determine when all data has actually drained, so we need to
4828 * check the port statusy register to be sure.
4829 */
4830
4831 static int stl_sc26198datastate(stlport_t *portp)
4832 {
4833 unsigned long flags;
4834 unsigned char sr;
4835
4836 #ifdef DEBUG
4837 printk("stl_sc26198datastate(portp=%x)\n", (int) portp);
4838 #endif
4839
4840 if (portp == (stlport_t *) NULL)
4841 return(0);
4842 if (test_bit(ASYI_TXBUSY, &portp->istate))
4843 return(1);
4844
4845 save_flags(flags);
4846 cli();
4847 BRDENABLE(portp->brdnr, portp->pagenr);
4848 sr = stl_sc26198getreg(portp, SR);
4849 BRDDISABLE(portp->brdnr);
4850 restore_flags(flags);
4851
4852 return((sr & SR_TXEMPTY) ? 0 : 1);
4853 }
4854
4855 /*****************************************************************************/
4856
4857 /*
4858 * Delay for a small amount of time, to give the sc26198 a chance
4859 * to process a command...
4860 */
4861
4862 static void stl_sc26198wait(stlport_t *portp)
4863 {
4864 int i;
4865
4866 #ifdef DEBUG
4867 printk("stl_sc26198wait(portp=%x)\n", (int) portp);
4868 #endif
4869
4870 if (portp == (stlport_t *) NULL)
4871 return;
4872
4873 for (i = 0; (i < 20); i++)
4874 stl_sc26198getglobreg(portp, TSTR);
4875 }
4876
4877 /*****************************************************************************/
4878
4879 /*
4880 * If we are TX flow controlled and in IXANY mode then we may
4881 * need to unflow control here. We gotta do this because of the
4882 * automatic flow control modes of the sc26198.
4883 */
4884
4885 static inline void stl_sc26198txunflow(stlport_t *portp, struct tty_struct *tty)
4886 {
4887 unsigned char mr0;
4888
4889 mr0 = stl_sc26198getreg(portp, MR0);
4890 stl_sc26198setreg(portp, MR0, (mr0 & ~MR0_SWFRXTX));
4891 stl_sc26198setreg(portp, SCCR, CR_HOSTXON);
4892 stl_sc26198wait(portp);
4893 stl_sc26198setreg(portp, MR0, mr0);
4894 clear_bit(ASYI_TXFLOWED, &portp->istate);
4895 }
4896
4897 /*****************************************************************************/
4898
4899 /*
4900 * Interrupt service routine for sc26198 panels.
4901 */
4902
4903 static void stl_sc26198intr(stlpanel_t *panelp, unsigned int iobase)
4904 {
4905 stlport_t *portp;
4906 unsigned int iack;
4907
4908 /*
4909 * Work around bug in sc26198 chip... Cannot have A6 address
4910 * line of UART high, else iack will be returned as 0.
4911 */
4912 outb(0, (iobase + 1));
4913
4914 iack = inb(iobase + XP_IACK);
4915 portp = panelp->ports[(iack & IVR_CHANMASK) + ((iobase & 0x4) << 1)];
4916
4917 if (iack & IVR_RXDATA)
4918 stl_sc26198rxisr(portp, iack);
4919 else if (iack & IVR_TXDATA)
4920 stl_sc26198txisr(portp);
4921 else
4922 stl_sc26198otherisr(portp, iack);
4923 }
4924
4925 /*****************************************************************************/
4926
4927 /*
4928 * Transmit interrupt handler. This has gotta be fast! Handling TX
4929 * chars is pretty simple, stuff as many as possible from the TX buffer
4930 * into the sc26198 FIFO.
4931 * In practice it is possible that interrupts are enabled but that the
4932 * port has been hung up. Need to handle not having any TX buffer here,
4933 * this is done by using the side effect that head and tail will also
4934 * be NULL if the buffer has been freed.
4935 */
4936
4937 static void stl_sc26198txisr(stlport_t *portp)
4938 {
4939 unsigned int ioaddr;
4940 unsigned char mr0;
4941 int len, stlen;
4942 char *head, *tail;
4943
4944 #ifdef DEBUG
4945 printk("stl_sc26198txisr(portp=%x)\n", (int) portp);
4946 #endif
4947
4948 ioaddr = portp->ioaddr;
4949 head = portp->tx.head;
4950 tail = portp->tx.tail;
4951 len = (head >= tail) ? (head - tail) : (STL_TXBUFSIZE - (tail - head));
4952 if ((len == 0) || ((len < STL_TXBUFLOW) &&
4953 (test_bit(ASYI_TXLOW, &portp->istate) == 0))) {
4954 set_bit(ASYI_TXLOW, &portp->istate);
4955 schedule_work(&portp->tqueue);
4956 }
4957
4958 if (len == 0) {
4959 outb((MR0 | portp->uartaddr), (ioaddr + XP_ADDR));
4960 mr0 = inb(ioaddr + XP_DATA);
4961 if ((mr0 & MR0_TXMASK) == MR0_TXEMPTY) {
4962 portp->imr &= ~IR_TXRDY;
4963 outb((IMR | portp->uartaddr), (ioaddr + XP_ADDR));
4964 outb(portp->imr, (ioaddr + XP_DATA));
4965 clear_bit(ASYI_TXBUSY, &portp->istate);
4966 } else {
4967 mr0 |= ((mr0 & ~MR0_TXMASK) | MR0_TXEMPTY);
4968 outb(mr0, (ioaddr + XP_DATA));
4969 }
4970 } else {
4971 len = MIN(len, SC26198_TXFIFOSIZE);
4972 portp->stats.txtotal += len;
4973 stlen = MIN(len, ((portp->tx.buf + STL_TXBUFSIZE) - tail));
4974 outb(GTXFIFO, (ioaddr + XP_ADDR));
4975 outsb((ioaddr + XP_DATA), tail, stlen);
4976 len -= stlen;
4977 tail += stlen;
4978 if (tail >= (portp->tx.buf + STL_TXBUFSIZE))
4979 tail = portp->tx.buf;
4980 if (len > 0) {
4981 outsb((ioaddr + XP_DATA), tail, len);
4982 tail += len;
4983 }
4984 portp->tx.tail = tail;
4985 }
4986 }
4987
4988 /*****************************************************************************/
4989
4990 /*
4991 * Receive character interrupt handler. Determine if we have good chars
4992 * or bad chars and then process appropriately. Good chars are easy
4993 * just shove the lot into the RX buffer and set all status byte to 0.
4994 * If a bad RX char then process as required. This routine needs to be
4995 * fast! In practice it is possible that we get an interrupt on a port
4996 * that is closed. This can happen on hangups - since they completely
4997 * shutdown a port not in user context. Need to handle this case.
4998 */
4999
5000 static void stl_sc26198rxisr(stlport_t *portp, unsigned int iack)
5001 {
5002 struct tty_struct *tty;
5003 unsigned int len, buflen, ioaddr;
5004
5005 #ifdef DEBUG
5006 printk("stl_sc26198rxisr(portp=%x,iack=%x)\n", (int) portp, iack);
5007 #endif
5008
5009 tty = portp->tty;
5010 ioaddr = portp->ioaddr;
5011 outb(GIBCR, (ioaddr + XP_ADDR));
5012 len = inb(ioaddr + XP_DATA) + 1;
5013
5014 if ((iack & IVR_TYPEMASK) == IVR_RXDATA) {
5015 if ((tty == (struct tty_struct *) NULL) ||
5016 (tty->flip.char_buf_ptr == (char *) NULL) ||
5017 ((buflen = TTY_FLIPBUF_SIZE - tty->flip.count) == 0)) {
5018 len = MIN(len, sizeof(stl_unwanted));
5019 outb(GRXFIFO, (ioaddr + XP_ADDR));
5020 insb((ioaddr + XP_DATA), &stl_unwanted[0], len);
5021 portp->stats.rxlost += len;
5022 portp->stats.rxtotal += len;
5023 } else {
5024 len = MIN(len, buflen);
5025 if (len > 0) {
5026 outb(GRXFIFO, (ioaddr + XP_ADDR));
5027 insb((ioaddr + XP_DATA), tty->flip.char_buf_ptr, len);
5028 memset(tty->flip.flag_buf_ptr, 0, len);
5029 tty->flip.flag_buf_ptr += len;
5030 tty->flip.char_buf_ptr += len;
5031 tty->flip.count += len;
5032 tty_schedule_flip(tty);
5033 portp->stats.rxtotal += len;
5034 }
5035 }
5036 } else {
5037 stl_sc26198rxbadchars(portp);
5038 }
5039
5040 /*
5041 * If we are TX flow controlled and in IXANY mode then we may need
5042 * to unflow control here. We gotta do this because of the automatic
5043 * flow control modes of the sc26198.
5044 */
5045 if (test_bit(ASYI_TXFLOWED, &portp->istate)) {
5046 if ((tty != (struct tty_struct *) NULL) &&
5047 (tty->termios != (struct termios *) NULL) &&
5048 (tty->termios->c_iflag & IXANY)) {
5049 stl_sc26198txunflow(portp, tty);
5050 }
5051 }
5052 }
5053
5054 /*****************************************************************************/
5055
5056 /*
5057 * Process an RX bad character.
5058 */
5059
5060 static inline void stl_sc26198rxbadch(stlport_t *portp, unsigned char status, char ch)
5061 {
5062 struct tty_struct *tty;
5063 unsigned int ioaddr;
5064
5065 tty = portp->tty;
5066 ioaddr = portp->ioaddr;
5067
5068 if (status & SR_RXPARITY)
5069 portp->stats.rxparity++;
5070 if (status & SR_RXFRAMING)
5071 portp->stats.rxframing++;
5072 if (status & SR_RXOVERRUN)
5073 portp->stats.rxoverrun++;
5074 if (status & SR_RXBREAK)
5075 portp->stats.rxbreaks++;
5076
5077 if ((tty != (struct tty_struct *) NULL) &&
5078 ((portp->rxignoremsk & status) == 0)) {
5079 if (portp->rxmarkmsk & status) {
5080 if (status & SR_RXBREAK) {
5081 status = TTY_BREAK;
5082 if (portp->flags & ASYNC_SAK) {
5083 do_SAK(tty);
5084 BRDENABLE(portp->brdnr, portp->pagenr);
5085 }
5086 } else if (status & SR_RXPARITY) {
5087 status = TTY_PARITY;
5088 } else if (status & SR_RXFRAMING) {
5089 status = TTY_FRAME;
5090 } else if(status & SR_RXOVERRUN) {
5091 status = TTY_OVERRUN;
5092 } else {
5093 status = 0;
5094 }
5095 } else {
5096 status = 0;
5097 }
5098
5099 if (tty->flip.char_buf_ptr != (char *) NULL) {
5100 if (tty->flip.count < TTY_FLIPBUF_SIZE) {
5101 *tty->flip.flag_buf_ptr++ = status;
5102 *tty->flip.char_buf_ptr++ = ch;
5103 tty->flip.count++;
5104 }
5105 tty_schedule_flip(tty);
5106 }
5107
5108 if (status == 0)
5109 portp->stats.rxtotal++;
5110 }
5111 }
5112
5113 /*****************************************************************************/
5114
5115 /*
5116 * Process all characters in the RX FIFO of the UART. Check all char
5117 * status bytes as well, and process as required. We need to check
5118 * all bytes in the FIFO, in case some more enter the FIFO while we
5119 * are here. To get the exact character error type we need to switch
5120 * into CHAR error mode (that is why we need to make sure we empty
5121 * the FIFO).
5122 */
5123
5124 static void stl_sc26198rxbadchars(stlport_t *portp)
5125 {
5126 unsigned char status, mr1;
5127 char ch;
5128
5129 /*
5130 * To get the precise error type for each character we must switch
5131 * back into CHAR error mode.
5132 */
5133 mr1 = stl_sc26198getreg(portp, MR1);
5134 stl_sc26198setreg(portp, MR1, (mr1 & ~MR1_ERRBLOCK));
5135
5136 while ((status = stl_sc26198getreg(portp, SR)) & SR_RXRDY) {
5137 stl_sc26198setreg(portp, SCCR, CR_CLEARRXERR);
5138 ch = stl_sc26198getreg(portp, RXFIFO);
5139 stl_sc26198rxbadch(portp, status, ch);
5140 }
5141
5142 /*
5143 * To get correct interrupt class we must switch back into BLOCK
5144 * error mode.
5145 */
5146 stl_sc26198setreg(portp, MR1, mr1);
5147 }
5148
5149 /*****************************************************************************/
5150
5151 /*
5152 * Other interrupt handler. This includes modem signals, flow
5153 * control actions, etc. Most stuff is left to off-level interrupt
5154 * processing time.
5155 */
5156
5157 static void stl_sc26198otherisr(stlport_t *portp, unsigned int iack)
5158 {
5159 unsigned char cir, ipr, xisr;
5160
5161 #ifdef DEBUG
5162 printk("stl_sc26198otherisr(portp=%x,iack=%x)\n", (int) portp, iack);
5163 #endif
5164
5165 cir = stl_sc26198getglobreg(portp, CIR);
5166
5167 switch (cir & CIR_SUBTYPEMASK) {
5168 case CIR_SUBCOS:
5169 ipr = stl_sc26198getreg(portp, IPR);
5170 if (ipr & IPR_DCDCHANGE) {
5171 set_bit(ASYI_DCDCHANGE, &portp->istate);
5172 schedule_work(&portp->tqueue);
5173 portp->stats.modem++;
5174 }
5175 break;
5176 case CIR_SUBXONXOFF:
5177 xisr = stl_sc26198getreg(portp, XISR);
5178 if (xisr & XISR_RXXONGOT) {
5179 set_bit(ASYI_TXFLOWED, &portp->istate);
5180 portp->stats.txxoff++;
5181 }
5182 if (xisr & XISR_RXXOFFGOT) {
5183 clear_bit(ASYI_TXFLOWED, &portp->istate);
5184 portp->stats.txxon++;
5185 }
5186 break;
5187 case CIR_SUBBREAK:
5188 stl_sc26198setreg(portp, SCCR, CR_BREAKRESET);
5189 stl_sc26198rxbadchars(portp);
5190 break;
5191 default:
5192 break;
5193 }
5194 }
5195
5196 /*****************************************************************************/
This page took 0.139732 seconds and 5 git commands to generate.