Merge branch 'fix/pcm-hwptr' into for-linus
[linux/fpc-iii.git] / drivers / char / stallion.c
blob53e504f41b2043fa89b4b9a83637fe31251d38ab
1 /*****************************************************************************/
3 /*
4 * stallion.c -- stallion multiport serial driver.
6 * Copyright (C) 1996-1999 Stallion Technologies
7 * Copyright (C) 1994-1996 Greg Ungerer.
9 * This code is loosely based on the Linux serial driver, written by
10 * Linus Torvalds, Theodore T'so and others.
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 /*****************************************************************************/
29 #include <linux/module.h>
30 #include <linux/slab.h>
31 #include <linux/interrupt.h>
32 #include <linux/tty.h>
33 #include <linux/tty_flip.h>
34 #include <linux/serial.h>
35 #include <linux/seq_file.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/device.h>
44 #include <linux/delay.h>
45 #include <linux/ctype.h>
47 #include <asm/io.h>
48 #include <asm/uaccess.h>
50 #include <linux/pci.h>
52 /*****************************************************************************/
55 * Define different board types. Use the standard Stallion "assigned"
56 * board numbers. Boards supported in this driver are abbreviated as
57 * EIO = EasyIO and ECH = EasyConnection 8/32.
59 #define BRD_EASYIO 20
60 #define BRD_ECH 21
61 #define BRD_ECHMC 22
62 #define BRD_ECHPCI 26
63 #define BRD_ECH64PCI 27
64 #define BRD_EASYIOPCI 28
66 struct stlconf {
67 unsigned int brdtype;
68 int ioaddr1;
69 int ioaddr2;
70 unsigned long memaddr;
71 int irq;
72 int irqtype;
75 static unsigned int stl_nrbrds;
77 /*****************************************************************************/
80 * Define some important driver characteristics. Device major numbers
81 * allocated as per Linux Device Registry.
83 #ifndef STL_SIOMEMMAJOR
84 #define STL_SIOMEMMAJOR 28
85 #endif
86 #ifndef STL_SERIALMAJOR
87 #define STL_SERIALMAJOR 24
88 #endif
89 #ifndef STL_CALLOUTMAJOR
90 #define STL_CALLOUTMAJOR 25
91 #endif
94 * Set the TX buffer size. Bigger is better, but we don't want
95 * to chew too much memory with buffers!
97 #define STL_TXBUFLOW 512
98 #define STL_TXBUFSIZE 4096
100 /*****************************************************************************/
103 * Define our local driver identity first. Set up stuff to deal with
104 * all the local structures required by a serial tty driver.
106 static char *stl_drvtitle = "Stallion Multiport Serial Driver";
107 static char *stl_drvname = "stallion";
108 static char *stl_drvversion = "5.6.0";
110 static struct tty_driver *stl_serial;
113 * Define a local default termios struct. All ports will be created
114 * with this termios initially. Basically all it defines is a raw port
115 * at 9600, 8 data bits, 1 stop bit.
117 static struct ktermios stl_deftermios = {
118 .c_cflag = (B9600 | CS8 | CREAD | HUPCL | CLOCAL),
119 .c_cc = INIT_C_CC,
120 .c_ispeed = 9600,
121 .c_ospeed = 9600,
125 * Define global place to put buffer overflow characters.
127 static char stl_unwanted[SC26198_RXFIFOSIZE];
129 /*****************************************************************************/
131 static DEFINE_MUTEX(stl_brdslock);
132 static struct stlbrd *stl_brds[STL_MAXBRDS];
134 static const struct tty_port_operations stl_port_ops;
137 * Per board state flags. Used with the state field of the board struct.
138 * Not really much here!
140 #define BRD_FOUND 0x1
141 #define STL_PROBED 0x2
145 * Define the port structure istate flags. These set of flags are
146 * modified at interrupt time - so setting and reseting them needs
147 * to be atomic. Use the bit clear/setting routines for this.
149 #define ASYI_TXBUSY 1
150 #define ASYI_TXLOW 2
151 #define ASYI_TXFLOWED 3
154 * Define an array of board names as printable strings. Handy for
155 * referencing boards when printing trace and stuff.
157 static char *stl_brdnames[] = {
158 NULL,
159 NULL,
160 NULL,
161 NULL,
162 NULL,
163 NULL,
164 NULL,
165 NULL,
166 NULL,
167 NULL,
168 NULL,
169 NULL,
170 NULL,
171 NULL,
172 NULL,
173 NULL,
174 NULL,
175 NULL,
176 NULL,
177 NULL,
178 "EasyIO",
179 "EC8/32-AT",
180 "EC8/32-MC",
181 NULL,
182 NULL,
183 NULL,
184 "EC8/32-PCI",
185 "EC8/64-PCI",
186 "EasyIO-PCI",
189 /*****************************************************************************/
192 * Define some string labels for arguments passed from the module
193 * load line. These allow for easy board definitions, and easy
194 * modification of the io, memory and irq resoucres.
196 static unsigned int stl_nargs;
197 static char *board0[4];
198 static char *board1[4];
199 static char *board2[4];
200 static char *board3[4];
202 static char **stl_brdsp[] = {
203 (char **) &board0,
204 (char **) &board1,
205 (char **) &board2,
206 (char **) &board3
210 * Define a set of common board names, and types. This is used to
211 * parse any module arguments.
214 static struct {
215 char *name;
216 int type;
217 } stl_brdstr[] = {
218 { "easyio", BRD_EASYIO },
219 { "eio", BRD_EASYIO },
220 { "20", BRD_EASYIO },
221 { "ec8/32", BRD_ECH },
222 { "ec8/32-at", BRD_ECH },
223 { "ec8/32-isa", BRD_ECH },
224 { "ech", BRD_ECH },
225 { "echat", BRD_ECH },
226 { "21", BRD_ECH },
227 { "ec8/32-mc", BRD_ECHMC },
228 { "ec8/32-mca", BRD_ECHMC },
229 { "echmc", BRD_ECHMC },
230 { "echmca", BRD_ECHMC },
231 { "22", BRD_ECHMC },
232 { "ec8/32-pc", BRD_ECHPCI },
233 { "ec8/32-pci", BRD_ECHPCI },
234 { "26", BRD_ECHPCI },
235 { "ec8/64-pc", BRD_ECH64PCI },
236 { "ec8/64-pci", BRD_ECH64PCI },
237 { "ech-pci", BRD_ECH64PCI },
238 { "echpci", BRD_ECH64PCI },
239 { "echpc", BRD_ECH64PCI },
240 { "27", BRD_ECH64PCI },
241 { "easyio-pc", BRD_EASYIOPCI },
242 { "easyio-pci", BRD_EASYIOPCI },
243 { "eio-pci", BRD_EASYIOPCI },
244 { "eiopci", BRD_EASYIOPCI },
245 { "28", BRD_EASYIOPCI },
249 * Define the module agruments.
252 module_param_array(board0, charp, &stl_nargs, 0);
253 MODULE_PARM_DESC(board0, "Board 0 config -> name[,ioaddr[,ioaddr2][,irq]]");
254 module_param_array(board1, charp, &stl_nargs, 0);
255 MODULE_PARM_DESC(board1, "Board 1 config -> name[,ioaddr[,ioaddr2][,irq]]");
256 module_param_array(board2, charp, &stl_nargs, 0);
257 MODULE_PARM_DESC(board2, "Board 2 config -> name[,ioaddr[,ioaddr2][,irq]]");
258 module_param_array(board3, charp, &stl_nargs, 0);
259 MODULE_PARM_DESC(board3, "Board 3 config -> name[,ioaddr[,ioaddr2][,irq]]");
261 /*****************************************************************************/
264 * Hardware ID bits for the EasyIO and ECH boards. These defines apply
265 * to the directly accessible io ports of these boards (not the uarts -
266 * they are in cd1400.h and sc26198.h).
268 #define EIO_8PORTRS 0x04
269 #define EIO_4PORTRS 0x05
270 #define EIO_8PORTDI 0x00
271 #define EIO_8PORTM 0x06
272 #define EIO_MK3 0x03
273 #define EIO_IDBITMASK 0x07
275 #define EIO_BRDMASK 0xf0
276 #define ID_BRD4 0x10
277 #define ID_BRD8 0x20
278 #define ID_BRD16 0x30
280 #define EIO_INTRPEND 0x08
281 #define EIO_INTEDGE 0x00
282 #define EIO_INTLEVEL 0x08
283 #define EIO_0WS 0x10
285 #define ECH_ID 0xa0
286 #define ECH_IDBITMASK 0xe0
287 #define ECH_BRDENABLE 0x08
288 #define ECH_BRDDISABLE 0x00
289 #define ECH_INTENABLE 0x01
290 #define ECH_INTDISABLE 0x00
291 #define ECH_INTLEVEL 0x02
292 #define ECH_INTEDGE 0x00
293 #define ECH_INTRPEND 0x01
294 #define ECH_BRDRESET 0x01
296 #define ECHMC_INTENABLE 0x01
297 #define ECHMC_BRDRESET 0x02
299 #define ECH_PNLSTATUS 2
300 #define ECH_PNL16PORT 0x20
301 #define ECH_PNLIDMASK 0x07
302 #define ECH_PNLXPID 0x40
303 #define ECH_PNLINTRPEND 0x80
305 #define ECH_ADDR2MASK 0x1e0
308 * Define the vector mapping bits for the programmable interrupt board
309 * hardware. These bits encode the interrupt for the board to use - it
310 * is software selectable (except the EIO-8M).
312 static unsigned char stl_vecmap[] = {
313 0xff, 0xff, 0xff, 0x04, 0x06, 0x05, 0xff, 0x07,
314 0xff, 0xff, 0x00, 0x02, 0x01, 0xff, 0xff, 0x03
318 * Lock ordering is that you may not take stallion_lock holding
319 * brd_lock.
322 static spinlock_t brd_lock; /* Guard the board mapping */
323 static spinlock_t stallion_lock; /* Guard the tty driver */
326 * Set up enable and disable macros for the ECH boards. They require
327 * the secondary io address space to be activated and deactivated.
328 * This way all ECH boards can share their secondary io region.
329 * If this is an ECH-PCI board then also need to set the page pointer
330 * to point to the correct page.
332 #define BRDENABLE(brdnr,pagenr) \
333 if (stl_brds[(brdnr)]->brdtype == BRD_ECH) \
334 outb((stl_brds[(brdnr)]->ioctrlval | ECH_BRDENABLE), \
335 stl_brds[(brdnr)]->ioctrl); \
336 else if (stl_brds[(brdnr)]->brdtype == BRD_ECHPCI) \
337 outb((pagenr), stl_brds[(brdnr)]->ioctrl);
339 #define BRDDISABLE(brdnr) \
340 if (stl_brds[(brdnr)]->brdtype == BRD_ECH) \
341 outb((stl_brds[(brdnr)]->ioctrlval | ECH_BRDDISABLE), \
342 stl_brds[(brdnr)]->ioctrl);
344 #define STL_CD1400MAXBAUD 230400
345 #define STL_SC26198MAXBAUD 460800
347 #define STL_BAUDBASE 115200
348 #define STL_CLOSEDELAY (5 * HZ / 10)
350 /*****************************************************************************/
353 * Define the Stallion PCI vendor and device IDs.
355 #ifndef PCI_VENDOR_ID_STALLION
356 #define PCI_VENDOR_ID_STALLION 0x124d
357 #endif
358 #ifndef PCI_DEVICE_ID_ECHPCI832
359 #define PCI_DEVICE_ID_ECHPCI832 0x0000
360 #endif
361 #ifndef PCI_DEVICE_ID_ECHPCI864
362 #define PCI_DEVICE_ID_ECHPCI864 0x0002
363 #endif
364 #ifndef PCI_DEVICE_ID_EIOPCI
365 #define PCI_DEVICE_ID_EIOPCI 0x0003
366 #endif
369 * Define structure to hold all Stallion PCI boards.
372 static struct pci_device_id stl_pcibrds[] = {
373 { PCI_DEVICE(PCI_VENDOR_ID_STALLION, PCI_DEVICE_ID_ECHPCI864),
374 .driver_data = BRD_ECH64PCI },
375 { PCI_DEVICE(PCI_VENDOR_ID_STALLION, PCI_DEVICE_ID_EIOPCI),
376 .driver_data = BRD_EASYIOPCI },
377 { PCI_DEVICE(PCI_VENDOR_ID_STALLION, PCI_DEVICE_ID_ECHPCI832),
378 .driver_data = BRD_ECHPCI },
379 { PCI_DEVICE(PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_87410),
380 .driver_data = BRD_ECHPCI },
383 MODULE_DEVICE_TABLE(pci, stl_pcibrds);
385 /*****************************************************************************/
388 * Define macros to extract a brd/port number from a minor number.
390 #define MINOR2BRD(min) (((min) & 0xc0) >> 6)
391 #define MINOR2PORT(min) ((min) & 0x3f)
394 * Define a baud rate table that converts termios baud rate selector
395 * into the actual baud rate value. All baud rate calculations are
396 * based on the actual baud rate required.
398 static unsigned int stl_baudrates[] = {
399 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
400 9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600
403 /*****************************************************************************/
406 * Declare all those functions in this driver!
409 static int stl_memioctl(struct inode *ip, struct file *fp, unsigned int cmd, unsigned long arg);
410 static int stl_brdinit(struct stlbrd *brdp);
411 static int stl_getportstats(struct tty_struct *tty, struct stlport *portp, comstats_t __user *cp);
412 static int stl_clrportstats(struct stlport *portp, comstats_t __user *cp);
415 * CD1400 uart specific handling functions.
417 static void stl_cd1400setreg(struct stlport *portp, int regnr, int value);
418 static int stl_cd1400getreg(struct stlport *portp, int regnr);
419 static int stl_cd1400updatereg(struct stlport *portp, int regnr, int value);
420 static int stl_cd1400panelinit(struct stlbrd *brdp, struct stlpanel *panelp);
421 static void stl_cd1400portinit(struct stlbrd *brdp, struct stlpanel *panelp, struct stlport *portp);
422 static void stl_cd1400setport(struct stlport *portp, struct ktermios *tiosp);
423 static int stl_cd1400getsignals(struct stlport *portp);
424 static void stl_cd1400setsignals(struct stlport *portp, int dtr, int rts);
425 static void stl_cd1400ccrwait(struct stlport *portp);
426 static void stl_cd1400enablerxtx(struct stlport *portp, int rx, int tx);
427 static void stl_cd1400startrxtx(struct stlport *portp, int rx, int tx);
428 static void stl_cd1400disableintrs(struct stlport *portp);
429 static void stl_cd1400sendbreak(struct stlport *portp, int len);
430 static void stl_cd1400flowctrl(struct stlport *portp, int state);
431 static void stl_cd1400sendflow(struct stlport *portp, int state);
432 static void stl_cd1400flush(struct stlport *portp);
433 static int stl_cd1400datastate(struct stlport *portp);
434 static void stl_cd1400eiointr(struct stlpanel *panelp, unsigned int iobase);
435 static void stl_cd1400echintr(struct stlpanel *panelp, unsigned int iobase);
436 static void stl_cd1400txisr(struct stlpanel *panelp, int ioaddr);
437 static void stl_cd1400rxisr(struct stlpanel *panelp, int ioaddr);
438 static void stl_cd1400mdmisr(struct stlpanel *panelp, int ioaddr);
440 static inline int stl_cd1400breakisr(struct stlport *portp, int ioaddr);
443 * SC26198 uart specific handling functions.
445 static void stl_sc26198setreg(struct stlport *portp, int regnr, int value);
446 static int stl_sc26198getreg(struct stlport *portp, int regnr);
447 static int stl_sc26198updatereg(struct stlport *portp, int regnr, int value);
448 static int stl_sc26198getglobreg(struct stlport *portp, int regnr);
449 static int stl_sc26198panelinit(struct stlbrd *brdp, struct stlpanel *panelp);
450 static void stl_sc26198portinit(struct stlbrd *brdp, struct stlpanel *panelp, struct stlport *portp);
451 static void stl_sc26198setport(struct stlport *portp, struct ktermios *tiosp);
452 static int stl_sc26198getsignals(struct stlport *portp);
453 static void stl_sc26198setsignals(struct stlport *portp, int dtr, int rts);
454 static void stl_sc26198enablerxtx(struct stlport *portp, int rx, int tx);
455 static void stl_sc26198startrxtx(struct stlport *portp, int rx, int tx);
456 static void stl_sc26198disableintrs(struct stlport *portp);
457 static void stl_sc26198sendbreak(struct stlport *portp, int len);
458 static void stl_sc26198flowctrl(struct stlport *portp, int state);
459 static void stl_sc26198sendflow(struct stlport *portp, int state);
460 static void stl_sc26198flush(struct stlport *portp);
461 static int stl_sc26198datastate(struct stlport *portp);
462 static void stl_sc26198wait(struct stlport *portp);
463 static void stl_sc26198txunflow(struct stlport *portp, struct tty_struct *tty);
464 static void stl_sc26198intr(struct stlpanel *panelp, unsigned int iobase);
465 static void stl_sc26198txisr(struct stlport *port);
466 static void stl_sc26198rxisr(struct stlport *port, unsigned int iack);
467 static void stl_sc26198rxbadch(struct stlport *portp, unsigned char status, char ch);
468 static void stl_sc26198rxbadchars(struct stlport *portp);
469 static void stl_sc26198otherisr(struct stlport *port, unsigned int iack);
471 /*****************************************************************************/
474 * Generic UART support structure.
476 typedef struct uart {
477 int (*panelinit)(struct stlbrd *brdp, struct stlpanel *panelp);
478 void (*portinit)(struct stlbrd *brdp, struct stlpanel *panelp, struct stlport *portp);
479 void (*setport)(struct stlport *portp, struct ktermios *tiosp);
480 int (*getsignals)(struct stlport *portp);
481 void (*setsignals)(struct stlport *portp, int dtr, int rts);
482 void (*enablerxtx)(struct stlport *portp, int rx, int tx);
483 void (*startrxtx)(struct stlport *portp, int rx, int tx);
484 void (*disableintrs)(struct stlport *portp);
485 void (*sendbreak)(struct stlport *portp, int len);
486 void (*flowctrl)(struct stlport *portp, int state);
487 void (*sendflow)(struct stlport *portp, int state);
488 void (*flush)(struct stlport *portp);
489 int (*datastate)(struct stlport *portp);
490 void (*intr)(struct stlpanel *panelp, unsigned int iobase);
491 } uart_t;
494 * Define some macros to make calling these functions nice and clean.
496 #define stl_panelinit (* ((uart_t *) panelp->uartp)->panelinit)
497 #define stl_portinit (* ((uart_t *) portp->uartp)->portinit)
498 #define stl_setport (* ((uart_t *) portp->uartp)->setport)
499 #define stl_getsignals (* ((uart_t *) portp->uartp)->getsignals)
500 #define stl_setsignals (* ((uart_t *) portp->uartp)->setsignals)
501 #define stl_enablerxtx (* ((uart_t *) portp->uartp)->enablerxtx)
502 #define stl_startrxtx (* ((uart_t *) portp->uartp)->startrxtx)
503 #define stl_disableintrs (* ((uart_t *) portp->uartp)->disableintrs)
504 #define stl_sendbreak (* ((uart_t *) portp->uartp)->sendbreak)
505 #define stl_flowctrl (* ((uart_t *) portp->uartp)->flowctrl)
506 #define stl_sendflow (* ((uart_t *) portp->uartp)->sendflow)
507 #define stl_flush (* ((uart_t *) portp->uartp)->flush)
508 #define stl_datastate (* ((uart_t *) portp->uartp)->datastate)
510 /*****************************************************************************/
513 * CD1400 UART specific data initialization.
515 static uart_t stl_cd1400uart = {
516 stl_cd1400panelinit,
517 stl_cd1400portinit,
518 stl_cd1400setport,
519 stl_cd1400getsignals,
520 stl_cd1400setsignals,
521 stl_cd1400enablerxtx,
522 stl_cd1400startrxtx,
523 stl_cd1400disableintrs,
524 stl_cd1400sendbreak,
525 stl_cd1400flowctrl,
526 stl_cd1400sendflow,
527 stl_cd1400flush,
528 stl_cd1400datastate,
529 stl_cd1400eiointr
533 * Define the offsets within the register bank of a cd1400 based panel.
534 * These io address offsets are common to the EasyIO board as well.
536 #define EREG_ADDR 0
537 #define EREG_DATA 4
538 #define EREG_RXACK 5
539 #define EREG_TXACK 6
540 #define EREG_MDACK 7
542 #define EREG_BANKSIZE 8
544 #define CD1400_CLK 25000000
545 #define CD1400_CLK8M 20000000
548 * Define the cd1400 baud rate clocks. These are used when calculating
549 * what clock and divisor to use for the required baud rate. Also
550 * define the maximum baud rate allowed, and the default base baud.
552 static int stl_cd1400clkdivs[] = {
553 CD1400_CLK0, CD1400_CLK1, CD1400_CLK2, CD1400_CLK3, CD1400_CLK4
556 /*****************************************************************************/
559 * SC26198 UART specific data initization.
561 static uart_t stl_sc26198uart = {
562 stl_sc26198panelinit,
563 stl_sc26198portinit,
564 stl_sc26198setport,
565 stl_sc26198getsignals,
566 stl_sc26198setsignals,
567 stl_sc26198enablerxtx,
568 stl_sc26198startrxtx,
569 stl_sc26198disableintrs,
570 stl_sc26198sendbreak,
571 stl_sc26198flowctrl,
572 stl_sc26198sendflow,
573 stl_sc26198flush,
574 stl_sc26198datastate,
575 stl_sc26198intr
579 * Define the offsets within the register bank of a sc26198 based panel.
581 #define XP_DATA 0
582 #define XP_ADDR 1
583 #define XP_MODID 2
584 #define XP_STATUS 2
585 #define XP_IACK 3
587 #define XP_BANKSIZE 4
590 * Define the sc26198 baud rate table. Offsets within the table
591 * represent the actual baud rate selector of sc26198 registers.
593 static unsigned int sc26198_baudtable[] = {
594 50, 75, 150, 200, 300, 450, 600, 900, 1200, 1800, 2400, 3600,
595 4800, 7200, 9600, 14400, 19200, 28800, 38400, 57600, 115200,
596 230400, 460800, 921600
599 #define SC26198_NRBAUDS ARRAY_SIZE(sc26198_baudtable)
601 /*****************************************************************************/
604 * Define the driver info for a user level control device. Used mainly
605 * to get at port stats - only not using the port device itself.
607 static const struct file_operations stl_fsiomem = {
608 .owner = THIS_MODULE,
609 .ioctl = stl_memioctl,
612 static struct class *stallion_class;
614 static void stl_cd_change(struct stlport *portp)
616 unsigned int oldsigs = portp->sigs;
617 struct tty_struct *tty = tty_port_tty_get(&portp->port);
619 if (!tty)
620 return;
622 portp->sigs = stl_getsignals(portp);
624 if ((portp->sigs & TIOCM_CD) && ((oldsigs & TIOCM_CD) == 0))
625 wake_up_interruptible(&portp->port.open_wait);
627 if ((oldsigs & TIOCM_CD) && ((portp->sigs & TIOCM_CD) == 0))
628 if (portp->port.flags & ASYNC_CHECK_CD)
629 tty_hangup(tty);
630 tty_kref_put(tty);
634 * Check for any arguments passed in on the module load command line.
637 /*****************************************************************************/
640 * Parse the supplied argument string, into the board conf struct.
643 static int __init stl_parsebrd(struct stlconf *confp, char **argp)
645 char *sp;
646 unsigned int i;
648 pr_debug("stl_parsebrd(confp=%p,argp=%p)\n", confp, argp);
650 if ((argp[0] == NULL) || (*argp[0] == 0))
651 return 0;
653 for (sp = argp[0], i = 0; (*sp != 0) && (i < 25); sp++, i++)
654 *sp = tolower(*sp);
656 for (i = 0; i < ARRAY_SIZE(stl_brdstr); i++)
657 if (strcmp(stl_brdstr[i].name, argp[0]) == 0)
658 break;
660 if (i == ARRAY_SIZE(stl_brdstr)) {
661 printk("STALLION: unknown board name, %s?\n", argp[0]);
662 return 0;
665 confp->brdtype = stl_brdstr[i].type;
667 i = 1;
668 if ((argp[i] != NULL) && (*argp[i] != 0))
669 confp->ioaddr1 = simple_strtoul(argp[i], NULL, 0);
670 i++;
671 if (confp->brdtype == BRD_ECH) {
672 if ((argp[i] != NULL) && (*argp[i] != 0))
673 confp->ioaddr2 = simple_strtoul(argp[i], NULL, 0);
674 i++;
676 if ((argp[i] != NULL) && (*argp[i] != 0))
677 confp->irq = simple_strtoul(argp[i], NULL, 0);
678 return 1;
681 /*****************************************************************************/
684 * Allocate a new board structure. Fill out the basic info in it.
687 static struct stlbrd *stl_allocbrd(void)
689 struct stlbrd *brdp;
691 brdp = kzalloc(sizeof(struct stlbrd), GFP_KERNEL);
692 if (!brdp) {
693 printk("STALLION: failed to allocate memory (size=%Zd)\n",
694 sizeof(struct stlbrd));
695 return NULL;
698 brdp->magic = STL_BOARDMAGIC;
699 return brdp;
702 /*****************************************************************************/
704 static int stl_open(struct tty_struct *tty, struct file *filp)
706 struct stlport *portp;
707 struct stlbrd *brdp;
708 struct tty_port *port;
709 unsigned int minordev, brdnr, panelnr;
710 int portnr;
712 pr_debug("stl_open(tty=%p,filp=%p): device=%s\n", tty, filp, tty->name);
714 minordev = tty->index;
715 brdnr = MINOR2BRD(minordev);
716 if (brdnr >= stl_nrbrds)
717 return -ENODEV;
718 brdp = stl_brds[brdnr];
719 if (brdp == NULL)
720 return -ENODEV;
722 minordev = MINOR2PORT(minordev);
723 for (portnr = -1, panelnr = 0; panelnr < STL_MAXPANELS; panelnr++) {
724 if (brdp->panels[panelnr] == NULL)
725 break;
726 if (minordev < brdp->panels[panelnr]->nrports) {
727 portnr = minordev;
728 break;
730 minordev -= brdp->panels[panelnr]->nrports;
732 if (portnr < 0)
733 return -ENODEV;
735 portp = brdp->panels[panelnr]->ports[portnr];
736 if (portp == NULL)
737 return -ENODEV;
738 port = &portp->port;
741 * On the first open of the device setup the port hardware, and
742 * initialize the per port data structure.
744 tty_port_tty_set(port, tty);
745 tty->driver_data = portp;
746 port->count++;
748 if ((port->flags & ASYNC_INITIALIZED) == 0) {
749 if (!portp->tx.buf) {
750 portp->tx.buf = kmalloc(STL_TXBUFSIZE, GFP_KERNEL);
751 if (!portp->tx.buf)
752 return -ENOMEM;
753 portp->tx.head = portp->tx.buf;
754 portp->tx.tail = portp->tx.buf;
756 stl_setport(portp, tty->termios);
757 portp->sigs = stl_getsignals(portp);
758 stl_setsignals(portp, 1, 1);
759 stl_enablerxtx(portp, 1, 1);
760 stl_startrxtx(portp, 1, 0);
761 clear_bit(TTY_IO_ERROR, &tty->flags);
762 port->flags |= ASYNC_INITIALIZED;
764 return tty_port_block_til_ready(port, tty, filp);
767 /*****************************************************************************/
769 static int stl_carrier_raised(struct tty_port *port)
771 struct stlport *portp = container_of(port, struct stlport, port);
772 return (portp->sigs & TIOCM_CD) ? 1 : 0;
775 static void stl_dtr_rts(struct tty_port *port, int on)
777 struct stlport *portp = container_of(port, struct stlport, port);
778 /* Takes brd_lock internally */
779 stl_setsignals(portp, on, on);
782 /*****************************************************************************/
784 static void stl_flushbuffer(struct tty_struct *tty)
786 struct stlport *portp;
788 pr_debug("stl_flushbuffer(tty=%p)\n", tty);
790 portp = tty->driver_data;
791 if (portp == NULL)
792 return;
794 stl_flush(portp);
795 tty_wakeup(tty);
798 /*****************************************************************************/
800 static void stl_waituntilsent(struct tty_struct *tty, int timeout)
802 struct stlport *portp;
803 unsigned long tend;
805 pr_debug("stl_waituntilsent(tty=%p,timeout=%d)\n", tty, timeout);
807 portp = tty->driver_data;
808 if (portp == NULL)
809 return;
811 if (timeout == 0)
812 timeout = HZ;
813 tend = jiffies + timeout;
815 lock_kernel();
816 while (stl_datastate(portp)) {
817 if (signal_pending(current))
818 break;
819 msleep_interruptible(20);
820 if (time_after_eq(jiffies, tend))
821 break;
823 unlock_kernel();
826 /*****************************************************************************/
828 static void stl_close(struct tty_struct *tty, struct file *filp)
830 struct stlport *portp;
831 struct tty_port *port;
832 unsigned long flags;
834 pr_debug("stl_close(tty=%p,filp=%p)\n", tty, filp);
836 portp = tty->driver_data;
837 BUG_ON(portp == NULL);
839 port = &portp->port;
841 if (tty_port_close_start(port, tty, filp) == 0)
842 return;
844 * May want to wait for any data to drain before closing. The BUSY
845 * flag keeps track of whether we are still sending or not - it is
846 * very accurate for the cd1400, not quite so for the sc26198.
847 * (The sc26198 has no "end-of-data" interrupt only empty FIFO)
849 stl_waituntilsent(tty, (HZ / 2));
851 spin_lock_irqsave(&port->lock, flags);
852 portp->port.flags &= ~ASYNC_INITIALIZED;
853 spin_unlock_irqrestore(&port->lock, flags);
855 stl_disableintrs(portp);
856 if (tty->termios->c_cflag & HUPCL)
857 stl_setsignals(portp, 0, 0);
858 stl_enablerxtx(portp, 0, 0);
859 stl_flushbuffer(tty);
860 portp->istate = 0;
861 if (portp->tx.buf != NULL) {
862 kfree(portp->tx.buf);
863 portp->tx.buf = NULL;
864 portp->tx.head = NULL;
865 portp->tx.tail = NULL;
868 tty_port_close_end(port, tty);
869 tty_port_tty_set(port, NULL);
872 /*****************************************************************************/
875 * Write routine. Take data and stuff it in to the TX ring queue.
876 * If transmit interrupts are not running then start them.
879 static int stl_write(struct tty_struct *tty, const unsigned char *buf, int count)
881 struct stlport *portp;
882 unsigned int len, stlen;
883 unsigned char *chbuf;
884 char *head, *tail;
886 pr_debug("stl_write(tty=%p,buf=%p,count=%d)\n", tty, buf, count);
888 portp = tty->driver_data;
889 if (portp == NULL)
890 return 0;
891 if (portp->tx.buf == NULL)
892 return 0;
895 * If copying direct from user space we must cater for page faults,
896 * causing us to "sleep" here for a while. To handle this copy in all
897 * the data we need now, into a local buffer. Then when we got it all
898 * copy it into the TX buffer.
900 chbuf = (unsigned char *) buf;
902 head = portp->tx.head;
903 tail = portp->tx.tail;
904 if (head >= tail) {
905 len = STL_TXBUFSIZE - (head - tail) - 1;
906 stlen = STL_TXBUFSIZE - (head - portp->tx.buf);
907 } else {
908 len = tail - head - 1;
909 stlen = len;
912 len = min(len, (unsigned int)count);
913 count = 0;
914 while (len > 0) {
915 stlen = min(len, stlen);
916 memcpy(head, chbuf, stlen);
917 len -= stlen;
918 chbuf += stlen;
919 count += stlen;
920 head += stlen;
921 if (head >= (portp->tx.buf + STL_TXBUFSIZE)) {
922 head = portp->tx.buf;
923 stlen = tail - head;
926 portp->tx.head = head;
928 clear_bit(ASYI_TXLOW, &portp->istate);
929 stl_startrxtx(portp, -1, 1);
931 return count;
934 /*****************************************************************************/
936 static int stl_putchar(struct tty_struct *tty, unsigned char ch)
938 struct stlport *portp;
939 unsigned int len;
940 char *head, *tail;
942 pr_debug("stl_putchar(tty=%p,ch=%x)\n", tty, ch);
944 portp = tty->driver_data;
945 if (portp == NULL)
946 return -EINVAL;
947 if (portp->tx.buf == NULL)
948 return -EINVAL;
950 head = portp->tx.head;
951 tail = portp->tx.tail;
953 len = (head >= tail) ? (STL_TXBUFSIZE - (head - tail)) : (tail - head);
954 len--;
956 if (len > 0) {
957 *head++ = ch;
958 if (head >= (portp->tx.buf + STL_TXBUFSIZE))
959 head = portp->tx.buf;
961 portp->tx.head = head;
962 return 0;
965 /*****************************************************************************/
968 * If there are any characters in the buffer then make sure that TX
969 * interrupts are on and get'em out. Normally used after the putchar
970 * routine has been called.
973 static void stl_flushchars(struct tty_struct *tty)
975 struct stlport *portp;
977 pr_debug("stl_flushchars(tty=%p)\n", tty);
979 portp = tty->driver_data;
980 if (portp == NULL)
981 return;
982 if (portp->tx.buf == NULL)
983 return;
985 stl_startrxtx(portp, -1, 1);
988 /*****************************************************************************/
990 static int stl_writeroom(struct tty_struct *tty)
992 struct stlport *portp;
993 char *head, *tail;
995 pr_debug("stl_writeroom(tty=%p)\n", tty);
997 portp = tty->driver_data;
998 if (portp == NULL)
999 return 0;
1000 if (portp->tx.buf == NULL)
1001 return 0;
1003 head = portp->tx.head;
1004 tail = portp->tx.tail;
1005 return (head >= tail) ? (STL_TXBUFSIZE - (head - tail) - 1) : (tail - head - 1);
1008 /*****************************************************************************/
1011 * Return number of chars in the TX buffer. Normally we would just
1012 * calculate the number of chars in the buffer and return that, but if
1013 * the buffer is empty and TX interrupts are still on then we return
1014 * that the buffer still has 1 char in it. This way whoever called us
1015 * will not think that ALL chars have drained - since the UART still
1016 * must have some chars in it (we are busy after all).
1019 static int stl_charsinbuffer(struct tty_struct *tty)
1021 struct stlport *portp;
1022 unsigned int size;
1023 char *head, *tail;
1025 pr_debug("stl_charsinbuffer(tty=%p)\n", tty);
1027 portp = tty->driver_data;
1028 if (portp == NULL)
1029 return 0;
1030 if (portp->tx.buf == NULL)
1031 return 0;
1033 head = portp->tx.head;
1034 tail = portp->tx.tail;
1035 size = (head >= tail) ? (head - tail) : (STL_TXBUFSIZE - (tail - head));
1036 if ((size == 0) && test_bit(ASYI_TXBUSY, &portp->istate))
1037 size = 1;
1038 return size;
1041 /*****************************************************************************/
1044 * Generate the serial struct info.
1047 static int stl_getserial(struct stlport *portp, struct serial_struct __user *sp)
1049 struct serial_struct sio;
1050 struct stlbrd *brdp;
1052 pr_debug("stl_getserial(portp=%p,sp=%p)\n", portp, sp);
1054 memset(&sio, 0, sizeof(struct serial_struct));
1055 sio.line = portp->portnr;
1056 sio.port = portp->ioaddr;
1057 sio.flags = portp->port.flags;
1058 sio.baud_base = portp->baud_base;
1059 sio.close_delay = portp->close_delay;
1060 sio.closing_wait = portp->closing_wait;
1061 sio.custom_divisor = portp->custom_divisor;
1062 sio.hub6 = 0;
1063 if (portp->uartp == &stl_cd1400uart) {
1064 sio.type = PORT_CIRRUS;
1065 sio.xmit_fifo_size = CD1400_TXFIFOSIZE;
1066 } else {
1067 sio.type = PORT_UNKNOWN;
1068 sio.xmit_fifo_size = SC26198_TXFIFOSIZE;
1071 brdp = stl_brds[portp->brdnr];
1072 if (brdp != NULL)
1073 sio.irq = brdp->irq;
1075 return copy_to_user(sp, &sio, sizeof(struct serial_struct)) ? -EFAULT : 0;
1078 /*****************************************************************************/
1081 * Set port according to the serial struct info.
1082 * At this point we do not do any auto-configure stuff, so we will
1083 * just quietly ignore any requests to change irq, etc.
1086 static int stl_setserial(struct tty_struct *tty, struct serial_struct __user *sp)
1088 struct stlport * portp = tty->driver_data;
1089 struct serial_struct sio;
1091 pr_debug("stl_setserial(portp=%p,sp=%p)\n", portp, sp);
1093 if (copy_from_user(&sio, sp, sizeof(struct serial_struct)))
1094 return -EFAULT;
1095 if (!capable(CAP_SYS_ADMIN)) {
1096 if ((sio.baud_base != portp->baud_base) ||
1097 (sio.close_delay != portp->close_delay) ||
1098 ((sio.flags & ~ASYNC_USR_MASK) !=
1099 (portp->port.flags & ~ASYNC_USR_MASK)))
1100 return -EPERM;
1103 portp->port.flags = (portp->port.flags & ~ASYNC_USR_MASK) |
1104 (sio.flags & ASYNC_USR_MASK);
1105 portp->baud_base = sio.baud_base;
1106 portp->close_delay = sio.close_delay;
1107 portp->closing_wait = sio.closing_wait;
1108 portp->custom_divisor = sio.custom_divisor;
1109 stl_setport(portp, tty->termios);
1110 return 0;
1113 /*****************************************************************************/
1115 static int stl_tiocmget(struct tty_struct *tty, struct file *file)
1117 struct stlport *portp;
1119 portp = tty->driver_data;
1120 if (portp == NULL)
1121 return -ENODEV;
1122 if (tty->flags & (1 << TTY_IO_ERROR))
1123 return -EIO;
1125 return stl_getsignals(portp);
1128 static int stl_tiocmset(struct tty_struct *tty, struct file *file,
1129 unsigned int set, unsigned int clear)
1131 struct stlport *portp;
1132 int rts = -1, dtr = -1;
1134 portp = tty->driver_data;
1135 if (portp == NULL)
1136 return -ENODEV;
1137 if (tty->flags & (1 << TTY_IO_ERROR))
1138 return -EIO;
1140 if (set & TIOCM_RTS)
1141 rts = 1;
1142 if (set & TIOCM_DTR)
1143 dtr = 1;
1144 if (clear & TIOCM_RTS)
1145 rts = 0;
1146 if (clear & TIOCM_DTR)
1147 dtr = 0;
1149 stl_setsignals(portp, dtr, rts);
1150 return 0;
1153 static int stl_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
1155 struct stlport *portp;
1156 int rc;
1157 void __user *argp = (void __user *)arg;
1159 pr_debug("stl_ioctl(tty=%p,file=%p,cmd=%x,arg=%lx)\n", tty, file, cmd,
1160 arg);
1162 portp = tty->driver_data;
1163 if (portp == NULL)
1164 return -ENODEV;
1166 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1167 (cmd != COM_GETPORTSTATS) && (cmd != COM_CLRPORTSTATS))
1168 if (tty->flags & (1 << TTY_IO_ERROR))
1169 return -EIO;
1171 rc = 0;
1173 lock_kernel();
1175 switch (cmd) {
1176 case TIOCGSERIAL:
1177 rc = stl_getserial(portp, argp);
1178 break;
1179 case TIOCSSERIAL:
1180 rc = stl_setserial(tty, argp);
1181 break;
1182 case COM_GETPORTSTATS:
1183 rc = stl_getportstats(tty, portp, argp);
1184 break;
1185 case COM_CLRPORTSTATS:
1186 rc = stl_clrportstats(portp, argp);
1187 break;
1188 case TIOCSERCONFIG:
1189 case TIOCSERGWILD:
1190 case TIOCSERSWILD:
1191 case TIOCSERGETLSR:
1192 case TIOCSERGSTRUCT:
1193 case TIOCSERGETMULTI:
1194 case TIOCSERSETMULTI:
1195 default:
1196 rc = -ENOIOCTLCMD;
1197 break;
1199 unlock_kernel();
1200 return rc;
1203 /*****************************************************************************/
1206 * Start the transmitter again. Just turn TX interrupts back on.
1209 static void stl_start(struct tty_struct *tty)
1211 struct stlport *portp;
1213 pr_debug("stl_start(tty=%p)\n", tty);
1215 portp = tty->driver_data;
1216 if (portp == NULL)
1217 return;
1218 stl_startrxtx(portp, -1, 1);
1221 /*****************************************************************************/
1223 static void stl_settermios(struct tty_struct *tty, struct ktermios *old)
1225 struct stlport *portp;
1226 struct ktermios *tiosp;
1228 pr_debug("stl_settermios(tty=%p,old=%p)\n", tty, old);
1230 portp = tty->driver_data;
1231 if (portp == NULL)
1232 return;
1234 tiosp = tty->termios;
1235 if ((tiosp->c_cflag == old->c_cflag) &&
1236 (tiosp->c_iflag == old->c_iflag))
1237 return;
1239 stl_setport(portp, tiosp);
1240 stl_setsignals(portp, ((tiosp->c_cflag & (CBAUD & ~CBAUDEX)) ? 1 : 0),
1241 -1);
1242 if ((old->c_cflag & CRTSCTS) && ((tiosp->c_cflag & CRTSCTS) == 0)) {
1243 tty->hw_stopped = 0;
1244 stl_start(tty);
1246 if (((old->c_cflag & CLOCAL) == 0) && (tiosp->c_cflag & CLOCAL))
1247 wake_up_interruptible(&portp->port.open_wait);
1250 /*****************************************************************************/
1253 * Attempt to flow control who ever is sending us data. Based on termios
1254 * settings use software or/and hardware flow control.
1257 static void stl_throttle(struct tty_struct *tty)
1259 struct stlport *portp;
1261 pr_debug("stl_throttle(tty=%p)\n", tty);
1263 portp = tty->driver_data;
1264 if (portp == NULL)
1265 return;
1266 stl_flowctrl(portp, 0);
1269 /*****************************************************************************/
1272 * Unflow control the device sending us data...
1275 static void stl_unthrottle(struct tty_struct *tty)
1277 struct stlport *portp;
1279 pr_debug("stl_unthrottle(tty=%p)\n", tty);
1281 portp = tty->driver_data;
1282 if (portp == NULL)
1283 return;
1284 stl_flowctrl(portp, 1);
1287 /*****************************************************************************/
1290 * Stop the transmitter. Basically to do this we will just turn TX
1291 * interrupts off.
1294 static void stl_stop(struct tty_struct *tty)
1296 struct stlport *portp;
1298 pr_debug("stl_stop(tty=%p)\n", tty);
1300 portp = tty->driver_data;
1301 if (portp == NULL)
1302 return;
1303 stl_startrxtx(portp, -1, 0);
1306 /*****************************************************************************/
1309 * Hangup this port. This is pretty much like closing the port, only
1310 * a little more brutal. No waiting for data to drain. Shutdown the
1311 * port and maybe drop signals.
1314 static void stl_hangup(struct tty_struct *tty)
1316 struct stlport *portp;
1317 struct tty_port *port;
1318 unsigned long flags;
1320 pr_debug("stl_hangup(tty=%p)\n", tty);
1322 portp = tty->driver_data;
1323 if (portp == NULL)
1324 return;
1325 port = &portp->port;
1327 spin_lock_irqsave(&port->lock, flags);
1328 port->flags &= ~ASYNC_INITIALIZED;
1329 spin_unlock_irqrestore(&port->lock, flags);
1331 stl_disableintrs(portp);
1332 if (tty->termios->c_cflag & HUPCL)
1333 stl_setsignals(portp, 0, 0);
1334 stl_enablerxtx(portp, 0, 0);
1335 stl_flushbuffer(tty);
1336 portp->istate = 0;
1337 set_bit(TTY_IO_ERROR, &tty->flags);
1338 if (portp->tx.buf != NULL) {
1339 kfree(portp->tx.buf);
1340 portp->tx.buf = NULL;
1341 portp->tx.head = NULL;
1342 portp->tx.tail = NULL;
1344 tty_port_hangup(port);
1347 /*****************************************************************************/
1349 static int stl_breakctl(struct tty_struct *tty, int state)
1351 struct stlport *portp;
1353 pr_debug("stl_breakctl(tty=%p,state=%d)\n", tty, state);
1355 portp = tty->driver_data;
1356 if (portp == NULL)
1357 return -EINVAL;
1359 stl_sendbreak(portp, ((state == -1) ? 1 : 2));
1360 return 0;
1363 /*****************************************************************************/
1365 static void stl_sendxchar(struct tty_struct *tty, char ch)
1367 struct stlport *portp;
1369 pr_debug("stl_sendxchar(tty=%p,ch=%x)\n", tty, ch);
1371 portp = tty->driver_data;
1372 if (portp == NULL)
1373 return;
1375 if (ch == STOP_CHAR(tty))
1376 stl_sendflow(portp, 0);
1377 else if (ch == START_CHAR(tty))
1378 stl_sendflow(portp, 1);
1379 else
1380 stl_putchar(tty, ch);
1383 static void stl_portinfo(struct seq_file *m, struct stlport *portp, int portnr)
1385 int sigs;
1386 char sep;
1388 seq_printf(m, "%d: uart:%s tx:%d rx:%d",
1389 portnr, (portp->hwid == 1) ? "SC26198" : "CD1400",
1390 (int) portp->stats.txtotal, (int) portp->stats.rxtotal);
1392 if (portp->stats.rxframing)
1393 seq_printf(m, " fe:%d", (int) portp->stats.rxframing);
1394 if (portp->stats.rxparity)
1395 seq_printf(m, " pe:%d", (int) portp->stats.rxparity);
1396 if (portp->stats.rxbreaks)
1397 seq_printf(m, " brk:%d", (int) portp->stats.rxbreaks);
1398 if (portp->stats.rxoverrun)
1399 seq_printf(m, " oe:%d", (int) portp->stats.rxoverrun);
1401 sigs = stl_getsignals(portp);
1402 sep = ' ';
1403 if (sigs & TIOCM_RTS) {
1404 seq_printf(m, "%c%s", sep, "RTS");
1405 sep = '|';
1407 if (sigs & TIOCM_CTS) {
1408 seq_printf(m, "%c%s", sep, "CTS");
1409 sep = '|';
1411 if (sigs & TIOCM_DTR) {
1412 seq_printf(m, "%c%s", sep, "DTR");
1413 sep = '|';
1415 if (sigs & TIOCM_CD) {
1416 seq_printf(m, "%c%s", sep, "DCD");
1417 sep = '|';
1419 if (sigs & TIOCM_DSR) {
1420 seq_printf(m, "%c%s", sep, "DSR");
1421 sep = '|';
1423 seq_putc(m, '\n');
1426 /*****************************************************************************/
1429 * Port info, read from the /proc file system.
1432 static int stl_proc_show(struct seq_file *m, void *v)
1434 struct stlbrd *brdp;
1435 struct stlpanel *panelp;
1436 struct stlport *portp;
1437 unsigned int brdnr, panelnr, portnr;
1438 int totalport;
1440 totalport = 0;
1442 seq_printf(m, "%s: version %s\n", stl_drvtitle, stl_drvversion);
1445 * We scan through for each board, panel and port. The offset is
1446 * calculated on the fly, and irrelevant ports are skipped.
1448 for (brdnr = 0; brdnr < stl_nrbrds; brdnr++) {
1449 brdp = stl_brds[brdnr];
1450 if (brdp == NULL)
1451 continue;
1452 if (brdp->state == 0)
1453 continue;
1455 totalport = brdnr * STL_MAXPORTS;
1456 for (panelnr = 0; panelnr < brdp->nrpanels; panelnr++) {
1457 panelp = brdp->panels[panelnr];
1458 if (panelp == NULL)
1459 continue;
1461 for (portnr = 0; portnr < panelp->nrports; portnr++,
1462 totalport++) {
1463 portp = panelp->ports[portnr];
1464 if (portp == NULL)
1465 continue;
1466 stl_portinfo(m, portp, totalport);
1470 return 0;
1473 static int stl_proc_open(struct inode *inode, struct file *file)
1475 return single_open(file, stl_proc_show, NULL);
1478 static const struct file_operations stl_proc_fops = {
1479 .owner = THIS_MODULE,
1480 .open = stl_proc_open,
1481 .read = seq_read,
1482 .llseek = seq_lseek,
1483 .release = single_release,
1486 /*****************************************************************************/
1489 * All board interrupts are vectored through here first. This code then
1490 * calls off to the approrpriate board interrupt handlers.
1493 static irqreturn_t stl_intr(int irq, void *dev_id)
1495 struct stlbrd *brdp = dev_id;
1497 pr_debug("stl_intr(brdp=%p,irq=%d)\n", brdp, brdp->irq);
1499 return IRQ_RETVAL((* brdp->isr)(brdp));
1502 /*****************************************************************************/
1505 * Interrupt service routine for EasyIO board types.
1508 static int stl_eiointr(struct stlbrd *brdp)
1510 struct stlpanel *panelp;
1511 unsigned int iobase;
1512 int handled = 0;
1514 spin_lock(&brd_lock);
1515 panelp = brdp->panels[0];
1516 iobase = panelp->iobase;
1517 while (inb(brdp->iostatus) & EIO_INTRPEND) {
1518 handled = 1;
1519 (* panelp->isr)(panelp, iobase);
1521 spin_unlock(&brd_lock);
1522 return handled;
1525 /*****************************************************************************/
1528 * Interrupt service routine for ECH-AT board types.
1531 static int stl_echatintr(struct stlbrd *brdp)
1533 struct stlpanel *panelp;
1534 unsigned int ioaddr, bnknr;
1535 int handled = 0;
1537 outb((brdp->ioctrlval | ECH_BRDENABLE), brdp->ioctrl);
1539 while (inb(brdp->iostatus) & ECH_INTRPEND) {
1540 handled = 1;
1541 for (bnknr = 0; bnknr < brdp->nrbnks; bnknr++) {
1542 ioaddr = brdp->bnkstataddr[bnknr];
1543 if (inb(ioaddr) & ECH_PNLINTRPEND) {
1544 panelp = brdp->bnk2panel[bnknr];
1545 (* panelp->isr)(panelp, (ioaddr & 0xfffc));
1550 outb((brdp->ioctrlval | ECH_BRDDISABLE), brdp->ioctrl);
1552 return handled;
1555 /*****************************************************************************/
1558 * Interrupt service routine for ECH-MCA board types.
1561 static int stl_echmcaintr(struct stlbrd *brdp)
1563 struct stlpanel *panelp;
1564 unsigned int ioaddr, bnknr;
1565 int handled = 0;
1567 while (inb(brdp->iostatus) & ECH_INTRPEND) {
1568 handled = 1;
1569 for (bnknr = 0; bnknr < brdp->nrbnks; bnknr++) {
1570 ioaddr = brdp->bnkstataddr[bnknr];
1571 if (inb(ioaddr) & ECH_PNLINTRPEND) {
1572 panelp = brdp->bnk2panel[bnknr];
1573 (* panelp->isr)(panelp, (ioaddr & 0xfffc));
1577 return handled;
1580 /*****************************************************************************/
1583 * Interrupt service routine for ECH-PCI board types.
1586 static int stl_echpciintr(struct stlbrd *brdp)
1588 struct stlpanel *panelp;
1589 unsigned int ioaddr, bnknr, recheck;
1590 int handled = 0;
1592 while (1) {
1593 recheck = 0;
1594 for (bnknr = 0; bnknr < brdp->nrbnks; bnknr++) {
1595 outb(brdp->bnkpageaddr[bnknr], brdp->ioctrl);
1596 ioaddr = brdp->bnkstataddr[bnknr];
1597 if (inb(ioaddr) & ECH_PNLINTRPEND) {
1598 panelp = brdp->bnk2panel[bnknr];
1599 (* panelp->isr)(panelp, (ioaddr & 0xfffc));
1600 recheck++;
1601 handled = 1;
1604 if (! recheck)
1605 break;
1607 return handled;
1610 /*****************************************************************************/
1613 * Interrupt service routine for ECH-8/64-PCI board types.
1616 static int stl_echpci64intr(struct stlbrd *brdp)
1618 struct stlpanel *panelp;
1619 unsigned int ioaddr, bnknr;
1620 int handled = 0;
1622 while (inb(brdp->ioctrl) & 0x1) {
1623 handled = 1;
1624 for (bnknr = 0; bnknr < brdp->nrbnks; bnknr++) {
1625 ioaddr = brdp->bnkstataddr[bnknr];
1626 if (inb(ioaddr) & ECH_PNLINTRPEND) {
1627 panelp = brdp->bnk2panel[bnknr];
1628 (* panelp->isr)(panelp, (ioaddr & 0xfffc));
1633 return handled;
1636 /*****************************************************************************/
1639 * Initialize all the ports on a panel.
1642 static int __devinit stl_initports(struct stlbrd *brdp, struct stlpanel *panelp)
1644 struct stlport *portp;
1645 unsigned int i;
1646 int chipmask;
1648 pr_debug("stl_initports(brdp=%p,panelp=%p)\n", brdp, panelp);
1650 chipmask = stl_panelinit(brdp, panelp);
1653 * All UART's are initialized (if found!). Now go through and setup
1654 * each ports data structures.
1656 for (i = 0; i < panelp->nrports; i++) {
1657 portp = kzalloc(sizeof(struct stlport), GFP_KERNEL);
1658 if (!portp) {
1659 printk("STALLION: failed to allocate memory "
1660 "(size=%Zd)\n", sizeof(struct stlport));
1661 break;
1663 tty_port_init(&portp->port);
1664 portp->port.ops = &stl_port_ops;
1665 portp->magic = STL_PORTMAGIC;
1666 portp->portnr = i;
1667 portp->brdnr = panelp->brdnr;
1668 portp->panelnr = panelp->panelnr;
1669 portp->uartp = panelp->uartp;
1670 portp->clk = brdp->clk;
1671 portp->baud_base = STL_BAUDBASE;
1672 portp->close_delay = STL_CLOSEDELAY;
1673 portp->closing_wait = 30 * HZ;
1674 init_waitqueue_head(&portp->port.open_wait);
1675 init_waitqueue_head(&portp->port.close_wait);
1676 portp->stats.brd = portp->brdnr;
1677 portp->stats.panel = portp->panelnr;
1678 portp->stats.port = portp->portnr;
1679 panelp->ports[i] = portp;
1680 stl_portinit(brdp, panelp, portp);
1683 return 0;
1686 static void stl_cleanup_panels(struct stlbrd *brdp)
1688 struct stlpanel *panelp;
1689 struct stlport *portp;
1690 unsigned int j, k;
1691 struct tty_struct *tty;
1693 for (j = 0; j < STL_MAXPANELS; j++) {
1694 panelp = brdp->panels[j];
1695 if (panelp == NULL)
1696 continue;
1697 for (k = 0; k < STL_PORTSPERPANEL; k++) {
1698 portp = panelp->ports[k];
1699 if (portp == NULL)
1700 continue;
1701 tty = tty_port_tty_get(&portp->port);
1702 if (tty != NULL) {
1703 stl_hangup(tty);
1704 tty_kref_put(tty);
1706 kfree(portp->tx.buf);
1707 kfree(portp);
1709 kfree(panelp);
1713 /*****************************************************************************/
1716 * Try to find and initialize an EasyIO board.
1719 static int __devinit stl_initeio(struct stlbrd *brdp)
1721 struct stlpanel *panelp;
1722 unsigned int status;
1723 char *name;
1724 int retval;
1726 pr_debug("stl_initeio(brdp=%p)\n", brdp);
1728 brdp->ioctrl = brdp->ioaddr1 + 1;
1729 brdp->iostatus = brdp->ioaddr1 + 2;
1731 status = inb(brdp->iostatus);
1732 if ((status & EIO_IDBITMASK) == EIO_MK3)
1733 brdp->ioctrl++;
1736 * Handle board specific stuff now. The real difference is PCI
1737 * or not PCI.
1739 if (brdp->brdtype == BRD_EASYIOPCI) {
1740 brdp->iosize1 = 0x80;
1741 brdp->iosize2 = 0x80;
1742 name = "serial(EIO-PCI)";
1743 outb(0x41, (brdp->ioaddr2 + 0x4c));
1744 } else {
1745 brdp->iosize1 = 8;
1746 name = "serial(EIO)";
1747 if ((brdp->irq < 0) || (brdp->irq > 15) ||
1748 (stl_vecmap[brdp->irq] == (unsigned char) 0xff)) {
1749 printk("STALLION: invalid irq=%d for brd=%d\n",
1750 brdp->irq, brdp->brdnr);
1751 retval = -EINVAL;
1752 goto err;
1754 outb((stl_vecmap[brdp->irq] | EIO_0WS |
1755 ((brdp->irqtype) ? EIO_INTLEVEL : EIO_INTEDGE)),
1756 brdp->ioctrl);
1759 retval = -EBUSY;
1760 if (!request_region(brdp->ioaddr1, brdp->iosize1, name)) {
1761 printk(KERN_WARNING "STALLION: Warning, board %d I/O address "
1762 "%x conflicts with another device\n", brdp->brdnr,
1763 brdp->ioaddr1);
1764 goto err;
1767 if (brdp->iosize2 > 0)
1768 if (!request_region(brdp->ioaddr2, brdp->iosize2, name)) {
1769 printk(KERN_WARNING "STALLION: Warning, board %d I/O "
1770 "address %x conflicts with another device\n",
1771 brdp->brdnr, brdp->ioaddr2);
1772 printk(KERN_WARNING "STALLION: Warning, also "
1773 "releasing board %d I/O address %x \n",
1774 brdp->brdnr, brdp->ioaddr1);
1775 goto err_rel1;
1779 * Everything looks OK, so let's go ahead and probe for the hardware.
1781 brdp->clk = CD1400_CLK;
1782 brdp->isr = stl_eiointr;
1784 retval = -ENODEV;
1785 switch (status & EIO_IDBITMASK) {
1786 case EIO_8PORTM:
1787 brdp->clk = CD1400_CLK8M;
1788 /* fall thru */
1789 case EIO_8PORTRS:
1790 case EIO_8PORTDI:
1791 brdp->nrports = 8;
1792 break;
1793 case EIO_4PORTRS:
1794 brdp->nrports = 4;
1795 break;
1796 case EIO_MK3:
1797 switch (status & EIO_BRDMASK) {
1798 case ID_BRD4:
1799 brdp->nrports = 4;
1800 break;
1801 case ID_BRD8:
1802 brdp->nrports = 8;
1803 break;
1804 case ID_BRD16:
1805 brdp->nrports = 16;
1806 break;
1807 default:
1808 goto err_rel2;
1810 break;
1811 default:
1812 goto err_rel2;
1816 * We have verified that the board is actually present, so now we
1817 * can complete the setup.
1820 panelp = kzalloc(sizeof(struct stlpanel), GFP_KERNEL);
1821 if (!panelp) {
1822 printk(KERN_WARNING "STALLION: failed to allocate memory "
1823 "(size=%Zd)\n", sizeof(struct stlpanel));
1824 retval = -ENOMEM;
1825 goto err_rel2;
1828 panelp->magic = STL_PANELMAGIC;
1829 panelp->brdnr = brdp->brdnr;
1830 panelp->panelnr = 0;
1831 panelp->nrports = brdp->nrports;
1832 panelp->iobase = brdp->ioaddr1;
1833 panelp->hwid = status;
1834 if ((status & EIO_IDBITMASK) == EIO_MK3) {
1835 panelp->uartp = &stl_sc26198uart;
1836 panelp->isr = stl_sc26198intr;
1837 } else {
1838 panelp->uartp = &stl_cd1400uart;
1839 panelp->isr = stl_cd1400eiointr;
1842 brdp->panels[0] = panelp;
1843 brdp->nrpanels = 1;
1844 brdp->state |= BRD_FOUND;
1845 brdp->hwid = status;
1846 if (request_irq(brdp->irq, stl_intr, IRQF_SHARED, name, brdp) != 0) {
1847 printk("STALLION: failed to register interrupt "
1848 "routine for %s irq=%d\n", name, brdp->irq);
1849 retval = -ENODEV;
1850 goto err_fr;
1853 return 0;
1854 err_fr:
1855 stl_cleanup_panels(brdp);
1856 err_rel2:
1857 if (brdp->iosize2 > 0)
1858 release_region(brdp->ioaddr2, brdp->iosize2);
1859 err_rel1:
1860 release_region(brdp->ioaddr1, brdp->iosize1);
1861 err:
1862 return retval;
1865 /*****************************************************************************/
1868 * Try to find an ECH board and initialize it. This code is capable of
1869 * dealing with all types of ECH board.
1872 static int __devinit stl_initech(struct stlbrd *brdp)
1874 struct stlpanel *panelp;
1875 unsigned int status, nxtid, ioaddr, conflict, panelnr, banknr, i;
1876 int retval;
1877 char *name;
1879 pr_debug("stl_initech(brdp=%p)\n", brdp);
1881 status = 0;
1882 conflict = 0;
1885 * Set up the initial board register contents for boards. This varies a
1886 * bit between the different board types. So we need to handle each
1887 * separately. Also do a check that the supplied IRQ is good.
1889 switch (brdp->brdtype) {
1891 case BRD_ECH:
1892 brdp->isr = stl_echatintr;
1893 brdp->ioctrl = brdp->ioaddr1 + 1;
1894 brdp->iostatus = brdp->ioaddr1 + 1;
1895 status = inb(brdp->iostatus);
1896 if ((status & ECH_IDBITMASK) != ECH_ID) {
1897 retval = -ENODEV;
1898 goto err;
1900 if ((brdp->irq < 0) || (brdp->irq > 15) ||
1901 (stl_vecmap[brdp->irq] == (unsigned char) 0xff)) {
1902 printk("STALLION: invalid irq=%d for brd=%d\n",
1903 brdp->irq, brdp->brdnr);
1904 retval = -EINVAL;
1905 goto err;
1907 status = ((brdp->ioaddr2 & ECH_ADDR2MASK) >> 1);
1908 status |= (stl_vecmap[brdp->irq] << 1);
1909 outb((status | ECH_BRDRESET), brdp->ioaddr1);
1910 brdp->ioctrlval = ECH_INTENABLE |
1911 ((brdp->irqtype) ? ECH_INTLEVEL : ECH_INTEDGE);
1912 for (i = 0; i < 10; i++)
1913 outb((brdp->ioctrlval | ECH_BRDENABLE), brdp->ioctrl);
1914 brdp->iosize1 = 2;
1915 brdp->iosize2 = 32;
1916 name = "serial(EC8/32)";
1917 outb(status, brdp->ioaddr1);
1918 break;
1920 case BRD_ECHMC:
1921 brdp->isr = stl_echmcaintr;
1922 brdp->ioctrl = brdp->ioaddr1 + 0x20;
1923 brdp->iostatus = brdp->ioctrl;
1924 status = inb(brdp->iostatus);
1925 if ((status & ECH_IDBITMASK) != ECH_ID) {
1926 retval = -ENODEV;
1927 goto err;
1929 if ((brdp->irq < 0) || (brdp->irq > 15) ||
1930 (stl_vecmap[brdp->irq] == (unsigned char) 0xff)) {
1931 printk("STALLION: invalid irq=%d for brd=%d\n",
1932 brdp->irq, brdp->brdnr);
1933 retval = -EINVAL;
1934 goto err;
1936 outb(ECHMC_BRDRESET, brdp->ioctrl);
1937 outb(ECHMC_INTENABLE, brdp->ioctrl);
1938 brdp->iosize1 = 64;
1939 name = "serial(EC8/32-MC)";
1940 break;
1942 case BRD_ECHPCI:
1943 brdp->isr = stl_echpciintr;
1944 brdp->ioctrl = brdp->ioaddr1 + 2;
1945 brdp->iosize1 = 4;
1946 brdp->iosize2 = 8;
1947 name = "serial(EC8/32-PCI)";
1948 break;
1950 case BRD_ECH64PCI:
1951 brdp->isr = stl_echpci64intr;
1952 brdp->ioctrl = brdp->ioaddr2 + 0x40;
1953 outb(0x43, (brdp->ioaddr1 + 0x4c));
1954 brdp->iosize1 = 0x80;
1955 brdp->iosize2 = 0x80;
1956 name = "serial(EC8/64-PCI)";
1957 break;
1959 default:
1960 printk("STALLION: unknown board type=%d\n", brdp->brdtype);
1961 retval = -EINVAL;
1962 goto err;
1966 * Check boards for possible IO address conflicts and return fail status
1967 * if an IO conflict found.
1969 retval = -EBUSY;
1970 if (!request_region(brdp->ioaddr1, brdp->iosize1, name)) {
1971 printk(KERN_WARNING "STALLION: Warning, board %d I/O address "
1972 "%x conflicts with another device\n", brdp->brdnr,
1973 brdp->ioaddr1);
1974 goto err;
1977 if (brdp->iosize2 > 0)
1978 if (!request_region(brdp->ioaddr2, brdp->iosize2, name)) {
1979 printk(KERN_WARNING "STALLION: Warning, board %d I/O "
1980 "address %x conflicts with another device\n",
1981 brdp->brdnr, brdp->ioaddr2);
1982 printk(KERN_WARNING "STALLION: Warning, also "
1983 "releasing board %d I/O address %x \n",
1984 brdp->brdnr, brdp->ioaddr1);
1985 goto err_rel1;
1989 * Scan through the secondary io address space looking for panels.
1990 * As we find'em allocate and initialize panel structures for each.
1992 brdp->clk = CD1400_CLK;
1993 brdp->hwid = status;
1995 ioaddr = brdp->ioaddr2;
1996 banknr = 0;
1997 panelnr = 0;
1998 nxtid = 0;
2000 for (i = 0; i < STL_MAXPANELS; i++) {
2001 if (brdp->brdtype == BRD_ECHPCI) {
2002 outb(nxtid, brdp->ioctrl);
2003 ioaddr = brdp->ioaddr2;
2005 status = inb(ioaddr + ECH_PNLSTATUS);
2006 if ((status & ECH_PNLIDMASK) != nxtid)
2007 break;
2008 panelp = kzalloc(sizeof(struct stlpanel), GFP_KERNEL);
2009 if (!panelp) {
2010 printk("STALLION: failed to allocate memory "
2011 "(size=%Zd)\n", sizeof(struct stlpanel));
2012 retval = -ENOMEM;
2013 goto err_fr;
2015 panelp->magic = STL_PANELMAGIC;
2016 panelp->brdnr = brdp->brdnr;
2017 panelp->panelnr = panelnr;
2018 panelp->iobase = ioaddr;
2019 panelp->pagenr = nxtid;
2020 panelp->hwid = status;
2021 brdp->bnk2panel[banknr] = panelp;
2022 brdp->bnkpageaddr[banknr] = nxtid;
2023 brdp->bnkstataddr[banknr++] = ioaddr + ECH_PNLSTATUS;
2025 if (status & ECH_PNLXPID) {
2026 panelp->uartp = &stl_sc26198uart;
2027 panelp->isr = stl_sc26198intr;
2028 if (status & ECH_PNL16PORT) {
2029 panelp->nrports = 16;
2030 brdp->bnk2panel[banknr] = panelp;
2031 brdp->bnkpageaddr[banknr] = nxtid;
2032 brdp->bnkstataddr[banknr++] = ioaddr + 4 +
2033 ECH_PNLSTATUS;
2034 } else
2035 panelp->nrports = 8;
2036 } else {
2037 panelp->uartp = &stl_cd1400uart;
2038 panelp->isr = stl_cd1400echintr;
2039 if (status & ECH_PNL16PORT) {
2040 panelp->nrports = 16;
2041 panelp->ackmask = 0x80;
2042 if (brdp->brdtype != BRD_ECHPCI)
2043 ioaddr += EREG_BANKSIZE;
2044 brdp->bnk2panel[banknr] = panelp;
2045 brdp->bnkpageaddr[banknr] = ++nxtid;
2046 brdp->bnkstataddr[banknr++] = ioaddr +
2047 ECH_PNLSTATUS;
2048 } else {
2049 panelp->nrports = 8;
2050 panelp->ackmask = 0xc0;
2054 nxtid++;
2055 ioaddr += EREG_BANKSIZE;
2056 brdp->nrports += panelp->nrports;
2057 brdp->panels[panelnr++] = panelp;
2058 if ((brdp->brdtype != BRD_ECHPCI) &&
2059 (ioaddr >= (brdp->ioaddr2 + brdp->iosize2))) {
2060 retval = -EINVAL;
2061 goto err_fr;
2065 brdp->nrpanels = panelnr;
2066 brdp->nrbnks = banknr;
2067 if (brdp->brdtype == BRD_ECH)
2068 outb((brdp->ioctrlval | ECH_BRDDISABLE), brdp->ioctrl);
2070 brdp->state |= BRD_FOUND;
2071 if (request_irq(brdp->irq, stl_intr, IRQF_SHARED, name, brdp) != 0) {
2072 printk("STALLION: failed to register interrupt "
2073 "routine for %s irq=%d\n", name, brdp->irq);
2074 retval = -ENODEV;
2075 goto err_fr;
2078 return 0;
2079 err_fr:
2080 stl_cleanup_panels(brdp);
2081 if (brdp->iosize2 > 0)
2082 release_region(brdp->ioaddr2, brdp->iosize2);
2083 err_rel1:
2084 release_region(brdp->ioaddr1, brdp->iosize1);
2085 err:
2086 return retval;
2089 /*****************************************************************************/
2092 * Initialize and configure the specified board.
2093 * Scan through all the boards in the configuration and see what we
2094 * can find. Handle EIO and the ECH boards a little differently here
2095 * since the initial search and setup is very different.
2098 static int __devinit stl_brdinit(struct stlbrd *brdp)
2100 int i, retval;
2102 pr_debug("stl_brdinit(brdp=%p)\n", brdp);
2104 switch (brdp->brdtype) {
2105 case BRD_EASYIO:
2106 case BRD_EASYIOPCI:
2107 retval = stl_initeio(brdp);
2108 if (retval)
2109 goto err;
2110 break;
2111 case BRD_ECH:
2112 case BRD_ECHMC:
2113 case BRD_ECHPCI:
2114 case BRD_ECH64PCI:
2115 retval = stl_initech(brdp);
2116 if (retval)
2117 goto err;
2118 break;
2119 default:
2120 printk("STALLION: board=%d is unknown board type=%d\n",
2121 brdp->brdnr, brdp->brdtype);
2122 retval = -ENODEV;
2123 goto err;
2126 if ((brdp->state & BRD_FOUND) == 0) {
2127 printk("STALLION: %s board not found, board=%d io=%x irq=%d\n",
2128 stl_brdnames[brdp->brdtype], brdp->brdnr,
2129 brdp->ioaddr1, brdp->irq);
2130 goto err_free;
2133 for (i = 0; i < STL_MAXPANELS; i++)
2134 if (brdp->panels[i] != NULL)
2135 stl_initports(brdp, brdp->panels[i]);
2137 printk("STALLION: %s found, board=%d io=%x irq=%d "
2138 "nrpanels=%d nrports=%d\n", stl_brdnames[brdp->brdtype],
2139 brdp->brdnr, brdp->ioaddr1, brdp->irq, brdp->nrpanels,
2140 brdp->nrports);
2142 return 0;
2143 err_free:
2144 free_irq(brdp->irq, brdp);
2146 stl_cleanup_panels(brdp);
2148 release_region(brdp->ioaddr1, brdp->iosize1);
2149 if (brdp->iosize2 > 0)
2150 release_region(brdp->ioaddr2, brdp->iosize2);
2151 err:
2152 return retval;
2155 /*****************************************************************************/
2158 * Find the next available board number that is free.
2161 static int __devinit stl_getbrdnr(void)
2163 unsigned int i;
2165 for (i = 0; i < STL_MAXBRDS; i++)
2166 if (stl_brds[i] == NULL) {
2167 if (i >= stl_nrbrds)
2168 stl_nrbrds = i + 1;
2169 return i;
2172 return -1;
2175 /*****************************************************************************/
2177 * We have a Stallion board. Allocate a board structure and
2178 * initialize it. Read its IO and IRQ resources from PCI
2179 * configuration space.
2182 static int __devinit stl_pciprobe(struct pci_dev *pdev,
2183 const struct pci_device_id *ent)
2185 struct stlbrd *brdp;
2186 unsigned int i, brdtype = ent->driver_data;
2187 int brdnr, retval = -ENODEV;
2189 if ((pdev->class >> 8) == PCI_CLASS_STORAGE_IDE)
2190 goto err;
2192 retval = pci_enable_device(pdev);
2193 if (retval)
2194 goto err;
2195 brdp = stl_allocbrd();
2196 if (brdp == NULL) {
2197 retval = -ENOMEM;
2198 goto err;
2200 mutex_lock(&stl_brdslock);
2201 brdnr = stl_getbrdnr();
2202 if (brdnr < 0) {
2203 dev_err(&pdev->dev, "too many boards found, "
2204 "maximum supported %d\n", STL_MAXBRDS);
2205 mutex_unlock(&stl_brdslock);
2206 retval = -ENODEV;
2207 goto err_fr;
2209 brdp->brdnr = (unsigned int)brdnr;
2210 stl_brds[brdp->brdnr] = brdp;
2211 mutex_unlock(&stl_brdslock);
2213 brdp->brdtype = brdtype;
2214 brdp->state |= STL_PROBED;
2217 * We have all resources from the board, so let's setup the actual
2218 * board structure now.
2220 switch (brdtype) {
2221 case BRD_ECHPCI:
2222 brdp->ioaddr2 = pci_resource_start(pdev, 0);
2223 brdp->ioaddr1 = pci_resource_start(pdev, 1);
2224 break;
2225 case BRD_ECH64PCI:
2226 brdp->ioaddr2 = pci_resource_start(pdev, 2);
2227 brdp->ioaddr1 = pci_resource_start(pdev, 1);
2228 break;
2229 case BRD_EASYIOPCI:
2230 brdp->ioaddr1 = pci_resource_start(pdev, 2);
2231 brdp->ioaddr2 = pci_resource_start(pdev, 1);
2232 break;
2233 default:
2234 dev_err(&pdev->dev, "unknown PCI board type=%u\n", brdtype);
2235 break;
2238 brdp->irq = pdev->irq;
2239 retval = stl_brdinit(brdp);
2240 if (retval)
2241 goto err_null;
2243 pci_set_drvdata(pdev, brdp);
2245 for (i = 0; i < brdp->nrports; i++)
2246 tty_register_device(stl_serial,
2247 brdp->brdnr * STL_MAXPORTS + i, &pdev->dev);
2249 return 0;
2250 err_null:
2251 stl_brds[brdp->brdnr] = NULL;
2252 err_fr:
2253 kfree(brdp);
2254 err:
2255 return retval;
2258 static void __devexit stl_pciremove(struct pci_dev *pdev)
2260 struct stlbrd *brdp = pci_get_drvdata(pdev);
2261 unsigned int i;
2263 free_irq(brdp->irq, brdp);
2265 stl_cleanup_panels(brdp);
2267 release_region(brdp->ioaddr1, brdp->iosize1);
2268 if (brdp->iosize2 > 0)
2269 release_region(brdp->ioaddr2, brdp->iosize2);
2271 for (i = 0; i < brdp->nrports; i++)
2272 tty_unregister_device(stl_serial,
2273 brdp->brdnr * STL_MAXPORTS + i);
2275 stl_brds[brdp->brdnr] = NULL;
2276 kfree(brdp);
2279 static struct pci_driver stl_pcidriver = {
2280 .name = "stallion",
2281 .id_table = stl_pcibrds,
2282 .probe = stl_pciprobe,
2283 .remove = __devexit_p(stl_pciremove)
2286 /*****************************************************************************/
2289 * Return the board stats structure to user app.
2292 static int stl_getbrdstats(combrd_t __user *bp)
2294 combrd_t stl_brdstats;
2295 struct stlbrd *brdp;
2296 struct stlpanel *panelp;
2297 unsigned int i;
2299 if (copy_from_user(&stl_brdstats, bp, sizeof(combrd_t)))
2300 return -EFAULT;
2301 if (stl_brdstats.brd >= STL_MAXBRDS)
2302 return -ENODEV;
2303 brdp = stl_brds[stl_brdstats.brd];
2304 if (brdp == NULL)
2305 return -ENODEV;
2307 memset(&stl_brdstats, 0, sizeof(combrd_t));
2308 stl_brdstats.brd = brdp->brdnr;
2309 stl_brdstats.type = brdp->brdtype;
2310 stl_brdstats.hwid = brdp->hwid;
2311 stl_brdstats.state = brdp->state;
2312 stl_brdstats.ioaddr = brdp->ioaddr1;
2313 stl_brdstats.ioaddr2 = brdp->ioaddr2;
2314 stl_brdstats.irq = brdp->irq;
2315 stl_brdstats.nrpanels = brdp->nrpanels;
2316 stl_brdstats.nrports = brdp->nrports;
2317 for (i = 0; i < brdp->nrpanels; i++) {
2318 panelp = brdp->panels[i];
2319 stl_brdstats.panels[i].panel = i;
2320 stl_brdstats.panels[i].hwid = panelp->hwid;
2321 stl_brdstats.panels[i].nrports = panelp->nrports;
2324 return copy_to_user(bp, &stl_brdstats, sizeof(combrd_t)) ? -EFAULT : 0;
2327 /*****************************************************************************/
2330 * Resolve the referenced port number into a port struct pointer.
2333 static struct stlport *stl_getport(int brdnr, int panelnr, int portnr)
2335 struct stlbrd *brdp;
2336 struct stlpanel *panelp;
2338 if (brdnr < 0 || brdnr >= STL_MAXBRDS)
2339 return NULL;
2340 brdp = stl_brds[brdnr];
2341 if (brdp == NULL)
2342 return NULL;
2343 if (panelnr < 0 || (unsigned int)panelnr >= brdp->nrpanels)
2344 return NULL;
2345 panelp = brdp->panels[panelnr];
2346 if (panelp == NULL)
2347 return NULL;
2348 if (portnr < 0 || (unsigned int)portnr >= panelp->nrports)
2349 return NULL;
2350 return panelp->ports[portnr];
2353 /*****************************************************************************/
2356 * Return the port stats structure to user app. A NULL port struct
2357 * pointer passed in means that we need to find out from the app
2358 * what port to get stats for (used through board control device).
2361 static int stl_getportstats(struct tty_struct *tty, struct stlport *portp, comstats_t __user *cp)
2363 comstats_t stl_comstats;
2364 unsigned char *head, *tail;
2365 unsigned long flags;
2367 if (!portp) {
2368 if (copy_from_user(&stl_comstats, cp, sizeof(comstats_t)))
2369 return -EFAULT;
2370 portp = stl_getport(stl_comstats.brd, stl_comstats.panel,
2371 stl_comstats.port);
2372 if (portp == NULL)
2373 return -ENODEV;
2376 portp->stats.state = portp->istate;
2377 portp->stats.flags = portp->port.flags;
2378 portp->stats.hwid = portp->hwid;
2380 portp->stats.ttystate = 0;
2381 portp->stats.cflags = 0;
2382 portp->stats.iflags = 0;
2383 portp->stats.oflags = 0;
2384 portp->stats.lflags = 0;
2385 portp->stats.rxbuffered = 0;
2387 spin_lock_irqsave(&stallion_lock, flags);
2388 if (tty != NULL && portp->port.tty == tty) {
2389 portp->stats.ttystate = tty->flags;
2390 /* No longer available as a statistic */
2391 portp->stats.rxbuffered = 1; /*tty->flip.count; */
2392 if (tty->termios != NULL) {
2393 portp->stats.cflags = tty->termios->c_cflag;
2394 portp->stats.iflags = tty->termios->c_iflag;
2395 portp->stats.oflags = tty->termios->c_oflag;
2396 portp->stats.lflags = tty->termios->c_lflag;
2399 spin_unlock_irqrestore(&stallion_lock, flags);
2401 head = portp->tx.head;
2402 tail = portp->tx.tail;
2403 portp->stats.txbuffered = (head >= tail) ? (head - tail) :
2404 (STL_TXBUFSIZE - (tail - head));
2406 portp->stats.signals = (unsigned long) stl_getsignals(portp);
2408 return copy_to_user(cp, &portp->stats,
2409 sizeof(comstats_t)) ? -EFAULT : 0;
2412 /*****************************************************************************/
2415 * Clear the port stats structure. We also return it zeroed out...
2418 static int stl_clrportstats(struct stlport *portp, comstats_t __user *cp)
2420 comstats_t stl_comstats;
2422 if (!portp) {
2423 if (copy_from_user(&stl_comstats, cp, sizeof(comstats_t)))
2424 return -EFAULT;
2425 portp = stl_getport(stl_comstats.brd, stl_comstats.panel,
2426 stl_comstats.port);
2427 if (portp == NULL)
2428 return -ENODEV;
2431 memset(&portp->stats, 0, sizeof(comstats_t));
2432 portp->stats.brd = portp->brdnr;
2433 portp->stats.panel = portp->panelnr;
2434 portp->stats.port = portp->portnr;
2435 return copy_to_user(cp, &portp->stats,
2436 sizeof(comstats_t)) ? -EFAULT : 0;
2439 /*****************************************************************************/
2442 * Return the entire driver ports structure to a user app.
2445 static int stl_getportstruct(struct stlport __user *arg)
2447 struct stlport stl_dummyport;
2448 struct stlport *portp;
2450 if (copy_from_user(&stl_dummyport, arg, sizeof(struct stlport)))
2451 return -EFAULT;
2452 portp = stl_getport(stl_dummyport.brdnr, stl_dummyport.panelnr,
2453 stl_dummyport.portnr);
2454 if (!portp)
2455 return -ENODEV;
2456 return copy_to_user(arg, portp, sizeof(struct stlport)) ? -EFAULT : 0;
2459 /*****************************************************************************/
2462 * Return the entire driver board structure to a user app.
2465 static int stl_getbrdstruct(struct stlbrd __user *arg)
2467 struct stlbrd stl_dummybrd;
2468 struct stlbrd *brdp;
2470 if (copy_from_user(&stl_dummybrd, arg, sizeof(struct stlbrd)))
2471 return -EFAULT;
2472 if (stl_dummybrd.brdnr >= STL_MAXBRDS)
2473 return -ENODEV;
2474 brdp = stl_brds[stl_dummybrd.brdnr];
2475 if (!brdp)
2476 return -ENODEV;
2477 return copy_to_user(arg, brdp, sizeof(struct stlbrd)) ? -EFAULT : 0;
2480 /*****************************************************************************/
2483 * The "staliomem" device is also required to do some special operations
2484 * on the board and/or ports. In this driver it is mostly used for stats
2485 * collection.
2488 static int stl_memioctl(struct inode *ip, struct file *fp, unsigned int cmd, unsigned long arg)
2490 int brdnr, rc;
2491 void __user *argp = (void __user *)arg;
2493 pr_debug("stl_memioctl(ip=%p,fp=%p,cmd=%x,arg=%lx)\n", ip, fp, cmd,arg);
2495 brdnr = iminor(ip);
2496 if (brdnr >= STL_MAXBRDS)
2497 return -ENODEV;
2498 rc = 0;
2500 switch (cmd) {
2501 case COM_GETPORTSTATS:
2502 rc = stl_getportstats(NULL, NULL, argp);
2503 break;
2504 case COM_CLRPORTSTATS:
2505 rc = stl_clrportstats(NULL, argp);
2506 break;
2507 case COM_GETBRDSTATS:
2508 rc = stl_getbrdstats(argp);
2509 break;
2510 case COM_READPORT:
2511 rc = stl_getportstruct(argp);
2512 break;
2513 case COM_READBOARD:
2514 rc = stl_getbrdstruct(argp);
2515 break;
2516 default:
2517 rc = -ENOIOCTLCMD;
2518 break;
2521 return rc;
2524 static const struct tty_operations stl_ops = {
2525 .open = stl_open,
2526 .close = stl_close,
2527 .write = stl_write,
2528 .put_char = stl_putchar,
2529 .flush_chars = stl_flushchars,
2530 .write_room = stl_writeroom,
2531 .chars_in_buffer = stl_charsinbuffer,
2532 .ioctl = stl_ioctl,
2533 .set_termios = stl_settermios,
2534 .throttle = stl_throttle,
2535 .unthrottle = stl_unthrottle,
2536 .stop = stl_stop,
2537 .start = stl_start,
2538 .hangup = stl_hangup,
2539 .flush_buffer = stl_flushbuffer,
2540 .break_ctl = stl_breakctl,
2541 .wait_until_sent = stl_waituntilsent,
2542 .send_xchar = stl_sendxchar,
2543 .tiocmget = stl_tiocmget,
2544 .tiocmset = stl_tiocmset,
2545 .proc_fops = &stl_proc_fops,
2548 static const struct tty_port_operations stl_port_ops = {
2549 .carrier_raised = stl_carrier_raised,
2550 .dtr_rts = stl_dtr_rts,
2553 /*****************************************************************************/
2554 /* CD1400 HARDWARE FUNCTIONS */
2555 /*****************************************************************************/
2558 * These functions get/set/update the registers of the cd1400 UARTs.
2559 * Access to the cd1400 registers is via an address/data io port pair.
2560 * (Maybe should make this inline...)
2563 static int stl_cd1400getreg(struct stlport *portp, int regnr)
2565 outb((regnr + portp->uartaddr), portp->ioaddr);
2566 return inb(portp->ioaddr + EREG_DATA);
2569 static void stl_cd1400setreg(struct stlport *portp, int regnr, int value)
2571 outb(regnr + portp->uartaddr, portp->ioaddr);
2572 outb(value, portp->ioaddr + EREG_DATA);
2575 static int stl_cd1400updatereg(struct stlport *portp, int regnr, int value)
2577 outb(regnr + portp->uartaddr, portp->ioaddr);
2578 if (inb(portp->ioaddr + EREG_DATA) != value) {
2579 outb(value, portp->ioaddr + EREG_DATA);
2580 return 1;
2582 return 0;
2585 /*****************************************************************************/
2588 * Inbitialize the UARTs in a panel. We don't care what sort of board
2589 * these ports are on - since the port io registers are almost
2590 * identical when dealing with ports.
2593 static int stl_cd1400panelinit(struct stlbrd *brdp, struct stlpanel *panelp)
2595 unsigned int gfrcr;
2596 int chipmask, i, j;
2597 int nrchips, uartaddr, ioaddr;
2598 unsigned long flags;
2600 pr_debug("stl_panelinit(brdp=%p,panelp=%p)\n", brdp, panelp);
2602 spin_lock_irqsave(&brd_lock, flags);
2603 BRDENABLE(panelp->brdnr, panelp->pagenr);
2606 * Check that each chip is present and started up OK.
2608 chipmask = 0;
2609 nrchips = panelp->nrports / CD1400_PORTS;
2610 for (i = 0; i < nrchips; i++) {
2611 if (brdp->brdtype == BRD_ECHPCI) {
2612 outb((panelp->pagenr + (i >> 1)), brdp->ioctrl);
2613 ioaddr = panelp->iobase;
2614 } else
2615 ioaddr = panelp->iobase + (EREG_BANKSIZE * (i >> 1));
2616 uartaddr = (i & 0x01) ? 0x080 : 0;
2617 outb((GFRCR + uartaddr), ioaddr);
2618 outb(0, (ioaddr + EREG_DATA));
2619 outb((CCR + uartaddr), ioaddr);
2620 outb(CCR_RESETFULL, (ioaddr + EREG_DATA));
2621 outb(CCR_RESETFULL, (ioaddr + EREG_DATA));
2622 outb((GFRCR + uartaddr), ioaddr);
2623 for (j = 0; j < CCR_MAXWAIT; j++)
2624 if ((gfrcr = inb(ioaddr + EREG_DATA)) != 0)
2625 break;
2627 if ((j >= CCR_MAXWAIT) || (gfrcr < 0x40) || (gfrcr > 0x60)) {
2628 printk("STALLION: cd1400 not responding, "
2629 "brd=%d panel=%d chip=%d\n",
2630 panelp->brdnr, panelp->panelnr, i);
2631 continue;
2633 chipmask |= (0x1 << i);
2634 outb((PPR + uartaddr), ioaddr);
2635 outb(PPR_SCALAR, (ioaddr + EREG_DATA));
2638 BRDDISABLE(panelp->brdnr);
2639 spin_unlock_irqrestore(&brd_lock, flags);
2640 return chipmask;
2643 /*****************************************************************************/
2646 * Initialize hardware specific port registers.
2649 static void stl_cd1400portinit(struct stlbrd *brdp, struct stlpanel *panelp, struct stlport *portp)
2651 unsigned long flags;
2652 pr_debug("stl_cd1400portinit(brdp=%p,panelp=%p,portp=%p)\n", brdp,
2653 panelp, portp);
2655 if ((brdp == NULL) || (panelp == NULL) ||
2656 (portp == NULL))
2657 return;
2659 spin_lock_irqsave(&brd_lock, flags);
2660 portp->ioaddr = panelp->iobase + (((brdp->brdtype == BRD_ECHPCI) ||
2661 (portp->portnr < 8)) ? 0 : EREG_BANKSIZE);
2662 portp->uartaddr = (portp->portnr & 0x04) << 5;
2663 portp->pagenr = panelp->pagenr + (portp->portnr >> 3);
2665 BRDENABLE(portp->brdnr, portp->pagenr);
2666 stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
2667 stl_cd1400setreg(portp, LIVR, (portp->portnr << 3));
2668 portp->hwid = stl_cd1400getreg(portp, GFRCR);
2669 BRDDISABLE(portp->brdnr);
2670 spin_unlock_irqrestore(&brd_lock, flags);
2673 /*****************************************************************************/
2676 * Wait for the command register to be ready. We will poll this,
2677 * since it won't usually take too long to be ready.
2680 static void stl_cd1400ccrwait(struct stlport *portp)
2682 int i;
2684 for (i = 0; i < CCR_MAXWAIT; i++)
2685 if (stl_cd1400getreg(portp, CCR) == 0)
2686 return;
2688 printk("STALLION: cd1400 not responding, port=%d panel=%d brd=%d\n",
2689 portp->portnr, portp->panelnr, portp->brdnr);
2692 /*****************************************************************************/
2695 * Set up the cd1400 registers for a port based on the termios port
2696 * settings.
2699 static void stl_cd1400setport(struct stlport *portp, struct ktermios *tiosp)
2701 struct stlbrd *brdp;
2702 unsigned long flags;
2703 unsigned int clkdiv, baudrate;
2704 unsigned char cor1, cor2, cor3;
2705 unsigned char cor4, cor5, ccr;
2706 unsigned char srer, sreron, sreroff;
2707 unsigned char mcor1, mcor2, rtpr;
2708 unsigned char clk, div;
2710 cor1 = 0;
2711 cor2 = 0;
2712 cor3 = 0;
2713 cor4 = 0;
2714 cor5 = 0;
2715 ccr = 0;
2716 rtpr = 0;
2717 clk = 0;
2718 div = 0;
2719 mcor1 = 0;
2720 mcor2 = 0;
2721 sreron = 0;
2722 sreroff = 0;
2724 brdp = stl_brds[portp->brdnr];
2725 if (brdp == NULL)
2726 return;
2729 * Set up the RX char ignore mask with those RX error types we
2730 * can ignore. We can get the cd1400 to help us out a little here,
2731 * it will ignore parity errors and breaks for us.
2733 portp->rxignoremsk = 0;
2734 if (tiosp->c_iflag & IGNPAR) {
2735 portp->rxignoremsk |= (ST_PARITY | ST_FRAMING | ST_OVERRUN);
2736 cor1 |= COR1_PARIGNORE;
2738 if (tiosp->c_iflag & IGNBRK) {
2739 portp->rxignoremsk |= ST_BREAK;
2740 cor4 |= COR4_IGNBRK;
2743 portp->rxmarkmsk = ST_OVERRUN;
2744 if (tiosp->c_iflag & (INPCK | PARMRK))
2745 portp->rxmarkmsk |= (ST_PARITY | ST_FRAMING);
2746 if (tiosp->c_iflag & BRKINT)
2747 portp->rxmarkmsk |= ST_BREAK;
2750 * Go through the char size, parity and stop bits and set all the
2751 * option register appropriately.
2753 switch (tiosp->c_cflag & CSIZE) {
2754 case CS5:
2755 cor1 |= COR1_CHL5;
2756 break;
2757 case CS6:
2758 cor1 |= COR1_CHL6;
2759 break;
2760 case CS7:
2761 cor1 |= COR1_CHL7;
2762 break;
2763 default:
2764 cor1 |= COR1_CHL8;
2765 break;
2768 if (tiosp->c_cflag & CSTOPB)
2769 cor1 |= COR1_STOP2;
2770 else
2771 cor1 |= COR1_STOP1;
2773 if (tiosp->c_cflag & PARENB) {
2774 if (tiosp->c_cflag & PARODD)
2775 cor1 |= (COR1_PARENB | COR1_PARODD);
2776 else
2777 cor1 |= (COR1_PARENB | COR1_PAREVEN);
2778 } else {
2779 cor1 |= COR1_PARNONE;
2783 * Set the RX FIFO threshold at 6 chars. This gives a bit of breathing
2784 * space for hardware flow control and the like. This should be set to
2785 * VMIN. Also here we will set the RX data timeout to 10ms - this should
2786 * really be based on VTIME.
2788 cor3 |= FIFO_RXTHRESHOLD;
2789 rtpr = 2;
2792 * Calculate the baud rate timers. For now we will just assume that
2793 * the input and output baud are the same. Could have used a baud
2794 * table here, but this way we can generate virtually any baud rate
2795 * we like!
2797 baudrate = tiosp->c_cflag & CBAUD;
2798 if (baudrate & CBAUDEX) {
2799 baudrate &= ~CBAUDEX;
2800 if ((baudrate < 1) || (baudrate > 4))
2801 tiosp->c_cflag &= ~CBAUDEX;
2802 else
2803 baudrate += 15;
2805 baudrate = stl_baudrates[baudrate];
2806 if ((tiosp->c_cflag & CBAUD) == B38400) {
2807 if ((portp->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
2808 baudrate = 57600;
2809 else if ((portp->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
2810 baudrate = 115200;
2811 else if ((portp->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
2812 baudrate = 230400;
2813 else if ((portp->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
2814 baudrate = 460800;
2815 else if ((portp->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)
2816 baudrate = (portp->baud_base / portp->custom_divisor);
2818 if (baudrate > STL_CD1400MAXBAUD)
2819 baudrate = STL_CD1400MAXBAUD;
2821 if (baudrate > 0) {
2822 for (clk = 0; clk < CD1400_NUMCLKS; clk++) {
2823 clkdiv = (portp->clk / stl_cd1400clkdivs[clk]) / baudrate;
2824 if (clkdiv < 0x100)
2825 break;
2827 div = (unsigned char) clkdiv;
2831 * Check what form of modem signaling is required and set it up.
2833 if ((tiosp->c_cflag & CLOCAL) == 0) {
2834 mcor1 |= MCOR1_DCD;
2835 mcor2 |= MCOR2_DCD;
2836 sreron |= SRER_MODEM;
2837 portp->port.flags |= ASYNC_CHECK_CD;
2838 } else
2839 portp->port.flags &= ~ASYNC_CHECK_CD;
2842 * Setup cd1400 enhanced modes if we can. In particular we want to
2843 * handle as much of the flow control as possible automatically. As
2844 * well as saving a few CPU cycles it will also greatly improve flow
2845 * control reliability.
2847 if (tiosp->c_iflag & IXON) {
2848 cor2 |= COR2_TXIBE;
2849 cor3 |= COR3_SCD12;
2850 if (tiosp->c_iflag & IXANY)
2851 cor2 |= COR2_IXM;
2854 if (tiosp->c_cflag & CRTSCTS) {
2855 cor2 |= COR2_CTSAE;
2856 mcor1 |= FIFO_RTSTHRESHOLD;
2860 * All cd1400 register values calculated so go through and set
2861 * them all up.
2864 pr_debug("SETPORT: portnr=%d panelnr=%d brdnr=%d\n",
2865 portp->portnr, portp->panelnr, portp->brdnr);
2866 pr_debug(" cor1=%x cor2=%x cor3=%x cor4=%x cor5=%x\n",
2867 cor1, cor2, cor3, cor4, cor5);
2868 pr_debug(" mcor1=%x mcor2=%x rtpr=%x sreron=%x sreroff=%x\n",
2869 mcor1, mcor2, rtpr, sreron, sreroff);
2870 pr_debug(" tcor=%x tbpr=%x rcor=%x rbpr=%x\n", clk, div, clk, div);
2871 pr_debug(" schr1=%x schr2=%x schr3=%x schr4=%x\n",
2872 tiosp->c_cc[VSTART], tiosp->c_cc[VSTOP],
2873 tiosp->c_cc[VSTART], tiosp->c_cc[VSTOP]);
2875 spin_lock_irqsave(&brd_lock, flags);
2876 BRDENABLE(portp->brdnr, portp->pagenr);
2877 stl_cd1400setreg(portp, CAR, (portp->portnr & 0x3));
2878 srer = stl_cd1400getreg(portp, SRER);
2879 stl_cd1400setreg(portp, SRER, 0);
2880 if (stl_cd1400updatereg(portp, COR1, cor1))
2881 ccr = 1;
2882 if (stl_cd1400updatereg(portp, COR2, cor2))
2883 ccr = 1;
2884 if (stl_cd1400updatereg(portp, COR3, cor3))
2885 ccr = 1;
2886 if (ccr) {
2887 stl_cd1400ccrwait(portp);
2888 stl_cd1400setreg(portp, CCR, CCR_CORCHANGE);
2890 stl_cd1400setreg(portp, COR4, cor4);
2891 stl_cd1400setreg(portp, COR5, cor5);
2892 stl_cd1400setreg(portp, MCOR1, mcor1);
2893 stl_cd1400setreg(portp, MCOR2, mcor2);
2894 if (baudrate > 0) {
2895 stl_cd1400setreg(portp, TCOR, clk);
2896 stl_cd1400setreg(portp, TBPR, div);
2897 stl_cd1400setreg(portp, RCOR, clk);
2898 stl_cd1400setreg(portp, RBPR, div);
2900 stl_cd1400setreg(portp, SCHR1, tiosp->c_cc[VSTART]);
2901 stl_cd1400setreg(portp, SCHR2, tiosp->c_cc[VSTOP]);
2902 stl_cd1400setreg(portp, SCHR3, tiosp->c_cc[VSTART]);
2903 stl_cd1400setreg(portp, SCHR4, tiosp->c_cc[VSTOP]);
2904 stl_cd1400setreg(portp, RTPR, rtpr);
2905 mcor1 = stl_cd1400getreg(portp, MSVR1);
2906 if (mcor1 & MSVR1_DCD)
2907 portp->sigs |= TIOCM_CD;
2908 else
2909 portp->sigs &= ~TIOCM_CD;
2910 stl_cd1400setreg(portp, SRER, ((srer & ~sreroff) | sreron));
2911 BRDDISABLE(portp->brdnr);
2912 spin_unlock_irqrestore(&brd_lock, flags);
2915 /*****************************************************************************/
2918 * Set the state of the DTR and RTS signals.
2921 static void stl_cd1400setsignals(struct stlport *portp, int dtr, int rts)
2923 unsigned char msvr1, msvr2;
2924 unsigned long flags;
2926 pr_debug("stl_cd1400setsignals(portp=%p,dtr=%d,rts=%d)\n",
2927 portp, dtr, rts);
2929 msvr1 = 0;
2930 msvr2 = 0;
2931 if (dtr > 0)
2932 msvr1 = MSVR1_DTR;
2933 if (rts > 0)
2934 msvr2 = MSVR2_RTS;
2936 spin_lock_irqsave(&brd_lock, flags);
2937 BRDENABLE(portp->brdnr, portp->pagenr);
2938 stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
2939 if (rts >= 0)
2940 stl_cd1400setreg(portp, MSVR2, msvr2);
2941 if (dtr >= 0)
2942 stl_cd1400setreg(portp, MSVR1, msvr1);
2943 BRDDISABLE(portp->brdnr);
2944 spin_unlock_irqrestore(&brd_lock, flags);
2947 /*****************************************************************************/
2950 * Return the state of the signals.
2953 static int stl_cd1400getsignals(struct stlport *portp)
2955 unsigned char msvr1, msvr2;
2956 unsigned long flags;
2957 int sigs;
2959 pr_debug("stl_cd1400getsignals(portp=%p)\n", portp);
2961 spin_lock_irqsave(&brd_lock, flags);
2962 BRDENABLE(portp->brdnr, portp->pagenr);
2963 stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
2964 msvr1 = stl_cd1400getreg(portp, MSVR1);
2965 msvr2 = stl_cd1400getreg(portp, MSVR2);
2966 BRDDISABLE(portp->brdnr);
2967 spin_unlock_irqrestore(&brd_lock, flags);
2969 sigs = 0;
2970 sigs |= (msvr1 & MSVR1_DCD) ? TIOCM_CD : 0;
2971 sigs |= (msvr1 & MSVR1_CTS) ? TIOCM_CTS : 0;
2972 sigs |= (msvr1 & MSVR1_DTR) ? TIOCM_DTR : 0;
2973 sigs |= (msvr2 & MSVR2_RTS) ? TIOCM_RTS : 0;
2974 #if 0
2975 sigs |= (msvr1 & MSVR1_RI) ? TIOCM_RI : 0;
2976 sigs |= (msvr1 & MSVR1_DSR) ? TIOCM_DSR : 0;
2977 #else
2978 sigs |= TIOCM_DSR;
2979 #endif
2980 return sigs;
2983 /*****************************************************************************/
2986 * Enable/Disable the Transmitter and/or Receiver.
2989 static void stl_cd1400enablerxtx(struct stlport *portp, int rx, int tx)
2991 unsigned char ccr;
2992 unsigned long flags;
2994 pr_debug("stl_cd1400enablerxtx(portp=%p,rx=%d,tx=%d)\n", portp, rx, tx);
2996 ccr = 0;
2998 if (tx == 0)
2999 ccr |= CCR_TXDISABLE;
3000 else if (tx > 0)
3001 ccr |= CCR_TXENABLE;
3002 if (rx == 0)
3003 ccr |= CCR_RXDISABLE;
3004 else if (rx > 0)
3005 ccr |= CCR_RXENABLE;
3007 spin_lock_irqsave(&brd_lock, flags);
3008 BRDENABLE(portp->brdnr, portp->pagenr);
3009 stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
3010 stl_cd1400ccrwait(portp);
3011 stl_cd1400setreg(portp, CCR, ccr);
3012 stl_cd1400ccrwait(portp);
3013 BRDDISABLE(portp->brdnr);
3014 spin_unlock_irqrestore(&brd_lock, flags);
3017 /*****************************************************************************/
3020 * Start/stop the Transmitter and/or Receiver.
3023 static void stl_cd1400startrxtx(struct stlport *portp, int rx, int tx)
3025 unsigned char sreron, sreroff;
3026 unsigned long flags;
3028 pr_debug("stl_cd1400startrxtx(portp=%p,rx=%d,tx=%d)\n", portp, rx, tx);
3030 sreron = 0;
3031 sreroff = 0;
3032 if (tx == 0)
3033 sreroff |= (SRER_TXDATA | SRER_TXEMPTY);
3034 else if (tx == 1)
3035 sreron |= SRER_TXDATA;
3036 else if (tx >= 2)
3037 sreron |= SRER_TXEMPTY;
3038 if (rx == 0)
3039 sreroff |= SRER_RXDATA;
3040 else if (rx > 0)
3041 sreron |= SRER_RXDATA;
3043 spin_lock_irqsave(&brd_lock, flags);
3044 BRDENABLE(portp->brdnr, portp->pagenr);
3045 stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
3046 stl_cd1400setreg(portp, SRER,
3047 ((stl_cd1400getreg(portp, SRER) & ~sreroff) | sreron));
3048 BRDDISABLE(portp->brdnr);
3049 if (tx > 0)
3050 set_bit(ASYI_TXBUSY, &portp->istate);
3051 spin_unlock_irqrestore(&brd_lock, flags);
3054 /*****************************************************************************/
3057 * Disable all interrupts from this port.
3060 static void stl_cd1400disableintrs(struct stlport *portp)
3062 unsigned long flags;
3064 pr_debug("stl_cd1400disableintrs(portp=%p)\n", portp);
3066 spin_lock_irqsave(&brd_lock, flags);
3067 BRDENABLE(portp->brdnr, portp->pagenr);
3068 stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
3069 stl_cd1400setreg(portp, SRER, 0);
3070 BRDDISABLE(portp->brdnr);
3071 spin_unlock_irqrestore(&brd_lock, flags);
3074 /*****************************************************************************/
3076 static void stl_cd1400sendbreak(struct stlport *portp, int len)
3078 unsigned long flags;
3080 pr_debug("stl_cd1400sendbreak(portp=%p,len=%d)\n", portp, len);
3082 spin_lock_irqsave(&brd_lock, flags);
3083 BRDENABLE(portp->brdnr, portp->pagenr);
3084 stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
3085 stl_cd1400setreg(portp, SRER,
3086 ((stl_cd1400getreg(portp, SRER) & ~SRER_TXDATA) |
3087 SRER_TXEMPTY));
3088 BRDDISABLE(portp->brdnr);
3089 portp->brklen = len;
3090 if (len == 1)
3091 portp->stats.txbreaks++;
3092 spin_unlock_irqrestore(&brd_lock, flags);
3095 /*****************************************************************************/
3098 * Take flow control actions...
3101 static void stl_cd1400flowctrl(struct stlport *portp, int state)
3103 struct tty_struct *tty;
3104 unsigned long flags;
3106 pr_debug("stl_cd1400flowctrl(portp=%p,state=%x)\n", portp, state);
3108 if (portp == NULL)
3109 return;
3110 tty = tty_port_tty_get(&portp->port);
3111 if (tty == NULL)
3112 return;
3114 spin_lock_irqsave(&brd_lock, flags);
3115 BRDENABLE(portp->brdnr, portp->pagenr);
3116 stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
3118 if (state) {
3119 if (tty->termios->c_iflag & IXOFF) {
3120 stl_cd1400ccrwait(portp);
3121 stl_cd1400setreg(portp, CCR, CCR_SENDSCHR1);
3122 portp->stats.rxxon++;
3123 stl_cd1400ccrwait(portp);
3126 * Question: should we return RTS to what it was before? It may
3127 * have been set by an ioctl... Suppose not, since if you have
3128 * hardware flow control set then it is pretty silly to go and
3129 * set the RTS line by hand.
3131 if (tty->termios->c_cflag & CRTSCTS) {
3132 stl_cd1400setreg(portp, MCOR1,
3133 (stl_cd1400getreg(portp, MCOR1) |
3134 FIFO_RTSTHRESHOLD));
3135 stl_cd1400setreg(portp, MSVR2, MSVR2_RTS);
3136 portp->stats.rxrtson++;
3138 } else {
3139 if (tty->termios->c_iflag & IXOFF) {
3140 stl_cd1400ccrwait(portp);
3141 stl_cd1400setreg(portp, CCR, CCR_SENDSCHR2);
3142 portp->stats.rxxoff++;
3143 stl_cd1400ccrwait(portp);
3145 if (tty->termios->c_cflag & CRTSCTS) {
3146 stl_cd1400setreg(portp, MCOR1,
3147 (stl_cd1400getreg(portp, MCOR1) & 0xf0));
3148 stl_cd1400setreg(portp, MSVR2, 0);
3149 portp->stats.rxrtsoff++;
3153 BRDDISABLE(portp->brdnr);
3154 spin_unlock_irqrestore(&brd_lock, flags);
3155 tty_kref_put(tty);
3158 /*****************************************************************************/
3161 * Send a flow control character...
3164 static void stl_cd1400sendflow(struct stlport *portp, int state)
3166 struct tty_struct *tty;
3167 unsigned long flags;
3169 pr_debug("stl_cd1400sendflow(portp=%p,state=%x)\n", portp, state);
3171 if (portp == NULL)
3172 return;
3173 tty = tty_port_tty_get(&portp->port);
3174 if (tty == NULL)
3175 return;
3177 spin_lock_irqsave(&brd_lock, flags);
3178 BRDENABLE(portp->brdnr, portp->pagenr);
3179 stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
3180 if (state) {
3181 stl_cd1400ccrwait(portp);
3182 stl_cd1400setreg(portp, CCR, CCR_SENDSCHR1);
3183 portp->stats.rxxon++;
3184 stl_cd1400ccrwait(portp);
3185 } else {
3186 stl_cd1400ccrwait(portp);
3187 stl_cd1400setreg(portp, CCR, CCR_SENDSCHR2);
3188 portp->stats.rxxoff++;
3189 stl_cd1400ccrwait(portp);
3191 BRDDISABLE(portp->brdnr);
3192 spin_unlock_irqrestore(&brd_lock, flags);
3193 tty_kref_put(tty);
3196 /*****************************************************************************/
3198 static void stl_cd1400flush(struct stlport *portp)
3200 unsigned long flags;
3202 pr_debug("stl_cd1400flush(portp=%p)\n", portp);
3204 if (portp == NULL)
3205 return;
3207 spin_lock_irqsave(&brd_lock, flags);
3208 BRDENABLE(portp->brdnr, portp->pagenr);
3209 stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
3210 stl_cd1400ccrwait(portp);
3211 stl_cd1400setreg(portp, CCR, CCR_TXFLUSHFIFO);
3212 stl_cd1400ccrwait(portp);
3213 portp->tx.tail = portp->tx.head;
3214 BRDDISABLE(portp->brdnr);
3215 spin_unlock_irqrestore(&brd_lock, flags);
3218 /*****************************************************************************/
3221 * Return the current state of data flow on this port. This is only
3222 * really interresting when determining if data has fully completed
3223 * transmission or not... This is easy for the cd1400, it accurately
3224 * maintains the busy port flag.
3227 static int stl_cd1400datastate(struct stlport *portp)
3229 pr_debug("stl_cd1400datastate(portp=%p)\n", portp);
3231 if (portp == NULL)
3232 return 0;
3234 return test_bit(ASYI_TXBUSY, &portp->istate) ? 1 : 0;
3237 /*****************************************************************************/
3240 * Interrupt service routine for cd1400 EasyIO boards.
3243 static void stl_cd1400eiointr(struct stlpanel *panelp, unsigned int iobase)
3245 unsigned char svrtype;
3247 pr_debug("stl_cd1400eiointr(panelp=%p,iobase=%x)\n", panelp, iobase);
3249 spin_lock(&brd_lock);
3250 outb(SVRR, iobase);
3251 svrtype = inb(iobase + EREG_DATA);
3252 if (panelp->nrports > 4) {
3253 outb((SVRR + 0x80), iobase);
3254 svrtype |= inb(iobase + EREG_DATA);
3257 if (svrtype & SVRR_RX)
3258 stl_cd1400rxisr(panelp, iobase);
3259 else if (svrtype & SVRR_TX)
3260 stl_cd1400txisr(panelp, iobase);
3261 else if (svrtype & SVRR_MDM)
3262 stl_cd1400mdmisr(panelp, iobase);
3264 spin_unlock(&brd_lock);
3267 /*****************************************************************************/
3270 * Interrupt service routine for cd1400 panels.
3273 static void stl_cd1400echintr(struct stlpanel *panelp, unsigned int iobase)
3275 unsigned char svrtype;
3277 pr_debug("stl_cd1400echintr(panelp=%p,iobase=%x)\n", panelp, iobase);
3279 outb(SVRR, iobase);
3280 svrtype = inb(iobase + EREG_DATA);
3281 outb((SVRR + 0x80), iobase);
3282 svrtype |= inb(iobase + EREG_DATA);
3283 if (svrtype & SVRR_RX)
3284 stl_cd1400rxisr(panelp, iobase);
3285 else if (svrtype & SVRR_TX)
3286 stl_cd1400txisr(panelp, iobase);
3287 else if (svrtype & SVRR_MDM)
3288 stl_cd1400mdmisr(panelp, iobase);
3292 /*****************************************************************************/
3295 * Unfortunately we need to handle breaks in the TX data stream, since
3296 * this is the only way to generate them on the cd1400.
3299 static int stl_cd1400breakisr(struct stlport *portp, int ioaddr)
3301 if (portp->brklen == 1) {
3302 outb((COR2 + portp->uartaddr), ioaddr);
3303 outb((inb(ioaddr + EREG_DATA) | COR2_ETC),
3304 (ioaddr + EREG_DATA));
3305 outb((TDR + portp->uartaddr), ioaddr);
3306 outb(ETC_CMD, (ioaddr + EREG_DATA));
3307 outb(ETC_STARTBREAK, (ioaddr + EREG_DATA));
3308 outb((SRER + portp->uartaddr), ioaddr);
3309 outb((inb(ioaddr + EREG_DATA) & ~(SRER_TXDATA | SRER_TXEMPTY)),
3310 (ioaddr + EREG_DATA));
3311 return 1;
3312 } else if (portp->brklen > 1) {
3313 outb((TDR + portp->uartaddr), ioaddr);
3314 outb(ETC_CMD, (ioaddr + EREG_DATA));
3315 outb(ETC_STOPBREAK, (ioaddr + EREG_DATA));
3316 portp->brklen = -1;
3317 return 1;
3318 } else {
3319 outb((COR2 + portp->uartaddr), ioaddr);
3320 outb((inb(ioaddr + EREG_DATA) & ~COR2_ETC),
3321 (ioaddr + EREG_DATA));
3322 portp->brklen = 0;
3324 return 0;
3327 /*****************************************************************************/
3330 * Transmit interrupt handler. This has gotta be fast! Handling TX
3331 * chars is pretty simple, stuff as many as possible from the TX buffer
3332 * into the cd1400 FIFO. Must also handle TX breaks here, since they
3333 * are embedded as commands in the data stream. Oh no, had to use a goto!
3334 * This could be optimized more, will do when I get time...
3335 * In practice it is possible that interrupts are enabled but that the
3336 * port has been hung up. Need to handle not having any TX buffer here,
3337 * this is done by using the side effect that head and tail will also
3338 * be NULL if the buffer has been freed.
3341 static void stl_cd1400txisr(struct stlpanel *panelp, int ioaddr)
3343 struct stlport *portp;
3344 int len, stlen;
3345 char *head, *tail;
3346 unsigned char ioack, srer;
3347 struct tty_struct *tty;
3349 pr_debug("stl_cd1400txisr(panelp=%p,ioaddr=%x)\n", panelp, ioaddr);
3351 ioack = inb(ioaddr + EREG_TXACK);
3352 if (((ioack & panelp->ackmask) != 0) ||
3353 ((ioack & ACK_TYPMASK) != ACK_TYPTX)) {
3354 printk("STALLION: bad TX interrupt ack value=%x\n", ioack);
3355 return;
3357 portp = panelp->ports[(ioack >> 3)];
3360 * Unfortunately we need to handle breaks in the data stream, since
3361 * this is the only way to generate them on the cd1400. Do it now if
3362 * a break is to be sent.
3364 if (portp->brklen != 0)
3365 if (stl_cd1400breakisr(portp, ioaddr))
3366 goto stl_txalldone;
3368 head = portp->tx.head;
3369 tail = portp->tx.tail;
3370 len = (head >= tail) ? (head - tail) : (STL_TXBUFSIZE - (tail - head));
3371 if ((len == 0) || ((len < STL_TXBUFLOW) &&
3372 (test_bit(ASYI_TXLOW, &portp->istate) == 0))) {
3373 set_bit(ASYI_TXLOW, &portp->istate);
3374 tty = tty_port_tty_get(&portp->port);
3375 if (tty) {
3376 tty_wakeup(tty);
3377 tty_kref_put(tty);
3381 if (len == 0) {
3382 outb((SRER + portp->uartaddr), ioaddr);
3383 srer = inb(ioaddr + EREG_DATA);
3384 if (srer & SRER_TXDATA) {
3385 srer = (srer & ~SRER_TXDATA) | SRER_TXEMPTY;
3386 } else {
3387 srer &= ~(SRER_TXDATA | SRER_TXEMPTY);
3388 clear_bit(ASYI_TXBUSY, &portp->istate);
3390 outb(srer, (ioaddr + EREG_DATA));
3391 } else {
3392 len = min(len, CD1400_TXFIFOSIZE);
3393 portp->stats.txtotal += len;
3394 stlen = min_t(unsigned int, len,
3395 (portp->tx.buf + STL_TXBUFSIZE) - tail);
3396 outb((TDR + portp->uartaddr), ioaddr);
3397 outsb((ioaddr + EREG_DATA), tail, stlen);
3398 len -= stlen;
3399 tail += stlen;
3400 if (tail >= (portp->tx.buf + STL_TXBUFSIZE))
3401 tail = portp->tx.buf;
3402 if (len > 0) {
3403 outsb((ioaddr + EREG_DATA), tail, len);
3404 tail += len;
3406 portp->tx.tail = tail;
3409 stl_txalldone:
3410 outb((EOSRR + portp->uartaddr), ioaddr);
3411 outb(0, (ioaddr + EREG_DATA));
3414 /*****************************************************************************/
3417 * Receive character interrupt handler. Determine if we have good chars
3418 * or bad chars and then process appropriately. Good chars are easy
3419 * just shove the lot into the RX buffer and set all status byte to 0.
3420 * If a bad RX char then process as required. This routine needs to be
3421 * fast! In practice it is possible that we get an interrupt on a port
3422 * that is closed. This can happen on hangups - since they completely
3423 * shutdown a port not in user context. Need to handle this case.
3426 static void stl_cd1400rxisr(struct stlpanel *panelp, int ioaddr)
3428 struct stlport *portp;
3429 struct tty_struct *tty;
3430 unsigned int ioack, len, buflen;
3431 unsigned char status;
3432 char ch;
3434 pr_debug("stl_cd1400rxisr(panelp=%p,ioaddr=%x)\n", panelp, ioaddr);
3436 ioack = inb(ioaddr + EREG_RXACK);
3437 if ((ioack & panelp->ackmask) != 0) {
3438 printk("STALLION: bad RX interrupt ack value=%x\n", ioack);
3439 return;
3441 portp = panelp->ports[(ioack >> 3)];
3442 tty = tty_port_tty_get(&portp->port);
3444 if ((ioack & ACK_TYPMASK) == ACK_TYPRXGOOD) {
3445 outb((RDCR + portp->uartaddr), ioaddr);
3446 len = inb(ioaddr + EREG_DATA);
3447 if (tty == NULL || (buflen = tty_buffer_request_room(tty, len)) == 0) {
3448 len = min_t(unsigned int, len, sizeof(stl_unwanted));
3449 outb((RDSR + portp->uartaddr), ioaddr);
3450 insb((ioaddr + EREG_DATA), &stl_unwanted[0], len);
3451 portp->stats.rxlost += len;
3452 portp->stats.rxtotal += len;
3453 } else {
3454 len = min(len, buflen);
3455 if (len > 0) {
3456 unsigned char *ptr;
3457 outb((RDSR + portp->uartaddr), ioaddr);
3458 tty_prepare_flip_string(tty, &ptr, len);
3459 insb((ioaddr + EREG_DATA), ptr, len);
3460 tty_schedule_flip(tty);
3461 portp->stats.rxtotal += len;
3464 } else if ((ioack & ACK_TYPMASK) == ACK_TYPRXBAD) {
3465 outb((RDSR + portp->uartaddr), ioaddr);
3466 status = inb(ioaddr + EREG_DATA);
3467 ch = inb(ioaddr + EREG_DATA);
3468 if (status & ST_PARITY)
3469 portp->stats.rxparity++;
3470 if (status & ST_FRAMING)
3471 portp->stats.rxframing++;
3472 if (status & ST_OVERRUN)
3473 portp->stats.rxoverrun++;
3474 if (status & ST_BREAK)
3475 portp->stats.rxbreaks++;
3476 if (status & ST_SCHARMASK) {
3477 if ((status & ST_SCHARMASK) == ST_SCHAR1)
3478 portp->stats.txxon++;
3479 if ((status & ST_SCHARMASK) == ST_SCHAR2)
3480 portp->stats.txxoff++;
3481 goto stl_rxalldone;
3483 if (tty != NULL && (portp->rxignoremsk & status) == 0) {
3484 if (portp->rxmarkmsk & status) {
3485 if (status & ST_BREAK) {
3486 status = TTY_BREAK;
3487 if (portp->port.flags & ASYNC_SAK) {
3488 do_SAK(tty);
3489 BRDENABLE(portp->brdnr, portp->pagenr);
3491 } else if (status & ST_PARITY)
3492 status = TTY_PARITY;
3493 else if (status & ST_FRAMING)
3494 status = TTY_FRAME;
3495 else if(status & ST_OVERRUN)
3496 status = TTY_OVERRUN;
3497 else
3498 status = 0;
3499 } else
3500 status = 0;
3501 tty_insert_flip_char(tty, ch, status);
3502 tty_schedule_flip(tty);
3504 } else {
3505 printk("STALLION: bad RX interrupt ack value=%x\n", ioack);
3506 tty_kref_put(tty);
3507 return;
3510 stl_rxalldone:
3511 tty_kref_put(tty);
3512 outb((EOSRR + portp->uartaddr), ioaddr);
3513 outb(0, (ioaddr + EREG_DATA));
3516 /*****************************************************************************/
3519 * Modem interrupt handler. The is called when the modem signal line
3520 * (DCD) has changed state. Leave most of the work to the off-level
3521 * processing routine.
3524 static void stl_cd1400mdmisr(struct stlpanel *panelp, int ioaddr)
3526 struct stlport *portp;
3527 unsigned int ioack;
3528 unsigned char misr;
3530 pr_debug("stl_cd1400mdmisr(panelp=%p)\n", panelp);
3532 ioack = inb(ioaddr + EREG_MDACK);
3533 if (((ioack & panelp->ackmask) != 0) ||
3534 ((ioack & ACK_TYPMASK) != ACK_TYPMDM)) {
3535 printk("STALLION: bad MODEM interrupt ack value=%x\n", ioack);
3536 return;
3538 portp = panelp->ports[(ioack >> 3)];
3540 outb((MISR + portp->uartaddr), ioaddr);
3541 misr = inb(ioaddr + EREG_DATA);
3542 if (misr & MISR_DCD) {
3543 stl_cd_change(portp);
3544 portp->stats.modem++;
3547 outb((EOSRR + portp->uartaddr), ioaddr);
3548 outb(0, (ioaddr + EREG_DATA));
3551 /*****************************************************************************/
3552 /* SC26198 HARDWARE FUNCTIONS */
3553 /*****************************************************************************/
3556 * These functions get/set/update the registers of the sc26198 UARTs.
3557 * Access to the sc26198 registers is via an address/data io port pair.
3558 * (Maybe should make this inline...)
3561 static int stl_sc26198getreg(struct stlport *portp, int regnr)
3563 outb((regnr | portp->uartaddr), (portp->ioaddr + XP_ADDR));
3564 return inb(portp->ioaddr + XP_DATA);
3567 static void stl_sc26198setreg(struct stlport *portp, int regnr, int value)
3569 outb((regnr | portp->uartaddr), (portp->ioaddr + XP_ADDR));
3570 outb(value, (portp->ioaddr + XP_DATA));
3573 static int stl_sc26198updatereg(struct stlport *portp, int regnr, int value)
3575 outb((regnr | portp->uartaddr), (portp->ioaddr + XP_ADDR));
3576 if (inb(portp->ioaddr + XP_DATA) != value) {
3577 outb(value, (portp->ioaddr + XP_DATA));
3578 return 1;
3580 return 0;
3583 /*****************************************************************************/
3586 * Functions to get and set the sc26198 global registers.
3589 static int stl_sc26198getglobreg(struct stlport *portp, int regnr)
3591 outb(regnr, (portp->ioaddr + XP_ADDR));
3592 return inb(portp->ioaddr + XP_DATA);
3595 #if 0
3596 static void stl_sc26198setglobreg(struct stlport *portp, int regnr, int value)
3598 outb(regnr, (portp->ioaddr + XP_ADDR));
3599 outb(value, (portp->ioaddr + XP_DATA));
3601 #endif
3603 /*****************************************************************************/
3606 * Inbitialize the UARTs in a panel. We don't care what sort of board
3607 * these ports are on - since the port io registers are almost
3608 * identical when dealing with ports.
3611 static int stl_sc26198panelinit(struct stlbrd *brdp, struct stlpanel *panelp)
3613 int chipmask, i;
3614 int nrchips, ioaddr;
3616 pr_debug("stl_sc26198panelinit(brdp=%p,panelp=%p)\n", brdp, panelp);
3618 BRDENABLE(panelp->brdnr, panelp->pagenr);
3621 * Check that each chip is present and started up OK.
3623 chipmask = 0;
3624 nrchips = (panelp->nrports + 4) / SC26198_PORTS;
3625 if (brdp->brdtype == BRD_ECHPCI)
3626 outb(panelp->pagenr, brdp->ioctrl);
3628 for (i = 0; i < nrchips; i++) {
3629 ioaddr = panelp->iobase + (i * 4);
3630 outb(SCCR, (ioaddr + XP_ADDR));
3631 outb(CR_RESETALL, (ioaddr + XP_DATA));
3632 outb(TSTR, (ioaddr + XP_ADDR));
3633 if (inb(ioaddr + XP_DATA) != 0) {
3634 printk("STALLION: sc26198 not responding, "
3635 "brd=%d panel=%d chip=%d\n",
3636 panelp->brdnr, panelp->panelnr, i);
3637 continue;
3639 chipmask |= (0x1 << i);
3640 outb(GCCR, (ioaddr + XP_ADDR));
3641 outb(GCCR_IVRTYPCHANACK, (ioaddr + XP_DATA));
3642 outb(WDTRCR, (ioaddr + XP_ADDR));
3643 outb(0xff, (ioaddr + XP_DATA));
3646 BRDDISABLE(panelp->brdnr);
3647 return chipmask;
3650 /*****************************************************************************/
3653 * Initialize hardware specific port registers.
3656 static void stl_sc26198portinit(struct stlbrd *brdp, struct stlpanel *panelp, struct stlport *portp)
3658 pr_debug("stl_sc26198portinit(brdp=%p,panelp=%p,portp=%p)\n", brdp,
3659 panelp, portp);
3661 if ((brdp == NULL) || (panelp == NULL) ||
3662 (portp == NULL))
3663 return;
3665 portp->ioaddr = panelp->iobase + ((portp->portnr < 8) ? 0 : 4);
3666 portp->uartaddr = (portp->portnr & 0x07) << 4;
3667 portp->pagenr = panelp->pagenr;
3668 portp->hwid = 0x1;
3670 BRDENABLE(portp->brdnr, portp->pagenr);
3671 stl_sc26198setreg(portp, IOPCR, IOPCR_SETSIGS);
3672 BRDDISABLE(portp->brdnr);
3675 /*****************************************************************************/
3678 * Set up the sc26198 registers for a port based on the termios port
3679 * settings.
3682 static void stl_sc26198setport(struct stlport *portp, struct ktermios *tiosp)
3684 struct stlbrd *brdp;
3685 unsigned long flags;
3686 unsigned int baudrate;
3687 unsigned char mr0, mr1, mr2, clk;
3688 unsigned char imron, imroff, iopr, ipr;
3690 mr0 = 0;
3691 mr1 = 0;
3692 mr2 = 0;
3693 clk = 0;
3694 iopr = 0;
3695 imron = 0;
3696 imroff = 0;
3698 brdp = stl_brds[portp->brdnr];
3699 if (brdp == NULL)
3700 return;
3703 * Set up the RX char ignore mask with those RX error types we
3704 * can ignore.
3706 portp->rxignoremsk = 0;
3707 if (tiosp->c_iflag & IGNPAR)
3708 portp->rxignoremsk |= (SR_RXPARITY | SR_RXFRAMING |
3709 SR_RXOVERRUN);
3710 if (tiosp->c_iflag & IGNBRK)
3711 portp->rxignoremsk |= SR_RXBREAK;
3713 portp->rxmarkmsk = SR_RXOVERRUN;
3714 if (tiosp->c_iflag & (INPCK | PARMRK))
3715 portp->rxmarkmsk |= (SR_RXPARITY | SR_RXFRAMING);
3716 if (tiosp->c_iflag & BRKINT)
3717 portp->rxmarkmsk |= SR_RXBREAK;
3720 * Go through the char size, parity and stop bits and set all the
3721 * option register appropriately.
3723 switch (tiosp->c_cflag & CSIZE) {
3724 case CS5:
3725 mr1 |= MR1_CS5;
3726 break;
3727 case CS6:
3728 mr1 |= MR1_CS6;
3729 break;
3730 case CS7:
3731 mr1 |= MR1_CS7;
3732 break;
3733 default:
3734 mr1 |= MR1_CS8;
3735 break;
3738 if (tiosp->c_cflag & CSTOPB)
3739 mr2 |= MR2_STOP2;
3740 else
3741 mr2 |= MR2_STOP1;
3743 if (tiosp->c_cflag & PARENB) {
3744 if (tiosp->c_cflag & PARODD)
3745 mr1 |= (MR1_PARENB | MR1_PARODD);
3746 else
3747 mr1 |= (MR1_PARENB | MR1_PAREVEN);
3748 } else
3749 mr1 |= MR1_PARNONE;
3751 mr1 |= MR1_ERRBLOCK;
3754 * Set the RX FIFO threshold at 8 chars. This gives a bit of breathing
3755 * space for hardware flow control and the like. This should be set to
3756 * VMIN.
3758 mr2 |= MR2_RXFIFOHALF;
3761 * Calculate the baud rate timers. For now we will just assume that
3762 * the input and output baud are the same. The sc26198 has a fixed
3763 * baud rate table, so only discrete baud rates possible.
3765 baudrate = tiosp->c_cflag & CBAUD;
3766 if (baudrate & CBAUDEX) {
3767 baudrate &= ~CBAUDEX;
3768 if ((baudrate < 1) || (baudrate > 4))
3769 tiosp->c_cflag &= ~CBAUDEX;
3770 else
3771 baudrate += 15;
3773 baudrate = stl_baudrates[baudrate];
3774 if ((tiosp->c_cflag & CBAUD) == B38400) {
3775 if ((portp->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
3776 baudrate = 57600;
3777 else if ((portp->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
3778 baudrate = 115200;
3779 else if ((portp->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
3780 baudrate = 230400;
3781 else if ((portp->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
3782 baudrate = 460800;
3783 else if ((portp->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST)
3784 baudrate = (portp->baud_base / portp->custom_divisor);
3786 if (baudrate > STL_SC26198MAXBAUD)
3787 baudrate = STL_SC26198MAXBAUD;
3789 if (baudrate > 0)
3790 for (clk = 0; clk < SC26198_NRBAUDS; clk++)
3791 if (baudrate <= sc26198_baudtable[clk])
3792 break;
3795 * Check what form of modem signaling is required and set it up.
3797 if (tiosp->c_cflag & CLOCAL) {
3798 portp->port.flags &= ~ASYNC_CHECK_CD;
3799 } else {
3800 iopr |= IOPR_DCDCOS;
3801 imron |= IR_IOPORT;
3802 portp->port.flags |= ASYNC_CHECK_CD;
3806 * Setup sc26198 enhanced modes if we can. In particular we want to
3807 * handle as much of the flow control as possible automatically. As
3808 * well as saving a few CPU cycles it will also greatly improve flow
3809 * control reliability.
3811 if (tiosp->c_iflag & IXON) {
3812 mr0 |= MR0_SWFTX | MR0_SWFT;
3813 imron |= IR_XONXOFF;
3814 } else
3815 imroff |= IR_XONXOFF;
3817 if (tiosp->c_iflag & IXOFF)
3818 mr0 |= MR0_SWFRX;
3820 if (tiosp->c_cflag & CRTSCTS) {
3821 mr2 |= MR2_AUTOCTS;
3822 mr1 |= MR1_AUTORTS;
3826 * All sc26198 register values calculated so go through and set
3827 * them all up.
3830 pr_debug("SETPORT: portnr=%d panelnr=%d brdnr=%d\n",
3831 portp->portnr, portp->panelnr, portp->brdnr);
3832 pr_debug(" mr0=%x mr1=%x mr2=%x clk=%x\n", mr0, mr1, mr2, clk);
3833 pr_debug(" iopr=%x imron=%x imroff=%x\n", iopr, imron, imroff);
3834 pr_debug(" schr1=%x schr2=%x schr3=%x schr4=%x\n",
3835 tiosp->c_cc[VSTART], tiosp->c_cc[VSTOP],
3836 tiosp->c_cc[VSTART], tiosp->c_cc[VSTOP]);
3838 spin_lock_irqsave(&brd_lock, flags);
3839 BRDENABLE(portp->brdnr, portp->pagenr);
3840 stl_sc26198setreg(portp, IMR, 0);
3841 stl_sc26198updatereg(portp, MR0, mr0);
3842 stl_sc26198updatereg(portp, MR1, mr1);
3843 stl_sc26198setreg(portp, SCCR, CR_RXERRBLOCK);
3844 stl_sc26198updatereg(portp, MR2, mr2);
3845 stl_sc26198updatereg(portp, IOPIOR,
3846 ((stl_sc26198getreg(portp, IOPIOR) & ~IPR_CHANGEMASK) | iopr));
3848 if (baudrate > 0) {
3849 stl_sc26198setreg(portp, TXCSR, clk);
3850 stl_sc26198setreg(portp, RXCSR, clk);
3853 stl_sc26198setreg(portp, XONCR, tiosp->c_cc[VSTART]);
3854 stl_sc26198setreg(portp, XOFFCR, tiosp->c_cc[VSTOP]);
3856 ipr = stl_sc26198getreg(portp, IPR);
3857 if (ipr & IPR_DCD)
3858 portp->sigs &= ~TIOCM_CD;
3859 else
3860 portp->sigs |= TIOCM_CD;
3862 portp->imr = (portp->imr & ~imroff) | imron;
3863 stl_sc26198setreg(portp, IMR, portp->imr);
3864 BRDDISABLE(portp->brdnr);
3865 spin_unlock_irqrestore(&brd_lock, flags);
3868 /*****************************************************************************/
3871 * Set the state of the DTR and RTS signals.
3874 static void stl_sc26198setsignals(struct stlport *portp, int dtr, int rts)
3876 unsigned char iopioron, iopioroff;
3877 unsigned long flags;
3879 pr_debug("stl_sc26198setsignals(portp=%p,dtr=%d,rts=%d)\n", portp,
3880 dtr, rts);
3882 iopioron = 0;
3883 iopioroff = 0;
3884 if (dtr == 0)
3885 iopioroff |= IPR_DTR;
3886 else if (dtr > 0)
3887 iopioron |= IPR_DTR;
3888 if (rts == 0)
3889 iopioroff |= IPR_RTS;
3890 else if (rts > 0)
3891 iopioron |= IPR_RTS;
3893 spin_lock_irqsave(&brd_lock, flags);
3894 BRDENABLE(portp->brdnr, portp->pagenr);
3895 stl_sc26198setreg(portp, IOPIOR,
3896 ((stl_sc26198getreg(portp, IOPIOR) & ~iopioroff) | iopioron));
3897 BRDDISABLE(portp->brdnr);
3898 spin_unlock_irqrestore(&brd_lock, flags);
3901 /*****************************************************************************/
3904 * Return the state of the signals.
3907 static int stl_sc26198getsignals(struct stlport *portp)
3909 unsigned char ipr;
3910 unsigned long flags;
3911 int sigs;
3913 pr_debug("stl_sc26198getsignals(portp=%p)\n", portp);
3915 spin_lock_irqsave(&brd_lock, flags);
3916 BRDENABLE(portp->brdnr, portp->pagenr);
3917 ipr = stl_sc26198getreg(portp, IPR);
3918 BRDDISABLE(portp->brdnr);
3919 spin_unlock_irqrestore(&brd_lock, flags);
3921 sigs = 0;
3922 sigs |= (ipr & IPR_DCD) ? 0 : TIOCM_CD;
3923 sigs |= (ipr & IPR_CTS) ? 0 : TIOCM_CTS;
3924 sigs |= (ipr & IPR_DTR) ? 0: TIOCM_DTR;
3925 sigs |= (ipr & IPR_RTS) ? 0: TIOCM_RTS;
3926 sigs |= TIOCM_DSR;
3927 return sigs;
3930 /*****************************************************************************/
3933 * Enable/Disable the Transmitter and/or Receiver.
3936 static void stl_sc26198enablerxtx(struct stlport *portp, int rx, int tx)
3938 unsigned char ccr;
3939 unsigned long flags;
3941 pr_debug("stl_sc26198enablerxtx(portp=%p,rx=%d,tx=%d)\n", portp, rx,tx);
3943 ccr = portp->crenable;
3944 if (tx == 0)
3945 ccr &= ~CR_TXENABLE;
3946 else if (tx > 0)
3947 ccr |= CR_TXENABLE;
3948 if (rx == 0)
3949 ccr &= ~CR_RXENABLE;
3950 else if (rx > 0)
3951 ccr |= CR_RXENABLE;
3953 spin_lock_irqsave(&brd_lock, flags);
3954 BRDENABLE(portp->brdnr, portp->pagenr);
3955 stl_sc26198setreg(portp, SCCR, ccr);
3956 BRDDISABLE(portp->brdnr);
3957 portp->crenable = ccr;
3958 spin_unlock_irqrestore(&brd_lock, flags);
3961 /*****************************************************************************/
3964 * Start/stop the Transmitter and/or Receiver.
3967 static void stl_sc26198startrxtx(struct stlport *portp, int rx, int tx)
3969 unsigned char imr;
3970 unsigned long flags;
3972 pr_debug("stl_sc26198startrxtx(portp=%p,rx=%d,tx=%d)\n", portp, rx, tx);
3974 imr = portp->imr;
3975 if (tx == 0)
3976 imr &= ~IR_TXRDY;
3977 else if (tx == 1)
3978 imr |= IR_TXRDY;
3979 if (rx == 0)
3980 imr &= ~(IR_RXRDY | IR_RXBREAK | IR_RXWATCHDOG);
3981 else if (rx > 0)
3982 imr |= IR_RXRDY | IR_RXBREAK | IR_RXWATCHDOG;
3984 spin_lock_irqsave(&brd_lock, flags);
3985 BRDENABLE(portp->brdnr, portp->pagenr);
3986 stl_sc26198setreg(portp, IMR, imr);
3987 BRDDISABLE(portp->brdnr);
3988 portp->imr = imr;
3989 if (tx > 0)
3990 set_bit(ASYI_TXBUSY, &portp->istate);
3991 spin_unlock_irqrestore(&brd_lock, flags);
3994 /*****************************************************************************/
3997 * Disable all interrupts from this port.
4000 static void stl_sc26198disableintrs(struct stlport *portp)
4002 unsigned long flags;
4004 pr_debug("stl_sc26198disableintrs(portp=%p)\n", portp);
4006 spin_lock_irqsave(&brd_lock, flags);
4007 BRDENABLE(portp->brdnr, portp->pagenr);
4008 portp->imr = 0;
4009 stl_sc26198setreg(portp, IMR, 0);
4010 BRDDISABLE(portp->brdnr);
4011 spin_unlock_irqrestore(&brd_lock, flags);
4014 /*****************************************************************************/
4016 static void stl_sc26198sendbreak(struct stlport *portp, int len)
4018 unsigned long flags;
4020 pr_debug("stl_sc26198sendbreak(portp=%p,len=%d)\n", portp, len);
4022 spin_lock_irqsave(&brd_lock, flags);
4023 BRDENABLE(portp->brdnr, portp->pagenr);
4024 if (len == 1) {
4025 stl_sc26198setreg(portp, SCCR, CR_TXSTARTBREAK);
4026 portp->stats.txbreaks++;
4027 } else
4028 stl_sc26198setreg(portp, SCCR, CR_TXSTOPBREAK);
4030 BRDDISABLE(portp->brdnr);
4031 spin_unlock_irqrestore(&brd_lock, flags);
4034 /*****************************************************************************/
4037 * Take flow control actions...
4040 static void stl_sc26198flowctrl(struct stlport *portp, int state)
4042 struct tty_struct *tty;
4043 unsigned long flags;
4044 unsigned char mr0;
4046 pr_debug("stl_sc26198flowctrl(portp=%p,state=%x)\n", portp, state);
4048 if (portp == NULL)
4049 return;
4050 tty = tty_port_tty_get(&portp->port);
4051 if (tty == NULL)
4052 return;
4054 spin_lock_irqsave(&brd_lock, flags);
4055 BRDENABLE(portp->brdnr, portp->pagenr);
4057 if (state) {
4058 if (tty->termios->c_iflag & IXOFF) {
4059 mr0 = stl_sc26198getreg(portp, MR0);
4060 stl_sc26198setreg(portp, MR0, (mr0 & ~MR0_SWFRXTX));
4061 stl_sc26198setreg(portp, SCCR, CR_TXSENDXON);
4062 mr0 |= MR0_SWFRX;
4063 portp->stats.rxxon++;
4064 stl_sc26198wait(portp);
4065 stl_sc26198setreg(portp, MR0, mr0);
4068 * Question: should we return RTS to what it was before? It may
4069 * have been set by an ioctl... Suppose not, since if you have
4070 * hardware flow control set then it is pretty silly to go and
4071 * set the RTS line by hand.
4073 if (tty->termios->c_cflag & CRTSCTS) {
4074 stl_sc26198setreg(portp, MR1,
4075 (stl_sc26198getreg(portp, MR1) | MR1_AUTORTS));
4076 stl_sc26198setreg(portp, IOPIOR,
4077 (stl_sc26198getreg(portp, IOPIOR) | IOPR_RTS));
4078 portp->stats.rxrtson++;
4080 } else {
4081 if (tty->termios->c_iflag & IXOFF) {
4082 mr0 = stl_sc26198getreg(portp, MR0);
4083 stl_sc26198setreg(portp, MR0, (mr0 & ~MR0_SWFRXTX));
4084 stl_sc26198setreg(portp, SCCR, CR_TXSENDXOFF);
4085 mr0 &= ~MR0_SWFRX;
4086 portp->stats.rxxoff++;
4087 stl_sc26198wait(portp);
4088 stl_sc26198setreg(portp, MR0, mr0);
4090 if (tty->termios->c_cflag & CRTSCTS) {
4091 stl_sc26198setreg(portp, MR1,
4092 (stl_sc26198getreg(portp, MR1) & ~MR1_AUTORTS));
4093 stl_sc26198setreg(portp, IOPIOR,
4094 (stl_sc26198getreg(portp, IOPIOR) & ~IOPR_RTS));
4095 portp->stats.rxrtsoff++;
4099 BRDDISABLE(portp->brdnr);
4100 spin_unlock_irqrestore(&brd_lock, flags);
4101 tty_kref_put(tty);
4104 /*****************************************************************************/
4107 * Send a flow control character.
4110 static void stl_sc26198sendflow(struct stlport *portp, int state)
4112 struct tty_struct *tty;
4113 unsigned long flags;
4114 unsigned char mr0;
4116 pr_debug("stl_sc26198sendflow(portp=%p,state=%x)\n", portp, state);
4118 if (portp == NULL)
4119 return;
4120 tty = tty_port_tty_get(&portp->port);
4121 if (tty == NULL)
4122 return;
4124 spin_lock_irqsave(&brd_lock, flags);
4125 BRDENABLE(portp->brdnr, portp->pagenr);
4126 if (state) {
4127 mr0 = stl_sc26198getreg(portp, MR0);
4128 stl_sc26198setreg(portp, MR0, (mr0 & ~MR0_SWFRXTX));
4129 stl_sc26198setreg(portp, SCCR, CR_TXSENDXON);
4130 mr0 |= MR0_SWFRX;
4131 portp->stats.rxxon++;
4132 stl_sc26198wait(portp);
4133 stl_sc26198setreg(portp, MR0, mr0);
4134 } else {
4135 mr0 = stl_sc26198getreg(portp, MR0);
4136 stl_sc26198setreg(portp, MR0, (mr0 & ~MR0_SWFRXTX));
4137 stl_sc26198setreg(portp, SCCR, CR_TXSENDXOFF);
4138 mr0 &= ~MR0_SWFRX;
4139 portp->stats.rxxoff++;
4140 stl_sc26198wait(portp);
4141 stl_sc26198setreg(portp, MR0, mr0);
4143 BRDDISABLE(portp->brdnr);
4144 spin_unlock_irqrestore(&brd_lock, flags);
4145 tty_kref_put(tty);
4148 /*****************************************************************************/
4150 static void stl_sc26198flush(struct stlport *portp)
4152 unsigned long flags;
4154 pr_debug("stl_sc26198flush(portp=%p)\n", portp);
4156 if (portp == NULL)
4157 return;
4159 spin_lock_irqsave(&brd_lock, flags);
4160 BRDENABLE(portp->brdnr, portp->pagenr);
4161 stl_sc26198setreg(portp, SCCR, CR_TXRESET);
4162 stl_sc26198setreg(portp, SCCR, portp->crenable);
4163 BRDDISABLE(portp->brdnr);
4164 portp->tx.tail = portp->tx.head;
4165 spin_unlock_irqrestore(&brd_lock, flags);
4168 /*****************************************************************************/
4171 * Return the current state of data flow on this port. This is only
4172 * really interresting when determining if data has fully completed
4173 * transmission or not... The sc26198 interrupt scheme cannot
4174 * determine when all data has actually drained, so we need to
4175 * check the port statusy register to be sure.
4178 static int stl_sc26198datastate(struct stlport *portp)
4180 unsigned long flags;
4181 unsigned char sr;
4183 pr_debug("stl_sc26198datastate(portp=%p)\n", portp);
4185 if (portp == NULL)
4186 return 0;
4187 if (test_bit(ASYI_TXBUSY, &portp->istate))
4188 return 1;
4190 spin_lock_irqsave(&brd_lock, flags);
4191 BRDENABLE(portp->brdnr, portp->pagenr);
4192 sr = stl_sc26198getreg(portp, SR);
4193 BRDDISABLE(portp->brdnr);
4194 spin_unlock_irqrestore(&brd_lock, flags);
4196 return (sr & SR_TXEMPTY) ? 0 : 1;
4199 /*****************************************************************************/
4202 * Delay for a small amount of time, to give the sc26198 a chance
4203 * to process a command...
4206 static void stl_sc26198wait(struct stlport *portp)
4208 int i;
4210 pr_debug("stl_sc26198wait(portp=%p)\n", portp);
4212 if (portp == NULL)
4213 return;
4215 for (i = 0; i < 20; i++)
4216 stl_sc26198getglobreg(portp, TSTR);
4219 /*****************************************************************************/
4222 * If we are TX flow controlled and in IXANY mode then we may
4223 * need to unflow control here. We gotta do this because of the
4224 * automatic flow control modes of the sc26198.
4227 static void stl_sc26198txunflow(struct stlport *portp, struct tty_struct *tty)
4229 unsigned char mr0;
4231 mr0 = stl_sc26198getreg(portp, MR0);
4232 stl_sc26198setreg(portp, MR0, (mr0 & ~MR0_SWFRXTX));
4233 stl_sc26198setreg(portp, SCCR, CR_HOSTXON);
4234 stl_sc26198wait(portp);
4235 stl_sc26198setreg(portp, MR0, mr0);
4236 clear_bit(ASYI_TXFLOWED, &portp->istate);
4239 /*****************************************************************************/
4242 * Interrupt service routine for sc26198 panels.
4245 static void stl_sc26198intr(struct stlpanel *panelp, unsigned int iobase)
4247 struct stlport *portp;
4248 unsigned int iack;
4250 spin_lock(&brd_lock);
4253 * Work around bug in sc26198 chip... Cannot have A6 address
4254 * line of UART high, else iack will be returned as 0.
4256 outb(0, (iobase + 1));
4258 iack = inb(iobase + XP_IACK);
4259 portp = panelp->ports[(iack & IVR_CHANMASK) + ((iobase & 0x4) << 1)];
4261 if (iack & IVR_RXDATA)
4262 stl_sc26198rxisr(portp, iack);
4263 else if (iack & IVR_TXDATA)
4264 stl_sc26198txisr(portp);
4265 else
4266 stl_sc26198otherisr(portp, iack);
4268 spin_unlock(&brd_lock);
4271 /*****************************************************************************/
4274 * Transmit interrupt handler. This has gotta be fast! Handling TX
4275 * chars is pretty simple, stuff as many as possible from the TX buffer
4276 * into the sc26198 FIFO.
4277 * In practice it is possible that interrupts are enabled but that the
4278 * port has been hung up. Need to handle not having any TX buffer here,
4279 * this is done by using the side effect that head and tail will also
4280 * be NULL if the buffer has been freed.
4283 static void stl_sc26198txisr(struct stlport *portp)
4285 struct tty_struct *tty;
4286 unsigned int ioaddr;
4287 unsigned char mr0;
4288 int len, stlen;
4289 char *head, *tail;
4291 pr_debug("stl_sc26198txisr(portp=%p)\n", portp);
4293 ioaddr = portp->ioaddr;
4294 head = portp->tx.head;
4295 tail = portp->tx.tail;
4296 len = (head >= tail) ? (head - tail) : (STL_TXBUFSIZE - (tail - head));
4297 if ((len == 0) || ((len < STL_TXBUFLOW) &&
4298 (test_bit(ASYI_TXLOW, &portp->istate) == 0))) {
4299 set_bit(ASYI_TXLOW, &portp->istate);
4300 tty = tty_port_tty_get(&portp->port);
4301 if (tty) {
4302 tty_wakeup(tty);
4303 tty_kref_put(tty);
4307 if (len == 0) {
4308 outb((MR0 | portp->uartaddr), (ioaddr + XP_ADDR));
4309 mr0 = inb(ioaddr + XP_DATA);
4310 if ((mr0 & MR0_TXMASK) == MR0_TXEMPTY) {
4311 portp->imr &= ~IR_TXRDY;
4312 outb((IMR | portp->uartaddr), (ioaddr + XP_ADDR));
4313 outb(portp->imr, (ioaddr + XP_DATA));
4314 clear_bit(ASYI_TXBUSY, &portp->istate);
4315 } else {
4316 mr0 |= ((mr0 & ~MR0_TXMASK) | MR0_TXEMPTY);
4317 outb(mr0, (ioaddr + XP_DATA));
4319 } else {
4320 len = min(len, SC26198_TXFIFOSIZE);
4321 portp->stats.txtotal += len;
4322 stlen = min_t(unsigned int, len,
4323 (portp->tx.buf + STL_TXBUFSIZE) - tail);
4324 outb(GTXFIFO, (ioaddr + XP_ADDR));
4325 outsb((ioaddr + XP_DATA), tail, stlen);
4326 len -= stlen;
4327 tail += stlen;
4328 if (tail >= (portp->tx.buf + STL_TXBUFSIZE))
4329 tail = portp->tx.buf;
4330 if (len > 0) {
4331 outsb((ioaddr + XP_DATA), tail, len);
4332 tail += len;
4334 portp->tx.tail = tail;
4338 /*****************************************************************************/
4341 * Receive character interrupt handler. Determine if we have good chars
4342 * or bad chars and then process appropriately. Good chars are easy
4343 * just shove the lot into the RX buffer and set all status byte to 0.
4344 * If a bad RX char then process as required. This routine needs to be
4345 * fast! In practice it is possible that we get an interrupt on a port
4346 * that is closed. This can happen on hangups - since they completely
4347 * shutdown a port not in user context. Need to handle this case.
4350 static void stl_sc26198rxisr(struct stlport *portp, unsigned int iack)
4352 struct tty_struct *tty;
4353 unsigned int len, buflen, ioaddr;
4355 pr_debug("stl_sc26198rxisr(portp=%p,iack=%x)\n", portp, iack);
4357 tty = tty_port_tty_get(&portp->port);
4358 ioaddr = portp->ioaddr;
4359 outb(GIBCR, (ioaddr + XP_ADDR));
4360 len = inb(ioaddr + XP_DATA) + 1;
4362 if ((iack & IVR_TYPEMASK) == IVR_RXDATA) {
4363 if (tty == NULL || (buflen = tty_buffer_request_room(tty, len)) == 0) {
4364 len = min_t(unsigned int, len, sizeof(stl_unwanted));
4365 outb(GRXFIFO, (ioaddr + XP_ADDR));
4366 insb((ioaddr + XP_DATA), &stl_unwanted[0], len);
4367 portp->stats.rxlost += len;
4368 portp->stats.rxtotal += len;
4369 } else {
4370 len = min(len, buflen);
4371 if (len > 0) {
4372 unsigned char *ptr;
4373 outb(GRXFIFO, (ioaddr + XP_ADDR));
4374 tty_prepare_flip_string(tty, &ptr, len);
4375 insb((ioaddr + XP_DATA), ptr, len);
4376 tty_schedule_flip(tty);
4377 portp->stats.rxtotal += len;
4380 } else {
4381 stl_sc26198rxbadchars(portp);
4385 * If we are TX flow controlled and in IXANY mode then we may need
4386 * to unflow control here. We gotta do this because of the automatic
4387 * flow control modes of the sc26198.
4389 if (test_bit(ASYI_TXFLOWED, &portp->istate)) {
4390 if ((tty != NULL) &&
4391 (tty->termios != NULL) &&
4392 (tty->termios->c_iflag & IXANY)) {
4393 stl_sc26198txunflow(portp, tty);
4396 tty_kref_put(tty);
4399 /*****************************************************************************/
4402 * Process an RX bad character.
4405 static void stl_sc26198rxbadch(struct stlport *portp, unsigned char status, char ch)
4407 struct tty_struct *tty;
4408 unsigned int ioaddr;
4410 tty = tty_port_tty_get(&portp->port);
4411 ioaddr = portp->ioaddr;
4413 if (status & SR_RXPARITY)
4414 portp->stats.rxparity++;
4415 if (status & SR_RXFRAMING)
4416 portp->stats.rxframing++;
4417 if (status & SR_RXOVERRUN)
4418 portp->stats.rxoverrun++;
4419 if (status & SR_RXBREAK)
4420 portp->stats.rxbreaks++;
4422 if ((tty != NULL) &&
4423 ((portp->rxignoremsk & status) == 0)) {
4424 if (portp->rxmarkmsk & status) {
4425 if (status & SR_RXBREAK) {
4426 status = TTY_BREAK;
4427 if (portp->port.flags & ASYNC_SAK) {
4428 do_SAK(tty);
4429 BRDENABLE(portp->brdnr, portp->pagenr);
4431 } else if (status & SR_RXPARITY)
4432 status = TTY_PARITY;
4433 else if (status & SR_RXFRAMING)
4434 status = TTY_FRAME;
4435 else if(status & SR_RXOVERRUN)
4436 status = TTY_OVERRUN;
4437 else
4438 status = 0;
4439 } else
4440 status = 0;
4442 tty_insert_flip_char(tty, ch, status);
4443 tty_schedule_flip(tty);
4445 if (status == 0)
4446 portp->stats.rxtotal++;
4448 tty_kref_put(tty);
4451 /*****************************************************************************/
4454 * Process all characters in the RX FIFO of the UART. Check all char
4455 * status bytes as well, and process as required. We need to check
4456 * all bytes in the FIFO, in case some more enter the FIFO while we
4457 * are here. To get the exact character error type we need to switch
4458 * into CHAR error mode (that is why we need to make sure we empty
4459 * the FIFO).
4462 static void stl_sc26198rxbadchars(struct stlport *portp)
4464 unsigned char status, mr1;
4465 char ch;
4468 * To get the precise error type for each character we must switch
4469 * back into CHAR error mode.
4471 mr1 = stl_sc26198getreg(portp, MR1);
4472 stl_sc26198setreg(portp, MR1, (mr1 & ~MR1_ERRBLOCK));
4474 while ((status = stl_sc26198getreg(portp, SR)) & SR_RXRDY) {
4475 stl_sc26198setreg(portp, SCCR, CR_CLEARRXERR);
4476 ch = stl_sc26198getreg(portp, RXFIFO);
4477 stl_sc26198rxbadch(portp, status, ch);
4481 * To get correct interrupt class we must switch back into BLOCK
4482 * error mode.
4484 stl_sc26198setreg(portp, MR1, mr1);
4487 /*****************************************************************************/
4490 * Other interrupt handler. This includes modem signals, flow
4491 * control actions, etc. Most stuff is left to off-level interrupt
4492 * processing time.
4495 static void stl_sc26198otherisr(struct stlport *portp, unsigned int iack)
4497 unsigned char cir, ipr, xisr;
4499 pr_debug("stl_sc26198otherisr(portp=%p,iack=%x)\n", portp, iack);
4501 cir = stl_sc26198getglobreg(portp, CIR);
4503 switch (cir & CIR_SUBTYPEMASK) {
4504 case CIR_SUBCOS:
4505 ipr = stl_sc26198getreg(portp, IPR);
4506 if (ipr & IPR_DCDCHANGE) {
4507 stl_cd_change(portp);
4508 portp->stats.modem++;
4510 break;
4511 case CIR_SUBXONXOFF:
4512 xisr = stl_sc26198getreg(portp, XISR);
4513 if (xisr & XISR_RXXONGOT) {
4514 set_bit(ASYI_TXFLOWED, &portp->istate);
4515 portp->stats.txxoff++;
4517 if (xisr & XISR_RXXOFFGOT) {
4518 clear_bit(ASYI_TXFLOWED, &portp->istate);
4519 portp->stats.txxon++;
4521 break;
4522 case CIR_SUBBREAK:
4523 stl_sc26198setreg(portp, SCCR, CR_BREAKRESET);
4524 stl_sc26198rxbadchars(portp);
4525 break;
4526 default:
4527 break;
4531 static void stl_free_isabrds(void)
4533 struct stlbrd *brdp;
4534 unsigned int i;
4536 for (i = 0; i < stl_nrbrds; i++) {
4537 if ((brdp = stl_brds[i]) == NULL || (brdp->state & STL_PROBED))
4538 continue;
4540 free_irq(brdp->irq, brdp);
4542 stl_cleanup_panels(brdp);
4544 release_region(brdp->ioaddr1, brdp->iosize1);
4545 if (brdp->iosize2 > 0)
4546 release_region(brdp->ioaddr2, brdp->iosize2);
4548 kfree(brdp);
4549 stl_brds[i] = NULL;
4554 * Loadable module initialization stuff.
4556 static int __init stallion_module_init(void)
4558 struct stlbrd *brdp;
4559 struct stlconf conf;
4560 unsigned int i, j;
4561 int retval;
4563 printk(KERN_INFO "%s: version %s\n", stl_drvtitle, stl_drvversion);
4565 spin_lock_init(&stallion_lock);
4566 spin_lock_init(&brd_lock);
4568 stl_serial = alloc_tty_driver(STL_MAXBRDS * STL_MAXPORTS);
4569 if (!stl_serial) {
4570 retval = -ENOMEM;
4571 goto err;
4574 stl_serial->owner = THIS_MODULE;
4575 stl_serial->driver_name = stl_drvname;
4576 stl_serial->name = "ttyE";
4577 stl_serial->major = STL_SERIALMAJOR;
4578 stl_serial->minor_start = 0;
4579 stl_serial->type = TTY_DRIVER_TYPE_SERIAL;
4580 stl_serial->subtype = SERIAL_TYPE_NORMAL;
4581 stl_serial->init_termios = stl_deftermios;
4582 stl_serial->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
4583 tty_set_operations(stl_serial, &stl_ops);
4585 retval = tty_register_driver(stl_serial);
4586 if (retval) {
4587 printk("STALLION: failed to register serial driver\n");
4588 goto err_frtty;
4592 * Find any dynamically supported boards. That is via module load
4593 * line options.
4595 for (i = stl_nrbrds; i < stl_nargs; i++) {
4596 memset(&conf, 0, sizeof(conf));
4597 if (stl_parsebrd(&conf, stl_brdsp[i]) == 0)
4598 continue;
4599 if ((brdp = stl_allocbrd()) == NULL)
4600 continue;
4601 brdp->brdnr = i;
4602 brdp->brdtype = conf.brdtype;
4603 brdp->ioaddr1 = conf.ioaddr1;
4604 brdp->ioaddr2 = conf.ioaddr2;
4605 brdp->irq = conf.irq;
4606 brdp->irqtype = conf.irqtype;
4607 stl_brds[brdp->brdnr] = brdp;
4608 if (stl_brdinit(brdp)) {
4609 stl_brds[brdp->brdnr] = NULL;
4610 kfree(brdp);
4611 } else {
4612 for (j = 0; j < brdp->nrports; j++)
4613 tty_register_device(stl_serial,
4614 brdp->brdnr * STL_MAXPORTS + j, NULL);
4615 stl_nrbrds = i + 1;
4619 /* this has to be _after_ isa finding because of locking */
4620 retval = pci_register_driver(&stl_pcidriver);
4621 if (retval && stl_nrbrds == 0) {
4622 printk(KERN_ERR "STALLION: can't register pci driver\n");
4623 goto err_unrtty;
4627 * Set up a character driver for per board stuff. This is mainly used
4628 * to do stats ioctls on the ports.
4630 if (register_chrdev(STL_SIOMEMMAJOR, "staliomem", &stl_fsiomem))
4631 printk("STALLION: failed to register serial board device\n");
4633 stallion_class = class_create(THIS_MODULE, "staliomem");
4634 if (IS_ERR(stallion_class))
4635 printk("STALLION: failed to create class\n");
4636 for (i = 0; i < 4; i++)
4637 device_create(stallion_class, NULL, MKDEV(STL_SIOMEMMAJOR, i),
4638 NULL, "staliomem%d", i);
4640 return 0;
4641 err_unrtty:
4642 tty_unregister_driver(stl_serial);
4643 err_frtty:
4644 put_tty_driver(stl_serial);
4645 err:
4646 return retval;
4649 static void __exit stallion_module_exit(void)
4651 struct stlbrd *brdp;
4652 unsigned int i, j;
4654 pr_debug("cleanup_module()\n");
4656 printk(KERN_INFO "Unloading %s: version %s\n", stl_drvtitle,
4657 stl_drvversion);
4660 * Free up all allocated resources used by the ports. This includes
4661 * memory and interrupts. As part of this process we will also do
4662 * a hangup on every open port - to try to flush out any processes
4663 * hanging onto ports.
4665 for (i = 0; i < stl_nrbrds; i++) {
4666 if ((brdp = stl_brds[i]) == NULL || (brdp->state & STL_PROBED))
4667 continue;
4668 for (j = 0; j < brdp->nrports; j++)
4669 tty_unregister_device(stl_serial,
4670 brdp->brdnr * STL_MAXPORTS + j);
4673 for (i = 0; i < 4; i++)
4674 device_destroy(stallion_class, MKDEV(STL_SIOMEMMAJOR, i));
4675 unregister_chrdev(STL_SIOMEMMAJOR, "staliomem");
4676 class_destroy(stallion_class);
4678 pci_unregister_driver(&stl_pcidriver);
4680 stl_free_isabrds();
4682 tty_unregister_driver(stl_serial);
4683 put_tty_driver(stl_serial);
4686 module_init(stallion_module_init);
4687 module_exit(stallion_module_exit);
4689 MODULE_AUTHOR("Greg Ungerer");
4690 MODULE_DESCRIPTION("Stallion Multiport Serial Driver");
4691 MODULE_LICENSE("GPL");