blob: 0e22f80104464f15386bf91e0e2ab2a1600b16ba [file] [log] [blame]
Jiri Slaby037ad482006-12-08 02:38:11 -08001/*
2 * mxser.c -- MOXA Smartio/Industio family multiport serial driver.
3 *
Jiri Slaby3306ce32006-12-08 02:38:13 -08004 * Copyright (C) 1999-2006 Moxa Technologies (support@moxa.com.tw).
Jiri Slabyc88cb8f2006-12-08 02:38:20 -08005 * Copyright (C) 2006 Jiri Slaby <jirislaby@gmail.com>
Jiri Slaby037ad482006-12-08 02:38:11 -08006 *
Jiri Slabyc88cb8f2006-12-08 02:38:20 -08007 * This code is loosely based on the 1.8 moxa driver which is based on
8 * Linux serial driver, written by Linus Torvalds, Theodore T'so and
9 * others.
Jiri Slaby037ad482006-12-08 02:38:11 -080010 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
Jiri Slaby037ad482006-12-08 02:38:11 -080016 * Fed through a cleanup, indent and remove of non 2.6 code by Alan Cox
17 * <alan@redhat.com>. The original 1.8 code is available on www.moxa.com.
18 * - Fixed x86_64 cleanness
19 * - Fixed sleep with spinlock held in mxser_send_break
20 */
21
Jiri Slaby037ad482006-12-08 02:38:11 -080022#include <linux/module.h>
23#include <linux/autoconf.h>
24#include <linux/errno.h>
25#include <linux/signal.h>
26#include <linux/sched.h>
27#include <linux/timer.h>
28#include <linux/interrupt.h>
29#include <linux/tty.h>
30#include <linux/tty_flip.h>
31#include <linux/serial.h>
32#include <linux/serial_reg.h>
33#include <linux/major.h>
34#include <linux/string.h>
35#include <linux/fcntl.h>
36#include <linux/ptrace.h>
37#include <linux/gfp.h>
38#include <linux/ioport.h>
39#include <linux/mm.h>
40#include <linux/smp_lock.h>
41#include <linux/delay.h>
42#include <linux/pci.h>
43
44#include <asm/system.h>
45#include <asm/io.h>
46#include <asm/irq.h>
47#include <asm/bitops.h>
48#include <asm/uaccess.h>
49
Jiri Slaby771f2d12006-12-08 02:38:12 -080050#include "mxser_new.h"
Jiri Slaby037ad482006-12-08 02:38:11 -080051
Jiri Slaby55b307d2006-12-08 02:38:14 -080052#define MXSER_VERSION "2.0"
Jiri Slaby037ad482006-12-08 02:38:11 -080053#define MXSERMAJOR 174
54#define MXSERCUMAJOR 175
55
56#define MXSER_EVENT_TXLOW 1
57#define MXSER_EVENT_HANGUP 2
58
59#define MXSER_BOARDS 4 /* Max. boards */
60#define MXSER_PORTS 32 /* Max. ports */
61#define MXSER_PORTS_PER_BOARD 8 /* Max. ports per board */
Jiri Slaby3306ce32006-12-08 02:38:13 -080062#define MXSER_ISR_PASS_LIMIT 99999L
Jiri Slaby037ad482006-12-08 02:38:11 -080063
64#define MXSER_ERR_IOADDR -1
65#define MXSER_ERR_IRQ -2
66#define MXSER_ERR_IRQ_CONFLIT -3
67#define MXSER_ERR_VECTOR -4
68
69#define SERIAL_TYPE_NORMAL 1
70#define SERIAL_TYPE_CALLOUT 2
71
72#define WAKEUP_CHARS 256
73
74#define UART_MCR_AFE 0x20
75#define UART_LSR_SPECIAL 0x1E
76
77#define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK|\
78 IXON|IXOFF))
79
Jiri Slaby037ad482006-12-08 02:38:11 -080080#define C168_ASIC_ID 1
81#define C104_ASIC_ID 2
82#define C102_ASIC_ID 0xB
83#define CI132_ASIC_ID 4
84#define CI134_ASIC_ID 3
85#define CI104J_ASIC_ID 5
86
87enum {
88 MXSER_BOARD_C168_ISA = 1,
89 MXSER_BOARD_C104_ISA,
90 MXSER_BOARD_CI104J,
91 MXSER_BOARD_C168_PCI,
92 MXSER_BOARD_C104_PCI,
93 MXSER_BOARD_C102_ISA,
94 MXSER_BOARD_CI132,
95 MXSER_BOARD_CI134,
96 MXSER_BOARD_CP132,
97 MXSER_BOARD_CP114,
98 MXSER_BOARD_CT114,
99 MXSER_BOARD_CP102,
100 MXSER_BOARD_CP104U,
101 MXSER_BOARD_CP168U,
102 MXSER_BOARD_CP132U,
103 MXSER_BOARD_CP134U,
104 MXSER_BOARD_CP104JU,
105 MXSER_BOARD_RC7000,
106 MXSER_BOARD_CP118U,
107 MXSER_BOARD_CP102UL,
108 MXSER_BOARD_CP102U,
Jiri Slaby3306ce32006-12-08 02:38:13 -0800109 MXSER_BOARD_CP118EL,
110 MXSER_BOARD_CP168EL,
111 MXSER_BOARD_CP104EL
Jiri Slaby037ad482006-12-08 02:38:11 -0800112};
113
114static char *mxser_brdname[] = {
115 "C168 series",
116 "C104 series",
117 "CI-104J series",
118 "C168H/PCI series",
119 "C104H/PCI series",
120 "C102 series",
121 "CI-132 series",
122 "CI-134 series",
123 "CP-132 series",
124 "CP-114 series",
125 "CT-114 series",
126 "CP-102 series",
127 "CP-104U series",
128 "CP-168U series",
129 "CP-132U series",
130 "CP-134U series",
131 "CP-104JU series",
132 "Moxa UC7000 Serial",
133 "CP-118U series",
134 "CP-102UL series",
135 "CP-102U series",
Jiri Slaby3306ce32006-12-08 02:38:13 -0800136 "CP-118EL series",
137 "CP-168EL series",
138 "CP-104EL series"
Jiri Slaby037ad482006-12-08 02:38:11 -0800139};
140
141static int mxser_numports[] = {
142 8, /* C168-ISA */
143 4, /* C104-ISA */
144 4, /* CI104J */
145 8, /* C168-PCI */
146 4, /* C104-PCI */
147 2, /* C102-ISA */
148 2, /* CI132 */
149 4, /* CI134 */
150 2, /* CP132 */
151 4, /* CP114 */
152 4, /* CT114 */
153 2, /* CP102 */
154 4, /* CP104U */
155 8, /* CP168U */
156 2, /* CP132U */
157 4, /* CP134U */
158 4, /* CP104JU */
159 8, /* RC7000 */
160 8, /* CP118U */
161 2, /* CP102UL */
162 2, /* CP102U */
Jiri Slaby3306ce32006-12-08 02:38:13 -0800163 8, /* CP118EL */
164 8, /* CP168EL */
165 4 /* CP104EL */
Jiri Slaby037ad482006-12-08 02:38:11 -0800166};
167
168#define UART_TYPE_NUM 2
169
170static const unsigned int Gmoxa_uart_id[UART_TYPE_NUM] = {
171 MOXA_MUST_MU150_HWID,
172 MOXA_MUST_MU860_HWID
173};
174
175/* This is only for PCI */
176#define UART_INFO_NUM 3
177struct mxpciuart_info {
178 int type;
179 int tx_fifo;
180 int rx_fifo;
181 int xmit_fifo_size;
182 int rx_high_water;
183 int rx_trigger;
184 int rx_low_water;
185 long max_baud;
186};
187
188static const struct mxpciuart_info Gpci_uart_info[UART_INFO_NUM] = {
189 {MOXA_OTHER_UART, 16, 16, 16, 14, 14, 1, 921600L},
190 {MOXA_MUST_MU150_HWID, 64, 64, 64, 48, 48, 16, 230400L},
191 {MOXA_MUST_MU860_HWID, 128, 128, 128, 96, 96, 32, 921600L}
192};
193
194
Jiri Slaby037ad482006-12-08 02:38:11 -0800195static struct pci_device_id mxser_pcibrds[] = {
Jiri Slaby3306ce32006-12-08 02:38:13 -0800196 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C168),
197 .driver_data = MXSER_BOARD_C168_PCI },
198 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C104),
199 .driver_data = MXSER_BOARD_C104_PCI },
200 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP132),
201 .driver_data = MXSER_BOARD_CP132 },
202 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP114),
203 .driver_data = MXSER_BOARD_CP114 },
204 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CT114),
205 .driver_data = MXSER_BOARD_CT114 },
206 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP102),
207 .driver_data = MXSER_BOARD_CP102 },
208 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP104U),
209 .driver_data = MXSER_BOARD_CP104U },
210 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP168U),
211 .driver_data = MXSER_BOARD_CP168U },
212 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP132U),
213 .driver_data = MXSER_BOARD_CP132U },
214 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP134U),
215 .driver_data = MXSER_BOARD_CP134U },
216 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP104JU),
217 .driver_data = MXSER_BOARD_CP104JU },
218 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_RC7000),
219 .driver_data = MXSER_BOARD_RC7000 },
220 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP118U),
221 .driver_data = MXSER_BOARD_CP118U },
222 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP102UL),
223 .driver_data = MXSER_BOARD_CP102UL },
224 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP102U),
225 .driver_data = MXSER_BOARD_CP102U },
226 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP118EL),
227 .driver_data = MXSER_BOARD_CP118EL },
228 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP168EL),
229 .driver_data = MXSER_BOARD_CP168EL },
230 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP104EL),
231 .driver_data = MXSER_BOARD_CP104EL },
232 { }
Jiri Slaby037ad482006-12-08 02:38:11 -0800233};
Jiri Slaby037ad482006-12-08 02:38:11 -0800234MODULE_DEVICE_TABLE(pci, mxser_pcibrds);
235
Jiri Slaby037ad482006-12-08 02:38:11 -0800236static int ioaddr[MXSER_BOARDS] = { 0, 0, 0, 0 };
237static int ttymajor = MXSERMAJOR;
238static int calloutmajor = MXSERCUMAJOR;
Jiri Slaby037ad482006-12-08 02:38:11 -0800239
240/* Variables for insmod */
241
242MODULE_AUTHOR("Casper Yang");
243MODULE_DESCRIPTION("MOXA Smartio/Industio Family Multiport Board Device Driver");
244module_param_array(ioaddr, int, NULL, 0);
245module_param(ttymajor, int, 0);
Jiri Slaby037ad482006-12-08 02:38:11 -0800246MODULE_LICENSE("GPL");
247
248struct mxser_log {
249 int tick;
250 unsigned long rxcnt[MXSER_PORTS];
251 unsigned long txcnt[MXSER_PORTS];
252};
253
254
255struct mxser_mon {
256 unsigned long rxcnt;
257 unsigned long txcnt;
258 unsigned long up_rxcnt;
259 unsigned long up_txcnt;
260 int modem_status;
261 unsigned char hold_reason;
262};
263
264struct mxser_mon_ext {
265 unsigned long rx_cnt[32];
266 unsigned long tx_cnt[32];
267 unsigned long up_rxcnt[32];
268 unsigned long up_txcnt[32];
269 int modem_status[32];
270
271 long baudrate[32];
272 int databits[32];
273 int stopbits[32];
274 int parity[32];
275 int flowctrl[32];
276 int fifo[32];
277 int iftype[32];
278};
279
Jiri Slaby55b307d2006-12-08 02:38:14 -0800280struct mxser_board;
Jiri Slaby037ad482006-12-08 02:38:11 -0800281
Jiri Slaby55b307d2006-12-08 02:38:14 -0800282struct mxser_port {
283 struct mxser_board *board;
284 struct tty_struct *tty;
285
286 unsigned long ioaddr;
287 unsigned long opmode_ioaddr;
288 int max_baud;
289
Jiri Slaby037ad482006-12-08 02:38:11 -0800290 int rx_high_water;
291 int rx_trigger; /* Rx fifo trigger level */
292 int rx_low_water;
293 int baud_base; /* max. speed */
Jiri Slaby55b307d2006-12-08 02:38:14 -0800294 long realbaud;
Jiri Slaby037ad482006-12-08 02:38:11 -0800295 int type; /* UART type */
Jiri Slaby55b307d2006-12-08 02:38:14 -0800296 int flags; /* defined in tty.h */
297 long session; /* Session of opening process */
298 long pgrp; /* pgrp of opening process */
299
300 int x_char; /* xon/xoff character */
301 int IER; /* Interrupt Enable Register */
302 int MCR; /* Modem control register */
303
304 unsigned char stop_rx;
305 unsigned char ldisc_stop_rx;
306
307 int custom_divisor;
308 int close_delay;
309 unsigned short closing_wait;
310 unsigned char err_shadow;
311 unsigned long event;
312
313 int count; /* # of fd on device */
314 int blocked_open; /* # of blocked opens */
315 struct async_icount icount; /* kernel counters for 4 input interrupts */
316 int timeout;
317
Jiri Slaby037ad482006-12-08 02:38:11 -0800318 int read_status_mask;
319 int ignore_status_mask;
320 int xmit_fifo_size;
Jiri Slaby037ad482006-12-08 02:38:11 -0800321 unsigned char *xmit_buf;
322 int xmit_head;
323 int xmit_tail;
324 int xmit_cnt;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800325
Jiri Slaby037ad482006-12-08 02:38:11 -0800326 struct termios normal_termios;
327 struct termios callout_termios;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800328
329 struct mxser_mon mon_data;
330
331 spinlock_t slock;
332 struct work_struct tqueue;
Jiri Slaby037ad482006-12-08 02:38:11 -0800333 wait_queue_head_t open_wait;
334 wait_queue_head_t close_wait;
335 wait_queue_head_t delta_msr_wait;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800336};
337
338struct mxser_board {
339 struct pci_dev *pdev; /* temporary (until pci probing) */
340
341 int irq;
342 int board_type;
343 unsigned int nports;
344 unsigned long vector;
345 unsigned long vector_mask;
346
347 int chip_flag;
348 int uart_type;
349
350 struct mxser_port ports[MXSER_PORTS_PER_BOARD];
Jiri Slaby037ad482006-12-08 02:38:11 -0800351};
352
353struct mxser_mstatus {
354 tcflag_t cflag;
355 int cts;
356 int dsr;
357 int ri;
358 int dcd;
359};
360
361static struct mxser_mstatus GMStatus[MXSER_PORTS];
362
363static int mxserBoardCAP[MXSER_BOARDS] = {
364 0, 0, 0, 0
365 /* 0x180, 0x280, 0x200, 0x320 */
366};
367
Jiri Slaby55b307d2006-12-08 02:38:14 -0800368static struct mxser_board mxser_boards[MXSER_BOARDS];
Jiri Slaby037ad482006-12-08 02:38:11 -0800369static struct tty_driver *mxvar_sdriver;
Jiri Slaby037ad482006-12-08 02:38:11 -0800370static struct tty_struct *mxvar_tty[MXSER_PORTS + 1];
371static struct termios *mxvar_termios[MXSER_PORTS + 1];
372static struct termios *mxvar_termios_locked[MXSER_PORTS + 1];
373static struct mxser_log mxvar_log;
374static int mxvar_diagflag;
375static unsigned char mxser_msr[MXSER_PORTS + 1];
376static struct mxser_mon_ext mon_data_ext;
377static int mxser_set_baud_method[MXSER_PORTS + 1];
378static spinlock_t gm_lock;
379
380/*
Jiri Slaby037ad482006-12-08 02:38:11 -0800381 * static functions:
382 */
383
Jiri Slaby037ad482006-12-08 02:38:11 -0800384static int mxser_init(void);
385
386/* static void mxser_poll(unsigned long); */
Jiri Slaby55b307d2006-12-08 02:38:14 -0800387static int mxser_get_ISA_conf(int, struct mxser_board *);
Jiri Slaby037ad482006-12-08 02:38:11 -0800388static void mxser_do_softint(void *);
389static int mxser_open(struct tty_struct *, struct file *);
390static void mxser_close(struct tty_struct *, struct file *);
391static int mxser_write(struct tty_struct *, const unsigned char *, int);
392static int mxser_write_room(struct tty_struct *);
393static void mxser_flush_buffer(struct tty_struct *);
394static int mxser_chars_in_buffer(struct tty_struct *);
395static void mxser_flush_chars(struct tty_struct *);
396static void mxser_put_char(struct tty_struct *, unsigned char);
397static int mxser_ioctl(struct tty_struct *, struct file *, uint, ulong);
398static int mxser_ioctl_special(unsigned int, void __user *);
399static void mxser_throttle(struct tty_struct *);
400static void mxser_unthrottle(struct tty_struct *);
401static void mxser_set_termios(struct tty_struct *, struct termios *);
402static void mxser_stop(struct tty_struct *);
403static void mxser_start(struct tty_struct *);
404static void mxser_hangup(struct tty_struct *);
405static void mxser_rs_break(struct tty_struct *, int);
406static irqreturn_t mxser_interrupt(int, void *, struct pt_regs *);
Jiri Slaby55b307d2006-12-08 02:38:14 -0800407static void mxser_receive_chars(struct mxser_port *, int *);
408static void mxser_transmit_chars(struct mxser_port *);
409static void mxser_check_modem_status(struct mxser_port *, int);
410static int mxser_block_til_ready(struct tty_struct *, struct file *,
411 struct mxser_port *);
412static int mxser_startup(struct mxser_port *);
413static void mxser_shutdown(struct mxser_port *);
414static int mxser_change_speed(struct mxser_port *, struct termios *);
415static int mxser_get_serial_info(struct mxser_port *,
416 struct serial_struct __user *);
417static int mxser_set_serial_info(struct mxser_port *,
418 struct serial_struct __user *);
419static int mxser_get_lsr_info(struct mxser_port *, unsigned int __user *);
420static void mxser_send_break(struct mxser_port *, int);
Jiri Slaby037ad482006-12-08 02:38:11 -0800421static int mxser_tiocmget(struct tty_struct *, struct file *);
Jiri Slaby55b307d2006-12-08 02:38:14 -0800422static int mxser_tiocmset(struct tty_struct *, struct file *, unsigned int,
423 unsigned int);
424static int mxser_set_baud(struct mxser_port *, long);
425static void mxser_wait_until_sent(struct tty_struct *, int);
Jiri Slaby037ad482006-12-08 02:38:11 -0800426
Jiri Slaby55b307d2006-12-08 02:38:14 -0800427static void mxser_startrx(struct tty_struct *);
428static void mxser_stoprx(struct tty_struct *);
Jiri Slaby037ad482006-12-08 02:38:11 -0800429
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);
443 return MOXA_OTHER_UART;
444 }
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])
449 return (int)hwid;
450 }
451 return MOXA_OTHER_UART;
452}
453
454/* above is modified by Victor Yu. 08-15-2002 */
455
456static const struct tty_operations mxser_ops = {
457 .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,
472 .break_ctl = mxser_rs_break,
473 .wait_until_sent = mxser_wait_until_sent,
474 .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
Jiri Slaby7f589922006-12-08 02:38:20 -0800486 pr_debug("Loading module mxser ...\n");
Jiri Slaby037ad482006-12-08 02:38:11 -0800487 ret = mxser_init();
Jiri Slaby7f589922006-12-08 02:38:20 -0800488 pr_debug("Done.\n");
Jiri Slaby037ad482006-12-08 02:38:11 -0800489 return ret;
490}
491
492static void __exit mxser_module_exit(void)
493{
494 int i, err;
495
Jiri Slaby7f589922006-12-08 02:38:20 -0800496 pr_debug("Unloading module mxser ...\n");
Jiri Slaby037ad482006-12-08 02:38:11 -0800497
498 err = tty_unregister_driver(mxvar_sdriver);
499 if (!err)
500 put_tty_driver(mxvar_sdriver);
501 else
502 printk(KERN_ERR "Couldn't unregister MOXA Smartio/Industio family serial driver\n");
503
504 for (i = 0; i < MXSER_BOARDS; i++) {
505 struct pci_dev *pdev;
506
Jiri Slaby55b307d2006-12-08 02:38:14 -0800507 if (mxser_boards[i].board_type == -1)
Jiri Slaby037ad482006-12-08 02:38:11 -0800508 continue;
509 else {
Jiri Slaby55b307d2006-12-08 02:38:14 -0800510 pdev = mxser_boards[i].pdev;
511 free_irq(mxser_boards[i].irq, &mxser_boards[i]);
Jiri Slaby037ad482006-12-08 02:38:11 -0800512 if (pdev != NULL) { /* PCI */
Jiri Slaby59628012006-12-08 02:38:16 -0800513 pci_release_region(pdev, 2);
514 pci_release_region(pdev, 3);
Jiri Slaby037ad482006-12-08 02:38:11 -0800515 pci_dev_put(pdev);
516 } else {
Jiri Slaby55b307d2006-12-08 02:38:14 -0800517 release_region(mxser_boards[i].ports[0].ioaddr, 8 * mxser_boards[i].nports);
518 release_region(mxser_boards[i].vector, 1);
Jiri Slaby037ad482006-12-08 02:38:11 -0800519 }
520 }
521 }
Jiri Slaby7f589922006-12-08 02:38:20 -0800522 pr_debug("Done.\n");
Jiri Slaby037ad482006-12-08 02:38:11 -0800523}
524
Jiri Slaby55b307d2006-12-08 02:38:14 -0800525static void process_txrx_fifo(struct mxser_port *info)
Jiri Slaby037ad482006-12-08 02:38:11 -0800526{
527 int i;
528
529 if ((info->type == PORT_16450) || (info->type == PORT_8250)) {
530 info->rx_trigger = 1;
531 info->rx_high_water = 1;
532 info->rx_low_water = 1;
533 info->xmit_fifo_size = 1;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800534 } else
535 for (i = 0; i < UART_INFO_NUM; i++)
536 if (info->board->chip_flag == Gpci_uart_info[i].type) {
Jiri Slaby037ad482006-12-08 02:38:11 -0800537 info->rx_trigger = Gpci_uart_info[i].rx_trigger;
538 info->rx_low_water = Gpci_uart_info[i].rx_low_water;
539 info->rx_high_water = Gpci_uart_info[i].rx_high_water;
540 info->xmit_fifo_size = Gpci_uart_info[i].xmit_fifo_size;
541 break;
542 }
Jiri Slaby037ad482006-12-08 02:38:11 -0800543}
544
Jiri Slaby943f2952006-12-08 02:38:15 -0800545static int __devinit mxser_initbrd(struct mxser_board *brd)
Jiri Slaby037ad482006-12-08 02:38:11 -0800546{
Jiri Slaby55b307d2006-12-08 02:38:14 -0800547 struct mxser_port *info;
548 unsigned int i;
Jiri Slaby037ad482006-12-08 02:38:11 -0800549 int retval;
Jiri Slaby037ad482006-12-08 02:38:11 -0800550
Jiri Slaby7f589922006-12-08 02:38:20 -0800551 printk(KERN_INFO "max. baud rate = %d bps.\n", brd->ports[0].max_baud);
Jiri Slaby037ad482006-12-08 02:38:11 -0800552
Jiri Slaby55b307d2006-12-08 02:38:14 -0800553 for (i = 0; i < brd->nports; i++) {
554 info = &brd->ports[i];
555 info->board = brd;
Jiri Slaby037ad482006-12-08 02:38:11 -0800556 info->stop_rx = 0;
557 info->ldisc_stop_rx = 0;
558
Jiri Slaby037ad482006-12-08 02:38:11 -0800559 /* Enhance mode enabled here */
Jiri Slaby55b307d2006-12-08 02:38:14 -0800560 if (brd->chip_flag != MOXA_OTHER_UART)
561 ENABLE_MOXA_MUST_ENCHANCE_MODE(info->ioaddr);
Jiri Slaby037ad482006-12-08 02:38:11 -0800562
563 info->flags = ASYNC_SHARE_IRQ;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800564 info->type = brd->uart_type;
Jiri Slaby037ad482006-12-08 02:38:11 -0800565
566 process_txrx_fifo(info);
567
Jiri Slaby55b307d2006-12-08 02:38:14 -0800568 info->custom_divisor = info->baud_base * 16;
Jiri Slaby037ad482006-12-08 02:38:11 -0800569 info->close_delay = 5 * HZ / 10;
570 info->closing_wait = 30 * HZ;
571 INIT_WORK(&info->tqueue, mxser_do_softint, info);
572 info->normal_termios = mxvar_sdriver->init_termios;
573 init_waitqueue_head(&info->open_wait);
574 init_waitqueue_head(&info->close_wait);
575 init_waitqueue_head(&info->delta_msr_wait);
576 memset(&info->mon_data, 0, sizeof(struct mxser_mon));
577 info->err_shadow = 0;
578 spin_lock_init(&info->slock);
Jiri Slaby55b307d2006-12-08 02:38:14 -0800579
580 /* before set INT ISR, disable all int */
581 outb(inb(info->ioaddr + UART_IER) & 0xf0,
582 info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -0800583 }
584 /*
585 * Allocate the IRQ if necessary
586 */
587
Jiri Slaby55b307d2006-12-08 02:38:14 -0800588 retval = request_irq(brd->irq, mxser_interrupt,
589 (brd->ports[0].flags & ASYNC_SHARE_IRQ) ? IRQF_SHARED :
590 IRQF_DISABLED, "mxser", brd);
Jiri Slaby037ad482006-12-08 02:38:11 -0800591 if (retval) {
Jiri Slaby55b307d2006-12-08 02:38:14 -0800592 printk(KERN_ERR "Board %s: Request irq failed, IRQ (%d) may "
593 "conflict with another device.\n",
594 mxser_brdname[brd->board_type - 1], brd->irq);
Jiri Slaby037ad482006-12-08 02:38:11 -0800595 return retval;
596 }
597 return 0;
598}
599
Jiri Slaby943f2952006-12-08 02:38:15 -0800600static int __init mxser_get_PCI_conf(int board_type, struct mxser_board *brd,
Jiri Slaby55b307d2006-12-08 02:38:14 -0800601 struct pci_dev *pdev)
Jiri Slaby037ad482006-12-08 02:38:11 -0800602{
Jiri Slaby55b307d2006-12-08 02:38:14 -0800603 unsigned int i, j;
Jiri Slaby3306ce32006-12-08 02:38:13 -0800604 unsigned long ioaddress;
Jiri Slaby7a7a5c32006-12-08 02:38:17 -0800605 int retval;
Jiri Slaby037ad482006-12-08 02:38:11 -0800606
607 /* io address */
Jiri Slaby55b307d2006-12-08 02:38:14 -0800608 brd->board_type = board_type;
609 brd->nports = mxser_numports[board_type - 1];
Jiri Slaby037ad482006-12-08 02:38:11 -0800610 ioaddress = pci_resource_start(pdev, 2);
Jiri Slaby7a7a5c32006-12-08 02:38:17 -0800611 retval = pci_request_region(pdev, 2, "mxser(IO)");
612 if (retval)
613 goto err;
Jiri Slaby037ad482006-12-08 02:38:11 -0800614
Jiri Slaby55b307d2006-12-08 02:38:14 -0800615 for (i = 0; i < brd->nports; i++)
616 brd->ports[i].ioaddr = ioaddress + 8 * i;
Jiri Slaby037ad482006-12-08 02:38:11 -0800617
618 /* vector */
619 ioaddress = pci_resource_start(pdev, 3);
Jiri Slaby7a7a5c32006-12-08 02:38:17 -0800620 retval = pci_request_region(pdev, 3, "mxser(vector)");
621 if (retval)
622 goto err_relio;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800623 brd->vector = ioaddress;
Jiri Slaby037ad482006-12-08 02:38:11 -0800624
625 /* irq */
Jiri Slaby55b307d2006-12-08 02:38:14 -0800626 brd->irq = pdev->irq;
Jiri Slaby037ad482006-12-08 02:38:11 -0800627
Jiri Slaby55b307d2006-12-08 02:38:14 -0800628 brd->chip_flag = CheckIsMoxaMust(brd->ports[0].ioaddr);
629 brd->uart_type = PORT_16550A;
630 brd->vector_mask = 0;
Jiri Slaby037ad482006-12-08 02:38:11 -0800631
Jiri Slaby55b307d2006-12-08 02:38:14 -0800632 for (i = 0; i < brd->nports; i++) {
Jiri Slaby037ad482006-12-08 02:38:11 -0800633 for (j = 0; j < UART_INFO_NUM; j++) {
Jiri Slaby55b307d2006-12-08 02:38:14 -0800634 if (Gpci_uart_info[j].type == brd->chip_flag) {
635 brd->ports[i].max_baud =
636 Gpci_uart_info[j].max_baud;
Jiri Slaby037ad482006-12-08 02:38:11 -0800637
638 /* exception....CP-102 */
639 if (board_type == MXSER_BOARD_CP102)
Jiri Slaby55b307d2006-12-08 02:38:14 -0800640 brd->ports[i].max_baud = 921600;
Jiri Slaby037ad482006-12-08 02:38:11 -0800641 break;
642 }
643 }
644 }
645
Jiri Slaby55b307d2006-12-08 02:38:14 -0800646 if (brd->chip_flag == MOXA_MUST_MU860_HWID) {
647 for (i = 0; i < brd->nports; i++) {
Jiri Slaby037ad482006-12-08 02:38:11 -0800648 if (i < 4)
Jiri Slaby55b307d2006-12-08 02:38:14 -0800649 brd->ports[i].opmode_ioaddr = ioaddress + 4;
Jiri Slaby037ad482006-12-08 02:38:11 -0800650 else
Jiri Slaby55b307d2006-12-08 02:38:14 -0800651 brd->ports[i].opmode_ioaddr = ioaddress + 0x0c;
Jiri Slaby037ad482006-12-08 02:38:11 -0800652 }
653 outb(0, ioaddress + 4); /* default set to RS232 mode */
654 outb(0, ioaddress + 0x0c); /* default set to RS232 mode */
655 }
656
Jiri Slaby55b307d2006-12-08 02:38:14 -0800657 for (i = 0; i < brd->nports; i++) {
658 brd->vector_mask |= (1 << i);
659 brd->ports[i].baud_base = 921600;
Jiri Slaby037ad482006-12-08 02:38:11 -0800660 }
661 return 0;
Jiri Slaby7a7a5c32006-12-08 02:38:17 -0800662err_relio:
663 pci_release_region(pdev, 2);
664err:
665 return retval;
Jiri Slaby037ad482006-12-08 02:38:11 -0800666}
Jiri Slaby037ad482006-12-08 02:38:11 -0800667
Jiri Slaby943f2952006-12-08 02:38:15 -0800668static int __init mxser_init(void)
Jiri Slaby037ad482006-12-08 02:38:11 -0800669{
Jiri Slaby037ad482006-12-08 02:38:11 -0800670 struct pci_dev *pdev = NULL;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800671 struct mxser_board *brd;
672 unsigned int i, m;
673 int retval, b, n;
Jiri Slaby037ad482006-12-08 02:38:11 -0800674
675 mxvar_sdriver = alloc_tty_driver(MXSER_PORTS + 1);
676 if (!mxvar_sdriver)
677 return -ENOMEM;
678 spin_lock_init(&gm_lock);
679
Jiri Slaby55b307d2006-12-08 02:38:14 -0800680 for (i = 0; i < MXSER_BOARDS; i++)
681 mxser_boards[i].board_type = -1;
Jiri Slaby037ad482006-12-08 02:38:11 -0800682
683 printk(KERN_INFO "MOXA Smartio/Industio family driver version %s\n",
684 MXSER_VERSION);
685
686 /* Initialize the tty_driver structure */
Jiri Slaby037ad482006-12-08 02:38:11 -0800687 mxvar_sdriver->magic = TTY_DRIVER_MAGIC;
688 mxvar_sdriver->name = "ttyM";
689 mxvar_sdriver->major = ttymajor;
690 mxvar_sdriver->minor_start = 0;
691 mxvar_sdriver->num = MXSER_PORTS + 1;
692 mxvar_sdriver->type = TTY_DRIVER_TYPE_SERIAL;
693 mxvar_sdriver->subtype = SERIAL_TYPE_NORMAL;
694 mxvar_sdriver->init_termios = tty_std_termios;
695 mxvar_sdriver->init_termios.c_cflag = B9600|CS8|CREAD|HUPCL|CLOCAL;
696 mxvar_sdriver->flags = TTY_DRIVER_REAL_RAW;
697 tty_set_operations(mxvar_sdriver, &mxser_ops);
698 mxvar_sdriver->ttys = mxvar_tty;
699 mxvar_sdriver->termios = mxvar_termios;
700 mxvar_sdriver->termios_locked = mxvar_termios_locked;
701
702 mxvar_diagflag = 0;
Jiri Slaby037ad482006-12-08 02:38:11 -0800703
704 m = 0;
705 /* Start finding ISA boards here */
706 for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
707 int cap;
708
709 if (!(cap = mxserBoardCAP[b]))
710 continue;
711
Jiri Slaby55b307d2006-12-08 02:38:14 -0800712 brd = &mxser_boards[m];
713 retval = mxser_get_ISA_conf(cap, brd);
Jiri Slaby037ad482006-12-08 02:38:11 -0800714
715 if (retval != 0)
716 printk(KERN_INFO "Found MOXA %s board (CAP=0x%x)\n",
Jiri Slaby55b307d2006-12-08 02:38:14 -0800717 mxser_brdname[brd->board_type - 1], ioaddr[b]);
Jiri Slaby037ad482006-12-08 02:38:11 -0800718
719 if (retval <= 0) {
720 if (retval == MXSER_ERR_IRQ)
721 printk(KERN_ERR "Invalid interrupt number, "
722 "board not configured\n");
723 else if (retval == MXSER_ERR_IRQ_CONFLIT)
724 printk(KERN_ERR "Invalid interrupt number, "
725 "board not configured\n");
726 else if (retval == MXSER_ERR_VECTOR)
727 printk(KERN_ERR "Invalid interrupt vector, "
728 "board not configured\n");
729 else if (retval == MXSER_ERR_IOADDR)
730 printk(KERN_ERR "Invalid I/O address, "
731 "board not configured\n");
732
733 continue;
734 }
735
Jiri Slaby55b307d2006-12-08 02:38:14 -0800736 brd->pdev = NULL;
Jiri Slaby037ad482006-12-08 02:38:11 -0800737
Jiri Slaby037ad482006-12-08 02:38:11 -0800738 /* mxser_initbrd will hook ISR. */
Jiri Slaby55b307d2006-12-08 02:38:14 -0800739 if (mxser_initbrd(brd) < 0)
Jiri Slaby037ad482006-12-08 02:38:11 -0800740 continue;
741
742 m++;
743 }
744
745 /* Start finding ISA boards from module arg */
746 for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
Jiri Slaby3306ce32006-12-08 02:38:13 -0800747 unsigned long cap;
Jiri Slaby037ad482006-12-08 02:38:11 -0800748
749 if (!(cap = ioaddr[b]))
750 continue;
751
Jiri Slaby55b307d2006-12-08 02:38:14 -0800752 brd = &mxser_boards[m];
753 retval = mxser_get_ISA_conf(cap, &mxser_boards[m]);
Jiri Slaby037ad482006-12-08 02:38:11 -0800754
755 if (retval != 0)
756 printk(KERN_INFO "Found MOXA %s board (CAP=0x%x)\n",
Jiri Slaby55b307d2006-12-08 02:38:14 -0800757 mxser_brdname[brd->board_type - 1], ioaddr[b]);
Jiri Slaby037ad482006-12-08 02:38:11 -0800758
759 if (retval <= 0) {
760 if (retval == MXSER_ERR_IRQ)
761 printk(KERN_ERR "Invalid interrupt number, "
762 "board not configured\n");
763 else if (retval == MXSER_ERR_IRQ_CONFLIT)
764 printk(KERN_ERR "Invalid interrupt number, "
765 "board not configured\n");
766 else if (retval == MXSER_ERR_VECTOR)
767 printk(KERN_ERR "Invalid interrupt vector, "
768 "board not configured\n");
769 else if (retval == MXSER_ERR_IOADDR)
770 printk(KERN_ERR "Invalid I/O address, "
771 "board not configured\n");
772
773 continue;
774 }
775
Jiri Slaby55b307d2006-12-08 02:38:14 -0800776 brd->pdev = NULL;
Jiri Slaby037ad482006-12-08 02:38:11 -0800777 /* mxser_initbrd will hook ISR. */
Jiri Slaby55b307d2006-12-08 02:38:14 -0800778 if (mxser_initbrd(brd) < 0)
Jiri Slaby037ad482006-12-08 02:38:11 -0800779 continue;
780
781 m++;
782 }
783
784 /* start finding PCI board here */
Jiri Slaby037ad482006-12-08 02:38:11 -0800785 n = ARRAY_SIZE(mxser_pcibrds) - 1;
Jiri Slaby037ad482006-12-08 02:38:11 -0800786 b = 0;
787 while (b < n) {
788 pdev = pci_get_device(mxser_pcibrds[b].vendor,
789 mxser_pcibrds[b].device, pdev);
790 if (pdev == NULL) {
791 b++;
792 continue;
793 }
Jiri Slaby037ad482006-12-08 02:38:11 -0800794 printk(KERN_INFO "Found MOXA %s board(BusNo=%d,DevNo=%d)\n",
795 mxser_brdname[(int) (mxser_pcibrds[b].driver_data) - 1],
Jiri Slaby55b307d2006-12-08 02:38:14 -0800796 pdev->bus->number, PCI_SLOT(pdev->devfn));
Jiri Slaby037ad482006-12-08 02:38:11 -0800797 if (m >= MXSER_BOARDS)
798 printk(KERN_ERR
799 "Too many Smartio/Industio family boards find "
800 "(maximum %d), board not configured\n",
801 MXSER_BOARDS);
802 else {
803 if (pci_enable_device(pdev)) {
804 printk(KERN_ERR "Moxa SmartI/O PCI enable "
805 "fail !\n");
806 continue;
807 }
Jiri Slaby55b307d2006-12-08 02:38:14 -0800808 brd = &mxser_boards[m];
809 brd->pdev = pdev;
810 retval = mxser_get_PCI_conf(
Jiri Slaby037ad482006-12-08 02:38:11 -0800811 (int)mxser_pcibrds[b].driver_data,
Jiri Slaby55b307d2006-12-08 02:38:14 -0800812 brd, pdev);
Jiri Slaby037ad482006-12-08 02:38:11 -0800813 if (retval < 0) {
814 if (retval == MXSER_ERR_IRQ)
815 printk(KERN_ERR
816 "Invalid interrupt number, "
817 "board not configured\n");
818 else if (retval == MXSER_ERR_IRQ_CONFLIT)
819 printk(KERN_ERR
820 "Invalid interrupt number, "
821 "board not configured\n");
822 else if (retval == MXSER_ERR_VECTOR)
823 printk(KERN_ERR
824 "Invalid interrupt vector, "
825 "board not configured\n");
826 else if (retval == MXSER_ERR_IOADDR)
827 printk(KERN_ERR
828 "Invalid I/O address, "
829 "board not configured\n");
830 continue;
831 }
Jiri Slaby037ad482006-12-08 02:38:11 -0800832 /* mxser_initbrd will hook ISR. */
Jiri Slaby55b307d2006-12-08 02:38:14 -0800833 if (mxser_initbrd(brd) < 0)
Jiri Slaby037ad482006-12-08 02:38:11 -0800834 continue;
835 m++;
836 /* Keep an extra reference if we succeeded. It will
837 be returned at unload time */
838 pci_dev_get(pdev);
839 }
840 }
Jiri Slaby037ad482006-12-08 02:38:11 -0800841
842 retval = tty_register_driver(mxvar_sdriver);
843 if (retval) {
844 printk(KERN_ERR "Couldn't install MOXA Smartio/Industio family"
845 " driver !\n");
846 put_tty_driver(mxvar_sdriver);
847
848 for (i = 0; i < MXSER_BOARDS; i++) {
Jiri Slaby55b307d2006-12-08 02:38:14 -0800849 if (mxser_boards[i].board_type == -1)
Jiri Slaby037ad482006-12-08 02:38:11 -0800850 continue;
851 else {
Jiri Slaby55b307d2006-12-08 02:38:14 -0800852 free_irq(mxser_boards[i].irq, &mxser_boards[i]);
Jiri Slaby037ad482006-12-08 02:38:11 -0800853 /* todo: release io, vector */
854 }
855 }
856 return retval;
857 }
858
859 return 0;
860}
861
862static void mxser_do_softint(void *private_)
863{
Jiri Slaby55b307d2006-12-08 02:38:14 -0800864 struct mxser_port *info = private_;
Jiri Slaby037ad482006-12-08 02:38:11 -0800865 struct tty_struct *tty;
866
867 tty = info->tty;
868
Jiri Slaby3306ce32006-12-08 02:38:13 -0800869 if (test_and_clear_bit(MXSER_EVENT_TXLOW, &info->event))
870 tty_wakeup(tty);
871 if (test_and_clear_bit(MXSER_EVENT_HANGUP, &info->event))
872 tty_hangup(tty);
Jiri Slaby037ad482006-12-08 02:38:11 -0800873}
874
Jiri Slaby55b307d2006-12-08 02:38:14 -0800875static unsigned char mxser_get_msr(int baseaddr, int mode, int port)
Jiri Slaby037ad482006-12-08 02:38:11 -0800876{
877 unsigned char status = 0;
878
879 status = inb(baseaddr + UART_MSR);
880
881 mxser_msr[port] &= 0x0F;
882 mxser_msr[port] |= status;
883 status = mxser_msr[port];
884 if (mode)
885 mxser_msr[port] = 0;
886
887 return status;
888}
889
890/*
891 * This routine is called whenever a serial port is opened. It
892 * enables interrupts for a serial port, linking in its async structure into
893 * the IRQ chain. It also performs the serial-specific
894 * initialization for the tty structure.
895 */
896static int mxser_open(struct tty_struct *tty, struct file *filp)
897{
Jiri Slaby55b307d2006-12-08 02:38:14 -0800898 struct mxser_port *info;
Jiri Slaby037ad482006-12-08 02:38:11 -0800899 int retval, line;
900
901 /* initialize driver_data in case something fails */
902 tty->driver_data = NULL;
903
904 line = tty->index;
905 if (line == MXSER_PORTS)
906 return 0;
907 if (line < 0 || line > MXSER_PORTS)
908 return -ENODEV;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800909 info = &mxser_boards[line / MXSER_PORTS_PER_BOARD].ports[line % MXSER_PORTS_PER_BOARD];
910 if (!info->ioaddr)
Jiri Slaby037ad482006-12-08 02:38:11 -0800911 return -ENODEV;
912
913 tty->driver_data = info;
914 info->tty = tty;
915 /*
916 * Start up serial port
917 */
Jiri Slaby3306ce32006-12-08 02:38:13 -0800918 info->count++;
Jiri Slaby037ad482006-12-08 02:38:11 -0800919 retval = mxser_startup(info);
920 if (retval)
921 return retval;
922
923 retval = mxser_block_til_ready(tty, filp, info);
924 if (retval)
925 return retval;
926
Jiri Slaby037ad482006-12-08 02:38:11 -0800927 if ((info->count == 1) && (info->flags & ASYNC_SPLIT_TERMIOS)) {
928 if (tty->driver->subtype == SERIAL_TYPE_NORMAL)
929 *tty->termios = info->normal_termios;
930 else
931 *tty->termios = info->callout_termios;
932 mxser_change_speed(info, NULL);
933 }
934
Andrew Morton08a4ae42006-12-08 02:38:12 -0800935 info->session = process_session(current);
Jiri Slaby037ad482006-12-08 02:38:11 -0800936 info->pgrp = process_group(current);
937
938 /*
939 status = mxser_get_msr(info->base, 0, info->port);
940 mxser_check_modem_status(info, status);
941 */
942
943/* unmark here for very high baud rate (ex. 921600 bps) used */
944 tty->low_latency = 1;
945 return 0;
946}
947
948/*
949 * This routine is called when the serial port gets closed. First, we
950 * wait for the last remaining data to be sent. Then, we unlink its
951 * async structure from the interrupt chain if necessary, and we free
952 * that IRQ if nothing is left in the chain.
953 */
954static void mxser_close(struct tty_struct *tty, struct file *filp)
955{
Jiri Slaby55b307d2006-12-08 02:38:14 -0800956 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -0800957
958 unsigned long timeout;
959 unsigned long flags;
960 struct tty_ldisc *ld;
961
962 if (tty->index == MXSER_PORTS)
963 return;
964 if (!info)
965 return;
966
967 spin_lock_irqsave(&info->slock, flags);
968
969 if (tty_hung_up_p(filp)) {
970 spin_unlock_irqrestore(&info->slock, flags);
971 return;
972 }
973 if ((tty->count == 1) && (info->count != 1)) {
974 /*
975 * Uh, oh. tty->count is 1, which means that the tty
976 * structure will be freed. Info->count should always
977 * be one in these conditions. If it's greater than
978 * one, we've got real problems, since it means the
979 * serial port won't be shutdown.
980 */
981 printk(KERN_ERR "mxser_close: bad serial port count; "
982 "tty->count is 1, info->count is %d\n", info->count);
983 info->count = 1;
984 }
985 if (--info->count < 0) {
986 printk(KERN_ERR "mxser_close: bad serial port count for "
Jiri Slaby55b307d2006-12-08 02:38:14 -0800987 "ttys%d: %d\n", tty->index, info->count);
Jiri Slaby037ad482006-12-08 02:38:11 -0800988 info->count = 0;
989 }
990 if (info->count) {
991 spin_unlock_irqrestore(&info->slock, flags);
992 return;
993 }
994 info->flags |= ASYNC_CLOSING;
995 spin_unlock_irqrestore(&info->slock, flags);
996 /*
997 * Save the termios structure, since this port may have
998 * separate termios for callout and dialin.
999 */
1000 if (info->flags & ASYNC_NORMAL_ACTIVE)
1001 info->normal_termios = *tty->termios;
1002 /*
1003 * Now we wait for the transmit buffer to clear; and we notify
1004 * the line discipline to only process XON/XOFF characters.
1005 */
1006 tty->closing = 1;
1007 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1008 tty_wait_until_sent(tty, info->closing_wait);
1009 /*
1010 * At this point we stop accepting input. To do this, we
1011 * disable the receive line status interrupts, and tell the
1012 * interrupt driver to stop checking the data ready bit in the
1013 * line status register.
1014 */
1015 info->IER &= ~UART_IER_RLSI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001016 if (info->board->chip_flag)
Jiri Slaby037ad482006-12-08 02:38:11 -08001017 info->IER &= ~MOXA_MUST_RECV_ISR;
1018/* by William
1019 info->read_status_mask &= ~UART_LSR_DR;
1020*/
1021 if (info->flags & ASYNC_INITIALIZED) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08001022 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001023 /*
1024 * Before we drop DTR, make sure the UART transmitter
1025 * has completely drained; this is especially
1026 * important if there is a transmit FIFO!
1027 */
1028 timeout = jiffies + HZ;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001029 while (!(inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001030 schedule_timeout_interruptible(5);
1031 if (time_after(jiffies, timeout))
1032 break;
1033 }
1034 }
1035 mxser_shutdown(info);
1036
1037 if (tty->driver->flush_buffer)
1038 tty->driver->flush_buffer(tty);
1039
1040 ld = tty_ldisc_ref(tty);
1041 if (ld) {
1042 if (ld->flush_buffer)
1043 ld->flush_buffer(tty);
1044 tty_ldisc_deref(ld);
1045 }
1046
1047 tty->closing = 0;
1048 info->event = 0;
1049 info->tty = NULL;
1050 if (info->blocked_open) {
1051 if (info->close_delay)
1052 schedule_timeout_interruptible(info->close_delay);
1053 wake_up_interruptible(&info->open_wait);
1054 }
1055
1056 info->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
1057 wake_up_interruptible(&info->close_wait);
1058
1059}
1060
1061static int mxser_write(struct tty_struct *tty, const unsigned char *buf, int count)
1062{
1063 int c, total = 0;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001064 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001065 unsigned long flags;
1066
1067 if (!info->xmit_buf)
1068 return 0;
1069
1070 while (1) {
1071 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1072 SERIAL_XMIT_SIZE - info->xmit_head));
1073 if (c <= 0)
1074 break;
1075
1076 memcpy(info->xmit_buf + info->xmit_head, buf, c);
1077 spin_lock_irqsave(&info->slock, flags);
1078 info->xmit_head = (info->xmit_head + c) &
1079 (SERIAL_XMIT_SIZE - 1);
1080 info->xmit_cnt += c;
1081 spin_unlock_irqrestore(&info->slock, flags);
1082
1083 buf += c;
1084 count -= c;
1085 total += c;
1086 }
1087
Jiri Slaby3306ce32006-12-08 02:38:13 -08001088 if (info->xmit_cnt && !tty->stopped
1089 /*&& !(info->IER & UART_IER_THRI)*/) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001090 if (!tty->hw_stopped ||
1091 (info->type == PORT_16550A) ||
Jiri Slaby55b307d2006-12-08 02:38:14 -08001092 (info->board->chip_flag)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001093 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby55b307d2006-12-08 02:38:14 -08001094 outb(info->IER & ~UART_IER_THRI, info->ioaddr +
1095 UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001096 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001097 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001098 spin_unlock_irqrestore(&info->slock, flags);
1099 }
1100 }
1101 return total;
1102}
1103
1104static void mxser_put_char(struct tty_struct *tty, unsigned char ch)
1105{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001106 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001107 unsigned long flags;
1108
1109 if (!info->xmit_buf)
1110 return;
1111
1112 if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1)
1113 return;
1114
1115 spin_lock_irqsave(&info->slock, flags);
1116 info->xmit_buf[info->xmit_head++] = ch;
1117 info->xmit_head &= SERIAL_XMIT_SIZE - 1;
1118 info->xmit_cnt++;
1119 spin_unlock_irqrestore(&info->slock, flags);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001120 if (!tty->stopped /*&& !(info->IER & UART_IER_THRI)*/) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001121 if (!tty->hw_stopped ||
1122 (info->type == PORT_16550A) ||
Jiri Slaby55b307d2006-12-08 02:38:14 -08001123 info->board->chip_flag) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001124 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby55b307d2006-12-08 02:38:14 -08001125 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001126 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001127 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001128 spin_unlock_irqrestore(&info->slock, flags);
1129 }
1130 }
1131}
1132
1133
1134static void mxser_flush_chars(struct tty_struct *tty)
1135{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001136 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001137 unsigned long flags;
1138
1139 if (info->xmit_cnt <= 0 ||
1140 tty->stopped ||
1141 !info->xmit_buf ||
1142 (tty->hw_stopped &&
1143 (info->type != PORT_16550A) &&
Jiri Slaby55b307d2006-12-08 02:38:14 -08001144 (!info->board->chip_flag)
Jiri Slaby037ad482006-12-08 02:38:11 -08001145 ))
1146 return;
1147
1148 spin_lock_irqsave(&info->slock, flags);
1149
Jiri Slaby55b307d2006-12-08 02:38:14 -08001150 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001151 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001152 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001153
1154 spin_unlock_irqrestore(&info->slock, flags);
1155}
1156
1157static int mxser_write_room(struct tty_struct *tty)
1158{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001159 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001160 int ret;
1161
1162 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1163 if (ret < 0)
1164 ret = 0;
1165 return ret;
1166}
1167
1168static int mxser_chars_in_buffer(struct tty_struct *tty)
1169{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001170 struct mxser_port *info = tty->driver_data;
Jiri Slaby3306ce32006-12-08 02:38:13 -08001171 int len = info->xmit_cnt;
1172
Jiri Slaby55b307d2006-12-08 02:38:14 -08001173 if (!(inb(info->ioaddr + UART_LSR) & UART_LSR_THRE))
Jiri Slaby3306ce32006-12-08 02:38:13 -08001174 len++;
1175
1176 return len;
Jiri Slaby037ad482006-12-08 02:38:11 -08001177}
1178
1179static void mxser_flush_buffer(struct tty_struct *tty)
1180{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001181 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001182 char fcr;
1183 unsigned long flags;
1184
1185
1186 spin_lock_irqsave(&info->slock, flags);
1187 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1188
1189 /* below added by shinhay */
Jiri Slaby55b307d2006-12-08 02:38:14 -08001190 fcr = inb(info->ioaddr + UART_FCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08001191 outb((fcr | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
Jiri Slaby55b307d2006-12-08 02:38:14 -08001192 info->ioaddr + UART_FCR);
1193 outb(fcr, info->ioaddr + UART_FCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08001194
1195 spin_unlock_irqrestore(&info->slock, flags);
1196 /* above added by shinhay */
1197
1198 wake_up_interruptible(&tty->write_wait);
1199 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) && tty->ldisc.write_wakeup)
1200 (tty->ldisc.write_wakeup) (tty);
1201}
1202
1203static int mxser_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
1204{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001205 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001206 int retval;
1207 struct async_icount cprev, cnow; /* kernel counter temps */
1208 struct serial_icounter_struct __user *p_cuser;
1209 unsigned long templ;
1210 unsigned long flags;
1211 void __user *argp = (void __user *)arg;
1212
1213 if (tty->index == MXSER_PORTS)
1214 return mxser_ioctl_special(cmd, argp);
1215
1216 /* following add by Victor Yu. 01-05-2004 */
1217 if (cmd == MOXA_SET_OP_MODE || cmd == MOXA_GET_OP_MODE) {
Jiri Slaby3306ce32006-12-08 02:38:13 -08001218 int p;
1219 unsigned long opmode;
Jiri Slaby037ad482006-12-08 02:38:11 -08001220 static unsigned char ModeMask[] = { 0xfc, 0xf3, 0xcf, 0x3f };
1221 int shiftbit;
1222 unsigned char val, mask;
1223
Jiri Slaby55b307d2006-12-08 02:38:14 -08001224 p = tty->index % 4;
Jiri Slaby037ad482006-12-08 02:38:11 -08001225 if (cmd == MOXA_SET_OP_MODE) {
1226 if (get_user(opmode, (int __user *) argp))
1227 return -EFAULT;
1228 if (opmode != RS232_MODE &&
1229 opmode != RS485_2WIRE_MODE &&
1230 opmode != RS422_MODE &&
1231 opmode != RS485_4WIRE_MODE)
1232 return -EFAULT;
1233 mask = ModeMask[p];
1234 shiftbit = p * 2;
1235 val = inb(info->opmode_ioaddr);
1236 val &= mask;
1237 val |= (opmode << shiftbit);
1238 outb(val, info->opmode_ioaddr);
1239 } else {
1240 shiftbit = p * 2;
1241 opmode = inb(info->opmode_ioaddr) >> shiftbit;
1242 opmode &= OP_MODE_MASK;
1243 if (copy_to_user(argp, &opmode, sizeof(int)))
1244 return -EFAULT;
1245 }
1246 return 0;
1247 }
1248 /* above add by Victor Yu. 01-05-2004 */
1249
1250 if ((cmd != TIOCGSERIAL) && (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
1251 if (tty->flags & (1 << TTY_IO_ERROR))
1252 return -EIO;
1253 }
1254 switch (cmd) {
1255 case TCSBRK: /* SVID version: non-zero arg --> no break */
1256 retval = tty_check_change(tty);
1257 if (retval)
1258 return retval;
1259 tty_wait_until_sent(tty, 0);
1260 if (!arg)
1261 mxser_send_break(info, HZ / 4); /* 1/4 second */
1262 return 0;
1263 case TCSBRKP: /* support for POSIX tcsendbreak() */
1264 retval = tty_check_change(tty);
1265 if (retval)
1266 return retval;
1267 tty_wait_until_sent(tty, 0);
1268 mxser_send_break(info, arg ? arg * (HZ / 10) : HZ / 4);
1269 return 0;
1270 case TIOCGSOFTCAR:
1271 return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *)argp);
1272 case TIOCSSOFTCAR:
1273 if (get_user(templ, (unsigned long __user *) argp))
1274 return -EFAULT;
1275 arg = templ;
1276 tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) | (arg ? CLOCAL : 0));
1277 return 0;
1278 case TIOCGSERIAL:
1279 return mxser_get_serial_info(info, argp);
1280 case TIOCSSERIAL:
1281 return mxser_set_serial_info(info, argp);
1282 case TIOCSERGETLSR: /* Get line status register */
1283 return mxser_get_lsr_info(info, argp);
1284 /*
1285 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1286 * - mask passed in arg for lines of interest
1287 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1288 * Caller should use TIOCGICOUNT to see which one it was
1289 */
1290 case TIOCMIWAIT: {
1291 DECLARE_WAITQUEUE(wait, current);
1292 int ret;
1293 spin_lock_irqsave(&info->slock, flags);
1294 cprev = info->icount; /* note the counters on entry */
1295 spin_unlock_irqrestore(&info->slock, flags);
1296
1297 add_wait_queue(&info->delta_msr_wait, &wait);
1298 while (1) {
1299 spin_lock_irqsave(&info->slock, flags);
1300 cnow = info->icount; /* atomic copy */
1301 spin_unlock_irqrestore(&info->slock, flags);
1302
1303 set_current_state(TASK_INTERRUPTIBLE);
1304 if (((arg & TIOCM_RNG) &&
1305 (cnow.rng != cprev.rng)) ||
1306 ((arg & TIOCM_DSR) &&
1307 (cnow.dsr != cprev.dsr)) ||
1308 ((arg & TIOCM_CD) &&
1309 (cnow.dcd != cprev.dcd)) ||
1310 ((arg & TIOCM_CTS) &&
1311 (cnow.cts != cprev.cts))) {
1312 ret = 0;
1313 break;
1314 }
1315 /* see if a signal did it */
1316 if (signal_pending(current)) {
1317 ret = -ERESTARTSYS;
1318 break;
1319 }
1320 cprev = cnow;
1321 }
1322 current->state = TASK_RUNNING;
1323 remove_wait_queue(&info->delta_msr_wait, &wait);
1324 break;
1325 }
1326 /* NOTREACHED */
1327 /*
1328 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1329 * Return: write counters to the user passed counter struct
1330 * NB: both 1->0 and 0->1 transitions are counted except for
1331 * RI where only 0->1 is counted.
1332 */
1333 case TIOCGICOUNT:
1334 spin_lock_irqsave(&info->slock, flags);
1335 cnow = info->icount;
1336 spin_unlock_irqrestore(&info->slock, flags);
1337 p_cuser = argp;
1338 /* modified by casper 1/11/2000 */
1339 if (put_user(cnow.frame, &p_cuser->frame))
1340 return -EFAULT;
1341 if (put_user(cnow.brk, &p_cuser->brk))
1342 return -EFAULT;
1343 if (put_user(cnow.overrun, &p_cuser->overrun))
1344 return -EFAULT;
1345 if (put_user(cnow.buf_overrun, &p_cuser->buf_overrun))
1346 return -EFAULT;
1347 if (put_user(cnow.parity, &p_cuser->parity))
1348 return -EFAULT;
1349 if (put_user(cnow.rx, &p_cuser->rx))
1350 return -EFAULT;
1351 if (put_user(cnow.tx, &p_cuser->tx))
1352 return -EFAULT;
1353 put_user(cnow.cts, &p_cuser->cts);
1354 put_user(cnow.dsr, &p_cuser->dsr);
1355 put_user(cnow.rng, &p_cuser->rng);
1356 put_user(cnow.dcd, &p_cuser->dcd);
1357 return 0;
1358 case MOXA_HighSpeedOn:
1359 return put_user(info->baud_base != 115200 ? 1 : 0, (int __user *)argp);
1360 case MOXA_SDS_RSTICOUNTER: {
1361 info->mon_data.rxcnt = 0;
1362 info->mon_data.txcnt = 0;
1363 return 0;
1364 }
1365/* (above) added by James. */
1366 case MOXA_ASPP_SETBAUD:{
1367 long baud;
1368 if (get_user(baud, (long __user *)argp))
1369 return -EFAULT;
1370 mxser_set_baud(info, baud);
1371 return 0;
1372 }
1373 case MOXA_ASPP_GETBAUD:
1374 if (copy_to_user(argp, &info->realbaud, sizeof(long)))
1375 return -EFAULT;
1376
1377 return 0;
1378
1379 case MOXA_ASPP_OQUEUE:{
1380 int len, lsr;
1381
1382 len = mxser_chars_in_buffer(tty);
1383
Jiri Slaby55b307d2006-12-08 02:38:14 -08001384 lsr = inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT;
Jiri Slaby037ad482006-12-08 02:38:11 -08001385
1386 len += (lsr ? 0 : 1);
1387
1388 if (copy_to_user(argp, &len, sizeof(int)))
1389 return -EFAULT;
1390
1391 return 0;
1392 }
1393 case MOXA_ASPP_MON: {
1394 int mcr, status;
1395
1396 /* info->mon_data.ser_param = tty->termios->c_cflag; */
1397
Jiri Slaby55b307d2006-12-08 02:38:14 -08001398 status = mxser_get_msr(info->ioaddr, 1, tty->index);
Jiri Slaby037ad482006-12-08 02:38:11 -08001399 mxser_check_modem_status(info, status);
1400
Jiri Slaby55b307d2006-12-08 02:38:14 -08001401 mcr = inb(info->ioaddr + UART_MCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08001402 if (mcr & MOXA_MUST_MCR_XON_FLAG)
1403 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFHOLD;
1404 else
1405 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFHOLD;
1406
1407 if (mcr & MOXA_MUST_MCR_TX_XON)
1408 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFXENT;
1409 else
1410 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFXENT;
1411
1412 if (info->tty->hw_stopped)
1413 info->mon_data.hold_reason |= NPPI_NOTIFY_CTSHOLD;
1414 else
1415 info->mon_data.hold_reason &= ~NPPI_NOTIFY_CTSHOLD;
1416
1417 if (copy_to_user(argp, &info->mon_data,
1418 sizeof(struct mxser_mon)))
1419 return -EFAULT;
1420
1421 return 0;
1422 }
1423
1424 case MOXA_ASPP_LSTATUS: {
1425 if (copy_to_user(argp, &info->err_shadow,
1426 sizeof(unsigned char)))
1427 return -EFAULT;
1428
1429 info->err_shadow = 0;
1430 return 0;
1431 }
1432 case MOXA_SET_BAUD_METHOD: {
1433 int method;
1434
1435 if (get_user(method, (int __user *)argp))
1436 return -EFAULT;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001437 mxser_set_baud_method[tty->index] = method;
Jiri Slaby037ad482006-12-08 02:38:11 -08001438 if (copy_to_user(argp, &method, sizeof(int)))
1439 return -EFAULT;
1440
1441 return 0;
1442 }
1443 default:
1444 return -ENOIOCTLCMD;
1445 }
1446 return 0;
1447}
1448
1449#ifndef CMSPAR
1450#define CMSPAR 010000000000
1451#endif
1452
1453static int mxser_ioctl_special(unsigned int cmd, void __user *argp)
1454{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001455 struct mxser_port *port;
1456 int result, status;
1457 unsigned int i, j;
Jiri Slaby037ad482006-12-08 02:38:11 -08001458
1459 switch (cmd) {
1460 case MOXA_GET_CONF:
Jiri Slaby55b307d2006-12-08 02:38:14 -08001461/* if (copy_to_user(argp, mxsercfg,
Jiri Slaby037ad482006-12-08 02:38:11 -08001462 sizeof(struct mxser_hwconf) * 4))
1463 return -EFAULT;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001464 return 0;*/
1465 return -ENXIO;
Jiri Slaby037ad482006-12-08 02:38:11 -08001466 case MOXA_GET_MAJOR:
1467 if (copy_to_user(argp, &ttymajor, sizeof(int)))
1468 return -EFAULT;
1469 return 0;
1470
1471 case MOXA_GET_CUMAJOR:
1472 if (copy_to_user(argp, &calloutmajor, sizeof(int)))
1473 return -EFAULT;
1474 return 0;
1475
1476 case MOXA_CHKPORTENABLE:
1477 result = 0;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001478
1479 for (i = 0; i < MXSER_BOARDS; i++)
1480 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++)
1481 if (mxser_boards[i].ports[j].ioaddr)
1482 result |= (1 << i);
1483
Jiri Slaby037ad482006-12-08 02:38:11 -08001484 return put_user(result, (unsigned long __user *)argp);
1485 case MOXA_GETDATACOUNT:
1486 if (copy_to_user(argp, &mxvar_log, sizeof(mxvar_log)))
1487 return -EFAULT;
1488 return 0;
1489 case MOXA_GETMSTATUS:
Jiri Slaby55b307d2006-12-08 02:38:14 -08001490 for (i = 0; i < MXSER_BOARDS; i++)
1491 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
1492 port = &mxser_boards[i].ports[j];
1493
1494 GMStatus[i].ri = 0;
1495 if (!port->ioaddr) {
1496 GMStatus[i].dcd = 0;
1497 GMStatus[i].dsr = 0;
1498 GMStatus[i].cts = 0;
1499 continue;
1500 }
1501
1502 if (!port->tty || !port->tty->termios)
1503 GMStatus[i].cflag =
1504 port->normal_termios.c_cflag;
1505 else
1506 GMStatus[i].cflag =
1507 port->tty->termios->c_cflag;
1508
1509 status = inb(port->ioaddr + UART_MSR);
1510 if (status & 0x80 /*UART_MSR_DCD */ )
1511 GMStatus[i].dcd = 1;
1512 else
1513 GMStatus[i].dcd = 0;
1514
1515 if (status & 0x20 /*UART_MSR_DSR */ )
1516 GMStatus[i].dsr = 1;
1517 else
1518 GMStatus[i].dsr = 0;
1519
1520
1521 if (status & 0x10 /*UART_MSR_CTS */ )
1522 GMStatus[i].cts = 1;
1523 else
1524 GMStatus[i].cts = 0;
Jiri Slaby037ad482006-12-08 02:38:11 -08001525 }
Jiri Slaby037ad482006-12-08 02:38:11 -08001526 if (copy_to_user(argp, GMStatus,
1527 sizeof(struct mxser_mstatus) * MXSER_PORTS))
1528 return -EFAULT;
1529 return 0;
1530 case MOXA_ASPP_MON_EXT: {
Jiri Slaby55b307d2006-12-08 02:38:14 -08001531 int status, p, shiftbit;
1532 unsigned long opmode;
1533 unsigned cflag, iflag;
Jiri Slaby037ad482006-12-08 02:38:11 -08001534
Jiri Slaby55b307d2006-12-08 02:38:14 -08001535 for (i = 0; i < MXSER_BOARDS; i++)
1536 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
1537 port = &mxser_boards[i].ports[j];
1538 if (!port->ioaddr)
Jiri Slaby037ad482006-12-08 02:38:11 -08001539 continue;
1540
Jiri Slaby55b307d2006-12-08 02:38:14 -08001541 status = mxser_get_msr(port->ioaddr, 0, i);
1542/* mxser_check_modem_status(port, status); */
1543
Jiri Slaby037ad482006-12-08 02:38:11 -08001544 if (status & UART_MSR_TERI)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001545 port->icount.rng++;
Jiri Slaby037ad482006-12-08 02:38:11 -08001546 if (status & UART_MSR_DDSR)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001547 port->icount.dsr++;
Jiri Slaby037ad482006-12-08 02:38:11 -08001548 if (status & UART_MSR_DDCD)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001549 port->icount.dcd++;
Jiri Slaby037ad482006-12-08 02:38:11 -08001550 if (status & UART_MSR_DCTS)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001551 port->icount.cts++;
Jiri Slaby037ad482006-12-08 02:38:11 -08001552
Jiri Slaby55b307d2006-12-08 02:38:14 -08001553 port->mon_data.modem_status = status;
1554 mon_data_ext.rx_cnt[i] = port->mon_data.rxcnt;
1555 mon_data_ext.tx_cnt[i] = port->mon_data.txcnt;
1556 mon_data_ext.up_rxcnt[i] =
1557 port->mon_data.up_rxcnt;
1558 mon_data_ext.up_txcnt[i] =
1559 port->mon_data.up_txcnt;
1560 mon_data_ext.modem_status[i] =
1561 port->mon_data.modem_status;
1562 mon_data_ext.baudrate[i] = port->realbaud;
Jiri Slaby037ad482006-12-08 02:38:11 -08001563
Jiri Slaby55b307d2006-12-08 02:38:14 -08001564 if (!port->tty || !port->tty->termios) {
1565 cflag = port->normal_termios.c_cflag;
1566 iflag = port->normal_termios.c_iflag;
Jiri Slaby037ad482006-12-08 02:38:11 -08001567 } else {
Jiri Slaby55b307d2006-12-08 02:38:14 -08001568 cflag = port->tty->termios->c_cflag;
1569 iflag = port->tty->termios->c_iflag;
Jiri Slaby037ad482006-12-08 02:38:11 -08001570 }
1571
1572 mon_data_ext.databits[i] = cflag & CSIZE;
1573
1574 mon_data_ext.stopbits[i] = cflag & CSTOPB;
1575
Jiri Slaby55b307d2006-12-08 02:38:14 -08001576 mon_data_ext.parity[i] =
1577 cflag & (PARENB | PARODD | CMSPAR);
Jiri Slaby037ad482006-12-08 02:38:11 -08001578
1579 mon_data_ext.flowctrl[i] = 0x00;
1580
1581 if (cflag & CRTSCTS)
1582 mon_data_ext.flowctrl[i] |= 0x03;
1583
1584 if (iflag & (IXON | IXOFF))
1585 mon_data_ext.flowctrl[i] |= 0x0C;
1586
Jiri Slaby55b307d2006-12-08 02:38:14 -08001587 if (port->type == PORT_16550A)
Jiri Slaby037ad482006-12-08 02:38:11 -08001588 mon_data_ext.fifo[i] = 1;
1589 else
1590 mon_data_ext.fifo[i] = 0;
1591
1592 p = i % 4;
1593 shiftbit = p * 2;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001594 opmode = inb(port->opmode_ioaddr) >> shiftbit;
Jiri Slaby037ad482006-12-08 02:38:11 -08001595 opmode &= OP_MODE_MASK;
1596
1597 mon_data_ext.iftype[i] = opmode;
1598
1599 }
Jiri Slaby55b307d2006-12-08 02:38:14 -08001600 if (copy_to_user(argp, &mon_data_ext,
1601 sizeof(mon_data_ext)))
Jiri Slaby037ad482006-12-08 02:38:11 -08001602 return -EFAULT;
1603
1604 return 0;
1605
Jiri Slaby55b307d2006-12-08 02:38:14 -08001606 } default:
Jiri Slaby037ad482006-12-08 02:38:11 -08001607 return -ENOIOCTLCMD;
1608 }
1609 return 0;
1610}
1611
1612static void mxser_stoprx(struct tty_struct *tty)
1613{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001614 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001615
1616 info->ldisc_stop_rx = 1;
1617 if (I_IXOFF(tty)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001618 /* following add by Victor Yu. 09-02-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08001619 if (info->board->chip_flag) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001620 info->IER &= ~MOXA_MUST_RECV_ISR;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001621 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001622 } else if (!(info->flags & ASYNC_CLOSING)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001623 info->x_char = STOP_CHAR(tty);
Jiri Slaby55b307d2006-12-08 02:38:14 -08001624 outb(0, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001625 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001626 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001627 }
Jiri Slaby037ad482006-12-08 02:38:11 -08001628 }
1629
1630 if (info->tty->termios->c_cflag & CRTSCTS) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001631 info->MCR &= ~UART_MCR_RTS;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001632 outb(info->MCR, info->ioaddr + UART_MCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08001633 }
1634}
1635
1636static void mxser_startrx(struct tty_struct *tty)
1637{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001638 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001639
1640 info->ldisc_stop_rx = 0;
1641 if (I_IXOFF(tty)) {
1642 if (info->x_char)
1643 info->x_char = 0;
1644 else {
Jiri Slaby037ad482006-12-08 02:38:11 -08001645 /* following add by Victor Yu. 09-02-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08001646 if (info->board->chip_flag) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001647 info->IER |= MOXA_MUST_RECV_ISR;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001648 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001649 } else if (!(info->flags & ASYNC_CLOSING)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001650 info->x_char = START_CHAR(tty);
Jiri Slaby55b307d2006-12-08 02:38:14 -08001651 outb(0, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001652 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001653 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001654 }
Jiri Slaby037ad482006-12-08 02:38:11 -08001655 }
1656 }
1657
1658 if (info->tty->termios->c_cflag & CRTSCTS) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001659 info->MCR |= UART_MCR_RTS;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001660 outb(info->MCR, info->ioaddr + UART_MCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08001661 }
1662}
1663
1664/*
1665 * This routine is called by the upper-layer tty layer to signal that
1666 * incoming characters should be throttled.
1667 */
1668static void mxser_throttle(struct tty_struct *tty)
1669{
Jiri Slaby037ad482006-12-08 02:38:11 -08001670 mxser_stoprx(tty);
Jiri Slaby037ad482006-12-08 02:38:11 -08001671}
1672
1673static void mxser_unthrottle(struct tty_struct *tty)
1674{
Jiri Slaby037ad482006-12-08 02:38:11 -08001675 mxser_startrx(tty);
Jiri Slaby037ad482006-12-08 02:38:11 -08001676}
1677
1678static void mxser_set_termios(struct tty_struct *tty, struct termios *old_termios)
1679{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001680 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001681 unsigned long flags;
1682
1683 if ((tty->termios->c_cflag != old_termios->c_cflag) ||
1684 (RELEVANT_IFLAG(tty->termios->c_iflag) != RELEVANT_IFLAG(old_termios->c_iflag))) {
1685
1686 mxser_change_speed(info, old_termios);
1687
1688 if ((old_termios->c_cflag & CRTSCTS) &&
1689 !(tty->termios->c_cflag & CRTSCTS)) {
1690 tty->hw_stopped = 0;
1691 mxser_start(tty);
1692 }
1693 }
1694
1695/* Handle sw stopped */
1696 if ((old_termios->c_iflag & IXON) &&
1697 !(tty->termios->c_iflag & IXON)) {
1698 tty->stopped = 0;
1699
1700 /* following add by Victor Yu. 09-02-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08001701 if (info->board->chip_flag) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001702 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby55b307d2006-12-08 02:38:14 -08001703 DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
Jiri Slaby037ad482006-12-08 02:38:11 -08001704 spin_unlock_irqrestore(&info->slock, flags);
1705 }
1706 /* above add by Victor Yu. 09-02-2002 */
1707
1708 mxser_start(tty);
1709 }
1710}
1711
1712/*
1713 * mxser_stop() and mxser_start()
1714 *
1715 * This routines are called before setting or resetting tty->stopped.
1716 * They enable or disable transmitter interrupts, as necessary.
1717 */
1718static void mxser_stop(struct tty_struct *tty)
1719{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001720 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001721 unsigned long flags;
1722
1723 spin_lock_irqsave(&info->slock, flags);
1724 if (info->IER & UART_IER_THRI) {
1725 info->IER &= ~UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001726 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001727 }
1728 spin_unlock_irqrestore(&info->slock, flags);
1729}
1730
1731static void mxser_start(struct tty_struct *tty)
1732{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001733 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001734 unsigned long flags;
1735
1736 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001737 if (info->xmit_cnt && info->xmit_buf
1738 /* && !(info->IER & UART_IER_THRI) */) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08001739 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001740 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001741 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001742 }
1743 spin_unlock_irqrestore(&info->slock, flags);
1744}
1745
1746/*
1747 * mxser_wait_until_sent() --- wait until the transmitter is empty
1748 */
1749static void mxser_wait_until_sent(struct tty_struct *tty, int timeout)
1750{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001751 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001752 unsigned long orig_jiffies, char_time;
1753 int lsr;
1754
1755 if (info->type == PORT_UNKNOWN)
1756 return;
1757
1758 if (info->xmit_fifo_size == 0)
1759 return; /* Just in case.... */
1760
1761 orig_jiffies = jiffies;
1762 /*
1763 * Set the check interval to be 1/5 of the estimated time to
1764 * send a single character, and make it at least 1. The check
1765 * interval should also be less than the timeout.
1766 *
1767 * Note: we have to use pretty tight timings here to satisfy
1768 * the NIST-PCTS.
1769 */
1770 char_time = (info->timeout - HZ / 50) / info->xmit_fifo_size;
1771 char_time = char_time / 5;
1772 if (char_time == 0)
1773 char_time = 1;
1774 if (timeout && timeout < char_time)
1775 char_time = timeout;
1776 /*
1777 * If the transmitter hasn't cleared in twice the approximate
1778 * amount of time to send the entire FIFO, it probably won't
1779 * ever clear. This assumes the UART isn't doing flow
1780 * control, which is currently the case. Hence, if it ever
1781 * takes longer than info->timeout, this is probably due to a
1782 * UART bug of some kind. So, we clamp the timeout parameter at
1783 * 2*info->timeout.
1784 */
1785 if (!timeout || timeout > 2 * info->timeout)
1786 timeout = 2 * info->timeout;
1787#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1788 printk(KERN_DEBUG "In rs_wait_until_sent(%d) check=%lu...",
1789 timeout, char_time);
1790 printk("jiff=%lu...", jiffies);
1791#endif
Jiri Slaby55b307d2006-12-08 02:38:14 -08001792 while (!((lsr = inb(info->ioaddr + UART_LSR)) & UART_LSR_TEMT)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001793#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1794 printk("lsr = %d (jiff=%lu)...", lsr, jiffies);
1795#endif
1796 schedule_timeout_interruptible(char_time);
1797 if (signal_pending(current))
1798 break;
1799 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1800 break;
1801 }
1802 set_current_state(TASK_RUNNING);
1803
1804#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1805 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
1806#endif
1807}
1808
1809
1810/*
1811 * This routine is called by tty_hangup() when a hangup is signaled.
1812 */
1813void mxser_hangup(struct tty_struct *tty)
1814{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001815 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001816
1817 mxser_flush_buffer(tty);
1818 mxser_shutdown(info);
1819 info->event = 0;
1820 info->count = 0;
1821 info->flags &= ~ASYNC_NORMAL_ACTIVE;
1822 info->tty = NULL;
1823 wake_up_interruptible(&info->open_wait);
1824}
1825
1826
1827/* added by James 03-12-2004. */
1828/*
1829 * mxser_rs_break() --- routine which turns the break handling on or off
1830 */
1831static void mxser_rs_break(struct tty_struct *tty, int break_state)
1832{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001833 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001834 unsigned long flags;
1835
1836 spin_lock_irqsave(&info->slock, flags);
1837 if (break_state == -1)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001838 outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
1839 info->ioaddr + UART_LCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08001840 else
Jiri Slaby55b307d2006-12-08 02:38:14 -08001841 outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
1842 info->ioaddr + UART_LCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08001843 spin_unlock_irqrestore(&info->slock, flags);
1844}
1845
1846/* (above) added by James. */
1847
1848
1849/*
1850 * This is the serial driver's generic interrupt routine
1851 */
1852static irqreturn_t mxser_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1853{
1854 int status, iir, i;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001855 struct mxser_board *brd = NULL;
1856 struct mxser_port *port;
Jiri Slaby037ad482006-12-08 02:38:11 -08001857 int max, irqbits, bits, msr;
1858 int pass_counter = 0;
Jiri Slaby3306ce32006-12-08 02:38:13 -08001859 unsigned int int_cnt;
Jiri Slaby037ad482006-12-08 02:38:11 -08001860 int handled = IRQ_NONE;
1861
Jiri Slaby037ad482006-12-08 02:38:11 -08001862 /* spin_lock(&gm_lock); */
1863
Jiri Slaby55b307d2006-12-08 02:38:14 -08001864 for (i = 0; i < MXSER_BOARDS; i++)
1865 if (dev_id == &mxser_boards[i]) {
1866 brd = dev_id;
Jiri Slaby037ad482006-12-08 02:38:11 -08001867 break;
1868 }
Jiri Slaby037ad482006-12-08 02:38:11 -08001869
1870 if (i == MXSER_BOARDS)
1871 goto irq_stop;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001872 if (brd == NULL)
Jiri Slaby037ad482006-12-08 02:38:11 -08001873 goto irq_stop;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001874 max = mxser_numports[brd->board_type - 1];
Jiri Slaby037ad482006-12-08 02:38:11 -08001875 while (1) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08001876 irqbits = inb(brd->vector) & brd->vector_mask;
1877 if (irqbits == brd->vector_mask)
Jiri Slaby037ad482006-12-08 02:38:11 -08001878 break;
1879
1880 handled = IRQ_HANDLED;
1881 for (i = 0, bits = 1; i < max; i++, irqbits |= bits, bits <<= 1) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08001882 if (irqbits == brd->vector_mask)
Jiri Slaby037ad482006-12-08 02:38:11 -08001883 break;
1884 if (bits & irqbits)
1885 continue;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001886 port = &brd->ports[i];
Jiri Slaby037ad482006-12-08 02:38:11 -08001887
Jiri Slaby3306ce32006-12-08 02:38:13 -08001888 int_cnt = 0;
1889 do {
1890 /* following add by Victor Yu. 09-13-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08001891 iir = inb(port->ioaddr + UART_IIR);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001892 if (iir & UART_IIR_NO_INT)
1893 break;
1894 iir &= MOXA_MUST_IIR_MASK;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001895 if (!port->tty) {
1896 status = inb(port->ioaddr + UART_LSR);
1897 outb(0x27, port->ioaddr + UART_FCR);
1898 inb(port->ioaddr + UART_MSR);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001899 break;
Jiri Slaby037ad482006-12-08 02:38:11 -08001900 }
Jiri Slaby037ad482006-12-08 02:38:11 -08001901 /* above add by Victor Yu. 09-13-2002 */
1902
Jiri Slaby3306ce32006-12-08 02:38:13 -08001903 /* following add by Victor Yu. 09-02-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08001904 status = inb(port->ioaddr + UART_LSR);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001905
1906 if (status & UART_LSR_PE)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001907 port->err_shadow |= NPPI_NOTIFY_PARITY;
Jiri Slaby3306ce32006-12-08 02:38:13 -08001908 if (status & UART_LSR_FE)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001909 port->err_shadow |= NPPI_NOTIFY_FRAMING;
Jiri Slaby3306ce32006-12-08 02:38:13 -08001910 if (status & UART_LSR_OE)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001911 port->err_shadow |=
Jiri Slaby3306ce32006-12-08 02:38:13 -08001912 NPPI_NOTIFY_HW_OVERRUN;
1913 if (status & UART_LSR_BI)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001914 port->err_shadow |= NPPI_NOTIFY_BREAK;
Jiri Slaby3306ce32006-12-08 02:38:13 -08001915
Jiri Slaby55b307d2006-12-08 02:38:14 -08001916 if (port->board->chip_flag) {
Jiri Slaby3306ce32006-12-08 02:38:13 -08001917 /*
1918 if ( (status & 0x02) && !(status & 0x01) ) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08001919 outb(port->ioaddr+UART_FCR, 0x23);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001920 continue;
1921 }
1922 */
1923 if (iir == MOXA_MUST_IIR_GDA ||
1924 iir == MOXA_MUST_IIR_RDA ||
1925 iir == MOXA_MUST_IIR_RTO ||
1926 iir == MOXA_MUST_IIR_LSR)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001927 mxser_receive_chars(port,
Jiri Slaby3306ce32006-12-08 02:38:13 -08001928 &status);
1929
1930 } else {
1931 /* above add by Victor Yu. 09-02-2002 */
1932
Jiri Slaby55b307d2006-12-08 02:38:14 -08001933 status &= port->read_status_mask;
Jiri Slaby3306ce32006-12-08 02:38:13 -08001934 if (status & UART_LSR_DR)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001935 mxser_receive_chars(port,
Jiri Slaby3306ce32006-12-08 02:38:13 -08001936 &status);
Jiri Slaby037ad482006-12-08 02:38:11 -08001937 }
Jiri Slaby55b307d2006-12-08 02:38:14 -08001938 msr = inb(port->ioaddr + UART_MSR);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001939 if (msr & UART_MSR_ANY_DELTA)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001940 mxser_check_modem_status(port, msr);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001941
1942 /* following add by Victor Yu. 09-13-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08001943 if (port->board->chip_flag) {
Jiri Slaby3306ce32006-12-08 02:38:13 -08001944 if (iir == 0x02 && (status &
1945 UART_LSR_THRE))
Jiri Slaby55b307d2006-12-08 02:38:14 -08001946 mxser_transmit_chars(port);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001947 } else {
1948 /* above add by Victor Yu. 09-13-2002 */
1949
1950 if (status & UART_LSR_THRE)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001951 mxser_transmit_chars(port);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001952 }
Jiri Slaby3306ce32006-12-08 02:38:13 -08001953 } while (int_cnt++ < MXSER_ISR_PASS_LIMIT);
Jiri Slaby037ad482006-12-08 02:38:11 -08001954 }
Jiri Slaby3306ce32006-12-08 02:38:13 -08001955 if (pass_counter++ > MXSER_ISR_PASS_LIMIT)
Jiri Slaby037ad482006-12-08 02:38:11 -08001956 break; /* Prevent infinite loops */
Jiri Slaby037ad482006-12-08 02:38:11 -08001957 }
1958
1959 irq_stop:
1960 /* spin_unlock(&gm_lock); */
1961 return handled;
1962}
1963
Jiri Slaby55b307d2006-12-08 02:38:14 -08001964static void mxser_receive_chars(struct mxser_port *port, int *status)
Jiri Slaby037ad482006-12-08 02:38:11 -08001965{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001966 struct tty_struct *tty = port->tty;
Jiri Slaby037ad482006-12-08 02:38:11 -08001967 unsigned char ch, gdl;
1968 int ignored = 0;
1969 int cnt = 0;
1970 int recv_room;
1971 int max = 256;
1972 unsigned long flags;
1973
Jiri Slaby55b307d2006-12-08 02:38:14 -08001974 spin_lock_irqsave(&port->slock, flags);
Jiri Slaby037ad482006-12-08 02:38:11 -08001975
1976 recv_room = tty->receive_room;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001977 if ((recv_room == 0) && (!port->ldisc_stop_rx)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001978 /* mxser_throttle(tty); */
1979 mxser_stoprx(tty);
1980 /* return; */
1981 }
1982
1983 /* following add by Victor Yu. 09-02-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08001984 if (port->board->chip_flag != MOXA_OTHER_UART) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001985
Jiri Slaby3306ce32006-12-08 02:38:13 -08001986 if (*status & UART_LSR_SPECIAL)
Jiri Slaby037ad482006-12-08 02:38:11 -08001987 goto intr_old;
Jiri Slaby037ad482006-12-08 02:38:11 -08001988 /* following add by Victor Yu. 02-11-2004 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08001989 if (port->board->chip_flag == MOXA_MUST_MU860_HWID &&
Jiri Slaby037ad482006-12-08 02:38:11 -08001990 (*status & MOXA_MUST_LSR_RERR))
1991 goto intr_old;
1992 /* above add by Victor Yu. 02-14-2004 */
1993 if (*status & MOXA_MUST_LSR_RERR)
1994 goto intr_old;
1995
Jiri Slaby55b307d2006-12-08 02:38:14 -08001996 gdl = inb(port->ioaddr + MOXA_MUST_GDL_REGISTER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001997
1998 /* add by Victor Yu. 02-11-2004 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08001999 if (port->board->chip_flag == MOXA_MUST_MU150_HWID)
Jiri Slaby037ad482006-12-08 02:38:11 -08002000 gdl &= MOXA_MUST_GDL_MASK;
2001 if (gdl >= recv_room) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002002 if (!port->ldisc_stop_rx) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002003 /* mxser_throttle(tty); */
2004 mxser_stoprx(tty);
2005 }
2006 /* return; */
2007 }
2008 while (gdl--) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002009 ch = inb(port->ioaddr + UART_RX);
Jiri Slaby037ad482006-12-08 02:38:11 -08002010 tty_insert_flip_char(tty, ch, 0);
2011 cnt++;
Jiri Slaby037ad482006-12-08 02:38:11 -08002012 }
2013 goto end_intr;
2014 }
2015 intr_old:
2016 /* above add by Victor Yu. 09-02-2002 */
2017
2018 do {
2019 if (max-- < 0)
2020 break;
Jiri Slaby037ad482006-12-08 02:38:11 -08002021
Jiri Slaby55b307d2006-12-08 02:38:14 -08002022 ch = inb(port->ioaddr + UART_RX);
Jiri Slaby037ad482006-12-08 02:38:11 -08002023 /* following add by Victor Yu. 09-02-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002024 if (port->board->chip_flag && (*status & UART_LSR_OE)
Jiri Slaby3306ce32006-12-08 02:38:13 -08002025 /*&& !(*status&UART_LSR_DR) */)
Jiri Slaby55b307d2006-12-08 02:38:14 -08002026 outb(0x23, port->ioaddr + UART_FCR);
2027 *status &= port->read_status_mask;
Jiri Slaby037ad482006-12-08 02:38:11 -08002028 /* above add by Victor Yu. 09-02-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002029 if (*status & port->ignore_status_mask) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002030 if (++ignored > 100)
2031 break;
2032 } else {
2033 char flag = 0;
2034 if (*status & UART_LSR_SPECIAL) {
2035 if (*status & UART_LSR_BI) {
2036 flag = TTY_BREAK;
2037/* added by casper 1/11/2000 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002038 port->icount.brk++;
Jiri Slaby3306ce32006-12-08 02:38:13 -08002039
Jiri Slaby55b307d2006-12-08 02:38:14 -08002040 if (port->flags & ASYNC_SAK)
Jiri Slaby037ad482006-12-08 02:38:11 -08002041 do_SAK(tty);
2042 } else if (*status & UART_LSR_PE) {
2043 flag = TTY_PARITY;
2044/* added by casper 1/11/2000 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002045 port->icount.parity++;
Jiri Slaby037ad482006-12-08 02:38:11 -08002046 } else if (*status & UART_LSR_FE) {
2047 flag = TTY_FRAME;
2048/* added by casper 1/11/2000 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002049 port->icount.frame++;
Jiri Slaby037ad482006-12-08 02:38:11 -08002050 } else if (*status & UART_LSR_OE) {
2051 flag = TTY_OVERRUN;
2052/* added by casper 1/11/2000 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002053 port->icount.overrun++;
Jiri Slaby3306ce32006-12-08 02:38:13 -08002054 } else
2055 flags = TTY_BREAK;
2056 } else
2057 flags = 0;
Jiri Slaby037ad482006-12-08 02:38:11 -08002058 tty_insert_flip_char(tty, ch, flag);
2059 cnt++;
2060 if (cnt >= recv_room) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002061 if (!port->ldisc_stop_rx) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002062 /* mxser_throttle(tty); */
2063 mxser_stoprx(tty);
2064 }
2065 break;
2066 }
2067
2068 }
2069
2070 /* following add by Victor Yu. 09-02-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002071 if (port->board->chip_flag)
Jiri Slaby037ad482006-12-08 02:38:11 -08002072 break;
Jiri Slaby037ad482006-12-08 02:38:11 -08002073
2074 /* mask by Victor Yu. 09-02-2002
Jiri Slaby55b307d2006-12-08 02:38:14 -08002075 *status = inb(port->ioaddr + UART_LSR) & port->read_status_mask;
Jiri Slaby037ad482006-12-08 02:38:11 -08002076 */
2077 /* following add by Victor Yu. 09-02-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002078 *status = inb(port->ioaddr + UART_LSR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002079 /* above add by Victor Yu. 09-02-2002 */
2080 } while (*status & UART_LSR_DR);
2081
2082end_intr: /* add by Victor Yu. 09-02-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002083 mxvar_log.rxcnt[port->tty->index] += cnt;
2084 port->mon_data.rxcnt += cnt;
2085 port->mon_data.up_rxcnt += cnt;
2086 spin_unlock_irqrestore(&port->slock, flags);
Jiri Slaby037ad482006-12-08 02:38:11 -08002087
2088 tty_flip_buffer_push(tty);
2089}
2090
Jiri Slaby55b307d2006-12-08 02:38:14 -08002091static void mxser_transmit_chars(struct mxser_port *port)
Jiri Slaby037ad482006-12-08 02:38:11 -08002092{
2093 int count, cnt;
2094 unsigned long flags;
2095
Jiri Slaby55b307d2006-12-08 02:38:14 -08002096 spin_lock_irqsave(&port->slock, flags);
Jiri Slaby037ad482006-12-08 02:38:11 -08002097
Jiri Slaby55b307d2006-12-08 02:38:14 -08002098 if (port->x_char) {
2099 outb(port->x_char, port->ioaddr + UART_TX);
2100 port->x_char = 0;
2101 mxvar_log.txcnt[port->tty->index]++;
2102 port->mon_data.txcnt++;
2103 port->mon_data.up_txcnt++;
Jiri Slaby037ad482006-12-08 02:38:11 -08002104
2105/* added by casper 1/11/2000 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002106 port->icount.tx++;
Jiri Slaby3306ce32006-12-08 02:38:13 -08002107 goto unlock;
Jiri Slaby037ad482006-12-08 02:38:11 -08002108 }
2109
Jiri Slaby55b307d2006-12-08 02:38:14 -08002110 if (port->xmit_buf == 0)
Jiri Slaby3306ce32006-12-08 02:38:13 -08002111 goto unlock;
Jiri Slaby037ad482006-12-08 02:38:11 -08002112
Jiri Slaby55b307d2006-12-08 02:38:14 -08002113 if (port->xmit_cnt == 0) {
2114 if (port->xmit_cnt < WAKEUP_CHARS) { /* XXX what's this for?? */
2115 set_bit(MXSER_EVENT_TXLOW, &port->event);
2116 schedule_work(&port->tqueue);
Jiri Slaby3306ce32006-12-08 02:38:13 -08002117 }
2118 goto unlock;
2119 }
Jiri Slaby55b307d2006-12-08 02:38:14 -08002120 if (port->tty->stopped || (port->tty->hw_stopped &&
2121 (port->type != PORT_16550A) &&
2122 (!port->board->chip_flag))) {
2123 port->IER &= ~UART_IER_THRI;
2124 outb(port->IER, port->ioaddr + UART_IER);
Jiri Slaby3306ce32006-12-08 02:38:13 -08002125 goto unlock;
Jiri Slaby037ad482006-12-08 02:38:11 -08002126 }
2127
Jiri Slaby55b307d2006-12-08 02:38:14 -08002128 cnt = port->xmit_cnt;
2129 count = port->xmit_fifo_size;
Jiri Slaby037ad482006-12-08 02:38:11 -08002130 do {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002131 outb(port->xmit_buf[port->xmit_tail++],
2132 port->ioaddr + UART_TX);
2133 port->xmit_tail = port->xmit_tail & (SERIAL_XMIT_SIZE - 1);
2134 if (--port->xmit_cnt <= 0)
Jiri Slaby037ad482006-12-08 02:38:11 -08002135 break;
2136 } while (--count > 0);
Jiri Slaby55b307d2006-12-08 02:38:14 -08002137 mxvar_log.txcnt[port->tty->index] += (cnt - port->xmit_cnt);
Jiri Slaby037ad482006-12-08 02:38:11 -08002138
2139/* added by James 03-12-2004. */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002140 port->mon_data.txcnt += (cnt - port->xmit_cnt);
2141 port->mon_data.up_txcnt += (cnt - port->xmit_cnt);
Jiri Slaby037ad482006-12-08 02:38:11 -08002142
2143/* added by casper 1/11/2000 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002144 port->icount.tx += (cnt - port->xmit_cnt);
Jiri Slaby037ad482006-12-08 02:38:11 -08002145
Jiri Slaby55b307d2006-12-08 02:38:14 -08002146 if (port->xmit_cnt < WAKEUP_CHARS) {
2147 set_bit(MXSER_EVENT_TXLOW, &port->event);
2148 schedule_work(&port->tqueue);
Jiri Slaby037ad482006-12-08 02:38:11 -08002149 }
Jiri Slaby55b307d2006-12-08 02:38:14 -08002150 if (port->xmit_cnt <= 0) {
2151 port->IER &= ~UART_IER_THRI;
2152 outb(port->IER, port->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08002153 }
Jiri Slaby3306ce32006-12-08 02:38:13 -08002154unlock:
Jiri Slaby55b307d2006-12-08 02:38:14 -08002155 spin_unlock_irqrestore(&port->slock, flags);
Jiri Slaby037ad482006-12-08 02:38:11 -08002156}
2157
Jiri Slaby55b307d2006-12-08 02:38:14 -08002158static void mxser_check_modem_status(struct mxser_port *port, int status)
Jiri Slaby037ad482006-12-08 02:38:11 -08002159{
2160 /* update input line counters */
2161 if (status & UART_MSR_TERI)
Jiri Slaby55b307d2006-12-08 02:38:14 -08002162 port->icount.rng++;
Jiri Slaby037ad482006-12-08 02:38:11 -08002163 if (status & UART_MSR_DDSR)
Jiri Slaby55b307d2006-12-08 02:38:14 -08002164 port->icount.dsr++;
Jiri Slaby037ad482006-12-08 02:38:11 -08002165 if (status & UART_MSR_DDCD)
Jiri Slaby55b307d2006-12-08 02:38:14 -08002166 port->icount.dcd++;
Jiri Slaby037ad482006-12-08 02:38:11 -08002167 if (status & UART_MSR_DCTS)
Jiri Slaby55b307d2006-12-08 02:38:14 -08002168 port->icount.cts++;
2169 port->mon_data.modem_status = status;
2170 wake_up_interruptible(&port->delta_msr_wait);
Jiri Slaby037ad482006-12-08 02:38:11 -08002171
Jiri Slaby55b307d2006-12-08 02:38:14 -08002172 if ((port->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002173 if (status & UART_MSR_DCD)
Jiri Slaby55b307d2006-12-08 02:38:14 -08002174 wake_up_interruptible(&port->open_wait);
2175 schedule_work(&port->tqueue);
Jiri Slaby037ad482006-12-08 02:38:11 -08002176 }
2177
Jiri Slaby55b307d2006-12-08 02:38:14 -08002178 if (port->flags & ASYNC_CTS_FLOW) {
2179 if (port->tty->hw_stopped) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002180 if (status & UART_MSR_CTS) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002181 port->tty->hw_stopped = 0;
Jiri Slaby037ad482006-12-08 02:38:11 -08002182
Jiri Slaby55b307d2006-12-08 02:38:14 -08002183 if ((port->type != PORT_16550A) &&
2184 (!port->board->chip_flag)) {
2185 outb(port->IER & ~UART_IER_THRI,
2186 port->ioaddr + UART_IER);
2187 port->IER |= UART_IER_THRI;
2188 outb(port->IER, port->ioaddr +
2189 UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08002190 }
Jiri Slaby55b307d2006-12-08 02:38:14 -08002191 set_bit(MXSER_EVENT_TXLOW, &port->event);
2192 schedule_work(&port->tqueue);
Jiri Slaby3306ce32006-12-08 02:38:13 -08002193 }
Jiri Slaby037ad482006-12-08 02:38:11 -08002194 } else {
2195 if (!(status & UART_MSR_CTS)) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002196 port->tty->hw_stopped = 1;
2197 if (port->type != PORT_16550A &&
2198 !port->board->chip_flag) {
2199 port->IER &= ~UART_IER_THRI;
2200 outb(port->IER, port->ioaddr +
2201 UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08002202 }
2203 }
2204 }
2205 }
2206}
2207
Jiri Slaby55b307d2006-12-08 02:38:14 -08002208static int mxser_block_til_ready(struct tty_struct *tty, struct file *filp, struct mxser_port *port)
Jiri Slaby037ad482006-12-08 02:38:11 -08002209{
2210 DECLARE_WAITQUEUE(wait, current);
2211 int retval;
2212 int do_clocal = 0;
2213 unsigned long flags;
2214
2215 /*
2216 * If non-blocking mode is set, or the port is not enabled,
2217 * then make the check up front and then exit.
2218 */
2219 if ((filp->f_flags & O_NONBLOCK) || (tty->flags & (1 << TTY_IO_ERROR))) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002220 port->flags |= ASYNC_NORMAL_ACTIVE;
Jiri Slaby037ad482006-12-08 02:38:11 -08002221 return 0;
2222 }
2223
2224 if (tty->termios->c_cflag & CLOCAL)
2225 do_clocal = 1;
2226
2227 /*
2228 * Block waiting for the carrier detect and the line to become
2229 * free (i.e., not in use by the callout). While we are in
Jiri Slaby55b307d2006-12-08 02:38:14 -08002230 * this loop, port->count is dropped by one, so that
Jiri Slaby037ad482006-12-08 02:38:11 -08002231 * mxser_close() knows when to free things. We restore it upon
2232 * exit, either normal or abnormal.
2233 */
2234 retval = 0;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002235 add_wait_queue(&port->open_wait, &wait);
Jiri Slaby037ad482006-12-08 02:38:11 -08002236
Jiri Slaby55b307d2006-12-08 02:38:14 -08002237 spin_lock_irqsave(&port->slock, flags);
Jiri Slaby037ad482006-12-08 02:38:11 -08002238 if (!tty_hung_up_p(filp))
Jiri Slaby55b307d2006-12-08 02:38:14 -08002239 port->count--;
2240 spin_unlock_irqrestore(&port->slock, flags);
2241 port->blocked_open++;
Jiri Slaby037ad482006-12-08 02:38:11 -08002242 while (1) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002243 spin_lock_irqsave(&port->slock, flags);
2244 outb(inb(port->ioaddr + UART_MCR) |
2245 UART_MCR_DTR | UART_MCR_RTS, port->ioaddr + UART_MCR);
2246 spin_unlock_irqrestore(&port->slock, flags);
Jiri Slaby037ad482006-12-08 02:38:11 -08002247 set_current_state(TASK_INTERRUPTIBLE);
Jiri Slaby55b307d2006-12-08 02:38:14 -08002248 if (tty_hung_up_p(filp) || !(port->flags & ASYNC_INITIALIZED)) {
2249 if (port->flags & ASYNC_HUP_NOTIFY)
Jiri Slaby037ad482006-12-08 02:38:11 -08002250 retval = -EAGAIN;
2251 else
2252 retval = -ERESTARTSYS;
2253 break;
2254 }
Jiri Slaby55b307d2006-12-08 02:38:14 -08002255 if (!(port->flags & ASYNC_CLOSING) &&
Jiri Slaby037ad482006-12-08 02:38:11 -08002256 (do_clocal ||
Jiri Slaby55b307d2006-12-08 02:38:14 -08002257 (inb(port->ioaddr + UART_MSR) & UART_MSR_DCD)))
Jiri Slaby037ad482006-12-08 02:38:11 -08002258 break;
2259 if (signal_pending(current)) {
2260 retval = -ERESTARTSYS;
2261 break;
2262 }
2263 schedule();
2264 }
2265 set_current_state(TASK_RUNNING);
Jiri Slaby55b307d2006-12-08 02:38:14 -08002266 remove_wait_queue(&port->open_wait, &wait);
Jiri Slaby037ad482006-12-08 02:38:11 -08002267 if (!tty_hung_up_p(filp))
Jiri Slaby55b307d2006-12-08 02:38:14 -08002268 port->count++;
2269 port->blocked_open--;
Jiri Slaby037ad482006-12-08 02:38:11 -08002270 if (retval)
2271 return retval;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002272 port->flags |= ASYNC_NORMAL_ACTIVE;
Jiri Slaby037ad482006-12-08 02:38:11 -08002273 return 0;
2274}
2275
Jiri Slaby55b307d2006-12-08 02:38:14 -08002276static int mxser_startup(struct mxser_port *info)
Jiri Slaby037ad482006-12-08 02:38:11 -08002277{
2278 unsigned long page;
2279 unsigned long flags;
2280
2281 page = __get_free_page(GFP_KERNEL);
2282 if (!page)
2283 return -ENOMEM;
2284
2285 spin_lock_irqsave(&info->slock, flags);
2286
2287 if (info->flags & ASYNC_INITIALIZED) {
2288 free_page(page);
2289 spin_unlock_irqrestore(&info->slock, flags);
2290 return 0;
2291 }
2292
Jiri Slaby55b307d2006-12-08 02:38:14 -08002293 if (!info->ioaddr || !info->type) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002294 if (info->tty)
2295 set_bit(TTY_IO_ERROR, &info->tty->flags);
2296 free_page(page);
2297 spin_unlock_irqrestore(&info->slock, flags);
2298 return 0;
2299 }
2300 if (info->xmit_buf)
2301 free_page(page);
2302 else
2303 info->xmit_buf = (unsigned char *) page;
2304
2305 /*
2306 * Clear the FIFO buffers and disable them
2307 * (they will be reenabled in mxser_change_speed())
2308 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002309 if (info->board->chip_flag)
Jiri Slaby037ad482006-12-08 02:38:11 -08002310 outb((UART_FCR_CLEAR_RCVR |
2311 UART_FCR_CLEAR_XMIT |
Jiri Slaby55b307d2006-12-08 02:38:14 -08002312 MOXA_MUST_FCR_GDA_MODE_ENABLE), info->ioaddr + UART_FCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002313 else
2314 outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
Jiri Slaby55b307d2006-12-08 02:38:14 -08002315 info->ioaddr + UART_FCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002316
2317 /*
2318 * At this point there's no way the LSR could still be 0xFF;
2319 * if it is, then bail out, because there's likely no UART
2320 * here.
2321 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002322 if (inb(info->ioaddr + UART_LSR) == 0xff) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002323 spin_unlock_irqrestore(&info->slock, flags);
2324 if (capable(CAP_SYS_ADMIN)) {
2325 if (info->tty)
2326 set_bit(TTY_IO_ERROR, &info->tty->flags);
2327 return 0;
2328 } else
2329 return -ENODEV;
2330 }
2331
2332 /*
2333 * Clear the interrupt registers.
2334 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002335 (void) inb(info->ioaddr + UART_LSR);
2336 (void) inb(info->ioaddr + UART_RX);
2337 (void) inb(info->ioaddr + UART_IIR);
2338 (void) inb(info->ioaddr + UART_MSR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002339
2340 /*
2341 * Now, initialize the UART
2342 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002343 outb(UART_LCR_WLEN8, info->ioaddr + UART_LCR); /* reset DLAB */
Jiri Slaby037ad482006-12-08 02:38:11 -08002344 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002345 outb(info->MCR, info->ioaddr + UART_MCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002346
2347 /*
2348 * Finally, enable interrupts
2349 */
2350 info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
2351 /* info->IER = UART_IER_RLSI | UART_IER_RDI; */
2352
2353 /* following add by Victor Yu. 08-30-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002354 if (info->board->chip_flag)
Jiri Slaby037ad482006-12-08 02:38:11 -08002355 info->IER |= MOXA_MUST_IER_EGDAI;
2356 /* above add by Victor Yu. 08-30-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002357 outb(info->IER, info->ioaddr + UART_IER); /* enable interrupts */
Jiri Slaby037ad482006-12-08 02:38:11 -08002358
2359 /*
2360 * And clear the interrupt registers again for luck.
2361 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002362 (void) inb(info->ioaddr + UART_LSR);
2363 (void) inb(info->ioaddr + UART_RX);
2364 (void) inb(info->ioaddr + UART_IIR);
2365 (void) inb(info->ioaddr + UART_MSR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002366
2367 if (info->tty)
2368 clear_bit(TTY_IO_ERROR, &info->tty->flags);
2369 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
2370
2371 /*
2372 * and set the speed of the serial port
2373 */
2374 spin_unlock_irqrestore(&info->slock, flags);
2375 mxser_change_speed(info, NULL);
2376
2377 info->flags |= ASYNC_INITIALIZED;
2378 return 0;
2379}
2380
2381/*
2382 * This routine will shutdown a serial port; interrupts maybe disabled, and
2383 * DTR is dropped if the hangup on close termio flag is on.
2384 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002385static void mxser_shutdown(struct mxser_port *info)
Jiri Slaby037ad482006-12-08 02:38:11 -08002386{
2387 unsigned long flags;
2388
2389 if (!(info->flags & ASYNC_INITIALIZED))
2390 return;
2391
2392 spin_lock_irqsave(&info->slock, flags);
2393
2394 /*
2395 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
2396 * here so the queue might never be waken up
2397 */
2398 wake_up_interruptible(&info->delta_msr_wait);
2399
2400 /*
2401 * Free the IRQ, if necessary
2402 */
2403 if (info->xmit_buf) {
2404 free_page((unsigned long) info->xmit_buf);
2405 info->xmit_buf = NULL;
2406 }
2407
2408 info->IER = 0;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002409 outb(0x00, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08002410
2411 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
2412 info->MCR &= ~(UART_MCR_DTR | UART_MCR_RTS);
Jiri Slaby55b307d2006-12-08 02:38:14 -08002413 outb(info->MCR, info->ioaddr + UART_MCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002414
2415 /* clear Rx/Tx FIFO's */
2416 /* following add by Victor Yu. 08-30-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002417 if (info->board->chip_flag)
2418 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT |
2419 MOXA_MUST_FCR_GDA_MODE_ENABLE,
2420 info->ioaddr + UART_FCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002421 else
2422 /* above add by Victor Yu. 08-30-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002423 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
2424 info->ioaddr + UART_FCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002425
2426 /* read data port to reset things */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002427 (void) inb(info->ioaddr + UART_RX);
Jiri Slaby037ad482006-12-08 02:38:11 -08002428
2429 if (info->tty)
2430 set_bit(TTY_IO_ERROR, &info->tty->flags);
2431
2432 info->flags &= ~ASYNC_INITIALIZED;
2433
2434 /* following add by Victor Yu. 09-23-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002435 if (info->board->chip_flag)
2436 SET_MOXA_MUST_NO_SOFTWARE_FLOW_CONTROL(info->ioaddr);
Jiri Slaby037ad482006-12-08 02:38:11 -08002437 /* above add by Victor Yu. 09-23-2002 */
2438
2439 spin_unlock_irqrestore(&info->slock, flags);
2440}
2441
2442/*
2443 * This routine is called to set the UART divisor registers to match
2444 * the specified baud rate for a serial port.
2445 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002446static int mxser_change_speed(struct mxser_port *info,
2447 struct termios *old_termios)
Jiri Slaby037ad482006-12-08 02:38:11 -08002448{
2449 unsigned cflag, cval, fcr;
2450 int ret = 0;
2451 unsigned char status;
2452 long baud;
2453 unsigned long flags;
2454
2455 if (!info->tty || !info->tty->termios)
2456 return ret;
2457 cflag = info->tty->termios->c_cflag;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002458 if (!(info->ioaddr))
Jiri Slaby037ad482006-12-08 02:38:11 -08002459 return ret;
2460
2461#ifndef B921600
2462#define B921600 (B460800 +1)
2463#endif
Jiri Slaby55b307d2006-12-08 02:38:14 -08002464 if (mxser_set_baud_method[info->tty->index] == 0) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002465 baud = tty_get_baud_rate(info->tty);
2466 mxser_set_baud(info, baud);
2467 }
2468
2469 /* byte size and parity */
2470 switch (cflag & CSIZE) {
2471 case CS5:
2472 cval = 0x00;
2473 break;
2474 case CS6:
2475 cval = 0x01;
2476 break;
2477 case CS7:
2478 cval = 0x02;
2479 break;
2480 case CS8:
2481 cval = 0x03;
2482 break;
2483 default:
2484 cval = 0x00;
2485 break; /* too keep GCC shut... */
2486 }
2487 if (cflag & CSTOPB)
2488 cval |= 0x04;
2489 if (cflag & PARENB)
2490 cval |= UART_LCR_PARITY;
2491 if (!(cflag & PARODD))
2492 cval |= UART_LCR_EPAR;
2493 if (cflag & CMSPAR)
2494 cval |= UART_LCR_SPAR;
2495
2496 if ((info->type == PORT_8250) || (info->type == PORT_16450)) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002497 if (info->board->chip_flag) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002498 fcr = UART_FCR_ENABLE_FIFO;
2499 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
2500 SET_MOXA_MUST_FIFO_VALUE(info);
2501 } else
2502 fcr = 0;
2503 } else {
2504 fcr = UART_FCR_ENABLE_FIFO;
2505 /* following add by Victor Yu. 08-30-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002506 if (info->board->chip_flag) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002507 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
2508 SET_MOXA_MUST_FIFO_VALUE(info);
2509 } else {
2510 /* above add by Victor Yu. 08-30-2002 */
2511 switch (info->rx_trigger) {
2512 case 1:
2513 fcr |= UART_FCR_TRIGGER_1;
2514 break;
2515 case 4:
2516 fcr |= UART_FCR_TRIGGER_4;
2517 break;
2518 case 8:
2519 fcr |= UART_FCR_TRIGGER_8;
2520 break;
2521 default:
2522 fcr |= UART_FCR_TRIGGER_14;
2523 break;
2524 }
2525 }
2526 }
2527
2528 /* CTS flow control flag and modem status interrupts */
2529 info->IER &= ~UART_IER_MSI;
2530 info->MCR &= ~UART_MCR_AFE;
2531 if (cflag & CRTSCTS) {
2532 info->flags |= ASYNC_CTS_FLOW;
2533 info->IER |= UART_IER_MSI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002534 if ((info->type == PORT_16550A) || (info->board->chip_flag)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002535 info->MCR |= UART_MCR_AFE;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002536/* status = mxser_get_msr(info->ioaddr, 0, info->port); */
Jiri Slaby037ad482006-12-08 02:38:11 -08002537/*
2538 save_flags(flags);
2539 cli();
2540 status = inb(baseaddr + UART_MSR);
2541 restore_flags(flags);
2542*/
2543 /* mxser_check_modem_status(info, status); */
2544 } else {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002545/* status = mxser_get_msr(info->ioaddr, 0, info->port); */
Jiri Slaby037ad482006-12-08 02:38:11 -08002546 /* MX_LOCK(&info->slock); */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002547 status = inb(info->ioaddr + UART_MSR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002548 /* MX_UNLOCK(&info->slock); */
2549 if (info->tty->hw_stopped) {
2550 if (status & UART_MSR_CTS) {
2551 info->tty->hw_stopped = 0;
Jiri Slaby3306ce32006-12-08 02:38:13 -08002552 if (info->type != PORT_16550A &&
Jiri Slaby55b307d2006-12-08 02:38:14 -08002553 !info->board->chip_flag) {
Jiri Slaby3306ce32006-12-08 02:38:13 -08002554 outb(info->IER & ~UART_IER_THRI,
Jiri Slaby55b307d2006-12-08 02:38:14 -08002555 info->ioaddr +
2556 UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08002557 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002558 outb(info->IER, info->ioaddr +
2559 UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08002560 }
2561 set_bit(MXSER_EVENT_TXLOW, &info->event);
2562 schedule_work(&info->tqueue); }
2563 } else {
2564 if (!(status & UART_MSR_CTS)) {
2565 info->tty->hw_stopped = 1;
2566 if ((info->type != PORT_16550A) &&
Jiri Slaby55b307d2006-12-08 02:38:14 -08002567 (!info->board->chip_flag)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002568 info->IER &= ~UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002569 outb(info->IER, info->ioaddr +
2570 UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08002571 }
2572 }
2573 }
2574 }
2575 } else {
2576 info->flags &= ~ASYNC_CTS_FLOW;
2577 }
Jiri Slaby55b307d2006-12-08 02:38:14 -08002578 outb(info->MCR, info->ioaddr + UART_MCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002579 if (cflag & CLOCAL) {
2580 info->flags &= ~ASYNC_CHECK_CD;
2581 } else {
2582 info->flags |= ASYNC_CHECK_CD;
2583 info->IER |= UART_IER_MSI;
2584 }
Jiri Slaby55b307d2006-12-08 02:38:14 -08002585 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08002586
2587 /*
2588 * Set up parity check flag
2589 */
2590 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
2591 if (I_INPCK(info->tty))
2592 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
2593 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
2594 info->read_status_mask |= UART_LSR_BI;
2595
2596 info->ignore_status_mask = 0;
2597
2598 if (I_IGNBRK(info->tty)) {
2599 info->ignore_status_mask |= UART_LSR_BI;
2600 info->read_status_mask |= UART_LSR_BI;
2601 /*
2602 * If we're ignore parity and break indicators, ignore
2603 * overruns too. (For real raw support).
2604 */
2605 if (I_IGNPAR(info->tty)) {
2606 info->ignore_status_mask |=
2607 UART_LSR_OE |
2608 UART_LSR_PE |
2609 UART_LSR_FE;
2610 info->read_status_mask |=
2611 UART_LSR_OE |
2612 UART_LSR_PE |
2613 UART_LSR_FE;
2614 }
2615 }
2616 /* following add by Victor Yu. 09-02-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002617 if (info->board->chip_flag) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002618 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby55b307d2006-12-08 02:38:14 -08002619 SET_MOXA_MUST_XON1_VALUE(info->ioaddr, START_CHAR(info->tty));
2620 SET_MOXA_MUST_XOFF1_VALUE(info->ioaddr, STOP_CHAR(info->tty));
Jiri Slaby037ad482006-12-08 02:38:11 -08002621 if (I_IXON(info->tty)) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002622 ENABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
Jiri Slaby037ad482006-12-08 02:38:11 -08002623 } else {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002624 DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
Jiri Slaby037ad482006-12-08 02:38:11 -08002625 }
2626 if (I_IXOFF(info->tty)) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002627 ENABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
Jiri Slaby037ad482006-12-08 02:38:11 -08002628 } else {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002629 DISABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
Jiri Slaby037ad482006-12-08 02:38:11 -08002630 }
2631 /*
2632 if ( I_IXANY(info->tty) ) {
2633 info->MCR |= MOXA_MUST_MCR_XON_ANY;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002634 ENABLE_MOXA_MUST_XON_ANY_FLOW_CONTROL(info->ioaddr);
Jiri Slaby037ad482006-12-08 02:38:11 -08002635 } else {
2636 info->MCR &= ~MOXA_MUST_MCR_XON_ANY;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002637 DISABLE_MOXA_MUST_XON_ANY_FLOW_CONTROL(info->ioaddr);
Jiri Slaby037ad482006-12-08 02:38:11 -08002638 }
2639 */
2640 spin_unlock_irqrestore(&info->slock, flags);
2641 }
2642 /* above add by Victor Yu. 09-02-2002 */
2643
2644
Jiri Slaby55b307d2006-12-08 02:38:14 -08002645 outb(fcr, info->ioaddr + UART_FCR); /* set fcr */
2646 outb(cval, info->ioaddr + UART_LCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002647
2648 return ret;
2649}
2650
2651
Jiri Slaby55b307d2006-12-08 02:38:14 -08002652static int mxser_set_baud(struct mxser_port *info, long newspd)
Jiri Slaby037ad482006-12-08 02:38:11 -08002653{
2654 int quot = 0;
2655 unsigned char cval;
2656 int ret = 0;
2657 unsigned long flags;
2658
2659 if (!info->tty || !info->tty->termios)
2660 return ret;
2661
Jiri Slaby55b307d2006-12-08 02:38:14 -08002662 if (!(info->ioaddr))
Jiri Slaby037ad482006-12-08 02:38:11 -08002663 return ret;
2664
Jiri Slaby55b307d2006-12-08 02:38:14 -08002665 if (newspd > info->max_baud)
Jiri Slaby037ad482006-12-08 02:38:11 -08002666 return 0;
2667
2668 info->realbaud = newspd;
2669 if (newspd == 134) {
2670 quot = (2 * info->baud_base / 269);
2671 } else if (newspd) {
2672 quot = info->baud_base / newspd;
2673 if (quot == 0)
2674 quot = 1;
2675 } else {
2676 quot = 0;
2677 }
2678
2679 info->timeout = ((info->xmit_fifo_size * HZ * 10 * quot) / info->baud_base);
2680 info->timeout += HZ / 50; /* Add .02 seconds of slop */
2681
2682 if (quot) {
2683 spin_lock_irqsave(&info->slock, flags);
2684 info->MCR |= UART_MCR_DTR;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002685 outb(info->MCR, info->ioaddr + UART_MCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002686 spin_unlock_irqrestore(&info->slock, flags);
2687 } else {
2688 spin_lock_irqsave(&info->slock, flags);
2689 info->MCR &= ~UART_MCR_DTR;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002690 outb(info->MCR, info->ioaddr + UART_MCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002691 spin_unlock_irqrestore(&info->slock, flags);
2692 return ret;
2693 }
2694
Jiri Slaby55b307d2006-12-08 02:38:14 -08002695 cval = inb(info->ioaddr + UART_LCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002696
Jiri Slaby55b307d2006-12-08 02:38:14 -08002697 outb(cval | UART_LCR_DLAB, info->ioaddr + UART_LCR); /* set DLAB */
Jiri Slaby037ad482006-12-08 02:38:11 -08002698
Jiri Slaby55b307d2006-12-08 02:38:14 -08002699 outb(quot & 0xff, info->ioaddr + UART_DLL); /* LS of divisor */
2700 outb(quot >> 8, info->ioaddr + UART_DLM); /* MS of divisor */
2701 outb(cval, info->ioaddr + UART_LCR); /* reset DLAB */
Jiri Slaby037ad482006-12-08 02:38:11 -08002702
2703
2704 return ret;
2705}
2706
2707/*
2708 * ------------------------------------------------------------
2709 * friends of mxser_ioctl()
2710 * ------------------------------------------------------------
2711 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002712static int mxser_get_serial_info(struct mxser_port *info,
2713 struct serial_struct __user *retinfo)
Jiri Slaby037ad482006-12-08 02:38:11 -08002714{
2715 struct serial_struct tmp;
2716
2717 if (!retinfo)
2718 return -EFAULT;
2719 memset(&tmp, 0, sizeof(tmp));
2720 tmp.type = info->type;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002721 tmp.line = info->tty->index;
2722 tmp.port = info->ioaddr;
2723 tmp.irq = info->board->irq;
Jiri Slaby037ad482006-12-08 02:38:11 -08002724 tmp.flags = info->flags;
2725 tmp.baud_base = info->baud_base;
2726 tmp.close_delay = info->close_delay;
2727 tmp.closing_wait = info->closing_wait;
2728 tmp.custom_divisor = info->custom_divisor;
2729 tmp.hub6 = 0;
2730 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2731 return -EFAULT;
2732 return 0;
2733}
2734
Jiri Slaby55b307d2006-12-08 02:38:14 -08002735static int mxser_set_serial_info(struct mxser_port *info,
2736 struct serial_struct __user *new_info)
Jiri Slaby037ad482006-12-08 02:38:11 -08002737{
2738 struct serial_struct new_serial;
2739 unsigned int flags;
2740 int retval = 0;
2741
Jiri Slaby55b307d2006-12-08 02:38:14 -08002742 if (!new_info || !info->ioaddr)
Jiri Slaby037ad482006-12-08 02:38:11 -08002743 return -EFAULT;
2744 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
2745 return -EFAULT;
2746
Jiri Slaby55b307d2006-12-08 02:38:14 -08002747 if ((new_serial.irq != info->board->irq) ||
2748 (new_serial.port != info->ioaddr) ||
Jiri Slaby037ad482006-12-08 02:38:11 -08002749 (new_serial.custom_divisor != info->custom_divisor) ||
2750 (new_serial.baud_base != info->baud_base))
2751 return -EPERM;
2752
2753 flags = info->flags & ASYNC_SPD_MASK;
2754
2755 if (!capable(CAP_SYS_ADMIN)) {
2756 if ((new_serial.baud_base != info->baud_base) ||
2757 (new_serial.close_delay != info->close_delay) ||
2758 ((new_serial.flags & ~ASYNC_USR_MASK) != (info->flags & ~ASYNC_USR_MASK)))
2759 return -EPERM;
2760 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
2761 (new_serial.flags & ASYNC_USR_MASK));
2762 } else {
2763 /*
2764 * OK, past this point, all the error checking has been done.
2765 * At this point, we start making changes.....
2766 */
2767 info->flags = ((info->flags & ~ASYNC_FLAGS) |
2768 (new_serial.flags & ASYNC_FLAGS));
2769 info->close_delay = new_serial.close_delay * HZ / 100;
2770 info->closing_wait = new_serial.closing_wait * HZ / 100;
2771 info->tty->low_latency =
2772 (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
2773 info->tty->low_latency = 0; /* (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0; */
2774 }
2775
2776 /* added by casper, 3/17/2000, for mouse */
2777 info->type = new_serial.type;
2778
2779 process_txrx_fifo(info);
2780
2781 if (info->flags & ASYNC_INITIALIZED) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002782 if (flags != (info->flags & ASYNC_SPD_MASK))
Jiri Slaby037ad482006-12-08 02:38:11 -08002783 mxser_change_speed(info, NULL);
Jiri Slaby55b307d2006-12-08 02:38:14 -08002784 } else
Jiri Slaby037ad482006-12-08 02:38:11 -08002785 retval = mxser_startup(info);
Jiri Slaby55b307d2006-12-08 02:38:14 -08002786
Jiri Slaby037ad482006-12-08 02:38:11 -08002787 return retval;
2788}
2789
2790/*
2791 * mxser_get_lsr_info - get line status register info
2792 *
2793 * Purpose: Let user call ioctl() to get info when the UART physically
2794 * is emptied. On bus types like RS485, the transmitter must
2795 * release the bus after transmitting. This must be done when
2796 * the transmit shift register is empty, not be done when the
2797 * transmit holding register is empty. This functionality
2798 * allows an RS485 driver to be written in user space.
2799 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002800static int mxser_get_lsr_info(struct mxser_port *info,
2801 unsigned int __user *value)
Jiri Slaby037ad482006-12-08 02:38:11 -08002802{
2803 unsigned char status;
2804 unsigned int result;
2805 unsigned long flags;
2806
2807 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby55b307d2006-12-08 02:38:14 -08002808 status = inb(info->ioaddr + UART_LSR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002809 spin_unlock_irqrestore(&info->slock, flags);
2810 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
2811 return put_user(result, value);
2812}
2813
2814/*
2815 * This routine sends a break character out the serial port.
2816 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002817static void mxser_send_break(struct mxser_port *info, int duration)
Jiri Slaby037ad482006-12-08 02:38:11 -08002818{
2819 unsigned long flags;
2820
Jiri Slaby55b307d2006-12-08 02:38:14 -08002821 if (!info->ioaddr)
Jiri Slaby037ad482006-12-08 02:38:11 -08002822 return;
2823 set_current_state(TASK_INTERRUPTIBLE);
2824 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby55b307d2006-12-08 02:38:14 -08002825 outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
2826 info->ioaddr + UART_LCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002827 spin_unlock_irqrestore(&info->slock, flags);
2828 schedule_timeout(duration);
2829 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby55b307d2006-12-08 02:38:14 -08002830 outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
2831 info->ioaddr + UART_LCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002832 spin_unlock_irqrestore(&info->slock, flags);
2833}
2834
2835static int mxser_tiocmget(struct tty_struct *tty, struct file *file)
2836{
Jiri Slaby55b307d2006-12-08 02:38:14 -08002837 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08002838 unsigned char control, status;
2839 unsigned long flags;
2840
2841
2842 if (tty->index == MXSER_PORTS)
2843 return -ENOIOCTLCMD;
2844 if (tty->flags & (1 << TTY_IO_ERROR))
2845 return -EIO;
2846
2847 control = info->MCR;
2848
2849 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby55b307d2006-12-08 02:38:14 -08002850 status = inb(info->ioaddr + UART_MSR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002851 if (status & UART_MSR_ANY_DELTA)
2852 mxser_check_modem_status(info, status);
2853 spin_unlock_irqrestore(&info->slock, flags);
2854 return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0) |
2855 ((control & UART_MCR_DTR) ? TIOCM_DTR : 0) |
2856 ((status & UART_MSR_DCD) ? TIOCM_CAR : 0) |
2857 ((status & UART_MSR_RI) ? TIOCM_RNG : 0) |
2858 ((status & UART_MSR_DSR) ? TIOCM_DSR : 0) |
2859 ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
2860}
2861
Jiri Slaby55b307d2006-12-08 02:38:14 -08002862static int mxser_tiocmset(struct tty_struct *tty, struct file *file,
2863 unsigned int set, unsigned int clear)
Jiri Slaby037ad482006-12-08 02:38:11 -08002864{
Jiri Slaby55b307d2006-12-08 02:38:14 -08002865 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08002866 unsigned long flags;
2867
2868
2869 if (tty->index == MXSER_PORTS)
2870 return -ENOIOCTLCMD;
2871 if (tty->flags & (1 << TTY_IO_ERROR))
2872 return -EIO;
2873
2874 spin_lock_irqsave(&info->slock, flags);
2875
2876 if (set & TIOCM_RTS)
2877 info->MCR |= UART_MCR_RTS;
2878 if (set & TIOCM_DTR)
2879 info->MCR |= UART_MCR_DTR;
2880
2881 if (clear & TIOCM_RTS)
2882 info->MCR &= ~UART_MCR_RTS;
2883 if (clear & TIOCM_DTR)
2884 info->MCR &= ~UART_MCR_DTR;
2885
Jiri Slaby55b307d2006-12-08 02:38:14 -08002886 outb(info->MCR, info->ioaddr + UART_MCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002887 spin_unlock_irqrestore(&info->slock, flags);
2888 return 0;
2889}
2890
2891
2892static int mxser_read_register(int, unsigned short *);
2893static int mxser_program_mode(int);
2894static void mxser_normal_mode(int);
2895
Jiri Slaby943f2952006-12-08 02:38:15 -08002896static int __init mxser_get_ISA_conf(int cap, struct mxser_board *brd)
Jiri Slaby037ad482006-12-08 02:38:11 -08002897{
2898 int id, i, bits;
2899 unsigned short regs[16], irq;
2900 unsigned char scratch, scratch2;
2901
Jiri Slaby55b307d2006-12-08 02:38:14 -08002902 brd->chip_flag = MOXA_OTHER_UART;
Jiri Slaby037ad482006-12-08 02:38:11 -08002903
2904 id = mxser_read_register(cap, regs);
2905 if (id == C168_ASIC_ID) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002906 brd->board_type = MXSER_BOARD_C168_ISA;
2907 brd->nports = 8;
Jiri Slaby037ad482006-12-08 02:38:11 -08002908 } else if (id == C104_ASIC_ID) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002909 brd->board_type = MXSER_BOARD_C104_ISA;
2910 brd->nports = 4;
Jiri Slaby037ad482006-12-08 02:38:11 -08002911 } else if (id == C102_ASIC_ID) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002912 brd->board_type = MXSER_BOARD_C102_ISA;
2913 brd->nports = 2;
Jiri Slaby037ad482006-12-08 02:38:11 -08002914 } else if (id == CI132_ASIC_ID) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002915 brd->board_type = MXSER_BOARD_CI132;
2916 brd->nports = 2;
Jiri Slaby037ad482006-12-08 02:38:11 -08002917 } else if (id == CI134_ASIC_ID) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002918 brd->board_type = MXSER_BOARD_CI134;
2919 brd->nports = 4;
Jiri Slaby037ad482006-12-08 02:38:11 -08002920 } else if (id == CI104J_ASIC_ID) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002921 brd->board_type = MXSER_BOARD_CI104J;
2922 brd->nports = 4;
Jiri Slaby037ad482006-12-08 02:38:11 -08002923 } else
2924 return 0;
2925
2926 irq = 0;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002927 if (brd->nports == 2) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002928 irq = regs[9] & 0xF000;
2929 irq = irq | (irq >> 4);
2930 if (irq != (regs[9] & 0xFF00))
2931 return MXSER_ERR_IRQ_CONFLIT;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002932 } else if (brd->nports == 4) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002933 irq = regs[9] & 0xF000;
2934 irq = irq | (irq >> 4);
2935 irq = irq | (irq >> 8);
2936 if (irq != regs[9])
2937 return MXSER_ERR_IRQ_CONFLIT;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002938 } else if (brd->nports == 8) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002939 irq = regs[9] & 0xF000;
2940 irq = irq | (irq >> 4);
2941 irq = irq | (irq >> 8);
2942 if ((irq != regs[9]) || (irq != regs[10]))
2943 return MXSER_ERR_IRQ_CONFLIT;
2944 }
2945
2946 if (!irq)
2947 return MXSER_ERR_IRQ;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002948 brd->irq = ((int)(irq & 0xF000) >> 12);
Jiri Slaby037ad482006-12-08 02:38:11 -08002949 for (i = 0; i < 8; i++)
Jiri Slaby55b307d2006-12-08 02:38:14 -08002950 brd->ports[i].ioaddr = (int) regs[i + 1] & 0xFFF8;
Jiri Slaby037ad482006-12-08 02:38:11 -08002951 if ((regs[12] & 0x80) == 0)
2952 return MXSER_ERR_VECTOR;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002953 brd->vector = (int)regs[11]; /* interrupt vector */
Jiri Slaby037ad482006-12-08 02:38:11 -08002954 if (id == 1)
Jiri Slaby55b307d2006-12-08 02:38:14 -08002955 brd->vector_mask = 0x00FF;
Jiri Slaby037ad482006-12-08 02:38:11 -08002956 else
Jiri Slaby55b307d2006-12-08 02:38:14 -08002957 brd->vector_mask = 0x000F;
Jiri Slaby037ad482006-12-08 02:38:11 -08002958 for (i = 7, bits = 0x0100; i >= 0; i--, bits <<= 1) {
2959 if (regs[12] & bits) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002960 brd->ports[i].baud_base = 921600;
2961 brd->ports[i].max_baud = 921600; /* add by Victor Yu. 09-04-2002 */
Jiri Slaby037ad482006-12-08 02:38:11 -08002962 } else {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002963 brd->ports[i].baud_base = 115200;
2964 brd->ports[i].max_baud = 115200; /* add by Victor Yu. 09-04-2002 */
Jiri Slaby037ad482006-12-08 02:38:11 -08002965 }
2966 }
2967 scratch2 = inb(cap + UART_LCR) & (~UART_LCR_DLAB);
2968 outb(scratch2 | UART_LCR_DLAB, cap + UART_LCR);
2969 outb(0, cap + UART_EFR); /* EFR is the same as FCR */
2970 outb(scratch2, cap + UART_LCR);
2971 outb(UART_FCR_ENABLE_FIFO, cap + UART_FCR);
2972 scratch = inb(cap + UART_IIR);
2973
2974 if (scratch & 0xC0)
Jiri Slaby55b307d2006-12-08 02:38:14 -08002975 brd->uart_type = PORT_16550A;
Jiri Slaby037ad482006-12-08 02:38:11 -08002976 else
Jiri Slaby55b307d2006-12-08 02:38:14 -08002977 brd->uart_type = PORT_16450;
Jiri Slaby037ad482006-12-08 02:38:11 -08002978 if (id == 1)
Jiri Slaby55b307d2006-12-08 02:38:14 -08002979 brd->nports = 8;
Jiri Slaby037ad482006-12-08 02:38:11 -08002980 else
Jiri Slaby55b307d2006-12-08 02:38:14 -08002981 brd->nports = 4;
Jiri Slaby7a7a5c32006-12-08 02:38:17 -08002982 if (!request_region(brd->ports[0].ioaddr, 8 * brd->nports, "mxser(IO)"))
2983 return MXSER_ERR_IOADDR;
2984 if (!request_region(brd->vector, 1, "mxser(vector)")) {
2985 release_region(brd->ports[0].ioaddr, 8 * brd->nports);
2986 return MXSER_ERR_VECTOR;
2987 }
Jiri Slaby55b307d2006-12-08 02:38:14 -08002988 return brd->nports;
Jiri Slaby037ad482006-12-08 02:38:11 -08002989}
2990
2991#define CHIP_SK 0x01 /* Serial Data Clock in Eprom */
2992#define CHIP_DO 0x02 /* Serial Data Output in Eprom */
2993#define CHIP_CS 0x04 /* Serial Chip Select in Eprom */
2994#define CHIP_DI 0x08 /* Serial Data Input in Eprom */
2995#define EN_CCMD 0x000 /* Chip's command register */
2996#define EN0_RSARLO 0x008 /* Remote start address reg 0 */
2997#define EN0_RSARHI 0x009 /* Remote start address reg 1 */
2998#define EN0_RCNTLO 0x00A /* Remote byte count reg WR */
2999#define EN0_RCNTHI 0x00B /* Remote byte count reg WR */
3000#define EN0_DCFG 0x00E /* Data configuration reg WR */
3001#define EN0_PORT 0x010 /* Rcv missed frame error counter RD */
3002#define ENC_PAGE0 0x000 /* Select page 0 of chip registers */
3003#define ENC_PAGE3 0x0C0 /* Select page 3 of chip registers */
3004static int mxser_read_register(int port, unsigned short *regs)
3005{
3006 int i, k, value, id;
3007 unsigned int j;
3008
3009 id = mxser_program_mode(port);
3010 if (id < 0)
3011 return id;
3012 for (i = 0; i < 14; i++) {
3013 k = (i & 0x3F) | 0x180;
3014 for (j = 0x100; j > 0; j >>= 1) {
3015 outb(CHIP_CS, port);
3016 if (k & j) {
3017 outb(CHIP_CS | CHIP_DO, port);
3018 outb(CHIP_CS | CHIP_DO | CHIP_SK, port); /* A? bit of read */
3019 } else {
3020 outb(CHIP_CS, port);
3021 outb(CHIP_CS | CHIP_SK, port); /* A? bit of read */
3022 }
3023 }
3024 (void)inb(port);
3025 value = 0;
3026 for (k = 0, j = 0x8000; k < 16; k++, j >>= 1) {
3027 outb(CHIP_CS, port);
3028 outb(CHIP_CS | CHIP_SK, port);
3029 if (inb(port) & CHIP_DI)
3030 value |= j;
3031 }
3032 regs[i] = value;
3033 outb(0, port);
3034 }
3035 mxser_normal_mode(port);
3036 return id;
3037}
3038
3039static int mxser_program_mode(int port)
3040{
3041 int id, i, j, n;
3042 /* unsigned long flags; */
3043
3044 spin_lock(&gm_lock);
3045 outb(0, port);
3046 outb(0, port);
3047 outb(0, port);
3048 (void)inb(port);
3049 (void)inb(port);
3050 outb(0, port);
3051 (void)inb(port);
3052 /* restore_flags(flags); */
3053 spin_unlock(&gm_lock);
3054
3055 id = inb(port + 1) & 0x1F;
3056 if ((id != C168_ASIC_ID) &&
3057 (id != C104_ASIC_ID) &&
3058 (id != C102_ASIC_ID) &&
3059 (id != CI132_ASIC_ID) &&
3060 (id != CI134_ASIC_ID) &&
3061 (id != CI104J_ASIC_ID))
3062 return -1;
3063 for (i = 0, j = 0; i < 4; i++) {
3064 n = inb(port + 2);
3065 if (n == 'M') {
3066 j = 1;
3067 } else if ((j == 1) && (n == 1)) {
3068 j = 2;
3069 break;
3070 } else
3071 j = 0;
3072 }
3073 if (j != 2)
3074 id = -2;
3075 return id;
3076}
3077
3078static void mxser_normal_mode(int port)
3079{
3080 int i, n;
3081
3082 outb(0xA5, port + 1);
3083 outb(0x80, port + 3);
3084 outb(12, port + 0); /* 9600 bps */
3085 outb(0, port + 1);
3086 outb(0x03, port + 3); /* 8 data bits */
3087 outb(0x13, port + 4); /* loop back mode */
3088 for (i = 0; i < 16; i++) {
3089 n = inb(port + 5);
3090 if ((n & 0x61) == 0x60)
3091 break;
3092 if ((n & 1) == 1)
3093 (void)inb(port);
3094 }
3095 outb(0x00, port + 4);
3096}
3097
3098module_init(mxser_module_init);
3099module_exit(mxser_module_exit);