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