blob: 83f604b19290706f3a04577c463a4cfeec029a0a [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;
Alan Cox606d0992006-12-08 02:38:45 -0800331 struct ktermios normal_termios;
332 struct ktermios callout_termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 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];
Alan Cox606d0992006-12-08 02:38:45 -0800367static struct ktermios *mxvar_termios[MXSER_PORTS + 1];
368static struct ktermios *mxvar_termios_locked[MXSER_PORTS + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369static 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 *);
Alan Cox606d0992006-12-08 02:38:45 -0800405static void mxser_set_termios(struct tty_struct *, struct ktermios *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406static 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 *);
Alan Cox606d0992006-12-08 02:38:45 -0800417static int mxser_change_speed(struct mxser_struct *, struct ktermios *old_termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418static 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));
Jiri Slaby31f87cf2006-12-29 16:47:34 -0800720 mxvar_sdriver->owner = THIS_MODULE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 mxvar_sdriver->magic = TTY_DRIVER_MAGIC;
Jiri Slabyae25f8e2006-12-08 02:38:09 -0800722 mxvar_sdriver->name = "ttyMI";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 mxvar_sdriver->major = ttymajor;
724 mxvar_sdriver->minor_start = 0;
725 mxvar_sdriver->num = MXSER_PORTS + 1;
726 mxvar_sdriver->type = TTY_DRIVER_TYPE_SERIAL;
727 mxvar_sdriver->subtype = SERIAL_TYPE_NORMAL;
728 mxvar_sdriver->init_termios = tty_std_termios;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700729 mxvar_sdriver->init_termios.c_cflag = B9600|CS8|CREAD|HUPCL|CLOCAL;
Alan Cox606d0992006-12-08 02:38:45 -0800730 mxvar_sdriver->init_termios.c_ispeed = 9600;
731 mxvar_sdriver->init_termios.c_ospeed = 9600;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 mxvar_sdriver->flags = TTY_DRIVER_REAL_RAW;
733 tty_set_operations(mxvar_sdriver, &mxser_ops);
734 mxvar_sdriver->ttys = mxvar_tty;
735 mxvar_sdriver->termios = mxvar_termios;
736 mxvar_sdriver->termios_locked = mxvar_termios_locked;
737
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 mxvar_diagflag = 0;
739 memset(mxvar_table, 0, MXSER_PORTS * sizeof(struct mxser_struct));
740 memset(&mxvar_log, 0, sizeof(struct mxser_log));
741
742 memset(&mxser_msr, 0, sizeof(unsigned char) * (MXSER_PORTS + 1));
743 memset(&mon_data_ext, 0, sizeof(struct mxser_mon_ext));
744 memset(&mxser_set_baud_method, 0, sizeof(int) * (MXSER_PORTS + 1));
745 memset(&hwconf, 0, sizeof(struct mxser_hwconf));
746
747 m = 0;
748 /* Start finding ISA boards here */
749 for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
750 int cap;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700751
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 if (!(cap = mxserBoardCAP[b]))
753 continue;
754
755 retval = mxser_get_ISA_conf(cap, &hwconf);
756
757 if (retval != 0)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700758 printk(KERN_INFO "Found MOXA %s board (CAP=0x%x)\n",
759 mxser_brdname[hwconf.board_type - 1], ioaddr[b]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
761 if (retval <= 0) {
762 if (retval == MXSER_ERR_IRQ)
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_IRQ_CONFLIT)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700766 printk(KERN_ERR "Invalid interrupt number, "
767 "board not configured\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 else if (retval == MXSER_ERR_VECTOR)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700769 printk(KERN_ERR "Invalid interrupt vector, "
770 "board not configured\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 else if (retval == MXSER_ERR_IOADDR)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700772 printk(KERN_ERR "Invalid I/O address, "
773 "board not configured\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
775 continue;
776 }
777
778 hwconf.pciInfo.busNum = 0;
779 hwconf.pciInfo.devNum = 0;
780 hwconf.pciInfo.pdev = NULL;
781
782 mxser_getcfg(m, &hwconf);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700783 /*
784 * init mxsercfg first,
785 * or mxsercfg data is not correct on ISR.
786 */
787 /* mxser_initbrd will hook ISR. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 if (mxser_initbrd(m, &hwconf) < 0)
789 continue;
790
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 m++;
792 }
793
794 /* Start finding ISA boards from module arg */
795 for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
796 int cap;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 if (!(cap = ioaddr[b]))
799 continue;
800
801 retval = mxser_get_ISA_conf(cap, &hwconf);
802
803 if (retval != 0)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700804 printk(KERN_INFO "Found MOXA %s board (CAP=0x%x)\n",
805 mxser_brdname[hwconf.board_type - 1], ioaddr[b]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
807 if (retval <= 0) {
808 if (retval == MXSER_ERR_IRQ)
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_IRQ_CONFLIT)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700812 printk(KERN_ERR "Invalid interrupt number, "
813 "board not configured\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 else if (retval == MXSER_ERR_VECTOR)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700815 printk(KERN_ERR "Invalid interrupt vector, "
816 "board not configured\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 else if (retval == MXSER_ERR_IOADDR)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700818 printk(KERN_ERR "Invalid I/O address, "
819 "board not configured\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
821 continue;
822 }
823
824 hwconf.pciInfo.busNum = 0;
825 hwconf.pciInfo.devNum = 0;
826 hwconf.pciInfo.pdev = NULL;
827
828 mxser_getcfg(m, &hwconf);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700829 /*
830 * init mxsercfg first,
831 * or mxsercfg data is not correct on ISR.
832 */
833 /* mxser_initbrd will hook ISR. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 if (mxser_initbrd(m, &hwconf) < 0)
835 continue;
836
837 m++;
838 }
839
840 /* start finding PCI board here */
841#ifdef CONFIG_PCI
Tobias Klauserfe971072006-01-09 20:54:02 -0800842 n = ARRAY_SIZE(mxser_pcibrds) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 index = 0;
844 b = 0;
845 while (b < n) {
Alan Cox1187ece2006-12-08 02:38:10 -0800846 pdev = pci_get_device(mxser_pcibrds[b].vendor,
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700847 mxser_pcibrds[b].device, pdev);
Alan Cox1187ece2006-12-08 02:38:10 -0800848 if (pdev == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 b++;
850 continue;
851 }
852 hwconf.pciInfo.busNum = busnum = pdev->bus->number;
853 hwconf.pciInfo.devNum = devnum = PCI_SLOT(pdev->devfn) << 3;
854 hwconf.pciInfo.pdev = pdev;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700855 printk(KERN_INFO "Found MOXA %s board(BusNo=%d,DevNo=%d)\n",
856 mxser_brdname[(int) (mxser_pcibrds[b].driver_data) - 1],
857 busnum, devnum >> 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 index++;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700859 if (m >= MXSER_BOARDS)
860 printk(KERN_ERR
861 "Too many Smartio/Industio family boards find "
862 "(maximum %d), board not configured\n",
863 MXSER_BOARDS);
864 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 if (pci_enable_device(pdev)) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700866 printk(KERN_ERR "Moxa SmartI/O PCI enable "
867 "fail !\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 continue;
869 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700870 retval = mxser_get_PCI_conf(busnum, devnum,
871 (int)mxser_pcibrds[b].driver_data,
872 &hwconf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 if (retval < 0) {
874 if (retval == MXSER_ERR_IRQ)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700875 printk(KERN_ERR
876 "Invalid interrupt number, "
877 "board not configured\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 else if (retval == MXSER_ERR_IRQ_CONFLIT)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700879 printk(KERN_ERR
880 "Invalid interrupt number, "
881 "board not configured\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 else if (retval == MXSER_ERR_VECTOR)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700883 printk(KERN_ERR
884 "Invalid interrupt vector, "
885 "board not configured\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 else if (retval == MXSER_ERR_IOADDR)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700887 printk(KERN_ERR
888 "Invalid I/O address, "
889 "board not configured\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 continue;
891 }
892 mxser_getcfg(m, &hwconf);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700893 /* init mxsercfg first,
894 * or mxsercfg data is not correct on ISR.
895 */
896 /* mxser_initbrd will hook ISR. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 if (mxser_initbrd(m, &hwconf) < 0)
898 continue;
899 m++;
Alan Cox1187ece2006-12-08 02:38:10 -0800900 /* Keep an extra reference if we succeeded. It will
901 be returned at unload time */
902 pci_dev_get(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 }
904 }
905#endif
906
Kirill Smelkov64698b62005-11-07 00:59:22 -0800907 retval = tty_register_driver(mxvar_sdriver);
908 if (retval) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700909 printk(KERN_ERR "Couldn't install MOXA Smartio/Industio family"
910 " driver !\n");
Kirill Smelkov64698b62005-11-07 00:59:22 -0800911 put_tty_driver(mxvar_sdriver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 for (i = 0; i < MXSER_BOARDS; i++) {
914 if (mxsercfg[i].board_type == -1)
915 continue;
916 else {
917 free_irq(mxsercfg[i].irq, &mxvar_table[i * MXSER_PORTS_PER_BOARD]);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700918 /* todo: release io, vector */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 }
920 }
Kirill Smelkov64698b62005-11-07 00:59:22 -0800921 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 }
923
Kirill Smelkov64698b62005-11-07 00:59:22 -0800924 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925}
926
David Howellsc4028952006-11-22 14:57:56 +0000927static void mxser_do_softint(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928{
David Howellsc4028952006-11-22 14:57:56 +0000929 struct mxser_struct *info =
930 container_of(work, struct mxser_struct, tqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 struct tty_struct *tty;
932
933 tty = info->tty;
934
935 if (tty) {
936 if (test_and_clear_bit(MXSER_EVENT_TXLOW, &info->event))
937 tty_wakeup(tty);
938 if (test_and_clear_bit(MXSER_EVENT_HANGUP, &info->event))
939 tty_hangup(tty);
940 }
941}
942
943static unsigned char mxser_get_msr(int baseaddr, int mode, int port, struct mxser_struct *info)
944{
945 unsigned char status = 0;
946
947 status = inb(baseaddr + UART_MSR);
948
949 mxser_msr[port] &= 0x0F;
950 mxser_msr[port] |= status;
951 status = mxser_msr[port];
952 if (mode)
953 mxser_msr[port] = 0;
954
955 return status;
956}
957
958/*
959 * This routine is called whenever a serial port is opened. It
960 * enables interrupts for a serial port, linking in its async structure into
961 * the IRQ chain. It also performs the serial-specific
962 * initialization for the tty structure.
963 */
964static int mxser_open(struct tty_struct *tty, struct file *filp)
965{
966 struct mxser_struct *info;
967 int retval, line;
968
Kirill Smelkov6f08b722005-11-07 00:59:23 -0800969 /* initialize driver_data in case something fails */
970 tty->driver_data = NULL;
971
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 line = tty->index;
973 if (line == MXSER_PORTS)
974 return 0;
975 if (line < 0 || line > MXSER_PORTS)
976 return -ENODEV;
977 info = mxvar_table + line;
978 if (!info->base)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700979 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
981 tty->driver_data = info;
982 info->tty = tty;
983 /*
984 * Start up serial port
985 */
986 retval = mxser_startup(info);
987 if (retval)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700988 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
990 retval = mxser_block_til_ready(tty, filp, info);
991 if (retval)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700992 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
994 info->count++;
995
996 if ((info->count == 1) && (info->flags & ASYNC_SPLIT_TERMIOS)) {
997 if (tty->driver->subtype == SERIAL_TYPE_NORMAL)
998 *tty->termios = info->normal_termios;
999 else
1000 *tty->termios = info->callout_termios;
1001 mxser_change_speed(info, NULL);
1002 }
1003
Cedric Le Goater937949d2006-12-08 02:37:54 -08001004 info->session = process_session(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 info->pgrp = process_group(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001007 /*
1008 status = mxser_get_msr(info->base, 0, info->port);
1009 mxser_check_modem_status(info, status);
1010 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001012/* unmark here for very high baud rate (ex. 921600 bps) used */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 tty->low_latency = 1;
1014 return 0;
1015}
1016
1017/*
1018 * This routine is called when the serial port gets closed. First, we
1019 * wait for the last remaining data to be sent. Then, we unlink its
1020 * async structure from the interrupt chain if necessary, and we free
1021 * that IRQ if nothing is left in the chain.
1022 */
1023static void mxser_close(struct tty_struct *tty, struct file *filp)
1024{
Jesper Juhl56e139f2006-06-25 05:47:52 -07001025 struct mxser_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
1027 unsigned long timeout;
1028 unsigned long flags;
1029 struct tty_ldisc *ld;
1030
1031 if (tty->index == MXSER_PORTS)
1032 return;
1033 if (!info)
Kirill Smelkov6f08b722005-11-07 00:59:23 -08001034 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
1036 spin_lock_irqsave(&info->slock, flags);
1037
1038 if (tty_hung_up_p(filp)) {
1039 spin_unlock_irqrestore(&info->slock, flags);
1040 return;
1041 }
1042 if ((tty->count == 1) && (info->count != 1)) {
1043 /*
1044 * Uh, oh. tty->count is 1, which means that the tty
1045 * structure will be freed. Info->count should always
1046 * be one in these conditions. If it's greater than
1047 * one, we've got real problems, since it means the
1048 * serial port won't be shutdown.
1049 */
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001050 printk(KERN_ERR "mxser_close: bad serial port count; "
1051 "tty->count is 1, info->count is %d\n", info->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 info->count = 1;
1053 }
1054 if (--info->count < 0) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001055 printk(KERN_ERR "mxser_close: bad serial port count for "
1056 "ttys%d: %d\n", info->port, info->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 info->count = 0;
1058 }
1059 if (info->count) {
1060 spin_unlock_irqrestore(&info->slock, flags);
1061 return;
1062 }
1063 info->flags |= ASYNC_CLOSING;
1064 spin_unlock_irqrestore(&info->slock, flags);
1065 /*
1066 * Save the termios structure, since this port may have
1067 * separate termios for callout and dialin.
1068 */
1069 if (info->flags & ASYNC_NORMAL_ACTIVE)
1070 info->normal_termios = *tty->termios;
1071 /*
1072 * Now we wait for the transmit buffer to clear; and we notify
1073 * the line discipline to only process XON/XOFF characters.
1074 */
1075 tty->closing = 1;
1076 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1077 tty_wait_until_sent(tty, info->closing_wait);
1078 /*
1079 * At this point we stop accepting input. To do this, we
1080 * disable the receive line status interrupts, and tell the
1081 * interrupt driver to stop checking the data ready bit in the
1082 * line status register.
1083 */
1084 info->IER &= ~UART_IER_RLSI;
1085 if (info->IsMoxaMustChipFlag)
1086 info->IER &= ~MOXA_MUST_RECV_ISR;
1087/* by William
1088 info->read_status_mask &= ~UART_LSR_DR;
1089*/
1090 if (info->flags & ASYNC_INITIALIZED) {
1091 outb(info->IER, info->base + UART_IER);
1092 /*
1093 * Before we drop DTR, make sure the UART transmitter
1094 * has completely drained; this is especially
1095 * important if there is a transmit FIFO!
1096 */
1097 timeout = jiffies + HZ;
1098 while (!(inb(info->base + UART_LSR) & UART_LSR_TEMT)) {
Nishanth Aravamudanda4cd8d2005-09-10 00:27:30 -07001099 schedule_timeout_interruptible(5);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 if (time_after(jiffies, timeout))
1101 break;
1102 }
1103 }
1104 mxser_shutdown(info);
1105
1106 if (tty->driver->flush_buffer)
1107 tty->driver->flush_buffer(tty);
1108
1109 ld = tty_ldisc_ref(tty);
1110 if (ld) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001111 if (ld->flush_buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 ld->flush_buffer(tty);
1113 tty_ldisc_deref(ld);
1114 }
1115
1116 tty->closing = 0;
1117 info->event = 0;
1118 info->tty = NULL;
1119 if (info->blocked_open) {
Nishanth Aravamudanda4cd8d2005-09-10 00:27:30 -07001120 if (info->close_delay)
1121 schedule_timeout_interruptible(info->close_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 wake_up_interruptible(&info->open_wait);
1123 }
1124
1125 info->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
1126 wake_up_interruptible(&info->close_wait);
1127
1128}
1129
1130static int mxser_write(struct tty_struct *tty, const unsigned char *buf, int count)
1131{
1132 int c, total = 0;
Jesper Juhl56e139f2006-06-25 05:47:52 -07001133 struct mxser_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 unsigned long flags;
1135
Jesper Juhl8a7f7c92006-06-25 05:47:53 -07001136 if (!info->xmit_buf)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001137 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
1139 while (1) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001140 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1141 SERIAL_XMIT_SIZE - info->xmit_head));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 if (c <= 0)
1143 break;
1144
1145 memcpy(info->xmit_buf + info->xmit_head, buf, c);
1146 spin_lock_irqsave(&info->slock, flags);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001147 info->xmit_head = (info->xmit_head + c) &
1148 (SERIAL_XMIT_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 info->xmit_cnt += c;
1150 spin_unlock_irqrestore(&info->slock, flags);
1151
1152 buf += c;
1153 count -= c;
1154 total += c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 }
1156
1157 if (info->xmit_cnt && !tty->stopped && !(info->IER & UART_IER_THRI)) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001158 if (!tty->hw_stopped ||
1159 (info->type == PORT_16550A) ||
1160 (info->IsMoxaMustChipFlag)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 spin_lock_irqsave(&info->slock, flags);
1162 info->IER |= UART_IER_THRI;
1163 outb(info->IER, info->base + UART_IER);
1164 spin_unlock_irqrestore(&info->slock, flags);
1165 }
1166 }
1167 return total;
1168}
1169
1170static void mxser_put_char(struct tty_struct *tty, unsigned char ch)
1171{
Jesper Juhl56e139f2006-06-25 05:47:52 -07001172 struct mxser_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 unsigned long flags;
1174
Jesper Juhl8a7f7c92006-06-25 05:47:53 -07001175 if (!info->xmit_buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 return;
1177
1178 if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1)
1179 return;
1180
1181 spin_lock_irqsave(&info->slock, flags);
1182 info->xmit_buf[info->xmit_head++] = ch;
1183 info->xmit_head &= SERIAL_XMIT_SIZE - 1;
1184 info->xmit_cnt++;
1185 spin_unlock_irqrestore(&info->slock, flags);
1186 if (!tty->stopped && !(info->IER & UART_IER_THRI)) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001187 if (!tty->hw_stopped ||
1188 (info->type == PORT_16550A) ||
1189 info->IsMoxaMustChipFlag) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 spin_lock_irqsave(&info->slock, flags);
1191 info->IER |= UART_IER_THRI;
1192 outb(info->IER, info->base + UART_IER);
1193 spin_unlock_irqrestore(&info->slock, flags);
1194 }
1195 }
1196}
1197
1198
1199static void mxser_flush_chars(struct tty_struct *tty)
1200{
Jesper Juhl56e139f2006-06-25 05:47:52 -07001201 struct mxser_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 unsigned long flags;
1203
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001204 if (info->xmit_cnt <= 0 ||
1205 tty->stopped ||
1206 !info->xmit_buf ||
1207 (tty->hw_stopped &&
1208 (info->type != PORT_16550A) &&
1209 (!info->IsMoxaMustChipFlag)
1210 ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 return;
1212
1213 spin_lock_irqsave(&info->slock, flags);
1214
1215 info->IER |= UART_IER_THRI;
1216 outb(info->IER, info->base + UART_IER);
1217
1218 spin_unlock_irqrestore(&info->slock, flags);
1219}
1220
1221static int mxser_write_room(struct tty_struct *tty)
1222{
Jesper Juhl56e139f2006-06-25 05:47:52 -07001223 struct mxser_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 int ret;
1225
1226 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1227 if (ret < 0)
1228 ret = 0;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001229 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230}
1231
1232static int mxser_chars_in_buffer(struct tty_struct *tty)
1233{
Jesper Juhl56e139f2006-06-25 05:47:52 -07001234 struct mxser_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 return info->xmit_cnt;
1236}
1237
1238static void mxser_flush_buffer(struct tty_struct *tty)
1239{
Jesper Juhl56e139f2006-06-25 05:47:52 -07001240 struct mxser_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 char fcr;
1242 unsigned long flags;
1243
1244
1245 spin_lock_irqsave(&info->slock, flags);
1246 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1247
1248 /* below added by shinhay */
1249 fcr = inb(info->base + UART_FCR);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001250 outb((fcr | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
1251 info->base + UART_FCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 outb(fcr, info->base + UART_FCR);
1253
1254 spin_unlock_irqrestore(&info->slock, flags);
1255 /* above added by shinhay */
1256
1257 wake_up_interruptible(&tty->write_wait);
1258 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) && tty->ldisc.write_wakeup)
1259 (tty->ldisc.write_wakeup) (tty);
1260}
1261
1262static int mxser_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
1263{
Jesper Juhl56e139f2006-06-25 05:47:52 -07001264 struct mxser_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 int retval;
1266 struct async_icount cprev, cnow; /* kernel counter temps */
1267 struct serial_icounter_struct __user *p_cuser;
1268 unsigned long templ;
1269 unsigned long flags;
1270 void __user *argp = (void __user *)arg;
1271
1272 if (tty->index == MXSER_PORTS)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001273 return mxser_ioctl_special(cmd, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001275 /* following add by Victor Yu. 01-05-2004 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 if (cmd == MOXA_SET_OP_MODE || cmd == MOXA_GET_OP_MODE) {
1277 int opmode, p;
1278 static unsigned char ModeMask[] = { 0xfc, 0xf3, 0xcf, 0x3f };
1279 int shiftbit;
1280 unsigned char val, mask;
1281
1282 p = info->port % 4;
1283 if (cmd == MOXA_SET_OP_MODE) {
1284 if (get_user(opmode, (int __user *) argp))
1285 return -EFAULT;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001286 if (opmode != RS232_MODE &&
1287 opmode != RS485_2WIRE_MODE &&
1288 opmode != RS422_MODE &&
1289 opmode != RS485_4WIRE_MODE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 return -EFAULT;
1291 mask = ModeMask[p];
1292 shiftbit = p * 2;
1293 val = inb(info->opmode_ioaddr);
1294 val &= mask;
1295 val |= (opmode << shiftbit);
1296 outb(val, info->opmode_ioaddr);
1297 } else {
1298 shiftbit = p * 2;
1299 opmode = inb(info->opmode_ioaddr) >> shiftbit;
1300 opmode &= OP_MODE_MASK;
1301 if (copy_to_user(argp, &opmode, sizeof(int)))
1302 return -EFAULT;
1303 }
1304 return 0;
1305 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001306 /* above add by Victor Yu. 01-05-2004 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
1308 if ((cmd != TIOCGSERIAL) && (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
1309 if (tty->flags & (1 << TTY_IO_ERROR))
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001310 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 }
1312 switch (cmd) {
1313 case TCSBRK: /* SVID version: non-zero arg --> no break */
1314 retval = tty_check_change(tty);
1315 if (retval)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001316 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 tty_wait_until_sent(tty, 0);
1318 if (!arg)
1319 mxser_send_break(info, HZ / 4); /* 1/4 second */
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001320 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 case TCSBRKP: /* support for POSIX tcsendbreak() */
1322 retval = tty_check_change(tty);
1323 if (retval)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001324 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 tty_wait_until_sent(tty, 0);
1326 mxser_send_break(info, arg ? arg * (HZ / 10) : HZ / 4);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001327 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 case TIOCGSOFTCAR:
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001329 return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *)argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 case TIOCSSOFTCAR:
1331 if (get_user(templ, (unsigned long __user *) argp))
1332 return -EFAULT;
1333 arg = templ;
1334 tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) | (arg ? CLOCAL : 0));
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001335 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 case TIOCGSERIAL:
1337 return mxser_get_serial_info(info, argp);
1338 case TIOCSSERIAL:
1339 return mxser_set_serial_info(info, argp);
1340 case TIOCSERGETLSR: /* Get line status register */
1341 return mxser_get_lsr_info(info, argp);
1342 /*
1343 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1344 * - mask passed in arg for lines of interest
1345 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1346 * Caller should use TIOCGICOUNT to see which one it was
1347 */
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001348 case TIOCMIWAIT: {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 DECLARE_WAITQUEUE(wait, current);
1350 int ret;
1351 spin_lock_irqsave(&info->slock, flags);
1352 cprev = info->icount; /* note the counters on entry */
1353 spin_unlock_irqrestore(&info->slock, flags);
1354
1355 add_wait_queue(&info->delta_msr_wait, &wait);
1356 while (1) {
1357 spin_lock_irqsave(&info->slock, flags);
1358 cnow = info->icount; /* atomic copy */
1359 spin_unlock_irqrestore(&info->slock, flags);
1360
1361 set_current_state(TASK_INTERRUPTIBLE);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001362 if (((arg & TIOCM_RNG) &&
1363 (cnow.rng != cprev.rng)) ||
1364 ((arg & TIOCM_DSR) &&
1365 (cnow.dsr != cprev.dsr)) ||
1366 ((arg & TIOCM_CD) &&
1367 (cnow.dcd != cprev.dcd)) ||
1368 ((arg & TIOCM_CTS) &&
1369 (cnow.cts != cprev.cts))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 ret = 0;
1371 break;
1372 }
1373 /* see if a signal did it */
1374 if (signal_pending(current)) {
1375 ret = -ERESTARTSYS;
1376 break;
1377 }
1378 cprev = cnow;
1379 }
1380 current->state = TASK_RUNNING;
1381 remove_wait_queue(&info->delta_msr_wait, &wait);
1382 break;
1383 }
1384 /* NOTREACHED */
1385 /*
1386 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1387 * Return: write counters to the user passed counter struct
1388 * NB: both 1->0 and 0->1 transitions are counted except for
1389 * RI where only 0->1 is counted.
1390 */
1391 case TIOCGICOUNT:
1392 spin_lock_irqsave(&info->slock, flags);
1393 cnow = info->icount;
1394 spin_unlock_irqrestore(&info->slock, flags);
1395 p_cuser = argp;
1396 /* modified by casper 1/11/2000 */
1397 if (put_user(cnow.frame, &p_cuser->frame))
1398 return -EFAULT;
1399 if (put_user(cnow.brk, &p_cuser->brk))
1400 return -EFAULT;
1401 if (put_user(cnow.overrun, &p_cuser->overrun))
1402 return -EFAULT;
1403 if (put_user(cnow.buf_overrun, &p_cuser->buf_overrun))
1404 return -EFAULT;
1405 if (put_user(cnow.parity, &p_cuser->parity))
1406 return -EFAULT;
1407 if (put_user(cnow.rx, &p_cuser->rx))
1408 return -EFAULT;
1409 if (put_user(cnow.tx, &p_cuser->tx))
1410 return -EFAULT;
1411 put_user(cnow.cts, &p_cuser->cts);
1412 put_user(cnow.dsr, &p_cuser->dsr);
1413 put_user(cnow.rng, &p_cuser->rng);
1414 put_user(cnow.dcd, &p_cuser->dcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 return 0;
1416 case MOXA_HighSpeedOn:
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001417 return put_user(info->baud_base != 115200 ? 1 : 0, (int __user *)argp);
1418 case MOXA_SDS_RSTICOUNTER: {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 info->mon_data.rxcnt = 0;
1420 info->mon_data.txcnt = 0;
1421 return 0;
1422 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001423/* (above) added by James. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 case MOXA_ASPP_SETBAUD:{
1425 long baud;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001426 if (get_user(baud, (long __user *)argp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 return -EFAULT;
1428 mxser_set_baud(info, baud);
1429 return 0;
1430 }
1431 case MOXA_ASPP_GETBAUD:
1432 if (copy_to_user(argp, &info->realbaud, sizeof(long)))
1433 return -EFAULT;
1434
1435 return 0;
1436
1437 case MOXA_ASPP_OQUEUE:{
1438 int len, lsr;
1439
1440 len = mxser_chars_in_buffer(tty);
1441
1442 lsr = inb(info->base + UART_LSR) & UART_LSR_TEMT;
1443
1444 len += (lsr ? 0 : 1);
1445
1446 if (copy_to_user(argp, &len, sizeof(int)))
1447 return -EFAULT;
1448
1449 return 0;
1450 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001451 case MOXA_ASPP_MON: {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 int mcr, status;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001453
1454 /* info->mon_data.ser_param = tty->termios->c_cflag; */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
1456 status = mxser_get_msr(info->base, 1, info->port, info);
1457 mxser_check_modem_status(info, status);
1458
1459 mcr = inb(info->base + UART_MCR);
1460 if (mcr & MOXA_MUST_MCR_XON_FLAG)
1461 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFHOLD;
1462 else
1463 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFHOLD;
1464
1465 if (mcr & MOXA_MUST_MCR_TX_XON)
1466 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFXENT;
1467 else
1468 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFXENT;
1469
1470 if (info->tty->hw_stopped)
1471 info->mon_data.hold_reason |= NPPI_NOTIFY_CTSHOLD;
1472 else
1473 info->mon_data.hold_reason &= ~NPPI_NOTIFY_CTSHOLD;
1474
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001475 if (copy_to_user(argp, &info->mon_data,
1476 sizeof(struct mxser_mon)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 return -EFAULT;
1478
1479 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 }
1481
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001482 case MOXA_ASPP_LSTATUS: {
1483 if (copy_to_user(argp, &info->err_shadow,
1484 sizeof(unsigned char)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 return -EFAULT;
1486
1487 info->err_shadow = 0;
1488 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001490 case MOXA_SET_BAUD_METHOD: {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 int method;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001492
1493 if (get_user(method, (int __user *)argp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 return -EFAULT;
1495 mxser_set_baud_method[info->port] = method;
1496 if (copy_to_user(argp, &method, sizeof(int)))
1497 return -EFAULT;
1498
1499 return 0;
1500 }
1501 default:
1502 return -ENOIOCTLCMD;
1503 }
1504 return 0;
1505}
1506
1507#ifndef CMSPAR
1508#define CMSPAR 010000000000
1509#endif
1510
1511static int mxser_ioctl_special(unsigned int cmd, void __user *argp)
1512{
1513 int i, result, status;
1514
1515 switch (cmd) {
1516 case MOXA_GET_CONF:
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001517 if (copy_to_user(argp, mxsercfg,
1518 sizeof(struct mxser_hwconf) * 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 return -EFAULT;
1520 return 0;
1521 case MOXA_GET_MAJOR:
1522 if (copy_to_user(argp, &ttymajor, sizeof(int)))
1523 return -EFAULT;
1524 return 0;
1525
1526 case MOXA_GET_CUMAJOR:
1527 if (copy_to_user(argp, &calloutmajor, sizeof(int)))
1528 return -EFAULT;
1529 return 0;
1530
1531 case MOXA_CHKPORTENABLE:
1532 result = 0;
1533 for (i = 0; i < MXSER_PORTS; i++) {
1534 if (mxvar_table[i].base)
1535 result |= (1 << i);
1536 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001537 return put_user(result, (unsigned long __user *)argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 case MOXA_GETDATACOUNT:
1539 if (copy_to_user(argp, &mxvar_log, sizeof(mxvar_log)))
1540 return -EFAULT;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001541 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 case MOXA_GETMSTATUS:
1543 for (i = 0; i < MXSER_PORTS; i++) {
1544 GMStatus[i].ri = 0;
1545 if (!mxvar_table[i].base) {
1546 GMStatus[i].dcd = 0;
1547 GMStatus[i].dsr = 0;
1548 GMStatus[i].cts = 0;
1549 continue;
1550 }
1551
1552 if (!mxvar_table[i].tty || !mxvar_table[i].tty->termios)
1553 GMStatus[i].cflag = mxvar_table[i].normal_termios.c_cflag;
1554 else
1555 GMStatus[i].cflag = mxvar_table[i].tty->termios->c_cflag;
1556
1557 status = inb(mxvar_table[i].base + UART_MSR);
1558 if (status & 0x80 /*UART_MSR_DCD */ )
1559 GMStatus[i].dcd = 1;
1560 else
1561 GMStatus[i].dcd = 0;
1562
1563 if (status & 0x20 /*UART_MSR_DSR */ )
1564 GMStatus[i].dsr = 1;
1565 else
1566 GMStatus[i].dsr = 0;
1567
1568
1569 if (status & 0x10 /*UART_MSR_CTS */ )
1570 GMStatus[i].cts = 1;
1571 else
1572 GMStatus[i].cts = 0;
1573 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001574 if (copy_to_user(argp, GMStatus,
1575 sizeof(struct mxser_mstatus) * MXSER_PORTS))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 return -EFAULT;
1577 return 0;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001578 case MOXA_ASPP_MON_EXT: {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 int status;
1580 int opmode, p;
1581 int shiftbit;
1582 unsigned cflag, iflag;
1583
1584 for (i = 0; i < MXSER_PORTS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 if (!mxvar_table[i].base)
1586 continue;
1587
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001588 status = mxser_get_msr(mxvar_table[i].base, 0,
1589 i, &(mxvar_table[i]));
1590 /*
1591 mxser_check_modem_status(&mxvar_table[i],
1592 status);
1593 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 if (status & UART_MSR_TERI)
1595 mxvar_table[i].icount.rng++;
1596 if (status & UART_MSR_DDSR)
1597 mxvar_table[i].icount.dsr++;
1598 if (status & UART_MSR_DDCD)
1599 mxvar_table[i].icount.dcd++;
1600 if (status & UART_MSR_DCTS)
1601 mxvar_table[i].icount.cts++;
1602
1603 mxvar_table[i].mon_data.modem_status = status;
1604 mon_data_ext.rx_cnt[i] = mxvar_table[i].mon_data.rxcnt;
1605 mon_data_ext.tx_cnt[i] = mxvar_table[i].mon_data.txcnt;
1606 mon_data_ext.up_rxcnt[i] = mxvar_table[i].mon_data.up_rxcnt;
1607 mon_data_ext.up_txcnt[i] = mxvar_table[i].mon_data.up_txcnt;
1608 mon_data_ext.modem_status[i] = mxvar_table[i].mon_data.modem_status;
1609 mon_data_ext.baudrate[i] = mxvar_table[i].realbaud;
1610
1611 if (!mxvar_table[i].tty || !mxvar_table[i].tty->termios) {
1612 cflag = mxvar_table[i].normal_termios.c_cflag;
1613 iflag = mxvar_table[i].normal_termios.c_iflag;
1614 } else {
1615 cflag = mxvar_table[i].tty->termios->c_cflag;
1616 iflag = mxvar_table[i].tty->termios->c_iflag;
1617 }
1618
1619 mon_data_ext.databits[i] = cflag & CSIZE;
1620
1621 mon_data_ext.stopbits[i] = cflag & CSTOPB;
1622
1623 mon_data_ext.parity[i] = cflag & (PARENB | PARODD | CMSPAR);
1624
1625 mon_data_ext.flowctrl[i] = 0x00;
1626
1627 if (cflag & CRTSCTS)
1628 mon_data_ext.flowctrl[i] |= 0x03;
1629
1630 if (iflag & (IXON | IXOFF))
1631 mon_data_ext.flowctrl[i] |= 0x0C;
1632
1633 if (mxvar_table[i].type == PORT_16550A)
1634 mon_data_ext.fifo[i] = 1;
1635 else
1636 mon_data_ext.fifo[i] = 0;
1637
1638 p = i % 4;
1639 shiftbit = p * 2;
1640 opmode = inb(mxvar_table[i].opmode_ioaddr) >> shiftbit;
1641 opmode &= OP_MODE_MASK;
1642
1643 mon_data_ext.iftype[i] = opmode;
1644
1645 }
1646 if (copy_to_user(argp, &mon_data_ext, sizeof(struct mxser_mon_ext)))
1647 return -EFAULT;
1648
1649 return 0;
1650
1651 }
1652 default:
1653 return -ENOIOCTLCMD;
1654 }
1655 return 0;
1656}
1657
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658static void mxser_stoprx(struct tty_struct *tty)
1659{
Jesper Juhl56e139f2006-06-25 05:47:52 -07001660 struct mxser_struct *info = tty->driver_data;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001661 /* unsigned long flags; */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
1663 info->ldisc_stop_rx = 1;
1664 if (I_IXOFF(tty)) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001665 /* MX_LOCK(&info->slock); */
1666 /* following add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 if (info->IsMoxaMustChipFlag) {
1668 info->IER &= ~MOXA_MUST_RECV_ISR;
1669 outb(info->IER, info->base + UART_IER);
1670 } else {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001671 /* above add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 info->x_char = STOP_CHAR(tty);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001673 /* mask by Victor Yu. 09-02-2002 */
1674 /* outb(info->IER, 0); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 outb(0, info->base + UART_IER);
1676 info->IER |= UART_IER_THRI;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001677 /* force Tx interrupt */
1678 outb(info->IER, info->base + UART_IER);
1679 } /* add by Victor Yu. 09-02-2002 */
1680 /* MX_UNLOCK(&info->slock); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 }
1682
1683 if (info->tty->termios->c_cflag & CRTSCTS) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001684 /* MX_LOCK(&info->slock); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 info->MCR &= ~UART_MCR_RTS;
1686 outb(info->MCR, info->base + UART_MCR);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001687 /* MX_UNLOCK(&info->slock); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 }
1689}
1690
1691static void mxser_startrx(struct tty_struct *tty)
1692{
Jesper Juhl56e139f2006-06-25 05:47:52 -07001693 struct mxser_struct *info = tty->driver_data;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001694 /* unsigned long flags; */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695
1696 info->ldisc_stop_rx = 0;
1697 if (I_IXOFF(tty)) {
1698 if (info->x_char)
1699 info->x_char = 0;
1700 else {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001701 /* MX_LOCK(&info->slock); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001703 /* following add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 if (info->IsMoxaMustChipFlag) {
1705 info->IER |= MOXA_MUST_RECV_ISR;
1706 outb(info->IER, info->base + UART_IER);
1707 } else {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001708 /* above add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709
1710 info->x_char = START_CHAR(tty);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001711 /* mask by Victor Yu. 09-02-2002 */
1712 /* outb(info->IER, 0); */
1713 /* add by Victor Yu. 09-02-2002 */
1714 outb(0, info->base + UART_IER);
1715 /* force Tx interrupt */
1716 info->IER |= UART_IER_THRI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 outb(info->IER, info->base + UART_IER);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001718 } /* add by Victor Yu. 09-02-2002 */
1719 /* MX_UNLOCK(&info->slock); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 }
1721 }
1722
1723 if (info->tty->termios->c_cflag & CRTSCTS) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001724 /* MX_LOCK(&info->slock); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 info->MCR |= UART_MCR_RTS;
1726 outb(info->MCR, info->base + UART_MCR);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001727 /* MX_UNLOCK(&info->slock); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 }
1729}
1730
1731/*
1732 * This routine is called by the upper-layer tty layer to signal that
1733 * incoming characters should be throttled.
1734 */
1735static void mxser_throttle(struct tty_struct *tty)
1736{
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001737 /* struct mxser_struct *info = tty->driver_data; */
1738 /* unsigned long flags; */
1739
1740 /* MX_LOCK(&info->slock); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 mxser_stoprx(tty);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001742 /* MX_UNLOCK(&info->slock); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743}
1744
1745static void mxser_unthrottle(struct tty_struct *tty)
1746{
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001747 /* struct mxser_struct *info = tty->driver_data; */
1748 /* unsigned long flags; */
1749
1750 /* MX_LOCK(&info->slock); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 mxser_startrx(tty);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001752 /* MX_UNLOCK(&info->slock); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753}
1754
Alan Cox606d0992006-12-08 02:38:45 -08001755static void mxser_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756{
Jesper Juhl56e139f2006-06-25 05:47:52 -07001757 struct mxser_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 unsigned long flags;
1759
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001760 if ((tty->termios->c_cflag != old_termios->c_cflag) ||
1761 (RELEVANT_IFLAG(tty->termios->c_iflag) != RELEVANT_IFLAG(old_termios->c_iflag))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762
1763 mxser_change_speed(info, old_termios);
1764
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001765 if ((old_termios->c_cflag & CRTSCTS) &&
1766 !(tty->termios->c_cflag & CRTSCTS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 tty->hw_stopped = 0;
1768 mxser_start(tty);
1769 }
1770 }
1771
1772/* Handle sw stopped */
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001773 if ((old_termios->c_iflag & IXON) &&
1774 !(tty->termios->c_iflag & IXON)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 tty->stopped = 0;
1776
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001777 /* following add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 if (info->IsMoxaMustChipFlag) {
1779 spin_lock_irqsave(&info->slock, flags);
1780 DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->base);
1781 spin_unlock_irqrestore(&info->slock, flags);
1782 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001783 /* above add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784
1785 mxser_start(tty);
1786 }
1787}
1788
1789/*
1790 * mxser_stop() and mxser_start()
1791 *
1792 * This routines are called before setting or resetting tty->stopped.
1793 * They enable or disable transmitter interrupts, as necessary.
1794 */
1795static void mxser_stop(struct tty_struct *tty)
1796{
Jesper Juhl56e139f2006-06-25 05:47:52 -07001797 struct mxser_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 unsigned long flags;
1799
1800 spin_lock_irqsave(&info->slock, flags);
1801 if (info->IER & UART_IER_THRI) {
1802 info->IER &= ~UART_IER_THRI;
1803 outb(info->IER, info->base + UART_IER);
1804 }
1805 spin_unlock_irqrestore(&info->slock, flags);
1806}
1807
1808static void mxser_start(struct tty_struct *tty)
1809{
Jesper Juhl56e139f2006-06-25 05:47:52 -07001810 struct mxser_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 unsigned long flags;
1812
1813 spin_lock_irqsave(&info->slock, flags);
1814 if (info->xmit_cnt && info->xmit_buf && !(info->IER & UART_IER_THRI)) {
1815 info->IER |= UART_IER_THRI;
1816 outb(info->IER, info->base + UART_IER);
1817 }
1818 spin_unlock_irqrestore(&info->slock, flags);
1819}
1820
1821/*
1822 * mxser_wait_until_sent() --- wait until the transmitter is empty
1823 */
1824static void mxser_wait_until_sent(struct tty_struct *tty, int timeout)
1825{
Jesper Juhl56e139f2006-06-25 05:47:52 -07001826 struct mxser_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 unsigned long orig_jiffies, char_time;
1828 int lsr;
1829
1830 if (info->type == PORT_UNKNOWN)
1831 return;
1832
1833 if (info->xmit_fifo_size == 0)
1834 return; /* Just in case.... */
1835
1836 orig_jiffies = jiffies;
1837 /*
1838 * Set the check interval to be 1/5 of the estimated time to
1839 * send a single character, and make it at least 1. The check
1840 * interval should also be less than the timeout.
1841 *
1842 * Note: we have to use pretty tight timings here to satisfy
1843 * the NIST-PCTS.
1844 */
1845 char_time = (info->timeout - HZ / 50) / info->xmit_fifo_size;
1846 char_time = char_time / 5;
1847 if (char_time == 0)
1848 char_time = 1;
1849 if (timeout && timeout < char_time)
1850 char_time = timeout;
1851 /*
1852 * If the transmitter hasn't cleared in twice the approximate
1853 * amount of time to send the entire FIFO, it probably won't
1854 * ever clear. This assumes the UART isn't doing flow
1855 * control, which is currently the case. Hence, if it ever
1856 * takes longer than info->timeout, this is probably due to a
1857 * UART bug of some kind. So, we clamp the timeout parameter at
1858 * 2*info->timeout.
1859 */
1860 if (!timeout || timeout > 2 * info->timeout)
1861 timeout = 2 * info->timeout;
1862#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001863 printk(KERN_DEBUG "In rs_wait_until_sent(%d) check=%lu...",
1864 timeout, char_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 printk("jiff=%lu...", jiffies);
1866#endif
1867 while (!((lsr = inb(info->base + UART_LSR)) & UART_LSR_TEMT)) {
1868#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1869 printk("lsr = %d (jiff=%lu)...", lsr, jiffies);
1870#endif
Nishanth Aravamudanda4cd8d2005-09-10 00:27:30 -07001871 schedule_timeout_interruptible(char_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 if (signal_pending(current))
1873 break;
1874 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1875 break;
1876 }
1877 set_current_state(TASK_RUNNING);
1878
1879#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1880 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
1881#endif
1882}
1883
1884
1885/*
1886 * This routine is called by tty_hangup() when a hangup is signaled.
1887 */
1888void mxser_hangup(struct tty_struct *tty)
1889{
Jesper Juhl56e139f2006-06-25 05:47:52 -07001890 struct mxser_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
1892 mxser_flush_buffer(tty);
1893 mxser_shutdown(info);
1894 info->event = 0;
1895 info->count = 0;
1896 info->flags &= ~ASYNC_NORMAL_ACTIVE;
1897 info->tty = NULL;
1898 wake_up_interruptible(&info->open_wait);
1899}
1900
1901
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001902/* added by James 03-12-2004. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903/*
1904 * mxser_rs_break() --- routine which turns the break handling on or off
1905 */
1906static void mxser_rs_break(struct tty_struct *tty, int break_state)
1907{
Jesper Juhl56e139f2006-06-25 05:47:52 -07001908 struct mxser_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 unsigned long flags;
1910
1911 spin_lock_irqsave(&info->slock, flags);
1912 if (break_state == -1)
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 else
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001916 outb(inb(info->base + UART_LCR) & ~UART_LCR_SBC,
1917 info->base + UART_LCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 spin_unlock_irqrestore(&info->slock, flags);
1919}
1920
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001921/* (above) added by James. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
1923
1924/*
1925 * This is the serial driver's generic interrupt routine
1926 */
David Howells7d12e782006-10-05 14:55:46 +01001927static irqreturn_t mxser_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928{
1929 int status, iir, i;
1930 struct mxser_struct *info;
1931 struct mxser_struct *port;
1932 int max, irqbits, bits, msr;
1933 int pass_counter = 0;
1934 int handled = IRQ_NONE;
1935
1936 port = NULL;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001937 /* spin_lock(&gm_lock); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
1939 for (i = 0; i < MXSER_BOARDS; i++) {
1940 if (dev_id == &(mxvar_table[i * MXSER_PORTS_PER_BOARD])) {
1941 port = dev_id;
1942 break;
1943 }
1944 }
1945
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001946 if (i == MXSER_BOARDS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 goto irq_stop;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001948 if (port == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 goto irq_stop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 max = mxser_numports[mxsercfg[i].board_type - 1];
1951 while (1) {
1952 irqbits = inb(port->vector) & port->vectormask;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001953 if (irqbits == port->vectormask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955
1956 handled = IRQ_HANDLED;
1957 for (i = 0, bits = 1; i < max; i++, irqbits |= bits, bits <<= 1) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001958 if (irqbits == port->vectormask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 if (bits & irqbits)
1961 continue;
1962 info = port + i;
1963
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001964 /* following add by Victor Yu. 09-13-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 iir = inb(info->base + UART_IIR);
1966 if (iir & UART_IIR_NO_INT)
1967 continue;
1968 iir &= MOXA_MUST_IIR_MASK;
1969 if (!info->tty) {
1970 status = inb(info->base + UART_LSR);
1971 outb(0x27, info->base + UART_FCR);
1972 inb(info->base + UART_MSR);
1973 continue;
1974 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001975 /* above add by Victor Yu. 09-13-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 /*
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001977 if (info->tty->flip.count < TTY_FLIPBUF_SIZE / 4) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 info->IER |= MOXA_MUST_RECV_ISR;
1979 outb(info->IER, info->base + UART_IER);
1980 }
1981 */
1982
1983
1984 /* mask by Victor Yu. 09-13-2002
1985 if ( !info->tty ||
1986 (inb(info->base + UART_IIR) & UART_IIR_NO_INT) )
1987 continue;
1988 */
1989 /* mask by Victor Yu. 09-02-2002
1990 status = inb(info->base + UART_LSR) & info->read_status_mask;
1991 */
1992
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001993 /* following add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 status = inb(info->base + UART_LSR);
1995
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001996 if (status & UART_LSR_PE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 info->err_shadow |= NPPI_NOTIFY_PARITY;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001998 if (status & UART_LSR_FE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 info->err_shadow |= NPPI_NOTIFY_FRAMING;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002000 if (status & UART_LSR_OE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 info->err_shadow |= NPPI_NOTIFY_HW_OVERRUN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 if (status & UART_LSR_BI)
2003 info->err_shadow |= NPPI_NOTIFY_BREAK;
2004
2005 if (info->IsMoxaMustChipFlag) {
2006 /*
2007 if ( (status & 0x02) && !(status & 0x01) ) {
2008 outb(info->base+UART_FCR, 0x23);
2009 continue;
2010 }
2011 */
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002012 if (iir == MOXA_MUST_IIR_GDA ||
2013 iir == MOXA_MUST_IIR_RDA ||
2014 iir == MOXA_MUST_IIR_RTO ||
2015 iir == MOXA_MUST_IIR_LSR)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 mxser_receive_chars(info, &status);
2017
2018 } else {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002019 /* above add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020
2021 status &= info->read_status_mask;
2022 if (status & UART_LSR_DR)
2023 mxser_receive_chars(info, &status);
2024 }
2025 msr = inb(info->base + UART_MSR);
2026 if (msr & UART_MSR_ANY_DELTA) {
2027 mxser_check_modem_status(info, msr);
2028 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002029 /* following add by Victor Yu. 09-13-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 if (info->IsMoxaMustChipFlag) {
2031 if ((iir == 0x02) && (status & UART_LSR_THRE)) {
2032 mxser_transmit_chars(info);
2033 }
2034 } else {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002035 /* above add by Victor Yu. 09-13-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036
2037 if (status & UART_LSR_THRE) {
2038/* 8-2-99 by William
2039 if ( info->x_char || (info->xmit_cnt > 0) )
2040*/
2041 mxser_transmit_chars(info);
2042 }
2043 }
2044 }
2045 if (pass_counter++ > MXSER_ISR_PASS_LIMIT) {
2046 break; /* Prevent infinite loops */
2047 }
2048 }
2049
2050 irq_stop:
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002051 /* spin_unlock(&gm_lock); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 return handled;
2053}
2054
2055static void mxser_receive_chars(struct mxser_struct *info, int *status)
2056{
2057 struct tty_struct *tty = info->tty;
2058 unsigned char ch, gdl;
2059 int ignored = 0;
2060 int cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 int recv_room;
2062 int max = 256;
2063 unsigned long flags;
2064
2065 spin_lock_irqsave(&info->slock, flags);
2066
Alan Cox33f0f882006-01-09 20:54:13 -08002067 recv_room = tty->receive_room;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 if ((recv_room == 0) && (!info->ldisc_stop_rx)) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002069 /* mxser_throttle(tty); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 mxser_stoprx(tty);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002071 /* return; */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 }
2073
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002074 /* following add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 if (info->IsMoxaMustChipFlag != MOXA_OTHER_UART) {
2076
2077 if (*status & UART_LSR_SPECIAL) {
2078 goto intr_old;
2079 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002080 /* following add by Victor Yu. 02-11-2004 */
2081 if (info->IsMoxaMustChipFlag == MOXA_MUST_MU860_HWID &&
2082 (*status & MOXA_MUST_LSR_RERR))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 goto intr_old;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002084 /* above add by Victor Yu. 02-14-2004 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 if (*status & MOXA_MUST_LSR_RERR)
2086 goto intr_old;
2087
2088 gdl = inb(info->base + MOXA_MUST_GDL_REGISTER);
2089
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002090 /* add by Victor Yu. 02-11-2004 */
2091 if (info->IsMoxaMustChipFlag == MOXA_MUST_MU150_HWID)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 gdl &= MOXA_MUST_GDL_MASK;
2093 if (gdl >= recv_room) {
2094 if (!info->ldisc_stop_rx) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002095 /* mxser_throttle(tty); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 mxser_stoprx(tty);
2097 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002098 /* return; */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 }
2100 while (gdl--) {
2101 ch = inb(info->base + UART_RX);
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002102 tty_insert_flip_char(tty, ch, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 cnt++;
2104 /*
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002105 if ((cnt >= HI_WATER) && (info->stop_rx == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 mxser_stoprx(tty);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002107 info->stop_rx = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 break;
2109 } */
2110 }
2111 goto end_intr;
2112 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002113 intr_old:
2114 /* above add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115
2116 do {
2117 if (max-- < 0)
2118 break;
2119 /*
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002120 if ((cnt >= HI_WATER) && (info->stop_rx == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 mxser_stoprx(tty);
2122 info->stop_rx=1;
2123 break;
2124 }
2125 */
2126
2127 ch = inb(info->base + UART_RX);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002128 /* following add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 if (info->IsMoxaMustChipFlag && (*status & UART_LSR_OE) /*&& !(*status&UART_LSR_DR) */ )
2130 outb(0x23, info->base + UART_FCR);
2131 *status &= info->read_status_mask;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002132 /* above add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 if (*status & info->ignore_status_mask) {
2134 if (++ignored > 100)
2135 break;
2136 } else {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002137 char flag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 if (*status & UART_LSR_SPECIAL) {
2139 if (*status & UART_LSR_BI) {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002140 flag = TTY_BREAK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141/* added by casper 1/11/2000 */
2142 info->icount.brk++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143/* */
2144 if (info->flags & ASYNC_SAK)
2145 do_SAK(tty);
2146 } else if (*status & UART_LSR_PE) {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002147 flag = TTY_PARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148/* added by casper 1/11/2000 */
2149 info->icount.parity++;
2150/* */
2151 } else if (*status & UART_LSR_FE) {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002152 flag = TTY_FRAME;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153/* added by casper 1/11/2000 */
2154 info->icount.frame++;
2155/* */
2156 } else if (*status & UART_LSR_OE) {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002157 flag = TTY_OVERRUN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158/* added by casper 1/11/2000 */
2159 info->icount.overrun++;
2160/* */
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002161 }
2162 }
2163 tty_insert_flip_char(tty, ch, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 cnt++;
2165 if (cnt >= recv_room) {
2166 if (!info->ldisc_stop_rx) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002167 /* mxser_throttle(tty); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 mxser_stoprx(tty);
2169 }
2170 break;
2171 }
2172
2173 }
2174
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002175 /* following add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 if (info->IsMoxaMustChipFlag)
2177 break;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002178 /* above add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179
2180 /* mask by Victor Yu. 09-02-2002
2181 *status = inb(info->base + UART_LSR) & info->read_status_mask;
2182 */
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002183 /* following add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 *status = inb(info->base + UART_LSR);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002185 /* above add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 } while (*status & UART_LSR_DR);
2187
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002188end_intr: /* add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 mxvar_log.rxcnt[info->port] += cnt;
2190 info->mon_data.rxcnt += cnt;
2191 info->mon_data.up_rxcnt += cnt;
2192 spin_unlock_irqrestore(&info->slock, flags);
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002193
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 tty_flip_buffer_push(tty);
2195}
2196
2197static void mxser_transmit_chars(struct mxser_struct *info)
2198{
2199 int count, cnt;
2200 unsigned long flags;
2201
2202 spin_lock_irqsave(&info->slock, flags);
2203
2204 if (info->x_char) {
2205 outb(info->x_char, info->base + UART_TX);
2206 info->x_char = 0;
2207 mxvar_log.txcnt[info->port]++;
2208 info->mon_data.txcnt++;
2209 info->mon_data.up_txcnt++;
2210
2211/* added by casper 1/11/2000 */
2212 info->icount.tx++;
2213/* */
2214 spin_unlock_irqrestore(&info->slock, flags);
2215 return;
2216 }
2217
2218 if (info->xmit_buf == 0) {
2219 spin_unlock_irqrestore(&info->slock, flags);
2220 return;
2221 }
2222
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002223 if ((info->xmit_cnt <= 0) || info->tty->stopped ||
2224 (info->tty->hw_stopped &&
2225 (info->type != PORT_16550A) &&
2226 (!info->IsMoxaMustChipFlag))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 info->IER &= ~UART_IER_THRI;
2228 outb(info->IER, info->base + UART_IER);
2229 spin_unlock_irqrestore(&info->slock, flags);
2230 return;
2231 }
2232
2233 cnt = info->xmit_cnt;
2234 count = info->xmit_fifo_size;
2235 do {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002236 outb(info->xmit_buf[info->xmit_tail++],
2237 info->base + UART_TX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE - 1);
2239 if (--info->xmit_cnt <= 0)
2240 break;
2241 } while (--count > 0);
2242 mxvar_log.txcnt[info->port] += (cnt - info->xmit_cnt);
2243
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002244/* added by James 03-12-2004. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 info->mon_data.txcnt += (cnt - info->xmit_cnt);
2246 info->mon_data.up_txcnt += (cnt - info->xmit_cnt);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002247/* (above) added by James. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248
2249/* added by casper 1/11/2000 */
2250 info->icount.tx += (cnt - info->xmit_cnt);
2251/* */
2252
2253 if (info->xmit_cnt < WAKEUP_CHARS) {
2254 set_bit(MXSER_EVENT_TXLOW, &info->event);
2255 schedule_work(&info->tqueue);
2256 }
2257 if (info->xmit_cnt <= 0) {
2258 info->IER &= ~UART_IER_THRI;
2259 outb(info->IER, info->base + UART_IER);
2260 }
2261 spin_unlock_irqrestore(&info->slock, flags);
2262}
2263
2264static void mxser_check_modem_status(struct mxser_struct *info, int status)
2265{
2266 /* update input line counters */
2267 if (status & UART_MSR_TERI)
2268 info->icount.rng++;
2269 if (status & UART_MSR_DDSR)
2270 info->icount.dsr++;
2271 if (status & UART_MSR_DDCD)
2272 info->icount.dcd++;
2273 if (status & UART_MSR_DCTS)
2274 info->icount.cts++;
2275 info->mon_data.modem_status = status;
2276 wake_up_interruptible(&info->delta_msr_wait);
2277
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 if ((info->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
2279 if (status & UART_MSR_DCD)
2280 wake_up_interruptible(&info->open_wait);
2281 schedule_work(&info->tqueue);
2282 }
2283
2284 if (info->flags & ASYNC_CTS_FLOW) {
2285 if (info->tty->hw_stopped) {
2286 if (status & UART_MSR_CTS) {
2287 info->tty->hw_stopped = 0;
2288
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002289 if ((info->type != PORT_16550A) &&
2290 (!info->IsMoxaMustChipFlag)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 info->IER |= UART_IER_THRI;
2292 outb(info->IER, info->base + UART_IER);
2293 }
2294 set_bit(MXSER_EVENT_TXLOW, &info->event);
2295 schedule_work(&info->tqueue); }
2296 } else {
2297 if (!(status & UART_MSR_CTS)) {
2298 info->tty->hw_stopped = 1;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002299 if ((info->type != PORT_16550A) &&
2300 (!info->IsMoxaMustChipFlag)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 info->IER &= ~UART_IER_THRI;
2302 outb(info->IER, info->base + UART_IER);
2303 }
2304 }
2305 }
2306 }
2307}
2308
2309static int mxser_block_til_ready(struct tty_struct *tty, struct file *filp, struct mxser_struct *info)
2310{
2311 DECLARE_WAITQUEUE(wait, current);
2312 int retval;
2313 int do_clocal = 0;
2314 unsigned long flags;
2315
2316 /*
2317 * If non-blocking mode is set, or the port is not enabled,
2318 * then make the check up front and then exit.
2319 */
2320 if ((filp->f_flags & O_NONBLOCK) || (tty->flags & (1 << TTY_IO_ERROR))) {
2321 info->flags |= ASYNC_NORMAL_ACTIVE;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002322 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 }
2324
2325 if (tty->termios->c_cflag & CLOCAL)
2326 do_clocal = 1;
2327
2328 /*
2329 * Block waiting for the carrier detect and the line to become
2330 * free (i.e., not in use by the callout). While we are in
2331 * this loop, info->count is dropped by one, so that
2332 * mxser_close() knows when to free things. We restore it upon
2333 * exit, either normal or abnormal.
2334 */
2335 retval = 0;
2336 add_wait_queue(&info->open_wait, &wait);
2337
2338 spin_lock_irqsave(&info->slock, flags);
2339 if (!tty_hung_up_p(filp))
2340 info->count--;
2341 spin_unlock_irqrestore(&info->slock, flags);
2342 info->blocked_open++;
2343 while (1) {
2344 spin_lock_irqsave(&info->slock, flags);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002345 outb(inb(info->base + UART_MCR) |
2346 UART_MCR_DTR | UART_MCR_RTS, info->base + UART_MCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 spin_unlock_irqrestore(&info->slock, flags);
2348 set_current_state(TASK_INTERRUPTIBLE);
2349 if (tty_hung_up_p(filp) || !(info->flags & ASYNC_INITIALIZED)) {
2350 if (info->flags & ASYNC_HUP_NOTIFY)
2351 retval = -EAGAIN;
2352 else
2353 retval = -ERESTARTSYS;
2354 break;
2355 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002356 if (!(info->flags & ASYNC_CLOSING) &&
2357 (do_clocal ||
2358 (inb(info->base + UART_MSR) & UART_MSR_DCD)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 break;
2360 if (signal_pending(current)) {
2361 retval = -ERESTARTSYS;
2362 break;
2363 }
2364 schedule();
2365 }
2366 set_current_state(TASK_RUNNING);
2367 remove_wait_queue(&info->open_wait, &wait);
2368 if (!tty_hung_up_p(filp))
2369 info->count++;
2370 info->blocked_open--;
2371 if (retval)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002372 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 info->flags |= ASYNC_NORMAL_ACTIVE;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002374 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375}
2376
2377static int mxser_startup(struct mxser_struct *info)
2378{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 unsigned long page;
2380 unsigned long flags;
2381
2382 page = __get_free_page(GFP_KERNEL);
2383 if (!page)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002384 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385
2386 spin_lock_irqsave(&info->slock, flags);
2387
2388 if (info->flags & ASYNC_INITIALIZED) {
2389 free_page(page);
2390 spin_unlock_irqrestore(&info->slock, flags);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002391 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 }
2393
2394 if (!info->base || !info->type) {
2395 if (info->tty)
2396 set_bit(TTY_IO_ERROR, &info->tty->flags);
2397 free_page(page);
2398 spin_unlock_irqrestore(&info->slock, flags);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002399 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 }
2401 if (info->xmit_buf)
2402 free_page(page);
2403 else
2404 info->xmit_buf = (unsigned char *) page;
2405
2406 /*
2407 * Clear the FIFO buffers and disable them
2408 * (they will be reenabled in mxser_change_speed())
2409 */
2410 if (info->IsMoxaMustChipFlag)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002411 outb((UART_FCR_CLEAR_RCVR |
2412 UART_FCR_CLEAR_XMIT |
2413 MOXA_MUST_FCR_GDA_MODE_ENABLE), info->base + UART_FCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 else
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002415 outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
2416 info->base + UART_FCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417
2418 /*
2419 * At this point there's no way the LSR could still be 0xFF;
2420 * if it is, then bail out, because there's likely no UART
2421 * here.
2422 */
2423 if (inb(info->base + UART_LSR) == 0xff) {
2424 spin_unlock_irqrestore(&info->slock, flags);
2425 if (capable(CAP_SYS_ADMIN)) {
2426 if (info->tty)
2427 set_bit(TTY_IO_ERROR, &info->tty->flags);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002428 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 } else
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002430 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 }
2432
2433 /*
2434 * Clear the interrupt registers.
2435 */
2436 (void) inb(info->base + UART_LSR);
2437 (void) inb(info->base + UART_RX);
2438 (void) inb(info->base + UART_IIR);
2439 (void) inb(info->base + UART_MSR);
2440
2441 /*
2442 * Now, initialize the UART
2443 */
2444 outb(UART_LCR_WLEN8, info->base + UART_LCR); /* reset DLAB */
2445 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
2446 outb(info->MCR, info->base + UART_MCR);
2447
2448 /*
2449 * Finally, enable interrupts
2450 */
2451 info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002452 /* info->IER = UART_IER_RLSI | UART_IER_RDI; */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002454 /* following add by Victor Yu. 08-30-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 if (info->IsMoxaMustChipFlag)
2456 info->IER |= MOXA_MUST_IER_EGDAI;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002457 /* above add by Victor Yu. 08-30-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 outb(info->IER, info->base + UART_IER); /* enable interrupts */
2459
2460 /*
2461 * And clear the interrupt registers again for luck.
2462 */
2463 (void) inb(info->base + UART_LSR);
2464 (void) inb(info->base + UART_RX);
2465 (void) inb(info->base + UART_IIR);
2466 (void) inb(info->base + UART_MSR);
2467
2468 if (info->tty)
2469 clear_bit(TTY_IO_ERROR, &info->tty->flags);
2470 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
2471
2472 /*
2473 * and set the speed of the serial port
2474 */
2475 spin_unlock_irqrestore(&info->slock, flags);
2476 mxser_change_speed(info, NULL);
2477
2478 info->flags |= ASYNC_INITIALIZED;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002479 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480}
2481
2482/*
2483 * This routine will shutdown a serial port; interrupts maybe disabled, and
2484 * DTR is dropped if the hangup on close termio flag is on.
2485 */
2486static void mxser_shutdown(struct mxser_struct *info)
2487{
2488 unsigned long flags;
2489
2490 if (!(info->flags & ASYNC_INITIALIZED))
2491 return;
2492
2493 spin_lock_irqsave(&info->slock, flags);
2494
2495 /*
2496 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
2497 * here so the queue might never be waken up
2498 */
2499 wake_up_interruptible(&info->delta_msr_wait);
2500
2501 /*
2502 * Free the IRQ, if necessary
2503 */
2504 if (info->xmit_buf) {
2505 free_page((unsigned long) info->xmit_buf);
2506 info->xmit_buf = NULL;
2507 }
2508
2509 info->IER = 0;
2510 outb(0x00, info->base + UART_IER);
2511
2512 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
2513 info->MCR &= ~(UART_MCR_DTR | UART_MCR_RTS);
2514 outb(info->MCR, info->base + UART_MCR);
2515
2516 /* clear Rx/Tx FIFO's */
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002517 /* following add by Victor Yu. 08-30-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 if (info->IsMoxaMustChipFlag)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002519 outb((UART_FCR_CLEAR_RCVR |
2520 UART_FCR_CLEAR_XMIT |
2521 MOXA_MUST_FCR_GDA_MODE_ENABLE), info->base + UART_FCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 else
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002523 /* above add by Victor Yu. 08-30-2002 */
2524 outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
2525 info->base + UART_FCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526
2527 /* read data port to reset things */
2528 (void) inb(info->base + UART_RX);
2529
2530 if (info->tty)
2531 set_bit(TTY_IO_ERROR, &info->tty->flags);
2532
2533 info->flags &= ~ASYNC_INITIALIZED;
2534
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002535 /* following add by Victor Yu. 09-23-2002 */
2536 if (info->IsMoxaMustChipFlag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 SET_MOXA_MUST_NO_SOFTWARE_FLOW_CONTROL(info->base);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002538 /* above add by Victor Yu. 09-23-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539
2540 spin_unlock_irqrestore(&info->slock, flags);
2541}
2542
2543/*
2544 * This routine is called to set the UART divisor registers to match
2545 * the specified baud rate for a serial port.
2546 */
Alan Cox606d0992006-12-08 02:38:45 -08002547static int mxser_change_speed(struct mxser_struct *info, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548{
2549 unsigned cflag, cval, fcr;
2550 int ret = 0;
2551 unsigned char status;
2552 long baud;
2553 unsigned long flags;
2554
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 if (!info->tty || !info->tty->termios)
2556 return ret;
2557 cflag = info->tty->termios->c_cflag;
2558 if (!(info->base))
2559 return ret;
2560
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561#ifndef B921600
2562#define B921600 (B460800 +1)
2563#endif
2564 if (mxser_set_baud_method[info->port] == 0) {
Alan Coxc7bce302006-09-30 23:27:24 -07002565 baud = tty_get_baud_rate(info->tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 mxser_set_baud(info, baud);
2567 }
2568
2569 /* byte size and parity */
2570 switch (cflag & CSIZE) {
2571 case CS5:
2572 cval = 0x00;
2573 break;
2574 case CS6:
2575 cval = 0x01;
2576 break;
2577 case CS7:
2578 cval = 0x02;
2579 break;
2580 case CS8:
2581 cval = 0x03;
2582 break;
2583 default:
2584 cval = 0x00;
2585 break; /* too keep GCC shut... */
2586 }
2587 if (cflag & CSTOPB)
2588 cval |= 0x04;
2589 if (cflag & PARENB)
2590 cval |= UART_LCR_PARITY;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002591 if (!(cflag & PARODD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 cval |= UART_LCR_EPAR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 if (cflag & CMSPAR)
2594 cval |= UART_LCR_SPAR;
2595
2596 if ((info->type == PORT_8250) || (info->type == PORT_16450)) {
2597 if (info->IsMoxaMustChipFlag) {
2598 fcr = UART_FCR_ENABLE_FIFO;
2599 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
2600 SET_MOXA_MUST_FIFO_VALUE(info);
2601 } else
2602 fcr = 0;
2603 } else {
2604 fcr = UART_FCR_ENABLE_FIFO;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002605 /* following add by Victor Yu. 08-30-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 if (info->IsMoxaMustChipFlag) {
2607 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
2608 SET_MOXA_MUST_FIFO_VALUE(info);
2609 } else {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002610 /* above add by Victor Yu. 08-30-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 switch (info->rx_trigger) {
2612 case 1:
2613 fcr |= UART_FCR_TRIGGER_1;
2614 break;
2615 case 4:
2616 fcr |= UART_FCR_TRIGGER_4;
2617 break;
2618 case 8:
2619 fcr |= UART_FCR_TRIGGER_8;
2620 break;
2621 default:
2622 fcr |= UART_FCR_TRIGGER_14;
2623 break;
2624 }
2625 }
2626 }
2627
2628 /* CTS flow control flag and modem status interrupts */
2629 info->IER &= ~UART_IER_MSI;
2630 info->MCR &= ~UART_MCR_AFE;
2631 if (cflag & CRTSCTS) {
2632 info->flags |= ASYNC_CTS_FLOW;
2633 info->IER |= UART_IER_MSI;
2634 if ((info->type == PORT_16550A) || (info->IsMoxaMustChipFlag)) {
2635 info->MCR |= UART_MCR_AFE;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002636 /* status = mxser_get_msr(info->base, 0, info->port); */
2637/*
2638 save_flags(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 cli();
2640 status = inb(baseaddr + UART_MSR);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002641 restore_flags(flags);
2642*/
2643 /* mxser_check_modem_status(info, status); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 } else {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002645 /* status = mxser_get_msr(info->base, 0, info->port); */
2646 /* MX_LOCK(&info->slock); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 status = inb(info->base + UART_MSR);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002648 /* MX_UNLOCK(&info->slock); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 if (info->tty->hw_stopped) {
2650 if (status & UART_MSR_CTS) {
2651 info->tty->hw_stopped = 0;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002652 if ((info->type != PORT_16550A) &&
2653 (!info->IsMoxaMustChipFlag)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 info->IER |= UART_IER_THRI;
2655 outb(info->IER, info->base + UART_IER);
2656 }
2657 set_bit(MXSER_EVENT_TXLOW, &info->event);
2658 schedule_work(&info->tqueue); }
2659 } else {
2660 if (!(status & UART_MSR_CTS)) {
2661 info->tty->hw_stopped = 1;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002662 if ((info->type != PORT_16550A) &&
2663 (!info->IsMoxaMustChipFlag)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 info->IER &= ~UART_IER_THRI;
2665 outb(info->IER, info->base + UART_IER);
2666 }
2667 }
2668 }
2669 }
2670 } else {
2671 info->flags &= ~ASYNC_CTS_FLOW;
2672 }
2673 outb(info->MCR, info->base + UART_MCR);
2674 if (cflag & CLOCAL) {
2675 info->flags &= ~ASYNC_CHECK_CD;
2676 } else {
2677 info->flags |= ASYNC_CHECK_CD;
2678 info->IER |= UART_IER_MSI;
2679 }
2680 outb(info->IER, info->base + UART_IER);
2681
2682 /*
2683 * Set up parity check flag
2684 */
2685 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
2686 if (I_INPCK(info->tty))
2687 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
2688 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
2689 info->read_status_mask |= UART_LSR_BI;
2690
2691 info->ignore_status_mask = 0;
2692
2693 if (I_IGNBRK(info->tty)) {
2694 info->ignore_status_mask |= UART_LSR_BI;
2695 info->read_status_mask |= UART_LSR_BI;
2696 /*
2697 * If we're ignore parity and break indicators, ignore
2698 * overruns too. (For real raw support).
2699 */
2700 if (I_IGNPAR(info->tty)) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002701 info->ignore_status_mask |=
2702 UART_LSR_OE |
2703 UART_LSR_PE |
2704 UART_LSR_FE;
2705 info->read_status_mask |=
2706 UART_LSR_OE |
2707 UART_LSR_PE |
2708 UART_LSR_FE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 }
2710 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002711 /* following add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 if (info->IsMoxaMustChipFlag) {
2713 spin_lock_irqsave(&info->slock, flags);
2714 SET_MOXA_MUST_XON1_VALUE(info->base, START_CHAR(info->tty));
2715 SET_MOXA_MUST_XOFF1_VALUE(info->base, STOP_CHAR(info->tty));
2716 if (I_IXON(info->tty)) {
2717 ENABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->base);
2718 } else {
2719 DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->base);
2720 }
2721 if (I_IXOFF(info->tty)) {
2722 ENABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->base);
2723 } else {
2724 DISABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->base);
2725 }
2726 /*
2727 if ( I_IXANY(info->tty) ) {
2728 info->MCR |= MOXA_MUST_MCR_XON_ANY;
2729 ENABLE_MOXA_MUST_XON_ANY_FLOW_CONTROL(info->base);
2730 } else {
2731 info->MCR &= ~MOXA_MUST_MCR_XON_ANY;
2732 DISABLE_MOXA_MUST_XON_ANY_FLOW_CONTROL(info->base);
2733 }
2734 */
2735 spin_unlock_irqrestore(&info->slock, flags);
2736 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002737 /* above add by Victor Yu. 09-02-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738
2739
2740 outb(fcr, info->base + UART_FCR); /* set fcr */
2741 outb(cval, info->base + UART_LCR);
2742
2743 return ret;
2744}
2745
2746
2747static int mxser_set_baud(struct mxser_struct *info, long newspd)
2748{
2749 int quot = 0;
2750 unsigned char cval;
2751 int ret = 0;
2752 unsigned long flags;
2753
2754 if (!info->tty || !info->tty->termios)
2755 return ret;
2756
2757 if (!(info->base))
2758 return ret;
2759
2760 if (newspd > info->MaxCanSetBaudRate)
2761 return 0;
2762
2763 info->realbaud = newspd;
2764 if (newspd == 134) {
2765 quot = (2 * info->baud_base / 269);
2766 } else if (newspd) {
2767 quot = info->baud_base / newspd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 if (quot == 0)
2769 quot = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 } else {
2771 quot = 0;
2772 }
2773
2774 info->timeout = ((info->xmit_fifo_size * HZ * 10 * quot) / info->baud_base);
2775 info->timeout += HZ / 50; /* Add .02 seconds of slop */
2776
2777 if (quot) {
2778 spin_lock_irqsave(&info->slock, flags);
2779 info->MCR |= UART_MCR_DTR;
2780 outb(info->MCR, info->base + UART_MCR);
2781 spin_unlock_irqrestore(&info->slock, flags);
2782 } else {
2783 spin_lock_irqsave(&info->slock, flags);
2784 info->MCR &= ~UART_MCR_DTR;
2785 outb(info->MCR, info->base + UART_MCR);
2786 spin_unlock_irqrestore(&info->slock, flags);
2787 return ret;
2788 }
2789
2790 cval = inb(info->base + UART_LCR);
2791
2792 outb(cval | UART_LCR_DLAB, info->base + UART_LCR); /* set DLAB */
2793
2794 outb(quot & 0xff, info->base + UART_DLL); /* LS of divisor */
2795 outb(quot >> 8, info->base + UART_DLM); /* MS of divisor */
2796 outb(cval, info->base + UART_LCR); /* reset DLAB */
2797
2798
2799 return ret;
2800}
2801
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802/*
2803 * ------------------------------------------------------------
2804 * friends of mxser_ioctl()
2805 * ------------------------------------------------------------
2806 */
2807static int mxser_get_serial_info(struct mxser_struct *info, struct serial_struct __user *retinfo)
2808{
2809 struct serial_struct tmp;
2810
2811 if (!retinfo)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002812 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 memset(&tmp, 0, sizeof(tmp));
2814 tmp.type = info->type;
2815 tmp.line = info->port;
2816 tmp.port = info->base;
2817 tmp.irq = info->irq;
2818 tmp.flags = info->flags;
2819 tmp.baud_base = info->baud_base;
2820 tmp.close_delay = info->close_delay;
2821 tmp.closing_wait = info->closing_wait;
2822 tmp.custom_divisor = info->custom_divisor;
2823 tmp.hub6 = 0;
2824 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2825 return -EFAULT;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002826 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827}
2828
2829static int mxser_set_serial_info(struct mxser_struct *info, struct serial_struct __user *new_info)
2830{
2831 struct serial_struct new_serial;
2832 unsigned int flags;
2833 int retval = 0;
2834
2835 if (!new_info || !info->base)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002836 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
2838 return -EFAULT;
2839
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002840 if ((new_serial.irq != info->irq) ||
2841 (new_serial.port != info->base) ||
2842 (new_serial.custom_divisor != info->custom_divisor) ||
2843 (new_serial.baud_base != info->baud_base))
2844 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845
2846 flags = info->flags & ASYNC_SPD_MASK;
2847
2848 if (!capable(CAP_SYS_ADMIN)) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002849 if ((new_serial.baud_base != info->baud_base) ||
2850 (new_serial.close_delay != info->close_delay) ||
2851 ((new_serial.flags & ~ASYNC_USR_MASK) != (info->flags & ~ASYNC_USR_MASK)))
2852 return -EPERM;
2853 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
2854 (new_serial.flags & ASYNC_USR_MASK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 } else {
2856 /*
2857 * OK, past this point, all the error checking has been done.
2858 * At this point, we start making changes.....
2859 */
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002860 info->flags = ((info->flags & ~ASYNC_FLAGS) |
2861 (new_serial.flags & ASYNC_FLAGS));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 info->close_delay = new_serial.close_delay * HZ / 100;
2863 info->closing_wait = new_serial.closing_wait * HZ / 100;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002864 info->tty->low_latency =
2865 (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
2866 info->tty->low_latency = 0; /* (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0; */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 }
2868
2869 /* added by casper, 3/17/2000, for mouse */
2870 info->type = new_serial.type;
2871
2872 process_txrx_fifo(info);
2873
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 if (info->flags & ASYNC_INITIALIZED) {
2875 if (flags != (info->flags & ASYNC_SPD_MASK)) {
2876 mxser_change_speed(info, NULL);
2877 }
2878 } else {
2879 retval = mxser_startup(info);
2880 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002881 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882}
2883
2884/*
2885 * mxser_get_lsr_info - get line status register info
2886 *
2887 * Purpose: Let user call ioctl() to get info when the UART physically
2888 * is emptied. On bus types like RS485, the transmitter must
2889 * release the bus after transmitting. This must be done when
2890 * the transmit shift register is empty, not be done when the
2891 * transmit holding register is empty. This functionality
2892 * allows an RS485 driver to be written in user space.
2893 */
2894static int mxser_get_lsr_info(struct mxser_struct *info, unsigned int __user *value)
2895{
2896 unsigned char status;
2897 unsigned int result;
2898 unsigned long flags;
2899
2900 spin_lock_irqsave(&info->slock, flags);
2901 status = inb(info->base + UART_LSR);
2902 spin_unlock_irqrestore(&info->slock, flags);
2903 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
2904 return put_user(result, value);
2905}
2906
2907/*
2908 * This routine sends a break character out the serial port.
2909 */
2910static void mxser_send_break(struct mxser_struct *info, int duration)
2911{
2912 unsigned long flags;
2913
2914 if (!info->base)
2915 return;
2916 set_current_state(TASK_INTERRUPTIBLE);
2917 spin_lock_irqsave(&info->slock, flags);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002918 outb(inb(info->base + UART_LCR) | UART_LCR_SBC,
2919 info->base + UART_LCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 spin_unlock_irqrestore(&info->slock, flags);
2921 schedule_timeout(duration);
2922 spin_lock_irqsave(&info->slock, flags);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002923 outb(inb(info->base + UART_LCR) & ~UART_LCR_SBC,
2924 info->base + UART_LCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 spin_unlock_irqrestore(&info->slock, flags);
2926}
2927
2928static int mxser_tiocmget(struct tty_struct *tty, struct file *file)
2929{
Jesper Juhl56e139f2006-06-25 05:47:52 -07002930 struct mxser_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 unsigned char control, status;
2932 unsigned long flags;
2933
2934
2935 if (tty->index == MXSER_PORTS)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002936 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 if (tty->flags & (1 << TTY_IO_ERROR))
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002938 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939
2940 control = info->MCR;
2941
2942 spin_lock_irqsave(&info->slock, flags);
2943 status = inb(info->base + UART_MSR);
2944 if (status & UART_MSR_ANY_DELTA)
2945 mxser_check_modem_status(info, status);
2946 spin_unlock_irqrestore(&info->slock, flags);
2947 return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0) |
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002948 ((control & UART_MCR_DTR) ? TIOCM_DTR : 0) |
2949 ((status & UART_MSR_DCD) ? TIOCM_CAR : 0) |
2950 ((status & UART_MSR_RI) ? TIOCM_RNG : 0) |
2951 ((status & UART_MSR_DSR) ? TIOCM_DSR : 0) |
2952 ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953}
2954
2955static int mxser_tiocmset(struct tty_struct *tty, struct file *file, unsigned int set, unsigned int clear)
2956{
Jesper Juhl56e139f2006-06-25 05:47:52 -07002957 struct mxser_struct *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 unsigned long flags;
2959
2960
2961 if (tty->index == MXSER_PORTS)
2962 return -ENOIOCTLCMD;
2963 if (tty->flags & (1 << TTY_IO_ERROR))
2964 return -EIO;
2965
2966 spin_lock_irqsave(&info->slock, flags);
2967
2968 if (set & TIOCM_RTS)
2969 info->MCR |= UART_MCR_RTS;
2970 if (set & TIOCM_DTR)
2971 info->MCR |= UART_MCR_DTR;
2972
2973 if (clear & TIOCM_RTS)
2974 info->MCR &= ~UART_MCR_RTS;
2975 if (clear & TIOCM_DTR)
2976 info->MCR &= ~UART_MCR_DTR;
2977
2978 outb(info->MCR, info->base + UART_MCR);
2979 spin_unlock_irqrestore(&info->slock, flags);
2980 return 0;
2981}
2982
2983
2984static int mxser_read_register(int, unsigned short *);
2985static int mxser_program_mode(int);
2986static void mxser_normal_mode(int);
2987
2988static int mxser_get_ISA_conf(int cap, struct mxser_hwconf *hwconf)
2989{
2990 int id, i, bits;
2991 unsigned short regs[16], irq;
2992 unsigned char scratch, scratch2;
2993
2994 hwconf->IsMoxaMustChipFlag = MOXA_OTHER_UART;
2995
2996 id = mxser_read_register(cap, regs);
2997 if (id == C168_ASIC_ID) {
2998 hwconf->board_type = MXSER_BOARD_C168_ISA;
2999 hwconf->ports = 8;
3000 } else if (id == C104_ASIC_ID) {
3001 hwconf->board_type = MXSER_BOARD_C104_ISA;
3002 hwconf->ports = 4;
3003 } else if (id == C102_ASIC_ID) {
3004 hwconf->board_type = MXSER_BOARD_C102_ISA;
3005 hwconf->ports = 2;
3006 } else if (id == CI132_ASIC_ID) {
3007 hwconf->board_type = MXSER_BOARD_CI132;
3008 hwconf->ports = 2;
3009 } else if (id == CI134_ASIC_ID) {
3010 hwconf->board_type = MXSER_BOARD_CI134;
3011 hwconf->ports = 4;
3012 } else if (id == CI104J_ASIC_ID) {
3013 hwconf->board_type = MXSER_BOARD_CI104J;
3014 hwconf->ports = 4;
3015 } else
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07003016 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017
3018 irq = 0;
3019 if (hwconf->ports == 2) {
3020 irq = regs[9] & 0xF000;
3021 irq = irq | (irq >> 4);
3022 if (irq != (regs[9] & 0xFF00))
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07003023 return MXSER_ERR_IRQ_CONFLIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 } else if (hwconf->ports == 4) {
3025 irq = regs[9] & 0xF000;
3026 irq = irq | (irq >> 4);
3027 irq = irq | (irq >> 8);
3028 if (irq != regs[9])
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07003029 return MXSER_ERR_IRQ_CONFLIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 } else if (hwconf->ports == 8) {
3031 irq = regs[9] & 0xF000;
3032 irq = irq | (irq >> 4);
3033 irq = irq | (irq >> 8);
3034 if ((irq != regs[9]) || (irq != regs[10]))
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07003035 return MXSER_ERR_IRQ_CONFLIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 }
3037
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07003038 if (!irq)
3039 return MXSER_ERR_IRQ;
3040 hwconf->irq = ((int)(irq & 0xF000) >> 12);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 for (i = 0; i < 8; i++)
3042 hwconf->ioaddr[i] = (int) regs[i + 1] & 0xFFF8;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07003043 if ((regs[12] & 0x80) == 0)
3044 return MXSER_ERR_VECTOR;
3045 hwconf->vector = (int)regs[11]; /* interrupt vector */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 if (id == 1)
3047 hwconf->vector_mask = 0x00FF;
3048 else
3049 hwconf->vector_mask = 0x000F;
3050 for (i = 7, bits = 0x0100; i >= 0; i--, bits <<= 1) {
3051 if (regs[12] & bits) {
3052 hwconf->baud_base[i] = 921600;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07003053 hwconf->MaxCanSetBaudRate[i] = 921600; /* add by Victor Yu. 09-04-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 } else {
3055 hwconf->baud_base[i] = 115200;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07003056 hwconf->MaxCanSetBaudRate[i] = 115200; /* add by Victor Yu. 09-04-2002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 }
3058 }
3059 scratch2 = inb(cap + UART_LCR) & (~UART_LCR_DLAB);
3060 outb(scratch2 | UART_LCR_DLAB, cap + UART_LCR);
3061 outb(0, cap + UART_EFR); /* EFR is the same as FCR */
3062 outb(scratch2, cap + UART_LCR);
3063 outb(UART_FCR_ENABLE_FIFO, cap + UART_FCR);
3064 scratch = inb(cap + UART_IIR);
3065
3066 if (scratch & 0xC0)
3067 hwconf->uart_type = PORT_16550A;
3068 else
3069 hwconf->uart_type = PORT_16450;
3070 if (id == 1)
3071 hwconf->ports = 8;
3072 else
3073 hwconf->ports = 4;
3074 request_region(hwconf->ioaddr[0], 8 * hwconf->ports, "mxser(IO)");
3075 request_region(hwconf->vector, 1, "mxser(vector)");
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07003076 return hwconf->ports;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077}
3078
3079#define CHIP_SK 0x01 /* Serial Data Clock in Eprom */
3080#define CHIP_DO 0x02 /* Serial Data Output in Eprom */
3081#define CHIP_CS 0x04 /* Serial Chip Select in Eprom */
3082#define CHIP_DI 0x08 /* Serial Data Input in Eprom */
3083#define EN_CCMD 0x000 /* Chip's command register */
3084#define EN0_RSARLO 0x008 /* Remote start address reg 0 */
3085#define EN0_RSARHI 0x009 /* Remote start address reg 1 */
3086#define EN0_RCNTLO 0x00A /* Remote byte count reg WR */
3087#define EN0_RCNTHI 0x00B /* Remote byte count reg WR */
3088#define EN0_DCFG 0x00E /* Data configuration reg WR */
3089#define EN0_PORT 0x010 /* Rcv missed frame error counter RD */
3090#define ENC_PAGE0 0x000 /* Select page 0 of chip registers */
3091#define ENC_PAGE3 0x0C0 /* Select page 3 of chip registers */
3092static int mxser_read_register(int port, unsigned short *regs)
3093{
3094 int i, k, value, id;
3095 unsigned int j;
3096
3097 id = mxser_program_mode(port);
3098 if (id < 0)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07003099 return id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 for (i = 0; i < 14; i++) {
3101 k = (i & 0x3F) | 0x180;
3102 for (j = 0x100; j > 0; j >>= 1) {
3103 outb(CHIP_CS, port);
3104 if (k & j) {
3105 outb(CHIP_CS | CHIP_DO, port);
3106 outb(CHIP_CS | CHIP_DO | CHIP_SK, port); /* A? bit of read */
3107 } else {
3108 outb(CHIP_CS, port);
3109 outb(CHIP_CS | CHIP_SK, port); /* A? bit of read */
3110 }
3111 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07003112 (void)inb(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 value = 0;
3114 for (k = 0, j = 0x8000; k < 16; k++, j >>= 1) {
3115 outb(CHIP_CS, port);
3116 outb(CHIP_CS | CHIP_SK, port);
3117 if (inb(port) & CHIP_DI)
3118 value |= j;
3119 }
3120 regs[i] = value;
3121 outb(0, port);
3122 }
3123 mxser_normal_mode(port);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07003124 return id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125}
3126
3127static int mxser_program_mode(int port)
3128{
3129 int id, i, j, n;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07003130 /* unsigned long flags; */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131
3132 spin_lock(&gm_lock);
3133 outb(0, port);
3134 outb(0, port);
3135 outb(0, port);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07003136 (void)inb(port);
3137 (void)inb(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 outb(0, port);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07003139 (void)inb(port);
3140 /* restore_flags(flags); */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 spin_unlock(&gm_lock);
3142
3143 id = inb(port + 1) & 0x1F;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07003144 if ((id != C168_ASIC_ID) &&
3145 (id != C104_ASIC_ID) &&
3146 (id != C102_ASIC_ID) &&
3147 (id != CI132_ASIC_ID) &&
3148 (id != CI134_ASIC_ID) &&
3149 (id != CI104J_ASIC_ID))
3150 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 for (i = 0, j = 0; i < 4; i++) {
3152 n = inb(port + 2);
3153 if (n == 'M') {
3154 j = 1;
3155 } else if ((j == 1) && (n == 1)) {
3156 j = 2;
3157 break;
3158 } else
3159 j = 0;
3160 }
3161 if (j != 2)
3162 id = -2;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07003163 return id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164}
3165
3166static void mxser_normal_mode(int port)
3167{
3168 int i, n;
3169
3170 outb(0xA5, port + 1);
3171 outb(0x80, port + 3);
3172 outb(12, port + 0); /* 9600 bps */
3173 outb(0, port + 1);
3174 outb(0x03, port + 3); /* 8 data bits */
3175 outb(0x13, port + 4); /* loop back mode */
3176 for (i = 0; i < 16; i++) {
3177 n = inb(port + 5);
3178 if ((n & 0x61) == 0x60)
3179 break;
3180 if ((n & 1) == 1)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07003181 (void)inb(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 }
3183 outb(0x00, port + 4);
3184}
3185
3186module_init(mxser_module_init);
3187module_exit(mxser_module_exit);