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