blob: e30575e87648d4efa42b18e444732820281ff71e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * mxser.c -- MOXA Smartio/Industio family multiport serial driver.
3 *
Jiri Slaby80ff8a82008-02-07 00:16:51 -08004 * Copyright (C) 1999-2006 Moxa Technologies (support@moxa.com).
5 * Copyright (C) 2006-2008 Jiri Slaby <jirislaby@gmail.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
Jiri Slaby1c456072008-02-07 00:16:46 -08007 * This code is loosely based on the 1.8 moxa driver which is based on
8 * Linux serial driver, written by Linus Torvalds, Theodore T'so and
9 * others.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -070014 * (at your option) any later version.
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070016 * Fed through a cleanup, indent and remove of non 2.6 code by Alan Cox
17 * <alan@redhat.com>. The original 1.8 code is available on www.moxa.com.
18 * - Fixed x86_64 cleanness
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 */
20
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/errno.h>
23#include <linux/signal.h>
24#include <linux/sched.h>
25#include <linux/timer.h>
26#include <linux/interrupt.h>
27#include <linux/tty.h>
28#include <linux/tty_flip.h>
29#include <linux/serial.h>
30#include <linux/serial_reg.h>
31#include <linux/major.h>
32#include <linux/string.h>
33#include <linux/fcntl.h>
34#include <linux/ptrace.h>
35#include <linux/gfp.h>
36#include <linux/ioport.h>
37#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/delay.h>
39#include <linux/pci.h>
Jiri Slaby1977f032007-10-18 23:40:25 -070040#include <linux/bitops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42#include <asm/system.h>
43#include <asm/io.h>
44#include <asm/irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <asm/uaccess.h>
46
47#include "mxser.h"
48
Jiri Slabye129def2008-07-22 11:20:34 +010049#define MXSER_VERSION "2.0.4" /* 1.12 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#define MXSERMAJOR 174
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#define MXSER_BOARDS 4 /* Max. boards */
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#define MXSER_PORTS_PER_BOARD 8 /* Max. ports per board */
Jiri Slaby1c456072008-02-07 00:16:46 -080054#define MXSER_PORTS (MXSER_BOARDS * MXSER_PORTS_PER_BOARD)
55#define MXSER_ISR_PASS_LIMIT 100
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Jiri Slaby1c456072008-02-07 00:16:46 -080057/*CheckIsMoxaMust return value*/
58#define MOXA_OTHER_UART 0x00
59#define MOXA_MUST_MU150_HWID 0x01
60#define MOXA_MUST_MU860_HWID 0x02
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#define WAKEUP_CHARS 256
63
64#define UART_MCR_AFE 0x20
65#define UART_LSR_SPECIAL 0x1E
66
Jiri Slabye129def2008-07-22 11:20:34 +010067#define PCI_DEVICE_ID_POS104UL 0x1044
Jiri Slaby1c456072008-02-07 00:16:46 -080068#define PCI_DEVICE_ID_CB108 0x1080
Jiri Slabye129def2008-07-22 11:20:34 +010069#define PCI_DEVICE_ID_CP102UF 0x1023
Jiri Slaby1c456072008-02-07 00:16:46 -080070#define PCI_DEVICE_ID_CB114 0x1142
Jiri Slaby80ff8a82008-02-07 00:16:51 -080071#define PCI_DEVICE_ID_CP114UL 0x1143
Jiri Slaby1c456072008-02-07 00:16:46 -080072#define PCI_DEVICE_ID_CB134I 0x1341
73#define PCI_DEVICE_ID_CP138U 0x1380
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76#define C168_ASIC_ID 1
77#define C104_ASIC_ID 2
78#define C102_ASIC_ID 0xB
79#define CI132_ASIC_ID 4
80#define CI134_ASIC_ID 3
81#define CI104J_ASIC_ID 5
82
Jiri Slaby1c456072008-02-07 00:16:46 -080083#define MXSER_HIGHBAUD 1
84#define MXSER_HAS2 2
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -070086/* This is only for PCI */
Jiri Slaby1c456072008-02-07 00:16:46 -080087static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 int type;
89 int tx_fifo;
90 int rx_fifo;
91 int xmit_fifo_size;
92 int rx_high_water;
93 int rx_trigger;
94 int rx_low_water;
95 long max_baud;
Jiri Slaby1c456072008-02-07 00:16:46 -080096} Gpci_uart_info[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 {MOXA_OTHER_UART, 16, 16, 16, 14, 14, 1, 921600L},
98 {MOXA_MUST_MU150_HWID, 64, 64, 64, 48, 48, 16, 230400L},
99 {MOXA_MUST_MU860_HWID, 128, 128, 128, 96, 96, 32, 921600L}
100};
Jiri Slaby1c456072008-02-07 00:16:46 -0800101#define UART_INFO_NUM ARRAY_SIZE(Gpci_uart_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Jiri Slaby1c456072008-02-07 00:16:46 -0800103struct mxser_cardinfo {
104 char *name;
105 unsigned int nports;
106 unsigned int flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107};
108
Jiri Slaby1c456072008-02-07 00:16:46 -0800109static const struct mxser_cardinfo mxser_cards[] = {
110/* 0*/ { "C168 series", 8, },
111 { "C104 series", 4, },
112 { "CI-104J series", 4, },
113 { "C168H/PCI series", 8, },
114 { "C104H/PCI series", 4, },
115/* 5*/ { "C102 series", 4, MXSER_HAS2 }, /* C102-ISA */
116 { "CI-132 series", 4, MXSER_HAS2 },
117 { "CI-134 series", 4, },
118 { "CP-132 series", 2, },
119 { "CP-114 series", 4, },
120/*10*/ { "CT-114 series", 4, },
121 { "CP-102 series", 2, MXSER_HIGHBAUD },
122 { "CP-104U series", 4, },
123 { "CP-168U series", 8, },
124 { "CP-132U series", 2, },
125/*15*/ { "CP-134U series", 4, },
126 { "CP-104JU series", 4, },
127 { "Moxa UC7000 Serial", 8, }, /* RC7000 */
128 { "CP-118U series", 8, },
129 { "CP-102UL series", 2, },
130/*20*/ { "CP-102U series", 2, },
131 { "CP-118EL series", 8, },
132 { "CP-168EL series", 8, },
133 { "CP-104EL series", 4, },
134 { "CB-108 series", 8, },
135/*25*/ { "CB-114 series", 4, },
136 { "CB-134I series", 4, },
137 { "CP-138U series", 8, },
Jiri Slaby80ff8a82008-02-07 00:16:51 -0800138 { "POS-104UL series", 4, },
Jiri Slabye129def2008-07-22 11:20:34 +0100139 { "CP-114UL series", 4, },
140/*30*/ { "CP-102UF series", 2, }
Jiri Slaby1c456072008-02-07 00:16:46 -0800141};
142
143/* driver_data correspond to the lines in the structure above
144 see also ISA probe function before you change something */
145static struct pci_device_id mxser_pcibrds[] = {
146 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_C168), .driver_data = 3 },
147 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_C104), .driver_data = 4 },
148 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP132), .driver_data = 8 },
149 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP114), .driver_data = 9 },
150 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CT114), .driver_data = 10 },
151 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102), .driver_data = 11 },
152 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104U), .driver_data = 12 },
153 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP168U), .driver_data = 13 },
154 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP132U), .driver_data = 14 },
155 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP134U), .driver_data = 15 },
156 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104JU),.driver_data = 16 },
157 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_RC7000), .driver_data = 17 },
158 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP118U), .driver_data = 18 },
159 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102UL),.driver_data = 19 },
160 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP102U), .driver_data = 20 },
161 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP118EL),.driver_data = 21 },
162 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP168EL),.driver_data = 22 },
163 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_MOXA_CP104EL),.driver_data = 23 },
164 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB108), .driver_data = 24 },
165 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB114), .driver_data = 25 },
166 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CB134I), .driver_data = 26 },
167 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP138U), .driver_data = 27 },
168 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_POS104UL), .driver_data = 28 },
Jiri Slaby80ff8a82008-02-07 00:16:51 -0800169 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP114UL), .driver_data = 29 },
Jiri Slabye129def2008-07-22 11:20:34 +0100170 { PCI_VDEVICE(MOXA, PCI_DEVICE_ID_CP102UF), .driver_data = 30 },
Jiri Slaby1c456072008-02-07 00:16:46 -0800171 { }
172};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173MODULE_DEVICE_TABLE(pci, mxser_pcibrds);
174
Jiri Slaby1df00922008-07-25 01:48:22 -0700175static unsigned long ioaddr[MXSER_BOARDS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176static int ttymajor = MXSERMAJOR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
178/* Variables for insmod */
179
180MODULE_AUTHOR("Casper Yang");
181MODULE_DESCRIPTION("MOXA Smartio/Industio Family Multiport Board Device Driver");
Jiri Slaby1df00922008-07-25 01:48:22 -0700182module_param_array(ioaddr, ulong, NULL, 0);
183MODULE_PARM_DESC(ioaddr, "ISA io addresses to look for a moxa board");
Rusty Russell8d3b33f2006-03-25 03:07:05 -0800184module_param(ttymajor, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185MODULE_LICENSE("GPL");
186
187struct mxser_log {
188 int tick;
189 unsigned long rxcnt[MXSER_PORTS];
190 unsigned long txcnt[MXSER_PORTS];
191};
192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193struct mxser_mon {
194 unsigned long rxcnt;
195 unsigned long txcnt;
196 unsigned long up_rxcnt;
197 unsigned long up_txcnt;
198 int modem_status;
199 unsigned char hold_reason;
200};
201
202struct mxser_mon_ext {
203 unsigned long rx_cnt[32];
204 unsigned long tx_cnt[32];
205 unsigned long up_rxcnt[32];
206 unsigned long up_txcnt[32];
207 int modem_status[32];
208
209 long baudrate[32];
210 int databits[32];
211 int stopbits[32];
212 int parity[32];
213 int flowctrl[32];
214 int fifo[32];
215 int iftype[32];
216};
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700217
Jiri Slaby1c456072008-02-07 00:16:46 -0800218struct mxser_board;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Jiri Slaby1c456072008-02-07 00:16:46 -0800220struct mxser_port {
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100221 struct tty_port port;
Jiri Slaby1c456072008-02-07 00:16:46 -0800222 struct mxser_board *board;
Jiri Slaby1c456072008-02-07 00:16:46 -0800223
224 unsigned long ioaddr;
225 unsigned long opmode_ioaddr;
226 int max_baud;
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 int rx_high_water;
229 int rx_trigger; /* Rx fifo trigger level */
230 int rx_low_water;
231 int baud_base; /* max. speed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 int type; /* UART type */
Jiri Slaby1c456072008-02-07 00:16:46 -0800233
234 int x_char; /* xon/xoff character */
235 int IER; /* Interrupt Enable Register */
236 int MCR; /* Modem control register */
237
238 unsigned char stop_rx;
239 unsigned char ldisc_stop_rx;
240
241 int custom_divisor;
Jiri Slaby1c456072008-02-07 00:16:46 -0800242 unsigned char err_shadow;
Jiri Slaby1c456072008-02-07 00:16:46 -0800243
Jiri Slaby1c456072008-02-07 00:16:46 -0800244 struct async_icount icount; /* kernel counters for 4 input interrupts */
245 int timeout;
246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 int read_status_mask;
248 int ignore_status_mask;
249 int xmit_fifo_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 int xmit_head;
251 int xmit_tail;
252 int xmit_cnt;
Jiri Slaby1c456072008-02-07 00:16:46 -0800253
Alan Cox606d0992006-12-08 02:38:45 -0800254 struct ktermios normal_termios;
Jiri Slaby1c456072008-02-07 00:16:46 -0800255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 struct mxser_mon mon_data;
Jiri Slaby1c456072008-02-07 00:16:46 -0800257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 spinlock_t slock;
Jiri Slaby1c456072008-02-07 00:16:46 -0800259 wait_queue_head_t delta_msr_wait;
260};
261
262struct mxser_board {
263 unsigned int idx;
264 int irq;
265 const struct mxser_cardinfo *info;
266 unsigned long vector;
267 unsigned long vector_mask;
268
269 int chip_flag;
270 int uart_type;
271
272 struct mxser_port ports[MXSER_PORTS_PER_BOARD];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273};
274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275struct mxser_mstatus {
276 tcflag_t cflag;
277 int cts;
278 int dsr;
279 int ri;
280 int dcd;
281};
282
Jiri Slaby1c456072008-02-07 00:16:46 -0800283static struct mxser_board mxser_boards[MXSER_BOARDS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284static struct tty_driver *mxvar_sdriver;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285static struct mxser_log mxvar_log;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286static int mxser_set_baud_method[MXSER_PORTS + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Christoph Hellwig148ff862008-04-30 00:54:29 -0700288static void mxser_enable_must_enchance_mode(unsigned long baseio)
289{
290 u8 oldlcr;
291 u8 efr;
292
293 oldlcr = inb(baseio + UART_LCR);
294 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
295
296 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
297 efr |= MOXA_MUST_EFR_EFRB_ENABLE;
298
299 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
300 outb(oldlcr, baseio + UART_LCR);
301}
302
303static void mxser_disable_must_enchance_mode(unsigned long baseio)
304{
305 u8 oldlcr;
306 u8 efr;
307
308 oldlcr = inb(baseio + UART_LCR);
309 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
310
311 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
312 efr &= ~MOXA_MUST_EFR_EFRB_ENABLE;
313
314 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
315 outb(oldlcr, baseio + UART_LCR);
316}
317
318static void mxser_set_must_xon1_value(unsigned long baseio, u8 value)
319{
320 u8 oldlcr;
321 u8 efr;
322
323 oldlcr = inb(baseio + UART_LCR);
324 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
325
326 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
327 efr &= ~MOXA_MUST_EFR_BANK_MASK;
328 efr |= MOXA_MUST_EFR_BANK0;
329
330 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
331 outb(value, baseio + MOXA_MUST_XON1_REGISTER);
332 outb(oldlcr, baseio + UART_LCR);
333}
334
335static void mxser_set_must_xoff1_value(unsigned long baseio, u8 value)
336{
337 u8 oldlcr;
338 u8 efr;
339
340 oldlcr = inb(baseio + UART_LCR);
341 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
342
343 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
344 efr &= ~MOXA_MUST_EFR_BANK_MASK;
345 efr |= MOXA_MUST_EFR_BANK0;
346
347 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
348 outb(value, baseio + MOXA_MUST_XOFF1_REGISTER);
349 outb(oldlcr, baseio + UART_LCR);
350}
351
352static void mxser_set_must_fifo_value(struct mxser_port *info)
353{
354 u8 oldlcr;
355 u8 efr;
356
357 oldlcr = inb(info->ioaddr + UART_LCR);
358 outb(MOXA_MUST_ENTER_ENCHANCE, info->ioaddr + UART_LCR);
359
360 efr = inb(info->ioaddr + MOXA_MUST_EFR_REGISTER);
361 efr &= ~MOXA_MUST_EFR_BANK_MASK;
362 efr |= MOXA_MUST_EFR_BANK1;
363
364 outb(efr, info->ioaddr + MOXA_MUST_EFR_REGISTER);
365 outb((u8)info->rx_high_water, info->ioaddr + MOXA_MUST_RBRTH_REGISTER);
366 outb((u8)info->rx_trigger, info->ioaddr + MOXA_MUST_RBRTI_REGISTER);
367 outb((u8)info->rx_low_water, info->ioaddr + MOXA_MUST_RBRTL_REGISTER);
368 outb(oldlcr, info->ioaddr + UART_LCR);
369}
370
371static void mxser_set_must_enum_value(unsigned long baseio, u8 value)
372{
373 u8 oldlcr;
374 u8 efr;
375
376 oldlcr = inb(baseio + UART_LCR);
377 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
378
379 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
380 efr &= ~MOXA_MUST_EFR_BANK_MASK;
381 efr |= MOXA_MUST_EFR_BANK2;
382
383 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
384 outb(value, baseio + MOXA_MUST_ENUM_REGISTER);
385 outb(oldlcr, baseio + UART_LCR);
386}
387
388static void mxser_get_must_hardware_id(unsigned long baseio, u8 *pId)
389{
390 u8 oldlcr;
391 u8 efr;
392
393 oldlcr = inb(baseio + UART_LCR);
394 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
395
396 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
397 efr &= ~MOXA_MUST_EFR_BANK_MASK;
398 efr |= MOXA_MUST_EFR_BANK2;
399
400 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
401 *pId = inb(baseio + MOXA_MUST_HWID_REGISTER);
402 outb(oldlcr, baseio + UART_LCR);
403}
404
405static void SET_MOXA_MUST_NO_SOFTWARE_FLOW_CONTROL(unsigned long baseio)
406{
407 u8 oldlcr;
408 u8 efr;
409
410 oldlcr = inb(baseio + UART_LCR);
411 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
412
413 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
414 efr &= ~MOXA_MUST_EFR_SF_MASK;
415
416 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
417 outb(oldlcr, baseio + UART_LCR);
418}
419
420static void mxser_enable_must_tx_software_flow_control(unsigned long baseio)
421{
422 u8 oldlcr;
423 u8 efr;
424
425 oldlcr = inb(baseio + UART_LCR);
426 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
427
428 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
429 efr &= ~MOXA_MUST_EFR_SF_TX_MASK;
430 efr |= MOXA_MUST_EFR_SF_TX1;
431
432 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
433 outb(oldlcr, baseio + UART_LCR);
434}
435
436static void mxser_disable_must_tx_software_flow_control(unsigned long baseio)
437{
438 u8 oldlcr;
439 u8 efr;
440
441 oldlcr = inb(baseio + UART_LCR);
442 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
443
444 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
445 efr &= ~MOXA_MUST_EFR_SF_TX_MASK;
446
447 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
448 outb(oldlcr, baseio + UART_LCR);
449}
450
451static void mxser_enable_must_rx_software_flow_control(unsigned long baseio)
452{
453 u8 oldlcr;
454 u8 efr;
455
456 oldlcr = inb(baseio + UART_LCR);
457 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
458
459 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
460 efr &= ~MOXA_MUST_EFR_SF_RX_MASK;
461 efr |= MOXA_MUST_EFR_SF_RX1;
462
463 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
464 outb(oldlcr, baseio + UART_LCR);
465}
466
467static void mxser_disable_must_rx_software_flow_control(unsigned long baseio)
468{
469 u8 oldlcr;
470 u8 efr;
471
472 oldlcr = inb(baseio + UART_LCR);
473 outb(MOXA_MUST_ENTER_ENCHANCE, baseio + UART_LCR);
474
475 efr = inb(baseio + MOXA_MUST_EFR_REGISTER);
476 efr &= ~MOXA_MUST_EFR_SF_RX_MASK;
477
478 outb(efr, baseio + MOXA_MUST_EFR_REGISTER);
479 outb(oldlcr, baseio + UART_LCR);
480}
481
Jesper Juhlb8cc5542007-10-18 03:06:03 -0700482#ifdef CONFIG_PCI
Jiri Slaby1c456072008-02-07 00:16:46 -0800483static int __devinit CheckIsMoxaMust(unsigned long io)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
485 u8 oldmcr, hwid;
486 int i;
487
488 outb(0, io + UART_LCR);
Christoph Hellwig148ff862008-04-30 00:54:29 -0700489 mxser_disable_must_enchance_mode(io);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 oldmcr = inb(io + UART_MCR);
491 outb(0, io + UART_MCR);
Christoph Hellwig148ff862008-04-30 00:54:29 -0700492 mxser_set_must_xon1_value(io, 0x11);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 if ((hwid = inb(io + UART_MCR)) != 0) {
494 outb(oldmcr, io + UART_MCR);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700495 return MOXA_OTHER_UART;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 }
497
Christoph Hellwig148ff862008-04-30 00:54:29 -0700498 mxser_get_must_hardware_id(io, &hwid);
Jiri Slaby1c456072008-02-07 00:16:46 -0800499 for (i = 1; i < UART_INFO_NUM; i++) { /* 0 = OTHER_UART */
500 if (hwid == Gpci_uart_info[i].type)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -0700501 return (int)hwid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 }
503 return MOXA_OTHER_UART;
504}
Jesper Juhlb8cc5542007-10-18 03:06:03 -0700505#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
Jiri Slaby1c456072008-02-07 00:16:46 -0800507static void process_txrx_fifo(struct mxser_port *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
509 int i;
510
511 if ((info->type == PORT_16450) || (info->type == PORT_8250)) {
512 info->rx_trigger = 1;
513 info->rx_high_water = 1;
514 info->rx_low_water = 1;
515 info->xmit_fifo_size = 1;
Jiri Slaby1c456072008-02-07 00:16:46 -0800516 } else
517 for (i = 0; i < UART_INFO_NUM; i++)
518 if (info->board->chip_flag == Gpci_uart_info[i].type) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 info->rx_trigger = Gpci_uart_info[i].rx_trigger;
520 info->rx_low_water = Gpci_uart_info[i].rx_low_water;
521 info->rx_high_water = Gpci_uart_info[i].rx_high_water;
522 info->xmit_fifo_size = Gpci_uart_info[i].xmit_fifo_size;
523 break;
524 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525}
526
Jiri Slaby1c456072008-02-07 00:16:46 -0800527static unsigned char mxser_get_msr(int baseaddr, int mode, int port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528{
Jiri Slaby72800df2008-07-25 01:48:20 -0700529 static unsigned char mxser_msr[MXSER_PORTS + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 unsigned char status = 0;
531
532 status = inb(baseaddr + UART_MSR);
533
534 mxser_msr[port] &= 0x0F;
535 mxser_msr[port] |= status;
536 status = mxser_msr[port];
537 if (mode)
538 mxser_msr[port] = 0;
539
540 return status;
541}
542
Jiri Slaby1c456072008-02-07 00:16:46 -0800543static int mxser_block_til_ready(struct tty_struct *tty, struct file *filp,
544 struct mxser_port *port)
545{
546 DECLARE_WAITQUEUE(wait, current);
547 int retval;
548 int do_clocal = 0;
549 unsigned long flags;
550
551 /*
552 * If non-blocking mode is set, or the port is not enabled,
553 * then make the check up front and then exit.
554 */
555 if ((filp->f_flags & O_NONBLOCK) ||
556 test_bit(TTY_IO_ERROR, &tty->flags)) {
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100557 port->port.flags |= ASYNC_NORMAL_ACTIVE;
Jiri Slaby1c456072008-02-07 00:16:46 -0800558 return 0;
559 }
560
561 if (tty->termios->c_cflag & CLOCAL)
562 do_clocal = 1;
563
564 /*
565 * Block waiting for the carrier detect and the line to become
566 * free (i.e., not in use by the callout). While we are in
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100567 * this loop, port->port.count is dropped by one, so that
Jiri Slaby1c456072008-02-07 00:16:46 -0800568 * mxser_close() knows when to free things. We restore it upon
569 * exit, either normal or abnormal.
570 */
571 retval = 0;
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100572 add_wait_queue(&port->port.open_wait, &wait);
Jiri Slaby1c456072008-02-07 00:16:46 -0800573
574 spin_lock_irqsave(&port->slock, flags);
575 if (!tty_hung_up_p(filp))
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100576 port->port.count--;
Jiri Slaby1c456072008-02-07 00:16:46 -0800577 spin_unlock_irqrestore(&port->slock, flags);
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100578 port->port.blocked_open++;
Jiri Slaby1c456072008-02-07 00:16:46 -0800579 while (1) {
580 spin_lock_irqsave(&port->slock, flags);
581 outb(inb(port->ioaddr + UART_MCR) |
582 UART_MCR_DTR | UART_MCR_RTS, port->ioaddr + UART_MCR);
583 spin_unlock_irqrestore(&port->slock, flags);
584 set_current_state(TASK_INTERRUPTIBLE);
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100585 if (tty_hung_up_p(filp) || !(port->port.flags & ASYNC_INITIALIZED)) {
586 if (port->port.flags & ASYNC_HUP_NOTIFY)
Jiri Slaby1c456072008-02-07 00:16:46 -0800587 retval = -EAGAIN;
588 else
589 retval = -ERESTARTSYS;
590 break;
591 }
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100592 if (!(port->port.flags & ASYNC_CLOSING) &&
Jiri Slaby1c456072008-02-07 00:16:46 -0800593 (do_clocal ||
594 (inb(port->ioaddr + UART_MSR) & UART_MSR_DCD)))
595 break;
596 if (signal_pending(current)) {
597 retval = -ERESTARTSYS;
598 break;
599 }
600 schedule();
601 }
602 set_current_state(TASK_RUNNING);
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100603 remove_wait_queue(&port->port.open_wait, &wait);
Jiri Slaby1c456072008-02-07 00:16:46 -0800604 if (!tty_hung_up_p(filp))
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100605 port->port.count++;
606 port->port.blocked_open--;
Jiri Slaby1c456072008-02-07 00:16:46 -0800607 if (retval)
608 return retval;
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100609 port->port.flags |= ASYNC_NORMAL_ACTIVE;
Jiri Slaby1c456072008-02-07 00:16:46 -0800610 return 0;
611}
612
613static int mxser_set_baud(struct mxser_port *info, long newspd)
614{
615 int quot = 0, baud;
616 unsigned char cval;
617
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100618 if (!info->port.tty || !info->port.tty->termios)
Jiri Slaby1c456072008-02-07 00:16:46 -0800619 return -1;
620
621 if (!(info->ioaddr))
622 return -1;
623
624 if (newspd > info->max_baud)
625 return -1;
626
627 if (newspd == 134) {
628 quot = 2 * info->baud_base / 269;
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100629 tty_encode_baud_rate(info->port.tty, 134, 134);
Jiri Slaby1c456072008-02-07 00:16:46 -0800630 } else if (newspd) {
631 quot = info->baud_base / newspd;
632 if (quot == 0)
633 quot = 1;
634 baud = info->baud_base/quot;
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100635 tty_encode_baud_rate(info->port.tty, baud, baud);
Jiri Slaby1c456072008-02-07 00:16:46 -0800636 } else {
637 quot = 0;
638 }
639
640 info->timeout = ((info->xmit_fifo_size * HZ * 10 * quot) / info->baud_base);
641 info->timeout += HZ / 50; /* Add .02 seconds of slop */
642
643 if (quot) {
644 info->MCR |= UART_MCR_DTR;
645 outb(info->MCR, info->ioaddr + UART_MCR);
646 } else {
647 info->MCR &= ~UART_MCR_DTR;
648 outb(info->MCR, info->ioaddr + UART_MCR);
649 return 0;
650 }
651
652 cval = inb(info->ioaddr + UART_LCR);
653
654 outb(cval | UART_LCR_DLAB, info->ioaddr + UART_LCR); /* set DLAB */
655
656 outb(quot & 0xff, info->ioaddr + UART_DLL); /* LS of divisor */
657 outb(quot >> 8, info->ioaddr + UART_DLM); /* MS of divisor */
658 outb(cval, info->ioaddr + UART_LCR); /* reset DLAB */
659
660#ifdef BOTHER
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100661 if (C_BAUD(info->port.tty) == BOTHER) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800662 quot = info->baud_base % newspd;
663 quot *= 8;
664 if (quot % newspd > newspd / 2) {
665 quot /= newspd;
666 quot++;
667 } else
668 quot /= newspd;
669
Christoph Hellwig148ff862008-04-30 00:54:29 -0700670 mxser_set_must_enum_value(info->ioaddr, quot);
Jiri Slaby1c456072008-02-07 00:16:46 -0800671 } else
672#endif
Christoph Hellwig148ff862008-04-30 00:54:29 -0700673 mxser_set_must_enum_value(info->ioaddr, 0);
Jiri Slaby1c456072008-02-07 00:16:46 -0800674
675 return 0;
676}
677
678/*
679 * This routine is called to set the UART divisor registers to match
680 * the specified baud rate for a serial port.
681 */
682static int mxser_change_speed(struct mxser_port *info,
683 struct ktermios *old_termios)
684{
685 unsigned cflag, cval, fcr;
686 int ret = 0;
687 unsigned char status;
688
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100689 if (!info->port.tty || !info->port.tty->termios)
Jiri Slaby1c456072008-02-07 00:16:46 -0800690 return ret;
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100691 cflag = info->port.tty->termios->c_cflag;
Jiri Slaby1c456072008-02-07 00:16:46 -0800692 if (!(info->ioaddr))
693 return ret;
694
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100695 if (mxser_set_baud_method[info->port.tty->index] == 0)
696 mxser_set_baud(info, tty_get_baud_rate(info->port.tty));
Jiri Slaby1c456072008-02-07 00:16:46 -0800697
698 /* byte size and parity */
699 switch (cflag & CSIZE) {
700 case CS5:
701 cval = 0x00;
702 break;
703 case CS6:
704 cval = 0x01;
705 break;
706 case CS7:
707 cval = 0x02;
708 break;
709 case CS8:
710 cval = 0x03;
711 break;
712 default:
713 cval = 0x00;
714 break; /* too keep GCC shut... */
715 }
716 if (cflag & CSTOPB)
717 cval |= 0x04;
718 if (cflag & PARENB)
719 cval |= UART_LCR_PARITY;
720 if (!(cflag & PARODD))
721 cval |= UART_LCR_EPAR;
722 if (cflag & CMSPAR)
723 cval |= UART_LCR_SPAR;
724
725 if ((info->type == PORT_8250) || (info->type == PORT_16450)) {
726 if (info->board->chip_flag) {
727 fcr = UART_FCR_ENABLE_FIFO;
728 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
Christoph Hellwig148ff862008-04-30 00:54:29 -0700729 mxser_set_must_fifo_value(info);
Jiri Slaby1c456072008-02-07 00:16:46 -0800730 } else
731 fcr = 0;
732 } else {
733 fcr = UART_FCR_ENABLE_FIFO;
734 if (info->board->chip_flag) {
735 fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
Christoph Hellwig148ff862008-04-30 00:54:29 -0700736 mxser_set_must_fifo_value(info);
Jiri Slaby1c456072008-02-07 00:16:46 -0800737 } else {
738 switch (info->rx_trigger) {
739 case 1:
740 fcr |= UART_FCR_TRIGGER_1;
741 break;
742 case 4:
743 fcr |= UART_FCR_TRIGGER_4;
744 break;
745 case 8:
746 fcr |= UART_FCR_TRIGGER_8;
747 break;
748 default:
749 fcr |= UART_FCR_TRIGGER_14;
750 break;
751 }
752 }
753 }
754
755 /* CTS flow control flag and modem status interrupts */
756 info->IER &= ~UART_IER_MSI;
757 info->MCR &= ~UART_MCR_AFE;
758 if (cflag & CRTSCTS) {
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100759 info->port.flags |= ASYNC_CTS_FLOW;
Jiri Slaby1c456072008-02-07 00:16:46 -0800760 info->IER |= UART_IER_MSI;
761 if ((info->type == PORT_16550A) || (info->board->chip_flag)) {
762 info->MCR |= UART_MCR_AFE;
763 } else {
764 status = inb(info->ioaddr + UART_MSR);
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100765 if (info->port.tty->hw_stopped) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800766 if (status & UART_MSR_CTS) {
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100767 info->port.tty->hw_stopped = 0;
Jiri Slaby1c456072008-02-07 00:16:46 -0800768 if (info->type != PORT_16550A &&
769 !info->board->chip_flag) {
770 outb(info->IER & ~UART_IER_THRI,
771 info->ioaddr +
772 UART_IER);
773 info->IER |= UART_IER_THRI;
774 outb(info->IER, info->ioaddr +
775 UART_IER);
776 }
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100777 tty_wakeup(info->port.tty);
Jiri Slaby1c456072008-02-07 00:16:46 -0800778 }
779 } else {
780 if (!(status & UART_MSR_CTS)) {
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100781 info->port.tty->hw_stopped = 1;
Jiri Slaby1c456072008-02-07 00:16:46 -0800782 if ((info->type != PORT_16550A) &&
783 (!info->board->chip_flag)) {
784 info->IER &= ~UART_IER_THRI;
785 outb(info->IER, info->ioaddr +
786 UART_IER);
787 }
788 }
789 }
790 }
791 } else {
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100792 info->port.flags &= ~ASYNC_CTS_FLOW;
Jiri Slaby1c456072008-02-07 00:16:46 -0800793 }
794 outb(info->MCR, info->ioaddr + UART_MCR);
795 if (cflag & CLOCAL) {
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100796 info->port.flags &= ~ASYNC_CHECK_CD;
Jiri Slaby1c456072008-02-07 00:16:46 -0800797 } else {
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100798 info->port.flags |= ASYNC_CHECK_CD;
Jiri Slaby1c456072008-02-07 00:16:46 -0800799 info->IER |= UART_IER_MSI;
800 }
801 outb(info->IER, info->ioaddr + UART_IER);
802
803 /*
804 * Set up parity check flag
805 */
806 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100807 if (I_INPCK(info->port.tty))
Jiri Slaby1c456072008-02-07 00:16:46 -0800808 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100809 if (I_BRKINT(info->port.tty) || I_PARMRK(info->port.tty))
Jiri Slaby1c456072008-02-07 00:16:46 -0800810 info->read_status_mask |= UART_LSR_BI;
811
812 info->ignore_status_mask = 0;
813
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100814 if (I_IGNBRK(info->port.tty)) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800815 info->ignore_status_mask |= UART_LSR_BI;
816 info->read_status_mask |= UART_LSR_BI;
817 /*
818 * If we're ignore parity and break indicators, ignore
819 * overruns too. (For real raw support).
820 */
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100821 if (I_IGNPAR(info->port.tty)) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800822 info->ignore_status_mask |=
823 UART_LSR_OE |
824 UART_LSR_PE |
825 UART_LSR_FE;
826 info->read_status_mask |=
827 UART_LSR_OE |
828 UART_LSR_PE |
829 UART_LSR_FE;
830 }
831 }
832 if (info->board->chip_flag) {
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100833 mxser_set_must_xon1_value(info->ioaddr, START_CHAR(info->port.tty));
834 mxser_set_must_xoff1_value(info->ioaddr, STOP_CHAR(info->port.tty));
835 if (I_IXON(info->port.tty)) {
Christoph Hellwig148ff862008-04-30 00:54:29 -0700836 mxser_enable_must_rx_software_flow_control(
837 info->ioaddr);
Jiri Slaby1c456072008-02-07 00:16:46 -0800838 } else {
Christoph Hellwig148ff862008-04-30 00:54:29 -0700839 mxser_disable_must_rx_software_flow_control(
840 info->ioaddr);
Jiri Slaby1c456072008-02-07 00:16:46 -0800841 }
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100842 if (I_IXOFF(info->port.tty)) {
Christoph Hellwig148ff862008-04-30 00:54:29 -0700843 mxser_enable_must_tx_software_flow_control(
844 info->ioaddr);
Jiri Slaby1c456072008-02-07 00:16:46 -0800845 } else {
Christoph Hellwig148ff862008-04-30 00:54:29 -0700846 mxser_disable_must_tx_software_flow_control(
847 info->ioaddr);
Jiri Slaby1c456072008-02-07 00:16:46 -0800848 }
849 }
850
851
852 outb(fcr, info->ioaddr + UART_FCR); /* set fcr */
853 outb(cval, info->ioaddr + UART_LCR);
854
855 return ret;
856}
857
858static void mxser_check_modem_status(struct mxser_port *port, int status)
859{
860 /* update input line counters */
861 if (status & UART_MSR_TERI)
862 port->icount.rng++;
863 if (status & UART_MSR_DDSR)
864 port->icount.dsr++;
865 if (status & UART_MSR_DDCD)
866 port->icount.dcd++;
867 if (status & UART_MSR_DCTS)
868 port->icount.cts++;
869 port->mon_data.modem_status = status;
870 wake_up_interruptible(&port->delta_msr_wait);
871
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100872 if ((port->port.flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800873 if (status & UART_MSR_DCD)
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100874 wake_up_interruptible(&port->port.open_wait);
Jiri Slaby1c456072008-02-07 00:16:46 -0800875 }
876
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100877 if (port->port.flags & ASYNC_CTS_FLOW) {
878 if (port->port.tty->hw_stopped) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800879 if (status & UART_MSR_CTS) {
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100880 port->port.tty->hw_stopped = 0;
Jiri Slaby1c456072008-02-07 00:16:46 -0800881
882 if ((port->type != PORT_16550A) &&
883 (!port->board->chip_flag)) {
884 outb(port->IER & ~UART_IER_THRI,
885 port->ioaddr + UART_IER);
886 port->IER |= UART_IER_THRI;
887 outb(port->IER, port->ioaddr +
888 UART_IER);
889 }
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100890 tty_wakeup(port->port.tty);
Jiri Slaby1c456072008-02-07 00:16:46 -0800891 }
892 } else {
893 if (!(status & UART_MSR_CTS)) {
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100894 port->port.tty->hw_stopped = 1;
Jiri Slaby1c456072008-02-07 00:16:46 -0800895 if (port->type != PORT_16550A &&
896 !port->board->chip_flag) {
897 port->IER &= ~UART_IER_THRI;
898 outb(port->IER, port->ioaddr +
899 UART_IER);
900 }
901 }
902 }
903 }
904}
905
906static int mxser_startup(struct mxser_port *info)
907{
908 unsigned long page;
909 unsigned long flags;
910
911 page = __get_free_page(GFP_KERNEL);
912 if (!page)
913 return -ENOMEM;
914
915 spin_lock_irqsave(&info->slock, flags);
916
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100917 if (info->port.flags & ASYNC_INITIALIZED) {
Jiri Slaby1c456072008-02-07 00:16:46 -0800918 free_page(page);
919 spin_unlock_irqrestore(&info->slock, flags);
920 return 0;
921 }
922
923 if (!info->ioaddr || !info->type) {
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100924 if (info->port.tty)
925 set_bit(TTY_IO_ERROR, &info->port.tty->flags);
Jiri Slaby1c456072008-02-07 00:16:46 -0800926 free_page(page);
927 spin_unlock_irqrestore(&info->slock, flags);
928 return 0;
929 }
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100930 if (info->port.xmit_buf)
Jiri Slaby1c456072008-02-07 00:16:46 -0800931 free_page(page);
932 else
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100933 info->port.xmit_buf = (unsigned char *) page;
Jiri Slaby1c456072008-02-07 00:16:46 -0800934
935 /*
936 * Clear the FIFO buffers and disable them
937 * (they will be reenabled in mxser_change_speed())
938 */
939 if (info->board->chip_flag)
940 outb((UART_FCR_CLEAR_RCVR |
941 UART_FCR_CLEAR_XMIT |
942 MOXA_MUST_FCR_GDA_MODE_ENABLE), info->ioaddr + UART_FCR);
943 else
944 outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
945 info->ioaddr + UART_FCR);
946
947 /*
948 * At this point there's no way the LSR could still be 0xFF;
949 * if it is, then bail out, because there's likely no UART
950 * here.
951 */
952 if (inb(info->ioaddr + UART_LSR) == 0xff) {
953 spin_unlock_irqrestore(&info->slock, flags);
954 if (capable(CAP_SYS_ADMIN)) {
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100955 if (info->port.tty)
956 set_bit(TTY_IO_ERROR, &info->port.tty->flags);
Jiri Slaby1c456072008-02-07 00:16:46 -0800957 return 0;
958 } else
959 return -ENODEV;
960 }
961
962 /*
963 * Clear the interrupt registers.
964 */
965 (void) inb(info->ioaddr + UART_LSR);
966 (void) inb(info->ioaddr + UART_RX);
967 (void) inb(info->ioaddr + UART_IIR);
968 (void) inb(info->ioaddr + UART_MSR);
969
970 /*
971 * Now, initialize the UART
972 */
973 outb(UART_LCR_WLEN8, info->ioaddr + UART_LCR); /* reset DLAB */
974 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
975 outb(info->MCR, info->ioaddr + UART_MCR);
976
977 /*
978 * Finally, enable interrupts
979 */
980 info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
981
982 if (info->board->chip_flag)
983 info->IER |= MOXA_MUST_IER_EGDAI;
984 outb(info->IER, info->ioaddr + UART_IER); /* enable interrupts */
985
986 /*
987 * And clear the interrupt registers again for luck.
988 */
989 (void) inb(info->ioaddr + UART_LSR);
990 (void) inb(info->ioaddr + UART_RX);
991 (void) inb(info->ioaddr + UART_IIR);
992 (void) inb(info->ioaddr + UART_MSR);
993
Alan Cox0ad9e7d2008-07-16 21:56:10 +0100994 if (info->port.tty)
995 clear_bit(TTY_IO_ERROR, &info->port.tty->flags);
Jiri Slaby1c456072008-02-07 00:16:46 -0800996 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
997
998 /*
999 * and set the speed of the serial port
1000 */
1001 mxser_change_speed(info, NULL);
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001002 info->port.flags |= ASYNC_INITIALIZED;
Jiri Slaby1c456072008-02-07 00:16:46 -08001003 spin_unlock_irqrestore(&info->slock, flags);
1004
1005 return 0;
1006}
1007
1008/*
1009 * This routine will shutdown a serial port; interrupts maybe disabled, and
1010 * DTR is dropped if the hangup on close termio flag is on.
1011 */
1012static void mxser_shutdown(struct mxser_port *info)
1013{
1014 unsigned long flags;
1015
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001016 if (!(info->port.flags & ASYNC_INITIALIZED))
Jiri Slaby1c456072008-02-07 00:16:46 -08001017 return;
1018
1019 spin_lock_irqsave(&info->slock, flags);
1020
1021 /*
1022 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
1023 * here so the queue might never be waken up
1024 */
1025 wake_up_interruptible(&info->delta_msr_wait);
1026
1027 /*
1028 * Free the IRQ, if necessary
1029 */
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001030 if (info->port.xmit_buf) {
1031 free_page((unsigned long) info->port.xmit_buf);
1032 info->port.xmit_buf = NULL;
Jiri Slaby1c456072008-02-07 00:16:46 -08001033 }
1034
1035 info->IER = 0;
1036 outb(0x00, info->ioaddr + UART_IER);
1037
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001038 if (!info->port.tty || (info->port.tty->termios->c_cflag & HUPCL))
Jiri Slaby1c456072008-02-07 00:16:46 -08001039 info->MCR &= ~(UART_MCR_DTR | UART_MCR_RTS);
1040 outb(info->MCR, info->ioaddr + UART_MCR);
1041
1042 /* clear Rx/Tx FIFO's */
1043 if (info->board->chip_flag)
1044 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT |
1045 MOXA_MUST_FCR_GDA_MODE_ENABLE,
1046 info->ioaddr + UART_FCR);
1047 else
1048 outb(UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT,
1049 info->ioaddr + UART_FCR);
1050
1051 /* read data port to reset things */
1052 (void) inb(info->ioaddr + UART_RX);
1053
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001054 if (info->port.tty)
1055 set_bit(TTY_IO_ERROR, &info->port.tty->flags);
Jiri Slaby1c456072008-02-07 00:16:46 -08001056
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001057 info->port.flags &= ~ASYNC_INITIALIZED;
Jiri Slaby1c456072008-02-07 00:16:46 -08001058
1059 if (info->board->chip_flag)
1060 SET_MOXA_MUST_NO_SOFTWARE_FLOW_CONTROL(info->ioaddr);
1061
1062 spin_unlock_irqrestore(&info->slock, flags);
1063}
1064
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065/*
1066 * This routine is called whenever a serial port is opened. It
1067 * enables interrupts for a serial port, linking in its async structure into
1068 * the IRQ chain. It also performs the serial-specific
1069 * initialization for the tty structure.
1070 */
1071static int mxser_open(struct tty_struct *tty, struct file *filp)
1072{
Jiri Slaby1c456072008-02-07 00:16:46 -08001073 struct mxser_port *info;
1074 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 int retval, line;
1076
1077 line = tty->index;
1078 if (line == MXSER_PORTS)
1079 return 0;
1080 if (line < 0 || line > MXSER_PORTS)
1081 return -ENODEV;
Jiri Slaby1c456072008-02-07 00:16:46 -08001082 info = &mxser_boards[line / MXSER_PORTS_PER_BOARD].ports[line % MXSER_PORTS_PER_BOARD];
1083 if (!info->ioaddr)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001084 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
1086 tty->driver_data = info;
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001087 info->port.tty = tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 /*
1089 * Start up serial port
1090 */
Jiri Slaby1c456072008-02-07 00:16:46 -08001091 spin_lock_irqsave(&info->slock, flags);
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001092 info->port.count++;
Jiri Slaby1c456072008-02-07 00:16:46 -08001093 spin_unlock_irqrestore(&info->slock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 retval = mxser_startup(info);
1095 if (retval)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001096 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
1098 retval = mxser_block_til_ready(tty, filp, info);
1099 if (retval)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001100 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
Cedric Le Goater8cddd702007-02-10 01:46:33 -08001102 /* unmark here for very high baud rate (ex. 921600 bps) used */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 tty->low_latency = 1;
1104 return 0;
1105}
1106
Alan Cox978e5952008-04-30 00:53:59 -07001107static void mxser_flush_buffer(struct tty_struct *tty)
1108{
1109 struct mxser_port *info = tty->driver_data;
1110 char fcr;
1111 unsigned long flags;
1112
1113
1114 spin_lock_irqsave(&info->slock, flags);
1115 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1116
1117 fcr = inb(info->ioaddr + UART_FCR);
1118 outb((fcr | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
1119 info->ioaddr + UART_FCR);
1120 outb(fcr, info->ioaddr + UART_FCR);
1121
1122 spin_unlock_irqrestore(&info->slock, flags);
1123
1124 tty_wakeup(tty);
1125}
1126
1127
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128/*
1129 * This routine is called when the serial port gets closed. First, we
1130 * wait for the last remaining data to be sent. Then, we unlink its
1131 * async structure from the interrupt chain if necessary, and we free
1132 * that IRQ if nothing is left in the chain.
1133 */
1134static void mxser_close(struct tty_struct *tty, struct file *filp)
1135{
Jiri Slaby1c456072008-02-07 00:16:46 -08001136 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
1138 unsigned long timeout;
1139 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
1141 if (tty->index == MXSER_PORTS)
1142 return;
1143 if (!info)
Kirill Smelkov6f08b722005-11-07 00:59:23 -08001144 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
1146 spin_lock_irqsave(&info->slock, flags);
1147
1148 if (tty_hung_up_p(filp)) {
1149 spin_unlock_irqrestore(&info->slock, flags);
1150 return;
1151 }
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001152 if ((tty->count == 1) && (info->port.count != 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 /*
1154 * Uh, oh. tty->count is 1, which means that the tty
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001155 * structure will be freed. Info->port.count should always
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 * be one in these conditions. If it's greater than
1157 * one, we've got real problems, since it means the
1158 * serial port won't be shutdown.
1159 */
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001160 printk(KERN_ERR "mxser_close: bad serial port count; "
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001161 "tty->count is 1, info->port.count is %d\n", info->port.count);
1162 info->port.count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 }
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001164 if (--info->port.count < 0) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001165 printk(KERN_ERR "mxser_close: bad serial port count for "
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001166 "ttys%d: %d\n", tty->index, info->port.count);
1167 info->port.count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 }
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001169 if (info->port.count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 spin_unlock_irqrestore(&info->slock, flags);
1171 return;
1172 }
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001173 info->port.flags |= ASYNC_CLOSING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 spin_unlock_irqrestore(&info->slock, flags);
1175 /*
1176 * Save the termios structure, since this port may have
1177 * separate termios for callout and dialin.
1178 */
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001179 if (info->port.flags & ASYNC_NORMAL_ACTIVE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 info->normal_termios = *tty->termios;
1181 /*
1182 * Now we wait for the transmit buffer to clear; and we notify
1183 * the line discipline to only process XON/XOFF characters.
1184 */
1185 tty->closing = 1;
Alan Cox44b7d1b2008-07-16 21:57:18 +01001186 if (info->port.closing_wait != ASYNC_CLOSING_WAIT_NONE)
1187 tty_wait_until_sent(tty, info->port.closing_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 /*
1189 * At this point we stop accepting input. To do this, we
1190 * disable the receive line status interrupts, and tell the
1191 * interrupt driver to stop checking the data ready bit in the
1192 * line status register.
1193 */
1194 info->IER &= ~UART_IER_RLSI;
Jiri Slaby1c456072008-02-07 00:16:46 -08001195 if (info->board->chip_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 info->IER &= ~MOXA_MUST_RECV_ISR;
Jiri Slaby1c456072008-02-07 00:16:46 -08001197
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001198 if (info->port.flags & ASYNC_INITIALIZED) {
Jiri Slaby1c456072008-02-07 00:16:46 -08001199 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 /*
1201 * Before we drop DTR, make sure the UART transmitter
1202 * has completely drained; this is especially
1203 * important if there is a transmit FIFO!
1204 */
1205 timeout = jiffies + HZ;
Jiri Slaby1c456072008-02-07 00:16:46 -08001206 while (!(inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT)) {
Nishanth Aravamudanda4cd8d2005-09-10 00:27:30 -07001207 schedule_timeout_interruptible(5);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 if (time_after(jiffies, timeout))
1209 break;
1210 }
1211 }
1212 mxser_shutdown(info);
1213
Alan Cox978e5952008-04-30 00:53:59 -07001214 mxser_flush_buffer(tty);
Jiri Slaby1c456072008-02-07 00:16:46 -08001215 tty_ldisc_flush(tty);
1216
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 tty->closing = 0;
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001218 info->port.tty = NULL;
1219 if (info->port.blocked_open) {
Alan Cox44b7d1b2008-07-16 21:57:18 +01001220 if (info->port.close_delay)
1221 schedule_timeout_interruptible(info->port.close_delay);
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001222 wake_up_interruptible(&info->port.open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 }
1224
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001225 info->port.flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226}
1227
1228static int mxser_write(struct tty_struct *tty, const unsigned char *buf, int count)
1229{
1230 int c, total = 0;
Jiri Slaby1c456072008-02-07 00:16:46 -08001231 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 unsigned long flags;
1233
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001234 if (!info->port.xmit_buf)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001235 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
1237 while (1) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001238 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1239 SERIAL_XMIT_SIZE - info->xmit_head));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 if (c <= 0)
1241 break;
1242
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001243 memcpy(info->port.xmit_buf + info->xmit_head, buf, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 spin_lock_irqsave(&info->slock, flags);
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001245 info->xmit_head = (info->xmit_head + c) &
1246 (SERIAL_XMIT_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 info->xmit_cnt += c;
1248 spin_unlock_irqrestore(&info->slock, flags);
1249
1250 buf += c;
1251 count -= c;
1252 total += c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 }
1254
Jiri Slaby1c456072008-02-07 00:16:46 -08001255 if (info->xmit_cnt && !tty->stopped) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001256 if (!tty->hw_stopped ||
1257 (info->type == PORT_16550A) ||
Jiri Slaby1c456072008-02-07 00:16:46 -08001258 (info->board->chip_flag)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby1c456072008-02-07 00:16:46 -08001260 outb(info->IER & ~UART_IER_THRI, info->ioaddr +
1261 UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 info->IER |= UART_IER_THRI;
Jiri Slaby1c456072008-02-07 00:16:46 -08001263 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 spin_unlock_irqrestore(&info->slock, flags);
1265 }
1266 }
1267 return total;
1268}
1269
Alan Cox0be2ead2008-04-30 00:54:03 -07001270static int mxser_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271{
Jiri Slaby1c456072008-02-07 00:16:46 -08001272 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 unsigned long flags;
1274
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001275 if (!info->port.xmit_buf)
Alan Cox0be2ead2008-04-30 00:54:03 -07001276 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
1278 if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1)
Alan Cox0be2ead2008-04-30 00:54:03 -07001279 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
1281 spin_lock_irqsave(&info->slock, flags);
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001282 info->port.xmit_buf[info->xmit_head++] = ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 info->xmit_head &= SERIAL_XMIT_SIZE - 1;
1284 info->xmit_cnt++;
1285 spin_unlock_irqrestore(&info->slock, flags);
Jiri Slaby1c456072008-02-07 00:16:46 -08001286 if (!tty->stopped) {
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001287 if (!tty->hw_stopped ||
1288 (info->type == PORT_16550A) ||
Jiri Slaby1c456072008-02-07 00:16:46 -08001289 info->board->chip_flag) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 spin_lock_irqsave(&info->slock, flags);
Jiri Slaby1c456072008-02-07 00:16:46 -08001291 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 info->IER |= UART_IER_THRI;
Jiri Slaby1c456072008-02-07 00:16:46 -08001293 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 spin_unlock_irqrestore(&info->slock, flags);
1295 }
1296 }
Alan Cox0be2ead2008-04-30 00:54:03 -07001297 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298}
1299
1300
1301static void mxser_flush_chars(struct tty_struct *tty)
1302{
Jiri Slaby1c456072008-02-07 00:16:46 -08001303 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 unsigned long flags;
1305
Jiri Slabyace7dd92008-07-25 01:48:22 -07001306 if (info->xmit_cnt <= 0 || tty->stopped || !info->port.xmit_buf ||
1307 (tty->hw_stopped && info->type != PORT_16550A &&
1308 !info->board->chip_flag))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 return;
1310
1311 spin_lock_irqsave(&info->slock, flags);
1312
Jiri Slaby1c456072008-02-07 00:16:46 -08001313 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 info->IER |= UART_IER_THRI;
Jiri Slaby1c456072008-02-07 00:16:46 -08001315 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
1317 spin_unlock_irqrestore(&info->slock, flags);
1318}
1319
1320static int mxser_write_room(struct tty_struct *tty)
1321{
Jiri Slaby1c456072008-02-07 00:16:46 -08001322 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 int ret;
1324
1325 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
Jiri Slabyace7dd92008-07-25 01:48:22 -07001326 return ret < 0 ? 0 : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327}
1328
1329static int mxser_chars_in_buffer(struct tty_struct *tty)
1330{
Jiri Slaby1c456072008-02-07 00:16:46 -08001331 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 return info->xmit_cnt;
1333}
1334
Jiri Slaby1c456072008-02-07 00:16:46 -08001335/*
1336 * ------------------------------------------------------------
1337 * friends of mxser_ioctl()
1338 * ------------------------------------------------------------
1339 */
1340static int mxser_get_serial_info(struct mxser_port *info,
1341 struct serial_struct __user *retinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342{
Jiri Slaby1c456072008-02-07 00:16:46 -08001343 struct serial_struct tmp = {
1344 .type = info->type,
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001345 .line = info->port.tty->index,
Jiri Slaby1c456072008-02-07 00:16:46 -08001346 .port = info->ioaddr,
1347 .irq = info->board->irq,
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001348 .flags = info->port.flags,
Jiri Slaby1c456072008-02-07 00:16:46 -08001349 .baud_base = info->baud_base,
Alan Cox44b7d1b2008-07-16 21:57:18 +01001350 .close_delay = info->port.close_delay,
1351 .closing_wait = info->port.closing_wait,
Jiri Slaby1c456072008-02-07 00:16:46 -08001352 .custom_divisor = info->custom_divisor,
1353 .hub6 = 0
1354 };
1355 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
1356 return -EFAULT;
1357 return 0;
1358}
1359
1360static int mxser_set_serial_info(struct mxser_port *info,
1361 struct serial_struct __user *new_info)
1362{
1363 struct serial_struct new_serial;
Jiri Slaby80ff8a82008-02-07 00:16:51 -08001364 speed_t baud;
Jiri Slaby1c456072008-02-07 00:16:46 -08001365 unsigned long sl_flags;
1366 unsigned int flags;
1367 int retval = 0;
1368
1369 if (!new_info || !info->ioaddr)
Jiri Slaby80ff8a82008-02-07 00:16:51 -08001370 return -ENODEV;
Jiri Slaby1c456072008-02-07 00:16:46 -08001371 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
1372 return -EFAULT;
1373
Jiri Slaby80ff8a82008-02-07 00:16:51 -08001374 if (new_serial.irq != info->board->irq ||
1375 new_serial.port != info->ioaddr)
1376 return -EINVAL;
Jiri Slaby1c456072008-02-07 00:16:46 -08001377
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001378 flags = info->port.flags & ASYNC_SPD_MASK;
Jiri Slaby1c456072008-02-07 00:16:46 -08001379
1380 if (!capable(CAP_SYS_ADMIN)) {
1381 if ((new_serial.baud_base != info->baud_base) ||
Alan Cox44b7d1b2008-07-16 21:57:18 +01001382 (new_serial.close_delay != info->port.close_delay) ||
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001383 ((new_serial.flags & ~ASYNC_USR_MASK) != (info->port.flags & ~ASYNC_USR_MASK)))
Jiri Slaby1c456072008-02-07 00:16:46 -08001384 return -EPERM;
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001385 info->port.flags = ((info->port.flags & ~ASYNC_USR_MASK) |
Jiri Slaby1c456072008-02-07 00:16:46 -08001386 (new_serial.flags & ASYNC_USR_MASK));
1387 } else {
1388 /*
1389 * OK, past this point, all the error checking has been done.
1390 * At this point, we start making changes.....
1391 */
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001392 info->port.flags = ((info->port.flags & ~ASYNC_FLAGS) |
Jiri Slaby1c456072008-02-07 00:16:46 -08001393 (new_serial.flags & ASYNC_FLAGS));
Alan Cox44b7d1b2008-07-16 21:57:18 +01001394 info->port.close_delay = new_serial.close_delay * HZ / 100;
1395 info->port.closing_wait = new_serial.closing_wait * HZ / 100;
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001396 info->port.tty->low_latency =
1397 (info->port.flags & ASYNC_LOW_LATENCY) ? 1 : 0;
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001398 if ((info->port.flags & ASYNC_SPD_MASK) == ASYNC_SPD_CUST &&
Jiri Slaby80ff8a82008-02-07 00:16:51 -08001399 (new_serial.baud_base != info->baud_base ||
1400 new_serial.custom_divisor !=
1401 info->custom_divisor)) {
1402 baud = new_serial.baud_base / new_serial.custom_divisor;
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001403 tty_encode_baud_rate(info->port.tty, baud, baud);
Jiri Slaby80ff8a82008-02-07 00:16:51 -08001404 }
Jiri Slaby1c456072008-02-07 00:16:46 -08001405 }
1406
1407 info->type = new_serial.type;
1408
1409 process_txrx_fifo(info);
1410
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001411 if (info->port.flags & ASYNC_INITIALIZED) {
1412 if (flags != (info->port.flags & ASYNC_SPD_MASK)) {
Jiri Slaby1c456072008-02-07 00:16:46 -08001413 spin_lock_irqsave(&info->slock, sl_flags);
1414 mxser_change_speed(info, NULL);
1415 spin_unlock_irqrestore(&info->slock, sl_flags);
1416 }
1417 } else
1418 retval = mxser_startup(info);
1419
1420 return retval;
1421}
1422
1423/*
1424 * mxser_get_lsr_info - get line status register info
1425 *
1426 * Purpose: Let user call ioctl() to get info when the UART physically
1427 * is emptied. On bus types like RS485, the transmitter must
1428 * release the bus after transmitting. This must be done when
1429 * the transmit shift register is empty, not be done when the
1430 * transmit holding register is empty. This functionality
1431 * allows an RS485 driver to be written in user space.
1432 */
1433static int mxser_get_lsr_info(struct mxser_port *info,
1434 unsigned int __user *value)
1435{
1436 unsigned char status;
1437 unsigned int result;
1438 unsigned long flags;
1439
1440 spin_lock_irqsave(&info->slock, flags);
1441 status = inb(info->ioaddr + UART_LSR);
1442 spin_unlock_irqrestore(&info->slock, flags);
1443 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1444 return put_user(result, value);
1445}
1446
Jiri Slaby1c456072008-02-07 00:16:46 -08001447static int mxser_tiocmget(struct tty_struct *tty, struct file *file)
1448{
1449 struct mxser_port *info = tty->driver_data;
1450 unsigned char control, status;
1451 unsigned long flags;
1452
1453
1454 if (tty->index == MXSER_PORTS)
1455 return -ENOIOCTLCMD;
1456 if (test_bit(TTY_IO_ERROR, &tty->flags))
1457 return -EIO;
1458
1459 control = info->MCR;
1460
1461 spin_lock_irqsave(&info->slock, flags);
1462 status = inb(info->ioaddr + UART_MSR);
1463 if (status & UART_MSR_ANY_DELTA)
1464 mxser_check_modem_status(info, status);
1465 spin_unlock_irqrestore(&info->slock, flags);
1466 return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0) |
1467 ((control & UART_MCR_DTR) ? TIOCM_DTR : 0) |
1468 ((status & UART_MSR_DCD) ? TIOCM_CAR : 0) |
1469 ((status & UART_MSR_RI) ? TIOCM_RNG : 0) |
1470 ((status & UART_MSR_DSR) ? TIOCM_DSR : 0) |
1471 ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
1472}
1473
1474static int mxser_tiocmset(struct tty_struct *tty, struct file *file,
1475 unsigned int set, unsigned int clear)
1476{
1477 struct mxser_port *info = tty->driver_data;
1478 unsigned long flags;
1479
1480
1481 if (tty->index == MXSER_PORTS)
1482 return -ENOIOCTLCMD;
1483 if (test_bit(TTY_IO_ERROR, &tty->flags))
1484 return -EIO;
1485
1486 spin_lock_irqsave(&info->slock, flags);
1487
1488 if (set & TIOCM_RTS)
1489 info->MCR |= UART_MCR_RTS;
1490 if (set & TIOCM_DTR)
1491 info->MCR |= UART_MCR_DTR;
1492
1493 if (clear & TIOCM_RTS)
1494 info->MCR &= ~UART_MCR_RTS;
1495 if (clear & TIOCM_DTR)
1496 info->MCR &= ~UART_MCR_DTR;
1497
1498 outb(info->MCR, info->ioaddr + UART_MCR);
1499 spin_unlock_irqrestore(&info->slock, flags);
1500 return 0;
1501}
1502
1503static int __init mxser_program_mode(int port)
1504{
1505 int id, i, j, n;
1506
1507 outb(0, port);
1508 outb(0, port);
1509 outb(0, port);
1510 (void)inb(port);
1511 (void)inb(port);
1512 outb(0, port);
1513 (void)inb(port);
1514
1515 id = inb(port + 1) & 0x1F;
1516 if ((id != C168_ASIC_ID) &&
1517 (id != C104_ASIC_ID) &&
1518 (id != C102_ASIC_ID) &&
1519 (id != CI132_ASIC_ID) &&
1520 (id != CI134_ASIC_ID) &&
1521 (id != CI104J_ASIC_ID))
1522 return -1;
1523 for (i = 0, j = 0; i < 4; i++) {
1524 n = inb(port + 2);
1525 if (n == 'M') {
1526 j = 1;
1527 } else if ((j == 1) && (n == 1)) {
1528 j = 2;
1529 break;
1530 } else
1531 j = 0;
1532 }
1533 if (j != 2)
1534 id = -2;
1535 return id;
1536}
1537
1538static void __init mxser_normal_mode(int port)
1539{
1540 int i, n;
1541
1542 outb(0xA5, port + 1);
1543 outb(0x80, port + 3);
1544 outb(12, port + 0); /* 9600 bps */
1545 outb(0, port + 1);
1546 outb(0x03, port + 3); /* 8 data bits */
1547 outb(0x13, port + 4); /* loop back mode */
1548 for (i = 0; i < 16; i++) {
1549 n = inb(port + 5);
1550 if ((n & 0x61) == 0x60)
1551 break;
1552 if ((n & 1) == 1)
1553 (void)inb(port);
1554 }
1555 outb(0x00, port + 4);
1556}
1557
1558#define CHIP_SK 0x01 /* Serial Data Clock in Eprom */
1559#define CHIP_DO 0x02 /* Serial Data Output in Eprom */
1560#define CHIP_CS 0x04 /* Serial Chip Select in Eprom */
1561#define CHIP_DI 0x08 /* Serial Data Input in Eprom */
1562#define EN_CCMD 0x000 /* Chip's command register */
1563#define EN0_RSARLO 0x008 /* Remote start address reg 0 */
1564#define EN0_RSARHI 0x009 /* Remote start address reg 1 */
1565#define EN0_RCNTLO 0x00A /* Remote byte count reg WR */
1566#define EN0_RCNTHI 0x00B /* Remote byte count reg WR */
1567#define EN0_DCFG 0x00E /* Data configuration reg WR */
1568#define EN0_PORT 0x010 /* Rcv missed frame error counter RD */
1569#define ENC_PAGE0 0x000 /* Select page 0 of chip registers */
1570#define ENC_PAGE3 0x0C0 /* Select page 3 of chip registers */
1571static int __init mxser_read_register(int port, unsigned short *regs)
1572{
1573 int i, k, value, id;
1574 unsigned int j;
1575
1576 id = mxser_program_mode(port);
1577 if (id < 0)
1578 return id;
1579 for (i = 0; i < 14; i++) {
1580 k = (i & 0x3F) | 0x180;
1581 for (j = 0x100; j > 0; j >>= 1) {
1582 outb(CHIP_CS, port);
1583 if (k & j) {
1584 outb(CHIP_CS | CHIP_DO, port);
1585 outb(CHIP_CS | CHIP_DO | CHIP_SK, port); /* A? bit of read */
1586 } else {
1587 outb(CHIP_CS, port);
1588 outb(CHIP_CS | CHIP_SK, port); /* A? bit of read */
1589 }
1590 }
1591 (void)inb(port);
1592 value = 0;
1593 for (k = 0, j = 0x8000; k < 16; k++, j >>= 1) {
1594 outb(CHIP_CS, port);
1595 outb(CHIP_CS | CHIP_SK, port);
1596 if (inb(port) & CHIP_DI)
1597 value |= j;
1598 }
1599 regs[i] = value;
1600 outb(0, port);
1601 }
1602 mxser_normal_mode(port);
1603 return id;
1604}
1605
1606static int mxser_ioctl_special(unsigned int cmd, void __user *argp)
1607{
1608 struct mxser_port *port;
1609 int result, status;
1610 unsigned int i, j;
Alan Cox9d6d1622008-04-30 00:53:20 -07001611 int ret = 0;
Jiri Slaby1c456072008-02-07 00:16:46 -08001612
1613 switch (cmd) {
1614 case MOXA_GET_MAJOR:
Jiri Slaby41aee9a2008-07-25 01:48:19 -07001615 printk(KERN_WARNING "mxser: '%s' uses deprecated ioctl %x, fix "
1616 "your userspace\n", current->comm, cmd);
Jiri Slaby1c456072008-02-07 00:16:46 -08001617 return put_user(ttymajor, (int __user *)argp);
1618
1619 case MOXA_CHKPORTENABLE:
1620 result = 0;
Alan Cox9d6d1622008-04-30 00:53:20 -07001621 lock_kernel();
Jiri Slaby1c456072008-02-07 00:16:46 -08001622 for (i = 0; i < MXSER_BOARDS; i++)
1623 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++)
1624 if (mxser_boards[i].ports[j].ioaddr)
1625 result |= (1 << i);
Alan Cox9d6d1622008-04-30 00:53:20 -07001626 unlock_kernel();
Jiri Slaby1c456072008-02-07 00:16:46 -08001627 return put_user(result, (unsigned long __user *)argp);
1628 case MOXA_GETDATACOUNT:
Alan Cox9d6d1622008-04-30 00:53:20 -07001629 lock_kernel();
Jiri Slaby1c456072008-02-07 00:16:46 -08001630 if (copy_to_user(argp, &mxvar_log, sizeof(mxvar_log)))
Alan Cox9d6d1622008-04-30 00:53:20 -07001631 ret = -EFAULT;
1632 unlock_kernel();
1633 return ret;
Jiri Slaby72800df2008-07-25 01:48:20 -07001634 case MOXA_GETMSTATUS: {
1635 struct mxser_mstatus ms, __user *msu = argp;
Alan Cox9d6d1622008-04-30 00:53:20 -07001636 lock_kernel();
Jiri Slaby1c456072008-02-07 00:16:46 -08001637 for (i = 0; i < MXSER_BOARDS; i++)
1638 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++) {
1639 port = &mxser_boards[i].ports[j];
Jiri Slaby72800df2008-07-25 01:48:20 -07001640 memset(&ms, 0, sizeof(ms));
Jiri Slaby1c456072008-02-07 00:16:46 -08001641
Jiri Slaby72800df2008-07-25 01:48:20 -07001642 if (!port->ioaddr)
1643 goto copy;
Jiri Slaby1c456072008-02-07 00:16:46 -08001644
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001645 if (!port->port.tty || !port->port.tty->termios)
Jiri Slaby72800df2008-07-25 01:48:20 -07001646 ms.cflag = port->normal_termios.c_cflag;
Jiri Slaby1c456072008-02-07 00:16:46 -08001647 else
Jiri Slaby72800df2008-07-25 01:48:20 -07001648 ms.cflag = port->port.tty->termios->c_cflag;
Jiri Slaby1c456072008-02-07 00:16:46 -08001649
1650 status = inb(port->ioaddr + UART_MSR);
Jiri Slaby72800df2008-07-25 01:48:20 -07001651 if (status & UART_MSR_DCD)
1652 ms.dcd = 1;
1653 if (status & UART_MSR_DSR)
1654 ms.dsr = 1;
1655 if (status & UART_MSR_CTS)
1656 ms.cts = 1;
1657 copy:
1658 if (copy_to_user(msu, &ms, sizeof(ms))) {
1659 unlock_kernel();
1660 return -EFAULT;
1661 }
1662 msu++;
Jiri Slaby1c456072008-02-07 00:16:46 -08001663 }
Alan Cox9d6d1622008-04-30 00:53:20 -07001664 unlock_kernel();
Jiri Slaby1c456072008-02-07 00:16:46 -08001665 return 0;
Jiri Slaby72800df2008-07-25 01:48:20 -07001666 }
Jiri Slaby1c456072008-02-07 00:16:46 -08001667 case MOXA_ASPP_MON_EXT: {
Jiri Slaby72800df2008-07-25 01:48:20 -07001668 struct mxser_mon_ext *me; /* it's 2k, stack unfriendly */
1669 unsigned int cflag, iflag, p;
1670 u8 opmode;
1671
1672 me = kzalloc(sizeof(*me), GFP_KERNEL);
1673 if (!me)
1674 return -ENOMEM;
Jiri Slaby1c456072008-02-07 00:16:46 -08001675
Alan Cox9d6d1622008-04-30 00:53:20 -07001676 lock_kernel();
Jiri Slaby72800df2008-07-25 01:48:20 -07001677 for (i = 0, p = 0; i < MXSER_BOARDS; i++) {
1678 for (j = 0; j < MXSER_PORTS_PER_BOARD; j++, p++) {
1679 if (p >= ARRAY_SIZE(me->rx_cnt)) {
1680 i = MXSER_BOARDS;
1681 break;
1682 }
Jiri Slaby1c456072008-02-07 00:16:46 -08001683 port = &mxser_boards[i].ports[j];
1684 if (!port->ioaddr)
1685 continue;
1686
Jiri Slaby72800df2008-07-25 01:48:20 -07001687 status = mxser_get_msr(port->ioaddr, 0, p);
Jiri Slaby1c456072008-02-07 00:16:46 -08001688
1689 if (status & UART_MSR_TERI)
1690 port->icount.rng++;
1691 if (status & UART_MSR_DDSR)
1692 port->icount.dsr++;
1693 if (status & UART_MSR_DDCD)
1694 port->icount.dcd++;
1695 if (status & UART_MSR_DCTS)
1696 port->icount.cts++;
1697
1698 port->mon_data.modem_status = status;
Jiri Slaby72800df2008-07-25 01:48:20 -07001699 me->rx_cnt[p] = port->mon_data.rxcnt;
1700 me->tx_cnt[p] = port->mon_data.txcnt;
1701 me->up_rxcnt[p] = port->mon_data.up_rxcnt;
1702 me->up_txcnt[p] = port->mon_data.up_txcnt;
1703 me->modem_status[p] =
Jiri Slaby1c456072008-02-07 00:16:46 -08001704 port->mon_data.modem_status;
Jiri Slaby72800df2008-07-25 01:48:20 -07001705 me->baudrate[p] = tty_get_baud_rate(port->port.tty);
Jiri Slaby1c456072008-02-07 00:16:46 -08001706
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001707 if (!port->port.tty || !port->port.tty->termios) {
Jiri Slaby1c456072008-02-07 00:16:46 -08001708 cflag = port->normal_termios.c_cflag;
1709 iflag = port->normal_termios.c_iflag;
1710 } else {
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001711 cflag = port->port.tty->termios->c_cflag;
1712 iflag = port->port.tty->termios->c_iflag;
Jiri Slaby1c456072008-02-07 00:16:46 -08001713 }
1714
Jiri Slaby72800df2008-07-25 01:48:20 -07001715 me->databits[p] = cflag & CSIZE;
1716 me->stopbits[p] = cflag & CSTOPB;
1717 me->parity[p] = cflag & (PARENB | PARODD |
1718 CMSPAR);
Jiri Slaby1c456072008-02-07 00:16:46 -08001719
1720 if (cflag & CRTSCTS)
Jiri Slaby72800df2008-07-25 01:48:20 -07001721 me->flowctrl[p] |= 0x03;
Jiri Slaby1c456072008-02-07 00:16:46 -08001722
1723 if (iflag & (IXON | IXOFF))
Jiri Slaby72800df2008-07-25 01:48:20 -07001724 me->flowctrl[p] |= 0x0C;
Jiri Slaby1c456072008-02-07 00:16:46 -08001725
1726 if (port->type == PORT_16550A)
Jiri Slaby72800df2008-07-25 01:48:20 -07001727 me->fifo[p] = 1;
Jiri Slaby1c456072008-02-07 00:16:46 -08001728
Jiri Slaby72800df2008-07-25 01:48:20 -07001729 opmode = inb(port->opmode_ioaddr) >>
1730 ((p % 4) * 2);
Jiri Slaby1c456072008-02-07 00:16:46 -08001731 opmode &= OP_MODE_MASK;
Jiri Slaby72800df2008-07-25 01:48:20 -07001732 me->iftype[p] = opmode;
Jiri Slaby1c456072008-02-07 00:16:46 -08001733 }
Alan Cox9d6d1622008-04-30 00:53:20 -07001734 }
1735 unlock_kernel();
Jiri Slaby72800df2008-07-25 01:48:20 -07001736 if (copy_to_user(argp, me, sizeof(*me)))
1737 ret = -EFAULT;
1738 kfree(me);
1739 return ret;
Alan Cox9d6d1622008-04-30 00:53:20 -07001740 }
1741 default:
Jiri Slaby1c456072008-02-07 00:16:46 -08001742 return -ENOIOCTLCMD;
1743 }
1744 return 0;
1745}
1746
1747static int mxser_cflags_changed(struct mxser_port *info, unsigned long arg,
1748 struct async_icount *cprev)
1749{
1750 struct async_icount cnow;
1751 unsigned long flags;
1752 int ret;
1753
1754 spin_lock_irqsave(&info->slock, flags);
1755 cnow = info->icount; /* atomic copy */
1756 spin_unlock_irqrestore(&info->slock, flags);
1757
1758 ret = ((arg & TIOCM_RNG) && (cnow.rng != cprev->rng)) ||
1759 ((arg & TIOCM_DSR) && (cnow.dsr != cprev->dsr)) ||
1760 ((arg & TIOCM_CD) && (cnow.dcd != cprev->dcd)) ||
1761 ((arg & TIOCM_CTS) && (cnow.cts != cprev->cts));
1762
1763 *cprev = cnow;
1764
1765 return ret;
1766}
1767
1768static int mxser_ioctl(struct tty_struct *tty, struct file *file,
1769 unsigned int cmd, unsigned long arg)
1770{
1771 struct mxser_port *info = tty->driver_data;
1772 struct async_icount cnow;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 unsigned long flags;
1774 void __user *argp = (void __user *)arg;
Jiri Slaby1c456072008-02-07 00:16:46 -08001775 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776
1777 if (tty->index == MXSER_PORTS)
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001778 return mxser_ioctl_special(cmd, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 if (cmd == MOXA_SET_OP_MODE || cmd == MOXA_GET_OP_MODE) {
Jiri Slaby1c456072008-02-07 00:16:46 -08001781 int p;
1782 unsigned long opmode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 static unsigned char ModeMask[] = { 0xfc, 0xf3, 0xcf, 0x3f };
1784 int shiftbit;
1785 unsigned char val, mask;
1786
Jiri Slaby1c456072008-02-07 00:16:46 -08001787 p = tty->index % 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 if (cmd == MOXA_SET_OP_MODE) {
1789 if (get_user(opmode, (int __user *) argp))
1790 return -EFAULT;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001791 if (opmode != RS232_MODE &&
1792 opmode != RS485_2WIRE_MODE &&
1793 opmode != RS422_MODE &&
1794 opmode != RS485_4WIRE_MODE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 return -EFAULT;
Alan Cox9d6d1622008-04-30 00:53:20 -07001796 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 mask = ModeMask[p];
1798 shiftbit = p * 2;
1799 val = inb(info->opmode_ioaddr);
1800 val &= mask;
1801 val |= (opmode << shiftbit);
1802 outb(val, info->opmode_ioaddr);
Alan Cox9d6d1622008-04-30 00:53:20 -07001803 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 } else {
Alan Cox9d6d1622008-04-30 00:53:20 -07001805 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 shiftbit = p * 2;
1807 opmode = inb(info->opmode_ioaddr) >> shiftbit;
1808 opmode &= OP_MODE_MASK;
Alan Cox9d6d1622008-04-30 00:53:20 -07001809 unlock_kernel();
Jiri Slaby1c456072008-02-07 00:16:46 -08001810 if (put_user(opmode, (int __user *)argp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 return -EFAULT;
1812 }
1813 return 0;
1814 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
Jiri Slaby1c456072008-02-07 00:16:46 -08001816 if (cmd != TIOCGSERIAL && cmd != TIOCMIWAIT && cmd != TIOCGICOUNT &&
1817 test_bit(TTY_IO_ERROR, &tty->flags))
1818 return -EIO;
1819
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 case TIOCGSERIAL:
Alan Cox9d6d1622008-04-30 00:53:20 -07001822 lock_kernel();
1823 retval = mxser_get_serial_info(info, argp);
1824 unlock_kernel();
1825 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 case TIOCSSERIAL:
Alan Cox9d6d1622008-04-30 00:53:20 -07001827 lock_kernel();
1828 retval = mxser_set_serial_info(info, argp);
1829 unlock_kernel();
1830 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 case TIOCSERGETLSR: /* Get line status register */
Alan Cox9d6d1622008-04-30 00:53:20 -07001832 return mxser_get_lsr_info(info, argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 /*
1834 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1835 * - mask passed in arg for lines of interest
1836 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1837 * Caller should use TIOCGICOUNT to see which one it was
1838 */
Jiri Slabyfc838152007-04-23 14:41:04 -07001839 case TIOCMIWAIT:
1840 spin_lock_irqsave(&info->slock, flags);
1841 cnow = info->icount; /* note the counters on entry */
1842 spin_unlock_irqrestore(&info->slock, flags);
1843
Jiri Slaby1c456072008-02-07 00:16:46 -08001844 return wait_event_interruptible(info->delta_msr_wait,
1845 mxser_cflags_changed(info, arg, &cnow));
1846 /*
1847 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1848 * Return: write counters to the user passed counter struct
1849 * NB: both 1->0 and 0->1 transitions are counted except for
1850 * RI where only 0->1 is counted.
1851 */
Jiri Slaby41aee9a2008-07-25 01:48:19 -07001852 case TIOCGICOUNT: {
1853 struct serial_icounter_struct icnt = { 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 spin_lock_irqsave(&info->slock, flags);
1855 cnow = info->icount;
1856 spin_unlock_irqrestore(&info->slock, flags);
Jiri Slaby41aee9a2008-07-25 01:48:19 -07001857
1858 icnt.frame = cnow.frame;
1859 icnt.brk = cnow.brk;
1860 icnt.overrun = cnow.overrun;
1861 icnt.buf_overrun = cnow.buf_overrun;
1862 icnt.parity = cnow.parity;
1863 icnt.rx = cnow.rx;
1864 icnt.tx = cnow.tx;
1865 icnt.cts = cnow.cts;
1866 icnt.dsr = cnow.dsr;
1867 icnt.rng = cnow.rng;
1868 icnt.dcd = cnow.dcd;
1869
1870 return copy_to_user(argp, &icnt, sizeof(icnt)) ? -EFAULT : 0;
1871 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 case MOXA_HighSpeedOn:
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001873 return put_user(info->baud_base != 115200 ? 1 : 0, (int __user *)argp);
Jiri Slaby1c456072008-02-07 00:16:46 -08001874 case MOXA_SDS_RSTICOUNTER:
Alan Cox9d6d1622008-04-30 00:53:20 -07001875 lock_kernel();
Jiri Slaby1c456072008-02-07 00:16:46 -08001876 info->mon_data.rxcnt = 0;
1877 info->mon_data.txcnt = 0;
Alan Cox9d6d1622008-04-30 00:53:20 -07001878 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 return 0;
1880
1881 case MOXA_ASPP_OQUEUE:{
Jiri Slaby1c456072008-02-07 00:16:46 -08001882 int len, lsr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883
Alan Cox9d6d1622008-04-30 00:53:20 -07001884 lock_kernel();
Jiri Slaby1c456072008-02-07 00:16:46 -08001885 len = mxser_chars_in_buffer(tty);
Jiri Slaby1c456072008-02-07 00:16:46 -08001886 lsr = inb(info->ioaddr + UART_LSR) & UART_LSR_TEMT;
Jiri Slaby1c456072008-02-07 00:16:46 -08001887 len += (lsr ? 0 : 1);
Alan Cox9d6d1622008-04-30 00:53:20 -07001888 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889
Jiri Slaby1c456072008-02-07 00:16:46 -08001890 return put_user(len, (int __user *)argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 }
Jiri Slaby1c456072008-02-07 00:16:46 -08001892 case MOXA_ASPP_MON: {
1893 int mcr, status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894
Alan Cox9d6d1622008-04-30 00:53:20 -07001895 lock_kernel();
Jiri Slaby1c456072008-02-07 00:16:46 -08001896 status = mxser_get_msr(info->ioaddr, 1, tty->index);
1897 mxser_check_modem_status(info, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898
Jiri Slaby1c456072008-02-07 00:16:46 -08001899 mcr = inb(info->ioaddr + UART_MCR);
1900 if (mcr & MOXA_MUST_MCR_XON_FLAG)
1901 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFHOLD;
1902 else
1903 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFHOLD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904
Jiri Slaby1c456072008-02-07 00:16:46 -08001905 if (mcr & MOXA_MUST_MCR_TX_XON)
1906 info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFXENT;
1907 else
1908 info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFXENT;
1909
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001910 if (info->port.tty->hw_stopped)
Jiri Slaby1c456072008-02-07 00:16:46 -08001911 info->mon_data.hold_reason |= NPPI_NOTIFY_CTSHOLD;
1912 else
1913 info->mon_data.hold_reason &= ~NPPI_NOTIFY_CTSHOLD;
Alan Cox9d6d1622008-04-30 00:53:20 -07001914 unlock_kernel();
Jiri Slaby1c456072008-02-07 00:16:46 -08001915 if (copy_to_user(argp, &info->mon_data,
1916 sizeof(struct mxser_mon)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 return -EFAULT;
Jiri Slaby1c456072008-02-07 00:16:46 -08001918
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 return 0;
Jiri Slaby1c456072008-02-07 00:16:46 -08001920 }
1921 case MOXA_ASPP_LSTATUS: {
1922 if (put_user(info->err_shadow, (unsigned char __user *)argp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
Jiri Slaby1c456072008-02-07 00:16:46 -08001925 info->err_shadow = 0;
1926 return 0;
1927 }
1928 case MOXA_SET_BAUD_METHOD: {
1929 int method;
1930
1931 if (get_user(method, (int __user *)argp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 return -EFAULT;
Jiri Slaby1c456072008-02-07 00:16:46 -08001933 mxser_set_baud_method[tty->index] = method;
1934 return put_user(method, (int __user *)argp);
1935 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 default:
1937 return -ENOIOCTLCMD;
1938 }
1939 return 0;
1940}
1941
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942static void mxser_stoprx(struct tty_struct *tty)
1943{
Jiri Slaby1c456072008-02-07 00:16:46 -08001944 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
1946 info->ldisc_stop_rx = 1;
1947 if (I_IXOFF(tty)) {
Jiri Slaby1c456072008-02-07 00:16:46 -08001948 if (info->board->chip_flag) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 info->IER &= ~MOXA_MUST_RECV_ISR;
Jiri Slaby1c456072008-02-07 00:16:46 -08001950 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 info->x_char = STOP_CHAR(tty);
Jiri Slaby1c456072008-02-07 00:16:46 -08001953 outb(0, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 info->IER |= UART_IER_THRI;
Jiri Slaby1c456072008-02-07 00:16:46 -08001955 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 }
1957 }
1958
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001959 if (info->port.tty->termios->c_cflag & CRTSCTS) {
Jiri Slaby1c456072008-02-07 00:16:46 -08001960 info->MCR &= ~UART_MCR_RTS;
1961 outb(info->MCR, info->ioaddr + UART_MCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 }
1963}
1964
1965/*
1966 * This routine is called by the upper-layer tty layer to signal that
1967 * incoming characters should be throttled.
1968 */
1969static void mxser_throttle(struct tty_struct *tty)
1970{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 mxser_stoprx(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972}
1973
1974static void mxser_unthrottle(struct tty_struct *tty)
1975{
Jiri Slaby1c456072008-02-07 00:16:46 -08001976 struct mxser_port *info = tty->driver_data;
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07001977
Jiri Slaby1c456072008-02-07 00:16:46 -08001978 /* startrx */
1979 info->ldisc_stop_rx = 0;
1980 if (I_IXOFF(tty)) {
1981 if (info->x_char)
1982 info->x_char = 0;
1983 else {
1984 if (info->board->chip_flag) {
1985 info->IER |= MOXA_MUST_RECV_ISR;
1986 outb(info->IER, info->ioaddr + UART_IER);
1987 } else {
1988 info->x_char = START_CHAR(tty);
1989 outb(0, info->ioaddr + UART_IER);
1990 info->IER |= UART_IER_THRI;
1991 outb(info->IER, info->ioaddr + UART_IER);
1992 }
1993 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 }
1995
Alan Cox0ad9e7d2008-07-16 21:56:10 +01001996 if (info->port.tty->termios->c_cflag & CRTSCTS) {
Jiri Slaby1c456072008-02-07 00:16:46 -08001997 info->MCR |= UART_MCR_RTS;
1998 outb(info->MCR, info->ioaddr + UART_MCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 }
2000}
2001
2002/*
2003 * mxser_stop() and mxser_start()
2004 *
2005 * This routines are called before setting or resetting tty->stopped.
2006 * They enable or disable transmitter interrupts, as necessary.
2007 */
2008static void mxser_stop(struct tty_struct *tty)
2009{
Jiri Slaby1c456072008-02-07 00:16:46 -08002010 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 unsigned long flags;
2012
2013 spin_lock_irqsave(&info->slock, flags);
2014 if (info->IER & UART_IER_THRI) {
2015 info->IER &= ~UART_IER_THRI;
Jiri Slaby1c456072008-02-07 00:16:46 -08002016 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 }
2018 spin_unlock_irqrestore(&info->slock, flags);
2019}
2020
2021static void mxser_start(struct tty_struct *tty)
2022{
Jiri Slaby1c456072008-02-07 00:16:46 -08002023 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 unsigned long flags;
2025
2026 spin_lock_irqsave(&info->slock, flags);
Alan Cox0ad9e7d2008-07-16 21:56:10 +01002027 if (info->xmit_cnt && info->port.xmit_buf) {
Jiri Slaby1c456072008-02-07 00:16:46 -08002028 outb(info->IER & ~UART_IER_THRI, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 info->IER |= UART_IER_THRI;
Jiri Slaby1c456072008-02-07 00:16:46 -08002030 outb(info->IER, info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 }
2032 spin_unlock_irqrestore(&info->slock, flags);
2033}
2034
Jiri Slaby1c456072008-02-07 00:16:46 -08002035static void mxser_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
2036{
2037 struct mxser_port *info = tty->driver_data;
2038 unsigned long flags;
2039
2040 spin_lock_irqsave(&info->slock, flags);
2041 mxser_change_speed(info, old_termios);
2042 spin_unlock_irqrestore(&info->slock, flags);
2043
2044 if ((old_termios->c_cflag & CRTSCTS) &&
2045 !(tty->termios->c_cflag & CRTSCTS)) {
2046 tty->hw_stopped = 0;
2047 mxser_start(tty);
2048 }
2049
2050 /* Handle sw stopped */
2051 if ((old_termios->c_iflag & IXON) &&
2052 !(tty->termios->c_iflag & IXON)) {
2053 tty->stopped = 0;
2054
2055 if (info->board->chip_flag) {
2056 spin_lock_irqsave(&info->slock, flags);
Christoph Hellwig148ff862008-04-30 00:54:29 -07002057 mxser_disable_must_rx_software_flow_control(
2058 info->ioaddr);
Jiri Slaby1c456072008-02-07 00:16:46 -08002059 spin_unlock_irqrestore(&info->slock, flags);
2060 }
2061
2062 mxser_start(tty);
2063 }
2064}
2065
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066/*
2067 * mxser_wait_until_sent() --- wait until the transmitter is empty
2068 */
2069static void mxser_wait_until_sent(struct tty_struct *tty, int timeout)
2070{
Jiri Slaby1c456072008-02-07 00:16:46 -08002071 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 unsigned long orig_jiffies, char_time;
2073 int lsr;
2074
2075 if (info->type == PORT_UNKNOWN)
2076 return;
2077
2078 if (info->xmit_fifo_size == 0)
2079 return; /* Just in case.... */
2080
2081 orig_jiffies = jiffies;
2082 /*
2083 * Set the check interval to be 1/5 of the estimated time to
2084 * send a single character, and make it at least 1. The check
2085 * interval should also be less than the timeout.
2086 *
2087 * Note: we have to use pretty tight timings here to satisfy
2088 * the NIST-PCTS.
2089 */
2090 char_time = (info->timeout - HZ / 50) / info->xmit_fifo_size;
2091 char_time = char_time / 5;
2092 if (char_time == 0)
2093 char_time = 1;
2094 if (timeout && timeout < char_time)
2095 char_time = timeout;
2096 /*
2097 * If the transmitter hasn't cleared in twice the approximate
2098 * amount of time to send the entire FIFO, it probably won't
2099 * ever clear. This assumes the UART isn't doing flow
2100 * control, which is currently the case. Hence, if it ever
2101 * takes longer than info->timeout, this is probably due to a
2102 * UART bug of some kind. So, we clamp the timeout parameter at
2103 * 2*info->timeout.
2104 */
2105 if (!timeout || timeout > 2 * info->timeout)
2106 timeout = 2 * info->timeout;
2107#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002108 printk(KERN_DEBUG "In rs_wait_until_sent(%d) check=%lu...",
2109 timeout, char_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 printk("jiff=%lu...", jiffies);
2111#endif
Alan Cox978e5952008-04-30 00:53:59 -07002112 lock_kernel();
Jiri Slaby1c456072008-02-07 00:16:46 -08002113 while (!((lsr = inb(info->ioaddr + UART_LSR)) & UART_LSR_TEMT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2115 printk("lsr = %d (jiff=%lu)...", lsr, jiffies);
2116#endif
Nishanth Aravamudanda4cd8d2005-09-10 00:27:30 -07002117 schedule_timeout_interruptible(char_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 if (signal_pending(current))
2119 break;
2120 if (timeout && time_after(jiffies, orig_jiffies + timeout))
2121 break;
2122 }
2123 set_current_state(TASK_RUNNING);
Alan Cox978e5952008-04-30 00:53:59 -07002124 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125
2126#ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
2127 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
2128#endif
2129}
2130
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131/*
2132 * This routine is called by tty_hangup() when a hangup is signaled.
2133 */
Jiri Slaby1c456072008-02-07 00:16:46 -08002134static void mxser_hangup(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135{
Jiri Slaby1c456072008-02-07 00:16:46 -08002136 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137
2138 mxser_flush_buffer(tty);
2139 mxser_shutdown(info);
Alan Cox0ad9e7d2008-07-16 21:56:10 +01002140 info->port.count = 0;
2141 info->port.flags &= ~ASYNC_NORMAL_ACTIVE;
2142 info->port.tty = NULL;
2143 wake_up_interruptible(&info->port.open_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144}
2145
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146/*
2147 * mxser_rs_break() --- routine which turns the break handling on or off
2148 */
Alan Cox9e989662008-07-22 11:18:03 +01002149static int mxser_rs_break(struct tty_struct *tty, int break_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150{
Jiri Slaby1c456072008-02-07 00:16:46 -08002151 struct mxser_port *info = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 unsigned long flags;
2153
2154 spin_lock_irqsave(&info->slock, flags);
2155 if (break_state == -1)
Jiri Slaby1c456072008-02-07 00:16:46 -08002156 outb(inb(info->ioaddr + UART_LCR) | UART_LCR_SBC,
2157 info->ioaddr + UART_LCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 else
Jiri Slaby1c456072008-02-07 00:16:46 -08002159 outb(inb(info->ioaddr + UART_LCR) & ~UART_LCR_SBC,
2160 info->ioaddr + UART_LCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 spin_unlock_irqrestore(&info->slock, flags);
Alan Cox9e989662008-07-22 11:18:03 +01002162 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163}
2164
Jiri Slaby1c456072008-02-07 00:16:46 -08002165static void mxser_receive_chars(struct mxser_port *port, int *status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166{
Alan Cox0ad9e7d2008-07-16 21:56:10 +01002167 struct tty_struct *tty = port->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 unsigned char ch, gdl;
2169 int ignored = 0;
2170 int cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 int recv_room;
2172 int max = 256;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173
Alan Cox33f0f882006-01-09 20:54:13 -08002174 recv_room = tty->receive_room;
Jiri Slaby1c456072008-02-07 00:16:46 -08002175 if ((recv_room == 0) && (!port->ldisc_stop_rx))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 mxser_stoprx(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177
Jiri Slaby1c456072008-02-07 00:16:46 -08002178 if (port->board->chip_flag != MOXA_OTHER_UART) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179
Jiri Slaby1c456072008-02-07 00:16:46 -08002180 if (*status & UART_LSR_SPECIAL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 goto intr_old;
Jiri Slaby1c456072008-02-07 00:16:46 -08002182 if (port->board->chip_flag == MOXA_MUST_MU860_HWID &&
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002183 (*status & MOXA_MUST_LSR_RERR))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 goto intr_old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 if (*status & MOXA_MUST_LSR_RERR)
2186 goto intr_old;
2187
Jiri Slaby1c456072008-02-07 00:16:46 -08002188 gdl = inb(port->ioaddr + MOXA_MUST_GDL_REGISTER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189
Jiri Slaby1c456072008-02-07 00:16:46 -08002190 if (port->board->chip_flag == MOXA_MUST_MU150_HWID)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 gdl &= MOXA_MUST_GDL_MASK;
2192 if (gdl >= recv_room) {
Jiri Slaby1c456072008-02-07 00:16:46 -08002193 if (!port->ldisc_stop_rx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 mxser_stoprx(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 }
2196 while (gdl--) {
Jiri Slaby1c456072008-02-07 00:16:46 -08002197 ch = inb(port->ioaddr + UART_RX);
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002198 tty_insert_flip_char(tty, ch, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 cnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 }
2201 goto end_intr;
2202 }
Jiri Slaby1c456072008-02-07 00:16:46 -08002203intr_old:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204
2205 do {
2206 if (max-- < 0)
2207 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208
Jiri Slaby1c456072008-02-07 00:16:46 -08002209 ch = inb(port->ioaddr + UART_RX);
2210 if (port->board->chip_flag && (*status & UART_LSR_OE))
2211 outb(0x23, port->ioaddr + UART_FCR);
2212 *status &= port->read_status_mask;
2213 if (*status & port->ignore_status_mask) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 if (++ignored > 100)
2215 break;
2216 } else {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002217 char flag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 if (*status & UART_LSR_SPECIAL) {
2219 if (*status & UART_LSR_BI) {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002220 flag = TTY_BREAK;
Jiri Slaby1c456072008-02-07 00:16:46 -08002221 port->icount.brk++;
2222
Alan Cox0ad9e7d2008-07-16 21:56:10 +01002223 if (port->port.flags & ASYNC_SAK)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 do_SAK(tty);
2225 } else if (*status & UART_LSR_PE) {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002226 flag = TTY_PARITY;
Jiri Slaby1c456072008-02-07 00:16:46 -08002227 port->icount.parity++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 } else if (*status & UART_LSR_FE) {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002229 flag = TTY_FRAME;
Jiri Slaby1c456072008-02-07 00:16:46 -08002230 port->icount.frame++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 } else if (*status & UART_LSR_OE) {
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002232 flag = TTY_OVERRUN;
Jiri Slaby1c456072008-02-07 00:16:46 -08002233 port->icount.overrun++;
2234 } else
2235 flag = TTY_BREAK;
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002236 }
2237 tty_insert_flip_char(tty, ch, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 cnt++;
2239 if (cnt >= recv_room) {
Jiri Slaby1c456072008-02-07 00:16:46 -08002240 if (!port->ldisc_stop_rx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 mxser_stoprx(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 break;
2243 }
2244
2245 }
2246
Jiri Slaby1c456072008-02-07 00:16:46 -08002247 if (port->board->chip_flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249
Jiri Slaby1c456072008-02-07 00:16:46 -08002250 *status = inb(port->ioaddr + UART_LSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 } while (*status & UART_LSR_DR);
2252
Jiri Slaby1c456072008-02-07 00:16:46 -08002253end_intr:
Alan Cox0ad9e7d2008-07-16 21:56:10 +01002254 mxvar_log.rxcnt[port->port.tty->index] += cnt;
Jiri Slaby1c456072008-02-07 00:16:46 -08002255 port->mon_data.rxcnt += cnt;
2256 port->mon_data.up_rxcnt += cnt;
Denis Vlasenko3399ba52005-06-06 13:35:55 -07002257
Jiri Slaby1c456072008-02-07 00:16:46 -08002258 /*
2259 * We are called from an interrupt context with &port->slock
2260 * being held. Drop it temporarily in order to prevent
2261 * recursive locking.
2262 */
2263 spin_unlock(&port->slock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 tty_flip_buffer_push(tty);
Jiri Slaby1c456072008-02-07 00:16:46 -08002265 spin_lock(&port->slock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266}
2267
Jiri Slaby1c456072008-02-07 00:16:46 -08002268static void mxser_transmit_chars(struct mxser_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269{
2270 int count, cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271
Jiri Slaby1c456072008-02-07 00:16:46 -08002272 if (port->x_char) {
2273 outb(port->x_char, port->ioaddr + UART_TX);
2274 port->x_char = 0;
Alan Cox0ad9e7d2008-07-16 21:56:10 +01002275 mxvar_log.txcnt[port->port.tty->index]++;
Jiri Slaby1c456072008-02-07 00:16:46 -08002276 port->mon_data.txcnt++;
2277 port->mon_data.up_txcnt++;
2278 port->icount.tx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 return;
2280 }
2281
Alan Cox0ad9e7d2008-07-16 21:56:10 +01002282 if (port->port.xmit_buf == NULL)
Jiri Slaby1c456072008-02-07 00:16:46 -08002283 return;
2284
Alan Cox0ad9e7d2008-07-16 21:56:10 +01002285 if ((port->xmit_cnt <= 0) || port->port.tty->stopped ||
2286 (port->port.tty->hw_stopped &&
Jiri Slaby1c456072008-02-07 00:16:46 -08002287 (port->type != PORT_16550A) &&
2288 (!port->board->chip_flag))) {
2289 port->IER &= ~UART_IER_THRI;
2290 outb(port->IER, port->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 return;
2292 }
2293
Jiri Slaby1c456072008-02-07 00:16:46 -08002294 cnt = port->xmit_cnt;
2295 count = port->xmit_fifo_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 do {
Alan Cox0ad9e7d2008-07-16 21:56:10 +01002297 outb(port->port.xmit_buf[port->xmit_tail++],
Jiri Slaby1c456072008-02-07 00:16:46 -08002298 port->ioaddr + UART_TX);
2299 port->xmit_tail = port->xmit_tail & (SERIAL_XMIT_SIZE - 1);
2300 if (--port->xmit_cnt <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 break;
2302 } while (--count > 0);
Alan Cox0ad9e7d2008-07-16 21:56:10 +01002303 mxvar_log.txcnt[port->port.tty->index] += (cnt - port->xmit_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304
Jiri Slaby1c456072008-02-07 00:16:46 -08002305 port->mon_data.txcnt += (cnt - port->xmit_cnt);
2306 port->mon_data.up_txcnt += (cnt - port->xmit_cnt);
2307 port->icount.tx += (cnt - port->xmit_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308
Jiri Slaby1c456072008-02-07 00:16:46 -08002309 if (port->xmit_cnt < WAKEUP_CHARS)
Alan Cox0ad9e7d2008-07-16 21:56:10 +01002310 tty_wakeup(port->port.tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311
Jiri Slaby1c456072008-02-07 00:16:46 -08002312 if (port->xmit_cnt <= 0) {
2313 port->IER &= ~UART_IER_THRI;
2314 outb(port->IER, port->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316}
2317
2318/*
Jiri Slaby1c456072008-02-07 00:16:46 -08002319 * This is the serial driver's generic interrupt routine
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 */
Jiri Slaby1c456072008-02-07 00:16:46 -08002321static irqreturn_t mxser_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322{
Jiri Slaby1c456072008-02-07 00:16:46 -08002323 int status, iir, i;
2324 struct mxser_board *brd = NULL;
2325 struct mxser_port *port;
2326 int max, irqbits, bits, msr;
2327 unsigned int int_cnt, pass_counter = 0;
2328 int handled = IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329
Jiri Slaby1c456072008-02-07 00:16:46 -08002330 for (i = 0; i < MXSER_BOARDS; i++)
2331 if (dev_id == &mxser_boards[i]) {
2332 brd = dev_id;
2333 break;
2334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335
Jiri Slaby1c456072008-02-07 00:16:46 -08002336 if (i == MXSER_BOARDS)
2337 goto irq_stop;
2338 if (brd == NULL)
2339 goto irq_stop;
2340 max = brd->info->nports;
2341 while (pass_counter++ < MXSER_ISR_PASS_LIMIT) {
2342 irqbits = inb(brd->vector) & brd->vector_mask;
2343 if (irqbits == brd->vector_mask)
2344 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345
Jiri Slaby1c456072008-02-07 00:16:46 -08002346 handled = IRQ_HANDLED;
2347 for (i = 0, bits = 1; i < max; i++, irqbits |= bits, bits <<= 1) {
2348 if (irqbits == brd->vector_mask)
2349 break;
2350 if (bits & irqbits)
2351 continue;
2352 port = &brd->ports[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353
Jiri Slaby1c456072008-02-07 00:16:46 -08002354 int_cnt = 0;
2355 spin_lock(&port->slock);
2356 do {
2357 iir = inb(port->ioaddr + UART_IIR);
2358 if (iir & UART_IIR_NO_INT)
2359 break;
2360 iir &= MOXA_MUST_IIR_MASK;
Alan Cox0ad9e7d2008-07-16 21:56:10 +01002361 if (!port->port.tty ||
2362 (port->port.flags & ASYNC_CLOSING) ||
2363 !(port->port.flags &
Jiri Slaby1c456072008-02-07 00:16:46 -08002364 ASYNC_INITIALIZED)) {
2365 status = inb(port->ioaddr + UART_LSR);
2366 outb(0x27, port->ioaddr + UART_FCR);
2367 inb(port->ioaddr + UART_MSR);
2368 break;
2369 }
2370
2371 status = inb(port->ioaddr + UART_LSR);
2372
2373 if (status & UART_LSR_PE)
2374 port->err_shadow |= NPPI_NOTIFY_PARITY;
2375 if (status & UART_LSR_FE)
2376 port->err_shadow |= NPPI_NOTIFY_FRAMING;
2377 if (status & UART_LSR_OE)
2378 port->err_shadow |=
2379 NPPI_NOTIFY_HW_OVERRUN;
2380 if (status & UART_LSR_BI)
2381 port->err_shadow |= NPPI_NOTIFY_BREAK;
2382
2383 if (port->board->chip_flag) {
2384 if (iir == MOXA_MUST_IIR_GDA ||
2385 iir == MOXA_MUST_IIR_RDA ||
2386 iir == MOXA_MUST_IIR_RTO ||
2387 iir == MOXA_MUST_IIR_LSR)
2388 mxser_receive_chars(port,
2389 &status);
2390
2391 } else {
2392 status &= port->read_status_mask;
2393 if (status & UART_LSR_DR)
2394 mxser_receive_chars(port,
2395 &status);
2396 }
2397 msr = inb(port->ioaddr + UART_MSR);
2398 if (msr & UART_MSR_ANY_DELTA)
2399 mxser_check_modem_status(port, msr);
2400
2401 if (port->board->chip_flag) {
2402 if (iir == 0x02 && (status &
2403 UART_LSR_THRE))
2404 mxser_transmit_chars(port);
2405 } else {
2406 if (status & UART_LSR_THRE)
2407 mxser_transmit_chars(port);
2408 }
2409 } while (int_cnt++ < MXSER_ISR_PASS_LIMIT);
2410 spin_unlock(&port->slock);
2411 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 }
2413
Jiri Slaby1c456072008-02-07 00:16:46 -08002414irq_stop:
2415 return handled;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416}
2417
Jiri Slaby1c456072008-02-07 00:16:46 -08002418static const struct tty_operations mxser_ops = {
2419 .open = mxser_open,
2420 .close = mxser_close,
2421 .write = mxser_write,
2422 .put_char = mxser_put_char,
2423 .flush_chars = mxser_flush_chars,
2424 .write_room = mxser_write_room,
2425 .chars_in_buffer = mxser_chars_in_buffer,
2426 .flush_buffer = mxser_flush_buffer,
2427 .ioctl = mxser_ioctl,
2428 .throttle = mxser_throttle,
2429 .unthrottle = mxser_unthrottle,
2430 .set_termios = mxser_set_termios,
2431 .stop = mxser_stop,
2432 .start = mxser_start,
2433 .hangup = mxser_hangup,
2434 .break_ctl = mxser_rs_break,
2435 .wait_until_sent = mxser_wait_until_sent,
2436 .tiocmget = mxser_tiocmget,
2437 .tiocmset = mxser_tiocmset,
2438};
2439
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440/*
Jiri Slaby1c456072008-02-07 00:16:46 -08002441 * The MOXA Smartio/Industio serial driver boot-time initialization code!
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 */
Jiri Slaby1c456072008-02-07 00:16:46 -08002443
2444static void mxser_release_res(struct mxser_board *brd, struct pci_dev *pdev,
2445 unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446{
Jiri Slaby1c456072008-02-07 00:16:46 -08002447 if (irq)
2448 free_irq(brd->irq, brd);
2449 if (pdev != NULL) { /* PCI */
2450#ifdef CONFIG_PCI
2451 pci_release_region(pdev, 2);
2452 pci_release_region(pdev, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 } else {
Jiri Slaby1c456072008-02-07 00:16:46 -08002455 release_region(brd->ports[0].ioaddr, 8 * brd->info->nports);
2456 release_region(brd->vector, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458}
2459
Jiri Slaby1c456072008-02-07 00:16:46 -08002460static int __devinit mxser_initbrd(struct mxser_board *brd,
2461 struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462{
Jiri Slaby1c456072008-02-07 00:16:46 -08002463 struct mxser_port *info;
2464 unsigned int i;
2465 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466
Jiri Slaby83766bc2008-07-25 01:48:21 -07002467 printk(KERN_INFO "mxser: max. baud rate = %d bps\n",
2468 brd->ports[0].max_baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469
Jiri Slaby1c456072008-02-07 00:16:46 -08002470 for (i = 0; i < brd->info->nports; i++) {
2471 info = &brd->ports[i];
Alan Cox44b7d1b2008-07-16 21:57:18 +01002472 tty_port_init(&info->port);
Jiri Slaby1c456072008-02-07 00:16:46 -08002473 info->board = brd;
2474 info->stop_rx = 0;
2475 info->ldisc_stop_rx = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476
Jiri Slaby1c456072008-02-07 00:16:46 -08002477 /* Enhance mode enabled here */
2478 if (brd->chip_flag != MOXA_OTHER_UART)
Christoph Hellwig148ff862008-04-30 00:54:29 -07002479 mxser_enable_must_enchance_mode(info->ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480
Alan Cox0ad9e7d2008-07-16 21:56:10 +01002481 info->port.flags = ASYNC_SHARE_IRQ;
Jiri Slaby1c456072008-02-07 00:16:46 -08002482 info->type = brd->uart_type;
2483
2484 process_txrx_fifo(info);
2485
2486 info->custom_divisor = info->baud_base * 16;
Alan Cox44b7d1b2008-07-16 21:57:18 +01002487 info->port.close_delay = 5 * HZ / 10;
2488 info->port.closing_wait = 30 * HZ;
Jiri Slaby1c456072008-02-07 00:16:46 -08002489 info->normal_termios = mxvar_sdriver->init_termios;
Jiri Slaby1c456072008-02-07 00:16:46 -08002490 init_waitqueue_head(&info->delta_msr_wait);
2491 memset(&info->mon_data, 0, sizeof(struct mxser_mon));
2492 info->err_shadow = 0;
2493 spin_lock_init(&info->slock);
2494
2495 /* before set INT ISR, disable all int */
2496 outb(inb(info->ioaddr + UART_IER) & 0xf0,
2497 info->ioaddr + UART_IER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 }
2499
Jiri Slaby1c456072008-02-07 00:16:46 -08002500 retval = request_irq(brd->irq, mxser_interrupt, IRQF_SHARED, "mxser",
2501 brd);
2502 if (retval) {
2503 printk(KERN_ERR "Board %s: Request irq failed, IRQ (%d) may "
2504 "conflict with another device.\n",
2505 brd->info->name, brd->irq);
2506 /* We hold resources, we need to release them. */
2507 mxser_release_res(brd, pdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 }
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002509 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510}
2511
Jiri Slaby1c456072008-02-07 00:16:46 -08002512static int __init mxser_get_ISA_conf(int cap, struct mxser_board *brd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513{
2514 int id, i, bits;
2515 unsigned short regs[16], irq;
2516 unsigned char scratch, scratch2;
2517
Jiri Slaby1c456072008-02-07 00:16:46 -08002518 brd->chip_flag = MOXA_OTHER_UART;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519
2520 id = mxser_read_register(cap, regs);
Jiri Slaby1c456072008-02-07 00:16:46 -08002521 switch (id) {
2522 case C168_ASIC_ID:
2523 brd->info = &mxser_cards[0];
2524 break;
2525 case C104_ASIC_ID:
2526 brd->info = &mxser_cards[1];
2527 break;
2528 case CI104J_ASIC_ID:
2529 brd->info = &mxser_cards[2];
2530 break;
2531 case C102_ASIC_ID:
2532 brd->info = &mxser_cards[5];
2533 break;
2534 case CI132_ASIC_ID:
2535 brd->info = &mxser_cards[6];
2536 break;
2537 case CI134_ASIC_ID:
2538 brd->info = &mxser_cards[7];
2539 break;
2540 default:
Jesper Juhl8ea2c2e2006-06-25 05:47:54 -07002541 return 0;
Jiri Slaby1c456072008-02-07 00:16:46 -08002542 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543
2544 irq = 0;
Jiri Slaby1c456072008-02-07 00:16:46 -08002545 /* some ISA cards have 2 ports, but we want to see them as 4-port (why?)
2546 Flag-hack checks if configuration should be read as 2-port here. */
2547 if (brd->info->nports == 2 || (brd->info->flags & MXSER_HAS2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 irq = regs[9] & 0xF000;
2549 irq = irq | (irq >> 4);
2550 if (irq != (regs[9] & 0xFF00))
Jiri Slaby83766bc2008-07-25 01:48:21 -07002551 goto err_irqconflict;
Jiri Slaby1c456072008-02-07 00:16:46 -08002552 } else if (brd->info->nports == 4) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 irq = regs[9] & 0xF000;
2554 irq = irq | (irq >> 4);
2555 irq = irq | (irq >> 8);
2556 if (irq != regs[9])
Jiri Slaby83766bc2008-07-25 01:48:21 -07002557 goto err_irqconflict;
Jiri Slaby1c456072008-02-07 00:16:46 -08002558 } else if (brd->info->nports == 8) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 irq = regs[9] & 0xF000;
2560 irq = irq | (irq >> 4);
2561 irq = irq | (irq >> 8);
2562 if ((irq != regs[9]) || (irq != regs[10]))
Jiri Slaby83766bc2008-07-25 01:48:21 -07002563 goto err_irqconflict;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 }
2565
Jiri Slaby83766bc2008-07-25 01:48:21 -07002566 if (!irq) {
2567 printk(KERN_ERR "mxser: interrupt number unset\n");
2568 return -EIO;
2569 }
Jiri Slaby1c456072008-02-07 00:16:46 -08002570 brd->irq = ((int)(irq & 0xF000) >> 12);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 for (i = 0; i < 8; i++)
Jiri Slaby1c456072008-02-07 00:16:46 -08002572 brd->ports[i].ioaddr = (int) regs[i + 1] & 0xFFF8;
Jiri Slaby83766bc2008-07-25 01:48:21 -07002573 if ((regs[12] & 0x80) == 0) {
2574 printk(KERN_ERR "mxser: invalid interrupt vector\n");
2575 return -EIO;
2576 }
Jiri Slaby1c456072008-02-07 00:16:46 -08002577 brd->vector = (int)regs[11]; /* interrupt vector */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 if (id == 1)
Jiri Slaby1c456072008-02-07 00:16:46 -08002579 brd->vector_mask = 0x00FF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 else
Jiri Slaby1c456072008-02-07 00:16:46 -08002581 brd->vector_mask = 0x000F;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 for (i = 7, bits = 0x0100; i >= 0; i--, bits <<= 1) {
2583 if (regs[12] & bits) {
Jiri Slaby1c456072008-02-07 00:16:46 -08002584 brd->ports[i].baud_base = 921600;
2585 brd->ports[i].max_baud = 921600;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 } else {
Jiri Slaby1c456072008-02-07 00:16:46 -08002587 brd->ports[i].baud_base = 115200;
2588 brd->ports[i].max_baud = 115200;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 }
2590 }
2591 scratch2 = inb(cap + UART_LCR) & (~UART_LCR_DLAB);
2592 outb(scratch2 | UART_LCR_DLAB, cap + UART_LCR);
2593 outb(0, cap + UART_EFR); /* EFR is the same as FCR */
2594 outb(scratch2, cap + UART_LCR);
2595 outb(UART_FCR_ENABLE_FIFO, cap + UART_FCR);
2596 scratch = inb(cap + UART_IIR);
2597
2598 if (scratch & 0xC0)
Jiri Slaby1c456072008-02-07 00:16:46 -08002599 brd->uart_type = PORT_16550A;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 else
Jiri Slaby1c456072008-02-07 00:16:46 -08002601 brd->uart_type = PORT_16450;
2602 if (!request_region(brd->ports[0].ioaddr, 8 * brd->info->nports,
Jiri Slaby83766bc2008-07-25 01:48:21 -07002603 "mxser(IO)")) {
2604 printk(KERN_ERR "mxser: can't request ports I/O region: "
2605 "0x%.8lx-0x%.8lx\n",
2606 brd->ports[0].ioaddr, brd->ports[0].ioaddr +
2607 8 * brd->info->nports - 1);
2608 return -EIO;
2609 }
Jiri Slaby1c456072008-02-07 00:16:46 -08002610 if (!request_region(brd->vector, 1, "mxser(vector)")) {
2611 release_region(brd->ports[0].ioaddr, 8 * brd->info->nports);
Jiri Slaby83766bc2008-07-25 01:48:21 -07002612 printk(KERN_ERR "mxser: can't request interrupt vector region: "
2613 "0x%.8lx-0x%.8lx\n",
2614 brd->ports[0].ioaddr, brd->ports[0].ioaddr +
2615 8 * brd->info->nports - 1);
2616 return -EIO;
Jiri Slaby1c456072008-02-07 00:16:46 -08002617 }
2618 return brd->info->nports;
Jiri Slaby83766bc2008-07-25 01:48:21 -07002619
2620err_irqconflict:
2621 printk(KERN_ERR "mxser: invalid interrupt number\n");
2622 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623}
2624
Jiri Slaby1c456072008-02-07 00:16:46 -08002625static int __devinit mxser_probe(struct pci_dev *pdev,
2626 const struct pci_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627{
Jiri Slaby1c456072008-02-07 00:16:46 -08002628#ifdef CONFIG_PCI
2629 struct mxser_board *brd;
2630 unsigned int i, j;
2631 unsigned long ioaddress;
2632 int retval = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633
Jiri Slaby1c456072008-02-07 00:16:46 -08002634 for (i = 0; i < MXSER_BOARDS; i++)
2635 if (mxser_boards[i].info == NULL)
2636 break;
2637
2638 if (i >= MXSER_BOARDS) {
Jiri Slaby83766bc2008-07-25 01:48:21 -07002639 dev_err(&pdev->dev, "too many boards found (maximum %d), board "
2640 "not configured\n", MXSER_BOARDS);
Jiri Slaby1c456072008-02-07 00:16:46 -08002641 goto err;
2642 }
2643
2644 brd = &mxser_boards[i];
2645 brd->idx = i * MXSER_PORTS_PER_BOARD;
Jiri Slaby83766bc2008-07-25 01:48:21 -07002646 dev_info(&pdev->dev, "found MOXA %s board (BusNo=%d, DevNo=%d)\n",
Jiri Slaby1c456072008-02-07 00:16:46 -08002647 mxser_cards[ent->driver_data].name,
2648 pdev->bus->number, PCI_SLOT(pdev->devfn));
2649
2650 retval = pci_enable_device(pdev);
2651 if (retval) {
Jiri Slaby83766bc2008-07-25 01:48:21 -07002652 dev_err(&pdev->dev, "PCI enable failed\n");
Jiri Slaby1c456072008-02-07 00:16:46 -08002653 goto err;
2654 }
2655
2656 /* io address */
2657 ioaddress = pci_resource_start(pdev, 2);
2658 retval = pci_request_region(pdev, 2, "mxser(IO)");
2659 if (retval)
2660 goto err;
2661
2662 brd->info = &mxser_cards[ent->driver_data];
2663 for (i = 0; i < brd->info->nports; i++)
2664 brd->ports[i].ioaddr = ioaddress + 8 * i;
2665
2666 /* vector */
2667 ioaddress = pci_resource_start(pdev, 3);
2668 retval = pci_request_region(pdev, 3, "mxser(vector)");
2669 if (retval)
2670 goto err_relio;
2671 brd->vector = ioaddress;
2672
2673 /* irq */
2674 brd->irq = pdev->irq;
2675
2676 brd->chip_flag = CheckIsMoxaMust(brd->ports[0].ioaddr);
2677 brd->uart_type = PORT_16550A;
2678 brd->vector_mask = 0;
2679
2680 for (i = 0; i < brd->info->nports; i++) {
2681 for (j = 0; j < UART_INFO_NUM; j++) {
2682 if (Gpci_uart_info[j].type == brd->chip_flag) {
2683 brd->ports[i].max_baud =
2684 Gpci_uart_info[j].max_baud;
2685
2686 /* exception....CP-102 */
2687 if (brd->info->flags & MXSER_HIGHBAUD)
2688 brd->ports[i].max_baud = 921600;
2689 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 }
2691 }
Jiri Slaby1c456072008-02-07 00:16:46 -08002692 }
2693
2694 if (brd->chip_flag == MOXA_MUST_MU860_HWID) {
2695 for (i = 0; i < brd->info->nports; i++) {
2696 if (i < 4)
2697 brd->ports[i].opmode_ioaddr = ioaddress + 4;
2698 else
2699 brd->ports[i].opmode_ioaddr = ioaddress + 0x0c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 }
Jiri Slaby1c456072008-02-07 00:16:46 -08002701 outb(0, ioaddress + 4); /* default set to RS232 mode */
2702 outb(0, ioaddress + 0x0c); /* default set to RS232 mode */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 }
Jiri Slaby1c456072008-02-07 00:16:46 -08002704
2705 for (i = 0; i < brd->info->nports; i++) {
2706 brd->vector_mask |= (1 << i);
2707 brd->ports[i].baud_base = 921600;
2708 }
2709
2710 /* mxser_initbrd will hook ISR. */
2711 retval = mxser_initbrd(brd, pdev);
2712 if (retval)
2713 goto err_null;
2714
2715 for (i = 0; i < brd->info->nports; i++)
2716 tty_register_device(mxvar_sdriver, brd->idx + i, &pdev->dev);
2717
2718 pci_set_drvdata(pdev, brd);
2719
2720 return 0;
2721err_relio:
2722 pci_release_region(pdev, 2);
2723err_null:
2724 brd->info = NULL;
2725err:
2726 return retval;
2727#else
2728 return -ENODEV;
2729#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730}
2731
Jiri Slaby1c456072008-02-07 00:16:46 -08002732static void __devexit mxser_remove(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733{
Jiri Slaby1c456072008-02-07 00:16:46 -08002734 struct mxser_board *brd = pci_get_drvdata(pdev);
2735 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736
Jiri Slaby1c456072008-02-07 00:16:46 -08002737 for (i = 0; i < brd->info->nports; i++)
2738 tty_unregister_device(mxvar_sdriver, brd->idx + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739
Jiri Slaby1c456072008-02-07 00:16:46 -08002740 mxser_release_res(brd, pdev, 1);
2741 brd->info = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742}
2743
Jiri Slaby1c456072008-02-07 00:16:46 -08002744static struct pci_driver mxser_driver = {
2745 .name = "mxser",
2746 .id_table = mxser_pcibrds,
2747 .probe = mxser_probe,
2748 .remove = __devexit_p(mxser_remove)
2749};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750
Jiri Slaby1c456072008-02-07 00:16:46 -08002751static int __init mxser_module_init(void)
2752{
2753 struct mxser_board *brd;
Jiri Slaby1df00922008-07-25 01:48:22 -07002754 unsigned int b, i, m;
2755 int retval;
Jiri Slaby1c456072008-02-07 00:16:46 -08002756
Jiri Slaby1c456072008-02-07 00:16:46 -08002757 mxvar_sdriver = alloc_tty_driver(MXSER_PORTS + 1);
2758 if (!mxvar_sdriver)
2759 return -ENOMEM;
2760
2761 printk(KERN_INFO "MOXA Smartio/Industio family driver version %s\n",
2762 MXSER_VERSION);
2763
2764 /* Initialize the tty_driver structure */
2765 mxvar_sdriver->owner = THIS_MODULE;
2766 mxvar_sdriver->magic = TTY_DRIVER_MAGIC;
2767 mxvar_sdriver->name = "ttyMI";
2768 mxvar_sdriver->major = ttymajor;
2769 mxvar_sdriver->minor_start = 0;
2770 mxvar_sdriver->num = MXSER_PORTS + 1;
2771 mxvar_sdriver->type = TTY_DRIVER_TYPE_SERIAL;
2772 mxvar_sdriver->subtype = SERIAL_TYPE_NORMAL;
2773 mxvar_sdriver->init_termios = tty_std_termios;
2774 mxvar_sdriver->init_termios.c_cflag = B9600|CS8|CREAD|HUPCL|CLOCAL;
2775 mxvar_sdriver->flags = TTY_DRIVER_REAL_RAW|TTY_DRIVER_DYNAMIC_DEV;
2776 tty_set_operations(mxvar_sdriver, &mxser_ops);
2777
2778 retval = tty_register_driver(mxvar_sdriver);
2779 if (retval) {
2780 printk(KERN_ERR "Couldn't install MOXA Smartio/Industio family "
2781 "tty driver !\n");
2782 goto err_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 }
Jiri Slaby1c456072008-02-07 00:16:46 -08002784
Jiri Slaby1c456072008-02-07 00:16:46 -08002785 /* Start finding ISA boards here */
Jiri Slaby1df00922008-07-25 01:48:22 -07002786 for (m = 0, b = 0; b < MXSER_BOARDS; b++) {
2787 if (!ioaddr[b])
2788 continue;
Jiri Slaby1c456072008-02-07 00:16:46 -08002789
Jiri Slaby1df00922008-07-25 01:48:22 -07002790 brd = &mxser_boards[m];
2791 retval = mxser_get_ISA_conf(!ioaddr[b], brd);
2792 if (retval <= 0) {
2793 brd->info = NULL;
2794 continue;
Jiri Slaby1c456072008-02-07 00:16:46 -08002795 }
2796
Jiri Slaby1df00922008-07-25 01:48:22 -07002797 printk(KERN_INFO "mxser: found MOXA %s board (CAP=0x%lx)\n",
2798 brd->info->name, ioaddr[b]);
2799
2800 /* mxser_initbrd will hook ISR. */
2801 if (mxser_initbrd(brd, NULL) < 0) {
2802 brd->info = NULL;
2803 continue;
2804 }
2805
2806 brd->idx = m * MXSER_PORTS_PER_BOARD;
2807 for (i = 0; i < brd->info->nports; i++)
2808 tty_register_device(mxvar_sdriver, brd->idx + i, NULL);
2809
2810 m++;
2811 }
2812
Jiri Slaby1c456072008-02-07 00:16:46 -08002813 retval = pci_register_driver(&mxser_driver);
2814 if (retval) {
Jiri Slaby83766bc2008-07-25 01:48:21 -07002815 printk(KERN_ERR "mxser: can't register pci driver\n");
Jiri Slaby1c456072008-02-07 00:16:46 -08002816 if (!m) {
2817 retval = -ENODEV;
2818 goto err_unr;
2819 } /* else: we have some ISA cards under control */
2820 }
2821
Jiri Slaby1c456072008-02-07 00:16:46 -08002822 return 0;
2823err_unr:
2824 tty_unregister_driver(mxvar_sdriver);
2825err_put:
2826 put_tty_driver(mxvar_sdriver);
2827 return retval;
2828}
2829
2830static void __exit mxser_module_exit(void)
2831{
2832 unsigned int i, j;
2833
Jiri Slaby1c456072008-02-07 00:16:46 -08002834 pci_unregister_driver(&mxser_driver);
2835
2836 for (i = 0; i < MXSER_BOARDS; i++) /* ISA remains */
2837 if (mxser_boards[i].info != NULL)
2838 for (j = 0; j < mxser_boards[i].info->nports; j++)
2839 tty_unregister_device(mxvar_sdriver,
2840 mxser_boards[i].idx + j);
2841 tty_unregister_driver(mxvar_sdriver);
2842 put_tty_driver(mxvar_sdriver);
2843
2844 for (i = 0; i < MXSER_BOARDS; i++)
2845 if (mxser_boards[i].info != NULL)
2846 mxser_release_res(&mxser_boards[i], NULL, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847}
2848
2849module_init(mxser_module_init);
2850module_exit(mxser_module_exit);