blob: 71381f955567c0a8a29ab8a8cc6bd8acf1b1ab75 [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
Jiri Slabya8dea4e2006-12-08 02:38:21 -0800595 if (mxser_set_baud_method[info->tty->index] == 0) {
596 baud = tty_get_baud_rate(info->tty);
597 mxser_set_baud(info, baud);
598 }
599
600 /* byte size and parity */
601 switch (cflag & CSIZE) {
602 case CS5:
603 cval = 0x00;
604 break;
605 case CS6:
606 cval = 0x01;
607 break;
608 case CS7:
609 cval = 0x02;
610 break;
611 case CS8:
612 cval = 0x03;
613 break;
614 default:
615 cval = 0x00;
616 break; /* too keep GCC shut... */
617 }
618 if (cflag & CSTOPB)
619 cval |= 0x04;
620 if (cflag & PARENB)
621 cval |= UART_LCR_PARITY;
622 if (!(cflag & PARODD))
623 cval |= UART_LCR_EPAR;
624 if (cflag & CMSPAR)
625 cval |= UART_LCR_SPAR;
626
627 if ((info->type == PORT_8250) || (info->type == PORT_16450)) {
628 if (info->board->chip_flag) {
629 fcr = UART_FCR_ENABLE_FIFO;
630 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
631 SET_MOXA_MUST_FIFO_VALUE(info);
632 } else
633 fcr = 0;
634 } else {
635 fcr = UART_FCR_ENABLE_FIFO;
636 /* following add by Victor Yu. 08-30-2002 */
637 if (info->board->chip_flag) {
638 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
639 SET_MOXA_MUST_FIFO_VALUE(info);
640 } else {
641 /* above add by Victor Yu. 08-30-2002 */
642 switch (info->rx_trigger) {
643 case 1:
644 fcr |= UART_FCR_TRIGGER_1;
645 break;
646 case 4:
647 fcr |= UART_FCR_TRIGGER_4;
648 break;
649 case 8:
650 fcr |= UART_FCR_TRIGGER_8;
651 break;
652 default:
653 fcr |= UART_FCR_TRIGGER_14;
654 break;
655 }
656 }
657 }
658
659 /* CTS flow control flag and modem status interrupts */
660 info->IER &= ~UART_IER_MSI;
661 info->MCR &= ~UART_MCR_AFE;
662 if (cflag & CRTSCTS) {
663 info->flags |= ASYNC_CTS_FLOW;
664 info->IER |= UART_IER_MSI;
665 if ((info->type == PORT_16550A) || (info->board->chip_flag)) {
666 info->MCR |= UART_MCR_AFE;
667/* status = mxser_get_msr(info->ioaddr, 0, info->port); */
668/*
669 save_flags(flags);
670 cli();
671 status = inb(baseaddr + UART_MSR);
672 restore_flags(flags);
673*/
674 /* mxser_check_modem_status(info, status); */
675 } else {
676/* status = mxser_get_msr(info->ioaddr, 0, info->port); */
677 /* MX_LOCK(&info->slock); */
678 status = inb(info->ioaddr + UART_MSR);
679 /* MX_UNLOCK(&info->slock); */
680 if (info->tty->hw_stopped) {
681 if (status & UART_MSR_CTS) {
682 info->tty->hw_stopped = 0;
683 if (info->type != PORT_16550A &&
684 !info->board->chip_flag) {
685 outb(info->IER & ~UART_IER_THRI,
686 info->ioaddr +
687 UART_IER);
688 info->IER |= UART_IER_THRI;
689 outb(info->IER, info->ioaddr +
690 UART_IER);
691 }
692 set_bit(MXSER_EVENT_TXLOW, &info->event);
693 schedule_work(&info->tqueue); }
694 } else {
695 if (!(status & UART_MSR_CTS)) {
696 info->tty->hw_stopped = 1;
697 if ((info->type != PORT_16550A) &&
698 (!info->board->chip_flag)) {
699 info->IER &= ~UART_IER_THRI;
700 outb(info->IER, info->ioaddr +
701 UART_IER);
702 }
703 }
704 }
705 }
706 } else {
707 info->flags &= ~ASYNC_CTS_FLOW;
708 }
709 outb(info->MCR, info->ioaddr + UART_MCR);
710 if (cflag & CLOCAL) {
711 info->flags &= ~ASYNC_CHECK_CD;
712 } else {
713 info->flags |= ASYNC_CHECK_CD;
714 info->IER |= UART_IER_MSI;
715 }
716 outb(info->IER, info->ioaddr + UART_IER);
717
718 /*
719 * Set up parity check flag
720 */
721 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
722 if (I_INPCK(info->tty))
723 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
724 if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
725 info->read_status_mask |= UART_LSR_BI;
726
727 info->ignore_status_mask = 0;
728
729 if (I_IGNBRK(info->tty)) {
730 info->ignore_status_mask |= UART_LSR_BI;
731 info->read_status_mask |= UART_LSR_BI;
732 /*
733 * If we're ignore parity and break indicators, ignore
734 * overruns too. (For real raw support).
735 */
736 if (I_IGNPAR(info->tty)) {
737 info->ignore_status_mask |=
738 UART_LSR_OE |
739 UART_LSR_PE |
740 UART_LSR_FE;
741 info->read_status_mask |=
742 UART_LSR_OE |
743 UART_LSR_PE |
744 UART_LSR_FE;
745 }
746 }
747 /* following add by Victor Yu. 09-02-2002 */
748 if (info->board->chip_flag) {
749 spin_lock_irqsave(&info->slock, flags);
750 SET_MOXA_MUST_XON1_VALUE(info->ioaddr, START_CHAR(info->tty));
751 SET_MOXA_MUST_XOFF1_VALUE(info->ioaddr, STOP_CHAR(info->tty));
752 if (I_IXON(info->tty)) {
753 ENABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
754 } else {
755 DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
756 }
757 if (I_IXOFF(info->tty)) {
758 ENABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
759 } else {
760 DISABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
761 }
762 /*
763 if ( I_IXANY(info->tty) ) {
764 info->MCR |= MOXA_MUST_MCR_XON_ANY;
765 ENABLE_MOXA_MUST_XON_ANY_FLOW_CONTROL(info->ioaddr);
766 } else {
767 info->MCR &= ~MOXA_MUST_MCR_XON_ANY;
768 DISABLE_MOXA_MUST_XON_ANY_FLOW_CONTROL(info->ioaddr);
769 }
770 */
771 spin_unlock_irqrestore(&info->slock, flags);
772 }
773 /* above add by Victor Yu. 09-02-2002 */
774
775
776 outb(fcr, info->ioaddr + UART_FCR); /* set fcr */
777 outb(cval, info->ioaddr + UART_LCR);
778
779 return ret;
780}
781
782static void mxser_check_modem_status(struct mxser_port *port, int status)
783{
784 /* update input line counters */
785 if (status & UART_MSR_TERI)
786 port->icount.rng++;
787 if (status & UART_MSR_DDSR)
788 port->icount.dsr++;
789 if (status & UART_MSR_DDCD)
790 port->icount.dcd++;
791 if (status & UART_MSR_DCTS)
792 port->icount.cts++;
793 port->mon_data.modem_status = status;
794 wake_up_interruptible(&port->delta_msr_wait);
795
796 if ((port->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
797 if (status & UART_MSR_DCD)
798 wake_up_interruptible(&port->open_wait);
799 schedule_work(&port->tqueue);
800 }
801
802 if (port->flags & ASYNC_CTS_FLOW) {
803 if (port->tty->hw_stopped) {
804 if (status & UART_MSR_CTS) {
805 port->tty->hw_stopped = 0;
806
807 if ((port->type != PORT_16550A) &&
808 (!port->board->chip_flag)) {
809 outb(port->IER & ~UART_IER_THRI,
810 port->ioaddr + UART_IER);
811 port->IER |= UART_IER_THRI;
812 outb(port->IER, port->ioaddr +
813 UART_IER);
814 }
815 set_bit(MXSER_EVENT_TXLOW, &port->event);
816 schedule_work(&port->tqueue);
817 }
818 } else {
819 if (!(status & UART_MSR_CTS)) {
820 port->tty->hw_stopped = 1;
821 if (port->type != PORT_16550A &&
822 !port->board->chip_flag) {
823 port->IER &= ~UART_IER_THRI;
824 outb(port->IER, port->ioaddr +
825 UART_IER);
826 }
827 }
828 }
829 }
830}
831
832static int mxser_startup(struct mxser_port *info)
833{
834 unsigned long page;
835 unsigned long flags;
836
837 page = __get_free_page(GFP_KERNEL);
838 if (!page)
839 return -ENOMEM;
840
841 spin_lock_irqsave(&info->slock, flags);
842
843 if (info->flags & ASYNC_INITIALIZED) {
844 free_page(page);
845 spin_unlock_irqrestore(&info->slock, flags);
846 return 0;
847 }
848
849 if (!info->ioaddr || !info->type) {
850 if (info->tty)
851 set_bit(TTY_IO_ERROR, &info->tty->flags);
852 free_page(page);
853 spin_unlock_irqrestore(&info->slock, flags);
854 return 0;
855 }
856 if (info->xmit_buf)
857 free_page(page);
858 else
859 info->xmit_buf = (unsigned char *) page;
860
861 /*
862 * Clear the FIFO buffers and disable them
863 * (they will be reenabled in mxser_change_speed())
864 */
865 if (info->board->chip_flag)
866 outb((UART_FCR_CLEAR_RCVR |
867 UART_FCR_CLEAR_XMIT |
868 MOXA_MUST_FCR_GDA_MODE_ENABLE), info->ioaddr + UART_FCR);
869 else
870 outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
871 info->ioaddr + UART_FCR);
872
873 /*
874 * At this point there's no way the LSR could still be 0xFF;
875 * if it is, then bail out, because there's likely no UART
876 * here.
877 */
878 if (inb(info->ioaddr + UART_LSR) == 0xff) {
879 spin_unlock_irqrestore(&info->slock, flags);
880 if (capable(CAP_SYS_ADMIN)) {
881 if (info->tty)
882 set_bit(TTY_IO_ERROR, &info->tty->flags);
883 return 0;
884 } else
885 return -ENODEV;
886 }
887
888 /*
889 * Clear the interrupt registers.
890 */
891 (void) inb(info->ioaddr + UART_LSR);
892 (void) inb(info->ioaddr + UART_RX);
893 (void) inb(info->ioaddr + UART_IIR);
894 (void) inb(info->ioaddr + UART_MSR);
895
896 /*
897 * Now, initialize the UART
898 */
899 outb(UART_LCR_WLEN8, info->ioaddr + UART_LCR); /* reset DLAB */
900 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
901 outb(info->MCR, info->ioaddr + UART_MCR);
902
903 /*
904 * Finally, enable interrupts
905 */
906 info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
907 /* info->IER = UART_IER_RLSI | UART_IER_RDI; */
908
909 /* following add by Victor Yu. 08-30-2002 */
910 if (info->board->chip_flag)
911 info->IER |= MOXA_MUST_IER_EGDAI;
912 /* above add by Victor Yu. 08-30-2002 */
913 outb(info->IER, info->ioaddr + UART_IER); /* enable interrupts */
914
915 /*
916 * And clear the interrupt registers again for luck.
917 */
918 (void) inb(info->ioaddr + UART_LSR);
919 (void) inb(info->ioaddr + UART_RX);
920 (void) inb(info->ioaddr + UART_IIR);
921 (void) inb(info->ioaddr + UART_MSR);
922
923 if (info->tty)
924 clear_bit(TTY_IO_ERROR, &info->tty->flags);
925 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
926
927 /*
928 * and set the speed of the serial port
929 */
930 spin_unlock_irqrestore(&info->slock, flags);
931 mxser_change_speed(info, NULL);
932
933 info->flags |= ASYNC_INITIALIZED;
934 return 0;
935}
936
937/*
938 * This routine will shutdown a serial port; interrupts maybe disabled, and
939 * DTR is dropped if the hangup on close termio flag is on.
940 */
941static void mxser_shutdown(struct mxser_port *info)
942{
943 unsigned long flags;
944
945 if (!(info->flags & ASYNC_INITIALIZED))
946 return;
947
948 spin_lock_irqsave(&info->slock, flags);
949
950 /*
951 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
952 * here so the queue might never be waken up
953 */
954 wake_up_interruptible(&info->delta_msr_wait);
955
956 /*
957 * Free the IRQ, if necessary
958 */
959 if (info->xmit_buf) {
960 free_page((unsigned long) info->xmit_buf);
961 info->xmit_buf = NULL;
962 }
963
964 info->IER = 0;
965 outb(0x00, info->ioaddr + UART_IER);
966
967 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
968 info->MCR &= ~(UART_MCR_DTR | UART_MCR_RTS);
969 outb(info->MCR, info->ioaddr + UART_MCR);
970
971 /* clear Rx/Tx FIFO's */
972 /* following add by Victor Yu. 08-30-2002 */
973 if (info->board->chip_flag)
974 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT |
975 MOXA_MUST_FCR_GDA_MODE_ENABLE,
976 info->ioaddr + UART_FCR);
977 else
978 /* above add by Victor Yu. 08-30-2002 */
979 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
980 info->ioaddr + UART_FCR);
981
982 /* read data port to reset things */
983 (void) inb(info->ioaddr + UART_RX);
984
985 if (info->tty)
986 set_bit(TTY_IO_ERROR, &info->tty->flags);
987
988 info->flags &= ~ASYNC_INITIALIZED;
989
990 /* following add by Victor Yu. 09-23-2002 */
991 if (info->board->chip_flag)
992 SET_MOXA_MUST_NO_SOFTWARE_FLOW_CONTROL(info->ioaddr);
993 /* above add by Victor Yu. 09-23-2002 */
994
995 spin_unlock_irqrestore(&info->slock, flags);
996}
997
Jiri Slaby037ad482006-12-08 02:38:11 -0800998/*
999 * This routine is called whenever a serial port is opened. It
1000 * enables interrupts for a serial port, linking in its async structure into
1001 * the IRQ chain. It also performs the serial-specific
1002 * initialization for the tty structure.
1003 */
1004static int mxser_open(struct tty_struct *tty, struct file *filp)
1005{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001006 struct mxser_port *info;
Jiri Slaby037ad482006-12-08 02:38:11 -08001007 int retval, line;
1008
1009 /* initialize driver_data in case something fails */
1010 tty->driver_data = NULL;
1011
1012 line = tty->index;
1013 if (line == MXSER_PORTS)
1014 return 0;
1015 if (line < 0 || line > MXSER_PORTS)
1016 return -ENODEV;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001017 info = &mxser_boards[line / MXSER_PORTS_PER_BOARD].ports[line % MXSER_PORTS_PER_BOARD];
1018 if (!info->ioaddr)
Jiri Slaby037ad482006-12-08 02:38:11 -08001019 return -ENODEV;
1020
1021 tty->driver_data = info;
1022 info->tty = tty;
1023 /*
1024 * Start up serial port
1025 */
Jiri Slaby3306ce32006-12-08 02:38:13 -08001026 info->count++;
Jiri Slaby037ad482006-12-08 02:38:11 -08001027 retval = mxser_startup(info);
1028 if (retval)
1029 return retval;
1030
1031 retval = mxser_block_til_ready(tty, filp, info);
1032 if (retval)
1033 return retval;
1034
Jiri Slaby037ad482006-12-08 02:38:11 -08001035 if ((info->count == 1) && (info->flags & ASYNC_SPLIT_TERMIOS)) {
1036 if (tty->driver->subtype == SERIAL_TYPE_NORMAL)
1037 *tty->termios = info->normal_termios;
1038 else
1039 *tty->termios = info->callout_termios;
1040 mxser_change_speed(info, NULL);
1041 }
1042
Andrew Morton08a4ae42006-12-08 02:38:12 -08001043 info->session = process_session(current);
Jiri Slaby037ad482006-12-08 02:38:11 -08001044 info->pgrp = process_group(current);
1045
1046 /*
1047 status = mxser_get_msr(info->base, 0, info->port);
1048 mxser_check_modem_status(info, status);
1049 */
1050
1051/* unmark here for very high baud rate (ex. 921600 bps) used */
1052 tty->low_latency = 1;
1053 return 0;
1054}
1055
1056/*
1057 * This routine is called when the serial port gets closed. First, we
1058 * wait for the last remaining data to be sent. Then, we unlink its
1059 * async structure from the interrupt chain if necessary, and we free
1060 * that IRQ if nothing is left in the chain.
1061 */
1062static void mxser_close(struct tty_struct *tty, struct file *filp)
1063{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001064 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001065
1066 unsigned long timeout;
1067 unsigned long flags;
Jiri Slaby037ad482006-12-08 02:38:11 -08001068
1069 if (tty->index == MXSER_PORTS)
1070 return;
1071 if (!info)
1072 return;
1073
1074 spin_lock_irqsave(&info->slock, flags);
1075
1076 if (tty_hung_up_p(filp)) {
1077 spin_unlock_irqrestore(&info->slock, flags);
1078 return;
1079 }
1080 if ((tty->count == 1) && (info->count != 1)) {
1081 /*
1082 * Uh, oh. tty->count is 1, which means that the tty
1083 * structure will be freed. Info->count should always
1084 * be one in these conditions. If it's greater than
1085 * one, we've got real problems, since it means the
1086 * serial port won't be shutdown.
1087 */
1088 printk(KERN_ERR "mxser_close: bad serial port count; "
1089 "tty->count is 1, info->count is %d\n", info->count);
1090 info->count = 1;
1091 }
1092 if (--info->count < 0) {
1093 printk(KERN_ERR "mxser_close: bad serial port count for "
Jiri Slaby55b307d2006-12-08 02:38:14 -08001094 "ttys%d: %d\n", tty->index, info->count);
Jiri Slaby037ad482006-12-08 02:38:11 -08001095 info->count = 0;
1096 }
1097 if (info->count) {
1098 spin_unlock_irqrestore(&info->slock, flags);
1099 return;
1100 }
1101 info->flags |= ASYNC_CLOSING;
1102 spin_unlock_irqrestore(&info->slock, flags);
1103 /*
1104 * Save the termios structure, since this port may have
1105 * separate termios for callout and dialin.
1106 */
1107 if (info->flags & ASYNC_NORMAL_ACTIVE)
1108 info->normal_termios = *tty->termios;
1109 /*
1110 * Now we wait for the transmit buffer to clear; and we notify
1111 * the line discipline to only process XON/XOFF characters.
1112 */
1113 tty->closing = 1;
1114 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1115 tty_wait_until_sent(tty, info->closing_wait);
1116 /*
1117 * At this point we stop accepting input. To do this, we
1118 * disable the receive line status interrupts, and tell the
1119 * interrupt driver to stop checking the data ready bit in the
1120 * line status register.
1121 */
1122 info->IER &= ~UART_IER_RLSI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001123 if (info->board->chip_flag)
Jiri Slaby037ad482006-12-08 02:38:11 -08001124 info->IER &= ~MOXA_MUST_RECV_ISR;
1125/* by William
1126 info->read_status_mask &= ~UART_LSR_DR;
1127*/
1128 if (info->flags & ASYNC_INITIALIZED) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08001129 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001130 /*
1131 * Before we drop DTR, make sure the UART transmitter
1132 * has completely drained; this is especially
1133 * important if there is a transmit FIFO!
1134 */
1135 timeout = jiffies + HZ;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001136 while (!(inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001137 schedule_timeout_interruptible(5);
1138 if (time_after(jiffies, timeout))
1139 break;
1140 }
1141 }
1142 mxser_shutdown(info);
1143
1144 if (tty->driver->flush_buffer)
1145 tty->driver->flush_buffer(tty);
1146
Jiri Slaby7e8bcf92006-12-08 02:38:24 -08001147 tty_ldisc_flush(tty);
Jiri Slaby037ad482006-12-08 02:38:11 -08001148
1149 tty->closing = 0;
1150 info->event = 0;
1151 info->tty = NULL;
1152 if (info->blocked_open) {
1153 if (info->close_delay)
1154 schedule_timeout_interruptible(info->close_delay);
1155 wake_up_interruptible(&info->open_wait);
1156 }
1157
1158 info->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
1159 wake_up_interruptible(&info->close_wait);
1160
1161}
1162
1163static int mxser_write(struct tty_struct *tty, const unsigned char *buf, int count)
1164{
1165 int c, total = 0;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001166 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001167 unsigned long flags;
1168
1169 if (!info->xmit_buf)
1170 return 0;
1171
1172 while (1) {
1173 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1174 SERIAL_XMIT_SIZE - info->xmit_head));
1175 if (c <= 0)
1176 break;
1177
1178 memcpy(info->xmit_buf + info->xmit_head, buf, c);
1179 spin_lock_irqsave(&info->slock, flags);
1180 info->xmit_head = (info->xmit_head + c) &
1181 (SERIAL_XMIT_SIZE - 1);
1182 info->xmit_cnt += c;
1183 spin_unlock_irqrestore(&info->slock, flags);
1184
1185 buf += c;
1186 count -= c;
1187 total += c;
1188 }
1189
Jiri Slaby3306ce32006-12-08 02:38:13 -08001190 if (info->xmit_cnt && !tty->stopped
1191 /*&& !(info->IER & UART_IER_THRI)*/) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001192 if (!tty->hw_stopped ||
1193 (info->type == PORT_16550A) ||
Jiri Slaby55b307d2006-12-08 02:38:14 -08001194 (info->board->chip_flag)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001195 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby55b307d2006-12-08 02:38:14 -08001196 outb(info->IER & ~UART_IER_THRI, info->ioaddr +
1197 UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001198 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001199 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001200 spin_unlock_irqrestore(&info->slock, flags);
1201 }
1202 }
1203 return total;
1204}
1205
1206static void mxser_put_char(struct tty_struct *tty, unsigned char ch)
1207{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001208 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001209 unsigned long flags;
1210
1211 if (!info->xmit_buf)
1212 return;
1213
1214 if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1)
1215 return;
1216
1217 spin_lock_irqsave(&info->slock, flags);
1218 info->xmit_buf[info->xmit_head++] = ch;
1219 info->xmit_head &= SERIAL_XMIT_SIZE - 1;
1220 info->xmit_cnt++;
1221 spin_unlock_irqrestore(&info->slock, flags);
Jiri Slaby3306ce32006-12-08 02:38:13 -08001222 if (!tty->stopped /*&& !(info->IER & UART_IER_THRI)*/) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001223 if (!tty->hw_stopped ||
1224 (info->type == PORT_16550A) ||
Jiri Slaby55b307d2006-12-08 02:38:14 -08001225 info->board->chip_flag) {
Jiri Slaby037ad482006-12-08 02:38:11 -08001226 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby55b307d2006-12-08 02:38:14 -08001227 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001228 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001229 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001230 spin_unlock_irqrestore(&info->slock, flags);
1231 }
1232 }
1233}
1234
1235
1236static void mxser_flush_chars(struct tty_struct *tty)
1237{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001238 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001239 unsigned long flags;
1240
1241 if (info->xmit_cnt <= 0 ||
1242 tty->stopped ||
1243 !info->xmit_buf ||
1244 (tty->hw_stopped &&
1245 (info->type != PORT_16550A) &&
Jiri Slaby55b307d2006-12-08 02:38:14 -08001246 (!info->board->chip_flag)
Jiri Slaby037ad482006-12-08 02:38:11 -08001247 ))
1248 return;
1249
1250 spin_lock_irqsave(&info->slock, flags);
1251
Jiri Slaby55b307d2006-12-08 02:38:14 -08001252 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001253 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001254 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08001255
1256 spin_unlock_irqrestore(&info->slock, flags);
1257}
1258
1259static int mxser_write_room(struct tty_struct *tty)
1260{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001261 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001262 int ret;
1263
1264 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1265 if (ret < 0)
1266 ret = 0;
1267 return ret;
1268}
1269
1270static int mxser_chars_in_buffer(struct tty_struct *tty)
1271{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001272 struct mxser_port *info = tty->driver_data;
Jiri Slaby3306ce32006-12-08 02:38:13 -08001273 int len = info->xmit_cnt;
1274
Jiri Slaby55b307d2006-12-08 02:38:14 -08001275 if (!(inb(info->ioaddr + UART_LSR) & UART_LSR_THRE))
Jiri Slaby3306ce32006-12-08 02:38:13 -08001276 len++;
1277
1278 return len;
Jiri Slaby037ad482006-12-08 02:38:11 -08001279}
1280
1281static void mxser_flush_buffer(struct tty_struct *tty)
1282{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001283 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08001284 char fcr;
1285 unsigned long flags;
1286
1287
1288 spin_lock_irqsave(&info->slock, flags);
1289 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1290
1291 /* below added by shinhay */
Jiri Slaby55b307d2006-12-08 02:38:14 -08001292 fcr = inb(info->ioaddr + UART_FCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08001293 outb((fcr | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
Jiri Slaby55b307d2006-12-08 02:38:14 -08001294 info->ioaddr + UART_FCR);
1295 outb(fcr, info->ioaddr + UART_FCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08001296
1297 spin_unlock_irqrestore(&info->slock, flags);
1298 /* above added by shinhay */
1299
Jiri Slaby7e8bcf92006-12-08 02:38:24 -08001300 tty_wakeup(tty);
Jiri Slaby037ad482006-12-08 02:38:11 -08001301}
1302
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001303/*
1304 * ------------------------------------------------------------
1305 * friends of mxser_ioctl()
1306 * ------------------------------------------------------------
1307 */
1308static int mxser_get_serial_info(struct mxser_port *info,
1309 struct serial_struct __user *retinfo)
1310{
1311 struct serial_struct tmp;
1312
1313 if (!retinfo)
1314 return -EFAULT;
1315 memset(&tmp, 0, sizeof(tmp));
1316 tmp.type = info->type;
1317 tmp.line = info->tty->index;
1318 tmp.port = info->ioaddr;
1319 tmp.irq = info->board->irq;
1320 tmp.flags = info->flags;
1321 tmp.baud_base = info->baud_base;
1322 tmp.close_delay = info->close_delay;
1323 tmp.closing_wait = info->closing_wait;
1324 tmp.custom_divisor = info->custom_divisor;
1325 tmp.hub6 = 0;
1326 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1327 return -EFAULT;
1328 return 0;
1329}
1330
1331static int mxser_set_serial_info(struct mxser_port *info,
1332 struct serial_struct __user *new_info)
1333{
1334 struct serial_struct new_serial;
1335 unsigned int flags;
1336 int retval = 0;
1337
1338 if (!new_info || !info->ioaddr)
1339 return -EFAULT;
1340 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
1341 return -EFAULT;
1342
1343 if ((new_serial.irq != info->board->irq) ||
1344 (new_serial.port != info->ioaddr) ||
1345 (new_serial.custom_divisor != info->custom_divisor) ||
1346 (new_serial.baud_base != info->baud_base))
1347 return -EPERM;
1348
1349 flags = info->flags & ASYNC_SPD_MASK;
1350
1351 if (!capable(CAP_SYS_ADMIN)) {
1352 if ((new_serial.baud_base != info->baud_base) ||
1353 (new_serial.close_delay != info->close_delay) ||
1354 ((new_serial.flags & ~ASYNC_USR_MASK) != (info->flags & ~ASYNC_USR_MASK)))
1355 return -EPERM;
1356 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
1357 (new_serial.flags & ASYNC_USR_MASK));
1358 } else {
1359 /*
1360 * OK, past this point, all the error checking has been done.
1361 * At this point, we start making changes.....
1362 */
1363 info->flags = ((info->flags & ~ASYNC_FLAGS) |
1364 (new_serial.flags & ASYNC_FLAGS));
1365 info->close_delay = new_serial.close_delay * HZ / 100;
1366 info->closing_wait = new_serial.closing_wait * HZ / 100;
1367 info->tty->low_latency =
1368 (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1369 info->tty->low_latency = 0; /* (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0; */
1370 }
1371
1372 /* added by casper, 3/17/2000, for mouse */
1373 info->type = new_serial.type;
1374
1375 process_txrx_fifo(info);
1376
1377 if (info->flags & ASYNC_INITIALIZED) {
1378 if (flags != (info->flags & ASYNC_SPD_MASK))
1379 mxser_change_speed(info, NULL);
1380 } else
1381 retval = mxser_startup(info);
1382
1383 return retval;
1384}
1385
1386/*
1387 * mxser_get_lsr_info - get line status register info
1388 *
1389 * Purpose: Let user call ioctl() to get info when the UART physically
1390 * is emptied. On bus types like RS485, the transmitter must
1391 * release the bus after transmitting. This must be done when
1392 * the transmit shift register is empty, not be done when the
1393 * transmit holding register is empty. This functionality
1394 * allows an RS485 driver to be written in user space.
1395 */
1396static int mxser_get_lsr_info(struct mxser_port *info,
1397 unsigned int __user *value)
1398{
1399 unsigned char status;
1400 unsigned int result;
1401 unsigned long flags;
1402
1403 spin_lock_irqsave(&info->slock, flags);
1404 status = inb(info->ioaddr + UART_LSR);
1405 spin_unlock_irqrestore(&info->slock, flags);
1406 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1407 return put_user(result, value);
1408}
1409
1410/*
1411 * This routine sends a break character out the serial port.
1412 */
1413static void mxser_send_break(struct mxser_port *info, int duration)
1414{
1415 unsigned long flags;
1416
1417 if (!info->ioaddr)
1418 return;
1419 set_current_state(TASK_INTERRUPTIBLE);
1420 spin_lock_irqsave(&info->slock, flags);
1421 outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
1422 info->ioaddr + UART_LCR);
1423 spin_unlock_irqrestore(&info->slock, flags);
1424 schedule_timeout(duration);
1425 spin_lock_irqsave(&info->slock, flags);
1426 outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
1427 info->ioaddr + UART_LCR);
1428 spin_unlock_irqrestore(&info->slock, flags);
1429}
1430
1431static int mxser_tiocmget(struct tty_struct *tty, struct file *file)
Jiri Slaby037ad482006-12-08 02:38:11 -08001432{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001433 struct mxser_port *info = tty->driver_data;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001434 unsigned char control, status;
Jiri Slaby037ad482006-12-08 02:38:11 -08001435 unsigned long flags;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001436
Jiri Slaby037ad482006-12-08 02:38:11 -08001437
1438 if (tty->index == MXSER_PORTS)
Jiri Slaby037ad482006-12-08 02:38:11 -08001439 return -ENOIOCTLCMD;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001440 if (tty->flags & (1 << TTY_IO_ERROR))
1441 return -EIO;
1442
1443 control = info->MCR;
1444
1445 spin_lock_irqsave(&info->slock, flags);
1446 status = inb(info->ioaddr + UART_MSR);
1447 if (status & UART_MSR_ANY_DELTA)
1448 mxser_check_modem_status(info, status);
1449 spin_unlock_irqrestore(&info->slock, flags);
1450 return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0) |
1451 ((control & UART_MCR_DTR) ? TIOCM_DTR : 0) |
1452 ((status & UART_MSR_DCD) ? TIOCM_CAR : 0) |
1453 ((status & UART_MSR_RI) ? TIOCM_RNG : 0) |
1454 ((status & UART_MSR_DSR) ? TIOCM_DSR : 0) |
1455 ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
1456}
1457
1458static int mxser_tiocmset(struct tty_struct *tty, struct file *file,
1459 unsigned int set, unsigned int clear)
1460{
1461 struct mxser_port *info = tty->driver_data;
1462 unsigned long flags;
1463
1464
1465 if (tty->index == MXSER_PORTS)
1466 return -ENOIOCTLCMD;
1467 if (tty->flags & (1 << TTY_IO_ERROR))
1468 return -EIO;
1469
1470 spin_lock_irqsave(&info->slock, flags);
1471
1472 if (set & TIOCM_RTS)
1473 info->MCR |= UART_MCR_RTS;
1474 if (set & TIOCM_DTR)
1475 info->MCR |= UART_MCR_DTR;
1476
1477 if (clear & TIOCM_RTS)
1478 info->MCR &= ~UART_MCR_RTS;
1479 if (clear & TIOCM_DTR)
1480 info->MCR &= ~UART_MCR_DTR;
1481
1482 outb(info->MCR, info->ioaddr + UART_MCR);
1483 spin_unlock_irqrestore(&info->slock, flags);
Jiri Slaby037ad482006-12-08 02:38:11 -08001484 return 0;
1485}
1486
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001487static int mxser_program_mode(int port)
1488{
1489 int id, i, j, n;
1490 /* unsigned long flags; */
1491
1492 spin_lock(&gm_lock);
1493 outb(0, port);
1494 outb(0, port);
1495 outb(0, port);
1496 (void)inb(port);
1497 (void)inb(port);
1498 outb(0, port);
1499 (void)inb(port);
1500 /* restore_flags(flags); */
1501 spin_unlock(&gm_lock);
1502
1503 id = inb(port + 1) & 0x1F;
1504 if ((id != C168_ASIC_ID) &&
1505 (id != C104_ASIC_ID) &&
1506 (id != C102_ASIC_ID) &&
1507 (id != CI132_ASIC_ID) &&
1508 (id != CI134_ASIC_ID) &&
1509 (id != CI104J_ASIC_ID))
1510 return -1;
1511 for (i = 0, j = 0; i < 4; i++) {
1512 n = inb(port + 2);
1513 if (n == 'M') {
1514 j = 1;
1515 } else if ((j == 1) && (n == 1)) {
1516 j = 2;
1517 break;
1518 } else
1519 j = 0;
1520 }
1521 if (j != 2)
1522 id = -2;
1523 return id;
1524}
1525
1526static void mxser_normal_mode(int port)
1527{
1528 int i, n;
1529
1530 outb(0xA5, port + 1);
1531 outb(0x80, port + 3);
1532 outb(12, port + 0); /* 9600 bps */
1533 outb(0, port + 1);
1534 outb(0x03, port + 3); /* 8 data bits */
1535 outb(0x13, port + 4); /* loop back mode */
1536 for (i = 0; i < 16; i++) {
1537 n = inb(port + 5);
1538 if ((n & 0x61) == 0x60)
1539 break;
1540 if ((n & 1) == 1)
1541 (void)inb(port);
1542 }
1543 outb(0x00, port + 4);
1544}
1545
1546#define CHIP_SK 0x01 /* Serial Data Clock in Eprom */
1547#define CHIP_DO 0x02 /* Serial Data Output in Eprom */
1548#define CHIP_CS 0x04 /* Serial Chip Select in Eprom */
1549#define CHIP_DI 0x08 /* Serial Data Input in Eprom */
1550#define EN_CCMD 0x000 /* Chip's command register */
1551#define EN0_RSARLO 0x008 /* Remote start address reg 0 */
1552#define EN0_RSARHI 0x009 /* Remote start address reg 1 */
1553#define EN0_RCNTLO 0x00A /* Remote byte count reg WR */
1554#define EN0_RCNTHI 0x00B /* Remote byte count reg WR */
1555#define EN0_DCFG 0x00E /* Data configuration reg WR */
1556#define EN0_PORT 0x010 /* Rcv missed frame error counter RD */
1557#define ENC_PAGE0 0x000 /* Select page 0 of chip registers */
1558#define ENC_PAGE3 0x0C0 /* Select page 3 of chip registers */
1559static int mxser_read_register(int port, unsigned short *regs)
1560{
1561 int i, k, value, id;
1562 unsigned int j;
1563
1564 id = mxser_program_mode(port);
1565 if (id < 0)
1566 return id;
1567 for (i = 0; i < 14; i++) {
1568 k = (i & 0x3F) | 0x180;
1569 for (j = 0x100; j > 0; j >>= 1) {
1570 outb(CHIP_CS, port);
1571 if (k & j) {
1572 outb(CHIP_CS | CHIP_DO, port);
1573 outb(CHIP_CS | CHIP_DO | CHIP_SK, port); /* A? bit of read */
1574 } else {
1575 outb(CHIP_CS, port);
1576 outb(CHIP_CS | CHIP_SK, port); /* A? bit of read */
1577 }
1578 }
1579 (void)inb(port);
1580 value = 0;
1581 for (k = 0, j = 0x8000; k < 16; k++, j >>= 1) {
1582 outb(CHIP_CS, port);
1583 outb(CHIP_CS | CHIP_SK, port);
1584 if (inb(port) & CHIP_DI)
1585 value |= j;
1586 }
1587 regs[i] = value;
1588 outb(0, port);
1589 }
1590 mxser_normal_mode(port);
1591 return id;
1592}
1593
Jiri Slaby037ad482006-12-08 02:38:11 -08001594static int mxser_ioctl_special(unsigned int cmd, void __user *argp)
1595{
Jiri Slaby55b307d2006-12-08 02:38:14 -08001596 struct mxser_port *port;
1597 int result, status;
1598 unsigned int i, j;
Jiri Slaby037ad482006-12-08 02:38:11 -08001599
1600 switch (cmd) {
1601 case MOXA_GET_CONF:
Jiri Slaby55b307d2006-12-08 02:38:14 -08001602/* if (copy_to_user(argp, mxsercfg,
Jiri Slaby037ad482006-12-08 02:38:11 -08001603 sizeof(struct mxser_hwconf) * 4))
1604 return -EFAULT;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001605 return 0;*/
1606 return -ENXIO;
Jiri Slaby037ad482006-12-08 02:38:11 -08001607 case MOXA_GET_MAJOR:
1608 if (copy_to_user(argp, &ttymajor, sizeof(int)))
1609 return -EFAULT;
1610 return 0;
1611
1612 case MOXA_GET_CUMAJOR:
1613 if (copy_to_user(argp, &calloutmajor, sizeof(int)))
1614 return -EFAULT;
1615 return 0;
1616
1617 case MOXA_CHKPORTENABLE:
1618 result = 0;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001619
1620 for (i = 0; i < MXSER_BOARDS; i++)
1621 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++)
1622 if (mxser_boards[i].ports[j].ioaddr)
1623 result |= (1 << i);
1624
Jiri Slaby037ad482006-12-08 02:38:11 -08001625 return put_user(result, (unsigned long __user *)argp);
1626 case MOXA_GETDATACOUNT:
1627 if (copy_to_user(argp, &mxvar_log, sizeof(mxvar_log)))
1628 return -EFAULT;
1629 return 0;
1630 case MOXA_GETMSTATUS:
Jiri Slaby55b307d2006-12-08 02:38:14 -08001631 for (i = 0; i < MXSER_BOARDS; i++)
1632 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
1633 port = &mxser_boards[i].ports[j];
1634
1635 GMStatus[i].ri = 0;
1636 if (!port->ioaddr) {
1637 GMStatus[i].dcd = 0;
1638 GMStatus[i].dsr = 0;
1639 GMStatus[i].cts = 0;
1640 continue;
1641 }
1642
1643 if (!port->tty || !port->tty->termios)
1644 GMStatus[i].cflag =
1645 port->normal_termios.c_cflag;
1646 else
1647 GMStatus[i].cflag =
1648 port->tty->termios->c_cflag;
1649
1650 status = inb(port->ioaddr + UART_MSR);
1651 if (status & 0x80 /*UART_MSR_DCD */ )
1652 GMStatus[i].dcd = 1;
1653 else
1654 GMStatus[i].dcd = 0;
1655
1656 if (status & 0x20 /*UART_MSR_DSR */ )
1657 GMStatus[i].dsr = 1;
1658 else
1659 GMStatus[i].dsr = 0;
1660
1661
1662 if (status & 0x10 /*UART_MSR_CTS */ )
1663 GMStatus[i].cts = 1;
1664 else
1665 GMStatus[i].cts = 0;
Jiri Slaby037ad482006-12-08 02:38:11 -08001666 }
Jiri Slaby037ad482006-12-08 02:38:11 -08001667 if (copy_to_user(argp, GMStatus,
1668 sizeof(struct mxser_mstatus) * MXSER_PORTS))
1669 return -EFAULT;
1670 return 0;
1671 case MOXA_ASPP_MON_EXT: {
Jiri Slaby55b307d2006-12-08 02:38:14 -08001672 int status, p, shiftbit;
1673 unsigned long opmode;
1674 unsigned cflag, iflag;
Jiri Slaby037ad482006-12-08 02:38:11 -08001675
Jiri Slaby55b307d2006-12-08 02:38:14 -08001676 for (i = 0; i < MXSER_BOARDS; i++)
1677 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
1678 port = &mxser_boards[i].ports[j];
1679 if (!port->ioaddr)
Jiri Slaby037ad482006-12-08 02:38:11 -08001680 continue;
1681
Jiri Slaby55b307d2006-12-08 02:38:14 -08001682 status = mxser_get_msr(port->ioaddr, 0, i);
1683/* mxser_check_modem_status(port, status); */
1684
Jiri Slaby037ad482006-12-08 02:38:11 -08001685 if (status & UART_MSR_TERI)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001686 port->icount.rng++;
Jiri Slaby037ad482006-12-08 02:38:11 -08001687 if (status & UART_MSR_DDSR)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001688 port->icount.dsr++;
Jiri Slaby037ad482006-12-08 02:38:11 -08001689 if (status & UART_MSR_DDCD)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001690 port->icount.dcd++;
Jiri Slaby037ad482006-12-08 02:38:11 -08001691 if (status & UART_MSR_DCTS)
Jiri Slaby55b307d2006-12-08 02:38:14 -08001692 port->icount.cts++;
Jiri Slaby037ad482006-12-08 02:38:11 -08001693
Jiri Slaby55b307d2006-12-08 02:38:14 -08001694 port->mon_data.modem_status = status;
1695 mon_data_ext.rx_cnt[i] = port->mon_data.rxcnt;
1696 mon_data_ext.tx_cnt[i] = port->mon_data.txcnt;
1697 mon_data_ext.up_rxcnt[i] =
1698 port->mon_data.up_rxcnt;
1699 mon_data_ext.up_txcnt[i] =
1700 port->mon_data.up_txcnt;
1701 mon_data_ext.modem_status[i] =
1702 port->mon_data.modem_status;
1703 mon_data_ext.baudrate[i] = port->realbaud;
Jiri Slaby037ad482006-12-08 02:38:11 -08001704
Jiri Slaby55b307d2006-12-08 02:38:14 -08001705 if (!port->tty || !port->tty->termios) {
1706 cflag = port->normal_termios.c_cflag;
1707 iflag = port->normal_termios.c_iflag;
Jiri Slaby037ad482006-12-08 02:38:11 -08001708 } else {
Jiri Slaby55b307d2006-12-08 02:38:14 -08001709 cflag = port->tty->termios->c_cflag;
1710 iflag = port->tty->termios->c_iflag;
Jiri Slaby037ad482006-12-08 02:38:11 -08001711 }
1712
1713 mon_data_ext.databits[i] = cflag & CSIZE;
1714
1715 mon_data_ext.stopbits[i] = cflag & CSTOPB;
1716
Jiri Slaby55b307d2006-12-08 02:38:14 -08001717 mon_data_ext.parity[i] =
1718 cflag & (PARENB | PARODD | CMSPAR);
Jiri Slaby037ad482006-12-08 02:38:11 -08001719
1720 mon_data_ext.flowctrl[i] = 0x00;
1721
1722 if (cflag & CRTSCTS)
1723 mon_data_ext.flowctrl[i] |= 0x03;
1724
1725 if (iflag & (IXON | IXOFF))
1726 mon_data_ext.flowctrl[i] |= 0x0C;
1727
Jiri Slaby55b307d2006-12-08 02:38:14 -08001728 if (port->type == PORT_16550A)
Jiri Slaby037ad482006-12-08 02:38:11 -08001729 mon_data_ext.fifo[i] = 1;
1730 else
1731 mon_data_ext.fifo[i] = 0;
1732
1733 p = i % 4;
1734 shiftbit = p * 2;
Jiri Slaby55b307d2006-12-08 02:38:14 -08001735 opmode = inb(port->opmode_ioaddr) >> shiftbit;
Jiri Slaby037ad482006-12-08 02:38:11 -08001736 opmode &= OP_MODE_MASK;
1737
1738 mon_data_ext.iftype[i] = opmode;
1739
1740 }
Jiri Slaby55b307d2006-12-08 02:38:14 -08001741 if (copy_to_user(argp, &mon_data_ext,
1742 sizeof(mon_data_ext)))
Jiri Slaby037ad482006-12-08 02:38:11 -08001743 return -EFAULT;
1744
1745 return 0;
1746
Jiri Slaby55b307d2006-12-08 02:38:14 -08001747 } default:
Jiri Slaby037ad482006-12-08 02:38:11 -08001748 return -ENOIOCTLCMD;
1749 }
1750 return 0;
1751}
1752
Jiri Slabya8dea4e2006-12-08 02:38:21 -08001753static int mxser_ioctl(struct tty_struct *tty, struct file *file,
1754 unsigned int cmd, unsigned long arg)
1755{
1756 struct mxser_port *info = tty->driver_data;
1757 struct async_icount cprev, cnow; /* kernel counter temps */
1758 struct serial_icounter_struct __user *p_cuser;
1759 unsigned long templ;
1760 unsigned long flags;
1761 void __user *argp = (void __user *)arg;
1762 int retval;
1763
1764 if (tty->index == MXSER_PORTS)
1765 return mxser_ioctl_special(cmd, argp);
1766
1767 /* following add by Victor Yu. 01-05-2004 */
1768 if (cmd == MOXA_SET_OP_MODE || cmd == MOXA_GET_OP_MODE) {
1769 int p;
1770 unsigned long opmode;
1771 static unsigned char ModeMask[] = { 0xfc, 0xf3, 0xcf, 0x3f };
1772 int shiftbit;
1773 unsigned char val, mask;
1774
1775 p = tty->index % 4;
1776 if (cmd == MOXA_SET_OP_MODE) {
1777 if (get_user(opmode, (int __user *) argp))
1778 return -EFAULT;
1779 if (opmode != RS232_MODE &&
1780 opmode != RS485_2WIRE_MODE &&
1781 opmode != RS422_MODE &&
1782 opmode != RS485_4WIRE_MODE)
1783 return -EFAULT;
1784 mask = ModeMask[p];
1785 shiftbit = p * 2;
1786 val = inb(info->opmode_ioaddr);
1787 val &= mask;
1788 val |= (opmode << shiftbit);
1789 outb(val, info->opmode_ioaddr);
1790 } else {
1791 shiftbit = p * 2;
1792 opmode = inb(info->opmode_ioaddr) >> shiftbit;
1793 opmode &= OP_MODE_MASK;
1794 if (copy_to_user(argp, &opmode, sizeof(int)))
1795 return -EFAULT;
1796 }
1797 return 0;
1798 }
1799 /* above add by Victor Yu. 01-05-2004 */
1800
1801 if ((cmd != TIOCGSERIAL) && (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
1802 if (tty->flags & (1 << TTY_IO_ERROR))
1803 return -EIO;
1804 }
1805 switch (cmd) {
1806 case TCSBRK: /* SVID version: non-zero arg --> no break */
1807 retval = tty_check_change(tty);
1808 if (retval)
1809 return retval;
1810 tty_wait_until_sent(tty, 0);
1811 if (!arg)
1812 mxser_send_break(info, HZ / 4); /* 1/4 second */
1813 return 0;
1814 case TCSBRKP: /* support for POSIX tcsendbreak() */
1815 retval = tty_check_change(tty);
1816 if (retval)
1817 return retval;
1818 tty_wait_until_sent(tty, 0);
1819 mxser_send_break(info, arg ? arg * (HZ / 10) : HZ / 4);
1820 return 0;
1821 case TIOCGSOFTCAR:
1822 return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *)argp);
1823 case TIOCSSOFTCAR:
1824 if (get_user(templ, (unsigned long __user *) argp))
1825 return -EFAULT;
1826 arg = templ;
1827 tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) | (arg ? CLOCAL : 0));
1828 return 0;
1829 case TIOCGSERIAL:
1830 return mxser_get_serial_info(info, argp);
1831 case TIOCSSERIAL:
1832 return mxser_set_serial_info(info, argp);
1833 case TIOCSERGETLSR: /* Get line status register */
1834 return mxser_get_lsr_info(info, argp);
1835 /*
1836 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1837 * - mask passed in arg for lines of interest
1838 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1839 * Caller should use TIOCGICOUNT to see which one it was
1840 */
1841 case TIOCMIWAIT: {
1842 DECLARE_WAITQUEUE(wait, current);
1843 int ret;
1844 spin_lock_irqsave(&info->slock, flags);
1845 cprev = info->icount; /* note the counters on entry */
1846 spin_unlock_irqrestore(&info->slock, flags);
1847
1848 add_wait_queue(&info->delta_msr_wait, &wait);
1849 while (1) {
1850 spin_lock_irqsave(&info->slock, flags);
1851 cnow = info->icount; /* atomic copy */
1852 spin_unlock_irqrestore(&info->slock, flags);
1853
1854 set_current_state(TASK_INTERRUPTIBLE);
1855 if (((arg & TIOCM_RNG) &&
1856 (cnow.rng != cprev.rng)) ||
1857 ((arg & TIOCM_DSR) &&
1858 (cnow.dsr != cprev.dsr)) ||
1859 ((arg & TIOCM_CD) &&
1860 (cnow.dcd != cprev.dcd)) ||
1861 ((arg & TIOCM_CTS) &&
1862 (cnow.cts != cprev.cts))) {
1863 ret = 0;
1864 break;
1865 }
1866 /* see if a signal did it */
1867 if (signal_pending(current)) {
1868 ret = -ERESTARTSYS;
1869 break;
1870 }
1871 cprev = cnow;
1872 }
1873 current->state = TASK_RUNNING;
1874 remove_wait_queue(&info->delta_msr_wait, &wait);
1875 break;
1876 }
1877 /* NOTREACHED */
1878 /*
1879 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1880 * Return: write counters to the user passed counter struct
1881 * NB: both 1->0 and 0->1 transitions are counted except for
1882 * RI where only 0->1 is counted.
1883 */
1884 case TIOCGICOUNT:
1885 spin_lock_irqsave(&info->slock, flags);
1886 cnow = info->icount;
1887 spin_unlock_irqrestore(&info->slock, flags);
1888 p_cuser = argp;
1889 /* modified by casper 1/11/2000 */
1890 if (put_user(cnow.frame, &p_cuser->frame))
1891 return -EFAULT;
1892 if (put_user(cnow.brk, &p_cuser->brk))
1893 return -EFAULT;
1894 if (put_user(cnow.overrun, &p_cuser->overrun))
1895 return -EFAULT;
1896 if (put_user(cnow.buf_overrun, &p_cuser->buf_overrun))
1897 return -EFAULT;
1898 if (put_user(cnow.parity, &p_cuser->parity))
1899 return -EFAULT;
1900 if (put_user(cnow.rx, &p_cuser->rx))
1901 return -EFAULT;
1902 if (put_user(cnow.tx, &p_cuser->tx))
1903 return -EFAULT;
1904 put_user(cnow.cts, &p_cuser->cts);
1905 put_user(cnow.dsr, &p_cuser->dsr);
1906 put_user(cnow.rng, &p_cuser->rng);
1907 put_user(cnow.dcd, &p_cuser->dcd);
1908 return 0;
1909 case MOXA_HighSpeedOn:
1910 return put_user(info->baud_base != 115200 ? 1 : 0, (int __user *)argp);
1911 case MOXA_SDS_RSTICOUNTER:
1912 info->mon_data.rxcnt = 0;
1913 info->mon_data.txcnt = 0;
1914 return 0;
1915/* (above) added by James. */
1916 case MOXA_ASPP_SETBAUD:{
1917 long baud;
1918 if (get_user(baud, (long __user *)argp))
1919 return -EFAULT;
1920 mxser_set_baud(info, baud);
1921 return 0;
1922 }
1923 case MOXA_ASPP_GETBAUD:
1924 if (copy_to_user(argp, &info->realbaud, sizeof(long)))
1925 return -EFAULT;
1926
1927 return 0;
1928
1929 case MOXA_ASPP_OQUEUE:{
1930 int len, lsr;
1931
1932 len = mxser_chars_in_buffer(tty);
1933
1934 lsr = inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT;
1935
1936 len += (lsr ? 0 : 1);
1937
1938 if (copy_to_user(argp, &len, sizeof(int)))
1939 return -EFAULT;
1940
1941 return 0;
1942 }
1943 case MOXA_ASPP_MON: {
1944 int mcr, status;
1945
1946 /* info->mon_data.ser_param = tty->termios->c_cflag; */
1947
1948 status = mxser_get_msr(info->ioaddr, 1, tty->index);
1949 mxser_check_modem_status(info, status);
1950
1951 mcr = inb(info->ioaddr + UART_MCR);
1952 if (mcr & MOXA_MUST_MCR_XON_FLAG)
1953 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFHOLD;
1954 else
1955 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFHOLD;
1956
1957 if (mcr & MOXA_MUST_MCR_TX_XON)
1958 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFXENT;
1959 else
1960 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFXENT;
1961
1962 if (info->tty->hw_stopped)
1963 info->mon_data.hold_reason |= NPPI_NOTIFY_CTSHOLD;
1964 else
1965 info->mon_data.hold_reason &= ~NPPI_NOTIFY_CTSHOLD;
1966
1967 if (copy_to_user(argp, &info->mon_data,
1968 sizeof(struct mxser_mon)))
1969 return -EFAULT;
1970
1971 return 0;
1972 }
1973 case MOXA_ASPP_LSTATUS: {
1974 if (copy_to_user(argp, &info->err_shadow,
1975 sizeof(unsigned char)))
1976 return -EFAULT;
1977
1978 info->err_shadow = 0;
1979 return 0;
1980 }
1981 case MOXA_SET_BAUD_METHOD: {
1982 int method;
1983
1984 if (get_user(method, (int __user *)argp))
1985 return -EFAULT;
1986 mxser_set_baud_method[tty->index] = method;
1987 if (copy_to_user(argp, &method, sizeof(int)))
1988 return -EFAULT;
1989
1990 return 0;
1991 }
1992 default:
1993 return -ENOIOCTLCMD;
1994 }
1995 return 0;
1996}
1997
Jiri Slaby037ad482006-12-08 02:38:11 -08001998static void mxser_stoprx(struct tty_struct *tty)
1999{
Jiri Slaby55b307d2006-12-08 02:38:14 -08002000 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08002001
2002 info->ldisc_stop_rx = 1;
2003 if (I_IXOFF(tty)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002004 /* following add by Victor Yu. 09-02-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002005 if (info->board->chip_flag) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002006 info->IER &= ~MOXA_MUST_RECV_ISR;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002007 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby3306ce32006-12-08 02:38:13 -08002008 } else if (!(info->flags & ASYNC_CLOSING)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002009 info->x_char = STOP_CHAR(tty);
Jiri Slaby55b307d2006-12-08 02:38:14 -08002010 outb(0, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08002011 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002012 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby3306ce32006-12-08 02:38:13 -08002013 }
Jiri Slaby037ad482006-12-08 02:38:11 -08002014 }
2015
2016 if (info->tty->termios->c_cflag & CRTSCTS) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002017 info->MCR &= ~UART_MCR_RTS;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002018 outb(info->MCR, info->ioaddr + UART_MCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002019 }
2020}
2021
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002022/*
2023 * This routine is called by the upper-layer tty layer to signal that
2024 * incoming characters should be throttled.
2025 */
2026static void mxser_throttle(struct tty_struct *tty)
2027{
2028 mxser_stoprx(tty);
2029}
2030
2031static void mxser_unthrottle(struct tty_struct *tty)
Jiri Slaby037ad482006-12-08 02:38:11 -08002032{
Jiri Slaby55b307d2006-12-08 02:38:14 -08002033 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08002034
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002035 /* startrx */
Jiri Slaby037ad482006-12-08 02:38:11 -08002036 info->ldisc_stop_rx = 0;
2037 if (I_IXOFF(tty)) {
2038 if (info->x_char)
2039 info->x_char = 0;
2040 else {
Jiri Slaby037ad482006-12-08 02:38:11 -08002041 /* following add by Victor Yu. 09-02-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002042 if (info->board->chip_flag) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002043 info->IER |= MOXA_MUST_RECV_ISR;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002044 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby3306ce32006-12-08 02:38:13 -08002045 } else if (!(info->flags & ASYNC_CLOSING)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002046 info->x_char = START_CHAR(tty);
Jiri Slaby55b307d2006-12-08 02:38:14 -08002047 outb(0, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08002048 info->IER |= UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002049 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby3306ce32006-12-08 02:38:13 -08002050 }
Jiri Slaby037ad482006-12-08 02:38:11 -08002051 }
2052 }
2053
2054 if (info->tty->termios->c_cflag & CRTSCTS) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002055 info->MCR |= UART_MCR_RTS;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002056 outb(info->MCR, info->ioaddr + UART_MCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002057 }
2058}
2059
2060/*
Jiri Slaby037ad482006-12-08 02:38:11 -08002061 * mxser_stop() and mxser_start()
2062 *
2063 * This routines are called before setting or resetting tty->stopped.
2064 * They enable or disable transmitter interrupts, as necessary.
2065 */
2066static void mxser_stop(struct tty_struct *tty)
2067{
Jiri Slaby55b307d2006-12-08 02:38:14 -08002068 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08002069 unsigned long flags;
2070
2071 spin_lock_irqsave(&info->slock, flags);
2072 if (info->IER & UART_IER_THRI) {
2073 info->IER &= ~UART_IER_THRI;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002074 outb(info->IER, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08002075 }
2076 spin_unlock_irqrestore(&info->slock, flags);
2077}
2078
2079static void mxser_start(struct tty_struct *tty)
2080{
Jiri Slaby55b307d2006-12-08 02:38:14 -08002081 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08002082 unsigned long flags;
2083
2084 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby3306ce32006-12-08 02:38:13 -08002085 if (info->xmit_cnt && info->xmit_buf
2086 /* && !(info->IER & UART_IER_THRI) */) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002087 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08002088 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
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002094static void mxser_set_termios(struct tty_struct *tty, struct termios *old_termios)
2095{
2096 struct mxser_port *info = tty->driver_data;
2097 unsigned long flags;
2098
2099 if ((tty->termios->c_cflag != old_termios->c_cflag) ||
2100 (RELEVANT_IFLAG(tty->termios->c_iflag) != RELEVANT_IFLAG(old_termios->c_iflag))) {
2101
2102 mxser_change_speed(info, old_termios);
2103
2104 if ((old_termios->c_cflag & CRTSCTS) &&
2105 !(tty->termios->c_cflag & CRTSCTS)) {
2106 tty->hw_stopped = 0;
2107 mxser_start(tty);
2108 }
2109 }
2110
2111/* Handle sw stopped */
2112 if ((old_termios->c_iflag & IXON) &&
2113 !(tty->termios->c_iflag & IXON)) {
2114 tty->stopped = 0;
2115
2116 /* following add by Victor Yu. 09-02-2002 */
2117 if (info->board->chip_flag) {
2118 spin_lock_irqsave(&info->slock, flags);
2119 DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->ioaddr);
2120 spin_unlock_irqrestore(&info->slock, flags);
2121 }
2122 /* above add by Victor Yu. 09-02-2002 */
2123
2124 mxser_start(tty);
2125 }
2126}
2127
Jiri Slaby037ad482006-12-08 02:38:11 -08002128/*
2129 * mxser_wait_until_sent() --- wait until the transmitter is empty
2130 */
2131static void mxser_wait_until_sent(struct tty_struct *tty, int timeout)
2132{
Jiri Slaby55b307d2006-12-08 02:38:14 -08002133 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08002134 unsigned long orig_jiffies, char_time;
2135 int lsr;
2136
2137 if (info->type == PORT_UNKNOWN)
2138 return;
2139
2140 if (info->xmit_fifo_size == 0)
2141 return; /* Just in case.... */
2142
2143 orig_jiffies = jiffies;
2144 /*
2145 * Set the check interval to be 1/5 of the estimated time to
2146 * send a single character, and make it at least 1. The check
2147 * interval should also be less than the timeout.
2148 *
2149 * Note: we have to use pretty tight timings here to satisfy
2150 * the NIST-PCTS.
2151 */
2152 char_time = (info->timeout - HZ / 50) / info->xmit_fifo_size;
2153 char_time = char_time / 5;
2154 if (char_time == 0)
2155 char_time = 1;
2156 if (timeout && timeout < char_time)
2157 char_time = timeout;
2158 /*
2159 * If the transmitter hasn't cleared in twice the approximate
2160 * amount of time to send the entire FIFO, it probably won't
2161 * ever clear. This assumes the UART isn't doing flow
2162 * control, which is currently the case. Hence, if it ever
2163 * takes longer than info->timeout, this is probably due to a
2164 * UART bug of some kind. So, we clamp the timeout parameter at
2165 * 2*info->timeout.
2166 */
2167 if (!timeout || timeout > 2 * info->timeout)
2168 timeout = 2 * info->timeout;
2169#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2170 printk(KERN_DEBUG "In rs_wait_until_sent(%d) check=%lu...",
2171 timeout, char_time);
2172 printk("jiff=%lu...", jiffies);
2173#endif
Jiri Slaby55b307d2006-12-08 02:38:14 -08002174 while (!((lsr = inb(info->ioaddr + UART_LSR)) & UART_LSR_TEMT)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002175#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2176 printk("lsr = %d (jiff=%lu)...", lsr, jiffies);
2177#endif
2178 schedule_timeout_interruptible(char_time);
2179 if (signal_pending(current))
2180 break;
2181 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2182 break;
2183 }
2184 set_current_state(TASK_RUNNING);
2185
2186#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2187 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
2188#endif
2189}
2190
2191
2192/*
2193 * This routine is called by tty_hangup() when a hangup is signaled.
2194 */
2195void mxser_hangup(struct tty_struct *tty)
2196{
Jiri Slaby55b307d2006-12-08 02:38:14 -08002197 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08002198
2199 mxser_flush_buffer(tty);
2200 mxser_shutdown(info);
2201 info->event = 0;
2202 info->count = 0;
2203 info->flags &= ~ASYNC_NORMAL_ACTIVE;
2204 info->tty = NULL;
2205 wake_up_interruptible(&info->open_wait);
2206}
2207
2208
2209/* added by James 03-12-2004. */
2210/*
2211 * mxser_rs_break() --- routine which turns the break handling on or off
2212 */
2213static void mxser_rs_break(struct tty_struct *tty, int break_state)
2214{
Jiri Slaby55b307d2006-12-08 02:38:14 -08002215 struct mxser_port *info = tty->driver_data;
Jiri Slaby037ad482006-12-08 02:38:11 -08002216 unsigned long flags;
2217
2218 spin_lock_irqsave(&info->slock, flags);
2219 if (break_state == -1)
Jiri Slaby55b307d2006-12-08 02:38:14 -08002220 outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
2221 info->ioaddr + UART_LCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002222 else
Jiri Slaby55b307d2006-12-08 02:38:14 -08002223 outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
2224 info->ioaddr + UART_LCR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002225 spin_unlock_irqrestore(&info->slock, flags);
2226}
2227
2228/* (above) added by James. */
2229
Jiri Slaby55b307d2006-12-08 02:38:14 -08002230static void mxser_receive_chars(struct mxser_port *port, int *status)
Jiri Slaby037ad482006-12-08 02:38:11 -08002231{
Jiri Slaby55b307d2006-12-08 02:38:14 -08002232 struct tty_struct *tty = port->tty;
Jiri Slaby037ad482006-12-08 02:38:11 -08002233 unsigned char ch, gdl;
2234 int ignored = 0;
2235 int cnt = 0;
2236 int recv_room;
2237 int max = 256;
2238 unsigned long flags;
2239
Jiri Slaby55b307d2006-12-08 02:38:14 -08002240 spin_lock_irqsave(&port->slock, flags);
Jiri Slaby037ad482006-12-08 02:38:11 -08002241
2242 recv_room = tty->receive_room;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002243 if ((recv_room == 0) && (!port->ldisc_stop_rx)) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002244 /* mxser_throttle(tty); */
2245 mxser_stoprx(tty);
2246 /* return; */
2247 }
2248
2249 /* following add by Victor Yu. 09-02-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002250 if (port->board->chip_flag != MOXA_OTHER_UART) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002251
Jiri Slaby3306ce32006-12-08 02:38:13 -08002252 if (*status & UART_LSR_SPECIAL)
Jiri Slaby037ad482006-12-08 02:38:11 -08002253 goto intr_old;
Jiri Slaby037ad482006-12-08 02:38:11 -08002254 /* following add by Victor Yu. 02-11-2004 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002255 if (port->board->chip_flag == MOXA_MUST_MU860_HWID &&
Jiri Slaby037ad482006-12-08 02:38:11 -08002256 (*status & MOXA_MUST_LSR_RERR))
2257 goto intr_old;
2258 /* above add by Victor Yu. 02-14-2004 */
2259 if (*status & MOXA_MUST_LSR_RERR)
2260 goto intr_old;
2261
Jiri Slaby55b307d2006-12-08 02:38:14 -08002262 gdl = inb(port->ioaddr + MOXA_MUST_GDL_REGISTER);
Jiri Slaby037ad482006-12-08 02:38:11 -08002263
2264 /* add by Victor Yu. 02-11-2004 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002265 if (port->board->chip_flag == MOXA_MUST_MU150_HWID)
Jiri Slaby037ad482006-12-08 02:38:11 -08002266 gdl &= MOXA_MUST_GDL_MASK;
2267 if (gdl >= recv_room) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002268 if (!port->ldisc_stop_rx) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002269 /* mxser_throttle(tty); */
2270 mxser_stoprx(tty);
2271 }
2272 /* return; */
2273 }
2274 while (gdl--) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002275 ch = inb(port->ioaddr + UART_RX);
Jiri Slaby037ad482006-12-08 02:38:11 -08002276 tty_insert_flip_char(tty, ch, 0);
2277 cnt++;
Jiri Slaby037ad482006-12-08 02:38:11 -08002278 }
2279 goto end_intr;
2280 }
2281 intr_old:
2282 /* above add by Victor Yu. 09-02-2002 */
2283
2284 do {
2285 if (max-- < 0)
2286 break;
Jiri Slaby037ad482006-12-08 02:38:11 -08002287
Jiri Slaby55b307d2006-12-08 02:38:14 -08002288 ch = inb(port->ioaddr + UART_RX);
Jiri Slaby037ad482006-12-08 02:38:11 -08002289 /* following add by Victor Yu. 09-02-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002290 if (port->board->chip_flag && (*status & UART_LSR_OE)
Jiri Slaby3306ce32006-12-08 02:38:13 -08002291 /*&& !(*status&UART_LSR_DR) */)
Jiri Slaby55b307d2006-12-08 02:38:14 -08002292 outb(0x23, port->ioaddr + UART_FCR);
2293 *status &= port->read_status_mask;
Jiri Slaby037ad482006-12-08 02:38:11 -08002294 /* above add by Victor Yu. 09-02-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002295 if (*status & port->ignore_status_mask) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002296 if (++ignored > 100)
2297 break;
2298 } else {
2299 char flag = 0;
2300 if (*status & UART_LSR_SPECIAL) {
2301 if (*status & UART_LSR_BI) {
2302 flag = TTY_BREAK;
2303/* added by casper 1/11/2000 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002304 port->icount.brk++;
Jiri Slaby3306ce32006-12-08 02:38:13 -08002305
Jiri Slaby55b307d2006-12-08 02:38:14 -08002306 if (port->flags & ASYNC_SAK)
Jiri Slaby037ad482006-12-08 02:38:11 -08002307 do_SAK(tty);
2308 } else if (*status & UART_LSR_PE) {
2309 flag = TTY_PARITY;
2310/* added by casper 1/11/2000 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002311 port->icount.parity++;
Jiri Slaby037ad482006-12-08 02:38:11 -08002312 } else if (*status & UART_LSR_FE) {
2313 flag = TTY_FRAME;
2314/* added by casper 1/11/2000 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002315 port->icount.frame++;
Jiri Slaby037ad482006-12-08 02:38:11 -08002316 } else if (*status & UART_LSR_OE) {
2317 flag = TTY_OVERRUN;
2318/* added by casper 1/11/2000 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002319 port->icount.overrun++;
Jiri Slaby3306ce32006-12-08 02:38:13 -08002320 } else
2321 flags = TTY_BREAK;
2322 } else
2323 flags = 0;
Jiri Slaby037ad482006-12-08 02:38:11 -08002324 tty_insert_flip_char(tty, ch, flag);
2325 cnt++;
2326 if (cnt >= recv_room) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002327 if (!port->ldisc_stop_rx) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002328 /* mxser_throttle(tty); */
2329 mxser_stoprx(tty);
2330 }
2331 break;
2332 }
2333
2334 }
2335
2336 /* following add by Victor Yu. 09-02-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002337 if (port->board->chip_flag)
Jiri Slaby037ad482006-12-08 02:38:11 -08002338 break;
Jiri Slaby037ad482006-12-08 02:38:11 -08002339
2340 /* mask by Victor Yu. 09-02-2002
Jiri Slaby55b307d2006-12-08 02:38:14 -08002341 *status = inb(port->ioaddr + UART_LSR) & port->read_status_mask;
Jiri Slaby037ad482006-12-08 02:38:11 -08002342 */
2343 /* following add by Victor Yu. 09-02-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002344 *status = inb(port->ioaddr + UART_LSR);
Jiri Slaby037ad482006-12-08 02:38:11 -08002345 /* above add by Victor Yu. 09-02-2002 */
2346 } while (*status & UART_LSR_DR);
2347
2348end_intr: /* add by Victor Yu. 09-02-2002 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002349 mxvar_log.rxcnt[port->tty->index] += cnt;
2350 port->mon_data.rxcnt += cnt;
2351 port->mon_data.up_rxcnt += cnt;
2352 spin_unlock_irqrestore(&port->slock, flags);
Jiri Slaby037ad482006-12-08 02:38:11 -08002353
2354 tty_flip_buffer_push(tty);
2355}
2356
Jiri Slaby55b307d2006-12-08 02:38:14 -08002357static void mxser_transmit_chars(struct mxser_port *port)
Jiri Slaby037ad482006-12-08 02:38:11 -08002358{
2359 int count, cnt;
2360 unsigned long flags;
2361
Jiri Slaby55b307d2006-12-08 02:38:14 -08002362 spin_lock_irqsave(&port->slock, flags);
Jiri Slaby037ad482006-12-08 02:38:11 -08002363
Jiri Slaby55b307d2006-12-08 02:38:14 -08002364 if (port->x_char) {
2365 outb(port->x_char, port->ioaddr + UART_TX);
2366 port->x_char = 0;
2367 mxvar_log.txcnt[port->tty->index]++;
2368 port->mon_data.txcnt++;
2369 port->mon_data.up_txcnt++;
Jiri Slaby037ad482006-12-08 02:38:11 -08002370
2371/* added by casper 1/11/2000 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002372 port->icount.tx++;
Jiri Slaby3306ce32006-12-08 02:38:13 -08002373 goto unlock;
Jiri Slaby037ad482006-12-08 02:38:11 -08002374 }
2375
Jiri Slaby55b307d2006-12-08 02:38:14 -08002376 if (port->xmit_buf == 0)
Jiri Slaby3306ce32006-12-08 02:38:13 -08002377 goto unlock;
Jiri Slaby037ad482006-12-08 02:38:11 -08002378
Jiri Slaby55b307d2006-12-08 02:38:14 -08002379 if (port->xmit_cnt == 0) {
2380 if (port->xmit_cnt < WAKEUP_CHARS) { /* XXX what's this for?? */
2381 set_bit(MXSER_EVENT_TXLOW, &port->event);
2382 schedule_work(&port->tqueue);
Jiri Slaby3306ce32006-12-08 02:38:13 -08002383 }
2384 goto unlock;
2385 }
Jiri Slaby55b307d2006-12-08 02:38:14 -08002386 if (port->tty->stopped || (port->tty->hw_stopped &&
2387 (port->type != PORT_16550A) &&
2388 (!port->board->chip_flag))) {
2389 port->IER &= ~UART_IER_THRI;
2390 outb(port->IER, port->ioaddr + UART_IER);
Jiri Slaby3306ce32006-12-08 02:38:13 -08002391 goto unlock;
Jiri Slaby037ad482006-12-08 02:38:11 -08002392 }
2393
Jiri Slaby55b307d2006-12-08 02:38:14 -08002394 cnt = port->xmit_cnt;
2395 count = port->xmit_fifo_size;
Jiri Slaby037ad482006-12-08 02:38:11 -08002396 do {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002397 outb(port->xmit_buf[port->xmit_tail++],
2398 port->ioaddr + UART_TX);
2399 port->xmit_tail = port->xmit_tail & (SERIAL_XMIT_SIZE - 1);
2400 if (--port->xmit_cnt <= 0)
Jiri Slaby037ad482006-12-08 02:38:11 -08002401 break;
2402 } while (--count > 0);
Jiri Slaby55b307d2006-12-08 02:38:14 -08002403 mxvar_log.txcnt[port->tty->index] += (cnt - port->xmit_cnt);
Jiri Slaby037ad482006-12-08 02:38:11 -08002404
2405/* added by James 03-12-2004. */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002406 port->mon_data.txcnt += (cnt - port->xmit_cnt);
2407 port->mon_data.up_txcnt += (cnt - port->xmit_cnt);
Jiri Slaby037ad482006-12-08 02:38:11 -08002408
2409/* added by casper 1/11/2000 */
Jiri Slaby55b307d2006-12-08 02:38:14 -08002410 port->icount.tx += (cnt - port->xmit_cnt);
Jiri Slaby037ad482006-12-08 02:38:11 -08002411
Jiri Slaby55b307d2006-12-08 02:38:14 -08002412 if (port->xmit_cnt < WAKEUP_CHARS) {
2413 set_bit(MXSER_EVENT_TXLOW, &port->event);
2414 schedule_work(&port->tqueue);
Jiri Slaby037ad482006-12-08 02:38:11 -08002415 }
Jiri Slaby55b307d2006-12-08 02:38:14 -08002416 if (port->xmit_cnt <= 0) {
2417 port->IER &= ~UART_IER_THRI;
2418 outb(port->IER, port->ioaddr + UART_IER);
Jiri Slaby037ad482006-12-08 02:38:11 -08002419 }
Jiri Slaby3306ce32006-12-08 02:38:13 -08002420unlock:
Jiri Slaby55b307d2006-12-08 02:38:14 -08002421 spin_unlock_irqrestore(&port->slock, flags);
Jiri Slaby037ad482006-12-08 02:38:11 -08002422}
2423
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002424/*
2425 * This is the serial driver's generic interrupt routine
2426 */
2427static irqreturn_t mxser_interrupt(int irq, void *dev_id, struct pt_regs *regs)
Jiri Slaby037ad482006-12-08 02:38:11 -08002428{
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002429 int status, iir, i;
2430 struct mxser_board *brd = NULL;
2431 struct mxser_port *port;
2432 int max, irqbits, bits, msr;
2433 int pass_counter = 0;
2434 unsigned int int_cnt;
2435 int handled = IRQ_NONE;
Jiri Slaby037ad482006-12-08 02:38:11 -08002436
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002437 /* spin_lock(&gm_lock); */
Jiri Slaby037ad482006-12-08 02:38:11 -08002438
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002439 for (i = 0; i < MXSER_BOARDS; i++)
2440 if (dev_id == &mxser_boards[i]) {
2441 brd = dev_id;
2442 break;
Jiri Slaby037ad482006-12-08 02:38:11 -08002443 }
Jiri Slaby037ad482006-12-08 02:38:11 -08002444
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002445 if (i == MXSER_BOARDS)
2446 goto irq_stop;
2447 if (brd == NULL)
2448 goto irq_stop;
2449 max = mxser_numports[brd->board_type - 1];
Jiri Slaby037ad482006-12-08 02:38:11 -08002450 while (1) {
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002451 irqbits = inb(brd->vector) & brd->vector_mask;
2452 if (irqbits == brd->vector_mask)
Jiri Slaby037ad482006-12-08 02:38:11 -08002453 break;
Jiri Slaby037ad482006-12-08 02:38:11 -08002454
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002455 handled = IRQ_HANDLED;
2456 for (i = 0, bits = 1; i < max; i++, irqbits |= bits, bits <<= 1) {
2457 if (irqbits == brd->vector_mask)
Jiri Slaby037ad482006-12-08 02:38:11 -08002458 break;
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002459 if (bits & irqbits)
2460 continue;
2461 port = &brd->ports[i];
Jiri Slaby037ad482006-12-08 02:38:11 -08002462
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002463 int_cnt = 0;
2464 do {
2465 /* following add by Victor Yu. 09-13-2002 */
2466 iir = inb(port->ioaddr + UART_IIR);
2467 if (iir & UART_IIR_NO_INT)
2468 break;
2469 iir &= MOXA_MUST_IIR_MASK;
2470 if (!port->tty) {
2471 status = inb(port->ioaddr + UART_LSR);
2472 outb(0x27, port->ioaddr + UART_FCR);
2473 inb(port->ioaddr + UART_MSR);
2474 break;
Jiri Slaby037ad482006-12-08 02:38:11 -08002475 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002476 /* above add by Victor Yu. 09-13-2002 */
Jiri Slaby037ad482006-12-08 02:38:11 -08002477
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002478 /* following add by Victor Yu. 09-02-2002 */
2479 status = inb(port->ioaddr + UART_LSR);
2480
2481 if (status & UART_LSR_PE)
2482 port->err_shadow |= NPPI_NOTIFY_PARITY;
2483 if (status & UART_LSR_FE)
2484 port->err_shadow |= NPPI_NOTIFY_FRAMING;
2485 if (status & UART_LSR_OE)
2486 port->err_shadow |=
2487 NPPI_NOTIFY_HW_OVERRUN;
2488 if (status & UART_LSR_BI)
2489 port->err_shadow |= NPPI_NOTIFY_BREAK;
2490
2491 if (port->board->chip_flag) {
2492 /*
2493 if ( (status & 0x02) && !(status & 0x01) ) {
2494 outb(port->ioaddr+UART_FCR, 0x23);
2495 continue;
2496 }
2497 */
2498 if (iir == MOXA_MUST_IIR_GDA ||
2499 iir == MOXA_MUST_IIR_RDA ||
2500 iir == MOXA_MUST_IIR_RTO ||
2501 iir == MOXA_MUST_IIR_LSR)
2502 mxser_receive_chars(port,
2503 &status);
2504
2505 } else {
2506 /* above add by Victor Yu. 09-02-2002 */
2507
2508 status &= port->read_status_mask;
2509 if (status & UART_LSR_DR)
2510 mxser_receive_chars(port,
2511 &status);
2512 }
2513 msr = inb(port->ioaddr + UART_MSR);
2514 if (msr & UART_MSR_ANY_DELTA)
2515 mxser_check_modem_status(port, msr);
2516
2517 /* following add by Victor Yu. 09-13-2002 */
2518 if (port->board->chip_flag) {
2519 if (iir == 0x02 && (status &
2520 UART_LSR_THRE))
2521 mxser_transmit_chars(port);
2522 } else {
2523 /* above add by Victor Yu. 09-13-2002 */
2524
2525 if (status & UART_LSR_THRE)
2526 mxser_transmit_chars(port);
2527 }
2528 } while (int_cnt++ < MXSER_ISR_PASS_LIMIT);
2529 }
2530 if (pass_counter++ > MXSER_ISR_PASS_LIMIT)
2531 break; /* Prevent infinite loops */
2532 }
2533
2534 irq_stop:
2535 /* spin_unlock(&gm_lock); */
2536 return handled;
2537}
2538
2539static const struct tty_operations mxser_ops = {
2540 .open = mxser_open,
2541 .close = mxser_close,
2542 .write = mxser_write,
2543 .put_char = mxser_put_char,
2544 .flush_chars = mxser_flush_chars,
2545 .write_room = mxser_write_room,
2546 .chars_in_buffer = mxser_chars_in_buffer,
2547 .flush_buffer = mxser_flush_buffer,
2548 .ioctl = mxser_ioctl,
2549 .throttle = mxser_throttle,
2550 .unthrottle = mxser_unthrottle,
2551 .set_termios = mxser_set_termios,
2552 .stop = mxser_stop,
2553 .start = mxser_start,
2554 .hangup = mxser_hangup,
2555 .break_ctl = mxser_rs_break,
2556 .wait_until_sent = mxser_wait_until_sent,
2557 .tiocmget = mxser_tiocmget,
2558 .tiocmset = mxser_tiocmset,
2559};
2560
2561/*
2562 * The MOXA Smartio/Industio serial driver boot-time initialization code!
2563 */
2564
2565static int __devinit mxser_initbrd(struct mxser_board *brd)
2566{
2567 struct mxser_port *info;
2568 unsigned int i;
2569 int retval;
2570
2571 printk(KERN_INFO "max. baud rate = %d bps.\n", brd->ports[0].max_baud);
2572
2573 for (i = 0; i < brd->nports; i++) {
2574 info = &brd->ports[i];
2575 info->board = brd;
2576 info->stop_rx = 0;
2577 info->ldisc_stop_rx = 0;
2578
2579 /* Enhance mode enabled here */
2580 if (brd->chip_flag != MOXA_OTHER_UART)
2581 ENABLE_MOXA_MUST_ENCHANCE_MODE(info->ioaddr);
2582
2583 info->flags = ASYNC_SHARE_IRQ;
2584 info->type = brd->uart_type;
2585
2586 process_txrx_fifo(info);
2587
2588 info->custom_divisor = info->baud_base * 16;
2589 info->close_delay = 5 * HZ / 10;
2590 info->closing_wait = 30 * HZ;
2591 INIT_WORK(&info->tqueue, mxser_do_softint, info);
2592 info->normal_termios = mxvar_sdriver->init_termios;
2593 init_waitqueue_head(&info->open_wait);
2594 init_waitqueue_head(&info->close_wait);
2595 init_waitqueue_head(&info->delta_msr_wait);
2596 memset(&info->mon_data, 0, sizeof(struct mxser_mon));
2597 info->err_shadow = 0;
2598 spin_lock_init(&info->slock);
2599
2600 /* before set INT ISR, disable all int */
2601 outb(inb(info->ioaddr + UART_IER) & 0xf0,
2602 info->ioaddr + UART_IER);
2603 }
Jiri Slaby037ad482006-12-08 02:38:11 -08002604 /*
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002605 * Allocate the IRQ if necessary
Jiri Slaby037ad482006-12-08 02:38:11 -08002606 */
Jiri Slaby037ad482006-12-08 02:38:11 -08002607
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002608 retval = request_irq(brd->irq, mxser_interrupt,
2609 (brd->ports[0].flags & ASYNC_SHARE_IRQ) ? IRQF_SHARED :
2610 IRQF_DISABLED, "mxser", brd);
2611 if (retval) {
2612 printk(KERN_ERR "Board %s: Request irq failed, IRQ (%d) may "
2613 "conflict with another device.\n",
2614 mxser_brdname[brd->board_type - 1], brd->irq);
2615 return retval;
Jiri Slaby037ad482006-12-08 02:38:11 -08002616 }
Jiri Slaby037ad482006-12-08 02:38:11 -08002617 return 0;
2618}
2619
Jiri Slaby943f2952006-12-08 02:38:15 -08002620static int __init mxser_get_ISA_conf(int cap, struct mxser_board *brd)
Jiri Slaby037ad482006-12-08 02:38:11 -08002621{
2622 int id, i, bits;
2623 unsigned short regs[16], irq;
2624 unsigned char scratch, scratch2;
2625
Jiri Slaby55b307d2006-12-08 02:38:14 -08002626 brd->chip_flag = MOXA_OTHER_UART;
Jiri Slaby037ad482006-12-08 02:38:11 -08002627
2628 id = mxser_read_register(cap, regs);
2629 if (id == C168_ASIC_ID) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002630 brd->board_type = MXSER_BOARD_C168_ISA;
2631 brd->nports = 8;
Jiri Slaby037ad482006-12-08 02:38:11 -08002632 } else if (id == C104_ASIC_ID) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002633 brd->board_type = MXSER_BOARD_C104_ISA;
2634 brd->nports = 4;
Jiri Slaby037ad482006-12-08 02:38:11 -08002635 } else if (id == C102_ASIC_ID) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002636 brd->board_type = MXSER_BOARD_C102_ISA;
2637 brd->nports = 2;
Jiri Slaby037ad482006-12-08 02:38:11 -08002638 } else if (id == CI132_ASIC_ID) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002639 brd->board_type = MXSER_BOARD_CI132;
2640 brd->nports = 2;
Jiri Slaby037ad482006-12-08 02:38:11 -08002641 } else if (id == CI134_ASIC_ID) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002642 brd->board_type = MXSER_BOARD_CI134;
2643 brd->nports = 4;
Jiri Slaby037ad482006-12-08 02:38:11 -08002644 } else if (id == CI104J_ASIC_ID) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002645 brd->board_type = MXSER_BOARD_CI104J;
2646 brd->nports = 4;
Jiri Slaby037ad482006-12-08 02:38:11 -08002647 } else
2648 return 0;
2649
2650 irq = 0;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002651 if (brd->nports == 2) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002652 irq = regs[9] & 0xF000;
2653 irq = irq | (irq >> 4);
2654 if (irq != (regs[9] & 0xFF00))
2655 return MXSER_ERR_IRQ_CONFLIT;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002656 } else if (brd->nports == 4) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002657 irq = regs[9] & 0xF000;
2658 irq = irq | (irq >> 4);
2659 irq = irq | (irq >> 8);
2660 if (irq != regs[9])
2661 return MXSER_ERR_IRQ_CONFLIT;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002662 } else if (brd->nports == 8) {
Jiri Slaby037ad482006-12-08 02:38:11 -08002663 irq = regs[9] & 0xF000;
2664 irq = irq | (irq >> 4);
2665 irq = irq | (irq >> 8);
2666 if ((irq != regs[9]) || (irq != regs[10]))
2667 return MXSER_ERR_IRQ_CONFLIT;
2668 }
2669
2670 if (!irq)
2671 return MXSER_ERR_IRQ;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002672 brd->irq = ((int)(irq & 0xF000) >> 12);
Jiri Slaby037ad482006-12-08 02:38:11 -08002673 for (i = 0; i < 8; i++)
Jiri Slaby55b307d2006-12-08 02:38:14 -08002674 brd->ports[i].ioaddr = (int) regs[i + 1] & 0xFFF8;
Jiri Slaby037ad482006-12-08 02:38:11 -08002675 if ((regs[12] & 0x80) == 0)
2676 return MXSER_ERR_VECTOR;
Jiri Slaby55b307d2006-12-08 02:38:14 -08002677 brd->vector = (int)regs[11]; /* interrupt vector */
Jiri Slaby037ad482006-12-08 02:38:11 -08002678 if (id == 1)
Jiri Slaby55b307d2006-12-08 02:38:14 -08002679 brd->vector_mask = 0x00FF;
Jiri Slaby037ad482006-12-08 02:38:11 -08002680 else
Jiri Slaby55b307d2006-12-08 02:38:14 -08002681 brd->vector_mask = 0x000F;
Jiri Slaby037ad482006-12-08 02:38:11 -08002682 for (i = 7, bits = 0x0100; i >= 0; i--, bits <<= 1) {
2683 if (regs[12] & bits) {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002684 brd->ports[i].baud_base = 921600;
2685 brd->ports[i].max_baud = 921600; /* add by Victor Yu. 09-04-2002 */
Jiri Slaby037ad482006-12-08 02:38:11 -08002686 } else {
Jiri Slaby55b307d2006-12-08 02:38:14 -08002687 brd->ports[i].baud_base = 115200;
2688 brd->ports[i].max_baud = 115200; /* add by Victor Yu. 09-04-2002 */
Jiri Slaby037ad482006-12-08 02:38:11 -08002689 }
2690 }
2691 scratch2 = inb(cap + UART_LCR) & (~UART_LCR_DLAB);
2692 outb(scratch2 | UART_LCR_DLAB, cap + UART_LCR);
2693 outb(0, cap + UART_EFR); /* EFR is the same as FCR */
2694 outb(scratch2, cap + UART_LCR);
2695 outb(UART_FCR_ENABLE_FIFO, cap + UART_FCR);
2696 scratch = inb(cap + UART_IIR);
2697
2698 if (scratch & 0xC0)
Jiri Slaby55b307d2006-12-08 02:38:14 -08002699 brd->uart_type = PORT_16550A;
Jiri Slaby037ad482006-12-08 02:38:11 -08002700 else
Jiri Slaby55b307d2006-12-08 02:38:14 -08002701 brd->uart_type = PORT_16450;
Jiri Slaby037ad482006-12-08 02:38:11 -08002702 if (id == 1)
Jiri Slaby55b307d2006-12-08 02:38:14 -08002703 brd->nports = 8;
Jiri Slaby037ad482006-12-08 02:38:11 -08002704 else
Jiri Slaby55b307d2006-12-08 02:38:14 -08002705 brd->nports = 4;
Jiri Slaby7a7a5c32006-12-08 02:38:17 -08002706 if (!request_region(brd->ports[0].ioaddr, 8 * brd->nports, "mxser(IO)"))
2707 return MXSER_ERR_IOADDR;
2708 if (!request_region(brd->vector, 1, "mxser(vector)")) {
2709 release_region(brd->ports[0].ioaddr, 8 * brd->nports);
2710 return MXSER_ERR_VECTOR;
2711 }
Jiri Slaby55b307d2006-12-08 02:38:14 -08002712 return brd->nports;
Jiri Slaby037ad482006-12-08 02:38:11 -08002713}
2714
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002715static int __init mxser_get_PCI_conf(int board_type, struct mxser_board *brd,
2716 struct pci_dev *pdev)
Jiri Slaby037ad482006-12-08 02:38:11 -08002717{
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002718 unsigned int i, j;
2719 unsigned long ioaddress;
2720 int retval;
Jiri Slaby037ad482006-12-08 02:38:11 -08002721
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002722 /* io address */
2723 brd->board_type = board_type;
2724 brd->nports = mxser_numports[board_type - 1];
2725 ioaddress = pci_resource_start(pdev, 2);
2726 retval = pci_request_region(pdev, 2, "mxser(IO)");
2727 if (retval)
2728 goto err;
2729
2730 for (i = 0; i < brd->nports; i++)
2731 brd->ports[i].ioaddr = ioaddress + 8 * i;
2732
2733 /* vector */
2734 ioaddress = pci_resource_start(pdev, 3);
2735 retval = pci_request_region(pdev, 3, "mxser(vector)");
2736 if (retval)
2737 goto err_relio;
2738 brd->vector = ioaddress;
2739
2740 /* irq */
2741 brd->irq = pdev->irq;
2742
2743 brd->chip_flag = CheckIsMoxaMust(brd->ports[0].ioaddr);
2744 brd->uart_type = PORT_16550A;
2745 brd->vector_mask = 0;
2746
2747 for (i = 0; i < brd->nports; i++) {
2748 for (j = 0; j < UART_INFO_NUM; j++) {
2749 if (Gpci_uart_info[j].type == brd->chip_flag) {
2750 brd->ports[i].max_baud =
2751 Gpci_uart_info[j].max_baud;
2752
2753 /* exception....CP-102 */
2754 if (board_type == MXSER_BOARD_CP102)
2755 brd->ports[i].max_baud = 921600;
2756 break;
Jiri Slaby037ad482006-12-08 02:38:11 -08002757 }
2758 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002759 }
2760
2761 if (brd->chip_flag == MOXA_MUST_MU860_HWID) {
2762 for (i = 0; i < brd->nports; i++) {
2763 if (i < 4)
2764 brd->ports[i].opmode_ioaddr = ioaddress + 4;
2765 else
2766 brd->ports[i].opmode_ioaddr = ioaddress + 0x0c;
Jiri Slaby037ad482006-12-08 02:38:11 -08002767 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002768 outb(0, ioaddress + 4); /* default set to RS232 mode */
2769 outb(0, ioaddress + 0x0c); /* default set to RS232 mode */
Jiri Slaby037ad482006-12-08 02:38:11 -08002770 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002771
2772 for (i = 0; i < brd->nports; i++) {
2773 brd->vector_mask |= (1 << i);
2774 brd->ports[i].baud_base = 921600;
2775 }
2776 return 0;
2777err_relio:
2778 pci_release_region(pdev, 2);
2779err:
2780 return retval;
Jiri Slaby037ad482006-12-08 02:38:11 -08002781}
2782
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002783static int __init mxser_module_init(void)
Jiri Slaby037ad482006-12-08 02:38:11 -08002784{
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002785 struct pci_dev *pdev = NULL;
2786 struct mxser_board *brd;
2787 unsigned int i, m;
2788 int retval, b, n;
Jiri Slaby037ad482006-12-08 02:38:11 -08002789
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002790 pr_debug("Loading module mxser ...\n");
Jiri Slaby037ad482006-12-08 02:38:11 -08002791
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002792 mxvar_sdriver = alloc_tty_driver(MXSER_PORTS + 1);
2793 if (!mxvar_sdriver)
2794 return -ENOMEM;
2795 spin_lock_init(&gm_lock);
2796
2797 for (i = 0; i < MXSER_BOARDS; i++)
2798 mxser_boards[i].board_type = -1;
2799
2800 printk(KERN_INFO "MOXA Smartio/Industio family driver version %s\n",
2801 MXSER_VERSION);
2802
2803 /* Initialize the tty_driver structure */
2804 mxvar_sdriver->magic = TTY_DRIVER_MAGIC;
2805 mxvar_sdriver->name = "ttyM";
2806 mxvar_sdriver->major = ttymajor;
2807 mxvar_sdriver->minor_start = 0;
2808 mxvar_sdriver->num = MXSER_PORTS + 1;
2809 mxvar_sdriver->type = TTY_DRIVER_TYPE_SERIAL;
2810 mxvar_sdriver->subtype = SERIAL_TYPE_NORMAL;
2811 mxvar_sdriver->init_termios = tty_std_termios;
2812 mxvar_sdriver->init_termios.c_cflag = B9600|CS8|CREAD|HUPCL|CLOCAL;
2813 mxvar_sdriver->flags = TTY_DRIVER_REAL_RAW;
2814 tty_set_operations(mxvar_sdriver, &mxser_ops);
2815 mxvar_sdriver->ttys = mxvar_tty;
2816 mxvar_sdriver->termios = mxvar_termios;
2817 mxvar_sdriver->termios_locked = mxvar_termios_locked;
2818
2819 mxvar_diagflag = 0;
2820
2821 m = 0;
2822 /* Start finding ISA boards here */
2823 for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
2824 int cap;
2825
2826 if (!(cap = mxserBoardCAP[b]))
2827 continue;
2828
2829 brd = &mxser_boards[m];
2830 retval = mxser_get_ISA_conf(cap, brd);
2831
2832 if (retval != 0)
2833 printk(KERN_INFO "Found MOXA %s board (CAP=0x%x)\n",
2834 mxser_brdname[brd->board_type - 1], ioaddr[b]);
2835
2836 if (retval <= 0) {
2837 if (retval == MXSER_ERR_IRQ)
2838 printk(KERN_ERR "Invalid interrupt number, "
2839 "board not configured\n");
2840 else if (retval == MXSER_ERR_IRQ_CONFLIT)
2841 printk(KERN_ERR "Invalid interrupt number, "
2842 "board not configured\n");
2843 else if (retval == MXSER_ERR_VECTOR)
2844 printk(KERN_ERR "Invalid interrupt vector, "
2845 "board not configured\n");
2846 else if (retval == MXSER_ERR_IOADDR)
2847 printk(KERN_ERR "Invalid I/O address, "
2848 "board not configured\n");
2849
2850 continue;
2851 }
2852
2853 brd->pdev = NULL;
2854
2855 /* mxser_initbrd will hook ISR. */
2856 if (mxser_initbrd(brd) < 0)
2857 continue;
2858
2859 m++;
Jiri Slaby037ad482006-12-08 02:38:11 -08002860 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002861
2862 /* Start finding ISA boards from module arg */
2863 for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
2864 unsigned long cap;
2865
2866 if (!(cap = ioaddr[b]))
2867 continue;
2868
2869 brd = &mxser_boards[m];
2870 retval = mxser_get_ISA_conf(cap, &mxser_boards[m]);
2871
2872 if (retval != 0)
2873 printk(KERN_INFO "Found MOXA %s board (CAP=0x%x)\n",
2874 mxser_brdname[brd->board_type - 1], ioaddr[b]);
2875
2876 if (retval <= 0) {
2877 if (retval == MXSER_ERR_IRQ)
2878 printk(KERN_ERR "Invalid interrupt number, "
2879 "board not configured\n");
2880 else if (retval == MXSER_ERR_IRQ_CONFLIT)
2881 printk(KERN_ERR "Invalid interrupt number, "
2882 "board not configured\n");
2883 else if (retval == MXSER_ERR_VECTOR)
2884 printk(KERN_ERR "Invalid interrupt vector, "
2885 "board not configured\n");
2886 else if (retval == MXSER_ERR_IOADDR)
2887 printk(KERN_ERR "Invalid I/O address, "
2888 "board not configured\n");
2889
2890 continue;
2891 }
2892
2893 brd->pdev = NULL;
2894 /* mxser_initbrd will hook ISR. */
2895 if (mxser_initbrd(brd) < 0)
2896 continue;
2897
2898 m++;
2899 }
2900
2901 /* start finding PCI board here */
2902 n = ARRAY_SIZE(mxser_pcibrds) - 1;
2903 b = 0;
2904 while (b < n) {
2905 pdev = pci_get_device(mxser_pcibrds[b].vendor,
2906 mxser_pcibrds[b].device, pdev);
2907 if (pdev == NULL) {
2908 b++;
2909 continue;
2910 }
2911 printk(KERN_INFO "Found MOXA %s board(BusNo=%d,DevNo=%d)\n",
2912 mxser_brdname[(int) (mxser_pcibrds[b].driver_data) - 1],
2913 pdev->bus->number, PCI_SLOT(pdev->devfn));
2914 if (m >= MXSER_BOARDS)
2915 printk(KERN_ERR
2916 "Too many Smartio/Industio family boards find "
2917 "(maximum %d), board not configured\n",
2918 MXSER_BOARDS);
2919 else {
2920 if (pci_enable_device(pdev)) {
2921 printk(KERN_ERR "Moxa SmartI/O PCI enable "
2922 "fail !\n");
2923 continue;
2924 }
2925 brd = &mxser_boards[m];
2926 brd->pdev = pdev;
2927 retval = mxser_get_PCI_conf(
2928 (int)mxser_pcibrds[b].driver_data,
2929 brd, pdev);
2930 if (retval < 0) {
2931 if (retval == MXSER_ERR_IRQ)
2932 printk(KERN_ERR
2933 "Invalid interrupt number, "
2934 "board not configured\n");
2935 else if (retval == MXSER_ERR_IRQ_CONFLIT)
2936 printk(KERN_ERR
2937 "Invalid interrupt number, "
2938 "board not configured\n");
2939 else if (retval == MXSER_ERR_VECTOR)
2940 printk(KERN_ERR
2941 "Invalid interrupt vector, "
2942 "board not configured\n");
2943 else if (retval == MXSER_ERR_IOADDR)
2944 printk(KERN_ERR
2945 "Invalid I/O address, "
2946 "board not configured\n");
2947 continue;
2948 }
2949 /* mxser_initbrd will hook ISR. */
2950 if (mxser_initbrd(brd) < 0)
2951 continue;
2952 m++;
2953 /* Keep an extra reference if we succeeded. It will
2954 be returned at unload time */
2955 pci_dev_get(pdev);
2956 }
2957 }
2958
2959 retval = tty_register_driver(mxvar_sdriver);
2960 if (retval) {
2961 printk(KERN_ERR "Couldn't install MOXA Smartio/Industio family"
2962 " driver !\n");
2963 put_tty_driver(mxvar_sdriver);
2964
2965 for (i = 0; i < MXSER_BOARDS; i++) {
2966 if (mxser_boards[i].board_type == -1)
2967 continue;
2968 else {
2969 free_irq(mxser_boards[i].irq, &mxser_boards[i]);
2970 /* todo: release io, vector */
2971 }
2972 }
2973 return retval;
2974 }
2975
2976 pr_debug("Done.\n");
2977
2978 return retval;
Jiri Slaby037ad482006-12-08 02:38:11 -08002979}
2980
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002981static void __exit mxser_module_exit(void)
Jiri Slaby037ad482006-12-08 02:38:11 -08002982{
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002983 int i, err;
Jiri Slaby037ad482006-12-08 02:38:11 -08002984
Jiri Slabya8dea4e2006-12-08 02:38:21 -08002985 pr_debug("Unloading module mxser ...\n");
2986
2987 err = tty_unregister_driver(mxvar_sdriver);
2988 if (!err)
2989 put_tty_driver(mxvar_sdriver);
2990 else
2991 printk(KERN_ERR "Couldn't unregister MOXA Smartio/Industio family serial driver\n");
2992
2993 for (i = 0; i < MXSER_BOARDS; i++) {
2994 struct pci_dev *pdev;
2995
2996 if (mxser_boards[i].board_type == -1)
2997 continue;
2998 else {
2999 pdev = mxser_boards[i].pdev;
3000 free_irq(mxser_boards[i].irq, &mxser_boards[i]);
3001 if (pdev != NULL) { /* PCI */
3002 pci_release_region(pdev, 2);
3003 pci_release_region(pdev, 3);
3004 pci_dev_put(pdev);
3005 } else {
3006 release_region(mxser_boards[i].ports[0].ioaddr, 8 * mxser_boards[i].nports);
3007 release_region(mxser_boards[i].vector, 1);
3008 }
3009 }
Jiri Slaby037ad482006-12-08 02:38:11 -08003010 }
Jiri Slabya8dea4e2006-12-08 02:38:21 -08003011 pr_debug("Done.\n");
Jiri Slaby037ad482006-12-08 02:38:11 -08003012}
3013
3014module_init(mxser_module_init);
3015module_exit(mxser_module_exit);