blob: 4db43f31b693cdbd12fcb185839370a88042cdee [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 */
Jiri Slaby59628012006-12-08 02:38:16 -0800529 pci_release_region(pdev, 2);
530 pci_release_region(pdev, 3);
Jiri Slaby037ad482006-12-08 02:38:11 -0800531 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);
Jiri Slaby59628012006-12-08 02:38:16 -0800630 pci_request_region(pdev, 2, "mxser(IO)");
Jiri Slaby037ad482006-12-08 02:38:11 -0800631
Jiri Slaby55b307d2006-12-08 02:38:14 -0800632 for (i = 0; i < brd->nports; i++)
633 brd->ports[i].ioaddr = ioaddress + 8 * i;
Jiri Slaby037ad482006-12-08 02:38:11 -0800634
635 /* vector */
636 ioaddress = pci_resource_start(pdev, 3);
Jiri Slaby59628012006-12-08 02:38:16 -0800637 pci_request_region(pdev, 3, "mxser(vector)");
Jiri Slaby55b307d2006-12-08 02:38:14 -0800638 brd->vector = ioaddress;
Jiri Slaby037ad482006-12-08 02:38:11 -0800639
640 /* irq */
Jiri Slaby55b307d2006-12-08 02:38:14 -0800641 brd->irq = pdev->irq;
Jiri Slaby037ad482006-12-08 02:38:11 -0800642
Jiri Slaby55b307d2006-12-08 02:38:14 -0800643 brd->chip_flag = CheckIsMoxaMust(brd->ports[0].ioaddr);
644 brd->uart_type = PORT_16550A;
645 brd->vector_mask = 0;
Jiri Slaby037ad482006-12-08 02:38:11 -0800646
Jiri Slaby55b307d2006-12-08 02:38:14 -0800647 for (i = 0; i < brd->nports; i++) {
Jiri Slaby037ad482006-12-08 02:38:11 -0800648 for (j = 0; j < UART_INFO_NUM; j++) {
Jiri Slaby55b307d2006-12-08 02:38:14 -0800649 if (Gpci_uart_info[j].type == brd->chip_flag) {
650 brd->ports[i].max_baud =
651 Gpci_uart_info[j].max_baud;
Jiri Slaby037ad482006-12-08 02:38:11 -0800652
653 /* exception....CP-102 */
654 if (board_type == MXSER_BOARD_CP102)
Jiri Slaby55b307d2006-12-08 02:38:14 -0800655 brd->ports[i].max_baud = 921600;
Jiri Slaby037ad482006-12-08 02:38:11 -0800656 break;
657 }
658 }
659 }
660
Jiri Slaby55b307d2006-12-08 02:38:14 -0800661 if (brd->chip_flag == MOXA_MUST_MU860_HWID) {
662 for (i = 0; i < brd->nports; i++) {
Jiri Slaby037ad482006-12-08 02:38:11 -0800663 if (i < 4)
Jiri Slaby55b307d2006-12-08 02:38:14 -0800664 brd->ports[i].opmode_ioaddr = ioaddress + 4;
Jiri Slaby037ad482006-12-08 02:38:11 -0800665 else
Jiri Slaby55b307d2006-12-08 02:38:14 -0800666 brd->ports[i].opmode_ioaddr = ioaddress + 0x0c;
Jiri Slaby037ad482006-12-08 02:38:11 -0800667 }
668 outb(0, ioaddress + 4); /* default set to RS232 mode */
669 outb(0, ioaddress + 0x0c); /* default set to RS232 mode */
670 }
671
Jiri Slaby55b307d2006-12-08 02:38:14 -0800672 for (i = 0; i < brd->nports; i++) {
673 brd->vector_mask |= (1 << i);
674 brd->ports[i].baud_base = 921600;
Jiri Slaby037ad482006-12-08 02:38:11 -0800675 }
676 return 0;
677}
Jiri Slaby037ad482006-12-08 02:38:11 -0800678
Jiri Slaby943f2952006-12-08 02:38:15 -0800679static int __init mxser_init(void)
Jiri Slaby037ad482006-12-08 02:38:11 -0800680{
Jiri Slaby037ad482006-12-08 02:38:11 -0800681 struct pci_dev *pdev = NULL;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800682 struct mxser_board *brd;
683 unsigned int i, m;
684 int retval, b, n;
Jiri Slaby037ad482006-12-08 02:38:11 -0800685
686 mxvar_sdriver = alloc_tty_driver(MXSER_PORTS + 1);
687 if (!mxvar_sdriver)
688 return -ENOMEM;
689 spin_lock_init(&gm_lock);
690
Jiri Slaby55b307d2006-12-08 02:38:14 -0800691 for (i = 0; i < MXSER_BOARDS; i++)
692 mxser_boards[i].board_type = -1;
Jiri Slaby037ad482006-12-08 02:38:11 -0800693
694 printk(KERN_INFO "MOXA Smartio/Industio family driver version %s\n",
695 MXSER_VERSION);
696
697 /* Initialize the tty_driver structure */
698 memset(mxvar_sdriver, 0, sizeof(struct tty_driver));
699 mxvar_sdriver->magic = TTY_DRIVER_MAGIC;
700 mxvar_sdriver->name = "ttyM";
701 mxvar_sdriver->major = ttymajor;
702 mxvar_sdriver->minor_start = 0;
703 mxvar_sdriver->num = MXSER_PORTS + 1;
704 mxvar_sdriver->type = TTY_DRIVER_TYPE_SERIAL;
705 mxvar_sdriver->subtype = SERIAL_TYPE_NORMAL;
706 mxvar_sdriver->init_termios = tty_std_termios;
707 mxvar_sdriver->init_termios.c_cflag = B9600|CS8|CREAD|HUPCL|CLOCAL;
708 mxvar_sdriver->flags = TTY_DRIVER_REAL_RAW;
709 tty_set_operations(mxvar_sdriver, &mxser_ops);
710 mxvar_sdriver->ttys = mxvar_tty;
711 mxvar_sdriver->termios = mxvar_termios;
712 mxvar_sdriver->termios_locked = mxvar_termios_locked;
713
714 mxvar_diagflag = 0;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800715 memset(mxser_boards, 0, sizeof(mxser_boards));
Jiri Slaby037ad482006-12-08 02:38:11 -0800716 memset(&mxvar_log, 0, sizeof(struct mxser_log));
717
718 memset(&mxser_msr, 0, sizeof(unsigned char) * (MXSER_PORTS + 1));
719 memset(&mon_data_ext, 0, sizeof(struct mxser_mon_ext));
720 memset(&mxser_set_baud_method, 0, sizeof(int) * (MXSER_PORTS + 1));
Jiri Slaby037ad482006-12-08 02:38:11 -0800721
722 m = 0;
723 /* Start finding ISA boards here */
724 for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
725 int cap;
726
727 if (!(cap = mxserBoardCAP[b]))
728 continue;
729
Jiri Slaby55b307d2006-12-08 02:38:14 -0800730 brd = &mxser_boards[m];
731 retval = mxser_get_ISA_conf(cap, brd);
Jiri Slaby037ad482006-12-08 02:38:11 -0800732
733 if (retval != 0)
734 printk(KERN_INFO "Found MOXA %s board (CAP=0x%x)\n",
Jiri Slaby55b307d2006-12-08 02:38:14 -0800735 mxser_brdname[brd->board_type - 1], ioaddr[b]);
Jiri Slaby037ad482006-12-08 02:38:11 -0800736
737 if (retval <= 0) {
738 if (retval == MXSER_ERR_IRQ)
739 printk(KERN_ERR "Invalid interrupt number, "
740 "board not configured\n");
741 else if (retval == MXSER_ERR_IRQ_CONFLIT)
742 printk(KERN_ERR "Invalid interrupt number, "
743 "board not configured\n");
744 else if (retval == MXSER_ERR_VECTOR)
745 printk(KERN_ERR "Invalid interrupt vector, "
746 "board not configured\n");
747 else if (retval == MXSER_ERR_IOADDR)
748 printk(KERN_ERR "Invalid I/O address, "
749 "board not configured\n");
750
751 continue;
752 }
753
Jiri Slaby55b307d2006-12-08 02:38:14 -0800754 brd->pdev = NULL;
Jiri Slaby037ad482006-12-08 02:38:11 -0800755
Jiri Slaby037ad482006-12-08 02:38:11 -0800756 /* mxser_initbrd will hook ISR. */
Jiri Slaby55b307d2006-12-08 02:38:14 -0800757 if (mxser_initbrd(brd) < 0)
Jiri Slaby037ad482006-12-08 02:38:11 -0800758 continue;
759
760 m++;
761 }
762
763 /* Start finding ISA boards from module arg */
764 for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
Jiri Slaby3306ce32006-12-08 02:38:13 -0800765 unsigned long cap;
Jiri Slaby037ad482006-12-08 02:38:11 -0800766
767 if (!(cap = ioaddr[b]))
768 continue;
769
Jiri Slaby55b307d2006-12-08 02:38:14 -0800770 brd = &mxser_boards[m];
771 retval = mxser_get_ISA_conf(cap, &mxser_boards[m]);
Jiri Slaby037ad482006-12-08 02:38:11 -0800772
773 if (retval != 0)
774 printk(KERN_INFO "Found MOXA %s board (CAP=0x%x)\n",
Jiri Slaby55b307d2006-12-08 02:38:14 -0800775 mxser_brdname[brd->board_type - 1], ioaddr[b]);
Jiri Slaby037ad482006-12-08 02:38:11 -0800776
777 if (retval <= 0) {
778 if (retval == MXSER_ERR_IRQ)
779 printk(KERN_ERR "Invalid interrupt number, "
780 "board not configured\n");
781 else if (retval == MXSER_ERR_IRQ_CONFLIT)
782 printk(KERN_ERR "Invalid interrupt number, "
783 "board not configured\n");
784 else if (retval == MXSER_ERR_VECTOR)
785 printk(KERN_ERR "Invalid interrupt vector, "
786 "board not configured\n");
787 else if (retval == MXSER_ERR_IOADDR)
788 printk(KERN_ERR "Invalid I/O address, "
789 "board not configured\n");
790
791 continue;
792 }
793
Jiri Slaby55b307d2006-12-08 02:38:14 -0800794 brd->pdev = NULL;
Jiri Slaby037ad482006-12-08 02:38:11 -0800795 /* mxser_initbrd will hook ISR. */
Jiri Slaby55b307d2006-12-08 02:38:14 -0800796 if (mxser_initbrd(brd) < 0)
Jiri Slaby037ad482006-12-08 02:38:11 -0800797 continue;
798
799 m++;
800 }
801
802 /* start finding PCI board here */
Jiri Slaby037ad482006-12-08 02:38:11 -0800803 n = ARRAY_SIZE(mxser_pcibrds) - 1;
Jiri Slaby037ad482006-12-08 02:38:11 -0800804 b = 0;
805 while (b < n) {
806 pdev = pci_get_device(mxser_pcibrds[b].vendor,
807 mxser_pcibrds[b].device, pdev);
808 if (pdev == NULL) {
809 b++;
810 continue;
811 }
Jiri Slaby037ad482006-12-08 02:38:11 -0800812 printk(KERN_INFO "Found MOXA %s board(BusNo=%d,DevNo=%d)\n",
813 mxser_brdname[(int) (mxser_pcibrds[b].driver_data) - 1],
Jiri Slaby55b307d2006-12-08 02:38:14 -0800814 pdev->bus->number, PCI_SLOT(pdev->devfn));
Jiri Slaby037ad482006-12-08 02:38:11 -0800815 if (m >= MXSER_BOARDS)
816 printk(KERN_ERR
817 "Too many Smartio/Industio family boards find "
818 "(maximum %d), board not configured\n",
819 MXSER_BOARDS);
820 else {
821 if (pci_enable_device(pdev)) {
822 printk(KERN_ERR "Moxa SmartI/O PCI enable "
823 "fail !\n");
824 continue;
825 }
Jiri Slaby55b307d2006-12-08 02:38:14 -0800826 brd = &mxser_boards[m];
827 brd->pdev = pdev;
828 retval = mxser_get_PCI_conf(
Jiri Slaby037ad482006-12-08 02:38:11 -0800829 (int)mxser_pcibrds[b].driver_data,
Jiri Slaby55b307d2006-12-08 02:38:14 -0800830 brd, pdev);
Jiri Slaby037ad482006-12-08 02:38:11 -0800831 if (retval < 0) {
832 if (retval == MXSER_ERR_IRQ)
833 printk(KERN_ERR
834 "Invalid interrupt number, "
835 "board not configured\n");
836 else if (retval == MXSER_ERR_IRQ_CONFLIT)
837 printk(KERN_ERR
838 "Invalid interrupt number, "
839 "board not configured\n");
840 else if (retval == MXSER_ERR_VECTOR)
841 printk(KERN_ERR
842 "Invalid interrupt vector, "
843 "board not configured\n");
844 else if (retval == MXSER_ERR_IOADDR)
845 printk(KERN_ERR
846 "Invalid I/O address, "
847 "board not configured\n");
848 continue;
849 }
Jiri Slaby037ad482006-12-08 02:38:11 -0800850 /* mxser_initbrd will hook ISR. */
Jiri Slaby55b307d2006-12-08 02:38:14 -0800851 if (mxser_initbrd(brd) < 0)
Jiri Slaby037ad482006-12-08 02:38:11 -0800852 continue;
853 m++;
854 /* Keep an extra reference if we succeeded. It will
855 be returned at unload time */
856 pci_dev_get(pdev);
857 }
858 }
Jiri Slaby037ad482006-12-08 02:38:11 -0800859
860 retval = tty_register_driver(mxvar_sdriver);
861 if (retval) {
862 printk(KERN_ERR "Couldn't install MOXA Smartio/Industio family"
863 " driver !\n");
864 put_tty_driver(mxvar_sdriver);
865
866 for (i = 0; i < MXSER_BOARDS; i++) {
Jiri Slaby55b307d2006-12-08 02:38:14 -0800867 if (mxser_boards[i].board_type == -1)
Jiri Slaby037ad482006-12-08 02:38:11 -0800868 continue;
869 else {
Jiri Slaby55b307d2006-12-08 02:38:14 -0800870 free_irq(mxser_boards[i].irq, &mxser_boards[i]);
Jiri Slaby037ad482006-12-08 02:38:11 -0800871 /* todo: release io, vector */
872 }
873 }
874 return retval;
875 }
876
877 return 0;
878}
879
880static void mxser_do_softint(void *private_)
881{
Jiri Slaby55b307d2006-12-08 02:38:14 -0800882 struct mxser_port *info = private_;
Jiri Slaby037ad482006-12-08 02:38:11 -0800883 struct tty_struct *tty;
884
885 tty = info->tty;
886
Jiri Slaby3306ce32006-12-08 02:38:13 -0800887 if (test_and_clear_bit(MXSER_EVENT_TXLOW, &info->event))
888 tty_wakeup(tty);
889 if (test_and_clear_bit(MXSER_EVENT_HANGUP, &info->event))
890 tty_hangup(tty);
Jiri Slaby037ad482006-12-08 02:38:11 -0800891}
892
Jiri Slaby55b307d2006-12-08 02:38:14 -0800893static unsigned char mxser_get_msr(int baseaddr, int mode, int port)
Jiri Slaby037ad482006-12-08 02:38:11 -0800894{
895 unsigned char status = 0;
896
897 status = inb(baseaddr + UART_MSR);
898
899 mxser_msr[port] &= 0x0F;
900 mxser_msr[port] |= status;
901 status = mxser_msr[port];
902 if (mode)
903 mxser_msr[port] = 0;
904
905 return status;
906}
907
908/*
909 * This routine is called whenever a serial port is opened. It
910 * enables interrupts for a serial port, linking in its async structure into
911 * the IRQ chain. It also performs the serial-specific
912 * initialization for the tty structure.
913 */
914static int mxser_open(struct tty_struct *tty, struct file *filp)
915{
Jiri Slaby55b307d2006-12-08 02:38:14 -0800916 struct mxser_port *info;
Jiri Slaby037ad482006-12-08 02:38:11 -0800917 int retval, line;
918
919 /* initialize driver_data in case something fails */
920 tty->driver_data = NULL;
921
922 line = tty->index;
923 if (line == MXSER_PORTS)
924 return 0;
925 if (line < 0 || line > MXSER_PORTS)
926 return -ENODEV;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800927 info = &mxser_boards[line / MXSER_PORTS_PER_BOARD].ports[line % MXSER_PORTS_PER_BOARD];
928 if (!info->ioaddr)
Jiri Slaby037ad482006-12-08 02:38:11 -0800929 return -ENODEV;
930
931 tty->driver_data = info;
932 info->tty = tty;
933 /*
934 * Start up serial port
935 */
Jiri Slaby3306ce32006-12-08 02:38:13 -0800936 info->count++;
Jiri Slaby037ad482006-12-08 02:38:11 -0800937 retval = mxser_startup(info);
938 if (retval)
939 return retval;
940
941 retval = mxser_block_til_ready(tty, filp, info);
942 if (retval)
943 return retval;
944
Jiri Slaby037ad482006-12-08 02:38:11 -0800945 if ((info->count == 1) && (info->flags & ASYNC_SPLIT_TERMIOS)) {
946 if (tty->driver->subtype == SERIAL_TYPE_NORMAL)
947 *tty->termios = info->normal_termios;
948 else
949 *tty->termios = info->callout_termios;
950 mxser_change_speed(info, NULL);
951 }
952
Andrew Morton08a4ae42006-12-08 02:38:12 -0800953 info->session = process_session(current);
Jiri Slaby037ad482006-12-08 02:38:11 -0800954 info->pgrp = process_group(current);
955
956 /*
957 status = mxser_get_msr(info->base, 0, info->port);
958 mxser_check_modem_status(info, status);
959 */
960
961/* unmark here for very high baud rate (ex. 921600 bps) used */
962 tty->low_latency = 1;
963 return 0;
964}
965
966/*
967 * This routine is called when the serial port gets closed. First, we
968 * wait for the last remaining data to be sent. Then, we unlink its
969 * async structure from the interrupt chain if necessary, and we free
970 * that IRQ if nothing is left in the chain.
971 */
972static void mxser_close(struct tty_struct *tty, struct file *filp)
973{
Jiri Slaby55b307d2006-12-08 02:38:14 -0800974 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -0800975
976 unsigned long timeout;
977 unsigned long flags;
978 struct tty_ldisc *ld;
979
980 if (tty->index == MXSER_PORTS)
981 return;
982 if (!info)
983 return;
984
985 spin_lock_irqsave(&info->slock, flags);
986
987 if (tty_hung_up_p(filp)) {
988 spin_unlock_irqrestore(&info->slock, flags);
989 return;
990 }
991 if ((tty->count == 1) && (info->count != 1)) {
992 /*
993 * Uh, oh. tty->count is 1, which means that the tty
994 * structure will be freed. Info->count should always
995 * be one in these conditions. If it's greater than
996 * one, we've got real problems, since it means the
997 * serial port won't be shutdown.
998 */
999 printk(KERN_ERR "mxser_close: bad serial port count; "
1000 "tty->count is 1, info->count is %d\n", info->count);
1001 info->count = 1;
1002 }
1003 if (--info->count < 0) {
1004 printk(KERN_ERR "mxser_close: bad serial port count for "
Jiri Slaby55b307d2006-12-08 02:38:14 -08001005 "ttys%d: %d\n", tty->index, info->count);
Jiri Slaby037ad482006-12-08 02:38:11 -08001006 info->count = 0;
1007 }
1008 if (info->count) {
1009 spin_unlock_irqrestore(&info->slock, flags);
1010 return;
1011 }
1012 info->flags |= ASYNC_CLOSING;
1013 spin_unlock_irqrestore(&info->slock, flags);
1014 /*
1015 * Save the termios structure, since this port may have
1016 * separate termios for callout and dialin.
1017 */
1018 if (info->flags & ASYNC_NORMAL_ACTIVE)
1019 info->normal_termios = *tty->termios;
1020 /*
1021 * Now we wait for the transmit buffer to clear; and we notify
1022 * the line discipline to only process XON/XOFF characters.
1023 */
1024 tty->closing = 1;
1025 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1026 tty_wait_until_sent(tty, info->closing_wait);
1027 /*
1028 * At this point we stop accepting input. To do this, we
1029 * disable the receive line status interrupts, and tell the
1030 * interrupt driver to stop checking the data ready bit in the
1031 * line status register.
1032 */
1033 info->IER &= ~UART_IER_RLSI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001034 if (info->board->chip_flag)
Jiri Slaby037ad482006-12-08 02:38:11 -08001035 info->IER &= ~MOXA_MUST_RECV_ISR;
1036/* by William
1037 info->read_status_mask &= ~UART_LSR_DR;
1038*/
1039 if (info->flags & ASYNC_INITIALIZED) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08001040 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001041 /*
1042 * Before we drop DTR, make sure the UART transmitter
1043 * has completely drained; this is especially
1044 * important if there is a transmit FIFO!
1045 */
1046 timeout = jiffies + HZ;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001047 while (!(inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001048 schedule_timeout_interruptible(5);
1049 if (time_after(jiffies, timeout))
1050 break;
1051 }
1052 }
1053 mxser_shutdown(info);
1054
1055 if (tty->driver->flush_buffer)
1056 tty->driver->flush_buffer(tty);
1057
1058 ld = tty_ldisc_ref(tty);
1059 if (ld) {
1060 if (ld->flush_buffer)
1061 ld->flush_buffer(tty);
1062 tty_ldisc_deref(ld);
1063 }
1064
1065 tty->closing = 0;
1066 info->event = 0;
1067 info->tty = NULL;
1068 if (info->blocked_open) {
1069 if (info->close_delay)
1070 schedule_timeout_interruptible(info->close_delay);
1071 wake_up_interruptible(&info->open_wait);
1072 }
1073
1074 info->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
1075 wake_up_interruptible(&info->close_wait);
1076
1077}
1078
1079static int mxser_write(struct tty_struct *tty, const unsigned char *buf, int count)
1080{
1081 int c, total = 0;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001082 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001083 unsigned long flags;
1084
1085 if (!info->xmit_buf)
1086 return 0;
1087
1088 while (1) {
1089 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1090 SERIAL_XMIT_SIZE - info->xmit_head));
1091 if (c <= 0)
1092 break;
1093
1094 memcpy(info->xmit_buf + info->xmit_head, buf, c);
1095 spin_lock_irqsave(&info->slock, flags);
1096 info->xmit_head = (info->xmit_head + c) &
1097 (SERIAL_XMIT_SIZE - 1);
1098 info->xmit_cnt += c;
1099 spin_unlock_irqrestore(&info->slock, flags);
1100
1101 buf += c;
1102 count -= c;
1103 total += c;
1104 }
1105
Jiri Slaby3306ce32006-12-08 02:38:13 -08001106 if (info->xmit_cnt && !tty->stopped
1107 /*&& !(info->IER & UART_IER_THRI)*/) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001108 if (!tty->hw_stopped ||
1109 (info->type == PORT_16550A) ||
Jiri Slaby55b307d2006-12-08 02:38:14 -08001110 (info->board->chip_flag)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001111 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby55b307d2006-12-08 02:38:14 -08001112 outb(info->IER & ~UART_IER_THRI, info->ioaddr +
1113 UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001114 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001115 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001116 spin_unlock_irqrestore(&info->slock, flags);
1117 }
1118 }
1119 return total;
1120}
1121
1122static void mxser_put_char(struct tty_struct *tty, unsigned char ch)
1123{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001124 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001125 unsigned long flags;
1126
1127 if (!info->xmit_buf)
1128 return;
1129
1130 if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1)
1131 return;
1132
1133 spin_lock_irqsave(&info->slock, flags);
1134 info->xmit_buf[info->xmit_head++] = ch;
1135 info->xmit_head &= SERIAL_XMIT_SIZE - 1;
1136 info->xmit_cnt++;
1137 spin_unlock_irqrestore(&info->slock, flags);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001138 if (!tty->stopped /*&& !(info->IER & UART_IER_THRI)*/) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001139 if (!tty->hw_stopped ||
1140 (info->type == PORT_16550A) ||
Jiri Slaby55b307d2006-12-08 02:38:14 -08001141 info->board->chip_flag) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001142 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby55b307d2006-12-08 02:38:14 -08001143 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001144 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001145 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001146 spin_unlock_irqrestore(&info->slock, flags);
1147 }
1148 }
1149}
1150
1151
1152static void mxser_flush_chars(struct tty_struct *tty)
1153{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001154 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001155 unsigned long flags;
1156
1157 if (info->xmit_cnt <= 0 ||
1158 tty->stopped ||
1159 !info->xmit_buf ||
1160 (tty->hw_stopped &&
1161 (info->type != PORT_16550A) &&
Jiri Slaby55b307d2006-12-08 02:38:14 -08001162 (!info->board->chip_flag)
Jiri Slaby037ad482006-12-08 02:38:11 -08001163 ))
1164 return;
1165
1166 spin_lock_irqsave(&info->slock, flags);
1167
Jiri Slaby55b307d2006-12-08 02:38:14 -08001168 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001169 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001170 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001171
1172 spin_unlock_irqrestore(&info->slock, flags);
1173}
1174
1175static int mxser_write_room(struct tty_struct *tty)
1176{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001177 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001178 int ret;
1179
1180 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1181 if (ret < 0)
1182 ret = 0;
1183 return ret;
1184}
1185
1186static int mxser_chars_in_buffer(struct tty_struct *tty)
1187{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001188 struct mxser_port *info = tty->driver_data;
Jiri Slaby3306ce32006-12-08 02:38:13 -08001189 int len = info->xmit_cnt;
1190
Jiri Slaby55b307d2006-12-08 02:38:14 -08001191 if (!(inb(info->ioaddr + UART_LSR) & UART_LSR_THRE))
Jiri Slaby3306ce32006-12-08 02:38:13 -08001192 len++;
1193
1194 return len;
Jiri Slaby037ad482006-12-08 02:38:11 -08001195}
1196
1197static void mxser_flush_buffer(struct tty_struct *tty)
1198{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001199 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001200 char fcr;
1201 unsigned long flags;
1202
1203
1204 spin_lock_irqsave(&info->slock, flags);
1205 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1206
1207 /* below added by shinhay */
Jiri Slaby55b307d2006-12-08 02:38:14 -08001208 fcr = inb(info->ioaddr + UART_FCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08001209 outb((fcr | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
Jiri Slaby55b307d2006-12-08 02:38:14 -08001210 info->ioaddr + UART_FCR);
1211 outb(fcr, info->ioaddr + UART_FCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08001212
1213 spin_unlock_irqrestore(&info->slock, flags);
1214 /* above added by shinhay */
1215
1216 wake_up_interruptible(&tty->write_wait);
1217 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) && tty->ldisc.write_wakeup)
1218 (tty->ldisc.write_wakeup) (tty);
1219}
1220
1221static int mxser_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
1222{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001223 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001224 int retval;
1225 struct async_icount cprev, cnow; /* kernel counter temps */
1226 struct serial_icounter_struct __user *p_cuser;
1227 unsigned long templ;
1228 unsigned long flags;
1229 void __user *argp = (void __user *)arg;
1230
1231 if (tty->index == MXSER_PORTS)
1232 return mxser_ioctl_special(cmd, argp);
1233
1234 /* following add by Victor Yu. 01-05-2004 */
1235 if (cmd == MOXA_SET_OP_MODE || cmd == MOXA_GET_OP_MODE) {
Jiri Slaby3306ce32006-12-08 02:38:13 -08001236 int p;
1237 unsigned long opmode;
Jiri Slaby037ad482006-12-08 02:38:11 -08001238 static unsigned char ModeMask[] = { 0xfc, 0xf3, 0xcf, 0x3f };
1239 int shiftbit;
1240 unsigned char val, mask;
1241
Jiri Slaby55b307d2006-12-08 02:38:14 -08001242 p = tty->index % 4;
Jiri Slaby037ad482006-12-08 02:38:11 -08001243 if (cmd == MOXA_SET_OP_MODE) {
1244 if (get_user(opmode, (int __user *) argp))
1245 return -EFAULT;
1246 if (opmode != RS232_MODE &&
1247 opmode != RS485_2WIRE_MODE &&
1248 opmode != RS422_MODE &&
1249 opmode != RS485_4WIRE_MODE)
1250 return -EFAULT;
1251 mask = ModeMask[p];
1252 shiftbit = p * 2;
1253 val = inb(info->opmode_ioaddr);
1254 val &= mask;
1255 val |= (opmode << shiftbit);
1256 outb(val, info->opmode_ioaddr);
1257 } else {
1258 shiftbit = p * 2;
1259 opmode = inb(info->opmode_ioaddr) >> shiftbit;
1260 opmode &= OP_MODE_MASK;
1261 if (copy_to_user(argp, &opmode, sizeof(int)))
1262 return -EFAULT;
1263 }
1264 return 0;
1265 }
1266 /* above add by Victor Yu. 01-05-2004 */
1267
1268 if ((cmd != TIOCGSERIAL) && (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
1269 if (tty->flags & (1 << TTY_IO_ERROR))
1270 return -EIO;
1271 }
1272 switch (cmd) {
1273 case TCSBRK: /* SVID version: non-zero arg --> no break */
1274 retval = tty_check_change(tty);
1275 if (retval)
1276 return retval;
1277 tty_wait_until_sent(tty, 0);
1278 if (!arg)
1279 mxser_send_break(info, HZ / 4); /* 1/4 second */
1280 return 0;
1281 case TCSBRKP: /* support for POSIX tcsendbreak() */
1282 retval = tty_check_change(tty);
1283 if (retval)
1284 return retval;
1285 tty_wait_until_sent(tty, 0);
1286 mxser_send_break(info, arg ? arg * (HZ / 10) : HZ / 4);
1287 return 0;
1288 case TIOCGSOFTCAR:
1289 return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *)argp);
1290 case TIOCSSOFTCAR:
1291 if (get_user(templ, (unsigned long __user *) argp))
1292 return -EFAULT;
1293 arg = templ;
1294 tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) | (arg ? CLOCAL : 0));
1295 return 0;
1296 case TIOCGSERIAL:
1297 return mxser_get_serial_info(info, argp);
1298 case TIOCSSERIAL:
1299 return mxser_set_serial_info(info, argp);
1300 case TIOCSERGETLSR: /* Get line status register */
1301 return mxser_get_lsr_info(info, argp);
1302 /*
1303 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1304 * - mask passed in arg for lines of interest
1305 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1306 * Caller should use TIOCGICOUNT to see which one it was
1307 */
1308 case TIOCMIWAIT: {
1309 DECLARE_WAITQUEUE(wait, current);
1310 int ret;
1311 spin_lock_irqsave(&info->slock, flags);
1312 cprev = info->icount; /* note the counters on entry */
1313 spin_unlock_irqrestore(&info->slock, flags);
1314
1315 add_wait_queue(&info->delta_msr_wait, &wait);
1316 while (1) {
1317 spin_lock_irqsave(&info->slock, flags);
1318 cnow = info->icount; /* atomic copy */
1319 spin_unlock_irqrestore(&info->slock, flags);
1320
1321 set_current_state(TASK_INTERRUPTIBLE);
1322 if (((arg & TIOCM_RNG) &&
1323 (cnow.rng != cprev.rng)) ||
1324 ((arg & TIOCM_DSR) &&
1325 (cnow.dsr != cprev.dsr)) ||
1326 ((arg & TIOCM_CD) &&
1327 (cnow.dcd != cprev.dcd)) ||
1328 ((arg & TIOCM_CTS) &&
1329 (cnow.cts != cprev.cts))) {
1330 ret = 0;
1331 break;
1332 }
1333 /* see if a signal did it */
1334 if (signal_pending(current)) {
1335 ret = -ERESTARTSYS;
1336 break;
1337 }
1338 cprev = cnow;
1339 }
1340 current->state = TASK_RUNNING;
1341 remove_wait_queue(&info->delta_msr_wait, &wait);
1342 break;
1343 }
1344 /* NOTREACHED */
1345 /*
1346 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1347 * Return: write counters to the user passed counter struct
1348 * NB: both 1->0 and 0->1 transitions are counted except for
1349 * RI where only 0->1 is counted.
1350 */
1351 case TIOCGICOUNT:
1352 spin_lock_irqsave(&info->slock, flags);
1353 cnow = info->icount;
1354 spin_unlock_irqrestore(&info->slock, flags);
1355 p_cuser = argp;
1356 /* modified by casper 1/11/2000 */
1357 if (put_user(cnow.frame, &p_cuser->frame))
1358 return -EFAULT;
1359 if (put_user(cnow.brk, &p_cuser->brk))
1360 return -EFAULT;
1361 if (put_user(cnow.overrun, &p_cuser->overrun))
1362 return -EFAULT;
1363 if (put_user(cnow.buf_overrun, &p_cuser->buf_overrun))
1364 return -EFAULT;
1365 if (put_user(cnow.parity, &p_cuser->parity))
1366 return -EFAULT;
1367 if (put_user(cnow.rx, &p_cuser->rx))
1368 return -EFAULT;
1369 if (put_user(cnow.tx, &p_cuser->tx))
1370 return -EFAULT;
1371 put_user(cnow.cts, &p_cuser->cts);
1372 put_user(cnow.dsr, &p_cuser->dsr);
1373 put_user(cnow.rng, &p_cuser->rng);
1374 put_user(cnow.dcd, &p_cuser->dcd);
1375 return 0;
1376 case MOXA_HighSpeedOn:
1377 return put_user(info->baud_base != 115200 ? 1 : 0, (int __user *)argp);
1378 case MOXA_SDS_RSTICOUNTER: {
1379 info->mon_data.rxcnt = 0;
1380 info->mon_data.txcnt = 0;
1381 return 0;
1382 }
1383/* (above) added by James. */
1384 case MOXA_ASPP_SETBAUD:{
1385 long baud;
1386 if (get_user(baud, (long __user *)argp))
1387 return -EFAULT;
1388 mxser_set_baud(info, baud);
1389 return 0;
1390 }
1391 case MOXA_ASPP_GETBAUD:
1392 if (copy_to_user(argp, &info->realbaud, sizeof(long)))
1393 return -EFAULT;
1394
1395 return 0;
1396
1397 case MOXA_ASPP_OQUEUE:{
1398 int len, lsr;
1399
1400 len = mxser_chars_in_buffer(tty);
1401
Jiri Slaby55b307d2006-12-08 02:38:14 -08001402 lsr = inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT;
Jiri Slaby037ad482006-12-08 02:38:11 -08001403
1404 len += (lsr ? 0 : 1);
1405
1406 if (copy_to_user(argp, &len, sizeof(int)))
1407 return -EFAULT;
1408
1409 return 0;
1410 }
1411 case MOXA_ASPP_MON: {
1412 int mcr, status;
1413
1414 /* info->mon_data.ser_param = tty->termios->c_cflag; */
1415
Jiri Slaby55b307d2006-12-08 02:38:14 -08001416 status = mxser_get_msr(info->ioaddr, 1, tty->index);
Jiri Slaby037ad482006-12-08 02:38:11 -08001417 mxser_check_modem_status(info, status);
1418
Jiri Slaby55b307d2006-12-08 02:38:14 -08001419 mcr = inb(info->ioaddr + UART_MCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08001420 if (mcr & MOXA_MUST_MCR_XON_FLAG)
1421 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFHOLD;
1422 else
1423 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFHOLD;
1424
1425 if (mcr & MOXA_MUST_MCR_TX_XON)
1426 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFXENT;
1427 else
1428 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFXENT;
1429
1430 if (info->tty->hw_stopped)
1431 info->mon_data.hold_reason |= NPPI_NOTIFY_CTSHOLD;
1432 else
1433 info->mon_data.hold_reason &= ~NPPI_NOTIFY_CTSHOLD;
1434
1435 if (copy_to_user(argp, &info->mon_data,
1436 sizeof(struct mxser_mon)))
1437 return -EFAULT;
1438
1439 return 0;
1440 }
1441
1442 case MOXA_ASPP_LSTATUS: {
1443 if (copy_to_user(argp, &info->err_shadow,
1444 sizeof(unsigned char)))
1445 return -EFAULT;
1446
1447 info->err_shadow = 0;
1448 return 0;
1449 }
1450 case MOXA_SET_BAUD_METHOD: {
1451 int method;
1452
1453 if (get_user(method, (int __user *)argp))
1454 return -EFAULT;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001455 mxser_set_baud_method[tty->index] = method;
Jiri Slaby037ad482006-12-08 02:38:11 -08001456 if (copy_to_user(argp, &method, sizeof(int)))
1457 return -EFAULT;
1458
1459 return 0;
1460 }
1461 default:
1462 return -ENOIOCTLCMD;
1463 }
1464 return 0;
1465}
1466
1467#ifndef CMSPAR
1468#define CMSPAR 010000000000
1469#endif
1470
1471static int mxser_ioctl_special(unsigned int cmd, void __user *argp)
1472{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001473 struct mxser_port *port;
1474 int result, status;
1475 unsigned int i, j;
Jiri Slaby037ad482006-12-08 02:38:11 -08001476
1477 switch (cmd) {
1478 case MOXA_GET_CONF:
Jiri Slaby55b307d2006-12-08 02:38:14 -08001479/* if (copy_to_user(argp, mxsercfg,
Jiri Slaby037ad482006-12-08 02:38:11 -08001480 sizeof(struct mxser_hwconf) * 4))
1481 return -EFAULT;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001482 return 0;*/
1483 return -ENXIO;
Jiri Slaby037ad482006-12-08 02:38:11 -08001484 case MOXA_GET_MAJOR:
1485 if (copy_to_user(argp, &ttymajor, sizeof(int)))
1486 return -EFAULT;
1487 return 0;
1488
1489 case MOXA_GET_CUMAJOR:
1490 if (copy_to_user(argp, &calloutmajor, sizeof(int)))
1491 return -EFAULT;
1492 return 0;
1493
1494 case MOXA_CHKPORTENABLE:
1495 result = 0;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001496
1497 for (i = 0; i < MXSER_BOARDS; i++)
1498 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++)
1499 if (mxser_boards[i].ports[j].ioaddr)
1500 result |= (1 << i);
1501
Jiri Slaby037ad482006-12-08 02:38:11 -08001502 return put_user(result, (unsigned long __user *)argp);
1503 case MOXA_GETDATACOUNT:
1504 if (copy_to_user(argp, &mxvar_log, sizeof(mxvar_log)))
1505 return -EFAULT;
1506 return 0;
1507 case MOXA_GETMSTATUS:
Jiri Slaby55b307d2006-12-08 02:38:14 -08001508 for (i = 0; i < MXSER_BOARDS; i++)
1509 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
1510 port = &mxser_boards[i].ports[j];
1511
1512 GMStatus[i].ri = 0;
1513 if (!port->ioaddr) {
1514 GMStatus[i].dcd = 0;
1515 GMStatus[i].dsr = 0;
1516 GMStatus[i].cts = 0;
1517 continue;
1518 }
1519
1520 if (!port->tty || !port->tty->termios)
1521 GMStatus[i].cflag =
1522 port->normal_termios.c_cflag;
1523 else
1524 GMStatus[i].cflag =
1525 port->tty->termios->c_cflag;
1526
1527 status = inb(port->ioaddr + UART_MSR);
1528 if (status & 0x80 /*UART_MSR_DCD */ )
1529 GMStatus[i].dcd = 1;
1530 else
1531 GMStatus[i].dcd = 0;
1532
1533 if (status & 0x20 /*UART_MSR_DSR */ )
1534 GMStatus[i].dsr = 1;
1535 else
1536 GMStatus[i].dsr = 0;
1537
1538
1539 if (status & 0x10 /*UART_MSR_CTS */ )
1540 GMStatus[i].cts = 1;
1541 else
1542 GMStatus[i].cts = 0;
Jiri Slaby037ad482006-12-08 02:38:11 -08001543 }
Jiri Slaby037ad482006-12-08 02:38:11 -08001544 if (copy_to_user(argp, GMStatus,
1545 sizeof(struct mxser_mstatus) * MXSER_PORTS))
1546 return -EFAULT;
1547 return 0;
1548 case MOXA_ASPP_MON_EXT: {
Jiri Slaby55b307d2006-12-08 02:38:14 -08001549 int status, p, shiftbit;
1550 unsigned long opmode;
1551 unsigned cflag, iflag;
Jiri Slaby037ad482006-12-08 02:38:11 -08001552
Jiri Slaby55b307d2006-12-08 02:38:14 -08001553 for (i = 0; i < MXSER_BOARDS; i++)
1554 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
1555 port = &mxser_boards[i].ports[j];
1556 if (!port->ioaddr)
Jiri Slaby037ad482006-12-08 02:38:11 -08001557 continue;
1558
Jiri Slaby55b307d2006-12-08 02:38:14 -08001559 status = mxser_get_msr(port->ioaddr, 0, i);
1560/* mxser_check_modem_status(port, status); */
1561
Jiri Slaby037ad482006-12-08 02:38:11 -08001562 if (status & UART_MSR_TERI)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001563 port->icount.rng++;
Jiri Slaby037ad482006-12-08 02:38:11 -08001564 if (status & UART_MSR_DDSR)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001565 port->icount.dsr++;
Jiri Slaby037ad482006-12-08 02:38:11 -08001566 if (status & UART_MSR_DDCD)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001567 port->icount.dcd++;
Jiri Slaby037ad482006-12-08 02:38:11 -08001568 if (status & UART_MSR_DCTS)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001569 port->icount.cts++;
Jiri Slaby037ad482006-12-08 02:38:11 -08001570
Jiri Slaby55b307d2006-12-08 02:38:14 -08001571 port->mon_data.modem_status = status;
1572 mon_data_ext.rx_cnt[i] = port->mon_data.rxcnt;
1573 mon_data_ext.tx_cnt[i] = port->mon_data.txcnt;
1574 mon_data_ext.up_rxcnt[i] =
1575 port->mon_data.up_rxcnt;
1576 mon_data_ext.up_txcnt[i] =
1577 port->mon_data.up_txcnt;
1578 mon_data_ext.modem_status[i] =
1579 port->mon_data.modem_status;
1580 mon_data_ext.baudrate[i] = port->realbaud;
Jiri Slaby037ad482006-12-08 02:38:11 -08001581
Jiri Slaby55b307d2006-12-08 02:38:14 -08001582 if (!port->tty || !port->tty->termios) {
1583 cflag = port->normal_termios.c_cflag;
1584 iflag = port->normal_termios.c_iflag;
Jiri Slaby037ad482006-12-08 02:38:11 -08001585 } else {
Jiri Slaby55b307d2006-12-08 02:38:14 -08001586 cflag = port->tty->termios->c_cflag;
1587 iflag = port->tty->termios->c_iflag;
Jiri Slaby037ad482006-12-08 02:38:11 -08001588 }
1589
1590 mon_data_ext.databits[i] = cflag & CSIZE;
1591
1592 mon_data_ext.stopbits[i] = cflag & CSTOPB;
1593
Jiri Slaby55b307d2006-12-08 02:38:14 -08001594 mon_data_ext.parity[i] =
1595 cflag & (PARENB | PARODD | CMSPAR);
Jiri Slaby037ad482006-12-08 02:38:11 -08001596
1597 mon_data_ext.flowctrl[i] = 0x00;
1598
1599 if (cflag & CRTSCTS)
1600 mon_data_ext.flowctrl[i] |= 0x03;
1601
1602 if (iflag & (IXON | IXOFF))
1603 mon_data_ext.flowctrl[i] |= 0x0C;
1604
Jiri Slaby55b307d2006-12-08 02:38:14 -08001605 if (port->type == PORT_16550A)
Jiri Slaby037ad482006-12-08 02:38:11 -08001606 mon_data_ext.fifo[i] = 1;
1607 else
1608 mon_data_ext.fifo[i] = 0;
1609
1610 p = i % 4;
1611 shiftbit = p * 2;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001612 opmode = inb(port->opmode_ioaddr) >> shiftbit;
Jiri Slaby037ad482006-12-08 02:38:11 -08001613 opmode &= OP_MODE_MASK;
1614
1615 mon_data_ext.iftype[i] = opmode;
1616
1617 }
Jiri Slaby55b307d2006-12-08 02:38:14 -08001618 if (copy_to_user(argp, &mon_data_ext,
1619 sizeof(mon_data_ext)))
Jiri Slaby037ad482006-12-08 02:38:11 -08001620 return -EFAULT;
1621
1622 return 0;
1623
Jiri Slaby55b307d2006-12-08 02:38:14 -08001624 } default:
Jiri Slaby037ad482006-12-08 02:38:11 -08001625 return -ENOIOCTLCMD;
1626 }
1627 return 0;
1628}
1629
1630static void mxser_stoprx(struct tty_struct *tty)
1631{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001632 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001633
1634 info->ldisc_stop_rx = 1;
1635 if (I_IXOFF(tty)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001636 /* following add by Victor Yu. 09-02-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08001637 if (info->board->chip_flag) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001638 info->IER &= ~MOXA_MUST_RECV_ISR;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001639 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001640 } else if (!(info->flags & ASYNC_CLOSING)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001641 info->x_char = STOP_CHAR(tty);
Jiri Slaby55b307d2006-12-08 02:38:14 -08001642 outb(0, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001643 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001644 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001645 }
Jiri Slaby037ad482006-12-08 02:38:11 -08001646 }
1647
1648 if (info->tty->termios->c_cflag & CRTSCTS) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001649 info->MCR &= ~UART_MCR_RTS;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001650 outb(info->MCR, info->ioaddr + UART_MCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08001651 }
1652}
1653
1654static void mxser_startrx(struct tty_struct *tty)
1655{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001656 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001657
1658 info->ldisc_stop_rx = 0;
1659 if (I_IXOFF(tty)) {
1660 if (info->x_char)
1661 info->x_char = 0;
1662 else {
Jiri Slaby037ad482006-12-08 02:38:11 -08001663 /* following add by Victor Yu. 09-02-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08001664 if (info->board->chip_flag) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001665 info->IER |= MOXA_MUST_RECV_ISR;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001666 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001667 } else if (!(info->flags & ASYNC_CLOSING)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001668 info->x_char = START_CHAR(tty);
Jiri Slaby55b307d2006-12-08 02:38:14 -08001669 outb(0, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001670 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001671 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001672 }
Jiri Slaby037ad482006-12-08 02:38:11 -08001673 }
1674 }
1675
1676 if (info->tty->termios->c_cflag & CRTSCTS) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001677 info->MCR |= UART_MCR_RTS;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001678 outb(info->MCR, info->ioaddr + UART_MCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08001679 }
1680}
1681
1682/*
1683 * This routine is called by the upper-layer tty layer to signal that
1684 * incoming characters should be throttled.
1685 */
1686static void mxser_throttle(struct tty_struct *tty)
1687{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001688 struct mxser_port *info = tty->driver_data;
Jiri Slaby3306ce32006-12-08 02:38:13 -08001689 unsigned long flags;
Jiri Slaby037ad482006-12-08 02:38:11 -08001690
Jiri Slaby3306ce32006-12-08 02:38:13 -08001691 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby037ad482006-12-08 02:38:11 -08001692 mxser_stoprx(tty);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001693 spin_unlock_irqrestore(&info->slock, flags);
Jiri Slaby037ad482006-12-08 02:38:11 -08001694}
1695
1696static void mxser_unthrottle(struct tty_struct *tty)
1697{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001698 struct mxser_port *info = tty->driver_data;
Jiri Slaby3306ce32006-12-08 02:38:13 -08001699 unsigned long flags;
Jiri Slaby037ad482006-12-08 02:38:11 -08001700
Jiri Slaby3306ce32006-12-08 02:38:13 -08001701 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby037ad482006-12-08 02:38:11 -08001702 mxser_startrx(tty);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001703 spin_unlock_irqrestore(&info->slock, flags);
Jiri Slaby037ad482006-12-08 02:38:11 -08001704}
1705
1706static void mxser_set_termios(struct tty_struct *tty, struct termios *old_termios)
1707{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001708 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001709 unsigned long flags;
1710
1711 if ((tty->termios->c_cflag != old_termios->c_cflag) ||
1712 (RELEVANT_IFLAG(tty->termios->c_iflag) != RELEVANT_IFLAG(old_termios->c_iflag))) {
1713
1714 mxser_change_speed(info, old_termios);
1715
1716 if ((old_termios->c_cflag & CRTSCTS) &&
1717 !(tty->termios->c_cflag & CRTSCTS)) {
1718 tty->hw_stopped = 0;
1719 mxser_start(tty);
1720 }
1721 }
1722
1723/* Handle sw stopped */
1724 if ((old_termios->c_iflag & IXON) &&
1725 !(tty->termios->c_iflag & IXON)) {
1726 tty->stopped = 0;
1727
1728 /* following add by Victor Yu. 09-02-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08001729 if (info->board->chip_flag) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001730 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby55b307d2006-12-08 02:38:14 -08001731 DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
Jiri Slaby037ad482006-12-08 02:38:11 -08001732 spin_unlock_irqrestore(&info->slock, flags);
1733 }
1734 /* above add by Victor Yu. 09-02-2002 */
1735
1736 mxser_start(tty);
1737 }
1738}
1739
1740/*
1741 * mxser_stop() and mxser_start()
1742 *
1743 * This routines are called before setting or resetting tty->stopped.
1744 * They enable or disable transmitter interrupts, as necessary.
1745 */
1746static void mxser_stop(struct tty_struct *tty)
1747{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001748 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001749 unsigned long flags;
1750
1751 spin_lock_irqsave(&info->slock, flags);
1752 if (info->IER & UART_IER_THRI) {
1753 info->IER &= ~UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001754 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001755 }
1756 spin_unlock_irqrestore(&info->slock, flags);
1757}
1758
1759static void mxser_start(struct tty_struct *tty)
1760{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001761 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001762 unsigned long flags;
1763
1764 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001765 if (info->xmit_cnt && info->xmit_buf
1766 /* && !(info->IER & UART_IER_THRI) */) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08001767 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001768 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001769 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001770 }
1771 spin_unlock_irqrestore(&info->slock, flags);
1772}
1773
1774/*
1775 * mxser_wait_until_sent() --- wait until the transmitter is empty
1776 */
1777static void mxser_wait_until_sent(struct tty_struct *tty, int timeout)
1778{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001779 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001780 unsigned long orig_jiffies, char_time;
1781 int lsr;
1782
1783 if (info->type == PORT_UNKNOWN)
1784 return;
1785
1786 if (info->xmit_fifo_size == 0)
1787 return; /* Just in case.... */
1788
1789 orig_jiffies = jiffies;
1790 /*
1791 * Set the check interval to be 1/5 of the estimated time to
1792 * send a single character, and make it at least 1. The check
1793 * interval should also be less than the timeout.
1794 *
1795 * Note: we have to use pretty tight timings here to satisfy
1796 * the NIST-PCTS.
1797 */
1798 char_time = (info->timeout - HZ / 50) / info->xmit_fifo_size;
1799 char_time = char_time / 5;
1800 if (char_time == 0)
1801 char_time = 1;
1802 if (timeout && timeout < char_time)
1803 char_time = timeout;
1804 /*
1805 * If the transmitter hasn't cleared in twice the approximate
1806 * amount of time to send the entire FIFO, it probably won't
1807 * ever clear. This assumes the UART isn't doing flow
1808 * control, which is currently the case. Hence, if it ever
1809 * takes longer than info->timeout, this is probably due to a
1810 * UART bug of some kind. So, we clamp the timeout parameter at
1811 * 2*info->timeout.
1812 */
1813 if (!timeout || timeout > 2 * info->timeout)
1814 timeout = 2 * info->timeout;
1815#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1816 printk(KERN_DEBUG "In rs_wait_until_sent(%d) check=%lu...",
1817 timeout, char_time);
1818 printk("jiff=%lu...", jiffies);
1819#endif
Jiri Slaby55b307d2006-12-08 02:38:14 -08001820 while (!((lsr = inb(info->ioaddr + UART_LSR)) & UART_LSR_TEMT)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001821#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1822 printk("lsr = %d (jiff=%lu)...", lsr, jiffies);
1823#endif
1824 schedule_timeout_interruptible(char_time);
1825 if (signal_pending(current))
1826 break;
1827 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1828 break;
1829 }
1830 set_current_state(TASK_RUNNING);
1831
1832#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1833 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
1834#endif
1835}
1836
1837
1838/*
1839 * This routine is called by tty_hangup() when a hangup is signaled.
1840 */
1841void mxser_hangup(struct tty_struct *tty)
1842{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001843 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001844
1845 mxser_flush_buffer(tty);
1846 mxser_shutdown(info);
1847 info->event = 0;
1848 info->count = 0;
1849 info->flags &= ~ASYNC_NORMAL_ACTIVE;
1850 info->tty = NULL;
1851 wake_up_interruptible(&info->open_wait);
1852}
1853
1854
1855/* added by James 03-12-2004. */
1856/*
1857 * mxser_rs_break() --- routine which turns the break handling on or off
1858 */
1859static void mxser_rs_break(struct tty_struct *tty, int break_state)
1860{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001861 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001862 unsigned long flags;
1863
1864 spin_lock_irqsave(&info->slock, flags);
1865 if (break_state == -1)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001866 outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
1867 info->ioaddr + UART_LCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08001868 else
Jiri Slaby55b307d2006-12-08 02:38:14 -08001869 outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
1870 info->ioaddr + UART_LCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08001871 spin_unlock_irqrestore(&info->slock, flags);
1872}
1873
1874/* (above) added by James. */
1875
1876
1877/*
1878 * This is the serial driver's generic interrupt routine
1879 */
1880static irqreturn_t mxser_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1881{
1882 int status, iir, i;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001883 struct mxser_board *brd = NULL;
1884 struct mxser_port *port;
Jiri Slaby037ad482006-12-08 02:38:11 -08001885 int max, irqbits, bits, msr;
1886 int pass_counter = 0;
Jiri Slaby3306ce32006-12-08 02:38:13 -08001887 unsigned int int_cnt;
Jiri Slaby037ad482006-12-08 02:38:11 -08001888 int handled = IRQ_NONE;
1889
Jiri Slaby037ad482006-12-08 02:38:11 -08001890 /* spin_lock(&gm_lock); */
1891
Jiri Slaby55b307d2006-12-08 02:38:14 -08001892 for (i = 0; i < MXSER_BOARDS; i++)
1893 if (dev_id == &mxser_boards[i]) {
1894 brd = dev_id;
Jiri Slaby037ad482006-12-08 02:38:11 -08001895 break;
1896 }
Jiri Slaby037ad482006-12-08 02:38:11 -08001897
1898 if (i == MXSER_BOARDS)
1899 goto irq_stop;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001900 if (brd == NULL)
Jiri Slaby037ad482006-12-08 02:38:11 -08001901 goto irq_stop;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001902 max = mxser_numports[brd->board_type - 1];
Jiri Slaby037ad482006-12-08 02:38:11 -08001903 while (1) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08001904 irqbits = inb(brd->vector) & brd->vector_mask;
1905 if (irqbits == brd->vector_mask)
Jiri Slaby037ad482006-12-08 02:38:11 -08001906 break;
1907
1908 handled = IRQ_HANDLED;
1909 for (i = 0, bits = 1; i < max; i++, irqbits |= bits, bits <<= 1) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08001910 if (irqbits == brd->vector_mask)
Jiri Slaby037ad482006-12-08 02:38:11 -08001911 break;
1912 if (bits & irqbits)
1913 continue;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001914 port = &brd->ports[i];
Jiri Slaby037ad482006-12-08 02:38:11 -08001915
Jiri Slaby3306ce32006-12-08 02:38:13 -08001916 int_cnt = 0;
1917 do {
1918 /* following add by Victor Yu. 09-13-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08001919 iir = inb(port->ioaddr + UART_IIR);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001920 if (iir & UART_IIR_NO_INT)
1921 break;
1922 iir &= MOXA_MUST_IIR_MASK;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001923 if (!port->tty) {
1924 status = inb(port->ioaddr + UART_LSR);
1925 outb(0x27, port->ioaddr + UART_FCR);
1926 inb(port->ioaddr + UART_MSR);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001927 break;
Jiri Slaby037ad482006-12-08 02:38:11 -08001928 }
Jiri Slaby037ad482006-12-08 02:38:11 -08001929 /* above add by Victor Yu. 09-13-2002 */
1930
Jiri Slaby55b307d2006-12-08 02:38:14 -08001931 spin_lock(&port->slock);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001932 /* following add by Victor Yu. 09-02-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08001933 status = inb(port->ioaddr + UART_LSR);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001934
1935 if (status & UART_LSR_PE)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001936 port->err_shadow |= NPPI_NOTIFY_PARITY;
Jiri Slaby3306ce32006-12-08 02:38:13 -08001937 if (status & UART_LSR_FE)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001938 port->err_shadow |= NPPI_NOTIFY_FRAMING;
Jiri Slaby3306ce32006-12-08 02:38:13 -08001939 if (status & UART_LSR_OE)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001940 port->err_shadow |=
Jiri Slaby3306ce32006-12-08 02:38:13 -08001941 NPPI_NOTIFY_HW_OVERRUN;
1942 if (status & UART_LSR_BI)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001943 port->err_shadow |= NPPI_NOTIFY_BREAK;
Jiri Slaby3306ce32006-12-08 02:38:13 -08001944
Jiri Slaby55b307d2006-12-08 02:38:14 -08001945 if (port->board->chip_flag) {
Jiri Slaby3306ce32006-12-08 02:38:13 -08001946 /*
1947 if ( (status & 0x02) && !(status & 0x01) ) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08001948 outb(port->ioaddr+UART_FCR, 0x23);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001949 continue;
1950 }
1951 */
1952 if (iir == MOXA_MUST_IIR_GDA ||
1953 iir == MOXA_MUST_IIR_RDA ||
1954 iir == MOXA_MUST_IIR_RTO ||
1955 iir == MOXA_MUST_IIR_LSR)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001956 mxser_receive_chars(port,
Jiri Slaby3306ce32006-12-08 02:38:13 -08001957 &status);
1958
1959 } else {
1960 /* above add by Victor Yu. 09-02-2002 */
1961
Jiri Slaby55b307d2006-12-08 02:38:14 -08001962 status &= port->read_status_mask;
Jiri Slaby3306ce32006-12-08 02:38:13 -08001963 if (status & UART_LSR_DR)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001964 mxser_receive_chars(port,
Jiri Slaby3306ce32006-12-08 02:38:13 -08001965 &status);
Jiri Slaby037ad482006-12-08 02:38:11 -08001966 }
Jiri Slaby55b307d2006-12-08 02:38:14 -08001967 msr = inb(port->ioaddr + UART_MSR);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001968 if (msr & UART_MSR_ANY_DELTA)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001969 mxser_check_modem_status(port, msr);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001970
1971 /* following add by Victor Yu. 09-13-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08001972 if (port->board->chip_flag) {
Jiri Slaby3306ce32006-12-08 02:38:13 -08001973 if (iir == 0x02 && (status &
1974 UART_LSR_THRE))
Jiri Slaby55b307d2006-12-08 02:38:14 -08001975 mxser_transmit_chars(port);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001976 } else {
1977 /* above add by Victor Yu. 09-13-2002 */
1978
1979 if (status & UART_LSR_THRE)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001980 mxser_transmit_chars(port);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001981 }
Jiri Slaby55b307d2006-12-08 02:38:14 -08001982 spin_unlock(&port->slock);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001983 } while (int_cnt++ < MXSER_ISR_PASS_LIMIT);
Jiri Slaby037ad482006-12-08 02:38:11 -08001984 }
Jiri Slaby3306ce32006-12-08 02:38:13 -08001985 if (pass_counter++ > MXSER_ISR_PASS_LIMIT)
Jiri Slaby037ad482006-12-08 02:38:11 -08001986 break; /* Prevent infinite loops */
Jiri Slaby037ad482006-12-08 02:38:11 -08001987 }
1988
1989 irq_stop:
1990 /* spin_unlock(&gm_lock); */
1991 return handled;
1992}
1993
Jiri Slaby55b307d2006-12-08 02:38:14 -08001994static void mxser_receive_chars(struct mxser_port *port, int *status)
Jiri Slaby037ad482006-12-08 02:38:11 -08001995{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001996 struct tty_struct *tty = port->tty;
Jiri Slaby037ad482006-12-08 02:38:11 -08001997 unsigned char ch, gdl;
1998 int ignored = 0;
1999 int cnt = 0;
2000 int recv_room;
2001 int max = 256;
2002 unsigned long flags;
2003
Jiri Slaby55b307d2006-12-08 02:38:14 -08002004 spin_lock_irqsave(&port->slock, flags);
Jiri Slaby037ad482006-12-08 02:38:11 -08002005
2006 recv_room = tty->receive_room;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002007 if ((recv_room == 0) && (!port->ldisc_stop_rx)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002008 /* mxser_throttle(tty); */
2009 mxser_stoprx(tty);
2010 /* return; */
2011 }
2012
2013 /* following add by Victor Yu. 09-02-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002014 if (port->board->chip_flag != MOXA_OTHER_UART) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002015
Jiri Slaby3306ce32006-12-08 02:38:13 -08002016 if (*status & UART_LSR_SPECIAL)
Jiri Slaby037ad482006-12-08 02:38:11 -08002017 goto intr_old;
Jiri Slaby037ad482006-12-08 02:38:11 -08002018 /* following add by Victor Yu. 02-11-2004 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002019 if (port->board->chip_flag == MOXA_MUST_MU860_HWID &&
Jiri Slaby037ad482006-12-08 02:38:11 -08002020 (*status & MOXA_MUST_LSR_RERR))
2021 goto intr_old;
2022 /* above add by Victor Yu. 02-14-2004 */
2023 if (*status & MOXA_MUST_LSR_RERR)
2024 goto intr_old;
2025
Jiri Slaby55b307d2006-12-08 02:38:14 -08002026 gdl = inb(port->ioaddr + MOXA_MUST_GDL_REGISTER);
Jiri Slaby037ad482006-12-08 02:38:11 -08002027
2028 /* add by Victor Yu. 02-11-2004 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002029 if (port->board->chip_flag == MOXA_MUST_MU150_HWID)
Jiri Slaby037ad482006-12-08 02:38:11 -08002030 gdl &= MOXA_MUST_GDL_MASK;
2031 if (gdl >= recv_room) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002032 if (!port->ldisc_stop_rx) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002033 /* mxser_throttle(tty); */
2034 mxser_stoprx(tty);
2035 }
2036 /* return; */
2037 }
2038 while (gdl--) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002039 ch = inb(port->ioaddr + UART_RX);
Jiri Slaby037ad482006-12-08 02:38:11 -08002040 tty_insert_flip_char(tty, ch, 0);
2041 cnt++;
Jiri Slaby037ad482006-12-08 02:38:11 -08002042 }
2043 goto end_intr;
2044 }
2045 intr_old:
2046 /* above add by Victor Yu. 09-02-2002 */
2047
2048 do {
2049 if (max-- < 0)
2050 break;
Jiri Slaby037ad482006-12-08 02:38:11 -08002051
Jiri Slaby55b307d2006-12-08 02:38:14 -08002052 ch = inb(port->ioaddr + UART_RX);
Jiri Slaby037ad482006-12-08 02:38:11 -08002053 /* following add by Victor Yu. 09-02-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002054 if (port->board->chip_flag && (*status & UART_LSR_OE)
Jiri Slaby3306ce32006-12-08 02:38:13 -08002055 /*&& !(*status&UART_LSR_DR) */)
Jiri Slaby55b307d2006-12-08 02:38:14 -08002056 outb(0x23, port->ioaddr + UART_FCR);
2057 *status &= port->read_status_mask;
Jiri Slaby037ad482006-12-08 02:38:11 -08002058 /* above add by Victor Yu. 09-02-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002059 if (*status & port->ignore_status_mask) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002060 if (++ignored > 100)
2061 break;
2062 } else {
2063 char flag = 0;
2064 if (*status & UART_LSR_SPECIAL) {
2065 if (*status & UART_LSR_BI) {
2066 flag = TTY_BREAK;
2067/* added by casper 1/11/2000 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002068 port->icount.brk++;
Jiri Slaby3306ce32006-12-08 02:38:13 -08002069
Jiri Slaby55b307d2006-12-08 02:38:14 -08002070 if (port->flags & ASYNC_SAK)
Jiri Slaby037ad482006-12-08 02:38:11 -08002071 do_SAK(tty);
2072 } else if (*status & UART_LSR_PE) {
2073 flag = TTY_PARITY;
2074/* added by casper 1/11/2000 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002075 port->icount.parity++;
Jiri Slaby037ad482006-12-08 02:38:11 -08002076 } else if (*status & UART_LSR_FE) {
2077 flag = TTY_FRAME;
2078/* added by casper 1/11/2000 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002079 port->icount.frame++;
Jiri Slaby037ad482006-12-08 02:38:11 -08002080 } else if (*status & UART_LSR_OE) {
2081 flag = TTY_OVERRUN;
2082/* added by casper 1/11/2000 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002083 port->icount.overrun++;
Jiri Slaby3306ce32006-12-08 02:38:13 -08002084 } else
2085 flags = TTY_BREAK;
2086 } else
2087 flags = 0;
Jiri Slaby037ad482006-12-08 02:38:11 -08002088 tty_insert_flip_char(tty, ch, flag);
2089 cnt++;
2090 if (cnt >= recv_room) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002091 if (!port->ldisc_stop_rx) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002092 /* mxser_throttle(tty); */
2093 mxser_stoprx(tty);
2094 }
2095 break;
2096 }
2097
2098 }
2099
2100 /* following add by Victor Yu. 09-02-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002101 if (port->board->chip_flag)
Jiri Slaby037ad482006-12-08 02:38:11 -08002102 break;
Jiri Slaby037ad482006-12-08 02:38:11 -08002103
2104 /* mask by Victor Yu. 09-02-2002
Jiri Slaby55b307d2006-12-08 02:38:14 -08002105 *status = inb(port->ioaddr + UART_LSR) & port->read_status_mask;
Jiri Slaby037ad482006-12-08 02:38:11 -08002106 */
2107 /* following add by Victor Yu. 09-02-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002108 *status = inb(port->ioaddr + UART_LSR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002109 /* above add by Victor Yu. 09-02-2002 */
2110 } while (*status & UART_LSR_DR);
2111
2112end_intr: /* add by Victor Yu. 09-02-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002113 mxvar_log.rxcnt[port->tty->index] += cnt;
2114 port->mon_data.rxcnt += cnt;
2115 port->mon_data.up_rxcnt += cnt;
2116 spin_unlock_irqrestore(&port->slock, flags);
Jiri Slaby037ad482006-12-08 02:38:11 -08002117
2118 tty_flip_buffer_push(tty);
2119}
2120
Jiri Slaby55b307d2006-12-08 02:38:14 -08002121static void mxser_transmit_chars(struct mxser_port *port)
Jiri Slaby037ad482006-12-08 02:38:11 -08002122{
2123 int count, cnt;
2124 unsigned long flags;
2125
Jiri Slaby55b307d2006-12-08 02:38:14 -08002126 spin_lock_irqsave(&port->slock, flags);
Jiri Slaby037ad482006-12-08 02:38:11 -08002127
Jiri Slaby55b307d2006-12-08 02:38:14 -08002128 if (port->x_char) {
2129 outb(port->x_char, port->ioaddr + UART_TX);
2130 port->x_char = 0;
2131 mxvar_log.txcnt[port->tty->index]++;
2132 port->mon_data.txcnt++;
2133 port->mon_data.up_txcnt++;
Jiri Slaby037ad482006-12-08 02:38:11 -08002134
2135/* added by casper 1/11/2000 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002136 port->icount.tx++;
Jiri Slaby3306ce32006-12-08 02:38:13 -08002137 goto unlock;
Jiri Slaby037ad482006-12-08 02:38:11 -08002138 }
2139
Jiri Slaby55b307d2006-12-08 02:38:14 -08002140 if (port->xmit_buf == 0)
Jiri Slaby3306ce32006-12-08 02:38:13 -08002141 goto unlock;
Jiri Slaby037ad482006-12-08 02:38:11 -08002142
Jiri Slaby55b307d2006-12-08 02:38:14 -08002143 if (port->xmit_cnt == 0) {
2144 if (port->xmit_cnt < WAKEUP_CHARS) { /* XXX what's this for?? */
2145 set_bit(MXSER_EVENT_TXLOW, &port->event);
2146 schedule_work(&port->tqueue);
Jiri Slaby3306ce32006-12-08 02:38:13 -08002147 }
2148 goto unlock;
2149 }
Jiri Slaby55b307d2006-12-08 02:38:14 -08002150 if (port->tty->stopped || (port->tty->hw_stopped &&
2151 (port->type != PORT_16550A) &&
2152 (!port->board->chip_flag))) {
2153 port->IER &= ~UART_IER_THRI;
2154 outb(port->IER, port->ioaddr + UART_IER);
Jiri Slaby3306ce32006-12-08 02:38:13 -08002155 goto unlock;
Jiri Slaby037ad482006-12-08 02:38:11 -08002156 }
2157
Jiri Slaby55b307d2006-12-08 02:38:14 -08002158 cnt = port->xmit_cnt;
2159 count = port->xmit_fifo_size;
Jiri Slaby037ad482006-12-08 02:38:11 -08002160 do {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002161 outb(port->xmit_buf[port->xmit_tail++],
2162 port->ioaddr + UART_TX);
2163 port->xmit_tail = port->xmit_tail & (SERIAL_XMIT_SIZE - 1);
2164 if (--port->xmit_cnt <= 0)
Jiri Slaby037ad482006-12-08 02:38:11 -08002165 break;
2166 } while (--count > 0);
Jiri Slaby55b307d2006-12-08 02:38:14 -08002167 mxvar_log.txcnt[port->tty->index] += (cnt - port->xmit_cnt);
Jiri Slaby037ad482006-12-08 02:38:11 -08002168
2169/* added by James 03-12-2004. */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002170 port->mon_data.txcnt += (cnt - port->xmit_cnt);
2171 port->mon_data.up_txcnt += (cnt - port->xmit_cnt);
Jiri Slaby037ad482006-12-08 02:38:11 -08002172
2173/* added by casper 1/11/2000 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002174 port->icount.tx += (cnt - port->xmit_cnt);
Jiri Slaby037ad482006-12-08 02:38:11 -08002175
Jiri Slaby55b307d2006-12-08 02:38:14 -08002176 if (port->xmit_cnt < WAKEUP_CHARS) {
2177 set_bit(MXSER_EVENT_TXLOW, &port->event);
2178 schedule_work(&port->tqueue);
Jiri Slaby037ad482006-12-08 02:38:11 -08002179 }
Jiri Slaby55b307d2006-12-08 02:38:14 -08002180 if (port->xmit_cnt <= 0) {
2181 port->IER &= ~UART_IER_THRI;
2182 outb(port->IER, port->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08002183 }
Jiri Slaby3306ce32006-12-08 02:38:13 -08002184unlock:
Jiri Slaby55b307d2006-12-08 02:38:14 -08002185 spin_unlock_irqrestore(&port->slock, flags);
Jiri Slaby037ad482006-12-08 02:38:11 -08002186}
2187
Jiri Slaby55b307d2006-12-08 02:38:14 -08002188static void mxser_check_modem_status(struct mxser_port *port, int status)
Jiri Slaby037ad482006-12-08 02:38:11 -08002189{
2190 /* update input line counters */
2191 if (status & UART_MSR_TERI)
Jiri Slaby55b307d2006-12-08 02:38:14 -08002192 port->icount.rng++;
Jiri Slaby037ad482006-12-08 02:38:11 -08002193 if (status & UART_MSR_DDSR)
Jiri Slaby55b307d2006-12-08 02:38:14 -08002194 port->icount.dsr++;
Jiri Slaby037ad482006-12-08 02:38:11 -08002195 if (status & UART_MSR_DDCD)
Jiri Slaby55b307d2006-12-08 02:38:14 -08002196 port->icount.dcd++;
Jiri Slaby037ad482006-12-08 02:38:11 -08002197 if (status & UART_MSR_DCTS)
Jiri Slaby55b307d2006-12-08 02:38:14 -08002198 port->icount.cts++;
2199 port->mon_data.modem_status = status;
2200 wake_up_interruptible(&port->delta_msr_wait);
Jiri Slaby037ad482006-12-08 02:38:11 -08002201
Jiri Slaby55b307d2006-12-08 02:38:14 -08002202 if ((port->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002203 if (status & UART_MSR_DCD)
Jiri Slaby55b307d2006-12-08 02:38:14 -08002204 wake_up_interruptible(&port->open_wait);
2205 schedule_work(&port->tqueue);
Jiri Slaby037ad482006-12-08 02:38:11 -08002206 }
2207
Jiri Slaby55b307d2006-12-08 02:38:14 -08002208 if (port->flags & ASYNC_CTS_FLOW) {
2209 if (port->tty->hw_stopped) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002210 if (status & UART_MSR_CTS) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002211 port->tty->hw_stopped = 0;
Jiri Slaby037ad482006-12-08 02:38:11 -08002212
Jiri Slaby55b307d2006-12-08 02:38:14 -08002213 if ((port->type != PORT_16550A) &&
2214 (!port->board->chip_flag)) {
2215 outb(port->IER & ~UART_IER_THRI,
2216 port->ioaddr + UART_IER);
2217 port->IER |= UART_IER_THRI;
2218 outb(port->IER, port->ioaddr +
2219 UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08002220 }
Jiri Slaby55b307d2006-12-08 02:38:14 -08002221 set_bit(MXSER_EVENT_TXLOW, &port->event);
2222 schedule_work(&port->tqueue);
Jiri Slaby3306ce32006-12-08 02:38:13 -08002223 }
Jiri Slaby037ad482006-12-08 02:38:11 -08002224 } else {
2225 if (!(status & UART_MSR_CTS)) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002226 port->tty->hw_stopped = 1;
2227 if (port->type != PORT_16550A &&
2228 !port->board->chip_flag) {
2229 port->IER &= ~UART_IER_THRI;
2230 outb(port->IER, port->ioaddr +
2231 UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08002232 }
2233 }
2234 }
2235 }
2236}
2237
Jiri Slaby55b307d2006-12-08 02:38:14 -08002238static int mxser_block_til_ready(struct tty_struct *tty, struct file *filp, struct mxser_port *port)
Jiri Slaby037ad482006-12-08 02:38:11 -08002239{
2240 DECLARE_WAITQUEUE(wait, current);
2241 int retval;
2242 int do_clocal = 0;
2243 unsigned long flags;
2244
2245 /*
2246 * If non-blocking mode is set, or the port is not enabled,
2247 * then make the check up front and then exit.
2248 */
2249 if ((filp->f_flags & O_NONBLOCK) || (tty->flags & (1 << TTY_IO_ERROR))) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002250 port->flags |= ASYNC_NORMAL_ACTIVE;
Jiri Slaby037ad482006-12-08 02:38:11 -08002251 return 0;
2252 }
2253
2254 if (tty->termios->c_cflag & CLOCAL)
2255 do_clocal = 1;
2256
2257 /*
2258 * Block waiting for the carrier detect and the line to become
2259 * free (i.e., not in use by the callout). While we are in
Jiri Slaby55b307d2006-12-08 02:38:14 -08002260 * this loop, port->count is dropped by one, so that
Jiri Slaby037ad482006-12-08 02:38:11 -08002261 * mxser_close() knows when to free things. We restore it upon
2262 * exit, either normal or abnormal.
2263 */
2264 retval = 0;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002265 add_wait_queue(&port->open_wait, &wait);
Jiri Slaby037ad482006-12-08 02:38:11 -08002266
Jiri Slaby55b307d2006-12-08 02:38:14 -08002267 spin_lock_irqsave(&port->slock, flags);
Jiri Slaby037ad482006-12-08 02:38:11 -08002268 if (!tty_hung_up_p(filp))
Jiri Slaby55b307d2006-12-08 02:38:14 -08002269 port->count--;
2270 spin_unlock_irqrestore(&port->slock, flags);
2271 port->blocked_open++;
Jiri Slaby037ad482006-12-08 02:38:11 -08002272 while (1) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002273 spin_lock_irqsave(&port->slock, flags);
2274 outb(inb(port->ioaddr + UART_MCR) |
2275 UART_MCR_DTR | UART_MCR_RTS, port->ioaddr + UART_MCR);
2276 spin_unlock_irqrestore(&port->slock, flags);
Jiri Slaby037ad482006-12-08 02:38:11 -08002277 set_current_state(TASK_INTERRUPTIBLE);
Jiri Slaby55b307d2006-12-08 02:38:14 -08002278 if (tty_hung_up_p(filp) || !(port->flags & ASYNC_INITIALIZED)) {
2279 if (port->flags & ASYNC_HUP_NOTIFY)
Jiri Slaby037ad482006-12-08 02:38:11 -08002280 retval = -EAGAIN;
2281 else
2282 retval = -ERESTARTSYS;
2283 break;
2284 }
Jiri Slaby55b307d2006-12-08 02:38:14 -08002285 if (!(port->flags & ASYNC_CLOSING) &&
Jiri Slaby037ad482006-12-08 02:38:11 -08002286 (do_clocal ||
Jiri Slaby55b307d2006-12-08 02:38:14 -08002287 (inb(port->ioaddr + UART_MSR) & UART_MSR_DCD)))
Jiri Slaby037ad482006-12-08 02:38:11 -08002288 break;
2289 if (signal_pending(current)) {
2290 retval = -ERESTARTSYS;
2291 break;
2292 }
2293 schedule();
2294 }
2295 set_current_state(TASK_RUNNING);
Jiri Slaby55b307d2006-12-08 02:38:14 -08002296 remove_wait_queue(&port->open_wait, &wait);
Jiri Slaby037ad482006-12-08 02:38:11 -08002297 if (!tty_hung_up_p(filp))
Jiri Slaby55b307d2006-12-08 02:38:14 -08002298 port->count++;
2299 port->blocked_open--;
Jiri Slaby037ad482006-12-08 02:38:11 -08002300 if (retval)
2301 return retval;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002302 port->flags |= ASYNC_NORMAL_ACTIVE;
Jiri Slaby037ad482006-12-08 02:38:11 -08002303 return 0;
2304}
2305
Jiri Slaby55b307d2006-12-08 02:38:14 -08002306static int mxser_startup(struct mxser_port *info)
Jiri Slaby037ad482006-12-08 02:38:11 -08002307{
2308 unsigned long page;
2309 unsigned long flags;
2310
2311 page = __get_free_page(GFP_KERNEL);
2312 if (!page)
2313 return -ENOMEM;
2314
2315 spin_lock_irqsave(&info->slock, flags);
2316
2317 if (info->flags & ASYNC_INITIALIZED) {
2318 free_page(page);
2319 spin_unlock_irqrestore(&info->slock, flags);
2320 return 0;
2321 }
2322
Jiri Slaby55b307d2006-12-08 02:38:14 -08002323 if (!info->ioaddr || !info->type) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002324 if (info->tty)
2325 set_bit(TTY_IO_ERROR, &info->tty->flags);
2326 free_page(page);
2327 spin_unlock_irqrestore(&info->slock, flags);
2328 return 0;
2329 }
2330 if (info->xmit_buf)
2331 free_page(page);
2332 else
2333 info->xmit_buf = (unsigned char *) page;
2334
2335 /*
2336 * Clear the FIFO buffers and disable them
2337 * (they will be reenabled in mxser_change_speed())
2338 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002339 if (info->board->chip_flag)
Jiri Slaby037ad482006-12-08 02:38:11 -08002340 outb((UART_FCR_CLEAR_RCVR |
2341 UART_FCR_CLEAR_XMIT |
Jiri Slaby55b307d2006-12-08 02:38:14 -08002342 MOXA_MUST_FCR_GDA_MODE_ENABLE), info->ioaddr + UART_FCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002343 else
2344 outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
Jiri Slaby55b307d2006-12-08 02:38:14 -08002345 info->ioaddr + UART_FCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002346
2347 /*
2348 * At this point there's no way the LSR could still be 0xFF;
2349 * if it is, then bail out, because there's likely no UART
2350 * here.
2351 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002352 if (inb(info->ioaddr + UART_LSR) == 0xff) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002353 spin_unlock_irqrestore(&info->slock, flags);
2354 if (capable(CAP_SYS_ADMIN)) {
2355 if (info->tty)
2356 set_bit(TTY_IO_ERROR, &info->tty->flags);
2357 return 0;
2358 } else
2359 return -ENODEV;
2360 }
2361
2362 /*
2363 * Clear the interrupt registers.
2364 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002365 (void) inb(info->ioaddr + UART_LSR);
2366 (void) inb(info->ioaddr + UART_RX);
2367 (void) inb(info->ioaddr + UART_IIR);
2368 (void) inb(info->ioaddr + UART_MSR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002369
2370 /*
2371 * Now, initialize the UART
2372 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002373 outb(UART_LCR_WLEN8, info->ioaddr + UART_LCR); /* reset DLAB */
Jiri Slaby037ad482006-12-08 02:38:11 -08002374 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002375 outb(info->MCR, info->ioaddr + UART_MCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002376
2377 /*
2378 * Finally, enable interrupts
2379 */
2380 info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
2381 /* info->IER = UART_IER_RLSI | UART_IER_RDI; */
2382
2383 /* following add by Victor Yu. 08-30-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002384 if (info->board->chip_flag)
Jiri Slaby037ad482006-12-08 02:38:11 -08002385 info->IER |= MOXA_MUST_IER_EGDAI;
2386 /* above add by Victor Yu. 08-30-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002387 outb(info->IER, info->ioaddr + UART_IER); /* enable interrupts */
Jiri Slaby037ad482006-12-08 02:38:11 -08002388
2389 /*
2390 * And clear the interrupt registers again for luck.
2391 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002392 (void) inb(info->ioaddr + UART_LSR);
2393 (void) inb(info->ioaddr + UART_RX);
2394 (void) inb(info->ioaddr + UART_IIR);
2395 (void) inb(info->ioaddr + UART_MSR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002396
2397 if (info->tty)
2398 clear_bit(TTY_IO_ERROR, &info->tty->flags);
2399 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
2400
2401 /*
2402 * and set the speed of the serial port
2403 */
2404 spin_unlock_irqrestore(&info->slock, flags);
2405 mxser_change_speed(info, NULL);
2406
2407 info->flags |= ASYNC_INITIALIZED;
2408 return 0;
2409}
2410
2411/*
2412 * This routine will shutdown a serial port; interrupts maybe disabled, and
2413 * DTR is dropped if the hangup on close termio flag is on.
2414 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002415static void mxser_shutdown(struct mxser_port *info)
Jiri Slaby037ad482006-12-08 02:38:11 -08002416{
2417 unsigned long flags;
2418
2419 if (!(info->flags & ASYNC_INITIALIZED))
2420 return;
2421
2422 spin_lock_irqsave(&info->slock, flags);
2423
2424 /*
2425 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
2426 * here so the queue might never be waken up
2427 */
2428 wake_up_interruptible(&info->delta_msr_wait);
2429
2430 /*
2431 * Free the IRQ, if necessary
2432 */
2433 if (info->xmit_buf) {
2434 free_page((unsigned long) info->xmit_buf);
2435 info->xmit_buf = NULL;
2436 }
2437
2438 info->IER = 0;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002439 outb(0x00, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08002440
2441 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
2442 info->MCR &= ~(UART_MCR_DTR | UART_MCR_RTS);
Jiri Slaby55b307d2006-12-08 02:38:14 -08002443 outb(info->MCR, info->ioaddr + UART_MCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002444
2445 /* clear Rx/Tx FIFO's */
2446 /* following add by Victor Yu. 08-30-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002447 if (info->board->chip_flag)
2448 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT |
2449 MOXA_MUST_FCR_GDA_MODE_ENABLE,
2450 info->ioaddr + UART_FCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002451 else
2452 /* above add by Victor Yu. 08-30-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002453 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
2454 info->ioaddr + UART_FCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002455
2456 /* read data port to reset things */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002457 (void) inb(info->ioaddr + UART_RX);
Jiri Slaby037ad482006-12-08 02:38:11 -08002458
2459 if (info->tty)
2460 set_bit(TTY_IO_ERROR, &info->tty->flags);
2461
2462 info->flags &= ~ASYNC_INITIALIZED;
2463
2464 /* following add by Victor Yu. 09-23-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002465 if (info->board->chip_flag)
2466 SET_MOXA_MUST_NO_SOFTWARE_FLOW_CONTROL(info->ioaddr);
Jiri Slaby037ad482006-12-08 02:38:11 -08002467 /* above add by Victor Yu. 09-23-2002 */
2468
2469 spin_unlock_irqrestore(&info->slock, flags);
2470}
2471
2472/*
2473 * This routine is called to set the UART divisor registers to match
2474 * the specified baud rate for a serial port.
2475 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002476static int mxser_change_speed(struct mxser_port *info,
2477 struct termios *old_termios)
Jiri Slaby037ad482006-12-08 02:38:11 -08002478{
2479 unsigned cflag, cval, fcr;
2480 int ret = 0;
2481 unsigned char status;
2482 long baud;
2483 unsigned long flags;
2484
2485 if (!info->tty || !info->tty->termios)
2486 return ret;
2487 cflag = info->tty->termios->c_cflag;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002488 if (!(info->ioaddr))
Jiri Slaby037ad482006-12-08 02:38:11 -08002489 return ret;
2490
2491#ifndef B921600
2492#define B921600 (B460800 +1)
2493#endif
Jiri Slaby55b307d2006-12-08 02:38:14 -08002494 if (mxser_set_baud_method[info->tty->index] == 0) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002495 baud = tty_get_baud_rate(info->tty);
2496 mxser_set_baud(info, baud);
2497 }
2498
2499 /* byte size and parity */
2500 switch (cflag & CSIZE) {
2501 case CS5:
2502 cval = 0x00;
2503 break;
2504 case CS6:
2505 cval = 0x01;
2506 break;
2507 case CS7:
2508 cval = 0x02;
2509 break;
2510 case CS8:
2511 cval = 0x03;
2512 break;
2513 default:
2514 cval = 0x00;
2515 break; /* too keep GCC shut... */
2516 }
2517 if (cflag & CSTOPB)
2518 cval |= 0x04;
2519 if (cflag & PARENB)
2520 cval |= UART_LCR_PARITY;
2521 if (!(cflag & PARODD))
2522 cval |= UART_LCR_EPAR;
2523 if (cflag & CMSPAR)
2524 cval |= UART_LCR_SPAR;
2525
2526 if ((info->type == PORT_8250) || (info->type == PORT_16450)) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002527 if (info->board->chip_flag) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002528 fcr = UART_FCR_ENABLE_FIFO;
2529 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
2530 SET_MOXA_MUST_FIFO_VALUE(info);
2531 } else
2532 fcr = 0;
2533 } else {
2534 fcr = UART_FCR_ENABLE_FIFO;
2535 /* following add by Victor Yu. 08-30-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002536 if (info->board->chip_flag) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002537 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
2538 SET_MOXA_MUST_FIFO_VALUE(info);
2539 } else {
2540 /* above add by Victor Yu. 08-30-2002 */
2541 switch (info->rx_trigger) {
2542 case 1:
2543 fcr |= UART_FCR_TRIGGER_1;
2544 break;
2545 case 4:
2546 fcr |= UART_FCR_TRIGGER_4;
2547 break;
2548 case 8:
2549 fcr |= UART_FCR_TRIGGER_8;
2550 break;
2551 default:
2552 fcr |= UART_FCR_TRIGGER_14;
2553 break;
2554 }
2555 }
2556 }
2557
2558 /* CTS flow control flag and modem status interrupts */
2559 info->IER &= ~UART_IER_MSI;
2560 info->MCR &= ~UART_MCR_AFE;
2561 if (cflag & CRTSCTS) {
2562 info->flags |= ASYNC_CTS_FLOW;
2563 info->IER |= UART_IER_MSI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002564 if ((info->type == PORT_16550A) || (info->board->chip_flag)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002565 info->MCR |= UART_MCR_AFE;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002566/* status = mxser_get_msr(info->ioaddr, 0, info->port); */
Jiri Slaby037ad482006-12-08 02:38:11 -08002567/*
2568 save_flags(flags);
2569 cli();
2570 status = inb(baseaddr + UART_MSR);
2571 restore_flags(flags);
2572*/
2573 /* mxser_check_modem_status(info, status); */
2574 } else {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002575/* status = mxser_get_msr(info->ioaddr, 0, info->port); */
Jiri Slaby037ad482006-12-08 02:38:11 -08002576 /* MX_LOCK(&info->slock); */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002577 status = inb(info->ioaddr + UART_MSR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002578 /* MX_UNLOCK(&info->slock); */
2579 if (info->tty->hw_stopped) {
2580 if (status & UART_MSR_CTS) {
2581 info->tty->hw_stopped = 0;
Jiri Slaby3306ce32006-12-08 02:38:13 -08002582 if (info->type != PORT_16550A &&
Jiri Slaby55b307d2006-12-08 02:38:14 -08002583 !info->board->chip_flag) {
Jiri Slaby3306ce32006-12-08 02:38:13 -08002584 outb(info->IER & ~UART_IER_THRI,
Jiri Slaby55b307d2006-12-08 02:38:14 -08002585 info->ioaddr +
2586 UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08002587 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002588 outb(info->IER, info->ioaddr +
2589 UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08002590 }
2591 set_bit(MXSER_EVENT_TXLOW, &info->event);
2592 schedule_work(&info->tqueue); }
2593 } else {
2594 if (!(status & UART_MSR_CTS)) {
2595 info->tty->hw_stopped = 1;
2596 if ((info->type != PORT_16550A) &&
Jiri Slaby55b307d2006-12-08 02:38:14 -08002597 (!info->board->chip_flag)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002598 info->IER &= ~UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002599 outb(info->IER, info->ioaddr +
2600 UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08002601 }
2602 }
2603 }
2604 }
2605 } else {
2606 info->flags &= ~ASYNC_CTS_FLOW;
2607 }
Jiri Slaby55b307d2006-12-08 02:38:14 -08002608 outb(info->MCR, info->ioaddr + UART_MCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002609 if (cflag & CLOCAL) {
2610 info->flags &= ~ASYNC_CHECK_CD;
2611 } else {
2612 info->flags |= ASYNC_CHECK_CD;
2613 info->IER |= UART_IER_MSI;
2614 }
Jiri Slaby55b307d2006-12-08 02:38:14 -08002615 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08002616
2617 /*
2618 * Set up parity check flag
2619 */
2620 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
2621 if (I_INPCK(info->tty))
2622 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
2623 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
2624 info->read_status_mask |= UART_LSR_BI;
2625
2626 info->ignore_status_mask = 0;
2627
2628 if (I_IGNBRK(info->tty)) {
2629 info->ignore_status_mask |= UART_LSR_BI;
2630 info->read_status_mask |= UART_LSR_BI;
2631 /*
2632 * If we're ignore parity and break indicators, ignore
2633 * overruns too. (For real raw support).
2634 */
2635 if (I_IGNPAR(info->tty)) {
2636 info->ignore_status_mask |=
2637 UART_LSR_OE |
2638 UART_LSR_PE |
2639 UART_LSR_FE;
2640 info->read_status_mask |=
2641 UART_LSR_OE |
2642 UART_LSR_PE |
2643 UART_LSR_FE;
2644 }
2645 }
2646 /* following add by Victor Yu. 09-02-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002647 if (info->board->chip_flag) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002648 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby55b307d2006-12-08 02:38:14 -08002649 SET_MOXA_MUST_XON1_VALUE(info->ioaddr, START_CHAR(info->tty));
2650 SET_MOXA_MUST_XOFF1_VALUE(info->ioaddr, STOP_CHAR(info->tty));
Jiri Slaby037ad482006-12-08 02:38:11 -08002651 if (I_IXON(info->tty)) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002652 ENABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
Jiri Slaby037ad482006-12-08 02:38:11 -08002653 } else {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002654 DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
Jiri Slaby037ad482006-12-08 02:38:11 -08002655 }
2656 if (I_IXOFF(info->tty)) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002657 ENABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
Jiri Slaby037ad482006-12-08 02:38:11 -08002658 } else {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002659 DISABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
Jiri Slaby037ad482006-12-08 02:38:11 -08002660 }
2661 /*
2662 if ( I_IXANY(info->tty) ) {
2663 info->MCR |= MOXA_MUST_MCR_XON_ANY;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002664 ENABLE_MOXA_MUST_XON_ANY_FLOW_CONTROL(info->ioaddr);
Jiri Slaby037ad482006-12-08 02:38:11 -08002665 } else {
2666 info->MCR &= ~MOXA_MUST_MCR_XON_ANY;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002667 DISABLE_MOXA_MUST_XON_ANY_FLOW_CONTROL(info->ioaddr);
Jiri Slaby037ad482006-12-08 02:38:11 -08002668 }
2669 */
2670 spin_unlock_irqrestore(&info->slock, flags);
2671 }
2672 /* above add by Victor Yu. 09-02-2002 */
2673
2674
Jiri Slaby55b307d2006-12-08 02:38:14 -08002675 outb(fcr, info->ioaddr + UART_FCR); /* set fcr */
2676 outb(cval, info->ioaddr + UART_LCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002677
2678 return ret;
2679}
2680
2681
Jiri Slaby55b307d2006-12-08 02:38:14 -08002682static int mxser_set_baud(struct mxser_port *info, long newspd)
Jiri Slaby037ad482006-12-08 02:38:11 -08002683{
2684 int quot = 0;
2685 unsigned char cval;
2686 int ret = 0;
2687 unsigned long flags;
2688
2689 if (!info->tty || !info->tty->termios)
2690 return ret;
2691
Jiri Slaby55b307d2006-12-08 02:38:14 -08002692 if (!(info->ioaddr))
Jiri Slaby037ad482006-12-08 02:38:11 -08002693 return ret;
2694
Jiri Slaby55b307d2006-12-08 02:38:14 -08002695 if (newspd > info->max_baud)
Jiri Slaby037ad482006-12-08 02:38:11 -08002696 return 0;
2697
2698 info->realbaud = newspd;
2699 if (newspd == 134) {
2700 quot = (2 * info->baud_base / 269);
2701 } else if (newspd) {
2702 quot = info->baud_base / newspd;
2703 if (quot == 0)
2704 quot = 1;
2705 } else {
2706 quot = 0;
2707 }
2708
2709 info->timeout = ((info->xmit_fifo_size * HZ * 10 * quot) / info->baud_base);
2710 info->timeout += HZ / 50; /* Add .02 seconds of slop */
2711
2712 if (quot) {
2713 spin_lock_irqsave(&info->slock, flags);
2714 info->MCR |= UART_MCR_DTR;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002715 outb(info->MCR, info->ioaddr + UART_MCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002716 spin_unlock_irqrestore(&info->slock, flags);
2717 } else {
2718 spin_lock_irqsave(&info->slock, flags);
2719 info->MCR &= ~UART_MCR_DTR;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002720 outb(info->MCR, info->ioaddr + UART_MCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002721 spin_unlock_irqrestore(&info->slock, flags);
2722 return ret;
2723 }
2724
Jiri Slaby55b307d2006-12-08 02:38:14 -08002725 cval = inb(info->ioaddr + UART_LCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002726
Jiri Slaby55b307d2006-12-08 02:38:14 -08002727 outb(cval | UART_LCR_DLAB, info->ioaddr + UART_LCR); /* set DLAB */
Jiri Slaby037ad482006-12-08 02:38:11 -08002728
Jiri Slaby55b307d2006-12-08 02:38:14 -08002729 outb(quot & 0xff, info->ioaddr + UART_DLL); /* LS of divisor */
2730 outb(quot >> 8, info->ioaddr + UART_DLM); /* MS of divisor */
2731 outb(cval, info->ioaddr + UART_LCR); /* reset DLAB */
Jiri Slaby037ad482006-12-08 02:38:11 -08002732
2733
2734 return ret;
2735}
2736
2737/*
2738 * ------------------------------------------------------------
2739 * friends of mxser_ioctl()
2740 * ------------------------------------------------------------
2741 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002742static int mxser_get_serial_info(struct mxser_port *info,
2743 struct serial_struct __user *retinfo)
Jiri Slaby037ad482006-12-08 02:38:11 -08002744{
2745 struct serial_struct tmp;
2746
2747 if (!retinfo)
2748 return -EFAULT;
2749 memset(&tmp, 0, sizeof(tmp));
2750 tmp.type = info->type;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002751 tmp.line = info->tty->index;
2752 tmp.port = info->ioaddr;
2753 tmp.irq = info->board->irq;
Jiri Slaby037ad482006-12-08 02:38:11 -08002754 tmp.flags = info->flags;
2755 tmp.baud_base = info->baud_base;
2756 tmp.close_delay = info->close_delay;
2757 tmp.closing_wait = info->closing_wait;
2758 tmp.custom_divisor = info->custom_divisor;
2759 tmp.hub6 = 0;
2760 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2761 return -EFAULT;
2762 return 0;
2763}
2764
Jiri Slaby55b307d2006-12-08 02:38:14 -08002765static int mxser_set_serial_info(struct mxser_port *info,
2766 struct serial_struct __user *new_info)
Jiri Slaby037ad482006-12-08 02:38:11 -08002767{
2768 struct serial_struct new_serial;
2769 unsigned int flags;
2770 int retval = 0;
2771
Jiri Slaby55b307d2006-12-08 02:38:14 -08002772 if (!new_info || !info->ioaddr)
Jiri Slaby037ad482006-12-08 02:38:11 -08002773 return -EFAULT;
2774 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
2775 return -EFAULT;
2776
Jiri Slaby55b307d2006-12-08 02:38:14 -08002777 if ((new_serial.irq != info->board->irq) ||
2778 (new_serial.port != info->ioaddr) ||
Jiri Slaby037ad482006-12-08 02:38:11 -08002779 (new_serial.custom_divisor != info->custom_divisor) ||
2780 (new_serial.baud_base != info->baud_base))
2781 return -EPERM;
2782
2783 flags = info->flags & ASYNC_SPD_MASK;
2784
2785 if (!capable(CAP_SYS_ADMIN)) {
2786 if ((new_serial.baud_base != info->baud_base) ||
2787 (new_serial.close_delay != info->close_delay) ||
2788 ((new_serial.flags & ~ASYNC_USR_MASK) != (info->flags & ~ASYNC_USR_MASK)))
2789 return -EPERM;
2790 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
2791 (new_serial.flags & ASYNC_USR_MASK));
2792 } else {
2793 /*
2794 * OK, past this point, all the error checking has been done.
2795 * At this point, we start making changes.....
2796 */
2797 info->flags = ((info->flags & ~ASYNC_FLAGS) |
2798 (new_serial.flags & ASYNC_FLAGS));
2799 info->close_delay = new_serial.close_delay * HZ / 100;
2800 info->closing_wait = new_serial.closing_wait * HZ / 100;
2801 info->tty->low_latency =
2802 (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
2803 info->tty->low_latency = 0; /* (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0; */
2804 }
2805
2806 /* added by casper, 3/17/2000, for mouse */
2807 info->type = new_serial.type;
2808
2809 process_txrx_fifo(info);
2810
2811 if (info->flags & ASYNC_INITIALIZED) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002812 if (flags != (info->flags & ASYNC_SPD_MASK))
Jiri Slaby037ad482006-12-08 02:38:11 -08002813 mxser_change_speed(info, NULL);
Jiri Slaby55b307d2006-12-08 02:38:14 -08002814 } else
Jiri Slaby037ad482006-12-08 02:38:11 -08002815 retval = mxser_startup(info);
Jiri Slaby55b307d2006-12-08 02:38:14 -08002816
Jiri Slaby037ad482006-12-08 02:38:11 -08002817 return retval;
2818}
2819
2820/*
2821 * mxser_get_lsr_info - get line status register info
2822 *
2823 * Purpose: Let user call ioctl() to get info when the UART physically
2824 * is emptied. On bus types like RS485, the transmitter must
2825 * release the bus after transmitting. This must be done when
2826 * the transmit shift register is empty, not be done when the
2827 * transmit holding register is empty. This functionality
2828 * allows an RS485 driver to be written in user space.
2829 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002830static int mxser_get_lsr_info(struct mxser_port *info,
2831 unsigned int __user *value)
Jiri Slaby037ad482006-12-08 02:38:11 -08002832{
2833 unsigned char status;
2834 unsigned int result;
2835 unsigned long flags;
2836
2837 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby55b307d2006-12-08 02:38:14 -08002838 status = inb(info->ioaddr + UART_LSR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002839 spin_unlock_irqrestore(&info->slock, flags);
2840 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
2841 return put_user(result, value);
2842}
2843
2844/*
2845 * This routine sends a break character out the serial port.
2846 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002847static void mxser_send_break(struct mxser_port *info, int duration)
Jiri Slaby037ad482006-12-08 02:38:11 -08002848{
2849 unsigned long flags;
2850
Jiri Slaby55b307d2006-12-08 02:38:14 -08002851 if (!info->ioaddr)
Jiri Slaby037ad482006-12-08 02:38:11 -08002852 return;
2853 set_current_state(TASK_INTERRUPTIBLE);
2854 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby55b307d2006-12-08 02:38:14 -08002855 outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
2856 info->ioaddr + UART_LCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002857 spin_unlock_irqrestore(&info->slock, flags);
2858 schedule_timeout(duration);
2859 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby55b307d2006-12-08 02:38:14 -08002860 outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
2861 info->ioaddr + UART_LCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002862 spin_unlock_irqrestore(&info->slock, flags);
2863}
2864
2865static int mxser_tiocmget(struct tty_struct *tty, struct file *file)
2866{
Jiri Slaby55b307d2006-12-08 02:38:14 -08002867 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08002868 unsigned char control, status;
2869 unsigned long flags;
2870
2871
2872 if (tty->index == MXSER_PORTS)
2873 return -ENOIOCTLCMD;
2874 if (tty->flags & (1 << TTY_IO_ERROR))
2875 return -EIO;
2876
2877 control = info->MCR;
2878
2879 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby55b307d2006-12-08 02:38:14 -08002880 status = inb(info->ioaddr + UART_MSR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002881 if (status & UART_MSR_ANY_DELTA)
2882 mxser_check_modem_status(info, status);
2883 spin_unlock_irqrestore(&info->slock, flags);
2884 return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0) |
2885 ((control & UART_MCR_DTR) ? TIOCM_DTR : 0) |
2886 ((status & UART_MSR_DCD) ? TIOCM_CAR : 0) |
2887 ((status & UART_MSR_RI) ? TIOCM_RNG : 0) |
2888 ((status & UART_MSR_DSR) ? TIOCM_DSR : 0) |
2889 ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
2890}
2891
Jiri Slaby55b307d2006-12-08 02:38:14 -08002892static int mxser_tiocmset(struct tty_struct *tty, struct file *file,
2893 unsigned int set, unsigned int clear)
Jiri Slaby037ad482006-12-08 02:38:11 -08002894{
Jiri Slaby55b307d2006-12-08 02:38:14 -08002895 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08002896 unsigned long flags;
2897
2898
2899 if (tty->index == MXSER_PORTS)
2900 return -ENOIOCTLCMD;
2901 if (tty->flags & (1 << TTY_IO_ERROR))
2902 return -EIO;
2903
2904 spin_lock_irqsave(&info->slock, flags);
2905
2906 if (set & TIOCM_RTS)
2907 info->MCR |= UART_MCR_RTS;
2908 if (set & TIOCM_DTR)
2909 info->MCR |= UART_MCR_DTR;
2910
2911 if (clear & TIOCM_RTS)
2912 info->MCR &= ~UART_MCR_RTS;
2913 if (clear & TIOCM_DTR)
2914 info->MCR &= ~UART_MCR_DTR;
2915
Jiri Slaby55b307d2006-12-08 02:38:14 -08002916 outb(info->MCR, info->ioaddr + UART_MCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002917 spin_unlock_irqrestore(&info->slock, flags);
2918 return 0;
2919}
2920
2921
2922static int mxser_read_register(int, unsigned short *);
2923static int mxser_program_mode(int);
2924static void mxser_normal_mode(int);
2925
Jiri Slaby943f2952006-12-08 02:38:15 -08002926static int __init mxser_get_ISA_conf(int cap, struct mxser_board *brd)
Jiri Slaby037ad482006-12-08 02:38:11 -08002927{
2928 int id, i, bits;
2929 unsigned short regs[16], irq;
2930 unsigned char scratch, scratch2;
2931
Jiri Slaby55b307d2006-12-08 02:38:14 -08002932 brd->chip_flag = MOXA_OTHER_UART;
Jiri Slaby037ad482006-12-08 02:38:11 -08002933
2934 id = mxser_read_register(cap, regs);
2935 if (id == C168_ASIC_ID) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002936 brd->board_type = MXSER_BOARD_C168_ISA;
2937 brd->nports = 8;
Jiri Slaby037ad482006-12-08 02:38:11 -08002938 } else if (id == C104_ASIC_ID) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002939 brd->board_type = MXSER_BOARD_C104_ISA;
2940 brd->nports = 4;
Jiri Slaby037ad482006-12-08 02:38:11 -08002941 } else if (id == C102_ASIC_ID) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002942 brd->board_type = MXSER_BOARD_C102_ISA;
2943 brd->nports = 2;
Jiri Slaby037ad482006-12-08 02:38:11 -08002944 } else if (id == CI132_ASIC_ID) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002945 brd->board_type = MXSER_BOARD_CI132;
2946 brd->nports = 2;
Jiri Slaby037ad482006-12-08 02:38:11 -08002947 } else if (id == CI134_ASIC_ID) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002948 brd->board_type = MXSER_BOARD_CI134;
2949 brd->nports = 4;
Jiri Slaby037ad482006-12-08 02:38:11 -08002950 } else if (id == CI104J_ASIC_ID) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002951 brd->board_type = MXSER_BOARD_CI104J;
2952 brd->nports = 4;
Jiri Slaby037ad482006-12-08 02:38:11 -08002953 } else
2954 return 0;
2955
2956 irq = 0;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002957 if (brd->nports == 2) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002958 irq = regs[9] & 0xF000;
2959 irq = irq | (irq >> 4);
2960 if (irq != (regs[9] & 0xFF00))
2961 return MXSER_ERR_IRQ_CONFLIT;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002962 } else if (brd->nports == 4) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002963 irq = regs[9] & 0xF000;
2964 irq = irq | (irq >> 4);
2965 irq = irq | (irq >> 8);
2966 if (irq != regs[9])
2967 return MXSER_ERR_IRQ_CONFLIT;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002968 } else if (brd->nports == 8) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002969 irq = regs[9] & 0xF000;
2970 irq = irq | (irq >> 4);
2971 irq = irq | (irq >> 8);
2972 if ((irq != regs[9]) || (irq != regs[10]))
2973 return MXSER_ERR_IRQ_CONFLIT;
2974 }
2975
2976 if (!irq)
2977 return MXSER_ERR_IRQ;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002978 brd->irq = ((int)(irq & 0xF000) >> 12);
Jiri Slaby037ad482006-12-08 02:38:11 -08002979 for (i = 0; i < 8; i++)
Jiri Slaby55b307d2006-12-08 02:38:14 -08002980 brd->ports[i].ioaddr = (int) regs[i + 1] & 0xFFF8;
Jiri Slaby037ad482006-12-08 02:38:11 -08002981 if ((regs[12] & 0x80) == 0)
2982 return MXSER_ERR_VECTOR;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002983 brd->vector = (int)regs[11]; /* interrupt vector */
Jiri Slaby037ad482006-12-08 02:38:11 -08002984 if (id == 1)
Jiri Slaby55b307d2006-12-08 02:38:14 -08002985 brd->vector_mask = 0x00FF;
Jiri Slaby037ad482006-12-08 02:38:11 -08002986 else
Jiri Slaby55b307d2006-12-08 02:38:14 -08002987 brd->vector_mask = 0x000F;
Jiri Slaby037ad482006-12-08 02:38:11 -08002988 for (i = 7, bits = 0x0100; i >= 0; i--, bits <<= 1) {
2989 if (regs[12] & bits) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002990 brd->ports[i].baud_base = 921600;
2991 brd->ports[i].max_baud = 921600; /* add by Victor Yu. 09-04-2002 */
Jiri Slaby037ad482006-12-08 02:38:11 -08002992 } else {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002993 brd->ports[i].baud_base = 115200;
2994 brd->ports[i].max_baud = 115200; /* add by Victor Yu. 09-04-2002 */
Jiri Slaby037ad482006-12-08 02:38:11 -08002995 }
2996 }
2997 scratch2 = inb(cap + UART_LCR) & (~UART_LCR_DLAB);
2998 outb(scratch2 | UART_LCR_DLAB, cap + UART_LCR);
2999 outb(0, cap + UART_EFR); /* EFR is the same as FCR */
3000 outb(scratch2, cap + UART_LCR);
3001 outb(UART_FCR_ENABLE_FIFO, cap + UART_FCR);
3002 scratch = inb(cap + UART_IIR);
3003
3004 if (scratch & 0xC0)
Jiri Slaby55b307d2006-12-08 02:38:14 -08003005 brd->uart_type = PORT_16550A;
Jiri Slaby037ad482006-12-08 02:38:11 -08003006 else
Jiri Slaby55b307d2006-12-08 02:38:14 -08003007 brd->uart_type = PORT_16450;
Jiri Slaby037ad482006-12-08 02:38:11 -08003008 if (id == 1)
Jiri Slaby55b307d2006-12-08 02:38:14 -08003009 brd->nports = 8;
Jiri Slaby037ad482006-12-08 02:38:11 -08003010 else
Jiri Slaby55b307d2006-12-08 02:38:14 -08003011 brd->nports = 4;
3012 request_region(brd->ports[0].ioaddr, 8 * brd->nports, "mxser(IO)");
3013 request_region(brd->vector, 1, "mxser(vector)");
3014 return brd->nports;
Jiri Slaby037ad482006-12-08 02:38:11 -08003015}
3016
3017#define CHIP_SK 0x01 /* Serial Data Clock in Eprom */
3018#define CHIP_DO 0x02 /* Serial Data Output in Eprom */
3019#define CHIP_CS 0x04 /* Serial Chip Select in Eprom */
3020#define CHIP_DI 0x08 /* Serial Data Input in Eprom */
3021#define EN_CCMD 0x000 /* Chip's command register */
3022#define EN0_RSARLO 0x008 /* Remote start address reg 0 */
3023#define EN0_RSARHI 0x009 /* Remote start address reg 1 */
3024#define EN0_RCNTLO 0x00A /* Remote byte count reg WR */
3025#define EN0_RCNTHI 0x00B /* Remote byte count reg WR */
3026#define EN0_DCFG 0x00E /* Data configuration reg WR */
3027#define EN0_PORT 0x010 /* Rcv missed frame error counter RD */
3028#define ENC_PAGE0 0x000 /* Select page 0 of chip registers */
3029#define ENC_PAGE3 0x0C0 /* Select page 3 of chip registers */
3030static int mxser_read_register(int port, unsigned short *regs)
3031{
3032 int i, k, value, id;
3033 unsigned int j;
3034
3035 id = mxser_program_mode(port);
3036 if (id < 0)
3037 return id;
3038 for (i = 0; i < 14; i++) {
3039 k = (i & 0x3F) | 0x180;
3040 for (j = 0x100; j > 0; j >>= 1) {
3041 outb(CHIP_CS, port);
3042 if (k & j) {
3043 outb(CHIP_CS | CHIP_DO, port);
3044 outb(CHIP_CS | CHIP_DO | CHIP_SK, port); /* A? bit of read */
3045 } else {
3046 outb(CHIP_CS, port);
3047 outb(CHIP_CS | CHIP_SK, port); /* A? bit of read */
3048 }
3049 }
3050 (void)inb(port);
3051 value = 0;
3052 for (k = 0, j = 0x8000; k < 16; k++, j >>= 1) {
3053 outb(CHIP_CS, port);
3054 outb(CHIP_CS | CHIP_SK, port);
3055 if (inb(port) & CHIP_DI)
3056 value |= j;
3057 }
3058 regs[i] = value;
3059 outb(0, port);
3060 }
3061 mxser_normal_mode(port);
3062 return id;
3063}
3064
3065static int mxser_program_mode(int port)
3066{
3067 int id, i, j, n;
3068 /* unsigned long flags; */
3069
3070 spin_lock(&gm_lock);
3071 outb(0, port);
3072 outb(0, port);
3073 outb(0, port);
3074 (void)inb(port);
3075 (void)inb(port);
3076 outb(0, port);
3077 (void)inb(port);
3078 /* restore_flags(flags); */
3079 spin_unlock(&gm_lock);
3080
3081 id = inb(port + 1) & 0x1F;
3082 if ((id != C168_ASIC_ID) &&
3083 (id != C104_ASIC_ID) &&
3084 (id != C102_ASIC_ID) &&
3085 (id != CI132_ASIC_ID) &&
3086 (id != CI134_ASIC_ID) &&
3087 (id != CI104J_ASIC_ID))
3088 return -1;
3089 for (i = 0, j = 0; i < 4; i++) {
3090 n = inb(port + 2);
3091 if (n == 'M') {
3092 j = 1;
3093 } else if ((j == 1) && (n == 1)) {
3094 j = 2;
3095 break;
3096 } else
3097 j = 0;
3098 }
3099 if (j != 2)
3100 id = -2;
3101 return id;
3102}
3103
3104static void mxser_normal_mode(int port)
3105{
3106 int i, n;
3107
3108 outb(0xA5, port + 1);
3109 outb(0x80, port + 3);
3110 outb(12, port + 0); /* 9600 bps */
3111 outb(0, port + 1);
3112 outb(0x03, port + 3); /* 8 data bits */
3113 outb(0x13, port + 4); /* loop back mode */
3114 for (i = 0; i < 16; i++) {
3115 n = inb(port + 5);
3116 if ((n & 0x61) == 0x60)
3117 break;
3118 if ((n & 1) == 1)
3119 (void)inb(port);
3120 }
3121 outb(0x00, port + 4);
3122}
3123
3124module_init(mxser_module_init);
3125module_exit(mxser_module_exit);