blob: 792524ab348ed44016428fc7d0b92588516f6009 [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 Slabyc88cb8f92006-12-08 02:38:20 -08005 * Copyright (C) 2006 Jiri Slaby <jirislaby@gmail.com>
Jiri Slaby037ad482006-12-08 02:38:11 -08006 *
Jiri Slabyc88cb8f92006-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
Jiri Slabycd7ed642006-12-08 02:38:29 -080087#define MXSER_HIGHBAUD 1
88#define MXSER_HAS2 2
Jiri Slaby037ad482006-12-08 02:38:11 -080089
90/* This is only for PCI */
Jiri Slabycd7ed642006-12-08 02:38:29 -080091static const struct {
Jiri Slaby037ad482006-12-08 02:38:11 -080092 int type;
93 int tx_fifo;
94 int rx_fifo;
95 int xmit_fifo_size;
96 int rx_high_water;
97 int rx_trigger;
98 int rx_low_water;
99 long max_baud;
Jiri Slabycd7ed642006-12-08 02:38:29 -0800100} Gpci_uart_info[] = {
Jiri Slaby037ad482006-12-08 02:38:11 -0800101 {MOXA_OTHER_UART, 16, 16, 16, 14, 14, 1, 921600L},
102 {MOXA_MUST_MU150_HWID, 64, 64, 64, 48, 48, 16, 230400L},
103 {MOXA_MUST_MU860_HWID, 128, 128, 128, 96, 96, 32, 921600L}
104};
Jiri Slabycd7ed642006-12-08 02:38:29 -0800105#define UART_INFO_NUM ARRAY_SIZE(Gpci_uart_info)
Jiri Slaby037ad482006-12-08 02:38:11 -0800106
Jiri Slabycd7ed642006-12-08 02:38:29 -0800107struct mxser_cardinfo {
108 unsigned int nports;
109 char *name;
110 unsigned int flags;
111};
112
113static const struct mxser_cardinfo mxser_cards[] = {
114 { 8, "C168 series", }, /* C168-ISA */
115 { 4, "C104 series", }, /* C104-ISA */
116 { 4, "CI-104J series", }, /* CI104J */
117 { 8, "C168H/PCI series", }, /* C168-PCI */
118 { 4, "C104H/PCI series", }, /* C104-PCI */
119 { 4, "C102 series", MXSER_HAS2 }, /* C102-ISA */
120 { 4, "CI-132 series", MXSER_HAS2 }, /* CI132 */
121 { 4, "CI-134 series", }, /* CI134 */
122 { 2, "CP-132 series", }, /* CP132 */
123 { 4, "CP-114 series", }, /* CP114 */
124 { 4, "CT-114 series", }, /* CT114 */
125 { 2, "CP-102 series", MXSER_HIGHBAUD }, /* CP102 */
126 { 4, "CP-104U series", }, /* CP104U */
127 { 8, "CP-168U series", }, /* CP168U */
128 { 2, "CP-132U series", }, /* CP132U */
129 { 4, "CP-134U series", }, /* CP134U */
130 { 4, "CP-104JU series", }, /* CP104JU */
131 { 8, "Moxa UC7000 Serial", }, /* RC7000 */
132 { 8, "CP-118U series", }, /* CP118U */
133 { 2, "CP-102UL series", }, /* CP102UL */
134 { 2, "CP-102U series", }, /* CP102U */
135 { 8, "CP-118EL series", }, /* CP118EL */
136 { 8, "CP-168EL series", }, /* CP168EL */
137 { 4, "CP-104EL series", } /* CP104EL */
138};
139
140/* driver_data correspond to the lines in the structure above
141 see also ISA probe function before you change something */
Jiri Slaby037ad482006-12-08 02:38:11 -0800142static struct pci_device_id mxser_pcibrds[] = {
Jiri Slaby3306ce32006-12-08 02:38:13 -0800143 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C168),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800144 .driver_data = 3 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800145 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C104),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800146 .driver_data = 4 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800147 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP132),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800148 .driver_data = 8 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800149 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP114),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800150 .driver_data = 9 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800151 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CT114),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800152 .driver_data = 10 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800153 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP102),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800154 .driver_data = 11 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800155 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP104U),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800156 .driver_data = 12 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800157 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP168U),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800158 .driver_data = 13 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800159 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP132U),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800160 .driver_data = 14 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800161 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP134U),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800162 .driver_data = 15 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800163 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP104JU),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800164 .driver_data = 16 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800165 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_RC7000),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800166 .driver_data = 17 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800167 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP118U),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800168 .driver_data = 18 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800169 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP102UL),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800170 .driver_data = 19 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800171 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP102U),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800172 .driver_data = 20 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800173 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP118EL),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800174 .driver_data = 21 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800175 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP168EL),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800176 .driver_data = 22 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800177 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP104EL),
Jiri Slabycd7ed642006-12-08 02:38:29 -0800178 .driver_data = 23 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800179 { }
Jiri Slaby037ad482006-12-08 02:38:11 -0800180};
Jiri Slaby037ad482006-12-08 02:38:11 -0800181MODULE_DEVICE_TABLE(pci, mxser_pcibrds);
182
Jiri Slaby037ad482006-12-08 02:38:11 -0800183static int ioaddr[MXSER_BOARDS] = { 0, 0, 0, 0 };
184static int ttymajor = MXSERMAJOR;
185static int calloutmajor = MXSERCUMAJOR;
Jiri Slaby037ad482006-12-08 02:38:11 -0800186
187/* Variables for insmod */
188
189MODULE_AUTHOR("Casper Yang");
190MODULE_DESCRIPTION("MOXA Smartio/Industio Family Multiport Board Device Driver");
191module_param_array(ioaddr, int, NULL, 0);
192module_param(ttymajor, int, 0);
Jiri Slaby037ad482006-12-08 02:38:11 -0800193MODULE_LICENSE("GPL");
194
195struct mxser_log {
196 int tick;
197 unsigned long rxcnt[MXSER_PORTS];
198 unsigned long txcnt[MXSER_PORTS];
199};
200
201
202struct mxser_mon {
203 unsigned long rxcnt;
204 unsigned long txcnt;
205 unsigned long up_rxcnt;
206 unsigned long up_txcnt;
207 int modem_status;
208 unsigned char hold_reason;
209};
210
211struct mxser_mon_ext {
212 unsigned long rx_cnt[32];
213 unsigned long tx_cnt[32];
214 unsigned long up_rxcnt[32];
215 unsigned long up_txcnt[32];
216 int modem_status[32];
217
218 long baudrate[32];
219 int databits[32];
220 int stopbits[32];
221 int parity[32];
222 int flowctrl[32];
223 int fifo[32];
224 int iftype[32];
225};
226
Jiri Slaby55b307d2006-12-08 02:38:14 -0800227struct mxser_board;
Jiri Slaby037ad482006-12-08 02:38:11 -0800228
Jiri Slaby55b307d2006-12-08 02:38:14 -0800229struct mxser_port {
230 struct mxser_board *board;
231 struct tty_struct *tty;
232
233 unsigned long ioaddr;
234 unsigned long opmode_ioaddr;
235 int max_baud;
236
Jiri Slaby037ad482006-12-08 02:38:11 -0800237 int rx_high_water;
238 int rx_trigger; /* Rx fifo trigger level */
239 int rx_low_water;
240 int baud_base; /* max. speed */
Jiri Slaby55b307d2006-12-08 02:38:14 -0800241 long realbaud;
Jiri Slaby037ad482006-12-08 02:38:11 -0800242 int type; /* UART type */
Jiri Slaby55b307d2006-12-08 02:38:14 -0800243 int flags; /* defined in tty.h */
244 long session; /* Session of opening process */
245 long pgrp; /* pgrp of opening process */
246
247 int x_char; /* xon/xoff character */
248 int IER; /* Interrupt Enable Register */
249 int MCR; /* Modem control register */
250
251 unsigned char stop_rx;
252 unsigned char ldisc_stop_rx;
253
254 int custom_divisor;
255 int close_delay;
256 unsigned short closing_wait;
257 unsigned char err_shadow;
258 unsigned long event;
259
260 int count; /* # of fd on device */
261 int blocked_open; /* # of blocked opens */
262 struct async_icount icount; /* kernel counters for 4 input interrupts */
263 int timeout;
264
Jiri Slaby037ad482006-12-08 02:38:11 -0800265 int read_status_mask;
266 int ignore_status_mask;
267 int xmit_fifo_size;
Jiri Slaby037ad482006-12-08 02:38:11 -0800268 unsigned char *xmit_buf;
269 int xmit_head;
270 int xmit_tail;
271 int xmit_cnt;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800272
Jiri Slaby037ad482006-12-08 02:38:11 -0800273 struct termios normal_termios;
274 struct termios callout_termios;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800275
276 struct mxser_mon mon_data;
277
278 spinlock_t slock;
279 struct work_struct tqueue;
Jiri Slaby037ad482006-12-08 02:38:11 -0800280 wait_queue_head_t open_wait;
281 wait_queue_head_t close_wait;
282 wait_queue_head_t delta_msr_wait;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800283};
284
285struct mxser_board {
286 struct pci_dev *pdev; /* temporary (until pci probing) */
287
288 int irq;
Jiri Slabycd7ed642006-12-08 02:38:29 -0800289 const struct mxser_cardinfo *info;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800290 unsigned long vector;
291 unsigned long vector_mask;
292
293 int chip_flag;
294 int uart_type;
295
296 struct mxser_port ports[MXSER_PORTS_PER_BOARD];
Jiri Slaby037ad482006-12-08 02:38:11 -0800297};
298
299struct mxser_mstatus {
300 tcflag_t cflag;
301 int cts;
302 int dsr;
303 int ri;
304 int dcd;
305};
306
307static struct mxser_mstatus GMStatus[MXSER_PORTS];
308
309static int mxserBoardCAP[MXSER_BOARDS] = {
310 0, 0, 0, 0
311 /* 0x180, 0x280, 0x200, 0x320 */
312};
313
Jiri Slaby55b307d2006-12-08 02:38:14 -0800314static struct mxser_board mxser_boards[MXSER_BOARDS];
Jiri Slaby037ad482006-12-08 02:38:11 -0800315static struct tty_driver *mxvar_sdriver;
Jiri Slaby037ad482006-12-08 02:38:11 -0800316static struct mxser_log mxvar_log;
317static int mxvar_diagflag;
318static unsigned char mxser_msr[MXSER_PORTS + 1];
319static struct mxser_mon_ext mon_data_ext;
320static int mxser_set_baud_method[MXSER_PORTS + 1];
321static spinlock_t gm_lock;
322
Jiri Slaby037ad482006-12-08 02:38:11 -0800323static int CheckIsMoxaMust(int io)
324{
325 u8 oldmcr, hwid;
326 int i;
327
328 outb(0, io + UART_LCR);
329 DISABLE_MOXA_MUST_ENCHANCE_MODE(io);
330 oldmcr = inb(io + UART_MCR);
331 outb(0, io + UART_MCR);
332 SET_MOXA_MUST_XON1_VALUE(io, 0x11);
333 if ((hwid = inb(io + UART_MCR)) != 0) {
334 outb(oldmcr, io + UART_MCR);
335 return MOXA_OTHER_UART;
336 }
337
338 GET_MOXA_MUST_HARDWARE_ID(io, &hwid);
Jiri Slabycd7ed642006-12-08 02:38:29 -0800339 for (i = 1; i < UART_INFO_NUM; i++) { /* 0 = OTHER_UART */
340 if (hwid == Gpci_uart_info[i].type)
Jiri Slaby037ad482006-12-08 02:38:11 -0800341 return (int)hwid;
342 }
343 return MOXA_OTHER_UART;
344}
345
Jiri Slaby55b307d2006-12-08 02:38:14 -0800346static void process_txrx_fifo(struct mxser_port *info)
Jiri Slaby037ad482006-12-08 02:38:11 -0800347{
348 int i;
349
350 if ((info->type == PORT_16450) || (info->type == PORT_8250)) {
351 info->rx_trigger = 1;
352 info->rx_high_water = 1;
353 info->rx_low_water = 1;
354 info->xmit_fifo_size = 1;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800355 } else
356 for (i = 0; i < UART_INFO_NUM; i++)
357 if (info->board->chip_flag == Gpci_uart_info[i].type) {
Jiri Slaby037ad482006-12-08 02:38:11 -0800358 info->rx_trigger = Gpci_uart_info[i].rx_trigger;
359 info->rx_low_water = Gpci_uart_info[i].rx_low_water;
360 info->rx_high_water = Gpci_uart_info[i].rx_high_water;
361 info->xmit_fifo_size = Gpci_uart_info[i].xmit_fifo_size;
362 break;
363 }
Jiri Slaby037ad482006-12-08 02:38:11 -0800364}
365
Jiri Slaby037ad482006-12-08 02:38:11 -0800366static void mxser_do_softint(void *private_)
367{
Jiri Slaby55b307d2006-12-08 02:38:14 -0800368 struct mxser_port *info = private_;
Jiri Slaby037ad482006-12-08 02:38:11 -0800369 struct tty_struct *tty;
370
371 tty = info->tty;
372
Jiri Slaby3306ce32006-12-08 02:38:13 -0800373 if (test_and_clear_bit(MXSER_EVENT_TXLOW, &info->event))
374 tty_wakeup(tty);
375 if (test_and_clear_bit(MXSER_EVENT_HANGUP, &info->event))
376 tty_hangup(tty);
Jiri Slaby037ad482006-12-08 02:38:11 -0800377}
378
Jiri Slaby55b307d2006-12-08 02:38:14 -0800379static unsigned char mxser_get_msr(int baseaddr, int mode, int port)
Jiri Slaby037ad482006-12-08 02:38:11 -0800380{
381 unsigned char status = 0;
382
383 status = inb(baseaddr + UART_MSR);
384
385 mxser_msr[port] &= 0x0F;
386 mxser_msr[port] |= status;
387 status = mxser_msr[port];
388 if (mode)
389 mxser_msr[port] = 0;
390
391 return status;
392}
393
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800394static int mxser_block_til_ready(struct tty_struct *tty, struct file *filp,
395 struct mxser_port *port)
396{
397 DECLARE_WAITQUEUE(wait, current);
398 int retval;
399 int do_clocal = 0;
400 unsigned long flags;
401
402 /*
403 * If non-blocking mode is set, or the port is not enabled,
404 * then make the check up front and then exit.
405 */
Jiri Slaby214efeb2006-12-08 02:38:25 -0800406 if ((filp->f_flags & O_NONBLOCK) ||
407 test_bit(TTY_IO_ERROR, &tty->flags)) {
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800408 port->flags |= ASYNC_NORMAL_ACTIVE;
409 return 0;
410 }
411
412 if (tty->termios->c_cflag & CLOCAL)
413 do_clocal = 1;
414
415 /*
416 * Block waiting for the carrier detect and the line to become
417 * free (i.e., not in use by the callout). While we are in
418 * this loop, port->count is dropped by one, so that
419 * mxser_close() knows when to free things. We restore it upon
420 * exit, either normal or abnormal.
421 */
422 retval = 0;
423 add_wait_queue(&port->open_wait, &wait);
424
425 spin_lock_irqsave(&port->slock, flags);
426 if (!tty_hung_up_p(filp))
427 port->count--;
428 spin_unlock_irqrestore(&port->slock, flags);
429 port->blocked_open++;
430 while (1) {
431 spin_lock_irqsave(&port->slock, flags);
432 outb(inb(port->ioaddr + UART_MCR) |
433 UART_MCR_DTR | UART_MCR_RTS, port->ioaddr + UART_MCR);
434 spin_unlock_irqrestore(&port->slock, flags);
435 set_current_state(TASK_INTERRUPTIBLE);
436 if (tty_hung_up_p(filp) || !(port->flags & ASYNC_INITIALIZED)) {
437 if (port->flags & ASYNC_HUP_NOTIFY)
438 retval = -EAGAIN;
439 else
440 retval = -ERESTARTSYS;
441 break;
442 }
443 if (!(port->flags & ASYNC_CLOSING) &&
444 (do_clocal ||
445 (inb(port->ioaddr + UART_MSR) & UART_MSR_DCD)))
446 break;
447 if (signal_pending(current)) {
448 retval = -ERESTARTSYS;
449 break;
450 }
451 schedule();
452 }
453 set_current_state(TASK_RUNNING);
454 remove_wait_queue(&port->open_wait, &wait);
455 if (!tty_hung_up_p(filp))
456 port->count++;
457 port->blocked_open--;
458 if (retval)
459 return retval;
460 port->flags |= ASYNC_NORMAL_ACTIVE;
461 return 0;
462}
463
464static int mxser_set_baud(struct mxser_port *info, long newspd)
465{
466 int quot = 0;
467 unsigned char cval;
468 int ret = 0;
469 unsigned long flags;
470
471 if (!info->tty || !info->tty->termios)
472 return ret;
473
474 if (!(info->ioaddr))
475 return ret;
476
477 if (newspd > info->max_baud)
478 return 0;
479
480 info->realbaud = newspd;
481 if (newspd == 134) {
482 quot = (2 * info->baud_base / 269);
483 } else if (newspd) {
484 quot = info->baud_base / newspd;
485 if (quot == 0)
486 quot = 1;
487 } else {
488 quot = 0;
489 }
490
491 info->timeout = ((info->xmit_fifo_size * HZ * 10 * quot) / info->baud_base);
492 info->timeout += HZ / 50; /* Add .02 seconds of slop */
493
494 if (quot) {
495 spin_lock_irqsave(&info->slock, flags);
496 info->MCR |= UART_MCR_DTR;
497 outb(info->MCR, info->ioaddr + UART_MCR);
498 spin_unlock_irqrestore(&info->slock, flags);
499 } else {
500 spin_lock_irqsave(&info->slock, flags);
501 info->MCR &= ~UART_MCR_DTR;
502 outb(info->MCR, info->ioaddr + UART_MCR);
503 spin_unlock_irqrestore(&info->slock, flags);
504 return ret;
505 }
506
507 cval = inb(info->ioaddr + UART_LCR);
508
509 outb(cval | UART_LCR_DLAB, info->ioaddr + UART_LCR); /* set DLAB */
510
511 outb(quot & 0xff, info->ioaddr + UART_DLL); /* LS of divisor */
512 outb(quot >> 8, info->ioaddr + UART_DLM); /* MS of divisor */
513 outb(cval, info->ioaddr + UART_LCR); /* reset DLAB */
514
515
516 return ret;
517}
518
519/*
520 * This routine is called to set the UART divisor registers to match
521 * the specified baud rate for a serial port.
522 */
523static int mxser_change_speed(struct mxser_port *info,
524 struct termios *old_termios)
525{
526 unsigned cflag, cval, fcr;
527 int ret = 0;
528 unsigned char status;
529 long baud;
530 unsigned long flags;
531
532 if (!info->tty || !info->tty->termios)
533 return ret;
534 cflag = info->tty->termios->c_cflag;
535 if (!(info->ioaddr))
536 return ret;
537
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800538 if (mxser_set_baud_method[info->tty->index] == 0) {
539 baud = tty_get_baud_rate(info->tty);
540 mxser_set_baud(info, baud);
541 }
542
543 /* byte size and parity */
544 switch (cflag & CSIZE) {
545 case CS5:
546 cval = 0x00;
547 break;
548 case CS6:
549 cval = 0x01;
550 break;
551 case CS7:
552 cval = 0x02;
553 break;
554 case CS8:
555 cval = 0x03;
556 break;
557 default:
558 cval = 0x00;
559 break; /* too keep GCC shut... */
560 }
561 if (cflag & CSTOPB)
562 cval |= 0x04;
563 if (cflag & PARENB)
564 cval |= UART_LCR_PARITY;
565 if (!(cflag & PARODD))
566 cval |= UART_LCR_EPAR;
567 if (cflag & CMSPAR)
568 cval |= UART_LCR_SPAR;
569
570 if ((info->type == PORT_8250) || (info->type == PORT_16450)) {
571 if (info->board->chip_flag) {
572 fcr = UART_FCR_ENABLE_FIFO;
573 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
574 SET_MOXA_MUST_FIFO_VALUE(info);
575 } else
576 fcr = 0;
577 } else {
578 fcr = UART_FCR_ENABLE_FIFO;
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800579 if (info->board->chip_flag) {
580 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
581 SET_MOXA_MUST_FIFO_VALUE(info);
582 } else {
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800583 switch (info->rx_trigger) {
584 case 1:
585 fcr |= UART_FCR_TRIGGER_1;
586 break;
587 case 4:
588 fcr |= UART_FCR_TRIGGER_4;
589 break;
590 case 8:
591 fcr |= UART_FCR_TRIGGER_8;
592 break;
593 default:
594 fcr |= UART_FCR_TRIGGER_14;
595 break;
596 }
597 }
598 }
599
600 /* CTS flow control flag and modem status interrupts */
601 info->IER &= ~UART_IER_MSI;
602 info->MCR &= ~UART_MCR_AFE;
603 if (cflag & CRTSCTS) {
604 info->flags |= ASYNC_CTS_FLOW;
605 info->IER |= UART_IER_MSI;
606 if ((info->type == PORT_16550A) || (info->board->chip_flag)) {
607 info->MCR |= UART_MCR_AFE;
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800608 } else {
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800609 status = inb(info->ioaddr + UART_MSR);
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800610 if (info->tty->hw_stopped) {
611 if (status & UART_MSR_CTS) {
612 info->tty->hw_stopped = 0;
613 if (info->type != PORT_16550A &&
614 !info->board->chip_flag) {
615 outb(info->IER & ~UART_IER_THRI,
616 info->ioaddr +
617 UART_IER);
618 info->IER |= UART_IER_THRI;
619 outb(info->IER, info->ioaddr +
620 UART_IER);
621 }
622 set_bit(MXSER_EVENT_TXLOW, &info->event);
623 schedule_work(&info->tqueue); }
624 } else {
625 if (!(status & UART_MSR_CTS)) {
626 info->tty->hw_stopped = 1;
627 if ((info->type != PORT_16550A) &&
628 (!info->board->chip_flag)) {
629 info->IER &= ~UART_IER_THRI;
630 outb(info->IER, info->ioaddr +
631 UART_IER);
632 }
633 }
634 }
635 }
636 } else {
637 info->flags &= ~ASYNC_CTS_FLOW;
638 }
639 outb(info->MCR, info->ioaddr + UART_MCR);
640 if (cflag & CLOCAL) {
641 info->flags &= ~ASYNC_CHECK_CD;
642 } else {
643 info->flags |= ASYNC_CHECK_CD;
644 info->IER |= UART_IER_MSI;
645 }
646 outb(info->IER, info->ioaddr + UART_IER);
647
648 /*
649 * Set up parity check flag
650 */
651 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
652 if (I_INPCK(info->tty))
653 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
654 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
655 info->read_status_mask |= UART_LSR_BI;
656
657 info->ignore_status_mask = 0;
658
659 if (I_IGNBRK(info->tty)) {
660 info->ignore_status_mask |= UART_LSR_BI;
661 info->read_status_mask |= UART_LSR_BI;
662 /*
663 * If we're ignore parity and break indicators, ignore
664 * overruns too. (For real raw support).
665 */
666 if (I_IGNPAR(info->tty)) {
667 info->ignore_status_mask |=
668 UART_LSR_OE |
669 UART_LSR_PE |
670 UART_LSR_FE;
671 info->read_status_mask |=
672 UART_LSR_OE |
673 UART_LSR_PE |
674 UART_LSR_FE;
675 }
676 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800677 if (info->board->chip_flag) {
678 spin_lock_irqsave(&info->slock, flags);
679 SET_MOXA_MUST_XON1_VALUE(info->ioaddr, START_CHAR(info->tty));
680 SET_MOXA_MUST_XOFF1_VALUE(info->ioaddr, STOP_CHAR(info->tty));
681 if (I_IXON(info->tty)) {
682 ENABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
683 } else {
684 DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
685 }
686 if (I_IXOFF(info->tty)) {
687 ENABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
688 } else {
689 DISABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
690 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800691 spin_unlock_irqrestore(&info->slock, flags);
692 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800693
694
695 outb(fcr, info->ioaddr + UART_FCR); /* set fcr */
696 outb(cval, info->ioaddr + UART_LCR);
697
698 return ret;
699}
700
701static void mxser_check_modem_status(struct mxser_port *port, int status)
702{
703 /* update input line counters */
704 if (status & UART_MSR_TERI)
705 port->icount.rng++;
706 if (status & UART_MSR_DDSR)
707 port->icount.dsr++;
708 if (status & UART_MSR_DDCD)
709 port->icount.dcd++;
710 if (status & UART_MSR_DCTS)
711 port->icount.cts++;
712 port->mon_data.modem_status = status;
713 wake_up_interruptible(&port->delta_msr_wait);
714
715 if ((port->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
716 if (status & UART_MSR_DCD)
717 wake_up_interruptible(&port->open_wait);
718 schedule_work(&port->tqueue);
719 }
720
721 if (port->flags & ASYNC_CTS_FLOW) {
722 if (port->tty->hw_stopped) {
723 if (status & UART_MSR_CTS) {
724 port->tty->hw_stopped = 0;
725
726 if ((port->type != PORT_16550A) &&
727 (!port->board->chip_flag)) {
728 outb(port->IER & ~UART_IER_THRI,
729 port->ioaddr + UART_IER);
730 port->IER |= UART_IER_THRI;
731 outb(port->IER, port->ioaddr +
732 UART_IER);
733 }
734 set_bit(MXSER_EVENT_TXLOW, &port->event);
735 schedule_work(&port->tqueue);
736 }
737 } else {
738 if (!(status & UART_MSR_CTS)) {
739 port->tty->hw_stopped = 1;
740 if (port->type != PORT_16550A &&
741 !port->board->chip_flag) {
742 port->IER &= ~UART_IER_THRI;
743 outb(port->IER, port->ioaddr +
744 UART_IER);
745 }
746 }
747 }
748 }
749}
750
751static int mxser_startup(struct mxser_port *info)
752{
753 unsigned long page;
754 unsigned long flags;
755
756 page = __get_free_page(GFP_KERNEL);
757 if (!page)
758 return -ENOMEM;
759
760 spin_lock_irqsave(&info->slock, flags);
761
762 if (info->flags & ASYNC_INITIALIZED) {
763 free_page(page);
764 spin_unlock_irqrestore(&info->slock, flags);
765 return 0;
766 }
767
768 if (!info->ioaddr || !info->type) {
769 if (info->tty)
770 set_bit(TTY_IO_ERROR, &info->tty->flags);
771 free_page(page);
772 spin_unlock_irqrestore(&info->slock, flags);
773 return 0;
774 }
775 if (info->xmit_buf)
776 free_page(page);
777 else
778 info->xmit_buf = (unsigned char *) page;
779
780 /*
781 * Clear the FIFO buffers and disable them
782 * (they will be reenabled in mxser_change_speed())
783 */
784 if (info->board->chip_flag)
785 outb((UART_FCR_CLEAR_RCVR |
786 UART_FCR_CLEAR_XMIT |
787 MOXA_MUST_FCR_GDA_MODE_ENABLE), info->ioaddr + UART_FCR);
788 else
789 outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
790 info->ioaddr + UART_FCR);
791
792 /*
793 * At this point there's no way the LSR could still be 0xFF;
794 * if it is, then bail out, because there's likely no UART
795 * here.
796 */
797 if (inb(info->ioaddr + UART_LSR) == 0xff) {
798 spin_unlock_irqrestore(&info->slock, flags);
799 if (capable(CAP_SYS_ADMIN)) {
800 if (info->tty)
801 set_bit(TTY_IO_ERROR, &info->tty->flags);
802 return 0;
803 } else
804 return -ENODEV;
805 }
806
807 /*
808 * Clear the interrupt registers.
809 */
810 (void) inb(info->ioaddr + UART_LSR);
811 (void) inb(info->ioaddr + UART_RX);
812 (void) inb(info->ioaddr + UART_IIR);
813 (void) inb(info->ioaddr + UART_MSR);
814
815 /*
816 * Now, initialize the UART
817 */
818 outb(UART_LCR_WLEN8, info->ioaddr + UART_LCR); /* reset DLAB */
819 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
820 outb(info->MCR, info->ioaddr + UART_MCR);
821
822 /*
823 * Finally, enable interrupts
824 */
825 info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800826
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800827 if (info->board->chip_flag)
828 info->IER |= MOXA_MUST_IER_EGDAI;
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800829 outb(info->IER, info->ioaddr + UART_IER); /* enable interrupts */
830
831 /*
832 * And clear the interrupt registers again for luck.
833 */
834 (void) inb(info->ioaddr + UART_LSR);
835 (void) inb(info->ioaddr + UART_RX);
836 (void) inb(info->ioaddr + UART_IIR);
837 (void) inb(info->ioaddr + UART_MSR);
838
839 if (info->tty)
840 clear_bit(TTY_IO_ERROR, &info->tty->flags);
841 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
842
843 /*
844 * and set the speed of the serial port
845 */
846 spin_unlock_irqrestore(&info->slock, flags);
847 mxser_change_speed(info, NULL);
848
849 info->flags |= ASYNC_INITIALIZED;
850 return 0;
851}
852
853/*
854 * This routine will shutdown a serial port; interrupts maybe disabled, and
855 * DTR is dropped if the hangup on close termio flag is on.
856 */
857static void mxser_shutdown(struct mxser_port *info)
858{
859 unsigned long flags;
860
861 if (!(info->flags & ASYNC_INITIALIZED))
862 return;
863
864 spin_lock_irqsave(&info->slock, flags);
865
866 /*
867 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
868 * here so the queue might never be waken up
869 */
870 wake_up_interruptible(&info->delta_msr_wait);
871
872 /*
873 * Free the IRQ, if necessary
874 */
875 if (info->xmit_buf) {
876 free_page((unsigned long) info->xmit_buf);
877 info->xmit_buf = NULL;
878 }
879
880 info->IER = 0;
881 outb(0x00, info->ioaddr + UART_IER);
882
883 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
884 info->MCR &= ~(UART_MCR_DTR | UART_MCR_RTS);
885 outb(info->MCR, info->ioaddr + UART_MCR);
886
887 /* clear Rx/Tx FIFO's */
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800888 if (info->board->chip_flag)
889 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT |
890 MOXA_MUST_FCR_GDA_MODE_ENABLE,
891 info->ioaddr + UART_FCR);
892 else
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800893 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
894 info->ioaddr + UART_FCR);
895
896 /* read data port to reset things */
897 (void) inb(info->ioaddr + UART_RX);
898
899 if (info->tty)
900 set_bit(TTY_IO_ERROR, &info->tty->flags);
901
902 info->flags &= ~ASYNC_INITIALIZED;
903
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800904 if (info->board->chip_flag)
905 SET_MOXA_MUST_NO_SOFTWARE_FLOW_CONTROL(info->ioaddr);
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800906
907 spin_unlock_irqrestore(&info->slock, flags);
908}
909
Jiri Slaby037ad482006-12-08 02:38:11 -0800910/*
911 * This routine is called whenever a serial port is opened. It
912 * enables interrupts for a serial port, linking in its async structure into
913 * the IRQ chain. It also performs the serial-specific
914 * initialization for the tty structure.
915 */
916static int mxser_open(struct tty_struct *tty, struct file *filp)
917{
Jiri Slaby55b307d2006-12-08 02:38:14 -0800918 struct mxser_port *info;
Jiri Slaby037ad482006-12-08 02:38:11 -0800919 int retval, line;
920
921 /* initialize driver_data in case something fails */
922 tty->driver_data = NULL;
923
924 line = tty->index;
925 if (line == MXSER_PORTS)
926 return 0;
927 if (line < 0 || line > MXSER_PORTS)
928 return -ENODEV;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800929 info = &mxser_boards[line / MXSER_PORTS_PER_BOARD].ports[line % MXSER_PORTS_PER_BOARD];
930 if (!info->ioaddr)
Jiri Slaby037ad482006-12-08 02:38:11 -0800931 return -ENODEV;
932
933 tty->driver_data = info;
934 info->tty = tty;
935 /*
936 * Start up serial port
937 */
Jiri Slaby3306ce32006-12-08 02:38:13 -0800938 info->count++;
Jiri Slaby037ad482006-12-08 02:38:11 -0800939 retval = mxser_startup(info);
940 if (retval)
941 return retval;
942
943 retval = mxser_block_til_ready(tty, filp, info);
944 if (retval)
945 return retval;
946
Jiri Slaby037ad482006-12-08 02:38:11 -0800947 if ((info->count == 1) && (info->flags & ASYNC_SPLIT_TERMIOS)) {
948 if (tty->driver->subtype == SERIAL_TYPE_NORMAL)
949 *tty->termios = info->normal_termios;
950 else
951 *tty->termios = info->callout_termios;
952 mxser_change_speed(info, NULL);
953 }
954
Andrew Morton08a4ae42006-12-08 02:38:12 -0800955 info->session = process_session(current);
Jiri Slaby037ad482006-12-08 02:38:11 -0800956 info->pgrp = process_group(current);
957
Jiri Slabye079f492006-12-08 02:38:31 -0800958 /* unmark here for very high baud rate (ex. 921600 bps) used */
Jiri Slaby037ad482006-12-08 02:38:11 -0800959 tty->low_latency = 1;
960 return 0;
961}
962
963/*
964 * This routine is called when the serial port gets closed. First, we
965 * wait for the last remaining data to be sent. Then, we unlink its
966 * async structure from the interrupt chain if necessary, and we free
967 * that IRQ if nothing is left in the chain.
968 */
969static void mxser_close(struct tty_struct *tty, struct file *filp)
970{
Jiri Slaby55b307d2006-12-08 02:38:14 -0800971 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -0800972
973 unsigned long timeout;
974 unsigned long flags;
Jiri Slaby037ad482006-12-08 02:38:11 -0800975
976 if (tty->index == MXSER_PORTS)
977 return;
978 if (!info)
979 return;
980
981 spin_lock_irqsave(&info->slock, flags);
982
983 if (tty_hung_up_p(filp)) {
984 spin_unlock_irqrestore(&info->slock, flags);
985 return;
986 }
987 if ((tty->count == 1) && (info->count != 1)) {
988 /*
989 * Uh, oh. tty->count is 1, which means that the tty
990 * structure will be freed. Info->count should always
991 * be one in these conditions. If it's greater than
992 * one, we've got real problems, since it means the
993 * serial port won't be shutdown.
994 */
995 printk(KERN_ERR "mxser_close: bad serial port count; "
996 "tty->count is 1, info->count is %d\n", info->count);
997 info->count = 1;
998 }
999 if (--info->count < 0) {
1000 printk(KERN_ERR "mxser_close: bad serial port count for "
Jiri Slaby55b307d2006-12-08 02:38:14 -08001001 "ttys%d: %d\n", tty->index, info->count);
Jiri Slaby037ad482006-12-08 02:38:11 -08001002 info->count = 0;
1003 }
1004 if (info->count) {
1005 spin_unlock_irqrestore(&info->slock, flags);
1006 return;
1007 }
1008 info->flags |= ASYNC_CLOSING;
1009 spin_unlock_irqrestore(&info->slock, flags);
1010 /*
1011 * Save the termios structure, since this port may have
1012 * separate termios for callout and dialin.
1013 */
1014 if (info->flags & ASYNC_NORMAL_ACTIVE)
1015 info->normal_termios = *tty->termios;
1016 /*
1017 * Now we wait for the transmit buffer to clear; and we notify
1018 * the line discipline to only process XON/XOFF characters.
1019 */
1020 tty->closing = 1;
1021 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1022 tty_wait_until_sent(tty, info->closing_wait);
1023 /*
1024 * At this point we stop accepting input. To do this, we
1025 * disable the receive line status interrupts, and tell the
1026 * interrupt driver to stop checking the data ready bit in the
1027 * line status register.
1028 */
1029 info->IER &= ~UART_IER_RLSI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001030 if (info->board->chip_flag)
Jiri Slaby037ad482006-12-08 02:38:11 -08001031 info->IER &= ~MOXA_MUST_RECV_ISR;
Jiri Slabye079f492006-12-08 02:38:31 -08001032
Jiri Slaby037ad482006-12-08 02:38:11 -08001033 if (info->flags & ASYNC_INITIALIZED) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08001034 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001035 /*
1036 * Before we drop DTR, make sure the UART transmitter
1037 * has completely drained; this is especially
1038 * important if there is a transmit FIFO!
1039 */
1040 timeout = jiffies + HZ;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001041 while (!(inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001042 schedule_timeout_interruptible(5);
1043 if (time_after(jiffies, timeout))
1044 break;
1045 }
1046 }
1047 mxser_shutdown(info);
1048
1049 if (tty->driver->flush_buffer)
1050 tty->driver->flush_buffer(tty);
1051
Jiri Slaby7e8bcf92006-12-08 02:38:24 -08001052 tty_ldisc_flush(tty);
Jiri Slaby037ad482006-12-08 02:38:11 -08001053
1054 tty->closing = 0;
1055 info->event = 0;
1056 info->tty = NULL;
1057 if (info->blocked_open) {
1058 if (info->close_delay)
1059 schedule_timeout_interruptible(info->close_delay);
1060 wake_up_interruptible(&info->open_wait);
1061 }
1062
1063 info->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
1064 wake_up_interruptible(&info->close_wait);
1065
1066}
1067
1068static int mxser_write(struct tty_struct *tty, const unsigned char *buf, int count)
1069{
1070 int c, total = 0;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001071 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001072 unsigned long flags;
1073
1074 if (!info->xmit_buf)
1075 return 0;
1076
1077 while (1) {
1078 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1079 SERIAL_XMIT_SIZE - info->xmit_head));
1080 if (c <= 0)
1081 break;
1082
1083 memcpy(info->xmit_buf + info->xmit_head, buf, c);
1084 spin_lock_irqsave(&info->slock, flags);
1085 info->xmit_head = (info->xmit_head + c) &
1086 (SERIAL_XMIT_SIZE - 1);
1087 info->xmit_cnt += c;
1088 spin_unlock_irqrestore(&info->slock, flags);
1089
1090 buf += c;
1091 count -= c;
1092 total += c;
1093 }
1094
Jiri Slabye079f492006-12-08 02:38:31 -08001095 if (info->xmit_cnt && !tty->stopped) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001096 if (!tty->hw_stopped ||
1097 (info->type == PORT_16550A) ||
Jiri Slaby55b307d2006-12-08 02:38:14 -08001098 (info->board->chip_flag)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001099 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby55b307d2006-12-08 02:38:14 -08001100 outb(info->IER & ~UART_IER_THRI, info->ioaddr +
1101 UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001102 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001103 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001104 spin_unlock_irqrestore(&info->slock, flags);
1105 }
1106 }
1107 return total;
1108}
1109
1110static void mxser_put_char(struct tty_struct *tty, unsigned char ch)
1111{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001112 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001113 unsigned long flags;
1114
1115 if (!info->xmit_buf)
1116 return;
1117
1118 if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1)
1119 return;
1120
1121 spin_lock_irqsave(&info->slock, flags);
1122 info->xmit_buf[info->xmit_head++] = ch;
1123 info->xmit_head &= SERIAL_XMIT_SIZE - 1;
1124 info->xmit_cnt++;
1125 spin_unlock_irqrestore(&info->slock, flags);
Jiri Slabye079f492006-12-08 02:38:31 -08001126 if (!tty->stopped) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001127 if (!tty->hw_stopped ||
1128 (info->type == PORT_16550A) ||
Jiri Slaby55b307d2006-12-08 02:38:14 -08001129 info->board->chip_flag) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001130 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby55b307d2006-12-08 02:38:14 -08001131 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001132 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001133 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001134 spin_unlock_irqrestore(&info->slock, flags);
1135 }
1136 }
1137}
1138
1139
1140static void mxser_flush_chars(struct tty_struct *tty)
1141{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001142 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001143 unsigned long flags;
1144
1145 if (info->xmit_cnt <= 0 ||
1146 tty->stopped ||
1147 !info->xmit_buf ||
1148 (tty->hw_stopped &&
1149 (info->type != PORT_16550A) &&
Jiri Slaby55b307d2006-12-08 02:38:14 -08001150 (!info->board->chip_flag)
Jiri Slaby037ad482006-12-08 02:38:11 -08001151 ))
1152 return;
1153
1154 spin_lock_irqsave(&info->slock, flags);
1155
Jiri Slaby55b307d2006-12-08 02:38:14 -08001156 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001157 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001158 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001159
1160 spin_unlock_irqrestore(&info->slock, flags);
1161}
1162
1163static int mxser_write_room(struct tty_struct *tty)
1164{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001165 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001166 int ret;
1167
1168 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1169 if (ret < 0)
1170 ret = 0;
1171 return ret;
1172}
1173
1174static int mxser_chars_in_buffer(struct tty_struct *tty)
1175{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001176 struct mxser_port *info = tty->driver_data;
Jiri Slaby925e9c12006-12-08 02:38:30 -08001177 return info->xmit_cnt;
Jiri Slaby037ad482006-12-08 02:38:11 -08001178}
1179
1180static void mxser_flush_buffer(struct tty_struct *tty)
1181{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001182 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001183 char fcr;
1184 unsigned long flags;
1185
1186
1187 spin_lock_irqsave(&info->slock, flags);
1188 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1189
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);
Jiri Slaby037ad482006-12-08 02:38:11 -08001196
Jiri Slaby7e8bcf92006-12-08 02:38:24 -08001197 tty_wakeup(tty);
Jiri Slaby037ad482006-12-08 02:38:11 -08001198}
1199
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001200/*
1201 * ------------------------------------------------------------
1202 * friends of mxser_ioctl()
1203 * ------------------------------------------------------------
1204 */
1205static int mxser_get_serial_info(struct mxser_port *info,
1206 struct serial_struct __user *retinfo)
1207{
1208 struct serial_struct tmp;
1209
1210 if (!retinfo)
1211 return -EFAULT;
1212 memset(&tmp, 0, sizeof(tmp));
1213 tmp.type = info->type;
1214 tmp.line = info->tty->index;
1215 tmp.port = info->ioaddr;
1216 tmp.irq = info->board->irq;
1217 tmp.flags = info->flags;
1218 tmp.baud_base = info->baud_base;
1219 tmp.close_delay = info->close_delay;
1220 tmp.closing_wait = info->closing_wait;
1221 tmp.custom_divisor = info->custom_divisor;
1222 tmp.hub6 = 0;
1223 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1224 return -EFAULT;
1225 return 0;
1226}
1227
1228static int mxser_set_serial_info(struct mxser_port *info,
1229 struct serial_struct __user *new_info)
1230{
1231 struct serial_struct new_serial;
1232 unsigned int flags;
1233 int retval = 0;
1234
1235 if (!new_info || !info->ioaddr)
1236 return -EFAULT;
1237 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
1238 return -EFAULT;
1239
1240 if ((new_serial.irq != info->board->irq) ||
1241 (new_serial.port != info->ioaddr) ||
1242 (new_serial.custom_divisor != info->custom_divisor) ||
1243 (new_serial.baud_base != info->baud_base))
1244 return -EPERM;
1245
1246 flags = info->flags & ASYNC_SPD_MASK;
1247
1248 if (!capable(CAP_SYS_ADMIN)) {
1249 if ((new_serial.baud_base != info->baud_base) ||
1250 (new_serial.close_delay != info->close_delay) ||
1251 ((new_serial.flags & ~ASYNC_USR_MASK) != (info->flags & ~ASYNC_USR_MASK)))
1252 return -EPERM;
1253 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
1254 (new_serial.flags & ASYNC_USR_MASK));
1255 } else {
1256 /*
1257 * OK, past this point, all the error checking has been done.
1258 * At this point, we start making changes.....
1259 */
1260 info->flags = ((info->flags & ~ASYNC_FLAGS) |
1261 (new_serial.flags & ASYNC_FLAGS));
1262 info->close_delay = new_serial.close_delay * HZ / 100;
1263 info->closing_wait = new_serial.closing_wait * HZ / 100;
1264 info->tty->low_latency =
1265 (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
Jiri Slabye079f492006-12-08 02:38:31 -08001266 info->tty->low_latency = 0;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001267 }
1268
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001269 info->type = new_serial.type;
1270
1271 process_txrx_fifo(info);
1272
1273 if (info->flags & ASYNC_INITIALIZED) {
1274 if (flags != (info->flags & ASYNC_SPD_MASK))
1275 mxser_change_speed(info, NULL);
1276 } else
1277 retval = mxser_startup(info);
1278
1279 return retval;
1280}
1281
1282/*
1283 * mxser_get_lsr_info - get line status register info
1284 *
1285 * Purpose: Let user call ioctl() to get info when the UART physically
1286 * is emptied. On bus types like RS485, the transmitter must
1287 * release the bus after transmitting. This must be done when
1288 * the transmit shift register is empty, not be done when the
1289 * transmit holding register is empty. This functionality
1290 * allows an RS485 driver to be written in user space.
1291 */
1292static int mxser_get_lsr_info(struct mxser_port *info,
1293 unsigned int __user *value)
1294{
1295 unsigned char status;
1296 unsigned int result;
1297 unsigned long flags;
1298
1299 spin_lock_irqsave(&info->slock, flags);
1300 status = inb(info->ioaddr + UART_LSR);
1301 spin_unlock_irqrestore(&info->slock, flags);
1302 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1303 return put_user(result, value);
1304}
1305
1306/*
1307 * This routine sends a break character out the serial port.
1308 */
1309static void mxser_send_break(struct mxser_port *info, int duration)
1310{
1311 unsigned long flags;
1312
1313 if (!info->ioaddr)
1314 return;
1315 set_current_state(TASK_INTERRUPTIBLE);
1316 spin_lock_irqsave(&info->slock, flags);
1317 outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
1318 info->ioaddr + UART_LCR);
1319 spin_unlock_irqrestore(&info->slock, flags);
1320 schedule_timeout(duration);
1321 spin_lock_irqsave(&info->slock, flags);
1322 outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
1323 info->ioaddr + UART_LCR);
1324 spin_unlock_irqrestore(&info->slock, flags);
1325}
1326
1327static int mxser_tiocmget(struct tty_struct *tty, struct file *file)
Jiri Slaby037ad482006-12-08 02:38:11 -08001328{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001329 struct mxser_port *info = tty->driver_data;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001330 unsigned char control, status;
Jiri Slaby037ad482006-12-08 02:38:11 -08001331 unsigned long flags;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001332
Jiri Slaby037ad482006-12-08 02:38:11 -08001333
1334 if (tty->index == MXSER_PORTS)
Jiri Slaby037ad482006-12-08 02:38:11 -08001335 return -ENOIOCTLCMD;
Jiri Slaby214efeb2006-12-08 02:38:25 -08001336 if (test_bit(TTY_IO_ERROR, &tty->flags))
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001337 return -EIO;
1338
1339 control = info->MCR;
1340
1341 spin_lock_irqsave(&info->slock, flags);
1342 status = inb(info->ioaddr + UART_MSR);
1343 if (status & UART_MSR_ANY_DELTA)
1344 mxser_check_modem_status(info, status);
1345 spin_unlock_irqrestore(&info->slock, flags);
1346 return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0) |
1347 ((control & UART_MCR_DTR) ? TIOCM_DTR : 0) |
1348 ((status & UART_MSR_DCD) ? TIOCM_CAR : 0) |
1349 ((status & UART_MSR_RI) ? TIOCM_RNG : 0) |
1350 ((status & UART_MSR_DSR) ? TIOCM_DSR : 0) |
1351 ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
1352}
1353
1354static int mxser_tiocmset(struct tty_struct *tty, struct file *file,
1355 unsigned int set, unsigned int clear)
1356{
1357 struct mxser_port *info = tty->driver_data;
1358 unsigned long flags;
1359
1360
1361 if (tty->index == MXSER_PORTS)
1362 return -ENOIOCTLCMD;
Jiri Slaby214efeb2006-12-08 02:38:25 -08001363 if (test_bit(TTY_IO_ERROR, &tty->flags))
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001364 return -EIO;
1365
1366 spin_lock_irqsave(&info->slock, flags);
1367
1368 if (set & TIOCM_RTS)
1369 info->MCR |= UART_MCR_RTS;
1370 if (set & TIOCM_DTR)
1371 info->MCR |= UART_MCR_DTR;
1372
1373 if (clear & TIOCM_RTS)
1374 info->MCR &= ~UART_MCR_RTS;
1375 if (clear & TIOCM_DTR)
1376 info->MCR &= ~UART_MCR_DTR;
1377
1378 outb(info->MCR, info->ioaddr + UART_MCR);
1379 spin_unlock_irqrestore(&info->slock, flags);
Jiri Slaby037ad482006-12-08 02:38:11 -08001380 return 0;
1381}
1382
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001383static int mxser_program_mode(int port)
1384{
1385 int id, i, j, n;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001386
1387 spin_lock(&gm_lock);
1388 outb(0, port);
1389 outb(0, port);
1390 outb(0, port);
1391 (void)inb(port);
1392 (void)inb(port);
1393 outb(0, port);
1394 (void)inb(port);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001395 spin_unlock(&gm_lock);
1396
1397 id = inb(port + 1) & 0x1F;
1398 if ((id != C168_ASIC_ID) &&
1399 (id != C104_ASIC_ID) &&
1400 (id != C102_ASIC_ID) &&
1401 (id != CI132_ASIC_ID) &&
1402 (id != CI134_ASIC_ID) &&
1403 (id != CI104J_ASIC_ID))
1404 return -1;
1405 for (i = 0, j = 0; i < 4; i++) {
1406 n = inb(port + 2);
1407 if (n == 'M') {
1408 j = 1;
1409 } else if ((j == 1) && (n == 1)) {
1410 j = 2;
1411 break;
1412 } else
1413 j = 0;
1414 }
1415 if (j != 2)
1416 id = -2;
1417 return id;
1418}
1419
1420static void mxser_normal_mode(int port)
1421{
1422 int i, n;
1423
1424 outb(0xA5, port + 1);
1425 outb(0x80, port + 3);
1426 outb(12, port + 0); /* 9600 bps */
1427 outb(0, port + 1);
1428 outb(0x03, port + 3); /* 8 data bits */
1429 outb(0x13, port + 4); /* loop back mode */
1430 for (i = 0; i < 16; i++) {
1431 n = inb(port + 5);
1432 if ((n & 0x61) == 0x60)
1433 break;
1434 if ((n & 1) == 1)
1435 (void)inb(port);
1436 }
1437 outb(0x00, port + 4);
1438}
1439
1440#define CHIP_SK 0x01 /* Serial Data Clock in Eprom */
1441#define CHIP_DO 0x02 /* Serial Data Output in Eprom */
1442#define CHIP_CS 0x04 /* Serial Chip Select in Eprom */
1443#define CHIP_DI 0x08 /* Serial Data Input in Eprom */
1444#define EN_CCMD 0x000 /* Chip's command register */
1445#define EN0_RSARLO 0x008 /* Remote start address reg 0 */
1446#define EN0_RSARHI 0x009 /* Remote start address reg 1 */
1447#define EN0_RCNTLO 0x00A /* Remote byte count reg WR */
1448#define EN0_RCNTHI 0x00B /* Remote byte count reg WR */
1449#define EN0_DCFG 0x00E /* Data configuration reg WR */
1450#define EN0_PORT 0x010 /* Rcv missed frame error counter RD */
1451#define ENC_PAGE0 0x000 /* Select page 0 of chip registers */
1452#define ENC_PAGE3 0x0C0 /* Select page 3 of chip registers */
1453static int mxser_read_register(int port, unsigned short *regs)
1454{
1455 int i, k, value, id;
1456 unsigned int j;
1457
1458 id = mxser_program_mode(port);
1459 if (id < 0)
1460 return id;
1461 for (i = 0; i < 14; i++) {
1462 k = (i & 0x3F) | 0x180;
1463 for (j = 0x100; j > 0; j >>= 1) {
1464 outb(CHIP_CS, port);
1465 if (k & j) {
1466 outb(CHIP_CS | CHIP_DO, port);
1467 outb(CHIP_CS | CHIP_DO | CHIP_SK, port); /* A? bit of read */
1468 } else {
1469 outb(CHIP_CS, port);
1470 outb(CHIP_CS | CHIP_SK, port); /* A? bit of read */
1471 }
1472 }
1473 (void)inb(port);
1474 value = 0;
1475 for (k = 0, j = 0x8000; k < 16; k++, j >>= 1) {
1476 outb(CHIP_CS, port);
1477 outb(CHIP_CS | CHIP_SK, port);
1478 if (inb(port) & CHIP_DI)
1479 value |= j;
1480 }
1481 regs[i] = value;
1482 outb(0, port);
1483 }
1484 mxser_normal_mode(port);
1485 return id;
1486}
1487
Jiri Slaby037ad482006-12-08 02:38:11 -08001488static int mxser_ioctl_special(unsigned int cmd, void __user *argp)
1489{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001490 struct mxser_port *port;
1491 int result, status;
1492 unsigned int i, j;
Jiri Slaby037ad482006-12-08 02:38:11 -08001493
1494 switch (cmd) {
1495 case MOXA_GET_CONF:
Jiri Slaby55b307d2006-12-08 02:38:14 -08001496/* if (copy_to_user(argp, mxsercfg,
Jiri Slaby037ad482006-12-08 02:38:11 -08001497 sizeof(struct mxser_hwconf) * 4))
1498 return -EFAULT;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001499 return 0;*/
1500 return -ENXIO;
Jiri Slaby037ad482006-12-08 02:38:11 -08001501 case MOXA_GET_MAJOR:
1502 if (copy_to_user(argp, &ttymajor, sizeof(int)))
1503 return -EFAULT;
1504 return 0;
1505
1506 case MOXA_GET_CUMAJOR:
1507 if (copy_to_user(argp, &calloutmajor, sizeof(int)))
1508 return -EFAULT;
1509 return 0;
1510
1511 case MOXA_CHKPORTENABLE:
1512 result = 0;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001513
1514 for (i = 0; i < MXSER_BOARDS; i++)
1515 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++)
1516 if (mxser_boards[i].ports[j].ioaddr)
1517 result |= (1 << i);
1518
Jiri Slaby037ad482006-12-08 02:38:11 -08001519 return put_user(result, (unsigned long __user *)argp);
1520 case MOXA_GETDATACOUNT:
1521 if (copy_to_user(argp, &mxvar_log, sizeof(mxvar_log)))
1522 return -EFAULT;
1523 return 0;
1524 case MOXA_GETMSTATUS:
Jiri Slaby55b307d2006-12-08 02:38:14 -08001525 for (i = 0; i < MXSER_BOARDS; i++)
1526 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
1527 port = &mxser_boards[i].ports[j];
1528
1529 GMStatus[i].ri = 0;
1530 if (!port->ioaddr) {
1531 GMStatus[i].dcd = 0;
1532 GMStatus[i].dsr = 0;
1533 GMStatus[i].cts = 0;
1534 continue;
1535 }
1536
1537 if (!port->tty || !port->tty->termios)
1538 GMStatus[i].cflag =
1539 port->normal_termios.c_cflag;
1540 else
1541 GMStatus[i].cflag =
1542 port->tty->termios->c_cflag;
1543
1544 status = inb(port->ioaddr + UART_MSR);
1545 if (status & 0x80 /*UART_MSR_DCD */ )
1546 GMStatus[i].dcd = 1;
1547 else
1548 GMStatus[i].dcd = 0;
1549
1550 if (status & 0x20 /*UART_MSR_DSR */ )
1551 GMStatus[i].dsr = 1;
1552 else
1553 GMStatus[i].dsr = 0;
1554
1555
1556 if (status & 0x10 /*UART_MSR_CTS */ )
1557 GMStatus[i].cts = 1;
1558 else
1559 GMStatus[i].cts = 0;
Jiri Slaby037ad482006-12-08 02:38:11 -08001560 }
Jiri Slaby037ad482006-12-08 02:38:11 -08001561 if (copy_to_user(argp, GMStatus,
1562 sizeof(struct mxser_mstatus) * MXSER_PORTS))
1563 return -EFAULT;
1564 return 0;
1565 case MOXA_ASPP_MON_EXT: {
Jiri Slaby55b307d2006-12-08 02:38:14 -08001566 int status, p, shiftbit;
1567 unsigned long opmode;
1568 unsigned cflag, iflag;
Jiri Slaby037ad482006-12-08 02:38:11 -08001569
Jiri Slaby55b307d2006-12-08 02:38:14 -08001570 for (i = 0; i < MXSER_BOARDS; i++)
1571 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
1572 port = &mxser_boards[i].ports[j];
1573 if (!port->ioaddr)
Jiri Slaby037ad482006-12-08 02:38:11 -08001574 continue;
1575
Jiri Slaby55b307d2006-12-08 02:38:14 -08001576 status = mxser_get_msr(port->ioaddr, 0, i);
Jiri Slaby55b307d2006-12-08 02:38:14 -08001577
Jiri Slaby037ad482006-12-08 02:38:11 -08001578 if (status & UART_MSR_TERI)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001579 port->icount.rng++;
Jiri Slaby037ad482006-12-08 02:38:11 -08001580 if (status & UART_MSR_DDSR)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001581 port->icount.dsr++;
Jiri Slaby037ad482006-12-08 02:38:11 -08001582 if (status & UART_MSR_DDCD)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001583 port->icount.dcd++;
Jiri Slaby037ad482006-12-08 02:38:11 -08001584 if (status & UART_MSR_DCTS)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001585 port->icount.cts++;
Jiri Slaby037ad482006-12-08 02:38:11 -08001586
Jiri Slaby55b307d2006-12-08 02:38:14 -08001587 port->mon_data.modem_status = status;
1588 mon_data_ext.rx_cnt[i] = port->mon_data.rxcnt;
1589 mon_data_ext.tx_cnt[i] = port->mon_data.txcnt;
1590 mon_data_ext.up_rxcnt[i] =
1591 port->mon_data.up_rxcnt;
1592 mon_data_ext.up_txcnt[i] =
1593 port->mon_data.up_txcnt;
1594 mon_data_ext.modem_status[i] =
1595 port->mon_data.modem_status;
1596 mon_data_ext.baudrate[i] = port->realbaud;
Jiri Slaby037ad482006-12-08 02:38:11 -08001597
Jiri Slaby55b307d2006-12-08 02:38:14 -08001598 if (!port->tty || !port->tty->termios) {
1599 cflag = port->normal_termios.c_cflag;
1600 iflag = port->normal_termios.c_iflag;
Jiri Slaby037ad482006-12-08 02:38:11 -08001601 } else {
Jiri Slaby55b307d2006-12-08 02:38:14 -08001602 cflag = port->tty->termios->c_cflag;
1603 iflag = port->tty->termios->c_iflag;
Jiri Slaby037ad482006-12-08 02:38:11 -08001604 }
1605
1606 mon_data_ext.databits[i] = cflag & CSIZE;
1607
1608 mon_data_ext.stopbits[i] = cflag & CSTOPB;
1609
Jiri Slaby55b307d2006-12-08 02:38:14 -08001610 mon_data_ext.parity[i] =
1611 cflag & (PARENB | PARODD | CMSPAR);
Jiri Slaby037ad482006-12-08 02:38:11 -08001612
1613 mon_data_ext.flowctrl[i] = 0x00;
1614
1615 if (cflag & CRTSCTS)
1616 mon_data_ext.flowctrl[i] |= 0x03;
1617
1618 if (iflag & (IXON | IXOFF))
1619 mon_data_ext.flowctrl[i] |= 0x0C;
1620
Jiri Slaby55b307d2006-12-08 02:38:14 -08001621 if (port->type == PORT_16550A)
Jiri Slaby037ad482006-12-08 02:38:11 -08001622 mon_data_ext.fifo[i] = 1;
1623 else
1624 mon_data_ext.fifo[i] = 0;
1625
1626 p = i % 4;
1627 shiftbit = p * 2;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001628 opmode = inb(port->opmode_ioaddr) >> shiftbit;
Jiri Slaby037ad482006-12-08 02:38:11 -08001629 opmode &= OP_MODE_MASK;
1630
1631 mon_data_ext.iftype[i] = opmode;
1632
1633 }
Jiri Slaby55b307d2006-12-08 02:38:14 -08001634 if (copy_to_user(argp, &mon_data_ext,
1635 sizeof(mon_data_ext)))
Jiri Slaby037ad482006-12-08 02:38:11 -08001636 return -EFAULT;
1637
1638 return 0;
1639
Jiri Slaby55b307d2006-12-08 02:38:14 -08001640 } default:
Jiri Slaby037ad482006-12-08 02:38:11 -08001641 return -ENOIOCTLCMD;
1642 }
1643 return 0;
1644}
1645
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001646static int mxser_ioctl(struct tty_struct *tty, struct file *file,
1647 unsigned int cmd, unsigned long arg)
1648{
1649 struct mxser_port *info = tty->driver_data;
1650 struct async_icount cprev, cnow; /* kernel counter temps */
1651 struct serial_icounter_struct __user *p_cuser;
1652 unsigned long templ;
1653 unsigned long flags;
1654 void __user *argp = (void __user *)arg;
1655 int retval;
1656
1657 if (tty->index == MXSER_PORTS)
1658 return mxser_ioctl_special(cmd, argp);
1659
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001660 if (cmd == MOXA_SET_OP_MODE || cmd == MOXA_GET_OP_MODE) {
1661 int p;
1662 unsigned long opmode;
1663 static unsigned char ModeMask[] = { 0xfc, 0xf3, 0xcf, 0x3f };
1664 int shiftbit;
1665 unsigned char val, mask;
1666
1667 p = tty->index % 4;
1668 if (cmd == MOXA_SET_OP_MODE) {
1669 if (get_user(opmode, (int __user *) argp))
1670 return -EFAULT;
1671 if (opmode != RS232_MODE &&
1672 opmode != RS485_2WIRE_MODE &&
1673 opmode != RS422_MODE &&
1674 opmode != RS485_4WIRE_MODE)
1675 return -EFAULT;
1676 mask = ModeMask[p];
1677 shiftbit = p * 2;
1678 val = inb(info->opmode_ioaddr);
1679 val &= mask;
1680 val |= (opmode << shiftbit);
1681 outb(val, info->opmode_ioaddr);
1682 } else {
1683 shiftbit = p * 2;
1684 opmode = inb(info->opmode_ioaddr) >> shiftbit;
1685 opmode &= OP_MODE_MASK;
1686 if (copy_to_user(argp, &opmode, sizeof(int)))
1687 return -EFAULT;
1688 }
1689 return 0;
1690 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001691
Jiri Slaby214efeb2006-12-08 02:38:25 -08001692 if (cmd != TIOCGSERIAL && cmd != TIOCMIWAIT && cmd != TIOCGICOUNT &&
1693 test_bit(TTY_IO_ERROR, &tty->flags))
1694 return -EIO;
1695
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001696 switch (cmd) {
1697 case TCSBRK: /* SVID version: non-zero arg --> no break */
1698 retval = tty_check_change(tty);
1699 if (retval)
1700 return retval;
1701 tty_wait_until_sent(tty, 0);
1702 if (!arg)
1703 mxser_send_break(info, HZ / 4); /* 1/4 second */
1704 return 0;
1705 case TCSBRKP: /* support for POSIX tcsendbreak() */
1706 retval = tty_check_change(tty);
1707 if (retval)
1708 return retval;
1709 tty_wait_until_sent(tty, 0);
1710 mxser_send_break(info, arg ? arg * (HZ / 10) : HZ / 4);
1711 return 0;
1712 case TIOCGSOFTCAR:
1713 return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *)argp);
1714 case TIOCSSOFTCAR:
1715 if (get_user(templ, (unsigned long __user *) argp))
1716 return -EFAULT;
1717 arg = templ;
1718 tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) | (arg ? CLOCAL : 0));
1719 return 0;
1720 case TIOCGSERIAL:
1721 return mxser_get_serial_info(info, argp);
1722 case TIOCSSERIAL:
1723 return mxser_set_serial_info(info, argp);
1724 case TIOCSERGETLSR: /* Get line status register */
1725 return mxser_get_lsr_info(info, argp);
1726 /*
1727 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1728 * - mask passed in arg for lines of interest
1729 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1730 * Caller should use TIOCGICOUNT to see which one it was
1731 */
1732 case TIOCMIWAIT: {
1733 DECLARE_WAITQUEUE(wait, current);
1734 int ret;
1735 spin_lock_irqsave(&info->slock, flags);
1736 cprev = info->icount; /* note the counters on entry */
1737 spin_unlock_irqrestore(&info->slock, flags);
1738
1739 add_wait_queue(&info->delta_msr_wait, &wait);
1740 while (1) {
1741 spin_lock_irqsave(&info->slock, flags);
1742 cnow = info->icount; /* atomic copy */
1743 spin_unlock_irqrestore(&info->slock, flags);
1744
1745 set_current_state(TASK_INTERRUPTIBLE);
1746 if (((arg & TIOCM_RNG) &&
1747 (cnow.rng != cprev.rng)) ||
1748 ((arg & TIOCM_DSR) &&
1749 (cnow.dsr != cprev.dsr)) ||
1750 ((arg & TIOCM_CD) &&
1751 (cnow.dcd != cprev.dcd)) ||
1752 ((arg & TIOCM_CTS) &&
1753 (cnow.cts != cprev.cts))) {
1754 ret = 0;
1755 break;
1756 }
1757 /* see if a signal did it */
1758 if (signal_pending(current)) {
1759 ret = -ERESTARTSYS;
1760 break;
1761 }
1762 cprev = cnow;
1763 }
1764 current->state = TASK_RUNNING;
1765 remove_wait_queue(&info->delta_msr_wait, &wait);
1766 break;
1767 }
1768 /* NOTREACHED */
1769 /*
1770 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1771 * Return: write counters to the user passed counter struct
1772 * NB: both 1->0 and 0->1 transitions are counted except for
1773 * RI where only 0->1 is counted.
1774 */
1775 case TIOCGICOUNT:
1776 spin_lock_irqsave(&info->slock, flags);
1777 cnow = info->icount;
1778 spin_unlock_irqrestore(&info->slock, flags);
1779 p_cuser = argp;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001780 if (put_user(cnow.frame, &p_cuser->frame))
1781 return -EFAULT;
1782 if (put_user(cnow.brk, &p_cuser->brk))
1783 return -EFAULT;
1784 if (put_user(cnow.overrun, &p_cuser->overrun))
1785 return -EFAULT;
1786 if (put_user(cnow.buf_overrun, &p_cuser->buf_overrun))
1787 return -EFAULT;
1788 if (put_user(cnow.parity, &p_cuser->parity))
1789 return -EFAULT;
1790 if (put_user(cnow.rx, &p_cuser->rx))
1791 return -EFAULT;
1792 if (put_user(cnow.tx, &p_cuser->tx))
1793 return -EFAULT;
1794 put_user(cnow.cts, &p_cuser->cts);
1795 put_user(cnow.dsr, &p_cuser->dsr);
1796 put_user(cnow.rng, &p_cuser->rng);
1797 put_user(cnow.dcd, &p_cuser->dcd);
1798 return 0;
1799 case MOXA_HighSpeedOn:
1800 return put_user(info->baud_base != 115200 ? 1 : 0, (int __user *)argp);
1801 case MOXA_SDS_RSTICOUNTER:
1802 info->mon_data.rxcnt = 0;
1803 info->mon_data.txcnt = 0;
1804 return 0;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001805 case MOXA_ASPP_SETBAUD:{
1806 long baud;
1807 if (get_user(baud, (long __user *)argp))
1808 return -EFAULT;
1809 mxser_set_baud(info, baud);
1810 return 0;
1811 }
1812 case MOXA_ASPP_GETBAUD:
1813 if (copy_to_user(argp, &info->realbaud, sizeof(long)))
1814 return -EFAULT;
1815
1816 return 0;
1817
1818 case MOXA_ASPP_OQUEUE:{
1819 int len, lsr;
1820
1821 len = mxser_chars_in_buffer(tty);
1822
1823 lsr = inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT;
1824
1825 len += (lsr ? 0 : 1);
1826
1827 if (copy_to_user(argp, &len, sizeof(int)))
1828 return -EFAULT;
1829
1830 return 0;
1831 }
1832 case MOXA_ASPP_MON: {
1833 int mcr, status;
1834
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001835 status = mxser_get_msr(info->ioaddr, 1, tty->index);
1836 mxser_check_modem_status(info, status);
1837
1838 mcr = inb(info->ioaddr + UART_MCR);
1839 if (mcr & MOXA_MUST_MCR_XON_FLAG)
1840 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFHOLD;
1841 else
1842 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFHOLD;
1843
1844 if (mcr & MOXA_MUST_MCR_TX_XON)
1845 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFXENT;
1846 else
1847 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFXENT;
1848
1849 if (info->tty->hw_stopped)
1850 info->mon_data.hold_reason |= NPPI_NOTIFY_CTSHOLD;
1851 else
1852 info->mon_data.hold_reason &= ~NPPI_NOTIFY_CTSHOLD;
1853
1854 if (copy_to_user(argp, &info->mon_data,
1855 sizeof(struct mxser_mon)))
1856 return -EFAULT;
1857
1858 return 0;
1859 }
1860 case MOXA_ASPP_LSTATUS: {
1861 if (copy_to_user(argp, &info->err_shadow,
1862 sizeof(unsigned char)))
1863 return -EFAULT;
1864
1865 info->err_shadow = 0;
1866 return 0;
1867 }
1868 case MOXA_SET_BAUD_METHOD: {
1869 int method;
1870
1871 if (get_user(method, (int __user *)argp))
1872 return -EFAULT;
1873 mxser_set_baud_method[tty->index] = method;
1874 if (copy_to_user(argp, &method, sizeof(int)))
1875 return -EFAULT;
1876
1877 return 0;
1878 }
1879 default:
1880 return -ENOIOCTLCMD;
1881 }
1882 return 0;
1883}
1884
Jiri Slaby037ad482006-12-08 02:38:11 -08001885static void mxser_stoprx(struct tty_struct *tty)
1886{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001887 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001888
1889 info->ldisc_stop_rx = 1;
1890 if (I_IXOFF(tty)) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08001891 if (info->board->chip_flag) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001892 info->IER &= ~MOXA_MUST_RECV_ISR;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001893 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby925e9c12006-12-08 02:38:30 -08001894 } else {
Jiri Slaby037ad482006-12-08 02:38:11 -08001895 info->x_char = STOP_CHAR(tty);
Jiri Slaby55b307d2006-12-08 02:38:14 -08001896 outb(0, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001897 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001898 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001899 }
Jiri Slaby037ad482006-12-08 02:38:11 -08001900 }
1901
1902 if (info->tty->termios->c_cflag & CRTSCTS) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001903 info->MCR &= ~UART_MCR_RTS;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001904 outb(info->MCR, info->ioaddr + UART_MCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08001905 }
1906}
1907
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001908/*
1909 * This routine is called by the upper-layer tty layer to signal that
1910 * incoming characters should be throttled.
1911 */
1912static void mxser_throttle(struct tty_struct *tty)
1913{
1914 mxser_stoprx(tty);
1915}
1916
1917static void mxser_unthrottle(struct tty_struct *tty)
Jiri Slaby037ad482006-12-08 02:38:11 -08001918{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001919 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001920
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001921 /* startrx */
Jiri Slaby037ad482006-12-08 02:38:11 -08001922 info->ldisc_stop_rx = 0;
1923 if (I_IXOFF(tty)) {
1924 if (info->x_char)
1925 info->x_char = 0;
1926 else {
Jiri Slaby55b307d2006-12-08 02:38:14 -08001927 if (info->board->chip_flag) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001928 info->IER |= MOXA_MUST_RECV_ISR;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001929 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby925e9c12006-12-08 02:38:30 -08001930 } else {
Jiri Slaby037ad482006-12-08 02:38:11 -08001931 info->x_char = START_CHAR(tty);
Jiri Slaby55b307d2006-12-08 02:38:14 -08001932 outb(0, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001933 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001934 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001935 }
Jiri Slaby037ad482006-12-08 02:38:11 -08001936 }
1937 }
1938
1939 if (info->tty->termios->c_cflag & CRTSCTS) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001940 info->MCR |= UART_MCR_RTS;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001941 outb(info->MCR, info->ioaddr + UART_MCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08001942 }
1943}
1944
1945/*
Jiri Slaby037ad482006-12-08 02:38:11 -08001946 * mxser_stop() and mxser_start()
1947 *
1948 * This routines are called before setting or resetting tty->stopped.
1949 * They enable or disable transmitter interrupts, as necessary.
1950 */
1951static void mxser_stop(struct tty_struct *tty)
1952{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001953 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001954 unsigned long flags;
1955
1956 spin_lock_irqsave(&info->slock, flags);
1957 if (info->IER & UART_IER_THRI) {
1958 info->IER &= ~UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001959 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001960 }
1961 spin_unlock_irqrestore(&info->slock, flags);
1962}
1963
1964static void mxser_start(struct tty_struct *tty)
1965{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001966 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001967 unsigned long flags;
1968
1969 spin_lock_irqsave(&info->slock, flags);
Jiri Slabye079f492006-12-08 02:38:31 -08001970 if (info->xmit_cnt && info->xmit_buf) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08001971 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001972 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001973 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001974 }
1975 spin_unlock_irqrestore(&info->slock, flags);
1976}
1977
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001978static void mxser_set_termios(struct tty_struct *tty, struct termios *old_termios)
1979{
1980 struct mxser_port *info = tty->driver_data;
1981 unsigned long flags;
1982
1983 if ((tty->termios->c_cflag != old_termios->c_cflag) ||
1984 (RELEVANT_IFLAG(tty->termios->c_iflag) != RELEVANT_IFLAG(old_termios->c_iflag))) {
1985
1986 mxser_change_speed(info, old_termios);
1987
1988 if ((old_termios->c_cflag & CRTSCTS) &&
1989 !(tty->termios->c_cflag & CRTSCTS)) {
1990 tty->hw_stopped = 0;
1991 mxser_start(tty);
1992 }
1993 }
1994
Jiri Slabye079f492006-12-08 02:38:31 -08001995 /* Handle sw stopped */
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001996 if ((old_termios->c_iflag & IXON) &&
1997 !(tty->termios->c_iflag & IXON)) {
1998 tty->stopped = 0;
1999
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002000 if (info->board->chip_flag) {
2001 spin_lock_irqsave(&info->slock, flags);
2002 DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
2003 spin_unlock_irqrestore(&info->slock, flags);
2004 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002005
2006 mxser_start(tty);
2007 }
2008}
2009
Jiri Slaby037ad482006-12-08 02:38:11 -08002010/*
2011 * mxser_wait_until_sent() --- wait until the transmitter is empty
2012 */
2013static void mxser_wait_until_sent(struct tty_struct *tty, int timeout)
2014{
Jiri Slaby55b307d2006-12-08 02:38:14 -08002015 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08002016 unsigned long orig_jiffies, char_time;
2017 int lsr;
2018
2019 if (info->type == PORT_UNKNOWN)
2020 return;
2021
2022 if (info->xmit_fifo_size == 0)
2023 return; /* Just in case.... */
2024
2025 orig_jiffies = jiffies;
2026 /*
2027 * Set the check interval to be 1/5 of the estimated time to
2028 * send a single character, and make it at least 1. The check
2029 * interval should also be less than the timeout.
2030 *
2031 * Note: we have to use pretty tight timings here to satisfy
2032 * the NIST-PCTS.
2033 */
2034 char_time = (info->timeout - HZ / 50) / info->xmit_fifo_size;
2035 char_time = char_time / 5;
2036 if (char_time == 0)
2037 char_time = 1;
2038 if (timeout && timeout < char_time)
2039 char_time = timeout;
2040 /*
2041 * If the transmitter hasn't cleared in twice the approximate
2042 * amount of time to send the entire FIFO, it probably won't
2043 * ever clear. This assumes the UART isn't doing flow
2044 * control, which is currently the case. Hence, if it ever
2045 * takes longer than info->timeout, this is probably due to a
2046 * UART bug of some kind. So, we clamp the timeout parameter at
2047 * 2*info->timeout.
2048 */
2049 if (!timeout || timeout > 2 * info->timeout)
2050 timeout = 2 * info->timeout;
2051#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2052 printk(KERN_DEBUG "In rs_wait_until_sent(%d) check=%lu...",
2053 timeout, char_time);
2054 printk("jiff=%lu...", jiffies);
2055#endif
Jiri Slaby55b307d2006-12-08 02:38:14 -08002056 while (!((lsr = inb(info->ioaddr + UART_LSR)) & UART_LSR_TEMT)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002057#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2058 printk("lsr = %d (jiff=%lu)...", lsr, jiffies);
2059#endif
2060 schedule_timeout_interruptible(char_time);
2061 if (signal_pending(current))
2062 break;
2063 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2064 break;
2065 }
2066 set_current_state(TASK_RUNNING);
2067
2068#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2069 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
2070#endif
2071}
2072
Jiri Slaby037ad482006-12-08 02:38:11 -08002073/*
2074 * This routine is called by tty_hangup() when a hangup is signaled.
2075 */
2076void mxser_hangup(struct tty_struct *tty)
2077{
Jiri Slaby55b307d2006-12-08 02:38:14 -08002078 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08002079
2080 mxser_flush_buffer(tty);
2081 mxser_shutdown(info);
2082 info->event = 0;
2083 info->count = 0;
2084 info->flags &= ~ASYNC_NORMAL_ACTIVE;
2085 info->tty = NULL;
2086 wake_up_interruptible(&info->open_wait);
2087}
2088
Jiri Slaby037ad482006-12-08 02:38:11 -08002089/*
2090 * mxser_rs_break() --- routine which turns the break handling on or off
2091 */
2092static void mxser_rs_break(struct tty_struct *tty, int break_state)
2093{
Jiri Slaby55b307d2006-12-08 02:38:14 -08002094 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08002095 unsigned long flags;
2096
2097 spin_lock_irqsave(&info->slock, flags);
2098 if (break_state == -1)
Jiri Slaby55b307d2006-12-08 02:38:14 -08002099 outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
2100 info->ioaddr + UART_LCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002101 else
Jiri Slaby55b307d2006-12-08 02:38:14 -08002102 outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
2103 info->ioaddr + UART_LCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002104 spin_unlock_irqrestore(&info->slock, flags);
2105}
2106
Jiri Slaby55b307d2006-12-08 02:38:14 -08002107static void mxser_receive_chars(struct mxser_port *port, int *status)
Jiri Slaby037ad482006-12-08 02:38:11 -08002108{
Jiri Slaby55b307d2006-12-08 02:38:14 -08002109 struct tty_struct *tty = port->tty;
Jiri Slaby037ad482006-12-08 02:38:11 -08002110 unsigned char ch, gdl;
2111 int ignored = 0;
2112 int cnt = 0;
2113 int recv_room;
2114 int max = 256;
2115 unsigned long flags;
2116
Jiri Slaby55b307d2006-12-08 02:38:14 -08002117 spin_lock_irqsave(&port->slock, flags);
Jiri Slaby037ad482006-12-08 02:38:11 -08002118
2119 recv_room = tty->receive_room;
Jiri Slabye079f492006-12-08 02:38:31 -08002120 if ((recv_room == 0) && (!port->ldisc_stop_rx))
Jiri Slaby037ad482006-12-08 02:38:11 -08002121 mxser_stoprx(tty);
Jiri Slaby037ad482006-12-08 02:38:11 -08002122
Jiri Slaby55b307d2006-12-08 02:38:14 -08002123 if (port->board->chip_flag != MOXA_OTHER_UART) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002124
Jiri Slaby3306ce32006-12-08 02:38:13 -08002125 if (*status & UART_LSR_SPECIAL)
Jiri Slaby037ad482006-12-08 02:38:11 -08002126 goto intr_old;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002127 if (port->board->chip_flag == MOXA_MUST_MU860_HWID &&
Jiri Slaby037ad482006-12-08 02:38:11 -08002128 (*status & MOXA_MUST_LSR_RERR))
2129 goto intr_old;
Jiri Slaby037ad482006-12-08 02:38:11 -08002130 if (*status & MOXA_MUST_LSR_RERR)
2131 goto intr_old;
2132
Jiri Slaby55b307d2006-12-08 02:38:14 -08002133 gdl = inb(port->ioaddr + MOXA_MUST_GDL_REGISTER);
Jiri Slaby037ad482006-12-08 02:38:11 -08002134
Jiri Slaby55b307d2006-12-08 02:38:14 -08002135 if (port->board->chip_flag == MOXA_MUST_MU150_HWID)
Jiri Slaby037ad482006-12-08 02:38:11 -08002136 gdl &= MOXA_MUST_GDL_MASK;
2137 if (gdl >= recv_room) {
Jiri Slabye079f492006-12-08 02:38:31 -08002138 if (!port->ldisc_stop_rx)
Jiri Slaby037ad482006-12-08 02:38:11 -08002139 mxser_stoprx(tty);
Jiri Slaby037ad482006-12-08 02:38:11 -08002140 }
2141 while (gdl--) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002142 ch = inb(port->ioaddr + UART_RX);
Jiri Slaby037ad482006-12-08 02:38:11 -08002143 tty_insert_flip_char(tty, ch, 0);
2144 cnt++;
Jiri Slaby037ad482006-12-08 02:38:11 -08002145 }
2146 goto end_intr;
2147 }
Jiri Slabye079f492006-12-08 02:38:31 -08002148intr_old:
Jiri Slaby037ad482006-12-08 02:38:11 -08002149
2150 do {
2151 if (max-- < 0)
2152 break;
Jiri Slaby037ad482006-12-08 02:38:11 -08002153
Jiri Slaby55b307d2006-12-08 02:38:14 -08002154 ch = inb(port->ioaddr + UART_RX);
Jiri Slabye079f492006-12-08 02:38:31 -08002155 if (port->board->chip_flag && (*status & UART_LSR_OE))
Jiri Slaby55b307d2006-12-08 02:38:14 -08002156 outb(0x23, port->ioaddr + UART_FCR);
2157 *status &= port->read_status_mask;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002158 if (*status & port->ignore_status_mask) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002159 if (++ignored > 100)
2160 break;
2161 } else {
2162 char flag = 0;
2163 if (*status & UART_LSR_SPECIAL) {
2164 if (*status & UART_LSR_BI) {
2165 flag = TTY_BREAK;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002166 port->icount.brk++;
Jiri Slaby3306ce32006-12-08 02:38:13 -08002167
Jiri Slaby55b307d2006-12-08 02:38:14 -08002168 if (port->flags & ASYNC_SAK)
Jiri Slaby037ad482006-12-08 02:38:11 -08002169 do_SAK(tty);
2170 } else if (*status & UART_LSR_PE) {
2171 flag = TTY_PARITY;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002172 port->icount.parity++;
Jiri Slaby037ad482006-12-08 02:38:11 -08002173 } else if (*status & UART_LSR_FE) {
2174 flag = TTY_FRAME;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002175 port->icount.frame++;
Jiri Slaby037ad482006-12-08 02:38:11 -08002176 } else if (*status & UART_LSR_OE) {
2177 flag = TTY_OVERRUN;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002178 port->icount.overrun++;
Jiri Slaby925e9c12006-12-08 02:38:30 -08002179 }
2180 }
Jiri Slaby037ad482006-12-08 02:38:11 -08002181 tty_insert_flip_char(tty, ch, flag);
2182 cnt++;
2183 if (cnt >= recv_room) {
Jiri Slabye079f492006-12-08 02:38:31 -08002184 if (!port->ldisc_stop_rx)
Jiri Slaby037ad482006-12-08 02:38:11 -08002185 mxser_stoprx(tty);
Jiri Slaby037ad482006-12-08 02:38:11 -08002186 break;
2187 }
2188
2189 }
2190
Jiri Slaby55b307d2006-12-08 02:38:14 -08002191 if (port->board->chip_flag)
Jiri Slaby037ad482006-12-08 02:38:11 -08002192 break;
Jiri Slaby037ad482006-12-08 02:38:11 -08002193
Jiri Slaby55b307d2006-12-08 02:38:14 -08002194 *status = inb(port->ioaddr + UART_LSR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002195 } while (*status & UART_LSR_DR);
2196
Jiri Slabye079f492006-12-08 02:38:31 -08002197end_intr:
Jiri Slaby55b307d2006-12-08 02:38:14 -08002198 mxvar_log.rxcnt[port->tty->index] += cnt;
2199 port->mon_data.rxcnt += cnt;
2200 port->mon_data.up_rxcnt += cnt;
2201 spin_unlock_irqrestore(&port->slock, flags);
Jiri Slaby037ad482006-12-08 02:38:11 -08002202
2203 tty_flip_buffer_push(tty);
2204}
2205
Jiri Slaby55b307d2006-12-08 02:38:14 -08002206static void mxser_transmit_chars(struct mxser_port *port)
Jiri Slaby037ad482006-12-08 02:38:11 -08002207{
2208 int count, cnt;
2209 unsigned long flags;
2210
Jiri Slaby55b307d2006-12-08 02:38:14 -08002211 spin_lock_irqsave(&port->slock, flags);
Jiri Slaby037ad482006-12-08 02:38:11 -08002212
Jiri Slaby55b307d2006-12-08 02:38:14 -08002213 if (port->x_char) {
2214 outb(port->x_char, port->ioaddr + UART_TX);
2215 port->x_char = 0;
2216 mxvar_log.txcnt[port->tty->index]++;
2217 port->mon_data.txcnt++;
2218 port->mon_data.up_txcnt++;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002219 port->icount.tx++;
Jiri Slaby3306ce32006-12-08 02:38:13 -08002220 goto unlock;
Jiri Slaby037ad482006-12-08 02:38:11 -08002221 }
2222
Jiri Slaby55b307d2006-12-08 02:38:14 -08002223 if (port->xmit_buf == 0)
Jiri Slaby3306ce32006-12-08 02:38:13 -08002224 goto unlock;
Jiri Slaby037ad482006-12-08 02:38:11 -08002225
Jiri Slaby925e9c12006-12-08 02:38:30 -08002226 if ((port->xmit_cnt <= 0) || port->tty->stopped ||
2227 (port->tty->hw_stopped &&
Jiri Slaby55b307d2006-12-08 02:38:14 -08002228 (port->type != PORT_16550A) &&
2229 (!port->board->chip_flag))) {
2230 port->IER &= ~UART_IER_THRI;
2231 outb(port->IER, port->ioaddr + UART_IER);
Jiri Slaby3306ce32006-12-08 02:38:13 -08002232 goto unlock;
Jiri Slaby037ad482006-12-08 02:38:11 -08002233 }
2234
Jiri Slaby55b307d2006-12-08 02:38:14 -08002235 cnt = port->xmit_cnt;
2236 count = port->xmit_fifo_size;
Jiri Slaby037ad482006-12-08 02:38:11 -08002237 do {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002238 outb(port->xmit_buf[port->xmit_tail++],
2239 port->ioaddr + UART_TX);
2240 port->xmit_tail = port->xmit_tail & (SERIAL_XMIT_SIZE - 1);
2241 if (--port->xmit_cnt <= 0)
Jiri Slaby037ad482006-12-08 02:38:11 -08002242 break;
2243 } while (--count > 0);
Jiri Slaby55b307d2006-12-08 02:38:14 -08002244 mxvar_log.txcnt[port->tty->index] += (cnt - port->xmit_cnt);
Jiri Slaby037ad482006-12-08 02:38:11 -08002245
Jiri Slaby55b307d2006-12-08 02:38:14 -08002246 port->mon_data.txcnt += (cnt - port->xmit_cnt);
2247 port->mon_data.up_txcnt += (cnt - port->xmit_cnt);
Jiri Slaby55b307d2006-12-08 02:38:14 -08002248 port->icount.tx += (cnt - port->xmit_cnt);
Jiri Slaby037ad482006-12-08 02:38:11 -08002249
Jiri Slaby55b307d2006-12-08 02:38:14 -08002250 if (port->xmit_cnt < WAKEUP_CHARS) {
2251 set_bit(MXSER_EVENT_TXLOW, &port->event);
2252 schedule_work(&port->tqueue);
Jiri Slaby037ad482006-12-08 02:38:11 -08002253 }
Jiri Slaby55b307d2006-12-08 02:38:14 -08002254 if (port->xmit_cnt <= 0) {
2255 port->IER &= ~UART_IER_THRI;
2256 outb(port->IER, port->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08002257 }
Jiri Slaby3306ce32006-12-08 02:38:13 -08002258unlock:
Jiri Slaby55b307d2006-12-08 02:38:14 -08002259 spin_unlock_irqrestore(&port->slock, flags);
Jiri Slaby037ad482006-12-08 02:38:11 -08002260}
2261
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002262/*
2263 * This is the serial driver's generic interrupt routine
2264 */
Jiri Slabyb1d1c8d2006-12-08 02:38:31 -08002265static irqreturn_t mxser_interrupt(int irq, void *dev_id)
Jiri Slaby037ad482006-12-08 02:38:11 -08002266{
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002267 int status, iir, i;
2268 struct mxser_board *brd = NULL;
2269 struct mxser_port *port;
2270 int max, irqbits, bits, msr;
2271 int pass_counter = 0;
2272 unsigned int int_cnt;
2273 int handled = IRQ_NONE;
Jiri Slaby037ad482006-12-08 02:38:11 -08002274
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002275 for (i = 0; i < MXSER_BOARDS; i++)
2276 if (dev_id == &mxser_boards[i]) {
2277 brd = dev_id;
2278 break;
Jiri Slaby037ad482006-12-08 02:38:11 -08002279 }
Jiri Slaby037ad482006-12-08 02:38:11 -08002280
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002281 if (i == MXSER_BOARDS)
2282 goto irq_stop;
2283 if (brd == NULL)
2284 goto irq_stop;
Jiri Slabycd7ed642006-12-08 02:38:29 -08002285 max = brd->info->nports;
Jiri Slaby037ad482006-12-08 02:38:11 -08002286 while (1) {
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002287 irqbits = inb(brd->vector) & brd->vector_mask;
2288 if (irqbits == brd->vector_mask)
Jiri Slaby037ad482006-12-08 02:38:11 -08002289 break;
Jiri Slaby037ad482006-12-08 02:38:11 -08002290
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002291 handled = IRQ_HANDLED;
2292 for (i = 0, bits = 1; i < max; i++, irqbits |= bits, bits <<= 1) {
2293 if (irqbits == brd->vector_mask)
Jiri Slaby037ad482006-12-08 02:38:11 -08002294 break;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002295 if (bits & irqbits)
2296 continue;
2297 port = &brd->ports[i];
Jiri Slaby037ad482006-12-08 02:38:11 -08002298
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002299 int_cnt = 0;
2300 do {
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002301 iir = inb(port->ioaddr + UART_IIR);
2302 if (iir & UART_IIR_NO_INT)
2303 break;
2304 iir &= MOXA_MUST_IIR_MASK;
2305 if (!port->tty) {
2306 status = inb(port->ioaddr + UART_LSR);
2307 outb(0x27, port->ioaddr + UART_FCR);
2308 inb(port->ioaddr + UART_MSR);
2309 break;
Jiri Slaby037ad482006-12-08 02:38:11 -08002310 }
Jiri Slaby037ad482006-12-08 02:38:11 -08002311
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002312 status = inb(port->ioaddr + UART_LSR);
2313
2314 if (status & UART_LSR_PE)
2315 port->err_shadow |= NPPI_NOTIFY_PARITY;
2316 if (status & UART_LSR_FE)
2317 port->err_shadow |= NPPI_NOTIFY_FRAMING;
2318 if (status & UART_LSR_OE)
2319 port->err_shadow |=
2320 NPPI_NOTIFY_HW_OVERRUN;
2321 if (status & UART_LSR_BI)
2322 port->err_shadow |= NPPI_NOTIFY_BREAK;
2323
2324 if (port->board->chip_flag) {
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002325 if (iir == MOXA_MUST_IIR_GDA ||
2326 iir == MOXA_MUST_IIR_RDA ||
2327 iir == MOXA_MUST_IIR_RTO ||
2328 iir == MOXA_MUST_IIR_LSR)
2329 mxser_receive_chars(port,
2330 &status);
2331
2332 } else {
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002333 status &= port->read_status_mask;
2334 if (status & UART_LSR_DR)
2335 mxser_receive_chars(port,
2336 &status);
2337 }
2338 msr = inb(port->ioaddr + UART_MSR);
2339 if (msr & UART_MSR_ANY_DELTA)
2340 mxser_check_modem_status(port, msr);
2341
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002342 if (port->board->chip_flag) {
2343 if (iir == 0x02 && (status &
2344 UART_LSR_THRE))
2345 mxser_transmit_chars(port);
2346 } else {
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002347 if (status & UART_LSR_THRE)
2348 mxser_transmit_chars(port);
2349 }
2350 } while (int_cnt++ < MXSER_ISR_PASS_LIMIT);
2351 }
2352 if (pass_counter++ > MXSER_ISR_PASS_LIMIT)
2353 break; /* Prevent infinite loops */
2354 }
2355
Jiri Slabye079f492006-12-08 02:38:31 -08002356irq_stop:
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002357 return handled;
2358}
2359
2360static const struct tty_operations mxser_ops = {
2361 .open = mxser_open,
2362 .close = mxser_close,
2363 .write = mxser_write,
2364 .put_char = mxser_put_char,
2365 .flush_chars = mxser_flush_chars,
2366 .write_room = mxser_write_room,
2367 .chars_in_buffer = mxser_chars_in_buffer,
2368 .flush_buffer = mxser_flush_buffer,
2369 .ioctl = mxser_ioctl,
2370 .throttle = mxser_throttle,
2371 .unthrottle = mxser_unthrottle,
2372 .set_termios = mxser_set_termios,
2373 .stop = mxser_stop,
2374 .start = mxser_start,
2375 .hangup = mxser_hangup,
2376 .break_ctl = mxser_rs_break,
2377 .wait_until_sent = mxser_wait_until_sent,
2378 .tiocmget = mxser_tiocmget,
2379 .tiocmset = mxser_tiocmset,
2380};
2381
2382/*
2383 * The MOXA Smartio/Industio serial driver boot-time initialization code!
2384 */
2385
Jiri Slaby171d3a82006-12-08 02:38:25 -08002386static void mxser_release_res(struct mxser_board *brd, unsigned int irq)
2387{
2388 struct pci_dev *pdev = brd->pdev;
2389
2390 if (irq)
2391 free_irq(brd->irq, brd);
2392 if (pdev != NULL) { /* PCI */
2393 pci_release_region(pdev, 2);
2394 pci_release_region(pdev, 3);
2395 pci_dev_put(pdev);
2396 } else {
Jiri Slabycd7ed642006-12-08 02:38:29 -08002397 release_region(brd->ports[0].ioaddr, 8 * brd->info->nports);
Jiri Slaby171d3a82006-12-08 02:38:25 -08002398 release_region(brd->vector, 1);
2399 }
2400}
2401
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002402static int __devinit mxser_initbrd(struct mxser_board *brd)
2403{
2404 struct mxser_port *info;
2405 unsigned int i;
2406 int retval;
2407
2408 printk(KERN_INFO "max. baud rate = %d bps.\n", brd->ports[0].max_baud);
2409
Jiri Slabycd7ed642006-12-08 02:38:29 -08002410 for (i = 0; i < brd->info->nports; i++) {
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002411 info = &brd->ports[i];
2412 info->board = brd;
2413 info->stop_rx = 0;
2414 info->ldisc_stop_rx = 0;
2415
2416 /* Enhance mode enabled here */
2417 if (brd->chip_flag != MOXA_OTHER_UART)
2418 ENABLE_MOXA_MUST_ENCHANCE_MODE(info->ioaddr);
2419
2420 info->flags = ASYNC_SHARE_IRQ;
2421 info->type = brd->uart_type;
2422
2423 process_txrx_fifo(info);
2424
2425 info->custom_divisor = info->baud_base * 16;
2426 info->close_delay = 5 * HZ / 10;
2427 info->closing_wait = 30 * HZ;
2428 INIT_WORK(&info->tqueue, mxser_do_softint, info);
2429 info->normal_termios = mxvar_sdriver->init_termios;
2430 init_waitqueue_head(&info->open_wait);
2431 init_waitqueue_head(&info->close_wait);
2432 init_waitqueue_head(&info->delta_msr_wait);
2433 memset(&info->mon_data, 0, sizeof(struct mxser_mon));
2434 info->err_shadow = 0;
2435 spin_lock_init(&info->slock);
2436
2437 /* before set INT ISR, disable all int */
2438 outb(inb(info->ioaddr + UART_IER) & 0xf0,
2439 info->ioaddr + UART_IER);
2440 }
Jiri Slaby037ad482006-12-08 02:38:11 -08002441 /*
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002442 * Allocate the IRQ if necessary
Jiri Slaby037ad482006-12-08 02:38:11 -08002443 */
Jiri Slaby037ad482006-12-08 02:38:11 -08002444
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002445 retval = request_irq(brd->irq, mxser_interrupt,
2446 (brd->ports[0].flags & ASYNC_SHARE_IRQ) ? IRQF_SHARED :
2447 IRQF_DISABLED, "mxser", brd);
2448 if (retval) {
2449 printk(KERN_ERR "Board %s: Request irq failed, IRQ (%d) may "
2450 "conflict with another device.\n",
Jiri Slabycd7ed642006-12-08 02:38:29 -08002451 brd->info->name, brd->irq);
Jiri Slaby171d3a82006-12-08 02:38:25 -08002452 /* We hold resources, we need to release them. */
2453 mxser_release_res(brd, 0);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002454 return retval;
Jiri Slaby037ad482006-12-08 02:38:11 -08002455 }
Jiri Slaby037ad482006-12-08 02:38:11 -08002456 return 0;
2457}
2458
Jiri Slaby943f2952006-12-08 02:38:15 -08002459static int __init mxser_get_ISA_conf(int cap, struct mxser_board *brd)
Jiri Slaby037ad482006-12-08 02:38:11 -08002460{
2461 int id, i, bits;
2462 unsigned short regs[16], irq;
2463 unsigned char scratch, scratch2;
2464
Jiri Slaby55b307d2006-12-08 02:38:14 -08002465 brd->chip_flag = MOXA_OTHER_UART;
Jiri Slaby037ad482006-12-08 02:38:11 -08002466
2467 id = mxser_read_register(cap, regs);
Jiri Slabycd7ed642006-12-08 02:38:29 -08002468 switch (id) {
2469 case C168_ASIC_ID:
2470 brd->info = &mxser_cards[0];
2471 break;
2472 case C104_ASIC_ID:
2473 brd->info = &mxser_cards[1];
2474 break;
2475 case CI104J_ASIC_ID:
2476 brd->info = &mxser_cards[2];
2477 break;
2478 case C102_ASIC_ID:
2479 brd->info = &mxser_cards[5];
2480 break;
2481 case CI132_ASIC_ID:
2482 brd->info = &mxser_cards[6];
2483 break;
2484 case CI134_ASIC_ID:
2485 brd->info = &mxser_cards[7];
2486 break;
2487 default:
Jiri Slaby037ad482006-12-08 02:38:11 -08002488 return 0;
Jiri Slabycd7ed642006-12-08 02:38:29 -08002489 }
Jiri Slaby037ad482006-12-08 02:38:11 -08002490
2491 irq = 0;
Jiri Slabycd7ed642006-12-08 02:38:29 -08002492 /* some ISA cards have 2 ports, but we want to see them as 4-port (why?)
2493 Flag-hack checks if configuration should be read as 2-port here. */
2494 if (brd->info->nports == 2 || (brd->info->flags & MXSER_HAS2)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002495 irq = regs[9] & 0xF000;
2496 irq = irq | (irq >> 4);
2497 if (irq != (regs[9] & 0xFF00))
2498 return MXSER_ERR_IRQ_CONFLIT;
Jiri Slabycd7ed642006-12-08 02:38:29 -08002499 } else if (brd->info->nports == 4) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002500 irq = regs[9] & 0xF000;
2501 irq = irq | (irq >> 4);
2502 irq = irq | (irq >> 8);
2503 if (irq != regs[9])
2504 return MXSER_ERR_IRQ_CONFLIT;
Jiri Slabycd7ed642006-12-08 02:38:29 -08002505 } else if (brd->info->nports == 8) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002506 irq = regs[9] & 0xF000;
2507 irq = irq | (irq >> 4);
2508 irq = irq | (irq >> 8);
2509 if ((irq != regs[9]) || (irq != regs[10]))
2510 return MXSER_ERR_IRQ_CONFLIT;
2511 }
2512
2513 if (!irq)
2514 return MXSER_ERR_IRQ;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002515 brd->irq = ((int)(irq & 0xF000) >> 12);
Jiri Slaby037ad482006-12-08 02:38:11 -08002516 for (i = 0; i < 8; i++)
Jiri Slaby55b307d2006-12-08 02:38:14 -08002517 brd->ports[i].ioaddr = (int) regs[i + 1] & 0xFFF8;
Jiri Slaby037ad482006-12-08 02:38:11 -08002518 if ((regs[12] & 0x80) == 0)
2519 return MXSER_ERR_VECTOR;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002520 brd->vector = (int)regs[11]; /* interrupt vector */
Jiri Slaby037ad482006-12-08 02:38:11 -08002521 if (id == 1)
Jiri Slaby55b307d2006-12-08 02:38:14 -08002522 brd->vector_mask = 0x00FF;
Jiri Slaby037ad482006-12-08 02:38:11 -08002523 else
Jiri Slaby55b307d2006-12-08 02:38:14 -08002524 brd->vector_mask = 0x000F;
Jiri Slaby037ad482006-12-08 02:38:11 -08002525 for (i = 7, bits = 0x0100; i >= 0; i--, bits <<= 1) {
2526 if (regs[12] & bits) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002527 brd->ports[i].baud_base = 921600;
Jiri Slabye079f492006-12-08 02:38:31 -08002528 brd->ports[i].max_baud = 921600;
Jiri Slaby037ad482006-12-08 02:38:11 -08002529 } else {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002530 brd->ports[i].baud_base = 115200;
Jiri Slabye079f492006-12-08 02:38:31 -08002531 brd->ports[i].max_baud = 115200;
Jiri Slaby037ad482006-12-08 02:38:11 -08002532 }
2533 }
2534 scratch2 = inb(cap + UART_LCR) & (~UART_LCR_DLAB);
2535 outb(scratch2 | UART_LCR_DLAB, cap + UART_LCR);
2536 outb(0, cap + UART_EFR); /* EFR is the same as FCR */
2537 outb(scratch2, cap + UART_LCR);
2538 outb(UART_FCR_ENABLE_FIFO, cap + UART_FCR);
2539 scratch = inb(cap + UART_IIR);
2540
2541 if (scratch & 0xC0)
Jiri Slaby55b307d2006-12-08 02:38:14 -08002542 brd->uart_type = PORT_16550A;
Jiri Slaby037ad482006-12-08 02:38:11 -08002543 else
Jiri Slaby55b307d2006-12-08 02:38:14 -08002544 brd->uart_type = PORT_16450;
Jiri Slabycd7ed642006-12-08 02:38:29 -08002545 if (!request_region(brd->ports[0].ioaddr, 8 * brd->info->nports,
2546 "mxser(IO)"))
Jiri Slaby7a7a5c32006-12-08 02:38:17 -08002547 return MXSER_ERR_IOADDR;
2548 if (!request_region(brd->vector, 1, "mxser(vector)")) {
Jiri Slabycd7ed642006-12-08 02:38:29 -08002549 release_region(brd->ports[0].ioaddr, 8 * brd->info->nports);
Jiri Slaby7a7a5c32006-12-08 02:38:17 -08002550 return MXSER_ERR_VECTOR;
2551 }
Jiri Slabycd7ed642006-12-08 02:38:29 -08002552 return brd->info->nports;
Jiri Slaby037ad482006-12-08 02:38:11 -08002553}
2554
Jiri Slabycd7ed642006-12-08 02:38:29 -08002555static int __init mxser_get_PCI_conf(const struct pci_device_id *ent,
2556 struct mxser_board *brd, struct pci_dev *pdev)
Jiri Slaby037ad482006-12-08 02:38:11 -08002557{
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002558 unsigned int i, j;
2559 unsigned long ioaddress;
2560 int retval;
Jiri Slaby037ad482006-12-08 02:38:11 -08002561
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002562 /* io address */
Jiri Slabycd7ed642006-12-08 02:38:29 -08002563 brd->info = &mxser_cards[ent->driver_data];
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002564 ioaddress = pci_resource_start(pdev, 2);
2565 retval = pci_request_region(pdev, 2, "mxser(IO)");
2566 if (retval)
2567 goto err;
2568
Jiri Slabycd7ed642006-12-08 02:38:29 -08002569 for (i = 0; i < brd->info->nports; i++)
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002570 brd->ports[i].ioaddr = ioaddress + 8 * i;
2571
2572 /* vector */
2573 ioaddress = pci_resource_start(pdev, 3);
2574 retval = pci_request_region(pdev, 3, "mxser(vector)");
2575 if (retval)
2576 goto err_relio;
2577 brd->vector = ioaddress;
2578
2579 /* irq */
2580 brd->irq = pdev->irq;
2581
2582 brd->chip_flag = CheckIsMoxaMust(brd->ports[0].ioaddr);
2583 brd->uart_type = PORT_16550A;
2584 brd->vector_mask = 0;
2585
Jiri Slabycd7ed642006-12-08 02:38:29 -08002586 for (i = 0; i < brd->info->nports; i++) {
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002587 for (j = 0; j < UART_INFO_NUM; j++) {
2588 if (Gpci_uart_info[j].type == brd->chip_flag) {
2589 brd->ports[i].max_baud =
2590 Gpci_uart_info[j].max_baud;
2591
2592 /* exception....CP-102 */
Jiri Slabycd7ed642006-12-08 02:38:29 -08002593 if (brd->info->flags & MXSER_HIGHBAUD)
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002594 brd->ports[i].max_baud = 921600;
2595 break;
Jiri Slaby037ad482006-12-08 02:38:11 -08002596 }
2597 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002598 }
2599
2600 if (brd->chip_flag == MOXA_MUST_MU860_HWID) {
Jiri Slabycd7ed642006-12-08 02:38:29 -08002601 for (i = 0; i < brd->info->nports; i++) {
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002602 if (i < 4)
2603 brd->ports[i].opmode_ioaddr = ioaddress + 4;
2604 else
2605 brd->ports[i].opmode_ioaddr = ioaddress + 0x0c;
Jiri Slaby037ad482006-12-08 02:38:11 -08002606 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002607 outb(0, ioaddress + 4); /* default set to RS232 mode */
2608 outb(0, ioaddress + 0x0c); /* default set to RS232 mode */
Jiri Slaby037ad482006-12-08 02:38:11 -08002609 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002610
Jiri Slabycd7ed642006-12-08 02:38:29 -08002611 for (i = 0; i < brd->info->nports; i++) {
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002612 brd->vector_mask |= (1 << i);
2613 brd->ports[i].baud_base = 921600;
2614 }
2615 return 0;
2616err_relio:
2617 pci_release_region(pdev, 2);
2618err:
2619 return retval;
Jiri Slaby037ad482006-12-08 02:38:11 -08002620}
2621
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002622static int __init mxser_module_init(void)
Jiri Slaby037ad482006-12-08 02:38:11 -08002623{
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002624 struct pci_dev *pdev = NULL;
2625 struct mxser_board *brd;
Jiri Slabyeae44362006-12-08 02:38:27 -08002626 unsigned long cap;
2627 unsigned int i, m, isaloop;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002628 int retval, b, n;
Jiri Slaby037ad482006-12-08 02:38:11 -08002629
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002630 pr_debug("Loading module mxser ...\n");
Jiri Slaby037ad482006-12-08 02:38:11 -08002631
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002632 mxvar_sdriver = alloc_tty_driver(MXSER_PORTS + 1);
2633 if (!mxvar_sdriver)
2634 return -ENOMEM;
2635 spin_lock_init(&gm_lock);
2636
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002637 printk(KERN_INFO "MOXA Smartio/Industio family driver version %s\n",
2638 MXSER_VERSION);
2639
2640 /* Initialize the tty_driver structure */
2641 mxvar_sdriver->magic = TTY_DRIVER_MAGIC;
2642 mxvar_sdriver->name = "ttyM";
2643 mxvar_sdriver->major = ttymajor;
2644 mxvar_sdriver->minor_start = 0;
2645 mxvar_sdriver->num = MXSER_PORTS + 1;
2646 mxvar_sdriver->type = TTY_DRIVER_TYPE_SERIAL;
2647 mxvar_sdriver->subtype = SERIAL_TYPE_NORMAL;
2648 mxvar_sdriver->init_termios = tty_std_termios;
2649 mxvar_sdriver->init_termios.c_cflag = B9600|CS8|CREAD|HUPCL|CLOCAL;
Jiri Slaby938ef182006-12-08 02:38:28 -08002650 mxvar_sdriver->flags = TTY_DRIVER_REAL_RAW|TTY_DRIVER_DYNAMIC_DEV;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002651 tty_set_operations(mxvar_sdriver, &mxser_ops);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002652
Jiri Slaby938ef182006-12-08 02:38:28 -08002653 retval = tty_register_driver(mxvar_sdriver);
2654 if (retval) {
2655 printk(KERN_ERR "Couldn't install MOXA Smartio/Industio family "
2656 "tty driver !\n");
2657 goto err_put;
2658 }
2659
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002660 mxvar_diagflag = 0;
2661
2662 m = 0;
2663 /* Start finding ISA boards here */
Jiri Slabyeae44362006-12-08 02:38:27 -08002664 for (isaloop = 0; isaloop < 2; isaloop++)
2665 for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
2666 if (!isaloop)
2667 cap = mxserBoardCAP[b]; /* predefined */
2668 else
2669 cap = ioaddr[b]; /* module param */
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002670
Jiri Slabyeae44362006-12-08 02:38:27 -08002671 if (!cap)
2672 continue;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002673
Jiri Slabyeae44362006-12-08 02:38:27 -08002674 brd = &mxser_boards[m];
2675 retval = mxser_get_ISA_conf(cap, brd);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002676
Jiri Slabyeae44362006-12-08 02:38:27 -08002677 if (retval != 0)
2678 printk(KERN_INFO "Found MOXA %s board "
2679 "(CAP=0x%x)\n",
Jiri Slabycd7ed642006-12-08 02:38:29 -08002680 brd->info->name, ioaddr[b]);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002681
Jiri Slabyeae44362006-12-08 02:38:27 -08002682 if (retval <= 0) {
2683 if (retval == MXSER_ERR_IRQ)
2684 printk(KERN_ERR "Invalid interrupt "
2685 "number, board not "
2686 "configured\n");
2687 else if (retval == MXSER_ERR_IRQ_CONFLIT)
2688 printk(KERN_ERR "Invalid interrupt "
2689 "number, board not "
2690 "configured\n");
2691 else if (retval == MXSER_ERR_VECTOR)
2692 printk(KERN_ERR "Invalid interrupt "
2693 "vector, board not "
2694 "configured\n");
2695 else if (retval == MXSER_ERR_IOADDR)
2696 printk(KERN_ERR "Invalid I/O address, "
2697 "board not configured\n");
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002698
Jiri Slabyeae44362006-12-08 02:38:27 -08002699 continue;
2700 }
2701
2702 brd->pdev = NULL;
2703
2704 /* mxser_initbrd will hook ISR. */
2705 if (mxser_initbrd(brd) < 0)
2706 continue;
2707
Jiri Slabycd7ed642006-12-08 02:38:29 -08002708 for (i = 0; i < brd->info->nports; i++)
Jiri Slaby938ef182006-12-08 02:38:28 -08002709 tty_register_device(mxvar_sdriver,
2710 m * MXSER_PORTS_PER_BOARD + i, NULL);
2711
Jiri Slabyeae44362006-12-08 02:38:27 -08002712 m++;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002713 }
2714
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002715 /* start finding PCI board here */
2716 n = ARRAY_SIZE(mxser_pcibrds) - 1;
2717 b = 0;
2718 while (b < n) {
2719 pdev = pci_get_device(mxser_pcibrds[b].vendor,
2720 mxser_pcibrds[b].device, pdev);
2721 if (pdev == NULL) {
2722 b++;
2723 continue;
2724 }
2725 printk(KERN_INFO "Found MOXA %s board(BusNo=%d,DevNo=%d)\n",
Jiri Slabycd7ed642006-12-08 02:38:29 -08002726 mxser_cards[mxser_pcibrds[b].driver_data].name,
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002727 pdev->bus->number, PCI_SLOT(pdev->devfn));
2728 if (m >= MXSER_BOARDS)
2729 printk(KERN_ERR
2730 "Too many Smartio/Industio family boards find "
2731 "(maximum %d), board not configured\n",
2732 MXSER_BOARDS);
2733 else {
2734 if (pci_enable_device(pdev)) {
2735 printk(KERN_ERR "Moxa SmartI/O PCI enable "
2736 "fail !\n");
2737 continue;
2738 }
2739 brd = &mxser_boards[m];
2740 brd->pdev = pdev;
Jiri Slabycd7ed642006-12-08 02:38:29 -08002741 retval = mxser_get_PCI_conf(&mxser_pcibrds[b],
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002742 brd, pdev);
2743 if (retval < 0) {
2744 if (retval == MXSER_ERR_IRQ)
2745 printk(KERN_ERR
2746 "Invalid interrupt number, "
2747 "board not configured\n");
2748 else if (retval == MXSER_ERR_IRQ_CONFLIT)
2749 printk(KERN_ERR
2750 "Invalid interrupt number, "
2751 "board not configured\n");
2752 else if (retval == MXSER_ERR_VECTOR)
2753 printk(KERN_ERR
2754 "Invalid interrupt vector, "
2755 "board not configured\n");
2756 else if (retval == MXSER_ERR_IOADDR)
2757 printk(KERN_ERR
2758 "Invalid I/O address, "
2759 "board not configured\n");
2760 continue;
2761 }
2762 /* mxser_initbrd will hook ISR. */
2763 if (mxser_initbrd(brd) < 0)
2764 continue;
Jiri Slabycd7ed642006-12-08 02:38:29 -08002765 for (i = 0; i < brd->info->nports; i++)
Jiri Slaby938ef182006-12-08 02:38:28 -08002766 tty_register_device(mxvar_sdriver,
2767 m * MXSER_PORTS_PER_BOARD + i,
2768 &pdev->dev);
2769
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002770 m++;
2771 /* Keep an extra reference if we succeeded. It will
2772 be returned at unload time */
2773 pci_dev_get(pdev);
2774 }
2775 }
2776
Jiri Slaby938ef182006-12-08 02:38:28 -08002777 if (!m) {
2778 retval = -ENODEV;
2779 goto err_unr;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002780 }
2781
2782 pr_debug("Done.\n");
2783
Jiri Slaby938ef182006-12-08 02:38:28 -08002784 return 0;
2785err_unr:
2786 tty_unregister_driver(mxvar_sdriver);
2787err_put:
2788 put_tty_driver(mxvar_sdriver);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002789 return retval;
Jiri Slaby037ad482006-12-08 02:38:11 -08002790}
2791
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002792static void __exit mxser_module_exit(void)
Jiri Slaby037ad482006-12-08 02:38:11 -08002793{
Jiri Slabyead568c2006-12-08 02:38:26 -08002794 unsigned int i;
Jiri Slaby037ad482006-12-08 02:38:11 -08002795
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002796 pr_debug("Unloading module mxser ...\n");
2797
Jiri Slaby938ef182006-12-08 02:38:28 -08002798 for (i = 0; i < MXSER_PORTS; i++)
2799 tty_unregister_device(mxvar_sdriver, i);
Jiri Slabyead568c2006-12-08 02:38:26 -08002800 tty_unregister_driver(mxvar_sdriver);
2801 put_tty_driver(mxvar_sdriver);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002802
Jiri Slaby171d3a82006-12-08 02:38:25 -08002803 for (i = 0; i < MXSER_BOARDS; i++)
Jiri Slabycd7ed642006-12-08 02:38:29 -08002804 if (mxser_boards[i].info != NULL)
Jiri Slaby171d3a82006-12-08 02:38:25 -08002805 mxser_release_res(&mxser_boards[i], 1);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002806
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002807 pr_debug("Done.\n");
Jiri Slaby037ad482006-12-08 02:38:11 -08002808}
2809
2810module_init(mxser_module_init);
2811module_exit(mxser_module_exit);