blob: 863c1b27849b4d6d28633d67f5b7aeb77eaaf766 [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;
247static int verbose = 0;
248
249/* Variables for insmod */
250
251MODULE_AUTHOR("Casper Yang");
252MODULE_DESCRIPTION("MOXA Smartio/Industio Family Multiport Board Device Driver");
253module_param_array(ioaddr, int, NULL, 0);
254module_param(ttymajor, int, 0);
Jiri Slaby037ad482006-12-08 02:38:11 -0800255module_param(verbose, bool, 0);
256MODULE_LICENSE("GPL");
257
258struct mxser_log {
259 int tick;
260 unsigned long rxcnt[MXSER_PORTS];
261 unsigned long txcnt[MXSER_PORTS];
262};
263
264
265struct mxser_mon {
266 unsigned long rxcnt;
267 unsigned long txcnt;
268 unsigned long up_rxcnt;
269 unsigned long up_txcnt;
270 int modem_status;
271 unsigned char hold_reason;
272};
273
274struct mxser_mon_ext {
275 unsigned long rx_cnt[32];
276 unsigned long tx_cnt[32];
277 unsigned long up_rxcnt[32];
278 unsigned long up_txcnt[32];
279 int modem_status[32];
280
281 long baudrate[32];
282 int databits[32];
283 int stopbits[32];
284 int parity[32];
285 int flowctrl[32];
286 int fifo[32];
287 int iftype[32];
288};
289
Jiri Slaby55b307d2006-12-08 02:38:14 -0800290struct mxser_board;
Jiri Slaby037ad482006-12-08 02:38:11 -0800291
Jiri Slaby55b307d2006-12-08 02:38:14 -0800292struct mxser_port {
293 struct mxser_board *board;
294 struct tty_struct *tty;
295
296 unsigned long ioaddr;
297 unsigned long opmode_ioaddr;
298 int max_baud;
299
Jiri Slaby037ad482006-12-08 02:38:11 -0800300 int rx_high_water;
301 int rx_trigger; /* Rx fifo trigger level */
302 int rx_low_water;
303 int baud_base; /* max. speed */
Jiri Slaby55b307d2006-12-08 02:38:14 -0800304 long realbaud;
Jiri Slaby037ad482006-12-08 02:38:11 -0800305 int type; /* UART type */
Jiri Slaby55b307d2006-12-08 02:38:14 -0800306 int flags; /* defined in tty.h */
307 long session; /* Session of opening process */
308 long pgrp; /* pgrp of opening process */
309
310 int x_char; /* xon/xoff character */
311 int IER; /* Interrupt Enable Register */
312 int MCR; /* Modem control register */
313
314 unsigned char stop_rx;
315 unsigned char ldisc_stop_rx;
316
317 int custom_divisor;
318 int close_delay;
319 unsigned short closing_wait;
320 unsigned char err_shadow;
321 unsigned long event;
322
323 int count; /* # of fd on device */
324 int blocked_open; /* # of blocked opens */
325 struct async_icount icount; /* kernel counters for 4 input interrupts */
326 int timeout;
327
Jiri Slaby037ad482006-12-08 02:38:11 -0800328 int read_status_mask;
329 int ignore_status_mask;
330 int xmit_fifo_size;
Jiri Slaby037ad482006-12-08 02:38:11 -0800331 unsigned char *xmit_buf;
332 int xmit_head;
333 int xmit_tail;
334 int xmit_cnt;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800335
Jiri Slaby037ad482006-12-08 02:38:11 -0800336 struct termios normal_termios;
337 struct termios callout_termios;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800338
339 struct mxser_mon mon_data;
340
341 spinlock_t slock;
342 struct work_struct tqueue;
Jiri Slaby037ad482006-12-08 02:38:11 -0800343 wait_queue_head_t open_wait;
344 wait_queue_head_t close_wait;
345 wait_queue_head_t delta_msr_wait;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800346};
347
348struct mxser_board {
349 struct pci_dev *pdev; /* temporary (until pci probing) */
350
351 int irq;
352 int board_type;
353 unsigned int nports;
354 unsigned long vector;
355 unsigned long vector_mask;
356
357 int chip_flag;
358 int uart_type;
359
360 struct mxser_port ports[MXSER_PORTS_PER_BOARD];
Jiri Slaby037ad482006-12-08 02:38:11 -0800361};
362
363struct mxser_mstatus {
364 tcflag_t cflag;
365 int cts;
366 int dsr;
367 int ri;
368 int dcd;
369};
370
371static struct mxser_mstatus GMStatus[MXSER_PORTS];
372
373static int mxserBoardCAP[MXSER_BOARDS] = {
374 0, 0, 0, 0
375 /* 0x180, 0x280, 0x200, 0x320 */
376};
377
Jiri Slaby55b307d2006-12-08 02:38:14 -0800378static struct mxser_board mxser_boards[MXSER_BOARDS];
Jiri Slaby037ad482006-12-08 02:38:11 -0800379static struct tty_driver *mxvar_sdriver;
Jiri Slaby037ad482006-12-08 02:38:11 -0800380static struct tty_struct *mxvar_tty[MXSER_PORTS + 1];
381static struct termios *mxvar_termios[MXSER_PORTS + 1];
382static struct termios *mxvar_termios_locked[MXSER_PORTS + 1];
383static struct mxser_log mxvar_log;
384static int mxvar_diagflag;
385static unsigned char mxser_msr[MXSER_PORTS + 1];
386static struct mxser_mon_ext mon_data_ext;
387static int mxser_set_baud_method[MXSER_PORTS + 1];
388static spinlock_t gm_lock;
389
390/*
Jiri Slaby037ad482006-12-08 02:38:11 -0800391 * static functions:
392 */
393
Jiri Slaby037ad482006-12-08 02:38:11 -0800394static int mxser_init(void);
395
396/* static void mxser_poll(unsigned long); */
Jiri Slaby55b307d2006-12-08 02:38:14 -0800397static int mxser_get_ISA_conf(int, struct mxser_board *);
Jiri Slaby037ad482006-12-08 02:38:11 -0800398static void mxser_do_softint(void *);
399static int mxser_open(struct tty_struct *, struct file *);
400static void mxser_close(struct tty_struct *, struct file *);
401static int mxser_write(struct tty_struct *, const unsigned char *, int);
402static int mxser_write_room(struct tty_struct *);
403static void mxser_flush_buffer(struct tty_struct *);
404static int mxser_chars_in_buffer(struct tty_struct *);
405static void mxser_flush_chars(struct tty_struct *);
406static void mxser_put_char(struct tty_struct *, unsigned char);
407static int mxser_ioctl(struct tty_struct *, struct file *, uint, ulong);
408static int mxser_ioctl_special(unsigned int, void __user *);
409static void mxser_throttle(struct tty_struct *);
410static void mxser_unthrottle(struct tty_struct *);
411static void mxser_set_termios(struct tty_struct *, struct termios *);
412static void mxser_stop(struct tty_struct *);
413static void mxser_start(struct tty_struct *);
414static void mxser_hangup(struct tty_struct *);
415static void mxser_rs_break(struct tty_struct *, int);
416static irqreturn_t mxser_interrupt(int, void *, struct pt_regs *);
Jiri Slaby55b307d2006-12-08 02:38:14 -0800417static void mxser_receive_chars(struct mxser_port *, int *);
418static void mxser_transmit_chars(struct mxser_port *);
419static void mxser_check_modem_status(struct mxser_port *, int);
420static int mxser_block_til_ready(struct tty_struct *, struct file *,
421 struct mxser_port *);
422static int mxser_startup(struct mxser_port *);
423static void mxser_shutdown(struct mxser_port *);
424static int mxser_change_speed(struct mxser_port *, struct termios *);
425static int mxser_get_serial_info(struct mxser_port *,
426 struct serial_struct __user *);
427static int mxser_set_serial_info(struct mxser_port *,
428 struct serial_struct __user *);
429static int mxser_get_lsr_info(struct mxser_port *, unsigned int __user *);
430static void mxser_send_break(struct mxser_port *, int);
Jiri Slaby037ad482006-12-08 02:38:11 -0800431static int mxser_tiocmget(struct tty_struct *, struct file *);
Jiri Slaby55b307d2006-12-08 02:38:14 -0800432static int mxser_tiocmset(struct tty_struct *, struct file *, unsigned int,
433 unsigned int);
434static int mxser_set_baud(struct mxser_port *, long);
435static void mxser_wait_until_sent(struct tty_struct *, int);
Jiri Slaby037ad482006-12-08 02:38:11 -0800436
Jiri Slaby55b307d2006-12-08 02:38:14 -0800437static void mxser_startrx(struct tty_struct *);
438static void mxser_stoprx(struct tty_struct *);
Jiri Slaby037ad482006-12-08 02:38:11 -0800439
440
441static int CheckIsMoxaMust(int io)
442{
443 u8 oldmcr, hwid;
444 int i;
445
446 outb(0, io + UART_LCR);
447 DISABLE_MOXA_MUST_ENCHANCE_MODE(io);
448 oldmcr = inb(io + UART_MCR);
449 outb(0, io + UART_MCR);
450 SET_MOXA_MUST_XON1_VALUE(io, 0x11);
451 if ((hwid = inb(io + UART_MCR)) != 0) {
452 outb(oldmcr, io + UART_MCR);
453 return MOXA_OTHER_UART;
454 }
455
456 GET_MOXA_MUST_HARDWARE_ID(io, &hwid);
457 for (i = 0; i < UART_TYPE_NUM; i++) {
458 if (hwid == Gmoxa_uart_id[i])
459 return (int)hwid;
460 }
461 return MOXA_OTHER_UART;
462}
463
464/* above is modified by Victor Yu. 08-15-2002 */
465
466static const struct tty_operations mxser_ops = {
467 .open = mxser_open,
468 .close = mxser_close,
469 .write = mxser_write,
470 .put_char = mxser_put_char,
471 .flush_chars = mxser_flush_chars,
472 .write_room = mxser_write_room,
473 .chars_in_buffer = mxser_chars_in_buffer,
474 .flush_buffer = mxser_flush_buffer,
475 .ioctl = mxser_ioctl,
476 .throttle = mxser_throttle,
477 .unthrottle = mxser_unthrottle,
478 .set_termios = mxser_set_termios,
479 .stop = mxser_stop,
480 .start = mxser_start,
481 .hangup = mxser_hangup,
482 .break_ctl = mxser_rs_break,
483 .wait_until_sent = mxser_wait_until_sent,
484 .tiocmget = mxser_tiocmget,
485 .tiocmset = mxser_tiocmset,
486};
487
488/*
489 * The MOXA Smartio/Industio serial driver boot-time initialization code!
490 */
491
492static int __init mxser_module_init(void)
493{
494 int ret;
495
496 if (verbose)
497 printk(KERN_DEBUG "Loading module mxser ...\n");
Jiri Slaby3306ce32006-12-08 02:38:13 -0800498 printk(KERN_INFO "This is mxser driver version 1.9.1 and needs TESTING."
499 "If your are willing to test this driver, please report to "
500 "jirislaby@gmail.com. Thanks.\n");
Jiri Slaby037ad482006-12-08 02:38:11 -0800501 ret = mxser_init();
502 if (verbose)
503 printk(KERN_DEBUG "Done.\n");
504 return ret;
505}
506
507static void __exit mxser_module_exit(void)
508{
509 int i, err;
510
511 if (verbose)
512 printk(KERN_DEBUG "Unloading module mxser ...\n");
513
514 err = tty_unregister_driver(mxvar_sdriver);
515 if (!err)
516 put_tty_driver(mxvar_sdriver);
517 else
518 printk(KERN_ERR "Couldn't unregister MOXA Smartio/Industio family serial driver\n");
519
520 for (i = 0; i < MXSER_BOARDS; i++) {
521 struct pci_dev *pdev;
522
Jiri Slaby55b307d2006-12-08 02:38:14 -0800523 if (mxser_boards[i].board_type == -1)
Jiri Slaby037ad482006-12-08 02:38:11 -0800524 continue;
525 else {
Jiri Slaby55b307d2006-12-08 02:38:14 -0800526 pdev = mxser_boards[i].pdev;
527 free_irq(mxser_boards[i].irq, &mxser_boards[i]);
Jiri Slaby037ad482006-12-08 02:38:11 -0800528 if (pdev != NULL) { /* PCI */
529 release_region(pci_resource_start(pdev, 2), pci_resource_len(pdev, 2));
530 release_region(pci_resource_start(pdev, 3), pci_resource_len(pdev, 3));
531 pci_dev_put(pdev);
532 } else {
Jiri Slaby55b307d2006-12-08 02:38:14 -0800533 release_region(mxser_boards[i].ports[0].ioaddr, 8 * mxser_boards[i].nports);
534 release_region(mxser_boards[i].vector, 1);
Jiri Slaby037ad482006-12-08 02:38:11 -0800535 }
536 }
537 }
538 if (verbose)
539 printk(KERN_DEBUG "Done.\n");
540}
541
Jiri Slaby55b307d2006-12-08 02:38:14 -0800542static void process_txrx_fifo(struct mxser_port *info)
Jiri Slaby037ad482006-12-08 02:38:11 -0800543{
544 int i;
545
546 if ((info->type == PORT_16450) || (info->type == PORT_8250)) {
547 info->rx_trigger = 1;
548 info->rx_high_water = 1;
549 info->rx_low_water = 1;
550 info->xmit_fifo_size = 1;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800551 } else
552 for (i = 0; i < UART_INFO_NUM; i++)
553 if (info->board->chip_flag == Gpci_uart_info[i].type) {
Jiri Slaby037ad482006-12-08 02:38:11 -0800554 info->rx_trigger = Gpci_uart_info[i].rx_trigger;
555 info->rx_low_water = Gpci_uart_info[i].rx_low_water;
556 info->rx_high_water = Gpci_uart_info[i].rx_high_water;
557 info->xmit_fifo_size = Gpci_uart_info[i].xmit_fifo_size;
558 break;
559 }
Jiri Slaby037ad482006-12-08 02:38:11 -0800560}
561
Jiri Slaby943f2952006-12-08 02:38:15 -0800562static int __devinit mxser_initbrd(struct mxser_board *brd)
Jiri Slaby037ad482006-12-08 02:38:11 -0800563{
Jiri Slaby55b307d2006-12-08 02:38:14 -0800564 struct mxser_port *info;
565 unsigned int i;
Jiri Slaby037ad482006-12-08 02:38:11 -0800566 int retval;
Jiri Slaby037ad482006-12-08 02:38:11 -0800567
Jiri Slaby037ad482006-12-08 02:38:11 -0800568 /*if (verbose) */ {
Jiri Slaby037ad482006-12-08 02:38:11 -0800569 printk(" max. baud rate = %d bps.\n",
Jiri Slaby55b307d2006-12-08 02:38:14 -0800570 brd->ports[0].max_baud);
Jiri Slaby037ad482006-12-08 02:38:11 -0800571 }
572
Jiri Slaby55b307d2006-12-08 02:38:14 -0800573 for (i = 0; i < brd->nports; i++) {
574 info = &brd->ports[i];
575 info->board = brd;
Jiri Slaby037ad482006-12-08 02:38:11 -0800576 info->stop_rx = 0;
577 info->ldisc_stop_rx = 0;
578
Jiri Slaby037ad482006-12-08 02:38:11 -0800579 /* Enhance mode enabled here */
Jiri Slaby55b307d2006-12-08 02:38:14 -0800580 if (brd->chip_flag != MOXA_OTHER_UART)
581 ENABLE_MOXA_MUST_ENCHANCE_MODE(info->ioaddr);
Jiri Slaby037ad482006-12-08 02:38:11 -0800582
583 info->flags = ASYNC_SHARE_IRQ;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800584 info->type = brd->uart_type;
Jiri Slaby037ad482006-12-08 02:38:11 -0800585
586 process_txrx_fifo(info);
587
Jiri Slaby55b307d2006-12-08 02:38:14 -0800588 info->custom_divisor = info->baud_base * 16;
Jiri Slaby037ad482006-12-08 02:38:11 -0800589 info->close_delay = 5 * HZ / 10;
590 info->closing_wait = 30 * HZ;
591 INIT_WORK(&info->tqueue, mxser_do_softint, info);
592 info->normal_termios = mxvar_sdriver->init_termios;
593 init_waitqueue_head(&info->open_wait);
594 init_waitqueue_head(&info->close_wait);
595 init_waitqueue_head(&info->delta_msr_wait);
596 memset(&info->mon_data, 0, sizeof(struct mxser_mon));
597 info->err_shadow = 0;
598 spin_lock_init(&info->slock);
Jiri Slaby55b307d2006-12-08 02:38:14 -0800599
600 /* before set INT ISR, disable all int */
601 outb(inb(info->ioaddr + UART_IER) & 0xf0,
602 info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -0800603 }
604 /*
605 * Allocate the IRQ if necessary
606 */
607
Jiri Slaby55b307d2006-12-08 02:38:14 -0800608 retval = request_irq(brd->irq, mxser_interrupt,
609 (brd->ports[0].flags & ASYNC_SHARE_IRQ) ? IRQF_SHARED :
610 IRQF_DISABLED, "mxser", brd);
Jiri Slaby037ad482006-12-08 02:38:11 -0800611 if (retval) {
Jiri Slaby55b307d2006-12-08 02:38:14 -0800612 printk(KERN_ERR "Board %s: Request irq failed, IRQ (%d) may "
613 "conflict with another device.\n",
614 mxser_brdname[brd->board_type - 1], brd->irq);
Jiri Slaby037ad482006-12-08 02:38:11 -0800615 return retval;
616 }
617 return 0;
618}
619
Jiri Slaby943f2952006-12-08 02:38:15 -0800620static int __init mxser_get_PCI_conf(int board_type, struct mxser_board *brd,
Jiri Slaby55b307d2006-12-08 02:38:14 -0800621 struct pci_dev *pdev)
Jiri Slaby037ad482006-12-08 02:38:11 -0800622{
Jiri Slaby55b307d2006-12-08 02:38:14 -0800623 unsigned int i, j;
Jiri Slaby3306ce32006-12-08 02:38:13 -0800624 unsigned long ioaddress;
Jiri Slaby037ad482006-12-08 02:38:11 -0800625
626 /* io address */
Jiri Slaby55b307d2006-12-08 02:38:14 -0800627 brd->board_type = board_type;
628 brd->nports = mxser_numports[board_type - 1];
Jiri Slaby037ad482006-12-08 02:38:11 -0800629 ioaddress = pci_resource_start(pdev, 2);
630 request_region(pci_resource_start(pdev, 2), pci_resource_len(pdev, 2),
631 "mxser(IO)");
632
Jiri Slaby55b307d2006-12-08 02:38:14 -0800633 for (i = 0; i < brd->nports; i++)
634 brd->ports[i].ioaddr = ioaddress + 8 * i;
Jiri Slaby037ad482006-12-08 02:38:11 -0800635
636 /* vector */
637 ioaddress = pci_resource_start(pdev, 3);
638 request_region(pci_resource_start(pdev, 3), pci_resource_len(pdev, 3),
639 "mxser(vector)");
Jiri Slaby55b307d2006-12-08 02:38:14 -0800640 brd->vector = ioaddress;
Jiri Slaby037ad482006-12-08 02:38:11 -0800641
642 /* irq */
Jiri Slaby55b307d2006-12-08 02:38:14 -0800643 brd->irq = pdev->irq;
Jiri Slaby037ad482006-12-08 02:38:11 -0800644
Jiri Slaby55b307d2006-12-08 02:38:14 -0800645 brd->chip_flag = CheckIsMoxaMust(brd->ports[0].ioaddr);
646 brd->uart_type = PORT_16550A;
647 brd->vector_mask = 0;
Jiri Slaby037ad482006-12-08 02:38:11 -0800648
Jiri Slaby55b307d2006-12-08 02:38:14 -0800649 for (i = 0; i < brd->nports; i++) {
Jiri Slaby037ad482006-12-08 02:38:11 -0800650 for (j = 0; j < UART_INFO_NUM; j++) {
Jiri Slaby55b307d2006-12-08 02:38:14 -0800651 if (Gpci_uart_info[j].type == brd->chip_flag) {
652 brd->ports[i].max_baud =
653 Gpci_uart_info[j].max_baud;
Jiri Slaby037ad482006-12-08 02:38:11 -0800654
655 /* exception....CP-102 */
656 if (board_type == MXSER_BOARD_CP102)
Jiri Slaby55b307d2006-12-08 02:38:14 -0800657 brd->ports[i].max_baud = 921600;
Jiri Slaby037ad482006-12-08 02:38:11 -0800658 break;
659 }
660 }
661 }
662
Jiri Slaby55b307d2006-12-08 02:38:14 -0800663 if (brd->chip_flag == MOXA_MUST_MU860_HWID) {
664 for (i = 0; i < brd->nports; i++) {
Jiri Slaby037ad482006-12-08 02:38:11 -0800665 if (i < 4)
Jiri Slaby55b307d2006-12-08 02:38:14 -0800666 brd->ports[i].opmode_ioaddr = ioaddress + 4;
Jiri Slaby037ad482006-12-08 02:38:11 -0800667 else
Jiri Slaby55b307d2006-12-08 02:38:14 -0800668 brd->ports[i].opmode_ioaddr = ioaddress + 0x0c;
Jiri Slaby037ad482006-12-08 02:38:11 -0800669 }
670 outb(0, ioaddress + 4); /* default set to RS232 mode */
671 outb(0, ioaddress + 0x0c); /* default set to RS232 mode */
672 }
673
Jiri Slaby55b307d2006-12-08 02:38:14 -0800674 for (i = 0; i < brd->nports; i++) {
675 brd->vector_mask |= (1 << i);
676 brd->ports[i].baud_base = 921600;
Jiri Slaby037ad482006-12-08 02:38:11 -0800677 }
678 return 0;
679}
Jiri Slaby037ad482006-12-08 02:38:11 -0800680
Jiri Slaby943f2952006-12-08 02:38:15 -0800681static int __init mxser_init(void)
Jiri Slaby037ad482006-12-08 02:38:11 -0800682{
Jiri Slaby037ad482006-12-08 02:38:11 -0800683 struct pci_dev *pdev = NULL;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800684 struct mxser_board *brd;
685 unsigned int i, m;
686 int retval, b, n;
Jiri Slaby037ad482006-12-08 02:38:11 -0800687
688 mxvar_sdriver = alloc_tty_driver(MXSER_PORTS + 1);
689 if (!mxvar_sdriver)
690 return -ENOMEM;
691 spin_lock_init(&gm_lock);
692
Jiri Slaby55b307d2006-12-08 02:38:14 -0800693 for (i = 0; i < MXSER_BOARDS; i++)
694 mxser_boards[i].board_type = -1;
Jiri Slaby037ad482006-12-08 02:38:11 -0800695
696 printk(KERN_INFO "MOXA Smartio/Industio family driver version %s\n",
697 MXSER_VERSION);
698
699 /* Initialize the tty_driver structure */
700 memset(mxvar_sdriver, 0, sizeof(struct tty_driver));
701 mxvar_sdriver->magic = TTY_DRIVER_MAGIC;
702 mxvar_sdriver->name = "ttyM";
703 mxvar_sdriver->major = ttymajor;
704 mxvar_sdriver->minor_start = 0;
705 mxvar_sdriver->num = MXSER_PORTS + 1;
706 mxvar_sdriver->type = TTY_DRIVER_TYPE_SERIAL;
707 mxvar_sdriver->subtype = SERIAL_TYPE_NORMAL;
708 mxvar_sdriver->init_termios = tty_std_termios;
709 mxvar_sdriver->init_termios.c_cflag = B9600|CS8|CREAD|HUPCL|CLOCAL;
710 mxvar_sdriver->flags = TTY_DRIVER_REAL_RAW;
711 tty_set_operations(mxvar_sdriver, &mxser_ops);
712 mxvar_sdriver->ttys = mxvar_tty;
713 mxvar_sdriver->termios = mxvar_termios;
714 mxvar_sdriver->termios_locked = mxvar_termios_locked;
715
716 mxvar_diagflag = 0;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800717 memset(mxser_boards, 0, sizeof(mxser_boards));
Jiri Slaby037ad482006-12-08 02:38:11 -0800718 memset(&mxvar_log, 0, sizeof(struct mxser_log));
719
720 memset(&mxser_msr, 0, sizeof(unsigned char) * (MXSER_PORTS + 1));
721 memset(&mon_data_ext, 0, sizeof(struct mxser_mon_ext));
722 memset(&mxser_set_baud_method, 0, sizeof(int) * (MXSER_PORTS + 1));
Jiri Slaby037ad482006-12-08 02:38:11 -0800723
724 m = 0;
725 /* Start finding ISA boards here */
726 for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
727 int cap;
728
729 if (!(cap = mxserBoardCAP[b]))
730 continue;
731
Jiri Slaby55b307d2006-12-08 02:38:14 -0800732 brd = &mxser_boards[m];
733 retval = mxser_get_ISA_conf(cap, brd);
Jiri Slaby037ad482006-12-08 02:38:11 -0800734
735 if (retval != 0)
736 printk(KERN_INFO "Found MOXA %s board (CAP=0x%x)\n",
Jiri Slaby55b307d2006-12-08 02:38:14 -0800737 mxser_brdname[brd->board_type - 1], ioaddr[b]);
Jiri Slaby037ad482006-12-08 02:38:11 -0800738
739 if (retval <= 0) {
740 if (retval == MXSER_ERR_IRQ)
741 printk(KERN_ERR "Invalid interrupt number, "
742 "board not configured\n");
743 else if (retval == MXSER_ERR_IRQ_CONFLIT)
744 printk(KERN_ERR "Invalid interrupt number, "
745 "board not configured\n");
746 else if (retval == MXSER_ERR_VECTOR)
747 printk(KERN_ERR "Invalid interrupt vector, "
748 "board not configured\n");
749 else if (retval == MXSER_ERR_IOADDR)
750 printk(KERN_ERR "Invalid I/O address, "
751 "board not configured\n");
752
753 continue;
754 }
755
Jiri Slaby55b307d2006-12-08 02:38:14 -0800756 brd->pdev = NULL;
Jiri Slaby037ad482006-12-08 02:38:11 -0800757
Jiri Slaby037ad482006-12-08 02:38:11 -0800758 /* mxser_initbrd will hook ISR. */
Jiri Slaby55b307d2006-12-08 02:38:14 -0800759 if (mxser_initbrd(brd) < 0)
Jiri Slaby037ad482006-12-08 02:38:11 -0800760 continue;
761
762 m++;
763 }
764
765 /* Start finding ISA boards from module arg */
766 for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
Jiri Slaby3306ce32006-12-08 02:38:13 -0800767 unsigned long cap;
Jiri Slaby037ad482006-12-08 02:38:11 -0800768
769 if (!(cap = ioaddr[b]))
770 continue;
771
Jiri Slaby55b307d2006-12-08 02:38:14 -0800772 brd = &mxser_boards[m];
773 retval = mxser_get_ISA_conf(cap, &mxser_boards[m]);
Jiri Slaby037ad482006-12-08 02:38:11 -0800774
775 if (retval != 0)
776 printk(KERN_INFO "Found MOXA %s board (CAP=0x%x)\n",
Jiri Slaby55b307d2006-12-08 02:38:14 -0800777 mxser_brdname[brd->board_type - 1], ioaddr[b]);
Jiri Slaby037ad482006-12-08 02:38:11 -0800778
779 if (retval <= 0) {
780 if (retval == MXSER_ERR_IRQ)
781 printk(KERN_ERR "Invalid interrupt number, "
782 "board not configured\n");
783 else if (retval == MXSER_ERR_IRQ_CONFLIT)
784 printk(KERN_ERR "Invalid interrupt number, "
785 "board not configured\n");
786 else if (retval == MXSER_ERR_VECTOR)
787 printk(KERN_ERR "Invalid interrupt vector, "
788 "board not configured\n");
789 else if (retval == MXSER_ERR_IOADDR)
790 printk(KERN_ERR "Invalid I/O address, "
791 "board not configured\n");
792
793 continue;
794 }
795
Jiri Slaby55b307d2006-12-08 02:38:14 -0800796 brd->pdev = NULL;
Jiri Slaby037ad482006-12-08 02:38:11 -0800797 /* mxser_initbrd will hook ISR. */
Jiri Slaby55b307d2006-12-08 02:38:14 -0800798 if (mxser_initbrd(brd) < 0)
Jiri Slaby037ad482006-12-08 02:38:11 -0800799 continue;
800
801 m++;
802 }
803
804 /* start finding PCI board here */
Jiri Slaby037ad482006-12-08 02:38:11 -0800805 n = ARRAY_SIZE(mxser_pcibrds) - 1;
Jiri Slaby037ad482006-12-08 02:38:11 -0800806 b = 0;
807 while (b < n) {
808 pdev = pci_get_device(mxser_pcibrds[b].vendor,
809 mxser_pcibrds[b].device, pdev);
810 if (pdev == NULL) {
811 b++;
812 continue;
813 }
Jiri Slaby037ad482006-12-08 02:38:11 -0800814 printk(KERN_INFO "Found MOXA %s board(BusNo=%d,DevNo=%d)\n",
815 mxser_brdname[(int) (mxser_pcibrds[b].driver_data) - 1],
Jiri Slaby55b307d2006-12-08 02:38:14 -0800816 pdev->bus->number, PCI_SLOT(pdev->devfn));
Jiri Slaby037ad482006-12-08 02:38:11 -0800817 if (m >= MXSER_BOARDS)
818 printk(KERN_ERR
819 "Too many Smartio/Industio family boards find "
820 "(maximum %d), board not configured\n",
821 MXSER_BOARDS);
822 else {
823 if (pci_enable_device(pdev)) {
824 printk(KERN_ERR "Moxa SmartI/O PCI enable "
825 "fail !\n");
826 continue;
827 }
Jiri Slaby55b307d2006-12-08 02:38:14 -0800828 brd = &mxser_boards[m];
829 brd->pdev = pdev;
830 retval = mxser_get_PCI_conf(
Jiri Slaby037ad482006-12-08 02:38:11 -0800831 (int)mxser_pcibrds[b].driver_data,
Jiri Slaby55b307d2006-12-08 02:38:14 -0800832 brd, pdev);
Jiri Slaby037ad482006-12-08 02:38:11 -0800833 if (retval < 0) {
834 if (retval == MXSER_ERR_IRQ)
835 printk(KERN_ERR
836 "Invalid interrupt number, "
837 "board not configured\n");
838 else if (retval == MXSER_ERR_IRQ_CONFLIT)
839 printk(KERN_ERR
840 "Invalid interrupt number, "
841 "board not configured\n");
842 else if (retval == MXSER_ERR_VECTOR)
843 printk(KERN_ERR
844 "Invalid interrupt vector, "
845 "board not configured\n");
846 else if (retval == MXSER_ERR_IOADDR)
847 printk(KERN_ERR
848 "Invalid I/O address, "
849 "board not configured\n");
850 continue;
851 }
Jiri Slaby037ad482006-12-08 02:38:11 -0800852 /* mxser_initbrd will hook ISR. */
Jiri Slaby55b307d2006-12-08 02:38:14 -0800853 if (mxser_initbrd(brd) < 0)
Jiri Slaby037ad482006-12-08 02:38:11 -0800854 continue;
855 m++;
856 /* Keep an extra reference if we succeeded. It will
857 be returned at unload time */
858 pci_dev_get(pdev);
859 }
860 }
Jiri Slaby037ad482006-12-08 02:38:11 -0800861
862 retval = tty_register_driver(mxvar_sdriver);
863 if (retval) {
864 printk(KERN_ERR "Couldn't install MOXA Smartio/Industio family"
865 " driver !\n");
866 put_tty_driver(mxvar_sdriver);
867
868 for (i = 0; i < MXSER_BOARDS; i++) {
Jiri Slaby55b307d2006-12-08 02:38:14 -0800869 if (mxser_boards[i].board_type == -1)
Jiri Slaby037ad482006-12-08 02:38:11 -0800870 continue;
871 else {
Jiri Slaby55b307d2006-12-08 02:38:14 -0800872 free_irq(mxser_boards[i].irq, &mxser_boards[i]);
Jiri Slaby037ad482006-12-08 02:38:11 -0800873 /* todo: release io, vector */
874 }
875 }
876 return retval;
877 }
878
879 return 0;
880}
881
882static void mxser_do_softint(void *private_)
883{
Jiri Slaby55b307d2006-12-08 02:38:14 -0800884 struct mxser_port *info = private_;
Jiri Slaby037ad482006-12-08 02:38:11 -0800885 struct tty_struct *tty;
886
887 tty = info->tty;
888
Jiri Slaby3306ce32006-12-08 02:38:13 -0800889 if (test_and_clear_bit(MXSER_EVENT_TXLOW, &info->event))
890 tty_wakeup(tty);
891 if (test_and_clear_bit(MXSER_EVENT_HANGUP, &info->event))
892 tty_hangup(tty);
Jiri Slaby037ad482006-12-08 02:38:11 -0800893}
894
Jiri Slaby55b307d2006-12-08 02:38:14 -0800895static unsigned char mxser_get_msr(int baseaddr, int mode, int port)
Jiri Slaby037ad482006-12-08 02:38:11 -0800896{
897 unsigned char status = 0;
898
899 status = inb(baseaddr + UART_MSR);
900
901 mxser_msr[port] &= 0x0F;
902 mxser_msr[port] |= status;
903 status = mxser_msr[port];
904 if (mode)
905 mxser_msr[port] = 0;
906
907 return status;
908}
909
910/*
911 * This routine is called whenever a serial port is opened. It
912 * enables interrupts for a serial port, linking in its async structure into
913 * the IRQ chain. It also performs the serial-specific
914 * initialization for the tty structure.
915 */
916static int mxser_open(struct tty_struct *tty, struct file *filp)
917{
Jiri Slaby55b307d2006-12-08 02:38:14 -0800918 struct mxser_port *info;
Jiri Slaby037ad482006-12-08 02:38:11 -0800919 int retval, line;
920
921 /* initialize driver_data in case something fails */
922 tty->driver_data = NULL;
923
924 line = tty->index;
925 if (line == MXSER_PORTS)
926 return 0;
927 if (line < 0 || line > MXSER_PORTS)
928 return -ENODEV;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800929 info = &mxser_boards[line / MXSER_PORTS_PER_BOARD].ports[line % MXSER_PORTS_PER_BOARD];
930 if (!info->ioaddr)
Jiri Slaby037ad482006-12-08 02:38:11 -0800931 return -ENODEV;
932
933 tty->driver_data = info;
934 info->tty = tty;
935 /*
936 * Start up serial port
937 */
Jiri Slaby3306ce32006-12-08 02:38:13 -0800938 info->count++;
Jiri Slaby037ad482006-12-08 02:38:11 -0800939 retval = mxser_startup(info);
940 if (retval)
941 return retval;
942
943 retval = mxser_block_til_ready(tty, filp, info);
944 if (retval)
945 return retval;
946
Jiri Slaby037ad482006-12-08 02:38:11 -0800947 if ((info->count == 1) && (info->flags & ASYNC_SPLIT_TERMIOS)) {
948 if (tty->driver->subtype == SERIAL_TYPE_NORMAL)
949 *tty->termios = info->normal_termios;
950 else
951 *tty->termios = info->callout_termios;
952 mxser_change_speed(info, NULL);
953 }
954
Andrew Morton08a4ae42006-12-08 02:38:12 -0800955 info->session = process_session(current);
Jiri Slaby037ad482006-12-08 02:38:11 -0800956 info->pgrp = process_group(current);
957
958 /*
959 status = mxser_get_msr(info->base, 0, info->port);
960 mxser_check_modem_status(info, status);
961 */
962
963/* unmark here for very high baud rate (ex. 921600 bps) used */
964 tty->low_latency = 1;
965 return 0;
966}
967
968/*
969 * This routine is called when the serial port gets closed. First, we
970 * wait for the last remaining data to be sent. Then, we unlink its
971 * async structure from the interrupt chain if necessary, and we free
972 * that IRQ if nothing is left in the chain.
973 */
974static void mxser_close(struct tty_struct *tty, struct file *filp)
975{
Jiri Slaby55b307d2006-12-08 02:38:14 -0800976 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -0800977
978 unsigned long timeout;
979 unsigned long flags;
980 struct tty_ldisc *ld;
981
982 if (tty->index == MXSER_PORTS)
983 return;
984 if (!info)
985 return;
986
987 spin_lock_irqsave(&info->slock, flags);
988
989 if (tty_hung_up_p(filp)) {
990 spin_unlock_irqrestore(&info->slock, flags);
991 return;
992 }
993 if ((tty->count == 1) && (info->count != 1)) {
994 /*
995 * Uh, oh. tty->count is 1, which means that the tty
996 * structure will be freed. Info->count should always
997 * be one in these conditions. If it's greater than
998 * one, we've got real problems, since it means the
999 * serial port won't be shutdown.
1000 */
1001 printk(KERN_ERR "mxser_close: bad serial port count; "
1002 "tty->count is 1, info->count is %d\n", info->count);
1003 info->count = 1;
1004 }
1005 if (--info->count < 0) {
1006 printk(KERN_ERR "mxser_close: bad serial port count for "
Jiri Slaby55b307d2006-12-08 02:38:14 -08001007 "ttys%d: %d\n", tty->index, info->count);
Jiri Slaby037ad482006-12-08 02:38:11 -08001008 info->count = 0;
1009 }
1010 if (info->count) {
1011 spin_unlock_irqrestore(&info->slock, flags);
1012 return;
1013 }
1014 info->flags |= ASYNC_CLOSING;
1015 spin_unlock_irqrestore(&info->slock, flags);
1016 /*
1017 * Save the termios structure, since this port may have
1018 * separate termios for callout and dialin.
1019 */
1020 if (info->flags & ASYNC_NORMAL_ACTIVE)
1021 info->normal_termios = *tty->termios;
1022 /*
1023 * Now we wait for the transmit buffer to clear; and we notify
1024 * the line discipline to only process XON/XOFF characters.
1025 */
1026 tty->closing = 1;
1027 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1028 tty_wait_until_sent(tty, info->closing_wait);
1029 /*
1030 * At this point we stop accepting input. To do this, we
1031 * disable the receive line status interrupts, and tell the
1032 * interrupt driver to stop checking the data ready bit in the
1033 * line status register.
1034 */
1035 info->IER &= ~UART_IER_RLSI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001036 if (info->board->chip_flag)
Jiri Slaby037ad482006-12-08 02:38:11 -08001037 info->IER &= ~MOXA_MUST_RECV_ISR;
1038/* by William
1039 info->read_status_mask &= ~UART_LSR_DR;
1040*/
1041 if (info->flags & ASYNC_INITIALIZED) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08001042 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001043 /*
1044 * Before we drop DTR, make sure the UART transmitter
1045 * has completely drained; this is especially
1046 * important if there is a transmit FIFO!
1047 */
1048 timeout = jiffies + HZ;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001049 while (!(inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001050 schedule_timeout_interruptible(5);
1051 if (time_after(jiffies, timeout))
1052 break;
1053 }
1054 }
1055 mxser_shutdown(info);
1056
1057 if (tty->driver->flush_buffer)
1058 tty->driver->flush_buffer(tty);
1059
1060 ld = tty_ldisc_ref(tty);
1061 if (ld) {
1062 if (ld->flush_buffer)
1063 ld->flush_buffer(tty);
1064 tty_ldisc_deref(ld);
1065 }
1066
1067 tty->closing = 0;
1068 info->event = 0;
1069 info->tty = NULL;
1070 if (info->blocked_open) {
1071 if (info->close_delay)
1072 schedule_timeout_interruptible(info->close_delay);
1073 wake_up_interruptible(&info->open_wait);
1074 }
1075
1076 info->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
1077 wake_up_interruptible(&info->close_wait);
1078
1079}
1080
1081static int mxser_write(struct tty_struct *tty, const unsigned char *buf, int count)
1082{
1083 int c, total = 0;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001084 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001085 unsigned long flags;
1086
1087 if (!info->xmit_buf)
1088 return 0;
1089
1090 while (1) {
1091 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1092 SERIAL_XMIT_SIZE - info->xmit_head));
1093 if (c <= 0)
1094 break;
1095
1096 memcpy(info->xmit_buf + info->xmit_head, buf, c);
1097 spin_lock_irqsave(&info->slock, flags);
1098 info->xmit_head = (info->xmit_head + c) &
1099 (SERIAL_XMIT_SIZE - 1);
1100 info->xmit_cnt += c;
1101 spin_unlock_irqrestore(&info->slock, flags);
1102
1103 buf += c;
1104 count -= c;
1105 total += c;
1106 }
1107
Jiri Slaby3306ce32006-12-08 02:38:13 -08001108 if (info->xmit_cnt && !tty->stopped
1109 /*&& !(info->IER & UART_IER_THRI)*/) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001110 if (!tty->hw_stopped ||
1111 (info->type == PORT_16550A) ||
Jiri Slaby55b307d2006-12-08 02:38:14 -08001112 (info->board->chip_flag)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001113 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby55b307d2006-12-08 02:38:14 -08001114 outb(info->IER & ~UART_IER_THRI, info->ioaddr +
1115 UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001116 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001117 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001118 spin_unlock_irqrestore(&info->slock, flags);
1119 }
1120 }
1121 return total;
1122}
1123
1124static void mxser_put_char(struct tty_struct *tty, unsigned char ch)
1125{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001126 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001127 unsigned long flags;
1128
1129 if (!info->xmit_buf)
1130 return;
1131
1132 if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1)
1133 return;
1134
1135 spin_lock_irqsave(&info->slock, flags);
1136 info->xmit_buf[info->xmit_head++] = ch;
1137 info->xmit_head &= SERIAL_XMIT_SIZE - 1;
1138 info->xmit_cnt++;
1139 spin_unlock_irqrestore(&info->slock, flags);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001140 if (!tty->stopped /*&& !(info->IER & UART_IER_THRI)*/) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001141 if (!tty->hw_stopped ||
1142 (info->type == PORT_16550A) ||
Jiri Slaby55b307d2006-12-08 02:38:14 -08001143 info->board->chip_flag) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001144 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby55b307d2006-12-08 02:38:14 -08001145 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001146 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001147 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001148 spin_unlock_irqrestore(&info->slock, flags);
1149 }
1150 }
1151}
1152
1153
1154static void mxser_flush_chars(struct tty_struct *tty)
1155{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001156 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001157 unsigned long flags;
1158
1159 if (info->xmit_cnt <= 0 ||
1160 tty->stopped ||
1161 !info->xmit_buf ||
1162 (tty->hw_stopped &&
1163 (info->type != PORT_16550A) &&
Jiri Slaby55b307d2006-12-08 02:38:14 -08001164 (!info->board->chip_flag)
Jiri Slaby037ad482006-12-08 02:38:11 -08001165 ))
1166 return;
1167
1168 spin_lock_irqsave(&info->slock, flags);
1169
Jiri Slaby55b307d2006-12-08 02:38:14 -08001170 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001171 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001172 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001173
1174 spin_unlock_irqrestore(&info->slock, flags);
1175}
1176
1177static int mxser_write_room(struct tty_struct *tty)
1178{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001179 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001180 int ret;
1181
1182 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1183 if (ret < 0)
1184 ret = 0;
1185 return ret;
1186}
1187
1188static int mxser_chars_in_buffer(struct tty_struct *tty)
1189{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001190 struct mxser_port *info = tty->driver_data;
Jiri Slaby3306ce32006-12-08 02:38:13 -08001191 int len = info->xmit_cnt;
1192
Jiri Slaby55b307d2006-12-08 02:38:14 -08001193 if (!(inb(info->ioaddr + UART_LSR) & UART_LSR_THRE))
Jiri Slaby3306ce32006-12-08 02:38:13 -08001194 len++;
1195
1196 return len;
Jiri Slaby037ad482006-12-08 02:38:11 -08001197}
1198
1199static void mxser_flush_buffer(struct tty_struct *tty)
1200{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001201 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001202 char fcr;
1203 unsigned long flags;
1204
1205
1206 spin_lock_irqsave(&info->slock, flags);
1207 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1208
1209 /* below added by shinhay */
Jiri Slaby55b307d2006-12-08 02:38:14 -08001210 fcr = inb(info->ioaddr + UART_FCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08001211 outb((fcr | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
Jiri Slaby55b307d2006-12-08 02:38:14 -08001212 info->ioaddr + UART_FCR);
1213 outb(fcr, info->ioaddr + UART_FCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08001214
1215 spin_unlock_irqrestore(&info->slock, flags);
1216 /* above added by shinhay */
1217
1218 wake_up_interruptible(&tty->write_wait);
1219 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) && tty->ldisc.write_wakeup)
1220 (tty->ldisc.write_wakeup) (tty);
1221}
1222
1223static int mxser_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
1224{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001225 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001226 int retval;
1227 struct async_icount cprev, cnow; /* kernel counter temps */
1228 struct serial_icounter_struct __user *p_cuser;
1229 unsigned long templ;
1230 unsigned long flags;
1231 void __user *argp = (void __user *)arg;
1232
1233 if (tty->index == MXSER_PORTS)
1234 return mxser_ioctl_special(cmd, argp);
1235
1236 /* following add by Victor Yu. 01-05-2004 */
1237 if (cmd == MOXA_SET_OP_MODE || cmd == MOXA_GET_OP_MODE) {
Jiri Slaby3306ce32006-12-08 02:38:13 -08001238 int p;
1239 unsigned long opmode;
Jiri Slaby037ad482006-12-08 02:38:11 -08001240 static unsigned char ModeMask[] = { 0xfc, 0xf3, 0xcf, 0x3f };
1241 int shiftbit;
1242 unsigned char val, mask;
1243
Jiri Slaby55b307d2006-12-08 02:38:14 -08001244 p = tty->index % 4;
Jiri Slaby037ad482006-12-08 02:38:11 -08001245 if (cmd == MOXA_SET_OP_MODE) {
1246 if (get_user(opmode, (int __user *) argp))
1247 return -EFAULT;
1248 if (opmode != RS232_MODE &&
1249 opmode != RS485_2WIRE_MODE &&
1250 opmode != RS422_MODE &&
1251 opmode != RS485_4WIRE_MODE)
1252 return -EFAULT;
1253 mask = ModeMask[p];
1254 shiftbit = p * 2;
1255 val = inb(info->opmode_ioaddr);
1256 val &= mask;
1257 val |= (opmode << shiftbit);
1258 outb(val, info->opmode_ioaddr);
1259 } else {
1260 shiftbit = p * 2;
1261 opmode = inb(info->opmode_ioaddr) >> shiftbit;
1262 opmode &= OP_MODE_MASK;
1263 if (copy_to_user(argp, &opmode, sizeof(int)))
1264 return -EFAULT;
1265 }
1266 return 0;
1267 }
1268 /* above add by Victor Yu. 01-05-2004 */
1269
1270 if ((cmd != TIOCGSERIAL) && (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
1271 if (tty->flags & (1 << TTY_IO_ERROR))
1272 return -EIO;
1273 }
1274 switch (cmd) {
1275 case TCSBRK: /* SVID version: non-zero arg --> no break */
1276 retval = tty_check_change(tty);
1277 if (retval)
1278 return retval;
1279 tty_wait_until_sent(tty, 0);
1280 if (!arg)
1281 mxser_send_break(info, HZ / 4); /* 1/4 second */
1282 return 0;
1283 case TCSBRKP: /* support for POSIX tcsendbreak() */
1284 retval = tty_check_change(tty);
1285 if (retval)
1286 return retval;
1287 tty_wait_until_sent(tty, 0);
1288 mxser_send_break(info, arg ? arg * (HZ / 10) : HZ / 4);
1289 return 0;
1290 case TIOCGSOFTCAR:
1291 return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *)argp);
1292 case TIOCSSOFTCAR:
1293 if (get_user(templ, (unsigned long __user *) argp))
1294 return -EFAULT;
1295 arg = templ;
1296 tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) | (arg ? CLOCAL : 0));
1297 return 0;
1298 case TIOCGSERIAL:
1299 return mxser_get_serial_info(info, argp);
1300 case TIOCSSERIAL:
1301 return mxser_set_serial_info(info, argp);
1302 case TIOCSERGETLSR: /* Get line status register */
1303 return mxser_get_lsr_info(info, argp);
1304 /*
1305 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1306 * - mask passed in arg for lines of interest
1307 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1308 * Caller should use TIOCGICOUNT to see which one it was
1309 */
1310 case TIOCMIWAIT: {
1311 DECLARE_WAITQUEUE(wait, current);
1312 int ret;
1313 spin_lock_irqsave(&info->slock, flags);
1314 cprev = info->icount; /* note the counters on entry */
1315 spin_unlock_irqrestore(&info->slock, flags);
1316
1317 add_wait_queue(&info->delta_msr_wait, &wait);
1318 while (1) {
1319 spin_lock_irqsave(&info->slock, flags);
1320 cnow = info->icount; /* atomic copy */
1321 spin_unlock_irqrestore(&info->slock, flags);
1322
1323 set_current_state(TASK_INTERRUPTIBLE);
1324 if (((arg & TIOCM_RNG) &&
1325 (cnow.rng != cprev.rng)) ||
1326 ((arg & TIOCM_DSR) &&
1327 (cnow.dsr != cprev.dsr)) ||
1328 ((arg & TIOCM_CD) &&
1329 (cnow.dcd != cprev.dcd)) ||
1330 ((arg & TIOCM_CTS) &&
1331 (cnow.cts != cprev.cts))) {
1332 ret = 0;
1333 break;
1334 }
1335 /* see if a signal did it */
1336 if (signal_pending(current)) {
1337 ret = -ERESTARTSYS;
1338 break;
1339 }
1340 cprev = cnow;
1341 }
1342 current->state = TASK_RUNNING;
1343 remove_wait_queue(&info->delta_msr_wait, &wait);
1344 break;
1345 }
1346 /* NOTREACHED */
1347 /*
1348 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1349 * Return: write counters to the user passed counter struct
1350 * NB: both 1->0 and 0->1 transitions are counted except for
1351 * RI where only 0->1 is counted.
1352 */
1353 case TIOCGICOUNT:
1354 spin_lock_irqsave(&info->slock, flags);
1355 cnow = info->icount;
1356 spin_unlock_irqrestore(&info->slock, flags);
1357 p_cuser = argp;
1358 /* modified by casper 1/11/2000 */
1359 if (put_user(cnow.frame, &p_cuser->frame))
1360 return -EFAULT;
1361 if (put_user(cnow.brk, &p_cuser->brk))
1362 return -EFAULT;
1363 if (put_user(cnow.overrun, &p_cuser->overrun))
1364 return -EFAULT;
1365 if (put_user(cnow.buf_overrun, &p_cuser->buf_overrun))
1366 return -EFAULT;
1367 if (put_user(cnow.parity, &p_cuser->parity))
1368 return -EFAULT;
1369 if (put_user(cnow.rx, &p_cuser->rx))
1370 return -EFAULT;
1371 if (put_user(cnow.tx, &p_cuser->tx))
1372 return -EFAULT;
1373 put_user(cnow.cts, &p_cuser->cts);
1374 put_user(cnow.dsr, &p_cuser->dsr);
1375 put_user(cnow.rng, &p_cuser->rng);
1376 put_user(cnow.dcd, &p_cuser->dcd);
1377 return 0;
1378 case MOXA_HighSpeedOn:
1379 return put_user(info->baud_base != 115200 ? 1 : 0, (int __user *)argp);
1380 case MOXA_SDS_RSTICOUNTER: {
1381 info->mon_data.rxcnt = 0;
1382 info->mon_data.txcnt = 0;
1383 return 0;
1384 }
1385/* (above) added by James. */
1386 case MOXA_ASPP_SETBAUD:{
1387 long baud;
1388 if (get_user(baud, (long __user *)argp))
1389 return -EFAULT;
1390 mxser_set_baud(info, baud);
1391 return 0;
1392 }
1393 case MOXA_ASPP_GETBAUD:
1394 if (copy_to_user(argp, &info->realbaud, sizeof(long)))
1395 return -EFAULT;
1396
1397 return 0;
1398
1399 case MOXA_ASPP_OQUEUE:{
1400 int len, lsr;
1401
1402 len = mxser_chars_in_buffer(tty);
1403
Jiri Slaby55b307d2006-12-08 02:38:14 -08001404 lsr = inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT;
Jiri Slaby037ad482006-12-08 02:38:11 -08001405
1406 len += (lsr ? 0 : 1);
1407
1408 if (copy_to_user(argp, &len, sizeof(int)))
1409 return -EFAULT;
1410
1411 return 0;
1412 }
1413 case MOXA_ASPP_MON: {
1414 int mcr, status;
1415
1416 /* info->mon_data.ser_param = tty->termios->c_cflag; */
1417
Jiri Slaby55b307d2006-12-08 02:38:14 -08001418 status = mxser_get_msr(info->ioaddr, 1, tty->index);
Jiri Slaby037ad482006-12-08 02:38:11 -08001419 mxser_check_modem_status(info, status);
1420
Jiri Slaby55b307d2006-12-08 02:38:14 -08001421 mcr = inb(info->ioaddr + UART_MCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08001422 if (mcr & MOXA_MUST_MCR_XON_FLAG)
1423 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFHOLD;
1424 else
1425 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFHOLD;
1426
1427 if (mcr & MOXA_MUST_MCR_TX_XON)
1428 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFXENT;
1429 else
1430 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFXENT;
1431
1432 if (info->tty->hw_stopped)
1433 info->mon_data.hold_reason |= NPPI_NOTIFY_CTSHOLD;
1434 else
1435 info->mon_data.hold_reason &= ~NPPI_NOTIFY_CTSHOLD;
1436
1437 if (copy_to_user(argp, &info->mon_data,
1438 sizeof(struct mxser_mon)))
1439 return -EFAULT;
1440
1441 return 0;
1442 }
1443
1444 case MOXA_ASPP_LSTATUS: {
1445 if (copy_to_user(argp, &info->err_shadow,
1446 sizeof(unsigned char)))
1447 return -EFAULT;
1448
1449 info->err_shadow = 0;
1450 return 0;
1451 }
1452 case MOXA_SET_BAUD_METHOD: {
1453 int method;
1454
1455 if (get_user(method, (int __user *)argp))
1456 return -EFAULT;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001457 mxser_set_baud_method[tty->index] = method;
Jiri Slaby037ad482006-12-08 02:38:11 -08001458 if (copy_to_user(argp, &method, sizeof(int)))
1459 return -EFAULT;
1460
1461 return 0;
1462 }
1463 default:
1464 return -ENOIOCTLCMD;
1465 }
1466 return 0;
1467}
1468
1469#ifndef CMSPAR
1470#define CMSPAR 010000000000
1471#endif
1472
1473static int mxser_ioctl_special(unsigned int cmd, void __user *argp)
1474{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001475 struct mxser_port *port;
1476 int result, status;
1477 unsigned int i, j;
Jiri Slaby037ad482006-12-08 02:38:11 -08001478
1479 switch (cmd) {
1480 case MOXA_GET_CONF:
Jiri Slaby55b307d2006-12-08 02:38:14 -08001481/* if (copy_to_user(argp, mxsercfg,
Jiri Slaby037ad482006-12-08 02:38:11 -08001482 sizeof(struct mxser_hwconf) * 4))
1483 return -EFAULT;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001484 return 0;*/
1485 return -ENXIO;
Jiri Slaby037ad482006-12-08 02:38:11 -08001486 case MOXA_GET_MAJOR:
1487 if (copy_to_user(argp, &ttymajor, sizeof(int)))
1488 return -EFAULT;
1489 return 0;
1490
1491 case MOXA_GET_CUMAJOR:
1492 if (copy_to_user(argp, &calloutmajor, sizeof(int)))
1493 return -EFAULT;
1494 return 0;
1495
1496 case MOXA_CHKPORTENABLE:
1497 result = 0;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001498
1499 for (i = 0; i < MXSER_BOARDS; i++)
1500 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++)
1501 if (mxser_boards[i].ports[j].ioaddr)
1502 result |= (1 << i);
1503
Jiri Slaby037ad482006-12-08 02:38:11 -08001504 return put_user(result, (unsigned long __user *)argp);
1505 case MOXA_GETDATACOUNT:
1506 if (copy_to_user(argp, &mxvar_log, sizeof(mxvar_log)))
1507 return -EFAULT;
1508 return 0;
1509 case MOXA_GETMSTATUS:
Jiri Slaby55b307d2006-12-08 02:38:14 -08001510 for (i = 0; i < MXSER_BOARDS; i++)
1511 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
1512 port = &mxser_boards[i].ports[j];
1513
1514 GMStatus[i].ri = 0;
1515 if (!port->ioaddr) {
1516 GMStatus[i].dcd = 0;
1517 GMStatus[i].dsr = 0;
1518 GMStatus[i].cts = 0;
1519 continue;
1520 }
1521
1522 if (!port->tty || !port->tty->termios)
1523 GMStatus[i].cflag =
1524 port->normal_termios.c_cflag;
1525 else
1526 GMStatus[i].cflag =
1527 port->tty->termios->c_cflag;
1528
1529 status = inb(port->ioaddr + UART_MSR);
1530 if (status & 0x80 /*UART_MSR_DCD */ )
1531 GMStatus[i].dcd = 1;
1532 else
1533 GMStatus[i].dcd = 0;
1534
1535 if (status & 0x20 /*UART_MSR_DSR */ )
1536 GMStatus[i].dsr = 1;
1537 else
1538 GMStatus[i].dsr = 0;
1539
1540
1541 if (status & 0x10 /*UART_MSR_CTS */ )
1542 GMStatus[i].cts = 1;
1543 else
1544 GMStatus[i].cts = 0;
Jiri Slaby037ad482006-12-08 02:38:11 -08001545 }
Jiri Slaby037ad482006-12-08 02:38:11 -08001546 if (copy_to_user(argp, GMStatus,
1547 sizeof(struct mxser_mstatus) * MXSER_PORTS))
1548 return -EFAULT;
1549 return 0;
1550 case MOXA_ASPP_MON_EXT: {
Jiri Slaby55b307d2006-12-08 02:38:14 -08001551 int status, p, shiftbit;
1552 unsigned long opmode;
1553 unsigned cflag, iflag;
Jiri Slaby037ad482006-12-08 02:38:11 -08001554
Jiri Slaby55b307d2006-12-08 02:38:14 -08001555 for (i = 0; i < MXSER_BOARDS; i++)
1556 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
1557 port = &mxser_boards[i].ports[j];
1558 if (!port->ioaddr)
Jiri Slaby037ad482006-12-08 02:38:11 -08001559 continue;
1560
Jiri Slaby55b307d2006-12-08 02:38:14 -08001561 status = mxser_get_msr(port->ioaddr, 0, i);
1562/* mxser_check_modem_status(port, status); */
1563
Jiri Slaby037ad482006-12-08 02:38:11 -08001564 if (status & UART_MSR_TERI)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001565 port->icount.rng++;
Jiri Slaby037ad482006-12-08 02:38:11 -08001566 if (status & UART_MSR_DDSR)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001567 port->icount.dsr++;
Jiri Slaby037ad482006-12-08 02:38:11 -08001568 if (status & UART_MSR_DDCD)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001569 port->icount.dcd++;
Jiri Slaby037ad482006-12-08 02:38:11 -08001570 if (status & UART_MSR_DCTS)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001571 port->icount.cts++;
Jiri Slaby037ad482006-12-08 02:38:11 -08001572
Jiri Slaby55b307d2006-12-08 02:38:14 -08001573 port->mon_data.modem_status = status;
1574 mon_data_ext.rx_cnt[i] = port->mon_data.rxcnt;
1575 mon_data_ext.tx_cnt[i] = port->mon_data.txcnt;
1576 mon_data_ext.up_rxcnt[i] =
1577 port->mon_data.up_rxcnt;
1578 mon_data_ext.up_txcnt[i] =
1579 port->mon_data.up_txcnt;
1580 mon_data_ext.modem_status[i] =
1581 port->mon_data.modem_status;
1582 mon_data_ext.baudrate[i] = port->realbaud;
Jiri Slaby037ad482006-12-08 02:38:11 -08001583
Jiri Slaby55b307d2006-12-08 02:38:14 -08001584 if (!port->tty || !port->tty->termios) {
1585 cflag = port->normal_termios.c_cflag;
1586 iflag = port->normal_termios.c_iflag;
Jiri Slaby037ad482006-12-08 02:38:11 -08001587 } else {
Jiri Slaby55b307d2006-12-08 02:38:14 -08001588 cflag = port->tty->termios->c_cflag;
1589 iflag = port->tty->termios->c_iflag;
Jiri Slaby037ad482006-12-08 02:38:11 -08001590 }
1591
1592 mon_data_ext.databits[i] = cflag & CSIZE;
1593
1594 mon_data_ext.stopbits[i] = cflag & CSTOPB;
1595
Jiri Slaby55b307d2006-12-08 02:38:14 -08001596 mon_data_ext.parity[i] =
1597 cflag & (PARENB | PARODD | CMSPAR);
Jiri Slaby037ad482006-12-08 02:38:11 -08001598
1599 mon_data_ext.flowctrl[i] = 0x00;
1600
1601 if (cflag & CRTSCTS)
1602 mon_data_ext.flowctrl[i] |= 0x03;
1603
1604 if (iflag & (IXON | IXOFF))
1605 mon_data_ext.flowctrl[i] |= 0x0C;
1606
Jiri Slaby55b307d2006-12-08 02:38:14 -08001607 if (port->type == PORT_16550A)
Jiri Slaby037ad482006-12-08 02:38:11 -08001608 mon_data_ext.fifo[i] = 1;
1609 else
1610 mon_data_ext.fifo[i] = 0;
1611
1612 p = i % 4;
1613 shiftbit = p * 2;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001614 opmode = inb(port->opmode_ioaddr) >> shiftbit;
Jiri Slaby037ad482006-12-08 02:38:11 -08001615 opmode &= OP_MODE_MASK;
1616
1617 mon_data_ext.iftype[i] = opmode;
1618
1619 }
Jiri Slaby55b307d2006-12-08 02:38:14 -08001620 if (copy_to_user(argp, &mon_data_ext,
1621 sizeof(mon_data_ext)))
Jiri Slaby037ad482006-12-08 02:38:11 -08001622 return -EFAULT;
1623
1624 return 0;
1625
Jiri Slaby55b307d2006-12-08 02:38:14 -08001626 } default:
Jiri Slaby037ad482006-12-08 02:38:11 -08001627 return -ENOIOCTLCMD;
1628 }
1629 return 0;
1630}
1631
1632static void mxser_stoprx(struct tty_struct *tty)
1633{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001634 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001635
1636 info->ldisc_stop_rx = 1;
1637 if (I_IXOFF(tty)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001638 /* following add by Victor Yu. 09-02-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08001639 if (info->board->chip_flag) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001640 info->IER &= ~MOXA_MUST_RECV_ISR;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001641 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001642 } else if (!(info->flags & ASYNC_CLOSING)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001643 info->x_char = STOP_CHAR(tty);
Jiri Slaby55b307d2006-12-08 02:38:14 -08001644 outb(0, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001645 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001646 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001647 }
Jiri Slaby037ad482006-12-08 02:38:11 -08001648 }
1649
1650 if (info->tty->termios->c_cflag & CRTSCTS) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001651 info->MCR &= ~UART_MCR_RTS;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001652 outb(info->MCR, info->ioaddr + UART_MCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08001653 }
1654}
1655
1656static void mxser_startrx(struct tty_struct *tty)
1657{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001658 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001659
1660 info->ldisc_stop_rx = 0;
1661 if (I_IXOFF(tty)) {
1662 if (info->x_char)
1663 info->x_char = 0;
1664 else {
Jiri Slaby037ad482006-12-08 02:38:11 -08001665 /* following add by Victor Yu. 09-02-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08001666 if (info->board->chip_flag) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001667 info->IER |= MOXA_MUST_RECV_ISR;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001668 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001669 } else if (!(info->flags & ASYNC_CLOSING)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001670 info->x_char = START_CHAR(tty);
Jiri Slaby55b307d2006-12-08 02:38:14 -08001671 outb(0, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001672 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001673 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001674 }
Jiri Slaby037ad482006-12-08 02:38:11 -08001675 }
1676 }
1677
1678 if (info->tty->termios->c_cflag & CRTSCTS) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001679 info->MCR |= UART_MCR_RTS;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001680 outb(info->MCR, info->ioaddr + UART_MCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08001681 }
1682}
1683
1684/*
1685 * This routine is called by the upper-layer tty layer to signal that
1686 * incoming characters should be throttled.
1687 */
1688static void mxser_throttle(struct tty_struct *tty)
1689{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001690 struct mxser_port *info = tty->driver_data;
Jiri Slaby3306ce32006-12-08 02:38:13 -08001691 unsigned long flags;
Jiri Slaby037ad482006-12-08 02:38:11 -08001692
Jiri Slaby3306ce32006-12-08 02:38:13 -08001693 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby037ad482006-12-08 02:38:11 -08001694 mxser_stoprx(tty);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001695 spin_unlock_irqrestore(&info->slock, flags);
Jiri Slaby037ad482006-12-08 02:38:11 -08001696}
1697
1698static void mxser_unthrottle(struct tty_struct *tty)
1699{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001700 struct mxser_port *info = tty->driver_data;
Jiri Slaby3306ce32006-12-08 02:38:13 -08001701 unsigned long flags;
Jiri Slaby037ad482006-12-08 02:38:11 -08001702
Jiri Slaby3306ce32006-12-08 02:38:13 -08001703 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby037ad482006-12-08 02:38:11 -08001704 mxser_startrx(tty);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001705 spin_unlock_irqrestore(&info->slock, flags);
Jiri Slaby037ad482006-12-08 02:38:11 -08001706}
1707
1708static void mxser_set_termios(struct tty_struct *tty, struct termios *old_termios)
1709{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001710 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001711 unsigned long flags;
1712
1713 if ((tty->termios->c_cflag != old_termios->c_cflag) ||
1714 (RELEVANT_IFLAG(tty->termios->c_iflag) != RELEVANT_IFLAG(old_termios->c_iflag))) {
1715
1716 mxser_change_speed(info, old_termios);
1717
1718 if ((old_termios->c_cflag & CRTSCTS) &&
1719 !(tty->termios->c_cflag & CRTSCTS)) {
1720 tty->hw_stopped = 0;
1721 mxser_start(tty);
1722 }
1723 }
1724
1725/* Handle sw stopped */
1726 if ((old_termios->c_iflag & IXON) &&
1727 !(tty->termios->c_iflag & IXON)) {
1728 tty->stopped = 0;
1729
1730 /* following add by Victor Yu. 09-02-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08001731 if (info->board->chip_flag) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001732 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby55b307d2006-12-08 02:38:14 -08001733 DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
Jiri Slaby037ad482006-12-08 02:38:11 -08001734 spin_unlock_irqrestore(&info->slock, flags);
1735 }
1736 /* above add by Victor Yu. 09-02-2002 */
1737
1738 mxser_start(tty);
1739 }
1740}
1741
1742/*
1743 * mxser_stop() and mxser_start()
1744 *
1745 * This routines are called before setting or resetting tty->stopped.
1746 * They enable or disable transmitter interrupts, as necessary.
1747 */
1748static void mxser_stop(struct tty_struct *tty)
1749{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001750 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001751 unsigned long flags;
1752
1753 spin_lock_irqsave(&info->slock, flags);
1754 if (info->IER & UART_IER_THRI) {
1755 info->IER &= ~UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001756 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001757 }
1758 spin_unlock_irqrestore(&info->slock, flags);
1759}
1760
1761static void mxser_start(struct tty_struct *tty)
1762{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001763 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001764 unsigned long flags;
1765
1766 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001767 if (info->xmit_cnt && info->xmit_buf
1768 /* && !(info->IER & UART_IER_THRI) */) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08001769 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001770 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001771 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001772 }
1773 spin_unlock_irqrestore(&info->slock, flags);
1774}
1775
1776/*
1777 * mxser_wait_until_sent() --- wait until the transmitter is empty
1778 */
1779static void mxser_wait_until_sent(struct tty_struct *tty, int timeout)
1780{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001781 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001782 unsigned long orig_jiffies, char_time;
1783 int lsr;
1784
1785 if (info->type == PORT_UNKNOWN)
1786 return;
1787
1788 if (info->xmit_fifo_size == 0)
1789 return; /* Just in case.... */
1790
1791 orig_jiffies = jiffies;
1792 /*
1793 * Set the check interval to be 1/5 of the estimated time to
1794 * send a single character, and make it at least 1. The check
1795 * interval should also be less than the timeout.
1796 *
1797 * Note: we have to use pretty tight timings here to satisfy
1798 * the NIST-PCTS.
1799 */
1800 char_time = (info->timeout - HZ / 50) / info->xmit_fifo_size;
1801 char_time = char_time / 5;
1802 if (char_time == 0)
1803 char_time = 1;
1804 if (timeout && timeout < char_time)
1805 char_time = timeout;
1806 /*
1807 * If the transmitter hasn't cleared in twice the approximate
1808 * amount of time to send the entire FIFO, it probably won't
1809 * ever clear. This assumes the UART isn't doing flow
1810 * control, which is currently the case. Hence, if it ever
1811 * takes longer than info->timeout, this is probably due to a
1812 * UART bug of some kind. So, we clamp the timeout parameter at
1813 * 2*info->timeout.
1814 */
1815 if (!timeout || timeout > 2 * info->timeout)
1816 timeout = 2 * info->timeout;
1817#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1818 printk(KERN_DEBUG "In rs_wait_until_sent(%d) check=%lu...",
1819 timeout, char_time);
1820 printk("jiff=%lu...", jiffies);
1821#endif
Jiri Slaby55b307d2006-12-08 02:38:14 -08001822 while (!((lsr = inb(info->ioaddr + UART_LSR)) & UART_LSR_TEMT)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001823#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1824 printk("lsr = %d (jiff=%lu)...", lsr, jiffies);
1825#endif
1826 schedule_timeout_interruptible(char_time);
1827 if (signal_pending(current))
1828 break;
1829 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1830 break;
1831 }
1832 set_current_state(TASK_RUNNING);
1833
1834#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1835 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
1836#endif
1837}
1838
1839
1840/*
1841 * This routine is called by tty_hangup() when a hangup is signaled.
1842 */
1843void mxser_hangup(struct tty_struct *tty)
1844{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001845 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001846
1847 mxser_flush_buffer(tty);
1848 mxser_shutdown(info);
1849 info->event = 0;
1850 info->count = 0;
1851 info->flags &= ~ASYNC_NORMAL_ACTIVE;
1852 info->tty = NULL;
1853 wake_up_interruptible(&info->open_wait);
1854}
1855
1856
1857/* added by James 03-12-2004. */
1858/*
1859 * mxser_rs_break() --- routine which turns the break handling on or off
1860 */
1861static void mxser_rs_break(struct tty_struct *tty, int break_state)
1862{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001863 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001864 unsigned long flags;
1865
1866 spin_lock_irqsave(&info->slock, flags);
1867 if (break_state == -1)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001868 outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
1869 info->ioaddr + UART_LCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08001870 else
Jiri Slaby55b307d2006-12-08 02:38:14 -08001871 outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
1872 info->ioaddr + UART_LCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08001873 spin_unlock_irqrestore(&info->slock, flags);
1874}
1875
1876/* (above) added by James. */
1877
1878
1879/*
1880 * This is the serial driver's generic interrupt routine
1881 */
1882static irqreturn_t mxser_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1883{
1884 int status, iir, i;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001885 struct mxser_board *brd = NULL;
1886 struct mxser_port *port;
Jiri Slaby037ad482006-12-08 02:38:11 -08001887 int max, irqbits, bits, msr;
1888 int pass_counter = 0;
Jiri Slaby3306ce32006-12-08 02:38:13 -08001889 unsigned int int_cnt;
Jiri Slaby037ad482006-12-08 02:38:11 -08001890 int handled = IRQ_NONE;
1891
Jiri Slaby037ad482006-12-08 02:38:11 -08001892 /* spin_lock(&gm_lock); */
1893
Jiri Slaby55b307d2006-12-08 02:38:14 -08001894 for (i = 0; i < MXSER_BOARDS; i++)
1895 if (dev_id == &mxser_boards[i]) {
1896 brd = dev_id;
Jiri Slaby037ad482006-12-08 02:38:11 -08001897 break;
1898 }
Jiri Slaby037ad482006-12-08 02:38:11 -08001899
1900 if (i == MXSER_BOARDS)
1901 goto irq_stop;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001902 if (brd == NULL)
Jiri Slaby037ad482006-12-08 02:38:11 -08001903 goto irq_stop;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001904 max = mxser_numports[brd->board_type - 1];
Jiri Slaby037ad482006-12-08 02:38:11 -08001905 while (1) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08001906 irqbits = inb(brd->vector) & brd->vector_mask;
1907 if (irqbits == brd->vector_mask)
Jiri Slaby037ad482006-12-08 02:38:11 -08001908 break;
1909
1910 handled = IRQ_HANDLED;
1911 for (i = 0, bits = 1; i < max; i++, irqbits |= bits, bits <<= 1) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08001912 if (irqbits == brd->vector_mask)
Jiri Slaby037ad482006-12-08 02:38:11 -08001913 break;
1914 if (bits & irqbits)
1915 continue;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001916 port = &brd->ports[i];
Jiri Slaby037ad482006-12-08 02:38:11 -08001917
Jiri Slaby3306ce32006-12-08 02:38:13 -08001918 int_cnt = 0;
1919 do {
1920 /* following add by Victor Yu. 09-13-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08001921 iir = inb(port->ioaddr + UART_IIR);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001922 if (iir & UART_IIR_NO_INT)
1923 break;
1924 iir &= MOXA_MUST_IIR_MASK;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001925 if (!port->tty) {
1926 status = inb(port->ioaddr + UART_LSR);
1927 outb(0x27, port->ioaddr + UART_FCR);
1928 inb(port->ioaddr + UART_MSR);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001929 break;
Jiri Slaby037ad482006-12-08 02:38:11 -08001930 }
Jiri Slaby037ad482006-12-08 02:38:11 -08001931 /* above add by Victor Yu. 09-13-2002 */
1932
Jiri Slaby55b307d2006-12-08 02:38:14 -08001933 spin_lock(&port->slock);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001934 /* following add by Victor Yu. 09-02-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08001935 status = inb(port->ioaddr + UART_LSR);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001936
1937 if (status & UART_LSR_PE)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001938 port->err_shadow |= NPPI_NOTIFY_PARITY;
Jiri Slaby3306ce32006-12-08 02:38:13 -08001939 if (status & UART_LSR_FE)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001940 port->err_shadow |= NPPI_NOTIFY_FRAMING;
Jiri Slaby3306ce32006-12-08 02:38:13 -08001941 if (status & UART_LSR_OE)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001942 port->err_shadow |=
Jiri Slaby3306ce32006-12-08 02:38:13 -08001943 NPPI_NOTIFY_HW_OVERRUN;
1944 if (status & UART_LSR_BI)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001945 port->err_shadow |= NPPI_NOTIFY_BREAK;
Jiri Slaby3306ce32006-12-08 02:38:13 -08001946
Jiri Slaby55b307d2006-12-08 02:38:14 -08001947 if (port->board->chip_flag) {
Jiri Slaby3306ce32006-12-08 02:38:13 -08001948 /*
1949 if ( (status & 0x02) && !(status & 0x01) ) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08001950 outb(port->ioaddr+UART_FCR, 0x23);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001951 continue;
1952 }
1953 */
1954 if (iir == MOXA_MUST_IIR_GDA ||
1955 iir == MOXA_MUST_IIR_RDA ||
1956 iir == MOXA_MUST_IIR_RTO ||
1957 iir == MOXA_MUST_IIR_LSR)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001958 mxser_receive_chars(port,
Jiri Slaby3306ce32006-12-08 02:38:13 -08001959 &status);
1960
1961 } else {
1962 /* above add by Victor Yu. 09-02-2002 */
1963
Jiri Slaby55b307d2006-12-08 02:38:14 -08001964 status &= port->read_status_mask;
Jiri Slaby3306ce32006-12-08 02:38:13 -08001965 if (status & UART_LSR_DR)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001966 mxser_receive_chars(port,
Jiri Slaby3306ce32006-12-08 02:38:13 -08001967 &status);
Jiri Slaby037ad482006-12-08 02:38:11 -08001968 }
Jiri Slaby55b307d2006-12-08 02:38:14 -08001969 msr = inb(port->ioaddr + UART_MSR);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001970 if (msr & UART_MSR_ANY_DELTA)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001971 mxser_check_modem_status(port, msr);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001972
1973 /* following add by Victor Yu. 09-13-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08001974 if (port->board->chip_flag) {
Jiri Slaby3306ce32006-12-08 02:38:13 -08001975 if (iir == 0x02 && (status &
1976 UART_LSR_THRE))
Jiri Slaby55b307d2006-12-08 02:38:14 -08001977 mxser_transmit_chars(port);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001978 } else {
1979 /* above add by Victor Yu. 09-13-2002 */
1980
1981 if (status & UART_LSR_THRE)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001982 mxser_transmit_chars(port);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001983 }
Jiri Slaby55b307d2006-12-08 02:38:14 -08001984 spin_unlock(&port->slock);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001985 } while (int_cnt++ < MXSER_ISR_PASS_LIMIT);
Jiri Slaby037ad482006-12-08 02:38:11 -08001986 }
Jiri Slaby3306ce32006-12-08 02:38:13 -08001987 if (pass_counter++ > MXSER_ISR_PASS_LIMIT)
Jiri Slaby037ad482006-12-08 02:38:11 -08001988 break; /* Prevent infinite loops */
Jiri Slaby037ad482006-12-08 02:38:11 -08001989 }
1990
1991 irq_stop:
1992 /* spin_unlock(&gm_lock); */
1993 return handled;
1994}
1995
Jiri Slaby55b307d2006-12-08 02:38:14 -08001996static void mxser_receive_chars(struct mxser_port *port, int *status)
Jiri Slaby037ad482006-12-08 02:38:11 -08001997{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001998 struct tty_struct *tty = port->tty;
Jiri Slaby037ad482006-12-08 02:38:11 -08001999 unsigned char ch, gdl;
2000 int ignored = 0;
2001 int cnt = 0;
2002 int recv_room;
2003 int max = 256;
2004 unsigned long flags;
2005
Jiri Slaby55b307d2006-12-08 02:38:14 -08002006 spin_lock_irqsave(&port->slock, flags);
Jiri Slaby037ad482006-12-08 02:38:11 -08002007
2008 recv_room = tty->receive_room;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002009 if ((recv_room == 0) && (!port->ldisc_stop_rx)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002010 /* mxser_throttle(tty); */
2011 mxser_stoprx(tty);
2012 /* return; */
2013 }
2014
2015 /* following add by Victor Yu. 09-02-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002016 if (port->board->chip_flag != MOXA_OTHER_UART) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002017
Jiri Slaby3306ce32006-12-08 02:38:13 -08002018 if (*status & UART_LSR_SPECIAL)
Jiri Slaby037ad482006-12-08 02:38:11 -08002019 goto intr_old;
Jiri Slaby037ad482006-12-08 02:38:11 -08002020 /* following add by Victor Yu. 02-11-2004 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002021 if (port->board->chip_flag == MOXA_MUST_MU860_HWID &&
Jiri Slaby037ad482006-12-08 02:38:11 -08002022 (*status & MOXA_MUST_LSR_RERR))
2023 goto intr_old;
2024 /* above add by Victor Yu. 02-14-2004 */
2025 if (*status & MOXA_MUST_LSR_RERR)
2026 goto intr_old;
2027
Jiri Slaby55b307d2006-12-08 02:38:14 -08002028 gdl = inb(port->ioaddr + MOXA_MUST_GDL_REGISTER);
Jiri Slaby037ad482006-12-08 02:38:11 -08002029
2030 /* add by Victor Yu. 02-11-2004 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002031 if (port->board->chip_flag == MOXA_MUST_MU150_HWID)
Jiri Slaby037ad482006-12-08 02:38:11 -08002032 gdl &= MOXA_MUST_GDL_MASK;
2033 if (gdl >= recv_room) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002034 if (!port->ldisc_stop_rx) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002035 /* mxser_throttle(tty); */
2036 mxser_stoprx(tty);
2037 }
2038 /* return; */
2039 }
2040 while (gdl--) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002041 ch = inb(port->ioaddr + UART_RX);
Jiri Slaby037ad482006-12-08 02:38:11 -08002042 tty_insert_flip_char(tty, ch, 0);
2043 cnt++;
Jiri Slaby037ad482006-12-08 02:38:11 -08002044 }
2045 goto end_intr;
2046 }
2047 intr_old:
2048 /* above add by Victor Yu. 09-02-2002 */
2049
2050 do {
2051 if (max-- < 0)
2052 break;
Jiri Slaby037ad482006-12-08 02:38:11 -08002053
Jiri Slaby55b307d2006-12-08 02:38:14 -08002054 ch = inb(port->ioaddr + UART_RX);
Jiri Slaby037ad482006-12-08 02:38:11 -08002055 /* following add by Victor Yu. 09-02-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002056 if (port->board->chip_flag && (*status & UART_LSR_OE)
Jiri Slaby3306ce32006-12-08 02:38:13 -08002057 /*&& !(*status&UART_LSR_DR) */)
Jiri Slaby55b307d2006-12-08 02:38:14 -08002058 outb(0x23, port->ioaddr + UART_FCR);
2059 *status &= port->read_status_mask;
Jiri Slaby037ad482006-12-08 02:38:11 -08002060 /* above add by Victor Yu. 09-02-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002061 if (*status & port->ignore_status_mask) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002062 if (++ignored > 100)
2063 break;
2064 } else {
2065 char flag = 0;
2066 if (*status & UART_LSR_SPECIAL) {
2067 if (*status & UART_LSR_BI) {
2068 flag = TTY_BREAK;
2069/* added by casper 1/11/2000 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002070 port->icount.brk++;
Jiri Slaby3306ce32006-12-08 02:38:13 -08002071
Jiri Slaby55b307d2006-12-08 02:38:14 -08002072 if (port->flags & ASYNC_SAK)
Jiri Slaby037ad482006-12-08 02:38:11 -08002073 do_SAK(tty);
2074 } else if (*status & UART_LSR_PE) {
2075 flag = TTY_PARITY;
2076/* added by casper 1/11/2000 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002077 port->icount.parity++;
Jiri Slaby037ad482006-12-08 02:38:11 -08002078 } else if (*status & UART_LSR_FE) {
2079 flag = TTY_FRAME;
2080/* added by casper 1/11/2000 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002081 port->icount.frame++;
Jiri Slaby037ad482006-12-08 02:38:11 -08002082 } else if (*status & UART_LSR_OE) {
2083 flag = TTY_OVERRUN;
2084/* added by casper 1/11/2000 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002085 port->icount.overrun++;
Jiri Slaby3306ce32006-12-08 02:38:13 -08002086 } else
2087 flags = TTY_BREAK;
2088 } else
2089 flags = 0;
Jiri Slaby037ad482006-12-08 02:38:11 -08002090 tty_insert_flip_char(tty, ch, flag);
2091 cnt++;
2092 if (cnt >= recv_room) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002093 if (!port->ldisc_stop_rx) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002094 /* mxser_throttle(tty); */
2095 mxser_stoprx(tty);
2096 }
2097 break;
2098 }
2099
2100 }
2101
2102 /* following add by Victor Yu. 09-02-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002103 if (port->board->chip_flag)
Jiri Slaby037ad482006-12-08 02:38:11 -08002104 break;
Jiri Slaby037ad482006-12-08 02:38:11 -08002105
2106 /* mask by Victor Yu. 09-02-2002
Jiri Slaby55b307d2006-12-08 02:38:14 -08002107 *status = inb(port->ioaddr + UART_LSR) & port->read_status_mask;
Jiri Slaby037ad482006-12-08 02:38:11 -08002108 */
2109 /* following add by Victor Yu. 09-02-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002110 *status = inb(port->ioaddr + UART_LSR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002111 /* above add by Victor Yu. 09-02-2002 */
2112 } while (*status & UART_LSR_DR);
2113
2114end_intr: /* add by Victor Yu. 09-02-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002115 mxvar_log.rxcnt[port->tty->index] += cnt;
2116 port->mon_data.rxcnt += cnt;
2117 port->mon_data.up_rxcnt += cnt;
2118 spin_unlock_irqrestore(&port->slock, flags);
Jiri Slaby037ad482006-12-08 02:38:11 -08002119
2120 tty_flip_buffer_push(tty);
2121}
2122
Jiri Slaby55b307d2006-12-08 02:38:14 -08002123static void mxser_transmit_chars(struct mxser_port *port)
Jiri Slaby037ad482006-12-08 02:38:11 -08002124{
2125 int count, cnt;
2126 unsigned long flags;
2127
Jiri Slaby55b307d2006-12-08 02:38:14 -08002128 spin_lock_irqsave(&port->slock, flags);
Jiri Slaby037ad482006-12-08 02:38:11 -08002129
Jiri Slaby55b307d2006-12-08 02:38:14 -08002130 if (port->x_char) {
2131 outb(port->x_char, port->ioaddr + UART_TX);
2132 port->x_char = 0;
2133 mxvar_log.txcnt[port->tty->index]++;
2134 port->mon_data.txcnt++;
2135 port->mon_data.up_txcnt++;
Jiri Slaby037ad482006-12-08 02:38:11 -08002136
2137/* added by casper 1/11/2000 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002138 port->icount.tx++;
Jiri Slaby3306ce32006-12-08 02:38:13 -08002139 goto unlock;
Jiri Slaby037ad482006-12-08 02:38:11 -08002140 }
2141
Jiri Slaby55b307d2006-12-08 02:38:14 -08002142 if (port->xmit_buf == 0)
Jiri Slaby3306ce32006-12-08 02:38:13 -08002143 goto unlock;
Jiri Slaby037ad482006-12-08 02:38:11 -08002144
Jiri Slaby55b307d2006-12-08 02:38:14 -08002145 if (port->xmit_cnt == 0) {
2146 if (port->xmit_cnt < WAKEUP_CHARS) { /* XXX what's this for?? */
2147 set_bit(MXSER_EVENT_TXLOW, &port->event);
2148 schedule_work(&port->tqueue);
Jiri Slaby3306ce32006-12-08 02:38:13 -08002149 }
2150 goto unlock;
2151 }
Jiri Slaby55b307d2006-12-08 02:38:14 -08002152 if (port->tty->stopped || (port->tty->hw_stopped &&
2153 (port->type != PORT_16550A) &&
2154 (!port->board->chip_flag))) {
2155 port->IER &= ~UART_IER_THRI;
2156 outb(port->IER, port->ioaddr + UART_IER);
Jiri Slaby3306ce32006-12-08 02:38:13 -08002157 goto unlock;
Jiri Slaby037ad482006-12-08 02:38:11 -08002158 }
2159
Jiri Slaby55b307d2006-12-08 02:38:14 -08002160 cnt = port->xmit_cnt;
2161 count = port->xmit_fifo_size;
Jiri Slaby037ad482006-12-08 02:38:11 -08002162 do {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002163 outb(port->xmit_buf[port->xmit_tail++],
2164 port->ioaddr + UART_TX);
2165 port->xmit_tail = port->xmit_tail & (SERIAL_XMIT_SIZE - 1);
2166 if (--port->xmit_cnt <= 0)
Jiri Slaby037ad482006-12-08 02:38:11 -08002167 break;
2168 } while (--count > 0);
Jiri Slaby55b307d2006-12-08 02:38:14 -08002169 mxvar_log.txcnt[port->tty->index] += (cnt - port->xmit_cnt);
Jiri Slaby037ad482006-12-08 02:38:11 -08002170
2171/* added by James 03-12-2004. */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002172 port->mon_data.txcnt += (cnt - port->xmit_cnt);
2173 port->mon_data.up_txcnt += (cnt - port->xmit_cnt);
Jiri Slaby037ad482006-12-08 02:38:11 -08002174
2175/* added by casper 1/11/2000 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002176 port->icount.tx += (cnt - port->xmit_cnt);
Jiri Slaby037ad482006-12-08 02:38:11 -08002177
Jiri Slaby55b307d2006-12-08 02:38:14 -08002178 if (port->xmit_cnt < WAKEUP_CHARS) {
2179 set_bit(MXSER_EVENT_TXLOW, &port->event);
2180 schedule_work(&port->tqueue);
Jiri Slaby037ad482006-12-08 02:38:11 -08002181 }
Jiri Slaby55b307d2006-12-08 02:38:14 -08002182 if (port->xmit_cnt <= 0) {
2183 port->IER &= ~UART_IER_THRI;
2184 outb(port->IER, port->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08002185 }
Jiri Slaby3306ce32006-12-08 02:38:13 -08002186unlock:
Jiri Slaby55b307d2006-12-08 02:38:14 -08002187 spin_unlock_irqrestore(&port->slock, flags);
Jiri Slaby037ad482006-12-08 02:38:11 -08002188}
2189
Jiri Slaby55b307d2006-12-08 02:38:14 -08002190static void mxser_check_modem_status(struct mxser_port *port, int status)
Jiri Slaby037ad482006-12-08 02:38:11 -08002191{
2192 /* update input line counters */
2193 if (status & UART_MSR_TERI)
Jiri Slaby55b307d2006-12-08 02:38:14 -08002194 port->icount.rng++;
Jiri Slaby037ad482006-12-08 02:38:11 -08002195 if (status & UART_MSR_DDSR)
Jiri Slaby55b307d2006-12-08 02:38:14 -08002196 port->icount.dsr++;
Jiri Slaby037ad482006-12-08 02:38:11 -08002197 if (status & UART_MSR_DDCD)
Jiri Slaby55b307d2006-12-08 02:38:14 -08002198 port->icount.dcd++;
Jiri Slaby037ad482006-12-08 02:38:11 -08002199 if (status & UART_MSR_DCTS)
Jiri Slaby55b307d2006-12-08 02:38:14 -08002200 port->icount.cts++;
2201 port->mon_data.modem_status = status;
2202 wake_up_interruptible(&port->delta_msr_wait);
Jiri Slaby037ad482006-12-08 02:38:11 -08002203
Jiri Slaby55b307d2006-12-08 02:38:14 -08002204 if ((port->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002205 if (status & UART_MSR_DCD)
Jiri Slaby55b307d2006-12-08 02:38:14 -08002206 wake_up_interruptible(&port->open_wait);
2207 schedule_work(&port->tqueue);
Jiri Slaby037ad482006-12-08 02:38:11 -08002208 }
2209
Jiri Slaby55b307d2006-12-08 02:38:14 -08002210 if (port->flags & ASYNC_CTS_FLOW) {
2211 if (port->tty->hw_stopped) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002212 if (status & UART_MSR_CTS) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002213 port->tty->hw_stopped = 0;
Jiri Slaby037ad482006-12-08 02:38:11 -08002214
Jiri Slaby55b307d2006-12-08 02:38:14 -08002215 if ((port->type != PORT_16550A) &&
2216 (!port->board->chip_flag)) {
2217 outb(port->IER & ~UART_IER_THRI,
2218 port->ioaddr + UART_IER);
2219 port->IER |= UART_IER_THRI;
2220 outb(port->IER, port->ioaddr +
2221 UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08002222 }
Jiri Slaby55b307d2006-12-08 02:38:14 -08002223 set_bit(MXSER_EVENT_TXLOW, &port->event);
2224 schedule_work(&port->tqueue);
Jiri Slaby3306ce32006-12-08 02:38:13 -08002225 }
Jiri Slaby037ad482006-12-08 02:38:11 -08002226 } else {
2227 if (!(status & UART_MSR_CTS)) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002228 port->tty->hw_stopped = 1;
2229 if (port->type != PORT_16550A &&
2230 !port->board->chip_flag) {
2231 port->IER &= ~UART_IER_THRI;
2232 outb(port->IER, port->ioaddr +
2233 UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08002234 }
2235 }
2236 }
2237 }
2238}
2239
Jiri Slaby55b307d2006-12-08 02:38:14 -08002240static int mxser_block_til_ready(struct tty_struct *tty, struct file *filp, struct mxser_port *port)
Jiri Slaby037ad482006-12-08 02:38:11 -08002241{
2242 DECLARE_WAITQUEUE(wait, current);
2243 int retval;
2244 int do_clocal = 0;
2245 unsigned long flags;
2246
2247 /*
2248 * If non-blocking mode is set, or the port is not enabled,
2249 * then make the check up front and then exit.
2250 */
2251 if ((filp->f_flags & O_NONBLOCK) || (tty->flags & (1 << TTY_IO_ERROR))) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002252 port->flags |= ASYNC_NORMAL_ACTIVE;
Jiri Slaby037ad482006-12-08 02:38:11 -08002253 return 0;
2254 }
2255
2256 if (tty->termios->c_cflag & CLOCAL)
2257 do_clocal = 1;
2258
2259 /*
2260 * Block waiting for the carrier detect and the line to become
2261 * free (i.e., not in use by the callout). While we are in
Jiri Slaby55b307d2006-12-08 02:38:14 -08002262 * this loop, port->count is dropped by one, so that
Jiri Slaby037ad482006-12-08 02:38:11 -08002263 * mxser_close() knows when to free things. We restore it upon
2264 * exit, either normal or abnormal.
2265 */
2266 retval = 0;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002267 add_wait_queue(&port->open_wait, &wait);
Jiri Slaby037ad482006-12-08 02:38:11 -08002268
Jiri Slaby55b307d2006-12-08 02:38:14 -08002269 spin_lock_irqsave(&port->slock, flags);
Jiri Slaby037ad482006-12-08 02:38:11 -08002270 if (!tty_hung_up_p(filp))
Jiri Slaby55b307d2006-12-08 02:38:14 -08002271 port->count--;
2272 spin_unlock_irqrestore(&port->slock, flags);
2273 port->blocked_open++;
Jiri Slaby037ad482006-12-08 02:38:11 -08002274 while (1) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002275 spin_lock_irqsave(&port->slock, flags);
2276 outb(inb(port->ioaddr + UART_MCR) |
2277 UART_MCR_DTR | UART_MCR_RTS, port->ioaddr + UART_MCR);
2278 spin_unlock_irqrestore(&port->slock, flags);
Jiri Slaby037ad482006-12-08 02:38:11 -08002279 set_current_state(TASK_INTERRUPTIBLE);
Jiri Slaby55b307d2006-12-08 02:38:14 -08002280 if (tty_hung_up_p(filp) || !(port->flags & ASYNC_INITIALIZED)) {
2281 if (port->flags & ASYNC_HUP_NOTIFY)
Jiri Slaby037ad482006-12-08 02:38:11 -08002282 retval = -EAGAIN;
2283 else
2284 retval = -ERESTARTSYS;
2285 break;
2286 }
Jiri Slaby55b307d2006-12-08 02:38:14 -08002287 if (!(port->flags & ASYNC_CLOSING) &&
Jiri Slaby037ad482006-12-08 02:38:11 -08002288 (do_clocal ||
Jiri Slaby55b307d2006-12-08 02:38:14 -08002289 (inb(port->ioaddr + UART_MSR) & UART_MSR_DCD)))
Jiri Slaby037ad482006-12-08 02:38:11 -08002290 break;
2291 if (signal_pending(current)) {
2292 retval = -ERESTARTSYS;
2293 break;
2294 }
2295 schedule();
2296 }
2297 set_current_state(TASK_RUNNING);
Jiri Slaby55b307d2006-12-08 02:38:14 -08002298 remove_wait_queue(&port->open_wait, &wait);
Jiri Slaby037ad482006-12-08 02:38:11 -08002299 if (!tty_hung_up_p(filp))
Jiri Slaby55b307d2006-12-08 02:38:14 -08002300 port->count++;
2301 port->blocked_open--;
Jiri Slaby037ad482006-12-08 02:38:11 -08002302 if (retval)
2303 return retval;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002304 port->flags |= ASYNC_NORMAL_ACTIVE;
Jiri Slaby037ad482006-12-08 02:38:11 -08002305 return 0;
2306}
2307
Jiri Slaby55b307d2006-12-08 02:38:14 -08002308static int mxser_startup(struct mxser_port *info)
Jiri Slaby037ad482006-12-08 02:38:11 -08002309{
2310 unsigned long page;
2311 unsigned long flags;
2312
2313 page = __get_free_page(GFP_KERNEL);
2314 if (!page)
2315 return -ENOMEM;
2316
2317 spin_lock_irqsave(&info->slock, flags);
2318
2319 if (info->flags & ASYNC_INITIALIZED) {
2320 free_page(page);
2321 spin_unlock_irqrestore(&info->slock, flags);
2322 return 0;
2323 }
2324
Jiri Slaby55b307d2006-12-08 02:38:14 -08002325 if (!info->ioaddr || !info->type) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002326 if (info->tty)
2327 set_bit(TTY_IO_ERROR, &info->tty->flags);
2328 free_page(page);
2329 spin_unlock_irqrestore(&info->slock, flags);
2330 return 0;
2331 }
2332 if (info->xmit_buf)
2333 free_page(page);
2334 else
2335 info->xmit_buf = (unsigned char *) page;
2336
2337 /*
2338 * Clear the FIFO buffers and disable them
2339 * (they will be reenabled in mxser_change_speed())
2340 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002341 if (info->board->chip_flag)
Jiri Slaby037ad482006-12-08 02:38:11 -08002342 outb((UART_FCR_CLEAR_RCVR |
2343 UART_FCR_CLEAR_XMIT |
Jiri Slaby55b307d2006-12-08 02:38:14 -08002344 MOXA_MUST_FCR_GDA_MODE_ENABLE), info->ioaddr + UART_FCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002345 else
2346 outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
Jiri Slaby55b307d2006-12-08 02:38:14 -08002347 info->ioaddr + UART_FCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002348
2349 /*
2350 * At this point there's no way the LSR could still be 0xFF;
2351 * if it is, then bail out, because there's likely no UART
2352 * here.
2353 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002354 if (inb(info->ioaddr + UART_LSR) == 0xff) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002355 spin_unlock_irqrestore(&info->slock, flags);
2356 if (capable(CAP_SYS_ADMIN)) {
2357 if (info->tty)
2358 set_bit(TTY_IO_ERROR, &info->tty->flags);
2359 return 0;
2360 } else
2361 return -ENODEV;
2362 }
2363
2364 /*
2365 * Clear the interrupt registers.
2366 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002367 (void) inb(info->ioaddr + UART_LSR);
2368 (void) inb(info->ioaddr + UART_RX);
2369 (void) inb(info->ioaddr + UART_IIR);
2370 (void) inb(info->ioaddr + UART_MSR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002371
2372 /*
2373 * Now, initialize the UART
2374 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002375 outb(UART_LCR_WLEN8, info->ioaddr + UART_LCR); /* reset DLAB */
Jiri Slaby037ad482006-12-08 02:38:11 -08002376 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002377 outb(info->MCR, info->ioaddr + UART_MCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002378
2379 /*
2380 * Finally, enable interrupts
2381 */
2382 info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
2383 /* info->IER = UART_IER_RLSI | UART_IER_RDI; */
2384
2385 /* following add by Victor Yu. 08-30-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002386 if (info->board->chip_flag)
Jiri Slaby037ad482006-12-08 02:38:11 -08002387 info->IER |= MOXA_MUST_IER_EGDAI;
2388 /* above add by Victor Yu. 08-30-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002389 outb(info->IER, info->ioaddr + UART_IER); /* enable interrupts */
Jiri Slaby037ad482006-12-08 02:38:11 -08002390
2391 /*
2392 * And clear the interrupt registers again for luck.
2393 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002394 (void) inb(info->ioaddr + UART_LSR);
2395 (void) inb(info->ioaddr + UART_RX);
2396 (void) inb(info->ioaddr + UART_IIR);
2397 (void) inb(info->ioaddr + UART_MSR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002398
2399 if (info->tty)
2400 clear_bit(TTY_IO_ERROR, &info->tty->flags);
2401 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
2402
2403 /*
2404 * and set the speed of the serial port
2405 */
2406 spin_unlock_irqrestore(&info->slock, flags);
2407 mxser_change_speed(info, NULL);
2408
2409 info->flags |= ASYNC_INITIALIZED;
2410 return 0;
2411}
2412
2413/*
2414 * This routine will shutdown a serial port; interrupts maybe disabled, and
2415 * DTR is dropped if the hangup on close termio flag is on.
2416 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002417static void mxser_shutdown(struct mxser_port *info)
Jiri Slaby037ad482006-12-08 02:38:11 -08002418{
2419 unsigned long flags;
2420
2421 if (!(info->flags & ASYNC_INITIALIZED))
2422 return;
2423
2424 spin_lock_irqsave(&info->slock, flags);
2425
2426 /*
2427 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
2428 * here so the queue might never be waken up
2429 */
2430 wake_up_interruptible(&info->delta_msr_wait);
2431
2432 /*
2433 * Free the IRQ, if necessary
2434 */
2435 if (info->xmit_buf) {
2436 free_page((unsigned long) info->xmit_buf);
2437 info->xmit_buf = NULL;
2438 }
2439
2440 info->IER = 0;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002441 outb(0x00, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08002442
2443 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
2444 info->MCR &= ~(UART_MCR_DTR | UART_MCR_RTS);
Jiri Slaby55b307d2006-12-08 02:38:14 -08002445 outb(info->MCR, info->ioaddr + UART_MCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002446
2447 /* clear Rx/Tx FIFO's */
2448 /* following add by Victor Yu. 08-30-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002449 if (info->board->chip_flag)
2450 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT |
2451 MOXA_MUST_FCR_GDA_MODE_ENABLE,
2452 info->ioaddr + UART_FCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002453 else
2454 /* above add by Victor Yu. 08-30-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002455 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
2456 info->ioaddr + UART_FCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002457
2458 /* read data port to reset things */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002459 (void) inb(info->ioaddr + UART_RX);
Jiri Slaby037ad482006-12-08 02:38:11 -08002460
2461 if (info->tty)
2462 set_bit(TTY_IO_ERROR, &info->tty->flags);
2463
2464 info->flags &= ~ASYNC_INITIALIZED;
2465
2466 /* following add by Victor Yu. 09-23-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002467 if (info->board->chip_flag)
2468 SET_MOXA_MUST_NO_SOFTWARE_FLOW_CONTROL(info->ioaddr);
Jiri Slaby037ad482006-12-08 02:38:11 -08002469 /* above add by Victor Yu. 09-23-2002 */
2470
2471 spin_unlock_irqrestore(&info->slock, flags);
2472}
2473
2474/*
2475 * This routine is called to set the UART divisor registers to match
2476 * the specified baud rate for a serial port.
2477 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002478static int mxser_change_speed(struct mxser_port *info,
2479 struct termios *old_termios)
Jiri Slaby037ad482006-12-08 02:38:11 -08002480{
2481 unsigned cflag, cval, fcr;
2482 int ret = 0;
2483 unsigned char status;
2484 long baud;
2485 unsigned long flags;
2486
2487 if (!info->tty || !info->tty->termios)
2488 return ret;
2489 cflag = info->tty->termios->c_cflag;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002490 if (!(info->ioaddr))
Jiri Slaby037ad482006-12-08 02:38:11 -08002491 return ret;
2492
2493#ifndef B921600
2494#define B921600 (B460800 +1)
2495#endif
Jiri Slaby55b307d2006-12-08 02:38:14 -08002496 if (mxser_set_baud_method[info->tty->index] == 0) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002497 baud = tty_get_baud_rate(info->tty);
2498 mxser_set_baud(info, baud);
2499 }
2500
2501 /* byte size and parity */
2502 switch (cflag & CSIZE) {
2503 case CS5:
2504 cval = 0x00;
2505 break;
2506 case CS6:
2507 cval = 0x01;
2508 break;
2509 case CS7:
2510 cval = 0x02;
2511 break;
2512 case CS8:
2513 cval = 0x03;
2514 break;
2515 default:
2516 cval = 0x00;
2517 break; /* too keep GCC shut... */
2518 }
2519 if (cflag & CSTOPB)
2520 cval |= 0x04;
2521 if (cflag & PARENB)
2522 cval |= UART_LCR_PARITY;
2523 if (!(cflag & PARODD))
2524 cval |= UART_LCR_EPAR;
2525 if (cflag & CMSPAR)
2526 cval |= UART_LCR_SPAR;
2527
2528 if ((info->type == PORT_8250) || (info->type == PORT_16450)) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002529 if (info->board->chip_flag) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002530 fcr = UART_FCR_ENABLE_FIFO;
2531 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
2532 SET_MOXA_MUST_FIFO_VALUE(info);
2533 } else
2534 fcr = 0;
2535 } else {
2536 fcr = UART_FCR_ENABLE_FIFO;
2537 /* following add by Victor Yu. 08-30-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002538 if (info->board->chip_flag) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002539 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
2540 SET_MOXA_MUST_FIFO_VALUE(info);
2541 } else {
2542 /* above add by Victor Yu. 08-30-2002 */
2543 switch (info->rx_trigger) {
2544 case 1:
2545 fcr |= UART_FCR_TRIGGER_1;
2546 break;
2547 case 4:
2548 fcr |= UART_FCR_TRIGGER_4;
2549 break;
2550 case 8:
2551 fcr |= UART_FCR_TRIGGER_8;
2552 break;
2553 default:
2554 fcr |= UART_FCR_TRIGGER_14;
2555 break;
2556 }
2557 }
2558 }
2559
2560 /* CTS flow control flag and modem status interrupts */
2561 info->IER &= ~UART_IER_MSI;
2562 info->MCR &= ~UART_MCR_AFE;
2563 if (cflag & CRTSCTS) {
2564 info->flags |= ASYNC_CTS_FLOW;
2565 info->IER |= UART_IER_MSI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002566 if ((info->type == PORT_16550A) || (info->board->chip_flag)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002567 info->MCR |= UART_MCR_AFE;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002568/* status = mxser_get_msr(info->ioaddr, 0, info->port); */
Jiri Slaby037ad482006-12-08 02:38:11 -08002569/*
2570 save_flags(flags);
2571 cli();
2572 status = inb(baseaddr + UART_MSR);
2573 restore_flags(flags);
2574*/
2575 /* mxser_check_modem_status(info, status); */
2576 } else {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002577/* status = mxser_get_msr(info->ioaddr, 0, info->port); */
Jiri Slaby037ad482006-12-08 02:38:11 -08002578 /* MX_LOCK(&info->slock); */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002579 status = inb(info->ioaddr + UART_MSR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002580 /* MX_UNLOCK(&info->slock); */
2581 if (info->tty->hw_stopped) {
2582 if (status & UART_MSR_CTS) {
2583 info->tty->hw_stopped = 0;
Jiri Slaby3306ce32006-12-08 02:38:13 -08002584 if (info->type != PORT_16550A &&
Jiri Slaby55b307d2006-12-08 02:38:14 -08002585 !info->board->chip_flag) {
Jiri Slaby3306ce32006-12-08 02:38:13 -08002586 outb(info->IER & ~UART_IER_THRI,
Jiri Slaby55b307d2006-12-08 02:38:14 -08002587 info->ioaddr +
2588 UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08002589 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002590 outb(info->IER, info->ioaddr +
2591 UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08002592 }
2593 set_bit(MXSER_EVENT_TXLOW, &info->event);
2594 schedule_work(&info->tqueue); }
2595 } else {
2596 if (!(status & UART_MSR_CTS)) {
2597 info->tty->hw_stopped = 1;
2598 if ((info->type != PORT_16550A) &&
Jiri Slaby55b307d2006-12-08 02:38:14 -08002599 (!info->board->chip_flag)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002600 info->IER &= ~UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002601 outb(info->IER, info->ioaddr +
2602 UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08002603 }
2604 }
2605 }
2606 }
2607 } else {
2608 info->flags &= ~ASYNC_CTS_FLOW;
2609 }
Jiri Slaby55b307d2006-12-08 02:38:14 -08002610 outb(info->MCR, info->ioaddr + UART_MCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002611 if (cflag & CLOCAL) {
2612 info->flags &= ~ASYNC_CHECK_CD;
2613 } else {
2614 info->flags |= ASYNC_CHECK_CD;
2615 info->IER |= UART_IER_MSI;
2616 }
Jiri Slaby55b307d2006-12-08 02:38:14 -08002617 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08002618
2619 /*
2620 * Set up parity check flag
2621 */
2622 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
2623 if (I_INPCK(info->tty))
2624 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
2625 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
2626 info->read_status_mask |= UART_LSR_BI;
2627
2628 info->ignore_status_mask = 0;
2629
2630 if (I_IGNBRK(info->tty)) {
2631 info->ignore_status_mask |= UART_LSR_BI;
2632 info->read_status_mask |= UART_LSR_BI;
2633 /*
2634 * If we're ignore parity and break indicators, ignore
2635 * overruns too. (For real raw support).
2636 */
2637 if (I_IGNPAR(info->tty)) {
2638 info->ignore_status_mask |=
2639 UART_LSR_OE |
2640 UART_LSR_PE |
2641 UART_LSR_FE;
2642 info->read_status_mask |=
2643 UART_LSR_OE |
2644 UART_LSR_PE |
2645 UART_LSR_FE;
2646 }
2647 }
2648 /* following add by Victor Yu. 09-02-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002649 if (info->board->chip_flag) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002650 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby55b307d2006-12-08 02:38:14 -08002651 SET_MOXA_MUST_XON1_VALUE(info->ioaddr, START_CHAR(info->tty));
2652 SET_MOXA_MUST_XOFF1_VALUE(info->ioaddr, STOP_CHAR(info->tty));
Jiri Slaby037ad482006-12-08 02:38:11 -08002653 if (I_IXON(info->tty)) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002654 ENABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
Jiri Slaby037ad482006-12-08 02:38:11 -08002655 } else {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002656 DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
Jiri Slaby037ad482006-12-08 02:38:11 -08002657 }
2658 if (I_IXOFF(info->tty)) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002659 ENABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
Jiri Slaby037ad482006-12-08 02:38:11 -08002660 } else {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002661 DISABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
Jiri Slaby037ad482006-12-08 02:38:11 -08002662 }
2663 /*
2664 if ( I_IXANY(info->tty) ) {
2665 info->MCR |= MOXA_MUST_MCR_XON_ANY;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002666 ENABLE_MOXA_MUST_XON_ANY_FLOW_CONTROL(info->ioaddr);
Jiri Slaby037ad482006-12-08 02:38:11 -08002667 } else {
2668 info->MCR &= ~MOXA_MUST_MCR_XON_ANY;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002669 DISABLE_MOXA_MUST_XON_ANY_FLOW_CONTROL(info->ioaddr);
Jiri Slaby037ad482006-12-08 02:38:11 -08002670 }
2671 */
2672 spin_unlock_irqrestore(&info->slock, flags);
2673 }
2674 /* above add by Victor Yu. 09-02-2002 */
2675
2676
Jiri Slaby55b307d2006-12-08 02:38:14 -08002677 outb(fcr, info->ioaddr + UART_FCR); /* set fcr */
2678 outb(cval, info->ioaddr + UART_LCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002679
2680 return ret;
2681}
2682
2683
Jiri Slaby55b307d2006-12-08 02:38:14 -08002684static int mxser_set_baud(struct mxser_port *info, long newspd)
Jiri Slaby037ad482006-12-08 02:38:11 -08002685{
2686 int quot = 0;
2687 unsigned char cval;
2688 int ret = 0;
2689 unsigned long flags;
2690
2691 if (!info->tty || !info->tty->termios)
2692 return ret;
2693
Jiri Slaby55b307d2006-12-08 02:38:14 -08002694 if (!(info->ioaddr))
Jiri Slaby037ad482006-12-08 02:38:11 -08002695 return ret;
2696
Jiri Slaby55b307d2006-12-08 02:38:14 -08002697 if (newspd > info->max_baud)
Jiri Slaby037ad482006-12-08 02:38:11 -08002698 return 0;
2699
2700 info->realbaud = newspd;
2701 if (newspd == 134) {
2702 quot = (2 * info->baud_base / 269);
2703 } else if (newspd) {
2704 quot = info->baud_base / newspd;
2705 if (quot == 0)
2706 quot = 1;
2707 } else {
2708 quot = 0;
2709 }
2710
2711 info->timeout = ((info->xmit_fifo_size * HZ * 10 * quot) / info->baud_base);
2712 info->timeout += HZ / 50; /* Add .02 seconds of slop */
2713
2714 if (quot) {
2715 spin_lock_irqsave(&info->slock, flags);
2716 info->MCR |= UART_MCR_DTR;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002717 outb(info->MCR, info->ioaddr + UART_MCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002718 spin_unlock_irqrestore(&info->slock, flags);
2719 } else {
2720 spin_lock_irqsave(&info->slock, flags);
2721 info->MCR &= ~UART_MCR_DTR;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002722 outb(info->MCR, info->ioaddr + UART_MCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002723 spin_unlock_irqrestore(&info->slock, flags);
2724 return ret;
2725 }
2726
Jiri Slaby55b307d2006-12-08 02:38:14 -08002727 cval = inb(info->ioaddr + UART_LCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002728
Jiri Slaby55b307d2006-12-08 02:38:14 -08002729 outb(cval | UART_LCR_DLAB, info->ioaddr + UART_LCR); /* set DLAB */
Jiri Slaby037ad482006-12-08 02:38:11 -08002730
Jiri Slaby55b307d2006-12-08 02:38:14 -08002731 outb(quot & 0xff, info->ioaddr + UART_DLL); /* LS of divisor */
2732 outb(quot >> 8, info->ioaddr + UART_DLM); /* MS of divisor */
2733 outb(cval, info->ioaddr + UART_LCR); /* reset DLAB */
Jiri Slaby037ad482006-12-08 02:38:11 -08002734
2735
2736 return ret;
2737}
2738
2739/*
2740 * ------------------------------------------------------------
2741 * friends of mxser_ioctl()
2742 * ------------------------------------------------------------
2743 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002744static int mxser_get_serial_info(struct mxser_port *info,
2745 struct serial_struct __user *retinfo)
Jiri Slaby037ad482006-12-08 02:38:11 -08002746{
2747 struct serial_struct tmp;
2748
2749 if (!retinfo)
2750 return -EFAULT;
2751 memset(&tmp, 0, sizeof(tmp));
2752 tmp.type = info->type;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002753 tmp.line = info->tty->index;
2754 tmp.port = info->ioaddr;
2755 tmp.irq = info->board->irq;
Jiri Slaby037ad482006-12-08 02:38:11 -08002756 tmp.flags = info->flags;
2757 tmp.baud_base = info->baud_base;
2758 tmp.close_delay = info->close_delay;
2759 tmp.closing_wait = info->closing_wait;
2760 tmp.custom_divisor = info->custom_divisor;
2761 tmp.hub6 = 0;
2762 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2763 return -EFAULT;
2764 return 0;
2765}
2766
Jiri Slaby55b307d2006-12-08 02:38:14 -08002767static int mxser_set_serial_info(struct mxser_port *info,
2768 struct serial_struct __user *new_info)
Jiri Slaby037ad482006-12-08 02:38:11 -08002769{
2770 struct serial_struct new_serial;
2771 unsigned int flags;
2772 int retval = 0;
2773
Jiri Slaby55b307d2006-12-08 02:38:14 -08002774 if (!new_info || !info->ioaddr)
Jiri Slaby037ad482006-12-08 02:38:11 -08002775 return -EFAULT;
2776 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
2777 return -EFAULT;
2778
Jiri Slaby55b307d2006-12-08 02:38:14 -08002779 if ((new_serial.irq != info->board->irq) ||
2780 (new_serial.port != info->ioaddr) ||
Jiri Slaby037ad482006-12-08 02:38:11 -08002781 (new_serial.custom_divisor != info->custom_divisor) ||
2782 (new_serial.baud_base != info->baud_base))
2783 return -EPERM;
2784
2785 flags = info->flags & ASYNC_SPD_MASK;
2786
2787 if (!capable(CAP_SYS_ADMIN)) {
2788 if ((new_serial.baud_base != info->baud_base) ||
2789 (new_serial.close_delay != info->close_delay) ||
2790 ((new_serial.flags & ~ASYNC_USR_MASK) != (info->flags & ~ASYNC_USR_MASK)))
2791 return -EPERM;
2792 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
2793 (new_serial.flags & ASYNC_USR_MASK));
2794 } else {
2795 /*
2796 * OK, past this point, all the error checking has been done.
2797 * At this point, we start making changes.....
2798 */
2799 info->flags = ((info->flags & ~ASYNC_FLAGS) |
2800 (new_serial.flags & ASYNC_FLAGS));
2801 info->close_delay = new_serial.close_delay * HZ / 100;
2802 info->closing_wait = new_serial.closing_wait * HZ / 100;
2803 info->tty->low_latency =
2804 (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
2805 info->tty->low_latency = 0; /* (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0; */
2806 }
2807
2808 /* added by casper, 3/17/2000, for mouse */
2809 info->type = new_serial.type;
2810
2811 process_txrx_fifo(info);
2812
2813 if (info->flags & ASYNC_INITIALIZED) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002814 if (flags != (info->flags & ASYNC_SPD_MASK))
Jiri Slaby037ad482006-12-08 02:38:11 -08002815 mxser_change_speed(info, NULL);
Jiri Slaby55b307d2006-12-08 02:38:14 -08002816 } else
Jiri Slaby037ad482006-12-08 02:38:11 -08002817 retval = mxser_startup(info);
Jiri Slaby55b307d2006-12-08 02:38:14 -08002818
Jiri Slaby037ad482006-12-08 02:38:11 -08002819 return retval;
2820}
2821
2822/*
2823 * mxser_get_lsr_info - get line status register info
2824 *
2825 * Purpose: Let user call ioctl() to get info when the UART physically
2826 * is emptied. On bus types like RS485, the transmitter must
2827 * release the bus after transmitting. This must be done when
2828 * the transmit shift register is empty, not be done when the
2829 * transmit holding register is empty. This functionality
2830 * allows an RS485 driver to be written in user space.
2831 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002832static int mxser_get_lsr_info(struct mxser_port *info,
2833 unsigned int __user *value)
Jiri Slaby037ad482006-12-08 02:38:11 -08002834{
2835 unsigned char status;
2836 unsigned int result;
2837 unsigned long flags;
2838
2839 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby55b307d2006-12-08 02:38:14 -08002840 status = inb(info->ioaddr + UART_LSR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002841 spin_unlock_irqrestore(&info->slock, flags);
2842 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
2843 return put_user(result, value);
2844}
2845
2846/*
2847 * This routine sends a break character out the serial port.
2848 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002849static void mxser_send_break(struct mxser_port *info, int duration)
Jiri Slaby037ad482006-12-08 02:38:11 -08002850{
2851 unsigned long flags;
2852
Jiri Slaby55b307d2006-12-08 02:38:14 -08002853 if (!info->ioaddr)
Jiri Slaby037ad482006-12-08 02:38:11 -08002854 return;
2855 set_current_state(TASK_INTERRUPTIBLE);
2856 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby55b307d2006-12-08 02:38:14 -08002857 outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
2858 info->ioaddr + UART_LCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002859 spin_unlock_irqrestore(&info->slock, flags);
2860 schedule_timeout(duration);
2861 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby55b307d2006-12-08 02:38:14 -08002862 outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
2863 info->ioaddr + UART_LCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002864 spin_unlock_irqrestore(&info->slock, flags);
2865}
2866
2867static int mxser_tiocmget(struct tty_struct *tty, struct file *file)
2868{
Jiri Slaby55b307d2006-12-08 02:38:14 -08002869 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08002870 unsigned char control, status;
2871 unsigned long flags;
2872
2873
2874 if (tty->index == MXSER_PORTS)
2875 return -ENOIOCTLCMD;
2876 if (tty->flags & (1 << TTY_IO_ERROR))
2877 return -EIO;
2878
2879 control = info->MCR;
2880
2881 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby55b307d2006-12-08 02:38:14 -08002882 status = inb(info->ioaddr + UART_MSR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002883 if (status & UART_MSR_ANY_DELTA)
2884 mxser_check_modem_status(info, status);
2885 spin_unlock_irqrestore(&info->slock, flags);
2886 return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0) |
2887 ((control & UART_MCR_DTR) ? TIOCM_DTR : 0) |
2888 ((status & UART_MSR_DCD) ? TIOCM_CAR : 0) |
2889 ((status & UART_MSR_RI) ? TIOCM_RNG : 0) |
2890 ((status & UART_MSR_DSR) ? TIOCM_DSR : 0) |
2891 ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
2892}
2893
Jiri Slaby55b307d2006-12-08 02:38:14 -08002894static int mxser_tiocmset(struct tty_struct *tty, struct file *file,
2895 unsigned int set, unsigned int clear)
Jiri Slaby037ad482006-12-08 02:38:11 -08002896{
Jiri Slaby55b307d2006-12-08 02:38:14 -08002897 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08002898 unsigned long flags;
2899
2900
2901 if (tty->index == MXSER_PORTS)
2902 return -ENOIOCTLCMD;
2903 if (tty->flags & (1 << TTY_IO_ERROR))
2904 return -EIO;
2905
2906 spin_lock_irqsave(&info->slock, flags);
2907
2908 if (set & TIOCM_RTS)
2909 info->MCR |= UART_MCR_RTS;
2910 if (set & TIOCM_DTR)
2911 info->MCR |= UART_MCR_DTR;
2912
2913 if (clear & TIOCM_RTS)
2914 info->MCR &= ~UART_MCR_RTS;
2915 if (clear & TIOCM_DTR)
2916 info->MCR &= ~UART_MCR_DTR;
2917
Jiri Slaby55b307d2006-12-08 02:38:14 -08002918 outb(info->MCR, info->ioaddr + UART_MCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002919 spin_unlock_irqrestore(&info->slock, flags);
2920 return 0;
2921}
2922
2923
2924static int mxser_read_register(int, unsigned short *);
2925static int mxser_program_mode(int);
2926static void mxser_normal_mode(int);
2927
Jiri Slaby943f2952006-12-08 02:38:15 -08002928static int __init mxser_get_ISA_conf(int cap, struct mxser_board *brd)
Jiri Slaby037ad482006-12-08 02:38:11 -08002929{
2930 int id, i, bits;
2931 unsigned short regs[16], irq;
2932 unsigned char scratch, scratch2;
2933
Jiri Slaby55b307d2006-12-08 02:38:14 -08002934 brd->chip_flag = MOXA_OTHER_UART;
Jiri Slaby037ad482006-12-08 02:38:11 -08002935
2936 id = mxser_read_register(cap, regs);
2937 if (id == C168_ASIC_ID) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002938 brd->board_type = MXSER_BOARD_C168_ISA;
2939 brd->nports = 8;
Jiri Slaby037ad482006-12-08 02:38:11 -08002940 } else if (id == C104_ASIC_ID) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002941 brd->board_type = MXSER_BOARD_C104_ISA;
2942 brd->nports = 4;
Jiri Slaby037ad482006-12-08 02:38:11 -08002943 } else if (id == C102_ASIC_ID) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002944 brd->board_type = MXSER_BOARD_C102_ISA;
2945 brd->nports = 2;
Jiri Slaby037ad482006-12-08 02:38:11 -08002946 } else if (id == CI132_ASIC_ID) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002947 brd->board_type = MXSER_BOARD_CI132;
2948 brd->nports = 2;
Jiri Slaby037ad482006-12-08 02:38:11 -08002949 } else if (id == CI134_ASIC_ID) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002950 brd->board_type = MXSER_BOARD_CI134;
2951 brd->nports = 4;
Jiri Slaby037ad482006-12-08 02:38:11 -08002952 } else if (id == CI104J_ASIC_ID) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002953 brd->board_type = MXSER_BOARD_CI104J;
2954 brd->nports = 4;
Jiri Slaby037ad482006-12-08 02:38:11 -08002955 } else
2956 return 0;
2957
2958 irq = 0;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002959 if (brd->nports == 2) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002960 irq = regs[9] & 0xF000;
2961 irq = irq | (irq >> 4);
2962 if (irq != (regs[9] & 0xFF00))
2963 return MXSER_ERR_IRQ_CONFLIT;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002964 } else if (brd->nports == 4) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002965 irq = regs[9] & 0xF000;
2966 irq = irq | (irq >> 4);
2967 irq = irq | (irq >> 8);
2968 if (irq != regs[9])
2969 return MXSER_ERR_IRQ_CONFLIT;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002970 } else if (brd->nports == 8) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002971 irq = regs[9] & 0xF000;
2972 irq = irq | (irq >> 4);
2973 irq = irq | (irq >> 8);
2974 if ((irq != regs[9]) || (irq != regs[10]))
2975 return MXSER_ERR_IRQ_CONFLIT;
2976 }
2977
2978 if (!irq)
2979 return MXSER_ERR_IRQ;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002980 brd->irq = ((int)(irq & 0xF000) >> 12);
Jiri Slaby037ad482006-12-08 02:38:11 -08002981 for (i = 0; i < 8; i++)
Jiri Slaby55b307d2006-12-08 02:38:14 -08002982 brd->ports[i].ioaddr = (int) regs[i + 1] & 0xFFF8;
Jiri Slaby037ad482006-12-08 02:38:11 -08002983 if ((regs[12] & 0x80) == 0)
2984 return MXSER_ERR_VECTOR;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002985 brd->vector = (int)regs[11]; /* interrupt vector */
Jiri Slaby037ad482006-12-08 02:38:11 -08002986 if (id == 1)
Jiri Slaby55b307d2006-12-08 02:38:14 -08002987 brd->vector_mask = 0x00FF;
Jiri Slaby037ad482006-12-08 02:38:11 -08002988 else
Jiri Slaby55b307d2006-12-08 02:38:14 -08002989 brd->vector_mask = 0x000F;
Jiri Slaby037ad482006-12-08 02:38:11 -08002990 for (i = 7, bits = 0x0100; i >= 0; i--, bits <<= 1) {
2991 if (regs[12] & bits) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002992 brd->ports[i].baud_base = 921600;
2993 brd->ports[i].max_baud = 921600; /* add by Victor Yu. 09-04-2002 */
Jiri Slaby037ad482006-12-08 02:38:11 -08002994 } else {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002995 brd->ports[i].baud_base = 115200;
2996 brd->ports[i].max_baud = 115200; /* add by Victor Yu. 09-04-2002 */
Jiri Slaby037ad482006-12-08 02:38:11 -08002997 }
2998 }
2999 scratch2 = inb(cap + UART_LCR) & (~UART_LCR_DLAB);
3000 outb(scratch2 | UART_LCR_DLAB, cap + UART_LCR);
3001 outb(0, cap + UART_EFR); /* EFR is the same as FCR */
3002 outb(scratch2, cap + UART_LCR);
3003 outb(UART_FCR_ENABLE_FIFO, cap + UART_FCR);
3004 scratch = inb(cap + UART_IIR);
3005
3006 if (scratch & 0xC0)
Jiri Slaby55b307d2006-12-08 02:38:14 -08003007 brd->uart_type = PORT_16550A;
Jiri Slaby037ad482006-12-08 02:38:11 -08003008 else
Jiri Slaby55b307d2006-12-08 02:38:14 -08003009 brd->uart_type = PORT_16450;
Jiri Slaby037ad482006-12-08 02:38:11 -08003010 if (id == 1)
Jiri Slaby55b307d2006-12-08 02:38:14 -08003011 brd->nports = 8;
Jiri Slaby037ad482006-12-08 02:38:11 -08003012 else
Jiri Slaby55b307d2006-12-08 02:38:14 -08003013 brd->nports = 4;
3014 request_region(brd->ports[0].ioaddr, 8 * brd->nports, "mxser(IO)");
3015 request_region(brd->vector, 1, "mxser(vector)");
3016 return brd->nports;
Jiri Slaby037ad482006-12-08 02:38:11 -08003017}
3018
3019#define CHIP_SK 0x01 /* Serial Data Clock in Eprom */
3020#define CHIP_DO 0x02 /* Serial Data Output in Eprom */
3021#define CHIP_CS 0x04 /* Serial Chip Select in Eprom */
3022#define CHIP_DI 0x08 /* Serial Data Input in Eprom */
3023#define EN_CCMD 0x000 /* Chip's command register */
3024#define EN0_RSARLO 0x008 /* Remote start address reg 0 */
3025#define EN0_RSARHI 0x009 /* Remote start address reg 1 */
3026#define EN0_RCNTLO 0x00A /* Remote byte count reg WR */
3027#define EN0_RCNTHI 0x00B /* Remote byte count reg WR */
3028#define EN0_DCFG 0x00E /* Data configuration reg WR */
3029#define EN0_PORT 0x010 /* Rcv missed frame error counter RD */
3030#define ENC_PAGE0 0x000 /* Select page 0 of chip registers */
3031#define ENC_PAGE3 0x0C0 /* Select page 3 of chip registers */
3032static int mxser_read_register(int port, unsigned short *regs)
3033{
3034 int i, k, value, id;
3035 unsigned int j;
3036
3037 id = mxser_program_mode(port);
3038 if (id < 0)
3039 return id;
3040 for (i = 0; i < 14; i++) {
3041 k = (i & 0x3F) | 0x180;
3042 for (j = 0x100; j > 0; j >>= 1) {
3043 outb(CHIP_CS, port);
3044 if (k & j) {
3045 outb(CHIP_CS | CHIP_DO, port);
3046 outb(CHIP_CS | CHIP_DO | CHIP_SK, port); /* A? bit of read */
3047 } else {
3048 outb(CHIP_CS, port);
3049 outb(CHIP_CS | CHIP_SK, port); /* A? bit of read */
3050 }
3051 }
3052 (void)inb(port);
3053 value = 0;
3054 for (k = 0, j = 0x8000; k < 16; k++, j >>= 1) {
3055 outb(CHIP_CS, port);
3056 outb(CHIP_CS | CHIP_SK, port);
3057 if (inb(port) & CHIP_DI)
3058 value |= j;
3059 }
3060 regs[i] = value;
3061 outb(0, port);
3062 }
3063 mxser_normal_mode(port);
3064 return id;
3065}
3066
3067static int mxser_program_mode(int port)
3068{
3069 int id, i, j, n;
3070 /* unsigned long flags; */
3071
3072 spin_lock(&gm_lock);
3073 outb(0, port);
3074 outb(0, port);
3075 outb(0, port);
3076 (void)inb(port);
3077 (void)inb(port);
3078 outb(0, port);
3079 (void)inb(port);
3080 /* restore_flags(flags); */
3081 spin_unlock(&gm_lock);
3082
3083 id = inb(port + 1) & 0x1F;
3084 if ((id != C168_ASIC_ID) &&
3085 (id != C104_ASIC_ID) &&
3086 (id != C102_ASIC_ID) &&
3087 (id != CI132_ASIC_ID) &&
3088 (id != CI134_ASIC_ID) &&
3089 (id != CI104J_ASIC_ID))
3090 return -1;
3091 for (i = 0, j = 0; i < 4; i++) {
3092 n = inb(port + 2);
3093 if (n == 'M') {
3094 j = 1;
3095 } else if ((j == 1) && (n == 1)) {
3096 j = 2;
3097 break;
3098 } else
3099 j = 0;
3100 }
3101 if (j != 2)
3102 id = -2;
3103 return id;
3104}
3105
3106static void mxser_normal_mode(int port)
3107{
3108 int i, n;
3109
3110 outb(0xA5, port + 1);
3111 outb(0x80, port + 3);
3112 outb(12, port + 0); /* 9600 bps */
3113 outb(0, port + 1);
3114 outb(0x03, port + 3); /* 8 data bits */
3115 outb(0x13, port + 4); /* loop back mode */
3116 for (i = 0; i < 16; i++) {
3117 n = inb(port + 5);
3118 if ((n & 0x61) == 0x60)
3119 break;
3120 if ((n & 1) == 1)
3121 (void)inb(port);
3122 }
3123 outb(0x00, port + 4);
3124}
3125
3126module_init(mxser_module_init);
3127module_exit(mxser_module_exit);