blob: 9b4d03cf4e1de0b28e1321ff4a75a52b14b2fdac [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * mxser.c -- MOXA Smartio/Industio family multiport serial driver.
3 *
Jiri Slaby80ff8a82008-02-07 00:16:51 -08004 * Copyright (C) 1999-2006 Moxa Technologies (support@moxa.com).
5 * Copyright (C) 2006-2008 Jiri Slaby <jirislaby@gmail.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
Jiri Slaby1c456072008-02-07 00:16:46 -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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
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
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -070014 * (at your option) any later version.
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 */
20
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/errno.h>
23#include <linux/signal.h>
24#include <linux/sched.h>
25#include <linux/timer.h>
26#include <linux/interrupt.h>
27#include <linux/tty.h>
28#include <linux/tty_flip.h>
29#include <linux/serial.h>
30#include <linux/serial_reg.h>
31#include <linux/major.h>
32#include <linux/string.h>
33#include <linux/fcntl.h>
34#include <linux/ptrace.h>
35#include <linux/gfp.h>
36#include <linux/ioport.h>
37#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/delay.h>
39#include <linux/pci.h>
Jiri Slaby1977f032007-10-18 23:40:25 -070040#include <linux/bitops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42#include <asm/system.h>
43#include <asm/io.h>
44#include <asm/irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <asm/uaccess.h>
46
47#include "mxser.h"
48
Jiri Slabye129def2008-07-22 11:20:34 +010049#define MXSER_VERSION "2.0.4" /* 1.12 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#define MXSERMAJOR 174
51#define MXSERCUMAJOR 175
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#define MXSER_BOARDS 4 /* Max. boards */
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#define MXSER_PORTS_PER_BOARD 8 /* Max. ports per board */
Jiri Slaby1c456072008-02-07 00:16:46 -080055#define MXSER_PORTS (MXSER_BOARDS * MXSER_PORTS_PER_BOARD)
56#define MXSER_ISR_PASS_LIMIT 100
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Jiri Slaby1c456072008-02-07 00:16:46 -080058/*CheckIsMoxaMust return value*/
59#define MOXA_OTHER_UART 0x00
60#define MOXA_MUST_MU150_HWID 0x01
61#define MOXA_MUST_MU860_HWID 0x02
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#define WAKEUP_CHARS 256
64
65#define UART_MCR_AFE 0x20
66#define UART_LSR_SPECIAL 0x1E
67
Jiri Slabye129def2008-07-22 11:20:34 +010068#define PCI_DEVICE_ID_POS104UL 0x1044
Jiri Slaby1c456072008-02-07 00:16:46 -080069#define PCI_DEVICE_ID_CB108 0x1080
Jiri Slabye129def2008-07-22 11:20:34 +010070#define PCI_DEVICE_ID_CP102UF 0x1023
Jiri Slaby1c456072008-02-07 00:16:46 -080071#define PCI_DEVICE_ID_CB114 0x1142
Jiri Slaby80ff8a82008-02-07 00:16:51 -080072#define PCI_DEVICE_ID_CP114UL 0x1143
Jiri Slaby1c456072008-02-07 00:16:46 -080073#define PCI_DEVICE_ID_CB134I 0x1341
74#define PCI_DEVICE_ID_CP138U 0x1380
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77#define C168_ASIC_ID 1
78#define C104_ASIC_ID 2
79#define C102_ASIC_ID 0xB
80#define CI132_ASIC_ID 4
81#define CI134_ASIC_ID 3
82#define CI104J_ASIC_ID 5
83
Jiri Slaby1c456072008-02-07 00:16:46 -080084#define MXSER_HIGHBAUD 1
85#define MXSER_HAS2 2
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -070087/* This is only for PCI */
Jiri Slaby1c456072008-02-07 00:16:46 -080088static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 int type;
90 int tx_fifo;
91 int rx_fifo;
92 int xmit_fifo_size;
93 int rx_high_water;
94 int rx_trigger;
95 int rx_low_water;
96 long max_baud;
Jiri Slaby1c456072008-02-07 00:16:46 -080097} Gpci_uart_info[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 {MOXA_OTHER_UART, 16, 16, 16, 14, 14, 1, 921600L},
99 {MOXA_MUST_MU150_HWID, 64, 64, 64, 48, 48, 16, 230400L},
100 {MOXA_MUST_MU860_HWID, 128, 128, 128, 96, 96, 32, 921600L}
101};
Jiri Slaby1c456072008-02-07 00:16:46 -0800102#define UART_INFO_NUM ARRAY_SIZE(Gpci_uart_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Jiri Slaby1c456072008-02-07 00:16:46 -0800104struct mxser_cardinfo {
105 char *name;
106 unsigned int nports;
107 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108};
109
Jiri Slaby1c456072008-02-07 00:16:46 -0800110static const struct mxser_cardinfo mxser_cards[] = {
111/* 0*/ { "C168 series", 8, },
112 { "C104 series", 4, },
113 { "CI-104J series", 4, },
114 { "C168H/PCI series", 8, },
115 { "C104H/PCI series", 4, },
116/* 5*/ { "C102 series", 4, MXSER_HAS2 }, /* C102-ISA */
117 { "CI-132 series", 4, MXSER_HAS2 },
118 { "CI-134 series", 4, },
119 { "CP-132 series", 2, },
120 { "CP-114 series", 4, },
121/*10*/ { "CT-114 series", 4, },
122 { "CP-102 series", 2, MXSER_HIGHBAUD },
123 { "CP-104U series", 4, },
124 { "CP-168U series", 8, },
125 { "CP-132U series", 2, },
126/*15*/ { "CP-134U series", 4, },
127 { "CP-104JU series", 4, },
128 { "Moxa UC7000 Serial", 8, }, /* RC7000 */
129 { "CP-118U series", 8, },
130 { "CP-102UL series", 2, },
131/*20*/ { "CP-102U series", 2, },
132 { "CP-118EL series", 8, },
133 { "CP-168EL series", 8, },
134 { "CP-104EL series", 4, },
135 { "CB-108 series", 8, },
136/*25*/ { "CB-114 series", 4, },
137 { "CB-134I series", 4, },
138 { "CP-138U series", 8, },
Jiri Slaby80ff8a82008-02-07 00:16:51 -0800139 { "POS-104UL series", 4, },
Jiri Slabye129def2008-07-22 11:20:34 +0100140 { "CP-114UL series", 4, },
141/*30*/ { "CP-102UF series", 2, }
Jiri Slaby1c456072008-02-07 00:16:46 -0800142};
143
144/* driver_data correspond to the lines in the structure above
145 see also ISA probe function before you change something */
146static struct pci_device_id mxser_pcibrds[] = {
147 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_C168), .driver_data = 3 },
148 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_C104), .driver_data = 4 },
149 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP132), .driver_data = 8 },
150 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP114), .driver_data = 9 },
151 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CT114), .driver_data = 10 },
152 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102), .driver_data = 11 },
153 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104U), .driver_data = 12 },
154 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP168U), .driver_data = 13 },
155 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP132U), .driver_data = 14 },
156 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP134U), .driver_data = 15 },
157 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104JU),.driver_data = 16 },
158 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_RC7000), .driver_data = 17 },
159 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP118U), .driver_data = 18 },
160 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102UL),.driver_data = 19 },
161 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102U), .driver_data = 20 },
162 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP118EL),.driver_data = 21 },
163 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP168EL),.driver_data = 22 },
164 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104EL),.driver_data = 23 },
165 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB108), .driver_data = 24 },
166 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB114), .driver_data = 25 },
167 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB134I), .driver_data = 26 },
168 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP138U), .driver_data = 27 },
169 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_POS104UL), .driver_data = 28 },
Jiri Slaby80ff8a82008-02-07 00:16:51 -0800170 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP114UL), .driver_data = 29 },
Jiri Slabye129def2008-07-22 11:20:34 +0100171 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP102UF), .driver_data = 30 },
Jiri Slaby1c456072008-02-07 00:16:46 -0800172 { }
173};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174MODULE_DEVICE_TABLE(pci, mxser_pcibrds);
175
Jiri Slaby1df00922008-07-25 01:48:22 -0700176static unsigned long ioaddr[MXSER_BOARDS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177static int ttymajor = MXSERMAJOR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179/* Variables for insmod */
180
181MODULE_AUTHOR("Casper Yang");
182MODULE_DESCRIPTION("MOXA Smartio/Industio Family Multiport Board Device Driver");
Jiri Slaby1df00922008-07-25 01:48:22 -0700183module_param_array(ioaddr, ulong, NULL, 0);
184MODULE_PARM_DESC(ioaddr, "ISA io addresses to look for a moxa board");
Rusty Russell8d3b33f2006-03-25 03:07:05 -0800185module_param(ttymajor, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186MODULE_LICENSE("GPL");
187
188struct mxser_log {
189 int tick;
190 unsigned long rxcnt[MXSER_PORTS];
191 unsigned long txcnt[MXSER_PORTS];
192};
193
194
195struct mxser_mon {
196 unsigned long rxcnt;
197 unsigned long txcnt;
198 unsigned long up_rxcnt;
199 unsigned long up_txcnt;
200 int modem_status;
201 unsigned char hold_reason;
202};
203
204struct mxser_mon_ext {
205 unsigned long rx_cnt[32];
206 unsigned long tx_cnt[32];
207 unsigned long up_rxcnt[32];
208 unsigned long up_txcnt[32];
209 int modem_status[32];
210
211 long baudrate[32];
212 int databits[32];
213 int stopbits[32];
214 int parity[32];
215 int flowctrl[32];
216 int fifo[32];
217 int iftype[32];
218};
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700219
Jiri Slaby1c456072008-02-07 00:16:46 -0800220struct mxser_board;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Jiri Slaby1c456072008-02-07 00:16:46 -0800222struct mxser_port {
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100223 struct tty_port port;
Jiri Slaby1c456072008-02-07 00:16:46 -0800224 struct mxser_board *board;
Jiri Slaby1c456072008-02-07 00:16:46 -0800225
226 unsigned long ioaddr;
227 unsigned long opmode_ioaddr;
228 int max_baud;
229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 int rx_high_water;
231 int rx_trigger; /* Rx fifo trigger level */
232 int rx_low_water;
233 int baud_base; /* max. speed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 int type; /* UART type */
Jiri Slaby1c456072008-02-07 00:16:46 -0800235
236 int x_char; /* xon/xoff character */
237 int IER; /* Interrupt Enable Register */
238 int MCR; /* Modem control register */
239
240 unsigned char stop_rx;
241 unsigned char ldisc_stop_rx;
242
243 int custom_divisor;
Jiri Slaby1c456072008-02-07 00:16:46 -0800244 unsigned char err_shadow;
Jiri Slaby1c456072008-02-07 00:16:46 -0800245
Jiri Slaby1c456072008-02-07 00:16:46 -0800246 struct async_icount icount; /* kernel counters for 4 input interrupts */
247 int timeout;
248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 int read_status_mask;
250 int ignore_status_mask;
251 int xmit_fifo_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 int xmit_head;
253 int xmit_tail;
254 int xmit_cnt;
Jiri Slaby1c456072008-02-07 00:16:46 -0800255
Alan Cox606d0992006-12-08 02:38:45 -0800256 struct ktermios normal_termios;
Jiri Slaby1c456072008-02-07 00:16:46 -0800257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 struct mxser_mon mon_data;
Jiri Slaby1c456072008-02-07 00:16:46 -0800259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 spinlock_t slock;
Jiri Slaby1c456072008-02-07 00:16:46 -0800261 wait_queue_head_t delta_msr_wait;
262};
263
264struct mxser_board {
265 unsigned int idx;
266 int irq;
267 const struct mxser_cardinfo *info;
268 unsigned long vector;
269 unsigned long vector_mask;
270
271 int chip_flag;
272 int uart_type;
273
274 struct mxser_port ports[MXSER_PORTS_PER_BOARD];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275};
276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277struct mxser_mstatus {
278 tcflag_t cflag;
279 int cts;
280 int dsr;
281 int ri;
282 int dcd;
283};
284
Jiri Slaby1c456072008-02-07 00:16:46 -0800285static struct mxser_board mxser_boards[MXSER_BOARDS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286static struct tty_driver *mxvar_sdriver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287static struct mxser_log mxvar_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288static int mxser_set_baud_method[MXSER_PORTS + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Christoph Hellwig148ff862008-04-30 00:54:29 -0700290static void mxser_enable_must_enchance_mode(unsigned long baseio)
291{
292 u8 oldlcr;
293 u8 efr;
294
295 oldlcr = inb(baseio + UART_LCR);
296 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
297
298 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
299 efr |= MOXA_MUST_EFR_EFRB_ENABLE;
300
301 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
302 outb(oldlcr, baseio + UART_LCR);
303}
304
305static void mxser_disable_must_enchance_mode(unsigned long baseio)
306{
307 u8 oldlcr;
308 u8 efr;
309
310 oldlcr = inb(baseio + UART_LCR);
311 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
312
313 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
314 efr &= ~MOXA_MUST_EFR_EFRB_ENABLE;
315
316 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
317 outb(oldlcr, baseio + UART_LCR);
318}
319
320static void mxser_set_must_xon1_value(unsigned long baseio, u8 value)
321{
322 u8 oldlcr;
323 u8 efr;
324
325 oldlcr = inb(baseio + UART_LCR);
326 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
327
328 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
329 efr &= ~MOXA_MUST_EFR_BANK_MASK;
330 efr |= MOXA_MUST_EFR_BANK0;
331
332 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
333 outb(value, baseio + MOXA_MUST_XON1_REGISTER);
334 outb(oldlcr, baseio + UART_LCR);
335}
336
337static void mxser_set_must_xoff1_value(unsigned long baseio, u8 value)
338{
339 u8 oldlcr;
340 u8 efr;
341
342 oldlcr = inb(baseio + UART_LCR);
343 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
344
345 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
346 efr &= ~MOXA_MUST_EFR_BANK_MASK;
347 efr |= MOXA_MUST_EFR_BANK0;
348
349 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
350 outb(value, baseio + MOXA_MUST_XOFF1_REGISTER);
351 outb(oldlcr, baseio + UART_LCR);
352}
353
354static void mxser_set_must_fifo_value(struct mxser_port *info)
355{
356 u8 oldlcr;
357 u8 efr;
358
359 oldlcr = inb(info->ioaddr + UART_LCR);
360 outb(MOXA_MUST_ENTER_ENCHANCE, info->ioaddr + UART_LCR);
361
362 efr = inb(info->ioaddr + MOXA_MUST_EFR_REGISTER);
363 efr &= ~MOXA_MUST_EFR_BANK_MASK;
364 efr |= MOXA_MUST_EFR_BANK1;
365
366 outb(efr, info->ioaddr + MOXA_MUST_EFR_REGISTER);
367 outb((u8)info->rx_high_water, info->ioaddr + MOXA_MUST_RBRTH_REGISTER);
368 outb((u8)info->rx_trigger, info->ioaddr + MOXA_MUST_RBRTI_REGISTER);
369 outb((u8)info->rx_low_water, info->ioaddr + MOXA_MUST_RBRTL_REGISTER);
370 outb(oldlcr, info->ioaddr + UART_LCR);
371}
372
373static void mxser_set_must_enum_value(unsigned long baseio, u8 value)
374{
375 u8 oldlcr;
376 u8 efr;
377
378 oldlcr = inb(baseio + UART_LCR);
379 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
380
381 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
382 efr &= ~MOXA_MUST_EFR_BANK_MASK;
383 efr |= MOXA_MUST_EFR_BANK2;
384
385 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
386 outb(value, baseio + MOXA_MUST_ENUM_REGISTER);
387 outb(oldlcr, baseio + UART_LCR);
388}
389
390static void mxser_get_must_hardware_id(unsigned long baseio, u8 *pId)
391{
392 u8 oldlcr;
393 u8 efr;
394
395 oldlcr = inb(baseio + UART_LCR);
396 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
397
398 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
399 efr &= ~MOXA_MUST_EFR_BANK_MASK;
400 efr |= MOXA_MUST_EFR_BANK2;
401
402 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
403 *pId = inb(baseio + MOXA_MUST_HWID_REGISTER);
404 outb(oldlcr, baseio + UART_LCR);
405}
406
407static void SET_MOXA_MUST_NO_SOFTWARE_FLOW_CONTROL(unsigned long baseio)
408{
409 u8 oldlcr;
410 u8 efr;
411
412 oldlcr = inb(baseio + UART_LCR);
413 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
414
415 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
416 efr &= ~MOXA_MUST_EFR_SF_MASK;
417
418 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
419 outb(oldlcr, baseio + UART_LCR);
420}
421
422static void mxser_enable_must_tx_software_flow_control(unsigned long baseio)
423{
424 u8 oldlcr;
425 u8 efr;
426
427 oldlcr = inb(baseio + UART_LCR);
428 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
429
430 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
431 efr &= ~MOXA_MUST_EFR_SF_TX_MASK;
432 efr |= MOXA_MUST_EFR_SF_TX1;
433
434 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
435 outb(oldlcr, baseio + UART_LCR);
436}
437
438static void mxser_disable_must_tx_software_flow_control(unsigned long baseio)
439{
440 u8 oldlcr;
441 u8 efr;
442
443 oldlcr = inb(baseio + UART_LCR);
444 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
445
446 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
447 efr &= ~MOXA_MUST_EFR_SF_TX_MASK;
448
449 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
450 outb(oldlcr, baseio + UART_LCR);
451}
452
453static void mxser_enable_must_rx_software_flow_control(unsigned long baseio)
454{
455 u8 oldlcr;
456 u8 efr;
457
458 oldlcr = inb(baseio + UART_LCR);
459 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
460
461 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
462 efr &= ~MOXA_MUST_EFR_SF_RX_MASK;
463 efr |= MOXA_MUST_EFR_SF_RX1;
464
465 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
466 outb(oldlcr, baseio + UART_LCR);
467}
468
469static void mxser_disable_must_rx_software_flow_control(unsigned long baseio)
470{
471 u8 oldlcr;
472 u8 efr;
473
474 oldlcr = inb(baseio + UART_LCR);
475 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
476
477 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
478 efr &= ~MOXA_MUST_EFR_SF_RX_MASK;
479
480 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
481 outb(oldlcr, baseio + UART_LCR);
482}
483
Jesper Juhlb8cc5542007-10-18 03:06:03 -0700484#ifdef CONFIG_PCI
Jiri Slaby1c456072008-02-07 00:16:46 -0800485static int __devinit CheckIsMoxaMust(unsigned long io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
487 u8 oldmcr, hwid;
488 int i;
489
490 outb(0, io + UART_LCR);
Christoph Hellwig148ff862008-04-30 00:54:29 -0700491 mxser_disable_must_enchance_mode(io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 oldmcr = inb(io + UART_MCR);
493 outb(0, io + UART_MCR);
Christoph Hellwig148ff862008-04-30 00:54:29 -0700494 mxser_set_must_xon1_value(io, 0x11);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 if ((hwid = inb(io + UART_MCR)) != 0) {
496 outb(oldmcr, io + UART_MCR);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700497 return MOXA_OTHER_UART;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 }
499
Christoph Hellwig148ff862008-04-30 00:54:29 -0700500 mxser_get_must_hardware_id(io, &hwid);
Jiri Slaby1c456072008-02-07 00:16:46 -0800501 for (i = 1; i < UART_INFO_NUM; i++) { /* 0 = OTHER_UART */
502 if (hwid == Gpci_uart_info[i].type)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700503 return (int)hwid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 }
505 return MOXA_OTHER_UART;
506}
Jesper Juhlb8cc5542007-10-18 03:06:03 -0700507#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Jiri Slaby1c456072008-02-07 00:16:46 -0800509static void process_txrx_fifo(struct mxser_port *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510{
511 int i;
512
513 if ((info->type == PORT_16450) || (info->type == PORT_8250)) {
514 info->rx_trigger = 1;
515 info->rx_high_water = 1;
516 info->rx_low_water = 1;
517 info->xmit_fifo_size = 1;
Jiri Slaby1c456072008-02-07 00:16:46 -0800518 } else
519 for (i = 0; i < UART_INFO_NUM; i++)
520 if (info->board->chip_flag == Gpci_uart_info[i].type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 info->rx_trigger = Gpci_uart_info[i].rx_trigger;
522 info->rx_low_water = Gpci_uart_info[i].rx_low_water;
523 info->rx_high_water = Gpci_uart_info[i].rx_high_water;
524 info->xmit_fifo_size = Gpci_uart_info[i].xmit_fifo_size;
525 break;
526 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527}
528
Jiri Slaby1c456072008-02-07 00:16:46 -0800529static unsigned char mxser_get_msr(int baseaddr, int mode, int port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530{
Jiri Slaby72800df2008-07-25 01:48:20 -0700531 static unsigned char mxser_msr[MXSER_PORTS + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 unsigned char status = 0;
533
534 status = inb(baseaddr + UART_MSR);
535
536 mxser_msr[port] &= 0x0F;
537 mxser_msr[port] |= status;
538 status = mxser_msr[port];
539 if (mode)
540 mxser_msr[port] = 0;
541
542 return status;
543}
544
Jiri Slaby1c456072008-02-07 00:16:46 -0800545static int mxser_block_til_ready(struct tty_struct *tty, struct file *filp,
546 struct mxser_port *port)
547{
548 DECLARE_WAITQUEUE(wait, current);
549 int retval;
550 int do_clocal = 0;
551 unsigned long flags;
552
553 /*
554 * If non-blocking mode is set, or the port is not enabled,
555 * then make the check up front and then exit.
556 */
557 if ((filp->f_flags & O_NONBLOCK) ||
558 test_bit(TTY_IO_ERROR, &tty->flags)) {
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100559 port->port.flags |= ASYNC_NORMAL_ACTIVE;
Jiri Slaby1c456072008-02-07 00:16:46 -0800560 return 0;
561 }
562
563 if (tty->termios->c_cflag & CLOCAL)
564 do_clocal = 1;
565
566 /*
567 * Block waiting for the carrier detect and the line to become
568 * free (i.e., not in use by the callout). While we are in
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100569 * this loop, port->port.count is dropped by one, so that
Jiri Slaby1c456072008-02-07 00:16:46 -0800570 * mxser_close() knows when to free things. We restore it upon
571 * exit, either normal or abnormal.
572 */
573 retval = 0;
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100574 add_wait_queue(&port->port.open_wait, &wait);
Jiri Slaby1c456072008-02-07 00:16:46 -0800575
576 spin_lock_irqsave(&port->slock, flags);
577 if (!tty_hung_up_p(filp))
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100578 port->port.count--;
Jiri Slaby1c456072008-02-07 00:16:46 -0800579 spin_unlock_irqrestore(&port->slock, flags);
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100580 port->port.blocked_open++;
Jiri Slaby1c456072008-02-07 00:16:46 -0800581 while (1) {
582 spin_lock_irqsave(&port->slock, flags);
583 outb(inb(port->ioaddr + UART_MCR) |
584 UART_MCR_DTR | UART_MCR_RTS, port->ioaddr + UART_MCR);
585 spin_unlock_irqrestore(&port->slock, flags);
586 set_current_state(TASK_INTERRUPTIBLE);
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100587 if (tty_hung_up_p(filp) || !(port->port.flags & ASYNC_INITIALIZED)) {
588 if (port->port.flags & ASYNC_HUP_NOTIFY)
Jiri Slaby1c456072008-02-07 00:16:46 -0800589 retval = -EAGAIN;
590 else
591 retval = -ERESTARTSYS;
592 break;
593 }
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100594 if (!(port->port.flags & ASYNC_CLOSING) &&
Jiri Slaby1c456072008-02-07 00:16:46 -0800595 (do_clocal ||
596 (inb(port->ioaddr + UART_MSR) & UART_MSR_DCD)))
597 break;
598 if (signal_pending(current)) {
599 retval = -ERESTARTSYS;
600 break;
601 }
602 schedule();
603 }
604 set_current_state(TASK_RUNNING);
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100605 remove_wait_queue(&port->port.open_wait, &wait);
Jiri Slaby1c456072008-02-07 00:16:46 -0800606 if (!tty_hung_up_p(filp))
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100607 port->port.count++;
608 port->port.blocked_open--;
Jiri Slaby1c456072008-02-07 00:16:46 -0800609 if (retval)
610 return retval;
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100611 port->port.flags |= ASYNC_NORMAL_ACTIVE;
Jiri Slaby1c456072008-02-07 00:16:46 -0800612 return 0;
613}
614
615static int mxser_set_baud(struct mxser_port *info, long newspd)
616{
617 int quot = 0, baud;
618 unsigned char cval;
619
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100620 if (!info->port.tty || !info->port.tty->termios)
Jiri Slaby1c456072008-02-07 00:16:46 -0800621 return -1;
622
623 if (!(info->ioaddr))
624 return -1;
625
626 if (newspd > info->max_baud)
627 return -1;
628
629 if (newspd == 134) {
630 quot = 2 * info->baud_base / 269;
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100631 tty_encode_baud_rate(info->port.tty, 134, 134);
Jiri Slaby1c456072008-02-07 00:16:46 -0800632 } else if (newspd) {
633 quot = info->baud_base / newspd;
634 if (quot == 0)
635 quot = 1;
636 baud = info->baud_base/quot;
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100637 tty_encode_baud_rate(info->port.tty, baud, baud);
Jiri Slaby1c456072008-02-07 00:16:46 -0800638 } else {
639 quot = 0;
640 }
641
642 info->timeout = ((info->xmit_fifo_size * HZ * 10 * quot) / info->baud_base);
643 info->timeout += HZ / 50; /* Add .02 seconds of slop */
644
645 if (quot) {
646 info->MCR |= UART_MCR_DTR;
647 outb(info->MCR, info->ioaddr + UART_MCR);
648 } else {
649 info->MCR &= ~UART_MCR_DTR;
650 outb(info->MCR, info->ioaddr + UART_MCR);
651 return 0;
652 }
653
654 cval = inb(info->ioaddr + UART_LCR);
655
656 outb(cval | UART_LCR_DLAB, info->ioaddr + UART_LCR); /* set DLAB */
657
658 outb(quot & 0xff, info->ioaddr + UART_DLL); /* LS of divisor */
659 outb(quot >> 8, info->ioaddr + UART_DLM); /* MS of divisor */
660 outb(cval, info->ioaddr + UART_LCR); /* reset DLAB */
661
662#ifdef BOTHER
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100663 if (C_BAUD(info->port.tty) == BOTHER) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800664 quot = info->baud_base % newspd;
665 quot *= 8;
666 if (quot % newspd > newspd / 2) {
667 quot /= newspd;
668 quot++;
669 } else
670 quot /= newspd;
671
Christoph Hellwig148ff862008-04-30 00:54:29 -0700672 mxser_set_must_enum_value(info->ioaddr, quot);
Jiri Slaby1c456072008-02-07 00:16:46 -0800673 } else
674#endif
Christoph Hellwig148ff862008-04-30 00:54:29 -0700675 mxser_set_must_enum_value(info->ioaddr, 0);
Jiri Slaby1c456072008-02-07 00:16:46 -0800676
677 return 0;
678}
679
680/*
681 * This routine is called to set the UART divisor registers to match
682 * the specified baud rate for a serial port.
683 */
684static int mxser_change_speed(struct mxser_port *info,
685 struct ktermios *old_termios)
686{
687 unsigned cflag, cval, fcr;
688 int ret = 0;
689 unsigned char status;
690
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100691 if (!info->port.tty || !info->port.tty->termios)
Jiri Slaby1c456072008-02-07 00:16:46 -0800692 return ret;
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100693 cflag = info->port.tty->termios->c_cflag;
Jiri Slaby1c456072008-02-07 00:16:46 -0800694 if (!(info->ioaddr))
695 return ret;
696
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100697 if (mxser_set_baud_method[info->port.tty->index] == 0)
698 mxser_set_baud(info, tty_get_baud_rate(info->port.tty));
Jiri Slaby1c456072008-02-07 00:16:46 -0800699
700 /* byte size and parity */
701 switch (cflag & CSIZE) {
702 case CS5:
703 cval = 0x00;
704 break;
705 case CS6:
706 cval = 0x01;
707 break;
708 case CS7:
709 cval = 0x02;
710 break;
711 case CS8:
712 cval = 0x03;
713 break;
714 default:
715 cval = 0x00;
716 break; /* too keep GCC shut... */
717 }
718 if (cflag & CSTOPB)
719 cval |= 0x04;
720 if (cflag & PARENB)
721 cval |= UART_LCR_PARITY;
722 if (!(cflag & PARODD))
723 cval |= UART_LCR_EPAR;
724 if (cflag & CMSPAR)
725 cval |= UART_LCR_SPAR;
726
727 if ((info->type == PORT_8250) || (info->type == PORT_16450)) {
728 if (info->board->chip_flag) {
729 fcr = UART_FCR_ENABLE_FIFO;
730 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
Christoph Hellwig148ff862008-04-30 00:54:29 -0700731 mxser_set_must_fifo_value(info);
Jiri Slaby1c456072008-02-07 00:16:46 -0800732 } else
733 fcr = 0;
734 } else {
735 fcr = UART_FCR_ENABLE_FIFO;
736 if (info->board->chip_flag) {
737 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
Christoph Hellwig148ff862008-04-30 00:54:29 -0700738 mxser_set_must_fifo_value(info);
Jiri Slaby1c456072008-02-07 00:16:46 -0800739 } else {
740 switch (info->rx_trigger) {
741 case 1:
742 fcr |= UART_FCR_TRIGGER_1;
743 break;
744 case 4:
745 fcr |= UART_FCR_TRIGGER_4;
746 break;
747 case 8:
748 fcr |= UART_FCR_TRIGGER_8;
749 break;
750 default:
751 fcr |= UART_FCR_TRIGGER_14;
752 break;
753 }
754 }
755 }
756
757 /* CTS flow control flag and modem status interrupts */
758 info->IER &= ~UART_IER_MSI;
759 info->MCR &= ~UART_MCR_AFE;
760 if (cflag & CRTSCTS) {
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100761 info->port.flags |= ASYNC_CTS_FLOW;
Jiri Slaby1c456072008-02-07 00:16:46 -0800762 info->IER |= UART_IER_MSI;
763 if ((info->type == PORT_16550A) || (info->board->chip_flag)) {
764 info->MCR |= UART_MCR_AFE;
765 } else {
766 status = inb(info->ioaddr + UART_MSR);
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100767 if (info->port.tty->hw_stopped) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800768 if (status & UART_MSR_CTS) {
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100769 info->port.tty->hw_stopped = 0;
Jiri Slaby1c456072008-02-07 00:16:46 -0800770 if (info->type != PORT_16550A &&
771 !info->board->chip_flag) {
772 outb(info->IER & ~UART_IER_THRI,
773 info->ioaddr +
774 UART_IER);
775 info->IER |= UART_IER_THRI;
776 outb(info->IER, info->ioaddr +
777 UART_IER);
778 }
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100779 tty_wakeup(info->port.tty);
Jiri Slaby1c456072008-02-07 00:16:46 -0800780 }
781 } else {
782 if (!(status & UART_MSR_CTS)) {
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100783 info->port.tty->hw_stopped = 1;
Jiri Slaby1c456072008-02-07 00:16:46 -0800784 if ((info->type != PORT_16550A) &&
785 (!info->board->chip_flag)) {
786 info->IER &= ~UART_IER_THRI;
787 outb(info->IER, info->ioaddr +
788 UART_IER);
789 }
790 }
791 }
792 }
793 } else {
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100794 info->port.flags &= ~ASYNC_CTS_FLOW;
Jiri Slaby1c456072008-02-07 00:16:46 -0800795 }
796 outb(info->MCR, info->ioaddr + UART_MCR);
797 if (cflag & CLOCAL) {
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100798 info->port.flags &= ~ASYNC_CHECK_CD;
Jiri Slaby1c456072008-02-07 00:16:46 -0800799 } else {
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100800 info->port.flags |= ASYNC_CHECK_CD;
Jiri Slaby1c456072008-02-07 00:16:46 -0800801 info->IER |= UART_IER_MSI;
802 }
803 outb(info->IER, info->ioaddr + UART_IER);
804
805 /*
806 * Set up parity check flag
807 */
808 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100809 if (I_INPCK(info->port.tty))
Jiri Slaby1c456072008-02-07 00:16:46 -0800810 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100811 if (I_BRKINT(info->port.tty) || I_PARMRK(info->port.tty))
Jiri Slaby1c456072008-02-07 00:16:46 -0800812 info->read_status_mask |= UART_LSR_BI;
813
814 info->ignore_status_mask = 0;
815
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100816 if (I_IGNBRK(info->port.tty)) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800817 info->ignore_status_mask |= UART_LSR_BI;
818 info->read_status_mask |= UART_LSR_BI;
819 /*
820 * If we're ignore parity and break indicators, ignore
821 * overruns too. (For real raw support).
822 */
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100823 if (I_IGNPAR(info->port.tty)) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800824 info->ignore_status_mask |=
825 UART_LSR_OE |
826 UART_LSR_PE |
827 UART_LSR_FE;
828 info->read_status_mask |=
829 UART_LSR_OE |
830 UART_LSR_PE |
831 UART_LSR_FE;
832 }
833 }
834 if (info->board->chip_flag) {
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100835 mxser_set_must_xon1_value(info->ioaddr, START_CHAR(info->port.tty));
836 mxser_set_must_xoff1_value(info->ioaddr, STOP_CHAR(info->port.tty));
837 if (I_IXON(info->port.tty)) {
Christoph Hellwig148ff862008-04-30 00:54:29 -0700838 mxser_enable_must_rx_software_flow_control(
839 info->ioaddr);
Jiri Slaby1c456072008-02-07 00:16:46 -0800840 } else {
Christoph Hellwig148ff862008-04-30 00:54:29 -0700841 mxser_disable_must_rx_software_flow_control(
842 info->ioaddr);
Jiri Slaby1c456072008-02-07 00:16:46 -0800843 }
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100844 if (I_IXOFF(info->port.tty)) {
Christoph Hellwig148ff862008-04-30 00:54:29 -0700845 mxser_enable_must_tx_software_flow_control(
846 info->ioaddr);
Jiri Slaby1c456072008-02-07 00:16:46 -0800847 } else {
Christoph Hellwig148ff862008-04-30 00:54:29 -0700848 mxser_disable_must_tx_software_flow_control(
849 info->ioaddr);
Jiri Slaby1c456072008-02-07 00:16:46 -0800850 }
851 }
852
853
854 outb(fcr, info->ioaddr + UART_FCR); /* set fcr */
855 outb(cval, info->ioaddr + UART_LCR);
856
857 return ret;
858}
859
860static void mxser_check_modem_status(struct mxser_port *port, int status)
861{
862 /* update input line counters */
863 if (status & UART_MSR_TERI)
864 port->icount.rng++;
865 if (status & UART_MSR_DDSR)
866 port->icount.dsr++;
867 if (status & UART_MSR_DDCD)
868 port->icount.dcd++;
869 if (status & UART_MSR_DCTS)
870 port->icount.cts++;
871 port->mon_data.modem_status = status;
872 wake_up_interruptible(&port->delta_msr_wait);
873
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100874 if ((port->port.flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800875 if (status & UART_MSR_DCD)
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100876 wake_up_interruptible(&port->port.open_wait);
Jiri Slaby1c456072008-02-07 00:16:46 -0800877 }
878
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100879 if (port->port.flags & ASYNC_CTS_FLOW) {
880 if (port->port.tty->hw_stopped) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800881 if (status & UART_MSR_CTS) {
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100882 port->port.tty->hw_stopped = 0;
Jiri Slaby1c456072008-02-07 00:16:46 -0800883
884 if ((port->type != PORT_16550A) &&
885 (!port->board->chip_flag)) {
886 outb(port->IER & ~UART_IER_THRI,
887 port->ioaddr + UART_IER);
888 port->IER |= UART_IER_THRI;
889 outb(port->IER, port->ioaddr +
890 UART_IER);
891 }
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100892 tty_wakeup(port->port.tty);
Jiri Slaby1c456072008-02-07 00:16:46 -0800893 }
894 } else {
895 if (!(status & UART_MSR_CTS)) {
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100896 port->port.tty->hw_stopped = 1;
Jiri Slaby1c456072008-02-07 00:16:46 -0800897 if (port->type != PORT_16550A &&
898 !port->board->chip_flag) {
899 port->IER &= ~UART_IER_THRI;
900 outb(port->IER, port->ioaddr +
901 UART_IER);
902 }
903 }
904 }
905 }
906}
907
908static int mxser_startup(struct mxser_port *info)
909{
910 unsigned long page;
911 unsigned long flags;
912
913 page = __get_free_page(GFP_KERNEL);
914 if (!page)
915 return -ENOMEM;
916
917 spin_lock_irqsave(&info->slock, flags);
918
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100919 if (info->port.flags & ASYNC_INITIALIZED) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800920 free_page(page);
921 spin_unlock_irqrestore(&info->slock, flags);
922 return 0;
923 }
924
925 if (!info->ioaddr || !info->type) {
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100926 if (info->port.tty)
927 set_bit(TTY_IO_ERROR, &info->port.tty->flags);
Jiri Slaby1c456072008-02-07 00:16:46 -0800928 free_page(page);
929 spin_unlock_irqrestore(&info->slock, flags);
930 return 0;
931 }
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100932 if (info->port.xmit_buf)
Jiri Slaby1c456072008-02-07 00:16:46 -0800933 free_page(page);
934 else
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100935 info->port.xmit_buf = (unsigned char *) page;
Jiri Slaby1c456072008-02-07 00:16:46 -0800936
937 /*
938 * Clear the FIFO buffers and disable them
939 * (they will be reenabled in mxser_change_speed())
940 */
941 if (info->board->chip_flag)
942 outb((UART_FCR_CLEAR_RCVR |
943 UART_FCR_CLEAR_XMIT |
944 MOXA_MUST_FCR_GDA_MODE_ENABLE), info->ioaddr + UART_FCR);
945 else
946 outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
947 info->ioaddr + UART_FCR);
948
949 /*
950 * At this point there's no way the LSR could still be 0xFF;
951 * if it is, then bail out, because there's likely no UART
952 * here.
953 */
954 if (inb(info->ioaddr + UART_LSR) == 0xff) {
955 spin_unlock_irqrestore(&info->slock, flags);
956 if (capable(CAP_SYS_ADMIN)) {
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100957 if (info->port.tty)
958 set_bit(TTY_IO_ERROR, &info->port.tty->flags);
Jiri Slaby1c456072008-02-07 00:16:46 -0800959 return 0;
960 } else
961 return -ENODEV;
962 }
963
964 /*
965 * Clear the interrupt registers.
966 */
967 (void) inb(info->ioaddr + UART_LSR);
968 (void) inb(info->ioaddr + UART_RX);
969 (void) inb(info->ioaddr + UART_IIR);
970 (void) inb(info->ioaddr + UART_MSR);
971
972 /*
973 * Now, initialize the UART
974 */
975 outb(UART_LCR_WLEN8, info->ioaddr + UART_LCR); /* reset DLAB */
976 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
977 outb(info->MCR, info->ioaddr + UART_MCR);
978
979 /*
980 * Finally, enable interrupts
981 */
982 info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
983
984 if (info->board->chip_flag)
985 info->IER |= MOXA_MUST_IER_EGDAI;
986 outb(info->IER, info->ioaddr + UART_IER); /* enable interrupts */
987
988 /*
989 * And clear the interrupt registers again for luck.
990 */
991 (void) inb(info->ioaddr + UART_LSR);
992 (void) inb(info->ioaddr + UART_RX);
993 (void) inb(info->ioaddr + UART_IIR);
994 (void) inb(info->ioaddr + UART_MSR);
995
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100996 if (info->port.tty)
997 clear_bit(TTY_IO_ERROR, &info->port.tty->flags);
Jiri Slaby1c456072008-02-07 00:16:46 -0800998 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
999
1000 /*
1001 * and set the speed of the serial port
1002 */
1003 mxser_change_speed(info, NULL);
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001004 info->port.flags |= ASYNC_INITIALIZED;
Jiri Slaby1c456072008-02-07 00:16:46 -08001005 spin_unlock_irqrestore(&info->slock, flags);
1006
1007 return 0;
1008}
1009
1010/*
1011 * This routine will shutdown a serial port; interrupts maybe disabled, and
1012 * DTR is dropped if the hangup on close termio flag is on.
1013 */
1014static void mxser_shutdown(struct mxser_port *info)
1015{
1016 unsigned long flags;
1017
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001018 if (!(info->port.flags & ASYNC_INITIALIZED))
Jiri Slaby1c456072008-02-07 00:16:46 -08001019 return;
1020
1021 spin_lock_irqsave(&info->slock, flags);
1022
1023 /*
1024 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
1025 * here so the queue might never be waken up
1026 */
1027 wake_up_interruptible(&info->delta_msr_wait);
1028
1029 /*
1030 * Free the IRQ, if necessary
1031 */
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001032 if (info->port.xmit_buf) {
1033 free_page((unsigned long) info->port.xmit_buf);
1034 info->port.xmit_buf = NULL;
Jiri Slaby1c456072008-02-07 00:16:46 -08001035 }
1036
1037 info->IER = 0;
1038 outb(0x00, info->ioaddr + UART_IER);
1039
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001040 if (!info->port.tty || (info->port.tty->termios->c_cflag & HUPCL))
Jiri Slaby1c456072008-02-07 00:16:46 -08001041 info->MCR &= ~(UART_MCR_DTR | UART_MCR_RTS);
1042 outb(info->MCR, info->ioaddr + UART_MCR);
1043
1044 /* clear Rx/Tx FIFO's */
1045 if (info->board->chip_flag)
1046 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT |
1047 MOXA_MUST_FCR_GDA_MODE_ENABLE,
1048 info->ioaddr + UART_FCR);
1049 else
1050 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
1051 info->ioaddr + UART_FCR);
1052
1053 /* read data port to reset things */
1054 (void) inb(info->ioaddr + UART_RX);
1055
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001056 if (info->port.tty)
1057 set_bit(TTY_IO_ERROR, &info->port.tty->flags);
Jiri Slaby1c456072008-02-07 00:16:46 -08001058
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001059 info->port.flags &= ~ASYNC_INITIALIZED;
Jiri Slaby1c456072008-02-07 00:16:46 -08001060
1061 if (info->board->chip_flag)
1062 SET_MOXA_MUST_NO_SOFTWARE_FLOW_CONTROL(info->ioaddr);
1063
1064 spin_unlock_irqrestore(&info->slock, flags);
1065}
1066
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067/*
1068 * This routine is called whenever a serial port is opened. It
1069 * enables interrupts for a serial port, linking in its async structure into
1070 * the IRQ chain. It also performs the serial-specific
1071 * initialization for the tty structure.
1072 */
1073static int mxser_open(struct tty_struct *tty, struct file *filp)
1074{
Jiri Slaby1c456072008-02-07 00:16:46 -08001075 struct mxser_port *info;
1076 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 int retval, line;
1078
1079 line = tty->index;
1080 if (line == MXSER_PORTS)
1081 return 0;
1082 if (line < 0 || line > MXSER_PORTS)
1083 return -ENODEV;
Jiri Slaby1c456072008-02-07 00:16:46 -08001084 info = &mxser_boards[line / MXSER_PORTS_PER_BOARD].ports[line % MXSER_PORTS_PER_BOARD];
1085 if (!info->ioaddr)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001086 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
1088 tty->driver_data = info;
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001089 info->port.tty = tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 /*
1091 * Start up serial port
1092 */
Jiri Slaby1c456072008-02-07 00:16:46 -08001093 spin_lock_irqsave(&info->slock, flags);
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001094 info->port.count++;
Jiri Slaby1c456072008-02-07 00:16:46 -08001095 spin_unlock_irqrestore(&info->slock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 retval = mxser_startup(info);
1097 if (retval)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001098 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
1100 retval = mxser_block_til_ready(tty, filp, info);
1101 if (retval)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001102 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
Cedric Le Goater8cddd702007-02-10 01:46:33 -08001104 /* unmark here for very high baud rate (ex. 921600 bps) used */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 tty->low_latency = 1;
1106 return 0;
1107}
1108
Alan Cox978e5952008-04-30 00:53:59 -07001109static void mxser_flush_buffer(struct tty_struct *tty)
1110{
1111 struct mxser_port *info = tty->driver_data;
1112 char fcr;
1113 unsigned long flags;
1114
1115
1116 spin_lock_irqsave(&info->slock, flags);
1117 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1118
1119 fcr = inb(info->ioaddr + UART_FCR);
1120 outb((fcr | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
1121 info->ioaddr + UART_FCR);
1122 outb(fcr, info->ioaddr + UART_FCR);
1123
1124 spin_unlock_irqrestore(&info->slock, flags);
1125
1126 tty_wakeup(tty);
1127}
1128
1129
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130/*
1131 * This routine is called when the serial port gets closed. First, we
1132 * wait for the last remaining data to be sent. Then, we unlink its
1133 * async structure from the interrupt chain if necessary, and we free
1134 * that IRQ if nothing is left in the chain.
1135 */
1136static void mxser_close(struct tty_struct *tty, struct file *filp)
1137{
Jiri Slaby1c456072008-02-07 00:16:46 -08001138 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
1140 unsigned long timeout;
1141 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
1143 if (tty->index == MXSER_PORTS)
1144 return;
1145 if (!info)
Kirill Smelkov6f08b722005-11-07 00:59:23 -08001146 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
1148 spin_lock_irqsave(&info->slock, flags);
1149
1150 if (tty_hung_up_p(filp)) {
1151 spin_unlock_irqrestore(&info->slock, flags);
1152 return;
1153 }
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001154 if ((tty->count == 1) && (info->port.count != 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 /*
1156 * Uh, oh. tty->count is 1, which means that the tty
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001157 * structure will be freed. Info->port.count should always
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 * be one in these conditions. If it's greater than
1159 * one, we've got real problems, since it means the
1160 * serial port won't be shutdown.
1161 */
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001162 printk(KERN_ERR "mxser_close: bad serial port count; "
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001163 "tty->count is 1, info->port.count is %d\n", info->port.count);
1164 info->port.count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 }
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001166 if (--info->port.count < 0) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001167 printk(KERN_ERR "mxser_close: bad serial port count for "
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001168 "ttys%d: %d\n", tty->index, info->port.count);
1169 info->port.count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 }
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001171 if (info->port.count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 spin_unlock_irqrestore(&info->slock, flags);
1173 return;
1174 }
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001175 info->port.flags |= ASYNC_CLOSING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 spin_unlock_irqrestore(&info->slock, flags);
1177 /*
1178 * Save the termios structure, since this port may have
1179 * separate termios for callout and dialin.
1180 */
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001181 if (info->port.flags & ASYNC_NORMAL_ACTIVE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 info->normal_termios = *tty->termios;
1183 /*
1184 * Now we wait for the transmit buffer to clear; and we notify
1185 * the line discipline to only process XON/XOFF characters.
1186 */
1187 tty->closing = 1;
Alan Cox44b7d1b2008-07-16 21:57:18 +01001188 if (info->port.closing_wait != ASYNC_CLOSING_WAIT_NONE)
1189 tty_wait_until_sent(tty, info->port.closing_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 /*
1191 * At this point we stop accepting input. To do this, we
1192 * disable the receive line status interrupts, and tell the
1193 * interrupt driver to stop checking the data ready bit in the
1194 * line status register.
1195 */
1196 info->IER &= ~UART_IER_RLSI;
Jiri Slaby1c456072008-02-07 00:16:46 -08001197 if (info->board->chip_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 info->IER &= ~MOXA_MUST_RECV_ISR;
Jiri Slaby1c456072008-02-07 00:16:46 -08001199
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001200 if (info->port.flags & ASYNC_INITIALIZED) {
Jiri Slaby1c456072008-02-07 00:16:46 -08001201 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 /*
1203 * Before we drop DTR, make sure the UART transmitter
1204 * has completely drained; this is especially
1205 * important if there is a transmit FIFO!
1206 */
1207 timeout = jiffies + HZ;
Jiri Slaby1c456072008-02-07 00:16:46 -08001208 while (!(inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT)) {
Nishanth Aravamudanda4cd8d2005-09-10 00:27:30 -07001209 schedule_timeout_interruptible(5);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 if (time_after(jiffies, timeout))
1211 break;
1212 }
1213 }
1214 mxser_shutdown(info);
1215
Alan Cox978e5952008-04-30 00:53:59 -07001216 mxser_flush_buffer(tty);
Jiri Slaby1c456072008-02-07 00:16:46 -08001217 tty_ldisc_flush(tty);
1218
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 tty->closing = 0;
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001220 info->port.tty = NULL;
1221 if (info->port.blocked_open) {
Alan Cox44b7d1b2008-07-16 21:57:18 +01001222 if (info->port.close_delay)
1223 schedule_timeout_interruptible(info->port.close_delay);
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001224 wake_up_interruptible(&info->port.open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 }
1226
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001227 info->port.flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228}
1229
1230static int mxser_write(struct tty_struct *tty, const unsigned char *buf, int count)
1231{
1232 int c, total = 0;
Jiri Slaby1c456072008-02-07 00:16:46 -08001233 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 unsigned long flags;
1235
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001236 if (!info->port.xmit_buf)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001237 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
1239 while (1) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001240 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1241 SERIAL_XMIT_SIZE - info->xmit_head));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 if (c <= 0)
1243 break;
1244
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001245 memcpy(info->port.xmit_buf + info->xmit_head, buf, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 spin_lock_irqsave(&info->slock, flags);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001247 info->xmit_head = (info->xmit_head + c) &
1248 (SERIAL_XMIT_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 info->xmit_cnt += c;
1250 spin_unlock_irqrestore(&info->slock, flags);
1251
1252 buf += c;
1253 count -= c;
1254 total += c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 }
1256
Jiri Slaby1c456072008-02-07 00:16:46 -08001257 if (info->xmit_cnt && !tty->stopped) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001258 if (!tty->hw_stopped ||
1259 (info->type == PORT_16550A) ||
Jiri Slaby1c456072008-02-07 00:16:46 -08001260 (info->board->chip_flag)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby1c456072008-02-07 00:16:46 -08001262 outb(info->IER & ~UART_IER_THRI, info->ioaddr +
1263 UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 info->IER |= UART_IER_THRI;
Jiri Slaby1c456072008-02-07 00:16:46 -08001265 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 spin_unlock_irqrestore(&info->slock, flags);
1267 }
1268 }
1269 return total;
1270}
1271
Alan Cox0be2ead2008-04-30 00:54:03 -07001272static int mxser_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273{
Jiri Slaby1c456072008-02-07 00:16:46 -08001274 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 unsigned long flags;
1276
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001277 if (!info->port.xmit_buf)
Alan Cox0be2ead2008-04-30 00:54:03 -07001278 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
1280 if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1)
Alan Cox0be2ead2008-04-30 00:54:03 -07001281 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
1283 spin_lock_irqsave(&info->slock, flags);
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001284 info->port.xmit_buf[info->xmit_head++] = ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 info->xmit_head &= SERIAL_XMIT_SIZE - 1;
1286 info->xmit_cnt++;
1287 spin_unlock_irqrestore(&info->slock, flags);
Jiri Slaby1c456072008-02-07 00:16:46 -08001288 if (!tty->stopped) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001289 if (!tty->hw_stopped ||
1290 (info->type == PORT_16550A) ||
Jiri Slaby1c456072008-02-07 00:16:46 -08001291 info->board->chip_flag) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby1c456072008-02-07 00:16:46 -08001293 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 info->IER |= UART_IER_THRI;
Jiri Slaby1c456072008-02-07 00:16:46 -08001295 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 spin_unlock_irqrestore(&info->slock, flags);
1297 }
1298 }
Alan Cox0be2ead2008-04-30 00:54:03 -07001299 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300}
1301
1302
1303static void mxser_flush_chars(struct tty_struct *tty)
1304{
Jiri Slaby1c456072008-02-07 00:16:46 -08001305 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 unsigned long flags;
1307
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001308 if (info->xmit_cnt <= 0 ||
1309 tty->stopped ||
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001310 !info->port.xmit_buf ||
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001311 (tty->hw_stopped &&
1312 (info->type != PORT_16550A) &&
Jiri Slaby1c456072008-02-07 00:16:46 -08001313 (!info->board->chip_flag)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001314 ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 return;
1316
1317 spin_lock_irqsave(&info->slock, flags);
1318
Jiri Slaby1c456072008-02-07 00:16:46 -08001319 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 info->IER |= UART_IER_THRI;
Jiri Slaby1c456072008-02-07 00:16:46 -08001321 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
1323 spin_unlock_irqrestore(&info->slock, flags);
1324}
1325
1326static int mxser_write_room(struct tty_struct *tty)
1327{
Jiri Slaby1c456072008-02-07 00:16:46 -08001328 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 int ret;
1330
1331 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1332 if (ret < 0)
1333 ret = 0;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001334 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335}
1336
1337static int mxser_chars_in_buffer(struct tty_struct *tty)
1338{
Jiri Slaby1c456072008-02-07 00:16:46 -08001339 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 return info->xmit_cnt;
1341}
1342
Jiri Slaby1c456072008-02-07 00:16:46 -08001343/*
1344 * ------------------------------------------------------------
1345 * friends of mxser_ioctl()
1346 * ------------------------------------------------------------
1347 */
1348static int mxser_get_serial_info(struct mxser_port *info,
1349 struct serial_struct __user *retinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350{
Jiri Slaby1c456072008-02-07 00:16:46 -08001351 struct serial_struct tmp = {
1352 .type = info->type,
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001353 .line = info->port.tty->index,
Jiri Slaby1c456072008-02-07 00:16:46 -08001354 .port = info->ioaddr,
1355 .irq = info->board->irq,
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001356 .flags = info->port.flags,
Jiri Slaby1c456072008-02-07 00:16:46 -08001357 .baud_base = info->baud_base,
Alan Cox44b7d1b2008-07-16 21:57:18 +01001358 .close_delay = info->port.close_delay,
1359 .closing_wait = info->port.closing_wait,
Jiri Slaby1c456072008-02-07 00:16:46 -08001360 .custom_divisor = info->custom_divisor,
1361 .hub6 = 0
1362 };
1363 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1364 return -EFAULT;
1365 return 0;
1366}
1367
1368static int mxser_set_serial_info(struct mxser_port *info,
1369 struct serial_struct __user *new_info)
1370{
1371 struct serial_struct new_serial;
Jiri Slaby80ff8a82008-02-07 00:16:51 -08001372 speed_t baud;
Jiri Slaby1c456072008-02-07 00:16:46 -08001373 unsigned long sl_flags;
1374 unsigned int flags;
1375 int retval = 0;
1376
1377 if (!new_info || !info->ioaddr)
Jiri Slaby80ff8a82008-02-07 00:16:51 -08001378 return -ENODEV;
Jiri Slaby1c456072008-02-07 00:16:46 -08001379 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
1380 return -EFAULT;
1381
Jiri Slaby80ff8a82008-02-07 00:16:51 -08001382 if (new_serial.irq != info->board->irq ||
1383 new_serial.port != info->ioaddr)
1384 return -EINVAL;
Jiri Slaby1c456072008-02-07 00:16:46 -08001385
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001386 flags = info->port.flags & ASYNC_SPD_MASK;
Jiri Slaby1c456072008-02-07 00:16:46 -08001387
1388 if (!capable(CAP_SYS_ADMIN)) {
1389 if ((new_serial.baud_base != info->baud_base) ||
Alan Cox44b7d1b2008-07-16 21:57:18 +01001390 (new_serial.close_delay != info->port.close_delay) ||
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001391 ((new_serial.flags & ~ASYNC_USR_MASK) != (info->port.flags & ~ASYNC_USR_MASK)))
Jiri Slaby1c456072008-02-07 00:16:46 -08001392 return -EPERM;
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001393 info->port.flags = ((info->port.flags & ~ASYNC_USR_MASK) |
Jiri Slaby1c456072008-02-07 00:16:46 -08001394 (new_serial.flags & ASYNC_USR_MASK));
1395 } else {
1396 /*
1397 * OK, past this point, all the error checking has been done.
1398 * At this point, we start making changes.....
1399 */
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001400 info->port.flags = ((info->port.flags & ~ASYNC_FLAGS) |
Jiri Slaby1c456072008-02-07 00:16:46 -08001401 (new_serial.flags & ASYNC_FLAGS));
Alan Cox44b7d1b2008-07-16 21:57:18 +01001402 info->port.close_delay = new_serial.close_delay * HZ / 100;
1403 info->port.closing_wait = new_serial.closing_wait * HZ / 100;
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001404 info->port.tty->low_latency =
1405 (info->port.flags & ASYNC_LOW_LATENCY) ? 1 : 0;
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001406 if ((info->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST &&
Jiri Slaby80ff8a82008-02-07 00:16:51 -08001407 (new_serial.baud_base != info->baud_base ||
1408 new_serial.custom_divisor !=
1409 info->custom_divisor)) {
1410 baud = new_serial.baud_base / new_serial.custom_divisor;
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001411 tty_encode_baud_rate(info->port.tty, baud, baud);
Jiri Slaby80ff8a82008-02-07 00:16:51 -08001412 }
Jiri Slaby1c456072008-02-07 00:16:46 -08001413 }
1414
1415 info->type = new_serial.type;
1416
1417 process_txrx_fifo(info);
1418
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001419 if (info->port.flags & ASYNC_INITIALIZED) {
1420 if (flags != (info->port.flags & ASYNC_SPD_MASK)) {
Jiri Slaby1c456072008-02-07 00:16:46 -08001421 spin_lock_irqsave(&info->slock, sl_flags);
1422 mxser_change_speed(info, NULL);
1423 spin_unlock_irqrestore(&info->slock, sl_flags);
1424 }
1425 } else
1426 retval = mxser_startup(info);
1427
1428 return retval;
1429}
1430
1431/*
1432 * mxser_get_lsr_info - get line status register info
1433 *
1434 * Purpose: Let user call ioctl() to get info when the UART physically
1435 * is emptied. On bus types like RS485, the transmitter must
1436 * release the bus after transmitting. This must be done when
1437 * the transmit shift register is empty, not be done when the
1438 * transmit holding register is empty. This functionality
1439 * allows an RS485 driver to be written in user space.
1440 */
1441static int mxser_get_lsr_info(struct mxser_port *info,
1442 unsigned int __user *value)
1443{
1444 unsigned char status;
1445 unsigned int result;
1446 unsigned long flags;
1447
1448 spin_lock_irqsave(&info->slock, flags);
1449 status = inb(info->ioaddr + UART_LSR);
1450 spin_unlock_irqrestore(&info->slock, flags);
1451 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1452 return put_user(result, value);
1453}
1454
Jiri Slaby1c456072008-02-07 00:16:46 -08001455static int mxser_tiocmget(struct tty_struct *tty, struct file *file)
1456{
1457 struct mxser_port *info = tty->driver_data;
1458 unsigned char control, status;
1459 unsigned long flags;
1460
1461
1462 if (tty->index == MXSER_PORTS)
1463 return -ENOIOCTLCMD;
1464 if (test_bit(TTY_IO_ERROR, &tty->flags))
1465 return -EIO;
1466
1467 control = info->MCR;
1468
1469 spin_lock_irqsave(&info->slock, flags);
1470 status = inb(info->ioaddr + UART_MSR);
1471 if (status & UART_MSR_ANY_DELTA)
1472 mxser_check_modem_status(info, status);
1473 spin_unlock_irqrestore(&info->slock, flags);
1474 return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0) |
1475 ((control & UART_MCR_DTR) ? TIOCM_DTR : 0) |
1476 ((status & UART_MSR_DCD) ? TIOCM_CAR : 0) |
1477 ((status & UART_MSR_RI) ? TIOCM_RNG : 0) |
1478 ((status & UART_MSR_DSR) ? TIOCM_DSR : 0) |
1479 ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
1480}
1481
1482static int mxser_tiocmset(struct tty_struct *tty, struct file *file,
1483 unsigned int set, unsigned int clear)
1484{
1485 struct mxser_port *info = tty->driver_data;
1486 unsigned long flags;
1487
1488
1489 if (tty->index == MXSER_PORTS)
1490 return -ENOIOCTLCMD;
1491 if (test_bit(TTY_IO_ERROR, &tty->flags))
1492 return -EIO;
1493
1494 spin_lock_irqsave(&info->slock, flags);
1495
1496 if (set & TIOCM_RTS)
1497 info->MCR |= UART_MCR_RTS;
1498 if (set & TIOCM_DTR)
1499 info->MCR |= UART_MCR_DTR;
1500
1501 if (clear & TIOCM_RTS)
1502 info->MCR &= ~UART_MCR_RTS;
1503 if (clear & TIOCM_DTR)
1504 info->MCR &= ~UART_MCR_DTR;
1505
1506 outb(info->MCR, info->ioaddr + UART_MCR);
1507 spin_unlock_irqrestore(&info->slock, flags);
1508 return 0;
1509}
1510
1511static int __init mxser_program_mode(int port)
1512{
1513 int id, i, j, n;
1514
1515 outb(0, port);
1516 outb(0, port);
1517 outb(0, port);
1518 (void)inb(port);
1519 (void)inb(port);
1520 outb(0, port);
1521 (void)inb(port);
1522
1523 id = inb(port + 1) & 0x1F;
1524 if ((id != C168_ASIC_ID) &&
1525 (id != C104_ASIC_ID) &&
1526 (id != C102_ASIC_ID) &&
1527 (id != CI132_ASIC_ID) &&
1528 (id != CI134_ASIC_ID) &&
1529 (id != CI104J_ASIC_ID))
1530 return -1;
1531 for (i = 0, j = 0; i < 4; i++) {
1532 n = inb(port + 2);
1533 if (n == 'M') {
1534 j = 1;
1535 } else if ((j == 1) && (n == 1)) {
1536 j = 2;
1537 break;
1538 } else
1539 j = 0;
1540 }
1541 if (j != 2)
1542 id = -2;
1543 return id;
1544}
1545
1546static void __init mxser_normal_mode(int port)
1547{
1548 int i, n;
1549
1550 outb(0xA5, port + 1);
1551 outb(0x80, port + 3);
1552 outb(12, port + 0); /* 9600 bps */
1553 outb(0, port + 1);
1554 outb(0x03, port + 3); /* 8 data bits */
1555 outb(0x13, port + 4); /* loop back mode */
1556 for (i = 0; i < 16; i++) {
1557 n = inb(port + 5);
1558 if ((n & 0x61) == 0x60)
1559 break;
1560 if ((n & 1) == 1)
1561 (void)inb(port);
1562 }
1563 outb(0x00, port + 4);
1564}
1565
1566#define CHIP_SK 0x01 /* Serial Data Clock in Eprom */
1567#define CHIP_DO 0x02 /* Serial Data Output in Eprom */
1568#define CHIP_CS 0x04 /* Serial Chip Select in Eprom */
1569#define CHIP_DI 0x08 /* Serial Data Input in Eprom */
1570#define EN_CCMD 0x000 /* Chip's command register */
1571#define EN0_RSARLO 0x008 /* Remote start address reg 0 */
1572#define EN0_RSARHI 0x009 /* Remote start address reg 1 */
1573#define EN0_RCNTLO 0x00A /* Remote byte count reg WR */
1574#define EN0_RCNTHI 0x00B /* Remote byte count reg WR */
1575#define EN0_DCFG 0x00E /* Data configuration reg WR */
1576#define EN0_PORT 0x010 /* Rcv missed frame error counter RD */
1577#define ENC_PAGE0 0x000 /* Select page 0 of chip registers */
1578#define ENC_PAGE3 0x0C0 /* Select page 3 of chip registers */
1579static int __init mxser_read_register(int port, unsigned short *regs)
1580{
1581 int i, k, value, id;
1582 unsigned int j;
1583
1584 id = mxser_program_mode(port);
1585 if (id < 0)
1586 return id;
1587 for (i = 0; i < 14; i++) {
1588 k = (i & 0x3F) | 0x180;
1589 for (j = 0x100; j > 0; j >>= 1) {
1590 outb(CHIP_CS, port);
1591 if (k & j) {
1592 outb(CHIP_CS | CHIP_DO, port);
1593 outb(CHIP_CS | CHIP_DO | CHIP_SK, port); /* A? bit of read */
1594 } else {
1595 outb(CHIP_CS, port);
1596 outb(CHIP_CS | CHIP_SK, port); /* A? bit of read */
1597 }
1598 }
1599 (void)inb(port);
1600 value = 0;
1601 for (k = 0, j = 0x8000; k < 16; k++, j >>= 1) {
1602 outb(CHIP_CS, port);
1603 outb(CHIP_CS | CHIP_SK, port);
1604 if (inb(port) & CHIP_DI)
1605 value |= j;
1606 }
1607 regs[i] = value;
1608 outb(0, port);
1609 }
1610 mxser_normal_mode(port);
1611 return id;
1612}
1613
1614static int mxser_ioctl_special(unsigned int cmd, void __user *argp)
1615{
1616 struct mxser_port *port;
1617 int result, status;
1618 unsigned int i, j;
Alan Cox9d6d1622008-04-30 00:53:20 -07001619 int ret = 0;
Jiri Slaby1c456072008-02-07 00:16:46 -08001620
1621 switch (cmd) {
1622 case MOXA_GET_MAJOR:
Jiri Slaby41aee9a2008-07-25 01:48:19 -07001623 printk(KERN_WARNING "mxser: '%s' uses deprecated ioctl %x, fix "
1624 "your userspace\n", current->comm, cmd);
Jiri Slaby1c456072008-02-07 00:16:46 -08001625 return put_user(ttymajor, (int __user *)argp);
1626
1627 case MOXA_CHKPORTENABLE:
1628 result = 0;
Alan Cox9d6d1622008-04-30 00:53:20 -07001629 lock_kernel();
Jiri Slaby1c456072008-02-07 00:16:46 -08001630 for (i = 0; i < MXSER_BOARDS; i++)
1631 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++)
1632 if (mxser_boards[i].ports[j].ioaddr)
1633 result |= (1 << i);
Alan Cox9d6d1622008-04-30 00:53:20 -07001634 unlock_kernel();
Jiri Slaby1c456072008-02-07 00:16:46 -08001635 return put_user(result, (unsigned long __user *)argp);
1636 case MOXA_GETDATACOUNT:
Alan Cox9d6d1622008-04-30 00:53:20 -07001637 lock_kernel();
Jiri Slaby1c456072008-02-07 00:16:46 -08001638 if (copy_to_user(argp, &mxvar_log, sizeof(mxvar_log)))
Alan Cox9d6d1622008-04-30 00:53:20 -07001639 ret = -EFAULT;
1640 unlock_kernel();
1641 return ret;
Jiri Slaby72800df2008-07-25 01:48:20 -07001642 case MOXA_GETMSTATUS: {
1643 struct mxser_mstatus ms, __user *msu = argp;
Alan Cox9d6d1622008-04-30 00:53:20 -07001644 lock_kernel();
Jiri Slaby1c456072008-02-07 00:16:46 -08001645 for (i = 0; i < MXSER_BOARDS; i++)
1646 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
1647 port = &mxser_boards[i].ports[j];
Jiri Slaby72800df2008-07-25 01:48:20 -07001648 memset(&ms, 0, sizeof(ms));
Jiri Slaby1c456072008-02-07 00:16:46 -08001649
Jiri Slaby72800df2008-07-25 01:48:20 -07001650 if (!port->ioaddr)
1651 goto copy;
Jiri Slaby1c456072008-02-07 00:16:46 -08001652
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001653 if (!port->port.tty || !port->port.tty->termios)
Jiri Slaby72800df2008-07-25 01:48:20 -07001654 ms.cflag = port->normal_termios.c_cflag;
Jiri Slaby1c456072008-02-07 00:16:46 -08001655 else
Jiri Slaby72800df2008-07-25 01:48:20 -07001656 ms.cflag = port->port.tty->termios->c_cflag;
Jiri Slaby1c456072008-02-07 00:16:46 -08001657
1658 status = inb(port->ioaddr + UART_MSR);
Jiri Slaby72800df2008-07-25 01:48:20 -07001659 if (status & UART_MSR_DCD)
1660 ms.dcd = 1;
1661 if (status & UART_MSR_DSR)
1662 ms.dsr = 1;
1663 if (status & UART_MSR_CTS)
1664 ms.cts = 1;
1665 copy:
1666 if (copy_to_user(msu, &ms, sizeof(ms))) {
1667 unlock_kernel();
1668 return -EFAULT;
1669 }
1670 msu++;
Jiri Slaby1c456072008-02-07 00:16:46 -08001671 }
Alan Cox9d6d1622008-04-30 00:53:20 -07001672 unlock_kernel();
Jiri Slaby1c456072008-02-07 00:16:46 -08001673 return 0;
Jiri Slaby72800df2008-07-25 01:48:20 -07001674 }
Jiri Slaby1c456072008-02-07 00:16:46 -08001675 case MOXA_ASPP_MON_EXT: {
Jiri Slaby72800df2008-07-25 01:48:20 -07001676 struct mxser_mon_ext *me; /* it's 2k, stack unfriendly */
1677 unsigned int cflag, iflag, p;
1678 u8 opmode;
1679
1680 me = kzalloc(sizeof(*me), GFP_KERNEL);
1681 if (!me)
1682 return -ENOMEM;
Jiri Slaby1c456072008-02-07 00:16:46 -08001683
Alan Cox9d6d1622008-04-30 00:53:20 -07001684 lock_kernel();
Jiri Slaby72800df2008-07-25 01:48:20 -07001685 for (i = 0, p = 0; i < MXSER_BOARDS; i++) {
1686 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++, p++) {
1687 if (p >= ARRAY_SIZE(me->rx_cnt)) {
1688 i = MXSER_BOARDS;
1689 break;
1690 }
Jiri Slaby1c456072008-02-07 00:16:46 -08001691 port = &mxser_boards[i].ports[j];
1692 if (!port->ioaddr)
1693 continue;
1694
Jiri Slaby72800df2008-07-25 01:48:20 -07001695 status = mxser_get_msr(port->ioaddr, 0, p);
Jiri Slaby1c456072008-02-07 00:16:46 -08001696
1697 if (status & UART_MSR_TERI)
1698 port->icount.rng++;
1699 if (status & UART_MSR_DDSR)
1700 port->icount.dsr++;
1701 if (status & UART_MSR_DDCD)
1702 port->icount.dcd++;
1703 if (status & UART_MSR_DCTS)
1704 port->icount.cts++;
1705
1706 port->mon_data.modem_status = status;
Jiri Slaby72800df2008-07-25 01:48:20 -07001707 me->rx_cnt[p] = port->mon_data.rxcnt;
1708 me->tx_cnt[p] = port->mon_data.txcnt;
1709 me->up_rxcnt[p] = port->mon_data.up_rxcnt;
1710 me->up_txcnt[p] = port->mon_data.up_txcnt;
1711 me->modem_status[p] =
Jiri Slaby1c456072008-02-07 00:16:46 -08001712 port->mon_data.modem_status;
Jiri Slaby72800df2008-07-25 01:48:20 -07001713 me->baudrate[p] = tty_get_baud_rate(port->port.tty);
Jiri Slaby1c456072008-02-07 00:16:46 -08001714
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001715 if (!port->port.tty || !port->port.tty->termios) {
Jiri Slaby1c456072008-02-07 00:16:46 -08001716 cflag = port->normal_termios.c_cflag;
1717 iflag = port->normal_termios.c_iflag;
1718 } else {
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001719 cflag = port->port.tty->termios->c_cflag;
1720 iflag = port->port.tty->termios->c_iflag;
Jiri Slaby1c456072008-02-07 00:16:46 -08001721 }
1722
Jiri Slaby72800df2008-07-25 01:48:20 -07001723 me->databits[p] = cflag & CSIZE;
1724 me->stopbits[p] = cflag & CSTOPB;
1725 me->parity[p] = cflag & (PARENB | PARODD |
1726 CMSPAR);
Jiri Slaby1c456072008-02-07 00:16:46 -08001727
1728 if (cflag & CRTSCTS)
Jiri Slaby72800df2008-07-25 01:48:20 -07001729 me->flowctrl[p] |= 0x03;
Jiri Slaby1c456072008-02-07 00:16:46 -08001730
1731 if (iflag & (IXON | IXOFF))
Jiri Slaby72800df2008-07-25 01:48:20 -07001732 me->flowctrl[p] |= 0x0C;
Jiri Slaby1c456072008-02-07 00:16:46 -08001733
1734 if (port->type == PORT_16550A)
Jiri Slaby72800df2008-07-25 01:48:20 -07001735 me->fifo[p] = 1;
Jiri Slaby1c456072008-02-07 00:16:46 -08001736
Jiri Slaby72800df2008-07-25 01:48:20 -07001737 opmode = inb(port->opmode_ioaddr) >>
1738 ((p % 4) * 2);
Jiri Slaby1c456072008-02-07 00:16:46 -08001739 opmode &= OP_MODE_MASK;
Jiri Slaby72800df2008-07-25 01:48:20 -07001740 me->iftype[p] = opmode;
Jiri Slaby1c456072008-02-07 00:16:46 -08001741 }
Alan Cox9d6d1622008-04-30 00:53:20 -07001742 }
1743 unlock_kernel();
Jiri Slaby72800df2008-07-25 01:48:20 -07001744 if (copy_to_user(argp, me, sizeof(*me)))
1745 ret = -EFAULT;
1746 kfree(me);
1747 return ret;
Alan Cox9d6d1622008-04-30 00:53:20 -07001748 }
1749 default:
Jiri Slaby1c456072008-02-07 00:16:46 -08001750 return -ENOIOCTLCMD;
1751 }
1752 return 0;
1753}
1754
1755static int mxser_cflags_changed(struct mxser_port *info, unsigned long arg,
1756 struct async_icount *cprev)
1757{
1758 struct async_icount cnow;
1759 unsigned long flags;
1760 int ret;
1761
1762 spin_lock_irqsave(&info->slock, flags);
1763 cnow = info->icount; /* atomic copy */
1764 spin_unlock_irqrestore(&info->slock, flags);
1765
1766 ret = ((arg & TIOCM_RNG) && (cnow.rng != cprev->rng)) ||
1767 ((arg & TIOCM_DSR) && (cnow.dsr != cprev->dsr)) ||
1768 ((arg & TIOCM_CD) && (cnow.dcd != cprev->dcd)) ||
1769 ((arg & TIOCM_CTS) && (cnow.cts != cprev->cts));
1770
1771 *cprev = cnow;
1772
1773 return ret;
1774}
1775
1776static int mxser_ioctl(struct tty_struct *tty, struct file *file,
1777 unsigned int cmd, unsigned long arg)
1778{
1779 struct mxser_port *info = tty->driver_data;
1780 struct async_icount cnow;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 unsigned long flags;
1782 void __user *argp = (void __user *)arg;
Jiri Slaby1c456072008-02-07 00:16:46 -08001783 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784
1785 if (tty->index == MXSER_PORTS)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001786 return mxser_ioctl_special(cmd, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 if (cmd == MOXA_SET_OP_MODE || cmd == MOXA_GET_OP_MODE) {
Jiri Slaby1c456072008-02-07 00:16:46 -08001789 int p;
1790 unsigned long opmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 static unsigned char ModeMask[] = { 0xfc, 0xf3, 0xcf, 0x3f };
1792 int shiftbit;
1793 unsigned char val, mask;
1794
Jiri Slaby1c456072008-02-07 00:16:46 -08001795 p = tty->index % 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 if (cmd == MOXA_SET_OP_MODE) {
1797 if (get_user(opmode, (int __user *) argp))
1798 return -EFAULT;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001799 if (opmode != RS232_MODE &&
1800 opmode != RS485_2WIRE_MODE &&
1801 opmode != RS422_MODE &&
1802 opmode != RS485_4WIRE_MODE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 return -EFAULT;
Alan Cox9d6d1622008-04-30 00:53:20 -07001804 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 mask = ModeMask[p];
1806 shiftbit = p * 2;
1807 val = inb(info->opmode_ioaddr);
1808 val &= mask;
1809 val |= (opmode << shiftbit);
1810 outb(val, info->opmode_ioaddr);
Alan Cox9d6d1622008-04-30 00:53:20 -07001811 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 } else {
Alan Cox9d6d1622008-04-30 00:53:20 -07001813 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 shiftbit = p * 2;
1815 opmode = inb(info->opmode_ioaddr) >> shiftbit;
1816 opmode &= OP_MODE_MASK;
Alan Cox9d6d1622008-04-30 00:53:20 -07001817 unlock_kernel();
Jiri Slaby1c456072008-02-07 00:16:46 -08001818 if (put_user(opmode, (int __user *)argp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 return -EFAULT;
1820 }
1821 return 0;
1822 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823
Jiri Slaby1c456072008-02-07 00:16:46 -08001824 if (cmd != TIOCGSERIAL && cmd != TIOCMIWAIT && cmd != TIOCGICOUNT &&
1825 test_bit(TTY_IO_ERROR, &tty->flags))
1826 return -EIO;
1827
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 case TIOCGSERIAL:
Alan Cox9d6d1622008-04-30 00:53:20 -07001830 lock_kernel();
1831 retval = mxser_get_serial_info(info, argp);
1832 unlock_kernel();
1833 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 case TIOCSSERIAL:
Alan Cox9d6d1622008-04-30 00:53:20 -07001835 lock_kernel();
1836 retval = mxser_set_serial_info(info, argp);
1837 unlock_kernel();
1838 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 case TIOCSERGETLSR: /* Get line status register */
Alan Cox9d6d1622008-04-30 00:53:20 -07001840 return mxser_get_lsr_info(info, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 /*
1842 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1843 * - mask passed in arg for lines of interest
1844 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1845 * Caller should use TIOCGICOUNT to see which one it was
1846 */
Jiri Slabyfc838152007-04-23 14:41:04 -07001847 case TIOCMIWAIT:
1848 spin_lock_irqsave(&info->slock, flags);
1849 cnow = info->icount; /* note the counters on entry */
1850 spin_unlock_irqrestore(&info->slock, flags);
1851
Jiri Slaby1c456072008-02-07 00:16:46 -08001852 return wait_event_interruptible(info->delta_msr_wait,
1853 mxser_cflags_changed(info, arg, &cnow));
1854 /*
1855 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1856 * Return: write counters to the user passed counter struct
1857 * NB: both 1->0 and 0->1 transitions are counted except for
1858 * RI where only 0->1 is counted.
1859 */
Jiri Slaby41aee9a2008-07-25 01:48:19 -07001860 case TIOCGICOUNT: {
1861 struct serial_icounter_struct icnt = { 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 spin_lock_irqsave(&info->slock, flags);
1863 cnow = info->icount;
1864 spin_unlock_irqrestore(&info->slock, flags);
Jiri Slaby41aee9a2008-07-25 01:48:19 -07001865
1866 icnt.frame = cnow.frame;
1867 icnt.brk = cnow.brk;
1868 icnt.overrun = cnow.overrun;
1869 icnt.buf_overrun = cnow.buf_overrun;
1870 icnt.parity = cnow.parity;
1871 icnt.rx = cnow.rx;
1872 icnt.tx = cnow.tx;
1873 icnt.cts = cnow.cts;
1874 icnt.dsr = cnow.dsr;
1875 icnt.rng = cnow.rng;
1876 icnt.dcd = cnow.dcd;
1877
1878 return copy_to_user(argp, &icnt, sizeof(icnt)) ? -EFAULT : 0;
1879 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 case MOXA_HighSpeedOn:
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001881 return put_user(info->baud_base != 115200 ? 1 : 0, (int __user *)argp);
Jiri Slaby1c456072008-02-07 00:16:46 -08001882 case MOXA_SDS_RSTICOUNTER:
Alan Cox9d6d1622008-04-30 00:53:20 -07001883 lock_kernel();
Jiri Slaby1c456072008-02-07 00:16:46 -08001884 info->mon_data.rxcnt = 0;
1885 info->mon_data.txcnt = 0;
Alan Cox9d6d1622008-04-30 00:53:20 -07001886 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 return 0;
1888
1889 case MOXA_ASPP_OQUEUE:{
Jiri Slaby1c456072008-02-07 00:16:46 -08001890 int len, lsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
Alan Cox9d6d1622008-04-30 00:53:20 -07001892 lock_kernel();
Jiri Slaby1c456072008-02-07 00:16:46 -08001893 len = mxser_chars_in_buffer(tty);
Jiri Slaby1c456072008-02-07 00:16:46 -08001894 lsr = inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT;
Jiri Slaby1c456072008-02-07 00:16:46 -08001895 len += (lsr ? 0 : 1);
Alan Cox9d6d1622008-04-30 00:53:20 -07001896 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897
Jiri Slaby1c456072008-02-07 00:16:46 -08001898 return put_user(len, (int __user *)argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 }
Jiri Slaby1c456072008-02-07 00:16:46 -08001900 case MOXA_ASPP_MON: {
1901 int mcr, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
Alan Cox9d6d1622008-04-30 00:53:20 -07001903 lock_kernel();
Jiri Slaby1c456072008-02-07 00:16:46 -08001904 status = mxser_get_msr(info->ioaddr, 1, tty->index);
1905 mxser_check_modem_status(info, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906
Jiri Slaby1c456072008-02-07 00:16:46 -08001907 mcr = inb(info->ioaddr + UART_MCR);
1908 if (mcr & MOXA_MUST_MCR_XON_FLAG)
1909 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFHOLD;
1910 else
1911 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFHOLD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912
Jiri Slaby1c456072008-02-07 00:16:46 -08001913 if (mcr & MOXA_MUST_MCR_TX_XON)
1914 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFXENT;
1915 else
1916 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFXENT;
1917
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001918 if (info->port.tty->hw_stopped)
Jiri Slaby1c456072008-02-07 00:16:46 -08001919 info->mon_data.hold_reason |= NPPI_NOTIFY_CTSHOLD;
1920 else
1921 info->mon_data.hold_reason &= ~NPPI_NOTIFY_CTSHOLD;
Alan Cox9d6d1622008-04-30 00:53:20 -07001922 unlock_kernel();
Jiri Slaby1c456072008-02-07 00:16:46 -08001923 if (copy_to_user(argp, &info->mon_data,
1924 sizeof(struct mxser_mon)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 return -EFAULT;
Jiri Slaby1c456072008-02-07 00:16:46 -08001926
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 return 0;
Jiri Slaby1c456072008-02-07 00:16:46 -08001928 }
1929 case MOXA_ASPP_LSTATUS: {
1930 if (put_user(info->err_shadow, (unsigned char __user *)argp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932
Jiri Slaby1c456072008-02-07 00:16:46 -08001933 info->err_shadow = 0;
1934 return 0;
1935 }
1936 case MOXA_SET_BAUD_METHOD: {
1937 int method;
1938
1939 if (get_user(method, (int __user *)argp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 return -EFAULT;
Jiri Slaby1c456072008-02-07 00:16:46 -08001941 mxser_set_baud_method[tty->index] = method;
1942 return put_user(method, (int __user *)argp);
1943 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 default:
1945 return -ENOIOCTLCMD;
1946 }
1947 return 0;
1948}
1949
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950static void mxser_stoprx(struct tty_struct *tty)
1951{
Jiri Slaby1c456072008-02-07 00:16:46 -08001952 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953
1954 info->ldisc_stop_rx = 1;
1955 if (I_IXOFF(tty)) {
Jiri Slaby1c456072008-02-07 00:16:46 -08001956 if (info->board->chip_flag) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 info->IER &= ~MOXA_MUST_RECV_ISR;
Jiri Slaby1c456072008-02-07 00:16:46 -08001958 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 info->x_char = STOP_CHAR(tty);
Jiri Slaby1c456072008-02-07 00:16:46 -08001961 outb(0, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 info->IER |= UART_IER_THRI;
Jiri Slaby1c456072008-02-07 00:16:46 -08001963 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 }
1965 }
1966
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001967 if (info->port.tty->termios->c_cflag & CRTSCTS) {
Jiri Slaby1c456072008-02-07 00:16:46 -08001968 info->MCR &= ~UART_MCR_RTS;
1969 outb(info->MCR, info->ioaddr + UART_MCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 }
1971}
1972
1973/*
1974 * This routine is called by the upper-layer tty layer to signal that
1975 * incoming characters should be throttled.
1976 */
1977static void mxser_throttle(struct tty_struct *tty)
1978{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 mxser_stoprx(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980}
1981
1982static void mxser_unthrottle(struct tty_struct *tty)
1983{
Jiri Slaby1c456072008-02-07 00:16:46 -08001984 struct mxser_port *info = tty->driver_data;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001985
Jiri Slaby1c456072008-02-07 00:16:46 -08001986 /* startrx */
1987 info->ldisc_stop_rx = 0;
1988 if (I_IXOFF(tty)) {
1989 if (info->x_char)
1990 info->x_char = 0;
1991 else {
1992 if (info->board->chip_flag) {
1993 info->IER |= MOXA_MUST_RECV_ISR;
1994 outb(info->IER, info->ioaddr + UART_IER);
1995 } else {
1996 info->x_char = START_CHAR(tty);
1997 outb(0, info->ioaddr + UART_IER);
1998 info->IER |= UART_IER_THRI;
1999 outb(info->IER, info->ioaddr + UART_IER);
2000 }
2001 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 }
2003
Alan Cox0ad9e7d2008-07-16 21:56:10 +01002004 if (info->port.tty->termios->c_cflag & CRTSCTS) {
Jiri Slaby1c456072008-02-07 00:16:46 -08002005 info->MCR |= UART_MCR_RTS;
2006 outb(info->MCR, info->ioaddr + UART_MCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 }
2008}
2009
2010/*
2011 * mxser_stop() and mxser_start()
2012 *
2013 * This routines are called before setting or resetting tty->stopped.
2014 * They enable or disable transmitter interrupts, as necessary.
2015 */
2016static void mxser_stop(struct tty_struct *tty)
2017{
Jiri Slaby1c456072008-02-07 00:16:46 -08002018 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 unsigned long flags;
2020
2021 spin_lock_irqsave(&info->slock, flags);
2022 if (info->IER & UART_IER_THRI) {
2023 info->IER &= ~UART_IER_THRI;
Jiri Slaby1c456072008-02-07 00:16:46 -08002024 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 }
2026 spin_unlock_irqrestore(&info->slock, flags);
2027}
2028
2029static void mxser_start(struct tty_struct *tty)
2030{
Jiri Slaby1c456072008-02-07 00:16:46 -08002031 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 unsigned long flags;
2033
2034 spin_lock_irqsave(&info->slock, flags);
Alan Cox0ad9e7d2008-07-16 21:56:10 +01002035 if (info->xmit_cnt && info->port.xmit_buf) {
Jiri Slaby1c456072008-02-07 00:16:46 -08002036 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 info->IER |= UART_IER_THRI;
Jiri Slaby1c456072008-02-07 00:16:46 -08002038 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 }
2040 spin_unlock_irqrestore(&info->slock, flags);
2041}
2042
Jiri Slaby1c456072008-02-07 00:16:46 -08002043static void mxser_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
2044{
2045 struct mxser_port *info = tty->driver_data;
2046 unsigned long flags;
2047
2048 spin_lock_irqsave(&info->slock, flags);
2049 mxser_change_speed(info, old_termios);
2050 spin_unlock_irqrestore(&info->slock, flags);
2051
2052 if ((old_termios->c_cflag & CRTSCTS) &&
2053 !(tty->termios->c_cflag & CRTSCTS)) {
2054 tty->hw_stopped = 0;
2055 mxser_start(tty);
2056 }
2057
2058 /* Handle sw stopped */
2059 if ((old_termios->c_iflag & IXON) &&
2060 !(tty->termios->c_iflag & IXON)) {
2061 tty->stopped = 0;
2062
2063 if (info->board->chip_flag) {
2064 spin_lock_irqsave(&info->slock, flags);
Christoph Hellwig148ff862008-04-30 00:54:29 -07002065 mxser_disable_must_rx_software_flow_control(
2066 info->ioaddr);
Jiri Slaby1c456072008-02-07 00:16:46 -08002067 spin_unlock_irqrestore(&info->slock, flags);
2068 }
2069
2070 mxser_start(tty);
2071 }
2072}
2073
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074/*
2075 * mxser_wait_until_sent() --- wait until the transmitter is empty
2076 */
2077static void mxser_wait_until_sent(struct tty_struct *tty, int timeout)
2078{
Jiri Slaby1c456072008-02-07 00:16:46 -08002079 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 unsigned long orig_jiffies, char_time;
2081 int lsr;
2082
2083 if (info->type == PORT_UNKNOWN)
2084 return;
2085
2086 if (info->xmit_fifo_size == 0)
2087 return; /* Just in case.... */
2088
2089 orig_jiffies = jiffies;
2090 /*
2091 * Set the check interval to be 1/5 of the estimated time to
2092 * send a single character, and make it at least 1. The check
2093 * interval should also be less than the timeout.
2094 *
2095 * Note: we have to use pretty tight timings here to satisfy
2096 * the NIST-PCTS.
2097 */
2098 char_time = (info->timeout - HZ / 50) / info->xmit_fifo_size;
2099 char_time = char_time / 5;
2100 if (char_time == 0)
2101 char_time = 1;
2102 if (timeout && timeout < char_time)
2103 char_time = timeout;
2104 /*
2105 * If the transmitter hasn't cleared in twice the approximate
2106 * amount of time to send the entire FIFO, it probably won't
2107 * ever clear. This assumes the UART isn't doing flow
2108 * control, which is currently the case. Hence, if it ever
2109 * takes longer than info->timeout, this is probably due to a
2110 * UART bug of some kind. So, we clamp the timeout parameter at
2111 * 2*info->timeout.
2112 */
2113 if (!timeout || timeout > 2 * info->timeout)
2114 timeout = 2 * info->timeout;
2115#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002116 printk(KERN_DEBUG "In rs_wait_until_sent(%d) check=%lu...",
2117 timeout, char_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 printk("jiff=%lu...", jiffies);
2119#endif
Alan Cox978e5952008-04-30 00:53:59 -07002120 lock_kernel();
Jiri Slaby1c456072008-02-07 00:16:46 -08002121 while (!((lsr = inb(info->ioaddr + UART_LSR)) & UART_LSR_TEMT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2123 printk("lsr = %d (jiff=%lu)...", lsr, jiffies);
2124#endif
Nishanth Aravamudanda4cd8d2005-09-10 00:27:30 -07002125 schedule_timeout_interruptible(char_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 if (signal_pending(current))
2127 break;
2128 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2129 break;
2130 }
2131 set_current_state(TASK_RUNNING);
Alan Cox978e5952008-04-30 00:53:59 -07002132 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133
2134#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2135 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
2136#endif
2137}
2138
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139/*
2140 * This routine is called by tty_hangup() when a hangup is signaled.
2141 */
Jiri Slaby1c456072008-02-07 00:16:46 -08002142static void mxser_hangup(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143{
Jiri Slaby1c456072008-02-07 00:16:46 -08002144 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145
2146 mxser_flush_buffer(tty);
2147 mxser_shutdown(info);
Alan Cox0ad9e7d2008-07-16 21:56:10 +01002148 info->port.count = 0;
2149 info->port.flags &= ~ASYNC_NORMAL_ACTIVE;
2150 info->port.tty = NULL;
2151 wake_up_interruptible(&info->port.open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152}
2153
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154/*
2155 * mxser_rs_break() --- routine which turns the break handling on or off
2156 */
Alan Cox9e989662008-07-22 11:18:03 +01002157static int mxser_rs_break(struct tty_struct *tty, int break_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158{
Jiri Slaby1c456072008-02-07 00:16:46 -08002159 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 unsigned long flags;
2161
2162 spin_lock_irqsave(&info->slock, flags);
2163 if (break_state == -1)
Jiri Slaby1c456072008-02-07 00:16:46 -08002164 outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
2165 info->ioaddr + UART_LCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 else
Jiri Slaby1c456072008-02-07 00:16:46 -08002167 outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
2168 info->ioaddr + UART_LCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 spin_unlock_irqrestore(&info->slock, flags);
Alan Cox9e989662008-07-22 11:18:03 +01002170 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171}
2172
Jiri Slaby1c456072008-02-07 00:16:46 -08002173static void mxser_receive_chars(struct mxser_port *port, int *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174{
Alan Cox0ad9e7d2008-07-16 21:56:10 +01002175 struct tty_struct *tty = port->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 unsigned char ch, gdl;
2177 int ignored = 0;
2178 int cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 int recv_room;
2180 int max = 256;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181
Alan Cox33f0f882006-01-09 20:54:13 -08002182 recv_room = tty->receive_room;
Jiri Slaby1c456072008-02-07 00:16:46 -08002183 if ((recv_room == 0) && (!port->ldisc_stop_rx))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 mxser_stoprx(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185
Jiri Slaby1c456072008-02-07 00:16:46 -08002186 if (port->board->chip_flag != MOXA_OTHER_UART) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187
Jiri Slaby1c456072008-02-07 00:16:46 -08002188 if (*status & UART_LSR_SPECIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 goto intr_old;
Jiri Slaby1c456072008-02-07 00:16:46 -08002190 if (port->board->chip_flag == MOXA_MUST_MU860_HWID &&
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002191 (*status & MOXA_MUST_LSR_RERR))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 goto intr_old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 if (*status & MOXA_MUST_LSR_RERR)
2194 goto intr_old;
2195
Jiri Slaby1c456072008-02-07 00:16:46 -08002196 gdl = inb(port->ioaddr + MOXA_MUST_GDL_REGISTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197
Jiri Slaby1c456072008-02-07 00:16:46 -08002198 if (port->board->chip_flag == MOXA_MUST_MU150_HWID)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 gdl &= MOXA_MUST_GDL_MASK;
2200 if (gdl >= recv_room) {
Jiri Slaby1c456072008-02-07 00:16:46 -08002201 if (!port->ldisc_stop_rx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 mxser_stoprx(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 }
2204 while (gdl--) {
Jiri Slaby1c456072008-02-07 00:16:46 -08002205 ch = inb(port->ioaddr + UART_RX);
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002206 tty_insert_flip_char(tty, ch, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 }
2209 goto end_intr;
2210 }
Jiri Slaby1c456072008-02-07 00:16:46 -08002211intr_old:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212
2213 do {
2214 if (max-- < 0)
2215 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216
Jiri Slaby1c456072008-02-07 00:16:46 -08002217 ch = inb(port->ioaddr + UART_RX);
2218 if (port->board->chip_flag && (*status & UART_LSR_OE))
2219 outb(0x23, port->ioaddr + UART_FCR);
2220 *status &= port->read_status_mask;
2221 if (*status & port->ignore_status_mask) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 if (++ignored > 100)
2223 break;
2224 } else {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002225 char flag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 if (*status & UART_LSR_SPECIAL) {
2227 if (*status & UART_LSR_BI) {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002228 flag = TTY_BREAK;
Jiri Slaby1c456072008-02-07 00:16:46 -08002229 port->icount.brk++;
2230
Alan Cox0ad9e7d2008-07-16 21:56:10 +01002231 if (port->port.flags & ASYNC_SAK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 do_SAK(tty);
2233 } else if (*status & UART_LSR_PE) {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002234 flag = TTY_PARITY;
Jiri Slaby1c456072008-02-07 00:16:46 -08002235 port->icount.parity++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 } else if (*status & UART_LSR_FE) {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002237 flag = TTY_FRAME;
Jiri Slaby1c456072008-02-07 00:16:46 -08002238 port->icount.frame++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 } else if (*status & UART_LSR_OE) {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002240 flag = TTY_OVERRUN;
Jiri Slaby1c456072008-02-07 00:16:46 -08002241 port->icount.overrun++;
2242 } else
2243 flag = TTY_BREAK;
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002244 }
2245 tty_insert_flip_char(tty, ch, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 cnt++;
2247 if (cnt >= recv_room) {
Jiri Slaby1c456072008-02-07 00:16:46 -08002248 if (!port->ldisc_stop_rx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 mxser_stoprx(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 break;
2251 }
2252
2253 }
2254
Jiri Slaby1c456072008-02-07 00:16:46 -08002255 if (port->board->chip_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257
Jiri Slaby1c456072008-02-07 00:16:46 -08002258 *status = inb(port->ioaddr + UART_LSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 } while (*status & UART_LSR_DR);
2260
Jiri Slaby1c456072008-02-07 00:16:46 -08002261end_intr:
Alan Cox0ad9e7d2008-07-16 21:56:10 +01002262 mxvar_log.rxcnt[port->port.tty->index] += cnt;
Jiri Slaby1c456072008-02-07 00:16:46 -08002263 port->mon_data.rxcnt += cnt;
2264 port->mon_data.up_rxcnt += cnt;
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002265
Jiri Slaby1c456072008-02-07 00:16:46 -08002266 /*
2267 * We are called from an interrupt context with &port->slock
2268 * being held. Drop it temporarily in order to prevent
2269 * recursive locking.
2270 */
2271 spin_unlock(&port->slock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 tty_flip_buffer_push(tty);
Jiri Slaby1c456072008-02-07 00:16:46 -08002273 spin_lock(&port->slock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274}
2275
Jiri Slaby1c456072008-02-07 00:16:46 -08002276static void mxser_transmit_chars(struct mxser_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277{
2278 int count, cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279
Jiri Slaby1c456072008-02-07 00:16:46 -08002280 if (port->x_char) {
2281 outb(port->x_char, port->ioaddr + UART_TX);
2282 port->x_char = 0;
Alan Cox0ad9e7d2008-07-16 21:56:10 +01002283 mxvar_log.txcnt[port->port.tty->index]++;
Jiri Slaby1c456072008-02-07 00:16:46 -08002284 port->mon_data.txcnt++;
2285 port->mon_data.up_txcnt++;
2286 port->icount.tx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 return;
2288 }
2289
Alan Cox0ad9e7d2008-07-16 21:56:10 +01002290 if (port->port.xmit_buf == NULL)
Jiri Slaby1c456072008-02-07 00:16:46 -08002291 return;
2292
Alan Cox0ad9e7d2008-07-16 21:56:10 +01002293 if ((port->xmit_cnt <= 0) || port->port.tty->stopped ||
2294 (port->port.tty->hw_stopped &&
Jiri Slaby1c456072008-02-07 00:16:46 -08002295 (port->type != PORT_16550A) &&
2296 (!port->board->chip_flag))) {
2297 port->IER &= ~UART_IER_THRI;
2298 outb(port->IER, port->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 return;
2300 }
2301
Jiri Slaby1c456072008-02-07 00:16:46 -08002302 cnt = port->xmit_cnt;
2303 count = port->xmit_fifo_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 do {
Alan Cox0ad9e7d2008-07-16 21:56:10 +01002305 outb(port->port.xmit_buf[port->xmit_tail++],
Jiri Slaby1c456072008-02-07 00:16:46 -08002306 port->ioaddr + UART_TX);
2307 port->xmit_tail = port->xmit_tail & (SERIAL_XMIT_SIZE - 1);
2308 if (--port->xmit_cnt <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 break;
2310 } while (--count > 0);
Alan Cox0ad9e7d2008-07-16 21:56:10 +01002311 mxvar_log.txcnt[port->port.tty->index] += (cnt - port->xmit_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312
Jiri Slaby1c456072008-02-07 00:16:46 -08002313 port->mon_data.txcnt += (cnt - port->xmit_cnt);
2314 port->mon_data.up_txcnt += (cnt - port->xmit_cnt);
2315 port->icount.tx += (cnt - port->xmit_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316
Jiri Slaby1c456072008-02-07 00:16:46 -08002317 if (port->xmit_cnt < WAKEUP_CHARS)
Alan Cox0ad9e7d2008-07-16 21:56:10 +01002318 tty_wakeup(port->port.tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319
Jiri Slaby1c456072008-02-07 00:16:46 -08002320 if (port->xmit_cnt <= 0) {
2321 port->IER &= ~UART_IER_THRI;
2322 outb(port->IER, port->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324}
2325
2326/*
Jiri Slaby1c456072008-02-07 00:16:46 -08002327 * This is the serial driver's generic interrupt routine
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 */
Jiri Slaby1c456072008-02-07 00:16:46 -08002329static irqreturn_t mxser_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330{
Jiri Slaby1c456072008-02-07 00:16:46 -08002331 int status, iir, i;
2332 struct mxser_board *brd = NULL;
2333 struct mxser_port *port;
2334 int max, irqbits, bits, msr;
2335 unsigned int int_cnt, pass_counter = 0;
2336 int handled = IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337
Jiri Slaby1c456072008-02-07 00:16:46 -08002338 for (i = 0; i < MXSER_BOARDS; i++)
2339 if (dev_id == &mxser_boards[i]) {
2340 brd = dev_id;
2341 break;
2342 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343
Jiri Slaby1c456072008-02-07 00:16:46 -08002344 if (i == MXSER_BOARDS)
2345 goto irq_stop;
2346 if (brd == NULL)
2347 goto irq_stop;
2348 max = brd->info->nports;
2349 while (pass_counter++ < MXSER_ISR_PASS_LIMIT) {
2350 irqbits = inb(brd->vector) & brd->vector_mask;
2351 if (irqbits == brd->vector_mask)
2352 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353
Jiri Slaby1c456072008-02-07 00:16:46 -08002354 handled = IRQ_HANDLED;
2355 for (i = 0, bits = 1; i < max; i++, irqbits |= bits, bits <<= 1) {
2356 if (irqbits == brd->vector_mask)
2357 break;
2358 if (bits & irqbits)
2359 continue;
2360 port = &brd->ports[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361
Jiri Slaby1c456072008-02-07 00:16:46 -08002362 int_cnt = 0;
2363 spin_lock(&port->slock);
2364 do {
2365 iir = inb(port->ioaddr + UART_IIR);
2366 if (iir & UART_IIR_NO_INT)
2367 break;
2368 iir &= MOXA_MUST_IIR_MASK;
Alan Cox0ad9e7d2008-07-16 21:56:10 +01002369 if (!port->port.tty ||
2370 (port->port.flags & ASYNC_CLOSING) ||
2371 !(port->port.flags &
Jiri Slaby1c456072008-02-07 00:16:46 -08002372 ASYNC_INITIALIZED)) {
2373 status = inb(port->ioaddr + UART_LSR);
2374 outb(0x27, port->ioaddr + UART_FCR);
2375 inb(port->ioaddr + UART_MSR);
2376 break;
2377 }
2378
2379 status = inb(port->ioaddr + UART_LSR);
2380
2381 if (status & UART_LSR_PE)
2382 port->err_shadow |= NPPI_NOTIFY_PARITY;
2383 if (status & UART_LSR_FE)
2384 port->err_shadow |= NPPI_NOTIFY_FRAMING;
2385 if (status & UART_LSR_OE)
2386 port->err_shadow |=
2387 NPPI_NOTIFY_HW_OVERRUN;
2388 if (status & UART_LSR_BI)
2389 port->err_shadow |= NPPI_NOTIFY_BREAK;
2390
2391 if (port->board->chip_flag) {
2392 if (iir == MOXA_MUST_IIR_GDA ||
2393 iir == MOXA_MUST_IIR_RDA ||
2394 iir == MOXA_MUST_IIR_RTO ||
2395 iir == MOXA_MUST_IIR_LSR)
2396 mxser_receive_chars(port,
2397 &status);
2398
2399 } else {
2400 status &= port->read_status_mask;
2401 if (status & UART_LSR_DR)
2402 mxser_receive_chars(port,
2403 &status);
2404 }
2405 msr = inb(port->ioaddr + UART_MSR);
2406 if (msr & UART_MSR_ANY_DELTA)
2407 mxser_check_modem_status(port, msr);
2408
2409 if (port->board->chip_flag) {
2410 if (iir == 0x02 && (status &
2411 UART_LSR_THRE))
2412 mxser_transmit_chars(port);
2413 } else {
2414 if (status & UART_LSR_THRE)
2415 mxser_transmit_chars(port);
2416 }
2417 } while (int_cnt++ < MXSER_ISR_PASS_LIMIT);
2418 spin_unlock(&port->slock);
2419 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 }
2421
Jiri Slaby1c456072008-02-07 00:16:46 -08002422irq_stop:
2423 return handled;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424}
2425
Jiri Slaby1c456072008-02-07 00:16:46 -08002426static const struct tty_operations mxser_ops = {
2427 .open = mxser_open,
2428 .close = mxser_close,
2429 .write = mxser_write,
2430 .put_char = mxser_put_char,
2431 .flush_chars = mxser_flush_chars,
2432 .write_room = mxser_write_room,
2433 .chars_in_buffer = mxser_chars_in_buffer,
2434 .flush_buffer = mxser_flush_buffer,
2435 .ioctl = mxser_ioctl,
2436 .throttle = mxser_throttle,
2437 .unthrottle = mxser_unthrottle,
2438 .set_termios = mxser_set_termios,
2439 .stop = mxser_stop,
2440 .start = mxser_start,
2441 .hangup = mxser_hangup,
2442 .break_ctl = mxser_rs_break,
2443 .wait_until_sent = mxser_wait_until_sent,
2444 .tiocmget = mxser_tiocmget,
2445 .tiocmset = mxser_tiocmset,
2446};
2447
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448/*
Jiri Slaby1c456072008-02-07 00:16:46 -08002449 * The MOXA Smartio/Industio serial driver boot-time initialization code!
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 */
Jiri Slaby1c456072008-02-07 00:16:46 -08002451
2452static void mxser_release_res(struct mxser_board *brd, struct pci_dev *pdev,
2453 unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454{
Jiri Slaby1c456072008-02-07 00:16:46 -08002455 if (irq)
2456 free_irq(brd->irq, brd);
2457 if (pdev != NULL) { /* PCI */
2458#ifdef CONFIG_PCI
2459 pci_release_region(pdev, 2);
2460 pci_release_region(pdev, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 } else {
Jiri Slaby1c456072008-02-07 00:16:46 -08002463 release_region(brd->ports[0].ioaddr, 8 * brd->info->nports);
2464 release_region(brd->vector, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466}
2467
Jiri Slaby1c456072008-02-07 00:16:46 -08002468static int __devinit mxser_initbrd(struct mxser_board *brd,
2469 struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470{
Jiri Slaby1c456072008-02-07 00:16:46 -08002471 struct mxser_port *info;
2472 unsigned int i;
2473 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474
Jiri Slaby83766bc2008-07-25 01:48:21 -07002475 printk(KERN_INFO "mxser: max. baud rate = %d bps\n",
2476 brd->ports[0].max_baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477
Jiri Slaby1c456072008-02-07 00:16:46 -08002478 for (i = 0; i < brd->info->nports; i++) {
2479 info = &brd->ports[i];
Alan Cox44b7d1b2008-07-16 21:57:18 +01002480 tty_port_init(&info->port);
Jiri Slaby1c456072008-02-07 00:16:46 -08002481 info->board = brd;
2482 info->stop_rx = 0;
2483 info->ldisc_stop_rx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484
Jiri Slaby1c456072008-02-07 00:16:46 -08002485 /* Enhance mode enabled here */
2486 if (brd->chip_flag != MOXA_OTHER_UART)
Christoph Hellwig148ff862008-04-30 00:54:29 -07002487 mxser_enable_must_enchance_mode(info->ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488
Alan Cox0ad9e7d2008-07-16 21:56:10 +01002489 info->port.flags = ASYNC_SHARE_IRQ;
Jiri Slaby1c456072008-02-07 00:16:46 -08002490 info->type = brd->uart_type;
2491
2492 process_txrx_fifo(info);
2493
2494 info->custom_divisor = info->baud_base * 16;
Alan Cox44b7d1b2008-07-16 21:57:18 +01002495 info->port.close_delay = 5 * HZ / 10;
2496 info->port.closing_wait = 30 * HZ;
Jiri Slaby1c456072008-02-07 00:16:46 -08002497 info->normal_termios = mxvar_sdriver->init_termios;
Jiri Slaby1c456072008-02-07 00:16:46 -08002498 init_waitqueue_head(&info->delta_msr_wait);
2499 memset(&info->mon_data, 0, sizeof(struct mxser_mon));
2500 info->err_shadow = 0;
2501 spin_lock_init(&info->slock);
2502
2503 /* before set INT ISR, disable all int */
2504 outb(inb(info->ioaddr + UART_IER) & 0xf0,
2505 info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 }
2507
Jiri Slaby1c456072008-02-07 00:16:46 -08002508 retval = request_irq(brd->irq, mxser_interrupt, IRQF_SHARED, "mxser",
2509 brd);
2510 if (retval) {
2511 printk(KERN_ERR "Board %s: Request irq failed, IRQ (%d) may "
2512 "conflict with another device.\n",
2513 brd->info->name, brd->irq);
2514 /* We hold resources, we need to release them. */
2515 mxser_release_res(brd, pdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002517 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518}
2519
Jiri Slaby1c456072008-02-07 00:16:46 -08002520static int __init mxser_get_ISA_conf(int cap, struct mxser_board *brd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521{
2522 int id, i, bits;
2523 unsigned short regs[16], irq;
2524 unsigned char scratch, scratch2;
2525
Jiri Slaby1c456072008-02-07 00:16:46 -08002526 brd->chip_flag = MOXA_OTHER_UART;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527
2528 id = mxser_read_register(cap, regs);
Jiri Slaby1c456072008-02-07 00:16:46 -08002529 switch (id) {
2530 case C168_ASIC_ID:
2531 brd->info = &mxser_cards[0];
2532 break;
2533 case C104_ASIC_ID:
2534 brd->info = &mxser_cards[1];
2535 break;
2536 case CI104J_ASIC_ID:
2537 brd->info = &mxser_cards[2];
2538 break;
2539 case C102_ASIC_ID:
2540 brd->info = &mxser_cards[5];
2541 break;
2542 case CI132_ASIC_ID:
2543 brd->info = &mxser_cards[6];
2544 break;
2545 case CI134_ASIC_ID:
2546 brd->info = &mxser_cards[7];
2547 break;
2548 default:
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002549 return 0;
Jiri Slaby1c456072008-02-07 00:16:46 -08002550 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551
2552 irq = 0;
Jiri Slaby1c456072008-02-07 00:16:46 -08002553 /* some ISA cards have 2 ports, but we want to see them as 4-port (why?)
2554 Flag-hack checks if configuration should be read as 2-port here. */
2555 if (brd->info->nports == 2 || (brd->info->flags & MXSER_HAS2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 irq = regs[9] & 0xF000;
2557 irq = irq | (irq >> 4);
2558 if (irq != (regs[9] & 0xFF00))
Jiri Slaby83766bc2008-07-25 01:48:21 -07002559 goto err_irqconflict;
Jiri Slaby1c456072008-02-07 00:16:46 -08002560 } else if (brd->info->nports == 4) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 irq = regs[9] & 0xF000;
2562 irq = irq | (irq >> 4);
2563 irq = irq | (irq >> 8);
2564 if (irq != regs[9])
Jiri Slaby83766bc2008-07-25 01:48:21 -07002565 goto err_irqconflict;
Jiri Slaby1c456072008-02-07 00:16:46 -08002566 } else if (brd->info->nports == 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 irq = regs[9] & 0xF000;
2568 irq = irq | (irq >> 4);
2569 irq = irq | (irq >> 8);
2570 if ((irq != regs[9]) || (irq != regs[10]))
Jiri Slaby83766bc2008-07-25 01:48:21 -07002571 goto err_irqconflict;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 }
2573
Jiri Slaby83766bc2008-07-25 01:48:21 -07002574 if (!irq) {
2575 printk(KERN_ERR "mxser: interrupt number unset\n");
2576 return -EIO;
2577 }
Jiri Slaby1c456072008-02-07 00:16:46 -08002578 brd->irq = ((int)(irq & 0xF000) >> 12);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 for (i = 0; i < 8; i++)
Jiri Slaby1c456072008-02-07 00:16:46 -08002580 brd->ports[i].ioaddr = (int) regs[i + 1] & 0xFFF8;
Jiri Slaby83766bc2008-07-25 01:48:21 -07002581 if ((regs[12] & 0x80) == 0) {
2582 printk(KERN_ERR "mxser: invalid interrupt vector\n");
2583 return -EIO;
2584 }
Jiri Slaby1c456072008-02-07 00:16:46 -08002585 brd->vector = (int)regs[11]; /* interrupt vector */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 if (id == 1)
Jiri Slaby1c456072008-02-07 00:16:46 -08002587 brd->vector_mask = 0x00FF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 else
Jiri Slaby1c456072008-02-07 00:16:46 -08002589 brd->vector_mask = 0x000F;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 for (i = 7, bits = 0x0100; i >= 0; i--, bits <<= 1) {
2591 if (regs[12] & bits) {
Jiri Slaby1c456072008-02-07 00:16:46 -08002592 brd->ports[i].baud_base = 921600;
2593 brd->ports[i].max_baud = 921600;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 } else {
Jiri Slaby1c456072008-02-07 00:16:46 -08002595 brd->ports[i].baud_base = 115200;
2596 brd->ports[i].max_baud = 115200;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 }
2598 }
2599 scratch2 = inb(cap + UART_LCR) & (~UART_LCR_DLAB);
2600 outb(scratch2 | UART_LCR_DLAB, cap + UART_LCR);
2601 outb(0, cap + UART_EFR); /* EFR is the same as FCR */
2602 outb(scratch2, cap + UART_LCR);
2603 outb(UART_FCR_ENABLE_FIFO, cap + UART_FCR);
2604 scratch = inb(cap + UART_IIR);
2605
2606 if (scratch & 0xC0)
Jiri Slaby1c456072008-02-07 00:16:46 -08002607 brd->uart_type = PORT_16550A;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 else
Jiri Slaby1c456072008-02-07 00:16:46 -08002609 brd->uart_type = PORT_16450;
2610 if (!request_region(brd->ports[0].ioaddr, 8 * brd->info->nports,
Jiri Slaby83766bc2008-07-25 01:48:21 -07002611 "mxser(IO)")) {
2612 printk(KERN_ERR "mxser: can't request ports I/O region: "
2613 "0x%.8lx-0x%.8lx\n",
2614 brd->ports[0].ioaddr, brd->ports[0].ioaddr +
2615 8 * brd->info->nports - 1);
2616 return -EIO;
2617 }
Jiri Slaby1c456072008-02-07 00:16:46 -08002618 if (!request_region(brd->vector, 1, "mxser(vector)")) {
2619 release_region(brd->ports[0].ioaddr, 8 * brd->info->nports);
Jiri Slaby83766bc2008-07-25 01:48:21 -07002620 printk(KERN_ERR "mxser: can't request interrupt vector region: "
2621 "0x%.8lx-0x%.8lx\n",
2622 brd->ports[0].ioaddr, brd->ports[0].ioaddr +
2623 8 * brd->info->nports - 1);
2624 return -EIO;
Jiri Slaby1c456072008-02-07 00:16:46 -08002625 }
2626 return brd->info->nports;
Jiri Slaby83766bc2008-07-25 01:48:21 -07002627
2628err_irqconflict:
2629 printk(KERN_ERR "mxser: invalid interrupt number\n");
2630 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631}
2632
Jiri Slaby1c456072008-02-07 00:16:46 -08002633static int __devinit mxser_probe(struct pci_dev *pdev,
2634 const struct pci_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635{
Jiri Slaby1c456072008-02-07 00:16:46 -08002636#ifdef CONFIG_PCI
2637 struct mxser_board *brd;
2638 unsigned int i, j;
2639 unsigned long ioaddress;
2640 int retval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641
Jiri Slaby1c456072008-02-07 00:16:46 -08002642 for (i = 0; i < MXSER_BOARDS; i++)
2643 if (mxser_boards[i].info == NULL)
2644 break;
2645
2646 if (i >= MXSER_BOARDS) {
Jiri Slaby83766bc2008-07-25 01:48:21 -07002647 dev_err(&pdev->dev, "too many boards found (maximum %d), board "
2648 "not configured\n", MXSER_BOARDS);
Jiri Slaby1c456072008-02-07 00:16:46 -08002649 goto err;
2650 }
2651
2652 brd = &mxser_boards[i];
2653 brd->idx = i * MXSER_PORTS_PER_BOARD;
Jiri Slaby83766bc2008-07-25 01:48:21 -07002654 dev_info(&pdev->dev, "found MOXA %s board (BusNo=%d, DevNo=%d)\n",
Jiri Slaby1c456072008-02-07 00:16:46 -08002655 mxser_cards[ent->driver_data].name,
2656 pdev->bus->number, PCI_SLOT(pdev->devfn));
2657
2658 retval = pci_enable_device(pdev);
2659 if (retval) {
Jiri Slaby83766bc2008-07-25 01:48:21 -07002660 dev_err(&pdev->dev, "PCI enable failed\n");
Jiri Slaby1c456072008-02-07 00:16:46 -08002661 goto err;
2662 }
2663
2664 /* io address */
2665 ioaddress = pci_resource_start(pdev, 2);
2666 retval = pci_request_region(pdev, 2, "mxser(IO)");
2667 if (retval)
2668 goto err;
2669
2670 brd->info = &mxser_cards[ent->driver_data];
2671 for (i = 0; i < brd->info->nports; i++)
2672 brd->ports[i].ioaddr = ioaddress + 8 * i;
2673
2674 /* vector */
2675 ioaddress = pci_resource_start(pdev, 3);
2676 retval = pci_request_region(pdev, 3, "mxser(vector)");
2677 if (retval)
2678 goto err_relio;
2679 brd->vector = ioaddress;
2680
2681 /* irq */
2682 brd->irq = pdev->irq;
2683
2684 brd->chip_flag = CheckIsMoxaMust(brd->ports[0].ioaddr);
2685 brd->uart_type = PORT_16550A;
2686 brd->vector_mask = 0;
2687
2688 for (i = 0; i < brd->info->nports; i++) {
2689 for (j = 0; j < UART_INFO_NUM; j++) {
2690 if (Gpci_uart_info[j].type == brd->chip_flag) {
2691 brd->ports[i].max_baud =
2692 Gpci_uart_info[j].max_baud;
2693
2694 /* exception....CP-102 */
2695 if (brd->info->flags & MXSER_HIGHBAUD)
2696 brd->ports[i].max_baud = 921600;
2697 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 }
2699 }
Jiri Slaby1c456072008-02-07 00:16:46 -08002700 }
2701
2702 if (brd->chip_flag == MOXA_MUST_MU860_HWID) {
2703 for (i = 0; i < brd->info->nports; i++) {
2704 if (i < 4)
2705 brd->ports[i].opmode_ioaddr = ioaddress + 4;
2706 else
2707 brd->ports[i].opmode_ioaddr = ioaddress + 0x0c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 }
Jiri Slaby1c456072008-02-07 00:16:46 -08002709 outb(0, ioaddress + 4); /* default set to RS232 mode */
2710 outb(0, ioaddress + 0x0c); /* default set to RS232 mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 }
Jiri Slaby1c456072008-02-07 00:16:46 -08002712
2713 for (i = 0; i < brd->info->nports; i++) {
2714 brd->vector_mask |= (1 << i);
2715 brd->ports[i].baud_base = 921600;
2716 }
2717
2718 /* mxser_initbrd will hook ISR. */
2719 retval = mxser_initbrd(brd, pdev);
2720 if (retval)
2721 goto err_null;
2722
2723 for (i = 0; i < brd->info->nports; i++)
2724 tty_register_device(mxvar_sdriver, brd->idx + i, &pdev->dev);
2725
2726 pci_set_drvdata(pdev, brd);
2727
2728 return 0;
2729err_relio:
2730 pci_release_region(pdev, 2);
2731err_null:
2732 brd->info = NULL;
2733err:
2734 return retval;
2735#else
2736 return -ENODEV;
2737#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738}
2739
Jiri Slaby1c456072008-02-07 00:16:46 -08002740static void __devexit mxser_remove(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741{
Jiri Slaby1c456072008-02-07 00:16:46 -08002742 struct mxser_board *brd = pci_get_drvdata(pdev);
2743 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744
Jiri Slaby1c456072008-02-07 00:16:46 -08002745 for (i = 0; i < brd->info->nports; i++)
2746 tty_unregister_device(mxvar_sdriver, brd->idx + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747
Jiri Slaby1c456072008-02-07 00:16:46 -08002748 mxser_release_res(brd, pdev, 1);
2749 brd->info = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750}
2751
Jiri Slaby1c456072008-02-07 00:16:46 -08002752static struct pci_driver mxser_driver = {
2753 .name = "mxser",
2754 .id_table = mxser_pcibrds,
2755 .probe = mxser_probe,
2756 .remove = __devexit_p(mxser_remove)
2757};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758
Jiri Slaby1c456072008-02-07 00:16:46 -08002759static int __init mxser_module_init(void)
2760{
2761 struct mxser_board *brd;
Jiri Slaby1df00922008-07-25 01:48:22 -07002762 unsigned int b, i, m;
2763 int retval;
Jiri Slaby1c456072008-02-07 00:16:46 -08002764
2765 pr_debug("Loading module mxser ...\n");
2766
2767 mxvar_sdriver = alloc_tty_driver(MXSER_PORTS + 1);
2768 if (!mxvar_sdriver)
2769 return -ENOMEM;
2770
2771 printk(KERN_INFO "MOXA Smartio/Industio family driver version %s\n",
2772 MXSER_VERSION);
2773
2774 /* Initialize the tty_driver structure */
2775 mxvar_sdriver->owner = THIS_MODULE;
2776 mxvar_sdriver->magic = TTY_DRIVER_MAGIC;
2777 mxvar_sdriver->name = "ttyMI";
2778 mxvar_sdriver->major = ttymajor;
2779 mxvar_sdriver->minor_start = 0;
2780 mxvar_sdriver->num = MXSER_PORTS + 1;
2781 mxvar_sdriver->type = TTY_DRIVER_TYPE_SERIAL;
2782 mxvar_sdriver->subtype = SERIAL_TYPE_NORMAL;
2783 mxvar_sdriver->init_termios = tty_std_termios;
2784 mxvar_sdriver->init_termios.c_cflag = B9600|CS8|CREAD|HUPCL|CLOCAL;
2785 mxvar_sdriver->flags = TTY_DRIVER_REAL_RAW|TTY_DRIVER_DYNAMIC_DEV;
2786 tty_set_operations(mxvar_sdriver, &mxser_ops);
2787
2788 retval = tty_register_driver(mxvar_sdriver);
2789 if (retval) {
2790 printk(KERN_ERR "Couldn't install MOXA Smartio/Industio family "
2791 "tty driver !\n");
2792 goto err_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 }
Jiri Slaby1c456072008-02-07 00:16:46 -08002794
Jiri Slaby1c456072008-02-07 00:16:46 -08002795 /* Start finding ISA boards here */
Jiri Slaby1df00922008-07-25 01:48:22 -07002796 for (m = 0, b = 0; b < MXSER_BOARDS; b++) {
2797 if (!ioaddr[b])
2798 continue;
Jiri Slaby1c456072008-02-07 00:16:46 -08002799
Jiri Slaby1df00922008-07-25 01:48:22 -07002800 brd = &mxser_boards[m];
2801 retval = mxser_get_ISA_conf(!ioaddr[b], brd);
2802 if (retval <= 0) {
2803 brd->info = NULL;
2804 continue;
Jiri Slaby1c456072008-02-07 00:16:46 -08002805 }
2806
Jiri Slaby1df00922008-07-25 01:48:22 -07002807 printk(KERN_INFO "mxser: found MOXA %s board (CAP=0x%lx)\n",
2808 brd->info->name, ioaddr[b]);
2809
2810 /* mxser_initbrd will hook ISR. */
2811 if (mxser_initbrd(brd, NULL) < 0) {
2812 brd->info = NULL;
2813 continue;
2814 }
2815
2816 brd->idx = m * MXSER_PORTS_PER_BOARD;
2817 for (i = 0; i < brd->info->nports; i++)
2818 tty_register_device(mxvar_sdriver, brd->idx + i, NULL);
2819
2820 m++;
2821 }
2822
Jiri Slaby1c456072008-02-07 00:16:46 -08002823 retval = pci_register_driver(&mxser_driver);
2824 if (retval) {
Jiri Slaby83766bc2008-07-25 01:48:21 -07002825 printk(KERN_ERR "mxser: can't register pci driver\n");
Jiri Slaby1c456072008-02-07 00:16:46 -08002826 if (!m) {
2827 retval = -ENODEV;
2828 goto err_unr;
2829 } /* else: we have some ISA cards under control */
2830 }
2831
2832 pr_debug("Done.\n");
2833
2834 return 0;
2835err_unr:
2836 tty_unregister_driver(mxvar_sdriver);
2837err_put:
2838 put_tty_driver(mxvar_sdriver);
2839 return retval;
2840}
2841
2842static void __exit mxser_module_exit(void)
2843{
2844 unsigned int i, j;
2845
2846 pr_debug("Unloading module mxser ...\n");
2847
2848 pci_unregister_driver(&mxser_driver);
2849
2850 for (i = 0; i < MXSER_BOARDS; i++) /* ISA remains */
2851 if (mxser_boards[i].info != NULL)
2852 for (j = 0; j < mxser_boards[i].info->nports; j++)
2853 tty_unregister_device(mxvar_sdriver,
2854 mxser_boards[i].idx + j);
2855 tty_unregister_driver(mxvar_sdriver);
2856 put_tty_driver(mxvar_sdriver);
2857
2858 for (i = 0; i < MXSER_BOARDS; i++)
2859 if (mxser_boards[i].info != NULL)
2860 mxser_release_res(&mxser_boards[i], NULL, 1);
2861
2862 pr_debug("Done.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863}
2864
2865module_init(mxser_module_init);
2866module_exit(mxser_module_exit);