blob: 71b965705177f4a9bb3a722bb8819d531c1904c8 [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>
Jiri Slaby037ad482006-12-08 02:38:11 -080040#include <linux/delay.h>
41#include <linux/pci.h>
42
43#include <asm/system.h>
44#include <asm/io.h>
45#include <asm/irq.h>
46#include <asm/bitops.h>
47#include <asm/uaccess.h>
48
Jiri Slaby771f2d12006-12-08 02:38:12 -080049#include "mxser_new.h"
Jiri Slaby037ad482006-12-08 02:38:11 -080050
Jiri Slabyf64c84a2007-02-10 01:45:21 -080051#define MXSER_VERSION "2.0.1" /* 1.9.15 */
Jiri Slaby037ad482006-12-08 02:38:11 -080052#define MXSERMAJOR 174
53#define MXSERCUMAJOR 175
54
Jiri Slaby037ad482006-12-08 02:38:11 -080055#define MXSER_BOARDS 4 /* Max. boards */
Jiri Slaby037ad482006-12-08 02:38:11 -080056#define MXSER_PORTS_PER_BOARD 8 /* Max. ports per board */
Jiri Slabya8b74de2006-12-08 02:38:34 -080057#define MXSER_PORTS (MXSER_BOARDS * MXSER_PORTS_PER_BOARD)
Jiri Slaby18b95572007-02-10 01:45:19 -080058#define MXSER_ISR_PASS_LIMIT 100
Jiri Slaby037ad482006-12-08 02:38:11 -080059
60#define MXSER_ERR_IOADDR -1
61#define MXSER_ERR_IRQ -2
62#define MXSER_ERR_IRQ_CONFLIT -3
63#define MXSER_ERR_VECTOR -4
64
Jiri Slabyed79ba12007-02-10 01:45:18 -080065/*CheckIsMoxaMust return value*/
66#define MOXA_OTHER_UART 0x00
67#define MOXA_MUST_MU150_HWID 0x01
68#define MOXA_MUST_MU860_HWID 0x02
69
Jiri Slaby037ad482006-12-08 02:38:11 -080070#define WAKEUP_CHARS 256
71
72#define UART_MCR_AFE 0x20
73#define UART_LSR_SPECIAL 0x1E
74
75#define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK|\
76 IXON|IXOFF))
77
Jiri Slaby037ad482006-12-08 02:38:11 -080078#define C168_ASIC_ID 1
79#define C104_ASIC_ID 2
80#define C102_ASIC_ID 0xB
81#define CI132_ASIC_ID 4
82#define CI134_ASIC_ID 3
83#define CI104J_ASIC_ID 5
84
Jiri Slabycd7ed642006-12-08 02:38:29 -080085#define MXSER_HIGHBAUD 1
86#define MXSER_HAS2 2
Jiri Slaby037ad482006-12-08 02:38:11 -080087
88/* This is only for PCI */
Jiri Slabycd7ed642006-12-08 02:38:29 -080089static const struct {
Jiri Slaby037ad482006-12-08 02:38:11 -080090 int type;
91 int tx_fifo;
92 int rx_fifo;
93 int xmit_fifo_size;
94 int rx_high_water;
95 int rx_trigger;
96 int rx_low_water;
97 long max_baud;
Jiri Slabycd7ed642006-12-08 02:38:29 -080098} Gpci_uart_info[] = {
Jiri Slaby037ad482006-12-08 02:38:11 -080099 {MOXA_OTHER_UART, 16, 16, 16, 14, 14, 1, 921600L},
100 {MOXA_MUST_MU150_HWID, 64, 64, 64, 48, 48, 16, 230400L},
101 {MOXA_MUST_MU860_HWID, 128, 128, 128, 96, 96, 32, 921600L}
102};
Jiri Slabycd7ed642006-12-08 02:38:29 -0800103#define UART_INFO_NUM ARRAY_SIZE(Gpci_uart_info)
Jiri Slaby037ad482006-12-08 02:38:11 -0800104
Jiri Slabycd7ed642006-12-08 02:38:29 -0800105struct mxser_cardinfo {
106 unsigned int nports;
107 char *name;
108 unsigned int flags;
109};
110
111static const struct mxser_cardinfo mxser_cards[] = {
112 { 8, "C168 series", }, /* C168-ISA */
113 { 4, "C104 series", }, /* C104-ISA */
114 { 4, "CI-104J series", }, /* CI104J */
115 { 8, "C168H/PCI series", }, /* C168-PCI */
116 { 4, "C104H/PCI series", }, /* C104-PCI */
117 { 4, "C102 series", MXSER_HAS2 }, /* C102-ISA */
118 { 4, "CI-132 series", MXSER_HAS2 }, /* CI132 */
119 { 4, "CI-134 series", }, /* CI134 */
120 { 2, "CP-132 series", }, /* CP132 */
121 { 4, "CP-114 series", }, /* CP114 */
122 { 4, "CT-114 series", }, /* CT114 */
123 { 2, "CP-102 series", MXSER_HIGHBAUD }, /* CP102 */
124 { 4, "CP-104U series", }, /* CP104U */
125 { 8, "CP-168U series", }, /* CP168U */
126 { 2, "CP-132U series", }, /* CP132U */
127 { 4, "CP-134U series", }, /* CP134U */
128 { 4, "CP-104JU series", }, /* CP104JU */
129 { 8, "Moxa UC7000 Serial", }, /* RC7000 */
130 { 8, "CP-118U series", }, /* CP118U */
131 { 2, "CP-102UL series", }, /* CP102UL */
132 { 2, "CP-102U series", }, /* CP102U */
133 { 8, "CP-118EL series", }, /* CP118EL */
134 { 8, "CP-168EL series", }, /* CP168EL */
135 { 4, "CP-104EL series", } /* CP104EL */
136};
137
138/* driver_data correspond to the lines in the structure above
139 see also ISA probe function before you change something */
Jiri Slaby037ad482006-12-08 02:38:11 -0800140static struct pci_device_id mxser_pcibrds[] = {
Jiri Slaby3306ce32006-12-08 02:38:13 -0800141 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C168),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800142 .driver_data = 3 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800143 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C104),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800144 .driver_data = 4 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800145 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP132),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800146 .driver_data = 8 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800147 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP114),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800148 .driver_data = 9 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800149 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CT114),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800150 .driver_data = 10 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800151 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP102),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800152 .driver_data = 11 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800153 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP104U),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800154 .driver_data = 12 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800155 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP168U),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800156 .driver_data = 13 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800157 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP132U),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800158 .driver_data = 14 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800159 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP134U),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800160 .driver_data = 15 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800161 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP104JU),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800162 .driver_data = 16 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800163 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_RC7000),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800164 .driver_data = 17 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800165 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP118U),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800166 .driver_data = 18 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800167 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP102UL),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800168 .driver_data = 19 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800169 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP102U),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800170 .driver_data = 20 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800171 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP118EL),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800172 .driver_data = 21 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800173 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP168EL),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800174 .driver_data = 22 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800175 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP104EL),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800176 .driver_data = 23 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800177 { }
Jiri Slaby037ad482006-12-08 02:38:11 -0800178};
Jiri Slaby037ad482006-12-08 02:38:11 -0800179MODULE_DEVICE_TABLE(pci, mxser_pcibrds);
180
Jiri Slabyf64c84a2007-02-10 01:45:21 -0800181static int mxvar_baud_table[] = {
182 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400,
183 4800, 9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600
184};
185static unsigned int mxvar_baud_table1[] = {
186 0, B50, B75, B110, B134, B150, B200, B300, B600, B1200, B1800, B2400,
187 B4800, B9600, B19200, B38400, B57600, B115200, B230400, B460800, B921600
188};
189#define BAUD_TABLE_NO ARRAY_SIZE(mxvar_baud_table)
190
191#define B_SPEC B2000000
192
Jiri Slaby037ad482006-12-08 02:38:11 -0800193static int ioaddr[MXSER_BOARDS] = { 0, 0, 0, 0 };
194static int ttymajor = MXSERMAJOR;
195static int calloutmajor = MXSERCUMAJOR;
Jiri Slaby037ad482006-12-08 02:38:11 -0800196
197/* Variables for insmod */
198
199MODULE_AUTHOR("Casper Yang");
200MODULE_DESCRIPTION("MOXA Smartio/Industio Family Multiport Board Device Driver");
201module_param_array(ioaddr, int, NULL, 0);
202module_param(ttymajor, int, 0);
Jiri Slaby037ad482006-12-08 02:38:11 -0800203MODULE_LICENSE("GPL");
204
205struct mxser_log {
206 int tick;
207 unsigned long rxcnt[MXSER_PORTS];
208 unsigned long txcnt[MXSER_PORTS];
209};
210
211
212struct mxser_mon {
213 unsigned long rxcnt;
214 unsigned long txcnt;
215 unsigned long up_rxcnt;
216 unsigned long up_txcnt;
217 int modem_status;
218 unsigned char hold_reason;
219};
220
221struct mxser_mon_ext {
222 unsigned long rx_cnt[32];
223 unsigned long tx_cnt[32];
224 unsigned long up_rxcnt[32];
225 unsigned long up_txcnt[32];
226 int modem_status[32];
227
228 long baudrate[32];
229 int databits[32];
230 int stopbits[32];
231 int parity[32];
232 int flowctrl[32];
233 int fifo[32];
234 int iftype[32];
235};
236
Jiri Slaby55b307d2006-12-08 02:38:14 -0800237struct mxser_board;
Jiri Slaby037ad482006-12-08 02:38:11 -0800238
Jiri Slaby55b307d2006-12-08 02:38:14 -0800239struct mxser_port {
240 struct mxser_board *board;
241 struct tty_struct *tty;
242
243 unsigned long ioaddr;
244 unsigned long opmode_ioaddr;
245 int max_baud;
246
Jiri Slaby037ad482006-12-08 02:38:11 -0800247 int rx_high_water;
248 int rx_trigger; /* Rx fifo trigger level */
249 int rx_low_water;
250 int baud_base; /* max. speed */
Jiri Slaby55b307d2006-12-08 02:38:14 -0800251 long realbaud;
Jiri Slaby037ad482006-12-08 02:38:11 -0800252 int type; /* UART type */
Jiri Slaby55b307d2006-12-08 02:38:14 -0800253 int flags; /* defined in tty.h */
Jiri Slabyf64c84a2007-02-10 01:45:21 -0800254 int speed;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800255
256 int x_char; /* xon/xoff character */
257 int IER; /* Interrupt Enable Register */
258 int MCR; /* Modem control register */
259
260 unsigned char stop_rx;
261 unsigned char ldisc_stop_rx;
262
263 int custom_divisor;
264 int close_delay;
265 unsigned short closing_wait;
266 unsigned char err_shadow;
267 unsigned long event;
268
269 int count; /* # of fd on device */
270 int blocked_open; /* # of blocked opens */
271 struct async_icount icount; /* kernel counters for 4 input interrupts */
272 int timeout;
273
Jiri Slaby037ad482006-12-08 02:38:11 -0800274 int read_status_mask;
275 int ignore_status_mask;
276 int xmit_fifo_size;
Jiri Slaby037ad482006-12-08 02:38:11 -0800277 unsigned char *xmit_buf;
278 int xmit_head;
279 int xmit_tail;
280 int xmit_cnt;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800281
Alan Cox606d0992006-12-08 02:38:45 -0800282 struct ktermios normal_termios;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800283
284 struct mxser_mon mon_data;
285
286 spinlock_t slock;
Jiri Slaby037ad482006-12-08 02:38:11 -0800287 wait_queue_head_t open_wait;
Jiri Slaby037ad482006-12-08 02:38:11 -0800288 wait_queue_head_t delta_msr_wait;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800289};
290
291struct mxser_board {
Jiri Slaby2094e752006-12-08 02:38:33 -0800292 unsigned int idx;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800293 int irq;
Jiri Slabycd7ed642006-12-08 02:38:29 -0800294 const struct mxser_cardinfo *info;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800295 unsigned long vector;
296 unsigned long vector_mask;
297
298 int chip_flag;
299 int uart_type;
300
301 struct mxser_port ports[MXSER_PORTS_PER_BOARD];
Jiri Slaby037ad482006-12-08 02:38:11 -0800302};
303
304struct mxser_mstatus {
305 tcflag_t cflag;
306 int cts;
307 int dsr;
308 int ri;
309 int dcd;
310};
311
312static struct mxser_mstatus GMStatus[MXSER_PORTS];
313
314static int mxserBoardCAP[MXSER_BOARDS] = {
315 0, 0, 0, 0
316 /* 0x180, 0x280, 0x200, 0x320 */
317};
318
Jiri Slaby55b307d2006-12-08 02:38:14 -0800319static struct mxser_board mxser_boards[MXSER_BOARDS];
Jiri Slaby037ad482006-12-08 02:38:11 -0800320static struct tty_driver *mxvar_sdriver;
Jiri Slaby037ad482006-12-08 02:38:11 -0800321static struct mxser_log mxvar_log;
322static int mxvar_diagflag;
323static unsigned char mxser_msr[MXSER_PORTS + 1];
324static struct mxser_mon_ext mon_data_ext;
325static int mxser_set_baud_method[MXSER_PORTS + 1];
Jiri Slaby037ad482006-12-08 02:38:11 -0800326
Jiri Slabyfb0c9292006-12-13 00:34:19 -0800327#ifdef CONFIG_PCI
Jiri Slabyc76b0932007-02-10 01:45:07 -0800328static int __devinit CheckIsMoxaMust(int io)
Jiri Slaby037ad482006-12-08 02:38:11 -0800329{
330 u8 oldmcr, hwid;
331 int i;
332
333 outb(0, io + UART_LCR);
334 DISABLE_MOXA_MUST_ENCHANCE_MODE(io);
335 oldmcr = inb(io + UART_MCR);
336 outb(0, io + UART_MCR);
337 SET_MOXA_MUST_XON1_VALUE(io, 0x11);
338 if ((hwid = inb(io + UART_MCR)) != 0) {
339 outb(oldmcr, io + UART_MCR);
340 return MOXA_OTHER_UART;
341 }
342
343 GET_MOXA_MUST_HARDWARE_ID(io, &hwid);
Jiri Slabycd7ed642006-12-08 02:38:29 -0800344 for (i = 1; i < UART_INFO_NUM; i++) { /* 0 = OTHER_UART */
345 if (hwid == Gpci_uart_info[i].type)
Jiri Slaby037ad482006-12-08 02:38:11 -0800346 return (int)hwid;
347 }
348 return MOXA_OTHER_UART;
349}
Jiri Slabyfb0c9292006-12-13 00:34:19 -0800350#endif
Jiri Slaby037ad482006-12-08 02:38:11 -0800351
Jiri Slaby55b307d2006-12-08 02:38:14 -0800352static void process_txrx_fifo(struct mxser_port *info)
Jiri Slaby037ad482006-12-08 02:38:11 -0800353{
354 int i;
355
356 if ((info->type == PORT_16450) || (info->type == PORT_8250)) {
357 info->rx_trigger = 1;
358 info->rx_high_water = 1;
359 info->rx_low_water = 1;
360 info->xmit_fifo_size = 1;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800361 } else
362 for (i = 0; i < UART_INFO_NUM; i++)
363 if (info->board->chip_flag == Gpci_uart_info[i].type) {
Jiri Slaby037ad482006-12-08 02:38:11 -0800364 info->rx_trigger = Gpci_uart_info[i].rx_trigger;
365 info->rx_low_water = Gpci_uart_info[i].rx_low_water;
366 info->rx_high_water = Gpci_uart_info[i].rx_high_water;
367 info->xmit_fifo_size = Gpci_uart_info[i].xmit_fifo_size;
368 break;
369 }
Jiri Slaby037ad482006-12-08 02:38:11 -0800370}
371
Jiri Slaby55b307d2006-12-08 02:38:14 -0800372static unsigned char mxser_get_msr(int baseaddr, int mode, int port)
Jiri Slaby037ad482006-12-08 02:38:11 -0800373{
374 unsigned char status = 0;
375
376 status = inb(baseaddr + UART_MSR);
377
378 mxser_msr[port] &= 0x0F;
379 mxser_msr[port] |= status;
380 status = mxser_msr[port];
381 if (mode)
382 mxser_msr[port] = 0;
383
384 return status;
385}
386
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800387static int mxser_block_til_ready(struct tty_struct *tty, struct file *filp,
388 struct mxser_port *port)
389{
390 DECLARE_WAITQUEUE(wait, current);
391 int retval;
392 int do_clocal = 0;
393 unsigned long flags;
394
395 /*
396 * If non-blocking mode is set, or the port is not enabled,
397 * then make the check up front and then exit.
398 */
Jiri Slaby214efeb2006-12-08 02:38:25 -0800399 if ((filp->f_flags & O_NONBLOCK) ||
400 test_bit(TTY_IO_ERROR, &tty->flags)) {
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800401 port->flags |= ASYNC_NORMAL_ACTIVE;
402 return 0;
403 }
404
405 if (tty->termios->c_cflag & CLOCAL)
406 do_clocal = 1;
407
408 /*
409 * Block waiting for the carrier detect and the line to become
410 * free (i.e., not in use by the callout). While we are in
411 * this loop, port->count is dropped by one, so that
412 * mxser_close() knows when to free things. We restore it upon
413 * exit, either normal or abnormal.
414 */
415 retval = 0;
416 add_wait_queue(&port->open_wait, &wait);
417
418 spin_lock_irqsave(&port->slock, flags);
419 if (!tty_hung_up_p(filp))
420 port->count--;
421 spin_unlock_irqrestore(&port->slock, flags);
422 port->blocked_open++;
423 while (1) {
424 spin_lock_irqsave(&port->slock, flags);
425 outb(inb(port->ioaddr + UART_MCR) |
426 UART_MCR_DTR | UART_MCR_RTS, port->ioaddr + UART_MCR);
427 spin_unlock_irqrestore(&port->slock, flags);
428 set_current_state(TASK_INTERRUPTIBLE);
429 if (tty_hung_up_p(filp) || !(port->flags & ASYNC_INITIALIZED)) {
430 if (port->flags & ASYNC_HUP_NOTIFY)
431 retval = -EAGAIN;
432 else
433 retval = -ERESTARTSYS;
434 break;
435 }
436 if (!(port->flags & ASYNC_CLOSING) &&
437 (do_clocal ||
438 (inb(port->ioaddr + UART_MSR) & UART_MSR_DCD)))
439 break;
440 if (signal_pending(current)) {
441 retval = -ERESTARTSYS;
442 break;
443 }
444 schedule();
445 }
446 set_current_state(TASK_RUNNING);
447 remove_wait_queue(&port->open_wait, &wait);
448 if (!tty_hung_up_p(filp))
449 port->count++;
450 port->blocked_open--;
451 if (retval)
452 return retval;
453 port->flags |= ASYNC_NORMAL_ACTIVE;
454 return 0;
455}
456
457static int mxser_set_baud(struct mxser_port *info, long newspd)
458{
Jiri Slabyf64c84a2007-02-10 01:45:21 -0800459 unsigned int i;
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800460 int quot = 0;
461 unsigned char cval;
462 int ret = 0;
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800463
464 if (!info->tty || !info->tty->termios)
465 return ret;
466
467 if (!(info->ioaddr))
468 return ret;
469
470 if (newspd > info->max_baud)
471 return 0;
472
473 info->realbaud = newspd;
Jiri Slabyf64c84a2007-02-10 01:45:21 -0800474 for (i = 0; i < BAUD_TABLE_NO; i++)
475 if (newspd == mxvar_baud_table[i])
476 break;
477 if (i == BAUD_TABLE_NO) {
478 quot = info->baud_base / info->speed;
479 if (info->speed <= 0 || info->speed > info->max_baud)
480 quot = 0;
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800481 } else {
Jiri Slabyf64c84a2007-02-10 01:45:21 -0800482 if (newspd == 134) {
483 quot = (2 * info->baud_base / 269);
484 } else if (newspd) {
485 quot = info->baud_base / newspd;
486 if (quot == 0)
487 quot = 1;
488 } else {
489 quot = 0;
490 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800491 }
492
493 info->timeout = ((info->xmit_fifo_size * HZ * 10 * quot) / info->baud_base);
494 info->timeout += HZ / 50; /* Add .02 seconds of slop */
495
496 if (quot) {
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800497 info->MCR |= UART_MCR_DTR;
498 outb(info->MCR, info->ioaddr + UART_MCR);
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800499 } else {
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800500 info->MCR &= ~UART_MCR_DTR;
501 outb(info->MCR, info->ioaddr + UART_MCR);
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800502 return ret;
503 }
504
505 cval = inb(info->ioaddr + UART_LCR);
506
507 outb(cval | UART_LCR_DLAB, info->ioaddr + UART_LCR); /* set DLAB */
508
509 outb(quot & 0xff, info->ioaddr + UART_DLL); /* LS of divisor */
510 outb(quot >> 8, info->ioaddr + UART_DLM); /* MS of divisor */
511 outb(cval, info->ioaddr + UART_LCR); /* reset DLAB */
512
Jiri Slabyf64c84a2007-02-10 01:45:21 -0800513 if (i == BAUD_TABLE_NO) {
514 quot = info->baud_base % info->speed;
515 quot *= 8;
516 if ((quot % info->speed) > (info->speed / 2)) {
517 quot /= info->speed;
518 quot++;
519 } else {
520 quot /= info->speed;
521 }
522 SET_MOXA_MUST_ENUM_VALUE(info->ioaddr, quot);
523 } else
524 SET_MOXA_MUST_ENUM_VALUE(info->ioaddr, 0);
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800525
526 return ret;
527}
528
529/*
530 * This routine is called to set the UART divisor registers to match
531 * the specified baud rate for a serial port.
532 */
533static int mxser_change_speed(struct mxser_port *info,
Alan Cox606d0992006-12-08 02:38:45 -0800534 struct ktermios *old_termios)
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800535{
536 unsigned cflag, cval, fcr;
537 int ret = 0;
538 unsigned char status;
539 long baud;
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800540
541 if (!info->tty || !info->tty->termios)
542 return ret;
543 cflag = info->tty->termios->c_cflag;
544 if (!(info->ioaddr))
545 return ret;
546
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800547 if (mxser_set_baud_method[info->tty->index] == 0) {
Jiri Slabyf64c84a2007-02-10 01:45:21 -0800548 if ((cflag & CBAUD) == B_SPEC)
549 baud = info->speed;
550 else
551 baud = tty_get_baud_rate(info->tty);
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800552 mxser_set_baud(info, baud);
553 }
554
555 /* byte size and parity */
556 switch (cflag & CSIZE) {
557 case CS5:
558 cval = 0x00;
559 break;
560 case CS6:
561 cval = 0x01;
562 break;
563 case CS7:
564 cval = 0x02;
565 break;
566 case CS8:
567 cval = 0x03;
568 break;
569 default:
570 cval = 0x00;
571 break; /* too keep GCC shut... */
572 }
573 if (cflag & CSTOPB)
574 cval |= 0x04;
575 if (cflag & PARENB)
576 cval |= UART_LCR_PARITY;
577 if (!(cflag & PARODD))
578 cval |= UART_LCR_EPAR;
579 if (cflag & CMSPAR)
580 cval |= UART_LCR_SPAR;
581
582 if ((info->type == PORT_8250) || (info->type == PORT_16450)) {
583 if (info->board->chip_flag) {
584 fcr = UART_FCR_ENABLE_FIFO;
585 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
586 SET_MOXA_MUST_FIFO_VALUE(info);
587 } else
588 fcr = 0;
589 } else {
590 fcr = UART_FCR_ENABLE_FIFO;
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800591 if (info->board->chip_flag) {
592 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
593 SET_MOXA_MUST_FIFO_VALUE(info);
594 } else {
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800595 switch (info->rx_trigger) {
596 case 1:
597 fcr |= UART_FCR_TRIGGER_1;
598 break;
599 case 4:
600 fcr |= UART_FCR_TRIGGER_4;
601 break;
602 case 8:
603 fcr |= UART_FCR_TRIGGER_8;
604 break;
605 default:
606 fcr |= UART_FCR_TRIGGER_14;
607 break;
608 }
609 }
610 }
611
612 /* CTS flow control flag and modem status interrupts */
613 info->IER &= ~UART_IER_MSI;
614 info->MCR &= ~UART_MCR_AFE;
615 if (cflag & CRTSCTS) {
616 info->flags |= ASYNC_CTS_FLOW;
617 info->IER |= UART_IER_MSI;
618 if ((info->type == PORT_16550A) || (info->board->chip_flag)) {
619 info->MCR |= UART_MCR_AFE;
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800620 } else {
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800621 status = inb(info->ioaddr + UART_MSR);
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800622 if (info->tty->hw_stopped) {
623 if (status & UART_MSR_CTS) {
624 info->tty->hw_stopped = 0;
625 if (info->type != PORT_16550A &&
626 !info->board->chip_flag) {
627 outb(info->IER & ~UART_IER_THRI,
628 info->ioaddr +
629 UART_IER);
630 info->IER |= UART_IER_THRI;
631 outb(info->IER, info->ioaddr +
632 UART_IER);
633 }
Jiri Slaby47c85c02007-02-10 01:45:15 -0800634 tty_wakeup(info->tty);
635 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800636 } else {
637 if (!(status & UART_MSR_CTS)) {
638 info->tty->hw_stopped = 1;
639 if ((info->type != PORT_16550A) &&
640 (!info->board->chip_flag)) {
641 info->IER &= ~UART_IER_THRI;
642 outb(info->IER, info->ioaddr +
643 UART_IER);
644 }
645 }
646 }
647 }
648 } else {
649 info->flags &= ~ASYNC_CTS_FLOW;
650 }
651 outb(info->MCR, info->ioaddr + UART_MCR);
652 if (cflag & CLOCAL) {
653 info->flags &= ~ASYNC_CHECK_CD;
654 } else {
655 info->flags |= ASYNC_CHECK_CD;
656 info->IER |= UART_IER_MSI;
657 }
658 outb(info->IER, info->ioaddr + UART_IER);
659
660 /*
661 * Set up parity check flag
662 */
663 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
664 if (I_INPCK(info->tty))
665 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
666 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
667 info->read_status_mask |= UART_LSR_BI;
668
669 info->ignore_status_mask = 0;
670
671 if (I_IGNBRK(info->tty)) {
672 info->ignore_status_mask |= UART_LSR_BI;
673 info->read_status_mask |= UART_LSR_BI;
674 /*
675 * If we're ignore parity and break indicators, ignore
676 * overruns too. (For real raw support).
677 */
678 if (I_IGNPAR(info->tty)) {
679 info->ignore_status_mask |=
680 UART_LSR_OE |
681 UART_LSR_PE |
682 UART_LSR_FE;
683 info->read_status_mask |=
684 UART_LSR_OE |
685 UART_LSR_PE |
686 UART_LSR_FE;
687 }
688 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800689 if (info->board->chip_flag) {
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800690 SET_MOXA_MUST_XON1_VALUE(info->ioaddr, START_CHAR(info->tty));
691 SET_MOXA_MUST_XOFF1_VALUE(info->ioaddr, STOP_CHAR(info->tty));
692 if (I_IXON(info->tty)) {
693 ENABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
694 } else {
695 DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
696 }
697 if (I_IXOFF(info->tty)) {
698 ENABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
699 } else {
700 DISABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
701 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800702 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800703
704
705 outb(fcr, info->ioaddr + UART_FCR); /* set fcr */
706 outb(cval, info->ioaddr + UART_LCR);
707
708 return ret;
709}
710
711static void mxser_check_modem_status(struct mxser_port *port, int status)
712{
713 /* update input line counters */
714 if (status & UART_MSR_TERI)
715 port->icount.rng++;
716 if (status & UART_MSR_DDSR)
717 port->icount.dsr++;
718 if (status & UART_MSR_DDCD)
719 port->icount.dcd++;
720 if (status & UART_MSR_DCTS)
721 port->icount.cts++;
722 port->mon_data.modem_status = status;
723 wake_up_interruptible(&port->delta_msr_wait);
724
725 if ((port->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
726 if (status & UART_MSR_DCD)
727 wake_up_interruptible(&port->open_wait);
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800728 }
729
730 if (port->flags & ASYNC_CTS_FLOW) {
731 if (port->tty->hw_stopped) {
732 if (status & UART_MSR_CTS) {
733 port->tty->hw_stopped = 0;
734
735 if ((port->type != PORT_16550A) &&
736 (!port->board->chip_flag)) {
737 outb(port->IER & ~UART_IER_THRI,
738 port->ioaddr + UART_IER);
739 port->IER |= UART_IER_THRI;
740 outb(port->IER, port->ioaddr +
741 UART_IER);
742 }
Jiri Slaby47c85c02007-02-10 01:45:15 -0800743 tty_wakeup(port->tty);
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800744 }
745 } else {
746 if (!(status & UART_MSR_CTS)) {
747 port->tty->hw_stopped = 1;
748 if (port->type != PORT_16550A &&
749 !port->board->chip_flag) {
750 port->IER &= ~UART_IER_THRI;
751 outb(port->IER, port->ioaddr +
752 UART_IER);
753 }
754 }
755 }
756 }
757}
758
759static int mxser_startup(struct mxser_port *info)
760{
761 unsigned long page;
762 unsigned long flags;
763
764 page = __get_free_page(GFP_KERNEL);
765 if (!page)
766 return -ENOMEM;
767
768 spin_lock_irqsave(&info->slock, flags);
769
770 if (info->flags & ASYNC_INITIALIZED) {
771 free_page(page);
772 spin_unlock_irqrestore(&info->slock, flags);
773 return 0;
774 }
775
776 if (!info->ioaddr || !info->type) {
777 if (info->tty)
778 set_bit(TTY_IO_ERROR, &info->tty->flags);
779 free_page(page);
780 spin_unlock_irqrestore(&info->slock, flags);
781 return 0;
782 }
783 if (info->xmit_buf)
784 free_page(page);
785 else
786 info->xmit_buf = (unsigned char *) page;
787
788 /*
789 * Clear the FIFO buffers and disable them
790 * (they will be reenabled in mxser_change_speed())
791 */
792 if (info->board->chip_flag)
793 outb((UART_FCR_CLEAR_RCVR |
794 UART_FCR_CLEAR_XMIT |
795 MOXA_MUST_FCR_GDA_MODE_ENABLE), info->ioaddr + UART_FCR);
796 else
797 outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
798 info->ioaddr + UART_FCR);
799
800 /*
801 * At this point there's no way the LSR could still be 0xFF;
802 * if it is, then bail out, because there's likely no UART
803 * here.
804 */
805 if (inb(info->ioaddr + UART_LSR) == 0xff) {
806 spin_unlock_irqrestore(&info->slock, flags);
807 if (capable(CAP_SYS_ADMIN)) {
808 if (info->tty)
809 set_bit(TTY_IO_ERROR, &info->tty->flags);
810 return 0;
811 } else
812 return -ENODEV;
813 }
814
815 /*
816 * Clear the interrupt registers.
817 */
818 (void) inb(info->ioaddr + UART_LSR);
819 (void) inb(info->ioaddr + UART_RX);
820 (void) inb(info->ioaddr + UART_IIR);
821 (void) inb(info->ioaddr + UART_MSR);
822
823 /*
824 * Now, initialize the UART
825 */
826 outb(UART_LCR_WLEN8, info->ioaddr + UART_LCR); /* reset DLAB */
827 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
828 outb(info->MCR, info->ioaddr + UART_MCR);
829
830 /*
831 * Finally, enable interrupts
832 */
833 info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800834
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800835 if (info->board->chip_flag)
836 info->IER |= MOXA_MUST_IER_EGDAI;
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800837 outb(info->IER, info->ioaddr + UART_IER); /* enable interrupts */
838
839 /*
840 * And clear the interrupt registers again for luck.
841 */
842 (void) inb(info->ioaddr + UART_LSR);
843 (void) inb(info->ioaddr + UART_RX);
844 (void) inb(info->ioaddr + UART_IIR);
845 (void) inb(info->ioaddr + UART_MSR);
846
847 if (info->tty)
848 clear_bit(TTY_IO_ERROR, &info->tty->flags);
849 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
850
851 /*
852 * and set the speed of the serial port
853 */
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800854 mxser_change_speed(info, NULL);
Jiri Slaby5ff72052007-02-10 01:45:23 -0800855 info->flags |= ASYNC_INITIALIZED;
Jiri Slabyf64c84a2007-02-10 01:45:21 -0800856 spin_unlock_irqrestore(&info->slock, flags);
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800857
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800858 return 0;
859}
860
861/*
862 * This routine will shutdown a serial port; interrupts maybe disabled, and
863 * DTR is dropped if the hangup on close termio flag is on.
864 */
865static void mxser_shutdown(struct mxser_port *info)
866{
867 unsigned long flags;
868
869 if (!(info->flags & ASYNC_INITIALIZED))
870 return;
871
872 spin_lock_irqsave(&info->slock, flags);
873
874 /*
875 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
876 * here so the queue might never be waken up
877 */
878 wake_up_interruptible(&info->delta_msr_wait);
879
880 /*
881 * Free the IRQ, if necessary
882 */
883 if (info->xmit_buf) {
884 free_page((unsigned long) info->xmit_buf);
885 info->xmit_buf = NULL;
886 }
887
888 info->IER = 0;
889 outb(0x00, info->ioaddr + UART_IER);
890
891 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
892 info->MCR &= ~(UART_MCR_DTR | UART_MCR_RTS);
893 outb(info->MCR, info->ioaddr + UART_MCR);
894
895 /* clear Rx/Tx FIFO's */
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800896 if (info->board->chip_flag)
897 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT |
898 MOXA_MUST_FCR_GDA_MODE_ENABLE,
899 info->ioaddr + UART_FCR);
900 else
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800901 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
902 info->ioaddr + UART_FCR);
903
904 /* read data port to reset things */
905 (void) inb(info->ioaddr + UART_RX);
906
907 if (info->tty)
908 set_bit(TTY_IO_ERROR, &info->tty->flags);
909
910 info->flags &= ~ASYNC_INITIALIZED;
911
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800912 if (info->board->chip_flag)
913 SET_MOXA_MUST_NO_SOFTWARE_FLOW_CONTROL(info->ioaddr);
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800914
915 spin_unlock_irqrestore(&info->slock, flags);
916}
917
Jiri Slaby037ad482006-12-08 02:38:11 -0800918/*
919 * This routine is called whenever a serial port is opened. It
920 * enables interrupts for a serial port, linking in its async structure into
921 * the IRQ chain. It also performs the serial-specific
922 * initialization for the tty structure.
923 */
924static int mxser_open(struct tty_struct *tty, struct file *filp)
925{
Jiri Slaby55b307d2006-12-08 02:38:14 -0800926 struct mxser_port *info;
Jiri Slaby5ff72052007-02-10 01:45:23 -0800927 unsigned long flags;
Jiri Slaby037ad482006-12-08 02:38:11 -0800928 int retval, line;
929
Jiri Slaby037ad482006-12-08 02:38:11 -0800930 line = tty->index;
931 if (line == MXSER_PORTS)
932 return 0;
933 if (line < 0 || line > MXSER_PORTS)
934 return -ENODEV;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800935 info = &mxser_boards[line / MXSER_PORTS_PER_BOARD].ports[line % MXSER_PORTS_PER_BOARD];
936 if (!info->ioaddr)
Jiri Slaby037ad482006-12-08 02:38:11 -0800937 return -ENODEV;
938
939 tty->driver_data = info;
940 info->tty = tty;
941 /*
942 * Start up serial port
943 */
Jiri Slaby5ff72052007-02-10 01:45:23 -0800944 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby3306ce32006-12-08 02:38:13 -0800945 info->count++;
Jiri Slaby5ff72052007-02-10 01:45:23 -0800946 spin_unlock_irqrestore(&info->slock, flags);
Jiri Slaby037ad482006-12-08 02:38:11 -0800947 retval = mxser_startup(info);
948 if (retval)
949 return retval;
950
951 retval = mxser_block_til_ready(tty, filp, info);
952 if (retval)
953 return retval;
954
Jiri Slabye079f492006-12-08 02:38:31 -0800955 /* unmark here for very high baud rate (ex. 921600 bps) used */
Jiri Slaby037ad482006-12-08 02:38:11 -0800956 tty->low_latency = 1;
957 return 0;
958}
959
960/*
961 * This routine is called when the serial port gets closed. First, we
962 * wait for the last remaining data to be sent. Then, we unlink its
963 * async structure from the interrupt chain if necessary, and we free
964 * that IRQ if nothing is left in the chain.
965 */
966static void mxser_close(struct tty_struct *tty, struct file *filp)
967{
Jiri Slaby55b307d2006-12-08 02:38:14 -0800968 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -0800969
970 unsigned long timeout;
971 unsigned long flags;
Jiri Slaby037ad482006-12-08 02:38:11 -0800972
973 if (tty->index == MXSER_PORTS)
974 return;
975 if (!info)
976 return;
977
978 spin_lock_irqsave(&info->slock, flags);
979
980 if (tty_hung_up_p(filp)) {
981 spin_unlock_irqrestore(&info->slock, flags);
982 return;
983 }
984 if ((tty->count == 1) && (info->count != 1)) {
985 /*
986 * Uh, oh. tty->count is 1, which means that the tty
987 * structure will be freed. Info->count should always
988 * be one in these conditions. If it's greater than
989 * one, we've got real problems, since it means the
990 * serial port won't be shutdown.
991 */
992 printk(KERN_ERR "mxser_close: bad serial port count; "
993 "tty->count is 1, info->count is %d\n", info->count);
994 info->count = 1;
995 }
996 if (--info->count < 0) {
997 printk(KERN_ERR "mxser_close: bad serial port count for "
Jiri Slaby55b307d2006-12-08 02:38:14 -0800998 "ttys%d: %d\n", tty->index, info->count);
Jiri Slaby037ad482006-12-08 02:38:11 -0800999 info->count = 0;
1000 }
1001 if (info->count) {
1002 spin_unlock_irqrestore(&info->slock, flags);
1003 return;
1004 }
1005 info->flags |= ASYNC_CLOSING;
1006 spin_unlock_irqrestore(&info->slock, flags);
1007 /*
1008 * Save the termios structure, since this port may have
1009 * separate termios for callout and dialin.
1010 */
1011 if (info->flags & ASYNC_NORMAL_ACTIVE)
1012 info->normal_termios = *tty->termios;
1013 /*
1014 * Now we wait for the transmit buffer to clear; and we notify
1015 * the line discipline to only process XON/XOFF characters.
1016 */
1017 tty->closing = 1;
1018 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1019 tty_wait_until_sent(tty, info->closing_wait);
1020 /*
1021 * At this point we stop accepting input. To do this, we
1022 * disable the receive line status interrupts, and tell the
1023 * interrupt driver to stop checking the data ready bit in the
1024 * line status register.
1025 */
1026 info->IER &= ~UART_IER_RLSI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001027 if (info->board->chip_flag)
Jiri Slaby037ad482006-12-08 02:38:11 -08001028 info->IER &= ~MOXA_MUST_RECV_ISR;
Jiri Slabye079f492006-12-08 02:38:31 -08001029
Jiri Slaby037ad482006-12-08 02:38:11 -08001030 if (info->flags & ASYNC_INITIALIZED) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08001031 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001032 /*
1033 * Before we drop DTR, make sure the UART transmitter
1034 * has completely drained; this is especially
1035 * important if there is a transmit FIFO!
1036 */
1037 timeout = jiffies + HZ;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001038 while (!(inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001039 schedule_timeout_interruptible(5);
1040 if (time_after(jiffies, timeout))
1041 break;
1042 }
1043 }
1044 mxser_shutdown(info);
1045
1046 if (tty->driver->flush_buffer)
1047 tty->driver->flush_buffer(tty);
1048
Jiri Slaby7e8bcf92006-12-08 02:38:24 -08001049 tty_ldisc_flush(tty);
Jiri Slaby037ad482006-12-08 02:38:11 -08001050
1051 tty->closing = 0;
1052 info->event = 0;
1053 info->tty = NULL;
1054 if (info->blocked_open) {
1055 if (info->close_delay)
1056 schedule_timeout_interruptible(info->close_delay);
1057 wake_up_interruptible(&info->open_wait);
1058 }
1059
1060 info->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
Jiri Slaby037ad482006-12-08 02:38:11 -08001061}
1062
1063static int mxser_write(struct tty_struct *tty, const unsigned char *buf, int count)
1064{
1065 int c, total = 0;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001066 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001067 unsigned long flags;
1068
1069 if (!info->xmit_buf)
1070 return 0;
1071
1072 while (1) {
1073 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1074 SERIAL_XMIT_SIZE - info->xmit_head));
1075 if (c <= 0)
1076 break;
1077
1078 memcpy(info->xmit_buf + info->xmit_head, buf, c);
1079 spin_lock_irqsave(&info->slock, flags);
1080 info->xmit_head = (info->xmit_head + c) &
1081 (SERIAL_XMIT_SIZE - 1);
1082 info->xmit_cnt += c;
1083 spin_unlock_irqrestore(&info->slock, flags);
1084
1085 buf += c;
1086 count -= c;
1087 total += c;
1088 }
1089
Jiri Slabye079f492006-12-08 02:38:31 -08001090 if (info->xmit_cnt && !tty->stopped) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001091 if (!tty->hw_stopped ||
1092 (info->type == PORT_16550A) ||
Jiri Slaby55b307d2006-12-08 02:38:14 -08001093 (info->board->chip_flag)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001094 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby55b307d2006-12-08 02:38:14 -08001095 outb(info->IER & ~UART_IER_THRI, info->ioaddr +
1096 UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001097 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001098 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001099 spin_unlock_irqrestore(&info->slock, flags);
1100 }
1101 }
1102 return total;
1103}
1104
1105static void mxser_put_char(struct tty_struct *tty, unsigned char ch)
1106{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001107 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001108 unsigned long flags;
1109
1110 if (!info->xmit_buf)
1111 return;
1112
1113 if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1)
1114 return;
1115
1116 spin_lock_irqsave(&info->slock, flags);
1117 info->xmit_buf[info->xmit_head++] = ch;
1118 info->xmit_head &= SERIAL_XMIT_SIZE - 1;
1119 info->xmit_cnt++;
1120 spin_unlock_irqrestore(&info->slock, flags);
Jiri Slabye079f492006-12-08 02:38:31 -08001121 if (!tty->stopped) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001122 if (!tty->hw_stopped ||
1123 (info->type == PORT_16550A) ||
Jiri Slaby55b307d2006-12-08 02:38:14 -08001124 info->board->chip_flag) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001125 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby55b307d2006-12-08 02:38:14 -08001126 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001127 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001128 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001129 spin_unlock_irqrestore(&info->slock, flags);
1130 }
1131 }
1132}
1133
1134
1135static void mxser_flush_chars(struct tty_struct *tty)
1136{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001137 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001138 unsigned long flags;
1139
1140 if (info->xmit_cnt <= 0 ||
1141 tty->stopped ||
1142 !info->xmit_buf ||
1143 (tty->hw_stopped &&
1144 (info->type != PORT_16550A) &&
Jiri Slaby55b307d2006-12-08 02:38:14 -08001145 (!info->board->chip_flag)
Jiri Slaby037ad482006-12-08 02:38:11 -08001146 ))
1147 return;
1148
1149 spin_lock_irqsave(&info->slock, flags);
1150
Jiri Slaby55b307d2006-12-08 02:38:14 -08001151 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001152 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001153 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001154
1155 spin_unlock_irqrestore(&info->slock, flags);
1156}
1157
1158static int mxser_write_room(struct tty_struct *tty)
1159{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001160 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001161 int ret;
1162
1163 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1164 if (ret < 0)
1165 ret = 0;
1166 return ret;
1167}
1168
1169static int mxser_chars_in_buffer(struct tty_struct *tty)
1170{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001171 struct mxser_port *info = tty->driver_data;
Jiri Slaby925e9c12006-12-08 02:38:30 -08001172 return info->xmit_cnt;
Jiri Slaby037ad482006-12-08 02:38:11 -08001173}
1174
1175static void mxser_flush_buffer(struct tty_struct *tty)
1176{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001177 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001178 char fcr;
1179 unsigned long flags;
1180
1181
1182 spin_lock_irqsave(&info->slock, flags);
1183 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1184
Jiri Slaby55b307d2006-12-08 02:38:14 -08001185 fcr = inb(info->ioaddr + UART_FCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08001186 outb((fcr | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
Jiri Slaby55b307d2006-12-08 02:38:14 -08001187 info->ioaddr + UART_FCR);
1188 outb(fcr, info->ioaddr + UART_FCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08001189
1190 spin_unlock_irqrestore(&info->slock, flags);
Jiri Slaby037ad482006-12-08 02:38:11 -08001191
Jiri Slaby7e8bcf92006-12-08 02:38:24 -08001192 tty_wakeup(tty);
Jiri Slaby037ad482006-12-08 02:38:11 -08001193}
1194
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001195/*
1196 * ------------------------------------------------------------
1197 * friends of mxser_ioctl()
1198 * ------------------------------------------------------------
1199 */
1200static int mxser_get_serial_info(struct mxser_port *info,
1201 struct serial_struct __user *retinfo)
1202{
1203 struct serial_struct tmp;
1204
1205 if (!retinfo)
1206 return -EFAULT;
1207 memset(&tmp, 0, sizeof(tmp));
1208 tmp.type = info->type;
1209 tmp.line = info->tty->index;
1210 tmp.port = info->ioaddr;
1211 tmp.irq = info->board->irq;
1212 tmp.flags = info->flags;
1213 tmp.baud_base = info->baud_base;
1214 tmp.close_delay = info->close_delay;
1215 tmp.closing_wait = info->closing_wait;
1216 tmp.custom_divisor = info->custom_divisor;
1217 tmp.hub6 = 0;
1218 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1219 return -EFAULT;
1220 return 0;
1221}
1222
1223static int mxser_set_serial_info(struct mxser_port *info,
1224 struct serial_struct __user *new_info)
1225{
1226 struct serial_struct new_serial;
Jiri Slabyf64c84a2007-02-10 01:45:21 -08001227 unsigned long sl_flags;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001228 unsigned int flags;
1229 int retval = 0;
1230
1231 if (!new_info || !info->ioaddr)
1232 return -EFAULT;
1233 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
1234 return -EFAULT;
1235
1236 if ((new_serial.irq != info->board->irq) ||
1237 (new_serial.port != info->ioaddr) ||
1238 (new_serial.custom_divisor != info->custom_divisor) ||
1239 (new_serial.baud_base != info->baud_base))
1240 return -EPERM;
1241
1242 flags = info->flags & ASYNC_SPD_MASK;
1243
1244 if (!capable(CAP_SYS_ADMIN)) {
1245 if ((new_serial.baud_base != info->baud_base) ||
1246 (new_serial.close_delay != info->close_delay) ||
1247 ((new_serial.flags & ~ASYNC_USR_MASK) != (info->flags & ~ASYNC_USR_MASK)))
1248 return -EPERM;
1249 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
1250 (new_serial.flags & ASYNC_USR_MASK));
1251 } else {
1252 /*
1253 * OK, past this point, all the error checking has been done.
1254 * At this point, we start making changes.....
1255 */
1256 info->flags = ((info->flags & ~ASYNC_FLAGS) |
1257 (new_serial.flags & ASYNC_FLAGS));
1258 info->close_delay = new_serial.close_delay * HZ / 100;
1259 info->closing_wait = new_serial.closing_wait * HZ / 100;
1260 info->tty->low_latency =
1261 (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
Jiri Slabye079f492006-12-08 02:38:31 -08001262 info->tty->low_latency = 0;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001263 }
1264
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001265 info->type = new_serial.type;
1266
1267 process_txrx_fifo(info);
1268
1269 if (info->flags & ASYNC_INITIALIZED) {
Jiri Slabyf64c84a2007-02-10 01:45:21 -08001270 if (flags != (info->flags & ASYNC_SPD_MASK)) {
1271 spin_lock_irqsave(&info->slock, sl_flags);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001272 mxser_change_speed(info, NULL);
Jiri Slabyf64c84a2007-02-10 01:45:21 -08001273 spin_unlock_irqrestore(&info->slock, sl_flags);
1274 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001275 } else
1276 retval = mxser_startup(info);
1277
1278 return retval;
1279}
1280
1281/*
1282 * mxser_get_lsr_info - get line status register info
1283 *
1284 * Purpose: Let user call ioctl() to get info when the UART physically
1285 * is emptied. On bus types like RS485, the transmitter must
1286 * release the bus after transmitting. This must be done when
1287 * the transmit shift register is empty, not be done when the
1288 * transmit holding register is empty. This functionality
1289 * allows an RS485 driver to be written in user space.
1290 */
1291static int mxser_get_lsr_info(struct mxser_port *info,
1292 unsigned int __user *value)
1293{
1294 unsigned char status;
1295 unsigned int result;
1296 unsigned long flags;
1297
1298 spin_lock_irqsave(&info->slock, flags);
1299 status = inb(info->ioaddr + UART_LSR);
1300 spin_unlock_irqrestore(&info->slock, flags);
1301 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1302 return put_user(result, value);
1303}
1304
1305/*
1306 * This routine sends a break character out the serial port.
1307 */
1308static void mxser_send_break(struct mxser_port *info, int duration)
1309{
1310 unsigned long flags;
1311
1312 if (!info->ioaddr)
1313 return;
1314 set_current_state(TASK_INTERRUPTIBLE);
1315 spin_lock_irqsave(&info->slock, flags);
1316 outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
1317 info->ioaddr + UART_LCR);
1318 spin_unlock_irqrestore(&info->slock, flags);
1319 schedule_timeout(duration);
1320 spin_lock_irqsave(&info->slock, flags);
1321 outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
1322 info->ioaddr + UART_LCR);
1323 spin_unlock_irqrestore(&info->slock, flags);
1324}
1325
1326static int mxser_tiocmget(struct tty_struct *tty, struct file *file)
Jiri Slaby037ad482006-12-08 02:38:11 -08001327{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001328 struct mxser_port *info = tty->driver_data;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001329 unsigned char control, status;
Jiri Slaby037ad482006-12-08 02:38:11 -08001330 unsigned long flags;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001331
Jiri Slaby037ad482006-12-08 02:38:11 -08001332
1333 if (tty->index == MXSER_PORTS)
Jiri Slaby037ad482006-12-08 02:38:11 -08001334 return -ENOIOCTLCMD;
Jiri Slaby214efeb2006-12-08 02:38:25 -08001335 if (test_bit(TTY_IO_ERROR, &tty->flags))
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001336 return -EIO;
1337
1338 control = info->MCR;
1339
1340 spin_lock_irqsave(&info->slock, flags);
1341 status = inb(info->ioaddr + UART_MSR);
1342 if (status & UART_MSR_ANY_DELTA)
1343 mxser_check_modem_status(info, status);
1344 spin_unlock_irqrestore(&info->slock, flags);
1345 return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0) |
1346 ((control & UART_MCR_DTR) ? TIOCM_DTR : 0) |
1347 ((status & UART_MSR_DCD) ? TIOCM_CAR : 0) |
1348 ((status & UART_MSR_RI) ? TIOCM_RNG : 0) |
1349 ((status & UART_MSR_DSR) ? TIOCM_DSR : 0) |
1350 ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
1351}
1352
1353static int mxser_tiocmset(struct tty_struct *tty, struct file *file,
1354 unsigned int set, unsigned int clear)
1355{
1356 struct mxser_port *info = tty->driver_data;
1357 unsigned long flags;
1358
1359
1360 if (tty->index == MXSER_PORTS)
1361 return -ENOIOCTLCMD;
Jiri Slaby214efeb2006-12-08 02:38:25 -08001362 if (test_bit(TTY_IO_ERROR, &tty->flags))
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001363 return -EIO;
1364
1365 spin_lock_irqsave(&info->slock, flags);
1366
1367 if (set & TIOCM_RTS)
1368 info->MCR |= UART_MCR_RTS;
1369 if (set & TIOCM_DTR)
1370 info->MCR |= UART_MCR_DTR;
1371
1372 if (clear & TIOCM_RTS)
1373 info->MCR &= ~UART_MCR_RTS;
1374 if (clear & TIOCM_DTR)
1375 info->MCR &= ~UART_MCR_DTR;
1376
1377 outb(info->MCR, info->ioaddr + UART_MCR);
1378 spin_unlock_irqrestore(&info->slock, flags);
Jiri Slaby037ad482006-12-08 02:38:11 -08001379 return 0;
1380}
1381
Jiri Slabyc76b0932007-02-10 01:45:07 -08001382static int __init mxser_program_mode(int port)
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001383{
1384 int id, i, j, n;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001385
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001386 outb(0, port);
1387 outb(0, port);
1388 outb(0, port);
1389 (void)inb(port);
1390 (void)inb(port);
1391 outb(0, port);
1392 (void)inb(port);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001393
1394 id = inb(port + 1) & 0x1F;
1395 if ((id != C168_ASIC_ID) &&
1396 (id != C104_ASIC_ID) &&
1397 (id != C102_ASIC_ID) &&
1398 (id != CI132_ASIC_ID) &&
1399 (id != CI134_ASIC_ID) &&
1400 (id != CI104J_ASIC_ID))
1401 return -1;
1402 for (i = 0, j = 0; i < 4; i++) {
1403 n = inb(port + 2);
1404 if (n == 'M') {
1405 j = 1;
1406 } else if ((j == 1) && (n == 1)) {
1407 j = 2;
1408 break;
1409 } else
1410 j = 0;
1411 }
1412 if (j != 2)
1413 id = -2;
1414 return id;
1415}
1416
Jiri Slabyc76b0932007-02-10 01:45:07 -08001417static void __init mxser_normal_mode(int port)
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001418{
1419 int i, n;
1420
1421 outb(0xA5, port + 1);
1422 outb(0x80, port + 3);
1423 outb(12, port + 0); /* 9600 bps */
1424 outb(0, port + 1);
1425 outb(0x03, port + 3); /* 8 data bits */
1426 outb(0x13, port + 4); /* loop back mode */
1427 for (i = 0; i < 16; i++) {
1428 n = inb(port + 5);
1429 if ((n & 0x61) == 0x60)
1430 break;
1431 if ((n & 1) == 1)
1432 (void)inb(port);
1433 }
1434 outb(0x00, port + 4);
1435}
1436
1437#define CHIP_SK 0x01 /* Serial Data Clock in Eprom */
1438#define CHIP_DO 0x02 /* Serial Data Output in Eprom */
1439#define CHIP_CS 0x04 /* Serial Chip Select in Eprom */
1440#define CHIP_DI 0x08 /* Serial Data Input in Eprom */
1441#define EN_CCMD 0x000 /* Chip's command register */
1442#define EN0_RSARLO 0x008 /* Remote start address reg 0 */
1443#define EN0_RSARHI 0x009 /* Remote start address reg 1 */
1444#define EN0_RCNTLO 0x00A /* Remote byte count reg WR */
1445#define EN0_RCNTHI 0x00B /* Remote byte count reg WR */
1446#define EN0_DCFG 0x00E /* Data configuration reg WR */
1447#define EN0_PORT 0x010 /* Rcv missed frame error counter RD */
1448#define ENC_PAGE0 0x000 /* Select page 0 of chip registers */
1449#define ENC_PAGE3 0x0C0 /* Select page 3 of chip registers */
Jiri Slabyc76b0932007-02-10 01:45:07 -08001450static int __init mxser_read_register(int port, unsigned short *regs)
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001451{
1452 int i, k, value, id;
1453 unsigned int j;
1454
1455 id = mxser_program_mode(port);
1456 if (id < 0)
1457 return id;
1458 for (i = 0; i < 14; i++) {
1459 k = (i & 0x3F) | 0x180;
1460 for (j = 0x100; j > 0; j >>= 1) {
1461 outb(CHIP_CS, port);
1462 if (k & j) {
1463 outb(CHIP_CS | CHIP_DO, port);
1464 outb(CHIP_CS | CHIP_DO | CHIP_SK, port); /* A? bit of read */
1465 } else {
1466 outb(CHIP_CS, port);
1467 outb(CHIP_CS | CHIP_SK, port); /* A? bit of read */
1468 }
1469 }
1470 (void)inb(port);
1471 value = 0;
1472 for (k = 0, j = 0x8000; k < 16; k++, j >>= 1) {
1473 outb(CHIP_CS, port);
1474 outb(CHIP_CS | CHIP_SK, port);
1475 if (inb(port) & CHIP_DI)
1476 value |= j;
1477 }
1478 regs[i] = value;
1479 outb(0, port);
1480 }
1481 mxser_normal_mode(port);
1482 return id;
1483}
1484
Jiri Slaby037ad482006-12-08 02:38:11 -08001485static int mxser_ioctl_special(unsigned int cmd, void __user *argp)
1486{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001487 struct mxser_port *port;
1488 int result, status;
1489 unsigned int i, j;
Jiri Slaby037ad482006-12-08 02:38:11 -08001490
1491 switch (cmd) {
1492 case MOXA_GET_CONF:
Jiri Slaby55b307d2006-12-08 02:38:14 -08001493/* if (copy_to_user(argp, mxsercfg,
Jiri Slaby037ad482006-12-08 02:38:11 -08001494 sizeof(struct mxser_hwconf) * 4))
1495 return -EFAULT;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001496 return 0;*/
1497 return -ENXIO;
Jiri Slaby037ad482006-12-08 02:38:11 -08001498 case MOXA_GET_MAJOR:
1499 if (copy_to_user(argp, &ttymajor, sizeof(int)))
1500 return -EFAULT;
1501 return 0;
1502
1503 case MOXA_GET_CUMAJOR:
1504 if (copy_to_user(argp, &calloutmajor, sizeof(int)))
1505 return -EFAULT;
1506 return 0;
1507
1508 case MOXA_CHKPORTENABLE:
1509 result = 0;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001510
1511 for (i = 0; i < MXSER_BOARDS; i++)
1512 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++)
1513 if (mxser_boards[i].ports[j].ioaddr)
1514 result |= (1 << i);
1515
Jiri Slaby037ad482006-12-08 02:38:11 -08001516 return put_user(result, (unsigned long __user *)argp);
1517 case MOXA_GETDATACOUNT:
1518 if (copy_to_user(argp, &mxvar_log, sizeof(mxvar_log)))
1519 return -EFAULT;
1520 return 0;
1521 case MOXA_GETMSTATUS:
Jiri Slaby55b307d2006-12-08 02:38:14 -08001522 for (i = 0; i < MXSER_BOARDS; i++)
1523 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
1524 port = &mxser_boards[i].ports[j];
1525
1526 GMStatus[i].ri = 0;
1527 if (!port->ioaddr) {
1528 GMStatus[i].dcd = 0;
1529 GMStatus[i].dsr = 0;
1530 GMStatus[i].cts = 0;
1531 continue;
1532 }
1533
1534 if (!port->tty || !port->tty->termios)
1535 GMStatus[i].cflag =
1536 port->normal_termios.c_cflag;
1537 else
1538 GMStatus[i].cflag =
1539 port->tty->termios->c_cflag;
1540
1541 status = inb(port->ioaddr + UART_MSR);
1542 if (status & 0x80 /*UART_MSR_DCD */ )
1543 GMStatus[i].dcd = 1;
1544 else
1545 GMStatus[i].dcd = 0;
1546
1547 if (status & 0x20 /*UART_MSR_DSR */ )
1548 GMStatus[i].dsr = 1;
1549 else
1550 GMStatus[i].dsr = 0;
1551
1552
1553 if (status & 0x10 /*UART_MSR_CTS */ )
1554 GMStatus[i].cts = 1;
1555 else
1556 GMStatus[i].cts = 0;
Jiri Slaby037ad482006-12-08 02:38:11 -08001557 }
Jiri Slaby037ad482006-12-08 02:38:11 -08001558 if (copy_to_user(argp, GMStatus,
1559 sizeof(struct mxser_mstatus) * MXSER_PORTS))
1560 return -EFAULT;
1561 return 0;
1562 case MOXA_ASPP_MON_EXT: {
Jiri Slaby2b770b42007-07-15 23:40:17 -07001563 int p, shiftbit;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001564 unsigned long opmode;
1565 unsigned cflag, iflag;
Jiri Slaby037ad482006-12-08 02:38:11 -08001566
Jiri Slaby55b307d2006-12-08 02:38:14 -08001567 for (i = 0; i < MXSER_BOARDS; i++)
1568 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
1569 port = &mxser_boards[i].ports[j];
1570 if (!port->ioaddr)
Jiri Slaby037ad482006-12-08 02:38:11 -08001571 continue;
1572
Jiri Slaby55b307d2006-12-08 02:38:14 -08001573 status = mxser_get_msr(port->ioaddr, 0, i);
Jiri Slaby55b307d2006-12-08 02:38:14 -08001574
Jiri Slaby037ad482006-12-08 02:38:11 -08001575 if (status & UART_MSR_TERI)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001576 port->icount.rng++;
Jiri Slaby037ad482006-12-08 02:38:11 -08001577 if (status & UART_MSR_DDSR)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001578 port->icount.dsr++;
Jiri Slaby037ad482006-12-08 02:38:11 -08001579 if (status & UART_MSR_DDCD)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001580 port->icount.dcd++;
Jiri Slaby037ad482006-12-08 02:38:11 -08001581 if (status & UART_MSR_DCTS)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001582 port->icount.cts++;
Jiri Slaby037ad482006-12-08 02:38:11 -08001583
Jiri Slaby55b307d2006-12-08 02:38:14 -08001584 port->mon_data.modem_status = status;
1585 mon_data_ext.rx_cnt[i] = port->mon_data.rxcnt;
1586 mon_data_ext.tx_cnt[i] = port->mon_data.txcnt;
1587 mon_data_ext.up_rxcnt[i] =
1588 port->mon_data.up_rxcnt;
1589 mon_data_ext.up_txcnt[i] =
1590 port->mon_data.up_txcnt;
1591 mon_data_ext.modem_status[i] =
1592 port->mon_data.modem_status;
1593 mon_data_ext.baudrate[i] = port->realbaud;
Jiri Slaby037ad482006-12-08 02:38:11 -08001594
Jiri Slaby55b307d2006-12-08 02:38:14 -08001595 if (!port->tty || !port->tty->termios) {
1596 cflag = port->normal_termios.c_cflag;
1597 iflag = port->normal_termios.c_iflag;
Jiri Slaby037ad482006-12-08 02:38:11 -08001598 } else {
Jiri Slaby55b307d2006-12-08 02:38:14 -08001599 cflag = port->tty->termios->c_cflag;
1600 iflag = port->tty->termios->c_iflag;
Jiri Slaby037ad482006-12-08 02:38:11 -08001601 }
1602
1603 mon_data_ext.databits[i] = cflag & CSIZE;
1604
1605 mon_data_ext.stopbits[i] = cflag & CSTOPB;
1606
Jiri Slaby55b307d2006-12-08 02:38:14 -08001607 mon_data_ext.parity[i] =
1608 cflag & (PARENB | PARODD | CMSPAR);
Jiri Slaby037ad482006-12-08 02:38:11 -08001609
1610 mon_data_ext.flowctrl[i] = 0x00;
1611
1612 if (cflag & CRTSCTS)
1613 mon_data_ext.flowctrl[i] |= 0x03;
1614
1615 if (iflag & (IXON | IXOFF))
1616 mon_data_ext.flowctrl[i] |= 0x0C;
1617
Jiri Slaby55b307d2006-12-08 02:38:14 -08001618 if (port->type == PORT_16550A)
Jiri Slaby037ad482006-12-08 02:38:11 -08001619 mon_data_ext.fifo[i] = 1;
1620 else
1621 mon_data_ext.fifo[i] = 0;
1622
1623 p = i % 4;
1624 shiftbit = p * 2;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001625 opmode = inb(port->opmode_ioaddr) >> shiftbit;
Jiri Slaby037ad482006-12-08 02:38:11 -08001626 opmode &= OP_MODE_MASK;
1627
1628 mon_data_ext.iftype[i] = opmode;
1629
1630 }
Jiri Slaby55b307d2006-12-08 02:38:14 -08001631 if (copy_to_user(argp, &mon_data_ext,
1632 sizeof(mon_data_ext)))
Jiri Slaby037ad482006-12-08 02:38:11 -08001633 return -EFAULT;
1634
1635 return 0;
1636
Jiri Slaby55b307d2006-12-08 02:38:14 -08001637 } default:
Jiri Slaby037ad482006-12-08 02:38:11 -08001638 return -ENOIOCTLCMD;
1639 }
1640 return 0;
1641}
1642
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001643static int mxser_ioctl(struct tty_struct *tty, struct file *file,
1644 unsigned int cmd, unsigned long arg)
1645{
1646 struct mxser_port *info = tty->driver_data;
1647 struct async_icount cprev, cnow; /* kernel counter temps */
1648 struct serial_icounter_struct __user *p_cuser;
1649 unsigned long templ;
1650 unsigned long flags;
Jiri Slabyf64c84a2007-02-10 01:45:21 -08001651 unsigned int i;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001652 void __user *argp = (void __user *)arg;
1653 int retval;
1654
1655 if (tty->index == MXSER_PORTS)
1656 return mxser_ioctl_special(cmd, argp);
1657
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001658 if (cmd == MOXA_SET_OP_MODE || cmd == MOXA_GET_OP_MODE) {
1659 int p;
1660 unsigned long opmode;
1661 static unsigned char ModeMask[] = { 0xfc, 0xf3, 0xcf, 0x3f };
1662 int shiftbit;
1663 unsigned char val, mask;
1664
1665 p = tty->index % 4;
1666 if (cmd == MOXA_SET_OP_MODE) {
1667 if (get_user(opmode, (int __user *) argp))
1668 return -EFAULT;
1669 if (opmode != RS232_MODE &&
1670 opmode != RS485_2WIRE_MODE &&
1671 opmode != RS422_MODE &&
1672 opmode != RS485_4WIRE_MODE)
1673 return -EFAULT;
1674 mask = ModeMask[p];
1675 shiftbit = p * 2;
1676 val = inb(info->opmode_ioaddr);
1677 val &= mask;
1678 val |= (opmode << shiftbit);
1679 outb(val, info->opmode_ioaddr);
1680 } else {
1681 shiftbit = p * 2;
1682 opmode = inb(info->opmode_ioaddr) >> shiftbit;
1683 opmode &= OP_MODE_MASK;
1684 if (copy_to_user(argp, &opmode, sizeof(int)))
1685 return -EFAULT;
1686 }
1687 return 0;
1688 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001689
Jiri Slabyf64c84a2007-02-10 01:45:21 -08001690 if (cmd == MOXA_SET_SPECIAL_BAUD_RATE) {
1691 int speed;
1692
1693 if (get_user(speed, (int __user *)argp))
1694 return -EFAULT;
1695 if (speed <= 0 || speed > info->max_baud)
1696 return -EFAULT;
1697 if (!info->tty || !info->tty->termios || !info->ioaddr)
1698 return 0;
1699 info->tty->termios->c_cflag &= ~(CBAUD | CBAUDEX);
1700 for (i = 0; i < BAUD_TABLE_NO; i++)
1701 if (speed == mxvar_baud_table[i])
1702 break;
1703 if (i == BAUD_TABLE_NO) {
1704 info->tty->termios->c_cflag |= B_SPEC;
1705 } else if (speed != 0)
1706 info->tty->termios->c_cflag |= mxvar_baud_table1[i];
1707
1708 info->speed = speed;
1709 spin_lock_irqsave(&info->slock, flags);
Jiri Slabya136e992007-02-10 01:45:24 -08001710 mxser_change_speed(info, NULL);
Jiri Slabyf64c84a2007-02-10 01:45:21 -08001711 spin_unlock_irqrestore(&info->slock, flags);
1712
1713 return 0;
1714 } else if (cmd == MOXA_GET_SPECIAL_BAUD_RATE) {
1715 if (copy_to_user(argp, &info->speed, sizeof(int)))
1716 return -EFAULT;
1717 return 0;
1718 }
1719
Jiri Slaby214efeb2006-12-08 02:38:25 -08001720 if (cmd != TIOCGSERIAL && cmd != TIOCMIWAIT && cmd != TIOCGICOUNT &&
1721 test_bit(TTY_IO_ERROR, &tty->flags))
1722 return -EIO;
1723
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001724 switch (cmd) {
1725 case TCSBRK: /* SVID version: non-zero arg --> no break */
1726 retval = tty_check_change(tty);
1727 if (retval)
1728 return retval;
1729 tty_wait_until_sent(tty, 0);
1730 if (!arg)
1731 mxser_send_break(info, HZ / 4); /* 1/4 second */
1732 return 0;
1733 case TCSBRKP: /* support for POSIX tcsendbreak() */
1734 retval = tty_check_change(tty);
1735 if (retval)
1736 return retval;
1737 tty_wait_until_sent(tty, 0);
1738 mxser_send_break(info, arg ? arg * (HZ / 10) : HZ / 4);
1739 return 0;
1740 case TIOCGSOFTCAR:
1741 return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *)argp);
1742 case TIOCSSOFTCAR:
1743 if (get_user(templ, (unsigned long __user *) argp))
1744 return -EFAULT;
1745 arg = templ;
1746 tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) | (arg ? CLOCAL : 0));
1747 return 0;
1748 case TIOCGSERIAL:
1749 return mxser_get_serial_info(info, argp);
1750 case TIOCSSERIAL:
1751 return mxser_set_serial_info(info, argp);
1752 case TIOCSERGETLSR: /* Get line status register */
1753 return mxser_get_lsr_info(info, argp);
1754 /*
1755 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1756 * - mask passed in arg for lines of interest
1757 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1758 * Caller should use TIOCGICOUNT to see which one it was
1759 */
Jiri Slabyb446a4a2007-04-23 14:41:03 -07001760 case TIOCMIWAIT:
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001761 spin_lock_irqsave(&info->slock, flags);
Jiri Slabyb446a4a2007-04-23 14:41:03 -07001762 cnow = info->icount; /* note the counters on entry */
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001763 spin_unlock_irqrestore(&info->slock, flags);
1764
Jiri Slabyb446a4a2007-04-23 14:41:03 -07001765 wait_event_interruptible(info->delta_msr_wait, ({
1766 cprev = cnow;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001767 spin_lock_irqsave(&info->slock, flags);
1768 cnow = info->icount; /* atomic copy */
1769 spin_unlock_irqrestore(&info->slock, flags);
1770
Jiri Slabyb446a4a2007-04-23 14:41:03 -07001771 ((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1772 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1773 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1774 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts));
1775 }));
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001776 break;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001777 /*
1778 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1779 * Return: write counters to the user passed counter struct
1780 * NB: both 1->0 and 0->1 transitions are counted except for
1781 * RI where only 0->1 is counted.
1782 */
1783 case TIOCGICOUNT:
1784 spin_lock_irqsave(&info->slock, flags);
1785 cnow = info->icount;
1786 spin_unlock_irqrestore(&info->slock, flags);
1787 p_cuser = argp;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001788 if (put_user(cnow.frame, &p_cuser->frame))
1789 return -EFAULT;
1790 if (put_user(cnow.brk, &p_cuser->brk))
1791 return -EFAULT;
1792 if (put_user(cnow.overrun, &p_cuser->overrun))
1793 return -EFAULT;
1794 if (put_user(cnow.buf_overrun, &p_cuser->buf_overrun))
1795 return -EFAULT;
1796 if (put_user(cnow.parity, &p_cuser->parity))
1797 return -EFAULT;
1798 if (put_user(cnow.rx, &p_cuser->rx))
1799 return -EFAULT;
1800 if (put_user(cnow.tx, &p_cuser->tx))
1801 return -EFAULT;
1802 put_user(cnow.cts, &p_cuser->cts);
1803 put_user(cnow.dsr, &p_cuser->dsr);
1804 put_user(cnow.rng, &p_cuser->rng);
1805 put_user(cnow.dcd, &p_cuser->dcd);
1806 return 0;
1807 case MOXA_HighSpeedOn:
1808 return put_user(info->baud_base != 115200 ? 1 : 0, (int __user *)argp);
1809 case MOXA_SDS_RSTICOUNTER:
1810 info->mon_data.rxcnt = 0;
1811 info->mon_data.txcnt = 0;
1812 return 0;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001813 case MOXA_ASPP_SETBAUD:{
1814 long baud;
1815 if (get_user(baud, (long __user *)argp))
1816 return -EFAULT;
Jiri Slabyf64c84a2007-02-10 01:45:21 -08001817 spin_lock_irqsave(&info->slock, flags);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001818 mxser_set_baud(info, baud);
Jiri Slabyf64c84a2007-02-10 01:45:21 -08001819 spin_unlock_irqrestore(&info->slock, flags);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001820 return 0;
1821 }
1822 case MOXA_ASPP_GETBAUD:
1823 if (copy_to_user(argp, &info->realbaud, sizeof(long)))
1824 return -EFAULT;
1825
1826 return 0;
1827
1828 case MOXA_ASPP_OQUEUE:{
1829 int len, lsr;
1830
1831 len = mxser_chars_in_buffer(tty);
1832
1833 lsr = inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT;
1834
1835 len += (lsr ? 0 : 1);
1836
1837 if (copy_to_user(argp, &len, sizeof(int)))
1838 return -EFAULT;
1839
1840 return 0;
1841 }
1842 case MOXA_ASPP_MON: {
1843 int mcr, status;
1844
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001845 status = mxser_get_msr(info->ioaddr, 1, tty->index);
1846 mxser_check_modem_status(info, status);
1847
1848 mcr = inb(info->ioaddr + UART_MCR);
1849 if (mcr & MOXA_MUST_MCR_XON_FLAG)
1850 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFHOLD;
1851 else
1852 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFHOLD;
1853
1854 if (mcr & MOXA_MUST_MCR_TX_XON)
1855 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFXENT;
1856 else
1857 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFXENT;
1858
1859 if (info->tty->hw_stopped)
1860 info->mon_data.hold_reason |= NPPI_NOTIFY_CTSHOLD;
1861 else
1862 info->mon_data.hold_reason &= ~NPPI_NOTIFY_CTSHOLD;
1863
1864 if (copy_to_user(argp, &info->mon_data,
1865 sizeof(struct mxser_mon)))
1866 return -EFAULT;
1867
1868 return 0;
1869 }
1870 case MOXA_ASPP_LSTATUS: {
1871 if (copy_to_user(argp, &info->err_shadow,
1872 sizeof(unsigned char)))
1873 return -EFAULT;
1874
1875 info->err_shadow = 0;
1876 return 0;
1877 }
1878 case MOXA_SET_BAUD_METHOD: {
1879 int method;
1880
1881 if (get_user(method, (int __user *)argp))
1882 return -EFAULT;
1883 mxser_set_baud_method[tty->index] = method;
1884 if (copy_to_user(argp, &method, sizeof(int)))
1885 return -EFAULT;
1886
1887 return 0;
1888 }
1889 default:
1890 return -ENOIOCTLCMD;
1891 }
1892 return 0;
1893}
1894
Jiri Slaby037ad482006-12-08 02:38:11 -08001895static void mxser_stoprx(struct tty_struct *tty)
1896{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001897 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001898
1899 info->ldisc_stop_rx = 1;
1900 if (I_IXOFF(tty)) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08001901 if (info->board->chip_flag) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001902 info->IER &= ~MOXA_MUST_RECV_ISR;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001903 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby925e9c12006-12-08 02:38:30 -08001904 } else {
Jiri Slaby037ad482006-12-08 02:38:11 -08001905 info->x_char = STOP_CHAR(tty);
Jiri Slaby55b307d2006-12-08 02:38:14 -08001906 outb(0, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001907 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001908 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001909 }
Jiri Slaby037ad482006-12-08 02:38:11 -08001910 }
1911
1912 if (info->tty->termios->c_cflag & CRTSCTS) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001913 info->MCR &= ~UART_MCR_RTS;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001914 outb(info->MCR, info->ioaddr + UART_MCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08001915 }
1916}
1917
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001918/*
1919 * This routine is called by the upper-layer tty layer to signal that
1920 * incoming characters should be throttled.
1921 */
1922static void mxser_throttle(struct tty_struct *tty)
1923{
1924 mxser_stoprx(tty);
1925}
1926
1927static void mxser_unthrottle(struct tty_struct *tty)
Jiri Slaby037ad482006-12-08 02:38:11 -08001928{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001929 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001930
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001931 /* startrx */
Jiri Slaby037ad482006-12-08 02:38:11 -08001932 info->ldisc_stop_rx = 0;
1933 if (I_IXOFF(tty)) {
1934 if (info->x_char)
1935 info->x_char = 0;
1936 else {
Jiri Slaby55b307d2006-12-08 02:38:14 -08001937 if (info->board->chip_flag) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001938 info->IER |= MOXA_MUST_RECV_ISR;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001939 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby925e9c12006-12-08 02:38:30 -08001940 } else {
Jiri Slaby037ad482006-12-08 02:38:11 -08001941 info->x_char = START_CHAR(tty);
Jiri Slaby55b307d2006-12-08 02:38:14 -08001942 outb(0, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001943 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001944 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001945 }
Jiri Slaby037ad482006-12-08 02:38:11 -08001946 }
1947 }
1948
1949 if (info->tty->termios->c_cflag & CRTSCTS) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001950 info->MCR |= UART_MCR_RTS;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001951 outb(info->MCR, info->ioaddr + UART_MCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08001952 }
1953}
1954
1955/*
Jiri Slaby037ad482006-12-08 02:38:11 -08001956 * mxser_stop() and mxser_start()
1957 *
1958 * This routines are called before setting or resetting tty->stopped.
1959 * They enable or disable transmitter interrupts, as necessary.
1960 */
1961static void mxser_stop(struct tty_struct *tty)
1962{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001963 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001964 unsigned long flags;
1965
1966 spin_lock_irqsave(&info->slock, flags);
1967 if (info->IER & UART_IER_THRI) {
1968 info->IER &= ~UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001969 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001970 }
1971 spin_unlock_irqrestore(&info->slock, flags);
1972}
1973
1974static void mxser_start(struct tty_struct *tty)
1975{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001976 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001977 unsigned long flags;
1978
1979 spin_lock_irqsave(&info->slock, flags);
Jiri Slabye079f492006-12-08 02:38:31 -08001980 if (info->xmit_cnt && info->xmit_buf) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08001981 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001982 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001983 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001984 }
1985 spin_unlock_irqrestore(&info->slock, flags);
1986}
1987
Alan Cox606d0992006-12-08 02:38:45 -08001988static void mxser_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001989{
1990 struct mxser_port *info = tty->driver_data;
1991 unsigned long flags;
1992
1993 if ((tty->termios->c_cflag != old_termios->c_cflag) ||
1994 (RELEVANT_IFLAG(tty->termios->c_iflag) != RELEVANT_IFLAG(old_termios->c_iflag))) {
1995
Jiri Slabyf64c84a2007-02-10 01:45:21 -08001996 spin_lock_irqsave(&info->slock, flags);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001997 mxser_change_speed(info, old_termios);
Jiri Slabyf64c84a2007-02-10 01:45:21 -08001998 spin_unlock_irqrestore(&info->slock, flags);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001999
2000 if ((old_termios->c_cflag & CRTSCTS) &&
2001 !(tty->termios->c_cflag & CRTSCTS)) {
2002 tty->hw_stopped = 0;
2003 mxser_start(tty);
2004 }
2005 }
2006
Jiri Slabye079f492006-12-08 02:38:31 -08002007 /* Handle sw stopped */
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002008 if ((old_termios->c_iflag & IXON) &&
2009 !(tty->termios->c_iflag & IXON)) {
2010 tty->stopped = 0;
2011
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002012 if (info->board->chip_flag) {
2013 spin_lock_irqsave(&info->slock, flags);
2014 DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
2015 spin_unlock_irqrestore(&info->slock, flags);
2016 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002017
2018 mxser_start(tty);
2019 }
2020}
2021
Jiri Slaby037ad482006-12-08 02:38:11 -08002022/*
2023 * mxser_wait_until_sent() --- wait until the transmitter is empty
2024 */
2025static void mxser_wait_until_sent(struct tty_struct *tty, int timeout)
2026{
Jiri Slaby55b307d2006-12-08 02:38:14 -08002027 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08002028 unsigned long orig_jiffies, char_time;
2029 int lsr;
2030
2031 if (info->type == PORT_UNKNOWN)
2032 return;
2033
2034 if (info->xmit_fifo_size == 0)
2035 return; /* Just in case.... */
2036
2037 orig_jiffies = jiffies;
2038 /*
2039 * Set the check interval to be 1/5 of the estimated time to
2040 * send a single character, and make it at least 1. The check
2041 * interval should also be less than the timeout.
2042 *
2043 * Note: we have to use pretty tight timings here to satisfy
2044 * the NIST-PCTS.
2045 */
2046 char_time = (info->timeout - HZ / 50) / info->xmit_fifo_size;
2047 char_time = char_time / 5;
2048 if (char_time == 0)
2049 char_time = 1;
2050 if (timeout && timeout < char_time)
2051 char_time = timeout;
2052 /*
2053 * If the transmitter hasn't cleared in twice the approximate
2054 * amount of time to send the entire FIFO, it probably won't
2055 * ever clear. This assumes the UART isn't doing flow
2056 * control, which is currently the case. Hence, if it ever
2057 * takes longer than info->timeout, this is probably due to a
2058 * UART bug of some kind. So, we clamp the timeout parameter at
2059 * 2*info->timeout.
2060 */
2061 if (!timeout || timeout > 2 * info->timeout)
2062 timeout = 2 * info->timeout;
2063#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2064 printk(KERN_DEBUG "In rs_wait_until_sent(%d) check=%lu...",
2065 timeout, char_time);
2066 printk("jiff=%lu...", jiffies);
2067#endif
Jiri Slaby55b307d2006-12-08 02:38:14 -08002068 while (!((lsr = inb(info->ioaddr + UART_LSR)) & UART_LSR_TEMT)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002069#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2070 printk("lsr = %d (jiff=%lu)...", lsr, jiffies);
2071#endif
2072 schedule_timeout_interruptible(char_time);
2073 if (signal_pending(current))
2074 break;
2075 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2076 break;
2077 }
2078 set_current_state(TASK_RUNNING);
2079
2080#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2081 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
2082#endif
2083}
2084
Jiri Slaby037ad482006-12-08 02:38:11 -08002085/*
2086 * This routine is called by tty_hangup() when a hangup is signaled.
2087 */
Adrian Bunk02d3fca2007-02-10 01:44:51 -08002088static void mxser_hangup(struct tty_struct *tty)
Jiri Slaby037ad482006-12-08 02:38:11 -08002089{
Jiri Slaby55b307d2006-12-08 02:38:14 -08002090 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08002091
2092 mxser_flush_buffer(tty);
2093 mxser_shutdown(info);
2094 info->event = 0;
2095 info->count = 0;
2096 info->flags &= ~ASYNC_NORMAL_ACTIVE;
2097 info->tty = NULL;
2098 wake_up_interruptible(&info->open_wait);
2099}
2100
Jiri Slaby037ad482006-12-08 02:38:11 -08002101/*
2102 * mxser_rs_break() --- routine which turns the break handling on or off
2103 */
2104static void mxser_rs_break(struct tty_struct *tty, int break_state)
2105{
Jiri Slaby55b307d2006-12-08 02:38:14 -08002106 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08002107 unsigned long flags;
2108
2109 spin_lock_irqsave(&info->slock, flags);
2110 if (break_state == -1)
Jiri Slaby55b307d2006-12-08 02:38:14 -08002111 outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
2112 info->ioaddr + UART_LCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002113 else
Jiri Slaby55b307d2006-12-08 02:38:14 -08002114 outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
2115 info->ioaddr + UART_LCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002116 spin_unlock_irqrestore(&info->slock, flags);
2117}
2118
Jiri Slaby55b307d2006-12-08 02:38:14 -08002119static void mxser_receive_chars(struct mxser_port *port, int *status)
Jiri Slaby037ad482006-12-08 02:38:11 -08002120{
Jiri Slaby55b307d2006-12-08 02:38:14 -08002121 struct tty_struct *tty = port->tty;
Jiri Slaby037ad482006-12-08 02:38:11 -08002122 unsigned char ch, gdl;
2123 int ignored = 0;
2124 int cnt = 0;
2125 int recv_room;
2126 int max = 256;
Jiri Slaby037ad482006-12-08 02:38:11 -08002127
2128 recv_room = tty->receive_room;
Jiri Slabye079f492006-12-08 02:38:31 -08002129 if ((recv_room == 0) && (!port->ldisc_stop_rx))
Jiri Slaby037ad482006-12-08 02:38:11 -08002130 mxser_stoprx(tty);
Jiri Slaby037ad482006-12-08 02:38:11 -08002131
Jiri Slaby55b307d2006-12-08 02:38:14 -08002132 if (port->board->chip_flag != MOXA_OTHER_UART) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002133
Jiri Slaby3306ce32006-12-08 02:38:13 -08002134 if (*status & UART_LSR_SPECIAL)
Jiri Slaby037ad482006-12-08 02:38:11 -08002135 goto intr_old;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002136 if (port->board->chip_flag == MOXA_MUST_MU860_HWID &&
Jiri Slaby037ad482006-12-08 02:38:11 -08002137 (*status & MOXA_MUST_LSR_RERR))
2138 goto intr_old;
Jiri Slaby037ad482006-12-08 02:38:11 -08002139 if (*status & MOXA_MUST_LSR_RERR)
2140 goto intr_old;
2141
Jiri Slaby55b307d2006-12-08 02:38:14 -08002142 gdl = inb(port->ioaddr + MOXA_MUST_GDL_REGISTER);
Jiri Slaby037ad482006-12-08 02:38:11 -08002143
Jiri Slaby55b307d2006-12-08 02:38:14 -08002144 if (port->board->chip_flag == MOXA_MUST_MU150_HWID)
Jiri Slaby037ad482006-12-08 02:38:11 -08002145 gdl &= MOXA_MUST_GDL_MASK;
2146 if (gdl >= recv_room) {
Jiri Slabye079f492006-12-08 02:38:31 -08002147 if (!port->ldisc_stop_rx)
Jiri Slaby037ad482006-12-08 02:38:11 -08002148 mxser_stoprx(tty);
Jiri Slaby037ad482006-12-08 02:38:11 -08002149 }
2150 while (gdl--) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002151 ch = inb(port->ioaddr + UART_RX);
Jiri Slaby037ad482006-12-08 02:38:11 -08002152 tty_insert_flip_char(tty, ch, 0);
2153 cnt++;
Jiri Slaby037ad482006-12-08 02:38:11 -08002154 }
2155 goto end_intr;
2156 }
Jiri Slabye079f492006-12-08 02:38:31 -08002157intr_old:
Jiri Slaby037ad482006-12-08 02:38:11 -08002158
2159 do {
2160 if (max-- < 0)
2161 break;
Jiri Slaby037ad482006-12-08 02:38:11 -08002162
Jiri Slaby55b307d2006-12-08 02:38:14 -08002163 ch = inb(port->ioaddr + UART_RX);
Jiri Slabye079f492006-12-08 02:38:31 -08002164 if (port->board->chip_flag && (*status & UART_LSR_OE))
Jiri Slaby55b307d2006-12-08 02:38:14 -08002165 outb(0x23, port->ioaddr + UART_FCR);
2166 *status &= port->read_status_mask;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002167 if (*status & port->ignore_status_mask) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002168 if (++ignored > 100)
2169 break;
2170 } else {
2171 char flag = 0;
2172 if (*status & UART_LSR_SPECIAL) {
2173 if (*status & UART_LSR_BI) {
2174 flag = TTY_BREAK;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002175 port->icount.brk++;
Jiri Slaby3306ce32006-12-08 02:38:13 -08002176
Jiri Slaby55b307d2006-12-08 02:38:14 -08002177 if (port->flags & ASYNC_SAK)
Jiri Slaby037ad482006-12-08 02:38:11 -08002178 do_SAK(tty);
2179 } else if (*status & UART_LSR_PE) {
2180 flag = TTY_PARITY;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002181 port->icount.parity++;
Jiri Slaby037ad482006-12-08 02:38:11 -08002182 } else if (*status & UART_LSR_FE) {
2183 flag = TTY_FRAME;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002184 port->icount.frame++;
Jiri Slaby037ad482006-12-08 02:38:11 -08002185 } else if (*status & UART_LSR_OE) {
2186 flag = TTY_OVERRUN;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002187 port->icount.overrun++;
Jiri Slabyf64c84a2007-02-10 01:45:21 -08002188 } else
2189 flag = TTY_BREAK;
Jiri Slaby925e9c12006-12-08 02:38:30 -08002190 }
Jiri Slaby037ad482006-12-08 02:38:11 -08002191 tty_insert_flip_char(tty, ch, flag);
2192 cnt++;
2193 if (cnt >= recv_room) {
Jiri Slabye079f492006-12-08 02:38:31 -08002194 if (!port->ldisc_stop_rx)
Jiri Slaby037ad482006-12-08 02:38:11 -08002195 mxser_stoprx(tty);
Jiri Slaby037ad482006-12-08 02:38:11 -08002196 break;
2197 }
2198
2199 }
2200
Jiri Slaby55b307d2006-12-08 02:38:14 -08002201 if (port->board->chip_flag)
Jiri Slaby037ad482006-12-08 02:38:11 -08002202 break;
Jiri Slaby037ad482006-12-08 02:38:11 -08002203
Jiri Slaby55b307d2006-12-08 02:38:14 -08002204 *status = inb(port->ioaddr + UART_LSR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002205 } while (*status & UART_LSR_DR);
2206
Jiri Slabye079f492006-12-08 02:38:31 -08002207end_intr:
Jiri Slaby55b307d2006-12-08 02:38:14 -08002208 mxvar_log.rxcnt[port->tty->index] += cnt;
2209 port->mon_data.rxcnt += cnt;
2210 port->mon_data.up_rxcnt += cnt;
Jiri Slaby037ad482006-12-08 02:38:11 -08002211
Jan Yenya Kasprzak67d2bc52007-04-23 14:41:02 -07002212 /*
2213 * We are called from an interrupt context with &port->slock
2214 * being held. Drop it temporarily in order to prevent
2215 * recursive locking.
2216 */
2217 spin_unlock(&port->slock);
Jiri Slaby037ad482006-12-08 02:38:11 -08002218 tty_flip_buffer_push(tty);
Jan Yenya Kasprzak67d2bc52007-04-23 14:41:02 -07002219 spin_lock(&port->slock);
Jiri Slaby037ad482006-12-08 02:38:11 -08002220}
2221
Jiri Slaby55b307d2006-12-08 02:38:14 -08002222static void mxser_transmit_chars(struct mxser_port *port)
Jiri Slaby037ad482006-12-08 02:38:11 -08002223{
2224 int count, cnt;
Jiri Slaby037ad482006-12-08 02:38:11 -08002225
Jiri Slaby55b307d2006-12-08 02:38:14 -08002226 if (port->x_char) {
2227 outb(port->x_char, port->ioaddr + UART_TX);
2228 port->x_char = 0;
2229 mxvar_log.txcnt[port->tty->index]++;
2230 port->mon_data.txcnt++;
2231 port->mon_data.up_txcnt++;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002232 port->icount.tx++;
Jiri Slabyf5748742007-02-10 01:45:18 -08002233 return;
Jiri Slaby037ad482006-12-08 02:38:11 -08002234 }
2235
Jiri Slaby55b307d2006-12-08 02:38:14 -08002236 if (port->xmit_buf == 0)
Jiri Slabyf5748742007-02-10 01:45:18 -08002237 return;
Jiri Slaby037ad482006-12-08 02:38:11 -08002238
Jiri Slaby925e9c12006-12-08 02:38:30 -08002239 if ((port->xmit_cnt <= 0) || port->tty->stopped ||
2240 (port->tty->hw_stopped &&
Jiri Slaby55b307d2006-12-08 02:38:14 -08002241 (port->type != PORT_16550A) &&
2242 (!port->board->chip_flag))) {
2243 port->IER &= ~UART_IER_THRI;
2244 outb(port->IER, port->ioaddr + UART_IER);
Jiri Slabyf5748742007-02-10 01:45:18 -08002245 return;
Jiri Slaby037ad482006-12-08 02:38:11 -08002246 }
2247
Jiri Slaby55b307d2006-12-08 02:38:14 -08002248 cnt = port->xmit_cnt;
2249 count = port->xmit_fifo_size;
Jiri Slaby037ad482006-12-08 02:38:11 -08002250 do {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002251 outb(port->xmit_buf[port->xmit_tail++],
2252 port->ioaddr + UART_TX);
2253 port->xmit_tail = port->xmit_tail & (SERIAL_XMIT_SIZE - 1);
2254 if (--port->xmit_cnt <= 0)
Jiri Slaby037ad482006-12-08 02:38:11 -08002255 break;
2256 } while (--count > 0);
Jiri Slaby55b307d2006-12-08 02:38:14 -08002257 mxvar_log.txcnt[port->tty->index] += (cnt - port->xmit_cnt);
Jiri Slaby037ad482006-12-08 02:38:11 -08002258
Jiri Slaby55b307d2006-12-08 02:38:14 -08002259 port->mon_data.txcnt += (cnt - port->xmit_cnt);
2260 port->mon_data.up_txcnt += (cnt - port->xmit_cnt);
Jiri Slaby55b307d2006-12-08 02:38:14 -08002261 port->icount.tx += (cnt - port->xmit_cnt);
Jiri Slaby037ad482006-12-08 02:38:11 -08002262
Jiri Slaby47c85c02007-02-10 01:45:15 -08002263 if (port->xmit_cnt < WAKEUP_CHARS)
2264 tty_wakeup(port->tty);
2265
Jiri Slaby55b307d2006-12-08 02:38:14 -08002266 if (port->xmit_cnt <= 0) {
2267 port->IER &= ~UART_IER_THRI;
2268 outb(port->IER, port->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08002269 }
Jiri Slaby037ad482006-12-08 02:38:11 -08002270}
2271
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002272/*
2273 * This is the serial driver's generic interrupt routine
2274 */
Jiri Slabyb1d1c8d2006-12-08 02:38:31 -08002275static irqreturn_t mxser_interrupt(int irq, void *dev_id)
Jiri Slaby037ad482006-12-08 02:38:11 -08002276{
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002277 int status, iir, i;
2278 struct mxser_board *brd = NULL;
2279 struct mxser_port *port;
2280 int max, irqbits, bits, msr;
Jiri Slaby18b95572007-02-10 01:45:19 -08002281 unsigned int int_cnt, pass_counter = 0;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002282 int handled = IRQ_NONE;
Jiri Slaby037ad482006-12-08 02:38:11 -08002283
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002284 for (i = 0; i < MXSER_BOARDS; i++)
2285 if (dev_id == &mxser_boards[i]) {
2286 brd = dev_id;
2287 break;
Jiri Slaby037ad482006-12-08 02:38:11 -08002288 }
Jiri Slaby037ad482006-12-08 02:38:11 -08002289
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002290 if (i == MXSER_BOARDS)
2291 goto irq_stop;
2292 if (brd == NULL)
2293 goto irq_stop;
Jiri Slabycd7ed642006-12-08 02:38:29 -08002294 max = brd->info->nports;
Jiri Slaby18b95572007-02-10 01:45:19 -08002295 while (pass_counter++ < MXSER_ISR_PASS_LIMIT) {
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002296 irqbits = inb(brd->vector) & brd->vector_mask;
2297 if (irqbits == brd->vector_mask)
Jiri Slaby037ad482006-12-08 02:38:11 -08002298 break;
Jiri Slaby037ad482006-12-08 02:38:11 -08002299
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002300 handled = IRQ_HANDLED;
2301 for (i = 0, bits = 1; i < max; i++, irqbits |= bits, bits <<= 1) {
2302 if (irqbits == brd->vector_mask)
Jiri Slaby037ad482006-12-08 02:38:11 -08002303 break;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002304 if (bits & irqbits)
2305 continue;
2306 port = &brd->ports[i];
Jiri Slaby037ad482006-12-08 02:38:11 -08002307
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002308 int_cnt = 0;
Jiri Slabyf5748742007-02-10 01:45:18 -08002309 spin_lock(&port->slock);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002310 do {
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002311 iir = inb(port->ioaddr + UART_IIR);
2312 if (iir & UART_IIR_NO_INT)
2313 break;
2314 iir &= MOXA_MUST_IIR_MASK;
Jiri Slabyf5748742007-02-10 01:45:18 -08002315 if (!port->tty ||
2316 (port->flags & ASYNC_CLOSING) ||
2317 !(port->flags &
2318 ASYNC_INITIALIZED)) {
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002319 status = inb(port->ioaddr + UART_LSR);
2320 outb(0x27, port->ioaddr + UART_FCR);
2321 inb(port->ioaddr + UART_MSR);
2322 break;
Jiri Slaby037ad482006-12-08 02:38:11 -08002323 }
Jiri Slaby037ad482006-12-08 02:38:11 -08002324
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002325 status = inb(port->ioaddr + UART_LSR);
2326
2327 if (status & UART_LSR_PE)
2328 port->err_shadow |= NPPI_NOTIFY_PARITY;
2329 if (status & UART_LSR_FE)
2330 port->err_shadow |= NPPI_NOTIFY_FRAMING;
2331 if (status & UART_LSR_OE)
2332 port->err_shadow |=
2333 NPPI_NOTIFY_HW_OVERRUN;
2334 if (status & UART_LSR_BI)
2335 port->err_shadow |= NPPI_NOTIFY_BREAK;
2336
2337 if (port->board->chip_flag) {
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002338 if (iir == MOXA_MUST_IIR_GDA ||
2339 iir == MOXA_MUST_IIR_RDA ||
2340 iir == MOXA_MUST_IIR_RTO ||
2341 iir == MOXA_MUST_IIR_LSR)
2342 mxser_receive_chars(port,
2343 &status);
2344
2345 } else {
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002346 status &= port->read_status_mask;
2347 if (status & UART_LSR_DR)
2348 mxser_receive_chars(port,
2349 &status);
2350 }
2351 msr = inb(port->ioaddr + UART_MSR);
2352 if (msr & UART_MSR_ANY_DELTA)
2353 mxser_check_modem_status(port, msr);
2354
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002355 if (port->board->chip_flag) {
2356 if (iir == 0x02 && (status &
2357 UART_LSR_THRE))
2358 mxser_transmit_chars(port);
2359 } else {
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002360 if (status & UART_LSR_THRE)
2361 mxser_transmit_chars(port);
2362 }
2363 } while (int_cnt++ < MXSER_ISR_PASS_LIMIT);
Jiri Slabyf5748742007-02-10 01:45:18 -08002364 spin_unlock(&port->slock);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002365 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002366 }
2367
Jiri Slabye079f492006-12-08 02:38:31 -08002368irq_stop:
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002369 return handled;
2370}
2371
2372static const struct tty_operations mxser_ops = {
2373 .open = mxser_open,
2374 .close = mxser_close,
2375 .write = mxser_write,
2376 .put_char = mxser_put_char,
2377 .flush_chars = mxser_flush_chars,
2378 .write_room = mxser_write_room,
2379 .chars_in_buffer = mxser_chars_in_buffer,
2380 .flush_buffer = mxser_flush_buffer,
2381 .ioctl = mxser_ioctl,
2382 .throttle = mxser_throttle,
2383 .unthrottle = mxser_unthrottle,
2384 .set_termios = mxser_set_termios,
2385 .stop = mxser_stop,
2386 .start = mxser_start,
2387 .hangup = mxser_hangup,
2388 .break_ctl = mxser_rs_break,
2389 .wait_until_sent = mxser_wait_until_sent,
2390 .tiocmget = mxser_tiocmget,
2391 .tiocmset = mxser_tiocmset,
2392};
2393
2394/*
2395 * The MOXA Smartio/Industio serial driver boot-time initialization code!
2396 */
2397
Jiri Slaby2094e752006-12-08 02:38:33 -08002398static void mxser_release_res(struct mxser_board *brd, struct pci_dev *pdev,
2399 unsigned int irq)
Jiri Slaby171d3a82006-12-08 02:38:25 -08002400{
Jiri Slaby171d3a82006-12-08 02:38:25 -08002401 if (irq)
2402 free_irq(brd->irq, brd);
2403 if (pdev != NULL) { /* PCI */
Jiri Slabyfb0c9292006-12-13 00:34:19 -08002404#ifdef CONFIG_PCI
Jiri Slaby171d3a82006-12-08 02:38:25 -08002405 pci_release_region(pdev, 2);
2406 pci_release_region(pdev, 3);
Jiri Slabyfb0c9292006-12-13 00:34:19 -08002407#endif
Jiri Slaby171d3a82006-12-08 02:38:25 -08002408 } else {
Jiri Slabycd7ed642006-12-08 02:38:29 -08002409 release_region(brd->ports[0].ioaddr, 8 * brd->info->nports);
Jiri Slaby171d3a82006-12-08 02:38:25 -08002410 release_region(brd->vector, 1);
2411 }
2412}
2413
Jiri Slaby2094e752006-12-08 02:38:33 -08002414static int __devinit mxser_initbrd(struct mxser_board *brd,
2415 struct pci_dev *pdev)
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002416{
2417 struct mxser_port *info;
2418 unsigned int i;
2419 int retval;
2420
2421 printk(KERN_INFO "max. baud rate = %d bps.\n", brd->ports[0].max_baud);
2422
Jiri Slabycd7ed642006-12-08 02:38:29 -08002423 for (i = 0; i < brd->info->nports; i++) {
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002424 info = &brd->ports[i];
2425 info->board = brd;
2426 info->stop_rx = 0;
2427 info->ldisc_stop_rx = 0;
2428
2429 /* Enhance mode enabled here */
2430 if (brd->chip_flag != MOXA_OTHER_UART)
2431 ENABLE_MOXA_MUST_ENCHANCE_MODE(info->ioaddr);
2432
2433 info->flags = ASYNC_SHARE_IRQ;
2434 info->type = brd->uart_type;
2435
2436 process_txrx_fifo(info);
2437
2438 info->custom_divisor = info->baud_base * 16;
2439 info->close_delay = 5 * HZ / 10;
2440 info->closing_wait = 30 * HZ;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002441 info->normal_termios = mxvar_sdriver->init_termios;
2442 init_waitqueue_head(&info->open_wait);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002443 init_waitqueue_head(&info->delta_msr_wait);
Jiri Slabyf64c84a2007-02-10 01:45:21 -08002444 info->speed = 9600;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002445 memset(&info->mon_data, 0, sizeof(struct mxser_mon));
2446 info->err_shadow = 0;
2447 spin_lock_init(&info->slock);
2448
2449 /* before set INT ISR, disable all int */
2450 outb(inb(info->ioaddr + UART_IER) & 0xf0,
2451 info->ioaddr + UART_IER);
2452 }
Jiri Slaby037ad482006-12-08 02:38:11 -08002453
Jiri Slabyc493edd2007-02-10 01:45:16 -08002454 retval = request_irq(brd->irq, mxser_interrupt, IRQF_SHARED, "mxser",
2455 brd);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002456 if (retval) {
2457 printk(KERN_ERR "Board %s: Request irq failed, IRQ (%d) may "
2458 "conflict with another device.\n",
Jiri Slabycd7ed642006-12-08 02:38:29 -08002459 brd->info->name, brd->irq);
Jiri Slaby171d3a82006-12-08 02:38:25 -08002460 /* We hold resources, we need to release them. */
Jiri Slaby2094e752006-12-08 02:38:33 -08002461 mxser_release_res(brd, pdev, 0);
Jiri Slaby037ad482006-12-08 02:38:11 -08002462 }
Jiri Slaby45257fa2007-02-10 01:45:20 -08002463 return retval;
Jiri Slaby037ad482006-12-08 02:38:11 -08002464}
2465
Jiri Slaby943f2952006-12-08 02:38:15 -08002466static int __init mxser_get_ISA_conf(int cap, struct mxser_board *brd)
Jiri Slaby037ad482006-12-08 02:38:11 -08002467{
2468 int id, i, bits;
2469 unsigned short regs[16], irq;
2470 unsigned char scratch, scratch2;
2471
Jiri Slaby55b307d2006-12-08 02:38:14 -08002472 brd->chip_flag = MOXA_OTHER_UART;
Jiri Slaby037ad482006-12-08 02:38:11 -08002473
2474 id = mxser_read_register(cap, regs);
Jiri Slabycd7ed642006-12-08 02:38:29 -08002475 switch (id) {
2476 case C168_ASIC_ID:
2477 brd->info = &mxser_cards[0];
2478 break;
2479 case C104_ASIC_ID:
2480 brd->info = &mxser_cards[1];
2481 break;
2482 case CI104J_ASIC_ID:
2483 brd->info = &mxser_cards[2];
2484 break;
2485 case C102_ASIC_ID:
2486 brd->info = &mxser_cards[5];
2487 break;
2488 case CI132_ASIC_ID:
2489 brd->info = &mxser_cards[6];
2490 break;
2491 case CI134_ASIC_ID:
2492 brd->info = &mxser_cards[7];
2493 break;
2494 default:
Jiri Slaby037ad482006-12-08 02:38:11 -08002495 return 0;
Jiri Slabycd7ed642006-12-08 02:38:29 -08002496 }
Jiri Slaby037ad482006-12-08 02:38:11 -08002497
2498 irq = 0;
Jiri Slabycd7ed642006-12-08 02:38:29 -08002499 /* some ISA cards have 2 ports, but we want to see them as 4-port (why?)
2500 Flag-hack checks if configuration should be read as 2-port here. */
2501 if (brd->info->nports == 2 || (brd->info->flags & MXSER_HAS2)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002502 irq = regs[9] & 0xF000;
2503 irq = irq | (irq >> 4);
2504 if (irq != (regs[9] & 0xFF00))
2505 return MXSER_ERR_IRQ_CONFLIT;
Jiri Slabycd7ed642006-12-08 02:38:29 -08002506 } else if (brd->info->nports == 4) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002507 irq = regs[9] & 0xF000;
2508 irq = irq | (irq >> 4);
2509 irq = irq | (irq >> 8);
2510 if (irq != regs[9])
2511 return MXSER_ERR_IRQ_CONFLIT;
Jiri Slabycd7ed642006-12-08 02:38:29 -08002512 } else if (brd->info->nports == 8) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002513 irq = regs[9] & 0xF000;
2514 irq = irq | (irq >> 4);
2515 irq = irq | (irq >> 8);
2516 if ((irq != regs[9]) || (irq != regs[10]))
2517 return MXSER_ERR_IRQ_CONFLIT;
2518 }
2519
2520 if (!irq)
2521 return MXSER_ERR_IRQ;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002522 brd->irq = ((int)(irq & 0xF000) >> 12);
Jiri Slaby037ad482006-12-08 02:38:11 -08002523 for (i = 0; i < 8; i++)
Jiri Slaby55b307d2006-12-08 02:38:14 -08002524 brd->ports[i].ioaddr = (int) regs[i + 1] & 0xFFF8;
Jiri Slaby037ad482006-12-08 02:38:11 -08002525 if ((regs[12] & 0x80) == 0)
2526 return MXSER_ERR_VECTOR;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002527 brd->vector = (int)regs[11]; /* interrupt vector */
Jiri Slaby037ad482006-12-08 02:38:11 -08002528 if (id == 1)
Jiri Slaby55b307d2006-12-08 02:38:14 -08002529 brd->vector_mask = 0x00FF;
Jiri Slaby037ad482006-12-08 02:38:11 -08002530 else
Jiri Slaby55b307d2006-12-08 02:38:14 -08002531 brd->vector_mask = 0x000F;
Jiri Slaby037ad482006-12-08 02:38:11 -08002532 for (i = 7, bits = 0x0100; i >= 0; i--, bits <<= 1) {
2533 if (regs[12] & bits) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002534 brd->ports[i].baud_base = 921600;
Jiri Slabye079f492006-12-08 02:38:31 -08002535 brd->ports[i].max_baud = 921600;
Jiri Slaby037ad482006-12-08 02:38:11 -08002536 } else {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002537 brd->ports[i].baud_base = 115200;
Jiri Slabye079f492006-12-08 02:38:31 -08002538 brd->ports[i].max_baud = 115200;
Jiri Slaby037ad482006-12-08 02:38:11 -08002539 }
2540 }
2541 scratch2 = inb(cap + UART_LCR) & (~UART_LCR_DLAB);
2542 outb(scratch2 | UART_LCR_DLAB, cap + UART_LCR);
2543 outb(0, cap + UART_EFR); /* EFR is the same as FCR */
2544 outb(scratch2, cap + UART_LCR);
2545 outb(UART_FCR_ENABLE_FIFO, cap + UART_FCR);
2546 scratch = inb(cap + UART_IIR);
2547
2548 if (scratch & 0xC0)
Jiri Slaby55b307d2006-12-08 02:38:14 -08002549 brd->uart_type = PORT_16550A;
Jiri Slaby037ad482006-12-08 02:38:11 -08002550 else
Jiri Slaby55b307d2006-12-08 02:38:14 -08002551 brd->uart_type = PORT_16450;
Jiri Slabycd7ed642006-12-08 02:38:29 -08002552 if (!request_region(brd->ports[0].ioaddr, 8 * brd->info->nports,
2553 "mxser(IO)"))
Jiri Slaby7a7a5c32006-12-08 02:38:17 -08002554 return MXSER_ERR_IOADDR;
2555 if (!request_region(brd->vector, 1, "mxser(vector)")) {
Jiri Slabycd7ed642006-12-08 02:38:29 -08002556 release_region(brd->ports[0].ioaddr, 8 * brd->info->nports);
Jiri Slaby7a7a5c32006-12-08 02:38:17 -08002557 return MXSER_ERR_VECTOR;
2558 }
Jiri Slabycd7ed642006-12-08 02:38:29 -08002559 return brd->info->nports;
Jiri Slaby037ad482006-12-08 02:38:11 -08002560}
2561
Jiri Slaby2094e752006-12-08 02:38:33 -08002562static int __devinit mxser_probe(struct pci_dev *pdev,
2563 const struct pci_device_id *ent)
Jiri Slaby037ad482006-12-08 02:38:11 -08002564{
Jiri Slabyfb0c9292006-12-13 00:34:19 -08002565#ifdef CONFIG_PCI
Jiri Slaby2094e752006-12-08 02:38:33 -08002566 struct mxser_board *brd;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002567 unsigned int i, j;
2568 unsigned long ioaddress;
Jiri Slaby2094e752006-12-08 02:38:33 -08002569 int retval = -EINVAL;
2570
2571 for (i = 0; i < MXSER_BOARDS; i++)
2572 if (mxser_boards[i].info == NULL)
2573 break;
2574
2575 if (i >= MXSER_BOARDS) {
2576 printk(KERN_ERR "Too many Smartio/Industio family boards found "
2577 "(maximum %d), board not configured\n", MXSER_BOARDS);
2578 goto err;
2579 }
2580
2581 brd = &mxser_boards[i];
2582 brd->idx = i * MXSER_PORTS_PER_BOARD;
2583 printk(KERN_INFO "Found MOXA %s board (BusNo=%d, DevNo=%d)\n",
2584 mxser_cards[ent->driver_data].name,
2585 pdev->bus->number, PCI_SLOT(pdev->devfn));
2586
2587 retval = pci_enable_device(pdev);
2588 if (retval) {
2589 printk(KERN_ERR "Moxa SmartI/O PCI enable fail !\n");
2590 goto err;
2591 }
Jiri Slaby037ad482006-12-08 02:38:11 -08002592
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002593 /* io address */
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002594 ioaddress = pci_resource_start(pdev, 2);
2595 retval = pci_request_region(pdev, 2, "mxser(IO)");
2596 if (retval)
2597 goto err;
2598
Jiri Slaby2094e752006-12-08 02:38:33 -08002599 brd->info = &mxser_cards[ent->driver_data];
Jiri Slabycd7ed642006-12-08 02:38:29 -08002600 for (i = 0; i < brd->info->nports; i++)
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002601 brd->ports[i].ioaddr = ioaddress + 8 * i;
2602
2603 /* vector */
2604 ioaddress = pci_resource_start(pdev, 3);
2605 retval = pci_request_region(pdev, 3, "mxser(vector)");
2606 if (retval)
2607 goto err_relio;
2608 brd->vector = ioaddress;
2609
2610 /* irq */
2611 brd->irq = pdev->irq;
2612
2613 brd->chip_flag = CheckIsMoxaMust(brd->ports[0].ioaddr);
2614 brd->uart_type = PORT_16550A;
2615 brd->vector_mask = 0;
2616
Jiri Slabycd7ed642006-12-08 02:38:29 -08002617 for (i = 0; i < brd->info->nports; i++) {
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002618 for (j = 0; j < UART_INFO_NUM; j++) {
2619 if (Gpci_uart_info[j].type == brd->chip_flag) {
2620 brd->ports[i].max_baud =
2621 Gpci_uart_info[j].max_baud;
2622
2623 /* exception....CP-102 */
Jiri Slabycd7ed642006-12-08 02:38:29 -08002624 if (brd->info->flags & MXSER_HIGHBAUD)
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002625 brd->ports[i].max_baud = 921600;
2626 break;
Jiri Slaby037ad482006-12-08 02:38:11 -08002627 }
2628 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002629 }
2630
2631 if (brd->chip_flag == MOXA_MUST_MU860_HWID) {
Jiri Slabycd7ed642006-12-08 02:38:29 -08002632 for (i = 0; i < brd->info->nports; i++) {
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002633 if (i < 4)
2634 brd->ports[i].opmode_ioaddr = ioaddress + 4;
2635 else
2636 brd->ports[i].opmode_ioaddr = ioaddress + 0x0c;
Jiri Slaby037ad482006-12-08 02:38:11 -08002637 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002638 outb(0, ioaddress + 4); /* default set to RS232 mode */
2639 outb(0, ioaddress + 0x0c); /* default set to RS232 mode */
Jiri Slaby037ad482006-12-08 02:38:11 -08002640 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002641
Jiri Slabycd7ed642006-12-08 02:38:29 -08002642 for (i = 0; i < brd->info->nports; i++) {
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002643 brd->vector_mask |= (1 << i);
2644 brd->ports[i].baud_base = 921600;
2645 }
Jiri Slaby2094e752006-12-08 02:38:33 -08002646
2647 /* mxser_initbrd will hook ISR. */
Jiri Slaby45257fa2007-02-10 01:45:20 -08002648 retval = mxser_initbrd(brd, pdev);
2649 if (retval)
2650 goto err_null;
Jiri Slaby2094e752006-12-08 02:38:33 -08002651
2652 for (i = 0; i < brd->info->nports; i++)
2653 tty_register_device(mxvar_sdriver, brd->idx + i, &pdev->dev);
2654
2655 pci_set_drvdata(pdev, brd);
2656
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002657 return 0;
2658err_relio:
2659 pci_release_region(pdev, 2);
Jiri Slaby45257fa2007-02-10 01:45:20 -08002660err_null:
Jiri Slaby2094e752006-12-08 02:38:33 -08002661 brd->info = NULL;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002662err:
2663 return retval;
Jiri Slabyfb0c9292006-12-13 00:34:19 -08002664#else
2665 return -ENODEV;
2666#endif
Jiri Slaby037ad482006-12-08 02:38:11 -08002667}
2668
Jiri Slaby2094e752006-12-08 02:38:33 -08002669static void __devexit mxser_remove(struct pci_dev *pdev)
2670{
2671 struct mxser_board *brd = pci_get_drvdata(pdev);
2672 unsigned int i;
2673
2674 for (i = 0; i < brd->info->nports; i++)
2675 tty_unregister_device(mxvar_sdriver, brd->idx + i);
2676
2677 mxser_release_res(brd, pdev, 1);
Jiri Slaby45257fa2007-02-10 01:45:20 -08002678 brd->info = NULL;
Jiri Slaby2094e752006-12-08 02:38:33 -08002679}
2680
2681static struct pci_driver mxser_driver = {
2682 .name = "mxser",
2683 .id_table = mxser_pcibrds,
2684 .probe = mxser_probe,
2685 .remove = __devexit_p(mxser_remove)
2686};
2687
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002688static int __init mxser_module_init(void)
Jiri Slaby037ad482006-12-08 02:38:11 -08002689{
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002690 struct mxser_board *brd;
Jiri Slabyeae44362006-12-08 02:38:27 -08002691 unsigned long cap;
2692 unsigned int i, m, isaloop;
Jiri Slaby2094e752006-12-08 02:38:33 -08002693 int retval, b;
Jiri Slaby037ad482006-12-08 02:38:11 -08002694
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002695 pr_debug("Loading module mxser ...\n");
Jiri Slaby037ad482006-12-08 02:38:11 -08002696
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002697 mxvar_sdriver = alloc_tty_driver(MXSER_PORTS + 1);
2698 if (!mxvar_sdriver)
2699 return -ENOMEM;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002700
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002701 printk(KERN_INFO "MOXA Smartio/Industio family driver version %s\n",
2702 MXSER_VERSION);
2703
2704 /* Initialize the tty_driver structure */
Jiri Slaby31f87cf2006-12-29 16:47:34 -08002705 mxvar_sdriver->owner = THIS_MODULE;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002706 mxvar_sdriver->magic = TTY_DRIVER_MAGIC;
Jiri Slabye9307902006-12-08 02:38:35 -08002707 mxvar_sdriver->name = "ttyMI";
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002708 mxvar_sdriver->major = ttymajor;
2709 mxvar_sdriver->minor_start = 0;
2710 mxvar_sdriver->num = MXSER_PORTS + 1;
2711 mxvar_sdriver->type = TTY_DRIVER_TYPE_SERIAL;
2712 mxvar_sdriver->subtype = SERIAL_TYPE_NORMAL;
2713 mxvar_sdriver->init_termios = tty_std_termios;
2714 mxvar_sdriver->init_termios.c_cflag = B9600|CS8|CREAD|HUPCL|CLOCAL;
Jiri Slaby938ef182006-12-08 02:38:28 -08002715 mxvar_sdriver->flags = TTY_DRIVER_REAL_RAW|TTY_DRIVER_DYNAMIC_DEV;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002716 tty_set_operations(mxvar_sdriver, &mxser_ops);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002717
Jiri Slaby938ef182006-12-08 02:38:28 -08002718 retval = tty_register_driver(mxvar_sdriver);
2719 if (retval) {
2720 printk(KERN_ERR "Couldn't install MOXA Smartio/Industio family "
2721 "tty driver !\n");
2722 goto err_put;
2723 }
2724
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002725 mxvar_diagflag = 0;
2726
2727 m = 0;
2728 /* Start finding ISA boards here */
Jiri Slabyeae44362006-12-08 02:38:27 -08002729 for (isaloop = 0; isaloop < 2; isaloop++)
2730 for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
2731 if (!isaloop)
2732 cap = mxserBoardCAP[b]; /* predefined */
2733 else
2734 cap = ioaddr[b]; /* module param */
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002735
Jiri Slabyeae44362006-12-08 02:38:27 -08002736 if (!cap)
2737 continue;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002738
Jiri Slabyeae44362006-12-08 02:38:27 -08002739 brd = &mxser_boards[m];
2740 retval = mxser_get_ISA_conf(cap, brd);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002741
Jiri Slabyeae44362006-12-08 02:38:27 -08002742 if (retval != 0)
2743 printk(KERN_INFO "Found MOXA %s board "
2744 "(CAP=0x%x)\n",
Jiri Slabycd7ed642006-12-08 02:38:29 -08002745 brd->info->name, ioaddr[b]);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002746
Jiri Slabyeae44362006-12-08 02:38:27 -08002747 if (retval <= 0) {
2748 if (retval == MXSER_ERR_IRQ)
2749 printk(KERN_ERR "Invalid interrupt "
2750 "number, board not "
2751 "configured\n");
2752 else if (retval == MXSER_ERR_IRQ_CONFLIT)
2753 printk(KERN_ERR "Invalid interrupt "
2754 "number, board not "
2755 "configured\n");
2756 else if (retval == MXSER_ERR_VECTOR)
2757 printk(KERN_ERR "Invalid interrupt "
2758 "vector, board not "
2759 "configured\n");
2760 else if (retval == MXSER_ERR_IOADDR)
2761 printk(KERN_ERR "Invalid I/O address, "
2762 "board not configured\n");
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002763
Jiri Slaby2094e752006-12-08 02:38:33 -08002764 brd->info = NULL;
Jiri Slabyeae44362006-12-08 02:38:27 -08002765 continue;
2766 }
2767
Jiri Slabyeae44362006-12-08 02:38:27 -08002768 /* mxser_initbrd will hook ISR. */
Jiri Slaby2094e752006-12-08 02:38:33 -08002769 if (mxser_initbrd(brd, NULL) < 0) {
2770 brd->info = NULL;
Jiri Slabyeae44362006-12-08 02:38:27 -08002771 continue;
Jiri Slaby2094e752006-12-08 02:38:33 -08002772 }
Jiri Slabyeae44362006-12-08 02:38:27 -08002773
Jiri Slaby2094e752006-12-08 02:38:33 -08002774 brd->idx = m * MXSER_PORTS_PER_BOARD;
Jiri Slabycd7ed642006-12-08 02:38:29 -08002775 for (i = 0; i < brd->info->nports; i++)
Jiri Slaby2094e752006-12-08 02:38:33 -08002776 tty_register_device(mxvar_sdriver, brd->idx + i,
2777 NULL);
Jiri Slaby938ef182006-12-08 02:38:28 -08002778
Jiri Slabyeae44362006-12-08 02:38:27 -08002779 m++;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002780 }
2781
Jiri Slaby2094e752006-12-08 02:38:33 -08002782 retval = pci_register_driver(&mxser_driver);
2783 if (retval) {
2784 printk(KERN_ERR "Can't register pci driver\n");
2785 if (!m) {
2786 retval = -ENODEV;
2787 goto err_unr;
2788 } /* else: we have some ISA cards under control */
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002789 }
2790
2791 pr_debug("Done.\n");
2792
Jiri Slaby938ef182006-12-08 02:38:28 -08002793 return 0;
2794err_unr:
2795 tty_unregister_driver(mxvar_sdriver);
2796err_put:
2797 put_tty_driver(mxvar_sdriver);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002798 return retval;
Jiri Slaby037ad482006-12-08 02:38:11 -08002799}
2800
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002801static void __exit mxser_module_exit(void)
Jiri Slaby037ad482006-12-08 02:38:11 -08002802{
Jiri Slaby2094e752006-12-08 02:38:33 -08002803 unsigned int i, j;
Jiri Slaby037ad482006-12-08 02:38:11 -08002804
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002805 pr_debug("Unloading module mxser ...\n");
2806
Jiri Slaby2094e752006-12-08 02:38:33 -08002807 pci_unregister_driver(&mxser_driver);
2808
2809 for (i = 0; i < MXSER_BOARDS; i++) /* ISA remains */
2810 if (mxser_boards[i].info != NULL)
2811 for (j = 0; j < mxser_boards[i].info->nports; j++)
2812 tty_unregister_device(mxvar_sdriver,
2813 mxser_boards[i].idx + j);
Jiri Slabyead568c2006-12-08 02:38:26 -08002814 tty_unregister_driver(mxvar_sdriver);
2815 put_tty_driver(mxvar_sdriver);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002816
Jiri Slaby171d3a82006-12-08 02:38:25 -08002817 for (i = 0; i < MXSER_BOARDS; i++)
Jiri Slabycd7ed642006-12-08 02:38:29 -08002818 if (mxser_boards[i].info != NULL)
Jiri Slaby2094e752006-12-08 02:38:33 -08002819 mxser_release_res(&mxser_boards[i], NULL, 1);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002820
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002821 pr_debug("Done.\n");
Jiri Slaby037ad482006-12-08 02:38:11 -08002822}
2823
2824module_init(mxser_module_init);
2825module_exit(mxser_module_exit);