blob: abcc16eba44f3a11aae89684a72bbf98e97224cc [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);
Jiri Slaby2108eba2008-04-30 00:53:44 -0700227static int MoxaPortWriteData(struct moxa_port *, const 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
Jiri Slaby2a541342008-04-30 00:53:45 -0700849 spin_lock_bh(&moxa_lock);
Jiri Slaby810ab092008-04-30 00:53:41 -0700850 brd->ready = 1;
Jiri Slaby0bcc4ca2008-04-30 00:53:42 -0700851 if (!timer_pending(&moxaTimer))
852 mod_timer(&moxaTimer, jiffies + HZ / 50);
Jiri Slaby2a541342008-04-30 00:53:45 -0700853 spin_unlock_bh(&moxa_lock);
Jiri Slaby0bcc4ca2008-04-30 00:53:42 -0700854
Jiri Slaby810ab092008-04-30 00:53:41 -0700855 return 0;
856err_free:
857 kfree(brd->ports);
858err:
Jiri Slaby03718232008-04-30 00:53:39 -0700859 return ret;
860}
861
Jiri Slaby810ab092008-04-30 00:53:41 -0700862static void moxa_board_deinit(struct moxa_board_conf *brd)
863{
Jiri Slaby7bcf97d2008-04-30 00:53:43 -0700864 spin_lock_bh(&moxa_lock);
Jiri Slaby810ab092008-04-30 00:53:41 -0700865 brd->ready = 0;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -0700866 spin_unlock_bh(&moxa_lock);
Jiri Slaby810ab092008-04-30 00:53:41 -0700867 iounmap(brd->basemem);
868 brd->basemem = NULL;
869 kfree(brd->ports);
870}
871
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872#ifdef CONFIG_PCI
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800873static int __devinit moxa_pci_probe(struct pci_dev *pdev,
874 const struct pci_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875{
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800876 struct moxa_board_conf *board;
877 unsigned int i;
878 int board_type = ent->driver_data;
879 int retval;
880
881 retval = pci_enable_device(pdev);
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700882 if (retval) {
883 dev_err(&pdev->dev, "can't enable pci device\n");
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800884 goto err;
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700885 }
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800886
887 for (i = 0; i < MAX_BOARDS; i++)
888 if (moxa_boards[i].basemem == NULL)
889 break;
890
891 retval = -ENODEV;
892 if (i >= MAX_BOARDS) {
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700893 dev_warn(&pdev->dev, "more than %u MOXA Intellio family boards "
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800894 "found. Board is ignored.\n", MAX_BOARDS);
895 goto err;
896 }
897
898 board = &moxa_boards[i];
Jiri Slabye46a5e32008-04-30 00:53:37 -0700899
900 retval = pci_request_region(pdev, 2, "moxa-base");
901 if (retval) {
902 dev_err(&pdev->dev, "can't request pci region 2\n");
903 goto err;
904 }
905
906 board->basemem = ioremap(pci_resource_start(pdev, 2), 0x4000);
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700907 if (board->basemem == NULL) {
908 dev_err(&pdev->dev, "can't remap io space 2\n");
Jiri Slabye46a5e32008-04-30 00:53:37 -0700909 goto err_reg;
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700910 }
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800911
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 board->boardType = board_type;
913 switch (board_type) {
914 case MOXA_BOARD_C218_ISA:
915 case MOXA_BOARD_C218_PCI:
916 board->numPorts = 8;
917 break;
918
919 case MOXA_BOARD_CP204J:
920 board->numPorts = 4;
921 break;
922 default:
923 board->numPorts = 0;
924 break;
925 }
926 board->busType = MOXA_BUS_TYPE_PCI;
Jiri Slabya784bf72007-02-10 01:45:36 -0800927
Jiri Slaby03718232008-04-30 00:53:39 -0700928 retval = moxa_init_board(board, &pdev->dev);
929 if (retval)
930 goto err_base;
931
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800932 pci_set_drvdata(pdev, board);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
934 return (0);
Jiri Slaby03718232008-04-30 00:53:39 -0700935err_base:
936 iounmap(board->basemem);
937 board->basemem = NULL;
Jiri Slabye46a5e32008-04-30 00:53:37 -0700938err_reg:
939 pci_release_region(pdev, 2);
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800940err:
941 return retval;
942}
943
944static void __devexit moxa_pci_remove(struct pci_dev *pdev)
945{
946 struct moxa_board_conf *brd = pci_get_drvdata(pdev);
947
Jiri Slaby810ab092008-04-30 00:53:41 -0700948 moxa_board_deinit(brd);
949
Jiri Slabye46a5e32008-04-30 00:53:37 -0700950 pci_release_region(pdev, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951}
Jiri Slabya784bf72007-02-10 01:45:36 -0800952
953static struct pci_driver moxa_pci_driver = {
954 .name = "moxa",
955 .id_table = moxa_pcibrds,
956 .probe = moxa_pci_probe,
957 .remove = __devexit_p(moxa_pci_remove)
958};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959#endif /* CONFIG_PCI */
960
961static int __init moxa_init(void)
962{
Jiri Slaby810ab092008-04-30 00:53:41 -0700963 unsigned int isabrds = 0;
Jiri Slabyd353eca2008-04-30 00:53:37 -0700964 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700966 printk(KERN_INFO "MOXA Intellio family driver version %s\n",
967 MOXA_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 moxaDriver = alloc_tty_driver(MAX_PORTS + 1);
969 if (!moxaDriver)
970 return -ENOMEM;
971
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 moxaDriver->owner = THIS_MODULE;
Sergey Vlasov9b4e3b12005-09-03 16:26:49 +0100973 moxaDriver->name = "ttyMX";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 moxaDriver->major = ttymajor;
975 moxaDriver->minor_start = 0;
976 moxaDriver->type = TTY_DRIVER_TYPE_SERIAL;
977 moxaDriver->subtype = SERIAL_TYPE_NORMAL;
978 moxaDriver->init_termios = tty_std_termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 moxaDriver->init_termios.c_cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
Alan Cox606d0992006-12-08 02:38:45 -0800980 moxaDriver->init_termios.c_ispeed = 9600;
981 moxaDriver->init_termios.c_ospeed = 9600;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 moxaDriver->flags = TTY_DRIVER_REAL_RAW;
983 tty_set_operations(moxaDriver, &moxa_ops);
984
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700985 pr_debug("Moxa tty devices major number = %d\n", ttymajor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
987 if (tty_register_driver(moxaDriver)) {
988 printk(KERN_ERR "Couldn't install MOXA Smartio family driver !\n");
989 put_tty_driver(moxaDriver);
990 return -1;
991 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992
Jiri Slabyd353eca2008-04-30 00:53:37 -0700993 /* Find the boards defined from module args. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994#ifdef MODULE
Jiri Slabyd353eca2008-04-30 00:53:37 -0700995 {
996 struct moxa_board_conf *brd = moxa_boards;
Jiri Slaby810ab092008-04-30 00:53:41 -0700997 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 for (i = 0; i < MAX_BOARDS; i++) {
Jiri Slabyd353eca2008-04-30 00:53:37 -0700999 if (!baseaddr[i])
1000 break;
1001 if (type[i] == MOXA_BOARD_C218_ISA ||
1002 type[i] == MOXA_BOARD_C320_ISA) {
Jiri Slaby7aeb95d2007-10-18 03:06:24 -07001003 pr_debug("Moxa board %2d: %s board(baseAddr=%lx)\n",
Jiri Slabyd353eca2008-04-30 00:53:37 -07001004 isabrds + 1, moxa_brdname[type[i] - 1],
1005 baseaddr[i]);
1006 brd->boardType = type[i];
1007 brd->numPorts = type[i] == MOXA_BOARD_C218_ISA ? 8 :
1008 numports[i];
1009 brd->busType = MOXA_BUS_TYPE_ISA;
1010 brd->basemem = ioremap(baseaddr[i], 0x4000);
1011 if (!brd->basemem) {
1012 printk(KERN_ERR "moxa: can't remap %lx\n",
1013 baseaddr[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 continue;
1015 }
Jiri Slaby03718232008-04-30 00:53:39 -07001016 if (moxa_init_board(brd, NULL)) {
1017 iounmap(brd->basemem);
1018 brd->basemem = NULL;
1019 continue;
1020 }
Jiri Slabyd353eca2008-04-30 00:53:37 -07001021
1022 brd++;
1023 isabrds++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 }
1025 }
Jiri Slabyd353eca2008-04-30 00:53:37 -07001026 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027#endif
Jiri Slabya784bf72007-02-10 01:45:36 -08001028
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029#ifdef CONFIG_PCI
Jiri Slabya784bf72007-02-10 01:45:36 -08001030 retval = pci_register_driver(&moxa_pci_driver);
1031 if (retval) {
1032 printk(KERN_ERR "Can't register moxa pci driver!\n");
Jiri Slabyd353eca2008-04-30 00:53:37 -07001033 if (isabrds)
Jiri Slabya784bf72007-02-10 01:45:36 -08001034 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 }
1036#endif
Jiri Slabya784bf72007-02-10 01:45:36 -08001037
Jiri Slabya784bf72007-02-10 01:45:36 -08001038 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039}
1040
1041static void __exit moxa_exit(void)
1042{
1043 int i;
1044
Jiri Slaby9cde5bf2007-02-10 01:45:35 -08001045#ifdef CONFIG_PCI
Jiri Slabya784bf72007-02-10 01:45:36 -08001046 pci_unregister_driver(&moxa_pci_driver);
Jiri Slaby9cde5bf2007-02-10 01:45:35 -08001047#endif
Jiri Slabya784bf72007-02-10 01:45:36 -08001048
Jiri Slaby810ab092008-04-30 00:53:41 -07001049 for (i = 0; i < MAX_BOARDS; i++) /* ISA boards */
1050 if (moxa_boards[i].ready)
1051 moxa_board_deinit(&moxa_boards[i]);
Jiri Slaby2a541342008-04-30 00:53:45 -07001052
1053 del_timer_sync(&moxaTimer);
1054
1055 if (tty_unregister_driver(moxaDriver))
1056 printk(KERN_ERR "Couldn't unregister MOXA Intellio family "
1057 "serial driver\n");
1058 put_tty_driver(moxaDriver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059}
1060
1061module_init(moxa_init);
1062module_exit(moxa_exit);
1063
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064static int moxa_open(struct tty_struct *tty, struct file *filp)
1065{
Jiri Slaby810ab092008-04-30 00:53:41 -07001066 struct moxa_board_conf *brd;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001067 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 int port;
1069 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
Jiri Slaby11324ed2007-02-10 01:45:31 -08001071 port = tty->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 if (port == MAX_PORTS) {
Jiri Slaby74d7d972008-04-30 00:53:43 -07001073 return capable(CAP_SYS_ADMIN) ? 0 : -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 }
Jiri Slaby810ab092008-04-30 00:53:41 -07001075 brd = &moxa_boards[port / MAX_PORTS_PER_BOARD];
1076 if (!brd->ready)
1077 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
Jiri Slaby810ab092008-04-30 00:53:41 -07001079 ch = &brd->ports[port % MAX_PORTS_PER_BOARD];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 ch->count++;
1081 tty->driver_data = ch;
1082 ch->tty = tty;
1083 if (!(ch->asyncflags & ASYNC_INITIALIZED)) {
1084 ch->statusflags = 0;
Alan Coxdb1acaa2008-02-08 04:18:43 -08001085 moxa_set_tty_param(tty, tty->termios);
Jiri Slabyb4173f42008-04-30 00:53:40 -07001086 MoxaPortLineCtrl(ch, 1, 1);
1087 MoxaPortEnable(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 ch->asyncflags |= ASYNC_INITIALIZED;
1089 }
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001090 retval = moxa_block_till_ready(tty, filp, ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
1092 moxa_unthrottle(tty);
1093
1094 if (ch->type == PORT_16550A) {
Jiri Slabyb4173f42008-04-30 00:53:40 -07001095 MoxaSetFifo(ch, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 } else {
Jiri Slabyb4173f42008-04-30 00:53:40 -07001097 MoxaSetFifo(ch, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 }
1099
1100 return (retval);
1101}
1102
1103static void moxa_close(struct tty_struct *tty, struct file *filp)
1104{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001105 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 int port;
1107
Jiri Slaby11324ed2007-02-10 01:45:31 -08001108 port = tty->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 if (port == MAX_PORTS) {
1110 return;
1111 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 if (tty->driver_data == NULL) {
1113 return;
1114 }
1115 if (tty_hung_up_p(filp)) {
1116 return;
1117 }
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001118 ch = (struct moxa_port *) tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
1120 if ((tty->count == 1) && (ch->count != 1)) {
Jiri Slaby7aeb95d2007-10-18 03:06:24 -07001121 printk(KERN_WARNING "moxa_close: bad serial port count; "
1122 "tty->count is 1, ch->count is %d\n", ch->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 ch->count = 1;
1124 }
1125 if (--ch->count < 0) {
Jiri Slaby7aeb95d2007-10-18 03:06:24 -07001126 printk(KERN_WARNING "moxa_close: bad serial port count, "
1127 "device=%s\n", tty->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 ch->count = 0;
1129 }
1130 if (ch->count) {
1131 return;
1132 }
1133 ch->asyncflags |= ASYNC_CLOSING;
1134
1135 ch->cflag = tty->termios->c_cflag;
1136 if (ch->asyncflags & ASYNC_INITIALIZED) {
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001137 moxa_setup_empty_event(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 tty_wait_until_sent(tty, 30 * HZ); /* 30 seconds timeout */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 }
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001140 moxa_shut_down(ch);
Jiri Slabyb4173f42008-04-30 00:53:40 -07001141 MoxaPortFlushData(ch, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
1143 if (tty->driver->flush_buffer)
1144 tty->driver->flush_buffer(tty);
1145 tty_ldisc_flush(tty);
1146
1147 tty->closing = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 ch->tty = NULL;
1149 if (ch->blocked_open) {
1150 if (ch->close_delay) {
1151 msleep_interruptible(jiffies_to_msecs(ch->close_delay));
1152 }
1153 wake_up_interruptible(&ch->open_wait);
1154 }
1155 ch->asyncflags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
Jiri Slaby95e07912007-10-18 03:06:25 -07001156 complete_all(&ch->close_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157}
1158
1159static int moxa_write(struct tty_struct *tty,
1160 const unsigned char *buf, int count)
1161{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001162 struct moxa_port *ch = tty->driver_data;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001163 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 if (ch == NULL)
Jiri Slabyb4173f42008-04-30 00:53:40 -07001166 return 0;
Alan Cox33f0f882006-01-09 20:54:13 -08001167
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001168 spin_lock_bh(&moxa_lock);
Jiri Slaby2108eba2008-04-30 00:53:44 -07001169 len = MoxaPortWriteData(ch, buf, count);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001170 spin_unlock_bh(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
1172 /*********************************************
1173 if ( !(ch->statusflags & LOWWAIT) &&
1174 ((len != count) || (MoxaPortTxFree(port) <= 100)) )
1175 ************************************************/
1176 ch->statusflags |= LOWWAIT;
1177 return (len);
1178}
1179
1180static int moxa_write_room(struct tty_struct *tty)
1181{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001182 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
1184 if (tty->stopped)
1185 return (0);
Jiri Slabyb4173f42008-04-30 00:53:40 -07001186 ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 if (ch == NULL)
1188 return (0);
Jiri Slabyb4173f42008-04-30 00:53:40 -07001189 return MoxaPortTxFree(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190}
1191
1192static void moxa_flush_buffer(struct tty_struct *tty)
1193{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001194 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
1196 if (ch == NULL)
1197 return;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001198 MoxaPortFlushData(ch, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 tty_wakeup(tty);
1200}
1201
1202static int moxa_chars_in_buffer(struct tty_struct *tty)
1203{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001204 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 int chars;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
1207 /*
1208 * Sigh...I have to check if driver_data is NULL here, because
1209 * if an open() fails, the TTY subsystem eventually calls
1210 * tty_wait_until_sent(), which calls the driver's chars_in_buffer()
1211 * routine. And since the open() failed, we return 0 here. TDJ
1212 */
1213 if (ch == NULL)
1214 return (0);
Jiri Slabyb4173f42008-04-30 00:53:40 -07001215 chars = MoxaPortTxQueue(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 if (chars) {
1217 /*
1218 * Make it possible to wakeup anything waiting for output
1219 * in tty_ioctl.c, etc.
1220 */
1221 if (!(ch->statusflags & EMPTYWAIT))
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001222 moxa_setup_empty_event(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 }
1224 return (chars);
1225}
1226
1227static void moxa_flush_chars(struct tty_struct *tty)
1228{
1229 /*
1230 * Don't think I need this, because this is called to empty the TX
1231 * buffer for the 16450, 16550, etc.
1232 */
1233}
1234
1235static void moxa_put_char(struct tty_struct *tty, unsigned char c)
1236{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001237 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 if (ch == NULL)
1240 return;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001241 spin_lock_bh(&moxa_lock);
Jiri Slabyb4173f42008-04-30 00:53:40 -07001242 MoxaPortWriteData(ch, &c, 1);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001243 spin_unlock_bh(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 /************************************************
1245 if ( !(ch->statusflags & LOWWAIT) && (MoxaPortTxFree(port) <= 100) )
1246 *************************************************/
1247 ch->statusflags |= LOWWAIT;
1248}
1249
1250static int moxa_tiocmget(struct tty_struct *tty, struct file *file)
1251{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001252 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 int flag = 0, dtr, rts;
1254
Jiri Slaby74d7d972008-04-30 00:53:43 -07001255 if (!ch)
1256 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
Jiri Slabyb4173f42008-04-30 00:53:40 -07001258 MoxaPortGetLineOut(ch, &dtr, &rts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 if (dtr)
1260 flag |= TIOCM_DTR;
1261 if (rts)
1262 flag |= TIOCM_RTS;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001263 dtr = MoxaPortLineStatus(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 if (dtr & 1)
1265 flag |= TIOCM_CTS;
1266 if (dtr & 2)
1267 flag |= TIOCM_DSR;
1268 if (dtr & 4)
1269 flag |= TIOCM_CD;
1270 return flag;
1271}
1272
1273static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
1274 unsigned int set, unsigned int clear)
1275{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001276 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 int port;
1278 int dtr, rts;
1279
Jiri Slaby11324ed2007-02-10 01:45:31 -08001280 port = tty->index;
Jiri Slaby74d7d972008-04-30 00:53:43 -07001281 if (!ch)
1282 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
Jiri Slabyb4173f42008-04-30 00:53:40 -07001284 MoxaPortGetLineOut(ch, &dtr, &rts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 if (set & TIOCM_RTS)
1286 rts = 1;
1287 if (set & TIOCM_DTR)
1288 dtr = 1;
1289 if (clear & TIOCM_RTS)
1290 rts = 0;
1291 if (clear & TIOCM_DTR)
1292 dtr = 0;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001293 MoxaPortLineCtrl(ch, dtr, rts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 return 0;
1295}
1296
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297static void moxa_throttle(struct tty_struct *tty)
1298{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001299 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
1301 ch->statusflags |= THROTTLE;
1302}
1303
1304static void moxa_unthrottle(struct tty_struct *tty)
1305{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001306 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
1308 ch->statusflags &= ~THROTTLE;
1309}
1310
1311static void moxa_set_termios(struct tty_struct *tty,
Alan Cox606d0992006-12-08 02:38:45 -08001312 struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001314 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
1316 if (ch == NULL)
1317 return;
Alan Coxdb1acaa2008-02-08 04:18:43 -08001318 moxa_set_tty_param(tty, old_termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 if (!(old_termios->c_cflag & CLOCAL) &&
1320 (tty->termios->c_cflag & CLOCAL))
1321 wake_up_interruptible(&ch->open_wait);
1322}
1323
1324static void moxa_stop(struct tty_struct *tty)
1325{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001326 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
1328 if (ch == NULL)
1329 return;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001330 MoxaPortTxDisable(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 ch->statusflags |= TXSTOPPED;
1332}
1333
1334
1335static void moxa_start(struct tty_struct *tty)
1336{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001337 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
1339 if (ch == NULL)
1340 return;
1341
1342 if (!(ch->statusflags & TXSTOPPED))
1343 return;
1344
Jiri Slabyb4173f42008-04-30 00:53:40 -07001345 MoxaPortTxEnable(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 ch->statusflags &= ~TXSTOPPED;
1347}
1348
1349static void moxa_hangup(struct tty_struct *tty)
1350{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001351 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
1353 moxa_flush_buffer(tty);
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001354 moxa_shut_down(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 ch->count = 0;
1356 ch->asyncflags &= ~ASYNC_NORMAL_ACTIVE;
1357 ch->tty = NULL;
1358 wake_up_interruptible(&ch->open_wait);
1359}
1360
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001361static void moxa_new_dcdstate(struct moxa_port *p, u8 dcd)
1362{
1363 dcd = !!dcd;
1364
1365 if ((dcd != p->DCDState) && p->tty && C_CLOCAL(p->tty)) {
1366 if (!dcd) {
1367 tty_hangup(p->tty);
1368 p->asyncflags &= ~ASYNC_NORMAL_ACTIVE;
1369 }
1370 wake_up_interruptible(&p->open_wait);
1371 }
1372 p->DCDState = dcd;
1373}
1374
1375static int moxa_poll_port(struct moxa_port *p, unsigned int handle,
1376 u16 __iomem *ip)
1377{
1378 struct tty_struct *tty = p->tty;
1379 void __iomem *ofsAddr;
1380 unsigned int inited = p->asyncflags & ASYNC_INITIALIZED;
1381 u16 intr;
1382
1383 if (tty) {
1384 if ((p->statusflags & EMPTYWAIT) &&
1385 MoxaPortTxQueue(p) == 0) {
1386 p->statusflags &= ~EMPTYWAIT;
1387 tty_wakeup(tty);
1388 }
1389 if ((p->statusflags & LOWWAIT) && !tty->stopped &&
1390 MoxaPortTxQueue(p) <= WAKEUP_CHARS) {
1391 p->statusflags &= ~LOWWAIT;
1392 tty_wakeup(tty);
1393 }
1394
1395 if (inited && !(p->statusflags & THROTTLE) &&
1396 MoxaPortRxQueue(p) > 0) { /* RX */
1397 MoxaPortReadData(p);
1398 tty_schedule_flip(tty);
1399 }
1400 } else {
1401 p->statusflags &= ~EMPTYWAIT;
1402 MoxaPortFlushData(p, 0); /* flush RX */
1403 }
1404
1405 if (!handle) /* nothing else to do */
1406 return 0;
1407
1408 intr = readw(ip); /* port irq status */
1409 if (intr == 0)
1410 return 0;
1411
1412 writew(0, ip); /* ACK port */
1413 ofsAddr = p->tableAddr;
1414 if (intr & IntrTx) /* disable tx intr */
1415 writew(readw(ofsAddr + HostStat) & ~WakeupTx,
1416 ofsAddr + HostStat);
1417
1418 if (!inited)
1419 return 0;
1420
1421 if (tty && (intr & IntrBreak) && !I_IGNBRK(tty)) { /* BREAK */
1422 tty_insert_flip_char(tty, 0, TTY_BREAK);
1423 tty_schedule_flip(tty);
1424 }
1425
1426 if (intr & IntrLine)
1427 moxa_new_dcdstate(p, readb(ofsAddr + FlagStat) & DCD_state);
1428
1429 return 0;
1430}
1431
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432static void moxa_poll(unsigned long ignored)
1433{
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001434 struct moxa_board_conf *brd;
1435 u16 __iomem *ip;
Jiri Slaby2a541342008-04-30 00:53:45 -07001436 unsigned int card, port, served = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001438 spin_lock(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 for (card = 0; card < MAX_BOARDS; card++) {
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001440 brd = &moxa_boards[card];
1441 if (!brd->ready)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 continue;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001443
Jiri Slaby2a541342008-04-30 00:53:45 -07001444 served++;
1445
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001446 ip = NULL;
1447 if (readb(brd->intPend) == 0xff)
1448 ip = brd->intTable + readb(brd->intNdx);
1449
1450 for (port = 0; port < brd->numPorts; port++)
1451 moxa_poll_port(&brd->ports[port], !!ip, ip + port);
1452
1453 if (ip)
1454 writeb(0, brd->intPend); /* ACK */
1455
1456 if (moxaLowWaterChk) {
1457 struct moxa_port *p = brd->ports;
1458 for (port = 0; port < brd->numPorts; port++, p++)
1459 if (p->lowChkFlag) {
1460 p->lowChkFlag = 0;
1461 moxa_low_water_check(p->tableAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 }
1464 }
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001465 moxaLowWaterChk = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
Jiri Slaby2a541342008-04-30 00:53:45 -07001467 if (served)
1468 mod_timer(&moxaTimer, jiffies + HZ / 50);
1469 spin_unlock(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470}
1471
1472/******************************************************************************/
1473
Alan Coxdb1acaa2008-02-08 04:18:43 -08001474static void moxa_set_tty_param(struct tty_struct *tty, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475{
Alan Cox606d0992006-12-08 02:38:45 -08001476 register struct ktermios *ts;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001477 struct moxa_port *ch;
Alan Coxdb1acaa2008-02-08 04:18:43 -08001478 int rts, cts, txflow, rxflow, xany, baud;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001480 ch = (struct moxa_port *) tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 ts = tty->termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 rts = cts = txflow = rxflow = xany = 0;
1483 if (ts->c_cflag & CRTSCTS)
1484 rts = cts = 1;
1485 if (ts->c_iflag & IXON)
1486 txflow = 1;
1487 if (ts->c_iflag & IXOFF)
1488 rxflow = 1;
1489 if (ts->c_iflag & IXANY)
1490 xany = 1;
Alan Coxdb1acaa2008-02-08 04:18:43 -08001491
1492 /* Clear the features we don't support */
1493 ts->c_cflag &= ~CMSPAR;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001494 MoxaPortFlowCtrl(ch, rts, cts, txflow, rxflow, xany);
1495 baud = MoxaPortSetTermio(ch, ts, tty_get_baud_rate(tty));
Alan Coxdb1acaa2008-02-08 04:18:43 -08001496 if (baud == -1)
1497 baud = tty_termios_baud_rate(old_termios);
1498 /* Not put the baud rate into the termios data */
1499 tty_encode_baud_rate(tty, baud, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500}
1501
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001502static int moxa_block_till_ready(struct tty_struct *tty, struct file *filp,
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001503 struct moxa_port *ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504{
1505 DECLARE_WAITQUEUE(wait,current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 int retval;
1507 int do_clocal = C_CLOCAL(tty);
1508
1509 /*
1510 * If the device is in the middle of being closed, then block
1511 * until it's done, and then try again.
1512 */
1513 if (tty_hung_up_p(filp) || (ch->asyncflags & ASYNC_CLOSING)) {
1514 if (ch->asyncflags & ASYNC_CLOSING)
Jiri Slaby95e07912007-10-18 03:06:25 -07001515 wait_for_completion_interruptible(&ch->close_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516#ifdef SERIAL_DO_RESTART
1517 if (ch->asyncflags & ASYNC_HUP_NOTIFY)
1518 return (-EAGAIN);
1519 else
1520 return (-ERESTARTSYS);
1521#else
1522 return (-EAGAIN);
1523#endif
1524 }
1525 /*
1526 * If non-blocking mode is set, then make the check up front
1527 * and then exit.
1528 */
1529 if (filp->f_flags & O_NONBLOCK) {
1530 ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
1531 return (0);
1532 }
1533 /*
1534 * Block waiting for the carrier detect and the line to become free
1535 */
1536 retval = 0;
1537 add_wait_queue(&ch->open_wait, &wait);
Jiri Slaby7aeb95d2007-10-18 03:06:24 -07001538 pr_debug("block_til_ready before block: ttys%d, count = %d\n",
Jiri Slabyb4173f42008-04-30 00:53:40 -07001539 tty->index, ch->count);
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001540 spin_lock_bh(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 if (!tty_hung_up_p(filp))
1542 ch->count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 ch->blocked_open++;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001544 spin_unlock_bh(&moxa_lock);
Alan Cox33f0f882006-01-09 20:54:13 -08001545
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 while (1) {
1547 set_current_state(TASK_INTERRUPTIBLE);
1548 if (tty_hung_up_p(filp) ||
1549 !(ch->asyncflags & ASYNC_INITIALIZED)) {
1550#ifdef SERIAL_DO_RESTART
1551 if (ch->asyncflags & ASYNC_HUP_NOTIFY)
1552 retval = -EAGAIN;
1553 else
1554 retval = -ERESTARTSYS;
1555#else
1556 retval = -EAGAIN;
1557#endif
1558 break;
1559 }
1560 if (!(ch->asyncflags & ASYNC_CLOSING) && (do_clocal ||
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001561 ch->DCDState))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 break;
1563
1564 if (signal_pending(current)) {
1565 retval = -ERESTARTSYS;
1566 break;
1567 }
1568 schedule();
1569 }
1570 set_current_state(TASK_RUNNING);
1571 remove_wait_queue(&ch->open_wait, &wait);
Alan Cox33f0f882006-01-09 20:54:13 -08001572
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001573 spin_lock_bh(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 if (!tty_hung_up_p(filp))
1575 ch->count++;
1576 ch->blocked_open--;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001577 spin_unlock_bh(&moxa_lock);
Jiri Slaby7aeb95d2007-10-18 03:06:24 -07001578 pr_debug("block_til_ready after blocking: ttys%d, count = %d\n",
Jiri Slabyb4173f42008-04-30 00:53:40 -07001579 tty->index, ch->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 if (retval)
1581 return (retval);
Alan Cox33f0f882006-01-09 20:54:13 -08001582 /* FIXME: review to see if we need to use set_bit on these */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
Alan Cox33f0f882006-01-09 20:54:13 -08001584 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585}
1586
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001587static void moxa_setup_empty_event(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001589 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001591 spin_lock_bh(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 ch->statusflags |= EMPTYWAIT;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07001593 spin_unlock_bh(&moxa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594}
1595
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001596static void moxa_shut_down(struct moxa_port *ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597{
1598 struct tty_struct *tp;
1599
1600 if (!(ch->asyncflags & ASYNC_INITIALIZED))
1601 return;
1602
1603 tp = ch->tty;
1604
Jiri Slabyb4173f42008-04-30 00:53:40 -07001605 MoxaPortDisable(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
1607 /*
1608 * If we're a modem control device and HUPCL is on, drop RTS & DTR.
1609 */
1610 if (tp->termios->c_cflag & HUPCL)
Jiri Slabyb4173f42008-04-30 00:53:40 -07001611 MoxaPortLineCtrl(ch, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612
1613 ch->asyncflags &= ~ASYNC_INITIALIZED;
1614}
1615
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616/*****************************************************************************
1617 * Driver level functions: *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 *****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619
Jiri Slabyb4173f42008-04-30 00:53:40 -07001620static void MoxaPortFlushData(struct moxa_port *port, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621{
1622 void __iomem *ofsAddr;
1623 if ((mode < 0) || (mode > 2))
1624 return;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001625 ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 moxafunc(ofsAddr, FC_FlushQueue, mode);
1627 if (mode != 1) {
Jiri Slabyb4173f42008-04-30 00:53:40 -07001628 port->lowChkFlag = 0;
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001629 moxa_low_water_check(ofsAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 }
1631}
1632
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633/*****************************************************************************
1634 * Port level functions: *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 * 2. MoxaPortEnable(int port); *
1636 * 3. MoxaPortDisable(int port); *
1637 * 4. MoxaPortGetMaxBaud(int port); *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 * 6. MoxaPortSetBaud(int port, long baud); *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 * 8. MoxaPortSetTermio(int port, unsigned char *termio); *
1640 * 9. MoxaPortGetLineOut(int port, int *dtrState, int *rtsState); *
1641 * 10. MoxaPortLineCtrl(int port, int dtrState, int rtsState); *
1642 * 11. MoxaPortFlowCtrl(int port, int rts, int cts, int rx, int tx,int xany); *
1643 * 12. MoxaPortLineStatus(int port); *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 * 15. MoxaPortFlushData(int port, int mode); *
1645 * 16. MoxaPortWriteData(int port, unsigned char * buffer, int length); *
Alan Cox33f0f882006-01-09 20:54:13 -08001646 * 17. MoxaPortReadData(int port, struct tty_struct *tty); *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 * 20. MoxaPortTxQueue(int port); *
1648 * 21. MoxaPortTxFree(int port); *
1649 * 22. MoxaPortRxQueue(int port); *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 * 24. MoxaPortTxDisable(int port); *
1651 * 25. MoxaPortTxEnable(int port); *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 * 27. MoxaPortResetBrkCnt(int port); *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 *****************************************************************************/
1654/*
1655 * Moxa Port Number Description:
1656 *
1657 * MOXA serial driver supports up to 4 MOXA-C218/C320 boards. And,
1658 * the port number using in MOXA driver functions will be 0 to 31 for
1659 * first MOXA board, 32 to 63 for second, 64 to 95 for third and 96
1660 * to 127 for fourth. For example, if you setup three MOXA boards,
1661 * first board is C218, second board is C320-16 and third board is
1662 * C320-32. The port number of first board (C218 - 8 ports) is from
1663 * 0 to 7. The port number of second board (C320 - 16 ports) is form
1664 * 32 to 47. The port number of third board (C320 - 32 ports) is from
1665 * 64 to 95. And those port numbers form 8 to 31, 48 to 63 and 96 to
1666 * 127 will be invalid.
1667 *
1668 *
1669 * Moxa Functions Description:
1670 *
1671 * Function 1: Driver initialization routine, this routine must be
1672 * called when initialized driver.
1673 * Syntax:
1674 * void MoxaDriverInit();
1675 *
1676 *
1677 * Function 2: Moxa driver private IOCTL command processing.
1678 * Syntax:
1679 * int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port);
1680 *
1681 * unsigned int cmd : IOCTL command
1682 * unsigned long arg : IOCTL argument
1683 * int port : port number (0 - 127)
1684 *
1685 * return: 0 (OK)
1686 * -EINVAL
1687 * -ENOIOCTLCMD
1688 *
1689 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 * Function 6: Enable this port to start Tx/Rx data.
1691 * Syntax:
1692 * void MoxaPortEnable(int port);
1693 * int port : port number (0 - 127)
1694 *
1695 *
1696 * Function 7: Disable this port
1697 * Syntax:
1698 * void MoxaPortDisable(int port);
1699 * int port : port number (0 - 127)
1700 *
1701 *
1702 * Function 8: Get the maximun available baud rate of this port.
1703 * Syntax:
1704 * long MoxaPortGetMaxBaud(int port);
1705 * int port : port number (0 - 127)
1706 *
1707 * return: 0 : this port is invalid
1708 * 38400/57600/115200 bps
1709 *
1710 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 * Function 10: Setting baud rate of this port.
1712 * Syntax:
1713 * long MoxaPortSetBaud(int port, long baud);
1714 * int port : port number (0 - 127)
1715 * long baud : baud rate (50 - 115200)
1716 *
1717 * return: 0 : this port is invalid or baud < 50
1718 * 50 - 115200 : the real baud rate set to the port, if
1719 * the argument baud is large than maximun
1720 * available baud rate, the real setting
1721 * baud rate will be the maximun baud rate.
1722 *
1723 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 * Function 12: Configure the port.
1725 * Syntax:
Alan Cox606d0992006-12-08 02:38:45 -08001726 * int MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 * int port : port number (0 - 127)
Alan Cox606d0992006-12-08 02:38:45 -08001728 * struct ktermios * termio : termio structure pointer
Alan Coxc7bce302006-09-30 23:27:24 -07001729 * speed_t baud : baud rate
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 *
1731 * return: -1 : this port is invalid or termio == NULL
1732 * 0 : setting O.K.
1733 *
1734 *
1735 * Function 13: Get the DTR/RTS state of this port.
1736 * Syntax:
1737 * int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState);
1738 * int port : port number (0 - 127)
1739 * int * dtrState : pointer to INT to receive the current DTR
1740 * state. (if NULL, this function will not
1741 * write to this address)
1742 * int * rtsState : pointer to INT to receive the current RTS
1743 * state. (if NULL, this function will not
1744 * write to this address)
1745 *
1746 * return: -1 : this port is invalid
1747 * 0 : O.K.
1748 *
1749 *
1750 * Function 14: Setting the DTR/RTS output state of this port.
1751 * Syntax:
1752 * void MoxaPortLineCtrl(int port, int dtrState, int rtsState);
1753 * int port : port number (0 - 127)
1754 * int dtrState : DTR output state (0: off, 1: on)
1755 * int rtsState : RTS output state (0: off, 1: on)
1756 *
1757 *
1758 * Function 15: Setting the flow control of this port.
1759 * Syntax:
1760 * void MoxaPortFlowCtrl(int port, int rtsFlow, int ctsFlow, int rxFlow,
1761 * int txFlow,int xany);
1762 * int port : port number (0 - 127)
1763 * int rtsFlow : H/W RTS flow control (0: no, 1: yes)
1764 * int ctsFlow : H/W CTS flow control (0: no, 1: yes)
1765 * int rxFlow : S/W Rx XON/XOFF flow control (0: no, 1: yes)
1766 * int txFlow : S/W Tx XON/XOFF flow control (0: no, 1: yes)
1767 * int xany : S/W XANY flow control (0: no, 1: yes)
1768 *
1769 *
1770 * Function 16: Get ths line status of this port
1771 * Syntax:
1772 * int MoxaPortLineStatus(int port);
1773 * int port : port number (0 - 127)
1774 *
1775 * return: Bit 0 - CTS state (0: off, 1: on)
1776 * Bit 1 - DSR state (0: off, 1: on)
1777 * Bit 2 - DCD state (0: off, 1: on)
1778 *
1779 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 * Function 19: Flush the Rx/Tx buffer data of this port.
1781 * Syntax:
1782 * void MoxaPortFlushData(int port, int mode);
1783 * int port : port number (0 - 127)
1784 * int mode
1785 * 0 : flush the Rx buffer
1786 * 1 : flush the Tx buffer
1787 * 2 : flush the Rx and Tx buffer
1788 *
1789 *
1790 * Function 20: Write data.
1791 * Syntax:
1792 * int MoxaPortWriteData(int port, unsigned char * buffer, int length);
1793 * int port : port number (0 - 127)
1794 * unsigned char * buffer : pointer to write data buffer.
1795 * int length : write data length
1796 *
1797 * return: 0 - length : real write data length
1798 *
1799 *
1800 * Function 21: Read data.
1801 * Syntax:
Alan Cox33f0f882006-01-09 20:54:13 -08001802 * int MoxaPortReadData(int port, struct tty_struct *tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 * int port : port number (0 - 127)
Alan Cox33f0f882006-01-09 20:54:13 -08001804 * struct tty_struct *tty : tty for data
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 *
1806 * return: 0 - length : real read data length
1807 *
1808 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 * Function 24: Get the Tx buffer current queued data bytes
1810 * Syntax:
1811 * int MoxaPortTxQueue(int port);
1812 * int port : port number (0 - 127)
1813 *
1814 * return: .. : Tx buffer current queued data bytes
1815 *
1816 *
1817 * Function 25: Get the Tx buffer current free space
1818 * Syntax:
1819 * int MoxaPortTxFree(int port);
1820 * int port : port number (0 - 127)
1821 *
1822 * return: .. : Tx buffer current free space
1823 *
1824 *
1825 * Function 26: Get the Rx buffer current queued data bytes
1826 * Syntax:
1827 * int MoxaPortRxQueue(int port);
1828 * int port : port number (0 - 127)
1829 *
1830 * return: .. : Rx buffer current queued data bytes
1831 *
1832 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 * Function 28: Disable port data transmission.
1834 * Syntax:
1835 * void MoxaPortTxDisable(int port);
1836 * int port : port number (0 - 127)
1837 *
1838 *
1839 * Function 29: Enable port data transmission.
1840 * Syntax:
1841 * void MoxaPortTxEnable(int port);
1842 * int port : port number (0 - 127)
1843 *
1844 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 * Function 31: Get the received BREAK signal count and reset it.
1846 * Syntax:
1847 * int MoxaPortResetBrkCnt(int port);
1848 * int port : port number (0 - 127)
1849 *
1850 * return: 0 - .. : BREAK signal count
1851 *
1852 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854
Jiri Slabyb4173f42008-04-30 00:53:40 -07001855static void MoxaPortEnable(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856{
1857 void __iomem *ofsAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 short lowwater = 512;
1859
Jiri Slabyb4173f42008-04-30 00:53:40 -07001860 ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 writew(lowwater, ofsAddr + Low_water);
Jiri Slabyb4173f42008-04-30 00:53:40 -07001862 if (port->board->boardType == MOXA_BOARD_C320_ISA ||
1863 port->board->boardType == MOXA_BOARD_C320_PCI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 moxafunc(ofsAddr, FC_SetBreakIrq, 0);
1865 } else {
1866 writew(readw(ofsAddr + HostStat) | WakeupBreak, ofsAddr + HostStat);
1867 }
1868
1869 moxafunc(ofsAddr, FC_SetLineIrq, Magic_code);
1870 moxafunc(ofsAddr, FC_FlushQueue, 2);
1871
1872 moxafunc(ofsAddr, FC_EnableCH, Magic_code);
1873 MoxaPortLineStatus(port);
1874}
1875
Jiri Slabyb4173f42008-04-30 00:53:40 -07001876static void MoxaPortDisable(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001878 void __iomem *ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879
1880 moxafunc(ofsAddr, FC_SetFlowCtl, 0); /* disable flow control */
1881 moxafunc(ofsAddr, FC_ClrLineIrq, Magic_code);
1882 writew(0, ofsAddr + HostStat);
1883 moxafunc(ofsAddr, FC_DisableCH, Magic_code);
1884}
1885
Jiri Slabyb4173f42008-04-30 00:53:40 -07001886static long MoxaPortGetMaxBaud(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001888 if (port->board->boardType == MOXA_BOARD_C320_ISA ||
1889 port->board->boardType == MOXA_BOARD_C320_PCI)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 return (460800L);
1891 else
1892 return (921600L);
1893}
1894
1895
Jiri Slabyb4173f42008-04-30 00:53:40 -07001896static long MoxaPortSetBaud(struct moxa_port *port, long baud)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897{
1898 void __iomem *ofsAddr;
1899 long max, clock;
1900 unsigned int val;
1901
1902 if ((baud < 50L) || ((max = MoxaPortGetMaxBaud(port)) == 0))
1903 return (0);
Jiri Slabyb4173f42008-04-30 00:53:40 -07001904 ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 if (baud > max)
1906 baud = max;
1907 if (max == 38400L)
1908 clock = 614400L; /* for 9.8304 Mhz : max. 38400 bps */
1909 else if (max == 57600L)
1910 clock = 691200L; /* for 11.0592 Mhz : max. 57600 bps */
1911 else
1912 clock = 921600L; /* for 14.7456 Mhz : max. 115200 bps */
1913 val = clock / baud;
1914 moxafunc(ofsAddr, FC_SetBaud, val);
1915 baud = clock / val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 return (baud);
1917}
1918
Jiri Slabyb4173f42008-04-30 00:53:40 -07001919static int MoxaPortSetTermio(struct moxa_port *port, struct ktermios *termio,
1920 speed_t baud)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921{
1922 void __iomem *ofsAddr;
1923 tcflag_t cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 tcflag_t mode = 0;
1925
Jiri Slabyb4173f42008-04-30 00:53:40 -07001926 ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 cflag = termio->c_cflag; /* termio->c_cflag */
1928
1929 mode = termio->c_cflag & CSIZE;
1930 if (mode == CS5)
1931 mode = MX_CS5;
1932 else if (mode == CS6)
1933 mode = MX_CS6;
1934 else if (mode == CS7)
1935 mode = MX_CS7;
1936 else if (mode == CS8)
1937 mode = MX_CS8;
1938
1939 if (termio->c_cflag & CSTOPB) {
1940 if (mode == MX_CS5)
1941 mode |= MX_STOP15;
1942 else
1943 mode |= MX_STOP2;
1944 } else
1945 mode |= MX_STOP1;
1946
1947 if (termio->c_cflag & PARENB) {
1948 if (termio->c_cflag & PARODD)
1949 mode |= MX_PARODD;
1950 else
1951 mode |= MX_PAREVEN;
1952 } else
1953 mode |= MX_PARNONE;
1954
1955 moxafunc(ofsAddr, FC_SetDataMode, (ushort) mode);
1956
Jiri Slabyb4173f42008-04-30 00:53:40 -07001957 if (port->board->boardType == MOXA_BOARD_C320_ISA ||
1958 port->board->boardType == MOXA_BOARD_C320_PCI) {
Alan Coxc7bce302006-09-30 23:27:24 -07001959 if (baud >= 921600L)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 return (-1);
1961 }
Alan Coxdb1acaa2008-02-08 04:18:43 -08001962 baud = MoxaPortSetBaud(port, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
1964 if (termio->c_iflag & (IXON | IXOFF | IXANY)) {
1965 writeb(termio->c_cc[VSTART], ofsAddr + FuncArg);
1966 writeb(termio->c_cc[VSTOP], ofsAddr + FuncArg1);
1967 writeb(FC_SetXonXoff, ofsAddr + FuncCode);
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001968 moxa_wait_finish(ofsAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969
1970 }
Alan Coxdb1acaa2008-02-08 04:18:43 -08001971 return (baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972}
1973
Jiri Slabyb4173f42008-04-30 00:53:40 -07001974static int MoxaPortGetLineOut(struct moxa_port *port, int *dtrState,
1975 int *rtsState)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976{
1977
Jiri Slabyb4173f42008-04-30 00:53:40 -07001978 if (dtrState)
1979 *dtrState = !!(port->lineCtrl & DTR_ON);
1980 if (rtsState)
1981 *rtsState = !!(port->lineCtrl & RTS_ON);
1982
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 return (0);
1984}
1985
Jiri Slabyb4173f42008-04-30 00:53:40 -07001986static void MoxaPortLineCtrl(struct moxa_port *port, int dtr, int rts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001988 int mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 if (dtr)
1991 mode |= DTR_ON;
1992 if (rts)
1993 mode |= RTS_ON;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001994 port->lineCtrl = mode;
1995 moxafunc(port->tableAddr, FC_LineControl, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996}
1997
Jiri Slabyb4173f42008-04-30 00:53:40 -07001998static void MoxaPortFlowCtrl(struct moxa_port *port, int rts, int cts,
1999 int txflow, int rxflow, int txany)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002001 int mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 if (rts)
2004 mode |= RTS_FlowCtl;
2005 if (cts)
2006 mode |= CTS_FlowCtl;
2007 if (txflow)
2008 mode |= Tx_FlowCtl;
2009 if (rxflow)
2010 mode |= Rx_FlowCtl;
2011 if (txany)
2012 mode |= IXM_IXANY;
Jiri Slabyb4173f42008-04-30 00:53:40 -07002013 moxafunc(port->tableAddr, FC_SetFlowCtl, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014}
2015
Jiri Slabyb4173f42008-04-30 00:53:40 -07002016static int MoxaPortLineStatus(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017{
2018 void __iomem *ofsAddr;
2019 int val;
2020
Jiri Slabyb4173f42008-04-30 00:53:40 -07002021 ofsAddr = port->tableAddr;
2022 if (port->board->boardType == MOXA_BOARD_C320_ISA ||
2023 port->board->boardType == MOXA_BOARD_C320_PCI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 moxafunc(ofsAddr, FC_LineStatus, 0);
2025 val = readw(ofsAddr + FuncArg);
2026 } else {
2027 val = readw(ofsAddr + FlagStat) >> 4;
2028 }
2029 val &= 0x0B;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07002030 if (val & 8)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 val |= 4;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07002032 moxa_new_dcdstate(port, val & 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 val &= 7;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07002034 return val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035}
2036
Jiri Slaby2108eba2008-04-30 00:53:44 -07002037static int MoxaPortWriteData(struct moxa_port *port,
2038 const unsigned char *buffer, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 void __iomem *baseAddr, *ofsAddr, *ofs;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002041 unsigned int c, total;
2042 u16 head, tail, tx_mask, spage, epage;
2043 u16 pageno, pageofs, bufhead;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044
Jiri Slabyb4173f42008-04-30 00:53:40 -07002045 ofsAddr = port->tableAddr;
2046 baseAddr = port->board->basemem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 tx_mask = readw(ofsAddr + TX_mask);
2048 spage = readw(ofsAddr + Page_txb);
2049 epage = readw(ofsAddr + EndPage_txb);
2050 tail = readw(ofsAddr + TXwptr);
2051 head = readw(ofsAddr + TXrptr);
Jiri Slaby2108eba2008-04-30 00:53:44 -07002052 c = (head > tail) ? (head - tail - 1) : (head - tail + tx_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 if (c > len)
2054 c = len;
Jiri Slabyb4173f42008-04-30 00:53:40 -07002055 moxaLog.txcnt[port->tty->index] += c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 total = c;
2057 if (spage == epage) {
2058 bufhead = readw(ofsAddr + Ofs_txb);
2059 writew(spage, baseAddr + Control_reg);
2060 while (c > 0) {
2061 if (head > tail)
2062 len = head - tail - 1;
2063 else
2064 len = tx_mask + 1 - tail;
2065 len = (c > len) ? len : c;
2066 ofs = baseAddr + DynPage_addr + bufhead + tail;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002067 memcpy_toio(ofs, buffer, len);
2068 buffer += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 tail = (tail + len) & tx_mask;
2070 c -= len;
2071 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 pageno = spage + (tail >> 13);
2074 pageofs = tail & Page_mask;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002075 while (c > 0) {
2076 len = Page_size - pageofs;
2077 if (len > c)
2078 len = c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 writeb(pageno, baseAddr + Control_reg);
2080 ofs = baseAddr + DynPage_addr + pageofs;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002081 memcpy_toio(ofs, buffer, len);
2082 buffer += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 if (++pageno == epage)
2084 pageno = spage;
2085 pageofs = 0;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002086 c -= len;
2087 }
2088 tail = (tail + total) & tx_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 }
Jiri Slaby2108eba2008-04-30 00:53:44 -07002090 writew(tail, ofsAddr + TXwptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 writeb(1, ofsAddr + CD180TXirq); /* start to send */
Jiri Slaby2108eba2008-04-30 00:53:44 -07002092 return total;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093}
2094
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07002095static int MoxaPortReadData(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096{
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07002097 struct tty_struct *tty = port->tty;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002098 unsigned char *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 void __iomem *baseAddr, *ofsAddr, *ofs;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002100 unsigned int count, len, total;
2101 u16 tail, rx_mask, spage, epage;
2102 u16 pageno, pageofs, bufhead, head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103
Jiri Slabyb4173f42008-04-30 00:53:40 -07002104 ofsAddr = port->tableAddr;
2105 baseAddr = port->board->basemem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 head = readw(ofsAddr + RXrptr);
2107 tail = readw(ofsAddr + RXwptr);
2108 rx_mask = readw(ofsAddr + RX_mask);
2109 spage = readw(ofsAddr + Page_rxb);
2110 epage = readw(ofsAddr + EndPage_rxb);
Jiri Slaby2108eba2008-04-30 00:53:44 -07002111 count = (tail >= head) ? (tail - head) : (tail - head + rx_mask + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 if (count == 0)
Alan Cox33f0f882006-01-09 20:54:13 -08002113 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114
Alan Cox33f0f882006-01-09 20:54:13 -08002115 total = count;
Jiri Slaby7bcf97d2008-04-30 00:53:43 -07002116 moxaLog.rxcnt[tty->index] += total;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 if (spage == epage) {
2118 bufhead = readw(ofsAddr + Ofs_rxb);
2119 writew(spage, baseAddr + Control_reg);
2120 while (count > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 ofs = baseAddr + DynPage_addr + bufhead + head;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002122 len = (tail >= head) ? (tail - head) :
2123 (rx_mask + 1 - head);
2124 len = tty_prepare_flip_string(tty, &dst,
2125 min(len, count));
2126 memcpy_fromio(dst, ofs, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 head = (head + len) & rx_mask;
2128 count -= len;
2129 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 pageno = spage + (head >> 13);
2132 pageofs = head & Page_mask;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002133 while (count > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 writew(pageno, baseAddr + Control_reg);
2135 ofs = baseAddr + DynPage_addr + pageofs;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002136 len = tty_prepare_flip_string(tty, &dst,
2137 min(Page_size - pageofs, count));
2138 memcpy_fromio(dst, ofs, len);
2139
2140 count -= len;
2141 pageofs = (pageofs + len) & Page_mask;
2142 if (pageofs == 0 && ++pageno == epage)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 pageno = spage;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002144 }
2145 head = (head + total) & rx_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 }
Jiri Slaby2108eba2008-04-30 00:53:44 -07002147 writew(head, ofsAddr + RXrptr);
2148 if (readb(ofsAddr + FlagStat) & Xoff_state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 moxaLowWaterChk = 1;
Jiri Slabyb4173f42008-04-30 00:53:40 -07002150 port->lowChkFlag = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 }
Jiri Slaby2108eba2008-04-30 00:53:44 -07002152 return total;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153}
2154
2155
Jiri Slabyb4173f42008-04-30 00:53:40 -07002156static int MoxaPortTxQueue(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002158 void __iomem *ofsAddr = port->tableAddr;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002159 u16 rptr, wptr, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 rptr = readw(ofsAddr + TXrptr);
2162 wptr = readw(ofsAddr + TXwptr);
2163 mask = readw(ofsAddr + TX_mask);
Jiri Slaby2108eba2008-04-30 00:53:44 -07002164 return (wptr - rptr) & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165}
2166
Jiri Slabyb4173f42008-04-30 00:53:40 -07002167static int MoxaPortTxFree(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002169 void __iomem *ofsAddr = port->tableAddr;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002170 u16 rptr, wptr, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 rptr = readw(ofsAddr + TXrptr);
2173 wptr = readw(ofsAddr + TXwptr);
2174 mask = readw(ofsAddr + TX_mask);
Jiri Slaby2108eba2008-04-30 00:53:44 -07002175 return mask - ((wptr - rptr) & mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176}
2177
Jiri Slabyb4173f42008-04-30 00:53:40 -07002178static int MoxaPortRxQueue(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002180 void __iomem *ofsAddr = port->tableAddr;
Jiri Slaby2108eba2008-04-30 00:53:44 -07002181 u16 rptr, wptr, mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 rptr = readw(ofsAddr + RXrptr);
2184 wptr = readw(ofsAddr + RXwptr);
2185 mask = readw(ofsAddr + RX_mask);
Jiri Slaby2108eba2008-04-30 00:53:44 -07002186 return (wptr - rptr) & mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187}
2188
Jiri Slabyb4173f42008-04-30 00:53:40 -07002189static void MoxaPortTxDisable(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002191 moxafunc(port->tableAddr, FC_SetXoffState, Magic_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192}
2193
Jiri Slabyb4173f42008-04-30 00:53:40 -07002194static void MoxaPortTxEnable(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002196 moxafunc(port->tableAddr, FC_SetXonState, Magic_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197}
2198
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002199static int moxa_get_serial_info(struct moxa_port *info,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 struct serial_struct __user *retinfo)
2201{
2202 struct serial_struct tmp;
2203
2204 memset(&tmp, 0, sizeof(tmp));
2205 tmp.type = info->type;
Jiri Slabyb4173f42008-04-30 00:53:40 -07002206 tmp.line = info->tty->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 tmp.port = 0;
2208 tmp.irq = 0;
2209 tmp.flags = info->asyncflags;
2210 tmp.baud_base = 921600;
2211 tmp.close_delay = info->close_delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 tmp.custom_divisor = 0;
2213 tmp.hub6 = 0;
2214 if(copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2215 return -EFAULT;
2216 return (0);
2217}
2218
2219
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002220static int moxa_set_serial_info(struct moxa_port *info,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 struct serial_struct __user *new_info)
2222{
2223 struct serial_struct new_serial;
2224
2225 if(copy_from_user(&new_serial, new_info, sizeof(new_serial)))
2226 return -EFAULT;
2227
2228 if ((new_serial.irq != 0) ||
2229 (new_serial.port != 0) ||
2230// (new_serial.type != info->type) ||
2231 (new_serial.custom_divisor != 0) ||
2232 (new_serial.baud_base != 921600))
2233 return (-EPERM);
2234
2235 if (!capable(CAP_SYS_ADMIN)) {
2236 if (((new_serial.flags & ~ASYNC_USR_MASK) !=
2237 (info->asyncflags & ~ASYNC_USR_MASK)))
2238 return (-EPERM);
2239 } else {
2240 info->close_delay = new_serial.close_delay * HZ / 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 }
2242
2243 new_serial.flags = (new_serial.flags & ~ASYNC_FLAGS);
2244 new_serial.flags |= (info->asyncflags & ASYNC_FLAGS);
2245
2246 if (new_serial.type == PORT_16550A) {
Jiri Slabyb4173f42008-04-30 00:53:40 -07002247 MoxaSetFifo(info, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 } else {
Jiri Slabyb4173f42008-04-30 00:53:40 -07002249 MoxaSetFifo(info, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 }
2251
2252 info->type = new_serial.type;
2253 return (0);
2254}
2255
2256
2257
2258/*****************************************************************************
2259 * Static local functions: *
2260 *****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261
Jiri Slabyb4173f42008-04-30 00:53:40 -07002262static void MoxaSetFifo(struct moxa_port *port, int enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002264 void __iomem *ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265
2266 if (!enable) {
2267 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 0);
2268 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 1);
2269 } else {
2270 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 3);
2271 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 16);
2272 }
2273}