blob: 285104d379cbf9734aafc72566dba32b24f5e670 [file] [log] [blame]
Jiri Slaby037ad482006-12-08 02:38:11 -08001/*
2 * mxser.c -- MOXA Smartio/Industio family multiport serial driver.
3 *
Jiri Slaby3306ce32006-12-08 02:38:13 -08004 * Copyright (C) 1999-2006 Moxa Technologies (support@moxa.com.tw).
Jiri Slabyc88cb8f92006-12-08 02:38:20 -08005 * Copyright (C) 2006 Jiri Slaby <jirislaby@gmail.com>
Jiri Slaby037ad482006-12-08 02:38:11 -08006 *
Jiri Slabyc88cb8f92006-12-08 02:38:20 -08007 * This code is loosely based on the 1.8 moxa driver which is based on
8 * Linux serial driver, written by Linus Torvalds, Theodore T'so and
9 * others.
Jiri Slaby037ad482006-12-08 02:38:11 -080010 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
Jiri Slaby037ad482006-12-08 02:38:11 -080016 * Fed through a cleanup, indent and remove of non 2.6 code by Alan Cox
17 * <alan@redhat.com>. The original 1.8 code is available on www.moxa.com.
18 * - Fixed x86_64 cleanness
19 * - Fixed sleep with spinlock held in mxser_send_break
20 */
21
Jiri Slaby037ad482006-12-08 02:38:11 -080022#include <linux/module.h>
23#include <linux/autoconf.h>
24#include <linux/errno.h>
25#include <linux/signal.h>
26#include <linux/sched.h>
27#include <linux/timer.h>
28#include <linux/interrupt.h>
29#include <linux/tty.h>
30#include <linux/tty_flip.h>
31#include <linux/serial.h>
32#include <linux/serial_reg.h>
33#include <linux/major.h>
34#include <linux/string.h>
35#include <linux/fcntl.h>
36#include <linux/ptrace.h>
37#include <linux/gfp.h>
38#include <linux/ioport.h>
39#include <linux/mm.h>
40#include <linux/smp_lock.h>
41#include <linux/delay.h>
42#include <linux/pci.h>
43
44#include <asm/system.h>
45#include <asm/io.h>
46#include <asm/irq.h>
47#include <asm/bitops.h>
48#include <asm/uaccess.h>
49
Jiri Slaby771f2d12006-12-08 02:38:12 -080050#include "mxser_new.h"
Jiri Slaby037ad482006-12-08 02:38:11 -080051
Jiri Slaby55b307d2006-12-08 02:38:14 -080052#define MXSER_VERSION "2.0"
Jiri Slaby037ad482006-12-08 02:38:11 -080053#define MXSERMAJOR 174
54#define MXSERCUMAJOR 175
55
56#define MXSER_EVENT_TXLOW 1
57#define MXSER_EVENT_HANGUP 2
58
59#define MXSER_BOARDS 4 /* Max. boards */
60#define MXSER_PORTS 32 /* Max. ports */
61#define MXSER_PORTS_PER_BOARD 8 /* Max. ports per board */
Jiri Slaby3306ce32006-12-08 02:38:13 -080062#define MXSER_ISR_PASS_LIMIT 99999L
Jiri Slaby037ad482006-12-08 02:38:11 -080063
64#define MXSER_ERR_IOADDR -1
65#define MXSER_ERR_IRQ -2
66#define MXSER_ERR_IRQ_CONFLIT -3
67#define MXSER_ERR_VECTOR -4
68
69#define SERIAL_TYPE_NORMAL 1
70#define SERIAL_TYPE_CALLOUT 2
71
72#define WAKEUP_CHARS 256
73
74#define UART_MCR_AFE 0x20
75#define UART_LSR_SPECIAL 0x1E
76
77#define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK|\
78 IXON|IXOFF))
79
Jiri Slaby037ad482006-12-08 02:38:11 -080080#define C168_ASIC_ID 1
81#define C104_ASIC_ID 2
82#define C102_ASIC_ID 0xB
83#define CI132_ASIC_ID 4
84#define CI134_ASIC_ID 3
85#define CI104J_ASIC_ID 5
86
Jiri Slabycd7ed642006-12-08 02:38:29 -080087#define MXSER_HIGHBAUD 1
88#define MXSER_HAS2 2
Jiri Slaby037ad482006-12-08 02:38:11 -080089
90/* This is only for PCI */
Jiri Slabycd7ed642006-12-08 02:38:29 -080091static const struct {
Jiri Slaby037ad482006-12-08 02:38:11 -080092 int type;
93 int tx_fifo;
94 int rx_fifo;
95 int xmit_fifo_size;
96 int rx_high_water;
97 int rx_trigger;
98 int rx_low_water;
99 long max_baud;
Jiri Slabycd7ed642006-12-08 02:38:29 -0800100} Gpci_uart_info[] = {
Jiri Slaby037ad482006-12-08 02:38:11 -0800101 {MOXA_OTHER_UART, 16, 16, 16, 14, 14, 1, 921600L},
102 {MOXA_MUST_MU150_HWID, 64, 64, 64, 48, 48, 16, 230400L},
103 {MOXA_MUST_MU860_HWID, 128, 128, 128, 96, 96, 32, 921600L}
104};
Jiri Slabycd7ed642006-12-08 02:38:29 -0800105#define UART_INFO_NUM ARRAY_SIZE(Gpci_uart_info)
Jiri Slaby037ad482006-12-08 02:38:11 -0800106
Jiri Slabycd7ed642006-12-08 02:38:29 -0800107struct mxser_cardinfo {
108 unsigned int nports;
109 char *name;
110 unsigned int flags;
111};
112
113static const struct mxser_cardinfo mxser_cards[] = {
114 { 8, "C168 series", }, /* C168-ISA */
115 { 4, "C104 series", }, /* C104-ISA */
116 { 4, "CI-104J series", }, /* CI104J */
117 { 8, "C168H/PCI series", }, /* C168-PCI */
118 { 4, "C104H/PCI series", }, /* C104-PCI */
119 { 4, "C102 series", MXSER_HAS2 }, /* C102-ISA */
120 { 4, "CI-132 series", MXSER_HAS2 }, /* CI132 */
121 { 4, "CI-134 series", }, /* CI134 */
122 { 2, "CP-132 series", }, /* CP132 */
123 { 4, "CP-114 series", }, /* CP114 */
124 { 4, "CT-114 series", }, /* CT114 */
125 { 2, "CP-102 series", MXSER_HIGHBAUD }, /* CP102 */
126 { 4, "CP-104U series", }, /* CP104U */
127 { 8, "CP-168U series", }, /* CP168U */
128 { 2, "CP-132U series", }, /* CP132U */
129 { 4, "CP-134U series", }, /* CP134U */
130 { 4, "CP-104JU series", }, /* CP104JU */
131 { 8, "Moxa UC7000 Serial", }, /* RC7000 */
132 { 8, "CP-118U series", }, /* CP118U */
133 { 2, "CP-102UL series", }, /* CP102UL */
134 { 2, "CP-102U series", }, /* CP102U */
135 { 8, "CP-118EL series", }, /* CP118EL */
136 { 8, "CP-168EL series", }, /* CP168EL */
137 { 4, "CP-104EL series", } /* CP104EL */
138};
139
140/* driver_data correspond to the lines in the structure above
141 see also ISA probe function before you change something */
Jiri Slaby037ad482006-12-08 02:38:11 -0800142static struct pci_device_id mxser_pcibrds[] = {
Jiri Slaby3306ce32006-12-08 02:38:13 -0800143 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C168),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800144 .driver_data = 3 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800145 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C104),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800146 .driver_data = 4 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800147 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP132),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800148 .driver_data = 8 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800149 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP114),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800150 .driver_data = 9 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800151 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CT114),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800152 .driver_data = 10 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800153 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP102),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800154 .driver_data = 11 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800155 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP104U),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800156 .driver_data = 12 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800157 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP168U),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800158 .driver_data = 13 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800159 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP132U),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800160 .driver_data = 14 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800161 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP134U),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800162 .driver_data = 15 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800163 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP104JU),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800164 .driver_data = 16 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800165 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_RC7000),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800166 .driver_data = 17 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800167 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP118U),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800168 .driver_data = 18 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800169 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP102UL),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800170 .driver_data = 19 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800171 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP102U),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800172 .driver_data = 20 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800173 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP118EL),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800174 .driver_data = 21 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800175 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP168EL),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800176 .driver_data = 22 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800177 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP104EL),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800178 .driver_data = 23 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800179 { }
Jiri Slaby037ad482006-12-08 02:38:11 -0800180};
Jiri Slaby037ad482006-12-08 02:38:11 -0800181MODULE_DEVICE_TABLE(pci, mxser_pcibrds);
182
Jiri Slaby037ad482006-12-08 02:38:11 -0800183static int ioaddr[MXSER_BOARDS] = { 0, 0, 0, 0 };
184static int ttymajor = MXSERMAJOR;
185static int calloutmajor = MXSERCUMAJOR;
Jiri Slaby037ad482006-12-08 02:38:11 -0800186
187/* Variables for insmod */
188
189MODULE_AUTHOR("Casper Yang");
190MODULE_DESCRIPTION("MOXA Smartio/Industio Family Multiport Board Device Driver");
191module_param_array(ioaddr, int, NULL, 0);
192module_param(ttymajor, int, 0);
Jiri Slaby037ad482006-12-08 02:38:11 -0800193MODULE_LICENSE("GPL");
194
195struct mxser_log {
196 int tick;
197 unsigned long rxcnt[MXSER_PORTS];
198 unsigned long txcnt[MXSER_PORTS];
199};
200
201
202struct mxser_mon {
203 unsigned long rxcnt;
204 unsigned long txcnt;
205 unsigned long up_rxcnt;
206 unsigned long up_txcnt;
207 int modem_status;
208 unsigned char hold_reason;
209};
210
211struct mxser_mon_ext {
212 unsigned long rx_cnt[32];
213 unsigned long tx_cnt[32];
214 unsigned long up_rxcnt[32];
215 unsigned long up_txcnt[32];
216 int modem_status[32];
217
218 long baudrate[32];
219 int databits[32];
220 int stopbits[32];
221 int parity[32];
222 int flowctrl[32];
223 int fifo[32];
224 int iftype[32];
225};
226
Jiri Slaby55b307d2006-12-08 02:38:14 -0800227struct mxser_board;
Jiri Slaby037ad482006-12-08 02:38:11 -0800228
Jiri Slaby55b307d2006-12-08 02:38:14 -0800229struct mxser_port {
230 struct mxser_board *board;
231 struct tty_struct *tty;
232
233 unsigned long ioaddr;
234 unsigned long opmode_ioaddr;
235 int max_baud;
236
Jiri Slaby037ad482006-12-08 02:38:11 -0800237 int rx_high_water;
238 int rx_trigger; /* Rx fifo trigger level */
239 int rx_low_water;
240 int baud_base; /* max. speed */
Jiri Slaby55b307d2006-12-08 02:38:14 -0800241 long realbaud;
Jiri Slaby037ad482006-12-08 02:38:11 -0800242 int type; /* UART type */
Jiri Slaby55b307d2006-12-08 02:38:14 -0800243 int flags; /* defined in tty.h */
244 long session; /* Session of opening process */
245 long pgrp; /* pgrp of opening process */
246
247 int x_char; /* xon/xoff character */
248 int IER; /* Interrupt Enable Register */
249 int MCR; /* Modem control register */
250
251 unsigned char stop_rx;
252 unsigned char ldisc_stop_rx;
253
254 int custom_divisor;
255 int close_delay;
256 unsigned short closing_wait;
257 unsigned char err_shadow;
258 unsigned long event;
259
260 int count; /* # of fd on device */
261 int blocked_open; /* # of blocked opens */
262 struct async_icount icount; /* kernel counters for 4 input interrupts */
263 int timeout;
264
Jiri Slaby037ad482006-12-08 02:38:11 -0800265 int read_status_mask;
266 int ignore_status_mask;
267 int xmit_fifo_size;
Jiri Slaby037ad482006-12-08 02:38:11 -0800268 unsigned char *xmit_buf;
269 int xmit_head;
270 int xmit_tail;
271 int xmit_cnt;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800272
Jiri Slaby037ad482006-12-08 02:38:11 -0800273 struct termios normal_termios;
274 struct termios callout_termios;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800275
276 struct mxser_mon mon_data;
277
278 spinlock_t slock;
279 struct work_struct tqueue;
Jiri Slaby037ad482006-12-08 02:38:11 -0800280 wait_queue_head_t open_wait;
281 wait_queue_head_t close_wait;
282 wait_queue_head_t delta_msr_wait;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800283};
284
285struct mxser_board {
286 struct pci_dev *pdev; /* temporary (until pci probing) */
287
288 int irq;
Jiri Slabycd7ed642006-12-08 02:38:29 -0800289 const struct mxser_cardinfo *info;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800290 unsigned long vector;
291 unsigned long vector_mask;
292
293 int chip_flag;
294 int uart_type;
295
296 struct mxser_port ports[MXSER_PORTS_PER_BOARD];
Jiri Slaby037ad482006-12-08 02:38:11 -0800297};
298
299struct mxser_mstatus {
300 tcflag_t cflag;
301 int cts;
302 int dsr;
303 int ri;
304 int dcd;
305};
306
307static struct mxser_mstatus GMStatus[MXSER_PORTS];
308
309static int mxserBoardCAP[MXSER_BOARDS] = {
310 0, 0, 0, 0
311 /* 0x180, 0x280, 0x200, 0x320 */
312};
313
Jiri Slaby55b307d2006-12-08 02:38:14 -0800314static struct mxser_board mxser_boards[MXSER_BOARDS];
Jiri Slaby037ad482006-12-08 02:38:11 -0800315static struct tty_driver *mxvar_sdriver;
Jiri Slaby037ad482006-12-08 02:38:11 -0800316static struct tty_struct *mxvar_tty[MXSER_PORTS + 1];
317static struct termios *mxvar_termios[MXSER_PORTS + 1];
318static struct termios *mxvar_termios_locked[MXSER_PORTS + 1];
319static struct mxser_log mxvar_log;
320static int mxvar_diagflag;
321static unsigned char mxser_msr[MXSER_PORTS + 1];
322static struct mxser_mon_ext mon_data_ext;
323static int mxser_set_baud_method[MXSER_PORTS + 1];
324static spinlock_t gm_lock;
325
Jiri Slaby037ad482006-12-08 02:38:11 -0800326static int CheckIsMoxaMust(int io)
327{
328 u8 oldmcr, hwid;
329 int i;
330
331 outb(0, io + UART_LCR);
332 DISABLE_MOXA_MUST_ENCHANCE_MODE(io);
333 oldmcr = inb(io + UART_MCR);
334 outb(0, io + UART_MCR);
335 SET_MOXA_MUST_XON1_VALUE(io, 0x11);
336 if ((hwid = inb(io + UART_MCR)) != 0) {
337 outb(oldmcr, io + UART_MCR);
338 return MOXA_OTHER_UART;
339 }
340
341 GET_MOXA_MUST_HARDWARE_ID(io, &hwid);
Jiri Slabycd7ed642006-12-08 02:38:29 -0800342 for (i = 1; i < UART_INFO_NUM; i++) { /* 0 = OTHER_UART */
343 if (hwid == Gpci_uart_info[i].type)
Jiri Slaby037ad482006-12-08 02:38:11 -0800344 return (int)hwid;
345 }
346 return MOXA_OTHER_UART;
347}
348
349/* above is modified by Victor Yu. 08-15-2002 */
350
Jiri Slaby55b307d2006-12-08 02:38:14 -0800351static void process_txrx_fifo(struct mxser_port *info)
Jiri Slaby037ad482006-12-08 02:38:11 -0800352{
353 int i;
354
355 if ((info->type == PORT_16450) || (info->type == PORT_8250)) {
356 info->rx_trigger = 1;
357 info->rx_high_water = 1;
358 info->rx_low_water = 1;
359 info->xmit_fifo_size = 1;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800360 } else
361 for (i = 0; i < UART_INFO_NUM; i++)
362 if (info->board->chip_flag == Gpci_uart_info[i].type) {
Jiri Slaby037ad482006-12-08 02:38:11 -0800363 info->rx_trigger = Gpci_uart_info[i].rx_trigger;
364 info->rx_low_water = Gpci_uart_info[i].rx_low_water;
365 info->rx_high_water = Gpci_uart_info[i].rx_high_water;
366 info->xmit_fifo_size = Gpci_uart_info[i].xmit_fifo_size;
367 break;
368 }
Jiri Slaby037ad482006-12-08 02:38:11 -0800369}
370
Jiri Slaby037ad482006-12-08 02:38:11 -0800371static void mxser_do_softint(void *private_)
372{
Jiri Slaby55b307d2006-12-08 02:38:14 -0800373 struct mxser_port *info = private_;
Jiri Slaby037ad482006-12-08 02:38:11 -0800374 struct tty_struct *tty;
375
376 tty = info->tty;
377
Jiri Slaby3306ce32006-12-08 02:38:13 -0800378 if (test_and_clear_bit(MXSER_EVENT_TXLOW, &info->event))
379 tty_wakeup(tty);
380 if (test_and_clear_bit(MXSER_EVENT_HANGUP, &info->event))
381 tty_hangup(tty);
Jiri Slaby037ad482006-12-08 02:38:11 -0800382}
383
Jiri Slaby55b307d2006-12-08 02:38:14 -0800384static unsigned char mxser_get_msr(int baseaddr, int mode, int port)
Jiri Slaby037ad482006-12-08 02:38:11 -0800385{
386 unsigned char status = 0;
387
388 status = inb(baseaddr + UART_MSR);
389
390 mxser_msr[port] &= 0x0F;
391 mxser_msr[port] |= status;
392 status = mxser_msr[port];
393 if (mode)
394 mxser_msr[port] = 0;
395
396 return status;
397}
398
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800399static int mxser_block_til_ready(struct tty_struct *tty, struct file *filp,
400 struct mxser_port *port)
401{
402 DECLARE_WAITQUEUE(wait, current);
403 int retval;
404 int do_clocal = 0;
405 unsigned long flags;
406
407 /*
408 * If non-blocking mode is set, or the port is not enabled,
409 * then make the check up front and then exit.
410 */
Jiri Slaby214efeb2006-12-08 02:38:25 -0800411 if ((filp->f_flags & O_NONBLOCK) ||
412 test_bit(TTY_IO_ERROR, &tty->flags)) {
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800413 port->flags |= ASYNC_NORMAL_ACTIVE;
414 return 0;
415 }
416
417 if (tty->termios->c_cflag & CLOCAL)
418 do_clocal = 1;
419
420 /*
421 * Block waiting for the carrier detect and the line to become
422 * free (i.e., not in use by the callout). While we are in
423 * this loop, port->count is dropped by one, so that
424 * mxser_close() knows when to free things. We restore it upon
425 * exit, either normal or abnormal.
426 */
427 retval = 0;
428 add_wait_queue(&port->open_wait, &wait);
429
430 spin_lock_irqsave(&port->slock, flags);
431 if (!tty_hung_up_p(filp))
432 port->count--;
433 spin_unlock_irqrestore(&port->slock, flags);
434 port->blocked_open++;
435 while (1) {
436 spin_lock_irqsave(&port->slock, flags);
437 outb(inb(port->ioaddr + UART_MCR) |
438 UART_MCR_DTR | UART_MCR_RTS, port->ioaddr + UART_MCR);
439 spin_unlock_irqrestore(&port->slock, flags);
440 set_current_state(TASK_INTERRUPTIBLE);
441 if (tty_hung_up_p(filp) || !(port->flags & ASYNC_INITIALIZED)) {
442 if (port->flags & ASYNC_HUP_NOTIFY)
443 retval = -EAGAIN;
444 else
445 retval = -ERESTARTSYS;
446 break;
447 }
448 if (!(port->flags & ASYNC_CLOSING) &&
449 (do_clocal ||
450 (inb(port->ioaddr + UART_MSR) & UART_MSR_DCD)))
451 break;
452 if (signal_pending(current)) {
453 retval = -ERESTARTSYS;
454 break;
455 }
456 schedule();
457 }
458 set_current_state(TASK_RUNNING);
459 remove_wait_queue(&port->open_wait, &wait);
460 if (!tty_hung_up_p(filp))
461 port->count++;
462 port->blocked_open--;
463 if (retval)
464 return retval;
465 port->flags |= ASYNC_NORMAL_ACTIVE;
466 return 0;
467}
468
469static int mxser_set_baud(struct mxser_port *info, long newspd)
470{
471 int quot = 0;
472 unsigned char cval;
473 int ret = 0;
474 unsigned long flags;
475
476 if (!info->tty || !info->tty->termios)
477 return ret;
478
479 if (!(info->ioaddr))
480 return ret;
481
482 if (newspd > info->max_baud)
483 return 0;
484
485 info->realbaud = newspd;
486 if (newspd == 134) {
487 quot = (2 * info->baud_base / 269);
488 } else if (newspd) {
489 quot = info->baud_base / newspd;
490 if (quot == 0)
491 quot = 1;
492 } else {
493 quot = 0;
494 }
495
496 info->timeout = ((info->xmit_fifo_size * HZ * 10 * quot) / info->baud_base);
497 info->timeout += HZ / 50; /* Add .02 seconds of slop */
498
499 if (quot) {
500 spin_lock_irqsave(&info->slock, flags);
501 info->MCR |= UART_MCR_DTR;
502 outb(info->MCR, info->ioaddr + UART_MCR);
503 spin_unlock_irqrestore(&info->slock, flags);
504 } else {
505 spin_lock_irqsave(&info->slock, flags);
506 info->MCR &= ~UART_MCR_DTR;
507 outb(info->MCR, info->ioaddr + UART_MCR);
508 spin_unlock_irqrestore(&info->slock, flags);
509 return ret;
510 }
511
512 cval = inb(info->ioaddr + UART_LCR);
513
514 outb(cval | UART_LCR_DLAB, info->ioaddr + UART_LCR); /* set DLAB */
515
516 outb(quot & 0xff, info->ioaddr + UART_DLL); /* LS of divisor */
517 outb(quot >> 8, info->ioaddr + UART_DLM); /* MS of divisor */
518 outb(cval, info->ioaddr + UART_LCR); /* reset DLAB */
519
520
521 return ret;
522}
523
524/*
525 * This routine is called to set the UART divisor registers to match
526 * the specified baud rate for a serial port.
527 */
528static int mxser_change_speed(struct mxser_port *info,
529 struct termios *old_termios)
530{
531 unsigned cflag, cval, fcr;
532 int ret = 0;
533 unsigned char status;
534 long baud;
535 unsigned long flags;
536
537 if (!info->tty || !info->tty->termios)
538 return ret;
539 cflag = info->tty->termios->c_cflag;
540 if (!(info->ioaddr))
541 return ret;
542
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800543 if (mxser_set_baud_method[info->tty->index] == 0) {
544 baud = tty_get_baud_rate(info->tty);
545 mxser_set_baud(info, baud);
546 }
547
548 /* byte size and parity */
549 switch (cflag & CSIZE) {
550 case CS5:
551 cval = 0x00;
552 break;
553 case CS6:
554 cval = 0x01;
555 break;
556 case CS7:
557 cval = 0x02;
558 break;
559 case CS8:
560 cval = 0x03;
561 break;
562 default:
563 cval = 0x00;
564 break; /* too keep GCC shut... */
565 }
566 if (cflag & CSTOPB)
567 cval |= 0x04;
568 if (cflag & PARENB)
569 cval |= UART_LCR_PARITY;
570 if (!(cflag & PARODD))
571 cval |= UART_LCR_EPAR;
572 if (cflag & CMSPAR)
573 cval |= UART_LCR_SPAR;
574
575 if ((info->type == PORT_8250) || (info->type == PORT_16450)) {
576 if (info->board->chip_flag) {
577 fcr = UART_FCR_ENABLE_FIFO;
578 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
579 SET_MOXA_MUST_FIFO_VALUE(info);
580 } else
581 fcr = 0;
582 } else {
583 fcr = UART_FCR_ENABLE_FIFO;
584 /* following add by Victor Yu. 08-30-2002 */
585 if (info->board->chip_flag) {
586 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
587 SET_MOXA_MUST_FIFO_VALUE(info);
588 } else {
589 /* above add by Victor Yu. 08-30-2002 */
590 switch (info->rx_trigger) {
591 case 1:
592 fcr |= UART_FCR_TRIGGER_1;
593 break;
594 case 4:
595 fcr |= UART_FCR_TRIGGER_4;
596 break;
597 case 8:
598 fcr |= UART_FCR_TRIGGER_8;
599 break;
600 default:
601 fcr |= UART_FCR_TRIGGER_14;
602 break;
603 }
604 }
605 }
606
607 /* CTS flow control flag and modem status interrupts */
608 info->IER &= ~UART_IER_MSI;
609 info->MCR &= ~UART_MCR_AFE;
610 if (cflag & CRTSCTS) {
611 info->flags |= ASYNC_CTS_FLOW;
612 info->IER |= UART_IER_MSI;
613 if ((info->type == PORT_16550A) || (info->board->chip_flag)) {
614 info->MCR |= UART_MCR_AFE;
615/* status = mxser_get_msr(info->ioaddr, 0, info->port); */
616/*
617 save_flags(flags);
618 cli();
619 status = inb(baseaddr + UART_MSR);
620 restore_flags(flags);
621*/
622 /* mxser_check_modem_status(info, status); */
623 } else {
624/* status = mxser_get_msr(info->ioaddr, 0, info->port); */
625 /* MX_LOCK(&info->slock); */
626 status = inb(info->ioaddr + UART_MSR);
627 /* MX_UNLOCK(&info->slock); */
628 if (info->tty->hw_stopped) {
629 if (status & UART_MSR_CTS) {
630 info->tty->hw_stopped = 0;
631 if (info->type != PORT_16550A &&
632 !info->board->chip_flag) {
633 outb(info->IER & ~UART_IER_THRI,
634 info->ioaddr +
635 UART_IER);
636 info->IER |= UART_IER_THRI;
637 outb(info->IER, info->ioaddr +
638 UART_IER);
639 }
640 set_bit(MXSER_EVENT_TXLOW, &info->event);
641 schedule_work(&info->tqueue); }
642 } else {
643 if (!(status & UART_MSR_CTS)) {
644 info->tty->hw_stopped = 1;
645 if ((info->type != PORT_16550A) &&
646 (!info->board->chip_flag)) {
647 info->IER &= ~UART_IER_THRI;
648 outb(info->IER, info->ioaddr +
649 UART_IER);
650 }
651 }
652 }
653 }
654 } else {
655 info->flags &= ~ASYNC_CTS_FLOW;
656 }
657 outb(info->MCR, info->ioaddr + UART_MCR);
658 if (cflag & CLOCAL) {
659 info->flags &= ~ASYNC_CHECK_CD;
660 } else {
661 info->flags |= ASYNC_CHECK_CD;
662 info->IER |= UART_IER_MSI;
663 }
664 outb(info->IER, info->ioaddr + UART_IER);
665
666 /*
667 * Set up parity check flag
668 */
669 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
670 if (I_INPCK(info->tty))
671 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
672 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
673 info->read_status_mask |= UART_LSR_BI;
674
675 info->ignore_status_mask = 0;
676
677 if (I_IGNBRK(info->tty)) {
678 info->ignore_status_mask |= UART_LSR_BI;
679 info->read_status_mask |= UART_LSR_BI;
680 /*
681 * If we're ignore parity and break indicators, ignore
682 * overruns too. (For real raw support).
683 */
684 if (I_IGNPAR(info->tty)) {
685 info->ignore_status_mask |=
686 UART_LSR_OE |
687 UART_LSR_PE |
688 UART_LSR_FE;
689 info->read_status_mask |=
690 UART_LSR_OE |
691 UART_LSR_PE |
692 UART_LSR_FE;
693 }
694 }
695 /* following add by Victor Yu. 09-02-2002 */
696 if (info->board->chip_flag) {
697 spin_lock_irqsave(&info->slock, flags);
698 SET_MOXA_MUST_XON1_VALUE(info->ioaddr, START_CHAR(info->tty));
699 SET_MOXA_MUST_XOFF1_VALUE(info->ioaddr, STOP_CHAR(info->tty));
700 if (I_IXON(info->tty)) {
701 ENABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
702 } else {
703 DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
704 }
705 if (I_IXOFF(info->tty)) {
706 ENABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
707 } else {
708 DISABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
709 }
710 /*
711 if ( I_IXANY(info->tty) ) {
712 info->MCR |= MOXA_MUST_MCR_XON_ANY;
713 ENABLE_MOXA_MUST_XON_ANY_FLOW_CONTROL(info->ioaddr);
714 } else {
715 info->MCR &= ~MOXA_MUST_MCR_XON_ANY;
716 DISABLE_MOXA_MUST_XON_ANY_FLOW_CONTROL(info->ioaddr);
717 }
718 */
719 spin_unlock_irqrestore(&info->slock, flags);
720 }
721 /* above add by Victor Yu. 09-02-2002 */
722
723
724 outb(fcr, info->ioaddr + UART_FCR); /* set fcr */
725 outb(cval, info->ioaddr + UART_LCR);
726
727 return ret;
728}
729
730static void mxser_check_modem_status(struct mxser_port *port, int status)
731{
732 /* update input line counters */
733 if (status & UART_MSR_TERI)
734 port->icount.rng++;
735 if (status & UART_MSR_DDSR)
736 port->icount.dsr++;
737 if (status & UART_MSR_DDCD)
738 port->icount.dcd++;
739 if (status & UART_MSR_DCTS)
740 port->icount.cts++;
741 port->mon_data.modem_status = status;
742 wake_up_interruptible(&port->delta_msr_wait);
743
744 if ((port->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
745 if (status & UART_MSR_DCD)
746 wake_up_interruptible(&port->open_wait);
747 schedule_work(&port->tqueue);
748 }
749
750 if (port->flags & ASYNC_CTS_FLOW) {
751 if (port->tty->hw_stopped) {
752 if (status & UART_MSR_CTS) {
753 port->tty->hw_stopped = 0;
754
755 if ((port->type != PORT_16550A) &&
756 (!port->board->chip_flag)) {
757 outb(port->IER & ~UART_IER_THRI,
758 port->ioaddr + UART_IER);
759 port->IER |= UART_IER_THRI;
760 outb(port->IER, port->ioaddr +
761 UART_IER);
762 }
763 set_bit(MXSER_EVENT_TXLOW, &port->event);
764 schedule_work(&port->tqueue);
765 }
766 } else {
767 if (!(status & UART_MSR_CTS)) {
768 port->tty->hw_stopped = 1;
769 if (port->type != PORT_16550A &&
770 !port->board->chip_flag) {
771 port->IER &= ~UART_IER_THRI;
772 outb(port->IER, port->ioaddr +
773 UART_IER);
774 }
775 }
776 }
777 }
778}
779
780static int mxser_startup(struct mxser_port *info)
781{
782 unsigned long page;
783 unsigned long flags;
784
785 page = __get_free_page(GFP_KERNEL);
786 if (!page)
787 return -ENOMEM;
788
789 spin_lock_irqsave(&info->slock, flags);
790
791 if (info->flags & ASYNC_INITIALIZED) {
792 free_page(page);
793 spin_unlock_irqrestore(&info->slock, flags);
794 return 0;
795 }
796
797 if (!info->ioaddr || !info->type) {
798 if (info->tty)
799 set_bit(TTY_IO_ERROR, &info->tty->flags);
800 free_page(page);
801 spin_unlock_irqrestore(&info->slock, flags);
802 return 0;
803 }
804 if (info->xmit_buf)
805 free_page(page);
806 else
807 info->xmit_buf = (unsigned char *) page;
808
809 /*
810 * Clear the FIFO buffers and disable them
811 * (they will be reenabled in mxser_change_speed())
812 */
813 if (info->board->chip_flag)
814 outb((UART_FCR_CLEAR_RCVR |
815 UART_FCR_CLEAR_XMIT |
816 MOXA_MUST_FCR_GDA_MODE_ENABLE), info->ioaddr + UART_FCR);
817 else
818 outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
819 info->ioaddr + UART_FCR);
820
821 /*
822 * At this point there's no way the LSR could still be 0xFF;
823 * if it is, then bail out, because there's likely no UART
824 * here.
825 */
826 if (inb(info->ioaddr + UART_LSR) == 0xff) {
827 spin_unlock_irqrestore(&info->slock, flags);
828 if (capable(CAP_SYS_ADMIN)) {
829 if (info->tty)
830 set_bit(TTY_IO_ERROR, &info->tty->flags);
831 return 0;
832 } else
833 return -ENODEV;
834 }
835
836 /*
837 * Clear the interrupt registers.
838 */
839 (void) inb(info->ioaddr + UART_LSR);
840 (void) inb(info->ioaddr + UART_RX);
841 (void) inb(info->ioaddr + UART_IIR);
842 (void) inb(info->ioaddr + UART_MSR);
843
844 /*
845 * Now, initialize the UART
846 */
847 outb(UART_LCR_WLEN8, info->ioaddr + UART_LCR); /* reset DLAB */
848 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
849 outb(info->MCR, info->ioaddr + UART_MCR);
850
851 /*
852 * Finally, enable interrupts
853 */
854 info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
855 /* info->IER = UART_IER_RLSI | UART_IER_RDI; */
856
857 /* following add by Victor Yu. 08-30-2002 */
858 if (info->board->chip_flag)
859 info->IER |= MOXA_MUST_IER_EGDAI;
860 /* above add by Victor Yu. 08-30-2002 */
861 outb(info->IER, info->ioaddr + UART_IER); /* enable interrupts */
862
863 /*
864 * And clear the interrupt registers again for luck.
865 */
866 (void) inb(info->ioaddr + UART_LSR);
867 (void) inb(info->ioaddr + UART_RX);
868 (void) inb(info->ioaddr + UART_IIR);
869 (void) inb(info->ioaddr + UART_MSR);
870
871 if (info->tty)
872 clear_bit(TTY_IO_ERROR, &info->tty->flags);
873 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
874
875 /*
876 * and set the speed of the serial port
877 */
878 spin_unlock_irqrestore(&info->slock, flags);
879 mxser_change_speed(info, NULL);
880
881 info->flags |= ASYNC_INITIALIZED;
882 return 0;
883}
884
885/*
886 * This routine will shutdown a serial port; interrupts maybe disabled, and
887 * DTR is dropped if the hangup on close termio flag is on.
888 */
889static void mxser_shutdown(struct mxser_port *info)
890{
891 unsigned long flags;
892
893 if (!(info->flags & ASYNC_INITIALIZED))
894 return;
895
896 spin_lock_irqsave(&info->slock, flags);
897
898 /*
899 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
900 * here so the queue might never be waken up
901 */
902 wake_up_interruptible(&info->delta_msr_wait);
903
904 /*
905 * Free the IRQ, if necessary
906 */
907 if (info->xmit_buf) {
908 free_page((unsigned long) info->xmit_buf);
909 info->xmit_buf = NULL;
910 }
911
912 info->IER = 0;
913 outb(0x00, info->ioaddr + UART_IER);
914
915 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
916 info->MCR &= ~(UART_MCR_DTR | UART_MCR_RTS);
917 outb(info->MCR, info->ioaddr + UART_MCR);
918
919 /* clear Rx/Tx FIFO's */
920 /* following add by Victor Yu. 08-30-2002 */
921 if (info->board->chip_flag)
922 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT |
923 MOXA_MUST_FCR_GDA_MODE_ENABLE,
924 info->ioaddr + UART_FCR);
925 else
926 /* above add by Victor Yu. 08-30-2002 */
927 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
928 info->ioaddr + UART_FCR);
929
930 /* read data port to reset things */
931 (void) inb(info->ioaddr + UART_RX);
932
933 if (info->tty)
934 set_bit(TTY_IO_ERROR, &info->tty->flags);
935
936 info->flags &= ~ASYNC_INITIALIZED;
937
938 /* following add by Victor Yu. 09-23-2002 */
939 if (info->board->chip_flag)
940 SET_MOXA_MUST_NO_SOFTWARE_FLOW_CONTROL(info->ioaddr);
941 /* above add by Victor Yu. 09-23-2002 */
942
943 spin_unlock_irqrestore(&info->slock, flags);
944}
945
Jiri Slaby037ad482006-12-08 02:38:11 -0800946/*
947 * This routine is called whenever a serial port is opened. It
948 * enables interrupts for a serial port, linking in its async structure into
949 * the IRQ chain. It also performs the serial-specific
950 * initialization for the tty structure.
951 */
952static int mxser_open(struct tty_struct *tty, struct file *filp)
953{
Jiri Slaby55b307d2006-12-08 02:38:14 -0800954 struct mxser_port *info;
Jiri Slaby037ad482006-12-08 02:38:11 -0800955 int retval, line;
956
957 /* initialize driver_data in case something fails */
958 tty->driver_data = NULL;
959
960 line = tty->index;
961 if (line == MXSER_PORTS)
962 return 0;
963 if (line < 0 || line > MXSER_PORTS)
964 return -ENODEV;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800965 info = &mxser_boards[line / MXSER_PORTS_PER_BOARD].ports[line % MXSER_PORTS_PER_BOARD];
966 if (!info->ioaddr)
Jiri Slaby037ad482006-12-08 02:38:11 -0800967 return -ENODEV;
968
969 tty->driver_data = info;
970 info->tty = tty;
971 /*
972 * Start up serial port
973 */
Jiri Slaby3306ce32006-12-08 02:38:13 -0800974 info->count++;
Jiri Slaby037ad482006-12-08 02:38:11 -0800975 retval = mxser_startup(info);
976 if (retval)
977 return retval;
978
979 retval = mxser_block_til_ready(tty, filp, info);
980 if (retval)
981 return retval;
982
Jiri Slaby037ad482006-12-08 02:38:11 -0800983 if ((info->count == 1) && (info->flags & ASYNC_SPLIT_TERMIOS)) {
984 if (tty->driver->subtype == SERIAL_TYPE_NORMAL)
985 *tty->termios = info->normal_termios;
986 else
987 *tty->termios = info->callout_termios;
988 mxser_change_speed(info, NULL);
989 }
990
Andrew Morton08a4ae42006-12-08 02:38:12 -0800991 info->session = process_session(current);
Jiri Slaby037ad482006-12-08 02:38:11 -0800992 info->pgrp = process_group(current);
993
994 /*
995 status = mxser_get_msr(info->base, 0, info->port);
996 mxser_check_modem_status(info, status);
997 */
998
999/* unmark here for very high baud rate (ex. 921600 bps) used */
1000 tty->low_latency = 1;
1001 return 0;
1002}
1003
1004/*
1005 * This routine is called when the serial port gets closed. First, we
1006 * wait for the last remaining data to be sent. Then, we unlink its
1007 * async structure from the interrupt chain if necessary, and we free
1008 * that IRQ if nothing is left in the chain.
1009 */
1010static void mxser_close(struct tty_struct *tty, struct file *filp)
1011{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001012 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001013
1014 unsigned long timeout;
1015 unsigned long flags;
Jiri Slaby037ad482006-12-08 02:38:11 -08001016
1017 if (tty->index == MXSER_PORTS)
1018 return;
1019 if (!info)
1020 return;
1021
1022 spin_lock_irqsave(&info->slock, flags);
1023
1024 if (tty_hung_up_p(filp)) {
1025 spin_unlock_irqrestore(&info->slock, flags);
1026 return;
1027 }
1028 if ((tty->count == 1) && (info->count != 1)) {
1029 /*
1030 * Uh, oh. tty->count is 1, which means that the tty
1031 * structure will be freed. Info->count should always
1032 * be one in these conditions. If it's greater than
1033 * one, we've got real problems, since it means the
1034 * serial port won't be shutdown.
1035 */
1036 printk(KERN_ERR "mxser_close: bad serial port count; "
1037 "tty->count is 1, info->count is %d\n", info->count);
1038 info->count = 1;
1039 }
1040 if (--info->count < 0) {
1041 printk(KERN_ERR "mxser_close: bad serial port count for "
Jiri Slaby55b307d2006-12-08 02:38:14 -08001042 "ttys%d: %d\n", tty->index, info->count);
Jiri Slaby037ad482006-12-08 02:38:11 -08001043 info->count = 0;
1044 }
1045 if (info->count) {
1046 spin_unlock_irqrestore(&info->slock, flags);
1047 return;
1048 }
1049 info->flags |= ASYNC_CLOSING;
1050 spin_unlock_irqrestore(&info->slock, flags);
1051 /*
1052 * Save the termios structure, since this port may have
1053 * separate termios for callout and dialin.
1054 */
1055 if (info->flags & ASYNC_NORMAL_ACTIVE)
1056 info->normal_termios = *tty->termios;
1057 /*
1058 * Now we wait for the transmit buffer to clear; and we notify
1059 * the line discipline to only process XON/XOFF characters.
1060 */
1061 tty->closing = 1;
1062 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1063 tty_wait_until_sent(tty, info->closing_wait);
1064 /*
1065 * At this point we stop accepting input. To do this, we
1066 * disable the receive line status interrupts, and tell the
1067 * interrupt driver to stop checking the data ready bit in the
1068 * line status register.
1069 */
1070 info->IER &= ~UART_IER_RLSI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001071 if (info->board->chip_flag)
Jiri Slaby037ad482006-12-08 02:38:11 -08001072 info->IER &= ~MOXA_MUST_RECV_ISR;
1073/* by William
1074 info->read_status_mask &= ~UART_LSR_DR;
1075*/
1076 if (info->flags & ASYNC_INITIALIZED) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08001077 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001078 /*
1079 * Before we drop DTR, make sure the UART transmitter
1080 * has completely drained; this is especially
1081 * important if there is a transmit FIFO!
1082 */
1083 timeout = jiffies + HZ;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001084 while (!(inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001085 schedule_timeout_interruptible(5);
1086 if (time_after(jiffies, timeout))
1087 break;
1088 }
1089 }
1090 mxser_shutdown(info);
1091
1092 if (tty->driver->flush_buffer)
1093 tty->driver->flush_buffer(tty);
1094
Jiri Slaby7e8bcf92006-12-08 02:38:24 -08001095 tty_ldisc_flush(tty);
Jiri Slaby037ad482006-12-08 02:38:11 -08001096
1097 tty->closing = 0;
1098 info->event = 0;
1099 info->tty = NULL;
1100 if (info->blocked_open) {
1101 if (info->close_delay)
1102 schedule_timeout_interruptible(info->close_delay);
1103 wake_up_interruptible(&info->open_wait);
1104 }
1105
1106 info->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
1107 wake_up_interruptible(&info->close_wait);
1108
1109}
1110
1111static int mxser_write(struct tty_struct *tty, const unsigned char *buf, int count)
1112{
1113 int c, total = 0;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001114 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001115 unsigned long flags;
1116
1117 if (!info->xmit_buf)
1118 return 0;
1119
1120 while (1) {
1121 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1122 SERIAL_XMIT_SIZE - info->xmit_head));
1123 if (c <= 0)
1124 break;
1125
1126 memcpy(info->xmit_buf + info->xmit_head, buf, c);
1127 spin_lock_irqsave(&info->slock, flags);
1128 info->xmit_head = (info->xmit_head + c) &
1129 (SERIAL_XMIT_SIZE - 1);
1130 info->xmit_cnt += c;
1131 spin_unlock_irqrestore(&info->slock, flags);
1132
1133 buf += c;
1134 count -= c;
1135 total += c;
1136 }
1137
Jiri Slaby3306ce32006-12-08 02:38:13 -08001138 if (info->xmit_cnt && !tty->stopped
1139 /*&& !(info->IER & UART_IER_THRI)*/) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001140 if (!tty->hw_stopped ||
1141 (info->type == PORT_16550A) ||
Jiri Slaby55b307d2006-12-08 02:38:14 -08001142 (info->board->chip_flag)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001143 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby55b307d2006-12-08 02:38:14 -08001144 outb(info->IER & ~UART_IER_THRI, info->ioaddr +
1145 UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001146 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001147 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001148 spin_unlock_irqrestore(&info->slock, flags);
1149 }
1150 }
1151 return total;
1152}
1153
1154static void mxser_put_char(struct tty_struct *tty, unsigned char ch)
1155{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001156 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001157 unsigned long flags;
1158
1159 if (!info->xmit_buf)
1160 return;
1161
1162 if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1)
1163 return;
1164
1165 spin_lock_irqsave(&info->slock, flags);
1166 info->xmit_buf[info->xmit_head++] = ch;
1167 info->xmit_head &= SERIAL_XMIT_SIZE - 1;
1168 info->xmit_cnt++;
1169 spin_unlock_irqrestore(&info->slock, flags);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001170 if (!tty->stopped /*&& !(info->IER & UART_IER_THRI)*/) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001171 if (!tty->hw_stopped ||
1172 (info->type == PORT_16550A) ||
Jiri Slaby55b307d2006-12-08 02:38:14 -08001173 info->board->chip_flag) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001174 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby55b307d2006-12-08 02:38:14 -08001175 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001176 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001177 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001178 spin_unlock_irqrestore(&info->slock, flags);
1179 }
1180 }
1181}
1182
1183
1184static void mxser_flush_chars(struct tty_struct *tty)
1185{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001186 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001187 unsigned long flags;
1188
1189 if (info->xmit_cnt <= 0 ||
1190 tty->stopped ||
1191 !info->xmit_buf ||
1192 (tty->hw_stopped &&
1193 (info->type != PORT_16550A) &&
Jiri Slaby55b307d2006-12-08 02:38:14 -08001194 (!info->board->chip_flag)
Jiri Slaby037ad482006-12-08 02:38:11 -08001195 ))
1196 return;
1197
1198 spin_lock_irqsave(&info->slock, flags);
1199
Jiri Slaby55b307d2006-12-08 02:38:14 -08001200 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001201 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001202 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001203
1204 spin_unlock_irqrestore(&info->slock, flags);
1205}
1206
1207static int mxser_write_room(struct tty_struct *tty)
1208{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001209 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001210 int ret;
1211
1212 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1213 if (ret < 0)
1214 ret = 0;
1215 return ret;
1216}
1217
1218static int mxser_chars_in_buffer(struct tty_struct *tty)
1219{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001220 struct mxser_port *info = tty->driver_data;
Jiri Slaby925e9c12006-12-08 02:38:30 -08001221 return info->xmit_cnt;
Jiri Slaby037ad482006-12-08 02:38:11 -08001222}
1223
1224static void mxser_flush_buffer(struct tty_struct *tty)
1225{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001226 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001227 char fcr;
1228 unsigned long flags;
1229
1230
1231 spin_lock_irqsave(&info->slock, flags);
1232 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1233
1234 /* below added by shinhay */
Jiri Slaby55b307d2006-12-08 02:38:14 -08001235 fcr = inb(info->ioaddr + UART_FCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08001236 outb((fcr | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
Jiri Slaby55b307d2006-12-08 02:38:14 -08001237 info->ioaddr + UART_FCR);
1238 outb(fcr, info->ioaddr + UART_FCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08001239
1240 spin_unlock_irqrestore(&info->slock, flags);
1241 /* above added by shinhay */
1242
Jiri Slaby7e8bcf92006-12-08 02:38:24 -08001243 tty_wakeup(tty);
Jiri Slaby037ad482006-12-08 02:38:11 -08001244}
1245
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001246/*
1247 * ------------------------------------------------------------
1248 * friends of mxser_ioctl()
1249 * ------------------------------------------------------------
1250 */
1251static int mxser_get_serial_info(struct mxser_port *info,
1252 struct serial_struct __user *retinfo)
1253{
1254 struct serial_struct tmp;
1255
1256 if (!retinfo)
1257 return -EFAULT;
1258 memset(&tmp, 0, sizeof(tmp));
1259 tmp.type = info->type;
1260 tmp.line = info->tty->index;
1261 tmp.port = info->ioaddr;
1262 tmp.irq = info->board->irq;
1263 tmp.flags = info->flags;
1264 tmp.baud_base = info->baud_base;
1265 tmp.close_delay = info->close_delay;
1266 tmp.closing_wait = info->closing_wait;
1267 tmp.custom_divisor = info->custom_divisor;
1268 tmp.hub6 = 0;
1269 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1270 return -EFAULT;
1271 return 0;
1272}
1273
1274static int mxser_set_serial_info(struct mxser_port *info,
1275 struct serial_struct __user *new_info)
1276{
1277 struct serial_struct new_serial;
1278 unsigned int flags;
1279 int retval = 0;
1280
1281 if (!new_info || !info->ioaddr)
1282 return -EFAULT;
1283 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
1284 return -EFAULT;
1285
1286 if ((new_serial.irq != info->board->irq) ||
1287 (new_serial.port != info->ioaddr) ||
1288 (new_serial.custom_divisor != info->custom_divisor) ||
1289 (new_serial.baud_base != info->baud_base))
1290 return -EPERM;
1291
1292 flags = info->flags & ASYNC_SPD_MASK;
1293
1294 if (!capable(CAP_SYS_ADMIN)) {
1295 if ((new_serial.baud_base != info->baud_base) ||
1296 (new_serial.close_delay != info->close_delay) ||
1297 ((new_serial.flags & ~ASYNC_USR_MASK) != (info->flags & ~ASYNC_USR_MASK)))
1298 return -EPERM;
1299 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
1300 (new_serial.flags & ASYNC_USR_MASK));
1301 } else {
1302 /*
1303 * OK, past this point, all the error checking has been done.
1304 * At this point, we start making changes.....
1305 */
1306 info->flags = ((info->flags & ~ASYNC_FLAGS) |
1307 (new_serial.flags & ASYNC_FLAGS));
1308 info->close_delay = new_serial.close_delay * HZ / 100;
1309 info->closing_wait = new_serial.closing_wait * HZ / 100;
1310 info->tty->low_latency =
1311 (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1312 info->tty->low_latency = 0; /* (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0; */
1313 }
1314
1315 /* added by casper, 3/17/2000, for mouse */
1316 info->type = new_serial.type;
1317
1318 process_txrx_fifo(info);
1319
1320 if (info->flags & ASYNC_INITIALIZED) {
1321 if (flags != (info->flags & ASYNC_SPD_MASK))
1322 mxser_change_speed(info, NULL);
1323 } else
1324 retval = mxser_startup(info);
1325
1326 return retval;
1327}
1328
1329/*
1330 * mxser_get_lsr_info - get line status register info
1331 *
1332 * Purpose: Let user call ioctl() to get info when the UART physically
1333 * is emptied. On bus types like RS485, the transmitter must
1334 * release the bus after transmitting. This must be done when
1335 * the transmit shift register is empty, not be done when the
1336 * transmit holding register is empty. This functionality
1337 * allows an RS485 driver to be written in user space.
1338 */
1339static int mxser_get_lsr_info(struct mxser_port *info,
1340 unsigned int __user *value)
1341{
1342 unsigned char status;
1343 unsigned int result;
1344 unsigned long flags;
1345
1346 spin_lock_irqsave(&info->slock, flags);
1347 status = inb(info->ioaddr + UART_LSR);
1348 spin_unlock_irqrestore(&info->slock, flags);
1349 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1350 return put_user(result, value);
1351}
1352
1353/*
1354 * This routine sends a break character out the serial port.
1355 */
1356static void mxser_send_break(struct mxser_port *info, int duration)
1357{
1358 unsigned long flags;
1359
1360 if (!info->ioaddr)
1361 return;
1362 set_current_state(TASK_INTERRUPTIBLE);
1363 spin_lock_irqsave(&info->slock, flags);
1364 outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
1365 info->ioaddr + UART_LCR);
1366 spin_unlock_irqrestore(&info->slock, flags);
1367 schedule_timeout(duration);
1368 spin_lock_irqsave(&info->slock, flags);
1369 outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
1370 info->ioaddr + UART_LCR);
1371 spin_unlock_irqrestore(&info->slock, flags);
1372}
1373
1374static int mxser_tiocmget(struct tty_struct *tty, struct file *file)
Jiri Slaby037ad482006-12-08 02:38:11 -08001375{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001376 struct mxser_port *info = tty->driver_data;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001377 unsigned char control, status;
Jiri Slaby037ad482006-12-08 02:38:11 -08001378 unsigned long flags;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001379
Jiri Slaby037ad482006-12-08 02:38:11 -08001380
1381 if (tty->index == MXSER_PORTS)
Jiri Slaby037ad482006-12-08 02:38:11 -08001382 return -ENOIOCTLCMD;
Jiri Slaby214efeb2006-12-08 02:38:25 -08001383 if (test_bit(TTY_IO_ERROR, &tty->flags))
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001384 return -EIO;
1385
1386 control = info->MCR;
1387
1388 spin_lock_irqsave(&info->slock, flags);
1389 status = inb(info->ioaddr + UART_MSR);
1390 if (status & UART_MSR_ANY_DELTA)
1391 mxser_check_modem_status(info, status);
1392 spin_unlock_irqrestore(&info->slock, flags);
1393 return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0) |
1394 ((control & UART_MCR_DTR) ? TIOCM_DTR : 0) |
1395 ((status & UART_MSR_DCD) ? TIOCM_CAR : 0) |
1396 ((status & UART_MSR_RI) ? TIOCM_RNG : 0) |
1397 ((status & UART_MSR_DSR) ? TIOCM_DSR : 0) |
1398 ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
1399}
1400
1401static int mxser_tiocmset(struct tty_struct *tty, struct file *file,
1402 unsigned int set, unsigned int clear)
1403{
1404 struct mxser_port *info = tty->driver_data;
1405 unsigned long flags;
1406
1407
1408 if (tty->index == MXSER_PORTS)
1409 return -ENOIOCTLCMD;
Jiri Slaby214efeb2006-12-08 02:38:25 -08001410 if (test_bit(TTY_IO_ERROR, &tty->flags))
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001411 return -EIO;
1412
1413 spin_lock_irqsave(&info->slock, flags);
1414
1415 if (set & TIOCM_RTS)
1416 info->MCR |= UART_MCR_RTS;
1417 if (set & TIOCM_DTR)
1418 info->MCR |= UART_MCR_DTR;
1419
1420 if (clear & TIOCM_RTS)
1421 info->MCR &= ~UART_MCR_RTS;
1422 if (clear & TIOCM_DTR)
1423 info->MCR &= ~UART_MCR_DTR;
1424
1425 outb(info->MCR, info->ioaddr + UART_MCR);
1426 spin_unlock_irqrestore(&info->slock, flags);
Jiri Slaby037ad482006-12-08 02:38:11 -08001427 return 0;
1428}
1429
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001430static int mxser_program_mode(int port)
1431{
1432 int id, i, j, n;
1433 /* unsigned long flags; */
1434
1435 spin_lock(&gm_lock);
1436 outb(0, port);
1437 outb(0, port);
1438 outb(0, port);
1439 (void)inb(port);
1440 (void)inb(port);
1441 outb(0, port);
1442 (void)inb(port);
1443 /* restore_flags(flags); */
1444 spin_unlock(&gm_lock);
1445
1446 id = inb(port + 1) & 0x1F;
1447 if ((id != C168_ASIC_ID) &&
1448 (id != C104_ASIC_ID) &&
1449 (id != C102_ASIC_ID) &&
1450 (id != CI132_ASIC_ID) &&
1451 (id != CI134_ASIC_ID) &&
1452 (id != CI104J_ASIC_ID))
1453 return -1;
1454 for (i = 0, j = 0; i < 4; i++) {
1455 n = inb(port + 2);
1456 if (n == 'M') {
1457 j = 1;
1458 } else if ((j == 1) && (n == 1)) {
1459 j = 2;
1460 break;
1461 } else
1462 j = 0;
1463 }
1464 if (j != 2)
1465 id = -2;
1466 return id;
1467}
1468
1469static void mxser_normal_mode(int port)
1470{
1471 int i, n;
1472
1473 outb(0xA5, port + 1);
1474 outb(0x80, port + 3);
1475 outb(12, port + 0); /* 9600 bps */
1476 outb(0, port + 1);
1477 outb(0x03, port + 3); /* 8 data bits */
1478 outb(0x13, port + 4); /* loop back mode */
1479 for (i = 0; i < 16; i++) {
1480 n = inb(port + 5);
1481 if ((n & 0x61) == 0x60)
1482 break;
1483 if ((n & 1) == 1)
1484 (void)inb(port);
1485 }
1486 outb(0x00, port + 4);
1487}
1488
1489#define CHIP_SK 0x01 /* Serial Data Clock in Eprom */
1490#define CHIP_DO 0x02 /* Serial Data Output in Eprom */
1491#define CHIP_CS 0x04 /* Serial Chip Select in Eprom */
1492#define CHIP_DI 0x08 /* Serial Data Input in Eprom */
1493#define EN_CCMD 0x000 /* Chip's command register */
1494#define EN0_RSARLO 0x008 /* Remote start address reg 0 */
1495#define EN0_RSARHI 0x009 /* Remote start address reg 1 */
1496#define EN0_RCNTLO 0x00A /* Remote byte count reg WR */
1497#define EN0_RCNTHI 0x00B /* Remote byte count reg WR */
1498#define EN0_DCFG 0x00E /* Data configuration reg WR */
1499#define EN0_PORT 0x010 /* Rcv missed frame error counter RD */
1500#define ENC_PAGE0 0x000 /* Select page 0 of chip registers */
1501#define ENC_PAGE3 0x0C0 /* Select page 3 of chip registers */
1502static int mxser_read_register(int port, unsigned short *regs)
1503{
1504 int i, k, value, id;
1505 unsigned int j;
1506
1507 id = mxser_program_mode(port);
1508 if (id < 0)
1509 return id;
1510 for (i = 0; i < 14; i++) {
1511 k = (i & 0x3F) | 0x180;
1512 for (j = 0x100; j > 0; j >>= 1) {
1513 outb(CHIP_CS, port);
1514 if (k & j) {
1515 outb(CHIP_CS | CHIP_DO, port);
1516 outb(CHIP_CS | CHIP_DO | CHIP_SK, port); /* A? bit of read */
1517 } else {
1518 outb(CHIP_CS, port);
1519 outb(CHIP_CS | CHIP_SK, port); /* A? bit of read */
1520 }
1521 }
1522 (void)inb(port);
1523 value = 0;
1524 for (k = 0, j = 0x8000; k < 16; k++, j >>= 1) {
1525 outb(CHIP_CS, port);
1526 outb(CHIP_CS | CHIP_SK, port);
1527 if (inb(port) & CHIP_DI)
1528 value |= j;
1529 }
1530 regs[i] = value;
1531 outb(0, port);
1532 }
1533 mxser_normal_mode(port);
1534 return id;
1535}
1536
Jiri Slaby037ad482006-12-08 02:38:11 -08001537static int mxser_ioctl_special(unsigned int cmd, void __user *argp)
1538{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001539 struct mxser_port *port;
1540 int result, status;
1541 unsigned int i, j;
Jiri Slaby037ad482006-12-08 02:38:11 -08001542
1543 switch (cmd) {
1544 case MOXA_GET_CONF:
Jiri Slaby55b307d2006-12-08 02:38:14 -08001545/* if (copy_to_user(argp, mxsercfg,
Jiri Slaby037ad482006-12-08 02:38:11 -08001546 sizeof(struct mxser_hwconf) * 4))
1547 return -EFAULT;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001548 return 0;*/
1549 return -ENXIO;
Jiri Slaby037ad482006-12-08 02:38:11 -08001550 case MOXA_GET_MAJOR:
1551 if (copy_to_user(argp, &ttymajor, sizeof(int)))
1552 return -EFAULT;
1553 return 0;
1554
1555 case MOXA_GET_CUMAJOR:
1556 if (copy_to_user(argp, &calloutmajor, sizeof(int)))
1557 return -EFAULT;
1558 return 0;
1559
1560 case MOXA_CHKPORTENABLE:
1561 result = 0;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001562
1563 for (i = 0; i < MXSER_BOARDS; i++)
1564 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++)
1565 if (mxser_boards[i].ports[j].ioaddr)
1566 result |= (1 << i);
1567
Jiri Slaby037ad482006-12-08 02:38:11 -08001568 return put_user(result, (unsigned long __user *)argp);
1569 case MOXA_GETDATACOUNT:
1570 if (copy_to_user(argp, &mxvar_log, sizeof(mxvar_log)))
1571 return -EFAULT;
1572 return 0;
1573 case MOXA_GETMSTATUS:
Jiri Slaby55b307d2006-12-08 02:38:14 -08001574 for (i = 0; i < MXSER_BOARDS; i++)
1575 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
1576 port = &mxser_boards[i].ports[j];
1577
1578 GMStatus[i].ri = 0;
1579 if (!port->ioaddr) {
1580 GMStatus[i].dcd = 0;
1581 GMStatus[i].dsr = 0;
1582 GMStatus[i].cts = 0;
1583 continue;
1584 }
1585
1586 if (!port->tty || !port->tty->termios)
1587 GMStatus[i].cflag =
1588 port->normal_termios.c_cflag;
1589 else
1590 GMStatus[i].cflag =
1591 port->tty->termios->c_cflag;
1592
1593 status = inb(port->ioaddr + UART_MSR);
1594 if (status & 0x80 /*UART_MSR_DCD */ )
1595 GMStatus[i].dcd = 1;
1596 else
1597 GMStatus[i].dcd = 0;
1598
1599 if (status & 0x20 /*UART_MSR_DSR */ )
1600 GMStatus[i].dsr = 1;
1601 else
1602 GMStatus[i].dsr = 0;
1603
1604
1605 if (status & 0x10 /*UART_MSR_CTS */ )
1606 GMStatus[i].cts = 1;
1607 else
1608 GMStatus[i].cts = 0;
Jiri Slaby037ad482006-12-08 02:38:11 -08001609 }
Jiri Slaby037ad482006-12-08 02:38:11 -08001610 if (copy_to_user(argp, GMStatus,
1611 sizeof(struct mxser_mstatus) * MXSER_PORTS))
1612 return -EFAULT;
1613 return 0;
1614 case MOXA_ASPP_MON_EXT: {
Jiri Slaby55b307d2006-12-08 02:38:14 -08001615 int status, p, shiftbit;
1616 unsigned long opmode;
1617 unsigned cflag, iflag;
Jiri Slaby037ad482006-12-08 02:38:11 -08001618
Jiri Slaby55b307d2006-12-08 02:38:14 -08001619 for (i = 0; i < MXSER_BOARDS; i++)
1620 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
1621 port = &mxser_boards[i].ports[j];
1622 if (!port->ioaddr)
Jiri Slaby037ad482006-12-08 02:38:11 -08001623 continue;
1624
Jiri Slaby55b307d2006-12-08 02:38:14 -08001625 status = mxser_get_msr(port->ioaddr, 0, i);
1626/* mxser_check_modem_status(port, status); */
1627
Jiri Slaby037ad482006-12-08 02:38:11 -08001628 if (status & UART_MSR_TERI)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001629 port->icount.rng++;
Jiri Slaby037ad482006-12-08 02:38:11 -08001630 if (status & UART_MSR_DDSR)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001631 port->icount.dsr++;
Jiri Slaby037ad482006-12-08 02:38:11 -08001632 if (status & UART_MSR_DDCD)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001633 port->icount.dcd++;
Jiri Slaby037ad482006-12-08 02:38:11 -08001634 if (status & UART_MSR_DCTS)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001635 port->icount.cts++;
Jiri Slaby037ad482006-12-08 02:38:11 -08001636
Jiri Slaby55b307d2006-12-08 02:38:14 -08001637 port->mon_data.modem_status = status;
1638 mon_data_ext.rx_cnt[i] = port->mon_data.rxcnt;
1639 mon_data_ext.tx_cnt[i] = port->mon_data.txcnt;
1640 mon_data_ext.up_rxcnt[i] =
1641 port->mon_data.up_rxcnt;
1642 mon_data_ext.up_txcnt[i] =
1643 port->mon_data.up_txcnt;
1644 mon_data_ext.modem_status[i] =
1645 port->mon_data.modem_status;
1646 mon_data_ext.baudrate[i] = port->realbaud;
Jiri Slaby037ad482006-12-08 02:38:11 -08001647
Jiri Slaby55b307d2006-12-08 02:38:14 -08001648 if (!port->tty || !port->tty->termios) {
1649 cflag = port->normal_termios.c_cflag;
1650 iflag = port->normal_termios.c_iflag;
Jiri Slaby037ad482006-12-08 02:38:11 -08001651 } else {
Jiri Slaby55b307d2006-12-08 02:38:14 -08001652 cflag = port->tty->termios->c_cflag;
1653 iflag = port->tty->termios->c_iflag;
Jiri Slaby037ad482006-12-08 02:38:11 -08001654 }
1655
1656 mon_data_ext.databits[i] = cflag & CSIZE;
1657
1658 mon_data_ext.stopbits[i] = cflag & CSTOPB;
1659
Jiri Slaby55b307d2006-12-08 02:38:14 -08001660 mon_data_ext.parity[i] =
1661 cflag & (PARENB | PARODD | CMSPAR);
Jiri Slaby037ad482006-12-08 02:38:11 -08001662
1663 mon_data_ext.flowctrl[i] = 0x00;
1664
1665 if (cflag & CRTSCTS)
1666 mon_data_ext.flowctrl[i] |= 0x03;
1667
1668 if (iflag & (IXON | IXOFF))
1669 mon_data_ext.flowctrl[i] |= 0x0C;
1670
Jiri Slaby55b307d2006-12-08 02:38:14 -08001671 if (port->type == PORT_16550A)
Jiri Slaby037ad482006-12-08 02:38:11 -08001672 mon_data_ext.fifo[i] = 1;
1673 else
1674 mon_data_ext.fifo[i] = 0;
1675
1676 p = i % 4;
1677 shiftbit = p * 2;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001678 opmode = inb(port->opmode_ioaddr) >> shiftbit;
Jiri Slaby037ad482006-12-08 02:38:11 -08001679 opmode &= OP_MODE_MASK;
1680
1681 mon_data_ext.iftype[i] = opmode;
1682
1683 }
Jiri Slaby55b307d2006-12-08 02:38:14 -08001684 if (copy_to_user(argp, &mon_data_ext,
1685 sizeof(mon_data_ext)))
Jiri Slaby037ad482006-12-08 02:38:11 -08001686 return -EFAULT;
1687
1688 return 0;
1689
Jiri Slaby55b307d2006-12-08 02:38:14 -08001690 } default:
Jiri Slaby037ad482006-12-08 02:38:11 -08001691 return -ENOIOCTLCMD;
1692 }
1693 return 0;
1694}
1695
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001696static int mxser_ioctl(struct tty_struct *tty, struct file *file,
1697 unsigned int cmd, unsigned long arg)
1698{
1699 struct mxser_port *info = tty->driver_data;
1700 struct async_icount cprev, cnow; /* kernel counter temps */
1701 struct serial_icounter_struct __user *p_cuser;
1702 unsigned long templ;
1703 unsigned long flags;
1704 void __user *argp = (void __user *)arg;
1705 int retval;
1706
1707 if (tty->index == MXSER_PORTS)
1708 return mxser_ioctl_special(cmd, argp);
1709
1710 /* following add by Victor Yu. 01-05-2004 */
1711 if (cmd == MOXA_SET_OP_MODE || cmd == MOXA_GET_OP_MODE) {
1712 int p;
1713 unsigned long opmode;
1714 static unsigned char ModeMask[] = { 0xfc, 0xf3, 0xcf, 0x3f };
1715 int shiftbit;
1716 unsigned char val, mask;
1717
1718 p = tty->index % 4;
1719 if (cmd == MOXA_SET_OP_MODE) {
1720 if (get_user(opmode, (int __user *) argp))
1721 return -EFAULT;
1722 if (opmode != RS232_MODE &&
1723 opmode != RS485_2WIRE_MODE &&
1724 opmode != RS422_MODE &&
1725 opmode != RS485_4WIRE_MODE)
1726 return -EFAULT;
1727 mask = ModeMask[p];
1728 shiftbit = p * 2;
1729 val = inb(info->opmode_ioaddr);
1730 val &= mask;
1731 val |= (opmode << shiftbit);
1732 outb(val, info->opmode_ioaddr);
1733 } else {
1734 shiftbit = p * 2;
1735 opmode = inb(info->opmode_ioaddr) >> shiftbit;
1736 opmode &= OP_MODE_MASK;
1737 if (copy_to_user(argp, &opmode, sizeof(int)))
1738 return -EFAULT;
1739 }
1740 return 0;
1741 }
1742 /* above add by Victor Yu. 01-05-2004 */
1743
Jiri Slaby214efeb2006-12-08 02:38:25 -08001744 if (cmd != TIOCGSERIAL && cmd != TIOCMIWAIT && cmd != TIOCGICOUNT &&
1745 test_bit(TTY_IO_ERROR, &tty->flags))
1746 return -EIO;
1747
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001748 switch (cmd) {
1749 case TCSBRK: /* SVID version: non-zero arg --> no break */
1750 retval = tty_check_change(tty);
1751 if (retval)
1752 return retval;
1753 tty_wait_until_sent(tty, 0);
1754 if (!arg)
1755 mxser_send_break(info, HZ / 4); /* 1/4 second */
1756 return 0;
1757 case TCSBRKP: /* support for POSIX tcsendbreak() */
1758 retval = tty_check_change(tty);
1759 if (retval)
1760 return retval;
1761 tty_wait_until_sent(tty, 0);
1762 mxser_send_break(info, arg ? arg * (HZ / 10) : HZ / 4);
1763 return 0;
1764 case TIOCGSOFTCAR:
1765 return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *)argp);
1766 case TIOCSSOFTCAR:
1767 if (get_user(templ, (unsigned long __user *) argp))
1768 return -EFAULT;
1769 arg = templ;
1770 tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) | (arg ? CLOCAL : 0));
1771 return 0;
1772 case TIOCGSERIAL:
1773 return mxser_get_serial_info(info, argp);
1774 case TIOCSSERIAL:
1775 return mxser_set_serial_info(info, argp);
1776 case TIOCSERGETLSR: /* Get line status register */
1777 return mxser_get_lsr_info(info, argp);
1778 /*
1779 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1780 * - mask passed in arg for lines of interest
1781 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1782 * Caller should use TIOCGICOUNT to see which one it was
1783 */
1784 case TIOCMIWAIT: {
1785 DECLARE_WAITQUEUE(wait, current);
1786 int ret;
1787 spin_lock_irqsave(&info->slock, flags);
1788 cprev = info->icount; /* note the counters on entry */
1789 spin_unlock_irqrestore(&info->slock, flags);
1790
1791 add_wait_queue(&info->delta_msr_wait, &wait);
1792 while (1) {
1793 spin_lock_irqsave(&info->slock, flags);
1794 cnow = info->icount; /* atomic copy */
1795 spin_unlock_irqrestore(&info->slock, flags);
1796
1797 set_current_state(TASK_INTERRUPTIBLE);
1798 if (((arg & TIOCM_RNG) &&
1799 (cnow.rng != cprev.rng)) ||
1800 ((arg & TIOCM_DSR) &&
1801 (cnow.dsr != cprev.dsr)) ||
1802 ((arg & TIOCM_CD) &&
1803 (cnow.dcd != cprev.dcd)) ||
1804 ((arg & TIOCM_CTS) &&
1805 (cnow.cts != cprev.cts))) {
1806 ret = 0;
1807 break;
1808 }
1809 /* see if a signal did it */
1810 if (signal_pending(current)) {
1811 ret = -ERESTARTSYS;
1812 break;
1813 }
1814 cprev = cnow;
1815 }
1816 current->state = TASK_RUNNING;
1817 remove_wait_queue(&info->delta_msr_wait, &wait);
1818 break;
1819 }
1820 /* NOTREACHED */
1821 /*
1822 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1823 * Return: write counters to the user passed counter struct
1824 * NB: both 1->0 and 0->1 transitions are counted except for
1825 * RI where only 0->1 is counted.
1826 */
1827 case TIOCGICOUNT:
1828 spin_lock_irqsave(&info->slock, flags);
1829 cnow = info->icount;
1830 spin_unlock_irqrestore(&info->slock, flags);
1831 p_cuser = argp;
1832 /* modified by casper 1/11/2000 */
1833 if (put_user(cnow.frame, &p_cuser->frame))
1834 return -EFAULT;
1835 if (put_user(cnow.brk, &p_cuser->brk))
1836 return -EFAULT;
1837 if (put_user(cnow.overrun, &p_cuser->overrun))
1838 return -EFAULT;
1839 if (put_user(cnow.buf_overrun, &p_cuser->buf_overrun))
1840 return -EFAULT;
1841 if (put_user(cnow.parity, &p_cuser->parity))
1842 return -EFAULT;
1843 if (put_user(cnow.rx, &p_cuser->rx))
1844 return -EFAULT;
1845 if (put_user(cnow.tx, &p_cuser->tx))
1846 return -EFAULT;
1847 put_user(cnow.cts, &p_cuser->cts);
1848 put_user(cnow.dsr, &p_cuser->dsr);
1849 put_user(cnow.rng, &p_cuser->rng);
1850 put_user(cnow.dcd, &p_cuser->dcd);
1851 return 0;
1852 case MOXA_HighSpeedOn:
1853 return put_user(info->baud_base != 115200 ? 1 : 0, (int __user *)argp);
1854 case MOXA_SDS_RSTICOUNTER:
1855 info->mon_data.rxcnt = 0;
1856 info->mon_data.txcnt = 0;
1857 return 0;
1858/* (above) added by James. */
1859 case MOXA_ASPP_SETBAUD:{
1860 long baud;
1861 if (get_user(baud, (long __user *)argp))
1862 return -EFAULT;
1863 mxser_set_baud(info, baud);
1864 return 0;
1865 }
1866 case MOXA_ASPP_GETBAUD:
1867 if (copy_to_user(argp, &info->realbaud, sizeof(long)))
1868 return -EFAULT;
1869
1870 return 0;
1871
1872 case MOXA_ASPP_OQUEUE:{
1873 int len, lsr;
1874
1875 len = mxser_chars_in_buffer(tty);
1876
1877 lsr = inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT;
1878
1879 len += (lsr ? 0 : 1);
1880
1881 if (copy_to_user(argp, &len, sizeof(int)))
1882 return -EFAULT;
1883
1884 return 0;
1885 }
1886 case MOXA_ASPP_MON: {
1887 int mcr, status;
1888
1889 /* info->mon_data.ser_param = tty->termios->c_cflag; */
1890
1891 status = mxser_get_msr(info->ioaddr, 1, tty->index);
1892 mxser_check_modem_status(info, status);
1893
1894 mcr = inb(info->ioaddr + UART_MCR);
1895 if (mcr & MOXA_MUST_MCR_XON_FLAG)
1896 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFHOLD;
1897 else
1898 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFHOLD;
1899
1900 if (mcr & MOXA_MUST_MCR_TX_XON)
1901 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFXENT;
1902 else
1903 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFXENT;
1904
1905 if (info->tty->hw_stopped)
1906 info->mon_data.hold_reason |= NPPI_NOTIFY_CTSHOLD;
1907 else
1908 info->mon_data.hold_reason &= ~NPPI_NOTIFY_CTSHOLD;
1909
1910 if (copy_to_user(argp, &info->mon_data,
1911 sizeof(struct mxser_mon)))
1912 return -EFAULT;
1913
1914 return 0;
1915 }
1916 case MOXA_ASPP_LSTATUS: {
1917 if (copy_to_user(argp, &info->err_shadow,
1918 sizeof(unsigned char)))
1919 return -EFAULT;
1920
1921 info->err_shadow = 0;
1922 return 0;
1923 }
1924 case MOXA_SET_BAUD_METHOD: {
1925 int method;
1926
1927 if (get_user(method, (int __user *)argp))
1928 return -EFAULT;
1929 mxser_set_baud_method[tty->index] = method;
1930 if (copy_to_user(argp, &method, sizeof(int)))
1931 return -EFAULT;
1932
1933 return 0;
1934 }
1935 default:
1936 return -ENOIOCTLCMD;
1937 }
1938 return 0;
1939}
1940
Jiri Slaby037ad482006-12-08 02:38:11 -08001941static void mxser_stoprx(struct tty_struct *tty)
1942{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001943 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001944
1945 info->ldisc_stop_rx = 1;
1946 if (I_IXOFF(tty)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001947 /* following add by Victor Yu. 09-02-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08001948 if (info->board->chip_flag) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001949 info->IER &= ~MOXA_MUST_RECV_ISR;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001950 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby925e9c12006-12-08 02:38:30 -08001951 } else {
Jiri Slaby037ad482006-12-08 02:38:11 -08001952 info->x_char = STOP_CHAR(tty);
Jiri Slaby55b307d2006-12-08 02:38:14 -08001953 outb(0, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001954 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001955 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001956 }
Jiri Slaby037ad482006-12-08 02:38:11 -08001957 }
1958
1959 if (info->tty->termios->c_cflag & CRTSCTS) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001960 info->MCR &= ~UART_MCR_RTS;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001961 outb(info->MCR, info->ioaddr + UART_MCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08001962 }
1963}
1964
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001965/*
1966 * This routine is called by the upper-layer tty layer to signal that
1967 * incoming characters should be throttled.
1968 */
1969static void mxser_throttle(struct tty_struct *tty)
1970{
1971 mxser_stoprx(tty);
1972}
1973
1974static void mxser_unthrottle(struct tty_struct *tty)
Jiri Slaby037ad482006-12-08 02:38:11 -08001975{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001976 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001977
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001978 /* startrx */
Jiri Slaby037ad482006-12-08 02:38:11 -08001979 info->ldisc_stop_rx = 0;
1980 if (I_IXOFF(tty)) {
1981 if (info->x_char)
1982 info->x_char = 0;
1983 else {
Jiri Slaby037ad482006-12-08 02:38:11 -08001984 /* following add by Victor Yu. 09-02-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08001985 if (info->board->chip_flag) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001986 info->IER |= MOXA_MUST_RECV_ISR;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001987 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby925e9c12006-12-08 02:38:30 -08001988 } else {
Jiri Slaby037ad482006-12-08 02:38:11 -08001989 info->x_char = START_CHAR(tty);
Jiri Slaby55b307d2006-12-08 02:38:14 -08001990 outb(0, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001991 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001992 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001993 }
Jiri Slaby037ad482006-12-08 02:38:11 -08001994 }
1995 }
1996
1997 if (info->tty->termios->c_cflag & CRTSCTS) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001998 info->MCR |= UART_MCR_RTS;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001999 outb(info->MCR, info->ioaddr + UART_MCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002000 }
2001}
2002
2003/*
Jiri Slaby037ad482006-12-08 02:38:11 -08002004 * mxser_stop() and mxser_start()
2005 *
2006 * This routines are called before setting or resetting tty->stopped.
2007 * They enable or disable transmitter interrupts, as necessary.
2008 */
2009static void mxser_stop(struct tty_struct *tty)
2010{
Jiri Slaby55b307d2006-12-08 02:38:14 -08002011 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08002012 unsigned long flags;
2013
2014 spin_lock_irqsave(&info->slock, flags);
2015 if (info->IER & UART_IER_THRI) {
2016 info->IER &= ~UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002017 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08002018 }
2019 spin_unlock_irqrestore(&info->slock, flags);
2020}
2021
2022static void mxser_start(struct tty_struct *tty)
2023{
Jiri Slaby55b307d2006-12-08 02:38:14 -08002024 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08002025 unsigned long flags;
2026
2027 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby3306ce32006-12-08 02:38:13 -08002028 if (info->xmit_cnt && info->xmit_buf
2029 /* && !(info->IER & UART_IER_THRI) */) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002030 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08002031 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002032 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08002033 }
2034 spin_unlock_irqrestore(&info->slock, flags);
2035}
2036
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002037static void mxser_set_termios(struct tty_struct *tty, struct termios *old_termios)
2038{
2039 struct mxser_port *info = tty->driver_data;
2040 unsigned long flags;
2041
2042 if ((tty->termios->c_cflag != old_termios->c_cflag) ||
2043 (RELEVANT_IFLAG(tty->termios->c_iflag) != RELEVANT_IFLAG(old_termios->c_iflag))) {
2044
2045 mxser_change_speed(info, old_termios);
2046
2047 if ((old_termios->c_cflag & CRTSCTS) &&
2048 !(tty->termios->c_cflag & CRTSCTS)) {
2049 tty->hw_stopped = 0;
2050 mxser_start(tty);
2051 }
2052 }
2053
2054/* Handle sw stopped */
2055 if ((old_termios->c_iflag & IXON) &&
2056 !(tty->termios->c_iflag & IXON)) {
2057 tty->stopped = 0;
2058
2059 /* following add by Victor Yu. 09-02-2002 */
2060 if (info->board->chip_flag) {
2061 spin_lock_irqsave(&info->slock, flags);
2062 DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
2063 spin_unlock_irqrestore(&info->slock, flags);
2064 }
2065 /* above add by Victor Yu. 09-02-2002 */
2066
2067 mxser_start(tty);
2068 }
2069}
2070
Jiri Slaby037ad482006-12-08 02:38:11 -08002071/*
2072 * mxser_wait_until_sent() --- wait until the transmitter is empty
2073 */
2074static void mxser_wait_until_sent(struct tty_struct *tty, int timeout)
2075{
Jiri Slaby55b307d2006-12-08 02:38:14 -08002076 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08002077 unsigned long orig_jiffies, char_time;
2078 int lsr;
2079
2080 if (info->type == PORT_UNKNOWN)
2081 return;
2082
2083 if (info->xmit_fifo_size == 0)
2084 return; /* Just in case.... */
2085
2086 orig_jiffies = jiffies;
2087 /*
2088 * Set the check interval to be 1/5 of the estimated time to
2089 * send a single character, and make it at least 1. The check
2090 * interval should also be less than the timeout.
2091 *
2092 * Note: we have to use pretty tight timings here to satisfy
2093 * the NIST-PCTS.
2094 */
2095 char_time = (info->timeout - HZ / 50) / info->xmit_fifo_size;
2096 char_time = char_time / 5;
2097 if (char_time == 0)
2098 char_time = 1;
2099 if (timeout && timeout < char_time)
2100 char_time = timeout;
2101 /*
2102 * If the transmitter hasn't cleared in twice the approximate
2103 * amount of time to send the entire FIFO, it probably won't
2104 * ever clear. This assumes the UART isn't doing flow
2105 * control, which is currently the case. Hence, if it ever
2106 * takes longer than info->timeout, this is probably due to a
2107 * UART bug of some kind. So, we clamp the timeout parameter at
2108 * 2*info->timeout.
2109 */
2110 if (!timeout || timeout > 2 * info->timeout)
2111 timeout = 2 * info->timeout;
2112#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2113 printk(KERN_DEBUG "In rs_wait_until_sent(%d) check=%lu...",
2114 timeout, char_time);
2115 printk("jiff=%lu...", jiffies);
2116#endif
Jiri Slaby55b307d2006-12-08 02:38:14 -08002117 while (!((lsr = inb(info->ioaddr + UART_LSR)) & UART_LSR_TEMT)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002118#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2119 printk("lsr = %d (jiff=%lu)...", lsr, jiffies);
2120#endif
2121 schedule_timeout_interruptible(char_time);
2122 if (signal_pending(current))
2123 break;
2124 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2125 break;
2126 }
2127 set_current_state(TASK_RUNNING);
2128
2129#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2130 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
2131#endif
2132}
2133
2134
2135/*
2136 * This routine is called by tty_hangup() when a hangup is signaled.
2137 */
2138void mxser_hangup(struct tty_struct *tty)
2139{
Jiri Slaby55b307d2006-12-08 02:38:14 -08002140 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08002141
2142 mxser_flush_buffer(tty);
2143 mxser_shutdown(info);
2144 info->event = 0;
2145 info->count = 0;
2146 info->flags &= ~ASYNC_NORMAL_ACTIVE;
2147 info->tty = NULL;
2148 wake_up_interruptible(&info->open_wait);
2149}
2150
2151
2152/* added by James 03-12-2004. */
2153/*
2154 * mxser_rs_break() --- routine which turns the break handling on or off
2155 */
2156static void mxser_rs_break(struct tty_struct *tty, int break_state)
2157{
Jiri Slaby55b307d2006-12-08 02:38:14 -08002158 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08002159 unsigned long flags;
2160
2161 spin_lock_irqsave(&info->slock, flags);
2162 if (break_state == -1)
Jiri Slaby55b307d2006-12-08 02:38:14 -08002163 outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
2164 info->ioaddr + UART_LCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002165 else
Jiri Slaby55b307d2006-12-08 02:38:14 -08002166 outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
2167 info->ioaddr + UART_LCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002168 spin_unlock_irqrestore(&info->slock, flags);
2169}
2170
2171/* (above) added by James. */
2172
Jiri Slaby55b307d2006-12-08 02:38:14 -08002173static void mxser_receive_chars(struct mxser_port *port, int *status)
Jiri Slaby037ad482006-12-08 02:38:11 -08002174{
Jiri Slaby55b307d2006-12-08 02:38:14 -08002175 struct tty_struct *tty = port->tty;
Jiri Slaby037ad482006-12-08 02:38:11 -08002176 unsigned char ch, gdl;
2177 int ignored = 0;
2178 int cnt = 0;
2179 int recv_room;
2180 int max = 256;
2181 unsigned long flags;
2182
Jiri Slaby55b307d2006-12-08 02:38:14 -08002183 spin_lock_irqsave(&port->slock, flags);
Jiri Slaby037ad482006-12-08 02:38:11 -08002184
2185 recv_room = tty->receive_room;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002186 if ((recv_room == 0) && (!port->ldisc_stop_rx)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002187 /* mxser_throttle(tty); */
2188 mxser_stoprx(tty);
2189 /* return; */
2190 }
2191
2192 /* following add by Victor Yu. 09-02-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002193 if (port->board->chip_flag != MOXA_OTHER_UART) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002194
Jiri Slaby3306ce32006-12-08 02:38:13 -08002195 if (*status & UART_LSR_SPECIAL)
Jiri Slaby037ad482006-12-08 02:38:11 -08002196 goto intr_old;
Jiri Slaby037ad482006-12-08 02:38:11 -08002197 /* following add by Victor Yu. 02-11-2004 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002198 if (port->board->chip_flag == MOXA_MUST_MU860_HWID &&
Jiri Slaby037ad482006-12-08 02:38:11 -08002199 (*status & MOXA_MUST_LSR_RERR))
2200 goto intr_old;
2201 /* above add by Victor Yu. 02-14-2004 */
2202 if (*status & MOXA_MUST_LSR_RERR)
2203 goto intr_old;
2204
Jiri Slaby55b307d2006-12-08 02:38:14 -08002205 gdl = inb(port->ioaddr + MOXA_MUST_GDL_REGISTER);
Jiri Slaby037ad482006-12-08 02:38:11 -08002206
2207 /* add by Victor Yu. 02-11-2004 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002208 if (port->board->chip_flag == MOXA_MUST_MU150_HWID)
Jiri Slaby037ad482006-12-08 02:38:11 -08002209 gdl &= MOXA_MUST_GDL_MASK;
2210 if (gdl >= recv_room) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002211 if (!port->ldisc_stop_rx) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002212 /* mxser_throttle(tty); */
2213 mxser_stoprx(tty);
2214 }
2215 /* return; */
2216 }
2217 while (gdl--) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002218 ch = inb(port->ioaddr + UART_RX);
Jiri Slaby037ad482006-12-08 02:38:11 -08002219 tty_insert_flip_char(tty, ch, 0);
2220 cnt++;
Jiri Slaby037ad482006-12-08 02:38:11 -08002221 }
2222 goto end_intr;
2223 }
2224 intr_old:
2225 /* above add by Victor Yu. 09-02-2002 */
2226
2227 do {
2228 if (max-- < 0)
2229 break;
Jiri Slaby037ad482006-12-08 02:38:11 -08002230
Jiri Slaby55b307d2006-12-08 02:38:14 -08002231 ch = inb(port->ioaddr + UART_RX);
Jiri Slaby037ad482006-12-08 02:38:11 -08002232 /* following add by Victor Yu. 09-02-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002233 if (port->board->chip_flag && (*status & UART_LSR_OE)
Jiri Slaby3306ce32006-12-08 02:38:13 -08002234 /*&& !(*status&UART_LSR_DR) */)
Jiri Slaby55b307d2006-12-08 02:38:14 -08002235 outb(0x23, port->ioaddr + UART_FCR);
2236 *status &= port->read_status_mask;
Jiri Slaby037ad482006-12-08 02:38:11 -08002237 /* above add by Victor Yu. 09-02-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002238 if (*status & port->ignore_status_mask) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002239 if (++ignored > 100)
2240 break;
2241 } else {
2242 char flag = 0;
2243 if (*status & UART_LSR_SPECIAL) {
2244 if (*status & UART_LSR_BI) {
2245 flag = TTY_BREAK;
2246/* added by casper 1/11/2000 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002247 port->icount.brk++;
Jiri Slaby3306ce32006-12-08 02:38:13 -08002248
Jiri Slaby55b307d2006-12-08 02:38:14 -08002249 if (port->flags & ASYNC_SAK)
Jiri Slaby037ad482006-12-08 02:38:11 -08002250 do_SAK(tty);
2251 } else if (*status & UART_LSR_PE) {
2252 flag = TTY_PARITY;
2253/* added by casper 1/11/2000 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002254 port->icount.parity++;
Jiri Slaby037ad482006-12-08 02:38:11 -08002255 } else if (*status & UART_LSR_FE) {
2256 flag = TTY_FRAME;
2257/* added by casper 1/11/2000 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002258 port->icount.frame++;
Jiri Slaby037ad482006-12-08 02:38:11 -08002259 } else if (*status & UART_LSR_OE) {
2260 flag = TTY_OVERRUN;
2261/* added by casper 1/11/2000 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002262 port->icount.overrun++;
Jiri Slaby925e9c12006-12-08 02:38:30 -08002263 }
2264 }
Jiri Slaby037ad482006-12-08 02:38:11 -08002265 tty_insert_flip_char(tty, ch, flag);
2266 cnt++;
2267 if (cnt >= recv_room) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002268 if (!port->ldisc_stop_rx) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002269 /* mxser_throttle(tty); */
2270 mxser_stoprx(tty);
2271 }
2272 break;
2273 }
2274
2275 }
2276
2277 /* following add by Victor Yu. 09-02-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002278 if (port->board->chip_flag)
Jiri Slaby037ad482006-12-08 02:38:11 -08002279 break;
Jiri Slaby037ad482006-12-08 02:38:11 -08002280
2281 /* mask by Victor Yu. 09-02-2002
Jiri Slaby55b307d2006-12-08 02:38:14 -08002282 *status = inb(port->ioaddr + UART_LSR) & port->read_status_mask;
Jiri Slaby037ad482006-12-08 02:38:11 -08002283 */
2284 /* following add by Victor Yu. 09-02-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002285 *status = inb(port->ioaddr + UART_LSR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002286 /* above add by Victor Yu. 09-02-2002 */
2287 } while (*status & UART_LSR_DR);
2288
2289end_intr: /* add by Victor Yu. 09-02-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002290 mxvar_log.rxcnt[port->tty->index] += cnt;
2291 port->mon_data.rxcnt += cnt;
2292 port->mon_data.up_rxcnt += cnt;
2293 spin_unlock_irqrestore(&port->slock, flags);
Jiri Slaby037ad482006-12-08 02:38:11 -08002294
2295 tty_flip_buffer_push(tty);
2296}
2297
Jiri Slaby55b307d2006-12-08 02:38:14 -08002298static void mxser_transmit_chars(struct mxser_port *port)
Jiri Slaby037ad482006-12-08 02:38:11 -08002299{
2300 int count, cnt;
2301 unsigned long flags;
2302
Jiri Slaby55b307d2006-12-08 02:38:14 -08002303 spin_lock_irqsave(&port->slock, flags);
Jiri Slaby037ad482006-12-08 02:38:11 -08002304
Jiri Slaby55b307d2006-12-08 02:38:14 -08002305 if (port->x_char) {
2306 outb(port->x_char, port->ioaddr + UART_TX);
2307 port->x_char = 0;
2308 mxvar_log.txcnt[port->tty->index]++;
2309 port->mon_data.txcnt++;
2310 port->mon_data.up_txcnt++;
Jiri Slaby037ad482006-12-08 02:38:11 -08002311
2312/* added by casper 1/11/2000 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002313 port->icount.tx++;
Jiri Slaby3306ce32006-12-08 02:38:13 -08002314 goto unlock;
Jiri Slaby037ad482006-12-08 02:38:11 -08002315 }
2316
Jiri Slaby55b307d2006-12-08 02:38:14 -08002317 if (port->xmit_buf == 0)
Jiri Slaby3306ce32006-12-08 02:38:13 -08002318 goto unlock;
Jiri Slaby037ad482006-12-08 02:38:11 -08002319
Jiri Slaby925e9c12006-12-08 02:38:30 -08002320 if ((port->xmit_cnt <= 0) || port->tty->stopped ||
2321 (port->tty->hw_stopped &&
Jiri Slaby55b307d2006-12-08 02:38:14 -08002322 (port->type != PORT_16550A) &&
2323 (!port->board->chip_flag))) {
2324 port->IER &= ~UART_IER_THRI;
2325 outb(port->IER, port->ioaddr + UART_IER);
Jiri Slaby3306ce32006-12-08 02:38:13 -08002326 goto unlock;
Jiri Slaby037ad482006-12-08 02:38:11 -08002327 }
2328
Jiri Slaby55b307d2006-12-08 02:38:14 -08002329 cnt = port->xmit_cnt;
2330 count = port->xmit_fifo_size;
Jiri Slaby037ad482006-12-08 02:38:11 -08002331 do {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002332 outb(port->xmit_buf[port->xmit_tail++],
2333 port->ioaddr + UART_TX);
2334 port->xmit_tail = port->xmit_tail & (SERIAL_XMIT_SIZE - 1);
2335 if (--port->xmit_cnt <= 0)
Jiri Slaby037ad482006-12-08 02:38:11 -08002336 break;
2337 } while (--count > 0);
Jiri Slaby55b307d2006-12-08 02:38:14 -08002338 mxvar_log.txcnt[port->tty->index] += (cnt - port->xmit_cnt);
Jiri Slaby037ad482006-12-08 02:38:11 -08002339
2340/* added by James 03-12-2004. */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002341 port->mon_data.txcnt += (cnt - port->xmit_cnt);
2342 port->mon_data.up_txcnt += (cnt - port->xmit_cnt);
Jiri Slaby037ad482006-12-08 02:38:11 -08002343
2344/* added by casper 1/11/2000 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002345 port->icount.tx += (cnt - port->xmit_cnt);
Jiri Slaby037ad482006-12-08 02:38:11 -08002346
Jiri Slaby55b307d2006-12-08 02:38:14 -08002347 if (port->xmit_cnt < WAKEUP_CHARS) {
2348 set_bit(MXSER_EVENT_TXLOW, &port->event);
2349 schedule_work(&port->tqueue);
Jiri Slaby037ad482006-12-08 02:38:11 -08002350 }
Jiri Slaby55b307d2006-12-08 02:38:14 -08002351 if (port->xmit_cnt <= 0) {
2352 port->IER &= ~UART_IER_THRI;
2353 outb(port->IER, port->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08002354 }
Jiri Slaby3306ce32006-12-08 02:38:13 -08002355unlock:
Jiri Slaby55b307d2006-12-08 02:38:14 -08002356 spin_unlock_irqrestore(&port->slock, flags);
Jiri Slaby037ad482006-12-08 02:38:11 -08002357}
2358
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002359/*
2360 * This is the serial driver's generic interrupt routine
2361 */
2362static irqreturn_t mxser_interrupt(int irq, void *dev_id, struct pt_regs *regs)
Jiri Slaby037ad482006-12-08 02:38:11 -08002363{
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002364 int status, iir, i;
2365 struct mxser_board *brd = NULL;
2366 struct mxser_port *port;
2367 int max, irqbits, bits, msr;
2368 int pass_counter = 0;
2369 unsigned int int_cnt;
2370 int handled = IRQ_NONE;
Jiri Slaby037ad482006-12-08 02:38:11 -08002371
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002372 /* spin_lock(&gm_lock); */
Jiri Slaby037ad482006-12-08 02:38:11 -08002373
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002374 for (i = 0; i < MXSER_BOARDS; i++)
2375 if (dev_id == &mxser_boards[i]) {
2376 brd = dev_id;
2377 break;
Jiri Slaby037ad482006-12-08 02:38:11 -08002378 }
Jiri Slaby037ad482006-12-08 02:38:11 -08002379
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002380 if (i == MXSER_BOARDS)
2381 goto irq_stop;
2382 if (brd == NULL)
2383 goto irq_stop;
Jiri Slabycd7ed642006-12-08 02:38:29 -08002384 max = brd->info->nports;
Jiri Slaby037ad482006-12-08 02:38:11 -08002385 while (1) {
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002386 irqbits = inb(brd->vector) & brd->vector_mask;
2387 if (irqbits == brd->vector_mask)
Jiri Slaby037ad482006-12-08 02:38:11 -08002388 break;
Jiri Slaby037ad482006-12-08 02:38:11 -08002389
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002390 handled = IRQ_HANDLED;
2391 for (i = 0, bits = 1; i < max; i++, irqbits |= bits, bits <<= 1) {
2392 if (irqbits == brd->vector_mask)
Jiri Slaby037ad482006-12-08 02:38:11 -08002393 break;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002394 if (bits & irqbits)
2395 continue;
2396 port = &brd->ports[i];
Jiri Slaby037ad482006-12-08 02:38:11 -08002397
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002398 int_cnt = 0;
2399 do {
2400 /* following add by Victor Yu. 09-13-2002 */
2401 iir = inb(port->ioaddr + UART_IIR);
2402 if (iir & UART_IIR_NO_INT)
2403 break;
2404 iir &= MOXA_MUST_IIR_MASK;
2405 if (!port->tty) {
2406 status = inb(port->ioaddr + UART_LSR);
2407 outb(0x27, port->ioaddr + UART_FCR);
2408 inb(port->ioaddr + UART_MSR);
2409 break;
Jiri Slaby037ad482006-12-08 02:38:11 -08002410 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002411 /* above add by Victor Yu. 09-13-2002 */
Jiri Slaby037ad482006-12-08 02:38:11 -08002412
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002413 /* following add by Victor Yu. 09-02-2002 */
2414 status = inb(port->ioaddr + UART_LSR);
2415
2416 if (status & UART_LSR_PE)
2417 port->err_shadow |= NPPI_NOTIFY_PARITY;
2418 if (status & UART_LSR_FE)
2419 port->err_shadow |= NPPI_NOTIFY_FRAMING;
2420 if (status & UART_LSR_OE)
2421 port->err_shadow |=
2422 NPPI_NOTIFY_HW_OVERRUN;
2423 if (status & UART_LSR_BI)
2424 port->err_shadow |= NPPI_NOTIFY_BREAK;
2425
2426 if (port->board->chip_flag) {
2427 /*
2428 if ( (status & 0x02) && !(status & 0x01) ) {
2429 outb(port->ioaddr+UART_FCR, 0x23);
2430 continue;
2431 }
2432 */
2433 if (iir == MOXA_MUST_IIR_GDA ||
2434 iir == MOXA_MUST_IIR_RDA ||
2435 iir == MOXA_MUST_IIR_RTO ||
2436 iir == MOXA_MUST_IIR_LSR)
2437 mxser_receive_chars(port,
2438 &status);
2439
2440 } else {
2441 /* above add by Victor Yu. 09-02-2002 */
2442
2443 status &= port->read_status_mask;
2444 if (status & UART_LSR_DR)
2445 mxser_receive_chars(port,
2446 &status);
2447 }
2448 msr = inb(port->ioaddr + UART_MSR);
2449 if (msr & UART_MSR_ANY_DELTA)
2450 mxser_check_modem_status(port, msr);
2451
2452 /* following add by Victor Yu. 09-13-2002 */
2453 if (port->board->chip_flag) {
2454 if (iir == 0x02 && (status &
2455 UART_LSR_THRE))
2456 mxser_transmit_chars(port);
2457 } else {
2458 /* above add by Victor Yu. 09-13-2002 */
2459
2460 if (status & UART_LSR_THRE)
2461 mxser_transmit_chars(port);
2462 }
2463 } while (int_cnt++ < MXSER_ISR_PASS_LIMIT);
2464 }
2465 if (pass_counter++ > MXSER_ISR_PASS_LIMIT)
2466 break; /* Prevent infinite loops */
2467 }
2468
2469 irq_stop:
2470 /* spin_unlock(&gm_lock); */
2471 return handled;
2472}
2473
2474static const struct tty_operations mxser_ops = {
2475 .open = mxser_open,
2476 .close = mxser_close,
2477 .write = mxser_write,
2478 .put_char = mxser_put_char,
2479 .flush_chars = mxser_flush_chars,
2480 .write_room = mxser_write_room,
2481 .chars_in_buffer = mxser_chars_in_buffer,
2482 .flush_buffer = mxser_flush_buffer,
2483 .ioctl = mxser_ioctl,
2484 .throttle = mxser_throttle,
2485 .unthrottle = mxser_unthrottle,
2486 .set_termios = mxser_set_termios,
2487 .stop = mxser_stop,
2488 .start = mxser_start,
2489 .hangup = mxser_hangup,
2490 .break_ctl = mxser_rs_break,
2491 .wait_until_sent = mxser_wait_until_sent,
2492 .tiocmget = mxser_tiocmget,
2493 .tiocmset = mxser_tiocmset,
2494};
2495
2496/*
2497 * The MOXA Smartio/Industio serial driver boot-time initialization code!
2498 */
2499
Jiri Slaby171d3a82006-12-08 02:38:25 -08002500static void mxser_release_res(struct mxser_board *brd, unsigned int irq)
2501{
2502 struct pci_dev *pdev = brd->pdev;
2503
2504 if (irq)
2505 free_irq(brd->irq, brd);
2506 if (pdev != NULL) { /* PCI */
2507 pci_release_region(pdev, 2);
2508 pci_release_region(pdev, 3);
2509 pci_dev_put(pdev);
2510 } else {
Jiri Slabycd7ed642006-12-08 02:38:29 -08002511 release_region(brd->ports[0].ioaddr, 8 * brd->info->nports);
Jiri Slaby171d3a82006-12-08 02:38:25 -08002512 release_region(brd->vector, 1);
2513 }
2514}
2515
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002516static int __devinit mxser_initbrd(struct mxser_board *brd)
2517{
2518 struct mxser_port *info;
2519 unsigned int i;
2520 int retval;
2521
2522 printk(KERN_INFO "max. baud rate = %d bps.\n", brd->ports[0].max_baud);
2523
Jiri Slabycd7ed642006-12-08 02:38:29 -08002524 for (i = 0; i < brd->info->nports; i++) {
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002525 info = &brd->ports[i];
2526 info->board = brd;
2527 info->stop_rx = 0;
2528 info->ldisc_stop_rx = 0;
2529
2530 /* Enhance mode enabled here */
2531 if (brd->chip_flag != MOXA_OTHER_UART)
2532 ENABLE_MOXA_MUST_ENCHANCE_MODE(info->ioaddr);
2533
2534 info->flags = ASYNC_SHARE_IRQ;
2535 info->type = brd->uart_type;
2536
2537 process_txrx_fifo(info);
2538
2539 info->custom_divisor = info->baud_base * 16;
2540 info->close_delay = 5 * HZ / 10;
2541 info->closing_wait = 30 * HZ;
2542 INIT_WORK(&info->tqueue, mxser_do_softint, info);
2543 info->normal_termios = mxvar_sdriver->init_termios;
2544 init_waitqueue_head(&info->open_wait);
2545 init_waitqueue_head(&info->close_wait);
2546 init_waitqueue_head(&info->delta_msr_wait);
2547 memset(&info->mon_data, 0, sizeof(struct mxser_mon));
2548 info->err_shadow = 0;
2549 spin_lock_init(&info->slock);
2550
2551 /* before set INT ISR, disable all int */
2552 outb(inb(info->ioaddr + UART_IER) & 0xf0,
2553 info->ioaddr + UART_IER);
2554 }
Jiri Slaby037ad482006-12-08 02:38:11 -08002555 /*
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002556 * Allocate the IRQ if necessary
Jiri Slaby037ad482006-12-08 02:38:11 -08002557 */
Jiri Slaby037ad482006-12-08 02:38:11 -08002558
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002559 retval = request_irq(brd->irq, mxser_interrupt,
2560 (brd->ports[0].flags & ASYNC_SHARE_IRQ) ? IRQF_SHARED :
2561 IRQF_DISABLED, "mxser", brd);
2562 if (retval) {
2563 printk(KERN_ERR "Board %s: Request irq failed, IRQ (%d) may "
2564 "conflict with another device.\n",
Jiri Slabycd7ed642006-12-08 02:38:29 -08002565 brd->info->name, brd->irq);
Jiri Slaby171d3a82006-12-08 02:38:25 -08002566 /* We hold resources, we need to release them. */
2567 mxser_release_res(brd, 0);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002568 return retval;
Jiri Slaby037ad482006-12-08 02:38:11 -08002569 }
Jiri Slaby037ad482006-12-08 02:38:11 -08002570 return 0;
2571}
2572
Jiri Slaby943f2952006-12-08 02:38:15 -08002573static int __init mxser_get_ISA_conf(int cap, struct mxser_board *brd)
Jiri Slaby037ad482006-12-08 02:38:11 -08002574{
2575 int id, i, bits;
2576 unsigned short regs[16], irq;
2577 unsigned char scratch, scratch2;
2578
Jiri Slaby55b307d2006-12-08 02:38:14 -08002579 brd->chip_flag = MOXA_OTHER_UART;
Jiri Slaby037ad482006-12-08 02:38:11 -08002580
2581 id = mxser_read_register(cap, regs);
Jiri Slabycd7ed642006-12-08 02:38:29 -08002582 switch (id) {
2583 case C168_ASIC_ID:
2584 brd->info = &mxser_cards[0];
2585 break;
2586 case C104_ASIC_ID:
2587 brd->info = &mxser_cards[1];
2588 break;
2589 case CI104J_ASIC_ID:
2590 brd->info = &mxser_cards[2];
2591 break;
2592 case C102_ASIC_ID:
2593 brd->info = &mxser_cards[5];
2594 break;
2595 case CI132_ASIC_ID:
2596 brd->info = &mxser_cards[6];
2597 break;
2598 case CI134_ASIC_ID:
2599 brd->info = &mxser_cards[7];
2600 break;
2601 default:
Jiri Slaby037ad482006-12-08 02:38:11 -08002602 return 0;
Jiri Slabycd7ed642006-12-08 02:38:29 -08002603 }
Jiri Slaby037ad482006-12-08 02:38:11 -08002604
2605 irq = 0;
Jiri Slabycd7ed642006-12-08 02:38:29 -08002606 /* some ISA cards have 2 ports, but we want to see them as 4-port (why?)
2607 Flag-hack checks if configuration should be read as 2-port here. */
2608 if (brd->info->nports == 2 || (brd->info->flags & MXSER_HAS2)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002609 irq = regs[9] & 0xF000;
2610 irq = irq | (irq >> 4);
2611 if (irq != (regs[9] & 0xFF00))
2612 return MXSER_ERR_IRQ_CONFLIT;
Jiri Slabycd7ed642006-12-08 02:38:29 -08002613 } else if (brd->info->nports == 4) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002614 irq = regs[9] & 0xF000;
2615 irq = irq | (irq >> 4);
2616 irq = irq | (irq >> 8);
2617 if (irq != regs[9])
2618 return MXSER_ERR_IRQ_CONFLIT;
Jiri Slabycd7ed642006-12-08 02:38:29 -08002619 } else if (brd->info->nports == 8) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002620 irq = regs[9] & 0xF000;
2621 irq = irq | (irq >> 4);
2622 irq = irq | (irq >> 8);
2623 if ((irq != regs[9]) || (irq != regs[10]))
2624 return MXSER_ERR_IRQ_CONFLIT;
2625 }
2626
2627 if (!irq)
2628 return MXSER_ERR_IRQ;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002629 brd->irq = ((int)(irq & 0xF000) >> 12);
Jiri Slaby037ad482006-12-08 02:38:11 -08002630 for (i = 0; i < 8; i++)
Jiri Slaby55b307d2006-12-08 02:38:14 -08002631 brd->ports[i].ioaddr = (int) regs[i + 1] & 0xFFF8;
Jiri Slaby037ad482006-12-08 02:38:11 -08002632 if ((regs[12] & 0x80) == 0)
2633 return MXSER_ERR_VECTOR;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002634 brd->vector = (int)regs[11]; /* interrupt vector */
Jiri Slaby037ad482006-12-08 02:38:11 -08002635 if (id == 1)
Jiri Slaby55b307d2006-12-08 02:38:14 -08002636 brd->vector_mask = 0x00FF;
Jiri Slaby037ad482006-12-08 02:38:11 -08002637 else
Jiri Slaby55b307d2006-12-08 02:38:14 -08002638 brd->vector_mask = 0x000F;
Jiri Slaby037ad482006-12-08 02:38:11 -08002639 for (i = 7, bits = 0x0100; i >= 0; i--, bits <<= 1) {
2640 if (regs[12] & bits) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002641 brd->ports[i].baud_base = 921600;
2642 brd->ports[i].max_baud = 921600; /* add by Victor Yu. 09-04-2002 */
Jiri Slaby037ad482006-12-08 02:38:11 -08002643 } else {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002644 brd->ports[i].baud_base = 115200;
2645 brd->ports[i].max_baud = 115200; /* add by Victor Yu. 09-04-2002 */
Jiri Slaby037ad482006-12-08 02:38:11 -08002646 }
2647 }
2648 scratch2 = inb(cap + UART_LCR) & (~UART_LCR_DLAB);
2649 outb(scratch2 | UART_LCR_DLAB, cap + UART_LCR);
2650 outb(0, cap + UART_EFR); /* EFR is the same as FCR */
2651 outb(scratch2, cap + UART_LCR);
2652 outb(UART_FCR_ENABLE_FIFO, cap + UART_FCR);
2653 scratch = inb(cap + UART_IIR);
2654
2655 if (scratch & 0xC0)
Jiri Slaby55b307d2006-12-08 02:38:14 -08002656 brd->uart_type = PORT_16550A;
Jiri Slaby037ad482006-12-08 02:38:11 -08002657 else
Jiri Slaby55b307d2006-12-08 02:38:14 -08002658 brd->uart_type = PORT_16450;
Jiri Slabycd7ed642006-12-08 02:38:29 -08002659 if (!request_region(brd->ports[0].ioaddr, 8 * brd->info->nports,
2660 "mxser(IO)"))
Jiri Slaby7a7a5c32006-12-08 02:38:17 -08002661 return MXSER_ERR_IOADDR;
2662 if (!request_region(brd->vector, 1, "mxser(vector)")) {
Jiri Slabycd7ed642006-12-08 02:38:29 -08002663 release_region(brd->ports[0].ioaddr, 8 * brd->info->nports);
Jiri Slaby7a7a5c32006-12-08 02:38:17 -08002664 return MXSER_ERR_VECTOR;
2665 }
Jiri Slabycd7ed642006-12-08 02:38:29 -08002666 return brd->info->nports;
Jiri Slaby037ad482006-12-08 02:38:11 -08002667}
2668
Jiri Slabycd7ed642006-12-08 02:38:29 -08002669static int __init mxser_get_PCI_conf(const struct pci_device_id *ent,
2670 struct mxser_board *brd, struct pci_dev *pdev)
Jiri Slaby037ad482006-12-08 02:38:11 -08002671{
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002672 unsigned int i, j;
2673 unsigned long ioaddress;
2674 int retval;
Jiri Slaby037ad482006-12-08 02:38:11 -08002675
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002676 /* io address */
Jiri Slabycd7ed642006-12-08 02:38:29 -08002677 brd->info = &mxser_cards[ent->driver_data];
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002678 ioaddress = pci_resource_start(pdev, 2);
2679 retval = pci_request_region(pdev, 2, "mxser(IO)");
2680 if (retval)
2681 goto err;
2682
Jiri Slabycd7ed642006-12-08 02:38:29 -08002683 for (i = 0; i < brd->info->nports; i++)
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002684 brd->ports[i].ioaddr = ioaddress + 8 * i;
2685
2686 /* vector */
2687 ioaddress = pci_resource_start(pdev, 3);
2688 retval = pci_request_region(pdev, 3, "mxser(vector)");
2689 if (retval)
2690 goto err_relio;
2691 brd->vector = ioaddress;
2692
2693 /* irq */
2694 brd->irq = pdev->irq;
2695
2696 brd->chip_flag = CheckIsMoxaMust(brd->ports[0].ioaddr);
2697 brd->uart_type = PORT_16550A;
2698 brd->vector_mask = 0;
2699
Jiri Slabycd7ed642006-12-08 02:38:29 -08002700 for (i = 0; i < brd->info->nports; i++) {
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002701 for (j = 0; j < UART_INFO_NUM; j++) {
2702 if (Gpci_uart_info[j].type == brd->chip_flag) {
2703 brd->ports[i].max_baud =
2704 Gpci_uart_info[j].max_baud;
2705
2706 /* exception....CP-102 */
Jiri Slabycd7ed642006-12-08 02:38:29 -08002707 if (brd->info->flags & MXSER_HIGHBAUD)
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002708 brd->ports[i].max_baud = 921600;
2709 break;
Jiri Slaby037ad482006-12-08 02:38:11 -08002710 }
2711 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002712 }
2713
2714 if (brd->chip_flag == MOXA_MUST_MU860_HWID) {
Jiri Slabycd7ed642006-12-08 02:38:29 -08002715 for (i = 0; i < brd->info->nports; i++) {
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002716 if (i < 4)
2717 brd->ports[i].opmode_ioaddr = ioaddress + 4;
2718 else
2719 brd->ports[i].opmode_ioaddr = ioaddress + 0x0c;
Jiri Slaby037ad482006-12-08 02:38:11 -08002720 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002721 outb(0, ioaddress + 4); /* default set to RS232 mode */
2722 outb(0, ioaddress + 0x0c); /* default set to RS232 mode */
Jiri Slaby037ad482006-12-08 02:38:11 -08002723 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002724
Jiri Slabycd7ed642006-12-08 02:38:29 -08002725 for (i = 0; i < brd->info->nports; i++) {
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002726 brd->vector_mask |= (1 << i);
2727 brd->ports[i].baud_base = 921600;
2728 }
2729 return 0;
2730err_relio:
2731 pci_release_region(pdev, 2);
2732err:
2733 return retval;
Jiri Slaby037ad482006-12-08 02:38:11 -08002734}
2735
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002736static int __init mxser_module_init(void)
Jiri Slaby037ad482006-12-08 02:38:11 -08002737{
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002738 struct pci_dev *pdev = NULL;
2739 struct mxser_board *brd;
Jiri Slabyeae44362006-12-08 02:38:27 -08002740 unsigned long cap;
2741 unsigned int i, m, isaloop;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002742 int retval, b, n;
Jiri Slaby037ad482006-12-08 02:38:11 -08002743
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002744 pr_debug("Loading module mxser ...\n");
Jiri Slaby037ad482006-12-08 02:38:11 -08002745
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002746 mxvar_sdriver = alloc_tty_driver(MXSER_PORTS + 1);
2747 if (!mxvar_sdriver)
2748 return -ENOMEM;
2749 spin_lock_init(&gm_lock);
2750
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002751 printk(KERN_INFO "MOXA Smartio/Industio family driver version %s\n",
2752 MXSER_VERSION);
2753
2754 /* Initialize the tty_driver structure */
2755 mxvar_sdriver->magic = TTY_DRIVER_MAGIC;
2756 mxvar_sdriver->name = "ttyM";
2757 mxvar_sdriver->major = ttymajor;
2758 mxvar_sdriver->minor_start = 0;
2759 mxvar_sdriver->num = MXSER_PORTS + 1;
2760 mxvar_sdriver->type = TTY_DRIVER_TYPE_SERIAL;
2761 mxvar_sdriver->subtype = SERIAL_TYPE_NORMAL;
2762 mxvar_sdriver->init_termios = tty_std_termios;
2763 mxvar_sdriver->init_termios.c_cflag = B9600|CS8|CREAD|HUPCL|CLOCAL;
Jiri Slaby938ef182006-12-08 02:38:28 -08002764 mxvar_sdriver->flags = TTY_DRIVER_REAL_RAW|TTY_DRIVER_DYNAMIC_DEV;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002765 tty_set_operations(mxvar_sdriver, &mxser_ops);
2766 mxvar_sdriver->ttys = mxvar_tty;
2767 mxvar_sdriver->termios = mxvar_termios;
2768 mxvar_sdriver->termios_locked = mxvar_termios_locked;
2769
Jiri Slaby938ef182006-12-08 02:38:28 -08002770 retval = tty_register_driver(mxvar_sdriver);
2771 if (retval) {
2772 printk(KERN_ERR "Couldn't install MOXA Smartio/Industio family "
2773 "tty driver !\n");
2774 goto err_put;
2775 }
2776
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002777 mxvar_diagflag = 0;
2778
2779 m = 0;
2780 /* Start finding ISA boards here */
Jiri Slabyeae44362006-12-08 02:38:27 -08002781 for (isaloop = 0; isaloop < 2; isaloop++)
2782 for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
2783 if (!isaloop)
2784 cap = mxserBoardCAP[b]; /* predefined */
2785 else
2786 cap = ioaddr[b]; /* module param */
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002787
Jiri Slabyeae44362006-12-08 02:38:27 -08002788 if (!cap)
2789 continue;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002790
Jiri Slabyeae44362006-12-08 02:38:27 -08002791 brd = &mxser_boards[m];
2792 retval = mxser_get_ISA_conf(cap, brd);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002793
Jiri Slabyeae44362006-12-08 02:38:27 -08002794 if (retval != 0)
2795 printk(KERN_INFO "Found MOXA %s board "
2796 "(CAP=0x%x)\n",
Jiri Slabycd7ed642006-12-08 02:38:29 -08002797 brd->info->name, ioaddr[b]);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002798
Jiri Slabyeae44362006-12-08 02:38:27 -08002799 if (retval <= 0) {
2800 if (retval == MXSER_ERR_IRQ)
2801 printk(KERN_ERR "Invalid interrupt "
2802 "number, board not "
2803 "configured\n");
2804 else if (retval == MXSER_ERR_IRQ_CONFLIT)
2805 printk(KERN_ERR "Invalid interrupt "
2806 "number, board not "
2807 "configured\n");
2808 else if (retval == MXSER_ERR_VECTOR)
2809 printk(KERN_ERR "Invalid interrupt "
2810 "vector, board not "
2811 "configured\n");
2812 else if (retval == MXSER_ERR_IOADDR)
2813 printk(KERN_ERR "Invalid I/O address, "
2814 "board not configured\n");
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002815
Jiri Slabyeae44362006-12-08 02:38:27 -08002816 continue;
2817 }
2818
2819 brd->pdev = NULL;
2820
2821 /* mxser_initbrd will hook ISR. */
2822 if (mxser_initbrd(brd) < 0)
2823 continue;
2824
Jiri Slabycd7ed642006-12-08 02:38:29 -08002825 for (i = 0; i < brd->info->nports; i++)
Jiri Slaby938ef182006-12-08 02:38:28 -08002826 tty_register_device(mxvar_sdriver,
2827 m * MXSER_PORTS_PER_BOARD + i, NULL);
2828
Jiri Slabyeae44362006-12-08 02:38:27 -08002829 m++;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002830 }
2831
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002832 /* start finding PCI board here */
2833 n = ARRAY_SIZE(mxser_pcibrds) - 1;
2834 b = 0;
2835 while (b < n) {
2836 pdev = pci_get_device(mxser_pcibrds[b].vendor,
2837 mxser_pcibrds[b].device, pdev);
2838 if (pdev == NULL) {
2839 b++;
2840 continue;
2841 }
2842 printk(KERN_INFO "Found MOXA %s board(BusNo=%d,DevNo=%d)\n",
Jiri Slabycd7ed642006-12-08 02:38:29 -08002843 mxser_cards[mxser_pcibrds[b].driver_data].name,
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002844 pdev->bus->number, PCI_SLOT(pdev->devfn));
2845 if (m >= MXSER_BOARDS)
2846 printk(KERN_ERR
2847 "Too many Smartio/Industio family boards find "
2848 "(maximum %d), board not configured\n",
2849 MXSER_BOARDS);
2850 else {
2851 if (pci_enable_device(pdev)) {
2852 printk(KERN_ERR "Moxa SmartI/O PCI enable "
2853 "fail !\n");
2854 continue;
2855 }
2856 brd = &mxser_boards[m];
2857 brd->pdev = pdev;
Jiri Slabycd7ed642006-12-08 02:38:29 -08002858 retval = mxser_get_PCI_conf(&mxser_pcibrds[b],
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002859 brd, pdev);
2860 if (retval < 0) {
2861 if (retval == MXSER_ERR_IRQ)
2862 printk(KERN_ERR
2863 "Invalid interrupt number, "
2864 "board not configured\n");
2865 else if (retval == MXSER_ERR_IRQ_CONFLIT)
2866 printk(KERN_ERR
2867 "Invalid interrupt number, "
2868 "board not configured\n");
2869 else if (retval == MXSER_ERR_VECTOR)
2870 printk(KERN_ERR
2871 "Invalid interrupt vector, "
2872 "board not configured\n");
2873 else if (retval == MXSER_ERR_IOADDR)
2874 printk(KERN_ERR
2875 "Invalid I/O address, "
2876 "board not configured\n");
2877 continue;
2878 }
2879 /* mxser_initbrd will hook ISR. */
2880 if (mxser_initbrd(brd) < 0)
2881 continue;
Jiri Slabycd7ed642006-12-08 02:38:29 -08002882 for (i = 0; i < brd->info->nports; i++)
Jiri Slaby938ef182006-12-08 02:38:28 -08002883 tty_register_device(mxvar_sdriver,
2884 m * MXSER_PORTS_PER_BOARD + i,
2885 &pdev->dev);
2886
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002887 m++;
2888 /* Keep an extra reference if we succeeded. It will
2889 be returned at unload time */
2890 pci_dev_get(pdev);
2891 }
2892 }
2893
Jiri Slaby938ef182006-12-08 02:38:28 -08002894 if (!m) {
2895 retval = -ENODEV;
2896 goto err_unr;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002897 }
2898
2899 pr_debug("Done.\n");
2900
Jiri Slaby938ef182006-12-08 02:38:28 -08002901 return 0;
2902err_unr:
2903 tty_unregister_driver(mxvar_sdriver);
2904err_put:
2905 put_tty_driver(mxvar_sdriver);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002906 return retval;
Jiri Slaby037ad482006-12-08 02:38:11 -08002907}
2908
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002909static void __exit mxser_module_exit(void)
Jiri Slaby037ad482006-12-08 02:38:11 -08002910{
Jiri Slabyead568c2006-12-08 02:38:26 -08002911 unsigned int i;
Jiri Slaby037ad482006-12-08 02:38:11 -08002912
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002913 pr_debug("Unloading module mxser ...\n");
2914
Jiri Slaby938ef182006-12-08 02:38:28 -08002915 for (i = 0; i < MXSER_PORTS; i++)
2916 tty_unregister_device(mxvar_sdriver, i);
Jiri Slabyead568c2006-12-08 02:38:26 -08002917 tty_unregister_driver(mxvar_sdriver);
2918 put_tty_driver(mxvar_sdriver);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002919
Jiri Slaby171d3a82006-12-08 02:38:25 -08002920 for (i = 0; i < MXSER_BOARDS; i++)
Jiri Slabycd7ed642006-12-08 02:38:29 -08002921 if (mxser_boards[i].info != NULL)
Jiri Slaby171d3a82006-12-08 02:38:25 -08002922 mxser_release_res(&mxser_boards[i], 1);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002923
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002924 pr_debug("Done.\n");
Jiri Slaby037ad482006-12-08 02:38:11 -08002925}
2926
2927module_init(mxser_module_init);
2928module_exit(mxser_module_exit);