blob: bf1bee4e1f5e6cea411dc276fb946ff231e70346 [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 Slabyc88cb8f2006-12-08 02:38:20 -08007 * This code is loosely based on the 1.8 moxa driver which is based on
8 * Linux serial driver, written by Linus Torvalds, Theodore T'so and
9 * others.
Jiri Slaby037ad482006-12-08 02:38:11 -080010 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
Jiri Slaby037ad482006-12-08 02:38:11 -080016 * Fed through a cleanup, indent and remove of non 2.6 code by Alan Cox
17 * <alan@redhat.com>. The original 1.8 code is available on www.moxa.com.
18 * - Fixed x86_64 cleanness
19 * - Fixed sleep with spinlock held in mxser_send_break
20 */
21
Jiri Slaby037ad482006-12-08 02:38:11 -080022#include <linux/module.h>
Jiri Slaby037ad482006-12-08 02:38:11 -080023#include <linux/errno.h>
24#include <linux/signal.h>
25#include <linux/sched.h>
26#include <linux/timer.h>
27#include <linux/interrupt.h>
28#include <linux/tty.h>
29#include <linux/tty_flip.h>
30#include <linux/serial.h>
31#include <linux/serial_reg.h>
32#include <linux/major.h>
33#include <linux/string.h>
34#include <linux/fcntl.h>
35#include <linux/ptrace.h>
36#include <linux/gfp.h>
37#include <linux/ioport.h>
38#include <linux/mm.h>
Jiri Slaby037ad482006-12-08 02:38:11 -080039#include <linux/delay.h>
40#include <linux/pci.h>
Jiri Slaby1977f032007-10-18 23:40:25 -070041#include <linux/bitops.h>
Jiri Slaby037ad482006-12-08 02:38:11 -080042
43#include <asm/system.h>
44#include <asm/io.h>
45#include <asm/irq.h>
Jiri Slaby037ad482006-12-08 02:38:11 -080046#include <asm/uaccess.h>
47
Jiri Slaby771f2d12006-12-08 02:38:12 -080048#include "mxser_new.h"
Jiri Slaby037ad482006-12-08 02:38:11 -080049
Jiri Slabyf8cce2e2007-10-18 03:06:01 -070050#define MXSER_VERSION "2.0.2" /* 1.10 */
Jiri Slaby037ad482006-12-08 02:38:11 -080051#define MXSERMAJOR 174
52#define MXSERCUMAJOR 175
53
Jiri Slaby037ad482006-12-08 02:38:11 -080054#define MXSER_BOARDS 4 /* Max. boards */
Jiri Slaby037ad482006-12-08 02:38:11 -080055#define MXSER_PORTS_PER_BOARD 8 /* Max. ports per board */
Jiri Slabya8b74de2006-12-08 02:38:34 -080056#define MXSER_PORTS (MXSER_BOARDS * MXSER_PORTS_PER_BOARD)
Jiri Slaby18b95572007-02-10 01:45:19 -080057#define MXSER_ISR_PASS_LIMIT 100
Jiri Slaby037ad482006-12-08 02:38:11 -080058
59#define MXSER_ERR_IOADDR -1
60#define MXSER_ERR_IRQ -2
61#define MXSER_ERR_IRQ_CONFLIT -3
62#define MXSER_ERR_VECTOR -4
63
Jiri Slabyed79ba12007-02-10 01:45:18 -080064/*CheckIsMoxaMust return value*/
65#define MOXA_OTHER_UART 0x00
66#define MOXA_MUST_MU150_HWID 0x01
67#define MOXA_MUST_MU860_HWID 0x02
68
Jiri Slaby037ad482006-12-08 02:38:11 -080069#define WAKEUP_CHARS 256
70
71#define UART_MCR_AFE 0x20
72#define UART_LSR_SPECIAL 0x1E
73
Jiri Slabyf8cce2e2007-10-18 03:06:01 -070074#define PCI_DEVICE_ID_CB108 0x1080
75#define PCI_DEVICE_ID_CB114 0x1142
76#define PCI_DEVICE_ID_CB134I 0x1341
77#define PCI_DEVICE_ID_CP138U 0x1380
78#define PCI_DEVICE_ID_POS104UL 0x1044
79
Jiri Slaby037ad482006-12-08 02:38:11 -080080
Jiri Slaby037ad482006-12-08 02:38:11 -080081#define C168_ASIC_ID 1
82#define C104_ASIC_ID 2
83#define C102_ASIC_ID 0xB
84#define CI132_ASIC_ID 4
85#define CI134_ASIC_ID 3
86#define CI104J_ASIC_ID 5
87
Jiri Slabycd7ed642006-12-08 02:38:29 -080088#define MXSER_HIGHBAUD 1
89#define MXSER_HAS2 2
Jiri Slaby037ad482006-12-08 02:38:11 -080090
91/* This is only for PCI */
Jiri Slabycd7ed642006-12-08 02:38:29 -080092static const struct {
Jiri Slaby037ad482006-12-08 02:38:11 -080093 int type;
94 int tx_fifo;
95 int rx_fifo;
96 int xmit_fifo_size;
97 int rx_high_water;
98 int rx_trigger;
99 int rx_low_water;
100 long max_baud;
Jiri Slabycd7ed642006-12-08 02:38:29 -0800101} Gpci_uart_info[] = {
Jiri Slaby037ad482006-12-08 02:38:11 -0800102 {MOXA_OTHER_UART, 16, 16, 16, 14, 14, 1, 921600L},
103 {MOXA_MUST_MU150_HWID, 64, 64, 64, 48, 48, 16, 230400L},
104 {MOXA_MUST_MU860_HWID, 128, 128, 128, 96, 96, 32, 921600L}
105};
Jiri Slabycd7ed642006-12-08 02:38:29 -0800106#define UART_INFO_NUM ARRAY_SIZE(Gpci_uart_info)
Jiri Slaby037ad482006-12-08 02:38:11 -0800107
Jiri Slabycd7ed642006-12-08 02:38:29 -0800108struct mxser_cardinfo {
109 unsigned int nports;
110 char *name;
111 unsigned int flags;
112};
113
114static const struct mxser_cardinfo mxser_cards[] = {
Jiri Slaby009ae1d2007-10-18 03:06:02 -0700115/* 0*/ { 8, "C168 series", },
116 { 4, "C104 series", },
117 { 4, "CI-104J series", },
118 { 8, "C168H/PCI series", },
119 { 4, "C104H/PCI series", },
Jiri Slabyf8cce2e2007-10-18 03:06:01 -0700120/* 5*/ { 4, "C102 series", MXSER_HAS2 }, /* C102-ISA */
Jiri Slaby009ae1d2007-10-18 03:06:02 -0700121 { 4, "CI-132 series", MXSER_HAS2 },
122 { 4, "CI-134 series", },
123 { 2, "CP-132 series", },
124 { 4, "CP-114 series", },
125/*10*/ { 4, "CT-114 series", },
126 { 2, "CP-102 series", MXSER_HIGHBAUD },
127 { 4, "CP-104U series", },
128 { 8, "CP-168U series", },
129 { 2, "CP-132U series", },
130/*15*/ { 4, "CP-134U series", },
131 { 4, "CP-104JU series", },
Jiri Slabycd7ed642006-12-08 02:38:29 -0800132 { 8, "Moxa UC7000 Serial", }, /* RC7000 */
Jiri Slaby009ae1d2007-10-18 03:06:02 -0700133 { 8, "CP-118U series", },
134 { 2, "CP-102UL series", },
135/*20*/ { 2, "CP-102U series", },
136 { 8, "CP-118EL series", },
137 { 8, "CP-168EL series", },
Jiri Slabyf8cce2e2007-10-18 03:06:01 -0700138 { 4, "CP-104EL series", },
139 { 8, "CB-108 series", },
140/*25*/ { 4, "CB-114 series", },
141 { 4, "CB-134I series", },
142 { 8, "CP-138U series", },
143 { 4, "POS-104UL series", }
Jiri Slabycd7ed642006-12-08 02:38:29 -0800144};
145
146/* driver_data correspond to the lines in the structure above
147 see also ISA probe function before you change something */
Jiri Slaby037ad482006-12-08 02:38:11 -0800148static struct pci_device_id mxser_pcibrds[] = {
Jiri Slaby931ecbb2007-10-18 03:06:01 -0700149 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_C168), .driver_data = 3 },
150 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_C104), .driver_data = 4 },
151 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP132), .driver_data = 8 },
152 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP114), .driver_data = 9 },
153 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CT114), .driver_data = 10 },
154 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102), .driver_data = 11 },
155 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104U), .driver_data = 12 },
156 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP168U), .driver_data = 13 },
157 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP132U), .driver_data = 14 },
158 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP134U), .driver_data = 15 },
159 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104JU),.driver_data = 16 },
160 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_RC7000), .driver_data = 17 },
161 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP118U), .driver_data = 18 },
162 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102UL),.driver_data = 19 },
163 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102U), .driver_data = 20 },
164 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP118EL),.driver_data = 21 },
165 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP168EL),.driver_data = 22 },
166 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104EL),.driver_data = 23 },
Jiri Slabyf8cce2e2007-10-18 03:06:01 -0700167 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB108), .driver_data = 24 },
168 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB114), .driver_data = 25 },
169 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB134I), .driver_data = 26 },
170 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP138U), .driver_data = 27 },
171 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_POS104UL), .driver_data = 28 },
Jiri Slaby3306ce32006-12-08 02:38:13 -0800172 { }
Jiri Slaby037ad482006-12-08 02:38:11 -0800173};
Jiri Slaby037ad482006-12-08 02:38:11 -0800174MODULE_DEVICE_TABLE(pci, mxser_pcibrds);
175
Jiri Slabyf64c84a2007-02-10 01:45:21 -0800176static int mxvar_baud_table[] = {
177 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400,
178 4800, 9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600
179};
180static unsigned int mxvar_baud_table1[] = {
181 0, B50, B75, B110, B134, B150, B200, B300, B600, B1200, B1800, B2400,
182 B4800, B9600, B19200, B38400, B57600, B115200, B230400, B460800, B921600
183};
184#define BAUD_TABLE_NO ARRAY_SIZE(mxvar_baud_table)
185
186#define B_SPEC B2000000
187
Jiri Slaby037ad482006-12-08 02:38:11 -0800188static int ioaddr[MXSER_BOARDS] = { 0, 0, 0, 0 };
189static int ttymajor = MXSERMAJOR;
190static int calloutmajor = MXSERCUMAJOR;
Jiri Slaby037ad482006-12-08 02:38:11 -0800191
192/* Variables for insmod */
193
194MODULE_AUTHOR("Casper Yang");
195MODULE_DESCRIPTION("MOXA Smartio/Industio Family Multiport Board Device Driver");
196module_param_array(ioaddr, int, NULL, 0);
197module_param(ttymajor, int, 0);
Jiri Slaby037ad482006-12-08 02:38:11 -0800198MODULE_LICENSE("GPL");
199
200struct mxser_log {
201 int tick;
202 unsigned long rxcnt[MXSER_PORTS];
203 unsigned long txcnt[MXSER_PORTS];
204};
205
206
207struct mxser_mon {
208 unsigned long rxcnt;
209 unsigned long txcnt;
210 unsigned long up_rxcnt;
211 unsigned long up_txcnt;
212 int modem_status;
213 unsigned char hold_reason;
214};
215
216struct mxser_mon_ext {
217 unsigned long rx_cnt[32];
218 unsigned long tx_cnt[32];
219 unsigned long up_rxcnt[32];
220 unsigned long up_txcnt[32];
221 int modem_status[32];
222
223 long baudrate[32];
224 int databits[32];
225 int stopbits[32];
226 int parity[32];
227 int flowctrl[32];
228 int fifo[32];
229 int iftype[32];
230};
231
Jiri Slaby55b307d2006-12-08 02:38:14 -0800232struct mxser_board;
Jiri Slaby037ad482006-12-08 02:38:11 -0800233
Jiri Slaby55b307d2006-12-08 02:38:14 -0800234struct mxser_port {
235 struct mxser_board *board;
236 struct tty_struct *tty;
237
238 unsigned long ioaddr;
239 unsigned long opmode_ioaddr;
240 int max_baud;
241
Jiri Slaby037ad482006-12-08 02:38:11 -0800242 int rx_high_water;
243 int rx_trigger; /* Rx fifo trigger level */
244 int rx_low_water;
245 int baud_base; /* max. speed */
Jiri Slaby55b307d2006-12-08 02:38:14 -0800246 long realbaud;
Jiri Slaby037ad482006-12-08 02:38:11 -0800247 int type; /* UART type */
Jiri Slaby55b307d2006-12-08 02:38:14 -0800248 int flags; /* defined in tty.h */
Jiri Slabyf64c84a2007-02-10 01:45:21 -0800249 int speed;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800250
251 int x_char; /* xon/xoff character */
252 int IER; /* Interrupt Enable Register */
253 int MCR; /* Modem control register */
254
255 unsigned char stop_rx;
256 unsigned char ldisc_stop_rx;
257
258 int custom_divisor;
259 int close_delay;
260 unsigned short closing_wait;
261 unsigned char err_shadow;
262 unsigned long event;
263
264 int count; /* # of fd on device */
265 int blocked_open; /* # of blocked opens */
266 struct async_icount icount; /* kernel counters for 4 input interrupts */
267 int timeout;
268
Jiri Slaby037ad482006-12-08 02:38:11 -0800269 int read_status_mask;
270 int ignore_status_mask;
271 int xmit_fifo_size;
Jiri Slaby037ad482006-12-08 02:38:11 -0800272 unsigned char *xmit_buf;
273 int xmit_head;
274 int xmit_tail;
275 int xmit_cnt;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800276
Alan Cox606d0992006-12-08 02:38:45 -0800277 struct ktermios normal_termios;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800278
279 struct mxser_mon mon_data;
280
281 spinlock_t slock;
Jiri Slaby037ad482006-12-08 02:38:11 -0800282 wait_queue_head_t open_wait;
Jiri Slaby037ad482006-12-08 02:38:11 -0800283 wait_queue_head_t delta_msr_wait;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800284};
285
286struct mxser_board {
Jiri Slaby2094e752006-12-08 02:38:33 -0800287 unsigned int idx;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800288 int irq;
Jiri Slabycd7ed642006-12-08 02:38:29 -0800289 const struct mxser_cardinfo *info;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800290 unsigned long vector;
291 unsigned long vector_mask;
292
293 int chip_flag;
294 int uart_type;
295
296 struct mxser_port ports[MXSER_PORTS_PER_BOARD];
Jiri Slaby037ad482006-12-08 02:38:11 -0800297};
298
299struct mxser_mstatus {
300 tcflag_t cflag;
301 int cts;
302 int dsr;
303 int ri;
304 int dcd;
305};
306
307static struct mxser_mstatus GMStatus[MXSER_PORTS];
308
309static int mxserBoardCAP[MXSER_BOARDS] = {
310 0, 0, 0, 0
311 /* 0x180, 0x280, 0x200, 0x320 */
312};
313
Jiri Slaby55b307d2006-12-08 02:38:14 -0800314static struct mxser_board mxser_boards[MXSER_BOARDS];
Jiri Slaby037ad482006-12-08 02:38:11 -0800315static struct tty_driver *mxvar_sdriver;
Jiri Slaby037ad482006-12-08 02:38:11 -0800316static struct mxser_log mxvar_log;
317static int mxvar_diagflag;
318static unsigned char mxser_msr[MXSER_PORTS + 1];
319static struct mxser_mon_ext mon_data_ext;
320static int mxser_set_baud_method[MXSER_PORTS + 1];
Jiri Slaby037ad482006-12-08 02:38:11 -0800321
Jiri Slabyfb0c9292006-12-13 00:34:19 -0800322#ifdef CONFIG_PCI
Jiri Slabyc76b0932007-02-10 01:45:07 -0800323static int __devinit CheckIsMoxaMust(int io)
Jiri Slaby037ad482006-12-08 02:38:11 -0800324{
325 u8 oldmcr, hwid;
326 int i;
327
328 outb(0, io + UART_LCR);
329 DISABLE_MOXA_MUST_ENCHANCE_MODE(io);
330 oldmcr = inb(io + UART_MCR);
331 outb(0, io + UART_MCR);
332 SET_MOXA_MUST_XON1_VALUE(io, 0x11);
333 if ((hwid = inb(io + UART_MCR)) != 0) {
334 outb(oldmcr, io + UART_MCR);
335 return MOXA_OTHER_UART;
336 }
337
338 GET_MOXA_MUST_HARDWARE_ID(io, &hwid);
Jiri Slabycd7ed642006-12-08 02:38:29 -0800339 for (i = 1; i < UART_INFO_NUM; i++) { /* 0 = OTHER_UART */
340 if (hwid == Gpci_uart_info[i].type)
Jiri Slaby037ad482006-12-08 02:38:11 -0800341 return (int)hwid;
342 }
343 return MOXA_OTHER_UART;
344}
Jiri Slabyfb0c9292006-12-13 00:34:19 -0800345#endif
Jiri Slaby037ad482006-12-08 02:38:11 -0800346
Jiri Slaby55b307d2006-12-08 02:38:14 -0800347static void process_txrx_fifo(struct mxser_port *info)
Jiri Slaby037ad482006-12-08 02:38:11 -0800348{
349 int i;
350
351 if ((info->type == PORT_16450) || (info->type == PORT_8250)) {
352 info->rx_trigger = 1;
353 info->rx_high_water = 1;
354 info->rx_low_water = 1;
355 info->xmit_fifo_size = 1;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800356 } else
357 for (i = 0; i < UART_INFO_NUM; i++)
358 if (info->board->chip_flag == Gpci_uart_info[i].type) {
Jiri Slaby037ad482006-12-08 02:38:11 -0800359 info->rx_trigger = Gpci_uart_info[i].rx_trigger;
360 info->rx_low_water = Gpci_uart_info[i].rx_low_water;
361 info->rx_high_water = Gpci_uart_info[i].rx_high_water;
362 info->xmit_fifo_size = Gpci_uart_info[i].xmit_fifo_size;
363 break;
364 }
Jiri Slaby037ad482006-12-08 02:38:11 -0800365}
366
Jiri Slaby55b307d2006-12-08 02:38:14 -0800367static unsigned char mxser_get_msr(int baseaddr, int mode, int port)
Jiri Slaby037ad482006-12-08 02:38:11 -0800368{
369 unsigned char status = 0;
370
371 status = inb(baseaddr + UART_MSR);
372
373 mxser_msr[port] &= 0x0F;
374 mxser_msr[port] |= status;
375 status = mxser_msr[port];
376 if (mode)
377 mxser_msr[port] = 0;
378
379 return status;
380}
381
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800382static int mxser_block_til_ready(struct tty_struct *tty, struct file *filp,
383 struct mxser_port *port)
384{
385 DECLARE_WAITQUEUE(wait, current);
386 int retval;
387 int do_clocal = 0;
388 unsigned long flags;
389
390 /*
391 * If non-blocking mode is set, or the port is not enabled,
392 * then make the check up front and then exit.
393 */
Jiri Slaby214efeb2006-12-08 02:38:25 -0800394 if ((filp->f_flags & O_NONBLOCK) ||
395 test_bit(TTY_IO_ERROR, &tty->flags)) {
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800396 port->flags |= ASYNC_NORMAL_ACTIVE;
397 return 0;
398 }
399
400 if (tty->termios->c_cflag & CLOCAL)
401 do_clocal = 1;
402
403 /*
404 * Block waiting for the carrier detect and the line to become
405 * free (i.e., not in use by the callout). While we are in
406 * this loop, port->count is dropped by one, so that
407 * mxser_close() knows when to free things. We restore it upon
408 * exit, either normal or abnormal.
409 */
410 retval = 0;
411 add_wait_queue(&port->open_wait, &wait);
412
413 spin_lock_irqsave(&port->slock, flags);
414 if (!tty_hung_up_p(filp))
415 port->count--;
416 spin_unlock_irqrestore(&port->slock, flags);
417 port->blocked_open++;
418 while (1) {
419 spin_lock_irqsave(&port->slock, flags);
420 outb(inb(port->ioaddr + UART_MCR) |
421 UART_MCR_DTR | UART_MCR_RTS, port->ioaddr + UART_MCR);
422 spin_unlock_irqrestore(&port->slock, flags);
423 set_current_state(TASK_INTERRUPTIBLE);
424 if (tty_hung_up_p(filp) || !(port->flags & ASYNC_INITIALIZED)) {
425 if (port->flags & ASYNC_HUP_NOTIFY)
426 retval = -EAGAIN;
427 else
428 retval = -ERESTARTSYS;
429 break;
430 }
431 if (!(port->flags & ASYNC_CLOSING) &&
432 (do_clocal ||
433 (inb(port->ioaddr + UART_MSR) & UART_MSR_DCD)))
434 break;
435 if (signal_pending(current)) {
436 retval = -ERESTARTSYS;
437 break;
438 }
439 schedule();
440 }
441 set_current_state(TASK_RUNNING);
442 remove_wait_queue(&port->open_wait, &wait);
443 if (!tty_hung_up_p(filp))
444 port->count++;
445 port->blocked_open--;
446 if (retval)
447 return retval;
448 port->flags |= ASYNC_NORMAL_ACTIVE;
449 return 0;
450}
451
452static int mxser_set_baud(struct mxser_port *info, long newspd)
453{
Jiri Slabyf64c84a2007-02-10 01:45:21 -0800454 unsigned int i;
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800455 int quot = 0;
456 unsigned char cval;
457 int ret = 0;
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800458
459 if (!info->tty || !info->tty->termios)
460 return ret;
461
462 if (!(info->ioaddr))
463 return ret;
464
465 if (newspd > info->max_baud)
466 return 0;
467
468 info->realbaud = newspd;
Jiri Slabyf64c84a2007-02-10 01:45:21 -0800469 for (i = 0; i < BAUD_TABLE_NO; i++)
470 if (newspd == mxvar_baud_table[i])
471 break;
472 if (i == BAUD_TABLE_NO) {
473 quot = info->baud_base / info->speed;
474 if (info->speed <= 0 || info->speed > info->max_baud)
475 quot = 0;
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800476 } else {
Jiri Slabyf64c84a2007-02-10 01:45:21 -0800477 if (newspd == 134) {
478 quot = (2 * info->baud_base / 269);
479 } else if (newspd) {
480 quot = info->baud_base / newspd;
481 if (quot == 0)
482 quot = 1;
483 } else {
484 quot = 0;
485 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800486 }
487
488 info->timeout = ((info->xmit_fifo_size * HZ * 10 * quot) / info->baud_base);
489 info->timeout += HZ / 50; /* Add .02 seconds of slop */
490
491 if (quot) {
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800492 info->MCR |= UART_MCR_DTR;
493 outb(info->MCR, info->ioaddr + UART_MCR);
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800494 } else {
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800495 info->MCR &= ~UART_MCR_DTR;
496 outb(info->MCR, info->ioaddr + UART_MCR);
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800497 return ret;
498 }
499
500 cval = inb(info->ioaddr + UART_LCR);
501
502 outb(cval | UART_LCR_DLAB, info->ioaddr + UART_LCR); /* set DLAB */
503
504 outb(quot & 0xff, info->ioaddr + UART_DLL); /* LS of divisor */
505 outb(quot >> 8, info->ioaddr + UART_DLM); /* MS of divisor */
506 outb(cval, info->ioaddr + UART_LCR); /* reset DLAB */
507
Jiri Slabyf64c84a2007-02-10 01:45:21 -0800508 if (i == BAUD_TABLE_NO) {
509 quot = info->baud_base % info->speed;
510 quot *= 8;
511 if ((quot % info->speed) > (info->speed / 2)) {
512 quot /= info->speed;
513 quot++;
514 } else {
515 quot /= info->speed;
516 }
517 SET_MOXA_MUST_ENUM_VALUE(info->ioaddr, quot);
518 } else
519 SET_MOXA_MUST_ENUM_VALUE(info->ioaddr, 0);
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800520
521 return ret;
522}
523
524/*
525 * This routine is called to set the UART divisor registers to match
526 * the specified baud rate for a serial port.
527 */
528static int mxser_change_speed(struct mxser_port *info,
Alan Cox606d0992006-12-08 02:38:45 -0800529 struct ktermios *old_termios)
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800530{
531 unsigned cflag, cval, fcr;
532 int ret = 0;
533 unsigned char status;
534 long baud;
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800535
536 if (!info->tty || !info->tty->termios)
537 return ret;
538 cflag = info->tty->termios->c_cflag;
539 if (!(info->ioaddr))
540 return ret;
541
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800542 if (mxser_set_baud_method[info->tty->index] == 0) {
Jiri Slabyf64c84a2007-02-10 01:45:21 -0800543 if ((cflag & CBAUD) == B_SPEC)
544 baud = info->speed;
545 else
546 baud = tty_get_baud_rate(info->tty);
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800547 mxser_set_baud(info, baud);
548 }
549
550 /* byte size and parity */
551 switch (cflag & CSIZE) {
552 case CS5:
553 cval = 0x00;
554 break;
555 case CS6:
556 cval = 0x01;
557 break;
558 case CS7:
559 cval = 0x02;
560 break;
561 case CS8:
562 cval = 0x03;
563 break;
564 default:
565 cval = 0x00;
566 break; /* too keep GCC shut... */
567 }
568 if (cflag & CSTOPB)
569 cval |= 0x04;
570 if (cflag & PARENB)
571 cval |= UART_LCR_PARITY;
572 if (!(cflag & PARODD))
573 cval |= UART_LCR_EPAR;
574 if (cflag & CMSPAR)
575 cval |= UART_LCR_SPAR;
576
577 if ((info->type == PORT_8250) || (info->type == PORT_16450)) {
578 if (info->board->chip_flag) {
579 fcr = UART_FCR_ENABLE_FIFO;
580 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
581 SET_MOXA_MUST_FIFO_VALUE(info);
582 } else
583 fcr = 0;
584 } else {
585 fcr = UART_FCR_ENABLE_FIFO;
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800586 if (info->board->chip_flag) {
587 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
588 SET_MOXA_MUST_FIFO_VALUE(info);
589 } else {
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800590 switch (info->rx_trigger) {
591 case 1:
592 fcr |= UART_FCR_TRIGGER_1;
593 break;
594 case 4:
595 fcr |= UART_FCR_TRIGGER_4;
596 break;
597 case 8:
598 fcr |= UART_FCR_TRIGGER_8;
599 break;
600 default:
601 fcr |= UART_FCR_TRIGGER_14;
602 break;
603 }
604 }
605 }
606
607 /* CTS flow control flag and modem status interrupts */
608 info->IER &= ~UART_IER_MSI;
609 info->MCR &= ~UART_MCR_AFE;
610 if (cflag & CRTSCTS) {
611 info->flags |= ASYNC_CTS_FLOW;
612 info->IER |= UART_IER_MSI;
613 if ((info->type == PORT_16550A) || (info->board->chip_flag)) {
614 info->MCR |= UART_MCR_AFE;
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800615 } else {
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800616 status = inb(info->ioaddr + UART_MSR);
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800617 if (info->tty->hw_stopped) {
618 if (status & UART_MSR_CTS) {
619 info->tty->hw_stopped = 0;
620 if (info->type != PORT_16550A &&
621 !info->board->chip_flag) {
622 outb(info->IER & ~UART_IER_THRI,
623 info->ioaddr +
624 UART_IER);
625 info->IER |= UART_IER_THRI;
626 outb(info->IER, info->ioaddr +
627 UART_IER);
628 }
Jiri Slaby47c85c02007-02-10 01:45:15 -0800629 tty_wakeup(info->tty);
630 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800631 } else {
632 if (!(status & UART_MSR_CTS)) {
633 info->tty->hw_stopped = 1;
634 if ((info->type != PORT_16550A) &&
635 (!info->board->chip_flag)) {
636 info->IER &= ~UART_IER_THRI;
637 outb(info->IER, info->ioaddr +
638 UART_IER);
639 }
640 }
641 }
642 }
643 } else {
644 info->flags &= ~ASYNC_CTS_FLOW;
645 }
646 outb(info->MCR, info->ioaddr + UART_MCR);
647 if (cflag & CLOCAL) {
648 info->flags &= ~ASYNC_CHECK_CD;
649 } else {
650 info->flags |= ASYNC_CHECK_CD;
651 info->IER |= UART_IER_MSI;
652 }
653 outb(info->IER, info->ioaddr + UART_IER);
654
655 /*
656 * Set up parity check flag
657 */
658 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
659 if (I_INPCK(info->tty))
660 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
661 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
662 info->read_status_mask |= UART_LSR_BI;
663
664 info->ignore_status_mask = 0;
665
666 if (I_IGNBRK(info->tty)) {
667 info->ignore_status_mask |= UART_LSR_BI;
668 info->read_status_mask |= UART_LSR_BI;
669 /*
670 * If we're ignore parity and break indicators, ignore
671 * overruns too. (For real raw support).
672 */
673 if (I_IGNPAR(info->tty)) {
674 info->ignore_status_mask |=
675 UART_LSR_OE |
676 UART_LSR_PE |
677 UART_LSR_FE;
678 info->read_status_mask |=
679 UART_LSR_OE |
680 UART_LSR_PE |
681 UART_LSR_FE;
682 }
683 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800684 if (info->board->chip_flag) {
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800685 SET_MOXA_MUST_XON1_VALUE(info->ioaddr, START_CHAR(info->tty));
686 SET_MOXA_MUST_XOFF1_VALUE(info->ioaddr, STOP_CHAR(info->tty));
687 if (I_IXON(info->tty)) {
688 ENABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
689 } else {
690 DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
691 }
692 if (I_IXOFF(info->tty)) {
693 ENABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
694 } else {
695 DISABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
696 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800697 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800698
699
700 outb(fcr, info->ioaddr + UART_FCR); /* set fcr */
701 outb(cval, info->ioaddr + UART_LCR);
702
703 return ret;
704}
705
706static void mxser_check_modem_status(struct mxser_port *port, int status)
707{
708 /* update input line counters */
709 if (status & UART_MSR_TERI)
710 port->icount.rng++;
711 if (status & UART_MSR_DDSR)
712 port->icount.dsr++;
713 if (status & UART_MSR_DDCD)
714 port->icount.dcd++;
715 if (status & UART_MSR_DCTS)
716 port->icount.cts++;
717 port->mon_data.modem_status = status;
718 wake_up_interruptible(&port->delta_msr_wait);
719
720 if ((port->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
721 if (status & UART_MSR_DCD)
722 wake_up_interruptible(&port->open_wait);
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800723 }
724
725 if (port->flags & ASYNC_CTS_FLOW) {
726 if (port->tty->hw_stopped) {
727 if (status & UART_MSR_CTS) {
728 port->tty->hw_stopped = 0;
729
730 if ((port->type != PORT_16550A) &&
731 (!port->board->chip_flag)) {
732 outb(port->IER & ~UART_IER_THRI,
733 port->ioaddr + UART_IER);
734 port->IER |= UART_IER_THRI;
735 outb(port->IER, port->ioaddr +
736 UART_IER);
737 }
Jiri Slaby47c85c02007-02-10 01:45:15 -0800738 tty_wakeup(port->tty);
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800739 }
740 } else {
741 if (!(status & UART_MSR_CTS)) {
742 port->tty->hw_stopped = 1;
743 if (port->type != PORT_16550A &&
744 !port->board->chip_flag) {
745 port->IER &= ~UART_IER_THRI;
746 outb(port->IER, port->ioaddr +
747 UART_IER);
748 }
749 }
750 }
751 }
752}
753
754static int mxser_startup(struct mxser_port *info)
755{
756 unsigned long page;
757 unsigned long flags;
758
759 page = __get_free_page(GFP_KERNEL);
760 if (!page)
761 return -ENOMEM;
762
763 spin_lock_irqsave(&info->slock, flags);
764
765 if (info->flags & ASYNC_INITIALIZED) {
766 free_page(page);
767 spin_unlock_irqrestore(&info->slock, flags);
768 return 0;
769 }
770
771 if (!info->ioaddr || !info->type) {
772 if (info->tty)
773 set_bit(TTY_IO_ERROR, &info->tty->flags);
774 free_page(page);
775 spin_unlock_irqrestore(&info->slock, flags);
776 return 0;
777 }
778 if (info->xmit_buf)
779 free_page(page);
780 else
781 info->xmit_buf = (unsigned char *) page;
782
783 /*
784 * Clear the FIFO buffers and disable them
785 * (they will be reenabled in mxser_change_speed())
786 */
787 if (info->board->chip_flag)
788 outb((UART_FCR_CLEAR_RCVR |
789 UART_FCR_CLEAR_XMIT |
790 MOXA_MUST_FCR_GDA_MODE_ENABLE), info->ioaddr + UART_FCR);
791 else
792 outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
793 info->ioaddr + UART_FCR);
794
795 /*
796 * At this point there's no way the LSR could still be 0xFF;
797 * if it is, then bail out, because there's likely no UART
798 * here.
799 */
800 if (inb(info->ioaddr + UART_LSR) == 0xff) {
801 spin_unlock_irqrestore(&info->slock, flags);
802 if (capable(CAP_SYS_ADMIN)) {
803 if (info->tty)
804 set_bit(TTY_IO_ERROR, &info->tty->flags);
805 return 0;
806 } else
807 return -ENODEV;
808 }
809
810 /*
811 * Clear the interrupt registers.
812 */
813 (void) inb(info->ioaddr + UART_LSR);
814 (void) inb(info->ioaddr + UART_RX);
815 (void) inb(info->ioaddr + UART_IIR);
816 (void) inb(info->ioaddr + UART_MSR);
817
818 /*
819 * Now, initialize the UART
820 */
821 outb(UART_LCR_WLEN8, info->ioaddr + UART_LCR); /* reset DLAB */
822 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
823 outb(info->MCR, info->ioaddr + UART_MCR);
824
825 /*
826 * Finally, enable interrupts
827 */
828 info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800829
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800830 if (info->board->chip_flag)
831 info->IER |= MOXA_MUST_IER_EGDAI;
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800832 outb(info->IER, info->ioaddr + UART_IER); /* enable interrupts */
833
834 /*
835 * And clear the interrupt registers again for luck.
836 */
837 (void) inb(info->ioaddr + UART_LSR);
838 (void) inb(info->ioaddr + UART_RX);
839 (void) inb(info->ioaddr + UART_IIR);
840 (void) inb(info->ioaddr + UART_MSR);
841
842 if (info->tty)
843 clear_bit(TTY_IO_ERROR, &info->tty->flags);
844 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
845
846 /*
847 * and set the speed of the serial port
848 */
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800849 mxser_change_speed(info, NULL);
Jiri Slaby5ff72052007-02-10 01:45:23 -0800850 info->flags |= ASYNC_INITIALIZED;
Jiri Slabyf64c84a2007-02-10 01:45:21 -0800851 spin_unlock_irqrestore(&info->slock, flags);
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800852
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800853 return 0;
854}
855
856/*
857 * This routine will shutdown a serial port; interrupts maybe disabled, and
858 * DTR is dropped if the hangup on close termio flag is on.
859 */
860static void mxser_shutdown(struct mxser_port *info)
861{
862 unsigned long flags;
863
864 if (!(info->flags & ASYNC_INITIALIZED))
865 return;
866
867 spin_lock_irqsave(&info->slock, flags);
868
869 /*
870 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
871 * here so the queue might never be waken up
872 */
873 wake_up_interruptible(&info->delta_msr_wait);
874
875 /*
876 * Free the IRQ, if necessary
877 */
878 if (info->xmit_buf) {
879 free_page((unsigned long) info->xmit_buf);
880 info->xmit_buf = NULL;
881 }
882
883 info->IER = 0;
884 outb(0x00, info->ioaddr + UART_IER);
885
886 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
887 info->MCR &= ~(UART_MCR_DTR | UART_MCR_RTS);
888 outb(info->MCR, info->ioaddr + UART_MCR);
889
890 /* clear Rx/Tx FIFO's */
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800891 if (info->board->chip_flag)
892 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT |
893 MOXA_MUST_FCR_GDA_MODE_ENABLE,
894 info->ioaddr + UART_FCR);
895 else
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800896 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
897 info->ioaddr + UART_FCR);
898
899 /* read data port to reset things */
900 (void) inb(info->ioaddr + UART_RX);
901
902 if (info->tty)
903 set_bit(TTY_IO_ERROR, &info->tty->flags);
904
905 info->flags &= ~ASYNC_INITIALIZED;
906
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800907 if (info->board->chip_flag)
908 SET_MOXA_MUST_NO_SOFTWARE_FLOW_CONTROL(info->ioaddr);
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800909
910 spin_unlock_irqrestore(&info->slock, flags);
911}
912
Jiri Slaby037ad482006-12-08 02:38:11 -0800913/*
914 * This routine is called whenever a serial port is opened. It
915 * enables interrupts for a serial port, linking in its async structure into
916 * the IRQ chain. It also performs the serial-specific
917 * initialization for the tty structure.
918 */
919static int mxser_open(struct tty_struct *tty, struct file *filp)
920{
Jiri Slaby55b307d2006-12-08 02:38:14 -0800921 struct mxser_port *info;
Jiri Slaby5ff72052007-02-10 01:45:23 -0800922 unsigned long flags;
Jiri Slaby037ad482006-12-08 02:38:11 -0800923 int retval, line;
924
Jiri Slaby037ad482006-12-08 02:38:11 -0800925 line = tty->index;
926 if (line == MXSER_PORTS)
927 return 0;
928 if (line < 0 || line > MXSER_PORTS)
929 return -ENODEV;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800930 info = &mxser_boards[line / MXSER_PORTS_PER_BOARD].ports[line % MXSER_PORTS_PER_BOARD];
931 if (!info->ioaddr)
Jiri Slaby037ad482006-12-08 02:38:11 -0800932 return -ENODEV;
933
934 tty->driver_data = info;
935 info->tty = tty;
936 /*
937 * Start up serial port
938 */
Jiri Slaby5ff72052007-02-10 01:45:23 -0800939 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby3306ce32006-12-08 02:38:13 -0800940 info->count++;
Jiri Slaby5ff72052007-02-10 01:45:23 -0800941 spin_unlock_irqrestore(&info->slock, flags);
Jiri Slaby037ad482006-12-08 02:38:11 -0800942 retval = mxser_startup(info);
943 if (retval)
944 return retval;
945
946 retval = mxser_block_til_ready(tty, filp, info);
947 if (retval)
948 return retval;
949
Jiri Slabye079f492006-12-08 02:38:31 -0800950 /* unmark here for very high baud rate (ex. 921600 bps) used */
Jiri Slaby037ad482006-12-08 02:38:11 -0800951 tty->low_latency = 1;
952 return 0;
953}
954
955/*
956 * This routine is called when the serial port gets closed. First, we
957 * wait for the last remaining data to be sent. Then, we unlink its
958 * async structure from the interrupt chain if necessary, and we free
959 * that IRQ if nothing is left in the chain.
960 */
961static void mxser_close(struct tty_struct *tty, struct file *filp)
962{
Jiri Slaby55b307d2006-12-08 02:38:14 -0800963 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -0800964
965 unsigned long timeout;
966 unsigned long flags;
Jiri Slaby037ad482006-12-08 02:38:11 -0800967
968 if (tty->index == MXSER_PORTS)
969 return;
970 if (!info)
971 return;
972
973 spin_lock_irqsave(&info->slock, flags);
974
975 if (tty_hung_up_p(filp)) {
976 spin_unlock_irqrestore(&info->slock, flags);
977 return;
978 }
979 if ((tty->count == 1) && (info->count != 1)) {
980 /*
981 * Uh, oh. tty->count is 1, which means that the tty
982 * structure will be freed. Info->count should always
983 * be one in these conditions. If it's greater than
984 * one, we've got real problems, since it means the
985 * serial port won't be shutdown.
986 */
987 printk(KERN_ERR "mxser_close: bad serial port count; "
988 "tty->count is 1, info->count is %d\n", info->count);
989 info->count = 1;
990 }
991 if (--info->count < 0) {
992 printk(KERN_ERR "mxser_close: bad serial port count for "
Jiri Slaby55b307d2006-12-08 02:38:14 -0800993 "ttys%d: %d\n", tty->index, info->count);
Jiri Slaby037ad482006-12-08 02:38:11 -0800994 info->count = 0;
995 }
996 if (info->count) {
997 spin_unlock_irqrestore(&info->slock, flags);
998 return;
999 }
1000 info->flags |= ASYNC_CLOSING;
1001 spin_unlock_irqrestore(&info->slock, flags);
1002 /*
1003 * Save the termios structure, since this port may have
1004 * separate termios for callout and dialin.
1005 */
1006 if (info->flags & ASYNC_NORMAL_ACTIVE)
1007 info->normal_termios = *tty->termios;
1008 /*
1009 * Now we wait for the transmit buffer to clear; and we notify
1010 * the line discipline to only process XON/XOFF characters.
1011 */
1012 tty->closing = 1;
1013 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1014 tty_wait_until_sent(tty, info->closing_wait);
1015 /*
1016 * At this point we stop accepting input. To do this, we
1017 * disable the receive line status interrupts, and tell the
1018 * interrupt driver to stop checking the data ready bit in the
1019 * line status register.
1020 */
1021 info->IER &= ~UART_IER_RLSI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001022 if (info->board->chip_flag)
Jiri Slaby037ad482006-12-08 02:38:11 -08001023 info->IER &= ~MOXA_MUST_RECV_ISR;
Jiri Slabye079f492006-12-08 02:38:31 -08001024
Jiri Slaby037ad482006-12-08 02:38:11 -08001025 if (info->flags & ASYNC_INITIALIZED) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08001026 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001027 /*
1028 * Before we drop DTR, make sure the UART transmitter
1029 * has completely drained; this is especially
1030 * important if there is a transmit FIFO!
1031 */
1032 timeout = jiffies + HZ;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001033 while (!(inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001034 schedule_timeout_interruptible(5);
1035 if (time_after(jiffies, timeout))
1036 break;
1037 }
1038 }
1039 mxser_shutdown(info);
1040
1041 if (tty->driver->flush_buffer)
1042 tty->driver->flush_buffer(tty);
1043
Jiri Slaby7e8bcf92006-12-08 02:38:24 -08001044 tty_ldisc_flush(tty);
Jiri Slaby037ad482006-12-08 02:38:11 -08001045
1046 tty->closing = 0;
1047 info->event = 0;
1048 info->tty = NULL;
1049 if (info->blocked_open) {
1050 if (info->close_delay)
1051 schedule_timeout_interruptible(info->close_delay);
1052 wake_up_interruptible(&info->open_wait);
1053 }
1054
1055 info->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
Jiri Slaby037ad482006-12-08 02:38:11 -08001056}
1057
1058static int mxser_write(struct tty_struct *tty, const unsigned char *buf, int count)
1059{
1060 int c, total = 0;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001061 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001062 unsigned long flags;
1063
1064 if (!info->xmit_buf)
1065 return 0;
1066
1067 while (1) {
1068 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1069 SERIAL_XMIT_SIZE - info->xmit_head));
1070 if (c <= 0)
1071 break;
1072
1073 memcpy(info->xmit_buf + info->xmit_head, buf, c);
1074 spin_lock_irqsave(&info->slock, flags);
1075 info->xmit_head = (info->xmit_head + c) &
1076 (SERIAL_XMIT_SIZE - 1);
1077 info->xmit_cnt += c;
1078 spin_unlock_irqrestore(&info->slock, flags);
1079
1080 buf += c;
1081 count -= c;
1082 total += c;
1083 }
1084
Jiri Slabye079f492006-12-08 02:38:31 -08001085 if (info->xmit_cnt && !tty->stopped) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001086 if (!tty->hw_stopped ||
1087 (info->type == PORT_16550A) ||
Jiri Slaby55b307d2006-12-08 02:38:14 -08001088 (info->board->chip_flag)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001089 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby55b307d2006-12-08 02:38:14 -08001090 outb(info->IER & ~UART_IER_THRI, info->ioaddr +
1091 UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001092 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001093 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001094 spin_unlock_irqrestore(&info->slock, flags);
1095 }
1096 }
1097 return total;
1098}
1099
1100static void mxser_put_char(struct tty_struct *tty, unsigned char ch)
1101{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001102 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001103 unsigned long flags;
1104
1105 if (!info->xmit_buf)
1106 return;
1107
1108 if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1)
1109 return;
1110
1111 spin_lock_irqsave(&info->slock, flags);
1112 info->xmit_buf[info->xmit_head++] = ch;
1113 info->xmit_head &= SERIAL_XMIT_SIZE - 1;
1114 info->xmit_cnt++;
1115 spin_unlock_irqrestore(&info->slock, flags);
Jiri Slabye079f492006-12-08 02:38:31 -08001116 if (!tty->stopped) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001117 if (!tty->hw_stopped ||
1118 (info->type == PORT_16550A) ||
Jiri Slaby55b307d2006-12-08 02:38:14 -08001119 info->board->chip_flag) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001120 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby55b307d2006-12-08 02:38:14 -08001121 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001122 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001123 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001124 spin_unlock_irqrestore(&info->slock, flags);
1125 }
1126 }
1127}
1128
1129
1130static void mxser_flush_chars(struct tty_struct *tty)
1131{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001132 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001133 unsigned long flags;
1134
1135 if (info->xmit_cnt <= 0 ||
1136 tty->stopped ||
1137 !info->xmit_buf ||
1138 (tty->hw_stopped &&
1139 (info->type != PORT_16550A) &&
Jiri Slaby55b307d2006-12-08 02:38:14 -08001140 (!info->board->chip_flag)
Jiri Slaby037ad482006-12-08 02:38:11 -08001141 ))
1142 return;
1143
1144 spin_lock_irqsave(&info->slock, flags);
1145
Jiri Slaby55b307d2006-12-08 02:38:14 -08001146 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001147 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001148 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001149
1150 spin_unlock_irqrestore(&info->slock, flags);
1151}
1152
1153static int mxser_write_room(struct tty_struct *tty)
1154{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001155 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001156 int ret;
1157
1158 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1159 if (ret < 0)
1160 ret = 0;
1161 return ret;
1162}
1163
1164static int mxser_chars_in_buffer(struct tty_struct *tty)
1165{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001166 struct mxser_port *info = tty->driver_data;
Jiri Slaby925e9c12006-12-08 02:38:30 -08001167 return info->xmit_cnt;
Jiri Slaby037ad482006-12-08 02:38:11 -08001168}
1169
1170static void mxser_flush_buffer(struct tty_struct *tty)
1171{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001172 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001173 char fcr;
1174 unsigned long flags;
1175
1176
1177 spin_lock_irqsave(&info->slock, flags);
1178 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1179
Jiri Slaby55b307d2006-12-08 02:38:14 -08001180 fcr = inb(info->ioaddr + UART_FCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08001181 outb((fcr | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
Jiri Slaby55b307d2006-12-08 02:38:14 -08001182 info->ioaddr + UART_FCR);
1183 outb(fcr, info->ioaddr + UART_FCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08001184
1185 spin_unlock_irqrestore(&info->slock, flags);
Jiri Slaby037ad482006-12-08 02:38:11 -08001186
Jiri Slaby7e8bcf92006-12-08 02:38:24 -08001187 tty_wakeup(tty);
Jiri Slaby037ad482006-12-08 02:38:11 -08001188}
1189
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001190/*
1191 * ------------------------------------------------------------
1192 * friends of mxser_ioctl()
1193 * ------------------------------------------------------------
1194 */
1195static int mxser_get_serial_info(struct mxser_port *info,
1196 struct serial_struct __user *retinfo)
1197{
1198 struct serial_struct tmp;
1199
1200 if (!retinfo)
1201 return -EFAULT;
1202 memset(&tmp, 0, sizeof(tmp));
1203 tmp.type = info->type;
1204 tmp.line = info->tty->index;
1205 tmp.port = info->ioaddr;
1206 tmp.irq = info->board->irq;
1207 tmp.flags = info->flags;
1208 tmp.baud_base = info->baud_base;
1209 tmp.close_delay = info->close_delay;
1210 tmp.closing_wait = info->closing_wait;
1211 tmp.custom_divisor = info->custom_divisor;
1212 tmp.hub6 = 0;
1213 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1214 return -EFAULT;
1215 return 0;
1216}
1217
1218static int mxser_set_serial_info(struct mxser_port *info,
1219 struct serial_struct __user *new_info)
1220{
1221 struct serial_struct new_serial;
Jiri Slabyf64c84a2007-02-10 01:45:21 -08001222 unsigned long sl_flags;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001223 unsigned int flags;
1224 int retval = 0;
1225
1226 if (!new_info || !info->ioaddr)
1227 return -EFAULT;
1228 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
1229 return -EFAULT;
1230
1231 if ((new_serial.irq != info->board->irq) ||
1232 (new_serial.port != info->ioaddr) ||
1233 (new_serial.custom_divisor != info->custom_divisor) ||
1234 (new_serial.baud_base != info->baud_base))
1235 return -EPERM;
1236
1237 flags = info->flags & ASYNC_SPD_MASK;
1238
1239 if (!capable(CAP_SYS_ADMIN)) {
1240 if ((new_serial.baud_base != info->baud_base) ||
1241 (new_serial.close_delay != info->close_delay) ||
1242 ((new_serial.flags & ~ASYNC_USR_MASK) != (info->flags & ~ASYNC_USR_MASK)))
1243 return -EPERM;
1244 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
1245 (new_serial.flags & ASYNC_USR_MASK));
1246 } else {
1247 /*
1248 * OK, past this point, all the error checking has been done.
1249 * At this point, we start making changes.....
1250 */
1251 info->flags = ((info->flags & ~ASYNC_FLAGS) |
1252 (new_serial.flags & ASYNC_FLAGS));
1253 info->close_delay = new_serial.close_delay * HZ / 100;
1254 info->closing_wait = new_serial.closing_wait * HZ / 100;
1255 info->tty->low_latency =
1256 (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
Jiri Slabye079f492006-12-08 02:38:31 -08001257 info->tty->low_latency = 0;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001258 }
1259
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001260 info->type = new_serial.type;
1261
1262 process_txrx_fifo(info);
1263
1264 if (info->flags & ASYNC_INITIALIZED) {
Jiri Slabyf64c84a2007-02-10 01:45:21 -08001265 if (flags != (info->flags & ASYNC_SPD_MASK)) {
1266 spin_lock_irqsave(&info->slock, sl_flags);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001267 mxser_change_speed(info, NULL);
Jiri Slabyf64c84a2007-02-10 01:45:21 -08001268 spin_unlock_irqrestore(&info->slock, sl_flags);
1269 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001270 } else
1271 retval = mxser_startup(info);
1272
1273 return retval;
1274}
1275
1276/*
1277 * mxser_get_lsr_info - get line status register info
1278 *
1279 * Purpose: Let user call ioctl() to get info when the UART physically
1280 * is emptied. On bus types like RS485, the transmitter must
1281 * release the bus after transmitting. This must be done when
1282 * the transmit shift register is empty, not be done when the
1283 * transmit holding register is empty. This functionality
1284 * allows an RS485 driver to be written in user space.
1285 */
1286static int mxser_get_lsr_info(struct mxser_port *info,
1287 unsigned int __user *value)
1288{
1289 unsigned char status;
1290 unsigned int result;
1291 unsigned long flags;
1292
1293 spin_lock_irqsave(&info->slock, flags);
1294 status = inb(info->ioaddr + UART_LSR);
1295 spin_unlock_irqrestore(&info->slock, flags);
1296 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1297 return put_user(result, value);
1298}
1299
1300/*
1301 * This routine sends a break character out the serial port.
1302 */
1303static void mxser_send_break(struct mxser_port *info, int duration)
1304{
1305 unsigned long flags;
1306
1307 if (!info->ioaddr)
1308 return;
1309 set_current_state(TASK_INTERRUPTIBLE);
1310 spin_lock_irqsave(&info->slock, flags);
1311 outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
1312 info->ioaddr + UART_LCR);
1313 spin_unlock_irqrestore(&info->slock, flags);
1314 schedule_timeout(duration);
1315 spin_lock_irqsave(&info->slock, flags);
1316 outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
1317 info->ioaddr + UART_LCR);
1318 spin_unlock_irqrestore(&info->slock, flags);
1319}
1320
1321static int mxser_tiocmget(struct tty_struct *tty, struct file *file)
Jiri Slaby037ad482006-12-08 02:38:11 -08001322{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001323 struct mxser_port *info = tty->driver_data;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001324 unsigned char control, status;
Jiri Slaby037ad482006-12-08 02:38:11 -08001325 unsigned long flags;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001326
Jiri Slaby037ad482006-12-08 02:38:11 -08001327
1328 if (tty->index == MXSER_PORTS)
Jiri Slaby037ad482006-12-08 02:38:11 -08001329 return -ENOIOCTLCMD;
Jiri Slaby214efeb2006-12-08 02:38:25 -08001330 if (test_bit(TTY_IO_ERROR, &tty->flags))
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001331 return -EIO;
1332
1333 control = info->MCR;
1334
1335 spin_lock_irqsave(&info->slock, flags);
1336 status = inb(info->ioaddr + UART_MSR);
1337 if (status & UART_MSR_ANY_DELTA)
1338 mxser_check_modem_status(info, status);
1339 spin_unlock_irqrestore(&info->slock, flags);
1340 return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0) |
1341 ((control & UART_MCR_DTR) ? TIOCM_DTR : 0) |
1342 ((status & UART_MSR_DCD) ? TIOCM_CAR : 0) |
1343 ((status & UART_MSR_RI) ? TIOCM_RNG : 0) |
1344 ((status & UART_MSR_DSR) ? TIOCM_DSR : 0) |
1345 ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
1346}
1347
1348static int mxser_tiocmset(struct tty_struct *tty, struct file *file,
1349 unsigned int set, unsigned int clear)
1350{
1351 struct mxser_port *info = tty->driver_data;
1352 unsigned long flags;
1353
1354
1355 if (tty->index == MXSER_PORTS)
1356 return -ENOIOCTLCMD;
Jiri Slaby214efeb2006-12-08 02:38:25 -08001357 if (test_bit(TTY_IO_ERROR, &tty->flags))
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001358 return -EIO;
1359
1360 spin_lock_irqsave(&info->slock, flags);
1361
1362 if (set & TIOCM_RTS)
1363 info->MCR |= UART_MCR_RTS;
1364 if (set & TIOCM_DTR)
1365 info->MCR |= UART_MCR_DTR;
1366
1367 if (clear & TIOCM_RTS)
1368 info->MCR &= ~UART_MCR_RTS;
1369 if (clear & TIOCM_DTR)
1370 info->MCR &= ~UART_MCR_DTR;
1371
1372 outb(info->MCR, info->ioaddr + UART_MCR);
1373 spin_unlock_irqrestore(&info->slock, flags);
Jiri Slaby037ad482006-12-08 02:38:11 -08001374 return 0;
1375}
1376
Jiri Slabyc76b0932007-02-10 01:45:07 -08001377static int __init mxser_program_mode(int port)
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001378{
1379 int id, i, j, n;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001380
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001381 outb(0, port);
1382 outb(0, port);
1383 outb(0, port);
1384 (void)inb(port);
1385 (void)inb(port);
1386 outb(0, port);
1387 (void)inb(port);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001388
1389 id = inb(port + 1) & 0x1F;
1390 if ((id != C168_ASIC_ID) &&
1391 (id != C104_ASIC_ID) &&
1392 (id != C102_ASIC_ID) &&
1393 (id != CI132_ASIC_ID) &&
1394 (id != CI134_ASIC_ID) &&
1395 (id != CI104J_ASIC_ID))
1396 return -1;
1397 for (i = 0, j = 0; i < 4; i++) {
1398 n = inb(port + 2);
1399 if (n == 'M') {
1400 j = 1;
1401 } else if ((j == 1) && (n == 1)) {
1402 j = 2;
1403 break;
1404 } else
1405 j = 0;
1406 }
1407 if (j != 2)
1408 id = -2;
1409 return id;
1410}
1411
Jiri Slabyc76b0932007-02-10 01:45:07 -08001412static void __init mxser_normal_mode(int port)
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001413{
1414 int i, n;
1415
1416 outb(0xA5, port + 1);
1417 outb(0x80, port + 3);
1418 outb(12, port + 0); /* 9600 bps */
1419 outb(0, port + 1);
1420 outb(0x03, port + 3); /* 8 data bits */
1421 outb(0x13, port + 4); /* loop back mode */
1422 for (i = 0; i < 16; i++) {
1423 n = inb(port + 5);
1424 if ((n & 0x61) == 0x60)
1425 break;
1426 if ((n & 1) == 1)
1427 (void)inb(port);
1428 }
1429 outb(0x00, port + 4);
1430}
1431
1432#define CHIP_SK 0x01 /* Serial Data Clock in Eprom */
1433#define CHIP_DO 0x02 /* Serial Data Output in Eprom */
1434#define CHIP_CS 0x04 /* Serial Chip Select in Eprom */
1435#define CHIP_DI 0x08 /* Serial Data Input in Eprom */
1436#define EN_CCMD 0x000 /* Chip's command register */
1437#define EN0_RSARLO 0x008 /* Remote start address reg 0 */
1438#define EN0_RSARHI 0x009 /* Remote start address reg 1 */
1439#define EN0_RCNTLO 0x00A /* Remote byte count reg WR */
1440#define EN0_RCNTHI 0x00B /* Remote byte count reg WR */
1441#define EN0_DCFG 0x00E /* Data configuration reg WR */
1442#define EN0_PORT 0x010 /* Rcv missed frame error counter RD */
1443#define ENC_PAGE0 0x000 /* Select page 0 of chip registers */
1444#define ENC_PAGE3 0x0C0 /* Select page 3 of chip registers */
Jiri Slabyc76b0932007-02-10 01:45:07 -08001445static int __init mxser_read_register(int port, unsigned short *regs)
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001446{
1447 int i, k, value, id;
1448 unsigned int j;
1449
1450 id = mxser_program_mode(port);
1451 if (id < 0)
1452 return id;
1453 for (i = 0; i < 14; i++) {
1454 k = (i & 0x3F) | 0x180;
1455 for (j = 0x100; j > 0; j >>= 1) {
1456 outb(CHIP_CS, port);
1457 if (k & j) {
1458 outb(CHIP_CS | CHIP_DO, port);
1459 outb(CHIP_CS | CHIP_DO | CHIP_SK, port); /* A? bit of read */
1460 } else {
1461 outb(CHIP_CS, port);
1462 outb(CHIP_CS | CHIP_SK, port); /* A? bit of read */
1463 }
1464 }
1465 (void)inb(port);
1466 value = 0;
1467 for (k = 0, j = 0x8000; k < 16; k++, j >>= 1) {
1468 outb(CHIP_CS, port);
1469 outb(CHIP_CS | CHIP_SK, port);
1470 if (inb(port) & CHIP_DI)
1471 value |= j;
1472 }
1473 regs[i] = value;
1474 outb(0, port);
1475 }
1476 mxser_normal_mode(port);
1477 return id;
1478}
1479
Jiri Slaby037ad482006-12-08 02:38:11 -08001480static int mxser_ioctl_special(unsigned int cmd, void __user *argp)
1481{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001482 struct mxser_port *port;
1483 int result, status;
1484 unsigned int i, j;
Jiri Slaby037ad482006-12-08 02:38:11 -08001485
1486 switch (cmd) {
1487 case MOXA_GET_CONF:
Jiri Slaby55b307d2006-12-08 02:38:14 -08001488/* if (copy_to_user(argp, mxsercfg,
Jiri Slaby037ad482006-12-08 02:38:11 -08001489 sizeof(struct mxser_hwconf) * 4))
1490 return -EFAULT;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001491 return 0;*/
1492 return -ENXIO;
Jiri Slaby037ad482006-12-08 02:38:11 -08001493 case MOXA_GET_MAJOR:
1494 if (copy_to_user(argp, &ttymajor, sizeof(int)))
1495 return -EFAULT;
1496 return 0;
1497
1498 case MOXA_GET_CUMAJOR:
1499 if (copy_to_user(argp, &calloutmajor, sizeof(int)))
1500 return -EFAULT;
1501 return 0;
1502
1503 case MOXA_CHKPORTENABLE:
1504 result = 0;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001505
1506 for (i = 0; i < MXSER_BOARDS; i++)
1507 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++)
1508 if (mxser_boards[i].ports[j].ioaddr)
1509 result |= (1 << i);
1510
Jiri Slaby037ad482006-12-08 02:38:11 -08001511 return put_user(result, (unsigned long __user *)argp);
1512 case MOXA_GETDATACOUNT:
1513 if (copy_to_user(argp, &mxvar_log, sizeof(mxvar_log)))
1514 return -EFAULT;
1515 return 0;
1516 case MOXA_GETMSTATUS:
Jiri Slaby55b307d2006-12-08 02:38:14 -08001517 for (i = 0; i < MXSER_BOARDS; i++)
1518 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
1519 port = &mxser_boards[i].ports[j];
1520
1521 GMStatus[i].ri = 0;
1522 if (!port->ioaddr) {
1523 GMStatus[i].dcd = 0;
1524 GMStatus[i].dsr = 0;
1525 GMStatus[i].cts = 0;
1526 continue;
1527 }
1528
1529 if (!port->tty || !port->tty->termios)
1530 GMStatus[i].cflag =
1531 port->normal_termios.c_cflag;
1532 else
1533 GMStatus[i].cflag =
1534 port->tty->termios->c_cflag;
1535
1536 status = inb(port->ioaddr + UART_MSR);
1537 if (status & 0x80 /*UART_MSR_DCD */ )
1538 GMStatus[i].dcd = 1;
1539 else
1540 GMStatus[i].dcd = 0;
1541
1542 if (status & 0x20 /*UART_MSR_DSR */ )
1543 GMStatus[i].dsr = 1;
1544 else
1545 GMStatus[i].dsr = 0;
1546
1547
1548 if (status & 0x10 /*UART_MSR_CTS */ )
1549 GMStatus[i].cts = 1;
1550 else
1551 GMStatus[i].cts = 0;
Jiri Slaby037ad482006-12-08 02:38:11 -08001552 }
Jiri Slaby037ad482006-12-08 02:38:11 -08001553 if (copy_to_user(argp, GMStatus,
1554 sizeof(struct mxser_mstatus) * MXSER_PORTS))
1555 return -EFAULT;
1556 return 0;
1557 case MOXA_ASPP_MON_EXT: {
Jiri Slaby2b770b42007-07-15 23:40:17 -07001558 int p, shiftbit;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001559 unsigned long opmode;
1560 unsigned cflag, iflag;
Jiri Slaby037ad482006-12-08 02:38:11 -08001561
Jiri Slaby55b307d2006-12-08 02:38:14 -08001562 for (i = 0; i < MXSER_BOARDS; i++)
1563 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
1564 port = &mxser_boards[i].ports[j];
1565 if (!port->ioaddr)
Jiri Slaby037ad482006-12-08 02:38:11 -08001566 continue;
1567
Jiri Slaby55b307d2006-12-08 02:38:14 -08001568 status = mxser_get_msr(port->ioaddr, 0, i);
Jiri Slaby55b307d2006-12-08 02:38:14 -08001569
Jiri Slaby037ad482006-12-08 02:38:11 -08001570 if (status & UART_MSR_TERI)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001571 port->icount.rng++;
Jiri Slaby037ad482006-12-08 02:38:11 -08001572 if (status & UART_MSR_DDSR)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001573 port->icount.dsr++;
Jiri Slaby037ad482006-12-08 02:38:11 -08001574 if (status & UART_MSR_DDCD)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001575 port->icount.dcd++;
Jiri Slaby037ad482006-12-08 02:38:11 -08001576 if (status & UART_MSR_DCTS)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001577 port->icount.cts++;
Jiri Slaby037ad482006-12-08 02:38:11 -08001578
Jiri Slaby55b307d2006-12-08 02:38:14 -08001579 port->mon_data.modem_status = status;
1580 mon_data_ext.rx_cnt[i] = port->mon_data.rxcnt;
1581 mon_data_ext.tx_cnt[i] = port->mon_data.txcnt;
1582 mon_data_ext.up_rxcnt[i] =
1583 port->mon_data.up_rxcnt;
1584 mon_data_ext.up_txcnt[i] =
1585 port->mon_data.up_txcnt;
1586 mon_data_ext.modem_status[i] =
1587 port->mon_data.modem_status;
1588 mon_data_ext.baudrate[i] = port->realbaud;
Jiri Slaby037ad482006-12-08 02:38:11 -08001589
Jiri Slaby55b307d2006-12-08 02:38:14 -08001590 if (!port->tty || !port->tty->termios) {
1591 cflag = port->normal_termios.c_cflag;
1592 iflag = port->normal_termios.c_iflag;
Jiri Slaby037ad482006-12-08 02:38:11 -08001593 } else {
Jiri Slaby55b307d2006-12-08 02:38:14 -08001594 cflag = port->tty->termios->c_cflag;
1595 iflag = port->tty->termios->c_iflag;
Jiri Slaby037ad482006-12-08 02:38:11 -08001596 }
1597
1598 mon_data_ext.databits[i] = cflag & CSIZE;
1599
1600 mon_data_ext.stopbits[i] = cflag & CSTOPB;
1601
Jiri Slaby55b307d2006-12-08 02:38:14 -08001602 mon_data_ext.parity[i] =
1603 cflag & (PARENB | PARODD | CMSPAR);
Jiri Slaby037ad482006-12-08 02:38:11 -08001604
1605 mon_data_ext.flowctrl[i] = 0x00;
1606
1607 if (cflag & CRTSCTS)
1608 mon_data_ext.flowctrl[i] |= 0x03;
1609
1610 if (iflag & (IXON | IXOFF))
1611 mon_data_ext.flowctrl[i] |= 0x0C;
1612
Jiri Slaby55b307d2006-12-08 02:38:14 -08001613 if (port->type == PORT_16550A)
Jiri Slaby037ad482006-12-08 02:38:11 -08001614 mon_data_ext.fifo[i] = 1;
1615 else
1616 mon_data_ext.fifo[i] = 0;
1617
1618 p = i % 4;
1619 shiftbit = p * 2;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001620 opmode = inb(port->opmode_ioaddr) >> shiftbit;
Jiri Slaby037ad482006-12-08 02:38:11 -08001621 opmode &= OP_MODE_MASK;
1622
1623 mon_data_ext.iftype[i] = opmode;
1624
1625 }
Jiri Slaby55b307d2006-12-08 02:38:14 -08001626 if (copy_to_user(argp, &mon_data_ext,
1627 sizeof(mon_data_ext)))
Jiri Slaby037ad482006-12-08 02:38:11 -08001628 return -EFAULT;
1629
1630 return 0;
1631
Jiri Slaby55b307d2006-12-08 02:38:14 -08001632 } default:
Jiri Slaby037ad482006-12-08 02:38:11 -08001633 return -ENOIOCTLCMD;
1634 }
1635 return 0;
1636}
1637
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001638static int mxser_ioctl(struct tty_struct *tty, struct file *file,
1639 unsigned int cmd, unsigned long arg)
1640{
1641 struct mxser_port *info = tty->driver_data;
1642 struct async_icount cprev, cnow; /* kernel counter temps */
1643 struct serial_icounter_struct __user *p_cuser;
1644 unsigned long templ;
1645 unsigned long flags;
Jiri Slabyf64c84a2007-02-10 01:45:21 -08001646 unsigned int i;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001647 void __user *argp = (void __user *)arg;
1648 int retval;
1649
1650 if (tty->index == MXSER_PORTS)
1651 return mxser_ioctl_special(cmd, argp);
1652
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001653 if (cmd == MOXA_SET_OP_MODE || cmd == MOXA_GET_OP_MODE) {
1654 int p;
1655 unsigned long opmode;
1656 static unsigned char ModeMask[] = { 0xfc, 0xf3, 0xcf, 0x3f };
1657 int shiftbit;
1658 unsigned char val, mask;
1659
1660 p = tty->index % 4;
1661 if (cmd == MOXA_SET_OP_MODE) {
1662 if (get_user(opmode, (int __user *) argp))
1663 return -EFAULT;
1664 if (opmode != RS232_MODE &&
1665 opmode != RS485_2WIRE_MODE &&
1666 opmode != RS422_MODE &&
1667 opmode != RS485_4WIRE_MODE)
1668 return -EFAULT;
1669 mask = ModeMask[p];
1670 shiftbit = p * 2;
1671 val = inb(info->opmode_ioaddr);
1672 val &= mask;
1673 val |= (opmode << shiftbit);
1674 outb(val, info->opmode_ioaddr);
1675 } else {
1676 shiftbit = p * 2;
1677 opmode = inb(info->opmode_ioaddr) >> shiftbit;
1678 opmode &= OP_MODE_MASK;
1679 if (copy_to_user(argp, &opmode, sizeof(int)))
1680 return -EFAULT;
1681 }
1682 return 0;
1683 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001684
Jiri Slabyf64c84a2007-02-10 01:45:21 -08001685 if (cmd == MOXA_SET_SPECIAL_BAUD_RATE) {
1686 int speed;
1687
1688 if (get_user(speed, (int __user *)argp))
1689 return -EFAULT;
1690 if (speed <= 0 || speed > info->max_baud)
1691 return -EFAULT;
1692 if (!info->tty || !info->tty->termios || !info->ioaddr)
1693 return 0;
1694 info->tty->termios->c_cflag &= ~(CBAUD | CBAUDEX);
1695 for (i = 0; i < BAUD_TABLE_NO; i++)
1696 if (speed == mxvar_baud_table[i])
1697 break;
1698 if (i == BAUD_TABLE_NO) {
1699 info->tty->termios->c_cflag |= B_SPEC;
1700 } else if (speed != 0)
1701 info->tty->termios->c_cflag |= mxvar_baud_table1[i];
1702
1703 info->speed = speed;
1704 spin_lock_irqsave(&info->slock, flags);
Jiri Slabya136e992007-02-10 01:45:24 -08001705 mxser_change_speed(info, NULL);
Jiri Slabyf64c84a2007-02-10 01:45:21 -08001706 spin_unlock_irqrestore(&info->slock, flags);
1707
1708 return 0;
1709 } else if (cmd == MOXA_GET_SPECIAL_BAUD_RATE) {
1710 if (copy_to_user(argp, &info->speed, sizeof(int)))
1711 return -EFAULT;
1712 return 0;
1713 }
1714
Jiri Slaby214efeb2006-12-08 02:38:25 -08001715 if (cmd != TIOCGSERIAL && cmd != TIOCMIWAIT && cmd != TIOCGICOUNT &&
1716 test_bit(TTY_IO_ERROR, &tty->flags))
1717 return -EIO;
1718
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001719 switch (cmd) {
1720 case TCSBRK: /* SVID version: non-zero arg --> no break */
1721 retval = tty_check_change(tty);
1722 if (retval)
1723 return retval;
1724 tty_wait_until_sent(tty, 0);
1725 if (!arg)
1726 mxser_send_break(info, HZ / 4); /* 1/4 second */
1727 return 0;
1728 case TCSBRKP: /* support for POSIX tcsendbreak() */
1729 retval = tty_check_change(tty);
1730 if (retval)
1731 return retval;
1732 tty_wait_until_sent(tty, 0);
1733 mxser_send_break(info, arg ? arg * (HZ / 10) : HZ / 4);
1734 return 0;
1735 case TIOCGSOFTCAR:
1736 return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *)argp);
1737 case TIOCSSOFTCAR:
1738 if (get_user(templ, (unsigned long __user *) argp))
1739 return -EFAULT;
1740 arg = templ;
1741 tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) | (arg ? CLOCAL : 0));
1742 return 0;
1743 case TIOCGSERIAL:
1744 return mxser_get_serial_info(info, argp);
1745 case TIOCSSERIAL:
1746 return mxser_set_serial_info(info, argp);
1747 case TIOCSERGETLSR: /* Get line status register */
1748 return mxser_get_lsr_info(info, argp);
1749 /*
1750 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1751 * - mask passed in arg for lines of interest
1752 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1753 * Caller should use TIOCGICOUNT to see which one it was
1754 */
Jiri Slabyb446a4a2007-04-23 14:41:03 -07001755 case TIOCMIWAIT:
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001756 spin_lock_irqsave(&info->slock, flags);
Jiri Slabyb446a4a2007-04-23 14:41:03 -07001757 cnow = info->icount; /* note the counters on entry */
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001758 spin_unlock_irqrestore(&info->slock, flags);
1759
Jiri Slabyb446a4a2007-04-23 14:41:03 -07001760 wait_event_interruptible(info->delta_msr_wait, ({
1761 cprev = cnow;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001762 spin_lock_irqsave(&info->slock, flags);
1763 cnow = info->icount; /* atomic copy */
1764 spin_unlock_irqrestore(&info->slock, flags);
1765
Jiri Slabyb446a4a2007-04-23 14:41:03 -07001766 ((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1767 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1768 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1769 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts));
1770 }));
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001771 break;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001772 /*
1773 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1774 * Return: write counters to the user passed counter struct
1775 * NB: both 1->0 and 0->1 transitions are counted except for
1776 * RI where only 0->1 is counted.
1777 */
1778 case TIOCGICOUNT:
1779 spin_lock_irqsave(&info->slock, flags);
1780 cnow = info->icount;
1781 spin_unlock_irqrestore(&info->slock, flags);
1782 p_cuser = argp;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001783 if (put_user(cnow.frame, &p_cuser->frame))
1784 return -EFAULT;
1785 if (put_user(cnow.brk, &p_cuser->brk))
1786 return -EFAULT;
1787 if (put_user(cnow.overrun, &p_cuser->overrun))
1788 return -EFAULT;
1789 if (put_user(cnow.buf_overrun, &p_cuser->buf_overrun))
1790 return -EFAULT;
1791 if (put_user(cnow.parity, &p_cuser->parity))
1792 return -EFAULT;
1793 if (put_user(cnow.rx, &p_cuser->rx))
1794 return -EFAULT;
1795 if (put_user(cnow.tx, &p_cuser->tx))
1796 return -EFAULT;
1797 put_user(cnow.cts, &p_cuser->cts);
1798 put_user(cnow.dsr, &p_cuser->dsr);
1799 put_user(cnow.rng, &p_cuser->rng);
1800 put_user(cnow.dcd, &p_cuser->dcd);
1801 return 0;
1802 case MOXA_HighSpeedOn:
1803 return put_user(info->baud_base != 115200 ? 1 : 0, (int __user *)argp);
1804 case MOXA_SDS_RSTICOUNTER:
1805 info->mon_data.rxcnt = 0;
1806 info->mon_data.txcnt = 0;
1807 return 0;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001808 case MOXA_ASPP_SETBAUD:{
1809 long baud;
1810 if (get_user(baud, (long __user *)argp))
1811 return -EFAULT;
Jiri Slabyf64c84a2007-02-10 01:45:21 -08001812 spin_lock_irqsave(&info->slock, flags);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001813 mxser_set_baud(info, baud);
Jiri Slabyf64c84a2007-02-10 01:45:21 -08001814 spin_unlock_irqrestore(&info->slock, flags);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001815 return 0;
1816 }
1817 case MOXA_ASPP_GETBAUD:
1818 if (copy_to_user(argp, &info->realbaud, sizeof(long)))
1819 return -EFAULT;
1820
1821 return 0;
1822
1823 case MOXA_ASPP_OQUEUE:{
1824 int len, lsr;
1825
1826 len = mxser_chars_in_buffer(tty);
1827
1828 lsr = inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT;
1829
1830 len += (lsr ? 0 : 1);
1831
1832 if (copy_to_user(argp, &len, sizeof(int)))
1833 return -EFAULT;
1834
1835 return 0;
1836 }
1837 case MOXA_ASPP_MON: {
1838 int mcr, status;
1839
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001840 status = mxser_get_msr(info->ioaddr, 1, tty->index);
1841 mxser_check_modem_status(info, status);
1842
1843 mcr = inb(info->ioaddr + UART_MCR);
1844 if (mcr & MOXA_MUST_MCR_XON_FLAG)
1845 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFHOLD;
1846 else
1847 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFHOLD;
1848
1849 if (mcr & MOXA_MUST_MCR_TX_XON)
1850 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFXENT;
1851 else
1852 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFXENT;
1853
1854 if (info->tty->hw_stopped)
1855 info->mon_data.hold_reason |= NPPI_NOTIFY_CTSHOLD;
1856 else
1857 info->mon_data.hold_reason &= ~NPPI_NOTIFY_CTSHOLD;
1858
1859 if (copy_to_user(argp, &info->mon_data,
1860 sizeof(struct mxser_mon)))
1861 return -EFAULT;
1862
1863 return 0;
1864 }
1865 case MOXA_ASPP_LSTATUS: {
1866 if (copy_to_user(argp, &info->err_shadow,
1867 sizeof(unsigned char)))
1868 return -EFAULT;
1869
1870 info->err_shadow = 0;
1871 return 0;
1872 }
1873 case MOXA_SET_BAUD_METHOD: {
1874 int method;
1875
1876 if (get_user(method, (int __user *)argp))
1877 return -EFAULT;
1878 mxser_set_baud_method[tty->index] = method;
1879 if (copy_to_user(argp, &method, sizeof(int)))
1880 return -EFAULT;
1881
1882 return 0;
1883 }
1884 default:
1885 return -ENOIOCTLCMD;
1886 }
1887 return 0;
1888}
1889
Jiri Slaby037ad482006-12-08 02:38:11 -08001890static void mxser_stoprx(struct tty_struct *tty)
1891{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001892 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001893
1894 info->ldisc_stop_rx = 1;
1895 if (I_IXOFF(tty)) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08001896 if (info->board->chip_flag) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001897 info->IER &= ~MOXA_MUST_RECV_ISR;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001898 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby925e9c12006-12-08 02:38:30 -08001899 } else {
Jiri Slaby037ad482006-12-08 02:38:11 -08001900 info->x_char = STOP_CHAR(tty);
Jiri Slaby55b307d2006-12-08 02:38:14 -08001901 outb(0, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001902 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001903 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001904 }
Jiri Slaby037ad482006-12-08 02:38:11 -08001905 }
1906
1907 if (info->tty->termios->c_cflag & CRTSCTS) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001908 info->MCR &= ~UART_MCR_RTS;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001909 outb(info->MCR, info->ioaddr + UART_MCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08001910 }
1911}
1912
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001913/*
1914 * This routine is called by the upper-layer tty layer to signal that
1915 * incoming characters should be throttled.
1916 */
1917static void mxser_throttle(struct tty_struct *tty)
1918{
1919 mxser_stoprx(tty);
1920}
1921
1922static void mxser_unthrottle(struct tty_struct *tty)
Jiri Slaby037ad482006-12-08 02:38:11 -08001923{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001924 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001925
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001926 /* startrx */
Jiri Slaby037ad482006-12-08 02:38:11 -08001927 info->ldisc_stop_rx = 0;
1928 if (I_IXOFF(tty)) {
1929 if (info->x_char)
1930 info->x_char = 0;
1931 else {
Jiri Slaby55b307d2006-12-08 02:38:14 -08001932 if (info->board->chip_flag) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001933 info->IER |= MOXA_MUST_RECV_ISR;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001934 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby925e9c12006-12-08 02:38:30 -08001935 } else {
Jiri Slaby037ad482006-12-08 02:38:11 -08001936 info->x_char = START_CHAR(tty);
Jiri Slaby55b307d2006-12-08 02:38:14 -08001937 outb(0, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001938 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001939 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001940 }
Jiri Slaby037ad482006-12-08 02:38:11 -08001941 }
1942 }
1943
1944 if (info->tty->termios->c_cflag & CRTSCTS) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001945 info->MCR |= UART_MCR_RTS;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001946 outb(info->MCR, info->ioaddr + UART_MCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08001947 }
1948}
1949
1950/*
Jiri Slaby037ad482006-12-08 02:38:11 -08001951 * mxser_stop() and mxser_start()
1952 *
1953 * This routines are called before setting or resetting tty->stopped.
1954 * They enable or disable transmitter interrupts, as necessary.
1955 */
1956static void mxser_stop(struct tty_struct *tty)
1957{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001958 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001959 unsigned long flags;
1960
1961 spin_lock_irqsave(&info->slock, flags);
1962 if (info->IER & UART_IER_THRI) {
1963 info->IER &= ~UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001964 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001965 }
1966 spin_unlock_irqrestore(&info->slock, flags);
1967}
1968
1969static void mxser_start(struct tty_struct *tty)
1970{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001971 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001972 unsigned long flags;
1973
1974 spin_lock_irqsave(&info->slock, flags);
Jiri Slabye079f492006-12-08 02:38:31 -08001975 if (info->xmit_cnt && info->xmit_buf) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08001976 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001977 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001978 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001979 }
1980 spin_unlock_irqrestore(&info->slock, flags);
1981}
1982
Alan Cox606d0992006-12-08 02:38:45 -08001983static void mxser_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001984{
1985 struct mxser_port *info = tty->driver_data;
1986 unsigned long flags;
1987
Alan Cox7e7d1362007-07-15 23:41:49 -07001988 spin_lock_irqsave(&info->slock, flags);
1989 mxser_change_speed(info, old_termios);
1990 spin_unlock_irqrestore(&info->slock, flags);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001991
Alan Cox7e7d1362007-07-15 23:41:49 -07001992 if ((old_termios->c_cflag & CRTSCTS) &&
1993 !(tty->termios->c_cflag & CRTSCTS)) {
1994 tty->hw_stopped = 0;
1995 mxser_start(tty);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001996 }
1997
Jiri Slabye079f492006-12-08 02:38:31 -08001998 /* Handle sw stopped */
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001999 if ((old_termios->c_iflag & IXON) &&
2000 !(tty->termios->c_iflag & IXON)) {
2001 tty->stopped = 0;
2002
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002003 if (info->board->chip_flag) {
2004 spin_lock_irqsave(&info->slock, flags);
2005 DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
2006 spin_unlock_irqrestore(&info->slock, flags);
2007 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002008
2009 mxser_start(tty);
2010 }
2011}
2012
Jiri Slaby037ad482006-12-08 02:38:11 -08002013/*
2014 * mxser_wait_until_sent() --- wait until the transmitter is empty
2015 */
2016static void mxser_wait_until_sent(struct tty_struct *tty, int timeout)
2017{
Jiri Slaby55b307d2006-12-08 02:38:14 -08002018 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08002019 unsigned long orig_jiffies, char_time;
2020 int lsr;
2021
2022 if (info->type == PORT_UNKNOWN)
2023 return;
2024
2025 if (info->xmit_fifo_size == 0)
2026 return; /* Just in case.... */
2027
2028 orig_jiffies = jiffies;
2029 /*
2030 * Set the check interval to be 1/5 of the estimated time to
2031 * send a single character, and make it at least 1. The check
2032 * interval should also be less than the timeout.
2033 *
2034 * Note: we have to use pretty tight timings here to satisfy
2035 * the NIST-PCTS.
2036 */
2037 char_time = (info->timeout - HZ / 50) / info->xmit_fifo_size;
2038 char_time = char_time / 5;
2039 if (char_time == 0)
2040 char_time = 1;
2041 if (timeout && timeout < char_time)
2042 char_time = timeout;
2043 /*
2044 * If the transmitter hasn't cleared in twice the approximate
2045 * amount of time to send the entire FIFO, it probably won't
2046 * ever clear. This assumes the UART isn't doing flow
2047 * control, which is currently the case. Hence, if it ever
2048 * takes longer than info->timeout, this is probably due to a
2049 * UART bug of some kind. So, we clamp the timeout parameter at
2050 * 2*info->timeout.
2051 */
2052 if (!timeout || timeout > 2 * info->timeout)
2053 timeout = 2 * info->timeout;
2054#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2055 printk(KERN_DEBUG "In rs_wait_until_sent(%d) check=%lu...",
2056 timeout, char_time);
2057 printk("jiff=%lu...", jiffies);
2058#endif
Jiri Slaby55b307d2006-12-08 02:38:14 -08002059 while (!((lsr = inb(info->ioaddr + UART_LSR)) & UART_LSR_TEMT)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002060#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2061 printk("lsr = %d (jiff=%lu)...", lsr, jiffies);
2062#endif
2063 schedule_timeout_interruptible(char_time);
2064 if (signal_pending(current))
2065 break;
2066 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2067 break;
2068 }
2069 set_current_state(TASK_RUNNING);
2070
2071#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2072 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
2073#endif
2074}
2075
Jiri Slaby037ad482006-12-08 02:38:11 -08002076/*
2077 * This routine is called by tty_hangup() when a hangup is signaled.
2078 */
Adrian Bunk02d3fca2007-02-10 01:44:51 -08002079static void mxser_hangup(struct tty_struct *tty)
Jiri Slaby037ad482006-12-08 02:38:11 -08002080{
Jiri Slaby55b307d2006-12-08 02:38:14 -08002081 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08002082
2083 mxser_flush_buffer(tty);
2084 mxser_shutdown(info);
2085 info->event = 0;
2086 info->count = 0;
2087 info->flags &= ~ASYNC_NORMAL_ACTIVE;
2088 info->tty = NULL;
2089 wake_up_interruptible(&info->open_wait);
2090}
2091
Jiri Slaby037ad482006-12-08 02:38:11 -08002092/*
2093 * mxser_rs_break() --- routine which turns the break handling on or off
2094 */
2095static void mxser_rs_break(struct tty_struct *tty, int break_state)
2096{
Jiri Slaby55b307d2006-12-08 02:38:14 -08002097 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08002098 unsigned long flags;
2099
2100 spin_lock_irqsave(&info->slock, flags);
2101 if (break_state == -1)
Jiri Slaby55b307d2006-12-08 02:38:14 -08002102 outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
2103 info->ioaddr + UART_LCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002104 else
Jiri Slaby55b307d2006-12-08 02:38:14 -08002105 outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
2106 info->ioaddr + UART_LCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002107 spin_unlock_irqrestore(&info->slock, flags);
2108}
2109
Jiri Slaby55b307d2006-12-08 02:38:14 -08002110static void mxser_receive_chars(struct mxser_port *port, int *status)
Jiri Slaby037ad482006-12-08 02:38:11 -08002111{
Jiri Slaby55b307d2006-12-08 02:38:14 -08002112 struct tty_struct *tty = port->tty;
Jiri Slaby037ad482006-12-08 02:38:11 -08002113 unsigned char ch, gdl;
2114 int ignored = 0;
2115 int cnt = 0;
2116 int recv_room;
2117 int max = 256;
Jiri Slaby037ad482006-12-08 02:38:11 -08002118
2119 recv_room = tty->receive_room;
Jiri Slabye079f492006-12-08 02:38:31 -08002120 if ((recv_room == 0) && (!port->ldisc_stop_rx))
Jiri Slaby037ad482006-12-08 02:38:11 -08002121 mxser_stoprx(tty);
Jiri Slaby037ad482006-12-08 02:38:11 -08002122
Jiri Slaby55b307d2006-12-08 02:38:14 -08002123 if (port->board->chip_flag != MOXA_OTHER_UART) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002124
Jiri Slaby3306ce32006-12-08 02:38:13 -08002125 if (*status & UART_LSR_SPECIAL)
Jiri Slaby037ad482006-12-08 02:38:11 -08002126 goto intr_old;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002127 if (port->board->chip_flag == MOXA_MUST_MU860_HWID &&
Jiri Slaby037ad482006-12-08 02:38:11 -08002128 (*status & MOXA_MUST_LSR_RERR))
2129 goto intr_old;
Jiri Slaby037ad482006-12-08 02:38:11 -08002130 if (*status & MOXA_MUST_LSR_RERR)
2131 goto intr_old;
2132
Jiri Slaby55b307d2006-12-08 02:38:14 -08002133 gdl = inb(port->ioaddr + MOXA_MUST_GDL_REGISTER);
Jiri Slaby037ad482006-12-08 02:38:11 -08002134
Jiri Slaby55b307d2006-12-08 02:38:14 -08002135 if (port->board->chip_flag == MOXA_MUST_MU150_HWID)
Jiri Slaby037ad482006-12-08 02:38:11 -08002136 gdl &= MOXA_MUST_GDL_MASK;
2137 if (gdl >= recv_room) {
Jiri Slabye079f492006-12-08 02:38:31 -08002138 if (!port->ldisc_stop_rx)
Jiri Slaby037ad482006-12-08 02:38:11 -08002139 mxser_stoprx(tty);
Jiri Slaby037ad482006-12-08 02:38:11 -08002140 }
2141 while (gdl--) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002142 ch = inb(port->ioaddr + UART_RX);
Jiri Slaby037ad482006-12-08 02:38:11 -08002143 tty_insert_flip_char(tty, ch, 0);
2144 cnt++;
Jiri Slaby037ad482006-12-08 02:38:11 -08002145 }
2146 goto end_intr;
2147 }
Jiri Slabye079f492006-12-08 02:38:31 -08002148intr_old:
Jiri Slaby037ad482006-12-08 02:38:11 -08002149
2150 do {
2151 if (max-- < 0)
2152 break;
Jiri Slaby037ad482006-12-08 02:38:11 -08002153
Jiri Slaby55b307d2006-12-08 02:38:14 -08002154 ch = inb(port->ioaddr + UART_RX);
Jiri Slabye079f492006-12-08 02:38:31 -08002155 if (port->board->chip_flag && (*status & UART_LSR_OE))
Jiri Slaby55b307d2006-12-08 02:38:14 -08002156 outb(0x23, port->ioaddr + UART_FCR);
2157 *status &= port->read_status_mask;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002158 if (*status & port->ignore_status_mask) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002159 if (++ignored > 100)
2160 break;
2161 } else {
2162 char flag = 0;
2163 if (*status & UART_LSR_SPECIAL) {
2164 if (*status & UART_LSR_BI) {
2165 flag = TTY_BREAK;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002166 port->icount.brk++;
Jiri Slaby3306ce32006-12-08 02:38:13 -08002167
Jiri Slaby55b307d2006-12-08 02:38:14 -08002168 if (port->flags & ASYNC_SAK)
Jiri Slaby037ad482006-12-08 02:38:11 -08002169 do_SAK(tty);
2170 } else if (*status & UART_LSR_PE) {
2171 flag = TTY_PARITY;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002172 port->icount.parity++;
Jiri Slaby037ad482006-12-08 02:38:11 -08002173 } else if (*status & UART_LSR_FE) {
2174 flag = TTY_FRAME;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002175 port->icount.frame++;
Jiri Slaby037ad482006-12-08 02:38:11 -08002176 } else if (*status & UART_LSR_OE) {
2177 flag = TTY_OVERRUN;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002178 port->icount.overrun++;
Jiri Slabyf64c84a2007-02-10 01:45:21 -08002179 } else
2180 flag = TTY_BREAK;
Jiri Slaby925e9c12006-12-08 02:38:30 -08002181 }
Jiri Slaby037ad482006-12-08 02:38:11 -08002182 tty_insert_flip_char(tty, ch, flag);
2183 cnt++;
2184 if (cnt >= recv_room) {
Jiri Slabye079f492006-12-08 02:38:31 -08002185 if (!port->ldisc_stop_rx)
Jiri Slaby037ad482006-12-08 02:38:11 -08002186 mxser_stoprx(tty);
Jiri Slaby037ad482006-12-08 02:38:11 -08002187 break;
2188 }
2189
2190 }
2191
Jiri Slaby55b307d2006-12-08 02:38:14 -08002192 if (port->board->chip_flag)
Jiri Slaby037ad482006-12-08 02:38:11 -08002193 break;
Jiri Slaby037ad482006-12-08 02:38:11 -08002194
Jiri Slaby55b307d2006-12-08 02:38:14 -08002195 *status = inb(port->ioaddr + UART_LSR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002196 } while (*status & UART_LSR_DR);
2197
Jiri Slabye079f492006-12-08 02:38:31 -08002198end_intr:
Jiri Slaby55b307d2006-12-08 02:38:14 -08002199 mxvar_log.rxcnt[port->tty->index] += cnt;
2200 port->mon_data.rxcnt += cnt;
2201 port->mon_data.up_rxcnt += cnt;
Jiri Slaby037ad482006-12-08 02:38:11 -08002202
Jan Yenya Kasprzak67d2bc52007-04-23 14:41:02 -07002203 /*
2204 * We are called from an interrupt context with &port->slock
2205 * being held. Drop it temporarily in order to prevent
2206 * recursive locking.
2207 */
2208 spin_unlock(&port->slock);
Jiri Slaby037ad482006-12-08 02:38:11 -08002209 tty_flip_buffer_push(tty);
Jan Yenya Kasprzak67d2bc52007-04-23 14:41:02 -07002210 spin_lock(&port->slock);
Jiri Slaby037ad482006-12-08 02:38:11 -08002211}
2212
Jiri Slaby55b307d2006-12-08 02:38:14 -08002213static void mxser_transmit_chars(struct mxser_port *port)
Jiri Slaby037ad482006-12-08 02:38:11 -08002214{
2215 int count, cnt;
Jiri Slaby037ad482006-12-08 02:38:11 -08002216
Jiri Slaby55b307d2006-12-08 02:38:14 -08002217 if (port->x_char) {
2218 outb(port->x_char, port->ioaddr + UART_TX);
2219 port->x_char = 0;
2220 mxvar_log.txcnt[port->tty->index]++;
2221 port->mon_data.txcnt++;
2222 port->mon_data.up_txcnt++;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002223 port->icount.tx++;
Jiri Slabyf5748742007-02-10 01:45:18 -08002224 return;
Jiri Slaby037ad482006-12-08 02:38:11 -08002225 }
2226
Jiri Slaby55b307d2006-12-08 02:38:14 -08002227 if (port->xmit_buf == 0)
Jiri Slabyf5748742007-02-10 01:45:18 -08002228 return;
Jiri Slaby037ad482006-12-08 02:38:11 -08002229
Jiri Slaby925e9c12006-12-08 02:38:30 -08002230 if ((port->xmit_cnt <= 0) || port->tty->stopped ||
2231 (port->tty->hw_stopped &&
Jiri Slaby55b307d2006-12-08 02:38:14 -08002232 (port->type != PORT_16550A) &&
2233 (!port->board->chip_flag))) {
2234 port->IER &= ~UART_IER_THRI;
2235 outb(port->IER, port->ioaddr + UART_IER);
Jiri Slabyf5748742007-02-10 01:45:18 -08002236 return;
Jiri Slaby037ad482006-12-08 02:38:11 -08002237 }
2238
Jiri Slaby55b307d2006-12-08 02:38:14 -08002239 cnt = port->xmit_cnt;
2240 count = port->xmit_fifo_size;
Jiri Slaby037ad482006-12-08 02:38:11 -08002241 do {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002242 outb(port->xmit_buf[port->xmit_tail++],
2243 port->ioaddr + UART_TX);
2244 port->xmit_tail = port->xmit_tail & (SERIAL_XMIT_SIZE - 1);
2245 if (--port->xmit_cnt <= 0)
Jiri Slaby037ad482006-12-08 02:38:11 -08002246 break;
2247 } while (--count > 0);
Jiri Slaby55b307d2006-12-08 02:38:14 -08002248 mxvar_log.txcnt[port->tty->index] += (cnt - port->xmit_cnt);
Jiri Slaby037ad482006-12-08 02:38:11 -08002249
Jiri Slaby55b307d2006-12-08 02:38:14 -08002250 port->mon_data.txcnt += (cnt - port->xmit_cnt);
2251 port->mon_data.up_txcnt += (cnt - port->xmit_cnt);
Jiri Slaby55b307d2006-12-08 02:38:14 -08002252 port->icount.tx += (cnt - port->xmit_cnt);
Jiri Slaby037ad482006-12-08 02:38:11 -08002253
Jiri Slaby47c85c02007-02-10 01:45:15 -08002254 if (port->xmit_cnt < WAKEUP_CHARS)
2255 tty_wakeup(port->tty);
2256
Jiri Slaby55b307d2006-12-08 02:38:14 -08002257 if (port->xmit_cnt <= 0) {
2258 port->IER &= ~UART_IER_THRI;
2259 outb(port->IER, port->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08002260 }
Jiri Slaby037ad482006-12-08 02:38:11 -08002261}
2262
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002263/*
2264 * This is the serial driver's generic interrupt routine
2265 */
Jiri Slabyb1d1c8d2006-12-08 02:38:31 -08002266static irqreturn_t mxser_interrupt(int irq, void *dev_id)
Jiri Slaby037ad482006-12-08 02:38:11 -08002267{
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002268 int status, iir, i;
2269 struct mxser_board *brd = NULL;
2270 struct mxser_port *port;
2271 int max, irqbits, bits, msr;
Jiri Slaby18b95572007-02-10 01:45:19 -08002272 unsigned int int_cnt, pass_counter = 0;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002273 int handled = IRQ_NONE;
Jiri Slaby037ad482006-12-08 02:38:11 -08002274
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002275 for (i = 0; i < MXSER_BOARDS; i++)
2276 if (dev_id == &mxser_boards[i]) {
2277 brd = dev_id;
2278 break;
Jiri Slaby037ad482006-12-08 02:38:11 -08002279 }
Jiri Slaby037ad482006-12-08 02:38:11 -08002280
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002281 if (i == MXSER_BOARDS)
2282 goto irq_stop;
2283 if (brd == NULL)
2284 goto irq_stop;
Jiri Slabycd7ed642006-12-08 02:38:29 -08002285 max = brd->info->nports;
Jiri Slaby18b95572007-02-10 01:45:19 -08002286 while (pass_counter++ < MXSER_ISR_PASS_LIMIT) {
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002287 irqbits = inb(brd->vector) & brd->vector_mask;
2288 if (irqbits == brd->vector_mask)
Jiri Slaby037ad482006-12-08 02:38:11 -08002289 break;
Jiri Slaby037ad482006-12-08 02:38:11 -08002290
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002291 handled = IRQ_HANDLED;
2292 for (i = 0, bits = 1; i < max; i++, irqbits |= bits, bits <<= 1) {
2293 if (irqbits == brd->vector_mask)
Jiri Slaby037ad482006-12-08 02:38:11 -08002294 break;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002295 if (bits & irqbits)
2296 continue;
2297 port = &brd->ports[i];
Jiri Slaby037ad482006-12-08 02:38:11 -08002298
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002299 int_cnt = 0;
Jiri Slabyf5748742007-02-10 01:45:18 -08002300 spin_lock(&port->slock);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002301 do {
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002302 iir = inb(port->ioaddr + UART_IIR);
2303 if (iir & UART_IIR_NO_INT)
2304 break;
2305 iir &= MOXA_MUST_IIR_MASK;
Jiri Slabyf5748742007-02-10 01:45:18 -08002306 if (!port->tty ||
2307 (port->flags & ASYNC_CLOSING) ||
2308 !(port->flags &
2309 ASYNC_INITIALIZED)) {
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002310 status = inb(port->ioaddr + UART_LSR);
2311 outb(0x27, port->ioaddr + UART_FCR);
2312 inb(port->ioaddr + UART_MSR);
2313 break;
Jiri Slaby037ad482006-12-08 02:38:11 -08002314 }
Jiri Slaby037ad482006-12-08 02:38:11 -08002315
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002316 status = inb(port->ioaddr + UART_LSR);
2317
2318 if (status & UART_LSR_PE)
2319 port->err_shadow |= NPPI_NOTIFY_PARITY;
2320 if (status & UART_LSR_FE)
2321 port->err_shadow |= NPPI_NOTIFY_FRAMING;
2322 if (status & UART_LSR_OE)
2323 port->err_shadow |=
2324 NPPI_NOTIFY_HW_OVERRUN;
2325 if (status & UART_LSR_BI)
2326 port->err_shadow |= NPPI_NOTIFY_BREAK;
2327
2328 if (port->board->chip_flag) {
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002329 if (iir == MOXA_MUST_IIR_GDA ||
2330 iir == MOXA_MUST_IIR_RDA ||
2331 iir == MOXA_MUST_IIR_RTO ||
2332 iir == MOXA_MUST_IIR_LSR)
2333 mxser_receive_chars(port,
2334 &status);
2335
2336 } else {
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002337 status &= port->read_status_mask;
2338 if (status & UART_LSR_DR)
2339 mxser_receive_chars(port,
2340 &status);
2341 }
2342 msr = inb(port->ioaddr + UART_MSR);
2343 if (msr & UART_MSR_ANY_DELTA)
2344 mxser_check_modem_status(port, msr);
2345
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002346 if (port->board->chip_flag) {
2347 if (iir == 0x02 && (status &
2348 UART_LSR_THRE))
2349 mxser_transmit_chars(port);
2350 } else {
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002351 if (status & UART_LSR_THRE)
2352 mxser_transmit_chars(port);
2353 }
2354 } while (int_cnt++ < MXSER_ISR_PASS_LIMIT);
Jiri Slabyf5748742007-02-10 01:45:18 -08002355 spin_unlock(&port->slock);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002356 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002357 }
2358
Jiri Slabye079f492006-12-08 02:38:31 -08002359irq_stop:
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002360 return handled;
2361}
2362
2363static const struct tty_operations mxser_ops = {
2364 .open = mxser_open,
2365 .close = mxser_close,
2366 .write = mxser_write,
2367 .put_char = mxser_put_char,
2368 .flush_chars = mxser_flush_chars,
2369 .write_room = mxser_write_room,
2370 .chars_in_buffer = mxser_chars_in_buffer,
2371 .flush_buffer = mxser_flush_buffer,
2372 .ioctl = mxser_ioctl,
2373 .throttle = mxser_throttle,
2374 .unthrottle = mxser_unthrottle,
2375 .set_termios = mxser_set_termios,
2376 .stop = mxser_stop,
2377 .start = mxser_start,
2378 .hangup = mxser_hangup,
2379 .break_ctl = mxser_rs_break,
2380 .wait_until_sent = mxser_wait_until_sent,
2381 .tiocmget = mxser_tiocmget,
2382 .tiocmset = mxser_tiocmset,
2383};
2384
2385/*
2386 * The MOXA Smartio/Industio serial driver boot-time initialization code!
2387 */
2388
Jiri Slaby2094e752006-12-08 02:38:33 -08002389static void mxser_release_res(struct mxser_board *brd, struct pci_dev *pdev,
2390 unsigned int irq)
Jiri Slaby171d3a82006-12-08 02:38:25 -08002391{
Jiri Slaby171d3a82006-12-08 02:38:25 -08002392 if (irq)
2393 free_irq(brd->irq, brd);
2394 if (pdev != NULL) { /* PCI */
Jiri Slabyfb0c9292006-12-13 00:34:19 -08002395#ifdef CONFIG_PCI
Jiri Slaby171d3a82006-12-08 02:38:25 -08002396 pci_release_region(pdev, 2);
2397 pci_release_region(pdev, 3);
Jiri Slabyfb0c9292006-12-13 00:34:19 -08002398#endif
Jiri Slaby171d3a82006-12-08 02:38:25 -08002399 } else {
Jiri Slabycd7ed642006-12-08 02:38:29 -08002400 release_region(brd->ports[0].ioaddr, 8 * brd->info->nports);
Jiri Slaby171d3a82006-12-08 02:38:25 -08002401 release_region(brd->vector, 1);
2402 }
2403}
2404
Jiri Slaby2094e752006-12-08 02:38:33 -08002405static int __devinit mxser_initbrd(struct mxser_board *brd,
2406 struct pci_dev *pdev)
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002407{
2408 struct mxser_port *info;
2409 unsigned int i;
2410 int retval;
2411
2412 printk(KERN_INFO "max. baud rate = %d bps.\n", brd->ports[0].max_baud);
2413
Jiri Slabycd7ed642006-12-08 02:38:29 -08002414 for (i = 0; i < brd->info->nports; i++) {
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002415 info = &brd->ports[i];
2416 info->board = brd;
2417 info->stop_rx = 0;
2418 info->ldisc_stop_rx = 0;
2419
2420 /* Enhance mode enabled here */
2421 if (brd->chip_flag != MOXA_OTHER_UART)
2422 ENABLE_MOXA_MUST_ENCHANCE_MODE(info->ioaddr);
2423
2424 info->flags = ASYNC_SHARE_IRQ;
2425 info->type = brd->uart_type;
2426
2427 process_txrx_fifo(info);
2428
2429 info->custom_divisor = info->baud_base * 16;
2430 info->close_delay = 5 * HZ / 10;
2431 info->closing_wait = 30 * HZ;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002432 info->normal_termios = mxvar_sdriver->init_termios;
2433 init_waitqueue_head(&info->open_wait);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002434 init_waitqueue_head(&info->delta_msr_wait);
Jiri Slabyf64c84a2007-02-10 01:45:21 -08002435 info->speed = 9600;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002436 memset(&info->mon_data, 0, sizeof(struct mxser_mon));
2437 info->err_shadow = 0;
2438 spin_lock_init(&info->slock);
2439
2440 /* before set INT ISR, disable all int */
2441 outb(inb(info->ioaddr + UART_IER) & 0xf0,
2442 info->ioaddr + UART_IER);
2443 }
Jiri Slaby037ad482006-12-08 02:38:11 -08002444
Jiri Slabyc493edd2007-02-10 01:45:16 -08002445 retval = request_irq(brd->irq, mxser_interrupt, IRQF_SHARED, "mxser",
2446 brd);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002447 if (retval) {
2448 printk(KERN_ERR "Board %s: Request irq failed, IRQ (%d) may "
2449 "conflict with another device.\n",
Jiri Slabycd7ed642006-12-08 02:38:29 -08002450 brd->info->name, brd->irq);
Jiri Slaby171d3a82006-12-08 02:38:25 -08002451 /* We hold resources, we need to release them. */
Jiri Slaby2094e752006-12-08 02:38:33 -08002452 mxser_release_res(brd, pdev, 0);
Jiri Slaby037ad482006-12-08 02:38:11 -08002453 }
Jiri Slaby45257fa2007-02-10 01:45:20 -08002454 return retval;
Jiri Slaby037ad482006-12-08 02:38:11 -08002455}
2456
Jiri Slaby943f2952006-12-08 02:38:15 -08002457static int __init mxser_get_ISA_conf(int cap, struct mxser_board *brd)
Jiri Slaby037ad482006-12-08 02:38:11 -08002458{
2459 int id, i, bits;
2460 unsigned short regs[16], irq;
2461 unsigned char scratch, scratch2;
2462
Jiri Slaby55b307d2006-12-08 02:38:14 -08002463 brd->chip_flag = MOXA_OTHER_UART;
Jiri Slaby037ad482006-12-08 02:38:11 -08002464
2465 id = mxser_read_register(cap, regs);
Jiri Slabycd7ed642006-12-08 02:38:29 -08002466 switch (id) {
2467 case C168_ASIC_ID:
2468 brd->info = &mxser_cards[0];
2469 break;
2470 case C104_ASIC_ID:
2471 brd->info = &mxser_cards[1];
2472 break;
2473 case CI104J_ASIC_ID:
2474 brd->info = &mxser_cards[2];
2475 break;
2476 case C102_ASIC_ID:
2477 brd->info = &mxser_cards[5];
2478 break;
2479 case CI132_ASIC_ID:
2480 brd->info = &mxser_cards[6];
2481 break;
2482 case CI134_ASIC_ID:
2483 brd->info = &mxser_cards[7];
2484 break;
2485 default:
Jiri Slaby037ad482006-12-08 02:38:11 -08002486 return 0;
Jiri Slabycd7ed642006-12-08 02:38:29 -08002487 }
Jiri Slaby037ad482006-12-08 02:38:11 -08002488
2489 irq = 0;
Jiri Slabycd7ed642006-12-08 02:38:29 -08002490 /* some ISA cards have 2 ports, but we want to see them as 4-port (why?)
2491 Flag-hack checks if configuration should be read as 2-port here. */
2492 if (brd->info->nports == 2 || (brd->info->flags & MXSER_HAS2)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002493 irq = regs[9] & 0xF000;
2494 irq = irq | (irq >> 4);
2495 if (irq != (regs[9] & 0xFF00))
2496 return MXSER_ERR_IRQ_CONFLIT;
Jiri Slabycd7ed642006-12-08 02:38:29 -08002497 } else if (brd->info->nports == 4) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002498 irq = regs[9] & 0xF000;
2499 irq = irq | (irq >> 4);
2500 irq = irq | (irq >> 8);
2501 if (irq != regs[9])
2502 return MXSER_ERR_IRQ_CONFLIT;
Jiri Slabycd7ed642006-12-08 02:38:29 -08002503 } else if (brd->info->nports == 8) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002504 irq = regs[9] & 0xF000;
2505 irq = irq | (irq >> 4);
2506 irq = irq | (irq >> 8);
2507 if ((irq != regs[9]) || (irq != regs[10]))
2508 return MXSER_ERR_IRQ_CONFLIT;
2509 }
2510
2511 if (!irq)
2512 return MXSER_ERR_IRQ;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002513 brd->irq = ((int)(irq & 0xF000) >> 12);
Jiri Slaby037ad482006-12-08 02:38:11 -08002514 for (i = 0; i < 8; i++)
Jiri Slaby55b307d2006-12-08 02:38:14 -08002515 brd->ports[i].ioaddr = (int) regs[i + 1] & 0xFFF8;
Jiri Slaby037ad482006-12-08 02:38:11 -08002516 if ((regs[12] & 0x80) == 0)
2517 return MXSER_ERR_VECTOR;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002518 brd->vector = (int)regs[11]; /* interrupt vector */
Jiri Slaby037ad482006-12-08 02:38:11 -08002519 if (id == 1)
Jiri Slaby55b307d2006-12-08 02:38:14 -08002520 brd->vector_mask = 0x00FF;
Jiri Slaby037ad482006-12-08 02:38:11 -08002521 else
Jiri Slaby55b307d2006-12-08 02:38:14 -08002522 brd->vector_mask = 0x000F;
Jiri Slaby037ad482006-12-08 02:38:11 -08002523 for (i = 7, bits = 0x0100; i >= 0; i--, bits <<= 1) {
2524 if (regs[12] & bits) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002525 brd->ports[i].baud_base = 921600;
Jiri Slabye079f492006-12-08 02:38:31 -08002526 brd->ports[i].max_baud = 921600;
Jiri Slaby037ad482006-12-08 02:38:11 -08002527 } else {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002528 brd->ports[i].baud_base = 115200;
Jiri Slabye079f492006-12-08 02:38:31 -08002529 brd->ports[i].max_baud = 115200;
Jiri Slaby037ad482006-12-08 02:38:11 -08002530 }
2531 }
2532 scratch2 = inb(cap + UART_LCR) & (~UART_LCR_DLAB);
2533 outb(scratch2 | UART_LCR_DLAB, cap + UART_LCR);
2534 outb(0, cap + UART_EFR); /* EFR is the same as FCR */
2535 outb(scratch2, cap + UART_LCR);
2536 outb(UART_FCR_ENABLE_FIFO, cap + UART_FCR);
2537 scratch = inb(cap + UART_IIR);
2538
2539 if (scratch & 0xC0)
Jiri Slaby55b307d2006-12-08 02:38:14 -08002540 brd->uart_type = PORT_16550A;
Jiri Slaby037ad482006-12-08 02:38:11 -08002541 else
Jiri Slaby55b307d2006-12-08 02:38:14 -08002542 brd->uart_type = PORT_16450;
Jiri Slabycd7ed642006-12-08 02:38:29 -08002543 if (!request_region(brd->ports[0].ioaddr, 8 * brd->info->nports,
2544 "mxser(IO)"))
Jiri Slaby7a7a5c32006-12-08 02:38:17 -08002545 return MXSER_ERR_IOADDR;
2546 if (!request_region(brd->vector, 1, "mxser(vector)")) {
Jiri Slabycd7ed642006-12-08 02:38:29 -08002547 release_region(brd->ports[0].ioaddr, 8 * brd->info->nports);
Jiri Slaby7a7a5c32006-12-08 02:38:17 -08002548 return MXSER_ERR_VECTOR;
2549 }
Jiri Slabycd7ed642006-12-08 02:38:29 -08002550 return brd->info->nports;
Jiri Slaby037ad482006-12-08 02:38:11 -08002551}
2552
Jiri Slaby2094e752006-12-08 02:38:33 -08002553static int __devinit mxser_probe(struct pci_dev *pdev,
2554 const struct pci_device_id *ent)
Jiri Slaby037ad482006-12-08 02:38:11 -08002555{
Jiri Slabyfb0c9292006-12-13 00:34:19 -08002556#ifdef CONFIG_PCI
Jiri Slaby2094e752006-12-08 02:38:33 -08002557 struct mxser_board *brd;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002558 unsigned int i, j;
2559 unsigned long ioaddress;
Jiri Slaby2094e752006-12-08 02:38:33 -08002560 int retval = -EINVAL;
2561
2562 for (i = 0; i < MXSER_BOARDS; i++)
2563 if (mxser_boards[i].info == NULL)
2564 break;
2565
2566 if (i >= MXSER_BOARDS) {
2567 printk(KERN_ERR "Too many Smartio/Industio family boards found "
2568 "(maximum %d), board not configured\n", MXSER_BOARDS);
2569 goto err;
2570 }
2571
2572 brd = &mxser_boards[i];
2573 brd->idx = i * MXSER_PORTS_PER_BOARD;
2574 printk(KERN_INFO "Found MOXA %s board (BusNo=%d, DevNo=%d)\n",
2575 mxser_cards[ent->driver_data].name,
2576 pdev->bus->number, PCI_SLOT(pdev->devfn));
2577
2578 retval = pci_enable_device(pdev);
2579 if (retval) {
2580 printk(KERN_ERR "Moxa SmartI/O PCI enable fail !\n");
2581 goto err;
2582 }
Jiri Slaby037ad482006-12-08 02:38:11 -08002583
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002584 /* io address */
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002585 ioaddress = pci_resource_start(pdev, 2);
2586 retval = pci_request_region(pdev, 2, "mxser(IO)");
2587 if (retval)
2588 goto err;
2589
Jiri Slaby2094e752006-12-08 02:38:33 -08002590 brd->info = &mxser_cards[ent->driver_data];
Jiri Slabycd7ed642006-12-08 02:38:29 -08002591 for (i = 0; i < brd->info->nports; i++)
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002592 brd->ports[i].ioaddr = ioaddress + 8 * i;
2593
2594 /* vector */
2595 ioaddress = pci_resource_start(pdev, 3);
2596 retval = pci_request_region(pdev, 3, "mxser(vector)");
2597 if (retval)
2598 goto err_relio;
2599 brd->vector = ioaddress;
2600
2601 /* irq */
2602 brd->irq = pdev->irq;
2603
2604 brd->chip_flag = CheckIsMoxaMust(brd->ports[0].ioaddr);
2605 brd->uart_type = PORT_16550A;
2606 brd->vector_mask = 0;
2607
Jiri Slabycd7ed642006-12-08 02:38:29 -08002608 for (i = 0; i < brd->info->nports; i++) {
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002609 for (j = 0; j < UART_INFO_NUM; j++) {
2610 if (Gpci_uart_info[j].type == brd->chip_flag) {
2611 brd->ports[i].max_baud =
2612 Gpci_uart_info[j].max_baud;
2613
2614 /* exception....CP-102 */
Jiri Slabycd7ed642006-12-08 02:38:29 -08002615 if (brd->info->flags & MXSER_HIGHBAUD)
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002616 brd->ports[i].max_baud = 921600;
2617 break;
Jiri Slaby037ad482006-12-08 02:38:11 -08002618 }
2619 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002620 }
2621
2622 if (brd->chip_flag == MOXA_MUST_MU860_HWID) {
Jiri Slabycd7ed642006-12-08 02:38:29 -08002623 for (i = 0; i < brd->info->nports; i++) {
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002624 if (i < 4)
2625 brd->ports[i].opmode_ioaddr = ioaddress + 4;
2626 else
2627 brd->ports[i].opmode_ioaddr = ioaddress + 0x0c;
Jiri Slaby037ad482006-12-08 02:38:11 -08002628 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002629 outb(0, ioaddress + 4); /* default set to RS232 mode */
2630 outb(0, ioaddress + 0x0c); /* default set to RS232 mode */
Jiri Slaby037ad482006-12-08 02:38:11 -08002631 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002632
Jiri Slabycd7ed642006-12-08 02:38:29 -08002633 for (i = 0; i < brd->info->nports; i++) {
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002634 brd->vector_mask |= (1 << i);
2635 brd->ports[i].baud_base = 921600;
2636 }
Jiri Slaby2094e752006-12-08 02:38:33 -08002637
2638 /* mxser_initbrd will hook ISR. */
Jiri Slaby45257fa2007-02-10 01:45:20 -08002639 retval = mxser_initbrd(brd, pdev);
2640 if (retval)
2641 goto err_null;
Jiri Slaby2094e752006-12-08 02:38:33 -08002642
2643 for (i = 0; i < brd->info->nports; i++)
2644 tty_register_device(mxvar_sdriver, brd->idx + i, &pdev->dev);
2645
2646 pci_set_drvdata(pdev, brd);
2647
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002648 return 0;
2649err_relio:
2650 pci_release_region(pdev, 2);
Jiri Slaby45257fa2007-02-10 01:45:20 -08002651err_null:
Jiri Slaby2094e752006-12-08 02:38:33 -08002652 brd->info = NULL;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002653err:
2654 return retval;
Jiri Slabyfb0c9292006-12-13 00:34:19 -08002655#else
2656 return -ENODEV;
2657#endif
Jiri Slaby037ad482006-12-08 02:38:11 -08002658}
2659
Jiri Slaby2094e752006-12-08 02:38:33 -08002660static void __devexit mxser_remove(struct pci_dev *pdev)
2661{
2662 struct mxser_board *brd = pci_get_drvdata(pdev);
2663 unsigned int i;
2664
2665 for (i = 0; i < brd->info->nports; i++)
2666 tty_unregister_device(mxvar_sdriver, brd->idx + i);
2667
2668 mxser_release_res(brd, pdev, 1);
Jiri Slaby45257fa2007-02-10 01:45:20 -08002669 brd->info = NULL;
Jiri Slaby2094e752006-12-08 02:38:33 -08002670}
2671
2672static struct pci_driver mxser_driver = {
2673 .name = "mxser",
2674 .id_table = mxser_pcibrds,
2675 .probe = mxser_probe,
2676 .remove = __devexit_p(mxser_remove)
2677};
2678
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002679static int __init mxser_module_init(void)
Jiri Slaby037ad482006-12-08 02:38:11 -08002680{
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002681 struct mxser_board *brd;
Jiri Slabyeae44362006-12-08 02:38:27 -08002682 unsigned long cap;
2683 unsigned int i, m, isaloop;
Jiri Slaby2094e752006-12-08 02:38:33 -08002684 int retval, b;
Jiri Slaby037ad482006-12-08 02:38:11 -08002685
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002686 pr_debug("Loading module mxser ...\n");
Jiri Slaby037ad482006-12-08 02:38:11 -08002687
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002688 mxvar_sdriver = alloc_tty_driver(MXSER_PORTS + 1);
2689 if (!mxvar_sdriver)
2690 return -ENOMEM;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002691
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002692 printk(KERN_INFO "MOXA Smartio/Industio family driver version %s\n",
2693 MXSER_VERSION);
2694
2695 /* Initialize the tty_driver structure */
Jiri Slaby31f87cf2006-12-29 16:47:34 -08002696 mxvar_sdriver->owner = THIS_MODULE;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002697 mxvar_sdriver->magic = TTY_DRIVER_MAGIC;
Jiri Slabye9307902006-12-08 02:38:35 -08002698 mxvar_sdriver->name = "ttyMI";
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002699 mxvar_sdriver->major = ttymajor;
2700 mxvar_sdriver->minor_start = 0;
2701 mxvar_sdriver->num = MXSER_PORTS + 1;
2702 mxvar_sdriver->type = TTY_DRIVER_TYPE_SERIAL;
2703 mxvar_sdriver->subtype = SERIAL_TYPE_NORMAL;
2704 mxvar_sdriver->init_termios = tty_std_termios;
2705 mxvar_sdriver->init_termios.c_cflag = B9600|CS8|CREAD|HUPCL|CLOCAL;
Jiri Slaby938ef182006-12-08 02:38:28 -08002706 mxvar_sdriver->flags = TTY_DRIVER_REAL_RAW|TTY_DRIVER_DYNAMIC_DEV;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002707 tty_set_operations(mxvar_sdriver, &mxser_ops);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002708
Jiri Slaby938ef182006-12-08 02:38:28 -08002709 retval = tty_register_driver(mxvar_sdriver);
2710 if (retval) {
2711 printk(KERN_ERR "Couldn't install MOXA Smartio/Industio family "
2712 "tty driver !\n");
2713 goto err_put;
2714 }
2715
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002716 mxvar_diagflag = 0;
2717
2718 m = 0;
2719 /* Start finding ISA boards here */
Jiri Slabyeae44362006-12-08 02:38:27 -08002720 for (isaloop = 0; isaloop < 2; isaloop++)
2721 for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
2722 if (!isaloop)
2723 cap = mxserBoardCAP[b]; /* predefined */
2724 else
2725 cap = ioaddr[b]; /* module param */
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002726
Jiri Slabyeae44362006-12-08 02:38:27 -08002727 if (!cap)
2728 continue;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002729
Jiri Slabyeae44362006-12-08 02:38:27 -08002730 brd = &mxser_boards[m];
2731 retval = mxser_get_ISA_conf(cap, brd);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002732
Jiri Slabyeae44362006-12-08 02:38:27 -08002733 if (retval != 0)
2734 printk(KERN_INFO "Found MOXA %s board "
2735 "(CAP=0x%x)\n",
Jiri Slabycd7ed642006-12-08 02:38:29 -08002736 brd->info->name, ioaddr[b]);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002737
Jiri Slabyeae44362006-12-08 02:38:27 -08002738 if (retval <= 0) {
2739 if (retval == MXSER_ERR_IRQ)
2740 printk(KERN_ERR "Invalid interrupt "
2741 "number, board not "
2742 "configured\n");
2743 else if (retval == MXSER_ERR_IRQ_CONFLIT)
2744 printk(KERN_ERR "Invalid interrupt "
2745 "number, board not "
2746 "configured\n");
2747 else if (retval == MXSER_ERR_VECTOR)
2748 printk(KERN_ERR "Invalid interrupt "
2749 "vector, board not "
2750 "configured\n");
2751 else if (retval == MXSER_ERR_IOADDR)
2752 printk(KERN_ERR "Invalid I/O address, "
2753 "board not configured\n");
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002754
Jiri Slaby2094e752006-12-08 02:38:33 -08002755 brd->info = NULL;
Jiri Slabyeae44362006-12-08 02:38:27 -08002756 continue;
2757 }
2758
Jiri Slabyeae44362006-12-08 02:38:27 -08002759 /* mxser_initbrd will hook ISR. */
Jiri Slaby2094e752006-12-08 02:38:33 -08002760 if (mxser_initbrd(brd, NULL) < 0) {
2761 brd->info = NULL;
Jiri Slabyeae44362006-12-08 02:38:27 -08002762 continue;
Jiri Slaby2094e752006-12-08 02:38:33 -08002763 }
Jiri Slabyeae44362006-12-08 02:38:27 -08002764
Jiri Slaby2094e752006-12-08 02:38:33 -08002765 brd->idx = m * MXSER_PORTS_PER_BOARD;
Jiri Slabycd7ed642006-12-08 02:38:29 -08002766 for (i = 0; i < brd->info->nports; i++)
Jiri Slaby2094e752006-12-08 02:38:33 -08002767 tty_register_device(mxvar_sdriver, brd->idx + i,
2768 NULL);
Jiri Slaby938ef182006-12-08 02:38:28 -08002769
Jiri Slabyeae44362006-12-08 02:38:27 -08002770 m++;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002771 }
2772
Jiri Slaby2094e752006-12-08 02:38:33 -08002773 retval = pci_register_driver(&mxser_driver);
2774 if (retval) {
2775 printk(KERN_ERR "Can't register pci driver\n");
2776 if (!m) {
2777 retval = -ENODEV;
2778 goto err_unr;
2779 } /* else: we have some ISA cards under control */
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002780 }
2781
2782 pr_debug("Done.\n");
2783
Jiri Slaby938ef182006-12-08 02:38:28 -08002784 return 0;
2785err_unr:
2786 tty_unregister_driver(mxvar_sdriver);
2787err_put:
2788 put_tty_driver(mxvar_sdriver);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002789 return retval;
Jiri Slaby037ad482006-12-08 02:38:11 -08002790}
2791
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002792static void __exit mxser_module_exit(void)
Jiri Slaby037ad482006-12-08 02:38:11 -08002793{
Jiri Slaby2094e752006-12-08 02:38:33 -08002794 unsigned int i, j;
Jiri Slaby037ad482006-12-08 02:38:11 -08002795
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002796 pr_debug("Unloading module mxser ...\n");
2797
Jiri Slaby2094e752006-12-08 02:38:33 -08002798 pci_unregister_driver(&mxser_driver);
2799
2800 for (i = 0; i < MXSER_BOARDS; i++) /* ISA remains */
2801 if (mxser_boards[i].info != NULL)
2802 for (j = 0; j < mxser_boards[i].info->nports; j++)
2803 tty_unregister_device(mxvar_sdriver,
2804 mxser_boards[i].idx + j);
Jiri Slabyead568c2006-12-08 02:38:26 -08002805 tty_unregister_driver(mxvar_sdriver);
2806 put_tty_driver(mxvar_sdriver);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002807
Jiri Slaby171d3a82006-12-08 02:38:25 -08002808 for (i = 0; i < MXSER_BOARDS; i++)
Jiri Slabycd7ed642006-12-08 02:38:29 -08002809 if (mxser_boards[i].info != NULL)
Jiri Slaby2094e752006-12-08 02:38:33 -08002810 mxser_release_res(&mxser_boards[i], NULL, 1);
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002811
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002812 pr_debug("Done.\n");
Jiri Slaby037ad482006-12-08 02:38:11 -08002813}
2814
2815module_init(mxser_module_init);
2816module_exit(mxser_module_exit);