blob: a64707623e47ddb694b0244578ab59a04f72e7b6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*****************************************************************************/
2/*
3 * moxa.c -- MOXA Intellio family multiport serial driver.
4 *
5 * Copyright (C) 1999-2000 Moxa Technologies (support@moxa.com.tw).
6 *
7 * This code is loosely based on the Linux serial driver, written by
8 * Linus Torvalds, Theodore T'so and others.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 */
15
16/*
17 * MOXA Intellio Series Driver
18 * for : LINUX
19 * date : 1999/1/7
20 * version : 5.1
21 */
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/module.h>
24#include <linux/types.h>
25#include <linux/mm.h>
26#include <linux/ioport.h>
27#include <linux/errno.h>
Jiri Slaby03718232008-04-30 00:53:39 -070028#include <linux/firmware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/signal.h>
30#include <linux/sched.h>
31#include <linux/timer.h>
32#include <linux/interrupt.h>
33#include <linux/tty.h>
34#include <linux/tty_flip.h>
35#include <linux/major.h>
36#include <linux/string.h>
37#include <linux/fcntl.h>
38#include <linux/ptrace.h>
39#include <linux/serial.h>
40#include <linux/tty_driver.h>
41#include <linux/delay.h>
42#include <linux/pci.h>
43#include <linux/init.h>
44#include <linux/bitops.h>
Jiri Slaby95e07912007-10-18 03:06:25 -070045#include <linux/completion.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47#include <asm/system.h>
48#include <asm/io.h>
49#include <asm/uaccess.h>
50
Jiri Slaby03718232008-04-30 00:53:39 -070051#include "moxa.h"
52
Jiri Slaby11324ed2007-02-10 01:45:31 -080053#define MOXA_VERSION "5.1k"
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Jiri Slaby03718232008-04-30 00:53:39 -070055#define MOXA_FW_HDRLEN 32
56
Jiri Slaby11324ed2007-02-10 01:45:31 -080057#define MOXAMAJOR 172
58#define MOXACUMAJOR 173
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Jiri Slaby11324ed2007-02-10 01:45:31 -080060#define MAX_BOARDS 4 /* Don't change this value */
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#define MAX_PORTS_PER_BOARD 32 /* Don't change this value */
Jiri Slaby11324ed2007-02-10 01:45:31 -080062#define MAX_PORTS (MAX_BOARDS * MAX_PORTS_PER_BOARD)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64/*
65 * Define the Moxa PCI vendor and device IDs.
66 */
Jiri Slaby11324ed2007-02-10 01:45:31 -080067#define MOXA_BUS_TYPE_ISA 0
68#define MOXA_BUS_TYPE_PCI 1
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Linus Torvalds1da177e2005-04-16 15:20:36 -070070enum {
71 MOXA_BOARD_C218_PCI = 1,
72 MOXA_BOARD_C218_ISA,
73 MOXA_BOARD_C320_PCI,
74 MOXA_BOARD_C320_ISA,
75 MOXA_BOARD_CP204J,
76};
77
78static char *moxa_brdname[] =
79{
80 "C218 Turbo PCI series",
81 "C218 Turbo ISA series",
82 "C320 Turbo PCI series",
83 "C320 Turbo ISA series",
84 "CP-204J series",
85};
86
87#ifdef CONFIG_PCI
88static struct pci_device_id moxa_pcibrds[] = {
Jiri Slaby5ebb4072007-02-10 01:45:30 -080089 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C218),
90 .driver_data = MOXA_BOARD_C218_PCI },
91 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C320),
92 .driver_data = MOXA_BOARD_C320_PCI },
93 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP204J),
94 .driver_data = MOXA_BOARD_CP204J },
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 { 0 }
96};
97MODULE_DEVICE_TABLE(pci, moxa_pcibrds);
98#endif /* CONFIG_PCI */
99
Jiri Slaby03718232008-04-30 00:53:39 -0700100struct moxa_port;
101
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800102static struct moxa_board_conf {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 int boardType;
104 int numPorts;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 int busType;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800106
Jiri Slaby810ab092008-04-30 00:53:41 -0700107 unsigned int ready;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800108
Jiri Slaby03718232008-04-30 00:53:39 -0700109 struct moxa_port *ports;
110
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800111 void __iomem *basemem;
112 void __iomem *intNdx;
113 void __iomem *intPend;
114 void __iomem *intTable;
115} moxa_boards[MAX_BOARDS];
116
117struct mxser_mstatus {
118 tcflag_t cflag;
119 int cts;
120 int dsr;
121 int ri;
122 int dcd;
Jiri Slaby9dff89c2007-02-10 01:45:30 -0800123};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800125struct moxaq_str {
126 int inq;
127 int outq;
128};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800130struct moxa_port {
Jiri Slabyb4173f42008-04-30 00:53:40 -0700131 struct moxa_board_conf *board;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -0700132 struct tty_struct *tty;
133 void __iomem *tableAddr;
134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 int close_delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 int count;
138 int blocked_open;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 int asyncflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 int cflag;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -0700141 unsigned long statusflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 wait_queue_head_t open_wait;
Jiri Slaby95e07912007-10-18 03:06:25 -0700143 struct completion close_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Jiri Slaby7bcf97d2008-04-30 00:53:43 -0700145 u8 DCDState;
146 u8 lineCtrl;
147 u8 lowChkFlag;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800148};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Jiri Slaby74d7d972008-04-30 00:53:43 -0700150struct mon_str {
151 int tick;
152 int rxcnt[MAX_PORTS];
153 int txcnt[MAX_PORTS];
154};
155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156/* statusflags */
157#define TXSTOPPED 0x1
158#define LOWWAIT 0x2
159#define EMPTYWAIT 0x4
160#define THROTTLE 0x8
161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162#define SERIAL_DO_RESTART
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164#define WAKEUP_CHARS 256
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166static int ttymajor = MOXAMAJOR;
Jiri Slaby74d7d972008-04-30 00:53:43 -0700167static struct mon_str moxaLog;
168static unsigned int moxaFuncTout = HZ / 2;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -0700169static unsigned int moxaLowWaterChk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170/* Variables for insmod */
171#ifdef MODULE
Jiri Slabyd353eca2008-04-30 00:53:37 -0700172static unsigned long baseaddr[MAX_BOARDS];
173static unsigned int type[MAX_BOARDS];
174static unsigned int numports[MAX_BOARDS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175#endif
176
177MODULE_AUTHOR("William Chen");
178MODULE_DESCRIPTION("MOXA Intellio Family Multiport Board Device Driver");
179MODULE_LICENSE("GPL");
180#ifdef MODULE
Jiri Slabyd353eca2008-04-30 00:53:37 -0700181module_param_array(type, uint, NULL, 0);
182MODULE_PARM_DESC(type, "card type: C218=2, C320=4");
183module_param_array(baseaddr, ulong, NULL, 0);
184MODULE_PARM_DESC(baseaddr, "base address");
185module_param_array(numports, uint, NULL, 0);
186MODULE_PARM_DESC(numports, "numports (ignored for C218)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187#endif
188module_param(ttymajor, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190/*
191 * static functions:
192 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193static int moxa_open(struct tty_struct *, struct file *);
194static void moxa_close(struct tty_struct *, struct file *);
195static int moxa_write(struct tty_struct *, const unsigned char *, int);
196static int moxa_write_room(struct tty_struct *);
197static void moxa_flush_buffer(struct tty_struct *);
198static int moxa_chars_in_buffer(struct tty_struct *);
199static void moxa_flush_chars(struct tty_struct *);
200static void moxa_put_char(struct tty_struct *, unsigned char);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201static void moxa_throttle(struct tty_struct *);
202static void moxa_unthrottle(struct tty_struct *);
Alan Cox606d0992006-12-08 02:38:45 -0800203static void moxa_set_termios(struct tty_struct *, struct ktermios *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204static void moxa_stop(struct tty_struct *);
205static void moxa_start(struct tty_struct *);
206static void moxa_hangup(struct tty_struct *);
207static int moxa_tiocmget(struct tty_struct *tty, struct file *file);
208static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
209 unsigned int set, unsigned int clear);
210static void moxa_poll(unsigned long);
Alan Coxdb1acaa2008-02-08 04:18:43 -0800211static void moxa_set_tty_param(struct tty_struct *, struct ktermios *);
Jiri Slaby6f56b6582007-10-18 03:06:24 -0700212static int moxa_block_till_ready(struct tty_struct *, struct file *,
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800213 struct moxa_port *);
Jiri Slaby6f56b6582007-10-18 03:06:24 -0700214static void moxa_setup_empty_event(struct tty_struct *);
Jiri Slaby6f56b6582007-10-18 03:06:24 -0700215static void moxa_shut_down(struct moxa_port *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216/*
217 * moxa board interface functions:
218 */
Jiri Slabyb4173f42008-04-30 00:53:40 -0700219static void MoxaPortEnable(struct moxa_port *);
220static void MoxaPortDisable(struct moxa_port *);
221static int MoxaPortSetTermio(struct moxa_port *, struct ktermios *, speed_t);
222static int MoxaPortGetLineOut(struct moxa_port *, int *, int *);
223static void MoxaPortLineCtrl(struct moxa_port *, int, int);
224static void MoxaPortFlowCtrl(struct moxa_port *, int, int, int, int, int);
225static int MoxaPortLineStatus(struct moxa_port *);
Jiri Slabyb4173f42008-04-30 00:53:40 -0700226static void MoxaPortFlushData(struct moxa_port *, int);
227static int MoxaPortWriteData(struct moxa_port *, unsigned char *, int);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -0700228static int MoxaPortReadData(struct moxa_port *);
Jiri Slabyb4173f42008-04-30 00:53:40 -0700229static int MoxaPortTxQueue(struct moxa_port *);
230static int MoxaPortRxQueue(struct moxa_port *);
231static int MoxaPortTxFree(struct moxa_port *);
232static void MoxaPortTxDisable(struct moxa_port *);
233static void MoxaPortTxEnable(struct moxa_port *);
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800234static int moxa_get_serial_info(struct moxa_port *, struct serial_struct __user *);
235static int moxa_set_serial_info(struct moxa_port *, struct serial_struct __user *);
Jiri Slabyb4173f42008-04-30 00:53:40 -0700236static void MoxaSetFifo(struct moxa_port *port, int enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Jiri Slaby74d7d972008-04-30 00:53:43 -0700238/*
239 * I/O functions
240 */
241
242static void moxa_wait_finish(void __iomem *ofsAddr)
243{
244 unsigned long end = jiffies + moxaFuncTout;
245
246 while (readw(ofsAddr + FuncCode) != 0)
247 if (time_after(jiffies, end))
248 return;
249 if (readw(ofsAddr + FuncCode) != 0 && printk_ratelimit())
250 printk(KERN_WARNING "moxa function expired\n");
251}
252
253static void moxafunc(void __iomem *ofsAddr, int cmd, ushort arg)
254{
255 writew(arg, ofsAddr + FuncArg);
256 writew(cmd, ofsAddr + FuncCode);
257 moxa_wait_finish(ofsAddr);
258}
259
Jiri Slaby7bcf97d2008-04-30 00:53:43 -0700260static void moxa_low_water_check(void __iomem *ofsAddr)
261{
262 u16 rptr, wptr, mask, len;
263
264 if (readb(ofsAddr + FlagStat) & Xoff_state) {
265 rptr = readw(ofsAddr + RXrptr);
266 wptr = readw(ofsAddr + RXwptr);
267 mask = readw(ofsAddr + RX_mask);
268 len = (wptr - rptr) & mask;
269 if (len <= Low_water)
270 moxafunc(ofsAddr, FC_SendXon, 0);
271 }
272}
273
Jiri Slaby74d7d972008-04-30 00:53:43 -0700274/*
275 * TTY operations
276 */
277
278static int moxa_ioctl(struct tty_struct *tty, struct file *file,
279 unsigned int cmd, unsigned long arg)
280{
281 struct moxa_port *ch = tty->driver_data;
282 void __user *argp = (void __user *)arg;
283 int status;
284
285 if (tty->index == MAX_PORTS) {
286 if (cmd != MOXA_GETDATACOUNT && cmd != MOXA_GET_IOQUEUE &&
287 cmd != MOXA_GETMSTATUS)
288 return -EINVAL;
289 } else if (!ch)
290 return -ENODEV;
291
292 switch (cmd) {
293 case MOXA_GETDATACOUNT:
294 moxaLog.tick = jiffies;
295 return copy_to_user(argp, &moxaLog, sizeof(moxaLog)) ?
296 -EFAULT : 0;
297 case MOXA_FLUSH_QUEUE:
298 MoxaPortFlushData(ch, arg);
299 return 0;
300 case MOXA_GET_IOQUEUE: {
301 struct moxaq_str __user *argm = argp;
302 struct moxaq_str tmp;
303 struct moxa_port *p;
304 unsigned int i, j;
305
306 for (i = 0; i < MAX_BOARDS; i++) {
307 p = moxa_boards[i].ports;
308 for (j = 0; j < MAX_PORTS_PER_BOARD; j++, p++, argm++) {
309 memset(&tmp, 0, sizeof(tmp));
310 if (moxa_boards[i].ready) {
311 tmp.inq = MoxaPortRxQueue(p);
312 tmp.outq = MoxaPortTxQueue(p);
313 }
314 if (copy_to_user(argm, &tmp, sizeof(tmp)))
315 return -EFAULT;
316 }
317 }
318 return 0;
319 } case MOXA_GET_OQUEUE:
320 status = MoxaPortTxQueue(ch);
321 return put_user(status, (unsigned long __user *)argp);
322 case MOXA_GET_IQUEUE:
323 status = MoxaPortRxQueue(ch);
324 return put_user(status, (unsigned long __user *)argp);
325 case MOXA_GETMSTATUS: {
326 struct mxser_mstatus __user *argm = argp;
327 struct mxser_mstatus tmp;
328 struct moxa_port *p;
329 unsigned int i, j;
330
331 for (i = 0; i < MAX_BOARDS; i++) {
332 p = moxa_boards[i].ports;
333 for (j = 0; j < MAX_PORTS_PER_BOARD; j++, p++, argm++) {
334 memset(&tmp, 0, sizeof(tmp));
335 if (!moxa_boards[i].ready)
336 goto copy;
337
338 status = MoxaPortLineStatus(p);
339 if (status & 1)
340 tmp.cts = 1;
341 if (status & 2)
342 tmp.dsr = 1;
343 if (status & 4)
344 tmp.dcd = 1;
345
346 if (!p->tty || !p->tty->termios)
347 tmp.cflag = p->cflag;
348 else
349 tmp.cflag = p->tty->termios->c_cflag;
350copy:
351 if (copy_to_user(argm, &tmp, sizeof(tmp)))
352 return -EFAULT;
353 }
354 }
355 return 0;
356 }
357 case TIOCGSERIAL:
358 return moxa_get_serial_info(ch, argp);
359 case TIOCSSERIAL:
360 return moxa_set_serial_info(ch, argp);
361 }
362 return -ENOIOCTLCMD;
363}
364
365static void moxa_break_ctl(struct tty_struct *tty, int state)
366{
367 struct moxa_port *port = tty->driver_data;
368
369 moxafunc(port->tableAddr, state ? FC_SendBreak : FC_StopBreak,
370 Magic_code);
371}
372
Jeff Dikeb68e31d2006-10-02 02:17:18 -0700373static const struct tty_operations moxa_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 .open = moxa_open,
375 .close = moxa_close,
376 .write = moxa_write,
377 .write_room = moxa_write_room,
378 .flush_buffer = moxa_flush_buffer,
379 .chars_in_buffer = moxa_chars_in_buffer,
380 .flush_chars = moxa_flush_chars,
381 .put_char = moxa_put_char,
382 .ioctl = moxa_ioctl,
383 .throttle = moxa_throttle,
384 .unthrottle = moxa_unthrottle,
385 .set_termios = moxa_set_termios,
386 .stop = moxa_stop,
387 .start = moxa_start,
388 .hangup = moxa_hangup,
Jiri Slaby74d7d972008-04-30 00:53:43 -0700389 .break_ctl = moxa_break_ctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 .tiocmget = moxa_tiocmget,
391 .tiocmset = moxa_tiocmset,
392};
393
Jiri Slabyaa7e5222007-02-10 01:45:27 -0800394static struct tty_driver *moxaDriver;
Jiri Slabyaa7e5222007-02-10 01:45:27 -0800395static DEFINE_TIMER(moxaTimer, moxa_poll, 0, 0);
Ingo Molnar34af9462006-06-27 02:53:55 -0700396static DEFINE_SPINLOCK(moxa_lock);
Alan Cox33f0f882006-01-09 20:54:13 -0800397
Jiri Slaby74d7d972008-04-30 00:53:43 -0700398/*
399 * HW init
400 */
401
Jiri Slaby03718232008-04-30 00:53:39 -0700402static int moxa_check_fw_model(struct moxa_board_conf *brd, u8 model)
403{
404 switch (brd->boardType) {
405 case MOXA_BOARD_C218_ISA:
406 case MOXA_BOARD_C218_PCI:
407 if (model != 1)
408 goto err;
409 break;
410 case MOXA_BOARD_CP204J:
411 if (model != 3)
412 goto err;
413 break;
414 default:
415 if (model != 2)
416 goto err;
417 break;
418 }
419 return 0;
420err:
421 return -EINVAL;
422}
423
424static int moxa_check_fw(const void *ptr)
425{
426 const __le16 *lptr = ptr;
427
428 if (*lptr != cpu_to_le16(0x7980))
429 return -EINVAL;
430
431 return 0;
432}
433
434static int moxa_load_bios(struct moxa_board_conf *brd, const u8 *buf,
435 size_t len)
436{
437 void __iomem *baseAddr = brd->basemem;
438 u16 tmp;
439
440 writeb(HW_reset, baseAddr + Control_reg); /* reset */
441 msleep(10);
442 memset_io(baseAddr, 0, 4096);
443 memcpy_toio(baseAddr, buf, len); /* download BIOS */
444 writeb(0, baseAddr + Control_reg); /* restart */
445
446 msleep(2000);
447
448 switch (brd->boardType) {
449 case MOXA_BOARD_C218_ISA:
450 case MOXA_BOARD_C218_PCI:
451 tmp = readw(baseAddr + C218_key);
452 if (tmp != C218_KeyCode)
453 goto err;
454 break;
455 case MOXA_BOARD_CP204J:
456 tmp = readw(baseAddr + C218_key);
457 if (tmp != CP204J_KeyCode)
458 goto err;
459 break;
460 default:
461 tmp = readw(baseAddr + C320_key);
462 if (tmp != C320_KeyCode)
463 goto err;
464 tmp = readw(baseAddr + C320_status);
465 if (tmp != STS_init) {
466 printk(KERN_ERR "moxa: bios upload failed -- CPU/Basic "
467 "module not found\n");
468 return -EIO;
469 }
470 break;
471 }
472
473 return 0;
474err:
475 printk(KERN_ERR "moxa: bios upload failed -- board not found\n");
476 return -EIO;
477}
478
479static int moxa_load_320b(struct moxa_board_conf *brd, const u8 *ptr,
480 size_t len)
481{
482 void __iomem *baseAddr = brd->basemem;
483
484 if (len < 7168) {
485 printk(KERN_ERR "moxa: invalid 320 bios -- too short\n");
486 return -EINVAL;
487 }
488
489 writew(len - 7168 - 2, baseAddr + C320bapi_len);
490 writeb(1, baseAddr + Control_reg); /* Select Page 1 */
491 memcpy_toio(baseAddr + DynPage_addr, ptr, 7168);
492 writeb(2, baseAddr + Control_reg); /* Select Page 2 */
493 memcpy_toio(baseAddr + DynPage_addr, ptr + 7168, len - 7168);
494
495 return 0;
496}
497
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700498static int moxa_real_load_code(struct moxa_board_conf *brd, const void *ptr,
Jiri Slaby03718232008-04-30 00:53:39 -0700499 size_t len)
500{
501 void __iomem *baseAddr = brd->basemem;
502 const u16 *uptr = ptr;
503 size_t wlen, len2, j;
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700504 unsigned long key, loadbuf, loadlen, checksum, checksum_ok;
505 unsigned int i, retry, c320;
Jiri Slaby03718232008-04-30 00:53:39 -0700506 u16 usum, keycode;
507
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700508 c320 = brd->boardType == MOXA_BOARD_C320_PCI ||
509 brd->boardType == MOXA_BOARD_C320_ISA;
510 keycode = (brd->boardType == MOXA_BOARD_CP204J) ? CP204J_KeyCode :
511 C218_KeyCode;
512
513 switch (brd->boardType) {
514 case MOXA_BOARD_CP204J:
515 case MOXA_BOARD_C218_ISA:
516 case MOXA_BOARD_C218_PCI:
517 key = C218_key;
518 loadbuf = C218_LoadBuf;
519 loadlen = C218DLoad_len;
520 checksum = C218check_sum;
521 checksum_ok = C218chksum_ok;
522 break;
523 default:
524 key = C320_key;
525 keycode = C320_KeyCode;
526 loadbuf = C320_LoadBuf;
527 loadlen = C320DLoad_len;
528 checksum = C320check_sum;
529 checksum_ok = C320chksum_ok;
530 break;
531 }
532
Jiri Slaby03718232008-04-30 00:53:39 -0700533 usum = 0;
534 wlen = len >> 1;
535 for (i = 0; i < wlen; i++)
536 usum += le16_to_cpu(uptr[i]);
537 retry = 0;
538 do {
539 wlen = len >> 1;
540 j = 0;
541 while (wlen) {
542 len2 = (wlen > 2048) ? 2048 : wlen;
543 wlen -= len2;
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700544 memcpy_toio(baseAddr + loadbuf, ptr + j, len2 << 1);
Jiri Slaby03718232008-04-30 00:53:39 -0700545 j += len2 << 1;
546
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700547 writew(len2, baseAddr + loadlen);
548 writew(0, baseAddr + key);
Jiri Slaby03718232008-04-30 00:53:39 -0700549 for (i = 0; i < 100; i++) {
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700550 if (readw(baseAddr + key) == keycode)
Jiri Slaby03718232008-04-30 00:53:39 -0700551 break;
552 msleep(10);
553 }
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700554 if (readw(baseAddr + key) != keycode)
Jiri Slaby03718232008-04-30 00:53:39 -0700555 return -EIO;
556 }
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700557 writew(0, baseAddr + loadlen);
558 writew(usum, baseAddr + checksum);
559 writew(0, baseAddr + key);
Jiri Slaby03718232008-04-30 00:53:39 -0700560 for (i = 0; i < 100; i++) {
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700561 if (readw(baseAddr + key) == keycode)
Jiri Slaby03718232008-04-30 00:53:39 -0700562 break;
563 msleep(10);
564 }
565 retry++;
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700566 } while ((readb(baseAddr + checksum_ok) != 1) && (retry < 3));
567 if (readb(baseAddr + checksum_ok) != 1)
Jiri Slaby03718232008-04-30 00:53:39 -0700568 return -EIO;
569
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700570 writew(0, baseAddr + key);
Jiri Slaby03718232008-04-30 00:53:39 -0700571 for (i = 0; i < 600; i++) {
572 if (readw(baseAddr + Magic_no) == Magic_code)
573 break;
574 msleep(10);
575 }
576 if (readw(baseAddr + Magic_no) != Magic_code)
577 return -EIO;
578
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700579 if (c320) {
580 if (brd->busType == MOXA_BUS_TYPE_PCI) { /* ASIC board */
581 writew(0x3800, baseAddr + TMS320_PORT1);
582 writew(0x3900, baseAddr + TMS320_PORT2);
583 writew(28499, baseAddr + TMS320_CLOCK);
584 } else {
585 writew(0x3200, baseAddr + TMS320_PORT1);
586 writew(0x3400, baseAddr + TMS320_PORT2);
587 writew(19999, baseAddr + TMS320_CLOCK);
588 }
Jiri Slaby03718232008-04-30 00:53:39 -0700589 }
590 writew(1, baseAddr + Disable_IRQ);
591 writew(0, baseAddr + Magic_no);
592 for (i = 0; i < 500; i++) {
593 if (readw(baseAddr + Magic_no) == Magic_code)
594 break;
595 msleep(10);
596 }
597 if (readw(baseAddr + Magic_no) != Magic_code)
598 return -EIO;
599
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700600 if (c320) {
601 j = readw(baseAddr + Module_cnt);
602 if (j <= 0)
603 return -EIO;
604 brd->numPorts = j * 8;
605 writew(j, baseAddr + Module_no);
606 writew(0, baseAddr + Magic_no);
607 for (i = 0; i < 600; i++) {
608 if (readw(baseAddr + Magic_no) == Magic_code)
609 break;
610 msleep(10);
611 }
612 if (readw(baseAddr + Magic_no) != Magic_code)
613 return -EIO;
Jiri Slaby03718232008-04-30 00:53:39 -0700614 }
Jiri Slaby03718232008-04-30 00:53:39 -0700615 brd->intNdx = baseAddr + IRQindex;
616 brd->intPend = baseAddr + IRQpending;
617 brd->intTable = baseAddr + IRQtable;
618
619 return 0;
620}
621
622static int moxa_load_code(struct moxa_board_conf *brd, const void *ptr,
623 size_t len)
624{
625 void __iomem *ofsAddr, *baseAddr = brd->basemem;
626 struct moxa_port *port;
627 int retval, i;
628
629 if (len % 2) {
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700630 printk(KERN_ERR "moxa: bios length is not even\n");
Jiri Slaby03718232008-04-30 00:53:39 -0700631 return -EINVAL;
632 }
633
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700634 retval = moxa_real_load_code(brd, ptr, len); /* may change numPorts */
635 if (retval)
636 return retval;
637
Jiri Slaby03718232008-04-30 00:53:39 -0700638 switch (brd->boardType) {
639 case MOXA_BOARD_C218_ISA:
640 case MOXA_BOARD_C218_PCI:
641 case MOXA_BOARD_CP204J:
Jiri Slaby03718232008-04-30 00:53:39 -0700642 port = brd->ports;
643 for (i = 0; i < brd->numPorts; i++, port++) {
Jiri Slabyb4173f42008-04-30 00:53:40 -0700644 port->board = brd;
Jiri Slaby03718232008-04-30 00:53:39 -0700645 port->DCDState = 0;
646 port->tableAddr = baseAddr + Extern_table +
647 Extern_size * i;
648 ofsAddr = port->tableAddr;
649 writew(C218rx_mask, ofsAddr + RX_mask);
650 writew(C218tx_mask, ofsAddr + TX_mask);
651 writew(C218rx_spage + i * C218buf_pageno, ofsAddr + Page_rxb);
652 writew(readw(ofsAddr + Page_rxb) + C218rx_pageno, ofsAddr + EndPage_rxb);
653
654 writew(C218tx_spage + i * C218buf_pageno, ofsAddr + Page_txb);
655 writew(readw(ofsAddr + Page_txb) + C218tx_pageno, ofsAddr + EndPage_txb);
656
657 }
658 break;
659 default:
Jiri Slaby03718232008-04-30 00:53:39 -0700660 port = brd->ports;
661 for (i = 0; i < brd->numPorts; i++, port++) {
Jiri Slabyb4173f42008-04-30 00:53:40 -0700662 port->board = brd;
Jiri Slaby03718232008-04-30 00:53:39 -0700663 port->DCDState = 0;
664 port->tableAddr = baseAddr + Extern_table +
665 Extern_size * i;
666 ofsAddr = port->tableAddr;
667 switch (brd->numPorts) {
668 case 8:
669 writew(C320p8rx_mask, ofsAddr + RX_mask);
670 writew(C320p8tx_mask, ofsAddr + TX_mask);
671 writew(C320p8rx_spage + i * C320p8buf_pgno, ofsAddr + Page_rxb);
672 writew(readw(ofsAddr + Page_rxb) + C320p8rx_pgno, ofsAddr + EndPage_rxb);
673 writew(C320p8tx_spage + i * C320p8buf_pgno, ofsAddr + Page_txb);
674 writew(readw(ofsAddr + Page_txb) + C320p8tx_pgno, ofsAddr + EndPage_txb);
675
676 break;
677 case 16:
678 writew(C320p16rx_mask, ofsAddr + RX_mask);
679 writew(C320p16tx_mask, ofsAddr + TX_mask);
680 writew(C320p16rx_spage + i * C320p16buf_pgno, ofsAddr + Page_rxb);
681 writew(readw(ofsAddr + Page_rxb) + C320p16rx_pgno, ofsAddr + EndPage_rxb);
682 writew(C320p16tx_spage + i * C320p16buf_pgno, ofsAddr + Page_txb);
683 writew(readw(ofsAddr + Page_txb) + C320p16tx_pgno, ofsAddr + EndPage_txb);
684 break;
685
686 case 24:
687 writew(C320p24rx_mask, ofsAddr + RX_mask);
688 writew(C320p24tx_mask, ofsAddr + TX_mask);
689 writew(C320p24rx_spage + i * C320p24buf_pgno, ofsAddr + Page_rxb);
690 writew(readw(ofsAddr + Page_rxb) + C320p24rx_pgno, ofsAddr + EndPage_rxb);
691 writew(C320p24tx_spage + i * C320p24buf_pgno, ofsAddr + Page_txb);
692 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
693 break;
694 case 32:
695 writew(C320p32rx_mask, ofsAddr + RX_mask);
696 writew(C320p32tx_mask, ofsAddr + TX_mask);
697 writew(C320p32tx_ofs, ofsAddr + Ofs_txb);
698 writew(C320p32rx_spage + i * C320p32buf_pgno, ofsAddr + Page_rxb);
699 writew(readb(ofsAddr + Page_rxb), ofsAddr + EndPage_rxb);
700 writew(C320p32tx_spage + i * C320p32buf_pgno, ofsAddr + Page_txb);
701 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
702 break;
703 }
704 }
705 break;
706 }
Jiri Slaby03718232008-04-30 00:53:39 -0700707 return 0;
708}
709
710static int moxa_load_fw(struct moxa_board_conf *brd, const struct firmware *fw)
711{
712 void *ptr = fw->data;
713 char rsn[64];
714 u16 lens[5];
715 size_t len;
716 unsigned int a, lenp, lencnt;
717 int ret = -EINVAL;
718 struct {
719 __le32 magic; /* 0x34303430 */
720 u8 reserved1[2];
721 u8 type; /* UNIX = 3 */
722 u8 model; /* C218T=1, C320T=2, CP204=3 */
723 u8 reserved2[8];
724 __le16 len[5];
725 } *hdr = ptr;
726
727 BUILD_BUG_ON(ARRAY_SIZE(hdr->len) != ARRAY_SIZE(lens));
728
729 if (fw->size < MOXA_FW_HDRLEN) {
730 strcpy(rsn, "too short (even header won't fit)");
731 goto err;
732 }
733 if (hdr->magic != cpu_to_le32(0x30343034)) {
734 sprintf(rsn, "bad magic: %.8x", le32_to_cpu(hdr->magic));
735 goto err;
736 }
737 if (hdr->type != 3) {
738 sprintf(rsn, "not for linux, type is %u", hdr->type);
739 goto err;
740 }
741 if (moxa_check_fw_model(brd, hdr->model)) {
742 sprintf(rsn, "not for this card, model is %u", hdr->model);
743 goto err;
744 }
745
746 len = MOXA_FW_HDRLEN;
747 lencnt = hdr->model == 2 ? 5 : 3;
748 for (a = 0; a < ARRAY_SIZE(lens); a++) {
749 lens[a] = le16_to_cpu(hdr->len[a]);
750 if (lens[a] && len + lens[a] <= fw->size &&
751 moxa_check_fw(&fw->data[len]))
752 printk(KERN_WARNING "moxa firmware: unexpected input "
753 "at offset %u, but going on\n", (u32)len);
754 if (!lens[a] && a < lencnt) {
755 sprintf(rsn, "too few entries in fw file");
756 goto err;
757 }
758 len += lens[a];
759 }
760
761 if (len != fw->size) {
762 sprintf(rsn, "bad length: %u (should be %u)", (u32)fw->size,
763 (u32)len);
764 goto err;
765 }
766
767 ptr += MOXA_FW_HDRLEN;
768 lenp = 0; /* bios */
769
770 strcpy(rsn, "read above");
771
772 ret = moxa_load_bios(brd, ptr, lens[lenp]);
773 if (ret)
774 goto err;
775
776 /* we skip the tty section (lens[1]), since we don't need it */
777 ptr += lens[lenp] + lens[lenp + 1];
778 lenp += 2; /* comm */
779
780 if (hdr->model == 2) {
781 ret = moxa_load_320b(brd, ptr, lens[lenp]);
782 if (ret)
783 goto err;
784 /* skip another tty */
785 ptr += lens[lenp] + lens[lenp + 1];
786 lenp += 2;
787 }
788
789 ret = moxa_load_code(brd, ptr, lens[lenp]);
790 if (ret)
791 goto err;
792
793 return 0;
794err:
795 printk(KERN_ERR "firmware failed to load, reason: %s\n", rsn);
796 return ret;
797}
798
799static int moxa_init_board(struct moxa_board_conf *brd, struct device *dev)
800{
801 const struct firmware *fw;
802 const char *file;
Jiri Slaby810ab092008-04-30 00:53:41 -0700803 struct moxa_port *p;
804 unsigned int i;
Jiri Slaby03718232008-04-30 00:53:39 -0700805 int ret;
806
Jiri Slaby810ab092008-04-30 00:53:41 -0700807 brd->ports = kcalloc(MAX_PORTS_PER_BOARD, sizeof(*brd->ports),
808 GFP_KERNEL);
809 if (brd->ports == NULL) {
810 printk(KERN_ERR "cannot allocate memory for ports\n");
811 ret = -ENOMEM;
812 goto err;
813 }
814
815 for (i = 0, p = brd->ports; i < MAX_PORTS_PER_BOARD; i++, p++) {
816 p->type = PORT_16550A;
817 p->close_delay = 5 * HZ / 10;
818 p->cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
819 init_waitqueue_head(&p->open_wait);
820 init_completion(&p->close_wait);
Jiri Slaby810ab092008-04-30 00:53:41 -0700821 }
822
Jiri Slaby03718232008-04-30 00:53:39 -0700823 switch (brd->boardType) {
824 case MOXA_BOARD_C218_ISA:
825 case MOXA_BOARD_C218_PCI:
826 file = "c218tunx.cod";
827 break;
828 case MOXA_BOARD_CP204J:
829 file = "cp204unx.cod";
830 break;
831 default:
832 file = "c320tunx.cod";
833 break;
834 }
835
836 ret = request_firmware(&fw, file, dev);
837 if (ret) {
838 printk(KERN_ERR "request_firmware failed\n");
Jiri Slaby810ab092008-04-30 00:53:41 -0700839 goto err_free;
Jiri Slaby03718232008-04-30 00:53:39 -0700840 }
841
842 ret = moxa_load_fw(brd, fw);
843
844 release_firmware(fw);
Jiri Slaby810ab092008-04-30 00:53:41 -0700845
846 if (ret)
847 goto err_free;
848
849 brd->ready = 1;
850
Jiri Slaby0bcc4ca2008-04-30 00:53:42 -0700851 if (!timer_pending(&moxaTimer))
852 mod_timer(&moxaTimer, jiffies + HZ / 50);
853
Jiri Slaby810ab092008-04-30 00:53:41 -0700854 return 0;
855err_free:
856 kfree(brd->ports);
857err:
Jiri Slaby03718232008-04-30 00:53:39 -0700858 return ret;
859}
860
Jiri Slaby810ab092008-04-30 00:53:41 -0700861static void moxa_board_deinit(struct moxa_board_conf *brd)
862{
Jiri Slaby7bcf97d2008-04-30 00:53:43 -0700863 spin_lock_bh(&moxa_lock);
Jiri Slaby810ab092008-04-30 00:53:41 -0700864 brd->ready = 0;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -0700865 spin_unlock_bh(&moxa_lock);
Jiri Slaby810ab092008-04-30 00:53:41 -0700866 iounmap(brd->basemem);
867 brd->basemem = NULL;
868 kfree(brd->ports);
869}
870
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871#ifdef CONFIG_PCI
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800872static int __devinit moxa_pci_probe(struct pci_dev *pdev,
873 const struct pci_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874{
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800875 struct moxa_board_conf *board;
876 unsigned int i;
877 int board_type = ent->driver_data;
878 int retval;
879
880 retval = pci_enable_device(pdev);
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700881 if (retval) {
882 dev_err(&pdev->dev, "can't enable pci device\n");
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800883 goto err;
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700884 }
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800885
886 for (i = 0; i < MAX_BOARDS; i++)
887 if (moxa_boards[i].basemem == NULL)
888 break;
889
890 retval = -ENODEV;
891 if (i >= MAX_BOARDS) {
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700892 dev_warn(&pdev->dev, "more than %u MOXA Intellio family boards "
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800893 "found. Board is ignored.\n", MAX_BOARDS);
894 goto err;
895 }
896
897 board = &moxa_boards[i];
Jiri Slabye46a5e32008-04-30 00:53:37 -0700898
899 retval = pci_request_region(pdev, 2, "moxa-base");
900 if (retval) {
901 dev_err(&pdev->dev, "can't request pci region 2\n");
902 goto err;
903 }
904
905 board->basemem = ioremap(pci_resource_start(pdev, 2), 0x4000);
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700906 if (board->basemem == NULL) {
907 dev_err(&pdev->dev, "can't remap io space 2\n");
Jiri Slabye46a5e32008-04-30 00:53:37 -0700908 goto err_reg;
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700909 }
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800910
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 board->boardType = board_type;
912 switch (board_type) {
913 case MOXA_BOARD_C218_ISA:
914 case MOXA_BOARD_C218_PCI:
915 board->numPorts = 8;
916 break;
917
918 case MOXA_BOARD_CP204J:
919 board->numPorts = 4;
920 break;
921 default:
922 board->numPorts = 0;
923 break;
924 }
925 board->busType = MOXA_BUS_TYPE_PCI;
Jiri Slabya784bf72007-02-10 01:45:36 -0800926
Jiri Slaby03718232008-04-30 00:53:39 -0700927 retval = moxa_init_board(board, &pdev->dev);
928 if (retval)
929 goto err_base;
930
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800931 pci_set_drvdata(pdev, board);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
933 return (0);
Jiri Slaby03718232008-04-30 00:53:39 -0700934err_base:
935 iounmap(board->basemem);
936 board->basemem = NULL;
Jiri Slabye46a5e32008-04-30 00:53:37 -0700937err_reg:
938 pci_release_region(pdev, 2);
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800939err:
940 return retval;
941}
942
943static void __devexit moxa_pci_remove(struct pci_dev *pdev)
944{
945 struct moxa_board_conf *brd = pci_get_drvdata(pdev);
946
Jiri Slaby810ab092008-04-30 00:53:41 -0700947 moxa_board_deinit(brd);
948
Jiri Slabye46a5e32008-04-30 00:53:37 -0700949 pci_release_region(pdev, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950}
Jiri Slabya784bf72007-02-10 01:45:36 -0800951
952static struct pci_driver moxa_pci_driver = {
953 .name = "moxa",
954 .id_table = moxa_pcibrds,
955 .probe = moxa_pci_probe,
956 .remove = __devexit_p(moxa_pci_remove)
957};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958#endif /* CONFIG_PCI */
959
960static int __init moxa_init(void)
961{
Jiri Slaby810ab092008-04-30 00:53:41 -0700962 unsigned int isabrds = 0;
Jiri Slabyd353eca2008-04-30 00:53:37 -0700963 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700965 printk(KERN_INFO "MOXA Intellio family driver version %s\n",
966 MOXA_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 moxaDriver = alloc_tty_driver(MAX_PORTS + 1);
968 if (!moxaDriver)
969 return -ENOMEM;
970
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 moxaDriver->owner = THIS_MODULE;
Sergey Vlasov9b4e3b12005-09-03 16:26:49 +0100972 moxaDriver->name = "ttyMX";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 moxaDriver->major = ttymajor;
974 moxaDriver->minor_start = 0;
975 moxaDriver->type = TTY_DRIVER_TYPE_SERIAL;
976 moxaDriver->subtype = SERIAL_TYPE_NORMAL;
977 moxaDriver->init_termios = tty_std_termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 moxaDriver->init_termios.c_cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
Alan Cox606d0992006-12-08 02:38:45 -0800979 moxaDriver->init_termios.c_ispeed = 9600;
980 moxaDriver->init_termios.c_ospeed = 9600;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 moxaDriver->flags = TTY_DRIVER_REAL_RAW;
982 tty_set_operations(moxaDriver, &moxa_ops);
983
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700984 pr_debug("Moxa tty devices major number = %d\n", ttymajor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
986 if (tty_register_driver(moxaDriver)) {
987 printk(KERN_ERR "Couldn't install MOXA Smartio family driver !\n");
988 put_tty_driver(moxaDriver);
989 return -1;
990 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
Jiri Slabyd353eca2008-04-30 00:53:37 -0700992 /* Find the boards defined from module args. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993#ifdef MODULE
Jiri Slabyd353eca2008-04-30 00:53:37 -0700994 {
995 struct moxa_board_conf *brd = moxa_boards;
Jiri Slaby810ab092008-04-30 00:53:41 -0700996 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 for (i = 0; i < MAX_BOARDS; i++) {
Jiri Slabyd353eca2008-04-30 00:53:37 -0700998 if (!baseaddr[i])
999 break;
1000 if (type[i] == MOXA_BOARD_C218_ISA ||
1001 type[i] == MOXA_BOARD_C320_ISA) {
Jiri Slaby7aeb95d2007-10-18 03:06:24 -07001002 pr_debug("Moxa board %2d: %s board(baseAddr=%lx)\n",
Jiri Slabyd353eca2008-04-30 00:53:37 -07001003 isabrds + 1, moxa_brdname[type[i] - 1],
1004 baseaddr[i]);
1005 brd->boardType = type[i];
1006 brd->numPorts = type[i] == MOXA_BOARD_C218_ISA ? 8 :
1007 numports[i];
1008 brd->busType = MOXA_BUS_TYPE_ISA;
1009 brd->basemem = ioremap(baseaddr[i], 0x4000);
1010 if (!brd->basemem) {
1011 printk(KERN_ERR "moxa: can't remap %lx\n",
1012 baseaddr[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 continue;
1014 }
Jiri Slaby03718232008-04-30 00:53:39 -07001015 if (moxa_init_board(brd, NULL)) {
1016 iounmap(brd->basemem);
1017 brd->basemem = NULL;
1018 continue;
1019 }
Jiri Slabyd353eca2008-04-30 00:53:37 -07001020
1021 brd++;
1022 isabrds++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 }
1024 }
Jiri Slabyd353eca2008-04-30 00:53:37 -07001025 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026#endif
Jiri Slabya784bf72007-02-10 01:45:36 -08001027
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028#ifdef CONFIG_PCI
Jiri Slabya784bf72007-02-10 01:45:36 -08001029 retval = pci_register_driver(&moxa_pci_driver);
1030 if (retval) {
1031 printk(KERN_ERR "Can't register moxa pci driver!\n");
Jiri Slabyd353eca2008-04-30 00:53:37 -07001032 if (isabrds)
Jiri Slabya784bf72007-02-10 01:45:36 -08001033 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 }
1035#endif
Jiri Slabya784bf72007-02-10 01:45:36 -08001036
Jiri Slabya784bf72007-02-10 01:45:36 -08001037 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038}
1039
1040static void __exit moxa_exit(void)
1041{
1042 int i;
1043
Jiri Slabyc251ae02007-02-10 01:45:32 -08001044 del_timer_sync(&moxaTimer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 if (tty_unregister_driver(moxaDriver))
Jiri Slaby7aeb95d2007-10-18 03:06:24 -07001047 printk(KERN_ERR "Couldn't unregister MOXA Intellio family "
1048 "serial driver\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 put_tty_driver(moxaDriver);
Jiri Slaby86fbf142006-10-21 10:24:01 -07001050
Jiri Slaby9cde5bf2007-02-10 01:45:35 -08001051#ifdef CONFIG_PCI
Jiri Slabya784bf72007-02-10 01:45:36 -08001052 pci_unregister_driver(&moxa_pci_driver);
Jiri Slaby9cde5bf2007-02-10 01:45:35 -08001053#endif
Jiri Slabya784bf72007-02-10 01:45:36 -08001054
Jiri Slaby810ab092008-04-30 00:53:41 -07001055 for (i = 0; i < MAX_BOARDS; i++) /* ISA boards */
1056 if (moxa_boards[i].ready)
1057 moxa_board_deinit(&moxa_boards[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058}
1059
1060module_init(moxa_init);
1061module_exit(moxa_exit);
1062
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063static int moxa_open(struct tty_struct *tty, struct file *filp)
1064{
Jiri Slaby810ab092008-04-30 00:53:41 -07001065 struct moxa_board_conf *brd;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001066 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 int port;
1068 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
Jiri Slaby11324ed2007-02-10 01:45:31 -08001070 port = tty->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 if (port == MAX_PORTS) {
Jiri Slaby74d7d972008-04-30 00:53:43 -07001072 return capable(CAP_SYS_ADMIN) ? 0 : -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 }
Jiri Slaby810ab092008-04-30 00:53:41 -07001074 brd = &moxa_boards[port / MAX_PORTS_PER_BOARD];
1075 if (!brd->ready)
1076 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
Jiri Slaby810ab092008-04-30 00:53:41 -07001078 ch = &brd->ports[port % MAX_PORTS_PER_BOARD];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 ch->count++;
1080 tty->driver_data = ch;
1081 ch->tty = tty;
1082 if (!(ch->asyncflags & ASYNC_INITIALIZED)) {
1083 ch->statusflags = 0;
Alan Coxdb1acaa2008-02-08 04:18:43 -08001084 moxa_set_tty_param(tty, tty->termios);
Jiri Slabyb4173f42008-04-30 00:53:40 -07001085 MoxaPortLineCtrl(ch, 1, 1);
1086 MoxaPortEnable(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 ch->asyncflags |= ASYNC_INITIALIZED;
1088 }
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001089 retval = moxa_block_till_ready(tty, filp, ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
1091 moxa_unthrottle(tty);
1092
1093 if (ch->type == PORT_16550A) {
Jiri Slabyb4173f42008-04-30 00:53:40 -07001094 MoxaSetFifo(ch, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 } else {
Jiri Slabyb4173f42008-04-30 00:53:40 -07001096 MoxaSetFifo(ch, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 }
1098
1099 return (retval);
1100}
1101
1102static void moxa_close(struct tty_struct *tty, struct file *filp)
1103{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001104 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 int port;
1106
Jiri Slaby11324ed2007-02-10 01:45:31 -08001107 port = tty->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 if (port == MAX_PORTS) {
1109 return;
1110 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 if (tty->driver_data == NULL) {
1112 return;
1113 }
1114 if (tty_hung_up_p(filp)) {
1115 return;
1116 }
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001117 ch = (struct moxa_port *) tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
1119 if ((tty->count == 1) && (ch->count != 1)) {
Jiri Slaby7aeb95d2007-10-18 03:06:24 -07001120 printk(KERN_WARNING "moxa_close: bad serial port count; "
1121 "tty->count is 1, ch->count is %d\n", ch->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 ch->count = 1;
1123 }
1124 if (--ch->count < 0) {
Jiri Slaby7aeb95d2007-10-18 03:06:24 -07001125 printk(KERN_WARNING "moxa_close: bad serial port count, "
1126 "device=%s\n", tty->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 ch->count = 0;
1128 }
1129 if (ch->count) {
1130 return;
1131 }
1132 ch->asyncflags |= ASYNC_CLOSING;
1133
1134 ch->cflag = tty->termios->c_cflag;
1135 if (ch->asyncflags & ASYNC_INITIALIZED) {
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001136 moxa_setup_empty_event(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 tty_wait_until_sent(tty, 30 * HZ); /* 30 seconds timeout */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 }
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001139 moxa_shut_down(ch);
Jiri Slabyb4173f42008-04-30 00:53:40 -07001140 MoxaPortFlushData(ch, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
1142 if (tty->driver->flush_buffer)
1143 tty->driver->flush_buffer(tty);
1144 tty_ldisc_flush(tty);
1145
1146 tty->closing = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 ch->tty = NULL;
1148 if (ch->blocked_open) {
1149 if (ch->close_delay) {
1150 msleep_interruptible(jiffies_to_msecs(ch->close_delay));
1151 }
1152 wake_up_interruptible(&ch->open_wait);
1153 }
1154 ch->asyncflags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
Jiri Slaby95e07912007-10-18 03:06:25 -07001155 complete_all(&ch->close_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156}
1157
1158static int moxa_write(struct tty_struct *tty,
1159 const unsigned char *buf, int count)
1160{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001161 struct moxa_port *ch = tty->driver_data;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001162 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 if (ch == NULL)
Jiri Slabyb4173f42008-04-30 00:53:40 -07001165 return 0;
Alan Cox33f0f882006-01-09 20:54:13 -08001166
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001167 spin_lock_bh(&moxa_lock);
Jiri Slabyb4173f42008-04-30 00:53:40 -07001168 len = MoxaPortWriteData(ch, (unsigned char *) buf, count);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001169 spin_unlock_bh(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
1171 /*********************************************
1172 if ( !(ch->statusflags & LOWWAIT) &&
1173 ((len != count) || (MoxaPortTxFree(port) <= 100)) )
1174 ************************************************/
1175 ch->statusflags |= LOWWAIT;
1176 return (len);
1177}
1178
1179static int moxa_write_room(struct tty_struct *tty)
1180{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001181 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
1183 if (tty->stopped)
1184 return (0);
Jiri Slabyb4173f42008-04-30 00:53:40 -07001185 ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 if (ch == NULL)
1187 return (0);
Jiri Slabyb4173f42008-04-30 00:53:40 -07001188 return MoxaPortTxFree(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189}
1190
1191static void moxa_flush_buffer(struct tty_struct *tty)
1192{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001193 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
1195 if (ch == NULL)
1196 return;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001197 MoxaPortFlushData(ch, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 tty_wakeup(tty);
1199}
1200
1201static int moxa_chars_in_buffer(struct tty_struct *tty)
1202{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001203 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 int chars;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
1206 /*
1207 * Sigh...I have to check if driver_data is NULL here, because
1208 * if an open() fails, the TTY subsystem eventually calls
1209 * tty_wait_until_sent(), which calls the driver's chars_in_buffer()
1210 * routine. And since the open() failed, we return 0 here. TDJ
1211 */
1212 if (ch == NULL)
1213 return (0);
Jiri Slabyb4173f42008-04-30 00:53:40 -07001214 chars = MoxaPortTxQueue(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 if (chars) {
1216 /*
1217 * Make it possible to wakeup anything waiting for output
1218 * in tty_ioctl.c, etc.
1219 */
1220 if (!(ch->statusflags & EMPTYWAIT))
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001221 moxa_setup_empty_event(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 }
1223 return (chars);
1224}
1225
1226static void moxa_flush_chars(struct tty_struct *tty)
1227{
1228 /*
1229 * Don't think I need this, because this is called to empty the TX
1230 * buffer for the 16450, 16550, etc.
1231 */
1232}
1233
1234static void moxa_put_char(struct tty_struct *tty, unsigned char c)
1235{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001236 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 if (ch == NULL)
1239 return;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001240 spin_lock_bh(&moxa_lock);
Jiri Slabyb4173f42008-04-30 00:53:40 -07001241 MoxaPortWriteData(ch, &c, 1);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001242 spin_unlock_bh(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 /************************************************
1244 if ( !(ch->statusflags & LOWWAIT) && (MoxaPortTxFree(port) <= 100) )
1245 *************************************************/
1246 ch->statusflags |= LOWWAIT;
1247}
1248
1249static int moxa_tiocmget(struct tty_struct *tty, struct file *file)
1250{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001251 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 int flag = 0, dtr, rts;
1253
Jiri Slaby74d7d972008-04-30 00:53:43 -07001254 if (!ch)
1255 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
Jiri Slabyb4173f42008-04-30 00:53:40 -07001257 MoxaPortGetLineOut(ch, &dtr, &rts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 if (dtr)
1259 flag |= TIOCM_DTR;
1260 if (rts)
1261 flag |= TIOCM_RTS;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001262 dtr = MoxaPortLineStatus(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 if (dtr & 1)
1264 flag |= TIOCM_CTS;
1265 if (dtr & 2)
1266 flag |= TIOCM_DSR;
1267 if (dtr & 4)
1268 flag |= TIOCM_CD;
1269 return flag;
1270}
1271
1272static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
1273 unsigned int set, unsigned int clear)
1274{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001275 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 int port;
1277 int dtr, rts;
1278
Jiri Slaby11324ed2007-02-10 01:45:31 -08001279 port = tty->index;
Jiri Slaby74d7d972008-04-30 00:53:43 -07001280 if (!ch)
1281 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
Jiri Slabyb4173f42008-04-30 00:53:40 -07001283 MoxaPortGetLineOut(ch, &dtr, &rts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 if (set & TIOCM_RTS)
1285 rts = 1;
1286 if (set & TIOCM_DTR)
1287 dtr = 1;
1288 if (clear & TIOCM_RTS)
1289 rts = 0;
1290 if (clear & TIOCM_DTR)
1291 dtr = 0;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001292 MoxaPortLineCtrl(ch, dtr, rts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 return 0;
1294}
1295
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296static void moxa_throttle(struct tty_struct *tty)
1297{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001298 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
1300 ch->statusflags |= THROTTLE;
1301}
1302
1303static void moxa_unthrottle(struct tty_struct *tty)
1304{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001305 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
1307 ch->statusflags &= ~THROTTLE;
1308}
1309
1310static void moxa_set_termios(struct tty_struct *tty,
Alan Cox606d0992006-12-08 02:38:45 -08001311 struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001313 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
1315 if (ch == NULL)
1316 return;
Alan Coxdb1acaa2008-02-08 04:18:43 -08001317 moxa_set_tty_param(tty, old_termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 if (!(old_termios->c_cflag & CLOCAL) &&
1319 (tty->termios->c_cflag & CLOCAL))
1320 wake_up_interruptible(&ch->open_wait);
1321}
1322
1323static void moxa_stop(struct tty_struct *tty)
1324{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001325 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
1327 if (ch == NULL)
1328 return;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001329 MoxaPortTxDisable(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 ch->statusflags |= TXSTOPPED;
1331}
1332
1333
1334static void moxa_start(struct tty_struct *tty)
1335{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001336 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
1338 if (ch == NULL)
1339 return;
1340
1341 if (!(ch->statusflags & TXSTOPPED))
1342 return;
1343
Jiri Slabyb4173f42008-04-30 00:53:40 -07001344 MoxaPortTxEnable(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 ch->statusflags &= ~TXSTOPPED;
1346}
1347
1348static void moxa_hangup(struct tty_struct *tty)
1349{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001350 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
1352 moxa_flush_buffer(tty);
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001353 moxa_shut_down(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 ch->count = 0;
1355 ch->asyncflags &= ~ASYNC_NORMAL_ACTIVE;
1356 ch->tty = NULL;
1357 wake_up_interruptible(&ch->open_wait);
1358}
1359
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001360static void moxa_new_dcdstate(struct moxa_port *p, u8 dcd)
1361{
1362 dcd = !!dcd;
1363
1364 if ((dcd != p->DCDState) && p->tty && C_CLOCAL(p->tty)) {
1365 if (!dcd) {
1366 tty_hangup(p->tty);
1367 p->asyncflags &= ~ASYNC_NORMAL_ACTIVE;
1368 }
1369 wake_up_interruptible(&p->open_wait);
1370 }
1371 p->DCDState = dcd;
1372}
1373
1374static int moxa_poll_port(struct moxa_port *p, unsigned int handle,
1375 u16 __iomem *ip)
1376{
1377 struct tty_struct *tty = p->tty;
1378 void __iomem *ofsAddr;
1379 unsigned int inited = p->asyncflags & ASYNC_INITIALIZED;
1380 u16 intr;
1381
1382 if (tty) {
1383 if ((p->statusflags & EMPTYWAIT) &&
1384 MoxaPortTxQueue(p) == 0) {
1385 p->statusflags &= ~EMPTYWAIT;
1386 tty_wakeup(tty);
1387 }
1388 if ((p->statusflags & LOWWAIT) && !tty->stopped &&
1389 MoxaPortTxQueue(p) <= WAKEUP_CHARS) {
1390 p->statusflags &= ~LOWWAIT;
1391 tty_wakeup(tty);
1392 }
1393
1394 if (inited && !(p->statusflags & THROTTLE) &&
1395 MoxaPortRxQueue(p) > 0) { /* RX */
1396 MoxaPortReadData(p);
1397 tty_schedule_flip(tty);
1398 }
1399 } else {
1400 p->statusflags &= ~EMPTYWAIT;
1401 MoxaPortFlushData(p, 0); /* flush RX */
1402 }
1403
1404 if (!handle) /* nothing else to do */
1405 return 0;
1406
1407 intr = readw(ip); /* port irq status */
1408 if (intr == 0)
1409 return 0;
1410
1411 writew(0, ip); /* ACK port */
1412 ofsAddr = p->tableAddr;
1413 if (intr & IntrTx) /* disable tx intr */
1414 writew(readw(ofsAddr + HostStat) & ~WakeupTx,
1415 ofsAddr + HostStat);
1416
1417 if (!inited)
1418 return 0;
1419
1420 if (tty && (intr & IntrBreak) && !I_IGNBRK(tty)) { /* BREAK */
1421 tty_insert_flip_char(tty, 0, TTY_BREAK);
1422 tty_schedule_flip(tty);
1423 }
1424
1425 if (intr & IntrLine)
1426 moxa_new_dcdstate(p, readb(ofsAddr + FlagStat) & DCD_state);
1427
1428 return 0;
1429}
1430
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431static void moxa_poll(unsigned long ignored)
1432{
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001433 struct moxa_board_conf *brd;
1434 u16 __iomem *ip;
1435 unsigned int card, port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001437 spin_lock(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 for (card = 0; card < MAX_BOARDS; card++) {
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001439 brd = &moxa_boards[card];
1440 if (!brd->ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 continue;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001442
1443 ip = NULL;
1444 if (readb(brd->intPend) == 0xff)
1445 ip = brd->intTable + readb(brd->intNdx);
1446
1447 for (port = 0; port < brd->numPorts; port++)
1448 moxa_poll_port(&brd->ports[port], !!ip, ip + port);
1449
1450 if (ip)
1451 writeb(0, brd->intPend); /* ACK */
1452
1453 if (moxaLowWaterChk) {
1454 struct moxa_port *p = brd->ports;
1455 for (port = 0; port < brd->numPorts; port++, p++)
1456 if (p->lowChkFlag) {
1457 p->lowChkFlag = 0;
1458 moxa_low_water_check(p->tableAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 }
1461 }
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001462 moxaLowWaterChk = 0;
1463 spin_unlock(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
Jiri Slabyaa7e5222007-02-10 01:45:27 -08001465 mod_timer(&moxaTimer, jiffies + HZ / 50);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466}
1467
1468/******************************************************************************/
1469
Alan Coxdb1acaa2008-02-08 04:18:43 -08001470static void moxa_set_tty_param(struct tty_struct *tty, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471{
Alan Cox606d0992006-12-08 02:38:45 -08001472 register struct ktermios *ts;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001473 struct moxa_port *ch;
Alan Coxdb1acaa2008-02-08 04:18:43 -08001474 int rts, cts, txflow, rxflow, xany, baud;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001476 ch = (struct moxa_port *) tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 ts = tty->termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 rts = cts = txflow = rxflow = xany = 0;
1479 if (ts->c_cflag & CRTSCTS)
1480 rts = cts = 1;
1481 if (ts->c_iflag & IXON)
1482 txflow = 1;
1483 if (ts->c_iflag & IXOFF)
1484 rxflow = 1;
1485 if (ts->c_iflag & IXANY)
1486 xany = 1;
Alan Coxdb1acaa2008-02-08 04:18:43 -08001487
1488 /* Clear the features we don't support */
1489 ts->c_cflag &= ~CMSPAR;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001490 MoxaPortFlowCtrl(ch, rts, cts, txflow, rxflow, xany);
1491 baud = MoxaPortSetTermio(ch, ts, tty_get_baud_rate(tty));
Alan Coxdb1acaa2008-02-08 04:18:43 -08001492 if (baud == -1)
1493 baud = tty_termios_baud_rate(old_termios);
1494 /* Not put the baud rate into the termios data */
1495 tty_encode_baud_rate(tty, baud, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496}
1497
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001498static int moxa_block_till_ready(struct tty_struct *tty, struct file *filp,
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001499 struct moxa_port *ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500{
1501 DECLARE_WAITQUEUE(wait,current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 int retval;
1503 int do_clocal = C_CLOCAL(tty);
1504
1505 /*
1506 * If the device is in the middle of being closed, then block
1507 * until it's done, and then try again.
1508 */
1509 if (tty_hung_up_p(filp) || (ch->asyncflags & ASYNC_CLOSING)) {
1510 if (ch->asyncflags & ASYNC_CLOSING)
Jiri Slaby95e07912007-10-18 03:06:25 -07001511 wait_for_completion_interruptible(&ch->close_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512#ifdef SERIAL_DO_RESTART
1513 if (ch->asyncflags & ASYNC_HUP_NOTIFY)
1514 return (-EAGAIN);
1515 else
1516 return (-ERESTARTSYS);
1517#else
1518 return (-EAGAIN);
1519#endif
1520 }
1521 /*
1522 * If non-blocking mode is set, then make the check up front
1523 * and then exit.
1524 */
1525 if (filp->f_flags & O_NONBLOCK) {
1526 ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
1527 return (0);
1528 }
1529 /*
1530 * Block waiting for the carrier detect and the line to become free
1531 */
1532 retval = 0;
1533 add_wait_queue(&ch->open_wait, &wait);
Jiri Slaby7aeb95d2007-10-18 03:06:24 -07001534 pr_debug("block_til_ready before block: ttys%d, count = %d\n",
Jiri Slabyb4173f42008-04-30 00:53:40 -07001535 tty->index, ch->count);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001536 spin_lock_bh(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 if (!tty_hung_up_p(filp))
1538 ch->count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 ch->blocked_open++;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001540 spin_unlock_bh(&moxa_lock);
Alan Cox33f0f882006-01-09 20:54:13 -08001541
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 while (1) {
1543 set_current_state(TASK_INTERRUPTIBLE);
1544 if (tty_hung_up_p(filp) ||
1545 !(ch->asyncflags & ASYNC_INITIALIZED)) {
1546#ifdef SERIAL_DO_RESTART
1547 if (ch->asyncflags & ASYNC_HUP_NOTIFY)
1548 retval = -EAGAIN;
1549 else
1550 retval = -ERESTARTSYS;
1551#else
1552 retval = -EAGAIN;
1553#endif
1554 break;
1555 }
1556 if (!(ch->asyncflags & ASYNC_CLOSING) && (do_clocal ||
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001557 ch->DCDState))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 break;
1559
1560 if (signal_pending(current)) {
1561 retval = -ERESTARTSYS;
1562 break;
1563 }
1564 schedule();
1565 }
1566 set_current_state(TASK_RUNNING);
1567 remove_wait_queue(&ch->open_wait, &wait);
Alan Cox33f0f882006-01-09 20:54:13 -08001568
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001569 spin_lock_bh(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 if (!tty_hung_up_p(filp))
1571 ch->count++;
1572 ch->blocked_open--;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001573 spin_unlock_bh(&moxa_lock);
Jiri Slaby7aeb95d2007-10-18 03:06:24 -07001574 pr_debug("block_til_ready after blocking: ttys%d, count = %d\n",
Jiri Slabyb4173f42008-04-30 00:53:40 -07001575 tty->index, ch->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 if (retval)
1577 return (retval);
Alan Cox33f0f882006-01-09 20:54:13 -08001578 /* FIXME: review to see if we need to use set_bit on these */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
Alan Cox33f0f882006-01-09 20:54:13 -08001580 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581}
1582
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001583static void moxa_setup_empty_event(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001585 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001587 spin_lock_bh(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 ch->statusflags |= EMPTYWAIT;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001589 spin_unlock_bh(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590}
1591
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001592static void moxa_shut_down(struct moxa_port *ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593{
1594 struct tty_struct *tp;
1595
1596 if (!(ch->asyncflags & ASYNC_INITIALIZED))
1597 return;
1598
1599 tp = ch->tty;
1600
Jiri Slabyb4173f42008-04-30 00:53:40 -07001601 MoxaPortDisable(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602
1603 /*
1604 * If we're a modem control device and HUPCL is on, drop RTS & DTR.
1605 */
1606 if (tp->termios->c_cflag & HUPCL)
Jiri Slabyb4173f42008-04-30 00:53:40 -07001607 MoxaPortLineCtrl(ch, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
1609 ch->asyncflags &= ~ASYNC_INITIALIZED;
1610}
1611
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612/*****************************************************************************
1613 * Driver level functions: *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 *****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
Jiri Slabyb4173f42008-04-30 00:53:40 -07001616static void MoxaPortFlushData(struct moxa_port *port, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617{
1618 void __iomem *ofsAddr;
1619 if ((mode < 0) || (mode > 2))
1620 return;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001621 ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 moxafunc(ofsAddr, FC_FlushQueue, mode);
1623 if (mode != 1) {
Jiri Slabyb4173f42008-04-30 00:53:40 -07001624 port->lowChkFlag = 0;
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001625 moxa_low_water_check(ofsAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 }
1627}
1628
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629/*****************************************************************************
1630 * Port level functions: *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 * 2. MoxaPortEnable(int port); *
1632 * 3. MoxaPortDisable(int port); *
1633 * 4. MoxaPortGetMaxBaud(int port); *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 * 6. MoxaPortSetBaud(int port, long baud); *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 * 8. MoxaPortSetTermio(int port, unsigned char *termio); *
1636 * 9. MoxaPortGetLineOut(int port, int *dtrState, int *rtsState); *
1637 * 10. MoxaPortLineCtrl(int port, int dtrState, int rtsState); *
1638 * 11. MoxaPortFlowCtrl(int port, int rts, int cts, int rx, int tx,int xany); *
1639 * 12. MoxaPortLineStatus(int port); *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 * 15. MoxaPortFlushData(int port, int mode); *
1641 * 16. MoxaPortWriteData(int port, unsigned char * buffer, int length); *
Alan Cox33f0f882006-01-09 20:54:13 -08001642 * 17. MoxaPortReadData(int port, struct tty_struct *tty); *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 * 20. MoxaPortTxQueue(int port); *
1644 * 21. MoxaPortTxFree(int port); *
1645 * 22. MoxaPortRxQueue(int port); *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 * 24. MoxaPortTxDisable(int port); *
1647 * 25. MoxaPortTxEnable(int port); *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 * 27. MoxaPortResetBrkCnt(int port); *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 *****************************************************************************/
1650/*
1651 * Moxa Port Number Description:
1652 *
1653 * MOXA serial driver supports up to 4 MOXA-C218/C320 boards. And,
1654 * the port number using in MOXA driver functions will be 0 to 31 for
1655 * first MOXA board, 32 to 63 for second, 64 to 95 for third and 96
1656 * to 127 for fourth. For example, if you setup three MOXA boards,
1657 * first board is C218, second board is C320-16 and third board is
1658 * C320-32. The port number of first board (C218 - 8 ports) is from
1659 * 0 to 7. The port number of second board (C320 - 16 ports) is form
1660 * 32 to 47. The port number of third board (C320 - 32 ports) is from
1661 * 64 to 95. And those port numbers form 8 to 31, 48 to 63 and 96 to
1662 * 127 will be invalid.
1663 *
1664 *
1665 * Moxa Functions Description:
1666 *
1667 * Function 1: Driver initialization routine, this routine must be
1668 * called when initialized driver.
1669 * Syntax:
1670 * void MoxaDriverInit();
1671 *
1672 *
1673 * Function 2: Moxa driver private IOCTL command processing.
1674 * Syntax:
1675 * int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port);
1676 *
1677 * unsigned int cmd : IOCTL command
1678 * unsigned long arg : IOCTL argument
1679 * int port : port number (0 - 127)
1680 *
1681 * return: 0 (OK)
1682 * -EINVAL
1683 * -ENOIOCTLCMD
1684 *
1685 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 * Function 6: Enable this port to start Tx/Rx data.
1687 * Syntax:
1688 * void MoxaPortEnable(int port);
1689 * int port : port number (0 - 127)
1690 *
1691 *
1692 * Function 7: Disable this port
1693 * Syntax:
1694 * void MoxaPortDisable(int port);
1695 * int port : port number (0 - 127)
1696 *
1697 *
1698 * Function 8: Get the maximun available baud rate of this port.
1699 * Syntax:
1700 * long MoxaPortGetMaxBaud(int port);
1701 * int port : port number (0 - 127)
1702 *
1703 * return: 0 : this port is invalid
1704 * 38400/57600/115200 bps
1705 *
1706 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 * Function 10: Setting baud rate of this port.
1708 * Syntax:
1709 * long MoxaPortSetBaud(int port, long baud);
1710 * int port : port number (0 - 127)
1711 * long baud : baud rate (50 - 115200)
1712 *
1713 * return: 0 : this port is invalid or baud < 50
1714 * 50 - 115200 : the real baud rate set to the port, if
1715 * the argument baud is large than maximun
1716 * available baud rate, the real setting
1717 * baud rate will be the maximun baud rate.
1718 *
1719 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 * Function 12: Configure the port.
1721 * Syntax:
Alan Cox606d0992006-12-08 02:38:45 -08001722 * int MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 * int port : port number (0 - 127)
Alan Cox606d0992006-12-08 02:38:45 -08001724 * struct ktermios * termio : termio structure pointer
Alan Coxc7bce302006-09-30 23:27:24 -07001725 * speed_t baud : baud rate
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 *
1727 * return: -1 : this port is invalid or termio == NULL
1728 * 0 : setting O.K.
1729 *
1730 *
1731 * Function 13: Get the DTR/RTS state of this port.
1732 * Syntax:
1733 * int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState);
1734 * int port : port number (0 - 127)
1735 * int * dtrState : pointer to INT to receive the current DTR
1736 * state. (if NULL, this function will not
1737 * write to this address)
1738 * int * rtsState : pointer to INT to receive the current RTS
1739 * state. (if NULL, this function will not
1740 * write to this address)
1741 *
1742 * return: -1 : this port is invalid
1743 * 0 : O.K.
1744 *
1745 *
1746 * Function 14: Setting the DTR/RTS output state of this port.
1747 * Syntax:
1748 * void MoxaPortLineCtrl(int port, int dtrState, int rtsState);
1749 * int port : port number (0 - 127)
1750 * int dtrState : DTR output state (0: off, 1: on)
1751 * int rtsState : RTS output state (0: off, 1: on)
1752 *
1753 *
1754 * Function 15: Setting the flow control of this port.
1755 * Syntax:
1756 * void MoxaPortFlowCtrl(int port, int rtsFlow, int ctsFlow, int rxFlow,
1757 * int txFlow,int xany);
1758 * int port : port number (0 - 127)
1759 * int rtsFlow : H/W RTS flow control (0: no, 1: yes)
1760 * int ctsFlow : H/W CTS flow control (0: no, 1: yes)
1761 * int rxFlow : S/W Rx XON/XOFF flow control (0: no, 1: yes)
1762 * int txFlow : S/W Tx XON/XOFF flow control (0: no, 1: yes)
1763 * int xany : S/W XANY flow control (0: no, 1: yes)
1764 *
1765 *
1766 * Function 16: Get ths line status of this port
1767 * Syntax:
1768 * int MoxaPortLineStatus(int port);
1769 * int port : port number (0 - 127)
1770 *
1771 * return: Bit 0 - CTS state (0: off, 1: on)
1772 * Bit 1 - DSR state (0: off, 1: on)
1773 * Bit 2 - DCD state (0: off, 1: on)
1774 *
1775 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 * Function 19: Flush the Rx/Tx buffer data of this port.
1777 * Syntax:
1778 * void MoxaPortFlushData(int port, int mode);
1779 * int port : port number (0 - 127)
1780 * int mode
1781 * 0 : flush the Rx buffer
1782 * 1 : flush the Tx buffer
1783 * 2 : flush the Rx and Tx buffer
1784 *
1785 *
1786 * Function 20: Write data.
1787 * Syntax:
1788 * int MoxaPortWriteData(int port, unsigned char * buffer, int length);
1789 * int port : port number (0 - 127)
1790 * unsigned char * buffer : pointer to write data buffer.
1791 * int length : write data length
1792 *
1793 * return: 0 - length : real write data length
1794 *
1795 *
1796 * Function 21: Read data.
1797 * Syntax:
Alan Cox33f0f882006-01-09 20:54:13 -08001798 * int MoxaPortReadData(int port, struct tty_struct *tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 * int port : port number (0 - 127)
Alan Cox33f0f882006-01-09 20:54:13 -08001800 * struct tty_struct *tty : tty for data
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 *
1802 * return: 0 - length : real read data length
1803 *
1804 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 * Function 24: Get the Tx buffer current queued data bytes
1806 * Syntax:
1807 * int MoxaPortTxQueue(int port);
1808 * int port : port number (0 - 127)
1809 *
1810 * return: .. : Tx buffer current queued data bytes
1811 *
1812 *
1813 * Function 25: Get the Tx buffer current free space
1814 * Syntax:
1815 * int MoxaPortTxFree(int port);
1816 * int port : port number (0 - 127)
1817 *
1818 * return: .. : Tx buffer current free space
1819 *
1820 *
1821 * Function 26: Get the Rx buffer current queued data bytes
1822 * Syntax:
1823 * int MoxaPortRxQueue(int port);
1824 * int port : port number (0 - 127)
1825 *
1826 * return: .. : Rx buffer current queued data bytes
1827 *
1828 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 * Function 28: Disable port data transmission.
1830 * Syntax:
1831 * void MoxaPortTxDisable(int port);
1832 * int port : port number (0 - 127)
1833 *
1834 *
1835 * Function 29: Enable port data transmission.
1836 * Syntax:
1837 * void MoxaPortTxEnable(int port);
1838 * int port : port number (0 - 127)
1839 *
1840 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 * Function 31: Get the received BREAK signal count and reset it.
1842 * Syntax:
1843 * int MoxaPortResetBrkCnt(int port);
1844 * int port : port number (0 - 127)
1845 *
1846 * return: 0 - .. : BREAK signal count
1847 *
1848 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850
Jiri Slabyb4173f42008-04-30 00:53:40 -07001851static void MoxaPortEnable(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852{
1853 void __iomem *ofsAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 short lowwater = 512;
1855
Jiri Slabyb4173f42008-04-30 00:53:40 -07001856 ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 writew(lowwater, ofsAddr + Low_water);
Jiri Slabyb4173f42008-04-30 00:53:40 -07001858 if (port->board->boardType == MOXA_BOARD_C320_ISA ||
1859 port->board->boardType == MOXA_BOARD_C320_PCI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 moxafunc(ofsAddr, FC_SetBreakIrq, 0);
1861 } else {
1862 writew(readw(ofsAddr + HostStat) | WakeupBreak, ofsAddr + HostStat);
1863 }
1864
1865 moxafunc(ofsAddr, FC_SetLineIrq, Magic_code);
1866 moxafunc(ofsAddr, FC_FlushQueue, 2);
1867
1868 moxafunc(ofsAddr, FC_EnableCH, Magic_code);
1869 MoxaPortLineStatus(port);
1870}
1871
Jiri Slabyb4173f42008-04-30 00:53:40 -07001872static void MoxaPortDisable(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001874 void __iomem *ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
1876 moxafunc(ofsAddr, FC_SetFlowCtl, 0); /* disable flow control */
1877 moxafunc(ofsAddr, FC_ClrLineIrq, Magic_code);
1878 writew(0, ofsAddr + HostStat);
1879 moxafunc(ofsAddr, FC_DisableCH, Magic_code);
1880}
1881
Jiri Slabyb4173f42008-04-30 00:53:40 -07001882static long MoxaPortGetMaxBaud(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001884 if (port->board->boardType == MOXA_BOARD_C320_ISA ||
1885 port->board->boardType == MOXA_BOARD_C320_PCI)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 return (460800L);
1887 else
1888 return (921600L);
1889}
1890
1891
Jiri Slabyb4173f42008-04-30 00:53:40 -07001892static long MoxaPortSetBaud(struct moxa_port *port, long baud)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893{
1894 void __iomem *ofsAddr;
1895 long max, clock;
1896 unsigned int val;
1897
1898 if ((baud < 50L) || ((max = MoxaPortGetMaxBaud(port)) == 0))
1899 return (0);
Jiri Slabyb4173f42008-04-30 00:53:40 -07001900 ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 if (baud > max)
1902 baud = max;
1903 if (max == 38400L)
1904 clock = 614400L; /* for 9.8304 Mhz : max. 38400 bps */
1905 else if (max == 57600L)
1906 clock = 691200L; /* for 11.0592 Mhz : max. 57600 bps */
1907 else
1908 clock = 921600L; /* for 14.7456 Mhz : max. 115200 bps */
1909 val = clock / baud;
1910 moxafunc(ofsAddr, FC_SetBaud, val);
1911 baud = clock / val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 return (baud);
1913}
1914
Jiri Slabyb4173f42008-04-30 00:53:40 -07001915static int MoxaPortSetTermio(struct moxa_port *port, struct ktermios *termio,
1916 speed_t baud)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917{
1918 void __iomem *ofsAddr;
1919 tcflag_t cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 tcflag_t mode = 0;
1921
Jiri Slabyb4173f42008-04-30 00:53:40 -07001922 ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 cflag = termio->c_cflag; /* termio->c_cflag */
1924
1925 mode = termio->c_cflag & CSIZE;
1926 if (mode == CS5)
1927 mode = MX_CS5;
1928 else if (mode == CS6)
1929 mode = MX_CS6;
1930 else if (mode == CS7)
1931 mode = MX_CS7;
1932 else if (mode == CS8)
1933 mode = MX_CS8;
1934
1935 if (termio->c_cflag & CSTOPB) {
1936 if (mode == MX_CS5)
1937 mode |= MX_STOP15;
1938 else
1939 mode |= MX_STOP2;
1940 } else
1941 mode |= MX_STOP1;
1942
1943 if (termio->c_cflag & PARENB) {
1944 if (termio->c_cflag & PARODD)
1945 mode |= MX_PARODD;
1946 else
1947 mode |= MX_PAREVEN;
1948 } else
1949 mode |= MX_PARNONE;
1950
1951 moxafunc(ofsAddr, FC_SetDataMode, (ushort) mode);
1952
Jiri Slabyb4173f42008-04-30 00:53:40 -07001953 if (port->board->boardType == MOXA_BOARD_C320_ISA ||
1954 port->board->boardType == MOXA_BOARD_C320_PCI) {
Alan Coxc7bce302006-09-30 23:27:24 -07001955 if (baud >= 921600L)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 return (-1);
1957 }
Alan Coxdb1acaa2008-02-08 04:18:43 -08001958 baud = MoxaPortSetBaud(port, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959
1960 if (termio->c_iflag & (IXON | IXOFF | IXANY)) {
1961 writeb(termio->c_cc[VSTART], ofsAddr + FuncArg);
1962 writeb(termio->c_cc[VSTOP], ofsAddr + FuncArg1);
1963 writeb(FC_SetXonXoff, ofsAddr + FuncCode);
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001964 moxa_wait_finish(ofsAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965
1966 }
Alan Coxdb1acaa2008-02-08 04:18:43 -08001967 return (baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968}
1969
Jiri Slabyb4173f42008-04-30 00:53:40 -07001970static int MoxaPortGetLineOut(struct moxa_port *port, int *dtrState,
1971 int *rtsState)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972{
1973
Jiri Slabyb4173f42008-04-30 00:53:40 -07001974 if (dtrState)
1975 *dtrState = !!(port->lineCtrl & DTR_ON);
1976 if (rtsState)
1977 *rtsState = !!(port->lineCtrl & RTS_ON);
1978
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 return (0);
1980}
1981
Jiri Slabyb4173f42008-04-30 00:53:40 -07001982static void MoxaPortLineCtrl(struct moxa_port *port, int dtr, int rts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001984 int mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 if (dtr)
1987 mode |= DTR_ON;
1988 if (rts)
1989 mode |= RTS_ON;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001990 port->lineCtrl = mode;
1991 moxafunc(port->tableAddr, FC_LineControl, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992}
1993
Jiri Slabyb4173f42008-04-30 00:53:40 -07001994static void MoxaPortFlowCtrl(struct moxa_port *port, int rts, int cts,
1995 int txflow, int rxflow, int txany)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001997 int mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 if (rts)
2000 mode |= RTS_FlowCtl;
2001 if (cts)
2002 mode |= CTS_FlowCtl;
2003 if (txflow)
2004 mode |= Tx_FlowCtl;
2005 if (rxflow)
2006 mode |= Rx_FlowCtl;
2007 if (txany)
2008 mode |= IXM_IXANY;
Jiri Slabyb4173f42008-04-30 00:53:40 -07002009 moxafunc(port->tableAddr, FC_SetFlowCtl, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010}
2011
Jiri Slabyb4173f42008-04-30 00:53:40 -07002012static int MoxaPortLineStatus(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013{
2014 void __iomem *ofsAddr;
2015 int val;
2016
Jiri Slabyb4173f42008-04-30 00:53:40 -07002017 ofsAddr = port->tableAddr;
2018 if (port->board->boardType == MOXA_BOARD_C320_ISA ||
2019 port->board->boardType == MOXA_BOARD_C320_PCI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 moxafunc(ofsAddr, FC_LineStatus, 0);
2021 val = readw(ofsAddr + FuncArg);
2022 } else {
2023 val = readw(ofsAddr + FlagStat) >> 4;
2024 }
2025 val &= 0x0B;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07002026 if (val & 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 val |= 4;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07002028 moxa_new_dcdstate(port, val & 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 val &= 7;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07002030 return val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031}
2032
Jiri Slabyb4173f42008-04-30 00:53:40 -07002033static int MoxaPortWriteData(struct moxa_port *port, unsigned char *buffer,
2034 int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035{
2036 int c, total, i;
2037 ushort tail;
2038 int cnt;
2039 ushort head, tx_mask, spage, epage;
2040 ushort pageno, pageofs, bufhead;
2041 void __iomem *baseAddr, *ofsAddr, *ofs;
2042
Jiri Slabyb4173f42008-04-30 00:53:40 -07002043 ofsAddr = port->tableAddr;
2044 baseAddr = port->board->basemem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 tx_mask = readw(ofsAddr + TX_mask);
2046 spage = readw(ofsAddr + Page_txb);
2047 epage = readw(ofsAddr + EndPage_txb);
2048 tail = readw(ofsAddr + TXwptr);
2049 head = readw(ofsAddr + TXrptr);
2050 c = (head > tail) ? (head - tail - 1)
2051 : (head - tail + tx_mask);
2052 if (c > len)
2053 c = len;
Jiri Slabyb4173f42008-04-30 00:53:40 -07002054 moxaLog.txcnt[port->tty->index] += c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 total = c;
2056 if (spage == epage) {
2057 bufhead = readw(ofsAddr + Ofs_txb);
2058 writew(spage, baseAddr + Control_reg);
2059 while (c > 0) {
2060 if (head > tail)
2061 len = head - tail - 1;
2062 else
2063 len = tx_mask + 1 - tail;
2064 len = (c > len) ? len : c;
2065 ofs = baseAddr + DynPage_addr + bufhead + tail;
2066 for (i = 0; i < len; i++)
2067 writeb(*buffer++, ofs + i);
2068 tail = (tail + len) & tx_mask;
2069 c -= len;
2070 }
2071 writew(tail, ofsAddr + TXwptr);
2072 } else {
2073 len = c;
2074 pageno = spage + (tail >> 13);
2075 pageofs = tail & Page_mask;
2076 do {
2077 cnt = Page_size - pageofs;
2078 if (cnt > c)
2079 cnt = c;
2080 c -= cnt;
2081 writeb(pageno, baseAddr + Control_reg);
2082 ofs = baseAddr + DynPage_addr + pageofs;
2083 for (i = 0; i < cnt; i++)
2084 writeb(*buffer++, ofs + i);
2085 if (c == 0) {
2086 writew((tail + len) & tx_mask, ofsAddr + TXwptr);
2087 break;
2088 }
2089 if (++pageno == epage)
2090 pageno = spage;
2091 pageofs = 0;
2092 } while (1);
2093 }
2094 writeb(1, ofsAddr + CD180TXirq); /* start to send */
2095 return (total);
2096}
2097
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07002098static int MoxaPortReadData(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099{
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07002100 struct tty_struct *tty = port->tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 register ushort head, pageofs;
2102 int i, count, cnt, len, total, remain;
2103 ushort tail, rx_mask, spage, epage;
2104 ushort pageno, bufhead;
2105 void __iomem *baseAddr, *ofsAddr, *ofs;
2106
Jiri Slabyb4173f42008-04-30 00:53:40 -07002107 ofsAddr = port->tableAddr;
2108 baseAddr = port->board->basemem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 head = readw(ofsAddr + RXrptr);
2110 tail = readw(ofsAddr + RXwptr);
2111 rx_mask = readw(ofsAddr + RX_mask);
2112 spage = readw(ofsAddr + Page_rxb);
2113 epage = readw(ofsAddr + EndPage_rxb);
2114 count = (tail >= head) ? (tail - head)
2115 : (tail - head + rx_mask + 1);
2116 if (count == 0)
Alan Cox33f0f882006-01-09 20:54:13 -08002117 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118
Alan Cox33f0f882006-01-09 20:54:13 -08002119 total = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 remain = count - total;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07002121 moxaLog.rxcnt[tty->index] += total;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 count = total;
2123 if (spage == epage) {
2124 bufhead = readw(ofsAddr + Ofs_rxb);
2125 writew(spage, baseAddr + Control_reg);
2126 while (count > 0) {
2127 if (tail >= head)
2128 len = tail - head;
2129 else
2130 len = rx_mask + 1 - head;
2131 len = (count > len) ? len : count;
2132 ofs = baseAddr + DynPage_addr + bufhead + head;
2133 for (i = 0; i < len; i++)
Alan Cox33f0f882006-01-09 20:54:13 -08002134 tty_insert_flip_char(tty, readb(ofs + i), TTY_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 head = (head + len) & rx_mask;
2136 count -= len;
2137 }
2138 writew(head, ofsAddr + RXrptr);
2139 } else {
2140 len = count;
2141 pageno = spage + (head >> 13);
2142 pageofs = head & Page_mask;
2143 do {
2144 cnt = Page_size - pageofs;
2145 if (cnt > count)
2146 cnt = count;
2147 count -= cnt;
2148 writew(pageno, baseAddr + Control_reg);
2149 ofs = baseAddr + DynPage_addr + pageofs;
2150 for (i = 0; i < cnt; i++)
Alan Cox33f0f882006-01-09 20:54:13 -08002151 tty_insert_flip_char(tty, readb(ofs + i), TTY_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 if (count == 0) {
2153 writew((head + len) & rx_mask, ofsAddr + RXrptr);
2154 break;
2155 }
2156 if (++pageno == epage)
2157 pageno = spage;
2158 pageofs = 0;
2159 } while (1);
2160 }
2161 if ((readb(ofsAddr + FlagStat) & Xoff_state) && (remain < LowWater)) {
2162 moxaLowWaterChk = 1;
Jiri Slabyb4173f42008-04-30 00:53:40 -07002163 port->lowChkFlag = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 }
2165 return (total);
2166}
2167
2168
Jiri Slabyb4173f42008-04-30 00:53:40 -07002169static int MoxaPortTxQueue(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002171 void __iomem *ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 ushort rptr, wptr, mask;
2173 int len;
2174
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 rptr = readw(ofsAddr + TXrptr);
2176 wptr = readw(ofsAddr + TXwptr);
2177 mask = readw(ofsAddr + TX_mask);
2178 len = (wptr - rptr) & mask;
2179 return (len);
2180}
2181
Jiri Slabyb4173f42008-04-30 00:53:40 -07002182static int MoxaPortTxFree(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002184 void __iomem *ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 ushort rptr, wptr, mask;
2186 int len;
2187
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 rptr = readw(ofsAddr + TXrptr);
2189 wptr = readw(ofsAddr + TXwptr);
2190 mask = readw(ofsAddr + TX_mask);
2191 len = mask - ((wptr - rptr) & mask);
2192 return (len);
2193}
2194
Jiri Slabyb4173f42008-04-30 00:53:40 -07002195static int MoxaPortRxQueue(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002197 void __iomem *ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 ushort rptr, wptr, mask;
2199 int len;
2200
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 rptr = readw(ofsAddr + RXrptr);
2202 wptr = readw(ofsAddr + RXwptr);
2203 mask = readw(ofsAddr + RX_mask);
2204 len = (wptr - rptr) & mask;
2205 return (len);
2206}
2207
2208
Jiri Slabyb4173f42008-04-30 00:53:40 -07002209static void MoxaPortTxDisable(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002211 moxafunc(port->tableAddr, FC_SetXoffState, Magic_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212}
2213
Jiri Slabyb4173f42008-04-30 00:53:40 -07002214static void MoxaPortTxEnable(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002216 moxafunc(port->tableAddr, FC_SetXonState, Magic_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217}
2218
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002219static int moxa_get_serial_info(struct moxa_port *info,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 struct serial_struct __user *retinfo)
2221{
2222 struct serial_struct tmp;
2223
2224 memset(&tmp, 0, sizeof(tmp));
2225 tmp.type = info->type;
Jiri Slabyb4173f42008-04-30 00:53:40 -07002226 tmp.line = info->tty->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 tmp.port = 0;
2228 tmp.irq = 0;
2229 tmp.flags = info->asyncflags;
2230 tmp.baud_base = 921600;
2231 tmp.close_delay = info->close_delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 tmp.custom_divisor = 0;
2233 tmp.hub6 = 0;
2234 if(copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2235 return -EFAULT;
2236 return (0);
2237}
2238
2239
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002240static int moxa_set_serial_info(struct moxa_port *info,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 struct serial_struct __user *new_info)
2242{
2243 struct serial_struct new_serial;
2244
2245 if(copy_from_user(&new_serial, new_info, sizeof(new_serial)))
2246 return -EFAULT;
2247
2248 if ((new_serial.irq != 0) ||
2249 (new_serial.port != 0) ||
2250// (new_serial.type != info->type) ||
2251 (new_serial.custom_divisor != 0) ||
2252 (new_serial.baud_base != 921600))
2253 return (-EPERM);
2254
2255 if (!capable(CAP_SYS_ADMIN)) {
2256 if (((new_serial.flags & ~ASYNC_USR_MASK) !=
2257 (info->asyncflags & ~ASYNC_USR_MASK)))
2258 return (-EPERM);
2259 } else {
2260 info->close_delay = new_serial.close_delay * HZ / 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 }
2262
2263 new_serial.flags = (new_serial.flags & ~ASYNC_FLAGS);
2264 new_serial.flags |= (info->asyncflags & ASYNC_FLAGS);
2265
2266 if (new_serial.type == PORT_16550A) {
Jiri Slabyb4173f42008-04-30 00:53:40 -07002267 MoxaSetFifo(info, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 } else {
Jiri Slabyb4173f42008-04-30 00:53:40 -07002269 MoxaSetFifo(info, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 }
2271
2272 info->type = new_serial.type;
2273 return (0);
2274}
2275
2276
2277
2278/*****************************************************************************
2279 * Static local functions: *
2280 *****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281
Jiri Slabyb4173f42008-04-30 00:53:40 -07002282static void MoxaSetFifo(struct moxa_port *port, int enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002284 void __iomem *ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285
2286 if (!enable) {
2287 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 0);
2288 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 1);
2289 } else {
2290 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 3);
2291 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 16);
2292 }
2293}