blob: 0f4660aea2607a76c3b94e3f7e6b374851a7adc1 [file] [log] [blame]
Jiri Slaby037ad482006-12-08 02:38:11 -08001/*
2 * mxser.c -- MOXA Smartio/Industio family multiport serial driver.
3 *
Jiri Slaby3306ce32006-12-08 02:38:13 -08004 * Copyright (C) 1999-2006 Moxa Technologies (support@moxa.com.tw).
Jiri Slaby037ad482006-12-08 02:38:11 -08005 *
6 * This code is loosely based on the Linux serial driver, written by
7 * Linus Torvalds, Theodore T'so and others.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 *
Jiri Slaby037ad482006-12-08 02:38:11 -080023 * Fed through a cleanup, indent and remove of non 2.6 code by Alan Cox
24 * <alan@redhat.com>. The original 1.8 code is available on www.moxa.com.
25 * - Fixed x86_64 cleanness
26 * - Fixed sleep with spinlock held in mxser_send_break
27 */
28
29
30#include <linux/module.h>
31#include <linux/autoconf.h>
32#include <linux/errno.h>
33#include <linux/signal.h>
34#include <linux/sched.h>
35#include <linux/timer.h>
36#include <linux/interrupt.h>
37#include <linux/tty.h>
38#include <linux/tty_flip.h>
39#include <linux/serial.h>
40#include <linux/serial_reg.h>
41#include <linux/major.h>
42#include <linux/string.h>
43#include <linux/fcntl.h>
44#include <linux/ptrace.h>
45#include <linux/gfp.h>
46#include <linux/ioport.h>
47#include <linux/mm.h>
48#include <linux/smp_lock.h>
49#include <linux/delay.h>
50#include <linux/pci.h>
51
52#include <asm/system.h>
53#include <asm/io.h>
54#include <asm/irq.h>
55#include <asm/bitops.h>
56#include <asm/uaccess.h>
57
Jiri Slaby771f2d12006-12-08 02:38:12 -080058#include "mxser_new.h"
Jiri Slaby037ad482006-12-08 02:38:11 -080059
Jiri Slaby55b307d2006-12-08 02:38:14 -080060#define MXSER_VERSION "2.0"
Jiri Slaby037ad482006-12-08 02:38:11 -080061#define MXSERMAJOR 174
62#define MXSERCUMAJOR 175
63
64#define MXSER_EVENT_TXLOW 1
65#define MXSER_EVENT_HANGUP 2
66
67#define MXSER_BOARDS 4 /* Max. boards */
68#define MXSER_PORTS 32 /* Max. ports */
69#define MXSER_PORTS_PER_BOARD 8 /* Max. ports per board */
Jiri Slaby3306ce32006-12-08 02:38:13 -080070#define MXSER_ISR_PASS_LIMIT 99999L
Jiri Slaby037ad482006-12-08 02:38:11 -080071
72#define MXSER_ERR_IOADDR -1
73#define MXSER_ERR_IRQ -2
74#define MXSER_ERR_IRQ_CONFLIT -3
75#define MXSER_ERR_VECTOR -4
76
77#define SERIAL_TYPE_NORMAL 1
78#define SERIAL_TYPE_CALLOUT 2
79
80#define WAKEUP_CHARS 256
81
82#define UART_MCR_AFE 0x20
83#define UART_LSR_SPECIAL 0x1E
84
85#define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK|\
86 IXON|IXOFF))
87
Jiri Slaby037ad482006-12-08 02:38:11 -080088#define C168_ASIC_ID 1
89#define C104_ASIC_ID 2
90#define C102_ASIC_ID 0xB
91#define CI132_ASIC_ID 4
92#define CI134_ASIC_ID 3
93#define CI104J_ASIC_ID 5
94
95enum {
96 MXSER_BOARD_C168_ISA = 1,
97 MXSER_BOARD_C104_ISA,
98 MXSER_BOARD_CI104J,
99 MXSER_BOARD_C168_PCI,
100 MXSER_BOARD_C104_PCI,
101 MXSER_BOARD_C102_ISA,
102 MXSER_BOARD_CI132,
103 MXSER_BOARD_CI134,
104 MXSER_BOARD_CP132,
105 MXSER_BOARD_CP114,
106 MXSER_BOARD_CT114,
107 MXSER_BOARD_CP102,
108 MXSER_BOARD_CP104U,
109 MXSER_BOARD_CP168U,
110 MXSER_BOARD_CP132U,
111 MXSER_BOARD_CP134U,
112 MXSER_BOARD_CP104JU,
113 MXSER_BOARD_RC7000,
114 MXSER_BOARD_CP118U,
115 MXSER_BOARD_CP102UL,
116 MXSER_BOARD_CP102U,
Jiri Slaby3306ce32006-12-08 02:38:13 -0800117 MXSER_BOARD_CP118EL,
118 MXSER_BOARD_CP168EL,
119 MXSER_BOARD_CP104EL
Jiri Slaby037ad482006-12-08 02:38:11 -0800120};
121
122static char *mxser_brdname[] = {
123 "C168 series",
124 "C104 series",
125 "CI-104J series",
126 "C168H/PCI series",
127 "C104H/PCI series",
128 "C102 series",
129 "CI-132 series",
130 "CI-134 series",
131 "CP-132 series",
132 "CP-114 series",
133 "CT-114 series",
134 "CP-102 series",
135 "CP-104U series",
136 "CP-168U series",
137 "CP-132U series",
138 "CP-134U series",
139 "CP-104JU series",
140 "Moxa UC7000 Serial",
141 "CP-118U series",
142 "CP-102UL series",
143 "CP-102U series",
Jiri Slaby3306ce32006-12-08 02:38:13 -0800144 "CP-118EL series",
145 "CP-168EL series",
146 "CP-104EL series"
Jiri Slaby037ad482006-12-08 02:38:11 -0800147};
148
149static int mxser_numports[] = {
150 8, /* C168-ISA */
151 4, /* C104-ISA */
152 4, /* CI104J */
153 8, /* C168-PCI */
154 4, /* C104-PCI */
155 2, /* C102-ISA */
156 2, /* CI132 */
157 4, /* CI134 */
158 2, /* CP132 */
159 4, /* CP114 */
160 4, /* CT114 */
161 2, /* CP102 */
162 4, /* CP104U */
163 8, /* CP168U */
164 2, /* CP132U */
165 4, /* CP134U */
166 4, /* CP104JU */
167 8, /* RC7000 */
168 8, /* CP118U */
169 2, /* CP102UL */
170 2, /* CP102U */
Jiri Slaby3306ce32006-12-08 02:38:13 -0800171 8, /* CP118EL */
172 8, /* CP168EL */
173 4 /* CP104EL */
Jiri Slaby037ad482006-12-08 02:38:11 -0800174};
175
176#define UART_TYPE_NUM 2
177
178static const unsigned int Gmoxa_uart_id[UART_TYPE_NUM] = {
179 MOXA_MUST_MU150_HWID,
180 MOXA_MUST_MU860_HWID
181};
182
183/* This is only for PCI */
184#define UART_INFO_NUM 3
185struct mxpciuart_info {
186 int type;
187 int tx_fifo;
188 int rx_fifo;
189 int xmit_fifo_size;
190 int rx_high_water;
191 int rx_trigger;
192 int rx_low_water;
193 long max_baud;
194};
195
196static const struct mxpciuart_info Gpci_uart_info[UART_INFO_NUM] = {
197 {MOXA_OTHER_UART, 16, 16, 16, 14, 14, 1, 921600L},
198 {MOXA_MUST_MU150_HWID, 64, 64, 64, 48, 48, 16, 230400L},
199 {MOXA_MUST_MU860_HWID, 128, 128, 128, 96, 96, 32, 921600L}
200};
201
202
Jiri Slaby037ad482006-12-08 02:38:11 -0800203static struct pci_device_id mxser_pcibrds[] = {
Jiri Slaby3306ce32006-12-08 02:38:13 -0800204 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C168),
205 .driver_data = MXSER_BOARD_C168_PCI },
206 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C104),
207 .driver_data = MXSER_BOARD_C104_PCI },
208 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP132),
209 .driver_data = MXSER_BOARD_CP132 },
210 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP114),
211 .driver_data = MXSER_BOARD_CP114 },
212 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CT114),
213 .driver_data = MXSER_BOARD_CT114 },
214 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP102),
215 .driver_data = MXSER_BOARD_CP102 },
216 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP104U),
217 .driver_data = MXSER_BOARD_CP104U },
218 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP168U),
219 .driver_data = MXSER_BOARD_CP168U },
220 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP132U),
221 .driver_data = MXSER_BOARD_CP132U },
222 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP134U),
223 .driver_data = MXSER_BOARD_CP134U },
224 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP104JU),
225 .driver_data = MXSER_BOARD_CP104JU },
226 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_RC7000),
227 .driver_data = MXSER_BOARD_RC7000 },
228 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP118U),
229 .driver_data = MXSER_BOARD_CP118U },
230 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP102UL),
231 .driver_data = MXSER_BOARD_CP102UL },
232 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP102U),
233 .driver_data = MXSER_BOARD_CP102U },
234 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP118EL),
235 .driver_data = MXSER_BOARD_CP118EL },
236 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP168EL),
237 .driver_data = MXSER_BOARD_CP168EL },
238 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP104EL),
239 .driver_data = MXSER_BOARD_CP104EL },
240 { }
Jiri Slaby037ad482006-12-08 02:38:11 -0800241};
Jiri Slaby037ad482006-12-08 02:38:11 -0800242MODULE_DEVICE_TABLE(pci, mxser_pcibrds);
243
Jiri Slaby037ad482006-12-08 02:38:11 -0800244static int ioaddr[MXSER_BOARDS] = { 0, 0, 0, 0 };
245static int ttymajor = MXSERMAJOR;
246static int calloutmajor = MXSERCUMAJOR;
Jiri Slaby037ad482006-12-08 02:38:11 -0800247
248/* Variables for insmod */
249
250MODULE_AUTHOR("Casper Yang");
251MODULE_DESCRIPTION("MOXA Smartio/Industio Family Multiport Board Device Driver");
252module_param_array(ioaddr, int, NULL, 0);
253module_param(ttymajor, int, 0);
Jiri Slaby037ad482006-12-08 02:38:11 -0800254MODULE_LICENSE("GPL");
255
256struct mxser_log {
257 int tick;
258 unsigned long rxcnt[MXSER_PORTS];
259 unsigned long txcnt[MXSER_PORTS];
260};
261
262
263struct mxser_mon {
264 unsigned long rxcnt;
265 unsigned long txcnt;
266 unsigned long up_rxcnt;
267 unsigned long up_txcnt;
268 int modem_status;
269 unsigned char hold_reason;
270};
271
272struct mxser_mon_ext {
273 unsigned long rx_cnt[32];
274 unsigned long tx_cnt[32];
275 unsigned long up_rxcnt[32];
276 unsigned long up_txcnt[32];
277 int modem_status[32];
278
279 long baudrate[32];
280 int databits[32];
281 int stopbits[32];
282 int parity[32];
283 int flowctrl[32];
284 int fifo[32];
285 int iftype[32];
286};
287
Jiri Slaby55b307d2006-12-08 02:38:14 -0800288struct mxser_board;
Jiri Slaby037ad482006-12-08 02:38:11 -0800289
Jiri Slaby55b307d2006-12-08 02:38:14 -0800290struct mxser_port {
291 struct mxser_board *board;
292 struct tty_struct *tty;
293
294 unsigned long ioaddr;
295 unsigned long opmode_ioaddr;
296 int max_baud;
297
Jiri Slaby037ad482006-12-08 02:38:11 -0800298 int rx_high_water;
299 int rx_trigger; /* Rx fifo trigger level */
300 int rx_low_water;
301 int baud_base; /* max. speed */
Jiri Slaby55b307d2006-12-08 02:38:14 -0800302 long realbaud;
Jiri Slaby037ad482006-12-08 02:38:11 -0800303 int type; /* UART type */
Jiri Slaby55b307d2006-12-08 02:38:14 -0800304 int flags; /* defined in tty.h */
305 long session; /* Session of opening process */
306 long pgrp; /* pgrp of opening process */
307
308 int x_char; /* xon/xoff character */
309 int IER; /* Interrupt Enable Register */
310 int MCR; /* Modem control register */
311
312 unsigned char stop_rx;
313 unsigned char ldisc_stop_rx;
314
315 int custom_divisor;
316 int close_delay;
317 unsigned short closing_wait;
318 unsigned char err_shadow;
319 unsigned long event;
320
321 int count; /* # of fd on device */
322 int blocked_open; /* # of blocked opens */
323 struct async_icount icount; /* kernel counters for 4 input interrupts */
324 int timeout;
325
Jiri Slaby037ad482006-12-08 02:38:11 -0800326 int read_status_mask;
327 int ignore_status_mask;
328 int xmit_fifo_size;
Jiri Slaby037ad482006-12-08 02:38:11 -0800329 unsigned char *xmit_buf;
330 int xmit_head;
331 int xmit_tail;
332 int xmit_cnt;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800333
Jiri Slaby037ad482006-12-08 02:38:11 -0800334 struct termios normal_termios;
335 struct termios callout_termios;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800336
337 struct mxser_mon mon_data;
338
339 spinlock_t slock;
340 struct work_struct tqueue;
Jiri Slaby037ad482006-12-08 02:38:11 -0800341 wait_queue_head_t open_wait;
342 wait_queue_head_t close_wait;
343 wait_queue_head_t delta_msr_wait;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800344};
345
346struct mxser_board {
347 struct pci_dev *pdev; /* temporary (until pci probing) */
348
349 int irq;
350 int board_type;
351 unsigned int nports;
352 unsigned long vector;
353 unsigned long vector_mask;
354
355 int chip_flag;
356 int uart_type;
357
358 struct mxser_port ports[MXSER_PORTS_PER_BOARD];
Jiri Slaby037ad482006-12-08 02:38:11 -0800359};
360
361struct mxser_mstatus {
362 tcflag_t cflag;
363 int cts;
364 int dsr;
365 int ri;
366 int dcd;
367};
368
369static struct mxser_mstatus GMStatus[MXSER_PORTS];
370
371static int mxserBoardCAP[MXSER_BOARDS] = {
372 0, 0, 0, 0
373 /* 0x180, 0x280, 0x200, 0x320 */
374};
375
Jiri Slaby55b307d2006-12-08 02:38:14 -0800376static struct mxser_board mxser_boards[MXSER_BOARDS];
Jiri Slaby037ad482006-12-08 02:38:11 -0800377static struct tty_driver *mxvar_sdriver;
Jiri Slaby037ad482006-12-08 02:38:11 -0800378static struct tty_struct *mxvar_tty[MXSER_PORTS + 1];
379static struct termios *mxvar_termios[MXSER_PORTS + 1];
380static struct termios *mxvar_termios_locked[MXSER_PORTS + 1];
381static struct mxser_log mxvar_log;
382static int mxvar_diagflag;
383static unsigned char mxser_msr[MXSER_PORTS + 1];
384static struct mxser_mon_ext mon_data_ext;
385static int mxser_set_baud_method[MXSER_PORTS + 1];
386static spinlock_t gm_lock;
387
388/*
Jiri Slaby037ad482006-12-08 02:38:11 -0800389 * static functions:
390 */
391
Jiri Slaby037ad482006-12-08 02:38:11 -0800392static int mxser_init(void);
393
394/* static void mxser_poll(unsigned long); */
Jiri Slaby55b307d2006-12-08 02:38:14 -0800395static int mxser_get_ISA_conf(int, struct mxser_board *);
Jiri Slaby037ad482006-12-08 02:38:11 -0800396static void mxser_do_softint(void *);
397static int mxser_open(struct tty_struct *, struct file *);
398static void mxser_close(struct tty_struct *, struct file *);
399static int mxser_write(struct tty_struct *, const unsigned char *, int);
400static int mxser_write_room(struct tty_struct *);
401static void mxser_flush_buffer(struct tty_struct *);
402static int mxser_chars_in_buffer(struct tty_struct *);
403static void mxser_flush_chars(struct tty_struct *);
404static void mxser_put_char(struct tty_struct *, unsigned char);
405static int mxser_ioctl(struct tty_struct *, struct file *, uint, ulong);
406static int mxser_ioctl_special(unsigned int, void __user *);
407static void mxser_throttle(struct tty_struct *);
408static void mxser_unthrottle(struct tty_struct *);
409static void mxser_set_termios(struct tty_struct *, struct termios *);
410static void mxser_stop(struct tty_struct *);
411static void mxser_start(struct tty_struct *);
412static void mxser_hangup(struct tty_struct *);
413static void mxser_rs_break(struct tty_struct *, int);
414static irqreturn_t mxser_interrupt(int, void *, struct pt_regs *);
Jiri Slaby55b307d2006-12-08 02:38:14 -0800415static void mxser_receive_chars(struct mxser_port *, int *);
416static void mxser_transmit_chars(struct mxser_port *);
417static void mxser_check_modem_status(struct mxser_port *, int);
418static int mxser_block_til_ready(struct tty_struct *, struct file *,
419 struct mxser_port *);
420static int mxser_startup(struct mxser_port *);
421static void mxser_shutdown(struct mxser_port *);
422static int mxser_change_speed(struct mxser_port *, struct termios *);
423static int mxser_get_serial_info(struct mxser_port *,
424 struct serial_struct __user *);
425static int mxser_set_serial_info(struct mxser_port *,
426 struct serial_struct __user *);
427static int mxser_get_lsr_info(struct mxser_port *, unsigned int __user *);
428static void mxser_send_break(struct mxser_port *, int);
Jiri Slaby037ad482006-12-08 02:38:11 -0800429static int mxser_tiocmget(struct tty_struct *, struct file *);
Jiri Slaby55b307d2006-12-08 02:38:14 -0800430static int mxser_tiocmset(struct tty_struct *, struct file *, unsigned int,
431 unsigned int);
432static int mxser_set_baud(struct mxser_port *, long);
433static void mxser_wait_until_sent(struct tty_struct *, int);
Jiri Slaby037ad482006-12-08 02:38:11 -0800434
Jiri Slaby55b307d2006-12-08 02:38:14 -0800435static void mxser_startrx(struct tty_struct *);
436static void mxser_stoprx(struct tty_struct *);
Jiri Slaby037ad482006-12-08 02:38:11 -0800437
438
439static int CheckIsMoxaMust(int io)
440{
441 u8 oldmcr, hwid;
442 int i;
443
444 outb(0, io + UART_LCR);
445 DISABLE_MOXA_MUST_ENCHANCE_MODE(io);
446 oldmcr = inb(io + UART_MCR);
447 outb(0, io + UART_MCR);
448 SET_MOXA_MUST_XON1_VALUE(io, 0x11);
449 if ((hwid = inb(io + UART_MCR)) != 0) {
450 outb(oldmcr, io + UART_MCR);
451 return MOXA_OTHER_UART;
452 }
453
454 GET_MOXA_MUST_HARDWARE_ID(io, &hwid);
455 for (i = 0; i < UART_TYPE_NUM; i++) {
456 if (hwid == Gmoxa_uart_id[i])
457 return (int)hwid;
458 }
459 return MOXA_OTHER_UART;
460}
461
462/* above is modified by Victor Yu. 08-15-2002 */
463
464static const struct tty_operations mxser_ops = {
465 .open = mxser_open,
466 .close = mxser_close,
467 .write = mxser_write,
468 .put_char = mxser_put_char,
469 .flush_chars = mxser_flush_chars,
470 .write_room = mxser_write_room,
471 .chars_in_buffer = mxser_chars_in_buffer,
472 .flush_buffer = mxser_flush_buffer,
473 .ioctl = mxser_ioctl,
474 .throttle = mxser_throttle,
475 .unthrottle = mxser_unthrottle,
476 .set_termios = mxser_set_termios,
477 .stop = mxser_stop,
478 .start = mxser_start,
479 .hangup = mxser_hangup,
480 .break_ctl = mxser_rs_break,
481 .wait_until_sent = mxser_wait_until_sent,
482 .tiocmget = mxser_tiocmget,
483 .tiocmset = mxser_tiocmset,
484};
485
486/*
487 * The MOXA Smartio/Industio serial driver boot-time initialization code!
488 */
489
490static int __init mxser_module_init(void)
491{
492 int ret;
493
Jiri Slaby7f589922006-12-08 02:38:20 -0800494 pr_debug("Loading module mxser ...\n");
Jiri Slaby037ad482006-12-08 02:38:11 -0800495 ret = mxser_init();
Jiri Slaby7f589922006-12-08 02:38:20 -0800496 pr_debug("Done.\n");
Jiri Slaby037ad482006-12-08 02:38:11 -0800497 return ret;
498}
499
500static void __exit mxser_module_exit(void)
501{
502 int i, err;
503
Jiri Slaby7f589922006-12-08 02:38:20 -0800504 pr_debug("Unloading module mxser ...\n");
Jiri Slaby037ad482006-12-08 02:38:11 -0800505
506 err = tty_unregister_driver(mxvar_sdriver);
507 if (!err)
508 put_tty_driver(mxvar_sdriver);
509 else
510 printk(KERN_ERR "Couldn't unregister MOXA Smartio/Industio family serial driver\n");
511
512 for (i = 0; i < MXSER_BOARDS; i++) {
513 struct pci_dev *pdev;
514
Jiri Slaby55b307d2006-12-08 02:38:14 -0800515 if (mxser_boards[i].board_type == -1)
Jiri Slaby037ad482006-12-08 02:38:11 -0800516 continue;
517 else {
Jiri Slaby55b307d2006-12-08 02:38:14 -0800518 pdev = mxser_boards[i].pdev;
519 free_irq(mxser_boards[i].irq, &mxser_boards[i]);
Jiri Slaby037ad482006-12-08 02:38:11 -0800520 if (pdev != NULL) { /* PCI */
Jiri Slaby59628012006-12-08 02:38:16 -0800521 pci_release_region(pdev, 2);
522 pci_release_region(pdev, 3);
Jiri Slaby037ad482006-12-08 02:38:11 -0800523 pci_dev_put(pdev);
524 } else {
Jiri Slaby55b307d2006-12-08 02:38:14 -0800525 release_region(mxser_boards[i].ports[0].ioaddr, 8 * mxser_boards[i].nports);
526 release_region(mxser_boards[i].vector, 1);
Jiri Slaby037ad482006-12-08 02:38:11 -0800527 }
528 }
529 }
Jiri Slaby7f589922006-12-08 02:38:20 -0800530 pr_debug("Done.\n");
Jiri Slaby037ad482006-12-08 02:38:11 -0800531}
532
Jiri Slaby55b307d2006-12-08 02:38:14 -0800533static void process_txrx_fifo(struct mxser_port *info)
Jiri Slaby037ad482006-12-08 02:38:11 -0800534{
535 int i;
536
537 if ((info->type == PORT_16450) || (info->type == PORT_8250)) {
538 info->rx_trigger = 1;
539 info->rx_high_water = 1;
540 info->rx_low_water = 1;
541 info->xmit_fifo_size = 1;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800542 } else
543 for (i = 0; i < UART_INFO_NUM; i++)
544 if (info->board->chip_flag == Gpci_uart_info[i].type) {
Jiri Slaby037ad482006-12-08 02:38:11 -0800545 info->rx_trigger = Gpci_uart_info[i].rx_trigger;
546 info->rx_low_water = Gpci_uart_info[i].rx_low_water;
547 info->rx_high_water = Gpci_uart_info[i].rx_high_water;
548 info->xmit_fifo_size = Gpci_uart_info[i].xmit_fifo_size;
549 break;
550 }
Jiri Slaby037ad482006-12-08 02:38:11 -0800551}
552
Jiri Slaby943f2952006-12-08 02:38:15 -0800553static int __devinit mxser_initbrd(struct mxser_board *brd)
Jiri Slaby037ad482006-12-08 02:38:11 -0800554{
Jiri Slaby55b307d2006-12-08 02:38:14 -0800555 struct mxser_port *info;
556 unsigned int i;
Jiri Slaby037ad482006-12-08 02:38:11 -0800557 int retval;
Jiri Slaby037ad482006-12-08 02:38:11 -0800558
Jiri Slaby7f589922006-12-08 02:38:20 -0800559 printk(KERN_INFO "max. baud rate = %d bps.\n", brd->ports[0].max_baud);
Jiri Slaby037ad482006-12-08 02:38:11 -0800560
Jiri Slaby55b307d2006-12-08 02:38:14 -0800561 for (i = 0; i < brd->nports; i++) {
562 info = &brd->ports[i];
563 info->board = brd;
Jiri Slaby037ad482006-12-08 02:38:11 -0800564 info->stop_rx = 0;
565 info->ldisc_stop_rx = 0;
566
Jiri Slaby037ad482006-12-08 02:38:11 -0800567 /* Enhance mode enabled here */
Jiri Slaby55b307d2006-12-08 02:38:14 -0800568 if (brd->chip_flag != MOXA_OTHER_UART)
569 ENABLE_MOXA_MUST_ENCHANCE_MODE(info->ioaddr);
Jiri Slaby037ad482006-12-08 02:38:11 -0800570
571 info->flags = ASYNC_SHARE_IRQ;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800572 info->type = brd->uart_type;
Jiri Slaby037ad482006-12-08 02:38:11 -0800573
574 process_txrx_fifo(info);
575
Jiri Slaby55b307d2006-12-08 02:38:14 -0800576 info->custom_divisor = info->baud_base * 16;
Jiri Slaby037ad482006-12-08 02:38:11 -0800577 info->close_delay = 5 * HZ / 10;
578 info->closing_wait = 30 * HZ;
579 INIT_WORK(&info->tqueue, mxser_do_softint, info);
580 info->normal_termios = mxvar_sdriver->init_termios;
581 init_waitqueue_head(&info->open_wait);
582 init_waitqueue_head(&info->close_wait);
583 init_waitqueue_head(&info->delta_msr_wait);
584 memset(&info->mon_data, 0, sizeof(struct mxser_mon));
585 info->err_shadow = 0;
586 spin_lock_init(&info->slock);
Jiri Slaby55b307d2006-12-08 02:38:14 -0800587
588 /* before set INT ISR, disable all int */
589 outb(inb(info->ioaddr + UART_IER) & 0xf0,
590 info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -0800591 }
592 /*
593 * Allocate the IRQ if necessary
594 */
595
Jiri Slaby55b307d2006-12-08 02:38:14 -0800596 retval = request_irq(brd->irq, mxser_interrupt,
597 (brd->ports[0].flags & ASYNC_SHARE_IRQ) ? IRQF_SHARED :
598 IRQF_DISABLED, "mxser", brd);
Jiri Slaby037ad482006-12-08 02:38:11 -0800599 if (retval) {
Jiri Slaby55b307d2006-12-08 02:38:14 -0800600 printk(KERN_ERR "Board %s: Request irq failed, IRQ (%d) may "
601 "conflict with another device.\n",
602 mxser_brdname[brd->board_type - 1], brd->irq);
Jiri Slaby037ad482006-12-08 02:38:11 -0800603 return retval;
604 }
605 return 0;
606}
607
Jiri Slaby943f2952006-12-08 02:38:15 -0800608static int __init mxser_get_PCI_conf(int board_type, struct mxser_board *brd,
Jiri Slaby55b307d2006-12-08 02:38:14 -0800609 struct pci_dev *pdev)
Jiri Slaby037ad482006-12-08 02:38:11 -0800610{
Jiri Slaby55b307d2006-12-08 02:38:14 -0800611 unsigned int i, j;
Jiri Slaby3306ce32006-12-08 02:38:13 -0800612 unsigned long ioaddress;
Jiri Slaby7a7a5c32006-12-08 02:38:17 -0800613 int retval;
Jiri Slaby037ad482006-12-08 02:38:11 -0800614
615 /* io address */
Jiri Slaby55b307d2006-12-08 02:38:14 -0800616 brd->board_type = board_type;
617 brd->nports = mxser_numports[board_type - 1];
Jiri Slaby037ad482006-12-08 02:38:11 -0800618 ioaddress = pci_resource_start(pdev, 2);
Jiri Slaby7a7a5c32006-12-08 02:38:17 -0800619 retval = pci_request_region(pdev, 2, "mxser(IO)");
620 if (retval)
621 goto err;
Jiri Slaby037ad482006-12-08 02:38:11 -0800622
Jiri Slaby55b307d2006-12-08 02:38:14 -0800623 for (i = 0; i < brd->nports; i++)
624 brd->ports[i].ioaddr = ioaddress + 8 * i;
Jiri Slaby037ad482006-12-08 02:38:11 -0800625
626 /* vector */
627 ioaddress = pci_resource_start(pdev, 3);
Jiri Slaby7a7a5c32006-12-08 02:38:17 -0800628 retval = pci_request_region(pdev, 3, "mxser(vector)");
629 if (retval)
630 goto err_relio;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800631 brd->vector = ioaddress;
Jiri Slaby037ad482006-12-08 02:38:11 -0800632
633 /* irq */
Jiri Slaby55b307d2006-12-08 02:38:14 -0800634 brd->irq = pdev->irq;
Jiri Slaby037ad482006-12-08 02:38:11 -0800635
Jiri Slaby55b307d2006-12-08 02:38:14 -0800636 brd->chip_flag = CheckIsMoxaMust(brd->ports[0].ioaddr);
637 brd->uart_type = PORT_16550A;
638 brd->vector_mask = 0;
Jiri Slaby037ad482006-12-08 02:38:11 -0800639
Jiri Slaby55b307d2006-12-08 02:38:14 -0800640 for (i = 0; i < brd->nports; i++) {
Jiri Slaby037ad482006-12-08 02:38:11 -0800641 for (j = 0; j < UART_INFO_NUM; j++) {
Jiri Slaby55b307d2006-12-08 02:38:14 -0800642 if (Gpci_uart_info[j].type == brd->chip_flag) {
643 brd->ports[i].max_baud =
644 Gpci_uart_info[j].max_baud;
Jiri Slaby037ad482006-12-08 02:38:11 -0800645
646 /* exception....CP-102 */
647 if (board_type == MXSER_BOARD_CP102)
Jiri Slaby55b307d2006-12-08 02:38:14 -0800648 brd->ports[i].max_baud = 921600;
Jiri Slaby037ad482006-12-08 02:38:11 -0800649 break;
650 }
651 }
652 }
653
Jiri Slaby55b307d2006-12-08 02:38:14 -0800654 if (brd->chip_flag == MOXA_MUST_MU860_HWID) {
655 for (i = 0; i < brd->nports; i++) {
Jiri Slaby037ad482006-12-08 02:38:11 -0800656 if (i < 4)
Jiri Slaby55b307d2006-12-08 02:38:14 -0800657 brd->ports[i].opmode_ioaddr = ioaddress + 4;
Jiri Slaby037ad482006-12-08 02:38:11 -0800658 else
Jiri Slaby55b307d2006-12-08 02:38:14 -0800659 brd->ports[i].opmode_ioaddr = ioaddress + 0x0c;
Jiri Slaby037ad482006-12-08 02:38:11 -0800660 }
661 outb(0, ioaddress + 4); /* default set to RS232 mode */
662 outb(0, ioaddress + 0x0c); /* default set to RS232 mode */
663 }
664
Jiri Slaby55b307d2006-12-08 02:38:14 -0800665 for (i = 0; i < brd->nports; i++) {
666 brd->vector_mask |= (1 << i);
667 brd->ports[i].baud_base = 921600;
Jiri Slaby037ad482006-12-08 02:38:11 -0800668 }
669 return 0;
Jiri Slaby7a7a5c32006-12-08 02:38:17 -0800670err_relio:
671 pci_release_region(pdev, 2);
672err:
673 return retval;
Jiri Slaby037ad482006-12-08 02:38:11 -0800674}
Jiri Slaby037ad482006-12-08 02:38:11 -0800675
Jiri Slaby943f2952006-12-08 02:38:15 -0800676static int __init mxser_init(void)
Jiri Slaby037ad482006-12-08 02:38:11 -0800677{
Jiri Slaby037ad482006-12-08 02:38:11 -0800678 struct pci_dev *pdev = NULL;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800679 struct mxser_board *brd;
680 unsigned int i, m;
681 int retval, b, n;
Jiri Slaby037ad482006-12-08 02:38:11 -0800682
683 mxvar_sdriver = alloc_tty_driver(MXSER_PORTS + 1);
684 if (!mxvar_sdriver)
685 return -ENOMEM;
686 spin_lock_init(&gm_lock);
687
Jiri Slaby55b307d2006-12-08 02:38:14 -0800688 for (i = 0; i < MXSER_BOARDS; i++)
689 mxser_boards[i].board_type = -1;
Jiri Slaby037ad482006-12-08 02:38:11 -0800690
691 printk(KERN_INFO "MOXA Smartio/Industio family driver version %s\n",
692 MXSER_VERSION);
693
694 /* Initialize the tty_driver structure */
Jiri Slaby037ad482006-12-08 02:38:11 -0800695 mxvar_sdriver->magic = TTY_DRIVER_MAGIC;
696 mxvar_sdriver->name = "ttyM";
697 mxvar_sdriver->major = ttymajor;
698 mxvar_sdriver->minor_start = 0;
699 mxvar_sdriver->num = MXSER_PORTS + 1;
700 mxvar_sdriver->type = TTY_DRIVER_TYPE_SERIAL;
701 mxvar_sdriver->subtype = SERIAL_TYPE_NORMAL;
702 mxvar_sdriver->init_termios = tty_std_termios;
703 mxvar_sdriver->init_termios.c_cflag = B9600|CS8|CREAD|HUPCL|CLOCAL;
704 mxvar_sdriver->flags = TTY_DRIVER_REAL_RAW;
705 tty_set_operations(mxvar_sdriver, &mxser_ops);
706 mxvar_sdriver->ttys = mxvar_tty;
707 mxvar_sdriver->termios = mxvar_termios;
708 mxvar_sdriver->termios_locked = mxvar_termios_locked;
709
710 mxvar_diagflag = 0;
Jiri Slaby037ad482006-12-08 02:38:11 -0800711
712 m = 0;
713 /* Start finding ISA boards here */
714 for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
715 int cap;
716
717 if (!(cap = mxserBoardCAP[b]))
718 continue;
719
Jiri Slaby55b307d2006-12-08 02:38:14 -0800720 brd = &mxser_boards[m];
721 retval = mxser_get_ISA_conf(cap, brd);
Jiri Slaby037ad482006-12-08 02:38:11 -0800722
723 if (retval != 0)
724 printk(KERN_INFO "Found MOXA %s board (CAP=0x%x)\n",
Jiri Slaby55b307d2006-12-08 02:38:14 -0800725 mxser_brdname[brd->board_type - 1], ioaddr[b]);
Jiri Slaby037ad482006-12-08 02:38:11 -0800726
727 if (retval <= 0) {
728 if (retval == MXSER_ERR_IRQ)
729 printk(KERN_ERR "Invalid interrupt number, "
730 "board not configured\n");
731 else if (retval == MXSER_ERR_IRQ_CONFLIT)
732 printk(KERN_ERR "Invalid interrupt number, "
733 "board not configured\n");
734 else if (retval == MXSER_ERR_VECTOR)
735 printk(KERN_ERR "Invalid interrupt vector, "
736 "board not configured\n");
737 else if (retval == MXSER_ERR_IOADDR)
738 printk(KERN_ERR "Invalid I/O address, "
739 "board not configured\n");
740
741 continue;
742 }
743
Jiri Slaby55b307d2006-12-08 02:38:14 -0800744 brd->pdev = NULL;
Jiri Slaby037ad482006-12-08 02:38:11 -0800745
Jiri Slaby037ad482006-12-08 02:38:11 -0800746 /* mxser_initbrd will hook ISR. */
Jiri Slaby55b307d2006-12-08 02:38:14 -0800747 if (mxser_initbrd(brd) < 0)
Jiri Slaby037ad482006-12-08 02:38:11 -0800748 continue;
749
750 m++;
751 }
752
753 /* Start finding ISA boards from module arg */
754 for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
Jiri Slaby3306ce32006-12-08 02:38:13 -0800755 unsigned long cap;
Jiri Slaby037ad482006-12-08 02:38:11 -0800756
757 if (!(cap = ioaddr[b]))
758 continue;
759
Jiri Slaby55b307d2006-12-08 02:38:14 -0800760 brd = &mxser_boards[m];
761 retval = mxser_get_ISA_conf(cap, &mxser_boards[m]);
Jiri Slaby037ad482006-12-08 02:38:11 -0800762
763 if (retval != 0)
764 printk(KERN_INFO "Found MOXA %s board (CAP=0x%x)\n",
Jiri Slaby55b307d2006-12-08 02:38:14 -0800765 mxser_brdname[brd->board_type - 1], ioaddr[b]);
Jiri Slaby037ad482006-12-08 02:38:11 -0800766
767 if (retval <= 0) {
768 if (retval == MXSER_ERR_IRQ)
769 printk(KERN_ERR "Invalid interrupt number, "
770 "board not configured\n");
771 else if (retval == MXSER_ERR_IRQ_CONFLIT)
772 printk(KERN_ERR "Invalid interrupt number, "
773 "board not configured\n");
774 else if (retval == MXSER_ERR_VECTOR)
775 printk(KERN_ERR "Invalid interrupt vector, "
776 "board not configured\n");
777 else if (retval == MXSER_ERR_IOADDR)
778 printk(KERN_ERR "Invalid I/O address, "
779 "board not configured\n");
780
781 continue;
782 }
783
Jiri Slaby55b307d2006-12-08 02:38:14 -0800784 brd->pdev = NULL;
Jiri Slaby037ad482006-12-08 02:38:11 -0800785 /* mxser_initbrd will hook ISR. */
Jiri Slaby55b307d2006-12-08 02:38:14 -0800786 if (mxser_initbrd(brd) < 0)
Jiri Slaby037ad482006-12-08 02:38:11 -0800787 continue;
788
789 m++;
790 }
791
792 /* start finding PCI board here */
Jiri Slaby037ad482006-12-08 02:38:11 -0800793 n = ARRAY_SIZE(mxser_pcibrds) - 1;
Jiri Slaby037ad482006-12-08 02:38:11 -0800794 b = 0;
795 while (b < n) {
796 pdev = pci_get_device(mxser_pcibrds[b].vendor,
797 mxser_pcibrds[b].device, pdev);
798 if (pdev == NULL) {
799 b++;
800 continue;
801 }
Jiri Slaby037ad482006-12-08 02:38:11 -0800802 printk(KERN_INFO "Found MOXA %s board(BusNo=%d,DevNo=%d)\n",
803 mxser_brdname[(int) (mxser_pcibrds[b].driver_data) - 1],
Jiri Slaby55b307d2006-12-08 02:38:14 -0800804 pdev->bus->number, PCI_SLOT(pdev->devfn));
Jiri Slaby037ad482006-12-08 02:38:11 -0800805 if (m >= MXSER_BOARDS)
806 printk(KERN_ERR
807 "Too many Smartio/Industio family boards find "
808 "(maximum %d), board not configured\n",
809 MXSER_BOARDS);
810 else {
811 if (pci_enable_device(pdev)) {
812 printk(KERN_ERR "Moxa SmartI/O PCI enable "
813 "fail !\n");
814 continue;
815 }
Jiri Slaby55b307d2006-12-08 02:38:14 -0800816 brd = &mxser_boards[m];
817 brd->pdev = pdev;
818 retval = mxser_get_PCI_conf(
Jiri Slaby037ad482006-12-08 02:38:11 -0800819 (int)mxser_pcibrds[b].driver_data,
Jiri Slaby55b307d2006-12-08 02:38:14 -0800820 brd, pdev);
Jiri Slaby037ad482006-12-08 02:38:11 -0800821 if (retval < 0) {
822 if (retval == MXSER_ERR_IRQ)
823 printk(KERN_ERR
824 "Invalid interrupt number, "
825 "board not configured\n");
826 else if (retval == MXSER_ERR_IRQ_CONFLIT)
827 printk(KERN_ERR
828 "Invalid interrupt number, "
829 "board not configured\n");
830 else if (retval == MXSER_ERR_VECTOR)
831 printk(KERN_ERR
832 "Invalid interrupt vector, "
833 "board not configured\n");
834 else if (retval == MXSER_ERR_IOADDR)
835 printk(KERN_ERR
836 "Invalid I/O address, "
837 "board not configured\n");
838 continue;
839 }
Jiri Slaby037ad482006-12-08 02:38:11 -0800840 /* mxser_initbrd will hook ISR. */
Jiri Slaby55b307d2006-12-08 02:38:14 -0800841 if (mxser_initbrd(brd) < 0)
Jiri Slaby037ad482006-12-08 02:38:11 -0800842 continue;
843 m++;
844 /* Keep an extra reference if we succeeded. It will
845 be returned at unload time */
846 pci_dev_get(pdev);
847 }
848 }
Jiri Slaby037ad482006-12-08 02:38:11 -0800849
850 retval = tty_register_driver(mxvar_sdriver);
851 if (retval) {
852 printk(KERN_ERR "Couldn't install MOXA Smartio/Industio family"
853 " driver !\n");
854 put_tty_driver(mxvar_sdriver);
855
856 for (i = 0; i < MXSER_BOARDS; i++) {
Jiri Slaby55b307d2006-12-08 02:38:14 -0800857 if (mxser_boards[i].board_type == -1)
Jiri Slaby037ad482006-12-08 02:38:11 -0800858 continue;
859 else {
Jiri Slaby55b307d2006-12-08 02:38:14 -0800860 free_irq(mxser_boards[i].irq, &mxser_boards[i]);
Jiri Slaby037ad482006-12-08 02:38:11 -0800861 /* todo: release io, vector */
862 }
863 }
864 return retval;
865 }
866
867 return 0;
868}
869
870static void mxser_do_softint(void *private_)
871{
Jiri Slaby55b307d2006-12-08 02:38:14 -0800872 struct mxser_port *info = private_;
Jiri Slaby037ad482006-12-08 02:38:11 -0800873 struct tty_struct *tty;
874
875 tty = info->tty;
876
Jiri Slaby3306ce32006-12-08 02:38:13 -0800877 if (test_and_clear_bit(MXSER_EVENT_TXLOW, &info->event))
878 tty_wakeup(tty);
879 if (test_and_clear_bit(MXSER_EVENT_HANGUP, &info->event))
880 tty_hangup(tty);
Jiri Slaby037ad482006-12-08 02:38:11 -0800881}
882
Jiri Slaby55b307d2006-12-08 02:38:14 -0800883static unsigned char mxser_get_msr(int baseaddr, int mode, int port)
Jiri Slaby037ad482006-12-08 02:38:11 -0800884{
885 unsigned char status = 0;
886
887 status = inb(baseaddr + UART_MSR);
888
889 mxser_msr[port] &= 0x0F;
890 mxser_msr[port] |= status;
891 status = mxser_msr[port];
892 if (mode)
893 mxser_msr[port] = 0;
894
895 return status;
896}
897
898/*
899 * This routine is called whenever a serial port is opened. It
900 * enables interrupts for a serial port, linking in its async structure into
901 * the IRQ chain. It also performs the serial-specific
902 * initialization for the tty structure.
903 */
904static int mxser_open(struct tty_struct *tty, struct file *filp)
905{
Jiri Slaby55b307d2006-12-08 02:38:14 -0800906 struct mxser_port *info;
Jiri Slaby037ad482006-12-08 02:38:11 -0800907 int retval, line;
908
909 /* initialize driver_data in case something fails */
910 tty->driver_data = NULL;
911
912 line = tty->index;
913 if (line == MXSER_PORTS)
914 return 0;
915 if (line < 0 || line > MXSER_PORTS)
916 return -ENODEV;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800917 info = &mxser_boards[line / MXSER_PORTS_PER_BOARD].ports[line % MXSER_PORTS_PER_BOARD];
918 if (!info->ioaddr)
Jiri Slaby037ad482006-12-08 02:38:11 -0800919 return -ENODEV;
920
921 tty->driver_data = info;
922 info->tty = tty;
923 /*
924 * Start up serial port
925 */
Jiri Slaby3306ce32006-12-08 02:38:13 -0800926 info->count++;
Jiri Slaby037ad482006-12-08 02:38:11 -0800927 retval = mxser_startup(info);
928 if (retval)
929 return retval;
930
931 retval = mxser_block_til_ready(tty, filp, info);
932 if (retval)
933 return retval;
934
Jiri Slaby037ad482006-12-08 02:38:11 -0800935 if ((info->count == 1) && (info->flags & ASYNC_SPLIT_TERMIOS)) {
936 if (tty->driver->subtype == SERIAL_TYPE_NORMAL)
937 *tty->termios = info->normal_termios;
938 else
939 *tty->termios = info->callout_termios;
940 mxser_change_speed(info, NULL);
941 }
942
Andrew Morton08a4ae42006-12-08 02:38:12 -0800943 info->session = process_session(current);
Jiri Slaby037ad482006-12-08 02:38:11 -0800944 info->pgrp = process_group(current);
945
946 /*
947 status = mxser_get_msr(info->base, 0, info->port);
948 mxser_check_modem_status(info, status);
949 */
950
951/* unmark here for very high baud rate (ex. 921600 bps) used */
952 tty->low_latency = 1;
953 return 0;
954}
955
956/*
957 * This routine is called when the serial port gets closed. First, we
958 * wait for the last remaining data to be sent. Then, we unlink its
959 * async structure from the interrupt chain if necessary, and we free
960 * that IRQ if nothing is left in the chain.
961 */
962static void mxser_close(struct tty_struct *tty, struct file *filp)
963{
Jiri Slaby55b307d2006-12-08 02:38:14 -0800964 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -0800965
966 unsigned long timeout;
967 unsigned long flags;
968 struct tty_ldisc *ld;
969
970 if (tty->index == MXSER_PORTS)
971 return;
972 if (!info)
973 return;
974
975 spin_lock_irqsave(&info->slock, flags);
976
977 if (tty_hung_up_p(filp)) {
978 spin_unlock_irqrestore(&info->slock, flags);
979 return;
980 }
981 if ((tty->count == 1) && (info->count != 1)) {
982 /*
983 * Uh, oh. tty->count is 1, which means that the tty
984 * structure will be freed. Info->count should always
985 * be one in these conditions. If it's greater than
986 * one, we've got real problems, since it means the
987 * serial port won't be shutdown.
988 */
989 printk(KERN_ERR "mxser_close: bad serial port count; "
990 "tty->count is 1, info->count is %d\n", info->count);
991 info->count = 1;
992 }
993 if (--info->count < 0) {
994 printk(KERN_ERR "mxser_close: bad serial port count for "
Jiri Slaby55b307d2006-12-08 02:38:14 -0800995 "ttys%d: %d\n", tty->index, info->count);
Jiri Slaby037ad482006-12-08 02:38:11 -0800996 info->count = 0;
997 }
998 if (info->count) {
999 spin_unlock_irqrestore(&info->slock, flags);
1000 return;
1001 }
1002 info->flags |= ASYNC_CLOSING;
1003 spin_unlock_irqrestore(&info->slock, flags);
1004 /*
1005 * Save the termios structure, since this port may have
1006 * separate termios for callout and dialin.
1007 */
1008 if (info->flags & ASYNC_NORMAL_ACTIVE)
1009 info->normal_termios = *tty->termios;
1010 /*
1011 * Now we wait for the transmit buffer to clear; and we notify
1012 * the line discipline to only process XON/XOFF characters.
1013 */
1014 tty->closing = 1;
1015 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1016 tty_wait_until_sent(tty, info->closing_wait);
1017 /*
1018 * At this point we stop accepting input. To do this, we
1019 * disable the receive line status interrupts, and tell the
1020 * interrupt driver to stop checking the data ready bit in the
1021 * line status register.
1022 */
1023 info->IER &= ~UART_IER_RLSI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001024 if (info->board->chip_flag)
Jiri Slaby037ad482006-12-08 02:38:11 -08001025 info->IER &= ~MOXA_MUST_RECV_ISR;
1026/* by William
1027 info->read_status_mask &= ~UART_LSR_DR;
1028*/
1029 if (info->flags & ASYNC_INITIALIZED) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08001030 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001031 /*
1032 * Before we drop DTR, make sure the UART transmitter
1033 * has completely drained; this is especially
1034 * important if there is a transmit FIFO!
1035 */
1036 timeout = jiffies + HZ;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001037 while (!(inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001038 schedule_timeout_interruptible(5);
1039 if (time_after(jiffies, timeout))
1040 break;
1041 }
1042 }
1043 mxser_shutdown(info);
1044
1045 if (tty->driver->flush_buffer)
1046 tty->driver->flush_buffer(tty);
1047
1048 ld = tty_ldisc_ref(tty);
1049 if (ld) {
1050 if (ld->flush_buffer)
1051 ld->flush_buffer(tty);
1052 tty_ldisc_deref(ld);
1053 }
1054
1055 tty->closing = 0;
1056 info->event = 0;
1057 info->tty = NULL;
1058 if (info->blocked_open) {
1059 if (info->close_delay)
1060 schedule_timeout_interruptible(info->close_delay);
1061 wake_up_interruptible(&info->open_wait);
1062 }
1063
1064 info->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
1065 wake_up_interruptible(&info->close_wait);
1066
1067}
1068
1069static int mxser_write(struct tty_struct *tty, const unsigned char *buf, int count)
1070{
1071 int c, total = 0;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001072 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001073 unsigned long flags;
1074
1075 if (!info->xmit_buf)
1076 return 0;
1077
1078 while (1) {
1079 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1080 SERIAL_XMIT_SIZE - info->xmit_head));
1081 if (c <= 0)
1082 break;
1083
1084 memcpy(info->xmit_buf + info->xmit_head, buf, c);
1085 spin_lock_irqsave(&info->slock, flags);
1086 info->xmit_head = (info->xmit_head + c) &
1087 (SERIAL_XMIT_SIZE - 1);
1088 info->xmit_cnt += c;
1089 spin_unlock_irqrestore(&info->slock, flags);
1090
1091 buf += c;
1092 count -= c;
1093 total += c;
1094 }
1095
Jiri Slaby3306ce32006-12-08 02:38:13 -08001096 if (info->xmit_cnt && !tty->stopped
1097 /*&& !(info->IER & UART_IER_THRI)*/) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001098 if (!tty->hw_stopped ||
1099 (info->type == PORT_16550A) ||
Jiri Slaby55b307d2006-12-08 02:38:14 -08001100 (info->board->chip_flag)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001101 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby55b307d2006-12-08 02:38:14 -08001102 outb(info->IER & ~UART_IER_THRI, info->ioaddr +
1103 UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001104 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001105 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001106 spin_unlock_irqrestore(&info->slock, flags);
1107 }
1108 }
1109 return total;
1110}
1111
1112static void mxser_put_char(struct tty_struct *tty, unsigned char ch)
1113{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001114 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001115 unsigned long flags;
1116
1117 if (!info->xmit_buf)
1118 return;
1119
1120 if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1)
1121 return;
1122
1123 spin_lock_irqsave(&info->slock, flags);
1124 info->xmit_buf[info->xmit_head++] = ch;
1125 info->xmit_head &= SERIAL_XMIT_SIZE - 1;
1126 info->xmit_cnt++;
1127 spin_unlock_irqrestore(&info->slock, flags);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001128 if (!tty->stopped /*&& !(info->IER & UART_IER_THRI)*/) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001129 if (!tty->hw_stopped ||
1130 (info->type == PORT_16550A) ||
Jiri Slaby55b307d2006-12-08 02:38:14 -08001131 info->board->chip_flag) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001132 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby55b307d2006-12-08 02:38:14 -08001133 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001134 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001135 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001136 spin_unlock_irqrestore(&info->slock, flags);
1137 }
1138 }
1139}
1140
1141
1142static void mxser_flush_chars(struct tty_struct *tty)
1143{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001144 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001145 unsigned long flags;
1146
1147 if (info->xmit_cnt <= 0 ||
1148 tty->stopped ||
1149 !info->xmit_buf ||
1150 (tty->hw_stopped &&
1151 (info->type != PORT_16550A) &&
Jiri Slaby55b307d2006-12-08 02:38:14 -08001152 (!info->board->chip_flag)
Jiri Slaby037ad482006-12-08 02:38:11 -08001153 ))
1154 return;
1155
1156 spin_lock_irqsave(&info->slock, flags);
1157
Jiri Slaby55b307d2006-12-08 02:38:14 -08001158 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001159 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001160 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001161
1162 spin_unlock_irqrestore(&info->slock, flags);
1163}
1164
1165static int mxser_write_room(struct tty_struct *tty)
1166{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001167 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001168 int ret;
1169
1170 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1171 if (ret < 0)
1172 ret = 0;
1173 return ret;
1174}
1175
1176static int mxser_chars_in_buffer(struct tty_struct *tty)
1177{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001178 struct mxser_port *info = tty->driver_data;
Jiri Slaby3306ce32006-12-08 02:38:13 -08001179 int len = info->xmit_cnt;
1180
Jiri Slaby55b307d2006-12-08 02:38:14 -08001181 if (!(inb(info->ioaddr + UART_LSR) & UART_LSR_THRE))
Jiri Slaby3306ce32006-12-08 02:38:13 -08001182 len++;
1183
1184 return len;
Jiri Slaby037ad482006-12-08 02:38:11 -08001185}
1186
1187static void mxser_flush_buffer(struct tty_struct *tty)
1188{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001189 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001190 char fcr;
1191 unsigned long flags;
1192
1193
1194 spin_lock_irqsave(&info->slock, flags);
1195 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1196
1197 /* below added by shinhay */
Jiri Slaby55b307d2006-12-08 02:38:14 -08001198 fcr = inb(info->ioaddr + UART_FCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08001199 outb((fcr | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
Jiri Slaby55b307d2006-12-08 02:38:14 -08001200 info->ioaddr + UART_FCR);
1201 outb(fcr, info->ioaddr + UART_FCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08001202
1203 spin_unlock_irqrestore(&info->slock, flags);
1204 /* above added by shinhay */
1205
1206 wake_up_interruptible(&tty->write_wait);
1207 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) && tty->ldisc.write_wakeup)
1208 (tty->ldisc.write_wakeup) (tty);
1209}
1210
1211static int mxser_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
1212{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001213 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001214 int retval;
1215 struct async_icount cprev, cnow; /* kernel counter temps */
1216 struct serial_icounter_struct __user *p_cuser;
1217 unsigned long templ;
1218 unsigned long flags;
1219 void __user *argp = (void __user *)arg;
1220
1221 if (tty->index == MXSER_PORTS)
1222 return mxser_ioctl_special(cmd, argp);
1223
1224 /* following add by Victor Yu. 01-05-2004 */
1225 if (cmd == MOXA_SET_OP_MODE || cmd == MOXA_GET_OP_MODE) {
Jiri Slaby3306ce32006-12-08 02:38:13 -08001226 int p;
1227 unsigned long opmode;
Jiri Slaby037ad482006-12-08 02:38:11 -08001228 static unsigned char ModeMask[] = { 0xfc, 0xf3, 0xcf, 0x3f };
1229 int shiftbit;
1230 unsigned char val, mask;
1231
Jiri Slaby55b307d2006-12-08 02:38:14 -08001232 p = tty->index % 4;
Jiri Slaby037ad482006-12-08 02:38:11 -08001233 if (cmd == MOXA_SET_OP_MODE) {
1234 if (get_user(opmode, (int __user *) argp))
1235 return -EFAULT;
1236 if (opmode != RS232_MODE &&
1237 opmode != RS485_2WIRE_MODE &&
1238 opmode != RS422_MODE &&
1239 opmode != RS485_4WIRE_MODE)
1240 return -EFAULT;
1241 mask = ModeMask[p];
1242 shiftbit = p * 2;
1243 val = inb(info->opmode_ioaddr);
1244 val &= mask;
1245 val |= (opmode << shiftbit);
1246 outb(val, info->opmode_ioaddr);
1247 } else {
1248 shiftbit = p * 2;
1249 opmode = inb(info->opmode_ioaddr) >> shiftbit;
1250 opmode &= OP_MODE_MASK;
1251 if (copy_to_user(argp, &opmode, sizeof(int)))
1252 return -EFAULT;
1253 }
1254 return 0;
1255 }
1256 /* above add by Victor Yu. 01-05-2004 */
1257
1258 if ((cmd != TIOCGSERIAL) && (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
1259 if (tty->flags & (1 << TTY_IO_ERROR))
1260 return -EIO;
1261 }
1262 switch (cmd) {
1263 case TCSBRK: /* SVID version: non-zero arg --> no break */
1264 retval = tty_check_change(tty);
1265 if (retval)
1266 return retval;
1267 tty_wait_until_sent(tty, 0);
1268 if (!arg)
1269 mxser_send_break(info, HZ / 4); /* 1/4 second */
1270 return 0;
1271 case TCSBRKP: /* support for POSIX tcsendbreak() */
1272 retval = tty_check_change(tty);
1273 if (retval)
1274 return retval;
1275 tty_wait_until_sent(tty, 0);
1276 mxser_send_break(info, arg ? arg * (HZ / 10) : HZ / 4);
1277 return 0;
1278 case TIOCGSOFTCAR:
1279 return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *)argp);
1280 case TIOCSSOFTCAR:
1281 if (get_user(templ, (unsigned long __user *) argp))
1282 return -EFAULT;
1283 arg = templ;
1284 tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) | (arg ? CLOCAL : 0));
1285 return 0;
1286 case TIOCGSERIAL:
1287 return mxser_get_serial_info(info, argp);
1288 case TIOCSSERIAL:
1289 return mxser_set_serial_info(info, argp);
1290 case TIOCSERGETLSR: /* Get line status register */
1291 return mxser_get_lsr_info(info, argp);
1292 /*
1293 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1294 * - mask passed in arg for lines of interest
1295 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1296 * Caller should use TIOCGICOUNT to see which one it was
1297 */
1298 case TIOCMIWAIT: {
1299 DECLARE_WAITQUEUE(wait, current);
1300 int ret;
1301 spin_lock_irqsave(&info->slock, flags);
1302 cprev = info->icount; /* note the counters on entry */
1303 spin_unlock_irqrestore(&info->slock, flags);
1304
1305 add_wait_queue(&info->delta_msr_wait, &wait);
1306 while (1) {
1307 spin_lock_irqsave(&info->slock, flags);
1308 cnow = info->icount; /* atomic copy */
1309 spin_unlock_irqrestore(&info->slock, flags);
1310
1311 set_current_state(TASK_INTERRUPTIBLE);
1312 if (((arg & TIOCM_RNG) &&
1313 (cnow.rng != cprev.rng)) ||
1314 ((arg & TIOCM_DSR) &&
1315 (cnow.dsr != cprev.dsr)) ||
1316 ((arg & TIOCM_CD) &&
1317 (cnow.dcd != cprev.dcd)) ||
1318 ((arg & TIOCM_CTS) &&
1319 (cnow.cts != cprev.cts))) {
1320 ret = 0;
1321 break;
1322 }
1323 /* see if a signal did it */
1324 if (signal_pending(current)) {
1325 ret = -ERESTARTSYS;
1326 break;
1327 }
1328 cprev = cnow;
1329 }
1330 current->state = TASK_RUNNING;
1331 remove_wait_queue(&info->delta_msr_wait, &wait);
1332 break;
1333 }
1334 /* NOTREACHED */
1335 /*
1336 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1337 * Return: write counters to the user passed counter struct
1338 * NB: both 1->0 and 0->1 transitions are counted except for
1339 * RI where only 0->1 is counted.
1340 */
1341 case TIOCGICOUNT:
1342 spin_lock_irqsave(&info->slock, flags);
1343 cnow = info->icount;
1344 spin_unlock_irqrestore(&info->slock, flags);
1345 p_cuser = argp;
1346 /* modified by casper 1/11/2000 */
1347 if (put_user(cnow.frame, &p_cuser->frame))
1348 return -EFAULT;
1349 if (put_user(cnow.brk, &p_cuser->brk))
1350 return -EFAULT;
1351 if (put_user(cnow.overrun, &p_cuser->overrun))
1352 return -EFAULT;
1353 if (put_user(cnow.buf_overrun, &p_cuser->buf_overrun))
1354 return -EFAULT;
1355 if (put_user(cnow.parity, &p_cuser->parity))
1356 return -EFAULT;
1357 if (put_user(cnow.rx, &p_cuser->rx))
1358 return -EFAULT;
1359 if (put_user(cnow.tx, &p_cuser->tx))
1360 return -EFAULT;
1361 put_user(cnow.cts, &p_cuser->cts);
1362 put_user(cnow.dsr, &p_cuser->dsr);
1363 put_user(cnow.rng, &p_cuser->rng);
1364 put_user(cnow.dcd, &p_cuser->dcd);
1365 return 0;
1366 case MOXA_HighSpeedOn:
1367 return put_user(info->baud_base != 115200 ? 1 : 0, (int __user *)argp);
1368 case MOXA_SDS_RSTICOUNTER: {
1369 info->mon_data.rxcnt = 0;
1370 info->mon_data.txcnt = 0;
1371 return 0;
1372 }
1373/* (above) added by James. */
1374 case MOXA_ASPP_SETBAUD:{
1375 long baud;
1376 if (get_user(baud, (long __user *)argp))
1377 return -EFAULT;
1378 mxser_set_baud(info, baud);
1379 return 0;
1380 }
1381 case MOXA_ASPP_GETBAUD:
1382 if (copy_to_user(argp, &info->realbaud, sizeof(long)))
1383 return -EFAULT;
1384
1385 return 0;
1386
1387 case MOXA_ASPP_OQUEUE:{
1388 int len, lsr;
1389
1390 len = mxser_chars_in_buffer(tty);
1391
Jiri Slaby55b307d2006-12-08 02:38:14 -08001392 lsr = inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT;
Jiri Slaby037ad482006-12-08 02:38:11 -08001393
1394 len += (lsr ? 0 : 1);
1395
1396 if (copy_to_user(argp, &len, sizeof(int)))
1397 return -EFAULT;
1398
1399 return 0;
1400 }
1401 case MOXA_ASPP_MON: {
1402 int mcr, status;
1403
1404 /* info->mon_data.ser_param = tty->termios->c_cflag; */
1405
Jiri Slaby55b307d2006-12-08 02:38:14 -08001406 status = mxser_get_msr(info->ioaddr, 1, tty->index);
Jiri Slaby037ad482006-12-08 02:38:11 -08001407 mxser_check_modem_status(info, status);
1408
Jiri Slaby55b307d2006-12-08 02:38:14 -08001409 mcr = inb(info->ioaddr + UART_MCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08001410 if (mcr & MOXA_MUST_MCR_XON_FLAG)
1411 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFHOLD;
1412 else
1413 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFHOLD;
1414
1415 if (mcr & MOXA_MUST_MCR_TX_XON)
1416 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFXENT;
1417 else
1418 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFXENT;
1419
1420 if (info->tty->hw_stopped)
1421 info->mon_data.hold_reason |= NPPI_NOTIFY_CTSHOLD;
1422 else
1423 info->mon_data.hold_reason &= ~NPPI_NOTIFY_CTSHOLD;
1424
1425 if (copy_to_user(argp, &info->mon_data,
1426 sizeof(struct mxser_mon)))
1427 return -EFAULT;
1428
1429 return 0;
1430 }
1431
1432 case MOXA_ASPP_LSTATUS: {
1433 if (copy_to_user(argp, &info->err_shadow,
1434 sizeof(unsigned char)))
1435 return -EFAULT;
1436
1437 info->err_shadow = 0;
1438 return 0;
1439 }
1440 case MOXA_SET_BAUD_METHOD: {
1441 int method;
1442
1443 if (get_user(method, (int __user *)argp))
1444 return -EFAULT;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001445 mxser_set_baud_method[tty->index] = method;
Jiri Slaby037ad482006-12-08 02:38:11 -08001446 if (copy_to_user(argp, &method, sizeof(int)))
1447 return -EFAULT;
1448
1449 return 0;
1450 }
1451 default:
1452 return -ENOIOCTLCMD;
1453 }
1454 return 0;
1455}
1456
1457#ifndef CMSPAR
1458#define CMSPAR 010000000000
1459#endif
1460
1461static int mxser_ioctl_special(unsigned int cmd, void __user *argp)
1462{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001463 struct mxser_port *port;
1464 int result, status;
1465 unsigned int i, j;
Jiri Slaby037ad482006-12-08 02:38:11 -08001466
1467 switch (cmd) {
1468 case MOXA_GET_CONF:
Jiri Slaby55b307d2006-12-08 02:38:14 -08001469/* if (copy_to_user(argp, mxsercfg,
Jiri Slaby037ad482006-12-08 02:38:11 -08001470 sizeof(struct mxser_hwconf) * 4))
1471 return -EFAULT;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001472 return 0;*/
1473 return -ENXIO;
Jiri Slaby037ad482006-12-08 02:38:11 -08001474 case MOXA_GET_MAJOR:
1475 if (copy_to_user(argp, &ttymajor, sizeof(int)))
1476 return -EFAULT;
1477 return 0;
1478
1479 case MOXA_GET_CUMAJOR:
1480 if (copy_to_user(argp, &calloutmajor, sizeof(int)))
1481 return -EFAULT;
1482 return 0;
1483
1484 case MOXA_CHKPORTENABLE:
1485 result = 0;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001486
1487 for (i = 0; i < MXSER_BOARDS; i++)
1488 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++)
1489 if (mxser_boards[i].ports[j].ioaddr)
1490 result |= (1 << i);
1491
Jiri Slaby037ad482006-12-08 02:38:11 -08001492 return put_user(result, (unsigned long __user *)argp);
1493 case MOXA_GETDATACOUNT:
1494 if (copy_to_user(argp, &mxvar_log, sizeof(mxvar_log)))
1495 return -EFAULT;
1496 return 0;
1497 case MOXA_GETMSTATUS:
Jiri Slaby55b307d2006-12-08 02:38:14 -08001498 for (i = 0; i < MXSER_BOARDS; i++)
1499 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
1500 port = &mxser_boards[i].ports[j];
1501
1502 GMStatus[i].ri = 0;
1503 if (!port->ioaddr) {
1504 GMStatus[i].dcd = 0;
1505 GMStatus[i].dsr = 0;
1506 GMStatus[i].cts = 0;
1507 continue;
1508 }
1509
1510 if (!port->tty || !port->tty->termios)
1511 GMStatus[i].cflag =
1512 port->normal_termios.c_cflag;
1513 else
1514 GMStatus[i].cflag =
1515 port->tty->termios->c_cflag;
1516
1517 status = inb(port->ioaddr + UART_MSR);
1518 if (status & 0x80 /*UART_MSR_DCD */ )
1519 GMStatus[i].dcd = 1;
1520 else
1521 GMStatus[i].dcd = 0;
1522
1523 if (status & 0x20 /*UART_MSR_DSR */ )
1524 GMStatus[i].dsr = 1;
1525 else
1526 GMStatus[i].dsr = 0;
1527
1528
1529 if (status & 0x10 /*UART_MSR_CTS */ )
1530 GMStatus[i].cts = 1;
1531 else
1532 GMStatus[i].cts = 0;
Jiri Slaby037ad482006-12-08 02:38:11 -08001533 }
Jiri Slaby037ad482006-12-08 02:38:11 -08001534 if (copy_to_user(argp, GMStatus,
1535 sizeof(struct mxser_mstatus) * MXSER_PORTS))
1536 return -EFAULT;
1537 return 0;
1538 case MOXA_ASPP_MON_EXT: {
Jiri Slaby55b307d2006-12-08 02:38:14 -08001539 int status, p, shiftbit;
1540 unsigned long opmode;
1541 unsigned cflag, iflag;
Jiri Slaby037ad482006-12-08 02:38:11 -08001542
Jiri Slaby55b307d2006-12-08 02:38:14 -08001543 for (i = 0; i < MXSER_BOARDS; i++)
1544 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
1545 port = &mxser_boards[i].ports[j];
1546 if (!port->ioaddr)
Jiri Slaby037ad482006-12-08 02:38:11 -08001547 continue;
1548
Jiri Slaby55b307d2006-12-08 02:38:14 -08001549 status = mxser_get_msr(port->ioaddr, 0, i);
1550/* mxser_check_modem_status(port, status); */
1551
Jiri Slaby037ad482006-12-08 02:38:11 -08001552 if (status & UART_MSR_TERI)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001553 port->icount.rng++;
Jiri Slaby037ad482006-12-08 02:38:11 -08001554 if (status & UART_MSR_DDSR)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001555 port->icount.dsr++;
Jiri Slaby037ad482006-12-08 02:38:11 -08001556 if (status & UART_MSR_DDCD)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001557 port->icount.dcd++;
Jiri Slaby037ad482006-12-08 02:38:11 -08001558 if (status & UART_MSR_DCTS)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001559 port->icount.cts++;
Jiri Slaby037ad482006-12-08 02:38:11 -08001560
Jiri Slaby55b307d2006-12-08 02:38:14 -08001561 port->mon_data.modem_status = status;
1562 mon_data_ext.rx_cnt[i] = port->mon_data.rxcnt;
1563 mon_data_ext.tx_cnt[i] = port->mon_data.txcnt;
1564 mon_data_ext.up_rxcnt[i] =
1565 port->mon_data.up_rxcnt;
1566 mon_data_ext.up_txcnt[i] =
1567 port->mon_data.up_txcnt;
1568 mon_data_ext.modem_status[i] =
1569 port->mon_data.modem_status;
1570 mon_data_ext.baudrate[i] = port->realbaud;
Jiri Slaby037ad482006-12-08 02:38:11 -08001571
Jiri Slaby55b307d2006-12-08 02:38:14 -08001572 if (!port->tty || !port->tty->termios) {
1573 cflag = port->normal_termios.c_cflag;
1574 iflag = port->normal_termios.c_iflag;
Jiri Slaby037ad482006-12-08 02:38:11 -08001575 } else {
Jiri Slaby55b307d2006-12-08 02:38:14 -08001576 cflag = port->tty->termios->c_cflag;
1577 iflag = port->tty->termios->c_iflag;
Jiri Slaby037ad482006-12-08 02:38:11 -08001578 }
1579
1580 mon_data_ext.databits[i] = cflag & CSIZE;
1581
1582 mon_data_ext.stopbits[i] = cflag & CSTOPB;
1583
Jiri Slaby55b307d2006-12-08 02:38:14 -08001584 mon_data_ext.parity[i] =
1585 cflag & (PARENB | PARODD | CMSPAR);
Jiri Slaby037ad482006-12-08 02:38:11 -08001586
1587 mon_data_ext.flowctrl[i] = 0x00;
1588
1589 if (cflag & CRTSCTS)
1590 mon_data_ext.flowctrl[i] |= 0x03;
1591
1592 if (iflag & (IXON | IXOFF))
1593 mon_data_ext.flowctrl[i] |= 0x0C;
1594
Jiri Slaby55b307d2006-12-08 02:38:14 -08001595 if (port->type == PORT_16550A)
Jiri Slaby037ad482006-12-08 02:38:11 -08001596 mon_data_ext.fifo[i] = 1;
1597 else
1598 mon_data_ext.fifo[i] = 0;
1599
1600 p = i % 4;
1601 shiftbit = p * 2;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001602 opmode = inb(port->opmode_ioaddr) >> shiftbit;
Jiri Slaby037ad482006-12-08 02:38:11 -08001603 opmode &= OP_MODE_MASK;
1604
1605 mon_data_ext.iftype[i] = opmode;
1606
1607 }
Jiri Slaby55b307d2006-12-08 02:38:14 -08001608 if (copy_to_user(argp, &mon_data_ext,
1609 sizeof(mon_data_ext)))
Jiri Slaby037ad482006-12-08 02:38:11 -08001610 return -EFAULT;
1611
1612 return 0;
1613
Jiri Slaby55b307d2006-12-08 02:38:14 -08001614 } default:
Jiri Slaby037ad482006-12-08 02:38:11 -08001615 return -ENOIOCTLCMD;
1616 }
1617 return 0;
1618}
1619
1620static void mxser_stoprx(struct tty_struct *tty)
1621{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001622 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001623
1624 info->ldisc_stop_rx = 1;
1625 if (I_IXOFF(tty)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001626 /* following add by Victor Yu. 09-02-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08001627 if (info->board->chip_flag) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001628 info->IER &= ~MOXA_MUST_RECV_ISR;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001629 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001630 } else if (!(info->flags & ASYNC_CLOSING)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001631 info->x_char = STOP_CHAR(tty);
Jiri Slaby55b307d2006-12-08 02:38:14 -08001632 outb(0, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001633 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001634 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001635 }
Jiri Slaby037ad482006-12-08 02:38:11 -08001636 }
1637
1638 if (info->tty->termios->c_cflag & CRTSCTS) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001639 info->MCR &= ~UART_MCR_RTS;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001640 outb(info->MCR, info->ioaddr + UART_MCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08001641 }
1642}
1643
1644static void mxser_startrx(struct tty_struct *tty)
1645{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001646 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001647
1648 info->ldisc_stop_rx = 0;
1649 if (I_IXOFF(tty)) {
1650 if (info->x_char)
1651 info->x_char = 0;
1652 else {
Jiri Slaby037ad482006-12-08 02:38:11 -08001653 /* following add by Victor Yu. 09-02-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08001654 if (info->board->chip_flag) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001655 info->IER |= MOXA_MUST_RECV_ISR;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001656 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001657 } else if (!(info->flags & ASYNC_CLOSING)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001658 info->x_char = START_CHAR(tty);
Jiri Slaby55b307d2006-12-08 02:38:14 -08001659 outb(0, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001660 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001661 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001662 }
Jiri Slaby037ad482006-12-08 02:38:11 -08001663 }
1664 }
1665
1666 if (info->tty->termios->c_cflag & CRTSCTS) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001667 info->MCR |= UART_MCR_RTS;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001668 outb(info->MCR, info->ioaddr + UART_MCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08001669 }
1670}
1671
1672/*
1673 * This routine is called by the upper-layer tty layer to signal that
1674 * incoming characters should be throttled.
1675 */
1676static void mxser_throttle(struct tty_struct *tty)
1677{
Jiri Slaby037ad482006-12-08 02:38:11 -08001678 mxser_stoprx(tty);
Jiri Slaby037ad482006-12-08 02:38:11 -08001679}
1680
1681static void mxser_unthrottle(struct tty_struct *tty)
1682{
Jiri Slaby037ad482006-12-08 02:38:11 -08001683 mxser_startrx(tty);
Jiri Slaby037ad482006-12-08 02:38:11 -08001684}
1685
1686static void mxser_set_termios(struct tty_struct *tty, struct termios *old_termios)
1687{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001688 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001689 unsigned long flags;
1690
1691 if ((tty->termios->c_cflag != old_termios->c_cflag) ||
1692 (RELEVANT_IFLAG(tty->termios->c_iflag) != RELEVANT_IFLAG(old_termios->c_iflag))) {
1693
1694 mxser_change_speed(info, old_termios);
1695
1696 if ((old_termios->c_cflag & CRTSCTS) &&
1697 !(tty->termios->c_cflag & CRTSCTS)) {
1698 tty->hw_stopped = 0;
1699 mxser_start(tty);
1700 }
1701 }
1702
1703/* Handle sw stopped */
1704 if ((old_termios->c_iflag & IXON) &&
1705 !(tty->termios->c_iflag & IXON)) {
1706 tty->stopped = 0;
1707
1708 /* following add by Victor Yu. 09-02-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08001709 if (info->board->chip_flag) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001710 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby55b307d2006-12-08 02:38:14 -08001711 DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
Jiri Slaby037ad482006-12-08 02:38:11 -08001712 spin_unlock_irqrestore(&info->slock, flags);
1713 }
1714 /* above add by Victor Yu. 09-02-2002 */
1715
1716 mxser_start(tty);
1717 }
1718}
1719
1720/*
1721 * mxser_stop() and mxser_start()
1722 *
1723 * This routines are called before setting or resetting tty->stopped.
1724 * They enable or disable transmitter interrupts, as necessary.
1725 */
1726static void mxser_stop(struct tty_struct *tty)
1727{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001728 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001729 unsigned long flags;
1730
1731 spin_lock_irqsave(&info->slock, flags);
1732 if (info->IER & UART_IER_THRI) {
1733 info->IER &= ~UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001734 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001735 }
1736 spin_unlock_irqrestore(&info->slock, flags);
1737}
1738
1739static void mxser_start(struct tty_struct *tty)
1740{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001741 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001742 unsigned long flags;
1743
1744 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001745 if (info->xmit_cnt && info->xmit_buf
1746 /* && !(info->IER & UART_IER_THRI) */) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08001747 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001748 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001749 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001750 }
1751 spin_unlock_irqrestore(&info->slock, flags);
1752}
1753
1754/*
1755 * mxser_wait_until_sent() --- wait until the transmitter is empty
1756 */
1757static void mxser_wait_until_sent(struct tty_struct *tty, int timeout)
1758{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001759 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001760 unsigned long orig_jiffies, char_time;
1761 int lsr;
1762
1763 if (info->type == PORT_UNKNOWN)
1764 return;
1765
1766 if (info->xmit_fifo_size == 0)
1767 return; /* Just in case.... */
1768
1769 orig_jiffies = jiffies;
1770 /*
1771 * Set the check interval to be 1/5 of the estimated time to
1772 * send a single character, and make it at least 1. The check
1773 * interval should also be less than the timeout.
1774 *
1775 * Note: we have to use pretty tight timings here to satisfy
1776 * the NIST-PCTS.
1777 */
1778 char_time = (info->timeout - HZ / 50) / info->xmit_fifo_size;
1779 char_time = char_time / 5;
1780 if (char_time == 0)
1781 char_time = 1;
1782 if (timeout && timeout < char_time)
1783 char_time = timeout;
1784 /*
1785 * If the transmitter hasn't cleared in twice the approximate
1786 * amount of time to send the entire FIFO, it probably won't
1787 * ever clear. This assumes the UART isn't doing flow
1788 * control, which is currently the case. Hence, if it ever
1789 * takes longer than info->timeout, this is probably due to a
1790 * UART bug of some kind. So, we clamp the timeout parameter at
1791 * 2*info->timeout.
1792 */
1793 if (!timeout || timeout > 2 * info->timeout)
1794 timeout = 2 * info->timeout;
1795#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1796 printk(KERN_DEBUG "In rs_wait_until_sent(%d) check=%lu...",
1797 timeout, char_time);
1798 printk("jiff=%lu...", jiffies);
1799#endif
Jiri Slaby55b307d2006-12-08 02:38:14 -08001800 while (!((lsr = inb(info->ioaddr + UART_LSR)) & UART_LSR_TEMT)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001801#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1802 printk("lsr = %d (jiff=%lu)...", lsr, jiffies);
1803#endif
1804 schedule_timeout_interruptible(char_time);
1805 if (signal_pending(current))
1806 break;
1807 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1808 break;
1809 }
1810 set_current_state(TASK_RUNNING);
1811
1812#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1813 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
1814#endif
1815}
1816
1817
1818/*
1819 * This routine is called by tty_hangup() when a hangup is signaled.
1820 */
1821void mxser_hangup(struct tty_struct *tty)
1822{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001823 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001824
1825 mxser_flush_buffer(tty);
1826 mxser_shutdown(info);
1827 info->event = 0;
1828 info->count = 0;
1829 info->flags &= ~ASYNC_NORMAL_ACTIVE;
1830 info->tty = NULL;
1831 wake_up_interruptible(&info->open_wait);
1832}
1833
1834
1835/* added by James 03-12-2004. */
1836/*
1837 * mxser_rs_break() --- routine which turns the break handling on or off
1838 */
1839static void mxser_rs_break(struct tty_struct *tty, int break_state)
1840{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001841 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001842 unsigned long flags;
1843
1844 spin_lock_irqsave(&info->slock, flags);
1845 if (break_state == -1)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001846 outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
1847 info->ioaddr + UART_LCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08001848 else
Jiri Slaby55b307d2006-12-08 02:38:14 -08001849 outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
1850 info->ioaddr + UART_LCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08001851 spin_unlock_irqrestore(&info->slock, flags);
1852}
1853
1854/* (above) added by James. */
1855
1856
1857/*
1858 * This is the serial driver's generic interrupt routine
1859 */
1860static irqreturn_t mxser_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1861{
1862 int status, iir, i;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001863 struct mxser_board *brd = NULL;
1864 struct mxser_port *port;
Jiri Slaby037ad482006-12-08 02:38:11 -08001865 int max, irqbits, bits, msr;
1866 int pass_counter = 0;
Jiri Slaby3306ce32006-12-08 02:38:13 -08001867 unsigned int int_cnt;
Jiri Slaby037ad482006-12-08 02:38:11 -08001868 int handled = IRQ_NONE;
1869
Jiri Slaby037ad482006-12-08 02:38:11 -08001870 /* spin_lock(&gm_lock); */
1871
Jiri Slaby55b307d2006-12-08 02:38:14 -08001872 for (i = 0; i < MXSER_BOARDS; i++)
1873 if (dev_id == &mxser_boards[i]) {
1874 brd = dev_id;
Jiri Slaby037ad482006-12-08 02:38:11 -08001875 break;
1876 }
Jiri Slaby037ad482006-12-08 02:38:11 -08001877
1878 if (i == MXSER_BOARDS)
1879 goto irq_stop;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001880 if (brd == NULL)
Jiri Slaby037ad482006-12-08 02:38:11 -08001881 goto irq_stop;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001882 max = mxser_numports[brd->board_type - 1];
Jiri Slaby037ad482006-12-08 02:38:11 -08001883 while (1) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08001884 irqbits = inb(brd->vector) & brd->vector_mask;
1885 if (irqbits == brd->vector_mask)
Jiri Slaby037ad482006-12-08 02:38:11 -08001886 break;
1887
1888 handled = IRQ_HANDLED;
1889 for (i = 0, bits = 1; i < max; i++, irqbits |= bits, bits <<= 1) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08001890 if (irqbits == brd->vector_mask)
Jiri Slaby037ad482006-12-08 02:38:11 -08001891 break;
1892 if (bits & irqbits)
1893 continue;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001894 port = &brd->ports[i];
Jiri Slaby037ad482006-12-08 02:38:11 -08001895
Jiri Slaby3306ce32006-12-08 02:38:13 -08001896 int_cnt = 0;
1897 do {
1898 /* following add by Victor Yu. 09-13-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08001899 iir = inb(port->ioaddr + UART_IIR);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001900 if (iir & UART_IIR_NO_INT)
1901 break;
1902 iir &= MOXA_MUST_IIR_MASK;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001903 if (!port->tty) {
1904 status = inb(port->ioaddr + UART_LSR);
1905 outb(0x27, port->ioaddr + UART_FCR);
1906 inb(port->ioaddr + UART_MSR);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001907 break;
Jiri Slaby037ad482006-12-08 02:38:11 -08001908 }
Jiri Slaby037ad482006-12-08 02:38:11 -08001909 /* above add by Victor Yu. 09-13-2002 */
1910
Jiri Slaby3306ce32006-12-08 02:38:13 -08001911 /* following add by Victor Yu. 09-02-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08001912 status = inb(port->ioaddr + UART_LSR);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001913
1914 if (status & UART_LSR_PE)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001915 port->err_shadow |= NPPI_NOTIFY_PARITY;
Jiri Slaby3306ce32006-12-08 02:38:13 -08001916 if (status & UART_LSR_FE)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001917 port->err_shadow |= NPPI_NOTIFY_FRAMING;
Jiri Slaby3306ce32006-12-08 02:38:13 -08001918 if (status & UART_LSR_OE)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001919 port->err_shadow |=
Jiri Slaby3306ce32006-12-08 02:38:13 -08001920 NPPI_NOTIFY_HW_OVERRUN;
1921 if (status & UART_LSR_BI)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001922 port->err_shadow |= NPPI_NOTIFY_BREAK;
Jiri Slaby3306ce32006-12-08 02:38:13 -08001923
Jiri Slaby55b307d2006-12-08 02:38:14 -08001924 if (port->board->chip_flag) {
Jiri Slaby3306ce32006-12-08 02:38:13 -08001925 /*
1926 if ( (status & 0x02) && !(status & 0x01) ) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08001927 outb(port->ioaddr+UART_FCR, 0x23);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001928 continue;
1929 }
1930 */
1931 if (iir == MOXA_MUST_IIR_GDA ||
1932 iir == MOXA_MUST_IIR_RDA ||
1933 iir == MOXA_MUST_IIR_RTO ||
1934 iir == MOXA_MUST_IIR_LSR)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001935 mxser_receive_chars(port,
Jiri Slaby3306ce32006-12-08 02:38:13 -08001936 &status);
1937
1938 } else {
1939 /* above add by Victor Yu. 09-02-2002 */
1940
Jiri Slaby55b307d2006-12-08 02:38:14 -08001941 status &= port->read_status_mask;
Jiri Slaby3306ce32006-12-08 02:38:13 -08001942 if (status & UART_LSR_DR)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001943 mxser_receive_chars(port,
Jiri Slaby3306ce32006-12-08 02:38:13 -08001944 &status);
Jiri Slaby037ad482006-12-08 02:38:11 -08001945 }
Jiri Slaby55b307d2006-12-08 02:38:14 -08001946 msr = inb(port->ioaddr + UART_MSR);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001947 if (msr & UART_MSR_ANY_DELTA)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001948 mxser_check_modem_status(port, msr);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001949
1950 /* following add by Victor Yu. 09-13-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08001951 if (port->board->chip_flag) {
Jiri Slaby3306ce32006-12-08 02:38:13 -08001952 if (iir == 0x02 && (status &
1953 UART_LSR_THRE))
Jiri Slaby55b307d2006-12-08 02:38:14 -08001954 mxser_transmit_chars(port);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001955 } else {
1956 /* above add by Victor Yu. 09-13-2002 */
1957
1958 if (status & UART_LSR_THRE)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001959 mxser_transmit_chars(port);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001960 }
Jiri Slaby3306ce32006-12-08 02:38:13 -08001961 } while (int_cnt++ < MXSER_ISR_PASS_LIMIT);
Jiri Slaby037ad482006-12-08 02:38:11 -08001962 }
Jiri Slaby3306ce32006-12-08 02:38:13 -08001963 if (pass_counter++ > MXSER_ISR_PASS_LIMIT)
Jiri Slaby037ad482006-12-08 02:38:11 -08001964 break; /* Prevent infinite loops */
Jiri Slaby037ad482006-12-08 02:38:11 -08001965 }
1966
1967 irq_stop:
1968 /* spin_unlock(&gm_lock); */
1969 return handled;
1970}
1971
Jiri Slaby55b307d2006-12-08 02:38:14 -08001972static void mxser_receive_chars(struct mxser_port *port, int *status)
Jiri Slaby037ad482006-12-08 02:38:11 -08001973{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001974 struct tty_struct *tty = port->tty;
Jiri Slaby037ad482006-12-08 02:38:11 -08001975 unsigned char ch, gdl;
1976 int ignored = 0;
1977 int cnt = 0;
1978 int recv_room;
1979 int max = 256;
1980 unsigned long flags;
1981
Jiri Slaby55b307d2006-12-08 02:38:14 -08001982 spin_lock_irqsave(&port->slock, flags);
Jiri Slaby037ad482006-12-08 02:38:11 -08001983
1984 recv_room = tty->receive_room;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001985 if ((recv_room == 0) && (!port->ldisc_stop_rx)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001986 /* mxser_throttle(tty); */
1987 mxser_stoprx(tty);
1988 /* return; */
1989 }
1990
1991 /* following add by Victor Yu. 09-02-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08001992 if (port->board->chip_flag != MOXA_OTHER_UART) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001993
Jiri Slaby3306ce32006-12-08 02:38:13 -08001994 if (*status & UART_LSR_SPECIAL)
Jiri Slaby037ad482006-12-08 02:38:11 -08001995 goto intr_old;
Jiri Slaby037ad482006-12-08 02:38:11 -08001996 /* following add by Victor Yu. 02-11-2004 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08001997 if (port->board->chip_flag == MOXA_MUST_MU860_HWID &&
Jiri Slaby037ad482006-12-08 02:38:11 -08001998 (*status & MOXA_MUST_LSR_RERR))
1999 goto intr_old;
2000 /* above add by Victor Yu. 02-14-2004 */
2001 if (*status & MOXA_MUST_LSR_RERR)
2002 goto intr_old;
2003
Jiri Slaby55b307d2006-12-08 02:38:14 -08002004 gdl = inb(port->ioaddr + MOXA_MUST_GDL_REGISTER);
Jiri Slaby037ad482006-12-08 02:38:11 -08002005
2006 /* add by Victor Yu. 02-11-2004 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002007 if (port->board->chip_flag == MOXA_MUST_MU150_HWID)
Jiri Slaby037ad482006-12-08 02:38:11 -08002008 gdl &= MOXA_MUST_GDL_MASK;
2009 if (gdl >= recv_room) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002010 if (!port->ldisc_stop_rx) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002011 /* mxser_throttle(tty); */
2012 mxser_stoprx(tty);
2013 }
2014 /* return; */
2015 }
2016 while (gdl--) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002017 ch = inb(port->ioaddr + UART_RX);
Jiri Slaby037ad482006-12-08 02:38:11 -08002018 tty_insert_flip_char(tty, ch, 0);
2019 cnt++;
Jiri Slaby037ad482006-12-08 02:38:11 -08002020 }
2021 goto end_intr;
2022 }
2023 intr_old:
2024 /* above add by Victor Yu. 09-02-2002 */
2025
2026 do {
2027 if (max-- < 0)
2028 break;
Jiri Slaby037ad482006-12-08 02:38:11 -08002029
Jiri Slaby55b307d2006-12-08 02:38:14 -08002030 ch = inb(port->ioaddr + UART_RX);
Jiri Slaby037ad482006-12-08 02:38:11 -08002031 /* following add by Victor Yu. 09-02-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002032 if (port->board->chip_flag && (*status & UART_LSR_OE)
Jiri Slaby3306ce32006-12-08 02:38:13 -08002033 /*&& !(*status&UART_LSR_DR) */)
Jiri Slaby55b307d2006-12-08 02:38:14 -08002034 outb(0x23, port->ioaddr + UART_FCR);
2035 *status &= port->read_status_mask;
Jiri Slaby037ad482006-12-08 02:38:11 -08002036 /* above add by Victor Yu. 09-02-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002037 if (*status & port->ignore_status_mask) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002038 if (++ignored > 100)
2039 break;
2040 } else {
2041 char flag = 0;
2042 if (*status & UART_LSR_SPECIAL) {
2043 if (*status & UART_LSR_BI) {
2044 flag = TTY_BREAK;
2045/* added by casper 1/11/2000 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002046 port->icount.brk++;
Jiri Slaby3306ce32006-12-08 02:38:13 -08002047
Jiri Slaby55b307d2006-12-08 02:38:14 -08002048 if (port->flags & ASYNC_SAK)
Jiri Slaby037ad482006-12-08 02:38:11 -08002049 do_SAK(tty);
2050 } else if (*status & UART_LSR_PE) {
2051 flag = TTY_PARITY;
2052/* added by casper 1/11/2000 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002053 port->icount.parity++;
Jiri Slaby037ad482006-12-08 02:38:11 -08002054 } else if (*status & UART_LSR_FE) {
2055 flag = TTY_FRAME;
2056/* added by casper 1/11/2000 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002057 port->icount.frame++;
Jiri Slaby037ad482006-12-08 02:38:11 -08002058 } else if (*status & UART_LSR_OE) {
2059 flag = TTY_OVERRUN;
2060/* added by casper 1/11/2000 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002061 port->icount.overrun++;
Jiri Slaby3306ce32006-12-08 02:38:13 -08002062 } else
2063 flags = TTY_BREAK;
2064 } else
2065 flags = 0;
Jiri Slaby037ad482006-12-08 02:38:11 -08002066 tty_insert_flip_char(tty, ch, flag);
2067 cnt++;
2068 if (cnt >= recv_room) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002069 if (!port->ldisc_stop_rx) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002070 /* mxser_throttle(tty); */
2071 mxser_stoprx(tty);
2072 }
2073 break;
2074 }
2075
2076 }
2077
2078 /* following add by Victor Yu. 09-02-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002079 if (port->board->chip_flag)
Jiri Slaby037ad482006-12-08 02:38:11 -08002080 break;
Jiri Slaby037ad482006-12-08 02:38:11 -08002081
2082 /* mask by Victor Yu. 09-02-2002
Jiri Slaby55b307d2006-12-08 02:38:14 -08002083 *status = inb(port->ioaddr + UART_LSR) & port->read_status_mask;
Jiri Slaby037ad482006-12-08 02:38:11 -08002084 */
2085 /* following add by Victor Yu. 09-02-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002086 *status = inb(port->ioaddr + UART_LSR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002087 /* above add by Victor Yu. 09-02-2002 */
2088 } while (*status & UART_LSR_DR);
2089
2090end_intr: /* add by Victor Yu. 09-02-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002091 mxvar_log.rxcnt[port->tty->index] += cnt;
2092 port->mon_data.rxcnt += cnt;
2093 port->mon_data.up_rxcnt += cnt;
2094 spin_unlock_irqrestore(&port->slock, flags);
Jiri Slaby037ad482006-12-08 02:38:11 -08002095
2096 tty_flip_buffer_push(tty);
2097}
2098
Jiri Slaby55b307d2006-12-08 02:38:14 -08002099static void mxser_transmit_chars(struct mxser_port *port)
Jiri Slaby037ad482006-12-08 02:38:11 -08002100{
2101 int count, cnt;
2102 unsigned long flags;
2103
Jiri Slaby55b307d2006-12-08 02:38:14 -08002104 spin_lock_irqsave(&port->slock, flags);
Jiri Slaby037ad482006-12-08 02:38:11 -08002105
Jiri Slaby55b307d2006-12-08 02:38:14 -08002106 if (port->x_char) {
2107 outb(port->x_char, port->ioaddr + UART_TX);
2108 port->x_char = 0;
2109 mxvar_log.txcnt[port->tty->index]++;
2110 port->mon_data.txcnt++;
2111 port->mon_data.up_txcnt++;
Jiri Slaby037ad482006-12-08 02:38:11 -08002112
2113/* added by casper 1/11/2000 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002114 port->icount.tx++;
Jiri Slaby3306ce32006-12-08 02:38:13 -08002115 goto unlock;
Jiri Slaby037ad482006-12-08 02:38:11 -08002116 }
2117
Jiri Slaby55b307d2006-12-08 02:38:14 -08002118 if (port->xmit_buf == 0)
Jiri Slaby3306ce32006-12-08 02:38:13 -08002119 goto unlock;
Jiri Slaby037ad482006-12-08 02:38:11 -08002120
Jiri Slaby55b307d2006-12-08 02:38:14 -08002121 if (port->xmit_cnt == 0) {
2122 if (port->xmit_cnt < WAKEUP_CHARS) { /* XXX what's this for?? */
2123 set_bit(MXSER_EVENT_TXLOW, &port->event);
2124 schedule_work(&port->tqueue);
Jiri Slaby3306ce32006-12-08 02:38:13 -08002125 }
2126 goto unlock;
2127 }
Jiri Slaby55b307d2006-12-08 02:38:14 -08002128 if (port->tty->stopped || (port->tty->hw_stopped &&
2129 (port->type != PORT_16550A) &&
2130 (!port->board->chip_flag))) {
2131 port->IER &= ~UART_IER_THRI;
2132 outb(port->IER, port->ioaddr + UART_IER);
Jiri Slaby3306ce32006-12-08 02:38:13 -08002133 goto unlock;
Jiri Slaby037ad482006-12-08 02:38:11 -08002134 }
2135
Jiri Slaby55b307d2006-12-08 02:38:14 -08002136 cnt = port->xmit_cnt;
2137 count = port->xmit_fifo_size;
Jiri Slaby037ad482006-12-08 02:38:11 -08002138 do {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002139 outb(port->xmit_buf[port->xmit_tail++],
2140 port->ioaddr + UART_TX);
2141 port->xmit_tail = port->xmit_tail & (SERIAL_XMIT_SIZE - 1);
2142 if (--port->xmit_cnt <= 0)
Jiri Slaby037ad482006-12-08 02:38:11 -08002143 break;
2144 } while (--count > 0);
Jiri Slaby55b307d2006-12-08 02:38:14 -08002145 mxvar_log.txcnt[port->tty->index] += (cnt - port->xmit_cnt);
Jiri Slaby037ad482006-12-08 02:38:11 -08002146
2147/* added by James 03-12-2004. */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002148 port->mon_data.txcnt += (cnt - port->xmit_cnt);
2149 port->mon_data.up_txcnt += (cnt - port->xmit_cnt);
Jiri Slaby037ad482006-12-08 02:38:11 -08002150
2151/* added by casper 1/11/2000 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002152 port->icount.tx += (cnt - port->xmit_cnt);
Jiri Slaby037ad482006-12-08 02:38:11 -08002153
Jiri Slaby55b307d2006-12-08 02:38:14 -08002154 if (port->xmit_cnt < WAKEUP_CHARS) {
2155 set_bit(MXSER_EVENT_TXLOW, &port->event);
2156 schedule_work(&port->tqueue);
Jiri Slaby037ad482006-12-08 02:38:11 -08002157 }
Jiri Slaby55b307d2006-12-08 02:38:14 -08002158 if (port->xmit_cnt <= 0) {
2159 port->IER &= ~UART_IER_THRI;
2160 outb(port->IER, port->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08002161 }
Jiri Slaby3306ce32006-12-08 02:38:13 -08002162unlock:
Jiri Slaby55b307d2006-12-08 02:38:14 -08002163 spin_unlock_irqrestore(&port->slock, flags);
Jiri Slaby037ad482006-12-08 02:38:11 -08002164}
2165
Jiri Slaby55b307d2006-12-08 02:38:14 -08002166static void mxser_check_modem_status(struct mxser_port *port, int status)
Jiri Slaby037ad482006-12-08 02:38:11 -08002167{
2168 /* update input line counters */
2169 if (status & UART_MSR_TERI)
Jiri Slaby55b307d2006-12-08 02:38:14 -08002170 port->icount.rng++;
Jiri Slaby037ad482006-12-08 02:38:11 -08002171 if (status & UART_MSR_DDSR)
Jiri Slaby55b307d2006-12-08 02:38:14 -08002172 port->icount.dsr++;
Jiri Slaby037ad482006-12-08 02:38:11 -08002173 if (status & UART_MSR_DDCD)
Jiri Slaby55b307d2006-12-08 02:38:14 -08002174 port->icount.dcd++;
Jiri Slaby037ad482006-12-08 02:38:11 -08002175 if (status & UART_MSR_DCTS)
Jiri Slaby55b307d2006-12-08 02:38:14 -08002176 port->icount.cts++;
2177 port->mon_data.modem_status = status;
2178 wake_up_interruptible(&port->delta_msr_wait);
Jiri Slaby037ad482006-12-08 02:38:11 -08002179
Jiri Slaby55b307d2006-12-08 02:38:14 -08002180 if ((port->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002181 if (status & UART_MSR_DCD)
Jiri Slaby55b307d2006-12-08 02:38:14 -08002182 wake_up_interruptible(&port->open_wait);
2183 schedule_work(&port->tqueue);
Jiri Slaby037ad482006-12-08 02:38:11 -08002184 }
2185
Jiri Slaby55b307d2006-12-08 02:38:14 -08002186 if (port->flags & ASYNC_CTS_FLOW) {
2187 if (port->tty->hw_stopped) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002188 if (status & UART_MSR_CTS) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002189 port->tty->hw_stopped = 0;
Jiri Slaby037ad482006-12-08 02:38:11 -08002190
Jiri Slaby55b307d2006-12-08 02:38:14 -08002191 if ((port->type != PORT_16550A) &&
2192 (!port->board->chip_flag)) {
2193 outb(port->IER & ~UART_IER_THRI,
2194 port->ioaddr + UART_IER);
2195 port->IER |= UART_IER_THRI;
2196 outb(port->IER, port->ioaddr +
2197 UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08002198 }
Jiri Slaby55b307d2006-12-08 02:38:14 -08002199 set_bit(MXSER_EVENT_TXLOW, &port->event);
2200 schedule_work(&port->tqueue);
Jiri Slaby3306ce32006-12-08 02:38:13 -08002201 }
Jiri Slaby037ad482006-12-08 02:38:11 -08002202 } else {
2203 if (!(status & UART_MSR_CTS)) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002204 port->tty->hw_stopped = 1;
2205 if (port->type != PORT_16550A &&
2206 !port->board->chip_flag) {
2207 port->IER &= ~UART_IER_THRI;
2208 outb(port->IER, port->ioaddr +
2209 UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08002210 }
2211 }
2212 }
2213 }
2214}
2215
Jiri Slaby55b307d2006-12-08 02:38:14 -08002216static int mxser_block_til_ready(struct tty_struct *tty, struct file *filp, struct mxser_port *port)
Jiri Slaby037ad482006-12-08 02:38:11 -08002217{
2218 DECLARE_WAITQUEUE(wait, current);
2219 int retval;
2220 int do_clocal = 0;
2221 unsigned long flags;
2222
2223 /*
2224 * If non-blocking mode is set, or the port is not enabled,
2225 * then make the check up front and then exit.
2226 */
2227 if ((filp->f_flags & O_NONBLOCK) || (tty->flags & (1 << TTY_IO_ERROR))) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002228 port->flags |= ASYNC_NORMAL_ACTIVE;
Jiri Slaby037ad482006-12-08 02:38:11 -08002229 return 0;
2230 }
2231
2232 if (tty->termios->c_cflag & CLOCAL)
2233 do_clocal = 1;
2234
2235 /*
2236 * Block waiting for the carrier detect and the line to become
2237 * free (i.e., not in use by the callout). While we are in
Jiri Slaby55b307d2006-12-08 02:38:14 -08002238 * this loop, port->count is dropped by one, so that
Jiri Slaby037ad482006-12-08 02:38:11 -08002239 * mxser_close() knows when to free things. We restore it upon
2240 * exit, either normal or abnormal.
2241 */
2242 retval = 0;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002243 add_wait_queue(&port->open_wait, &wait);
Jiri Slaby037ad482006-12-08 02:38:11 -08002244
Jiri Slaby55b307d2006-12-08 02:38:14 -08002245 spin_lock_irqsave(&port->slock, flags);
Jiri Slaby037ad482006-12-08 02:38:11 -08002246 if (!tty_hung_up_p(filp))
Jiri Slaby55b307d2006-12-08 02:38:14 -08002247 port->count--;
2248 spin_unlock_irqrestore(&port->slock, flags);
2249 port->blocked_open++;
Jiri Slaby037ad482006-12-08 02:38:11 -08002250 while (1) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002251 spin_lock_irqsave(&port->slock, flags);
2252 outb(inb(port->ioaddr + UART_MCR) |
2253 UART_MCR_DTR | UART_MCR_RTS, port->ioaddr + UART_MCR);
2254 spin_unlock_irqrestore(&port->slock, flags);
Jiri Slaby037ad482006-12-08 02:38:11 -08002255 set_current_state(TASK_INTERRUPTIBLE);
Jiri Slaby55b307d2006-12-08 02:38:14 -08002256 if (tty_hung_up_p(filp) || !(port->flags & ASYNC_INITIALIZED)) {
2257 if (port->flags & ASYNC_HUP_NOTIFY)
Jiri Slaby037ad482006-12-08 02:38:11 -08002258 retval = -EAGAIN;
2259 else
2260 retval = -ERESTARTSYS;
2261 break;
2262 }
Jiri Slaby55b307d2006-12-08 02:38:14 -08002263 if (!(port->flags & ASYNC_CLOSING) &&
Jiri Slaby037ad482006-12-08 02:38:11 -08002264 (do_clocal ||
Jiri Slaby55b307d2006-12-08 02:38:14 -08002265 (inb(port->ioaddr + UART_MSR) & UART_MSR_DCD)))
Jiri Slaby037ad482006-12-08 02:38:11 -08002266 break;
2267 if (signal_pending(current)) {
2268 retval = -ERESTARTSYS;
2269 break;
2270 }
2271 schedule();
2272 }
2273 set_current_state(TASK_RUNNING);
Jiri Slaby55b307d2006-12-08 02:38:14 -08002274 remove_wait_queue(&port->open_wait, &wait);
Jiri Slaby037ad482006-12-08 02:38:11 -08002275 if (!tty_hung_up_p(filp))
Jiri Slaby55b307d2006-12-08 02:38:14 -08002276 port->count++;
2277 port->blocked_open--;
Jiri Slaby037ad482006-12-08 02:38:11 -08002278 if (retval)
2279 return retval;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002280 port->flags |= ASYNC_NORMAL_ACTIVE;
Jiri Slaby037ad482006-12-08 02:38:11 -08002281 return 0;
2282}
2283
Jiri Slaby55b307d2006-12-08 02:38:14 -08002284static int mxser_startup(struct mxser_port *info)
Jiri Slaby037ad482006-12-08 02:38:11 -08002285{
2286 unsigned long page;
2287 unsigned long flags;
2288
2289 page = __get_free_page(GFP_KERNEL);
2290 if (!page)
2291 return -ENOMEM;
2292
2293 spin_lock_irqsave(&info->slock, flags);
2294
2295 if (info->flags & ASYNC_INITIALIZED) {
2296 free_page(page);
2297 spin_unlock_irqrestore(&info->slock, flags);
2298 return 0;
2299 }
2300
Jiri Slaby55b307d2006-12-08 02:38:14 -08002301 if (!info->ioaddr || !info->type) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002302 if (info->tty)
2303 set_bit(TTY_IO_ERROR, &info->tty->flags);
2304 free_page(page);
2305 spin_unlock_irqrestore(&info->slock, flags);
2306 return 0;
2307 }
2308 if (info->xmit_buf)
2309 free_page(page);
2310 else
2311 info->xmit_buf = (unsigned char *) page;
2312
2313 /*
2314 * Clear the FIFO buffers and disable them
2315 * (they will be reenabled in mxser_change_speed())
2316 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002317 if (info->board->chip_flag)
Jiri Slaby037ad482006-12-08 02:38:11 -08002318 outb((UART_FCR_CLEAR_RCVR |
2319 UART_FCR_CLEAR_XMIT |
Jiri Slaby55b307d2006-12-08 02:38:14 -08002320 MOXA_MUST_FCR_GDA_MODE_ENABLE), info->ioaddr + UART_FCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002321 else
2322 outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
Jiri Slaby55b307d2006-12-08 02:38:14 -08002323 info->ioaddr + UART_FCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002324
2325 /*
2326 * At this point there's no way the LSR could still be 0xFF;
2327 * if it is, then bail out, because there's likely no UART
2328 * here.
2329 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002330 if (inb(info->ioaddr + UART_LSR) == 0xff) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002331 spin_unlock_irqrestore(&info->slock, flags);
2332 if (capable(CAP_SYS_ADMIN)) {
2333 if (info->tty)
2334 set_bit(TTY_IO_ERROR, &info->tty->flags);
2335 return 0;
2336 } else
2337 return -ENODEV;
2338 }
2339
2340 /*
2341 * Clear the interrupt registers.
2342 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002343 (void) inb(info->ioaddr + UART_LSR);
2344 (void) inb(info->ioaddr + UART_RX);
2345 (void) inb(info->ioaddr + UART_IIR);
2346 (void) inb(info->ioaddr + UART_MSR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002347
2348 /*
2349 * Now, initialize the UART
2350 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002351 outb(UART_LCR_WLEN8, info->ioaddr + UART_LCR); /* reset DLAB */
Jiri Slaby037ad482006-12-08 02:38:11 -08002352 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002353 outb(info->MCR, info->ioaddr + UART_MCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002354
2355 /*
2356 * Finally, enable interrupts
2357 */
2358 info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
2359 /* info->IER = UART_IER_RLSI | UART_IER_RDI; */
2360
2361 /* following add by Victor Yu. 08-30-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002362 if (info->board->chip_flag)
Jiri Slaby037ad482006-12-08 02:38:11 -08002363 info->IER |= MOXA_MUST_IER_EGDAI;
2364 /* above add by Victor Yu. 08-30-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002365 outb(info->IER, info->ioaddr + UART_IER); /* enable interrupts */
Jiri Slaby037ad482006-12-08 02:38:11 -08002366
2367 /*
2368 * And clear the interrupt registers again for luck.
2369 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002370 (void) inb(info->ioaddr + UART_LSR);
2371 (void) inb(info->ioaddr + UART_RX);
2372 (void) inb(info->ioaddr + UART_IIR);
2373 (void) inb(info->ioaddr + UART_MSR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002374
2375 if (info->tty)
2376 clear_bit(TTY_IO_ERROR, &info->tty->flags);
2377 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
2378
2379 /*
2380 * and set the speed of the serial port
2381 */
2382 spin_unlock_irqrestore(&info->slock, flags);
2383 mxser_change_speed(info, NULL);
2384
2385 info->flags |= ASYNC_INITIALIZED;
2386 return 0;
2387}
2388
2389/*
2390 * This routine will shutdown a serial port; interrupts maybe disabled, and
2391 * DTR is dropped if the hangup on close termio flag is on.
2392 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002393static void mxser_shutdown(struct mxser_port *info)
Jiri Slaby037ad482006-12-08 02:38:11 -08002394{
2395 unsigned long flags;
2396
2397 if (!(info->flags & ASYNC_INITIALIZED))
2398 return;
2399
2400 spin_lock_irqsave(&info->slock, flags);
2401
2402 /*
2403 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
2404 * here so the queue might never be waken up
2405 */
2406 wake_up_interruptible(&info->delta_msr_wait);
2407
2408 /*
2409 * Free the IRQ, if necessary
2410 */
2411 if (info->xmit_buf) {
2412 free_page((unsigned long) info->xmit_buf);
2413 info->xmit_buf = NULL;
2414 }
2415
2416 info->IER = 0;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002417 outb(0x00, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08002418
2419 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
2420 info->MCR &= ~(UART_MCR_DTR | UART_MCR_RTS);
Jiri Slaby55b307d2006-12-08 02:38:14 -08002421 outb(info->MCR, info->ioaddr + UART_MCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002422
2423 /* clear Rx/Tx FIFO's */
2424 /* following add by Victor Yu. 08-30-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002425 if (info->board->chip_flag)
2426 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT |
2427 MOXA_MUST_FCR_GDA_MODE_ENABLE,
2428 info->ioaddr + UART_FCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002429 else
2430 /* above add by Victor Yu. 08-30-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002431 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
2432 info->ioaddr + UART_FCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002433
2434 /* read data port to reset things */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002435 (void) inb(info->ioaddr + UART_RX);
Jiri Slaby037ad482006-12-08 02:38:11 -08002436
2437 if (info->tty)
2438 set_bit(TTY_IO_ERROR, &info->tty->flags);
2439
2440 info->flags &= ~ASYNC_INITIALIZED;
2441
2442 /* following add by Victor Yu. 09-23-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002443 if (info->board->chip_flag)
2444 SET_MOXA_MUST_NO_SOFTWARE_FLOW_CONTROL(info->ioaddr);
Jiri Slaby037ad482006-12-08 02:38:11 -08002445 /* above add by Victor Yu. 09-23-2002 */
2446
2447 spin_unlock_irqrestore(&info->slock, flags);
2448}
2449
2450/*
2451 * This routine is called to set the UART divisor registers to match
2452 * the specified baud rate for a serial port.
2453 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002454static int mxser_change_speed(struct mxser_port *info,
2455 struct termios *old_termios)
Jiri Slaby037ad482006-12-08 02:38:11 -08002456{
2457 unsigned cflag, cval, fcr;
2458 int ret = 0;
2459 unsigned char status;
2460 long baud;
2461 unsigned long flags;
2462
2463 if (!info->tty || !info->tty->termios)
2464 return ret;
2465 cflag = info->tty->termios->c_cflag;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002466 if (!(info->ioaddr))
Jiri Slaby037ad482006-12-08 02:38:11 -08002467 return ret;
2468
2469#ifndef B921600
2470#define B921600 (B460800 +1)
2471#endif
Jiri Slaby55b307d2006-12-08 02:38:14 -08002472 if (mxser_set_baud_method[info->tty->index] == 0) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002473 baud = tty_get_baud_rate(info->tty);
2474 mxser_set_baud(info, baud);
2475 }
2476
2477 /* byte size and parity */
2478 switch (cflag & CSIZE) {
2479 case CS5:
2480 cval = 0x00;
2481 break;
2482 case CS6:
2483 cval = 0x01;
2484 break;
2485 case CS7:
2486 cval = 0x02;
2487 break;
2488 case CS8:
2489 cval = 0x03;
2490 break;
2491 default:
2492 cval = 0x00;
2493 break; /* too keep GCC shut... */
2494 }
2495 if (cflag & CSTOPB)
2496 cval |= 0x04;
2497 if (cflag & PARENB)
2498 cval |= UART_LCR_PARITY;
2499 if (!(cflag & PARODD))
2500 cval |= UART_LCR_EPAR;
2501 if (cflag & CMSPAR)
2502 cval |= UART_LCR_SPAR;
2503
2504 if ((info->type == PORT_8250) || (info->type == PORT_16450)) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002505 if (info->board->chip_flag) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002506 fcr = UART_FCR_ENABLE_FIFO;
2507 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
2508 SET_MOXA_MUST_FIFO_VALUE(info);
2509 } else
2510 fcr = 0;
2511 } else {
2512 fcr = UART_FCR_ENABLE_FIFO;
2513 /* following add by Victor Yu. 08-30-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002514 if (info->board->chip_flag) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002515 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
2516 SET_MOXA_MUST_FIFO_VALUE(info);
2517 } else {
2518 /* above add by Victor Yu. 08-30-2002 */
2519 switch (info->rx_trigger) {
2520 case 1:
2521 fcr |= UART_FCR_TRIGGER_1;
2522 break;
2523 case 4:
2524 fcr |= UART_FCR_TRIGGER_4;
2525 break;
2526 case 8:
2527 fcr |= UART_FCR_TRIGGER_8;
2528 break;
2529 default:
2530 fcr |= UART_FCR_TRIGGER_14;
2531 break;
2532 }
2533 }
2534 }
2535
2536 /* CTS flow control flag and modem status interrupts */
2537 info->IER &= ~UART_IER_MSI;
2538 info->MCR &= ~UART_MCR_AFE;
2539 if (cflag & CRTSCTS) {
2540 info->flags |= ASYNC_CTS_FLOW;
2541 info->IER |= UART_IER_MSI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002542 if ((info->type == PORT_16550A) || (info->board->chip_flag)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002543 info->MCR |= UART_MCR_AFE;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002544/* status = mxser_get_msr(info->ioaddr, 0, info->port); */
Jiri Slaby037ad482006-12-08 02:38:11 -08002545/*
2546 save_flags(flags);
2547 cli();
2548 status = inb(baseaddr + UART_MSR);
2549 restore_flags(flags);
2550*/
2551 /* mxser_check_modem_status(info, status); */
2552 } else {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002553/* status = mxser_get_msr(info->ioaddr, 0, info->port); */
Jiri Slaby037ad482006-12-08 02:38:11 -08002554 /* MX_LOCK(&info->slock); */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002555 status = inb(info->ioaddr + UART_MSR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002556 /* MX_UNLOCK(&info->slock); */
2557 if (info->tty->hw_stopped) {
2558 if (status & UART_MSR_CTS) {
2559 info->tty->hw_stopped = 0;
Jiri Slaby3306ce32006-12-08 02:38:13 -08002560 if (info->type != PORT_16550A &&
Jiri Slaby55b307d2006-12-08 02:38:14 -08002561 !info->board->chip_flag) {
Jiri Slaby3306ce32006-12-08 02:38:13 -08002562 outb(info->IER & ~UART_IER_THRI,
Jiri Slaby55b307d2006-12-08 02:38:14 -08002563 info->ioaddr +
2564 UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08002565 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002566 outb(info->IER, info->ioaddr +
2567 UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08002568 }
2569 set_bit(MXSER_EVENT_TXLOW, &info->event);
2570 schedule_work(&info->tqueue); }
2571 } else {
2572 if (!(status & UART_MSR_CTS)) {
2573 info->tty->hw_stopped = 1;
2574 if ((info->type != PORT_16550A) &&
Jiri Slaby55b307d2006-12-08 02:38:14 -08002575 (!info->board->chip_flag)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002576 info->IER &= ~UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002577 outb(info->IER, info->ioaddr +
2578 UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08002579 }
2580 }
2581 }
2582 }
2583 } else {
2584 info->flags &= ~ASYNC_CTS_FLOW;
2585 }
Jiri Slaby55b307d2006-12-08 02:38:14 -08002586 outb(info->MCR, info->ioaddr + UART_MCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002587 if (cflag & CLOCAL) {
2588 info->flags &= ~ASYNC_CHECK_CD;
2589 } else {
2590 info->flags |= ASYNC_CHECK_CD;
2591 info->IER |= UART_IER_MSI;
2592 }
Jiri Slaby55b307d2006-12-08 02:38:14 -08002593 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08002594
2595 /*
2596 * Set up parity check flag
2597 */
2598 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
2599 if (I_INPCK(info->tty))
2600 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
2601 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
2602 info->read_status_mask |= UART_LSR_BI;
2603
2604 info->ignore_status_mask = 0;
2605
2606 if (I_IGNBRK(info->tty)) {
2607 info->ignore_status_mask |= UART_LSR_BI;
2608 info->read_status_mask |= UART_LSR_BI;
2609 /*
2610 * If we're ignore parity and break indicators, ignore
2611 * overruns too. (For real raw support).
2612 */
2613 if (I_IGNPAR(info->tty)) {
2614 info->ignore_status_mask |=
2615 UART_LSR_OE |
2616 UART_LSR_PE |
2617 UART_LSR_FE;
2618 info->read_status_mask |=
2619 UART_LSR_OE |
2620 UART_LSR_PE |
2621 UART_LSR_FE;
2622 }
2623 }
2624 /* following add by Victor Yu. 09-02-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002625 if (info->board->chip_flag) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002626 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby55b307d2006-12-08 02:38:14 -08002627 SET_MOXA_MUST_XON1_VALUE(info->ioaddr, START_CHAR(info->tty));
2628 SET_MOXA_MUST_XOFF1_VALUE(info->ioaddr, STOP_CHAR(info->tty));
Jiri Slaby037ad482006-12-08 02:38:11 -08002629 if (I_IXON(info->tty)) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002630 ENABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
Jiri Slaby037ad482006-12-08 02:38:11 -08002631 } else {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002632 DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
Jiri Slaby037ad482006-12-08 02:38:11 -08002633 }
2634 if (I_IXOFF(info->tty)) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002635 ENABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
Jiri Slaby037ad482006-12-08 02:38:11 -08002636 } else {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002637 DISABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
Jiri Slaby037ad482006-12-08 02:38:11 -08002638 }
2639 /*
2640 if ( I_IXANY(info->tty) ) {
2641 info->MCR |= MOXA_MUST_MCR_XON_ANY;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002642 ENABLE_MOXA_MUST_XON_ANY_FLOW_CONTROL(info->ioaddr);
Jiri Slaby037ad482006-12-08 02:38:11 -08002643 } else {
2644 info->MCR &= ~MOXA_MUST_MCR_XON_ANY;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002645 DISABLE_MOXA_MUST_XON_ANY_FLOW_CONTROL(info->ioaddr);
Jiri Slaby037ad482006-12-08 02:38:11 -08002646 }
2647 */
2648 spin_unlock_irqrestore(&info->slock, flags);
2649 }
2650 /* above add by Victor Yu. 09-02-2002 */
2651
2652
Jiri Slaby55b307d2006-12-08 02:38:14 -08002653 outb(fcr, info->ioaddr + UART_FCR); /* set fcr */
2654 outb(cval, info->ioaddr + UART_LCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002655
2656 return ret;
2657}
2658
2659
Jiri Slaby55b307d2006-12-08 02:38:14 -08002660static int mxser_set_baud(struct mxser_port *info, long newspd)
Jiri Slaby037ad482006-12-08 02:38:11 -08002661{
2662 int quot = 0;
2663 unsigned char cval;
2664 int ret = 0;
2665 unsigned long flags;
2666
2667 if (!info->tty || !info->tty->termios)
2668 return ret;
2669
Jiri Slaby55b307d2006-12-08 02:38:14 -08002670 if (!(info->ioaddr))
Jiri Slaby037ad482006-12-08 02:38:11 -08002671 return ret;
2672
Jiri Slaby55b307d2006-12-08 02:38:14 -08002673 if (newspd > info->max_baud)
Jiri Slaby037ad482006-12-08 02:38:11 -08002674 return 0;
2675
2676 info->realbaud = newspd;
2677 if (newspd == 134) {
2678 quot = (2 * info->baud_base / 269);
2679 } else if (newspd) {
2680 quot = info->baud_base / newspd;
2681 if (quot == 0)
2682 quot = 1;
2683 } else {
2684 quot = 0;
2685 }
2686
2687 info->timeout = ((info->xmit_fifo_size * HZ * 10 * quot) / info->baud_base);
2688 info->timeout += HZ / 50; /* Add .02 seconds of slop */
2689
2690 if (quot) {
2691 spin_lock_irqsave(&info->slock, flags);
2692 info->MCR |= UART_MCR_DTR;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002693 outb(info->MCR, info->ioaddr + UART_MCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002694 spin_unlock_irqrestore(&info->slock, flags);
2695 } else {
2696 spin_lock_irqsave(&info->slock, flags);
2697 info->MCR &= ~UART_MCR_DTR;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002698 outb(info->MCR, info->ioaddr + UART_MCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002699 spin_unlock_irqrestore(&info->slock, flags);
2700 return ret;
2701 }
2702
Jiri Slaby55b307d2006-12-08 02:38:14 -08002703 cval = inb(info->ioaddr + UART_LCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002704
Jiri Slaby55b307d2006-12-08 02:38:14 -08002705 outb(cval | UART_LCR_DLAB, info->ioaddr + UART_LCR); /* set DLAB */
Jiri Slaby037ad482006-12-08 02:38:11 -08002706
Jiri Slaby55b307d2006-12-08 02:38:14 -08002707 outb(quot & 0xff, info->ioaddr + UART_DLL); /* LS of divisor */
2708 outb(quot >> 8, info->ioaddr + UART_DLM); /* MS of divisor */
2709 outb(cval, info->ioaddr + UART_LCR); /* reset DLAB */
Jiri Slaby037ad482006-12-08 02:38:11 -08002710
2711
2712 return ret;
2713}
2714
2715/*
2716 * ------------------------------------------------------------
2717 * friends of mxser_ioctl()
2718 * ------------------------------------------------------------
2719 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002720static int mxser_get_serial_info(struct mxser_port *info,
2721 struct serial_struct __user *retinfo)
Jiri Slaby037ad482006-12-08 02:38:11 -08002722{
2723 struct serial_struct tmp;
2724
2725 if (!retinfo)
2726 return -EFAULT;
2727 memset(&tmp, 0, sizeof(tmp));
2728 tmp.type = info->type;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002729 tmp.line = info->tty->index;
2730 tmp.port = info->ioaddr;
2731 tmp.irq = info->board->irq;
Jiri Slaby037ad482006-12-08 02:38:11 -08002732 tmp.flags = info->flags;
2733 tmp.baud_base = info->baud_base;
2734 tmp.close_delay = info->close_delay;
2735 tmp.closing_wait = info->closing_wait;
2736 tmp.custom_divisor = info->custom_divisor;
2737 tmp.hub6 = 0;
2738 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2739 return -EFAULT;
2740 return 0;
2741}
2742
Jiri Slaby55b307d2006-12-08 02:38:14 -08002743static int mxser_set_serial_info(struct mxser_port *info,
2744 struct serial_struct __user *new_info)
Jiri Slaby037ad482006-12-08 02:38:11 -08002745{
2746 struct serial_struct new_serial;
2747 unsigned int flags;
2748 int retval = 0;
2749
Jiri Slaby55b307d2006-12-08 02:38:14 -08002750 if (!new_info || !info->ioaddr)
Jiri Slaby037ad482006-12-08 02:38:11 -08002751 return -EFAULT;
2752 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
2753 return -EFAULT;
2754
Jiri Slaby55b307d2006-12-08 02:38:14 -08002755 if ((new_serial.irq != info->board->irq) ||
2756 (new_serial.port != info->ioaddr) ||
Jiri Slaby037ad482006-12-08 02:38:11 -08002757 (new_serial.custom_divisor != info->custom_divisor) ||
2758 (new_serial.baud_base != info->baud_base))
2759 return -EPERM;
2760
2761 flags = info->flags & ASYNC_SPD_MASK;
2762
2763 if (!capable(CAP_SYS_ADMIN)) {
2764 if ((new_serial.baud_base != info->baud_base) ||
2765 (new_serial.close_delay != info->close_delay) ||
2766 ((new_serial.flags & ~ASYNC_USR_MASK) != (info->flags & ~ASYNC_USR_MASK)))
2767 return -EPERM;
2768 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
2769 (new_serial.flags & ASYNC_USR_MASK));
2770 } else {
2771 /*
2772 * OK, past this point, all the error checking has been done.
2773 * At this point, we start making changes.....
2774 */
2775 info->flags = ((info->flags & ~ASYNC_FLAGS) |
2776 (new_serial.flags & ASYNC_FLAGS));
2777 info->close_delay = new_serial.close_delay * HZ / 100;
2778 info->closing_wait = new_serial.closing_wait * HZ / 100;
2779 info->tty->low_latency =
2780 (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
2781 info->tty->low_latency = 0; /* (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0; */
2782 }
2783
2784 /* added by casper, 3/17/2000, for mouse */
2785 info->type = new_serial.type;
2786
2787 process_txrx_fifo(info);
2788
2789 if (info->flags & ASYNC_INITIALIZED) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002790 if (flags != (info->flags & ASYNC_SPD_MASK))
Jiri Slaby037ad482006-12-08 02:38:11 -08002791 mxser_change_speed(info, NULL);
Jiri Slaby55b307d2006-12-08 02:38:14 -08002792 } else
Jiri Slaby037ad482006-12-08 02:38:11 -08002793 retval = mxser_startup(info);
Jiri Slaby55b307d2006-12-08 02:38:14 -08002794
Jiri Slaby037ad482006-12-08 02:38:11 -08002795 return retval;
2796}
2797
2798/*
2799 * mxser_get_lsr_info - get line status register info
2800 *
2801 * Purpose: Let user call ioctl() to get info when the UART physically
2802 * is emptied. On bus types like RS485, the transmitter must
2803 * release the bus after transmitting. This must be done when
2804 * the transmit shift register is empty, not be done when the
2805 * transmit holding register is empty. This functionality
2806 * allows an RS485 driver to be written in user space.
2807 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002808static int mxser_get_lsr_info(struct mxser_port *info,
2809 unsigned int __user *value)
Jiri Slaby037ad482006-12-08 02:38:11 -08002810{
2811 unsigned char status;
2812 unsigned int result;
2813 unsigned long flags;
2814
2815 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby55b307d2006-12-08 02:38:14 -08002816 status = inb(info->ioaddr + UART_LSR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002817 spin_unlock_irqrestore(&info->slock, flags);
2818 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
2819 return put_user(result, value);
2820}
2821
2822/*
2823 * This routine sends a break character out the serial port.
2824 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002825static void mxser_send_break(struct mxser_port *info, int duration)
Jiri Slaby037ad482006-12-08 02:38:11 -08002826{
2827 unsigned long flags;
2828
Jiri Slaby55b307d2006-12-08 02:38:14 -08002829 if (!info->ioaddr)
Jiri Slaby037ad482006-12-08 02:38:11 -08002830 return;
2831 set_current_state(TASK_INTERRUPTIBLE);
2832 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby55b307d2006-12-08 02:38:14 -08002833 outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
2834 info->ioaddr + UART_LCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002835 spin_unlock_irqrestore(&info->slock, flags);
2836 schedule_timeout(duration);
2837 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby55b307d2006-12-08 02:38:14 -08002838 outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
2839 info->ioaddr + UART_LCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002840 spin_unlock_irqrestore(&info->slock, flags);
2841}
2842
2843static int mxser_tiocmget(struct tty_struct *tty, struct file *file)
2844{
Jiri Slaby55b307d2006-12-08 02:38:14 -08002845 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08002846 unsigned char control, status;
2847 unsigned long flags;
2848
2849
2850 if (tty->index == MXSER_PORTS)
2851 return -ENOIOCTLCMD;
2852 if (tty->flags & (1 << TTY_IO_ERROR))
2853 return -EIO;
2854
2855 control = info->MCR;
2856
2857 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby55b307d2006-12-08 02:38:14 -08002858 status = inb(info->ioaddr + UART_MSR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002859 if (status & UART_MSR_ANY_DELTA)
2860 mxser_check_modem_status(info, status);
2861 spin_unlock_irqrestore(&info->slock, flags);
2862 return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0) |
2863 ((control & UART_MCR_DTR) ? TIOCM_DTR : 0) |
2864 ((status & UART_MSR_DCD) ? TIOCM_CAR : 0) |
2865 ((status & UART_MSR_RI) ? TIOCM_RNG : 0) |
2866 ((status & UART_MSR_DSR) ? TIOCM_DSR : 0) |
2867 ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
2868}
2869
Jiri Slaby55b307d2006-12-08 02:38:14 -08002870static int mxser_tiocmset(struct tty_struct *tty, struct file *file,
2871 unsigned int set, unsigned int clear)
Jiri Slaby037ad482006-12-08 02:38:11 -08002872{
Jiri Slaby55b307d2006-12-08 02:38:14 -08002873 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08002874 unsigned long flags;
2875
2876
2877 if (tty->index == MXSER_PORTS)
2878 return -ENOIOCTLCMD;
2879 if (tty->flags & (1 << TTY_IO_ERROR))
2880 return -EIO;
2881
2882 spin_lock_irqsave(&info->slock, flags);
2883
2884 if (set & TIOCM_RTS)
2885 info->MCR |= UART_MCR_RTS;
2886 if (set & TIOCM_DTR)
2887 info->MCR |= UART_MCR_DTR;
2888
2889 if (clear & TIOCM_RTS)
2890 info->MCR &= ~UART_MCR_RTS;
2891 if (clear & TIOCM_DTR)
2892 info->MCR &= ~UART_MCR_DTR;
2893
Jiri Slaby55b307d2006-12-08 02:38:14 -08002894 outb(info->MCR, info->ioaddr + UART_MCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002895 spin_unlock_irqrestore(&info->slock, flags);
2896 return 0;
2897}
2898
2899
2900static int mxser_read_register(int, unsigned short *);
2901static int mxser_program_mode(int);
2902static void mxser_normal_mode(int);
2903
Jiri Slaby943f2952006-12-08 02:38:15 -08002904static int __init mxser_get_ISA_conf(int cap, struct mxser_board *brd)
Jiri Slaby037ad482006-12-08 02:38:11 -08002905{
2906 int id, i, bits;
2907 unsigned short regs[16], irq;
2908 unsigned char scratch, scratch2;
2909
Jiri Slaby55b307d2006-12-08 02:38:14 -08002910 brd->chip_flag = MOXA_OTHER_UART;
Jiri Slaby037ad482006-12-08 02:38:11 -08002911
2912 id = mxser_read_register(cap, regs);
2913 if (id == C168_ASIC_ID) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002914 brd->board_type = MXSER_BOARD_C168_ISA;
2915 brd->nports = 8;
Jiri Slaby037ad482006-12-08 02:38:11 -08002916 } else if (id == C104_ASIC_ID) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002917 brd->board_type = MXSER_BOARD_C104_ISA;
2918 brd->nports = 4;
Jiri Slaby037ad482006-12-08 02:38:11 -08002919 } else if (id == C102_ASIC_ID) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002920 brd->board_type = MXSER_BOARD_C102_ISA;
2921 brd->nports = 2;
Jiri Slaby037ad482006-12-08 02:38:11 -08002922 } else if (id == CI132_ASIC_ID) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002923 brd->board_type = MXSER_BOARD_CI132;
2924 brd->nports = 2;
Jiri Slaby037ad482006-12-08 02:38:11 -08002925 } else if (id == CI134_ASIC_ID) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002926 brd->board_type = MXSER_BOARD_CI134;
2927 brd->nports = 4;
Jiri Slaby037ad482006-12-08 02:38:11 -08002928 } else if (id == CI104J_ASIC_ID) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002929 brd->board_type = MXSER_BOARD_CI104J;
2930 brd->nports = 4;
Jiri Slaby037ad482006-12-08 02:38:11 -08002931 } else
2932 return 0;
2933
2934 irq = 0;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002935 if (brd->nports == 2) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002936 irq = regs[9] & 0xF000;
2937 irq = irq | (irq >> 4);
2938 if (irq != (regs[9] & 0xFF00))
2939 return MXSER_ERR_IRQ_CONFLIT;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002940 } else if (brd->nports == 4) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002941 irq = regs[9] & 0xF000;
2942 irq = irq | (irq >> 4);
2943 irq = irq | (irq >> 8);
2944 if (irq != regs[9])
2945 return MXSER_ERR_IRQ_CONFLIT;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002946 } else if (brd->nports == 8) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002947 irq = regs[9] & 0xF000;
2948 irq = irq | (irq >> 4);
2949 irq = irq | (irq >> 8);
2950 if ((irq != regs[9]) || (irq != regs[10]))
2951 return MXSER_ERR_IRQ_CONFLIT;
2952 }
2953
2954 if (!irq)
2955 return MXSER_ERR_IRQ;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002956 brd->irq = ((int)(irq & 0xF000) >> 12);
Jiri Slaby037ad482006-12-08 02:38:11 -08002957 for (i = 0; i < 8; i++)
Jiri Slaby55b307d2006-12-08 02:38:14 -08002958 brd->ports[i].ioaddr = (int) regs[i + 1] & 0xFFF8;
Jiri Slaby037ad482006-12-08 02:38:11 -08002959 if ((regs[12] & 0x80) == 0)
2960 return MXSER_ERR_VECTOR;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002961 brd->vector = (int)regs[11]; /* interrupt vector */
Jiri Slaby037ad482006-12-08 02:38:11 -08002962 if (id == 1)
Jiri Slaby55b307d2006-12-08 02:38:14 -08002963 brd->vector_mask = 0x00FF;
Jiri Slaby037ad482006-12-08 02:38:11 -08002964 else
Jiri Slaby55b307d2006-12-08 02:38:14 -08002965 brd->vector_mask = 0x000F;
Jiri Slaby037ad482006-12-08 02:38:11 -08002966 for (i = 7, bits = 0x0100; i >= 0; i--, bits <<= 1) {
2967 if (regs[12] & bits) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002968 brd->ports[i].baud_base = 921600;
2969 brd->ports[i].max_baud = 921600; /* add by Victor Yu. 09-04-2002 */
Jiri Slaby037ad482006-12-08 02:38:11 -08002970 } else {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002971 brd->ports[i].baud_base = 115200;
2972 brd->ports[i].max_baud = 115200; /* add by Victor Yu. 09-04-2002 */
Jiri Slaby037ad482006-12-08 02:38:11 -08002973 }
2974 }
2975 scratch2 = inb(cap + UART_LCR) & (~UART_LCR_DLAB);
2976 outb(scratch2 | UART_LCR_DLAB, cap + UART_LCR);
2977 outb(0, cap + UART_EFR); /* EFR is the same as FCR */
2978 outb(scratch2, cap + UART_LCR);
2979 outb(UART_FCR_ENABLE_FIFO, cap + UART_FCR);
2980 scratch = inb(cap + UART_IIR);
2981
2982 if (scratch & 0xC0)
Jiri Slaby55b307d2006-12-08 02:38:14 -08002983 brd->uart_type = PORT_16550A;
Jiri Slaby037ad482006-12-08 02:38:11 -08002984 else
Jiri Slaby55b307d2006-12-08 02:38:14 -08002985 brd->uart_type = PORT_16450;
Jiri Slaby037ad482006-12-08 02:38:11 -08002986 if (id == 1)
Jiri Slaby55b307d2006-12-08 02:38:14 -08002987 brd->nports = 8;
Jiri Slaby037ad482006-12-08 02:38:11 -08002988 else
Jiri Slaby55b307d2006-12-08 02:38:14 -08002989 brd->nports = 4;
Jiri Slaby7a7a5c32006-12-08 02:38:17 -08002990 if (!request_region(brd->ports[0].ioaddr, 8 * brd->nports, "mxser(IO)"))
2991 return MXSER_ERR_IOADDR;
2992 if (!request_region(brd->vector, 1, "mxser(vector)")) {
2993 release_region(brd->ports[0].ioaddr, 8 * brd->nports);
2994 return MXSER_ERR_VECTOR;
2995 }
Jiri Slaby55b307d2006-12-08 02:38:14 -08002996 return brd->nports;
Jiri Slaby037ad482006-12-08 02:38:11 -08002997}
2998
2999#define CHIP_SK 0x01 /* Serial Data Clock in Eprom */
3000#define CHIP_DO 0x02 /* Serial Data Output in Eprom */
3001#define CHIP_CS 0x04 /* Serial Chip Select in Eprom */
3002#define CHIP_DI 0x08 /* Serial Data Input in Eprom */
3003#define EN_CCMD 0x000 /* Chip's command register */
3004#define EN0_RSARLO 0x008 /* Remote start address reg 0 */
3005#define EN0_RSARHI 0x009 /* Remote start address reg 1 */
3006#define EN0_RCNTLO 0x00A /* Remote byte count reg WR */
3007#define EN0_RCNTHI 0x00B /* Remote byte count reg WR */
3008#define EN0_DCFG 0x00E /* Data configuration reg WR */
3009#define EN0_PORT 0x010 /* Rcv missed frame error counter RD */
3010#define ENC_PAGE0 0x000 /* Select page 0 of chip registers */
3011#define ENC_PAGE3 0x0C0 /* Select page 3 of chip registers */
3012static int mxser_read_register(int port, unsigned short *regs)
3013{
3014 int i, k, value, id;
3015 unsigned int j;
3016
3017 id = mxser_program_mode(port);
3018 if (id < 0)
3019 return id;
3020 for (i = 0; i < 14; i++) {
3021 k = (i & 0x3F) | 0x180;
3022 for (j = 0x100; j > 0; j >>= 1) {
3023 outb(CHIP_CS, port);
3024 if (k & j) {
3025 outb(CHIP_CS | CHIP_DO, port);
3026 outb(CHIP_CS | CHIP_DO | CHIP_SK, port); /* A? bit of read */
3027 } else {
3028 outb(CHIP_CS, port);
3029 outb(CHIP_CS | CHIP_SK, port); /* A? bit of read */
3030 }
3031 }
3032 (void)inb(port);
3033 value = 0;
3034 for (k = 0, j = 0x8000; k < 16; k++, j >>= 1) {
3035 outb(CHIP_CS, port);
3036 outb(CHIP_CS | CHIP_SK, port);
3037 if (inb(port) & CHIP_DI)
3038 value |= j;
3039 }
3040 regs[i] = value;
3041 outb(0, port);
3042 }
3043 mxser_normal_mode(port);
3044 return id;
3045}
3046
3047static int mxser_program_mode(int port)
3048{
3049 int id, i, j, n;
3050 /* unsigned long flags; */
3051
3052 spin_lock(&gm_lock);
3053 outb(0, port);
3054 outb(0, port);
3055 outb(0, port);
3056 (void)inb(port);
3057 (void)inb(port);
3058 outb(0, port);
3059 (void)inb(port);
3060 /* restore_flags(flags); */
3061 spin_unlock(&gm_lock);
3062
3063 id = inb(port + 1) & 0x1F;
3064 if ((id != C168_ASIC_ID) &&
3065 (id != C104_ASIC_ID) &&
3066 (id != C102_ASIC_ID) &&
3067 (id != CI132_ASIC_ID) &&
3068 (id != CI134_ASIC_ID) &&
3069 (id != CI104J_ASIC_ID))
3070 return -1;
3071 for (i = 0, j = 0; i < 4; i++) {
3072 n = inb(port + 2);
3073 if (n == 'M') {
3074 j = 1;
3075 } else if ((j == 1) && (n == 1)) {
3076 j = 2;
3077 break;
3078 } else
3079 j = 0;
3080 }
3081 if (j != 2)
3082 id = -2;
3083 return id;
3084}
3085
3086static void mxser_normal_mode(int port)
3087{
3088 int i, n;
3089
3090 outb(0xA5, port + 1);
3091 outb(0x80, port + 3);
3092 outb(12, port + 0); /* 9600 bps */
3093 outb(0, port + 1);
3094 outb(0x03, port + 3); /* 8 data bits */
3095 outb(0x13, port + 4); /* loop back mode */
3096 for (i = 0; i < 16; i++) {
3097 n = inb(port + 5);
3098 if ((n & 0x61) == 0x60)
3099 break;
3100 if ((n & 1) == 1)
3101 (void)inb(port);
3102 }
3103 outb(0x00, port + 4);
3104}
3105
3106module_init(mxser_module_init);
3107module_exit(mxser_module_exit);