[PATCH] Char: moxa, variables cleanup
[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>
28#include <linux/signal.h>
29#include <linux/sched.h>
30#include <linux/timer.h>
31#include <linux/interrupt.h>
32#include <linux/tty.h>
33#include <linux/tty_flip.h>
34#include <linux/major.h>
35#include <linux/string.h>
36#include <linux/fcntl.h>
37#include <linux/ptrace.h>
38#include <linux/serial.h>
39#include <linux/tty_driver.h>
40#include <linux/delay.h>
41#include <linux/pci.h>
42#include <linux/init.h>
43#include <linux/bitops.h>
44
45#include <asm/system.h>
46#include <asm/io.h>
47#include <asm/uaccess.h>
48
11324edd 49#define MOXA_VERSION "5.1k"
1da177e4 50
11324edd
JS
51#define MOXAMAJOR 172
52#define MOXACUMAJOR 173
1da177e4 53
11324edd 54#define MAX_BOARDS 4 /* Don't change this value */
1da177e4 55#define MAX_PORTS_PER_BOARD 32 /* Don't change this value */
11324edd 56#define MAX_PORTS (MAX_BOARDS * MAX_PORTS_PER_BOARD)
1da177e4
LT
57
58/*
59 * Define the Moxa PCI vendor and device IDs.
60 */
11324edd
JS
61#define MOXA_BUS_TYPE_ISA 0
62#define MOXA_BUS_TYPE_PCI 1
1da177e4 63
1da177e4
LT
64enum {
65 MOXA_BOARD_C218_PCI = 1,
66 MOXA_BOARD_C218_ISA,
67 MOXA_BOARD_C320_PCI,
68 MOXA_BOARD_C320_ISA,
69 MOXA_BOARD_CP204J,
70};
71
72static char *moxa_brdname[] =
73{
74 "C218 Turbo PCI series",
75 "C218 Turbo ISA series",
76 "C320 Turbo PCI series",
77 "C320 Turbo ISA series",
78 "CP-204J series",
79};
80
81#ifdef CONFIG_PCI
82static struct pci_device_id moxa_pcibrds[] = {
5ebb4078
JS
83 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C218),
84 .driver_data = MOXA_BOARD_C218_PCI },
85 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C320),
86 .driver_data = MOXA_BOARD_C320_PCI },
87 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP204J),
88 .driver_data = MOXA_BOARD_CP204J },
1da177e4
LT
89 { 0 }
90};
91MODULE_DEVICE_TABLE(pci, moxa_pcibrds);
92#endif /* CONFIG_PCI */
93
9dff89cd 94struct moxa_isa_board_conf {
1da177e4
LT
95 int boardType;
96 int numPorts;
97 unsigned long baseAddr;
9dff89cd 98};
1da177e4 99
9dff89cd 100static struct moxa_isa_board_conf moxa_isa_boards[] =
1da177e4
LT
101{
102/* {MOXA_BOARD_C218_ISA,8,0xDC000}, */
103};
104
8f8ecbad 105static struct moxa_board_conf {
1da177e4
LT
106 int boardType;
107 int numPorts;
108 unsigned long baseAddr;
109 int busType;
889074ee 110 struct pci_dev *pdev;
8f8ecbad
JS
111
112 int loadstat;
113
114 void __iomem *basemem;
115 void __iomem *intNdx;
116 void __iomem *intPend;
117 void __iomem *intTable;
118} moxa_boards[MAX_BOARDS];
119
120struct mxser_mstatus {
121 tcflag_t cflag;
122 int cts;
123 int dsr;
124 int ri;
125 int dcd;
9dff89cd 126};
1da177e4 127
8f8ecbad
JS
128struct moxaq_str {
129 int inq;
130 int outq;
131};
1da177e4 132
8f8ecbad 133struct moxa_port {
1da177e4
LT
134 int type;
135 int port;
136 int close_delay;
137 unsigned short closing_wait;
138 int count;
139 int blocked_open;
140 long event; /* long req'd for set_bit --RR */
141 int asyncflags;
142 unsigned long statusflags;
143 struct tty_struct *tty;
144 int cflag;
145 wait_queue_head_t open_wait;
146 wait_queue_head_t close_wait;
1da177e4 147
8f8ecbad
JS
148 struct timer_list emptyTimer;
149 struct mxser_mstatus GMStatus;
150 struct moxaq_str temp_queue;
1da177e4 151
8f8ecbad
JS
152 char chkPort;
153 char lineCtrl;
154 void __iomem *tableAddr;
155 long curBaud;
156 char DCDState;
157 char lowChkFlag;
158
159 ushort breakCnt;
160};
1da177e4
LT
161
162/* statusflags */
163#define TXSTOPPED 0x1
164#define LOWWAIT 0x2
165#define EMPTYWAIT 0x4
166#define THROTTLE 0x8
167
1da177e4
LT
168#define SERIAL_DO_RESTART
169
1da177e4
LT
170#define WAKEUP_CHARS 256
171
1da177e4
LT
172static int verbose = 0;
173static int ttymajor = MOXAMAJOR;
174/* Variables for insmod */
175#ifdef MODULE
9fa372a8
JS
176static int baseaddr[4];
177static int type[4];
178static int numports[4];
1da177e4
LT
179#endif
180
181MODULE_AUTHOR("William Chen");
182MODULE_DESCRIPTION("MOXA Intellio Family Multiport Board Device Driver");
183MODULE_LICENSE("GPL");
184#ifdef MODULE
185module_param_array(type, int, NULL, 0);
186module_param_array(baseaddr, int, NULL, 0);
187module_param_array(numports, int, NULL, 0);
188#endif
189module_param(ttymajor, int, 0);
190module_param(verbose, bool, 0644);
191
1da177e4
LT
192/*
193 * static functions:
194 */
1da177e4
LT
195static int moxa_open(struct tty_struct *, struct file *);
196static void moxa_close(struct tty_struct *, struct file *);
197static int moxa_write(struct tty_struct *, const unsigned char *, int);
198static int moxa_write_room(struct tty_struct *);
199static void moxa_flush_buffer(struct tty_struct *);
200static int moxa_chars_in_buffer(struct tty_struct *);
201static void moxa_flush_chars(struct tty_struct *);
202static void moxa_put_char(struct tty_struct *, unsigned char);
203static int moxa_ioctl(struct tty_struct *, struct file *, unsigned int, unsigned long);
204static void moxa_throttle(struct tty_struct *);
205static void moxa_unthrottle(struct tty_struct *);
606d099c 206static void moxa_set_termios(struct tty_struct *, struct ktermios *);
1da177e4
LT
207static void moxa_stop(struct tty_struct *);
208static void moxa_start(struct tty_struct *);
209static void moxa_hangup(struct tty_struct *);
210static int moxa_tiocmget(struct tty_struct *tty, struct file *file);
211static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
212 unsigned int set, unsigned int clear);
213static void moxa_poll(unsigned long);
214static void set_tty_param(struct tty_struct *);
215static int block_till_ready(struct tty_struct *, struct file *,
8f8ecbad 216 struct moxa_port *);
1da177e4
LT
217static void setup_empty_event(struct tty_struct *);
218static void check_xmit_empty(unsigned long);
8f8ecbad
JS
219static void shut_down(struct moxa_port *);
220static void receive_data(struct moxa_port *);
1da177e4
LT
221/*
222 * moxa board interface functions:
223 */
224static void MoxaDriverInit(void);
225static int MoxaDriverIoctl(unsigned int, unsigned long, int);
226static int MoxaDriverPoll(void);
227static int MoxaPortsOfCard(int);
228static int MoxaPortIsValid(int);
229static void MoxaPortEnable(int);
230static void MoxaPortDisable(int);
231static long MoxaPortGetMaxBaud(int);
232static long MoxaPortSetBaud(int, long);
606d099c 233static int MoxaPortSetTermio(int, struct ktermios *, speed_t);
1da177e4
LT
234static int MoxaPortGetLineOut(int, int *, int *);
235static void MoxaPortLineCtrl(int, int, int);
236static void MoxaPortFlowCtrl(int, int, int, int, int, int);
237static int MoxaPortLineStatus(int);
238static int MoxaPortDCDChange(int);
239static int MoxaPortDCDON(int);
240static void MoxaPortFlushData(int, int);
241static int MoxaPortWriteData(int, unsigned char *, int);
33f0f88f 242static int MoxaPortReadData(int, struct tty_struct *tty);
1da177e4
LT
243static int MoxaPortTxQueue(int);
244static int MoxaPortRxQueue(int);
245static int MoxaPortTxFree(int);
246static void MoxaPortTxDisable(int);
247static void MoxaPortTxEnable(int);
248static int MoxaPortResetBrkCnt(int);
249static void MoxaPortSendBreak(int, int);
8f8ecbad
JS
250static int moxa_get_serial_info(struct moxa_port *, struct serial_struct __user *);
251static int moxa_set_serial_info(struct moxa_port *, struct serial_struct __user *);
1da177e4
LT
252static void MoxaSetFifo(int port, int enable);
253
b68e31d0 254static const struct tty_operations moxa_ops = {
1da177e4
LT
255 .open = moxa_open,
256 .close = moxa_close,
257 .write = moxa_write,
258 .write_room = moxa_write_room,
259 .flush_buffer = moxa_flush_buffer,
260 .chars_in_buffer = moxa_chars_in_buffer,
261 .flush_chars = moxa_flush_chars,
262 .put_char = moxa_put_char,
263 .ioctl = moxa_ioctl,
264 .throttle = moxa_throttle,
265 .unthrottle = moxa_unthrottle,
266 .set_termios = moxa_set_termios,
267 .stop = moxa_stop,
268 .start = moxa_start,
269 .hangup = moxa_hangup,
270 .tiocmget = moxa_tiocmget,
271 .tiocmset = moxa_tiocmset,
272};
273
aa7e5221 274static struct tty_driver *moxaDriver;
8f8ecbad 275static struct moxa_port moxa_ports[MAX_PORTS];
aa7e5221 276static DEFINE_TIMER(moxaTimer, moxa_poll, 0, 0);
34af946a 277static DEFINE_SPINLOCK(moxa_lock);
33f0f88f 278
1da177e4 279#ifdef CONFIG_PCI
9dff89cd
JS
280static int moxa_get_PCI_conf(struct pci_dev *p, int board_type,
281 struct moxa_board_conf *board)
1da177e4
LT
282{
283 board->baseAddr = pci_resource_start (p, 2);
284 board->boardType = board_type;
285 switch (board_type) {
286 case MOXA_BOARD_C218_ISA:
287 case MOXA_BOARD_C218_PCI:
288 board->numPorts = 8;
289 break;
290
291 case MOXA_BOARD_CP204J:
292 board->numPorts = 4;
293 break;
294 default:
295 board->numPorts = 0;
296 break;
297 }
298 board->busType = MOXA_BUS_TYPE_PCI;
86fbf148 299 /* don't lose the reference in the next pci_get_device iteration */
889074ee 300 board->pdev = pci_dev_get(p);
1da177e4
LT
301
302 return (0);
303}
304#endif /* CONFIG_PCI */
305
306static int __init moxa_init(void)
307{
308 int i, numBoards;
8f8ecbad 309 struct moxa_port *ch;
1da177e4
LT
310
311 printk(KERN_INFO "MOXA Intellio family driver version %s\n", MOXA_VERSION);
312 moxaDriver = alloc_tty_driver(MAX_PORTS + 1);
313 if (!moxaDriver)
314 return -ENOMEM;
315
1da177e4 316 moxaDriver->owner = THIS_MODULE;
9b4e3b13 317 moxaDriver->name = "ttyMX";
1da177e4
LT
318 moxaDriver->major = ttymajor;
319 moxaDriver->minor_start = 0;
320 moxaDriver->type = TTY_DRIVER_TYPE_SERIAL;
321 moxaDriver->subtype = SERIAL_TYPE_NORMAL;
322 moxaDriver->init_termios = tty_std_termios;
1da177e4 323 moxaDriver->init_termios.c_cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
606d099c
AC
324 moxaDriver->init_termios.c_ispeed = 9600;
325 moxaDriver->init_termios.c_ospeed = 9600;
1da177e4
LT
326 moxaDriver->flags = TTY_DRIVER_REAL_RAW;
327 tty_set_operations(moxaDriver, &moxa_ops);
328
8f8ecbad 329 for (i = 0, ch = moxa_ports; i < MAX_PORTS; i++, ch++) {
1da177e4
LT
330 ch->type = PORT_16550A;
331 ch->port = i;
1da177e4
LT
332 ch->close_delay = 5 * HZ / 10;
333 ch->closing_wait = 30 * HZ;
1da177e4
LT
334 ch->cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
335 init_waitqueue_head(&ch->open_wait);
336 init_waitqueue_head(&ch->close_wait);
8f8ecbad
JS
337
338 setup_timer(&ch->emptyTimer, check_xmit_empty,
339 (unsigned long)ch);
1da177e4
LT
340 }
341
1da177e4
LT
342 printk("Tty devices major number = %d\n", ttymajor);
343
344 if (tty_register_driver(moxaDriver)) {
345 printk(KERN_ERR "Couldn't install MOXA Smartio family driver !\n");
346 put_tty_driver(moxaDriver);
347 return -1;
348 }
1da177e4 349
aa7e5221 350 mod_timer(&moxaTimer, jiffies + HZ / 50);
1da177e4
LT
351
352 /* Find the boards defined in source code */
353 numBoards = 0;
354 for (i = 0; i < MAX_BOARDS; i++) {
355 if ((moxa_isa_boards[i].boardType == MOXA_BOARD_C218_ISA) ||
356 (moxa_isa_boards[i].boardType == MOXA_BOARD_C320_ISA)) {
357 moxa_boards[numBoards].boardType = moxa_isa_boards[i].boardType;
358 if (moxa_isa_boards[i].boardType == MOXA_BOARD_C218_ISA)
359 moxa_boards[numBoards].numPorts = 8;
360 else
361 moxa_boards[numBoards].numPorts = moxa_isa_boards[i].numPorts;
362 moxa_boards[numBoards].busType = MOXA_BUS_TYPE_ISA;
363 moxa_boards[numBoards].baseAddr = moxa_isa_boards[i].baseAddr;
364 if (verbose)
365 printk("Board %2d: %s board(baseAddr=%lx)\n",
366 numBoards + 1,
367 moxa_brdname[moxa_boards[numBoards].boardType - 1],
368 moxa_boards[numBoards].baseAddr);
369 numBoards++;
370 }
371 }
372 /* Find the boards defined form module args. */
373#ifdef MODULE
374 for (i = 0; i < MAX_BOARDS; i++) {
375 if ((type[i] == MOXA_BOARD_C218_ISA) ||
376 (type[i] == MOXA_BOARD_C320_ISA)) {
377 if (verbose)
378 printk("Board %2d: %s board(baseAddr=%lx)\n",
379 numBoards + 1,
380 moxa_brdname[type[i] - 1],
381 (unsigned long) baseaddr[i]);
382 if (numBoards >= MAX_BOARDS) {
383 if (verbose)
384 printk("More than %d MOXA Intellio family boards found. Board is ignored.", MAX_BOARDS);
385 continue;
386 }
387 moxa_boards[numBoards].boardType = type[i];
388 if (moxa_isa_boards[i].boardType == MOXA_BOARD_C218_ISA)
389 moxa_boards[numBoards].numPorts = 8;
390 else
391 moxa_boards[numBoards].numPorts = numports[i];
392 moxa_boards[numBoards].busType = MOXA_BUS_TYPE_ISA;
393 moxa_boards[numBoards].baseAddr = baseaddr[i];
394 numBoards++;
395 }
396 }
397#endif
398 /* Find PCI boards here */
399#ifdef CONFIG_PCI
400 {
401 struct pci_dev *p = NULL;
fe971071 402 int n = ARRAY_SIZE(moxa_pcibrds) - 1;
1da177e4
LT
403 i = 0;
404 while (i < n) {
881a8c12 405 while ((p = pci_get_device(moxa_pcibrds[i].vendor, moxa_pcibrds[i].device, p))!=NULL)
1da177e4
LT
406 {
407 if (pci_enable_device(p))
408 continue;
409 if (numBoards >= MAX_BOARDS) {
410 if (verbose)
411 printk("More than %d MOXA Intellio family boards found. Board is ignored.", MAX_BOARDS);
412 } else {
413 moxa_get_PCI_conf(p, moxa_pcibrds[i].driver_data,
414 &moxa_boards[numBoards]);
415 numBoards++;
416 }
417 }
418 i++;
419 }
420 }
421#endif
422 for (i = 0; i < numBoards; i++) {
8f8ecbad
JS
423 moxa_boards[i].basemem = ioremap(moxa_boards[i].baseAddr,
424 0x4000);
1da177e4
LT
425 }
426
427 return (0);
428}
429
430static void __exit moxa_exit(void)
431{
432 int i;
433
434 if (verbose)
435 printk("Unloading module moxa ...\n");
436
c251ae0d 437 del_timer_sync(&moxaTimer);
1da177e4
LT
438
439 for (i = 0; i < MAX_PORTS; i++)
8f8ecbad 440 del_timer_sync(&moxa_ports[i].emptyTimer);
1da177e4
LT
441
442 if (tty_unregister_driver(moxaDriver))
443 printk("Couldn't unregister MOXA Intellio family serial driver\n");
444 put_tty_driver(moxaDriver);
86fbf148 445
41bdabbb 446 for (i = 0; i < MAX_BOARDS; i++) {
8f8ecbad
JS
447 if (moxa_boards[i].basemem)
448 iounmap(moxa_boards[i].basemem);
86fbf148 449 if (moxa_boards[i].busType == MOXA_BUS_TYPE_PCI)
889074ee 450 pci_dev_put(moxa_boards[i].pdev);
41bdabbb 451 }
86fbf148 452
1da177e4
LT
453 if (verbose)
454 printk("Done\n");
455}
456
457module_init(moxa_init);
458module_exit(moxa_exit);
459
1da177e4
LT
460static int moxa_open(struct tty_struct *tty, struct file *filp)
461{
8f8ecbad 462 struct moxa_port *ch;
1da177e4
LT
463 int port;
464 int retval;
1da177e4 465
11324edd 466 port = tty->index;
1da177e4
LT
467 if (port == MAX_PORTS) {
468 return (0);
469 }
470 if (!MoxaPortIsValid(port)) {
471 tty->driver_data = NULL;
472 return (-ENODEV);
473 }
1da177e4 474
8f8ecbad 475 ch = &moxa_ports[port];
1da177e4
LT
476 ch->count++;
477 tty->driver_data = ch;
478 ch->tty = tty;
479 if (!(ch->asyncflags & ASYNC_INITIALIZED)) {
480 ch->statusflags = 0;
481 set_tty_param(tty);
482 MoxaPortLineCtrl(ch->port, 1, 1);
483 MoxaPortEnable(ch->port);
484 ch->asyncflags |= ASYNC_INITIALIZED;
485 }
486 retval = block_till_ready(tty, filp, ch);
487
488 moxa_unthrottle(tty);
489
490 if (ch->type == PORT_16550A) {
491 MoxaSetFifo(ch->port, 1);
492 } else {
493 MoxaSetFifo(ch->port, 0);
494 }
495
496 return (retval);
497}
498
499static void moxa_close(struct tty_struct *tty, struct file *filp)
500{
8f8ecbad 501 struct moxa_port *ch;
1da177e4
LT
502 int port;
503
11324edd 504 port = tty->index;
1da177e4
LT
505 if (port == MAX_PORTS) {
506 return;
507 }
508 if (!MoxaPortIsValid(port)) {
509#ifdef SERIAL_DEBUG_CLOSE
510 printk("Invalid portno in moxa_close\n");
511#endif
512 tty->driver_data = NULL;
513 return;
514 }
515 if (tty->driver_data == NULL) {
516 return;
517 }
518 if (tty_hung_up_p(filp)) {
519 return;
520 }
8f8ecbad 521 ch = (struct moxa_port *) tty->driver_data;
1da177e4
LT
522
523 if ((tty->count == 1) && (ch->count != 1)) {
524 printk("moxa_close: bad serial port count; tty->count is 1, "
525 "ch->count is %d\n", ch->count);
526 ch->count = 1;
527 }
528 if (--ch->count < 0) {
529 printk("moxa_close: bad serial port count, device=%s\n",
530 tty->name);
531 ch->count = 0;
532 }
533 if (ch->count) {
534 return;
535 }
536 ch->asyncflags |= ASYNC_CLOSING;
537
538 ch->cflag = tty->termios->c_cflag;
539 if (ch->asyncflags & ASYNC_INITIALIZED) {
540 setup_empty_event(tty);
541 tty_wait_until_sent(tty, 30 * HZ); /* 30 seconds timeout */
8f8ecbad 542 del_timer_sync(&moxa_ports[ch->port].emptyTimer);
1da177e4
LT
543 }
544 shut_down(ch);
545 MoxaPortFlushData(port, 2);
546
547 if (tty->driver->flush_buffer)
548 tty->driver->flush_buffer(tty);
549 tty_ldisc_flush(tty);
550
551 tty->closing = 0;
552 ch->event = 0;
553 ch->tty = NULL;
554 if (ch->blocked_open) {
555 if (ch->close_delay) {
556 msleep_interruptible(jiffies_to_msecs(ch->close_delay));
557 }
558 wake_up_interruptible(&ch->open_wait);
559 }
560 ch->asyncflags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
561 wake_up_interruptible(&ch->close_wait);
562}
563
564static int moxa_write(struct tty_struct *tty,
565 const unsigned char *buf, int count)
566{
8f8ecbad 567 struct moxa_port *ch;
1da177e4
LT
568 int len, port;
569 unsigned long flags;
570
8f8ecbad 571 ch = (struct moxa_port *) tty->driver_data;
1da177e4
LT
572 if (ch == NULL)
573 return (0);
574 port = ch->port;
33f0f88f
AC
575
576 spin_lock_irqsave(&moxa_lock, flags);
1da177e4 577 len = MoxaPortWriteData(port, (unsigned char *) buf, count);
33f0f88f 578 spin_unlock_irqrestore(&moxa_lock, flags);
1da177e4
LT
579
580 /*********************************************
581 if ( !(ch->statusflags & LOWWAIT) &&
582 ((len != count) || (MoxaPortTxFree(port) <= 100)) )
583 ************************************************/
584 ch->statusflags |= LOWWAIT;
585 return (len);
586}
587
588static int moxa_write_room(struct tty_struct *tty)
589{
8f8ecbad 590 struct moxa_port *ch;
1da177e4
LT
591
592 if (tty->stopped)
593 return (0);
8f8ecbad 594 ch = (struct moxa_port *) tty->driver_data;
1da177e4
LT
595 if (ch == NULL)
596 return (0);
597 return (MoxaPortTxFree(ch->port));
598}
599
600static void moxa_flush_buffer(struct tty_struct *tty)
601{
8f8ecbad 602 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
1da177e4
LT
603
604 if (ch == NULL)
605 return;
606 MoxaPortFlushData(ch->port, 1);
607 tty_wakeup(tty);
608}
609
610static int moxa_chars_in_buffer(struct tty_struct *tty)
611{
612 int chars;
8f8ecbad 613 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
1da177e4
LT
614
615 /*
616 * Sigh...I have to check if driver_data is NULL here, because
617 * if an open() fails, the TTY subsystem eventually calls
618 * tty_wait_until_sent(), which calls the driver's chars_in_buffer()
619 * routine. And since the open() failed, we return 0 here. TDJ
620 */
621 if (ch == NULL)
622 return (0);
623 chars = MoxaPortTxQueue(ch->port);
624 if (chars) {
625 /*
626 * Make it possible to wakeup anything waiting for output
627 * in tty_ioctl.c, etc.
628 */
629 if (!(ch->statusflags & EMPTYWAIT))
630 setup_empty_event(tty);
631 }
632 return (chars);
633}
634
635static void moxa_flush_chars(struct tty_struct *tty)
636{
637 /*
638 * Don't think I need this, because this is called to empty the TX
639 * buffer for the 16450, 16550, etc.
640 */
641}
642
643static void moxa_put_char(struct tty_struct *tty, unsigned char c)
644{
8f8ecbad 645 struct moxa_port *ch;
1da177e4
LT
646 int port;
647 unsigned long flags;
648
8f8ecbad 649 ch = (struct moxa_port *) tty->driver_data;
1da177e4
LT
650 if (ch == NULL)
651 return;
652 port = ch->port;
33f0f88f 653 spin_lock_irqsave(&moxa_lock, flags);
f204d267 654 MoxaPortWriteData(port, &c, 1);
33f0f88f 655 spin_unlock_irqrestore(&moxa_lock, flags);
1da177e4
LT
656 /************************************************
657 if ( !(ch->statusflags & LOWWAIT) && (MoxaPortTxFree(port) <= 100) )
658 *************************************************/
659 ch->statusflags |= LOWWAIT;
660}
661
662static int moxa_tiocmget(struct tty_struct *tty, struct file *file)
663{
8f8ecbad 664 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
1da177e4
LT
665 int port;
666 int flag = 0, dtr, rts;
667
11324edd 668 port = tty->index;
1da177e4
LT
669 if ((port != MAX_PORTS) && (!ch))
670 return (-EINVAL);
671
672 MoxaPortGetLineOut(ch->port, &dtr, &rts);
673 if (dtr)
674 flag |= TIOCM_DTR;
675 if (rts)
676 flag |= TIOCM_RTS;
677 dtr = MoxaPortLineStatus(ch->port);
678 if (dtr & 1)
679 flag |= TIOCM_CTS;
680 if (dtr & 2)
681 flag |= TIOCM_DSR;
682 if (dtr & 4)
683 flag |= TIOCM_CD;
684 return flag;
685}
686
687static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
688 unsigned int set, unsigned int clear)
689{
8f8ecbad 690 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
1da177e4
LT
691 int port;
692 int dtr, rts;
693
11324edd 694 port = tty->index;
1da177e4
LT
695 if ((port != MAX_PORTS) && (!ch))
696 return (-EINVAL);
697
698 MoxaPortGetLineOut(ch->port, &dtr, &rts);
699 if (set & TIOCM_RTS)
700 rts = 1;
701 if (set & TIOCM_DTR)
702 dtr = 1;
703 if (clear & TIOCM_RTS)
704 rts = 0;
705 if (clear & TIOCM_DTR)
706 dtr = 0;
707 MoxaPortLineCtrl(ch->port, dtr, rts);
708 return 0;
709}
710
711static int moxa_ioctl(struct tty_struct *tty, struct file *file,
712 unsigned int cmd, unsigned long arg)
713{
8f8ecbad 714 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
1da177e4
LT
715 register int port;
716 void __user *argp = (void __user *)arg;
717 int retval;
718
11324edd 719 port = tty->index;
1da177e4
LT
720 if ((port != MAX_PORTS) && (!ch))
721 return (-EINVAL);
722
723 switch (cmd) {
724 case TCSBRK: /* SVID version: non-zero arg --> no break */
725 retval = tty_check_change(tty);
726 if (retval)
727 return (retval);
728 setup_empty_event(tty);
729 tty_wait_until_sent(tty, 0);
730 if (!arg)
731 MoxaPortSendBreak(ch->port, 0);
732 return (0);
733 case TCSBRKP: /* support for POSIX tcsendbreak() */
734 retval = tty_check_change(tty);
735 if (retval)
736 return (retval);
737 setup_empty_event(tty);
738 tty_wait_until_sent(tty, 0);
739 MoxaPortSendBreak(ch->port, arg);
740 return (0);
741 case TIOCGSOFTCAR:
742 return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *) argp);
743 case TIOCSSOFTCAR:
744 if(get_user(retval, (unsigned long __user *) argp))
745 return -EFAULT;
746 arg = retval;
747 tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) |
748 (arg ? CLOCAL : 0));
749 if (C_CLOCAL(tty))
750 ch->asyncflags &= ~ASYNC_CHECK_CD;
751 else
752 ch->asyncflags |= ASYNC_CHECK_CD;
753 return (0);
754 case TIOCGSERIAL:
755 return moxa_get_serial_info(ch, argp);
756
757 case TIOCSSERIAL:
758 return moxa_set_serial_info(ch, argp);
759 default:
760 retval = MoxaDriverIoctl(cmd, arg, port);
761 }
762 return (retval);
763}
764
765static void moxa_throttle(struct tty_struct *tty)
766{
8f8ecbad 767 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
1da177e4
LT
768
769 ch->statusflags |= THROTTLE;
770}
771
772static void moxa_unthrottle(struct tty_struct *tty)
773{
8f8ecbad 774 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
1da177e4
LT
775
776 ch->statusflags &= ~THROTTLE;
777}
778
779static void moxa_set_termios(struct tty_struct *tty,
606d099c 780 struct ktermios *old_termios)
1da177e4 781{
8f8ecbad 782 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
1da177e4
LT
783
784 if (ch == NULL)
785 return;
786 set_tty_param(tty);
787 if (!(old_termios->c_cflag & CLOCAL) &&
788 (tty->termios->c_cflag & CLOCAL))
789 wake_up_interruptible(&ch->open_wait);
790}
791
792static void moxa_stop(struct tty_struct *tty)
793{
8f8ecbad 794 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
1da177e4
LT
795
796 if (ch == NULL)
797 return;
798 MoxaPortTxDisable(ch->port);
799 ch->statusflags |= TXSTOPPED;
800}
801
802
803static void moxa_start(struct tty_struct *tty)
804{
8f8ecbad 805 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
1da177e4
LT
806
807 if (ch == NULL)
808 return;
809
810 if (!(ch->statusflags & TXSTOPPED))
811 return;
812
813 MoxaPortTxEnable(ch->port);
814 ch->statusflags &= ~TXSTOPPED;
815}
816
817static void moxa_hangup(struct tty_struct *tty)
818{
8f8ecbad 819 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
1da177e4
LT
820
821 moxa_flush_buffer(tty);
822 shut_down(ch);
823 ch->event = 0;
824 ch->count = 0;
825 ch->asyncflags &= ~ASYNC_NORMAL_ACTIVE;
826 ch->tty = NULL;
827 wake_up_interruptible(&ch->open_wait);
828}
829
830static void moxa_poll(unsigned long ignored)
831{
832 register int card;
8f8ecbad 833 struct moxa_port *ch;
1da177e4
LT
834 struct tty_struct *tp;
835 int i, ports;
836
1da177e4
LT
837 del_timer(&moxaTimer);
838
839 if (MoxaDriverPoll() < 0) {
aa7e5221 840 mod_timer(&moxaTimer, jiffies + HZ / 50);
1da177e4
LT
841 return;
842 }
843 for (card = 0; card < MAX_BOARDS; card++) {
844 if ((ports = MoxaPortsOfCard(card)) <= 0)
845 continue;
8f8ecbad 846 ch = &moxa_ports[card * MAX_PORTS_PER_BOARD];
1da177e4
LT
847 for (i = 0; i < ports; i++, ch++) {
848 if ((ch->asyncflags & ASYNC_INITIALIZED) == 0)
849 continue;
850 if (!(ch->statusflags & THROTTLE) &&
851 (MoxaPortRxQueue(ch->port) > 0))
852 receive_data(ch);
853 if ((tp = ch->tty) == 0)
854 continue;
855 if (ch->statusflags & LOWWAIT) {
856 if (MoxaPortTxQueue(ch->port) <= WAKEUP_CHARS) {
857 if (!tp->stopped) {
858 ch->statusflags &= ~LOWWAIT;
859 tty_wakeup(tp);
860 }
861 }
862 }
863 if (!I_IGNBRK(tp) && (MoxaPortResetBrkCnt(ch->port) > 0)) {
864 tty_insert_flip_char(tp, 0, TTY_BREAK);
865 tty_schedule_flip(tp);
866 }
867 if (MoxaPortDCDChange(ch->port)) {
868 if (ch->asyncflags & ASYNC_CHECK_CD) {
869 if (MoxaPortDCDON(ch->port))
870 wake_up_interruptible(&ch->open_wait);
871 else {
ba196df3
JS
872 tty_hangup(tp);
873 wake_up_interruptible(&ch->open_wait);
874 ch->asyncflags &= ~ASYNC_NORMAL_ACTIVE;
1da177e4
LT
875 }
876 }
877 }
878 }
879 }
880
aa7e5221 881 mod_timer(&moxaTimer, jiffies + HZ / 50);
1da177e4
LT
882}
883
884/******************************************************************************/
885
886static void set_tty_param(struct tty_struct *tty)
887{
606d099c 888 register struct ktermios *ts;
8f8ecbad 889 struct moxa_port *ch;
1da177e4
LT
890 int rts, cts, txflow, rxflow, xany;
891
8f8ecbad 892 ch = (struct moxa_port *) tty->driver_data;
1da177e4
LT
893 ts = tty->termios;
894 if (ts->c_cflag & CLOCAL)
895 ch->asyncflags &= ~ASYNC_CHECK_CD;
896 else
897 ch->asyncflags |= ASYNC_CHECK_CD;
898 rts = cts = txflow = rxflow = xany = 0;
899 if (ts->c_cflag & CRTSCTS)
900 rts = cts = 1;
901 if (ts->c_iflag & IXON)
902 txflow = 1;
903 if (ts->c_iflag & IXOFF)
904 rxflow = 1;
905 if (ts->c_iflag & IXANY)
906 xany = 1;
907 MoxaPortFlowCtrl(ch->port, rts, cts, txflow, rxflow, xany);
c7bce309 908 MoxaPortSetTermio(ch->port, ts, tty_get_baud_rate(tty));
1da177e4
LT
909}
910
911static int block_till_ready(struct tty_struct *tty, struct file *filp,
8f8ecbad 912 struct moxa_port *ch)
1da177e4
LT
913{
914 DECLARE_WAITQUEUE(wait,current);
915 unsigned long flags;
916 int retval;
917 int do_clocal = C_CLOCAL(tty);
918
919 /*
920 * If the device is in the middle of being closed, then block
921 * until it's done, and then try again.
922 */
923 if (tty_hung_up_p(filp) || (ch->asyncflags & ASYNC_CLOSING)) {
924 if (ch->asyncflags & ASYNC_CLOSING)
925 interruptible_sleep_on(&ch->close_wait);
926#ifdef SERIAL_DO_RESTART
927 if (ch->asyncflags & ASYNC_HUP_NOTIFY)
928 return (-EAGAIN);
929 else
930 return (-ERESTARTSYS);
931#else
932 return (-EAGAIN);
933#endif
934 }
935 /*
936 * If non-blocking mode is set, then make the check up front
937 * and then exit.
938 */
939 if (filp->f_flags & O_NONBLOCK) {
940 ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
941 return (0);
942 }
943 /*
944 * Block waiting for the carrier detect and the line to become free
945 */
946 retval = 0;
947 add_wait_queue(&ch->open_wait, &wait);
948#ifdef SERIAL_DEBUG_OPEN
949 printk("block_til_ready before block: ttys%d, count = %d\n",
950 ch->line, ch->count);
951#endif
33f0f88f 952 spin_lock_irqsave(&moxa_lock, flags);
1da177e4
LT
953 if (!tty_hung_up_p(filp))
954 ch->count--;
1da177e4 955 ch->blocked_open++;
33f0f88f
AC
956 spin_unlock_irqrestore(&moxa_lock, flags);
957
1da177e4
LT
958 while (1) {
959 set_current_state(TASK_INTERRUPTIBLE);
960 if (tty_hung_up_p(filp) ||
961 !(ch->asyncflags & ASYNC_INITIALIZED)) {
962#ifdef SERIAL_DO_RESTART
963 if (ch->asyncflags & ASYNC_HUP_NOTIFY)
964 retval = -EAGAIN;
965 else
966 retval = -ERESTARTSYS;
967#else
968 retval = -EAGAIN;
969#endif
970 break;
971 }
972 if (!(ch->asyncflags & ASYNC_CLOSING) && (do_clocal ||
973 MoxaPortDCDON(ch->port)))
974 break;
975
976 if (signal_pending(current)) {
977 retval = -ERESTARTSYS;
978 break;
979 }
980 schedule();
981 }
982 set_current_state(TASK_RUNNING);
983 remove_wait_queue(&ch->open_wait, &wait);
33f0f88f
AC
984
985 spin_lock_irqsave(&moxa_lock, flags);
1da177e4
LT
986 if (!tty_hung_up_p(filp))
987 ch->count++;
988 ch->blocked_open--;
33f0f88f 989 spin_unlock_irqrestore(&moxa_lock, flags);
1da177e4
LT
990#ifdef SERIAL_DEBUG_OPEN
991 printk("block_til_ready after blocking: ttys%d, count = %d\n",
992 ch->line, ch->count);
993#endif
994 if (retval)
995 return (retval);
33f0f88f 996 /* FIXME: review to see if we need to use set_bit on these */
1da177e4 997 ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
33f0f88f 998 return 0;
1da177e4
LT
999}
1000
1001static void setup_empty_event(struct tty_struct *tty)
1002{
8f8ecbad 1003 struct moxa_port *ch = tty->driver_data;
1da177e4
LT
1004 unsigned long flags;
1005
33f0f88f 1006 spin_lock_irqsave(&moxa_lock, flags);
1da177e4 1007 ch->statusflags |= EMPTYWAIT;
8f8ecbad 1008 mod_timer(&moxa_ports[ch->port].emptyTimer, jiffies + HZ);
33f0f88f 1009 spin_unlock_irqrestore(&moxa_lock, flags);
1da177e4
LT
1010}
1011
1012static void check_xmit_empty(unsigned long data)
1013{
8f8ecbad 1014 struct moxa_port *ch;
1da177e4 1015
8f8ecbad
JS
1016 ch = (struct moxa_port *) data;
1017 del_timer_sync(&moxa_ports[ch->port].emptyTimer);
1da177e4
LT
1018 if (ch->tty && (ch->statusflags & EMPTYWAIT)) {
1019 if (MoxaPortTxQueue(ch->port) == 0) {
1020 ch->statusflags &= ~EMPTYWAIT;
1021 tty_wakeup(ch->tty);
1022 return;
1023 }
8f8ecbad 1024 mod_timer(&moxa_ports[ch->port].emptyTimer, jiffies + HZ);
1da177e4
LT
1025 } else
1026 ch->statusflags &= ~EMPTYWAIT;
1027}
1028
8f8ecbad 1029static void shut_down(struct moxa_port *ch)
1da177e4
LT
1030{
1031 struct tty_struct *tp;
1032
1033 if (!(ch->asyncflags & ASYNC_INITIALIZED))
1034 return;
1035
1036 tp = ch->tty;
1037
1038 MoxaPortDisable(ch->port);
1039
1040 /*
1041 * If we're a modem control device and HUPCL is on, drop RTS & DTR.
1042 */
1043 if (tp->termios->c_cflag & HUPCL)
1044 MoxaPortLineCtrl(ch->port, 0, 0);
1045
1046 ch->asyncflags &= ~ASYNC_INITIALIZED;
1047}
1048
8f8ecbad 1049static void receive_data(struct moxa_port *ch)
1da177e4
LT
1050{
1051 struct tty_struct *tp;
606d099c 1052 struct ktermios *ts;
1da177e4
LT
1053 unsigned long flags;
1054
1055 ts = NULL;
1056 tp = ch->tty;
1057 if (tp)
1058 ts = tp->termios;
1059 /**************************************************
1060 if ( !tp || !ts || !(ts->c_cflag & CREAD) ) {
1061 *****************************************************/
1062 if (!tp || !ts) {
1063 MoxaPortFlushData(ch->port, 0);
1064 return;
1065 }
33f0f88f
AC
1066 spin_lock_irqsave(&moxa_lock, flags);
1067 MoxaPortReadData(ch->port, tp);
1068 spin_unlock_irqrestore(&moxa_lock, flags);
1069 tty_schedule_flip(tp);
1da177e4
LT
1070}
1071
1072#define Magic_code 0x404
1073
1074/*
1075 * System Configuration
1076 */
1077/*
1078 * for C218 BIOS initialization
1079 */
1080#define C218_ConfBase 0x800
1081#define C218_status (C218_ConfBase + 0) /* BIOS running status */
1082#define C218_diag (C218_ConfBase + 2) /* diagnostic status */
1083#define C218_key (C218_ConfBase + 4) /* WORD (0x218 for C218) */
1084#define C218DLoad_len (C218_ConfBase + 6) /* WORD */
1085#define C218check_sum (C218_ConfBase + 8) /* BYTE */
1086#define C218chksum_ok (C218_ConfBase + 0x0a) /* BYTE (1:ok) */
1087#define C218_TestRx (C218_ConfBase + 0x10) /* 8 bytes for 8 ports */
1088#define C218_TestTx (C218_ConfBase + 0x18) /* 8 bytes for 8 ports */
1089#define C218_RXerr (C218_ConfBase + 0x20) /* 8 bytes for 8 ports */
1090#define C218_ErrFlag (C218_ConfBase + 0x28) /* 8 bytes for 8 ports */
1091
1092#define C218_LoadBuf 0x0F00
1093#define C218_KeyCode 0x218
1094#define CP204J_KeyCode 0x204
1095
1096/*
1097 * for C320 BIOS initialization
1098 */
1099#define C320_ConfBase 0x800
1100#define C320_LoadBuf 0x0f00
1101#define STS_init 0x05 /* for C320_status */
1102
1103#define C320_status C320_ConfBase + 0 /* BIOS running status */
1104#define C320_diag C320_ConfBase + 2 /* diagnostic status */
1105#define C320_key C320_ConfBase + 4 /* WORD (0320H for C320) */
1106#define C320DLoad_len C320_ConfBase + 6 /* WORD */
1107#define C320check_sum C320_ConfBase + 8 /* WORD */
1108#define C320chksum_ok C320_ConfBase + 0x0a /* WORD (1:ok) */
1109#define C320bapi_len C320_ConfBase + 0x0c /* WORD */
1110#define C320UART_no C320_ConfBase + 0x0e /* WORD */
1111
1112#define C320_KeyCode 0x320
1113
1114#define FixPage_addr 0x0000 /* starting addr of static page */
1115#define DynPage_addr 0x2000 /* starting addr of dynamic page */
1116#define C218_start 0x3000 /* starting addr of C218 BIOS prg */
1117#define Control_reg 0x1ff0 /* select page and reset control */
1118#define HW_reset 0x80
1119
1120/*
1121 * Function Codes
1122 */
1123#define FC_CardReset 0x80
1124#define FC_ChannelReset 1 /* C320 firmware not supported */
1125#define FC_EnableCH 2
1126#define FC_DisableCH 3
1127#define FC_SetParam 4
1128#define FC_SetMode 5
1129#define FC_SetRate 6
1130#define FC_LineControl 7
1131#define FC_LineStatus 8
1132#define FC_XmitControl 9
1133#define FC_FlushQueue 10
1134#define FC_SendBreak 11
1135#define FC_StopBreak 12
1136#define FC_LoopbackON 13
1137#define FC_LoopbackOFF 14
1138#define FC_ClrIrqTable 15
1139#define FC_SendXon 16
1140#define FC_SetTermIrq 17 /* C320 firmware not supported */
1141#define FC_SetCntIrq 18 /* C320 firmware not supported */
1142#define FC_SetBreakIrq 19
1143#define FC_SetLineIrq 20
1144#define FC_SetFlowCtl 21
1145#define FC_GenIrq 22
1146#define FC_InCD180 23
1147#define FC_OutCD180 24
1148#define FC_InUARTreg 23
1149#define FC_OutUARTreg 24
1150#define FC_SetXonXoff 25
1151#define FC_OutCD180CCR 26
1152#define FC_ExtIQueue 27
1153#define FC_ExtOQueue 28
1154#define FC_ClrLineIrq 29
1155#define FC_HWFlowCtl 30
1156#define FC_GetClockRate 35
1157#define FC_SetBaud 36
1158#define FC_SetDataMode 41
1159#define FC_GetCCSR 43
1160#define FC_GetDataError 45
1161#define FC_RxControl 50
1162#define FC_ImmSend 51
1163#define FC_SetXonState 52
1164#define FC_SetXoffState 53
1165#define FC_SetRxFIFOTrig 54
1166#define FC_SetTxFIFOCnt 55
1167#define FC_UnixRate 56
1168#define FC_UnixResetTimer 57
1169
1170#define RxFIFOTrig1 0
1171#define RxFIFOTrig4 1
1172#define RxFIFOTrig8 2
1173#define RxFIFOTrig14 3
1174
1175/*
1176 * Dual-Ported RAM
1177 */
1178#define DRAM_global 0
1179#define INT_data (DRAM_global + 0)
1180#define Config_base (DRAM_global + 0x108)
1181
1182#define IRQindex (INT_data + 0)
1183#define IRQpending (INT_data + 4)
1184#define IRQtable (INT_data + 8)
1185
1186/*
1187 * Interrupt Status
1188 */
1189#define IntrRx 0x01 /* receiver data O.K. */
1190#define IntrTx 0x02 /* transmit buffer empty */
1191#define IntrFunc 0x04 /* function complete */
1192#define IntrBreak 0x08 /* received break */
1193#define IntrLine 0x10 /* line status change
1194 for transmitter */
1195#define IntrIntr 0x20 /* received INTR code */
1196#define IntrQuit 0x40 /* received QUIT code */
1197#define IntrEOF 0x80 /* received EOF code */
1198
1199#define IntrRxTrigger 0x100 /* rx data count reach tigger value */
1200#define IntrTxTrigger 0x200 /* tx data count below trigger value */
1201
1202#define Magic_no (Config_base + 0)
1203#define Card_model_no (Config_base + 2)
1204#define Total_ports (Config_base + 4)
1205#define Module_cnt (Config_base + 8)
1206#define Module_no (Config_base + 10)
1207#define Timer_10ms (Config_base + 14)
1208#define Disable_IRQ (Config_base + 20)
1209#define TMS320_PORT1 (Config_base + 22)
1210#define TMS320_PORT2 (Config_base + 24)
1211#define TMS320_CLOCK (Config_base + 26)
1212
1213/*
1214 * DATA BUFFER in DRAM
1215 */
1216#define Extern_table 0x400 /* Base address of the external table
1217 (24 words * 64) total 3K bytes
1218 (24 words * 128) total 6K bytes */
1219#define Extern_size 0x60 /* 96 bytes */
1220#define RXrptr 0x00 /* read pointer for RX buffer */
1221#define RXwptr 0x02 /* write pointer for RX buffer */
1222#define TXrptr 0x04 /* read pointer for TX buffer */
1223#define TXwptr 0x06 /* write pointer for TX buffer */
1224#define HostStat 0x08 /* IRQ flag and general flag */
1225#define FlagStat 0x0A
1226#define FlowControl 0x0C /* B7 B6 B5 B4 B3 B2 B1 B0 */
1227 /* x x x x | | | | */
1228 /* | | | + CTS flow */
1229 /* | | +--- RTS flow */
1230 /* | +------ TX Xon/Xoff */
1231 /* +--------- RX Xon/Xoff */
1232#define Break_cnt 0x0E /* received break count */
1233#define CD180TXirq 0x10 /* if non-0: enable TX irq */
1234#define RX_mask 0x12
1235#define TX_mask 0x14
1236#define Ofs_rxb 0x16
1237#define Ofs_txb 0x18
1238#define Page_rxb 0x1A
1239#define Page_txb 0x1C
1240#define EndPage_rxb 0x1E
1241#define EndPage_txb 0x20
1242#define Data_error 0x22
1243#define RxTrigger 0x28
1244#define TxTrigger 0x2a
1245
1246#define rRXwptr 0x34
1247#define Low_water 0x36
1248
1249#define FuncCode 0x40
1250#define FuncArg 0x42
1251#define FuncArg1 0x44
1252
1253#define C218rx_size 0x2000 /* 8K bytes */
1254#define C218tx_size 0x8000 /* 32K bytes */
1255
1256#define C218rx_mask (C218rx_size - 1)
1257#define C218tx_mask (C218tx_size - 1)
1258
1259#define C320p8rx_size 0x2000
1260#define C320p8tx_size 0x8000
1261#define C320p8rx_mask (C320p8rx_size - 1)
1262#define C320p8tx_mask (C320p8tx_size - 1)
1263
1264#define C320p16rx_size 0x2000
1265#define C320p16tx_size 0x4000
1266#define C320p16rx_mask (C320p16rx_size - 1)
1267#define C320p16tx_mask (C320p16tx_size - 1)
1268
1269#define C320p24rx_size 0x2000
1270#define C320p24tx_size 0x2000
1271#define C320p24rx_mask (C320p24rx_size - 1)
1272#define C320p24tx_mask (C320p24tx_size - 1)
1273
1274#define C320p32rx_size 0x1000
1275#define C320p32tx_size 0x1000
1276#define C320p32rx_mask (C320p32rx_size - 1)
1277#define C320p32tx_mask (C320p32tx_size - 1)
1278
1279#define Page_size 0x2000
1280#define Page_mask (Page_size - 1)
1281#define C218rx_spage 3
1282#define C218tx_spage 4
1283#define C218rx_pageno 1
1284#define C218tx_pageno 4
1285#define C218buf_pageno 5
1286
1287#define C320p8rx_spage 3
1288#define C320p8tx_spage 4
1289#define C320p8rx_pgno 1
1290#define C320p8tx_pgno 4
1291#define C320p8buf_pgno 5
1292
1293#define C320p16rx_spage 3
1294#define C320p16tx_spage 4
1295#define C320p16rx_pgno 1
1296#define C320p16tx_pgno 2
1297#define C320p16buf_pgno 3
1298
1299#define C320p24rx_spage 3
1300#define C320p24tx_spage 4
1301#define C320p24rx_pgno 1
1302#define C320p24tx_pgno 1
1303#define C320p24buf_pgno 2
1304
1305#define C320p32rx_spage 3
1306#define C320p32tx_ofs C320p32rx_size
1307#define C320p32tx_spage 3
1308#define C320p32buf_pgno 1
1309
1310/*
1311 * Host Status
1312 */
1313#define WakeupRx 0x01
1314#define WakeupTx 0x02
1315#define WakeupBreak 0x08
1316#define WakeupLine 0x10
1317#define WakeupIntr 0x20
1318#define WakeupQuit 0x40
1319#define WakeupEOF 0x80 /* used in VTIME control */
1320#define WakeupRxTrigger 0x100
1321#define WakeupTxTrigger 0x200
1322/*
1323 * Flag status
1324 */
1325#define Rx_over 0x01
1326#define Xoff_state 0x02
1327#define Tx_flowOff 0x04
1328#define Tx_enable 0x08
1329#define CTS_state 0x10
1330#define DSR_state 0x20
1331#define DCD_state 0x80
1332/*
1333 * FlowControl
1334 */
1335#define CTS_FlowCtl 1
1336#define RTS_FlowCtl 2
1337#define Tx_FlowCtl 4
1338#define Rx_FlowCtl 8
1339#define IXM_IXANY 0x10
1340
1341#define LowWater 128
1342
1343#define DTR_ON 1
1344#define RTS_ON 2
1345#define CTS_ON 1
1346#define DSR_ON 2
1347#define DCD_ON 8
1348
1349/* mode definition */
1350#define MX_CS8 0x03
1351#define MX_CS7 0x02
1352#define MX_CS6 0x01
1353#define MX_CS5 0x00
1354
1355#define MX_STOP1 0x00
1356#define MX_STOP15 0x04
1357#define MX_STOP2 0x08
1358
1359#define MX_PARNONE 0x00
1360#define MX_PAREVEN 0x40
1361#define MX_PARODD 0xC0
1362
1363/*
1364 * Query
1365 */
1da177e4
LT
1366
1367struct mon_str {
1368 int tick;
1369 int rxcnt[MAX_PORTS];
1370 int txcnt[MAX_PORTS];
1371};
1da177e4
LT
1372
1373#define DCD_changed 0x01
1374#define DCD_oldstate 0x80
1375
1376static unsigned char moxaBuff[10240];
1da177e4
LT
1377static int moxaLowWaterChk;
1378static int moxaCard;
9dff89cd 1379static struct mon_str moxaLog;
9fa372a8 1380static int moxaFuncTout = HZ / 2;
1da177e4
LT
1381
1382static void moxadelay(int);
1383static void moxafunc(void __iomem *, int, ushort);
1384static void wait_finish(void __iomem *);
1385static void low_water_check(void __iomem *);
1386static int moxaloadbios(int, unsigned char __user *, int);
1387static int moxafindcard(int);
1388static int moxaload320b(int, unsigned char __user *, int);
1389static int moxaloadcode(int, unsigned char __user *, int);
1390static int moxaloadc218(int, void __iomem *, int);
1391static int moxaloadc320(int, void __iomem *, int, int *);
1392
1393/*****************************************************************************
1394 * Driver level functions: *
1395 * 1. MoxaDriverInit(void); *
1396 * 2. MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port); *
1397 * 3. MoxaDriverPoll(void); *
1398 *****************************************************************************/
1399void MoxaDriverInit(void)
1400{
8f8ecbad
JS
1401 struct moxa_port *p;
1402 unsigned int i;
1da177e4
LT
1403
1404 moxaFuncTout = HZ / 2; /* 500 mini-seconds */
1405 moxaCard = 0;
1406 moxaLog.tick = 0;
1407 moxaLowWaterChk = 0;
1408 for (i = 0; i < MAX_PORTS; i++) {
8f8ecbad
JS
1409 p = &moxa_ports[i];
1410 p->chkPort = 0;
1411 p->lowChkFlag = 0;
1412 p->lineCtrl = 0;
1da177e4
LT
1413 moxaLog.rxcnt[i] = 0;
1414 moxaLog.txcnt[i] = 0;
1415 }
1416}
1417
1418#define MOXA 0x400
1419#define MOXA_GET_IQUEUE (MOXA + 1) /* get input buffered count */
1420#define MOXA_GET_OQUEUE (MOXA + 2) /* get output buffered count */
1421#define MOXA_INIT_DRIVER (MOXA + 6) /* moxaCard=0 */
1422#define MOXA_LOAD_BIOS (MOXA + 9) /* download BIOS */
1423#define MOXA_FIND_BOARD (MOXA + 10) /* Check if MOXA card exist? */
1424#define MOXA_LOAD_C320B (MOXA + 11) /* download 320B firmware */
1425#define MOXA_LOAD_CODE (MOXA + 12) /* download firmware */
1426#define MOXA_GETDATACOUNT (MOXA + 23)
1427#define MOXA_GET_IOQUEUE (MOXA + 27)
1428#define MOXA_FLUSH_QUEUE (MOXA + 28)
1429#define MOXA_GET_CONF (MOXA + 35) /* configuration */
1430#define MOXA_GET_MAJOR (MOXA + 63)
1431#define MOXA_GET_CUMAJOR (MOXA + 64)
1432#define MOXA_GETMSTATUS (MOXA + 65)
1433
1da177e4
LT
1434struct dl_str {
1435 char __user *buf;
1436 int len;
1437 int cardno;
1438};
1439
1da177e4
LT
1440static struct dl_str dltmp;
1441
1442void MoxaPortFlushData(int port, int mode)
1443{
1444 void __iomem *ofsAddr;
1445 if ((mode < 0) || (mode > 2))
1446 return;
8f8ecbad 1447 ofsAddr = moxa_ports[port].tableAddr;
1da177e4
LT
1448 moxafunc(ofsAddr, FC_FlushQueue, mode);
1449 if (mode != 1) {
8f8ecbad 1450 moxa_ports[port].lowChkFlag = 0;
1da177e4
LT
1451 low_water_check(ofsAddr);
1452 }
1453}
1454
1455int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port)
1456{
1457 int i;
1458 int status;
1459 int MoxaPortTxQueue(int), MoxaPortRxQueue(int);
1460 void __user *argp = (void __user *)arg;
1461
11324edd 1462 if (port == MAX_PORTS) {
1da177e4
LT
1463 if ((cmd != MOXA_GET_CONF) && (cmd != MOXA_INIT_DRIVER) &&
1464 (cmd != MOXA_LOAD_BIOS) && (cmd != MOXA_FIND_BOARD) && (cmd != MOXA_LOAD_C320B) &&
1465 (cmd != MOXA_LOAD_CODE) && (cmd != MOXA_GETDATACOUNT) &&
1466 (cmd != MOXA_GET_IOQUEUE) && (cmd != MOXA_GET_MAJOR) &&
1467 (cmd != MOXA_GET_CUMAJOR) && (cmd != MOXA_GETMSTATUS))
1468 return (-EINVAL);
1469 }
1470 switch (cmd) {
1471 case MOXA_GET_CONF:
9dff89cd
JS
1472 if(copy_to_user(argp, &moxa_boards, MAX_BOARDS *
1473 sizeof(struct moxa_board_conf)))
1da177e4
LT
1474 return -EFAULT;
1475 return (0);
1476 case MOXA_INIT_DRIVER:
1477 if ((int) arg == 0x404)
1478 MoxaDriverInit();
1479 return (0);
1480 case MOXA_GETDATACOUNT:
1481 moxaLog.tick = jiffies;
9dff89cd 1482 if(copy_to_user(argp, &moxaLog, sizeof(struct mon_str)))
1da177e4
LT
1483 return -EFAULT;
1484 return (0);
1485 case MOXA_FLUSH_QUEUE:
1486 MoxaPortFlushData(port, arg);
1487 return (0);
8f8ecbad
JS
1488 case MOXA_GET_IOQUEUE: {
1489 struct moxaq_str __user *argm = argp;
1490 struct moxa_port *p;
1491
1492 for (i = 0; i < MAX_PORTS; i++, argm++) {
1493 p = &moxa_ports[i];
1494 memset(&p->temp_queue, 0, sizeof(p->temp_queue));
1495 if (p->chkPort) {
1496 p->temp_queue.inq = MoxaPortRxQueue(i);
1497 p->temp_queue.outq = MoxaPortTxQueue(i);
1da177e4 1498 }
8f8ecbad
JS
1499 if (copy_to_user(argm, &p->temp_queue,
1500 sizeof(p->temp_queue)))
1501 return -EFAULT;
1da177e4 1502 }
1da177e4 1503 return (0);
8f8ecbad 1504 } case MOXA_GET_OQUEUE:
1da177e4
LT
1505 i = MoxaPortTxQueue(port);
1506 return put_user(i, (unsigned long __user *)argp);
1507 case MOXA_GET_IQUEUE:
1508 i = MoxaPortRxQueue(port);
1509 return put_user(i, (unsigned long __user *)argp);
1510 case MOXA_GET_MAJOR:
1511 if(copy_to_user(argp, &ttymajor, sizeof(int)))
1512 return -EFAULT;
1513 return 0;
1514 case MOXA_GET_CUMAJOR:
1515 i = 0;
1516 if(copy_to_user(argp, &i, sizeof(int)))
1517 return -EFAULT;
1518 return 0;
8f8ecbad
JS
1519 case MOXA_GETMSTATUS: {
1520 struct mxser_mstatus __user *argm = argp;
1521 struct moxa_port *p;
1522
1523 for (i = 0; i < MAX_PORTS; i++, argm++) {
1524 p = &moxa_ports[i];
1525 p->GMStatus.ri = 0;
1526 p->GMStatus.dcd = 0;
1527 p->GMStatus.dsr = 0;
1528 p->GMStatus.cts = 0;
1529 if (!p->chkPort) {
1530 goto copy;
1da177e4 1531 } else {
8f8ecbad 1532 status = MoxaPortLineStatus(p->port);
1da177e4 1533 if (status & 1)
8f8ecbad 1534 p->GMStatus.cts = 1;
1da177e4 1535 if (status & 2)
8f8ecbad 1536 p->GMStatus.dsr = 1;
1da177e4 1537 if (status & 4)
8f8ecbad 1538 p->GMStatus.dcd = 1;
1da177e4
LT
1539 }
1540
8f8ecbad
JS
1541 if (!p->tty || !p->tty->termios)
1542 p->GMStatus.cflag = p->cflag;
1da177e4 1543 else
8f8ecbad
JS
1544 p->GMStatus.cflag = p->tty->termios->c_cflag;
1545copy:
1546 if (copy_to_user(argm, &p->GMStatus,
1547 sizeof(p->GMStatus)))
1548 return -EFAULT;
1da177e4 1549 }
1da177e4 1550 return 0;
8f8ecbad 1551 } default:
1da177e4
LT
1552 return (-ENOIOCTLCMD);
1553 case MOXA_LOAD_BIOS:
1554 case MOXA_FIND_BOARD:
1555 case MOXA_LOAD_C320B:
1556 case MOXA_LOAD_CODE:
49cd619c
AC
1557 if (!capable(CAP_SYS_RAWIO))
1558 return -EPERM;
1da177e4
LT
1559 break;
1560 }
1561
1562 if(copy_from_user(&dltmp, argp, sizeof(struct dl_str)))
1563 return -EFAULT;
1564 if(dltmp.cardno < 0 || dltmp.cardno >= MAX_BOARDS)
1565 return -EINVAL;
1566
1567 switch(cmd)
1568 {
1569 case MOXA_LOAD_BIOS:
1570 i = moxaloadbios(dltmp.cardno, dltmp.buf, dltmp.len);
1571 return (i);
1572 case MOXA_FIND_BOARD:
1573 return moxafindcard(dltmp.cardno);
1574 case MOXA_LOAD_C320B:
1575 moxaload320b(dltmp.cardno, dltmp.buf, dltmp.len);
1576 default: /* to keep gcc happy */
1577 return (0);
1578 case MOXA_LOAD_CODE:
1579 i = moxaloadcode(dltmp.cardno, dltmp.buf, dltmp.len);
1580 if (i == -1)
1581 return (-EFAULT);
1582 return (i);
1583
1584 }
1585}
1586
1587int MoxaDriverPoll(void)
1588{
8f8ecbad 1589 struct moxa_board_conf *brd;
1da177e4
LT
1590 register ushort temp;
1591 register int card;
1592 void __iomem *ofsAddr;
1593 void __iomem *ip;
1594 int port, p, ports;
1595
1596 if (moxaCard == 0)
1597 return (-1);
1598 for (card = 0; card < MAX_BOARDS; card++) {
8f8ecbad
JS
1599 brd = &moxa_boards[card];
1600 if (brd->loadstat == 0)
01cfaf0d 1601 continue;
8f8ecbad 1602 if ((ports = brd->numPorts) == 0)
1da177e4 1603 continue;
8f8ecbad
JS
1604 if (readb(brd->intPend) == 0xff) {
1605 ip = brd->intTable + readb(brd->intNdx);
1da177e4
LT
1606 p = card * MAX_PORTS_PER_BOARD;
1607 ports <<= 1;
1608 for (port = 0; port < ports; port += 2, p++) {
1609 if ((temp = readw(ip + port)) != 0) {
1610 writew(0, ip + port);
8f8ecbad 1611 ofsAddr = moxa_ports[p].tableAddr;
1da177e4
LT
1612 if (temp & IntrTx)
1613 writew(readw(ofsAddr + HostStat) & ~WakeupTx, ofsAddr + HostStat);
1614 if (temp & IntrBreak) {
8f8ecbad 1615 moxa_ports[p].breakCnt++;
1da177e4
LT
1616 }
1617 if (temp & IntrLine) {
1618 if (readb(ofsAddr + FlagStat) & DCD_state) {
8f8ecbad
JS
1619 if ((moxa_ports[p].DCDState & DCD_oldstate) == 0)
1620 moxa_ports[p].DCDState = (DCD_oldstate |
1da177e4
LT
1621 DCD_changed);
1622 } else {
8f8ecbad
JS
1623 if (moxa_ports[p].DCDState & DCD_oldstate)
1624 moxa_ports[p].DCDState = DCD_changed;
1da177e4
LT
1625 }
1626 }
1627 }
1628 }
8f8ecbad 1629 writeb(0, brd->intPend);
1da177e4
LT
1630 }
1631 if (moxaLowWaterChk) {
1632 p = card * MAX_PORTS_PER_BOARD;
1633 for (port = 0; port < ports; port++, p++) {
8f8ecbad
JS
1634 if (moxa_ports[p].lowChkFlag) {
1635 moxa_ports[p].lowChkFlag = 0;
1636 ofsAddr = moxa_ports[p].tableAddr;
1da177e4
LT
1637 low_water_check(ofsAddr);
1638 }
1639 }
1640 }
1641 }
1642 moxaLowWaterChk = 0;
1643 return (0);
1644}
1645
1646/*****************************************************************************
1647 * Card level function: *
1648 * 1. MoxaPortsOfCard(int cardno); *
1649 *****************************************************************************/
1650int MoxaPortsOfCard(int cardno)
1651{
1652
1653 if (moxa_boards[cardno].boardType == 0)
1654 return (0);
1655 return (moxa_boards[cardno].numPorts);
1656}
1657
1658/*****************************************************************************
1659 * Port level functions: *
1660 * 1. MoxaPortIsValid(int port); *
1661 * 2. MoxaPortEnable(int port); *
1662 * 3. MoxaPortDisable(int port); *
1663 * 4. MoxaPortGetMaxBaud(int port); *
1da177e4 1664 * 6. MoxaPortSetBaud(int port, long baud); *
1da177e4
LT
1665 * 8. MoxaPortSetTermio(int port, unsigned char *termio); *
1666 * 9. MoxaPortGetLineOut(int port, int *dtrState, int *rtsState); *
1667 * 10. MoxaPortLineCtrl(int port, int dtrState, int rtsState); *
1668 * 11. MoxaPortFlowCtrl(int port, int rts, int cts, int rx, int tx,int xany); *
1669 * 12. MoxaPortLineStatus(int port); *
1670 * 13. MoxaPortDCDChange(int port); *
1671 * 14. MoxaPortDCDON(int port); *
1672 * 15. MoxaPortFlushData(int port, int mode); *
1673 * 16. MoxaPortWriteData(int port, unsigned char * buffer, int length); *
33f0f88f 1674 * 17. MoxaPortReadData(int port, struct tty_struct *tty); *
1da177e4
LT
1675 * 20. MoxaPortTxQueue(int port); *
1676 * 21. MoxaPortTxFree(int port); *
1677 * 22. MoxaPortRxQueue(int port); *
1da177e4
LT
1678 * 24. MoxaPortTxDisable(int port); *
1679 * 25. MoxaPortTxEnable(int port); *
1da177e4 1680 * 27. MoxaPortResetBrkCnt(int port); *
1da177e4
LT
1681 * 30. MoxaPortSendBreak(int port, int ticks); *
1682 *****************************************************************************/
1683/*
1684 * Moxa Port Number Description:
1685 *
1686 * MOXA serial driver supports up to 4 MOXA-C218/C320 boards. And,
1687 * the port number using in MOXA driver functions will be 0 to 31 for
1688 * first MOXA board, 32 to 63 for second, 64 to 95 for third and 96
1689 * to 127 for fourth. For example, if you setup three MOXA boards,
1690 * first board is C218, second board is C320-16 and third board is
1691 * C320-32. The port number of first board (C218 - 8 ports) is from
1692 * 0 to 7. The port number of second board (C320 - 16 ports) is form
1693 * 32 to 47. The port number of third board (C320 - 32 ports) is from
1694 * 64 to 95. And those port numbers form 8 to 31, 48 to 63 and 96 to
1695 * 127 will be invalid.
1696 *
1697 *
1698 * Moxa Functions Description:
1699 *
1700 * Function 1: Driver initialization routine, this routine must be
1701 * called when initialized driver.
1702 * Syntax:
1703 * void MoxaDriverInit();
1704 *
1705 *
1706 * Function 2: Moxa driver private IOCTL command processing.
1707 * Syntax:
1708 * int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port);
1709 *
1710 * unsigned int cmd : IOCTL command
1711 * unsigned long arg : IOCTL argument
1712 * int port : port number (0 - 127)
1713 *
1714 * return: 0 (OK)
1715 * -EINVAL
1716 * -ENOIOCTLCMD
1717 *
1718 *
1719 * Function 3: Moxa driver polling process routine.
1720 * Syntax:
1721 * int MoxaDriverPoll(void);
1722 *
1723 * return: 0 ; polling O.K.
1724 * -1 : no any Moxa card.
1725 *
1726 *
1727 * Function 4: Get the ports of this card.
1728 * Syntax:
1729 * int MoxaPortsOfCard(int cardno);
1730 *
1731 * int cardno : card number (0 - 3)
1732 *
1733 * return: 0 : this card is invalid
1734 * 8/16/24/32
1735 *
1736 *
1737 * Function 5: Check this port is valid or invalid
1738 * Syntax:
1739 * int MoxaPortIsValid(int port);
1740 * int port : port number (0 - 127, ref port description)
1741 *
1742 * return: 0 : this port is invalid
1743 * 1 : this port is valid
1744 *
1745 *
1746 * Function 6: Enable this port to start Tx/Rx data.
1747 * Syntax:
1748 * void MoxaPortEnable(int port);
1749 * int port : port number (0 - 127)
1750 *
1751 *
1752 * Function 7: Disable this port
1753 * Syntax:
1754 * void MoxaPortDisable(int port);
1755 * int port : port number (0 - 127)
1756 *
1757 *
1758 * Function 8: Get the maximun available baud rate of this port.
1759 * Syntax:
1760 * long MoxaPortGetMaxBaud(int port);
1761 * int port : port number (0 - 127)
1762 *
1763 * return: 0 : this port is invalid
1764 * 38400/57600/115200 bps
1765 *
1766 *
1da177e4
LT
1767 * Function 10: Setting baud rate of this port.
1768 * Syntax:
1769 * long MoxaPortSetBaud(int port, long baud);
1770 * int port : port number (0 - 127)
1771 * long baud : baud rate (50 - 115200)
1772 *
1773 * return: 0 : this port is invalid or baud < 50
1774 * 50 - 115200 : the real baud rate set to the port, if
1775 * the argument baud is large than maximun
1776 * available baud rate, the real setting
1777 * baud rate will be the maximun baud rate.
1778 *
1779 *
1da177e4
LT
1780 * Function 12: Configure the port.
1781 * Syntax:
606d099c 1782 * int MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud);
1da177e4 1783 * int port : port number (0 - 127)
606d099c 1784 * struct ktermios * termio : termio structure pointer
c7bce309 1785 * speed_t baud : baud rate
1da177e4
LT
1786 *
1787 * return: -1 : this port is invalid or termio == NULL
1788 * 0 : setting O.K.
1789 *
1790 *
1791 * Function 13: Get the DTR/RTS state of this port.
1792 * Syntax:
1793 * int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState);
1794 * int port : port number (0 - 127)
1795 * int * dtrState : pointer to INT to receive the current DTR
1796 * state. (if NULL, this function will not
1797 * write to this address)
1798 * int * rtsState : pointer to INT to receive the current RTS
1799 * state. (if NULL, this function will not
1800 * write to this address)
1801 *
1802 * return: -1 : this port is invalid
1803 * 0 : O.K.
1804 *
1805 *
1806 * Function 14: Setting the DTR/RTS output state of this port.
1807 * Syntax:
1808 * void MoxaPortLineCtrl(int port, int dtrState, int rtsState);
1809 * int port : port number (0 - 127)
1810 * int dtrState : DTR output state (0: off, 1: on)
1811 * int rtsState : RTS output state (0: off, 1: on)
1812 *
1813 *
1814 * Function 15: Setting the flow control of this port.
1815 * Syntax:
1816 * void MoxaPortFlowCtrl(int port, int rtsFlow, int ctsFlow, int rxFlow,
1817 * int txFlow,int xany);
1818 * int port : port number (0 - 127)
1819 * int rtsFlow : H/W RTS flow control (0: no, 1: yes)
1820 * int ctsFlow : H/W CTS flow control (0: no, 1: yes)
1821 * int rxFlow : S/W Rx XON/XOFF flow control (0: no, 1: yes)
1822 * int txFlow : S/W Tx XON/XOFF flow control (0: no, 1: yes)
1823 * int xany : S/W XANY flow control (0: no, 1: yes)
1824 *
1825 *
1826 * Function 16: Get ths line status of this port
1827 * Syntax:
1828 * int MoxaPortLineStatus(int port);
1829 * int port : port number (0 - 127)
1830 *
1831 * return: Bit 0 - CTS state (0: off, 1: on)
1832 * Bit 1 - DSR state (0: off, 1: on)
1833 * Bit 2 - DCD state (0: off, 1: on)
1834 *
1835 *
1836 * Function 17: Check the DCD state has changed since the last read
1837 * of this function.
1838 * Syntax:
1839 * int MoxaPortDCDChange(int port);
1840 * int port : port number (0 - 127)
1841 *
1842 * return: 0 : no changed
1843 * 1 : DCD has changed
1844 *
1845 *
1846 * Function 18: Check ths current DCD state is ON or not.
1847 * Syntax:
1848 * int MoxaPortDCDON(int port);
1849 * int port : port number (0 - 127)
1850 *
1851 * return: 0 : DCD off
1852 * 1 : DCD on
1853 *
1854 *
1855 * Function 19: Flush the Rx/Tx buffer data of this port.
1856 * Syntax:
1857 * void MoxaPortFlushData(int port, int mode);
1858 * int port : port number (0 - 127)
1859 * int mode
1860 * 0 : flush the Rx buffer
1861 * 1 : flush the Tx buffer
1862 * 2 : flush the Rx and Tx buffer
1863 *
1864 *
1865 * Function 20: Write data.
1866 * Syntax:
1867 * int MoxaPortWriteData(int port, unsigned char * buffer, int length);
1868 * int port : port number (0 - 127)
1869 * unsigned char * buffer : pointer to write data buffer.
1870 * int length : write data length
1871 *
1872 * return: 0 - length : real write data length
1873 *
1874 *
1875 * Function 21: Read data.
1876 * Syntax:
33f0f88f 1877 * int MoxaPortReadData(int port, struct tty_struct *tty);
1da177e4 1878 * int port : port number (0 - 127)
33f0f88f 1879 * struct tty_struct *tty : tty for data
1da177e4
LT
1880 *
1881 * return: 0 - length : real read data length
1882 *
1883 *
1da177e4
LT
1884 * Function 24: Get the Tx buffer current queued data bytes
1885 * Syntax:
1886 * int MoxaPortTxQueue(int port);
1887 * int port : port number (0 - 127)
1888 *
1889 * return: .. : Tx buffer current queued data bytes
1890 *
1891 *
1892 * Function 25: Get the Tx buffer current free space
1893 * Syntax:
1894 * int MoxaPortTxFree(int port);
1895 * int port : port number (0 - 127)
1896 *
1897 * return: .. : Tx buffer current free space
1898 *
1899 *
1900 * Function 26: Get the Rx buffer current queued data bytes
1901 * Syntax:
1902 * int MoxaPortRxQueue(int port);
1903 * int port : port number (0 - 127)
1904 *
1905 * return: .. : Rx buffer current queued data bytes
1906 *
1907 *
1da177e4
LT
1908 * Function 28: Disable port data transmission.
1909 * Syntax:
1910 * void MoxaPortTxDisable(int port);
1911 * int port : port number (0 - 127)
1912 *
1913 *
1914 * Function 29: Enable port data transmission.
1915 * Syntax:
1916 * void MoxaPortTxEnable(int port);
1917 * int port : port number (0 - 127)
1918 *
1919 *
1da177e4
LT
1920 * Function 31: Get the received BREAK signal count and reset it.
1921 * Syntax:
1922 * int MoxaPortResetBrkCnt(int port);
1923 * int port : port number (0 - 127)
1924 *
1925 * return: 0 - .. : BREAK signal count
1926 *
1927 *
1da177e4
LT
1928 * Function 34: Send out a BREAK signal.
1929 * Syntax:
1930 * void MoxaPortSendBreak(int port, int ms100);
1931 * int port : port number (0 - 127)
1932 * int ms100 : break signal time interval.
1933 * unit: 100 mini-second. if ms100 == 0, it will
1934 * send out a about 250 ms BREAK signal.
1935 *
1936 */
1937int MoxaPortIsValid(int port)
1938{
1939
1940 if (moxaCard == 0)
1941 return (0);
8f8ecbad 1942 if (moxa_ports[port].chkPort == 0)
1da177e4
LT
1943 return (0);
1944 return (1);
1945}
1946
1947void MoxaPortEnable(int port)
1948{
1949 void __iomem *ofsAddr;
1950 int MoxaPortLineStatus(int);
1951 short lowwater = 512;
1952
8f8ecbad 1953 ofsAddr = moxa_ports[port].tableAddr;
1da177e4 1954 writew(lowwater, ofsAddr + Low_water);
8f8ecbad 1955 moxa_ports[port].breakCnt = 0;
1da177e4
LT
1956 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
1957 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
1958 moxafunc(ofsAddr, FC_SetBreakIrq, 0);
1959 } else {
1960 writew(readw(ofsAddr + HostStat) | WakeupBreak, ofsAddr + HostStat);
1961 }
1962
1963 moxafunc(ofsAddr, FC_SetLineIrq, Magic_code);
1964 moxafunc(ofsAddr, FC_FlushQueue, 2);
1965
1966 moxafunc(ofsAddr, FC_EnableCH, Magic_code);
1967 MoxaPortLineStatus(port);
1968}
1969
1970void MoxaPortDisable(int port)
1971{
8f8ecbad 1972 void __iomem *ofsAddr = moxa_ports[port].tableAddr;
1da177e4
LT
1973
1974 moxafunc(ofsAddr, FC_SetFlowCtl, 0); /* disable flow control */
1975 moxafunc(ofsAddr, FC_ClrLineIrq, Magic_code);
1976 writew(0, ofsAddr + HostStat);
1977 moxafunc(ofsAddr, FC_DisableCH, Magic_code);
1978}
1979
1980long MoxaPortGetMaxBaud(int port)
1981{
1982 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
1983 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI))
1984 return (460800L);
1985 else
1986 return (921600L);
1987}
1988
1989
1990long MoxaPortSetBaud(int port, long baud)
1991{
1992 void __iomem *ofsAddr;
1993 long max, clock;
1994 unsigned int val;
1995
1996 if ((baud < 50L) || ((max = MoxaPortGetMaxBaud(port)) == 0))
1997 return (0);
8f8ecbad 1998 ofsAddr = moxa_ports[port].tableAddr;
1da177e4
LT
1999 if (baud > max)
2000 baud = max;
2001 if (max == 38400L)
2002 clock = 614400L; /* for 9.8304 Mhz : max. 38400 bps */
2003 else if (max == 57600L)
2004 clock = 691200L; /* for 11.0592 Mhz : max. 57600 bps */
2005 else
2006 clock = 921600L; /* for 14.7456 Mhz : max. 115200 bps */
2007 val = clock / baud;
2008 moxafunc(ofsAddr, FC_SetBaud, val);
2009 baud = clock / val;
8f8ecbad 2010 moxa_ports[port].curBaud = baud;
1da177e4
LT
2011 return (baud);
2012}
2013
606d099c 2014int MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud)
1da177e4
LT
2015{
2016 void __iomem *ofsAddr;
2017 tcflag_t cflag;
1da177e4
LT
2018 tcflag_t mode = 0;
2019
8f8ecbad 2020 if (moxa_ports[port].chkPort == 0 || termio == 0)
1da177e4 2021 return (-1);
8f8ecbad 2022 ofsAddr = moxa_ports[port].tableAddr;
1da177e4
LT
2023 cflag = termio->c_cflag; /* termio->c_cflag */
2024
2025 mode = termio->c_cflag & CSIZE;
2026 if (mode == CS5)
2027 mode = MX_CS5;
2028 else if (mode == CS6)
2029 mode = MX_CS6;
2030 else if (mode == CS7)
2031 mode = MX_CS7;
2032 else if (mode == CS8)
2033 mode = MX_CS8;
2034
2035 if (termio->c_cflag & CSTOPB) {
2036 if (mode == MX_CS5)
2037 mode |= MX_STOP15;
2038 else
2039 mode |= MX_STOP2;
2040 } else
2041 mode |= MX_STOP1;
2042
2043 if (termio->c_cflag & PARENB) {
2044 if (termio->c_cflag & PARODD)
2045 mode |= MX_PARODD;
2046 else
2047 mode |= MX_PAREVEN;
2048 } else
2049 mode |= MX_PARNONE;
2050
2051 moxafunc(ofsAddr, FC_SetDataMode, (ushort) mode);
2052
1da177e4
LT
2053 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
2054 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
c7bce309 2055 if (baud >= 921600L)
1da177e4
LT
2056 return (-1);
2057 }
2058 MoxaPortSetBaud(port, baud);
2059
2060 if (termio->c_iflag & (IXON | IXOFF | IXANY)) {
2061 writeb(termio->c_cc[VSTART], ofsAddr + FuncArg);
2062 writeb(termio->c_cc[VSTOP], ofsAddr + FuncArg1);
2063 writeb(FC_SetXonXoff, ofsAddr + FuncCode);
2064 wait_finish(ofsAddr);
2065
2066 }
2067 return (0);
2068}
2069
2070int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState)
2071{
2072
2073 if (!MoxaPortIsValid(port))
2074 return (-1);
2075 if (dtrState) {
8f8ecbad 2076 if (moxa_ports[port].lineCtrl & DTR_ON)
1da177e4
LT
2077 *dtrState = 1;
2078 else
2079 *dtrState = 0;
2080 }
2081 if (rtsState) {
8f8ecbad 2082 if (moxa_ports[port].lineCtrl & RTS_ON)
1da177e4
LT
2083 *rtsState = 1;
2084 else
2085 *rtsState = 0;
2086 }
2087 return (0);
2088}
2089
2090void MoxaPortLineCtrl(int port, int dtr, int rts)
2091{
2092 void __iomem *ofsAddr;
2093 int mode;
2094
8f8ecbad 2095 ofsAddr = moxa_ports[port].tableAddr;
1da177e4
LT
2096 mode = 0;
2097 if (dtr)
2098 mode |= DTR_ON;
2099 if (rts)
2100 mode |= RTS_ON;
8f8ecbad 2101 moxa_ports[port].lineCtrl = mode;
1da177e4
LT
2102 moxafunc(ofsAddr, FC_LineControl, mode);
2103}
2104
2105void MoxaPortFlowCtrl(int port, int rts, int cts, int txflow, int rxflow, int txany)
2106{
2107 void __iomem *ofsAddr;
2108 int mode;
2109
8f8ecbad 2110 ofsAddr = moxa_ports[port].tableAddr;
1da177e4
LT
2111 mode = 0;
2112 if (rts)
2113 mode |= RTS_FlowCtl;
2114 if (cts)
2115 mode |= CTS_FlowCtl;
2116 if (txflow)
2117 mode |= Tx_FlowCtl;
2118 if (rxflow)
2119 mode |= Rx_FlowCtl;
2120 if (txany)
2121 mode |= IXM_IXANY;
2122 moxafunc(ofsAddr, FC_SetFlowCtl, mode);
2123}
2124
2125int MoxaPortLineStatus(int port)
2126{
2127 void __iomem *ofsAddr;
2128 int val;
2129
8f8ecbad 2130 ofsAddr = moxa_ports[port].tableAddr;
1da177e4
LT
2131 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
2132 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
2133 moxafunc(ofsAddr, FC_LineStatus, 0);
2134 val = readw(ofsAddr + FuncArg);
2135 } else {
2136 val = readw(ofsAddr + FlagStat) >> 4;
2137 }
2138 val &= 0x0B;
2139 if (val & 8) {
2140 val |= 4;
8f8ecbad
JS
2141 if ((moxa_ports[port].DCDState & DCD_oldstate) == 0)
2142 moxa_ports[port].DCDState = (DCD_oldstate | DCD_changed);
1da177e4 2143 } else {
8f8ecbad
JS
2144 if (moxa_ports[port].DCDState & DCD_oldstate)
2145 moxa_ports[port].DCDState = DCD_changed;
1da177e4
LT
2146 }
2147 val &= 7;
2148 return (val);
2149}
2150
2151int MoxaPortDCDChange(int port)
2152{
2153 int n;
2154
8f8ecbad 2155 if (moxa_ports[port].chkPort == 0)
1da177e4 2156 return (0);
8f8ecbad
JS
2157 n = moxa_ports[port].DCDState;
2158 moxa_ports[port].DCDState &= ~DCD_changed;
1da177e4
LT
2159 n &= DCD_changed;
2160 return (n);
2161}
2162
2163int MoxaPortDCDON(int port)
2164{
2165 int n;
2166
8f8ecbad 2167 if (moxa_ports[port].chkPort == 0)
1da177e4 2168 return (0);
8f8ecbad 2169 if (moxa_ports[port].DCDState & DCD_oldstate)
1da177e4
LT
2170 n = 1;
2171 else
2172 n = 0;
2173 return (n);
2174}
2175
1da177e4
LT
2176int MoxaPortWriteData(int port, unsigned char * buffer, int len)
2177{
2178 int c, total, i;
2179 ushort tail;
2180 int cnt;
2181 ushort head, tx_mask, spage, epage;
2182 ushort pageno, pageofs, bufhead;
2183 void __iomem *baseAddr, *ofsAddr, *ofs;
2184
8f8ecbad
JS
2185 ofsAddr = moxa_ports[port].tableAddr;
2186 baseAddr = moxa_boards[port / MAX_PORTS_PER_BOARD].basemem;
1da177e4
LT
2187 tx_mask = readw(ofsAddr + TX_mask);
2188 spage = readw(ofsAddr + Page_txb);
2189 epage = readw(ofsAddr + EndPage_txb);
2190 tail = readw(ofsAddr + TXwptr);
2191 head = readw(ofsAddr + TXrptr);
2192 c = (head > tail) ? (head - tail - 1)
2193 : (head - tail + tx_mask);
2194 if (c > len)
2195 c = len;
2196 moxaLog.txcnt[port] += c;
2197 total = c;
2198 if (spage == epage) {
2199 bufhead = readw(ofsAddr + Ofs_txb);
2200 writew(spage, baseAddr + Control_reg);
2201 while (c > 0) {
2202 if (head > tail)
2203 len = head - tail - 1;
2204 else
2205 len = tx_mask + 1 - tail;
2206 len = (c > len) ? len : c;
2207 ofs = baseAddr + DynPage_addr + bufhead + tail;
2208 for (i = 0; i < len; i++)
2209 writeb(*buffer++, ofs + i);
2210 tail = (tail + len) & tx_mask;
2211 c -= len;
2212 }
2213 writew(tail, ofsAddr + TXwptr);
2214 } else {
2215 len = c;
2216 pageno = spage + (tail >> 13);
2217 pageofs = tail & Page_mask;
2218 do {
2219 cnt = Page_size - pageofs;
2220 if (cnt > c)
2221 cnt = c;
2222 c -= cnt;
2223 writeb(pageno, baseAddr + Control_reg);
2224 ofs = baseAddr + DynPage_addr + pageofs;
2225 for (i = 0; i < cnt; i++)
2226 writeb(*buffer++, ofs + i);
2227 if (c == 0) {
2228 writew((tail + len) & tx_mask, ofsAddr + TXwptr);
2229 break;
2230 }
2231 if (++pageno == epage)
2232 pageno = spage;
2233 pageofs = 0;
2234 } while (1);
2235 }
2236 writeb(1, ofsAddr + CD180TXirq); /* start to send */
2237 return (total);
2238}
2239
33f0f88f 2240int MoxaPortReadData(int port, struct tty_struct *tty)
1da177e4
LT
2241{
2242 register ushort head, pageofs;
2243 int i, count, cnt, len, total, remain;
2244 ushort tail, rx_mask, spage, epage;
2245 ushort pageno, bufhead;
2246 void __iomem *baseAddr, *ofsAddr, *ofs;
2247
8f8ecbad
JS
2248 ofsAddr = moxa_ports[port].tableAddr;
2249 baseAddr = moxa_boards[port / MAX_PORTS_PER_BOARD].basemem;
1da177e4
LT
2250 head = readw(ofsAddr + RXrptr);
2251 tail = readw(ofsAddr + RXwptr);
2252 rx_mask = readw(ofsAddr + RX_mask);
2253 spage = readw(ofsAddr + Page_rxb);
2254 epage = readw(ofsAddr + EndPage_rxb);
2255 count = (tail >= head) ? (tail - head)
2256 : (tail - head + rx_mask + 1);
2257 if (count == 0)
33f0f88f 2258 return 0;
1da177e4 2259
33f0f88f 2260 total = count;
1da177e4
LT
2261 remain = count - total;
2262 moxaLog.rxcnt[port] += total;
2263 count = total;
2264 if (spage == epage) {
2265 bufhead = readw(ofsAddr + Ofs_rxb);
2266 writew(spage, baseAddr + Control_reg);
2267 while (count > 0) {
2268 if (tail >= head)
2269 len = tail - head;
2270 else
2271 len = rx_mask + 1 - head;
2272 len = (count > len) ? len : count;
2273 ofs = baseAddr + DynPage_addr + bufhead + head;
2274 for (i = 0; i < len; i++)
33f0f88f 2275 tty_insert_flip_char(tty, readb(ofs + i), TTY_NORMAL);
1da177e4
LT
2276 head = (head + len) & rx_mask;
2277 count -= len;
2278 }
2279 writew(head, ofsAddr + RXrptr);
2280 } else {
2281 len = count;
2282 pageno = spage + (head >> 13);
2283 pageofs = head & Page_mask;
2284 do {
2285 cnt = Page_size - pageofs;
2286 if (cnt > count)
2287 cnt = count;
2288 count -= cnt;
2289 writew(pageno, baseAddr + Control_reg);
2290 ofs = baseAddr + DynPage_addr + pageofs;
2291 for (i = 0; i < cnt; i++)
33f0f88f 2292 tty_insert_flip_char(tty, readb(ofs + i), TTY_NORMAL);
1da177e4
LT
2293 if (count == 0) {
2294 writew((head + len) & rx_mask, ofsAddr + RXrptr);
2295 break;
2296 }
2297 if (++pageno == epage)
2298 pageno = spage;
2299 pageofs = 0;
2300 } while (1);
2301 }
2302 if ((readb(ofsAddr + FlagStat) & Xoff_state) && (remain < LowWater)) {
2303 moxaLowWaterChk = 1;
8f8ecbad 2304 moxa_ports[port].lowChkFlag = 1;
1da177e4
LT
2305 }
2306 return (total);
2307}
2308
2309
2310int MoxaPortTxQueue(int port)
2311{
2312 void __iomem *ofsAddr;
2313 ushort rptr, wptr, mask;
2314 int len;
2315
8f8ecbad 2316 ofsAddr = moxa_ports[port].tableAddr;
1da177e4
LT
2317 rptr = readw(ofsAddr + TXrptr);
2318 wptr = readw(ofsAddr + TXwptr);
2319 mask = readw(ofsAddr + TX_mask);
2320 len = (wptr - rptr) & mask;
2321 return (len);
2322}
2323
2324int MoxaPortTxFree(int port)
2325{
2326 void __iomem *ofsAddr;
2327 ushort rptr, wptr, mask;
2328 int len;
2329
8f8ecbad 2330 ofsAddr = moxa_ports[port].tableAddr;
1da177e4
LT
2331 rptr = readw(ofsAddr + TXrptr);
2332 wptr = readw(ofsAddr + TXwptr);
2333 mask = readw(ofsAddr + TX_mask);
2334 len = mask - ((wptr - rptr) & mask);
2335 return (len);
2336}
2337
2338int MoxaPortRxQueue(int port)
2339{
2340 void __iomem *ofsAddr;
2341 ushort rptr, wptr, mask;
2342 int len;
2343
8f8ecbad 2344 ofsAddr = moxa_ports[port].tableAddr;
1da177e4
LT
2345 rptr = readw(ofsAddr + RXrptr);
2346 wptr = readw(ofsAddr + RXwptr);
2347 mask = readw(ofsAddr + RX_mask);
2348 len = (wptr - rptr) & mask;
2349 return (len);
2350}
2351
2352
2353void MoxaPortTxDisable(int port)
2354{
2355 void __iomem *ofsAddr;
2356
8f8ecbad 2357 ofsAddr = moxa_ports[port].tableAddr;
1da177e4
LT
2358 moxafunc(ofsAddr, FC_SetXoffState, Magic_code);
2359}
2360
2361void MoxaPortTxEnable(int port)
2362{
2363 void __iomem *ofsAddr;
2364
8f8ecbad 2365 ofsAddr = moxa_ports[port].tableAddr;
1da177e4
LT
2366 moxafunc(ofsAddr, FC_SetXonState, Magic_code);
2367}
2368
2369
2370int MoxaPortResetBrkCnt(int port)
2371{
2372 ushort cnt;
8f8ecbad
JS
2373 cnt = moxa_ports[port].breakCnt;
2374 moxa_ports[port].breakCnt = 0;
1da177e4
LT
2375 return (cnt);
2376}
2377
2378
2379void MoxaPortSendBreak(int port, int ms100)
2380{
2381 void __iomem *ofsAddr;
2382
8f8ecbad 2383 ofsAddr = moxa_ports[port].tableAddr;
1da177e4
LT
2384 if (ms100) {
2385 moxafunc(ofsAddr, FC_SendBreak, Magic_code);
2386 moxadelay(ms100 * (HZ / 10));
2387 } else {
2388 moxafunc(ofsAddr, FC_SendBreak, Magic_code);
2389 moxadelay(HZ / 4); /* 250 ms */
2390 }
2391 moxafunc(ofsAddr, FC_StopBreak, Magic_code);
2392}
2393
8f8ecbad 2394static int moxa_get_serial_info(struct moxa_port *info,
1da177e4
LT
2395 struct serial_struct __user *retinfo)
2396{
2397 struct serial_struct tmp;
2398
2399 memset(&tmp, 0, sizeof(tmp));
2400 tmp.type = info->type;
2401 tmp.line = info->port;
2402 tmp.port = 0;
2403 tmp.irq = 0;
2404 tmp.flags = info->asyncflags;
2405 tmp.baud_base = 921600;
2406 tmp.close_delay = info->close_delay;
2407 tmp.closing_wait = info->closing_wait;
2408 tmp.custom_divisor = 0;
2409 tmp.hub6 = 0;
2410 if(copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2411 return -EFAULT;
2412 return (0);
2413}
2414
2415
8f8ecbad 2416static int moxa_set_serial_info(struct moxa_port *info,
1da177e4
LT
2417 struct serial_struct __user *new_info)
2418{
2419 struct serial_struct new_serial;
2420
2421 if(copy_from_user(&new_serial, new_info, sizeof(new_serial)))
2422 return -EFAULT;
2423
2424 if ((new_serial.irq != 0) ||
2425 (new_serial.port != 0) ||
2426// (new_serial.type != info->type) ||
2427 (new_serial.custom_divisor != 0) ||
2428 (new_serial.baud_base != 921600))
2429 return (-EPERM);
2430
2431 if (!capable(CAP_SYS_ADMIN)) {
2432 if (((new_serial.flags & ~ASYNC_USR_MASK) !=
2433 (info->asyncflags & ~ASYNC_USR_MASK)))
2434 return (-EPERM);
2435 } else {
2436 info->close_delay = new_serial.close_delay * HZ / 100;
2437 info->closing_wait = new_serial.closing_wait * HZ / 100;
2438 }
2439
2440 new_serial.flags = (new_serial.flags & ~ASYNC_FLAGS);
2441 new_serial.flags |= (info->asyncflags & ASYNC_FLAGS);
2442
2443 if (new_serial.type == PORT_16550A) {
2444 MoxaSetFifo(info->port, 1);
2445 } else {
2446 MoxaSetFifo(info->port, 0);
2447 }
2448
2449 info->type = new_serial.type;
2450 return (0);
2451}
2452
2453
2454
2455/*****************************************************************************
2456 * Static local functions: *
2457 *****************************************************************************/
2458/*
2459 * moxadelay - delays a specified number ticks
2460 */
2461static void moxadelay(int tick)
2462{
2463 unsigned long st, et;
2464
2465 st = jiffies;
2466 et = st + tick;
2467 while (time_before(jiffies, et));
2468}
2469
2470static void moxafunc(void __iomem *ofsAddr, int cmd, ushort arg)
2471{
2472
2473 writew(arg, ofsAddr + FuncArg);
2474 writew(cmd, ofsAddr + FuncCode);
2475 wait_finish(ofsAddr);
2476}
2477
2478static void wait_finish(void __iomem *ofsAddr)
2479{
2480 unsigned long i, j;
2481
2482 i = jiffies;
2483 while (readw(ofsAddr + FuncCode) != 0) {
2484 j = jiffies;
2485 if ((j - i) > moxaFuncTout) {
2486 return;
2487 }
2488 }
2489}
2490
2491static void low_water_check(void __iomem *ofsAddr)
2492{
2493 int len;
2494 ushort rptr, wptr, mask;
2495
2496 if (readb(ofsAddr + FlagStat) & Xoff_state) {
2497 rptr = readw(ofsAddr + RXrptr);
2498 wptr = readw(ofsAddr + RXwptr);
2499 mask = readw(ofsAddr + RX_mask);
2500 len = (wptr - rptr) & mask;
2501 if (len <= Low_water)
2502 moxafunc(ofsAddr, FC_SendXon, 0);
2503 }
2504}
2505
2506static int moxaloadbios(int cardno, unsigned char __user *tmp, int len)
2507{
2508 void __iomem *baseAddr;
2509 int i;
2510
2511 if(copy_from_user(moxaBuff, tmp, len))
2512 return -EFAULT;
8f8ecbad 2513 baseAddr = moxa_boards[cardno].basemem;
1da177e4
LT
2514 writeb(HW_reset, baseAddr + Control_reg); /* reset */
2515 moxadelay(1); /* delay 10 ms */
2516 for (i = 0; i < 4096; i++)
2517 writeb(0, baseAddr + i); /* clear fix page */
2518 for (i = 0; i < len; i++)
2519 writeb(moxaBuff[i], baseAddr + i); /* download BIOS */
2520 writeb(0, baseAddr + Control_reg); /* restart */
2521 return (0);
2522}
2523
2524static int moxafindcard(int cardno)
2525{
2526 void __iomem *baseAddr;
2527 ushort tmp;
2528
8f8ecbad 2529 baseAddr = moxa_boards[cardno].basemem;
1da177e4
LT
2530 switch (moxa_boards[cardno].boardType) {
2531 case MOXA_BOARD_C218_ISA:
2532 case MOXA_BOARD_C218_PCI:
2533 if ((tmp = readw(baseAddr + C218_key)) != C218_KeyCode) {
2534 return (-1);
2535 }
2536 break;
2537 case MOXA_BOARD_CP204J:
2538 if ((tmp = readw(baseAddr + C218_key)) != CP204J_KeyCode) {
2539 return (-1);
2540 }
2541 break;
2542 default:
2543 if ((tmp = readw(baseAddr + C320_key)) != C320_KeyCode) {
2544 return (-1);
2545 }
2546 if ((tmp = readw(baseAddr + C320_status)) != STS_init) {
2547 return (-2);
2548 }
2549 }
2550 return (0);
2551}
2552
2553static int moxaload320b(int cardno, unsigned char __user *tmp, int len)
2554{
2555 void __iomem *baseAddr;
2556 int i;
2557
2558 if(len > sizeof(moxaBuff))
2559 return -EINVAL;
2560 if(copy_from_user(moxaBuff, tmp, len))
2561 return -EFAULT;
8f8ecbad 2562 baseAddr = moxa_boards[cardno].basemem;
1da177e4
LT
2563 writew(len - 7168 - 2, baseAddr + C320bapi_len);
2564 writeb(1, baseAddr + Control_reg); /* Select Page 1 */
2565 for (i = 0; i < 7168; i++)
2566 writeb(moxaBuff[i], baseAddr + DynPage_addr + i);
2567 writeb(2, baseAddr + Control_reg); /* Select Page 2 */
2568 for (i = 0; i < (len - 7168); i++)
2569 writeb(moxaBuff[i + 7168], baseAddr + DynPage_addr + i);
2570 return (0);
2571}
2572
2573static int moxaloadcode(int cardno, unsigned char __user *tmp, int len)
2574{
2575 void __iomem *baseAddr, *ofsAddr;
2576 int retval, port, i;
2577
2578 if(copy_from_user(moxaBuff, tmp, len))
2579 return -EFAULT;
8f8ecbad 2580 baseAddr = moxa_boards[cardno].basemem;
1da177e4
LT
2581 switch (moxa_boards[cardno].boardType) {
2582 case MOXA_BOARD_C218_ISA:
2583 case MOXA_BOARD_C218_PCI:
2584 case MOXA_BOARD_CP204J:
2585 retval = moxaloadc218(cardno, baseAddr, len);
2586 if (retval)
2587 return (retval);
2588 port = cardno * MAX_PORTS_PER_BOARD;
2589 for (i = 0; i < moxa_boards[cardno].numPorts; i++, port++) {
8f8ecbad
JS
2590 struct moxa_port *p = &moxa_ports[port];
2591
2592 p->chkPort = 1;
2593 p->curBaud = 9600L;
2594 p->DCDState = 0;
2595 p->tableAddr = baseAddr + Extern_table + Extern_size * i;
2596 ofsAddr = p->tableAddr;
1da177e4
LT
2597 writew(C218rx_mask, ofsAddr + RX_mask);
2598 writew(C218tx_mask, ofsAddr + TX_mask);
2599 writew(C218rx_spage + i * C218buf_pageno, ofsAddr + Page_rxb);
2600 writew(readw(ofsAddr + Page_rxb) + C218rx_pageno, ofsAddr + EndPage_rxb);
2601
2602 writew(C218tx_spage + i * C218buf_pageno, ofsAddr + Page_txb);
2603 writew(readw(ofsAddr + Page_txb) + C218tx_pageno, ofsAddr + EndPage_txb);
2604
2605 }
2606 break;
2607 default:
2608 retval = moxaloadc320(cardno, baseAddr, len,
2609 &moxa_boards[cardno].numPorts);
2610 if (retval)
2611 return (retval);
2612 port = cardno * MAX_PORTS_PER_BOARD;
2613 for (i = 0; i < moxa_boards[cardno].numPorts; i++, port++) {
8f8ecbad
JS
2614 struct moxa_port *p = &moxa_ports[port];
2615
2616 p->chkPort = 1;
2617 p->curBaud = 9600L;
2618 p->DCDState = 0;
2619 p->tableAddr = baseAddr + Extern_table + Extern_size * i;
2620 ofsAddr = p->tableAddr;
1da177e4
LT
2621 if (moxa_boards[cardno].numPorts == 8) {
2622 writew(C320p8rx_mask, ofsAddr + RX_mask);
2623 writew(C320p8tx_mask, ofsAddr + TX_mask);
2624 writew(C320p8rx_spage + i * C320p8buf_pgno, ofsAddr + Page_rxb);
2625 writew(readw(ofsAddr + Page_rxb) + C320p8rx_pgno, ofsAddr + EndPage_rxb);
2626 writew(C320p8tx_spage + i * C320p8buf_pgno, ofsAddr + Page_txb);
2627 writew(readw(ofsAddr + Page_txb) + C320p8tx_pgno, ofsAddr + EndPage_txb);
2628
2629 } else if (moxa_boards[cardno].numPorts == 16) {
2630 writew(C320p16rx_mask, ofsAddr + RX_mask);
2631 writew(C320p16tx_mask, ofsAddr + TX_mask);
2632 writew(C320p16rx_spage + i * C320p16buf_pgno, ofsAddr + Page_rxb);
2633 writew(readw(ofsAddr + Page_rxb) + C320p16rx_pgno, ofsAddr + EndPage_rxb);
2634 writew(C320p16tx_spage + i * C320p16buf_pgno, ofsAddr + Page_txb);
2635 writew(readw(ofsAddr + Page_txb) + C320p16tx_pgno, ofsAddr + EndPage_txb);
2636
2637 } else if (moxa_boards[cardno].numPorts == 24) {
2638 writew(C320p24rx_mask, ofsAddr + RX_mask);
2639 writew(C320p24tx_mask, ofsAddr + TX_mask);
2640 writew(C320p24rx_spage + i * C320p24buf_pgno, ofsAddr + Page_rxb);
2641 writew(readw(ofsAddr + Page_rxb) + C320p24rx_pgno, ofsAddr + EndPage_rxb);
2642 writew(C320p24tx_spage + i * C320p24buf_pgno, ofsAddr + Page_txb);
2643 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
2644 } else if (moxa_boards[cardno].numPorts == 32) {
2645 writew(C320p32rx_mask, ofsAddr + RX_mask);
2646 writew(C320p32tx_mask, ofsAddr + TX_mask);
2647 writew(C320p32tx_ofs, ofsAddr + Ofs_txb);
2648 writew(C320p32rx_spage + i * C320p32buf_pgno, ofsAddr + Page_rxb);
2649 writew(readb(ofsAddr + Page_rxb), ofsAddr + EndPage_rxb);
2650 writew(C320p32tx_spage + i * C320p32buf_pgno, ofsAddr + Page_txb);
2651 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
2652 }
2653 }
2654 break;
2655 }
8f8ecbad 2656 moxa_boards[cardno].loadstat = 1;
1da177e4
LT
2657 return (0);
2658}
2659
2660static int moxaloadc218(int cardno, void __iomem *baseAddr, int len)
2661{
2662 char retry;
2663 int i, j, len1, len2;
2664 ushort usum, *ptr, keycode;
2665
2666 if (moxa_boards[cardno].boardType == MOXA_BOARD_CP204J)
2667 keycode = CP204J_KeyCode;
2668 else
2669 keycode = C218_KeyCode;
2670 usum = 0;
2671 len1 = len >> 1;
2672 ptr = (ushort *) moxaBuff;
2673 for (i = 0; i < len1; i++)
01cfaf0d 2674 usum += le16_to_cpu(*(ptr + i));
1da177e4
LT
2675 retry = 0;
2676 do {
2677 len1 = len >> 1;
2678 j = 0;
2679 while (len1) {
2680 len2 = (len1 > 2048) ? 2048 : len1;
2681 len1 -= len2;
2682 for (i = 0; i < len2 << 1; i++)
2683 writeb(moxaBuff[i + j], baseAddr + C218_LoadBuf + i);
2684 j += i;
2685
2686 writew(len2, baseAddr + C218DLoad_len);
2687 writew(0, baseAddr + C218_key);
2688 for (i = 0; i < 100; i++) {
2689 if (readw(baseAddr + C218_key) == keycode)
2690 break;
2691 moxadelay(1); /* delay 10 ms */
2692 }
2693 if (readw(baseAddr + C218_key) != keycode) {
2694 return (-1);
2695 }
2696 }
2697 writew(0, baseAddr + C218DLoad_len);
2698 writew(usum, baseAddr + C218check_sum);
2699 writew(0, baseAddr + C218_key);
2700 for (i = 0; i < 100; i++) {
2701 if (readw(baseAddr + C218_key) == keycode)
2702 break;
2703 moxadelay(1); /* delay 10 ms */
2704 }
2705 retry++;
2706 } while ((readb(baseAddr + C218chksum_ok) != 1) && (retry < 3));
2707 if (readb(baseAddr + C218chksum_ok) != 1) {
2708 return (-1);
2709 }
2710 writew(0, baseAddr + C218_key);
2711 for (i = 0; i < 100; i++) {
2712 if (readw(baseAddr + Magic_no) == Magic_code)
2713 break;
2714 moxadelay(1); /* delay 10 ms */
2715 }
2716 if (readw(baseAddr + Magic_no) != Magic_code) {
2717 return (-1);
2718 }
2719 writew(1, baseAddr + Disable_IRQ);
2720 writew(0, baseAddr + Magic_no);
2721 for (i = 0; i < 100; i++) {
2722 if (readw(baseAddr + Magic_no) == Magic_code)
2723 break;
2724 moxadelay(1); /* delay 10 ms */
2725 }
2726 if (readw(baseAddr + Magic_no) != Magic_code) {
2727 return (-1);
2728 }
2729 moxaCard = 1;
8f8ecbad
JS
2730 moxa_boards[cardno].intNdx = baseAddr + IRQindex;
2731 moxa_boards[cardno].intPend = baseAddr + IRQpending;
2732 moxa_boards[cardno].intTable = baseAddr + IRQtable;
1da177e4
LT
2733 return (0);
2734}
2735
2736static int moxaloadc320(int cardno, void __iomem *baseAddr, int len, int *numPorts)
2737{
2738 ushort usum;
2739 int i, j, wlen, len2, retry;
2740 ushort *uptr;
2741
2742 usum = 0;
2743 wlen = len >> 1;
2744 uptr = (ushort *) moxaBuff;
2745 for (i = 0; i < wlen; i++)
01cfaf0d 2746 usum += le16_to_cpu(uptr[i]);
1da177e4
LT
2747 retry = 0;
2748 j = 0;
2749 do {
2750 while (wlen) {
2751 if (wlen > 2048)
2752 len2 = 2048;
2753 else
2754 len2 = wlen;
2755 wlen -= len2;
2756 len2 <<= 1;
2757 for (i = 0; i < len2; i++)
2758 writeb(moxaBuff[j + i], baseAddr + C320_LoadBuf + i);
2759 len2 >>= 1;
2760 j += i;
2761 writew(len2, baseAddr + C320DLoad_len);
2762 writew(0, baseAddr + C320_key);
2763 for (i = 0; i < 10; i++) {
2764 if (readw(baseAddr + C320_key) == C320_KeyCode)
2765 break;
2766 moxadelay(1);
2767 }
2768 if (readw(baseAddr + C320_key) != C320_KeyCode)
2769 return (-1);
2770 }
2771 writew(0, baseAddr + C320DLoad_len);
2772 writew(usum, baseAddr + C320check_sum);
2773 writew(0, baseAddr + C320_key);
2774 for (i = 0; i < 10; i++) {
2775 if (readw(baseAddr + C320_key) == C320_KeyCode)
2776 break;
2777 moxadelay(1);
2778 }
2779 retry++;
2780 } while ((readb(baseAddr + C320chksum_ok) != 1) && (retry < 3));
2781 if (readb(baseAddr + C320chksum_ok) != 1)
2782 return (-1);
2783 writew(0, baseAddr + C320_key);
2784 for (i = 0; i < 600; i++) {
2785 if (readw(baseAddr + Magic_no) == Magic_code)
2786 break;
2787 moxadelay(1);
2788 }
2789 if (readw(baseAddr + Magic_no) != Magic_code)
2790 return (-100);
2791
2792 if (moxa_boards[cardno].busType == MOXA_BUS_TYPE_PCI) { /* ASIC board */
2793 writew(0x3800, baseAddr + TMS320_PORT1);
2794 writew(0x3900, baseAddr + TMS320_PORT2);
2795 writew(28499, baseAddr + TMS320_CLOCK);
2796 } else {
2797 writew(0x3200, baseAddr + TMS320_PORT1);
2798 writew(0x3400, baseAddr + TMS320_PORT2);
2799 writew(19999, baseAddr + TMS320_CLOCK);
2800 }
2801 writew(1, baseAddr + Disable_IRQ);
2802 writew(0, baseAddr + Magic_no);
2803 for (i = 0; i < 500; i++) {
2804 if (readw(baseAddr + Magic_no) == Magic_code)
2805 break;
2806 moxadelay(1);
2807 }
2808 if (readw(baseAddr + Magic_no) != Magic_code)
2809 return (-102);
2810
2811 j = readw(baseAddr + Module_cnt);
2812 if (j <= 0)
2813 return (-101);
2814 *numPorts = j * 8;
2815 writew(j, baseAddr + Module_no);
2816 writew(0, baseAddr + Magic_no);
2817 for (i = 0; i < 600; i++) {
2818 if (readw(baseAddr + Magic_no) == Magic_code)
2819 break;
2820 moxadelay(1);
2821 }
2822 if (readw(baseAddr + Magic_no) != Magic_code)
2823 return (-102);
2824 moxaCard = 1;
8f8ecbad
JS
2825 moxa_boards[cardno].intNdx = baseAddr + IRQindex;
2826 moxa_boards[cardno].intPend = baseAddr + IRQpending;
2827 moxa_boards[cardno].intTable = baseAddr + IRQtable;
1da177e4
LT
2828 return (0);
2829}
2830
1da177e4
LT
2831static void MoxaSetFifo(int port, int enable)
2832{
8f8ecbad 2833 void __iomem *ofsAddr = moxa_ports[port].tableAddr;
1da177e4
LT
2834
2835 if (!enable) {
2836 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 0);
2837 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 1);
2838 } else {
2839 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 3);
2840 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 16);
2841 }
2842}
This page took 0.341903 seconds and 5 git commands to generate.