Char: moxa, remove useless tty functions
[deliverable/linux.git] / drivers / char / moxa.c
CommitLineData
1da177e4
LT
1/*****************************************************************************/
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.
1da177e4
LT
14 */
15
16/*
17 * MOXA Intellio Series Driver
18 * for : LINUX
19 * date : 1999/1/7
20 * version : 5.1
21 */
22
1da177e4
LT
23#include <linux/module.h>
24#include <linux/types.h>
25#include <linux/mm.h>
26#include <linux/ioport.h>
27#include <linux/errno.h>
03718234 28#include <linux/firmware.h>
1da177e4
LT
29#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>
45
46#include <asm/system.h>
47#include <asm/io.h>
48#include <asm/uaccess.h>
49
03718234
JS
50#include "moxa.h"
51
11324edd 52#define MOXA_VERSION "5.1k"
1da177e4 53
03718234
JS
54#define MOXA_FW_HDRLEN 32
55
11324edd 56#define MOXAMAJOR 172
1da177e4 57
11324edd 58#define MAX_BOARDS 4 /* Don't change this value */
1da177e4 59#define MAX_PORTS_PER_BOARD 32 /* Don't change this value */
11324edd 60#define MAX_PORTS (MAX_BOARDS * MAX_PORTS_PER_BOARD)
1da177e4
LT
61
62/*
63 * Define the Moxa PCI vendor and device IDs.
64 */
11324edd
JS
65#define MOXA_BUS_TYPE_ISA 0
66#define MOXA_BUS_TYPE_PCI 1
1da177e4 67
1da177e4
LT
68enum {
69 MOXA_BOARD_C218_PCI = 1,
70 MOXA_BOARD_C218_ISA,
71 MOXA_BOARD_C320_PCI,
72 MOXA_BOARD_C320_ISA,
73 MOXA_BOARD_CP204J,
74};
75
76static char *moxa_brdname[] =
77{
78 "C218 Turbo PCI series",
79 "C218 Turbo ISA series",
80 "C320 Turbo PCI series",
81 "C320 Turbo ISA series",
82 "CP-204J series",
83};
84
85#ifdef CONFIG_PCI
86static struct pci_device_id moxa_pcibrds[] = {
5ebb4078
JS
87 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C218),
88 .driver_data = MOXA_BOARD_C218_PCI },
89 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C320),
90 .driver_data = MOXA_BOARD_C320_PCI },
91 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP204J),
92 .driver_data = MOXA_BOARD_CP204J },
1da177e4
LT
93 { 0 }
94};
95MODULE_DEVICE_TABLE(pci, moxa_pcibrds);
96#endif /* CONFIG_PCI */
97
03718234
JS
98struct moxa_port;
99
8f8ecbad 100static struct moxa_board_conf {
1da177e4
LT
101 int boardType;
102 int numPorts;
1da177e4 103 int busType;
8f8ecbad 104
810ab09b 105 unsigned int ready;
8f8ecbad 106
03718234
JS
107 struct moxa_port *ports;
108
8f8ecbad
JS
109 void __iomem *basemem;
110 void __iomem *intNdx;
111 void __iomem *intPend;
112 void __iomem *intTable;
113} moxa_boards[MAX_BOARDS];
114
115struct mxser_mstatus {
116 tcflag_t cflag;
117 int cts;
118 int dsr;
119 int ri;
120 int dcd;
9dff89cd 121};
1da177e4 122
8f8ecbad
JS
123struct moxaq_str {
124 int inq;
125 int outq;
126};
1da177e4 127
8f8ecbad 128struct moxa_port {
b4173f45 129 struct moxa_board_conf *board;
7bcf97d1
JS
130 struct tty_struct *tty;
131 void __iomem *tableAddr;
132
1da177e4 133 int type;
1da177e4 134 int close_delay;
a8f5cda0 135 unsigned int count;
1da177e4 136 int asyncflags;
1da177e4 137 int cflag;
7bcf97d1 138 unsigned long statusflags;
1da177e4 139 wait_queue_head_t open_wait;
1da177e4 140
7bcf97d1
JS
141 u8 DCDState;
142 u8 lineCtrl;
143 u8 lowChkFlag;
8f8ecbad 144};
1da177e4 145
74d7d97b
JS
146struct mon_str {
147 int tick;
148 int rxcnt[MAX_PORTS];
149 int txcnt[MAX_PORTS];
150};
151
1da177e4
LT
152/* statusflags */
153#define TXSTOPPED 0x1
154#define LOWWAIT 0x2
155#define EMPTYWAIT 0x4
156#define THROTTLE 0x8
157
1da177e4
LT
158#define SERIAL_DO_RESTART
159
1da177e4
LT
160#define WAKEUP_CHARS 256
161
1da177e4 162static int ttymajor = MOXAMAJOR;
74d7d97b
JS
163static struct mon_str moxaLog;
164static unsigned int moxaFuncTout = HZ / 2;
7bcf97d1 165static unsigned int moxaLowWaterChk;
a8f5cda0 166static DEFINE_MUTEX(moxa_openlock);
1da177e4
LT
167/* Variables for insmod */
168#ifdef MODULE
d353eca4
JS
169static unsigned long baseaddr[MAX_BOARDS];
170static unsigned int type[MAX_BOARDS];
171static unsigned int numports[MAX_BOARDS];
1da177e4
LT
172#endif
173
174MODULE_AUTHOR("William Chen");
175MODULE_DESCRIPTION("MOXA Intellio Family Multiport Board Device Driver");
176MODULE_LICENSE("GPL");
177#ifdef MODULE
d353eca4
JS
178module_param_array(type, uint, NULL, 0);
179MODULE_PARM_DESC(type, "card type: C218=2, C320=4");
180module_param_array(baseaddr, ulong, NULL, 0);
181MODULE_PARM_DESC(baseaddr, "base address");
182module_param_array(numports, uint, NULL, 0);
183MODULE_PARM_DESC(numports, "numports (ignored for C218)");
1da177e4
LT
184#endif
185module_param(ttymajor, int, 0);
1da177e4 186
1da177e4
LT
187/*
188 * static functions:
189 */
1da177e4
LT
190static int moxa_open(struct tty_struct *, struct file *);
191static void moxa_close(struct tty_struct *, struct file *);
192static int moxa_write(struct tty_struct *, const unsigned char *, int);
193static int moxa_write_room(struct tty_struct *);
194static void moxa_flush_buffer(struct tty_struct *);
195static int moxa_chars_in_buffer(struct tty_struct *);
1da177e4
LT
196static void moxa_throttle(struct tty_struct *);
197static void moxa_unthrottle(struct tty_struct *);
606d099c 198static void moxa_set_termios(struct tty_struct *, struct ktermios *);
1da177e4
LT
199static void moxa_stop(struct tty_struct *);
200static void moxa_start(struct tty_struct *);
201static void moxa_hangup(struct tty_struct *);
202static int moxa_tiocmget(struct tty_struct *tty, struct file *file);
203static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
204 unsigned int set, unsigned int clear);
205static void moxa_poll(unsigned long);
db1acaa6 206static void moxa_set_tty_param(struct tty_struct *, struct ktermios *);
6f56b658 207static void moxa_setup_empty_event(struct tty_struct *);
6f56b658 208static void moxa_shut_down(struct moxa_port *);
1da177e4
LT
209/*
210 * moxa board interface functions:
211 */
b4173f45
JS
212static void MoxaPortEnable(struct moxa_port *);
213static void MoxaPortDisable(struct moxa_port *);
214static int MoxaPortSetTermio(struct moxa_port *, struct ktermios *, speed_t);
215static int MoxaPortGetLineOut(struct moxa_port *, int *, int *);
216static void MoxaPortLineCtrl(struct moxa_port *, int, int);
217static void MoxaPortFlowCtrl(struct moxa_port *, int, int, int, int, int);
218static int MoxaPortLineStatus(struct moxa_port *);
b4173f45 219static void MoxaPortFlushData(struct moxa_port *, int);
2108eba5 220static int MoxaPortWriteData(struct moxa_port *, const unsigned char *, int);
7bcf97d1 221static int MoxaPortReadData(struct moxa_port *);
b4173f45
JS
222static int MoxaPortTxQueue(struct moxa_port *);
223static int MoxaPortRxQueue(struct moxa_port *);
224static int MoxaPortTxFree(struct moxa_port *);
225static void MoxaPortTxDisable(struct moxa_port *);
226static void MoxaPortTxEnable(struct moxa_port *);
8f8ecbad
JS
227static int moxa_get_serial_info(struct moxa_port *, struct serial_struct __user *);
228static int moxa_set_serial_info(struct moxa_port *, struct serial_struct __user *);
b4173f45 229static void MoxaSetFifo(struct moxa_port *port, int enable);
1da177e4 230
74d7d97b
JS
231/*
232 * I/O functions
233 */
234
235static void moxa_wait_finish(void __iomem *ofsAddr)
236{
237 unsigned long end = jiffies + moxaFuncTout;
238
239 while (readw(ofsAddr + FuncCode) != 0)
240 if (time_after(jiffies, end))
241 return;
242 if (readw(ofsAddr + FuncCode) != 0 && printk_ratelimit())
243 printk(KERN_WARNING "moxa function expired\n");
244}
245
eaa95a8d 246static void moxafunc(void __iomem *ofsAddr, u16 cmd, u16 arg)
74d7d97b
JS
247{
248 writew(arg, ofsAddr + FuncArg);
249 writew(cmd, ofsAddr + FuncCode);
250 moxa_wait_finish(ofsAddr);
251}
252
7bcf97d1
JS
253static void moxa_low_water_check(void __iomem *ofsAddr)
254{
255 u16 rptr, wptr, mask, len;
256
257 if (readb(ofsAddr + FlagStat) & Xoff_state) {
258 rptr = readw(ofsAddr + RXrptr);
259 wptr = readw(ofsAddr + RXwptr);
260 mask = readw(ofsAddr + RX_mask);
261 len = (wptr - rptr) & mask;
262 if (len <= Low_water)
263 moxafunc(ofsAddr, FC_SendXon, 0);
264 }
265}
266
74d7d97b
JS
267/*
268 * TTY operations
269 */
270
271static int moxa_ioctl(struct tty_struct *tty, struct file *file,
272 unsigned int cmd, unsigned long arg)
273{
274 struct moxa_port *ch = tty->driver_data;
275 void __user *argp = (void __user *)arg;
a8f5cda0 276 int status, ret = 0;
74d7d97b
JS
277
278 if (tty->index == MAX_PORTS) {
279 if (cmd != MOXA_GETDATACOUNT && cmd != MOXA_GET_IOQUEUE &&
280 cmd != MOXA_GETMSTATUS)
281 return -EINVAL;
282 } else if (!ch)
283 return -ENODEV;
284
285 switch (cmd) {
286 case MOXA_GETDATACOUNT:
287 moxaLog.tick = jiffies;
a8f5cda0
JS
288 if (copy_to_user(argp, &moxaLog, sizeof(moxaLog)))
289 ret = -EFAULT;
290 break;
74d7d97b
JS
291 case MOXA_FLUSH_QUEUE:
292 MoxaPortFlushData(ch, arg);
a8f5cda0 293 break;
74d7d97b
JS
294 case MOXA_GET_IOQUEUE: {
295 struct moxaq_str __user *argm = argp;
296 struct moxaq_str tmp;
297 struct moxa_port *p;
298 unsigned int i, j;
299
a8f5cda0 300 mutex_lock(&moxa_openlock);
74d7d97b
JS
301 for (i = 0; i < MAX_BOARDS; i++) {
302 p = moxa_boards[i].ports;
303 for (j = 0; j < MAX_PORTS_PER_BOARD; j++, p++, argm++) {
304 memset(&tmp, 0, sizeof(tmp));
305 if (moxa_boards[i].ready) {
306 tmp.inq = MoxaPortRxQueue(p);
307 tmp.outq = MoxaPortTxQueue(p);
308 }
a8f5cda0
JS
309 if (copy_to_user(argm, &tmp, sizeof(tmp))) {
310 mutex_unlock(&moxa_openlock);
74d7d97b 311 return -EFAULT;
a8f5cda0 312 }
74d7d97b
JS
313 }
314 }
a8f5cda0
JS
315 mutex_unlock(&moxa_openlock);
316 break;
74d7d97b
JS
317 } case MOXA_GET_OQUEUE:
318 status = MoxaPortTxQueue(ch);
a8f5cda0
JS
319 ret = put_user(status, (unsigned long __user *)argp);
320 break;
74d7d97b
JS
321 case MOXA_GET_IQUEUE:
322 status = MoxaPortRxQueue(ch);
a8f5cda0
JS
323 ret = put_user(status, (unsigned long __user *)argp);
324 break;
74d7d97b
JS
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
a8f5cda0 331 mutex_lock(&moxa_openlock);
74d7d97b
JS
332 for (i = 0; i < MAX_BOARDS; i++) {
333 p = moxa_boards[i].ports;
334 for (j = 0; j < MAX_PORTS_PER_BOARD; j++, p++, argm++) {
335 memset(&tmp, 0, sizeof(tmp));
336 if (!moxa_boards[i].ready)
337 goto copy;
338
339 status = MoxaPortLineStatus(p);
340 if (status & 1)
341 tmp.cts = 1;
342 if (status & 2)
343 tmp.dsr = 1;
344 if (status & 4)
345 tmp.dcd = 1;
346
347 if (!p->tty || !p->tty->termios)
348 tmp.cflag = p->cflag;
349 else
350 tmp.cflag = p->tty->termios->c_cflag;
351copy:
a8f5cda0
JS
352 if (copy_to_user(argm, &tmp, sizeof(tmp))) {
353 mutex_unlock(&moxa_openlock);
74d7d97b 354 return -EFAULT;
a8f5cda0 355 }
74d7d97b
JS
356 }
357 }
a8f5cda0
JS
358 mutex_unlock(&moxa_openlock);
359 break;
74d7d97b
JS
360 }
361 case TIOCGSERIAL:
a8f5cda0
JS
362 mutex_lock(&moxa_openlock);
363 ret = moxa_get_serial_info(ch, argp);
364 mutex_unlock(&moxa_openlock);
365 break;
74d7d97b 366 case TIOCSSERIAL:
a8f5cda0
JS
367 mutex_lock(&moxa_openlock);
368 ret = moxa_set_serial_info(ch, argp);
369 mutex_unlock(&moxa_openlock);
370 break;
371 default:
372 ret = -ENOIOCTLCMD;
74d7d97b 373 }
a8f5cda0 374 return ret;
74d7d97b
JS
375}
376
377static void moxa_break_ctl(struct tty_struct *tty, int state)
378{
379 struct moxa_port *port = tty->driver_data;
380
381 moxafunc(port->tableAddr, state ? FC_SendBreak : FC_StopBreak,
382 Magic_code);
383}
384
b68e31d0 385static const struct tty_operations moxa_ops = {
1da177e4
LT
386 .open = moxa_open,
387 .close = moxa_close,
388 .write = moxa_write,
389 .write_room = moxa_write_room,
390 .flush_buffer = moxa_flush_buffer,
391 .chars_in_buffer = moxa_chars_in_buffer,
1da177e4
LT
392 .ioctl = moxa_ioctl,
393 .throttle = moxa_throttle,
394 .unthrottle = moxa_unthrottle,
395 .set_termios = moxa_set_termios,
396 .stop = moxa_stop,
397 .start = moxa_start,
398 .hangup = moxa_hangup,
74d7d97b 399 .break_ctl = moxa_break_ctl,
1da177e4
LT
400 .tiocmget = moxa_tiocmget,
401 .tiocmset = moxa_tiocmset,
402};
403
aa7e5221 404static struct tty_driver *moxaDriver;
aa7e5221 405static DEFINE_TIMER(moxaTimer, moxa_poll, 0, 0);
34af946a 406static DEFINE_SPINLOCK(moxa_lock);
33f0f88f 407
74d7d97b
JS
408/*
409 * HW init
410 */
411
03718234
JS
412static int moxa_check_fw_model(struct moxa_board_conf *brd, u8 model)
413{
414 switch (brd->boardType) {
415 case MOXA_BOARD_C218_ISA:
416 case MOXA_BOARD_C218_PCI:
417 if (model != 1)
418 goto err;
419 break;
420 case MOXA_BOARD_CP204J:
421 if (model != 3)
422 goto err;
423 break;
424 default:
425 if (model != 2)
426 goto err;
427 break;
428 }
429 return 0;
430err:
431 return -EINVAL;
432}
433
434static int moxa_check_fw(const void *ptr)
435{
436 const __le16 *lptr = ptr;
437
438 if (*lptr != cpu_to_le16(0x7980))
439 return -EINVAL;
440
441 return 0;
442}
443
444static int moxa_load_bios(struct moxa_board_conf *brd, const u8 *buf,
445 size_t len)
446{
447 void __iomem *baseAddr = brd->basemem;
448 u16 tmp;
449
450 writeb(HW_reset, baseAddr + Control_reg); /* reset */
451 msleep(10);
452 memset_io(baseAddr, 0, 4096);
453 memcpy_toio(baseAddr, buf, len); /* download BIOS */
454 writeb(0, baseAddr + Control_reg); /* restart */
455
456 msleep(2000);
457
458 switch (brd->boardType) {
459 case MOXA_BOARD_C218_ISA:
460 case MOXA_BOARD_C218_PCI:
461 tmp = readw(baseAddr + C218_key);
462 if (tmp != C218_KeyCode)
463 goto err;
464 break;
465 case MOXA_BOARD_CP204J:
466 tmp = readw(baseAddr + C218_key);
467 if (tmp != CP204J_KeyCode)
468 goto err;
469 break;
470 default:
471 tmp = readw(baseAddr + C320_key);
472 if (tmp != C320_KeyCode)
473 goto err;
474 tmp = readw(baseAddr + C320_status);
475 if (tmp != STS_init) {
eaa95a8d 476 printk(KERN_ERR "MOXA: bios upload failed -- CPU/Basic "
03718234
JS
477 "module not found\n");
478 return -EIO;
479 }
480 break;
481 }
482
483 return 0;
484err:
eaa95a8d 485 printk(KERN_ERR "MOXA: bios upload failed -- board not found\n");
03718234
JS
486 return -EIO;
487}
488
489static int moxa_load_320b(struct moxa_board_conf *brd, const u8 *ptr,
490 size_t len)
491{
492 void __iomem *baseAddr = brd->basemem;
493
494 if (len < 7168) {
eaa95a8d 495 printk(KERN_ERR "MOXA: invalid 320 bios -- too short\n");
03718234
JS
496 return -EINVAL;
497 }
498
499 writew(len - 7168 - 2, baseAddr + C320bapi_len);
500 writeb(1, baseAddr + Control_reg); /* Select Page 1 */
501 memcpy_toio(baseAddr + DynPage_addr, ptr, 7168);
502 writeb(2, baseAddr + Control_reg); /* Select Page 2 */
503 memcpy_toio(baseAddr + DynPage_addr, ptr + 7168, len - 7168);
504
505 return 0;
506}
507
5292bcd3 508static int moxa_real_load_code(struct moxa_board_conf *brd, const void *ptr,
03718234
JS
509 size_t len)
510{
511 void __iomem *baseAddr = brd->basemem;
512 const u16 *uptr = ptr;
513 size_t wlen, len2, j;
5292bcd3
JS
514 unsigned long key, loadbuf, loadlen, checksum, checksum_ok;
515 unsigned int i, retry, c320;
03718234
JS
516 u16 usum, keycode;
517
5292bcd3
JS
518 c320 = brd->boardType == MOXA_BOARD_C320_PCI ||
519 brd->boardType == MOXA_BOARD_C320_ISA;
520 keycode = (brd->boardType == MOXA_BOARD_CP204J) ? CP204J_KeyCode :
521 C218_KeyCode;
03718234 522
5292bcd3
JS
523 switch (brd->boardType) {
524 case MOXA_BOARD_CP204J:
525 case MOXA_BOARD_C218_ISA:
526 case MOXA_BOARD_C218_PCI:
527 key = C218_key;
528 loadbuf = C218_LoadBuf;
529 loadlen = C218DLoad_len;
530 checksum = C218check_sum;
531 checksum_ok = C218chksum_ok;
532 break;
533 default:
534 key = C320_key;
535 keycode = C320_KeyCode;
536 loadbuf = C320_LoadBuf;
537 loadlen = C320DLoad_len;
538 checksum = C320check_sum;
539 checksum_ok = C320chksum_ok;
540 break;
03718234 541 }
03718234
JS
542
543 usum = 0;
544 wlen = len >> 1;
545 for (i = 0; i < wlen; i++)
546 usum += le16_to_cpu(uptr[i]);
547 retry = 0;
548 do {
549 wlen = len >> 1;
550 j = 0;
551 while (wlen) {
552 len2 = (wlen > 2048) ? 2048 : wlen;
553 wlen -= len2;
5292bcd3 554 memcpy_toio(baseAddr + loadbuf, ptr + j, len2 << 1);
03718234 555 j += len2 << 1;
5292bcd3
JS
556
557 writew(len2, baseAddr + loadlen);
558 writew(0, baseAddr + key);
559 for (i = 0; i < 100; i++) {
560 if (readw(baseAddr + key) == keycode)
03718234
JS
561 break;
562 msleep(10);
563 }
5292bcd3 564 if (readw(baseAddr + key) != keycode)
03718234
JS
565 return -EIO;
566 }
5292bcd3
JS
567 writew(0, baseAddr + loadlen);
568 writew(usum, baseAddr + checksum);
569 writew(0, baseAddr + key);
570 for (i = 0; i < 100; i++) {
571 if (readw(baseAddr + key) == keycode)
03718234
JS
572 break;
573 msleep(10);
574 }
575 retry++;
5292bcd3
JS
576 } while ((readb(baseAddr + checksum_ok) != 1) && (retry < 3));
577 if (readb(baseAddr + checksum_ok) != 1)
03718234
JS
578 return -EIO;
579
5292bcd3 580 writew(0, baseAddr + key);
03718234
JS
581 for (i = 0; i < 600; i++) {
582 if (readw(baseAddr + Magic_no) == Magic_code)
583 break;
584 msleep(10);
585 }
586 if (readw(baseAddr + Magic_no) != Magic_code)
587 return -EIO;
588
5292bcd3
JS
589 if (c320) {
590 if (brd->busType == MOXA_BUS_TYPE_PCI) { /* ASIC board */
591 writew(0x3800, baseAddr + TMS320_PORT1);
592 writew(0x3900, baseAddr + TMS320_PORT2);
593 writew(28499, baseAddr + TMS320_CLOCK);
594 } else {
595 writew(0x3200, baseAddr + TMS320_PORT1);
596 writew(0x3400, baseAddr + TMS320_PORT2);
597 writew(19999, baseAddr + TMS320_CLOCK);
598 }
03718234
JS
599 }
600 writew(1, baseAddr + Disable_IRQ);
601 writew(0, baseAddr + Magic_no);
602 for (i = 0; i < 500; i++) {
603 if (readw(baseAddr + Magic_no) == Magic_code)
604 break;
605 msleep(10);
606 }
607 if (readw(baseAddr + Magic_no) != Magic_code)
608 return -EIO;
609
5292bcd3
JS
610 if (c320) {
611 j = readw(baseAddr + Module_cnt);
612 if (j <= 0)
613 return -EIO;
614 brd->numPorts = j * 8;
615 writew(j, baseAddr + Module_no);
616 writew(0, baseAddr + Magic_no);
617 for (i = 0; i < 600; i++) {
618 if (readw(baseAddr + Magic_no) == Magic_code)
619 break;
620 msleep(10);
621 }
622 if (readw(baseAddr + Magic_no) != Magic_code)
623 return -EIO;
03718234 624 }
03718234
JS
625 brd->intNdx = baseAddr + IRQindex;
626 brd->intPend = baseAddr + IRQpending;
627 brd->intTable = baseAddr + IRQtable;
628
629 return 0;
630}
631
632static int moxa_load_code(struct moxa_board_conf *brd, const void *ptr,
633 size_t len)
634{
635 void __iomem *ofsAddr, *baseAddr = brd->basemem;
636 struct moxa_port *port;
637 int retval, i;
638
639 if (len % 2) {
eaa95a8d 640 printk(KERN_ERR "MOXA: bios length is not even\n");
03718234
JS
641 return -EINVAL;
642 }
643
5292bcd3
JS
644 retval = moxa_real_load_code(brd, ptr, len); /* may change numPorts */
645 if (retval)
646 return retval;
647
03718234
JS
648 switch (brd->boardType) {
649 case MOXA_BOARD_C218_ISA:
650 case MOXA_BOARD_C218_PCI:
651 case MOXA_BOARD_CP204J:
03718234
JS
652 port = brd->ports;
653 for (i = 0; i < brd->numPorts; i++, port++) {
b4173f45 654 port->board = brd;
03718234
JS
655 port->DCDState = 0;
656 port->tableAddr = baseAddr + Extern_table +
657 Extern_size * i;
658 ofsAddr = port->tableAddr;
659 writew(C218rx_mask, ofsAddr + RX_mask);
660 writew(C218tx_mask, ofsAddr + TX_mask);
661 writew(C218rx_spage + i * C218buf_pageno, ofsAddr + Page_rxb);
662 writew(readw(ofsAddr + Page_rxb) + C218rx_pageno, ofsAddr + EndPage_rxb);
663
664 writew(C218tx_spage + i * C218buf_pageno, ofsAddr + Page_txb);
665 writew(readw(ofsAddr + Page_txb) + C218tx_pageno, ofsAddr + EndPage_txb);
666
667 }
668 break;
669 default:
03718234
JS
670 port = brd->ports;
671 for (i = 0; i < brd->numPorts; i++, port++) {
b4173f45 672 port->board = brd;
03718234
JS
673 port->DCDState = 0;
674 port->tableAddr = baseAddr + Extern_table +
675 Extern_size * i;
676 ofsAddr = port->tableAddr;
677 switch (brd->numPorts) {
678 case 8:
679 writew(C320p8rx_mask, ofsAddr + RX_mask);
680 writew(C320p8tx_mask, ofsAddr + TX_mask);
681 writew(C320p8rx_spage + i * C320p8buf_pgno, ofsAddr + Page_rxb);
682 writew(readw(ofsAddr + Page_rxb) + C320p8rx_pgno, ofsAddr + EndPage_rxb);
683 writew(C320p8tx_spage + i * C320p8buf_pgno, ofsAddr + Page_txb);
684 writew(readw(ofsAddr + Page_txb) + C320p8tx_pgno, ofsAddr + EndPage_txb);
685
686 break;
687 case 16:
688 writew(C320p16rx_mask, ofsAddr + RX_mask);
689 writew(C320p16tx_mask, ofsAddr + TX_mask);
690 writew(C320p16rx_spage + i * C320p16buf_pgno, ofsAddr + Page_rxb);
691 writew(readw(ofsAddr + Page_rxb) + C320p16rx_pgno, ofsAddr + EndPage_rxb);
692 writew(C320p16tx_spage + i * C320p16buf_pgno, ofsAddr + Page_txb);
693 writew(readw(ofsAddr + Page_txb) + C320p16tx_pgno, ofsAddr + EndPage_txb);
694 break;
695
696 case 24:
697 writew(C320p24rx_mask, ofsAddr + RX_mask);
698 writew(C320p24tx_mask, ofsAddr + TX_mask);
699 writew(C320p24rx_spage + i * C320p24buf_pgno, ofsAddr + Page_rxb);
700 writew(readw(ofsAddr + Page_rxb) + C320p24rx_pgno, ofsAddr + EndPage_rxb);
701 writew(C320p24tx_spage + i * C320p24buf_pgno, ofsAddr + Page_txb);
702 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
703 break;
704 case 32:
705 writew(C320p32rx_mask, ofsAddr + RX_mask);
706 writew(C320p32tx_mask, ofsAddr + TX_mask);
707 writew(C320p32tx_ofs, ofsAddr + Ofs_txb);
708 writew(C320p32rx_spage + i * C320p32buf_pgno, ofsAddr + Page_rxb);
709 writew(readb(ofsAddr + Page_rxb), ofsAddr + EndPage_rxb);
710 writew(C320p32tx_spage + i * C320p32buf_pgno, ofsAddr + Page_txb);
711 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
712 break;
713 }
714 }
715 break;
716 }
03718234
JS
717 return 0;
718}
719
720static int moxa_load_fw(struct moxa_board_conf *brd, const struct firmware *fw)
721{
722 void *ptr = fw->data;
723 char rsn[64];
724 u16 lens[5];
725 size_t len;
726 unsigned int a, lenp, lencnt;
727 int ret = -EINVAL;
728 struct {
729 __le32 magic; /* 0x34303430 */
730 u8 reserved1[2];
731 u8 type; /* UNIX = 3 */
732 u8 model; /* C218T=1, C320T=2, CP204=3 */
733 u8 reserved2[8];
734 __le16 len[5];
735 } *hdr = ptr;
736
737 BUILD_BUG_ON(ARRAY_SIZE(hdr->len) != ARRAY_SIZE(lens));
738
739 if (fw->size < MOXA_FW_HDRLEN) {
740 strcpy(rsn, "too short (even header won't fit)");
741 goto err;
742 }
743 if (hdr->magic != cpu_to_le32(0x30343034)) {
744 sprintf(rsn, "bad magic: %.8x", le32_to_cpu(hdr->magic));
745 goto err;
746 }
747 if (hdr->type != 3) {
748 sprintf(rsn, "not for linux, type is %u", hdr->type);
749 goto err;
750 }
751 if (moxa_check_fw_model(brd, hdr->model)) {
752 sprintf(rsn, "not for this card, model is %u", hdr->model);
753 goto err;
754 }
755
756 len = MOXA_FW_HDRLEN;
757 lencnt = hdr->model == 2 ? 5 : 3;
758 for (a = 0; a < ARRAY_SIZE(lens); a++) {
759 lens[a] = le16_to_cpu(hdr->len[a]);
760 if (lens[a] && len + lens[a] <= fw->size &&
761 moxa_check_fw(&fw->data[len]))
eaa95a8d 762 printk(KERN_WARNING "MOXA firmware: unexpected input "
03718234
JS
763 "at offset %u, but going on\n", (u32)len);
764 if (!lens[a] && a < lencnt) {
765 sprintf(rsn, "too few entries in fw file");
766 goto err;
767 }
768 len += lens[a];
769 }
770
771 if (len != fw->size) {
772 sprintf(rsn, "bad length: %u (should be %u)", (u32)fw->size,
773 (u32)len);
774 goto err;
775 }
776
777 ptr += MOXA_FW_HDRLEN;
778 lenp = 0; /* bios */
779
780 strcpy(rsn, "read above");
781
782 ret = moxa_load_bios(brd, ptr, lens[lenp]);
783 if (ret)
784 goto err;
785
786 /* we skip the tty section (lens[1]), since we don't need it */
787 ptr += lens[lenp] + lens[lenp + 1];
788 lenp += 2; /* comm */
789
790 if (hdr->model == 2) {
791 ret = moxa_load_320b(brd, ptr, lens[lenp]);
792 if (ret)
793 goto err;
794 /* skip another tty */
795 ptr += lens[lenp] + lens[lenp + 1];
796 lenp += 2;
797 }
798
799 ret = moxa_load_code(brd, ptr, lens[lenp]);
800 if (ret)
801 goto err;
802
803 return 0;
804err:
805 printk(KERN_ERR "firmware failed to load, reason: %s\n", rsn);
806 return ret;
807}
808
809static int moxa_init_board(struct moxa_board_conf *brd, struct device *dev)
810{
811 const struct firmware *fw;
812 const char *file;
810ab09b
JS
813 struct moxa_port *p;
814 unsigned int i;
03718234
JS
815 int ret;
816
810ab09b
JS
817 brd->ports = kcalloc(MAX_PORTS_PER_BOARD, sizeof(*brd->ports),
818 GFP_KERNEL);
819 if (brd->ports == NULL) {
820 printk(KERN_ERR "cannot allocate memory for ports\n");
821 ret = -ENOMEM;
822 goto err;
823 }
824
825 for (i = 0, p = brd->ports; i < MAX_PORTS_PER_BOARD; i++, p++) {
826 p->type = PORT_16550A;
827 p->close_delay = 5 * HZ / 10;
828 p->cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
829 init_waitqueue_head(&p->open_wait);
810ab09b
JS
830 }
831
03718234
JS
832 switch (brd->boardType) {
833 case MOXA_BOARD_C218_ISA:
834 case MOXA_BOARD_C218_PCI:
835 file = "c218tunx.cod";
836 break;
837 case MOXA_BOARD_CP204J:
838 file = "cp204unx.cod";
839 break;
840 default:
841 file = "c320tunx.cod";
842 break;
843 }
844
845 ret = request_firmware(&fw, file, dev);
846 if (ret) {
847 printk(KERN_ERR "request_firmware failed\n");
810ab09b 848 goto err_free;
03718234
JS
849 }
850
851 ret = moxa_load_fw(brd, fw);
852
853 release_firmware(fw);
810ab09b
JS
854
855 if (ret)
856 goto err_free;
857
2a541341 858 spin_lock_bh(&moxa_lock);
810ab09b 859 brd->ready = 1;
0bcc4caa
JS
860 if (!timer_pending(&moxaTimer))
861 mod_timer(&moxaTimer, jiffies + HZ / 50);
2a541341 862 spin_unlock_bh(&moxa_lock);
0bcc4caa 863
810ab09b
JS
864 return 0;
865err_free:
866 kfree(brd->ports);
867err:
03718234
JS
868 return ret;
869}
870
810ab09b
JS
871static void moxa_board_deinit(struct moxa_board_conf *brd)
872{
a8f5cda0
JS
873 unsigned int a, opened;
874
875 mutex_lock(&moxa_openlock);
7bcf97d1 876 spin_lock_bh(&moxa_lock);
810ab09b 877 brd->ready = 0;
7bcf97d1 878 spin_unlock_bh(&moxa_lock);
a8f5cda0
JS
879
880 /* pci hot-un-plug support */
881 for (a = 0; a < brd->numPorts; a++)
882 if (brd->ports[a].asyncflags & ASYNC_INITIALIZED)
883 tty_hangup(brd->ports[a].tty);
884 while (1) {
885 opened = 0;
886 for (a = 0; a < brd->numPorts; a++)
887 if (brd->ports[a].asyncflags & ASYNC_INITIALIZED)
888 opened++;
889 mutex_unlock(&moxa_openlock);
890 if (!opened)
891 break;
892 msleep(50);
893 mutex_lock(&moxa_openlock);
894 }
895
810ab09b
JS
896 iounmap(brd->basemem);
897 brd->basemem = NULL;
898 kfree(brd->ports);
899}
900
1da177e4 901#ifdef CONFIG_PCI
9cde5bf0
JS
902static int __devinit moxa_pci_probe(struct pci_dev *pdev,
903 const struct pci_device_id *ent)
1da177e4 904{
9cde5bf0
JS
905 struct moxa_board_conf *board;
906 unsigned int i;
907 int board_type = ent->driver_data;
908 int retval;
909
910 retval = pci_enable_device(pdev);
7aeb95da
JS
911 if (retval) {
912 dev_err(&pdev->dev, "can't enable pci device\n");
9cde5bf0 913 goto err;
7aeb95da 914 }
9cde5bf0
JS
915
916 for (i = 0; i < MAX_BOARDS; i++)
917 if (moxa_boards[i].basemem == NULL)
918 break;
919
920 retval = -ENODEV;
921 if (i >= MAX_BOARDS) {
7aeb95da 922 dev_warn(&pdev->dev, "more than %u MOXA Intellio family boards "
9cde5bf0
JS
923 "found. Board is ignored.\n", MAX_BOARDS);
924 goto err;
925 }
926
927 board = &moxa_boards[i];
e46a5e3f
JS
928
929 retval = pci_request_region(pdev, 2, "moxa-base");
930 if (retval) {
931 dev_err(&pdev->dev, "can't request pci region 2\n");
932 goto err;
933 }
934
935 board->basemem = ioremap(pci_resource_start(pdev, 2), 0x4000);
7aeb95da
JS
936 if (board->basemem == NULL) {
937 dev_err(&pdev->dev, "can't remap io space 2\n");
e46a5e3f 938 goto err_reg;
7aeb95da 939 }
9cde5bf0 940
1da177e4
LT
941 board->boardType = board_type;
942 switch (board_type) {
943 case MOXA_BOARD_C218_ISA:
944 case MOXA_BOARD_C218_PCI:
945 board->numPorts = 8;
946 break;
947
948 case MOXA_BOARD_CP204J:
949 board->numPorts = 4;
950 break;
951 default:
952 board->numPorts = 0;
953 break;
954 }
955 board->busType = MOXA_BUS_TYPE_PCI;
a784bf7c 956
03718234
JS
957 retval = moxa_init_board(board, &pdev->dev);
958 if (retval)
959 goto err_base;
960
9cde5bf0 961 pci_set_drvdata(pdev, board);
1da177e4 962
eaa95a8d 963 return 0;
03718234
JS
964err_base:
965 iounmap(board->basemem);
966 board->basemem = NULL;
e46a5e3f
JS
967err_reg:
968 pci_release_region(pdev, 2);
9cde5bf0
JS
969err:
970 return retval;
971}
972
973static void __devexit moxa_pci_remove(struct pci_dev *pdev)
974{
975 struct moxa_board_conf *brd = pci_get_drvdata(pdev);
976
810ab09b
JS
977 moxa_board_deinit(brd);
978
e46a5e3f 979 pci_release_region(pdev, 2);
1da177e4 980}
a784bf7c
JS
981
982static struct pci_driver moxa_pci_driver = {
983 .name = "moxa",
984 .id_table = moxa_pcibrds,
985 .probe = moxa_pci_probe,
986 .remove = __devexit_p(moxa_pci_remove)
987};
1da177e4
LT
988#endif /* CONFIG_PCI */
989
990static int __init moxa_init(void)
991{
810ab09b 992 unsigned int isabrds = 0;
d353eca4 993 int retval = 0;
1da177e4 994
7aeb95da
JS
995 printk(KERN_INFO "MOXA Intellio family driver version %s\n",
996 MOXA_VERSION);
1da177e4
LT
997 moxaDriver = alloc_tty_driver(MAX_PORTS + 1);
998 if (!moxaDriver)
999 return -ENOMEM;
1000
1da177e4 1001 moxaDriver->owner = THIS_MODULE;
9b4e3b13 1002 moxaDriver->name = "ttyMX";
1da177e4
LT
1003 moxaDriver->major = ttymajor;
1004 moxaDriver->minor_start = 0;
1005 moxaDriver->type = TTY_DRIVER_TYPE_SERIAL;
1006 moxaDriver->subtype = SERIAL_TYPE_NORMAL;
1007 moxaDriver->init_termios = tty_std_termios;
1da177e4 1008 moxaDriver->init_termios.c_cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
606d099c
AC
1009 moxaDriver->init_termios.c_ispeed = 9600;
1010 moxaDriver->init_termios.c_ospeed = 9600;
1da177e4
LT
1011 moxaDriver->flags = TTY_DRIVER_REAL_RAW;
1012 tty_set_operations(moxaDriver, &moxa_ops);
1013
1da177e4 1014 if (tty_register_driver(moxaDriver)) {
eaa95a8d 1015 printk(KERN_ERR "can't register MOXA Smartio tty driver!\n");
1da177e4
LT
1016 put_tty_driver(moxaDriver);
1017 return -1;
1018 }
1da177e4 1019
d353eca4 1020 /* Find the boards defined from module args. */
1da177e4 1021#ifdef MODULE
d353eca4
JS
1022 {
1023 struct moxa_board_conf *brd = moxa_boards;
810ab09b 1024 unsigned int i;
1da177e4 1025 for (i = 0; i < MAX_BOARDS; i++) {
d353eca4
JS
1026 if (!baseaddr[i])
1027 break;
1028 if (type[i] == MOXA_BOARD_C218_ISA ||
1029 type[i] == MOXA_BOARD_C320_ISA) {
7aeb95da 1030 pr_debug("Moxa board %2d: %s board(baseAddr=%lx)\n",
d353eca4
JS
1031 isabrds + 1, moxa_brdname[type[i] - 1],
1032 baseaddr[i]);
1033 brd->boardType = type[i];
1034 brd->numPorts = type[i] == MOXA_BOARD_C218_ISA ? 8 :
1035 numports[i];
1036 brd->busType = MOXA_BUS_TYPE_ISA;
1037 brd->basemem = ioremap(baseaddr[i], 0x4000);
1038 if (!brd->basemem) {
eaa95a8d 1039 printk(KERN_ERR "MOXA: can't remap %lx\n",
d353eca4 1040 baseaddr[i]);
1da177e4
LT
1041 continue;
1042 }
03718234
JS
1043 if (moxa_init_board(brd, NULL)) {
1044 iounmap(brd->basemem);
1045 brd->basemem = NULL;
1046 continue;
1047 }
d353eca4
JS
1048
1049 brd++;
1050 isabrds++;
1da177e4
LT
1051 }
1052 }
d353eca4 1053 }
1da177e4 1054#endif
a784bf7c 1055
1da177e4 1056#ifdef CONFIG_PCI
a784bf7c
JS
1057 retval = pci_register_driver(&moxa_pci_driver);
1058 if (retval) {
eaa95a8d 1059 printk(KERN_ERR "Can't register MOXA pci driver!\n");
d353eca4 1060 if (isabrds)
a784bf7c 1061 retval = 0;
1da177e4
LT
1062 }
1063#endif
a784bf7c 1064
a784bf7c 1065 return retval;
1da177e4
LT
1066}
1067
1068static void __exit moxa_exit(void)
1069{
eaa95a8d 1070 unsigned int i;
1da177e4 1071
9cde5bf0 1072#ifdef CONFIG_PCI
a784bf7c 1073 pci_unregister_driver(&moxa_pci_driver);
9cde5bf0 1074#endif
a784bf7c 1075
810ab09b
JS
1076 for (i = 0; i < MAX_BOARDS; i++) /* ISA boards */
1077 if (moxa_boards[i].ready)
1078 moxa_board_deinit(&moxa_boards[i]);
2a541341
JS
1079
1080 del_timer_sync(&moxaTimer);
1081
1082 if (tty_unregister_driver(moxaDriver))
1083 printk(KERN_ERR "Couldn't unregister MOXA Intellio family "
1084 "serial driver\n");
1085 put_tty_driver(moxaDriver);
1da177e4
LT
1086}
1087
1088module_init(moxa_init);
1089module_exit(moxa_exit);
1090
a8f5cda0
JS
1091static void moxa_close_port(struct moxa_port *ch)
1092{
1093 moxa_shut_down(ch);
1094 MoxaPortFlushData(ch, 2);
1095 ch->asyncflags &= ~ASYNC_NORMAL_ACTIVE;
1096 ch->tty->driver_data = NULL;
1097 ch->tty = NULL;
1098}
1099
1100static int moxa_block_till_ready(struct tty_struct *tty, struct file *filp,
1101 struct moxa_port *ch)
1102{
1103 DEFINE_WAIT(wait);
1104 int retval = 0;
1105 u8 dcd;
1106
1107 while (1) {
1108 prepare_to_wait(&ch->open_wait, &wait, TASK_INTERRUPTIBLE);
1109 if (tty_hung_up_p(filp)) {
1110#ifdef SERIAL_DO_RESTART
1111 retval = -ERESTARTSYS;
1112#else
1113 retval = -EAGAIN;
1114#endif
1115 break;
1116 }
1117 spin_lock_bh(&moxa_lock);
1118 dcd = ch->DCDState;
1119 spin_unlock_bh(&moxa_lock);
1120 if (dcd)
1121 break;
1122
1123 if (signal_pending(current)) {
1124 retval = -ERESTARTSYS;
1125 break;
1126 }
1127 schedule();
1128 }
1129 finish_wait(&ch->open_wait, &wait);
1130
1131 return retval;
1132}
1133
1da177e4
LT
1134static int moxa_open(struct tty_struct *tty, struct file *filp)
1135{
810ab09b 1136 struct moxa_board_conf *brd;
8f8ecbad 1137 struct moxa_port *ch;
1da177e4
LT
1138 int port;
1139 int retval;
1da177e4 1140
11324edd 1141 port = tty->index;
1da177e4 1142 if (port == MAX_PORTS) {
74d7d97b 1143 return capable(CAP_SYS_ADMIN) ? 0 : -EPERM;
1da177e4 1144 }
a8f5cda0
JS
1145 if (mutex_lock_interruptible(&moxa_openlock))
1146 return -ERESTARTSYS;
810ab09b 1147 brd = &moxa_boards[port / MAX_PORTS_PER_BOARD];
a8f5cda0
JS
1148 if (!brd->ready) {
1149 mutex_unlock(&moxa_openlock);
810ab09b 1150 return -ENODEV;
a8f5cda0 1151 }
1da177e4 1152
810ab09b 1153 ch = &brd->ports[port % MAX_PORTS_PER_BOARD];
1da177e4
LT
1154 ch->count++;
1155 tty->driver_data = ch;
1156 ch->tty = tty;
1157 if (!(ch->asyncflags & ASYNC_INITIALIZED)) {
1158 ch->statusflags = 0;
db1acaa6 1159 moxa_set_tty_param(tty, tty->termios);
b4173f45
JS
1160 MoxaPortLineCtrl(ch, 1, 1);
1161 MoxaPortEnable(ch);
a8f5cda0 1162 MoxaSetFifo(ch, ch->type == PORT_16550A);
1da177e4
LT
1163 ch->asyncflags |= ASYNC_INITIALIZED;
1164 }
a8f5cda0 1165 mutex_unlock(&moxa_openlock);
1da177e4 1166
a8f5cda0
JS
1167 retval = 0;
1168 if (!(filp->f_flags & O_NONBLOCK) && !C_CLOCAL(tty))
1169 retval = moxa_block_till_ready(tty, filp, ch);
1170 mutex_lock(&moxa_openlock);
1171 if (retval) {
1172 if (ch->count) /* 0 means already hung up... */
1173 if (--ch->count == 0)
1174 moxa_close_port(ch);
1175 } else
1176 ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
1177 mutex_unlock(&moxa_openlock);
1da177e4 1178
a8f5cda0 1179 return retval;
1da177e4
LT
1180}
1181
1182static void moxa_close(struct tty_struct *tty, struct file *filp)
1183{
8f8ecbad 1184 struct moxa_port *ch;
1da177e4
LT
1185 int port;
1186
11324edd 1187 port = tty->index;
a8f5cda0 1188 if (port == MAX_PORTS || tty_hung_up_p(filp))
1da177e4 1189 return;
1da177e4 1190
a8f5cda0
JS
1191 mutex_lock(&moxa_openlock);
1192 ch = tty->driver_data;
1193 if (ch == NULL)
1194 goto unlock;
1195 if (tty->count == 1 && ch->count != 1) {
7aeb95da
JS
1196 printk(KERN_WARNING "moxa_close: bad serial port count; "
1197 "tty->count is 1, ch->count is %d\n", ch->count);
1da177e4
LT
1198 ch->count = 1;
1199 }
1200 if (--ch->count < 0) {
7aeb95da
JS
1201 printk(KERN_WARNING "moxa_close: bad serial port count, "
1202 "device=%s\n", tty->name);
1da177e4
LT
1203 ch->count = 0;
1204 }
a8f5cda0
JS
1205 if (ch->count)
1206 goto unlock;
1da177e4
LT
1207
1208 ch->cflag = tty->termios->c_cflag;
1209 if (ch->asyncflags & ASYNC_INITIALIZED) {
6f56b658 1210 moxa_setup_empty_event(tty);
1da177e4 1211 tty_wait_until_sent(tty, 30 * HZ); /* 30 seconds timeout */
1da177e4 1212 }
1da177e4 1213
a8f5cda0
JS
1214 moxa_close_port(ch);
1215unlock:
1216 mutex_unlock(&moxa_openlock);
1da177e4
LT
1217}
1218
1219static int moxa_write(struct tty_struct *tty,
1220 const unsigned char *buf, int count)
1221{
b4173f45 1222 struct moxa_port *ch = tty->driver_data;
b4173f45 1223 int len;
1da177e4 1224
1da177e4 1225 if (ch == NULL)
b4173f45 1226 return 0;
33f0f88f 1227
7bcf97d1 1228 spin_lock_bh(&moxa_lock);
2108eba5 1229 len = MoxaPortWriteData(ch, buf, count);
7bcf97d1 1230 spin_unlock_bh(&moxa_lock);
1da177e4 1231
1da177e4 1232 ch->statusflags |= LOWWAIT;
eaa95a8d 1233 return len;
1da177e4
LT
1234}
1235
1236static int moxa_write_room(struct tty_struct *tty)
1237{
8f8ecbad 1238 struct moxa_port *ch;
1da177e4
LT
1239
1240 if (tty->stopped)
eaa95a8d 1241 return 0;
b4173f45 1242 ch = tty->driver_data;
1da177e4 1243 if (ch == NULL)
eaa95a8d 1244 return 0;
b4173f45 1245 return MoxaPortTxFree(ch);
1da177e4
LT
1246}
1247
1248static void moxa_flush_buffer(struct tty_struct *tty)
1249{
b4173f45 1250 struct moxa_port *ch = tty->driver_data;
1da177e4
LT
1251
1252 if (ch == NULL)
1253 return;
b4173f45 1254 MoxaPortFlushData(ch, 1);
1da177e4
LT
1255 tty_wakeup(tty);
1256}
1257
1258static int moxa_chars_in_buffer(struct tty_struct *tty)
1259{
b4173f45 1260 struct moxa_port *ch = tty->driver_data;
1da177e4 1261 int chars;
1da177e4
LT
1262
1263 /*
1264 * Sigh...I have to check if driver_data is NULL here, because
1265 * if an open() fails, the TTY subsystem eventually calls
1266 * tty_wait_until_sent(), which calls the driver's chars_in_buffer()
1267 * routine. And since the open() failed, we return 0 here. TDJ
1268 */
1269 if (ch == NULL)
eaa95a8d 1270 return 0;
b4173f45 1271 chars = MoxaPortTxQueue(ch);
1da177e4
LT
1272 if (chars) {
1273 /*
1274 * Make it possible to wakeup anything waiting for output
1275 * in tty_ioctl.c, etc.
1276 */
1277 if (!(ch->statusflags & EMPTYWAIT))
6f56b658 1278 moxa_setup_empty_event(tty);
1da177e4 1279 }
eaa95a8d 1280 return chars;
1da177e4
LT
1281}
1282
1da177e4
LT
1283static int moxa_tiocmget(struct tty_struct *tty, struct file *file)
1284{
a8f5cda0 1285 struct moxa_port *ch;
1da177e4
LT
1286 int flag = 0, dtr, rts;
1287
a8f5cda0
JS
1288 mutex_lock(&moxa_openlock);
1289 ch = tty->driver_data;
1290 if (!ch) {
1291 mutex_unlock(&moxa_openlock);
74d7d97b 1292 return -EINVAL;
a8f5cda0 1293 }
1da177e4 1294
b4173f45 1295 MoxaPortGetLineOut(ch, &dtr, &rts);
1da177e4
LT
1296 if (dtr)
1297 flag |= TIOCM_DTR;
1298 if (rts)
1299 flag |= TIOCM_RTS;
b4173f45 1300 dtr = MoxaPortLineStatus(ch);
1da177e4
LT
1301 if (dtr & 1)
1302 flag |= TIOCM_CTS;
1303 if (dtr & 2)
1304 flag |= TIOCM_DSR;
1305 if (dtr & 4)
1306 flag |= TIOCM_CD;
a8f5cda0 1307 mutex_unlock(&moxa_openlock);
1da177e4
LT
1308 return flag;
1309}
1310
1311static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
1312 unsigned int set, unsigned int clear)
1313{
a8f5cda0 1314 struct moxa_port *ch;
1da177e4
LT
1315 int port;
1316 int dtr, rts;
1317
11324edd 1318 port = tty->index;
a8f5cda0
JS
1319 mutex_lock(&moxa_openlock);
1320 ch = tty->driver_data;
1321 if (!ch) {
1322 mutex_unlock(&moxa_openlock);
74d7d97b 1323 return -EINVAL;
a8f5cda0 1324 }
1da177e4 1325
b4173f45 1326 MoxaPortGetLineOut(ch, &dtr, &rts);
1da177e4
LT
1327 if (set & TIOCM_RTS)
1328 rts = 1;
1329 if (set & TIOCM_DTR)
1330 dtr = 1;
1331 if (clear & TIOCM_RTS)
1332 rts = 0;
1333 if (clear & TIOCM_DTR)
1334 dtr = 0;
b4173f45 1335 MoxaPortLineCtrl(ch, dtr, rts);
a8f5cda0 1336 mutex_unlock(&moxa_openlock);
1da177e4
LT
1337 return 0;
1338}
1339
1da177e4
LT
1340static void moxa_throttle(struct tty_struct *tty)
1341{
eaa95a8d 1342 struct moxa_port *ch = tty->driver_data;
1da177e4
LT
1343
1344 ch->statusflags |= THROTTLE;
1345}
1346
1347static void moxa_unthrottle(struct tty_struct *tty)
1348{
eaa95a8d 1349 struct moxa_port *ch = tty->driver_data;
1da177e4
LT
1350
1351 ch->statusflags &= ~THROTTLE;
1352}
1353
1354static void moxa_set_termios(struct tty_struct *tty,
eaa95a8d 1355 struct ktermios *old_termios)
1da177e4 1356{
eaa95a8d 1357 struct moxa_port *ch = tty->driver_data;
1da177e4
LT
1358
1359 if (ch == NULL)
1360 return;
db1acaa6 1361 moxa_set_tty_param(tty, old_termios);
eaa95a8d 1362 if (!(old_termios->c_cflag & CLOCAL) && C_CLOCAL(tty))
1da177e4
LT
1363 wake_up_interruptible(&ch->open_wait);
1364}
1365
1366static void moxa_stop(struct tty_struct *tty)
1367{
eaa95a8d 1368 struct moxa_port *ch = tty->driver_data;
1da177e4
LT
1369
1370 if (ch == NULL)
1371 return;
b4173f45 1372 MoxaPortTxDisable(ch);
1da177e4
LT
1373 ch->statusflags |= TXSTOPPED;
1374}
1375
1376
1377static void moxa_start(struct tty_struct *tty)
1378{
eaa95a8d 1379 struct moxa_port *ch = tty->driver_data;
1da177e4
LT
1380
1381 if (ch == NULL)
1382 return;
1383
1384 if (!(ch->statusflags & TXSTOPPED))
1385 return;
1386
b4173f45 1387 MoxaPortTxEnable(ch);
1da177e4
LT
1388 ch->statusflags &= ~TXSTOPPED;
1389}
1390
1391static void moxa_hangup(struct tty_struct *tty)
1392{
a8f5cda0 1393 struct moxa_port *ch;
1da177e4 1394
a8f5cda0
JS
1395 mutex_lock(&moxa_openlock);
1396 ch = tty->driver_data;
1397 if (ch == NULL) {
1398 mutex_unlock(&moxa_openlock);
1399 return;
1400 }
1da177e4 1401 ch->count = 0;
a8f5cda0
JS
1402 moxa_close_port(ch);
1403 mutex_unlock(&moxa_openlock);
1404
1da177e4
LT
1405 wake_up_interruptible(&ch->open_wait);
1406}
1407
7bcf97d1 1408static void moxa_new_dcdstate(struct moxa_port *p, u8 dcd)
1da177e4 1409{
7bcf97d1 1410 dcd = !!dcd;
1da177e4 1411
eaa95a8d 1412 if (dcd != p->DCDState && p->tty && C_CLOCAL(p->tty)) {
a8f5cda0 1413 if (!dcd)
7bcf97d1 1414 tty_hangup(p->tty);
7bcf97d1
JS
1415 }
1416 p->DCDState = dcd;
1417}
1da177e4 1418
7bcf97d1
JS
1419static int moxa_poll_port(struct moxa_port *p, unsigned int handle,
1420 u16 __iomem *ip)
1421{
1422 struct tty_struct *tty = p->tty;
1423 void __iomem *ofsAddr;
1424 unsigned int inited = p->asyncflags & ASYNC_INITIALIZED;
1425 u16 intr;
1426
1427 if (tty) {
1428 if ((p->statusflags & EMPTYWAIT) &&
1429 MoxaPortTxQueue(p) == 0) {
1430 p->statusflags &= ~EMPTYWAIT;
1431 tty_wakeup(tty);
1432 }
1433 if ((p->statusflags & LOWWAIT) && !tty->stopped &&
1434 MoxaPortTxQueue(p) <= WAKEUP_CHARS) {
1435 p->statusflags &= ~LOWWAIT;
1436 tty_wakeup(tty);
1437 }
1438
1439 if (inited && !(p->statusflags & THROTTLE) &&
1440 MoxaPortRxQueue(p) > 0) { /* RX */
1441 MoxaPortReadData(p);
1442 tty_schedule_flip(tty);
1443 }
1444 } else {
1445 p->statusflags &= ~EMPTYWAIT;
1446 MoxaPortFlushData(p, 0); /* flush RX */
1da177e4 1447 }
0bcc4caa 1448
7bcf97d1
JS
1449 if (!handle) /* nothing else to do */
1450 return 0;
1451
1452 intr = readw(ip); /* port irq status */
1453 if (intr == 0)
1454 return 0;
1455
1456 writew(0, ip); /* ACK port */
1457 ofsAddr = p->tableAddr;
1458 if (intr & IntrTx) /* disable tx intr */
1459 writew(readw(ofsAddr + HostStat) & ~WakeupTx,
1460 ofsAddr + HostStat);
1461
1462 if (!inited)
1463 return 0;
1464
1465 if (tty && (intr & IntrBreak) && !I_IGNBRK(tty)) { /* BREAK */
1466 tty_insert_flip_char(tty, 0, TTY_BREAK);
1467 tty_schedule_flip(tty);
1468 }
1469
1470 if (intr & IntrLine)
1471 moxa_new_dcdstate(p, readb(ofsAddr + FlagStat) & DCD_state);
1472
1473 return 0;
1474}
1475
1476static void moxa_poll(unsigned long ignored)
1477{
1478 struct moxa_board_conf *brd;
1479 u16 __iomem *ip;
2a541341 1480 unsigned int card, port, served = 0;
7bcf97d1
JS
1481
1482 spin_lock(&moxa_lock);
1da177e4 1483 for (card = 0; card < MAX_BOARDS; card++) {
7bcf97d1
JS
1484 brd = &moxa_boards[card];
1485 if (!brd->ready)
1da177e4 1486 continue;
7bcf97d1 1487
2a541341
JS
1488 served++;
1489
7bcf97d1
JS
1490 ip = NULL;
1491 if (readb(brd->intPend) == 0xff)
1492 ip = brd->intTable + readb(brd->intNdx);
1493
1494 for (port = 0; port < brd->numPorts; port++)
1495 moxa_poll_port(&brd->ports[port], !!ip, ip + port);
1496
1497 if (ip)
1498 writeb(0, brd->intPend); /* ACK */
1499
1500 if (moxaLowWaterChk) {
1501 struct moxa_port *p = brd->ports;
1502 for (port = 0; port < brd->numPorts; port++, p++)
1503 if (p->lowChkFlag) {
1504 p->lowChkFlag = 0;
1505 moxa_low_water_check(p->tableAddr);
1da177e4 1506 }
1da177e4
LT
1507 }
1508 }
7bcf97d1 1509 moxaLowWaterChk = 0;
1da177e4 1510
2a541341
JS
1511 if (served)
1512 mod_timer(&moxaTimer, jiffies + HZ / 50);
1513 spin_unlock(&moxa_lock);
1da177e4
LT
1514}
1515
1516/******************************************************************************/
1517
db1acaa6 1518static void moxa_set_tty_param(struct tty_struct *tty, struct ktermios *old_termios)
1da177e4 1519{
eaa95a8d
JS
1520 register struct ktermios *ts = tty->termios;
1521 struct moxa_port *ch = tty->driver_data;
db1acaa6 1522 int rts, cts, txflow, rxflow, xany, baud;
1da177e4 1523
1da177e4
LT
1524 rts = cts = txflow = rxflow = xany = 0;
1525 if (ts->c_cflag & CRTSCTS)
1526 rts = cts = 1;
1527 if (ts->c_iflag & IXON)
1528 txflow = 1;
1529 if (ts->c_iflag & IXOFF)
1530 rxflow = 1;
1531 if (ts->c_iflag & IXANY)
1532 xany = 1;
db1acaa6
AC
1533
1534 /* Clear the features we don't support */
1535 ts->c_cflag &= ~CMSPAR;
b4173f45
JS
1536 MoxaPortFlowCtrl(ch, rts, cts, txflow, rxflow, xany);
1537 baud = MoxaPortSetTermio(ch, ts, tty_get_baud_rate(tty));
db1acaa6
AC
1538 if (baud == -1)
1539 baud = tty_termios_baud_rate(old_termios);
1540 /* Not put the baud rate into the termios data */
1541 tty_encode_baud_rate(tty, baud, baud);
1da177e4
LT
1542}
1543
6f56b658 1544static void moxa_setup_empty_event(struct tty_struct *tty)
1da177e4 1545{
8f8ecbad 1546 struct moxa_port *ch = tty->driver_data;
1da177e4 1547
7bcf97d1 1548 spin_lock_bh(&moxa_lock);
1da177e4 1549 ch->statusflags |= EMPTYWAIT;
7bcf97d1 1550 spin_unlock_bh(&moxa_lock);
1da177e4
LT
1551}
1552
6f56b658 1553static void moxa_shut_down(struct moxa_port *ch)
1da177e4 1554{
a8f5cda0 1555 struct tty_struct *tp = ch->tty;
1da177e4
LT
1556
1557 if (!(ch->asyncflags & ASYNC_INITIALIZED))
1558 return;
1559
b4173f45 1560 MoxaPortDisable(ch);
1da177e4
LT
1561
1562 /*
1563 * If we're a modem control device and HUPCL is on, drop RTS & DTR.
1564 */
a8f5cda0 1565 if (C_HUPCL(tp))
b4173f45 1566 MoxaPortLineCtrl(ch, 0, 0);
1da177e4 1567
a8f5cda0 1568 spin_lock_bh(&moxa_lock);
1da177e4 1569 ch->asyncflags &= ~ASYNC_INITIALIZED;
a8f5cda0 1570 spin_unlock_bh(&moxa_lock);
1da177e4
LT
1571}
1572
1da177e4
LT
1573/*****************************************************************************
1574 * Driver level functions: *
1da177e4 1575 *****************************************************************************/
1da177e4 1576
b4173f45 1577static void MoxaPortFlushData(struct moxa_port *port, int mode)
1da177e4
LT
1578{
1579 void __iomem *ofsAddr;
eaa95a8d 1580 if (mode < 0 || mode > 2)
1da177e4 1581 return;
b4173f45 1582 ofsAddr = port->tableAddr;
1da177e4
LT
1583 moxafunc(ofsAddr, FC_FlushQueue, mode);
1584 if (mode != 1) {
b4173f45 1585 port->lowChkFlag = 0;
6f56b658 1586 moxa_low_water_check(ofsAddr);
1da177e4
LT
1587 }
1588}
1589
1da177e4
LT
1590/*
1591 * Moxa Port Number Description:
1592 *
1593 * MOXA serial driver supports up to 4 MOXA-C218/C320 boards. And,
1594 * the port number using in MOXA driver functions will be 0 to 31 for
1595 * first MOXA board, 32 to 63 for second, 64 to 95 for third and 96
1596 * to 127 for fourth. For example, if you setup three MOXA boards,
1597 * first board is C218, second board is C320-16 and third board is
1598 * C320-32. The port number of first board (C218 - 8 ports) is from
1599 * 0 to 7. The port number of second board (C320 - 16 ports) is form
1600 * 32 to 47. The port number of third board (C320 - 32 ports) is from
1601 * 64 to 95. And those port numbers form 8 to 31, 48 to 63 and 96 to
1602 * 127 will be invalid.
1603 *
1604 *
1605 * Moxa Functions Description:
1606 *
1607 * Function 1: Driver initialization routine, this routine must be
1608 * called when initialized driver.
1609 * Syntax:
1610 * void MoxaDriverInit();
1611 *
1612 *
1613 * Function 2: Moxa driver private IOCTL command processing.
1614 * Syntax:
1615 * int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port);
1616 *
1617 * unsigned int cmd : IOCTL command
1618 * unsigned long arg : IOCTL argument
1619 * int port : port number (0 - 127)
1620 *
1621 * return: 0 (OK)
1622 * -EINVAL
1623 * -ENOIOCTLCMD
1624 *
1625 *
1da177e4
LT
1626 * Function 6: Enable this port to start Tx/Rx data.
1627 * Syntax:
1628 * void MoxaPortEnable(int port);
1629 * int port : port number (0 - 127)
1630 *
1631 *
1632 * Function 7: Disable this port
1633 * Syntax:
1634 * void MoxaPortDisable(int port);
1635 * int port : port number (0 - 127)
1636 *
1637 *
1638 * Function 8: Get the maximun available baud rate of this port.
1639 * Syntax:
1640 * long MoxaPortGetMaxBaud(int port);
1641 * int port : port number (0 - 127)
1642 *
1643 * return: 0 : this port is invalid
1644 * 38400/57600/115200 bps
1645 *
1646 *
1da177e4
LT
1647 * Function 10: Setting baud rate of this port.
1648 * Syntax:
1649 * long MoxaPortSetBaud(int port, long baud);
1650 * int port : port number (0 - 127)
1651 * long baud : baud rate (50 - 115200)
1652 *
1653 * return: 0 : this port is invalid or baud < 50
1654 * 50 - 115200 : the real baud rate set to the port, if
1655 * the argument baud is large than maximun
1656 * available baud rate, the real setting
1657 * baud rate will be the maximun baud rate.
1658 *
1659 *
1da177e4
LT
1660 * Function 12: Configure the port.
1661 * Syntax:
606d099c 1662 * int MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud);
1da177e4 1663 * int port : port number (0 - 127)
606d099c 1664 * struct ktermios * termio : termio structure pointer
c7bce309 1665 * speed_t baud : baud rate
1da177e4
LT
1666 *
1667 * return: -1 : this port is invalid or termio == NULL
1668 * 0 : setting O.K.
1669 *
1670 *
1671 * Function 13: Get the DTR/RTS state of this port.
1672 * Syntax:
1673 * int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState);
1674 * int port : port number (0 - 127)
1675 * int * dtrState : pointer to INT to receive the current DTR
1676 * state. (if NULL, this function will not
1677 * write to this address)
1678 * int * rtsState : pointer to INT to receive the current RTS
1679 * state. (if NULL, this function will not
1680 * write to this address)
1681 *
1682 * return: -1 : this port is invalid
1683 * 0 : O.K.
1684 *
1685 *
1686 * Function 14: Setting the DTR/RTS output state of this port.
1687 * Syntax:
1688 * void MoxaPortLineCtrl(int port, int dtrState, int rtsState);
1689 * int port : port number (0 - 127)
1690 * int dtrState : DTR output state (0: off, 1: on)
1691 * int rtsState : RTS output state (0: off, 1: on)
1692 *
1693 *
1694 * Function 15: Setting the flow control of this port.
1695 * Syntax:
1696 * void MoxaPortFlowCtrl(int port, int rtsFlow, int ctsFlow, int rxFlow,
1697 * int txFlow,int xany);
1698 * int port : port number (0 - 127)
1699 * int rtsFlow : H/W RTS flow control (0: no, 1: yes)
1700 * int ctsFlow : H/W CTS flow control (0: no, 1: yes)
1701 * int rxFlow : S/W Rx XON/XOFF flow control (0: no, 1: yes)
1702 * int txFlow : S/W Tx XON/XOFF flow control (0: no, 1: yes)
1703 * int xany : S/W XANY flow control (0: no, 1: yes)
1704 *
1705 *
1706 * Function 16: Get ths line status of this port
1707 * Syntax:
1708 * int MoxaPortLineStatus(int port);
1709 * int port : port number (0 - 127)
1710 *
1711 * return: Bit 0 - CTS state (0: off, 1: on)
1712 * Bit 1 - DSR state (0: off, 1: on)
1713 * Bit 2 - DCD state (0: off, 1: on)
1714 *
1715 *
1da177e4
LT
1716 * Function 19: Flush the Rx/Tx buffer data of this port.
1717 * Syntax:
1718 * void MoxaPortFlushData(int port, int mode);
1719 * int port : port number (0 - 127)
1720 * int mode
1721 * 0 : flush the Rx buffer
1722 * 1 : flush the Tx buffer
1723 * 2 : flush the Rx and Tx buffer
1724 *
1725 *
1726 * Function 20: Write data.
1727 * Syntax:
1728 * int MoxaPortWriteData(int port, unsigned char * buffer, int length);
1729 * int port : port number (0 - 127)
1730 * unsigned char * buffer : pointer to write data buffer.
1731 * int length : write data length
1732 *
1733 * return: 0 - length : real write data length
1734 *
1735 *
1736 * Function 21: Read data.
1737 * Syntax:
33f0f88f 1738 * int MoxaPortReadData(int port, struct tty_struct *tty);
1da177e4 1739 * int port : port number (0 - 127)
33f0f88f 1740 * struct tty_struct *tty : tty for data
1da177e4
LT
1741 *
1742 * return: 0 - length : real read data length
1743 *
1744 *
1da177e4
LT
1745 * Function 24: Get the Tx buffer current queued data bytes
1746 * Syntax:
1747 * int MoxaPortTxQueue(int port);
1748 * int port : port number (0 - 127)
1749 *
1750 * return: .. : Tx buffer current queued data bytes
1751 *
1752 *
1753 * Function 25: Get the Tx buffer current free space
1754 * Syntax:
1755 * int MoxaPortTxFree(int port);
1756 * int port : port number (0 - 127)
1757 *
1758 * return: .. : Tx buffer current free space
1759 *
1760 *
1761 * Function 26: Get the Rx buffer current queued data bytes
1762 * Syntax:
1763 * int MoxaPortRxQueue(int port);
1764 * int port : port number (0 - 127)
1765 *
1766 * return: .. : Rx buffer current queued data bytes
1767 *
1768 *
1da177e4
LT
1769 * Function 28: Disable port data transmission.
1770 * Syntax:
1771 * void MoxaPortTxDisable(int port);
1772 * int port : port number (0 - 127)
1773 *
1774 *
1775 * Function 29: Enable port data transmission.
1776 * Syntax:
1777 * void MoxaPortTxEnable(int port);
1778 * int port : port number (0 - 127)
1779 *
1780 *
1da177e4
LT
1781 * Function 31: Get the received BREAK signal count and reset it.
1782 * Syntax:
1783 * int MoxaPortResetBrkCnt(int port);
1784 * int port : port number (0 - 127)
1785 *
1786 * return: 0 - .. : BREAK signal count
1787 *
1788 *
1da177e4 1789 */
1da177e4 1790
b4173f45 1791static void MoxaPortEnable(struct moxa_port *port)
1da177e4
LT
1792{
1793 void __iomem *ofsAddr;
eaa95a8d 1794 u16 lowwater = 512;
1da177e4 1795
b4173f45 1796 ofsAddr = port->tableAddr;
1da177e4 1797 writew(lowwater, ofsAddr + Low_water);
b4173f45 1798 if (port->board->boardType == MOXA_BOARD_C320_ISA ||
eaa95a8d 1799 port->board->boardType == MOXA_BOARD_C320_PCI)
1da177e4 1800 moxafunc(ofsAddr, FC_SetBreakIrq, 0);
eaa95a8d
JS
1801 else
1802 writew(readw(ofsAddr + HostStat) | WakeupBreak,
1803 ofsAddr + HostStat);
1da177e4
LT
1804
1805 moxafunc(ofsAddr, FC_SetLineIrq, Magic_code);
1806 moxafunc(ofsAddr, FC_FlushQueue, 2);
1807
1808 moxafunc(ofsAddr, FC_EnableCH, Magic_code);
1809 MoxaPortLineStatus(port);
1810}
1811
b4173f45 1812static void MoxaPortDisable(struct moxa_port *port)
1da177e4 1813{
b4173f45 1814 void __iomem *ofsAddr = port->tableAddr;
1da177e4
LT
1815
1816 moxafunc(ofsAddr, FC_SetFlowCtl, 0); /* disable flow control */
1817 moxafunc(ofsAddr, FC_ClrLineIrq, Magic_code);
1818 writew(0, ofsAddr + HostStat);
1819 moxafunc(ofsAddr, FC_DisableCH, Magic_code);
1820}
1821
b4173f45 1822static long MoxaPortGetMaxBaud(struct moxa_port *port)
1da177e4 1823{
b4173f45
JS
1824 if (port->board->boardType == MOXA_BOARD_C320_ISA ||
1825 port->board->boardType == MOXA_BOARD_C320_PCI)
eaa95a8d 1826 return 460800L;
1da177e4 1827 else
eaa95a8d 1828 return 921600L;
1da177e4
LT
1829}
1830
1831
b4173f45 1832static long MoxaPortSetBaud(struct moxa_port *port, long baud)
1da177e4
LT
1833{
1834 void __iomem *ofsAddr;
1835 long max, clock;
1836 unsigned int val;
1837
eaa95a8d
JS
1838 if (baud < 50L || (max = MoxaPortGetMaxBaud(port)) == 0)
1839 return 0;
b4173f45 1840 ofsAddr = port->tableAddr;
1da177e4
LT
1841 if (baud > max)
1842 baud = max;
1843 if (max == 38400L)
1844 clock = 614400L; /* for 9.8304 Mhz : max. 38400 bps */
1845 else if (max == 57600L)
1846 clock = 691200L; /* for 11.0592 Mhz : max. 57600 bps */
1847 else
1848 clock = 921600L; /* for 14.7456 Mhz : max. 115200 bps */
1849 val = clock / baud;
1850 moxafunc(ofsAddr, FC_SetBaud, val);
1851 baud = clock / val;
eaa95a8d 1852 return baud;
1da177e4
LT
1853}
1854
b4173f45
JS
1855static int MoxaPortSetTermio(struct moxa_port *port, struct ktermios *termio,
1856 speed_t baud)
1da177e4
LT
1857{
1858 void __iomem *ofsAddr;
1859 tcflag_t cflag;
1da177e4
LT
1860 tcflag_t mode = 0;
1861
b4173f45 1862 ofsAddr = port->tableAddr;
1da177e4
LT
1863 cflag = termio->c_cflag; /* termio->c_cflag */
1864
1865 mode = termio->c_cflag & CSIZE;
1866 if (mode == CS5)
1867 mode = MX_CS5;
1868 else if (mode == CS6)
1869 mode = MX_CS6;
1870 else if (mode == CS7)
1871 mode = MX_CS7;
1872 else if (mode == CS8)
1873 mode = MX_CS8;
1874
1875 if (termio->c_cflag & CSTOPB) {
1876 if (mode == MX_CS5)
1877 mode |= MX_STOP15;
1878 else
1879 mode |= MX_STOP2;
1880 } else
1881 mode |= MX_STOP1;
1882
1883 if (termio->c_cflag & PARENB) {
1884 if (termio->c_cflag & PARODD)
1885 mode |= MX_PARODD;
1886 else
1887 mode |= MX_PAREVEN;
1888 } else
1889 mode |= MX_PARNONE;
1890
eaa95a8d 1891 moxafunc(ofsAddr, FC_SetDataMode, (u16)mode);
1da177e4 1892
b4173f45
JS
1893 if (port->board->boardType == MOXA_BOARD_C320_ISA ||
1894 port->board->boardType == MOXA_BOARD_C320_PCI) {
c7bce309 1895 if (baud >= 921600L)
eaa95a8d 1896 return -1;
1da177e4 1897 }
db1acaa6 1898 baud = MoxaPortSetBaud(port, baud);
1da177e4
LT
1899
1900 if (termio->c_iflag & (IXON | IXOFF | IXANY)) {
1901 writeb(termio->c_cc[VSTART], ofsAddr + FuncArg);
1902 writeb(termio->c_cc[VSTOP], ofsAddr + FuncArg1);
1903 writeb(FC_SetXonXoff, ofsAddr + FuncCode);
6f56b658 1904 moxa_wait_finish(ofsAddr);
1da177e4
LT
1905
1906 }
eaa95a8d 1907 return baud;
1da177e4
LT
1908}
1909
b4173f45
JS
1910static int MoxaPortGetLineOut(struct moxa_port *port, int *dtrState,
1911 int *rtsState)
1da177e4 1912{
b4173f45
JS
1913 if (dtrState)
1914 *dtrState = !!(port->lineCtrl & DTR_ON);
1915 if (rtsState)
1916 *rtsState = !!(port->lineCtrl & RTS_ON);
1917
eaa95a8d 1918 return 0;
1da177e4
LT
1919}
1920
b4173f45 1921static void MoxaPortLineCtrl(struct moxa_port *port, int dtr, int rts)
1da177e4 1922{
eaa95a8d 1923 u8 mode = 0;
1da177e4 1924
1da177e4
LT
1925 if (dtr)
1926 mode |= DTR_ON;
1927 if (rts)
1928 mode |= RTS_ON;
b4173f45
JS
1929 port->lineCtrl = mode;
1930 moxafunc(port->tableAddr, FC_LineControl, mode);
1da177e4
LT
1931}
1932
b4173f45
JS
1933static void MoxaPortFlowCtrl(struct moxa_port *port, int rts, int cts,
1934 int txflow, int rxflow, int txany)
1da177e4 1935{
b4173f45 1936 int mode = 0;
1da177e4 1937
1da177e4
LT
1938 if (rts)
1939 mode |= RTS_FlowCtl;
1940 if (cts)
1941 mode |= CTS_FlowCtl;
1942 if (txflow)
1943 mode |= Tx_FlowCtl;
1944 if (rxflow)
1945 mode |= Rx_FlowCtl;
1946 if (txany)
1947 mode |= IXM_IXANY;
b4173f45 1948 moxafunc(port->tableAddr, FC_SetFlowCtl, mode);
1da177e4
LT
1949}
1950
b4173f45 1951static int MoxaPortLineStatus(struct moxa_port *port)
1da177e4
LT
1952{
1953 void __iomem *ofsAddr;
1954 int val;
1955
b4173f45
JS
1956 ofsAddr = port->tableAddr;
1957 if (port->board->boardType == MOXA_BOARD_C320_ISA ||
1958 port->board->boardType == MOXA_BOARD_C320_PCI) {
1da177e4
LT
1959 moxafunc(ofsAddr, FC_LineStatus, 0);
1960 val = readw(ofsAddr + FuncArg);
1961 } else {
1962 val = readw(ofsAddr + FlagStat) >> 4;
1963 }
1964 val &= 0x0B;
7bcf97d1 1965 if (val & 8)
1da177e4 1966 val |= 4;
a8f5cda0 1967 spin_lock_bh(&moxa_lock);
7bcf97d1 1968 moxa_new_dcdstate(port, val & 8);
a8f5cda0 1969 spin_unlock_bh(&moxa_lock);
1da177e4 1970 val &= 7;
7bcf97d1 1971 return val;
1da177e4
LT
1972}
1973
2108eba5
JS
1974static int MoxaPortWriteData(struct moxa_port *port,
1975 const unsigned char *buffer, int len)
1da177e4 1976{
1da177e4 1977 void __iomem *baseAddr, *ofsAddr, *ofs;
2108eba5
JS
1978 unsigned int c, total;
1979 u16 head, tail, tx_mask, spage, epage;
1980 u16 pageno, pageofs, bufhead;
1da177e4 1981
b4173f45
JS
1982 ofsAddr = port->tableAddr;
1983 baseAddr = port->board->basemem;
1da177e4
LT
1984 tx_mask = readw(ofsAddr + TX_mask);
1985 spage = readw(ofsAddr + Page_txb);
1986 epage = readw(ofsAddr + EndPage_txb);
1987 tail = readw(ofsAddr + TXwptr);
1988 head = readw(ofsAddr + TXrptr);
2108eba5 1989 c = (head > tail) ? (head - tail - 1) : (head - tail + tx_mask);
1da177e4
LT
1990 if (c > len)
1991 c = len;
b4173f45 1992 moxaLog.txcnt[port->tty->index] += c;
1da177e4
LT
1993 total = c;
1994 if (spage == epage) {
1995 bufhead = readw(ofsAddr + Ofs_txb);
1996 writew(spage, baseAddr + Control_reg);
1997 while (c > 0) {
1998 if (head > tail)
1999 len = head - tail - 1;
2000 else
2001 len = tx_mask + 1 - tail;
2002 len = (c > len) ? len : c;
2003 ofs = baseAddr + DynPage_addr + bufhead + tail;
2108eba5
JS
2004 memcpy_toio(ofs, buffer, len);
2005 buffer += len;
1da177e4
LT
2006 tail = (tail + len) & tx_mask;
2007 c -= len;
2008 }
1da177e4 2009 } else {
1da177e4
LT
2010 pageno = spage + (tail >> 13);
2011 pageofs = tail & Page_mask;
2108eba5
JS
2012 while (c > 0) {
2013 len = Page_size - pageofs;
2014 if (len > c)
2015 len = c;
1da177e4
LT
2016 writeb(pageno, baseAddr + Control_reg);
2017 ofs = baseAddr + DynPage_addr + pageofs;
2108eba5
JS
2018 memcpy_toio(ofs, buffer, len);
2019 buffer += len;
1da177e4
LT
2020 if (++pageno == epage)
2021 pageno = spage;
2022 pageofs = 0;
2108eba5
JS
2023 c -= len;
2024 }
2025 tail = (tail + total) & tx_mask;
1da177e4 2026 }
2108eba5 2027 writew(tail, ofsAddr + TXwptr);
1da177e4 2028 writeb(1, ofsAddr + CD180TXirq); /* start to send */
2108eba5 2029 return total;
1da177e4
LT
2030}
2031
7bcf97d1 2032static int MoxaPortReadData(struct moxa_port *port)
1da177e4 2033{
7bcf97d1 2034 struct tty_struct *tty = port->tty;
2108eba5 2035 unsigned char *dst;
1da177e4 2036 void __iomem *baseAddr, *ofsAddr, *ofs;
2108eba5
JS
2037 unsigned int count, len, total;
2038 u16 tail, rx_mask, spage, epage;
2039 u16 pageno, pageofs, bufhead, head;
1da177e4 2040
b4173f45
JS
2041 ofsAddr = port->tableAddr;
2042 baseAddr = port->board->basemem;
1da177e4
LT
2043 head = readw(ofsAddr + RXrptr);
2044 tail = readw(ofsAddr + RXwptr);
2045 rx_mask = readw(ofsAddr + RX_mask);
2046 spage = readw(ofsAddr + Page_rxb);
2047 epage = readw(ofsAddr + EndPage_rxb);
2108eba5 2048 count = (tail >= head) ? (tail - head) : (tail - head + rx_mask + 1);
1da177e4 2049 if (count == 0)
33f0f88f 2050 return 0;
1da177e4 2051
33f0f88f 2052 total = count;
7bcf97d1 2053 moxaLog.rxcnt[tty->index] += total;
1da177e4
LT
2054 if (spage == epage) {
2055 bufhead = readw(ofsAddr + Ofs_rxb);
2056 writew(spage, baseAddr + Control_reg);
2057 while (count > 0) {
1da177e4 2058 ofs = baseAddr + DynPage_addr + bufhead + head;
2108eba5
JS
2059 len = (tail >= head) ? (tail - head) :
2060 (rx_mask + 1 - head);
2061 len = tty_prepare_flip_string(tty, &dst,
2062 min(len, count));
2063 memcpy_fromio(dst, ofs, len);
1da177e4
LT
2064 head = (head + len) & rx_mask;
2065 count -= len;
2066 }
1da177e4 2067 } else {
1da177e4
LT
2068 pageno = spage + (head >> 13);
2069 pageofs = head & Page_mask;
2108eba5 2070 while (count > 0) {
1da177e4
LT
2071 writew(pageno, baseAddr + Control_reg);
2072 ofs = baseAddr + DynPage_addr + pageofs;
2108eba5
JS
2073 len = tty_prepare_flip_string(tty, &dst,
2074 min(Page_size - pageofs, count));
2075 memcpy_fromio(dst, ofs, len);
2076
2077 count -= len;
2078 pageofs = (pageofs + len) & Page_mask;
2079 if (pageofs == 0 && ++pageno == epage)
1da177e4 2080 pageno = spage;
2108eba5
JS
2081 }
2082 head = (head + total) & rx_mask;
1da177e4 2083 }
2108eba5
JS
2084 writew(head, ofsAddr + RXrptr);
2085 if (readb(ofsAddr + FlagStat) & Xoff_state) {
1da177e4 2086 moxaLowWaterChk = 1;
b4173f45 2087 port->lowChkFlag = 1;
1da177e4 2088 }
2108eba5 2089 return total;
1da177e4
LT
2090}
2091
2092
b4173f45 2093static int MoxaPortTxQueue(struct moxa_port *port)
1da177e4 2094{
b4173f45 2095 void __iomem *ofsAddr = port->tableAddr;
2108eba5 2096 u16 rptr, wptr, mask;
1da177e4 2097
1da177e4
LT
2098 rptr = readw(ofsAddr + TXrptr);
2099 wptr = readw(ofsAddr + TXwptr);
2100 mask = readw(ofsAddr + TX_mask);
2108eba5 2101 return (wptr - rptr) & mask;
1da177e4
LT
2102}
2103
b4173f45 2104static int MoxaPortTxFree(struct moxa_port *port)
1da177e4 2105{
b4173f45 2106 void __iomem *ofsAddr = port->tableAddr;
2108eba5 2107 u16 rptr, wptr, mask;
1da177e4 2108
1da177e4
LT
2109 rptr = readw(ofsAddr + TXrptr);
2110 wptr = readw(ofsAddr + TXwptr);
2111 mask = readw(ofsAddr + TX_mask);
2108eba5 2112 return mask - ((wptr - rptr) & mask);
1da177e4
LT
2113}
2114
b4173f45 2115static int MoxaPortRxQueue(struct moxa_port *port)
1da177e4 2116{
b4173f45 2117 void __iomem *ofsAddr = port->tableAddr;
2108eba5 2118 u16 rptr, wptr, mask;
1da177e4 2119
1da177e4
LT
2120 rptr = readw(ofsAddr + RXrptr);
2121 wptr = readw(ofsAddr + RXwptr);
2122 mask = readw(ofsAddr + RX_mask);
2108eba5 2123 return (wptr - rptr) & mask;
1da177e4
LT
2124}
2125
b4173f45 2126static void MoxaPortTxDisable(struct moxa_port *port)
1da177e4 2127{
b4173f45 2128 moxafunc(port->tableAddr, FC_SetXoffState, Magic_code);
1da177e4
LT
2129}
2130
b4173f45 2131static void MoxaPortTxEnable(struct moxa_port *port)
1da177e4 2132{
b4173f45 2133 moxafunc(port->tableAddr, FC_SetXonState, Magic_code);
1da177e4
LT
2134}
2135
8f8ecbad 2136static int moxa_get_serial_info(struct moxa_port *info,
eaa95a8d 2137 struct serial_struct __user *retinfo)
1da177e4 2138{
eaa95a8d
JS
2139 struct serial_struct tmp = {
2140 .type = info->type,
2141 .line = info->tty->index,
2142 .flags = info->asyncflags,
2143 .baud_base = 921600,
2144 .close_delay = info->close_delay
2145 };
2146 return copy_to_user(retinfo, &tmp, sizeof(*retinfo)) ? -EFAULT : 0;
1da177e4
LT
2147}
2148
2149
8f8ecbad 2150static int moxa_set_serial_info(struct moxa_port *info,
eaa95a8d 2151 struct serial_struct __user *new_info)
1da177e4
LT
2152{
2153 struct serial_struct new_serial;
2154
eaa95a8d 2155 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
1da177e4
LT
2156 return -EFAULT;
2157
eaa95a8d
JS
2158 if (new_serial.irq != 0 || new_serial.port != 0 ||
2159 new_serial.custom_divisor != 0 ||
2160 new_serial.baud_base != 921600)
2161 return -EPERM;
1da177e4
LT
2162
2163 if (!capable(CAP_SYS_ADMIN)) {
2164 if (((new_serial.flags & ~ASYNC_USR_MASK) !=
2165 (info->asyncflags & ~ASYNC_USR_MASK)))
eaa95a8d
JS
2166 return -EPERM;
2167 } else
1da177e4 2168 info->close_delay = new_serial.close_delay * HZ / 100;
1da177e4
LT
2169
2170 new_serial.flags = (new_serial.flags & ~ASYNC_FLAGS);
2171 new_serial.flags |= (info->asyncflags & ASYNC_FLAGS);
2172
eaa95a8d 2173 MoxaSetFifo(info, new_serial.type == PORT_16550A);
1da177e4
LT
2174
2175 info->type = new_serial.type;
eaa95a8d 2176 return 0;
1da177e4
LT
2177}
2178
2179
2180
2181/*****************************************************************************
2182 * Static local functions: *
2183 *****************************************************************************/
1da177e4 2184
b4173f45 2185static void MoxaSetFifo(struct moxa_port *port, int enable)
1da177e4 2186{
b4173f45 2187 void __iomem *ofsAddr = port->tableAddr;
1da177e4
LT
2188
2189 if (!enable) {
2190 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 0);
2191 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 1);
2192 } else {
2193 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 3);
2194 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 16);
2195 }
2196}
This page took 0.491984 seconds and 5 git commands to generate.