blob: 92ca016d2e3ebebe788a0268bf21268ae571758f [file] [log] [blame]
Jiri Slaby037ad482006-12-08 02:38:11 -08001/*
2 * mxser.c -- MOXA Smartio/Industio family multiport serial driver.
3 *
Jiri Slaby3306ce32006-12-08 02:38:13 -08004 * Copyright (C) 1999-2006 Moxa Technologies (support@moxa.com.tw).
Jiri Slabyc88cb8f92006-12-08 02:38:20 -08005 * Copyright (C) 2006 Jiri Slaby <jirislaby@gmail.com>
Jiri Slaby037ad482006-12-08 02:38:11 -08006 *
Jiri Slabyc88cb8f92006-12-08 02:38:20 -08007 * This code is loosely based on the 1.8 moxa driver which is based on
8 * Linux serial driver, written by Linus Torvalds, Theodore T'so and
9 * others.
Jiri Slaby037ad482006-12-08 02:38:11 -080010 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
Jiri Slaby037ad482006-12-08 02:38:11 -080016 * Fed through a cleanup, indent and remove of non 2.6 code by Alan Cox
17 * <alan@redhat.com>. The original 1.8 code is available on www.moxa.com.
18 * - Fixed x86_64 cleanness
19 * - Fixed sleep with spinlock held in mxser_send_break
20 */
21
Jiri Slaby037ad482006-12-08 02:38:11 -080022#include <linux/module.h>
23#include <linux/autoconf.h>
24#include <linux/errno.h>
25#include <linux/signal.h>
26#include <linux/sched.h>
27#include <linux/timer.h>
28#include <linux/interrupt.h>
29#include <linux/tty.h>
30#include <linux/tty_flip.h>
31#include <linux/serial.h>
32#include <linux/serial_reg.h>
33#include <linux/major.h>
34#include <linux/string.h>
35#include <linux/fcntl.h>
36#include <linux/ptrace.h>
37#include <linux/gfp.h>
38#include <linux/ioport.h>
39#include <linux/mm.h>
40#include <linux/smp_lock.h>
41#include <linux/delay.h>
42#include <linux/pci.h>
43
44#include <asm/system.h>
45#include <asm/io.h>
46#include <asm/irq.h>
47#include <asm/bitops.h>
48#include <asm/uaccess.h>
49
Jiri Slaby771f2d12006-12-08 02:38:12 -080050#include "mxser_new.h"
Jiri Slaby037ad482006-12-08 02:38:11 -080051
Jiri Slaby55b307d2006-12-08 02:38:14 -080052#define MXSER_VERSION "2.0"
Jiri Slaby037ad482006-12-08 02:38:11 -080053#define MXSERMAJOR 174
54#define MXSERCUMAJOR 175
55
56#define MXSER_EVENT_TXLOW 1
57#define MXSER_EVENT_HANGUP 2
58
59#define MXSER_BOARDS 4 /* Max. boards */
60#define MXSER_PORTS 32 /* Max. ports */
61#define MXSER_PORTS_PER_BOARD 8 /* Max. ports per board */
Jiri Slaby3306ce32006-12-08 02:38:13 -080062#define MXSER_ISR_PASS_LIMIT 99999L
Jiri Slaby037ad482006-12-08 02:38:11 -080063
64#define MXSER_ERR_IOADDR -1
65#define MXSER_ERR_IRQ -2
66#define MXSER_ERR_IRQ_CONFLIT -3
67#define MXSER_ERR_VECTOR -4
68
69#define SERIAL_TYPE_NORMAL 1
70#define SERIAL_TYPE_CALLOUT 2
71
72#define WAKEUP_CHARS 256
73
74#define UART_MCR_AFE 0x20
75#define UART_LSR_SPECIAL 0x1E
76
77#define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK|\
78 IXON|IXOFF))
79
Jiri Slaby037ad482006-12-08 02:38:11 -080080#define C168_ASIC_ID 1
81#define C104_ASIC_ID 2
82#define C102_ASIC_ID 0xB
83#define CI132_ASIC_ID 4
84#define CI134_ASIC_ID 3
85#define CI104J_ASIC_ID 5
86
87enum {
88 MXSER_BOARD_C168_ISA = 1,
89 MXSER_BOARD_C104_ISA,
90 MXSER_BOARD_CI104J,
91 MXSER_BOARD_C168_PCI,
92 MXSER_BOARD_C104_PCI,
93 MXSER_BOARD_C102_ISA,
94 MXSER_BOARD_CI132,
95 MXSER_BOARD_CI134,
96 MXSER_BOARD_CP132,
97 MXSER_BOARD_CP114,
98 MXSER_BOARD_CT114,
99 MXSER_BOARD_CP102,
100 MXSER_BOARD_CP104U,
101 MXSER_BOARD_CP168U,
102 MXSER_BOARD_CP132U,
103 MXSER_BOARD_CP134U,
104 MXSER_BOARD_CP104JU,
105 MXSER_BOARD_RC7000,
106 MXSER_BOARD_CP118U,
107 MXSER_BOARD_CP102UL,
108 MXSER_BOARD_CP102U,
Jiri Slaby3306ce32006-12-08 02:38:13 -0800109 MXSER_BOARD_CP118EL,
110 MXSER_BOARD_CP168EL,
111 MXSER_BOARD_CP104EL
Jiri Slaby037ad482006-12-08 02:38:11 -0800112};
113
114static char *mxser_brdname[] = {
115 "C168 series",
116 "C104 series",
117 "CI-104J series",
118 "C168H/PCI series",
119 "C104H/PCI series",
120 "C102 series",
121 "CI-132 series",
122 "CI-134 series",
123 "CP-132 series",
124 "CP-114 series",
125 "CT-114 series",
126 "CP-102 series",
127 "CP-104U series",
128 "CP-168U series",
129 "CP-132U series",
130 "CP-134U series",
131 "CP-104JU series",
132 "Moxa UC7000 Serial",
133 "CP-118U series",
134 "CP-102UL series",
135 "CP-102U series",
Jiri Slaby3306ce32006-12-08 02:38:13 -0800136 "CP-118EL series",
137 "CP-168EL series",
138 "CP-104EL series"
Jiri Slaby037ad482006-12-08 02:38:11 -0800139};
140
141static int mxser_numports[] = {
142 8, /* C168-ISA */
143 4, /* C104-ISA */
144 4, /* CI104J */
145 8, /* C168-PCI */
146 4, /* C104-PCI */
147 2, /* C102-ISA */
148 2, /* CI132 */
149 4, /* CI134 */
150 2, /* CP132 */
151 4, /* CP114 */
152 4, /* CT114 */
153 2, /* CP102 */
154 4, /* CP104U */
155 8, /* CP168U */
156 2, /* CP132U */
157 4, /* CP134U */
158 4, /* CP104JU */
159 8, /* RC7000 */
160 8, /* CP118U */
161 2, /* CP102UL */
162 2, /* CP102U */
Jiri Slaby3306ce32006-12-08 02:38:13 -0800163 8, /* CP118EL */
164 8, /* CP168EL */
165 4 /* CP104EL */
Jiri Slaby037ad482006-12-08 02:38:11 -0800166};
167
168#define UART_TYPE_NUM 2
169
170static const unsigned int Gmoxa_uart_id[UART_TYPE_NUM] = {
171 MOXA_MUST_MU150_HWID,
172 MOXA_MUST_MU860_HWID
173};
174
175/* This is only for PCI */
176#define UART_INFO_NUM 3
177struct mxpciuart_info {
178 int type;
179 int tx_fifo;
180 int rx_fifo;
181 int xmit_fifo_size;
182 int rx_high_water;
183 int rx_trigger;
184 int rx_low_water;
185 long max_baud;
186};
187
188static const struct mxpciuart_info Gpci_uart_info[UART_INFO_NUM] = {
189 {MOXA_OTHER_UART, 16, 16, 16, 14, 14, 1, 921600L},
190 {MOXA_MUST_MU150_HWID, 64, 64, 64, 48, 48, 16, 230400L},
191 {MOXA_MUST_MU860_HWID, 128, 128, 128, 96, 96, 32, 921600L}
192};
193
Jiri Slaby037ad482006-12-08 02:38:11 -0800194static struct pci_device_id mxser_pcibrds[] = {
Jiri Slaby3306ce32006-12-08 02:38:13 -0800195 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C168),
196 .driver_data = MXSER_BOARD_C168_PCI },
197 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C104),
198 .driver_data = MXSER_BOARD_C104_PCI },
199 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP132),
200 .driver_data = MXSER_BOARD_CP132 },
201 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP114),
202 .driver_data = MXSER_BOARD_CP114 },
203 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CT114),
204 .driver_data = MXSER_BOARD_CT114 },
205 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP102),
206 .driver_data = MXSER_BOARD_CP102 },
207 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP104U),
208 .driver_data = MXSER_BOARD_CP104U },
209 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP168U),
210 .driver_data = MXSER_BOARD_CP168U },
211 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP132U),
212 .driver_data = MXSER_BOARD_CP132U },
213 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP134U),
214 .driver_data = MXSER_BOARD_CP134U },
215 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP104JU),
216 .driver_data = MXSER_BOARD_CP104JU },
217 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_RC7000),
218 .driver_data = MXSER_BOARD_RC7000 },
219 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP118U),
220 .driver_data = MXSER_BOARD_CP118U },
221 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP102UL),
222 .driver_data = MXSER_BOARD_CP102UL },
223 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP102U),
224 .driver_data = MXSER_BOARD_CP102U },
225 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP118EL),
226 .driver_data = MXSER_BOARD_CP118EL },
227 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP168EL),
228 .driver_data = MXSER_BOARD_CP168EL },
229 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP104EL),
230 .driver_data = MXSER_BOARD_CP104EL },
231 { }
Jiri Slaby037ad482006-12-08 02:38:11 -0800232};
Jiri Slaby037ad482006-12-08 02:38:11 -0800233MODULE_DEVICE_TABLE(pci, mxser_pcibrds);
234
Jiri Slaby037ad482006-12-08 02:38:11 -0800235static int ioaddr[MXSER_BOARDS] = { 0, 0, 0, 0 };
236static int ttymajor = MXSERMAJOR;
237static int calloutmajor = MXSERCUMAJOR;
Jiri Slaby037ad482006-12-08 02:38:11 -0800238
239/* Variables for insmod */
240
241MODULE_AUTHOR("Casper Yang");
242MODULE_DESCRIPTION("MOXA Smartio/Industio Family Multiport Board Device Driver");
243module_param_array(ioaddr, int, NULL, 0);
244module_param(ttymajor, int, 0);
Jiri Slaby037ad482006-12-08 02:38:11 -0800245MODULE_LICENSE("GPL");
246
247struct mxser_log {
248 int tick;
249 unsigned long rxcnt[MXSER_PORTS];
250 unsigned long txcnt[MXSER_PORTS];
251};
252
253
254struct mxser_mon {
255 unsigned long rxcnt;
256 unsigned long txcnt;
257 unsigned long up_rxcnt;
258 unsigned long up_txcnt;
259 int modem_status;
260 unsigned char hold_reason;
261};
262
263struct mxser_mon_ext {
264 unsigned long rx_cnt[32];
265 unsigned long tx_cnt[32];
266 unsigned long up_rxcnt[32];
267 unsigned long up_txcnt[32];
268 int modem_status[32];
269
270 long baudrate[32];
271 int databits[32];
272 int stopbits[32];
273 int parity[32];
274 int flowctrl[32];
275 int fifo[32];
276 int iftype[32];
277};
278
Jiri Slaby55b307d2006-12-08 02:38:14 -0800279struct mxser_board;
Jiri Slaby037ad482006-12-08 02:38:11 -0800280
Jiri Slaby55b307d2006-12-08 02:38:14 -0800281struct mxser_port {
282 struct mxser_board *board;
283 struct tty_struct *tty;
284
285 unsigned long ioaddr;
286 unsigned long opmode_ioaddr;
287 int max_baud;
288
Jiri Slaby037ad482006-12-08 02:38:11 -0800289 int rx_high_water;
290 int rx_trigger; /* Rx fifo trigger level */
291 int rx_low_water;
292 int baud_base; /* max. speed */
Jiri Slaby55b307d2006-12-08 02:38:14 -0800293 long realbaud;
Jiri Slaby037ad482006-12-08 02:38:11 -0800294 int type; /* UART type */
Jiri Slaby55b307d2006-12-08 02:38:14 -0800295 int flags; /* defined in tty.h */
296 long session; /* Session of opening process */
297 long pgrp; /* pgrp of opening process */
298
299 int x_char; /* xon/xoff character */
300 int IER; /* Interrupt Enable Register */
301 int MCR; /* Modem control register */
302
303 unsigned char stop_rx;
304 unsigned char ldisc_stop_rx;
305
306 int custom_divisor;
307 int close_delay;
308 unsigned short closing_wait;
309 unsigned char err_shadow;
310 unsigned long event;
311
312 int count; /* # of fd on device */
313 int blocked_open; /* # of blocked opens */
314 struct async_icount icount; /* kernel counters for 4 input interrupts */
315 int timeout;
316
Jiri Slaby037ad482006-12-08 02:38:11 -0800317 int read_status_mask;
318 int ignore_status_mask;
319 int xmit_fifo_size;
Jiri Slaby037ad482006-12-08 02:38:11 -0800320 unsigned char *xmit_buf;
321 int xmit_head;
322 int xmit_tail;
323 int xmit_cnt;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800324
Jiri Slaby037ad482006-12-08 02:38:11 -0800325 struct termios normal_termios;
326 struct termios callout_termios;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800327
328 struct mxser_mon mon_data;
329
330 spinlock_t slock;
331 struct work_struct tqueue;
Jiri Slaby037ad482006-12-08 02:38:11 -0800332 wait_queue_head_t open_wait;
333 wait_queue_head_t close_wait;
334 wait_queue_head_t delta_msr_wait;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800335};
336
337struct mxser_board {
338 struct pci_dev *pdev; /* temporary (until pci probing) */
339
340 int irq;
341 int board_type;
342 unsigned int nports;
343 unsigned long vector;
344 unsigned long vector_mask;
345
346 int chip_flag;
347 int uart_type;
348
349 struct mxser_port ports[MXSER_PORTS_PER_BOARD];
Jiri Slaby037ad482006-12-08 02:38:11 -0800350};
351
352struct mxser_mstatus {
353 tcflag_t cflag;
354 int cts;
355 int dsr;
356 int ri;
357 int dcd;
358};
359
360static struct mxser_mstatus GMStatus[MXSER_PORTS];
361
362static int mxserBoardCAP[MXSER_BOARDS] = {
363 0, 0, 0, 0
364 /* 0x180, 0x280, 0x200, 0x320 */
365};
366
Jiri Slaby55b307d2006-12-08 02:38:14 -0800367static struct mxser_board mxser_boards[MXSER_BOARDS];
Jiri Slaby037ad482006-12-08 02:38:11 -0800368static struct tty_driver *mxvar_sdriver;
Jiri Slaby037ad482006-12-08 02:38:11 -0800369static struct tty_struct *mxvar_tty[MXSER_PORTS + 1];
370static struct termios *mxvar_termios[MXSER_PORTS + 1];
371static struct termios *mxvar_termios_locked[MXSER_PORTS + 1];
372static struct mxser_log mxvar_log;
373static int mxvar_diagflag;
374static unsigned char mxser_msr[MXSER_PORTS + 1];
375static struct mxser_mon_ext mon_data_ext;
376static int mxser_set_baud_method[MXSER_PORTS + 1];
377static spinlock_t gm_lock;
378
Jiri Slaby037ad482006-12-08 02:38:11 -0800379static int CheckIsMoxaMust(int io)
380{
381 u8 oldmcr, hwid;
382 int i;
383
384 outb(0, io + UART_LCR);
385 DISABLE_MOXA_MUST_ENCHANCE_MODE(io);
386 oldmcr = inb(io + UART_MCR);
387 outb(0, io + UART_MCR);
388 SET_MOXA_MUST_XON1_VALUE(io, 0x11);
389 if ((hwid = inb(io + UART_MCR)) != 0) {
390 outb(oldmcr, io + UART_MCR);
391 return MOXA_OTHER_UART;
392 }
393
394 GET_MOXA_MUST_HARDWARE_ID(io, &hwid);
395 for (i = 0; i < UART_TYPE_NUM; i++) {
396 if (hwid == Gmoxa_uart_id[i])
397 return (int)hwid;
398 }
399 return MOXA_OTHER_UART;
400}
401
402/* above is modified by Victor Yu. 08-15-2002 */
403
Jiri Slaby55b307d2006-12-08 02:38:14 -0800404static void process_txrx_fifo(struct mxser_port *info)
Jiri Slaby037ad482006-12-08 02:38:11 -0800405{
406 int i;
407
408 if ((info->type == PORT_16450) || (info->type == PORT_8250)) {
409 info->rx_trigger = 1;
410 info->rx_high_water = 1;
411 info->rx_low_water = 1;
412 info->xmit_fifo_size = 1;
Jiri Slaby55b307d2006-12-08 02:38:14 -0800413 } else
414 for (i = 0; i < UART_INFO_NUM; i++)
415 if (info->board->chip_flag == Gpci_uart_info[i].type) {
Jiri Slaby037ad482006-12-08 02:38:11 -0800416 info->rx_trigger = Gpci_uart_info[i].rx_trigger;
417 info->rx_low_water = Gpci_uart_info[i].rx_low_water;
418 info->rx_high_water = Gpci_uart_info[i].rx_high_water;
419 info->xmit_fifo_size = Gpci_uart_info[i].xmit_fifo_size;
420 break;
421 }
Jiri Slaby037ad482006-12-08 02:38:11 -0800422}
423
Jiri Slaby037ad482006-12-08 02:38:11 -0800424static void mxser_do_softint(void *private_)
425{
Jiri Slaby55b307d2006-12-08 02:38:14 -0800426 struct mxser_port *info = private_;
Jiri Slaby037ad482006-12-08 02:38:11 -0800427 struct tty_struct *tty;
428
429 tty = info->tty;
430
Jiri Slaby3306ce32006-12-08 02:38:13 -0800431 if (test_and_clear_bit(MXSER_EVENT_TXLOW, &info->event))
432 tty_wakeup(tty);
433 if (test_and_clear_bit(MXSER_EVENT_HANGUP, &info->event))
434 tty_hangup(tty);
Jiri Slaby037ad482006-12-08 02:38:11 -0800435}
436
Jiri Slaby55b307d2006-12-08 02:38:14 -0800437static unsigned char mxser_get_msr(int baseaddr, int mode, int port)
Jiri Slaby037ad482006-12-08 02:38:11 -0800438{
439 unsigned char status = 0;
440
441 status = inb(baseaddr + UART_MSR);
442
443 mxser_msr[port] &= 0x0F;
444 mxser_msr[port] |= status;
445 status = mxser_msr[port];
446 if (mode)
447 mxser_msr[port] = 0;
448
449 return status;
450}
451
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800452static int mxser_block_til_ready(struct tty_struct *tty, struct file *filp,
453 struct mxser_port *port)
454{
455 DECLARE_WAITQUEUE(wait, current);
456 int retval;
457 int do_clocal = 0;
458 unsigned long flags;
459
460 /*
461 * If non-blocking mode is set, or the port is not enabled,
462 * then make the check up front and then exit.
463 */
464 if ((filp->f_flags & O_NONBLOCK) || (tty->flags & (1 << TTY_IO_ERROR))) {
465 port->flags |= ASYNC_NORMAL_ACTIVE;
466 return 0;
467 }
468
469 if (tty->termios->c_cflag & CLOCAL)
470 do_clocal = 1;
471
472 /*
473 * Block waiting for the carrier detect and the line to become
474 * free (i.e., not in use by the callout). While we are in
475 * this loop, port->count is dropped by one, so that
476 * mxser_close() knows when to free things. We restore it upon
477 * exit, either normal or abnormal.
478 */
479 retval = 0;
480 add_wait_queue(&port->open_wait, &wait);
481
482 spin_lock_irqsave(&port->slock, flags);
483 if (!tty_hung_up_p(filp))
484 port->count--;
485 spin_unlock_irqrestore(&port->slock, flags);
486 port->blocked_open++;
487 while (1) {
488 spin_lock_irqsave(&port->slock, flags);
489 outb(inb(port->ioaddr + UART_MCR) |
490 UART_MCR_DTR | UART_MCR_RTS, port->ioaddr + UART_MCR);
491 spin_unlock_irqrestore(&port->slock, flags);
492 set_current_state(TASK_INTERRUPTIBLE);
493 if (tty_hung_up_p(filp) || !(port->flags & ASYNC_INITIALIZED)) {
494 if (port->flags & ASYNC_HUP_NOTIFY)
495 retval = -EAGAIN;
496 else
497 retval = -ERESTARTSYS;
498 break;
499 }
500 if (!(port->flags & ASYNC_CLOSING) &&
501 (do_clocal ||
502 (inb(port->ioaddr + UART_MSR) & UART_MSR_DCD)))
503 break;
504 if (signal_pending(current)) {
505 retval = -ERESTARTSYS;
506 break;
507 }
508 schedule();
509 }
510 set_current_state(TASK_RUNNING);
511 remove_wait_queue(&port->open_wait, &wait);
512 if (!tty_hung_up_p(filp))
513 port->count++;
514 port->blocked_open--;
515 if (retval)
516 return retval;
517 port->flags |= ASYNC_NORMAL_ACTIVE;
518 return 0;
519}
520
521static int mxser_set_baud(struct mxser_port *info, long newspd)
522{
523 int quot = 0;
524 unsigned char cval;
525 int ret = 0;
526 unsigned long flags;
527
528 if (!info->tty || !info->tty->termios)
529 return ret;
530
531 if (!(info->ioaddr))
532 return ret;
533
534 if (newspd > info->max_baud)
535 return 0;
536
537 info->realbaud = newspd;
538 if (newspd == 134) {
539 quot = (2 * info->baud_base / 269);
540 } else if (newspd) {
541 quot = info->baud_base / newspd;
542 if (quot == 0)
543 quot = 1;
544 } else {
545 quot = 0;
546 }
547
548 info->timeout = ((info->xmit_fifo_size * HZ * 10 * quot) / info->baud_base);
549 info->timeout += HZ / 50; /* Add .02 seconds of slop */
550
551 if (quot) {
552 spin_lock_irqsave(&info->slock, flags);
553 info->MCR |= UART_MCR_DTR;
554 outb(info->MCR, info->ioaddr + UART_MCR);
555 spin_unlock_irqrestore(&info->slock, flags);
556 } else {
557 spin_lock_irqsave(&info->slock, flags);
558 info->MCR &= ~UART_MCR_DTR;
559 outb(info->MCR, info->ioaddr + UART_MCR);
560 spin_unlock_irqrestore(&info->slock, flags);
561 return ret;
562 }
563
564 cval = inb(info->ioaddr + UART_LCR);
565
566 outb(cval | UART_LCR_DLAB, info->ioaddr + UART_LCR); /* set DLAB */
567
568 outb(quot & 0xff, info->ioaddr + UART_DLL); /* LS of divisor */
569 outb(quot >> 8, info->ioaddr + UART_DLM); /* MS of divisor */
570 outb(cval, info->ioaddr + UART_LCR); /* reset DLAB */
571
572
573 return ret;
574}
575
576/*
577 * This routine is called to set the UART divisor registers to match
578 * the specified baud rate for a serial port.
579 */
580static int mxser_change_speed(struct mxser_port *info,
581 struct termios *old_termios)
582{
583 unsigned cflag, cval, fcr;
584 int ret = 0;
585 unsigned char status;
586 long baud;
587 unsigned long flags;
588
589 if (!info->tty || !info->tty->termios)
590 return ret;
591 cflag = info->tty->termios->c_cflag;
592 if (!(info->ioaddr))
593 return ret;
594
595#ifndef B921600
596#define B921600 (B460800 +1)
597#endif
598 if (mxser_set_baud_method[info->tty->index] == 0) {
599 baud = tty_get_baud_rate(info->tty);
600 mxser_set_baud(info, baud);
601 }
602
603 /* byte size and parity */
604 switch (cflag & CSIZE) {
605 case CS5:
606 cval = 0x00;
607 break;
608 case CS6:
609 cval = 0x01;
610 break;
611 case CS7:
612 cval = 0x02;
613 break;
614 case CS8:
615 cval = 0x03;
616 break;
617 default:
618 cval = 0x00;
619 break; /* too keep GCC shut... */
620 }
621 if (cflag & CSTOPB)
622 cval |= 0x04;
623 if (cflag & PARENB)
624 cval |= UART_LCR_PARITY;
625 if (!(cflag & PARODD))
626 cval |= UART_LCR_EPAR;
627 if (cflag & CMSPAR)
628 cval |= UART_LCR_SPAR;
629
630 if ((info->type == PORT_8250) || (info->type == PORT_16450)) {
631 if (info->board->chip_flag) {
632 fcr = UART_FCR_ENABLE_FIFO;
633 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
634 SET_MOXA_MUST_FIFO_VALUE(info);
635 } else
636 fcr = 0;
637 } else {
638 fcr = UART_FCR_ENABLE_FIFO;
639 /* following add by Victor Yu. 08-30-2002 */
640 if (info->board->chip_flag) {
641 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
642 SET_MOXA_MUST_FIFO_VALUE(info);
643 } else {
644 /* above add by Victor Yu. 08-30-2002 */
645 switch (info->rx_trigger) {
646 case 1:
647 fcr |= UART_FCR_TRIGGER_1;
648 break;
649 case 4:
650 fcr |= UART_FCR_TRIGGER_4;
651 break;
652 case 8:
653 fcr |= UART_FCR_TRIGGER_8;
654 break;
655 default:
656 fcr |= UART_FCR_TRIGGER_14;
657 break;
658 }
659 }
660 }
661
662 /* CTS flow control flag and modem status interrupts */
663 info->IER &= ~UART_IER_MSI;
664 info->MCR &= ~UART_MCR_AFE;
665 if (cflag & CRTSCTS) {
666 info->flags |= ASYNC_CTS_FLOW;
667 info->IER |= UART_IER_MSI;
668 if ((info->type == PORT_16550A) || (info->board->chip_flag)) {
669 info->MCR |= UART_MCR_AFE;
670/* status = mxser_get_msr(info->ioaddr, 0, info->port); */
671/*
672 save_flags(flags);
673 cli();
674 status = inb(baseaddr + UART_MSR);
675 restore_flags(flags);
676*/
677 /* mxser_check_modem_status(info, status); */
678 } else {
679/* status = mxser_get_msr(info->ioaddr, 0, info->port); */
680 /* MX_LOCK(&info->slock); */
681 status = inb(info->ioaddr + UART_MSR);
682 /* MX_UNLOCK(&info->slock); */
683 if (info->tty->hw_stopped) {
684 if (status & UART_MSR_CTS) {
685 info->tty->hw_stopped = 0;
686 if (info->type != PORT_16550A &&
687 !info->board->chip_flag) {
688 outb(info->IER & ~UART_IER_THRI,
689 info->ioaddr +
690 UART_IER);
691 info->IER |= UART_IER_THRI;
692 outb(info->IER, info->ioaddr +
693 UART_IER);
694 }
695 set_bit(MXSER_EVENT_TXLOW, &info->event);
696 schedule_work(&info->tqueue); }
697 } else {
698 if (!(status & UART_MSR_CTS)) {
699 info->tty->hw_stopped = 1;
700 if ((info->type != PORT_16550A) &&
701 (!info->board->chip_flag)) {
702 info->IER &= ~UART_IER_THRI;
703 outb(info->IER, info->ioaddr +
704 UART_IER);
705 }
706 }
707 }
708 }
709 } else {
710 info->flags &= ~ASYNC_CTS_FLOW;
711 }
712 outb(info->MCR, info->ioaddr + UART_MCR);
713 if (cflag & CLOCAL) {
714 info->flags &= ~ASYNC_CHECK_CD;
715 } else {
716 info->flags |= ASYNC_CHECK_CD;
717 info->IER |= UART_IER_MSI;
718 }
719 outb(info->IER, info->ioaddr + UART_IER);
720
721 /*
722 * Set up parity check flag
723 */
724 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
725 if (I_INPCK(info->tty))
726 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
727 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
728 info->read_status_mask |= UART_LSR_BI;
729
730 info->ignore_status_mask = 0;
731
732 if (I_IGNBRK(info->tty)) {
733 info->ignore_status_mask |= UART_LSR_BI;
734 info->read_status_mask |= UART_LSR_BI;
735 /*
736 * If we're ignore parity and break indicators, ignore
737 * overruns too. (For real raw support).
738 */
739 if (I_IGNPAR(info->tty)) {
740 info->ignore_status_mask |=
741 UART_LSR_OE |
742 UART_LSR_PE |
743 UART_LSR_FE;
744 info->read_status_mask |=
745 UART_LSR_OE |
746 UART_LSR_PE |
747 UART_LSR_FE;
748 }
749 }
750 /* following add by Victor Yu. 09-02-2002 */
751 if (info->board->chip_flag) {
752 spin_lock_irqsave(&info->slock, flags);
753 SET_MOXA_MUST_XON1_VALUE(info->ioaddr, START_CHAR(info->tty));
754 SET_MOXA_MUST_XOFF1_VALUE(info->ioaddr, STOP_CHAR(info->tty));
755 if (I_IXON(info->tty)) {
756 ENABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
757 } else {
758 DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
759 }
760 if (I_IXOFF(info->tty)) {
761 ENABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
762 } else {
763 DISABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
764 }
765 /*
766 if ( I_IXANY(info->tty) ) {
767 info->MCR |= MOXA_MUST_MCR_XON_ANY;
768 ENABLE_MOXA_MUST_XON_ANY_FLOW_CONTROL(info->ioaddr);
769 } else {
770 info->MCR &= ~MOXA_MUST_MCR_XON_ANY;
771 DISABLE_MOXA_MUST_XON_ANY_FLOW_CONTROL(info->ioaddr);
772 }
773 */
774 spin_unlock_irqrestore(&info->slock, flags);
775 }
776 /* above add by Victor Yu. 09-02-2002 */
777
778
779 outb(fcr, info->ioaddr + UART_FCR); /* set fcr */
780 outb(cval, info->ioaddr + UART_LCR);
781
782 return ret;
783}
784
785static void mxser_check_modem_status(struct mxser_port *port, int status)
786{
787 /* update input line counters */
788 if (status & UART_MSR_TERI)
789 port->icount.rng++;
790 if (status & UART_MSR_DDSR)
791 port->icount.dsr++;
792 if (status & UART_MSR_DDCD)
793 port->icount.dcd++;
794 if (status & UART_MSR_DCTS)
795 port->icount.cts++;
796 port->mon_data.modem_status = status;
797 wake_up_interruptible(&port->delta_msr_wait);
798
799 if ((port->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
800 if (status & UART_MSR_DCD)
801 wake_up_interruptible(&port->open_wait);
802 schedule_work(&port->tqueue);
803 }
804
805 if (port->flags & ASYNC_CTS_FLOW) {
806 if (port->tty->hw_stopped) {
807 if (status & UART_MSR_CTS) {
808 port->tty->hw_stopped = 0;
809
810 if ((port->type != PORT_16550A) &&
811 (!port->board->chip_flag)) {
812 outb(port->IER & ~UART_IER_THRI,
813 port->ioaddr + UART_IER);
814 port->IER |= UART_IER_THRI;
815 outb(port->IER, port->ioaddr +
816 UART_IER);
817 }
818 set_bit(MXSER_EVENT_TXLOW, &port->event);
819 schedule_work(&port->tqueue);
820 }
821 } else {
822 if (!(status & UART_MSR_CTS)) {
823 port->tty->hw_stopped = 1;
824 if (port->type != PORT_16550A &&
825 !port->board->chip_flag) {
826 port->IER &= ~UART_IER_THRI;
827 outb(port->IER, port->ioaddr +
828 UART_IER);
829 }
830 }
831 }
832 }
833}
834
835static int mxser_startup(struct mxser_port *info)
836{
837 unsigned long page;
838 unsigned long flags;
839
840 page = __get_free_page(GFP_KERNEL);
841 if (!page)
842 return -ENOMEM;
843
844 spin_lock_irqsave(&info->slock, flags);
845
846 if (info->flags & ASYNC_INITIALIZED) {
847 free_page(page);
848 spin_unlock_irqrestore(&info->slock, flags);
849 return 0;
850 }
851
852 if (!info->ioaddr || !info->type) {
853 if (info->tty)
854 set_bit(TTY_IO_ERROR, &info->tty->flags);
855 free_page(page);
856 spin_unlock_irqrestore(&info->slock, flags);
857 return 0;
858 }
859 if (info->xmit_buf)
860 free_page(page);
861 else
862 info->xmit_buf = (unsigned char *) page;
863
864 /*
865 * Clear the FIFO buffers and disable them
866 * (they will be reenabled in mxser_change_speed())
867 */
868 if (info->board->chip_flag)
869 outb((UART_FCR_CLEAR_RCVR |
870 UART_FCR_CLEAR_XMIT |
871 MOXA_MUST_FCR_GDA_MODE_ENABLE), info->ioaddr + UART_FCR);
872 else
873 outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
874 info->ioaddr + UART_FCR);
875
876 /*
877 * At this point there's no way the LSR could still be 0xFF;
878 * if it is, then bail out, because there's likely no UART
879 * here.
880 */
881 if (inb(info->ioaddr + UART_LSR) == 0xff) {
882 spin_unlock_irqrestore(&info->slock, flags);
883 if (capable(CAP_SYS_ADMIN)) {
884 if (info->tty)
885 set_bit(TTY_IO_ERROR, &info->tty->flags);
886 return 0;
887 } else
888 return -ENODEV;
889 }
890
891 /*
892 * Clear the interrupt registers.
893 */
894 (void) inb(info->ioaddr + UART_LSR);
895 (void) inb(info->ioaddr + UART_RX);
896 (void) inb(info->ioaddr + UART_IIR);
897 (void) inb(info->ioaddr + UART_MSR);
898
899 /*
900 * Now, initialize the UART
901 */
902 outb(UART_LCR_WLEN8, info->ioaddr + UART_LCR); /* reset DLAB */
903 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
904 outb(info->MCR, info->ioaddr + UART_MCR);
905
906 /*
907 * Finally, enable interrupts
908 */
909 info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
910 /* info->IER = UART_IER_RLSI | UART_IER_RDI; */
911
912 /* following add by Victor Yu. 08-30-2002 */
913 if (info->board->chip_flag)
914 info->IER |= MOXA_MUST_IER_EGDAI;
915 /* above add by Victor Yu. 08-30-2002 */
916 outb(info->IER, info->ioaddr + UART_IER); /* enable interrupts */
917
918 /*
919 * And clear the interrupt registers again for luck.
920 */
921 (void) inb(info->ioaddr + UART_LSR);
922 (void) inb(info->ioaddr + UART_RX);
923 (void) inb(info->ioaddr + UART_IIR);
924 (void) inb(info->ioaddr + UART_MSR);
925
926 if (info->tty)
927 clear_bit(TTY_IO_ERROR, &info->tty->flags);
928 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
929
930 /*
931 * and set the speed of the serial port
932 */
933 spin_unlock_irqrestore(&info->slock, flags);
934 mxser_change_speed(info, NULL);
935
936 info->flags |= ASYNC_INITIALIZED;
937 return 0;
938}
939
940/*
941 * This routine will shutdown a serial port; interrupts maybe disabled, and
942 * DTR is dropped if the hangup on close termio flag is on.
943 */
944static void mxser_shutdown(struct mxser_port *info)
945{
946 unsigned long flags;
947
948 if (!(info->flags & ASYNC_INITIALIZED))
949 return;
950
951 spin_lock_irqsave(&info->slock, flags);
952
953 /*
954 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
955 * here so the queue might never be waken up
956 */
957 wake_up_interruptible(&info->delta_msr_wait);
958
959 /*
960 * Free the IRQ, if necessary
961 */
962 if (info->xmit_buf) {
963 free_page((unsigned long) info->xmit_buf);
964 info->xmit_buf = NULL;
965 }
966
967 info->IER = 0;
968 outb(0x00, info->ioaddr + UART_IER);
969
970 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
971 info->MCR &= ~(UART_MCR_DTR | UART_MCR_RTS);
972 outb(info->MCR, info->ioaddr + UART_MCR);
973
974 /* clear Rx/Tx FIFO's */
975 /* following add by Victor Yu. 08-30-2002 */
976 if (info->board->chip_flag)
977 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT |
978 MOXA_MUST_FCR_GDA_MODE_ENABLE,
979 info->ioaddr + UART_FCR);
980 else
981 /* above add by Victor Yu. 08-30-2002 */
982 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
983 info->ioaddr + UART_FCR);
984
985 /* read data port to reset things */
986 (void) inb(info->ioaddr + UART_RX);
987
988 if (info->tty)
989 set_bit(TTY_IO_ERROR, &info->tty->flags);
990
991 info->flags &= ~ASYNC_INITIALIZED;
992
993 /* following add by Victor Yu. 09-23-2002 */
994 if (info->board->chip_flag)
995 SET_MOXA_MUST_NO_SOFTWARE_FLOW_CONTROL(info->ioaddr);
996 /* above add by Victor Yu. 09-23-2002 */
997
998 spin_unlock_irqrestore(&info->slock, flags);
999}
1000
Jiri Slaby037ad482006-12-08 02:38:11 -08001001/*
1002 * This routine is called whenever a serial port is opened. It
1003 * enables interrupts for a serial port, linking in its async structure into
1004 * the IRQ chain. It also performs the serial-specific
1005 * initialization for the tty structure.
1006 */
1007static int mxser_open(struct tty_struct *tty, struct file *filp)
1008{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001009 struct mxser_port *info;
Jiri Slaby037ad482006-12-08 02:38:11 -08001010 int retval, line;
1011
1012 /* initialize driver_data in case something fails */
1013 tty->driver_data = NULL;
1014
1015 line = tty->index;
1016 if (line == MXSER_PORTS)
1017 return 0;
1018 if (line < 0 || line > MXSER_PORTS)
1019 return -ENODEV;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001020 info = &mxser_boards[line / MXSER_PORTS_PER_BOARD].ports[line % MXSER_PORTS_PER_BOARD];
1021 if (!info->ioaddr)
Jiri Slaby037ad482006-12-08 02:38:11 -08001022 return -ENODEV;
1023
1024 tty->driver_data = info;
1025 info->tty = tty;
1026 /*
1027 * Start up serial port
1028 */
Jiri Slaby3306ce32006-12-08 02:38:13 -08001029 info->count++;
Jiri Slaby037ad482006-12-08 02:38:11 -08001030 retval = mxser_startup(info);
1031 if (retval)
1032 return retval;
1033
1034 retval = mxser_block_til_ready(tty, filp, info);
1035 if (retval)
1036 return retval;
1037
Jiri Slaby037ad482006-12-08 02:38:11 -08001038 if ((info->count == 1) && (info->flags & ASYNC_SPLIT_TERMIOS)) {
1039 if (tty->driver->subtype == SERIAL_TYPE_NORMAL)
1040 *tty->termios = info->normal_termios;
1041 else
1042 *tty->termios = info->callout_termios;
1043 mxser_change_speed(info, NULL);
1044 }
1045
Andrew Morton08a4ae42006-12-08 02:38:12 -08001046 info->session = process_session(current);
Jiri Slaby037ad482006-12-08 02:38:11 -08001047 info->pgrp = process_group(current);
1048
1049 /*
1050 status = mxser_get_msr(info->base, 0, info->port);
1051 mxser_check_modem_status(info, status);
1052 */
1053
1054/* unmark here for very high baud rate (ex. 921600 bps) used */
1055 tty->low_latency = 1;
1056 return 0;
1057}
1058
1059/*
1060 * This routine is called when the serial port gets closed. First, we
1061 * wait for the last remaining data to be sent. Then, we unlink its
1062 * async structure from the interrupt chain if necessary, and we free
1063 * that IRQ if nothing is left in the chain.
1064 */
1065static void mxser_close(struct tty_struct *tty, struct file *filp)
1066{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001067 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001068
1069 unsigned long timeout;
1070 unsigned long flags;
1071 struct tty_ldisc *ld;
1072
1073 if (tty->index == MXSER_PORTS)
1074 return;
1075 if (!info)
1076 return;
1077
1078 spin_lock_irqsave(&info->slock, flags);
1079
1080 if (tty_hung_up_p(filp)) {
1081 spin_unlock_irqrestore(&info->slock, flags);
1082 return;
1083 }
1084 if ((tty->count == 1) && (info->count != 1)) {
1085 /*
1086 * Uh, oh. tty->count is 1, which means that the tty
1087 * structure will be freed. Info->count should always
1088 * be one in these conditions. If it's greater than
1089 * one, we've got real problems, since it means the
1090 * serial port won't be shutdown.
1091 */
1092 printk(KERN_ERR "mxser_close: bad serial port count; "
1093 "tty->count is 1, info->count is %d\n", info->count);
1094 info->count = 1;
1095 }
1096 if (--info->count < 0) {
1097 printk(KERN_ERR "mxser_close: bad serial port count for "
Jiri Slaby55b307d2006-12-08 02:38:14 -08001098 "ttys%d: %d\n", tty->index, info->count);
Jiri Slaby037ad482006-12-08 02:38:11 -08001099 info->count = 0;
1100 }
1101 if (info->count) {
1102 spin_unlock_irqrestore(&info->slock, flags);
1103 return;
1104 }
1105 info->flags |= ASYNC_CLOSING;
1106 spin_unlock_irqrestore(&info->slock, flags);
1107 /*
1108 * Save the termios structure, since this port may have
1109 * separate termios for callout and dialin.
1110 */
1111 if (info->flags & ASYNC_NORMAL_ACTIVE)
1112 info->normal_termios = *tty->termios;
1113 /*
1114 * Now we wait for the transmit buffer to clear; and we notify
1115 * the line discipline to only process XON/XOFF characters.
1116 */
1117 tty->closing = 1;
1118 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1119 tty_wait_until_sent(tty, info->closing_wait);
1120 /*
1121 * At this point we stop accepting input. To do this, we
1122 * disable the receive line status interrupts, and tell the
1123 * interrupt driver to stop checking the data ready bit in the
1124 * line status register.
1125 */
1126 info->IER &= ~UART_IER_RLSI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001127 if (info->board->chip_flag)
Jiri Slaby037ad482006-12-08 02:38:11 -08001128 info->IER &= ~MOXA_MUST_RECV_ISR;
1129/* by William
1130 info->read_status_mask &= ~UART_LSR_DR;
1131*/
1132 if (info->flags & ASYNC_INITIALIZED) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08001133 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001134 /*
1135 * Before we drop DTR, make sure the UART transmitter
1136 * has completely drained; this is especially
1137 * important if there is a transmit FIFO!
1138 */
1139 timeout = jiffies + HZ;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001140 while (!(inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001141 schedule_timeout_interruptible(5);
1142 if (time_after(jiffies, timeout))
1143 break;
1144 }
1145 }
1146 mxser_shutdown(info);
1147
1148 if (tty->driver->flush_buffer)
1149 tty->driver->flush_buffer(tty);
1150
1151 ld = tty_ldisc_ref(tty);
1152 if (ld) {
1153 if (ld->flush_buffer)
1154 ld->flush_buffer(tty);
1155 tty_ldisc_deref(ld);
1156 }
1157
1158 tty->closing = 0;
1159 info->event = 0;
1160 info->tty = NULL;
1161 if (info->blocked_open) {
1162 if (info->close_delay)
1163 schedule_timeout_interruptible(info->close_delay);
1164 wake_up_interruptible(&info->open_wait);
1165 }
1166
1167 info->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
1168 wake_up_interruptible(&info->close_wait);
1169
1170}
1171
1172static int mxser_write(struct tty_struct *tty, const unsigned char *buf, int count)
1173{
1174 int c, total = 0;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001175 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001176 unsigned long flags;
1177
1178 if (!info->xmit_buf)
1179 return 0;
1180
1181 while (1) {
1182 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1183 SERIAL_XMIT_SIZE - info->xmit_head));
1184 if (c <= 0)
1185 break;
1186
1187 memcpy(info->xmit_buf + info->xmit_head, buf, c);
1188 spin_lock_irqsave(&info->slock, flags);
1189 info->xmit_head = (info->xmit_head + c) &
1190 (SERIAL_XMIT_SIZE - 1);
1191 info->xmit_cnt += c;
1192 spin_unlock_irqrestore(&info->slock, flags);
1193
1194 buf += c;
1195 count -= c;
1196 total += c;
1197 }
1198
Jiri Slaby3306ce32006-12-08 02:38:13 -08001199 if (info->xmit_cnt && !tty->stopped
1200 /*&& !(info->IER & UART_IER_THRI)*/) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001201 if (!tty->hw_stopped ||
1202 (info->type == PORT_16550A) ||
Jiri Slaby55b307d2006-12-08 02:38:14 -08001203 (info->board->chip_flag)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001204 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby55b307d2006-12-08 02:38:14 -08001205 outb(info->IER & ~UART_IER_THRI, info->ioaddr +
1206 UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001207 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001208 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001209 spin_unlock_irqrestore(&info->slock, flags);
1210 }
1211 }
1212 return total;
1213}
1214
1215static void mxser_put_char(struct tty_struct *tty, unsigned char ch)
1216{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001217 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001218 unsigned long flags;
1219
1220 if (!info->xmit_buf)
1221 return;
1222
1223 if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1)
1224 return;
1225
1226 spin_lock_irqsave(&info->slock, flags);
1227 info->xmit_buf[info->xmit_head++] = ch;
1228 info->xmit_head &= SERIAL_XMIT_SIZE - 1;
1229 info->xmit_cnt++;
1230 spin_unlock_irqrestore(&info->slock, flags);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001231 if (!tty->stopped /*&& !(info->IER & UART_IER_THRI)*/) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001232 if (!tty->hw_stopped ||
1233 (info->type == PORT_16550A) ||
Jiri Slaby55b307d2006-12-08 02:38:14 -08001234 info->board->chip_flag) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001235 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby55b307d2006-12-08 02:38:14 -08001236 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001237 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001238 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001239 spin_unlock_irqrestore(&info->slock, flags);
1240 }
1241 }
1242}
1243
1244
1245static void mxser_flush_chars(struct tty_struct *tty)
1246{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001247 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001248 unsigned long flags;
1249
1250 if (info->xmit_cnt <= 0 ||
1251 tty->stopped ||
1252 !info->xmit_buf ||
1253 (tty->hw_stopped &&
1254 (info->type != PORT_16550A) &&
Jiri Slaby55b307d2006-12-08 02:38:14 -08001255 (!info->board->chip_flag)
Jiri Slaby037ad482006-12-08 02:38:11 -08001256 ))
1257 return;
1258
1259 spin_lock_irqsave(&info->slock, flags);
1260
Jiri Slaby55b307d2006-12-08 02:38:14 -08001261 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001262 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001263 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001264
1265 spin_unlock_irqrestore(&info->slock, flags);
1266}
1267
1268static int mxser_write_room(struct tty_struct *tty)
1269{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001270 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001271 int ret;
1272
1273 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1274 if (ret < 0)
1275 ret = 0;
1276 return ret;
1277}
1278
1279static int mxser_chars_in_buffer(struct tty_struct *tty)
1280{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001281 struct mxser_port *info = tty->driver_data;
Jiri Slaby3306ce32006-12-08 02:38:13 -08001282 int len = info->xmit_cnt;
1283
Jiri Slaby55b307d2006-12-08 02:38:14 -08001284 if (!(inb(info->ioaddr + UART_LSR) & UART_LSR_THRE))
Jiri Slaby3306ce32006-12-08 02:38:13 -08001285 len++;
1286
1287 return len;
Jiri Slaby037ad482006-12-08 02:38:11 -08001288}
1289
1290static void mxser_flush_buffer(struct tty_struct *tty)
1291{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001292 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001293 char fcr;
1294 unsigned long flags;
1295
1296
1297 spin_lock_irqsave(&info->slock, flags);
1298 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1299
1300 /* below added by shinhay */
Jiri Slaby55b307d2006-12-08 02:38:14 -08001301 fcr = inb(info->ioaddr + UART_FCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08001302 outb((fcr | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
Jiri Slaby55b307d2006-12-08 02:38:14 -08001303 info->ioaddr + UART_FCR);
1304 outb(fcr, info->ioaddr + UART_FCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08001305
1306 spin_unlock_irqrestore(&info->slock, flags);
1307 /* above added by shinhay */
1308
1309 wake_up_interruptible(&tty->write_wait);
1310 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) && tty->ldisc.write_wakeup)
1311 (tty->ldisc.write_wakeup) (tty);
1312}
1313
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001314/*
1315 * ------------------------------------------------------------
1316 * friends of mxser_ioctl()
1317 * ------------------------------------------------------------
1318 */
1319static int mxser_get_serial_info(struct mxser_port *info,
1320 struct serial_struct __user *retinfo)
1321{
1322 struct serial_struct tmp;
1323
1324 if (!retinfo)
1325 return -EFAULT;
1326 memset(&tmp, 0, sizeof(tmp));
1327 tmp.type = info->type;
1328 tmp.line = info->tty->index;
1329 tmp.port = info->ioaddr;
1330 tmp.irq = info->board->irq;
1331 tmp.flags = info->flags;
1332 tmp.baud_base = info->baud_base;
1333 tmp.close_delay = info->close_delay;
1334 tmp.closing_wait = info->closing_wait;
1335 tmp.custom_divisor = info->custom_divisor;
1336 tmp.hub6 = 0;
1337 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1338 return -EFAULT;
1339 return 0;
1340}
1341
1342static int mxser_set_serial_info(struct mxser_port *info,
1343 struct serial_struct __user *new_info)
1344{
1345 struct serial_struct new_serial;
1346 unsigned int flags;
1347 int retval = 0;
1348
1349 if (!new_info || !info->ioaddr)
1350 return -EFAULT;
1351 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
1352 return -EFAULT;
1353
1354 if ((new_serial.irq != info->board->irq) ||
1355 (new_serial.port != info->ioaddr) ||
1356 (new_serial.custom_divisor != info->custom_divisor) ||
1357 (new_serial.baud_base != info->baud_base))
1358 return -EPERM;
1359
1360 flags = info->flags & ASYNC_SPD_MASK;
1361
1362 if (!capable(CAP_SYS_ADMIN)) {
1363 if ((new_serial.baud_base != info->baud_base) ||
1364 (new_serial.close_delay != info->close_delay) ||
1365 ((new_serial.flags & ~ASYNC_USR_MASK) != (info->flags & ~ASYNC_USR_MASK)))
1366 return -EPERM;
1367 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
1368 (new_serial.flags & ASYNC_USR_MASK));
1369 } else {
1370 /*
1371 * OK, past this point, all the error checking has been done.
1372 * At this point, we start making changes.....
1373 */
1374 info->flags = ((info->flags & ~ASYNC_FLAGS) |
1375 (new_serial.flags & ASYNC_FLAGS));
1376 info->close_delay = new_serial.close_delay * HZ / 100;
1377 info->closing_wait = new_serial.closing_wait * HZ / 100;
1378 info->tty->low_latency =
1379 (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1380 info->tty->low_latency = 0; /* (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0; */
1381 }
1382
1383 /* added by casper, 3/17/2000, for mouse */
1384 info->type = new_serial.type;
1385
1386 process_txrx_fifo(info);
1387
1388 if (info->flags & ASYNC_INITIALIZED) {
1389 if (flags != (info->flags & ASYNC_SPD_MASK))
1390 mxser_change_speed(info, NULL);
1391 } else
1392 retval = mxser_startup(info);
1393
1394 return retval;
1395}
1396
1397/*
1398 * mxser_get_lsr_info - get line status register info
1399 *
1400 * Purpose: Let user call ioctl() to get info when the UART physically
1401 * is emptied. On bus types like RS485, the transmitter must
1402 * release the bus after transmitting. This must be done when
1403 * the transmit shift register is empty, not be done when the
1404 * transmit holding register is empty. This functionality
1405 * allows an RS485 driver to be written in user space.
1406 */
1407static int mxser_get_lsr_info(struct mxser_port *info,
1408 unsigned int __user *value)
1409{
1410 unsigned char status;
1411 unsigned int result;
1412 unsigned long flags;
1413
1414 spin_lock_irqsave(&info->slock, flags);
1415 status = inb(info->ioaddr + UART_LSR);
1416 spin_unlock_irqrestore(&info->slock, flags);
1417 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1418 return put_user(result, value);
1419}
1420
1421/*
1422 * This routine sends a break character out the serial port.
1423 */
1424static void mxser_send_break(struct mxser_port *info, int duration)
1425{
1426 unsigned long flags;
1427
1428 if (!info->ioaddr)
1429 return;
1430 set_current_state(TASK_INTERRUPTIBLE);
1431 spin_lock_irqsave(&info->slock, flags);
1432 outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
1433 info->ioaddr + UART_LCR);
1434 spin_unlock_irqrestore(&info->slock, flags);
1435 schedule_timeout(duration);
1436 spin_lock_irqsave(&info->slock, flags);
1437 outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
1438 info->ioaddr + UART_LCR);
1439 spin_unlock_irqrestore(&info->slock, flags);
1440}
1441
1442static int mxser_tiocmget(struct tty_struct *tty, struct file *file)
Jiri Slaby037ad482006-12-08 02:38:11 -08001443{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001444 struct mxser_port *info = tty->driver_data;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001445 unsigned char control, status;
Jiri Slaby037ad482006-12-08 02:38:11 -08001446 unsigned long flags;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001447
Jiri Slaby037ad482006-12-08 02:38:11 -08001448
1449 if (tty->index == MXSER_PORTS)
Jiri Slaby037ad482006-12-08 02:38:11 -08001450 return -ENOIOCTLCMD;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001451 if (tty->flags & (1 << TTY_IO_ERROR))
1452 return -EIO;
1453
1454 control = info->MCR;
1455
1456 spin_lock_irqsave(&info->slock, flags);
1457 status = inb(info->ioaddr + UART_MSR);
1458 if (status & UART_MSR_ANY_DELTA)
1459 mxser_check_modem_status(info, status);
1460 spin_unlock_irqrestore(&info->slock, flags);
1461 return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0) |
1462 ((control & UART_MCR_DTR) ? TIOCM_DTR : 0) |
1463 ((status & UART_MSR_DCD) ? TIOCM_CAR : 0) |
1464 ((status & UART_MSR_RI) ? TIOCM_RNG : 0) |
1465 ((status & UART_MSR_DSR) ? TIOCM_DSR : 0) |
1466 ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
1467}
1468
1469static int mxser_tiocmset(struct tty_struct *tty, struct file *file,
1470 unsigned int set, unsigned int clear)
1471{
1472 struct mxser_port *info = tty->driver_data;
1473 unsigned long flags;
1474
1475
1476 if (tty->index == MXSER_PORTS)
1477 return -ENOIOCTLCMD;
1478 if (tty->flags & (1 << TTY_IO_ERROR))
1479 return -EIO;
1480
1481 spin_lock_irqsave(&info->slock, flags);
1482
1483 if (set & TIOCM_RTS)
1484 info->MCR |= UART_MCR_RTS;
1485 if (set & TIOCM_DTR)
1486 info->MCR |= UART_MCR_DTR;
1487
1488 if (clear & TIOCM_RTS)
1489 info->MCR &= ~UART_MCR_RTS;
1490 if (clear & TIOCM_DTR)
1491 info->MCR &= ~UART_MCR_DTR;
1492
1493 outb(info->MCR, info->ioaddr + UART_MCR);
1494 spin_unlock_irqrestore(&info->slock, flags);
Jiri Slaby037ad482006-12-08 02:38:11 -08001495 return 0;
1496}
1497
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001498static int mxser_program_mode(int port)
1499{
1500 int id, i, j, n;
1501 /* unsigned long flags; */
1502
1503 spin_lock(&gm_lock);
1504 outb(0, port);
1505 outb(0, port);
1506 outb(0, port);
1507 (void)inb(port);
1508 (void)inb(port);
1509 outb(0, port);
1510 (void)inb(port);
1511 /* restore_flags(flags); */
1512 spin_unlock(&gm_lock);
1513
1514 id = inb(port + 1) & 0x1F;
1515 if ((id != C168_ASIC_ID) &&
1516 (id != C104_ASIC_ID) &&
1517 (id != C102_ASIC_ID) &&
1518 (id != CI132_ASIC_ID) &&
1519 (id != CI134_ASIC_ID) &&
1520 (id != CI104J_ASIC_ID))
1521 return -1;
1522 for (i = 0, j = 0; i < 4; i++) {
1523 n = inb(port + 2);
1524 if (n == 'M') {
1525 j = 1;
1526 } else if ((j == 1) && (n == 1)) {
1527 j = 2;
1528 break;
1529 } else
1530 j = 0;
1531 }
1532 if (j != 2)
1533 id = -2;
1534 return id;
1535}
1536
1537static void mxser_normal_mode(int port)
1538{
1539 int i, n;
1540
1541 outb(0xA5, port + 1);
1542 outb(0x80, port + 3);
1543 outb(12, port + 0); /* 9600 bps */
1544 outb(0, port + 1);
1545 outb(0x03, port + 3); /* 8 data bits */
1546 outb(0x13, port + 4); /* loop back mode */
1547 for (i = 0; i < 16; i++) {
1548 n = inb(port + 5);
1549 if ((n & 0x61) == 0x60)
1550 break;
1551 if ((n & 1) == 1)
1552 (void)inb(port);
1553 }
1554 outb(0x00, port + 4);
1555}
1556
1557#define CHIP_SK 0x01 /* Serial Data Clock in Eprom */
1558#define CHIP_DO 0x02 /* Serial Data Output in Eprom */
1559#define CHIP_CS 0x04 /* Serial Chip Select in Eprom */
1560#define CHIP_DI 0x08 /* Serial Data Input in Eprom */
1561#define EN_CCMD 0x000 /* Chip's command register */
1562#define EN0_RSARLO 0x008 /* Remote start address reg 0 */
1563#define EN0_RSARHI 0x009 /* Remote start address reg 1 */
1564#define EN0_RCNTLO 0x00A /* Remote byte count reg WR */
1565#define EN0_RCNTHI 0x00B /* Remote byte count reg WR */
1566#define EN0_DCFG 0x00E /* Data configuration reg WR */
1567#define EN0_PORT 0x010 /* Rcv missed frame error counter RD */
1568#define ENC_PAGE0 0x000 /* Select page 0 of chip registers */
1569#define ENC_PAGE3 0x0C0 /* Select page 3 of chip registers */
1570static int mxser_read_register(int port, unsigned short *regs)
1571{
1572 int i, k, value, id;
1573 unsigned int j;
1574
1575 id = mxser_program_mode(port);
1576 if (id < 0)
1577 return id;
1578 for (i = 0; i < 14; i++) {
1579 k = (i & 0x3F) | 0x180;
1580 for (j = 0x100; j > 0; j >>= 1) {
1581 outb(CHIP_CS, port);
1582 if (k & j) {
1583 outb(CHIP_CS | CHIP_DO, port);
1584 outb(CHIP_CS | CHIP_DO | CHIP_SK, port); /* A? bit of read */
1585 } else {
1586 outb(CHIP_CS, port);
1587 outb(CHIP_CS | CHIP_SK, port); /* A? bit of read */
1588 }
1589 }
1590 (void)inb(port);
1591 value = 0;
1592 for (k = 0, j = 0x8000; k < 16; k++, j >>= 1) {
1593 outb(CHIP_CS, port);
1594 outb(CHIP_CS | CHIP_SK, port);
1595 if (inb(port) & CHIP_DI)
1596 value |= j;
1597 }
1598 regs[i] = value;
1599 outb(0, port);
1600 }
1601 mxser_normal_mode(port);
1602 return id;
1603}
1604
Jiri Slaby037ad482006-12-08 02:38:11 -08001605#ifndef CMSPAR
1606#define CMSPAR 010000000000
1607#endif
1608
1609static int mxser_ioctl_special(unsigned int cmd, void __user *argp)
1610{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001611 struct mxser_port *port;
1612 int result, status;
1613 unsigned int i, j;
Jiri Slaby037ad482006-12-08 02:38:11 -08001614
1615 switch (cmd) {
1616 case MOXA_GET_CONF:
Jiri Slaby55b307d2006-12-08 02:38:14 -08001617/* if (copy_to_user(argp, mxsercfg,
Jiri Slaby037ad482006-12-08 02:38:11 -08001618 sizeof(struct mxser_hwconf) * 4))
1619 return -EFAULT;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001620 return 0;*/
1621 return -ENXIO;
Jiri Slaby037ad482006-12-08 02:38:11 -08001622 case MOXA_GET_MAJOR:
1623 if (copy_to_user(argp, &ttymajor, sizeof(int)))
1624 return -EFAULT;
1625 return 0;
1626
1627 case MOXA_GET_CUMAJOR:
1628 if (copy_to_user(argp, &calloutmajor, sizeof(int)))
1629 return -EFAULT;
1630 return 0;
1631
1632 case MOXA_CHKPORTENABLE:
1633 result = 0;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001634
1635 for (i = 0; i < MXSER_BOARDS; i++)
1636 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++)
1637 if (mxser_boards[i].ports[j].ioaddr)
1638 result |= (1 << i);
1639
Jiri Slaby037ad482006-12-08 02:38:11 -08001640 return put_user(result, (unsigned long __user *)argp);
1641 case MOXA_GETDATACOUNT:
1642 if (copy_to_user(argp, &mxvar_log, sizeof(mxvar_log)))
1643 return -EFAULT;
1644 return 0;
1645 case MOXA_GETMSTATUS:
Jiri Slaby55b307d2006-12-08 02:38:14 -08001646 for (i = 0; i < MXSER_BOARDS; i++)
1647 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
1648 port = &mxser_boards[i].ports[j];
1649
1650 GMStatus[i].ri = 0;
1651 if (!port->ioaddr) {
1652 GMStatus[i].dcd = 0;
1653 GMStatus[i].dsr = 0;
1654 GMStatus[i].cts = 0;
1655 continue;
1656 }
1657
1658 if (!port->tty || !port->tty->termios)
1659 GMStatus[i].cflag =
1660 port->normal_termios.c_cflag;
1661 else
1662 GMStatus[i].cflag =
1663 port->tty->termios->c_cflag;
1664
1665 status = inb(port->ioaddr + UART_MSR);
1666 if (status & 0x80 /*UART_MSR_DCD */ )
1667 GMStatus[i].dcd = 1;
1668 else
1669 GMStatus[i].dcd = 0;
1670
1671 if (status & 0x20 /*UART_MSR_DSR */ )
1672 GMStatus[i].dsr = 1;
1673 else
1674 GMStatus[i].dsr = 0;
1675
1676
1677 if (status & 0x10 /*UART_MSR_CTS */ )
1678 GMStatus[i].cts = 1;
1679 else
1680 GMStatus[i].cts = 0;
Jiri Slaby037ad482006-12-08 02:38:11 -08001681 }
Jiri Slaby037ad482006-12-08 02:38:11 -08001682 if (copy_to_user(argp, GMStatus,
1683 sizeof(struct mxser_mstatus) * MXSER_PORTS))
1684 return -EFAULT;
1685 return 0;
1686 case MOXA_ASPP_MON_EXT: {
Jiri Slaby55b307d2006-12-08 02:38:14 -08001687 int status, p, shiftbit;
1688 unsigned long opmode;
1689 unsigned cflag, iflag;
Jiri Slaby037ad482006-12-08 02:38:11 -08001690
Jiri Slaby55b307d2006-12-08 02:38:14 -08001691 for (i = 0; i < MXSER_BOARDS; i++)
1692 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
1693 port = &mxser_boards[i].ports[j];
1694 if (!port->ioaddr)
Jiri Slaby037ad482006-12-08 02:38:11 -08001695 continue;
1696
Jiri Slaby55b307d2006-12-08 02:38:14 -08001697 status = mxser_get_msr(port->ioaddr, 0, i);
1698/* mxser_check_modem_status(port, status); */
1699
Jiri Slaby037ad482006-12-08 02:38:11 -08001700 if (status & UART_MSR_TERI)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001701 port->icount.rng++;
Jiri Slaby037ad482006-12-08 02:38:11 -08001702 if (status & UART_MSR_DDSR)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001703 port->icount.dsr++;
Jiri Slaby037ad482006-12-08 02:38:11 -08001704 if (status & UART_MSR_DDCD)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001705 port->icount.dcd++;
Jiri Slaby037ad482006-12-08 02:38:11 -08001706 if (status & UART_MSR_DCTS)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001707 port->icount.cts++;
Jiri Slaby037ad482006-12-08 02:38:11 -08001708
Jiri Slaby55b307d2006-12-08 02:38:14 -08001709 port->mon_data.modem_status = status;
1710 mon_data_ext.rx_cnt[i] = port->mon_data.rxcnt;
1711 mon_data_ext.tx_cnt[i] = port->mon_data.txcnt;
1712 mon_data_ext.up_rxcnt[i] =
1713 port->mon_data.up_rxcnt;
1714 mon_data_ext.up_txcnt[i] =
1715 port->mon_data.up_txcnt;
1716 mon_data_ext.modem_status[i] =
1717 port->mon_data.modem_status;
1718 mon_data_ext.baudrate[i] = port->realbaud;
Jiri Slaby037ad482006-12-08 02:38:11 -08001719
Jiri Slaby55b307d2006-12-08 02:38:14 -08001720 if (!port->tty || !port->tty->termios) {
1721 cflag = port->normal_termios.c_cflag;
1722 iflag = port->normal_termios.c_iflag;
Jiri Slaby037ad482006-12-08 02:38:11 -08001723 } else {
Jiri Slaby55b307d2006-12-08 02:38:14 -08001724 cflag = port->tty->termios->c_cflag;
1725 iflag = port->tty->termios->c_iflag;
Jiri Slaby037ad482006-12-08 02:38:11 -08001726 }
1727
1728 mon_data_ext.databits[i] = cflag & CSIZE;
1729
1730 mon_data_ext.stopbits[i] = cflag & CSTOPB;
1731
Jiri Slaby55b307d2006-12-08 02:38:14 -08001732 mon_data_ext.parity[i] =
1733 cflag & (PARENB | PARODD | CMSPAR);
Jiri Slaby037ad482006-12-08 02:38:11 -08001734
1735 mon_data_ext.flowctrl[i] = 0x00;
1736
1737 if (cflag & CRTSCTS)
1738 mon_data_ext.flowctrl[i] |= 0x03;
1739
1740 if (iflag & (IXON | IXOFF))
1741 mon_data_ext.flowctrl[i] |= 0x0C;
1742
Jiri Slaby55b307d2006-12-08 02:38:14 -08001743 if (port->type == PORT_16550A)
Jiri Slaby037ad482006-12-08 02:38:11 -08001744 mon_data_ext.fifo[i] = 1;
1745 else
1746 mon_data_ext.fifo[i] = 0;
1747
1748 p = i % 4;
1749 shiftbit = p * 2;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001750 opmode = inb(port->opmode_ioaddr) >> shiftbit;
Jiri Slaby037ad482006-12-08 02:38:11 -08001751 opmode &= OP_MODE_MASK;
1752
1753 mon_data_ext.iftype[i] = opmode;
1754
1755 }
Jiri Slaby55b307d2006-12-08 02:38:14 -08001756 if (copy_to_user(argp, &mon_data_ext,
1757 sizeof(mon_data_ext)))
Jiri Slaby037ad482006-12-08 02:38:11 -08001758 return -EFAULT;
1759
1760 return 0;
1761
Jiri Slaby55b307d2006-12-08 02:38:14 -08001762 } default:
Jiri Slaby037ad482006-12-08 02:38:11 -08001763 return -ENOIOCTLCMD;
1764 }
1765 return 0;
1766}
1767
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001768static int mxser_ioctl(struct tty_struct *tty, struct file *file,
1769 unsigned int cmd, unsigned long arg)
1770{
1771 struct mxser_port *info = tty->driver_data;
1772 struct async_icount cprev, cnow; /* kernel counter temps */
1773 struct serial_icounter_struct __user *p_cuser;
1774 unsigned long templ;
1775 unsigned long flags;
1776 void __user *argp = (void __user *)arg;
1777 int retval;
1778
1779 if (tty->index == MXSER_PORTS)
1780 return mxser_ioctl_special(cmd, argp);
1781
1782 /* following add by Victor Yu. 01-05-2004 */
1783 if (cmd == MOXA_SET_OP_MODE || cmd == MOXA_GET_OP_MODE) {
1784 int p;
1785 unsigned long opmode;
1786 static unsigned char ModeMask[] = { 0xfc, 0xf3, 0xcf, 0x3f };
1787 int shiftbit;
1788 unsigned char val, mask;
1789
1790 p = tty->index % 4;
1791 if (cmd == MOXA_SET_OP_MODE) {
1792 if (get_user(opmode, (int __user *) argp))
1793 return -EFAULT;
1794 if (opmode != RS232_MODE &&
1795 opmode != RS485_2WIRE_MODE &&
1796 opmode != RS422_MODE &&
1797 opmode != RS485_4WIRE_MODE)
1798 return -EFAULT;
1799 mask = ModeMask[p];
1800 shiftbit = p * 2;
1801 val = inb(info->opmode_ioaddr);
1802 val &= mask;
1803 val |= (opmode << shiftbit);
1804 outb(val, info->opmode_ioaddr);
1805 } else {
1806 shiftbit = p * 2;
1807 opmode = inb(info->opmode_ioaddr) >> shiftbit;
1808 opmode &= OP_MODE_MASK;
1809 if (copy_to_user(argp, &opmode, sizeof(int)))
1810 return -EFAULT;
1811 }
1812 return 0;
1813 }
1814 /* above add by Victor Yu. 01-05-2004 */
1815
1816 if ((cmd != TIOCGSERIAL) && (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
1817 if (tty->flags & (1 << TTY_IO_ERROR))
1818 return -EIO;
1819 }
1820 switch (cmd) {
1821 case TCSBRK: /* SVID version: non-zero arg --> no break */
1822 retval = tty_check_change(tty);
1823 if (retval)
1824 return retval;
1825 tty_wait_until_sent(tty, 0);
1826 if (!arg)
1827 mxser_send_break(info, HZ / 4); /* 1/4 second */
1828 return 0;
1829 case TCSBRKP: /* support for POSIX tcsendbreak() */
1830 retval = tty_check_change(tty);
1831 if (retval)
1832 return retval;
1833 tty_wait_until_sent(tty, 0);
1834 mxser_send_break(info, arg ? arg * (HZ / 10) : HZ / 4);
1835 return 0;
1836 case TIOCGSOFTCAR:
1837 return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *)argp);
1838 case TIOCSSOFTCAR:
1839 if (get_user(templ, (unsigned long __user *) argp))
1840 return -EFAULT;
1841 arg = templ;
1842 tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) | (arg ? CLOCAL : 0));
1843 return 0;
1844 case TIOCGSERIAL:
1845 return mxser_get_serial_info(info, argp);
1846 case TIOCSSERIAL:
1847 return mxser_set_serial_info(info, argp);
1848 case TIOCSERGETLSR: /* Get line status register */
1849 return mxser_get_lsr_info(info, argp);
1850 /*
1851 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1852 * - mask passed in arg for lines of interest
1853 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1854 * Caller should use TIOCGICOUNT to see which one it was
1855 */
1856 case TIOCMIWAIT: {
1857 DECLARE_WAITQUEUE(wait, current);
1858 int ret;
1859 spin_lock_irqsave(&info->slock, flags);
1860 cprev = info->icount; /* note the counters on entry */
1861 spin_unlock_irqrestore(&info->slock, flags);
1862
1863 add_wait_queue(&info->delta_msr_wait, &wait);
1864 while (1) {
1865 spin_lock_irqsave(&info->slock, flags);
1866 cnow = info->icount; /* atomic copy */
1867 spin_unlock_irqrestore(&info->slock, flags);
1868
1869 set_current_state(TASK_INTERRUPTIBLE);
1870 if (((arg & TIOCM_RNG) &&
1871 (cnow.rng != cprev.rng)) ||
1872 ((arg & TIOCM_DSR) &&
1873 (cnow.dsr != cprev.dsr)) ||
1874 ((arg & TIOCM_CD) &&
1875 (cnow.dcd != cprev.dcd)) ||
1876 ((arg & TIOCM_CTS) &&
1877 (cnow.cts != cprev.cts))) {
1878 ret = 0;
1879 break;
1880 }
1881 /* see if a signal did it */
1882 if (signal_pending(current)) {
1883 ret = -ERESTARTSYS;
1884 break;
1885 }
1886 cprev = cnow;
1887 }
1888 current->state = TASK_RUNNING;
1889 remove_wait_queue(&info->delta_msr_wait, &wait);
1890 break;
1891 }
1892 /* NOTREACHED */
1893 /*
1894 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1895 * Return: write counters to the user passed counter struct
1896 * NB: both 1->0 and 0->1 transitions are counted except for
1897 * RI where only 0->1 is counted.
1898 */
1899 case TIOCGICOUNT:
1900 spin_lock_irqsave(&info->slock, flags);
1901 cnow = info->icount;
1902 spin_unlock_irqrestore(&info->slock, flags);
1903 p_cuser = argp;
1904 /* modified by casper 1/11/2000 */
1905 if (put_user(cnow.frame, &p_cuser->frame))
1906 return -EFAULT;
1907 if (put_user(cnow.brk, &p_cuser->brk))
1908 return -EFAULT;
1909 if (put_user(cnow.overrun, &p_cuser->overrun))
1910 return -EFAULT;
1911 if (put_user(cnow.buf_overrun, &p_cuser->buf_overrun))
1912 return -EFAULT;
1913 if (put_user(cnow.parity, &p_cuser->parity))
1914 return -EFAULT;
1915 if (put_user(cnow.rx, &p_cuser->rx))
1916 return -EFAULT;
1917 if (put_user(cnow.tx, &p_cuser->tx))
1918 return -EFAULT;
1919 put_user(cnow.cts, &p_cuser->cts);
1920 put_user(cnow.dsr, &p_cuser->dsr);
1921 put_user(cnow.rng, &p_cuser->rng);
1922 put_user(cnow.dcd, &p_cuser->dcd);
1923 return 0;
1924 case MOXA_HighSpeedOn:
1925 return put_user(info->baud_base != 115200 ? 1 : 0, (int __user *)argp);
1926 case MOXA_SDS_RSTICOUNTER:
1927 info->mon_data.rxcnt = 0;
1928 info->mon_data.txcnt = 0;
1929 return 0;
1930/* (above) added by James. */
1931 case MOXA_ASPP_SETBAUD:{
1932 long baud;
1933 if (get_user(baud, (long __user *)argp))
1934 return -EFAULT;
1935 mxser_set_baud(info, baud);
1936 return 0;
1937 }
1938 case MOXA_ASPP_GETBAUD:
1939 if (copy_to_user(argp, &info->realbaud, sizeof(long)))
1940 return -EFAULT;
1941
1942 return 0;
1943
1944 case MOXA_ASPP_OQUEUE:{
1945 int len, lsr;
1946
1947 len = mxser_chars_in_buffer(tty);
1948
1949 lsr = inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT;
1950
1951 len += (lsr ? 0 : 1);
1952
1953 if (copy_to_user(argp, &len, sizeof(int)))
1954 return -EFAULT;
1955
1956 return 0;
1957 }
1958 case MOXA_ASPP_MON: {
1959 int mcr, status;
1960
1961 /* info->mon_data.ser_param = tty->termios->c_cflag; */
1962
1963 status = mxser_get_msr(info->ioaddr, 1, tty->index);
1964 mxser_check_modem_status(info, status);
1965
1966 mcr = inb(info->ioaddr + UART_MCR);
1967 if (mcr & MOXA_MUST_MCR_XON_FLAG)
1968 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFHOLD;
1969 else
1970 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFHOLD;
1971
1972 if (mcr & MOXA_MUST_MCR_TX_XON)
1973 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFXENT;
1974 else
1975 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFXENT;
1976
1977 if (info->tty->hw_stopped)
1978 info->mon_data.hold_reason |= NPPI_NOTIFY_CTSHOLD;
1979 else
1980 info->mon_data.hold_reason &= ~NPPI_NOTIFY_CTSHOLD;
1981
1982 if (copy_to_user(argp, &info->mon_data,
1983 sizeof(struct mxser_mon)))
1984 return -EFAULT;
1985
1986 return 0;
1987 }
1988 case MOXA_ASPP_LSTATUS: {
1989 if (copy_to_user(argp, &info->err_shadow,
1990 sizeof(unsigned char)))
1991 return -EFAULT;
1992
1993 info->err_shadow = 0;
1994 return 0;
1995 }
1996 case MOXA_SET_BAUD_METHOD: {
1997 int method;
1998
1999 if (get_user(method, (int __user *)argp))
2000 return -EFAULT;
2001 mxser_set_baud_method[tty->index] = method;
2002 if (copy_to_user(argp, &method, sizeof(int)))
2003 return -EFAULT;
2004
2005 return 0;
2006 }
2007 default:
2008 return -ENOIOCTLCMD;
2009 }
2010 return 0;
2011}
2012
Jiri Slaby037ad482006-12-08 02:38:11 -08002013static void mxser_stoprx(struct tty_struct *tty)
2014{
Jiri Slaby55b307d2006-12-08 02:38:14 -08002015 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08002016
2017 info->ldisc_stop_rx = 1;
2018 if (I_IXOFF(tty)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002019 /* following add by Victor Yu. 09-02-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002020 if (info->board->chip_flag) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002021 info->IER &= ~MOXA_MUST_RECV_ISR;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002022 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby3306ce32006-12-08 02:38:13 -08002023 } else if (!(info->flags & ASYNC_CLOSING)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002024 info->x_char = STOP_CHAR(tty);
Jiri Slaby55b307d2006-12-08 02:38:14 -08002025 outb(0, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08002026 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002027 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby3306ce32006-12-08 02:38:13 -08002028 }
Jiri Slaby037ad482006-12-08 02:38:11 -08002029 }
2030
2031 if (info->tty->termios->c_cflag & CRTSCTS) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002032 info->MCR &= ~UART_MCR_RTS;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002033 outb(info->MCR, info->ioaddr + UART_MCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002034 }
2035}
2036
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002037/*
2038 * This routine is called by the upper-layer tty layer to signal that
2039 * incoming characters should be throttled.
2040 */
2041static void mxser_throttle(struct tty_struct *tty)
2042{
2043 mxser_stoprx(tty);
2044}
2045
2046static void mxser_unthrottle(struct tty_struct *tty)
Jiri Slaby037ad482006-12-08 02:38:11 -08002047{
Jiri Slaby55b307d2006-12-08 02:38:14 -08002048 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08002049
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002050 /* startrx */
Jiri Slaby037ad482006-12-08 02:38:11 -08002051 info->ldisc_stop_rx = 0;
2052 if (I_IXOFF(tty)) {
2053 if (info->x_char)
2054 info->x_char = 0;
2055 else {
Jiri Slaby037ad482006-12-08 02:38:11 -08002056 /* following add by Victor Yu. 09-02-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002057 if (info->board->chip_flag) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002058 info->IER |= MOXA_MUST_RECV_ISR;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002059 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby3306ce32006-12-08 02:38:13 -08002060 } else if (!(info->flags & ASYNC_CLOSING)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002061 info->x_char = START_CHAR(tty);
Jiri Slaby55b307d2006-12-08 02:38:14 -08002062 outb(0, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08002063 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002064 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby3306ce32006-12-08 02:38:13 -08002065 }
Jiri Slaby037ad482006-12-08 02:38:11 -08002066 }
2067 }
2068
2069 if (info->tty->termios->c_cflag & CRTSCTS) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002070 info->MCR |= UART_MCR_RTS;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002071 outb(info->MCR, info->ioaddr + UART_MCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002072 }
2073}
2074
2075/*
Jiri Slaby037ad482006-12-08 02:38:11 -08002076 * mxser_stop() and mxser_start()
2077 *
2078 * This routines are called before setting or resetting tty->stopped.
2079 * They enable or disable transmitter interrupts, as necessary.
2080 */
2081static void mxser_stop(struct tty_struct *tty)
2082{
Jiri Slaby55b307d2006-12-08 02:38:14 -08002083 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08002084 unsigned long flags;
2085
2086 spin_lock_irqsave(&info->slock, flags);
2087 if (info->IER & UART_IER_THRI) {
2088 info->IER &= ~UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002089 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08002090 }
2091 spin_unlock_irqrestore(&info->slock, flags);
2092}
2093
2094static void mxser_start(struct tty_struct *tty)
2095{
Jiri Slaby55b307d2006-12-08 02:38:14 -08002096 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08002097 unsigned long flags;
2098
2099 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby3306ce32006-12-08 02:38:13 -08002100 if (info->xmit_cnt && info->xmit_buf
2101 /* && !(info->IER & UART_IER_THRI) */) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002102 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08002103 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002104 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08002105 }
2106 spin_unlock_irqrestore(&info->slock, flags);
2107}
2108
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002109static void mxser_set_termios(struct tty_struct *tty, struct termios *old_termios)
2110{
2111 struct mxser_port *info = tty->driver_data;
2112 unsigned long flags;
2113
2114 if ((tty->termios->c_cflag != old_termios->c_cflag) ||
2115 (RELEVANT_IFLAG(tty->termios->c_iflag) != RELEVANT_IFLAG(old_termios->c_iflag))) {
2116
2117 mxser_change_speed(info, old_termios);
2118
2119 if ((old_termios->c_cflag & CRTSCTS) &&
2120 !(tty->termios->c_cflag & CRTSCTS)) {
2121 tty->hw_stopped = 0;
2122 mxser_start(tty);
2123 }
2124 }
2125
2126/* Handle sw stopped */
2127 if ((old_termios->c_iflag & IXON) &&
2128 !(tty->termios->c_iflag & IXON)) {
2129 tty->stopped = 0;
2130
2131 /* following add by Victor Yu. 09-02-2002 */
2132 if (info->board->chip_flag) {
2133 spin_lock_irqsave(&info->slock, flags);
2134 DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
2135 spin_unlock_irqrestore(&info->slock, flags);
2136 }
2137 /* above add by Victor Yu. 09-02-2002 */
2138
2139 mxser_start(tty);
2140 }
2141}
2142
Jiri Slaby037ad482006-12-08 02:38:11 -08002143/*
2144 * mxser_wait_until_sent() --- wait until the transmitter is empty
2145 */
2146static void mxser_wait_until_sent(struct tty_struct *tty, int timeout)
2147{
Jiri Slaby55b307d2006-12-08 02:38:14 -08002148 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08002149 unsigned long orig_jiffies, char_time;
2150 int lsr;
2151
2152 if (info->type == PORT_UNKNOWN)
2153 return;
2154
2155 if (info->xmit_fifo_size == 0)
2156 return; /* Just in case.... */
2157
2158 orig_jiffies = jiffies;
2159 /*
2160 * Set the check interval to be 1/5 of the estimated time to
2161 * send a single character, and make it at least 1. The check
2162 * interval should also be less than the timeout.
2163 *
2164 * Note: we have to use pretty tight timings here to satisfy
2165 * the NIST-PCTS.
2166 */
2167 char_time = (info->timeout - HZ / 50) / info->xmit_fifo_size;
2168 char_time = char_time / 5;
2169 if (char_time == 0)
2170 char_time = 1;
2171 if (timeout && timeout < char_time)
2172 char_time = timeout;
2173 /*
2174 * If the transmitter hasn't cleared in twice the approximate
2175 * amount of time to send the entire FIFO, it probably won't
2176 * ever clear. This assumes the UART isn't doing flow
2177 * control, which is currently the case. Hence, if it ever
2178 * takes longer than info->timeout, this is probably due to a
2179 * UART bug of some kind. So, we clamp the timeout parameter at
2180 * 2*info->timeout.
2181 */
2182 if (!timeout || timeout > 2 * info->timeout)
2183 timeout = 2 * info->timeout;
2184#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2185 printk(KERN_DEBUG "In rs_wait_until_sent(%d) check=%lu...",
2186 timeout, char_time);
2187 printk("jiff=%lu...", jiffies);
2188#endif
Jiri Slaby55b307d2006-12-08 02:38:14 -08002189 while (!((lsr = inb(info->ioaddr + UART_LSR)) & UART_LSR_TEMT)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002190#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2191 printk("lsr = %d (jiff=%lu)...", lsr, jiffies);
2192#endif
2193 schedule_timeout_interruptible(char_time);
2194 if (signal_pending(current))
2195 break;
2196 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2197 break;
2198 }
2199 set_current_state(TASK_RUNNING);
2200
2201#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2202 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
2203#endif
2204}
2205
2206
2207/*
2208 * This routine is called by tty_hangup() when a hangup is signaled.
2209 */
2210void mxser_hangup(struct tty_struct *tty)
2211{
Jiri Slaby55b307d2006-12-08 02:38:14 -08002212 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08002213
2214 mxser_flush_buffer(tty);
2215 mxser_shutdown(info);
2216 info->event = 0;
2217 info->count = 0;
2218 info->flags &= ~ASYNC_NORMAL_ACTIVE;
2219 info->tty = NULL;
2220 wake_up_interruptible(&info->open_wait);
2221}
2222
2223
2224/* added by James 03-12-2004. */
2225/*
2226 * mxser_rs_break() --- routine which turns the break handling on or off
2227 */
2228static void mxser_rs_break(struct tty_struct *tty, int break_state)
2229{
Jiri Slaby55b307d2006-12-08 02:38:14 -08002230 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08002231 unsigned long flags;
2232
2233 spin_lock_irqsave(&info->slock, flags);
2234 if (break_state == -1)
Jiri Slaby55b307d2006-12-08 02:38:14 -08002235 outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
2236 info->ioaddr + UART_LCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002237 else
Jiri Slaby55b307d2006-12-08 02:38:14 -08002238 outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
2239 info->ioaddr + UART_LCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002240 spin_unlock_irqrestore(&info->slock, flags);
2241}
2242
2243/* (above) added by James. */
2244
Jiri Slaby55b307d2006-12-08 02:38:14 -08002245static void mxser_receive_chars(struct mxser_port *port, int *status)
Jiri Slaby037ad482006-12-08 02:38:11 -08002246{
Jiri Slaby55b307d2006-12-08 02:38:14 -08002247 struct tty_struct *tty = port->tty;
Jiri Slaby037ad482006-12-08 02:38:11 -08002248 unsigned char ch, gdl;
2249 int ignored = 0;
2250 int cnt = 0;
2251 int recv_room;
2252 int max = 256;
2253 unsigned long flags;
2254
Jiri Slaby55b307d2006-12-08 02:38:14 -08002255 spin_lock_irqsave(&port->slock, flags);
Jiri Slaby037ad482006-12-08 02:38:11 -08002256
2257 recv_room = tty->receive_room;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002258 if ((recv_room == 0) && (!port->ldisc_stop_rx)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002259 /* mxser_throttle(tty); */
2260 mxser_stoprx(tty);
2261 /* return; */
2262 }
2263
2264 /* following add by Victor Yu. 09-02-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002265 if (port->board->chip_flag != MOXA_OTHER_UART) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002266
Jiri Slaby3306ce32006-12-08 02:38:13 -08002267 if (*status & UART_LSR_SPECIAL)
Jiri Slaby037ad482006-12-08 02:38:11 -08002268 goto intr_old;
Jiri Slaby037ad482006-12-08 02:38:11 -08002269 /* following add by Victor Yu. 02-11-2004 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002270 if (port->board->chip_flag == MOXA_MUST_MU860_HWID &&
Jiri Slaby037ad482006-12-08 02:38:11 -08002271 (*status & MOXA_MUST_LSR_RERR))
2272 goto intr_old;
2273 /* above add by Victor Yu. 02-14-2004 */
2274 if (*status & MOXA_MUST_LSR_RERR)
2275 goto intr_old;
2276
Jiri Slaby55b307d2006-12-08 02:38:14 -08002277 gdl = inb(port->ioaddr + MOXA_MUST_GDL_REGISTER);
Jiri Slaby037ad482006-12-08 02:38:11 -08002278
2279 /* add by Victor Yu. 02-11-2004 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002280 if (port->board->chip_flag == MOXA_MUST_MU150_HWID)
Jiri Slaby037ad482006-12-08 02:38:11 -08002281 gdl &= MOXA_MUST_GDL_MASK;
2282 if (gdl >= recv_room) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002283 if (!port->ldisc_stop_rx) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002284 /* mxser_throttle(tty); */
2285 mxser_stoprx(tty);
2286 }
2287 /* return; */
2288 }
2289 while (gdl--) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002290 ch = inb(port->ioaddr + UART_RX);
Jiri Slaby037ad482006-12-08 02:38:11 -08002291 tty_insert_flip_char(tty, ch, 0);
2292 cnt++;
Jiri Slaby037ad482006-12-08 02:38:11 -08002293 }
2294 goto end_intr;
2295 }
2296 intr_old:
2297 /* above add by Victor Yu. 09-02-2002 */
2298
2299 do {
2300 if (max-- < 0)
2301 break;
Jiri Slaby037ad482006-12-08 02:38:11 -08002302
Jiri Slaby55b307d2006-12-08 02:38:14 -08002303 ch = inb(port->ioaddr + UART_RX);
Jiri Slaby037ad482006-12-08 02:38:11 -08002304 /* following add by Victor Yu. 09-02-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002305 if (port->board->chip_flag && (*status & UART_LSR_OE)
Jiri Slaby3306ce32006-12-08 02:38:13 -08002306 /*&& !(*status&UART_LSR_DR) */)
Jiri Slaby55b307d2006-12-08 02:38:14 -08002307 outb(0x23, port->ioaddr + UART_FCR);
2308 *status &= port->read_status_mask;
Jiri Slaby037ad482006-12-08 02:38:11 -08002309 /* above add by Victor Yu. 09-02-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002310 if (*status & port->ignore_status_mask) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002311 if (++ignored > 100)
2312 break;
2313 } else {
2314 char flag = 0;
2315 if (*status & UART_LSR_SPECIAL) {
2316 if (*status & UART_LSR_BI) {
2317 flag = TTY_BREAK;
2318/* added by casper 1/11/2000 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002319 port->icount.brk++;
Jiri Slaby3306ce32006-12-08 02:38:13 -08002320
Jiri Slaby55b307d2006-12-08 02:38:14 -08002321 if (port->flags & ASYNC_SAK)
Jiri Slaby037ad482006-12-08 02:38:11 -08002322 do_SAK(tty);
2323 } else if (*status & UART_LSR_PE) {
2324 flag = TTY_PARITY;
2325/* added by casper 1/11/2000 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002326 port->icount.parity++;
Jiri Slaby037ad482006-12-08 02:38:11 -08002327 } else if (*status & UART_LSR_FE) {
2328 flag = TTY_FRAME;
2329/* added by casper 1/11/2000 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002330 port->icount.frame++;
Jiri Slaby037ad482006-12-08 02:38:11 -08002331 } else if (*status & UART_LSR_OE) {
2332 flag = TTY_OVERRUN;
2333/* added by casper 1/11/2000 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002334 port->icount.overrun++;
Jiri Slaby3306ce32006-12-08 02:38:13 -08002335 } else
2336 flags = TTY_BREAK;
2337 } else
2338 flags = 0;
Jiri Slaby037ad482006-12-08 02:38:11 -08002339 tty_insert_flip_char(tty, ch, flag);
2340 cnt++;
2341 if (cnt >= recv_room) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002342 if (!port->ldisc_stop_rx) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002343 /* mxser_throttle(tty); */
2344 mxser_stoprx(tty);
2345 }
2346 break;
2347 }
2348
2349 }
2350
2351 /* following add by Victor Yu. 09-02-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002352 if (port->board->chip_flag)
Jiri Slaby037ad482006-12-08 02:38:11 -08002353 break;
Jiri Slaby037ad482006-12-08 02:38:11 -08002354
2355 /* mask by Victor Yu. 09-02-2002
Jiri Slaby55b307d2006-12-08 02:38:14 -08002356 *status = inb(port->ioaddr + UART_LSR) & port->read_status_mask;
Jiri Slaby037ad482006-12-08 02:38:11 -08002357 */
2358 /* following add by Victor Yu. 09-02-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002359 *status = inb(port->ioaddr + UART_LSR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002360 /* above add by Victor Yu. 09-02-2002 */
2361 } while (*status & UART_LSR_DR);
2362
2363end_intr: /* add by Victor Yu. 09-02-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002364 mxvar_log.rxcnt[port->tty->index] += cnt;
2365 port->mon_data.rxcnt += cnt;
2366 port->mon_data.up_rxcnt += cnt;
2367 spin_unlock_irqrestore(&port->slock, flags);
Jiri Slaby037ad482006-12-08 02:38:11 -08002368
2369 tty_flip_buffer_push(tty);
2370}
2371
Jiri Slaby55b307d2006-12-08 02:38:14 -08002372static void mxser_transmit_chars(struct mxser_port *port)
Jiri Slaby037ad482006-12-08 02:38:11 -08002373{
2374 int count, cnt;
2375 unsigned long flags;
2376
Jiri Slaby55b307d2006-12-08 02:38:14 -08002377 spin_lock_irqsave(&port->slock, flags);
Jiri Slaby037ad482006-12-08 02:38:11 -08002378
Jiri Slaby55b307d2006-12-08 02:38:14 -08002379 if (port->x_char) {
2380 outb(port->x_char, port->ioaddr + UART_TX);
2381 port->x_char = 0;
2382 mxvar_log.txcnt[port->tty->index]++;
2383 port->mon_data.txcnt++;
2384 port->mon_data.up_txcnt++;
Jiri Slaby037ad482006-12-08 02:38:11 -08002385
2386/* added by casper 1/11/2000 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002387 port->icount.tx++;
Jiri Slaby3306ce32006-12-08 02:38:13 -08002388 goto unlock;
Jiri Slaby037ad482006-12-08 02:38:11 -08002389 }
2390
Jiri Slaby55b307d2006-12-08 02:38:14 -08002391 if (port->xmit_buf == 0)
Jiri Slaby3306ce32006-12-08 02:38:13 -08002392 goto unlock;
Jiri Slaby037ad482006-12-08 02:38:11 -08002393
Jiri Slaby55b307d2006-12-08 02:38:14 -08002394 if (port->xmit_cnt == 0) {
2395 if (port->xmit_cnt < WAKEUP_CHARS) { /* XXX what's this for?? */
2396 set_bit(MXSER_EVENT_TXLOW, &port->event);
2397 schedule_work(&port->tqueue);
Jiri Slaby3306ce32006-12-08 02:38:13 -08002398 }
2399 goto unlock;
2400 }
Jiri Slaby55b307d2006-12-08 02:38:14 -08002401 if (port->tty->stopped || (port->tty->hw_stopped &&
2402 (port->type != PORT_16550A) &&
2403 (!port->board->chip_flag))) {
2404 port->IER &= ~UART_IER_THRI;
2405 outb(port->IER, port->ioaddr + UART_IER);
Jiri Slaby3306ce32006-12-08 02:38:13 -08002406 goto unlock;
Jiri Slaby037ad482006-12-08 02:38:11 -08002407 }
2408
Jiri Slaby55b307d2006-12-08 02:38:14 -08002409 cnt = port->xmit_cnt;
2410 count = port->xmit_fifo_size;
Jiri Slaby037ad482006-12-08 02:38:11 -08002411 do {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002412 outb(port->xmit_buf[port->xmit_tail++],
2413 port->ioaddr + UART_TX);
2414 port->xmit_tail = port->xmit_tail & (SERIAL_XMIT_SIZE - 1);
2415 if (--port->xmit_cnt <= 0)
Jiri Slaby037ad482006-12-08 02:38:11 -08002416 break;
2417 } while (--count > 0);
Jiri Slaby55b307d2006-12-08 02:38:14 -08002418 mxvar_log.txcnt[port->tty->index] += (cnt - port->xmit_cnt);
Jiri Slaby037ad482006-12-08 02:38:11 -08002419
2420/* added by James 03-12-2004. */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002421 port->mon_data.txcnt += (cnt - port->xmit_cnt);
2422 port->mon_data.up_txcnt += (cnt - port->xmit_cnt);
Jiri Slaby037ad482006-12-08 02:38:11 -08002423
2424/* added by casper 1/11/2000 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002425 port->icount.tx += (cnt - port->xmit_cnt);
Jiri Slaby037ad482006-12-08 02:38:11 -08002426
Jiri Slaby55b307d2006-12-08 02:38:14 -08002427 if (port->xmit_cnt < WAKEUP_CHARS) {
2428 set_bit(MXSER_EVENT_TXLOW, &port->event);
2429 schedule_work(&port->tqueue);
Jiri Slaby037ad482006-12-08 02:38:11 -08002430 }
Jiri Slaby55b307d2006-12-08 02:38:14 -08002431 if (port->xmit_cnt <= 0) {
2432 port->IER &= ~UART_IER_THRI;
2433 outb(port->IER, port->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08002434 }
Jiri Slaby3306ce32006-12-08 02:38:13 -08002435unlock:
Jiri Slaby55b307d2006-12-08 02:38:14 -08002436 spin_unlock_irqrestore(&port->slock, flags);
Jiri Slaby037ad482006-12-08 02:38:11 -08002437}
2438
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002439/*
2440 * This is the serial driver's generic interrupt routine
2441 */
2442static irqreturn_t mxser_interrupt(int irq, void *dev_id, struct pt_regs *regs)
Jiri Slaby037ad482006-12-08 02:38:11 -08002443{
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002444 int status, iir, i;
2445 struct mxser_board *brd = NULL;
2446 struct mxser_port *port;
2447 int max, irqbits, bits, msr;
2448 int pass_counter = 0;
2449 unsigned int int_cnt;
2450 int handled = IRQ_NONE;
Jiri Slaby037ad482006-12-08 02:38:11 -08002451
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002452 /* spin_lock(&gm_lock); */
Jiri Slaby037ad482006-12-08 02:38:11 -08002453
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002454 for (i = 0; i < MXSER_BOARDS; i++)
2455 if (dev_id == &mxser_boards[i]) {
2456 brd = dev_id;
2457 break;
Jiri Slaby037ad482006-12-08 02:38:11 -08002458 }
Jiri Slaby037ad482006-12-08 02:38:11 -08002459
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002460 if (i == MXSER_BOARDS)
2461 goto irq_stop;
2462 if (brd == NULL)
2463 goto irq_stop;
2464 max = mxser_numports[brd->board_type - 1];
Jiri Slaby037ad482006-12-08 02:38:11 -08002465 while (1) {
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002466 irqbits = inb(brd->vector) & brd->vector_mask;
2467 if (irqbits == brd->vector_mask)
Jiri Slaby037ad482006-12-08 02:38:11 -08002468 break;
Jiri Slaby037ad482006-12-08 02:38:11 -08002469
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002470 handled = IRQ_HANDLED;
2471 for (i = 0, bits = 1; i < max; i++, irqbits |= bits, bits <<= 1) {
2472 if (irqbits == brd->vector_mask)
Jiri Slaby037ad482006-12-08 02:38:11 -08002473 break;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002474 if (bits & irqbits)
2475 continue;
2476 port = &brd->ports[i];
Jiri Slaby037ad482006-12-08 02:38:11 -08002477
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002478 int_cnt = 0;
2479 do {
2480 /* following add by Victor Yu. 09-13-2002 */
2481 iir = inb(port->ioaddr + UART_IIR);
2482 if (iir & UART_IIR_NO_INT)
2483 break;
2484 iir &= MOXA_MUST_IIR_MASK;
2485 if (!port->tty) {
2486 status = inb(port->ioaddr + UART_LSR);
2487 outb(0x27, port->ioaddr + UART_FCR);
2488 inb(port->ioaddr + UART_MSR);
2489 break;
Jiri Slaby037ad482006-12-08 02:38:11 -08002490 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002491 /* above add by Victor Yu. 09-13-2002 */
Jiri Slaby037ad482006-12-08 02:38:11 -08002492
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002493 /* following add by Victor Yu. 09-02-2002 */
2494 status = inb(port->ioaddr + UART_LSR);
2495
2496 if (status & UART_LSR_PE)
2497 port->err_shadow |= NPPI_NOTIFY_PARITY;
2498 if (status & UART_LSR_FE)
2499 port->err_shadow |= NPPI_NOTIFY_FRAMING;
2500 if (status & UART_LSR_OE)
2501 port->err_shadow |=
2502 NPPI_NOTIFY_HW_OVERRUN;
2503 if (status & UART_LSR_BI)
2504 port->err_shadow |= NPPI_NOTIFY_BREAK;
2505
2506 if (port->board->chip_flag) {
2507 /*
2508 if ( (status & 0x02) && !(status & 0x01) ) {
2509 outb(port->ioaddr+UART_FCR, 0x23);
2510 continue;
2511 }
2512 */
2513 if (iir == MOXA_MUST_IIR_GDA ||
2514 iir == MOXA_MUST_IIR_RDA ||
2515 iir == MOXA_MUST_IIR_RTO ||
2516 iir == MOXA_MUST_IIR_LSR)
2517 mxser_receive_chars(port,
2518 &status);
2519
2520 } else {
2521 /* above add by Victor Yu. 09-02-2002 */
2522
2523 status &= port->read_status_mask;
2524 if (status & UART_LSR_DR)
2525 mxser_receive_chars(port,
2526 &status);
2527 }
2528 msr = inb(port->ioaddr + UART_MSR);
2529 if (msr & UART_MSR_ANY_DELTA)
2530 mxser_check_modem_status(port, msr);
2531
2532 /* following add by Victor Yu. 09-13-2002 */
2533 if (port->board->chip_flag) {
2534 if (iir == 0x02 && (status &
2535 UART_LSR_THRE))
2536 mxser_transmit_chars(port);
2537 } else {
2538 /* above add by Victor Yu. 09-13-2002 */
2539
2540 if (status & UART_LSR_THRE)
2541 mxser_transmit_chars(port);
2542 }
2543 } while (int_cnt++ < MXSER_ISR_PASS_LIMIT);
2544 }
2545 if (pass_counter++ > MXSER_ISR_PASS_LIMIT)
2546 break; /* Prevent infinite loops */
2547 }
2548
2549 irq_stop:
2550 /* spin_unlock(&gm_lock); */
2551 return handled;
2552}
2553
2554static const struct tty_operations mxser_ops = {
2555 .open = mxser_open,
2556 .close = mxser_close,
2557 .write = mxser_write,
2558 .put_char = mxser_put_char,
2559 .flush_chars = mxser_flush_chars,
2560 .write_room = mxser_write_room,
2561 .chars_in_buffer = mxser_chars_in_buffer,
2562 .flush_buffer = mxser_flush_buffer,
2563 .ioctl = mxser_ioctl,
2564 .throttle = mxser_throttle,
2565 .unthrottle = mxser_unthrottle,
2566 .set_termios = mxser_set_termios,
2567 .stop = mxser_stop,
2568 .start = mxser_start,
2569 .hangup = mxser_hangup,
2570 .break_ctl = mxser_rs_break,
2571 .wait_until_sent = mxser_wait_until_sent,
2572 .tiocmget = mxser_tiocmget,
2573 .tiocmset = mxser_tiocmset,
2574};
2575
2576/*
2577 * The MOXA Smartio/Industio serial driver boot-time initialization code!
2578 */
2579
2580static int __devinit mxser_initbrd(struct mxser_board *brd)
2581{
2582 struct mxser_port *info;
2583 unsigned int i;
2584 int retval;
2585
2586 printk(KERN_INFO "max. baud rate = %d bps.\n", brd->ports[0].max_baud);
2587
2588 for (i = 0; i < brd->nports; i++) {
2589 info = &brd->ports[i];
2590 info->board = brd;
2591 info->stop_rx = 0;
2592 info->ldisc_stop_rx = 0;
2593
2594 /* Enhance mode enabled here */
2595 if (brd->chip_flag != MOXA_OTHER_UART)
2596 ENABLE_MOXA_MUST_ENCHANCE_MODE(info->ioaddr);
2597
2598 info->flags = ASYNC_SHARE_IRQ;
2599 info->type = brd->uart_type;
2600
2601 process_txrx_fifo(info);
2602
2603 info->custom_divisor = info->baud_base * 16;
2604 info->close_delay = 5 * HZ / 10;
2605 info->closing_wait = 30 * HZ;
2606 INIT_WORK(&info->tqueue, mxser_do_softint, info);
2607 info->normal_termios = mxvar_sdriver->init_termios;
2608 init_waitqueue_head(&info->open_wait);
2609 init_waitqueue_head(&info->close_wait);
2610 init_waitqueue_head(&info->delta_msr_wait);
2611 memset(&info->mon_data, 0, sizeof(struct mxser_mon));
2612 info->err_shadow = 0;
2613 spin_lock_init(&info->slock);
2614
2615 /* before set INT ISR, disable all int */
2616 outb(inb(info->ioaddr + UART_IER) & 0xf0,
2617 info->ioaddr + UART_IER);
2618 }
Jiri Slaby037ad482006-12-08 02:38:11 -08002619 /*
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002620 * Allocate the IRQ if necessary
Jiri Slaby037ad482006-12-08 02:38:11 -08002621 */
Jiri Slaby037ad482006-12-08 02:38:11 -08002622
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002623 retval = request_irq(brd->irq, mxser_interrupt,
2624 (brd->ports[0].flags & ASYNC_SHARE_IRQ) ? IRQF_SHARED :
2625 IRQF_DISABLED, "mxser", brd);
2626 if (retval) {
2627 printk(KERN_ERR "Board %s: Request irq failed, IRQ (%d) may "
2628 "conflict with another device.\n",
2629 mxser_brdname[brd->board_type - 1], brd->irq);
2630 return retval;
Jiri Slaby037ad482006-12-08 02:38:11 -08002631 }
Jiri Slaby037ad482006-12-08 02:38:11 -08002632 return 0;
2633}
2634
Jiri Slaby943f2952006-12-08 02:38:15 -08002635static int __init mxser_get_ISA_conf(int cap, struct mxser_board *brd)
Jiri Slaby037ad482006-12-08 02:38:11 -08002636{
2637 int id, i, bits;
2638 unsigned short regs[16], irq;
2639 unsigned char scratch, scratch2;
2640
Jiri Slaby55b307d2006-12-08 02:38:14 -08002641 brd->chip_flag = MOXA_OTHER_UART;
Jiri Slaby037ad482006-12-08 02:38:11 -08002642
2643 id = mxser_read_register(cap, regs);
2644 if (id == C168_ASIC_ID) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002645 brd->board_type = MXSER_BOARD_C168_ISA;
2646 brd->nports = 8;
Jiri Slaby037ad482006-12-08 02:38:11 -08002647 } else if (id == C104_ASIC_ID) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002648 brd->board_type = MXSER_BOARD_C104_ISA;
2649 brd->nports = 4;
Jiri Slaby037ad482006-12-08 02:38:11 -08002650 } else if (id == C102_ASIC_ID) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002651 brd->board_type = MXSER_BOARD_C102_ISA;
2652 brd->nports = 2;
Jiri Slaby037ad482006-12-08 02:38:11 -08002653 } else if (id == CI132_ASIC_ID) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002654 brd->board_type = MXSER_BOARD_CI132;
2655 brd->nports = 2;
Jiri Slaby037ad482006-12-08 02:38:11 -08002656 } else if (id == CI134_ASIC_ID) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002657 brd->board_type = MXSER_BOARD_CI134;
2658 brd->nports = 4;
Jiri Slaby037ad482006-12-08 02:38:11 -08002659 } else if (id == CI104J_ASIC_ID) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002660 brd->board_type = MXSER_BOARD_CI104J;
2661 brd->nports = 4;
Jiri Slaby037ad482006-12-08 02:38:11 -08002662 } else
2663 return 0;
2664
2665 irq = 0;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002666 if (brd->nports == 2) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002667 irq = regs[9] & 0xF000;
2668 irq = irq | (irq >> 4);
2669 if (irq != (regs[9] & 0xFF00))
2670 return MXSER_ERR_IRQ_CONFLIT;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002671 } else if (brd->nports == 4) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002672 irq = regs[9] & 0xF000;
2673 irq = irq | (irq >> 4);
2674 irq = irq | (irq >> 8);
2675 if (irq != regs[9])
2676 return MXSER_ERR_IRQ_CONFLIT;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002677 } else if (brd->nports == 8) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002678 irq = regs[9] & 0xF000;
2679 irq = irq | (irq >> 4);
2680 irq = irq | (irq >> 8);
2681 if ((irq != regs[9]) || (irq != regs[10]))
2682 return MXSER_ERR_IRQ_CONFLIT;
2683 }
2684
2685 if (!irq)
2686 return MXSER_ERR_IRQ;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002687 brd->irq = ((int)(irq & 0xF000) >> 12);
Jiri Slaby037ad482006-12-08 02:38:11 -08002688 for (i = 0; i < 8; i++)
Jiri Slaby55b307d2006-12-08 02:38:14 -08002689 brd->ports[i].ioaddr = (int) regs[i + 1] & 0xFFF8;
Jiri Slaby037ad482006-12-08 02:38:11 -08002690 if ((regs[12] & 0x80) == 0)
2691 return MXSER_ERR_VECTOR;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002692 brd->vector = (int)regs[11]; /* interrupt vector */
Jiri Slaby037ad482006-12-08 02:38:11 -08002693 if (id == 1)
Jiri Slaby55b307d2006-12-08 02:38:14 -08002694 brd->vector_mask = 0x00FF;
Jiri Slaby037ad482006-12-08 02:38:11 -08002695 else
Jiri Slaby55b307d2006-12-08 02:38:14 -08002696 brd->vector_mask = 0x000F;
Jiri Slaby037ad482006-12-08 02:38:11 -08002697 for (i = 7, bits = 0x0100; i >= 0; i--, bits <<= 1) {
2698 if (regs[12] & bits) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002699 brd->ports[i].baud_base = 921600;
2700 brd->ports[i].max_baud = 921600; /* add by Victor Yu. 09-04-2002 */
Jiri Slaby037ad482006-12-08 02:38:11 -08002701 } else {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002702 brd->ports[i].baud_base = 115200;
2703 brd->ports[i].max_baud = 115200; /* add by Victor Yu. 09-04-2002 */
Jiri Slaby037ad482006-12-08 02:38:11 -08002704 }
2705 }
2706 scratch2 = inb(cap + UART_LCR) & (~UART_LCR_DLAB);
2707 outb(scratch2 | UART_LCR_DLAB, cap + UART_LCR);
2708 outb(0, cap + UART_EFR); /* EFR is the same as FCR */
2709 outb(scratch2, cap + UART_LCR);
2710 outb(UART_FCR_ENABLE_FIFO, cap + UART_FCR);
2711 scratch = inb(cap + UART_IIR);
2712
2713 if (scratch & 0xC0)
Jiri Slaby55b307d2006-12-08 02:38:14 -08002714 brd->uart_type = PORT_16550A;
Jiri Slaby037ad482006-12-08 02:38:11 -08002715 else
Jiri Slaby55b307d2006-12-08 02:38:14 -08002716 brd->uart_type = PORT_16450;
Jiri Slaby037ad482006-12-08 02:38:11 -08002717 if (id == 1)
Jiri Slaby55b307d2006-12-08 02:38:14 -08002718 brd->nports = 8;
Jiri Slaby037ad482006-12-08 02:38:11 -08002719 else
Jiri Slaby55b307d2006-12-08 02:38:14 -08002720 brd->nports = 4;
Jiri Slaby7a7a5c32006-12-08 02:38:17 -08002721 if (!request_region(brd->ports[0].ioaddr, 8 * brd->nports, "mxser(IO)"))
2722 return MXSER_ERR_IOADDR;
2723 if (!request_region(brd->vector, 1, "mxser(vector)")) {
2724 release_region(brd->ports[0].ioaddr, 8 * brd->nports);
2725 return MXSER_ERR_VECTOR;
2726 }
Jiri Slaby55b307d2006-12-08 02:38:14 -08002727 return brd->nports;
Jiri Slaby037ad482006-12-08 02:38:11 -08002728}
2729
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002730static int __init mxser_get_PCI_conf(int board_type, struct mxser_board *brd,
2731 struct pci_dev *pdev)
Jiri Slaby037ad482006-12-08 02:38:11 -08002732{
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002733 unsigned int i, j;
2734 unsigned long ioaddress;
2735 int retval;
Jiri Slaby037ad482006-12-08 02:38:11 -08002736
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002737 /* io address */
2738 brd->board_type = board_type;
2739 brd->nports = mxser_numports[board_type - 1];
2740 ioaddress = pci_resource_start(pdev, 2);
2741 retval = pci_request_region(pdev, 2, "mxser(IO)");
2742 if (retval)
2743 goto err;
2744
2745 for (i = 0; i < brd->nports; i++)
2746 brd->ports[i].ioaddr = ioaddress + 8 * i;
2747
2748 /* vector */
2749 ioaddress = pci_resource_start(pdev, 3);
2750 retval = pci_request_region(pdev, 3, "mxser(vector)");
2751 if (retval)
2752 goto err_relio;
2753 brd->vector = ioaddress;
2754
2755 /* irq */
2756 brd->irq = pdev->irq;
2757
2758 brd->chip_flag = CheckIsMoxaMust(brd->ports[0].ioaddr);
2759 brd->uart_type = PORT_16550A;
2760 brd->vector_mask = 0;
2761
2762 for (i = 0; i < brd->nports; i++) {
2763 for (j = 0; j < UART_INFO_NUM; j++) {
2764 if (Gpci_uart_info[j].type == brd->chip_flag) {
2765 brd->ports[i].max_baud =
2766 Gpci_uart_info[j].max_baud;
2767
2768 /* exception....CP-102 */
2769 if (board_type == MXSER_BOARD_CP102)
2770 brd->ports[i].max_baud = 921600;
2771 break;
Jiri Slaby037ad482006-12-08 02:38:11 -08002772 }
2773 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002774 }
2775
2776 if (brd->chip_flag == MOXA_MUST_MU860_HWID) {
2777 for (i = 0; i < brd->nports; i++) {
2778 if (i < 4)
2779 brd->ports[i].opmode_ioaddr = ioaddress + 4;
2780 else
2781 brd->ports[i].opmode_ioaddr = ioaddress + 0x0c;
Jiri Slaby037ad482006-12-08 02:38:11 -08002782 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002783 outb(0, ioaddress + 4); /* default set to RS232 mode */
2784 outb(0, ioaddress + 0x0c); /* default set to RS232 mode */
Jiri Slaby037ad482006-12-08 02:38:11 -08002785 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002786
2787 for (i = 0; i < brd->nports; i++) {
2788 brd->vector_mask |= (1 << i);
2789 brd->ports[i].baud_base = 921600;
2790 }
2791 return 0;
2792err_relio:
2793 pci_release_region(pdev, 2);
2794err:
2795 return retval;
Jiri Slaby037ad482006-12-08 02:38:11 -08002796}
2797
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002798static int __init mxser_module_init(void)
Jiri Slaby037ad482006-12-08 02:38:11 -08002799{
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002800 struct pci_dev *pdev = NULL;
2801 struct mxser_board *brd;
2802 unsigned int i, m;
2803 int retval, b, n;
Jiri Slaby037ad482006-12-08 02:38:11 -08002804
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002805 pr_debug("Loading module mxser ...\n");
Jiri Slaby037ad482006-12-08 02:38:11 -08002806
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002807 mxvar_sdriver = alloc_tty_driver(MXSER_PORTS + 1);
2808 if (!mxvar_sdriver)
2809 return -ENOMEM;
2810 spin_lock_init(&gm_lock);
2811
2812 for (i = 0; i < MXSER_BOARDS; i++)
2813 mxser_boards[i].board_type = -1;
2814
2815 printk(KERN_INFO "MOXA Smartio/Industio family driver version %s\n",
2816 MXSER_VERSION);
2817
2818 /* Initialize the tty_driver structure */
2819 mxvar_sdriver->magic = TTY_DRIVER_MAGIC;
2820 mxvar_sdriver->name = "ttyM";
2821 mxvar_sdriver->major = ttymajor;
2822 mxvar_sdriver->minor_start = 0;
2823 mxvar_sdriver->num = MXSER_PORTS + 1;
2824 mxvar_sdriver->type = TTY_DRIVER_TYPE_SERIAL;
2825 mxvar_sdriver->subtype = SERIAL_TYPE_NORMAL;
2826 mxvar_sdriver->init_termios = tty_std_termios;
2827 mxvar_sdriver->init_termios.c_cflag = B9600|CS8|CREAD|HUPCL|CLOCAL;
2828 mxvar_sdriver->flags = TTY_DRIVER_REAL_RAW;
2829 tty_set_operations(mxvar_sdriver, &mxser_ops);
2830 mxvar_sdriver->ttys = mxvar_tty;
2831 mxvar_sdriver->termios = mxvar_termios;
2832 mxvar_sdriver->termios_locked = mxvar_termios_locked;
2833
2834 mxvar_diagflag = 0;
2835
2836 m = 0;
2837 /* Start finding ISA boards here */
2838 for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
2839 int cap;
2840
2841 if (!(cap = mxserBoardCAP[b]))
2842 continue;
2843
2844 brd = &mxser_boards[m];
2845 retval = mxser_get_ISA_conf(cap, brd);
2846
2847 if (retval != 0)
2848 printk(KERN_INFO "Found MOXA %s board (CAP=0x%x)\n",
2849 mxser_brdname[brd->board_type - 1], ioaddr[b]);
2850
2851 if (retval <= 0) {
2852 if (retval == MXSER_ERR_IRQ)
2853 printk(KERN_ERR "Invalid interrupt number, "
2854 "board not configured\n");
2855 else if (retval == MXSER_ERR_IRQ_CONFLIT)
2856 printk(KERN_ERR "Invalid interrupt number, "
2857 "board not configured\n");
2858 else if (retval == MXSER_ERR_VECTOR)
2859 printk(KERN_ERR "Invalid interrupt vector, "
2860 "board not configured\n");
2861 else if (retval == MXSER_ERR_IOADDR)
2862 printk(KERN_ERR "Invalid I/O address, "
2863 "board not configured\n");
2864
2865 continue;
2866 }
2867
2868 brd->pdev = NULL;
2869
2870 /* mxser_initbrd will hook ISR. */
2871 if (mxser_initbrd(brd) < 0)
2872 continue;
2873
2874 m++;
Jiri Slaby037ad482006-12-08 02:38:11 -08002875 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002876
2877 /* Start finding ISA boards from module arg */
2878 for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
2879 unsigned long cap;
2880
2881 if (!(cap = ioaddr[b]))
2882 continue;
2883
2884 brd = &mxser_boards[m];
2885 retval = mxser_get_ISA_conf(cap, &mxser_boards[m]);
2886
2887 if (retval != 0)
2888 printk(KERN_INFO "Found MOXA %s board (CAP=0x%x)\n",
2889 mxser_brdname[brd->board_type - 1], ioaddr[b]);
2890
2891 if (retval <= 0) {
2892 if (retval == MXSER_ERR_IRQ)
2893 printk(KERN_ERR "Invalid interrupt number, "
2894 "board not configured\n");
2895 else if (retval == MXSER_ERR_IRQ_CONFLIT)
2896 printk(KERN_ERR "Invalid interrupt number, "
2897 "board not configured\n");
2898 else if (retval == MXSER_ERR_VECTOR)
2899 printk(KERN_ERR "Invalid interrupt vector, "
2900 "board not configured\n");
2901 else if (retval == MXSER_ERR_IOADDR)
2902 printk(KERN_ERR "Invalid I/O address, "
2903 "board not configured\n");
2904
2905 continue;
2906 }
2907
2908 brd->pdev = NULL;
2909 /* mxser_initbrd will hook ISR. */
2910 if (mxser_initbrd(brd) < 0)
2911 continue;
2912
2913 m++;
2914 }
2915
2916 /* start finding PCI board here */
2917 n = ARRAY_SIZE(mxser_pcibrds) - 1;
2918 b = 0;
2919 while (b < n) {
2920 pdev = pci_get_device(mxser_pcibrds[b].vendor,
2921 mxser_pcibrds[b].device, pdev);
2922 if (pdev == NULL) {
2923 b++;
2924 continue;
2925 }
2926 printk(KERN_INFO "Found MOXA %s board(BusNo=%d,DevNo=%d)\n",
2927 mxser_brdname[(int) (mxser_pcibrds[b].driver_data) - 1],
2928 pdev->bus->number, PCI_SLOT(pdev->devfn));
2929 if (m >= MXSER_BOARDS)
2930 printk(KERN_ERR
2931 "Too many Smartio/Industio family boards find "
2932 "(maximum %d), board not configured\n",
2933 MXSER_BOARDS);
2934 else {
2935 if (pci_enable_device(pdev)) {
2936 printk(KERN_ERR "Moxa SmartI/O PCI enable "
2937 "fail !\n");
2938 continue;
2939 }
2940 brd = &mxser_boards[m];
2941 brd->pdev = pdev;
2942 retval = mxser_get_PCI_conf(
2943 (int)mxser_pcibrds[b].driver_data,
2944 brd, pdev);
2945 if (retval < 0) {
2946 if (retval == MXSER_ERR_IRQ)
2947 printk(KERN_ERR
2948 "Invalid interrupt number, "
2949 "board not configured\n");
2950 else if (retval == MXSER_ERR_IRQ_CONFLIT)
2951 printk(KERN_ERR
2952 "Invalid interrupt number, "
2953 "board not configured\n");
2954 else if (retval == MXSER_ERR_VECTOR)
2955 printk(KERN_ERR
2956 "Invalid interrupt vector, "
2957 "board not configured\n");
2958 else if (retval == MXSER_ERR_IOADDR)
2959 printk(KERN_ERR
2960 "Invalid I/O address, "
2961 "board not configured\n");
2962 continue;
2963 }
2964 /* mxser_initbrd will hook ISR. */
2965 if (mxser_initbrd(brd) < 0)
2966 continue;
2967 m++;
2968 /* Keep an extra reference if we succeeded. It will
2969 be returned at unload time */
2970 pci_dev_get(pdev);
2971 }
2972 }
2973
2974 retval = tty_register_driver(mxvar_sdriver);
2975 if (retval) {
2976 printk(KERN_ERR "Couldn't install MOXA Smartio/Industio family"
2977 " driver !\n");
2978 put_tty_driver(mxvar_sdriver);
2979
2980 for (i = 0; i < MXSER_BOARDS; i++) {
2981 if (mxser_boards[i].board_type == -1)
2982 continue;
2983 else {
2984 free_irq(mxser_boards[i].irq, &mxser_boards[i]);
2985 /* todo: release io, vector */
2986 }
2987 }
2988 return retval;
2989 }
2990
2991 pr_debug("Done.\n");
2992
2993 return retval;
Jiri Slaby037ad482006-12-08 02:38:11 -08002994}
2995
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002996static void __exit mxser_module_exit(void)
Jiri Slaby037ad482006-12-08 02:38:11 -08002997{
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002998 int i, err;
Jiri Slaby037ad482006-12-08 02:38:11 -08002999
Jiri Slabya8dea4e2006-12-08 02:38:21 -08003000 pr_debug("Unloading module mxser ...\n");
3001
3002 err = tty_unregister_driver(mxvar_sdriver);
3003 if (!err)
3004 put_tty_driver(mxvar_sdriver);
3005 else
3006 printk(KERN_ERR "Couldn't unregister MOXA Smartio/Industio family serial driver\n");
3007
3008 for (i = 0; i < MXSER_BOARDS; i++) {
3009 struct pci_dev *pdev;
3010
3011 if (mxser_boards[i].board_type == -1)
3012 continue;
3013 else {
3014 pdev = mxser_boards[i].pdev;
3015 free_irq(mxser_boards[i].irq, &mxser_boards[i]);
3016 if (pdev != NULL) { /* PCI */
3017 pci_release_region(pdev, 2);
3018 pci_release_region(pdev, 3);
3019 pci_dev_put(pdev);
3020 } else {
3021 release_region(mxser_boards[i].ports[0].ioaddr, 8 * mxser_boards[i].nports);
3022 release_region(mxser_boards[i].vector, 1);
3023 }
3024 }
Jiri Slaby037ad482006-12-08 02:38:11 -08003025 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -08003026 pr_debug("Done.\n");
Jiri Slaby037ad482006-12-08 02:38:11 -08003027}
3028
3029module_init(mxser_module_init);
3030module_exit(mxser_module_exit);