blob: 7677df3156790c9630485d2a199ad524273c4585 [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 Slabyf8cce2e2007-10-18 03:06:01 -07005 * Copyright (C) 2006-2007 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 Slabyf8cce2e2007-10-18 03:06:01 -070051#define MXSER_VERSION "2.0.2" /* 1.10 */
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
Jiri Slabyf8cce2e2007-10-18 03:06:01 -070075#define PCI_DEVICE_ID_CB108 0x1080
76#define PCI_DEVICE_ID_CB114 0x1142
77#define PCI_DEVICE_ID_CB134I 0x1341
78#define PCI_DEVICE_ID_CP138U 0x1380
79#define PCI_DEVICE_ID_POS104UL 0x1044
80
Jiri Slaby037ad482006-12-08 02:38:11 -080081
Jiri Slaby037ad482006-12-08 02:38:11 -080082#define C168_ASIC_ID 1
83#define C104_ASIC_ID 2
84#define C102_ASIC_ID 0xB
85#define CI132_ASIC_ID 4
86#define CI134_ASIC_ID 3
87#define CI104J_ASIC_ID 5
88
Jiri Slabycd7ed642006-12-08 02:38:29 -080089#define MXSER_HIGHBAUD 1
90#define MXSER_HAS2 2
Jiri Slaby037ad482006-12-08 02:38:11 -080091
92/* This is only for PCI */
Jiri Slabycd7ed642006-12-08 02:38:29 -080093static const struct {
Jiri Slaby037ad482006-12-08 02:38:11 -080094 int type;
95 int tx_fifo;
96 int rx_fifo;
97 int xmit_fifo_size;
98 int rx_high_water;
99 int rx_trigger;
100 int rx_low_water;
101 long max_baud;
Jiri Slabycd7ed642006-12-08 02:38:29 -0800102} Gpci_uart_info[] = {
Jiri Slaby037ad482006-12-08 02:38:11 -0800103 {MOXA_OTHER_UART, 16, 16, 16, 14, 14, 1, 921600L},
104 {MOXA_MUST_MU150_HWID, 64, 64, 64, 48, 48, 16, 230400L},
105 {MOXA_MUST_MU860_HWID, 128, 128, 128, 96, 96, 32, 921600L}
106};
Jiri Slabycd7ed642006-12-08 02:38:29 -0800107#define UART_INFO_NUM ARRAY_SIZE(Gpci_uart_info)
Jiri Slaby037ad482006-12-08 02:38:11 -0800108
Jiri Slabycd7ed642006-12-08 02:38:29 -0800109struct mxser_cardinfo {
110 unsigned int nports;
111 char *name;
112 unsigned int flags;
113};
114
115static const struct mxser_cardinfo mxser_cards[] = {
Jiri Slabyf8cce2e2007-10-18 03:06:01 -0700116/* 0*/ { 8, "C168 series", }, /* C168-ISA */
Jiri Slabycd7ed642006-12-08 02:38:29 -0800117 { 4, "C104 series", }, /* C104-ISA */
118 { 4, "CI-104J series", }, /* CI104J */
119 { 8, "C168H/PCI series", }, /* C168-PCI */
120 { 4, "C104H/PCI series", }, /* C104-PCI */
Jiri Slabyf8cce2e2007-10-18 03:06:01 -0700121/* 5*/ { 4, "C102 series", MXSER_HAS2 }, /* C102-ISA */
Jiri Slabycd7ed642006-12-08 02:38:29 -0800122 { 4, "CI-132 series", MXSER_HAS2 }, /* CI132 */
123 { 4, "CI-134 series", }, /* CI134 */
124 { 2, "CP-132 series", }, /* CP132 */
125 { 4, "CP-114 series", }, /* CP114 */
Jiri Slabyf8cce2e2007-10-18 03:06:01 -0700126/*10*/ { 4, "CT-114 series", }, /* CT114 */
Jiri Slabycd7ed642006-12-08 02:38:29 -0800127 { 2, "CP-102 series", MXSER_HIGHBAUD }, /* CP102 */
128 { 4, "CP-104U series", }, /* CP104U */
129 { 8, "CP-168U series", }, /* CP168U */
130 { 2, "CP-132U series", }, /* CP132U */
Jiri Slabyf8cce2e2007-10-18 03:06:01 -0700131/*15*/ { 4, "CP-134U series", }, /* CP134U */
Jiri Slabycd7ed642006-12-08 02:38:29 -0800132 { 4, "CP-104JU series", }, /* CP104JU */
133 { 8, "Moxa UC7000 Serial", }, /* RC7000 */
134 { 8, "CP-118U series", }, /* CP118U */
135 { 2, "CP-102UL series", }, /* CP102UL */
Jiri Slabyf8cce2e2007-10-18 03:06:01 -0700136/*20*/ { 2, "CP-102U series", }, /* CP102U */
Jiri Slabycd7ed642006-12-08 02:38:29 -0800137 { 8, "CP-118EL series", }, /* CP118EL */
138 { 8, "CP-168EL series", }, /* CP168EL */
Jiri Slabyf8cce2e2007-10-18 03:06:01 -0700139 { 4, "CP-104EL series", },
140 { 8, "CB-108 series", },
141/*25*/ { 4, "CB-114 series", },
142 { 4, "CB-134I series", },
143 { 8, "CP-138U series", },
144 { 4, "POS-104UL series", }
Jiri Slabycd7ed642006-12-08 02:38:29 -0800145};
146
147/* driver_data correspond to the lines in the structure above
148 see also ISA probe function before you change something */
Jiri Slaby037ad482006-12-08 02:38:11 -0800149static struct pci_device_id mxser_pcibrds[] = {
Jiri Slaby3306ce32006-12-08 02:38:13 -0800150 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C168),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800151 .driver_data = 3 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800152 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C104),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800153 .driver_data = 4 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800154 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP132),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800155 .driver_data = 8 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800156 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP114),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800157 .driver_data = 9 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800158 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CT114),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800159 .driver_data = 10 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800160 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP102),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800161 .driver_data = 11 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800162 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP104U),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800163 .driver_data = 12 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800164 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP168U),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800165 .driver_data = 13 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800166 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP132U),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800167 .driver_data = 14 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800168 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP134U),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800169 .driver_data = 15 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800170 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP104JU),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800171 .driver_data = 16 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800172 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_RC7000),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800173 .driver_data = 17 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800174 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP118U),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800175 .driver_data = 18 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800176 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP102UL),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800177 .driver_data = 19 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800178 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP102U),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800179 .driver_data = 20 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800180 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP118EL),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800181 .driver_data = 21 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800182 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP168EL),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800183 .driver_data = 22 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800184 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP104EL),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800185 .driver_data = 23 },
Jiri Slabyf8cce2e2007-10-18 03:06:01 -0700186 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB108), .driver_data = 24 },
187 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB114), .driver_data = 25 },
188 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB134I), .driver_data = 26 },
189 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP138U), .driver_data = 27 },
190 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_POS104UL), .driver_data = 28 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800191 { }
Jiri Slaby037ad482006-12-08 02:38:11 -0800192};
Jiri Slaby037ad482006-12-08 02:38:11 -0800193MODULE_DEVICE_TABLE(pci, mxser_pcibrds);
194
Jiri Slabyf64c84a2007-02-10 01:45:21 -0800195static int mxvar_baud_table[] = {
196 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400,
197 4800, 9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600
198};
199static unsigned int mxvar_baud_table1[] = {
200 0, B50, B75, B110, B134, B150, B200, B300, B600, B1200, B1800, B2400,
201 B4800, B9600, B19200, B38400, B57600, B115200, B230400, B460800, B921600
202};
203#define BAUD_TABLE_NO ARRAY_SIZE(mxvar_baud_table)
204
205#define B_SPEC B2000000
206
Jiri Slaby037ad482006-12-08 02:38:11 -0800207static int ioaddr[MXSER_BOARDS] = { 0, 0, 0, 0 };
208static int ttymajor = MXSERMAJOR;
209static int calloutmajor = MXSERCUMAJOR;
Jiri Slaby037ad482006-12-08 02:38:11 -0800210
211/* Variables for insmod */
212
213MODULE_AUTHOR("Casper Yang");
214MODULE_DESCRIPTION("MOXA Smartio/Industio Family Multiport Board Device Driver");
215module_param_array(ioaddr, int, NULL, 0);
216module_param(ttymajor, int, 0);
Jiri Slaby037ad482006-12-08 02:38:11 -0800217MODULE_LICENSE("GPL");
218
219struct mxser_log {
220 int tick;
221 unsigned long rxcnt[MXSER_PORTS];
222 unsigned long txcnt[MXSER_PORTS];
223};
224
225
226struct mxser_mon {
227 unsigned long rxcnt;
228 unsigned long txcnt;
229 unsigned long up_rxcnt;
230 unsigned long up_txcnt;
231 int modem_status;
232 unsigned char hold_reason;
233};
234
235struct mxser_mon_ext {
236 unsigned long rx_cnt[32];
237 unsigned long tx_cnt[32];
238 unsigned long up_rxcnt[32];
239 unsigned long up_txcnt[32];
240 int modem_status[32];
241
242 long baudrate[32];
243 int databits[32];
244 int stopbits[32];
245 int parity[32];
246 int flowctrl[32];
247 int fifo[32];
248 int iftype[32];
249};
250
Jiri Slaby55b307d2006-12-08 02:38:14 -0800251struct mxser_board;
Jiri Slaby037ad482006-12-08 02:38:11 -0800252
Jiri Slaby55b307d2006-12-08 02:38:14 -0800253struct mxser_port {
254 struct mxser_board *board;
255 struct tty_struct *tty;
256
257 unsigned long ioaddr;
258 unsigned long opmode_ioaddr;
259 int max_baud;
260
Jiri Slaby037ad482006-12-08 02:38:11 -0800261 int rx_high_water;
262 int rx_trigger; /* Rx fifo trigger level */
263 int rx_low_water;
264 int baud_base; /* max. speed */
Jiri Slaby55b307d2006-12-08 02:38:14 -0800265 long realbaud;
Jiri Slaby037ad482006-12-08 02:38:11 -0800266 int type; /* UART type */
Jiri Slaby55b307d2006-12-08 02:38:14 -0800267 int flags; /* defined in tty.h */
Jiri Slabyf64c84a2007-02-10 01:45:21 -0800268 int speed;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800269
270 int x_char; /* xon/xoff character */
271 int IER; /* Interrupt Enable Register */
272 int MCR; /* Modem control register */
273
274 unsigned char stop_rx;
275 unsigned char ldisc_stop_rx;
276
277 int custom_divisor;
278 int close_delay;
279 unsigned short closing_wait;
280 unsigned char err_shadow;
281 unsigned long event;
282
283 int count; /* # of fd on device */
284 int blocked_open; /* # of blocked opens */
285 struct async_icount icount; /* kernel counters for 4 input interrupts */
286 int timeout;
287
Jiri Slaby037ad482006-12-08 02:38:11 -0800288 int read_status_mask;
289 int ignore_status_mask;
290 int xmit_fifo_size;
Jiri Slaby037ad482006-12-08 02:38:11 -0800291 unsigned char *xmit_buf;
292 int xmit_head;
293 int xmit_tail;
294 int xmit_cnt;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800295
Alan Cox606d0992006-12-08 02:38:45 -0800296 struct ktermios normal_termios;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800297
298 struct mxser_mon mon_data;
299
300 spinlock_t slock;
Jiri Slaby037ad482006-12-08 02:38:11 -0800301 wait_queue_head_t open_wait;
Jiri Slaby037ad482006-12-08 02:38:11 -0800302 wait_queue_head_t delta_msr_wait;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800303};
304
305struct mxser_board {
Jiri Slaby2094e752006-12-08 02:38:33 -0800306 unsigned int idx;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800307 int irq;
Jiri Slabycd7ed642006-12-08 02:38:29 -0800308 const struct mxser_cardinfo *info;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800309 unsigned long vector;
310 unsigned long vector_mask;
311
312 int chip_flag;
313 int uart_type;
314
315 struct mxser_port ports[MXSER_PORTS_PER_BOARD];
Jiri Slaby037ad482006-12-08 02:38:11 -0800316};
317
318struct mxser_mstatus {
319 tcflag_t cflag;
320 int cts;
321 int dsr;
322 int ri;
323 int dcd;
324};
325
326static struct mxser_mstatus GMStatus[MXSER_PORTS];
327
328static int mxserBoardCAP[MXSER_BOARDS] = {
329 0, 0, 0, 0
330 /* 0x180, 0x280, 0x200, 0x320 */
331};
332
Jiri Slaby55b307d2006-12-08 02:38:14 -0800333static struct mxser_board mxser_boards[MXSER_BOARDS];
Jiri Slaby037ad482006-12-08 02:38:11 -0800334static struct tty_driver *mxvar_sdriver;
Jiri Slaby037ad482006-12-08 02:38:11 -0800335static struct mxser_log mxvar_log;
336static int mxvar_diagflag;
337static unsigned char mxser_msr[MXSER_PORTS + 1];
338static struct mxser_mon_ext mon_data_ext;
339static int mxser_set_baud_method[MXSER_PORTS + 1];
Jiri Slaby037ad482006-12-08 02:38:11 -0800340
Jiri Slabyfb0c9292006-12-13 00:34:19 -0800341#ifdef CONFIG_PCI
Jiri Slabyc76b0932007-02-10 01:45:07 -0800342static int __devinit CheckIsMoxaMust(int io)
Jiri Slaby037ad482006-12-08 02:38:11 -0800343{
344 u8 oldmcr, hwid;
345 int i;
346
347 outb(0, io + UART_LCR);
348 DISABLE_MOXA_MUST_ENCHANCE_MODE(io);
349 oldmcr = inb(io + UART_MCR);
350 outb(0, io + UART_MCR);
351 SET_MOXA_MUST_XON1_VALUE(io, 0x11);
352 if ((hwid = inb(io + UART_MCR)) != 0) {
353 outb(oldmcr, io + UART_MCR);
354 return MOXA_OTHER_UART;
355 }
356
357 GET_MOXA_MUST_HARDWARE_ID(io, &hwid);
Jiri Slabycd7ed642006-12-08 02:38:29 -0800358 for (i = 1; i < UART_INFO_NUM; i++) { /* 0 = OTHER_UART */
359 if (hwid == Gpci_uart_info[i].type)
Jiri Slaby037ad482006-12-08 02:38:11 -0800360 return (int)hwid;
361 }
362 return MOXA_OTHER_UART;
363}
Jiri Slabyfb0c9292006-12-13 00:34:19 -0800364#endif
Jiri Slaby037ad482006-12-08 02:38:11 -0800365
Jiri Slaby55b307d2006-12-08 02:38:14 -0800366static void process_txrx_fifo(struct mxser_port *info)
Jiri Slaby037ad482006-12-08 02:38:11 -0800367{
368 int i;
369
370 if ((info->type == PORT_16450) || (info->type == PORT_8250)) {
371 info->rx_trigger = 1;
372 info->rx_high_water = 1;
373 info->rx_low_water = 1;
374 info->xmit_fifo_size = 1;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800375 } else
376 for (i = 0; i < UART_INFO_NUM; i++)
377 if (info->board->chip_flag == Gpci_uart_info[i].type) {
Jiri Slaby037ad482006-12-08 02:38:11 -0800378 info->rx_trigger = Gpci_uart_info[i].rx_trigger;
379 info->rx_low_water = Gpci_uart_info[i].rx_low_water;
380 info->rx_high_water = Gpci_uart_info[i].rx_high_water;
381 info->xmit_fifo_size = Gpci_uart_info[i].xmit_fifo_size;
382 break;
383 }
Jiri Slaby037ad482006-12-08 02:38:11 -0800384}
385
Jiri Slaby55b307d2006-12-08 02:38:14 -0800386static unsigned char mxser_get_msr(int baseaddr, int mode, int port)
Jiri Slaby037ad482006-12-08 02:38:11 -0800387{
388 unsigned char status = 0;
389
390 status = inb(baseaddr + UART_MSR);
391
392 mxser_msr[port] &= 0x0F;
393 mxser_msr[port] |= status;
394 status = mxser_msr[port];
395 if (mode)
396 mxser_msr[port] = 0;
397
398 return status;
399}
400
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800401static int mxser_block_til_ready(struct tty_struct *tty, struct file *filp,
402 struct mxser_port *port)
403{
404 DECLARE_WAITQUEUE(wait, current);
405 int retval;
406 int do_clocal = 0;
407 unsigned long flags;
408
409 /*
410 * If non-blocking mode is set, or the port is not enabled,
411 * then make the check up front and then exit.
412 */
Jiri Slaby214efeb2006-12-08 02:38:25 -0800413 if ((filp->f_flags & O_NONBLOCK) ||
414 test_bit(TTY_IO_ERROR, &tty->flags)) {
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800415 port->flags |= ASYNC_NORMAL_ACTIVE;
416 return 0;
417 }
418
419 if (tty->termios->c_cflag & CLOCAL)
420 do_clocal = 1;
421
422 /*
423 * Block waiting for the carrier detect and the line to become
424 * free (i.e., not in use by the callout). While we are in
425 * this loop, port->count is dropped by one, so that
426 * mxser_close() knows when to free things. We restore it upon
427 * exit, either normal or abnormal.
428 */
429 retval = 0;
430 add_wait_queue(&port->open_wait, &wait);
431
432 spin_lock_irqsave(&port->slock, flags);
433 if (!tty_hung_up_p(filp))
434 port->count--;
435 spin_unlock_irqrestore(&port->slock, flags);
436 port->blocked_open++;
437 while (1) {
438 spin_lock_irqsave(&port->slock, flags);
439 outb(inb(port->ioaddr + UART_MCR) |
440 UART_MCR_DTR | UART_MCR_RTS, port->ioaddr + UART_MCR);
441 spin_unlock_irqrestore(&port->slock, flags);
442 set_current_state(TASK_INTERRUPTIBLE);
443 if (tty_hung_up_p(filp) || !(port->flags & ASYNC_INITIALIZED)) {
444 if (port->flags & ASYNC_HUP_NOTIFY)
445 retval = -EAGAIN;
446 else
447 retval = -ERESTARTSYS;
448 break;
449 }
450 if (!(port->flags & ASYNC_CLOSING) &&
451 (do_clocal ||
452 (inb(port->ioaddr + UART_MSR) & UART_MSR_DCD)))
453 break;
454 if (signal_pending(current)) {
455 retval = -ERESTARTSYS;
456 break;
457 }
458 schedule();
459 }
460 set_current_state(TASK_RUNNING);
461 remove_wait_queue(&port->open_wait, &wait);
462 if (!tty_hung_up_p(filp))
463 port->count++;
464 port->blocked_open--;
465 if (retval)
466 return retval;
467 port->flags |= ASYNC_NORMAL_ACTIVE;
468 return 0;
469}
470
471static int mxser_set_baud(struct mxser_port *info, long newspd)
472{
Jiri Slabyf64c84a2007-02-10 01:45:21 -0800473 unsigned int i;
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800474 int quot = 0;
475 unsigned char cval;
476 int ret = 0;
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800477
478 if (!info->tty || !info->tty->termios)
479 return ret;
480
481 if (!(info->ioaddr))
482 return ret;
483
484 if (newspd > info->max_baud)
485 return 0;
486
487 info->realbaud = newspd;
Jiri Slabyf64c84a2007-02-10 01:45:21 -0800488 for (i = 0; i < BAUD_TABLE_NO; i++)
489 if (newspd == mxvar_baud_table[i])
490 break;
491 if (i == BAUD_TABLE_NO) {
492 quot = info->baud_base / info->speed;
493 if (info->speed <= 0 || info->speed > info->max_baud)
494 quot = 0;
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800495 } else {
Jiri Slabyf64c84a2007-02-10 01:45:21 -0800496 if (newspd == 134) {
497 quot = (2 * info->baud_base / 269);
498 } else if (newspd) {
499 quot = info->baud_base / newspd;
500 if (quot == 0)
501 quot = 1;
502 } else {
503 quot = 0;
504 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800505 }
506
507 info->timeout = ((info->xmit_fifo_size * HZ * 10 * quot) / info->baud_base);
508 info->timeout += HZ / 50; /* Add .02 seconds of slop */
509
510 if (quot) {
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800511 info->MCR |= UART_MCR_DTR;
512 outb(info->MCR, info->ioaddr + UART_MCR);
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800513 } else {
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800514 info->MCR &= ~UART_MCR_DTR;
515 outb(info->MCR, info->ioaddr + UART_MCR);
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800516 return ret;
517 }
518
519 cval = inb(info->ioaddr + UART_LCR);
520
521 outb(cval | UART_LCR_DLAB, info->ioaddr + UART_LCR); /* set DLAB */
522
523 outb(quot & 0xff, info->ioaddr + UART_DLL); /* LS of divisor */
524 outb(quot >> 8, info->ioaddr + UART_DLM); /* MS of divisor */
525 outb(cval, info->ioaddr + UART_LCR); /* reset DLAB */
526
Jiri Slabyf64c84a2007-02-10 01:45:21 -0800527 if (i == BAUD_TABLE_NO) {
528 quot = info->baud_base % info->speed;
529 quot *= 8;
530 if ((quot % info->speed) > (info->speed / 2)) {
531 quot /= info->speed;
532 quot++;
533 } else {
534 quot /= info->speed;
535 }
536 SET_MOXA_MUST_ENUM_VALUE(info->ioaddr, quot);
537 } else
538 SET_MOXA_MUST_ENUM_VALUE(info->ioaddr, 0);
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800539
540 return ret;
541}
542
543/*
544 * This routine is called to set the UART divisor registers to match
545 * the specified baud rate for a serial port.
546 */
547static int mxser_change_speed(struct mxser_port *info,
Alan Cox606d0992006-12-08 02:38:45 -0800548 struct ktermios *old_termios)
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800549{
550 unsigned cflag, cval, fcr;
551 int ret = 0;
552 unsigned char status;
553 long baud;
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800554
555 if (!info->tty || !info->tty->termios)
556 return ret;
557 cflag = info->tty->termios->c_cflag;
558 if (!(info->ioaddr))
559 return ret;
560
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800561 if (mxser_set_baud_method[info->tty->index] == 0) {
Jiri Slabyf64c84a2007-02-10 01:45:21 -0800562 if ((cflag & CBAUD) == B_SPEC)
563 baud = info->speed;
564 else
565 baud = tty_get_baud_rate(info->tty);
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800566 mxser_set_baud(info, baud);
567 }
568
569 /* byte size and parity */
570 switch (cflag & CSIZE) {
571 case CS5:
572 cval = 0x00;
573 break;
574 case CS6:
575 cval = 0x01;
576 break;
577 case CS7:
578 cval = 0x02;
579 break;
580 case CS8:
581 cval = 0x03;
582 break;
583 default:
584 cval = 0x00;
585 break; /* too keep GCC shut... */
586 }
587 if (cflag & CSTOPB)
588 cval |= 0x04;
589 if (cflag & PARENB)
590 cval |= UART_LCR_PARITY;
591 if (!(cflag & PARODD))
592 cval |= UART_LCR_EPAR;
593 if (cflag & CMSPAR)
594 cval |= UART_LCR_SPAR;
595
596 if ((info->type == PORT_8250) || (info->type == PORT_16450)) {
597 if (info->board->chip_flag) {
598 fcr = UART_FCR_ENABLE_FIFO;
599 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
600 SET_MOXA_MUST_FIFO_VALUE(info);
601 } else
602 fcr = 0;
603 } else {
604 fcr = UART_FCR_ENABLE_FIFO;
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800605 if (info->board->chip_flag) {
606 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
607 SET_MOXA_MUST_FIFO_VALUE(info);
608 } else {
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800609 switch (info->rx_trigger) {
610 case 1:
611 fcr |= UART_FCR_TRIGGER_1;
612 break;
613 case 4:
614 fcr |= UART_FCR_TRIGGER_4;
615 break;
616 case 8:
617 fcr |= UART_FCR_TRIGGER_8;
618 break;
619 default:
620 fcr |= UART_FCR_TRIGGER_14;
621 break;
622 }
623 }
624 }
625
626 /* CTS flow control flag and modem status interrupts */
627 info->IER &= ~UART_IER_MSI;
628 info->MCR &= ~UART_MCR_AFE;
629 if (cflag & CRTSCTS) {
630 info->flags |= ASYNC_CTS_FLOW;
631 info->IER |= UART_IER_MSI;
632 if ((info->type == PORT_16550A) || (info->board->chip_flag)) {
633 info->MCR |= UART_MCR_AFE;
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800634 } else {
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800635 status = inb(info->ioaddr + UART_MSR);
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800636 if (info->tty->hw_stopped) {
637 if (status & UART_MSR_CTS) {
638 info->tty->hw_stopped = 0;
639 if (info->type != PORT_16550A &&
640 !info->board->chip_flag) {
641 outb(info->IER & ~UART_IER_THRI,
642 info->ioaddr +
643 UART_IER);
644 info->IER |= UART_IER_THRI;
645 outb(info->IER, info->ioaddr +
646 UART_IER);
647 }
Jiri Slaby47c85c02007-02-10 01:45:15 -0800648 tty_wakeup(info->tty);
649 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800650 } else {
651 if (!(status & UART_MSR_CTS)) {
652 info->tty->hw_stopped = 1;
653 if ((info->type != PORT_16550A) &&
654 (!info->board->chip_flag)) {
655 info->IER &= ~UART_IER_THRI;
656 outb(info->IER, info->ioaddr +
657 UART_IER);
658 }
659 }
660 }
661 }
662 } else {
663 info->flags &= ~ASYNC_CTS_FLOW;
664 }
665 outb(info->MCR, info->ioaddr + UART_MCR);
666 if (cflag & CLOCAL) {
667 info->flags &= ~ASYNC_CHECK_CD;
668 } else {
669 info->flags |= ASYNC_CHECK_CD;
670 info->IER |= UART_IER_MSI;
671 }
672 outb(info->IER, info->ioaddr + UART_IER);
673
674 /*
675 * Set up parity check flag
676 */
677 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
678 if (I_INPCK(info->tty))
679 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
680 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
681 info->read_status_mask |= UART_LSR_BI;
682
683 info->ignore_status_mask = 0;
684
685 if (I_IGNBRK(info->tty)) {
686 info->ignore_status_mask |= UART_LSR_BI;
687 info->read_status_mask |= UART_LSR_BI;
688 /*
689 * If we're ignore parity and break indicators, ignore
690 * overruns too. (For real raw support).
691 */
692 if (I_IGNPAR(info->tty)) {
693 info->ignore_status_mask |=
694 UART_LSR_OE |
695 UART_LSR_PE |
696 UART_LSR_FE;
697 info->read_status_mask |=
698 UART_LSR_OE |
699 UART_LSR_PE |
700 UART_LSR_FE;
701 }
702 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800703 if (info->board->chip_flag) {
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800704 SET_MOXA_MUST_XON1_VALUE(info->ioaddr, START_CHAR(info->tty));
705 SET_MOXA_MUST_XOFF1_VALUE(info->ioaddr, STOP_CHAR(info->tty));
706 if (I_IXON(info->tty)) {
707 ENABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
708 } else {
709 DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
710 }
711 if (I_IXOFF(info->tty)) {
712 ENABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
713 } else {
714 DISABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
715 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800716 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800717
718
719 outb(fcr, info->ioaddr + UART_FCR); /* set fcr */
720 outb(cval, info->ioaddr + UART_LCR);
721
722 return ret;
723}
724
725static void mxser_check_modem_status(struct mxser_port *port, int status)
726{
727 /* update input line counters */
728 if (status & UART_MSR_TERI)
729 port->icount.rng++;
730 if (status & UART_MSR_DDSR)
731 port->icount.dsr++;
732 if (status & UART_MSR_DDCD)
733 port->icount.dcd++;
734 if (status & UART_MSR_DCTS)
735 port->icount.cts++;
736 port->mon_data.modem_status = status;
737 wake_up_interruptible(&port->delta_msr_wait);
738
739 if ((port->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
740 if (status & UART_MSR_DCD)
741 wake_up_interruptible(&port->open_wait);
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800742 }
743
744 if (port->flags & ASYNC_CTS_FLOW) {
745 if (port->tty->hw_stopped) {
746 if (status & UART_MSR_CTS) {
747 port->tty->hw_stopped = 0;
748
749 if ((port->type != PORT_16550A) &&
750 (!port->board->chip_flag)) {
751 outb(port->IER & ~UART_IER_THRI,
752 port->ioaddr + UART_IER);
753 port->IER |= UART_IER_THRI;
754 outb(port->IER, port->ioaddr +
755 UART_IER);
756 }
Jiri Slaby47c85c02007-02-10 01:45:15 -0800757 tty_wakeup(port->tty);
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800758 }
759 } else {
760 if (!(status & UART_MSR_CTS)) {
761 port->tty->hw_stopped = 1;
762 if (port->type != PORT_16550A &&
763 !port->board->chip_flag) {
764 port->IER &= ~UART_IER_THRI;
765 outb(port->IER, port->ioaddr +
766 UART_IER);
767 }
768 }
769 }
770 }
771}
772
773static int mxser_startup(struct mxser_port *info)
774{
775 unsigned long page;
776 unsigned long flags;
777
778 page = __get_free_page(GFP_KERNEL);
779 if (!page)
780 return -ENOMEM;
781
782 spin_lock_irqsave(&info->slock, flags);
783
784 if (info->flags & ASYNC_INITIALIZED) {
785 free_page(page);
786 spin_unlock_irqrestore(&info->slock, flags);
787 return 0;
788 }
789
790 if (!info->ioaddr || !info->type) {
791 if (info->tty)
792 set_bit(TTY_IO_ERROR, &info->tty->flags);
793 free_page(page);
794 spin_unlock_irqrestore(&info->slock, flags);
795 return 0;
796 }
797 if (info->xmit_buf)
798 free_page(page);
799 else
800 info->xmit_buf = (unsigned char *) page;
801
802 /*
803 * Clear the FIFO buffers and disable them
804 * (they will be reenabled in mxser_change_speed())
805 */
806 if (info->board->chip_flag)
807 outb((UART_FCR_CLEAR_RCVR |
808 UART_FCR_CLEAR_XMIT |
809 MOXA_MUST_FCR_GDA_MODE_ENABLE), info->ioaddr + UART_FCR);
810 else
811 outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
812 info->ioaddr + UART_FCR);
813
814 /*
815 * At this point there's no way the LSR could still be 0xFF;
816 * if it is, then bail out, because there's likely no UART
817 * here.
818 */
819 if (inb(info->ioaddr + UART_LSR) == 0xff) {
820 spin_unlock_irqrestore(&info->slock, flags);
821 if (capable(CAP_SYS_ADMIN)) {
822 if (info->tty)
823 set_bit(TTY_IO_ERROR, &info->tty->flags);
824 return 0;
825 } else
826 return -ENODEV;
827 }
828
829 /*
830 * Clear the interrupt registers.
831 */
832 (void) inb(info->ioaddr + UART_LSR);
833 (void) inb(info->ioaddr + UART_RX);
834 (void) inb(info->ioaddr + UART_IIR);
835 (void) inb(info->ioaddr + UART_MSR);
836
837 /*
838 * Now, initialize the UART
839 */
840 outb(UART_LCR_WLEN8, info->ioaddr + UART_LCR); /* reset DLAB */
841 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
842 outb(info->MCR, info->ioaddr + UART_MCR);
843
844 /*
845 * Finally, enable interrupts
846 */
847 info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800848
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800849 if (info->board->chip_flag)
850 info->IER |= MOXA_MUST_IER_EGDAI;
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800851 outb(info->IER, info->ioaddr + UART_IER); /* enable interrupts */
852
853 /*
854 * And clear the interrupt registers again for luck.
855 */
856 (void) inb(info->ioaddr + UART_LSR);
857 (void) inb(info->ioaddr + UART_RX);
858 (void) inb(info->ioaddr + UART_IIR);
859 (void) inb(info->ioaddr + UART_MSR);
860
861 if (info->tty)
862 clear_bit(TTY_IO_ERROR, &info->tty->flags);
863 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
864
865 /*
866 * and set the speed of the serial port
867 */
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800868 mxser_change_speed(info, NULL);
Jiri Slaby5ff72052007-02-10 01:45:23 -0800869 info->flags |= ASYNC_INITIALIZED;
Jiri Slabyf64c84a2007-02-10 01:45:21 -0800870 spin_unlock_irqrestore(&info->slock, flags);
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800871
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800872 return 0;
873}
874
875/*
876 * This routine will shutdown a serial port; interrupts maybe disabled, and
877 * DTR is dropped if the hangup on close termio flag is on.
878 */
879static void mxser_shutdown(struct mxser_port *info)
880{
881 unsigned long flags;
882
883 if (!(info->flags & ASYNC_INITIALIZED))
884 return;
885
886 spin_lock_irqsave(&info->slock, flags);
887
888 /*
889 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
890 * here so the queue might never be waken up
891 */
892 wake_up_interruptible(&info->delta_msr_wait);
893
894 /*
895 * Free the IRQ, if necessary
896 */
897 if (info->xmit_buf) {
898 free_page((unsigned long) info->xmit_buf);
899 info->xmit_buf = NULL;
900 }
901
902 info->IER = 0;
903 outb(0x00, info->ioaddr + UART_IER);
904
905 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
906 info->MCR &= ~(UART_MCR_DTR | UART_MCR_RTS);
907 outb(info->MCR, info->ioaddr + UART_MCR);
908
909 /* clear Rx/Tx FIFO's */
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800910 if (info->board->chip_flag)
911 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT |
912 MOXA_MUST_FCR_GDA_MODE_ENABLE,
913 info->ioaddr + UART_FCR);
914 else
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800915 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
916 info->ioaddr + UART_FCR);
917
918 /* read data port to reset things */
919 (void) inb(info->ioaddr + UART_RX);
920
921 if (info->tty)
922 set_bit(TTY_IO_ERROR, &info->tty->flags);
923
924 info->flags &= ~ASYNC_INITIALIZED;
925
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800926 if (info->board->chip_flag)
927 SET_MOXA_MUST_NO_SOFTWARE_FLOW_CONTROL(info->ioaddr);
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800928
929 spin_unlock_irqrestore(&info->slock, flags);
930}
931
Jiri Slaby037ad482006-12-08 02:38:11 -0800932/*
933 * This routine is called whenever a serial port is opened. It
934 * enables interrupts for a serial port, linking in its async structure into
935 * the IRQ chain. It also performs the serial-specific
936 * initialization for the tty structure.
937 */
938static int mxser_open(struct tty_struct *tty, struct file *filp)
939{
Jiri Slaby55b307d2006-12-08 02:38:14 -0800940 struct mxser_port *info;
Jiri Slaby5ff72052007-02-10 01:45:23 -0800941 unsigned long flags;
Jiri Slaby037ad482006-12-08 02:38:11 -0800942 int retval, line;
943
Jiri Slaby037ad482006-12-08 02:38:11 -0800944 line = tty->index;
945 if (line == MXSER_PORTS)
946 return 0;
947 if (line < 0 || line > MXSER_PORTS)
948 return -ENODEV;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800949 info = &mxser_boards[line / MXSER_PORTS_PER_BOARD].ports[line % MXSER_PORTS_PER_BOARD];
950 if (!info->ioaddr)
Jiri Slaby037ad482006-12-08 02:38:11 -0800951 return -ENODEV;
952
953 tty->driver_data = info;
954 info->tty = tty;
955 /*
956 * Start up serial port
957 */
Jiri Slaby5ff72052007-02-10 01:45:23 -0800958 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby3306ce32006-12-08 02:38:13 -0800959 info->count++;
Jiri Slaby5ff72052007-02-10 01:45:23 -0800960 spin_unlock_irqrestore(&info->slock, flags);
Jiri Slaby037ad482006-12-08 02:38:11 -0800961 retval = mxser_startup(info);
962 if (retval)
963 return retval;
964
965 retval = mxser_block_til_ready(tty, filp, info);
966 if (retval)
967 return retval;
968
Jiri Slabye079f4952006-12-08 02:38:31 -0800969 /* unmark here for very high baud rate (ex. 921600 bps) used */
Jiri Slaby037ad482006-12-08 02:38:11 -0800970 tty->low_latency = 1;
971 return 0;
972}
973
974/*
975 * This routine is called when the serial port gets closed. First, we
976 * wait for the last remaining data to be sent. Then, we unlink its
977 * async structure from the interrupt chain if necessary, and we free
978 * that IRQ if nothing is left in the chain.
979 */
980static void mxser_close(struct tty_struct *tty, struct file *filp)
981{
Jiri Slaby55b307d2006-12-08 02:38:14 -0800982 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -0800983
984 unsigned long timeout;
985 unsigned long flags;
Jiri Slaby037ad482006-12-08 02:38:11 -0800986
987 if (tty->index == MXSER_PORTS)
988 return;
989 if (!info)
990 return;
991
992 spin_lock_irqsave(&info->slock, flags);
993
994 if (tty_hung_up_p(filp)) {
995 spin_unlock_irqrestore(&info->slock, flags);
996 return;
997 }
998 if ((tty->count == 1) && (info->count != 1)) {
999 /*
1000 * Uh, oh. tty->count is 1, which means that the tty
1001 * structure will be freed. Info->count should always
1002 * be one in these conditions. If it's greater than
1003 * one, we've got real problems, since it means the
1004 * serial port won't be shutdown.
1005 */
1006 printk(KERN_ERR "mxser_close: bad serial port count; "
1007 "tty->count is 1, info->count is %d\n", info->count);
1008 info->count = 1;
1009 }
1010 if (--info->count < 0) {
1011 printk(KERN_ERR "mxser_close: bad serial port count for "
Jiri Slaby55b307d2006-12-08 02:38:14 -08001012 "ttys%d: %d\n", tty->index, info->count);
Jiri Slaby037ad482006-12-08 02:38:11 -08001013 info->count = 0;
1014 }
1015 if (info->count) {
1016 spin_unlock_irqrestore(&info->slock, flags);
1017 return;
1018 }
1019 info->flags |= ASYNC_CLOSING;
1020 spin_unlock_irqrestore(&info->slock, flags);
1021 /*
1022 * Save the termios structure, since this port may have
1023 * separate termios for callout and dialin.
1024 */
1025 if (info->flags & ASYNC_NORMAL_ACTIVE)
1026 info->normal_termios = *tty->termios;
1027 /*
1028 * Now we wait for the transmit buffer to clear; and we notify
1029 * the line discipline to only process XON/XOFF characters.
1030 */
1031 tty->closing = 1;
1032 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1033 tty_wait_until_sent(tty, info->closing_wait);
1034 /*
1035 * At this point we stop accepting input. To do this, we
1036 * disable the receive line status interrupts, and tell the
1037 * interrupt driver to stop checking the data ready bit in the
1038 * line status register.
1039 */
1040 info->IER &= ~UART_IER_RLSI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001041 if (info->board->chip_flag)
Jiri Slaby037ad482006-12-08 02:38:11 -08001042 info->IER &= ~MOXA_MUST_RECV_ISR;
Jiri Slabye079f4952006-12-08 02:38:31 -08001043
Jiri Slaby037ad482006-12-08 02:38:11 -08001044 if (info->flags & ASYNC_INITIALIZED) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08001045 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001046 /*
1047 * Before we drop DTR, make sure the UART transmitter
1048 * has completely drained; this is especially
1049 * important if there is a transmit FIFO!
1050 */
1051 timeout = jiffies + HZ;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001052 while (!(inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001053 schedule_timeout_interruptible(5);
1054 if (time_after(jiffies, timeout))
1055 break;
1056 }
1057 }
1058 mxser_shutdown(info);
1059
1060 if (tty->driver->flush_buffer)
1061 tty->driver->flush_buffer(tty);
1062
Jiri Slaby7e8bcf92006-12-08 02:38:24 -08001063 tty_ldisc_flush(tty);
Jiri Slaby037ad482006-12-08 02:38:11 -08001064
1065 tty->closing = 0;
1066 info->event = 0;
1067 info->tty = NULL;
1068 if (info->blocked_open) {
1069 if (info->close_delay)
1070 schedule_timeout_interruptible(info->close_delay);
1071 wake_up_interruptible(&info->open_wait);
1072 }
1073
1074 info->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
Jiri Slaby037ad482006-12-08 02:38:11 -08001075}
1076
1077static int mxser_write(struct tty_struct *tty, const unsigned char *buf, int count)
1078{
1079 int c, total = 0;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001080 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001081 unsigned long flags;
1082
1083 if (!info->xmit_buf)
1084 return 0;
1085
1086 while (1) {
1087 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1088 SERIAL_XMIT_SIZE - info->xmit_head));
1089 if (c <= 0)
1090 break;
1091
1092 memcpy(info->xmit_buf + info->xmit_head, buf, c);
1093 spin_lock_irqsave(&info->slock, flags);
1094 info->xmit_head = (info->xmit_head + c) &
1095 (SERIAL_XMIT_SIZE - 1);
1096 info->xmit_cnt += c;
1097 spin_unlock_irqrestore(&info->slock, flags);
1098
1099 buf += c;
1100 count -= c;
1101 total += c;
1102 }
1103
Jiri Slabye079f4952006-12-08 02:38:31 -08001104 if (info->xmit_cnt && !tty->stopped) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001105 if (!tty->hw_stopped ||
1106 (info->type == PORT_16550A) ||
Jiri Slaby55b307d2006-12-08 02:38:14 -08001107 (info->board->chip_flag)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001108 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby55b307d2006-12-08 02:38:14 -08001109 outb(info->IER & ~UART_IER_THRI, info->ioaddr +
1110 UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001111 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001112 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001113 spin_unlock_irqrestore(&info->slock, flags);
1114 }
1115 }
1116 return total;
1117}
1118
1119static void mxser_put_char(struct tty_struct *tty, unsigned char ch)
1120{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001121 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001122 unsigned long flags;
1123
1124 if (!info->xmit_buf)
1125 return;
1126
1127 if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1)
1128 return;
1129
1130 spin_lock_irqsave(&info->slock, flags);
1131 info->xmit_buf[info->xmit_head++] = ch;
1132 info->xmit_head &= SERIAL_XMIT_SIZE - 1;
1133 info->xmit_cnt++;
1134 spin_unlock_irqrestore(&info->slock, flags);
Jiri Slabye079f4952006-12-08 02:38:31 -08001135 if (!tty->stopped) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001136 if (!tty->hw_stopped ||
1137 (info->type == PORT_16550A) ||
Jiri Slaby55b307d2006-12-08 02:38:14 -08001138 info->board->chip_flag) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001139 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby55b307d2006-12-08 02:38:14 -08001140 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001141 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001142 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001143 spin_unlock_irqrestore(&info->slock, flags);
1144 }
1145 }
1146}
1147
1148
1149static void mxser_flush_chars(struct tty_struct *tty)
1150{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001151 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001152 unsigned long flags;
1153
1154 if (info->xmit_cnt <= 0 ||
1155 tty->stopped ||
1156 !info->xmit_buf ||
1157 (tty->hw_stopped &&
1158 (info->type != PORT_16550A) &&
Jiri Slaby55b307d2006-12-08 02:38:14 -08001159 (!info->board->chip_flag)
Jiri Slaby037ad482006-12-08 02:38:11 -08001160 ))
1161 return;
1162
1163 spin_lock_irqsave(&info->slock, flags);
1164
Jiri Slaby55b307d2006-12-08 02:38:14 -08001165 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001166 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001167 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001168
1169 spin_unlock_irqrestore(&info->slock, flags);
1170}
1171
1172static int mxser_write_room(struct tty_struct *tty)
1173{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001174 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001175 int ret;
1176
1177 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1178 if (ret < 0)
1179 ret = 0;
1180 return ret;
1181}
1182
1183static int mxser_chars_in_buffer(struct tty_struct *tty)
1184{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001185 struct mxser_port *info = tty->driver_data;
Jiri Slaby925e9c12006-12-08 02:38:30 -08001186 return info->xmit_cnt;
Jiri Slaby037ad482006-12-08 02:38:11 -08001187}
1188
1189static void mxser_flush_buffer(struct tty_struct *tty)
1190{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001191 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001192 char fcr;
1193 unsigned long flags;
1194
1195
1196 spin_lock_irqsave(&info->slock, flags);
1197 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1198
Jiri Slaby55b307d2006-12-08 02:38:14 -08001199 fcr = inb(info->ioaddr + UART_FCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08001200 outb((fcr | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
Jiri Slaby55b307d2006-12-08 02:38:14 -08001201 info->ioaddr + UART_FCR);
1202 outb(fcr, info->ioaddr + UART_FCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08001203
1204 spin_unlock_irqrestore(&info->slock, flags);
Jiri Slaby037ad482006-12-08 02:38:11 -08001205
Jiri Slaby7e8bcf92006-12-08 02:38:24 -08001206 tty_wakeup(tty);
Jiri Slaby037ad482006-12-08 02:38:11 -08001207}
1208
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001209/*
1210 * ------------------------------------------------------------
1211 * friends of mxser_ioctl()
1212 * ------------------------------------------------------------
1213 */
1214static int mxser_get_serial_info(struct mxser_port *info,
1215 struct serial_struct __user *retinfo)
1216{
1217 struct serial_struct tmp;
1218
1219 if (!retinfo)
1220 return -EFAULT;
1221 memset(&tmp, 0, sizeof(tmp));
1222 tmp.type = info->type;
1223 tmp.line = info->tty->index;
1224 tmp.port = info->ioaddr;
1225 tmp.irq = info->board->irq;
1226 tmp.flags = info->flags;
1227 tmp.baud_base = info->baud_base;
1228 tmp.close_delay = info->close_delay;
1229 tmp.closing_wait = info->closing_wait;
1230 tmp.custom_divisor = info->custom_divisor;
1231 tmp.hub6 = 0;
1232 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1233 return -EFAULT;
1234 return 0;
1235}
1236
1237static int mxser_set_serial_info(struct mxser_port *info,
1238 struct serial_struct __user *new_info)
1239{
1240 struct serial_struct new_serial;
Jiri Slabyf64c84a2007-02-10 01:45:21 -08001241 unsigned long sl_flags;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001242 unsigned int flags;
1243 int retval = 0;
1244
1245 if (!new_info || !info->ioaddr)
1246 return -EFAULT;
1247 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
1248 return -EFAULT;
1249
1250 if ((new_serial.irq != info->board->irq) ||
1251 (new_serial.port != info->ioaddr) ||
1252 (new_serial.custom_divisor != info->custom_divisor) ||
1253 (new_serial.baud_base != info->baud_base))
1254 return -EPERM;
1255
1256 flags = info->flags & ASYNC_SPD_MASK;
1257
1258 if (!capable(CAP_SYS_ADMIN)) {
1259 if ((new_serial.baud_base != info->baud_base) ||
1260 (new_serial.close_delay != info->close_delay) ||
1261 ((new_serial.flags & ~ASYNC_USR_MASK) != (info->flags & ~ASYNC_USR_MASK)))
1262 return -EPERM;
1263 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
1264 (new_serial.flags & ASYNC_USR_MASK));
1265 } else {
1266 /*
1267 * OK, past this point, all the error checking has been done.
1268 * At this point, we start making changes.....
1269 */
1270 info->flags = ((info->flags & ~ASYNC_FLAGS) |
1271 (new_serial.flags & ASYNC_FLAGS));
1272 info->close_delay = new_serial.close_delay * HZ / 100;
1273 info->closing_wait = new_serial.closing_wait * HZ / 100;
1274 info->tty->low_latency =
1275 (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
Jiri Slabye079f4952006-12-08 02:38:31 -08001276 info->tty->low_latency = 0;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001277 }
1278
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001279 info->type = new_serial.type;
1280
1281 process_txrx_fifo(info);
1282
1283 if (info->flags & ASYNC_INITIALIZED) {
Jiri Slabyf64c84a2007-02-10 01:45:21 -08001284 if (flags != (info->flags & ASYNC_SPD_MASK)) {
1285 spin_lock_irqsave(&info->slock, sl_flags);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001286 mxser_change_speed(info, NULL);
Jiri Slabyf64c84a2007-02-10 01:45:21 -08001287 spin_unlock_irqrestore(&info->slock, sl_flags);
1288 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001289 } else
1290 retval = mxser_startup(info);
1291
1292 return retval;
1293}
1294
1295/*
1296 * mxser_get_lsr_info - get line status register info
1297 *
1298 * Purpose: Let user call ioctl() to get info when the UART physically
1299 * is emptied. On bus types like RS485, the transmitter must
1300 * release the bus after transmitting. This must be done when
1301 * the transmit shift register is empty, not be done when the
1302 * transmit holding register is empty. This functionality
1303 * allows an RS485 driver to be written in user space.
1304 */
1305static int mxser_get_lsr_info(struct mxser_port *info,
1306 unsigned int __user *value)
1307{
1308 unsigned char status;
1309 unsigned int result;
1310 unsigned long flags;
1311
1312 spin_lock_irqsave(&info->slock, flags);
1313 status = inb(info->ioaddr + UART_LSR);
1314 spin_unlock_irqrestore(&info->slock, flags);
1315 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1316 return put_user(result, value);
1317}
1318
1319/*
1320 * This routine sends a break character out the serial port.
1321 */
1322static void mxser_send_break(struct mxser_port *info, int duration)
1323{
1324 unsigned long flags;
1325
1326 if (!info->ioaddr)
1327 return;
1328 set_current_state(TASK_INTERRUPTIBLE);
1329 spin_lock_irqsave(&info->slock, flags);
1330 outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
1331 info->ioaddr + UART_LCR);
1332 spin_unlock_irqrestore(&info->slock, flags);
1333 schedule_timeout(duration);
1334 spin_lock_irqsave(&info->slock, flags);
1335 outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
1336 info->ioaddr + UART_LCR);
1337 spin_unlock_irqrestore(&info->slock, flags);
1338}
1339
1340static int mxser_tiocmget(struct tty_struct *tty, struct file *file)
Jiri Slaby037ad482006-12-08 02:38:11 -08001341{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001342 struct mxser_port *info = tty->driver_data;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001343 unsigned char control, status;
Jiri Slaby037ad482006-12-08 02:38:11 -08001344 unsigned long flags;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001345
Jiri Slaby037ad482006-12-08 02:38:11 -08001346
1347 if (tty->index == MXSER_PORTS)
Jiri Slaby037ad482006-12-08 02:38:11 -08001348 return -ENOIOCTLCMD;
Jiri Slaby214efeb2006-12-08 02:38:25 -08001349 if (test_bit(TTY_IO_ERROR, &tty->flags))
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001350 return -EIO;
1351
1352 control = info->MCR;
1353
1354 spin_lock_irqsave(&info->slock, flags);
1355 status = inb(info->ioaddr + UART_MSR);
1356 if (status & UART_MSR_ANY_DELTA)
1357 mxser_check_modem_status(info, status);
1358 spin_unlock_irqrestore(&info->slock, flags);
1359 return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0) |
1360 ((control & UART_MCR_DTR) ? TIOCM_DTR : 0) |
1361 ((status & UART_MSR_DCD) ? TIOCM_CAR : 0) |
1362 ((status & UART_MSR_RI) ? TIOCM_RNG : 0) |
1363 ((status & UART_MSR_DSR) ? TIOCM_DSR : 0) |
1364 ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
1365}
1366
1367static int mxser_tiocmset(struct tty_struct *tty, struct file *file,
1368 unsigned int set, unsigned int clear)
1369{
1370 struct mxser_port *info = tty->driver_data;
1371 unsigned long flags;
1372
1373
1374 if (tty->index == MXSER_PORTS)
1375 return -ENOIOCTLCMD;
Jiri Slaby214efeb2006-12-08 02:38:25 -08001376 if (test_bit(TTY_IO_ERROR, &tty->flags))
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001377 return -EIO;
1378
1379 spin_lock_irqsave(&info->slock, flags);
1380
1381 if (set & TIOCM_RTS)
1382 info->MCR |= UART_MCR_RTS;
1383 if (set & TIOCM_DTR)
1384 info->MCR |= UART_MCR_DTR;
1385
1386 if (clear & TIOCM_RTS)
1387 info->MCR &= ~UART_MCR_RTS;
1388 if (clear & TIOCM_DTR)
1389 info->MCR &= ~UART_MCR_DTR;
1390
1391 outb(info->MCR, info->ioaddr + UART_MCR);
1392 spin_unlock_irqrestore(&info->slock, flags);
Jiri Slaby037ad482006-12-08 02:38:11 -08001393 return 0;
1394}
1395
Jiri Slabyc76b0932007-02-10 01:45:07 -08001396static int __init mxser_program_mode(int port)
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001397{
1398 int id, i, j, n;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001399
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001400 outb(0, port);
1401 outb(0, port);
1402 outb(0, port);
1403 (void)inb(port);
1404 (void)inb(port);
1405 outb(0, port);
1406 (void)inb(port);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001407
1408 id = inb(port + 1) & 0x1F;
1409 if ((id != C168_ASIC_ID) &&
1410 (id != C104_ASIC_ID) &&
1411 (id != C102_ASIC_ID) &&
1412 (id != CI132_ASIC_ID) &&
1413 (id != CI134_ASIC_ID) &&
1414 (id != CI104J_ASIC_ID))
1415 return -1;
1416 for (i = 0, j = 0; i < 4; i++) {
1417 n = inb(port + 2);
1418 if (n == 'M') {
1419 j = 1;
1420 } else if ((j == 1) && (n == 1)) {
1421 j = 2;
1422 break;
1423 } else
1424 j = 0;
1425 }
1426 if (j != 2)
1427 id = -2;
1428 return id;
1429}
1430
Jiri Slabyc76b0932007-02-10 01:45:07 -08001431static void __init mxser_normal_mode(int port)
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001432{
1433 int i, n;
1434
1435 outb(0xA5, port + 1);
1436 outb(0x80, port + 3);
1437 outb(12, port + 0); /* 9600 bps */
1438 outb(0, port + 1);
1439 outb(0x03, port + 3); /* 8 data bits */
1440 outb(0x13, port + 4); /* loop back mode */
1441 for (i = 0; i < 16; i++) {
1442 n = inb(port + 5);
1443 if ((n & 0x61) == 0x60)
1444 break;
1445 if ((n & 1) == 1)
1446 (void)inb(port);
1447 }
1448 outb(0x00, port + 4);
1449}
1450
1451#define CHIP_SK 0x01 /* Serial Data Clock in Eprom */
1452#define CHIP_DO 0x02 /* Serial Data Output in Eprom */
1453#define CHIP_CS 0x04 /* Serial Chip Select in Eprom */
1454#define CHIP_DI 0x08 /* Serial Data Input in Eprom */
1455#define EN_CCMD 0x000 /* Chip's command register */
1456#define EN0_RSARLO 0x008 /* Remote start address reg 0 */
1457#define EN0_RSARHI 0x009 /* Remote start address reg 1 */
1458#define EN0_RCNTLO 0x00A /* Remote byte count reg WR */
1459#define EN0_RCNTHI 0x00B /* Remote byte count reg WR */
1460#define EN0_DCFG 0x00E /* Data configuration reg WR */
1461#define EN0_PORT 0x010 /* Rcv missed frame error counter RD */
1462#define ENC_PAGE0 0x000 /* Select page 0 of chip registers */
1463#define ENC_PAGE3 0x0C0 /* Select page 3 of chip registers */
Jiri Slabyc76b0932007-02-10 01:45:07 -08001464static int __init mxser_read_register(int port, unsigned short *regs)
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001465{
1466 int i, k, value, id;
1467 unsigned int j;
1468
1469 id = mxser_program_mode(port);
1470 if (id < 0)
1471 return id;
1472 for (i = 0; i < 14; i++) {
1473 k = (i & 0x3F) | 0x180;
1474 for (j = 0x100; j > 0; j >>= 1) {
1475 outb(CHIP_CS, port);
1476 if (k & j) {
1477 outb(CHIP_CS | CHIP_DO, port);
1478 outb(CHIP_CS | CHIP_DO | CHIP_SK, port); /* A? bit of read */
1479 } else {
1480 outb(CHIP_CS, port);
1481 outb(CHIP_CS | CHIP_SK, port); /* A? bit of read */
1482 }
1483 }
1484 (void)inb(port);
1485 value = 0;
1486 for (k = 0, j = 0x8000; k < 16; k++, j >>= 1) {
1487 outb(CHIP_CS, port);
1488 outb(CHIP_CS | CHIP_SK, port);
1489 if (inb(port) & CHIP_DI)
1490 value |= j;
1491 }
1492 regs[i] = value;
1493 outb(0, port);
1494 }
1495 mxser_normal_mode(port);
1496 return id;
1497}
1498
Jiri Slaby037ad482006-12-08 02:38:11 -08001499static int mxser_ioctl_special(unsigned int cmd, void __user *argp)
1500{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001501 struct mxser_port *port;
1502 int result, status;
1503 unsigned int i, j;
Jiri Slaby037ad482006-12-08 02:38:11 -08001504
1505 switch (cmd) {
1506 case MOXA_GET_CONF:
Jiri Slaby55b307d2006-12-08 02:38:14 -08001507/* if (copy_to_user(argp, mxsercfg,
Jiri Slaby037ad482006-12-08 02:38:11 -08001508 sizeof(struct mxser_hwconf) * 4))
1509 return -EFAULT;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001510 return 0;*/
1511 return -ENXIO;
Jiri Slaby037ad482006-12-08 02:38:11 -08001512 case MOXA_GET_MAJOR:
1513 if (copy_to_user(argp, &ttymajor, sizeof(int)))
1514 return -EFAULT;
1515 return 0;
1516
1517 case MOXA_GET_CUMAJOR:
1518 if (copy_to_user(argp, &calloutmajor, sizeof(int)))
1519 return -EFAULT;
1520 return 0;
1521
1522 case MOXA_CHKPORTENABLE:
1523 result = 0;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001524
1525 for (i = 0; i < MXSER_BOARDS; i++)
1526 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++)
1527 if (mxser_boards[i].ports[j].ioaddr)
1528 result |= (1 << i);
1529
Jiri Slaby037ad482006-12-08 02:38:11 -08001530 return put_user(result, (unsigned long __user *)argp);
1531 case MOXA_GETDATACOUNT:
1532 if (copy_to_user(argp, &mxvar_log, sizeof(mxvar_log)))
1533 return -EFAULT;
1534 return 0;
1535 case MOXA_GETMSTATUS:
Jiri Slaby55b307d2006-12-08 02:38:14 -08001536 for (i = 0; i < MXSER_BOARDS; i++)
1537 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
1538 port = &mxser_boards[i].ports[j];
1539
1540 GMStatus[i].ri = 0;
1541 if (!port->ioaddr) {
1542 GMStatus[i].dcd = 0;
1543 GMStatus[i].dsr = 0;
1544 GMStatus[i].cts = 0;
1545 continue;
1546 }
1547
1548 if (!port->tty || !port->tty->termios)
1549 GMStatus[i].cflag =
1550 port->normal_termios.c_cflag;
1551 else
1552 GMStatus[i].cflag =
1553 port->tty->termios->c_cflag;
1554
1555 status = inb(port->ioaddr + UART_MSR);
1556 if (status & 0x80 /*UART_MSR_DCD */ )
1557 GMStatus[i].dcd = 1;
1558 else
1559 GMStatus[i].dcd = 0;
1560
1561 if (status & 0x20 /*UART_MSR_DSR */ )
1562 GMStatus[i].dsr = 1;
1563 else
1564 GMStatus[i].dsr = 0;
1565
1566
1567 if (status & 0x10 /*UART_MSR_CTS */ )
1568 GMStatus[i].cts = 1;
1569 else
1570 GMStatus[i].cts = 0;
Jiri Slaby037ad482006-12-08 02:38:11 -08001571 }
Jiri Slaby037ad482006-12-08 02:38:11 -08001572 if (copy_to_user(argp, GMStatus,
1573 sizeof(struct mxser_mstatus) * MXSER_PORTS))
1574 return -EFAULT;
1575 return 0;
1576 case MOXA_ASPP_MON_EXT: {
Jiri Slaby2b770b42007-07-15 23:40:17 -07001577 int p, shiftbit;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001578 unsigned long opmode;
1579 unsigned cflag, iflag;
Jiri Slaby037ad482006-12-08 02:38:11 -08001580
Jiri Slaby55b307d2006-12-08 02:38:14 -08001581 for (i = 0; i < MXSER_BOARDS; i++)
1582 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
1583 port = &mxser_boards[i].ports[j];
1584 if (!port->ioaddr)
Jiri Slaby037ad482006-12-08 02:38:11 -08001585 continue;
1586
Jiri Slaby55b307d2006-12-08 02:38:14 -08001587 status = mxser_get_msr(port->ioaddr, 0, i);
Jiri Slaby55b307d2006-12-08 02:38:14 -08001588
Jiri Slaby037ad482006-12-08 02:38:11 -08001589 if (status & UART_MSR_TERI)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001590 port->icount.rng++;
Jiri Slaby037ad482006-12-08 02:38:11 -08001591 if (status & UART_MSR_DDSR)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001592 port->icount.dsr++;
Jiri Slaby037ad482006-12-08 02:38:11 -08001593 if (status & UART_MSR_DDCD)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001594 port->icount.dcd++;
Jiri Slaby037ad482006-12-08 02:38:11 -08001595 if (status & UART_MSR_DCTS)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001596 port->icount.cts++;
Jiri Slaby037ad482006-12-08 02:38:11 -08001597
Jiri Slaby55b307d2006-12-08 02:38:14 -08001598 port->mon_data.modem_status = status;
1599 mon_data_ext.rx_cnt[i] = port->mon_data.rxcnt;
1600 mon_data_ext.tx_cnt[i] = port->mon_data.txcnt;
1601 mon_data_ext.up_rxcnt[i] =
1602 port->mon_data.up_rxcnt;
1603 mon_data_ext.up_txcnt[i] =
1604 port->mon_data.up_txcnt;
1605 mon_data_ext.modem_status[i] =
1606 port->mon_data.modem_status;
1607 mon_data_ext.baudrate[i] = port->realbaud;
Jiri Slaby037ad482006-12-08 02:38:11 -08001608
Jiri Slaby55b307d2006-12-08 02:38:14 -08001609 if (!port->tty || !port->tty->termios) {
1610 cflag = port->normal_termios.c_cflag;
1611 iflag = port->normal_termios.c_iflag;
Jiri Slaby037ad482006-12-08 02:38:11 -08001612 } else {
Jiri Slaby55b307d2006-12-08 02:38:14 -08001613 cflag = port->tty->termios->c_cflag;
1614 iflag = port->tty->termios->c_iflag;
Jiri Slaby037ad482006-12-08 02:38:11 -08001615 }
1616
1617 mon_data_ext.databits[i] = cflag & CSIZE;
1618
1619 mon_data_ext.stopbits[i] = cflag & CSTOPB;
1620
Jiri Slaby55b307d2006-12-08 02:38:14 -08001621 mon_data_ext.parity[i] =
1622 cflag & (PARENB | PARODD | CMSPAR);
Jiri Slaby037ad482006-12-08 02:38:11 -08001623
1624 mon_data_ext.flowctrl[i] = 0x00;
1625
1626 if (cflag & CRTSCTS)
1627 mon_data_ext.flowctrl[i] |= 0x03;
1628
1629 if (iflag & (IXON | IXOFF))
1630 mon_data_ext.flowctrl[i] |= 0x0C;
1631
Jiri Slaby55b307d2006-12-08 02:38:14 -08001632 if (port->type == PORT_16550A)
Jiri Slaby037ad482006-12-08 02:38:11 -08001633 mon_data_ext.fifo[i] = 1;
1634 else
1635 mon_data_ext.fifo[i] = 0;
1636
1637 p = i % 4;
1638 shiftbit = p * 2;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001639 opmode = inb(port->opmode_ioaddr) >> shiftbit;
Jiri Slaby037ad482006-12-08 02:38:11 -08001640 opmode &= OP_MODE_MASK;
1641
1642 mon_data_ext.iftype[i] = opmode;
1643
1644 }
Jiri Slaby55b307d2006-12-08 02:38:14 -08001645 if (copy_to_user(argp, &mon_data_ext,
1646 sizeof(mon_data_ext)))
Jiri Slaby037ad482006-12-08 02:38:11 -08001647 return -EFAULT;
1648
1649 return 0;
1650
Jiri Slaby55b307d2006-12-08 02:38:14 -08001651 } default:
Jiri Slaby037ad482006-12-08 02:38:11 -08001652 return -ENOIOCTLCMD;
1653 }
1654 return 0;
1655}
1656
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001657static int mxser_ioctl(struct tty_struct *tty, struct file *file,
1658 unsigned int cmd, unsigned long arg)
1659{
1660 struct mxser_port *info = tty->driver_data;
1661 struct async_icount cprev, cnow; /* kernel counter temps */
1662 struct serial_icounter_struct __user *p_cuser;
1663 unsigned long templ;
1664 unsigned long flags;
Jiri Slabyf64c84a2007-02-10 01:45:21 -08001665 unsigned int i;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001666 void __user *argp = (void __user *)arg;
1667 int retval;
1668
1669 if (tty->index == MXSER_PORTS)
1670 return mxser_ioctl_special(cmd, argp);
1671
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001672 if (cmd == MOXA_SET_OP_MODE || cmd == MOXA_GET_OP_MODE) {
1673 int p;
1674 unsigned long opmode;
1675 static unsigned char ModeMask[] = { 0xfc, 0xf3, 0xcf, 0x3f };
1676 int shiftbit;
1677 unsigned char val, mask;
1678
1679 p = tty->index % 4;
1680 if (cmd == MOXA_SET_OP_MODE) {
1681 if (get_user(opmode, (int __user *) argp))
1682 return -EFAULT;
1683 if (opmode != RS232_MODE &&
1684 opmode != RS485_2WIRE_MODE &&
1685 opmode != RS422_MODE &&
1686 opmode != RS485_4WIRE_MODE)
1687 return -EFAULT;
1688 mask = ModeMask[p];
1689 shiftbit = p * 2;
1690 val = inb(info->opmode_ioaddr);
1691 val &= mask;
1692 val |= (opmode << shiftbit);
1693 outb(val, info->opmode_ioaddr);
1694 } else {
1695 shiftbit = p * 2;
1696 opmode = inb(info->opmode_ioaddr) >> shiftbit;
1697 opmode &= OP_MODE_MASK;
1698 if (copy_to_user(argp, &opmode, sizeof(int)))
1699 return -EFAULT;
1700 }
1701 return 0;
1702 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001703
Jiri Slabyf64c84a2007-02-10 01:45:21 -08001704 if (cmd == MOXA_SET_SPECIAL_BAUD_RATE) {
1705 int speed;
1706
1707 if (get_user(speed, (int __user *)argp))
1708 return -EFAULT;
1709 if (speed <= 0 || speed > info->max_baud)
1710 return -EFAULT;
1711 if (!info->tty || !info->tty->termios || !info->ioaddr)
1712 return 0;
1713 info->tty->termios->c_cflag &= ~(CBAUD | CBAUDEX);
1714 for (i = 0; i < BAUD_TABLE_NO; i++)
1715 if (speed == mxvar_baud_table[i])
1716 break;
1717 if (i == BAUD_TABLE_NO) {
1718 info->tty->termios->c_cflag |= B_SPEC;
1719 } else if (speed != 0)
1720 info->tty->termios->c_cflag |= mxvar_baud_table1[i];
1721
1722 info->speed = speed;
1723 spin_lock_irqsave(&info->slock, flags);
Jiri Slabya136e992007-02-10 01:45:24 -08001724 mxser_change_speed(info, NULL);
Jiri Slabyf64c84a2007-02-10 01:45:21 -08001725 spin_unlock_irqrestore(&info->slock, flags);
1726
1727 return 0;
1728 } else if (cmd == MOXA_GET_SPECIAL_BAUD_RATE) {
1729 if (copy_to_user(argp, &info->speed, sizeof(int)))
1730 return -EFAULT;
1731 return 0;
1732 }
1733
Jiri Slaby214efeb2006-12-08 02:38:25 -08001734 if (cmd != TIOCGSERIAL && cmd != TIOCMIWAIT && cmd != TIOCGICOUNT &&
1735 test_bit(TTY_IO_ERROR, &tty->flags))
1736 return -EIO;
1737
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001738 switch (cmd) {
1739 case TCSBRK: /* SVID version: non-zero arg --> no break */
1740 retval = tty_check_change(tty);
1741 if (retval)
1742 return retval;
1743 tty_wait_until_sent(tty, 0);
1744 if (!arg)
1745 mxser_send_break(info, HZ / 4); /* 1/4 second */
1746 return 0;
1747 case TCSBRKP: /* support for POSIX tcsendbreak() */
1748 retval = tty_check_change(tty);
1749 if (retval)
1750 return retval;
1751 tty_wait_until_sent(tty, 0);
1752 mxser_send_break(info, arg ? arg * (HZ / 10) : HZ / 4);
1753 return 0;
1754 case TIOCGSOFTCAR:
1755 return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *)argp);
1756 case TIOCSSOFTCAR:
1757 if (get_user(templ, (unsigned long __user *) argp))
1758 return -EFAULT;
1759 arg = templ;
1760 tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) | (arg ? CLOCAL : 0));
1761 return 0;
1762 case TIOCGSERIAL:
1763 return mxser_get_serial_info(info, argp);
1764 case TIOCSSERIAL:
1765 return mxser_set_serial_info(info, argp);
1766 case TIOCSERGETLSR: /* Get line status register */
1767 return mxser_get_lsr_info(info, argp);
1768 /*
1769 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1770 * - mask passed in arg for lines of interest
1771 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1772 * Caller should use TIOCGICOUNT to see which one it was
1773 */
Jiri Slabyb446a4a2007-04-23 14:41:03 -07001774 case TIOCMIWAIT:
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001775 spin_lock_irqsave(&info->slock, flags);
Jiri Slabyb446a4a2007-04-23 14:41:03 -07001776 cnow = info->icount; /* note the counters on entry */
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001777 spin_unlock_irqrestore(&info->slock, flags);
1778
Jiri Slabyb446a4a2007-04-23 14:41:03 -07001779 wait_event_interruptible(info->delta_msr_wait, ({
1780 cprev = cnow;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001781 spin_lock_irqsave(&info->slock, flags);
1782 cnow = info->icount; /* atomic copy */
1783 spin_unlock_irqrestore(&info->slock, flags);
1784
Jiri Slabyb446a4a2007-04-23 14:41:03 -07001785 ((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1786 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1787 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1788 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts));
1789 }));
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001790 break;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001791 /*
1792 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1793 * Return: write counters to the user passed counter struct
1794 * NB: both 1->0 and 0->1 transitions are counted except for
1795 * RI where only 0->1 is counted.
1796 */
1797 case TIOCGICOUNT:
1798 spin_lock_irqsave(&info->slock, flags);
1799 cnow = info->icount;
1800 spin_unlock_irqrestore(&info->slock, flags);
1801 p_cuser = argp;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001802 if (put_user(cnow.frame, &p_cuser->frame))
1803 return -EFAULT;
1804 if (put_user(cnow.brk, &p_cuser->brk))
1805 return -EFAULT;
1806 if (put_user(cnow.overrun, &p_cuser->overrun))
1807 return -EFAULT;
1808 if (put_user(cnow.buf_overrun, &p_cuser->buf_overrun))
1809 return -EFAULT;
1810 if (put_user(cnow.parity, &p_cuser->parity))
1811 return -EFAULT;
1812 if (put_user(cnow.rx, &p_cuser->rx))
1813 return -EFAULT;
1814 if (put_user(cnow.tx, &p_cuser->tx))
1815 return -EFAULT;
1816 put_user(cnow.cts, &p_cuser->cts);
1817 put_user(cnow.dsr, &p_cuser->dsr);
1818 put_user(cnow.rng, &p_cuser->rng);
1819 put_user(cnow.dcd, &p_cuser->dcd);
1820 return 0;
1821 case MOXA_HighSpeedOn:
1822 return put_user(info->baud_base != 115200 ? 1 : 0, (int __user *)argp);
1823 case MOXA_SDS_RSTICOUNTER:
1824 info->mon_data.rxcnt = 0;
1825 info->mon_data.txcnt = 0;
1826 return 0;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001827 case MOXA_ASPP_SETBAUD:{
1828 long baud;
1829 if (get_user(baud, (long __user *)argp))
1830 return -EFAULT;
Jiri Slabyf64c84a2007-02-10 01:45:21 -08001831 spin_lock_irqsave(&info->slock, flags);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001832 mxser_set_baud(info, baud);
Jiri Slabyf64c84a2007-02-10 01:45:21 -08001833 spin_unlock_irqrestore(&info->slock, flags);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001834 return 0;
1835 }
1836 case MOXA_ASPP_GETBAUD:
1837 if (copy_to_user(argp, &info->realbaud, sizeof(long)))
1838 return -EFAULT;
1839
1840 return 0;
1841
1842 case MOXA_ASPP_OQUEUE:{
1843 int len, lsr;
1844
1845 len = mxser_chars_in_buffer(tty);
1846
1847 lsr = inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT;
1848
1849 len += (lsr ? 0 : 1);
1850
1851 if (copy_to_user(argp, &len, sizeof(int)))
1852 return -EFAULT;
1853
1854 return 0;
1855 }
1856 case MOXA_ASPP_MON: {
1857 int mcr, status;
1858
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001859 status = mxser_get_msr(info->ioaddr, 1, tty->index);
1860 mxser_check_modem_status(info, status);
1861
1862 mcr = inb(info->ioaddr + UART_MCR);
1863 if (mcr & MOXA_MUST_MCR_XON_FLAG)
1864 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFHOLD;
1865 else
1866 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFHOLD;
1867
1868 if (mcr & MOXA_MUST_MCR_TX_XON)
1869 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFXENT;
1870 else
1871 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFXENT;
1872
1873 if (info->tty->hw_stopped)
1874 info->mon_data.hold_reason |= NPPI_NOTIFY_CTSHOLD;
1875 else
1876 info->mon_data.hold_reason &= ~NPPI_NOTIFY_CTSHOLD;
1877
1878 if (copy_to_user(argp, &info->mon_data,
1879 sizeof(struct mxser_mon)))
1880 return -EFAULT;
1881
1882 return 0;
1883 }
1884 case MOXA_ASPP_LSTATUS: {
1885 if (copy_to_user(argp, &info->err_shadow,
1886 sizeof(unsigned char)))
1887 return -EFAULT;
1888
1889 info->err_shadow = 0;
1890 return 0;
1891 }
1892 case MOXA_SET_BAUD_METHOD: {
1893 int method;
1894
1895 if (get_user(method, (int __user *)argp))
1896 return -EFAULT;
1897 mxser_set_baud_method[tty->index] = method;
1898 if (copy_to_user(argp, &method, sizeof(int)))
1899 return -EFAULT;
1900
1901 return 0;
1902 }
1903 default:
1904 return -ENOIOCTLCMD;
1905 }
1906 return 0;
1907}
1908
Jiri Slaby037ad482006-12-08 02:38:11 -08001909static void mxser_stoprx(struct tty_struct *tty)
1910{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001911 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001912
1913 info->ldisc_stop_rx = 1;
1914 if (I_IXOFF(tty)) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08001915 if (info->board->chip_flag) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001916 info->IER &= ~MOXA_MUST_RECV_ISR;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001917 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby925e9c12006-12-08 02:38:30 -08001918 } else {
Jiri Slaby037ad482006-12-08 02:38:11 -08001919 info->x_char = STOP_CHAR(tty);
Jiri Slaby55b307d2006-12-08 02:38:14 -08001920 outb(0, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001921 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001922 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001923 }
Jiri Slaby037ad482006-12-08 02:38:11 -08001924 }
1925
1926 if (info->tty->termios->c_cflag & CRTSCTS) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001927 info->MCR &= ~UART_MCR_RTS;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001928 outb(info->MCR, info->ioaddr + UART_MCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08001929 }
1930}
1931
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001932/*
1933 * This routine is called by the upper-layer tty layer to signal that
1934 * incoming characters should be throttled.
1935 */
1936static void mxser_throttle(struct tty_struct *tty)
1937{
1938 mxser_stoprx(tty);
1939}
1940
1941static void mxser_unthrottle(struct tty_struct *tty)
Jiri Slaby037ad482006-12-08 02:38:11 -08001942{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001943 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001944
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001945 /* startrx */
Jiri Slaby037ad482006-12-08 02:38:11 -08001946 info->ldisc_stop_rx = 0;
1947 if (I_IXOFF(tty)) {
1948 if (info->x_char)
1949 info->x_char = 0;
1950 else {
Jiri Slaby55b307d2006-12-08 02:38:14 -08001951 if (info->board->chip_flag) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001952 info->IER |= MOXA_MUST_RECV_ISR;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001953 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby925e9c12006-12-08 02:38:30 -08001954 } else {
Jiri Slaby037ad482006-12-08 02:38:11 -08001955 info->x_char = START_CHAR(tty);
Jiri Slaby55b307d2006-12-08 02:38:14 -08001956 outb(0, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001957 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001958 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001959 }
Jiri Slaby037ad482006-12-08 02:38:11 -08001960 }
1961 }
1962
1963 if (info->tty->termios->c_cflag & CRTSCTS) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001964 info->MCR |= UART_MCR_RTS;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001965 outb(info->MCR, info->ioaddr + UART_MCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08001966 }
1967}
1968
1969/*
Jiri Slaby037ad482006-12-08 02:38:11 -08001970 * mxser_stop() and mxser_start()
1971 *
1972 * This routines are called before setting or resetting tty->stopped.
1973 * They enable or disable transmitter interrupts, as necessary.
1974 */
1975static void mxser_stop(struct tty_struct *tty)
1976{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001977 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001978 unsigned long flags;
1979
1980 spin_lock_irqsave(&info->slock, flags);
1981 if (info->IER & UART_IER_THRI) {
1982 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
1988static void mxser_start(struct tty_struct *tty)
1989{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001990 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001991 unsigned long flags;
1992
1993 spin_lock_irqsave(&info->slock, flags);
Jiri Slabye079f4952006-12-08 02:38:31 -08001994 if (info->xmit_cnt && info->xmit_buf) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08001995 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001996 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001997 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001998 }
1999 spin_unlock_irqrestore(&info->slock, flags);
2000}
2001
Alan Cox606d0992006-12-08 02:38:45 -08002002static void mxser_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002003{
2004 struct mxser_port *info = tty->driver_data;
2005 unsigned long flags;
2006
Alan Cox7e7d1362007-07-15 23:41:49 -07002007 spin_lock_irqsave(&info->slock, flags);
2008 mxser_change_speed(info, old_termios);
2009 spin_unlock_irqrestore(&info->slock, flags);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002010
Alan Cox7e7d1362007-07-15 23:41:49 -07002011 if ((old_termios->c_cflag & CRTSCTS) &&
2012 !(tty->termios->c_cflag & CRTSCTS)) {
2013 tty->hw_stopped = 0;
2014 mxser_start(tty);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002015 }
2016
Jiri Slabye079f4952006-12-08 02:38:31 -08002017 /* Handle sw stopped */
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002018 if ((old_termios->c_iflag & IXON) &&
2019 !(tty->termios->c_iflag & IXON)) {
2020 tty->stopped = 0;
2021
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002022 if (info->board->chip_flag) {
2023 spin_lock_irqsave(&info->slock, flags);
2024 DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
2025 spin_unlock_irqrestore(&info->slock, flags);
2026 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002027
2028 mxser_start(tty);
2029 }
2030}
2031
Jiri Slaby037ad482006-12-08 02:38:11 -08002032/*
2033 * mxser_wait_until_sent() --- wait until the transmitter is empty
2034 */
2035static void mxser_wait_until_sent(struct tty_struct *tty, int timeout)
2036{
Jiri Slaby55b307d2006-12-08 02:38:14 -08002037 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08002038 unsigned long orig_jiffies, char_time;
2039 int lsr;
2040
2041 if (info->type == PORT_UNKNOWN)
2042 return;
2043
2044 if (info->xmit_fifo_size == 0)
2045 return; /* Just in case.... */
2046
2047 orig_jiffies = jiffies;
2048 /*
2049 * Set the check interval to be 1/5 of the estimated time to
2050 * send a single character, and make it at least 1. The check
2051 * interval should also be less than the timeout.
2052 *
2053 * Note: we have to use pretty tight timings here to satisfy
2054 * the NIST-PCTS.
2055 */
2056 char_time = (info->timeout - HZ / 50) / info->xmit_fifo_size;
2057 char_time = char_time / 5;
2058 if (char_time == 0)
2059 char_time = 1;
2060 if (timeout && timeout < char_time)
2061 char_time = timeout;
2062 /*
2063 * If the transmitter hasn't cleared in twice the approximate
2064 * amount of time to send the entire FIFO, it probably won't
2065 * ever clear. This assumes the UART isn't doing flow
2066 * control, which is currently the case. Hence, if it ever
2067 * takes longer than info->timeout, this is probably due to a
2068 * UART bug of some kind. So, we clamp the timeout parameter at
2069 * 2*info->timeout.
2070 */
2071 if (!timeout || timeout > 2 * info->timeout)
2072 timeout = 2 * info->timeout;
2073#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2074 printk(KERN_DEBUG "In rs_wait_until_sent(%d) check=%lu...",
2075 timeout, char_time);
2076 printk("jiff=%lu...", jiffies);
2077#endif
Jiri Slaby55b307d2006-12-08 02:38:14 -08002078 while (!((lsr = inb(info->ioaddr + UART_LSR)) & UART_LSR_TEMT)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002079#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2080 printk("lsr = %d (jiff=%lu)...", lsr, jiffies);
2081#endif
2082 schedule_timeout_interruptible(char_time);
2083 if (signal_pending(current))
2084 break;
2085 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2086 break;
2087 }
2088 set_current_state(TASK_RUNNING);
2089
2090#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2091 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
2092#endif
2093}
2094
Jiri Slaby037ad482006-12-08 02:38:11 -08002095/*
2096 * This routine is called by tty_hangup() when a hangup is signaled.
2097 */
Adrian Bunk02d3fca2007-02-10 01:44:51 -08002098static void mxser_hangup(struct tty_struct *tty)
Jiri Slaby037ad482006-12-08 02:38:11 -08002099{
Jiri Slaby55b307d2006-12-08 02:38:14 -08002100 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08002101
2102 mxser_flush_buffer(tty);
2103 mxser_shutdown(info);
2104 info->event = 0;
2105 info->count = 0;
2106 info->flags &= ~ASYNC_NORMAL_ACTIVE;
2107 info->tty = NULL;
2108 wake_up_interruptible(&info->open_wait);
2109}
2110
Jiri Slaby037ad482006-12-08 02:38:11 -08002111/*
2112 * mxser_rs_break() --- routine which turns the break handling on or off
2113 */
2114static void mxser_rs_break(struct tty_struct *tty, int break_state)
2115{
Jiri Slaby55b307d2006-12-08 02:38:14 -08002116 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08002117 unsigned long flags;
2118
2119 spin_lock_irqsave(&info->slock, flags);
2120 if (break_state == -1)
Jiri Slaby55b307d2006-12-08 02:38:14 -08002121 outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
2122 info->ioaddr + UART_LCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002123 else
Jiri Slaby55b307d2006-12-08 02:38:14 -08002124 outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
2125 info->ioaddr + UART_LCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002126 spin_unlock_irqrestore(&info->slock, flags);
2127}
2128
Jiri Slaby55b307d2006-12-08 02:38:14 -08002129static void mxser_receive_chars(struct mxser_port *port, int *status)
Jiri Slaby037ad482006-12-08 02:38:11 -08002130{
Jiri Slaby55b307d2006-12-08 02:38:14 -08002131 struct tty_struct *tty = port->tty;
Jiri Slaby037ad482006-12-08 02:38:11 -08002132 unsigned char ch, gdl;
2133 int ignored = 0;
2134 int cnt = 0;
2135 int recv_room;
2136 int max = 256;
Jiri Slaby037ad482006-12-08 02:38:11 -08002137
2138 recv_room = tty->receive_room;
Jiri Slabye079f4952006-12-08 02:38:31 -08002139 if ((recv_room == 0) && (!port->ldisc_stop_rx))
Jiri Slaby037ad482006-12-08 02:38:11 -08002140 mxser_stoprx(tty);
Jiri Slaby037ad482006-12-08 02:38:11 -08002141
Jiri Slaby55b307d2006-12-08 02:38:14 -08002142 if (port->board->chip_flag != MOXA_OTHER_UART) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002143
Jiri Slaby3306ce32006-12-08 02:38:13 -08002144 if (*status & UART_LSR_SPECIAL)
Jiri Slaby037ad482006-12-08 02:38:11 -08002145 goto intr_old;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002146 if (port->board->chip_flag == MOXA_MUST_MU860_HWID &&
Jiri Slaby037ad482006-12-08 02:38:11 -08002147 (*status & MOXA_MUST_LSR_RERR))
2148 goto intr_old;
Jiri Slaby037ad482006-12-08 02:38:11 -08002149 if (*status & MOXA_MUST_LSR_RERR)
2150 goto intr_old;
2151
Jiri Slaby55b307d2006-12-08 02:38:14 -08002152 gdl = inb(port->ioaddr + MOXA_MUST_GDL_REGISTER);
Jiri Slaby037ad482006-12-08 02:38:11 -08002153
Jiri Slaby55b307d2006-12-08 02:38:14 -08002154 if (port->board->chip_flag == MOXA_MUST_MU150_HWID)
Jiri Slaby037ad482006-12-08 02:38:11 -08002155 gdl &= MOXA_MUST_GDL_MASK;
2156 if (gdl >= recv_room) {
Jiri Slabye079f4952006-12-08 02:38:31 -08002157 if (!port->ldisc_stop_rx)
Jiri Slaby037ad482006-12-08 02:38:11 -08002158 mxser_stoprx(tty);
Jiri Slaby037ad482006-12-08 02:38:11 -08002159 }
2160 while (gdl--) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002161 ch = inb(port->ioaddr + UART_RX);
Jiri Slaby037ad482006-12-08 02:38:11 -08002162 tty_insert_flip_char(tty, ch, 0);
2163 cnt++;
Jiri Slaby037ad482006-12-08 02:38:11 -08002164 }
2165 goto end_intr;
2166 }
Jiri Slabye079f4952006-12-08 02:38:31 -08002167intr_old:
Jiri Slaby037ad482006-12-08 02:38:11 -08002168
2169 do {
2170 if (max-- < 0)
2171 break;
Jiri Slaby037ad482006-12-08 02:38:11 -08002172
Jiri Slaby55b307d2006-12-08 02:38:14 -08002173 ch = inb(port->ioaddr + UART_RX);
Jiri Slabye079f4952006-12-08 02:38:31 -08002174 if (port->board->chip_flag && (*status & UART_LSR_OE))
Jiri Slaby55b307d2006-12-08 02:38:14 -08002175 outb(0x23, port->ioaddr + UART_FCR);
2176 *status &= port->read_status_mask;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002177 if (*status & port->ignore_status_mask) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002178 if (++ignored > 100)
2179 break;
2180 } else {
2181 char flag = 0;
2182 if (*status & UART_LSR_SPECIAL) {
2183 if (*status & UART_LSR_BI) {
2184 flag = TTY_BREAK;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002185 port->icount.brk++;
Jiri Slaby3306ce32006-12-08 02:38:13 -08002186
Jiri Slaby55b307d2006-12-08 02:38:14 -08002187 if (port->flags & ASYNC_SAK)
Jiri Slaby037ad482006-12-08 02:38:11 -08002188 do_SAK(tty);
2189 } else if (*status & UART_LSR_PE) {
2190 flag = TTY_PARITY;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002191 port->icount.parity++;
Jiri Slaby037ad482006-12-08 02:38:11 -08002192 } else if (*status & UART_LSR_FE) {
2193 flag = TTY_FRAME;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002194 port->icount.frame++;
Jiri Slaby037ad482006-12-08 02:38:11 -08002195 } else if (*status & UART_LSR_OE) {
2196 flag = TTY_OVERRUN;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002197 port->icount.overrun++;
Jiri Slabyf64c84a2007-02-10 01:45:21 -08002198 } else
2199 flag = TTY_BREAK;
Jiri Slaby925e9c12006-12-08 02:38:30 -08002200 }
Jiri Slaby037ad482006-12-08 02:38:11 -08002201 tty_insert_flip_char(tty, ch, flag);
2202 cnt++;
2203 if (cnt >= recv_room) {
Jiri Slabye079f4952006-12-08 02:38:31 -08002204 if (!port->ldisc_stop_rx)
Jiri Slaby037ad482006-12-08 02:38:11 -08002205 mxser_stoprx(tty);
Jiri Slaby037ad482006-12-08 02:38:11 -08002206 break;
2207 }
2208
2209 }
2210
Jiri Slaby55b307d2006-12-08 02:38:14 -08002211 if (port->board->chip_flag)
Jiri Slaby037ad482006-12-08 02:38:11 -08002212 break;
Jiri Slaby037ad482006-12-08 02:38:11 -08002213
Jiri Slaby55b307d2006-12-08 02:38:14 -08002214 *status = inb(port->ioaddr + UART_LSR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002215 } while (*status & UART_LSR_DR);
2216
Jiri Slabye079f4952006-12-08 02:38:31 -08002217end_intr:
Jiri Slaby55b307d2006-12-08 02:38:14 -08002218 mxvar_log.rxcnt[port->tty->index] += cnt;
2219 port->mon_data.rxcnt += cnt;
2220 port->mon_data.up_rxcnt += cnt;
Jiri Slaby037ad482006-12-08 02:38:11 -08002221
Jan Yenya Kasprzak67d2bc52007-04-23 14:41:02 -07002222 /*
2223 * We are called from an interrupt context with &port->slock
2224 * being held. Drop it temporarily in order to prevent
2225 * recursive locking.
2226 */
2227 spin_unlock(&port->slock);
Jiri Slaby037ad482006-12-08 02:38:11 -08002228 tty_flip_buffer_push(tty);
Jan Yenya Kasprzak67d2bc52007-04-23 14:41:02 -07002229 spin_lock(&port->slock);
Jiri Slaby037ad482006-12-08 02:38:11 -08002230}
2231
Jiri Slaby55b307d2006-12-08 02:38:14 -08002232static void mxser_transmit_chars(struct mxser_port *port)
Jiri Slaby037ad482006-12-08 02:38:11 -08002233{
2234 int count, cnt;
Jiri Slaby037ad482006-12-08 02:38:11 -08002235
Jiri Slaby55b307d2006-12-08 02:38:14 -08002236 if (port->x_char) {
2237 outb(port->x_char, port->ioaddr + UART_TX);
2238 port->x_char = 0;
2239 mxvar_log.txcnt[port->tty->index]++;
2240 port->mon_data.txcnt++;
2241 port->mon_data.up_txcnt++;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002242 port->icount.tx++;
Jiri Slabyf5748742007-02-10 01:45:18 -08002243 return;
Jiri Slaby037ad482006-12-08 02:38:11 -08002244 }
2245
Jiri Slaby55b307d2006-12-08 02:38:14 -08002246 if (port->xmit_buf == 0)
Jiri Slabyf5748742007-02-10 01:45:18 -08002247 return;
Jiri Slaby037ad482006-12-08 02:38:11 -08002248
Jiri Slaby925e9c12006-12-08 02:38:30 -08002249 if ((port->xmit_cnt <= 0) || port->tty->stopped ||
2250 (port->tty->hw_stopped &&
Jiri Slaby55b307d2006-12-08 02:38:14 -08002251 (port->type != PORT_16550A) &&
2252 (!port->board->chip_flag))) {
2253 port->IER &= ~UART_IER_THRI;
2254 outb(port->IER, port->ioaddr + UART_IER);
Jiri Slabyf5748742007-02-10 01:45:18 -08002255 return;
Jiri Slaby037ad482006-12-08 02:38:11 -08002256 }
2257
Jiri Slaby55b307d2006-12-08 02:38:14 -08002258 cnt = port->xmit_cnt;
2259 count = port->xmit_fifo_size;
Jiri Slaby037ad482006-12-08 02:38:11 -08002260 do {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002261 outb(port->xmit_buf[port->xmit_tail++],
2262 port->ioaddr + UART_TX);
2263 port->xmit_tail = port->xmit_tail & (SERIAL_XMIT_SIZE - 1);
2264 if (--port->xmit_cnt <= 0)
Jiri Slaby037ad482006-12-08 02:38:11 -08002265 break;
2266 } while (--count > 0);
Jiri Slaby55b307d2006-12-08 02:38:14 -08002267 mxvar_log.txcnt[port->tty->index] += (cnt - port->xmit_cnt);
Jiri Slaby037ad482006-12-08 02:38:11 -08002268
Jiri Slaby55b307d2006-12-08 02:38:14 -08002269 port->mon_data.txcnt += (cnt - port->xmit_cnt);
2270 port->mon_data.up_txcnt += (cnt - port->xmit_cnt);
Jiri Slaby55b307d2006-12-08 02:38:14 -08002271 port->icount.tx += (cnt - port->xmit_cnt);
Jiri Slaby037ad482006-12-08 02:38:11 -08002272
Jiri Slaby47c85c02007-02-10 01:45:15 -08002273 if (port->xmit_cnt < WAKEUP_CHARS)
2274 tty_wakeup(port->tty);
2275
Jiri Slaby55b307d2006-12-08 02:38:14 -08002276 if (port->xmit_cnt <= 0) {
2277 port->IER &= ~UART_IER_THRI;
2278 outb(port->IER, port->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08002279 }
Jiri Slaby037ad482006-12-08 02:38:11 -08002280}
2281
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002282/*
2283 * This is the serial driver's generic interrupt routine
2284 */
Jiri Slabyb1d1c8d2006-12-08 02:38:31 -08002285static irqreturn_t mxser_interrupt(int irq, void *dev_id)
Jiri Slaby037ad482006-12-08 02:38:11 -08002286{
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002287 int status, iir, i;
2288 struct mxser_board *brd = NULL;
2289 struct mxser_port *port;
2290 int max, irqbits, bits, msr;
Jiri Slaby18b95572007-02-10 01:45:19 -08002291 unsigned int int_cnt, pass_counter = 0;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002292 int handled = IRQ_NONE;
Jiri Slaby037ad482006-12-08 02:38:11 -08002293
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002294 for (i = 0; i < MXSER_BOARDS; i++)
2295 if (dev_id == &mxser_boards[i]) {
2296 brd = dev_id;
2297 break;
Jiri Slaby037ad482006-12-08 02:38:11 -08002298 }
Jiri Slaby037ad482006-12-08 02:38:11 -08002299
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002300 if (i == MXSER_BOARDS)
2301 goto irq_stop;
2302 if (brd == NULL)
2303 goto irq_stop;
Jiri Slabycd7ed642006-12-08 02:38:29 -08002304 max = brd->info->nports;
Jiri Slaby18b95572007-02-10 01:45:19 -08002305 while (pass_counter++ < MXSER_ISR_PASS_LIMIT) {
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002306 irqbits = inb(brd->vector) & brd->vector_mask;
2307 if (irqbits == brd->vector_mask)
Jiri Slaby037ad482006-12-08 02:38:11 -08002308 break;
Jiri Slaby037ad482006-12-08 02:38:11 -08002309
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002310 handled = IRQ_HANDLED;
2311 for (i = 0, bits = 1; i < max; i++, irqbits |= bits, bits <<= 1) {
2312 if (irqbits == brd->vector_mask)
Jiri Slaby037ad482006-12-08 02:38:11 -08002313 break;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002314 if (bits & irqbits)
2315 continue;
2316 port = &brd->ports[i];
Jiri Slaby037ad482006-12-08 02:38:11 -08002317
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002318 int_cnt = 0;
Jiri Slabyf5748742007-02-10 01:45:18 -08002319 spin_lock(&port->slock);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002320 do {
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002321 iir = inb(port->ioaddr + UART_IIR);
2322 if (iir & UART_IIR_NO_INT)
2323 break;
2324 iir &= MOXA_MUST_IIR_MASK;
Jiri Slabyf5748742007-02-10 01:45:18 -08002325 if (!port->tty ||
2326 (port->flags & ASYNC_CLOSING) ||
2327 !(port->flags &
2328 ASYNC_INITIALIZED)) {
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002329 status = inb(port->ioaddr + UART_LSR);
2330 outb(0x27, port->ioaddr + UART_FCR);
2331 inb(port->ioaddr + UART_MSR);
2332 break;
Jiri Slaby037ad482006-12-08 02:38:11 -08002333 }
Jiri Slaby037ad482006-12-08 02:38:11 -08002334
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002335 status = inb(port->ioaddr + UART_LSR);
2336
2337 if (status & UART_LSR_PE)
2338 port->err_shadow |= NPPI_NOTIFY_PARITY;
2339 if (status & UART_LSR_FE)
2340 port->err_shadow |= NPPI_NOTIFY_FRAMING;
2341 if (status & UART_LSR_OE)
2342 port->err_shadow |=
2343 NPPI_NOTIFY_HW_OVERRUN;
2344 if (status & UART_LSR_BI)
2345 port->err_shadow |= NPPI_NOTIFY_BREAK;
2346
2347 if (port->board->chip_flag) {
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002348 if (iir == MOXA_MUST_IIR_GDA ||
2349 iir == MOXA_MUST_IIR_RDA ||
2350 iir == MOXA_MUST_IIR_RTO ||
2351 iir == MOXA_MUST_IIR_LSR)
2352 mxser_receive_chars(port,
2353 &status);
2354
2355 } else {
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002356 status &= port->read_status_mask;
2357 if (status & UART_LSR_DR)
2358 mxser_receive_chars(port,
2359 &status);
2360 }
2361 msr = inb(port->ioaddr + UART_MSR);
2362 if (msr & UART_MSR_ANY_DELTA)
2363 mxser_check_modem_status(port, msr);
2364
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002365 if (port->board->chip_flag) {
2366 if (iir == 0x02 && (status &
2367 UART_LSR_THRE))
2368 mxser_transmit_chars(port);
2369 } else {
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002370 if (status & UART_LSR_THRE)
2371 mxser_transmit_chars(port);
2372 }
2373 } while (int_cnt++ < MXSER_ISR_PASS_LIMIT);
Jiri Slabyf5748742007-02-10 01:45:18 -08002374 spin_unlock(&port->slock);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002375 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002376 }
2377
Jiri Slabye079f4952006-12-08 02:38:31 -08002378irq_stop:
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002379 return handled;
2380}
2381
2382static const struct tty_operations mxser_ops = {
2383 .open = mxser_open,
2384 .close = mxser_close,
2385 .write = mxser_write,
2386 .put_char = mxser_put_char,
2387 .flush_chars = mxser_flush_chars,
2388 .write_room = mxser_write_room,
2389 .chars_in_buffer = mxser_chars_in_buffer,
2390 .flush_buffer = mxser_flush_buffer,
2391 .ioctl = mxser_ioctl,
2392 .throttle = mxser_throttle,
2393 .unthrottle = mxser_unthrottle,
2394 .set_termios = mxser_set_termios,
2395 .stop = mxser_stop,
2396 .start = mxser_start,
2397 .hangup = mxser_hangup,
2398 .break_ctl = mxser_rs_break,
2399 .wait_until_sent = mxser_wait_until_sent,
2400 .tiocmget = mxser_tiocmget,
2401 .tiocmset = mxser_tiocmset,
2402};
2403
2404/*
2405 * The MOXA Smartio/Industio serial driver boot-time initialization code!
2406 */
2407
Jiri Slaby2094e752006-12-08 02:38:33 -08002408static void mxser_release_res(struct mxser_board *brd, struct pci_dev *pdev,
2409 unsigned int irq)
Jiri Slaby171d3a82006-12-08 02:38:25 -08002410{
Jiri Slaby171d3a82006-12-08 02:38:25 -08002411 if (irq)
2412 free_irq(brd->irq, brd);
2413 if (pdev != NULL) { /* PCI */
Jiri Slabyfb0c9292006-12-13 00:34:19 -08002414#ifdef CONFIG_PCI
Jiri Slaby171d3a82006-12-08 02:38:25 -08002415 pci_release_region(pdev, 2);
2416 pci_release_region(pdev, 3);
Jiri Slabyfb0c9292006-12-13 00:34:19 -08002417#endif
Jiri Slaby171d3a82006-12-08 02:38:25 -08002418 } else {
Jiri Slabycd7ed642006-12-08 02:38:29 -08002419 release_region(brd->ports[0].ioaddr, 8 * brd->info->nports);
Jiri Slaby171d3a82006-12-08 02:38:25 -08002420 release_region(brd->vector, 1);
2421 }
2422}
2423
Jiri Slaby2094e752006-12-08 02:38:33 -08002424static int __devinit mxser_initbrd(struct mxser_board *brd,
2425 struct pci_dev *pdev)
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002426{
2427 struct mxser_port *info;
2428 unsigned int i;
2429 int retval;
2430
2431 printk(KERN_INFO "max. baud rate = %d bps.\n", brd->ports[0].max_baud);
2432
Jiri Slabycd7ed642006-12-08 02:38:29 -08002433 for (i = 0; i < brd->info->nports; i++) {
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002434 info = &brd->ports[i];
2435 info->board = brd;
2436 info->stop_rx = 0;
2437 info->ldisc_stop_rx = 0;
2438
2439 /* Enhance mode enabled here */
2440 if (brd->chip_flag != MOXA_OTHER_UART)
2441 ENABLE_MOXA_MUST_ENCHANCE_MODE(info->ioaddr);
2442
2443 info->flags = ASYNC_SHARE_IRQ;
2444 info->type = brd->uart_type;
2445
2446 process_txrx_fifo(info);
2447
2448 info->custom_divisor = info->baud_base * 16;
2449 info->close_delay = 5 * HZ / 10;
2450 info->closing_wait = 30 * HZ;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002451 info->normal_termios = mxvar_sdriver->init_termios;
2452 init_waitqueue_head(&info->open_wait);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002453 init_waitqueue_head(&info->delta_msr_wait);
Jiri Slabyf64c84a2007-02-10 01:45:21 -08002454 info->speed = 9600;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002455 memset(&info->mon_data, 0, sizeof(struct mxser_mon));
2456 info->err_shadow = 0;
2457 spin_lock_init(&info->slock);
2458
2459 /* before set INT ISR, disable all int */
2460 outb(inb(info->ioaddr + UART_IER) & 0xf0,
2461 info->ioaddr + UART_IER);
2462 }
Jiri Slaby037ad482006-12-08 02:38:11 -08002463
Jiri Slabyc493edd2007-02-10 01:45:16 -08002464 retval = request_irq(brd->irq, mxser_interrupt, IRQF_SHARED, "mxser",
2465 brd);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002466 if (retval) {
2467 printk(KERN_ERR "Board %s: Request irq failed, IRQ (%d) may "
2468 "conflict with another device.\n",
Jiri Slabycd7ed642006-12-08 02:38:29 -08002469 brd->info->name, brd->irq);
Jiri Slaby171d3a82006-12-08 02:38:25 -08002470 /* We hold resources, we need to release them. */
Jiri Slaby2094e752006-12-08 02:38:33 -08002471 mxser_release_res(brd, pdev, 0);
Jiri Slaby037ad482006-12-08 02:38:11 -08002472 }
Jiri Slaby45257fa2007-02-10 01:45:20 -08002473 return retval;
Jiri Slaby037ad482006-12-08 02:38:11 -08002474}
2475
Jiri Slaby943f2952006-12-08 02:38:15 -08002476static int __init mxser_get_ISA_conf(int cap, struct mxser_board *brd)
Jiri Slaby037ad482006-12-08 02:38:11 -08002477{
2478 int id, i, bits;
2479 unsigned short regs[16], irq;
2480 unsigned char scratch, scratch2;
2481
Jiri Slaby55b307d2006-12-08 02:38:14 -08002482 brd->chip_flag = MOXA_OTHER_UART;
Jiri Slaby037ad482006-12-08 02:38:11 -08002483
2484 id = mxser_read_register(cap, regs);
Jiri Slabycd7ed642006-12-08 02:38:29 -08002485 switch (id) {
2486 case C168_ASIC_ID:
2487 brd->info = &mxser_cards[0];
2488 break;
2489 case C104_ASIC_ID:
2490 brd->info = &mxser_cards[1];
2491 break;
2492 case CI104J_ASIC_ID:
2493 brd->info = &mxser_cards[2];
2494 break;
2495 case C102_ASIC_ID:
2496 brd->info = &mxser_cards[5];
2497 break;
2498 case CI132_ASIC_ID:
2499 brd->info = &mxser_cards[6];
2500 break;
2501 case CI134_ASIC_ID:
2502 brd->info = &mxser_cards[7];
2503 break;
2504 default:
Jiri Slaby037ad482006-12-08 02:38:11 -08002505 return 0;
Jiri Slabycd7ed642006-12-08 02:38:29 -08002506 }
Jiri Slaby037ad482006-12-08 02:38:11 -08002507
2508 irq = 0;
Jiri Slabycd7ed642006-12-08 02:38:29 -08002509 /* some ISA cards have 2 ports, but we want to see them as 4-port (why?)
2510 Flag-hack checks if configuration should be read as 2-port here. */
2511 if (brd->info->nports == 2 || (brd->info->flags & MXSER_HAS2)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002512 irq = regs[9] & 0xF000;
2513 irq = irq | (irq >> 4);
2514 if (irq != (regs[9] & 0xFF00))
2515 return MXSER_ERR_IRQ_CONFLIT;
Jiri Slabycd7ed642006-12-08 02:38:29 -08002516 } else if (brd->info->nports == 4) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002517 irq = regs[9] & 0xF000;
2518 irq = irq | (irq >> 4);
2519 irq = irq | (irq >> 8);
2520 if (irq != regs[9])
2521 return MXSER_ERR_IRQ_CONFLIT;
Jiri Slabycd7ed642006-12-08 02:38:29 -08002522 } else if (brd->info->nports == 8) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002523 irq = regs[9] & 0xF000;
2524 irq = irq | (irq >> 4);
2525 irq = irq | (irq >> 8);
2526 if ((irq != regs[9]) || (irq != regs[10]))
2527 return MXSER_ERR_IRQ_CONFLIT;
2528 }
2529
2530 if (!irq)
2531 return MXSER_ERR_IRQ;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002532 brd->irq = ((int)(irq & 0xF000) >> 12);
Jiri Slaby037ad482006-12-08 02:38:11 -08002533 for (i = 0; i < 8; i++)
Jiri Slaby55b307d2006-12-08 02:38:14 -08002534 brd->ports[i].ioaddr = (int) regs[i + 1] & 0xFFF8;
Jiri Slaby037ad482006-12-08 02:38:11 -08002535 if ((regs[12] & 0x80) == 0)
2536 return MXSER_ERR_VECTOR;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002537 brd->vector = (int)regs[11]; /* interrupt vector */
Jiri Slaby037ad482006-12-08 02:38:11 -08002538 if (id == 1)
Jiri Slaby55b307d2006-12-08 02:38:14 -08002539 brd->vector_mask = 0x00FF;
Jiri Slaby037ad482006-12-08 02:38:11 -08002540 else
Jiri Slaby55b307d2006-12-08 02:38:14 -08002541 brd->vector_mask = 0x000F;
Jiri Slaby037ad482006-12-08 02:38:11 -08002542 for (i = 7, bits = 0x0100; i >= 0; i--, bits <<= 1) {
2543 if (regs[12] & bits) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002544 brd->ports[i].baud_base = 921600;
Jiri Slabye079f4952006-12-08 02:38:31 -08002545 brd->ports[i].max_baud = 921600;
Jiri Slaby037ad482006-12-08 02:38:11 -08002546 } else {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002547 brd->ports[i].baud_base = 115200;
Jiri Slabye079f4952006-12-08 02:38:31 -08002548 brd->ports[i].max_baud = 115200;
Jiri Slaby037ad482006-12-08 02:38:11 -08002549 }
2550 }
2551 scratch2 = inb(cap + UART_LCR) & (~UART_LCR_DLAB);
2552 outb(scratch2 | UART_LCR_DLAB, cap + UART_LCR);
2553 outb(0, cap + UART_EFR); /* EFR is the same as FCR */
2554 outb(scratch2, cap + UART_LCR);
2555 outb(UART_FCR_ENABLE_FIFO, cap + UART_FCR);
2556 scratch = inb(cap + UART_IIR);
2557
2558 if (scratch & 0xC0)
Jiri Slaby55b307d2006-12-08 02:38:14 -08002559 brd->uart_type = PORT_16550A;
Jiri Slaby037ad482006-12-08 02:38:11 -08002560 else
Jiri Slaby55b307d2006-12-08 02:38:14 -08002561 brd->uart_type = PORT_16450;
Jiri Slabycd7ed642006-12-08 02:38:29 -08002562 if (!request_region(brd->ports[0].ioaddr, 8 * brd->info->nports,
2563 "mxser(IO)"))
Jiri Slaby7a7a5c32006-12-08 02:38:17 -08002564 return MXSER_ERR_IOADDR;
2565 if (!request_region(brd->vector, 1, "mxser(vector)")) {
Jiri Slabycd7ed642006-12-08 02:38:29 -08002566 release_region(brd->ports[0].ioaddr, 8 * brd->info->nports);
Jiri Slaby7a7a5c32006-12-08 02:38:17 -08002567 return MXSER_ERR_VECTOR;
2568 }
Jiri Slabycd7ed642006-12-08 02:38:29 -08002569 return brd->info->nports;
Jiri Slaby037ad482006-12-08 02:38:11 -08002570}
2571
Jiri Slaby2094e752006-12-08 02:38:33 -08002572static int __devinit mxser_probe(struct pci_dev *pdev,
2573 const struct pci_device_id *ent)
Jiri Slaby037ad482006-12-08 02:38:11 -08002574{
Jiri Slabyfb0c9292006-12-13 00:34:19 -08002575#ifdef CONFIG_PCI
Jiri Slaby2094e752006-12-08 02:38:33 -08002576 struct mxser_board *brd;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002577 unsigned int i, j;
2578 unsigned long ioaddress;
Jiri Slaby2094e752006-12-08 02:38:33 -08002579 int retval = -EINVAL;
2580
2581 for (i = 0; i < MXSER_BOARDS; i++)
2582 if (mxser_boards[i].info == NULL)
2583 break;
2584
2585 if (i >= MXSER_BOARDS) {
2586 printk(KERN_ERR "Too many Smartio/Industio family boards found "
2587 "(maximum %d), board not configured\n", MXSER_BOARDS);
2588 goto err;
2589 }
2590
2591 brd = &mxser_boards[i];
2592 brd->idx = i * MXSER_PORTS_PER_BOARD;
2593 printk(KERN_INFO "Found MOXA %s board (BusNo=%d, DevNo=%d)\n",
2594 mxser_cards[ent->driver_data].name,
2595 pdev->bus->number, PCI_SLOT(pdev->devfn));
2596
2597 retval = pci_enable_device(pdev);
2598 if (retval) {
2599 printk(KERN_ERR "Moxa SmartI/O PCI enable fail !\n");
2600 goto err;
2601 }
Jiri Slaby037ad482006-12-08 02:38:11 -08002602
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002603 /* io address */
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002604 ioaddress = pci_resource_start(pdev, 2);
2605 retval = pci_request_region(pdev, 2, "mxser(IO)");
2606 if (retval)
2607 goto err;
2608
Jiri Slaby2094e752006-12-08 02:38:33 -08002609 brd->info = &mxser_cards[ent->driver_data];
Jiri Slabycd7ed642006-12-08 02:38:29 -08002610 for (i = 0; i < brd->info->nports; i++)
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002611 brd->ports[i].ioaddr = ioaddress + 8 * i;
2612
2613 /* vector */
2614 ioaddress = pci_resource_start(pdev, 3);
2615 retval = pci_request_region(pdev, 3, "mxser(vector)");
2616 if (retval)
2617 goto err_relio;
2618 brd->vector = ioaddress;
2619
2620 /* irq */
2621 brd->irq = pdev->irq;
2622
2623 brd->chip_flag = CheckIsMoxaMust(brd->ports[0].ioaddr);
2624 brd->uart_type = PORT_16550A;
2625 brd->vector_mask = 0;
2626
Jiri Slabycd7ed642006-12-08 02:38:29 -08002627 for (i = 0; i < brd->info->nports; i++) {
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002628 for (j = 0; j < UART_INFO_NUM; j++) {
2629 if (Gpci_uart_info[j].type == brd->chip_flag) {
2630 brd->ports[i].max_baud =
2631 Gpci_uart_info[j].max_baud;
2632
2633 /* exception....CP-102 */
Jiri Slabycd7ed642006-12-08 02:38:29 -08002634 if (brd->info->flags & MXSER_HIGHBAUD)
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002635 brd->ports[i].max_baud = 921600;
2636 break;
Jiri Slaby037ad482006-12-08 02:38:11 -08002637 }
2638 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002639 }
2640
2641 if (brd->chip_flag == MOXA_MUST_MU860_HWID) {
Jiri Slabycd7ed642006-12-08 02:38:29 -08002642 for (i = 0; i < brd->info->nports; i++) {
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002643 if (i < 4)
2644 brd->ports[i].opmode_ioaddr = ioaddress + 4;
2645 else
2646 brd->ports[i].opmode_ioaddr = ioaddress + 0x0c;
Jiri Slaby037ad482006-12-08 02:38:11 -08002647 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002648 outb(0, ioaddress + 4); /* default set to RS232 mode */
2649 outb(0, ioaddress + 0x0c); /* default set to RS232 mode */
Jiri Slaby037ad482006-12-08 02:38:11 -08002650 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002651
Jiri Slabycd7ed642006-12-08 02:38:29 -08002652 for (i = 0; i < brd->info->nports; i++) {
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002653 brd->vector_mask |= (1 << i);
2654 brd->ports[i].baud_base = 921600;
2655 }
Jiri Slaby2094e752006-12-08 02:38:33 -08002656
2657 /* mxser_initbrd will hook ISR. */
Jiri Slaby45257fa2007-02-10 01:45:20 -08002658 retval = mxser_initbrd(brd, pdev);
2659 if (retval)
2660 goto err_null;
Jiri Slaby2094e752006-12-08 02:38:33 -08002661
2662 for (i = 0; i < brd->info->nports; i++)
2663 tty_register_device(mxvar_sdriver, brd->idx + i, &pdev->dev);
2664
2665 pci_set_drvdata(pdev, brd);
2666
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002667 return 0;
2668err_relio:
2669 pci_release_region(pdev, 2);
Jiri Slaby45257fa2007-02-10 01:45:20 -08002670err_null:
Jiri Slaby2094e752006-12-08 02:38:33 -08002671 brd->info = NULL;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002672err:
2673 return retval;
Jiri Slabyfb0c9292006-12-13 00:34:19 -08002674#else
2675 return -ENODEV;
2676#endif
Jiri Slaby037ad482006-12-08 02:38:11 -08002677}
2678
Jiri Slaby2094e752006-12-08 02:38:33 -08002679static void __devexit mxser_remove(struct pci_dev *pdev)
2680{
2681 struct mxser_board *brd = pci_get_drvdata(pdev);
2682 unsigned int i;
2683
2684 for (i = 0; i < brd->info->nports; i++)
2685 tty_unregister_device(mxvar_sdriver, brd->idx + i);
2686
2687 mxser_release_res(brd, pdev, 1);
Jiri Slaby45257fa2007-02-10 01:45:20 -08002688 brd->info = NULL;
Jiri Slaby2094e752006-12-08 02:38:33 -08002689}
2690
2691static struct pci_driver mxser_driver = {
2692 .name = "mxser",
2693 .id_table = mxser_pcibrds,
2694 .probe = mxser_probe,
2695 .remove = __devexit_p(mxser_remove)
2696};
2697
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002698static int __init mxser_module_init(void)
Jiri Slaby037ad482006-12-08 02:38:11 -08002699{
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002700 struct mxser_board *brd;
Jiri Slabyeae44362006-12-08 02:38:27 -08002701 unsigned long cap;
2702 unsigned int i, m, isaloop;
Jiri Slaby2094e752006-12-08 02:38:33 -08002703 int retval, b;
Jiri Slaby037ad482006-12-08 02:38:11 -08002704
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002705 pr_debug("Loading module mxser ...\n");
Jiri Slaby037ad482006-12-08 02:38:11 -08002706
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002707 mxvar_sdriver = alloc_tty_driver(MXSER_PORTS + 1);
2708 if (!mxvar_sdriver)
2709 return -ENOMEM;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002710
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002711 printk(KERN_INFO "MOXA Smartio/Industio family driver version %s\n",
2712 MXSER_VERSION);
2713
2714 /* Initialize the tty_driver structure */
Jiri Slaby31f87cf2006-12-29 16:47:34 -08002715 mxvar_sdriver->owner = THIS_MODULE;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002716 mxvar_sdriver->magic = TTY_DRIVER_MAGIC;
Jiri Slabye9307902006-12-08 02:38:35 -08002717 mxvar_sdriver->name = "ttyMI";
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002718 mxvar_sdriver->major = ttymajor;
2719 mxvar_sdriver->minor_start = 0;
2720 mxvar_sdriver->num = MXSER_PORTS + 1;
2721 mxvar_sdriver->type = TTY_DRIVER_TYPE_SERIAL;
2722 mxvar_sdriver->subtype = SERIAL_TYPE_NORMAL;
2723 mxvar_sdriver->init_termios = tty_std_termios;
2724 mxvar_sdriver->init_termios.c_cflag = B9600|CS8|CREAD|HUPCL|CLOCAL;
Jiri Slaby938ef182006-12-08 02:38:28 -08002725 mxvar_sdriver->flags = TTY_DRIVER_REAL_RAW|TTY_DRIVER_DYNAMIC_DEV;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002726 tty_set_operations(mxvar_sdriver, &mxser_ops);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002727
Jiri Slaby938ef182006-12-08 02:38:28 -08002728 retval = tty_register_driver(mxvar_sdriver);
2729 if (retval) {
2730 printk(KERN_ERR "Couldn't install MOXA Smartio/Industio family "
2731 "tty driver !\n");
2732 goto err_put;
2733 }
2734
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002735 mxvar_diagflag = 0;
2736
2737 m = 0;
2738 /* Start finding ISA boards here */
Jiri Slabyeae44362006-12-08 02:38:27 -08002739 for (isaloop = 0; isaloop < 2; isaloop++)
2740 for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
2741 if (!isaloop)
2742 cap = mxserBoardCAP[b]; /* predefined */
2743 else
2744 cap = ioaddr[b]; /* module param */
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002745
Jiri Slabyeae44362006-12-08 02:38:27 -08002746 if (!cap)
2747 continue;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002748
Jiri Slabyeae44362006-12-08 02:38:27 -08002749 brd = &mxser_boards[m];
2750 retval = mxser_get_ISA_conf(cap, brd);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002751
Jiri Slabyeae44362006-12-08 02:38:27 -08002752 if (retval != 0)
2753 printk(KERN_INFO "Found MOXA %s board "
2754 "(CAP=0x%x)\n",
Jiri Slabycd7ed642006-12-08 02:38:29 -08002755 brd->info->name, ioaddr[b]);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002756
Jiri Slabyeae44362006-12-08 02:38:27 -08002757 if (retval <= 0) {
2758 if (retval == MXSER_ERR_IRQ)
2759 printk(KERN_ERR "Invalid interrupt "
2760 "number, board not "
2761 "configured\n");
2762 else if (retval == MXSER_ERR_IRQ_CONFLIT)
2763 printk(KERN_ERR "Invalid interrupt "
2764 "number, board not "
2765 "configured\n");
2766 else if (retval == MXSER_ERR_VECTOR)
2767 printk(KERN_ERR "Invalid interrupt "
2768 "vector, board not "
2769 "configured\n");
2770 else if (retval == MXSER_ERR_IOADDR)
2771 printk(KERN_ERR "Invalid I/O address, "
2772 "board not configured\n");
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002773
Jiri Slaby2094e752006-12-08 02:38:33 -08002774 brd->info = NULL;
Jiri Slabyeae44362006-12-08 02:38:27 -08002775 continue;
2776 }
2777
Jiri Slabyeae44362006-12-08 02:38:27 -08002778 /* mxser_initbrd will hook ISR. */
Jiri Slaby2094e752006-12-08 02:38:33 -08002779 if (mxser_initbrd(brd, NULL) < 0) {
2780 brd->info = NULL;
Jiri Slabyeae44362006-12-08 02:38:27 -08002781 continue;
Jiri Slaby2094e752006-12-08 02:38:33 -08002782 }
Jiri Slabyeae44362006-12-08 02:38:27 -08002783
Jiri Slaby2094e752006-12-08 02:38:33 -08002784 brd->idx = m * MXSER_PORTS_PER_BOARD;
Jiri Slabycd7ed642006-12-08 02:38:29 -08002785 for (i = 0; i < brd->info->nports; i++)
Jiri Slaby2094e752006-12-08 02:38:33 -08002786 tty_register_device(mxvar_sdriver, brd->idx + i,
2787 NULL);
Jiri Slaby938ef182006-12-08 02:38:28 -08002788
Jiri Slabyeae44362006-12-08 02:38:27 -08002789 m++;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002790 }
2791
Jiri Slaby2094e752006-12-08 02:38:33 -08002792 retval = pci_register_driver(&mxser_driver);
2793 if (retval) {
2794 printk(KERN_ERR "Can't register pci driver\n");
2795 if (!m) {
2796 retval = -ENODEV;
2797 goto err_unr;
2798 } /* else: we have some ISA cards under control */
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002799 }
2800
2801 pr_debug("Done.\n");
2802
Jiri Slaby938ef182006-12-08 02:38:28 -08002803 return 0;
2804err_unr:
2805 tty_unregister_driver(mxvar_sdriver);
2806err_put:
2807 put_tty_driver(mxvar_sdriver);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002808 return retval;
Jiri Slaby037ad482006-12-08 02:38:11 -08002809}
2810
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002811static void __exit mxser_module_exit(void)
Jiri Slaby037ad482006-12-08 02:38:11 -08002812{
Jiri Slaby2094e752006-12-08 02:38:33 -08002813 unsigned int i, j;
Jiri Slaby037ad482006-12-08 02:38:11 -08002814
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002815 pr_debug("Unloading module mxser ...\n");
2816
Jiri Slaby2094e752006-12-08 02:38:33 -08002817 pci_unregister_driver(&mxser_driver);
2818
2819 for (i = 0; i < MXSER_BOARDS; i++) /* ISA remains */
2820 if (mxser_boards[i].info != NULL)
2821 for (j = 0; j < mxser_boards[i].info->nports; j++)
2822 tty_unregister_device(mxvar_sdriver,
2823 mxser_boards[i].idx + j);
Jiri Slabyead568c2006-12-08 02:38:26 -08002824 tty_unregister_driver(mxvar_sdriver);
2825 put_tty_driver(mxvar_sdriver);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002826
Jiri Slaby171d3a82006-12-08 02:38:25 -08002827 for (i = 0; i < MXSER_BOARDS; i++)
Jiri Slabycd7ed642006-12-08 02:38:29 -08002828 if (mxser_boards[i].info != NULL)
Jiri Slaby2094e752006-12-08 02:38:33 -08002829 mxser_release_res(&mxser_boards[i], NULL, 1);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002830
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002831 pr_debug("Done.\n");
Jiri Slaby037ad482006-12-08 02:38:11 -08002832}
2833
2834module_init(mxser_module_init);
2835module_exit(mxser_module_exit);