Portability cleanup as required by Linus.
[linux-2.6/linux-mips.git] / drivers / char / mxser.c
blobfbbec0fb18505e2ad19199ad43ef6cb6d623b14d
1 /*****************************************************************************/
2 /*
3 * mxser.c -- MOXA Smartio family multiport serial driver.
5 * Copyright (C) 1999-2000 Moxa Technologies (support@moxa.com.tw).
7 * This code is loosely based on the Linux serial driver, written by
8 * Linus Torvalds, Theodore T'so and others.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 * MOXA Smartio Family Serial Driver
28 * Copyright (C) 1999,2000 Moxa Technologies Co., LTD.
30 * for : LINUX 2.0.X, 2.2.X
31 * date : 1999/07/22
32 * version : 1.1
36 #include <linux/config.h>
37 #include <linux/module.h>
38 #include <linux/version.h>
39 #include <linux/errno.h>
40 #include <linux/signal.h>
41 #include <linux/sched.h>
42 #include <linux/timer.h>
43 #include <linux/interrupt.h>
44 #include <linux/tty.h>
45 #include <linux/tty_flip.h>
46 #include <linux/serial.h>
47 #include <linux/serial_reg.h>
48 #include <linux/major.h>
49 #include <linux/string.h>
50 #include <linux/fcntl.h>
51 #include <linux/ptrace.h>
52 #include <linux/ioport.h>
53 #include <linux/mm.h>
54 #include <linux/smp_lock.h>
55 #include <linux/pci.h>
57 #include <asm/system.h>
58 #include <asm/io.h>
59 #include <asm/irq.h>
60 #include <asm/segment.h>
61 #include <asm/bitops.h>
62 #include <asm/uaccess.h>
64 #define MXSER_VERSION "1.1kern"
66 #define MXSERMAJOR 174
67 #define MXSERCUMAJOR 175
70 #define MXSER_EVENT_TXLOW 1
71 #define MXSER_EVENT_HANGUP 2
74 #define SERIAL_DO_RESTART
76 #define MXSER_BOARDS 4 /* Max. boards */
77 #define MXSER_PORTS 32 /* Max. ports */
78 #define MXSER_PORTS_PER_BOARD 8 /* Max. ports per board */
79 #define MXSER_ISR_PASS_LIMIT 256
81 #define MXSER_ERR_IOADDR -1
82 #define MXSER_ERR_IRQ -2
83 #define MXSER_ERR_IRQ_CONFLIT -3
84 #define MXSER_ERR_VECTOR -4
86 #define SERIAL_TYPE_NORMAL 1
87 #define SERIAL_TYPE_CALLOUT 2
89 #define WAKEUP_CHARS 256
91 #define UART_MCR_AFE 0x20
92 #define UART_LSR_SPECIAL 0x1E
94 #define PORTNO(x) (MINOR((x)->device) - (x)->driver.minor_start)
96 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
98 #define IRQ_T(info) ((info->flags & ASYNC_SHARE_IRQ) ? SA_SHIRQ : SA_INTERRUPT)
100 #ifndef MIN
101 #define MIN(a,b) ((a) < (b) ? (a) : (b))
102 #endif
105 * Define the Moxa PCI vendor and device IDs.
108 #ifndef PCI_VENDOR_ID_MOXA
109 #define PCI_VENDOR_ID_MOXA 0x1393
110 #endif
111 #ifndef PCI_DEVICE_ID_C168
112 #define PCI_DEVICE_ID_C168 0x1680
113 #endif
114 #ifndef PCI_DEVICE_ID_C104
115 #define PCI_DEVICE_ID_C104 0x1040
116 #endif
118 #define C168_ASIC_ID 1
119 #define C104_ASIC_ID 2
120 #define CI104J_ASIC_ID 5
122 enum {
123 MXSER_BOARD_C168_ISA = 1,
124 MXSER_BOARD_C104_ISA,
125 MXSER_BOARD_CI104J,
126 MXSER_BOARD_C168_PCI,
127 MXSER_BOARD_C104_PCI,
130 static char *mxser_brdname[] =
132 "C168 series",
133 "C104 series",
134 "CI-104J series",
135 "C168H/PCI series",
136 "C104H/PCI series",
139 static int mxser_numports[] =
149 * MOXA ioctls
151 #define MOXA 0x400
152 #define MOXA_GETDATACOUNT (MOXA + 23)
153 #define MOXA_GET_CONF (MOXA + 35)
154 #define MOXA_DIAGNOSE (MOXA + 50)
155 #define MOXA_CHKPORTENABLE (MOXA + 60)
156 #define MOXA_HighSpeedOn (MOXA + 61)
157 #define MOXA_GET_MAJOR (MOXA + 63)
158 #define MOXA_GET_CUMAJOR (MOXA + 64)
159 #define MOXA_GETMSTATUS (MOXA + 65)
161 typedef struct {
162 unsigned short vendor_id;
163 unsigned short device_id;
164 unsigned short board_type;
165 } mxser_pciinfo;
167 static mxser_pciinfo mxser_pcibrds[] =
169 {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_C168, MXSER_BOARD_C168_PCI},
170 {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_C104, MXSER_BOARD_C104_PCI},
173 static int ioaddr[MXSER_BOARDS] = {0, 0, 0, 0};
174 static int ttymajor = MXSERMAJOR;
175 static int calloutmajor = MXSERCUMAJOR;
176 static int verbose = 0;
178 /* Variables for insmod */
180 MODULE_AUTHOR("William Chen");
181 MODULE_DESCRIPTION("MOXA Smartio Family Multiport Board Device Driver");
182 MODULE_PARM(ioaddr, "1-4i");
183 MODULE_PARM(ttymajor, "i");
184 MODULE_PARM(calloutmajor, "i");
185 MODULE_PARM(verbose, "i");
187 struct mxser_hwconf {
188 int board_type;
189 int ports;
190 int irq;
191 int vector;
192 int vector_mask;
193 int uart_type;
194 int ioaddr[MXSER_PORTS_PER_BOARD];
195 int baud_base[MXSER_PORTS_PER_BOARD];
196 struct pci_dev *pdev;
199 struct mxser_struct {
200 int port;
201 int base; /* port base address */
202 int irq; /* port using irq no. */
203 int vector; /* port irq vector */
204 int vectormask; /* port vector mask */
205 int rx_trigger; /* Rx fifo trigger level */
206 int baud_base; /* max. speed */
207 int flags; /* defined in tty.h */
208 int type; /* UART type */
209 struct tty_struct *tty;
210 int read_status_mask;
211 int ignore_status_mask;
212 int xmit_fifo_size;
213 int custom_divisor;
214 int x_char; /* xon/xoff character */
215 int close_delay;
216 unsigned short closing_wait;
217 int IER; /* Interrupt Enable Register */
218 int MCR; /* Modem control register */
219 unsigned long event;
220 int count; /* # of fd on device */
221 int blocked_open; /* # of blocked opens */
222 long session; /* Session of opening process */
223 long pgrp; /* pgrp of opening process */
224 unsigned char *xmit_buf;
225 int xmit_head;
226 int xmit_tail;
227 int xmit_cnt;
228 struct tq_struct tqueue;
229 struct termios normal_termios;
230 struct termios callout_termios;
231 wait_queue_head_t open_wait;
232 wait_queue_head_t close_wait;
233 wait_queue_head_t delta_msr_wait;
234 struct async_icount icount; /* kernel counters for the 4 input interrupts */
237 struct mxser_log {
238 int tick;
239 int rxcnt[MXSER_PORTS];
240 int txcnt[MXSER_PORTS];
243 struct mxser_mstatus {
244 tcflag_t cflag;
245 int cts;
246 int dsr;
247 int ri;
248 int dcd;
251 static struct mxser_mstatus GMStatus[MXSER_PORTS];
253 static int mxserBoardCAP[MXSER_BOARDS] =
255 0, 0, 0, 0
256 /* 0x180, 0x280, 0x200, 0x320 */
260 static struct tty_driver mxvar_sdriver, mxvar_cdriver;
261 static int mxvar_refcount;
262 static struct mxser_struct mxvar_table[MXSER_PORTS];
263 static struct tty_struct *mxvar_tty[MXSER_PORTS + 1];
264 static struct termios *mxvar_termios[MXSER_PORTS + 1];
265 static struct termios *mxvar_termios_locked[MXSER_PORTS + 1];
266 static struct mxser_log mxvar_log;
267 static int mxvar_diagflag;
269 * mxvar_tmp_buf is used as a temporary buffer by serial_write. We need
270 * to lock it in case the memcpy_fromfs blocks while swapping in a page,
271 * and some other program tries to do a serial write at the same time.
272 * Since the lock will only come under contention when the system is
273 * swapping and available memory is low, it makes sense to share one
274 * buffer across all the serial ports, since it significantly saves
275 * memory if large numbers of serial ports are open.
277 static unsigned char *mxvar_tmp_buf = 0;
278 static struct semaphore mxvar_tmp_buf_sem;
281 * This is used to figure out the divisor speeds and the timeouts
283 static int mxvar_baud_table[] =
285 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
286 9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600, 0};
288 struct mxser_hwconf mxsercfg[MXSER_BOARDS];
291 * static functions:
294 #ifdef MODULE
295 int init_module(void);
296 void cleanup_module(void);
297 #endif
299 static void mxser_getcfg(int board, struct mxser_hwconf *hwconf);
300 int mxser_init(void);
301 static int mxser_get_ISA_conf(int, struct mxser_hwconf *);
302 static int mxser_get_PCI_conf(struct pci_dev *, int, struct mxser_hwconf *);
303 static void mxser_do_softint(void *);
304 static int mxser_open(struct tty_struct *, struct file *);
305 static void mxser_close(struct tty_struct *, struct file *);
306 static int mxser_write(struct tty_struct *, int, const unsigned char *, int);
307 static int mxser_write_room(struct tty_struct *);
308 static void mxser_flush_buffer(struct tty_struct *);
309 static int mxser_chars_in_buffer(struct tty_struct *);
310 static void mxser_flush_chars(struct tty_struct *);
311 static void mxser_put_char(struct tty_struct *, unsigned char);
312 static int mxser_ioctl(struct tty_struct *, struct file *, uint, ulong);
313 static int mxser_ioctl_special(unsigned int, unsigned long);
314 static void mxser_throttle(struct tty_struct *);
315 static void mxser_unthrottle(struct tty_struct *);
316 static void mxser_set_termios(struct tty_struct *, struct termios *);
317 static void mxser_stop(struct tty_struct *);
318 static void mxser_start(struct tty_struct *);
319 static void mxser_hangup(struct tty_struct *);
320 static void mxser_interrupt(int, void *, struct pt_regs *);
321 static inline void mxser_receive_chars(struct mxser_struct *, int *);
322 static inline void mxser_transmit_chars(struct mxser_struct *);
323 static inline void mxser_check_modem_status(struct mxser_struct *, int);
324 static int mxser_block_til_ready(struct tty_struct *, struct file *, struct mxser_struct *);
325 static int mxser_startup(struct mxser_struct *);
326 static void mxser_shutdown(struct mxser_struct *);
327 static int mxser_change_speed(struct mxser_struct *, struct termios *old_termios);
328 static int mxser_get_serial_info(struct mxser_struct *, struct serial_struct *);
329 static int mxser_set_serial_info(struct mxser_struct *, struct serial_struct *);
330 static int mxser_get_lsr_info(struct mxser_struct *, unsigned int *);
331 static void mxser_send_break(struct mxser_struct *, int);
332 static int mxser_get_modem_info(struct mxser_struct *, unsigned int *);
333 static int mxser_set_modem_info(struct mxser_struct *, unsigned int, unsigned int *);
336 * The MOXA C168/C104 serial driver boot-time initialization code!
340 #ifdef MODULE
341 int init_module(void)
343 int ret;
345 if (verbose)
346 printk("Loading module mxser ...\n");
347 ret = mxser_init();
348 if (verbose)
349 printk("Done.\n");
350 return (ret);
353 void cleanup_module(void)
355 int i, err = 0;
358 if (verbose)
359 printk("Unloading module mxser ...\n");
360 if ((err |= tty_unregister_driver(&mxvar_cdriver)))
361 printk("Couldn't unregister MOXA Smartio family callout driver\n");
362 if ((err |= tty_unregister_driver(&mxvar_sdriver)))
363 printk("Couldn't unregister MOXA Smartio family serial driver\n");
365 for (i = 0; i < MXSER_BOARDS; i++) {
366 if (mxsercfg[i].board_type == -1)
367 continue;
368 else {
369 free_irq(mxsercfg[i].irq, &mxvar_table[i * MXSER_PORTS_PER_BOARD]);
373 if (verbose)
374 printk("Done.\n");
377 #endif
380 int mxser_initbrd(int board, struct mxser_hwconf *hwconf)
382 struct mxser_struct *info;
383 unsigned long flags;
384 int retval;
385 int i, n;
387 init_MUTEX(&mxvar_tmp_buf_sem);
389 n = board * MXSER_PORTS_PER_BOARD;
390 info = &mxvar_table[n];
391 for (i = 0; i < hwconf->ports; i++, n++, info++) {
392 if (verbose) {
393 printk(" ttyM%d/cum%d at 0x%04x ", n, n, hwconf->ioaddr[i]);
394 if (hwconf->baud_base[i] == 115200)
395 printk(" max. baud rate up to 115200 bps.\n");
396 else
397 printk(" max. baud rate up to 921600 bps.\n");
399 info->port = n;
400 info->base = hwconf->ioaddr[i];
401 info->irq = hwconf->irq;
402 info->vector = hwconf->vector;
403 info->vectormask = hwconf->vector_mask;
404 info->rx_trigger = 14;
405 info->baud_base = hwconf->baud_base[i];
406 info->flags = ASYNC_SHARE_IRQ;
407 info->type = hwconf->uart_type;
408 if ((info->type == PORT_16450) || (info->type == PORT_8250))
409 info->xmit_fifo_size = 1;
410 else
411 info->xmit_fifo_size = 16;
412 info->custom_divisor = hwconf->baud_base[i] * 16;
413 info->close_delay = 5 * HZ / 10;
414 info->closing_wait = 30 * HZ;
415 info->tqueue.routine = mxser_do_softint;
416 info->tqueue.data = info;
417 info->callout_termios = mxvar_cdriver.init_termios;
418 info->normal_termios = mxvar_sdriver.init_termios;
419 init_waitqueue_head(&info->open_wait);
420 init_waitqueue_head(&info->close_wait);
421 init_waitqueue_head(&info->delta_msr_wait);
425 * Allocate the IRQ if necessary
427 save_flags(flags);
429 n = board * MXSER_PORTS_PER_BOARD;
430 info = &mxvar_table[n];
432 cli();
433 retval = request_irq(hwconf->irq, mxser_interrupt, IRQ_T(info),
434 "mxser", info);
435 if (retval) {
436 restore_flags(flags);
437 printk("Board %d: %s", board, mxser_brdname[hwconf->board_type - 1]);
438 printk(" Request irq fail,IRQ (%d) may be conflit with another device.\n", info->irq);
439 return (retval);
441 restore_flags(flags);
443 return 0;
447 static void mxser_getcfg(int board, struct mxser_hwconf *hwconf)
449 mxsercfg[board] = *hwconf;
452 static int mxser_get_PCI_conf(struct pci_dev *pdev, int board_type, struct mxser_hwconf *hwconf)
454 int i;
455 unsigned int ioaddress;
457 hwconf->board_type = board_type;
458 hwconf->ports = mxser_numports[board_type - 1];
459 ioaddress = pci_resource_start (pdev, 2);
460 for (i = 0; i < hwconf->ports; i++)
461 hwconf->ioaddr[i] = ioaddress + 8 * i;
463 ioaddress = pci_resource_start (pdev, 3);
464 hwconf->vector = ioaddress;
466 hwconf->irq = pdev->irq;
468 hwconf->uart_type = PORT_16550A;
469 hwconf->vector_mask = 0;
470 for (i = 0; i < hwconf->ports; i++) {
471 hwconf->vector_mask |= (1 << i);
472 hwconf->baud_base[i] = 921600;
474 return (0);
477 int mxser_init(void)
479 int i, m, retval, b;
480 int n, index;
481 int ret1, ret2;
482 struct mxser_hwconf hwconf;
484 printk("MOXA Smartio family driver version %s\n", MXSER_VERSION);
486 /* Initialize the tty_driver structure */
488 memset(&mxvar_sdriver, 0, sizeof(struct tty_driver));
489 mxvar_sdriver.magic = TTY_DRIVER_MAGIC;
490 mxvar_sdriver.name = "ttyM";
491 mxvar_sdriver.major = ttymajor;
492 mxvar_sdriver.minor_start = 0;
493 mxvar_sdriver.num = MXSER_PORTS + 1;
494 mxvar_sdriver.type = TTY_DRIVER_TYPE_SERIAL;
495 mxvar_sdriver.subtype = SERIAL_TYPE_NORMAL;
496 mxvar_sdriver.init_termios = tty_std_termios;
497 mxvar_sdriver.init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
498 mxvar_sdriver.flags = TTY_DRIVER_REAL_RAW;
499 mxvar_sdriver.refcount = &mxvar_refcount;
500 mxvar_sdriver.table = mxvar_tty;
501 mxvar_sdriver.termios = mxvar_termios;
502 mxvar_sdriver.termios_locked = mxvar_termios_locked;
504 mxvar_sdriver.open = mxser_open;
505 mxvar_sdriver.close = mxser_close;
506 mxvar_sdriver.write = mxser_write;
507 mxvar_sdriver.put_char = mxser_put_char;
508 mxvar_sdriver.flush_chars = mxser_flush_chars;
509 mxvar_sdriver.write_room = mxser_write_room;
510 mxvar_sdriver.chars_in_buffer = mxser_chars_in_buffer;
511 mxvar_sdriver.flush_buffer = mxser_flush_buffer;
512 mxvar_sdriver.ioctl = mxser_ioctl;
513 mxvar_sdriver.throttle = mxser_throttle;
514 mxvar_sdriver.unthrottle = mxser_unthrottle;
515 mxvar_sdriver.set_termios = mxser_set_termios;
516 mxvar_sdriver.stop = mxser_stop;
517 mxvar_sdriver.start = mxser_start;
518 mxvar_sdriver.hangup = mxser_hangup;
521 * The callout device is just like normal device except for
522 * major number and the subtype code.
524 mxvar_cdriver = mxvar_sdriver;
525 mxvar_cdriver.name = "cum";
526 mxvar_cdriver.major = calloutmajor;
527 mxvar_cdriver.subtype = SERIAL_TYPE_CALLOUT;
529 printk("Tty devices major number = %d, callout devices major number = %d\n", ttymajor, calloutmajor);
531 mxvar_diagflag = 0;
532 memset(mxvar_table, 0, MXSER_PORTS * sizeof(struct mxser_struct));
533 memset(&mxvar_log, 0, sizeof(struct mxser_log));
536 m = 0;
537 /* Start finding ISA boards here */
538 for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
539 int cap;
540 if (!(cap = mxserBoardCAP[b]))
541 continue;
543 retval = mxser_get_ISA_conf(cap, &hwconf);
545 if (retval != 0)
546 printk("Found MOXA %s board (CAP=0x%x)\n",
547 mxser_brdname[hwconf.board_type - 1],
548 ioaddr[b]);
550 if (retval <= 0) {
551 if (retval == MXSER_ERR_IRQ)
552 printk("Invalid interrupt number,board not configured\n");
553 else if (retval == MXSER_ERR_IRQ_CONFLIT)
554 printk("Invalid interrupt number,board not configured\n");
555 else if (retval == MXSER_ERR_VECTOR)
556 printk("Invalid interrupt vector,board not configured\n");
557 else if (retval == MXSER_ERR_IOADDR)
558 printk("Invalid I/O address,board not configured\n");
560 continue;
562 hwconf.pdev = NULL;
564 if (mxser_initbrd(m, &hwconf) < 0)
565 continue;
567 mxser_getcfg(m, &hwconf);
569 m++;
572 /* Start finding ISA boards from module arg */
573 for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
574 int cap;
575 if (!(cap = ioaddr[b]))
576 continue;
578 retval = mxser_get_ISA_conf(cap, &hwconf);
580 if (retval != 0)
581 printk("Found MOXA %s board (CAP=0x%x)\n",
582 mxser_brdname[hwconf.board_type - 1],
583 ioaddr[b]);
585 if (retval <= 0) {
586 if (retval == MXSER_ERR_IRQ)
587 printk("Invalid interrupt number,board not configured\n");
588 else if (retval == MXSER_ERR_IRQ_CONFLIT)
589 printk("Invalid interrupt number,board not configured\n");
590 else if (retval == MXSER_ERR_VECTOR)
591 printk("Invalid interrupt vector,board not configured\n");
592 else if (retval == MXSER_ERR_IOADDR)
593 printk("Invalid I/O address,board not configured\n");
595 continue;
597 hwconf.pdev = NULL;
599 if (mxser_initbrd(m, &hwconf) < 0)
600 continue;
602 mxser_getcfg(m, &hwconf);
604 m++;
607 /* start finding PCI board here */
609 #ifdef CONFIG_PCI
611 struct pci_dev *pdev = NULL;
613 n = sizeof(mxser_pcibrds) / sizeof(mxser_pciinfo);
614 index = 0;
615 b = 0;
616 while (b < n) {
617 pdev = pci_find_device(mxser_pcibrds[b].vendor_id,
618 mxser_pcibrds[b].device_id, pdev);
619 if (!pdev)
620 break;
621 if (pci_enable_device(pdev))
622 continue;
623 b++;
624 hwconf.pdev = pdev;
625 printk("Found MOXA %s board(BusNo=%d,DevNo=%d)\n",
626 mxser_brdname[mxser_pcibrds[b].board_type - 1],
627 pdev->bus->number, PCI_SLOT(pdev->devfn >> 3));
628 if (m >= MXSER_BOARDS) {
629 printk("Too many Smartio family boards find (maximum %d),board not configured\n", MXSER_BOARDS);
630 } else {
631 retval = mxser_get_PCI_conf(pdev,
632 mxser_pcibrds[b].board_type, &hwconf);
633 if (retval < 0) {
634 if (retval == MXSER_ERR_IRQ)
635 printk("Invalid interrupt number,board not configured\n");
636 else if (retval == MXSER_ERR_IRQ_CONFLIT)
637 printk("Invalid interrupt number,board not configured\n");
638 else if (retval == MXSER_ERR_VECTOR)
639 printk("Invalid interrupt vector,board not configured\n");
640 else if (retval == MXSER_ERR_IOADDR)
641 printk("Invalid I/O address,board not configured\n");
642 continue;
645 if (mxser_initbrd(m, &hwconf) < 0)
646 continue;
647 mxser_getcfg(m, &hwconf);
648 m++;
654 #endif
656 for (i = m; i < MXSER_BOARDS; i++) {
657 mxsercfg[i].board_type = -1;
661 ret1 = 0;
662 ret2 = 0;
663 if (!(ret1 = tty_register_driver(&mxvar_sdriver))) {
664 if (!(ret2 = tty_register_driver(&mxvar_cdriver))) {
665 return 0;
666 } else {
667 tty_unregister_driver(&mxvar_sdriver);
668 printk("Couldn't install MOXA Smartio family callout driver !\n");
670 } else
671 printk("Couldn't install MOXA Smartio family driver !\n");
674 if (ret1 || ret2) {
675 for (i = 0; i < MXSER_BOARDS; i++) {
676 if (mxsercfg[i].board_type == -1)
677 continue;
678 else {
679 free_irq(mxsercfg[i].irq, &mxvar_table[i * MXSER_PORTS_PER_BOARD]);
682 return -1;
684 return (0);
687 static void mxser_do_softint(void *private_)
689 struct mxser_struct *info = (struct mxser_struct *) private_;
690 struct tty_struct *tty;
692 tty = info->tty;
693 if (!tty)
694 return;
695 if (test_and_clear_bit(MXSER_EVENT_TXLOW, &info->event)) {
696 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
697 tty->ldisc.write_wakeup)
698 (tty->ldisc.write_wakeup) (tty);
699 wake_up_interruptible(&tty->write_wait);
701 if (test_and_clear_bit(MXSER_EVENT_HANGUP, &info->event)) {
702 tty_hangup(tty);
707 * This routine is called whenever a serial port is opened. It
708 * enables interrupts for a serial port, linking in its async structure into
709 * the IRQ chain. It also performs the serial-specific
710 * initialization for the tty structure.
713 static int mxser_open(struct tty_struct *tty, struct file *filp)
715 struct mxser_struct *info;
716 int retval, line;
717 unsigned long page;
719 line = PORTNO(tty);
720 if (line == MXSER_PORTS)
721 return (0);
722 if ((line < 0) || (line > MXSER_PORTS))
723 return (-ENODEV);
724 info = mxvar_table + line;
725 if (!info->base)
726 return (-ENODEV);
728 info->count++;
729 tty->driver_data = info;
730 info->tty = tty;
732 if (!mxvar_tmp_buf) {
733 page = get_free_page(GFP_KERNEL);
734 if (!page)
735 return (-ENOMEM);
736 if (mxvar_tmp_buf)
737 free_page(page);
738 else
739 mxvar_tmp_buf = (unsigned char *) page;
742 * Start up serial port
744 retval = mxser_startup(info);
745 if (retval)
746 return (retval);
748 retval = mxser_block_til_ready(tty, filp, info);
749 if (retval)
750 return (retval);
752 if ((info->count == 1) && (info->flags & ASYNC_SPLIT_TERMIOS)) {
753 if (tty->driver.subtype == SERIAL_TYPE_NORMAL)
754 *tty->termios = info->normal_termios;
755 else
756 *tty->termios = info->callout_termios;
757 mxser_change_speed(info, 0);
759 info->session = current->session;
760 info->pgrp = current->pgrp;
762 MOD_INC_USE_COUNT;
764 return (0);
768 * This routine is called when the serial port gets closed. First, we
769 * wait for the last remaining data to be sent. Then, we unlink its
770 * async structure from the interrupt chain if necessary, and we free
771 * that IRQ if nothing is left in the chain.
774 static void mxser_close(struct tty_struct *tty, struct file *filp)
776 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
777 unsigned long flags;
778 unsigned long timeout;
780 if (PORTNO(tty) == MXSER_PORTS)
781 return;
782 if (!info)
783 return;
785 save_flags(flags);
786 cli();
788 if (tty_hung_up_p(filp)) {
789 restore_flags(flags);
790 MOD_DEC_USE_COUNT;
791 return;
793 if ((tty->count == 1) && (info->count != 1)) {
795 * Uh, oh. tty->count is 1, which means that the tty
796 * structure will be freed. Info->count should always
797 * be one in these conditions. If it's greater than
798 * one, we've got real problems, since it means the
799 * serial port won't be shutdown.
801 printk("mxser_close: bad serial port count; tty->count is 1, "
802 "info->count is %d\n", info->count);
803 info->count = 1;
805 if (--info->count < 0) {
806 printk("mxser_close: bad serial port count for ttys%d: %d\n",
807 info->port, info->count);
808 info->count = 0;
810 if (info->count) {
811 restore_flags(flags);
812 MOD_DEC_USE_COUNT;
813 return;
815 info->flags |= ASYNC_CLOSING;
817 * Save the termios structure, since this port may have
818 * separate termios for callout and dialin.
820 if (info->flags & ASYNC_NORMAL_ACTIVE)
821 info->normal_termios = *tty->termios;
822 if (info->flags & ASYNC_CALLOUT_ACTIVE)
823 info->callout_termios = *tty->termios;
825 * Now we wait for the transmit buffer to clear; and we notify
826 * the line discipline to only process XON/XOFF characters.
828 tty->closing = 1;
829 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
830 tty_wait_until_sent(tty, info->closing_wait);
832 * At this point we stop accepting input. To do this, we
833 * disable the receive line status interrupts, and tell the
834 * interrupt driver to stop checking the data ready bit in the
835 * line status register.
837 info->IER &= ~UART_IER_RLSI;
838 /* by William
839 info->read_status_mask &= ~UART_LSR_DR;
841 if (info->flags & ASYNC_INITIALIZED) {
842 outb(info->IER, info->base + UART_IER);
844 * Before we drop DTR, make sure the UART transmitter
845 * has completely drained; this is especially
846 * important if there is a transmit FIFO!
848 timeout = jiffies + HZ;
849 while (!(inb(info->base + UART_LSR) & UART_LSR_TEMT)) {
850 current->state = TASK_INTERRUPTIBLE;
851 schedule_timeout(5);
852 if (jiffies > timeout)
853 break;
856 mxser_shutdown(info);
857 if (tty->driver.flush_buffer)
858 tty->driver.flush_buffer(tty);
859 if (tty->ldisc.flush_buffer)
860 tty->ldisc.flush_buffer(tty);
861 tty->closing = 0;
862 info->event = 0;
863 info->tty = 0;
864 if (info->blocked_open) {
865 if (info->close_delay) {
866 current->state = TASK_INTERRUPTIBLE;
867 schedule_timeout(info->close_delay);
869 wake_up_interruptible(&info->open_wait);
871 info->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CALLOUT_ACTIVE |
872 ASYNC_CLOSING);
873 wake_up_interruptible(&info->close_wait);
874 restore_flags(flags);
876 MOD_DEC_USE_COUNT;
879 static int mxser_write(struct tty_struct *tty, int from_user,
880 const unsigned char *buf, int count)
882 int c, total = 0;
883 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
884 unsigned long flags;
886 if (!tty || !info->xmit_buf || !mxvar_tmp_buf)
887 return (0);
889 if (from_user)
890 down(&mxvar_tmp_buf_sem);
891 save_flags(flags);
892 while (1) {
893 cli();
894 c = MIN(count, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
895 SERIAL_XMIT_SIZE - info->xmit_head));
896 if (c <= 0)
897 break;
899 if (from_user) {
900 copy_from_user(mxvar_tmp_buf, buf, c);
901 c = MIN(c, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
902 SERIAL_XMIT_SIZE - info->xmit_head));
903 memcpy(info->xmit_buf + info->xmit_head, mxvar_tmp_buf, c);
904 } else
905 memcpy(info->xmit_buf + info->xmit_head, buf, c);
906 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE - 1);
907 info->xmit_cnt += c;
908 restore_flags(flags);
909 buf += c;
910 count -= c;
911 total += c;
913 if (from_user)
914 up(&mxvar_tmp_buf_sem);
915 if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped &&
916 !(info->IER & UART_IER_THRI)) {
917 info->IER |= UART_IER_THRI;
918 outb(info->IER, info->base + UART_IER);
920 restore_flags(flags);
921 return (total);
924 static void mxser_put_char(struct tty_struct *tty, unsigned char ch)
926 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
927 unsigned long flags;
929 if (!tty || !info->xmit_buf)
930 return;
932 save_flags(flags);
933 cli();
934 if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1) {
935 restore_flags(flags);
936 return;
938 info->xmit_buf[info->xmit_head++] = ch;
939 info->xmit_head &= SERIAL_XMIT_SIZE - 1;
940 info->xmit_cnt++;
941 /********************************************** why ??? ***********
942 if ( !tty->stopped && !tty->hw_stopped &&
943 !(info->IER & UART_IER_THRI) ) {
944 info->IER |= UART_IER_THRI;
945 outb(info->IER, info->base + UART_IER);
947 *****************************************************************/
948 restore_flags(flags);
951 static void mxser_flush_chars(struct tty_struct *tty)
953 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
954 unsigned long flags;
956 if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
957 !info->xmit_buf)
958 return;
960 save_flags(flags);
961 cli();
962 info->IER |= UART_IER_THRI;
963 outb(info->IER, info->base + UART_IER);
964 restore_flags(flags);
967 static int mxser_write_room(struct tty_struct *tty)
969 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
970 int ret;
972 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
973 if (ret < 0)
974 ret = 0;
975 return (ret);
978 static int mxser_chars_in_buffer(struct tty_struct *tty)
980 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
982 return (info->xmit_cnt);
985 static void mxser_flush_buffer(struct tty_struct *tty)
987 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
988 unsigned long flags;
990 save_flags(flags);
991 cli();
992 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
993 restore_flags(flags);
994 wake_up_interruptible(&tty->write_wait);
995 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
996 tty->ldisc.write_wakeup)
997 (tty->ldisc.write_wakeup) (tty);
1000 static int mxser_ioctl(struct tty_struct *tty, struct file *file,
1001 unsigned int cmd, unsigned long arg)
1003 unsigned long flags;
1004 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
1005 int retval;
1006 struct async_icount cprev, cnow; /* kernel counter temps */
1007 struct serial_icounter_struct *p_cuser; /* user space */
1008 unsigned long templ;
1010 if (PORTNO(tty) == MXSER_PORTS)
1011 return (mxser_ioctl_special(cmd, arg));
1012 if ((cmd != TIOCGSERIAL) && (cmd != TIOCMIWAIT) &&
1013 (cmd != TIOCGICOUNT)) {
1014 if (tty->flags & (1 << TTY_IO_ERROR))
1015 return (-EIO);
1017 switch (cmd) {
1018 case TCSBRK: /* SVID version: non-zero arg --> no break */
1019 retval = tty_check_change(tty);
1020 if (retval)
1021 return (retval);
1022 tty_wait_until_sent(tty, 0);
1023 if (!arg)
1024 mxser_send_break(info, HZ / 4); /* 1/4 second */
1025 return (0);
1026 case TCSBRKP: /* support for POSIX tcsendbreak() */
1027 retval = tty_check_change(tty);
1028 if (retval)
1029 return (retval);
1030 tty_wait_until_sent(tty, 0);
1031 mxser_send_break(info, arg ? arg * (HZ / 10) : HZ / 4);
1032 return (0);
1033 case TIOCGSOFTCAR:
1034 return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long *) arg);
1035 case TIOCSSOFTCAR:
1036 if(get_user(templ, (unsigned long *) arg))
1037 return -EFAULT;
1038 arg = templ;
1039 tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) |
1040 (arg ? CLOCAL : 0));
1041 return (0);
1042 case TIOCMGET:
1043 return (mxser_get_modem_info(info, (unsigned int *) arg));
1044 case TIOCMBIS:
1045 case TIOCMBIC:
1046 case TIOCMSET:
1047 return (mxser_set_modem_info(info, cmd, (unsigned int *) arg));
1048 case TIOCGSERIAL:
1049 return (mxser_get_serial_info(info, (struct serial_struct *) arg));
1050 case TIOCSSERIAL:
1051 return (mxser_set_serial_info(info, (struct serial_struct *) arg));
1052 case TIOCSERGETLSR: /* Get line status register */
1053 return (mxser_get_lsr_info(info, (unsigned int *) arg));
1055 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1056 * - mask passed in arg for lines of interest
1057 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1058 * Caller should use TIOCGICOUNT to see which one it was
1060 case TIOCMIWAIT:
1061 save_flags(flags);
1062 cli();
1063 cprev = info->icount; /* note the counters on entry */
1064 restore_flags(flags);
1065 while (1) {
1066 interruptible_sleep_on(&info->delta_msr_wait);
1067 /* see if a signal did it */
1068 if (signal_pending(current))
1069 return (-ERESTARTSYS);
1070 save_flags(flags);
1071 cli();
1072 cnow = info->icount; /* atomic copy */
1073 restore_flags(flags);
1074 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1075 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1076 return (-EIO); /* no change => error */
1077 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1078 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1079 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1080 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
1081 return (0);
1083 cprev = cnow;
1085 /* NOTREACHED */
1087 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1088 * Return: write counters to the user passed counter struct
1089 * NB: both 1->0 and 0->1 transitions are counted except for
1090 * RI where only 0->1 is counted.
1092 case TIOCGICOUNT:
1093 save_flags(flags);
1094 cli();
1095 cnow = info->icount;
1096 restore_flags(flags);
1097 p_cuser = (struct serial_icounter_struct *) arg;
1098 if(put_user(cnow.cts, &p_cuser->cts))
1099 return -EFAULT;
1100 if(put_user(cnow.dsr, &p_cuser->dsr))
1101 return -EFAULT;
1102 if(put_user(cnow.rng, &p_cuser->rng))
1103 return -EFAULT;
1104 return put_user(cnow.dcd, &p_cuser->dcd);
1105 case MOXA_HighSpeedOn:
1106 return put_user(info->baud_base != 115200 ? 1 : 0, (int *) arg);
1107 default:
1108 return (-ENOIOCTLCMD);
1110 return (0);
1113 static int mxser_ioctl_special(unsigned int cmd, unsigned long arg)
1115 int i, result, status;
1117 switch (cmd) {
1118 case MOXA_GET_CONF:
1119 if(copy_to_user((struct mxser_hwconf *) arg, mxsercfg,
1120 sizeof(struct mxser_hwconf) * 4))
1121 return -EFAULT;
1122 return 0;
1123 case MOXA_GET_MAJOR:
1124 if(copy_to_user((int *) arg, &ttymajor, sizeof(int)))
1125 return -EFAULT;
1126 return 0;
1128 case MOXA_GET_CUMAJOR:
1129 if(copy_to_user((int *) arg, &calloutmajor, sizeof(int)))
1130 return -EFAULT;
1131 return 0;
1133 case MOXA_CHKPORTENABLE:
1134 result = 0;
1135 for (i = 0; i < MXSER_PORTS; i++) {
1136 if (mxvar_table[i].base)
1137 result |= (1 << i);
1139 return put_user(result, (unsigned long *) arg);
1140 case MOXA_GETDATACOUNT:
1141 if(copy_to_user((struct mxser_log *) arg, &mxvar_log, sizeof(mxvar_log)))
1142 return -EFAULT;
1143 return (0);
1144 case MOXA_GETMSTATUS:
1145 for (i = 0; i < MXSER_PORTS; i++) {
1146 GMStatus[i].ri = 0;
1147 if (!mxvar_table[i].base) {
1148 GMStatus[i].dcd = 0;
1149 GMStatus[i].dsr = 0;
1150 GMStatus[i].cts = 0;
1151 continue;
1153 if (!mxvar_table[i].tty || !mxvar_table[i].tty->termios)
1154 GMStatus[i].cflag = mxvar_table[i].normal_termios.c_cflag;
1155 else
1156 GMStatus[i].cflag = mxvar_table[i].tty->termios->c_cflag;
1158 status = inb(mxvar_table[i].base + UART_MSR);
1159 if (status & 0x80 /*UART_MSR_DCD */ )
1160 GMStatus[i].dcd = 1;
1161 else
1162 GMStatus[i].dcd = 0;
1164 if (status & 0x20 /*UART_MSR_DSR */ )
1165 GMStatus[i].dsr = 1;
1166 else
1167 GMStatus[i].dsr = 0;
1170 if (status & 0x10 /*UART_MSR_CTS */ )
1171 GMStatus[i].cts = 1;
1172 else
1173 GMStatus[i].cts = 0;
1175 if(copy_to_user((struct mxser_mstatus *) arg, GMStatus,
1176 sizeof(struct mxser_mstatus) * MXSER_PORTS))
1177 return -EFAULT;
1178 return 0;
1179 default:
1180 return (-ENOIOCTLCMD);
1182 return (0);
1186 * This routine is called by the upper-layer tty layer to signal that
1187 * incoming characters should be throttled.
1189 static void mxser_throttle(struct tty_struct *tty)
1191 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
1192 unsigned long flags;
1194 if (I_IXOFF(tty)) {
1195 info->x_char = STOP_CHAR(tty);
1196 save_flags(flags);
1197 cli();
1198 outb(info->IER, 0);
1199 info->IER |= UART_IER_THRI;
1200 outb(info->IER, info->base + UART_IER); /* force Tx interrupt */
1201 restore_flags(flags);
1203 if (info->tty->termios->c_cflag & CRTSCTS) {
1204 info->MCR &= ~UART_MCR_RTS;
1205 save_flags(flags);
1206 cli();
1207 outb(info->MCR, info->base + UART_MCR);
1208 restore_flags(flags);
1212 static void mxser_unthrottle(struct tty_struct *tty)
1214 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
1215 unsigned long flags;
1217 if (I_IXOFF(tty)) {
1218 if (info->x_char)
1219 info->x_char = 0;
1220 else {
1221 info->x_char = START_CHAR(tty);
1222 save_flags(flags);
1223 cli();
1224 outb(info->IER, 0);
1225 info->IER |= UART_IER_THRI; /* force Tx interrupt */
1226 outb(info->IER, info->base + UART_IER);
1227 restore_flags(flags);
1230 if (info->tty->termios->c_cflag & CRTSCTS) {
1231 info->MCR |= UART_MCR_RTS;
1232 save_flags(flags);
1233 cli();
1234 outb(info->MCR, info->base + UART_MCR);
1235 restore_flags(flags);
1239 static void mxser_set_termios(struct tty_struct *tty,
1240 struct termios *old_termios)
1242 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
1244 /* 8-2-99 by William
1245 if ( (tty->termios->c_cflag == old_termios->c_cflag) &&
1246 (RELEVANT_IFLAG(tty->termios->c_iflag) ==
1247 RELEVANT_IFLAG(old_termios->c_iflag)) )
1248 return;
1250 mxser_change_speed(info, old_termios);
1252 if ( (old_termios->c_cflag & CRTSCTS) &&
1253 !(tty->termios->c_cflag & CRTSCTS) ) {
1254 tty->hw_stopped = 0;
1255 mxser_start(tty);
1258 if ((tty->termios->c_cflag != old_termios->c_cflag) ||
1259 (RELEVANT_IFLAG(tty->termios->c_iflag) !=
1260 RELEVANT_IFLAG(old_termios->c_iflag))) {
1262 mxser_change_speed(info, old_termios);
1264 if ((old_termios->c_cflag & CRTSCTS) &&
1265 !(tty->termios->c_cflag & CRTSCTS)) {
1266 tty->hw_stopped = 0;
1267 mxser_start(tty);
1270 /* Handle sw stopped */
1271 if ((old_termios->c_iflag & IXON) &&
1272 !(tty->termios->c_iflag & IXON)) {
1273 tty->stopped = 0;
1274 mxser_start(tty);
1279 * mxser_stop() and mxser_start()
1281 * This routines are called before setting or resetting tty->stopped.
1282 * They enable or disable transmitter interrupts, as necessary.
1284 static void mxser_stop(struct tty_struct *tty)
1286 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
1287 unsigned long flags;
1289 save_flags(flags);
1290 cli();
1291 if (info->IER & UART_IER_THRI) {
1292 info->IER &= ~UART_IER_THRI;
1293 outb(info->IER, info->base + UART_IER);
1295 restore_flags(flags);
1298 static void mxser_start(struct tty_struct *tty)
1300 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
1301 unsigned long flags;
1303 save_flags(flags);
1304 cli();
1305 if (info->xmit_cnt && info->xmit_buf &&
1306 !(info->IER & UART_IER_THRI)) {
1307 info->IER |= UART_IER_THRI;
1308 outb(info->IER, info->base + UART_IER);
1310 restore_flags(flags);
1314 * This routine is called by tty_hangup() when a hangup is signaled.
1316 void mxser_hangup(struct tty_struct *tty)
1318 struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
1320 mxser_flush_buffer(tty);
1321 mxser_shutdown(info);
1322 info->event = 0;
1323 info->count = 0;
1324 info->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CALLOUT_ACTIVE);
1325 info->tty = 0;
1326 wake_up_interruptible(&info->open_wait);
1330 * This is the serial driver's generic interrupt routine
1332 static void mxser_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1334 int status, i;
1335 struct mxser_struct *info;
1336 struct mxser_struct *port;
1337 int max, irqbits, bits, msr;
1338 int pass_counter = 0;
1340 port = 0;
1341 for (i = 0; i < MXSER_BOARDS; i++) {
1342 if (dev_id == &(mxvar_table[i * MXSER_PORTS_PER_BOARD])) {
1343 port = dev_id;
1344 break;
1348 if (i == MXSER_BOARDS)
1349 return;
1350 if (port == 0)
1351 return;
1352 max = mxser_numports[mxsercfg[i].board_type - 1];
1354 while (1) {
1355 irqbits = inb(port->vector) & port->vectormask;
1356 if (irqbits == port->vectormask)
1357 break;
1358 for (i = 0, bits = 1; i < max; i++, irqbits |= bits, bits <<= 1) {
1359 if (irqbits == port->vectormask)
1360 break;
1361 if (bits & irqbits)
1362 continue;
1363 info = port + i;
1364 if (!info->tty ||
1365 (inb(info->base + UART_IIR) & UART_IIR_NO_INT))
1366 continue;
1367 status = inb(info->base + UART_LSR) & info->read_status_mask;
1368 if (status & UART_LSR_DR)
1369 mxser_receive_chars(info, &status);
1370 msr = inb(info->base + UART_MSR);
1371 if (msr & UART_MSR_ANY_DELTA)
1372 mxser_check_modem_status(info, msr);
1373 if (status & UART_LSR_THRE) {
1374 /* 8-2-99 by William
1375 if ( info->x_char || (info->xmit_cnt > 0) )
1377 mxser_transmit_chars(info);
1380 if (pass_counter++ > MXSER_ISR_PASS_LIMIT) {
1381 #if 0
1382 printk("MOXA Smartio/Indusrtio family driver interrupt loop break\n");
1383 #endif
1384 break; /* Prevent infinite loops */
1389 static inline void mxser_receive_chars(struct mxser_struct *info,
1390 int *status)
1392 struct tty_struct *tty = info->tty;
1393 unsigned char ch;
1394 int ignored = 0;
1395 int cnt = 0;
1397 do {
1398 ch = inb(info->base + UART_RX);
1399 if (*status & info->ignore_status_mask) {
1400 if (++ignored > 100)
1401 break;
1402 } else {
1403 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
1404 break;
1405 tty->flip.count++;
1406 if (*status & UART_LSR_SPECIAL) {
1407 if (*status & UART_LSR_BI) {
1408 *tty->flip.flag_buf_ptr++ = TTY_BREAK;
1409 if (info->flags & ASYNC_SAK)
1410 do_SAK(tty);
1411 } else if (*status & UART_LSR_PE) {
1412 *tty->flip.flag_buf_ptr++ = TTY_PARITY;
1413 } else if (*status & UART_LSR_FE) {
1414 *tty->flip.flag_buf_ptr++ = TTY_FRAME;
1415 } else if (*status & UART_LSR_OE) {
1416 *tty->flip.flag_buf_ptr++ = TTY_OVERRUN;
1417 } else
1418 *tty->flip.flag_buf_ptr++ = 0;
1419 } else
1420 *tty->flip.flag_buf_ptr++ = 0;
1421 *tty->flip.char_buf_ptr++ = ch;
1422 cnt++;
1424 *status = inb(info->base + UART_LSR) & info->read_status_mask;
1425 } while (*status & UART_LSR_DR);
1426 mxvar_log.rxcnt[info->port] += cnt;
1427 queue_task(&tty->flip.tqueue, &tq_timer);
1431 static inline void mxser_transmit_chars(struct mxser_struct *info)
1433 int count, cnt;
1435 if (info->x_char) {
1436 outb(info->x_char, info->base + UART_TX);
1437 info->x_char = 0;
1438 mxvar_log.txcnt[info->port]++;
1439 return;
1441 if ((info->xmit_cnt <= 0) || info->tty->stopped ||
1442 info->tty->hw_stopped) {
1443 info->IER &= ~UART_IER_THRI;
1444 outb(info->IER, info->base + UART_IER);
1445 return;
1447 cnt = info->xmit_cnt;
1448 count = info->xmit_fifo_size;
1449 do {
1450 outb(info->xmit_buf[info->xmit_tail++], info->base + UART_TX);
1451 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE - 1);
1452 if (--info->xmit_cnt <= 0)
1453 break;
1454 } while (--count > 0);
1455 mxvar_log.txcnt[info->port] += (cnt - info->xmit_cnt);
1457 if (info->xmit_cnt < WAKEUP_CHARS) {
1458 set_bit(MXSER_EVENT_TXLOW, &info->event);
1459 queue_task(&info->tqueue, &tq_scheduler);
1461 if (info->xmit_cnt <= 0) {
1462 info->IER &= ~UART_IER_THRI;
1463 outb(info->IER, info->base + UART_IER);
1467 static inline void mxser_check_modem_status(struct mxser_struct *info,
1468 int status)
1471 /* update input line counters */
1472 if (status & UART_MSR_TERI)
1473 info->icount.rng++;
1474 if (status & UART_MSR_DDSR)
1475 info->icount.dsr++;
1476 if (status & UART_MSR_DDCD)
1477 info->icount.dcd++;
1478 if (status & UART_MSR_DCTS)
1479 info->icount.cts++;
1480 wake_up_interruptible(&info->delta_msr_wait);
1482 if ((info->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
1483 if (status & UART_MSR_DCD)
1484 wake_up_interruptible(&info->open_wait);
1485 else if (!((info->flags & ASYNC_CALLOUT_ACTIVE) &&
1486 (info->flags & ASYNC_CALLOUT_NOHUP)))
1487 set_bit(MXSER_EVENT_HANGUP, &info->event);
1488 queue_task(&info->tqueue, &tq_scheduler);
1491 if (info->flags & ASYNC_CTS_FLOW) {
1492 if (info->tty->hw_stopped) {
1493 if (status & UART_MSR_CTS) {
1494 info->tty->hw_stopped = 0;
1495 info->IER |= UART_IER_THRI;
1496 outb(info->IER, info->base + UART_IER);
1498 set_bit(MXSER_EVENT_TXLOW, &info->event);
1499 queue_task(&info->tqueue, &tq_scheduler);
1501 } else {
1502 if (!(status & UART_MSR_CTS)) {
1503 info->tty->hw_stopped = 1;
1504 info->IER &= ~UART_IER_THRI;
1505 outb(info->IER, info->base + UART_IER);
1511 static int mxser_block_til_ready(struct tty_struct *tty, struct file *filp,
1512 struct mxser_struct *info)
1514 DECLARE_WAITQUEUE(wait, current);
1515 unsigned long flags;
1516 int retval;
1517 int do_clocal = 0;
1520 * If the device is in the middle of being closed, then block
1521 * until it's done, and then try again.
1523 if (tty_hung_up_p(filp) || (info->flags & ASYNC_CLOSING)) {
1524 if (info->flags & ASYNC_CLOSING)
1525 interruptible_sleep_on(&info->close_wait);
1526 #ifdef SERIAL_DO_RESTART
1527 if (info->flags & ASYNC_HUP_NOTIFY)
1528 return (-EAGAIN);
1529 else
1530 return (-ERESTARTSYS);
1531 #else
1532 return (-EAGAIN);
1533 #endif
1536 * If this is a callout device, then just make sure the normal
1537 * device isn't being used.
1539 if (tty->driver.subtype == SERIAL_TYPE_CALLOUT) {
1540 if (info->flags & ASYNC_NORMAL_ACTIVE)
1541 return (-EBUSY);
1542 if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
1543 (info->flags & ASYNC_SESSION_LOCKOUT) &&
1544 (info->session != current->session))
1545 return (-EBUSY);
1546 if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
1547 (info->flags & ASYNC_PGRP_LOCKOUT) &&
1548 (info->pgrp != current->pgrp))
1549 return (-EBUSY);
1550 info->flags |= ASYNC_CALLOUT_ACTIVE;
1551 return (0);
1554 * If non-blocking mode is set, or the port is not enabled,
1555 * then make the check up front and then exit.
1557 if ((filp->f_flags & O_NONBLOCK) ||
1558 (tty->flags & (1 << TTY_IO_ERROR))) {
1559 if (info->flags & ASYNC_CALLOUT_ACTIVE)
1560 return (-EBUSY);
1561 info->flags |= ASYNC_NORMAL_ACTIVE;
1562 return (0);
1564 if (info->flags & ASYNC_CALLOUT_ACTIVE) {
1565 if (info->normal_termios.c_cflag & CLOCAL)
1566 do_clocal = 1;
1567 } else {
1568 if (tty->termios->c_cflag & CLOCAL)
1569 do_clocal = 1;
1573 * Block waiting for the carrier detect and the line to become
1574 * free (i.e., not in use by the callout). While we are in
1575 * this loop, info->count is dropped by one, so that
1576 * mxser_close() knows when to free things. We restore it upon
1577 * exit, either normal or abnormal.
1579 retval = 0;
1580 add_wait_queue(&info->open_wait, &wait);
1581 save_flags(flags);
1582 cli();
1583 if (!tty_hung_up_p(filp))
1584 info->count--;
1585 restore_flags(flags);
1586 info->blocked_open++;
1587 while (1) {
1588 save_flags(flags);
1589 cli();
1590 if (!(info->flags & ASYNC_CALLOUT_ACTIVE))
1591 outb(inb(info->base + UART_MCR) | UART_MCR_DTR | UART_MCR_RTS,
1592 info->base + UART_MCR);
1593 restore_flags(flags);
1594 current->state = TASK_INTERRUPTIBLE;
1595 if (tty_hung_up_p(filp) || !(info->flags & ASYNC_INITIALIZED)) {
1596 #ifdef SERIAL_DO_RESTART
1597 if (info->flags & ASYNC_HUP_NOTIFY)
1598 retval = -EAGAIN;
1599 else
1600 retval = -ERESTARTSYS;
1601 #else
1602 retval = -EAGAIN;
1603 #endif
1604 break;
1606 if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
1607 !(info->flags & ASYNC_CLOSING) &&
1608 (do_clocal || (inb(info->base + UART_MSR) & UART_MSR_DCD)))
1609 break;
1610 if (signal_pending(current)) {
1611 retval = -ERESTARTSYS;
1612 break;
1614 schedule();
1616 current->state = TASK_RUNNING;
1617 remove_wait_queue(&info->open_wait, &wait);
1618 if (!tty_hung_up_p(filp))
1619 info->count++;
1620 info->blocked_open--;
1621 if (retval)
1622 return (retval);
1623 info->flags |= ASYNC_NORMAL_ACTIVE;
1624 return (0);
1627 static int mxser_startup(struct mxser_struct *info)
1629 unsigned long flags;
1630 unsigned long page;
1632 page = get_free_page(GFP_KERNEL);
1633 if (!page)
1634 return (-ENOMEM);
1636 save_flags(flags);
1637 cli();
1639 if (info->flags & ASYNC_INITIALIZED) {
1640 free_page(page);
1641 restore_flags(flags);
1642 return (0);
1644 if (!info->base || !info->type) {
1645 if (info->tty)
1646 set_bit(TTY_IO_ERROR, &info->tty->flags);
1647 free_page(page);
1648 restore_flags(flags);
1649 return (0);
1651 if (info->xmit_buf)
1652 free_page(page);
1653 else
1654 info->xmit_buf = (unsigned char *) page;
1657 * Clear the FIFO buffers and disable them
1658 * (they will be reenabled in mxser_change_speed())
1660 if (info->xmit_fifo_size == 16)
1661 outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
1662 info->base + UART_FCR);
1665 * At this point there's no way the LSR could still be 0xFF;
1666 * if it is, then bail out, because there's likely no UART
1667 * here.
1669 if (inb(info->base + UART_LSR) == 0xff) {
1670 restore_flags(flags);
1671 if (suser()) {
1672 if (info->tty)
1673 set_bit(TTY_IO_ERROR, &info->tty->flags);
1674 return (0);
1675 } else
1676 return (-ENODEV);
1679 * Clear the interrupt registers.
1681 (void) inb(info->base + UART_LSR);
1682 (void) inb(info->base + UART_RX);
1683 (void) inb(info->base + UART_IIR);
1684 (void) inb(info->base + UART_MSR);
1687 * Now, initialize the UART
1689 outb(UART_LCR_WLEN8, info->base + UART_LCR); /* reset DLAB */
1690 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
1691 outb(info->MCR, info->base + UART_MCR);
1694 * Finally, enable interrupts
1696 info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
1697 outb(info->IER, info->base + UART_IER); /* enable interrupts */
1700 * And clear the interrupt registers again for luck.
1702 (void) inb(info->base + UART_LSR);
1703 (void) inb(info->base + UART_RX);
1704 (void) inb(info->base + UART_IIR);
1705 (void) inb(info->base + UART_MSR);
1707 if (info->tty)
1708 test_and_clear_bit(TTY_IO_ERROR, &info->tty->flags);
1710 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1713 * and set the speed of the serial port
1715 mxser_change_speed(info, 0);
1717 info->flags |= ASYNC_INITIALIZED;
1718 restore_flags(flags);
1719 return (0);
1723 * This routine will shutdown a serial port; interrupts maybe disabled, and
1724 * DTR is dropped if the hangup on close termio flag is on.
1726 static void mxser_shutdown(struct mxser_struct *info)
1728 unsigned long flags;
1730 if (!(info->flags & ASYNC_INITIALIZED))
1731 return;
1733 save_flags(flags);
1734 cli(); /* Disable interrupts */
1737 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
1738 * here so the queue might never be waken up
1740 wake_up_interruptible(&info->delta_msr_wait);
1743 * Free the IRQ, if necessary
1745 if (info->xmit_buf) {
1746 free_page((unsigned long) info->xmit_buf);
1747 info->xmit_buf = 0;
1749 info->IER = 0;
1750 outb(0x00, info->base + UART_IER); /* disable all intrs */
1752 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
1753 info->MCR &= ~(UART_MCR_DTR | UART_MCR_RTS);
1754 outb(info->MCR, info->base + UART_MCR);
1756 /* clear Rx/Tx FIFO's */
1757 outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT), info->base + UART_FCR);
1758 /* read data port to reset things */
1759 (void) inb(info->base + UART_RX);
1761 if (info->tty)
1762 set_bit(TTY_IO_ERROR, &info->tty->flags);
1764 info->flags &= ~ASYNC_INITIALIZED;
1765 restore_flags(flags);
1769 * This routine is called to set the UART divisor registers to match
1770 * the specified baud rate for a serial port.
1772 static int mxser_change_speed(struct mxser_struct *info,
1773 struct termios *old_termios)
1775 int quot = 0;
1776 unsigned cflag, cval, fcr;
1777 int i;
1778 int ret = 0;
1779 unsigned long flags;
1781 if (!info->tty || !info->tty->termios)
1782 return ret;
1783 cflag = info->tty->termios->c_cflag;
1784 if (!(info->base))
1785 return ret;
1787 #ifndef B921600
1788 #define B921600 (B460800 +1)
1789 #endif
1790 switch (cflag & (CBAUD | CBAUDEX)) {
1791 case B921600:
1792 i = 20;
1793 break;
1794 case B460800:
1795 i = 19;
1796 break;
1797 case B230400:
1798 i = 18;
1799 break;
1800 case B115200:
1801 i = 17;
1802 break;
1803 case B57600:
1804 i = 16;
1805 break;
1806 case B38400:
1807 i = 15;
1808 break;
1809 case B19200:
1810 i = 14;
1811 break;
1812 case B9600:
1813 i = 13;
1814 break;
1815 case B4800:
1816 i = 12;
1817 break;
1818 case B2400:
1819 i = 11;
1820 break;
1821 case B1800:
1822 i = 10;
1823 break;
1824 case B1200:
1825 i = 9;
1826 break;
1827 case B600:
1828 i = 8;
1829 break;
1830 case B300:
1831 i = 7;
1832 break;
1833 case B200:
1834 i = 6;
1835 break;
1836 case B150:
1837 i = 5;
1838 break;
1839 case B134:
1840 i = 4;
1841 break;
1842 case B110:
1843 i = 3;
1844 break;
1845 case B75:
1846 i = 2;
1847 break;
1848 case B50:
1849 i = 1;
1850 break;
1851 default:
1852 i = 0;
1853 break;
1856 if (i == 15) {
1857 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
1858 i = 16; /* 57600 bps */
1859 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
1860 i = 17; /* 115200 bps */
1862 #ifdef ASYNC_SPD_SHI
1863 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
1864 i = 18;
1865 #endif
1867 #ifdef ASYNC_SPD_WARP
1868 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
1869 i = 19;
1870 #endif
1872 if (mxvar_baud_table[i] == 134) {
1873 quot = (2 * info->baud_base / 269);
1874 } else if (mxvar_baud_table[i]) {
1875 quot = info->baud_base / mxvar_baud_table[i];
1876 if (!quot && old_termios) {
1877 /* re-calculate */
1878 info->tty->termios->c_cflag &= ~CBAUD;
1879 info->tty->termios->c_cflag |= (old_termios->c_cflag & CBAUD);
1880 switch (info->tty->termios->c_cflag & (CBAUD | CBAUDEX)) {
1881 case B921600:
1882 i = 20;
1883 break;
1884 case B460800:
1885 i = 19;
1886 break;
1887 case B230400:
1888 i = 18;
1889 break;
1890 case B115200:
1891 i = 17;
1892 break;
1893 case B57600:
1894 i = 16;
1895 break;
1896 case B38400:
1897 i = 15;
1898 break;
1899 case B19200:
1900 i = 14;
1901 break;
1902 case B9600:
1903 i = 13;
1904 break;
1905 case B4800:
1906 i = 12;
1907 break;
1908 case B2400:
1909 i = 11;
1910 break;
1911 case B1800:
1912 i = 10;
1913 break;
1914 case B1200:
1915 i = 9;
1916 break;
1917 case B600:
1918 i = 8;
1919 break;
1920 case B300:
1921 i = 7;
1922 break;
1923 case B200:
1924 i = 6;
1925 break;
1926 case B150:
1927 i = 5;
1928 break;
1929 case B134:
1930 i = 4;
1931 break;
1932 case B110:
1933 i = 3;
1934 break;
1935 case B75:
1936 i = 2;
1937 break;
1938 case B50:
1939 i = 1;
1940 break;
1941 default:
1942 i = 0;
1943 break;
1945 if (i == 15) {
1946 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
1947 i = 16; /* 57600 bps */
1948 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
1949 i = 17; /* 115200 bps */
1950 #ifdef ASYNC_SPD_SHI
1951 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_SHI)
1952 i = 18;
1953 #endif
1954 #ifdef ASYNC_SPD_WARP
1955 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_WARP)
1956 i = 19;
1957 #endif
1959 if (mxvar_baud_table[i] == 134) {
1960 quot = (2 * info->baud_base / 269);
1961 } else if (mxvar_baud_table[i]) {
1962 quot = info->baud_base / mxvar_baud_table[i];
1963 if (quot == 0)
1964 quot = 1;
1965 } else {
1966 quot = 0;
1968 } else if (quot == 0)
1969 quot = 1;
1970 } else {
1971 quot = 0;
1974 if (quot) {
1975 info->MCR |= UART_MCR_DTR;
1976 save_flags(flags);
1977 cli();
1978 outb(info->MCR, info->base + UART_MCR);
1979 restore_flags(flags);
1980 } else {
1981 info->MCR &= ~UART_MCR_DTR;
1982 save_flags(flags);
1983 cli();
1984 outb(info->MCR, info->base + UART_MCR);
1985 restore_flags(flags);
1986 return ret;
1988 /* byte size and parity */
1989 switch (cflag & CSIZE) {
1990 case CS5:
1991 cval = 0x00;
1992 break;
1993 case CS6:
1994 cval = 0x01;
1995 break;
1996 case CS7:
1997 cval = 0x02;
1998 break;
1999 case CS8:
2000 cval = 0x03;
2001 break;
2002 default:
2003 cval = 0x00;
2004 break; /* too keep GCC shut... */
2006 if (cflag & CSTOPB)
2007 cval |= 0x04;
2008 if (cflag & PARENB)
2009 cval |= UART_LCR_PARITY;
2010 if (!(cflag & PARODD))
2011 cval |= UART_LCR_EPAR;
2012 if ((info->type == PORT_8250) || (info->type == PORT_16450)) {
2013 fcr = 0;
2014 } else {
2015 fcr = UART_FCR_ENABLE_FIFO;
2016 switch (info->rx_trigger) {
2017 case 1:
2018 fcr |= UART_FCR_TRIGGER_1;
2019 break;
2020 case 4:
2021 fcr |= UART_FCR_TRIGGER_4;
2022 break;
2023 case 8:
2024 fcr |= UART_FCR_TRIGGER_8;
2025 break;
2026 default:
2027 fcr |= UART_FCR_TRIGGER_14;
2031 /* CTS flow control flag and modem status interrupts */
2032 info->IER &= ~UART_IER_MSI;
2033 info->MCR &= ~UART_MCR_AFE;
2034 if (cflag & CRTSCTS) {
2035 info->flags |= ASYNC_CTS_FLOW;
2036 info->IER |= UART_IER_MSI;
2037 if (info->type == PORT_16550A)
2038 info->MCR |= UART_MCR_AFE;
2039 } else {
2040 info->flags &= ~ASYNC_CTS_FLOW;
2042 outb(info->MCR, info->base + UART_MCR);
2043 if (cflag & CLOCAL)
2044 info->flags &= ~ASYNC_CHECK_CD;
2045 else {
2046 info->flags |= ASYNC_CHECK_CD;
2047 info->IER |= UART_IER_MSI;
2049 outb(info->IER, info->base + UART_IER);
2052 * Set up parity check flag
2054 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
2055 if (I_INPCK(info->tty))
2056 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
2057 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
2058 info->read_status_mask |= UART_LSR_BI;
2060 info->ignore_status_mask = 0;
2061 #if 0
2062 /* This should be safe, but for some broken bits of hardware... */
2063 if (I_IGNPAR(info->tty)) {
2064 info->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
2065 info->read_status_mask |= UART_LSR_PE | UART_LSR_FE;
2067 #endif
2068 if (I_IGNBRK(info->tty)) {
2069 info->ignore_status_mask |= UART_LSR_BI;
2070 info->read_status_mask |= UART_LSR_BI;
2072 * If we're ignore parity and break indicators, ignore
2073 * overruns too. (For real raw support).
2075 if (I_IGNPAR(info->tty)) {
2076 info->ignore_status_mask |= UART_LSR_OE | UART_LSR_PE | UART_LSR_FE;
2077 info->read_status_mask |= UART_LSR_OE | UART_LSR_PE | UART_LSR_FE;
2080 save_flags(flags);
2081 cli();
2082 outb(cval | UART_LCR_DLAB, info->base + UART_LCR); /* set DLAB */
2083 outb(quot & 0xff, info->base + UART_DLL); /* LS of divisor */
2084 outb(quot >> 8, info->base + UART_DLM); /* MS of divisor */
2085 outb(cval, info->base + UART_LCR); /* reset DLAB */
2086 outb(fcr, info->base + UART_FCR); /* set fcr */
2087 restore_flags(flags);
2089 return ret;
2093 * ------------------------------------------------------------
2094 * friends of mxser_ioctl()
2095 * ------------------------------------------------------------
2097 static int mxser_get_serial_info(struct mxser_struct *info,
2098 struct serial_struct *retinfo)
2100 struct serial_struct tmp;
2102 if (!retinfo)
2103 return (-EFAULT);
2104 memset(&tmp, 0, sizeof(tmp));
2105 tmp.type = info->type;
2106 tmp.line = info->port;
2107 tmp.port = info->base;
2108 tmp.irq = info->irq;
2109 tmp.flags = info->flags;
2110 tmp.baud_base = info->baud_base;
2111 tmp.close_delay = info->close_delay;
2112 tmp.closing_wait = info->closing_wait;
2113 tmp.custom_divisor = info->custom_divisor;
2114 tmp.hub6 = 0;
2115 copy_to_user(retinfo, &tmp, sizeof(*retinfo));
2116 return (0);
2119 static int mxser_set_serial_info(struct mxser_struct *info,
2120 struct serial_struct *new_info)
2122 struct serial_struct new_serial;
2123 unsigned int flags;
2124 int retval = 0;
2126 if (!new_info || !info->base)
2127 return (-EFAULT);
2128 copy_from_user(&new_serial, new_info, sizeof(new_serial));
2130 if ((new_serial.irq != info->irq) ||
2131 (new_serial.port != info->base) ||
2132 (new_serial.type != info->type) ||
2133 (new_serial.custom_divisor != info->custom_divisor) ||
2134 (new_serial.baud_base != info->baud_base))
2135 return (-EPERM);
2137 flags = info->flags & ASYNC_SPD_MASK;
2139 if (!suser()) {
2140 if ((new_serial.baud_base != info->baud_base) ||
2141 (new_serial.close_delay != info->close_delay) ||
2142 ((new_serial.flags & ~ASYNC_USR_MASK) !=
2143 (info->flags & ~ASYNC_USR_MASK)))
2144 return (-EPERM);
2145 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
2146 (new_serial.flags & ASYNC_USR_MASK));
2147 } else {
2149 * OK, past this point, all the error checking has been done.
2150 * At this point, we start making changes.....
2152 info->flags = ((info->flags & ~ASYNC_FLAGS) |
2153 (new_serial.flags & ASYNC_FLAGS));
2154 info->close_delay = new_serial.close_delay * HZ / 100;
2155 info->closing_wait = new_serial.closing_wait * HZ / 100;
2158 if (info->flags & ASYNC_INITIALIZED) {
2159 if (flags != (info->flags & ASYNC_SPD_MASK)) {
2160 mxser_change_speed(info, 0);
2162 } else
2163 retval = mxser_startup(info);
2164 return (retval);
2168 * mxser_get_lsr_info - get line status register info
2170 * Purpose: Let user call ioctl() to get info when the UART physically
2171 * is emptied. On bus types like RS485, the transmitter must
2172 * release the bus after transmitting. This must be done when
2173 * the transmit shift register is empty, not be done when the
2174 * transmit holding register is empty. This functionality
2175 * allows an RS485 driver to be written in user space.
2177 static int mxser_get_lsr_info(struct mxser_struct *info, unsigned int *value)
2179 unsigned char status;
2180 unsigned int result;
2181 unsigned long flags;
2183 save_flags(flags);
2184 cli();
2185 status = inb(info->base + UART_LSR);
2186 restore_flags(flags);
2187 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
2188 put_user(result, value);
2189 return (0);
2193 * This routine sends a break character out the serial port.
2195 static void mxser_send_break(struct mxser_struct *info, int duration)
2197 unsigned long flags;
2198 if (!info->base)
2199 return;
2200 current->state = TASK_INTERRUPTIBLE;
2201 save_flags(flags);
2202 cli();
2203 outb(inb(info->base + UART_LCR) | UART_LCR_SBC, info->base + UART_LCR);
2204 schedule_timeout(duration);
2205 outb(inb(info->base + UART_LCR) & ~UART_LCR_SBC, info->base + UART_LCR);
2206 restore_flags(flags);
2209 static int mxser_get_modem_info(struct mxser_struct *info,
2210 unsigned int *value)
2212 unsigned char control, status;
2213 unsigned int result;
2214 unsigned long flags;
2216 control = info->MCR;
2217 save_flags(flags);
2218 cli();
2219 status = inb(info->base + UART_MSR);
2220 if (status & UART_MSR_ANY_DELTA)
2221 mxser_check_modem_status(info, status);
2222 restore_flags(flags);
2223 result = ((control & UART_MCR_RTS) ? TIOCM_RTS : 0) |
2224 ((control & UART_MCR_DTR) ? TIOCM_DTR : 0) |
2225 ((status & UART_MSR_DCD) ? TIOCM_CAR : 0) |
2226 ((status & UART_MSR_RI) ? TIOCM_RNG : 0) |
2227 ((status & UART_MSR_DSR) ? TIOCM_DSR : 0) |
2228 ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
2229 put_user(result, value);
2230 return (0);
2233 static int mxser_set_modem_info(struct mxser_struct *info, unsigned int cmd,
2234 unsigned int *value)
2236 unsigned int arg;
2237 unsigned long flags;
2239 if(get_user(arg, value))
2240 return -EFAULT;
2241 switch (cmd) {
2242 case TIOCMBIS:
2243 if (arg & TIOCM_RTS)
2244 info->MCR |= UART_MCR_RTS;
2245 if (arg & TIOCM_DTR)
2246 info->MCR |= UART_MCR_DTR;
2247 break;
2248 case TIOCMBIC:
2249 if (arg & TIOCM_RTS)
2250 info->MCR &= ~UART_MCR_RTS;
2251 if (arg & TIOCM_DTR)
2252 info->MCR &= ~UART_MCR_DTR;
2253 break;
2254 case TIOCMSET:
2255 info->MCR = ((info->MCR & ~(UART_MCR_RTS | UART_MCR_DTR)) |
2256 ((arg & TIOCM_RTS) ? UART_MCR_RTS : 0) |
2257 ((arg & TIOCM_DTR) ? UART_MCR_DTR : 0));
2258 break;
2259 default:
2260 return (-EINVAL);
2262 save_flags(flags);
2263 cli();
2264 outb(info->MCR, info->base + UART_MCR);
2265 restore_flags(flags);
2266 return (0);
2269 static int mxser_read_register(int, unsigned short *);
2270 static int mxser_program_mode(int);
2271 static void mxser_normal_mode(int);
2273 static int mxser_get_ISA_conf(int cap, struct mxser_hwconf *hwconf)
2275 int id, i, bits;
2276 unsigned short regs[16], irq;
2277 unsigned char scratch, scratch2;
2279 id = mxser_read_register(cap, regs);
2280 if (id == C168_ASIC_ID)
2281 hwconf->board_type = MXSER_BOARD_C168_ISA;
2282 else if (id == C104_ASIC_ID)
2283 hwconf->board_type = MXSER_BOARD_C104_ISA;
2284 else if (id == CI104J_ASIC_ID)
2285 hwconf->board_type = MXSER_BOARD_CI104J;
2286 else
2287 return (0);
2288 irq = regs[9] & 0x0F;
2289 irq = irq | (irq << 4);
2290 irq = irq | (irq << 8);
2291 if ((irq != regs[9]) || ((id == 1) && (irq != regs[10]))) {
2292 return (MXSER_ERR_IRQ_CONFLIT);
2294 if (!irq) {
2295 return (MXSER_ERR_IRQ);
2297 for (i = 0; i < 8; i++)
2298 hwconf->ioaddr[i] = (int) regs[i + 1] & 0xFFF8;
2299 hwconf->irq = (int) (irq & 0x0F);
2300 if ((regs[12] & 0x80) == 0) {
2301 return (MXSER_ERR_VECTOR);
2303 hwconf->vector = (int) regs[11]; /* interrupt vector */
2304 if (id == 1)
2305 hwconf->vector_mask = 0x00FF;
2306 else
2307 hwconf->vector_mask = 0x000F;
2308 for (i = 7, bits = 0x0100; i >= 0; i--, bits <<= 1) {
2309 if (regs[12] & bits)
2310 hwconf->baud_base[i] = 921600;
2311 else
2312 hwconf->baud_base[i] = 115200;
2314 scratch2 = inb(cap + UART_LCR) & (~UART_LCR_DLAB);
2315 outb(scratch2 | UART_LCR_DLAB, cap + UART_LCR);
2316 outb(0, cap + UART_EFR); /* EFR is the same as FCR */
2317 outb(scratch2, cap + UART_LCR);
2318 outb(UART_FCR_ENABLE_FIFO, cap + UART_FCR);
2319 scratch = inb(cap + UART_IIR);
2320 if (scratch & 0xC0)
2321 hwconf->uart_type = PORT_16550A;
2322 else
2323 hwconf->uart_type = PORT_16450;
2324 if (id == 1)
2325 hwconf->ports = 8;
2326 else
2327 hwconf->ports = 4;
2328 return (hwconf->ports);
2331 #define CHIP_SK 0x01 /* Serial Data Clock in Eprom */
2332 #define CHIP_DO 0x02 /* Serial Data Output in Eprom */
2333 #define CHIP_CS 0x04 /* Serial Chip Select in Eprom */
2334 #define CHIP_DI 0x08 /* Serial Data Input in Eprom */
2335 #define EN_CCMD 0x000 /* Chip's command register */
2336 #define EN0_RSARLO 0x008 /* Remote start address reg 0 */
2337 #define EN0_RSARHI 0x009 /* Remote start address reg 1 */
2338 #define EN0_RCNTLO 0x00A /* Remote byte count reg WR */
2339 #define EN0_RCNTHI 0x00B /* Remote byte count reg WR */
2340 #define EN0_DCFG 0x00E /* Data configuration reg WR */
2341 #define EN0_PORT 0x010 /* Rcv missed frame error counter RD */
2342 #define ENC_PAGE0 0x000 /* Select page 0 of chip registers */
2343 #define ENC_PAGE3 0x0C0 /* Select page 3 of chip registers */
2344 static int mxser_read_register(int port, unsigned short *regs)
2346 int i, k, value, id;
2347 unsigned int j;
2349 id = mxser_program_mode(port);
2350 if (id < 0)
2351 return (id);
2352 for (i = 0; i < 14; i++) {
2353 k = (i & 0x3F) | 0x180;
2354 for (j = 0x100; j > 0; j >>= 1) {
2355 outb(CHIP_CS, port);
2356 if (k & j) {
2357 outb(CHIP_CS | CHIP_DO, port);
2358 outb(CHIP_CS | CHIP_DO | CHIP_SK, port); /* A? bit of read */
2359 } else {
2360 outb(CHIP_CS, port);
2361 outb(CHIP_CS | CHIP_SK, port); /* A? bit of read */
2364 (void) inb(port);
2365 value = 0;
2366 for (k = 0, j = 0x8000; k < 16; k++, j >>= 1) {
2367 outb(CHIP_CS, port);
2368 outb(CHIP_CS | CHIP_SK, port);
2369 if (inb(port) & CHIP_DI)
2370 value |= j;
2372 regs[i] = value;
2373 outb(0, port);
2375 mxser_normal_mode(port);
2376 return (id);
2379 static int mxser_program_mode(int port)
2381 int id, i, j, n;
2382 unsigned long flags;
2384 save_flags(flags);
2385 cli();
2386 outb(0, port);
2387 outb(0, port);
2388 outb(0, port);
2389 (void) inb(port);
2390 (void) inb(port);
2391 outb(0, port);
2392 (void) inb(port);
2393 restore_flags(flags);
2394 id = inb(port + 1) & 0x1F;
2395 if ((id != C168_ASIC_ID) && (id != C104_ASIC_ID) && (id != CI104J_ASIC_ID))
2396 return (-1);
2397 for (i = 0, j = 0; i < 4; i++) {
2398 n = inb(port + 2);
2399 if (n == 'M') {
2400 j = 1;
2401 } else if ((j == 1) && (n == 1)) {
2402 j = 2;
2403 break;
2404 } else
2405 j = 0;
2407 if (j != 2)
2408 id = -2;
2409 return (id);
2412 static void mxser_normal_mode(int port)
2414 int i, n;
2416 outb(0xA5, port + 1);
2417 outb(0x80, port + 3);
2418 outb(12, port + 0); /* 9600 bps */
2419 outb(0, port + 1);
2420 outb(0x03, port + 3); /* 8 data bits */
2421 outb(0x13, port + 4); /* loop back mode */
2422 for (i = 0; i < 16; i++) {
2423 n = inb(port + 5);
2424 if ((n & 0x61) == 0x60)
2425 break;
2426 if ((n & 1) == 1)
2427 (void) inb(port);
2429 outb(0x00, port + 4);