blob: 3d7f2a970495fd5a589e3ef4716f4a861335fe16 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * mxser.c -- MOXA Smartio/Industio family multiport serial driver.
3 *
Jiri Slaby80ff8a82008-02-07 00:16:51 -08004 * Copyright (C) 1999-2006 Moxa Technologies (support@moxa.com).
5 * Copyright (C) 2006-2008 Jiri Slaby <jirislaby@gmail.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
Jiri Slaby1c456072008-02-07 00:16:46 -08007 * This code is loosely based on the 1.8 moxa driver which is based on
8 * Linux serial driver, written by Linus Torvalds, Theodore T'so and
9 * others.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -070014 * (at your option) any later version.
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 * Fed through a cleanup, indent and remove of non 2.6 code by Alan Cox
17 * <alan@redhat.com>. The original 1.8 code is available on www.moxa.com.
18 * - Fixed x86_64 cleanness
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 */
20
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/errno.h>
23#include <linux/signal.h>
24#include <linux/sched.h>
25#include <linux/timer.h>
26#include <linux/interrupt.h>
27#include <linux/tty.h>
28#include <linux/tty_flip.h>
29#include <linux/serial.h>
30#include <linux/serial_reg.h>
31#include <linux/major.h>
32#include <linux/string.h>
33#include <linux/fcntl.h>
34#include <linux/ptrace.h>
35#include <linux/gfp.h>
36#include <linux/ioport.h>
37#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/delay.h>
39#include <linux/pci.h>
Jiri Slaby1977f032007-10-18 23:40:25 -070040#include <linux/bitops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42#include <asm/system.h>
43#include <asm/io.h>
44#include <asm/irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <asm/uaccess.h>
46
47#include "mxser.h"
48
Jiri Slabye129def2008-07-22 11:20:34 +010049#define MXSER_VERSION "2.0.4" /* 1.12 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#define MXSERMAJOR 174
51#define MXSERCUMAJOR 175
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#define MXSER_BOARDS 4 /* Max. boards */
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#define MXSER_PORTS_PER_BOARD 8 /* Max. ports per board */
Jiri Slaby1c456072008-02-07 00:16:46 -080055#define MXSER_PORTS (MXSER_BOARDS * MXSER_PORTS_PER_BOARD)
56#define MXSER_ISR_PASS_LIMIT 100
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58#define MXSER_ERR_IOADDR -1
59#define MXSER_ERR_IRQ -2
60#define MXSER_ERR_IRQ_CONFLIT -3
61#define MXSER_ERR_VECTOR -4
62
Jiri Slaby1c456072008-02-07 00:16:46 -080063/*CheckIsMoxaMust return value*/
64#define MOXA_OTHER_UART 0x00
65#define MOXA_MUST_MU150_HWID 0x01
66#define MOXA_MUST_MU860_HWID 0x02
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#define WAKEUP_CHARS 256
69
70#define UART_MCR_AFE 0x20
71#define UART_LSR_SPECIAL 0x1E
72
Jiri Slabye129def2008-07-22 11:20:34 +010073#define PCI_DEVICE_ID_POS104UL 0x1044
Jiri Slaby1c456072008-02-07 00:16:46 -080074#define PCI_DEVICE_ID_CB108 0x1080
Jiri Slabye129def2008-07-22 11:20:34 +010075#define PCI_DEVICE_ID_CP102UF 0x1023
Jiri Slaby1c456072008-02-07 00:16:46 -080076#define PCI_DEVICE_ID_CB114 0x1142
Jiri Slaby80ff8a82008-02-07 00:16:51 -080077#define PCI_DEVICE_ID_CP114UL 0x1143
Jiri Slaby1c456072008-02-07 00:16:46 -080078#define PCI_DEVICE_ID_CB134I 0x1341
79#define PCI_DEVICE_ID_CP138U 0x1380
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, },
Jiri Slabye129def2008-07-22 11:20:34 +0100145 { "CP-114UL series", 4, },
146/*30*/ { "CP-102UF series", 2, }
Jiri Slaby1c456072008-02-07 00:16:46 -0800147};
148
149/* driver_data correspond to the lines in the structure above
150 see also ISA probe function before you change something */
151static struct pci_device_id mxser_pcibrds[] = {
152 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_C168), .driver_data = 3 },
153 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_C104), .driver_data = 4 },
154 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP132), .driver_data = 8 },
155 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP114), .driver_data = 9 },
156 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CT114), .driver_data = 10 },
157 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102), .driver_data = 11 },
158 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104U), .driver_data = 12 },
159 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP168U), .driver_data = 13 },
160 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP132U), .driver_data = 14 },
161 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP134U), .driver_data = 15 },
162 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104JU),.driver_data = 16 },
163 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_RC7000), .driver_data = 17 },
164 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP118U), .driver_data = 18 },
165 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102UL),.driver_data = 19 },
166 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102U), .driver_data = 20 },
167 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP118EL),.driver_data = 21 },
168 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP168EL),.driver_data = 22 },
169 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104EL),.driver_data = 23 },
170 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB108), .driver_data = 24 },
171 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB114), .driver_data = 25 },
172 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB134I), .driver_data = 26 },
173 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP138U), .driver_data = 27 },
174 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_POS104UL), .driver_data = 28 },
Jiri Slaby80ff8a82008-02-07 00:16:51 -0800175 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP114UL), .driver_data = 29 },
Jiri Slabye129def2008-07-22 11:20:34 +0100176 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP102UF), .driver_data = 30 },
Jiri Slaby1c456072008-02-07 00:16:46 -0800177 { }
178};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179MODULE_DEVICE_TABLE(pci, mxser_pcibrds);
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181static int ioaddr[MXSER_BOARDS] = { 0, 0, 0, 0 };
182static int ttymajor = MXSERMAJOR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
184/* Variables for insmod */
185
186MODULE_AUTHOR("Casper Yang");
187MODULE_DESCRIPTION("MOXA Smartio/Industio Family Multiport Board Device Driver");
Rusty Russell8d3b33f2006-03-25 03:07:05 -0800188module_param_array(ioaddr, int, NULL, 0);
189module_param(ttymajor, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190MODULE_LICENSE("GPL");
191
192struct mxser_log {
193 int tick;
194 unsigned long rxcnt[MXSER_PORTS];
195 unsigned long txcnt[MXSER_PORTS];
196};
197
198
199struct mxser_mon {
200 unsigned long rxcnt;
201 unsigned long txcnt;
202 unsigned long up_rxcnt;
203 unsigned long up_txcnt;
204 int modem_status;
205 unsigned char hold_reason;
206};
207
208struct mxser_mon_ext {
209 unsigned long rx_cnt[32];
210 unsigned long tx_cnt[32];
211 unsigned long up_rxcnt[32];
212 unsigned long up_txcnt[32];
213 int modem_status[32];
214
215 long baudrate[32];
216 int databits[32];
217 int stopbits[32];
218 int parity[32];
219 int flowctrl[32];
220 int fifo[32];
221 int iftype[32];
222};
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700223
Jiri Slaby1c456072008-02-07 00:16:46 -0800224struct mxser_board;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Jiri Slaby1c456072008-02-07 00:16:46 -0800226struct mxser_port {
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100227 struct tty_port port;
Jiri Slaby1c456072008-02-07 00:16:46 -0800228 struct mxser_board *board;
Jiri Slaby1c456072008-02-07 00:16:46 -0800229
230 unsigned long ioaddr;
231 unsigned long opmode_ioaddr;
232 int max_baud;
233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 int rx_high_water;
235 int rx_trigger; /* Rx fifo trigger level */
236 int rx_low_water;
237 int baud_base; /* max. speed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 int type; /* UART type */
Jiri Slaby1c456072008-02-07 00:16:46 -0800239
240 int x_char; /* xon/xoff character */
241 int IER; /* Interrupt Enable Register */
242 int MCR; /* Modem control register */
243
244 unsigned char stop_rx;
245 unsigned char ldisc_stop_rx;
246
247 int custom_divisor;
Jiri Slaby1c456072008-02-07 00:16:46 -0800248 unsigned char err_shadow;
Jiri Slaby1c456072008-02-07 00:16:46 -0800249
Jiri Slaby1c456072008-02-07 00:16:46 -0800250 struct async_icount icount; /* kernel counters for 4 input interrupts */
251 int timeout;
252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 int read_status_mask;
254 int ignore_status_mask;
255 int xmit_fifo_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 int xmit_head;
257 int xmit_tail;
258 int xmit_cnt;
Jiri Slaby1c456072008-02-07 00:16:46 -0800259
Alan Cox606d0992006-12-08 02:38:45 -0800260 struct ktermios normal_termios;
Jiri Slaby1c456072008-02-07 00:16:46 -0800261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 struct mxser_mon mon_data;
Jiri Slaby1c456072008-02-07 00:16:46 -0800263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 spinlock_t slock;
Jiri Slaby1c456072008-02-07 00:16:46 -0800265 wait_queue_head_t delta_msr_wait;
266};
267
268struct mxser_board {
269 unsigned int idx;
270 int irq;
271 const struct mxser_cardinfo *info;
272 unsigned long vector;
273 unsigned long vector_mask;
274
275 int chip_flag;
276 int uart_type;
277
278 struct mxser_port ports[MXSER_PORTS_PER_BOARD];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279};
280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281struct mxser_mstatus {
282 tcflag_t cflag;
283 int cts;
284 int dsr;
285 int ri;
286 int dcd;
287};
288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289static int mxserBoardCAP[MXSER_BOARDS] = {
290 0, 0, 0, 0
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700291 /* 0x180, 0x280, 0x200, 0x320 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292};
293
Jiri Slaby1c456072008-02-07 00:16:46 -0800294static struct mxser_board mxser_boards[MXSER_BOARDS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295static struct tty_driver *mxvar_sdriver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296static struct mxser_log mxvar_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297static int mxser_set_baud_method[MXSER_PORTS + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Christoph Hellwig148ff862008-04-30 00:54:29 -0700299static void mxser_enable_must_enchance_mode(unsigned long baseio)
300{
301 u8 oldlcr;
302 u8 efr;
303
304 oldlcr = inb(baseio + UART_LCR);
305 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
306
307 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
308 efr |= MOXA_MUST_EFR_EFRB_ENABLE;
309
310 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
311 outb(oldlcr, baseio + UART_LCR);
312}
313
314static void mxser_disable_must_enchance_mode(unsigned long baseio)
315{
316 u8 oldlcr;
317 u8 efr;
318
319 oldlcr = inb(baseio + UART_LCR);
320 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
321
322 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
323 efr &= ~MOXA_MUST_EFR_EFRB_ENABLE;
324
325 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
326 outb(oldlcr, baseio + UART_LCR);
327}
328
329static void mxser_set_must_xon1_value(unsigned long baseio, u8 value)
330{
331 u8 oldlcr;
332 u8 efr;
333
334 oldlcr = inb(baseio + UART_LCR);
335 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
336
337 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
338 efr &= ~MOXA_MUST_EFR_BANK_MASK;
339 efr |= MOXA_MUST_EFR_BANK0;
340
341 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
342 outb(value, baseio + MOXA_MUST_XON1_REGISTER);
343 outb(oldlcr, baseio + UART_LCR);
344}
345
346static void mxser_set_must_xoff1_value(unsigned long baseio, u8 value)
347{
348 u8 oldlcr;
349 u8 efr;
350
351 oldlcr = inb(baseio + UART_LCR);
352 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
353
354 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
355 efr &= ~MOXA_MUST_EFR_BANK_MASK;
356 efr |= MOXA_MUST_EFR_BANK0;
357
358 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
359 outb(value, baseio + MOXA_MUST_XOFF1_REGISTER);
360 outb(oldlcr, baseio + UART_LCR);
361}
362
363static void mxser_set_must_fifo_value(struct mxser_port *info)
364{
365 u8 oldlcr;
366 u8 efr;
367
368 oldlcr = inb(info->ioaddr + UART_LCR);
369 outb(MOXA_MUST_ENTER_ENCHANCE, info->ioaddr + UART_LCR);
370
371 efr = inb(info->ioaddr + MOXA_MUST_EFR_REGISTER);
372 efr &= ~MOXA_MUST_EFR_BANK_MASK;
373 efr |= MOXA_MUST_EFR_BANK1;
374
375 outb(efr, info->ioaddr + MOXA_MUST_EFR_REGISTER);
376 outb((u8)info->rx_high_water, info->ioaddr + MOXA_MUST_RBRTH_REGISTER);
377 outb((u8)info->rx_trigger, info->ioaddr + MOXA_MUST_RBRTI_REGISTER);
378 outb((u8)info->rx_low_water, info->ioaddr + MOXA_MUST_RBRTL_REGISTER);
379 outb(oldlcr, info->ioaddr + UART_LCR);
380}
381
382static void mxser_set_must_enum_value(unsigned long baseio, u8 value)
383{
384 u8 oldlcr;
385 u8 efr;
386
387 oldlcr = inb(baseio + UART_LCR);
388 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
389
390 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
391 efr &= ~MOXA_MUST_EFR_BANK_MASK;
392 efr |= MOXA_MUST_EFR_BANK2;
393
394 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
395 outb(value, baseio + MOXA_MUST_ENUM_REGISTER);
396 outb(oldlcr, baseio + UART_LCR);
397}
398
399static void mxser_get_must_hardware_id(unsigned long baseio, u8 *pId)
400{
401 u8 oldlcr;
402 u8 efr;
403
404 oldlcr = inb(baseio + UART_LCR);
405 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
406
407 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
408 efr &= ~MOXA_MUST_EFR_BANK_MASK;
409 efr |= MOXA_MUST_EFR_BANK2;
410
411 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
412 *pId = inb(baseio + MOXA_MUST_HWID_REGISTER);
413 outb(oldlcr, baseio + UART_LCR);
414}
415
416static void SET_MOXA_MUST_NO_SOFTWARE_FLOW_CONTROL(unsigned long baseio)
417{
418 u8 oldlcr;
419 u8 efr;
420
421 oldlcr = inb(baseio + UART_LCR);
422 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
423
424 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
425 efr &= ~MOXA_MUST_EFR_SF_MASK;
426
427 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
428 outb(oldlcr, baseio + UART_LCR);
429}
430
431static void mxser_enable_must_tx_software_flow_control(unsigned long baseio)
432{
433 u8 oldlcr;
434 u8 efr;
435
436 oldlcr = inb(baseio + UART_LCR);
437 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
438
439 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
440 efr &= ~MOXA_MUST_EFR_SF_TX_MASK;
441 efr |= MOXA_MUST_EFR_SF_TX1;
442
443 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
444 outb(oldlcr, baseio + UART_LCR);
445}
446
447static void mxser_disable_must_tx_software_flow_control(unsigned long baseio)
448{
449 u8 oldlcr;
450 u8 efr;
451
452 oldlcr = inb(baseio + UART_LCR);
453 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
454
455 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
456 efr &= ~MOXA_MUST_EFR_SF_TX_MASK;
457
458 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
459 outb(oldlcr, baseio + UART_LCR);
460}
461
462static void mxser_enable_must_rx_software_flow_control(unsigned long baseio)
463{
464 u8 oldlcr;
465 u8 efr;
466
467 oldlcr = inb(baseio + UART_LCR);
468 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
469
470 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
471 efr &= ~MOXA_MUST_EFR_SF_RX_MASK;
472 efr |= MOXA_MUST_EFR_SF_RX1;
473
474 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
475 outb(oldlcr, baseio + UART_LCR);
476}
477
478static void mxser_disable_must_rx_software_flow_control(unsigned long baseio)
479{
480 u8 oldlcr;
481 u8 efr;
482
483 oldlcr = inb(baseio + UART_LCR);
484 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
485
486 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
487 efr &= ~MOXA_MUST_EFR_SF_RX_MASK;
488
489 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
490 outb(oldlcr, baseio + UART_LCR);
491}
492
Jesper Juhlb8cc5542007-10-18 03:06:03 -0700493#ifdef CONFIG_PCI
Jiri Slaby1c456072008-02-07 00:16:46 -0800494static int __devinit CheckIsMoxaMust(unsigned long io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
496 u8 oldmcr, hwid;
497 int i;
498
499 outb(0, io + UART_LCR);
Christoph Hellwig148ff862008-04-30 00:54:29 -0700500 mxser_disable_must_enchance_mode(io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 oldmcr = inb(io + UART_MCR);
502 outb(0, io + UART_MCR);
Christoph Hellwig148ff862008-04-30 00:54:29 -0700503 mxser_set_must_xon1_value(io, 0x11);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 if ((hwid = inb(io + UART_MCR)) != 0) {
505 outb(oldmcr, io + UART_MCR);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700506 return MOXA_OTHER_UART;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 }
508
Christoph Hellwig148ff862008-04-30 00:54:29 -0700509 mxser_get_must_hardware_id(io, &hwid);
Jiri Slaby1c456072008-02-07 00:16:46 -0800510 for (i = 1; i < UART_INFO_NUM; i++) { /* 0 = OTHER_UART */
511 if (hwid == Gpci_uart_info[i].type)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700512 return (int)hwid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 }
514 return MOXA_OTHER_UART;
515}
Jesper Juhlb8cc5542007-10-18 03:06:03 -0700516#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Jiri Slaby1c456072008-02-07 00:16:46 -0800518static void process_txrx_fifo(struct mxser_port *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
520 int i;
521
522 if ((info->type == PORT_16450) || (info->type == PORT_8250)) {
523 info->rx_trigger = 1;
524 info->rx_high_water = 1;
525 info->rx_low_water = 1;
526 info->xmit_fifo_size = 1;
Jiri Slaby1c456072008-02-07 00:16:46 -0800527 } else
528 for (i = 0; i < UART_INFO_NUM; i++)
529 if (info->board->chip_flag == Gpci_uart_info[i].type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 info->rx_trigger = Gpci_uart_info[i].rx_trigger;
531 info->rx_low_water = Gpci_uart_info[i].rx_low_water;
532 info->rx_high_water = Gpci_uart_info[i].rx_high_water;
533 info->xmit_fifo_size = Gpci_uart_info[i].xmit_fifo_size;
534 break;
535 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536}
537
Jiri Slaby1c456072008-02-07 00:16:46 -0800538static unsigned char mxser_get_msr(int baseaddr, int mode, int port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539{
Jiri Slaby72800df2008-07-25 01:48:20 -0700540 static unsigned char mxser_msr[MXSER_PORTS + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 unsigned char status = 0;
542
543 status = inb(baseaddr + UART_MSR);
544
545 mxser_msr[port] &= 0x0F;
546 mxser_msr[port] |= status;
547 status = mxser_msr[port];
548 if (mode)
549 mxser_msr[port] = 0;
550
551 return status;
552}
553
Jiri Slaby1c456072008-02-07 00:16:46 -0800554static int mxser_block_til_ready(struct tty_struct *tty, struct file *filp,
555 struct mxser_port *port)
556{
557 DECLARE_WAITQUEUE(wait, current);
558 int retval;
559 int do_clocal = 0;
560 unsigned long flags;
561
562 /*
563 * If non-blocking mode is set, or the port is not enabled,
564 * then make the check up front and then exit.
565 */
566 if ((filp->f_flags & O_NONBLOCK) ||
567 test_bit(TTY_IO_ERROR, &tty->flags)) {
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100568 port->port.flags |= ASYNC_NORMAL_ACTIVE;
Jiri Slaby1c456072008-02-07 00:16:46 -0800569 return 0;
570 }
571
572 if (tty->termios->c_cflag & CLOCAL)
573 do_clocal = 1;
574
575 /*
576 * Block waiting for the carrier detect and the line to become
577 * free (i.e., not in use by the callout). While we are in
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100578 * this loop, port->port.count is dropped by one, so that
Jiri Slaby1c456072008-02-07 00:16:46 -0800579 * mxser_close() knows when to free things. We restore it upon
580 * exit, either normal or abnormal.
581 */
582 retval = 0;
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100583 add_wait_queue(&port->port.open_wait, &wait);
Jiri Slaby1c456072008-02-07 00:16:46 -0800584
585 spin_lock_irqsave(&port->slock, flags);
586 if (!tty_hung_up_p(filp))
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100587 port->port.count--;
Jiri Slaby1c456072008-02-07 00:16:46 -0800588 spin_unlock_irqrestore(&port->slock, flags);
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100589 port->port.blocked_open++;
Jiri Slaby1c456072008-02-07 00:16:46 -0800590 while (1) {
591 spin_lock_irqsave(&port->slock, flags);
592 outb(inb(port->ioaddr + UART_MCR) |
593 UART_MCR_DTR | UART_MCR_RTS, port->ioaddr + UART_MCR);
594 spin_unlock_irqrestore(&port->slock, flags);
595 set_current_state(TASK_INTERRUPTIBLE);
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100596 if (tty_hung_up_p(filp) || !(port->port.flags & ASYNC_INITIALIZED)) {
597 if (port->port.flags & ASYNC_HUP_NOTIFY)
Jiri Slaby1c456072008-02-07 00:16:46 -0800598 retval = -EAGAIN;
599 else
600 retval = -ERESTARTSYS;
601 break;
602 }
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100603 if (!(port->port.flags & ASYNC_CLOSING) &&
Jiri Slaby1c456072008-02-07 00:16:46 -0800604 (do_clocal ||
605 (inb(port->ioaddr + UART_MSR) & UART_MSR_DCD)))
606 break;
607 if (signal_pending(current)) {
608 retval = -ERESTARTSYS;
609 break;
610 }
611 schedule();
612 }
613 set_current_state(TASK_RUNNING);
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100614 remove_wait_queue(&port->port.open_wait, &wait);
Jiri Slaby1c456072008-02-07 00:16:46 -0800615 if (!tty_hung_up_p(filp))
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100616 port->port.count++;
617 port->port.blocked_open--;
Jiri Slaby1c456072008-02-07 00:16:46 -0800618 if (retval)
619 return retval;
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100620 port->port.flags |= ASYNC_NORMAL_ACTIVE;
Jiri Slaby1c456072008-02-07 00:16:46 -0800621 return 0;
622}
623
624static int mxser_set_baud(struct mxser_port *info, long newspd)
625{
626 int quot = 0, baud;
627 unsigned char cval;
628
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100629 if (!info->port.tty || !info->port.tty->termios)
Jiri Slaby1c456072008-02-07 00:16:46 -0800630 return -1;
631
632 if (!(info->ioaddr))
633 return -1;
634
635 if (newspd > info->max_baud)
636 return -1;
637
638 if (newspd == 134) {
639 quot = 2 * info->baud_base / 269;
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100640 tty_encode_baud_rate(info->port.tty, 134, 134);
Jiri Slaby1c456072008-02-07 00:16:46 -0800641 } else if (newspd) {
642 quot = info->baud_base / newspd;
643 if (quot == 0)
644 quot = 1;
645 baud = info->baud_base/quot;
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100646 tty_encode_baud_rate(info->port.tty, baud, baud);
Jiri Slaby1c456072008-02-07 00:16:46 -0800647 } else {
648 quot = 0;
649 }
650
651 info->timeout = ((info->xmit_fifo_size * HZ * 10 * quot) / info->baud_base);
652 info->timeout += HZ / 50; /* Add .02 seconds of slop */
653
654 if (quot) {
655 info->MCR |= UART_MCR_DTR;
656 outb(info->MCR, info->ioaddr + UART_MCR);
657 } else {
658 info->MCR &= ~UART_MCR_DTR;
659 outb(info->MCR, info->ioaddr + UART_MCR);
660 return 0;
661 }
662
663 cval = inb(info->ioaddr + UART_LCR);
664
665 outb(cval | UART_LCR_DLAB, info->ioaddr + UART_LCR); /* set DLAB */
666
667 outb(quot & 0xff, info->ioaddr + UART_DLL); /* LS of divisor */
668 outb(quot >> 8, info->ioaddr + UART_DLM); /* MS of divisor */
669 outb(cval, info->ioaddr + UART_LCR); /* reset DLAB */
670
671#ifdef BOTHER
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100672 if (C_BAUD(info->port.tty) == BOTHER) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800673 quot = info->baud_base % newspd;
674 quot *= 8;
675 if (quot % newspd > newspd / 2) {
676 quot /= newspd;
677 quot++;
678 } else
679 quot /= newspd;
680
Christoph Hellwig148ff862008-04-30 00:54:29 -0700681 mxser_set_must_enum_value(info->ioaddr, quot);
Jiri Slaby1c456072008-02-07 00:16:46 -0800682 } else
683#endif
Christoph Hellwig148ff862008-04-30 00:54:29 -0700684 mxser_set_must_enum_value(info->ioaddr, 0);
Jiri Slaby1c456072008-02-07 00:16:46 -0800685
686 return 0;
687}
688
689/*
690 * This routine is called to set the UART divisor registers to match
691 * the specified baud rate for a serial port.
692 */
693static int mxser_change_speed(struct mxser_port *info,
694 struct ktermios *old_termios)
695{
696 unsigned cflag, cval, fcr;
697 int ret = 0;
698 unsigned char status;
699
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100700 if (!info->port.tty || !info->port.tty->termios)
Jiri Slaby1c456072008-02-07 00:16:46 -0800701 return ret;
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100702 cflag = info->port.tty->termios->c_cflag;
Jiri Slaby1c456072008-02-07 00:16:46 -0800703 if (!(info->ioaddr))
704 return ret;
705
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100706 if (mxser_set_baud_method[info->port.tty->index] == 0)
707 mxser_set_baud(info, tty_get_baud_rate(info->port.tty));
Jiri Slaby1c456072008-02-07 00:16:46 -0800708
709 /* byte size and parity */
710 switch (cflag & CSIZE) {
711 case CS5:
712 cval = 0x00;
713 break;
714 case CS6:
715 cval = 0x01;
716 break;
717 case CS7:
718 cval = 0x02;
719 break;
720 case CS8:
721 cval = 0x03;
722 break;
723 default:
724 cval = 0x00;
725 break; /* too keep GCC shut... */
726 }
727 if (cflag & CSTOPB)
728 cval |= 0x04;
729 if (cflag & PARENB)
730 cval |= UART_LCR_PARITY;
731 if (!(cflag & PARODD))
732 cval |= UART_LCR_EPAR;
733 if (cflag & CMSPAR)
734 cval |= UART_LCR_SPAR;
735
736 if ((info->type == PORT_8250) || (info->type == PORT_16450)) {
737 if (info->board->chip_flag) {
738 fcr = UART_FCR_ENABLE_FIFO;
739 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
Christoph Hellwig148ff862008-04-30 00:54:29 -0700740 mxser_set_must_fifo_value(info);
Jiri Slaby1c456072008-02-07 00:16:46 -0800741 } else
742 fcr = 0;
743 } else {
744 fcr = UART_FCR_ENABLE_FIFO;
745 if (info->board->chip_flag) {
746 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
Christoph Hellwig148ff862008-04-30 00:54:29 -0700747 mxser_set_must_fifo_value(info);
Jiri Slaby1c456072008-02-07 00:16:46 -0800748 } else {
749 switch (info->rx_trigger) {
750 case 1:
751 fcr |= UART_FCR_TRIGGER_1;
752 break;
753 case 4:
754 fcr |= UART_FCR_TRIGGER_4;
755 break;
756 case 8:
757 fcr |= UART_FCR_TRIGGER_8;
758 break;
759 default:
760 fcr |= UART_FCR_TRIGGER_14;
761 break;
762 }
763 }
764 }
765
766 /* CTS flow control flag and modem status interrupts */
767 info->IER &= ~UART_IER_MSI;
768 info->MCR &= ~UART_MCR_AFE;
769 if (cflag & CRTSCTS) {
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100770 info->port.flags |= ASYNC_CTS_FLOW;
Jiri Slaby1c456072008-02-07 00:16:46 -0800771 info->IER |= UART_IER_MSI;
772 if ((info->type == PORT_16550A) || (info->board->chip_flag)) {
773 info->MCR |= UART_MCR_AFE;
774 } else {
775 status = inb(info->ioaddr + UART_MSR);
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100776 if (info->port.tty->hw_stopped) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800777 if (status & UART_MSR_CTS) {
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100778 info->port.tty->hw_stopped = 0;
Jiri Slaby1c456072008-02-07 00:16:46 -0800779 if (info->type != PORT_16550A &&
780 !info->board->chip_flag) {
781 outb(info->IER & ~UART_IER_THRI,
782 info->ioaddr +
783 UART_IER);
784 info->IER |= UART_IER_THRI;
785 outb(info->IER, info->ioaddr +
786 UART_IER);
787 }
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100788 tty_wakeup(info->port.tty);
Jiri Slaby1c456072008-02-07 00:16:46 -0800789 }
790 } else {
791 if (!(status & UART_MSR_CTS)) {
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100792 info->port.tty->hw_stopped = 1;
Jiri Slaby1c456072008-02-07 00:16:46 -0800793 if ((info->type != PORT_16550A) &&
794 (!info->board->chip_flag)) {
795 info->IER &= ~UART_IER_THRI;
796 outb(info->IER, info->ioaddr +
797 UART_IER);
798 }
799 }
800 }
801 }
802 } else {
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100803 info->port.flags &= ~ASYNC_CTS_FLOW;
Jiri Slaby1c456072008-02-07 00:16:46 -0800804 }
805 outb(info->MCR, info->ioaddr + UART_MCR);
806 if (cflag & CLOCAL) {
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100807 info->port.flags &= ~ASYNC_CHECK_CD;
Jiri Slaby1c456072008-02-07 00:16:46 -0800808 } else {
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100809 info->port.flags |= ASYNC_CHECK_CD;
Jiri Slaby1c456072008-02-07 00:16:46 -0800810 info->IER |= UART_IER_MSI;
811 }
812 outb(info->IER, info->ioaddr + UART_IER);
813
814 /*
815 * Set up parity check flag
816 */
817 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100818 if (I_INPCK(info->port.tty))
Jiri Slaby1c456072008-02-07 00:16:46 -0800819 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100820 if (I_BRKINT(info->port.tty) || I_PARMRK(info->port.tty))
Jiri Slaby1c456072008-02-07 00:16:46 -0800821 info->read_status_mask |= UART_LSR_BI;
822
823 info->ignore_status_mask = 0;
824
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100825 if (I_IGNBRK(info->port.tty)) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800826 info->ignore_status_mask |= UART_LSR_BI;
827 info->read_status_mask |= UART_LSR_BI;
828 /*
829 * If we're ignore parity and break indicators, ignore
830 * overruns too. (For real raw support).
831 */
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100832 if (I_IGNPAR(info->port.tty)) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800833 info->ignore_status_mask |=
834 UART_LSR_OE |
835 UART_LSR_PE |
836 UART_LSR_FE;
837 info->read_status_mask |=
838 UART_LSR_OE |
839 UART_LSR_PE |
840 UART_LSR_FE;
841 }
842 }
843 if (info->board->chip_flag) {
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100844 mxser_set_must_xon1_value(info->ioaddr, START_CHAR(info->port.tty));
845 mxser_set_must_xoff1_value(info->ioaddr, STOP_CHAR(info->port.tty));
846 if (I_IXON(info->port.tty)) {
Christoph Hellwig148ff862008-04-30 00:54:29 -0700847 mxser_enable_must_rx_software_flow_control(
848 info->ioaddr);
Jiri Slaby1c456072008-02-07 00:16:46 -0800849 } else {
Christoph Hellwig148ff862008-04-30 00:54:29 -0700850 mxser_disable_must_rx_software_flow_control(
851 info->ioaddr);
Jiri Slaby1c456072008-02-07 00:16:46 -0800852 }
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100853 if (I_IXOFF(info->port.tty)) {
Christoph Hellwig148ff862008-04-30 00:54:29 -0700854 mxser_enable_must_tx_software_flow_control(
855 info->ioaddr);
Jiri Slaby1c456072008-02-07 00:16:46 -0800856 } else {
Christoph Hellwig148ff862008-04-30 00:54:29 -0700857 mxser_disable_must_tx_software_flow_control(
858 info->ioaddr);
Jiri Slaby1c456072008-02-07 00:16:46 -0800859 }
860 }
861
862
863 outb(fcr, info->ioaddr + UART_FCR); /* set fcr */
864 outb(cval, info->ioaddr + UART_LCR);
865
866 return ret;
867}
868
869static void mxser_check_modem_status(struct mxser_port *port, int status)
870{
871 /* update input line counters */
872 if (status & UART_MSR_TERI)
873 port->icount.rng++;
874 if (status & UART_MSR_DDSR)
875 port->icount.dsr++;
876 if (status & UART_MSR_DDCD)
877 port->icount.dcd++;
878 if (status & UART_MSR_DCTS)
879 port->icount.cts++;
880 port->mon_data.modem_status = status;
881 wake_up_interruptible(&port->delta_msr_wait);
882
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100883 if ((port->port.flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800884 if (status & UART_MSR_DCD)
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100885 wake_up_interruptible(&port->port.open_wait);
Jiri Slaby1c456072008-02-07 00:16:46 -0800886 }
887
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100888 if (port->port.flags & ASYNC_CTS_FLOW) {
889 if (port->port.tty->hw_stopped) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800890 if (status & UART_MSR_CTS) {
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100891 port->port.tty->hw_stopped = 0;
Jiri Slaby1c456072008-02-07 00:16:46 -0800892
893 if ((port->type != PORT_16550A) &&
894 (!port->board->chip_flag)) {
895 outb(port->IER & ~UART_IER_THRI,
896 port->ioaddr + UART_IER);
897 port->IER |= UART_IER_THRI;
898 outb(port->IER, port->ioaddr +
899 UART_IER);
900 }
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100901 tty_wakeup(port->port.tty);
Jiri Slaby1c456072008-02-07 00:16:46 -0800902 }
903 } else {
904 if (!(status & UART_MSR_CTS)) {
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100905 port->port.tty->hw_stopped = 1;
Jiri Slaby1c456072008-02-07 00:16:46 -0800906 if (port->type != PORT_16550A &&
907 !port->board->chip_flag) {
908 port->IER &= ~UART_IER_THRI;
909 outb(port->IER, port->ioaddr +
910 UART_IER);
911 }
912 }
913 }
914 }
915}
916
917static int mxser_startup(struct mxser_port *info)
918{
919 unsigned long page;
920 unsigned long flags;
921
922 page = __get_free_page(GFP_KERNEL);
923 if (!page)
924 return -ENOMEM;
925
926 spin_lock_irqsave(&info->slock, flags);
927
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100928 if (info->port.flags & ASYNC_INITIALIZED) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800929 free_page(page);
930 spin_unlock_irqrestore(&info->slock, flags);
931 return 0;
932 }
933
934 if (!info->ioaddr || !info->type) {
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100935 if (info->port.tty)
936 set_bit(TTY_IO_ERROR, &info->port.tty->flags);
Jiri Slaby1c456072008-02-07 00:16:46 -0800937 free_page(page);
938 spin_unlock_irqrestore(&info->slock, flags);
939 return 0;
940 }
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100941 if (info->port.xmit_buf)
Jiri Slaby1c456072008-02-07 00:16:46 -0800942 free_page(page);
943 else
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100944 info->port.xmit_buf = (unsigned char *) page;
Jiri Slaby1c456072008-02-07 00:16:46 -0800945
946 /*
947 * Clear the FIFO buffers and disable them
948 * (they will be reenabled in mxser_change_speed())
949 */
950 if (info->board->chip_flag)
951 outb((UART_FCR_CLEAR_RCVR |
952 UART_FCR_CLEAR_XMIT |
953 MOXA_MUST_FCR_GDA_MODE_ENABLE), info->ioaddr + UART_FCR);
954 else
955 outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
956 info->ioaddr + UART_FCR);
957
958 /*
959 * At this point there's no way the LSR could still be 0xFF;
960 * if it is, then bail out, because there's likely no UART
961 * here.
962 */
963 if (inb(info->ioaddr + UART_LSR) == 0xff) {
964 spin_unlock_irqrestore(&info->slock, flags);
965 if (capable(CAP_SYS_ADMIN)) {
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100966 if (info->port.tty)
967 set_bit(TTY_IO_ERROR, &info->port.tty->flags);
Jiri Slaby1c456072008-02-07 00:16:46 -0800968 return 0;
969 } else
970 return -ENODEV;
971 }
972
973 /*
974 * Clear the interrupt registers.
975 */
976 (void) inb(info->ioaddr + UART_LSR);
977 (void) inb(info->ioaddr + UART_RX);
978 (void) inb(info->ioaddr + UART_IIR);
979 (void) inb(info->ioaddr + UART_MSR);
980
981 /*
982 * Now, initialize the UART
983 */
984 outb(UART_LCR_WLEN8, info->ioaddr + UART_LCR); /* reset DLAB */
985 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
986 outb(info->MCR, info->ioaddr + UART_MCR);
987
988 /*
989 * Finally, enable interrupts
990 */
991 info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
992
993 if (info->board->chip_flag)
994 info->IER |= MOXA_MUST_IER_EGDAI;
995 outb(info->IER, info->ioaddr + UART_IER); /* enable interrupts */
996
997 /*
998 * And clear the interrupt registers again for luck.
999 */
1000 (void) inb(info->ioaddr + UART_LSR);
1001 (void) inb(info->ioaddr + UART_RX);
1002 (void) inb(info->ioaddr + UART_IIR);
1003 (void) inb(info->ioaddr + UART_MSR);
1004
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001005 if (info->port.tty)
1006 clear_bit(TTY_IO_ERROR, &info->port.tty->flags);
Jiri Slaby1c456072008-02-07 00:16:46 -08001007 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1008
1009 /*
1010 * and set the speed of the serial port
1011 */
1012 mxser_change_speed(info, NULL);
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001013 info->port.flags |= ASYNC_INITIALIZED;
Jiri Slaby1c456072008-02-07 00:16:46 -08001014 spin_unlock_irqrestore(&info->slock, flags);
1015
1016 return 0;
1017}
1018
1019/*
1020 * This routine will shutdown a serial port; interrupts maybe disabled, and
1021 * DTR is dropped if the hangup on close termio flag is on.
1022 */
1023static void mxser_shutdown(struct mxser_port *info)
1024{
1025 unsigned long flags;
1026
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001027 if (!(info->port.flags & ASYNC_INITIALIZED))
Jiri Slaby1c456072008-02-07 00:16:46 -08001028 return;
1029
1030 spin_lock_irqsave(&info->slock, flags);
1031
1032 /*
1033 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
1034 * here so the queue might never be waken up
1035 */
1036 wake_up_interruptible(&info->delta_msr_wait);
1037
1038 /*
1039 * Free the IRQ, if necessary
1040 */
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001041 if (info->port.xmit_buf) {
1042 free_page((unsigned long) info->port.xmit_buf);
1043 info->port.xmit_buf = NULL;
Jiri Slaby1c456072008-02-07 00:16:46 -08001044 }
1045
1046 info->IER = 0;
1047 outb(0x00, info->ioaddr + UART_IER);
1048
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001049 if (!info->port.tty || (info->port.tty->termios->c_cflag & HUPCL))
Jiri Slaby1c456072008-02-07 00:16:46 -08001050 info->MCR &= ~(UART_MCR_DTR | UART_MCR_RTS);
1051 outb(info->MCR, info->ioaddr + UART_MCR);
1052
1053 /* clear Rx/Tx FIFO's */
1054 if (info->board->chip_flag)
1055 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT |
1056 MOXA_MUST_FCR_GDA_MODE_ENABLE,
1057 info->ioaddr + UART_FCR);
1058 else
1059 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
1060 info->ioaddr + UART_FCR);
1061
1062 /* read data port to reset things */
1063 (void) inb(info->ioaddr + UART_RX);
1064
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001065 if (info->port.tty)
1066 set_bit(TTY_IO_ERROR, &info->port.tty->flags);
Jiri Slaby1c456072008-02-07 00:16:46 -08001067
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001068 info->port.flags &= ~ASYNC_INITIALIZED;
Jiri Slaby1c456072008-02-07 00:16:46 -08001069
1070 if (info->board->chip_flag)
1071 SET_MOXA_MUST_NO_SOFTWARE_FLOW_CONTROL(info->ioaddr);
1072
1073 spin_unlock_irqrestore(&info->slock, flags);
1074}
1075
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076/*
1077 * This routine is called whenever a serial port is opened. It
1078 * enables interrupts for a serial port, linking in its async structure into
1079 * the IRQ chain. It also performs the serial-specific
1080 * initialization for the tty structure.
1081 */
1082static int mxser_open(struct tty_struct *tty, struct file *filp)
1083{
Jiri Slaby1c456072008-02-07 00:16:46 -08001084 struct mxser_port *info;
1085 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 int retval, line;
1087
1088 line = tty->index;
1089 if (line == MXSER_PORTS)
1090 return 0;
1091 if (line < 0 || line > MXSER_PORTS)
1092 return -ENODEV;
Jiri Slaby1c456072008-02-07 00:16:46 -08001093 info = &mxser_boards[line / MXSER_PORTS_PER_BOARD].ports[line % MXSER_PORTS_PER_BOARD];
1094 if (!info->ioaddr)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001095 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
1097 tty->driver_data = info;
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001098 info->port.tty = tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 /*
1100 * Start up serial port
1101 */
Jiri Slaby1c456072008-02-07 00:16:46 -08001102 spin_lock_irqsave(&info->slock, flags);
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001103 info->port.count++;
Jiri Slaby1c456072008-02-07 00:16:46 -08001104 spin_unlock_irqrestore(&info->slock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 retval = mxser_startup(info);
1106 if (retval)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001107 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
1109 retval = mxser_block_til_ready(tty, filp, info);
1110 if (retval)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001111 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
Cedric Le Goater8cddd702007-02-10 01:46:33 -08001113 /* unmark here for very high baud rate (ex. 921600 bps) used */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 tty->low_latency = 1;
1115 return 0;
1116}
1117
Alan Cox978e5952008-04-30 00:53:59 -07001118static void mxser_flush_buffer(struct tty_struct *tty)
1119{
1120 struct mxser_port *info = tty->driver_data;
1121 char fcr;
1122 unsigned long flags;
1123
1124
1125 spin_lock_irqsave(&info->slock, flags);
1126 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1127
1128 fcr = inb(info->ioaddr + UART_FCR);
1129 outb((fcr | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
1130 info->ioaddr + UART_FCR);
1131 outb(fcr, info->ioaddr + UART_FCR);
1132
1133 spin_unlock_irqrestore(&info->slock, flags);
1134
1135 tty_wakeup(tty);
1136}
1137
1138
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139/*
1140 * This routine is called when the serial port gets closed. First, we
1141 * wait for the last remaining data to be sent. Then, we unlink its
1142 * async structure from the interrupt chain if necessary, and we free
1143 * that IRQ if nothing is left in the chain.
1144 */
1145static void mxser_close(struct tty_struct *tty, struct file *filp)
1146{
Jiri Slaby1c456072008-02-07 00:16:46 -08001147 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
1149 unsigned long timeout;
1150 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
1152 if (tty->index == MXSER_PORTS)
1153 return;
1154 if (!info)
Kirill Smelkov6f08b722005-11-07 00:59:23 -08001155 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
1157 spin_lock_irqsave(&info->slock, flags);
1158
1159 if (tty_hung_up_p(filp)) {
1160 spin_unlock_irqrestore(&info->slock, flags);
1161 return;
1162 }
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001163 if ((tty->count == 1) && (info->port.count != 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 /*
1165 * Uh, oh. tty->count is 1, which means that the tty
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001166 * structure will be freed. Info->port.count should always
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 * be one in these conditions. If it's greater than
1168 * one, we've got real problems, since it means the
1169 * serial port won't be shutdown.
1170 */
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001171 printk(KERN_ERR "mxser_close: bad serial port count; "
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001172 "tty->count is 1, info->port.count is %d\n", info->port.count);
1173 info->port.count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 }
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001175 if (--info->port.count < 0) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001176 printk(KERN_ERR "mxser_close: bad serial port count for "
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001177 "ttys%d: %d\n", tty->index, info->port.count);
1178 info->port.count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 }
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001180 if (info->port.count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 spin_unlock_irqrestore(&info->slock, flags);
1182 return;
1183 }
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001184 info->port.flags |= ASYNC_CLOSING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 spin_unlock_irqrestore(&info->slock, flags);
1186 /*
1187 * Save the termios structure, since this port may have
1188 * separate termios for callout and dialin.
1189 */
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001190 if (info->port.flags & ASYNC_NORMAL_ACTIVE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 info->normal_termios = *tty->termios;
1192 /*
1193 * Now we wait for the transmit buffer to clear; and we notify
1194 * the line discipline to only process XON/XOFF characters.
1195 */
1196 tty->closing = 1;
Alan Cox44b7d1b2008-07-16 21:57:18 +01001197 if (info->port.closing_wait != ASYNC_CLOSING_WAIT_NONE)
1198 tty_wait_until_sent(tty, info->port.closing_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 /*
1200 * At this point we stop accepting input. To do this, we
1201 * disable the receive line status interrupts, and tell the
1202 * interrupt driver to stop checking the data ready bit in the
1203 * line status register.
1204 */
1205 info->IER &= ~UART_IER_RLSI;
Jiri Slaby1c456072008-02-07 00:16:46 -08001206 if (info->board->chip_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 info->IER &= ~MOXA_MUST_RECV_ISR;
Jiri Slaby1c456072008-02-07 00:16:46 -08001208
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001209 if (info->port.flags & ASYNC_INITIALIZED) {
Jiri Slaby1c456072008-02-07 00:16:46 -08001210 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 /*
1212 * Before we drop DTR, make sure the UART transmitter
1213 * has completely drained; this is especially
1214 * important if there is a transmit FIFO!
1215 */
1216 timeout = jiffies + HZ;
Jiri Slaby1c456072008-02-07 00:16:46 -08001217 while (!(inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT)) {
Nishanth Aravamudanda4cd8d2005-09-10 00:27:30 -07001218 schedule_timeout_interruptible(5);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 if (time_after(jiffies, timeout))
1220 break;
1221 }
1222 }
1223 mxser_shutdown(info);
1224
Alan Cox978e5952008-04-30 00:53:59 -07001225 mxser_flush_buffer(tty);
Jiri Slaby1c456072008-02-07 00:16:46 -08001226 tty_ldisc_flush(tty);
1227
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 tty->closing = 0;
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001229 info->port.tty = NULL;
1230 if (info->port.blocked_open) {
Alan Cox44b7d1b2008-07-16 21:57:18 +01001231 if (info->port.close_delay)
1232 schedule_timeout_interruptible(info->port.close_delay);
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001233 wake_up_interruptible(&info->port.open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 }
1235
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001236 info->port.flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237}
1238
1239static int mxser_write(struct tty_struct *tty, const unsigned char *buf, int count)
1240{
1241 int c, total = 0;
Jiri Slaby1c456072008-02-07 00:16:46 -08001242 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 unsigned long flags;
1244
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001245 if (!info->port.xmit_buf)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001246 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
1248 while (1) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001249 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1250 SERIAL_XMIT_SIZE - info->xmit_head));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 if (c <= 0)
1252 break;
1253
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001254 memcpy(info->port.xmit_buf + info->xmit_head, buf, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 spin_lock_irqsave(&info->slock, flags);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001256 info->xmit_head = (info->xmit_head + c) &
1257 (SERIAL_XMIT_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 info->xmit_cnt += c;
1259 spin_unlock_irqrestore(&info->slock, flags);
1260
1261 buf += c;
1262 count -= c;
1263 total += c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 }
1265
Jiri Slaby1c456072008-02-07 00:16:46 -08001266 if (info->xmit_cnt && !tty->stopped) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001267 if (!tty->hw_stopped ||
1268 (info->type == PORT_16550A) ||
Jiri Slaby1c456072008-02-07 00:16:46 -08001269 (info->board->chip_flag)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby1c456072008-02-07 00:16:46 -08001271 outb(info->IER & ~UART_IER_THRI, info->ioaddr +
1272 UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 info->IER |= UART_IER_THRI;
Jiri Slaby1c456072008-02-07 00:16:46 -08001274 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 spin_unlock_irqrestore(&info->slock, flags);
1276 }
1277 }
1278 return total;
1279}
1280
Alan Cox0be2ead2008-04-30 00:54:03 -07001281static int mxser_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282{
Jiri Slaby1c456072008-02-07 00:16:46 -08001283 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 unsigned long flags;
1285
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001286 if (!info->port.xmit_buf)
Alan Cox0be2ead2008-04-30 00:54:03 -07001287 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
1289 if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1)
Alan Cox0be2ead2008-04-30 00:54:03 -07001290 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
1292 spin_lock_irqsave(&info->slock, flags);
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001293 info->port.xmit_buf[info->xmit_head++] = ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 info->xmit_head &= SERIAL_XMIT_SIZE - 1;
1295 info->xmit_cnt++;
1296 spin_unlock_irqrestore(&info->slock, flags);
Jiri Slaby1c456072008-02-07 00:16:46 -08001297 if (!tty->stopped) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001298 if (!tty->hw_stopped ||
1299 (info->type == PORT_16550A) ||
Jiri Slaby1c456072008-02-07 00:16:46 -08001300 info->board->chip_flag) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby1c456072008-02-07 00:16:46 -08001302 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 info->IER |= UART_IER_THRI;
Jiri Slaby1c456072008-02-07 00:16:46 -08001304 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 spin_unlock_irqrestore(&info->slock, flags);
1306 }
1307 }
Alan Cox0be2ead2008-04-30 00:54:03 -07001308 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309}
1310
1311
1312static void mxser_flush_chars(struct tty_struct *tty)
1313{
Jiri Slaby1c456072008-02-07 00:16:46 -08001314 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 unsigned long flags;
1316
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001317 if (info->xmit_cnt <= 0 ||
1318 tty->stopped ||
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001319 !info->port.xmit_buf ||
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001320 (tty->hw_stopped &&
1321 (info->type != PORT_16550A) &&
Jiri Slaby1c456072008-02-07 00:16:46 -08001322 (!info->board->chip_flag)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001323 ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 return;
1325
1326 spin_lock_irqsave(&info->slock, flags);
1327
Jiri Slaby1c456072008-02-07 00:16:46 -08001328 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 info->IER |= UART_IER_THRI;
Jiri Slaby1c456072008-02-07 00:16:46 -08001330 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
1332 spin_unlock_irqrestore(&info->slock, flags);
1333}
1334
1335static int mxser_write_room(struct tty_struct *tty)
1336{
Jiri Slaby1c456072008-02-07 00:16:46 -08001337 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 int ret;
1339
1340 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1341 if (ret < 0)
1342 ret = 0;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001343 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344}
1345
1346static int mxser_chars_in_buffer(struct tty_struct *tty)
1347{
Jiri Slaby1c456072008-02-07 00:16:46 -08001348 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 return info->xmit_cnt;
1350}
1351
Jiri Slaby1c456072008-02-07 00:16:46 -08001352/*
1353 * ------------------------------------------------------------
1354 * friends of mxser_ioctl()
1355 * ------------------------------------------------------------
1356 */
1357static int mxser_get_serial_info(struct mxser_port *info,
1358 struct serial_struct __user *retinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359{
Jiri Slaby1c456072008-02-07 00:16:46 -08001360 struct serial_struct tmp = {
1361 .type = info->type,
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001362 .line = info->port.tty->index,
Jiri Slaby1c456072008-02-07 00:16:46 -08001363 .port = info->ioaddr,
1364 .irq = info->board->irq,
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001365 .flags = info->port.flags,
Jiri Slaby1c456072008-02-07 00:16:46 -08001366 .baud_base = info->baud_base,
Alan Cox44b7d1b2008-07-16 21:57:18 +01001367 .close_delay = info->port.close_delay,
1368 .closing_wait = info->port.closing_wait,
Jiri Slaby1c456072008-02-07 00:16:46 -08001369 .custom_divisor = info->custom_divisor,
1370 .hub6 = 0
1371 };
1372 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1373 return -EFAULT;
1374 return 0;
1375}
1376
1377static int mxser_set_serial_info(struct mxser_port *info,
1378 struct serial_struct __user *new_info)
1379{
1380 struct serial_struct new_serial;
Jiri Slaby80ff8a82008-02-07 00:16:51 -08001381 speed_t baud;
Jiri Slaby1c456072008-02-07 00:16:46 -08001382 unsigned long sl_flags;
1383 unsigned int flags;
1384 int retval = 0;
1385
1386 if (!new_info || !info->ioaddr)
Jiri Slaby80ff8a82008-02-07 00:16:51 -08001387 return -ENODEV;
Jiri Slaby1c456072008-02-07 00:16:46 -08001388 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
1389 return -EFAULT;
1390
Jiri Slaby80ff8a82008-02-07 00:16:51 -08001391 if (new_serial.irq != info->board->irq ||
1392 new_serial.port != info->ioaddr)
1393 return -EINVAL;
Jiri Slaby1c456072008-02-07 00:16:46 -08001394
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001395 flags = info->port.flags & ASYNC_SPD_MASK;
Jiri Slaby1c456072008-02-07 00:16:46 -08001396
1397 if (!capable(CAP_SYS_ADMIN)) {
1398 if ((new_serial.baud_base != info->baud_base) ||
Alan Cox44b7d1b2008-07-16 21:57:18 +01001399 (new_serial.close_delay != info->port.close_delay) ||
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001400 ((new_serial.flags & ~ASYNC_USR_MASK) != (info->port.flags & ~ASYNC_USR_MASK)))
Jiri Slaby1c456072008-02-07 00:16:46 -08001401 return -EPERM;
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001402 info->port.flags = ((info->port.flags & ~ASYNC_USR_MASK) |
Jiri Slaby1c456072008-02-07 00:16:46 -08001403 (new_serial.flags & ASYNC_USR_MASK));
1404 } else {
1405 /*
1406 * OK, past this point, all the error checking has been done.
1407 * At this point, we start making changes.....
1408 */
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001409 info->port.flags = ((info->port.flags & ~ASYNC_FLAGS) |
Jiri Slaby1c456072008-02-07 00:16:46 -08001410 (new_serial.flags & ASYNC_FLAGS));
Alan Cox44b7d1b2008-07-16 21:57:18 +01001411 info->port.close_delay = new_serial.close_delay * HZ / 100;
1412 info->port.closing_wait = new_serial.closing_wait * HZ / 100;
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001413 info->port.tty->low_latency =
1414 (info->port.flags & ASYNC_LOW_LATENCY) ? 1 : 0;
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001415 if ((info->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST &&
Jiri Slaby80ff8a82008-02-07 00:16:51 -08001416 (new_serial.baud_base != info->baud_base ||
1417 new_serial.custom_divisor !=
1418 info->custom_divisor)) {
1419 baud = new_serial.baud_base / new_serial.custom_divisor;
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001420 tty_encode_baud_rate(info->port.tty, baud, baud);
Jiri Slaby80ff8a82008-02-07 00:16:51 -08001421 }
Jiri Slaby1c456072008-02-07 00:16:46 -08001422 }
1423
1424 info->type = new_serial.type;
1425
1426 process_txrx_fifo(info);
1427
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001428 if (info->port.flags & ASYNC_INITIALIZED) {
1429 if (flags != (info->port.flags & ASYNC_SPD_MASK)) {
Jiri Slaby1c456072008-02-07 00:16:46 -08001430 spin_lock_irqsave(&info->slock, sl_flags);
1431 mxser_change_speed(info, NULL);
1432 spin_unlock_irqrestore(&info->slock, sl_flags);
1433 }
1434 } else
1435 retval = mxser_startup(info);
1436
1437 return retval;
1438}
1439
1440/*
1441 * mxser_get_lsr_info - get line status register info
1442 *
1443 * Purpose: Let user call ioctl() to get info when the UART physically
1444 * is emptied. On bus types like RS485, the transmitter must
1445 * release the bus after transmitting. This must be done when
1446 * the transmit shift register is empty, not be done when the
1447 * transmit holding register is empty. This functionality
1448 * allows an RS485 driver to be written in user space.
1449 */
1450static int mxser_get_lsr_info(struct mxser_port *info,
1451 unsigned int __user *value)
1452{
1453 unsigned char status;
1454 unsigned int result;
1455 unsigned long flags;
1456
1457 spin_lock_irqsave(&info->slock, flags);
1458 status = inb(info->ioaddr + UART_LSR);
1459 spin_unlock_irqrestore(&info->slock, flags);
1460 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1461 return put_user(result, value);
1462}
1463
Jiri Slaby1c456072008-02-07 00:16:46 -08001464static int mxser_tiocmget(struct tty_struct *tty, struct file *file)
1465{
1466 struct mxser_port *info = tty->driver_data;
1467 unsigned char control, status;
1468 unsigned long flags;
1469
1470
1471 if (tty->index == MXSER_PORTS)
1472 return -ENOIOCTLCMD;
1473 if (test_bit(TTY_IO_ERROR, &tty->flags))
1474 return -EIO;
1475
1476 control = info->MCR;
1477
1478 spin_lock_irqsave(&info->slock, flags);
1479 status = inb(info->ioaddr + UART_MSR);
1480 if (status & UART_MSR_ANY_DELTA)
1481 mxser_check_modem_status(info, status);
1482 spin_unlock_irqrestore(&info->slock, flags);
1483 return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0) |
1484 ((control & UART_MCR_DTR) ? TIOCM_DTR : 0) |
1485 ((status & UART_MSR_DCD) ? TIOCM_CAR : 0) |
1486 ((status & UART_MSR_RI) ? TIOCM_RNG : 0) |
1487 ((status & UART_MSR_DSR) ? TIOCM_DSR : 0) |
1488 ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
1489}
1490
1491static int mxser_tiocmset(struct tty_struct *tty, struct file *file,
1492 unsigned int set, unsigned int clear)
1493{
1494 struct mxser_port *info = tty->driver_data;
1495 unsigned long flags;
1496
1497
1498 if (tty->index == MXSER_PORTS)
1499 return -ENOIOCTLCMD;
1500 if (test_bit(TTY_IO_ERROR, &tty->flags))
1501 return -EIO;
1502
1503 spin_lock_irqsave(&info->slock, flags);
1504
1505 if (set & TIOCM_RTS)
1506 info->MCR |= UART_MCR_RTS;
1507 if (set & TIOCM_DTR)
1508 info->MCR |= UART_MCR_DTR;
1509
1510 if (clear & TIOCM_RTS)
1511 info->MCR &= ~UART_MCR_RTS;
1512 if (clear & TIOCM_DTR)
1513 info->MCR &= ~UART_MCR_DTR;
1514
1515 outb(info->MCR, info->ioaddr + UART_MCR);
1516 spin_unlock_irqrestore(&info->slock, flags);
1517 return 0;
1518}
1519
1520static int __init mxser_program_mode(int port)
1521{
1522 int id, i, j, n;
1523
1524 outb(0, port);
1525 outb(0, port);
1526 outb(0, port);
1527 (void)inb(port);
1528 (void)inb(port);
1529 outb(0, port);
1530 (void)inb(port);
1531
1532 id = inb(port + 1) & 0x1F;
1533 if ((id != C168_ASIC_ID) &&
1534 (id != C104_ASIC_ID) &&
1535 (id != C102_ASIC_ID) &&
1536 (id != CI132_ASIC_ID) &&
1537 (id != CI134_ASIC_ID) &&
1538 (id != CI104J_ASIC_ID))
1539 return -1;
1540 for (i = 0, j = 0; i < 4; i++) {
1541 n = inb(port + 2);
1542 if (n == 'M') {
1543 j = 1;
1544 } else if ((j == 1) && (n == 1)) {
1545 j = 2;
1546 break;
1547 } else
1548 j = 0;
1549 }
1550 if (j != 2)
1551 id = -2;
1552 return id;
1553}
1554
1555static void __init mxser_normal_mode(int port)
1556{
1557 int i, n;
1558
1559 outb(0xA5, port + 1);
1560 outb(0x80, port + 3);
1561 outb(12, port + 0); /* 9600 bps */
1562 outb(0, port + 1);
1563 outb(0x03, port + 3); /* 8 data bits */
1564 outb(0x13, port + 4); /* loop back mode */
1565 for (i = 0; i < 16; i++) {
1566 n = inb(port + 5);
1567 if ((n & 0x61) == 0x60)
1568 break;
1569 if ((n & 1) == 1)
1570 (void)inb(port);
1571 }
1572 outb(0x00, port + 4);
1573}
1574
1575#define CHIP_SK 0x01 /* Serial Data Clock in Eprom */
1576#define CHIP_DO 0x02 /* Serial Data Output in Eprom */
1577#define CHIP_CS 0x04 /* Serial Chip Select in Eprom */
1578#define CHIP_DI 0x08 /* Serial Data Input in Eprom */
1579#define EN_CCMD 0x000 /* Chip's command register */
1580#define EN0_RSARLO 0x008 /* Remote start address reg 0 */
1581#define EN0_RSARHI 0x009 /* Remote start address reg 1 */
1582#define EN0_RCNTLO 0x00A /* Remote byte count reg WR */
1583#define EN0_RCNTHI 0x00B /* Remote byte count reg WR */
1584#define EN0_DCFG 0x00E /* Data configuration reg WR */
1585#define EN0_PORT 0x010 /* Rcv missed frame error counter RD */
1586#define ENC_PAGE0 0x000 /* Select page 0 of chip registers */
1587#define ENC_PAGE3 0x0C0 /* Select page 3 of chip registers */
1588static int __init mxser_read_register(int port, unsigned short *regs)
1589{
1590 int i, k, value, id;
1591 unsigned int j;
1592
1593 id = mxser_program_mode(port);
1594 if (id < 0)
1595 return id;
1596 for (i = 0; i < 14; i++) {
1597 k = (i & 0x3F) | 0x180;
1598 for (j = 0x100; j > 0; j >>= 1) {
1599 outb(CHIP_CS, port);
1600 if (k & j) {
1601 outb(CHIP_CS | CHIP_DO, port);
1602 outb(CHIP_CS | CHIP_DO | CHIP_SK, port); /* A? bit of read */
1603 } else {
1604 outb(CHIP_CS, port);
1605 outb(CHIP_CS | CHIP_SK, port); /* A? bit of read */
1606 }
1607 }
1608 (void)inb(port);
1609 value = 0;
1610 for (k = 0, j = 0x8000; k < 16; k++, j >>= 1) {
1611 outb(CHIP_CS, port);
1612 outb(CHIP_CS | CHIP_SK, port);
1613 if (inb(port) & CHIP_DI)
1614 value |= j;
1615 }
1616 regs[i] = value;
1617 outb(0, port);
1618 }
1619 mxser_normal_mode(port);
1620 return id;
1621}
1622
1623static int mxser_ioctl_special(unsigned int cmd, void __user *argp)
1624{
1625 struct mxser_port *port;
1626 int result, status;
1627 unsigned int i, j;
Alan Cox9d6d1622008-04-30 00:53:20 -07001628 int ret = 0;
Jiri Slaby1c456072008-02-07 00:16:46 -08001629
1630 switch (cmd) {
1631 case MOXA_GET_MAJOR:
Jiri Slaby41aee9a2008-07-25 01:48:19 -07001632 printk(KERN_WARNING "mxser: '%s' uses deprecated ioctl %x, fix "
1633 "your userspace\n", current->comm, cmd);
Jiri Slaby1c456072008-02-07 00:16:46 -08001634 return put_user(ttymajor, (int __user *)argp);
1635
1636 case MOXA_CHKPORTENABLE:
1637 result = 0;
Alan Cox9d6d1622008-04-30 00:53:20 -07001638 lock_kernel();
Jiri Slaby1c456072008-02-07 00:16:46 -08001639 for (i = 0; i < MXSER_BOARDS; i++)
1640 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++)
1641 if (mxser_boards[i].ports[j].ioaddr)
1642 result |= (1 << i);
Alan Cox9d6d1622008-04-30 00:53:20 -07001643 unlock_kernel();
Jiri Slaby1c456072008-02-07 00:16:46 -08001644 return put_user(result, (unsigned long __user *)argp);
1645 case MOXA_GETDATACOUNT:
Alan Cox9d6d1622008-04-30 00:53:20 -07001646 lock_kernel();
Jiri Slaby1c456072008-02-07 00:16:46 -08001647 if (copy_to_user(argp, &mxvar_log, sizeof(mxvar_log)))
Alan Cox9d6d1622008-04-30 00:53:20 -07001648 ret = -EFAULT;
1649 unlock_kernel();
1650 return ret;
Jiri Slaby72800df2008-07-25 01:48:20 -07001651 case MOXA_GETMSTATUS: {
1652 struct mxser_mstatus ms, __user *msu = argp;
Alan Cox9d6d1622008-04-30 00:53:20 -07001653 lock_kernel();
Jiri Slaby1c456072008-02-07 00:16:46 -08001654 for (i = 0; i < MXSER_BOARDS; i++)
1655 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
1656 port = &mxser_boards[i].ports[j];
Jiri Slaby72800df2008-07-25 01:48:20 -07001657 memset(&ms, 0, sizeof(ms));
Jiri Slaby1c456072008-02-07 00:16:46 -08001658
Jiri Slaby72800df2008-07-25 01:48:20 -07001659 if (!port->ioaddr)
1660 goto copy;
Jiri Slaby1c456072008-02-07 00:16:46 -08001661
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001662 if (!port->port.tty || !port->port.tty->termios)
Jiri Slaby72800df2008-07-25 01:48:20 -07001663 ms.cflag = port->normal_termios.c_cflag;
Jiri Slaby1c456072008-02-07 00:16:46 -08001664 else
Jiri Slaby72800df2008-07-25 01:48:20 -07001665 ms.cflag = port->port.tty->termios->c_cflag;
Jiri Slaby1c456072008-02-07 00:16:46 -08001666
1667 status = inb(port->ioaddr + UART_MSR);
Jiri Slaby72800df2008-07-25 01:48:20 -07001668 if (status & UART_MSR_DCD)
1669 ms.dcd = 1;
1670 if (status & UART_MSR_DSR)
1671 ms.dsr = 1;
1672 if (status & UART_MSR_CTS)
1673 ms.cts = 1;
1674 copy:
1675 if (copy_to_user(msu, &ms, sizeof(ms))) {
1676 unlock_kernel();
1677 return -EFAULT;
1678 }
1679 msu++;
Jiri Slaby1c456072008-02-07 00:16:46 -08001680 }
Alan Cox9d6d1622008-04-30 00:53:20 -07001681 unlock_kernel();
Jiri Slaby1c456072008-02-07 00:16:46 -08001682 return 0;
Jiri Slaby72800df2008-07-25 01:48:20 -07001683 }
Jiri Slaby1c456072008-02-07 00:16:46 -08001684 case MOXA_ASPP_MON_EXT: {
Jiri Slaby72800df2008-07-25 01:48:20 -07001685 struct mxser_mon_ext *me; /* it's 2k, stack unfriendly */
1686 unsigned int cflag, iflag, p;
1687 u8 opmode;
1688
1689 me = kzalloc(sizeof(*me), GFP_KERNEL);
1690 if (!me)
1691 return -ENOMEM;
Jiri Slaby1c456072008-02-07 00:16:46 -08001692
Alan Cox9d6d1622008-04-30 00:53:20 -07001693 lock_kernel();
Jiri Slaby72800df2008-07-25 01:48:20 -07001694 for (i = 0, p = 0; i < MXSER_BOARDS; i++) {
1695 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++, p++) {
1696 if (p >= ARRAY_SIZE(me->rx_cnt)) {
1697 i = MXSER_BOARDS;
1698 break;
1699 }
Jiri Slaby1c456072008-02-07 00:16:46 -08001700 port = &mxser_boards[i].ports[j];
1701 if (!port->ioaddr)
1702 continue;
1703
Jiri Slaby72800df2008-07-25 01:48:20 -07001704 status = mxser_get_msr(port->ioaddr, 0, p);
Jiri Slaby1c456072008-02-07 00:16:46 -08001705
1706 if (status & UART_MSR_TERI)
1707 port->icount.rng++;
1708 if (status & UART_MSR_DDSR)
1709 port->icount.dsr++;
1710 if (status & UART_MSR_DDCD)
1711 port->icount.dcd++;
1712 if (status & UART_MSR_DCTS)
1713 port->icount.cts++;
1714
1715 port->mon_data.modem_status = status;
Jiri Slaby72800df2008-07-25 01:48:20 -07001716 me->rx_cnt[p] = port->mon_data.rxcnt;
1717 me->tx_cnt[p] = port->mon_data.txcnt;
1718 me->up_rxcnt[p] = port->mon_data.up_rxcnt;
1719 me->up_txcnt[p] = port->mon_data.up_txcnt;
1720 me->modem_status[p] =
Jiri Slaby1c456072008-02-07 00:16:46 -08001721 port->mon_data.modem_status;
Jiri Slaby72800df2008-07-25 01:48:20 -07001722 me->baudrate[p] = tty_get_baud_rate(port->port.tty);
Jiri Slaby1c456072008-02-07 00:16:46 -08001723
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001724 if (!port->port.tty || !port->port.tty->termios) {
Jiri Slaby1c456072008-02-07 00:16:46 -08001725 cflag = port->normal_termios.c_cflag;
1726 iflag = port->normal_termios.c_iflag;
1727 } else {
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001728 cflag = port->port.tty->termios->c_cflag;
1729 iflag = port->port.tty->termios->c_iflag;
Jiri Slaby1c456072008-02-07 00:16:46 -08001730 }
1731
Jiri Slaby72800df2008-07-25 01:48:20 -07001732 me->databits[p] = cflag & CSIZE;
1733 me->stopbits[p] = cflag & CSTOPB;
1734 me->parity[p] = cflag & (PARENB | PARODD |
1735 CMSPAR);
Jiri Slaby1c456072008-02-07 00:16:46 -08001736
1737 if (cflag & CRTSCTS)
Jiri Slaby72800df2008-07-25 01:48:20 -07001738 me->flowctrl[p] |= 0x03;
Jiri Slaby1c456072008-02-07 00:16:46 -08001739
1740 if (iflag & (IXON | IXOFF))
Jiri Slaby72800df2008-07-25 01:48:20 -07001741 me->flowctrl[p] |= 0x0C;
Jiri Slaby1c456072008-02-07 00:16:46 -08001742
1743 if (port->type == PORT_16550A)
Jiri Slaby72800df2008-07-25 01:48:20 -07001744 me->fifo[p] = 1;
Jiri Slaby1c456072008-02-07 00:16:46 -08001745
Jiri Slaby72800df2008-07-25 01:48:20 -07001746 opmode = inb(port->opmode_ioaddr) >>
1747 ((p % 4) * 2);
Jiri Slaby1c456072008-02-07 00:16:46 -08001748 opmode &= OP_MODE_MASK;
Jiri Slaby72800df2008-07-25 01:48:20 -07001749 me->iftype[p] = opmode;
Jiri Slaby1c456072008-02-07 00:16:46 -08001750 }
Alan Cox9d6d1622008-04-30 00:53:20 -07001751 }
1752 unlock_kernel();
Jiri Slaby72800df2008-07-25 01:48:20 -07001753 if (copy_to_user(argp, me, sizeof(*me)))
1754 ret = -EFAULT;
1755 kfree(me);
1756 return ret;
Alan Cox9d6d1622008-04-30 00:53:20 -07001757 }
1758 default:
Jiri Slaby1c456072008-02-07 00:16:46 -08001759 return -ENOIOCTLCMD;
1760 }
1761 return 0;
1762}
1763
1764static int mxser_cflags_changed(struct mxser_port *info, unsigned long arg,
1765 struct async_icount *cprev)
1766{
1767 struct async_icount cnow;
1768 unsigned long flags;
1769 int ret;
1770
1771 spin_lock_irqsave(&info->slock, flags);
1772 cnow = info->icount; /* atomic copy */
1773 spin_unlock_irqrestore(&info->slock, flags);
1774
1775 ret = ((arg & TIOCM_RNG) && (cnow.rng != cprev->rng)) ||
1776 ((arg & TIOCM_DSR) && (cnow.dsr != cprev->dsr)) ||
1777 ((arg & TIOCM_CD) && (cnow.dcd != cprev->dcd)) ||
1778 ((arg & TIOCM_CTS) && (cnow.cts != cprev->cts));
1779
1780 *cprev = cnow;
1781
1782 return ret;
1783}
1784
1785static int mxser_ioctl(struct tty_struct *tty, struct file *file,
1786 unsigned int cmd, unsigned long arg)
1787{
1788 struct mxser_port *info = tty->driver_data;
1789 struct async_icount cnow;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 unsigned long flags;
1791 void __user *argp = (void __user *)arg;
Jiri Slaby1c456072008-02-07 00:16:46 -08001792 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
1794 if (tty->index == MXSER_PORTS)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001795 return mxser_ioctl_special(cmd, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 if (cmd == MOXA_SET_OP_MODE || cmd == MOXA_GET_OP_MODE) {
Jiri Slaby1c456072008-02-07 00:16:46 -08001798 int p;
1799 unsigned long opmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 static unsigned char ModeMask[] = { 0xfc, 0xf3, 0xcf, 0x3f };
1801 int shiftbit;
1802 unsigned char val, mask;
1803
Jiri Slaby1c456072008-02-07 00:16:46 -08001804 p = tty->index % 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 if (cmd == MOXA_SET_OP_MODE) {
1806 if (get_user(opmode, (int __user *) argp))
1807 return -EFAULT;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001808 if (opmode != RS232_MODE &&
1809 opmode != RS485_2WIRE_MODE &&
1810 opmode != RS422_MODE &&
1811 opmode != RS485_4WIRE_MODE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 return -EFAULT;
Alan Cox9d6d1622008-04-30 00:53:20 -07001813 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 mask = ModeMask[p];
1815 shiftbit = p * 2;
1816 val = inb(info->opmode_ioaddr);
1817 val &= mask;
1818 val |= (opmode << shiftbit);
1819 outb(val, info->opmode_ioaddr);
Alan Cox9d6d1622008-04-30 00:53:20 -07001820 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 } else {
Alan Cox9d6d1622008-04-30 00:53:20 -07001822 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 shiftbit = p * 2;
1824 opmode = inb(info->opmode_ioaddr) >> shiftbit;
1825 opmode &= OP_MODE_MASK;
Alan Cox9d6d1622008-04-30 00:53:20 -07001826 unlock_kernel();
Jiri Slaby1c456072008-02-07 00:16:46 -08001827 if (put_user(opmode, (int __user *)argp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 return -EFAULT;
1829 }
1830 return 0;
1831 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832
Jiri Slaby1c456072008-02-07 00:16:46 -08001833 if (cmd != TIOCGSERIAL && cmd != TIOCMIWAIT && cmd != TIOCGICOUNT &&
1834 test_bit(TTY_IO_ERROR, &tty->flags))
1835 return -EIO;
1836
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 case TIOCGSERIAL:
Alan Cox9d6d1622008-04-30 00:53:20 -07001839 lock_kernel();
1840 retval = mxser_get_serial_info(info, argp);
1841 unlock_kernel();
1842 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 case TIOCSSERIAL:
Alan Cox9d6d1622008-04-30 00:53:20 -07001844 lock_kernel();
1845 retval = mxser_set_serial_info(info, argp);
1846 unlock_kernel();
1847 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 case TIOCSERGETLSR: /* Get line status register */
Alan Cox9d6d1622008-04-30 00:53:20 -07001849 return mxser_get_lsr_info(info, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 /*
1851 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1852 * - mask passed in arg for lines of interest
1853 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1854 * Caller should use TIOCGICOUNT to see which one it was
1855 */
Jiri Slabyfc838152007-04-23 14:41:04 -07001856 case TIOCMIWAIT:
1857 spin_lock_irqsave(&info->slock, flags);
1858 cnow = info->icount; /* note the counters on entry */
1859 spin_unlock_irqrestore(&info->slock, flags);
1860
Jiri Slaby1c456072008-02-07 00:16:46 -08001861 return wait_event_interruptible(info->delta_msr_wait,
1862 mxser_cflags_changed(info, arg, &cnow));
1863 /*
1864 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1865 * Return: write counters to the user passed counter struct
1866 * NB: both 1->0 and 0->1 transitions are counted except for
1867 * RI where only 0->1 is counted.
1868 */
Jiri Slaby41aee9a2008-07-25 01:48:19 -07001869 case TIOCGICOUNT: {
1870 struct serial_icounter_struct icnt = { 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 spin_lock_irqsave(&info->slock, flags);
1872 cnow = info->icount;
1873 spin_unlock_irqrestore(&info->slock, flags);
Jiri Slaby41aee9a2008-07-25 01:48:19 -07001874
1875 icnt.frame = cnow.frame;
1876 icnt.brk = cnow.brk;
1877 icnt.overrun = cnow.overrun;
1878 icnt.buf_overrun = cnow.buf_overrun;
1879 icnt.parity = cnow.parity;
1880 icnt.rx = cnow.rx;
1881 icnt.tx = cnow.tx;
1882 icnt.cts = cnow.cts;
1883 icnt.dsr = cnow.dsr;
1884 icnt.rng = cnow.rng;
1885 icnt.dcd = cnow.dcd;
1886
1887 return copy_to_user(argp, &icnt, sizeof(icnt)) ? -EFAULT : 0;
1888 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 case MOXA_HighSpeedOn:
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001890 return put_user(info->baud_base != 115200 ? 1 : 0, (int __user *)argp);
Jiri Slaby1c456072008-02-07 00:16:46 -08001891 case MOXA_SDS_RSTICOUNTER:
Alan Cox9d6d1622008-04-30 00:53:20 -07001892 lock_kernel();
Jiri Slaby1c456072008-02-07 00:16:46 -08001893 info->mon_data.rxcnt = 0;
1894 info->mon_data.txcnt = 0;
Alan Cox9d6d1622008-04-30 00:53:20 -07001895 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 return 0;
1897
1898 case MOXA_ASPP_OQUEUE:{
Jiri Slaby1c456072008-02-07 00:16:46 -08001899 int len, lsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
Alan Cox9d6d1622008-04-30 00:53:20 -07001901 lock_kernel();
Jiri Slaby1c456072008-02-07 00:16:46 -08001902 len = mxser_chars_in_buffer(tty);
Jiri Slaby1c456072008-02-07 00:16:46 -08001903 lsr = inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT;
Jiri Slaby1c456072008-02-07 00:16:46 -08001904 len += (lsr ? 0 : 1);
Alan Cox9d6d1622008-04-30 00:53:20 -07001905 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906
Jiri Slaby1c456072008-02-07 00:16:46 -08001907 return put_user(len, (int __user *)argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 }
Jiri Slaby1c456072008-02-07 00:16:46 -08001909 case MOXA_ASPP_MON: {
1910 int mcr, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911
Alan Cox9d6d1622008-04-30 00:53:20 -07001912 lock_kernel();
Jiri Slaby1c456072008-02-07 00:16:46 -08001913 status = mxser_get_msr(info->ioaddr, 1, tty->index);
1914 mxser_check_modem_status(info, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
Jiri Slaby1c456072008-02-07 00:16:46 -08001916 mcr = inb(info->ioaddr + UART_MCR);
1917 if (mcr & MOXA_MUST_MCR_XON_FLAG)
1918 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFHOLD;
1919 else
1920 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFHOLD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921
Jiri Slaby1c456072008-02-07 00:16:46 -08001922 if (mcr & MOXA_MUST_MCR_TX_XON)
1923 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFXENT;
1924 else
1925 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFXENT;
1926
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001927 if (info->port.tty->hw_stopped)
Jiri Slaby1c456072008-02-07 00:16:46 -08001928 info->mon_data.hold_reason |= NPPI_NOTIFY_CTSHOLD;
1929 else
1930 info->mon_data.hold_reason &= ~NPPI_NOTIFY_CTSHOLD;
Alan Cox9d6d1622008-04-30 00:53:20 -07001931 unlock_kernel();
Jiri Slaby1c456072008-02-07 00:16:46 -08001932 if (copy_to_user(argp, &info->mon_data,
1933 sizeof(struct mxser_mon)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 return -EFAULT;
Jiri Slaby1c456072008-02-07 00:16:46 -08001935
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 return 0;
Jiri Slaby1c456072008-02-07 00:16:46 -08001937 }
1938 case MOXA_ASPP_LSTATUS: {
1939 if (put_user(info->err_shadow, (unsigned char __user *)argp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941
Jiri Slaby1c456072008-02-07 00:16:46 -08001942 info->err_shadow = 0;
1943 return 0;
1944 }
1945 case MOXA_SET_BAUD_METHOD: {
1946 int method;
1947
1948 if (get_user(method, (int __user *)argp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 return -EFAULT;
Jiri Slaby1c456072008-02-07 00:16:46 -08001950 mxser_set_baud_method[tty->index] = method;
1951 return put_user(method, (int __user *)argp);
1952 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 default:
1954 return -ENOIOCTLCMD;
1955 }
1956 return 0;
1957}
1958
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959static void mxser_stoprx(struct tty_struct *tty)
1960{
Jiri Slaby1c456072008-02-07 00:16:46 -08001961 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
1963 info->ldisc_stop_rx = 1;
1964 if (I_IXOFF(tty)) {
Jiri Slaby1c456072008-02-07 00:16:46 -08001965 if (info->board->chip_flag) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 info->IER &= ~MOXA_MUST_RECV_ISR;
Jiri Slaby1c456072008-02-07 00:16:46 -08001967 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 info->x_char = STOP_CHAR(tty);
Jiri Slaby1c456072008-02-07 00:16:46 -08001970 outb(0, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 info->IER |= UART_IER_THRI;
Jiri Slaby1c456072008-02-07 00:16:46 -08001972 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 }
1974 }
1975
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001976 if (info->port.tty->termios->c_cflag & CRTSCTS) {
Jiri Slaby1c456072008-02-07 00:16:46 -08001977 info->MCR &= ~UART_MCR_RTS;
1978 outb(info->MCR, info->ioaddr + UART_MCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 }
1980}
1981
1982/*
1983 * This routine is called by the upper-layer tty layer to signal that
1984 * incoming characters should be throttled.
1985 */
1986static void mxser_throttle(struct tty_struct *tty)
1987{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 mxser_stoprx(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989}
1990
1991static void mxser_unthrottle(struct tty_struct *tty)
1992{
Jiri Slaby1c456072008-02-07 00:16:46 -08001993 struct mxser_port *info = tty->driver_data;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001994
Jiri Slaby1c456072008-02-07 00:16:46 -08001995 /* startrx */
1996 info->ldisc_stop_rx = 0;
1997 if (I_IXOFF(tty)) {
1998 if (info->x_char)
1999 info->x_char = 0;
2000 else {
2001 if (info->board->chip_flag) {
2002 info->IER |= MOXA_MUST_RECV_ISR;
2003 outb(info->IER, info->ioaddr + UART_IER);
2004 } else {
2005 info->x_char = START_CHAR(tty);
2006 outb(0, info->ioaddr + UART_IER);
2007 info->IER |= UART_IER_THRI;
2008 outb(info->IER, info->ioaddr + UART_IER);
2009 }
2010 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 }
2012
Alan Cox0ad9e7d2008-07-16 21:56:10 +01002013 if (info->port.tty->termios->c_cflag & CRTSCTS) {
Jiri Slaby1c456072008-02-07 00:16:46 -08002014 info->MCR |= UART_MCR_RTS;
2015 outb(info->MCR, info->ioaddr + UART_MCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 }
2017}
2018
2019/*
2020 * mxser_stop() and mxser_start()
2021 *
2022 * This routines are called before setting or resetting tty->stopped.
2023 * They enable or disable transmitter interrupts, as necessary.
2024 */
2025static void mxser_stop(struct tty_struct *tty)
2026{
Jiri Slaby1c456072008-02-07 00:16:46 -08002027 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 unsigned long flags;
2029
2030 spin_lock_irqsave(&info->slock, flags);
2031 if (info->IER & UART_IER_THRI) {
2032 info->IER &= ~UART_IER_THRI;
Jiri Slaby1c456072008-02-07 00:16:46 -08002033 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 }
2035 spin_unlock_irqrestore(&info->slock, flags);
2036}
2037
2038static void mxser_start(struct tty_struct *tty)
2039{
Jiri Slaby1c456072008-02-07 00:16:46 -08002040 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 unsigned long flags;
2042
2043 spin_lock_irqsave(&info->slock, flags);
Alan Cox0ad9e7d2008-07-16 21:56:10 +01002044 if (info->xmit_cnt && info->port.xmit_buf) {
Jiri Slaby1c456072008-02-07 00:16:46 -08002045 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 info->IER |= UART_IER_THRI;
Jiri Slaby1c456072008-02-07 00:16:46 -08002047 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 }
2049 spin_unlock_irqrestore(&info->slock, flags);
2050}
2051
Jiri Slaby1c456072008-02-07 00:16:46 -08002052static void mxser_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
2053{
2054 struct mxser_port *info = tty->driver_data;
2055 unsigned long flags;
2056
2057 spin_lock_irqsave(&info->slock, flags);
2058 mxser_change_speed(info, old_termios);
2059 spin_unlock_irqrestore(&info->slock, flags);
2060
2061 if ((old_termios->c_cflag & CRTSCTS) &&
2062 !(tty->termios->c_cflag & CRTSCTS)) {
2063 tty->hw_stopped = 0;
2064 mxser_start(tty);
2065 }
2066
2067 /* Handle sw stopped */
2068 if ((old_termios->c_iflag & IXON) &&
2069 !(tty->termios->c_iflag & IXON)) {
2070 tty->stopped = 0;
2071
2072 if (info->board->chip_flag) {
2073 spin_lock_irqsave(&info->slock, flags);
Christoph Hellwig148ff862008-04-30 00:54:29 -07002074 mxser_disable_must_rx_software_flow_control(
2075 info->ioaddr);
Jiri Slaby1c456072008-02-07 00:16:46 -08002076 spin_unlock_irqrestore(&info->slock, flags);
2077 }
2078
2079 mxser_start(tty);
2080 }
2081}
2082
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083/*
2084 * mxser_wait_until_sent() --- wait until the transmitter is empty
2085 */
2086static void mxser_wait_until_sent(struct tty_struct *tty, int timeout)
2087{
Jiri Slaby1c456072008-02-07 00:16:46 -08002088 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 unsigned long orig_jiffies, char_time;
2090 int lsr;
2091
2092 if (info->type == PORT_UNKNOWN)
2093 return;
2094
2095 if (info->xmit_fifo_size == 0)
2096 return; /* Just in case.... */
2097
2098 orig_jiffies = jiffies;
2099 /*
2100 * Set the check interval to be 1/5 of the estimated time to
2101 * send a single character, and make it at least 1. The check
2102 * interval should also be less than the timeout.
2103 *
2104 * Note: we have to use pretty tight timings here to satisfy
2105 * the NIST-PCTS.
2106 */
2107 char_time = (info->timeout - HZ / 50) / info->xmit_fifo_size;
2108 char_time = char_time / 5;
2109 if (char_time == 0)
2110 char_time = 1;
2111 if (timeout && timeout < char_time)
2112 char_time = timeout;
2113 /*
2114 * If the transmitter hasn't cleared in twice the approximate
2115 * amount of time to send the entire FIFO, it probably won't
2116 * ever clear. This assumes the UART isn't doing flow
2117 * control, which is currently the case. Hence, if it ever
2118 * takes longer than info->timeout, this is probably due to a
2119 * UART bug of some kind. So, we clamp the timeout parameter at
2120 * 2*info->timeout.
2121 */
2122 if (!timeout || timeout > 2 * info->timeout)
2123 timeout = 2 * info->timeout;
2124#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002125 printk(KERN_DEBUG "In rs_wait_until_sent(%d) check=%lu...",
2126 timeout, char_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 printk("jiff=%lu...", jiffies);
2128#endif
Alan Cox978e5952008-04-30 00:53:59 -07002129 lock_kernel();
Jiri Slaby1c456072008-02-07 00:16:46 -08002130 while (!((lsr = inb(info->ioaddr + UART_LSR)) & UART_LSR_TEMT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2132 printk("lsr = %d (jiff=%lu)...", lsr, jiffies);
2133#endif
Nishanth Aravamudanda4cd8d2005-09-10 00:27:30 -07002134 schedule_timeout_interruptible(char_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 if (signal_pending(current))
2136 break;
2137 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2138 break;
2139 }
2140 set_current_state(TASK_RUNNING);
Alan Cox978e5952008-04-30 00:53:59 -07002141 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142
2143#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2144 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
2145#endif
2146}
2147
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148/*
2149 * This routine is called by tty_hangup() when a hangup is signaled.
2150 */
Jiri Slaby1c456072008-02-07 00:16:46 -08002151static void mxser_hangup(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152{
Jiri Slaby1c456072008-02-07 00:16:46 -08002153 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154
2155 mxser_flush_buffer(tty);
2156 mxser_shutdown(info);
Alan Cox0ad9e7d2008-07-16 21:56:10 +01002157 info->port.count = 0;
2158 info->port.flags &= ~ASYNC_NORMAL_ACTIVE;
2159 info->port.tty = NULL;
2160 wake_up_interruptible(&info->port.open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161}
2162
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163/*
2164 * mxser_rs_break() --- routine which turns the break handling on or off
2165 */
Alan Cox9e989662008-07-22 11:18:03 +01002166static int mxser_rs_break(struct tty_struct *tty, int break_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167{
Jiri Slaby1c456072008-02-07 00:16:46 -08002168 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 unsigned long flags;
2170
2171 spin_lock_irqsave(&info->slock, flags);
2172 if (break_state == -1)
Jiri Slaby1c456072008-02-07 00:16:46 -08002173 outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
2174 info->ioaddr + UART_LCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 else
Jiri Slaby1c456072008-02-07 00:16:46 -08002176 outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
2177 info->ioaddr + UART_LCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 spin_unlock_irqrestore(&info->slock, flags);
Alan Cox9e989662008-07-22 11:18:03 +01002179 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180}
2181
Jiri Slaby1c456072008-02-07 00:16:46 -08002182static void mxser_receive_chars(struct mxser_port *port, int *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183{
Alan Cox0ad9e7d2008-07-16 21:56:10 +01002184 struct tty_struct *tty = port->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 unsigned char ch, gdl;
2186 int ignored = 0;
2187 int cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 int recv_room;
2189 int max = 256;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190
Alan Cox33f0f882006-01-09 20:54:13 -08002191 recv_room = tty->receive_room;
Jiri Slaby1c456072008-02-07 00:16:46 -08002192 if ((recv_room == 0) && (!port->ldisc_stop_rx))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 mxser_stoprx(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194
Jiri Slaby1c456072008-02-07 00:16:46 -08002195 if (port->board->chip_flag != MOXA_OTHER_UART) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196
Jiri Slaby1c456072008-02-07 00:16:46 -08002197 if (*status & UART_LSR_SPECIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 goto intr_old;
Jiri Slaby1c456072008-02-07 00:16:46 -08002199 if (port->board->chip_flag == MOXA_MUST_MU860_HWID &&
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002200 (*status & MOXA_MUST_LSR_RERR))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 goto intr_old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 if (*status & MOXA_MUST_LSR_RERR)
2203 goto intr_old;
2204
Jiri Slaby1c456072008-02-07 00:16:46 -08002205 gdl = inb(port->ioaddr + MOXA_MUST_GDL_REGISTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206
Jiri Slaby1c456072008-02-07 00:16:46 -08002207 if (port->board->chip_flag == MOXA_MUST_MU150_HWID)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 gdl &= MOXA_MUST_GDL_MASK;
2209 if (gdl >= recv_room) {
Jiri Slaby1c456072008-02-07 00:16:46 -08002210 if (!port->ldisc_stop_rx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 mxser_stoprx(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 }
2213 while (gdl--) {
Jiri Slaby1c456072008-02-07 00:16:46 -08002214 ch = inb(port->ioaddr + UART_RX);
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002215 tty_insert_flip_char(tty, ch, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 }
2218 goto end_intr;
2219 }
Jiri Slaby1c456072008-02-07 00:16:46 -08002220intr_old:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221
2222 do {
2223 if (max-- < 0)
2224 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225
Jiri Slaby1c456072008-02-07 00:16:46 -08002226 ch = inb(port->ioaddr + UART_RX);
2227 if (port->board->chip_flag && (*status & UART_LSR_OE))
2228 outb(0x23, port->ioaddr + UART_FCR);
2229 *status &= port->read_status_mask;
2230 if (*status & port->ignore_status_mask) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 if (++ignored > 100)
2232 break;
2233 } else {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002234 char flag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 if (*status & UART_LSR_SPECIAL) {
2236 if (*status & UART_LSR_BI) {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002237 flag = TTY_BREAK;
Jiri Slaby1c456072008-02-07 00:16:46 -08002238 port->icount.brk++;
2239
Alan Cox0ad9e7d2008-07-16 21:56:10 +01002240 if (port->port.flags & ASYNC_SAK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 do_SAK(tty);
2242 } else if (*status & UART_LSR_PE) {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002243 flag = TTY_PARITY;
Jiri Slaby1c456072008-02-07 00:16:46 -08002244 port->icount.parity++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 } else if (*status & UART_LSR_FE) {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002246 flag = TTY_FRAME;
Jiri Slaby1c456072008-02-07 00:16:46 -08002247 port->icount.frame++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 } else if (*status & UART_LSR_OE) {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002249 flag = TTY_OVERRUN;
Jiri Slaby1c456072008-02-07 00:16:46 -08002250 port->icount.overrun++;
2251 } else
2252 flag = TTY_BREAK;
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002253 }
2254 tty_insert_flip_char(tty, ch, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 cnt++;
2256 if (cnt >= recv_room) {
Jiri Slaby1c456072008-02-07 00:16:46 -08002257 if (!port->ldisc_stop_rx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 mxser_stoprx(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 break;
2260 }
2261
2262 }
2263
Jiri Slaby1c456072008-02-07 00:16:46 -08002264 if (port->board->chip_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266
Jiri Slaby1c456072008-02-07 00:16:46 -08002267 *status = inb(port->ioaddr + UART_LSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 } while (*status & UART_LSR_DR);
2269
Jiri Slaby1c456072008-02-07 00:16:46 -08002270end_intr:
Alan Cox0ad9e7d2008-07-16 21:56:10 +01002271 mxvar_log.rxcnt[port->port.tty->index] += cnt;
Jiri Slaby1c456072008-02-07 00:16:46 -08002272 port->mon_data.rxcnt += cnt;
2273 port->mon_data.up_rxcnt += cnt;
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002274
Jiri Slaby1c456072008-02-07 00:16:46 -08002275 /*
2276 * We are called from an interrupt context with &port->slock
2277 * being held. Drop it temporarily in order to prevent
2278 * recursive locking.
2279 */
2280 spin_unlock(&port->slock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 tty_flip_buffer_push(tty);
Jiri Slaby1c456072008-02-07 00:16:46 -08002282 spin_lock(&port->slock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283}
2284
Jiri Slaby1c456072008-02-07 00:16:46 -08002285static void mxser_transmit_chars(struct mxser_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286{
2287 int count, cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288
Jiri Slaby1c456072008-02-07 00:16:46 -08002289 if (port->x_char) {
2290 outb(port->x_char, port->ioaddr + UART_TX);
2291 port->x_char = 0;
Alan Cox0ad9e7d2008-07-16 21:56:10 +01002292 mxvar_log.txcnt[port->port.tty->index]++;
Jiri Slaby1c456072008-02-07 00:16:46 -08002293 port->mon_data.txcnt++;
2294 port->mon_data.up_txcnt++;
2295 port->icount.tx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 return;
2297 }
2298
Alan Cox0ad9e7d2008-07-16 21:56:10 +01002299 if (port->port.xmit_buf == NULL)
Jiri Slaby1c456072008-02-07 00:16:46 -08002300 return;
2301
Alan Cox0ad9e7d2008-07-16 21:56:10 +01002302 if ((port->xmit_cnt <= 0) || port->port.tty->stopped ||
2303 (port->port.tty->hw_stopped &&
Jiri Slaby1c456072008-02-07 00:16:46 -08002304 (port->type != PORT_16550A) &&
2305 (!port->board->chip_flag))) {
2306 port->IER &= ~UART_IER_THRI;
2307 outb(port->IER, port->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 return;
2309 }
2310
Jiri Slaby1c456072008-02-07 00:16:46 -08002311 cnt = port->xmit_cnt;
2312 count = port->xmit_fifo_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 do {
Alan Cox0ad9e7d2008-07-16 21:56:10 +01002314 outb(port->port.xmit_buf[port->xmit_tail++],
Jiri Slaby1c456072008-02-07 00:16:46 -08002315 port->ioaddr + UART_TX);
2316 port->xmit_tail = port->xmit_tail & (SERIAL_XMIT_SIZE - 1);
2317 if (--port->xmit_cnt <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 break;
2319 } while (--count > 0);
Alan Cox0ad9e7d2008-07-16 21:56:10 +01002320 mxvar_log.txcnt[port->port.tty->index] += (cnt - port->xmit_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321
Jiri Slaby1c456072008-02-07 00:16:46 -08002322 port->mon_data.txcnt += (cnt - port->xmit_cnt);
2323 port->mon_data.up_txcnt += (cnt - port->xmit_cnt);
2324 port->icount.tx += (cnt - port->xmit_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325
Jiri Slaby1c456072008-02-07 00:16:46 -08002326 if (port->xmit_cnt < WAKEUP_CHARS)
Alan Cox0ad9e7d2008-07-16 21:56:10 +01002327 tty_wakeup(port->port.tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328
Jiri Slaby1c456072008-02-07 00:16:46 -08002329 if (port->xmit_cnt <= 0) {
2330 port->IER &= ~UART_IER_THRI;
2331 outb(port->IER, port->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333}
2334
2335/*
Jiri Slaby1c456072008-02-07 00:16:46 -08002336 * This is the serial driver's generic interrupt routine
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 */
Jiri Slaby1c456072008-02-07 00:16:46 -08002338static irqreturn_t mxser_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339{
Jiri Slaby1c456072008-02-07 00:16:46 -08002340 int status, iir, i;
2341 struct mxser_board *brd = NULL;
2342 struct mxser_port *port;
2343 int max, irqbits, bits, msr;
2344 unsigned int int_cnt, pass_counter = 0;
2345 int handled = IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346
Jiri Slaby1c456072008-02-07 00:16:46 -08002347 for (i = 0; i < MXSER_BOARDS; i++)
2348 if (dev_id == &mxser_boards[i]) {
2349 brd = dev_id;
2350 break;
2351 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352
Jiri Slaby1c456072008-02-07 00:16:46 -08002353 if (i == MXSER_BOARDS)
2354 goto irq_stop;
2355 if (brd == NULL)
2356 goto irq_stop;
2357 max = brd->info->nports;
2358 while (pass_counter++ < MXSER_ISR_PASS_LIMIT) {
2359 irqbits = inb(brd->vector) & brd->vector_mask;
2360 if (irqbits == brd->vector_mask)
2361 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362
Jiri Slaby1c456072008-02-07 00:16:46 -08002363 handled = IRQ_HANDLED;
2364 for (i = 0, bits = 1; i < max; i++, irqbits |= bits, bits <<= 1) {
2365 if (irqbits == brd->vector_mask)
2366 break;
2367 if (bits & irqbits)
2368 continue;
2369 port = &brd->ports[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370
Jiri Slaby1c456072008-02-07 00:16:46 -08002371 int_cnt = 0;
2372 spin_lock(&port->slock);
2373 do {
2374 iir = inb(port->ioaddr + UART_IIR);
2375 if (iir & UART_IIR_NO_INT)
2376 break;
2377 iir &= MOXA_MUST_IIR_MASK;
Alan Cox0ad9e7d2008-07-16 21:56:10 +01002378 if (!port->port.tty ||
2379 (port->port.flags & ASYNC_CLOSING) ||
2380 !(port->port.flags &
Jiri Slaby1c456072008-02-07 00:16:46 -08002381 ASYNC_INITIALIZED)) {
2382 status = inb(port->ioaddr + UART_LSR);
2383 outb(0x27, port->ioaddr + UART_FCR);
2384 inb(port->ioaddr + UART_MSR);
2385 break;
2386 }
2387
2388 status = inb(port->ioaddr + UART_LSR);
2389
2390 if (status & UART_LSR_PE)
2391 port->err_shadow |= NPPI_NOTIFY_PARITY;
2392 if (status & UART_LSR_FE)
2393 port->err_shadow |= NPPI_NOTIFY_FRAMING;
2394 if (status & UART_LSR_OE)
2395 port->err_shadow |=
2396 NPPI_NOTIFY_HW_OVERRUN;
2397 if (status & UART_LSR_BI)
2398 port->err_shadow |= NPPI_NOTIFY_BREAK;
2399
2400 if (port->board->chip_flag) {
2401 if (iir == MOXA_MUST_IIR_GDA ||
2402 iir == MOXA_MUST_IIR_RDA ||
2403 iir == MOXA_MUST_IIR_RTO ||
2404 iir == MOXA_MUST_IIR_LSR)
2405 mxser_receive_chars(port,
2406 &status);
2407
2408 } else {
2409 status &= port->read_status_mask;
2410 if (status & UART_LSR_DR)
2411 mxser_receive_chars(port,
2412 &status);
2413 }
2414 msr = inb(port->ioaddr + UART_MSR);
2415 if (msr & UART_MSR_ANY_DELTA)
2416 mxser_check_modem_status(port, msr);
2417
2418 if (port->board->chip_flag) {
2419 if (iir == 0x02 && (status &
2420 UART_LSR_THRE))
2421 mxser_transmit_chars(port);
2422 } else {
2423 if (status & UART_LSR_THRE)
2424 mxser_transmit_chars(port);
2425 }
2426 } while (int_cnt++ < MXSER_ISR_PASS_LIMIT);
2427 spin_unlock(&port->slock);
2428 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 }
2430
Jiri Slaby1c456072008-02-07 00:16:46 -08002431irq_stop:
2432 return handled;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433}
2434
Jiri Slaby1c456072008-02-07 00:16:46 -08002435static const struct tty_operations mxser_ops = {
2436 .open = mxser_open,
2437 .close = mxser_close,
2438 .write = mxser_write,
2439 .put_char = mxser_put_char,
2440 .flush_chars = mxser_flush_chars,
2441 .write_room = mxser_write_room,
2442 .chars_in_buffer = mxser_chars_in_buffer,
2443 .flush_buffer = mxser_flush_buffer,
2444 .ioctl = mxser_ioctl,
2445 .throttle = mxser_throttle,
2446 .unthrottle = mxser_unthrottle,
2447 .set_termios = mxser_set_termios,
2448 .stop = mxser_stop,
2449 .start = mxser_start,
2450 .hangup = mxser_hangup,
2451 .break_ctl = mxser_rs_break,
2452 .wait_until_sent = mxser_wait_until_sent,
2453 .tiocmget = mxser_tiocmget,
2454 .tiocmset = mxser_tiocmset,
2455};
2456
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457/*
Jiri Slaby1c456072008-02-07 00:16:46 -08002458 * The MOXA Smartio/Industio serial driver boot-time initialization code!
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 */
Jiri Slaby1c456072008-02-07 00:16:46 -08002460
2461static void mxser_release_res(struct mxser_board *brd, struct pci_dev *pdev,
2462 unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463{
Jiri Slaby1c456072008-02-07 00:16:46 -08002464 if (irq)
2465 free_irq(brd->irq, brd);
2466 if (pdev != NULL) { /* PCI */
2467#ifdef CONFIG_PCI
2468 pci_release_region(pdev, 2);
2469 pci_release_region(pdev, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 } else {
Jiri Slaby1c456072008-02-07 00:16:46 -08002472 release_region(brd->ports[0].ioaddr, 8 * brd->info->nports);
2473 release_region(brd->vector, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475}
2476
Jiri Slaby1c456072008-02-07 00:16:46 -08002477static int __devinit mxser_initbrd(struct mxser_board *brd,
2478 struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479{
Jiri Slaby1c456072008-02-07 00:16:46 -08002480 struct mxser_port *info;
2481 unsigned int i;
2482 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483
Jiri Slaby1c456072008-02-07 00:16:46 -08002484 printk(KERN_INFO "max. baud rate = %d bps.\n", brd->ports[0].max_baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485
Jiri Slaby1c456072008-02-07 00:16:46 -08002486 for (i = 0; i < brd->info->nports; i++) {
2487 info = &brd->ports[i];
Alan Cox44b7d1b2008-07-16 21:57:18 +01002488 tty_port_init(&info->port);
Jiri Slaby1c456072008-02-07 00:16:46 -08002489 info->board = brd;
2490 info->stop_rx = 0;
2491 info->ldisc_stop_rx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492
Jiri Slaby1c456072008-02-07 00:16:46 -08002493 /* Enhance mode enabled here */
2494 if (brd->chip_flag != MOXA_OTHER_UART)
Christoph Hellwig148ff862008-04-30 00:54:29 -07002495 mxser_enable_must_enchance_mode(info->ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496
Alan Cox0ad9e7d2008-07-16 21:56:10 +01002497 info->port.flags = ASYNC_SHARE_IRQ;
Jiri Slaby1c456072008-02-07 00:16:46 -08002498 info->type = brd->uart_type;
2499
2500 process_txrx_fifo(info);
2501
2502 info->custom_divisor = info->baud_base * 16;
Alan Cox44b7d1b2008-07-16 21:57:18 +01002503 info->port.close_delay = 5 * HZ / 10;
2504 info->port.closing_wait = 30 * HZ;
Jiri Slaby1c456072008-02-07 00:16:46 -08002505 info->normal_termios = mxvar_sdriver->init_termios;
Jiri Slaby1c456072008-02-07 00:16:46 -08002506 init_waitqueue_head(&info->delta_msr_wait);
2507 memset(&info->mon_data, 0, sizeof(struct mxser_mon));
2508 info->err_shadow = 0;
2509 spin_lock_init(&info->slock);
2510
2511 /* before set INT ISR, disable all int */
2512 outb(inb(info->ioaddr + UART_IER) & 0xf0,
2513 info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 }
2515
Jiri Slaby1c456072008-02-07 00:16:46 -08002516 retval = request_irq(brd->irq, mxser_interrupt, IRQF_SHARED, "mxser",
2517 brd);
2518 if (retval) {
2519 printk(KERN_ERR "Board %s: Request irq failed, IRQ (%d) may "
2520 "conflict with another device.\n",
2521 brd->info->name, brd->irq);
2522 /* We hold resources, we need to release them. */
2523 mxser_release_res(brd, pdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002525 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526}
2527
Jiri Slaby1c456072008-02-07 00:16:46 -08002528static int __init mxser_get_ISA_conf(int cap, struct mxser_board *brd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529{
2530 int id, i, bits;
2531 unsigned short regs[16], irq;
2532 unsigned char scratch, scratch2;
2533
Jiri Slaby1c456072008-02-07 00:16:46 -08002534 brd->chip_flag = MOXA_OTHER_UART;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535
2536 id = mxser_read_register(cap, regs);
Jiri Slaby1c456072008-02-07 00:16:46 -08002537 switch (id) {
2538 case C168_ASIC_ID:
2539 brd->info = &mxser_cards[0];
2540 break;
2541 case C104_ASIC_ID:
2542 brd->info = &mxser_cards[1];
2543 break;
2544 case CI104J_ASIC_ID:
2545 brd->info = &mxser_cards[2];
2546 break;
2547 case C102_ASIC_ID:
2548 brd->info = &mxser_cards[5];
2549 break;
2550 case CI132_ASIC_ID:
2551 brd->info = &mxser_cards[6];
2552 break;
2553 case CI134_ASIC_ID:
2554 brd->info = &mxser_cards[7];
2555 break;
2556 default:
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002557 return 0;
Jiri Slaby1c456072008-02-07 00:16:46 -08002558 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559
2560 irq = 0;
Jiri Slaby1c456072008-02-07 00:16:46 -08002561 /* some ISA cards have 2 ports, but we want to see them as 4-port (why?)
2562 Flag-hack checks if configuration should be read as 2-port here. */
2563 if (brd->info->nports == 2 || (brd->info->flags & MXSER_HAS2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 irq = regs[9] & 0xF000;
2565 irq = irq | (irq >> 4);
2566 if (irq != (regs[9] & 0xFF00))
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002567 return MXSER_ERR_IRQ_CONFLIT;
Jiri Slaby1c456072008-02-07 00:16:46 -08002568 } else if (brd->info->nports == 4) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 irq = regs[9] & 0xF000;
2570 irq = irq | (irq >> 4);
2571 irq = irq | (irq >> 8);
2572 if (irq != regs[9])
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002573 return MXSER_ERR_IRQ_CONFLIT;
Jiri Slaby1c456072008-02-07 00:16:46 -08002574 } else if (brd->info->nports == 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 irq = regs[9] & 0xF000;
2576 irq = irq | (irq >> 4);
2577 irq = irq | (irq >> 8);
2578 if ((irq != regs[9]) || (irq != regs[10]))
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002579 return MXSER_ERR_IRQ_CONFLIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 }
2581
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002582 if (!irq)
2583 return MXSER_ERR_IRQ;
Jiri Slaby1c456072008-02-07 00:16:46 -08002584 brd->irq = ((int)(irq & 0xF000) >> 12);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 for (i = 0; i < 8; i++)
Jiri Slaby1c456072008-02-07 00:16:46 -08002586 brd->ports[i].ioaddr = (int) regs[i + 1] & 0xFFF8;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002587 if ((regs[12] & 0x80) == 0)
2588 return MXSER_ERR_VECTOR;
Jiri Slaby1c456072008-02-07 00:16:46 -08002589 brd->vector = (int)regs[11]; /* interrupt vector */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 if (id == 1)
Jiri Slaby1c456072008-02-07 00:16:46 -08002591 brd->vector_mask = 0x00FF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 else
Jiri Slaby1c456072008-02-07 00:16:46 -08002593 brd->vector_mask = 0x000F;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 for (i = 7, bits = 0x0100; i >= 0; i--, bits <<= 1) {
2595 if (regs[12] & bits) {
Jiri Slaby1c456072008-02-07 00:16:46 -08002596 brd->ports[i].baud_base = 921600;
2597 brd->ports[i].max_baud = 921600;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 } else {
Jiri Slaby1c456072008-02-07 00:16:46 -08002599 brd->ports[i].baud_base = 115200;
2600 brd->ports[i].max_baud = 115200;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 }
2602 }
2603 scratch2 = inb(cap + UART_LCR) & (~UART_LCR_DLAB);
2604 outb(scratch2 | UART_LCR_DLAB, cap + UART_LCR);
2605 outb(0, cap + UART_EFR); /* EFR is the same as FCR */
2606 outb(scratch2, cap + UART_LCR);
2607 outb(UART_FCR_ENABLE_FIFO, cap + UART_FCR);
2608 scratch = inb(cap + UART_IIR);
2609
2610 if (scratch & 0xC0)
Jiri Slaby1c456072008-02-07 00:16:46 -08002611 brd->uart_type = PORT_16550A;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 else
Jiri Slaby1c456072008-02-07 00:16:46 -08002613 brd->uart_type = PORT_16450;
2614 if (!request_region(brd->ports[0].ioaddr, 8 * brd->info->nports,
2615 "mxser(IO)"))
2616 return MXSER_ERR_IOADDR;
2617 if (!request_region(brd->vector, 1, "mxser(vector)")) {
2618 release_region(brd->ports[0].ioaddr, 8 * brd->info->nports);
2619 return MXSER_ERR_VECTOR;
2620 }
2621 return brd->info->nports;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622}
2623
Jiri Slaby1c456072008-02-07 00:16:46 -08002624static int __devinit mxser_probe(struct pci_dev *pdev,
2625 const struct pci_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626{
Jiri Slaby1c456072008-02-07 00:16:46 -08002627#ifdef CONFIG_PCI
2628 struct mxser_board *brd;
2629 unsigned int i, j;
2630 unsigned long ioaddress;
2631 int retval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632
Jiri Slaby1c456072008-02-07 00:16:46 -08002633 for (i = 0; i < MXSER_BOARDS; i++)
2634 if (mxser_boards[i].info == NULL)
2635 break;
2636
2637 if (i >= MXSER_BOARDS) {
2638 printk(KERN_ERR "Too many Smartio/Industio family boards found "
2639 "(maximum %d), board not configured\n", MXSER_BOARDS);
2640 goto err;
2641 }
2642
2643 brd = &mxser_boards[i];
2644 brd->idx = i * MXSER_PORTS_PER_BOARD;
2645 printk(KERN_INFO "Found MOXA %s board (BusNo=%d, DevNo=%d)\n",
2646 mxser_cards[ent->driver_data].name,
2647 pdev->bus->number, PCI_SLOT(pdev->devfn));
2648
2649 retval = pci_enable_device(pdev);
2650 if (retval) {
2651 printk(KERN_ERR "Moxa SmartI/O PCI enable fail !\n");
2652 goto err;
2653 }
2654
2655 /* io address */
2656 ioaddress = pci_resource_start(pdev, 2);
2657 retval = pci_request_region(pdev, 2, "mxser(IO)");
2658 if (retval)
2659 goto err;
2660
2661 brd->info = &mxser_cards[ent->driver_data];
2662 for (i = 0; i < brd->info->nports; i++)
2663 brd->ports[i].ioaddr = ioaddress + 8 * i;
2664
2665 /* vector */
2666 ioaddress = pci_resource_start(pdev, 3);
2667 retval = pci_request_region(pdev, 3, "mxser(vector)");
2668 if (retval)
2669 goto err_relio;
2670 brd->vector = ioaddress;
2671
2672 /* irq */
2673 brd->irq = pdev->irq;
2674
2675 brd->chip_flag = CheckIsMoxaMust(brd->ports[0].ioaddr);
2676 brd->uart_type = PORT_16550A;
2677 brd->vector_mask = 0;
2678
2679 for (i = 0; i < brd->info->nports; i++) {
2680 for (j = 0; j < UART_INFO_NUM; j++) {
2681 if (Gpci_uart_info[j].type == brd->chip_flag) {
2682 brd->ports[i].max_baud =
2683 Gpci_uart_info[j].max_baud;
2684
2685 /* exception....CP-102 */
2686 if (brd->info->flags & MXSER_HIGHBAUD)
2687 brd->ports[i].max_baud = 921600;
2688 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 }
2690 }
Jiri Slaby1c456072008-02-07 00:16:46 -08002691 }
2692
2693 if (brd->chip_flag == MOXA_MUST_MU860_HWID) {
2694 for (i = 0; i < brd->info->nports; i++) {
2695 if (i < 4)
2696 brd->ports[i].opmode_ioaddr = ioaddress + 4;
2697 else
2698 brd->ports[i].opmode_ioaddr = ioaddress + 0x0c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 }
Jiri Slaby1c456072008-02-07 00:16:46 -08002700 outb(0, ioaddress + 4); /* default set to RS232 mode */
2701 outb(0, ioaddress + 0x0c); /* default set to RS232 mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 }
Jiri Slaby1c456072008-02-07 00:16:46 -08002703
2704 for (i = 0; i < brd->info->nports; i++) {
2705 brd->vector_mask |= (1 << i);
2706 brd->ports[i].baud_base = 921600;
2707 }
2708
2709 /* mxser_initbrd will hook ISR. */
2710 retval = mxser_initbrd(brd, pdev);
2711 if (retval)
2712 goto err_null;
2713
2714 for (i = 0; i < brd->info->nports; i++)
2715 tty_register_device(mxvar_sdriver, brd->idx + i, &pdev->dev);
2716
2717 pci_set_drvdata(pdev, brd);
2718
2719 return 0;
2720err_relio:
2721 pci_release_region(pdev, 2);
2722err_null:
2723 brd->info = NULL;
2724err:
2725 return retval;
2726#else
2727 return -ENODEV;
2728#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729}
2730
Jiri Slaby1c456072008-02-07 00:16:46 -08002731static void __devexit mxser_remove(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732{
Jiri Slaby1c456072008-02-07 00:16:46 -08002733 struct mxser_board *brd = pci_get_drvdata(pdev);
2734 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735
Jiri Slaby1c456072008-02-07 00:16:46 -08002736 for (i = 0; i < brd->info->nports; i++)
2737 tty_unregister_device(mxvar_sdriver, brd->idx + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738
Jiri Slaby1c456072008-02-07 00:16:46 -08002739 mxser_release_res(brd, pdev, 1);
2740 brd->info = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741}
2742
Jiri Slaby1c456072008-02-07 00:16:46 -08002743static struct pci_driver mxser_driver = {
2744 .name = "mxser",
2745 .id_table = mxser_pcibrds,
2746 .probe = mxser_probe,
2747 .remove = __devexit_p(mxser_remove)
2748};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749
Jiri Slaby1c456072008-02-07 00:16:46 -08002750static int __init mxser_module_init(void)
2751{
2752 struct mxser_board *brd;
2753 unsigned long cap;
2754 unsigned int i, m, isaloop;
2755 int retval, b;
2756
2757 pr_debug("Loading module mxser ...\n");
2758
2759 mxvar_sdriver = alloc_tty_driver(MXSER_PORTS + 1);
2760 if (!mxvar_sdriver)
2761 return -ENOMEM;
2762
2763 printk(KERN_INFO "MOXA Smartio/Industio family driver version %s\n",
2764 MXSER_VERSION);
2765
2766 /* Initialize the tty_driver structure */
2767 mxvar_sdriver->owner = THIS_MODULE;
2768 mxvar_sdriver->magic = TTY_DRIVER_MAGIC;
2769 mxvar_sdriver->name = "ttyMI";
2770 mxvar_sdriver->major = ttymajor;
2771 mxvar_sdriver->minor_start = 0;
2772 mxvar_sdriver->num = MXSER_PORTS + 1;
2773 mxvar_sdriver->type = TTY_DRIVER_TYPE_SERIAL;
2774 mxvar_sdriver->subtype = SERIAL_TYPE_NORMAL;
2775 mxvar_sdriver->init_termios = tty_std_termios;
2776 mxvar_sdriver->init_termios.c_cflag = B9600|CS8|CREAD|HUPCL|CLOCAL;
2777 mxvar_sdriver->flags = TTY_DRIVER_REAL_RAW|TTY_DRIVER_DYNAMIC_DEV;
2778 tty_set_operations(mxvar_sdriver, &mxser_ops);
2779
2780 retval = tty_register_driver(mxvar_sdriver);
2781 if (retval) {
2782 printk(KERN_ERR "Couldn't install MOXA Smartio/Industio family "
2783 "tty driver !\n");
2784 goto err_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 }
Jiri Slaby1c456072008-02-07 00:16:46 -08002786
Jiri Slaby1c456072008-02-07 00:16:46 -08002787 m = 0;
2788 /* Start finding ISA boards here */
2789 for (isaloop = 0; isaloop < 2; isaloop++)
2790 for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
2791 if (!isaloop)
2792 cap = mxserBoardCAP[b]; /* predefined */
2793 else
2794 cap = ioaddr[b]; /* module param */
2795
2796 if (!cap)
2797 continue;
2798
2799 brd = &mxser_boards[m];
2800 retval = mxser_get_ISA_conf(cap, brd);
2801
2802 if (retval != 0)
2803 printk(KERN_INFO "Found MOXA %s board "
2804 "(CAP=0x%x)\n",
2805 brd->info->name, ioaddr[b]);
2806
2807 if (retval <= 0) {
2808 if (retval == MXSER_ERR_IRQ)
2809 printk(KERN_ERR "Invalid interrupt "
2810 "number, board not "
2811 "configured\n");
2812 else if (retval == MXSER_ERR_IRQ_CONFLIT)
2813 printk(KERN_ERR "Invalid interrupt "
2814 "number, board not "
2815 "configured\n");
2816 else if (retval == MXSER_ERR_VECTOR)
2817 printk(KERN_ERR "Invalid interrupt "
2818 "vector, board not "
2819 "configured\n");
2820 else if (retval == MXSER_ERR_IOADDR)
2821 printk(KERN_ERR "Invalid I/O address, "
2822 "board not configured\n");
2823
2824 brd->info = NULL;
2825 continue;
2826 }
2827
2828 /* mxser_initbrd will hook ISR. */
2829 if (mxser_initbrd(brd, NULL) < 0) {
2830 brd->info = NULL;
2831 continue;
2832 }
2833
2834 brd->idx = m * MXSER_PORTS_PER_BOARD;
2835 for (i = 0; i < brd->info->nports; i++)
2836 tty_register_device(mxvar_sdriver, brd->idx + i,
2837 NULL);
2838
2839 m++;
2840 }
2841
2842 retval = pci_register_driver(&mxser_driver);
2843 if (retval) {
2844 printk(KERN_ERR "Can't register pci driver\n");
2845 if (!m) {
2846 retval = -ENODEV;
2847 goto err_unr;
2848 } /* else: we have some ISA cards under control */
2849 }
2850
2851 pr_debug("Done.\n");
2852
2853 return 0;
2854err_unr:
2855 tty_unregister_driver(mxvar_sdriver);
2856err_put:
2857 put_tty_driver(mxvar_sdriver);
2858 return retval;
2859}
2860
2861static void __exit mxser_module_exit(void)
2862{
2863 unsigned int i, j;
2864
2865 pr_debug("Unloading module mxser ...\n");
2866
2867 pci_unregister_driver(&mxser_driver);
2868
2869 for (i = 0; i < MXSER_BOARDS; i++) /* ISA remains */
2870 if (mxser_boards[i].info != NULL)
2871 for (j = 0; j < mxser_boards[i].info->nports; j++)
2872 tty_unregister_device(mxvar_sdriver,
2873 mxser_boards[i].idx + j);
2874 tty_unregister_driver(mxvar_sdriver);
2875 put_tty_driver(mxvar_sdriver);
2876
2877 for (i = 0; i < MXSER_BOARDS; i++)
2878 if (mxser_boards[i].info != NULL)
2879 mxser_release_res(&mxser_boards[i], NULL, 1);
2880
2881 pr_debug("Done.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882}
2883
2884module_init(mxser_module_init);
2885module_exit(mxser_module_exit);