blob: a3328bb147b96f473a4498ae82d2cc6c1d6f457e [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
Jiri Slaby55b307d2006-12-08 02:38:14 -0800349static void process_txrx_fifo(struct mxser_port *info)
Jiri Slaby037ad482006-12-08 02:38:11 -0800350{
351 int i;
352
353 if ((info->type == PORT_16450) || (info->type == PORT_8250)) {
354 info->rx_trigger = 1;
355 info->rx_high_water = 1;
356 info->rx_low_water = 1;
357 info->xmit_fifo_size = 1;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800358 } else
359 for (i = 0; i < UART_INFO_NUM; i++)
360 if (info->board->chip_flag == Gpci_uart_info[i].type) {
Jiri Slaby037ad482006-12-08 02:38:11 -0800361 info->rx_trigger = Gpci_uart_info[i].rx_trigger;
362 info->rx_low_water = Gpci_uart_info[i].rx_low_water;
363 info->rx_high_water = Gpci_uart_info[i].rx_high_water;
364 info->xmit_fifo_size = Gpci_uart_info[i].xmit_fifo_size;
365 break;
366 }
Jiri Slaby037ad482006-12-08 02:38:11 -0800367}
368
Jiri Slaby037ad482006-12-08 02:38:11 -0800369static void mxser_do_softint(void *private_)
370{
Jiri Slaby55b307d2006-12-08 02:38:14 -0800371 struct mxser_port *info = private_;
Jiri Slaby037ad482006-12-08 02:38:11 -0800372 struct tty_struct *tty;
373
374 tty = info->tty;
375
Jiri Slaby3306ce32006-12-08 02:38:13 -0800376 if (test_and_clear_bit(MXSER_EVENT_TXLOW, &info->event))
377 tty_wakeup(tty);
378 if (test_and_clear_bit(MXSER_EVENT_HANGUP, &info->event))
379 tty_hangup(tty);
Jiri Slaby037ad482006-12-08 02:38:11 -0800380}
381
Jiri Slaby55b307d2006-12-08 02:38:14 -0800382static unsigned char mxser_get_msr(int baseaddr, int mode, int port)
Jiri Slaby037ad482006-12-08 02:38:11 -0800383{
384 unsigned char status = 0;
385
386 status = inb(baseaddr + UART_MSR);
387
388 mxser_msr[port] &= 0x0F;
389 mxser_msr[port] |= status;
390 status = mxser_msr[port];
391 if (mode)
392 mxser_msr[port] = 0;
393
394 return status;
395}
396
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800397static int mxser_block_til_ready(struct tty_struct *tty, struct file *filp,
398 struct mxser_port *port)
399{
400 DECLARE_WAITQUEUE(wait, current);
401 int retval;
402 int do_clocal = 0;
403 unsigned long flags;
404
405 /*
406 * If non-blocking mode is set, or the port is not enabled,
407 * then make the check up front and then exit.
408 */
Jiri Slaby214efeb2006-12-08 02:38:25 -0800409 if ((filp->f_flags & O_NONBLOCK) ||
410 test_bit(TTY_IO_ERROR, &tty->flags)) {
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800411 port->flags |= ASYNC_NORMAL_ACTIVE;
412 return 0;
413 }
414
415 if (tty->termios->c_cflag & CLOCAL)
416 do_clocal = 1;
417
418 /*
419 * Block waiting for the carrier detect and the line to become
420 * free (i.e., not in use by the callout). While we are in
421 * this loop, port->count is dropped by one, so that
422 * mxser_close() knows when to free things. We restore it upon
423 * exit, either normal or abnormal.
424 */
425 retval = 0;
426 add_wait_queue(&port->open_wait, &wait);
427
428 spin_lock_irqsave(&port->slock, flags);
429 if (!tty_hung_up_p(filp))
430 port->count--;
431 spin_unlock_irqrestore(&port->slock, flags);
432 port->blocked_open++;
433 while (1) {
434 spin_lock_irqsave(&port->slock, flags);
435 outb(inb(port->ioaddr + UART_MCR) |
436 UART_MCR_DTR | UART_MCR_RTS, port->ioaddr + UART_MCR);
437 spin_unlock_irqrestore(&port->slock, flags);
438 set_current_state(TASK_INTERRUPTIBLE);
439 if (tty_hung_up_p(filp) || !(port->flags & ASYNC_INITIALIZED)) {
440 if (port->flags & ASYNC_HUP_NOTIFY)
441 retval = -EAGAIN;
442 else
443 retval = -ERESTARTSYS;
444 break;
445 }
446 if (!(port->flags & ASYNC_CLOSING) &&
447 (do_clocal ||
448 (inb(port->ioaddr + UART_MSR) & UART_MSR_DCD)))
449 break;
450 if (signal_pending(current)) {
451 retval = -ERESTARTSYS;
452 break;
453 }
454 schedule();
455 }
456 set_current_state(TASK_RUNNING);
457 remove_wait_queue(&port->open_wait, &wait);
458 if (!tty_hung_up_p(filp))
459 port->count++;
460 port->blocked_open--;
461 if (retval)
462 return retval;
463 port->flags |= ASYNC_NORMAL_ACTIVE;
464 return 0;
465}
466
467static int mxser_set_baud(struct mxser_port *info, long newspd)
468{
469 int quot = 0;
470 unsigned char cval;
471 int ret = 0;
472 unsigned long flags;
473
474 if (!info->tty || !info->tty->termios)
475 return ret;
476
477 if (!(info->ioaddr))
478 return ret;
479
480 if (newspd > info->max_baud)
481 return 0;
482
483 info->realbaud = newspd;
484 if (newspd == 134) {
485 quot = (2 * info->baud_base / 269);
486 } else if (newspd) {
487 quot = info->baud_base / newspd;
488 if (quot == 0)
489 quot = 1;
490 } else {
491 quot = 0;
492 }
493
494 info->timeout = ((info->xmit_fifo_size * HZ * 10 * quot) / info->baud_base);
495 info->timeout += HZ / 50; /* Add .02 seconds of slop */
496
497 if (quot) {
498 spin_lock_irqsave(&info->slock, flags);
499 info->MCR |= UART_MCR_DTR;
500 outb(info->MCR, info->ioaddr + UART_MCR);
501 spin_unlock_irqrestore(&info->slock, flags);
502 } else {
503 spin_lock_irqsave(&info->slock, flags);
504 info->MCR &= ~UART_MCR_DTR;
505 outb(info->MCR, info->ioaddr + UART_MCR);
506 spin_unlock_irqrestore(&info->slock, flags);
507 return ret;
508 }
509
510 cval = inb(info->ioaddr + UART_LCR);
511
512 outb(cval | UART_LCR_DLAB, info->ioaddr + UART_LCR); /* set DLAB */
513
514 outb(quot & 0xff, info->ioaddr + UART_DLL); /* LS of divisor */
515 outb(quot >> 8, info->ioaddr + UART_DLM); /* MS of divisor */
516 outb(cval, info->ioaddr + UART_LCR); /* reset DLAB */
517
518
519 return ret;
520}
521
522/*
523 * This routine is called to set the UART divisor registers to match
524 * the specified baud rate for a serial port.
525 */
526static int mxser_change_speed(struct mxser_port *info,
527 struct termios *old_termios)
528{
529 unsigned cflag, cval, fcr;
530 int ret = 0;
531 unsigned char status;
532 long baud;
533 unsigned long flags;
534
535 if (!info->tty || !info->tty->termios)
536 return ret;
537 cflag = info->tty->termios->c_cflag;
538 if (!(info->ioaddr))
539 return ret;
540
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800541 if (mxser_set_baud_method[info->tty->index] == 0) {
542 baud = tty_get_baud_rate(info->tty);
543 mxser_set_baud(info, baud);
544 }
545
546 /* byte size and parity */
547 switch (cflag & CSIZE) {
548 case CS5:
549 cval = 0x00;
550 break;
551 case CS6:
552 cval = 0x01;
553 break;
554 case CS7:
555 cval = 0x02;
556 break;
557 case CS8:
558 cval = 0x03;
559 break;
560 default:
561 cval = 0x00;
562 break; /* too keep GCC shut... */
563 }
564 if (cflag & CSTOPB)
565 cval |= 0x04;
566 if (cflag & PARENB)
567 cval |= UART_LCR_PARITY;
568 if (!(cflag & PARODD))
569 cval |= UART_LCR_EPAR;
570 if (cflag & CMSPAR)
571 cval |= UART_LCR_SPAR;
572
573 if ((info->type == PORT_8250) || (info->type == PORT_16450)) {
574 if (info->board->chip_flag) {
575 fcr = UART_FCR_ENABLE_FIFO;
576 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
577 SET_MOXA_MUST_FIFO_VALUE(info);
578 } else
579 fcr = 0;
580 } else {
581 fcr = UART_FCR_ENABLE_FIFO;
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800582 if (info->board->chip_flag) {
583 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
584 SET_MOXA_MUST_FIFO_VALUE(info);
585 } else {
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800586 switch (info->rx_trigger) {
587 case 1:
588 fcr |= UART_FCR_TRIGGER_1;
589 break;
590 case 4:
591 fcr |= UART_FCR_TRIGGER_4;
592 break;
593 case 8:
594 fcr |= UART_FCR_TRIGGER_8;
595 break;
596 default:
597 fcr |= UART_FCR_TRIGGER_14;
598 break;
599 }
600 }
601 }
602
603 /* CTS flow control flag and modem status interrupts */
604 info->IER &= ~UART_IER_MSI;
605 info->MCR &= ~UART_MCR_AFE;
606 if (cflag & CRTSCTS) {
607 info->flags |= ASYNC_CTS_FLOW;
608 info->IER |= UART_IER_MSI;
609 if ((info->type == PORT_16550A) || (info->board->chip_flag)) {
610 info->MCR |= UART_MCR_AFE;
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800611 } else {
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800612 status = inb(info->ioaddr + UART_MSR);
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800613 if (info->tty->hw_stopped) {
614 if (status & UART_MSR_CTS) {
615 info->tty->hw_stopped = 0;
616 if (info->type != PORT_16550A &&
617 !info->board->chip_flag) {
618 outb(info->IER & ~UART_IER_THRI,
619 info->ioaddr +
620 UART_IER);
621 info->IER |= UART_IER_THRI;
622 outb(info->IER, info->ioaddr +
623 UART_IER);
624 }
625 set_bit(MXSER_EVENT_TXLOW, &info->event);
626 schedule_work(&info->tqueue); }
627 } else {
628 if (!(status & UART_MSR_CTS)) {
629 info->tty->hw_stopped = 1;
630 if ((info->type != PORT_16550A) &&
631 (!info->board->chip_flag)) {
632 info->IER &= ~UART_IER_THRI;
633 outb(info->IER, info->ioaddr +
634 UART_IER);
635 }
636 }
637 }
638 }
639 } else {
640 info->flags &= ~ASYNC_CTS_FLOW;
641 }
642 outb(info->MCR, info->ioaddr + UART_MCR);
643 if (cflag & CLOCAL) {
644 info->flags &= ~ASYNC_CHECK_CD;
645 } else {
646 info->flags |= ASYNC_CHECK_CD;
647 info->IER |= UART_IER_MSI;
648 }
649 outb(info->IER, info->ioaddr + UART_IER);
650
651 /*
652 * Set up parity check flag
653 */
654 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
655 if (I_INPCK(info->tty))
656 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
657 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
658 info->read_status_mask |= UART_LSR_BI;
659
660 info->ignore_status_mask = 0;
661
662 if (I_IGNBRK(info->tty)) {
663 info->ignore_status_mask |= UART_LSR_BI;
664 info->read_status_mask |= UART_LSR_BI;
665 /*
666 * If we're ignore parity and break indicators, ignore
667 * overruns too. (For real raw support).
668 */
669 if (I_IGNPAR(info->tty)) {
670 info->ignore_status_mask |=
671 UART_LSR_OE |
672 UART_LSR_PE |
673 UART_LSR_FE;
674 info->read_status_mask |=
675 UART_LSR_OE |
676 UART_LSR_PE |
677 UART_LSR_FE;
678 }
679 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800680 if (info->board->chip_flag) {
681 spin_lock_irqsave(&info->slock, flags);
682 SET_MOXA_MUST_XON1_VALUE(info->ioaddr, START_CHAR(info->tty));
683 SET_MOXA_MUST_XOFF1_VALUE(info->ioaddr, STOP_CHAR(info->tty));
684 if (I_IXON(info->tty)) {
685 ENABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
686 } else {
687 DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
688 }
689 if (I_IXOFF(info->tty)) {
690 ENABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
691 } else {
692 DISABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
693 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800694 spin_unlock_irqrestore(&info->slock, flags);
695 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800696
697
698 outb(fcr, info->ioaddr + UART_FCR); /* set fcr */
699 outb(cval, info->ioaddr + UART_LCR);
700
701 return ret;
702}
703
704static void mxser_check_modem_status(struct mxser_port *port, int status)
705{
706 /* update input line counters */
707 if (status & UART_MSR_TERI)
708 port->icount.rng++;
709 if (status & UART_MSR_DDSR)
710 port->icount.dsr++;
711 if (status & UART_MSR_DDCD)
712 port->icount.dcd++;
713 if (status & UART_MSR_DCTS)
714 port->icount.cts++;
715 port->mon_data.modem_status = status;
716 wake_up_interruptible(&port->delta_msr_wait);
717
718 if ((port->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
719 if (status & UART_MSR_DCD)
720 wake_up_interruptible(&port->open_wait);
721 schedule_work(&port->tqueue);
722 }
723
724 if (port->flags & ASYNC_CTS_FLOW) {
725 if (port->tty->hw_stopped) {
726 if (status & UART_MSR_CTS) {
727 port->tty->hw_stopped = 0;
728
729 if ((port->type != PORT_16550A) &&
730 (!port->board->chip_flag)) {
731 outb(port->IER & ~UART_IER_THRI,
732 port->ioaddr + UART_IER);
733 port->IER |= UART_IER_THRI;
734 outb(port->IER, port->ioaddr +
735 UART_IER);
736 }
737 set_bit(MXSER_EVENT_TXLOW, &port->event);
738 schedule_work(&port->tqueue);
739 }
740 } else {
741 if (!(status & UART_MSR_CTS)) {
742 port->tty->hw_stopped = 1;
743 if (port->type != PORT_16550A &&
744 !port->board->chip_flag) {
745 port->IER &= ~UART_IER_THRI;
746 outb(port->IER, port->ioaddr +
747 UART_IER);
748 }
749 }
750 }
751 }
752}
753
754static int mxser_startup(struct mxser_port *info)
755{
756 unsigned long page;
757 unsigned long flags;
758
759 page = __get_free_page(GFP_KERNEL);
760 if (!page)
761 return -ENOMEM;
762
763 spin_lock_irqsave(&info->slock, flags);
764
765 if (info->flags & ASYNC_INITIALIZED) {
766 free_page(page);
767 spin_unlock_irqrestore(&info->slock, flags);
768 return 0;
769 }
770
771 if (!info->ioaddr || !info->type) {
772 if (info->tty)
773 set_bit(TTY_IO_ERROR, &info->tty->flags);
774 free_page(page);
775 spin_unlock_irqrestore(&info->slock, flags);
776 return 0;
777 }
778 if (info->xmit_buf)
779 free_page(page);
780 else
781 info->xmit_buf = (unsigned char *) page;
782
783 /*
784 * Clear the FIFO buffers and disable them
785 * (they will be reenabled in mxser_change_speed())
786 */
787 if (info->board->chip_flag)
788 outb((UART_FCR_CLEAR_RCVR |
789 UART_FCR_CLEAR_XMIT |
790 MOXA_MUST_FCR_GDA_MODE_ENABLE), info->ioaddr + UART_FCR);
791 else
792 outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
793 info->ioaddr + UART_FCR);
794
795 /*
796 * At this point there's no way the LSR could still be 0xFF;
797 * if it is, then bail out, because there's likely no UART
798 * here.
799 */
800 if (inb(info->ioaddr + UART_LSR) == 0xff) {
801 spin_unlock_irqrestore(&info->slock, flags);
802 if (capable(CAP_SYS_ADMIN)) {
803 if (info->tty)
804 set_bit(TTY_IO_ERROR, &info->tty->flags);
805 return 0;
806 } else
807 return -ENODEV;
808 }
809
810 /*
811 * Clear the interrupt registers.
812 */
813 (void) inb(info->ioaddr + UART_LSR);
814 (void) inb(info->ioaddr + UART_RX);
815 (void) inb(info->ioaddr + UART_IIR);
816 (void) inb(info->ioaddr + UART_MSR);
817
818 /*
819 * Now, initialize the UART
820 */
821 outb(UART_LCR_WLEN8, info->ioaddr + UART_LCR); /* reset DLAB */
822 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
823 outb(info->MCR, info->ioaddr + UART_MCR);
824
825 /*
826 * Finally, enable interrupts
827 */
828 info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800829
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800830 if (info->board->chip_flag)
831 info->IER |= MOXA_MUST_IER_EGDAI;
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800832 outb(info->IER, info->ioaddr + UART_IER); /* enable interrupts */
833
834 /*
835 * And clear the interrupt registers again for luck.
836 */
837 (void) inb(info->ioaddr + UART_LSR);
838 (void) inb(info->ioaddr + UART_RX);
839 (void) inb(info->ioaddr + UART_IIR);
840 (void) inb(info->ioaddr + UART_MSR);
841
842 if (info->tty)
843 clear_bit(TTY_IO_ERROR, &info->tty->flags);
844 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
845
846 /*
847 * and set the speed of the serial port
848 */
849 spin_unlock_irqrestore(&info->slock, flags);
850 mxser_change_speed(info, NULL);
851
852 info->flags |= ASYNC_INITIALIZED;
853 return 0;
854}
855
856/*
857 * This routine will shutdown a serial port; interrupts maybe disabled, and
858 * DTR is dropped if the hangup on close termio flag is on.
859 */
860static void mxser_shutdown(struct mxser_port *info)
861{
862 unsigned long flags;
863
864 if (!(info->flags & ASYNC_INITIALIZED))
865 return;
866
867 spin_lock_irqsave(&info->slock, flags);
868
869 /*
870 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
871 * here so the queue might never be waken up
872 */
873 wake_up_interruptible(&info->delta_msr_wait);
874
875 /*
876 * Free the IRQ, if necessary
877 */
878 if (info->xmit_buf) {
879 free_page((unsigned long) info->xmit_buf);
880 info->xmit_buf = NULL;
881 }
882
883 info->IER = 0;
884 outb(0x00, info->ioaddr + UART_IER);
885
886 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
887 info->MCR &= ~(UART_MCR_DTR | UART_MCR_RTS);
888 outb(info->MCR, info->ioaddr + UART_MCR);
889
890 /* clear Rx/Tx FIFO's */
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800891 if (info->board->chip_flag)
892 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT |
893 MOXA_MUST_FCR_GDA_MODE_ENABLE,
894 info->ioaddr + UART_FCR);
895 else
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800896 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
897 info->ioaddr + UART_FCR);
898
899 /* read data port to reset things */
900 (void) inb(info->ioaddr + UART_RX);
901
902 if (info->tty)
903 set_bit(TTY_IO_ERROR, &info->tty->flags);
904
905 info->flags &= ~ASYNC_INITIALIZED;
906
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800907 if (info->board->chip_flag)
908 SET_MOXA_MUST_NO_SOFTWARE_FLOW_CONTROL(info->ioaddr);
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800909
910 spin_unlock_irqrestore(&info->slock, flags);
911}
912
Jiri Slaby037ad482006-12-08 02:38:11 -0800913/*
914 * This routine is called whenever a serial port is opened. It
915 * enables interrupts for a serial port, linking in its async structure into
916 * the IRQ chain. It also performs the serial-specific
917 * initialization for the tty structure.
918 */
919static int mxser_open(struct tty_struct *tty, struct file *filp)
920{
Jiri Slaby55b307d2006-12-08 02:38:14 -0800921 struct mxser_port *info;
Jiri Slaby037ad482006-12-08 02:38:11 -0800922 int retval, line;
923
924 /* initialize driver_data in case something fails */
925 tty->driver_data = NULL;
926
927 line = tty->index;
928 if (line == MXSER_PORTS)
929 return 0;
930 if (line < 0 || line > MXSER_PORTS)
931 return -ENODEV;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800932 info = &mxser_boards[line / MXSER_PORTS_PER_BOARD].ports[line % MXSER_PORTS_PER_BOARD];
933 if (!info->ioaddr)
Jiri Slaby037ad482006-12-08 02:38:11 -0800934 return -ENODEV;
935
936 tty->driver_data = info;
937 info->tty = tty;
938 /*
939 * Start up serial port
940 */
Jiri Slaby3306ce32006-12-08 02:38:13 -0800941 info->count++;
Jiri Slaby037ad482006-12-08 02:38:11 -0800942 retval = mxser_startup(info);
943 if (retval)
944 return retval;
945
946 retval = mxser_block_til_ready(tty, filp, info);
947 if (retval)
948 return retval;
949
Jiri Slaby037ad482006-12-08 02:38:11 -0800950 if ((info->count == 1) && (info->flags & ASYNC_SPLIT_TERMIOS)) {
951 if (tty->driver->subtype == SERIAL_TYPE_NORMAL)
952 *tty->termios = info->normal_termios;
953 else
954 *tty->termios = info->callout_termios;
955 mxser_change_speed(info, NULL);
956 }
957
Andrew Morton08a4ae42006-12-08 02:38:12 -0800958 info->session = process_session(current);
Jiri Slaby037ad482006-12-08 02:38:11 -0800959 info->pgrp = process_group(current);
960
Jiri Slabye079f492006-12-08 02:38:31 -0800961 /* unmark here for very high baud rate (ex. 921600 bps) used */
Jiri Slaby037ad482006-12-08 02:38:11 -0800962 tty->low_latency = 1;
963 return 0;
964}
965
966/*
967 * This routine is called when the serial port gets closed. First, we
968 * wait for the last remaining data to be sent. Then, we unlink its
969 * async structure from the interrupt chain if necessary, and we free
970 * that IRQ if nothing is left in the chain.
971 */
972static void mxser_close(struct tty_struct *tty, struct file *filp)
973{
Jiri Slaby55b307d2006-12-08 02:38:14 -0800974 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -0800975
976 unsigned long timeout;
977 unsigned long flags;
Jiri Slaby037ad482006-12-08 02:38:11 -0800978
979 if (tty->index == MXSER_PORTS)
980 return;
981 if (!info)
982 return;
983
984 spin_lock_irqsave(&info->slock, flags);
985
986 if (tty_hung_up_p(filp)) {
987 spin_unlock_irqrestore(&info->slock, flags);
988 return;
989 }
990 if ((tty->count == 1) && (info->count != 1)) {
991 /*
992 * Uh, oh. tty->count is 1, which means that the tty
993 * structure will be freed. Info->count should always
994 * be one in these conditions. If it's greater than
995 * one, we've got real problems, since it means the
996 * serial port won't be shutdown.
997 */
998 printk(KERN_ERR "mxser_close: bad serial port count; "
999 "tty->count is 1, info->count is %d\n", info->count);
1000 info->count = 1;
1001 }
1002 if (--info->count < 0) {
1003 printk(KERN_ERR "mxser_close: bad serial port count for "
Jiri Slaby55b307d2006-12-08 02:38:14 -08001004 "ttys%d: %d\n", tty->index, info->count);
Jiri Slaby037ad482006-12-08 02:38:11 -08001005 info->count = 0;
1006 }
1007 if (info->count) {
1008 spin_unlock_irqrestore(&info->slock, flags);
1009 return;
1010 }
1011 info->flags |= ASYNC_CLOSING;
1012 spin_unlock_irqrestore(&info->slock, flags);
1013 /*
1014 * Save the termios structure, since this port may have
1015 * separate termios for callout and dialin.
1016 */
1017 if (info->flags & ASYNC_NORMAL_ACTIVE)
1018 info->normal_termios = *tty->termios;
1019 /*
1020 * Now we wait for the transmit buffer to clear; and we notify
1021 * the line discipline to only process XON/XOFF characters.
1022 */
1023 tty->closing = 1;
1024 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1025 tty_wait_until_sent(tty, info->closing_wait);
1026 /*
1027 * At this point we stop accepting input. To do this, we
1028 * disable the receive line status interrupts, and tell the
1029 * interrupt driver to stop checking the data ready bit in the
1030 * line status register.
1031 */
1032 info->IER &= ~UART_IER_RLSI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001033 if (info->board->chip_flag)
Jiri Slaby037ad482006-12-08 02:38:11 -08001034 info->IER &= ~MOXA_MUST_RECV_ISR;
Jiri Slabye079f492006-12-08 02:38:31 -08001035
Jiri Slaby037ad482006-12-08 02:38:11 -08001036 if (info->flags & ASYNC_INITIALIZED) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08001037 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001038 /*
1039 * Before we drop DTR, make sure the UART transmitter
1040 * has completely drained; this is especially
1041 * important if there is a transmit FIFO!
1042 */
1043 timeout = jiffies + HZ;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001044 while (!(inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001045 schedule_timeout_interruptible(5);
1046 if (time_after(jiffies, timeout))
1047 break;
1048 }
1049 }
1050 mxser_shutdown(info);
1051
1052 if (tty->driver->flush_buffer)
1053 tty->driver->flush_buffer(tty);
1054
Jiri Slaby7e8bcf92006-12-08 02:38:24 -08001055 tty_ldisc_flush(tty);
Jiri Slaby037ad482006-12-08 02:38:11 -08001056
1057 tty->closing = 0;
1058 info->event = 0;
1059 info->tty = NULL;
1060 if (info->blocked_open) {
1061 if (info->close_delay)
1062 schedule_timeout_interruptible(info->close_delay);
1063 wake_up_interruptible(&info->open_wait);
1064 }
1065
1066 info->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
1067 wake_up_interruptible(&info->close_wait);
1068
1069}
1070
1071static int mxser_write(struct tty_struct *tty, const unsigned char *buf, int count)
1072{
1073 int c, total = 0;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001074 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001075 unsigned long flags;
1076
1077 if (!info->xmit_buf)
1078 return 0;
1079
1080 while (1) {
1081 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1082 SERIAL_XMIT_SIZE - info->xmit_head));
1083 if (c <= 0)
1084 break;
1085
1086 memcpy(info->xmit_buf + info->xmit_head, buf, c);
1087 spin_lock_irqsave(&info->slock, flags);
1088 info->xmit_head = (info->xmit_head + c) &
1089 (SERIAL_XMIT_SIZE - 1);
1090 info->xmit_cnt += c;
1091 spin_unlock_irqrestore(&info->slock, flags);
1092
1093 buf += c;
1094 count -= c;
1095 total += c;
1096 }
1097
Jiri Slabye079f492006-12-08 02:38:31 -08001098 if (info->xmit_cnt && !tty->stopped) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001099 if (!tty->hw_stopped ||
1100 (info->type == PORT_16550A) ||
Jiri Slaby55b307d2006-12-08 02:38:14 -08001101 (info->board->chip_flag)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001102 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby55b307d2006-12-08 02:38:14 -08001103 outb(info->IER & ~UART_IER_THRI, info->ioaddr +
1104 UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001105 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001106 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001107 spin_unlock_irqrestore(&info->slock, flags);
1108 }
1109 }
1110 return total;
1111}
1112
1113static void mxser_put_char(struct tty_struct *tty, unsigned char ch)
1114{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001115 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001116 unsigned long flags;
1117
1118 if (!info->xmit_buf)
1119 return;
1120
1121 if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1)
1122 return;
1123
1124 spin_lock_irqsave(&info->slock, flags);
1125 info->xmit_buf[info->xmit_head++] = ch;
1126 info->xmit_head &= SERIAL_XMIT_SIZE - 1;
1127 info->xmit_cnt++;
1128 spin_unlock_irqrestore(&info->slock, flags);
Jiri Slabye079f492006-12-08 02:38:31 -08001129 if (!tty->stopped) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001130 if (!tty->hw_stopped ||
1131 (info->type == PORT_16550A) ||
Jiri Slaby55b307d2006-12-08 02:38:14 -08001132 info->board->chip_flag) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001133 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby55b307d2006-12-08 02:38:14 -08001134 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001135 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001136 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001137 spin_unlock_irqrestore(&info->slock, flags);
1138 }
1139 }
1140}
1141
1142
1143static void mxser_flush_chars(struct tty_struct *tty)
1144{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001145 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001146 unsigned long flags;
1147
1148 if (info->xmit_cnt <= 0 ||
1149 tty->stopped ||
1150 !info->xmit_buf ||
1151 (tty->hw_stopped &&
1152 (info->type != PORT_16550A) &&
Jiri Slaby55b307d2006-12-08 02:38:14 -08001153 (!info->board->chip_flag)
Jiri Slaby037ad482006-12-08 02:38:11 -08001154 ))
1155 return;
1156
1157 spin_lock_irqsave(&info->slock, flags);
1158
Jiri Slaby55b307d2006-12-08 02:38:14 -08001159 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001160 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001161 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001162
1163 spin_unlock_irqrestore(&info->slock, flags);
1164}
1165
1166static int mxser_write_room(struct tty_struct *tty)
1167{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001168 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001169 int ret;
1170
1171 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1172 if (ret < 0)
1173 ret = 0;
1174 return ret;
1175}
1176
1177static int mxser_chars_in_buffer(struct tty_struct *tty)
1178{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001179 struct mxser_port *info = tty->driver_data;
Jiri Slaby925e9c12006-12-08 02:38:30 -08001180 return info->xmit_cnt;
Jiri Slaby037ad482006-12-08 02:38:11 -08001181}
1182
1183static void mxser_flush_buffer(struct tty_struct *tty)
1184{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001185 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001186 char fcr;
1187 unsigned long flags;
1188
1189
1190 spin_lock_irqsave(&info->slock, flags);
1191 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1192
Jiri Slaby55b307d2006-12-08 02:38:14 -08001193 fcr = inb(info->ioaddr + UART_FCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08001194 outb((fcr | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
Jiri Slaby55b307d2006-12-08 02:38:14 -08001195 info->ioaddr + UART_FCR);
1196 outb(fcr, info->ioaddr + UART_FCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08001197
1198 spin_unlock_irqrestore(&info->slock, flags);
Jiri Slaby037ad482006-12-08 02:38:11 -08001199
Jiri Slaby7e8bcf92006-12-08 02:38:24 -08001200 tty_wakeup(tty);
Jiri Slaby037ad482006-12-08 02:38:11 -08001201}
1202
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001203/*
1204 * ------------------------------------------------------------
1205 * friends of mxser_ioctl()
1206 * ------------------------------------------------------------
1207 */
1208static int mxser_get_serial_info(struct mxser_port *info,
1209 struct serial_struct __user *retinfo)
1210{
1211 struct serial_struct tmp;
1212
1213 if (!retinfo)
1214 return -EFAULT;
1215 memset(&tmp, 0, sizeof(tmp));
1216 tmp.type = info->type;
1217 tmp.line = info->tty->index;
1218 tmp.port = info->ioaddr;
1219 tmp.irq = info->board->irq;
1220 tmp.flags = info->flags;
1221 tmp.baud_base = info->baud_base;
1222 tmp.close_delay = info->close_delay;
1223 tmp.closing_wait = info->closing_wait;
1224 tmp.custom_divisor = info->custom_divisor;
1225 tmp.hub6 = 0;
1226 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1227 return -EFAULT;
1228 return 0;
1229}
1230
1231static int mxser_set_serial_info(struct mxser_port *info,
1232 struct serial_struct __user *new_info)
1233{
1234 struct serial_struct new_serial;
1235 unsigned int flags;
1236 int retval = 0;
1237
1238 if (!new_info || !info->ioaddr)
1239 return -EFAULT;
1240 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
1241 return -EFAULT;
1242
1243 if ((new_serial.irq != info->board->irq) ||
1244 (new_serial.port != info->ioaddr) ||
1245 (new_serial.custom_divisor != info->custom_divisor) ||
1246 (new_serial.baud_base != info->baud_base))
1247 return -EPERM;
1248
1249 flags = info->flags & ASYNC_SPD_MASK;
1250
1251 if (!capable(CAP_SYS_ADMIN)) {
1252 if ((new_serial.baud_base != info->baud_base) ||
1253 (new_serial.close_delay != info->close_delay) ||
1254 ((new_serial.flags & ~ASYNC_USR_MASK) != (info->flags & ~ASYNC_USR_MASK)))
1255 return -EPERM;
1256 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
1257 (new_serial.flags & ASYNC_USR_MASK));
1258 } else {
1259 /*
1260 * OK, past this point, all the error checking has been done.
1261 * At this point, we start making changes.....
1262 */
1263 info->flags = ((info->flags & ~ASYNC_FLAGS) |
1264 (new_serial.flags & ASYNC_FLAGS));
1265 info->close_delay = new_serial.close_delay * HZ / 100;
1266 info->closing_wait = new_serial.closing_wait * HZ / 100;
1267 info->tty->low_latency =
1268 (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
Jiri Slabye079f492006-12-08 02:38:31 -08001269 info->tty->low_latency = 0;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001270 }
1271
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001272 info->type = new_serial.type;
1273
1274 process_txrx_fifo(info);
1275
1276 if (info->flags & ASYNC_INITIALIZED) {
1277 if (flags != (info->flags & ASYNC_SPD_MASK))
1278 mxser_change_speed(info, NULL);
1279 } else
1280 retval = mxser_startup(info);
1281
1282 return retval;
1283}
1284
1285/*
1286 * mxser_get_lsr_info - get line status register info
1287 *
1288 * Purpose: Let user call ioctl() to get info when the UART physically
1289 * is emptied. On bus types like RS485, the transmitter must
1290 * release the bus after transmitting. This must be done when
1291 * the transmit shift register is empty, not be done when the
1292 * transmit holding register is empty. This functionality
1293 * allows an RS485 driver to be written in user space.
1294 */
1295static int mxser_get_lsr_info(struct mxser_port *info,
1296 unsigned int __user *value)
1297{
1298 unsigned char status;
1299 unsigned int result;
1300 unsigned long flags;
1301
1302 spin_lock_irqsave(&info->slock, flags);
1303 status = inb(info->ioaddr + UART_LSR);
1304 spin_unlock_irqrestore(&info->slock, flags);
1305 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1306 return put_user(result, value);
1307}
1308
1309/*
1310 * This routine sends a break character out the serial port.
1311 */
1312static void mxser_send_break(struct mxser_port *info, int duration)
1313{
1314 unsigned long flags;
1315
1316 if (!info->ioaddr)
1317 return;
1318 set_current_state(TASK_INTERRUPTIBLE);
1319 spin_lock_irqsave(&info->slock, flags);
1320 outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
1321 info->ioaddr + UART_LCR);
1322 spin_unlock_irqrestore(&info->slock, flags);
1323 schedule_timeout(duration);
1324 spin_lock_irqsave(&info->slock, flags);
1325 outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
1326 info->ioaddr + UART_LCR);
1327 spin_unlock_irqrestore(&info->slock, flags);
1328}
1329
1330static int mxser_tiocmget(struct tty_struct *tty, struct file *file)
Jiri Slaby037ad482006-12-08 02:38:11 -08001331{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001332 struct mxser_port *info = tty->driver_data;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001333 unsigned char control, status;
Jiri Slaby037ad482006-12-08 02:38:11 -08001334 unsigned long flags;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001335
Jiri Slaby037ad482006-12-08 02:38:11 -08001336
1337 if (tty->index == MXSER_PORTS)
Jiri Slaby037ad482006-12-08 02:38:11 -08001338 return -ENOIOCTLCMD;
Jiri Slaby214efeb2006-12-08 02:38:25 -08001339 if (test_bit(TTY_IO_ERROR, &tty->flags))
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001340 return -EIO;
1341
1342 control = info->MCR;
1343
1344 spin_lock_irqsave(&info->slock, flags);
1345 status = inb(info->ioaddr + UART_MSR);
1346 if (status & UART_MSR_ANY_DELTA)
1347 mxser_check_modem_status(info, status);
1348 spin_unlock_irqrestore(&info->slock, flags);
1349 return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0) |
1350 ((control & UART_MCR_DTR) ? TIOCM_DTR : 0) |
1351 ((status & UART_MSR_DCD) ? TIOCM_CAR : 0) |
1352 ((status & UART_MSR_RI) ? TIOCM_RNG : 0) |
1353 ((status & UART_MSR_DSR) ? TIOCM_DSR : 0) |
1354 ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
1355}
1356
1357static int mxser_tiocmset(struct tty_struct *tty, struct file *file,
1358 unsigned int set, unsigned int clear)
1359{
1360 struct mxser_port *info = tty->driver_data;
1361 unsigned long flags;
1362
1363
1364 if (tty->index == MXSER_PORTS)
1365 return -ENOIOCTLCMD;
Jiri Slaby214efeb2006-12-08 02:38:25 -08001366 if (test_bit(TTY_IO_ERROR, &tty->flags))
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001367 return -EIO;
1368
1369 spin_lock_irqsave(&info->slock, flags);
1370
1371 if (set & TIOCM_RTS)
1372 info->MCR |= UART_MCR_RTS;
1373 if (set & TIOCM_DTR)
1374 info->MCR |= UART_MCR_DTR;
1375
1376 if (clear & TIOCM_RTS)
1377 info->MCR &= ~UART_MCR_RTS;
1378 if (clear & TIOCM_DTR)
1379 info->MCR &= ~UART_MCR_DTR;
1380
1381 outb(info->MCR, info->ioaddr + UART_MCR);
1382 spin_unlock_irqrestore(&info->slock, flags);
Jiri Slaby037ad482006-12-08 02:38:11 -08001383 return 0;
1384}
1385
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001386static int mxser_program_mode(int port)
1387{
1388 int id, i, j, n;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001389
1390 spin_lock(&gm_lock);
1391 outb(0, port);
1392 outb(0, port);
1393 outb(0, port);
1394 (void)inb(port);
1395 (void)inb(port);
1396 outb(0, port);
1397 (void)inb(port);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001398 spin_unlock(&gm_lock);
1399
1400 id = inb(port + 1) & 0x1F;
1401 if ((id != C168_ASIC_ID) &&
1402 (id != C104_ASIC_ID) &&
1403 (id != C102_ASIC_ID) &&
1404 (id != CI132_ASIC_ID) &&
1405 (id != CI134_ASIC_ID) &&
1406 (id != CI104J_ASIC_ID))
1407 return -1;
1408 for (i = 0, j = 0; i < 4; i++) {
1409 n = inb(port + 2);
1410 if (n == 'M') {
1411 j = 1;
1412 } else if ((j == 1) && (n == 1)) {
1413 j = 2;
1414 break;
1415 } else
1416 j = 0;
1417 }
1418 if (j != 2)
1419 id = -2;
1420 return id;
1421}
1422
1423static void mxser_normal_mode(int port)
1424{
1425 int i, n;
1426
1427 outb(0xA5, port + 1);
1428 outb(0x80, port + 3);
1429 outb(12, port + 0); /* 9600 bps */
1430 outb(0, port + 1);
1431 outb(0x03, port + 3); /* 8 data bits */
1432 outb(0x13, port + 4); /* loop back mode */
1433 for (i = 0; i < 16; i++) {
1434 n = inb(port + 5);
1435 if ((n & 0x61) == 0x60)
1436 break;
1437 if ((n & 1) == 1)
1438 (void)inb(port);
1439 }
1440 outb(0x00, port + 4);
1441}
1442
1443#define CHIP_SK 0x01 /* Serial Data Clock in Eprom */
1444#define CHIP_DO 0x02 /* Serial Data Output in Eprom */
1445#define CHIP_CS 0x04 /* Serial Chip Select in Eprom */
1446#define CHIP_DI 0x08 /* Serial Data Input in Eprom */
1447#define EN_CCMD 0x000 /* Chip's command register */
1448#define EN0_RSARLO 0x008 /* Remote start address reg 0 */
1449#define EN0_RSARHI 0x009 /* Remote start address reg 1 */
1450#define EN0_RCNTLO 0x00A /* Remote byte count reg WR */
1451#define EN0_RCNTHI 0x00B /* Remote byte count reg WR */
1452#define EN0_DCFG 0x00E /* Data configuration reg WR */
1453#define EN0_PORT 0x010 /* Rcv missed frame error counter RD */
1454#define ENC_PAGE0 0x000 /* Select page 0 of chip registers */
1455#define ENC_PAGE3 0x0C0 /* Select page 3 of chip registers */
1456static int mxser_read_register(int port, unsigned short *regs)
1457{
1458 int i, k, value, id;
1459 unsigned int j;
1460
1461 id = mxser_program_mode(port);
1462 if (id < 0)
1463 return id;
1464 for (i = 0; i < 14; i++) {
1465 k = (i & 0x3F) | 0x180;
1466 for (j = 0x100; j > 0; j >>= 1) {
1467 outb(CHIP_CS, port);
1468 if (k & j) {
1469 outb(CHIP_CS | CHIP_DO, port);
1470 outb(CHIP_CS | CHIP_DO | CHIP_SK, port); /* A? bit of read */
1471 } else {
1472 outb(CHIP_CS, port);
1473 outb(CHIP_CS | CHIP_SK, port); /* A? bit of read */
1474 }
1475 }
1476 (void)inb(port);
1477 value = 0;
1478 for (k = 0, j = 0x8000; k < 16; k++, j >>= 1) {
1479 outb(CHIP_CS, port);
1480 outb(CHIP_CS | CHIP_SK, port);
1481 if (inb(port) & CHIP_DI)
1482 value |= j;
1483 }
1484 regs[i] = value;
1485 outb(0, port);
1486 }
1487 mxser_normal_mode(port);
1488 return id;
1489}
1490
Jiri Slaby037ad482006-12-08 02:38:11 -08001491static int mxser_ioctl_special(unsigned int cmd, void __user *argp)
1492{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001493 struct mxser_port *port;
1494 int result, status;
1495 unsigned int i, j;
Jiri Slaby037ad482006-12-08 02:38:11 -08001496
1497 switch (cmd) {
1498 case MOXA_GET_CONF:
Jiri Slaby55b307d2006-12-08 02:38:14 -08001499/* if (copy_to_user(argp, mxsercfg,
Jiri Slaby037ad482006-12-08 02:38:11 -08001500 sizeof(struct mxser_hwconf) * 4))
1501 return -EFAULT;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001502 return 0;*/
1503 return -ENXIO;
Jiri Slaby037ad482006-12-08 02:38:11 -08001504 case MOXA_GET_MAJOR:
1505 if (copy_to_user(argp, &ttymajor, sizeof(int)))
1506 return -EFAULT;
1507 return 0;
1508
1509 case MOXA_GET_CUMAJOR:
1510 if (copy_to_user(argp, &calloutmajor, sizeof(int)))
1511 return -EFAULT;
1512 return 0;
1513
1514 case MOXA_CHKPORTENABLE:
1515 result = 0;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001516
1517 for (i = 0; i < MXSER_BOARDS; i++)
1518 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++)
1519 if (mxser_boards[i].ports[j].ioaddr)
1520 result |= (1 << i);
1521
Jiri Slaby037ad482006-12-08 02:38:11 -08001522 return put_user(result, (unsigned long __user *)argp);
1523 case MOXA_GETDATACOUNT:
1524 if (copy_to_user(argp, &mxvar_log, sizeof(mxvar_log)))
1525 return -EFAULT;
1526 return 0;
1527 case MOXA_GETMSTATUS:
Jiri Slaby55b307d2006-12-08 02:38:14 -08001528 for (i = 0; i < MXSER_BOARDS; i++)
1529 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
1530 port = &mxser_boards[i].ports[j];
1531
1532 GMStatus[i].ri = 0;
1533 if (!port->ioaddr) {
1534 GMStatus[i].dcd = 0;
1535 GMStatus[i].dsr = 0;
1536 GMStatus[i].cts = 0;
1537 continue;
1538 }
1539
1540 if (!port->tty || !port->tty->termios)
1541 GMStatus[i].cflag =
1542 port->normal_termios.c_cflag;
1543 else
1544 GMStatus[i].cflag =
1545 port->tty->termios->c_cflag;
1546
1547 status = inb(port->ioaddr + UART_MSR);
1548 if (status & 0x80 /*UART_MSR_DCD */ )
1549 GMStatus[i].dcd = 1;
1550 else
1551 GMStatus[i].dcd = 0;
1552
1553 if (status & 0x20 /*UART_MSR_DSR */ )
1554 GMStatus[i].dsr = 1;
1555 else
1556 GMStatus[i].dsr = 0;
1557
1558
1559 if (status & 0x10 /*UART_MSR_CTS */ )
1560 GMStatus[i].cts = 1;
1561 else
1562 GMStatus[i].cts = 0;
Jiri Slaby037ad482006-12-08 02:38:11 -08001563 }
Jiri Slaby037ad482006-12-08 02:38:11 -08001564 if (copy_to_user(argp, GMStatus,
1565 sizeof(struct mxser_mstatus) * MXSER_PORTS))
1566 return -EFAULT;
1567 return 0;
1568 case MOXA_ASPP_MON_EXT: {
Jiri Slaby55b307d2006-12-08 02:38:14 -08001569 int status, p, shiftbit;
1570 unsigned long opmode;
1571 unsigned cflag, iflag;
Jiri Slaby037ad482006-12-08 02:38:11 -08001572
Jiri Slaby55b307d2006-12-08 02:38:14 -08001573 for (i = 0; i < MXSER_BOARDS; i++)
1574 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
1575 port = &mxser_boards[i].ports[j];
1576 if (!port->ioaddr)
Jiri Slaby037ad482006-12-08 02:38:11 -08001577 continue;
1578
Jiri Slaby55b307d2006-12-08 02:38:14 -08001579 status = mxser_get_msr(port->ioaddr, 0, i);
Jiri Slaby55b307d2006-12-08 02:38:14 -08001580
Jiri Slaby037ad482006-12-08 02:38:11 -08001581 if (status & UART_MSR_TERI)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001582 port->icount.rng++;
Jiri Slaby037ad482006-12-08 02:38:11 -08001583 if (status & UART_MSR_DDSR)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001584 port->icount.dsr++;
Jiri Slaby037ad482006-12-08 02:38:11 -08001585 if (status & UART_MSR_DDCD)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001586 port->icount.dcd++;
Jiri Slaby037ad482006-12-08 02:38:11 -08001587 if (status & UART_MSR_DCTS)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001588 port->icount.cts++;
Jiri Slaby037ad482006-12-08 02:38:11 -08001589
Jiri Slaby55b307d2006-12-08 02:38:14 -08001590 port->mon_data.modem_status = status;
1591 mon_data_ext.rx_cnt[i] = port->mon_data.rxcnt;
1592 mon_data_ext.tx_cnt[i] = port->mon_data.txcnt;
1593 mon_data_ext.up_rxcnt[i] =
1594 port->mon_data.up_rxcnt;
1595 mon_data_ext.up_txcnt[i] =
1596 port->mon_data.up_txcnt;
1597 mon_data_ext.modem_status[i] =
1598 port->mon_data.modem_status;
1599 mon_data_ext.baudrate[i] = port->realbaud;
Jiri Slaby037ad482006-12-08 02:38:11 -08001600
Jiri Slaby55b307d2006-12-08 02:38:14 -08001601 if (!port->tty || !port->tty->termios) {
1602 cflag = port->normal_termios.c_cflag;
1603 iflag = port->normal_termios.c_iflag;
Jiri Slaby037ad482006-12-08 02:38:11 -08001604 } else {
Jiri Slaby55b307d2006-12-08 02:38:14 -08001605 cflag = port->tty->termios->c_cflag;
1606 iflag = port->tty->termios->c_iflag;
Jiri Slaby037ad482006-12-08 02:38:11 -08001607 }
1608
1609 mon_data_ext.databits[i] = cflag & CSIZE;
1610
1611 mon_data_ext.stopbits[i] = cflag & CSTOPB;
1612
Jiri Slaby55b307d2006-12-08 02:38:14 -08001613 mon_data_ext.parity[i] =
1614 cflag & (PARENB | PARODD | CMSPAR);
Jiri Slaby037ad482006-12-08 02:38:11 -08001615
1616 mon_data_ext.flowctrl[i] = 0x00;
1617
1618 if (cflag & CRTSCTS)
1619 mon_data_ext.flowctrl[i] |= 0x03;
1620
1621 if (iflag & (IXON | IXOFF))
1622 mon_data_ext.flowctrl[i] |= 0x0C;
1623
Jiri Slaby55b307d2006-12-08 02:38:14 -08001624 if (port->type == PORT_16550A)
Jiri Slaby037ad482006-12-08 02:38:11 -08001625 mon_data_ext.fifo[i] = 1;
1626 else
1627 mon_data_ext.fifo[i] = 0;
1628
1629 p = i % 4;
1630 shiftbit = p * 2;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001631 opmode = inb(port->opmode_ioaddr) >> shiftbit;
Jiri Slaby037ad482006-12-08 02:38:11 -08001632 opmode &= OP_MODE_MASK;
1633
1634 mon_data_ext.iftype[i] = opmode;
1635
1636 }
Jiri Slaby55b307d2006-12-08 02:38:14 -08001637 if (copy_to_user(argp, &mon_data_ext,
1638 sizeof(mon_data_ext)))
Jiri Slaby037ad482006-12-08 02:38:11 -08001639 return -EFAULT;
1640
1641 return 0;
1642
Jiri Slaby55b307d2006-12-08 02:38:14 -08001643 } default:
Jiri Slaby037ad482006-12-08 02:38:11 -08001644 return -ENOIOCTLCMD;
1645 }
1646 return 0;
1647}
1648
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001649static int mxser_ioctl(struct tty_struct *tty, struct file *file,
1650 unsigned int cmd, unsigned long arg)
1651{
1652 struct mxser_port *info = tty->driver_data;
1653 struct async_icount cprev, cnow; /* kernel counter temps */
1654 struct serial_icounter_struct __user *p_cuser;
1655 unsigned long templ;
1656 unsigned long flags;
1657 void __user *argp = (void __user *)arg;
1658 int retval;
1659
1660 if (tty->index == MXSER_PORTS)
1661 return mxser_ioctl_special(cmd, argp);
1662
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001663 if (cmd == MOXA_SET_OP_MODE || cmd == MOXA_GET_OP_MODE) {
1664 int p;
1665 unsigned long opmode;
1666 static unsigned char ModeMask[] = { 0xfc, 0xf3, 0xcf, 0x3f };
1667 int shiftbit;
1668 unsigned char val, mask;
1669
1670 p = tty->index % 4;
1671 if (cmd == MOXA_SET_OP_MODE) {
1672 if (get_user(opmode, (int __user *) argp))
1673 return -EFAULT;
1674 if (opmode != RS232_MODE &&
1675 opmode != RS485_2WIRE_MODE &&
1676 opmode != RS422_MODE &&
1677 opmode != RS485_4WIRE_MODE)
1678 return -EFAULT;
1679 mask = ModeMask[p];
1680 shiftbit = p * 2;
1681 val = inb(info->opmode_ioaddr);
1682 val &= mask;
1683 val |= (opmode << shiftbit);
1684 outb(val, info->opmode_ioaddr);
1685 } else {
1686 shiftbit = p * 2;
1687 opmode = inb(info->opmode_ioaddr) >> shiftbit;
1688 opmode &= OP_MODE_MASK;
1689 if (copy_to_user(argp, &opmode, sizeof(int)))
1690 return -EFAULT;
1691 }
1692 return 0;
1693 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001694
Jiri Slaby214efeb2006-12-08 02:38:25 -08001695 if (cmd != TIOCGSERIAL && cmd != TIOCMIWAIT && cmd != TIOCGICOUNT &&
1696 test_bit(TTY_IO_ERROR, &tty->flags))
1697 return -EIO;
1698
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001699 switch (cmd) {
1700 case TCSBRK: /* SVID version: non-zero arg --> no break */
1701 retval = tty_check_change(tty);
1702 if (retval)
1703 return retval;
1704 tty_wait_until_sent(tty, 0);
1705 if (!arg)
1706 mxser_send_break(info, HZ / 4); /* 1/4 second */
1707 return 0;
1708 case TCSBRKP: /* support for POSIX tcsendbreak() */
1709 retval = tty_check_change(tty);
1710 if (retval)
1711 return retval;
1712 tty_wait_until_sent(tty, 0);
1713 mxser_send_break(info, arg ? arg * (HZ / 10) : HZ / 4);
1714 return 0;
1715 case TIOCGSOFTCAR:
1716 return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *)argp);
1717 case TIOCSSOFTCAR:
1718 if (get_user(templ, (unsigned long __user *) argp))
1719 return -EFAULT;
1720 arg = templ;
1721 tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) | (arg ? CLOCAL : 0));
1722 return 0;
1723 case TIOCGSERIAL:
1724 return mxser_get_serial_info(info, argp);
1725 case TIOCSSERIAL:
1726 return mxser_set_serial_info(info, argp);
1727 case TIOCSERGETLSR: /* Get line status register */
1728 return mxser_get_lsr_info(info, argp);
1729 /*
1730 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1731 * - mask passed in arg for lines of interest
1732 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1733 * Caller should use TIOCGICOUNT to see which one it was
1734 */
1735 case TIOCMIWAIT: {
1736 DECLARE_WAITQUEUE(wait, current);
1737 int ret;
1738 spin_lock_irqsave(&info->slock, flags);
1739 cprev = info->icount; /* note the counters on entry */
1740 spin_unlock_irqrestore(&info->slock, flags);
1741
1742 add_wait_queue(&info->delta_msr_wait, &wait);
1743 while (1) {
1744 spin_lock_irqsave(&info->slock, flags);
1745 cnow = info->icount; /* atomic copy */
1746 spin_unlock_irqrestore(&info->slock, flags);
1747
1748 set_current_state(TASK_INTERRUPTIBLE);
1749 if (((arg & TIOCM_RNG) &&
1750 (cnow.rng != cprev.rng)) ||
1751 ((arg & TIOCM_DSR) &&
1752 (cnow.dsr != cprev.dsr)) ||
1753 ((arg & TIOCM_CD) &&
1754 (cnow.dcd != cprev.dcd)) ||
1755 ((arg & TIOCM_CTS) &&
1756 (cnow.cts != cprev.cts))) {
1757 ret = 0;
1758 break;
1759 }
1760 /* see if a signal did it */
1761 if (signal_pending(current)) {
1762 ret = -ERESTARTSYS;
1763 break;
1764 }
1765 cprev = cnow;
1766 }
1767 current->state = TASK_RUNNING;
1768 remove_wait_queue(&info->delta_msr_wait, &wait);
1769 break;
1770 }
1771 /* NOTREACHED */
1772 /*
1773 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1774 * Return: write counters to the user passed counter struct
1775 * NB: both 1->0 and 0->1 transitions are counted except for
1776 * RI where only 0->1 is counted.
1777 */
1778 case TIOCGICOUNT:
1779 spin_lock_irqsave(&info->slock, flags);
1780 cnow = info->icount;
1781 spin_unlock_irqrestore(&info->slock, flags);
1782 p_cuser = argp;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001783 if (put_user(cnow.frame, &p_cuser->frame))
1784 return -EFAULT;
1785 if (put_user(cnow.brk, &p_cuser->brk))
1786 return -EFAULT;
1787 if (put_user(cnow.overrun, &p_cuser->overrun))
1788 return -EFAULT;
1789 if (put_user(cnow.buf_overrun, &p_cuser->buf_overrun))
1790 return -EFAULT;
1791 if (put_user(cnow.parity, &p_cuser->parity))
1792 return -EFAULT;
1793 if (put_user(cnow.rx, &p_cuser->rx))
1794 return -EFAULT;
1795 if (put_user(cnow.tx, &p_cuser->tx))
1796 return -EFAULT;
1797 put_user(cnow.cts, &p_cuser->cts);
1798 put_user(cnow.dsr, &p_cuser->dsr);
1799 put_user(cnow.rng, &p_cuser->rng);
1800 put_user(cnow.dcd, &p_cuser->dcd);
1801 return 0;
1802 case MOXA_HighSpeedOn:
1803 return put_user(info->baud_base != 115200 ? 1 : 0, (int __user *)argp);
1804 case MOXA_SDS_RSTICOUNTER:
1805 info->mon_data.rxcnt = 0;
1806 info->mon_data.txcnt = 0;
1807 return 0;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001808 case MOXA_ASPP_SETBAUD:{
1809 long baud;
1810 if (get_user(baud, (long __user *)argp))
1811 return -EFAULT;
1812 mxser_set_baud(info, baud);
1813 return 0;
1814 }
1815 case MOXA_ASPP_GETBAUD:
1816 if (copy_to_user(argp, &info->realbaud, sizeof(long)))
1817 return -EFAULT;
1818
1819 return 0;
1820
1821 case MOXA_ASPP_OQUEUE:{
1822 int len, lsr;
1823
1824 len = mxser_chars_in_buffer(tty);
1825
1826 lsr = inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT;
1827
1828 len += (lsr ? 0 : 1);
1829
1830 if (copy_to_user(argp, &len, sizeof(int)))
1831 return -EFAULT;
1832
1833 return 0;
1834 }
1835 case MOXA_ASPP_MON: {
1836 int mcr, status;
1837
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001838 status = mxser_get_msr(info->ioaddr, 1, tty->index);
1839 mxser_check_modem_status(info, status);
1840
1841 mcr = inb(info->ioaddr + UART_MCR);
1842 if (mcr & MOXA_MUST_MCR_XON_FLAG)
1843 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFHOLD;
1844 else
1845 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFHOLD;
1846
1847 if (mcr & MOXA_MUST_MCR_TX_XON)
1848 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFXENT;
1849 else
1850 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFXENT;
1851
1852 if (info->tty->hw_stopped)
1853 info->mon_data.hold_reason |= NPPI_NOTIFY_CTSHOLD;
1854 else
1855 info->mon_data.hold_reason &= ~NPPI_NOTIFY_CTSHOLD;
1856
1857 if (copy_to_user(argp, &info->mon_data,
1858 sizeof(struct mxser_mon)))
1859 return -EFAULT;
1860
1861 return 0;
1862 }
1863 case MOXA_ASPP_LSTATUS: {
1864 if (copy_to_user(argp, &info->err_shadow,
1865 sizeof(unsigned char)))
1866 return -EFAULT;
1867
1868 info->err_shadow = 0;
1869 return 0;
1870 }
1871 case MOXA_SET_BAUD_METHOD: {
1872 int method;
1873
1874 if (get_user(method, (int __user *)argp))
1875 return -EFAULT;
1876 mxser_set_baud_method[tty->index] = method;
1877 if (copy_to_user(argp, &method, sizeof(int)))
1878 return -EFAULT;
1879
1880 return 0;
1881 }
1882 default:
1883 return -ENOIOCTLCMD;
1884 }
1885 return 0;
1886}
1887
Jiri Slaby037ad482006-12-08 02:38:11 -08001888static void mxser_stoprx(struct tty_struct *tty)
1889{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001890 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001891
1892 info->ldisc_stop_rx = 1;
1893 if (I_IXOFF(tty)) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08001894 if (info->board->chip_flag) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001895 info->IER &= ~MOXA_MUST_RECV_ISR;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001896 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby925e9c12006-12-08 02:38:30 -08001897 } else {
Jiri Slaby037ad482006-12-08 02:38:11 -08001898 info->x_char = STOP_CHAR(tty);
Jiri Slaby55b307d2006-12-08 02:38:14 -08001899 outb(0, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001900 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001901 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001902 }
Jiri Slaby037ad482006-12-08 02:38:11 -08001903 }
1904
1905 if (info->tty->termios->c_cflag & CRTSCTS) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001906 info->MCR &= ~UART_MCR_RTS;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001907 outb(info->MCR, info->ioaddr + UART_MCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08001908 }
1909}
1910
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001911/*
1912 * This routine is called by the upper-layer tty layer to signal that
1913 * incoming characters should be throttled.
1914 */
1915static void mxser_throttle(struct tty_struct *tty)
1916{
1917 mxser_stoprx(tty);
1918}
1919
1920static void mxser_unthrottle(struct tty_struct *tty)
Jiri Slaby037ad482006-12-08 02:38:11 -08001921{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001922 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001923
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001924 /* startrx */
Jiri Slaby037ad482006-12-08 02:38:11 -08001925 info->ldisc_stop_rx = 0;
1926 if (I_IXOFF(tty)) {
1927 if (info->x_char)
1928 info->x_char = 0;
1929 else {
Jiri Slaby55b307d2006-12-08 02:38:14 -08001930 if (info->board->chip_flag) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001931 info->IER |= MOXA_MUST_RECV_ISR;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001932 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby925e9c12006-12-08 02:38:30 -08001933 } else {
Jiri Slaby037ad482006-12-08 02:38:11 -08001934 info->x_char = START_CHAR(tty);
Jiri Slaby55b307d2006-12-08 02:38:14 -08001935 outb(0, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001936 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001937 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001938 }
Jiri Slaby037ad482006-12-08 02:38:11 -08001939 }
1940 }
1941
1942 if (info->tty->termios->c_cflag & CRTSCTS) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001943 info->MCR |= UART_MCR_RTS;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001944 outb(info->MCR, info->ioaddr + UART_MCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08001945 }
1946}
1947
1948/*
Jiri Slaby037ad482006-12-08 02:38:11 -08001949 * mxser_stop() and mxser_start()
1950 *
1951 * This routines are called before setting or resetting tty->stopped.
1952 * They enable or disable transmitter interrupts, as necessary.
1953 */
1954static void mxser_stop(struct tty_struct *tty)
1955{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001956 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001957 unsigned long flags;
1958
1959 spin_lock_irqsave(&info->slock, flags);
1960 if (info->IER & UART_IER_THRI) {
1961 info->IER &= ~UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001962 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001963 }
1964 spin_unlock_irqrestore(&info->slock, flags);
1965}
1966
1967static void mxser_start(struct tty_struct *tty)
1968{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001969 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001970 unsigned long flags;
1971
1972 spin_lock_irqsave(&info->slock, flags);
Jiri Slabye079f492006-12-08 02:38:31 -08001973 if (info->xmit_cnt && info->xmit_buf) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08001974 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001975 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001976 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001977 }
1978 spin_unlock_irqrestore(&info->slock, flags);
1979}
1980
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001981static void mxser_set_termios(struct tty_struct *tty, struct termios *old_termios)
1982{
1983 struct mxser_port *info = tty->driver_data;
1984 unsigned long flags;
1985
1986 if ((tty->termios->c_cflag != old_termios->c_cflag) ||
1987 (RELEVANT_IFLAG(tty->termios->c_iflag) != RELEVANT_IFLAG(old_termios->c_iflag))) {
1988
1989 mxser_change_speed(info, old_termios);
1990
1991 if ((old_termios->c_cflag & CRTSCTS) &&
1992 !(tty->termios->c_cflag & CRTSCTS)) {
1993 tty->hw_stopped = 0;
1994 mxser_start(tty);
1995 }
1996 }
1997
Jiri Slabye079f492006-12-08 02:38:31 -08001998 /* Handle sw stopped */
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001999 if ((old_termios->c_iflag & IXON) &&
2000 !(tty->termios->c_iflag & IXON)) {
2001 tty->stopped = 0;
2002
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002003 if (info->board->chip_flag) {
2004 spin_lock_irqsave(&info->slock, flags);
2005 DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
2006 spin_unlock_irqrestore(&info->slock, flags);
2007 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002008
2009 mxser_start(tty);
2010 }
2011}
2012
Jiri Slaby037ad482006-12-08 02:38:11 -08002013/*
2014 * mxser_wait_until_sent() --- wait until the transmitter is empty
2015 */
2016static void mxser_wait_until_sent(struct tty_struct *tty, int timeout)
2017{
Jiri Slaby55b307d2006-12-08 02:38:14 -08002018 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08002019 unsigned long orig_jiffies, char_time;
2020 int lsr;
2021
2022 if (info->type == PORT_UNKNOWN)
2023 return;
2024
2025 if (info->xmit_fifo_size == 0)
2026 return; /* Just in case.... */
2027
2028 orig_jiffies = jiffies;
2029 /*
2030 * Set the check interval to be 1/5 of the estimated time to
2031 * send a single character, and make it at least 1. The check
2032 * interval should also be less than the timeout.
2033 *
2034 * Note: we have to use pretty tight timings here to satisfy
2035 * the NIST-PCTS.
2036 */
2037 char_time = (info->timeout - HZ / 50) / info->xmit_fifo_size;
2038 char_time = char_time / 5;
2039 if (char_time == 0)
2040 char_time = 1;
2041 if (timeout && timeout < char_time)
2042 char_time = timeout;
2043 /*
2044 * If the transmitter hasn't cleared in twice the approximate
2045 * amount of time to send the entire FIFO, it probably won't
2046 * ever clear. This assumes the UART isn't doing flow
2047 * control, which is currently the case. Hence, if it ever
2048 * takes longer than info->timeout, this is probably due to a
2049 * UART bug of some kind. So, we clamp the timeout parameter at
2050 * 2*info->timeout.
2051 */
2052 if (!timeout || timeout > 2 * info->timeout)
2053 timeout = 2 * info->timeout;
2054#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2055 printk(KERN_DEBUG "In rs_wait_until_sent(%d) check=%lu...",
2056 timeout, char_time);
2057 printk("jiff=%lu...", jiffies);
2058#endif
Jiri Slaby55b307d2006-12-08 02:38:14 -08002059 while (!((lsr = inb(info->ioaddr + UART_LSR)) & UART_LSR_TEMT)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002060#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2061 printk("lsr = %d (jiff=%lu)...", lsr, jiffies);
2062#endif
2063 schedule_timeout_interruptible(char_time);
2064 if (signal_pending(current))
2065 break;
2066 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2067 break;
2068 }
2069 set_current_state(TASK_RUNNING);
2070
2071#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2072 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
2073#endif
2074}
2075
Jiri Slaby037ad482006-12-08 02:38:11 -08002076/*
2077 * This routine is called by tty_hangup() when a hangup is signaled.
2078 */
2079void mxser_hangup(struct tty_struct *tty)
2080{
Jiri Slaby55b307d2006-12-08 02:38:14 -08002081 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08002082
2083 mxser_flush_buffer(tty);
2084 mxser_shutdown(info);
2085 info->event = 0;
2086 info->count = 0;
2087 info->flags &= ~ASYNC_NORMAL_ACTIVE;
2088 info->tty = NULL;
2089 wake_up_interruptible(&info->open_wait);
2090}
2091
Jiri Slaby037ad482006-12-08 02:38:11 -08002092/*
2093 * mxser_rs_break() --- routine which turns the break handling on or off
2094 */
2095static void mxser_rs_break(struct tty_struct *tty, int break_state)
2096{
Jiri Slaby55b307d2006-12-08 02:38:14 -08002097 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08002098 unsigned long flags;
2099
2100 spin_lock_irqsave(&info->slock, flags);
2101 if (break_state == -1)
Jiri Slaby55b307d2006-12-08 02:38:14 -08002102 outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
2103 info->ioaddr + UART_LCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002104 else
Jiri Slaby55b307d2006-12-08 02:38:14 -08002105 outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
2106 info->ioaddr + UART_LCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002107 spin_unlock_irqrestore(&info->slock, flags);
2108}
2109
Jiri Slaby55b307d2006-12-08 02:38:14 -08002110static void mxser_receive_chars(struct mxser_port *port, int *status)
Jiri Slaby037ad482006-12-08 02:38:11 -08002111{
Jiri Slaby55b307d2006-12-08 02:38:14 -08002112 struct tty_struct *tty = port->tty;
Jiri Slaby037ad482006-12-08 02:38:11 -08002113 unsigned char ch, gdl;
2114 int ignored = 0;
2115 int cnt = 0;
2116 int recv_room;
2117 int max = 256;
2118 unsigned long flags;
2119
Jiri Slaby55b307d2006-12-08 02:38:14 -08002120 spin_lock_irqsave(&port->slock, flags);
Jiri Slaby037ad482006-12-08 02:38:11 -08002121
2122 recv_room = tty->receive_room;
Jiri Slabye079f492006-12-08 02:38:31 -08002123 if ((recv_room == 0) && (!port->ldisc_stop_rx))
Jiri Slaby037ad482006-12-08 02:38:11 -08002124 mxser_stoprx(tty);
Jiri Slaby037ad482006-12-08 02:38:11 -08002125
Jiri Slaby55b307d2006-12-08 02:38:14 -08002126 if (port->board->chip_flag != MOXA_OTHER_UART) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002127
Jiri Slaby3306ce32006-12-08 02:38:13 -08002128 if (*status & UART_LSR_SPECIAL)
Jiri Slaby037ad482006-12-08 02:38:11 -08002129 goto intr_old;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002130 if (port->board->chip_flag == MOXA_MUST_MU860_HWID &&
Jiri Slaby037ad482006-12-08 02:38:11 -08002131 (*status & MOXA_MUST_LSR_RERR))
2132 goto intr_old;
Jiri Slaby037ad482006-12-08 02:38:11 -08002133 if (*status & MOXA_MUST_LSR_RERR)
2134 goto intr_old;
2135
Jiri Slaby55b307d2006-12-08 02:38:14 -08002136 gdl = inb(port->ioaddr + MOXA_MUST_GDL_REGISTER);
Jiri Slaby037ad482006-12-08 02:38:11 -08002137
Jiri Slaby55b307d2006-12-08 02:38:14 -08002138 if (port->board->chip_flag == MOXA_MUST_MU150_HWID)
Jiri Slaby037ad482006-12-08 02:38:11 -08002139 gdl &= MOXA_MUST_GDL_MASK;
2140 if (gdl >= recv_room) {
Jiri Slabye079f492006-12-08 02:38:31 -08002141 if (!port->ldisc_stop_rx)
Jiri Slaby037ad482006-12-08 02:38:11 -08002142 mxser_stoprx(tty);
Jiri Slaby037ad482006-12-08 02:38:11 -08002143 }
2144 while (gdl--) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002145 ch = inb(port->ioaddr + UART_RX);
Jiri Slaby037ad482006-12-08 02:38:11 -08002146 tty_insert_flip_char(tty, ch, 0);
2147 cnt++;
Jiri Slaby037ad482006-12-08 02:38:11 -08002148 }
2149 goto end_intr;
2150 }
Jiri Slabye079f492006-12-08 02:38:31 -08002151intr_old:
Jiri Slaby037ad482006-12-08 02:38:11 -08002152
2153 do {
2154 if (max-- < 0)
2155 break;
Jiri Slaby037ad482006-12-08 02:38:11 -08002156
Jiri Slaby55b307d2006-12-08 02:38:14 -08002157 ch = inb(port->ioaddr + UART_RX);
Jiri Slabye079f492006-12-08 02:38:31 -08002158 if (port->board->chip_flag && (*status & UART_LSR_OE))
Jiri Slaby55b307d2006-12-08 02:38:14 -08002159 outb(0x23, port->ioaddr + UART_FCR);
2160 *status &= port->read_status_mask;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002161 if (*status & port->ignore_status_mask) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002162 if (++ignored > 100)
2163 break;
2164 } else {
2165 char flag = 0;
2166 if (*status & UART_LSR_SPECIAL) {
2167 if (*status & UART_LSR_BI) {
2168 flag = TTY_BREAK;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002169 port->icount.brk++;
Jiri Slaby3306ce32006-12-08 02:38:13 -08002170
Jiri Slaby55b307d2006-12-08 02:38:14 -08002171 if (port->flags & ASYNC_SAK)
Jiri Slaby037ad482006-12-08 02:38:11 -08002172 do_SAK(tty);
2173 } else if (*status & UART_LSR_PE) {
2174 flag = TTY_PARITY;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002175 port->icount.parity++;
Jiri Slaby037ad482006-12-08 02:38:11 -08002176 } else if (*status & UART_LSR_FE) {
2177 flag = TTY_FRAME;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002178 port->icount.frame++;
Jiri Slaby037ad482006-12-08 02:38:11 -08002179 } else if (*status & UART_LSR_OE) {
2180 flag = TTY_OVERRUN;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002181 port->icount.overrun++;
Jiri Slaby925e9c12006-12-08 02:38:30 -08002182 }
2183 }
Jiri Slaby037ad482006-12-08 02:38:11 -08002184 tty_insert_flip_char(tty, ch, flag);
2185 cnt++;
2186 if (cnt >= recv_room) {
Jiri Slabye079f492006-12-08 02:38:31 -08002187 if (!port->ldisc_stop_rx)
Jiri Slaby037ad482006-12-08 02:38:11 -08002188 mxser_stoprx(tty);
Jiri Slaby037ad482006-12-08 02:38:11 -08002189 break;
2190 }
2191
2192 }
2193
Jiri Slaby55b307d2006-12-08 02:38:14 -08002194 if (port->board->chip_flag)
Jiri Slaby037ad482006-12-08 02:38:11 -08002195 break;
Jiri Slaby037ad482006-12-08 02:38:11 -08002196
Jiri Slaby55b307d2006-12-08 02:38:14 -08002197 *status = inb(port->ioaddr + UART_LSR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002198 } while (*status & UART_LSR_DR);
2199
Jiri Slabye079f492006-12-08 02:38:31 -08002200end_intr:
Jiri Slaby55b307d2006-12-08 02:38:14 -08002201 mxvar_log.rxcnt[port->tty->index] += cnt;
2202 port->mon_data.rxcnt += cnt;
2203 port->mon_data.up_rxcnt += cnt;
2204 spin_unlock_irqrestore(&port->slock, flags);
Jiri Slaby037ad482006-12-08 02:38:11 -08002205
2206 tty_flip_buffer_push(tty);
2207}
2208
Jiri Slaby55b307d2006-12-08 02:38:14 -08002209static void mxser_transmit_chars(struct mxser_port *port)
Jiri Slaby037ad482006-12-08 02:38:11 -08002210{
2211 int count, cnt;
2212 unsigned long flags;
2213
Jiri Slaby55b307d2006-12-08 02:38:14 -08002214 spin_lock_irqsave(&port->slock, flags);
Jiri Slaby037ad482006-12-08 02:38:11 -08002215
Jiri Slaby55b307d2006-12-08 02:38:14 -08002216 if (port->x_char) {
2217 outb(port->x_char, port->ioaddr + UART_TX);
2218 port->x_char = 0;
2219 mxvar_log.txcnt[port->tty->index]++;
2220 port->mon_data.txcnt++;
2221 port->mon_data.up_txcnt++;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002222 port->icount.tx++;
Jiri Slaby3306ce32006-12-08 02:38:13 -08002223 goto unlock;
Jiri Slaby037ad482006-12-08 02:38:11 -08002224 }
2225
Jiri Slaby55b307d2006-12-08 02:38:14 -08002226 if (port->xmit_buf == 0)
Jiri Slaby3306ce32006-12-08 02:38:13 -08002227 goto unlock;
Jiri Slaby037ad482006-12-08 02:38:11 -08002228
Jiri Slaby925e9c12006-12-08 02:38:30 -08002229 if ((port->xmit_cnt <= 0) || port->tty->stopped ||
2230 (port->tty->hw_stopped &&
Jiri Slaby55b307d2006-12-08 02:38:14 -08002231 (port->type != PORT_16550A) &&
2232 (!port->board->chip_flag))) {
2233 port->IER &= ~UART_IER_THRI;
2234 outb(port->IER, port->ioaddr + UART_IER);
Jiri Slaby3306ce32006-12-08 02:38:13 -08002235 goto unlock;
Jiri Slaby037ad482006-12-08 02:38:11 -08002236 }
2237
Jiri Slaby55b307d2006-12-08 02:38:14 -08002238 cnt = port->xmit_cnt;
2239 count = port->xmit_fifo_size;
Jiri Slaby037ad482006-12-08 02:38:11 -08002240 do {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002241 outb(port->xmit_buf[port->xmit_tail++],
2242 port->ioaddr + UART_TX);
2243 port->xmit_tail = port->xmit_tail & (SERIAL_XMIT_SIZE - 1);
2244 if (--port->xmit_cnt <= 0)
Jiri Slaby037ad482006-12-08 02:38:11 -08002245 break;
2246 } while (--count > 0);
Jiri Slaby55b307d2006-12-08 02:38:14 -08002247 mxvar_log.txcnt[port->tty->index] += (cnt - port->xmit_cnt);
Jiri Slaby037ad482006-12-08 02:38:11 -08002248
Jiri Slaby55b307d2006-12-08 02:38:14 -08002249 port->mon_data.txcnt += (cnt - port->xmit_cnt);
2250 port->mon_data.up_txcnt += (cnt - port->xmit_cnt);
Jiri Slaby55b307d2006-12-08 02:38:14 -08002251 port->icount.tx += (cnt - port->xmit_cnt);
Jiri Slaby037ad482006-12-08 02:38:11 -08002252
Jiri Slaby55b307d2006-12-08 02:38:14 -08002253 if (port->xmit_cnt < WAKEUP_CHARS) {
2254 set_bit(MXSER_EVENT_TXLOW, &port->event);
2255 schedule_work(&port->tqueue);
Jiri Slaby037ad482006-12-08 02:38:11 -08002256 }
Jiri Slaby55b307d2006-12-08 02:38:14 -08002257 if (port->xmit_cnt <= 0) {
2258 port->IER &= ~UART_IER_THRI;
2259 outb(port->IER, port->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08002260 }
Jiri Slaby3306ce32006-12-08 02:38:13 -08002261unlock:
Jiri Slaby55b307d2006-12-08 02:38:14 -08002262 spin_unlock_irqrestore(&port->slock, flags);
Jiri Slaby037ad482006-12-08 02:38:11 -08002263}
2264
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002265/*
2266 * This is the serial driver's generic interrupt routine
2267 */
2268static irqreturn_t mxser_interrupt(int irq, void *dev_id, struct pt_regs *regs)
Jiri Slaby037ad482006-12-08 02:38:11 -08002269{
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002270 int status, iir, i;
2271 struct mxser_board *brd = NULL;
2272 struct mxser_port *port;
2273 int max, irqbits, bits, msr;
2274 int pass_counter = 0;
2275 unsigned int int_cnt;
2276 int handled = IRQ_NONE;
Jiri Slaby037ad482006-12-08 02:38:11 -08002277
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002278 for (i = 0; i < MXSER_BOARDS; i++)
2279 if (dev_id == &mxser_boards[i]) {
2280 brd = dev_id;
2281 break;
Jiri Slaby037ad482006-12-08 02:38:11 -08002282 }
Jiri Slaby037ad482006-12-08 02:38:11 -08002283
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002284 if (i == MXSER_BOARDS)
2285 goto irq_stop;
2286 if (brd == NULL)
2287 goto irq_stop;
Jiri Slabycd7ed642006-12-08 02:38:29 -08002288 max = brd->info->nports;
Jiri Slaby037ad482006-12-08 02:38:11 -08002289 while (1) {
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002290 irqbits = inb(brd->vector) & brd->vector_mask;
2291 if (irqbits == brd->vector_mask)
Jiri Slaby037ad482006-12-08 02:38:11 -08002292 break;
Jiri Slaby037ad482006-12-08 02:38:11 -08002293
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002294 handled = IRQ_HANDLED;
2295 for (i = 0, bits = 1; i < max; i++, irqbits |= bits, bits <<= 1) {
2296 if (irqbits == brd->vector_mask)
Jiri Slaby037ad482006-12-08 02:38:11 -08002297 break;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002298 if (bits & irqbits)
2299 continue;
2300 port = &brd->ports[i];
Jiri Slaby037ad482006-12-08 02:38:11 -08002301
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002302 int_cnt = 0;
2303 do {
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002304 iir = inb(port->ioaddr + UART_IIR);
2305 if (iir & UART_IIR_NO_INT)
2306 break;
2307 iir &= MOXA_MUST_IIR_MASK;
2308 if (!port->tty) {
2309 status = inb(port->ioaddr + UART_LSR);
2310 outb(0x27, port->ioaddr + UART_FCR);
2311 inb(port->ioaddr + UART_MSR);
2312 break;
Jiri Slaby037ad482006-12-08 02:38:11 -08002313 }
Jiri Slaby037ad482006-12-08 02:38:11 -08002314
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002315 status = inb(port->ioaddr + UART_LSR);
2316
2317 if (status & UART_LSR_PE)
2318 port->err_shadow |= NPPI_NOTIFY_PARITY;
2319 if (status & UART_LSR_FE)
2320 port->err_shadow |= NPPI_NOTIFY_FRAMING;
2321 if (status & UART_LSR_OE)
2322 port->err_shadow |=
2323 NPPI_NOTIFY_HW_OVERRUN;
2324 if (status & UART_LSR_BI)
2325 port->err_shadow |= NPPI_NOTIFY_BREAK;
2326
2327 if (port->board->chip_flag) {
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002328 if (iir == MOXA_MUST_IIR_GDA ||
2329 iir == MOXA_MUST_IIR_RDA ||
2330 iir == MOXA_MUST_IIR_RTO ||
2331 iir == MOXA_MUST_IIR_LSR)
2332 mxser_receive_chars(port,
2333 &status);
2334
2335 } else {
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002336 status &= port->read_status_mask;
2337 if (status & UART_LSR_DR)
2338 mxser_receive_chars(port,
2339 &status);
2340 }
2341 msr = inb(port->ioaddr + UART_MSR);
2342 if (msr & UART_MSR_ANY_DELTA)
2343 mxser_check_modem_status(port, msr);
2344
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002345 if (port->board->chip_flag) {
2346 if (iir == 0x02 && (status &
2347 UART_LSR_THRE))
2348 mxser_transmit_chars(port);
2349 } else {
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002350 if (status & UART_LSR_THRE)
2351 mxser_transmit_chars(port);
2352 }
2353 } while (int_cnt++ < MXSER_ISR_PASS_LIMIT);
2354 }
2355 if (pass_counter++ > MXSER_ISR_PASS_LIMIT)
2356 break; /* Prevent infinite loops */
2357 }
2358
Jiri Slabye079f492006-12-08 02:38:31 -08002359irq_stop:
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002360 return handled;
2361}
2362
2363static const struct tty_operations mxser_ops = {
2364 .open = mxser_open,
2365 .close = mxser_close,
2366 .write = mxser_write,
2367 .put_char = mxser_put_char,
2368 .flush_chars = mxser_flush_chars,
2369 .write_room = mxser_write_room,
2370 .chars_in_buffer = mxser_chars_in_buffer,
2371 .flush_buffer = mxser_flush_buffer,
2372 .ioctl = mxser_ioctl,
2373 .throttle = mxser_throttle,
2374 .unthrottle = mxser_unthrottle,
2375 .set_termios = mxser_set_termios,
2376 .stop = mxser_stop,
2377 .start = mxser_start,
2378 .hangup = mxser_hangup,
2379 .break_ctl = mxser_rs_break,
2380 .wait_until_sent = mxser_wait_until_sent,
2381 .tiocmget = mxser_tiocmget,
2382 .tiocmset = mxser_tiocmset,
2383};
2384
2385/*
2386 * The MOXA Smartio/Industio serial driver boot-time initialization code!
2387 */
2388
Jiri Slaby171d3a82006-12-08 02:38:25 -08002389static void mxser_release_res(struct mxser_board *brd, unsigned int irq)
2390{
2391 struct pci_dev *pdev = brd->pdev;
2392
2393 if (irq)
2394 free_irq(brd->irq, brd);
2395 if (pdev != NULL) { /* PCI */
2396 pci_release_region(pdev, 2);
2397 pci_release_region(pdev, 3);
2398 pci_dev_put(pdev);
2399 } else {
Jiri Slabycd7ed642006-12-08 02:38:29 -08002400 release_region(brd->ports[0].ioaddr, 8 * brd->info->nports);
Jiri Slaby171d3a82006-12-08 02:38:25 -08002401 release_region(brd->vector, 1);
2402 }
2403}
2404
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002405static int __devinit mxser_initbrd(struct mxser_board *brd)
2406{
2407 struct mxser_port *info;
2408 unsigned int i;
2409 int retval;
2410
2411 printk(KERN_INFO "max. baud rate = %d bps.\n", brd->ports[0].max_baud);
2412
Jiri Slabycd7ed642006-12-08 02:38:29 -08002413 for (i = 0; i < brd->info->nports; i++) {
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002414 info = &brd->ports[i];
2415 info->board = brd;
2416 info->stop_rx = 0;
2417 info->ldisc_stop_rx = 0;
2418
2419 /* Enhance mode enabled here */
2420 if (brd->chip_flag != MOXA_OTHER_UART)
2421 ENABLE_MOXA_MUST_ENCHANCE_MODE(info->ioaddr);
2422
2423 info->flags = ASYNC_SHARE_IRQ;
2424 info->type = brd->uart_type;
2425
2426 process_txrx_fifo(info);
2427
2428 info->custom_divisor = info->baud_base * 16;
2429 info->close_delay = 5 * HZ / 10;
2430 info->closing_wait = 30 * HZ;
2431 INIT_WORK(&info->tqueue, mxser_do_softint, info);
2432 info->normal_termios = mxvar_sdriver->init_termios;
2433 init_waitqueue_head(&info->open_wait);
2434 init_waitqueue_head(&info->close_wait);
2435 init_waitqueue_head(&info->delta_msr_wait);
2436 memset(&info->mon_data, 0, sizeof(struct mxser_mon));
2437 info->err_shadow = 0;
2438 spin_lock_init(&info->slock);
2439
2440 /* before set INT ISR, disable all int */
2441 outb(inb(info->ioaddr + UART_IER) & 0xf0,
2442 info->ioaddr + UART_IER);
2443 }
Jiri Slaby037ad482006-12-08 02:38:11 -08002444 /*
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002445 * Allocate the IRQ if necessary
Jiri Slaby037ad482006-12-08 02:38:11 -08002446 */
Jiri Slaby037ad482006-12-08 02:38:11 -08002447
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002448 retval = request_irq(brd->irq, mxser_interrupt,
2449 (brd->ports[0].flags & ASYNC_SHARE_IRQ) ? IRQF_SHARED :
2450 IRQF_DISABLED, "mxser", brd);
2451 if (retval) {
2452 printk(KERN_ERR "Board %s: Request irq failed, IRQ (%d) may "
2453 "conflict with another device.\n",
Jiri Slabycd7ed642006-12-08 02:38:29 -08002454 brd->info->name, brd->irq);
Jiri Slaby171d3a82006-12-08 02:38:25 -08002455 /* We hold resources, we need to release them. */
2456 mxser_release_res(brd, 0);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002457 return retval;
Jiri Slaby037ad482006-12-08 02:38:11 -08002458 }
Jiri Slaby037ad482006-12-08 02:38:11 -08002459 return 0;
2460}
2461
Jiri Slaby943f2952006-12-08 02:38:15 -08002462static int __init mxser_get_ISA_conf(int cap, struct mxser_board *brd)
Jiri Slaby037ad482006-12-08 02:38:11 -08002463{
2464 int id, i, bits;
2465 unsigned short regs[16], irq;
2466 unsigned char scratch, scratch2;
2467
Jiri Slaby55b307d2006-12-08 02:38:14 -08002468 brd->chip_flag = MOXA_OTHER_UART;
Jiri Slaby037ad482006-12-08 02:38:11 -08002469
2470 id = mxser_read_register(cap, regs);
Jiri Slabycd7ed642006-12-08 02:38:29 -08002471 switch (id) {
2472 case C168_ASIC_ID:
2473 brd->info = &mxser_cards[0];
2474 break;
2475 case C104_ASIC_ID:
2476 brd->info = &mxser_cards[1];
2477 break;
2478 case CI104J_ASIC_ID:
2479 brd->info = &mxser_cards[2];
2480 break;
2481 case C102_ASIC_ID:
2482 brd->info = &mxser_cards[5];
2483 break;
2484 case CI132_ASIC_ID:
2485 brd->info = &mxser_cards[6];
2486 break;
2487 case CI134_ASIC_ID:
2488 brd->info = &mxser_cards[7];
2489 break;
2490 default:
Jiri Slaby037ad482006-12-08 02:38:11 -08002491 return 0;
Jiri Slabycd7ed642006-12-08 02:38:29 -08002492 }
Jiri Slaby037ad482006-12-08 02:38:11 -08002493
2494 irq = 0;
Jiri Slabycd7ed642006-12-08 02:38:29 -08002495 /* some ISA cards have 2 ports, but we want to see them as 4-port (why?)
2496 Flag-hack checks if configuration should be read as 2-port here. */
2497 if (brd->info->nports == 2 || (brd->info->flags & MXSER_HAS2)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002498 irq = regs[9] & 0xF000;
2499 irq = irq | (irq >> 4);
2500 if (irq != (regs[9] & 0xFF00))
2501 return MXSER_ERR_IRQ_CONFLIT;
Jiri Slabycd7ed642006-12-08 02:38:29 -08002502 } else if (brd->info->nports == 4) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002503 irq = regs[9] & 0xF000;
2504 irq = irq | (irq >> 4);
2505 irq = irq | (irq >> 8);
2506 if (irq != regs[9])
2507 return MXSER_ERR_IRQ_CONFLIT;
Jiri Slabycd7ed642006-12-08 02:38:29 -08002508 } else if (brd->info->nports == 8) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002509 irq = regs[9] & 0xF000;
2510 irq = irq | (irq >> 4);
2511 irq = irq | (irq >> 8);
2512 if ((irq != regs[9]) || (irq != regs[10]))
2513 return MXSER_ERR_IRQ_CONFLIT;
2514 }
2515
2516 if (!irq)
2517 return MXSER_ERR_IRQ;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002518 brd->irq = ((int)(irq & 0xF000) >> 12);
Jiri Slaby037ad482006-12-08 02:38:11 -08002519 for (i = 0; i < 8; i++)
Jiri Slaby55b307d2006-12-08 02:38:14 -08002520 brd->ports[i].ioaddr = (int) regs[i + 1] & 0xFFF8;
Jiri Slaby037ad482006-12-08 02:38:11 -08002521 if ((regs[12] & 0x80) == 0)
2522 return MXSER_ERR_VECTOR;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002523 brd->vector = (int)regs[11]; /* interrupt vector */
Jiri Slaby037ad482006-12-08 02:38:11 -08002524 if (id == 1)
Jiri Slaby55b307d2006-12-08 02:38:14 -08002525 brd->vector_mask = 0x00FF;
Jiri Slaby037ad482006-12-08 02:38:11 -08002526 else
Jiri Slaby55b307d2006-12-08 02:38:14 -08002527 brd->vector_mask = 0x000F;
Jiri Slaby037ad482006-12-08 02:38:11 -08002528 for (i = 7, bits = 0x0100; i >= 0; i--, bits <<= 1) {
2529 if (regs[12] & bits) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002530 brd->ports[i].baud_base = 921600;
Jiri Slabye079f492006-12-08 02:38:31 -08002531 brd->ports[i].max_baud = 921600;
Jiri Slaby037ad482006-12-08 02:38:11 -08002532 } else {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002533 brd->ports[i].baud_base = 115200;
Jiri Slabye079f492006-12-08 02:38:31 -08002534 brd->ports[i].max_baud = 115200;
Jiri Slaby037ad482006-12-08 02:38:11 -08002535 }
2536 }
2537 scratch2 = inb(cap + UART_LCR) & (~UART_LCR_DLAB);
2538 outb(scratch2 | UART_LCR_DLAB, cap + UART_LCR);
2539 outb(0, cap + UART_EFR); /* EFR is the same as FCR */
2540 outb(scratch2, cap + UART_LCR);
2541 outb(UART_FCR_ENABLE_FIFO, cap + UART_FCR);
2542 scratch = inb(cap + UART_IIR);
2543
2544 if (scratch & 0xC0)
Jiri Slaby55b307d2006-12-08 02:38:14 -08002545 brd->uart_type = PORT_16550A;
Jiri Slaby037ad482006-12-08 02:38:11 -08002546 else
Jiri Slaby55b307d2006-12-08 02:38:14 -08002547 brd->uart_type = PORT_16450;
Jiri Slabycd7ed642006-12-08 02:38:29 -08002548 if (!request_region(brd->ports[0].ioaddr, 8 * brd->info->nports,
2549 "mxser(IO)"))
Jiri Slaby7a7a5c32006-12-08 02:38:17 -08002550 return MXSER_ERR_IOADDR;
2551 if (!request_region(brd->vector, 1, "mxser(vector)")) {
Jiri Slabycd7ed642006-12-08 02:38:29 -08002552 release_region(brd->ports[0].ioaddr, 8 * brd->info->nports);
Jiri Slaby7a7a5c32006-12-08 02:38:17 -08002553 return MXSER_ERR_VECTOR;
2554 }
Jiri Slabycd7ed642006-12-08 02:38:29 -08002555 return brd->info->nports;
Jiri Slaby037ad482006-12-08 02:38:11 -08002556}
2557
Jiri Slabycd7ed642006-12-08 02:38:29 -08002558static int __init mxser_get_PCI_conf(const struct pci_device_id *ent,
2559 struct mxser_board *brd, struct pci_dev *pdev)
Jiri Slaby037ad482006-12-08 02:38:11 -08002560{
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002561 unsigned int i, j;
2562 unsigned long ioaddress;
2563 int retval;
Jiri Slaby037ad482006-12-08 02:38:11 -08002564
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002565 /* io address */
Jiri Slabycd7ed642006-12-08 02:38:29 -08002566 brd->info = &mxser_cards[ent->driver_data];
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002567 ioaddress = pci_resource_start(pdev, 2);
2568 retval = pci_request_region(pdev, 2, "mxser(IO)");
2569 if (retval)
2570 goto err;
2571
Jiri Slabycd7ed642006-12-08 02:38:29 -08002572 for (i = 0; i < brd->info->nports; i++)
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002573 brd->ports[i].ioaddr = ioaddress + 8 * i;
2574
2575 /* vector */
2576 ioaddress = pci_resource_start(pdev, 3);
2577 retval = pci_request_region(pdev, 3, "mxser(vector)");
2578 if (retval)
2579 goto err_relio;
2580 brd->vector = ioaddress;
2581
2582 /* irq */
2583 brd->irq = pdev->irq;
2584
2585 brd->chip_flag = CheckIsMoxaMust(brd->ports[0].ioaddr);
2586 brd->uart_type = PORT_16550A;
2587 brd->vector_mask = 0;
2588
Jiri Slabycd7ed642006-12-08 02:38:29 -08002589 for (i = 0; i < brd->info->nports; i++) {
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002590 for (j = 0; j < UART_INFO_NUM; j++) {
2591 if (Gpci_uart_info[j].type == brd->chip_flag) {
2592 brd->ports[i].max_baud =
2593 Gpci_uart_info[j].max_baud;
2594
2595 /* exception....CP-102 */
Jiri Slabycd7ed642006-12-08 02:38:29 -08002596 if (brd->info->flags & MXSER_HIGHBAUD)
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002597 brd->ports[i].max_baud = 921600;
2598 break;
Jiri Slaby037ad482006-12-08 02:38:11 -08002599 }
2600 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002601 }
2602
2603 if (brd->chip_flag == MOXA_MUST_MU860_HWID) {
Jiri Slabycd7ed642006-12-08 02:38:29 -08002604 for (i = 0; i < brd->info->nports; i++) {
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002605 if (i < 4)
2606 brd->ports[i].opmode_ioaddr = ioaddress + 4;
2607 else
2608 brd->ports[i].opmode_ioaddr = ioaddress + 0x0c;
Jiri Slaby037ad482006-12-08 02:38:11 -08002609 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002610 outb(0, ioaddress + 4); /* default set to RS232 mode */
2611 outb(0, ioaddress + 0x0c); /* default set to RS232 mode */
Jiri Slaby037ad482006-12-08 02:38:11 -08002612 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002613
Jiri Slabycd7ed642006-12-08 02:38:29 -08002614 for (i = 0; i < brd->info->nports; i++) {
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002615 brd->vector_mask |= (1 << i);
2616 brd->ports[i].baud_base = 921600;
2617 }
2618 return 0;
2619err_relio:
2620 pci_release_region(pdev, 2);
2621err:
2622 return retval;
Jiri Slaby037ad482006-12-08 02:38:11 -08002623}
2624
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002625static int __init mxser_module_init(void)
Jiri Slaby037ad482006-12-08 02:38:11 -08002626{
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002627 struct pci_dev *pdev = NULL;
2628 struct mxser_board *brd;
Jiri Slabyeae44362006-12-08 02:38:27 -08002629 unsigned long cap;
2630 unsigned int i, m, isaloop;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002631 int retval, b, n;
Jiri Slaby037ad482006-12-08 02:38:11 -08002632
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002633 pr_debug("Loading module mxser ...\n");
Jiri Slaby037ad482006-12-08 02:38:11 -08002634
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002635 mxvar_sdriver = alloc_tty_driver(MXSER_PORTS + 1);
2636 if (!mxvar_sdriver)
2637 return -ENOMEM;
2638 spin_lock_init(&gm_lock);
2639
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002640 printk(KERN_INFO "MOXA Smartio/Industio family driver version %s\n",
2641 MXSER_VERSION);
2642
2643 /* Initialize the tty_driver structure */
2644 mxvar_sdriver->magic = TTY_DRIVER_MAGIC;
2645 mxvar_sdriver->name = "ttyM";
2646 mxvar_sdriver->major = ttymajor;
2647 mxvar_sdriver->minor_start = 0;
2648 mxvar_sdriver->num = MXSER_PORTS + 1;
2649 mxvar_sdriver->type = TTY_DRIVER_TYPE_SERIAL;
2650 mxvar_sdriver->subtype = SERIAL_TYPE_NORMAL;
2651 mxvar_sdriver->init_termios = tty_std_termios;
2652 mxvar_sdriver->init_termios.c_cflag = B9600|CS8|CREAD|HUPCL|CLOCAL;
Jiri Slaby938ef182006-12-08 02:38:28 -08002653 mxvar_sdriver->flags = TTY_DRIVER_REAL_RAW|TTY_DRIVER_DYNAMIC_DEV;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002654 tty_set_operations(mxvar_sdriver, &mxser_ops);
2655 mxvar_sdriver->ttys = mxvar_tty;
2656 mxvar_sdriver->termios = mxvar_termios;
2657 mxvar_sdriver->termios_locked = mxvar_termios_locked;
2658
Jiri Slaby938ef182006-12-08 02:38:28 -08002659 retval = tty_register_driver(mxvar_sdriver);
2660 if (retval) {
2661 printk(KERN_ERR "Couldn't install MOXA Smartio/Industio family "
2662 "tty driver !\n");
2663 goto err_put;
2664 }
2665
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002666 mxvar_diagflag = 0;
2667
2668 m = 0;
2669 /* Start finding ISA boards here */
Jiri Slabyeae44362006-12-08 02:38:27 -08002670 for (isaloop = 0; isaloop < 2; isaloop++)
2671 for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
2672 if (!isaloop)
2673 cap = mxserBoardCAP[b]; /* predefined */
2674 else
2675 cap = ioaddr[b]; /* module param */
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002676
Jiri Slabyeae44362006-12-08 02:38:27 -08002677 if (!cap)
2678 continue;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002679
Jiri Slabyeae44362006-12-08 02:38:27 -08002680 brd = &mxser_boards[m];
2681 retval = mxser_get_ISA_conf(cap, brd);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002682
Jiri Slabyeae44362006-12-08 02:38:27 -08002683 if (retval != 0)
2684 printk(KERN_INFO "Found MOXA %s board "
2685 "(CAP=0x%x)\n",
Jiri Slabycd7ed642006-12-08 02:38:29 -08002686 brd->info->name, ioaddr[b]);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002687
Jiri Slabyeae44362006-12-08 02:38:27 -08002688 if (retval <= 0) {
2689 if (retval == MXSER_ERR_IRQ)
2690 printk(KERN_ERR "Invalid interrupt "
2691 "number, board not "
2692 "configured\n");
2693 else if (retval == MXSER_ERR_IRQ_CONFLIT)
2694 printk(KERN_ERR "Invalid interrupt "
2695 "number, board not "
2696 "configured\n");
2697 else if (retval == MXSER_ERR_VECTOR)
2698 printk(KERN_ERR "Invalid interrupt "
2699 "vector, board not "
2700 "configured\n");
2701 else if (retval == MXSER_ERR_IOADDR)
2702 printk(KERN_ERR "Invalid I/O address, "
2703 "board not configured\n");
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002704
Jiri Slabyeae44362006-12-08 02:38:27 -08002705 continue;
2706 }
2707
2708 brd->pdev = NULL;
2709
2710 /* mxser_initbrd will hook ISR. */
2711 if (mxser_initbrd(brd) < 0)
2712 continue;
2713
Jiri Slabycd7ed642006-12-08 02:38:29 -08002714 for (i = 0; i < brd->info->nports; i++)
Jiri Slaby938ef182006-12-08 02:38:28 -08002715 tty_register_device(mxvar_sdriver,
2716 m * MXSER_PORTS_PER_BOARD + i, NULL);
2717
Jiri Slabyeae44362006-12-08 02:38:27 -08002718 m++;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002719 }
2720
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002721 /* start finding PCI board here */
2722 n = ARRAY_SIZE(mxser_pcibrds) - 1;
2723 b = 0;
2724 while (b < n) {
2725 pdev = pci_get_device(mxser_pcibrds[b].vendor,
2726 mxser_pcibrds[b].device, pdev);
2727 if (pdev == NULL) {
2728 b++;
2729 continue;
2730 }
2731 printk(KERN_INFO "Found MOXA %s board(BusNo=%d,DevNo=%d)\n",
Jiri Slabycd7ed642006-12-08 02:38:29 -08002732 mxser_cards[mxser_pcibrds[b].driver_data].name,
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002733 pdev->bus->number, PCI_SLOT(pdev->devfn));
2734 if (m >= MXSER_BOARDS)
2735 printk(KERN_ERR
2736 "Too many Smartio/Industio family boards find "
2737 "(maximum %d), board not configured\n",
2738 MXSER_BOARDS);
2739 else {
2740 if (pci_enable_device(pdev)) {
2741 printk(KERN_ERR "Moxa SmartI/O PCI enable "
2742 "fail !\n");
2743 continue;
2744 }
2745 brd = &mxser_boards[m];
2746 brd->pdev = pdev;
Jiri Slabycd7ed642006-12-08 02:38:29 -08002747 retval = mxser_get_PCI_conf(&mxser_pcibrds[b],
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002748 brd, pdev);
2749 if (retval < 0) {
2750 if (retval == MXSER_ERR_IRQ)
2751 printk(KERN_ERR
2752 "Invalid interrupt number, "
2753 "board not configured\n");
2754 else if (retval == MXSER_ERR_IRQ_CONFLIT)
2755 printk(KERN_ERR
2756 "Invalid interrupt number, "
2757 "board not configured\n");
2758 else if (retval == MXSER_ERR_VECTOR)
2759 printk(KERN_ERR
2760 "Invalid interrupt vector, "
2761 "board not configured\n");
2762 else if (retval == MXSER_ERR_IOADDR)
2763 printk(KERN_ERR
2764 "Invalid I/O address, "
2765 "board not configured\n");
2766 continue;
2767 }
2768 /* mxser_initbrd will hook ISR. */
2769 if (mxser_initbrd(brd) < 0)
2770 continue;
Jiri Slabycd7ed642006-12-08 02:38:29 -08002771 for (i = 0; i < brd->info->nports; i++)
Jiri Slaby938ef182006-12-08 02:38:28 -08002772 tty_register_device(mxvar_sdriver,
2773 m * MXSER_PORTS_PER_BOARD + i,
2774 &pdev->dev);
2775
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002776 m++;
2777 /* Keep an extra reference if we succeeded. It will
2778 be returned at unload time */
2779 pci_dev_get(pdev);
2780 }
2781 }
2782
Jiri Slaby938ef182006-12-08 02:38:28 -08002783 if (!m) {
2784 retval = -ENODEV;
2785 goto err_unr;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002786 }
2787
2788 pr_debug("Done.\n");
2789
Jiri Slaby938ef182006-12-08 02:38:28 -08002790 return 0;
2791err_unr:
2792 tty_unregister_driver(mxvar_sdriver);
2793err_put:
2794 put_tty_driver(mxvar_sdriver);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002795 return retval;
Jiri Slaby037ad482006-12-08 02:38:11 -08002796}
2797
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002798static void __exit mxser_module_exit(void)
Jiri Slaby037ad482006-12-08 02:38:11 -08002799{
Jiri Slabyead568c2006-12-08 02:38:26 -08002800 unsigned int i;
Jiri Slaby037ad482006-12-08 02:38:11 -08002801
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002802 pr_debug("Unloading module mxser ...\n");
2803
Jiri Slaby938ef182006-12-08 02:38:28 -08002804 for (i = 0; i < MXSER_PORTS; i++)
2805 tty_unregister_device(mxvar_sdriver, i);
Jiri Slabyead568c2006-12-08 02:38:26 -08002806 tty_unregister_driver(mxvar_sdriver);
2807 put_tty_driver(mxvar_sdriver);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002808
Jiri Slaby171d3a82006-12-08 02:38:25 -08002809 for (i = 0; i < MXSER_BOARDS; i++)
Jiri Slabycd7ed642006-12-08 02:38:29 -08002810 if (mxser_boards[i].info != NULL)
Jiri Slaby171d3a82006-12-08 02:38:25 -08002811 mxser_release_res(&mxser_boards[i], 1);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002812
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002813 pr_debug("Done.\n");
Jiri Slaby037ad482006-12-08 02:38:11 -08002814}
2815
2816module_init(mxser_module_init);
2817module_exit(mxser_module_exit);