blob: e83ccee03161fd0e83fe7ce4d1f3e4f6e5cac6b2 [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
19 * - Fixed sleep with spinlock held in mxser_send_break
20 */
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/errno.h>
24#include <linux/signal.h>
25#include <linux/sched.h>
26#include <linux/timer.h>
27#include <linux/interrupt.h>
28#include <linux/tty.h>
29#include <linux/tty_flip.h>
30#include <linux/serial.h>
31#include <linux/serial_reg.h>
32#include <linux/major.h>
33#include <linux/string.h>
34#include <linux/fcntl.h>
35#include <linux/ptrace.h>
36#include <linux/gfp.h>
37#include <linux/ioport.h>
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/pci.h>
Jiri Slaby1977f032007-10-18 23:40:25 -070041#include <linux/bitops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#include <asm/system.h>
44#include <asm/io.h>
45#include <asm/irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <asm/uaccess.h>
47
48#include "mxser.h"
49
Jiri Slaby80ff8a82008-02-07 00:16:51 -080050#define MXSER_VERSION "2.0.3" /* 1.11 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#define MXSERMAJOR 174
52#define MXSERCUMAJOR 175
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#define MXSER_BOARDS 4 /* Max. boards */
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#define MXSER_PORTS_PER_BOARD 8 /* Max. ports per board */
Jiri Slaby1c456072008-02-07 00:16:46 -080056#define MXSER_PORTS (MXSER_BOARDS * MXSER_PORTS_PER_BOARD)
57#define MXSER_ISR_PASS_LIMIT 100
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59#define MXSER_ERR_IOADDR -1
60#define MXSER_ERR_IRQ -2
61#define MXSER_ERR_IRQ_CONFLIT -3
62#define MXSER_ERR_VECTOR -4
63
Jiri Slaby1c456072008-02-07 00:16:46 -080064/*CheckIsMoxaMust return value*/
65#define MOXA_OTHER_UART 0x00
66#define MOXA_MUST_MU150_HWID 0x01
67#define MOXA_MUST_MU860_HWID 0x02
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#define WAKEUP_CHARS 256
70
71#define UART_MCR_AFE 0x20
72#define UART_LSR_SPECIAL 0x1E
73
Jiri Slaby1c456072008-02-07 00:16:46 -080074#define PCI_DEVICE_ID_CB108 0x1080
75#define PCI_DEVICE_ID_CB114 0x1142
Jiri Slaby80ff8a82008-02-07 00:16:51 -080076#define PCI_DEVICE_ID_CP114UL 0x1143
Jiri Slaby1c456072008-02-07 00:16:46 -080077#define PCI_DEVICE_ID_CB134I 0x1341
78#define PCI_DEVICE_ID_CP138U 0x1380
79#define PCI_DEVICE_ID_POS104UL 0x1044
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82#define C168_ASIC_ID 1
83#define C104_ASIC_ID 2
84#define C102_ASIC_ID 0xB
85#define CI132_ASIC_ID 4
86#define CI134_ASIC_ID 3
87#define CI104J_ASIC_ID 5
88
Jiri Slaby1c456072008-02-07 00:16:46 -080089#define MXSER_HIGHBAUD 1
90#define MXSER_HAS2 2
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -070092/* This is only for PCI */
Jiri Slaby1c456072008-02-07 00:16:46 -080093static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 int type;
95 int tx_fifo;
96 int rx_fifo;
97 int xmit_fifo_size;
98 int rx_high_water;
99 int rx_trigger;
100 int rx_low_water;
101 long max_baud;
Jiri Slaby1c456072008-02-07 00:16:46 -0800102} Gpci_uart_info[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 {MOXA_OTHER_UART, 16, 16, 16, 14, 14, 1, 921600L},
104 {MOXA_MUST_MU150_HWID, 64, 64, 64, 48, 48, 16, 230400L},
105 {MOXA_MUST_MU860_HWID, 128, 128, 128, 96, 96, 32, 921600L}
106};
Jiri Slaby1c456072008-02-07 00:16:46 -0800107#define UART_INFO_NUM ARRAY_SIZE(Gpci_uart_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Jiri Slaby1c456072008-02-07 00:16:46 -0800109struct mxser_cardinfo {
110 char *name;
111 unsigned int nports;
112 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113};
114
Jiri Slaby1c456072008-02-07 00:16:46 -0800115static const struct mxser_cardinfo mxser_cards[] = {
116/* 0*/ { "C168 series", 8, },
117 { "C104 series", 4, },
118 { "CI-104J series", 4, },
119 { "C168H/PCI series", 8, },
120 { "C104H/PCI series", 4, },
121/* 5*/ { "C102 series", 4, MXSER_HAS2 }, /* C102-ISA */
122 { "CI-132 series", 4, MXSER_HAS2 },
123 { "CI-134 series", 4, },
124 { "CP-132 series", 2, },
125 { "CP-114 series", 4, },
126/*10*/ { "CT-114 series", 4, },
127 { "CP-102 series", 2, MXSER_HIGHBAUD },
128 { "CP-104U series", 4, },
129 { "CP-168U series", 8, },
130 { "CP-132U series", 2, },
131/*15*/ { "CP-134U series", 4, },
132 { "CP-104JU series", 4, },
133 { "Moxa UC7000 Serial", 8, }, /* RC7000 */
134 { "CP-118U series", 8, },
135 { "CP-102UL series", 2, },
136/*20*/ { "CP-102U series", 2, },
137 { "CP-118EL series", 8, },
138 { "CP-168EL series", 8, },
139 { "CP-104EL series", 4, },
140 { "CB-108 series", 8, },
141/*25*/ { "CB-114 series", 4, },
142 { "CB-134I series", 4, },
143 { "CP-138U series", 8, },
Jiri Slaby80ff8a82008-02-07 00:16:51 -0800144 { "POS-104UL series", 4, },
145 { "CP-114UL series", 4, }
Jiri Slaby1c456072008-02-07 00:16:46 -0800146};
147
148/* driver_data correspond to the lines in the structure above
149 see also ISA probe function before you change something */
150static struct pci_device_id mxser_pcibrds[] = {
151 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_C168), .driver_data = 3 },
152 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_C104), .driver_data = 4 },
153 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP132), .driver_data = 8 },
154 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP114), .driver_data = 9 },
155 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CT114), .driver_data = 10 },
156 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102), .driver_data = 11 },
157 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104U), .driver_data = 12 },
158 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP168U), .driver_data = 13 },
159 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP132U), .driver_data = 14 },
160 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP134U), .driver_data = 15 },
161 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104JU),.driver_data = 16 },
162 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_RC7000), .driver_data = 17 },
163 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP118U), .driver_data = 18 },
164 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102UL),.driver_data = 19 },
165 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102U), .driver_data = 20 },
166 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP118EL),.driver_data = 21 },
167 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP168EL),.driver_data = 22 },
168 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104EL),.driver_data = 23 },
169 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB108), .driver_data = 24 },
170 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB114), .driver_data = 25 },
171 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB134I), .driver_data = 26 },
172 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP138U), .driver_data = 27 },
173 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_POS104UL), .driver_data = 28 },
Jiri Slaby80ff8a82008-02-07 00:16:51 -0800174 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP114UL), .driver_data = 29 },
Jiri Slaby1c456072008-02-07 00:16:46 -0800175 { }
176};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177MODULE_DEVICE_TABLE(pci, mxser_pcibrds);
178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179static int ioaddr[MXSER_BOARDS] = { 0, 0, 0, 0 };
180static int ttymajor = MXSERMAJOR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182/* Variables for insmod */
183
184MODULE_AUTHOR("Casper Yang");
185MODULE_DESCRIPTION("MOXA Smartio/Industio Family Multiport Board Device Driver");
Rusty Russell8d3b33f2006-03-25 03:07:05 -0800186module_param_array(ioaddr, int, NULL, 0);
187module_param(ttymajor, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188MODULE_LICENSE("GPL");
189
190struct mxser_log {
191 int tick;
192 unsigned long rxcnt[MXSER_PORTS];
193 unsigned long txcnt[MXSER_PORTS];
194};
195
196
197struct mxser_mon {
198 unsigned long rxcnt;
199 unsigned long txcnt;
200 unsigned long up_rxcnt;
201 unsigned long up_txcnt;
202 int modem_status;
203 unsigned char hold_reason;
204};
205
206struct mxser_mon_ext {
207 unsigned long rx_cnt[32];
208 unsigned long tx_cnt[32];
209 unsigned long up_rxcnt[32];
210 unsigned long up_txcnt[32];
211 int modem_status[32];
212
213 long baudrate[32];
214 int databits[32];
215 int stopbits[32];
216 int parity[32];
217 int flowctrl[32];
218 int fifo[32];
219 int iftype[32];
220};
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700221
Jiri Slaby1c456072008-02-07 00:16:46 -0800222struct mxser_board;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Jiri Slaby1c456072008-02-07 00:16:46 -0800224struct mxser_port {
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100225 struct tty_port port;
Jiri Slaby1c456072008-02-07 00:16:46 -0800226 struct mxser_board *board;
Jiri Slaby1c456072008-02-07 00:16:46 -0800227
228 unsigned long ioaddr;
229 unsigned long opmode_ioaddr;
230 int max_baud;
231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 int rx_high_water;
233 int rx_trigger; /* Rx fifo trigger level */
234 int rx_low_water;
235 int baud_base; /* max. speed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 int type; /* UART type */
Jiri Slaby1c456072008-02-07 00:16:46 -0800237
238 int x_char; /* xon/xoff character */
239 int IER; /* Interrupt Enable Register */
240 int MCR; /* Modem control register */
241
242 unsigned char stop_rx;
243 unsigned char ldisc_stop_rx;
244
245 int custom_divisor;
246 int close_delay;
247 unsigned short closing_wait;
248 unsigned char err_shadow;
249 unsigned long event;
250
Jiri Slaby1c456072008-02-07 00:16:46 -0800251 struct async_icount icount; /* kernel counters for 4 input interrupts */
252 int timeout;
253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 int read_status_mask;
255 int ignore_status_mask;
256 int xmit_fifo_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 int xmit_head;
258 int xmit_tail;
259 int xmit_cnt;
Jiri Slaby1c456072008-02-07 00:16:46 -0800260
Alan Cox606d0992006-12-08 02:38:45 -0800261 struct ktermios normal_termios;
Jiri Slaby1c456072008-02-07 00:16:46 -0800262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 struct mxser_mon mon_data;
Jiri Slaby1c456072008-02-07 00:16:46 -0800264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 spinlock_t slock;
Jiri Slaby1c456072008-02-07 00:16:46 -0800266 wait_queue_head_t delta_msr_wait;
267};
268
269struct mxser_board {
270 unsigned int idx;
271 int irq;
272 const struct mxser_cardinfo *info;
273 unsigned long vector;
274 unsigned long vector_mask;
275
276 int chip_flag;
277 int uart_type;
278
279 struct mxser_port ports[MXSER_PORTS_PER_BOARD];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280};
281
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282struct mxser_mstatus {
283 tcflag_t cflag;
284 int cts;
285 int dsr;
286 int ri;
287 int dcd;
288};
289
290static struct mxser_mstatus GMStatus[MXSER_PORTS];
291
292static int mxserBoardCAP[MXSER_BOARDS] = {
293 0, 0, 0, 0
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700294 /* 0x180, 0x280, 0x200, 0x320 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295};
296
Jiri Slaby1c456072008-02-07 00:16:46 -0800297static struct mxser_board mxser_boards[MXSER_BOARDS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298static struct tty_driver *mxvar_sdriver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299static struct mxser_log mxvar_log;
300static int mxvar_diagflag;
301static unsigned char mxser_msr[MXSER_PORTS + 1];
302static struct mxser_mon_ext mon_data_ext;
303static int mxser_set_baud_method[MXSER_PORTS + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Christoph Hellwig148ff862008-04-30 00:54:29 -0700305static void mxser_enable_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_disable_must_enchance_mode(unsigned long baseio)
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_EFRB_ENABLE;
330
331 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
332 outb(oldlcr, baseio + UART_LCR);
333}
334
335static void mxser_set_must_xon1_value(unsigned long baseio, u8 value)
336{
337 u8 oldlcr;
338 u8 efr;
339
340 oldlcr = inb(baseio + UART_LCR);
341 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
342
343 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
344 efr &= ~MOXA_MUST_EFR_BANK_MASK;
345 efr |= MOXA_MUST_EFR_BANK0;
346
347 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
348 outb(value, baseio + MOXA_MUST_XON1_REGISTER);
349 outb(oldlcr, baseio + UART_LCR);
350}
351
352static void mxser_set_must_xoff1_value(unsigned long baseio, u8 value)
353{
354 u8 oldlcr;
355 u8 efr;
356
357 oldlcr = inb(baseio + UART_LCR);
358 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
359
360 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
361 efr &= ~MOXA_MUST_EFR_BANK_MASK;
362 efr |= MOXA_MUST_EFR_BANK0;
363
364 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
365 outb(value, baseio + MOXA_MUST_XOFF1_REGISTER);
366 outb(oldlcr, baseio + UART_LCR);
367}
368
369static void mxser_set_must_fifo_value(struct mxser_port *info)
370{
371 u8 oldlcr;
372 u8 efr;
373
374 oldlcr = inb(info->ioaddr + UART_LCR);
375 outb(MOXA_MUST_ENTER_ENCHANCE, info->ioaddr + UART_LCR);
376
377 efr = inb(info->ioaddr + MOXA_MUST_EFR_REGISTER);
378 efr &= ~MOXA_MUST_EFR_BANK_MASK;
379 efr |= MOXA_MUST_EFR_BANK1;
380
381 outb(efr, info->ioaddr + MOXA_MUST_EFR_REGISTER);
382 outb((u8)info->rx_high_water, info->ioaddr + MOXA_MUST_RBRTH_REGISTER);
383 outb((u8)info->rx_trigger, info->ioaddr + MOXA_MUST_RBRTI_REGISTER);
384 outb((u8)info->rx_low_water, info->ioaddr + MOXA_MUST_RBRTL_REGISTER);
385 outb(oldlcr, info->ioaddr + UART_LCR);
386}
387
388static void mxser_set_must_enum_value(unsigned long baseio, u8 value)
389{
390 u8 oldlcr;
391 u8 efr;
392
393 oldlcr = inb(baseio + UART_LCR);
394 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
395
396 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
397 efr &= ~MOXA_MUST_EFR_BANK_MASK;
398 efr |= MOXA_MUST_EFR_BANK2;
399
400 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
401 outb(value, baseio + MOXA_MUST_ENUM_REGISTER);
402 outb(oldlcr, baseio + UART_LCR);
403}
404
405static void mxser_get_must_hardware_id(unsigned long baseio, u8 *pId)
406{
407 u8 oldlcr;
408 u8 efr;
409
410 oldlcr = inb(baseio + UART_LCR);
411 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
412
413 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
414 efr &= ~MOXA_MUST_EFR_BANK_MASK;
415 efr |= MOXA_MUST_EFR_BANK2;
416
417 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
418 *pId = inb(baseio + MOXA_MUST_HWID_REGISTER);
419 outb(oldlcr, baseio + UART_LCR);
420}
421
422static void SET_MOXA_MUST_NO_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_MASK;
432
433 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
434 outb(oldlcr, baseio + UART_LCR);
435}
436
437static void mxser_enable_must_tx_software_flow_control(unsigned long baseio)
438{
439 u8 oldlcr;
440 u8 efr;
441
442 oldlcr = inb(baseio + UART_LCR);
443 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
444
445 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
446 efr &= ~MOXA_MUST_EFR_SF_TX_MASK;
447 efr |= MOXA_MUST_EFR_SF_TX1;
448
449 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
450 outb(oldlcr, baseio + UART_LCR);
451}
452
453static void mxser_disable_must_tx_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_TX_MASK;
463
464 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
465 outb(oldlcr, baseio + UART_LCR);
466}
467
468static void mxser_enable_must_rx_software_flow_control(unsigned long baseio)
469{
470 u8 oldlcr;
471 u8 efr;
472
473 oldlcr = inb(baseio + UART_LCR);
474 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
475
476 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
477 efr &= ~MOXA_MUST_EFR_SF_RX_MASK;
478 efr |= MOXA_MUST_EFR_SF_RX1;
479
480 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
481 outb(oldlcr, baseio + UART_LCR);
482}
483
484static void mxser_disable_must_rx_software_flow_control(unsigned long baseio)
485{
486 u8 oldlcr;
487 u8 efr;
488
489 oldlcr = inb(baseio + UART_LCR);
490 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
491
492 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
493 efr &= ~MOXA_MUST_EFR_SF_RX_MASK;
494
495 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
496 outb(oldlcr, baseio + UART_LCR);
497}
498
Jesper Juhlb8cc5542007-10-18 03:06:03 -0700499#ifdef CONFIG_PCI
Jiri Slaby1c456072008-02-07 00:16:46 -0800500static int __devinit CheckIsMoxaMust(unsigned long io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
502 u8 oldmcr, hwid;
503 int i;
504
505 outb(0, io + UART_LCR);
Christoph Hellwig148ff862008-04-30 00:54:29 -0700506 mxser_disable_must_enchance_mode(io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 oldmcr = inb(io + UART_MCR);
508 outb(0, io + UART_MCR);
Christoph Hellwig148ff862008-04-30 00:54:29 -0700509 mxser_set_must_xon1_value(io, 0x11);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 if ((hwid = inb(io + UART_MCR)) != 0) {
511 outb(oldmcr, io + UART_MCR);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700512 return MOXA_OTHER_UART;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 }
514
Christoph Hellwig148ff862008-04-30 00:54:29 -0700515 mxser_get_must_hardware_id(io, &hwid);
Jiri Slaby1c456072008-02-07 00:16:46 -0800516 for (i = 1; i < UART_INFO_NUM; i++) { /* 0 = OTHER_UART */
517 if (hwid == Gpci_uart_info[i].type)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700518 return (int)hwid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 }
520 return MOXA_OTHER_UART;
521}
Jesper Juhlb8cc5542007-10-18 03:06:03 -0700522#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
Jiri Slaby1c456072008-02-07 00:16:46 -0800524static void process_txrx_fifo(struct mxser_port *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525{
526 int i;
527
528 if ((info->type == PORT_16450) || (info->type == PORT_8250)) {
529 info->rx_trigger = 1;
530 info->rx_high_water = 1;
531 info->rx_low_water = 1;
532 info->xmit_fifo_size = 1;
Jiri Slaby1c456072008-02-07 00:16:46 -0800533 } else
534 for (i = 0; i < UART_INFO_NUM; i++)
535 if (info->board->chip_flag == Gpci_uart_info[i].type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 info->rx_trigger = Gpci_uart_info[i].rx_trigger;
537 info->rx_low_water = Gpci_uart_info[i].rx_low_water;
538 info->rx_high_water = Gpci_uart_info[i].rx_high_water;
539 info->xmit_fifo_size = Gpci_uart_info[i].xmit_fifo_size;
540 break;
541 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542}
543
Jiri Slaby1c456072008-02-07 00:16:46 -0800544static unsigned char mxser_get_msr(int baseaddr, int mode, int port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
546 unsigned char status = 0;
547
548 status = inb(baseaddr + UART_MSR);
549
550 mxser_msr[port] &= 0x0F;
551 mxser_msr[port] |= status;
552 status = mxser_msr[port];
553 if (mode)
554 mxser_msr[port] = 0;
555
556 return status;
557}
558
Jiri Slaby1c456072008-02-07 00:16:46 -0800559static int mxser_block_til_ready(struct tty_struct *tty, struct file *filp,
560 struct mxser_port *port)
561{
562 DECLARE_WAITQUEUE(wait, current);
563 int retval;
564 int do_clocal = 0;
565 unsigned long flags;
566
567 /*
568 * If non-blocking mode is set, or the port is not enabled,
569 * then make the check up front and then exit.
570 */
571 if ((filp->f_flags & O_NONBLOCK) ||
572 test_bit(TTY_IO_ERROR, &tty->flags)) {
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100573 port->port.flags |= ASYNC_NORMAL_ACTIVE;
Jiri Slaby1c456072008-02-07 00:16:46 -0800574 return 0;
575 }
576
577 if (tty->termios->c_cflag & CLOCAL)
578 do_clocal = 1;
579
580 /*
581 * Block waiting for the carrier detect and the line to become
582 * free (i.e., not in use by the callout). While we are in
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100583 * this loop, port->port.count is dropped by one, so that
Jiri Slaby1c456072008-02-07 00:16:46 -0800584 * mxser_close() knows when to free things. We restore it upon
585 * exit, either normal or abnormal.
586 */
587 retval = 0;
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100588 add_wait_queue(&port->port.open_wait, &wait);
Jiri Slaby1c456072008-02-07 00:16:46 -0800589
590 spin_lock_irqsave(&port->slock, flags);
591 if (!tty_hung_up_p(filp))
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100592 port->port.count--;
Jiri Slaby1c456072008-02-07 00:16:46 -0800593 spin_unlock_irqrestore(&port->slock, flags);
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100594 port->port.blocked_open++;
Jiri Slaby1c456072008-02-07 00:16:46 -0800595 while (1) {
596 spin_lock_irqsave(&port->slock, flags);
597 outb(inb(port->ioaddr + UART_MCR) |
598 UART_MCR_DTR | UART_MCR_RTS, port->ioaddr + UART_MCR);
599 spin_unlock_irqrestore(&port->slock, flags);
600 set_current_state(TASK_INTERRUPTIBLE);
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100601 if (tty_hung_up_p(filp) || !(port->port.flags & ASYNC_INITIALIZED)) {
602 if (port->port.flags & ASYNC_HUP_NOTIFY)
Jiri Slaby1c456072008-02-07 00:16:46 -0800603 retval = -EAGAIN;
604 else
605 retval = -ERESTARTSYS;
606 break;
607 }
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100608 if (!(port->port.flags & ASYNC_CLOSING) &&
Jiri Slaby1c456072008-02-07 00:16:46 -0800609 (do_clocal ||
610 (inb(port->ioaddr + UART_MSR) & UART_MSR_DCD)))
611 break;
612 if (signal_pending(current)) {
613 retval = -ERESTARTSYS;
614 break;
615 }
616 schedule();
617 }
618 set_current_state(TASK_RUNNING);
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100619 remove_wait_queue(&port->port.open_wait, &wait);
Jiri Slaby1c456072008-02-07 00:16:46 -0800620 if (!tty_hung_up_p(filp))
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100621 port->port.count++;
622 port->port.blocked_open--;
Jiri Slaby1c456072008-02-07 00:16:46 -0800623 if (retval)
624 return retval;
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100625 port->port.flags |= ASYNC_NORMAL_ACTIVE;
Jiri Slaby1c456072008-02-07 00:16:46 -0800626 return 0;
627}
628
629static int mxser_set_baud(struct mxser_port *info, long newspd)
630{
631 int quot = 0, baud;
632 unsigned char cval;
633
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100634 if (!info->port.tty || !info->port.tty->termios)
Jiri Slaby1c456072008-02-07 00:16:46 -0800635 return -1;
636
637 if (!(info->ioaddr))
638 return -1;
639
640 if (newspd > info->max_baud)
641 return -1;
642
643 if (newspd == 134) {
644 quot = 2 * info->baud_base / 269;
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100645 tty_encode_baud_rate(info->port.tty, 134, 134);
Jiri Slaby1c456072008-02-07 00:16:46 -0800646 } else if (newspd) {
647 quot = info->baud_base / newspd;
648 if (quot == 0)
649 quot = 1;
650 baud = info->baud_base/quot;
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100651 tty_encode_baud_rate(info->port.tty, baud, baud);
Jiri Slaby1c456072008-02-07 00:16:46 -0800652 } else {
653 quot = 0;
654 }
655
656 info->timeout = ((info->xmit_fifo_size * HZ * 10 * quot) / info->baud_base);
657 info->timeout += HZ / 50; /* Add .02 seconds of slop */
658
659 if (quot) {
660 info->MCR |= UART_MCR_DTR;
661 outb(info->MCR, info->ioaddr + UART_MCR);
662 } else {
663 info->MCR &= ~UART_MCR_DTR;
664 outb(info->MCR, info->ioaddr + UART_MCR);
665 return 0;
666 }
667
668 cval = inb(info->ioaddr + UART_LCR);
669
670 outb(cval | UART_LCR_DLAB, info->ioaddr + UART_LCR); /* set DLAB */
671
672 outb(quot & 0xff, info->ioaddr + UART_DLL); /* LS of divisor */
673 outb(quot >> 8, info->ioaddr + UART_DLM); /* MS of divisor */
674 outb(cval, info->ioaddr + UART_LCR); /* reset DLAB */
675
676#ifdef BOTHER
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100677 if (C_BAUD(info->port.tty) == BOTHER) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800678 quot = info->baud_base % newspd;
679 quot *= 8;
680 if (quot % newspd > newspd / 2) {
681 quot /= newspd;
682 quot++;
683 } else
684 quot /= newspd;
685
Christoph Hellwig148ff862008-04-30 00:54:29 -0700686 mxser_set_must_enum_value(info->ioaddr, quot);
Jiri Slaby1c456072008-02-07 00:16:46 -0800687 } else
688#endif
Christoph Hellwig148ff862008-04-30 00:54:29 -0700689 mxser_set_must_enum_value(info->ioaddr, 0);
Jiri Slaby1c456072008-02-07 00:16:46 -0800690
691 return 0;
692}
693
694/*
695 * This routine is called to set the UART divisor registers to match
696 * the specified baud rate for a serial port.
697 */
698static int mxser_change_speed(struct mxser_port *info,
699 struct ktermios *old_termios)
700{
701 unsigned cflag, cval, fcr;
702 int ret = 0;
703 unsigned char status;
704
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100705 if (!info->port.tty || !info->port.tty->termios)
Jiri Slaby1c456072008-02-07 00:16:46 -0800706 return ret;
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100707 cflag = info->port.tty->termios->c_cflag;
Jiri Slaby1c456072008-02-07 00:16:46 -0800708 if (!(info->ioaddr))
709 return ret;
710
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100711 if (mxser_set_baud_method[info->port.tty->index] == 0)
712 mxser_set_baud(info, tty_get_baud_rate(info->port.tty));
Jiri Slaby1c456072008-02-07 00:16:46 -0800713
714 /* byte size and parity */
715 switch (cflag & CSIZE) {
716 case CS5:
717 cval = 0x00;
718 break;
719 case CS6:
720 cval = 0x01;
721 break;
722 case CS7:
723 cval = 0x02;
724 break;
725 case CS8:
726 cval = 0x03;
727 break;
728 default:
729 cval = 0x00;
730 break; /* too keep GCC shut... */
731 }
732 if (cflag & CSTOPB)
733 cval |= 0x04;
734 if (cflag & PARENB)
735 cval |= UART_LCR_PARITY;
736 if (!(cflag & PARODD))
737 cval |= UART_LCR_EPAR;
738 if (cflag & CMSPAR)
739 cval |= UART_LCR_SPAR;
740
741 if ((info->type == PORT_8250) || (info->type == PORT_16450)) {
742 if (info->board->chip_flag) {
743 fcr = UART_FCR_ENABLE_FIFO;
744 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
Christoph Hellwig148ff862008-04-30 00:54:29 -0700745 mxser_set_must_fifo_value(info);
Jiri Slaby1c456072008-02-07 00:16:46 -0800746 } else
747 fcr = 0;
748 } else {
749 fcr = UART_FCR_ENABLE_FIFO;
750 if (info->board->chip_flag) {
751 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
Christoph Hellwig148ff862008-04-30 00:54:29 -0700752 mxser_set_must_fifo_value(info);
Jiri Slaby1c456072008-02-07 00:16:46 -0800753 } else {
754 switch (info->rx_trigger) {
755 case 1:
756 fcr |= UART_FCR_TRIGGER_1;
757 break;
758 case 4:
759 fcr |= UART_FCR_TRIGGER_4;
760 break;
761 case 8:
762 fcr |= UART_FCR_TRIGGER_8;
763 break;
764 default:
765 fcr |= UART_FCR_TRIGGER_14;
766 break;
767 }
768 }
769 }
770
771 /* CTS flow control flag and modem status interrupts */
772 info->IER &= ~UART_IER_MSI;
773 info->MCR &= ~UART_MCR_AFE;
774 if (cflag & CRTSCTS) {
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100775 info->port.flags |= ASYNC_CTS_FLOW;
Jiri Slaby1c456072008-02-07 00:16:46 -0800776 info->IER |= UART_IER_MSI;
777 if ((info->type == PORT_16550A) || (info->board->chip_flag)) {
778 info->MCR |= UART_MCR_AFE;
779 } else {
780 status = inb(info->ioaddr + UART_MSR);
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100781 if (info->port.tty->hw_stopped) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800782 if (status & UART_MSR_CTS) {
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100783 info->port.tty->hw_stopped = 0;
Jiri Slaby1c456072008-02-07 00:16:46 -0800784 if (info->type != PORT_16550A &&
785 !info->board->chip_flag) {
786 outb(info->IER & ~UART_IER_THRI,
787 info->ioaddr +
788 UART_IER);
789 info->IER |= UART_IER_THRI;
790 outb(info->IER, info->ioaddr +
791 UART_IER);
792 }
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100793 tty_wakeup(info->port.tty);
Jiri Slaby1c456072008-02-07 00:16:46 -0800794 }
795 } else {
796 if (!(status & UART_MSR_CTS)) {
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100797 info->port.tty->hw_stopped = 1;
Jiri Slaby1c456072008-02-07 00:16:46 -0800798 if ((info->type != PORT_16550A) &&
799 (!info->board->chip_flag)) {
800 info->IER &= ~UART_IER_THRI;
801 outb(info->IER, info->ioaddr +
802 UART_IER);
803 }
804 }
805 }
806 }
807 } else {
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100808 info->port.flags &= ~ASYNC_CTS_FLOW;
Jiri Slaby1c456072008-02-07 00:16:46 -0800809 }
810 outb(info->MCR, info->ioaddr + UART_MCR);
811 if (cflag & CLOCAL) {
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100812 info->port.flags &= ~ASYNC_CHECK_CD;
Jiri Slaby1c456072008-02-07 00:16:46 -0800813 } else {
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100814 info->port.flags |= ASYNC_CHECK_CD;
Jiri Slaby1c456072008-02-07 00:16:46 -0800815 info->IER |= UART_IER_MSI;
816 }
817 outb(info->IER, info->ioaddr + UART_IER);
818
819 /*
820 * Set up parity check flag
821 */
822 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100823 if (I_INPCK(info->port.tty))
Jiri Slaby1c456072008-02-07 00:16:46 -0800824 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100825 if (I_BRKINT(info->port.tty) || I_PARMRK(info->port.tty))
Jiri Slaby1c456072008-02-07 00:16:46 -0800826 info->read_status_mask |= UART_LSR_BI;
827
828 info->ignore_status_mask = 0;
829
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100830 if (I_IGNBRK(info->port.tty)) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800831 info->ignore_status_mask |= UART_LSR_BI;
832 info->read_status_mask |= UART_LSR_BI;
833 /*
834 * If we're ignore parity and break indicators, ignore
835 * overruns too. (For real raw support).
836 */
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100837 if (I_IGNPAR(info->port.tty)) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800838 info->ignore_status_mask |=
839 UART_LSR_OE |
840 UART_LSR_PE |
841 UART_LSR_FE;
842 info->read_status_mask |=
843 UART_LSR_OE |
844 UART_LSR_PE |
845 UART_LSR_FE;
846 }
847 }
848 if (info->board->chip_flag) {
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100849 mxser_set_must_xon1_value(info->ioaddr, START_CHAR(info->port.tty));
850 mxser_set_must_xoff1_value(info->ioaddr, STOP_CHAR(info->port.tty));
851 if (I_IXON(info->port.tty)) {
Christoph Hellwig148ff862008-04-30 00:54:29 -0700852 mxser_enable_must_rx_software_flow_control(
853 info->ioaddr);
Jiri Slaby1c456072008-02-07 00:16:46 -0800854 } else {
Christoph Hellwig148ff862008-04-30 00:54:29 -0700855 mxser_disable_must_rx_software_flow_control(
856 info->ioaddr);
Jiri Slaby1c456072008-02-07 00:16:46 -0800857 }
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100858 if (I_IXOFF(info->port.tty)) {
Christoph Hellwig148ff862008-04-30 00:54:29 -0700859 mxser_enable_must_tx_software_flow_control(
860 info->ioaddr);
Jiri Slaby1c456072008-02-07 00:16:46 -0800861 } else {
Christoph Hellwig148ff862008-04-30 00:54:29 -0700862 mxser_disable_must_tx_software_flow_control(
863 info->ioaddr);
Jiri Slaby1c456072008-02-07 00:16:46 -0800864 }
865 }
866
867
868 outb(fcr, info->ioaddr + UART_FCR); /* set fcr */
869 outb(cval, info->ioaddr + UART_LCR);
870
871 return ret;
872}
873
874static void mxser_check_modem_status(struct mxser_port *port, int status)
875{
876 /* update input line counters */
877 if (status & UART_MSR_TERI)
878 port->icount.rng++;
879 if (status & UART_MSR_DDSR)
880 port->icount.dsr++;
881 if (status & UART_MSR_DDCD)
882 port->icount.dcd++;
883 if (status & UART_MSR_DCTS)
884 port->icount.cts++;
885 port->mon_data.modem_status = status;
886 wake_up_interruptible(&port->delta_msr_wait);
887
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100888 if ((port->port.flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800889 if (status & UART_MSR_DCD)
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100890 wake_up_interruptible(&port->port.open_wait);
Jiri Slaby1c456072008-02-07 00:16:46 -0800891 }
892
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100893 if (port->port.flags & ASYNC_CTS_FLOW) {
894 if (port->port.tty->hw_stopped) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800895 if (status & UART_MSR_CTS) {
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100896 port->port.tty->hw_stopped = 0;
Jiri Slaby1c456072008-02-07 00:16:46 -0800897
898 if ((port->type != PORT_16550A) &&
899 (!port->board->chip_flag)) {
900 outb(port->IER & ~UART_IER_THRI,
901 port->ioaddr + UART_IER);
902 port->IER |= UART_IER_THRI;
903 outb(port->IER, port->ioaddr +
904 UART_IER);
905 }
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100906 tty_wakeup(port->port.tty);
Jiri Slaby1c456072008-02-07 00:16:46 -0800907 }
908 } else {
909 if (!(status & UART_MSR_CTS)) {
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100910 port->port.tty->hw_stopped = 1;
Jiri Slaby1c456072008-02-07 00:16:46 -0800911 if (port->type != PORT_16550A &&
912 !port->board->chip_flag) {
913 port->IER &= ~UART_IER_THRI;
914 outb(port->IER, port->ioaddr +
915 UART_IER);
916 }
917 }
918 }
919 }
920}
921
922static int mxser_startup(struct mxser_port *info)
923{
924 unsigned long page;
925 unsigned long flags;
926
927 page = __get_free_page(GFP_KERNEL);
928 if (!page)
929 return -ENOMEM;
930
931 spin_lock_irqsave(&info->slock, flags);
932
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100933 if (info->port.flags & ASYNC_INITIALIZED) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800934 free_page(page);
935 spin_unlock_irqrestore(&info->slock, flags);
936 return 0;
937 }
938
939 if (!info->ioaddr || !info->type) {
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100940 if (info->port.tty)
941 set_bit(TTY_IO_ERROR, &info->port.tty->flags);
Jiri Slaby1c456072008-02-07 00:16:46 -0800942 free_page(page);
943 spin_unlock_irqrestore(&info->slock, flags);
944 return 0;
945 }
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100946 if (info->port.xmit_buf)
Jiri Slaby1c456072008-02-07 00:16:46 -0800947 free_page(page);
948 else
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100949 info->port.xmit_buf = (unsigned char *) page;
Jiri Slaby1c456072008-02-07 00:16:46 -0800950
951 /*
952 * Clear the FIFO buffers and disable them
953 * (they will be reenabled in mxser_change_speed())
954 */
955 if (info->board->chip_flag)
956 outb((UART_FCR_CLEAR_RCVR |
957 UART_FCR_CLEAR_XMIT |
958 MOXA_MUST_FCR_GDA_MODE_ENABLE), info->ioaddr + UART_FCR);
959 else
960 outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
961 info->ioaddr + UART_FCR);
962
963 /*
964 * At this point there's no way the LSR could still be 0xFF;
965 * if it is, then bail out, because there's likely no UART
966 * here.
967 */
968 if (inb(info->ioaddr + UART_LSR) == 0xff) {
969 spin_unlock_irqrestore(&info->slock, flags);
970 if (capable(CAP_SYS_ADMIN)) {
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100971 if (info->port.tty)
972 set_bit(TTY_IO_ERROR, &info->port.tty->flags);
Jiri Slaby1c456072008-02-07 00:16:46 -0800973 return 0;
974 } else
975 return -ENODEV;
976 }
977
978 /*
979 * Clear the interrupt registers.
980 */
981 (void) inb(info->ioaddr + UART_LSR);
982 (void) inb(info->ioaddr + UART_RX);
983 (void) inb(info->ioaddr + UART_IIR);
984 (void) inb(info->ioaddr + UART_MSR);
985
986 /*
987 * Now, initialize the UART
988 */
989 outb(UART_LCR_WLEN8, info->ioaddr + UART_LCR); /* reset DLAB */
990 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
991 outb(info->MCR, info->ioaddr + UART_MCR);
992
993 /*
994 * Finally, enable interrupts
995 */
996 info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
997
998 if (info->board->chip_flag)
999 info->IER |= MOXA_MUST_IER_EGDAI;
1000 outb(info->IER, info->ioaddr + UART_IER); /* enable interrupts */
1001
1002 /*
1003 * And clear the interrupt registers again for luck.
1004 */
1005 (void) inb(info->ioaddr + UART_LSR);
1006 (void) inb(info->ioaddr + UART_RX);
1007 (void) inb(info->ioaddr + UART_IIR);
1008 (void) inb(info->ioaddr + UART_MSR);
1009
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001010 if (info->port.tty)
1011 clear_bit(TTY_IO_ERROR, &info->port.tty->flags);
Jiri Slaby1c456072008-02-07 00:16:46 -08001012 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1013
1014 /*
1015 * and set the speed of the serial port
1016 */
1017 mxser_change_speed(info, NULL);
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001018 info->port.flags |= ASYNC_INITIALIZED;
Jiri Slaby1c456072008-02-07 00:16:46 -08001019 spin_unlock_irqrestore(&info->slock, flags);
1020
1021 return 0;
1022}
1023
1024/*
1025 * This routine will shutdown a serial port; interrupts maybe disabled, and
1026 * DTR is dropped if the hangup on close termio flag is on.
1027 */
1028static void mxser_shutdown(struct mxser_port *info)
1029{
1030 unsigned long flags;
1031
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001032 if (!(info->port.flags & ASYNC_INITIALIZED))
Jiri Slaby1c456072008-02-07 00:16:46 -08001033 return;
1034
1035 spin_lock_irqsave(&info->slock, flags);
1036
1037 /*
1038 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
1039 * here so the queue might never be waken up
1040 */
1041 wake_up_interruptible(&info->delta_msr_wait);
1042
1043 /*
1044 * Free the IRQ, if necessary
1045 */
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001046 if (info->port.xmit_buf) {
1047 free_page((unsigned long) info->port.xmit_buf);
1048 info->port.xmit_buf = NULL;
Jiri Slaby1c456072008-02-07 00:16:46 -08001049 }
1050
1051 info->IER = 0;
1052 outb(0x00, info->ioaddr + UART_IER);
1053
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001054 if (!info->port.tty || (info->port.tty->termios->c_cflag & HUPCL))
Jiri Slaby1c456072008-02-07 00:16:46 -08001055 info->MCR &= ~(UART_MCR_DTR | UART_MCR_RTS);
1056 outb(info->MCR, info->ioaddr + UART_MCR);
1057
1058 /* clear Rx/Tx FIFO's */
1059 if (info->board->chip_flag)
1060 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT |
1061 MOXA_MUST_FCR_GDA_MODE_ENABLE,
1062 info->ioaddr + UART_FCR);
1063 else
1064 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
1065 info->ioaddr + UART_FCR);
1066
1067 /* read data port to reset things */
1068 (void) inb(info->ioaddr + UART_RX);
1069
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001070 if (info->port.tty)
1071 set_bit(TTY_IO_ERROR, &info->port.tty->flags);
Jiri Slaby1c456072008-02-07 00:16:46 -08001072
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001073 info->port.flags &= ~ASYNC_INITIALIZED;
Jiri Slaby1c456072008-02-07 00:16:46 -08001074
1075 if (info->board->chip_flag)
1076 SET_MOXA_MUST_NO_SOFTWARE_FLOW_CONTROL(info->ioaddr);
1077
1078 spin_unlock_irqrestore(&info->slock, flags);
1079}
1080
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081/*
1082 * This routine is called whenever a serial port is opened. It
1083 * enables interrupts for a serial port, linking in its async structure into
1084 * the IRQ chain. It also performs the serial-specific
1085 * initialization for the tty structure.
1086 */
1087static int mxser_open(struct tty_struct *tty, struct file *filp)
1088{
Jiri Slaby1c456072008-02-07 00:16:46 -08001089 struct mxser_port *info;
1090 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 int retval, line;
1092
1093 line = tty->index;
1094 if (line == MXSER_PORTS)
1095 return 0;
1096 if (line < 0 || line > MXSER_PORTS)
1097 return -ENODEV;
Jiri Slaby1c456072008-02-07 00:16:46 -08001098 info = &mxser_boards[line / MXSER_PORTS_PER_BOARD].ports[line % MXSER_PORTS_PER_BOARD];
1099 if (!info->ioaddr)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001100 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
1102 tty->driver_data = info;
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001103 info->port.tty = tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 /*
1105 * Start up serial port
1106 */
Jiri Slaby1c456072008-02-07 00:16:46 -08001107 spin_lock_irqsave(&info->slock, flags);
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001108 info->port.count++;
Jiri Slaby1c456072008-02-07 00:16:46 -08001109 spin_unlock_irqrestore(&info->slock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 retval = mxser_startup(info);
1111 if (retval)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001112 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
1114 retval = mxser_block_til_ready(tty, filp, info);
1115 if (retval)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001116 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
Cedric Le Goater8cddd702007-02-10 01:46:33 -08001118 /* unmark here for very high baud rate (ex. 921600 bps) used */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 tty->low_latency = 1;
1120 return 0;
1121}
1122
Alan Cox978e5952008-04-30 00:53:59 -07001123static void mxser_flush_buffer(struct tty_struct *tty)
1124{
1125 struct mxser_port *info = tty->driver_data;
1126 char fcr;
1127 unsigned long flags;
1128
1129
1130 spin_lock_irqsave(&info->slock, flags);
1131 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1132
1133 fcr = inb(info->ioaddr + UART_FCR);
1134 outb((fcr | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
1135 info->ioaddr + UART_FCR);
1136 outb(fcr, info->ioaddr + UART_FCR);
1137
1138 spin_unlock_irqrestore(&info->slock, flags);
1139
1140 tty_wakeup(tty);
1141}
1142
1143
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144/*
1145 * This routine is called when the serial port gets closed. First, we
1146 * wait for the last remaining data to be sent. Then, we unlink its
1147 * async structure from the interrupt chain if necessary, and we free
1148 * that IRQ if nothing is left in the chain.
1149 */
1150static void mxser_close(struct tty_struct *tty, struct file *filp)
1151{
Jiri Slaby1c456072008-02-07 00:16:46 -08001152 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
1154 unsigned long timeout;
1155 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
1157 if (tty->index == MXSER_PORTS)
1158 return;
1159 if (!info)
Kirill Smelkov6f08b722005-11-07 00:59:23 -08001160 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
1162 spin_lock_irqsave(&info->slock, flags);
1163
1164 if (tty_hung_up_p(filp)) {
1165 spin_unlock_irqrestore(&info->slock, flags);
1166 return;
1167 }
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001168 if ((tty->count == 1) && (info->port.count != 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 /*
1170 * Uh, oh. tty->count is 1, which means that the tty
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001171 * structure will be freed. Info->port.count should always
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 * be one in these conditions. If it's greater than
1173 * one, we've got real problems, since it means the
1174 * serial port won't be shutdown.
1175 */
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001176 printk(KERN_ERR "mxser_close: bad serial port count; "
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001177 "tty->count is 1, info->port.count is %d\n", info->port.count);
1178 info->port.count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 }
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001180 if (--info->port.count < 0) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001181 printk(KERN_ERR "mxser_close: bad serial port count for "
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001182 "ttys%d: %d\n", tty->index, info->port.count);
1183 info->port.count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 }
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001185 if (info->port.count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 spin_unlock_irqrestore(&info->slock, flags);
1187 return;
1188 }
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001189 info->port.flags |= ASYNC_CLOSING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 spin_unlock_irqrestore(&info->slock, flags);
1191 /*
1192 * Save the termios structure, since this port may have
1193 * separate termios for callout and dialin.
1194 */
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001195 if (info->port.flags & ASYNC_NORMAL_ACTIVE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 info->normal_termios = *tty->termios;
1197 /*
1198 * Now we wait for the transmit buffer to clear; and we notify
1199 * the line discipline to only process XON/XOFF characters.
1200 */
1201 tty->closing = 1;
1202 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1203 tty_wait_until_sent(tty, info->closing_wait);
1204 /*
1205 * At this point we stop accepting input. To do this, we
1206 * disable the receive line status interrupts, and tell the
1207 * interrupt driver to stop checking the data ready bit in the
1208 * line status register.
1209 */
1210 info->IER &= ~UART_IER_RLSI;
Jiri Slaby1c456072008-02-07 00:16:46 -08001211 if (info->board->chip_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 info->IER &= ~MOXA_MUST_RECV_ISR;
Jiri Slaby1c456072008-02-07 00:16:46 -08001213
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001214 if (info->port.flags & ASYNC_INITIALIZED) {
Jiri Slaby1c456072008-02-07 00:16:46 -08001215 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 /*
1217 * Before we drop DTR, make sure the UART transmitter
1218 * has completely drained; this is especially
1219 * important if there is a transmit FIFO!
1220 */
1221 timeout = jiffies + HZ;
Jiri Slaby1c456072008-02-07 00:16:46 -08001222 while (!(inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT)) {
Nishanth Aravamudanda4cd8d2005-09-10 00:27:30 -07001223 schedule_timeout_interruptible(5);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 if (time_after(jiffies, timeout))
1225 break;
1226 }
1227 }
1228 mxser_shutdown(info);
1229
Alan Cox978e5952008-04-30 00:53:59 -07001230 mxser_flush_buffer(tty);
Jiri Slaby1c456072008-02-07 00:16:46 -08001231 tty_ldisc_flush(tty);
1232
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 tty->closing = 0;
1234 info->event = 0;
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001235 info->port.tty = NULL;
1236 if (info->port.blocked_open) {
Nishanth Aravamudanda4cd8d2005-09-10 00:27:30 -07001237 if (info->close_delay)
1238 schedule_timeout_interruptible(info->close_delay);
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001239 wake_up_interruptible(&info->port.open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 }
1241
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001242 info->port.flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243}
1244
1245static int mxser_write(struct tty_struct *tty, const unsigned char *buf, int count)
1246{
1247 int c, total = 0;
Jiri Slaby1c456072008-02-07 00:16:46 -08001248 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 unsigned long flags;
1250
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001251 if (!info->port.xmit_buf)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001252 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
1254 while (1) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001255 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1256 SERIAL_XMIT_SIZE - info->xmit_head));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 if (c <= 0)
1258 break;
1259
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001260 memcpy(info->port.xmit_buf + info->xmit_head, buf, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 spin_lock_irqsave(&info->slock, flags);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001262 info->xmit_head = (info->xmit_head + c) &
1263 (SERIAL_XMIT_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 info->xmit_cnt += c;
1265 spin_unlock_irqrestore(&info->slock, flags);
1266
1267 buf += c;
1268 count -= c;
1269 total += c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 }
1271
Jiri Slaby1c456072008-02-07 00:16:46 -08001272 if (info->xmit_cnt && !tty->stopped) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001273 if (!tty->hw_stopped ||
1274 (info->type == PORT_16550A) ||
Jiri Slaby1c456072008-02-07 00:16:46 -08001275 (info->board->chip_flag)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby1c456072008-02-07 00:16:46 -08001277 outb(info->IER & ~UART_IER_THRI, info->ioaddr +
1278 UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 info->IER |= UART_IER_THRI;
Jiri Slaby1c456072008-02-07 00:16:46 -08001280 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 spin_unlock_irqrestore(&info->slock, flags);
1282 }
1283 }
1284 return total;
1285}
1286
Alan Cox0be2ead2008-04-30 00:54:03 -07001287static int mxser_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288{
Jiri Slaby1c456072008-02-07 00:16:46 -08001289 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 unsigned long flags;
1291
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001292 if (!info->port.xmit_buf)
Alan Cox0be2ead2008-04-30 00:54:03 -07001293 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
1295 if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1)
Alan Cox0be2ead2008-04-30 00:54:03 -07001296 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
1298 spin_lock_irqsave(&info->slock, flags);
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001299 info->port.xmit_buf[info->xmit_head++] = ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 info->xmit_head &= SERIAL_XMIT_SIZE - 1;
1301 info->xmit_cnt++;
1302 spin_unlock_irqrestore(&info->slock, flags);
Jiri Slaby1c456072008-02-07 00:16:46 -08001303 if (!tty->stopped) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001304 if (!tty->hw_stopped ||
1305 (info->type == PORT_16550A) ||
Jiri Slaby1c456072008-02-07 00:16:46 -08001306 info->board->chip_flag) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby1c456072008-02-07 00:16:46 -08001308 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 info->IER |= UART_IER_THRI;
Jiri Slaby1c456072008-02-07 00:16:46 -08001310 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 spin_unlock_irqrestore(&info->slock, flags);
1312 }
1313 }
Alan Cox0be2ead2008-04-30 00:54:03 -07001314 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315}
1316
1317
1318static void mxser_flush_chars(struct tty_struct *tty)
1319{
Jiri Slaby1c456072008-02-07 00:16:46 -08001320 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 unsigned long flags;
1322
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001323 if (info->xmit_cnt <= 0 ||
1324 tty->stopped ||
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001325 !info->port.xmit_buf ||
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001326 (tty->hw_stopped &&
1327 (info->type != PORT_16550A) &&
Jiri Slaby1c456072008-02-07 00:16:46 -08001328 (!info->board->chip_flag)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001329 ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 return;
1331
1332 spin_lock_irqsave(&info->slock, flags);
1333
Jiri Slaby1c456072008-02-07 00:16:46 -08001334 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 info->IER |= UART_IER_THRI;
Jiri Slaby1c456072008-02-07 00:16:46 -08001336 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
1338 spin_unlock_irqrestore(&info->slock, flags);
1339}
1340
1341static int mxser_write_room(struct tty_struct *tty)
1342{
Jiri Slaby1c456072008-02-07 00:16:46 -08001343 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 int ret;
1345
1346 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1347 if (ret < 0)
1348 ret = 0;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001349 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350}
1351
1352static int mxser_chars_in_buffer(struct tty_struct *tty)
1353{
Jiri Slaby1c456072008-02-07 00:16:46 -08001354 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 return info->xmit_cnt;
1356}
1357
Jiri Slaby1c456072008-02-07 00:16:46 -08001358/*
1359 * ------------------------------------------------------------
1360 * friends of mxser_ioctl()
1361 * ------------------------------------------------------------
1362 */
1363static int mxser_get_serial_info(struct mxser_port *info,
1364 struct serial_struct __user *retinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365{
Jiri Slaby1c456072008-02-07 00:16:46 -08001366 struct serial_struct tmp = {
1367 .type = info->type,
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001368 .line = info->port.tty->index,
Jiri Slaby1c456072008-02-07 00:16:46 -08001369 .port = info->ioaddr,
1370 .irq = info->board->irq,
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001371 .flags = info->port.flags,
Jiri Slaby1c456072008-02-07 00:16:46 -08001372 .baud_base = info->baud_base,
1373 .close_delay = info->close_delay,
1374 .closing_wait = info->closing_wait,
1375 .custom_divisor = info->custom_divisor,
1376 .hub6 = 0
1377 };
1378 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1379 return -EFAULT;
1380 return 0;
1381}
1382
1383static int mxser_set_serial_info(struct mxser_port *info,
1384 struct serial_struct __user *new_info)
1385{
1386 struct serial_struct new_serial;
Jiri Slaby80ff8a82008-02-07 00:16:51 -08001387 speed_t baud;
Jiri Slaby1c456072008-02-07 00:16:46 -08001388 unsigned long sl_flags;
1389 unsigned int flags;
1390 int retval = 0;
1391
1392 if (!new_info || !info->ioaddr)
Jiri Slaby80ff8a82008-02-07 00:16:51 -08001393 return -ENODEV;
Jiri Slaby1c456072008-02-07 00:16:46 -08001394 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
1395 return -EFAULT;
1396
Jiri Slaby80ff8a82008-02-07 00:16:51 -08001397 if (new_serial.irq != info->board->irq ||
1398 new_serial.port != info->ioaddr)
1399 return -EINVAL;
Jiri Slaby1c456072008-02-07 00:16:46 -08001400
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001401 flags = info->port.flags & ASYNC_SPD_MASK;
Jiri Slaby1c456072008-02-07 00:16:46 -08001402
1403 if (!capable(CAP_SYS_ADMIN)) {
1404 if ((new_serial.baud_base != info->baud_base) ||
1405 (new_serial.close_delay != info->close_delay) ||
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001406 ((new_serial.flags & ~ASYNC_USR_MASK) != (info->port.flags & ~ASYNC_USR_MASK)))
Jiri Slaby1c456072008-02-07 00:16:46 -08001407 return -EPERM;
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001408 info->port.flags = ((info->port.flags & ~ASYNC_USR_MASK) |
Jiri Slaby1c456072008-02-07 00:16:46 -08001409 (new_serial.flags & ASYNC_USR_MASK));
1410 } else {
1411 /*
1412 * OK, past this point, all the error checking has been done.
1413 * At this point, we start making changes.....
1414 */
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001415 info->port.flags = ((info->port.flags & ~ASYNC_FLAGS) |
Jiri Slaby1c456072008-02-07 00:16:46 -08001416 (new_serial.flags & ASYNC_FLAGS));
1417 info->close_delay = new_serial.close_delay * HZ / 100;
1418 info->closing_wait = new_serial.closing_wait * HZ / 100;
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001419 info->port.tty->low_latency =
1420 (info->port.flags & ASYNC_LOW_LATENCY) ? 1 : 0;
1421 info->port.tty->low_latency = 0;
1422 if ((info->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST &&
Jiri Slaby80ff8a82008-02-07 00:16:51 -08001423 (new_serial.baud_base != info->baud_base ||
1424 new_serial.custom_divisor !=
1425 info->custom_divisor)) {
1426 baud = new_serial.baud_base / new_serial.custom_divisor;
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001427 tty_encode_baud_rate(info->port.tty, baud, baud);
Jiri Slaby80ff8a82008-02-07 00:16:51 -08001428 }
Jiri Slaby1c456072008-02-07 00:16:46 -08001429 }
1430
1431 info->type = new_serial.type;
1432
1433 process_txrx_fifo(info);
1434
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001435 if (info->port.flags & ASYNC_INITIALIZED) {
1436 if (flags != (info->port.flags & ASYNC_SPD_MASK)) {
Jiri Slaby1c456072008-02-07 00:16:46 -08001437 spin_lock_irqsave(&info->slock, sl_flags);
1438 mxser_change_speed(info, NULL);
1439 spin_unlock_irqrestore(&info->slock, sl_flags);
1440 }
1441 } else
1442 retval = mxser_startup(info);
1443
1444 return retval;
1445}
1446
1447/*
1448 * mxser_get_lsr_info - get line status register info
1449 *
1450 * Purpose: Let user call ioctl() to get info when the UART physically
1451 * is emptied. On bus types like RS485, the transmitter must
1452 * release the bus after transmitting. This must be done when
1453 * the transmit shift register is empty, not be done when the
1454 * transmit holding register is empty. This functionality
1455 * allows an RS485 driver to be written in user space.
1456 */
1457static int mxser_get_lsr_info(struct mxser_port *info,
1458 unsigned int __user *value)
1459{
1460 unsigned char status;
1461 unsigned int result;
1462 unsigned long flags;
1463
1464 spin_lock_irqsave(&info->slock, flags);
1465 status = inb(info->ioaddr + UART_LSR);
1466 spin_unlock_irqrestore(&info->slock, flags);
1467 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1468 return put_user(result, value);
1469}
1470
1471/*
1472 * This routine sends a break character out the serial port.
1473 */
1474static void mxser_send_break(struct mxser_port *info, int duration)
1475{
1476 unsigned long flags;
1477
1478 if (!info->ioaddr)
1479 return;
1480 set_current_state(TASK_INTERRUPTIBLE);
1481 spin_lock_irqsave(&info->slock, flags);
1482 outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
1483 info->ioaddr + UART_LCR);
1484 spin_unlock_irqrestore(&info->slock, flags);
1485 schedule_timeout(duration);
1486 spin_lock_irqsave(&info->slock, flags);
1487 outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
1488 info->ioaddr + UART_LCR);
1489 spin_unlock_irqrestore(&info->slock, flags);
1490}
1491
1492static int mxser_tiocmget(struct tty_struct *tty, struct file *file)
1493{
1494 struct mxser_port *info = tty->driver_data;
1495 unsigned char control, status;
1496 unsigned long flags;
1497
1498
1499 if (tty->index == MXSER_PORTS)
1500 return -ENOIOCTLCMD;
1501 if (test_bit(TTY_IO_ERROR, &tty->flags))
1502 return -EIO;
1503
1504 control = info->MCR;
1505
1506 spin_lock_irqsave(&info->slock, flags);
1507 status = inb(info->ioaddr + UART_MSR);
1508 if (status & UART_MSR_ANY_DELTA)
1509 mxser_check_modem_status(info, status);
1510 spin_unlock_irqrestore(&info->slock, flags);
1511 return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0) |
1512 ((control & UART_MCR_DTR) ? TIOCM_DTR : 0) |
1513 ((status & UART_MSR_DCD) ? TIOCM_CAR : 0) |
1514 ((status & UART_MSR_RI) ? TIOCM_RNG : 0) |
1515 ((status & UART_MSR_DSR) ? TIOCM_DSR : 0) |
1516 ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
1517}
1518
1519static int mxser_tiocmset(struct tty_struct *tty, struct file *file,
1520 unsigned int set, unsigned int clear)
1521{
1522 struct mxser_port *info = tty->driver_data;
1523 unsigned long flags;
1524
1525
1526 if (tty->index == MXSER_PORTS)
1527 return -ENOIOCTLCMD;
1528 if (test_bit(TTY_IO_ERROR, &tty->flags))
1529 return -EIO;
1530
1531 spin_lock_irqsave(&info->slock, flags);
1532
1533 if (set & TIOCM_RTS)
1534 info->MCR |= UART_MCR_RTS;
1535 if (set & TIOCM_DTR)
1536 info->MCR |= UART_MCR_DTR;
1537
1538 if (clear & TIOCM_RTS)
1539 info->MCR &= ~UART_MCR_RTS;
1540 if (clear & TIOCM_DTR)
1541 info->MCR &= ~UART_MCR_DTR;
1542
1543 outb(info->MCR, info->ioaddr + UART_MCR);
1544 spin_unlock_irqrestore(&info->slock, flags);
1545 return 0;
1546}
1547
1548static int __init mxser_program_mode(int port)
1549{
1550 int id, i, j, n;
1551
1552 outb(0, port);
1553 outb(0, port);
1554 outb(0, port);
1555 (void)inb(port);
1556 (void)inb(port);
1557 outb(0, port);
1558 (void)inb(port);
1559
1560 id = inb(port + 1) & 0x1F;
1561 if ((id != C168_ASIC_ID) &&
1562 (id != C104_ASIC_ID) &&
1563 (id != C102_ASIC_ID) &&
1564 (id != CI132_ASIC_ID) &&
1565 (id != CI134_ASIC_ID) &&
1566 (id != CI104J_ASIC_ID))
1567 return -1;
1568 for (i = 0, j = 0; i < 4; i++) {
1569 n = inb(port + 2);
1570 if (n == 'M') {
1571 j = 1;
1572 } else if ((j == 1) && (n == 1)) {
1573 j = 2;
1574 break;
1575 } else
1576 j = 0;
1577 }
1578 if (j != 2)
1579 id = -2;
1580 return id;
1581}
1582
1583static void __init mxser_normal_mode(int port)
1584{
1585 int i, n;
1586
1587 outb(0xA5, port + 1);
1588 outb(0x80, port + 3);
1589 outb(12, port + 0); /* 9600 bps */
1590 outb(0, port + 1);
1591 outb(0x03, port + 3); /* 8 data bits */
1592 outb(0x13, port + 4); /* loop back mode */
1593 for (i = 0; i < 16; i++) {
1594 n = inb(port + 5);
1595 if ((n & 0x61) == 0x60)
1596 break;
1597 if ((n & 1) == 1)
1598 (void)inb(port);
1599 }
1600 outb(0x00, port + 4);
1601}
1602
1603#define CHIP_SK 0x01 /* Serial Data Clock in Eprom */
1604#define CHIP_DO 0x02 /* Serial Data Output in Eprom */
1605#define CHIP_CS 0x04 /* Serial Chip Select in Eprom */
1606#define CHIP_DI 0x08 /* Serial Data Input in Eprom */
1607#define EN_CCMD 0x000 /* Chip's command register */
1608#define EN0_RSARLO 0x008 /* Remote start address reg 0 */
1609#define EN0_RSARHI 0x009 /* Remote start address reg 1 */
1610#define EN0_RCNTLO 0x00A /* Remote byte count reg WR */
1611#define EN0_RCNTHI 0x00B /* Remote byte count reg WR */
1612#define EN0_DCFG 0x00E /* Data configuration reg WR */
1613#define EN0_PORT 0x010 /* Rcv missed frame error counter RD */
1614#define ENC_PAGE0 0x000 /* Select page 0 of chip registers */
1615#define ENC_PAGE3 0x0C0 /* Select page 3 of chip registers */
1616static int __init mxser_read_register(int port, unsigned short *regs)
1617{
1618 int i, k, value, id;
1619 unsigned int j;
1620
1621 id = mxser_program_mode(port);
1622 if (id < 0)
1623 return id;
1624 for (i = 0; i < 14; i++) {
1625 k = (i & 0x3F) | 0x180;
1626 for (j = 0x100; j > 0; j >>= 1) {
1627 outb(CHIP_CS, port);
1628 if (k & j) {
1629 outb(CHIP_CS | CHIP_DO, port);
1630 outb(CHIP_CS | CHIP_DO | CHIP_SK, port); /* A? bit of read */
1631 } else {
1632 outb(CHIP_CS, port);
1633 outb(CHIP_CS | CHIP_SK, port); /* A? bit of read */
1634 }
1635 }
1636 (void)inb(port);
1637 value = 0;
1638 for (k = 0, j = 0x8000; k < 16; k++, j >>= 1) {
1639 outb(CHIP_CS, port);
1640 outb(CHIP_CS | CHIP_SK, port);
1641 if (inb(port) & CHIP_DI)
1642 value |= j;
1643 }
1644 regs[i] = value;
1645 outb(0, port);
1646 }
1647 mxser_normal_mode(port);
1648 return id;
1649}
1650
1651static int mxser_ioctl_special(unsigned int cmd, void __user *argp)
1652{
1653 struct mxser_port *port;
1654 int result, status;
1655 unsigned int i, j;
Alan Cox9d6d1622008-04-30 00:53:20 -07001656 int ret = 0;
Jiri Slaby1c456072008-02-07 00:16:46 -08001657
1658 switch (cmd) {
1659 case MOXA_GET_MAJOR:
1660 return put_user(ttymajor, (int __user *)argp);
1661
1662 case MOXA_CHKPORTENABLE:
1663 result = 0;
Alan Cox9d6d1622008-04-30 00:53:20 -07001664 lock_kernel();
Jiri Slaby1c456072008-02-07 00:16:46 -08001665 for (i = 0; i < MXSER_BOARDS; i++)
1666 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++)
1667 if (mxser_boards[i].ports[j].ioaddr)
1668 result |= (1 << i);
Alan Cox9d6d1622008-04-30 00:53:20 -07001669 unlock_kernel();
Jiri Slaby1c456072008-02-07 00:16:46 -08001670 return put_user(result, (unsigned long __user *)argp);
1671 case MOXA_GETDATACOUNT:
Alan Cox9d6d1622008-04-30 00:53:20 -07001672 lock_kernel();
Jiri Slaby1c456072008-02-07 00:16:46 -08001673 if (copy_to_user(argp, &mxvar_log, sizeof(mxvar_log)))
Alan Cox9d6d1622008-04-30 00:53:20 -07001674 ret = -EFAULT;
1675 unlock_kernel();
1676 return ret;
Jiri Slaby1c456072008-02-07 00:16:46 -08001677 case MOXA_GETMSTATUS:
Alan Cox9d6d1622008-04-30 00:53:20 -07001678 lock_kernel();
Jiri Slaby1c456072008-02-07 00:16:46 -08001679 for (i = 0; i < MXSER_BOARDS; i++)
1680 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
1681 port = &mxser_boards[i].ports[j];
1682
1683 GMStatus[i].ri = 0;
1684 if (!port->ioaddr) {
1685 GMStatus[i].dcd = 0;
1686 GMStatus[i].dsr = 0;
1687 GMStatus[i].cts = 0;
1688 continue;
1689 }
1690
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001691 if (!port->port.tty || !port->port.tty->termios)
Jiri Slaby1c456072008-02-07 00:16:46 -08001692 GMStatus[i].cflag =
1693 port->normal_termios.c_cflag;
1694 else
1695 GMStatus[i].cflag =
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001696 port->port.tty->termios->c_cflag;
Jiri Slaby1c456072008-02-07 00:16:46 -08001697
1698 status = inb(port->ioaddr + UART_MSR);
1699 if (status & 0x80 /*UART_MSR_DCD */ )
1700 GMStatus[i].dcd = 1;
1701 else
1702 GMStatus[i].dcd = 0;
1703
1704 if (status & 0x20 /*UART_MSR_DSR */ )
1705 GMStatus[i].dsr = 1;
1706 else
1707 GMStatus[i].dsr = 0;
1708
1709
1710 if (status & 0x10 /*UART_MSR_CTS */ )
1711 GMStatus[i].cts = 1;
1712 else
1713 GMStatus[i].cts = 0;
1714 }
Alan Cox9d6d1622008-04-30 00:53:20 -07001715 unlock_kernel();
Jiri Slaby1c456072008-02-07 00:16:46 -08001716 if (copy_to_user(argp, GMStatus,
1717 sizeof(struct mxser_mstatus) * MXSER_PORTS))
1718 return -EFAULT;
1719 return 0;
1720 case MOXA_ASPP_MON_EXT: {
1721 int p, shiftbit;
1722 unsigned long opmode;
1723 unsigned cflag, iflag;
1724
Alan Cox9d6d1622008-04-30 00:53:20 -07001725 lock_kernel();
1726 for (i = 0; i < MXSER_BOARDS; i++) {
Jiri Slaby1c456072008-02-07 00:16:46 -08001727 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
1728 port = &mxser_boards[i].ports[j];
1729 if (!port->ioaddr)
1730 continue;
1731
1732 status = mxser_get_msr(port->ioaddr, 0, i);
1733
1734 if (status & UART_MSR_TERI)
1735 port->icount.rng++;
1736 if (status & UART_MSR_DDSR)
1737 port->icount.dsr++;
1738 if (status & UART_MSR_DDCD)
1739 port->icount.dcd++;
1740 if (status & UART_MSR_DCTS)
1741 port->icount.cts++;
1742
1743 port->mon_data.modem_status = status;
1744 mon_data_ext.rx_cnt[i] = port->mon_data.rxcnt;
1745 mon_data_ext.tx_cnt[i] = port->mon_data.txcnt;
1746 mon_data_ext.up_rxcnt[i] =
1747 port->mon_data.up_rxcnt;
1748 mon_data_ext.up_txcnt[i] =
1749 port->mon_data.up_txcnt;
1750 mon_data_ext.modem_status[i] =
1751 port->mon_data.modem_status;
1752 mon_data_ext.baudrate[i] =
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001753 tty_get_baud_rate(port->port.tty);
Jiri Slaby1c456072008-02-07 00:16:46 -08001754
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001755 if (!port->port.tty || !port->port.tty->termios) {
Jiri Slaby1c456072008-02-07 00:16:46 -08001756 cflag = port->normal_termios.c_cflag;
1757 iflag = port->normal_termios.c_iflag;
1758 } else {
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001759 cflag = port->port.tty->termios->c_cflag;
1760 iflag = port->port.tty->termios->c_iflag;
Jiri Slaby1c456072008-02-07 00:16:46 -08001761 }
1762
1763 mon_data_ext.databits[i] = cflag & CSIZE;
1764
1765 mon_data_ext.stopbits[i] = cflag & CSTOPB;
1766
1767 mon_data_ext.parity[i] =
1768 cflag & (PARENB | PARODD | CMSPAR);
1769
1770 mon_data_ext.flowctrl[i] = 0x00;
1771
1772 if (cflag & CRTSCTS)
1773 mon_data_ext.flowctrl[i] |= 0x03;
1774
1775 if (iflag & (IXON | IXOFF))
1776 mon_data_ext.flowctrl[i] |= 0x0C;
1777
1778 if (port->type == PORT_16550A)
1779 mon_data_ext.fifo[i] = 1;
1780 else
1781 mon_data_ext.fifo[i] = 0;
1782
1783 p = i % 4;
1784 shiftbit = p * 2;
1785 opmode = inb(port->opmode_ioaddr) >> shiftbit;
1786 opmode &= OP_MODE_MASK;
1787
1788 mon_data_ext.iftype[i] = opmode;
1789
1790 }
Alan Cox9d6d1622008-04-30 00:53:20 -07001791 }
1792 unlock_kernel();
1793 if (copy_to_user(argp, &mon_data_ext,
1794 sizeof(mon_data_ext)))
1795 return -EFAULT;
1796 return 0;
1797 }
1798 default:
Jiri Slaby1c456072008-02-07 00:16:46 -08001799 return -ENOIOCTLCMD;
1800 }
1801 return 0;
1802}
1803
1804static int mxser_cflags_changed(struct mxser_port *info, unsigned long arg,
1805 struct async_icount *cprev)
1806{
1807 struct async_icount cnow;
1808 unsigned long flags;
1809 int ret;
1810
1811 spin_lock_irqsave(&info->slock, flags);
1812 cnow = info->icount; /* atomic copy */
1813 spin_unlock_irqrestore(&info->slock, flags);
1814
1815 ret = ((arg & TIOCM_RNG) && (cnow.rng != cprev->rng)) ||
1816 ((arg & TIOCM_DSR) && (cnow.dsr != cprev->dsr)) ||
1817 ((arg & TIOCM_CD) && (cnow.dcd != cprev->dcd)) ||
1818 ((arg & TIOCM_CTS) && (cnow.cts != cprev->cts));
1819
1820 *cprev = cnow;
1821
1822 return ret;
1823}
1824
1825static int mxser_ioctl(struct tty_struct *tty, struct file *file,
1826 unsigned int cmd, unsigned long arg)
1827{
1828 struct mxser_port *info = tty->driver_data;
1829 struct async_icount cnow;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 struct serial_icounter_struct __user *p_cuser;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 unsigned long flags;
1832 void __user *argp = (void __user *)arg;
Jiri Slaby1c456072008-02-07 00:16:46 -08001833 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834
1835 if (tty->index == MXSER_PORTS)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001836 return mxser_ioctl_special(cmd, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 if (cmd == MOXA_SET_OP_MODE || cmd == MOXA_GET_OP_MODE) {
Jiri Slaby1c456072008-02-07 00:16:46 -08001839 int p;
1840 unsigned long opmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 static unsigned char ModeMask[] = { 0xfc, 0xf3, 0xcf, 0x3f };
1842 int shiftbit;
1843 unsigned char val, mask;
1844
Jiri Slaby1c456072008-02-07 00:16:46 -08001845 p = tty->index % 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 if (cmd == MOXA_SET_OP_MODE) {
1847 if (get_user(opmode, (int __user *) argp))
1848 return -EFAULT;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001849 if (opmode != RS232_MODE &&
1850 opmode != RS485_2WIRE_MODE &&
1851 opmode != RS422_MODE &&
1852 opmode != RS485_4WIRE_MODE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 return -EFAULT;
Alan Cox9d6d1622008-04-30 00:53:20 -07001854 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 mask = ModeMask[p];
1856 shiftbit = p * 2;
1857 val = inb(info->opmode_ioaddr);
1858 val &= mask;
1859 val |= (opmode << shiftbit);
1860 outb(val, info->opmode_ioaddr);
Alan Cox9d6d1622008-04-30 00:53:20 -07001861 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 } else {
Alan Cox9d6d1622008-04-30 00:53:20 -07001863 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 shiftbit = p * 2;
1865 opmode = inb(info->opmode_ioaddr) >> shiftbit;
1866 opmode &= OP_MODE_MASK;
Alan Cox9d6d1622008-04-30 00:53:20 -07001867 unlock_kernel();
Jiri Slaby1c456072008-02-07 00:16:46 -08001868 if (put_user(opmode, (int __user *)argp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 return -EFAULT;
1870 }
1871 return 0;
1872 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873
Jiri Slaby1c456072008-02-07 00:16:46 -08001874 if (cmd != TIOCGSERIAL && cmd != TIOCMIWAIT && cmd != TIOCGICOUNT &&
1875 test_bit(TTY_IO_ERROR, &tty->flags))
1876 return -EIO;
1877
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 switch (cmd) {
1879 case TCSBRK: /* SVID version: non-zero arg --> no break */
1880 retval = tty_check_change(tty);
1881 if (retval)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001882 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 tty_wait_until_sent(tty, 0);
1884 if (!arg)
1885 mxser_send_break(info, HZ / 4); /* 1/4 second */
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001886 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 case TCSBRKP: /* support for POSIX tcsendbreak() */
1888 retval = tty_check_change(tty);
1889 if (retval)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001890 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 tty_wait_until_sent(tty, 0);
1892 mxser_send_break(info, arg ? arg * (HZ / 10) : HZ / 4);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001893 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 case TIOCGSERIAL:
Alan Cox9d6d1622008-04-30 00:53:20 -07001895 lock_kernel();
1896 retval = mxser_get_serial_info(info, argp);
1897 unlock_kernel();
1898 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 case TIOCSSERIAL:
Alan Cox9d6d1622008-04-30 00:53:20 -07001900 lock_kernel();
1901 retval = mxser_set_serial_info(info, argp);
1902 unlock_kernel();
1903 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 case TIOCSERGETLSR: /* Get line status register */
Alan Cox9d6d1622008-04-30 00:53:20 -07001905 return mxser_get_lsr_info(info, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 /*
1907 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1908 * - mask passed in arg for lines of interest
1909 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1910 * Caller should use TIOCGICOUNT to see which one it was
1911 */
Jiri Slabyfc838152007-04-23 14:41:04 -07001912 case TIOCMIWAIT:
1913 spin_lock_irqsave(&info->slock, flags);
1914 cnow = info->icount; /* note the counters on entry */
1915 spin_unlock_irqrestore(&info->slock, flags);
1916
Jiri Slaby1c456072008-02-07 00:16:46 -08001917 return wait_event_interruptible(info->delta_msr_wait,
1918 mxser_cflags_changed(info, arg, &cnow));
1919 /*
1920 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1921 * Return: write counters to the user passed counter struct
1922 * NB: both 1->0 and 0->1 transitions are counted except for
1923 * RI where only 0->1 is counted.
1924 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 case TIOCGICOUNT:
1926 spin_lock_irqsave(&info->slock, flags);
1927 cnow = info->icount;
1928 spin_unlock_irqrestore(&info->slock, flags);
1929 p_cuser = argp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 if (put_user(cnow.frame, &p_cuser->frame))
1931 return -EFAULT;
1932 if (put_user(cnow.brk, &p_cuser->brk))
1933 return -EFAULT;
1934 if (put_user(cnow.overrun, &p_cuser->overrun))
1935 return -EFAULT;
1936 if (put_user(cnow.buf_overrun, &p_cuser->buf_overrun))
1937 return -EFAULT;
1938 if (put_user(cnow.parity, &p_cuser->parity))
1939 return -EFAULT;
1940 if (put_user(cnow.rx, &p_cuser->rx))
1941 return -EFAULT;
1942 if (put_user(cnow.tx, &p_cuser->tx))
1943 return -EFAULT;
1944 put_user(cnow.cts, &p_cuser->cts);
1945 put_user(cnow.dsr, &p_cuser->dsr);
1946 put_user(cnow.rng, &p_cuser->rng);
1947 put_user(cnow.dcd, &p_cuser->dcd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 return 0;
1949 case MOXA_HighSpeedOn:
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001950 return put_user(info->baud_base != 115200 ? 1 : 0, (int __user *)argp);
Jiri Slaby1c456072008-02-07 00:16:46 -08001951 case MOXA_SDS_RSTICOUNTER:
Alan Cox9d6d1622008-04-30 00:53:20 -07001952 lock_kernel();
Jiri Slaby1c456072008-02-07 00:16:46 -08001953 info->mon_data.rxcnt = 0;
1954 info->mon_data.txcnt = 0;
Alan Cox9d6d1622008-04-30 00:53:20 -07001955 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 return 0;
1957
1958 case MOXA_ASPP_OQUEUE:{
Jiri Slaby1c456072008-02-07 00:16:46 -08001959 int len, lsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960
Alan Cox9d6d1622008-04-30 00:53:20 -07001961 lock_kernel();
Jiri Slaby1c456072008-02-07 00:16:46 -08001962 len = mxser_chars_in_buffer(tty);
Jiri Slaby1c456072008-02-07 00:16:46 -08001963 lsr = inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT;
Jiri Slaby1c456072008-02-07 00:16:46 -08001964 len += (lsr ? 0 : 1);
Alan Cox9d6d1622008-04-30 00:53:20 -07001965 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
Jiri Slaby1c456072008-02-07 00:16:46 -08001967 return put_user(len, (int __user *)argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 }
Jiri Slaby1c456072008-02-07 00:16:46 -08001969 case MOXA_ASPP_MON: {
1970 int mcr, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
Alan Cox9d6d1622008-04-30 00:53:20 -07001972 lock_kernel();
Jiri Slaby1c456072008-02-07 00:16:46 -08001973 status = mxser_get_msr(info->ioaddr, 1, tty->index);
1974 mxser_check_modem_status(info, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975
Jiri Slaby1c456072008-02-07 00:16:46 -08001976 mcr = inb(info->ioaddr + UART_MCR);
1977 if (mcr & MOXA_MUST_MCR_XON_FLAG)
1978 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFHOLD;
1979 else
1980 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFHOLD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981
Jiri Slaby1c456072008-02-07 00:16:46 -08001982 if (mcr & MOXA_MUST_MCR_TX_XON)
1983 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFXENT;
1984 else
1985 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFXENT;
1986
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001987 if (info->port.tty->hw_stopped)
Jiri Slaby1c456072008-02-07 00:16:46 -08001988 info->mon_data.hold_reason |= NPPI_NOTIFY_CTSHOLD;
1989 else
1990 info->mon_data.hold_reason &= ~NPPI_NOTIFY_CTSHOLD;
Alan Cox9d6d1622008-04-30 00:53:20 -07001991 unlock_kernel();
Jiri Slaby1c456072008-02-07 00:16:46 -08001992 if (copy_to_user(argp, &info->mon_data,
1993 sizeof(struct mxser_mon)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 return -EFAULT;
Jiri Slaby1c456072008-02-07 00:16:46 -08001995
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 return 0;
Jiri Slaby1c456072008-02-07 00:16:46 -08001997 }
1998 case MOXA_ASPP_LSTATUS: {
1999 if (put_user(info->err_shadow, (unsigned char __user *)argp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001
Jiri Slaby1c456072008-02-07 00:16:46 -08002002 info->err_shadow = 0;
2003 return 0;
2004 }
2005 case MOXA_SET_BAUD_METHOD: {
2006 int method;
2007
2008 if (get_user(method, (int __user *)argp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 return -EFAULT;
Jiri Slaby1c456072008-02-07 00:16:46 -08002010 mxser_set_baud_method[tty->index] = method;
2011 return put_user(method, (int __user *)argp);
2012 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 default:
2014 return -ENOIOCTLCMD;
2015 }
2016 return 0;
2017}
2018
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019static void mxser_stoprx(struct tty_struct *tty)
2020{
Jiri Slaby1c456072008-02-07 00:16:46 -08002021 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022
2023 info->ldisc_stop_rx = 1;
2024 if (I_IXOFF(tty)) {
Jiri Slaby1c456072008-02-07 00:16:46 -08002025 if (info->board->chip_flag) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 info->IER &= ~MOXA_MUST_RECV_ISR;
Jiri Slaby1c456072008-02-07 00:16:46 -08002027 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 info->x_char = STOP_CHAR(tty);
Jiri Slaby1c456072008-02-07 00:16:46 -08002030 outb(0, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 info->IER |= UART_IER_THRI;
Jiri Slaby1c456072008-02-07 00:16:46 -08002032 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 }
2034 }
2035
Alan Cox0ad9e7d2008-07-16 21:56:10 +01002036 if (info->port.tty->termios->c_cflag & CRTSCTS) {
Jiri Slaby1c456072008-02-07 00:16:46 -08002037 info->MCR &= ~UART_MCR_RTS;
2038 outb(info->MCR, info->ioaddr + UART_MCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 }
2040}
2041
2042/*
2043 * This routine is called by the upper-layer tty layer to signal that
2044 * incoming characters should be throttled.
2045 */
2046static void mxser_throttle(struct tty_struct *tty)
2047{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 mxser_stoprx(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049}
2050
2051static void mxser_unthrottle(struct tty_struct *tty)
2052{
Jiri Slaby1c456072008-02-07 00:16:46 -08002053 struct mxser_port *info = tty->driver_data;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002054
Jiri Slaby1c456072008-02-07 00:16:46 -08002055 /* startrx */
2056 info->ldisc_stop_rx = 0;
2057 if (I_IXOFF(tty)) {
2058 if (info->x_char)
2059 info->x_char = 0;
2060 else {
2061 if (info->board->chip_flag) {
2062 info->IER |= MOXA_MUST_RECV_ISR;
2063 outb(info->IER, info->ioaddr + UART_IER);
2064 } else {
2065 info->x_char = START_CHAR(tty);
2066 outb(0, info->ioaddr + UART_IER);
2067 info->IER |= UART_IER_THRI;
2068 outb(info->IER, info->ioaddr + UART_IER);
2069 }
2070 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 }
2072
Alan Cox0ad9e7d2008-07-16 21:56:10 +01002073 if (info->port.tty->termios->c_cflag & CRTSCTS) {
Jiri Slaby1c456072008-02-07 00:16:46 -08002074 info->MCR |= UART_MCR_RTS;
2075 outb(info->MCR, info->ioaddr + UART_MCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 }
2077}
2078
2079/*
2080 * mxser_stop() and mxser_start()
2081 *
2082 * This routines are called before setting or resetting tty->stopped.
2083 * They enable or disable transmitter interrupts, as necessary.
2084 */
2085static void mxser_stop(struct tty_struct *tty)
2086{
Jiri Slaby1c456072008-02-07 00:16:46 -08002087 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 unsigned long flags;
2089
2090 spin_lock_irqsave(&info->slock, flags);
2091 if (info->IER & UART_IER_THRI) {
2092 info->IER &= ~UART_IER_THRI;
Jiri Slaby1c456072008-02-07 00:16:46 -08002093 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 }
2095 spin_unlock_irqrestore(&info->slock, flags);
2096}
2097
2098static void mxser_start(struct tty_struct *tty)
2099{
Jiri Slaby1c456072008-02-07 00:16:46 -08002100 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 unsigned long flags;
2102
2103 spin_lock_irqsave(&info->slock, flags);
Alan Cox0ad9e7d2008-07-16 21:56:10 +01002104 if (info->xmit_cnt && info->port.xmit_buf) {
Jiri Slaby1c456072008-02-07 00:16:46 -08002105 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 info->IER |= UART_IER_THRI;
Jiri Slaby1c456072008-02-07 00:16:46 -08002107 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 }
2109 spin_unlock_irqrestore(&info->slock, flags);
2110}
2111
Jiri Slaby1c456072008-02-07 00:16:46 -08002112static void mxser_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
2113{
2114 struct mxser_port *info = tty->driver_data;
2115 unsigned long flags;
2116
2117 spin_lock_irqsave(&info->slock, flags);
2118 mxser_change_speed(info, old_termios);
2119 spin_unlock_irqrestore(&info->slock, flags);
2120
2121 if ((old_termios->c_cflag & CRTSCTS) &&
2122 !(tty->termios->c_cflag & CRTSCTS)) {
2123 tty->hw_stopped = 0;
2124 mxser_start(tty);
2125 }
2126
2127 /* Handle sw stopped */
2128 if ((old_termios->c_iflag & IXON) &&
2129 !(tty->termios->c_iflag & IXON)) {
2130 tty->stopped = 0;
2131
2132 if (info->board->chip_flag) {
2133 spin_lock_irqsave(&info->slock, flags);
Christoph Hellwig148ff862008-04-30 00:54:29 -07002134 mxser_disable_must_rx_software_flow_control(
2135 info->ioaddr);
Jiri Slaby1c456072008-02-07 00:16:46 -08002136 spin_unlock_irqrestore(&info->slock, flags);
2137 }
2138
2139 mxser_start(tty);
2140 }
2141}
2142
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143/*
2144 * mxser_wait_until_sent() --- wait until the transmitter is empty
2145 */
2146static void mxser_wait_until_sent(struct tty_struct *tty, int timeout)
2147{
Jiri Slaby1c456072008-02-07 00:16:46 -08002148 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 unsigned long orig_jiffies, char_time;
2150 int lsr;
2151
2152 if (info->type == PORT_UNKNOWN)
2153 return;
2154
2155 if (info->xmit_fifo_size == 0)
2156 return; /* Just in case.... */
2157
2158 orig_jiffies = jiffies;
2159 /*
2160 * Set the check interval to be 1/5 of the estimated time to
2161 * send a single character, and make it at least 1. The check
2162 * interval should also be less than the timeout.
2163 *
2164 * Note: we have to use pretty tight timings here to satisfy
2165 * the NIST-PCTS.
2166 */
2167 char_time = (info->timeout - HZ / 50) / info->xmit_fifo_size;
2168 char_time = char_time / 5;
2169 if (char_time == 0)
2170 char_time = 1;
2171 if (timeout && timeout < char_time)
2172 char_time = timeout;
2173 /*
2174 * If the transmitter hasn't cleared in twice the approximate
2175 * amount of time to send the entire FIFO, it probably won't
2176 * ever clear. This assumes the UART isn't doing flow
2177 * control, which is currently the case. Hence, if it ever
2178 * takes longer than info->timeout, this is probably due to a
2179 * UART bug of some kind. So, we clamp the timeout parameter at
2180 * 2*info->timeout.
2181 */
2182 if (!timeout || timeout > 2 * info->timeout)
2183 timeout = 2 * info->timeout;
2184#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002185 printk(KERN_DEBUG "In rs_wait_until_sent(%d) check=%lu...",
2186 timeout, char_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 printk("jiff=%lu...", jiffies);
2188#endif
Alan Cox978e5952008-04-30 00:53:59 -07002189 lock_kernel();
Jiri Slaby1c456072008-02-07 00:16:46 -08002190 while (!((lsr = inb(info->ioaddr + UART_LSR)) & UART_LSR_TEMT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2192 printk("lsr = %d (jiff=%lu)...", lsr, jiffies);
2193#endif
Nishanth Aravamudanda4cd8d2005-09-10 00:27:30 -07002194 schedule_timeout_interruptible(char_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 if (signal_pending(current))
2196 break;
2197 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2198 break;
2199 }
2200 set_current_state(TASK_RUNNING);
Alan Cox978e5952008-04-30 00:53:59 -07002201 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202
2203#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2204 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
2205#endif
2206}
2207
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208/*
2209 * This routine is called by tty_hangup() when a hangup is signaled.
2210 */
Jiri Slaby1c456072008-02-07 00:16:46 -08002211static void mxser_hangup(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212{
Jiri Slaby1c456072008-02-07 00:16:46 -08002213 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214
2215 mxser_flush_buffer(tty);
2216 mxser_shutdown(info);
2217 info->event = 0;
Alan Cox0ad9e7d2008-07-16 21:56:10 +01002218 info->port.count = 0;
2219 info->port.flags &= ~ASYNC_NORMAL_ACTIVE;
2220 info->port.tty = NULL;
2221 wake_up_interruptible(&info->port.open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222}
2223
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224/*
2225 * mxser_rs_break() --- routine which turns the break handling on or off
2226 */
2227static void mxser_rs_break(struct tty_struct *tty, int break_state)
2228{
Jiri Slaby1c456072008-02-07 00:16:46 -08002229 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 unsigned long flags;
2231
2232 spin_lock_irqsave(&info->slock, flags);
2233 if (break_state == -1)
Jiri Slaby1c456072008-02-07 00:16:46 -08002234 outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
2235 info->ioaddr + UART_LCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 else
Jiri Slaby1c456072008-02-07 00:16:46 -08002237 outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
2238 info->ioaddr + UART_LCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 spin_unlock_irqrestore(&info->slock, flags);
2240}
2241
Jiri Slaby1c456072008-02-07 00:16:46 -08002242static void mxser_receive_chars(struct mxser_port *port, int *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243{
Alan Cox0ad9e7d2008-07-16 21:56:10 +01002244 struct tty_struct *tty = port->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 unsigned char ch, gdl;
2246 int ignored = 0;
2247 int cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 int recv_room;
2249 int max = 256;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250
Alan Cox33f0f882006-01-09 20:54:13 -08002251 recv_room = tty->receive_room;
Jiri Slaby1c456072008-02-07 00:16:46 -08002252 if ((recv_room == 0) && (!port->ldisc_stop_rx))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 mxser_stoprx(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254
Jiri Slaby1c456072008-02-07 00:16:46 -08002255 if (port->board->chip_flag != MOXA_OTHER_UART) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256
Jiri Slaby1c456072008-02-07 00:16:46 -08002257 if (*status & UART_LSR_SPECIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 goto intr_old;
Jiri Slaby1c456072008-02-07 00:16:46 -08002259 if (port->board->chip_flag == MOXA_MUST_MU860_HWID &&
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002260 (*status & MOXA_MUST_LSR_RERR))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 goto intr_old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 if (*status & MOXA_MUST_LSR_RERR)
2263 goto intr_old;
2264
Jiri Slaby1c456072008-02-07 00:16:46 -08002265 gdl = inb(port->ioaddr + MOXA_MUST_GDL_REGISTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266
Jiri Slaby1c456072008-02-07 00:16:46 -08002267 if (port->board->chip_flag == MOXA_MUST_MU150_HWID)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 gdl &= MOXA_MUST_GDL_MASK;
2269 if (gdl >= recv_room) {
Jiri Slaby1c456072008-02-07 00:16:46 -08002270 if (!port->ldisc_stop_rx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 mxser_stoprx(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 }
2273 while (gdl--) {
Jiri Slaby1c456072008-02-07 00:16:46 -08002274 ch = inb(port->ioaddr + UART_RX);
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002275 tty_insert_flip_char(tty, ch, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 }
2278 goto end_intr;
2279 }
Jiri Slaby1c456072008-02-07 00:16:46 -08002280intr_old:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281
2282 do {
2283 if (max-- < 0)
2284 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285
Jiri Slaby1c456072008-02-07 00:16:46 -08002286 ch = inb(port->ioaddr + UART_RX);
2287 if (port->board->chip_flag && (*status & UART_LSR_OE))
2288 outb(0x23, port->ioaddr + UART_FCR);
2289 *status &= port->read_status_mask;
2290 if (*status & port->ignore_status_mask) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 if (++ignored > 100)
2292 break;
2293 } else {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002294 char flag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 if (*status & UART_LSR_SPECIAL) {
2296 if (*status & UART_LSR_BI) {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002297 flag = TTY_BREAK;
Jiri Slaby1c456072008-02-07 00:16:46 -08002298 port->icount.brk++;
2299
Alan Cox0ad9e7d2008-07-16 21:56:10 +01002300 if (port->port.flags & ASYNC_SAK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 do_SAK(tty);
2302 } else if (*status & UART_LSR_PE) {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002303 flag = TTY_PARITY;
Jiri Slaby1c456072008-02-07 00:16:46 -08002304 port->icount.parity++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 } else if (*status & UART_LSR_FE) {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002306 flag = TTY_FRAME;
Jiri Slaby1c456072008-02-07 00:16:46 -08002307 port->icount.frame++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 } else if (*status & UART_LSR_OE) {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002309 flag = TTY_OVERRUN;
Jiri Slaby1c456072008-02-07 00:16:46 -08002310 port->icount.overrun++;
2311 } else
2312 flag = TTY_BREAK;
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002313 }
2314 tty_insert_flip_char(tty, ch, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 cnt++;
2316 if (cnt >= recv_room) {
Jiri Slaby1c456072008-02-07 00:16:46 -08002317 if (!port->ldisc_stop_rx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 mxser_stoprx(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 break;
2320 }
2321
2322 }
2323
Jiri Slaby1c456072008-02-07 00:16:46 -08002324 if (port->board->chip_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326
Jiri Slaby1c456072008-02-07 00:16:46 -08002327 *status = inb(port->ioaddr + UART_LSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 } while (*status & UART_LSR_DR);
2329
Jiri Slaby1c456072008-02-07 00:16:46 -08002330end_intr:
Alan Cox0ad9e7d2008-07-16 21:56:10 +01002331 mxvar_log.rxcnt[port->port.tty->index] += cnt;
Jiri Slaby1c456072008-02-07 00:16:46 -08002332 port->mon_data.rxcnt += cnt;
2333 port->mon_data.up_rxcnt += cnt;
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002334
Jiri Slaby1c456072008-02-07 00:16:46 -08002335 /*
2336 * We are called from an interrupt context with &port->slock
2337 * being held. Drop it temporarily in order to prevent
2338 * recursive locking.
2339 */
2340 spin_unlock(&port->slock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 tty_flip_buffer_push(tty);
Jiri Slaby1c456072008-02-07 00:16:46 -08002342 spin_lock(&port->slock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343}
2344
Jiri Slaby1c456072008-02-07 00:16:46 -08002345static void mxser_transmit_chars(struct mxser_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346{
2347 int count, cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348
Jiri Slaby1c456072008-02-07 00:16:46 -08002349 if (port->x_char) {
2350 outb(port->x_char, port->ioaddr + UART_TX);
2351 port->x_char = 0;
Alan Cox0ad9e7d2008-07-16 21:56:10 +01002352 mxvar_log.txcnt[port->port.tty->index]++;
Jiri Slaby1c456072008-02-07 00:16:46 -08002353 port->mon_data.txcnt++;
2354 port->mon_data.up_txcnt++;
2355 port->icount.tx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 return;
2357 }
2358
Alan Cox0ad9e7d2008-07-16 21:56:10 +01002359 if (port->port.xmit_buf == NULL)
Jiri Slaby1c456072008-02-07 00:16:46 -08002360 return;
2361
Alan Cox0ad9e7d2008-07-16 21:56:10 +01002362 if ((port->xmit_cnt <= 0) || port->port.tty->stopped ||
2363 (port->port.tty->hw_stopped &&
Jiri Slaby1c456072008-02-07 00:16:46 -08002364 (port->type != PORT_16550A) &&
2365 (!port->board->chip_flag))) {
2366 port->IER &= ~UART_IER_THRI;
2367 outb(port->IER, port->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 return;
2369 }
2370
Jiri Slaby1c456072008-02-07 00:16:46 -08002371 cnt = port->xmit_cnt;
2372 count = port->xmit_fifo_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 do {
Alan Cox0ad9e7d2008-07-16 21:56:10 +01002374 outb(port->port.xmit_buf[port->xmit_tail++],
Jiri Slaby1c456072008-02-07 00:16:46 -08002375 port->ioaddr + UART_TX);
2376 port->xmit_tail = port->xmit_tail & (SERIAL_XMIT_SIZE - 1);
2377 if (--port->xmit_cnt <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 break;
2379 } while (--count > 0);
Alan Cox0ad9e7d2008-07-16 21:56:10 +01002380 mxvar_log.txcnt[port->port.tty->index] += (cnt - port->xmit_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381
Jiri Slaby1c456072008-02-07 00:16:46 -08002382 port->mon_data.txcnt += (cnt - port->xmit_cnt);
2383 port->mon_data.up_txcnt += (cnt - port->xmit_cnt);
2384 port->icount.tx += (cnt - port->xmit_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385
Jiri Slaby1c456072008-02-07 00:16:46 -08002386 if (port->xmit_cnt < WAKEUP_CHARS)
Alan Cox0ad9e7d2008-07-16 21:56:10 +01002387 tty_wakeup(port->port.tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388
Jiri Slaby1c456072008-02-07 00:16:46 -08002389 if (port->xmit_cnt <= 0) {
2390 port->IER &= ~UART_IER_THRI;
2391 outb(port->IER, port->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393}
2394
2395/*
Jiri Slaby1c456072008-02-07 00:16:46 -08002396 * This is the serial driver's generic interrupt routine
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 */
Jiri Slaby1c456072008-02-07 00:16:46 -08002398static irqreturn_t mxser_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399{
Jiri Slaby1c456072008-02-07 00:16:46 -08002400 int status, iir, i;
2401 struct mxser_board *brd = NULL;
2402 struct mxser_port *port;
2403 int max, irqbits, bits, msr;
2404 unsigned int int_cnt, pass_counter = 0;
2405 int handled = IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406
Jiri Slaby1c456072008-02-07 00:16:46 -08002407 for (i = 0; i < MXSER_BOARDS; i++)
2408 if (dev_id == &mxser_boards[i]) {
2409 brd = dev_id;
2410 break;
2411 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412
Jiri Slaby1c456072008-02-07 00:16:46 -08002413 if (i == MXSER_BOARDS)
2414 goto irq_stop;
2415 if (brd == NULL)
2416 goto irq_stop;
2417 max = brd->info->nports;
2418 while (pass_counter++ < MXSER_ISR_PASS_LIMIT) {
2419 irqbits = inb(brd->vector) & brd->vector_mask;
2420 if (irqbits == brd->vector_mask)
2421 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422
Jiri Slaby1c456072008-02-07 00:16:46 -08002423 handled = IRQ_HANDLED;
2424 for (i = 0, bits = 1; i < max; i++, irqbits |= bits, bits <<= 1) {
2425 if (irqbits == brd->vector_mask)
2426 break;
2427 if (bits & irqbits)
2428 continue;
2429 port = &brd->ports[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430
Jiri Slaby1c456072008-02-07 00:16:46 -08002431 int_cnt = 0;
2432 spin_lock(&port->slock);
2433 do {
2434 iir = inb(port->ioaddr + UART_IIR);
2435 if (iir & UART_IIR_NO_INT)
2436 break;
2437 iir &= MOXA_MUST_IIR_MASK;
Alan Cox0ad9e7d2008-07-16 21:56:10 +01002438 if (!port->port.tty ||
2439 (port->port.flags & ASYNC_CLOSING) ||
2440 !(port->port.flags &
Jiri Slaby1c456072008-02-07 00:16:46 -08002441 ASYNC_INITIALIZED)) {
2442 status = inb(port->ioaddr + UART_LSR);
2443 outb(0x27, port->ioaddr + UART_FCR);
2444 inb(port->ioaddr + UART_MSR);
2445 break;
2446 }
2447
2448 status = inb(port->ioaddr + UART_LSR);
2449
2450 if (status & UART_LSR_PE)
2451 port->err_shadow |= NPPI_NOTIFY_PARITY;
2452 if (status & UART_LSR_FE)
2453 port->err_shadow |= NPPI_NOTIFY_FRAMING;
2454 if (status & UART_LSR_OE)
2455 port->err_shadow |=
2456 NPPI_NOTIFY_HW_OVERRUN;
2457 if (status & UART_LSR_BI)
2458 port->err_shadow |= NPPI_NOTIFY_BREAK;
2459
2460 if (port->board->chip_flag) {
2461 if (iir == MOXA_MUST_IIR_GDA ||
2462 iir == MOXA_MUST_IIR_RDA ||
2463 iir == MOXA_MUST_IIR_RTO ||
2464 iir == MOXA_MUST_IIR_LSR)
2465 mxser_receive_chars(port,
2466 &status);
2467
2468 } else {
2469 status &= port->read_status_mask;
2470 if (status & UART_LSR_DR)
2471 mxser_receive_chars(port,
2472 &status);
2473 }
2474 msr = inb(port->ioaddr + UART_MSR);
2475 if (msr & UART_MSR_ANY_DELTA)
2476 mxser_check_modem_status(port, msr);
2477
2478 if (port->board->chip_flag) {
2479 if (iir == 0x02 && (status &
2480 UART_LSR_THRE))
2481 mxser_transmit_chars(port);
2482 } else {
2483 if (status & UART_LSR_THRE)
2484 mxser_transmit_chars(port);
2485 }
2486 } while (int_cnt++ < MXSER_ISR_PASS_LIMIT);
2487 spin_unlock(&port->slock);
2488 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 }
2490
Jiri Slaby1c456072008-02-07 00:16:46 -08002491irq_stop:
2492 return handled;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493}
2494
Jiri Slaby1c456072008-02-07 00:16:46 -08002495static const struct tty_operations mxser_ops = {
2496 .open = mxser_open,
2497 .close = mxser_close,
2498 .write = mxser_write,
2499 .put_char = mxser_put_char,
2500 .flush_chars = mxser_flush_chars,
2501 .write_room = mxser_write_room,
2502 .chars_in_buffer = mxser_chars_in_buffer,
2503 .flush_buffer = mxser_flush_buffer,
2504 .ioctl = mxser_ioctl,
2505 .throttle = mxser_throttle,
2506 .unthrottle = mxser_unthrottle,
2507 .set_termios = mxser_set_termios,
2508 .stop = mxser_stop,
2509 .start = mxser_start,
2510 .hangup = mxser_hangup,
2511 .break_ctl = mxser_rs_break,
2512 .wait_until_sent = mxser_wait_until_sent,
2513 .tiocmget = mxser_tiocmget,
2514 .tiocmset = mxser_tiocmset,
2515};
2516
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517/*
Jiri Slaby1c456072008-02-07 00:16:46 -08002518 * The MOXA Smartio/Industio serial driver boot-time initialization code!
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 */
Jiri Slaby1c456072008-02-07 00:16:46 -08002520
2521static void mxser_release_res(struct mxser_board *brd, struct pci_dev *pdev,
2522 unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523{
Jiri Slaby1c456072008-02-07 00:16:46 -08002524 if (irq)
2525 free_irq(brd->irq, brd);
2526 if (pdev != NULL) { /* PCI */
2527#ifdef CONFIG_PCI
2528 pci_release_region(pdev, 2);
2529 pci_release_region(pdev, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 } else {
Jiri Slaby1c456072008-02-07 00:16:46 -08002532 release_region(brd->ports[0].ioaddr, 8 * brd->info->nports);
2533 release_region(brd->vector, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535}
2536
Jiri Slaby1c456072008-02-07 00:16:46 -08002537static int __devinit mxser_initbrd(struct mxser_board *brd,
2538 struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539{
Jiri Slaby1c456072008-02-07 00:16:46 -08002540 struct mxser_port *info;
2541 unsigned int i;
2542 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543
Jiri Slaby1c456072008-02-07 00:16:46 -08002544 printk(KERN_INFO "max. baud rate = %d bps.\n", brd->ports[0].max_baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545
Jiri Slaby1c456072008-02-07 00:16:46 -08002546 for (i = 0; i < brd->info->nports; i++) {
2547 info = &brd->ports[i];
2548 info->board = brd;
2549 info->stop_rx = 0;
2550 info->ldisc_stop_rx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551
Jiri Slaby1c456072008-02-07 00:16:46 -08002552 /* Enhance mode enabled here */
2553 if (brd->chip_flag != MOXA_OTHER_UART)
Christoph Hellwig148ff862008-04-30 00:54:29 -07002554 mxser_enable_must_enchance_mode(info->ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555
Alan Cox0ad9e7d2008-07-16 21:56:10 +01002556 info->port.flags = ASYNC_SHARE_IRQ;
Jiri Slaby1c456072008-02-07 00:16:46 -08002557 info->type = brd->uart_type;
2558
2559 process_txrx_fifo(info);
2560
2561 info->custom_divisor = info->baud_base * 16;
2562 info->close_delay = 5 * HZ / 10;
2563 info->closing_wait = 30 * HZ;
2564 info->normal_termios = mxvar_sdriver->init_termios;
Alan Cox0ad9e7d2008-07-16 21:56:10 +01002565 tty_port_init(&info->port);
Jiri Slaby1c456072008-02-07 00:16:46 -08002566 init_waitqueue_head(&info->delta_msr_wait);
2567 memset(&info->mon_data, 0, sizeof(struct mxser_mon));
2568 info->err_shadow = 0;
2569 spin_lock_init(&info->slock);
2570
2571 /* before set INT ISR, disable all int */
2572 outb(inb(info->ioaddr + UART_IER) & 0xf0,
2573 info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 }
2575
Jiri Slaby1c456072008-02-07 00:16:46 -08002576 retval = request_irq(brd->irq, mxser_interrupt, IRQF_SHARED, "mxser",
2577 brd);
2578 if (retval) {
2579 printk(KERN_ERR "Board %s: Request irq failed, IRQ (%d) may "
2580 "conflict with another device.\n",
2581 brd->info->name, brd->irq);
2582 /* We hold resources, we need to release them. */
2583 mxser_release_res(brd, pdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002585 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586}
2587
Jiri Slaby1c456072008-02-07 00:16:46 -08002588static int __init mxser_get_ISA_conf(int cap, struct mxser_board *brd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589{
2590 int id, i, bits;
2591 unsigned short regs[16], irq;
2592 unsigned char scratch, scratch2;
2593
Jiri Slaby1c456072008-02-07 00:16:46 -08002594 brd->chip_flag = MOXA_OTHER_UART;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595
2596 id = mxser_read_register(cap, regs);
Jiri Slaby1c456072008-02-07 00:16:46 -08002597 switch (id) {
2598 case C168_ASIC_ID:
2599 brd->info = &mxser_cards[0];
2600 break;
2601 case C104_ASIC_ID:
2602 brd->info = &mxser_cards[1];
2603 break;
2604 case CI104J_ASIC_ID:
2605 brd->info = &mxser_cards[2];
2606 break;
2607 case C102_ASIC_ID:
2608 brd->info = &mxser_cards[5];
2609 break;
2610 case CI132_ASIC_ID:
2611 brd->info = &mxser_cards[6];
2612 break;
2613 case CI134_ASIC_ID:
2614 brd->info = &mxser_cards[7];
2615 break;
2616 default:
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002617 return 0;
Jiri Slaby1c456072008-02-07 00:16:46 -08002618 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619
2620 irq = 0;
Jiri Slaby1c456072008-02-07 00:16:46 -08002621 /* some ISA cards have 2 ports, but we want to see them as 4-port (why?)
2622 Flag-hack checks if configuration should be read as 2-port here. */
2623 if (brd->info->nports == 2 || (brd->info->flags & MXSER_HAS2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 irq = regs[9] & 0xF000;
2625 irq = irq | (irq >> 4);
2626 if (irq != (regs[9] & 0xFF00))
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002627 return MXSER_ERR_IRQ_CONFLIT;
Jiri Slaby1c456072008-02-07 00:16:46 -08002628 } else if (brd->info->nports == 4) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 irq = regs[9] & 0xF000;
2630 irq = irq | (irq >> 4);
2631 irq = irq | (irq >> 8);
2632 if (irq != regs[9])
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002633 return MXSER_ERR_IRQ_CONFLIT;
Jiri Slaby1c456072008-02-07 00:16:46 -08002634 } else if (brd->info->nports == 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 irq = regs[9] & 0xF000;
2636 irq = irq | (irq >> 4);
2637 irq = irq | (irq >> 8);
2638 if ((irq != regs[9]) || (irq != regs[10]))
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002639 return MXSER_ERR_IRQ_CONFLIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 }
2641
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002642 if (!irq)
2643 return MXSER_ERR_IRQ;
Jiri Slaby1c456072008-02-07 00:16:46 -08002644 brd->irq = ((int)(irq & 0xF000) >> 12);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 for (i = 0; i < 8; i++)
Jiri Slaby1c456072008-02-07 00:16:46 -08002646 brd->ports[i].ioaddr = (int) regs[i + 1] & 0xFFF8;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002647 if ((regs[12] & 0x80) == 0)
2648 return MXSER_ERR_VECTOR;
Jiri Slaby1c456072008-02-07 00:16:46 -08002649 brd->vector = (int)regs[11]; /* interrupt vector */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 if (id == 1)
Jiri Slaby1c456072008-02-07 00:16:46 -08002651 brd->vector_mask = 0x00FF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 else
Jiri Slaby1c456072008-02-07 00:16:46 -08002653 brd->vector_mask = 0x000F;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 for (i = 7, bits = 0x0100; i >= 0; i--, bits <<= 1) {
2655 if (regs[12] & bits) {
Jiri Slaby1c456072008-02-07 00:16:46 -08002656 brd->ports[i].baud_base = 921600;
2657 brd->ports[i].max_baud = 921600;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 } else {
Jiri Slaby1c456072008-02-07 00:16:46 -08002659 brd->ports[i].baud_base = 115200;
2660 brd->ports[i].max_baud = 115200;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 }
2662 }
2663 scratch2 = inb(cap + UART_LCR) & (~UART_LCR_DLAB);
2664 outb(scratch2 | UART_LCR_DLAB, cap + UART_LCR);
2665 outb(0, cap + UART_EFR); /* EFR is the same as FCR */
2666 outb(scratch2, cap + UART_LCR);
2667 outb(UART_FCR_ENABLE_FIFO, cap + UART_FCR);
2668 scratch = inb(cap + UART_IIR);
2669
2670 if (scratch & 0xC0)
Jiri Slaby1c456072008-02-07 00:16:46 -08002671 brd->uart_type = PORT_16550A;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 else
Jiri Slaby1c456072008-02-07 00:16:46 -08002673 brd->uart_type = PORT_16450;
2674 if (!request_region(brd->ports[0].ioaddr, 8 * brd->info->nports,
2675 "mxser(IO)"))
2676 return MXSER_ERR_IOADDR;
2677 if (!request_region(brd->vector, 1, "mxser(vector)")) {
2678 release_region(brd->ports[0].ioaddr, 8 * brd->info->nports);
2679 return MXSER_ERR_VECTOR;
2680 }
2681 return brd->info->nports;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682}
2683
Jiri Slaby1c456072008-02-07 00:16:46 -08002684static int __devinit mxser_probe(struct pci_dev *pdev,
2685 const struct pci_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686{
Jiri Slaby1c456072008-02-07 00:16:46 -08002687#ifdef CONFIG_PCI
2688 struct mxser_board *brd;
2689 unsigned int i, j;
2690 unsigned long ioaddress;
2691 int retval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692
Jiri Slaby1c456072008-02-07 00:16:46 -08002693 for (i = 0; i < MXSER_BOARDS; i++)
2694 if (mxser_boards[i].info == NULL)
2695 break;
2696
2697 if (i >= MXSER_BOARDS) {
2698 printk(KERN_ERR "Too many Smartio/Industio family boards found "
2699 "(maximum %d), board not configured\n", MXSER_BOARDS);
2700 goto err;
2701 }
2702
2703 brd = &mxser_boards[i];
2704 brd->idx = i * MXSER_PORTS_PER_BOARD;
2705 printk(KERN_INFO "Found MOXA %s board (BusNo=%d, DevNo=%d)\n",
2706 mxser_cards[ent->driver_data].name,
2707 pdev->bus->number, PCI_SLOT(pdev->devfn));
2708
2709 retval = pci_enable_device(pdev);
2710 if (retval) {
2711 printk(KERN_ERR "Moxa SmartI/O PCI enable fail !\n");
2712 goto err;
2713 }
2714
2715 /* io address */
2716 ioaddress = pci_resource_start(pdev, 2);
2717 retval = pci_request_region(pdev, 2, "mxser(IO)");
2718 if (retval)
2719 goto err;
2720
2721 brd->info = &mxser_cards[ent->driver_data];
2722 for (i = 0; i < brd->info->nports; i++)
2723 brd->ports[i].ioaddr = ioaddress + 8 * i;
2724
2725 /* vector */
2726 ioaddress = pci_resource_start(pdev, 3);
2727 retval = pci_request_region(pdev, 3, "mxser(vector)");
2728 if (retval)
2729 goto err_relio;
2730 brd->vector = ioaddress;
2731
2732 /* irq */
2733 brd->irq = pdev->irq;
2734
2735 brd->chip_flag = CheckIsMoxaMust(brd->ports[0].ioaddr);
2736 brd->uart_type = PORT_16550A;
2737 brd->vector_mask = 0;
2738
2739 for (i = 0; i < brd->info->nports; i++) {
2740 for (j = 0; j < UART_INFO_NUM; j++) {
2741 if (Gpci_uart_info[j].type == brd->chip_flag) {
2742 brd->ports[i].max_baud =
2743 Gpci_uart_info[j].max_baud;
2744
2745 /* exception....CP-102 */
2746 if (brd->info->flags & MXSER_HIGHBAUD)
2747 brd->ports[i].max_baud = 921600;
2748 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 }
2750 }
Jiri Slaby1c456072008-02-07 00:16:46 -08002751 }
2752
2753 if (brd->chip_flag == MOXA_MUST_MU860_HWID) {
2754 for (i = 0; i < brd->info->nports; i++) {
2755 if (i < 4)
2756 brd->ports[i].opmode_ioaddr = ioaddress + 4;
2757 else
2758 brd->ports[i].opmode_ioaddr = ioaddress + 0x0c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 }
Jiri Slaby1c456072008-02-07 00:16:46 -08002760 outb(0, ioaddress + 4); /* default set to RS232 mode */
2761 outb(0, ioaddress + 0x0c); /* default set to RS232 mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 }
Jiri Slaby1c456072008-02-07 00:16:46 -08002763
2764 for (i = 0; i < brd->info->nports; i++) {
2765 brd->vector_mask |= (1 << i);
2766 brd->ports[i].baud_base = 921600;
2767 }
2768
2769 /* mxser_initbrd will hook ISR. */
2770 retval = mxser_initbrd(brd, pdev);
2771 if (retval)
2772 goto err_null;
2773
2774 for (i = 0; i < brd->info->nports; i++)
2775 tty_register_device(mxvar_sdriver, brd->idx + i, &pdev->dev);
2776
2777 pci_set_drvdata(pdev, brd);
2778
2779 return 0;
2780err_relio:
2781 pci_release_region(pdev, 2);
2782err_null:
2783 brd->info = NULL;
2784err:
2785 return retval;
2786#else
2787 return -ENODEV;
2788#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789}
2790
Jiri Slaby1c456072008-02-07 00:16:46 -08002791static void __devexit mxser_remove(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792{
Jiri Slaby1c456072008-02-07 00:16:46 -08002793 struct mxser_board *brd = pci_get_drvdata(pdev);
2794 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795
Jiri Slaby1c456072008-02-07 00:16:46 -08002796 for (i = 0; i < brd->info->nports; i++)
2797 tty_unregister_device(mxvar_sdriver, brd->idx + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798
Jiri Slaby1c456072008-02-07 00:16:46 -08002799 mxser_release_res(brd, pdev, 1);
2800 brd->info = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801}
2802
Jiri Slaby1c456072008-02-07 00:16:46 -08002803static struct pci_driver mxser_driver = {
2804 .name = "mxser",
2805 .id_table = mxser_pcibrds,
2806 .probe = mxser_probe,
2807 .remove = __devexit_p(mxser_remove)
2808};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809
Jiri Slaby1c456072008-02-07 00:16:46 -08002810static int __init mxser_module_init(void)
2811{
2812 struct mxser_board *brd;
2813 unsigned long cap;
2814 unsigned int i, m, isaloop;
2815 int retval, b;
2816
2817 pr_debug("Loading module mxser ...\n");
2818
2819 mxvar_sdriver = alloc_tty_driver(MXSER_PORTS + 1);
2820 if (!mxvar_sdriver)
2821 return -ENOMEM;
2822
2823 printk(KERN_INFO "MOXA Smartio/Industio family driver version %s\n",
2824 MXSER_VERSION);
2825
2826 /* Initialize the tty_driver structure */
2827 mxvar_sdriver->owner = THIS_MODULE;
2828 mxvar_sdriver->magic = TTY_DRIVER_MAGIC;
2829 mxvar_sdriver->name = "ttyMI";
2830 mxvar_sdriver->major = ttymajor;
2831 mxvar_sdriver->minor_start = 0;
2832 mxvar_sdriver->num = MXSER_PORTS + 1;
2833 mxvar_sdriver->type = TTY_DRIVER_TYPE_SERIAL;
2834 mxvar_sdriver->subtype = SERIAL_TYPE_NORMAL;
2835 mxvar_sdriver->init_termios = tty_std_termios;
2836 mxvar_sdriver->init_termios.c_cflag = B9600|CS8|CREAD|HUPCL|CLOCAL;
2837 mxvar_sdriver->flags = TTY_DRIVER_REAL_RAW|TTY_DRIVER_DYNAMIC_DEV;
2838 tty_set_operations(mxvar_sdriver, &mxser_ops);
2839
2840 retval = tty_register_driver(mxvar_sdriver);
2841 if (retval) {
2842 printk(KERN_ERR "Couldn't install MOXA Smartio/Industio family "
2843 "tty driver !\n");
2844 goto err_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 }
Jiri Slaby1c456072008-02-07 00:16:46 -08002846
2847 mxvar_diagflag = 0;
2848
2849 m = 0;
2850 /* Start finding ISA boards here */
2851 for (isaloop = 0; isaloop < 2; isaloop++)
2852 for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
2853 if (!isaloop)
2854 cap = mxserBoardCAP[b]; /* predefined */
2855 else
2856 cap = ioaddr[b]; /* module param */
2857
2858 if (!cap)
2859 continue;
2860
2861 brd = &mxser_boards[m];
2862 retval = mxser_get_ISA_conf(cap, brd);
2863
2864 if (retval != 0)
2865 printk(KERN_INFO "Found MOXA %s board "
2866 "(CAP=0x%x)\n",
2867 brd->info->name, ioaddr[b]);
2868
2869 if (retval <= 0) {
2870 if (retval == MXSER_ERR_IRQ)
2871 printk(KERN_ERR "Invalid interrupt "
2872 "number, board not "
2873 "configured\n");
2874 else if (retval == MXSER_ERR_IRQ_CONFLIT)
2875 printk(KERN_ERR "Invalid interrupt "
2876 "number, board not "
2877 "configured\n");
2878 else if (retval == MXSER_ERR_VECTOR)
2879 printk(KERN_ERR "Invalid interrupt "
2880 "vector, board not "
2881 "configured\n");
2882 else if (retval == MXSER_ERR_IOADDR)
2883 printk(KERN_ERR "Invalid I/O address, "
2884 "board not configured\n");
2885
2886 brd->info = NULL;
2887 continue;
2888 }
2889
2890 /* mxser_initbrd will hook ISR. */
2891 if (mxser_initbrd(brd, NULL) < 0) {
2892 brd->info = NULL;
2893 continue;
2894 }
2895
2896 brd->idx = m * MXSER_PORTS_PER_BOARD;
2897 for (i = 0; i < brd->info->nports; i++)
2898 tty_register_device(mxvar_sdriver, brd->idx + i,
2899 NULL);
2900
2901 m++;
2902 }
2903
2904 retval = pci_register_driver(&mxser_driver);
2905 if (retval) {
2906 printk(KERN_ERR "Can't register pci driver\n");
2907 if (!m) {
2908 retval = -ENODEV;
2909 goto err_unr;
2910 } /* else: we have some ISA cards under control */
2911 }
2912
2913 pr_debug("Done.\n");
2914
2915 return 0;
2916err_unr:
2917 tty_unregister_driver(mxvar_sdriver);
2918err_put:
2919 put_tty_driver(mxvar_sdriver);
2920 return retval;
2921}
2922
2923static void __exit mxser_module_exit(void)
2924{
2925 unsigned int i, j;
2926
2927 pr_debug("Unloading module mxser ...\n");
2928
2929 pci_unregister_driver(&mxser_driver);
2930
2931 for (i = 0; i < MXSER_BOARDS; i++) /* ISA remains */
2932 if (mxser_boards[i].info != NULL)
2933 for (j = 0; j < mxser_boards[i].info->nports; j++)
2934 tty_unregister_device(mxvar_sdriver,
2935 mxser_boards[i].idx + j);
2936 tty_unregister_driver(mxvar_sdriver);
2937 put_tty_driver(mxvar_sdriver);
2938
2939 for (i = 0; i < MXSER_BOARDS; i++)
2940 if (mxser_boards[i].info != NULL)
2941 mxser_release_res(&mxser_boards[i], NULL, 1);
2942
2943 pr_debug("Done.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944}
2945
2946module_init(mxser_module_init);
2947module_exit(mxser_module_exit);