blob: 081c84c7b5489ae9b3e80fb2730d8622dc552790 [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 Slabyf8cce2e2007-10-18 03:06:01 -07005 * Copyright (C) 2006-2007 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>
Jiri Slaby037ad482006-12-08 02:38:11 -080040#include <linux/delay.h>
41#include <linux/pci.h>
Jiri Slaby1977f032007-10-18 23:40:25 -070042#include <linux/bitops.h>
Jiri Slaby037ad482006-12-08 02:38:11 -080043
44#include <asm/system.h>
45#include <asm/io.h>
46#include <asm/irq.h>
Jiri Slaby037ad482006-12-08 02:38:11 -080047#include <asm/uaccess.h>
48
Jiri Slaby771f2d12006-12-08 02:38:12 -080049#include "mxser_new.h"
Jiri Slaby037ad482006-12-08 02:38:11 -080050
Jiri Slabyf8cce2e2007-10-18 03:06:01 -070051#define MXSER_VERSION "2.0.2" /* 1.10 */
Jiri Slaby037ad482006-12-08 02:38:11 -080052#define MXSERMAJOR 174
53#define MXSERCUMAJOR 175
54
Jiri Slaby037ad482006-12-08 02:38:11 -080055#define MXSER_BOARDS 4 /* Max. boards */
Jiri Slaby037ad482006-12-08 02:38:11 -080056#define MXSER_PORTS_PER_BOARD 8 /* Max. ports per board */
Jiri Slabya8b74de2006-12-08 02:38:34 -080057#define MXSER_PORTS (MXSER_BOARDS * MXSER_PORTS_PER_BOARD)
Jiri Slaby18b95572007-02-10 01:45:19 -080058#define MXSER_ISR_PASS_LIMIT 100
Jiri Slaby037ad482006-12-08 02:38:11 -080059
60#define MXSER_ERR_IOADDR -1
61#define MXSER_ERR_IRQ -2
62#define MXSER_ERR_IRQ_CONFLIT -3
63#define MXSER_ERR_VECTOR -4
64
Jiri Slabyed79ba12007-02-10 01:45:18 -080065/*CheckIsMoxaMust return value*/
66#define MOXA_OTHER_UART 0x00
67#define MOXA_MUST_MU150_HWID 0x01
68#define MOXA_MUST_MU860_HWID 0x02
69
Jiri Slaby037ad482006-12-08 02:38:11 -080070#define WAKEUP_CHARS 256
71
72#define UART_MCR_AFE 0x20
73#define UART_LSR_SPECIAL 0x1E
74
Jiri Slabyf8cce2e2007-10-18 03:06:01 -070075#define PCI_DEVICE_ID_CB108 0x1080
76#define PCI_DEVICE_ID_CB114 0x1142
77#define PCI_DEVICE_ID_CB134I 0x1341
78#define PCI_DEVICE_ID_CP138U 0x1380
79#define PCI_DEVICE_ID_POS104UL 0x1044
80
Jiri Slaby037ad482006-12-08 02:38:11 -080081
Jiri Slaby037ad482006-12-08 02:38:11 -080082#define C168_ASIC_ID 1
83#define C104_ASIC_ID 2
84#define C102_ASIC_ID 0xB
85#define CI132_ASIC_ID 4
86#define CI134_ASIC_ID 3
87#define CI104J_ASIC_ID 5
88
Jiri Slabycd7ed642006-12-08 02:38:29 -080089#define MXSER_HIGHBAUD 1
90#define MXSER_HAS2 2
Jiri Slaby037ad482006-12-08 02:38:11 -080091
92/* This is only for PCI */
Jiri Slabycd7ed642006-12-08 02:38:29 -080093static const struct {
Jiri Slaby037ad482006-12-08 02:38:11 -080094 int type;
95 int tx_fifo;
96 int rx_fifo;
97 int xmit_fifo_size;
98 int rx_high_water;
99 int rx_trigger;
100 int rx_low_water;
101 long max_baud;
Jiri Slabycd7ed642006-12-08 02:38:29 -0800102} Gpci_uart_info[] = {
Jiri Slaby037ad482006-12-08 02:38:11 -0800103 {MOXA_OTHER_UART, 16, 16, 16, 14, 14, 1, 921600L},
104 {MOXA_MUST_MU150_HWID, 64, 64, 64, 48, 48, 16, 230400L},
105 {MOXA_MUST_MU860_HWID, 128, 128, 128, 96, 96, 32, 921600L}
106};
Jiri Slabycd7ed642006-12-08 02:38:29 -0800107#define UART_INFO_NUM ARRAY_SIZE(Gpci_uart_info)
Jiri Slaby037ad482006-12-08 02:38:11 -0800108
Jiri Slabycd7ed642006-12-08 02:38:29 -0800109struct mxser_cardinfo {
110 unsigned int nports;
111 char *name;
112 unsigned int flags;
113};
114
115static const struct mxser_cardinfo mxser_cards[] = {
Jiri Slaby009ae1d2007-10-18 03:06:02 -0700116/* 0*/ { 8, "C168 series", },
117 { 4, "C104 series", },
118 { 4, "CI-104J series", },
119 { 8, "C168H/PCI series", },
120 { 4, "C104H/PCI series", },
Jiri Slabyf8cce2e2007-10-18 03:06:01 -0700121/* 5*/ { 4, "C102 series", MXSER_HAS2 }, /* C102-ISA */
Jiri Slaby009ae1d2007-10-18 03:06:02 -0700122 { 4, "CI-132 series", MXSER_HAS2 },
123 { 4, "CI-134 series", },
124 { 2, "CP-132 series", },
125 { 4, "CP-114 series", },
126/*10*/ { 4, "CT-114 series", },
127 { 2, "CP-102 series", MXSER_HIGHBAUD },
128 { 4, "CP-104U series", },
129 { 8, "CP-168U series", },
130 { 2, "CP-132U series", },
131/*15*/ { 4, "CP-134U series", },
132 { 4, "CP-104JU series", },
Jiri Slabycd7ed642006-12-08 02:38:29 -0800133 { 8, "Moxa UC7000 Serial", }, /* RC7000 */
Jiri Slaby009ae1d2007-10-18 03:06:02 -0700134 { 8, "CP-118U series", },
135 { 2, "CP-102UL series", },
136/*20*/ { 2, "CP-102U series", },
137 { 8, "CP-118EL series", },
138 { 8, "CP-168EL series", },
Jiri Slabyf8cce2e2007-10-18 03:06:01 -0700139 { 4, "CP-104EL series", },
140 { 8, "CB-108 series", },
141/*25*/ { 4, "CB-114 series", },
142 { 4, "CB-134I series", },
143 { 8, "CP-138U series", },
144 { 4, "POS-104UL series", }
Jiri Slabycd7ed642006-12-08 02:38:29 -0800145};
146
147/* driver_data correspond to the lines in the structure above
148 see also ISA probe function before you change something */
Jiri Slaby037ad482006-12-08 02:38:11 -0800149static struct pci_device_id mxser_pcibrds[] = {
Jiri Slaby931ecbb2007-10-18 03:06:01 -0700150 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_C168), .driver_data = 3 },
151 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_C104), .driver_data = 4 },
152 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP132), .driver_data = 8 },
153 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP114), .driver_data = 9 },
154 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CT114), .driver_data = 10 },
155 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102), .driver_data = 11 },
156 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104U), .driver_data = 12 },
157 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP168U), .driver_data = 13 },
158 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP132U), .driver_data = 14 },
159 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP134U), .driver_data = 15 },
160 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104JU),.driver_data = 16 },
161 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_RC7000), .driver_data = 17 },
162 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP118U), .driver_data = 18 },
163 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102UL),.driver_data = 19 },
164 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102U), .driver_data = 20 },
165 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP118EL),.driver_data = 21 },
166 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP168EL),.driver_data = 22 },
167 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104EL),.driver_data = 23 },
Jiri Slabyf8cce2e2007-10-18 03:06:01 -0700168 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB108), .driver_data = 24 },
169 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB114), .driver_data = 25 },
170 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB134I), .driver_data = 26 },
171 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP138U), .driver_data = 27 },
172 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_POS104UL), .driver_data = 28 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800173 { }
Jiri Slaby037ad482006-12-08 02:38:11 -0800174};
Jiri Slaby037ad482006-12-08 02:38:11 -0800175MODULE_DEVICE_TABLE(pci, mxser_pcibrds);
176
Jiri Slabyf64c84a2007-02-10 01:45:21 -0800177static int mxvar_baud_table[] = {
178 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400,
179 4800, 9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600
180};
181static unsigned int mxvar_baud_table1[] = {
182 0, B50, B75, B110, B134, B150, B200, B300, B600, B1200, B1800, B2400,
183 B4800, B9600, B19200, B38400, B57600, B115200, B230400, B460800, B921600
184};
185#define BAUD_TABLE_NO ARRAY_SIZE(mxvar_baud_table)
186
187#define B_SPEC B2000000
188
Jiri Slaby037ad482006-12-08 02:38:11 -0800189static int ioaddr[MXSER_BOARDS] = { 0, 0, 0, 0 };
190static int ttymajor = MXSERMAJOR;
191static int calloutmajor = MXSERCUMAJOR;
Jiri Slaby037ad482006-12-08 02:38:11 -0800192
193/* Variables for insmod */
194
195MODULE_AUTHOR("Casper Yang");
196MODULE_DESCRIPTION("MOXA Smartio/Industio Family Multiport Board Device Driver");
197module_param_array(ioaddr, int, NULL, 0);
198module_param(ttymajor, int, 0);
Jiri Slaby037ad482006-12-08 02:38:11 -0800199MODULE_LICENSE("GPL");
200
201struct mxser_log {
202 int tick;
203 unsigned long rxcnt[MXSER_PORTS];
204 unsigned long txcnt[MXSER_PORTS];
205};
206
207
208struct mxser_mon {
209 unsigned long rxcnt;
210 unsigned long txcnt;
211 unsigned long up_rxcnt;
212 unsigned long up_txcnt;
213 int modem_status;
214 unsigned char hold_reason;
215};
216
217struct mxser_mon_ext {
218 unsigned long rx_cnt[32];
219 unsigned long tx_cnt[32];
220 unsigned long up_rxcnt[32];
221 unsigned long up_txcnt[32];
222 int modem_status[32];
223
224 long baudrate[32];
225 int databits[32];
226 int stopbits[32];
227 int parity[32];
228 int flowctrl[32];
229 int fifo[32];
230 int iftype[32];
231};
232
Jiri Slaby55b307d2006-12-08 02:38:14 -0800233struct mxser_board;
Jiri Slaby037ad482006-12-08 02:38:11 -0800234
Jiri Slaby55b307d2006-12-08 02:38:14 -0800235struct mxser_port {
236 struct mxser_board *board;
237 struct tty_struct *tty;
238
239 unsigned long ioaddr;
240 unsigned long opmode_ioaddr;
241 int max_baud;
242
Jiri Slaby037ad482006-12-08 02:38:11 -0800243 int rx_high_water;
244 int rx_trigger; /* Rx fifo trigger level */
245 int rx_low_water;
246 int baud_base; /* max. speed */
Jiri Slaby55b307d2006-12-08 02:38:14 -0800247 long realbaud;
Jiri Slaby037ad482006-12-08 02:38:11 -0800248 int type; /* UART type */
Jiri Slaby55b307d2006-12-08 02:38:14 -0800249 int flags; /* defined in tty.h */
Jiri Slabyf64c84a2007-02-10 01:45:21 -0800250 int speed;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800251
252 int x_char; /* xon/xoff character */
253 int IER; /* Interrupt Enable Register */
254 int MCR; /* Modem control register */
255
256 unsigned char stop_rx;
257 unsigned char ldisc_stop_rx;
258
259 int custom_divisor;
260 int close_delay;
261 unsigned short closing_wait;
262 unsigned char err_shadow;
263 unsigned long event;
264
265 int count; /* # of fd on device */
266 int blocked_open; /* # of blocked opens */
267 struct async_icount icount; /* kernel counters for 4 input interrupts */
268 int timeout;
269
Jiri Slaby037ad482006-12-08 02:38:11 -0800270 int read_status_mask;
271 int ignore_status_mask;
272 int xmit_fifo_size;
Jiri Slaby037ad482006-12-08 02:38:11 -0800273 unsigned char *xmit_buf;
274 int xmit_head;
275 int xmit_tail;
276 int xmit_cnt;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800277
Alan Cox606d0992006-12-08 02:38:45 -0800278 struct ktermios normal_termios;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800279
280 struct mxser_mon mon_data;
281
282 spinlock_t slock;
Jiri Slaby037ad482006-12-08 02:38:11 -0800283 wait_queue_head_t open_wait;
Jiri Slaby037ad482006-12-08 02:38:11 -0800284 wait_queue_head_t delta_msr_wait;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800285};
286
287struct mxser_board {
Jiri Slaby2094e752006-12-08 02:38:33 -0800288 unsigned int idx;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800289 int irq;
Jiri Slabycd7ed642006-12-08 02:38:29 -0800290 const struct mxser_cardinfo *info;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800291 unsigned long vector;
292 unsigned long vector_mask;
293
294 int chip_flag;
295 int uart_type;
296
297 struct mxser_port ports[MXSER_PORTS_PER_BOARD];
Jiri Slaby037ad482006-12-08 02:38:11 -0800298};
299
300struct mxser_mstatus {
301 tcflag_t cflag;
302 int cts;
303 int dsr;
304 int ri;
305 int dcd;
306};
307
308static struct mxser_mstatus GMStatus[MXSER_PORTS];
309
310static int mxserBoardCAP[MXSER_BOARDS] = {
311 0, 0, 0, 0
312 /* 0x180, 0x280, 0x200, 0x320 */
313};
314
Jiri Slaby55b307d2006-12-08 02:38:14 -0800315static struct mxser_board mxser_boards[MXSER_BOARDS];
Jiri Slaby037ad482006-12-08 02:38:11 -0800316static struct tty_driver *mxvar_sdriver;
Jiri Slaby037ad482006-12-08 02:38:11 -0800317static struct mxser_log mxvar_log;
318static int mxvar_diagflag;
319static unsigned char mxser_msr[MXSER_PORTS + 1];
320static struct mxser_mon_ext mon_data_ext;
321static int mxser_set_baud_method[MXSER_PORTS + 1];
Jiri Slaby037ad482006-12-08 02:38:11 -0800322
Jiri Slabyfb0c9292006-12-13 00:34:19 -0800323#ifdef CONFIG_PCI
Jiri Slabyc76b0932007-02-10 01:45:07 -0800324static int __devinit CheckIsMoxaMust(int io)
Jiri Slaby037ad482006-12-08 02:38:11 -0800325{
326 u8 oldmcr, hwid;
327 int i;
328
329 outb(0, io + UART_LCR);
330 DISABLE_MOXA_MUST_ENCHANCE_MODE(io);
331 oldmcr = inb(io + UART_MCR);
332 outb(0, io + UART_MCR);
333 SET_MOXA_MUST_XON1_VALUE(io, 0x11);
334 if ((hwid = inb(io + UART_MCR)) != 0) {
335 outb(oldmcr, io + UART_MCR);
336 return MOXA_OTHER_UART;
337 }
338
339 GET_MOXA_MUST_HARDWARE_ID(io, &hwid);
Jiri Slabycd7ed642006-12-08 02:38:29 -0800340 for (i = 1; i < UART_INFO_NUM; i++) { /* 0 = OTHER_UART */
341 if (hwid == Gpci_uart_info[i].type)
Jiri Slaby037ad482006-12-08 02:38:11 -0800342 return (int)hwid;
343 }
344 return MOXA_OTHER_UART;
345}
Jiri Slabyfb0c9292006-12-13 00:34:19 -0800346#endif
Jiri Slaby037ad482006-12-08 02:38:11 -0800347
Jiri Slaby55b307d2006-12-08 02:38:14 -0800348static void process_txrx_fifo(struct mxser_port *info)
Jiri Slaby037ad482006-12-08 02:38:11 -0800349{
350 int i;
351
352 if ((info->type == PORT_16450) || (info->type == PORT_8250)) {
353 info->rx_trigger = 1;
354 info->rx_high_water = 1;
355 info->rx_low_water = 1;
356 info->xmit_fifo_size = 1;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800357 } else
358 for (i = 0; i < UART_INFO_NUM; i++)
359 if (info->board->chip_flag == Gpci_uart_info[i].type) {
Jiri Slaby037ad482006-12-08 02:38:11 -0800360 info->rx_trigger = Gpci_uart_info[i].rx_trigger;
361 info->rx_low_water = Gpci_uart_info[i].rx_low_water;
362 info->rx_high_water = Gpci_uart_info[i].rx_high_water;
363 info->xmit_fifo_size = Gpci_uart_info[i].xmit_fifo_size;
364 break;
365 }
Jiri Slaby037ad482006-12-08 02:38:11 -0800366}
367
Jiri Slaby55b307d2006-12-08 02:38:14 -0800368static unsigned char mxser_get_msr(int baseaddr, int mode, int port)
Jiri Slaby037ad482006-12-08 02:38:11 -0800369{
370 unsigned char status = 0;
371
372 status = inb(baseaddr + UART_MSR);
373
374 mxser_msr[port] &= 0x0F;
375 mxser_msr[port] |= status;
376 status = mxser_msr[port];
377 if (mode)
378 mxser_msr[port] = 0;
379
380 return status;
381}
382
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800383static int mxser_block_til_ready(struct tty_struct *tty, struct file *filp,
384 struct mxser_port *port)
385{
386 DECLARE_WAITQUEUE(wait, current);
387 int retval;
388 int do_clocal = 0;
389 unsigned long flags;
390
391 /*
392 * If non-blocking mode is set, or the port is not enabled,
393 * then make the check up front and then exit.
394 */
Jiri Slaby214efeb2006-12-08 02:38:25 -0800395 if ((filp->f_flags & O_NONBLOCK) ||
396 test_bit(TTY_IO_ERROR, &tty->flags)) {
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800397 port->flags |= ASYNC_NORMAL_ACTIVE;
398 return 0;
399 }
400
401 if (tty->termios->c_cflag & CLOCAL)
402 do_clocal = 1;
403
404 /*
405 * Block waiting for the carrier detect and the line to become
406 * free (i.e., not in use by the callout). While we are in
407 * this loop, port->count is dropped by one, so that
408 * mxser_close() knows when to free things. We restore it upon
409 * exit, either normal or abnormal.
410 */
411 retval = 0;
412 add_wait_queue(&port->open_wait, &wait);
413
414 spin_lock_irqsave(&port->slock, flags);
415 if (!tty_hung_up_p(filp))
416 port->count--;
417 spin_unlock_irqrestore(&port->slock, flags);
418 port->blocked_open++;
419 while (1) {
420 spin_lock_irqsave(&port->slock, flags);
421 outb(inb(port->ioaddr + UART_MCR) |
422 UART_MCR_DTR | UART_MCR_RTS, port->ioaddr + UART_MCR);
423 spin_unlock_irqrestore(&port->slock, flags);
424 set_current_state(TASK_INTERRUPTIBLE);
425 if (tty_hung_up_p(filp) || !(port->flags & ASYNC_INITIALIZED)) {
426 if (port->flags & ASYNC_HUP_NOTIFY)
427 retval = -EAGAIN;
428 else
429 retval = -ERESTARTSYS;
430 break;
431 }
432 if (!(port->flags & ASYNC_CLOSING) &&
433 (do_clocal ||
434 (inb(port->ioaddr + UART_MSR) & UART_MSR_DCD)))
435 break;
436 if (signal_pending(current)) {
437 retval = -ERESTARTSYS;
438 break;
439 }
440 schedule();
441 }
442 set_current_state(TASK_RUNNING);
443 remove_wait_queue(&port->open_wait, &wait);
444 if (!tty_hung_up_p(filp))
445 port->count++;
446 port->blocked_open--;
447 if (retval)
448 return retval;
449 port->flags |= ASYNC_NORMAL_ACTIVE;
450 return 0;
451}
452
453static int mxser_set_baud(struct mxser_port *info, long newspd)
454{
Jiri Slabyf64c84a2007-02-10 01:45:21 -0800455 unsigned int i;
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800456 int quot = 0;
457 unsigned char cval;
458 int ret = 0;
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800459
460 if (!info->tty || !info->tty->termios)
461 return ret;
462
463 if (!(info->ioaddr))
464 return ret;
465
466 if (newspd > info->max_baud)
467 return 0;
468
469 info->realbaud = newspd;
Jiri Slabyf64c84a2007-02-10 01:45:21 -0800470 for (i = 0; i < BAUD_TABLE_NO; i++)
471 if (newspd == mxvar_baud_table[i])
472 break;
473 if (i == BAUD_TABLE_NO) {
474 quot = info->baud_base / info->speed;
475 if (info->speed <= 0 || info->speed > info->max_baud)
476 quot = 0;
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800477 } else {
Jiri Slabyf64c84a2007-02-10 01:45:21 -0800478 if (newspd == 134) {
479 quot = (2 * info->baud_base / 269);
480 } else if (newspd) {
481 quot = info->baud_base / newspd;
482 if (quot == 0)
483 quot = 1;
484 } else {
485 quot = 0;
486 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800487 }
488
489 info->timeout = ((info->xmit_fifo_size * HZ * 10 * quot) / info->baud_base);
490 info->timeout += HZ / 50; /* Add .02 seconds of slop */
491
492 if (quot) {
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800493 info->MCR |= UART_MCR_DTR;
494 outb(info->MCR, info->ioaddr + UART_MCR);
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800495 } else {
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800496 info->MCR &= ~UART_MCR_DTR;
497 outb(info->MCR, info->ioaddr + UART_MCR);
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800498 return ret;
499 }
500
501 cval = inb(info->ioaddr + UART_LCR);
502
503 outb(cval | UART_LCR_DLAB, info->ioaddr + UART_LCR); /* set DLAB */
504
505 outb(quot & 0xff, info->ioaddr + UART_DLL); /* LS of divisor */
506 outb(quot >> 8, info->ioaddr + UART_DLM); /* MS of divisor */
507 outb(cval, info->ioaddr + UART_LCR); /* reset DLAB */
508
Jiri Slabyf64c84a2007-02-10 01:45:21 -0800509 if (i == BAUD_TABLE_NO) {
510 quot = info->baud_base % info->speed;
511 quot *= 8;
512 if ((quot % info->speed) > (info->speed / 2)) {
513 quot /= info->speed;
514 quot++;
515 } else {
516 quot /= info->speed;
517 }
518 SET_MOXA_MUST_ENUM_VALUE(info->ioaddr, quot);
519 } else
520 SET_MOXA_MUST_ENUM_VALUE(info->ioaddr, 0);
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800521
522 return ret;
523}
524
525/*
526 * This routine is called to set the UART divisor registers to match
527 * the specified baud rate for a serial port.
528 */
529static int mxser_change_speed(struct mxser_port *info,
Alan Cox606d0992006-12-08 02:38:45 -0800530 struct ktermios *old_termios)
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800531{
532 unsigned cflag, cval, fcr;
533 int ret = 0;
534 unsigned char status;
535 long baud;
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800536
537 if (!info->tty || !info->tty->termios)
538 return ret;
539 cflag = info->tty->termios->c_cflag;
540 if (!(info->ioaddr))
541 return ret;
542
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800543 if (mxser_set_baud_method[info->tty->index] == 0) {
Jiri Slabyf64c84a2007-02-10 01:45:21 -0800544 if ((cflag & CBAUD) == B_SPEC)
545 baud = info->speed;
546 else
547 baud = tty_get_baud_rate(info->tty);
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800548 mxser_set_baud(info, baud);
549 }
550
551 /* byte size and parity */
552 switch (cflag & CSIZE) {
553 case CS5:
554 cval = 0x00;
555 break;
556 case CS6:
557 cval = 0x01;
558 break;
559 case CS7:
560 cval = 0x02;
561 break;
562 case CS8:
563 cval = 0x03;
564 break;
565 default:
566 cval = 0x00;
567 break; /* too keep GCC shut... */
568 }
569 if (cflag & CSTOPB)
570 cval |= 0x04;
571 if (cflag & PARENB)
572 cval |= UART_LCR_PARITY;
573 if (!(cflag & PARODD))
574 cval |= UART_LCR_EPAR;
575 if (cflag & CMSPAR)
576 cval |= UART_LCR_SPAR;
577
578 if ((info->type == PORT_8250) || (info->type == PORT_16450)) {
579 if (info->board->chip_flag) {
580 fcr = UART_FCR_ENABLE_FIFO;
581 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
582 SET_MOXA_MUST_FIFO_VALUE(info);
583 } else
584 fcr = 0;
585 } else {
586 fcr = UART_FCR_ENABLE_FIFO;
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800587 if (info->board->chip_flag) {
588 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
589 SET_MOXA_MUST_FIFO_VALUE(info);
590 } else {
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800591 switch (info->rx_trigger) {
592 case 1:
593 fcr |= UART_FCR_TRIGGER_1;
594 break;
595 case 4:
596 fcr |= UART_FCR_TRIGGER_4;
597 break;
598 case 8:
599 fcr |= UART_FCR_TRIGGER_8;
600 break;
601 default:
602 fcr |= UART_FCR_TRIGGER_14;
603 break;
604 }
605 }
606 }
607
608 /* CTS flow control flag and modem status interrupts */
609 info->IER &= ~UART_IER_MSI;
610 info->MCR &= ~UART_MCR_AFE;
611 if (cflag & CRTSCTS) {
612 info->flags |= ASYNC_CTS_FLOW;
613 info->IER |= UART_IER_MSI;
614 if ((info->type == PORT_16550A) || (info->board->chip_flag)) {
615 info->MCR |= UART_MCR_AFE;
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800616 } else {
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800617 status = inb(info->ioaddr + UART_MSR);
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800618 if (info->tty->hw_stopped) {
619 if (status & UART_MSR_CTS) {
620 info->tty->hw_stopped = 0;
621 if (info->type != PORT_16550A &&
622 !info->board->chip_flag) {
623 outb(info->IER & ~UART_IER_THRI,
624 info->ioaddr +
625 UART_IER);
626 info->IER |= UART_IER_THRI;
627 outb(info->IER, info->ioaddr +
628 UART_IER);
629 }
Jiri Slaby47c85c02007-02-10 01:45:15 -0800630 tty_wakeup(info->tty);
631 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800632 } else {
633 if (!(status & UART_MSR_CTS)) {
634 info->tty->hw_stopped = 1;
635 if ((info->type != PORT_16550A) &&
636 (!info->board->chip_flag)) {
637 info->IER &= ~UART_IER_THRI;
638 outb(info->IER, info->ioaddr +
639 UART_IER);
640 }
641 }
642 }
643 }
644 } else {
645 info->flags &= ~ASYNC_CTS_FLOW;
646 }
647 outb(info->MCR, info->ioaddr + UART_MCR);
648 if (cflag & CLOCAL) {
649 info->flags &= ~ASYNC_CHECK_CD;
650 } else {
651 info->flags |= ASYNC_CHECK_CD;
652 info->IER |= UART_IER_MSI;
653 }
654 outb(info->IER, info->ioaddr + UART_IER);
655
656 /*
657 * Set up parity check flag
658 */
659 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
660 if (I_INPCK(info->tty))
661 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
662 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
663 info->read_status_mask |= UART_LSR_BI;
664
665 info->ignore_status_mask = 0;
666
667 if (I_IGNBRK(info->tty)) {
668 info->ignore_status_mask |= UART_LSR_BI;
669 info->read_status_mask |= UART_LSR_BI;
670 /*
671 * If we're ignore parity and break indicators, ignore
672 * overruns too. (For real raw support).
673 */
674 if (I_IGNPAR(info->tty)) {
675 info->ignore_status_mask |=
676 UART_LSR_OE |
677 UART_LSR_PE |
678 UART_LSR_FE;
679 info->read_status_mask |=
680 UART_LSR_OE |
681 UART_LSR_PE |
682 UART_LSR_FE;
683 }
684 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800685 if (info->board->chip_flag) {
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800686 SET_MOXA_MUST_XON1_VALUE(info->ioaddr, START_CHAR(info->tty));
687 SET_MOXA_MUST_XOFF1_VALUE(info->ioaddr, STOP_CHAR(info->tty));
688 if (I_IXON(info->tty)) {
689 ENABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
690 } else {
691 DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
692 }
693 if (I_IXOFF(info->tty)) {
694 ENABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
695 } else {
696 DISABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
697 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800698 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800699
700
701 outb(fcr, info->ioaddr + UART_FCR); /* set fcr */
702 outb(cval, info->ioaddr + UART_LCR);
703
704 return ret;
705}
706
707static void mxser_check_modem_status(struct mxser_port *port, int status)
708{
709 /* update input line counters */
710 if (status & UART_MSR_TERI)
711 port->icount.rng++;
712 if (status & UART_MSR_DDSR)
713 port->icount.dsr++;
714 if (status & UART_MSR_DDCD)
715 port->icount.dcd++;
716 if (status & UART_MSR_DCTS)
717 port->icount.cts++;
718 port->mon_data.modem_status = status;
719 wake_up_interruptible(&port->delta_msr_wait);
720
721 if ((port->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
722 if (status & UART_MSR_DCD)
723 wake_up_interruptible(&port->open_wait);
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800724 }
725
726 if (port->flags & ASYNC_CTS_FLOW) {
727 if (port->tty->hw_stopped) {
728 if (status & UART_MSR_CTS) {
729 port->tty->hw_stopped = 0;
730
731 if ((port->type != PORT_16550A) &&
732 (!port->board->chip_flag)) {
733 outb(port->IER & ~UART_IER_THRI,
734 port->ioaddr + UART_IER);
735 port->IER |= UART_IER_THRI;
736 outb(port->IER, port->ioaddr +
737 UART_IER);
738 }
Jiri Slaby47c85c02007-02-10 01:45:15 -0800739 tty_wakeup(port->tty);
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800740 }
741 } else {
742 if (!(status & UART_MSR_CTS)) {
743 port->tty->hw_stopped = 1;
744 if (port->type != PORT_16550A &&
745 !port->board->chip_flag) {
746 port->IER &= ~UART_IER_THRI;
747 outb(port->IER, port->ioaddr +
748 UART_IER);
749 }
750 }
751 }
752 }
753}
754
755static int mxser_startup(struct mxser_port *info)
756{
757 unsigned long page;
758 unsigned long flags;
759
760 page = __get_free_page(GFP_KERNEL);
761 if (!page)
762 return -ENOMEM;
763
764 spin_lock_irqsave(&info->slock, flags);
765
766 if (info->flags & ASYNC_INITIALIZED) {
767 free_page(page);
768 spin_unlock_irqrestore(&info->slock, flags);
769 return 0;
770 }
771
772 if (!info->ioaddr || !info->type) {
773 if (info->tty)
774 set_bit(TTY_IO_ERROR, &info->tty->flags);
775 free_page(page);
776 spin_unlock_irqrestore(&info->slock, flags);
777 return 0;
778 }
779 if (info->xmit_buf)
780 free_page(page);
781 else
782 info->xmit_buf = (unsigned char *) page;
783
784 /*
785 * Clear the FIFO buffers and disable them
786 * (they will be reenabled in mxser_change_speed())
787 */
788 if (info->board->chip_flag)
789 outb((UART_FCR_CLEAR_RCVR |
790 UART_FCR_CLEAR_XMIT |
791 MOXA_MUST_FCR_GDA_MODE_ENABLE), info->ioaddr + UART_FCR);
792 else
793 outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
794 info->ioaddr + UART_FCR);
795
796 /*
797 * At this point there's no way the LSR could still be 0xFF;
798 * if it is, then bail out, because there's likely no UART
799 * here.
800 */
801 if (inb(info->ioaddr + UART_LSR) == 0xff) {
802 spin_unlock_irqrestore(&info->slock, flags);
803 if (capable(CAP_SYS_ADMIN)) {
804 if (info->tty)
805 set_bit(TTY_IO_ERROR, &info->tty->flags);
806 return 0;
807 } else
808 return -ENODEV;
809 }
810
811 /*
812 * Clear the interrupt registers.
813 */
814 (void) inb(info->ioaddr + UART_LSR);
815 (void) inb(info->ioaddr + UART_RX);
816 (void) inb(info->ioaddr + UART_IIR);
817 (void) inb(info->ioaddr + UART_MSR);
818
819 /*
820 * Now, initialize the UART
821 */
822 outb(UART_LCR_WLEN8, info->ioaddr + UART_LCR); /* reset DLAB */
823 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
824 outb(info->MCR, info->ioaddr + UART_MCR);
825
826 /*
827 * Finally, enable interrupts
828 */
829 info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800830
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800831 if (info->board->chip_flag)
832 info->IER |= MOXA_MUST_IER_EGDAI;
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800833 outb(info->IER, info->ioaddr + UART_IER); /* enable interrupts */
834
835 /*
836 * And clear the interrupt registers again for luck.
837 */
838 (void) inb(info->ioaddr + UART_LSR);
839 (void) inb(info->ioaddr + UART_RX);
840 (void) inb(info->ioaddr + UART_IIR);
841 (void) inb(info->ioaddr + UART_MSR);
842
843 if (info->tty)
844 clear_bit(TTY_IO_ERROR, &info->tty->flags);
845 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
846
847 /*
848 * and set the speed of the serial port
849 */
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800850 mxser_change_speed(info, NULL);
Jiri Slaby5ff72052007-02-10 01:45:23 -0800851 info->flags |= ASYNC_INITIALIZED;
Jiri Slabyf64c84a2007-02-10 01:45:21 -0800852 spin_unlock_irqrestore(&info->slock, flags);
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800853
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800854 return 0;
855}
856
857/*
858 * This routine will shutdown a serial port; interrupts maybe disabled, and
859 * DTR is dropped if the hangup on close termio flag is on.
860 */
861static void mxser_shutdown(struct mxser_port *info)
862{
863 unsigned long flags;
864
865 if (!(info->flags & ASYNC_INITIALIZED))
866 return;
867
868 spin_lock_irqsave(&info->slock, flags);
869
870 /*
871 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
872 * here so the queue might never be waken up
873 */
874 wake_up_interruptible(&info->delta_msr_wait);
875
876 /*
877 * Free the IRQ, if necessary
878 */
879 if (info->xmit_buf) {
880 free_page((unsigned long) info->xmit_buf);
881 info->xmit_buf = NULL;
882 }
883
884 info->IER = 0;
885 outb(0x00, info->ioaddr + UART_IER);
886
887 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
888 info->MCR &= ~(UART_MCR_DTR | UART_MCR_RTS);
889 outb(info->MCR, info->ioaddr + UART_MCR);
890
891 /* clear Rx/Tx FIFO's */
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800892 if (info->board->chip_flag)
893 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT |
894 MOXA_MUST_FCR_GDA_MODE_ENABLE,
895 info->ioaddr + UART_FCR);
896 else
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800897 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
898 info->ioaddr + UART_FCR);
899
900 /* read data port to reset things */
901 (void) inb(info->ioaddr + UART_RX);
902
903 if (info->tty)
904 set_bit(TTY_IO_ERROR, &info->tty->flags);
905
906 info->flags &= ~ASYNC_INITIALIZED;
907
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800908 if (info->board->chip_flag)
909 SET_MOXA_MUST_NO_SOFTWARE_FLOW_CONTROL(info->ioaddr);
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800910
911 spin_unlock_irqrestore(&info->slock, flags);
912}
913
Jiri Slaby037ad482006-12-08 02:38:11 -0800914/*
915 * This routine is called whenever a serial port is opened. It
916 * enables interrupts for a serial port, linking in its async structure into
917 * the IRQ chain. It also performs the serial-specific
918 * initialization for the tty structure.
919 */
920static int mxser_open(struct tty_struct *tty, struct file *filp)
921{
Jiri Slaby55b307d2006-12-08 02:38:14 -0800922 struct mxser_port *info;
Jiri Slaby5ff72052007-02-10 01:45:23 -0800923 unsigned long flags;
Jiri Slaby037ad482006-12-08 02:38:11 -0800924 int retval, line;
925
Jiri Slaby037ad482006-12-08 02:38:11 -0800926 line = tty->index;
927 if (line == MXSER_PORTS)
928 return 0;
929 if (line < 0 || line > MXSER_PORTS)
930 return -ENODEV;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800931 info = &mxser_boards[line / MXSER_PORTS_PER_BOARD].ports[line % MXSER_PORTS_PER_BOARD];
932 if (!info->ioaddr)
Jiri Slaby037ad482006-12-08 02:38:11 -0800933 return -ENODEV;
934
935 tty->driver_data = info;
936 info->tty = tty;
937 /*
938 * Start up serial port
939 */
Jiri Slaby5ff72052007-02-10 01:45:23 -0800940 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby3306ce32006-12-08 02:38:13 -0800941 info->count++;
Jiri Slaby5ff72052007-02-10 01:45:23 -0800942 spin_unlock_irqrestore(&info->slock, flags);
Jiri Slaby037ad482006-12-08 02:38:11 -0800943 retval = mxser_startup(info);
944 if (retval)
945 return retval;
946
947 retval = mxser_block_til_ready(tty, filp, info);
948 if (retval)
949 return retval;
950
Jiri Slabye079f492006-12-08 02:38:31 -0800951 /* unmark here for very high baud rate (ex. 921600 bps) used */
Jiri Slaby037ad482006-12-08 02:38:11 -0800952 tty->low_latency = 1;
953 return 0;
954}
955
956/*
957 * This routine is called when the serial port gets closed. First, we
958 * wait for the last remaining data to be sent. Then, we unlink its
959 * async structure from the interrupt chain if necessary, and we free
960 * that IRQ if nothing is left in the chain.
961 */
962static void mxser_close(struct tty_struct *tty, struct file *filp)
963{
Jiri Slaby55b307d2006-12-08 02:38:14 -0800964 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -0800965
966 unsigned long timeout;
967 unsigned long flags;
Jiri Slaby037ad482006-12-08 02:38:11 -0800968
969 if (tty->index == MXSER_PORTS)
970 return;
971 if (!info)
972 return;
973
974 spin_lock_irqsave(&info->slock, flags);
975
976 if (tty_hung_up_p(filp)) {
977 spin_unlock_irqrestore(&info->slock, flags);
978 return;
979 }
980 if ((tty->count == 1) && (info->count != 1)) {
981 /*
982 * Uh, oh. tty->count is 1, which means that the tty
983 * structure will be freed. Info->count should always
984 * be one in these conditions. If it's greater than
985 * one, we've got real problems, since it means the
986 * serial port won't be shutdown.
987 */
988 printk(KERN_ERR "mxser_close: bad serial port count; "
989 "tty->count is 1, info->count is %d\n", info->count);
990 info->count = 1;
991 }
992 if (--info->count < 0) {
993 printk(KERN_ERR "mxser_close: bad serial port count for "
Jiri Slaby55b307d2006-12-08 02:38:14 -0800994 "ttys%d: %d\n", tty->index, info->count);
Jiri Slaby037ad482006-12-08 02:38:11 -0800995 info->count = 0;
996 }
997 if (info->count) {
998 spin_unlock_irqrestore(&info->slock, flags);
999 return;
1000 }
1001 info->flags |= ASYNC_CLOSING;
1002 spin_unlock_irqrestore(&info->slock, flags);
1003 /*
1004 * Save the termios structure, since this port may have
1005 * separate termios for callout and dialin.
1006 */
1007 if (info->flags & ASYNC_NORMAL_ACTIVE)
1008 info->normal_termios = *tty->termios;
1009 /*
1010 * Now we wait for the transmit buffer to clear; and we notify
1011 * the line discipline to only process XON/XOFF characters.
1012 */
1013 tty->closing = 1;
1014 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1015 tty_wait_until_sent(tty, info->closing_wait);
1016 /*
1017 * At this point we stop accepting input. To do this, we
1018 * disable the receive line status interrupts, and tell the
1019 * interrupt driver to stop checking the data ready bit in the
1020 * line status register.
1021 */
1022 info->IER &= ~UART_IER_RLSI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001023 if (info->board->chip_flag)
Jiri Slaby037ad482006-12-08 02:38:11 -08001024 info->IER &= ~MOXA_MUST_RECV_ISR;
Jiri Slabye079f492006-12-08 02:38:31 -08001025
Jiri Slaby037ad482006-12-08 02:38:11 -08001026 if (info->flags & ASYNC_INITIALIZED) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08001027 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001028 /*
1029 * Before we drop DTR, make sure the UART transmitter
1030 * has completely drained; this is especially
1031 * important if there is a transmit FIFO!
1032 */
1033 timeout = jiffies + HZ;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001034 while (!(inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001035 schedule_timeout_interruptible(5);
1036 if (time_after(jiffies, timeout))
1037 break;
1038 }
1039 }
1040 mxser_shutdown(info);
1041
1042 if (tty->driver->flush_buffer)
1043 tty->driver->flush_buffer(tty);
1044
Jiri Slaby7e8bcf92006-12-08 02:38:24 -08001045 tty_ldisc_flush(tty);
Jiri Slaby037ad482006-12-08 02:38:11 -08001046
1047 tty->closing = 0;
1048 info->event = 0;
1049 info->tty = NULL;
1050 if (info->blocked_open) {
1051 if (info->close_delay)
1052 schedule_timeout_interruptible(info->close_delay);
1053 wake_up_interruptible(&info->open_wait);
1054 }
1055
1056 info->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
Jiri Slaby037ad482006-12-08 02:38:11 -08001057}
1058
1059static int mxser_write(struct tty_struct *tty, const unsigned char *buf, int count)
1060{
1061 int c, total = 0;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001062 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001063 unsigned long flags;
1064
1065 if (!info->xmit_buf)
1066 return 0;
1067
1068 while (1) {
1069 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1070 SERIAL_XMIT_SIZE - info->xmit_head));
1071 if (c <= 0)
1072 break;
1073
1074 memcpy(info->xmit_buf + info->xmit_head, buf, c);
1075 spin_lock_irqsave(&info->slock, flags);
1076 info->xmit_head = (info->xmit_head + c) &
1077 (SERIAL_XMIT_SIZE - 1);
1078 info->xmit_cnt += c;
1079 spin_unlock_irqrestore(&info->slock, flags);
1080
1081 buf += c;
1082 count -= c;
1083 total += c;
1084 }
1085
Jiri Slabye079f492006-12-08 02:38:31 -08001086 if (info->xmit_cnt && !tty->stopped) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001087 if (!tty->hw_stopped ||
1088 (info->type == PORT_16550A) ||
Jiri Slaby55b307d2006-12-08 02:38:14 -08001089 (info->board->chip_flag)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001090 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby55b307d2006-12-08 02:38:14 -08001091 outb(info->IER & ~UART_IER_THRI, info->ioaddr +
1092 UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001093 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001094 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001095 spin_unlock_irqrestore(&info->slock, flags);
1096 }
1097 }
1098 return total;
1099}
1100
1101static void mxser_put_char(struct tty_struct *tty, unsigned char ch)
1102{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001103 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001104 unsigned long flags;
1105
1106 if (!info->xmit_buf)
1107 return;
1108
1109 if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1)
1110 return;
1111
1112 spin_lock_irqsave(&info->slock, flags);
1113 info->xmit_buf[info->xmit_head++] = ch;
1114 info->xmit_head &= SERIAL_XMIT_SIZE - 1;
1115 info->xmit_cnt++;
1116 spin_unlock_irqrestore(&info->slock, flags);
Jiri Slabye079f492006-12-08 02:38:31 -08001117 if (!tty->stopped) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001118 if (!tty->hw_stopped ||
1119 (info->type == PORT_16550A) ||
Jiri Slaby55b307d2006-12-08 02:38:14 -08001120 info->board->chip_flag) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001121 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby55b307d2006-12-08 02:38:14 -08001122 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001123 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001124 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001125 spin_unlock_irqrestore(&info->slock, flags);
1126 }
1127 }
1128}
1129
1130
1131static void mxser_flush_chars(struct tty_struct *tty)
1132{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001133 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001134 unsigned long flags;
1135
1136 if (info->xmit_cnt <= 0 ||
1137 tty->stopped ||
1138 !info->xmit_buf ||
1139 (tty->hw_stopped &&
1140 (info->type != PORT_16550A) &&
Jiri Slaby55b307d2006-12-08 02:38:14 -08001141 (!info->board->chip_flag)
Jiri Slaby037ad482006-12-08 02:38:11 -08001142 ))
1143 return;
1144
1145 spin_lock_irqsave(&info->slock, flags);
1146
Jiri Slaby55b307d2006-12-08 02:38:14 -08001147 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001148 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001149 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001150
1151 spin_unlock_irqrestore(&info->slock, flags);
1152}
1153
1154static int mxser_write_room(struct tty_struct *tty)
1155{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001156 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001157 int ret;
1158
1159 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1160 if (ret < 0)
1161 ret = 0;
1162 return ret;
1163}
1164
1165static int mxser_chars_in_buffer(struct tty_struct *tty)
1166{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001167 struct mxser_port *info = tty->driver_data;
Jiri Slaby925e9c12006-12-08 02:38:30 -08001168 return info->xmit_cnt;
Jiri Slaby037ad482006-12-08 02:38:11 -08001169}
1170
1171static void mxser_flush_buffer(struct tty_struct *tty)
1172{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001173 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001174 char fcr;
1175 unsigned long flags;
1176
1177
1178 spin_lock_irqsave(&info->slock, flags);
1179 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1180
Jiri Slaby55b307d2006-12-08 02:38:14 -08001181 fcr = inb(info->ioaddr + UART_FCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08001182 outb((fcr | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
Jiri Slaby55b307d2006-12-08 02:38:14 -08001183 info->ioaddr + UART_FCR);
1184 outb(fcr, info->ioaddr + UART_FCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08001185
1186 spin_unlock_irqrestore(&info->slock, flags);
Jiri Slaby037ad482006-12-08 02:38:11 -08001187
Jiri Slaby7e8bcf92006-12-08 02:38:24 -08001188 tty_wakeup(tty);
Jiri Slaby037ad482006-12-08 02:38:11 -08001189}
1190
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001191/*
1192 * ------------------------------------------------------------
1193 * friends of mxser_ioctl()
1194 * ------------------------------------------------------------
1195 */
1196static int mxser_get_serial_info(struct mxser_port *info,
1197 struct serial_struct __user *retinfo)
1198{
1199 struct serial_struct tmp;
1200
1201 if (!retinfo)
1202 return -EFAULT;
1203 memset(&tmp, 0, sizeof(tmp));
1204 tmp.type = info->type;
1205 tmp.line = info->tty->index;
1206 tmp.port = info->ioaddr;
1207 tmp.irq = info->board->irq;
1208 tmp.flags = info->flags;
1209 tmp.baud_base = info->baud_base;
1210 tmp.close_delay = info->close_delay;
1211 tmp.closing_wait = info->closing_wait;
1212 tmp.custom_divisor = info->custom_divisor;
1213 tmp.hub6 = 0;
1214 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1215 return -EFAULT;
1216 return 0;
1217}
1218
1219static int mxser_set_serial_info(struct mxser_port *info,
1220 struct serial_struct __user *new_info)
1221{
1222 struct serial_struct new_serial;
Jiri Slabyf64c84a2007-02-10 01:45:21 -08001223 unsigned long sl_flags;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001224 unsigned int flags;
1225 int retval = 0;
1226
1227 if (!new_info || !info->ioaddr)
1228 return -EFAULT;
1229 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
1230 return -EFAULT;
1231
1232 if ((new_serial.irq != info->board->irq) ||
1233 (new_serial.port != info->ioaddr) ||
1234 (new_serial.custom_divisor != info->custom_divisor) ||
1235 (new_serial.baud_base != info->baud_base))
1236 return -EPERM;
1237
1238 flags = info->flags & ASYNC_SPD_MASK;
1239
1240 if (!capable(CAP_SYS_ADMIN)) {
1241 if ((new_serial.baud_base != info->baud_base) ||
1242 (new_serial.close_delay != info->close_delay) ||
1243 ((new_serial.flags & ~ASYNC_USR_MASK) != (info->flags & ~ASYNC_USR_MASK)))
1244 return -EPERM;
1245 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
1246 (new_serial.flags & ASYNC_USR_MASK));
1247 } else {
1248 /*
1249 * OK, past this point, all the error checking has been done.
1250 * At this point, we start making changes.....
1251 */
1252 info->flags = ((info->flags & ~ASYNC_FLAGS) |
1253 (new_serial.flags & ASYNC_FLAGS));
1254 info->close_delay = new_serial.close_delay * HZ / 100;
1255 info->closing_wait = new_serial.closing_wait * HZ / 100;
1256 info->tty->low_latency =
1257 (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
Jiri Slabye079f492006-12-08 02:38:31 -08001258 info->tty->low_latency = 0;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001259 }
1260
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001261 info->type = new_serial.type;
1262
1263 process_txrx_fifo(info);
1264
1265 if (info->flags & ASYNC_INITIALIZED) {
Jiri Slabyf64c84a2007-02-10 01:45:21 -08001266 if (flags != (info->flags & ASYNC_SPD_MASK)) {
1267 spin_lock_irqsave(&info->slock, sl_flags);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001268 mxser_change_speed(info, NULL);
Jiri Slabyf64c84a2007-02-10 01:45:21 -08001269 spin_unlock_irqrestore(&info->slock, sl_flags);
1270 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001271 } else
1272 retval = mxser_startup(info);
1273
1274 return retval;
1275}
1276
1277/*
1278 * mxser_get_lsr_info - get line status register info
1279 *
1280 * Purpose: Let user call ioctl() to get info when the UART physically
1281 * is emptied. On bus types like RS485, the transmitter must
1282 * release the bus after transmitting. This must be done when
1283 * the transmit shift register is empty, not be done when the
1284 * transmit holding register is empty. This functionality
1285 * allows an RS485 driver to be written in user space.
1286 */
1287static int mxser_get_lsr_info(struct mxser_port *info,
1288 unsigned int __user *value)
1289{
1290 unsigned char status;
1291 unsigned int result;
1292 unsigned long flags;
1293
1294 spin_lock_irqsave(&info->slock, flags);
1295 status = inb(info->ioaddr + UART_LSR);
1296 spin_unlock_irqrestore(&info->slock, flags);
1297 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1298 return put_user(result, value);
1299}
1300
1301/*
1302 * This routine sends a break character out the serial port.
1303 */
1304static void mxser_send_break(struct mxser_port *info, int duration)
1305{
1306 unsigned long flags;
1307
1308 if (!info->ioaddr)
1309 return;
1310 set_current_state(TASK_INTERRUPTIBLE);
1311 spin_lock_irqsave(&info->slock, flags);
1312 outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
1313 info->ioaddr + UART_LCR);
1314 spin_unlock_irqrestore(&info->slock, flags);
1315 schedule_timeout(duration);
1316 spin_lock_irqsave(&info->slock, flags);
1317 outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
1318 info->ioaddr + UART_LCR);
1319 spin_unlock_irqrestore(&info->slock, flags);
1320}
1321
1322static int mxser_tiocmget(struct tty_struct *tty, struct file *file)
Jiri Slaby037ad482006-12-08 02:38:11 -08001323{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001324 struct mxser_port *info = tty->driver_data;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001325 unsigned char control, status;
Jiri Slaby037ad482006-12-08 02:38:11 -08001326 unsigned long flags;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001327
Jiri Slaby037ad482006-12-08 02:38:11 -08001328
1329 if (tty->index == MXSER_PORTS)
Jiri Slaby037ad482006-12-08 02:38:11 -08001330 return -ENOIOCTLCMD;
Jiri Slaby214efeb2006-12-08 02:38:25 -08001331 if (test_bit(TTY_IO_ERROR, &tty->flags))
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001332 return -EIO;
1333
1334 control = info->MCR;
1335
1336 spin_lock_irqsave(&info->slock, flags);
1337 status = inb(info->ioaddr + UART_MSR);
1338 if (status & UART_MSR_ANY_DELTA)
1339 mxser_check_modem_status(info, status);
1340 spin_unlock_irqrestore(&info->slock, flags);
1341 return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0) |
1342 ((control & UART_MCR_DTR) ? TIOCM_DTR : 0) |
1343 ((status & UART_MSR_DCD) ? TIOCM_CAR : 0) |
1344 ((status & UART_MSR_RI) ? TIOCM_RNG : 0) |
1345 ((status & UART_MSR_DSR) ? TIOCM_DSR : 0) |
1346 ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
1347}
1348
1349static int mxser_tiocmset(struct tty_struct *tty, struct file *file,
1350 unsigned int set, unsigned int clear)
1351{
1352 struct mxser_port *info = tty->driver_data;
1353 unsigned long flags;
1354
1355
1356 if (tty->index == MXSER_PORTS)
1357 return -ENOIOCTLCMD;
Jiri Slaby214efeb2006-12-08 02:38:25 -08001358 if (test_bit(TTY_IO_ERROR, &tty->flags))
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001359 return -EIO;
1360
1361 spin_lock_irqsave(&info->slock, flags);
1362
1363 if (set & TIOCM_RTS)
1364 info->MCR |= UART_MCR_RTS;
1365 if (set & TIOCM_DTR)
1366 info->MCR |= UART_MCR_DTR;
1367
1368 if (clear & TIOCM_RTS)
1369 info->MCR &= ~UART_MCR_RTS;
1370 if (clear & TIOCM_DTR)
1371 info->MCR &= ~UART_MCR_DTR;
1372
1373 outb(info->MCR, info->ioaddr + UART_MCR);
1374 spin_unlock_irqrestore(&info->slock, flags);
Jiri Slaby037ad482006-12-08 02:38:11 -08001375 return 0;
1376}
1377
Jiri Slabyc76b0932007-02-10 01:45:07 -08001378static int __init mxser_program_mode(int port)
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001379{
1380 int id, i, j, n;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001381
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001382 outb(0, port);
1383 outb(0, port);
1384 outb(0, port);
1385 (void)inb(port);
1386 (void)inb(port);
1387 outb(0, port);
1388 (void)inb(port);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001389
1390 id = inb(port + 1) & 0x1F;
1391 if ((id != C168_ASIC_ID) &&
1392 (id != C104_ASIC_ID) &&
1393 (id != C102_ASIC_ID) &&
1394 (id != CI132_ASIC_ID) &&
1395 (id != CI134_ASIC_ID) &&
1396 (id != CI104J_ASIC_ID))
1397 return -1;
1398 for (i = 0, j = 0; i < 4; i++) {
1399 n = inb(port + 2);
1400 if (n == 'M') {
1401 j = 1;
1402 } else if ((j == 1) && (n == 1)) {
1403 j = 2;
1404 break;
1405 } else
1406 j = 0;
1407 }
1408 if (j != 2)
1409 id = -2;
1410 return id;
1411}
1412
Jiri Slabyc76b0932007-02-10 01:45:07 -08001413static void __init mxser_normal_mode(int port)
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001414{
1415 int i, n;
1416
1417 outb(0xA5, port + 1);
1418 outb(0x80, port + 3);
1419 outb(12, port + 0); /* 9600 bps */
1420 outb(0, port + 1);
1421 outb(0x03, port + 3); /* 8 data bits */
1422 outb(0x13, port + 4); /* loop back mode */
1423 for (i = 0; i < 16; i++) {
1424 n = inb(port + 5);
1425 if ((n & 0x61) == 0x60)
1426 break;
1427 if ((n & 1) == 1)
1428 (void)inb(port);
1429 }
1430 outb(0x00, port + 4);
1431}
1432
1433#define CHIP_SK 0x01 /* Serial Data Clock in Eprom */
1434#define CHIP_DO 0x02 /* Serial Data Output in Eprom */
1435#define CHIP_CS 0x04 /* Serial Chip Select in Eprom */
1436#define CHIP_DI 0x08 /* Serial Data Input in Eprom */
1437#define EN_CCMD 0x000 /* Chip's command register */
1438#define EN0_RSARLO 0x008 /* Remote start address reg 0 */
1439#define EN0_RSARHI 0x009 /* Remote start address reg 1 */
1440#define EN0_RCNTLO 0x00A /* Remote byte count reg WR */
1441#define EN0_RCNTHI 0x00B /* Remote byte count reg WR */
1442#define EN0_DCFG 0x00E /* Data configuration reg WR */
1443#define EN0_PORT 0x010 /* Rcv missed frame error counter RD */
1444#define ENC_PAGE0 0x000 /* Select page 0 of chip registers */
1445#define ENC_PAGE3 0x0C0 /* Select page 3 of chip registers */
Jiri Slabyc76b0932007-02-10 01:45:07 -08001446static int __init mxser_read_register(int port, unsigned short *regs)
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001447{
1448 int i, k, value, id;
1449 unsigned int j;
1450
1451 id = mxser_program_mode(port);
1452 if (id < 0)
1453 return id;
1454 for (i = 0; i < 14; i++) {
1455 k = (i & 0x3F) | 0x180;
1456 for (j = 0x100; j > 0; j >>= 1) {
1457 outb(CHIP_CS, port);
1458 if (k & j) {
1459 outb(CHIP_CS | CHIP_DO, port);
1460 outb(CHIP_CS | CHIP_DO | CHIP_SK, port); /* A? bit of read */
1461 } else {
1462 outb(CHIP_CS, port);
1463 outb(CHIP_CS | CHIP_SK, port); /* A? bit of read */
1464 }
1465 }
1466 (void)inb(port);
1467 value = 0;
1468 for (k = 0, j = 0x8000; k < 16; k++, j >>= 1) {
1469 outb(CHIP_CS, port);
1470 outb(CHIP_CS | CHIP_SK, port);
1471 if (inb(port) & CHIP_DI)
1472 value |= j;
1473 }
1474 regs[i] = value;
1475 outb(0, port);
1476 }
1477 mxser_normal_mode(port);
1478 return id;
1479}
1480
Jiri Slaby037ad482006-12-08 02:38:11 -08001481static int mxser_ioctl_special(unsigned int cmd, void __user *argp)
1482{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001483 struct mxser_port *port;
1484 int result, status;
1485 unsigned int i, j;
Jiri Slaby037ad482006-12-08 02:38:11 -08001486
1487 switch (cmd) {
1488 case MOXA_GET_CONF:
Jiri Slaby55b307d2006-12-08 02:38:14 -08001489/* if (copy_to_user(argp, mxsercfg,
Jiri Slaby037ad482006-12-08 02:38:11 -08001490 sizeof(struct mxser_hwconf) * 4))
1491 return -EFAULT;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001492 return 0;*/
1493 return -ENXIO;
Jiri Slaby037ad482006-12-08 02:38:11 -08001494 case MOXA_GET_MAJOR:
1495 if (copy_to_user(argp, &ttymajor, sizeof(int)))
1496 return -EFAULT;
1497 return 0;
1498
1499 case MOXA_GET_CUMAJOR:
1500 if (copy_to_user(argp, &calloutmajor, sizeof(int)))
1501 return -EFAULT;
1502 return 0;
1503
1504 case MOXA_CHKPORTENABLE:
1505 result = 0;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001506
1507 for (i = 0; i < MXSER_BOARDS; i++)
1508 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++)
1509 if (mxser_boards[i].ports[j].ioaddr)
1510 result |= (1 << i);
1511
Jiri Slaby037ad482006-12-08 02:38:11 -08001512 return put_user(result, (unsigned long __user *)argp);
1513 case MOXA_GETDATACOUNT:
1514 if (copy_to_user(argp, &mxvar_log, sizeof(mxvar_log)))
1515 return -EFAULT;
1516 return 0;
1517 case MOXA_GETMSTATUS:
Jiri Slaby55b307d2006-12-08 02:38:14 -08001518 for (i = 0; i < MXSER_BOARDS; i++)
1519 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
1520 port = &mxser_boards[i].ports[j];
1521
1522 GMStatus[i].ri = 0;
1523 if (!port->ioaddr) {
1524 GMStatus[i].dcd = 0;
1525 GMStatus[i].dsr = 0;
1526 GMStatus[i].cts = 0;
1527 continue;
1528 }
1529
1530 if (!port->tty || !port->tty->termios)
1531 GMStatus[i].cflag =
1532 port->normal_termios.c_cflag;
1533 else
1534 GMStatus[i].cflag =
1535 port->tty->termios->c_cflag;
1536
1537 status = inb(port->ioaddr + UART_MSR);
1538 if (status & 0x80 /*UART_MSR_DCD */ )
1539 GMStatus[i].dcd = 1;
1540 else
1541 GMStatus[i].dcd = 0;
1542
1543 if (status & 0x20 /*UART_MSR_DSR */ )
1544 GMStatus[i].dsr = 1;
1545 else
1546 GMStatus[i].dsr = 0;
1547
1548
1549 if (status & 0x10 /*UART_MSR_CTS */ )
1550 GMStatus[i].cts = 1;
1551 else
1552 GMStatus[i].cts = 0;
Jiri Slaby037ad482006-12-08 02:38:11 -08001553 }
Jiri Slaby037ad482006-12-08 02:38:11 -08001554 if (copy_to_user(argp, GMStatus,
1555 sizeof(struct mxser_mstatus) * MXSER_PORTS))
1556 return -EFAULT;
1557 return 0;
1558 case MOXA_ASPP_MON_EXT: {
Jiri Slaby2b770b42007-07-15 23:40:17 -07001559 int p, shiftbit;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001560 unsigned long opmode;
1561 unsigned cflag, iflag;
Jiri Slaby037ad482006-12-08 02:38:11 -08001562
Jiri Slaby55b307d2006-12-08 02:38:14 -08001563 for (i = 0; i < MXSER_BOARDS; i++)
1564 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
1565 port = &mxser_boards[i].ports[j];
1566 if (!port->ioaddr)
Jiri Slaby037ad482006-12-08 02:38:11 -08001567 continue;
1568
Jiri Slaby55b307d2006-12-08 02:38:14 -08001569 status = mxser_get_msr(port->ioaddr, 0, i);
Jiri Slaby55b307d2006-12-08 02:38:14 -08001570
Jiri Slaby037ad482006-12-08 02:38:11 -08001571 if (status & UART_MSR_TERI)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001572 port->icount.rng++;
Jiri Slaby037ad482006-12-08 02:38:11 -08001573 if (status & UART_MSR_DDSR)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001574 port->icount.dsr++;
Jiri Slaby037ad482006-12-08 02:38:11 -08001575 if (status & UART_MSR_DDCD)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001576 port->icount.dcd++;
Jiri Slaby037ad482006-12-08 02:38:11 -08001577 if (status & UART_MSR_DCTS)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001578 port->icount.cts++;
Jiri Slaby037ad482006-12-08 02:38:11 -08001579
Jiri Slaby55b307d2006-12-08 02:38:14 -08001580 port->mon_data.modem_status = status;
1581 mon_data_ext.rx_cnt[i] = port->mon_data.rxcnt;
1582 mon_data_ext.tx_cnt[i] = port->mon_data.txcnt;
1583 mon_data_ext.up_rxcnt[i] =
1584 port->mon_data.up_rxcnt;
1585 mon_data_ext.up_txcnt[i] =
1586 port->mon_data.up_txcnt;
1587 mon_data_ext.modem_status[i] =
1588 port->mon_data.modem_status;
1589 mon_data_ext.baudrate[i] = port->realbaud;
Jiri Slaby037ad482006-12-08 02:38:11 -08001590
Jiri Slaby55b307d2006-12-08 02:38:14 -08001591 if (!port->tty || !port->tty->termios) {
1592 cflag = port->normal_termios.c_cflag;
1593 iflag = port->normal_termios.c_iflag;
Jiri Slaby037ad482006-12-08 02:38:11 -08001594 } else {
Jiri Slaby55b307d2006-12-08 02:38:14 -08001595 cflag = port->tty->termios->c_cflag;
1596 iflag = port->tty->termios->c_iflag;
Jiri Slaby037ad482006-12-08 02:38:11 -08001597 }
1598
1599 mon_data_ext.databits[i] = cflag & CSIZE;
1600
1601 mon_data_ext.stopbits[i] = cflag & CSTOPB;
1602
Jiri Slaby55b307d2006-12-08 02:38:14 -08001603 mon_data_ext.parity[i] =
1604 cflag & (PARENB | PARODD | CMSPAR);
Jiri Slaby037ad482006-12-08 02:38:11 -08001605
1606 mon_data_ext.flowctrl[i] = 0x00;
1607
1608 if (cflag & CRTSCTS)
1609 mon_data_ext.flowctrl[i] |= 0x03;
1610
1611 if (iflag & (IXON | IXOFF))
1612 mon_data_ext.flowctrl[i] |= 0x0C;
1613
Jiri Slaby55b307d2006-12-08 02:38:14 -08001614 if (port->type == PORT_16550A)
Jiri Slaby037ad482006-12-08 02:38:11 -08001615 mon_data_ext.fifo[i] = 1;
1616 else
1617 mon_data_ext.fifo[i] = 0;
1618
1619 p = i % 4;
1620 shiftbit = p * 2;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001621 opmode = inb(port->opmode_ioaddr) >> shiftbit;
Jiri Slaby037ad482006-12-08 02:38:11 -08001622 opmode &= OP_MODE_MASK;
1623
1624 mon_data_ext.iftype[i] = opmode;
1625
1626 }
Jiri Slaby55b307d2006-12-08 02:38:14 -08001627 if (copy_to_user(argp, &mon_data_ext,
1628 sizeof(mon_data_ext)))
Jiri Slaby037ad482006-12-08 02:38:11 -08001629 return -EFAULT;
1630
1631 return 0;
1632
Jiri Slaby55b307d2006-12-08 02:38:14 -08001633 } default:
Jiri Slaby037ad482006-12-08 02:38:11 -08001634 return -ENOIOCTLCMD;
1635 }
1636 return 0;
1637}
1638
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001639static int mxser_ioctl(struct tty_struct *tty, struct file *file,
1640 unsigned int cmd, unsigned long arg)
1641{
1642 struct mxser_port *info = tty->driver_data;
1643 struct async_icount cprev, cnow; /* kernel counter temps */
1644 struct serial_icounter_struct __user *p_cuser;
1645 unsigned long templ;
1646 unsigned long flags;
Jiri Slabyf64c84a2007-02-10 01:45:21 -08001647 unsigned int i;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001648 void __user *argp = (void __user *)arg;
1649 int retval;
1650
1651 if (tty->index == MXSER_PORTS)
1652 return mxser_ioctl_special(cmd, argp);
1653
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001654 if (cmd == MOXA_SET_OP_MODE || cmd == MOXA_GET_OP_MODE) {
1655 int p;
1656 unsigned long opmode;
1657 static unsigned char ModeMask[] = { 0xfc, 0xf3, 0xcf, 0x3f };
1658 int shiftbit;
1659 unsigned char val, mask;
1660
1661 p = tty->index % 4;
1662 if (cmd == MOXA_SET_OP_MODE) {
1663 if (get_user(opmode, (int __user *) argp))
1664 return -EFAULT;
1665 if (opmode != RS232_MODE &&
1666 opmode != RS485_2WIRE_MODE &&
1667 opmode != RS422_MODE &&
1668 opmode != RS485_4WIRE_MODE)
1669 return -EFAULT;
1670 mask = ModeMask[p];
1671 shiftbit = p * 2;
1672 val = inb(info->opmode_ioaddr);
1673 val &= mask;
1674 val |= (opmode << shiftbit);
1675 outb(val, info->opmode_ioaddr);
1676 } else {
1677 shiftbit = p * 2;
1678 opmode = inb(info->opmode_ioaddr) >> shiftbit;
1679 opmode &= OP_MODE_MASK;
1680 if (copy_to_user(argp, &opmode, sizeof(int)))
1681 return -EFAULT;
1682 }
1683 return 0;
1684 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001685
Jiri Slabyf64c84a2007-02-10 01:45:21 -08001686 if (cmd == MOXA_SET_SPECIAL_BAUD_RATE) {
1687 int speed;
1688
1689 if (get_user(speed, (int __user *)argp))
1690 return -EFAULT;
1691 if (speed <= 0 || speed > info->max_baud)
1692 return -EFAULT;
1693 if (!info->tty || !info->tty->termios || !info->ioaddr)
1694 return 0;
1695 info->tty->termios->c_cflag &= ~(CBAUD | CBAUDEX);
1696 for (i = 0; i < BAUD_TABLE_NO; i++)
1697 if (speed == mxvar_baud_table[i])
1698 break;
1699 if (i == BAUD_TABLE_NO) {
1700 info->tty->termios->c_cflag |= B_SPEC;
1701 } else if (speed != 0)
1702 info->tty->termios->c_cflag |= mxvar_baud_table1[i];
1703
1704 info->speed = speed;
1705 spin_lock_irqsave(&info->slock, flags);
Jiri Slabya136e992007-02-10 01:45:24 -08001706 mxser_change_speed(info, NULL);
Jiri Slabyf64c84a2007-02-10 01:45:21 -08001707 spin_unlock_irqrestore(&info->slock, flags);
1708
1709 return 0;
1710 } else if (cmd == MOXA_GET_SPECIAL_BAUD_RATE) {
1711 if (copy_to_user(argp, &info->speed, sizeof(int)))
1712 return -EFAULT;
1713 return 0;
1714 }
1715
Jiri Slaby214efeb2006-12-08 02:38:25 -08001716 if (cmd != TIOCGSERIAL && cmd != TIOCMIWAIT && cmd != TIOCGICOUNT &&
1717 test_bit(TTY_IO_ERROR, &tty->flags))
1718 return -EIO;
1719
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001720 switch (cmd) {
1721 case TCSBRK: /* SVID version: non-zero arg --> no break */
1722 retval = tty_check_change(tty);
1723 if (retval)
1724 return retval;
1725 tty_wait_until_sent(tty, 0);
1726 if (!arg)
1727 mxser_send_break(info, HZ / 4); /* 1/4 second */
1728 return 0;
1729 case TCSBRKP: /* support for POSIX tcsendbreak() */
1730 retval = tty_check_change(tty);
1731 if (retval)
1732 return retval;
1733 tty_wait_until_sent(tty, 0);
1734 mxser_send_break(info, arg ? arg * (HZ / 10) : HZ / 4);
1735 return 0;
1736 case TIOCGSOFTCAR:
1737 return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *)argp);
1738 case TIOCSSOFTCAR:
1739 if (get_user(templ, (unsigned long __user *) argp))
1740 return -EFAULT;
1741 arg = templ;
1742 tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) | (arg ? CLOCAL : 0));
1743 return 0;
1744 case TIOCGSERIAL:
1745 return mxser_get_serial_info(info, argp);
1746 case TIOCSSERIAL:
1747 return mxser_set_serial_info(info, argp);
1748 case TIOCSERGETLSR: /* Get line status register */
1749 return mxser_get_lsr_info(info, argp);
1750 /*
1751 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1752 * - mask passed in arg for lines of interest
1753 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1754 * Caller should use TIOCGICOUNT to see which one it was
1755 */
Jiri Slabyb446a4a2007-04-23 14:41:03 -07001756 case TIOCMIWAIT:
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001757 spin_lock_irqsave(&info->slock, flags);
Jiri Slabyb446a4a2007-04-23 14:41:03 -07001758 cnow = info->icount; /* note the counters on entry */
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001759 spin_unlock_irqrestore(&info->slock, flags);
1760
Jiri Slabyb446a4a2007-04-23 14:41:03 -07001761 wait_event_interruptible(info->delta_msr_wait, ({
1762 cprev = cnow;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001763 spin_lock_irqsave(&info->slock, flags);
1764 cnow = info->icount; /* atomic copy */
1765 spin_unlock_irqrestore(&info->slock, flags);
1766
Jiri Slabyb446a4a2007-04-23 14:41:03 -07001767 ((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1768 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1769 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1770 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts));
1771 }));
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001772 break;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001773 /*
1774 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1775 * Return: write counters to the user passed counter struct
1776 * NB: both 1->0 and 0->1 transitions are counted except for
1777 * RI where only 0->1 is counted.
1778 */
1779 case TIOCGICOUNT:
1780 spin_lock_irqsave(&info->slock, flags);
1781 cnow = info->icount;
1782 spin_unlock_irqrestore(&info->slock, flags);
1783 p_cuser = argp;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001784 if (put_user(cnow.frame, &p_cuser->frame))
1785 return -EFAULT;
1786 if (put_user(cnow.brk, &p_cuser->brk))
1787 return -EFAULT;
1788 if (put_user(cnow.overrun, &p_cuser->overrun))
1789 return -EFAULT;
1790 if (put_user(cnow.buf_overrun, &p_cuser->buf_overrun))
1791 return -EFAULT;
1792 if (put_user(cnow.parity, &p_cuser->parity))
1793 return -EFAULT;
1794 if (put_user(cnow.rx, &p_cuser->rx))
1795 return -EFAULT;
1796 if (put_user(cnow.tx, &p_cuser->tx))
1797 return -EFAULT;
1798 put_user(cnow.cts, &p_cuser->cts);
1799 put_user(cnow.dsr, &p_cuser->dsr);
1800 put_user(cnow.rng, &p_cuser->rng);
1801 put_user(cnow.dcd, &p_cuser->dcd);
1802 return 0;
1803 case MOXA_HighSpeedOn:
1804 return put_user(info->baud_base != 115200 ? 1 : 0, (int __user *)argp);
1805 case MOXA_SDS_RSTICOUNTER:
1806 info->mon_data.rxcnt = 0;
1807 info->mon_data.txcnt = 0;
1808 return 0;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001809 case MOXA_ASPP_SETBAUD:{
1810 long baud;
1811 if (get_user(baud, (long __user *)argp))
1812 return -EFAULT;
Jiri Slabyf64c84a2007-02-10 01:45:21 -08001813 spin_lock_irqsave(&info->slock, flags);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001814 mxser_set_baud(info, baud);
Jiri Slabyf64c84a2007-02-10 01:45:21 -08001815 spin_unlock_irqrestore(&info->slock, flags);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001816 return 0;
1817 }
1818 case MOXA_ASPP_GETBAUD:
1819 if (copy_to_user(argp, &info->realbaud, sizeof(long)))
1820 return -EFAULT;
1821
1822 return 0;
1823
1824 case MOXA_ASPP_OQUEUE:{
1825 int len, lsr;
1826
1827 len = mxser_chars_in_buffer(tty);
1828
1829 lsr = inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT;
1830
1831 len += (lsr ? 0 : 1);
1832
1833 if (copy_to_user(argp, &len, sizeof(int)))
1834 return -EFAULT;
1835
1836 return 0;
1837 }
1838 case MOXA_ASPP_MON: {
1839 int mcr, status;
1840
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001841 status = mxser_get_msr(info->ioaddr, 1, tty->index);
1842 mxser_check_modem_status(info, status);
1843
1844 mcr = inb(info->ioaddr + UART_MCR);
1845 if (mcr & MOXA_MUST_MCR_XON_FLAG)
1846 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFHOLD;
1847 else
1848 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFHOLD;
1849
1850 if (mcr & MOXA_MUST_MCR_TX_XON)
1851 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFXENT;
1852 else
1853 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFXENT;
1854
1855 if (info->tty->hw_stopped)
1856 info->mon_data.hold_reason |= NPPI_NOTIFY_CTSHOLD;
1857 else
1858 info->mon_data.hold_reason &= ~NPPI_NOTIFY_CTSHOLD;
1859
1860 if (copy_to_user(argp, &info->mon_data,
1861 sizeof(struct mxser_mon)))
1862 return -EFAULT;
1863
1864 return 0;
1865 }
1866 case MOXA_ASPP_LSTATUS: {
1867 if (copy_to_user(argp, &info->err_shadow,
1868 sizeof(unsigned char)))
1869 return -EFAULT;
1870
1871 info->err_shadow = 0;
1872 return 0;
1873 }
1874 case MOXA_SET_BAUD_METHOD: {
1875 int method;
1876
1877 if (get_user(method, (int __user *)argp))
1878 return -EFAULT;
1879 mxser_set_baud_method[tty->index] = method;
1880 if (copy_to_user(argp, &method, sizeof(int)))
1881 return -EFAULT;
1882
1883 return 0;
1884 }
1885 default:
1886 return -ENOIOCTLCMD;
1887 }
1888 return 0;
1889}
1890
Jiri Slaby037ad482006-12-08 02:38:11 -08001891static void mxser_stoprx(struct tty_struct *tty)
1892{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001893 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001894
1895 info->ldisc_stop_rx = 1;
1896 if (I_IXOFF(tty)) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08001897 if (info->board->chip_flag) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001898 info->IER &= ~MOXA_MUST_RECV_ISR;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001899 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby925e9c12006-12-08 02:38:30 -08001900 } else {
Jiri Slaby037ad482006-12-08 02:38:11 -08001901 info->x_char = STOP_CHAR(tty);
Jiri Slaby55b307d2006-12-08 02:38:14 -08001902 outb(0, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001903 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001904 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001905 }
Jiri Slaby037ad482006-12-08 02:38:11 -08001906 }
1907
1908 if (info->tty->termios->c_cflag & CRTSCTS) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001909 info->MCR &= ~UART_MCR_RTS;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001910 outb(info->MCR, info->ioaddr + UART_MCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08001911 }
1912}
1913
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001914/*
1915 * This routine is called by the upper-layer tty layer to signal that
1916 * incoming characters should be throttled.
1917 */
1918static void mxser_throttle(struct tty_struct *tty)
1919{
1920 mxser_stoprx(tty);
1921}
1922
1923static void mxser_unthrottle(struct tty_struct *tty)
Jiri Slaby037ad482006-12-08 02:38:11 -08001924{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001925 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001926
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001927 /* startrx */
Jiri Slaby037ad482006-12-08 02:38:11 -08001928 info->ldisc_stop_rx = 0;
1929 if (I_IXOFF(tty)) {
1930 if (info->x_char)
1931 info->x_char = 0;
1932 else {
Jiri Slaby55b307d2006-12-08 02:38:14 -08001933 if (info->board->chip_flag) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001934 info->IER |= MOXA_MUST_RECV_ISR;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001935 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby925e9c12006-12-08 02:38:30 -08001936 } else {
Jiri Slaby037ad482006-12-08 02:38:11 -08001937 info->x_char = START_CHAR(tty);
Jiri Slaby55b307d2006-12-08 02:38:14 -08001938 outb(0, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001939 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001940 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001941 }
Jiri Slaby037ad482006-12-08 02:38:11 -08001942 }
1943 }
1944
1945 if (info->tty->termios->c_cflag & CRTSCTS) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001946 info->MCR |= UART_MCR_RTS;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001947 outb(info->MCR, info->ioaddr + UART_MCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08001948 }
1949}
1950
1951/*
Jiri Slaby037ad482006-12-08 02:38:11 -08001952 * mxser_stop() and mxser_start()
1953 *
1954 * This routines are called before setting or resetting tty->stopped.
1955 * They enable or disable transmitter interrupts, as necessary.
1956 */
1957static void mxser_stop(struct tty_struct *tty)
1958{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001959 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001960 unsigned long flags;
1961
1962 spin_lock_irqsave(&info->slock, flags);
1963 if (info->IER & UART_IER_THRI) {
1964 info->IER &= ~UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001965 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001966 }
1967 spin_unlock_irqrestore(&info->slock, flags);
1968}
1969
1970static void mxser_start(struct tty_struct *tty)
1971{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001972 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001973 unsigned long flags;
1974
1975 spin_lock_irqsave(&info->slock, flags);
Jiri Slabye079f492006-12-08 02:38:31 -08001976 if (info->xmit_cnt && info->xmit_buf) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08001977 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001978 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001979 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001980 }
1981 spin_unlock_irqrestore(&info->slock, flags);
1982}
1983
Alan Cox606d0992006-12-08 02:38:45 -08001984static void mxser_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001985{
1986 struct mxser_port *info = tty->driver_data;
1987 unsigned long flags;
1988
Alan Cox7e7d1362007-07-15 23:41:49 -07001989 spin_lock_irqsave(&info->slock, flags);
1990 mxser_change_speed(info, old_termios);
1991 spin_unlock_irqrestore(&info->slock, flags);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001992
Alan Cox7e7d1362007-07-15 23:41:49 -07001993 if ((old_termios->c_cflag & CRTSCTS) &&
1994 !(tty->termios->c_cflag & CRTSCTS)) {
1995 tty->hw_stopped = 0;
1996 mxser_start(tty);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001997 }
1998
Jiri Slabye079f492006-12-08 02:38:31 -08001999 /* Handle sw stopped */
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002000 if ((old_termios->c_iflag & IXON) &&
2001 !(tty->termios->c_iflag & IXON)) {
2002 tty->stopped = 0;
2003
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002004 if (info->board->chip_flag) {
2005 spin_lock_irqsave(&info->slock, flags);
2006 DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
2007 spin_unlock_irqrestore(&info->slock, flags);
2008 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002009
2010 mxser_start(tty);
2011 }
2012}
2013
Jiri Slaby037ad482006-12-08 02:38:11 -08002014/*
2015 * mxser_wait_until_sent() --- wait until the transmitter is empty
2016 */
2017static void mxser_wait_until_sent(struct tty_struct *tty, int timeout)
2018{
Jiri Slaby55b307d2006-12-08 02:38:14 -08002019 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08002020 unsigned long orig_jiffies, char_time;
2021 int lsr;
2022
2023 if (info->type == PORT_UNKNOWN)
2024 return;
2025
2026 if (info->xmit_fifo_size == 0)
2027 return; /* Just in case.... */
2028
2029 orig_jiffies = jiffies;
2030 /*
2031 * Set the check interval to be 1/5 of the estimated time to
2032 * send a single character, and make it at least 1. The check
2033 * interval should also be less than the timeout.
2034 *
2035 * Note: we have to use pretty tight timings here to satisfy
2036 * the NIST-PCTS.
2037 */
2038 char_time = (info->timeout - HZ / 50) / info->xmit_fifo_size;
2039 char_time = char_time / 5;
2040 if (char_time == 0)
2041 char_time = 1;
2042 if (timeout && timeout < char_time)
2043 char_time = timeout;
2044 /*
2045 * If the transmitter hasn't cleared in twice the approximate
2046 * amount of time to send the entire FIFO, it probably won't
2047 * ever clear. This assumes the UART isn't doing flow
2048 * control, which is currently the case. Hence, if it ever
2049 * takes longer than info->timeout, this is probably due to a
2050 * UART bug of some kind. So, we clamp the timeout parameter at
2051 * 2*info->timeout.
2052 */
2053 if (!timeout || timeout > 2 * info->timeout)
2054 timeout = 2 * info->timeout;
2055#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2056 printk(KERN_DEBUG "In rs_wait_until_sent(%d) check=%lu...",
2057 timeout, char_time);
2058 printk("jiff=%lu...", jiffies);
2059#endif
Jiri Slaby55b307d2006-12-08 02:38:14 -08002060 while (!((lsr = inb(info->ioaddr + UART_LSR)) & UART_LSR_TEMT)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002061#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2062 printk("lsr = %d (jiff=%lu)...", lsr, jiffies);
2063#endif
2064 schedule_timeout_interruptible(char_time);
2065 if (signal_pending(current))
2066 break;
2067 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2068 break;
2069 }
2070 set_current_state(TASK_RUNNING);
2071
2072#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2073 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
2074#endif
2075}
2076
Jiri Slaby037ad482006-12-08 02:38:11 -08002077/*
2078 * This routine is called by tty_hangup() when a hangup is signaled.
2079 */
Adrian Bunk02d3fca2007-02-10 01:44:51 -08002080static void mxser_hangup(struct tty_struct *tty)
Jiri Slaby037ad482006-12-08 02:38:11 -08002081{
Jiri Slaby55b307d2006-12-08 02:38:14 -08002082 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08002083
2084 mxser_flush_buffer(tty);
2085 mxser_shutdown(info);
2086 info->event = 0;
2087 info->count = 0;
2088 info->flags &= ~ASYNC_NORMAL_ACTIVE;
2089 info->tty = NULL;
2090 wake_up_interruptible(&info->open_wait);
2091}
2092
Jiri Slaby037ad482006-12-08 02:38:11 -08002093/*
2094 * mxser_rs_break() --- routine which turns the break handling on or off
2095 */
2096static void mxser_rs_break(struct tty_struct *tty, int break_state)
2097{
Jiri Slaby55b307d2006-12-08 02:38:14 -08002098 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08002099 unsigned long flags;
2100
2101 spin_lock_irqsave(&info->slock, flags);
2102 if (break_state == -1)
Jiri Slaby55b307d2006-12-08 02:38:14 -08002103 outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
2104 info->ioaddr + UART_LCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002105 else
Jiri Slaby55b307d2006-12-08 02:38:14 -08002106 outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
2107 info->ioaddr + UART_LCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002108 spin_unlock_irqrestore(&info->slock, flags);
2109}
2110
Jiri Slaby55b307d2006-12-08 02:38:14 -08002111static void mxser_receive_chars(struct mxser_port *port, int *status)
Jiri Slaby037ad482006-12-08 02:38:11 -08002112{
Jiri Slaby55b307d2006-12-08 02:38:14 -08002113 struct tty_struct *tty = port->tty;
Jiri Slaby037ad482006-12-08 02:38:11 -08002114 unsigned char ch, gdl;
2115 int ignored = 0;
2116 int cnt = 0;
2117 int recv_room;
2118 int max = 256;
Jiri Slaby037ad482006-12-08 02:38:11 -08002119
2120 recv_room = tty->receive_room;
Jiri Slabye079f492006-12-08 02:38:31 -08002121 if ((recv_room == 0) && (!port->ldisc_stop_rx))
Jiri Slaby037ad482006-12-08 02:38:11 -08002122 mxser_stoprx(tty);
Jiri Slaby037ad482006-12-08 02:38:11 -08002123
Jiri Slaby55b307d2006-12-08 02:38:14 -08002124 if (port->board->chip_flag != MOXA_OTHER_UART) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002125
Jiri Slaby3306ce32006-12-08 02:38:13 -08002126 if (*status & UART_LSR_SPECIAL)
Jiri Slaby037ad482006-12-08 02:38:11 -08002127 goto intr_old;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002128 if (port->board->chip_flag == MOXA_MUST_MU860_HWID &&
Jiri Slaby037ad482006-12-08 02:38:11 -08002129 (*status & MOXA_MUST_LSR_RERR))
2130 goto intr_old;
Jiri Slaby037ad482006-12-08 02:38:11 -08002131 if (*status & MOXA_MUST_LSR_RERR)
2132 goto intr_old;
2133
Jiri Slaby55b307d2006-12-08 02:38:14 -08002134 gdl = inb(port->ioaddr + MOXA_MUST_GDL_REGISTER);
Jiri Slaby037ad482006-12-08 02:38:11 -08002135
Jiri Slaby55b307d2006-12-08 02:38:14 -08002136 if (port->board->chip_flag == MOXA_MUST_MU150_HWID)
Jiri Slaby037ad482006-12-08 02:38:11 -08002137 gdl &= MOXA_MUST_GDL_MASK;
2138 if (gdl >= recv_room) {
Jiri Slabye079f492006-12-08 02:38:31 -08002139 if (!port->ldisc_stop_rx)
Jiri Slaby037ad482006-12-08 02:38:11 -08002140 mxser_stoprx(tty);
Jiri Slaby037ad482006-12-08 02:38:11 -08002141 }
2142 while (gdl--) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002143 ch = inb(port->ioaddr + UART_RX);
Jiri Slaby037ad482006-12-08 02:38:11 -08002144 tty_insert_flip_char(tty, ch, 0);
2145 cnt++;
Jiri Slaby037ad482006-12-08 02:38:11 -08002146 }
2147 goto end_intr;
2148 }
Jiri Slabye079f492006-12-08 02:38:31 -08002149intr_old:
Jiri Slaby037ad482006-12-08 02:38:11 -08002150
2151 do {
2152 if (max-- < 0)
2153 break;
Jiri Slaby037ad482006-12-08 02:38:11 -08002154
Jiri Slaby55b307d2006-12-08 02:38:14 -08002155 ch = inb(port->ioaddr + UART_RX);
Jiri Slabye079f492006-12-08 02:38:31 -08002156 if (port->board->chip_flag && (*status & UART_LSR_OE))
Jiri Slaby55b307d2006-12-08 02:38:14 -08002157 outb(0x23, port->ioaddr + UART_FCR);
2158 *status &= port->read_status_mask;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002159 if (*status & port->ignore_status_mask) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002160 if (++ignored > 100)
2161 break;
2162 } else {
2163 char flag = 0;
2164 if (*status & UART_LSR_SPECIAL) {
2165 if (*status & UART_LSR_BI) {
2166 flag = TTY_BREAK;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002167 port->icount.brk++;
Jiri Slaby3306ce32006-12-08 02:38:13 -08002168
Jiri Slaby55b307d2006-12-08 02:38:14 -08002169 if (port->flags & ASYNC_SAK)
Jiri Slaby037ad482006-12-08 02:38:11 -08002170 do_SAK(tty);
2171 } else if (*status & UART_LSR_PE) {
2172 flag = TTY_PARITY;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002173 port->icount.parity++;
Jiri Slaby037ad482006-12-08 02:38:11 -08002174 } else if (*status & UART_LSR_FE) {
2175 flag = TTY_FRAME;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002176 port->icount.frame++;
Jiri Slaby037ad482006-12-08 02:38:11 -08002177 } else if (*status & UART_LSR_OE) {
2178 flag = TTY_OVERRUN;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002179 port->icount.overrun++;
Jiri Slabyf64c84a2007-02-10 01:45:21 -08002180 } else
2181 flag = TTY_BREAK;
Jiri Slaby925e9c12006-12-08 02:38:30 -08002182 }
Jiri Slaby037ad482006-12-08 02:38:11 -08002183 tty_insert_flip_char(tty, ch, flag);
2184 cnt++;
2185 if (cnt >= recv_room) {
Jiri Slabye079f492006-12-08 02:38:31 -08002186 if (!port->ldisc_stop_rx)
Jiri Slaby037ad482006-12-08 02:38:11 -08002187 mxser_stoprx(tty);
Jiri Slaby037ad482006-12-08 02:38:11 -08002188 break;
2189 }
2190
2191 }
2192
Jiri Slaby55b307d2006-12-08 02:38:14 -08002193 if (port->board->chip_flag)
Jiri Slaby037ad482006-12-08 02:38:11 -08002194 break;
Jiri Slaby037ad482006-12-08 02:38:11 -08002195
Jiri Slaby55b307d2006-12-08 02:38:14 -08002196 *status = inb(port->ioaddr + UART_LSR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002197 } while (*status & UART_LSR_DR);
2198
Jiri Slabye079f492006-12-08 02:38:31 -08002199end_intr:
Jiri Slaby55b307d2006-12-08 02:38:14 -08002200 mxvar_log.rxcnt[port->tty->index] += cnt;
2201 port->mon_data.rxcnt += cnt;
2202 port->mon_data.up_rxcnt += cnt;
Jiri Slaby037ad482006-12-08 02:38:11 -08002203
Jan Yenya Kasprzak67d2bc52007-04-23 14:41:02 -07002204 /*
2205 * We are called from an interrupt context with &port->slock
2206 * being held. Drop it temporarily in order to prevent
2207 * recursive locking.
2208 */
2209 spin_unlock(&port->slock);
Jiri Slaby037ad482006-12-08 02:38:11 -08002210 tty_flip_buffer_push(tty);
Jan Yenya Kasprzak67d2bc52007-04-23 14:41:02 -07002211 spin_lock(&port->slock);
Jiri Slaby037ad482006-12-08 02:38:11 -08002212}
2213
Jiri Slaby55b307d2006-12-08 02:38:14 -08002214static void mxser_transmit_chars(struct mxser_port *port)
Jiri Slaby037ad482006-12-08 02:38:11 -08002215{
2216 int count, cnt;
Jiri Slaby037ad482006-12-08 02:38:11 -08002217
Jiri Slaby55b307d2006-12-08 02:38:14 -08002218 if (port->x_char) {
2219 outb(port->x_char, port->ioaddr + UART_TX);
2220 port->x_char = 0;
2221 mxvar_log.txcnt[port->tty->index]++;
2222 port->mon_data.txcnt++;
2223 port->mon_data.up_txcnt++;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002224 port->icount.tx++;
Jiri Slabyf5748742007-02-10 01:45:18 -08002225 return;
Jiri Slaby037ad482006-12-08 02:38:11 -08002226 }
2227
Jiri Slaby55b307d2006-12-08 02:38:14 -08002228 if (port->xmit_buf == 0)
Jiri Slabyf5748742007-02-10 01:45:18 -08002229 return;
Jiri Slaby037ad482006-12-08 02:38:11 -08002230
Jiri Slaby925e9c12006-12-08 02:38:30 -08002231 if ((port->xmit_cnt <= 0) || port->tty->stopped ||
2232 (port->tty->hw_stopped &&
Jiri Slaby55b307d2006-12-08 02:38:14 -08002233 (port->type != PORT_16550A) &&
2234 (!port->board->chip_flag))) {
2235 port->IER &= ~UART_IER_THRI;
2236 outb(port->IER, port->ioaddr + UART_IER);
Jiri Slabyf5748742007-02-10 01:45:18 -08002237 return;
Jiri Slaby037ad482006-12-08 02:38:11 -08002238 }
2239
Jiri Slaby55b307d2006-12-08 02:38:14 -08002240 cnt = port->xmit_cnt;
2241 count = port->xmit_fifo_size;
Jiri Slaby037ad482006-12-08 02:38:11 -08002242 do {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002243 outb(port->xmit_buf[port->xmit_tail++],
2244 port->ioaddr + UART_TX);
2245 port->xmit_tail = port->xmit_tail & (SERIAL_XMIT_SIZE - 1);
2246 if (--port->xmit_cnt <= 0)
Jiri Slaby037ad482006-12-08 02:38:11 -08002247 break;
2248 } while (--count > 0);
Jiri Slaby55b307d2006-12-08 02:38:14 -08002249 mxvar_log.txcnt[port->tty->index] += (cnt - port->xmit_cnt);
Jiri Slaby037ad482006-12-08 02:38:11 -08002250
Jiri Slaby55b307d2006-12-08 02:38:14 -08002251 port->mon_data.txcnt += (cnt - port->xmit_cnt);
2252 port->mon_data.up_txcnt += (cnt - port->xmit_cnt);
Jiri Slaby55b307d2006-12-08 02:38:14 -08002253 port->icount.tx += (cnt - port->xmit_cnt);
Jiri Slaby037ad482006-12-08 02:38:11 -08002254
Jiri Slaby47c85c02007-02-10 01:45:15 -08002255 if (port->xmit_cnt < WAKEUP_CHARS)
2256 tty_wakeup(port->tty);
2257
Jiri Slaby55b307d2006-12-08 02:38:14 -08002258 if (port->xmit_cnt <= 0) {
2259 port->IER &= ~UART_IER_THRI;
2260 outb(port->IER, port->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08002261 }
Jiri Slaby037ad482006-12-08 02:38:11 -08002262}
2263
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002264/*
2265 * This is the serial driver's generic interrupt routine
2266 */
Jiri Slabyb1d1c8d2006-12-08 02:38:31 -08002267static irqreturn_t mxser_interrupt(int irq, void *dev_id)
Jiri Slaby037ad482006-12-08 02:38:11 -08002268{
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002269 int status, iir, i;
2270 struct mxser_board *brd = NULL;
2271 struct mxser_port *port;
2272 int max, irqbits, bits, msr;
Jiri Slaby18b95572007-02-10 01:45:19 -08002273 unsigned int int_cnt, pass_counter = 0;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002274 int handled = IRQ_NONE;
Jiri Slaby037ad482006-12-08 02:38:11 -08002275
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002276 for (i = 0; i < MXSER_BOARDS; i++)
2277 if (dev_id == &mxser_boards[i]) {
2278 brd = dev_id;
2279 break;
Jiri Slaby037ad482006-12-08 02:38:11 -08002280 }
Jiri Slaby037ad482006-12-08 02:38:11 -08002281
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002282 if (i == MXSER_BOARDS)
2283 goto irq_stop;
2284 if (brd == NULL)
2285 goto irq_stop;
Jiri Slabycd7ed642006-12-08 02:38:29 -08002286 max = brd->info->nports;
Jiri Slaby18b95572007-02-10 01:45:19 -08002287 while (pass_counter++ < MXSER_ISR_PASS_LIMIT) {
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002288 irqbits = inb(brd->vector) & brd->vector_mask;
2289 if (irqbits == brd->vector_mask)
Jiri Slaby037ad482006-12-08 02:38:11 -08002290 break;
Jiri Slaby037ad482006-12-08 02:38:11 -08002291
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002292 handled = IRQ_HANDLED;
2293 for (i = 0, bits = 1; i < max; i++, irqbits |= bits, bits <<= 1) {
2294 if (irqbits == brd->vector_mask)
Jiri Slaby037ad482006-12-08 02:38:11 -08002295 break;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002296 if (bits & irqbits)
2297 continue;
2298 port = &brd->ports[i];
Jiri Slaby037ad482006-12-08 02:38:11 -08002299
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002300 int_cnt = 0;
Jiri Slabyf5748742007-02-10 01:45:18 -08002301 spin_lock(&port->slock);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002302 do {
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002303 iir = inb(port->ioaddr + UART_IIR);
2304 if (iir & UART_IIR_NO_INT)
2305 break;
2306 iir &= MOXA_MUST_IIR_MASK;
Jiri Slabyf5748742007-02-10 01:45:18 -08002307 if (!port->tty ||
2308 (port->flags & ASYNC_CLOSING) ||
2309 !(port->flags &
2310 ASYNC_INITIALIZED)) {
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002311 status = inb(port->ioaddr + UART_LSR);
2312 outb(0x27, port->ioaddr + UART_FCR);
2313 inb(port->ioaddr + UART_MSR);
2314 break;
Jiri Slaby037ad482006-12-08 02:38:11 -08002315 }
Jiri Slaby037ad482006-12-08 02:38:11 -08002316
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002317 status = inb(port->ioaddr + UART_LSR);
2318
2319 if (status & UART_LSR_PE)
2320 port->err_shadow |= NPPI_NOTIFY_PARITY;
2321 if (status & UART_LSR_FE)
2322 port->err_shadow |= NPPI_NOTIFY_FRAMING;
2323 if (status & UART_LSR_OE)
2324 port->err_shadow |=
2325 NPPI_NOTIFY_HW_OVERRUN;
2326 if (status & UART_LSR_BI)
2327 port->err_shadow |= NPPI_NOTIFY_BREAK;
2328
2329 if (port->board->chip_flag) {
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002330 if (iir == MOXA_MUST_IIR_GDA ||
2331 iir == MOXA_MUST_IIR_RDA ||
2332 iir == MOXA_MUST_IIR_RTO ||
2333 iir == MOXA_MUST_IIR_LSR)
2334 mxser_receive_chars(port,
2335 &status);
2336
2337 } else {
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002338 status &= port->read_status_mask;
2339 if (status & UART_LSR_DR)
2340 mxser_receive_chars(port,
2341 &status);
2342 }
2343 msr = inb(port->ioaddr + UART_MSR);
2344 if (msr & UART_MSR_ANY_DELTA)
2345 mxser_check_modem_status(port, msr);
2346
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002347 if (port->board->chip_flag) {
2348 if (iir == 0x02 && (status &
2349 UART_LSR_THRE))
2350 mxser_transmit_chars(port);
2351 } else {
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002352 if (status & UART_LSR_THRE)
2353 mxser_transmit_chars(port);
2354 }
2355 } while (int_cnt++ < MXSER_ISR_PASS_LIMIT);
Jiri Slabyf5748742007-02-10 01:45:18 -08002356 spin_unlock(&port->slock);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002357 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002358 }
2359
Jiri Slabye079f492006-12-08 02:38:31 -08002360irq_stop:
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002361 return handled;
2362}
2363
2364static const struct tty_operations mxser_ops = {
2365 .open = mxser_open,
2366 .close = mxser_close,
2367 .write = mxser_write,
2368 .put_char = mxser_put_char,
2369 .flush_chars = mxser_flush_chars,
2370 .write_room = mxser_write_room,
2371 .chars_in_buffer = mxser_chars_in_buffer,
2372 .flush_buffer = mxser_flush_buffer,
2373 .ioctl = mxser_ioctl,
2374 .throttle = mxser_throttle,
2375 .unthrottle = mxser_unthrottle,
2376 .set_termios = mxser_set_termios,
2377 .stop = mxser_stop,
2378 .start = mxser_start,
2379 .hangup = mxser_hangup,
2380 .break_ctl = mxser_rs_break,
2381 .wait_until_sent = mxser_wait_until_sent,
2382 .tiocmget = mxser_tiocmget,
2383 .tiocmset = mxser_tiocmset,
2384};
2385
2386/*
2387 * The MOXA Smartio/Industio serial driver boot-time initialization code!
2388 */
2389
Jiri Slaby2094e752006-12-08 02:38:33 -08002390static void mxser_release_res(struct mxser_board *brd, struct pci_dev *pdev,
2391 unsigned int irq)
Jiri Slaby171d3a82006-12-08 02:38:25 -08002392{
Jiri Slaby171d3a82006-12-08 02:38:25 -08002393 if (irq)
2394 free_irq(brd->irq, brd);
2395 if (pdev != NULL) { /* PCI */
Jiri Slabyfb0c9292006-12-13 00:34:19 -08002396#ifdef CONFIG_PCI
Jiri Slaby171d3a82006-12-08 02:38:25 -08002397 pci_release_region(pdev, 2);
2398 pci_release_region(pdev, 3);
Jiri Slabyfb0c9292006-12-13 00:34:19 -08002399#endif
Jiri Slaby171d3a82006-12-08 02:38:25 -08002400 } else {
Jiri Slabycd7ed642006-12-08 02:38:29 -08002401 release_region(brd->ports[0].ioaddr, 8 * brd->info->nports);
Jiri Slaby171d3a82006-12-08 02:38:25 -08002402 release_region(brd->vector, 1);
2403 }
2404}
2405
Jiri Slaby2094e752006-12-08 02:38:33 -08002406static int __devinit mxser_initbrd(struct mxser_board *brd,
2407 struct pci_dev *pdev)
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002408{
2409 struct mxser_port *info;
2410 unsigned int i;
2411 int retval;
2412
2413 printk(KERN_INFO "max. baud rate = %d bps.\n", brd->ports[0].max_baud);
2414
Jiri Slabycd7ed642006-12-08 02:38:29 -08002415 for (i = 0; i < brd->info->nports; i++) {
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002416 info = &brd->ports[i];
2417 info->board = brd;
2418 info->stop_rx = 0;
2419 info->ldisc_stop_rx = 0;
2420
2421 /* Enhance mode enabled here */
2422 if (brd->chip_flag != MOXA_OTHER_UART)
2423 ENABLE_MOXA_MUST_ENCHANCE_MODE(info->ioaddr);
2424
2425 info->flags = ASYNC_SHARE_IRQ;
2426 info->type = brd->uart_type;
2427
2428 process_txrx_fifo(info);
2429
2430 info->custom_divisor = info->baud_base * 16;
2431 info->close_delay = 5 * HZ / 10;
2432 info->closing_wait = 30 * HZ;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002433 info->normal_termios = mxvar_sdriver->init_termios;
2434 init_waitqueue_head(&info->open_wait);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002435 init_waitqueue_head(&info->delta_msr_wait);
Jiri Slabyf64c84a2007-02-10 01:45:21 -08002436 info->speed = 9600;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002437 memset(&info->mon_data, 0, sizeof(struct mxser_mon));
2438 info->err_shadow = 0;
2439 spin_lock_init(&info->slock);
2440
2441 /* before set INT ISR, disable all int */
2442 outb(inb(info->ioaddr + UART_IER) & 0xf0,
2443 info->ioaddr + UART_IER);
2444 }
Jiri Slaby037ad482006-12-08 02:38:11 -08002445
Jiri Slabyc493edd2007-02-10 01:45:16 -08002446 retval = request_irq(brd->irq, mxser_interrupt, IRQF_SHARED, "mxser",
2447 brd);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002448 if (retval) {
2449 printk(KERN_ERR "Board %s: Request irq failed, IRQ (%d) may "
2450 "conflict with another device.\n",
Jiri Slabycd7ed642006-12-08 02:38:29 -08002451 brd->info->name, brd->irq);
Jiri Slaby171d3a82006-12-08 02:38:25 -08002452 /* We hold resources, we need to release them. */
Jiri Slaby2094e752006-12-08 02:38:33 -08002453 mxser_release_res(brd, pdev, 0);
Jiri Slaby037ad482006-12-08 02:38:11 -08002454 }
Jiri Slaby45257fa2007-02-10 01:45:20 -08002455 return retval;
Jiri Slaby037ad482006-12-08 02:38:11 -08002456}
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 Slabyfb0c9292006-12-13 00:34:19 -08002557#ifdef CONFIG_PCI
Jiri Slaby2094e752006-12-08 02:38:33 -08002558 struct mxser_board *brd;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002559 unsigned int i, j;
2560 unsigned long ioaddress;
Jiri Slaby2094e752006-12-08 02:38:33 -08002561 int retval = -EINVAL;
2562
2563 for (i = 0; i < MXSER_BOARDS; i++)
2564 if (mxser_boards[i].info == NULL)
2565 break;
2566
2567 if (i >= MXSER_BOARDS) {
2568 printk(KERN_ERR "Too many Smartio/Industio family boards found "
2569 "(maximum %d), board not configured\n", MXSER_BOARDS);
2570 goto err;
2571 }
2572
2573 brd = &mxser_boards[i];
2574 brd->idx = i * MXSER_PORTS_PER_BOARD;
2575 printk(KERN_INFO "Found MOXA %s board (BusNo=%d, DevNo=%d)\n",
2576 mxser_cards[ent->driver_data].name,
2577 pdev->bus->number, PCI_SLOT(pdev->devfn));
2578
2579 retval = pci_enable_device(pdev);
2580 if (retval) {
2581 printk(KERN_ERR "Moxa SmartI/O PCI enable fail !\n");
2582 goto err;
2583 }
Jiri Slaby037ad482006-12-08 02:38:11 -08002584
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002585 /* io address */
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002586 ioaddress = pci_resource_start(pdev, 2);
2587 retval = pci_request_region(pdev, 2, "mxser(IO)");
2588 if (retval)
2589 goto err;
2590
Jiri Slaby2094e752006-12-08 02:38:33 -08002591 brd->info = &mxser_cards[ent->driver_data];
Jiri Slabycd7ed642006-12-08 02:38:29 -08002592 for (i = 0; i < brd->info->nports; i++)
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002593 brd->ports[i].ioaddr = ioaddress + 8 * i;
2594
2595 /* vector */
2596 ioaddress = pci_resource_start(pdev, 3);
2597 retval = pci_request_region(pdev, 3, "mxser(vector)");
2598 if (retval)
2599 goto err_relio;
2600 brd->vector = ioaddress;
2601
2602 /* irq */
2603 brd->irq = pdev->irq;
2604
2605 brd->chip_flag = CheckIsMoxaMust(brd->ports[0].ioaddr);
2606 brd->uart_type = PORT_16550A;
2607 brd->vector_mask = 0;
2608
Jiri Slabycd7ed642006-12-08 02:38:29 -08002609 for (i = 0; i < brd->info->nports; i++) {
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002610 for (j = 0; j < UART_INFO_NUM; j++) {
2611 if (Gpci_uart_info[j].type == brd->chip_flag) {
2612 brd->ports[i].max_baud =
2613 Gpci_uart_info[j].max_baud;
2614
2615 /* exception....CP-102 */
Jiri Slabycd7ed642006-12-08 02:38:29 -08002616 if (brd->info->flags & MXSER_HIGHBAUD)
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002617 brd->ports[i].max_baud = 921600;
2618 break;
Jiri Slaby037ad482006-12-08 02:38:11 -08002619 }
2620 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002621 }
2622
2623 if (brd->chip_flag == MOXA_MUST_MU860_HWID) {
Jiri Slabycd7ed642006-12-08 02:38:29 -08002624 for (i = 0; i < brd->info->nports; i++) {
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002625 if (i < 4)
2626 brd->ports[i].opmode_ioaddr = ioaddress + 4;
2627 else
2628 brd->ports[i].opmode_ioaddr = ioaddress + 0x0c;
Jiri Slaby037ad482006-12-08 02:38:11 -08002629 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002630 outb(0, ioaddress + 4); /* default set to RS232 mode */
2631 outb(0, ioaddress + 0x0c); /* default set to RS232 mode */
Jiri Slaby037ad482006-12-08 02:38:11 -08002632 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002633
Jiri Slabycd7ed642006-12-08 02:38:29 -08002634 for (i = 0; i < brd->info->nports; i++) {
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002635 brd->vector_mask |= (1 << i);
2636 brd->ports[i].baud_base = 921600;
2637 }
Jiri Slaby2094e752006-12-08 02:38:33 -08002638
2639 /* mxser_initbrd will hook ISR. */
Jiri Slaby45257fa2007-02-10 01:45:20 -08002640 retval = mxser_initbrd(brd, pdev);
2641 if (retval)
2642 goto err_null;
Jiri Slaby2094e752006-12-08 02:38:33 -08002643
2644 for (i = 0; i < brd->info->nports; i++)
2645 tty_register_device(mxvar_sdriver, brd->idx + i, &pdev->dev);
2646
2647 pci_set_drvdata(pdev, brd);
2648
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002649 return 0;
2650err_relio:
2651 pci_release_region(pdev, 2);
Jiri Slaby45257fa2007-02-10 01:45:20 -08002652err_null:
Jiri Slaby2094e752006-12-08 02:38:33 -08002653 brd->info = NULL;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002654err:
2655 return retval;
Jiri Slabyfb0c9292006-12-13 00:34:19 -08002656#else
2657 return -ENODEV;
2658#endif
Jiri Slaby037ad482006-12-08 02:38:11 -08002659}
2660
Jiri Slaby2094e752006-12-08 02:38:33 -08002661static void __devexit mxser_remove(struct pci_dev *pdev)
2662{
2663 struct mxser_board *brd = pci_get_drvdata(pdev);
2664 unsigned int i;
2665
2666 for (i = 0; i < brd->info->nports; i++)
2667 tty_unregister_device(mxvar_sdriver, brd->idx + i);
2668
2669 mxser_release_res(brd, pdev, 1);
Jiri Slaby45257fa2007-02-10 01:45:20 -08002670 brd->info = NULL;
Jiri Slaby2094e752006-12-08 02:38:33 -08002671}
2672
2673static struct pci_driver mxser_driver = {
2674 .name = "mxser",
2675 .id_table = mxser_pcibrds,
2676 .probe = mxser_probe,
2677 .remove = __devexit_p(mxser_remove)
2678};
2679
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002680static int __init mxser_module_init(void)
Jiri Slaby037ad482006-12-08 02:38:11 -08002681{
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002682 struct mxser_board *brd;
Jiri Slabyeae44362006-12-08 02:38:27 -08002683 unsigned long cap;
2684 unsigned int i, m, isaloop;
Jiri Slaby2094e752006-12-08 02:38:33 -08002685 int retval, b;
Jiri Slaby037ad482006-12-08 02:38:11 -08002686
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002687 pr_debug("Loading module mxser ...\n");
Jiri Slaby037ad482006-12-08 02:38:11 -08002688
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002689 mxvar_sdriver = alloc_tty_driver(MXSER_PORTS + 1);
2690 if (!mxvar_sdriver)
2691 return -ENOMEM;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002692
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002693 printk(KERN_INFO "MOXA Smartio/Industio family driver version %s\n",
2694 MXSER_VERSION);
2695
2696 /* Initialize the tty_driver structure */
Jiri Slaby31f87cf2006-12-29 16:47:34 -08002697 mxvar_sdriver->owner = THIS_MODULE;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002698 mxvar_sdriver->magic = TTY_DRIVER_MAGIC;
Jiri Slabye9307902006-12-08 02:38:35 -08002699 mxvar_sdriver->name = "ttyMI";
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002700 mxvar_sdriver->major = ttymajor;
2701 mxvar_sdriver->minor_start = 0;
2702 mxvar_sdriver->num = MXSER_PORTS + 1;
2703 mxvar_sdriver->type = TTY_DRIVER_TYPE_SERIAL;
2704 mxvar_sdriver->subtype = SERIAL_TYPE_NORMAL;
2705 mxvar_sdriver->init_termios = tty_std_termios;
2706 mxvar_sdriver->init_termios.c_cflag = B9600|CS8|CREAD|HUPCL|CLOCAL;
Jiri Slaby938ef182006-12-08 02:38:28 -08002707 mxvar_sdriver->flags = TTY_DRIVER_REAL_RAW|TTY_DRIVER_DYNAMIC_DEV;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002708 tty_set_operations(mxvar_sdriver, &mxser_ops);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002709
Jiri Slaby938ef182006-12-08 02:38:28 -08002710 retval = tty_register_driver(mxvar_sdriver);
2711 if (retval) {
2712 printk(KERN_ERR "Couldn't install MOXA Smartio/Industio family "
2713 "tty driver !\n");
2714 goto err_put;
2715 }
2716
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002717 mxvar_diagflag = 0;
2718
2719 m = 0;
2720 /* Start finding ISA boards here */
Jiri Slabyeae44362006-12-08 02:38:27 -08002721 for (isaloop = 0; isaloop < 2; isaloop++)
2722 for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
2723 if (!isaloop)
2724 cap = mxserBoardCAP[b]; /* predefined */
2725 else
2726 cap = ioaddr[b]; /* module param */
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002727
Jiri Slabyeae44362006-12-08 02:38:27 -08002728 if (!cap)
2729 continue;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002730
Jiri Slabyeae44362006-12-08 02:38:27 -08002731 brd = &mxser_boards[m];
2732 retval = mxser_get_ISA_conf(cap, brd);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002733
Jiri Slabyeae44362006-12-08 02:38:27 -08002734 if (retval != 0)
2735 printk(KERN_INFO "Found MOXA %s board "
2736 "(CAP=0x%x)\n",
Jiri Slabycd7ed642006-12-08 02:38:29 -08002737 brd->info->name, ioaddr[b]);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002738
Jiri Slabyeae44362006-12-08 02:38:27 -08002739 if (retval <= 0) {
2740 if (retval == MXSER_ERR_IRQ)
2741 printk(KERN_ERR "Invalid interrupt "
2742 "number, board not "
2743 "configured\n");
2744 else if (retval == MXSER_ERR_IRQ_CONFLIT)
2745 printk(KERN_ERR "Invalid interrupt "
2746 "number, board not "
2747 "configured\n");
2748 else if (retval == MXSER_ERR_VECTOR)
2749 printk(KERN_ERR "Invalid interrupt "
2750 "vector, board not "
2751 "configured\n");
2752 else if (retval == MXSER_ERR_IOADDR)
2753 printk(KERN_ERR "Invalid I/O address, "
2754 "board not configured\n");
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002755
Jiri Slaby2094e752006-12-08 02:38:33 -08002756 brd->info = NULL;
Jiri Slabyeae44362006-12-08 02:38:27 -08002757 continue;
2758 }
2759
Jiri Slabyeae44362006-12-08 02:38:27 -08002760 /* mxser_initbrd will hook ISR. */
Jiri Slaby2094e752006-12-08 02:38:33 -08002761 if (mxser_initbrd(brd, NULL) < 0) {
2762 brd->info = NULL;
Jiri Slabyeae44362006-12-08 02:38:27 -08002763 continue;
Jiri Slaby2094e752006-12-08 02:38:33 -08002764 }
Jiri Slabyeae44362006-12-08 02:38:27 -08002765
Jiri Slaby2094e752006-12-08 02:38:33 -08002766 brd->idx = m * MXSER_PORTS_PER_BOARD;
Jiri Slabycd7ed642006-12-08 02:38:29 -08002767 for (i = 0; i < brd->info->nports; i++)
Jiri Slaby2094e752006-12-08 02:38:33 -08002768 tty_register_device(mxvar_sdriver, brd->idx + i,
2769 NULL);
Jiri Slaby938ef182006-12-08 02:38:28 -08002770
Jiri Slabyeae44362006-12-08 02:38:27 -08002771 m++;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002772 }
2773
Jiri Slaby2094e752006-12-08 02:38:33 -08002774 retval = pci_register_driver(&mxser_driver);
2775 if (retval) {
2776 printk(KERN_ERR "Can't register pci driver\n");
2777 if (!m) {
2778 retval = -ENODEV;
2779 goto err_unr;
2780 } /* else: we have some ISA cards under control */
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002781 }
2782
2783 pr_debug("Done.\n");
2784
Jiri Slaby938ef182006-12-08 02:38:28 -08002785 return 0;
2786err_unr:
2787 tty_unregister_driver(mxvar_sdriver);
2788err_put:
2789 put_tty_driver(mxvar_sdriver);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002790 return retval;
Jiri Slaby037ad482006-12-08 02:38:11 -08002791}
2792
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002793static void __exit mxser_module_exit(void)
Jiri Slaby037ad482006-12-08 02:38:11 -08002794{
Jiri Slaby2094e752006-12-08 02:38:33 -08002795 unsigned int i, j;
Jiri Slaby037ad482006-12-08 02:38:11 -08002796
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002797 pr_debug("Unloading module mxser ...\n");
2798
Jiri Slaby2094e752006-12-08 02:38:33 -08002799 pci_unregister_driver(&mxser_driver);
2800
2801 for (i = 0; i < MXSER_BOARDS; i++) /* ISA remains */
2802 if (mxser_boards[i].info != NULL)
2803 for (j = 0; j < mxser_boards[i].info->nports; j++)
2804 tty_unregister_device(mxvar_sdriver,
2805 mxser_boards[i].idx + j);
Jiri Slabyead568c2006-12-08 02:38:26 -08002806 tty_unregister_driver(mxvar_sdriver);
2807 put_tty_driver(mxvar_sdriver);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002808
Jiri Slaby171d3a82006-12-08 02:38:25 -08002809 for (i = 0; i < MXSER_BOARDS; i++)
Jiri Slabycd7ed642006-12-08 02:38:29 -08002810 if (mxser_boards[i].info != NULL)
Jiri Slaby2094e752006-12-08 02:38:33 -08002811 mxser_release_res(&mxser_boards[i], NULL, 1);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002812
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002813 pr_debug("Done.\n");
Jiri Slaby037ad482006-12-08 02:38:11 -08002814}
2815
2816module_init(mxser_module_init);
2817module_exit(mxser_module_exit);