blob: 28f63566ab8a2029f417ed5983c30a24781792e6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * mxser.c -- MOXA Smartio/Industio family multiport serial driver.
3 *
Jiri Slaby80ff8a82008-02-07 00:16:51 -08004 * Copyright (C) 1999-2006 Moxa Technologies (support@moxa.com).
5 * Copyright (C) 2006-2008 Jiri Slaby <jirislaby@gmail.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
Jiri Slaby1c456072008-02-07 00:16:46 -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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
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
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -070014 * (at your option) any later version.
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/errno.h>
24#include <linux/signal.h>
25#include <linux/sched.h>
26#include <linux/timer.h>
27#include <linux/interrupt.h>
28#include <linux/tty.h>
29#include <linux/tty_flip.h>
30#include <linux/serial.h>
31#include <linux/serial_reg.h>
32#include <linux/major.h>
33#include <linux/string.h>
34#include <linux/fcntl.h>
35#include <linux/ptrace.h>
36#include <linux/gfp.h>
37#include <linux/ioport.h>
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/pci.h>
Jiri Slaby1977f032007-10-18 23:40:25 -070041#include <linux/bitops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#include <asm/system.h>
44#include <asm/io.h>
45#include <asm/irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <asm/uaccess.h>
47
48#include "mxser.h"
49
Jiri Slaby80ff8a82008-02-07 00:16:51 -080050#define MXSER_VERSION "2.0.3" /* 1.11 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#define MXSERMAJOR 174
52#define MXSERCUMAJOR 175
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#define MXSER_BOARDS 4 /* Max. boards */
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#define MXSER_PORTS_PER_BOARD 8 /* Max. ports per board */
Jiri Slaby1c456072008-02-07 00:16:46 -080056#define MXSER_PORTS (MXSER_BOARDS * MXSER_PORTS_PER_BOARD)
57#define MXSER_ISR_PASS_LIMIT 100
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59#define MXSER_ERR_IOADDR -1
60#define MXSER_ERR_IRQ -2
61#define MXSER_ERR_IRQ_CONFLIT -3
62#define MXSER_ERR_VECTOR -4
63
Jiri Slaby1c456072008-02-07 00:16:46 -080064/*CheckIsMoxaMust return value*/
65#define MOXA_OTHER_UART 0x00
66#define MOXA_MUST_MU150_HWID 0x01
67#define MOXA_MUST_MU860_HWID 0x02
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#define WAKEUP_CHARS 256
70
71#define UART_MCR_AFE 0x20
72#define UART_LSR_SPECIAL 0x1E
73
Jiri Slaby1c456072008-02-07 00:16:46 -080074#define PCI_DEVICE_ID_CB108 0x1080
75#define PCI_DEVICE_ID_CB114 0x1142
Jiri Slaby80ff8a82008-02-07 00:16:51 -080076#define PCI_DEVICE_ID_CP114UL 0x1143
Jiri Slaby1c456072008-02-07 00:16:46 -080077#define PCI_DEVICE_ID_CB134I 0x1341
78#define PCI_DEVICE_ID_CP138U 0x1380
79#define PCI_DEVICE_ID_POS104UL 0x1044
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82#define C168_ASIC_ID 1
83#define C104_ASIC_ID 2
84#define C102_ASIC_ID 0xB
85#define CI132_ASIC_ID 4
86#define CI134_ASIC_ID 3
87#define CI104J_ASIC_ID 5
88
Jiri Slaby1c456072008-02-07 00:16:46 -080089#define MXSER_HIGHBAUD 1
90#define MXSER_HAS2 2
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -070092/* This is only for PCI */
Jiri Slaby1c456072008-02-07 00:16:46 -080093static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 int type;
95 int tx_fifo;
96 int rx_fifo;
97 int xmit_fifo_size;
98 int rx_high_water;
99 int rx_trigger;
100 int rx_low_water;
101 long max_baud;
Jiri Slaby1c456072008-02-07 00:16:46 -0800102} Gpci_uart_info[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 {MOXA_OTHER_UART, 16, 16, 16, 14, 14, 1, 921600L},
104 {MOXA_MUST_MU150_HWID, 64, 64, 64, 48, 48, 16, 230400L},
105 {MOXA_MUST_MU860_HWID, 128, 128, 128, 96, 96, 32, 921600L}
106};
Jiri Slaby1c456072008-02-07 00:16:46 -0800107#define UART_INFO_NUM ARRAY_SIZE(Gpci_uart_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Jiri Slaby1c456072008-02-07 00:16:46 -0800109struct mxser_cardinfo {
110 char *name;
111 unsigned int nports;
112 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113};
114
Jiri Slaby1c456072008-02-07 00:16:46 -0800115static const struct mxser_cardinfo mxser_cards[] = {
116/* 0*/ { "C168 series", 8, },
117 { "C104 series", 4, },
118 { "CI-104J series", 4, },
119 { "C168H/PCI series", 8, },
120 { "C104H/PCI series", 4, },
121/* 5*/ { "C102 series", 4, MXSER_HAS2 }, /* C102-ISA */
122 { "CI-132 series", 4, MXSER_HAS2 },
123 { "CI-134 series", 4, },
124 { "CP-132 series", 2, },
125 { "CP-114 series", 4, },
126/*10*/ { "CT-114 series", 4, },
127 { "CP-102 series", 2, MXSER_HIGHBAUD },
128 { "CP-104U series", 4, },
129 { "CP-168U series", 8, },
130 { "CP-132U series", 2, },
131/*15*/ { "CP-134U series", 4, },
132 { "CP-104JU series", 4, },
133 { "Moxa UC7000 Serial", 8, }, /* RC7000 */
134 { "CP-118U series", 8, },
135 { "CP-102UL series", 2, },
136/*20*/ { "CP-102U series", 2, },
137 { "CP-118EL series", 8, },
138 { "CP-168EL series", 8, },
139 { "CP-104EL series", 4, },
140 { "CB-108 series", 8, },
141/*25*/ { "CB-114 series", 4, },
142 { "CB-134I series", 4, },
143 { "CP-138U series", 8, },
Jiri Slaby80ff8a82008-02-07 00:16:51 -0800144 { "POS-104UL series", 4, },
145 { "CP-114UL series", 4, }
Jiri Slaby1c456072008-02-07 00:16:46 -0800146};
147
148/* driver_data correspond to the lines in the structure above
149 see also ISA probe function before you change something */
150static struct pci_device_id mxser_pcibrds[] = {
151 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_C168), .driver_data = 3 },
152 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_C104), .driver_data = 4 },
153 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP132), .driver_data = 8 },
154 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP114), .driver_data = 9 },
155 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CT114), .driver_data = 10 },
156 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102), .driver_data = 11 },
157 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104U), .driver_data = 12 },
158 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP168U), .driver_data = 13 },
159 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP132U), .driver_data = 14 },
160 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP134U), .driver_data = 15 },
161 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104JU),.driver_data = 16 },
162 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_RC7000), .driver_data = 17 },
163 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP118U), .driver_data = 18 },
164 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102UL),.driver_data = 19 },
165 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102U), .driver_data = 20 },
166 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP118EL),.driver_data = 21 },
167 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP168EL),.driver_data = 22 },
168 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104EL),.driver_data = 23 },
169 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB108), .driver_data = 24 },
170 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB114), .driver_data = 25 },
171 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB134I), .driver_data = 26 },
172 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP138U), .driver_data = 27 },
173 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_POS104UL), .driver_data = 28 },
Jiri Slaby80ff8a82008-02-07 00:16:51 -0800174 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP114UL), .driver_data = 29 },
Jiri Slaby1c456072008-02-07 00:16:46 -0800175 { }
176};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177MODULE_DEVICE_TABLE(pci, mxser_pcibrds);
178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179static int ioaddr[MXSER_BOARDS] = { 0, 0, 0, 0 };
180static int ttymajor = MXSERMAJOR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182/* Variables for insmod */
183
184MODULE_AUTHOR("Casper Yang");
185MODULE_DESCRIPTION("MOXA Smartio/Industio Family Multiport Board Device Driver");
Rusty Russell8d3b33f2006-03-25 03:07:05 -0800186module_param_array(ioaddr, int, NULL, 0);
187module_param(ttymajor, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188MODULE_LICENSE("GPL");
189
190struct mxser_log {
191 int tick;
192 unsigned long rxcnt[MXSER_PORTS];
193 unsigned long txcnt[MXSER_PORTS];
194};
195
196
197struct mxser_mon {
198 unsigned long rxcnt;
199 unsigned long txcnt;
200 unsigned long up_rxcnt;
201 unsigned long up_txcnt;
202 int modem_status;
203 unsigned char hold_reason;
204};
205
206struct mxser_mon_ext {
207 unsigned long rx_cnt[32];
208 unsigned long tx_cnt[32];
209 unsigned long up_rxcnt[32];
210 unsigned long up_txcnt[32];
211 int modem_status[32];
212
213 long baudrate[32];
214 int databits[32];
215 int stopbits[32];
216 int parity[32];
217 int flowctrl[32];
218 int fifo[32];
219 int iftype[32];
220};
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700221
Jiri Slaby1c456072008-02-07 00:16:46 -0800222struct mxser_board;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Jiri Slaby1c456072008-02-07 00:16:46 -0800224struct mxser_port {
225 struct mxser_board *board;
226 struct tty_struct *tty;
227
228 unsigned long ioaddr;
229 unsigned long opmode_ioaddr;
230 int max_baud;
231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 int rx_high_water;
233 int rx_trigger; /* Rx fifo trigger level */
234 int rx_low_water;
235 int baud_base; /* max. speed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 int type; /* UART type */
Jiri Slaby1c456072008-02-07 00:16:46 -0800237 int flags; /* defined in tty.h */
238
239 int x_char; /* xon/xoff character */
240 int IER; /* Interrupt Enable Register */
241 int MCR; /* Modem control register */
242
243 unsigned char stop_rx;
244 unsigned char ldisc_stop_rx;
245
246 int custom_divisor;
247 int close_delay;
248 unsigned short closing_wait;
249 unsigned char err_shadow;
250 unsigned long event;
251
252 int count; /* # of fd on device */
253 int blocked_open; /* # of blocked opens */
254 struct async_icount icount; /* kernel counters for 4 input interrupts */
255 int timeout;
256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 int read_status_mask;
258 int ignore_status_mask;
259 int xmit_fifo_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 unsigned char *xmit_buf;
261 int xmit_head;
262 int xmit_tail;
263 int xmit_cnt;
Jiri Slaby1c456072008-02-07 00:16:46 -0800264
Alan Cox606d0992006-12-08 02:38:45 -0800265 struct ktermios normal_termios;
Jiri Slaby1c456072008-02-07 00:16:46 -0800266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 struct mxser_mon mon_data;
Jiri Slaby1c456072008-02-07 00:16:46 -0800268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 spinlock_t slock;
Jiri Slaby1c456072008-02-07 00:16:46 -0800270 wait_queue_head_t open_wait;
271 wait_queue_head_t delta_msr_wait;
272};
273
274struct mxser_board {
275 unsigned int idx;
276 int irq;
277 const struct mxser_cardinfo *info;
278 unsigned long vector;
279 unsigned long vector_mask;
280
281 int chip_flag;
282 int uart_type;
283
284 struct mxser_port ports[MXSER_PORTS_PER_BOARD];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285};
286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287struct mxser_mstatus {
288 tcflag_t cflag;
289 int cts;
290 int dsr;
291 int ri;
292 int dcd;
293};
294
295static struct mxser_mstatus GMStatus[MXSER_PORTS];
296
297static int mxserBoardCAP[MXSER_BOARDS] = {
298 0, 0, 0, 0
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700299 /* 0x180, 0x280, 0x200, 0x320 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300};
301
Jiri Slaby1c456072008-02-07 00:16:46 -0800302static struct mxser_board mxser_boards[MXSER_BOARDS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303static struct tty_driver *mxvar_sdriver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304static struct mxser_log mxvar_log;
305static int mxvar_diagflag;
306static unsigned char mxser_msr[MXSER_PORTS + 1];
307static struct mxser_mon_ext mon_data_ext;
308static int mxser_set_baud_method[MXSER_PORTS + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Jesper Juhlb8cc5542007-10-18 03:06:03 -0700310#ifdef CONFIG_PCI
Jiri Slaby1c456072008-02-07 00:16:46 -0800311static int __devinit CheckIsMoxaMust(unsigned long io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312{
313 u8 oldmcr, hwid;
314 int i;
315
316 outb(0, io + UART_LCR);
317 DISABLE_MOXA_MUST_ENCHANCE_MODE(io);
318 oldmcr = inb(io + UART_MCR);
319 outb(0, io + UART_MCR);
320 SET_MOXA_MUST_XON1_VALUE(io, 0x11);
321 if ((hwid = inb(io + UART_MCR)) != 0) {
322 outb(oldmcr, io + UART_MCR);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700323 return MOXA_OTHER_UART;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 }
325
326 GET_MOXA_MUST_HARDWARE_ID(io, &hwid);
Jiri Slaby1c456072008-02-07 00:16:46 -0800327 for (i = 1; i < UART_INFO_NUM; i++) { /* 0 = OTHER_UART */
328 if (hwid == Gpci_uart_info[i].type)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700329 return (int)hwid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 }
331 return MOXA_OTHER_UART;
332}
Jesper Juhlb8cc5542007-10-18 03:06:03 -0700333#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Jiri Slaby1c456072008-02-07 00:16:46 -0800335static void process_txrx_fifo(struct mxser_port *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
337 int i;
338
339 if ((info->type == PORT_16450) || (info->type == PORT_8250)) {
340 info->rx_trigger = 1;
341 info->rx_high_water = 1;
342 info->rx_low_water = 1;
343 info->xmit_fifo_size = 1;
Jiri Slaby1c456072008-02-07 00:16:46 -0800344 } else
345 for (i = 0; i < UART_INFO_NUM; i++)
346 if (info->board->chip_flag == Gpci_uart_info[i].type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 info->rx_trigger = Gpci_uart_info[i].rx_trigger;
348 info->rx_low_water = Gpci_uart_info[i].rx_low_water;
349 info->rx_high_water = Gpci_uart_info[i].rx_high_water;
350 info->xmit_fifo_size = Gpci_uart_info[i].xmit_fifo_size;
351 break;
352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353}
354
Jiri Slaby1c456072008-02-07 00:16:46 -0800355static unsigned char mxser_get_msr(int baseaddr, int mode, int port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356{
357 unsigned char status = 0;
358
359 status = inb(baseaddr + UART_MSR);
360
361 mxser_msr[port] &= 0x0F;
362 mxser_msr[port] |= status;
363 status = mxser_msr[port];
364 if (mode)
365 mxser_msr[port] = 0;
366
367 return status;
368}
369
Jiri Slaby1c456072008-02-07 00:16:46 -0800370static int mxser_block_til_ready(struct tty_struct *tty, struct file *filp,
371 struct mxser_port *port)
372{
373 DECLARE_WAITQUEUE(wait, current);
374 int retval;
375 int do_clocal = 0;
376 unsigned long flags;
377
378 /*
379 * If non-blocking mode is set, or the port is not enabled,
380 * then make the check up front and then exit.
381 */
382 if ((filp->f_flags & O_NONBLOCK) ||
383 test_bit(TTY_IO_ERROR, &tty->flags)) {
384 port->flags |= ASYNC_NORMAL_ACTIVE;
385 return 0;
386 }
387
388 if (tty->termios->c_cflag & CLOCAL)
389 do_clocal = 1;
390
391 /*
392 * Block waiting for the carrier detect and the line to become
393 * free (i.e., not in use by the callout). While we are in
394 * this loop, port->count is dropped by one, so that
395 * mxser_close() knows when to free things. We restore it upon
396 * exit, either normal or abnormal.
397 */
398 retval = 0;
399 add_wait_queue(&port->open_wait, &wait);
400
401 spin_lock_irqsave(&port->slock, flags);
402 if (!tty_hung_up_p(filp))
403 port->count--;
404 spin_unlock_irqrestore(&port->slock, flags);
405 port->blocked_open++;
406 while (1) {
407 spin_lock_irqsave(&port->slock, flags);
408 outb(inb(port->ioaddr + UART_MCR) |
409 UART_MCR_DTR | UART_MCR_RTS, port->ioaddr + UART_MCR);
410 spin_unlock_irqrestore(&port->slock, flags);
411 set_current_state(TASK_INTERRUPTIBLE);
412 if (tty_hung_up_p(filp) || !(port->flags & ASYNC_INITIALIZED)) {
413 if (port->flags & ASYNC_HUP_NOTIFY)
414 retval = -EAGAIN;
415 else
416 retval = -ERESTARTSYS;
417 break;
418 }
419 if (!(port->flags & ASYNC_CLOSING) &&
420 (do_clocal ||
421 (inb(port->ioaddr + UART_MSR) & UART_MSR_DCD)))
422 break;
423 if (signal_pending(current)) {
424 retval = -ERESTARTSYS;
425 break;
426 }
427 schedule();
428 }
429 set_current_state(TASK_RUNNING);
430 remove_wait_queue(&port->open_wait, &wait);
431 if (!tty_hung_up_p(filp))
432 port->count++;
433 port->blocked_open--;
434 if (retval)
435 return retval;
436 port->flags |= ASYNC_NORMAL_ACTIVE;
437 return 0;
438}
439
440static int mxser_set_baud(struct mxser_port *info, long newspd)
441{
442 int quot = 0, baud;
443 unsigned char cval;
444
445 if (!info->tty || !info->tty->termios)
446 return -1;
447
448 if (!(info->ioaddr))
449 return -1;
450
451 if (newspd > info->max_baud)
452 return -1;
453
454 if (newspd == 134) {
455 quot = 2 * info->baud_base / 269;
456 tty_encode_baud_rate(info->tty, 134, 134);
457 } else if (newspd) {
458 quot = info->baud_base / newspd;
459 if (quot == 0)
460 quot = 1;
461 baud = info->baud_base/quot;
462 tty_encode_baud_rate(info->tty, baud, baud);
463 } else {
464 quot = 0;
465 }
466
467 info->timeout = ((info->xmit_fifo_size * HZ * 10 * quot) / info->baud_base);
468 info->timeout += HZ / 50; /* Add .02 seconds of slop */
469
470 if (quot) {
471 info->MCR |= UART_MCR_DTR;
472 outb(info->MCR, info->ioaddr + UART_MCR);
473 } else {
474 info->MCR &= ~UART_MCR_DTR;
475 outb(info->MCR, info->ioaddr + UART_MCR);
476 return 0;
477 }
478
479 cval = inb(info->ioaddr + UART_LCR);
480
481 outb(cval | UART_LCR_DLAB, info->ioaddr + UART_LCR); /* set DLAB */
482
483 outb(quot & 0xff, info->ioaddr + UART_DLL); /* LS of divisor */
484 outb(quot >> 8, info->ioaddr + UART_DLM); /* MS of divisor */
485 outb(cval, info->ioaddr + UART_LCR); /* reset DLAB */
486
487#ifdef BOTHER
488 if (C_BAUD(info->tty) == BOTHER) {
489 quot = info->baud_base % newspd;
490 quot *= 8;
491 if (quot % newspd > newspd / 2) {
492 quot /= newspd;
493 quot++;
494 } else
495 quot /= newspd;
496
497 SET_MOXA_MUST_ENUM_VALUE(info->ioaddr, quot);
498 } else
499#endif
500 SET_MOXA_MUST_ENUM_VALUE(info->ioaddr, 0);
501
502 return 0;
503}
504
505/*
506 * This routine is called to set the UART divisor registers to match
507 * the specified baud rate for a serial port.
508 */
509static int mxser_change_speed(struct mxser_port *info,
510 struct ktermios *old_termios)
511{
512 unsigned cflag, cval, fcr;
513 int ret = 0;
514 unsigned char status;
515
516 if (!info->tty || !info->tty->termios)
517 return ret;
518 cflag = info->tty->termios->c_cflag;
519 if (!(info->ioaddr))
520 return ret;
521
522 if (mxser_set_baud_method[info->tty->index] == 0)
523 mxser_set_baud(info, tty_get_baud_rate(info->tty));
524
525 /* byte size and parity */
526 switch (cflag & CSIZE) {
527 case CS5:
528 cval = 0x00;
529 break;
530 case CS6:
531 cval = 0x01;
532 break;
533 case CS7:
534 cval = 0x02;
535 break;
536 case CS8:
537 cval = 0x03;
538 break;
539 default:
540 cval = 0x00;
541 break; /* too keep GCC shut... */
542 }
543 if (cflag & CSTOPB)
544 cval |= 0x04;
545 if (cflag & PARENB)
546 cval |= UART_LCR_PARITY;
547 if (!(cflag & PARODD))
548 cval |= UART_LCR_EPAR;
549 if (cflag & CMSPAR)
550 cval |= UART_LCR_SPAR;
551
552 if ((info->type == PORT_8250) || (info->type == PORT_16450)) {
553 if (info->board->chip_flag) {
554 fcr = UART_FCR_ENABLE_FIFO;
555 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
556 SET_MOXA_MUST_FIFO_VALUE(info);
557 } else
558 fcr = 0;
559 } else {
560 fcr = UART_FCR_ENABLE_FIFO;
561 if (info->board->chip_flag) {
562 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
563 SET_MOXA_MUST_FIFO_VALUE(info);
564 } else {
565 switch (info->rx_trigger) {
566 case 1:
567 fcr |= UART_FCR_TRIGGER_1;
568 break;
569 case 4:
570 fcr |= UART_FCR_TRIGGER_4;
571 break;
572 case 8:
573 fcr |= UART_FCR_TRIGGER_8;
574 break;
575 default:
576 fcr |= UART_FCR_TRIGGER_14;
577 break;
578 }
579 }
580 }
581
582 /* CTS flow control flag and modem status interrupts */
583 info->IER &= ~UART_IER_MSI;
584 info->MCR &= ~UART_MCR_AFE;
585 if (cflag & CRTSCTS) {
586 info->flags |= ASYNC_CTS_FLOW;
587 info->IER |= UART_IER_MSI;
588 if ((info->type == PORT_16550A) || (info->board->chip_flag)) {
589 info->MCR |= UART_MCR_AFE;
590 } else {
591 status = inb(info->ioaddr + UART_MSR);
592 if (info->tty->hw_stopped) {
593 if (status & UART_MSR_CTS) {
594 info->tty->hw_stopped = 0;
595 if (info->type != PORT_16550A &&
596 !info->board->chip_flag) {
597 outb(info->IER & ~UART_IER_THRI,
598 info->ioaddr +
599 UART_IER);
600 info->IER |= UART_IER_THRI;
601 outb(info->IER, info->ioaddr +
602 UART_IER);
603 }
604 tty_wakeup(info->tty);
605 }
606 } else {
607 if (!(status & UART_MSR_CTS)) {
608 info->tty->hw_stopped = 1;
609 if ((info->type != PORT_16550A) &&
610 (!info->board->chip_flag)) {
611 info->IER &= ~UART_IER_THRI;
612 outb(info->IER, info->ioaddr +
613 UART_IER);
614 }
615 }
616 }
617 }
618 } else {
619 info->flags &= ~ASYNC_CTS_FLOW;
620 }
621 outb(info->MCR, info->ioaddr + UART_MCR);
622 if (cflag & CLOCAL) {
623 info->flags &= ~ASYNC_CHECK_CD;
624 } else {
625 info->flags |= ASYNC_CHECK_CD;
626 info->IER |= UART_IER_MSI;
627 }
628 outb(info->IER, info->ioaddr + UART_IER);
629
630 /*
631 * Set up parity check flag
632 */
633 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
634 if (I_INPCK(info->tty))
635 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
636 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
637 info->read_status_mask |= UART_LSR_BI;
638
639 info->ignore_status_mask = 0;
640
641 if (I_IGNBRK(info->tty)) {
642 info->ignore_status_mask |= UART_LSR_BI;
643 info->read_status_mask |= UART_LSR_BI;
644 /*
645 * If we're ignore parity and break indicators, ignore
646 * overruns too. (For real raw support).
647 */
648 if (I_IGNPAR(info->tty)) {
649 info->ignore_status_mask |=
650 UART_LSR_OE |
651 UART_LSR_PE |
652 UART_LSR_FE;
653 info->read_status_mask |=
654 UART_LSR_OE |
655 UART_LSR_PE |
656 UART_LSR_FE;
657 }
658 }
659 if (info->board->chip_flag) {
660 SET_MOXA_MUST_XON1_VALUE(info->ioaddr, START_CHAR(info->tty));
661 SET_MOXA_MUST_XOFF1_VALUE(info->ioaddr, STOP_CHAR(info->tty));
662 if (I_IXON(info->tty)) {
663 ENABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
664 } else {
665 DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
666 }
667 if (I_IXOFF(info->tty)) {
668 ENABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
669 } else {
670 DISABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
671 }
672 }
673
674
675 outb(fcr, info->ioaddr + UART_FCR); /* set fcr */
676 outb(cval, info->ioaddr + UART_LCR);
677
678 return ret;
679}
680
681static void mxser_check_modem_status(struct mxser_port *port, int status)
682{
683 /* update input line counters */
684 if (status & UART_MSR_TERI)
685 port->icount.rng++;
686 if (status & UART_MSR_DDSR)
687 port->icount.dsr++;
688 if (status & UART_MSR_DDCD)
689 port->icount.dcd++;
690 if (status & UART_MSR_DCTS)
691 port->icount.cts++;
692 port->mon_data.modem_status = status;
693 wake_up_interruptible(&port->delta_msr_wait);
694
695 if ((port->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
696 if (status & UART_MSR_DCD)
697 wake_up_interruptible(&port->open_wait);
698 }
699
700 if (port->flags & ASYNC_CTS_FLOW) {
701 if (port->tty->hw_stopped) {
702 if (status & UART_MSR_CTS) {
703 port->tty->hw_stopped = 0;
704
705 if ((port->type != PORT_16550A) &&
706 (!port->board->chip_flag)) {
707 outb(port->IER & ~UART_IER_THRI,
708 port->ioaddr + UART_IER);
709 port->IER |= UART_IER_THRI;
710 outb(port->IER, port->ioaddr +
711 UART_IER);
712 }
713 tty_wakeup(port->tty);
714 }
715 } else {
716 if (!(status & UART_MSR_CTS)) {
717 port->tty->hw_stopped = 1;
718 if (port->type != PORT_16550A &&
719 !port->board->chip_flag) {
720 port->IER &= ~UART_IER_THRI;
721 outb(port->IER, port->ioaddr +
722 UART_IER);
723 }
724 }
725 }
726 }
727}
728
729static int mxser_startup(struct mxser_port *info)
730{
731 unsigned long page;
732 unsigned long flags;
733
734 page = __get_free_page(GFP_KERNEL);
735 if (!page)
736 return -ENOMEM;
737
738 spin_lock_irqsave(&info->slock, flags);
739
740 if (info->flags & ASYNC_INITIALIZED) {
741 free_page(page);
742 spin_unlock_irqrestore(&info->slock, flags);
743 return 0;
744 }
745
746 if (!info->ioaddr || !info->type) {
747 if (info->tty)
748 set_bit(TTY_IO_ERROR, &info->tty->flags);
749 free_page(page);
750 spin_unlock_irqrestore(&info->slock, flags);
751 return 0;
752 }
753 if (info->xmit_buf)
754 free_page(page);
755 else
756 info->xmit_buf = (unsigned char *) page;
757
758 /*
759 * Clear the FIFO buffers and disable them
760 * (they will be reenabled in mxser_change_speed())
761 */
762 if (info->board->chip_flag)
763 outb((UART_FCR_CLEAR_RCVR |
764 UART_FCR_CLEAR_XMIT |
765 MOXA_MUST_FCR_GDA_MODE_ENABLE), info->ioaddr + UART_FCR);
766 else
767 outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
768 info->ioaddr + UART_FCR);
769
770 /*
771 * At this point there's no way the LSR could still be 0xFF;
772 * if it is, then bail out, because there's likely no UART
773 * here.
774 */
775 if (inb(info->ioaddr + UART_LSR) == 0xff) {
776 spin_unlock_irqrestore(&info->slock, flags);
777 if (capable(CAP_SYS_ADMIN)) {
778 if (info->tty)
779 set_bit(TTY_IO_ERROR, &info->tty->flags);
780 return 0;
781 } else
782 return -ENODEV;
783 }
784
785 /*
786 * Clear the interrupt registers.
787 */
788 (void) inb(info->ioaddr + UART_LSR);
789 (void) inb(info->ioaddr + UART_RX);
790 (void) inb(info->ioaddr + UART_IIR);
791 (void) inb(info->ioaddr + UART_MSR);
792
793 /*
794 * Now, initialize the UART
795 */
796 outb(UART_LCR_WLEN8, info->ioaddr + UART_LCR); /* reset DLAB */
797 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
798 outb(info->MCR, info->ioaddr + UART_MCR);
799
800 /*
801 * Finally, enable interrupts
802 */
803 info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
804
805 if (info->board->chip_flag)
806 info->IER |= MOXA_MUST_IER_EGDAI;
807 outb(info->IER, info->ioaddr + UART_IER); /* enable interrupts */
808
809 /*
810 * And clear the interrupt registers again for luck.
811 */
812 (void) inb(info->ioaddr + UART_LSR);
813 (void) inb(info->ioaddr + UART_RX);
814 (void) inb(info->ioaddr + UART_IIR);
815 (void) inb(info->ioaddr + UART_MSR);
816
817 if (info->tty)
818 clear_bit(TTY_IO_ERROR, &info->tty->flags);
819 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
820
821 /*
822 * and set the speed of the serial port
823 */
824 mxser_change_speed(info, NULL);
825 info->flags |= ASYNC_INITIALIZED;
826 spin_unlock_irqrestore(&info->slock, flags);
827
828 return 0;
829}
830
831/*
832 * This routine will shutdown a serial port; interrupts maybe disabled, and
833 * DTR is dropped if the hangup on close termio flag is on.
834 */
835static void mxser_shutdown(struct mxser_port *info)
836{
837 unsigned long flags;
838
839 if (!(info->flags & ASYNC_INITIALIZED))
840 return;
841
842 spin_lock_irqsave(&info->slock, flags);
843
844 /*
845 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
846 * here so the queue might never be waken up
847 */
848 wake_up_interruptible(&info->delta_msr_wait);
849
850 /*
851 * Free the IRQ, if necessary
852 */
853 if (info->xmit_buf) {
854 free_page((unsigned long) info->xmit_buf);
855 info->xmit_buf = NULL;
856 }
857
858 info->IER = 0;
859 outb(0x00, info->ioaddr + UART_IER);
860
861 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
862 info->MCR &= ~(UART_MCR_DTR | UART_MCR_RTS);
863 outb(info->MCR, info->ioaddr + UART_MCR);
864
865 /* clear Rx/Tx FIFO's */
866 if (info->board->chip_flag)
867 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT |
868 MOXA_MUST_FCR_GDA_MODE_ENABLE,
869 info->ioaddr + UART_FCR);
870 else
871 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
872 info->ioaddr + UART_FCR);
873
874 /* read data port to reset things */
875 (void) inb(info->ioaddr + UART_RX);
876
877 if (info->tty)
878 set_bit(TTY_IO_ERROR, &info->tty->flags);
879
880 info->flags &= ~ASYNC_INITIALIZED;
881
882 if (info->board->chip_flag)
883 SET_MOXA_MUST_NO_SOFTWARE_FLOW_CONTROL(info->ioaddr);
884
885 spin_unlock_irqrestore(&info->slock, flags);
886}
887
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888/*
889 * This routine is called whenever a serial port is opened. It
890 * enables interrupts for a serial port, linking in its async structure into
891 * the IRQ chain. It also performs the serial-specific
892 * initialization for the tty structure.
893 */
894static int mxser_open(struct tty_struct *tty, struct file *filp)
895{
Jiri Slaby1c456072008-02-07 00:16:46 -0800896 struct mxser_port *info;
897 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 int retval, line;
899
900 line = tty->index;
901 if (line == MXSER_PORTS)
902 return 0;
903 if (line < 0 || line > MXSER_PORTS)
904 return -ENODEV;
Jiri Slaby1c456072008-02-07 00:16:46 -0800905 info = &mxser_boards[line / MXSER_PORTS_PER_BOARD].ports[line % MXSER_PORTS_PER_BOARD];
906 if (!info->ioaddr)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700907 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
909 tty->driver_data = info;
910 info->tty = tty;
911 /*
912 * Start up serial port
913 */
Jiri Slaby1c456072008-02-07 00:16:46 -0800914 spin_lock_irqsave(&info->slock, flags);
915 info->count++;
916 spin_unlock_irqrestore(&info->slock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 retval = mxser_startup(info);
918 if (retval)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700919 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
921 retval = mxser_block_til_ready(tty, filp, info);
922 if (retval)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700923 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
Cedric Le Goater8cddd702007-02-10 01:46:33 -0800925 /* unmark here for very high baud rate (ex. 921600 bps) used */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 tty->low_latency = 1;
927 return 0;
928}
929
Alan Cox978e5952008-04-30 00:53:59 -0700930static void mxser_flush_buffer(struct tty_struct *tty)
931{
932 struct mxser_port *info = tty->driver_data;
933 char fcr;
934 unsigned long flags;
935
936
937 spin_lock_irqsave(&info->slock, flags);
938 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
939
940 fcr = inb(info->ioaddr + UART_FCR);
941 outb((fcr | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
942 info->ioaddr + UART_FCR);
943 outb(fcr, info->ioaddr + UART_FCR);
944
945 spin_unlock_irqrestore(&info->slock, flags);
946
947 tty_wakeup(tty);
948}
949
950
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951/*
952 * This routine is called when the serial port gets closed. First, we
953 * wait for the last remaining data to be sent. Then, we unlink its
954 * async structure from the interrupt chain if necessary, and we free
955 * that IRQ if nothing is left in the chain.
956 */
957static void mxser_close(struct tty_struct *tty, struct file *filp)
958{
Jiri Slaby1c456072008-02-07 00:16:46 -0800959 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
961 unsigned long timeout;
962 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
964 if (tty->index == MXSER_PORTS)
965 return;
966 if (!info)
Kirill Smelkov6f08b722005-11-07 00:59:23 -0800967 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
969 spin_lock_irqsave(&info->slock, flags);
970
971 if (tty_hung_up_p(filp)) {
972 spin_unlock_irqrestore(&info->slock, flags);
973 return;
974 }
975 if ((tty->count == 1) && (info->count != 1)) {
976 /*
977 * Uh, oh. tty->count is 1, which means that the tty
978 * structure will be freed. Info->count should always
979 * be one in these conditions. If it's greater than
980 * one, we've got real problems, since it means the
981 * serial port won't be shutdown.
982 */
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700983 printk(KERN_ERR "mxser_close: bad serial port count; "
984 "tty->count is 1, info->count is %d\n", info->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 info->count = 1;
986 }
987 if (--info->count < 0) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700988 printk(KERN_ERR "mxser_close: bad serial port count for "
Jiri Slaby1c456072008-02-07 00:16:46 -0800989 "ttys%d: %d\n", tty->index, info->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 info->count = 0;
991 }
992 if (info->count) {
993 spin_unlock_irqrestore(&info->slock, flags);
994 return;
995 }
996 info->flags |= ASYNC_CLOSING;
997 spin_unlock_irqrestore(&info->slock, flags);
998 /*
999 * Save the termios structure, since this port may have
1000 * separate termios for callout and dialin.
1001 */
1002 if (info->flags & ASYNC_NORMAL_ACTIVE)
1003 info->normal_termios = *tty->termios;
1004 /*
1005 * Now we wait for the transmit buffer to clear; and we notify
1006 * the line discipline to only process XON/XOFF characters.
1007 */
1008 tty->closing = 1;
1009 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1010 tty_wait_until_sent(tty, info->closing_wait);
1011 /*
1012 * At this point we stop accepting input. To do this, we
1013 * disable the receive line status interrupts, and tell the
1014 * interrupt driver to stop checking the data ready bit in the
1015 * line status register.
1016 */
1017 info->IER &= ~UART_IER_RLSI;
Jiri Slaby1c456072008-02-07 00:16:46 -08001018 if (info->board->chip_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 info->IER &= ~MOXA_MUST_RECV_ISR;
Jiri Slaby1c456072008-02-07 00:16:46 -08001020
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 if (info->flags & ASYNC_INITIALIZED) {
Jiri Slaby1c456072008-02-07 00:16:46 -08001022 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 /*
1024 * Before we drop DTR, make sure the UART transmitter
1025 * has completely drained; this is especially
1026 * important if there is a transmit FIFO!
1027 */
1028 timeout = jiffies + HZ;
Jiri Slaby1c456072008-02-07 00:16:46 -08001029 while (!(inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT)) {
Nishanth Aravamudanda4cd8d2005-09-10 00:27:30 -07001030 schedule_timeout_interruptible(5);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 if (time_after(jiffies, timeout))
1032 break;
1033 }
1034 }
1035 mxser_shutdown(info);
1036
Alan Cox978e5952008-04-30 00:53:59 -07001037 mxser_flush_buffer(tty);
Jiri Slaby1c456072008-02-07 00:16:46 -08001038 tty_ldisc_flush(tty);
1039
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 tty->closing = 0;
1041 info->event = 0;
1042 info->tty = NULL;
1043 if (info->blocked_open) {
Nishanth Aravamudanda4cd8d2005-09-10 00:27:30 -07001044 if (info->close_delay)
1045 schedule_timeout_interruptible(info->close_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 wake_up_interruptible(&info->open_wait);
1047 }
1048
1049 info->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050}
1051
1052static int mxser_write(struct tty_struct *tty, const unsigned char *buf, int count)
1053{
1054 int c, total = 0;
Jiri Slaby1c456072008-02-07 00:16:46 -08001055 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 unsigned long flags;
1057
Jesper Juhl8a7f7c92006-06-25 05:47:53 -07001058 if (!info->xmit_buf)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001059 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
1061 while (1) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001062 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1063 SERIAL_XMIT_SIZE - info->xmit_head));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 if (c <= 0)
1065 break;
1066
1067 memcpy(info->xmit_buf + info->xmit_head, buf, c);
1068 spin_lock_irqsave(&info->slock, flags);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001069 info->xmit_head = (info->xmit_head + c) &
1070 (SERIAL_XMIT_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 info->xmit_cnt += c;
1072 spin_unlock_irqrestore(&info->slock, flags);
1073
1074 buf += c;
1075 count -= c;
1076 total += c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 }
1078
Jiri Slaby1c456072008-02-07 00:16:46 -08001079 if (info->xmit_cnt && !tty->stopped) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001080 if (!tty->hw_stopped ||
1081 (info->type == PORT_16550A) ||
Jiri Slaby1c456072008-02-07 00:16:46 -08001082 (info->board->chip_flag)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby1c456072008-02-07 00:16:46 -08001084 outb(info->IER & ~UART_IER_THRI, info->ioaddr +
1085 UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 info->IER |= UART_IER_THRI;
Jiri Slaby1c456072008-02-07 00:16:46 -08001087 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 spin_unlock_irqrestore(&info->slock, flags);
1089 }
1090 }
1091 return total;
1092}
1093
1094static void mxser_put_char(struct tty_struct *tty, unsigned char ch)
1095{
Jiri Slaby1c456072008-02-07 00:16:46 -08001096 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 unsigned long flags;
1098
Jesper Juhl8a7f7c92006-06-25 05:47:53 -07001099 if (!info->xmit_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 return;
1101
1102 if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1)
1103 return;
1104
1105 spin_lock_irqsave(&info->slock, flags);
1106 info->xmit_buf[info->xmit_head++] = ch;
1107 info->xmit_head &= SERIAL_XMIT_SIZE - 1;
1108 info->xmit_cnt++;
1109 spin_unlock_irqrestore(&info->slock, flags);
Jiri Slaby1c456072008-02-07 00:16:46 -08001110 if (!tty->stopped) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001111 if (!tty->hw_stopped ||
1112 (info->type == PORT_16550A) ||
Jiri Slaby1c456072008-02-07 00:16:46 -08001113 info->board->chip_flag) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby1c456072008-02-07 00:16:46 -08001115 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 info->IER |= UART_IER_THRI;
Jiri Slaby1c456072008-02-07 00:16:46 -08001117 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 spin_unlock_irqrestore(&info->slock, flags);
1119 }
1120 }
1121}
1122
1123
1124static void mxser_flush_chars(struct tty_struct *tty)
1125{
Jiri Slaby1c456072008-02-07 00:16:46 -08001126 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 unsigned long flags;
1128
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001129 if (info->xmit_cnt <= 0 ||
1130 tty->stopped ||
1131 !info->xmit_buf ||
1132 (tty->hw_stopped &&
1133 (info->type != PORT_16550A) &&
Jiri Slaby1c456072008-02-07 00:16:46 -08001134 (!info->board->chip_flag)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001135 ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 return;
1137
1138 spin_lock_irqsave(&info->slock, flags);
1139
Jiri Slaby1c456072008-02-07 00:16:46 -08001140 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 info->IER |= UART_IER_THRI;
Jiri Slaby1c456072008-02-07 00:16:46 -08001142 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
1144 spin_unlock_irqrestore(&info->slock, flags);
1145}
1146
1147static int mxser_write_room(struct tty_struct *tty)
1148{
Jiri Slaby1c456072008-02-07 00:16:46 -08001149 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 int ret;
1151
1152 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1153 if (ret < 0)
1154 ret = 0;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001155 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156}
1157
1158static int mxser_chars_in_buffer(struct tty_struct *tty)
1159{
Jiri Slaby1c456072008-02-07 00:16:46 -08001160 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 return info->xmit_cnt;
1162}
1163
Jiri Slaby1c456072008-02-07 00:16:46 -08001164/*
1165 * ------------------------------------------------------------
1166 * friends of mxser_ioctl()
1167 * ------------------------------------------------------------
1168 */
1169static int mxser_get_serial_info(struct mxser_port *info,
1170 struct serial_struct __user *retinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171{
Jiri Slaby1c456072008-02-07 00:16:46 -08001172 struct serial_struct tmp = {
1173 .type = info->type,
1174 .line = info->tty->index,
1175 .port = info->ioaddr,
1176 .irq = info->board->irq,
1177 .flags = info->flags,
1178 .baud_base = info->baud_base,
1179 .close_delay = info->close_delay,
1180 .closing_wait = info->closing_wait,
1181 .custom_divisor = info->custom_divisor,
1182 .hub6 = 0
1183 };
1184 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1185 return -EFAULT;
1186 return 0;
1187}
1188
1189static int mxser_set_serial_info(struct mxser_port *info,
1190 struct serial_struct __user *new_info)
1191{
1192 struct serial_struct new_serial;
Jiri Slaby80ff8a82008-02-07 00:16:51 -08001193 speed_t baud;
Jiri Slaby1c456072008-02-07 00:16:46 -08001194 unsigned long sl_flags;
1195 unsigned int flags;
1196 int retval = 0;
1197
1198 if (!new_info || !info->ioaddr)
Jiri Slaby80ff8a82008-02-07 00:16:51 -08001199 return -ENODEV;
Jiri Slaby1c456072008-02-07 00:16:46 -08001200 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
1201 return -EFAULT;
1202
Jiri Slaby80ff8a82008-02-07 00:16:51 -08001203 if (new_serial.irq != info->board->irq ||
1204 new_serial.port != info->ioaddr)
1205 return -EINVAL;
Jiri Slaby1c456072008-02-07 00:16:46 -08001206
1207 flags = info->flags & ASYNC_SPD_MASK;
1208
1209 if (!capable(CAP_SYS_ADMIN)) {
1210 if ((new_serial.baud_base != info->baud_base) ||
1211 (new_serial.close_delay != info->close_delay) ||
1212 ((new_serial.flags & ~ASYNC_USR_MASK) != (info->flags & ~ASYNC_USR_MASK)))
1213 return -EPERM;
1214 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
1215 (new_serial.flags & ASYNC_USR_MASK));
1216 } else {
1217 /*
1218 * OK, past this point, all the error checking has been done.
1219 * At this point, we start making changes.....
1220 */
1221 info->flags = ((info->flags & ~ASYNC_FLAGS) |
1222 (new_serial.flags & ASYNC_FLAGS));
1223 info->close_delay = new_serial.close_delay * HZ / 100;
1224 info->closing_wait = new_serial.closing_wait * HZ / 100;
1225 info->tty->low_latency =
1226 (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1227 info->tty->low_latency = 0;
Jiri Slaby80ff8a82008-02-07 00:16:51 -08001228 if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST &&
1229 (new_serial.baud_base != info->baud_base ||
1230 new_serial.custom_divisor !=
1231 info->custom_divisor)) {
1232 baud = new_serial.baud_base / new_serial.custom_divisor;
1233 tty_encode_baud_rate(info->tty, baud, baud);
1234 }
Jiri Slaby1c456072008-02-07 00:16:46 -08001235 }
1236
1237 info->type = new_serial.type;
1238
1239 process_txrx_fifo(info);
1240
1241 if (info->flags & ASYNC_INITIALIZED) {
1242 if (flags != (info->flags & ASYNC_SPD_MASK)) {
1243 spin_lock_irqsave(&info->slock, sl_flags);
1244 mxser_change_speed(info, NULL);
1245 spin_unlock_irqrestore(&info->slock, sl_flags);
1246 }
1247 } else
1248 retval = mxser_startup(info);
1249
1250 return retval;
1251}
1252
1253/*
1254 * mxser_get_lsr_info - get line status register info
1255 *
1256 * Purpose: Let user call ioctl() to get info when the UART physically
1257 * is emptied. On bus types like RS485, the transmitter must
1258 * release the bus after transmitting. This must be done when
1259 * the transmit shift register is empty, not be done when the
1260 * transmit holding register is empty. This functionality
1261 * allows an RS485 driver to be written in user space.
1262 */
1263static int mxser_get_lsr_info(struct mxser_port *info,
1264 unsigned int __user *value)
1265{
1266 unsigned char status;
1267 unsigned int result;
1268 unsigned long flags;
1269
1270 spin_lock_irqsave(&info->slock, flags);
1271 status = inb(info->ioaddr + UART_LSR);
1272 spin_unlock_irqrestore(&info->slock, flags);
1273 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1274 return put_user(result, value);
1275}
1276
1277/*
1278 * This routine sends a break character out the serial port.
1279 */
1280static void mxser_send_break(struct mxser_port *info, int duration)
1281{
1282 unsigned long flags;
1283
1284 if (!info->ioaddr)
1285 return;
1286 set_current_state(TASK_INTERRUPTIBLE);
1287 spin_lock_irqsave(&info->slock, flags);
1288 outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
1289 info->ioaddr + UART_LCR);
1290 spin_unlock_irqrestore(&info->slock, flags);
1291 schedule_timeout(duration);
1292 spin_lock_irqsave(&info->slock, flags);
1293 outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
1294 info->ioaddr + UART_LCR);
1295 spin_unlock_irqrestore(&info->slock, flags);
1296}
1297
1298static int mxser_tiocmget(struct tty_struct *tty, struct file *file)
1299{
1300 struct mxser_port *info = tty->driver_data;
1301 unsigned char control, status;
1302 unsigned long flags;
1303
1304
1305 if (tty->index == MXSER_PORTS)
1306 return -ENOIOCTLCMD;
1307 if (test_bit(TTY_IO_ERROR, &tty->flags))
1308 return -EIO;
1309
1310 control = info->MCR;
1311
1312 spin_lock_irqsave(&info->slock, flags);
1313 status = inb(info->ioaddr + UART_MSR);
1314 if (status & UART_MSR_ANY_DELTA)
1315 mxser_check_modem_status(info, status);
1316 spin_unlock_irqrestore(&info->slock, flags);
1317 return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0) |
1318 ((control & UART_MCR_DTR) ? TIOCM_DTR : 0) |
1319 ((status & UART_MSR_DCD) ? TIOCM_CAR : 0) |
1320 ((status & UART_MSR_RI) ? TIOCM_RNG : 0) |
1321 ((status & UART_MSR_DSR) ? TIOCM_DSR : 0) |
1322 ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
1323}
1324
1325static int mxser_tiocmset(struct tty_struct *tty, struct file *file,
1326 unsigned int set, unsigned int clear)
1327{
1328 struct mxser_port *info = tty->driver_data;
1329 unsigned long flags;
1330
1331
1332 if (tty->index == MXSER_PORTS)
1333 return -ENOIOCTLCMD;
1334 if (test_bit(TTY_IO_ERROR, &tty->flags))
1335 return -EIO;
1336
1337 spin_lock_irqsave(&info->slock, flags);
1338
1339 if (set & TIOCM_RTS)
1340 info->MCR |= UART_MCR_RTS;
1341 if (set & TIOCM_DTR)
1342 info->MCR |= UART_MCR_DTR;
1343
1344 if (clear & TIOCM_RTS)
1345 info->MCR &= ~UART_MCR_RTS;
1346 if (clear & TIOCM_DTR)
1347 info->MCR &= ~UART_MCR_DTR;
1348
1349 outb(info->MCR, info->ioaddr + UART_MCR);
1350 spin_unlock_irqrestore(&info->slock, flags);
1351 return 0;
1352}
1353
1354static int __init mxser_program_mode(int port)
1355{
1356 int id, i, j, n;
1357
1358 outb(0, port);
1359 outb(0, port);
1360 outb(0, port);
1361 (void)inb(port);
1362 (void)inb(port);
1363 outb(0, port);
1364 (void)inb(port);
1365
1366 id = inb(port + 1) & 0x1F;
1367 if ((id != C168_ASIC_ID) &&
1368 (id != C104_ASIC_ID) &&
1369 (id != C102_ASIC_ID) &&
1370 (id != CI132_ASIC_ID) &&
1371 (id != CI134_ASIC_ID) &&
1372 (id != CI104J_ASIC_ID))
1373 return -1;
1374 for (i = 0, j = 0; i < 4; i++) {
1375 n = inb(port + 2);
1376 if (n == 'M') {
1377 j = 1;
1378 } else if ((j == 1) && (n == 1)) {
1379 j = 2;
1380 break;
1381 } else
1382 j = 0;
1383 }
1384 if (j != 2)
1385 id = -2;
1386 return id;
1387}
1388
1389static void __init mxser_normal_mode(int port)
1390{
1391 int i, n;
1392
1393 outb(0xA5, port + 1);
1394 outb(0x80, port + 3);
1395 outb(12, port + 0); /* 9600 bps */
1396 outb(0, port + 1);
1397 outb(0x03, port + 3); /* 8 data bits */
1398 outb(0x13, port + 4); /* loop back mode */
1399 for (i = 0; i < 16; i++) {
1400 n = inb(port + 5);
1401 if ((n & 0x61) == 0x60)
1402 break;
1403 if ((n & 1) == 1)
1404 (void)inb(port);
1405 }
1406 outb(0x00, port + 4);
1407}
1408
1409#define CHIP_SK 0x01 /* Serial Data Clock in Eprom */
1410#define CHIP_DO 0x02 /* Serial Data Output in Eprom */
1411#define CHIP_CS 0x04 /* Serial Chip Select in Eprom */
1412#define CHIP_DI 0x08 /* Serial Data Input in Eprom */
1413#define EN_CCMD 0x000 /* Chip's command register */
1414#define EN0_RSARLO 0x008 /* Remote start address reg 0 */
1415#define EN0_RSARHI 0x009 /* Remote start address reg 1 */
1416#define EN0_RCNTLO 0x00A /* Remote byte count reg WR */
1417#define EN0_RCNTHI 0x00B /* Remote byte count reg WR */
1418#define EN0_DCFG 0x00E /* Data configuration reg WR */
1419#define EN0_PORT 0x010 /* Rcv missed frame error counter RD */
1420#define ENC_PAGE0 0x000 /* Select page 0 of chip registers */
1421#define ENC_PAGE3 0x0C0 /* Select page 3 of chip registers */
1422static int __init mxser_read_register(int port, unsigned short *regs)
1423{
1424 int i, k, value, id;
1425 unsigned int j;
1426
1427 id = mxser_program_mode(port);
1428 if (id < 0)
1429 return id;
1430 for (i = 0; i < 14; i++) {
1431 k = (i & 0x3F) | 0x180;
1432 for (j = 0x100; j > 0; j >>= 1) {
1433 outb(CHIP_CS, port);
1434 if (k & j) {
1435 outb(CHIP_CS | CHIP_DO, port);
1436 outb(CHIP_CS | CHIP_DO | CHIP_SK, port); /* A? bit of read */
1437 } else {
1438 outb(CHIP_CS, port);
1439 outb(CHIP_CS | CHIP_SK, port); /* A? bit of read */
1440 }
1441 }
1442 (void)inb(port);
1443 value = 0;
1444 for (k = 0, j = 0x8000; k < 16; k++, j >>= 1) {
1445 outb(CHIP_CS, port);
1446 outb(CHIP_CS | CHIP_SK, port);
1447 if (inb(port) & CHIP_DI)
1448 value |= j;
1449 }
1450 regs[i] = value;
1451 outb(0, port);
1452 }
1453 mxser_normal_mode(port);
1454 return id;
1455}
1456
1457static int mxser_ioctl_special(unsigned int cmd, void __user *argp)
1458{
1459 struct mxser_port *port;
1460 int result, status;
1461 unsigned int i, j;
Alan Cox9d6d1622008-04-30 00:53:20 -07001462 int ret = 0;
Jiri Slaby1c456072008-02-07 00:16:46 -08001463
1464 switch (cmd) {
1465 case MOXA_GET_MAJOR:
1466 return put_user(ttymajor, (int __user *)argp);
1467
1468 case MOXA_CHKPORTENABLE:
1469 result = 0;
Alan Cox9d6d1622008-04-30 00:53:20 -07001470 lock_kernel();
Jiri Slaby1c456072008-02-07 00:16:46 -08001471 for (i = 0; i < MXSER_BOARDS; i++)
1472 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++)
1473 if (mxser_boards[i].ports[j].ioaddr)
1474 result |= (1 << i);
Alan Cox9d6d1622008-04-30 00:53:20 -07001475 unlock_kernel();
Jiri Slaby1c456072008-02-07 00:16:46 -08001476 return put_user(result, (unsigned long __user *)argp);
1477 case MOXA_GETDATACOUNT:
Alan Cox9d6d1622008-04-30 00:53:20 -07001478 lock_kernel();
Jiri Slaby1c456072008-02-07 00:16:46 -08001479 if (copy_to_user(argp, &mxvar_log, sizeof(mxvar_log)))
Alan Cox9d6d1622008-04-30 00:53:20 -07001480 ret = -EFAULT;
1481 unlock_kernel();
1482 return ret;
Jiri Slaby1c456072008-02-07 00:16:46 -08001483 case MOXA_GETMSTATUS:
Alan Cox9d6d1622008-04-30 00:53:20 -07001484 lock_kernel();
Jiri Slaby1c456072008-02-07 00:16:46 -08001485 for (i = 0; i < MXSER_BOARDS; i++)
1486 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
1487 port = &mxser_boards[i].ports[j];
1488
1489 GMStatus[i].ri = 0;
1490 if (!port->ioaddr) {
1491 GMStatus[i].dcd = 0;
1492 GMStatus[i].dsr = 0;
1493 GMStatus[i].cts = 0;
1494 continue;
1495 }
1496
1497 if (!port->tty || !port->tty->termios)
1498 GMStatus[i].cflag =
1499 port->normal_termios.c_cflag;
1500 else
1501 GMStatus[i].cflag =
1502 port->tty->termios->c_cflag;
1503
1504 status = inb(port->ioaddr + UART_MSR);
1505 if (status & 0x80 /*UART_MSR_DCD */ )
1506 GMStatus[i].dcd = 1;
1507 else
1508 GMStatus[i].dcd = 0;
1509
1510 if (status & 0x20 /*UART_MSR_DSR */ )
1511 GMStatus[i].dsr = 1;
1512 else
1513 GMStatus[i].dsr = 0;
1514
1515
1516 if (status & 0x10 /*UART_MSR_CTS */ )
1517 GMStatus[i].cts = 1;
1518 else
1519 GMStatus[i].cts = 0;
1520 }
Alan Cox9d6d1622008-04-30 00:53:20 -07001521 unlock_kernel();
Jiri Slaby1c456072008-02-07 00:16:46 -08001522 if (copy_to_user(argp, GMStatus,
1523 sizeof(struct mxser_mstatus) * MXSER_PORTS))
1524 return -EFAULT;
1525 return 0;
1526 case MOXA_ASPP_MON_EXT: {
1527 int p, shiftbit;
1528 unsigned long opmode;
1529 unsigned cflag, iflag;
1530
Alan Cox9d6d1622008-04-30 00:53:20 -07001531 lock_kernel();
1532 for (i = 0; i < MXSER_BOARDS; i++) {
Jiri Slaby1c456072008-02-07 00:16:46 -08001533 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
1534 port = &mxser_boards[i].ports[j];
1535 if (!port->ioaddr)
1536 continue;
1537
1538 status = mxser_get_msr(port->ioaddr, 0, i);
1539
1540 if (status & UART_MSR_TERI)
1541 port->icount.rng++;
1542 if (status & UART_MSR_DDSR)
1543 port->icount.dsr++;
1544 if (status & UART_MSR_DDCD)
1545 port->icount.dcd++;
1546 if (status & UART_MSR_DCTS)
1547 port->icount.cts++;
1548
1549 port->mon_data.modem_status = status;
1550 mon_data_ext.rx_cnt[i] = port->mon_data.rxcnt;
1551 mon_data_ext.tx_cnt[i] = port->mon_data.txcnt;
1552 mon_data_ext.up_rxcnt[i] =
1553 port->mon_data.up_rxcnt;
1554 mon_data_ext.up_txcnt[i] =
1555 port->mon_data.up_txcnt;
1556 mon_data_ext.modem_status[i] =
1557 port->mon_data.modem_status;
1558 mon_data_ext.baudrate[i] =
1559 tty_get_baud_rate(port->tty);
1560
1561 if (!port->tty || !port->tty->termios) {
1562 cflag = port->normal_termios.c_cflag;
1563 iflag = port->normal_termios.c_iflag;
1564 } else {
1565 cflag = port->tty->termios->c_cflag;
1566 iflag = port->tty->termios->c_iflag;
1567 }
1568
1569 mon_data_ext.databits[i] = cflag & CSIZE;
1570
1571 mon_data_ext.stopbits[i] = cflag & CSTOPB;
1572
1573 mon_data_ext.parity[i] =
1574 cflag & (PARENB | PARODD | CMSPAR);
1575
1576 mon_data_ext.flowctrl[i] = 0x00;
1577
1578 if (cflag & CRTSCTS)
1579 mon_data_ext.flowctrl[i] |= 0x03;
1580
1581 if (iflag & (IXON | IXOFF))
1582 mon_data_ext.flowctrl[i] |= 0x0C;
1583
1584 if (port->type == PORT_16550A)
1585 mon_data_ext.fifo[i] = 1;
1586 else
1587 mon_data_ext.fifo[i] = 0;
1588
1589 p = i % 4;
1590 shiftbit = p * 2;
1591 opmode = inb(port->opmode_ioaddr) >> shiftbit;
1592 opmode &= OP_MODE_MASK;
1593
1594 mon_data_ext.iftype[i] = opmode;
1595
1596 }
Alan Cox9d6d1622008-04-30 00:53:20 -07001597 }
1598 unlock_kernel();
1599 if (copy_to_user(argp, &mon_data_ext,
1600 sizeof(mon_data_ext)))
1601 return -EFAULT;
1602 return 0;
1603 }
1604 default:
Jiri Slaby1c456072008-02-07 00:16:46 -08001605 return -ENOIOCTLCMD;
1606 }
1607 return 0;
1608}
1609
1610static int mxser_cflags_changed(struct mxser_port *info, unsigned long arg,
1611 struct async_icount *cprev)
1612{
1613 struct async_icount cnow;
1614 unsigned long flags;
1615 int ret;
1616
1617 spin_lock_irqsave(&info->slock, flags);
1618 cnow = info->icount; /* atomic copy */
1619 spin_unlock_irqrestore(&info->slock, flags);
1620
1621 ret = ((arg & TIOCM_RNG) && (cnow.rng != cprev->rng)) ||
1622 ((arg & TIOCM_DSR) && (cnow.dsr != cprev->dsr)) ||
1623 ((arg & TIOCM_CD) && (cnow.dcd != cprev->dcd)) ||
1624 ((arg & TIOCM_CTS) && (cnow.cts != cprev->cts));
1625
1626 *cprev = cnow;
1627
1628 return ret;
1629}
1630
1631static int mxser_ioctl(struct tty_struct *tty, struct file *file,
1632 unsigned int cmd, unsigned long arg)
1633{
1634 struct mxser_port *info = tty->driver_data;
1635 struct async_icount cnow;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 struct serial_icounter_struct __user *p_cuser;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 unsigned long flags;
1638 void __user *argp = (void __user *)arg;
Jiri Slaby1c456072008-02-07 00:16:46 -08001639 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640
1641 if (tty->index == MXSER_PORTS)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001642 return mxser_ioctl_special(cmd, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 if (cmd == MOXA_SET_OP_MODE || cmd == MOXA_GET_OP_MODE) {
Jiri Slaby1c456072008-02-07 00:16:46 -08001645 int p;
1646 unsigned long opmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 static unsigned char ModeMask[] = { 0xfc, 0xf3, 0xcf, 0x3f };
1648 int shiftbit;
1649 unsigned char val, mask;
1650
Jiri Slaby1c456072008-02-07 00:16:46 -08001651 p = tty->index % 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 if (cmd == MOXA_SET_OP_MODE) {
1653 if (get_user(opmode, (int __user *) argp))
1654 return -EFAULT;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001655 if (opmode != RS232_MODE &&
1656 opmode != RS485_2WIRE_MODE &&
1657 opmode != RS422_MODE &&
1658 opmode != RS485_4WIRE_MODE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 return -EFAULT;
Alan Cox9d6d1622008-04-30 00:53:20 -07001660 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 mask = ModeMask[p];
1662 shiftbit = p * 2;
1663 val = inb(info->opmode_ioaddr);
1664 val &= mask;
1665 val |= (opmode << shiftbit);
1666 outb(val, info->opmode_ioaddr);
Alan Cox9d6d1622008-04-30 00:53:20 -07001667 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 } else {
Alan Cox9d6d1622008-04-30 00:53:20 -07001669 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 shiftbit = p * 2;
1671 opmode = inb(info->opmode_ioaddr) >> shiftbit;
1672 opmode &= OP_MODE_MASK;
Alan Cox9d6d1622008-04-30 00:53:20 -07001673 unlock_kernel();
Jiri Slaby1c456072008-02-07 00:16:46 -08001674 if (put_user(opmode, (int __user *)argp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 return -EFAULT;
1676 }
1677 return 0;
1678 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
Jiri Slaby1c456072008-02-07 00:16:46 -08001680 if (cmd != TIOCGSERIAL && cmd != TIOCMIWAIT && cmd != TIOCGICOUNT &&
1681 test_bit(TTY_IO_ERROR, &tty->flags))
1682 return -EIO;
1683
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 switch (cmd) {
1685 case TCSBRK: /* SVID version: non-zero arg --> no break */
1686 retval = tty_check_change(tty);
1687 if (retval)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001688 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 tty_wait_until_sent(tty, 0);
1690 if (!arg)
1691 mxser_send_break(info, HZ / 4); /* 1/4 second */
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001692 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 case TCSBRKP: /* support for POSIX tcsendbreak() */
1694 retval = tty_check_change(tty);
1695 if (retval)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001696 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 tty_wait_until_sent(tty, 0);
1698 mxser_send_break(info, arg ? arg * (HZ / 10) : HZ / 4);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001699 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 case TIOCGSERIAL:
Alan Cox9d6d1622008-04-30 00:53:20 -07001701 lock_kernel();
1702 retval = mxser_get_serial_info(info, argp);
1703 unlock_kernel();
1704 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 case TIOCSSERIAL:
Alan Cox9d6d1622008-04-30 00:53:20 -07001706 lock_kernel();
1707 retval = mxser_set_serial_info(info, argp);
1708 unlock_kernel();
1709 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 case TIOCSERGETLSR: /* Get line status register */
Alan Cox9d6d1622008-04-30 00:53:20 -07001711 return mxser_get_lsr_info(info, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 /*
1713 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1714 * - mask passed in arg for lines of interest
1715 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1716 * Caller should use TIOCGICOUNT to see which one it was
1717 */
Jiri Slabyfc838152007-04-23 14:41:04 -07001718 case TIOCMIWAIT:
1719 spin_lock_irqsave(&info->slock, flags);
1720 cnow = info->icount; /* note the counters on entry */
1721 spin_unlock_irqrestore(&info->slock, flags);
1722
Jiri Slaby1c456072008-02-07 00:16:46 -08001723 return wait_event_interruptible(info->delta_msr_wait,
1724 mxser_cflags_changed(info, arg, &cnow));
1725 /*
1726 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1727 * Return: write counters to the user passed counter struct
1728 * NB: both 1->0 and 0->1 transitions are counted except for
1729 * RI where only 0->1 is counted.
1730 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 case TIOCGICOUNT:
1732 spin_lock_irqsave(&info->slock, flags);
1733 cnow = info->icount;
1734 spin_unlock_irqrestore(&info->slock, flags);
1735 p_cuser = argp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 if (put_user(cnow.frame, &p_cuser->frame))
1737 return -EFAULT;
1738 if (put_user(cnow.brk, &p_cuser->brk))
1739 return -EFAULT;
1740 if (put_user(cnow.overrun, &p_cuser->overrun))
1741 return -EFAULT;
1742 if (put_user(cnow.buf_overrun, &p_cuser->buf_overrun))
1743 return -EFAULT;
1744 if (put_user(cnow.parity, &p_cuser->parity))
1745 return -EFAULT;
1746 if (put_user(cnow.rx, &p_cuser->rx))
1747 return -EFAULT;
1748 if (put_user(cnow.tx, &p_cuser->tx))
1749 return -EFAULT;
1750 put_user(cnow.cts, &p_cuser->cts);
1751 put_user(cnow.dsr, &p_cuser->dsr);
1752 put_user(cnow.rng, &p_cuser->rng);
1753 put_user(cnow.dcd, &p_cuser->dcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 return 0;
1755 case MOXA_HighSpeedOn:
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001756 return put_user(info->baud_base != 115200 ? 1 : 0, (int __user *)argp);
Jiri Slaby1c456072008-02-07 00:16:46 -08001757 case MOXA_SDS_RSTICOUNTER:
Alan Cox9d6d1622008-04-30 00:53:20 -07001758 lock_kernel();
Jiri Slaby1c456072008-02-07 00:16:46 -08001759 info->mon_data.rxcnt = 0;
1760 info->mon_data.txcnt = 0;
Alan Cox9d6d1622008-04-30 00:53:20 -07001761 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 return 0;
1763
1764 case MOXA_ASPP_OQUEUE:{
Jiri Slaby1c456072008-02-07 00:16:46 -08001765 int len, lsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766
Alan Cox9d6d1622008-04-30 00:53:20 -07001767 lock_kernel();
Jiri Slaby1c456072008-02-07 00:16:46 -08001768 len = mxser_chars_in_buffer(tty);
Jiri Slaby1c456072008-02-07 00:16:46 -08001769 lsr = inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT;
Jiri Slaby1c456072008-02-07 00:16:46 -08001770 len += (lsr ? 0 : 1);
Alan Cox9d6d1622008-04-30 00:53:20 -07001771 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
Jiri Slaby1c456072008-02-07 00:16:46 -08001773 return put_user(len, (int __user *)argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 }
Jiri Slaby1c456072008-02-07 00:16:46 -08001775 case MOXA_ASPP_MON: {
1776 int mcr, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
Alan Cox9d6d1622008-04-30 00:53:20 -07001778 lock_kernel();
Jiri Slaby1c456072008-02-07 00:16:46 -08001779 status = mxser_get_msr(info->ioaddr, 1, tty->index);
1780 mxser_check_modem_status(info, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
Jiri Slaby1c456072008-02-07 00:16:46 -08001782 mcr = inb(info->ioaddr + UART_MCR);
1783 if (mcr & MOXA_MUST_MCR_XON_FLAG)
1784 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFHOLD;
1785 else
1786 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFHOLD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
Jiri Slaby1c456072008-02-07 00:16:46 -08001788 if (mcr & MOXA_MUST_MCR_TX_XON)
1789 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFXENT;
1790 else
1791 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFXENT;
1792
1793 if (info->tty->hw_stopped)
1794 info->mon_data.hold_reason |= NPPI_NOTIFY_CTSHOLD;
1795 else
1796 info->mon_data.hold_reason &= ~NPPI_NOTIFY_CTSHOLD;
Alan Cox9d6d1622008-04-30 00:53:20 -07001797 unlock_kernel();
Jiri Slaby1c456072008-02-07 00:16:46 -08001798 if (copy_to_user(argp, &info->mon_data,
1799 sizeof(struct mxser_mon)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 return -EFAULT;
Jiri Slaby1c456072008-02-07 00:16:46 -08001801
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 return 0;
Jiri Slaby1c456072008-02-07 00:16:46 -08001803 }
1804 case MOXA_ASPP_LSTATUS: {
1805 if (put_user(info->err_shadow, (unsigned char __user *)argp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
Jiri Slaby1c456072008-02-07 00:16:46 -08001808 info->err_shadow = 0;
1809 return 0;
1810 }
1811 case MOXA_SET_BAUD_METHOD: {
1812 int method;
1813
1814 if (get_user(method, (int __user *)argp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 return -EFAULT;
Jiri Slaby1c456072008-02-07 00:16:46 -08001816 mxser_set_baud_method[tty->index] = method;
1817 return put_user(method, (int __user *)argp);
1818 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 default:
1820 return -ENOIOCTLCMD;
1821 }
1822 return 0;
1823}
1824
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825static void mxser_stoprx(struct tty_struct *tty)
1826{
Jiri Slaby1c456072008-02-07 00:16:46 -08001827 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828
1829 info->ldisc_stop_rx = 1;
1830 if (I_IXOFF(tty)) {
Jiri Slaby1c456072008-02-07 00:16:46 -08001831 if (info->board->chip_flag) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 info->IER &= ~MOXA_MUST_RECV_ISR;
Jiri Slaby1c456072008-02-07 00:16:46 -08001833 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 info->x_char = STOP_CHAR(tty);
Jiri Slaby1c456072008-02-07 00:16:46 -08001836 outb(0, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 info->IER |= UART_IER_THRI;
Jiri Slaby1c456072008-02-07 00:16:46 -08001838 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 }
1840 }
1841
1842 if (info->tty->termios->c_cflag & CRTSCTS) {
Jiri Slaby1c456072008-02-07 00:16:46 -08001843 info->MCR &= ~UART_MCR_RTS;
1844 outb(info->MCR, info->ioaddr + UART_MCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 }
1846}
1847
1848/*
1849 * This routine is called by the upper-layer tty layer to signal that
1850 * incoming characters should be throttled.
1851 */
1852static void mxser_throttle(struct tty_struct *tty)
1853{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 mxser_stoprx(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855}
1856
1857static void mxser_unthrottle(struct tty_struct *tty)
1858{
Jiri Slaby1c456072008-02-07 00:16:46 -08001859 struct mxser_port *info = tty->driver_data;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001860
Jiri Slaby1c456072008-02-07 00:16:46 -08001861 /* startrx */
1862 info->ldisc_stop_rx = 0;
1863 if (I_IXOFF(tty)) {
1864 if (info->x_char)
1865 info->x_char = 0;
1866 else {
1867 if (info->board->chip_flag) {
1868 info->IER |= MOXA_MUST_RECV_ISR;
1869 outb(info->IER, info->ioaddr + UART_IER);
1870 } else {
1871 info->x_char = START_CHAR(tty);
1872 outb(0, info->ioaddr + UART_IER);
1873 info->IER |= UART_IER_THRI;
1874 outb(info->IER, info->ioaddr + UART_IER);
1875 }
1876 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 }
1878
Jiri Slaby1c456072008-02-07 00:16:46 -08001879 if (info->tty->termios->c_cflag & CRTSCTS) {
1880 info->MCR |= UART_MCR_RTS;
1881 outb(info->MCR, info->ioaddr + UART_MCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 }
1883}
1884
1885/*
1886 * mxser_stop() and mxser_start()
1887 *
1888 * This routines are called before setting or resetting tty->stopped.
1889 * They enable or disable transmitter interrupts, as necessary.
1890 */
1891static void mxser_stop(struct tty_struct *tty)
1892{
Jiri Slaby1c456072008-02-07 00:16:46 -08001893 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 unsigned long flags;
1895
1896 spin_lock_irqsave(&info->slock, flags);
1897 if (info->IER & UART_IER_THRI) {
1898 info->IER &= ~UART_IER_THRI;
Jiri Slaby1c456072008-02-07 00:16:46 -08001899 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 }
1901 spin_unlock_irqrestore(&info->slock, flags);
1902}
1903
1904static void mxser_start(struct tty_struct *tty)
1905{
Jiri Slaby1c456072008-02-07 00:16:46 -08001906 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 unsigned long flags;
1908
1909 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby1c456072008-02-07 00:16:46 -08001910 if (info->xmit_cnt && info->xmit_buf) {
1911 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 info->IER |= UART_IER_THRI;
Jiri Slaby1c456072008-02-07 00:16:46 -08001913 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 }
1915 spin_unlock_irqrestore(&info->slock, flags);
1916}
1917
Jiri Slaby1c456072008-02-07 00:16:46 -08001918static void mxser_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
1919{
1920 struct mxser_port *info = tty->driver_data;
1921 unsigned long flags;
1922
1923 spin_lock_irqsave(&info->slock, flags);
1924 mxser_change_speed(info, old_termios);
1925 spin_unlock_irqrestore(&info->slock, flags);
1926
1927 if ((old_termios->c_cflag & CRTSCTS) &&
1928 !(tty->termios->c_cflag & CRTSCTS)) {
1929 tty->hw_stopped = 0;
1930 mxser_start(tty);
1931 }
1932
1933 /* Handle sw stopped */
1934 if ((old_termios->c_iflag & IXON) &&
1935 !(tty->termios->c_iflag & IXON)) {
1936 tty->stopped = 0;
1937
1938 if (info->board->chip_flag) {
1939 spin_lock_irqsave(&info->slock, flags);
1940 DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
1941 spin_unlock_irqrestore(&info->slock, flags);
1942 }
1943
1944 mxser_start(tty);
1945 }
1946}
1947
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948/*
1949 * mxser_wait_until_sent() --- wait until the transmitter is empty
1950 */
1951static void mxser_wait_until_sent(struct tty_struct *tty, int timeout)
1952{
Jiri Slaby1c456072008-02-07 00:16:46 -08001953 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 unsigned long orig_jiffies, char_time;
1955 int lsr;
1956
1957 if (info->type == PORT_UNKNOWN)
1958 return;
1959
1960 if (info->xmit_fifo_size == 0)
1961 return; /* Just in case.... */
1962
1963 orig_jiffies = jiffies;
1964 /*
1965 * Set the check interval to be 1/5 of the estimated time to
1966 * send a single character, and make it at least 1. The check
1967 * interval should also be less than the timeout.
1968 *
1969 * Note: we have to use pretty tight timings here to satisfy
1970 * the NIST-PCTS.
1971 */
1972 char_time = (info->timeout - HZ / 50) / info->xmit_fifo_size;
1973 char_time = char_time / 5;
1974 if (char_time == 0)
1975 char_time = 1;
1976 if (timeout && timeout < char_time)
1977 char_time = timeout;
1978 /*
1979 * If the transmitter hasn't cleared in twice the approximate
1980 * amount of time to send the entire FIFO, it probably won't
1981 * ever clear. This assumes the UART isn't doing flow
1982 * control, which is currently the case. Hence, if it ever
1983 * takes longer than info->timeout, this is probably due to a
1984 * UART bug of some kind. So, we clamp the timeout parameter at
1985 * 2*info->timeout.
1986 */
1987 if (!timeout || timeout > 2 * info->timeout)
1988 timeout = 2 * info->timeout;
1989#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001990 printk(KERN_DEBUG "In rs_wait_until_sent(%d) check=%lu...",
1991 timeout, char_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 printk("jiff=%lu...", jiffies);
1993#endif
Alan Cox978e5952008-04-30 00:53:59 -07001994 lock_kernel();
Jiri Slaby1c456072008-02-07 00:16:46 -08001995 while (!((lsr = inb(info->ioaddr + UART_LSR)) & UART_LSR_TEMT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1997 printk("lsr = %d (jiff=%lu)...", lsr, jiffies);
1998#endif
Nishanth Aravamudanda4cd8d2005-09-10 00:27:30 -07001999 schedule_timeout_interruptible(char_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 if (signal_pending(current))
2001 break;
2002 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2003 break;
2004 }
2005 set_current_state(TASK_RUNNING);
Alan Cox978e5952008-04-30 00:53:59 -07002006 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007
2008#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2009 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
2010#endif
2011}
2012
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013/*
2014 * This routine is called by tty_hangup() when a hangup is signaled.
2015 */
Jiri Slaby1c456072008-02-07 00:16:46 -08002016static void mxser_hangup(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017{
Jiri Slaby1c456072008-02-07 00:16:46 -08002018 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019
2020 mxser_flush_buffer(tty);
2021 mxser_shutdown(info);
2022 info->event = 0;
2023 info->count = 0;
2024 info->flags &= ~ASYNC_NORMAL_ACTIVE;
2025 info->tty = NULL;
2026 wake_up_interruptible(&info->open_wait);
2027}
2028
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029/*
2030 * mxser_rs_break() --- routine which turns the break handling on or off
2031 */
2032static void mxser_rs_break(struct tty_struct *tty, int break_state)
2033{
Jiri Slaby1c456072008-02-07 00:16:46 -08002034 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 unsigned long flags;
2036
2037 spin_lock_irqsave(&info->slock, flags);
2038 if (break_state == -1)
Jiri Slaby1c456072008-02-07 00:16:46 -08002039 outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
2040 info->ioaddr + UART_LCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 else
Jiri Slaby1c456072008-02-07 00:16:46 -08002042 outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
2043 info->ioaddr + UART_LCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 spin_unlock_irqrestore(&info->slock, flags);
2045}
2046
Jiri Slaby1c456072008-02-07 00:16:46 -08002047static void mxser_receive_chars(struct mxser_port *port, int *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048{
Jiri Slaby1c456072008-02-07 00:16:46 -08002049 struct tty_struct *tty = port->tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 unsigned char ch, gdl;
2051 int ignored = 0;
2052 int cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 int recv_room;
2054 int max = 256;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
Alan Cox33f0f882006-01-09 20:54:13 -08002056 recv_room = tty->receive_room;
Jiri Slaby1c456072008-02-07 00:16:46 -08002057 if ((recv_room == 0) && (!port->ldisc_stop_rx))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 mxser_stoprx(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059
Jiri Slaby1c456072008-02-07 00:16:46 -08002060 if (port->board->chip_flag != MOXA_OTHER_UART) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061
Jiri Slaby1c456072008-02-07 00:16:46 -08002062 if (*status & UART_LSR_SPECIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 goto intr_old;
Jiri Slaby1c456072008-02-07 00:16:46 -08002064 if (port->board->chip_flag == MOXA_MUST_MU860_HWID &&
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002065 (*status & MOXA_MUST_LSR_RERR))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 goto intr_old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 if (*status & MOXA_MUST_LSR_RERR)
2068 goto intr_old;
2069
Jiri Slaby1c456072008-02-07 00:16:46 -08002070 gdl = inb(port->ioaddr + MOXA_MUST_GDL_REGISTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071
Jiri Slaby1c456072008-02-07 00:16:46 -08002072 if (port->board->chip_flag == MOXA_MUST_MU150_HWID)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 gdl &= MOXA_MUST_GDL_MASK;
2074 if (gdl >= recv_room) {
Jiri Slaby1c456072008-02-07 00:16:46 -08002075 if (!port->ldisc_stop_rx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 mxser_stoprx(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 }
2078 while (gdl--) {
Jiri Slaby1c456072008-02-07 00:16:46 -08002079 ch = inb(port->ioaddr + UART_RX);
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002080 tty_insert_flip_char(tty, ch, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 }
2083 goto end_intr;
2084 }
Jiri Slaby1c456072008-02-07 00:16:46 -08002085intr_old:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086
2087 do {
2088 if (max-- < 0)
2089 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090
Jiri Slaby1c456072008-02-07 00:16:46 -08002091 ch = inb(port->ioaddr + UART_RX);
2092 if (port->board->chip_flag && (*status & UART_LSR_OE))
2093 outb(0x23, port->ioaddr + UART_FCR);
2094 *status &= port->read_status_mask;
2095 if (*status & port->ignore_status_mask) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 if (++ignored > 100)
2097 break;
2098 } else {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002099 char flag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 if (*status & UART_LSR_SPECIAL) {
2101 if (*status & UART_LSR_BI) {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002102 flag = TTY_BREAK;
Jiri Slaby1c456072008-02-07 00:16:46 -08002103 port->icount.brk++;
2104
2105 if (port->flags & ASYNC_SAK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 do_SAK(tty);
2107 } else if (*status & UART_LSR_PE) {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002108 flag = TTY_PARITY;
Jiri Slaby1c456072008-02-07 00:16:46 -08002109 port->icount.parity++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 } else if (*status & UART_LSR_FE) {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002111 flag = TTY_FRAME;
Jiri Slaby1c456072008-02-07 00:16:46 -08002112 port->icount.frame++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 } else if (*status & UART_LSR_OE) {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002114 flag = TTY_OVERRUN;
Jiri Slaby1c456072008-02-07 00:16:46 -08002115 port->icount.overrun++;
2116 } else
2117 flag = TTY_BREAK;
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002118 }
2119 tty_insert_flip_char(tty, ch, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 cnt++;
2121 if (cnt >= recv_room) {
Jiri Slaby1c456072008-02-07 00:16:46 -08002122 if (!port->ldisc_stop_rx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 mxser_stoprx(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 break;
2125 }
2126
2127 }
2128
Jiri Slaby1c456072008-02-07 00:16:46 -08002129 if (port->board->chip_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131
Jiri Slaby1c456072008-02-07 00:16:46 -08002132 *status = inb(port->ioaddr + UART_LSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 } while (*status & UART_LSR_DR);
2134
Jiri Slaby1c456072008-02-07 00:16:46 -08002135end_intr:
2136 mxvar_log.rxcnt[port->tty->index] += cnt;
2137 port->mon_data.rxcnt += cnt;
2138 port->mon_data.up_rxcnt += cnt;
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002139
Jiri Slaby1c456072008-02-07 00:16:46 -08002140 /*
2141 * We are called from an interrupt context with &port->slock
2142 * being held. Drop it temporarily in order to prevent
2143 * recursive locking.
2144 */
2145 spin_unlock(&port->slock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 tty_flip_buffer_push(tty);
Jiri Slaby1c456072008-02-07 00:16:46 -08002147 spin_lock(&port->slock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148}
2149
Jiri Slaby1c456072008-02-07 00:16:46 -08002150static void mxser_transmit_chars(struct mxser_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151{
2152 int count, cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153
Jiri Slaby1c456072008-02-07 00:16:46 -08002154 if (port->x_char) {
2155 outb(port->x_char, port->ioaddr + UART_TX);
2156 port->x_char = 0;
2157 mxvar_log.txcnt[port->tty->index]++;
2158 port->mon_data.txcnt++;
2159 port->mon_data.up_txcnt++;
2160 port->icount.tx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 return;
2162 }
2163
Jiri Slaby1c456072008-02-07 00:16:46 -08002164 if (port->xmit_buf == NULL)
2165 return;
2166
2167 if ((port->xmit_cnt <= 0) || port->tty->stopped ||
2168 (port->tty->hw_stopped &&
2169 (port->type != PORT_16550A) &&
2170 (!port->board->chip_flag))) {
2171 port->IER &= ~UART_IER_THRI;
2172 outb(port->IER, port->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 return;
2174 }
2175
Jiri Slaby1c456072008-02-07 00:16:46 -08002176 cnt = port->xmit_cnt;
2177 count = port->xmit_fifo_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 do {
Jiri Slaby1c456072008-02-07 00:16:46 -08002179 outb(port->xmit_buf[port->xmit_tail++],
2180 port->ioaddr + UART_TX);
2181 port->xmit_tail = port->xmit_tail & (SERIAL_XMIT_SIZE - 1);
2182 if (--port->xmit_cnt <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 break;
2184 } while (--count > 0);
Jiri Slaby1c456072008-02-07 00:16:46 -08002185 mxvar_log.txcnt[port->tty->index] += (cnt - port->xmit_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186
Jiri Slaby1c456072008-02-07 00:16:46 -08002187 port->mon_data.txcnt += (cnt - port->xmit_cnt);
2188 port->mon_data.up_txcnt += (cnt - port->xmit_cnt);
2189 port->icount.tx += (cnt - port->xmit_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190
Jiri Slaby1c456072008-02-07 00:16:46 -08002191 if (port->xmit_cnt < WAKEUP_CHARS)
2192 tty_wakeup(port->tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193
Jiri Slaby1c456072008-02-07 00:16:46 -08002194 if (port->xmit_cnt <= 0) {
2195 port->IER &= ~UART_IER_THRI;
2196 outb(port->IER, port->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198}
2199
2200/*
Jiri Slaby1c456072008-02-07 00:16:46 -08002201 * This is the serial driver's generic interrupt routine
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 */
Jiri Slaby1c456072008-02-07 00:16:46 -08002203static irqreturn_t mxser_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204{
Jiri Slaby1c456072008-02-07 00:16:46 -08002205 int status, iir, i;
2206 struct mxser_board *brd = NULL;
2207 struct mxser_port *port;
2208 int max, irqbits, bits, msr;
2209 unsigned int int_cnt, pass_counter = 0;
2210 int handled = IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211
Jiri Slaby1c456072008-02-07 00:16:46 -08002212 for (i = 0; i < MXSER_BOARDS; i++)
2213 if (dev_id == &mxser_boards[i]) {
2214 brd = dev_id;
2215 break;
2216 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217
Jiri Slaby1c456072008-02-07 00:16:46 -08002218 if (i == MXSER_BOARDS)
2219 goto irq_stop;
2220 if (brd == NULL)
2221 goto irq_stop;
2222 max = brd->info->nports;
2223 while (pass_counter++ < MXSER_ISR_PASS_LIMIT) {
2224 irqbits = inb(brd->vector) & brd->vector_mask;
2225 if (irqbits == brd->vector_mask)
2226 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227
Jiri Slaby1c456072008-02-07 00:16:46 -08002228 handled = IRQ_HANDLED;
2229 for (i = 0, bits = 1; i < max; i++, irqbits |= bits, bits <<= 1) {
2230 if (irqbits == brd->vector_mask)
2231 break;
2232 if (bits & irqbits)
2233 continue;
2234 port = &brd->ports[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235
Jiri Slaby1c456072008-02-07 00:16:46 -08002236 int_cnt = 0;
2237 spin_lock(&port->slock);
2238 do {
2239 iir = inb(port->ioaddr + UART_IIR);
2240 if (iir & UART_IIR_NO_INT)
2241 break;
2242 iir &= MOXA_MUST_IIR_MASK;
2243 if (!port->tty ||
2244 (port->flags & ASYNC_CLOSING) ||
2245 !(port->flags &
2246 ASYNC_INITIALIZED)) {
2247 status = inb(port->ioaddr + UART_LSR);
2248 outb(0x27, port->ioaddr + UART_FCR);
2249 inb(port->ioaddr + UART_MSR);
2250 break;
2251 }
2252
2253 status = inb(port->ioaddr + UART_LSR);
2254
2255 if (status & UART_LSR_PE)
2256 port->err_shadow |= NPPI_NOTIFY_PARITY;
2257 if (status & UART_LSR_FE)
2258 port->err_shadow |= NPPI_NOTIFY_FRAMING;
2259 if (status & UART_LSR_OE)
2260 port->err_shadow |=
2261 NPPI_NOTIFY_HW_OVERRUN;
2262 if (status & UART_LSR_BI)
2263 port->err_shadow |= NPPI_NOTIFY_BREAK;
2264
2265 if (port->board->chip_flag) {
2266 if (iir == MOXA_MUST_IIR_GDA ||
2267 iir == MOXA_MUST_IIR_RDA ||
2268 iir == MOXA_MUST_IIR_RTO ||
2269 iir == MOXA_MUST_IIR_LSR)
2270 mxser_receive_chars(port,
2271 &status);
2272
2273 } else {
2274 status &= port->read_status_mask;
2275 if (status & UART_LSR_DR)
2276 mxser_receive_chars(port,
2277 &status);
2278 }
2279 msr = inb(port->ioaddr + UART_MSR);
2280 if (msr & UART_MSR_ANY_DELTA)
2281 mxser_check_modem_status(port, msr);
2282
2283 if (port->board->chip_flag) {
2284 if (iir == 0x02 && (status &
2285 UART_LSR_THRE))
2286 mxser_transmit_chars(port);
2287 } else {
2288 if (status & UART_LSR_THRE)
2289 mxser_transmit_chars(port);
2290 }
2291 } while (int_cnt++ < MXSER_ISR_PASS_LIMIT);
2292 spin_unlock(&port->slock);
2293 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 }
2295
Jiri Slaby1c456072008-02-07 00:16:46 -08002296irq_stop:
2297 return handled;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298}
2299
Jiri Slaby1c456072008-02-07 00:16:46 -08002300static const struct tty_operations mxser_ops = {
2301 .open = mxser_open,
2302 .close = mxser_close,
2303 .write = mxser_write,
2304 .put_char = mxser_put_char,
2305 .flush_chars = mxser_flush_chars,
2306 .write_room = mxser_write_room,
2307 .chars_in_buffer = mxser_chars_in_buffer,
2308 .flush_buffer = mxser_flush_buffer,
2309 .ioctl = mxser_ioctl,
2310 .throttle = mxser_throttle,
2311 .unthrottle = mxser_unthrottle,
2312 .set_termios = mxser_set_termios,
2313 .stop = mxser_stop,
2314 .start = mxser_start,
2315 .hangup = mxser_hangup,
2316 .break_ctl = mxser_rs_break,
2317 .wait_until_sent = mxser_wait_until_sent,
2318 .tiocmget = mxser_tiocmget,
2319 .tiocmset = mxser_tiocmset,
2320};
2321
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322/*
Jiri Slaby1c456072008-02-07 00:16:46 -08002323 * The MOXA Smartio/Industio serial driver boot-time initialization code!
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 */
Jiri Slaby1c456072008-02-07 00:16:46 -08002325
2326static void mxser_release_res(struct mxser_board *brd, struct pci_dev *pdev,
2327 unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328{
Jiri Slaby1c456072008-02-07 00:16:46 -08002329 if (irq)
2330 free_irq(brd->irq, brd);
2331 if (pdev != NULL) { /* PCI */
2332#ifdef CONFIG_PCI
2333 pci_release_region(pdev, 2);
2334 pci_release_region(pdev, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 } else {
Jiri Slaby1c456072008-02-07 00:16:46 -08002337 release_region(brd->ports[0].ioaddr, 8 * brd->info->nports);
2338 release_region(brd->vector, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340}
2341
Jiri Slaby1c456072008-02-07 00:16:46 -08002342static int __devinit mxser_initbrd(struct mxser_board *brd,
2343 struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344{
Jiri Slaby1c456072008-02-07 00:16:46 -08002345 struct mxser_port *info;
2346 unsigned int i;
2347 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348
Jiri Slaby1c456072008-02-07 00:16:46 -08002349 printk(KERN_INFO "max. baud rate = %d bps.\n", brd->ports[0].max_baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350
Jiri Slaby1c456072008-02-07 00:16:46 -08002351 for (i = 0; i < brd->info->nports; i++) {
2352 info = &brd->ports[i];
2353 info->board = brd;
2354 info->stop_rx = 0;
2355 info->ldisc_stop_rx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356
Jiri Slaby1c456072008-02-07 00:16:46 -08002357 /* Enhance mode enabled here */
2358 if (brd->chip_flag != MOXA_OTHER_UART)
2359 ENABLE_MOXA_MUST_ENCHANCE_MODE(info->ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360
Jiri Slaby1c456072008-02-07 00:16:46 -08002361 info->flags = ASYNC_SHARE_IRQ;
2362 info->type = brd->uart_type;
2363
2364 process_txrx_fifo(info);
2365
2366 info->custom_divisor = info->baud_base * 16;
2367 info->close_delay = 5 * HZ / 10;
2368 info->closing_wait = 30 * HZ;
2369 info->normal_termios = mxvar_sdriver->init_termios;
2370 init_waitqueue_head(&info->open_wait);
2371 init_waitqueue_head(&info->delta_msr_wait);
2372 memset(&info->mon_data, 0, sizeof(struct mxser_mon));
2373 info->err_shadow = 0;
2374 spin_lock_init(&info->slock);
2375
2376 /* before set INT ISR, disable all int */
2377 outb(inb(info->ioaddr + UART_IER) & 0xf0,
2378 info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 }
2380
Jiri Slaby1c456072008-02-07 00:16:46 -08002381 retval = request_irq(brd->irq, mxser_interrupt, IRQF_SHARED, "mxser",
2382 brd);
2383 if (retval) {
2384 printk(KERN_ERR "Board %s: Request irq failed, IRQ (%d) may "
2385 "conflict with another device.\n",
2386 brd->info->name, brd->irq);
2387 /* We hold resources, we need to release them. */
2388 mxser_release_res(brd, pdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002390 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391}
2392
Jiri Slaby1c456072008-02-07 00:16:46 -08002393static int __init mxser_get_ISA_conf(int cap, struct mxser_board *brd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394{
2395 int id, i, bits;
2396 unsigned short regs[16], irq;
2397 unsigned char scratch, scratch2;
2398
Jiri Slaby1c456072008-02-07 00:16:46 -08002399 brd->chip_flag = MOXA_OTHER_UART;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400
2401 id = mxser_read_register(cap, regs);
Jiri Slaby1c456072008-02-07 00:16:46 -08002402 switch (id) {
2403 case C168_ASIC_ID:
2404 brd->info = &mxser_cards[0];
2405 break;
2406 case C104_ASIC_ID:
2407 brd->info = &mxser_cards[1];
2408 break;
2409 case CI104J_ASIC_ID:
2410 brd->info = &mxser_cards[2];
2411 break;
2412 case C102_ASIC_ID:
2413 brd->info = &mxser_cards[5];
2414 break;
2415 case CI132_ASIC_ID:
2416 brd->info = &mxser_cards[6];
2417 break;
2418 case CI134_ASIC_ID:
2419 brd->info = &mxser_cards[7];
2420 break;
2421 default:
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002422 return 0;
Jiri Slaby1c456072008-02-07 00:16:46 -08002423 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424
2425 irq = 0;
Jiri Slaby1c456072008-02-07 00:16:46 -08002426 /* some ISA cards have 2 ports, but we want to see them as 4-port (why?)
2427 Flag-hack checks if configuration should be read as 2-port here. */
2428 if (brd->info->nports == 2 || (brd->info->flags & MXSER_HAS2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 irq = regs[9] & 0xF000;
2430 irq = irq | (irq >> 4);
2431 if (irq != (regs[9] & 0xFF00))
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002432 return MXSER_ERR_IRQ_CONFLIT;
Jiri Slaby1c456072008-02-07 00:16:46 -08002433 } else if (brd->info->nports == 4) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 irq = regs[9] & 0xF000;
2435 irq = irq | (irq >> 4);
2436 irq = irq | (irq >> 8);
2437 if (irq != regs[9])
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002438 return MXSER_ERR_IRQ_CONFLIT;
Jiri Slaby1c456072008-02-07 00:16:46 -08002439 } else if (brd->info->nports == 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 irq = regs[9] & 0xF000;
2441 irq = irq | (irq >> 4);
2442 irq = irq | (irq >> 8);
2443 if ((irq != regs[9]) || (irq != regs[10]))
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002444 return MXSER_ERR_IRQ_CONFLIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 }
2446
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002447 if (!irq)
2448 return MXSER_ERR_IRQ;
Jiri Slaby1c456072008-02-07 00:16:46 -08002449 brd->irq = ((int)(irq & 0xF000) >> 12);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 for (i = 0; i < 8; i++)
Jiri Slaby1c456072008-02-07 00:16:46 -08002451 brd->ports[i].ioaddr = (int) regs[i + 1] & 0xFFF8;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002452 if ((regs[12] & 0x80) == 0)
2453 return MXSER_ERR_VECTOR;
Jiri Slaby1c456072008-02-07 00:16:46 -08002454 brd->vector = (int)regs[11]; /* interrupt vector */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 if (id == 1)
Jiri Slaby1c456072008-02-07 00:16:46 -08002456 brd->vector_mask = 0x00FF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 else
Jiri Slaby1c456072008-02-07 00:16:46 -08002458 brd->vector_mask = 0x000F;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 for (i = 7, bits = 0x0100; i >= 0; i--, bits <<= 1) {
2460 if (regs[12] & bits) {
Jiri Slaby1c456072008-02-07 00:16:46 -08002461 brd->ports[i].baud_base = 921600;
2462 brd->ports[i].max_baud = 921600;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 } else {
Jiri Slaby1c456072008-02-07 00:16:46 -08002464 brd->ports[i].baud_base = 115200;
2465 brd->ports[i].max_baud = 115200;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 }
2467 }
2468 scratch2 = inb(cap + UART_LCR) & (~UART_LCR_DLAB);
2469 outb(scratch2 | UART_LCR_DLAB, cap + UART_LCR);
2470 outb(0, cap + UART_EFR); /* EFR is the same as FCR */
2471 outb(scratch2, cap + UART_LCR);
2472 outb(UART_FCR_ENABLE_FIFO, cap + UART_FCR);
2473 scratch = inb(cap + UART_IIR);
2474
2475 if (scratch & 0xC0)
Jiri Slaby1c456072008-02-07 00:16:46 -08002476 brd->uart_type = PORT_16550A;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 else
Jiri Slaby1c456072008-02-07 00:16:46 -08002478 brd->uart_type = PORT_16450;
2479 if (!request_region(brd->ports[0].ioaddr, 8 * brd->info->nports,
2480 "mxser(IO)"))
2481 return MXSER_ERR_IOADDR;
2482 if (!request_region(brd->vector, 1, "mxser(vector)")) {
2483 release_region(brd->ports[0].ioaddr, 8 * brd->info->nports);
2484 return MXSER_ERR_VECTOR;
2485 }
2486 return brd->info->nports;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487}
2488
Jiri Slaby1c456072008-02-07 00:16:46 -08002489static int __devinit mxser_probe(struct pci_dev *pdev,
2490 const struct pci_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491{
Jiri Slaby1c456072008-02-07 00:16:46 -08002492#ifdef CONFIG_PCI
2493 struct mxser_board *brd;
2494 unsigned int i, j;
2495 unsigned long ioaddress;
2496 int retval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497
Jiri Slaby1c456072008-02-07 00:16:46 -08002498 for (i = 0; i < MXSER_BOARDS; i++)
2499 if (mxser_boards[i].info == NULL)
2500 break;
2501
2502 if (i >= MXSER_BOARDS) {
2503 printk(KERN_ERR "Too many Smartio/Industio family boards found "
2504 "(maximum %d), board not configured\n", MXSER_BOARDS);
2505 goto err;
2506 }
2507
2508 brd = &mxser_boards[i];
2509 brd->idx = i * MXSER_PORTS_PER_BOARD;
2510 printk(KERN_INFO "Found MOXA %s board (BusNo=%d, DevNo=%d)\n",
2511 mxser_cards[ent->driver_data].name,
2512 pdev->bus->number, PCI_SLOT(pdev->devfn));
2513
2514 retval = pci_enable_device(pdev);
2515 if (retval) {
2516 printk(KERN_ERR "Moxa SmartI/O PCI enable fail !\n");
2517 goto err;
2518 }
2519
2520 /* io address */
2521 ioaddress = pci_resource_start(pdev, 2);
2522 retval = pci_request_region(pdev, 2, "mxser(IO)");
2523 if (retval)
2524 goto err;
2525
2526 brd->info = &mxser_cards[ent->driver_data];
2527 for (i = 0; i < brd->info->nports; i++)
2528 brd->ports[i].ioaddr = ioaddress + 8 * i;
2529
2530 /* vector */
2531 ioaddress = pci_resource_start(pdev, 3);
2532 retval = pci_request_region(pdev, 3, "mxser(vector)");
2533 if (retval)
2534 goto err_relio;
2535 brd->vector = ioaddress;
2536
2537 /* irq */
2538 brd->irq = pdev->irq;
2539
2540 brd->chip_flag = CheckIsMoxaMust(brd->ports[0].ioaddr);
2541 brd->uart_type = PORT_16550A;
2542 brd->vector_mask = 0;
2543
2544 for (i = 0; i < brd->info->nports; i++) {
2545 for (j = 0; j < UART_INFO_NUM; j++) {
2546 if (Gpci_uart_info[j].type == brd->chip_flag) {
2547 brd->ports[i].max_baud =
2548 Gpci_uart_info[j].max_baud;
2549
2550 /* exception....CP-102 */
2551 if (brd->info->flags & MXSER_HIGHBAUD)
2552 brd->ports[i].max_baud = 921600;
2553 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 }
2555 }
Jiri Slaby1c456072008-02-07 00:16:46 -08002556 }
2557
2558 if (brd->chip_flag == MOXA_MUST_MU860_HWID) {
2559 for (i = 0; i < brd->info->nports; i++) {
2560 if (i < 4)
2561 brd->ports[i].opmode_ioaddr = ioaddress + 4;
2562 else
2563 brd->ports[i].opmode_ioaddr = ioaddress + 0x0c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 }
Jiri Slaby1c456072008-02-07 00:16:46 -08002565 outb(0, ioaddress + 4); /* default set to RS232 mode */
2566 outb(0, ioaddress + 0x0c); /* default set to RS232 mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 }
Jiri Slaby1c456072008-02-07 00:16:46 -08002568
2569 for (i = 0; i < brd->info->nports; i++) {
2570 brd->vector_mask |= (1 << i);
2571 brd->ports[i].baud_base = 921600;
2572 }
2573
2574 /* mxser_initbrd will hook ISR. */
2575 retval = mxser_initbrd(brd, pdev);
2576 if (retval)
2577 goto err_null;
2578
2579 for (i = 0; i < brd->info->nports; i++)
2580 tty_register_device(mxvar_sdriver, brd->idx + i, &pdev->dev);
2581
2582 pci_set_drvdata(pdev, brd);
2583
2584 return 0;
2585err_relio:
2586 pci_release_region(pdev, 2);
2587err_null:
2588 brd->info = NULL;
2589err:
2590 return retval;
2591#else
2592 return -ENODEV;
2593#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594}
2595
Jiri Slaby1c456072008-02-07 00:16:46 -08002596static void __devexit mxser_remove(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597{
Jiri Slaby1c456072008-02-07 00:16:46 -08002598 struct mxser_board *brd = pci_get_drvdata(pdev);
2599 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600
Jiri Slaby1c456072008-02-07 00:16:46 -08002601 for (i = 0; i < brd->info->nports; i++)
2602 tty_unregister_device(mxvar_sdriver, brd->idx + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603
Jiri Slaby1c456072008-02-07 00:16:46 -08002604 mxser_release_res(brd, pdev, 1);
2605 brd->info = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606}
2607
Jiri Slaby1c456072008-02-07 00:16:46 -08002608static struct pci_driver mxser_driver = {
2609 .name = "mxser",
2610 .id_table = mxser_pcibrds,
2611 .probe = mxser_probe,
2612 .remove = __devexit_p(mxser_remove)
2613};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614
Jiri Slaby1c456072008-02-07 00:16:46 -08002615static int __init mxser_module_init(void)
2616{
2617 struct mxser_board *brd;
2618 unsigned long cap;
2619 unsigned int i, m, isaloop;
2620 int retval, b;
2621
2622 pr_debug("Loading module mxser ...\n");
2623
2624 mxvar_sdriver = alloc_tty_driver(MXSER_PORTS + 1);
2625 if (!mxvar_sdriver)
2626 return -ENOMEM;
2627
2628 printk(KERN_INFO "MOXA Smartio/Industio family driver version %s\n",
2629 MXSER_VERSION);
2630
2631 /* Initialize the tty_driver structure */
2632 mxvar_sdriver->owner = THIS_MODULE;
2633 mxvar_sdriver->magic = TTY_DRIVER_MAGIC;
2634 mxvar_sdriver->name = "ttyMI";
2635 mxvar_sdriver->major = ttymajor;
2636 mxvar_sdriver->minor_start = 0;
2637 mxvar_sdriver->num = MXSER_PORTS + 1;
2638 mxvar_sdriver->type = TTY_DRIVER_TYPE_SERIAL;
2639 mxvar_sdriver->subtype = SERIAL_TYPE_NORMAL;
2640 mxvar_sdriver->init_termios = tty_std_termios;
2641 mxvar_sdriver->init_termios.c_cflag = B9600|CS8|CREAD|HUPCL|CLOCAL;
2642 mxvar_sdriver->flags = TTY_DRIVER_REAL_RAW|TTY_DRIVER_DYNAMIC_DEV;
2643 tty_set_operations(mxvar_sdriver, &mxser_ops);
2644
2645 retval = tty_register_driver(mxvar_sdriver);
2646 if (retval) {
2647 printk(KERN_ERR "Couldn't install MOXA Smartio/Industio family "
2648 "tty driver !\n");
2649 goto err_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 }
Jiri Slaby1c456072008-02-07 00:16:46 -08002651
2652 mxvar_diagflag = 0;
2653
2654 m = 0;
2655 /* Start finding ISA boards here */
2656 for (isaloop = 0; isaloop < 2; isaloop++)
2657 for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
2658 if (!isaloop)
2659 cap = mxserBoardCAP[b]; /* predefined */
2660 else
2661 cap = ioaddr[b]; /* module param */
2662
2663 if (!cap)
2664 continue;
2665
2666 brd = &mxser_boards[m];
2667 retval = mxser_get_ISA_conf(cap, brd);
2668
2669 if (retval != 0)
2670 printk(KERN_INFO "Found MOXA %s board "
2671 "(CAP=0x%x)\n",
2672 brd->info->name, ioaddr[b]);
2673
2674 if (retval <= 0) {
2675 if (retval == MXSER_ERR_IRQ)
2676 printk(KERN_ERR "Invalid interrupt "
2677 "number, board not "
2678 "configured\n");
2679 else if (retval == MXSER_ERR_IRQ_CONFLIT)
2680 printk(KERN_ERR "Invalid interrupt "
2681 "number, board not "
2682 "configured\n");
2683 else if (retval == MXSER_ERR_VECTOR)
2684 printk(KERN_ERR "Invalid interrupt "
2685 "vector, board not "
2686 "configured\n");
2687 else if (retval == MXSER_ERR_IOADDR)
2688 printk(KERN_ERR "Invalid I/O address, "
2689 "board not configured\n");
2690
2691 brd->info = NULL;
2692 continue;
2693 }
2694
2695 /* mxser_initbrd will hook ISR. */
2696 if (mxser_initbrd(brd, NULL) < 0) {
2697 brd->info = NULL;
2698 continue;
2699 }
2700
2701 brd->idx = m * MXSER_PORTS_PER_BOARD;
2702 for (i = 0; i < brd->info->nports; i++)
2703 tty_register_device(mxvar_sdriver, brd->idx + i,
2704 NULL);
2705
2706 m++;
2707 }
2708
2709 retval = pci_register_driver(&mxser_driver);
2710 if (retval) {
2711 printk(KERN_ERR "Can't register pci driver\n");
2712 if (!m) {
2713 retval = -ENODEV;
2714 goto err_unr;
2715 } /* else: we have some ISA cards under control */
2716 }
2717
2718 pr_debug("Done.\n");
2719
2720 return 0;
2721err_unr:
2722 tty_unregister_driver(mxvar_sdriver);
2723err_put:
2724 put_tty_driver(mxvar_sdriver);
2725 return retval;
2726}
2727
2728static void __exit mxser_module_exit(void)
2729{
2730 unsigned int i, j;
2731
2732 pr_debug("Unloading module mxser ...\n");
2733
2734 pci_unregister_driver(&mxser_driver);
2735
2736 for (i = 0; i < MXSER_BOARDS; i++) /* ISA remains */
2737 if (mxser_boards[i].info != NULL)
2738 for (j = 0; j < mxser_boards[i].info->nports; j++)
2739 tty_unregister_device(mxvar_sdriver,
2740 mxser_boards[i].idx + j);
2741 tty_unregister_driver(mxvar_sdriver);
2742 put_tty_driver(mxvar_sdriver);
2743
2744 for (i = 0; i < MXSER_BOARDS; i++)
2745 if (mxser_boards[i].info != NULL)
2746 mxser_release_res(&mxser_boards[i], NULL, 1);
2747
2748 pr_debug("Done.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749}
2750
2751module_init(mxser_module_init);
2752module_exit(mxser_module_exit);