blob: c06c5920bf1cd43527b512dbd7fa5fc57d92c8fc [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 {
Jiri Slaby2094e752006-12-08 02:38:33 -0800286 unsigned int idx;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800287 int irq;
Jiri Slabycd7ed642006-12-08 02:38:29 -0800288 const struct mxser_cardinfo *info;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800289 unsigned long vector;
290 unsigned long vector_mask;
291
292 int chip_flag;
293 int uart_type;
294
295 struct mxser_port ports[MXSER_PORTS_PER_BOARD];
Jiri Slaby037ad482006-12-08 02:38:11 -0800296};
297
298struct mxser_mstatus {
299 tcflag_t cflag;
300 int cts;
301 int dsr;
302 int ri;
303 int dcd;
304};
305
306static struct mxser_mstatus GMStatus[MXSER_PORTS];
307
308static int mxserBoardCAP[MXSER_BOARDS] = {
309 0, 0, 0, 0
310 /* 0x180, 0x280, 0x200, 0x320 */
311};
312
Jiri Slaby55b307d2006-12-08 02:38:14 -0800313static struct mxser_board mxser_boards[MXSER_BOARDS];
Jiri Slaby037ad482006-12-08 02:38:11 -0800314static struct tty_driver *mxvar_sdriver;
Jiri Slaby037ad482006-12-08 02:38:11 -0800315static struct mxser_log mxvar_log;
316static int mxvar_diagflag;
317static unsigned char mxser_msr[MXSER_PORTS + 1];
318static struct mxser_mon_ext mon_data_ext;
319static int mxser_set_baud_method[MXSER_PORTS + 1];
320static spinlock_t gm_lock;
321
Jiri Slaby037ad482006-12-08 02:38:11 -0800322static int CheckIsMoxaMust(int io)
323{
324 u8 oldmcr, hwid;
325 int i;
326
327 outb(0, io + UART_LCR);
328 DISABLE_MOXA_MUST_ENCHANCE_MODE(io);
329 oldmcr = inb(io + UART_MCR);
330 outb(0, io + UART_MCR);
331 SET_MOXA_MUST_XON1_VALUE(io, 0x11);
332 if ((hwid = inb(io + UART_MCR)) != 0) {
333 outb(oldmcr, io + UART_MCR);
334 return MOXA_OTHER_UART;
335 }
336
337 GET_MOXA_MUST_HARDWARE_ID(io, &hwid);
Jiri Slabycd7ed642006-12-08 02:38:29 -0800338 for (i = 1; i < UART_INFO_NUM; i++) { /* 0 = OTHER_UART */
339 if (hwid == Gpci_uart_info[i].type)
Jiri Slaby037ad482006-12-08 02:38:11 -0800340 return (int)hwid;
341 }
342 return MOXA_OTHER_UART;
343}
344
Jiri Slaby55b307d2006-12-08 02:38:14 -0800345static void process_txrx_fifo(struct mxser_port *info)
Jiri Slaby037ad482006-12-08 02:38:11 -0800346{
347 int i;
348
349 if ((info->type == PORT_16450) || (info->type == PORT_8250)) {
350 info->rx_trigger = 1;
351 info->rx_high_water = 1;
352 info->rx_low_water = 1;
353 info->xmit_fifo_size = 1;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800354 } else
355 for (i = 0; i < UART_INFO_NUM; i++)
356 if (info->board->chip_flag == Gpci_uart_info[i].type) {
Jiri Slaby037ad482006-12-08 02:38:11 -0800357 info->rx_trigger = Gpci_uart_info[i].rx_trigger;
358 info->rx_low_water = Gpci_uart_info[i].rx_low_water;
359 info->rx_high_water = Gpci_uart_info[i].rx_high_water;
360 info->xmit_fifo_size = Gpci_uart_info[i].xmit_fifo_size;
361 break;
362 }
Jiri Slaby037ad482006-12-08 02:38:11 -0800363}
364
Jiri Slaby037ad482006-12-08 02:38:11 -0800365static void mxser_do_softint(void *private_)
366{
Jiri Slaby55b307d2006-12-08 02:38:14 -0800367 struct mxser_port *info = private_;
Jiri Slaby037ad482006-12-08 02:38:11 -0800368 struct tty_struct *tty;
369
370 tty = info->tty;
371
Jiri Slaby3306ce32006-12-08 02:38:13 -0800372 if (test_and_clear_bit(MXSER_EVENT_TXLOW, &info->event))
373 tty_wakeup(tty);
374 if (test_and_clear_bit(MXSER_EVENT_HANGUP, &info->event))
375 tty_hangup(tty);
Jiri Slaby037ad482006-12-08 02:38:11 -0800376}
377
Jiri Slaby55b307d2006-12-08 02:38:14 -0800378static unsigned char mxser_get_msr(int baseaddr, int mode, int port)
Jiri Slaby037ad482006-12-08 02:38:11 -0800379{
380 unsigned char status = 0;
381
382 status = inb(baseaddr + UART_MSR);
383
384 mxser_msr[port] &= 0x0F;
385 mxser_msr[port] |= status;
386 status = mxser_msr[port];
387 if (mode)
388 mxser_msr[port] = 0;
389
390 return status;
391}
392
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800393static int mxser_block_til_ready(struct tty_struct *tty, struct file *filp,
394 struct mxser_port *port)
395{
396 DECLARE_WAITQUEUE(wait, current);
397 int retval;
398 int do_clocal = 0;
399 unsigned long flags;
400
401 /*
402 * If non-blocking mode is set, or the port is not enabled,
403 * then make the check up front and then exit.
404 */
Jiri Slaby214efeb2006-12-08 02:38:25 -0800405 if ((filp->f_flags & O_NONBLOCK) ||
406 test_bit(TTY_IO_ERROR, &tty->flags)) {
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800407 port->flags |= ASYNC_NORMAL_ACTIVE;
408 return 0;
409 }
410
411 if (tty->termios->c_cflag & CLOCAL)
412 do_clocal = 1;
413
414 /*
415 * Block waiting for the carrier detect and the line to become
416 * free (i.e., not in use by the callout). While we are in
417 * this loop, port->count is dropped by one, so that
418 * mxser_close() knows when to free things. We restore it upon
419 * exit, either normal or abnormal.
420 */
421 retval = 0;
422 add_wait_queue(&port->open_wait, &wait);
423
424 spin_lock_irqsave(&port->slock, flags);
425 if (!tty_hung_up_p(filp))
426 port->count--;
427 spin_unlock_irqrestore(&port->slock, flags);
428 port->blocked_open++;
429 while (1) {
430 spin_lock_irqsave(&port->slock, flags);
431 outb(inb(port->ioaddr + UART_MCR) |
432 UART_MCR_DTR | UART_MCR_RTS, port->ioaddr + UART_MCR);
433 spin_unlock_irqrestore(&port->slock, flags);
434 set_current_state(TASK_INTERRUPTIBLE);
435 if (tty_hung_up_p(filp) || !(port->flags & ASYNC_INITIALIZED)) {
436 if (port->flags & ASYNC_HUP_NOTIFY)
437 retval = -EAGAIN;
438 else
439 retval = -ERESTARTSYS;
440 break;
441 }
442 if (!(port->flags & ASYNC_CLOSING) &&
443 (do_clocal ||
444 (inb(port->ioaddr + UART_MSR) & UART_MSR_DCD)))
445 break;
446 if (signal_pending(current)) {
447 retval = -ERESTARTSYS;
448 break;
449 }
450 schedule();
451 }
452 set_current_state(TASK_RUNNING);
453 remove_wait_queue(&port->open_wait, &wait);
454 if (!tty_hung_up_p(filp))
455 port->count++;
456 port->blocked_open--;
457 if (retval)
458 return retval;
459 port->flags |= ASYNC_NORMAL_ACTIVE;
460 return 0;
461}
462
463static int mxser_set_baud(struct mxser_port *info, long newspd)
464{
465 int quot = 0;
466 unsigned char cval;
467 int ret = 0;
468 unsigned long flags;
469
470 if (!info->tty || !info->tty->termios)
471 return ret;
472
473 if (!(info->ioaddr))
474 return ret;
475
476 if (newspd > info->max_baud)
477 return 0;
478
479 info->realbaud = newspd;
480 if (newspd == 134) {
481 quot = (2 * info->baud_base / 269);
482 } else if (newspd) {
483 quot = info->baud_base / newspd;
484 if (quot == 0)
485 quot = 1;
486 } else {
487 quot = 0;
488 }
489
490 info->timeout = ((info->xmit_fifo_size * HZ * 10 * quot) / info->baud_base);
491 info->timeout += HZ / 50; /* Add .02 seconds of slop */
492
493 if (quot) {
494 spin_lock_irqsave(&info->slock, flags);
495 info->MCR |= UART_MCR_DTR;
496 outb(info->MCR, info->ioaddr + UART_MCR);
497 spin_unlock_irqrestore(&info->slock, flags);
498 } else {
499 spin_lock_irqsave(&info->slock, flags);
500 info->MCR &= ~UART_MCR_DTR;
501 outb(info->MCR, info->ioaddr + UART_MCR);
502 spin_unlock_irqrestore(&info->slock, flags);
503 return ret;
504 }
505
506 cval = inb(info->ioaddr + UART_LCR);
507
508 outb(cval | UART_LCR_DLAB, info->ioaddr + UART_LCR); /* set DLAB */
509
510 outb(quot & 0xff, info->ioaddr + UART_DLL); /* LS of divisor */
511 outb(quot >> 8, info->ioaddr + UART_DLM); /* MS of divisor */
512 outb(cval, info->ioaddr + UART_LCR); /* reset DLAB */
513
514
515 return ret;
516}
517
518/*
519 * This routine is called to set the UART divisor registers to match
520 * the specified baud rate for a serial port.
521 */
522static int mxser_change_speed(struct mxser_port *info,
523 struct termios *old_termios)
524{
525 unsigned cflag, cval, fcr;
526 int ret = 0;
527 unsigned char status;
528 long baud;
529 unsigned long flags;
530
531 if (!info->tty || !info->tty->termios)
532 return ret;
533 cflag = info->tty->termios->c_cflag;
534 if (!(info->ioaddr))
535 return ret;
536
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800537 if (mxser_set_baud_method[info->tty->index] == 0) {
538 baud = tty_get_baud_rate(info->tty);
539 mxser_set_baud(info, baud);
540 }
541
542 /* byte size and parity */
543 switch (cflag & CSIZE) {
544 case CS5:
545 cval = 0x00;
546 break;
547 case CS6:
548 cval = 0x01;
549 break;
550 case CS7:
551 cval = 0x02;
552 break;
553 case CS8:
554 cval = 0x03;
555 break;
556 default:
557 cval = 0x00;
558 break; /* too keep GCC shut... */
559 }
560 if (cflag & CSTOPB)
561 cval |= 0x04;
562 if (cflag & PARENB)
563 cval |= UART_LCR_PARITY;
564 if (!(cflag & PARODD))
565 cval |= UART_LCR_EPAR;
566 if (cflag & CMSPAR)
567 cval |= UART_LCR_SPAR;
568
569 if ((info->type == PORT_8250) || (info->type == PORT_16450)) {
570 if (info->board->chip_flag) {
571 fcr = UART_FCR_ENABLE_FIFO;
572 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
573 SET_MOXA_MUST_FIFO_VALUE(info);
574 } else
575 fcr = 0;
576 } else {
577 fcr = UART_FCR_ENABLE_FIFO;
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800578 if (info->board->chip_flag) {
579 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
580 SET_MOXA_MUST_FIFO_VALUE(info);
581 } else {
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800582 switch (info->rx_trigger) {
583 case 1:
584 fcr |= UART_FCR_TRIGGER_1;
585 break;
586 case 4:
587 fcr |= UART_FCR_TRIGGER_4;
588 break;
589 case 8:
590 fcr |= UART_FCR_TRIGGER_8;
591 break;
592 default:
593 fcr |= UART_FCR_TRIGGER_14;
594 break;
595 }
596 }
597 }
598
599 /* CTS flow control flag and modem status interrupts */
600 info->IER &= ~UART_IER_MSI;
601 info->MCR &= ~UART_MCR_AFE;
602 if (cflag & CRTSCTS) {
603 info->flags |= ASYNC_CTS_FLOW;
604 info->IER |= UART_IER_MSI;
605 if ((info->type == PORT_16550A) || (info->board->chip_flag)) {
606 info->MCR |= UART_MCR_AFE;
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800607 } else {
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800608 status = inb(info->ioaddr + UART_MSR);
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800609 if (info->tty->hw_stopped) {
610 if (status & UART_MSR_CTS) {
611 info->tty->hw_stopped = 0;
612 if (info->type != PORT_16550A &&
613 !info->board->chip_flag) {
614 outb(info->IER & ~UART_IER_THRI,
615 info->ioaddr +
616 UART_IER);
617 info->IER |= UART_IER_THRI;
618 outb(info->IER, info->ioaddr +
619 UART_IER);
620 }
621 set_bit(MXSER_EVENT_TXLOW, &info->event);
622 schedule_work(&info->tqueue); }
623 } else {
624 if (!(status & UART_MSR_CTS)) {
625 info->tty->hw_stopped = 1;
626 if ((info->type != PORT_16550A) &&
627 (!info->board->chip_flag)) {
628 info->IER &= ~UART_IER_THRI;
629 outb(info->IER, info->ioaddr +
630 UART_IER);
631 }
632 }
633 }
634 }
635 } else {
636 info->flags &= ~ASYNC_CTS_FLOW;
637 }
638 outb(info->MCR, info->ioaddr + UART_MCR);
639 if (cflag & CLOCAL) {
640 info->flags &= ~ASYNC_CHECK_CD;
641 } else {
642 info->flags |= ASYNC_CHECK_CD;
643 info->IER |= UART_IER_MSI;
644 }
645 outb(info->IER, info->ioaddr + UART_IER);
646
647 /*
648 * Set up parity check flag
649 */
650 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
651 if (I_INPCK(info->tty))
652 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
653 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
654 info->read_status_mask |= UART_LSR_BI;
655
656 info->ignore_status_mask = 0;
657
658 if (I_IGNBRK(info->tty)) {
659 info->ignore_status_mask |= UART_LSR_BI;
660 info->read_status_mask |= UART_LSR_BI;
661 /*
662 * If we're ignore parity and break indicators, ignore
663 * overruns too. (For real raw support).
664 */
665 if (I_IGNPAR(info->tty)) {
666 info->ignore_status_mask |=
667 UART_LSR_OE |
668 UART_LSR_PE |
669 UART_LSR_FE;
670 info->read_status_mask |=
671 UART_LSR_OE |
672 UART_LSR_PE |
673 UART_LSR_FE;
674 }
675 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800676 if (info->board->chip_flag) {
677 spin_lock_irqsave(&info->slock, flags);
678 SET_MOXA_MUST_XON1_VALUE(info->ioaddr, START_CHAR(info->tty));
679 SET_MOXA_MUST_XOFF1_VALUE(info->ioaddr, STOP_CHAR(info->tty));
680 if (I_IXON(info->tty)) {
681 ENABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
682 } else {
683 DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
684 }
685 if (I_IXOFF(info->tty)) {
686 ENABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
687 } else {
688 DISABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
689 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800690 spin_unlock_irqrestore(&info->slock, flags);
691 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800692
693
694 outb(fcr, info->ioaddr + UART_FCR); /* set fcr */
695 outb(cval, info->ioaddr + UART_LCR);
696
697 return ret;
698}
699
700static void mxser_check_modem_status(struct mxser_port *port, int status)
701{
702 /* update input line counters */
703 if (status & UART_MSR_TERI)
704 port->icount.rng++;
705 if (status & UART_MSR_DDSR)
706 port->icount.dsr++;
707 if (status & UART_MSR_DDCD)
708 port->icount.dcd++;
709 if (status & UART_MSR_DCTS)
710 port->icount.cts++;
711 port->mon_data.modem_status = status;
712 wake_up_interruptible(&port->delta_msr_wait);
713
714 if ((port->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
715 if (status & UART_MSR_DCD)
716 wake_up_interruptible(&port->open_wait);
717 schedule_work(&port->tqueue);
718 }
719
720 if (port->flags & ASYNC_CTS_FLOW) {
721 if (port->tty->hw_stopped) {
722 if (status & UART_MSR_CTS) {
723 port->tty->hw_stopped = 0;
724
725 if ((port->type != PORT_16550A) &&
726 (!port->board->chip_flag)) {
727 outb(port->IER & ~UART_IER_THRI,
728 port->ioaddr + UART_IER);
729 port->IER |= UART_IER_THRI;
730 outb(port->IER, port->ioaddr +
731 UART_IER);
732 }
733 set_bit(MXSER_EVENT_TXLOW, &port->event);
734 schedule_work(&port->tqueue);
735 }
736 } else {
737 if (!(status & UART_MSR_CTS)) {
738 port->tty->hw_stopped = 1;
739 if (port->type != PORT_16550A &&
740 !port->board->chip_flag) {
741 port->IER &= ~UART_IER_THRI;
742 outb(port->IER, port->ioaddr +
743 UART_IER);
744 }
745 }
746 }
747 }
748}
749
750static int mxser_startup(struct mxser_port *info)
751{
752 unsigned long page;
753 unsigned long flags;
754
755 page = __get_free_page(GFP_KERNEL);
756 if (!page)
757 return -ENOMEM;
758
759 spin_lock_irqsave(&info->slock, flags);
760
761 if (info->flags & ASYNC_INITIALIZED) {
762 free_page(page);
763 spin_unlock_irqrestore(&info->slock, flags);
764 return 0;
765 }
766
767 if (!info->ioaddr || !info->type) {
768 if (info->tty)
769 set_bit(TTY_IO_ERROR, &info->tty->flags);
770 free_page(page);
771 spin_unlock_irqrestore(&info->slock, flags);
772 return 0;
773 }
774 if (info->xmit_buf)
775 free_page(page);
776 else
777 info->xmit_buf = (unsigned char *) page;
778
779 /*
780 * Clear the FIFO buffers and disable them
781 * (they will be reenabled in mxser_change_speed())
782 */
783 if (info->board->chip_flag)
784 outb((UART_FCR_CLEAR_RCVR |
785 UART_FCR_CLEAR_XMIT |
786 MOXA_MUST_FCR_GDA_MODE_ENABLE), info->ioaddr + UART_FCR);
787 else
788 outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
789 info->ioaddr + UART_FCR);
790
791 /*
792 * At this point there's no way the LSR could still be 0xFF;
793 * if it is, then bail out, because there's likely no UART
794 * here.
795 */
796 if (inb(info->ioaddr + UART_LSR) == 0xff) {
797 spin_unlock_irqrestore(&info->slock, flags);
798 if (capable(CAP_SYS_ADMIN)) {
799 if (info->tty)
800 set_bit(TTY_IO_ERROR, &info->tty->flags);
801 return 0;
802 } else
803 return -ENODEV;
804 }
805
806 /*
807 * Clear the interrupt registers.
808 */
809 (void) inb(info->ioaddr + UART_LSR);
810 (void) inb(info->ioaddr + UART_RX);
811 (void) inb(info->ioaddr + UART_IIR);
812 (void) inb(info->ioaddr + UART_MSR);
813
814 /*
815 * Now, initialize the UART
816 */
817 outb(UART_LCR_WLEN8, info->ioaddr + UART_LCR); /* reset DLAB */
818 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
819 outb(info->MCR, info->ioaddr + UART_MCR);
820
821 /*
822 * Finally, enable interrupts
823 */
824 info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800825
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800826 if (info->board->chip_flag)
827 info->IER |= MOXA_MUST_IER_EGDAI;
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800828 outb(info->IER, info->ioaddr + UART_IER); /* enable interrupts */
829
830 /*
831 * And clear the interrupt registers again for luck.
832 */
833 (void) inb(info->ioaddr + UART_LSR);
834 (void) inb(info->ioaddr + UART_RX);
835 (void) inb(info->ioaddr + UART_IIR);
836 (void) inb(info->ioaddr + UART_MSR);
837
838 if (info->tty)
839 clear_bit(TTY_IO_ERROR, &info->tty->flags);
840 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
841
842 /*
843 * and set the speed of the serial port
844 */
845 spin_unlock_irqrestore(&info->slock, flags);
846 mxser_change_speed(info, NULL);
847
848 info->flags |= ASYNC_INITIALIZED;
849 return 0;
850}
851
852/*
853 * This routine will shutdown a serial port; interrupts maybe disabled, and
854 * DTR is dropped if the hangup on close termio flag is on.
855 */
856static void mxser_shutdown(struct mxser_port *info)
857{
858 unsigned long flags;
859
860 if (!(info->flags & ASYNC_INITIALIZED))
861 return;
862
863 spin_lock_irqsave(&info->slock, flags);
864
865 /*
866 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
867 * here so the queue might never be waken up
868 */
869 wake_up_interruptible(&info->delta_msr_wait);
870
871 /*
872 * Free the IRQ, if necessary
873 */
874 if (info->xmit_buf) {
875 free_page((unsigned long) info->xmit_buf);
876 info->xmit_buf = NULL;
877 }
878
879 info->IER = 0;
880 outb(0x00, info->ioaddr + UART_IER);
881
882 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
883 info->MCR &= ~(UART_MCR_DTR | UART_MCR_RTS);
884 outb(info->MCR, info->ioaddr + UART_MCR);
885
886 /* clear Rx/Tx FIFO's */
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800887 if (info->board->chip_flag)
888 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT |
889 MOXA_MUST_FCR_GDA_MODE_ENABLE,
890 info->ioaddr + UART_FCR);
891 else
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800892 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
893 info->ioaddr + UART_FCR);
894
895 /* read data port to reset things */
896 (void) inb(info->ioaddr + UART_RX);
897
898 if (info->tty)
899 set_bit(TTY_IO_ERROR, &info->tty->flags);
900
901 info->flags &= ~ASYNC_INITIALIZED;
902
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800903 if (info->board->chip_flag)
904 SET_MOXA_MUST_NO_SOFTWARE_FLOW_CONTROL(info->ioaddr);
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800905
906 spin_unlock_irqrestore(&info->slock, flags);
907}
908
Jiri Slaby037ad482006-12-08 02:38:11 -0800909/*
910 * This routine is called whenever a serial port is opened. It
911 * enables interrupts for a serial port, linking in its async structure into
912 * the IRQ chain. It also performs the serial-specific
913 * initialization for the tty structure.
914 */
915static int mxser_open(struct tty_struct *tty, struct file *filp)
916{
Jiri Slaby55b307d2006-12-08 02:38:14 -0800917 struct mxser_port *info;
Jiri Slaby037ad482006-12-08 02:38:11 -0800918 int retval, line;
919
920 /* initialize driver_data in case something fails */
921 tty->driver_data = NULL;
922
923 line = tty->index;
924 if (line == MXSER_PORTS)
925 return 0;
926 if (line < 0 || line > MXSER_PORTS)
927 return -ENODEV;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800928 info = &mxser_boards[line / MXSER_PORTS_PER_BOARD].ports[line % MXSER_PORTS_PER_BOARD];
929 if (!info->ioaddr)
Jiri Slaby037ad482006-12-08 02:38:11 -0800930 return -ENODEV;
931
932 tty->driver_data = info;
933 info->tty = tty;
934 /*
935 * Start up serial port
936 */
Jiri Slaby3306ce32006-12-08 02:38:13 -0800937 info->count++;
Jiri Slaby037ad482006-12-08 02:38:11 -0800938 retval = mxser_startup(info);
939 if (retval)
940 return retval;
941
942 retval = mxser_block_til_ready(tty, filp, info);
943 if (retval)
944 return retval;
945
Jiri Slaby037ad482006-12-08 02:38:11 -0800946 if ((info->count == 1) && (info->flags & ASYNC_SPLIT_TERMIOS)) {
947 if (tty->driver->subtype == SERIAL_TYPE_NORMAL)
948 *tty->termios = info->normal_termios;
949 else
950 *tty->termios = info->callout_termios;
951 mxser_change_speed(info, NULL);
952 }
953
Andrew Morton08a4ae42006-12-08 02:38:12 -0800954 info->session = process_session(current);
Jiri Slaby037ad482006-12-08 02:38:11 -0800955 info->pgrp = process_group(current);
956
Jiri Slabye079f492006-12-08 02:38:31 -0800957 /* unmark here for very high baud rate (ex. 921600 bps) used */
Jiri Slaby037ad482006-12-08 02:38:11 -0800958 tty->low_latency = 1;
959 return 0;
960}
961
962/*
963 * This routine is called when the serial port gets closed. First, we
964 * wait for the last remaining data to be sent. Then, we unlink its
965 * async structure from the interrupt chain if necessary, and we free
966 * that IRQ if nothing is left in the chain.
967 */
968static void mxser_close(struct tty_struct *tty, struct file *filp)
969{
Jiri Slaby55b307d2006-12-08 02:38:14 -0800970 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -0800971
972 unsigned long timeout;
973 unsigned long flags;
Jiri Slaby037ad482006-12-08 02:38:11 -0800974
975 if (tty->index == MXSER_PORTS)
976 return;
977 if (!info)
978 return;
979
980 spin_lock_irqsave(&info->slock, flags);
981
982 if (tty_hung_up_p(filp)) {
983 spin_unlock_irqrestore(&info->slock, flags);
984 return;
985 }
986 if ((tty->count == 1) && (info->count != 1)) {
987 /*
988 * Uh, oh. tty->count is 1, which means that the tty
989 * structure will be freed. Info->count should always
990 * be one in these conditions. If it's greater than
991 * one, we've got real problems, since it means the
992 * serial port won't be shutdown.
993 */
994 printk(KERN_ERR "mxser_close: bad serial port count; "
995 "tty->count is 1, info->count is %d\n", info->count);
996 info->count = 1;
997 }
998 if (--info->count < 0) {
999 printk(KERN_ERR "mxser_close: bad serial port count for "
Jiri Slaby55b307d2006-12-08 02:38:14 -08001000 "ttys%d: %d\n", tty->index, info->count);
Jiri Slaby037ad482006-12-08 02:38:11 -08001001 info->count = 0;
1002 }
1003 if (info->count) {
1004 spin_unlock_irqrestore(&info->slock, flags);
1005 return;
1006 }
1007 info->flags |= ASYNC_CLOSING;
1008 spin_unlock_irqrestore(&info->slock, flags);
1009 /*
1010 * Save the termios structure, since this port may have
1011 * separate termios for callout and dialin.
1012 */
1013 if (info->flags & ASYNC_NORMAL_ACTIVE)
1014 info->normal_termios = *tty->termios;
1015 /*
1016 * Now we wait for the transmit buffer to clear; and we notify
1017 * the line discipline to only process XON/XOFF characters.
1018 */
1019 tty->closing = 1;
1020 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1021 tty_wait_until_sent(tty, info->closing_wait);
1022 /*
1023 * At this point we stop accepting input. To do this, we
1024 * disable the receive line status interrupts, and tell the
1025 * interrupt driver to stop checking the data ready bit in the
1026 * line status register.
1027 */
1028 info->IER &= ~UART_IER_RLSI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001029 if (info->board->chip_flag)
Jiri Slaby037ad482006-12-08 02:38:11 -08001030 info->IER &= ~MOXA_MUST_RECV_ISR;
Jiri Slabye079f492006-12-08 02:38:31 -08001031
Jiri Slaby037ad482006-12-08 02:38:11 -08001032 if (info->flags & ASYNC_INITIALIZED) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08001033 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001034 /*
1035 * Before we drop DTR, make sure the UART transmitter
1036 * has completely drained; this is especially
1037 * important if there is a transmit FIFO!
1038 */
1039 timeout = jiffies + HZ;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001040 while (!(inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001041 schedule_timeout_interruptible(5);
1042 if (time_after(jiffies, timeout))
1043 break;
1044 }
1045 }
1046 mxser_shutdown(info);
1047
1048 if (tty->driver->flush_buffer)
1049 tty->driver->flush_buffer(tty);
1050
Jiri Slaby7e8bcf92006-12-08 02:38:24 -08001051 tty_ldisc_flush(tty);
Jiri Slaby037ad482006-12-08 02:38:11 -08001052
1053 tty->closing = 0;
1054 info->event = 0;
1055 info->tty = NULL;
1056 if (info->blocked_open) {
1057 if (info->close_delay)
1058 schedule_timeout_interruptible(info->close_delay);
1059 wake_up_interruptible(&info->open_wait);
1060 }
1061
1062 info->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
1063 wake_up_interruptible(&info->close_wait);
1064
1065}
1066
1067static int mxser_write(struct tty_struct *tty, const unsigned char *buf, int count)
1068{
1069 int c, total = 0;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001070 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001071 unsigned long flags;
1072
1073 if (!info->xmit_buf)
1074 return 0;
1075
1076 while (1) {
1077 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1078 SERIAL_XMIT_SIZE - info->xmit_head));
1079 if (c <= 0)
1080 break;
1081
1082 memcpy(info->xmit_buf + info->xmit_head, buf, c);
1083 spin_lock_irqsave(&info->slock, flags);
1084 info->xmit_head = (info->xmit_head + c) &
1085 (SERIAL_XMIT_SIZE - 1);
1086 info->xmit_cnt += c;
1087 spin_unlock_irqrestore(&info->slock, flags);
1088
1089 buf += c;
1090 count -= c;
1091 total += c;
1092 }
1093
Jiri Slabye079f492006-12-08 02:38:31 -08001094 if (info->xmit_cnt && !tty->stopped) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001095 if (!tty->hw_stopped ||
1096 (info->type == PORT_16550A) ||
Jiri Slaby55b307d2006-12-08 02:38:14 -08001097 (info->board->chip_flag)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001098 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby55b307d2006-12-08 02:38:14 -08001099 outb(info->IER & ~UART_IER_THRI, info->ioaddr +
1100 UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001101 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001102 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001103 spin_unlock_irqrestore(&info->slock, flags);
1104 }
1105 }
1106 return total;
1107}
1108
1109static void mxser_put_char(struct tty_struct *tty, unsigned char ch)
1110{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001111 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001112 unsigned long flags;
1113
1114 if (!info->xmit_buf)
1115 return;
1116
1117 if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1)
1118 return;
1119
1120 spin_lock_irqsave(&info->slock, flags);
1121 info->xmit_buf[info->xmit_head++] = ch;
1122 info->xmit_head &= SERIAL_XMIT_SIZE - 1;
1123 info->xmit_cnt++;
1124 spin_unlock_irqrestore(&info->slock, flags);
Jiri Slabye079f492006-12-08 02:38:31 -08001125 if (!tty->stopped) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001126 if (!tty->hw_stopped ||
1127 (info->type == PORT_16550A) ||
Jiri Slaby55b307d2006-12-08 02:38:14 -08001128 info->board->chip_flag) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001129 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby55b307d2006-12-08 02:38:14 -08001130 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001131 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001132 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001133 spin_unlock_irqrestore(&info->slock, flags);
1134 }
1135 }
1136}
1137
1138
1139static void mxser_flush_chars(struct tty_struct *tty)
1140{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001141 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001142 unsigned long flags;
1143
1144 if (info->xmit_cnt <= 0 ||
1145 tty->stopped ||
1146 !info->xmit_buf ||
1147 (tty->hw_stopped &&
1148 (info->type != PORT_16550A) &&
Jiri Slaby55b307d2006-12-08 02:38:14 -08001149 (!info->board->chip_flag)
Jiri Slaby037ad482006-12-08 02:38:11 -08001150 ))
1151 return;
1152
1153 spin_lock_irqsave(&info->slock, flags);
1154
Jiri Slaby55b307d2006-12-08 02:38:14 -08001155 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001156 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001157 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001158
1159 spin_unlock_irqrestore(&info->slock, flags);
1160}
1161
1162static int mxser_write_room(struct tty_struct *tty)
1163{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001164 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001165 int ret;
1166
1167 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1168 if (ret < 0)
1169 ret = 0;
1170 return ret;
1171}
1172
1173static int mxser_chars_in_buffer(struct tty_struct *tty)
1174{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001175 struct mxser_port *info = tty->driver_data;
Jiri Slaby925e9c12006-12-08 02:38:30 -08001176 return info->xmit_cnt;
Jiri Slaby037ad482006-12-08 02:38:11 -08001177}
1178
1179static void mxser_flush_buffer(struct tty_struct *tty)
1180{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001181 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001182 char fcr;
1183 unsigned long flags;
1184
1185
1186 spin_lock_irqsave(&info->slock, flags);
1187 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1188
Jiri Slaby55b307d2006-12-08 02:38:14 -08001189 fcr = inb(info->ioaddr + UART_FCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08001190 outb((fcr | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
Jiri Slaby55b307d2006-12-08 02:38:14 -08001191 info->ioaddr + UART_FCR);
1192 outb(fcr, info->ioaddr + UART_FCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08001193
1194 spin_unlock_irqrestore(&info->slock, flags);
Jiri Slaby037ad482006-12-08 02:38:11 -08001195
Jiri Slaby7e8bcf92006-12-08 02:38:24 -08001196 tty_wakeup(tty);
Jiri Slaby037ad482006-12-08 02:38:11 -08001197}
1198
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001199/*
1200 * ------------------------------------------------------------
1201 * friends of mxser_ioctl()
1202 * ------------------------------------------------------------
1203 */
1204static int mxser_get_serial_info(struct mxser_port *info,
1205 struct serial_struct __user *retinfo)
1206{
1207 struct serial_struct tmp;
1208
1209 if (!retinfo)
1210 return -EFAULT;
1211 memset(&tmp, 0, sizeof(tmp));
1212 tmp.type = info->type;
1213 tmp.line = info->tty->index;
1214 tmp.port = info->ioaddr;
1215 tmp.irq = info->board->irq;
1216 tmp.flags = info->flags;
1217 tmp.baud_base = info->baud_base;
1218 tmp.close_delay = info->close_delay;
1219 tmp.closing_wait = info->closing_wait;
1220 tmp.custom_divisor = info->custom_divisor;
1221 tmp.hub6 = 0;
1222 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1223 return -EFAULT;
1224 return 0;
1225}
1226
1227static int mxser_set_serial_info(struct mxser_port *info,
1228 struct serial_struct __user *new_info)
1229{
1230 struct serial_struct new_serial;
1231 unsigned int flags;
1232 int retval = 0;
1233
1234 if (!new_info || !info->ioaddr)
1235 return -EFAULT;
1236 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
1237 return -EFAULT;
1238
1239 if ((new_serial.irq != info->board->irq) ||
1240 (new_serial.port != info->ioaddr) ||
1241 (new_serial.custom_divisor != info->custom_divisor) ||
1242 (new_serial.baud_base != info->baud_base))
1243 return -EPERM;
1244
1245 flags = info->flags & ASYNC_SPD_MASK;
1246
1247 if (!capable(CAP_SYS_ADMIN)) {
1248 if ((new_serial.baud_base != info->baud_base) ||
1249 (new_serial.close_delay != info->close_delay) ||
1250 ((new_serial.flags & ~ASYNC_USR_MASK) != (info->flags & ~ASYNC_USR_MASK)))
1251 return -EPERM;
1252 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
1253 (new_serial.flags & ASYNC_USR_MASK));
1254 } else {
1255 /*
1256 * OK, past this point, all the error checking has been done.
1257 * At this point, we start making changes.....
1258 */
1259 info->flags = ((info->flags & ~ASYNC_FLAGS) |
1260 (new_serial.flags & ASYNC_FLAGS));
1261 info->close_delay = new_serial.close_delay * HZ / 100;
1262 info->closing_wait = new_serial.closing_wait * HZ / 100;
1263 info->tty->low_latency =
1264 (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
Jiri Slabye079f492006-12-08 02:38:31 -08001265 info->tty->low_latency = 0;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001266 }
1267
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001268 info->type = new_serial.type;
1269
1270 process_txrx_fifo(info);
1271
1272 if (info->flags & ASYNC_INITIALIZED) {
1273 if (flags != (info->flags & ASYNC_SPD_MASK))
1274 mxser_change_speed(info, NULL);
1275 } else
1276 retval = mxser_startup(info);
1277
1278 return retval;
1279}
1280
1281/*
1282 * mxser_get_lsr_info - get line status register info
1283 *
1284 * Purpose: Let user call ioctl() to get info when the UART physically
1285 * is emptied. On bus types like RS485, the transmitter must
1286 * release the bus after transmitting. This must be done when
1287 * the transmit shift register is empty, not be done when the
1288 * transmit holding register is empty. This functionality
1289 * allows an RS485 driver to be written in user space.
1290 */
1291static int mxser_get_lsr_info(struct mxser_port *info,
1292 unsigned int __user *value)
1293{
1294 unsigned char status;
1295 unsigned int result;
1296 unsigned long flags;
1297
1298 spin_lock_irqsave(&info->slock, flags);
1299 status = inb(info->ioaddr + UART_LSR);
1300 spin_unlock_irqrestore(&info->slock, flags);
1301 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1302 return put_user(result, value);
1303}
1304
1305/*
1306 * This routine sends a break character out the serial port.
1307 */
1308static void mxser_send_break(struct mxser_port *info, int duration)
1309{
1310 unsigned long flags;
1311
1312 if (!info->ioaddr)
1313 return;
1314 set_current_state(TASK_INTERRUPTIBLE);
1315 spin_lock_irqsave(&info->slock, flags);
1316 outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
1317 info->ioaddr + UART_LCR);
1318 spin_unlock_irqrestore(&info->slock, flags);
1319 schedule_timeout(duration);
1320 spin_lock_irqsave(&info->slock, flags);
1321 outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
1322 info->ioaddr + UART_LCR);
1323 spin_unlock_irqrestore(&info->slock, flags);
1324}
1325
1326static int mxser_tiocmget(struct tty_struct *tty, struct file *file)
Jiri Slaby037ad482006-12-08 02:38:11 -08001327{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001328 struct mxser_port *info = tty->driver_data;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001329 unsigned char control, status;
Jiri Slaby037ad482006-12-08 02:38:11 -08001330 unsigned long flags;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001331
Jiri Slaby037ad482006-12-08 02:38:11 -08001332
1333 if (tty->index == MXSER_PORTS)
Jiri Slaby037ad482006-12-08 02:38:11 -08001334 return -ENOIOCTLCMD;
Jiri Slaby214efeb2006-12-08 02:38:25 -08001335 if (test_bit(TTY_IO_ERROR, &tty->flags))
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001336 return -EIO;
1337
1338 control = info->MCR;
1339
1340 spin_lock_irqsave(&info->slock, flags);
1341 status = inb(info->ioaddr + UART_MSR);
1342 if (status & UART_MSR_ANY_DELTA)
1343 mxser_check_modem_status(info, status);
1344 spin_unlock_irqrestore(&info->slock, flags);
1345 return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0) |
1346 ((control & UART_MCR_DTR) ? TIOCM_DTR : 0) |
1347 ((status & UART_MSR_DCD) ? TIOCM_CAR : 0) |
1348 ((status & UART_MSR_RI) ? TIOCM_RNG : 0) |
1349 ((status & UART_MSR_DSR) ? TIOCM_DSR : 0) |
1350 ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
1351}
1352
1353static int mxser_tiocmset(struct tty_struct *tty, struct file *file,
1354 unsigned int set, unsigned int clear)
1355{
1356 struct mxser_port *info = tty->driver_data;
1357 unsigned long flags;
1358
1359
1360 if (tty->index == MXSER_PORTS)
1361 return -ENOIOCTLCMD;
Jiri Slaby214efeb2006-12-08 02:38:25 -08001362 if (test_bit(TTY_IO_ERROR, &tty->flags))
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001363 return -EIO;
1364
1365 spin_lock_irqsave(&info->slock, flags);
1366
1367 if (set & TIOCM_RTS)
1368 info->MCR |= UART_MCR_RTS;
1369 if (set & TIOCM_DTR)
1370 info->MCR |= UART_MCR_DTR;
1371
1372 if (clear & TIOCM_RTS)
1373 info->MCR &= ~UART_MCR_RTS;
1374 if (clear & TIOCM_DTR)
1375 info->MCR &= ~UART_MCR_DTR;
1376
1377 outb(info->MCR, info->ioaddr + UART_MCR);
1378 spin_unlock_irqrestore(&info->slock, flags);
Jiri Slaby037ad482006-12-08 02:38:11 -08001379 return 0;
1380}
1381
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001382static int mxser_program_mode(int port)
1383{
1384 int id, i, j, n;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001385
1386 spin_lock(&gm_lock);
1387 outb(0, port);
1388 outb(0, port);
1389 outb(0, port);
1390 (void)inb(port);
1391 (void)inb(port);
1392 outb(0, port);
1393 (void)inb(port);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001394 spin_unlock(&gm_lock);
1395
1396 id = inb(port + 1) & 0x1F;
1397 if ((id != C168_ASIC_ID) &&
1398 (id != C104_ASIC_ID) &&
1399 (id != C102_ASIC_ID) &&
1400 (id != CI132_ASIC_ID) &&
1401 (id != CI134_ASIC_ID) &&
1402 (id != CI104J_ASIC_ID))
1403 return -1;
1404 for (i = 0, j = 0; i < 4; i++) {
1405 n = inb(port + 2);
1406 if (n == 'M') {
1407 j = 1;
1408 } else if ((j == 1) && (n == 1)) {
1409 j = 2;
1410 break;
1411 } else
1412 j = 0;
1413 }
1414 if (j != 2)
1415 id = -2;
1416 return id;
1417}
1418
1419static void mxser_normal_mode(int port)
1420{
1421 int i, n;
1422
1423 outb(0xA5, port + 1);
1424 outb(0x80, port + 3);
1425 outb(12, port + 0); /* 9600 bps */
1426 outb(0, port + 1);
1427 outb(0x03, port + 3); /* 8 data bits */
1428 outb(0x13, port + 4); /* loop back mode */
1429 for (i = 0; i < 16; i++) {
1430 n = inb(port + 5);
1431 if ((n & 0x61) == 0x60)
1432 break;
1433 if ((n & 1) == 1)
1434 (void)inb(port);
1435 }
1436 outb(0x00, port + 4);
1437}
1438
1439#define CHIP_SK 0x01 /* Serial Data Clock in Eprom */
1440#define CHIP_DO 0x02 /* Serial Data Output in Eprom */
1441#define CHIP_CS 0x04 /* Serial Chip Select in Eprom */
1442#define CHIP_DI 0x08 /* Serial Data Input in Eprom */
1443#define EN_CCMD 0x000 /* Chip's command register */
1444#define EN0_RSARLO 0x008 /* Remote start address reg 0 */
1445#define EN0_RSARHI 0x009 /* Remote start address reg 1 */
1446#define EN0_RCNTLO 0x00A /* Remote byte count reg WR */
1447#define EN0_RCNTHI 0x00B /* Remote byte count reg WR */
1448#define EN0_DCFG 0x00E /* Data configuration reg WR */
1449#define EN0_PORT 0x010 /* Rcv missed frame error counter RD */
1450#define ENC_PAGE0 0x000 /* Select page 0 of chip registers */
1451#define ENC_PAGE3 0x0C0 /* Select page 3 of chip registers */
1452static int mxser_read_register(int port, unsigned short *regs)
1453{
1454 int i, k, value, id;
1455 unsigned int j;
1456
1457 id = mxser_program_mode(port);
1458 if (id < 0)
1459 return id;
1460 for (i = 0; i < 14; i++) {
1461 k = (i & 0x3F) | 0x180;
1462 for (j = 0x100; j > 0; j >>= 1) {
1463 outb(CHIP_CS, port);
1464 if (k & j) {
1465 outb(CHIP_CS | CHIP_DO, port);
1466 outb(CHIP_CS | CHIP_DO | CHIP_SK, port); /* A? bit of read */
1467 } else {
1468 outb(CHIP_CS, port);
1469 outb(CHIP_CS | CHIP_SK, port); /* A? bit of read */
1470 }
1471 }
1472 (void)inb(port);
1473 value = 0;
1474 for (k = 0, j = 0x8000; k < 16; k++, j >>= 1) {
1475 outb(CHIP_CS, port);
1476 outb(CHIP_CS | CHIP_SK, port);
1477 if (inb(port) & CHIP_DI)
1478 value |= j;
1479 }
1480 regs[i] = value;
1481 outb(0, port);
1482 }
1483 mxser_normal_mode(port);
1484 return id;
1485}
1486
Jiri Slaby037ad482006-12-08 02:38:11 -08001487static int mxser_ioctl_special(unsigned int cmd, void __user *argp)
1488{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001489 struct mxser_port *port;
1490 int result, status;
1491 unsigned int i, j;
Jiri Slaby037ad482006-12-08 02:38:11 -08001492
1493 switch (cmd) {
1494 case MOXA_GET_CONF:
Jiri Slaby55b307d2006-12-08 02:38:14 -08001495/* if (copy_to_user(argp, mxsercfg,
Jiri Slaby037ad482006-12-08 02:38:11 -08001496 sizeof(struct mxser_hwconf) * 4))
1497 return -EFAULT;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001498 return 0;*/
1499 return -ENXIO;
Jiri Slaby037ad482006-12-08 02:38:11 -08001500 case MOXA_GET_MAJOR:
1501 if (copy_to_user(argp, &ttymajor, sizeof(int)))
1502 return -EFAULT;
1503 return 0;
1504
1505 case MOXA_GET_CUMAJOR:
1506 if (copy_to_user(argp, &calloutmajor, sizeof(int)))
1507 return -EFAULT;
1508 return 0;
1509
1510 case MOXA_CHKPORTENABLE:
1511 result = 0;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001512
1513 for (i = 0; i < MXSER_BOARDS; i++)
1514 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++)
1515 if (mxser_boards[i].ports[j].ioaddr)
1516 result |= (1 << i);
1517
Jiri Slaby037ad482006-12-08 02:38:11 -08001518 return put_user(result, (unsigned long __user *)argp);
1519 case MOXA_GETDATACOUNT:
1520 if (copy_to_user(argp, &mxvar_log, sizeof(mxvar_log)))
1521 return -EFAULT;
1522 return 0;
1523 case MOXA_GETMSTATUS:
Jiri Slaby55b307d2006-12-08 02:38:14 -08001524 for (i = 0; i < MXSER_BOARDS; i++)
1525 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
1526 port = &mxser_boards[i].ports[j];
1527
1528 GMStatus[i].ri = 0;
1529 if (!port->ioaddr) {
1530 GMStatus[i].dcd = 0;
1531 GMStatus[i].dsr = 0;
1532 GMStatus[i].cts = 0;
1533 continue;
1534 }
1535
1536 if (!port->tty || !port->tty->termios)
1537 GMStatus[i].cflag =
1538 port->normal_termios.c_cflag;
1539 else
1540 GMStatus[i].cflag =
1541 port->tty->termios->c_cflag;
1542
1543 status = inb(port->ioaddr + UART_MSR);
1544 if (status & 0x80 /*UART_MSR_DCD */ )
1545 GMStatus[i].dcd = 1;
1546 else
1547 GMStatus[i].dcd = 0;
1548
1549 if (status & 0x20 /*UART_MSR_DSR */ )
1550 GMStatus[i].dsr = 1;
1551 else
1552 GMStatus[i].dsr = 0;
1553
1554
1555 if (status & 0x10 /*UART_MSR_CTS */ )
1556 GMStatus[i].cts = 1;
1557 else
1558 GMStatus[i].cts = 0;
Jiri Slaby037ad482006-12-08 02:38:11 -08001559 }
Jiri Slaby037ad482006-12-08 02:38:11 -08001560 if (copy_to_user(argp, GMStatus,
1561 sizeof(struct mxser_mstatus) * MXSER_PORTS))
1562 return -EFAULT;
1563 return 0;
1564 case MOXA_ASPP_MON_EXT: {
Jiri Slaby55b307d2006-12-08 02:38:14 -08001565 int status, p, shiftbit;
1566 unsigned long opmode;
1567 unsigned cflag, iflag;
Jiri Slaby037ad482006-12-08 02:38:11 -08001568
Jiri Slaby55b307d2006-12-08 02:38:14 -08001569 for (i = 0; i < MXSER_BOARDS; i++)
1570 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
1571 port = &mxser_boards[i].ports[j];
1572 if (!port->ioaddr)
Jiri Slaby037ad482006-12-08 02:38:11 -08001573 continue;
1574
Jiri Slaby55b307d2006-12-08 02:38:14 -08001575 status = mxser_get_msr(port->ioaddr, 0, i);
Jiri Slaby55b307d2006-12-08 02:38:14 -08001576
Jiri Slaby037ad482006-12-08 02:38:11 -08001577 if (status & UART_MSR_TERI)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001578 port->icount.rng++;
Jiri Slaby037ad482006-12-08 02:38:11 -08001579 if (status & UART_MSR_DDSR)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001580 port->icount.dsr++;
Jiri Slaby037ad482006-12-08 02:38:11 -08001581 if (status & UART_MSR_DDCD)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001582 port->icount.dcd++;
Jiri Slaby037ad482006-12-08 02:38:11 -08001583 if (status & UART_MSR_DCTS)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001584 port->icount.cts++;
Jiri Slaby037ad482006-12-08 02:38:11 -08001585
Jiri Slaby55b307d2006-12-08 02:38:14 -08001586 port->mon_data.modem_status = status;
1587 mon_data_ext.rx_cnt[i] = port->mon_data.rxcnt;
1588 mon_data_ext.tx_cnt[i] = port->mon_data.txcnt;
1589 mon_data_ext.up_rxcnt[i] =
1590 port->mon_data.up_rxcnt;
1591 mon_data_ext.up_txcnt[i] =
1592 port->mon_data.up_txcnt;
1593 mon_data_ext.modem_status[i] =
1594 port->mon_data.modem_status;
1595 mon_data_ext.baudrate[i] = port->realbaud;
Jiri Slaby037ad482006-12-08 02:38:11 -08001596
Jiri Slaby55b307d2006-12-08 02:38:14 -08001597 if (!port->tty || !port->tty->termios) {
1598 cflag = port->normal_termios.c_cflag;
1599 iflag = port->normal_termios.c_iflag;
Jiri Slaby037ad482006-12-08 02:38:11 -08001600 } else {
Jiri Slaby55b307d2006-12-08 02:38:14 -08001601 cflag = port->tty->termios->c_cflag;
1602 iflag = port->tty->termios->c_iflag;
Jiri Slaby037ad482006-12-08 02:38:11 -08001603 }
1604
1605 mon_data_ext.databits[i] = cflag & CSIZE;
1606
1607 mon_data_ext.stopbits[i] = cflag & CSTOPB;
1608
Jiri Slaby55b307d2006-12-08 02:38:14 -08001609 mon_data_ext.parity[i] =
1610 cflag & (PARENB | PARODD | CMSPAR);
Jiri Slaby037ad482006-12-08 02:38:11 -08001611
1612 mon_data_ext.flowctrl[i] = 0x00;
1613
1614 if (cflag & CRTSCTS)
1615 mon_data_ext.flowctrl[i] |= 0x03;
1616
1617 if (iflag & (IXON | IXOFF))
1618 mon_data_ext.flowctrl[i] |= 0x0C;
1619
Jiri Slaby55b307d2006-12-08 02:38:14 -08001620 if (port->type == PORT_16550A)
Jiri Slaby037ad482006-12-08 02:38:11 -08001621 mon_data_ext.fifo[i] = 1;
1622 else
1623 mon_data_ext.fifo[i] = 0;
1624
1625 p = i % 4;
1626 shiftbit = p * 2;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001627 opmode = inb(port->opmode_ioaddr) >> shiftbit;
Jiri Slaby037ad482006-12-08 02:38:11 -08001628 opmode &= OP_MODE_MASK;
1629
1630 mon_data_ext.iftype[i] = opmode;
1631
1632 }
Jiri Slaby55b307d2006-12-08 02:38:14 -08001633 if (copy_to_user(argp, &mon_data_ext,
1634 sizeof(mon_data_ext)))
Jiri Slaby037ad482006-12-08 02:38:11 -08001635 return -EFAULT;
1636
1637 return 0;
1638
Jiri Slaby55b307d2006-12-08 02:38:14 -08001639 } default:
Jiri Slaby037ad482006-12-08 02:38:11 -08001640 return -ENOIOCTLCMD;
1641 }
1642 return 0;
1643}
1644
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001645static int mxser_ioctl(struct tty_struct *tty, struct file *file,
1646 unsigned int cmd, unsigned long arg)
1647{
1648 struct mxser_port *info = tty->driver_data;
1649 struct async_icount cprev, cnow; /* kernel counter temps */
1650 struct serial_icounter_struct __user *p_cuser;
1651 unsigned long templ;
1652 unsigned long flags;
1653 void __user *argp = (void __user *)arg;
1654 int retval;
1655
1656 if (tty->index == MXSER_PORTS)
1657 return mxser_ioctl_special(cmd, argp);
1658
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001659 if (cmd == MOXA_SET_OP_MODE || cmd == MOXA_GET_OP_MODE) {
1660 int p;
1661 unsigned long opmode;
1662 static unsigned char ModeMask[] = { 0xfc, 0xf3, 0xcf, 0x3f };
1663 int shiftbit;
1664 unsigned char val, mask;
1665
1666 p = tty->index % 4;
1667 if (cmd == MOXA_SET_OP_MODE) {
1668 if (get_user(opmode, (int __user *) argp))
1669 return -EFAULT;
1670 if (opmode != RS232_MODE &&
1671 opmode != RS485_2WIRE_MODE &&
1672 opmode != RS422_MODE &&
1673 opmode != RS485_4WIRE_MODE)
1674 return -EFAULT;
1675 mask = ModeMask[p];
1676 shiftbit = p * 2;
1677 val = inb(info->opmode_ioaddr);
1678 val &= mask;
1679 val |= (opmode << shiftbit);
1680 outb(val, info->opmode_ioaddr);
1681 } else {
1682 shiftbit = p * 2;
1683 opmode = inb(info->opmode_ioaddr) >> shiftbit;
1684 opmode &= OP_MODE_MASK;
1685 if (copy_to_user(argp, &opmode, sizeof(int)))
1686 return -EFAULT;
1687 }
1688 return 0;
1689 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001690
Jiri Slaby214efeb2006-12-08 02:38:25 -08001691 if (cmd != TIOCGSERIAL && cmd != TIOCMIWAIT && cmd != TIOCGICOUNT &&
1692 test_bit(TTY_IO_ERROR, &tty->flags))
1693 return -EIO;
1694
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001695 switch (cmd) {
1696 case TCSBRK: /* SVID version: non-zero arg --> no break */
1697 retval = tty_check_change(tty);
1698 if (retval)
1699 return retval;
1700 tty_wait_until_sent(tty, 0);
1701 if (!arg)
1702 mxser_send_break(info, HZ / 4); /* 1/4 second */
1703 return 0;
1704 case TCSBRKP: /* support for POSIX tcsendbreak() */
1705 retval = tty_check_change(tty);
1706 if (retval)
1707 return retval;
1708 tty_wait_until_sent(tty, 0);
1709 mxser_send_break(info, arg ? arg * (HZ / 10) : HZ / 4);
1710 return 0;
1711 case TIOCGSOFTCAR:
1712 return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *)argp);
1713 case TIOCSSOFTCAR:
1714 if (get_user(templ, (unsigned long __user *) argp))
1715 return -EFAULT;
1716 arg = templ;
1717 tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) | (arg ? CLOCAL : 0));
1718 return 0;
1719 case TIOCGSERIAL:
1720 return mxser_get_serial_info(info, argp);
1721 case TIOCSSERIAL:
1722 return mxser_set_serial_info(info, argp);
1723 case TIOCSERGETLSR: /* Get line status register */
1724 return mxser_get_lsr_info(info, argp);
1725 /*
1726 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1727 * - mask passed in arg for lines of interest
1728 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1729 * Caller should use TIOCGICOUNT to see which one it was
1730 */
1731 case TIOCMIWAIT: {
1732 DECLARE_WAITQUEUE(wait, current);
1733 int ret;
1734 spin_lock_irqsave(&info->slock, flags);
1735 cprev = info->icount; /* note the counters on entry */
1736 spin_unlock_irqrestore(&info->slock, flags);
1737
1738 add_wait_queue(&info->delta_msr_wait, &wait);
1739 while (1) {
1740 spin_lock_irqsave(&info->slock, flags);
1741 cnow = info->icount; /* atomic copy */
1742 spin_unlock_irqrestore(&info->slock, flags);
1743
1744 set_current_state(TASK_INTERRUPTIBLE);
1745 if (((arg & TIOCM_RNG) &&
1746 (cnow.rng != cprev.rng)) ||
1747 ((arg & TIOCM_DSR) &&
1748 (cnow.dsr != cprev.dsr)) ||
1749 ((arg & TIOCM_CD) &&
1750 (cnow.dcd != cprev.dcd)) ||
1751 ((arg & TIOCM_CTS) &&
1752 (cnow.cts != cprev.cts))) {
1753 ret = 0;
1754 break;
1755 }
1756 /* see if a signal did it */
1757 if (signal_pending(current)) {
1758 ret = -ERESTARTSYS;
1759 break;
1760 }
1761 cprev = cnow;
1762 }
1763 current->state = TASK_RUNNING;
1764 remove_wait_queue(&info->delta_msr_wait, &wait);
1765 break;
1766 }
1767 /* NOTREACHED */
1768 /*
1769 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1770 * Return: write counters to the user passed counter struct
1771 * NB: both 1->0 and 0->1 transitions are counted except for
1772 * RI where only 0->1 is counted.
1773 */
1774 case TIOCGICOUNT:
1775 spin_lock_irqsave(&info->slock, flags);
1776 cnow = info->icount;
1777 spin_unlock_irqrestore(&info->slock, flags);
1778 p_cuser = argp;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001779 if (put_user(cnow.frame, &p_cuser->frame))
1780 return -EFAULT;
1781 if (put_user(cnow.brk, &p_cuser->brk))
1782 return -EFAULT;
1783 if (put_user(cnow.overrun, &p_cuser->overrun))
1784 return -EFAULT;
1785 if (put_user(cnow.buf_overrun, &p_cuser->buf_overrun))
1786 return -EFAULT;
1787 if (put_user(cnow.parity, &p_cuser->parity))
1788 return -EFAULT;
1789 if (put_user(cnow.rx, &p_cuser->rx))
1790 return -EFAULT;
1791 if (put_user(cnow.tx, &p_cuser->tx))
1792 return -EFAULT;
1793 put_user(cnow.cts, &p_cuser->cts);
1794 put_user(cnow.dsr, &p_cuser->dsr);
1795 put_user(cnow.rng, &p_cuser->rng);
1796 put_user(cnow.dcd, &p_cuser->dcd);
1797 return 0;
1798 case MOXA_HighSpeedOn:
1799 return put_user(info->baud_base != 115200 ? 1 : 0, (int __user *)argp);
1800 case MOXA_SDS_RSTICOUNTER:
1801 info->mon_data.rxcnt = 0;
1802 info->mon_data.txcnt = 0;
1803 return 0;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001804 case MOXA_ASPP_SETBAUD:{
1805 long baud;
1806 if (get_user(baud, (long __user *)argp))
1807 return -EFAULT;
1808 mxser_set_baud(info, baud);
1809 return 0;
1810 }
1811 case MOXA_ASPP_GETBAUD:
1812 if (copy_to_user(argp, &info->realbaud, sizeof(long)))
1813 return -EFAULT;
1814
1815 return 0;
1816
1817 case MOXA_ASPP_OQUEUE:{
1818 int len, lsr;
1819
1820 len = mxser_chars_in_buffer(tty);
1821
1822 lsr = inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT;
1823
1824 len += (lsr ? 0 : 1);
1825
1826 if (copy_to_user(argp, &len, sizeof(int)))
1827 return -EFAULT;
1828
1829 return 0;
1830 }
1831 case MOXA_ASPP_MON: {
1832 int mcr, status;
1833
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001834 status = mxser_get_msr(info->ioaddr, 1, tty->index);
1835 mxser_check_modem_status(info, status);
1836
1837 mcr = inb(info->ioaddr + UART_MCR);
1838 if (mcr & MOXA_MUST_MCR_XON_FLAG)
1839 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFHOLD;
1840 else
1841 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFHOLD;
1842
1843 if (mcr & MOXA_MUST_MCR_TX_XON)
1844 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFXENT;
1845 else
1846 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFXENT;
1847
1848 if (info->tty->hw_stopped)
1849 info->mon_data.hold_reason |= NPPI_NOTIFY_CTSHOLD;
1850 else
1851 info->mon_data.hold_reason &= ~NPPI_NOTIFY_CTSHOLD;
1852
1853 if (copy_to_user(argp, &info->mon_data,
1854 sizeof(struct mxser_mon)))
1855 return -EFAULT;
1856
1857 return 0;
1858 }
1859 case MOXA_ASPP_LSTATUS: {
1860 if (copy_to_user(argp, &info->err_shadow,
1861 sizeof(unsigned char)))
1862 return -EFAULT;
1863
1864 info->err_shadow = 0;
1865 return 0;
1866 }
1867 case MOXA_SET_BAUD_METHOD: {
1868 int method;
1869
1870 if (get_user(method, (int __user *)argp))
1871 return -EFAULT;
1872 mxser_set_baud_method[tty->index] = method;
1873 if (copy_to_user(argp, &method, sizeof(int)))
1874 return -EFAULT;
1875
1876 return 0;
1877 }
1878 default:
1879 return -ENOIOCTLCMD;
1880 }
1881 return 0;
1882}
1883
Jiri Slaby037ad482006-12-08 02:38:11 -08001884static void mxser_stoprx(struct tty_struct *tty)
1885{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001886 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001887
1888 info->ldisc_stop_rx = 1;
1889 if (I_IXOFF(tty)) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08001890 if (info->board->chip_flag) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001891 info->IER &= ~MOXA_MUST_RECV_ISR;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001892 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby925e9c12006-12-08 02:38:30 -08001893 } else {
Jiri Slaby037ad482006-12-08 02:38:11 -08001894 info->x_char = STOP_CHAR(tty);
Jiri Slaby55b307d2006-12-08 02:38:14 -08001895 outb(0, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001896 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001897 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001898 }
Jiri Slaby037ad482006-12-08 02:38:11 -08001899 }
1900
1901 if (info->tty->termios->c_cflag & CRTSCTS) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001902 info->MCR &= ~UART_MCR_RTS;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001903 outb(info->MCR, info->ioaddr + UART_MCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08001904 }
1905}
1906
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001907/*
1908 * This routine is called by the upper-layer tty layer to signal that
1909 * incoming characters should be throttled.
1910 */
1911static void mxser_throttle(struct tty_struct *tty)
1912{
1913 mxser_stoprx(tty);
1914}
1915
1916static void mxser_unthrottle(struct tty_struct *tty)
Jiri Slaby037ad482006-12-08 02:38:11 -08001917{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001918 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001919
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001920 /* startrx */
Jiri Slaby037ad482006-12-08 02:38:11 -08001921 info->ldisc_stop_rx = 0;
1922 if (I_IXOFF(tty)) {
1923 if (info->x_char)
1924 info->x_char = 0;
1925 else {
Jiri Slaby55b307d2006-12-08 02:38:14 -08001926 if (info->board->chip_flag) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001927 info->IER |= MOXA_MUST_RECV_ISR;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001928 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby925e9c12006-12-08 02:38:30 -08001929 } else {
Jiri Slaby037ad482006-12-08 02:38:11 -08001930 info->x_char = START_CHAR(tty);
Jiri Slaby55b307d2006-12-08 02:38:14 -08001931 outb(0, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001932 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001933 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001934 }
Jiri Slaby037ad482006-12-08 02:38:11 -08001935 }
1936 }
1937
1938 if (info->tty->termios->c_cflag & CRTSCTS) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001939 info->MCR |= UART_MCR_RTS;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001940 outb(info->MCR, info->ioaddr + UART_MCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08001941 }
1942}
1943
1944/*
Jiri Slaby037ad482006-12-08 02:38:11 -08001945 * mxser_stop() and mxser_start()
1946 *
1947 * This routines are called before setting or resetting tty->stopped.
1948 * They enable or disable transmitter interrupts, as necessary.
1949 */
1950static void mxser_stop(struct tty_struct *tty)
1951{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001952 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001953 unsigned long flags;
1954
1955 spin_lock_irqsave(&info->slock, flags);
1956 if (info->IER & UART_IER_THRI) {
1957 info->IER &= ~UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001958 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001959 }
1960 spin_unlock_irqrestore(&info->slock, flags);
1961}
1962
1963static void mxser_start(struct tty_struct *tty)
1964{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001965 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001966 unsigned long flags;
1967
1968 spin_lock_irqsave(&info->slock, flags);
Jiri Slabye079f492006-12-08 02:38:31 -08001969 if (info->xmit_cnt && info->xmit_buf) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08001970 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001971 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001972 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001973 }
1974 spin_unlock_irqrestore(&info->slock, flags);
1975}
1976
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001977static void mxser_set_termios(struct tty_struct *tty, struct termios *old_termios)
1978{
1979 struct mxser_port *info = tty->driver_data;
1980 unsigned long flags;
1981
1982 if ((tty->termios->c_cflag != old_termios->c_cflag) ||
1983 (RELEVANT_IFLAG(tty->termios->c_iflag) != RELEVANT_IFLAG(old_termios->c_iflag))) {
1984
1985 mxser_change_speed(info, old_termios);
1986
1987 if ((old_termios->c_cflag & CRTSCTS) &&
1988 !(tty->termios->c_cflag & CRTSCTS)) {
1989 tty->hw_stopped = 0;
1990 mxser_start(tty);
1991 }
1992 }
1993
Jiri Slabye079f492006-12-08 02:38:31 -08001994 /* Handle sw stopped */
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001995 if ((old_termios->c_iflag & IXON) &&
1996 !(tty->termios->c_iflag & IXON)) {
1997 tty->stopped = 0;
1998
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001999 if (info->board->chip_flag) {
2000 spin_lock_irqsave(&info->slock, flags);
2001 DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
2002 spin_unlock_irqrestore(&info->slock, flags);
2003 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002004
2005 mxser_start(tty);
2006 }
2007}
2008
Jiri Slaby037ad482006-12-08 02:38:11 -08002009/*
2010 * mxser_wait_until_sent() --- wait until the transmitter is empty
2011 */
2012static void mxser_wait_until_sent(struct tty_struct *tty, int timeout)
2013{
Jiri Slaby55b307d2006-12-08 02:38:14 -08002014 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08002015 unsigned long orig_jiffies, char_time;
2016 int lsr;
2017
2018 if (info->type == PORT_UNKNOWN)
2019 return;
2020
2021 if (info->xmit_fifo_size == 0)
2022 return; /* Just in case.... */
2023
2024 orig_jiffies = jiffies;
2025 /*
2026 * Set the check interval to be 1/5 of the estimated time to
2027 * send a single character, and make it at least 1. The check
2028 * interval should also be less than the timeout.
2029 *
2030 * Note: we have to use pretty tight timings here to satisfy
2031 * the NIST-PCTS.
2032 */
2033 char_time = (info->timeout - HZ / 50) / info->xmit_fifo_size;
2034 char_time = char_time / 5;
2035 if (char_time == 0)
2036 char_time = 1;
2037 if (timeout && timeout < char_time)
2038 char_time = timeout;
2039 /*
2040 * If the transmitter hasn't cleared in twice the approximate
2041 * amount of time to send the entire FIFO, it probably won't
2042 * ever clear. This assumes the UART isn't doing flow
2043 * control, which is currently the case. Hence, if it ever
2044 * takes longer than info->timeout, this is probably due to a
2045 * UART bug of some kind. So, we clamp the timeout parameter at
2046 * 2*info->timeout.
2047 */
2048 if (!timeout || timeout > 2 * info->timeout)
2049 timeout = 2 * info->timeout;
2050#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2051 printk(KERN_DEBUG "In rs_wait_until_sent(%d) check=%lu...",
2052 timeout, char_time);
2053 printk("jiff=%lu...", jiffies);
2054#endif
Jiri Slaby55b307d2006-12-08 02:38:14 -08002055 while (!((lsr = inb(info->ioaddr + UART_LSR)) & UART_LSR_TEMT)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002056#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2057 printk("lsr = %d (jiff=%lu)...", lsr, jiffies);
2058#endif
2059 schedule_timeout_interruptible(char_time);
2060 if (signal_pending(current))
2061 break;
2062 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2063 break;
2064 }
2065 set_current_state(TASK_RUNNING);
2066
2067#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2068 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
2069#endif
2070}
2071
Jiri Slaby037ad482006-12-08 02:38:11 -08002072/*
2073 * This routine is called by tty_hangup() when a hangup is signaled.
2074 */
2075void mxser_hangup(struct tty_struct *tty)
2076{
Jiri Slaby55b307d2006-12-08 02:38:14 -08002077 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08002078
2079 mxser_flush_buffer(tty);
2080 mxser_shutdown(info);
2081 info->event = 0;
2082 info->count = 0;
2083 info->flags &= ~ASYNC_NORMAL_ACTIVE;
2084 info->tty = NULL;
2085 wake_up_interruptible(&info->open_wait);
2086}
2087
Jiri Slaby037ad482006-12-08 02:38:11 -08002088/*
2089 * mxser_rs_break() --- routine which turns the break handling on or off
2090 */
2091static void mxser_rs_break(struct tty_struct *tty, int break_state)
2092{
Jiri Slaby55b307d2006-12-08 02:38:14 -08002093 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08002094 unsigned long flags;
2095
2096 spin_lock_irqsave(&info->slock, flags);
2097 if (break_state == -1)
Jiri Slaby55b307d2006-12-08 02:38:14 -08002098 outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
2099 info->ioaddr + UART_LCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002100 else
Jiri Slaby55b307d2006-12-08 02:38:14 -08002101 outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
2102 info->ioaddr + UART_LCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002103 spin_unlock_irqrestore(&info->slock, flags);
2104}
2105
Jiri Slaby55b307d2006-12-08 02:38:14 -08002106static void mxser_receive_chars(struct mxser_port *port, int *status)
Jiri Slaby037ad482006-12-08 02:38:11 -08002107{
Jiri Slaby55b307d2006-12-08 02:38:14 -08002108 struct tty_struct *tty = port->tty;
Jiri Slaby037ad482006-12-08 02:38:11 -08002109 unsigned char ch, gdl;
2110 int ignored = 0;
2111 int cnt = 0;
2112 int recv_room;
2113 int max = 256;
2114 unsigned long flags;
2115
Jiri Slaby55b307d2006-12-08 02:38:14 -08002116 spin_lock_irqsave(&port->slock, flags);
Jiri Slaby037ad482006-12-08 02:38:11 -08002117
2118 recv_room = tty->receive_room;
Jiri Slabye079f492006-12-08 02:38:31 -08002119 if ((recv_room == 0) && (!port->ldisc_stop_rx))
Jiri Slaby037ad482006-12-08 02:38:11 -08002120 mxser_stoprx(tty);
Jiri Slaby037ad482006-12-08 02:38:11 -08002121
Jiri Slaby55b307d2006-12-08 02:38:14 -08002122 if (port->board->chip_flag != MOXA_OTHER_UART) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002123
Jiri Slaby3306ce32006-12-08 02:38:13 -08002124 if (*status & UART_LSR_SPECIAL)
Jiri Slaby037ad482006-12-08 02:38:11 -08002125 goto intr_old;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002126 if (port->board->chip_flag == MOXA_MUST_MU860_HWID &&
Jiri Slaby037ad482006-12-08 02:38:11 -08002127 (*status & MOXA_MUST_LSR_RERR))
2128 goto intr_old;
Jiri Slaby037ad482006-12-08 02:38:11 -08002129 if (*status & MOXA_MUST_LSR_RERR)
2130 goto intr_old;
2131
Jiri Slaby55b307d2006-12-08 02:38:14 -08002132 gdl = inb(port->ioaddr + MOXA_MUST_GDL_REGISTER);
Jiri Slaby037ad482006-12-08 02:38:11 -08002133
Jiri Slaby55b307d2006-12-08 02:38:14 -08002134 if (port->board->chip_flag == MOXA_MUST_MU150_HWID)
Jiri Slaby037ad482006-12-08 02:38:11 -08002135 gdl &= MOXA_MUST_GDL_MASK;
2136 if (gdl >= recv_room) {
Jiri Slabye079f492006-12-08 02:38:31 -08002137 if (!port->ldisc_stop_rx)
Jiri Slaby037ad482006-12-08 02:38:11 -08002138 mxser_stoprx(tty);
Jiri Slaby037ad482006-12-08 02:38:11 -08002139 }
2140 while (gdl--) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002141 ch = inb(port->ioaddr + UART_RX);
Jiri Slaby037ad482006-12-08 02:38:11 -08002142 tty_insert_flip_char(tty, ch, 0);
2143 cnt++;
Jiri Slaby037ad482006-12-08 02:38:11 -08002144 }
2145 goto end_intr;
2146 }
Jiri Slabye079f492006-12-08 02:38:31 -08002147intr_old:
Jiri Slaby037ad482006-12-08 02:38:11 -08002148
2149 do {
2150 if (max-- < 0)
2151 break;
Jiri Slaby037ad482006-12-08 02:38:11 -08002152
Jiri Slaby55b307d2006-12-08 02:38:14 -08002153 ch = inb(port->ioaddr + UART_RX);
Jiri Slabye079f492006-12-08 02:38:31 -08002154 if (port->board->chip_flag && (*status & UART_LSR_OE))
Jiri Slaby55b307d2006-12-08 02:38:14 -08002155 outb(0x23, port->ioaddr + UART_FCR);
2156 *status &= port->read_status_mask;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002157 if (*status & port->ignore_status_mask) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002158 if (++ignored > 100)
2159 break;
2160 } else {
2161 char flag = 0;
2162 if (*status & UART_LSR_SPECIAL) {
2163 if (*status & UART_LSR_BI) {
2164 flag = TTY_BREAK;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002165 port->icount.brk++;
Jiri Slaby3306ce32006-12-08 02:38:13 -08002166
Jiri Slaby55b307d2006-12-08 02:38:14 -08002167 if (port->flags & ASYNC_SAK)
Jiri Slaby037ad482006-12-08 02:38:11 -08002168 do_SAK(tty);
2169 } else if (*status & UART_LSR_PE) {
2170 flag = TTY_PARITY;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002171 port->icount.parity++;
Jiri Slaby037ad482006-12-08 02:38:11 -08002172 } else if (*status & UART_LSR_FE) {
2173 flag = TTY_FRAME;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002174 port->icount.frame++;
Jiri Slaby037ad482006-12-08 02:38:11 -08002175 } else if (*status & UART_LSR_OE) {
2176 flag = TTY_OVERRUN;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002177 port->icount.overrun++;
Jiri Slaby925e9c12006-12-08 02:38:30 -08002178 }
2179 }
Jiri Slaby037ad482006-12-08 02:38:11 -08002180 tty_insert_flip_char(tty, ch, flag);
2181 cnt++;
2182 if (cnt >= recv_room) {
Jiri Slabye079f492006-12-08 02:38:31 -08002183 if (!port->ldisc_stop_rx)
Jiri Slaby037ad482006-12-08 02:38:11 -08002184 mxser_stoprx(tty);
Jiri Slaby037ad482006-12-08 02:38:11 -08002185 break;
2186 }
2187
2188 }
2189
Jiri Slaby55b307d2006-12-08 02:38:14 -08002190 if (port->board->chip_flag)
Jiri Slaby037ad482006-12-08 02:38:11 -08002191 break;
Jiri Slaby037ad482006-12-08 02:38:11 -08002192
Jiri Slaby55b307d2006-12-08 02:38:14 -08002193 *status = inb(port->ioaddr + UART_LSR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002194 } while (*status & UART_LSR_DR);
2195
Jiri Slabye079f492006-12-08 02:38:31 -08002196end_intr:
Jiri Slaby55b307d2006-12-08 02:38:14 -08002197 mxvar_log.rxcnt[port->tty->index] += cnt;
2198 port->mon_data.rxcnt += cnt;
2199 port->mon_data.up_rxcnt += cnt;
2200 spin_unlock_irqrestore(&port->slock, flags);
Jiri Slaby037ad482006-12-08 02:38:11 -08002201
2202 tty_flip_buffer_push(tty);
2203}
2204
Jiri Slaby55b307d2006-12-08 02:38:14 -08002205static void mxser_transmit_chars(struct mxser_port *port)
Jiri Slaby037ad482006-12-08 02:38:11 -08002206{
2207 int count, cnt;
2208 unsigned long flags;
2209
Jiri Slaby55b307d2006-12-08 02:38:14 -08002210 spin_lock_irqsave(&port->slock, flags);
Jiri Slaby037ad482006-12-08 02:38:11 -08002211
Jiri Slaby55b307d2006-12-08 02:38:14 -08002212 if (port->x_char) {
2213 outb(port->x_char, port->ioaddr + UART_TX);
2214 port->x_char = 0;
2215 mxvar_log.txcnt[port->tty->index]++;
2216 port->mon_data.txcnt++;
2217 port->mon_data.up_txcnt++;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002218 port->icount.tx++;
Jiri Slaby3306ce32006-12-08 02:38:13 -08002219 goto unlock;
Jiri Slaby037ad482006-12-08 02:38:11 -08002220 }
2221
Jiri Slaby55b307d2006-12-08 02:38:14 -08002222 if (port->xmit_buf == 0)
Jiri Slaby3306ce32006-12-08 02:38:13 -08002223 goto unlock;
Jiri Slaby037ad482006-12-08 02:38:11 -08002224
Jiri Slaby925e9c12006-12-08 02:38:30 -08002225 if ((port->xmit_cnt <= 0) || port->tty->stopped ||
2226 (port->tty->hw_stopped &&
Jiri Slaby55b307d2006-12-08 02:38:14 -08002227 (port->type != PORT_16550A) &&
2228 (!port->board->chip_flag))) {
2229 port->IER &= ~UART_IER_THRI;
2230 outb(port->IER, port->ioaddr + UART_IER);
Jiri Slaby3306ce32006-12-08 02:38:13 -08002231 goto unlock;
Jiri Slaby037ad482006-12-08 02:38:11 -08002232 }
2233
Jiri Slaby55b307d2006-12-08 02:38:14 -08002234 cnt = port->xmit_cnt;
2235 count = port->xmit_fifo_size;
Jiri Slaby037ad482006-12-08 02:38:11 -08002236 do {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002237 outb(port->xmit_buf[port->xmit_tail++],
2238 port->ioaddr + UART_TX);
2239 port->xmit_tail = port->xmit_tail & (SERIAL_XMIT_SIZE - 1);
2240 if (--port->xmit_cnt <= 0)
Jiri Slaby037ad482006-12-08 02:38:11 -08002241 break;
2242 } while (--count > 0);
Jiri Slaby55b307d2006-12-08 02:38:14 -08002243 mxvar_log.txcnt[port->tty->index] += (cnt - port->xmit_cnt);
Jiri Slaby037ad482006-12-08 02:38:11 -08002244
Jiri Slaby55b307d2006-12-08 02:38:14 -08002245 port->mon_data.txcnt += (cnt - port->xmit_cnt);
2246 port->mon_data.up_txcnt += (cnt - port->xmit_cnt);
Jiri Slaby55b307d2006-12-08 02:38:14 -08002247 port->icount.tx += (cnt - port->xmit_cnt);
Jiri Slaby037ad482006-12-08 02:38:11 -08002248
Jiri Slaby55b307d2006-12-08 02:38:14 -08002249 if (port->xmit_cnt < WAKEUP_CHARS) {
2250 set_bit(MXSER_EVENT_TXLOW, &port->event);
2251 schedule_work(&port->tqueue);
Jiri Slaby037ad482006-12-08 02:38:11 -08002252 }
Jiri Slaby55b307d2006-12-08 02:38:14 -08002253 if (port->xmit_cnt <= 0) {
2254 port->IER &= ~UART_IER_THRI;
2255 outb(port->IER, port->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08002256 }
Jiri Slaby3306ce32006-12-08 02:38:13 -08002257unlock:
Jiri Slaby55b307d2006-12-08 02:38:14 -08002258 spin_unlock_irqrestore(&port->slock, flags);
Jiri Slaby037ad482006-12-08 02:38:11 -08002259}
2260
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002261/*
2262 * This is the serial driver's generic interrupt routine
2263 */
Jiri Slabyb1d1c8d2006-12-08 02:38:31 -08002264static irqreturn_t mxser_interrupt(int irq, void *dev_id)
Jiri Slaby037ad482006-12-08 02:38:11 -08002265{
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002266 int status, iir, i;
2267 struct mxser_board *brd = NULL;
2268 struct mxser_port *port;
2269 int max, irqbits, bits, msr;
2270 int pass_counter = 0;
2271 unsigned int int_cnt;
2272 int handled = IRQ_NONE;
Jiri Slaby037ad482006-12-08 02:38:11 -08002273
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002274 for (i = 0; i < MXSER_BOARDS; i++)
2275 if (dev_id == &mxser_boards[i]) {
2276 brd = dev_id;
2277 break;
Jiri Slaby037ad482006-12-08 02:38:11 -08002278 }
Jiri Slaby037ad482006-12-08 02:38:11 -08002279
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002280 if (i == MXSER_BOARDS)
2281 goto irq_stop;
2282 if (brd == NULL)
2283 goto irq_stop;
Jiri Slabycd7ed642006-12-08 02:38:29 -08002284 max = brd->info->nports;
Jiri Slaby037ad482006-12-08 02:38:11 -08002285 while (1) {
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002286 irqbits = inb(brd->vector) & brd->vector_mask;
2287 if (irqbits == brd->vector_mask)
Jiri Slaby037ad482006-12-08 02:38:11 -08002288 break;
Jiri Slaby037ad482006-12-08 02:38:11 -08002289
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002290 handled = IRQ_HANDLED;
2291 for (i = 0, bits = 1; i < max; i++, irqbits |= bits, bits <<= 1) {
2292 if (irqbits == brd->vector_mask)
Jiri Slaby037ad482006-12-08 02:38:11 -08002293 break;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002294 if (bits & irqbits)
2295 continue;
2296 port = &brd->ports[i];
Jiri Slaby037ad482006-12-08 02:38:11 -08002297
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002298 int_cnt = 0;
2299 do {
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002300 iir = inb(port->ioaddr + UART_IIR);
2301 if (iir & UART_IIR_NO_INT)
2302 break;
2303 iir &= MOXA_MUST_IIR_MASK;
2304 if (!port->tty) {
2305 status = inb(port->ioaddr + UART_LSR);
2306 outb(0x27, port->ioaddr + UART_FCR);
2307 inb(port->ioaddr + UART_MSR);
2308 break;
Jiri Slaby037ad482006-12-08 02:38:11 -08002309 }
Jiri Slaby037ad482006-12-08 02:38:11 -08002310
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002311 status = inb(port->ioaddr + UART_LSR);
2312
2313 if (status & UART_LSR_PE)
2314 port->err_shadow |= NPPI_NOTIFY_PARITY;
2315 if (status & UART_LSR_FE)
2316 port->err_shadow |= NPPI_NOTIFY_FRAMING;
2317 if (status & UART_LSR_OE)
2318 port->err_shadow |=
2319 NPPI_NOTIFY_HW_OVERRUN;
2320 if (status & UART_LSR_BI)
2321 port->err_shadow |= NPPI_NOTIFY_BREAK;
2322
2323 if (port->board->chip_flag) {
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002324 if (iir == MOXA_MUST_IIR_GDA ||
2325 iir == MOXA_MUST_IIR_RDA ||
2326 iir == MOXA_MUST_IIR_RTO ||
2327 iir == MOXA_MUST_IIR_LSR)
2328 mxser_receive_chars(port,
2329 &status);
2330
2331 } else {
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002332 status &= port->read_status_mask;
2333 if (status & UART_LSR_DR)
2334 mxser_receive_chars(port,
2335 &status);
2336 }
2337 msr = inb(port->ioaddr + UART_MSR);
2338 if (msr & UART_MSR_ANY_DELTA)
2339 mxser_check_modem_status(port, msr);
2340
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002341 if (port->board->chip_flag) {
2342 if (iir == 0x02 && (status &
2343 UART_LSR_THRE))
2344 mxser_transmit_chars(port);
2345 } else {
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002346 if (status & UART_LSR_THRE)
2347 mxser_transmit_chars(port);
2348 }
2349 } while (int_cnt++ < MXSER_ISR_PASS_LIMIT);
2350 }
2351 if (pass_counter++ > MXSER_ISR_PASS_LIMIT)
2352 break; /* Prevent infinite loops */
2353 }
2354
Jiri Slabye079f492006-12-08 02:38:31 -08002355irq_stop:
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002356 return handled;
2357}
2358
2359static const struct tty_operations mxser_ops = {
2360 .open = mxser_open,
2361 .close = mxser_close,
2362 .write = mxser_write,
2363 .put_char = mxser_put_char,
2364 .flush_chars = mxser_flush_chars,
2365 .write_room = mxser_write_room,
2366 .chars_in_buffer = mxser_chars_in_buffer,
2367 .flush_buffer = mxser_flush_buffer,
2368 .ioctl = mxser_ioctl,
2369 .throttle = mxser_throttle,
2370 .unthrottle = mxser_unthrottle,
2371 .set_termios = mxser_set_termios,
2372 .stop = mxser_stop,
2373 .start = mxser_start,
2374 .hangup = mxser_hangup,
2375 .break_ctl = mxser_rs_break,
2376 .wait_until_sent = mxser_wait_until_sent,
2377 .tiocmget = mxser_tiocmget,
2378 .tiocmset = mxser_tiocmset,
2379};
2380
2381/*
2382 * The MOXA Smartio/Industio serial driver boot-time initialization code!
2383 */
2384
Jiri Slaby2094e752006-12-08 02:38:33 -08002385static void mxser_release_res(struct mxser_board *brd, struct pci_dev *pdev,
2386 unsigned int irq)
Jiri Slaby171d3a82006-12-08 02:38:25 -08002387{
Jiri Slaby171d3a82006-12-08 02:38:25 -08002388 if (irq)
2389 free_irq(brd->irq, brd);
2390 if (pdev != NULL) { /* PCI */
2391 pci_release_region(pdev, 2);
2392 pci_release_region(pdev, 3);
2393 pci_dev_put(pdev);
2394 } else {
Jiri Slabycd7ed642006-12-08 02:38:29 -08002395 release_region(brd->ports[0].ioaddr, 8 * brd->info->nports);
Jiri Slaby171d3a82006-12-08 02:38:25 -08002396 release_region(brd->vector, 1);
2397 }
2398}
2399
Jiri Slaby2094e752006-12-08 02:38:33 -08002400static int __devinit mxser_initbrd(struct mxser_board *brd,
2401 struct pci_dev *pdev)
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002402{
2403 struct mxser_port *info;
2404 unsigned int i;
2405 int retval;
2406
2407 printk(KERN_INFO "max. baud rate = %d bps.\n", brd->ports[0].max_baud);
2408
Jiri Slabycd7ed642006-12-08 02:38:29 -08002409 for (i = 0; i < brd->info->nports; i++) {
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002410 info = &brd->ports[i];
2411 info->board = brd;
2412 info->stop_rx = 0;
2413 info->ldisc_stop_rx = 0;
2414
2415 /* Enhance mode enabled here */
2416 if (brd->chip_flag != MOXA_OTHER_UART)
2417 ENABLE_MOXA_MUST_ENCHANCE_MODE(info->ioaddr);
2418
2419 info->flags = ASYNC_SHARE_IRQ;
2420 info->type = brd->uart_type;
2421
2422 process_txrx_fifo(info);
2423
2424 info->custom_divisor = info->baud_base * 16;
2425 info->close_delay = 5 * HZ / 10;
2426 info->closing_wait = 30 * HZ;
2427 INIT_WORK(&info->tqueue, mxser_do_softint, info);
2428 info->normal_termios = mxvar_sdriver->init_termios;
2429 init_waitqueue_head(&info->open_wait);
2430 init_waitqueue_head(&info->close_wait);
2431 init_waitqueue_head(&info->delta_msr_wait);
2432 memset(&info->mon_data, 0, sizeof(struct mxser_mon));
2433 info->err_shadow = 0;
2434 spin_lock_init(&info->slock);
2435
2436 /* before set INT ISR, disable all int */
2437 outb(inb(info->ioaddr + UART_IER) & 0xf0,
2438 info->ioaddr + UART_IER);
2439 }
Jiri Slaby037ad482006-12-08 02:38:11 -08002440 /*
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002441 * Allocate the IRQ if necessary
Jiri Slaby037ad482006-12-08 02:38:11 -08002442 */
Jiri Slaby037ad482006-12-08 02:38:11 -08002443
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002444 retval = request_irq(brd->irq, mxser_interrupt,
2445 (brd->ports[0].flags & ASYNC_SHARE_IRQ) ? IRQF_SHARED :
2446 IRQF_DISABLED, "mxser", brd);
2447 if (retval) {
2448 printk(KERN_ERR "Board %s: Request irq failed, IRQ (%d) may "
2449 "conflict with another device.\n",
Jiri Slabycd7ed642006-12-08 02:38:29 -08002450 brd->info->name, brd->irq);
Jiri Slaby171d3a82006-12-08 02:38:25 -08002451 /* We hold resources, we need to release them. */
Jiri Slaby2094e752006-12-08 02:38:33 -08002452 mxser_release_res(brd, pdev, 0);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002453 return retval;
Jiri Slaby037ad482006-12-08 02:38:11 -08002454 }
Jiri Slaby037ad482006-12-08 02:38:11 -08002455 return 0;
2456}
2457
Jiri Slaby943f2952006-12-08 02:38:15 -08002458static int __init mxser_get_ISA_conf(int cap, struct mxser_board *brd)
Jiri Slaby037ad482006-12-08 02:38:11 -08002459{
2460 int id, i, bits;
2461 unsigned short regs[16], irq;
2462 unsigned char scratch, scratch2;
2463
Jiri Slaby55b307d2006-12-08 02:38:14 -08002464 brd->chip_flag = MOXA_OTHER_UART;
Jiri Slaby037ad482006-12-08 02:38:11 -08002465
2466 id = mxser_read_register(cap, regs);
Jiri Slabycd7ed642006-12-08 02:38:29 -08002467 switch (id) {
2468 case C168_ASIC_ID:
2469 brd->info = &mxser_cards[0];
2470 break;
2471 case C104_ASIC_ID:
2472 brd->info = &mxser_cards[1];
2473 break;
2474 case CI104J_ASIC_ID:
2475 brd->info = &mxser_cards[2];
2476 break;
2477 case C102_ASIC_ID:
2478 brd->info = &mxser_cards[5];
2479 break;
2480 case CI132_ASIC_ID:
2481 brd->info = &mxser_cards[6];
2482 break;
2483 case CI134_ASIC_ID:
2484 brd->info = &mxser_cards[7];
2485 break;
2486 default:
Jiri Slaby037ad482006-12-08 02:38:11 -08002487 return 0;
Jiri Slabycd7ed642006-12-08 02:38:29 -08002488 }
Jiri Slaby037ad482006-12-08 02:38:11 -08002489
2490 irq = 0;
Jiri Slabycd7ed642006-12-08 02:38:29 -08002491 /* some ISA cards have 2 ports, but we want to see them as 4-port (why?)
2492 Flag-hack checks if configuration should be read as 2-port here. */
2493 if (brd->info->nports == 2 || (brd->info->flags & MXSER_HAS2)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002494 irq = regs[9] & 0xF000;
2495 irq = irq | (irq >> 4);
2496 if (irq != (regs[9] & 0xFF00))
2497 return MXSER_ERR_IRQ_CONFLIT;
Jiri Slabycd7ed642006-12-08 02:38:29 -08002498 } else if (brd->info->nports == 4) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002499 irq = regs[9] & 0xF000;
2500 irq = irq | (irq >> 4);
2501 irq = irq | (irq >> 8);
2502 if (irq != regs[9])
2503 return MXSER_ERR_IRQ_CONFLIT;
Jiri Slabycd7ed642006-12-08 02:38:29 -08002504 } else if (brd->info->nports == 8) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002505 irq = regs[9] & 0xF000;
2506 irq = irq | (irq >> 4);
2507 irq = irq | (irq >> 8);
2508 if ((irq != regs[9]) || (irq != regs[10]))
2509 return MXSER_ERR_IRQ_CONFLIT;
2510 }
2511
2512 if (!irq)
2513 return MXSER_ERR_IRQ;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002514 brd->irq = ((int)(irq & 0xF000) >> 12);
Jiri Slaby037ad482006-12-08 02:38:11 -08002515 for (i = 0; i < 8; i++)
Jiri Slaby55b307d2006-12-08 02:38:14 -08002516 brd->ports[i].ioaddr = (int) regs[i + 1] & 0xFFF8;
Jiri Slaby037ad482006-12-08 02:38:11 -08002517 if ((regs[12] & 0x80) == 0)
2518 return MXSER_ERR_VECTOR;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002519 brd->vector = (int)regs[11]; /* interrupt vector */
Jiri Slaby037ad482006-12-08 02:38:11 -08002520 if (id == 1)
Jiri Slaby55b307d2006-12-08 02:38:14 -08002521 brd->vector_mask = 0x00FF;
Jiri Slaby037ad482006-12-08 02:38:11 -08002522 else
Jiri Slaby55b307d2006-12-08 02:38:14 -08002523 brd->vector_mask = 0x000F;
Jiri Slaby037ad482006-12-08 02:38:11 -08002524 for (i = 7, bits = 0x0100; i >= 0; i--, bits <<= 1) {
2525 if (regs[12] & bits) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002526 brd->ports[i].baud_base = 921600;
Jiri Slabye079f492006-12-08 02:38:31 -08002527 brd->ports[i].max_baud = 921600;
Jiri Slaby037ad482006-12-08 02:38:11 -08002528 } else {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002529 brd->ports[i].baud_base = 115200;
Jiri Slabye079f492006-12-08 02:38:31 -08002530 brd->ports[i].max_baud = 115200;
Jiri Slaby037ad482006-12-08 02:38:11 -08002531 }
2532 }
2533 scratch2 = inb(cap + UART_LCR) & (~UART_LCR_DLAB);
2534 outb(scratch2 | UART_LCR_DLAB, cap + UART_LCR);
2535 outb(0, cap + UART_EFR); /* EFR is the same as FCR */
2536 outb(scratch2, cap + UART_LCR);
2537 outb(UART_FCR_ENABLE_FIFO, cap + UART_FCR);
2538 scratch = inb(cap + UART_IIR);
2539
2540 if (scratch & 0xC0)
Jiri Slaby55b307d2006-12-08 02:38:14 -08002541 brd->uart_type = PORT_16550A;
Jiri Slaby037ad482006-12-08 02:38:11 -08002542 else
Jiri Slaby55b307d2006-12-08 02:38:14 -08002543 brd->uart_type = PORT_16450;
Jiri Slabycd7ed642006-12-08 02:38:29 -08002544 if (!request_region(brd->ports[0].ioaddr, 8 * brd->info->nports,
2545 "mxser(IO)"))
Jiri Slaby7a7a5c32006-12-08 02:38:17 -08002546 return MXSER_ERR_IOADDR;
2547 if (!request_region(brd->vector, 1, "mxser(vector)")) {
Jiri Slabycd7ed642006-12-08 02:38:29 -08002548 release_region(brd->ports[0].ioaddr, 8 * brd->info->nports);
Jiri Slaby7a7a5c32006-12-08 02:38:17 -08002549 return MXSER_ERR_VECTOR;
2550 }
Jiri Slabycd7ed642006-12-08 02:38:29 -08002551 return brd->info->nports;
Jiri Slaby037ad482006-12-08 02:38:11 -08002552}
2553
Jiri Slaby2094e752006-12-08 02:38:33 -08002554static int __devinit mxser_probe(struct pci_dev *pdev,
2555 const struct pci_device_id *ent)
Jiri Slaby037ad482006-12-08 02:38:11 -08002556{
Jiri Slaby2094e752006-12-08 02:38:33 -08002557 struct mxser_board *brd;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002558 unsigned int i, j;
2559 unsigned long ioaddress;
Jiri Slaby2094e752006-12-08 02:38:33 -08002560 int retval = -EINVAL;
2561
2562 for (i = 0; i < MXSER_BOARDS; i++)
2563 if (mxser_boards[i].info == NULL)
2564 break;
2565
2566 if (i >= MXSER_BOARDS) {
2567 printk(KERN_ERR "Too many Smartio/Industio family boards found "
2568 "(maximum %d), board not configured\n", MXSER_BOARDS);
2569 goto err;
2570 }
2571
2572 brd = &mxser_boards[i];
2573 brd->idx = i * MXSER_PORTS_PER_BOARD;
2574 printk(KERN_INFO "Found MOXA %s board (BusNo=%d, DevNo=%d)\n",
2575 mxser_cards[ent->driver_data].name,
2576 pdev->bus->number, PCI_SLOT(pdev->devfn));
2577
2578 retval = pci_enable_device(pdev);
2579 if (retval) {
2580 printk(KERN_ERR "Moxa SmartI/O PCI enable fail !\n");
2581 goto err;
2582 }
Jiri Slaby037ad482006-12-08 02:38:11 -08002583
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002584 /* io address */
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002585 ioaddress = pci_resource_start(pdev, 2);
2586 retval = pci_request_region(pdev, 2, "mxser(IO)");
2587 if (retval)
2588 goto err;
2589
Jiri Slaby2094e752006-12-08 02:38:33 -08002590 brd->info = &mxser_cards[ent->driver_data];
Jiri Slabycd7ed642006-12-08 02:38:29 -08002591 for (i = 0; i < brd->info->nports; i++)
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002592 brd->ports[i].ioaddr = ioaddress + 8 * i;
2593
2594 /* vector */
2595 ioaddress = pci_resource_start(pdev, 3);
2596 retval = pci_request_region(pdev, 3, "mxser(vector)");
2597 if (retval)
2598 goto err_relio;
2599 brd->vector = ioaddress;
2600
2601 /* irq */
2602 brd->irq = pdev->irq;
2603
2604 brd->chip_flag = CheckIsMoxaMust(brd->ports[0].ioaddr);
2605 brd->uart_type = PORT_16550A;
2606 brd->vector_mask = 0;
2607
Jiri Slabycd7ed642006-12-08 02:38:29 -08002608 for (i = 0; i < brd->info->nports; i++) {
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002609 for (j = 0; j < UART_INFO_NUM; j++) {
2610 if (Gpci_uart_info[j].type == brd->chip_flag) {
2611 brd->ports[i].max_baud =
2612 Gpci_uart_info[j].max_baud;
2613
2614 /* exception....CP-102 */
Jiri Slabycd7ed642006-12-08 02:38:29 -08002615 if (brd->info->flags & MXSER_HIGHBAUD)
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002616 brd->ports[i].max_baud = 921600;
2617 break;
Jiri Slaby037ad482006-12-08 02:38:11 -08002618 }
2619 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002620 }
2621
2622 if (brd->chip_flag == MOXA_MUST_MU860_HWID) {
Jiri Slabycd7ed642006-12-08 02:38:29 -08002623 for (i = 0; i < brd->info->nports; i++) {
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002624 if (i < 4)
2625 brd->ports[i].opmode_ioaddr = ioaddress + 4;
2626 else
2627 brd->ports[i].opmode_ioaddr = ioaddress + 0x0c;
Jiri Slaby037ad482006-12-08 02:38:11 -08002628 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002629 outb(0, ioaddress + 4); /* default set to RS232 mode */
2630 outb(0, ioaddress + 0x0c); /* default set to RS232 mode */
Jiri Slaby037ad482006-12-08 02:38:11 -08002631 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002632
Jiri Slabycd7ed642006-12-08 02:38:29 -08002633 for (i = 0; i < brd->info->nports; i++) {
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002634 brd->vector_mask |= (1 << i);
2635 brd->ports[i].baud_base = 921600;
2636 }
Jiri Slaby2094e752006-12-08 02:38:33 -08002637
2638 /* mxser_initbrd will hook ISR. */
2639 if (mxser_initbrd(brd, pdev) < 0)
2640 goto err_relvec;
2641
2642 for (i = 0; i < brd->info->nports; i++)
2643 tty_register_device(mxvar_sdriver, brd->idx + i, &pdev->dev);
2644
2645 pci_set_drvdata(pdev, brd);
2646
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002647 return 0;
Jiri Slaby2094e752006-12-08 02:38:33 -08002648err_relvec:
2649 pci_release_region(pdev, 3);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002650err_relio:
2651 pci_release_region(pdev, 2);
Jiri Slaby2094e752006-12-08 02:38:33 -08002652 brd->info = NULL;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002653err:
2654 return retval;
Jiri Slaby037ad482006-12-08 02:38:11 -08002655}
2656
Jiri Slaby2094e752006-12-08 02:38:33 -08002657static void __devexit mxser_remove(struct pci_dev *pdev)
2658{
2659 struct mxser_board *brd = pci_get_drvdata(pdev);
2660 unsigned int i;
2661
2662 for (i = 0; i < brd->info->nports; i++)
2663 tty_unregister_device(mxvar_sdriver, brd->idx + i);
2664
2665 mxser_release_res(brd, pdev, 1);
2666}
2667
2668static struct pci_driver mxser_driver = {
2669 .name = "mxser",
2670 .id_table = mxser_pcibrds,
2671 .probe = mxser_probe,
2672 .remove = __devexit_p(mxser_remove)
2673};
2674
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002675static int __init mxser_module_init(void)
Jiri Slaby037ad482006-12-08 02:38:11 -08002676{
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002677 struct mxser_board *brd;
Jiri Slabyeae44362006-12-08 02:38:27 -08002678 unsigned long cap;
2679 unsigned int i, m, isaloop;
Jiri Slaby2094e752006-12-08 02:38:33 -08002680 int retval, b;
Jiri Slaby037ad482006-12-08 02:38:11 -08002681
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002682 pr_debug("Loading module mxser ...\n");
Jiri Slaby037ad482006-12-08 02:38:11 -08002683
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002684 mxvar_sdriver = alloc_tty_driver(MXSER_PORTS + 1);
2685 if (!mxvar_sdriver)
2686 return -ENOMEM;
2687 spin_lock_init(&gm_lock);
2688
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002689 printk(KERN_INFO "MOXA Smartio/Industio family driver version %s\n",
2690 MXSER_VERSION);
2691
2692 /* Initialize the tty_driver structure */
2693 mxvar_sdriver->magic = TTY_DRIVER_MAGIC;
2694 mxvar_sdriver->name = "ttyM";
2695 mxvar_sdriver->major = ttymajor;
2696 mxvar_sdriver->minor_start = 0;
2697 mxvar_sdriver->num = MXSER_PORTS + 1;
2698 mxvar_sdriver->type = TTY_DRIVER_TYPE_SERIAL;
2699 mxvar_sdriver->subtype = SERIAL_TYPE_NORMAL;
2700 mxvar_sdriver->init_termios = tty_std_termios;
2701 mxvar_sdriver->init_termios.c_cflag = B9600|CS8|CREAD|HUPCL|CLOCAL;
Jiri Slaby938ef182006-12-08 02:38:28 -08002702 mxvar_sdriver->flags = TTY_DRIVER_REAL_RAW|TTY_DRIVER_DYNAMIC_DEV;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002703 tty_set_operations(mxvar_sdriver, &mxser_ops);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002704
Jiri Slaby938ef182006-12-08 02:38:28 -08002705 retval = tty_register_driver(mxvar_sdriver);
2706 if (retval) {
2707 printk(KERN_ERR "Couldn't install MOXA Smartio/Industio family "
2708 "tty driver !\n");
2709 goto err_put;
2710 }
2711
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002712 mxvar_diagflag = 0;
2713
2714 m = 0;
2715 /* Start finding ISA boards here */
Jiri Slabyeae44362006-12-08 02:38:27 -08002716 for (isaloop = 0; isaloop < 2; isaloop++)
2717 for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
2718 if (!isaloop)
2719 cap = mxserBoardCAP[b]; /* predefined */
2720 else
2721 cap = ioaddr[b]; /* module param */
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002722
Jiri Slabyeae44362006-12-08 02:38:27 -08002723 if (!cap)
2724 continue;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002725
Jiri Slabyeae44362006-12-08 02:38:27 -08002726 brd = &mxser_boards[m];
2727 retval = mxser_get_ISA_conf(cap, brd);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002728
Jiri Slabyeae44362006-12-08 02:38:27 -08002729 if (retval != 0)
2730 printk(KERN_INFO "Found MOXA %s board "
2731 "(CAP=0x%x)\n",
Jiri Slabycd7ed642006-12-08 02:38:29 -08002732 brd->info->name, ioaddr[b]);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002733
Jiri Slabyeae44362006-12-08 02:38:27 -08002734 if (retval <= 0) {
2735 if (retval == MXSER_ERR_IRQ)
2736 printk(KERN_ERR "Invalid interrupt "
2737 "number, board not "
2738 "configured\n");
2739 else if (retval == MXSER_ERR_IRQ_CONFLIT)
2740 printk(KERN_ERR "Invalid interrupt "
2741 "number, board not "
2742 "configured\n");
2743 else if (retval == MXSER_ERR_VECTOR)
2744 printk(KERN_ERR "Invalid interrupt "
2745 "vector, board not "
2746 "configured\n");
2747 else if (retval == MXSER_ERR_IOADDR)
2748 printk(KERN_ERR "Invalid I/O address, "
2749 "board not configured\n");
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002750
Jiri Slaby2094e752006-12-08 02:38:33 -08002751 brd->info = NULL;
Jiri Slabyeae44362006-12-08 02:38:27 -08002752 continue;
2753 }
2754
Jiri Slabyeae44362006-12-08 02:38:27 -08002755 /* mxser_initbrd will hook ISR. */
Jiri Slaby2094e752006-12-08 02:38:33 -08002756 if (mxser_initbrd(brd, NULL) < 0) {
2757 brd->info = NULL;
Jiri Slabyeae44362006-12-08 02:38:27 -08002758 continue;
Jiri Slaby2094e752006-12-08 02:38:33 -08002759 }
Jiri Slabyeae44362006-12-08 02:38:27 -08002760
Jiri Slaby2094e752006-12-08 02:38:33 -08002761 brd->idx = m * MXSER_PORTS_PER_BOARD;
Jiri Slabycd7ed642006-12-08 02:38:29 -08002762 for (i = 0; i < brd->info->nports; i++)
Jiri Slaby2094e752006-12-08 02:38:33 -08002763 tty_register_device(mxvar_sdriver, brd->idx + i,
2764 NULL);
Jiri Slaby938ef182006-12-08 02:38:28 -08002765
Jiri Slabyeae44362006-12-08 02:38:27 -08002766 m++;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002767 }
2768
Jiri Slaby2094e752006-12-08 02:38:33 -08002769 retval = pci_register_driver(&mxser_driver);
2770 if (retval) {
2771 printk(KERN_ERR "Can't register pci driver\n");
2772 if (!m) {
2773 retval = -ENODEV;
2774 goto err_unr;
2775 } /* else: we have some ISA cards under control */
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002776 }
2777
2778 pr_debug("Done.\n");
2779
Jiri Slaby938ef182006-12-08 02:38:28 -08002780 return 0;
2781err_unr:
2782 tty_unregister_driver(mxvar_sdriver);
2783err_put:
2784 put_tty_driver(mxvar_sdriver);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002785 return retval;
Jiri Slaby037ad482006-12-08 02:38:11 -08002786}
2787
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002788static void __exit mxser_module_exit(void)
Jiri Slaby037ad482006-12-08 02:38:11 -08002789{
Jiri Slaby2094e752006-12-08 02:38:33 -08002790 unsigned int i, j;
Jiri Slaby037ad482006-12-08 02:38:11 -08002791
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002792 pr_debug("Unloading module mxser ...\n");
2793
Jiri Slaby2094e752006-12-08 02:38:33 -08002794 pci_unregister_driver(&mxser_driver);
2795
2796 for (i = 0; i < MXSER_BOARDS; i++) /* ISA remains */
2797 if (mxser_boards[i].info != NULL)
2798 for (j = 0; j < mxser_boards[i].info->nports; j++)
2799 tty_unregister_device(mxvar_sdriver,
2800 mxser_boards[i].idx + j);
Jiri Slabyead568c2006-12-08 02:38:26 -08002801 tty_unregister_driver(mxvar_sdriver);
2802 put_tty_driver(mxvar_sdriver);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002803
Jiri Slaby171d3a82006-12-08 02:38:25 -08002804 for (i = 0; i < MXSER_BOARDS; i++)
Jiri Slabycd7ed642006-12-08 02:38:29 -08002805 if (mxser_boards[i].info != NULL)
Jiri Slaby2094e752006-12-08 02:38:33 -08002806 mxser_release_res(&mxser_boards[i], NULL, 1);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002807
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002808 pr_debug("Done.\n");
Jiri Slaby037ad482006-12-08 02:38:11 -08002809}
2810
2811module_init(mxser_module_init);
2812module_exit(mxser_module_exit);