blob: f078ddf48b791ae6b4152ac0bbb753968558ca7b [file] [log] [blame]
Jiri Slaby037ad482006-12-08 02:38:11 -08001/*
2 * mxser.c -- MOXA Smartio/Industio family multiport serial driver.
3 *
Jiri Slaby3306ce32006-12-08 02:38:13 -08004 * Copyright (C) 1999-2006 Moxa Technologies (support@moxa.com.tw).
Jiri Slabyc88cb8f92006-12-08 02:38:20 -08005 * Copyright (C) 2006 Jiri Slaby <jirislaby@gmail.com>
Jiri Slaby037ad482006-12-08 02:38:11 -08006 *
Jiri Slabyc88cb8f92006-12-08 02:38:20 -08007 * This code is loosely based on the 1.8 moxa driver which is based on
8 * Linux serial driver, written by Linus Torvalds, Theodore T'so and
9 * others.
Jiri Slaby037ad482006-12-08 02:38:11 -080010 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
Jiri Slaby037ad482006-12-08 02:38:11 -080016 * Fed through a cleanup, indent and remove of non 2.6 code by Alan Cox
17 * <alan@redhat.com>. The original 1.8 code is available on www.moxa.com.
18 * - Fixed x86_64 cleanness
19 * - Fixed sleep with spinlock held in mxser_send_break
20 */
21
Jiri Slaby037ad482006-12-08 02:38:11 -080022#include <linux/module.h>
23#include <linux/autoconf.h>
24#include <linux/errno.h>
25#include <linux/signal.h>
26#include <linux/sched.h>
27#include <linux/timer.h>
28#include <linux/interrupt.h>
29#include <linux/tty.h>
30#include <linux/tty_flip.h>
31#include <linux/serial.h>
32#include <linux/serial_reg.h>
33#include <linux/major.h>
34#include <linux/string.h>
35#include <linux/fcntl.h>
36#include <linux/ptrace.h>
37#include <linux/gfp.h>
38#include <linux/ioport.h>
39#include <linux/mm.h>
40#include <linux/smp_lock.h>
41#include <linux/delay.h>
42#include <linux/pci.h>
43
44#include <asm/system.h>
45#include <asm/io.h>
46#include <asm/irq.h>
47#include <asm/bitops.h>
48#include <asm/uaccess.h>
49
Jiri Slaby771f2d12006-12-08 02:38:12 -080050#include "mxser_new.h"
Jiri Slaby037ad482006-12-08 02:38:11 -080051
Jiri Slaby55b307d2006-12-08 02:38:14 -080052#define MXSER_VERSION "2.0"
Jiri Slaby037ad482006-12-08 02:38:11 -080053#define MXSERMAJOR 174
54#define MXSERCUMAJOR 175
55
Jiri Slaby037ad482006-12-08 02:38:11 -080056#define MXSER_BOARDS 4 /* Max. boards */
Jiri Slaby037ad482006-12-08 02:38:11 -080057#define MXSER_PORTS_PER_BOARD 8 /* Max. ports per board */
Jiri Slabya8b74de2006-12-08 02:38:34 -080058#define MXSER_PORTS (MXSER_BOARDS * MXSER_PORTS_PER_BOARD)
Jiri Slaby18b95572007-02-10 01:45:19 -080059#define MXSER_ISR_PASS_LIMIT 100
Jiri Slaby037ad482006-12-08 02:38:11 -080060
61#define MXSER_ERR_IOADDR -1
62#define MXSER_ERR_IRQ -2
63#define MXSER_ERR_IRQ_CONFLIT -3
64#define MXSER_ERR_VECTOR -4
65
Jiri Slabyed79ba12007-02-10 01:45:18 -080066/*CheckIsMoxaMust return value*/
67#define MOXA_OTHER_UART 0x00
68#define MOXA_MUST_MU150_HWID 0x01
69#define MOXA_MUST_MU860_HWID 0x02
70
Jiri Slaby037ad482006-12-08 02:38:11 -080071#define WAKEUP_CHARS 256
72
73#define UART_MCR_AFE 0x20
74#define UART_LSR_SPECIAL 0x1E
75
76#define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK|\
77 IXON|IXOFF))
78
Jiri Slaby037ad482006-12-08 02:38:11 -080079#define C168_ASIC_ID 1
80#define C104_ASIC_ID 2
81#define C102_ASIC_ID 0xB
82#define CI132_ASIC_ID 4
83#define CI134_ASIC_ID 3
84#define CI104J_ASIC_ID 5
85
Jiri Slabycd7ed642006-12-08 02:38:29 -080086#define MXSER_HIGHBAUD 1
87#define MXSER_HAS2 2
Jiri Slaby037ad482006-12-08 02:38:11 -080088
89/* This is only for PCI */
Jiri Slabycd7ed642006-12-08 02:38:29 -080090static const struct {
Jiri Slaby037ad482006-12-08 02:38:11 -080091 int type;
92 int tx_fifo;
93 int rx_fifo;
94 int xmit_fifo_size;
95 int rx_high_water;
96 int rx_trigger;
97 int rx_low_water;
98 long max_baud;
Jiri Slabycd7ed642006-12-08 02:38:29 -080099} Gpci_uart_info[] = {
Jiri Slaby037ad482006-12-08 02:38:11 -0800100 {MOXA_OTHER_UART, 16, 16, 16, 14, 14, 1, 921600L},
101 {MOXA_MUST_MU150_HWID, 64, 64, 64, 48, 48, 16, 230400L},
102 {MOXA_MUST_MU860_HWID, 128, 128, 128, 96, 96, 32, 921600L}
103};
Jiri Slabycd7ed642006-12-08 02:38:29 -0800104#define UART_INFO_NUM ARRAY_SIZE(Gpci_uart_info)
Jiri Slaby037ad482006-12-08 02:38:11 -0800105
Jiri Slabycd7ed642006-12-08 02:38:29 -0800106struct mxser_cardinfo {
107 unsigned int nports;
108 char *name;
109 unsigned int flags;
110};
111
112static const struct mxser_cardinfo mxser_cards[] = {
113 { 8, "C168 series", }, /* C168-ISA */
114 { 4, "C104 series", }, /* C104-ISA */
115 { 4, "CI-104J series", }, /* CI104J */
116 { 8, "C168H/PCI series", }, /* C168-PCI */
117 { 4, "C104H/PCI series", }, /* C104-PCI */
118 { 4, "C102 series", MXSER_HAS2 }, /* C102-ISA */
119 { 4, "CI-132 series", MXSER_HAS2 }, /* CI132 */
120 { 4, "CI-134 series", }, /* CI134 */
121 { 2, "CP-132 series", }, /* CP132 */
122 { 4, "CP-114 series", }, /* CP114 */
123 { 4, "CT-114 series", }, /* CT114 */
124 { 2, "CP-102 series", MXSER_HIGHBAUD }, /* CP102 */
125 { 4, "CP-104U series", }, /* CP104U */
126 { 8, "CP-168U series", }, /* CP168U */
127 { 2, "CP-132U series", }, /* CP132U */
128 { 4, "CP-134U series", }, /* CP134U */
129 { 4, "CP-104JU series", }, /* CP104JU */
130 { 8, "Moxa UC7000 Serial", }, /* RC7000 */
131 { 8, "CP-118U series", }, /* CP118U */
132 { 2, "CP-102UL series", }, /* CP102UL */
133 { 2, "CP-102U series", }, /* CP102U */
134 { 8, "CP-118EL series", }, /* CP118EL */
135 { 8, "CP-168EL series", }, /* CP168EL */
136 { 4, "CP-104EL series", } /* CP104EL */
137};
138
139/* driver_data correspond to the lines in the structure above
140 see also ISA probe function before you change something */
Jiri Slaby037ad482006-12-08 02:38:11 -0800141static struct pci_device_id mxser_pcibrds[] = {
Jiri Slaby3306ce32006-12-08 02:38:13 -0800142 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C168),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800143 .driver_data = 3 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800144 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C104),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800145 .driver_data = 4 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800146 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP132),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800147 .driver_data = 8 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800148 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP114),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800149 .driver_data = 9 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800150 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CT114),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800151 .driver_data = 10 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800152 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP102),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800153 .driver_data = 11 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800154 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP104U),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800155 .driver_data = 12 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800156 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP168U),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800157 .driver_data = 13 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800158 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP132U),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800159 .driver_data = 14 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800160 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP134U),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800161 .driver_data = 15 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800162 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP104JU),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800163 .driver_data = 16 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800164 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_RC7000),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800165 .driver_data = 17 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800166 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP118U),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800167 .driver_data = 18 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800168 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP102UL),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800169 .driver_data = 19 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800170 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP102U),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800171 .driver_data = 20 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800172 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP118EL),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800173 .driver_data = 21 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800174 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP168EL),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800175 .driver_data = 22 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800176 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP104EL),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800177 .driver_data = 23 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800178 { }
Jiri Slaby037ad482006-12-08 02:38:11 -0800179};
Jiri Slaby037ad482006-12-08 02:38:11 -0800180MODULE_DEVICE_TABLE(pci, mxser_pcibrds);
181
Jiri Slaby037ad482006-12-08 02:38:11 -0800182static int ioaddr[MXSER_BOARDS] = { 0, 0, 0, 0 };
183static int ttymajor = MXSERMAJOR;
184static int calloutmajor = MXSERCUMAJOR;
Jiri Slaby037ad482006-12-08 02:38:11 -0800185
186/* Variables for insmod */
187
188MODULE_AUTHOR("Casper Yang");
189MODULE_DESCRIPTION("MOXA Smartio/Industio Family Multiport Board Device Driver");
190module_param_array(ioaddr, int, NULL, 0);
191module_param(ttymajor, int, 0);
Jiri Slaby037ad482006-12-08 02:38:11 -0800192MODULE_LICENSE("GPL");
193
194struct mxser_log {
195 int tick;
196 unsigned long rxcnt[MXSER_PORTS];
197 unsigned long txcnt[MXSER_PORTS];
198};
199
200
201struct mxser_mon {
202 unsigned long rxcnt;
203 unsigned long txcnt;
204 unsigned long up_rxcnt;
205 unsigned long up_txcnt;
206 int modem_status;
207 unsigned char hold_reason;
208};
209
210struct mxser_mon_ext {
211 unsigned long rx_cnt[32];
212 unsigned long tx_cnt[32];
213 unsigned long up_rxcnt[32];
214 unsigned long up_txcnt[32];
215 int modem_status[32];
216
217 long baudrate[32];
218 int databits[32];
219 int stopbits[32];
220 int parity[32];
221 int flowctrl[32];
222 int fifo[32];
223 int iftype[32];
224};
225
Jiri Slaby55b307d2006-12-08 02:38:14 -0800226struct mxser_board;
Jiri Slaby037ad482006-12-08 02:38:11 -0800227
Jiri Slaby55b307d2006-12-08 02:38:14 -0800228struct mxser_port {
229 struct mxser_board *board;
230 struct tty_struct *tty;
231
232 unsigned long ioaddr;
233 unsigned long opmode_ioaddr;
234 int max_baud;
235
Jiri Slaby037ad482006-12-08 02:38:11 -0800236 int rx_high_water;
237 int rx_trigger; /* Rx fifo trigger level */
238 int rx_low_water;
239 int baud_base; /* max. speed */
Jiri Slaby55b307d2006-12-08 02:38:14 -0800240 long realbaud;
Jiri Slaby037ad482006-12-08 02:38:11 -0800241 int type; /* UART type */
Jiri Slaby55b307d2006-12-08 02:38:14 -0800242 int flags; /* defined in tty.h */
Jiri Slaby55b307d2006-12-08 02:38:14 -0800243
244 int x_char; /* xon/xoff character */
245 int IER; /* Interrupt Enable Register */
246 int MCR; /* Modem control register */
247
248 unsigned char stop_rx;
249 unsigned char ldisc_stop_rx;
250
251 int custom_divisor;
252 int close_delay;
253 unsigned short closing_wait;
254 unsigned char err_shadow;
255 unsigned long event;
256
257 int count; /* # of fd on device */
258 int blocked_open; /* # of blocked opens */
259 struct async_icount icount; /* kernel counters for 4 input interrupts */
260 int timeout;
261
Jiri Slaby037ad482006-12-08 02:38:11 -0800262 int read_status_mask;
263 int ignore_status_mask;
264 int xmit_fifo_size;
Jiri Slaby037ad482006-12-08 02:38:11 -0800265 unsigned char *xmit_buf;
266 int xmit_head;
267 int xmit_tail;
268 int xmit_cnt;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800269
Alan Cox606d0992006-12-08 02:38:45 -0800270 struct ktermios normal_termios;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800271
272 struct mxser_mon mon_data;
273
274 spinlock_t slock;
Jiri Slaby037ad482006-12-08 02:38:11 -0800275 wait_queue_head_t open_wait;
Jiri Slaby037ad482006-12-08 02:38:11 -0800276 wait_queue_head_t delta_msr_wait;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800277};
278
279struct mxser_board {
Jiri Slaby2094e752006-12-08 02:38:33 -0800280 unsigned int idx;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800281 int irq;
Jiri Slabycd7ed642006-12-08 02:38:29 -0800282 const struct mxser_cardinfo *info;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800283 unsigned long vector;
284 unsigned long vector_mask;
285
286 int chip_flag;
287 int uart_type;
288
289 struct mxser_port ports[MXSER_PORTS_PER_BOARD];
Jiri Slaby037ad482006-12-08 02:38:11 -0800290};
291
292struct mxser_mstatus {
293 tcflag_t cflag;
294 int cts;
295 int dsr;
296 int ri;
297 int dcd;
298};
299
300static struct mxser_mstatus GMStatus[MXSER_PORTS];
301
302static int mxserBoardCAP[MXSER_BOARDS] = {
303 0, 0, 0, 0
304 /* 0x180, 0x280, 0x200, 0x320 */
305};
306
Jiri Slaby55b307d2006-12-08 02:38:14 -0800307static struct mxser_board mxser_boards[MXSER_BOARDS];
Jiri Slaby037ad482006-12-08 02:38:11 -0800308static struct tty_driver *mxvar_sdriver;
Jiri Slaby037ad482006-12-08 02:38:11 -0800309static struct mxser_log mxvar_log;
310static int mxvar_diagflag;
311static unsigned char mxser_msr[MXSER_PORTS + 1];
312static struct mxser_mon_ext mon_data_ext;
313static int mxser_set_baud_method[MXSER_PORTS + 1];
Jiri Slaby037ad482006-12-08 02:38:11 -0800314
Jiri Slabyfb0c9292006-12-13 00:34:19 -0800315#ifdef CONFIG_PCI
Jiri Slabyc76b0932007-02-10 01:45:07 -0800316static int __devinit CheckIsMoxaMust(int io)
Jiri Slaby037ad482006-12-08 02:38:11 -0800317{
318 u8 oldmcr, hwid;
319 int i;
320
321 outb(0, io + UART_LCR);
322 DISABLE_MOXA_MUST_ENCHANCE_MODE(io);
323 oldmcr = inb(io + UART_MCR);
324 outb(0, io + UART_MCR);
325 SET_MOXA_MUST_XON1_VALUE(io, 0x11);
326 if ((hwid = inb(io + UART_MCR)) != 0) {
327 outb(oldmcr, io + UART_MCR);
328 return MOXA_OTHER_UART;
329 }
330
331 GET_MOXA_MUST_HARDWARE_ID(io, &hwid);
Jiri Slabycd7ed642006-12-08 02:38:29 -0800332 for (i = 1; i < UART_INFO_NUM; i++) { /* 0 = OTHER_UART */
333 if (hwid == Gpci_uart_info[i].type)
Jiri Slaby037ad482006-12-08 02:38:11 -0800334 return (int)hwid;
335 }
336 return MOXA_OTHER_UART;
337}
Jiri Slabyfb0c9292006-12-13 00:34:19 -0800338#endif
Jiri Slaby037ad482006-12-08 02:38:11 -0800339
Jiri Slaby55b307d2006-12-08 02:38:14 -0800340static void process_txrx_fifo(struct mxser_port *info)
Jiri Slaby037ad482006-12-08 02:38:11 -0800341{
342 int i;
343
344 if ((info->type == PORT_16450) || (info->type == PORT_8250)) {
345 info->rx_trigger = 1;
346 info->rx_high_water = 1;
347 info->rx_low_water = 1;
348 info->xmit_fifo_size = 1;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800349 } else
350 for (i = 0; i < UART_INFO_NUM; i++)
351 if (info->board->chip_flag == Gpci_uart_info[i].type) {
Jiri Slaby037ad482006-12-08 02:38:11 -0800352 info->rx_trigger = Gpci_uart_info[i].rx_trigger;
353 info->rx_low_water = Gpci_uart_info[i].rx_low_water;
354 info->rx_high_water = Gpci_uart_info[i].rx_high_water;
355 info->xmit_fifo_size = Gpci_uart_info[i].xmit_fifo_size;
356 break;
357 }
Jiri Slaby037ad482006-12-08 02:38:11 -0800358}
359
Jiri Slaby55b307d2006-12-08 02:38:14 -0800360static unsigned char mxser_get_msr(int baseaddr, int mode, int port)
Jiri Slaby037ad482006-12-08 02:38:11 -0800361{
362 unsigned char status = 0;
363
364 status = inb(baseaddr + UART_MSR);
365
366 mxser_msr[port] &= 0x0F;
367 mxser_msr[port] |= status;
368 status = mxser_msr[port];
369 if (mode)
370 mxser_msr[port] = 0;
371
372 return status;
373}
374
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800375static int mxser_block_til_ready(struct tty_struct *tty, struct file *filp,
376 struct mxser_port *port)
377{
378 DECLARE_WAITQUEUE(wait, current);
379 int retval;
380 int do_clocal = 0;
381 unsigned long flags;
382
383 /*
384 * If non-blocking mode is set, or the port is not enabled,
385 * then make the check up front and then exit.
386 */
Jiri Slaby214efeb2006-12-08 02:38:25 -0800387 if ((filp->f_flags & O_NONBLOCK) ||
388 test_bit(TTY_IO_ERROR, &tty->flags)) {
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800389 port->flags |= ASYNC_NORMAL_ACTIVE;
390 return 0;
391 }
392
393 if (tty->termios->c_cflag & CLOCAL)
394 do_clocal = 1;
395
396 /*
397 * Block waiting for the carrier detect and the line to become
398 * free (i.e., not in use by the callout). While we are in
399 * this loop, port->count is dropped by one, so that
400 * mxser_close() knows when to free things. We restore it upon
401 * exit, either normal or abnormal.
402 */
403 retval = 0;
404 add_wait_queue(&port->open_wait, &wait);
405
406 spin_lock_irqsave(&port->slock, flags);
407 if (!tty_hung_up_p(filp))
408 port->count--;
409 spin_unlock_irqrestore(&port->slock, flags);
410 port->blocked_open++;
411 while (1) {
412 spin_lock_irqsave(&port->slock, flags);
413 outb(inb(port->ioaddr + UART_MCR) |
414 UART_MCR_DTR | UART_MCR_RTS, port->ioaddr + UART_MCR);
415 spin_unlock_irqrestore(&port->slock, flags);
416 set_current_state(TASK_INTERRUPTIBLE);
417 if (tty_hung_up_p(filp) || !(port->flags & ASYNC_INITIALIZED)) {
418 if (port->flags & ASYNC_HUP_NOTIFY)
419 retval = -EAGAIN;
420 else
421 retval = -ERESTARTSYS;
422 break;
423 }
424 if (!(port->flags & ASYNC_CLOSING) &&
425 (do_clocal ||
426 (inb(port->ioaddr + UART_MSR) & UART_MSR_DCD)))
427 break;
428 if (signal_pending(current)) {
429 retval = -ERESTARTSYS;
430 break;
431 }
432 schedule();
433 }
434 set_current_state(TASK_RUNNING);
435 remove_wait_queue(&port->open_wait, &wait);
436 if (!tty_hung_up_p(filp))
437 port->count++;
438 port->blocked_open--;
439 if (retval)
440 return retval;
441 port->flags |= ASYNC_NORMAL_ACTIVE;
442 return 0;
443}
444
445static int mxser_set_baud(struct mxser_port *info, long newspd)
446{
447 int quot = 0;
448 unsigned char cval;
449 int ret = 0;
450 unsigned long flags;
451
452 if (!info->tty || !info->tty->termios)
453 return ret;
454
455 if (!(info->ioaddr))
456 return ret;
457
458 if (newspd > info->max_baud)
459 return 0;
460
461 info->realbaud = newspd;
462 if (newspd == 134) {
463 quot = (2 * info->baud_base / 269);
464 } else if (newspd) {
465 quot = info->baud_base / newspd;
466 if (quot == 0)
467 quot = 1;
468 } else {
469 quot = 0;
470 }
471
472 info->timeout = ((info->xmit_fifo_size * HZ * 10 * quot) / info->baud_base);
473 info->timeout += HZ / 50; /* Add .02 seconds of slop */
474
475 if (quot) {
476 spin_lock_irqsave(&info->slock, flags);
477 info->MCR |= UART_MCR_DTR;
478 outb(info->MCR, info->ioaddr + UART_MCR);
479 spin_unlock_irqrestore(&info->slock, flags);
480 } else {
481 spin_lock_irqsave(&info->slock, flags);
482 info->MCR &= ~UART_MCR_DTR;
483 outb(info->MCR, info->ioaddr + UART_MCR);
484 spin_unlock_irqrestore(&info->slock, flags);
485 return ret;
486 }
487
488 cval = inb(info->ioaddr + UART_LCR);
489
490 outb(cval | UART_LCR_DLAB, info->ioaddr + UART_LCR); /* set DLAB */
491
492 outb(quot & 0xff, info->ioaddr + UART_DLL); /* LS of divisor */
493 outb(quot >> 8, info->ioaddr + UART_DLM); /* MS of divisor */
494 outb(cval, info->ioaddr + UART_LCR); /* reset DLAB */
495
496
497 return ret;
498}
499
500/*
501 * This routine is called to set the UART divisor registers to match
502 * the specified baud rate for a serial port.
503 */
504static int mxser_change_speed(struct mxser_port *info,
Alan Cox606d0992006-12-08 02:38:45 -0800505 struct ktermios *old_termios)
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800506{
507 unsigned cflag, cval, fcr;
508 int ret = 0;
509 unsigned char status;
510 long baud;
511 unsigned long flags;
512
513 if (!info->tty || !info->tty->termios)
514 return ret;
515 cflag = info->tty->termios->c_cflag;
516 if (!(info->ioaddr))
517 return ret;
518
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800519 if (mxser_set_baud_method[info->tty->index] == 0) {
520 baud = tty_get_baud_rate(info->tty);
521 mxser_set_baud(info, baud);
522 }
523
524 /* byte size and parity */
525 switch (cflag & CSIZE) {
526 case CS5:
527 cval = 0x00;
528 break;
529 case CS6:
530 cval = 0x01;
531 break;
532 case CS7:
533 cval = 0x02;
534 break;
535 case CS8:
536 cval = 0x03;
537 break;
538 default:
539 cval = 0x00;
540 break; /* too keep GCC shut... */
541 }
542 if (cflag & CSTOPB)
543 cval |= 0x04;
544 if (cflag & PARENB)
545 cval |= UART_LCR_PARITY;
546 if (!(cflag & PARODD))
547 cval |= UART_LCR_EPAR;
548 if (cflag & CMSPAR)
549 cval |= UART_LCR_SPAR;
550
551 if ((info->type == PORT_8250) || (info->type == PORT_16450)) {
552 if (info->board->chip_flag) {
553 fcr = UART_FCR_ENABLE_FIFO;
554 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
555 SET_MOXA_MUST_FIFO_VALUE(info);
556 } else
557 fcr = 0;
558 } else {
559 fcr = UART_FCR_ENABLE_FIFO;
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800560 if (info->board->chip_flag) {
561 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
562 SET_MOXA_MUST_FIFO_VALUE(info);
563 } else {
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800564 switch (info->rx_trigger) {
565 case 1:
566 fcr |= UART_FCR_TRIGGER_1;
567 break;
568 case 4:
569 fcr |= UART_FCR_TRIGGER_4;
570 break;
571 case 8:
572 fcr |= UART_FCR_TRIGGER_8;
573 break;
574 default:
575 fcr |= UART_FCR_TRIGGER_14;
576 break;
577 }
578 }
579 }
580
581 /* CTS flow control flag and modem status interrupts */
582 info->IER &= ~UART_IER_MSI;
583 info->MCR &= ~UART_MCR_AFE;
584 if (cflag & CRTSCTS) {
585 info->flags |= ASYNC_CTS_FLOW;
586 info->IER |= UART_IER_MSI;
587 if ((info->type == PORT_16550A) || (info->board->chip_flag)) {
588 info->MCR |= UART_MCR_AFE;
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800589 } else {
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800590 status = inb(info->ioaddr + UART_MSR);
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800591 if (info->tty->hw_stopped) {
592 if (status & UART_MSR_CTS) {
593 info->tty->hw_stopped = 0;
594 if (info->type != PORT_16550A &&
595 !info->board->chip_flag) {
596 outb(info->IER & ~UART_IER_THRI,
597 info->ioaddr +
598 UART_IER);
599 info->IER |= UART_IER_THRI;
600 outb(info->IER, info->ioaddr +
601 UART_IER);
602 }
Jiri Slaby47c85c02007-02-10 01:45:15 -0800603 tty_wakeup(info->tty);
604 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800605 } else {
606 if (!(status & UART_MSR_CTS)) {
607 info->tty->hw_stopped = 1;
608 if ((info->type != PORT_16550A) &&
609 (!info->board->chip_flag)) {
610 info->IER &= ~UART_IER_THRI;
611 outb(info->IER, info->ioaddr +
612 UART_IER);
613 }
614 }
615 }
616 }
617 } else {
618 info->flags &= ~ASYNC_CTS_FLOW;
619 }
620 outb(info->MCR, info->ioaddr + UART_MCR);
621 if (cflag & CLOCAL) {
622 info->flags &= ~ASYNC_CHECK_CD;
623 } else {
624 info->flags |= ASYNC_CHECK_CD;
625 info->IER |= UART_IER_MSI;
626 }
627 outb(info->IER, info->ioaddr + UART_IER);
628
629 /*
630 * Set up parity check flag
631 */
632 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
633 if (I_INPCK(info->tty))
634 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
635 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
636 info->read_status_mask |= UART_LSR_BI;
637
638 info->ignore_status_mask = 0;
639
640 if (I_IGNBRK(info->tty)) {
641 info->ignore_status_mask |= UART_LSR_BI;
642 info->read_status_mask |= UART_LSR_BI;
643 /*
644 * If we're ignore parity and break indicators, ignore
645 * overruns too. (For real raw support).
646 */
647 if (I_IGNPAR(info->tty)) {
648 info->ignore_status_mask |=
649 UART_LSR_OE |
650 UART_LSR_PE |
651 UART_LSR_FE;
652 info->read_status_mask |=
653 UART_LSR_OE |
654 UART_LSR_PE |
655 UART_LSR_FE;
656 }
657 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800658 if (info->board->chip_flag) {
659 spin_lock_irqsave(&info->slock, flags);
660 SET_MOXA_MUST_XON1_VALUE(info->ioaddr, START_CHAR(info->tty));
661 SET_MOXA_MUST_XOFF1_VALUE(info->ioaddr, STOP_CHAR(info->tty));
662 if (I_IXON(info->tty)) {
663 ENABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
664 } else {
665 DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
666 }
667 if (I_IXOFF(info->tty)) {
668 ENABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
669 } else {
670 DISABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
671 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800672 spin_unlock_irqrestore(&info->slock, flags);
673 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800674
675
676 outb(fcr, info->ioaddr + UART_FCR); /* set fcr */
677 outb(cval, info->ioaddr + UART_LCR);
678
679 return ret;
680}
681
682static void mxser_check_modem_status(struct mxser_port *port, int status)
683{
684 /* update input line counters */
685 if (status & UART_MSR_TERI)
686 port->icount.rng++;
687 if (status & UART_MSR_DDSR)
688 port->icount.dsr++;
689 if (status & UART_MSR_DDCD)
690 port->icount.dcd++;
691 if (status & UART_MSR_DCTS)
692 port->icount.cts++;
693 port->mon_data.modem_status = status;
694 wake_up_interruptible(&port->delta_msr_wait);
695
696 if ((port->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
697 if (status & UART_MSR_DCD)
698 wake_up_interruptible(&port->open_wait);
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800699 }
700
701 if (port->flags & ASYNC_CTS_FLOW) {
702 if (port->tty->hw_stopped) {
703 if (status & UART_MSR_CTS) {
704 port->tty->hw_stopped = 0;
705
706 if ((port->type != PORT_16550A) &&
707 (!port->board->chip_flag)) {
708 outb(port->IER & ~UART_IER_THRI,
709 port->ioaddr + UART_IER);
710 port->IER |= UART_IER_THRI;
711 outb(port->IER, port->ioaddr +
712 UART_IER);
713 }
Jiri Slaby47c85c02007-02-10 01:45:15 -0800714 tty_wakeup(port->tty);
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800715 }
716 } else {
717 if (!(status & UART_MSR_CTS)) {
718 port->tty->hw_stopped = 1;
719 if (port->type != PORT_16550A &&
720 !port->board->chip_flag) {
721 port->IER &= ~UART_IER_THRI;
722 outb(port->IER, port->ioaddr +
723 UART_IER);
724 }
725 }
726 }
727 }
728}
729
730static int mxser_startup(struct mxser_port *info)
731{
732 unsigned long page;
733 unsigned long flags;
734
735 page = __get_free_page(GFP_KERNEL);
736 if (!page)
737 return -ENOMEM;
738
739 spin_lock_irqsave(&info->slock, flags);
740
741 if (info->flags & ASYNC_INITIALIZED) {
742 free_page(page);
743 spin_unlock_irqrestore(&info->slock, flags);
744 return 0;
745 }
746
747 if (!info->ioaddr || !info->type) {
748 if (info->tty)
749 set_bit(TTY_IO_ERROR, &info->tty->flags);
750 free_page(page);
751 spin_unlock_irqrestore(&info->slock, flags);
752 return 0;
753 }
754 if (info->xmit_buf)
755 free_page(page);
756 else
757 info->xmit_buf = (unsigned char *) page;
758
759 /*
760 * Clear the FIFO buffers and disable them
761 * (they will be reenabled in mxser_change_speed())
762 */
763 if (info->board->chip_flag)
764 outb((UART_FCR_CLEAR_RCVR |
765 UART_FCR_CLEAR_XMIT |
766 MOXA_MUST_FCR_GDA_MODE_ENABLE), info->ioaddr + UART_FCR);
767 else
768 outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
769 info->ioaddr + UART_FCR);
770
771 /*
772 * At this point there's no way the LSR could still be 0xFF;
773 * if it is, then bail out, because there's likely no UART
774 * here.
775 */
776 if (inb(info->ioaddr + UART_LSR) == 0xff) {
777 spin_unlock_irqrestore(&info->slock, flags);
778 if (capable(CAP_SYS_ADMIN)) {
779 if (info->tty)
780 set_bit(TTY_IO_ERROR, &info->tty->flags);
781 return 0;
782 } else
783 return -ENODEV;
784 }
785
786 /*
787 * Clear the interrupt registers.
788 */
789 (void) inb(info->ioaddr + UART_LSR);
790 (void) inb(info->ioaddr + UART_RX);
791 (void) inb(info->ioaddr + UART_IIR);
792 (void) inb(info->ioaddr + UART_MSR);
793
794 /*
795 * Now, initialize the UART
796 */
797 outb(UART_LCR_WLEN8, info->ioaddr + UART_LCR); /* reset DLAB */
798 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
799 outb(info->MCR, info->ioaddr + UART_MCR);
800
801 /*
802 * Finally, enable interrupts
803 */
804 info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800805
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800806 if (info->board->chip_flag)
807 info->IER |= MOXA_MUST_IER_EGDAI;
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800808 outb(info->IER, info->ioaddr + UART_IER); /* enable interrupts */
809
810 /*
811 * And clear the interrupt registers again for luck.
812 */
813 (void) inb(info->ioaddr + UART_LSR);
814 (void) inb(info->ioaddr + UART_RX);
815 (void) inb(info->ioaddr + UART_IIR);
816 (void) inb(info->ioaddr + UART_MSR);
817
818 if (info->tty)
819 clear_bit(TTY_IO_ERROR, &info->tty->flags);
820 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
821
822 /*
823 * and set the speed of the serial port
824 */
825 spin_unlock_irqrestore(&info->slock, flags);
826 mxser_change_speed(info, NULL);
827
828 info->flags |= ASYNC_INITIALIZED;
829 return 0;
830}
831
832/*
833 * This routine will shutdown a serial port; interrupts maybe disabled, and
834 * DTR is dropped if the hangup on close termio flag is on.
835 */
836static void mxser_shutdown(struct mxser_port *info)
837{
838 unsigned long flags;
839
840 if (!(info->flags & ASYNC_INITIALIZED))
841 return;
842
843 spin_lock_irqsave(&info->slock, flags);
844
845 /*
846 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
847 * here so the queue might never be waken up
848 */
849 wake_up_interruptible(&info->delta_msr_wait);
850
851 /*
852 * Free the IRQ, if necessary
853 */
854 if (info->xmit_buf) {
855 free_page((unsigned long) info->xmit_buf);
856 info->xmit_buf = NULL;
857 }
858
859 info->IER = 0;
860 outb(0x00, info->ioaddr + UART_IER);
861
862 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
863 info->MCR &= ~(UART_MCR_DTR | UART_MCR_RTS);
864 outb(info->MCR, info->ioaddr + UART_MCR);
865
866 /* clear Rx/Tx FIFO's */
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800867 if (info->board->chip_flag)
868 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT |
869 MOXA_MUST_FCR_GDA_MODE_ENABLE,
870 info->ioaddr + UART_FCR);
871 else
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800872 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
873 info->ioaddr + UART_FCR);
874
875 /* read data port to reset things */
876 (void) inb(info->ioaddr + UART_RX);
877
878 if (info->tty)
879 set_bit(TTY_IO_ERROR, &info->tty->flags);
880
881 info->flags &= ~ASYNC_INITIALIZED;
882
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800883 if (info->board->chip_flag)
884 SET_MOXA_MUST_NO_SOFTWARE_FLOW_CONTROL(info->ioaddr);
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800885
886 spin_unlock_irqrestore(&info->slock, flags);
887}
888
Jiri Slaby037ad482006-12-08 02:38:11 -0800889/*
890 * This routine is called whenever a serial port is opened. It
891 * enables interrupts for a serial port, linking in its async structure into
892 * the IRQ chain. It also performs the serial-specific
893 * initialization for the tty structure.
894 */
895static int mxser_open(struct tty_struct *tty, struct file *filp)
896{
Jiri Slaby55b307d2006-12-08 02:38:14 -0800897 struct mxser_port *info;
Jiri Slaby037ad482006-12-08 02:38:11 -0800898 int retval, line;
899
900 /* initialize driver_data in case something fails */
901 tty->driver_data = NULL;
902
903 line = tty->index;
904 if (line == MXSER_PORTS)
905 return 0;
906 if (line < 0 || line > MXSER_PORTS)
907 return -ENODEV;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800908 info = &mxser_boards[line / MXSER_PORTS_PER_BOARD].ports[line % MXSER_PORTS_PER_BOARD];
909 if (!info->ioaddr)
Jiri Slaby037ad482006-12-08 02:38:11 -0800910 return -ENODEV;
911
912 tty->driver_data = info;
913 info->tty = tty;
914 /*
915 * Start up serial port
916 */
Jiri Slaby3306ce32006-12-08 02:38:13 -0800917 info->count++;
Jiri Slaby037ad482006-12-08 02:38:11 -0800918 retval = mxser_startup(info);
919 if (retval)
920 return retval;
921
922 retval = mxser_block_til_ready(tty, filp, info);
923 if (retval)
924 return retval;
925
Jiri Slabye079f492006-12-08 02:38:31 -0800926 /* unmark here for very high baud rate (ex. 921600 bps) used */
Jiri Slaby037ad482006-12-08 02:38:11 -0800927 tty->low_latency = 1;
928 return 0;
929}
930
931/*
932 * This routine is called when the serial port gets closed. First, we
933 * wait for the last remaining data to be sent. Then, we unlink its
934 * async structure from the interrupt chain if necessary, and we free
935 * that IRQ if nothing is left in the chain.
936 */
937static void mxser_close(struct tty_struct *tty, struct file *filp)
938{
Jiri Slaby55b307d2006-12-08 02:38:14 -0800939 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -0800940
941 unsigned long timeout;
942 unsigned long flags;
Jiri Slaby037ad482006-12-08 02:38:11 -0800943
944 if (tty->index == MXSER_PORTS)
945 return;
946 if (!info)
947 return;
948
949 spin_lock_irqsave(&info->slock, flags);
950
951 if (tty_hung_up_p(filp)) {
952 spin_unlock_irqrestore(&info->slock, flags);
953 return;
954 }
955 if ((tty->count == 1) && (info->count != 1)) {
956 /*
957 * Uh, oh. tty->count is 1, which means that the tty
958 * structure will be freed. Info->count should always
959 * be one in these conditions. If it's greater than
960 * one, we've got real problems, since it means the
961 * serial port won't be shutdown.
962 */
963 printk(KERN_ERR "mxser_close: bad serial port count; "
964 "tty->count is 1, info->count is %d\n", info->count);
965 info->count = 1;
966 }
967 if (--info->count < 0) {
968 printk(KERN_ERR "mxser_close: bad serial port count for "
Jiri Slaby55b307d2006-12-08 02:38:14 -0800969 "ttys%d: %d\n", tty->index, info->count);
Jiri Slaby037ad482006-12-08 02:38:11 -0800970 info->count = 0;
971 }
972 if (info->count) {
973 spin_unlock_irqrestore(&info->slock, flags);
974 return;
975 }
976 info->flags |= ASYNC_CLOSING;
977 spin_unlock_irqrestore(&info->slock, flags);
978 /*
979 * Save the termios structure, since this port may have
980 * separate termios for callout and dialin.
981 */
982 if (info->flags & ASYNC_NORMAL_ACTIVE)
983 info->normal_termios = *tty->termios;
984 /*
985 * Now we wait for the transmit buffer to clear; and we notify
986 * the line discipline to only process XON/XOFF characters.
987 */
988 tty->closing = 1;
989 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
990 tty_wait_until_sent(tty, info->closing_wait);
991 /*
992 * At this point we stop accepting input. To do this, we
993 * disable the receive line status interrupts, and tell the
994 * interrupt driver to stop checking the data ready bit in the
995 * line status register.
996 */
997 info->IER &= ~UART_IER_RLSI;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800998 if (info->board->chip_flag)
Jiri Slaby037ad482006-12-08 02:38:11 -0800999 info->IER &= ~MOXA_MUST_RECV_ISR;
Jiri Slabye079f492006-12-08 02:38:31 -08001000
Jiri Slaby037ad482006-12-08 02:38:11 -08001001 if (info->flags & ASYNC_INITIALIZED) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08001002 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001003 /*
1004 * Before we drop DTR, make sure the UART transmitter
1005 * has completely drained; this is especially
1006 * important if there is a transmit FIFO!
1007 */
1008 timeout = jiffies + HZ;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001009 while (!(inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001010 schedule_timeout_interruptible(5);
1011 if (time_after(jiffies, timeout))
1012 break;
1013 }
1014 }
1015 mxser_shutdown(info);
1016
1017 if (tty->driver->flush_buffer)
1018 tty->driver->flush_buffer(tty);
1019
Jiri Slaby7e8bcf92006-12-08 02:38:24 -08001020 tty_ldisc_flush(tty);
Jiri Slaby037ad482006-12-08 02:38:11 -08001021
1022 tty->closing = 0;
1023 info->event = 0;
1024 info->tty = NULL;
1025 if (info->blocked_open) {
1026 if (info->close_delay)
1027 schedule_timeout_interruptible(info->close_delay);
1028 wake_up_interruptible(&info->open_wait);
1029 }
1030
1031 info->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
Jiri Slaby037ad482006-12-08 02:38:11 -08001032}
1033
1034static int mxser_write(struct tty_struct *tty, const unsigned char *buf, int count)
1035{
1036 int c, total = 0;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001037 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001038 unsigned long flags;
1039
1040 if (!info->xmit_buf)
1041 return 0;
1042
1043 while (1) {
1044 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1045 SERIAL_XMIT_SIZE - info->xmit_head));
1046 if (c <= 0)
1047 break;
1048
1049 memcpy(info->xmit_buf + info->xmit_head, buf, c);
1050 spin_lock_irqsave(&info->slock, flags);
1051 info->xmit_head = (info->xmit_head + c) &
1052 (SERIAL_XMIT_SIZE - 1);
1053 info->xmit_cnt += c;
1054 spin_unlock_irqrestore(&info->slock, flags);
1055
1056 buf += c;
1057 count -= c;
1058 total += c;
1059 }
1060
Jiri Slabye079f492006-12-08 02:38:31 -08001061 if (info->xmit_cnt && !tty->stopped) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001062 if (!tty->hw_stopped ||
1063 (info->type == PORT_16550A) ||
Jiri Slaby55b307d2006-12-08 02:38:14 -08001064 (info->board->chip_flag)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001065 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby55b307d2006-12-08 02:38:14 -08001066 outb(info->IER & ~UART_IER_THRI, info->ioaddr +
1067 UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001068 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001069 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001070 spin_unlock_irqrestore(&info->slock, flags);
1071 }
1072 }
1073 return total;
1074}
1075
1076static void mxser_put_char(struct tty_struct *tty, unsigned char ch)
1077{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001078 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001079 unsigned long flags;
1080
1081 if (!info->xmit_buf)
1082 return;
1083
1084 if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1)
1085 return;
1086
1087 spin_lock_irqsave(&info->slock, flags);
1088 info->xmit_buf[info->xmit_head++] = ch;
1089 info->xmit_head &= SERIAL_XMIT_SIZE - 1;
1090 info->xmit_cnt++;
1091 spin_unlock_irqrestore(&info->slock, flags);
Jiri Slabye079f492006-12-08 02:38:31 -08001092 if (!tty->stopped) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001093 if (!tty->hw_stopped ||
1094 (info->type == PORT_16550A) ||
Jiri Slaby55b307d2006-12-08 02:38:14 -08001095 info->board->chip_flag) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001096 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby55b307d2006-12-08 02:38:14 -08001097 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001098 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001099 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001100 spin_unlock_irqrestore(&info->slock, flags);
1101 }
1102 }
1103}
1104
1105
1106static void mxser_flush_chars(struct tty_struct *tty)
1107{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001108 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001109 unsigned long flags;
1110
1111 if (info->xmit_cnt <= 0 ||
1112 tty->stopped ||
1113 !info->xmit_buf ||
1114 (tty->hw_stopped &&
1115 (info->type != PORT_16550A) &&
Jiri Slaby55b307d2006-12-08 02:38:14 -08001116 (!info->board->chip_flag)
Jiri Slaby037ad482006-12-08 02:38:11 -08001117 ))
1118 return;
1119
1120 spin_lock_irqsave(&info->slock, flags);
1121
Jiri Slaby55b307d2006-12-08 02:38:14 -08001122 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001123 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001124 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001125
1126 spin_unlock_irqrestore(&info->slock, flags);
1127}
1128
1129static int mxser_write_room(struct tty_struct *tty)
1130{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001131 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001132 int ret;
1133
1134 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1135 if (ret < 0)
1136 ret = 0;
1137 return ret;
1138}
1139
1140static int mxser_chars_in_buffer(struct tty_struct *tty)
1141{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001142 struct mxser_port *info = tty->driver_data;
Jiri Slaby925e9c12006-12-08 02:38:30 -08001143 return info->xmit_cnt;
Jiri Slaby037ad482006-12-08 02:38:11 -08001144}
1145
1146static void mxser_flush_buffer(struct tty_struct *tty)
1147{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001148 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001149 char fcr;
1150 unsigned long flags;
1151
1152
1153 spin_lock_irqsave(&info->slock, flags);
1154 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1155
Jiri Slaby55b307d2006-12-08 02:38:14 -08001156 fcr = inb(info->ioaddr + UART_FCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08001157 outb((fcr | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
Jiri Slaby55b307d2006-12-08 02:38:14 -08001158 info->ioaddr + UART_FCR);
1159 outb(fcr, info->ioaddr + UART_FCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08001160
1161 spin_unlock_irqrestore(&info->slock, flags);
Jiri Slaby037ad482006-12-08 02:38:11 -08001162
Jiri Slaby7e8bcf92006-12-08 02:38:24 -08001163 tty_wakeup(tty);
Jiri Slaby037ad482006-12-08 02:38:11 -08001164}
1165
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001166/*
1167 * ------------------------------------------------------------
1168 * friends of mxser_ioctl()
1169 * ------------------------------------------------------------
1170 */
1171static int mxser_get_serial_info(struct mxser_port *info,
1172 struct serial_struct __user *retinfo)
1173{
1174 struct serial_struct tmp;
1175
1176 if (!retinfo)
1177 return -EFAULT;
1178 memset(&tmp, 0, sizeof(tmp));
1179 tmp.type = info->type;
1180 tmp.line = info->tty->index;
1181 tmp.port = info->ioaddr;
1182 tmp.irq = info->board->irq;
1183 tmp.flags = info->flags;
1184 tmp.baud_base = info->baud_base;
1185 tmp.close_delay = info->close_delay;
1186 tmp.closing_wait = info->closing_wait;
1187 tmp.custom_divisor = info->custom_divisor;
1188 tmp.hub6 = 0;
1189 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1190 return -EFAULT;
1191 return 0;
1192}
1193
1194static int mxser_set_serial_info(struct mxser_port *info,
1195 struct serial_struct __user *new_info)
1196{
1197 struct serial_struct new_serial;
1198 unsigned int flags;
1199 int retval = 0;
1200
1201 if (!new_info || !info->ioaddr)
1202 return -EFAULT;
1203 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
1204 return -EFAULT;
1205
1206 if ((new_serial.irq != info->board->irq) ||
1207 (new_serial.port != info->ioaddr) ||
1208 (new_serial.custom_divisor != info->custom_divisor) ||
1209 (new_serial.baud_base != info->baud_base))
1210 return -EPERM;
1211
1212 flags = info->flags & ASYNC_SPD_MASK;
1213
1214 if (!capable(CAP_SYS_ADMIN)) {
1215 if ((new_serial.baud_base != info->baud_base) ||
1216 (new_serial.close_delay != info->close_delay) ||
1217 ((new_serial.flags & ~ASYNC_USR_MASK) != (info->flags & ~ASYNC_USR_MASK)))
1218 return -EPERM;
1219 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
1220 (new_serial.flags & ASYNC_USR_MASK));
1221 } else {
1222 /*
1223 * OK, past this point, all the error checking has been done.
1224 * At this point, we start making changes.....
1225 */
1226 info->flags = ((info->flags & ~ASYNC_FLAGS) |
1227 (new_serial.flags & ASYNC_FLAGS));
1228 info->close_delay = new_serial.close_delay * HZ / 100;
1229 info->closing_wait = new_serial.closing_wait * HZ / 100;
1230 info->tty->low_latency =
1231 (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
Jiri Slabye079f492006-12-08 02:38:31 -08001232 info->tty->low_latency = 0;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001233 }
1234
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001235 info->type = new_serial.type;
1236
1237 process_txrx_fifo(info);
1238
1239 if (info->flags & ASYNC_INITIALIZED) {
1240 if (flags != (info->flags & ASYNC_SPD_MASK))
1241 mxser_change_speed(info, NULL);
1242 } else
1243 retval = mxser_startup(info);
1244
1245 return retval;
1246}
1247
1248/*
1249 * mxser_get_lsr_info - get line status register info
1250 *
1251 * Purpose: Let user call ioctl() to get info when the UART physically
1252 * is emptied. On bus types like RS485, the transmitter must
1253 * release the bus after transmitting. This must be done when
1254 * the transmit shift register is empty, not be done when the
1255 * transmit holding register is empty. This functionality
1256 * allows an RS485 driver to be written in user space.
1257 */
1258static int mxser_get_lsr_info(struct mxser_port *info,
1259 unsigned int __user *value)
1260{
1261 unsigned char status;
1262 unsigned int result;
1263 unsigned long flags;
1264
1265 spin_lock_irqsave(&info->slock, flags);
1266 status = inb(info->ioaddr + UART_LSR);
1267 spin_unlock_irqrestore(&info->slock, flags);
1268 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1269 return put_user(result, value);
1270}
1271
1272/*
1273 * This routine sends a break character out the serial port.
1274 */
1275static void mxser_send_break(struct mxser_port *info, int duration)
1276{
1277 unsigned long flags;
1278
1279 if (!info->ioaddr)
1280 return;
1281 set_current_state(TASK_INTERRUPTIBLE);
1282 spin_lock_irqsave(&info->slock, flags);
1283 outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
1284 info->ioaddr + UART_LCR);
1285 spin_unlock_irqrestore(&info->slock, flags);
1286 schedule_timeout(duration);
1287 spin_lock_irqsave(&info->slock, flags);
1288 outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
1289 info->ioaddr + UART_LCR);
1290 spin_unlock_irqrestore(&info->slock, flags);
1291}
1292
1293static int mxser_tiocmget(struct tty_struct *tty, struct file *file)
Jiri Slaby037ad482006-12-08 02:38:11 -08001294{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001295 struct mxser_port *info = tty->driver_data;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001296 unsigned char control, status;
Jiri Slaby037ad482006-12-08 02:38:11 -08001297 unsigned long flags;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001298
Jiri Slaby037ad482006-12-08 02:38:11 -08001299
1300 if (tty->index == MXSER_PORTS)
Jiri Slaby037ad482006-12-08 02:38:11 -08001301 return -ENOIOCTLCMD;
Jiri Slaby214efeb2006-12-08 02:38:25 -08001302 if (test_bit(TTY_IO_ERROR, &tty->flags))
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001303 return -EIO;
1304
1305 control = info->MCR;
1306
1307 spin_lock_irqsave(&info->slock, flags);
1308 status = inb(info->ioaddr + UART_MSR);
1309 if (status & UART_MSR_ANY_DELTA)
1310 mxser_check_modem_status(info, status);
1311 spin_unlock_irqrestore(&info->slock, flags);
1312 return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0) |
1313 ((control & UART_MCR_DTR) ? TIOCM_DTR : 0) |
1314 ((status & UART_MSR_DCD) ? TIOCM_CAR : 0) |
1315 ((status & UART_MSR_RI) ? TIOCM_RNG : 0) |
1316 ((status & UART_MSR_DSR) ? TIOCM_DSR : 0) |
1317 ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
1318}
1319
1320static int mxser_tiocmset(struct tty_struct *tty, struct file *file,
1321 unsigned int set, unsigned int clear)
1322{
1323 struct mxser_port *info = tty->driver_data;
1324 unsigned long flags;
1325
1326
1327 if (tty->index == MXSER_PORTS)
1328 return -ENOIOCTLCMD;
Jiri Slaby214efeb2006-12-08 02:38:25 -08001329 if (test_bit(TTY_IO_ERROR, &tty->flags))
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001330 return -EIO;
1331
1332 spin_lock_irqsave(&info->slock, flags);
1333
1334 if (set & TIOCM_RTS)
1335 info->MCR |= UART_MCR_RTS;
1336 if (set & TIOCM_DTR)
1337 info->MCR |= UART_MCR_DTR;
1338
1339 if (clear & TIOCM_RTS)
1340 info->MCR &= ~UART_MCR_RTS;
1341 if (clear & TIOCM_DTR)
1342 info->MCR &= ~UART_MCR_DTR;
1343
1344 outb(info->MCR, info->ioaddr + UART_MCR);
1345 spin_unlock_irqrestore(&info->slock, flags);
Jiri Slaby037ad482006-12-08 02:38:11 -08001346 return 0;
1347}
1348
Jiri Slabyc76b0932007-02-10 01:45:07 -08001349static int __init mxser_program_mode(int port)
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001350{
1351 int id, i, j, n;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001352
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001353 outb(0, port);
1354 outb(0, port);
1355 outb(0, port);
1356 (void)inb(port);
1357 (void)inb(port);
1358 outb(0, port);
1359 (void)inb(port);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001360
1361 id = inb(port + 1) & 0x1F;
1362 if ((id != C168_ASIC_ID) &&
1363 (id != C104_ASIC_ID) &&
1364 (id != C102_ASIC_ID) &&
1365 (id != CI132_ASIC_ID) &&
1366 (id != CI134_ASIC_ID) &&
1367 (id != CI104J_ASIC_ID))
1368 return -1;
1369 for (i = 0, j = 0; i < 4; i++) {
1370 n = inb(port + 2);
1371 if (n == 'M') {
1372 j = 1;
1373 } else if ((j == 1) && (n == 1)) {
1374 j = 2;
1375 break;
1376 } else
1377 j = 0;
1378 }
1379 if (j != 2)
1380 id = -2;
1381 return id;
1382}
1383
Jiri Slabyc76b0932007-02-10 01:45:07 -08001384static void __init mxser_normal_mode(int port)
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001385{
1386 int i, n;
1387
1388 outb(0xA5, port + 1);
1389 outb(0x80, port + 3);
1390 outb(12, port + 0); /* 9600 bps */
1391 outb(0, port + 1);
1392 outb(0x03, port + 3); /* 8 data bits */
1393 outb(0x13, port + 4); /* loop back mode */
1394 for (i = 0; i < 16; i++) {
1395 n = inb(port + 5);
1396 if ((n & 0x61) == 0x60)
1397 break;
1398 if ((n & 1) == 1)
1399 (void)inb(port);
1400 }
1401 outb(0x00, port + 4);
1402}
1403
1404#define CHIP_SK 0x01 /* Serial Data Clock in Eprom */
1405#define CHIP_DO 0x02 /* Serial Data Output in Eprom */
1406#define CHIP_CS 0x04 /* Serial Chip Select in Eprom */
1407#define CHIP_DI 0x08 /* Serial Data Input in Eprom */
1408#define EN_CCMD 0x000 /* Chip's command register */
1409#define EN0_RSARLO 0x008 /* Remote start address reg 0 */
1410#define EN0_RSARHI 0x009 /* Remote start address reg 1 */
1411#define EN0_RCNTLO 0x00A /* Remote byte count reg WR */
1412#define EN0_RCNTHI 0x00B /* Remote byte count reg WR */
1413#define EN0_DCFG 0x00E /* Data configuration reg WR */
1414#define EN0_PORT 0x010 /* Rcv missed frame error counter RD */
1415#define ENC_PAGE0 0x000 /* Select page 0 of chip registers */
1416#define ENC_PAGE3 0x0C0 /* Select page 3 of chip registers */
Jiri Slabyc76b0932007-02-10 01:45:07 -08001417static int __init mxser_read_register(int port, unsigned short *regs)
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001418{
1419 int i, k, value, id;
1420 unsigned int j;
1421
1422 id = mxser_program_mode(port);
1423 if (id < 0)
1424 return id;
1425 for (i = 0; i < 14; i++) {
1426 k = (i & 0x3F) | 0x180;
1427 for (j = 0x100; j > 0; j >>= 1) {
1428 outb(CHIP_CS, port);
1429 if (k & j) {
1430 outb(CHIP_CS | CHIP_DO, port);
1431 outb(CHIP_CS | CHIP_DO | CHIP_SK, port); /* A? bit of read */
1432 } else {
1433 outb(CHIP_CS, port);
1434 outb(CHIP_CS | CHIP_SK, port); /* A? bit of read */
1435 }
1436 }
1437 (void)inb(port);
1438 value = 0;
1439 for (k = 0, j = 0x8000; k < 16; k++, j >>= 1) {
1440 outb(CHIP_CS, port);
1441 outb(CHIP_CS | CHIP_SK, port);
1442 if (inb(port) & CHIP_DI)
1443 value |= j;
1444 }
1445 regs[i] = value;
1446 outb(0, port);
1447 }
1448 mxser_normal_mode(port);
1449 return id;
1450}
1451
Jiri Slaby037ad482006-12-08 02:38:11 -08001452static int mxser_ioctl_special(unsigned int cmd, void __user *argp)
1453{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001454 struct mxser_port *port;
1455 int result, status;
1456 unsigned int i, j;
Jiri Slaby037ad482006-12-08 02:38:11 -08001457
1458 switch (cmd) {
1459 case MOXA_GET_CONF:
Jiri Slaby55b307d2006-12-08 02:38:14 -08001460/* if (copy_to_user(argp, mxsercfg,
Jiri Slaby037ad482006-12-08 02:38:11 -08001461 sizeof(struct mxser_hwconf) * 4))
1462 return -EFAULT;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001463 return 0;*/
1464 return -ENXIO;
Jiri Slaby037ad482006-12-08 02:38:11 -08001465 case MOXA_GET_MAJOR:
1466 if (copy_to_user(argp, &ttymajor, sizeof(int)))
1467 return -EFAULT;
1468 return 0;
1469
1470 case MOXA_GET_CUMAJOR:
1471 if (copy_to_user(argp, &calloutmajor, sizeof(int)))
1472 return -EFAULT;
1473 return 0;
1474
1475 case MOXA_CHKPORTENABLE:
1476 result = 0;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001477
1478 for (i = 0; i < MXSER_BOARDS; i++)
1479 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++)
1480 if (mxser_boards[i].ports[j].ioaddr)
1481 result |= (1 << i);
1482
Jiri Slaby037ad482006-12-08 02:38:11 -08001483 return put_user(result, (unsigned long __user *)argp);
1484 case MOXA_GETDATACOUNT:
1485 if (copy_to_user(argp, &mxvar_log, sizeof(mxvar_log)))
1486 return -EFAULT;
1487 return 0;
1488 case MOXA_GETMSTATUS:
Jiri Slaby55b307d2006-12-08 02:38:14 -08001489 for (i = 0; i < MXSER_BOARDS; i++)
1490 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
1491 port = &mxser_boards[i].ports[j];
1492
1493 GMStatus[i].ri = 0;
1494 if (!port->ioaddr) {
1495 GMStatus[i].dcd = 0;
1496 GMStatus[i].dsr = 0;
1497 GMStatus[i].cts = 0;
1498 continue;
1499 }
1500
1501 if (!port->tty || !port->tty->termios)
1502 GMStatus[i].cflag =
1503 port->normal_termios.c_cflag;
1504 else
1505 GMStatus[i].cflag =
1506 port->tty->termios->c_cflag;
1507
1508 status = inb(port->ioaddr + UART_MSR);
1509 if (status & 0x80 /*UART_MSR_DCD */ )
1510 GMStatus[i].dcd = 1;
1511 else
1512 GMStatus[i].dcd = 0;
1513
1514 if (status & 0x20 /*UART_MSR_DSR */ )
1515 GMStatus[i].dsr = 1;
1516 else
1517 GMStatus[i].dsr = 0;
1518
1519
1520 if (status & 0x10 /*UART_MSR_CTS */ )
1521 GMStatus[i].cts = 1;
1522 else
1523 GMStatus[i].cts = 0;
Jiri Slaby037ad482006-12-08 02:38:11 -08001524 }
Jiri Slaby037ad482006-12-08 02:38:11 -08001525 if (copy_to_user(argp, GMStatus,
1526 sizeof(struct mxser_mstatus) * MXSER_PORTS))
1527 return -EFAULT;
1528 return 0;
1529 case MOXA_ASPP_MON_EXT: {
Jiri Slaby55b307d2006-12-08 02:38:14 -08001530 int status, p, shiftbit;
1531 unsigned long opmode;
1532 unsigned cflag, iflag;
Jiri Slaby037ad482006-12-08 02:38:11 -08001533
Jiri Slaby55b307d2006-12-08 02:38:14 -08001534 for (i = 0; i < MXSER_BOARDS; i++)
1535 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
1536 port = &mxser_boards[i].ports[j];
1537 if (!port->ioaddr)
Jiri Slaby037ad482006-12-08 02:38:11 -08001538 continue;
1539
Jiri Slaby55b307d2006-12-08 02:38:14 -08001540 status = mxser_get_msr(port->ioaddr, 0, i);
Jiri Slaby55b307d2006-12-08 02:38:14 -08001541
Jiri Slaby037ad482006-12-08 02:38:11 -08001542 if (status & UART_MSR_TERI)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001543 port->icount.rng++;
Jiri Slaby037ad482006-12-08 02:38:11 -08001544 if (status & UART_MSR_DDSR)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001545 port->icount.dsr++;
Jiri Slaby037ad482006-12-08 02:38:11 -08001546 if (status & UART_MSR_DDCD)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001547 port->icount.dcd++;
Jiri Slaby037ad482006-12-08 02:38:11 -08001548 if (status & UART_MSR_DCTS)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001549 port->icount.cts++;
Jiri Slaby037ad482006-12-08 02:38:11 -08001550
Jiri Slaby55b307d2006-12-08 02:38:14 -08001551 port->mon_data.modem_status = status;
1552 mon_data_ext.rx_cnt[i] = port->mon_data.rxcnt;
1553 mon_data_ext.tx_cnt[i] = port->mon_data.txcnt;
1554 mon_data_ext.up_rxcnt[i] =
1555 port->mon_data.up_rxcnt;
1556 mon_data_ext.up_txcnt[i] =
1557 port->mon_data.up_txcnt;
1558 mon_data_ext.modem_status[i] =
1559 port->mon_data.modem_status;
1560 mon_data_ext.baudrate[i] = port->realbaud;
Jiri Slaby037ad482006-12-08 02:38:11 -08001561
Jiri Slaby55b307d2006-12-08 02:38:14 -08001562 if (!port->tty || !port->tty->termios) {
1563 cflag = port->normal_termios.c_cflag;
1564 iflag = port->normal_termios.c_iflag;
Jiri Slaby037ad482006-12-08 02:38:11 -08001565 } else {
Jiri Slaby55b307d2006-12-08 02:38:14 -08001566 cflag = port->tty->termios->c_cflag;
1567 iflag = port->tty->termios->c_iflag;
Jiri Slaby037ad482006-12-08 02:38:11 -08001568 }
1569
1570 mon_data_ext.databits[i] = cflag & CSIZE;
1571
1572 mon_data_ext.stopbits[i] = cflag & CSTOPB;
1573
Jiri Slaby55b307d2006-12-08 02:38:14 -08001574 mon_data_ext.parity[i] =
1575 cflag & (PARENB | PARODD | CMSPAR);
Jiri Slaby037ad482006-12-08 02:38:11 -08001576
1577 mon_data_ext.flowctrl[i] = 0x00;
1578
1579 if (cflag & CRTSCTS)
1580 mon_data_ext.flowctrl[i] |= 0x03;
1581
1582 if (iflag & (IXON | IXOFF))
1583 mon_data_ext.flowctrl[i] |= 0x0C;
1584
Jiri Slaby55b307d2006-12-08 02:38:14 -08001585 if (port->type == PORT_16550A)
Jiri Slaby037ad482006-12-08 02:38:11 -08001586 mon_data_ext.fifo[i] = 1;
1587 else
1588 mon_data_ext.fifo[i] = 0;
1589
1590 p = i % 4;
1591 shiftbit = p * 2;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001592 opmode = inb(port->opmode_ioaddr) >> shiftbit;
Jiri Slaby037ad482006-12-08 02:38:11 -08001593 opmode &= OP_MODE_MASK;
1594
1595 mon_data_ext.iftype[i] = opmode;
1596
1597 }
Jiri Slaby55b307d2006-12-08 02:38:14 -08001598 if (copy_to_user(argp, &mon_data_ext,
1599 sizeof(mon_data_ext)))
Jiri Slaby037ad482006-12-08 02:38:11 -08001600 return -EFAULT;
1601
1602 return 0;
1603
Jiri Slaby55b307d2006-12-08 02:38:14 -08001604 } default:
Jiri Slaby037ad482006-12-08 02:38:11 -08001605 return -ENOIOCTLCMD;
1606 }
1607 return 0;
1608}
1609
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001610static int mxser_ioctl(struct tty_struct *tty, struct file *file,
1611 unsigned int cmd, unsigned long arg)
1612{
1613 struct mxser_port *info = tty->driver_data;
1614 struct async_icount cprev, cnow; /* kernel counter temps */
1615 struct serial_icounter_struct __user *p_cuser;
1616 unsigned long templ;
1617 unsigned long flags;
1618 void __user *argp = (void __user *)arg;
1619 int retval;
1620
1621 if (tty->index == MXSER_PORTS)
1622 return mxser_ioctl_special(cmd, argp);
1623
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001624 if (cmd == MOXA_SET_OP_MODE || cmd == MOXA_GET_OP_MODE) {
1625 int p;
1626 unsigned long opmode;
1627 static unsigned char ModeMask[] = { 0xfc, 0xf3, 0xcf, 0x3f };
1628 int shiftbit;
1629 unsigned char val, mask;
1630
1631 p = tty->index % 4;
1632 if (cmd == MOXA_SET_OP_MODE) {
1633 if (get_user(opmode, (int __user *) argp))
1634 return -EFAULT;
1635 if (opmode != RS232_MODE &&
1636 opmode != RS485_2WIRE_MODE &&
1637 opmode != RS422_MODE &&
1638 opmode != RS485_4WIRE_MODE)
1639 return -EFAULT;
1640 mask = ModeMask[p];
1641 shiftbit = p * 2;
1642 val = inb(info->opmode_ioaddr);
1643 val &= mask;
1644 val |= (opmode << shiftbit);
1645 outb(val, info->opmode_ioaddr);
1646 } else {
1647 shiftbit = p * 2;
1648 opmode = inb(info->opmode_ioaddr) >> shiftbit;
1649 opmode &= OP_MODE_MASK;
1650 if (copy_to_user(argp, &opmode, sizeof(int)))
1651 return -EFAULT;
1652 }
1653 return 0;
1654 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001655
Jiri Slaby214efeb2006-12-08 02:38:25 -08001656 if (cmd != TIOCGSERIAL && cmd != TIOCMIWAIT && cmd != TIOCGICOUNT &&
1657 test_bit(TTY_IO_ERROR, &tty->flags))
1658 return -EIO;
1659
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001660 switch (cmd) {
1661 case TCSBRK: /* SVID version: non-zero arg --> no break */
1662 retval = tty_check_change(tty);
1663 if (retval)
1664 return retval;
1665 tty_wait_until_sent(tty, 0);
1666 if (!arg)
1667 mxser_send_break(info, HZ / 4); /* 1/4 second */
1668 return 0;
1669 case TCSBRKP: /* support for POSIX tcsendbreak() */
1670 retval = tty_check_change(tty);
1671 if (retval)
1672 return retval;
1673 tty_wait_until_sent(tty, 0);
1674 mxser_send_break(info, arg ? arg * (HZ / 10) : HZ / 4);
1675 return 0;
1676 case TIOCGSOFTCAR:
1677 return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *)argp);
1678 case TIOCSSOFTCAR:
1679 if (get_user(templ, (unsigned long __user *) argp))
1680 return -EFAULT;
1681 arg = templ;
1682 tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) | (arg ? CLOCAL : 0));
1683 return 0;
1684 case TIOCGSERIAL:
1685 return mxser_get_serial_info(info, argp);
1686 case TIOCSSERIAL:
1687 return mxser_set_serial_info(info, argp);
1688 case TIOCSERGETLSR: /* Get line status register */
1689 return mxser_get_lsr_info(info, argp);
1690 /*
1691 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1692 * - mask passed in arg for lines of interest
1693 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1694 * Caller should use TIOCGICOUNT to see which one it was
1695 */
1696 case TIOCMIWAIT: {
1697 DECLARE_WAITQUEUE(wait, current);
1698 int ret;
1699 spin_lock_irqsave(&info->slock, flags);
1700 cprev = info->icount; /* note the counters on entry */
1701 spin_unlock_irqrestore(&info->slock, flags);
1702
1703 add_wait_queue(&info->delta_msr_wait, &wait);
1704 while (1) {
1705 spin_lock_irqsave(&info->slock, flags);
1706 cnow = info->icount; /* atomic copy */
1707 spin_unlock_irqrestore(&info->slock, flags);
1708
1709 set_current_state(TASK_INTERRUPTIBLE);
1710 if (((arg & TIOCM_RNG) &&
1711 (cnow.rng != cprev.rng)) ||
1712 ((arg & TIOCM_DSR) &&
1713 (cnow.dsr != cprev.dsr)) ||
1714 ((arg & TIOCM_CD) &&
1715 (cnow.dcd != cprev.dcd)) ||
1716 ((arg & TIOCM_CTS) &&
1717 (cnow.cts != cprev.cts))) {
1718 ret = 0;
1719 break;
1720 }
1721 /* see if a signal did it */
1722 if (signal_pending(current)) {
1723 ret = -ERESTARTSYS;
1724 break;
1725 }
1726 cprev = cnow;
1727 }
1728 current->state = TASK_RUNNING;
1729 remove_wait_queue(&info->delta_msr_wait, &wait);
1730 break;
1731 }
1732 /* NOTREACHED */
1733 /*
1734 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1735 * Return: write counters to the user passed counter struct
1736 * NB: both 1->0 and 0->1 transitions are counted except for
1737 * RI where only 0->1 is counted.
1738 */
1739 case TIOCGICOUNT:
1740 spin_lock_irqsave(&info->slock, flags);
1741 cnow = info->icount;
1742 spin_unlock_irqrestore(&info->slock, flags);
1743 p_cuser = argp;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001744 if (put_user(cnow.frame, &p_cuser->frame))
1745 return -EFAULT;
1746 if (put_user(cnow.brk, &p_cuser->brk))
1747 return -EFAULT;
1748 if (put_user(cnow.overrun, &p_cuser->overrun))
1749 return -EFAULT;
1750 if (put_user(cnow.buf_overrun, &p_cuser->buf_overrun))
1751 return -EFAULT;
1752 if (put_user(cnow.parity, &p_cuser->parity))
1753 return -EFAULT;
1754 if (put_user(cnow.rx, &p_cuser->rx))
1755 return -EFAULT;
1756 if (put_user(cnow.tx, &p_cuser->tx))
1757 return -EFAULT;
1758 put_user(cnow.cts, &p_cuser->cts);
1759 put_user(cnow.dsr, &p_cuser->dsr);
1760 put_user(cnow.rng, &p_cuser->rng);
1761 put_user(cnow.dcd, &p_cuser->dcd);
1762 return 0;
1763 case MOXA_HighSpeedOn:
1764 return put_user(info->baud_base != 115200 ? 1 : 0, (int __user *)argp);
1765 case MOXA_SDS_RSTICOUNTER:
1766 info->mon_data.rxcnt = 0;
1767 info->mon_data.txcnt = 0;
1768 return 0;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001769 case MOXA_ASPP_SETBAUD:{
1770 long baud;
1771 if (get_user(baud, (long __user *)argp))
1772 return -EFAULT;
1773 mxser_set_baud(info, baud);
1774 return 0;
1775 }
1776 case MOXA_ASPP_GETBAUD:
1777 if (copy_to_user(argp, &info->realbaud, sizeof(long)))
1778 return -EFAULT;
1779
1780 return 0;
1781
1782 case MOXA_ASPP_OQUEUE:{
1783 int len, lsr;
1784
1785 len = mxser_chars_in_buffer(tty);
1786
1787 lsr = inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT;
1788
1789 len += (lsr ? 0 : 1);
1790
1791 if (copy_to_user(argp, &len, sizeof(int)))
1792 return -EFAULT;
1793
1794 return 0;
1795 }
1796 case MOXA_ASPP_MON: {
1797 int mcr, status;
1798
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001799 status = mxser_get_msr(info->ioaddr, 1, tty->index);
1800 mxser_check_modem_status(info, status);
1801
1802 mcr = inb(info->ioaddr + UART_MCR);
1803 if (mcr & MOXA_MUST_MCR_XON_FLAG)
1804 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFHOLD;
1805 else
1806 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFHOLD;
1807
1808 if (mcr & MOXA_MUST_MCR_TX_XON)
1809 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFXENT;
1810 else
1811 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFXENT;
1812
1813 if (info->tty->hw_stopped)
1814 info->mon_data.hold_reason |= NPPI_NOTIFY_CTSHOLD;
1815 else
1816 info->mon_data.hold_reason &= ~NPPI_NOTIFY_CTSHOLD;
1817
1818 if (copy_to_user(argp, &info->mon_data,
1819 sizeof(struct mxser_mon)))
1820 return -EFAULT;
1821
1822 return 0;
1823 }
1824 case MOXA_ASPP_LSTATUS: {
1825 if (copy_to_user(argp, &info->err_shadow,
1826 sizeof(unsigned char)))
1827 return -EFAULT;
1828
1829 info->err_shadow = 0;
1830 return 0;
1831 }
1832 case MOXA_SET_BAUD_METHOD: {
1833 int method;
1834
1835 if (get_user(method, (int __user *)argp))
1836 return -EFAULT;
1837 mxser_set_baud_method[tty->index] = method;
1838 if (copy_to_user(argp, &method, sizeof(int)))
1839 return -EFAULT;
1840
1841 return 0;
1842 }
1843 default:
1844 return -ENOIOCTLCMD;
1845 }
1846 return 0;
1847}
1848
Jiri Slaby037ad482006-12-08 02:38:11 -08001849static void mxser_stoprx(struct tty_struct *tty)
1850{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001851 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001852
1853 info->ldisc_stop_rx = 1;
1854 if (I_IXOFF(tty)) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08001855 if (info->board->chip_flag) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001856 info->IER &= ~MOXA_MUST_RECV_ISR;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001857 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby925e9c12006-12-08 02:38:30 -08001858 } else {
Jiri Slaby037ad482006-12-08 02:38:11 -08001859 info->x_char = STOP_CHAR(tty);
Jiri Slaby55b307d2006-12-08 02:38:14 -08001860 outb(0, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001861 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001862 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001863 }
Jiri Slaby037ad482006-12-08 02:38:11 -08001864 }
1865
1866 if (info->tty->termios->c_cflag & CRTSCTS) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001867 info->MCR &= ~UART_MCR_RTS;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001868 outb(info->MCR, info->ioaddr + UART_MCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08001869 }
1870}
1871
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001872/*
1873 * This routine is called by the upper-layer tty layer to signal that
1874 * incoming characters should be throttled.
1875 */
1876static void mxser_throttle(struct tty_struct *tty)
1877{
1878 mxser_stoprx(tty);
1879}
1880
1881static void mxser_unthrottle(struct tty_struct *tty)
Jiri Slaby037ad482006-12-08 02:38:11 -08001882{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001883 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001884
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001885 /* startrx */
Jiri Slaby037ad482006-12-08 02:38:11 -08001886 info->ldisc_stop_rx = 0;
1887 if (I_IXOFF(tty)) {
1888 if (info->x_char)
1889 info->x_char = 0;
1890 else {
Jiri Slaby55b307d2006-12-08 02:38:14 -08001891 if (info->board->chip_flag) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001892 info->IER |= MOXA_MUST_RECV_ISR;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001893 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby925e9c12006-12-08 02:38:30 -08001894 } else {
Jiri Slaby037ad482006-12-08 02:38:11 -08001895 info->x_char = START_CHAR(tty);
Jiri Slaby55b307d2006-12-08 02:38:14 -08001896 outb(0, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001897 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001898 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001899 }
Jiri Slaby037ad482006-12-08 02:38:11 -08001900 }
1901 }
1902
1903 if (info->tty->termios->c_cflag & CRTSCTS) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001904 info->MCR |= UART_MCR_RTS;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001905 outb(info->MCR, info->ioaddr + UART_MCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08001906 }
1907}
1908
1909/*
Jiri Slaby037ad482006-12-08 02:38:11 -08001910 * mxser_stop() and mxser_start()
1911 *
1912 * This routines are called before setting or resetting tty->stopped.
1913 * They enable or disable transmitter interrupts, as necessary.
1914 */
1915static void mxser_stop(struct tty_struct *tty)
1916{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001917 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001918 unsigned long flags;
1919
1920 spin_lock_irqsave(&info->slock, flags);
1921 if (info->IER & UART_IER_THRI) {
1922 info->IER &= ~UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001923 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001924 }
1925 spin_unlock_irqrestore(&info->slock, flags);
1926}
1927
1928static void mxser_start(struct tty_struct *tty)
1929{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001930 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001931 unsigned long flags;
1932
1933 spin_lock_irqsave(&info->slock, flags);
Jiri Slabye079f492006-12-08 02:38:31 -08001934 if (info->xmit_cnt && info->xmit_buf) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08001935 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001936 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001937 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001938 }
1939 spin_unlock_irqrestore(&info->slock, flags);
1940}
1941
Alan Cox606d0992006-12-08 02:38:45 -08001942static void mxser_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001943{
1944 struct mxser_port *info = tty->driver_data;
1945 unsigned long flags;
1946
1947 if ((tty->termios->c_cflag != old_termios->c_cflag) ||
1948 (RELEVANT_IFLAG(tty->termios->c_iflag) != RELEVANT_IFLAG(old_termios->c_iflag))) {
1949
1950 mxser_change_speed(info, old_termios);
1951
1952 if ((old_termios->c_cflag & CRTSCTS) &&
1953 !(tty->termios->c_cflag & CRTSCTS)) {
1954 tty->hw_stopped = 0;
1955 mxser_start(tty);
1956 }
1957 }
1958
Jiri Slabye079f492006-12-08 02:38:31 -08001959 /* Handle sw stopped */
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001960 if ((old_termios->c_iflag & IXON) &&
1961 !(tty->termios->c_iflag & IXON)) {
1962 tty->stopped = 0;
1963
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001964 if (info->board->chip_flag) {
1965 spin_lock_irqsave(&info->slock, flags);
1966 DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
1967 spin_unlock_irqrestore(&info->slock, flags);
1968 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001969
1970 mxser_start(tty);
1971 }
1972}
1973
Jiri Slaby037ad482006-12-08 02:38:11 -08001974/*
1975 * mxser_wait_until_sent() --- wait until the transmitter is empty
1976 */
1977static void mxser_wait_until_sent(struct tty_struct *tty, int timeout)
1978{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001979 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001980 unsigned long orig_jiffies, char_time;
1981 int lsr;
1982
1983 if (info->type == PORT_UNKNOWN)
1984 return;
1985
1986 if (info->xmit_fifo_size == 0)
1987 return; /* Just in case.... */
1988
1989 orig_jiffies = jiffies;
1990 /*
1991 * Set the check interval to be 1/5 of the estimated time to
1992 * send a single character, and make it at least 1. The check
1993 * interval should also be less than the timeout.
1994 *
1995 * Note: we have to use pretty tight timings here to satisfy
1996 * the NIST-PCTS.
1997 */
1998 char_time = (info->timeout - HZ / 50) / info->xmit_fifo_size;
1999 char_time = char_time / 5;
2000 if (char_time == 0)
2001 char_time = 1;
2002 if (timeout && timeout < char_time)
2003 char_time = timeout;
2004 /*
2005 * If the transmitter hasn't cleared in twice the approximate
2006 * amount of time to send the entire FIFO, it probably won't
2007 * ever clear. This assumes the UART isn't doing flow
2008 * control, which is currently the case. Hence, if it ever
2009 * takes longer than info->timeout, this is probably due to a
2010 * UART bug of some kind. So, we clamp the timeout parameter at
2011 * 2*info->timeout.
2012 */
2013 if (!timeout || timeout > 2 * info->timeout)
2014 timeout = 2 * info->timeout;
2015#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2016 printk(KERN_DEBUG "In rs_wait_until_sent(%d) check=%lu...",
2017 timeout, char_time);
2018 printk("jiff=%lu...", jiffies);
2019#endif
Jiri Slaby55b307d2006-12-08 02:38:14 -08002020 while (!((lsr = inb(info->ioaddr + UART_LSR)) & UART_LSR_TEMT)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002021#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2022 printk("lsr = %d (jiff=%lu)...", lsr, jiffies);
2023#endif
2024 schedule_timeout_interruptible(char_time);
2025 if (signal_pending(current))
2026 break;
2027 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2028 break;
2029 }
2030 set_current_state(TASK_RUNNING);
2031
2032#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2033 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
2034#endif
2035}
2036
Jiri Slaby037ad482006-12-08 02:38:11 -08002037/*
2038 * This routine is called by tty_hangup() when a hangup is signaled.
2039 */
Adrian Bunk02d3fca2007-02-10 01:44:51 -08002040static void mxser_hangup(struct tty_struct *tty)
Jiri Slaby037ad482006-12-08 02:38:11 -08002041{
Jiri Slaby55b307d2006-12-08 02:38:14 -08002042 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08002043
2044 mxser_flush_buffer(tty);
2045 mxser_shutdown(info);
2046 info->event = 0;
2047 info->count = 0;
2048 info->flags &= ~ASYNC_NORMAL_ACTIVE;
2049 info->tty = NULL;
2050 wake_up_interruptible(&info->open_wait);
2051}
2052
Jiri Slaby037ad482006-12-08 02:38:11 -08002053/*
2054 * mxser_rs_break() --- routine which turns the break handling on or off
2055 */
2056static void mxser_rs_break(struct tty_struct *tty, int break_state)
2057{
Jiri Slaby55b307d2006-12-08 02:38:14 -08002058 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08002059 unsigned long flags;
2060
2061 spin_lock_irqsave(&info->slock, flags);
2062 if (break_state == -1)
Jiri Slaby55b307d2006-12-08 02:38:14 -08002063 outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
2064 info->ioaddr + UART_LCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002065 else
Jiri Slaby55b307d2006-12-08 02:38:14 -08002066 outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
2067 info->ioaddr + UART_LCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002068 spin_unlock_irqrestore(&info->slock, flags);
2069}
2070
Jiri Slaby55b307d2006-12-08 02:38:14 -08002071static void mxser_receive_chars(struct mxser_port *port, int *status)
Jiri Slaby037ad482006-12-08 02:38:11 -08002072{
Jiri Slaby55b307d2006-12-08 02:38:14 -08002073 struct tty_struct *tty = port->tty;
Jiri Slaby037ad482006-12-08 02:38:11 -08002074 unsigned char ch, gdl;
2075 int ignored = 0;
2076 int cnt = 0;
2077 int recv_room;
2078 int max = 256;
Jiri Slaby037ad482006-12-08 02:38:11 -08002079
2080 recv_room = tty->receive_room;
Jiri Slabye079f492006-12-08 02:38:31 -08002081 if ((recv_room == 0) && (!port->ldisc_stop_rx))
Jiri Slaby037ad482006-12-08 02:38:11 -08002082 mxser_stoprx(tty);
Jiri Slaby037ad482006-12-08 02:38:11 -08002083
Jiri Slaby55b307d2006-12-08 02:38:14 -08002084 if (port->board->chip_flag != MOXA_OTHER_UART) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002085
Jiri Slaby3306ce32006-12-08 02:38:13 -08002086 if (*status & UART_LSR_SPECIAL)
Jiri Slaby037ad482006-12-08 02:38:11 -08002087 goto intr_old;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002088 if (port->board->chip_flag == MOXA_MUST_MU860_HWID &&
Jiri Slaby037ad482006-12-08 02:38:11 -08002089 (*status & MOXA_MUST_LSR_RERR))
2090 goto intr_old;
Jiri Slaby037ad482006-12-08 02:38:11 -08002091 if (*status & MOXA_MUST_LSR_RERR)
2092 goto intr_old;
2093
Jiri Slaby55b307d2006-12-08 02:38:14 -08002094 gdl = inb(port->ioaddr + MOXA_MUST_GDL_REGISTER);
Jiri Slaby037ad482006-12-08 02:38:11 -08002095
Jiri Slaby55b307d2006-12-08 02:38:14 -08002096 if (port->board->chip_flag == MOXA_MUST_MU150_HWID)
Jiri Slaby037ad482006-12-08 02:38:11 -08002097 gdl &= MOXA_MUST_GDL_MASK;
2098 if (gdl >= recv_room) {
Jiri Slabye079f492006-12-08 02:38:31 -08002099 if (!port->ldisc_stop_rx)
Jiri Slaby037ad482006-12-08 02:38:11 -08002100 mxser_stoprx(tty);
Jiri Slaby037ad482006-12-08 02:38:11 -08002101 }
2102 while (gdl--) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002103 ch = inb(port->ioaddr + UART_RX);
Jiri Slaby037ad482006-12-08 02:38:11 -08002104 tty_insert_flip_char(tty, ch, 0);
2105 cnt++;
Jiri Slaby037ad482006-12-08 02:38:11 -08002106 }
2107 goto end_intr;
2108 }
Jiri Slabye079f492006-12-08 02:38:31 -08002109intr_old:
Jiri Slaby037ad482006-12-08 02:38:11 -08002110
2111 do {
2112 if (max-- < 0)
2113 break;
Jiri Slaby037ad482006-12-08 02:38:11 -08002114
Jiri Slaby55b307d2006-12-08 02:38:14 -08002115 ch = inb(port->ioaddr + UART_RX);
Jiri Slabye079f492006-12-08 02:38:31 -08002116 if (port->board->chip_flag && (*status & UART_LSR_OE))
Jiri Slaby55b307d2006-12-08 02:38:14 -08002117 outb(0x23, port->ioaddr + UART_FCR);
2118 *status &= port->read_status_mask;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002119 if (*status & port->ignore_status_mask) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002120 if (++ignored > 100)
2121 break;
2122 } else {
2123 char flag = 0;
2124 if (*status & UART_LSR_SPECIAL) {
2125 if (*status & UART_LSR_BI) {
2126 flag = TTY_BREAK;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002127 port->icount.brk++;
Jiri Slaby3306ce32006-12-08 02:38:13 -08002128
Jiri Slaby55b307d2006-12-08 02:38:14 -08002129 if (port->flags & ASYNC_SAK)
Jiri Slaby037ad482006-12-08 02:38:11 -08002130 do_SAK(tty);
2131 } else if (*status & UART_LSR_PE) {
2132 flag = TTY_PARITY;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002133 port->icount.parity++;
Jiri Slaby037ad482006-12-08 02:38:11 -08002134 } else if (*status & UART_LSR_FE) {
2135 flag = TTY_FRAME;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002136 port->icount.frame++;
Jiri Slaby037ad482006-12-08 02:38:11 -08002137 } else if (*status & UART_LSR_OE) {
2138 flag = TTY_OVERRUN;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002139 port->icount.overrun++;
Jiri Slaby925e9c12006-12-08 02:38:30 -08002140 }
2141 }
Jiri Slaby037ad482006-12-08 02:38:11 -08002142 tty_insert_flip_char(tty, ch, flag);
2143 cnt++;
2144 if (cnt >= recv_room) {
Jiri Slabye079f492006-12-08 02:38:31 -08002145 if (!port->ldisc_stop_rx)
Jiri Slaby037ad482006-12-08 02:38:11 -08002146 mxser_stoprx(tty);
Jiri Slaby037ad482006-12-08 02:38:11 -08002147 break;
2148 }
2149
2150 }
2151
Jiri Slaby55b307d2006-12-08 02:38:14 -08002152 if (port->board->chip_flag)
Jiri Slaby037ad482006-12-08 02:38:11 -08002153 break;
Jiri Slaby037ad482006-12-08 02:38:11 -08002154
Jiri Slaby55b307d2006-12-08 02:38:14 -08002155 *status = inb(port->ioaddr + UART_LSR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002156 } while (*status & UART_LSR_DR);
2157
Jiri Slabye079f492006-12-08 02:38:31 -08002158end_intr:
Jiri Slaby55b307d2006-12-08 02:38:14 -08002159 mxvar_log.rxcnt[port->tty->index] += cnt;
2160 port->mon_data.rxcnt += cnt;
2161 port->mon_data.up_rxcnt += cnt;
Jiri Slaby037ad482006-12-08 02:38:11 -08002162
2163 tty_flip_buffer_push(tty);
2164}
2165
Jiri Slaby55b307d2006-12-08 02:38:14 -08002166static void mxser_transmit_chars(struct mxser_port *port)
Jiri Slaby037ad482006-12-08 02:38:11 -08002167{
2168 int count, cnt;
Jiri Slaby037ad482006-12-08 02:38:11 -08002169
Jiri Slaby55b307d2006-12-08 02:38:14 -08002170 if (port->x_char) {
2171 outb(port->x_char, port->ioaddr + UART_TX);
2172 port->x_char = 0;
2173 mxvar_log.txcnt[port->tty->index]++;
2174 port->mon_data.txcnt++;
2175 port->mon_data.up_txcnt++;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002176 port->icount.tx++;
Jiri Slabyf5748742007-02-10 01:45:18 -08002177 return;
Jiri Slaby037ad482006-12-08 02:38:11 -08002178 }
2179
Jiri Slaby55b307d2006-12-08 02:38:14 -08002180 if (port->xmit_buf == 0)
Jiri Slabyf5748742007-02-10 01:45:18 -08002181 return;
Jiri Slaby037ad482006-12-08 02:38:11 -08002182
Jiri Slaby925e9c12006-12-08 02:38:30 -08002183 if ((port->xmit_cnt <= 0) || port->tty->stopped ||
2184 (port->tty->hw_stopped &&
Jiri Slaby55b307d2006-12-08 02:38:14 -08002185 (port->type != PORT_16550A) &&
2186 (!port->board->chip_flag))) {
2187 port->IER &= ~UART_IER_THRI;
2188 outb(port->IER, port->ioaddr + UART_IER);
Jiri Slabyf5748742007-02-10 01:45:18 -08002189 return;
Jiri Slaby037ad482006-12-08 02:38:11 -08002190 }
2191
Jiri Slaby55b307d2006-12-08 02:38:14 -08002192 cnt = port->xmit_cnt;
2193 count = port->xmit_fifo_size;
Jiri Slaby037ad482006-12-08 02:38:11 -08002194 do {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002195 outb(port->xmit_buf[port->xmit_tail++],
2196 port->ioaddr + UART_TX);
2197 port->xmit_tail = port->xmit_tail & (SERIAL_XMIT_SIZE - 1);
2198 if (--port->xmit_cnt <= 0)
Jiri Slaby037ad482006-12-08 02:38:11 -08002199 break;
2200 } while (--count > 0);
Jiri Slaby55b307d2006-12-08 02:38:14 -08002201 mxvar_log.txcnt[port->tty->index] += (cnt - port->xmit_cnt);
Jiri Slaby037ad482006-12-08 02:38:11 -08002202
Jiri Slaby55b307d2006-12-08 02:38:14 -08002203 port->mon_data.txcnt += (cnt - port->xmit_cnt);
2204 port->mon_data.up_txcnt += (cnt - port->xmit_cnt);
Jiri Slaby55b307d2006-12-08 02:38:14 -08002205 port->icount.tx += (cnt - port->xmit_cnt);
Jiri Slaby037ad482006-12-08 02:38:11 -08002206
Jiri Slaby47c85c02007-02-10 01:45:15 -08002207 if (port->xmit_cnt < WAKEUP_CHARS)
2208 tty_wakeup(port->tty);
2209
Jiri Slaby55b307d2006-12-08 02:38:14 -08002210 if (port->xmit_cnt <= 0) {
2211 port->IER &= ~UART_IER_THRI;
2212 outb(port->IER, port->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08002213 }
Jiri Slaby037ad482006-12-08 02:38:11 -08002214}
2215
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002216/*
2217 * This is the serial driver's generic interrupt routine
2218 */
Jiri Slabyb1d1c8d2006-12-08 02:38:31 -08002219static irqreturn_t mxser_interrupt(int irq, void *dev_id)
Jiri Slaby037ad482006-12-08 02:38:11 -08002220{
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002221 int status, iir, i;
2222 struct mxser_board *brd = NULL;
2223 struct mxser_port *port;
2224 int max, irqbits, bits, msr;
Jiri Slaby18b95572007-02-10 01:45:19 -08002225 unsigned int int_cnt, pass_counter = 0;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002226 int handled = IRQ_NONE;
Jiri Slaby037ad482006-12-08 02:38:11 -08002227
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002228 for (i = 0; i < MXSER_BOARDS; i++)
2229 if (dev_id == &mxser_boards[i]) {
2230 brd = dev_id;
2231 break;
Jiri Slaby037ad482006-12-08 02:38:11 -08002232 }
Jiri Slaby037ad482006-12-08 02:38:11 -08002233
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002234 if (i == MXSER_BOARDS)
2235 goto irq_stop;
2236 if (brd == NULL)
2237 goto irq_stop;
Jiri Slabycd7ed642006-12-08 02:38:29 -08002238 max = brd->info->nports;
Jiri Slaby18b95572007-02-10 01:45:19 -08002239 while (pass_counter++ < MXSER_ISR_PASS_LIMIT) {
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002240 irqbits = inb(brd->vector) & brd->vector_mask;
2241 if (irqbits == brd->vector_mask)
Jiri Slaby037ad482006-12-08 02:38:11 -08002242 break;
Jiri Slaby037ad482006-12-08 02:38:11 -08002243
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002244 handled = IRQ_HANDLED;
2245 for (i = 0, bits = 1; i < max; i++, irqbits |= bits, bits <<= 1) {
2246 if (irqbits == brd->vector_mask)
Jiri Slaby037ad482006-12-08 02:38:11 -08002247 break;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002248 if (bits & irqbits)
2249 continue;
2250 port = &brd->ports[i];
Jiri Slaby037ad482006-12-08 02:38:11 -08002251
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002252 int_cnt = 0;
Jiri Slabyf5748742007-02-10 01:45:18 -08002253 spin_lock(&port->slock);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002254 do {
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002255 iir = inb(port->ioaddr + UART_IIR);
2256 if (iir & UART_IIR_NO_INT)
2257 break;
2258 iir &= MOXA_MUST_IIR_MASK;
Jiri Slabyf5748742007-02-10 01:45:18 -08002259 if (!port->tty ||
2260 (port->flags & ASYNC_CLOSING) ||
2261 !(port->flags &
2262 ASYNC_INITIALIZED)) {
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002263 status = inb(port->ioaddr + UART_LSR);
2264 outb(0x27, port->ioaddr + UART_FCR);
2265 inb(port->ioaddr + UART_MSR);
2266 break;
Jiri Slaby037ad482006-12-08 02:38:11 -08002267 }
Jiri Slaby037ad482006-12-08 02:38:11 -08002268
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002269 status = inb(port->ioaddr + UART_LSR);
2270
2271 if (status & UART_LSR_PE)
2272 port->err_shadow |= NPPI_NOTIFY_PARITY;
2273 if (status & UART_LSR_FE)
2274 port->err_shadow |= NPPI_NOTIFY_FRAMING;
2275 if (status & UART_LSR_OE)
2276 port->err_shadow |=
2277 NPPI_NOTIFY_HW_OVERRUN;
2278 if (status & UART_LSR_BI)
2279 port->err_shadow |= NPPI_NOTIFY_BREAK;
2280
2281 if (port->board->chip_flag) {
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002282 if (iir == MOXA_MUST_IIR_GDA ||
2283 iir == MOXA_MUST_IIR_RDA ||
2284 iir == MOXA_MUST_IIR_RTO ||
2285 iir == MOXA_MUST_IIR_LSR)
2286 mxser_receive_chars(port,
2287 &status);
2288
2289 } else {
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002290 status &= port->read_status_mask;
2291 if (status & UART_LSR_DR)
2292 mxser_receive_chars(port,
2293 &status);
2294 }
2295 msr = inb(port->ioaddr + UART_MSR);
2296 if (msr & UART_MSR_ANY_DELTA)
2297 mxser_check_modem_status(port, msr);
2298
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002299 if (port->board->chip_flag) {
2300 if (iir == 0x02 && (status &
2301 UART_LSR_THRE))
2302 mxser_transmit_chars(port);
2303 } else {
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002304 if (status & UART_LSR_THRE)
2305 mxser_transmit_chars(port);
2306 }
2307 } while (int_cnt++ < MXSER_ISR_PASS_LIMIT);
Jiri Slabyf5748742007-02-10 01:45:18 -08002308 spin_unlock(&port->slock);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002309 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002310 }
2311
Jiri Slabye079f492006-12-08 02:38:31 -08002312irq_stop:
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002313 return handled;
2314}
2315
2316static const struct tty_operations mxser_ops = {
2317 .open = mxser_open,
2318 .close = mxser_close,
2319 .write = mxser_write,
2320 .put_char = mxser_put_char,
2321 .flush_chars = mxser_flush_chars,
2322 .write_room = mxser_write_room,
2323 .chars_in_buffer = mxser_chars_in_buffer,
2324 .flush_buffer = mxser_flush_buffer,
2325 .ioctl = mxser_ioctl,
2326 .throttle = mxser_throttle,
2327 .unthrottle = mxser_unthrottle,
2328 .set_termios = mxser_set_termios,
2329 .stop = mxser_stop,
2330 .start = mxser_start,
2331 .hangup = mxser_hangup,
2332 .break_ctl = mxser_rs_break,
2333 .wait_until_sent = mxser_wait_until_sent,
2334 .tiocmget = mxser_tiocmget,
2335 .tiocmset = mxser_tiocmset,
2336};
2337
2338/*
2339 * The MOXA Smartio/Industio serial driver boot-time initialization code!
2340 */
2341
Jiri Slaby2094e752006-12-08 02:38:33 -08002342static void mxser_release_res(struct mxser_board *brd, struct pci_dev *pdev,
2343 unsigned int irq)
Jiri Slaby171d3a82006-12-08 02:38:25 -08002344{
Jiri Slaby171d3a82006-12-08 02:38:25 -08002345 if (irq)
2346 free_irq(brd->irq, brd);
2347 if (pdev != NULL) { /* PCI */
Jiri Slabyfb0c9292006-12-13 00:34:19 -08002348#ifdef CONFIG_PCI
Jiri Slaby171d3a82006-12-08 02:38:25 -08002349 pci_release_region(pdev, 2);
2350 pci_release_region(pdev, 3);
2351 pci_dev_put(pdev);
Jiri Slabyfb0c9292006-12-13 00:34:19 -08002352#endif
Jiri Slaby171d3a82006-12-08 02:38:25 -08002353 } else {
Jiri Slabycd7ed642006-12-08 02:38:29 -08002354 release_region(brd->ports[0].ioaddr, 8 * brd->info->nports);
Jiri Slaby171d3a82006-12-08 02:38:25 -08002355 release_region(brd->vector, 1);
2356 }
2357}
2358
Jiri Slaby2094e752006-12-08 02:38:33 -08002359static int __devinit mxser_initbrd(struct mxser_board *brd,
2360 struct pci_dev *pdev)
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002361{
2362 struct mxser_port *info;
2363 unsigned int i;
2364 int retval;
2365
2366 printk(KERN_INFO "max. baud rate = %d bps.\n", brd->ports[0].max_baud);
2367
Jiri Slabycd7ed642006-12-08 02:38:29 -08002368 for (i = 0; i < brd->info->nports; i++) {
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002369 info = &brd->ports[i];
2370 info->board = brd;
2371 info->stop_rx = 0;
2372 info->ldisc_stop_rx = 0;
2373
2374 /* Enhance mode enabled here */
2375 if (brd->chip_flag != MOXA_OTHER_UART)
2376 ENABLE_MOXA_MUST_ENCHANCE_MODE(info->ioaddr);
2377
2378 info->flags = ASYNC_SHARE_IRQ;
2379 info->type = brd->uart_type;
2380
2381 process_txrx_fifo(info);
2382
2383 info->custom_divisor = info->baud_base * 16;
2384 info->close_delay = 5 * HZ / 10;
2385 info->closing_wait = 30 * HZ;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002386 info->normal_termios = mxvar_sdriver->init_termios;
2387 init_waitqueue_head(&info->open_wait);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002388 init_waitqueue_head(&info->delta_msr_wait);
2389 memset(&info->mon_data, 0, sizeof(struct mxser_mon));
2390 info->err_shadow = 0;
2391 spin_lock_init(&info->slock);
2392
2393 /* before set INT ISR, disable all int */
2394 outb(inb(info->ioaddr + UART_IER) & 0xf0,
2395 info->ioaddr + UART_IER);
2396 }
Jiri Slaby037ad482006-12-08 02:38:11 -08002397
Jiri Slabyc493edd2007-02-10 01:45:16 -08002398 retval = request_irq(brd->irq, mxser_interrupt, IRQF_SHARED, "mxser",
2399 brd);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002400 if (retval) {
2401 printk(KERN_ERR "Board %s: Request irq failed, IRQ (%d) may "
2402 "conflict with another device.\n",
Jiri Slabycd7ed642006-12-08 02:38:29 -08002403 brd->info->name, brd->irq);
Jiri Slaby171d3a82006-12-08 02:38:25 -08002404 /* We hold resources, we need to release them. */
Jiri Slaby2094e752006-12-08 02:38:33 -08002405 mxser_release_res(brd, pdev, 0);
Jiri Slaby037ad482006-12-08 02:38:11 -08002406 }
Jiri Slaby45257fa2007-02-10 01:45:20 -08002407 return retval;
Jiri Slaby037ad482006-12-08 02:38:11 -08002408}
2409
Jiri Slaby943f2952006-12-08 02:38:15 -08002410static int __init mxser_get_ISA_conf(int cap, struct mxser_board *brd)
Jiri Slaby037ad482006-12-08 02:38:11 -08002411{
2412 int id, i, bits;
2413 unsigned short regs[16], irq;
2414 unsigned char scratch, scratch2;
2415
Jiri Slaby55b307d2006-12-08 02:38:14 -08002416 brd->chip_flag = MOXA_OTHER_UART;
Jiri Slaby037ad482006-12-08 02:38:11 -08002417
2418 id = mxser_read_register(cap, regs);
Jiri Slabycd7ed642006-12-08 02:38:29 -08002419 switch (id) {
2420 case C168_ASIC_ID:
2421 brd->info = &mxser_cards[0];
2422 break;
2423 case C104_ASIC_ID:
2424 brd->info = &mxser_cards[1];
2425 break;
2426 case CI104J_ASIC_ID:
2427 brd->info = &mxser_cards[2];
2428 break;
2429 case C102_ASIC_ID:
2430 brd->info = &mxser_cards[5];
2431 break;
2432 case CI132_ASIC_ID:
2433 brd->info = &mxser_cards[6];
2434 break;
2435 case CI134_ASIC_ID:
2436 brd->info = &mxser_cards[7];
2437 break;
2438 default:
Jiri Slaby037ad482006-12-08 02:38:11 -08002439 return 0;
Jiri Slabycd7ed642006-12-08 02:38:29 -08002440 }
Jiri Slaby037ad482006-12-08 02:38:11 -08002441
2442 irq = 0;
Jiri Slabycd7ed642006-12-08 02:38:29 -08002443 /* some ISA cards have 2 ports, but we want to see them as 4-port (why?)
2444 Flag-hack checks if configuration should be read as 2-port here. */
2445 if (brd->info->nports == 2 || (brd->info->flags & MXSER_HAS2)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002446 irq = regs[9] & 0xF000;
2447 irq = irq | (irq >> 4);
2448 if (irq != (regs[9] & 0xFF00))
2449 return MXSER_ERR_IRQ_CONFLIT;
Jiri Slabycd7ed642006-12-08 02:38:29 -08002450 } else if (brd->info->nports == 4) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002451 irq = regs[9] & 0xF000;
2452 irq = irq | (irq >> 4);
2453 irq = irq | (irq >> 8);
2454 if (irq != regs[9])
2455 return MXSER_ERR_IRQ_CONFLIT;
Jiri Slabycd7ed642006-12-08 02:38:29 -08002456 } else if (brd->info->nports == 8) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002457 irq = regs[9] & 0xF000;
2458 irq = irq | (irq >> 4);
2459 irq = irq | (irq >> 8);
2460 if ((irq != regs[9]) || (irq != regs[10]))
2461 return MXSER_ERR_IRQ_CONFLIT;
2462 }
2463
2464 if (!irq)
2465 return MXSER_ERR_IRQ;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002466 brd->irq = ((int)(irq & 0xF000) >> 12);
Jiri Slaby037ad482006-12-08 02:38:11 -08002467 for (i = 0; i < 8; i++)
Jiri Slaby55b307d2006-12-08 02:38:14 -08002468 brd->ports[i].ioaddr = (int) regs[i + 1] & 0xFFF8;
Jiri Slaby037ad482006-12-08 02:38:11 -08002469 if ((regs[12] & 0x80) == 0)
2470 return MXSER_ERR_VECTOR;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002471 brd->vector = (int)regs[11]; /* interrupt vector */
Jiri Slaby037ad482006-12-08 02:38:11 -08002472 if (id == 1)
Jiri Slaby55b307d2006-12-08 02:38:14 -08002473 brd->vector_mask = 0x00FF;
Jiri Slaby037ad482006-12-08 02:38:11 -08002474 else
Jiri Slaby55b307d2006-12-08 02:38:14 -08002475 brd->vector_mask = 0x000F;
Jiri Slaby037ad482006-12-08 02:38:11 -08002476 for (i = 7, bits = 0x0100; i >= 0; i--, bits <<= 1) {
2477 if (regs[12] & bits) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002478 brd->ports[i].baud_base = 921600;
Jiri Slabye079f492006-12-08 02:38:31 -08002479 brd->ports[i].max_baud = 921600;
Jiri Slaby037ad482006-12-08 02:38:11 -08002480 } else {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002481 brd->ports[i].baud_base = 115200;
Jiri Slabye079f492006-12-08 02:38:31 -08002482 brd->ports[i].max_baud = 115200;
Jiri Slaby037ad482006-12-08 02:38:11 -08002483 }
2484 }
2485 scratch2 = inb(cap + UART_LCR) & (~UART_LCR_DLAB);
2486 outb(scratch2 | UART_LCR_DLAB, cap + UART_LCR);
2487 outb(0, cap + UART_EFR); /* EFR is the same as FCR */
2488 outb(scratch2, cap + UART_LCR);
2489 outb(UART_FCR_ENABLE_FIFO, cap + UART_FCR);
2490 scratch = inb(cap + UART_IIR);
2491
2492 if (scratch & 0xC0)
Jiri Slaby55b307d2006-12-08 02:38:14 -08002493 brd->uart_type = PORT_16550A;
Jiri Slaby037ad482006-12-08 02:38:11 -08002494 else
Jiri Slaby55b307d2006-12-08 02:38:14 -08002495 brd->uart_type = PORT_16450;
Jiri Slabycd7ed642006-12-08 02:38:29 -08002496 if (!request_region(brd->ports[0].ioaddr, 8 * brd->info->nports,
2497 "mxser(IO)"))
Jiri Slaby7a7a5c32006-12-08 02:38:17 -08002498 return MXSER_ERR_IOADDR;
2499 if (!request_region(brd->vector, 1, "mxser(vector)")) {
Jiri Slabycd7ed642006-12-08 02:38:29 -08002500 release_region(brd->ports[0].ioaddr, 8 * brd->info->nports);
Jiri Slaby7a7a5c32006-12-08 02:38:17 -08002501 return MXSER_ERR_VECTOR;
2502 }
Jiri Slabycd7ed642006-12-08 02:38:29 -08002503 return brd->info->nports;
Jiri Slaby037ad482006-12-08 02:38:11 -08002504}
2505
Jiri Slaby2094e752006-12-08 02:38:33 -08002506static int __devinit mxser_probe(struct pci_dev *pdev,
2507 const struct pci_device_id *ent)
Jiri Slaby037ad482006-12-08 02:38:11 -08002508{
Jiri Slabyfb0c9292006-12-13 00:34:19 -08002509#ifdef CONFIG_PCI
Jiri Slaby2094e752006-12-08 02:38:33 -08002510 struct mxser_board *brd;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002511 unsigned int i, j;
2512 unsigned long ioaddress;
Jiri Slaby2094e752006-12-08 02:38:33 -08002513 int retval = -EINVAL;
2514
2515 for (i = 0; i < MXSER_BOARDS; i++)
2516 if (mxser_boards[i].info == NULL)
2517 break;
2518
2519 if (i >= MXSER_BOARDS) {
2520 printk(KERN_ERR "Too many Smartio/Industio family boards found "
2521 "(maximum %d), board not configured\n", MXSER_BOARDS);
2522 goto err;
2523 }
2524
2525 brd = &mxser_boards[i];
2526 brd->idx = i * MXSER_PORTS_PER_BOARD;
2527 printk(KERN_INFO "Found MOXA %s board (BusNo=%d, DevNo=%d)\n",
2528 mxser_cards[ent->driver_data].name,
2529 pdev->bus->number, PCI_SLOT(pdev->devfn));
2530
2531 retval = pci_enable_device(pdev);
2532 if (retval) {
2533 printk(KERN_ERR "Moxa SmartI/O PCI enable fail !\n");
2534 goto err;
2535 }
Jiri Slaby037ad482006-12-08 02:38:11 -08002536
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002537 /* io address */
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002538 ioaddress = pci_resource_start(pdev, 2);
2539 retval = pci_request_region(pdev, 2, "mxser(IO)");
2540 if (retval)
2541 goto err;
2542
Jiri Slaby2094e752006-12-08 02:38:33 -08002543 brd->info = &mxser_cards[ent->driver_data];
Jiri Slabycd7ed642006-12-08 02:38:29 -08002544 for (i = 0; i < brd->info->nports; i++)
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002545 brd->ports[i].ioaddr = ioaddress + 8 * i;
2546
2547 /* vector */
2548 ioaddress = pci_resource_start(pdev, 3);
2549 retval = pci_request_region(pdev, 3, "mxser(vector)");
2550 if (retval)
2551 goto err_relio;
2552 brd->vector = ioaddress;
2553
2554 /* irq */
2555 brd->irq = pdev->irq;
2556
2557 brd->chip_flag = CheckIsMoxaMust(brd->ports[0].ioaddr);
2558 brd->uart_type = PORT_16550A;
2559 brd->vector_mask = 0;
2560
Jiri Slabycd7ed642006-12-08 02:38:29 -08002561 for (i = 0; i < brd->info->nports; i++) {
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002562 for (j = 0; j < UART_INFO_NUM; j++) {
2563 if (Gpci_uart_info[j].type == brd->chip_flag) {
2564 brd->ports[i].max_baud =
2565 Gpci_uart_info[j].max_baud;
2566
2567 /* exception....CP-102 */
Jiri Slabycd7ed642006-12-08 02:38:29 -08002568 if (brd->info->flags & MXSER_HIGHBAUD)
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002569 brd->ports[i].max_baud = 921600;
2570 break;
Jiri Slaby037ad482006-12-08 02:38:11 -08002571 }
2572 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002573 }
2574
2575 if (brd->chip_flag == MOXA_MUST_MU860_HWID) {
Jiri Slabycd7ed642006-12-08 02:38:29 -08002576 for (i = 0; i < brd->info->nports; i++) {
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002577 if (i < 4)
2578 brd->ports[i].opmode_ioaddr = ioaddress + 4;
2579 else
2580 brd->ports[i].opmode_ioaddr = ioaddress + 0x0c;
Jiri Slaby037ad482006-12-08 02:38:11 -08002581 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002582 outb(0, ioaddress + 4); /* default set to RS232 mode */
2583 outb(0, ioaddress + 0x0c); /* default set to RS232 mode */
Jiri Slaby037ad482006-12-08 02:38:11 -08002584 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002585
Jiri Slabycd7ed642006-12-08 02:38:29 -08002586 for (i = 0; i < brd->info->nports; i++) {
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002587 brd->vector_mask |= (1 << i);
2588 brd->ports[i].baud_base = 921600;
2589 }
Jiri Slaby2094e752006-12-08 02:38:33 -08002590
2591 /* mxser_initbrd will hook ISR. */
Jiri Slaby45257fa2007-02-10 01:45:20 -08002592 retval = mxser_initbrd(brd, pdev);
2593 if (retval)
2594 goto err_null;
Jiri Slaby2094e752006-12-08 02:38:33 -08002595
2596 for (i = 0; i < brd->info->nports; i++)
2597 tty_register_device(mxvar_sdriver, brd->idx + i, &pdev->dev);
2598
2599 pci_set_drvdata(pdev, brd);
2600
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002601 return 0;
2602err_relio:
2603 pci_release_region(pdev, 2);
Jiri Slaby45257fa2007-02-10 01:45:20 -08002604err_null:
Jiri Slaby2094e752006-12-08 02:38:33 -08002605 brd->info = NULL;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002606err:
2607 return retval;
Jiri Slabyfb0c9292006-12-13 00:34:19 -08002608#else
2609 return -ENODEV;
2610#endif
Jiri Slaby037ad482006-12-08 02:38:11 -08002611}
2612
Jiri Slaby2094e752006-12-08 02:38:33 -08002613static void __devexit mxser_remove(struct pci_dev *pdev)
2614{
2615 struct mxser_board *brd = pci_get_drvdata(pdev);
2616 unsigned int i;
2617
2618 for (i = 0; i < brd->info->nports; i++)
2619 tty_unregister_device(mxvar_sdriver, brd->idx + i);
2620
2621 mxser_release_res(brd, pdev, 1);
Jiri Slaby45257fa2007-02-10 01:45:20 -08002622 brd->info = NULL;
Jiri Slaby2094e752006-12-08 02:38:33 -08002623}
2624
2625static struct pci_driver mxser_driver = {
2626 .name = "mxser",
2627 .id_table = mxser_pcibrds,
2628 .probe = mxser_probe,
2629 .remove = __devexit_p(mxser_remove)
2630};
2631
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002632static int __init mxser_module_init(void)
Jiri Slaby037ad482006-12-08 02:38:11 -08002633{
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002634 struct mxser_board *brd;
Jiri Slabyeae44362006-12-08 02:38:27 -08002635 unsigned long cap;
2636 unsigned int i, m, isaloop;
Jiri Slaby2094e752006-12-08 02:38:33 -08002637 int retval, b;
Jiri Slaby037ad482006-12-08 02:38:11 -08002638
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002639 pr_debug("Loading module mxser ...\n");
Jiri Slaby037ad482006-12-08 02:38:11 -08002640
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002641 mxvar_sdriver = alloc_tty_driver(MXSER_PORTS + 1);
2642 if (!mxvar_sdriver)
2643 return -ENOMEM;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002644
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002645 printk(KERN_INFO "MOXA Smartio/Industio family driver version %s\n",
2646 MXSER_VERSION);
2647
2648 /* Initialize the tty_driver structure */
Jiri Slaby31f87cf2006-12-29 16:47:34 -08002649 mxvar_sdriver->owner = THIS_MODULE;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002650 mxvar_sdriver->magic = TTY_DRIVER_MAGIC;
Jiri Slabye9307902006-12-08 02:38:35 -08002651 mxvar_sdriver->name = "ttyMI";
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002652 mxvar_sdriver->major = ttymajor;
2653 mxvar_sdriver->minor_start = 0;
2654 mxvar_sdriver->num = MXSER_PORTS + 1;
2655 mxvar_sdriver->type = TTY_DRIVER_TYPE_SERIAL;
2656 mxvar_sdriver->subtype = SERIAL_TYPE_NORMAL;
2657 mxvar_sdriver->init_termios = tty_std_termios;
2658 mxvar_sdriver->init_termios.c_cflag = B9600|CS8|CREAD|HUPCL|CLOCAL;
Jiri Slaby938ef182006-12-08 02:38:28 -08002659 mxvar_sdriver->flags = TTY_DRIVER_REAL_RAW|TTY_DRIVER_DYNAMIC_DEV;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002660 tty_set_operations(mxvar_sdriver, &mxser_ops);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002661
Jiri Slaby938ef182006-12-08 02:38:28 -08002662 retval = tty_register_driver(mxvar_sdriver);
2663 if (retval) {
2664 printk(KERN_ERR "Couldn't install MOXA Smartio/Industio family "
2665 "tty driver !\n");
2666 goto err_put;
2667 }
2668
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002669 mxvar_diagflag = 0;
2670
2671 m = 0;
2672 /* Start finding ISA boards here */
Jiri Slabyeae44362006-12-08 02:38:27 -08002673 for (isaloop = 0; isaloop < 2; isaloop++)
2674 for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
2675 if (!isaloop)
2676 cap = mxserBoardCAP[b]; /* predefined */
2677 else
2678 cap = ioaddr[b]; /* module param */
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002679
Jiri Slabyeae44362006-12-08 02:38:27 -08002680 if (!cap)
2681 continue;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002682
Jiri Slabyeae44362006-12-08 02:38:27 -08002683 brd = &mxser_boards[m];
2684 retval = mxser_get_ISA_conf(cap, brd);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002685
Jiri Slabyeae44362006-12-08 02:38:27 -08002686 if (retval != 0)
2687 printk(KERN_INFO "Found MOXA %s board "
2688 "(CAP=0x%x)\n",
Jiri Slabycd7ed642006-12-08 02:38:29 -08002689 brd->info->name, ioaddr[b]);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002690
Jiri Slabyeae44362006-12-08 02:38:27 -08002691 if (retval <= 0) {
2692 if (retval == MXSER_ERR_IRQ)
2693 printk(KERN_ERR "Invalid interrupt "
2694 "number, board not "
2695 "configured\n");
2696 else if (retval == MXSER_ERR_IRQ_CONFLIT)
2697 printk(KERN_ERR "Invalid interrupt "
2698 "number, board not "
2699 "configured\n");
2700 else if (retval == MXSER_ERR_VECTOR)
2701 printk(KERN_ERR "Invalid interrupt "
2702 "vector, board not "
2703 "configured\n");
2704 else if (retval == MXSER_ERR_IOADDR)
2705 printk(KERN_ERR "Invalid I/O address, "
2706 "board not configured\n");
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002707
Jiri Slaby2094e752006-12-08 02:38:33 -08002708 brd->info = NULL;
Jiri Slabyeae44362006-12-08 02:38:27 -08002709 continue;
2710 }
2711
Jiri Slabyeae44362006-12-08 02:38:27 -08002712 /* mxser_initbrd will hook ISR. */
Jiri Slaby2094e752006-12-08 02:38:33 -08002713 if (mxser_initbrd(brd, NULL) < 0) {
2714 brd->info = NULL;
Jiri Slabyeae44362006-12-08 02:38:27 -08002715 continue;
Jiri Slaby2094e752006-12-08 02:38:33 -08002716 }
Jiri Slabyeae44362006-12-08 02:38:27 -08002717
Jiri Slaby2094e752006-12-08 02:38:33 -08002718 brd->idx = m * MXSER_PORTS_PER_BOARD;
Jiri Slabycd7ed642006-12-08 02:38:29 -08002719 for (i = 0; i < brd->info->nports; i++)
Jiri Slaby2094e752006-12-08 02:38:33 -08002720 tty_register_device(mxvar_sdriver, brd->idx + i,
2721 NULL);
Jiri Slaby938ef182006-12-08 02:38:28 -08002722
Jiri Slabyeae44362006-12-08 02:38:27 -08002723 m++;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002724 }
2725
Jiri Slaby2094e752006-12-08 02:38:33 -08002726 retval = pci_register_driver(&mxser_driver);
2727 if (retval) {
2728 printk(KERN_ERR "Can't register pci driver\n");
2729 if (!m) {
2730 retval = -ENODEV;
2731 goto err_unr;
2732 } /* else: we have some ISA cards under control */
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002733 }
2734
2735 pr_debug("Done.\n");
2736
Jiri Slaby938ef182006-12-08 02:38:28 -08002737 return 0;
2738err_unr:
2739 tty_unregister_driver(mxvar_sdriver);
2740err_put:
2741 put_tty_driver(mxvar_sdriver);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002742 return retval;
Jiri Slaby037ad482006-12-08 02:38:11 -08002743}
2744
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002745static void __exit mxser_module_exit(void)
Jiri Slaby037ad482006-12-08 02:38:11 -08002746{
Jiri Slaby2094e752006-12-08 02:38:33 -08002747 unsigned int i, j;
Jiri Slaby037ad482006-12-08 02:38:11 -08002748
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002749 pr_debug("Unloading module mxser ...\n");
2750
Jiri Slaby2094e752006-12-08 02:38:33 -08002751 pci_unregister_driver(&mxser_driver);
2752
2753 for (i = 0; i < MXSER_BOARDS; i++) /* ISA remains */
2754 if (mxser_boards[i].info != NULL)
2755 for (j = 0; j < mxser_boards[i].info->nports; j++)
2756 tty_unregister_device(mxvar_sdriver,
2757 mxser_boards[i].idx + j);
Jiri Slabyead568c2006-12-08 02:38:26 -08002758 tty_unregister_driver(mxvar_sdriver);
2759 put_tty_driver(mxvar_sdriver);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002760
Jiri Slaby171d3a82006-12-08 02:38:25 -08002761 for (i = 0; i < MXSER_BOARDS; i++)
Jiri Slabycd7ed642006-12-08 02:38:29 -08002762 if (mxser_boards[i].info != NULL)
Jiri Slaby2094e752006-12-08 02:38:33 -08002763 mxser_release_res(&mxser_boards[i], NULL, 1);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002764
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002765 pr_debug("Done.\n");
Jiri Slaby037ad482006-12-08 02:38:11 -08002766}
2767
2768module_init(mxser_module_init);
2769module_exit(mxser_module_exit);