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