blob: e2471cf1ee95238708241e04b4f7c5f97721cbb0 [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
Alan Cox8eb04cf2008-11-11 14:48:44 +000017 * <alan@lxorguk.ukuu.org.uk>. The original 1.8 code is available on
18 * www.moxa.com.
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 * - Fixed x86_64 cleanness
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 */
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/errno.h>
24#include <linux/signal.h>
25#include <linux/sched.h>
26#include <linux/timer.h>
27#include <linux/interrupt.h>
28#include <linux/tty.h>
29#include <linux/tty_flip.h>
30#include <linux/serial.h>
31#include <linux/serial_reg.h>
32#include <linux/major.h>
33#include <linux/string.h>
34#include <linux/fcntl.h>
35#include <linux/ptrace.h>
36#include <linux/gfp.h>
37#include <linux/ioport.h>
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/pci.h>
Jiri Slaby1977f032007-10-18 23:40:25 -070041#include <linux/bitops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#include <asm/system.h>
44#include <asm/io.h>
45#include <asm/irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <asm/uaccess.h>
47
48#include "mxser.h"
49
Jiri Slabye129def2008-07-22 11:20:34 +010050#define MXSER_VERSION "2.0.4" /* 1.12 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#define MXSERMAJOR 174
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#define MXSER_BOARDS 4 /* Max. boards */
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#define MXSER_PORTS_PER_BOARD 8 /* Max. ports per board */
Jiri Slaby1c456072008-02-07 00:16:46 -080055#define MXSER_PORTS (MXSER_BOARDS * MXSER_PORTS_PER_BOARD)
56#define MXSER_ISR_PASS_LIMIT 100
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Jiri Slaby1c456072008-02-07 00:16:46 -080058/*CheckIsMoxaMust return value*/
59#define MOXA_OTHER_UART 0x00
60#define MOXA_MUST_MU150_HWID 0x01
61#define MOXA_MUST_MU860_HWID 0x02
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#define WAKEUP_CHARS 256
64
65#define UART_MCR_AFE 0x20
66#define UART_LSR_SPECIAL 0x1E
67
Jiri Slabye129def2008-07-22 11:20:34 +010068#define PCI_DEVICE_ID_POS104UL 0x1044
Jiri Slaby1c456072008-02-07 00:16:46 -080069#define PCI_DEVICE_ID_CB108 0x1080
Jiri Slabye129def2008-07-22 11:20:34 +010070#define PCI_DEVICE_ID_CP102UF 0x1023
Jiri Slaby1c456072008-02-07 00:16:46 -080071#define PCI_DEVICE_ID_CB114 0x1142
Jiri Slaby80ff8a82008-02-07 00:16:51 -080072#define PCI_DEVICE_ID_CP114UL 0x1143
Jiri Slaby1c456072008-02-07 00:16:46 -080073#define PCI_DEVICE_ID_CB134I 0x1341
74#define PCI_DEVICE_ID_CP138U 0x1380
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77#define C168_ASIC_ID 1
78#define C104_ASIC_ID 2
79#define C102_ASIC_ID 0xB
80#define CI132_ASIC_ID 4
81#define CI134_ASIC_ID 3
82#define CI104J_ASIC_ID 5
83
Jiri Slaby1c456072008-02-07 00:16:46 -080084#define MXSER_HIGHBAUD 1
85#define MXSER_HAS2 2
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -070087/* This is only for PCI */
Jiri Slaby1c456072008-02-07 00:16:46 -080088static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 int type;
90 int tx_fifo;
91 int rx_fifo;
92 int xmit_fifo_size;
93 int rx_high_water;
94 int rx_trigger;
95 int rx_low_water;
96 long max_baud;
Jiri Slaby1c456072008-02-07 00:16:46 -080097} Gpci_uart_info[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 {MOXA_OTHER_UART, 16, 16, 16, 14, 14, 1, 921600L},
99 {MOXA_MUST_MU150_HWID, 64, 64, 64, 48, 48, 16, 230400L},
100 {MOXA_MUST_MU860_HWID, 128, 128, 128, 96, 96, 32, 921600L}
101};
Jiri Slaby1c456072008-02-07 00:16:46 -0800102#define UART_INFO_NUM ARRAY_SIZE(Gpci_uart_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Jiri Slaby1c456072008-02-07 00:16:46 -0800104struct mxser_cardinfo {
105 char *name;
106 unsigned int nports;
107 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108};
109
Jiri Slaby1c456072008-02-07 00:16:46 -0800110static const struct mxser_cardinfo mxser_cards[] = {
111/* 0*/ { "C168 series", 8, },
112 { "C104 series", 4, },
113 { "CI-104J series", 4, },
114 { "C168H/PCI series", 8, },
115 { "C104H/PCI series", 4, },
116/* 5*/ { "C102 series", 4, MXSER_HAS2 }, /* C102-ISA */
117 { "CI-132 series", 4, MXSER_HAS2 },
118 { "CI-134 series", 4, },
119 { "CP-132 series", 2, },
120 { "CP-114 series", 4, },
121/*10*/ { "CT-114 series", 4, },
122 { "CP-102 series", 2, MXSER_HIGHBAUD },
123 { "CP-104U series", 4, },
124 { "CP-168U series", 8, },
125 { "CP-132U series", 2, },
126/*15*/ { "CP-134U series", 4, },
127 { "CP-104JU series", 4, },
128 { "Moxa UC7000 Serial", 8, }, /* RC7000 */
129 { "CP-118U series", 8, },
130 { "CP-102UL series", 2, },
131/*20*/ { "CP-102U series", 2, },
132 { "CP-118EL series", 8, },
133 { "CP-168EL series", 8, },
134 { "CP-104EL series", 4, },
135 { "CB-108 series", 8, },
136/*25*/ { "CB-114 series", 4, },
137 { "CB-134I series", 4, },
138 { "CP-138U series", 8, },
Jiri Slaby80ff8a82008-02-07 00:16:51 -0800139 { "POS-104UL series", 4, },
Jiri Slabye129def2008-07-22 11:20:34 +0100140 { "CP-114UL series", 4, },
141/*30*/ { "CP-102UF series", 2, }
Jiri Slaby1c456072008-02-07 00:16:46 -0800142};
143
144/* driver_data correspond to the lines in the structure above
145 see also ISA probe function before you change something */
146static struct pci_device_id mxser_pcibrds[] = {
147 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_C168), .driver_data = 3 },
148 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_C104), .driver_data = 4 },
149 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP132), .driver_data = 8 },
150 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP114), .driver_data = 9 },
151 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CT114), .driver_data = 10 },
152 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102), .driver_data = 11 },
153 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104U), .driver_data = 12 },
154 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP168U), .driver_data = 13 },
155 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP132U), .driver_data = 14 },
156 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP134U), .driver_data = 15 },
157 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104JU),.driver_data = 16 },
158 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_RC7000), .driver_data = 17 },
159 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP118U), .driver_data = 18 },
160 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102UL),.driver_data = 19 },
161 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102U), .driver_data = 20 },
162 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP118EL),.driver_data = 21 },
163 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP168EL),.driver_data = 22 },
164 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104EL),.driver_data = 23 },
165 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB108), .driver_data = 24 },
166 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB114), .driver_data = 25 },
167 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB134I), .driver_data = 26 },
168 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP138U), .driver_data = 27 },
169 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_POS104UL), .driver_data = 28 },
Jiri Slaby80ff8a82008-02-07 00:16:51 -0800170 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP114UL), .driver_data = 29 },
Jiri Slabye129def2008-07-22 11:20:34 +0100171 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP102UF), .driver_data = 30 },
Jiri Slaby1c456072008-02-07 00:16:46 -0800172 { }
173};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174MODULE_DEVICE_TABLE(pci, mxser_pcibrds);
175
Jiri Slaby1df00922008-07-25 01:48:22 -0700176static unsigned long ioaddr[MXSER_BOARDS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177static int ttymajor = MXSERMAJOR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179/* Variables for insmod */
180
181MODULE_AUTHOR("Casper Yang");
182MODULE_DESCRIPTION("MOXA Smartio/Industio Family Multiport Board Device Driver");
Jiri Slaby1df00922008-07-25 01:48:22 -0700183module_param_array(ioaddr, ulong, NULL, 0);
184MODULE_PARM_DESC(ioaddr, "ISA io addresses to look for a moxa board");
Rusty Russell8d3b33f2006-03-25 03:07:05 -0800185module_param(ttymajor, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186MODULE_LICENSE("GPL");
187
188struct mxser_log {
189 int tick;
190 unsigned long rxcnt[MXSER_PORTS];
191 unsigned long txcnt[MXSER_PORTS];
192};
193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194struct mxser_mon {
195 unsigned long rxcnt;
196 unsigned long txcnt;
197 unsigned long up_rxcnt;
198 unsigned long up_txcnt;
199 int modem_status;
200 unsigned char hold_reason;
201};
202
203struct mxser_mon_ext {
204 unsigned long rx_cnt[32];
205 unsigned long tx_cnt[32];
206 unsigned long up_rxcnt[32];
207 unsigned long up_txcnt[32];
208 int modem_status[32];
209
210 long baudrate[32];
211 int databits[32];
212 int stopbits[32];
213 int parity[32];
214 int flowctrl[32];
215 int fifo[32];
216 int iftype[32];
217};
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700218
Jiri Slaby1c456072008-02-07 00:16:46 -0800219struct mxser_board;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Jiri Slaby1c456072008-02-07 00:16:46 -0800221struct mxser_port {
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100222 struct tty_port port;
Jiri Slaby1c456072008-02-07 00:16:46 -0800223 struct mxser_board *board;
Jiri Slaby1c456072008-02-07 00:16:46 -0800224
225 unsigned long ioaddr;
226 unsigned long opmode_ioaddr;
227 int max_baud;
228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 int rx_high_water;
230 int rx_trigger; /* Rx fifo trigger level */
231 int rx_low_water;
232 int baud_base; /* max. speed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 int type; /* UART type */
Jiri Slaby1c456072008-02-07 00:16:46 -0800234
235 int x_char; /* xon/xoff character */
236 int IER; /* Interrupt Enable Register */
237 int MCR; /* Modem control register */
238
239 unsigned char stop_rx;
240 unsigned char ldisc_stop_rx;
241
242 int custom_divisor;
Jiri Slaby1c456072008-02-07 00:16:46 -0800243 unsigned char err_shadow;
Jiri Slaby1c456072008-02-07 00:16:46 -0800244
Jiri Slaby1c456072008-02-07 00:16:46 -0800245 struct async_icount icount; /* kernel counters for 4 input interrupts */
246 int timeout;
247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 int read_status_mask;
249 int ignore_status_mask;
250 int xmit_fifo_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 int xmit_head;
252 int xmit_tail;
253 int xmit_cnt;
Jiri Slaby1c456072008-02-07 00:16:46 -0800254
Alan Cox606d0992006-12-08 02:38:45 -0800255 struct ktermios normal_termios;
Jiri Slaby1c456072008-02-07 00:16:46 -0800256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 struct mxser_mon mon_data;
Jiri Slaby1c456072008-02-07 00:16:46 -0800258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 spinlock_t slock;
Jiri Slaby1c456072008-02-07 00:16:46 -0800260 wait_queue_head_t delta_msr_wait;
261};
262
263struct mxser_board {
264 unsigned int idx;
265 int irq;
266 const struct mxser_cardinfo *info;
267 unsigned long vector;
268 unsigned long vector_mask;
269
270 int chip_flag;
271 int uart_type;
272
273 struct mxser_port ports[MXSER_PORTS_PER_BOARD];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274};
275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276struct mxser_mstatus {
277 tcflag_t cflag;
278 int cts;
279 int dsr;
280 int ri;
281 int dcd;
282};
283
Jiri Slaby1c456072008-02-07 00:16:46 -0800284static struct mxser_board mxser_boards[MXSER_BOARDS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285static struct tty_driver *mxvar_sdriver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286static struct mxser_log mxvar_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287static int mxser_set_baud_method[MXSER_PORTS + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Christoph Hellwig148ff862008-04-30 00:54:29 -0700289static void mxser_enable_must_enchance_mode(unsigned long baseio)
290{
291 u8 oldlcr;
292 u8 efr;
293
294 oldlcr = inb(baseio + UART_LCR);
295 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
296
297 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
298 efr |= MOXA_MUST_EFR_EFRB_ENABLE;
299
300 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
301 outb(oldlcr, baseio + UART_LCR);
302}
303
304static void mxser_disable_must_enchance_mode(unsigned long baseio)
305{
306 u8 oldlcr;
307 u8 efr;
308
309 oldlcr = inb(baseio + UART_LCR);
310 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
311
312 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
313 efr &= ~MOXA_MUST_EFR_EFRB_ENABLE;
314
315 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
316 outb(oldlcr, baseio + UART_LCR);
317}
318
319static void mxser_set_must_xon1_value(unsigned long baseio, u8 value)
320{
321 u8 oldlcr;
322 u8 efr;
323
324 oldlcr = inb(baseio + UART_LCR);
325 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
326
327 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
328 efr &= ~MOXA_MUST_EFR_BANK_MASK;
329 efr |= MOXA_MUST_EFR_BANK0;
330
331 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
332 outb(value, baseio + MOXA_MUST_XON1_REGISTER);
333 outb(oldlcr, baseio + UART_LCR);
334}
335
336static void mxser_set_must_xoff1_value(unsigned long baseio, u8 value)
337{
338 u8 oldlcr;
339 u8 efr;
340
341 oldlcr = inb(baseio + UART_LCR);
342 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
343
344 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
345 efr &= ~MOXA_MUST_EFR_BANK_MASK;
346 efr |= MOXA_MUST_EFR_BANK0;
347
348 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
349 outb(value, baseio + MOXA_MUST_XOFF1_REGISTER);
350 outb(oldlcr, baseio + UART_LCR);
351}
352
353static void mxser_set_must_fifo_value(struct mxser_port *info)
354{
355 u8 oldlcr;
356 u8 efr;
357
358 oldlcr = inb(info->ioaddr + UART_LCR);
359 outb(MOXA_MUST_ENTER_ENCHANCE, info->ioaddr + UART_LCR);
360
361 efr = inb(info->ioaddr + MOXA_MUST_EFR_REGISTER);
362 efr &= ~MOXA_MUST_EFR_BANK_MASK;
363 efr |= MOXA_MUST_EFR_BANK1;
364
365 outb(efr, info->ioaddr + MOXA_MUST_EFR_REGISTER);
366 outb((u8)info->rx_high_water, info->ioaddr + MOXA_MUST_RBRTH_REGISTER);
367 outb((u8)info->rx_trigger, info->ioaddr + MOXA_MUST_RBRTI_REGISTER);
368 outb((u8)info->rx_low_water, info->ioaddr + MOXA_MUST_RBRTL_REGISTER);
369 outb(oldlcr, info->ioaddr + UART_LCR);
370}
371
372static void mxser_set_must_enum_value(unsigned long baseio, u8 value)
373{
374 u8 oldlcr;
375 u8 efr;
376
377 oldlcr = inb(baseio + UART_LCR);
378 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
379
380 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
381 efr &= ~MOXA_MUST_EFR_BANK_MASK;
382 efr |= MOXA_MUST_EFR_BANK2;
383
384 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
385 outb(value, baseio + MOXA_MUST_ENUM_REGISTER);
386 outb(oldlcr, baseio + UART_LCR);
387}
388
389static void mxser_get_must_hardware_id(unsigned long baseio, u8 *pId)
390{
391 u8 oldlcr;
392 u8 efr;
393
394 oldlcr = inb(baseio + UART_LCR);
395 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
396
397 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
398 efr &= ~MOXA_MUST_EFR_BANK_MASK;
399 efr |= MOXA_MUST_EFR_BANK2;
400
401 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
402 *pId = inb(baseio + MOXA_MUST_HWID_REGISTER);
403 outb(oldlcr, baseio + UART_LCR);
404}
405
406static void SET_MOXA_MUST_NO_SOFTWARE_FLOW_CONTROL(unsigned long baseio)
407{
408 u8 oldlcr;
409 u8 efr;
410
411 oldlcr = inb(baseio + UART_LCR);
412 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
413
414 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
415 efr &= ~MOXA_MUST_EFR_SF_MASK;
416
417 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
418 outb(oldlcr, baseio + UART_LCR);
419}
420
421static void mxser_enable_must_tx_software_flow_control(unsigned long baseio)
422{
423 u8 oldlcr;
424 u8 efr;
425
426 oldlcr = inb(baseio + UART_LCR);
427 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
428
429 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
430 efr &= ~MOXA_MUST_EFR_SF_TX_MASK;
431 efr |= MOXA_MUST_EFR_SF_TX1;
432
433 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
434 outb(oldlcr, baseio + UART_LCR);
435}
436
437static void mxser_disable_must_tx_software_flow_control(unsigned long baseio)
438{
439 u8 oldlcr;
440 u8 efr;
441
442 oldlcr = inb(baseio + UART_LCR);
443 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
444
445 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
446 efr &= ~MOXA_MUST_EFR_SF_TX_MASK;
447
448 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
449 outb(oldlcr, baseio + UART_LCR);
450}
451
452static void mxser_enable_must_rx_software_flow_control(unsigned long baseio)
453{
454 u8 oldlcr;
455 u8 efr;
456
457 oldlcr = inb(baseio + UART_LCR);
458 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
459
460 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
461 efr &= ~MOXA_MUST_EFR_SF_RX_MASK;
462 efr |= MOXA_MUST_EFR_SF_RX1;
463
464 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
465 outb(oldlcr, baseio + UART_LCR);
466}
467
468static void mxser_disable_must_rx_software_flow_control(unsigned long baseio)
469{
470 u8 oldlcr;
471 u8 efr;
472
473 oldlcr = inb(baseio + UART_LCR);
474 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
475
476 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
477 efr &= ~MOXA_MUST_EFR_SF_RX_MASK;
478
479 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
480 outb(oldlcr, baseio + UART_LCR);
481}
482
Jesper Juhlb8cc5542007-10-18 03:06:03 -0700483#ifdef CONFIG_PCI
Jiri Slaby1c456072008-02-07 00:16:46 -0800484static int __devinit CheckIsMoxaMust(unsigned long io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
486 u8 oldmcr, hwid;
487 int i;
488
489 outb(0, io + UART_LCR);
Christoph Hellwig148ff862008-04-30 00:54:29 -0700490 mxser_disable_must_enchance_mode(io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 oldmcr = inb(io + UART_MCR);
492 outb(0, io + UART_MCR);
Christoph Hellwig148ff862008-04-30 00:54:29 -0700493 mxser_set_must_xon1_value(io, 0x11);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 if ((hwid = inb(io + UART_MCR)) != 0) {
495 outb(oldmcr, io + UART_MCR);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700496 return MOXA_OTHER_UART;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 }
498
Christoph Hellwig148ff862008-04-30 00:54:29 -0700499 mxser_get_must_hardware_id(io, &hwid);
Jiri Slaby1c456072008-02-07 00:16:46 -0800500 for (i = 1; i < UART_INFO_NUM; i++) { /* 0 = OTHER_UART */
501 if (hwid == Gpci_uart_info[i].type)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700502 return (int)hwid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 }
504 return MOXA_OTHER_UART;
505}
Jesper Juhlb8cc5542007-10-18 03:06:03 -0700506#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Jiri Slaby1c456072008-02-07 00:16:46 -0800508static void process_txrx_fifo(struct mxser_port *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509{
510 int i;
511
512 if ((info->type == PORT_16450) || (info->type == PORT_8250)) {
513 info->rx_trigger = 1;
514 info->rx_high_water = 1;
515 info->rx_low_water = 1;
516 info->xmit_fifo_size = 1;
Jiri Slaby1c456072008-02-07 00:16:46 -0800517 } else
518 for (i = 0; i < UART_INFO_NUM; i++)
519 if (info->board->chip_flag == Gpci_uart_info[i].type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 info->rx_trigger = Gpci_uart_info[i].rx_trigger;
521 info->rx_low_water = Gpci_uart_info[i].rx_low_water;
522 info->rx_high_water = Gpci_uart_info[i].rx_high_water;
523 info->xmit_fifo_size = Gpci_uart_info[i].xmit_fifo_size;
524 break;
525 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526}
527
Jiri Slaby1c456072008-02-07 00:16:46 -0800528static unsigned char mxser_get_msr(int baseaddr, int mode, int port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529{
Jiri Slaby72800df2008-07-25 01:48:20 -0700530 static unsigned char mxser_msr[MXSER_PORTS + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 unsigned char status = 0;
532
533 status = inb(baseaddr + UART_MSR);
534
535 mxser_msr[port] &= 0x0F;
536 mxser_msr[port] |= status;
537 status = mxser_msr[port];
538 if (mode)
539 mxser_msr[port] = 0;
540
541 return status;
542}
543
Alan Cox31f35932009-01-02 13:45:05 +0000544static int mxser_carrier_raised(struct tty_port *port)
545{
546 struct mxser_port *mp = container_of(port, struct mxser_port, port);
547 return (inb(mp->ioaddr + UART_MSR) & UART_MSR_DCD)?1:0;
548}
549
Alan Cox5d951fb2009-01-02 13:45:19 +0000550static void mxser_raise_dtr_rts(struct tty_port *port)
551{
552 struct mxser_port *mp = container_of(port, struct mxser_port, port);
553 unsigned long flags;
554
555 spin_lock_irqsave(&mp->slock, flags);
556 outb(inb(mp->ioaddr + UART_MCR) |
557 UART_MCR_DTR | UART_MCR_RTS, mp->ioaddr + UART_MCR);
558 spin_unlock_irqrestore(&mp->slock, flags);
559}
560
Jiri Slaby1c456072008-02-07 00:16:46 -0800561static int mxser_block_til_ready(struct tty_struct *tty, struct file *filp,
Alan Cox31f35932009-01-02 13:45:05 +0000562 struct mxser_port *mp)
Jiri Slaby1c456072008-02-07 00:16:46 -0800563{
564 DECLARE_WAITQUEUE(wait, current);
565 int retval;
566 int do_clocal = 0;
567 unsigned long flags;
Alan Cox31f35932009-01-02 13:45:05 +0000568 int cd;
569 struct tty_port *port = &mp->port;
Jiri Slaby1c456072008-02-07 00:16:46 -0800570
571 /*
572 * If non-blocking mode is set, or the port is not enabled,
573 * then make the check up front and then exit.
574 */
575 if ((filp->f_flags & O_NONBLOCK) ||
576 test_bit(TTY_IO_ERROR, &tty->flags)) {
Alan Cox31f35932009-01-02 13:45:05 +0000577 port->flags |= ASYNC_NORMAL_ACTIVE;
Jiri Slaby1c456072008-02-07 00:16:46 -0800578 return 0;
579 }
580
581 if (tty->termios->c_cflag & CLOCAL)
582 do_clocal = 1;
583
584 /*
585 * Block waiting for the carrier detect and the line to become
586 * free (i.e., not in use by the callout). While we are in
Alan Cox31f35932009-01-02 13:45:05 +0000587 * this loop, port->count is dropped by one, so that
Jiri Slaby1c456072008-02-07 00:16:46 -0800588 * mxser_close() knows when to free things. We restore it upon
589 * exit, either normal or abnormal.
590 */
591 retval = 0;
Alan Cox31f35932009-01-02 13:45:05 +0000592 add_wait_queue(&port->open_wait, &wait);
Jiri Slaby1c456072008-02-07 00:16:46 -0800593
Alan Cox3b6826b2009-01-02 13:45:58 +0000594 spin_lock_irqsave(&port->lock, flags);
Jiri Slaby1c456072008-02-07 00:16:46 -0800595 if (!tty_hung_up_p(filp))
Alan Cox31f35932009-01-02 13:45:05 +0000596 port->count--;
Alan Cox31f35932009-01-02 13:45:05 +0000597 port->blocked_open++;
Alan Cox3b6826b2009-01-02 13:45:58 +0000598 spin_unlock_irqrestore(&port->lock, flags);
Jiri Slaby1c456072008-02-07 00:16:46 -0800599 while (1) {
Alan Cox5d951fb2009-01-02 13:45:19 +0000600 tty_port_raise_dtr_rts(port);
Jiri Slaby1c456072008-02-07 00:16:46 -0800601 set_current_state(TASK_INTERRUPTIBLE);
Alan Cox31f35932009-01-02 13:45:05 +0000602 if (tty_hung_up_p(filp) || !(port->flags & ASYNC_INITIALIZED)) {
603 if (port->flags & ASYNC_HUP_NOTIFY)
Jiri Slaby1c456072008-02-07 00:16:46 -0800604 retval = -EAGAIN;
605 else
606 retval = -ERESTARTSYS;
607 break;
608 }
Alan Cox31f35932009-01-02 13:45:05 +0000609 cd = tty_port_carrier_raised(port);
610 if (!(port->flags & ASYNC_CLOSING) && (do_clocal || cd))
Jiri Slaby1c456072008-02-07 00:16:46 -0800611 break;
612 if (signal_pending(current)) {
613 retval = -ERESTARTSYS;
614 break;
615 }
616 schedule();
617 }
618 set_current_state(TASK_RUNNING);
Alan Cox31f35932009-01-02 13:45:05 +0000619 remove_wait_queue(&port->open_wait, &wait);
Alan Cox3b6826b2009-01-02 13:45:58 +0000620 spin_lock_irqsave(&port->lock, flags);
Jiri Slaby1c456072008-02-07 00:16:46 -0800621 if (!tty_hung_up_p(filp))
Alan Cox31f35932009-01-02 13:45:05 +0000622 port->count++;
623 port->blocked_open--;
Alan Cox3b6826b2009-01-02 13:45:58 +0000624 if (retval == 0)
625 port->flags |= ASYNC_NORMAL_ACTIVE;
626 spin_unlock_irqrestore(&port->lock, flags);
Jiri Slaby1c456072008-02-07 00:16:46 -0800627 return 0;
628}
629
Alan Cox216ba022008-10-13 10:40:19 +0100630static int mxser_set_baud(struct tty_struct *tty, long newspd)
Jiri Slaby1c456072008-02-07 00:16:46 -0800631{
Alan Cox216ba022008-10-13 10:40:19 +0100632 struct mxser_port *info = tty->driver_data;
Jiri Slaby1c456072008-02-07 00:16:46 -0800633 int quot = 0, baud;
634 unsigned char cval;
635
Alan Cox216ba022008-10-13 10:40:19 +0100636 if (!info->ioaddr)
Jiri Slaby1c456072008-02-07 00:16:46 -0800637 return -1;
638
639 if (newspd > info->max_baud)
640 return -1;
641
642 if (newspd == 134) {
643 quot = 2 * info->baud_base / 269;
Alan Cox216ba022008-10-13 10:40:19 +0100644 tty_encode_baud_rate(tty, 134, 134);
Jiri Slaby1c456072008-02-07 00:16:46 -0800645 } else if (newspd) {
646 quot = info->baud_base / newspd;
647 if (quot == 0)
648 quot = 1;
649 baud = info->baud_base/quot;
Alan Cox216ba022008-10-13 10:40:19 +0100650 tty_encode_baud_rate(tty, baud, baud);
Jiri Slaby1c456072008-02-07 00:16:46 -0800651 } else {
652 quot = 0;
653 }
654
655 info->timeout = ((info->xmit_fifo_size * HZ * 10 * quot) / info->baud_base);
656 info->timeout += HZ / 50; /* Add .02 seconds of slop */
657
658 if (quot) {
659 info->MCR |= UART_MCR_DTR;
660 outb(info->MCR, info->ioaddr + UART_MCR);
661 } else {
662 info->MCR &= ~UART_MCR_DTR;
663 outb(info->MCR, info->ioaddr + UART_MCR);
664 return 0;
665 }
666
667 cval = inb(info->ioaddr + UART_LCR);
668
669 outb(cval | UART_LCR_DLAB, info->ioaddr + UART_LCR); /* set DLAB */
670
671 outb(quot & 0xff, info->ioaddr + UART_DLL); /* LS of divisor */
672 outb(quot >> 8, info->ioaddr + UART_DLM); /* MS of divisor */
673 outb(cval, info->ioaddr + UART_LCR); /* reset DLAB */
674
675#ifdef BOTHER
Alan Cox216ba022008-10-13 10:40:19 +0100676 if (C_BAUD(tty) == BOTHER) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800677 quot = info->baud_base % newspd;
678 quot *= 8;
679 if (quot % newspd > newspd / 2) {
680 quot /= newspd;
681 quot++;
682 } else
683 quot /= newspd;
684
Christoph Hellwig148ff862008-04-30 00:54:29 -0700685 mxser_set_must_enum_value(info->ioaddr, quot);
Jiri Slaby1c456072008-02-07 00:16:46 -0800686 } else
687#endif
Christoph Hellwig148ff862008-04-30 00:54:29 -0700688 mxser_set_must_enum_value(info->ioaddr, 0);
Jiri Slaby1c456072008-02-07 00:16:46 -0800689
690 return 0;
691}
692
693/*
694 * This routine is called to set the UART divisor registers to match
695 * the specified baud rate for a serial port.
696 */
Alan Cox216ba022008-10-13 10:40:19 +0100697static int mxser_change_speed(struct tty_struct *tty,
698 struct ktermios *old_termios)
Jiri Slaby1c456072008-02-07 00:16:46 -0800699{
Alan Cox216ba022008-10-13 10:40:19 +0100700 struct mxser_port *info = tty->driver_data;
Jiri Slaby1c456072008-02-07 00:16:46 -0800701 unsigned cflag, cval, fcr;
702 int ret = 0;
703 unsigned char status;
704
Alan Cox216ba022008-10-13 10:40:19 +0100705 cflag = tty->termios->c_cflag;
706 if (!info->ioaddr)
Jiri Slaby1c456072008-02-07 00:16:46 -0800707 return ret;
708
Alan Cox216ba022008-10-13 10:40:19 +0100709 if (mxser_set_baud_method[tty->index] == 0)
710 mxser_set_baud(tty, tty_get_baud_rate(tty));
Jiri Slaby1c456072008-02-07 00:16:46 -0800711
712 /* byte size and parity */
713 switch (cflag & CSIZE) {
714 case CS5:
715 cval = 0x00;
716 break;
717 case CS6:
718 cval = 0x01;
719 break;
720 case CS7:
721 cval = 0x02;
722 break;
723 case CS8:
724 cval = 0x03;
725 break;
726 default:
727 cval = 0x00;
728 break; /* too keep GCC shut... */
729 }
730 if (cflag & CSTOPB)
731 cval |= 0x04;
732 if (cflag & PARENB)
733 cval |= UART_LCR_PARITY;
734 if (!(cflag & PARODD))
735 cval |= UART_LCR_EPAR;
736 if (cflag & CMSPAR)
737 cval |= UART_LCR_SPAR;
738
739 if ((info->type == PORT_8250) || (info->type == PORT_16450)) {
740 if (info->board->chip_flag) {
741 fcr = UART_FCR_ENABLE_FIFO;
742 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
Christoph Hellwig148ff862008-04-30 00:54:29 -0700743 mxser_set_must_fifo_value(info);
Jiri Slaby1c456072008-02-07 00:16:46 -0800744 } else
745 fcr = 0;
746 } else {
747 fcr = UART_FCR_ENABLE_FIFO;
748 if (info->board->chip_flag) {
749 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
Christoph Hellwig148ff862008-04-30 00:54:29 -0700750 mxser_set_must_fifo_value(info);
Jiri Slaby1c456072008-02-07 00:16:46 -0800751 } else {
752 switch (info->rx_trigger) {
753 case 1:
754 fcr |= UART_FCR_TRIGGER_1;
755 break;
756 case 4:
757 fcr |= UART_FCR_TRIGGER_4;
758 break;
759 case 8:
760 fcr |= UART_FCR_TRIGGER_8;
761 break;
762 default:
763 fcr |= UART_FCR_TRIGGER_14;
764 break;
765 }
766 }
767 }
768
769 /* CTS flow control flag and modem status interrupts */
770 info->IER &= ~UART_IER_MSI;
771 info->MCR &= ~UART_MCR_AFE;
772 if (cflag & CRTSCTS) {
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100773 info->port.flags |= ASYNC_CTS_FLOW;
Jiri Slaby1c456072008-02-07 00:16:46 -0800774 info->IER |= UART_IER_MSI;
775 if ((info->type == PORT_16550A) || (info->board->chip_flag)) {
776 info->MCR |= UART_MCR_AFE;
777 } else {
778 status = inb(info->ioaddr + UART_MSR);
Alan Cox216ba022008-10-13 10:40:19 +0100779 if (tty->hw_stopped) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800780 if (status & UART_MSR_CTS) {
Alan Cox216ba022008-10-13 10:40:19 +0100781 tty->hw_stopped = 0;
Jiri Slaby1c456072008-02-07 00:16:46 -0800782 if (info->type != PORT_16550A &&
783 !info->board->chip_flag) {
784 outb(info->IER & ~UART_IER_THRI,
785 info->ioaddr +
786 UART_IER);
787 info->IER |= UART_IER_THRI;
788 outb(info->IER, info->ioaddr +
789 UART_IER);
790 }
Alan Cox216ba022008-10-13 10:40:19 +0100791 tty_wakeup(tty);
Jiri Slaby1c456072008-02-07 00:16:46 -0800792 }
793 } else {
794 if (!(status & UART_MSR_CTS)) {
Alan Cox216ba022008-10-13 10:40:19 +0100795 tty->hw_stopped = 1;
Jiri Slaby1c456072008-02-07 00:16:46 -0800796 if ((info->type != PORT_16550A) &&
797 (!info->board->chip_flag)) {
798 info->IER &= ~UART_IER_THRI;
799 outb(info->IER, info->ioaddr +
800 UART_IER);
801 }
802 }
803 }
804 }
805 } else {
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100806 info->port.flags &= ~ASYNC_CTS_FLOW;
Jiri Slaby1c456072008-02-07 00:16:46 -0800807 }
808 outb(info->MCR, info->ioaddr + UART_MCR);
809 if (cflag & CLOCAL) {
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100810 info->port.flags &= ~ASYNC_CHECK_CD;
Jiri Slaby1c456072008-02-07 00:16:46 -0800811 } else {
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100812 info->port.flags |= ASYNC_CHECK_CD;
Jiri Slaby1c456072008-02-07 00:16:46 -0800813 info->IER |= UART_IER_MSI;
814 }
815 outb(info->IER, info->ioaddr + UART_IER);
816
817 /*
818 * Set up parity check flag
819 */
820 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
Alan Cox216ba022008-10-13 10:40:19 +0100821 if (I_INPCK(tty))
Jiri Slaby1c456072008-02-07 00:16:46 -0800822 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
Alan Cox216ba022008-10-13 10:40:19 +0100823 if (I_BRKINT(tty) || I_PARMRK(tty))
Jiri Slaby1c456072008-02-07 00:16:46 -0800824 info->read_status_mask |= UART_LSR_BI;
825
826 info->ignore_status_mask = 0;
827
Alan Cox216ba022008-10-13 10:40:19 +0100828 if (I_IGNBRK(tty)) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800829 info->ignore_status_mask |= UART_LSR_BI;
830 info->read_status_mask |= UART_LSR_BI;
831 /*
832 * If we're ignore parity and break indicators, ignore
833 * overruns too. (For real raw support).
834 */
Alan Cox216ba022008-10-13 10:40:19 +0100835 if (I_IGNPAR(tty)) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800836 info->ignore_status_mask |=
837 UART_LSR_OE |
838 UART_LSR_PE |
839 UART_LSR_FE;
840 info->read_status_mask |=
841 UART_LSR_OE |
842 UART_LSR_PE |
843 UART_LSR_FE;
844 }
845 }
846 if (info->board->chip_flag) {
Alan Cox216ba022008-10-13 10:40:19 +0100847 mxser_set_must_xon1_value(info->ioaddr, START_CHAR(tty));
848 mxser_set_must_xoff1_value(info->ioaddr, STOP_CHAR(tty));
849 if (I_IXON(tty)) {
Christoph Hellwig148ff862008-04-30 00:54:29 -0700850 mxser_enable_must_rx_software_flow_control(
851 info->ioaddr);
Jiri Slaby1c456072008-02-07 00:16:46 -0800852 } else {
Christoph Hellwig148ff862008-04-30 00:54:29 -0700853 mxser_disable_must_rx_software_flow_control(
854 info->ioaddr);
Jiri Slaby1c456072008-02-07 00:16:46 -0800855 }
Alan Cox216ba022008-10-13 10:40:19 +0100856 if (I_IXOFF(tty)) {
Christoph Hellwig148ff862008-04-30 00:54:29 -0700857 mxser_enable_must_tx_software_flow_control(
858 info->ioaddr);
Jiri Slaby1c456072008-02-07 00:16:46 -0800859 } else {
Christoph Hellwig148ff862008-04-30 00:54:29 -0700860 mxser_disable_must_tx_software_flow_control(
861 info->ioaddr);
Jiri Slaby1c456072008-02-07 00:16:46 -0800862 }
863 }
864
865
866 outb(fcr, info->ioaddr + UART_FCR); /* set fcr */
867 outb(cval, info->ioaddr + UART_LCR);
868
869 return ret;
870}
871
Alan Cox216ba022008-10-13 10:40:19 +0100872static void mxser_check_modem_status(struct tty_struct *tty,
873 struct mxser_port *port, int status)
Jiri Slaby1c456072008-02-07 00:16:46 -0800874{
875 /* update input line counters */
876 if (status & UART_MSR_TERI)
877 port->icount.rng++;
878 if (status & UART_MSR_DDSR)
879 port->icount.dsr++;
880 if (status & UART_MSR_DDCD)
881 port->icount.dcd++;
882 if (status & UART_MSR_DCTS)
883 port->icount.cts++;
884 port->mon_data.modem_status = status;
885 wake_up_interruptible(&port->delta_msr_wait);
886
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100887 if ((port->port.flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800888 if (status & UART_MSR_DCD)
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100889 wake_up_interruptible(&port->port.open_wait);
Jiri Slaby1c456072008-02-07 00:16:46 -0800890 }
891
Alan Cox216ba022008-10-13 10:40:19 +0100892 tty = tty_port_tty_get(&port->port);
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100893 if (port->port.flags & ASYNC_CTS_FLOW) {
Alan Cox216ba022008-10-13 10:40:19 +0100894 if (tty->hw_stopped) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800895 if (status & UART_MSR_CTS) {
Alan Cox216ba022008-10-13 10:40:19 +0100896 tty->hw_stopped = 0;
Jiri Slaby1c456072008-02-07 00:16:46 -0800897
898 if ((port->type != PORT_16550A) &&
899 (!port->board->chip_flag)) {
900 outb(port->IER & ~UART_IER_THRI,
901 port->ioaddr + UART_IER);
902 port->IER |= UART_IER_THRI;
903 outb(port->IER, port->ioaddr +
904 UART_IER);
905 }
Alan Cox216ba022008-10-13 10:40:19 +0100906 tty_wakeup(tty);
Jiri Slaby1c456072008-02-07 00:16:46 -0800907 }
908 } else {
909 if (!(status & UART_MSR_CTS)) {
Alan Cox216ba022008-10-13 10:40:19 +0100910 tty->hw_stopped = 1;
Jiri Slaby1c456072008-02-07 00:16:46 -0800911 if (port->type != PORT_16550A &&
912 !port->board->chip_flag) {
913 port->IER &= ~UART_IER_THRI;
914 outb(port->IER, port->ioaddr +
915 UART_IER);
916 }
917 }
918 }
919 }
920}
921
Alan Cox216ba022008-10-13 10:40:19 +0100922static int mxser_startup(struct tty_struct *tty)
Jiri Slaby1c456072008-02-07 00:16:46 -0800923{
Alan Cox216ba022008-10-13 10:40:19 +0100924 struct mxser_port *info = tty->driver_data;
Jiri Slaby1c456072008-02-07 00:16:46 -0800925 unsigned long page;
926 unsigned long flags;
927
928 page = __get_free_page(GFP_KERNEL);
929 if (!page)
930 return -ENOMEM;
931
932 spin_lock_irqsave(&info->slock, flags);
933
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100934 if (info->port.flags & ASYNC_INITIALIZED) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800935 free_page(page);
936 spin_unlock_irqrestore(&info->slock, flags);
937 return 0;
938 }
939
940 if (!info->ioaddr || !info->type) {
Alan Cox216ba022008-10-13 10:40:19 +0100941 set_bit(TTY_IO_ERROR, &tty->flags);
Jiri Slaby1c456072008-02-07 00:16:46 -0800942 free_page(page);
943 spin_unlock_irqrestore(&info->slock, flags);
944 return 0;
945 }
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100946 if (info->port.xmit_buf)
Jiri Slaby1c456072008-02-07 00:16:46 -0800947 free_page(page);
948 else
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100949 info->port.xmit_buf = (unsigned char *) page;
Jiri Slaby1c456072008-02-07 00:16:46 -0800950
951 /*
952 * Clear the FIFO buffers and disable them
953 * (they will be reenabled in mxser_change_speed())
954 */
955 if (info->board->chip_flag)
956 outb((UART_FCR_CLEAR_RCVR |
957 UART_FCR_CLEAR_XMIT |
958 MOXA_MUST_FCR_GDA_MODE_ENABLE), info->ioaddr + UART_FCR);
959 else
960 outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
961 info->ioaddr + UART_FCR);
962
963 /*
964 * At this point there's no way the LSR could still be 0xFF;
965 * if it is, then bail out, because there's likely no UART
966 * here.
967 */
968 if (inb(info->ioaddr + UART_LSR) == 0xff) {
969 spin_unlock_irqrestore(&info->slock, flags);
970 if (capable(CAP_SYS_ADMIN)) {
Alan Cox216ba022008-10-13 10:40:19 +0100971 if (tty)
972 set_bit(TTY_IO_ERROR, &tty->flags);
Jiri Slaby1c456072008-02-07 00:16:46 -0800973 return 0;
974 } else
975 return -ENODEV;
976 }
977
978 /*
979 * Clear the interrupt registers.
980 */
981 (void) inb(info->ioaddr + UART_LSR);
982 (void) inb(info->ioaddr + UART_RX);
983 (void) inb(info->ioaddr + UART_IIR);
984 (void) inb(info->ioaddr + UART_MSR);
985
986 /*
987 * Now, initialize the UART
988 */
989 outb(UART_LCR_WLEN8, info->ioaddr + UART_LCR); /* reset DLAB */
990 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
991 outb(info->MCR, info->ioaddr + UART_MCR);
992
993 /*
994 * Finally, enable interrupts
995 */
996 info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
997
998 if (info->board->chip_flag)
999 info->IER |= MOXA_MUST_IER_EGDAI;
1000 outb(info->IER, info->ioaddr + UART_IER); /* enable interrupts */
1001
1002 /*
1003 * And clear the interrupt registers again for luck.
1004 */
1005 (void) inb(info->ioaddr + UART_LSR);
1006 (void) inb(info->ioaddr + UART_RX);
1007 (void) inb(info->ioaddr + UART_IIR);
1008 (void) inb(info->ioaddr + UART_MSR);
1009
Alan Cox216ba022008-10-13 10:40:19 +01001010 clear_bit(TTY_IO_ERROR, &tty->flags);
Jiri Slaby1c456072008-02-07 00:16:46 -08001011 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1012
1013 /*
1014 * and set the speed of the serial port
1015 */
Alan Cox216ba022008-10-13 10:40:19 +01001016 mxser_change_speed(tty, NULL);
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001017 info->port.flags |= ASYNC_INITIALIZED;
Jiri Slaby1c456072008-02-07 00:16:46 -08001018 spin_unlock_irqrestore(&info->slock, flags);
1019
1020 return 0;
1021}
1022
1023/*
1024 * This routine will shutdown a serial port; interrupts maybe disabled, and
1025 * DTR is dropped if the hangup on close termio flag is on.
1026 */
Alan Cox216ba022008-10-13 10:40:19 +01001027static void mxser_shutdown(struct tty_struct *tty)
Jiri Slaby1c456072008-02-07 00:16:46 -08001028{
Alan Cox216ba022008-10-13 10:40:19 +01001029 struct mxser_port *info = tty->driver_data;
Jiri Slaby1c456072008-02-07 00:16:46 -08001030 unsigned long flags;
1031
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001032 if (!(info->port.flags & ASYNC_INITIALIZED))
Jiri Slaby1c456072008-02-07 00:16:46 -08001033 return;
1034
1035 spin_lock_irqsave(&info->slock, flags);
1036
1037 /*
1038 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
1039 * here so the queue might never be waken up
1040 */
1041 wake_up_interruptible(&info->delta_msr_wait);
1042
1043 /*
1044 * Free the IRQ, if necessary
1045 */
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001046 if (info->port.xmit_buf) {
1047 free_page((unsigned long) info->port.xmit_buf);
1048 info->port.xmit_buf = NULL;
Jiri Slaby1c456072008-02-07 00:16:46 -08001049 }
1050
1051 info->IER = 0;
1052 outb(0x00, info->ioaddr + UART_IER);
1053
Alan Cox216ba022008-10-13 10:40:19 +01001054 if (tty->termios->c_cflag & HUPCL)
Jiri Slaby1c456072008-02-07 00:16:46 -08001055 info->MCR &= ~(UART_MCR_DTR | UART_MCR_RTS);
1056 outb(info->MCR, info->ioaddr + UART_MCR);
1057
1058 /* clear Rx/Tx FIFO's */
1059 if (info->board->chip_flag)
1060 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT |
1061 MOXA_MUST_FCR_GDA_MODE_ENABLE,
1062 info->ioaddr + UART_FCR);
1063 else
1064 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
1065 info->ioaddr + UART_FCR);
1066
1067 /* read data port to reset things */
1068 (void) inb(info->ioaddr + UART_RX);
1069
Alan Cox216ba022008-10-13 10:40:19 +01001070 set_bit(TTY_IO_ERROR, &tty->flags);
Jiri Slaby1c456072008-02-07 00:16:46 -08001071
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001072 info->port.flags &= ~ASYNC_INITIALIZED;
Jiri Slaby1c456072008-02-07 00:16:46 -08001073
1074 if (info->board->chip_flag)
1075 SET_MOXA_MUST_NO_SOFTWARE_FLOW_CONTROL(info->ioaddr);
1076
1077 spin_unlock_irqrestore(&info->slock, flags);
1078}
1079
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080/*
1081 * This routine is called whenever a serial port is opened. It
1082 * enables interrupts for a serial port, linking in its async structure into
1083 * the IRQ chain. It also performs the serial-specific
1084 * initialization for the tty structure.
1085 */
1086static int mxser_open(struct tty_struct *tty, struct file *filp)
1087{
Jiri Slaby1c456072008-02-07 00:16:46 -08001088 struct mxser_port *info;
1089 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 int retval, line;
1091
1092 line = tty->index;
1093 if (line == MXSER_PORTS)
1094 return 0;
1095 if (line < 0 || line > MXSER_PORTS)
1096 return -ENODEV;
Jiri Slaby1c456072008-02-07 00:16:46 -08001097 info = &mxser_boards[line / MXSER_PORTS_PER_BOARD].ports[line % MXSER_PORTS_PER_BOARD];
1098 if (!info->ioaddr)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001099 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
1101 tty->driver_data = info;
Alan Cox216ba022008-10-13 10:40:19 +01001102 tty_port_tty_set(&info->port, tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 /*
1104 * Start up serial port
1105 */
Alan Cox3b6826b2009-01-02 13:45:58 +00001106 spin_lock_irqsave(&info->port.lock, flags);
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001107 info->port.count++;
Alan Cox3b6826b2009-01-02 13:45:58 +00001108 spin_unlock_irqrestore(&info->port.lock, flags);
Alan Cox216ba022008-10-13 10:40:19 +01001109 retval = mxser_startup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 if (retval)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001111 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112
1113 retval = mxser_block_til_ready(tty, filp, info);
1114 if (retval)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001115 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
Cedric Le Goater8cddd702007-02-10 01:46:33 -08001117 /* unmark here for very high baud rate (ex. 921600 bps) used */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 tty->low_latency = 1;
1119 return 0;
1120}
1121
Alan Cox978e5952008-04-30 00:53:59 -07001122static void mxser_flush_buffer(struct tty_struct *tty)
1123{
1124 struct mxser_port *info = tty->driver_data;
1125 char fcr;
1126 unsigned long flags;
1127
1128
1129 spin_lock_irqsave(&info->slock, flags);
1130 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1131
1132 fcr = inb(info->ioaddr + UART_FCR);
1133 outb((fcr | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
1134 info->ioaddr + UART_FCR);
1135 outb(fcr, info->ioaddr + UART_FCR);
1136
1137 spin_unlock_irqrestore(&info->slock, flags);
1138
1139 tty_wakeup(tty);
1140}
1141
1142
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143/*
1144 * This routine is called when the serial port gets closed. First, we
1145 * wait for the last remaining data to be sent. Then, we unlink its
1146 * async structure from the interrupt chain if necessary, and we free
1147 * that IRQ if nothing is left in the chain.
1148 */
1149static void mxser_close(struct tty_struct *tty, struct file *filp)
1150{
Jiri Slaby1c456072008-02-07 00:16:46 -08001151 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
1153 unsigned long timeout;
1154 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
1156 if (tty->index == MXSER_PORTS)
1157 return;
1158 if (!info)
Kirill Smelkov6f08b722005-11-07 00:59:23 -08001159 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
Alan Cox3b6826b2009-01-02 13:45:58 +00001161 spin_lock_irqsave(&info->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
1163 if (tty_hung_up_p(filp)) {
Alan Cox3b6826b2009-01-02 13:45:58 +00001164 spin_unlock_irqrestore(&info->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 return;
1166 }
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001167 if ((tty->count == 1) && (info->port.count != 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 /*
1169 * Uh, oh. tty->count is 1, which means that the tty
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001170 * structure will be freed. Info->port.count should always
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 * be one in these conditions. If it's greater than
1172 * one, we've got real problems, since it means the
1173 * serial port won't be shutdown.
1174 */
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001175 printk(KERN_ERR "mxser_close: bad serial port count; "
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001176 "tty->count is 1, info->port.count is %d\n", info->port.count);
1177 info->port.count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 }
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001179 if (--info->port.count < 0) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001180 printk(KERN_ERR "mxser_close: bad serial port count for "
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001181 "ttys%d: %d\n", tty->index, info->port.count);
1182 info->port.count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 }
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001184 if (info->port.count) {
Alan Cox3b6826b2009-01-02 13:45:58 +00001185 spin_unlock_irqrestore(&info->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 return;
1187 }
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001188 info->port.flags |= ASYNC_CLOSING;
Alan Cox3b6826b2009-01-02 13:45:58 +00001189 spin_unlock_irqrestore(&info->port.lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 /*
1191 * Save the termios structure, since this port may have
1192 * separate termios for callout and dialin.
1193 */
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001194 if (info->port.flags & ASYNC_NORMAL_ACTIVE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 info->normal_termios = *tty->termios;
1196 /*
1197 * Now we wait for the transmit buffer to clear; and we notify
1198 * the line discipline to only process XON/XOFF characters.
1199 */
1200 tty->closing = 1;
Alan Cox44b7d1b2008-07-16 21:57:18 +01001201 if (info->port.closing_wait != ASYNC_CLOSING_WAIT_NONE)
1202 tty_wait_until_sent(tty, info->port.closing_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 /*
1204 * At this point we stop accepting input. To do this, we
1205 * disable the receive line status interrupts, and tell the
1206 * interrupt driver to stop checking the data ready bit in the
1207 * line status register.
1208 */
1209 info->IER &= ~UART_IER_RLSI;
Jiri Slaby1c456072008-02-07 00:16:46 -08001210 if (info->board->chip_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 info->IER &= ~MOXA_MUST_RECV_ISR;
Jiri Slaby1c456072008-02-07 00:16:46 -08001212
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001213 if (info->port.flags & ASYNC_INITIALIZED) {
Jiri Slaby1c456072008-02-07 00:16:46 -08001214 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 /*
1216 * Before we drop DTR, make sure the UART transmitter
1217 * has completely drained; this is especially
1218 * important if there is a transmit FIFO!
1219 */
1220 timeout = jiffies + HZ;
Jiri Slaby1c456072008-02-07 00:16:46 -08001221 while (!(inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT)) {
Nishanth Aravamudanda4cd8d2005-09-10 00:27:30 -07001222 schedule_timeout_interruptible(5);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 if (time_after(jiffies, timeout))
1224 break;
1225 }
1226 }
Alan Cox216ba022008-10-13 10:40:19 +01001227 mxser_shutdown(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
Alan Cox978e5952008-04-30 00:53:59 -07001229 mxser_flush_buffer(tty);
Jiri Slaby1c456072008-02-07 00:16:46 -08001230 tty_ldisc_flush(tty);
1231
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 tty->closing = 0;
Alan Cox216ba022008-10-13 10:40:19 +01001233 tty_port_tty_set(&info->port, NULL);
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001234 if (info->port.blocked_open) {
Alan Cox44b7d1b2008-07-16 21:57:18 +01001235 if (info->port.close_delay)
1236 schedule_timeout_interruptible(info->port.close_delay);
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001237 wake_up_interruptible(&info->port.open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 }
1239
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001240 info->port.flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241}
1242
1243static int mxser_write(struct tty_struct *tty, const unsigned char *buf, int count)
1244{
1245 int c, total = 0;
Jiri Slaby1c456072008-02-07 00:16:46 -08001246 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 unsigned long flags;
1248
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001249 if (!info->port.xmit_buf)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001250 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
1252 while (1) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001253 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1254 SERIAL_XMIT_SIZE - info->xmit_head));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 if (c <= 0)
1256 break;
1257
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001258 memcpy(info->port.xmit_buf + info->xmit_head, buf, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 spin_lock_irqsave(&info->slock, flags);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001260 info->xmit_head = (info->xmit_head + c) &
1261 (SERIAL_XMIT_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 info->xmit_cnt += c;
1263 spin_unlock_irqrestore(&info->slock, flags);
1264
1265 buf += c;
1266 count -= c;
1267 total += c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 }
1269
Jiri Slaby1c456072008-02-07 00:16:46 -08001270 if (info->xmit_cnt && !tty->stopped) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001271 if (!tty->hw_stopped ||
1272 (info->type == PORT_16550A) ||
Jiri Slaby1c456072008-02-07 00:16:46 -08001273 (info->board->chip_flag)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby1c456072008-02-07 00:16:46 -08001275 outb(info->IER & ~UART_IER_THRI, info->ioaddr +
1276 UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 info->IER |= UART_IER_THRI;
Jiri Slaby1c456072008-02-07 00:16:46 -08001278 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 spin_unlock_irqrestore(&info->slock, flags);
1280 }
1281 }
1282 return total;
1283}
1284
Alan Cox0be2ead2008-04-30 00:54:03 -07001285static int mxser_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286{
Jiri Slaby1c456072008-02-07 00:16:46 -08001287 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 unsigned long flags;
1289
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001290 if (!info->port.xmit_buf)
Alan Cox0be2ead2008-04-30 00:54:03 -07001291 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
1293 if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1)
Alan Cox0be2ead2008-04-30 00:54:03 -07001294 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
1296 spin_lock_irqsave(&info->slock, flags);
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001297 info->port.xmit_buf[info->xmit_head++] = ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 info->xmit_head &= SERIAL_XMIT_SIZE - 1;
1299 info->xmit_cnt++;
1300 spin_unlock_irqrestore(&info->slock, flags);
Jiri Slaby1c456072008-02-07 00:16:46 -08001301 if (!tty->stopped) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001302 if (!tty->hw_stopped ||
1303 (info->type == PORT_16550A) ||
Jiri Slaby1c456072008-02-07 00:16:46 -08001304 info->board->chip_flag) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby1c456072008-02-07 00:16:46 -08001306 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 info->IER |= UART_IER_THRI;
Jiri Slaby1c456072008-02-07 00:16:46 -08001308 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 spin_unlock_irqrestore(&info->slock, flags);
1310 }
1311 }
Alan Cox0be2ead2008-04-30 00:54:03 -07001312 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313}
1314
1315
1316static void mxser_flush_chars(struct tty_struct *tty)
1317{
Jiri Slaby1c456072008-02-07 00:16:46 -08001318 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 unsigned long flags;
1320
Jiri Slabyace7dd92008-07-25 01:48:22 -07001321 if (info->xmit_cnt <= 0 || tty->stopped || !info->port.xmit_buf ||
1322 (tty->hw_stopped && info->type != PORT_16550A &&
1323 !info->board->chip_flag))
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;
Jiri Slabyace7dd92008-07-25 01:48:22 -07001341 return ret < 0 ? 0 : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342}
1343
1344static int mxser_chars_in_buffer(struct tty_struct *tty)
1345{
Jiri Slaby1c456072008-02-07 00:16:46 -08001346 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 return info->xmit_cnt;
1348}
1349
Jiri Slaby1c456072008-02-07 00:16:46 -08001350/*
1351 * ------------------------------------------------------------
1352 * friends of mxser_ioctl()
1353 * ------------------------------------------------------------
1354 */
Alan Cox216ba022008-10-13 10:40:19 +01001355static int mxser_get_serial_info(struct tty_struct *tty,
Jiri Slaby1c456072008-02-07 00:16:46 -08001356 struct serial_struct __user *retinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357{
Alan Cox216ba022008-10-13 10:40:19 +01001358 struct mxser_port *info = tty->driver_data;
Jiri Slaby1c456072008-02-07 00:16:46 -08001359 struct serial_struct tmp = {
1360 .type = info->type,
Alan Cox216ba022008-10-13 10:40:19 +01001361 .line = tty->index,
Jiri Slaby1c456072008-02-07 00:16:46 -08001362 .port = info->ioaddr,
1363 .irq = info->board->irq,
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001364 .flags = info->port.flags,
Jiri Slaby1c456072008-02-07 00:16:46 -08001365 .baud_base = info->baud_base,
Alan Cox44b7d1b2008-07-16 21:57:18 +01001366 .close_delay = info->port.close_delay,
1367 .closing_wait = info->port.closing_wait,
Jiri Slaby1c456072008-02-07 00:16:46 -08001368 .custom_divisor = info->custom_divisor,
1369 .hub6 = 0
1370 };
1371 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1372 return -EFAULT;
1373 return 0;
1374}
1375
Alan Cox216ba022008-10-13 10:40:19 +01001376static int mxser_set_serial_info(struct tty_struct *tty,
Jiri Slaby1c456072008-02-07 00:16:46 -08001377 struct serial_struct __user *new_info)
1378{
Alan Cox216ba022008-10-13 10:40:19 +01001379 struct mxser_port *info = tty->driver_data;
Jiri Slaby1c456072008-02-07 00:16:46 -08001380 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 Cox216ba022008-10-13 10:40:19 +01001413 tty->low_latency = (info->port.flags & ASYNC_LOW_LATENCY)
1414 ? 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 Cox216ba022008-10-13 10:40:19 +01001420 tty_encode_baud_rate(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);
Alan Cox216ba022008-10-13 10:40:19 +01001431 mxser_change_speed(tty, NULL);
Jiri Slaby1c456072008-02-07 00:16:46 -08001432 spin_unlock_irqrestore(&info->slock, sl_flags);
1433 }
1434 } else
Alan Cox216ba022008-10-13 10:40:19 +01001435 retval = mxser_startup(tty);
Jiri Slaby1c456072008-02-07 00:16:46 -08001436
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)
Alan Cox216ba022008-10-13 10:40:19 +01001481 mxser_check_modem_status(tty, info, status);
Jiri Slaby1c456072008-02-07 00:16:46 -08001482 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;
Alan Cox216ba022008-10-13 10:40:19 +01001626 struct tty_struct *tty;
Jiri Slaby1c456072008-02-07 00:16:46 -08001627 int result, status;
1628 unsigned int i, j;
Alan Cox9d6d1622008-04-30 00:53:20 -07001629 int ret = 0;
Jiri Slaby1c456072008-02-07 00:16:46 -08001630
1631 switch (cmd) {
1632 case MOXA_GET_MAJOR:
Jiri Slaby8f3d1372008-07-29 22:33:38 -07001633 if (printk_ratelimit())
1634 printk(KERN_WARNING "mxser: '%s' uses deprecated ioctl "
1635 "%x (GET_MAJOR), fix your userspace\n",
1636 current->comm, cmd);
Jiri Slaby1c456072008-02-07 00:16:46 -08001637 return put_user(ttymajor, (int __user *)argp);
1638
1639 case MOXA_CHKPORTENABLE:
1640 result = 0;
Alan Cox9d6d1622008-04-30 00:53:20 -07001641 lock_kernel();
Jiri Slaby1c456072008-02-07 00:16:46 -08001642 for (i = 0; i < MXSER_BOARDS; i++)
1643 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++)
1644 if (mxser_boards[i].ports[j].ioaddr)
1645 result |= (1 << i);
Alan Cox9d6d1622008-04-30 00:53:20 -07001646 unlock_kernel();
Jiri Slaby1c456072008-02-07 00:16:46 -08001647 return put_user(result, (unsigned long __user *)argp);
1648 case MOXA_GETDATACOUNT:
Alan Cox9d6d1622008-04-30 00:53:20 -07001649 lock_kernel();
Jiri Slaby1c456072008-02-07 00:16:46 -08001650 if (copy_to_user(argp, &mxvar_log, sizeof(mxvar_log)))
Alan Cox9d6d1622008-04-30 00:53:20 -07001651 ret = -EFAULT;
1652 unlock_kernel();
1653 return ret;
Jiri Slaby72800df2008-07-25 01:48:20 -07001654 case MOXA_GETMSTATUS: {
1655 struct mxser_mstatus ms, __user *msu = argp;
Alan Cox9d6d1622008-04-30 00:53:20 -07001656 lock_kernel();
Jiri Slaby1c456072008-02-07 00:16:46 -08001657 for (i = 0; i < MXSER_BOARDS; i++)
1658 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
1659 port = &mxser_boards[i].ports[j];
Jiri Slaby72800df2008-07-25 01:48:20 -07001660 memset(&ms, 0, sizeof(ms));
Jiri Slaby1c456072008-02-07 00:16:46 -08001661
Jiri Slaby72800df2008-07-25 01:48:20 -07001662 if (!port->ioaddr)
1663 goto copy;
Alan Cox216ba022008-10-13 10:40:19 +01001664
1665 tty = tty_port_tty_get(&port->port);
Jiri Slaby1c456072008-02-07 00:16:46 -08001666
Alan Cox216ba022008-10-13 10:40:19 +01001667 if (!tty || !tty->termios)
Jiri Slaby72800df2008-07-25 01:48:20 -07001668 ms.cflag = port->normal_termios.c_cflag;
Jiri Slaby1c456072008-02-07 00:16:46 -08001669 else
Alan Cox216ba022008-10-13 10:40:19 +01001670 ms.cflag = tty->termios->c_cflag;
1671 tty_kref_put(tty);
Jiri Slaby1c456072008-02-07 00:16:46 -08001672 status = inb(port->ioaddr + UART_MSR);
Jiri Slaby72800df2008-07-25 01:48:20 -07001673 if (status & UART_MSR_DCD)
1674 ms.dcd = 1;
1675 if (status & UART_MSR_DSR)
1676 ms.dsr = 1;
1677 if (status & UART_MSR_CTS)
1678 ms.cts = 1;
1679 copy:
1680 if (copy_to_user(msu, &ms, sizeof(ms))) {
1681 unlock_kernel();
1682 return -EFAULT;
1683 }
1684 msu++;
Jiri Slaby1c456072008-02-07 00:16:46 -08001685 }
Alan Cox9d6d1622008-04-30 00:53:20 -07001686 unlock_kernel();
Jiri Slaby1c456072008-02-07 00:16:46 -08001687 return 0;
Jiri Slaby72800df2008-07-25 01:48:20 -07001688 }
Jiri Slaby1c456072008-02-07 00:16:46 -08001689 case MOXA_ASPP_MON_EXT: {
Jiri Slaby72800df2008-07-25 01:48:20 -07001690 struct mxser_mon_ext *me; /* it's 2k, stack unfriendly */
1691 unsigned int cflag, iflag, p;
1692 u8 opmode;
1693
1694 me = kzalloc(sizeof(*me), GFP_KERNEL);
1695 if (!me)
1696 return -ENOMEM;
Jiri Slaby1c456072008-02-07 00:16:46 -08001697
Alan Cox9d6d1622008-04-30 00:53:20 -07001698 lock_kernel();
Jiri Slaby72800df2008-07-25 01:48:20 -07001699 for (i = 0, p = 0; i < MXSER_BOARDS; i++) {
1700 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++, p++) {
1701 if (p >= ARRAY_SIZE(me->rx_cnt)) {
1702 i = MXSER_BOARDS;
1703 break;
1704 }
Jiri Slaby1c456072008-02-07 00:16:46 -08001705 port = &mxser_boards[i].ports[j];
1706 if (!port->ioaddr)
1707 continue;
1708
Jiri Slaby72800df2008-07-25 01:48:20 -07001709 status = mxser_get_msr(port->ioaddr, 0, p);
Jiri Slaby1c456072008-02-07 00:16:46 -08001710
1711 if (status & UART_MSR_TERI)
1712 port->icount.rng++;
1713 if (status & UART_MSR_DDSR)
1714 port->icount.dsr++;
1715 if (status & UART_MSR_DDCD)
1716 port->icount.dcd++;
1717 if (status & UART_MSR_DCTS)
1718 port->icount.cts++;
1719
1720 port->mon_data.modem_status = status;
Jiri Slaby72800df2008-07-25 01:48:20 -07001721 me->rx_cnt[p] = port->mon_data.rxcnt;
1722 me->tx_cnt[p] = port->mon_data.txcnt;
1723 me->up_rxcnt[p] = port->mon_data.up_rxcnt;
1724 me->up_txcnt[p] = port->mon_data.up_txcnt;
1725 me->modem_status[p] =
Jiri Slaby1c456072008-02-07 00:16:46 -08001726 port->mon_data.modem_status;
Alan Cox216ba022008-10-13 10:40:19 +01001727 tty = tty_port_tty_get(&port->port);
Jiri Slaby1c456072008-02-07 00:16:46 -08001728
Alan Cox216ba022008-10-13 10:40:19 +01001729 if (!tty || !tty->termios) {
Jiri Slaby1c456072008-02-07 00:16:46 -08001730 cflag = port->normal_termios.c_cflag;
1731 iflag = port->normal_termios.c_iflag;
Alan Cox216ba022008-10-13 10:40:19 +01001732 me->baudrate[p] = tty_termios_baud_rate(&port->normal_termios);
Jiri Slaby1c456072008-02-07 00:16:46 -08001733 } else {
Alan Cox216ba022008-10-13 10:40:19 +01001734 cflag = tty->termios->c_cflag;
1735 iflag = tty->termios->c_iflag;
1736 me->baudrate[p] = tty_get_baud_rate(tty);
Jiri Slaby1c456072008-02-07 00:16:46 -08001737 }
Alan Cox216ba022008-10-13 10:40:19 +01001738 tty_kref_put(tty);
Jiri Slaby1c456072008-02-07 00:16:46 -08001739
Jiri Slaby72800df2008-07-25 01:48:20 -07001740 me->databits[p] = cflag & CSIZE;
1741 me->stopbits[p] = cflag & CSTOPB;
1742 me->parity[p] = cflag & (PARENB | PARODD |
1743 CMSPAR);
Jiri Slaby1c456072008-02-07 00:16:46 -08001744
1745 if (cflag & CRTSCTS)
Jiri Slaby72800df2008-07-25 01:48:20 -07001746 me->flowctrl[p] |= 0x03;
Jiri Slaby1c456072008-02-07 00:16:46 -08001747
1748 if (iflag & (IXON | IXOFF))
Jiri Slaby72800df2008-07-25 01:48:20 -07001749 me->flowctrl[p] |= 0x0C;
Jiri Slaby1c456072008-02-07 00:16:46 -08001750
1751 if (port->type == PORT_16550A)
Jiri Slaby72800df2008-07-25 01:48:20 -07001752 me->fifo[p] = 1;
Jiri Slaby1c456072008-02-07 00:16:46 -08001753
Jiri Slaby72800df2008-07-25 01:48:20 -07001754 opmode = inb(port->opmode_ioaddr) >>
1755 ((p % 4) * 2);
Jiri Slaby1c456072008-02-07 00:16:46 -08001756 opmode &= OP_MODE_MASK;
Jiri Slaby72800df2008-07-25 01:48:20 -07001757 me->iftype[p] = opmode;
Jiri Slaby1c456072008-02-07 00:16:46 -08001758 }
Alan Cox9d6d1622008-04-30 00:53:20 -07001759 }
1760 unlock_kernel();
Jiri Slaby72800df2008-07-25 01:48:20 -07001761 if (copy_to_user(argp, me, sizeof(*me)))
1762 ret = -EFAULT;
1763 kfree(me);
1764 return ret;
Alan Cox9d6d1622008-04-30 00:53:20 -07001765 }
1766 default:
Jiri Slaby1c456072008-02-07 00:16:46 -08001767 return -ENOIOCTLCMD;
1768 }
1769 return 0;
1770}
1771
1772static int mxser_cflags_changed(struct mxser_port *info, unsigned long arg,
1773 struct async_icount *cprev)
1774{
1775 struct async_icount cnow;
1776 unsigned long flags;
1777 int ret;
1778
1779 spin_lock_irqsave(&info->slock, flags);
1780 cnow = info->icount; /* atomic copy */
1781 spin_unlock_irqrestore(&info->slock, flags);
1782
1783 ret = ((arg & TIOCM_RNG) && (cnow.rng != cprev->rng)) ||
1784 ((arg & TIOCM_DSR) && (cnow.dsr != cprev->dsr)) ||
1785 ((arg & TIOCM_CD) && (cnow.dcd != cprev->dcd)) ||
1786 ((arg & TIOCM_CTS) && (cnow.cts != cprev->cts));
1787
1788 *cprev = cnow;
1789
1790 return ret;
1791}
1792
1793static int mxser_ioctl(struct tty_struct *tty, struct file *file,
1794 unsigned int cmd, unsigned long arg)
1795{
1796 struct mxser_port *info = tty->driver_data;
1797 struct async_icount cnow;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 unsigned long flags;
1799 void __user *argp = (void __user *)arg;
Jiri Slaby1c456072008-02-07 00:16:46 -08001800 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801
1802 if (tty->index == MXSER_PORTS)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001803 return mxser_ioctl_special(cmd, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 if (cmd == MOXA_SET_OP_MODE || cmd == MOXA_GET_OP_MODE) {
Jiri Slaby1c456072008-02-07 00:16:46 -08001806 int p;
1807 unsigned long opmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 static unsigned char ModeMask[] = { 0xfc, 0xf3, 0xcf, 0x3f };
1809 int shiftbit;
1810 unsigned char val, mask;
1811
Jiri Slaby1c456072008-02-07 00:16:46 -08001812 p = tty->index % 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 if (cmd == MOXA_SET_OP_MODE) {
1814 if (get_user(opmode, (int __user *) argp))
1815 return -EFAULT;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001816 if (opmode != RS232_MODE &&
1817 opmode != RS485_2WIRE_MODE &&
1818 opmode != RS422_MODE &&
1819 opmode != RS485_4WIRE_MODE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 return -EFAULT;
Alan Cox9d6d1622008-04-30 00:53:20 -07001821 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 mask = ModeMask[p];
1823 shiftbit = p * 2;
1824 val = inb(info->opmode_ioaddr);
1825 val &= mask;
1826 val |= (opmode << shiftbit);
1827 outb(val, info->opmode_ioaddr);
Alan Cox9d6d1622008-04-30 00:53:20 -07001828 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 } else {
Alan Cox9d6d1622008-04-30 00:53:20 -07001830 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 shiftbit = p * 2;
1832 opmode = inb(info->opmode_ioaddr) >> shiftbit;
1833 opmode &= OP_MODE_MASK;
Alan Cox9d6d1622008-04-30 00:53:20 -07001834 unlock_kernel();
Jiri Slaby1c456072008-02-07 00:16:46 -08001835 if (put_user(opmode, (int __user *)argp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 return -EFAULT;
1837 }
1838 return 0;
1839 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840
Jiri Slaby1c456072008-02-07 00:16:46 -08001841 if (cmd != TIOCGSERIAL && cmd != TIOCMIWAIT && cmd != TIOCGICOUNT &&
1842 test_bit(TTY_IO_ERROR, &tty->flags))
1843 return -EIO;
1844
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 case TIOCGSERIAL:
Alan Cox9d6d1622008-04-30 00:53:20 -07001847 lock_kernel();
Alan Cox216ba022008-10-13 10:40:19 +01001848 retval = mxser_get_serial_info(tty, argp);
Alan Cox9d6d1622008-04-30 00:53:20 -07001849 unlock_kernel();
1850 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 case TIOCSSERIAL:
Alan Cox9d6d1622008-04-30 00:53:20 -07001852 lock_kernel();
Alan Cox216ba022008-10-13 10:40:19 +01001853 retval = mxser_set_serial_info(tty, argp);
Alan Cox9d6d1622008-04-30 00:53:20 -07001854 unlock_kernel();
1855 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 case TIOCSERGETLSR: /* Get line status register */
Alan Cox9d6d1622008-04-30 00:53:20 -07001857 return mxser_get_lsr_info(info, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 /*
1859 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1860 * - mask passed in arg for lines of interest
1861 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1862 * Caller should use TIOCGICOUNT to see which one it was
1863 */
Jiri Slabyfc838152007-04-23 14:41:04 -07001864 case TIOCMIWAIT:
1865 spin_lock_irqsave(&info->slock, flags);
1866 cnow = info->icount; /* note the counters on entry */
1867 spin_unlock_irqrestore(&info->slock, flags);
1868
Jiri Slaby1c456072008-02-07 00:16:46 -08001869 return wait_event_interruptible(info->delta_msr_wait,
1870 mxser_cflags_changed(info, arg, &cnow));
1871 /*
1872 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1873 * Return: write counters to the user passed counter struct
1874 * NB: both 1->0 and 0->1 transitions are counted except for
1875 * RI where only 0->1 is counted.
1876 */
Jiri Slaby41aee9a2008-07-25 01:48:19 -07001877 case TIOCGICOUNT: {
1878 struct serial_icounter_struct icnt = { 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 spin_lock_irqsave(&info->slock, flags);
1880 cnow = info->icount;
1881 spin_unlock_irqrestore(&info->slock, flags);
Jiri Slaby41aee9a2008-07-25 01:48:19 -07001882
1883 icnt.frame = cnow.frame;
1884 icnt.brk = cnow.brk;
1885 icnt.overrun = cnow.overrun;
1886 icnt.buf_overrun = cnow.buf_overrun;
1887 icnt.parity = cnow.parity;
1888 icnt.rx = cnow.rx;
1889 icnt.tx = cnow.tx;
1890 icnt.cts = cnow.cts;
1891 icnt.dsr = cnow.dsr;
1892 icnt.rng = cnow.rng;
1893 icnt.dcd = cnow.dcd;
1894
1895 return copy_to_user(argp, &icnt, sizeof(icnt)) ? -EFAULT : 0;
1896 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 case MOXA_HighSpeedOn:
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001898 return put_user(info->baud_base != 115200 ? 1 : 0, (int __user *)argp);
Jiri Slaby1c456072008-02-07 00:16:46 -08001899 case MOXA_SDS_RSTICOUNTER:
Alan Cox9d6d1622008-04-30 00:53:20 -07001900 lock_kernel();
Jiri Slaby1c456072008-02-07 00:16:46 -08001901 info->mon_data.rxcnt = 0;
1902 info->mon_data.txcnt = 0;
Alan Cox9d6d1622008-04-30 00:53:20 -07001903 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 return 0;
1905
1906 case MOXA_ASPP_OQUEUE:{
Jiri Slaby1c456072008-02-07 00:16:46 -08001907 int len, lsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908
Alan Cox9d6d1622008-04-30 00:53:20 -07001909 lock_kernel();
Jiri Slaby1c456072008-02-07 00:16:46 -08001910 len = mxser_chars_in_buffer(tty);
Jiri Slaby1c456072008-02-07 00:16:46 -08001911 lsr = inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT;
Jiri Slaby1c456072008-02-07 00:16:46 -08001912 len += (lsr ? 0 : 1);
Alan Cox9d6d1622008-04-30 00:53:20 -07001913 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914
Jiri Slaby1c456072008-02-07 00:16:46 -08001915 return put_user(len, (int __user *)argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 }
Jiri Slaby1c456072008-02-07 00:16:46 -08001917 case MOXA_ASPP_MON: {
1918 int mcr, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919
Alan Cox9d6d1622008-04-30 00:53:20 -07001920 lock_kernel();
Jiri Slaby1c456072008-02-07 00:16:46 -08001921 status = mxser_get_msr(info->ioaddr, 1, tty->index);
Alan Cox216ba022008-10-13 10:40:19 +01001922 mxser_check_modem_status(tty, info, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923
Jiri Slaby1c456072008-02-07 00:16:46 -08001924 mcr = inb(info->ioaddr + UART_MCR);
1925 if (mcr & MOXA_MUST_MCR_XON_FLAG)
1926 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFHOLD;
1927 else
1928 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFHOLD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929
Jiri Slaby1c456072008-02-07 00:16:46 -08001930 if (mcr & MOXA_MUST_MCR_TX_XON)
1931 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFXENT;
1932 else
1933 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFXENT;
1934
Alan Cox216ba022008-10-13 10:40:19 +01001935 if (tty->hw_stopped)
Jiri Slaby1c456072008-02-07 00:16:46 -08001936 info->mon_data.hold_reason |= NPPI_NOTIFY_CTSHOLD;
1937 else
1938 info->mon_data.hold_reason &= ~NPPI_NOTIFY_CTSHOLD;
Alan Cox9d6d1622008-04-30 00:53:20 -07001939 unlock_kernel();
Jiri Slaby1c456072008-02-07 00:16:46 -08001940 if (copy_to_user(argp, &info->mon_data,
1941 sizeof(struct mxser_mon)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 return -EFAULT;
Jiri Slaby1c456072008-02-07 00:16:46 -08001943
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 return 0;
Jiri Slaby1c456072008-02-07 00:16:46 -08001945 }
1946 case MOXA_ASPP_LSTATUS: {
1947 if (put_user(info->err_shadow, (unsigned char __user *)argp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
Jiri Slaby1c456072008-02-07 00:16:46 -08001950 info->err_shadow = 0;
1951 return 0;
1952 }
1953 case MOXA_SET_BAUD_METHOD: {
1954 int method;
1955
1956 if (get_user(method, (int __user *)argp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 return -EFAULT;
Jiri Slaby1c456072008-02-07 00:16:46 -08001958 mxser_set_baud_method[tty->index] = method;
1959 return put_user(method, (int __user *)argp);
1960 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 default:
1962 return -ENOIOCTLCMD;
1963 }
1964 return 0;
1965}
1966
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967static void mxser_stoprx(struct tty_struct *tty)
1968{
Jiri Slaby1c456072008-02-07 00:16:46 -08001969 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
1971 info->ldisc_stop_rx = 1;
1972 if (I_IXOFF(tty)) {
Jiri Slaby1c456072008-02-07 00:16:46 -08001973 if (info->board->chip_flag) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 info->IER &= ~MOXA_MUST_RECV_ISR;
Jiri Slaby1c456072008-02-07 00:16:46 -08001975 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 info->x_char = STOP_CHAR(tty);
Jiri Slaby1c456072008-02-07 00:16:46 -08001978 outb(0, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 info->IER |= UART_IER_THRI;
Jiri Slaby1c456072008-02-07 00:16:46 -08001980 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 }
1982 }
1983
Alan Cox216ba022008-10-13 10:40:19 +01001984 if (tty->termios->c_cflag & CRTSCTS) {
Jiri Slaby1c456072008-02-07 00:16:46 -08001985 info->MCR &= ~UART_MCR_RTS;
1986 outb(info->MCR, info->ioaddr + UART_MCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 }
1988}
1989
1990/*
1991 * This routine is called by the upper-layer tty layer to signal that
1992 * incoming characters should be throttled.
1993 */
1994static void mxser_throttle(struct tty_struct *tty)
1995{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 mxser_stoprx(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997}
1998
1999static void mxser_unthrottle(struct tty_struct *tty)
2000{
Jiri Slaby1c456072008-02-07 00:16:46 -08002001 struct mxser_port *info = tty->driver_data;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002002
Jiri Slaby1c456072008-02-07 00:16:46 -08002003 /* startrx */
2004 info->ldisc_stop_rx = 0;
2005 if (I_IXOFF(tty)) {
2006 if (info->x_char)
2007 info->x_char = 0;
2008 else {
2009 if (info->board->chip_flag) {
2010 info->IER |= MOXA_MUST_RECV_ISR;
2011 outb(info->IER, info->ioaddr + UART_IER);
2012 } else {
2013 info->x_char = START_CHAR(tty);
2014 outb(0, info->ioaddr + UART_IER);
2015 info->IER |= UART_IER_THRI;
2016 outb(info->IER, info->ioaddr + UART_IER);
2017 }
2018 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 }
2020
Alan Cox216ba022008-10-13 10:40:19 +01002021 if (tty->termios->c_cflag & CRTSCTS) {
Jiri Slaby1c456072008-02-07 00:16:46 -08002022 info->MCR |= UART_MCR_RTS;
2023 outb(info->MCR, info->ioaddr + UART_MCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 }
2025}
2026
2027/*
2028 * mxser_stop() and mxser_start()
2029 *
2030 * This routines are called before setting or resetting tty->stopped.
2031 * They enable or disable transmitter interrupts, as necessary.
2032 */
2033static void mxser_stop(struct tty_struct *tty)
2034{
Jiri Slaby1c456072008-02-07 00:16:46 -08002035 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 unsigned long flags;
2037
2038 spin_lock_irqsave(&info->slock, flags);
2039 if (info->IER & UART_IER_THRI) {
2040 info->IER &= ~UART_IER_THRI;
Jiri Slaby1c456072008-02-07 00:16:46 -08002041 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 }
2043 spin_unlock_irqrestore(&info->slock, flags);
2044}
2045
2046static void mxser_start(struct tty_struct *tty)
2047{
Jiri Slaby1c456072008-02-07 00:16:46 -08002048 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 unsigned long flags;
2050
2051 spin_lock_irqsave(&info->slock, flags);
Alan Cox0ad9e7d2008-07-16 21:56:10 +01002052 if (info->xmit_cnt && info->port.xmit_buf) {
Jiri Slaby1c456072008-02-07 00:16:46 -08002053 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 info->IER |= UART_IER_THRI;
Jiri Slaby1c456072008-02-07 00:16:46 -08002055 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 }
2057 spin_unlock_irqrestore(&info->slock, flags);
2058}
2059
Jiri Slaby1c456072008-02-07 00:16:46 -08002060static void mxser_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
2061{
2062 struct mxser_port *info = tty->driver_data;
2063 unsigned long flags;
2064
2065 spin_lock_irqsave(&info->slock, flags);
Alan Cox216ba022008-10-13 10:40:19 +01002066 mxser_change_speed(tty, old_termios);
Jiri Slaby1c456072008-02-07 00:16:46 -08002067 spin_unlock_irqrestore(&info->slock, flags);
2068
2069 if ((old_termios->c_cflag & CRTSCTS) &&
2070 !(tty->termios->c_cflag & CRTSCTS)) {
2071 tty->hw_stopped = 0;
2072 mxser_start(tty);
2073 }
2074
2075 /* Handle sw stopped */
2076 if ((old_termios->c_iflag & IXON) &&
2077 !(tty->termios->c_iflag & IXON)) {
2078 tty->stopped = 0;
2079
2080 if (info->board->chip_flag) {
2081 spin_lock_irqsave(&info->slock, flags);
Christoph Hellwig148ff862008-04-30 00:54:29 -07002082 mxser_disable_must_rx_software_flow_control(
2083 info->ioaddr);
Jiri Slaby1c456072008-02-07 00:16:46 -08002084 spin_unlock_irqrestore(&info->slock, flags);
2085 }
2086
2087 mxser_start(tty);
2088 }
2089}
2090
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091/*
2092 * mxser_wait_until_sent() --- wait until the transmitter is empty
2093 */
2094static void mxser_wait_until_sent(struct tty_struct *tty, int timeout)
2095{
Jiri Slaby1c456072008-02-07 00:16:46 -08002096 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 unsigned long orig_jiffies, char_time;
2098 int lsr;
2099
2100 if (info->type == PORT_UNKNOWN)
2101 return;
2102
2103 if (info->xmit_fifo_size == 0)
2104 return; /* Just in case.... */
2105
2106 orig_jiffies = jiffies;
2107 /*
2108 * Set the check interval to be 1/5 of the estimated time to
2109 * send a single character, and make it at least 1. The check
2110 * interval should also be less than the timeout.
2111 *
2112 * Note: we have to use pretty tight timings here to satisfy
2113 * the NIST-PCTS.
2114 */
2115 char_time = (info->timeout - HZ / 50) / info->xmit_fifo_size;
2116 char_time = char_time / 5;
2117 if (char_time == 0)
2118 char_time = 1;
2119 if (timeout && timeout < char_time)
2120 char_time = timeout;
2121 /*
2122 * If the transmitter hasn't cleared in twice the approximate
2123 * amount of time to send the entire FIFO, it probably won't
2124 * ever clear. This assumes the UART isn't doing flow
2125 * control, which is currently the case. Hence, if it ever
2126 * takes longer than info->timeout, this is probably due to a
2127 * UART bug of some kind. So, we clamp the timeout parameter at
2128 * 2*info->timeout.
2129 */
2130 if (!timeout || timeout > 2 * info->timeout)
2131 timeout = 2 * info->timeout;
2132#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002133 printk(KERN_DEBUG "In rs_wait_until_sent(%d) check=%lu...",
2134 timeout, char_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 printk("jiff=%lu...", jiffies);
2136#endif
Alan Cox978e5952008-04-30 00:53:59 -07002137 lock_kernel();
Jiri Slaby1c456072008-02-07 00:16:46 -08002138 while (!((lsr = inb(info->ioaddr + UART_LSR)) & UART_LSR_TEMT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2140 printk("lsr = %d (jiff=%lu)...", lsr, jiffies);
2141#endif
Nishanth Aravamudanda4cd8d2005-09-10 00:27:30 -07002142 schedule_timeout_interruptible(char_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 if (signal_pending(current))
2144 break;
2145 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2146 break;
2147 }
2148 set_current_state(TASK_RUNNING);
Alan Cox978e5952008-04-30 00:53:59 -07002149 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150
2151#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2152 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
2153#endif
2154}
2155
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156/*
2157 * This routine is called by tty_hangup() when a hangup is signaled.
2158 */
Jiri Slaby1c456072008-02-07 00:16:46 -08002159static void mxser_hangup(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160{
Jiri Slaby1c456072008-02-07 00:16:46 -08002161 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162
2163 mxser_flush_buffer(tty);
Alan Cox216ba022008-10-13 10:40:19 +01002164 mxser_shutdown(tty);
Alan Cox3b6826b2009-01-02 13:45:58 +00002165 tty_port_hangup(&info->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166}
2167
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168/*
2169 * mxser_rs_break() --- routine which turns the break handling on or off
2170 */
Alan Cox9e989662008-07-22 11:18:03 +01002171static int mxser_rs_break(struct tty_struct *tty, int break_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172{
Jiri Slaby1c456072008-02-07 00:16:46 -08002173 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 unsigned long flags;
2175
2176 spin_lock_irqsave(&info->slock, flags);
2177 if (break_state == -1)
Jiri Slaby1c456072008-02-07 00:16:46 -08002178 outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
2179 info->ioaddr + UART_LCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 else
Jiri Slaby1c456072008-02-07 00:16:46 -08002181 outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
2182 info->ioaddr + UART_LCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 spin_unlock_irqrestore(&info->slock, flags);
Alan Cox9e989662008-07-22 11:18:03 +01002184 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185}
2186
Alan Cox216ba022008-10-13 10:40:19 +01002187static void mxser_receive_chars(struct tty_struct *tty,
2188 struct mxser_port *port, int *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 unsigned char ch, gdl;
2191 int ignored = 0;
2192 int cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 int recv_room;
2194 int max = 256;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195
Alan Cox33f0f882006-01-09 20:54:13 -08002196 recv_room = tty->receive_room;
Alan Cox216ba022008-10-13 10:40:19 +01002197 if (recv_room == 0 && !port->ldisc_stop_rx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 mxser_stoprx(tty);
Jiri Slaby1c456072008-02-07 00:16:46 -08002199 if (port->board->chip_flag != MOXA_OTHER_UART) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200
Jiri Slaby1c456072008-02-07 00:16:46 -08002201 if (*status & UART_LSR_SPECIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 goto intr_old;
Jiri Slaby1c456072008-02-07 00:16:46 -08002203 if (port->board->chip_flag == MOXA_MUST_MU860_HWID &&
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002204 (*status & MOXA_MUST_LSR_RERR))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 goto intr_old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 if (*status & MOXA_MUST_LSR_RERR)
2207 goto intr_old;
2208
Jiri Slaby1c456072008-02-07 00:16:46 -08002209 gdl = inb(port->ioaddr + MOXA_MUST_GDL_REGISTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210
Jiri Slaby1c456072008-02-07 00:16:46 -08002211 if (port->board->chip_flag == MOXA_MUST_MU150_HWID)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 gdl &= MOXA_MUST_GDL_MASK;
2213 if (gdl >= recv_room) {
Jiri Slaby1c456072008-02-07 00:16:46 -08002214 if (!port->ldisc_stop_rx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 mxser_stoprx(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 }
2217 while (gdl--) {
Jiri Slaby1c456072008-02-07 00:16:46 -08002218 ch = inb(port->ioaddr + UART_RX);
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002219 tty_insert_flip_char(tty, ch, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 }
2222 goto end_intr;
2223 }
Jiri Slaby1c456072008-02-07 00:16:46 -08002224intr_old:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225
2226 do {
2227 if (max-- < 0)
2228 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229
Jiri Slaby1c456072008-02-07 00:16:46 -08002230 ch = inb(port->ioaddr + UART_RX);
2231 if (port->board->chip_flag && (*status & UART_LSR_OE))
2232 outb(0x23, port->ioaddr + UART_FCR);
2233 *status &= port->read_status_mask;
2234 if (*status & port->ignore_status_mask) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 if (++ignored > 100)
2236 break;
2237 } else {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002238 char flag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 if (*status & UART_LSR_SPECIAL) {
2240 if (*status & UART_LSR_BI) {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002241 flag = TTY_BREAK;
Jiri Slaby1c456072008-02-07 00:16:46 -08002242 port->icount.brk++;
2243
Alan Cox0ad9e7d2008-07-16 21:56:10 +01002244 if (port->port.flags & ASYNC_SAK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 do_SAK(tty);
2246 } else if (*status & UART_LSR_PE) {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002247 flag = TTY_PARITY;
Jiri Slaby1c456072008-02-07 00:16:46 -08002248 port->icount.parity++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 } else if (*status & UART_LSR_FE) {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002250 flag = TTY_FRAME;
Jiri Slaby1c456072008-02-07 00:16:46 -08002251 port->icount.frame++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 } else if (*status & UART_LSR_OE) {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002253 flag = TTY_OVERRUN;
Jiri Slaby1c456072008-02-07 00:16:46 -08002254 port->icount.overrun++;
2255 } else
2256 flag = TTY_BREAK;
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002257 }
2258 tty_insert_flip_char(tty, ch, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 cnt++;
2260 if (cnt >= recv_room) {
Jiri Slaby1c456072008-02-07 00:16:46 -08002261 if (!port->ldisc_stop_rx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 mxser_stoprx(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 break;
2264 }
2265
2266 }
2267
Jiri Slaby1c456072008-02-07 00:16:46 -08002268 if (port->board->chip_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270
Jiri Slaby1c456072008-02-07 00:16:46 -08002271 *status = inb(port->ioaddr + UART_LSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 } while (*status & UART_LSR_DR);
2273
Jiri Slaby1c456072008-02-07 00:16:46 -08002274end_intr:
Alan Cox216ba022008-10-13 10:40:19 +01002275 mxvar_log.rxcnt[tty->index] += cnt;
Jiri Slaby1c456072008-02-07 00:16:46 -08002276 port->mon_data.rxcnt += cnt;
2277 port->mon_data.up_rxcnt += cnt;
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002278
Jiri Slaby1c456072008-02-07 00:16:46 -08002279 /*
2280 * We are called from an interrupt context with &port->slock
2281 * being held. Drop it temporarily in order to prevent
2282 * recursive locking.
2283 */
2284 spin_unlock(&port->slock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 tty_flip_buffer_push(tty);
Jiri Slaby1c456072008-02-07 00:16:46 -08002286 spin_lock(&port->slock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287}
2288
Alan Cox216ba022008-10-13 10:40:19 +01002289static void mxser_transmit_chars(struct tty_struct *tty, struct mxser_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290{
2291 int count, cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292
Jiri Slaby1c456072008-02-07 00:16:46 -08002293 if (port->x_char) {
2294 outb(port->x_char, port->ioaddr + UART_TX);
2295 port->x_char = 0;
Alan Cox216ba022008-10-13 10:40:19 +01002296 mxvar_log.txcnt[tty->index]++;
Jiri Slaby1c456072008-02-07 00:16:46 -08002297 port->mon_data.txcnt++;
2298 port->mon_data.up_txcnt++;
2299 port->icount.tx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 return;
2301 }
2302
Alan Cox0ad9e7d2008-07-16 21:56:10 +01002303 if (port->port.xmit_buf == NULL)
Jiri Slaby1c456072008-02-07 00:16:46 -08002304 return;
2305
Alan Cox216ba022008-10-13 10:40:19 +01002306 if (port->xmit_cnt <= 0 || tty->stopped ||
2307 (tty->hw_stopped &&
Jiri Slaby1c456072008-02-07 00:16:46 -08002308 (port->type != PORT_16550A) &&
2309 (!port->board->chip_flag))) {
2310 port->IER &= ~UART_IER_THRI;
2311 outb(port->IER, port->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 return;
2313 }
2314
Jiri Slaby1c456072008-02-07 00:16:46 -08002315 cnt = port->xmit_cnt;
2316 count = port->xmit_fifo_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 do {
Alan Cox0ad9e7d2008-07-16 21:56:10 +01002318 outb(port->port.xmit_buf[port->xmit_tail++],
Jiri Slaby1c456072008-02-07 00:16:46 -08002319 port->ioaddr + UART_TX);
2320 port->xmit_tail = port->xmit_tail & (SERIAL_XMIT_SIZE - 1);
2321 if (--port->xmit_cnt <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 break;
2323 } while (--count > 0);
Alan Cox216ba022008-10-13 10:40:19 +01002324 mxvar_log.txcnt[tty->index] += (cnt - port->xmit_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325
Jiri Slaby1c456072008-02-07 00:16:46 -08002326 port->mon_data.txcnt += (cnt - port->xmit_cnt);
2327 port->mon_data.up_txcnt += (cnt - port->xmit_cnt);
2328 port->icount.tx += (cnt - port->xmit_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329
Alan Cox216ba022008-10-13 10:40:19 +01002330 if (port->xmit_cnt < WAKEUP_CHARS && tty)
2331 tty_wakeup(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332
Jiri Slaby1c456072008-02-07 00:16:46 -08002333 if (port->xmit_cnt <= 0) {
2334 port->IER &= ~UART_IER_THRI;
2335 outb(port->IER, port->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337}
2338
2339/*
Jiri Slaby1c456072008-02-07 00:16:46 -08002340 * This is the serial driver's generic interrupt routine
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 */
Jiri Slaby1c456072008-02-07 00:16:46 -08002342static irqreturn_t mxser_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343{
Jiri Slaby1c456072008-02-07 00:16:46 -08002344 int status, iir, i;
2345 struct mxser_board *brd = NULL;
2346 struct mxser_port *port;
2347 int max, irqbits, bits, msr;
2348 unsigned int int_cnt, pass_counter = 0;
2349 int handled = IRQ_NONE;
Alan Cox216ba022008-10-13 10:40:19 +01002350 struct tty_struct *tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351
Jiri Slaby1c456072008-02-07 00:16:46 -08002352 for (i = 0; i < MXSER_BOARDS; i++)
2353 if (dev_id == &mxser_boards[i]) {
2354 brd = dev_id;
2355 break;
2356 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357
Jiri Slaby1c456072008-02-07 00:16:46 -08002358 if (i == MXSER_BOARDS)
2359 goto irq_stop;
2360 if (brd == NULL)
2361 goto irq_stop;
2362 max = brd->info->nports;
2363 while (pass_counter++ < MXSER_ISR_PASS_LIMIT) {
2364 irqbits = inb(brd->vector) & brd->vector_mask;
2365 if (irqbits == brd->vector_mask)
2366 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367
Jiri Slaby1c456072008-02-07 00:16:46 -08002368 handled = IRQ_HANDLED;
2369 for (i = 0, bits = 1; i < max; i++, irqbits |= bits, bits <<= 1) {
2370 if (irqbits == brd->vector_mask)
2371 break;
2372 if (bits & irqbits)
2373 continue;
2374 port = &brd->ports[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375
Jiri Slaby1c456072008-02-07 00:16:46 -08002376 int_cnt = 0;
2377 spin_lock(&port->slock);
2378 do {
2379 iir = inb(port->ioaddr + UART_IIR);
2380 if (iir & UART_IIR_NO_INT)
2381 break;
2382 iir &= MOXA_MUST_IIR_MASK;
Alan Cox216ba022008-10-13 10:40:19 +01002383 tty = tty_port_tty_get(&port->port);
2384 if (!tty ||
Alan Cox0ad9e7d2008-07-16 21:56:10 +01002385 (port->port.flags & ASYNC_CLOSING) ||
2386 !(port->port.flags &
Jiri Slaby1c456072008-02-07 00:16:46 -08002387 ASYNC_INITIALIZED)) {
2388 status = inb(port->ioaddr + UART_LSR);
2389 outb(0x27, port->ioaddr + UART_FCR);
2390 inb(port->ioaddr + UART_MSR);
Alan Cox216ba022008-10-13 10:40:19 +01002391 tty_kref_put(tty);
Jiri Slaby1c456072008-02-07 00:16:46 -08002392 break;
2393 }
2394
2395 status = inb(port->ioaddr + UART_LSR);
2396
2397 if (status & UART_LSR_PE)
2398 port->err_shadow |= NPPI_NOTIFY_PARITY;
2399 if (status & UART_LSR_FE)
2400 port->err_shadow |= NPPI_NOTIFY_FRAMING;
2401 if (status & UART_LSR_OE)
2402 port->err_shadow |=
2403 NPPI_NOTIFY_HW_OVERRUN;
2404 if (status & UART_LSR_BI)
2405 port->err_shadow |= NPPI_NOTIFY_BREAK;
2406
2407 if (port->board->chip_flag) {
2408 if (iir == MOXA_MUST_IIR_GDA ||
2409 iir == MOXA_MUST_IIR_RDA ||
2410 iir == MOXA_MUST_IIR_RTO ||
2411 iir == MOXA_MUST_IIR_LSR)
Alan Cox216ba022008-10-13 10:40:19 +01002412 mxser_receive_chars(tty, port,
Jiri Slaby1c456072008-02-07 00:16:46 -08002413 &status);
2414
2415 } else {
2416 status &= port->read_status_mask;
2417 if (status & UART_LSR_DR)
Alan Cox216ba022008-10-13 10:40:19 +01002418 mxser_receive_chars(tty, port,
Jiri Slaby1c456072008-02-07 00:16:46 -08002419 &status);
2420 }
2421 msr = inb(port->ioaddr + UART_MSR);
2422 if (msr & UART_MSR_ANY_DELTA)
Alan Cox216ba022008-10-13 10:40:19 +01002423 mxser_check_modem_status(tty, port, msr);
Jiri Slaby1c456072008-02-07 00:16:46 -08002424
2425 if (port->board->chip_flag) {
2426 if (iir == 0x02 && (status &
2427 UART_LSR_THRE))
Alan Cox216ba022008-10-13 10:40:19 +01002428 mxser_transmit_chars(tty, port);
Jiri Slaby1c456072008-02-07 00:16:46 -08002429 } else {
2430 if (status & UART_LSR_THRE)
Alan Cox216ba022008-10-13 10:40:19 +01002431 mxser_transmit_chars(tty, port);
Jiri Slaby1c456072008-02-07 00:16:46 -08002432 }
Alan Cox216ba022008-10-13 10:40:19 +01002433 tty_kref_put(tty);
Jiri Slaby1c456072008-02-07 00:16:46 -08002434 } while (int_cnt++ < MXSER_ISR_PASS_LIMIT);
2435 spin_unlock(&port->slock);
2436 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 }
2438
Jiri Slaby1c456072008-02-07 00:16:46 -08002439irq_stop:
2440 return handled;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441}
2442
Jiri Slaby1c456072008-02-07 00:16:46 -08002443static const struct tty_operations mxser_ops = {
2444 .open = mxser_open,
2445 .close = mxser_close,
2446 .write = mxser_write,
2447 .put_char = mxser_put_char,
2448 .flush_chars = mxser_flush_chars,
2449 .write_room = mxser_write_room,
2450 .chars_in_buffer = mxser_chars_in_buffer,
2451 .flush_buffer = mxser_flush_buffer,
2452 .ioctl = mxser_ioctl,
2453 .throttle = mxser_throttle,
2454 .unthrottle = mxser_unthrottle,
2455 .set_termios = mxser_set_termios,
2456 .stop = mxser_stop,
2457 .start = mxser_start,
2458 .hangup = mxser_hangup,
2459 .break_ctl = mxser_rs_break,
2460 .wait_until_sent = mxser_wait_until_sent,
2461 .tiocmget = mxser_tiocmget,
2462 .tiocmset = mxser_tiocmset,
2463};
2464
Alan Cox31f35932009-01-02 13:45:05 +00002465struct tty_port_operations mxser_port_ops = {
2466 .carrier_raised = mxser_carrier_raised,
Alan Cox5d951fb2009-01-02 13:45:19 +00002467 .raise_dtr_rts = mxser_raise_dtr_rts,
Alan Cox31f35932009-01-02 13:45:05 +00002468};
2469
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470/*
Jiri Slaby1c456072008-02-07 00:16:46 -08002471 * The MOXA Smartio/Industio serial driver boot-time initialization code!
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 */
Jiri Slaby1c456072008-02-07 00:16:46 -08002473
2474static void mxser_release_res(struct mxser_board *brd, struct pci_dev *pdev,
2475 unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476{
Jiri Slaby1c456072008-02-07 00:16:46 -08002477 if (irq)
2478 free_irq(brd->irq, brd);
2479 if (pdev != NULL) { /* PCI */
2480#ifdef CONFIG_PCI
2481 pci_release_region(pdev, 2);
2482 pci_release_region(pdev, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 } else {
Jiri Slaby1c456072008-02-07 00:16:46 -08002485 release_region(brd->ports[0].ioaddr, 8 * brd->info->nports);
2486 release_region(brd->vector, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488}
2489
Jiri Slaby1c456072008-02-07 00:16:46 -08002490static int __devinit mxser_initbrd(struct mxser_board *brd,
2491 struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492{
Jiri Slaby1c456072008-02-07 00:16:46 -08002493 struct mxser_port *info;
2494 unsigned int i;
2495 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496
Jiri Slaby83766bc2008-07-25 01:48:21 -07002497 printk(KERN_INFO "mxser: max. baud rate = %d bps\n",
2498 brd->ports[0].max_baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499
Jiri Slaby1c456072008-02-07 00:16:46 -08002500 for (i = 0; i < brd->info->nports; i++) {
2501 info = &brd->ports[i];
Alan Cox44b7d1b2008-07-16 21:57:18 +01002502 tty_port_init(&info->port);
Alan Cox31f35932009-01-02 13:45:05 +00002503 info->port.ops = &mxser_port_ops;
Jiri Slaby1c456072008-02-07 00:16:46 -08002504 info->board = brd;
2505 info->stop_rx = 0;
2506 info->ldisc_stop_rx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507
Jiri Slaby1c456072008-02-07 00:16:46 -08002508 /* Enhance mode enabled here */
2509 if (brd->chip_flag != MOXA_OTHER_UART)
Christoph Hellwig148ff862008-04-30 00:54:29 -07002510 mxser_enable_must_enchance_mode(info->ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511
Alan Cox0ad9e7d2008-07-16 21:56:10 +01002512 info->port.flags = ASYNC_SHARE_IRQ;
Jiri Slaby1c456072008-02-07 00:16:46 -08002513 info->type = brd->uart_type;
2514
2515 process_txrx_fifo(info);
2516
2517 info->custom_divisor = info->baud_base * 16;
Alan Cox44b7d1b2008-07-16 21:57:18 +01002518 info->port.close_delay = 5 * HZ / 10;
2519 info->port.closing_wait = 30 * HZ;
Jiri Slaby1c456072008-02-07 00:16:46 -08002520 info->normal_termios = mxvar_sdriver->init_termios;
Jiri Slaby1c456072008-02-07 00:16:46 -08002521 init_waitqueue_head(&info->delta_msr_wait);
2522 memset(&info->mon_data, 0, sizeof(struct mxser_mon));
2523 info->err_shadow = 0;
2524 spin_lock_init(&info->slock);
2525
2526 /* before set INT ISR, disable all int */
2527 outb(inb(info->ioaddr + UART_IER) & 0xf0,
2528 info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 }
2530
Jiri Slaby1c456072008-02-07 00:16:46 -08002531 retval = request_irq(brd->irq, mxser_interrupt, IRQF_SHARED, "mxser",
2532 brd);
2533 if (retval) {
2534 printk(KERN_ERR "Board %s: Request irq failed, IRQ (%d) may "
2535 "conflict with another device.\n",
2536 brd->info->name, brd->irq);
2537 /* We hold resources, we need to release them. */
2538 mxser_release_res(brd, pdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002540 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541}
2542
Jiri Slaby1c456072008-02-07 00:16:46 -08002543static int __init mxser_get_ISA_conf(int cap, struct mxser_board *brd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544{
2545 int id, i, bits;
2546 unsigned short regs[16], irq;
2547 unsigned char scratch, scratch2;
2548
Jiri Slaby1c456072008-02-07 00:16:46 -08002549 brd->chip_flag = MOXA_OTHER_UART;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550
2551 id = mxser_read_register(cap, regs);
Jiri Slaby1c456072008-02-07 00:16:46 -08002552 switch (id) {
2553 case C168_ASIC_ID:
2554 brd->info = &mxser_cards[0];
2555 break;
2556 case C104_ASIC_ID:
2557 brd->info = &mxser_cards[1];
2558 break;
2559 case CI104J_ASIC_ID:
2560 brd->info = &mxser_cards[2];
2561 break;
2562 case C102_ASIC_ID:
2563 brd->info = &mxser_cards[5];
2564 break;
2565 case CI132_ASIC_ID:
2566 brd->info = &mxser_cards[6];
2567 break;
2568 case CI134_ASIC_ID:
2569 brd->info = &mxser_cards[7];
2570 break;
2571 default:
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002572 return 0;
Jiri Slaby1c456072008-02-07 00:16:46 -08002573 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574
2575 irq = 0;
Jiri Slaby1c456072008-02-07 00:16:46 -08002576 /* some ISA cards have 2 ports, but we want to see them as 4-port (why?)
2577 Flag-hack checks if configuration should be read as 2-port here. */
2578 if (brd->info->nports == 2 || (brd->info->flags & MXSER_HAS2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 irq = regs[9] & 0xF000;
2580 irq = irq | (irq >> 4);
2581 if (irq != (regs[9] & 0xFF00))
Jiri Slaby83766bc2008-07-25 01:48:21 -07002582 goto err_irqconflict;
Jiri Slaby1c456072008-02-07 00:16:46 -08002583 } else if (brd->info->nports == 4) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 irq = regs[9] & 0xF000;
2585 irq = irq | (irq >> 4);
2586 irq = irq | (irq >> 8);
2587 if (irq != regs[9])
Jiri Slaby83766bc2008-07-25 01:48:21 -07002588 goto err_irqconflict;
Jiri Slaby1c456072008-02-07 00:16:46 -08002589 } else if (brd->info->nports == 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 irq = regs[9] & 0xF000;
2591 irq = irq | (irq >> 4);
2592 irq = irq | (irq >> 8);
2593 if ((irq != regs[9]) || (irq != regs[10]))
Jiri Slaby83766bc2008-07-25 01:48:21 -07002594 goto err_irqconflict;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 }
2596
Jiri Slaby83766bc2008-07-25 01:48:21 -07002597 if (!irq) {
2598 printk(KERN_ERR "mxser: interrupt number unset\n");
2599 return -EIO;
2600 }
Jiri Slaby1c456072008-02-07 00:16:46 -08002601 brd->irq = ((int)(irq & 0xF000) >> 12);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 for (i = 0; i < 8; i++)
Jiri Slaby1c456072008-02-07 00:16:46 -08002603 brd->ports[i].ioaddr = (int) regs[i + 1] & 0xFFF8;
Jiri Slaby83766bc2008-07-25 01:48:21 -07002604 if ((regs[12] & 0x80) == 0) {
2605 printk(KERN_ERR "mxser: invalid interrupt vector\n");
2606 return -EIO;
2607 }
Jiri Slaby1c456072008-02-07 00:16:46 -08002608 brd->vector = (int)regs[11]; /* interrupt vector */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 if (id == 1)
Jiri Slaby1c456072008-02-07 00:16:46 -08002610 brd->vector_mask = 0x00FF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 else
Jiri Slaby1c456072008-02-07 00:16:46 -08002612 brd->vector_mask = 0x000F;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 for (i = 7, bits = 0x0100; i >= 0; i--, bits <<= 1) {
2614 if (regs[12] & bits) {
Jiri Slaby1c456072008-02-07 00:16:46 -08002615 brd->ports[i].baud_base = 921600;
2616 brd->ports[i].max_baud = 921600;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 } else {
Jiri Slaby1c456072008-02-07 00:16:46 -08002618 brd->ports[i].baud_base = 115200;
2619 brd->ports[i].max_baud = 115200;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 }
2621 }
2622 scratch2 = inb(cap + UART_LCR) & (~UART_LCR_DLAB);
2623 outb(scratch2 | UART_LCR_DLAB, cap + UART_LCR);
2624 outb(0, cap + UART_EFR); /* EFR is the same as FCR */
2625 outb(scratch2, cap + UART_LCR);
2626 outb(UART_FCR_ENABLE_FIFO, cap + UART_FCR);
2627 scratch = inb(cap + UART_IIR);
2628
2629 if (scratch & 0xC0)
Jiri Slaby1c456072008-02-07 00:16:46 -08002630 brd->uart_type = PORT_16550A;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 else
Jiri Slaby1c456072008-02-07 00:16:46 -08002632 brd->uart_type = PORT_16450;
2633 if (!request_region(brd->ports[0].ioaddr, 8 * brd->info->nports,
Jiri Slaby83766bc2008-07-25 01:48:21 -07002634 "mxser(IO)")) {
2635 printk(KERN_ERR "mxser: can't request ports I/O region: "
2636 "0x%.8lx-0x%.8lx\n",
2637 brd->ports[0].ioaddr, brd->ports[0].ioaddr +
2638 8 * brd->info->nports - 1);
2639 return -EIO;
2640 }
Jiri Slaby1c456072008-02-07 00:16:46 -08002641 if (!request_region(brd->vector, 1, "mxser(vector)")) {
2642 release_region(brd->ports[0].ioaddr, 8 * brd->info->nports);
Jiri Slaby83766bc2008-07-25 01:48:21 -07002643 printk(KERN_ERR "mxser: can't request interrupt vector region: "
2644 "0x%.8lx-0x%.8lx\n",
2645 brd->ports[0].ioaddr, brd->ports[0].ioaddr +
2646 8 * brd->info->nports - 1);
2647 return -EIO;
Jiri Slaby1c456072008-02-07 00:16:46 -08002648 }
2649 return brd->info->nports;
Jiri Slaby83766bc2008-07-25 01:48:21 -07002650
2651err_irqconflict:
2652 printk(KERN_ERR "mxser: invalid interrupt number\n");
2653 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654}
2655
Jiri Slaby1c456072008-02-07 00:16:46 -08002656static int __devinit mxser_probe(struct pci_dev *pdev,
2657 const struct pci_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658{
Jiri Slaby1c456072008-02-07 00:16:46 -08002659#ifdef CONFIG_PCI
2660 struct mxser_board *brd;
2661 unsigned int i, j;
2662 unsigned long ioaddress;
2663 int retval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664
Jiri Slaby1c456072008-02-07 00:16:46 -08002665 for (i = 0; i < MXSER_BOARDS; i++)
2666 if (mxser_boards[i].info == NULL)
2667 break;
2668
2669 if (i >= MXSER_BOARDS) {
Jiri Slaby83766bc2008-07-25 01:48:21 -07002670 dev_err(&pdev->dev, "too many boards found (maximum %d), board "
2671 "not configured\n", MXSER_BOARDS);
Jiri Slaby1c456072008-02-07 00:16:46 -08002672 goto err;
2673 }
2674
2675 brd = &mxser_boards[i];
2676 brd->idx = i * MXSER_PORTS_PER_BOARD;
Jiri Slaby83766bc2008-07-25 01:48:21 -07002677 dev_info(&pdev->dev, "found MOXA %s board (BusNo=%d, DevNo=%d)\n",
Jiri Slaby1c456072008-02-07 00:16:46 -08002678 mxser_cards[ent->driver_data].name,
2679 pdev->bus->number, PCI_SLOT(pdev->devfn));
2680
2681 retval = pci_enable_device(pdev);
2682 if (retval) {
Jiri Slaby83766bc2008-07-25 01:48:21 -07002683 dev_err(&pdev->dev, "PCI enable failed\n");
Jiri Slaby1c456072008-02-07 00:16:46 -08002684 goto err;
2685 }
2686
2687 /* io address */
2688 ioaddress = pci_resource_start(pdev, 2);
2689 retval = pci_request_region(pdev, 2, "mxser(IO)");
2690 if (retval)
2691 goto err;
2692
2693 brd->info = &mxser_cards[ent->driver_data];
2694 for (i = 0; i < brd->info->nports; i++)
2695 brd->ports[i].ioaddr = ioaddress + 8 * i;
2696
2697 /* vector */
2698 ioaddress = pci_resource_start(pdev, 3);
2699 retval = pci_request_region(pdev, 3, "mxser(vector)");
2700 if (retval)
2701 goto err_relio;
2702 brd->vector = ioaddress;
2703
2704 /* irq */
2705 brd->irq = pdev->irq;
2706
2707 brd->chip_flag = CheckIsMoxaMust(brd->ports[0].ioaddr);
2708 brd->uart_type = PORT_16550A;
2709 brd->vector_mask = 0;
2710
2711 for (i = 0; i < brd->info->nports; i++) {
2712 for (j = 0; j < UART_INFO_NUM; j++) {
2713 if (Gpci_uart_info[j].type == brd->chip_flag) {
2714 brd->ports[i].max_baud =
2715 Gpci_uart_info[j].max_baud;
2716
2717 /* exception....CP-102 */
2718 if (brd->info->flags & MXSER_HIGHBAUD)
2719 brd->ports[i].max_baud = 921600;
2720 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 }
2722 }
Jiri Slaby1c456072008-02-07 00:16:46 -08002723 }
2724
2725 if (brd->chip_flag == MOXA_MUST_MU860_HWID) {
2726 for (i = 0; i < brd->info->nports; i++) {
2727 if (i < 4)
2728 brd->ports[i].opmode_ioaddr = ioaddress + 4;
2729 else
2730 brd->ports[i].opmode_ioaddr = ioaddress + 0x0c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 }
Jiri Slaby1c456072008-02-07 00:16:46 -08002732 outb(0, ioaddress + 4); /* default set to RS232 mode */
2733 outb(0, ioaddress + 0x0c); /* default set to RS232 mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 }
Jiri Slaby1c456072008-02-07 00:16:46 -08002735
2736 for (i = 0; i < brd->info->nports; i++) {
2737 brd->vector_mask |= (1 << i);
2738 brd->ports[i].baud_base = 921600;
2739 }
2740
2741 /* mxser_initbrd will hook ISR. */
2742 retval = mxser_initbrd(brd, pdev);
2743 if (retval)
2744 goto err_null;
2745
2746 for (i = 0; i < brd->info->nports; i++)
2747 tty_register_device(mxvar_sdriver, brd->idx + i, &pdev->dev);
2748
2749 pci_set_drvdata(pdev, brd);
2750
2751 return 0;
2752err_relio:
2753 pci_release_region(pdev, 2);
2754err_null:
2755 brd->info = NULL;
2756err:
2757 return retval;
2758#else
2759 return -ENODEV;
2760#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761}
2762
Jiri Slaby1c456072008-02-07 00:16:46 -08002763static void __devexit mxser_remove(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764{
Jiri Slaby1c456072008-02-07 00:16:46 -08002765 struct mxser_board *brd = pci_get_drvdata(pdev);
2766 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767
Jiri Slaby1c456072008-02-07 00:16:46 -08002768 for (i = 0; i < brd->info->nports; i++)
2769 tty_unregister_device(mxvar_sdriver, brd->idx + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770
Jiri Slaby1c456072008-02-07 00:16:46 -08002771 mxser_release_res(brd, pdev, 1);
2772 brd->info = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773}
2774
Jiri Slaby1c456072008-02-07 00:16:46 -08002775static struct pci_driver mxser_driver = {
2776 .name = "mxser",
2777 .id_table = mxser_pcibrds,
2778 .probe = mxser_probe,
2779 .remove = __devexit_p(mxser_remove)
2780};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781
Jiri Slaby1c456072008-02-07 00:16:46 -08002782static int __init mxser_module_init(void)
2783{
2784 struct mxser_board *brd;
Jiri Slaby1df00922008-07-25 01:48:22 -07002785 unsigned int b, i, m;
2786 int retval;
Jiri Slaby1c456072008-02-07 00:16:46 -08002787
Jiri Slaby1c456072008-02-07 00:16:46 -08002788 mxvar_sdriver = alloc_tty_driver(MXSER_PORTS + 1);
2789 if (!mxvar_sdriver)
2790 return -ENOMEM;
2791
2792 printk(KERN_INFO "MOXA Smartio/Industio family driver version %s\n",
2793 MXSER_VERSION);
2794
2795 /* Initialize the tty_driver structure */
2796 mxvar_sdriver->owner = THIS_MODULE;
2797 mxvar_sdriver->magic = TTY_DRIVER_MAGIC;
2798 mxvar_sdriver->name = "ttyMI";
2799 mxvar_sdriver->major = ttymajor;
2800 mxvar_sdriver->minor_start = 0;
2801 mxvar_sdriver->num = MXSER_PORTS + 1;
2802 mxvar_sdriver->type = TTY_DRIVER_TYPE_SERIAL;
2803 mxvar_sdriver->subtype = SERIAL_TYPE_NORMAL;
2804 mxvar_sdriver->init_termios = tty_std_termios;
2805 mxvar_sdriver->init_termios.c_cflag = B9600|CS8|CREAD|HUPCL|CLOCAL;
2806 mxvar_sdriver->flags = TTY_DRIVER_REAL_RAW|TTY_DRIVER_DYNAMIC_DEV;
2807 tty_set_operations(mxvar_sdriver, &mxser_ops);
2808
2809 retval = tty_register_driver(mxvar_sdriver);
2810 if (retval) {
2811 printk(KERN_ERR "Couldn't install MOXA Smartio/Industio family "
2812 "tty driver !\n");
2813 goto err_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 }
Jiri Slaby1c456072008-02-07 00:16:46 -08002815
Jiri Slaby1c456072008-02-07 00:16:46 -08002816 /* Start finding ISA boards here */
Jiri Slaby1df00922008-07-25 01:48:22 -07002817 for (m = 0, b = 0; b < MXSER_BOARDS; b++) {
2818 if (!ioaddr[b])
2819 continue;
Jiri Slaby1c456072008-02-07 00:16:46 -08002820
Jiri Slaby1df00922008-07-25 01:48:22 -07002821 brd = &mxser_boards[m];
2822 retval = mxser_get_ISA_conf(!ioaddr[b], brd);
2823 if (retval <= 0) {
2824 brd->info = NULL;
2825 continue;
Jiri Slaby1c456072008-02-07 00:16:46 -08002826 }
2827
Jiri Slaby1df00922008-07-25 01:48:22 -07002828 printk(KERN_INFO "mxser: found MOXA %s board (CAP=0x%lx)\n",
2829 brd->info->name, ioaddr[b]);
2830
2831 /* mxser_initbrd will hook ISR. */
2832 if (mxser_initbrd(brd, NULL) < 0) {
2833 brd->info = NULL;
2834 continue;
2835 }
2836
2837 brd->idx = m * MXSER_PORTS_PER_BOARD;
2838 for (i = 0; i < brd->info->nports; i++)
2839 tty_register_device(mxvar_sdriver, brd->idx + i, NULL);
2840
2841 m++;
2842 }
2843
Jiri Slaby1c456072008-02-07 00:16:46 -08002844 retval = pci_register_driver(&mxser_driver);
2845 if (retval) {
Jiri Slaby83766bc2008-07-25 01:48:21 -07002846 printk(KERN_ERR "mxser: can't register pci driver\n");
Jiri Slaby1c456072008-02-07 00:16:46 -08002847 if (!m) {
2848 retval = -ENODEV;
2849 goto err_unr;
2850 } /* else: we have some ISA cards under control */
2851 }
2852
Jiri Slaby1c456072008-02-07 00:16:46 -08002853 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
Jiri Slaby1c456072008-02-07 00:16:46 -08002865 pci_unregister_driver(&mxser_driver);
2866
2867 for (i = 0; i < MXSER_BOARDS; i++) /* ISA remains */
2868 if (mxser_boards[i].info != NULL)
2869 for (j = 0; j < mxser_boards[i].info->nports; j++)
2870 tty_unregister_device(mxvar_sdriver,
2871 mxser_boards[i].idx + j);
2872 tty_unregister_driver(mxvar_sdriver);
2873 put_tty_driver(mxvar_sdriver);
2874
2875 for (i = 0; i < MXSER_BOARDS; i++)
2876 if (mxser_boards[i].info != NULL)
2877 mxser_release_res(&mxser_boards[i], NULL, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878}
2879
2880module_init(mxser_module_init);
2881module_exit(mxser_module_exit);