blob: 2dc49be144e64b8821f0edd70adb62b5d20fb5f4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * mxser.c -- MOXA Smartio/Industio family multiport serial driver.
3 *
4 * Copyright (C) 1999-2001 Moxa Technologies (support@moxa.com.tw).
5 *
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
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -070012 * (at your option) any later version.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
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 *
23 * Original release 10/26/00
24 *
25 * 02/06/01 Support MOXA Industio family boards.
26 * 02/06/01 Support TIOCGICOUNT.
27 * 02/06/01 Fix the problem for connecting to serial mouse.
28 * 02/06/01 Fix the problem for H/W flow control.
29 * 02/06/01 Fix the compling warning when CONFIG_PCI
30 * don't be defined.
31 *
32 * Fed through a cleanup, indent and remove of non 2.6 code by Alan Cox
33 * <alan@redhat.com>. The original 1.8 code is available on www.moxa.com.
34 * - Fixed x86_64 cleanness
35 * - Fixed sleep with spinlock held in mxser_send_break
36 */
37
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/autoconf.h>
41#include <linux/errno.h>
42#include <linux/signal.h>
43#include <linux/sched.h>
44#include <linux/timer.h>
45#include <linux/interrupt.h>
46#include <linux/tty.h>
47#include <linux/tty_flip.h>
48#include <linux/serial.h>
49#include <linux/serial_reg.h>
50#include <linux/major.h>
51#include <linux/string.h>
52#include <linux/fcntl.h>
53#include <linux/ptrace.h>
54#include <linux/gfp.h>
55#include <linux/ioport.h>
56#include <linux/mm.h>
57#include <linux/smp_lock.h>
58#include <linux/delay.h>
59#include <linux/pci.h>
60
61#include <asm/system.h>
62#include <asm/io.h>
63#include <asm/irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#include <asm/bitops.h>
65#include <asm/uaccess.h>
66
67#include "mxser.h"
68
69#define MXSER_VERSION "1.8"
70#define MXSERMAJOR 174
71#define MXSERCUMAJOR 175
72
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -070073#define MXSER_EVENT_TXLOW 1
74#define MXSER_EVENT_HANGUP 2
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76#define MXSER_BOARDS 4 /* Max. boards */
77#define MXSER_PORTS 32 /* Max. ports */
78#define MXSER_PORTS_PER_BOARD 8 /* Max. ports per board */
79#define MXSER_ISR_PASS_LIMIT 256
80
81#define MXSER_ERR_IOADDR -1
82#define MXSER_ERR_IRQ -2
83#define MXSER_ERR_IRQ_CONFLIT -3
84#define MXSER_ERR_VECTOR -4
85
86#define SERIAL_TYPE_NORMAL 1
87#define SERIAL_TYPE_CALLOUT 2
88
89#define WAKEUP_CHARS 256
90
91#define UART_MCR_AFE 0x20
92#define UART_LSR_SPECIAL 0x1E
93
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -070094#define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK|\
95 IXON|IXOFF))
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Thomas Gleixner0f2ed4c2006-07-01 19:29:33 -070097#define IRQ_T(info) ((info->flags & ASYNC_SHARE_IRQ) ? IRQF_SHARED : IRQF_DISABLED)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99#define C168_ASIC_ID 1
100#define C104_ASIC_ID 2
101#define C102_ASIC_ID 0xB
102#define CI132_ASIC_ID 4
103#define CI134_ASIC_ID 3
104#define CI104J_ASIC_ID 5
105
106enum {
107 MXSER_BOARD_C168_ISA = 1,
108 MXSER_BOARD_C104_ISA,
109 MXSER_BOARD_CI104J,
110 MXSER_BOARD_C168_PCI,
111 MXSER_BOARD_C104_PCI,
112 MXSER_BOARD_C102_ISA,
113 MXSER_BOARD_CI132,
114 MXSER_BOARD_CI134,
115 MXSER_BOARD_CP132,
116 MXSER_BOARD_CP114,
117 MXSER_BOARD_CT114,
118 MXSER_BOARD_CP102,
119 MXSER_BOARD_CP104U,
120 MXSER_BOARD_CP168U,
121 MXSER_BOARD_CP132U,
122 MXSER_BOARD_CP134U,
123 MXSER_BOARD_CP104JU,
124 MXSER_BOARD_RC7000,
125 MXSER_BOARD_CP118U,
126 MXSER_BOARD_CP102UL,
127 MXSER_BOARD_CP102U,
128};
129
130static char *mxser_brdname[] = {
131 "C168 series",
132 "C104 series",
133 "CI-104J series",
134 "C168H/PCI series",
135 "C104H/PCI series",
136 "C102 series",
137 "CI-132 series",
138 "CI-134 series",
139 "CP-132 series",
140 "CP-114 series",
141 "CT-114 series",
142 "CP-102 series",
143 "CP-104U series",
144 "CP-168U series",
145 "CP-132U series",
146 "CP-134U series",
147 "CP-104JU series",
148 "Moxa UC7000 Serial",
149 "CP-118U series",
150 "CP-102UL series",
151 "CP-102U series",
152};
153
154static int mxser_numports[] = {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700155 8, /* C168-ISA */
156 4, /* C104-ISA */
157 4, /* CI104J */
158 8, /* C168-PCI */
159 4, /* C104-PCI */
160 2, /* C102-ISA */
161 2, /* CI132 */
162 4, /* CI134 */
163 2, /* CP132 */
164 4, /* CP114 */
165 4, /* CT114 */
166 2, /* CP102 */
167 4, /* CP104U */
168 8, /* CP168U */
169 2, /* CP132U */
170 4, /* CP134U */
171 4, /* CP104JU */
172 8, /* RC7000 */
173 8, /* CP118U */
174 2, /* CP102UL */
175 2, /* CP102U */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176};
177
178#define UART_TYPE_NUM 2
179
180static const unsigned int Gmoxa_uart_id[UART_TYPE_NUM] = {
181 MOXA_MUST_MU150_HWID,
182 MOXA_MUST_MU860_HWID
183};
184
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700185/* This is only for PCI */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186#define UART_INFO_NUM 3
187struct mxpciuart_info {
188 int type;
189 int tx_fifo;
190 int rx_fifo;
191 int xmit_fifo_size;
192 int rx_high_water;
193 int rx_trigger;
194 int rx_low_water;
195 long max_baud;
196};
197
198static const struct mxpciuart_info Gpci_uart_info[UART_INFO_NUM] = {
199 {MOXA_OTHER_UART, 16, 16, 16, 14, 14, 1, 921600L},
200 {MOXA_MUST_MU150_HWID, 64, 64, 64, 48, 48, 16, 230400L},
201 {MOXA_MUST_MU860_HWID, 128, 128, 128, 96, 96, 32, 921600L}
202};
203
204
205#ifdef CONFIG_PCI
206
207static struct pci_device_id mxser_pcibrds[] = {
208 {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C168, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_C168_PCI},
209 {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C104, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_C104_PCI},
210 {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP132, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_CP132},
211 {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP114, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_CP114},
212 {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CT114, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_CT114},
213 {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP102, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_CP102},
214 {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP104U, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_CP104U},
215 {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP168U, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_CP168U},
216 {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP132U, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_CP132U},
217 {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP134U, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_CP134U},
218 {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP104JU, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_CP104JU},
219 {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_RC7000, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_RC7000},
220 {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP118U, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_CP118U},
221 {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP102UL, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_CP102UL},
222 {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP102U, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_CP102U},
223 {0}
224};
225
226MODULE_DEVICE_TABLE(pci, mxser_pcibrds);
227
228
229#endif
230
231typedef struct _moxa_pci_info {
232 unsigned short busNum;
233 unsigned short devNum;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700234 struct pci_dev *pdev; /* add by Victor Yu. 06-23-2003 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235} moxa_pci_info;
236
237static int ioaddr[MXSER_BOARDS] = { 0, 0, 0, 0 };
238static int ttymajor = MXSERMAJOR;
239static int calloutmajor = MXSERCUMAJOR;
240static int verbose = 0;
241
242/* Variables for insmod */
243
244MODULE_AUTHOR("Casper Yang");
245MODULE_DESCRIPTION("MOXA Smartio/Industio Family Multiport Board Device Driver");
Rusty Russell8d3b33f2006-03-25 03:07:05 -0800246module_param_array(ioaddr, int, NULL, 0);
247module_param(ttymajor, int, 0);
248module_param(calloutmajor, int, 0);
249module_param(verbose, bool, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250MODULE_LICENSE("GPL");
251
252struct mxser_log {
253 int tick;
254 unsigned long rxcnt[MXSER_PORTS];
255 unsigned long txcnt[MXSER_PORTS];
256};
257
258
259struct mxser_mon {
260 unsigned long rxcnt;
261 unsigned long txcnt;
262 unsigned long up_rxcnt;
263 unsigned long up_txcnt;
264 int modem_status;
265 unsigned char hold_reason;
266};
267
268struct mxser_mon_ext {
269 unsigned long rx_cnt[32];
270 unsigned long tx_cnt[32];
271 unsigned long up_rxcnt[32];
272 unsigned long up_txcnt[32];
273 int modem_status[32];
274
275 long baudrate[32];
276 int databits[32];
277 int stopbits[32];
278 int parity[32];
279 int flowctrl[32];
280 int fifo[32];
281 int iftype[32];
282};
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284struct mxser_hwconf {
285 int board_type;
286 int ports;
287 int irq;
288 int vector;
289 int vector_mask;
290 int uart_type;
291 int ioaddr[MXSER_PORTS_PER_BOARD];
292 int baud_base[MXSER_PORTS_PER_BOARD];
293 moxa_pci_info pciInfo;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700294 int IsMoxaMustChipFlag; /* add by Victor Yu. 08-30-2002 */
295 int MaxCanSetBaudRate[MXSER_PORTS_PER_BOARD]; /* add by Victor Yu. 09-04-2002 */
296 int opmode_ioaddr[MXSER_PORTS_PER_BOARD]; /* add by Victor Yu. 01-05-2004 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297};
298
299struct mxser_struct {
300 int port;
301 int base; /* port base address */
302 int irq; /* port using irq no. */
303 int vector; /* port irq vector */
304 int vectormask; /* port vector mask */
305 int rx_high_water;
306 int rx_trigger; /* Rx fifo trigger level */
307 int rx_low_water;
308 int baud_base; /* max. speed */
309 int flags; /* defined in tty.h */
310 int type; /* UART type */
311 struct tty_struct *tty;
312 int read_status_mask;
313 int ignore_status_mask;
314 int xmit_fifo_size;
315 int custom_divisor;
316 int x_char; /* xon/xoff character */
317 int close_delay;
318 unsigned short closing_wait;
319 int IER; /* Interrupt Enable Register */
320 int MCR; /* Modem control register */
321 unsigned long event;
322 int count; /* # of fd on device */
323 int blocked_open; /* # of blocked opens */
324 long session; /* Session of opening process */
325 long pgrp; /* pgrp of opening process */
326 unsigned char *xmit_buf;
327 int xmit_head;
328 int xmit_tail;
329 int xmit_cnt;
330 struct work_struct tqueue;
331 struct termios normal_termios;
332 struct termios callout_termios;
333 wait_queue_head_t open_wait;
334 wait_queue_head_t close_wait;
335 wait_queue_head_t delta_msr_wait;
336 struct async_icount icount; /* kernel counters for the 4 input interrupts */
337 int timeout;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700338 int IsMoxaMustChipFlag; /* add by Victor Yu. 08-30-2002 */
339 int MaxCanSetBaudRate; /* add by Victor Yu. 09-04-2002 */
340 int opmode_ioaddr; /* add by Victor Yu. 01-05-2004 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 unsigned char stop_rx;
342 unsigned char ldisc_stop_rx;
343 long realbaud;
344 struct mxser_mon mon_data;
345 unsigned char err_shadow;
346 spinlock_t slock;
347};
348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349struct mxser_mstatus {
350 tcflag_t cflag;
351 int cts;
352 int dsr;
353 int ri;
354 int dcd;
355};
356
357static struct mxser_mstatus GMStatus[MXSER_PORTS];
358
359static int mxserBoardCAP[MXSER_BOARDS] = {
360 0, 0, 0, 0
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700361 /* 0x180, 0x280, 0x200, 0x320 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362};
363
364static struct tty_driver *mxvar_sdriver;
365static struct mxser_struct mxvar_table[MXSER_PORTS];
366static struct tty_struct *mxvar_tty[MXSER_PORTS + 1];
367static struct termios *mxvar_termios[MXSER_PORTS + 1];
368static struct termios *mxvar_termios_locked[MXSER_PORTS + 1];
369static struct mxser_log mxvar_log;
370static int mxvar_diagflag;
371static unsigned char mxser_msr[MXSER_PORTS + 1];
372static struct mxser_mon_ext mon_data_ext;
373static int mxser_set_baud_method[MXSER_PORTS + 1];
374static spinlock_t gm_lock;
375
376/*
377 * This is used to figure out the divisor speeds and the timeouts
378 */
379
380static struct mxser_hwconf mxsercfg[MXSER_BOARDS];
381
382/*
383 * static functions:
384 */
385
386static void mxser_getcfg(int board, struct mxser_hwconf *hwconf);
387static int mxser_init(void);
388
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700389/* static void mxser_poll(unsigned long); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390static int mxser_get_ISA_conf(int, struct mxser_hwconf *);
391static int mxser_get_PCI_conf(int, int, int, struct mxser_hwconf *);
David Howellsc4028952006-11-22 14:57:56 +0000392static void mxser_do_softint(struct work_struct *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393static int mxser_open(struct tty_struct *, struct file *);
394static void mxser_close(struct tty_struct *, struct file *);
395static int mxser_write(struct tty_struct *, const unsigned char *, int);
396static int mxser_write_room(struct tty_struct *);
397static void mxser_flush_buffer(struct tty_struct *);
398static int mxser_chars_in_buffer(struct tty_struct *);
399static void mxser_flush_chars(struct tty_struct *);
400static void mxser_put_char(struct tty_struct *, unsigned char);
401static int mxser_ioctl(struct tty_struct *, struct file *, uint, ulong);
402static int mxser_ioctl_special(unsigned int, void __user *);
403static void mxser_throttle(struct tty_struct *);
404static void mxser_unthrottle(struct tty_struct *);
405static void mxser_set_termios(struct tty_struct *, struct termios *);
406static void mxser_stop(struct tty_struct *);
407static void mxser_start(struct tty_struct *);
408static void mxser_hangup(struct tty_struct *);
409static void mxser_rs_break(struct tty_struct *, int);
David Howells7d12e782006-10-05 14:55:46 +0100410static irqreturn_t mxser_interrupt(int, void *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411static void mxser_receive_chars(struct mxser_struct *, int *);
412static void mxser_transmit_chars(struct mxser_struct *);
413static void mxser_check_modem_status(struct mxser_struct *, int);
414static int mxser_block_til_ready(struct tty_struct *, struct file *, struct mxser_struct *);
415static int mxser_startup(struct mxser_struct *);
416static void mxser_shutdown(struct mxser_struct *);
417static int mxser_change_speed(struct mxser_struct *, struct termios *old_termios);
418static int mxser_get_serial_info(struct mxser_struct *, struct serial_struct __user *);
419static int mxser_set_serial_info(struct mxser_struct *, struct serial_struct __user *);
420static int mxser_get_lsr_info(struct mxser_struct *, unsigned int __user *);
421static void mxser_send_break(struct mxser_struct *, int);
422static int mxser_tiocmget(struct tty_struct *, struct file *);
423static int mxser_tiocmset(struct tty_struct *, struct file *, unsigned int, unsigned int);
424static int mxser_set_baud(struct mxser_struct *info, long newspd);
425static void mxser_wait_until_sent(struct tty_struct *tty, int timeout);
426
427static void mxser_startrx(struct tty_struct *tty);
428static void mxser_stoprx(struct tty_struct *tty);
429
430
431static int CheckIsMoxaMust(int io)
432{
433 u8 oldmcr, hwid;
434 int i;
435
436 outb(0, io + UART_LCR);
437 DISABLE_MOXA_MUST_ENCHANCE_MODE(io);
438 oldmcr = inb(io + UART_MCR);
439 outb(0, io + UART_MCR);
440 SET_MOXA_MUST_XON1_VALUE(io, 0x11);
441 if ((hwid = inb(io + UART_MCR)) != 0) {
442 outb(oldmcr, io + UART_MCR);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700443 return MOXA_OTHER_UART;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 }
445
446 GET_MOXA_MUST_HARDWARE_ID(io, &hwid);
447 for (i = 0; i < UART_TYPE_NUM; i++) {
448 if (hwid == Gmoxa_uart_id[i])
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700449 return (int)hwid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 }
451 return MOXA_OTHER_UART;
452}
453
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700454/* above is modified by Victor Yu. 08-15-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Jeff Dikeb68e31d2006-10-02 02:17:18 -0700456static const struct tty_operations mxser_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 .open = mxser_open,
458 .close = mxser_close,
459 .write = mxser_write,
460 .put_char = mxser_put_char,
461 .flush_chars = mxser_flush_chars,
462 .write_room = mxser_write_room,
463 .chars_in_buffer = mxser_chars_in_buffer,
464 .flush_buffer = mxser_flush_buffer,
465 .ioctl = mxser_ioctl,
466 .throttle = mxser_throttle,
467 .unthrottle = mxser_unthrottle,
468 .set_termios = mxser_set_termios,
469 .stop = mxser_stop,
470 .start = mxser_start,
471 .hangup = mxser_hangup,
Kirill Smelkov57432342005-11-07 00:59:20 -0800472 .break_ctl = mxser_rs_break,
473 .wait_until_sent = mxser_wait_until_sent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 .tiocmget = mxser_tiocmget,
475 .tiocmset = mxser_tiocmset,
476};
477
478/*
479 * The MOXA Smartio/Industio serial driver boot-time initialization code!
480 */
481
482static int __init mxser_module_init(void)
483{
484 int ret;
485
486 if (verbose)
487 printk(KERN_DEBUG "Loading module mxser ...\n");
488 ret = mxser_init();
489 if (verbose)
490 printk(KERN_DEBUG "Done.\n");
491 return ret;
492}
493
494static void __exit mxser_module_exit(void)
495{
Kirill Smelkov64698b62005-11-07 00:59:22 -0800496 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498 if (verbose)
499 printk(KERN_DEBUG "Unloading module mxser ...\n");
500
Kirill Smelkov64698b62005-11-07 00:59:22 -0800501 err = tty_unregister_driver(mxvar_sdriver);
502 if (!err)
503 put_tty_driver(mxvar_sdriver);
504 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 printk(KERN_ERR "Couldn't unregister MOXA Smartio/Industio family serial driver\n");
506
507 for (i = 0; i < MXSER_BOARDS; i++) {
508 struct pci_dev *pdev;
509
510 if (mxsercfg[i].board_type == -1)
511 continue;
512 else {
513 pdev = mxsercfg[i].pciInfo.pdev;
514 free_irq(mxsercfg[i].irq, &mxvar_table[i * MXSER_PORTS_PER_BOARD]);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700515 if (pdev != NULL) { /* PCI */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 release_region(pci_resource_start(pdev, 2), pci_resource_len(pdev, 2));
517 release_region(pci_resource_start(pdev, 3), pci_resource_len(pdev, 3));
Alan Cox1187ece2006-12-08 02:38:10 -0800518 pci_dev_put(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 } else {
520 release_region(mxsercfg[i].ioaddr[0], 8 * mxsercfg[i].ports);
521 release_region(mxsercfg[i].vector, 1);
522 }
523 }
524 }
525 if (verbose)
526 printk(KERN_DEBUG "Done.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527}
528
529static void process_txrx_fifo(struct mxser_struct *info)
530{
531 int i;
532
533 if ((info->type == PORT_16450) || (info->type == PORT_8250)) {
534 info->rx_trigger = 1;
535 info->rx_high_water = 1;
536 info->rx_low_water = 1;
537 info->xmit_fifo_size = 1;
538 } else {
539 for (i = 0; i < UART_INFO_NUM; i++) {
540 if (info->IsMoxaMustChipFlag == Gpci_uart_info[i].type) {
541 info->rx_trigger = Gpci_uart_info[i].rx_trigger;
542 info->rx_low_water = Gpci_uart_info[i].rx_low_water;
543 info->rx_high_water = Gpci_uart_info[i].rx_high_water;
544 info->xmit_fifo_size = Gpci_uart_info[i].xmit_fifo_size;
545 break;
546 }
547 }
548 }
549}
550
551static int mxser_initbrd(int board, struct mxser_hwconf *hwconf)
552{
553 struct mxser_struct *info;
554 int retval;
555 int i, n;
556
557 n = board * MXSER_PORTS_PER_BOARD;
558 info = &mxvar_table[n];
559 /*if (verbose) */ {
Jiri Slabyae25f8e2006-12-08 02:38:09 -0800560 printk(KERN_DEBUG " ttyMI%d - ttyMI%d ",
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700561 n, n + hwconf->ports - 1);
562 printk(" max. baud rate = %d bps.\n",
563 hwconf->MaxCanSetBaudRate[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 }
565
566 for (i = 0; i < hwconf->ports; i++, n++, info++) {
567 info->port = n;
568 info->base = hwconf->ioaddr[i];
569 info->irq = hwconf->irq;
570 info->vector = hwconf->vector;
571 info->vectormask = hwconf->vector_mask;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700572 info->opmode_ioaddr = hwconf->opmode_ioaddr[i]; /* add by Victor Yu. 01-05-2004 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 info->stop_rx = 0;
574 info->ldisc_stop_rx = 0;
575
576 info->IsMoxaMustChipFlag = hwconf->IsMoxaMustChipFlag;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700577 /* Enhance mode enabled here */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 if (info->IsMoxaMustChipFlag != MOXA_OTHER_UART) {
579 ENABLE_MOXA_MUST_ENCHANCE_MODE(info->base);
580 }
581
582 info->flags = ASYNC_SHARE_IRQ;
583 info->type = hwconf->uart_type;
584 info->baud_base = hwconf->baud_base[i];
585
586 info->MaxCanSetBaudRate = hwconf->MaxCanSetBaudRate[i];
587
588 process_txrx_fifo(info);
589
590
591 info->custom_divisor = hwconf->baud_base[i] * 16;
592 info->close_delay = 5 * HZ / 10;
593 info->closing_wait = 30 * HZ;
David Howellsc4028952006-11-22 14:57:56 +0000594 INIT_WORK(&info->tqueue, mxser_do_softint);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 info->normal_termios = mxvar_sdriver->init_termios;
596 init_waitqueue_head(&info->open_wait);
597 init_waitqueue_head(&info->close_wait);
598 init_waitqueue_head(&info->delta_msr_wait);
599 memset(&info->mon_data, 0, sizeof(struct mxser_mon));
600 info->err_shadow = 0;
601 spin_lock_init(&info->slock);
602 }
603 /*
604 * Allocate the IRQ if necessary
605 */
606
607
608 /* before set INT ISR, disable all int */
609 for (i = 0; i < hwconf->ports; i++) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700610 outb(inb(hwconf->ioaddr[i] + UART_IER) & 0xf0,
611 hwconf->ioaddr[i] + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 }
613
614 n = board * MXSER_PORTS_PER_BOARD;
615 info = &mxvar_table[n];
616
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700617 retval = request_irq(hwconf->irq, mxser_interrupt, IRQ_T(info),
618 "mxser", info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 if (retval) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700620 printk(KERN_ERR "Board %d: %s",
621 board, mxser_brdname[hwconf->board_type - 1]);
622 printk(" Request irq failed, IRQ (%d) may conflict with"
623 " another device.\n", info->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 return retval;
625 }
626 return 0;
627}
628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629static void mxser_getcfg(int board, struct mxser_hwconf *hwconf)
630{
631 mxsercfg[board] = *hwconf;
632}
633
634#ifdef CONFIG_PCI
635static int mxser_get_PCI_conf(int busnum, int devnum, int board_type, struct mxser_hwconf *hwconf)
636{
637 int i, j;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700638 /* unsigned int val; */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 unsigned int ioaddress;
640 struct pci_dev *pdev = hwconf->pciInfo.pdev;
641
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700642 /* io address */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 hwconf->board_type = board_type;
644 hwconf->ports = mxser_numports[board_type - 1];
645 ioaddress = pci_resource_start(pdev, 2);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700646 request_region(pci_resource_start(pdev, 2), pci_resource_len(pdev, 2),
647 "mxser(IO)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700649 for (i = 0; i < hwconf->ports; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 hwconf->ioaddr[i] = ioaddress + 8 * i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700652 /* vector */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 ioaddress = pci_resource_start(pdev, 3);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700654 request_region(pci_resource_start(pdev, 3), pci_resource_len(pdev, 3),
655 "mxser(vector)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 hwconf->vector = ioaddress;
657
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700658 /* irq */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 hwconf->irq = hwconf->pciInfo.pdev->irq;
660
661 hwconf->IsMoxaMustChipFlag = CheckIsMoxaMust(hwconf->ioaddr[0]);
662 hwconf->uart_type = PORT_16550A;
663 hwconf->vector_mask = 0;
664
665
666 for (i = 0; i < hwconf->ports; i++) {
667 for (j = 0; j < UART_INFO_NUM; j++) {
668 if (Gpci_uart_info[j].type == hwconf->IsMoxaMustChipFlag) {
669 hwconf->MaxCanSetBaudRate[i] = Gpci_uart_info[j].max_baud;
670
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700671 /* exception....CP-102 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 if (board_type == MXSER_BOARD_CP102)
673 hwconf->MaxCanSetBaudRate[i] = 921600;
674 break;
675 }
676 }
677 }
678
679 if (hwconf->IsMoxaMustChipFlag == MOXA_MUST_MU860_HWID) {
680 for (i = 0; i < hwconf->ports; i++) {
681 if (i < 4)
682 hwconf->opmode_ioaddr[i] = ioaddress + 4;
683 else
684 hwconf->opmode_ioaddr[i] = ioaddress + 0x0c;
685 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700686 outb(0, ioaddress + 4); /* default set to RS232 mode */
687 outb(0, ioaddress + 0x0c); /* default set to RS232 mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 }
689
690 for (i = 0; i < hwconf->ports; i++) {
691 hwconf->vector_mask |= (1 << i);
692 hwconf->baud_base[i] = 921600;
693 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700694 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695}
696#endif
697
698static int mxser_init(void)
699{
700 int i, m, retval, b, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 struct pci_dev *pdev = NULL;
702 int index;
703 unsigned char busnum, devnum;
704 struct mxser_hwconf hwconf;
705
706 mxvar_sdriver = alloc_tty_driver(MXSER_PORTS + 1);
707 if (!mxvar_sdriver)
708 return -ENOMEM;
709 spin_lock_init(&gm_lock);
710
711 for (i = 0; i < MXSER_BOARDS; i++) {
712 mxsercfg[i].board_type = -1;
713 }
714
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700715 printk(KERN_INFO "MOXA Smartio/Industio family driver version %s\n",
716 MXSER_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
718 /* Initialize the tty_driver structure */
719 memset(mxvar_sdriver, 0, sizeof(struct tty_driver));
720 mxvar_sdriver->magic = TTY_DRIVER_MAGIC;
Jiri Slabyae25f8e2006-12-08 02:38:09 -0800721 mxvar_sdriver->name = "ttyMI";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 mxvar_sdriver->major = ttymajor;
723 mxvar_sdriver->minor_start = 0;
724 mxvar_sdriver->num = MXSER_PORTS + 1;
725 mxvar_sdriver->type = TTY_DRIVER_TYPE_SERIAL;
726 mxvar_sdriver->subtype = SERIAL_TYPE_NORMAL;
727 mxvar_sdriver->init_termios = tty_std_termios;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700728 mxvar_sdriver->init_termios.c_cflag = B9600|CS8|CREAD|HUPCL|CLOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 mxvar_sdriver->flags = TTY_DRIVER_REAL_RAW;
730 tty_set_operations(mxvar_sdriver, &mxser_ops);
731 mxvar_sdriver->ttys = mxvar_tty;
732 mxvar_sdriver->termios = mxvar_termios;
733 mxvar_sdriver->termios_locked = mxvar_termios_locked;
734
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 mxvar_diagflag = 0;
736 memset(mxvar_table, 0, MXSER_PORTS * sizeof(struct mxser_struct));
737 memset(&mxvar_log, 0, sizeof(struct mxser_log));
738
739 memset(&mxser_msr, 0, sizeof(unsigned char) * (MXSER_PORTS + 1));
740 memset(&mon_data_ext, 0, sizeof(struct mxser_mon_ext));
741 memset(&mxser_set_baud_method, 0, sizeof(int) * (MXSER_PORTS + 1));
742 memset(&hwconf, 0, sizeof(struct mxser_hwconf));
743
744 m = 0;
745 /* Start finding ISA boards here */
746 for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
747 int cap;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700748
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 if (!(cap = mxserBoardCAP[b]))
750 continue;
751
752 retval = mxser_get_ISA_conf(cap, &hwconf);
753
754 if (retval != 0)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700755 printk(KERN_INFO "Found MOXA %s board (CAP=0x%x)\n",
756 mxser_brdname[hwconf.board_type - 1], ioaddr[b]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
758 if (retval <= 0) {
759 if (retval == MXSER_ERR_IRQ)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700760 printk(KERN_ERR "Invalid interrupt number, "
761 "board not configured\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 else if (retval == MXSER_ERR_IRQ_CONFLIT)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700763 printk(KERN_ERR "Invalid interrupt number, "
764 "board not configured\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 else if (retval == MXSER_ERR_VECTOR)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700766 printk(KERN_ERR "Invalid interrupt vector, "
767 "board not configured\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 else if (retval == MXSER_ERR_IOADDR)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700769 printk(KERN_ERR "Invalid I/O address, "
770 "board not configured\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
772 continue;
773 }
774
775 hwconf.pciInfo.busNum = 0;
776 hwconf.pciInfo.devNum = 0;
777 hwconf.pciInfo.pdev = NULL;
778
779 mxser_getcfg(m, &hwconf);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700780 /*
781 * init mxsercfg first,
782 * or mxsercfg data is not correct on ISR.
783 */
784 /* mxser_initbrd will hook ISR. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 if (mxser_initbrd(m, &hwconf) < 0)
786 continue;
787
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 m++;
789 }
790
791 /* Start finding ISA boards from module arg */
792 for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
793 int cap;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700794
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 if (!(cap = ioaddr[b]))
796 continue;
797
798 retval = mxser_get_ISA_conf(cap, &hwconf);
799
800 if (retval != 0)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700801 printk(KERN_INFO "Found MOXA %s board (CAP=0x%x)\n",
802 mxser_brdname[hwconf.board_type - 1], ioaddr[b]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
804 if (retval <= 0) {
805 if (retval == MXSER_ERR_IRQ)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700806 printk(KERN_ERR "Invalid interrupt number, "
807 "board not configured\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 else if (retval == MXSER_ERR_IRQ_CONFLIT)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700809 printk(KERN_ERR "Invalid interrupt number, "
810 "board not configured\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 else if (retval == MXSER_ERR_VECTOR)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700812 printk(KERN_ERR "Invalid interrupt vector, "
813 "board not configured\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 else if (retval == MXSER_ERR_IOADDR)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700815 printk(KERN_ERR "Invalid I/O address, "
816 "board not configured\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
818 continue;
819 }
820
821 hwconf.pciInfo.busNum = 0;
822 hwconf.pciInfo.devNum = 0;
823 hwconf.pciInfo.pdev = NULL;
824
825 mxser_getcfg(m, &hwconf);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700826 /*
827 * init mxsercfg first,
828 * or mxsercfg data is not correct on ISR.
829 */
830 /* mxser_initbrd will hook ISR. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 if (mxser_initbrd(m, &hwconf) < 0)
832 continue;
833
834 m++;
835 }
836
837 /* start finding PCI board here */
838#ifdef CONFIG_PCI
Tobias Klauserfe971072006-01-09 20:54:02 -0800839 n = ARRAY_SIZE(mxser_pcibrds) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 index = 0;
841 b = 0;
842 while (b < n) {
Alan Cox1187ece2006-12-08 02:38:10 -0800843 pdev = pci_get_device(mxser_pcibrds[b].vendor,
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700844 mxser_pcibrds[b].device, pdev);
Alan Cox1187ece2006-12-08 02:38:10 -0800845 if (pdev == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 b++;
847 continue;
848 }
849 hwconf.pciInfo.busNum = busnum = pdev->bus->number;
850 hwconf.pciInfo.devNum = devnum = PCI_SLOT(pdev->devfn) << 3;
851 hwconf.pciInfo.pdev = pdev;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700852 printk(KERN_INFO "Found MOXA %s board(BusNo=%d,DevNo=%d)\n",
853 mxser_brdname[(int) (mxser_pcibrds[b].driver_data) - 1],
854 busnum, devnum >> 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 index++;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700856 if (m >= MXSER_BOARDS)
857 printk(KERN_ERR
858 "Too many Smartio/Industio family boards find "
859 "(maximum %d), board not configured\n",
860 MXSER_BOARDS);
861 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 if (pci_enable_device(pdev)) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700863 printk(KERN_ERR "Moxa SmartI/O PCI enable "
864 "fail !\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 continue;
866 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700867 retval = mxser_get_PCI_conf(busnum, devnum,
868 (int)mxser_pcibrds[b].driver_data,
869 &hwconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 if (retval < 0) {
871 if (retval == MXSER_ERR_IRQ)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700872 printk(KERN_ERR
873 "Invalid interrupt number, "
874 "board not configured\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 else if (retval == MXSER_ERR_IRQ_CONFLIT)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700876 printk(KERN_ERR
877 "Invalid interrupt number, "
878 "board not configured\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 else if (retval == MXSER_ERR_VECTOR)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700880 printk(KERN_ERR
881 "Invalid interrupt vector, "
882 "board not configured\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 else if (retval == MXSER_ERR_IOADDR)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700884 printk(KERN_ERR
885 "Invalid I/O address, "
886 "board not configured\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 continue;
888 }
889 mxser_getcfg(m, &hwconf);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700890 /* init mxsercfg first,
891 * or mxsercfg data is not correct on ISR.
892 */
893 /* mxser_initbrd will hook ISR. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 if (mxser_initbrd(m, &hwconf) < 0)
895 continue;
896 m++;
Alan Cox1187ece2006-12-08 02:38:10 -0800897 /* Keep an extra reference if we succeeded. It will
898 be returned at unload time */
899 pci_dev_get(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 }
901 }
902#endif
903
Kirill Smelkov64698b62005-11-07 00:59:22 -0800904 retval = tty_register_driver(mxvar_sdriver);
905 if (retval) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700906 printk(KERN_ERR "Couldn't install MOXA Smartio/Industio family"
907 " driver !\n");
Kirill Smelkov64698b62005-11-07 00:59:22 -0800908 put_tty_driver(mxvar_sdriver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 for (i = 0; i < MXSER_BOARDS; i++) {
911 if (mxsercfg[i].board_type == -1)
912 continue;
913 else {
914 free_irq(mxsercfg[i].irq, &mxvar_table[i * MXSER_PORTS_PER_BOARD]);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700915 /* todo: release io, vector */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 }
917 }
Kirill Smelkov64698b62005-11-07 00:59:22 -0800918 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 }
920
Kirill Smelkov64698b62005-11-07 00:59:22 -0800921 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922}
923
David Howellsc4028952006-11-22 14:57:56 +0000924static void mxser_do_softint(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925{
David Howellsc4028952006-11-22 14:57:56 +0000926 struct mxser_struct *info =
927 container_of(work, struct mxser_struct, tqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 struct tty_struct *tty;
929
930 tty = info->tty;
931
932 if (tty) {
933 if (test_and_clear_bit(MXSER_EVENT_TXLOW, &info->event))
934 tty_wakeup(tty);
935 if (test_and_clear_bit(MXSER_EVENT_HANGUP, &info->event))
936 tty_hangup(tty);
937 }
938}
939
940static unsigned char mxser_get_msr(int baseaddr, int mode, int port, struct mxser_struct *info)
941{
942 unsigned char status = 0;
943
944 status = inb(baseaddr + UART_MSR);
945
946 mxser_msr[port] &= 0x0F;
947 mxser_msr[port] |= status;
948 status = mxser_msr[port];
949 if (mode)
950 mxser_msr[port] = 0;
951
952 return status;
953}
954
955/*
956 * This routine is called whenever a serial port is opened. It
957 * enables interrupts for a serial port, linking in its async structure into
958 * the IRQ chain. It also performs the serial-specific
959 * initialization for the tty structure.
960 */
961static int mxser_open(struct tty_struct *tty, struct file *filp)
962{
963 struct mxser_struct *info;
964 int retval, line;
965
Kirill Smelkov6f08b722005-11-07 00:59:23 -0800966 /* initialize driver_data in case something fails */
967 tty->driver_data = NULL;
968
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 line = tty->index;
970 if (line == MXSER_PORTS)
971 return 0;
972 if (line < 0 || line > MXSER_PORTS)
973 return -ENODEV;
974 info = mxvar_table + line;
975 if (!info->base)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700976 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
978 tty->driver_data = info;
979 info->tty = tty;
980 /*
981 * Start up serial port
982 */
983 retval = mxser_startup(info);
984 if (retval)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700985 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
987 retval = mxser_block_til_ready(tty, filp, info);
988 if (retval)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700989 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
991 info->count++;
992
993 if ((info->count == 1) && (info->flags & ASYNC_SPLIT_TERMIOS)) {
994 if (tty->driver->subtype == SERIAL_TYPE_NORMAL)
995 *tty->termios = info->normal_termios;
996 else
997 *tty->termios = info->callout_termios;
998 mxser_change_speed(info, NULL);
999 }
1000
Cedric Le Goater937949d2006-12-08 02:37:54 -08001001 info->session = process_session(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 info->pgrp = process_group(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001004 /*
1005 status = mxser_get_msr(info->base, 0, info->port);
1006 mxser_check_modem_status(info, status);
1007 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001009/* unmark here for very high baud rate (ex. 921600 bps) used */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 tty->low_latency = 1;
1011 return 0;
1012}
1013
1014/*
1015 * This routine is called when the serial port gets closed. First, we
1016 * wait for the last remaining data to be sent. Then, we unlink its
1017 * async structure from the interrupt chain if necessary, and we free
1018 * that IRQ if nothing is left in the chain.
1019 */
1020static void mxser_close(struct tty_struct *tty, struct file *filp)
1021{
Jesper Juhl56e139f2006-06-25 05:47:52 -07001022 struct mxser_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
1024 unsigned long timeout;
1025 unsigned long flags;
1026 struct tty_ldisc *ld;
1027
1028 if (tty->index == MXSER_PORTS)
1029 return;
1030 if (!info)
Kirill Smelkov6f08b722005-11-07 00:59:23 -08001031 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
1033 spin_lock_irqsave(&info->slock, flags);
1034
1035 if (tty_hung_up_p(filp)) {
1036 spin_unlock_irqrestore(&info->slock, flags);
1037 return;
1038 }
1039 if ((tty->count == 1) && (info->count != 1)) {
1040 /*
1041 * Uh, oh. tty->count is 1, which means that the tty
1042 * structure will be freed. Info->count should always
1043 * be one in these conditions. If it's greater than
1044 * one, we've got real problems, since it means the
1045 * serial port won't be shutdown.
1046 */
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001047 printk(KERN_ERR "mxser_close: bad serial port count; "
1048 "tty->count is 1, info->count is %d\n", info->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 info->count = 1;
1050 }
1051 if (--info->count < 0) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001052 printk(KERN_ERR "mxser_close: bad serial port count for "
1053 "ttys%d: %d\n", info->port, info->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 info->count = 0;
1055 }
1056 if (info->count) {
1057 spin_unlock_irqrestore(&info->slock, flags);
1058 return;
1059 }
1060 info->flags |= ASYNC_CLOSING;
1061 spin_unlock_irqrestore(&info->slock, flags);
1062 /*
1063 * Save the termios structure, since this port may have
1064 * separate termios for callout and dialin.
1065 */
1066 if (info->flags & ASYNC_NORMAL_ACTIVE)
1067 info->normal_termios = *tty->termios;
1068 /*
1069 * Now we wait for the transmit buffer to clear; and we notify
1070 * the line discipline to only process XON/XOFF characters.
1071 */
1072 tty->closing = 1;
1073 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1074 tty_wait_until_sent(tty, info->closing_wait);
1075 /*
1076 * At this point we stop accepting input. To do this, we
1077 * disable the receive line status interrupts, and tell the
1078 * interrupt driver to stop checking the data ready bit in the
1079 * line status register.
1080 */
1081 info->IER &= ~UART_IER_RLSI;
1082 if (info->IsMoxaMustChipFlag)
1083 info->IER &= ~MOXA_MUST_RECV_ISR;
1084/* by William
1085 info->read_status_mask &= ~UART_LSR_DR;
1086*/
1087 if (info->flags & ASYNC_INITIALIZED) {
1088 outb(info->IER, info->base + UART_IER);
1089 /*
1090 * Before we drop DTR, make sure the UART transmitter
1091 * has completely drained; this is especially
1092 * important if there is a transmit FIFO!
1093 */
1094 timeout = jiffies + HZ;
1095 while (!(inb(info->base + UART_LSR) & UART_LSR_TEMT)) {
Nishanth Aravamudanda4cd8d2005-09-10 00:27:30 -07001096 schedule_timeout_interruptible(5);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 if (time_after(jiffies, timeout))
1098 break;
1099 }
1100 }
1101 mxser_shutdown(info);
1102
1103 if (tty->driver->flush_buffer)
1104 tty->driver->flush_buffer(tty);
1105
1106 ld = tty_ldisc_ref(tty);
1107 if (ld) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001108 if (ld->flush_buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 ld->flush_buffer(tty);
1110 tty_ldisc_deref(ld);
1111 }
1112
1113 tty->closing = 0;
1114 info->event = 0;
1115 info->tty = NULL;
1116 if (info->blocked_open) {
Nishanth Aravamudanda4cd8d2005-09-10 00:27:30 -07001117 if (info->close_delay)
1118 schedule_timeout_interruptible(info->close_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 wake_up_interruptible(&info->open_wait);
1120 }
1121
1122 info->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
1123 wake_up_interruptible(&info->close_wait);
1124
1125}
1126
1127static int mxser_write(struct tty_struct *tty, const unsigned char *buf, int count)
1128{
1129 int c, total = 0;
Jesper Juhl56e139f2006-06-25 05:47:52 -07001130 struct mxser_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 unsigned long flags;
1132
Jesper Juhl8a7f7c92006-06-25 05:47:53 -07001133 if (!info->xmit_buf)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001134 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
1136 while (1) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001137 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1138 SERIAL_XMIT_SIZE - info->xmit_head));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 if (c <= 0)
1140 break;
1141
1142 memcpy(info->xmit_buf + info->xmit_head, buf, c);
1143 spin_lock_irqsave(&info->slock, flags);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001144 info->xmit_head = (info->xmit_head + c) &
1145 (SERIAL_XMIT_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 info->xmit_cnt += c;
1147 spin_unlock_irqrestore(&info->slock, flags);
1148
1149 buf += c;
1150 count -= c;
1151 total += c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 }
1153
1154 if (info->xmit_cnt && !tty->stopped && !(info->IER & UART_IER_THRI)) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001155 if (!tty->hw_stopped ||
1156 (info->type == PORT_16550A) ||
1157 (info->IsMoxaMustChipFlag)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 spin_lock_irqsave(&info->slock, flags);
1159 info->IER |= UART_IER_THRI;
1160 outb(info->IER, info->base + UART_IER);
1161 spin_unlock_irqrestore(&info->slock, flags);
1162 }
1163 }
1164 return total;
1165}
1166
1167static void mxser_put_char(struct tty_struct *tty, unsigned char ch)
1168{
Jesper Juhl56e139f2006-06-25 05:47:52 -07001169 struct mxser_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 unsigned long flags;
1171
Jesper Juhl8a7f7c92006-06-25 05:47:53 -07001172 if (!info->xmit_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 return;
1174
1175 if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1)
1176 return;
1177
1178 spin_lock_irqsave(&info->slock, flags);
1179 info->xmit_buf[info->xmit_head++] = ch;
1180 info->xmit_head &= SERIAL_XMIT_SIZE - 1;
1181 info->xmit_cnt++;
1182 spin_unlock_irqrestore(&info->slock, flags);
1183 if (!tty->stopped && !(info->IER & UART_IER_THRI)) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001184 if (!tty->hw_stopped ||
1185 (info->type == PORT_16550A) ||
1186 info->IsMoxaMustChipFlag) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 spin_lock_irqsave(&info->slock, flags);
1188 info->IER |= UART_IER_THRI;
1189 outb(info->IER, info->base + UART_IER);
1190 spin_unlock_irqrestore(&info->slock, flags);
1191 }
1192 }
1193}
1194
1195
1196static void mxser_flush_chars(struct tty_struct *tty)
1197{
Jesper Juhl56e139f2006-06-25 05:47:52 -07001198 struct mxser_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 unsigned long flags;
1200
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001201 if (info->xmit_cnt <= 0 ||
1202 tty->stopped ||
1203 !info->xmit_buf ||
1204 (tty->hw_stopped &&
1205 (info->type != PORT_16550A) &&
1206 (!info->IsMoxaMustChipFlag)
1207 ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 return;
1209
1210 spin_lock_irqsave(&info->slock, flags);
1211
1212 info->IER |= UART_IER_THRI;
1213 outb(info->IER, info->base + UART_IER);
1214
1215 spin_unlock_irqrestore(&info->slock, flags);
1216}
1217
1218static int mxser_write_room(struct tty_struct *tty)
1219{
Jesper Juhl56e139f2006-06-25 05:47:52 -07001220 struct mxser_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 int ret;
1222
1223 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1224 if (ret < 0)
1225 ret = 0;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001226 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227}
1228
1229static int mxser_chars_in_buffer(struct tty_struct *tty)
1230{
Jesper Juhl56e139f2006-06-25 05:47:52 -07001231 struct mxser_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 return info->xmit_cnt;
1233}
1234
1235static void mxser_flush_buffer(struct tty_struct *tty)
1236{
Jesper Juhl56e139f2006-06-25 05:47:52 -07001237 struct mxser_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 char fcr;
1239 unsigned long flags;
1240
1241
1242 spin_lock_irqsave(&info->slock, flags);
1243 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1244
1245 /* below added by shinhay */
1246 fcr = inb(info->base + UART_FCR);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001247 outb((fcr | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
1248 info->base + UART_FCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 outb(fcr, info->base + UART_FCR);
1250
1251 spin_unlock_irqrestore(&info->slock, flags);
1252 /* above added by shinhay */
1253
1254 wake_up_interruptible(&tty->write_wait);
1255 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) && tty->ldisc.write_wakeup)
1256 (tty->ldisc.write_wakeup) (tty);
1257}
1258
1259static int mxser_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
1260{
Jesper Juhl56e139f2006-06-25 05:47:52 -07001261 struct mxser_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 int retval;
1263 struct async_icount cprev, cnow; /* kernel counter temps */
1264 struct serial_icounter_struct __user *p_cuser;
1265 unsigned long templ;
1266 unsigned long flags;
1267 void __user *argp = (void __user *)arg;
1268
1269 if (tty->index == MXSER_PORTS)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001270 return mxser_ioctl_special(cmd, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001272 /* following add by Victor Yu. 01-05-2004 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 if (cmd == MOXA_SET_OP_MODE || cmd == MOXA_GET_OP_MODE) {
1274 int opmode, p;
1275 static unsigned char ModeMask[] = { 0xfc, 0xf3, 0xcf, 0x3f };
1276 int shiftbit;
1277 unsigned char val, mask;
1278
1279 p = info->port % 4;
1280 if (cmd == MOXA_SET_OP_MODE) {
1281 if (get_user(opmode, (int __user *) argp))
1282 return -EFAULT;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001283 if (opmode != RS232_MODE &&
1284 opmode != RS485_2WIRE_MODE &&
1285 opmode != RS422_MODE &&
1286 opmode != RS485_4WIRE_MODE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 return -EFAULT;
1288 mask = ModeMask[p];
1289 shiftbit = p * 2;
1290 val = inb(info->opmode_ioaddr);
1291 val &= mask;
1292 val |= (opmode << shiftbit);
1293 outb(val, info->opmode_ioaddr);
1294 } else {
1295 shiftbit = p * 2;
1296 opmode = inb(info->opmode_ioaddr) >> shiftbit;
1297 opmode &= OP_MODE_MASK;
1298 if (copy_to_user(argp, &opmode, sizeof(int)))
1299 return -EFAULT;
1300 }
1301 return 0;
1302 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001303 /* above add by Victor Yu. 01-05-2004 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
1305 if ((cmd != TIOCGSERIAL) && (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
1306 if (tty->flags & (1 << TTY_IO_ERROR))
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001307 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 }
1309 switch (cmd) {
1310 case TCSBRK: /* SVID version: non-zero arg --> no break */
1311 retval = tty_check_change(tty);
1312 if (retval)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001313 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 tty_wait_until_sent(tty, 0);
1315 if (!arg)
1316 mxser_send_break(info, HZ / 4); /* 1/4 second */
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001317 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 case TCSBRKP: /* support for POSIX tcsendbreak() */
1319 retval = tty_check_change(tty);
1320 if (retval)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001321 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 tty_wait_until_sent(tty, 0);
1323 mxser_send_break(info, arg ? arg * (HZ / 10) : HZ / 4);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001324 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 case TIOCGSOFTCAR:
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001326 return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *)argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 case TIOCSSOFTCAR:
1328 if (get_user(templ, (unsigned long __user *) argp))
1329 return -EFAULT;
1330 arg = templ;
1331 tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) | (arg ? CLOCAL : 0));
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001332 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 case TIOCGSERIAL:
1334 return mxser_get_serial_info(info, argp);
1335 case TIOCSSERIAL:
1336 return mxser_set_serial_info(info, argp);
1337 case TIOCSERGETLSR: /* Get line status register */
1338 return mxser_get_lsr_info(info, argp);
1339 /*
1340 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1341 * - mask passed in arg for lines of interest
1342 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1343 * Caller should use TIOCGICOUNT to see which one it was
1344 */
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001345 case TIOCMIWAIT: {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 DECLARE_WAITQUEUE(wait, current);
1347 int ret;
1348 spin_lock_irqsave(&info->slock, flags);
1349 cprev = info->icount; /* note the counters on entry */
1350 spin_unlock_irqrestore(&info->slock, flags);
1351
1352 add_wait_queue(&info->delta_msr_wait, &wait);
1353 while (1) {
1354 spin_lock_irqsave(&info->slock, flags);
1355 cnow = info->icount; /* atomic copy */
1356 spin_unlock_irqrestore(&info->slock, flags);
1357
1358 set_current_state(TASK_INTERRUPTIBLE);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001359 if (((arg & TIOCM_RNG) &&
1360 (cnow.rng != cprev.rng)) ||
1361 ((arg & TIOCM_DSR) &&
1362 (cnow.dsr != cprev.dsr)) ||
1363 ((arg & TIOCM_CD) &&
1364 (cnow.dcd != cprev.dcd)) ||
1365 ((arg & TIOCM_CTS) &&
1366 (cnow.cts != cprev.cts))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 ret = 0;
1368 break;
1369 }
1370 /* see if a signal did it */
1371 if (signal_pending(current)) {
1372 ret = -ERESTARTSYS;
1373 break;
1374 }
1375 cprev = cnow;
1376 }
1377 current->state = TASK_RUNNING;
1378 remove_wait_queue(&info->delta_msr_wait, &wait);
1379 break;
1380 }
1381 /* NOTREACHED */
1382 /*
1383 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1384 * Return: write counters to the user passed counter struct
1385 * NB: both 1->0 and 0->1 transitions are counted except for
1386 * RI where only 0->1 is counted.
1387 */
1388 case TIOCGICOUNT:
1389 spin_lock_irqsave(&info->slock, flags);
1390 cnow = info->icount;
1391 spin_unlock_irqrestore(&info->slock, flags);
1392 p_cuser = argp;
1393 /* modified by casper 1/11/2000 */
1394 if (put_user(cnow.frame, &p_cuser->frame))
1395 return -EFAULT;
1396 if (put_user(cnow.brk, &p_cuser->brk))
1397 return -EFAULT;
1398 if (put_user(cnow.overrun, &p_cuser->overrun))
1399 return -EFAULT;
1400 if (put_user(cnow.buf_overrun, &p_cuser->buf_overrun))
1401 return -EFAULT;
1402 if (put_user(cnow.parity, &p_cuser->parity))
1403 return -EFAULT;
1404 if (put_user(cnow.rx, &p_cuser->rx))
1405 return -EFAULT;
1406 if (put_user(cnow.tx, &p_cuser->tx))
1407 return -EFAULT;
1408 put_user(cnow.cts, &p_cuser->cts);
1409 put_user(cnow.dsr, &p_cuser->dsr);
1410 put_user(cnow.rng, &p_cuser->rng);
1411 put_user(cnow.dcd, &p_cuser->dcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 return 0;
1413 case MOXA_HighSpeedOn:
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001414 return put_user(info->baud_base != 115200 ? 1 : 0, (int __user *)argp);
1415 case MOXA_SDS_RSTICOUNTER: {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 info->mon_data.rxcnt = 0;
1417 info->mon_data.txcnt = 0;
1418 return 0;
1419 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001420/* (above) added by James. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 case MOXA_ASPP_SETBAUD:{
1422 long baud;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001423 if (get_user(baud, (long __user *)argp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 return -EFAULT;
1425 mxser_set_baud(info, baud);
1426 return 0;
1427 }
1428 case MOXA_ASPP_GETBAUD:
1429 if (copy_to_user(argp, &info->realbaud, sizeof(long)))
1430 return -EFAULT;
1431
1432 return 0;
1433
1434 case MOXA_ASPP_OQUEUE:{
1435 int len, lsr;
1436
1437 len = mxser_chars_in_buffer(tty);
1438
1439 lsr = inb(info->base + UART_LSR) & UART_LSR_TEMT;
1440
1441 len += (lsr ? 0 : 1);
1442
1443 if (copy_to_user(argp, &len, sizeof(int)))
1444 return -EFAULT;
1445
1446 return 0;
1447 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001448 case MOXA_ASPP_MON: {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 int mcr, status;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001450
1451 /* info->mon_data.ser_param = tty->termios->c_cflag; */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
1453 status = mxser_get_msr(info->base, 1, info->port, info);
1454 mxser_check_modem_status(info, status);
1455
1456 mcr = inb(info->base + UART_MCR);
1457 if (mcr & MOXA_MUST_MCR_XON_FLAG)
1458 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFHOLD;
1459 else
1460 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFHOLD;
1461
1462 if (mcr & MOXA_MUST_MCR_TX_XON)
1463 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFXENT;
1464 else
1465 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFXENT;
1466
1467 if (info->tty->hw_stopped)
1468 info->mon_data.hold_reason |= NPPI_NOTIFY_CTSHOLD;
1469 else
1470 info->mon_data.hold_reason &= ~NPPI_NOTIFY_CTSHOLD;
1471
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001472 if (copy_to_user(argp, &info->mon_data,
1473 sizeof(struct mxser_mon)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 return -EFAULT;
1475
1476 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 }
1478
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001479 case MOXA_ASPP_LSTATUS: {
1480 if (copy_to_user(argp, &info->err_shadow,
1481 sizeof(unsigned char)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 return -EFAULT;
1483
1484 info->err_shadow = 0;
1485 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001487 case MOXA_SET_BAUD_METHOD: {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 int method;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001489
1490 if (get_user(method, (int __user *)argp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 return -EFAULT;
1492 mxser_set_baud_method[info->port] = method;
1493 if (copy_to_user(argp, &method, sizeof(int)))
1494 return -EFAULT;
1495
1496 return 0;
1497 }
1498 default:
1499 return -ENOIOCTLCMD;
1500 }
1501 return 0;
1502}
1503
1504#ifndef CMSPAR
1505#define CMSPAR 010000000000
1506#endif
1507
1508static int mxser_ioctl_special(unsigned int cmd, void __user *argp)
1509{
1510 int i, result, status;
1511
1512 switch (cmd) {
1513 case MOXA_GET_CONF:
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001514 if (copy_to_user(argp, mxsercfg,
1515 sizeof(struct mxser_hwconf) * 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 return -EFAULT;
1517 return 0;
1518 case MOXA_GET_MAJOR:
1519 if (copy_to_user(argp, &ttymajor, sizeof(int)))
1520 return -EFAULT;
1521 return 0;
1522
1523 case MOXA_GET_CUMAJOR:
1524 if (copy_to_user(argp, &calloutmajor, sizeof(int)))
1525 return -EFAULT;
1526 return 0;
1527
1528 case MOXA_CHKPORTENABLE:
1529 result = 0;
1530 for (i = 0; i < MXSER_PORTS; i++) {
1531 if (mxvar_table[i].base)
1532 result |= (1 << i);
1533 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001534 return put_user(result, (unsigned long __user *)argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 case MOXA_GETDATACOUNT:
1536 if (copy_to_user(argp, &mxvar_log, sizeof(mxvar_log)))
1537 return -EFAULT;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001538 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 case MOXA_GETMSTATUS:
1540 for (i = 0; i < MXSER_PORTS; i++) {
1541 GMStatus[i].ri = 0;
1542 if (!mxvar_table[i].base) {
1543 GMStatus[i].dcd = 0;
1544 GMStatus[i].dsr = 0;
1545 GMStatus[i].cts = 0;
1546 continue;
1547 }
1548
1549 if (!mxvar_table[i].tty || !mxvar_table[i].tty->termios)
1550 GMStatus[i].cflag = mxvar_table[i].normal_termios.c_cflag;
1551 else
1552 GMStatus[i].cflag = mxvar_table[i].tty->termios->c_cflag;
1553
1554 status = inb(mxvar_table[i].base + UART_MSR);
1555 if (status & 0x80 /*UART_MSR_DCD */ )
1556 GMStatus[i].dcd = 1;
1557 else
1558 GMStatus[i].dcd = 0;
1559
1560 if (status & 0x20 /*UART_MSR_DSR */ )
1561 GMStatus[i].dsr = 1;
1562 else
1563 GMStatus[i].dsr = 0;
1564
1565
1566 if (status & 0x10 /*UART_MSR_CTS */ )
1567 GMStatus[i].cts = 1;
1568 else
1569 GMStatus[i].cts = 0;
1570 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001571 if (copy_to_user(argp, GMStatus,
1572 sizeof(struct mxser_mstatus) * MXSER_PORTS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 return -EFAULT;
1574 return 0;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001575 case MOXA_ASPP_MON_EXT: {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 int status;
1577 int opmode, p;
1578 int shiftbit;
1579 unsigned cflag, iflag;
1580
1581 for (i = 0; i < MXSER_PORTS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 if (!mxvar_table[i].base)
1583 continue;
1584
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001585 status = mxser_get_msr(mxvar_table[i].base, 0,
1586 i, &(mxvar_table[i]));
1587 /*
1588 mxser_check_modem_status(&mxvar_table[i],
1589 status);
1590 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 if (status & UART_MSR_TERI)
1592 mxvar_table[i].icount.rng++;
1593 if (status & UART_MSR_DDSR)
1594 mxvar_table[i].icount.dsr++;
1595 if (status & UART_MSR_DDCD)
1596 mxvar_table[i].icount.dcd++;
1597 if (status & UART_MSR_DCTS)
1598 mxvar_table[i].icount.cts++;
1599
1600 mxvar_table[i].mon_data.modem_status = status;
1601 mon_data_ext.rx_cnt[i] = mxvar_table[i].mon_data.rxcnt;
1602 mon_data_ext.tx_cnt[i] = mxvar_table[i].mon_data.txcnt;
1603 mon_data_ext.up_rxcnt[i] = mxvar_table[i].mon_data.up_rxcnt;
1604 mon_data_ext.up_txcnt[i] = mxvar_table[i].mon_data.up_txcnt;
1605 mon_data_ext.modem_status[i] = mxvar_table[i].mon_data.modem_status;
1606 mon_data_ext.baudrate[i] = mxvar_table[i].realbaud;
1607
1608 if (!mxvar_table[i].tty || !mxvar_table[i].tty->termios) {
1609 cflag = mxvar_table[i].normal_termios.c_cflag;
1610 iflag = mxvar_table[i].normal_termios.c_iflag;
1611 } else {
1612 cflag = mxvar_table[i].tty->termios->c_cflag;
1613 iflag = mxvar_table[i].tty->termios->c_iflag;
1614 }
1615
1616 mon_data_ext.databits[i] = cflag & CSIZE;
1617
1618 mon_data_ext.stopbits[i] = cflag & CSTOPB;
1619
1620 mon_data_ext.parity[i] = cflag & (PARENB | PARODD | CMSPAR);
1621
1622 mon_data_ext.flowctrl[i] = 0x00;
1623
1624 if (cflag & CRTSCTS)
1625 mon_data_ext.flowctrl[i] |= 0x03;
1626
1627 if (iflag & (IXON | IXOFF))
1628 mon_data_ext.flowctrl[i] |= 0x0C;
1629
1630 if (mxvar_table[i].type == PORT_16550A)
1631 mon_data_ext.fifo[i] = 1;
1632 else
1633 mon_data_ext.fifo[i] = 0;
1634
1635 p = i % 4;
1636 shiftbit = p * 2;
1637 opmode = inb(mxvar_table[i].opmode_ioaddr) >> shiftbit;
1638 opmode &= OP_MODE_MASK;
1639
1640 mon_data_ext.iftype[i] = opmode;
1641
1642 }
1643 if (copy_to_user(argp, &mon_data_ext, sizeof(struct mxser_mon_ext)))
1644 return -EFAULT;
1645
1646 return 0;
1647
1648 }
1649 default:
1650 return -ENOIOCTLCMD;
1651 }
1652 return 0;
1653}
1654
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655static void mxser_stoprx(struct tty_struct *tty)
1656{
Jesper Juhl56e139f2006-06-25 05:47:52 -07001657 struct mxser_struct *info = tty->driver_data;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001658 /* unsigned long flags; */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
1660 info->ldisc_stop_rx = 1;
1661 if (I_IXOFF(tty)) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001662 /* MX_LOCK(&info->slock); */
1663 /* following add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 if (info->IsMoxaMustChipFlag) {
1665 info->IER &= ~MOXA_MUST_RECV_ISR;
1666 outb(info->IER, info->base + UART_IER);
1667 } else {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001668 /* above add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 info->x_char = STOP_CHAR(tty);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001670 /* mask by Victor Yu. 09-02-2002 */
1671 /* outb(info->IER, 0); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 outb(0, info->base + UART_IER);
1673 info->IER |= UART_IER_THRI;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001674 /* force Tx interrupt */
1675 outb(info->IER, info->base + UART_IER);
1676 } /* add by Victor Yu. 09-02-2002 */
1677 /* MX_UNLOCK(&info->slock); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 }
1679
1680 if (info->tty->termios->c_cflag & CRTSCTS) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001681 /* MX_LOCK(&info->slock); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 info->MCR &= ~UART_MCR_RTS;
1683 outb(info->MCR, info->base + UART_MCR);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001684 /* MX_UNLOCK(&info->slock); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 }
1686}
1687
1688static void mxser_startrx(struct tty_struct *tty)
1689{
Jesper Juhl56e139f2006-06-25 05:47:52 -07001690 struct mxser_struct *info = tty->driver_data;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001691 /* unsigned long flags; */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692
1693 info->ldisc_stop_rx = 0;
1694 if (I_IXOFF(tty)) {
1695 if (info->x_char)
1696 info->x_char = 0;
1697 else {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001698 /* MX_LOCK(&info->slock); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001700 /* following add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 if (info->IsMoxaMustChipFlag) {
1702 info->IER |= MOXA_MUST_RECV_ISR;
1703 outb(info->IER, info->base + UART_IER);
1704 } else {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001705 /* above add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706
1707 info->x_char = START_CHAR(tty);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001708 /* mask by Victor Yu. 09-02-2002 */
1709 /* outb(info->IER, 0); */
1710 /* add by Victor Yu. 09-02-2002 */
1711 outb(0, info->base + UART_IER);
1712 /* force Tx interrupt */
1713 info->IER |= UART_IER_THRI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 outb(info->IER, info->base + UART_IER);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001715 } /* add by Victor Yu. 09-02-2002 */
1716 /* MX_UNLOCK(&info->slock); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 }
1718 }
1719
1720 if (info->tty->termios->c_cflag & CRTSCTS) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001721 /* MX_LOCK(&info->slock); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 info->MCR |= UART_MCR_RTS;
1723 outb(info->MCR, info->base + UART_MCR);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001724 /* MX_UNLOCK(&info->slock); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 }
1726}
1727
1728/*
1729 * This routine is called by the upper-layer tty layer to signal that
1730 * incoming characters should be throttled.
1731 */
1732static void mxser_throttle(struct tty_struct *tty)
1733{
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001734 /* struct mxser_struct *info = tty->driver_data; */
1735 /* unsigned long flags; */
1736
1737 /* MX_LOCK(&info->slock); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 mxser_stoprx(tty);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001739 /* MX_UNLOCK(&info->slock); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740}
1741
1742static void mxser_unthrottle(struct tty_struct *tty)
1743{
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001744 /* struct mxser_struct *info = tty->driver_data; */
1745 /* unsigned long flags; */
1746
1747 /* MX_LOCK(&info->slock); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 mxser_startrx(tty);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001749 /* MX_UNLOCK(&info->slock); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750}
1751
1752static void mxser_set_termios(struct tty_struct *tty, struct termios *old_termios)
1753{
Jesper Juhl56e139f2006-06-25 05:47:52 -07001754 struct mxser_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 unsigned long flags;
1756
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001757 if ((tty->termios->c_cflag != old_termios->c_cflag) ||
1758 (RELEVANT_IFLAG(tty->termios->c_iflag) != RELEVANT_IFLAG(old_termios->c_iflag))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759
1760 mxser_change_speed(info, old_termios);
1761
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001762 if ((old_termios->c_cflag & CRTSCTS) &&
1763 !(tty->termios->c_cflag & CRTSCTS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 tty->hw_stopped = 0;
1765 mxser_start(tty);
1766 }
1767 }
1768
1769/* Handle sw stopped */
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001770 if ((old_termios->c_iflag & IXON) &&
1771 !(tty->termios->c_iflag & IXON)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 tty->stopped = 0;
1773
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001774 /* following add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 if (info->IsMoxaMustChipFlag) {
1776 spin_lock_irqsave(&info->slock, flags);
1777 DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->base);
1778 spin_unlock_irqrestore(&info->slock, flags);
1779 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001780 /* above add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
1782 mxser_start(tty);
1783 }
1784}
1785
1786/*
1787 * mxser_stop() and mxser_start()
1788 *
1789 * This routines are called before setting or resetting tty->stopped.
1790 * They enable or disable transmitter interrupts, as necessary.
1791 */
1792static void mxser_stop(struct tty_struct *tty)
1793{
Jesper Juhl56e139f2006-06-25 05:47:52 -07001794 struct mxser_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 unsigned long flags;
1796
1797 spin_lock_irqsave(&info->slock, flags);
1798 if (info->IER & UART_IER_THRI) {
1799 info->IER &= ~UART_IER_THRI;
1800 outb(info->IER, info->base + UART_IER);
1801 }
1802 spin_unlock_irqrestore(&info->slock, flags);
1803}
1804
1805static void mxser_start(struct tty_struct *tty)
1806{
Jesper Juhl56e139f2006-06-25 05:47:52 -07001807 struct mxser_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 unsigned long flags;
1809
1810 spin_lock_irqsave(&info->slock, flags);
1811 if (info->xmit_cnt && info->xmit_buf && !(info->IER & UART_IER_THRI)) {
1812 info->IER |= UART_IER_THRI;
1813 outb(info->IER, info->base + UART_IER);
1814 }
1815 spin_unlock_irqrestore(&info->slock, flags);
1816}
1817
1818/*
1819 * mxser_wait_until_sent() --- wait until the transmitter is empty
1820 */
1821static void mxser_wait_until_sent(struct tty_struct *tty, int timeout)
1822{
Jesper Juhl56e139f2006-06-25 05:47:52 -07001823 struct mxser_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 unsigned long orig_jiffies, char_time;
1825 int lsr;
1826
1827 if (info->type == PORT_UNKNOWN)
1828 return;
1829
1830 if (info->xmit_fifo_size == 0)
1831 return; /* Just in case.... */
1832
1833 orig_jiffies = jiffies;
1834 /*
1835 * Set the check interval to be 1/5 of the estimated time to
1836 * send a single character, and make it at least 1. The check
1837 * interval should also be less than the timeout.
1838 *
1839 * Note: we have to use pretty tight timings here to satisfy
1840 * the NIST-PCTS.
1841 */
1842 char_time = (info->timeout - HZ / 50) / info->xmit_fifo_size;
1843 char_time = char_time / 5;
1844 if (char_time == 0)
1845 char_time = 1;
1846 if (timeout && timeout < char_time)
1847 char_time = timeout;
1848 /*
1849 * If the transmitter hasn't cleared in twice the approximate
1850 * amount of time to send the entire FIFO, it probably won't
1851 * ever clear. This assumes the UART isn't doing flow
1852 * control, which is currently the case. Hence, if it ever
1853 * takes longer than info->timeout, this is probably due to a
1854 * UART bug of some kind. So, we clamp the timeout parameter at
1855 * 2*info->timeout.
1856 */
1857 if (!timeout || timeout > 2 * info->timeout)
1858 timeout = 2 * info->timeout;
1859#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001860 printk(KERN_DEBUG "In rs_wait_until_sent(%d) check=%lu...",
1861 timeout, char_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 printk("jiff=%lu...", jiffies);
1863#endif
1864 while (!((lsr = inb(info->base + UART_LSR)) & UART_LSR_TEMT)) {
1865#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1866 printk("lsr = %d (jiff=%lu)...", lsr, jiffies);
1867#endif
Nishanth Aravamudanda4cd8d2005-09-10 00:27:30 -07001868 schedule_timeout_interruptible(char_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 if (signal_pending(current))
1870 break;
1871 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1872 break;
1873 }
1874 set_current_state(TASK_RUNNING);
1875
1876#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1877 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
1878#endif
1879}
1880
1881
1882/*
1883 * This routine is called by tty_hangup() when a hangup is signaled.
1884 */
1885void mxser_hangup(struct tty_struct *tty)
1886{
Jesper Juhl56e139f2006-06-25 05:47:52 -07001887 struct mxser_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888
1889 mxser_flush_buffer(tty);
1890 mxser_shutdown(info);
1891 info->event = 0;
1892 info->count = 0;
1893 info->flags &= ~ASYNC_NORMAL_ACTIVE;
1894 info->tty = NULL;
1895 wake_up_interruptible(&info->open_wait);
1896}
1897
1898
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001899/* added by James 03-12-2004. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900/*
1901 * mxser_rs_break() --- routine which turns the break handling on or off
1902 */
1903static void mxser_rs_break(struct tty_struct *tty, int break_state)
1904{
Jesper Juhl56e139f2006-06-25 05:47:52 -07001905 struct mxser_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 unsigned long flags;
1907
1908 spin_lock_irqsave(&info->slock, flags);
1909 if (break_state == -1)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001910 outb(inb(info->base + UART_LCR) | UART_LCR_SBC,
1911 info->base + UART_LCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 else
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001913 outb(inb(info->base + UART_LCR) & ~UART_LCR_SBC,
1914 info->base + UART_LCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 spin_unlock_irqrestore(&info->slock, flags);
1916}
1917
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001918/* (above) added by James. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919
1920
1921/*
1922 * This is the serial driver's generic interrupt routine
1923 */
David Howells7d12e782006-10-05 14:55:46 +01001924static irqreturn_t mxser_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925{
1926 int status, iir, i;
1927 struct mxser_struct *info;
1928 struct mxser_struct *port;
1929 int max, irqbits, bits, msr;
1930 int pass_counter = 0;
1931 int handled = IRQ_NONE;
1932
1933 port = NULL;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001934 /* spin_lock(&gm_lock); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935
1936 for (i = 0; i < MXSER_BOARDS; i++) {
1937 if (dev_id == &(mxvar_table[i * MXSER_PORTS_PER_BOARD])) {
1938 port = dev_id;
1939 break;
1940 }
1941 }
1942
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001943 if (i == MXSER_BOARDS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 goto irq_stop;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001945 if (port == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 goto irq_stop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 max = mxser_numports[mxsercfg[i].board_type - 1];
1948 while (1) {
1949 irqbits = inb(port->vector) & port->vectormask;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001950 if (irqbits == port->vectormask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
1953 handled = IRQ_HANDLED;
1954 for (i = 0, bits = 1; i < max; i++, irqbits |= bits, bits <<= 1) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001955 if (irqbits == port->vectormask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 if (bits & irqbits)
1958 continue;
1959 info = port + i;
1960
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001961 /* following add by Victor Yu. 09-13-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 iir = inb(info->base + UART_IIR);
1963 if (iir & UART_IIR_NO_INT)
1964 continue;
1965 iir &= MOXA_MUST_IIR_MASK;
1966 if (!info->tty) {
1967 status = inb(info->base + UART_LSR);
1968 outb(0x27, info->base + UART_FCR);
1969 inb(info->base + UART_MSR);
1970 continue;
1971 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001972 /* above add by Victor Yu. 09-13-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 /*
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001974 if (info->tty->flip.count < TTY_FLIPBUF_SIZE / 4) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 info->IER |= MOXA_MUST_RECV_ISR;
1976 outb(info->IER, info->base + UART_IER);
1977 }
1978 */
1979
1980
1981 /* mask by Victor Yu. 09-13-2002
1982 if ( !info->tty ||
1983 (inb(info->base + UART_IIR) & UART_IIR_NO_INT) )
1984 continue;
1985 */
1986 /* mask by Victor Yu. 09-02-2002
1987 status = inb(info->base + UART_LSR) & info->read_status_mask;
1988 */
1989
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001990 /* following add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 status = inb(info->base + UART_LSR);
1992
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001993 if (status & UART_LSR_PE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 info->err_shadow |= NPPI_NOTIFY_PARITY;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001995 if (status & UART_LSR_FE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 info->err_shadow |= NPPI_NOTIFY_FRAMING;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001997 if (status & UART_LSR_OE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 info->err_shadow |= NPPI_NOTIFY_HW_OVERRUN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 if (status & UART_LSR_BI)
2000 info->err_shadow |= NPPI_NOTIFY_BREAK;
2001
2002 if (info->IsMoxaMustChipFlag) {
2003 /*
2004 if ( (status & 0x02) && !(status & 0x01) ) {
2005 outb(info->base+UART_FCR, 0x23);
2006 continue;
2007 }
2008 */
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002009 if (iir == MOXA_MUST_IIR_GDA ||
2010 iir == MOXA_MUST_IIR_RDA ||
2011 iir == MOXA_MUST_IIR_RTO ||
2012 iir == MOXA_MUST_IIR_LSR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 mxser_receive_chars(info, &status);
2014
2015 } else {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002016 /* above add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017
2018 status &= info->read_status_mask;
2019 if (status & UART_LSR_DR)
2020 mxser_receive_chars(info, &status);
2021 }
2022 msr = inb(info->base + UART_MSR);
2023 if (msr & UART_MSR_ANY_DELTA) {
2024 mxser_check_modem_status(info, msr);
2025 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002026 /* following add by Victor Yu. 09-13-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 if (info->IsMoxaMustChipFlag) {
2028 if ((iir == 0x02) && (status & UART_LSR_THRE)) {
2029 mxser_transmit_chars(info);
2030 }
2031 } else {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002032 /* above add by Victor Yu. 09-13-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033
2034 if (status & UART_LSR_THRE) {
2035/* 8-2-99 by William
2036 if ( info->x_char || (info->xmit_cnt > 0) )
2037*/
2038 mxser_transmit_chars(info);
2039 }
2040 }
2041 }
2042 if (pass_counter++ > MXSER_ISR_PASS_LIMIT) {
2043 break; /* Prevent infinite loops */
2044 }
2045 }
2046
2047 irq_stop:
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002048 /* spin_unlock(&gm_lock); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 return handled;
2050}
2051
2052static void mxser_receive_chars(struct mxser_struct *info, int *status)
2053{
2054 struct tty_struct *tty = info->tty;
2055 unsigned char ch, gdl;
2056 int ignored = 0;
2057 int cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 int recv_room;
2059 int max = 256;
2060 unsigned long flags;
2061
2062 spin_lock_irqsave(&info->slock, flags);
2063
Alan Cox33f0f882006-01-09 20:54:13 -08002064 recv_room = tty->receive_room;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 if ((recv_room == 0) && (!info->ldisc_stop_rx)) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002066 /* mxser_throttle(tty); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 mxser_stoprx(tty);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002068 /* return; */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 }
2070
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002071 /* following add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 if (info->IsMoxaMustChipFlag != MOXA_OTHER_UART) {
2073
2074 if (*status & UART_LSR_SPECIAL) {
2075 goto intr_old;
2076 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002077 /* following add by Victor Yu. 02-11-2004 */
2078 if (info->IsMoxaMustChipFlag == MOXA_MUST_MU860_HWID &&
2079 (*status & MOXA_MUST_LSR_RERR))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 goto intr_old;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002081 /* above add by Victor Yu. 02-14-2004 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 if (*status & MOXA_MUST_LSR_RERR)
2083 goto intr_old;
2084
2085 gdl = inb(info->base + MOXA_MUST_GDL_REGISTER);
2086
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002087 /* add by Victor Yu. 02-11-2004 */
2088 if (info->IsMoxaMustChipFlag == MOXA_MUST_MU150_HWID)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 gdl &= MOXA_MUST_GDL_MASK;
2090 if (gdl >= recv_room) {
2091 if (!info->ldisc_stop_rx) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002092 /* mxser_throttle(tty); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 mxser_stoprx(tty);
2094 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002095 /* return; */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 }
2097 while (gdl--) {
2098 ch = inb(info->base + UART_RX);
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002099 tty_insert_flip_char(tty, ch, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 cnt++;
2101 /*
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002102 if ((cnt >= HI_WATER) && (info->stop_rx == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 mxser_stoprx(tty);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002104 info->stop_rx = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 break;
2106 } */
2107 }
2108 goto end_intr;
2109 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002110 intr_old:
2111 /* above add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112
2113 do {
2114 if (max-- < 0)
2115 break;
2116 /*
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002117 if ((cnt >= HI_WATER) && (info->stop_rx == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 mxser_stoprx(tty);
2119 info->stop_rx=1;
2120 break;
2121 }
2122 */
2123
2124 ch = inb(info->base + UART_RX);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002125 /* following add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 if (info->IsMoxaMustChipFlag && (*status & UART_LSR_OE) /*&& !(*status&UART_LSR_DR) */ )
2127 outb(0x23, info->base + UART_FCR);
2128 *status &= info->read_status_mask;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002129 /* above add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 if (*status & info->ignore_status_mask) {
2131 if (++ignored > 100)
2132 break;
2133 } else {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002134 char flag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 if (*status & UART_LSR_SPECIAL) {
2136 if (*status & UART_LSR_BI) {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002137 flag = TTY_BREAK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138/* added by casper 1/11/2000 */
2139 info->icount.brk++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140/* */
2141 if (info->flags & ASYNC_SAK)
2142 do_SAK(tty);
2143 } else if (*status & UART_LSR_PE) {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002144 flag = TTY_PARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145/* added by casper 1/11/2000 */
2146 info->icount.parity++;
2147/* */
2148 } else if (*status & UART_LSR_FE) {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002149 flag = TTY_FRAME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150/* added by casper 1/11/2000 */
2151 info->icount.frame++;
2152/* */
2153 } else if (*status & UART_LSR_OE) {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002154 flag = TTY_OVERRUN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155/* added by casper 1/11/2000 */
2156 info->icount.overrun++;
2157/* */
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002158 }
2159 }
2160 tty_insert_flip_char(tty, ch, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 cnt++;
2162 if (cnt >= recv_room) {
2163 if (!info->ldisc_stop_rx) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002164 /* mxser_throttle(tty); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 mxser_stoprx(tty);
2166 }
2167 break;
2168 }
2169
2170 }
2171
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002172 /* following add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 if (info->IsMoxaMustChipFlag)
2174 break;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002175 /* above add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176
2177 /* mask by Victor Yu. 09-02-2002
2178 *status = inb(info->base + UART_LSR) & info->read_status_mask;
2179 */
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002180 /* following add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 *status = inb(info->base + UART_LSR);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002182 /* above add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 } while (*status & UART_LSR_DR);
2184
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002185end_intr: /* add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 mxvar_log.rxcnt[info->port] += cnt;
2187 info->mon_data.rxcnt += cnt;
2188 info->mon_data.up_rxcnt += cnt;
2189 spin_unlock_irqrestore(&info->slock, flags);
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002190
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 tty_flip_buffer_push(tty);
2192}
2193
2194static void mxser_transmit_chars(struct mxser_struct *info)
2195{
2196 int count, cnt;
2197 unsigned long flags;
2198
2199 spin_lock_irqsave(&info->slock, flags);
2200
2201 if (info->x_char) {
2202 outb(info->x_char, info->base + UART_TX);
2203 info->x_char = 0;
2204 mxvar_log.txcnt[info->port]++;
2205 info->mon_data.txcnt++;
2206 info->mon_data.up_txcnt++;
2207
2208/* added by casper 1/11/2000 */
2209 info->icount.tx++;
2210/* */
2211 spin_unlock_irqrestore(&info->slock, flags);
2212 return;
2213 }
2214
2215 if (info->xmit_buf == 0) {
2216 spin_unlock_irqrestore(&info->slock, flags);
2217 return;
2218 }
2219
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002220 if ((info->xmit_cnt <= 0) || info->tty->stopped ||
2221 (info->tty->hw_stopped &&
2222 (info->type != PORT_16550A) &&
2223 (!info->IsMoxaMustChipFlag))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 info->IER &= ~UART_IER_THRI;
2225 outb(info->IER, info->base + UART_IER);
2226 spin_unlock_irqrestore(&info->slock, flags);
2227 return;
2228 }
2229
2230 cnt = info->xmit_cnt;
2231 count = info->xmit_fifo_size;
2232 do {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002233 outb(info->xmit_buf[info->xmit_tail++],
2234 info->base + UART_TX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE - 1);
2236 if (--info->xmit_cnt <= 0)
2237 break;
2238 } while (--count > 0);
2239 mxvar_log.txcnt[info->port] += (cnt - info->xmit_cnt);
2240
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002241/* added by James 03-12-2004. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 info->mon_data.txcnt += (cnt - info->xmit_cnt);
2243 info->mon_data.up_txcnt += (cnt - info->xmit_cnt);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002244/* (above) added by James. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245
2246/* added by casper 1/11/2000 */
2247 info->icount.tx += (cnt - info->xmit_cnt);
2248/* */
2249
2250 if (info->xmit_cnt < WAKEUP_CHARS) {
2251 set_bit(MXSER_EVENT_TXLOW, &info->event);
2252 schedule_work(&info->tqueue);
2253 }
2254 if (info->xmit_cnt <= 0) {
2255 info->IER &= ~UART_IER_THRI;
2256 outb(info->IER, info->base + UART_IER);
2257 }
2258 spin_unlock_irqrestore(&info->slock, flags);
2259}
2260
2261static void mxser_check_modem_status(struct mxser_struct *info, int status)
2262{
2263 /* update input line counters */
2264 if (status & UART_MSR_TERI)
2265 info->icount.rng++;
2266 if (status & UART_MSR_DDSR)
2267 info->icount.dsr++;
2268 if (status & UART_MSR_DDCD)
2269 info->icount.dcd++;
2270 if (status & UART_MSR_DCTS)
2271 info->icount.cts++;
2272 info->mon_data.modem_status = status;
2273 wake_up_interruptible(&info->delta_msr_wait);
2274
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 if ((info->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
2276 if (status & UART_MSR_DCD)
2277 wake_up_interruptible(&info->open_wait);
2278 schedule_work(&info->tqueue);
2279 }
2280
2281 if (info->flags & ASYNC_CTS_FLOW) {
2282 if (info->tty->hw_stopped) {
2283 if (status & UART_MSR_CTS) {
2284 info->tty->hw_stopped = 0;
2285
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002286 if ((info->type != PORT_16550A) &&
2287 (!info->IsMoxaMustChipFlag)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 info->IER |= UART_IER_THRI;
2289 outb(info->IER, info->base + UART_IER);
2290 }
2291 set_bit(MXSER_EVENT_TXLOW, &info->event);
2292 schedule_work(&info->tqueue); }
2293 } else {
2294 if (!(status & UART_MSR_CTS)) {
2295 info->tty->hw_stopped = 1;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002296 if ((info->type != PORT_16550A) &&
2297 (!info->IsMoxaMustChipFlag)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 info->IER &= ~UART_IER_THRI;
2299 outb(info->IER, info->base + UART_IER);
2300 }
2301 }
2302 }
2303 }
2304}
2305
2306static int mxser_block_til_ready(struct tty_struct *tty, struct file *filp, struct mxser_struct *info)
2307{
2308 DECLARE_WAITQUEUE(wait, current);
2309 int retval;
2310 int do_clocal = 0;
2311 unsigned long flags;
2312
2313 /*
2314 * If non-blocking mode is set, or the port is not enabled,
2315 * then make the check up front and then exit.
2316 */
2317 if ((filp->f_flags & O_NONBLOCK) || (tty->flags & (1 << TTY_IO_ERROR))) {
2318 info->flags |= ASYNC_NORMAL_ACTIVE;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002319 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 }
2321
2322 if (tty->termios->c_cflag & CLOCAL)
2323 do_clocal = 1;
2324
2325 /*
2326 * Block waiting for the carrier detect and the line to become
2327 * free (i.e., not in use by the callout). While we are in
2328 * this loop, info->count is dropped by one, so that
2329 * mxser_close() knows when to free things. We restore it upon
2330 * exit, either normal or abnormal.
2331 */
2332 retval = 0;
2333 add_wait_queue(&info->open_wait, &wait);
2334
2335 spin_lock_irqsave(&info->slock, flags);
2336 if (!tty_hung_up_p(filp))
2337 info->count--;
2338 spin_unlock_irqrestore(&info->slock, flags);
2339 info->blocked_open++;
2340 while (1) {
2341 spin_lock_irqsave(&info->slock, flags);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002342 outb(inb(info->base + UART_MCR) |
2343 UART_MCR_DTR | UART_MCR_RTS, info->base + UART_MCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 spin_unlock_irqrestore(&info->slock, flags);
2345 set_current_state(TASK_INTERRUPTIBLE);
2346 if (tty_hung_up_p(filp) || !(info->flags & ASYNC_INITIALIZED)) {
2347 if (info->flags & ASYNC_HUP_NOTIFY)
2348 retval = -EAGAIN;
2349 else
2350 retval = -ERESTARTSYS;
2351 break;
2352 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002353 if (!(info->flags & ASYNC_CLOSING) &&
2354 (do_clocal ||
2355 (inb(info->base + UART_MSR) & UART_MSR_DCD)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 break;
2357 if (signal_pending(current)) {
2358 retval = -ERESTARTSYS;
2359 break;
2360 }
2361 schedule();
2362 }
2363 set_current_state(TASK_RUNNING);
2364 remove_wait_queue(&info->open_wait, &wait);
2365 if (!tty_hung_up_p(filp))
2366 info->count++;
2367 info->blocked_open--;
2368 if (retval)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002369 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 info->flags |= ASYNC_NORMAL_ACTIVE;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002371 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372}
2373
2374static int mxser_startup(struct mxser_struct *info)
2375{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 unsigned long page;
2377 unsigned long flags;
2378
2379 page = __get_free_page(GFP_KERNEL);
2380 if (!page)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002381 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382
2383 spin_lock_irqsave(&info->slock, flags);
2384
2385 if (info->flags & ASYNC_INITIALIZED) {
2386 free_page(page);
2387 spin_unlock_irqrestore(&info->slock, flags);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002388 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 }
2390
2391 if (!info->base || !info->type) {
2392 if (info->tty)
2393 set_bit(TTY_IO_ERROR, &info->tty->flags);
2394 free_page(page);
2395 spin_unlock_irqrestore(&info->slock, flags);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002396 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 }
2398 if (info->xmit_buf)
2399 free_page(page);
2400 else
2401 info->xmit_buf = (unsigned char *) page;
2402
2403 /*
2404 * Clear the FIFO buffers and disable them
2405 * (they will be reenabled in mxser_change_speed())
2406 */
2407 if (info->IsMoxaMustChipFlag)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002408 outb((UART_FCR_CLEAR_RCVR |
2409 UART_FCR_CLEAR_XMIT |
2410 MOXA_MUST_FCR_GDA_MODE_ENABLE), info->base + UART_FCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 else
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002412 outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
2413 info->base + UART_FCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414
2415 /*
2416 * At this point there's no way the LSR could still be 0xFF;
2417 * if it is, then bail out, because there's likely no UART
2418 * here.
2419 */
2420 if (inb(info->base + UART_LSR) == 0xff) {
2421 spin_unlock_irqrestore(&info->slock, flags);
2422 if (capable(CAP_SYS_ADMIN)) {
2423 if (info->tty)
2424 set_bit(TTY_IO_ERROR, &info->tty->flags);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002425 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 } else
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002427 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 }
2429
2430 /*
2431 * Clear the interrupt registers.
2432 */
2433 (void) inb(info->base + UART_LSR);
2434 (void) inb(info->base + UART_RX);
2435 (void) inb(info->base + UART_IIR);
2436 (void) inb(info->base + UART_MSR);
2437
2438 /*
2439 * Now, initialize the UART
2440 */
2441 outb(UART_LCR_WLEN8, info->base + UART_LCR); /* reset DLAB */
2442 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
2443 outb(info->MCR, info->base + UART_MCR);
2444
2445 /*
2446 * Finally, enable interrupts
2447 */
2448 info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002449 /* info->IER = UART_IER_RLSI | UART_IER_RDI; */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002451 /* following add by Victor Yu. 08-30-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 if (info->IsMoxaMustChipFlag)
2453 info->IER |= MOXA_MUST_IER_EGDAI;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002454 /* above add by Victor Yu. 08-30-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 outb(info->IER, info->base + UART_IER); /* enable interrupts */
2456
2457 /*
2458 * And clear the interrupt registers again for luck.
2459 */
2460 (void) inb(info->base + UART_LSR);
2461 (void) inb(info->base + UART_RX);
2462 (void) inb(info->base + UART_IIR);
2463 (void) inb(info->base + UART_MSR);
2464
2465 if (info->tty)
2466 clear_bit(TTY_IO_ERROR, &info->tty->flags);
2467 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
2468
2469 /*
2470 * and set the speed of the serial port
2471 */
2472 spin_unlock_irqrestore(&info->slock, flags);
2473 mxser_change_speed(info, NULL);
2474
2475 info->flags |= ASYNC_INITIALIZED;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002476 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477}
2478
2479/*
2480 * This routine will shutdown a serial port; interrupts maybe disabled, and
2481 * DTR is dropped if the hangup on close termio flag is on.
2482 */
2483static void mxser_shutdown(struct mxser_struct *info)
2484{
2485 unsigned long flags;
2486
2487 if (!(info->flags & ASYNC_INITIALIZED))
2488 return;
2489
2490 spin_lock_irqsave(&info->slock, flags);
2491
2492 /*
2493 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
2494 * here so the queue might never be waken up
2495 */
2496 wake_up_interruptible(&info->delta_msr_wait);
2497
2498 /*
2499 * Free the IRQ, if necessary
2500 */
2501 if (info->xmit_buf) {
2502 free_page((unsigned long) info->xmit_buf);
2503 info->xmit_buf = NULL;
2504 }
2505
2506 info->IER = 0;
2507 outb(0x00, info->base + UART_IER);
2508
2509 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
2510 info->MCR &= ~(UART_MCR_DTR | UART_MCR_RTS);
2511 outb(info->MCR, info->base + UART_MCR);
2512
2513 /* clear Rx/Tx FIFO's */
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002514 /* following add by Victor Yu. 08-30-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 if (info->IsMoxaMustChipFlag)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002516 outb((UART_FCR_CLEAR_RCVR |
2517 UART_FCR_CLEAR_XMIT |
2518 MOXA_MUST_FCR_GDA_MODE_ENABLE), info->base + UART_FCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 else
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002520 /* above add by Victor Yu. 08-30-2002 */
2521 outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
2522 info->base + UART_FCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523
2524 /* read data port to reset things */
2525 (void) inb(info->base + UART_RX);
2526
2527 if (info->tty)
2528 set_bit(TTY_IO_ERROR, &info->tty->flags);
2529
2530 info->flags &= ~ASYNC_INITIALIZED;
2531
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002532 /* following add by Victor Yu. 09-23-2002 */
2533 if (info->IsMoxaMustChipFlag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 SET_MOXA_MUST_NO_SOFTWARE_FLOW_CONTROL(info->base);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002535 /* above add by Victor Yu. 09-23-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536
2537 spin_unlock_irqrestore(&info->slock, flags);
2538}
2539
2540/*
2541 * This routine is called to set the UART divisor registers to match
2542 * the specified baud rate for a serial port.
2543 */
2544static int mxser_change_speed(struct mxser_struct *info, struct termios *old_termios)
2545{
2546 unsigned cflag, cval, fcr;
2547 int ret = 0;
2548 unsigned char status;
2549 long baud;
2550 unsigned long flags;
2551
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 if (!info->tty || !info->tty->termios)
2553 return ret;
2554 cflag = info->tty->termios->c_cflag;
2555 if (!(info->base))
2556 return ret;
2557
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558#ifndef B921600
2559#define B921600 (B460800 +1)
2560#endif
2561 if (mxser_set_baud_method[info->port] == 0) {
Alan Coxc7bce302006-09-30 23:27:24 -07002562 baud = tty_get_baud_rate(info->tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 mxser_set_baud(info, baud);
2564 }
2565
2566 /* byte size and parity */
2567 switch (cflag & CSIZE) {
2568 case CS5:
2569 cval = 0x00;
2570 break;
2571 case CS6:
2572 cval = 0x01;
2573 break;
2574 case CS7:
2575 cval = 0x02;
2576 break;
2577 case CS8:
2578 cval = 0x03;
2579 break;
2580 default:
2581 cval = 0x00;
2582 break; /* too keep GCC shut... */
2583 }
2584 if (cflag & CSTOPB)
2585 cval |= 0x04;
2586 if (cflag & PARENB)
2587 cval |= UART_LCR_PARITY;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002588 if (!(cflag & PARODD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 cval |= UART_LCR_EPAR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 if (cflag & CMSPAR)
2591 cval |= UART_LCR_SPAR;
2592
2593 if ((info->type == PORT_8250) || (info->type == PORT_16450)) {
2594 if (info->IsMoxaMustChipFlag) {
2595 fcr = UART_FCR_ENABLE_FIFO;
2596 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
2597 SET_MOXA_MUST_FIFO_VALUE(info);
2598 } else
2599 fcr = 0;
2600 } else {
2601 fcr = UART_FCR_ENABLE_FIFO;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002602 /* following add by Victor Yu. 08-30-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 if (info->IsMoxaMustChipFlag) {
2604 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
2605 SET_MOXA_MUST_FIFO_VALUE(info);
2606 } else {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002607 /* above add by Victor Yu. 08-30-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 switch (info->rx_trigger) {
2609 case 1:
2610 fcr |= UART_FCR_TRIGGER_1;
2611 break;
2612 case 4:
2613 fcr |= UART_FCR_TRIGGER_4;
2614 break;
2615 case 8:
2616 fcr |= UART_FCR_TRIGGER_8;
2617 break;
2618 default:
2619 fcr |= UART_FCR_TRIGGER_14;
2620 break;
2621 }
2622 }
2623 }
2624
2625 /* CTS flow control flag and modem status interrupts */
2626 info->IER &= ~UART_IER_MSI;
2627 info->MCR &= ~UART_MCR_AFE;
2628 if (cflag & CRTSCTS) {
2629 info->flags |= ASYNC_CTS_FLOW;
2630 info->IER |= UART_IER_MSI;
2631 if ((info->type == PORT_16550A) || (info->IsMoxaMustChipFlag)) {
2632 info->MCR |= UART_MCR_AFE;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002633 /* status = mxser_get_msr(info->base, 0, info->port); */
2634/*
2635 save_flags(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 cli();
2637 status = inb(baseaddr + UART_MSR);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002638 restore_flags(flags);
2639*/
2640 /* mxser_check_modem_status(info, status); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 } else {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002642 /* status = mxser_get_msr(info->base, 0, info->port); */
2643 /* MX_LOCK(&info->slock); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 status = inb(info->base + UART_MSR);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002645 /* MX_UNLOCK(&info->slock); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 if (info->tty->hw_stopped) {
2647 if (status & UART_MSR_CTS) {
2648 info->tty->hw_stopped = 0;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002649 if ((info->type != PORT_16550A) &&
2650 (!info->IsMoxaMustChipFlag)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 info->IER |= UART_IER_THRI;
2652 outb(info->IER, info->base + UART_IER);
2653 }
2654 set_bit(MXSER_EVENT_TXLOW, &info->event);
2655 schedule_work(&info->tqueue); }
2656 } else {
2657 if (!(status & UART_MSR_CTS)) {
2658 info->tty->hw_stopped = 1;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002659 if ((info->type != PORT_16550A) &&
2660 (!info->IsMoxaMustChipFlag)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 info->IER &= ~UART_IER_THRI;
2662 outb(info->IER, info->base + UART_IER);
2663 }
2664 }
2665 }
2666 }
2667 } else {
2668 info->flags &= ~ASYNC_CTS_FLOW;
2669 }
2670 outb(info->MCR, info->base + UART_MCR);
2671 if (cflag & CLOCAL) {
2672 info->flags &= ~ASYNC_CHECK_CD;
2673 } else {
2674 info->flags |= ASYNC_CHECK_CD;
2675 info->IER |= UART_IER_MSI;
2676 }
2677 outb(info->IER, info->base + UART_IER);
2678
2679 /*
2680 * Set up parity check flag
2681 */
2682 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
2683 if (I_INPCK(info->tty))
2684 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
2685 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
2686 info->read_status_mask |= UART_LSR_BI;
2687
2688 info->ignore_status_mask = 0;
2689
2690 if (I_IGNBRK(info->tty)) {
2691 info->ignore_status_mask |= UART_LSR_BI;
2692 info->read_status_mask |= UART_LSR_BI;
2693 /*
2694 * If we're ignore parity and break indicators, ignore
2695 * overruns too. (For real raw support).
2696 */
2697 if (I_IGNPAR(info->tty)) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002698 info->ignore_status_mask |=
2699 UART_LSR_OE |
2700 UART_LSR_PE |
2701 UART_LSR_FE;
2702 info->read_status_mask |=
2703 UART_LSR_OE |
2704 UART_LSR_PE |
2705 UART_LSR_FE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 }
2707 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002708 /* following add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 if (info->IsMoxaMustChipFlag) {
2710 spin_lock_irqsave(&info->slock, flags);
2711 SET_MOXA_MUST_XON1_VALUE(info->base, START_CHAR(info->tty));
2712 SET_MOXA_MUST_XOFF1_VALUE(info->base, STOP_CHAR(info->tty));
2713 if (I_IXON(info->tty)) {
2714 ENABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->base);
2715 } else {
2716 DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->base);
2717 }
2718 if (I_IXOFF(info->tty)) {
2719 ENABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->base);
2720 } else {
2721 DISABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->base);
2722 }
2723 /*
2724 if ( I_IXANY(info->tty) ) {
2725 info->MCR |= MOXA_MUST_MCR_XON_ANY;
2726 ENABLE_MOXA_MUST_XON_ANY_FLOW_CONTROL(info->base);
2727 } else {
2728 info->MCR &= ~MOXA_MUST_MCR_XON_ANY;
2729 DISABLE_MOXA_MUST_XON_ANY_FLOW_CONTROL(info->base);
2730 }
2731 */
2732 spin_unlock_irqrestore(&info->slock, flags);
2733 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002734 /* above add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735
2736
2737 outb(fcr, info->base + UART_FCR); /* set fcr */
2738 outb(cval, info->base + UART_LCR);
2739
2740 return ret;
2741}
2742
2743
2744static int mxser_set_baud(struct mxser_struct *info, long newspd)
2745{
2746 int quot = 0;
2747 unsigned char cval;
2748 int ret = 0;
2749 unsigned long flags;
2750
2751 if (!info->tty || !info->tty->termios)
2752 return ret;
2753
2754 if (!(info->base))
2755 return ret;
2756
2757 if (newspd > info->MaxCanSetBaudRate)
2758 return 0;
2759
2760 info->realbaud = newspd;
2761 if (newspd == 134) {
2762 quot = (2 * info->baud_base / 269);
2763 } else if (newspd) {
2764 quot = info->baud_base / newspd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 if (quot == 0)
2766 quot = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 } else {
2768 quot = 0;
2769 }
2770
2771 info->timeout = ((info->xmit_fifo_size * HZ * 10 * quot) / info->baud_base);
2772 info->timeout += HZ / 50; /* Add .02 seconds of slop */
2773
2774 if (quot) {
2775 spin_lock_irqsave(&info->slock, flags);
2776 info->MCR |= UART_MCR_DTR;
2777 outb(info->MCR, info->base + UART_MCR);
2778 spin_unlock_irqrestore(&info->slock, flags);
2779 } else {
2780 spin_lock_irqsave(&info->slock, flags);
2781 info->MCR &= ~UART_MCR_DTR;
2782 outb(info->MCR, info->base + UART_MCR);
2783 spin_unlock_irqrestore(&info->slock, flags);
2784 return ret;
2785 }
2786
2787 cval = inb(info->base + UART_LCR);
2788
2789 outb(cval | UART_LCR_DLAB, info->base + UART_LCR); /* set DLAB */
2790
2791 outb(quot & 0xff, info->base + UART_DLL); /* LS of divisor */
2792 outb(quot >> 8, info->base + UART_DLM); /* MS of divisor */
2793 outb(cval, info->base + UART_LCR); /* reset DLAB */
2794
2795
2796 return ret;
2797}
2798
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799/*
2800 * ------------------------------------------------------------
2801 * friends of mxser_ioctl()
2802 * ------------------------------------------------------------
2803 */
2804static int mxser_get_serial_info(struct mxser_struct *info, struct serial_struct __user *retinfo)
2805{
2806 struct serial_struct tmp;
2807
2808 if (!retinfo)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002809 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 memset(&tmp, 0, sizeof(tmp));
2811 tmp.type = info->type;
2812 tmp.line = info->port;
2813 tmp.port = info->base;
2814 tmp.irq = info->irq;
2815 tmp.flags = info->flags;
2816 tmp.baud_base = info->baud_base;
2817 tmp.close_delay = info->close_delay;
2818 tmp.closing_wait = info->closing_wait;
2819 tmp.custom_divisor = info->custom_divisor;
2820 tmp.hub6 = 0;
2821 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2822 return -EFAULT;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002823 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824}
2825
2826static int mxser_set_serial_info(struct mxser_struct *info, struct serial_struct __user *new_info)
2827{
2828 struct serial_struct new_serial;
2829 unsigned int flags;
2830 int retval = 0;
2831
2832 if (!new_info || !info->base)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002833 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
2835 return -EFAULT;
2836
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002837 if ((new_serial.irq != info->irq) ||
2838 (new_serial.port != info->base) ||
2839 (new_serial.custom_divisor != info->custom_divisor) ||
2840 (new_serial.baud_base != info->baud_base))
2841 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842
2843 flags = info->flags & ASYNC_SPD_MASK;
2844
2845 if (!capable(CAP_SYS_ADMIN)) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002846 if ((new_serial.baud_base != info->baud_base) ||
2847 (new_serial.close_delay != info->close_delay) ||
2848 ((new_serial.flags & ~ASYNC_USR_MASK) != (info->flags & ~ASYNC_USR_MASK)))
2849 return -EPERM;
2850 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
2851 (new_serial.flags & ASYNC_USR_MASK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 } else {
2853 /*
2854 * OK, past this point, all the error checking has been done.
2855 * At this point, we start making changes.....
2856 */
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002857 info->flags = ((info->flags & ~ASYNC_FLAGS) |
2858 (new_serial.flags & ASYNC_FLAGS));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 info->close_delay = new_serial.close_delay * HZ / 100;
2860 info->closing_wait = new_serial.closing_wait * HZ / 100;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002861 info->tty->low_latency =
2862 (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
2863 info->tty->low_latency = 0; /* (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0; */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 }
2865
2866 /* added by casper, 3/17/2000, for mouse */
2867 info->type = new_serial.type;
2868
2869 process_txrx_fifo(info);
2870
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 if (info->flags & ASYNC_INITIALIZED) {
2872 if (flags != (info->flags & ASYNC_SPD_MASK)) {
2873 mxser_change_speed(info, NULL);
2874 }
2875 } else {
2876 retval = mxser_startup(info);
2877 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002878 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879}
2880
2881/*
2882 * mxser_get_lsr_info - get line status register info
2883 *
2884 * Purpose: Let user call ioctl() to get info when the UART physically
2885 * is emptied. On bus types like RS485, the transmitter must
2886 * release the bus after transmitting. This must be done when
2887 * the transmit shift register is empty, not be done when the
2888 * transmit holding register is empty. This functionality
2889 * allows an RS485 driver to be written in user space.
2890 */
2891static int mxser_get_lsr_info(struct mxser_struct *info, unsigned int __user *value)
2892{
2893 unsigned char status;
2894 unsigned int result;
2895 unsigned long flags;
2896
2897 spin_lock_irqsave(&info->slock, flags);
2898 status = inb(info->base + UART_LSR);
2899 spin_unlock_irqrestore(&info->slock, flags);
2900 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
2901 return put_user(result, value);
2902}
2903
2904/*
2905 * This routine sends a break character out the serial port.
2906 */
2907static void mxser_send_break(struct mxser_struct *info, int duration)
2908{
2909 unsigned long flags;
2910
2911 if (!info->base)
2912 return;
2913 set_current_state(TASK_INTERRUPTIBLE);
2914 spin_lock_irqsave(&info->slock, flags);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002915 outb(inb(info->base + UART_LCR) | UART_LCR_SBC,
2916 info->base + UART_LCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 spin_unlock_irqrestore(&info->slock, flags);
2918 schedule_timeout(duration);
2919 spin_lock_irqsave(&info->slock, flags);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002920 outb(inb(info->base + UART_LCR) & ~UART_LCR_SBC,
2921 info->base + UART_LCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 spin_unlock_irqrestore(&info->slock, flags);
2923}
2924
2925static int mxser_tiocmget(struct tty_struct *tty, struct file *file)
2926{
Jesper Juhl56e139f2006-06-25 05:47:52 -07002927 struct mxser_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 unsigned char control, status;
2929 unsigned long flags;
2930
2931
2932 if (tty->index == MXSER_PORTS)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002933 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 if (tty->flags & (1 << TTY_IO_ERROR))
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002935 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936
2937 control = info->MCR;
2938
2939 spin_lock_irqsave(&info->slock, flags);
2940 status = inb(info->base + UART_MSR);
2941 if (status & UART_MSR_ANY_DELTA)
2942 mxser_check_modem_status(info, status);
2943 spin_unlock_irqrestore(&info->slock, flags);
2944 return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0) |
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002945 ((control & UART_MCR_DTR) ? TIOCM_DTR : 0) |
2946 ((status & UART_MSR_DCD) ? TIOCM_CAR : 0) |
2947 ((status & UART_MSR_RI) ? TIOCM_RNG : 0) |
2948 ((status & UART_MSR_DSR) ? TIOCM_DSR : 0) |
2949 ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950}
2951
2952static int mxser_tiocmset(struct tty_struct *tty, struct file *file, unsigned int set, unsigned int clear)
2953{
Jesper Juhl56e139f2006-06-25 05:47:52 -07002954 struct mxser_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 unsigned long flags;
2956
2957
2958 if (tty->index == MXSER_PORTS)
2959 return -ENOIOCTLCMD;
2960 if (tty->flags & (1 << TTY_IO_ERROR))
2961 return -EIO;
2962
2963 spin_lock_irqsave(&info->slock, flags);
2964
2965 if (set & TIOCM_RTS)
2966 info->MCR |= UART_MCR_RTS;
2967 if (set & TIOCM_DTR)
2968 info->MCR |= UART_MCR_DTR;
2969
2970 if (clear & TIOCM_RTS)
2971 info->MCR &= ~UART_MCR_RTS;
2972 if (clear & TIOCM_DTR)
2973 info->MCR &= ~UART_MCR_DTR;
2974
2975 outb(info->MCR, info->base + UART_MCR);
2976 spin_unlock_irqrestore(&info->slock, flags);
2977 return 0;
2978}
2979
2980
2981static int mxser_read_register(int, unsigned short *);
2982static int mxser_program_mode(int);
2983static void mxser_normal_mode(int);
2984
2985static int mxser_get_ISA_conf(int cap, struct mxser_hwconf *hwconf)
2986{
2987 int id, i, bits;
2988 unsigned short regs[16], irq;
2989 unsigned char scratch, scratch2;
2990
2991 hwconf->IsMoxaMustChipFlag = MOXA_OTHER_UART;
2992
2993 id = mxser_read_register(cap, regs);
2994 if (id == C168_ASIC_ID) {
2995 hwconf->board_type = MXSER_BOARD_C168_ISA;
2996 hwconf->ports = 8;
2997 } else if (id == C104_ASIC_ID) {
2998 hwconf->board_type = MXSER_BOARD_C104_ISA;
2999 hwconf->ports = 4;
3000 } else if (id == C102_ASIC_ID) {
3001 hwconf->board_type = MXSER_BOARD_C102_ISA;
3002 hwconf->ports = 2;
3003 } else if (id == CI132_ASIC_ID) {
3004 hwconf->board_type = MXSER_BOARD_CI132;
3005 hwconf->ports = 2;
3006 } else if (id == CI134_ASIC_ID) {
3007 hwconf->board_type = MXSER_BOARD_CI134;
3008 hwconf->ports = 4;
3009 } else if (id == CI104J_ASIC_ID) {
3010 hwconf->board_type = MXSER_BOARD_CI104J;
3011 hwconf->ports = 4;
3012 } else
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07003013 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014
3015 irq = 0;
3016 if (hwconf->ports == 2) {
3017 irq = regs[9] & 0xF000;
3018 irq = irq | (irq >> 4);
3019 if (irq != (regs[9] & 0xFF00))
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07003020 return MXSER_ERR_IRQ_CONFLIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 } else if (hwconf->ports == 4) {
3022 irq = regs[9] & 0xF000;
3023 irq = irq | (irq >> 4);
3024 irq = irq | (irq >> 8);
3025 if (irq != regs[9])
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07003026 return MXSER_ERR_IRQ_CONFLIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 } else if (hwconf->ports == 8) {
3028 irq = regs[9] & 0xF000;
3029 irq = irq | (irq >> 4);
3030 irq = irq | (irq >> 8);
3031 if ((irq != regs[9]) || (irq != regs[10]))
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07003032 return MXSER_ERR_IRQ_CONFLIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 }
3034
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07003035 if (!irq)
3036 return MXSER_ERR_IRQ;
3037 hwconf->irq = ((int)(irq & 0xF000) >> 12);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 for (i = 0; i < 8; i++)
3039 hwconf->ioaddr[i] = (int) regs[i + 1] & 0xFFF8;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07003040 if ((regs[12] & 0x80) == 0)
3041 return MXSER_ERR_VECTOR;
3042 hwconf->vector = (int)regs[11]; /* interrupt vector */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 if (id == 1)
3044 hwconf->vector_mask = 0x00FF;
3045 else
3046 hwconf->vector_mask = 0x000F;
3047 for (i = 7, bits = 0x0100; i >= 0; i--, bits <<= 1) {
3048 if (regs[12] & bits) {
3049 hwconf->baud_base[i] = 921600;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07003050 hwconf->MaxCanSetBaudRate[i] = 921600; /* add by Victor Yu. 09-04-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 } else {
3052 hwconf->baud_base[i] = 115200;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07003053 hwconf->MaxCanSetBaudRate[i] = 115200; /* add by Victor Yu. 09-04-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 }
3055 }
3056 scratch2 = inb(cap + UART_LCR) & (~UART_LCR_DLAB);
3057 outb(scratch2 | UART_LCR_DLAB, cap + UART_LCR);
3058 outb(0, cap + UART_EFR); /* EFR is the same as FCR */
3059 outb(scratch2, cap + UART_LCR);
3060 outb(UART_FCR_ENABLE_FIFO, cap + UART_FCR);
3061 scratch = inb(cap + UART_IIR);
3062
3063 if (scratch & 0xC0)
3064 hwconf->uart_type = PORT_16550A;
3065 else
3066 hwconf->uart_type = PORT_16450;
3067 if (id == 1)
3068 hwconf->ports = 8;
3069 else
3070 hwconf->ports = 4;
3071 request_region(hwconf->ioaddr[0], 8 * hwconf->ports, "mxser(IO)");
3072 request_region(hwconf->vector, 1, "mxser(vector)");
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07003073 return hwconf->ports;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074}
3075
3076#define CHIP_SK 0x01 /* Serial Data Clock in Eprom */
3077#define CHIP_DO 0x02 /* Serial Data Output in Eprom */
3078#define CHIP_CS 0x04 /* Serial Chip Select in Eprom */
3079#define CHIP_DI 0x08 /* Serial Data Input in Eprom */
3080#define EN_CCMD 0x000 /* Chip's command register */
3081#define EN0_RSARLO 0x008 /* Remote start address reg 0 */
3082#define EN0_RSARHI 0x009 /* Remote start address reg 1 */
3083#define EN0_RCNTLO 0x00A /* Remote byte count reg WR */
3084#define EN0_RCNTHI 0x00B /* Remote byte count reg WR */
3085#define EN0_DCFG 0x00E /* Data configuration reg WR */
3086#define EN0_PORT 0x010 /* Rcv missed frame error counter RD */
3087#define ENC_PAGE0 0x000 /* Select page 0 of chip registers */
3088#define ENC_PAGE3 0x0C0 /* Select page 3 of chip registers */
3089static int mxser_read_register(int port, unsigned short *regs)
3090{
3091 int i, k, value, id;
3092 unsigned int j;
3093
3094 id = mxser_program_mode(port);
3095 if (id < 0)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07003096 return id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 for (i = 0; i < 14; i++) {
3098 k = (i & 0x3F) | 0x180;
3099 for (j = 0x100; j > 0; j >>= 1) {
3100 outb(CHIP_CS, port);
3101 if (k & j) {
3102 outb(CHIP_CS | CHIP_DO, port);
3103 outb(CHIP_CS | CHIP_DO | CHIP_SK, port); /* A? bit of read */
3104 } else {
3105 outb(CHIP_CS, port);
3106 outb(CHIP_CS | CHIP_SK, port); /* A? bit of read */
3107 }
3108 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07003109 (void)inb(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 value = 0;
3111 for (k = 0, j = 0x8000; k < 16; k++, j >>= 1) {
3112 outb(CHIP_CS, port);
3113 outb(CHIP_CS | CHIP_SK, port);
3114 if (inb(port) & CHIP_DI)
3115 value |= j;
3116 }
3117 regs[i] = value;
3118 outb(0, port);
3119 }
3120 mxser_normal_mode(port);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07003121 return id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122}
3123
3124static int mxser_program_mode(int port)
3125{
3126 int id, i, j, n;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07003127 /* unsigned long flags; */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128
3129 spin_lock(&gm_lock);
3130 outb(0, port);
3131 outb(0, port);
3132 outb(0, port);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07003133 (void)inb(port);
3134 (void)inb(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 outb(0, port);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07003136 (void)inb(port);
3137 /* restore_flags(flags); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 spin_unlock(&gm_lock);
3139
3140 id = inb(port + 1) & 0x1F;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07003141 if ((id != C168_ASIC_ID) &&
3142 (id != C104_ASIC_ID) &&
3143 (id != C102_ASIC_ID) &&
3144 (id != CI132_ASIC_ID) &&
3145 (id != CI134_ASIC_ID) &&
3146 (id != CI104J_ASIC_ID))
3147 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 for (i = 0, j = 0; i < 4; i++) {
3149 n = inb(port + 2);
3150 if (n == 'M') {
3151 j = 1;
3152 } else if ((j == 1) && (n == 1)) {
3153 j = 2;
3154 break;
3155 } else
3156 j = 0;
3157 }
3158 if (j != 2)
3159 id = -2;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07003160 return id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161}
3162
3163static void mxser_normal_mode(int port)
3164{
3165 int i, n;
3166
3167 outb(0xA5, port + 1);
3168 outb(0x80, port + 3);
3169 outb(12, port + 0); /* 9600 bps */
3170 outb(0, port + 1);
3171 outb(0x03, port + 3); /* 8 data bits */
3172 outb(0x13, port + 4); /* loop back mode */
3173 for (i = 0; i < 16; i++) {
3174 n = inb(port + 5);
3175 if ((n & 0x61) == 0x60)
3176 break;
3177 if ((n & 1) == 1)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07003178 (void)inb(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 }
3180 outb(0x00, port + 4);
3181}
3182
3183module_init(mxser_module_init);
3184module_exit(mxser_module_exit);