Char: moxa, remove unused port entries
[linux/fpc-iii.git] / drivers / char / moxa.c
blobc440ec5426861778ce2ca38cf982686aeda6627e
1 /*****************************************************************************/
2 /*
3 * moxa.c -- MOXA Intellio family multiport serial driver.
5 * Copyright (C) 1999-2000 Moxa Technologies (support@moxa.com.tw).
7 * This code is loosely based on the Linux serial driver, written by
8 * Linus Torvalds, Theodore T'so and others.
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.
17 * MOXA Intellio Series Driver
18 * for : LINUX
19 * date : 1999/1/7
20 * version : 5.1
23 #include <linux/module.h>
24 #include <linux/types.h>
25 #include <linux/mm.h>
26 #include <linux/ioport.h>
27 #include <linux/errno.h>
28 #include <linux/firmware.h>
29 #include <linux/signal.h>
30 #include <linux/sched.h>
31 #include <linux/timer.h>
32 #include <linux/interrupt.h>
33 #include <linux/tty.h>
34 #include <linux/tty_flip.h>
35 #include <linux/major.h>
36 #include <linux/string.h>
37 #include <linux/fcntl.h>
38 #include <linux/ptrace.h>
39 #include <linux/serial.h>
40 #include <linux/tty_driver.h>
41 #include <linux/delay.h>
42 #include <linux/pci.h>
43 #include <linux/init.h>
44 #include <linux/bitops.h>
45 #include <linux/completion.h>
47 #include <asm/system.h>
48 #include <asm/io.h>
49 #include <asm/uaccess.h>
51 #include "moxa.h"
53 #define MOXA_VERSION "5.1k"
55 #define MOXA_FW_HDRLEN 32
57 #define MOXAMAJOR 172
58 #define MOXACUMAJOR 173
60 #define MAX_BOARDS 4 /* Don't change this value */
61 #define MAX_PORTS_PER_BOARD 32 /* Don't change this value */
62 #define MAX_PORTS (MAX_BOARDS * MAX_PORTS_PER_BOARD)
65 * Define the Moxa PCI vendor and device IDs.
67 #define MOXA_BUS_TYPE_ISA 0
68 #define MOXA_BUS_TYPE_PCI 1
70 enum {
71 MOXA_BOARD_C218_PCI = 1,
72 MOXA_BOARD_C218_ISA,
73 MOXA_BOARD_C320_PCI,
74 MOXA_BOARD_C320_ISA,
75 MOXA_BOARD_CP204J,
78 static char *moxa_brdname[] =
80 "C218 Turbo PCI series",
81 "C218 Turbo ISA series",
82 "C320 Turbo PCI series",
83 "C320 Turbo ISA series",
84 "CP-204J series",
87 #ifdef CONFIG_PCI
88 static struct pci_device_id moxa_pcibrds[] = {
89 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C218),
90 .driver_data = MOXA_BOARD_C218_PCI },
91 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C320),
92 .driver_data = MOXA_BOARD_C320_PCI },
93 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP204J),
94 .driver_data = MOXA_BOARD_CP204J },
95 { 0 }
97 MODULE_DEVICE_TABLE(pci, moxa_pcibrds);
98 #endif /* CONFIG_PCI */
100 struct moxa_port;
102 static struct moxa_board_conf {
103 int boardType;
104 int numPorts;
105 int busType;
107 int loadstat;
109 struct moxa_port *ports;
111 void __iomem *basemem;
112 void __iomem *intNdx;
113 void __iomem *intPend;
114 void __iomem *intTable;
115 } moxa_boards[MAX_BOARDS];
117 struct mxser_mstatus {
118 tcflag_t cflag;
119 int cts;
120 int dsr;
121 int ri;
122 int dcd;
125 struct moxaq_str {
126 int inq;
127 int outq;
130 struct moxa_port {
131 struct moxa_board_conf *board;
132 int type;
133 int close_delay;
134 int count;
135 int blocked_open;
136 int asyncflags;
137 unsigned long statusflags;
138 struct tty_struct *tty;
139 int cflag;
140 wait_queue_head_t open_wait;
141 struct completion close_wait;
143 struct timer_list emptyTimer;
145 char chkPort;
146 char lineCtrl;
147 void __iomem *tableAddr;
148 char DCDState;
149 char lowChkFlag;
151 ushort breakCnt;
154 /* statusflags */
155 #define TXSTOPPED 0x1
156 #define LOWWAIT 0x2
157 #define EMPTYWAIT 0x4
158 #define THROTTLE 0x8
160 #define SERIAL_DO_RESTART
162 #define WAKEUP_CHARS 256
164 static int ttymajor = MOXAMAJOR;
165 static int moxaCard;
166 /* Variables for insmod */
167 #ifdef MODULE
168 static unsigned long baseaddr[MAX_BOARDS];
169 static unsigned int type[MAX_BOARDS];
170 static unsigned int numports[MAX_BOARDS];
171 #endif
173 MODULE_AUTHOR("William Chen");
174 MODULE_DESCRIPTION("MOXA Intellio Family Multiport Board Device Driver");
175 MODULE_LICENSE("GPL");
176 #ifdef MODULE
177 module_param_array(type, uint, NULL, 0);
178 MODULE_PARM_DESC(type, "card type: C218=2, C320=4");
179 module_param_array(baseaddr, ulong, NULL, 0);
180 MODULE_PARM_DESC(baseaddr, "base address");
181 module_param_array(numports, uint, NULL, 0);
182 MODULE_PARM_DESC(numports, "numports (ignored for C218)");
183 #endif
184 module_param(ttymajor, int, 0);
187 * static functions:
189 static int moxa_open(struct tty_struct *, struct file *);
190 static void moxa_close(struct tty_struct *, struct file *);
191 static int moxa_write(struct tty_struct *, const unsigned char *, int);
192 static int moxa_write_room(struct tty_struct *);
193 static void moxa_flush_buffer(struct tty_struct *);
194 static int moxa_chars_in_buffer(struct tty_struct *);
195 static void moxa_flush_chars(struct tty_struct *);
196 static void moxa_put_char(struct tty_struct *, unsigned char);
197 static int moxa_ioctl(struct tty_struct *, struct file *, unsigned int, unsigned long);
198 static void moxa_throttle(struct tty_struct *);
199 static void moxa_unthrottle(struct tty_struct *);
200 static void moxa_set_termios(struct tty_struct *, struct ktermios *);
201 static void moxa_stop(struct tty_struct *);
202 static void moxa_start(struct tty_struct *);
203 static void moxa_hangup(struct tty_struct *);
204 static int moxa_tiocmget(struct tty_struct *tty, struct file *file);
205 static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
206 unsigned int set, unsigned int clear);
207 static void moxa_poll(unsigned long);
208 static void moxa_set_tty_param(struct tty_struct *, struct ktermios *);
209 static int moxa_block_till_ready(struct tty_struct *, struct file *,
210 struct moxa_port *);
211 static void moxa_setup_empty_event(struct tty_struct *);
212 static void moxa_check_xmit_empty(unsigned long);
213 static void moxa_shut_down(struct moxa_port *);
214 static void moxa_receive_data(struct moxa_port *);
216 * moxa board interface functions:
218 static int MoxaDriverIoctl(struct tty_struct *, unsigned int, unsigned long);
219 static int MoxaDriverPoll(void);
220 static int MoxaPortsOfCard(int);
221 static int MoxaPortIsValid(int);
222 static void MoxaPortEnable(struct moxa_port *);
223 static void MoxaPortDisable(struct moxa_port *);
224 static int MoxaPortSetTermio(struct moxa_port *, struct ktermios *, speed_t);
225 static int MoxaPortGetLineOut(struct moxa_port *, int *, int *);
226 static void MoxaPortLineCtrl(struct moxa_port *, int, int);
227 static void MoxaPortFlowCtrl(struct moxa_port *, int, int, int, int, int);
228 static int MoxaPortLineStatus(struct moxa_port *);
229 static int MoxaPortDCDChange(struct moxa_port *);
230 static int MoxaPortDCDON(struct moxa_port *);
231 static void MoxaPortFlushData(struct moxa_port *, int);
232 static int MoxaPortWriteData(struct moxa_port *, unsigned char *, int);
233 static int MoxaPortReadData(struct moxa_port *, struct tty_struct *tty);
234 static int MoxaPortTxQueue(struct moxa_port *);
235 static int MoxaPortRxQueue(struct moxa_port *);
236 static int MoxaPortTxFree(struct moxa_port *);
237 static void MoxaPortTxDisable(struct moxa_port *);
238 static void MoxaPortTxEnable(struct moxa_port *);
239 static int MoxaPortResetBrkCnt(struct moxa_port *);
240 static void MoxaPortSendBreak(struct moxa_port *, int);
241 static int moxa_get_serial_info(struct moxa_port *, struct serial_struct __user *);
242 static int moxa_set_serial_info(struct moxa_port *, struct serial_struct __user *);
243 static void MoxaSetFifo(struct moxa_port *port, int enable);
245 static const struct tty_operations moxa_ops = {
246 .open = moxa_open,
247 .close = moxa_close,
248 .write = moxa_write,
249 .write_room = moxa_write_room,
250 .flush_buffer = moxa_flush_buffer,
251 .chars_in_buffer = moxa_chars_in_buffer,
252 .flush_chars = moxa_flush_chars,
253 .put_char = moxa_put_char,
254 .ioctl = moxa_ioctl,
255 .throttle = moxa_throttle,
256 .unthrottle = moxa_unthrottle,
257 .set_termios = moxa_set_termios,
258 .stop = moxa_stop,
259 .start = moxa_start,
260 .hangup = moxa_hangup,
261 .tiocmget = moxa_tiocmget,
262 .tiocmset = moxa_tiocmset,
265 static struct tty_driver *moxaDriver;
266 static struct moxa_port moxa_ports[MAX_PORTS];
267 static DEFINE_TIMER(moxaTimer, moxa_poll, 0, 0);
268 static DEFINE_SPINLOCK(moxa_lock);
270 static int moxa_check_fw_model(struct moxa_board_conf *brd, u8 model)
272 switch (brd->boardType) {
273 case MOXA_BOARD_C218_ISA:
274 case MOXA_BOARD_C218_PCI:
275 if (model != 1)
276 goto err;
277 break;
278 case MOXA_BOARD_CP204J:
279 if (model != 3)
280 goto err;
281 break;
282 default:
283 if (model != 2)
284 goto err;
285 break;
287 return 0;
288 err:
289 return -EINVAL;
292 static int moxa_check_fw(const void *ptr)
294 const __le16 *lptr = ptr;
296 if (*lptr != cpu_to_le16(0x7980))
297 return -EINVAL;
299 return 0;
302 static int moxa_load_bios(struct moxa_board_conf *brd, const u8 *buf,
303 size_t len)
305 void __iomem *baseAddr = brd->basemem;
306 u16 tmp;
308 writeb(HW_reset, baseAddr + Control_reg); /* reset */
309 msleep(10);
310 memset_io(baseAddr, 0, 4096);
311 memcpy_toio(baseAddr, buf, len); /* download BIOS */
312 writeb(0, baseAddr + Control_reg); /* restart */
314 msleep(2000);
316 switch (brd->boardType) {
317 case MOXA_BOARD_C218_ISA:
318 case MOXA_BOARD_C218_PCI:
319 tmp = readw(baseAddr + C218_key);
320 if (tmp != C218_KeyCode)
321 goto err;
322 break;
323 case MOXA_BOARD_CP204J:
324 tmp = readw(baseAddr + C218_key);
325 if (tmp != CP204J_KeyCode)
326 goto err;
327 break;
328 default:
329 tmp = readw(baseAddr + C320_key);
330 if (tmp != C320_KeyCode)
331 goto err;
332 tmp = readw(baseAddr + C320_status);
333 if (tmp != STS_init) {
334 printk(KERN_ERR "moxa: bios upload failed -- CPU/Basic "
335 "module not found\n");
336 return -EIO;
338 break;
341 return 0;
342 err:
343 printk(KERN_ERR "moxa: bios upload failed -- board not found\n");
344 return -EIO;
347 static int moxa_load_320b(struct moxa_board_conf *brd, const u8 *ptr,
348 size_t len)
350 void __iomem *baseAddr = brd->basemem;
352 if (len < 7168) {
353 printk(KERN_ERR "moxa: invalid 320 bios -- too short\n");
354 return -EINVAL;
357 writew(len - 7168 - 2, baseAddr + C320bapi_len);
358 writeb(1, baseAddr + Control_reg); /* Select Page 1 */
359 memcpy_toio(baseAddr + DynPage_addr, ptr, 7168);
360 writeb(2, baseAddr + Control_reg); /* Select Page 2 */
361 memcpy_toio(baseAddr + DynPage_addr, ptr + 7168, len - 7168);
363 return 0;
366 static int moxa_real_load_code(struct moxa_board_conf *brd, const void *ptr,
367 size_t len)
369 void __iomem *baseAddr = brd->basemem;
370 const u16 *uptr = ptr;
371 size_t wlen, len2, j;
372 unsigned long key, loadbuf, loadlen, checksum, checksum_ok;
373 unsigned int i, retry, c320;
374 u16 usum, keycode;
376 c320 = brd->boardType == MOXA_BOARD_C320_PCI ||
377 brd->boardType == MOXA_BOARD_C320_ISA;
378 keycode = (brd->boardType == MOXA_BOARD_CP204J) ? CP204J_KeyCode :
379 C218_KeyCode;
381 switch (brd->boardType) {
382 case MOXA_BOARD_CP204J:
383 case MOXA_BOARD_C218_ISA:
384 case MOXA_BOARD_C218_PCI:
385 key = C218_key;
386 loadbuf = C218_LoadBuf;
387 loadlen = C218DLoad_len;
388 checksum = C218check_sum;
389 checksum_ok = C218chksum_ok;
390 break;
391 default:
392 key = C320_key;
393 keycode = C320_KeyCode;
394 loadbuf = C320_LoadBuf;
395 loadlen = C320DLoad_len;
396 checksum = C320check_sum;
397 checksum_ok = C320chksum_ok;
398 break;
401 usum = 0;
402 wlen = len >> 1;
403 for (i = 0; i < wlen; i++)
404 usum += le16_to_cpu(uptr[i]);
405 retry = 0;
406 do {
407 wlen = len >> 1;
408 j = 0;
409 while (wlen) {
410 len2 = (wlen > 2048) ? 2048 : wlen;
411 wlen -= len2;
412 memcpy_toio(baseAddr + loadbuf, ptr + j, len2 << 1);
413 j += len2 << 1;
415 writew(len2, baseAddr + loadlen);
416 writew(0, baseAddr + key);
417 for (i = 0; i < 100; i++) {
418 if (readw(baseAddr + key) == keycode)
419 break;
420 msleep(10);
422 if (readw(baseAddr + key) != keycode)
423 return -EIO;
425 writew(0, baseAddr + loadlen);
426 writew(usum, baseAddr + checksum);
427 writew(0, baseAddr + key);
428 for (i = 0; i < 100; i++) {
429 if (readw(baseAddr + key) == keycode)
430 break;
431 msleep(10);
433 retry++;
434 } while ((readb(baseAddr + checksum_ok) != 1) && (retry < 3));
435 if (readb(baseAddr + checksum_ok) != 1)
436 return -EIO;
438 writew(0, baseAddr + key);
439 for (i = 0; i < 600; i++) {
440 if (readw(baseAddr + Magic_no) == Magic_code)
441 break;
442 msleep(10);
444 if (readw(baseAddr + Magic_no) != Magic_code)
445 return -EIO;
447 if (c320) {
448 if (brd->busType == MOXA_BUS_TYPE_PCI) { /* ASIC board */
449 writew(0x3800, baseAddr + TMS320_PORT1);
450 writew(0x3900, baseAddr + TMS320_PORT2);
451 writew(28499, baseAddr + TMS320_CLOCK);
452 } else {
453 writew(0x3200, baseAddr + TMS320_PORT1);
454 writew(0x3400, baseAddr + TMS320_PORT2);
455 writew(19999, baseAddr + TMS320_CLOCK);
458 writew(1, baseAddr + Disable_IRQ);
459 writew(0, baseAddr + Magic_no);
460 for (i = 0; i < 500; i++) {
461 if (readw(baseAddr + Magic_no) == Magic_code)
462 break;
463 msleep(10);
465 if (readw(baseAddr + Magic_no) != Magic_code)
466 return -EIO;
468 if (c320) {
469 j = readw(baseAddr + Module_cnt);
470 if (j <= 0)
471 return -EIO;
472 brd->numPorts = j * 8;
473 writew(j, baseAddr + Module_no);
474 writew(0, baseAddr + Magic_no);
475 for (i = 0; i < 600; i++) {
476 if (readw(baseAddr + Magic_no) == Magic_code)
477 break;
478 msleep(10);
480 if (readw(baseAddr + Magic_no) != Magic_code)
481 return -EIO;
483 moxaCard = 1;
484 brd->intNdx = baseAddr + IRQindex;
485 brd->intPend = baseAddr + IRQpending;
486 brd->intTable = baseAddr + IRQtable;
488 return 0;
491 static int moxa_load_code(struct moxa_board_conf *brd, const void *ptr,
492 size_t len)
494 void __iomem *ofsAddr, *baseAddr = brd->basemem;
495 struct moxa_port *port;
496 int retval, i;
498 if (len % 2) {
499 printk(KERN_ERR "moxa: bios length is not even\n");
500 return -EINVAL;
503 retval = moxa_real_load_code(brd, ptr, len); /* may change numPorts */
504 if (retval)
505 return retval;
507 switch (brd->boardType) {
508 case MOXA_BOARD_C218_ISA:
509 case MOXA_BOARD_C218_PCI:
510 case MOXA_BOARD_CP204J:
511 port = brd->ports;
512 for (i = 0; i < brd->numPorts; i++, port++) {
513 port->board = brd;
514 port->chkPort = 1;
515 port->DCDState = 0;
516 port->tableAddr = baseAddr + Extern_table +
517 Extern_size * i;
518 ofsAddr = port->tableAddr;
519 writew(C218rx_mask, ofsAddr + RX_mask);
520 writew(C218tx_mask, ofsAddr + TX_mask);
521 writew(C218rx_spage + i * C218buf_pageno, ofsAddr + Page_rxb);
522 writew(readw(ofsAddr + Page_rxb) + C218rx_pageno, ofsAddr + EndPage_rxb);
524 writew(C218tx_spage + i * C218buf_pageno, ofsAddr + Page_txb);
525 writew(readw(ofsAddr + Page_txb) + C218tx_pageno, ofsAddr + EndPage_txb);
528 break;
529 default:
530 port = brd->ports;
531 for (i = 0; i < brd->numPorts; i++, port++) {
532 port->board = brd;
533 port->chkPort = 1;
534 port->DCDState = 0;
535 port->tableAddr = baseAddr + Extern_table +
536 Extern_size * i;
537 ofsAddr = port->tableAddr;
538 switch (brd->numPorts) {
539 case 8:
540 writew(C320p8rx_mask, ofsAddr + RX_mask);
541 writew(C320p8tx_mask, ofsAddr + TX_mask);
542 writew(C320p8rx_spage + i * C320p8buf_pgno, ofsAddr + Page_rxb);
543 writew(readw(ofsAddr + Page_rxb) + C320p8rx_pgno, ofsAddr + EndPage_rxb);
544 writew(C320p8tx_spage + i * C320p8buf_pgno, ofsAddr + Page_txb);
545 writew(readw(ofsAddr + Page_txb) + C320p8tx_pgno, ofsAddr + EndPage_txb);
547 break;
548 case 16:
549 writew(C320p16rx_mask, ofsAddr + RX_mask);
550 writew(C320p16tx_mask, ofsAddr + TX_mask);
551 writew(C320p16rx_spage + i * C320p16buf_pgno, ofsAddr + Page_rxb);
552 writew(readw(ofsAddr + Page_rxb) + C320p16rx_pgno, ofsAddr + EndPage_rxb);
553 writew(C320p16tx_spage + i * C320p16buf_pgno, ofsAddr + Page_txb);
554 writew(readw(ofsAddr + Page_txb) + C320p16tx_pgno, ofsAddr + EndPage_txb);
555 break;
557 case 24:
558 writew(C320p24rx_mask, ofsAddr + RX_mask);
559 writew(C320p24tx_mask, ofsAddr + TX_mask);
560 writew(C320p24rx_spage + i * C320p24buf_pgno, ofsAddr + Page_rxb);
561 writew(readw(ofsAddr + Page_rxb) + C320p24rx_pgno, ofsAddr + EndPage_rxb);
562 writew(C320p24tx_spage + i * C320p24buf_pgno, ofsAddr + Page_txb);
563 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
564 break;
565 case 32:
566 writew(C320p32rx_mask, ofsAddr + RX_mask);
567 writew(C320p32tx_mask, ofsAddr + TX_mask);
568 writew(C320p32tx_ofs, ofsAddr + Ofs_txb);
569 writew(C320p32rx_spage + i * C320p32buf_pgno, ofsAddr + Page_rxb);
570 writew(readb(ofsAddr + Page_rxb), ofsAddr + EndPage_rxb);
571 writew(C320p32tx_spage + i * C320p32buf_pgno, ofsAddr + Page_txb);
572 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
573 break;
576 break;
578 brd->loadstat = 1;
579 return 0;
582 static int moxa_load_fw(struct moxa_board_conf *brd, const struct firmware *fw)
584 void *ptr = fw->data;
585 char rsn[64];
586 u16 lens[5];
587 size_t len;
588 unsigned int a, lenp, lencnt;
589 int ret = -EINVAL;
590 struct {
591 __le32 magic; /* 0x34303430 */
592 u8 reserved1[2];
593 u8 type; /* UNIX = 3 */
594 u8 model; /* C218T=1, C320T=2, CP204=3 */
595 u8 reserved2[8];
596 __le16 len[5];
597 } *hdr = ptr;
599 BUILD_BUG_ON(ARRAY_SIZE(hdr->len) != ARRAY_SIZE(lens));
601 if (fw->size < MOXA_FW_HDRLEN) {
602 strcpy(rsn, "too short (even header won't fit)");
603 goto err;
605 if (hdr->magic != cpu_to_le32(0x30343034)) {
606 sprintf(rsn, "bad magic: %.8x", le32_to_cpu(hdr->magic));
607 goto err;
609 if (hdr->type != 3) {
610 sprintf(rsn, "not for linux, type is %u", hdr->type);
611 goto err;
613 if (moxa_check_fw_model(brd, hdr->model)) {
614 sprintf(rsn, "not for this card, model is %u", hdr->model);
615 goto err;
618 len = MOXA_FW_HDRLEN;
619 lencnt = hdr->model == 2 ? 5 : 3;
620 for (a = 0; a < ARRAY_SIZE(lens); a++) {
621 lens[a] = le16_to_cpu(hdr->len[a]);
622 if (lens[a] && len + lens[a] <= fw->size &&
623 moxa_check_fw(&fw->data[len]))
624 printk(KERN_WARNING "moxa firmware: unexpected input "
625 "at offset %u, but going on\n", (u32)len);
626 if (!lens[a] && a < lencnt) {
627 sprintf(rsn, "too few entries in fw file");
628 goto err;
630 len += lens[a];
633 if (len != fw->size) {
634 sprintf(rsn, "bad length: %u (should be %u)", (u32)fw->size,
635 (u32)len);
636 goto err;
639 ptr += MOXA_FW_HDRLEN;
640 lenp = 0; /* bios */
642 strcpy(rsn, "read above");
644 ret = moxa_load_bios(brd, ptr, lens[lenp]);
645 if (ret)
646 goto err;
648 /* we skip the tty section (lens[1]), since we don't need it */
649 ptr += lens[lenp] + lens[lenp + 1];
650 lenp += 2; /* comm */
652 if (hdr->model == 2) {
653 ret = moxa_load_320b(brd, ptr, lens[lenp]);
654 if (ret)
655 goto err;
656 /* skip another tty */
657 ptr += lens[lenp] + lens[lenp + 1];
658 lenp += 2;
661 ret = moxa_load_code(brd, ptr, lens[lenp]);
662 if (ret)
663 goto err;
665 return 0;
666 err:
667 printk(KERN_ERR "firmware failed to load, reason: %s\n", rsn);
668 return ret;
671 static int moxa_init_board(struct moxa_board_conf *brd, struct device *dev)
673 const struct firmware *fw;
674 const char *file;
675 int ret;
677 switch (brd->boardType) {
678 case MOXA_BOARD_C218_ISA:
679 case MOXA_BOARD_C218_PCI:
680 file = "c218tunx.cod";
681 break;
682 case MOXA_BOARD_CP204J:
683 file = "cp204unx.cod";
684 break;
685 default:
686 file = "c320tunx.cod";
687 break;
690 ret = request_firmware(&fw, file, dev);
691 if (ret) {
692 printk(KERN_ERR "request_firmware failed\n");
693 goto end;
696 ret = moxa_load_fw(brd, fw);
698 release_firmware(fw);
699 end:
700 return ret;
703 #ifdef CONFIG_PCI
704 static int __devinit moxa_pci_probe(struct pci_dev *pdev,
705 const struct pci_device_id *ent)
707 struct moxa_board_conf *board;
708 unsigned int i;
709 int board_type = ent->driver_data;
710 int retval;
712 retval = pci_enable_device(pdev);
713 if (retval) {
714 dev_err(&pdev->dev, "can't enable pci device\n");
715 goto err;
718 for (i = 0; i < MAX_BOARDS; i++)
719 if (moxa_boards[i].basemem == NULL)
720 break;
722 retval = -ENODEV;
723 if (i >= MAX_BOARDS) {
724 dev_warn(&pdev->dev, "more than %u MOXA Intellio family boards "
725 "found. Board is ignored.\n", MAX_BOARDS);
726 goto err;
729 board = &moxa_boards[i];
730 board->ports = &moxa_ports[i * MAX_PORTS_PER_BOARD];
732 retval = pci_request_region(pdev, 2, "moxa-base");
733 if (retval) {
734 dev_err(&pdev->dev, "can't request pci region 2\n");
735 goto err;
738 board->basemem = ioremap(pci_resource_start(pdev, 2), 0x4000);
739 if (board->basemem == NULL) {
740 dev_err(&pdev->dev, "can't remap io space 2\n");
741 goto err_reg;
744 board->boardType = board_type;
745 switch (board_type) {
746 case MOXA_BOARD_C218_ISA:
747 case MOXA_BOARD_C218_PCI:
748 board->numPorts = 8;
749 break;
751 case MOXA_BOARD_CP204J:
752 board->numPorts = 4;
753 break;
754 default:
755 board->numPorts = 0;
756 break;
758 board->busType = MOXA_BUS_TYPE_PCI;
760 retval = moxa_init_board(board, &pdev->dev);
761 if (retval)
762 goto err_base;
764 pci_set_drvdata(pdev, board);
766 return (0);
767 err_base:
768 iounmap(board->basemem);
769 board->basemem = NULL;
770 err_reg:
771 pci_release_region(pdev, 2);
772 err:
773 return retval;
776 static void __devexit moxa_pci_remove(struct pci_dev *pdev)
778 struct moxa_board_conf *brd = pci_get_drvdata(pdev);
780 iounmap(brd->basemem);
781 brd->basemem = NULL;
782 pci_release_region(pdev, 2);
785 static struct pci_driver moxa_pci_driver = {
786 .name = "moxa",
787 .id_table = moxa_pcibrds,
788 .probe = moxa_pci_probe,
789 .remove = __devexit_p(moxa_pci_remove)
791 #endif /* CONFIG_PCI */
793 static int __init moxa_init(void)
795 struct moxa_port *ch;
796 unsigned int i, isabrds = 0;
797 int retval = 0;
799 printk(KERN_INFO "MOXA Intellio family driver version %s\n",
800 MOXA_VERSION);
801 moxaDriver = alloc_tty_driver(MAX_PORTS + 1);
802 if (!moxaDriver)
803 return -ENOMEM;
805 moxaDriver->owner = THIS_MODULE;
806 moxaDriver->name = "ttyMX";
807 moxaDriver->major = ttymajor;
808 moxaDriver->minor_start = 0;
809 moxaDriver->type = TTY_DRIVER_TYPE_SERIAL;
810 moxaDriver->subtype = SERIAL_TYPE_NORMAL;
811 moxaDriver->init_termios = tty_std_termios;
812 moxaDriver->init_termios.c_cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
813 moxaDriver->init_termios.c_ispeed = 9600;
814 moxaDriver->init_termios.c_ospeed = 9600;
815 moxaDriver->flags = TTY_DRIVER_REAL_RAW;
816 tty_set_operations(moxaDriver, &moxa_ops);
818 for (i = 0, ch = moxa_ports; i < MAX_PORTS; i++, ch++) {
819 ch->type = PORT_16550A;
820 ch->close_delay = 5 * HZ / 10;
821 ch->cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
822 init_waitqueue_head(&ch->open_wait);
823 init_completion(&ch->close_wait);
825 setup_timer(&ch->emptyTimer, moxa_check_xmit_empty,
826 (unsigned long)ch);
829 pr_debug("Moxa tty devices major number = %d\n", ttymajor);
831 if (tty_register_driver(moxaDriver)) {
832 printk(KERN_ERR "Couldn't install MOXA Smartio family driver !\n");
833 put_tty_driver(moxaDriver);
834 return -1;
837 mod_timer(&moxaTimer, jiffies + HZ / 50);
839 /* Find the boards defined from module args. */
840 #ifdef MODULE
842 struct moxa_board_conf *brd = moxa_boards;
843 for (i = 0; i < MAX_BOARDS; i++) {
844 if (!baseaddr[i])
845 break;
846 if (type[i] == MOXA_BOARD_C218_ISA ||
847 type[i] == MOXA_BOARD_C320_ISA) {
848 pr_debug("Moxa board %2d: %s board(baseAddr=%lx)\n",
849 isabrds + 1, moxa_brdname[type[i] - 1],
850 baseaddr[i]);
851 brd->boardType = type[i];
852 brd->ports = &moxa_ports[isabrds * MAX_PORTS_PER_BOARD];
853 brd->numPorts = type[i] == MOXA_BOARD_C218_ISA ? 8 :
854 numports[i];
855 brd->busType = MOXA_BUS_TYPE_ISA;
856 brd->basemem = ioremap(baseaddr[i], 0x4000);
857 if (!brd->basemem) {
858 printk(KERN_ERR "moxa: can't remap %lx\n",
859 baseaddr[i]);
860 continue;
862 if (moxa_init_board(brd, NULL)) {
863 iounmap(brd->basemem);
864 brd->basemem = NULL;
865 continue;
868 brd++;
869 isabrds++;
873 #endif
875 #ifdef CONFIG_PCI
876 retval = pci_register_driver(&moxa_pci_driver);
877 if (retval) {
878 printk(KERN_ERR "Can't register moxa pci driver!\n");
879 if (isabrds)
880 retval = 0;
882 #endif
884 return retval;
887 static void __exit moxa_exit(void)
889 int i;
891 del_timer_sync(&moxaTimer);
893 for (i = 0; i < MAX_PORTS; i++)
894 del_timer_sync(&moxa_ports[i].emptyTimer);
896 if (tty_unregister_driver(moxaDriver))
897 printk(KERN_ERR "Couldn't unregister MOXA Intellio family "
898 "serial driver\n");
899 put_tty_driver(moxaDriver);
901 #ifdef CONFIG_PCI
902 pci_unregister_driver(&moxa_pci_driver);
903 #endif
905 for (i = 0; i < MAX_BOARDS; i++)
906 if (moxa_boards[i].basemem)
907 iounmap(moxa_boards[i].basemem);
910 module_init(moxa_init);
911 module_exit(moxa_exit);
913 static int moxa_open(struct tty_struct *tty, struct file *filp)
915 struct moxa_port *ch;
916 int port;
917 int retval;
919 port = tty->index;
920 if (port == MAX_PORTS) {
921 return (0);
923 if (!MoxaPortIsValid(port)) {
924 tty->driver_data = NULL;
925 return (-ENODEV);
928 ch = &moxa_ports[port];
929 ch->count++;
930 tty->driver_data = ch;
931 ch->tty = tty;
932 if (!(ch->asyncflags & ASYNC_INITIALIZED)) {
933 ch->statusflags = 0;
934 moxa_set_tty_param(tty, tty->termios);
935 MoxaPortLineCtrl(ch, 1, 1);
936 MoxaPortEnable(ch);
937 ch->asyncflags |= ASYNC_INITIALIZED;
939 retval = moxa_block_till_ready(tty, filp, ch);
941 moxa_unthrottle(tty);
943 if (ch->type == PORT_16550A) {
944 MoxaSetFifo(ch, 1);
945 } else {
946 MoxaSetFifo(ch, 0);
949 return (retval);
952 static void moxa_close(struct tty_struct *tty, struct file *filp)
954 struct moxa_port *ch;
955 int port;
957 port = tty->index;
958 if (port == MAX_PORTS) {
959 return;
961 if (!MoxaPortIsValid(port)) {
962 pr_debug("Invalid portno in moxa_close\n");
963 tty->driver_data = NULL;
964 return;
966 if (tty->driver_data == NULL) {
967 return;
969 if (tty_hung_up_p(filp)) {
970 return;
972 ch = (struct moxa_port *) tty->driver_data;
974 if ((tty->count == 1) && (ch->count != 1)) {
975 printk(KERN_WARNING "moxa_close: bad serial port count; "
976 "tty->count is 1, ch->count is %d\n", ch->count);
977 ch->count = 1;
979 if (--ch->count < 0) {
980 printk(KERN_WARNING "moxa_close: bad serial port count, "
981 "device=%s\n", tty->name);
982 ch->count = 0;
984 if (ch->count) {
985 return;
987 ch->asyncflags |= ASYNC_CLOSING;
989 ch->cflag = tty->termios->c_cflag;
990 if (ch->asyncflags & ASYNC_INITIALIZED) {
991 moxa_setup_empty_event(tty);
992 tty_wait_until_sent(tty, 30 * HZ); /* 30 seconds timeout */
993 del_timer_sync(&ch->emptyTimer);
995 moxa_shut_down(ch);
996 MoxaPortFlushData(ch, 2);
998 if (tty->driver->flush_buffer)
999 tty->driver->flush_buffer(tty);
1000 tty_ldisc_flush(tty);
1002 tty->closing = 0;
1003 ch->tty = NULL;
1004 if (ch->blocked_open) {
1005 if (ch->close_delay) {
1006 msleep_interruptible(jiffies_to_msecs(ch->close_delay));
1008 wake_up_interruptible(&ch->open_wait);
1010 ch->asyncflags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
1011 complete_all(&ch->close_wait);
1014 static int moxa_write(struct tty_struct *tty,
1015 const unsigned char *buf, int count)
1017 struct moxa_port *ch = tty->driver_data;
1018 unsigned long flags;
1019 int len;
1021 if (ch == NULL)
1022 return 0;
1024 spin_lock_irqsave(&moxa_lock, flags);
1025 len = MoxaPortWriteData(ch, (unsigned char *) buf, count);
1026 spin_unlock_irqrestore(&moxa_lock, flags);
1028 /*********************************************
1029 if ( !(ch->statusflags & LOWWAIT) &&
1030 ((len != count) || (MoxaPortTxFree(port) <= 100)) )
1031 ************************************************/
1032 ch->statusflags |= LOWWAIT;
1033 return (len);
1036 static int moxa_write_room(struct tty_struct *tty)
1038 struct moxa_port *ch;
1040 if (tty->stopped)
1041 return (0);
1042 ch = tty->driver_data;
1043 if (ch == NULL)
1044 return (0);
1045 return MoxaPortTxFree(ch);
1048 static void moxa_flush_buffer(struct tty_struct *tty)
1050 struct moxa_port *ch = tty->driver_data;
1052 if (ch == NULL)
1053 return;
1054 MoxaPortFlushData(ch, 1);
1055 tty_wakeup(tty);
1058 static int moxa_chars_in_buffer(struct tty_struct *tty)
1060 struct moxa_port *ch = tty->driver_data;
1061 int chars;
1064 * Sigh...I have to check if driver_data is NULL here, because
1065 * if an open() fails, the TTY subsystem eventually calls
1066 * tty_wait_until_sent(), which calls the driver's chars_in_buffer()
1067 * routine. And since the open() failed, we return 0 here. TDJ
1069 if (ch == NULL)
1070 return (0);
1071 chars = MoxaPortTxQueue(ch);
1072 if (chars) {
1074 * Make it possible to wakeup anything waiting for output
1075 * in tty_ioctl.c, etc.
1077 if (!(ch->statusflags & EMPTYWAIT))
1078 moxa_setup_empty_event(tty);
1080 return (chars);
1083 static void moxa_flush_chars(struct tty_struct *tty)
1086 * Don't think I need this, because this is called to empty the TX
1087 * buffer for the 16450, 16550, etc.
1091 static void moxa_put_char(struct tty_struct *tty, unsigned char c)
1093 struct moxa_port *ch = tty->driver_data;
1094 unsigned long flags;
1096 if (ch == NULL)
1097 return;
1098 spin_lock_irqsave(&moxa_lock, flags);
1099 MoxaPortWriteData(ch, &c, 1);
1100 spin_unlock_irqrestore(&moxa_lock, flags);
1101 /************************************************
1102 if ( !(ch->statusflags & LOWWAIT) && (MoxaPortTxFree(port) <= 100) )
1103 *************************************************/
1104 ch->statusflags |= LOWWAIT;
1107 static int moxa_tiocmget(struct tty_struct *tty, struct file *file)
1109 struct moxa_port *ch = tty->driver_data;
1110 int flag = 0, dtr, rts;
1112 if ((tty->index != MAX_PORTS) && (!ch))
1113 return (-EINVAL);
1115 MoxaPortGetLineOut(ch, &dtr, &rts);
1116 if (dtr)
1117 flag |= TIOCM_DTR;
1118 if (rts)
1119 flag |= TIOCM_RTS;
1120 dtr = MoxaPortLineStatus(ch);
1121 if (dtr & 1)
1122 flag |= TIOCM_CTS;
1123 if (dtr & 2)
1124 flag |= TIOCM_DSR;
1125 if (dtr & 4)
1126 flag |= TIOCM_CD;
1127 return flag;
1130 static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
1131 unsigned int set, unsigned int clear)
1133 struct moxa_port *ch = tty->driver_data;
1134 int port;
1135 int dtr, rts;
1137 port = tty->index;
1138 if ((port != MAX_PORTS) && (!ch))
1139 return (-EINVAL);
1141 MoxaPortGetLineOut(ch, &dtr, &rts);
1142 if (set & TIOCM_RTS)
1143 rts = 1;
1144 if (set & TIOCM_DTR)
1145 dtr = 1;
1146 if (clear & TIOCM_RTS)
1147 rts = 0;
1148 if (clear & TIOCM_DTR)
1149 dtr = 0;
1150 MoxaPortLineCtrl(ch, dtr, rts);
1151 return 0;
1154 static int moxa_ioctl(struct tty_struct *tty, struct file *file,
1155 unsigned int cmd, unsigned long arg)
1157 struct moxa_port *ch = tty->driver_data;
1158 register int port;
1159 void __user *argp = (void __user *)arg;
1160 int retval;
1162 port = tty->index;
1163 if ((port != MAX_PORTS) && (!ch))
1164 return (-EINVAL);
1166 switch (cmd) {
1167 case TCSBRK: /* SVID version: non-zero arg --> no break */
1168 retval = tty_check_change(tty);
1169 if (retval)
1170 return (retval);
1171 moxa_setup_empty_event(tty);
1172 tty_wait_until_sent(tty, 0);
1173 if (!arg)
1174 MoxaPortSendBreak(ch, 0);
1175 return (0);
1176 case TCSBRKP: /* support for POSIX tcsendbreak() */
1177 retval = tty_check_change(tty);
1178 if (retval)
1179 return (retval);
1180 moxa_setup_empty_event(tty);
1181 tty_wait_until_sent(tty, 0);
1182 MoxaPortSendBreak(ch, arg);
1183 return (0);
1184 case TIOCGSOFTCAR:
1185 return put_user(C_CLOCAL(tty) ? 1 : 0, (int __user *)argp);
1186 case TIOCSSOFTCAR:
1187 if (get_user(retval, (int __user *)argp))
1188 return -EFAULT;
1189 arg = retval;
1190 tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) |
1191 (arg ? CLOCAL : 0));
1192 if (C_CLOCAL(tty))
1193 ch->asyncflags &= ~ASYNC_CHECK_CD;
1194 else
1195 ch->asyncflags |= ASYNC_CHECK_CD;
1196 return (0);
1197 case TIOCGSERIAL:
1198 return moxa_get_serial_info(ch, argp);
1200 case TIOCSSERIAL:
1201 return moxa_set_serial_info(ch, argp);
1202 default:
1203 retval = MoxaDriverIoctl(tty, cmd, arg);
1205 return (retval);
1208 static void moxa_throttle(struct tty_struct *tty)
1210 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
1212 ch->statusflags |= THROTTLE;
1215 static void moxa_unthrottle(struct tty_struct *tty)
1217 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
1219 ch->statusflags &= ~THROTTLE;
1222 static void moxa_set_termios(struct tty_struct *tty,
1223 struct ktermios *old_termios)
1225 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
1227 if (ch == NULL)
1228 return;
1229 moxa_set_tty_param(tty, old_termios);
1230 if (!(old_termios->c_cflag & CLOCAL) &&
1231 (tty->termios->c_cflag & CLOCAL))
1232 wake_up_interruptible(&ch->open_wait);
1235 static void moxa_stop(struct tty_struct *tty)
1237 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
1239 if (ch == NULL)
1240 return;
1241 MoxaPortTxDisable(ch);
1242 ch->statusflags |= TXSTOPPED;
1246 static void moxa_start(struct tty_struct *tty)
1248 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
1250 if (ch == NULL)
1251 return;
1253 if (!(ch->statusflags & TXSTOPPED))
1254 return;
1256 MoxaPortTxEnable(ch);
1257 ch->statusflags &= ~TXSTOPPED;
1260 static void moxa_hangup(struct tty_struct *tty)
1262 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
1264 moxa_flush_buffer(tty);
1265 moxa_shut_down(ch);
1266 ch->count = 0;
1267 ch->asyncflags &= ~ASYNC_NORMAL_ACTIVE;
1268 ch->tty = NULL;
1269 wake_up_interruptible(&ch->open_wait);
1272 static void moxa_poll(unsigned long ignored)
1274 register int card;
1275 struct moxa_port *ch;
1276 struct tty_struct *tp;
1277 int i, ports;
1279 del_timer(&moxaTimer);
1281 if (MoxaDriverPoll() < 0) {
1282 mod_timer(&moxaTimer, jiffies + HZ / 50);
1283 return;
1285 for (card = 0; card < MAX_BOARDS; card++) {
1286 if ((ports = MoxaPortsOfCard(card)) <= 0)
1287 continue;
1288 ch = &moxa_ports[card * MAX_PORTS_PER_BOARD];
1289 for (i = 0; i < ports; i++, ch++) {
1290 if ((ch->asyncflags & ASYNC_INITIALIZED) == 0)
1291 continue;
1292 if (!(ch->statusflags & THROTTLE) &&
1293 (MoxaPortRxQueue(ch) > 0))
1294 moxa_receive_data(ch);
1295 if ((tp = ch->tty) == 0)
1296 continue;
1297 if (ch->statusflags & LOWWAIT) {
1298 if (MoxaPortTxQueue(ch) <= WAKEUP_CHARS) {
1299 if (!tp->stopped) {
1300 ch->statusflags &= ~LOWWAIT;
1301 tty_wakeup(tp);
1305 if (!I_IGNBRK(tp) && (MoxaPortResetBrkCnt(ch) > 0)) {
1306 tty_insert_flip_char(tp, 0, TTY_BREAK);
1307 tty_schedule_flip(tp);
1309 if (MoxaPortDCDChange(ch)) {
1310 if (ch->asyncflags & ASYNC_CHECK_CD) {
1311 if (MoxaPortDCDON(ch))
1312 wake_up_interruptible(&ch->open_wait);
1313 else {
1314 tty_hangup(tp);
1315 wake_up_interruptible(&ch->open_wait);
1316 ch->asyncflags &= ~ASYNC_NORMAL_ACTIVE;
1323 mod_timer(&moxaTimer, jiffies + HZ / 50);
1326 /******************************************************************************/
1328 static void moxa_set_tty_param(struct tty_struct *tty, struct ktermios *old_termios)
1330 register struct ktermios *ts;
1331 struct moxa_port *ch;
1332 int rts, cts, txflow, rxflow, xany, baud;
1334 ch = (struct moxa_port *) tty->driver_data;
1335 ts = tty->termios;
1336 if (ts->c_cflag & CLOCAL)
1337 ch->asyncflags &= ~ASYNC_CHECK_CD;
1338 else
1339 ch->asyncflags |= ASYNC_CHECK_CD;
1340 rts = cts = txflow = rxflow = xany = 0;
1341 if (ts->c_cflag & CRTSCTS)
1342 rts = cts = 1;
1343 if (ts->c_iflag & IXON)
1344 txflow = 1;
1345 if (ts->c_iflag & IXOFF)
1346 rxflow = 1;
1347 if (ts->c_iflag & IXANY)
1348 xany = 1;
1350 /* Clear the features we don't support */
1351 ts->c_cflag &= ~CMSPAR;
1352 MoxaPortFlowCtrl(ch, rts, cts, txflow, rxflow, xany);
1353 baud = MoxaPortSetTermio(ch, ts, tty_get_baud_rate(tty));
1354 if (baud == -1)
1355 baud = tty_termios_baud_rate(old_termios);
1356 /* Not put the baud rate into the termios data */
1357 tty_encode_baud_rate(tty, baud, baud);
1360 static int moxa_block_till_ready(struct tty_struct *tty, struct file *filp,
1361 struct moxa_port *ch)
1363 DECLARE_WAITQUEUE(wait,current);
1364 unsigned long flags;
1365 int retval;
1366 int do_clocal = C_CLOCAL(tty);
1369 * If the device is in the middle of being closed, then block
1370 * until it's done, and then try again.
1372 if (tty_hung_up_p(filp) || (ch->asyncflags & ASYNC_CLOSING)) {
1373 if (ch->asyncflags & ASYNC_CLOSING)
1374 wait_for_completion_interruptible(&ch->close_wait);
1375 #ifdef SERIAL_DO_RESTART
1376 if (ch->asyncflags & ASYNC_HUP_NOTIFY)
1377 return (-EAGAIN);
1378 else
1379 return (-ERESTARTSYS);
1380 #else
1381 return (-EAGAIN);
1382 #endif
1385 * If non-blocking mode is set, then make the check up front
1386 * and then exit.
1388 if (filp->f_flags & O_NONBLOCK) {
1389 ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
1390 return (0);
1393 * Block waiting for the carrier detect and the line to become free
1395 retval = 0;
1396 add_wait_queue(&ch->open_wait, &wait);
1397 pr_debug("block_til_ready before block: ttys%d, count = %d\n",
1398 tty->index, ch->count);
1399 spin_lock_irqsave(&moxa_lock, flags);
1400 if (!tty_hung_up_p(filp))
1401 ch->count--;
1402 ch->blocked_open++;
1403 spin_unlock_irqrestore(&moxa_lock, flags);
1405 while (1) {
1406 set_current_state(TASK_INTERRUPTIBLE);
1407 if (tty_hung_up_p(filp) ||
1408 !(ch->asyncflags & ASYNC_INITIALIZED)) {
1409 #ifdef SERIAL_DO_RESTART
1410 if (ch->asyncflags & ASYNC_HUP_NOTIFY)
1411 retval = -EAGAIN;
1412 else
1413 retval = -ERESTARTSYS;
1414 #else
1415 retval = -EAGAIN;
1416 #endif
1417 break;
1419 if (!(ch->asyncflags & ASYNC_CLOSING) && (do_clocal ||
1420 MoxaPortDCDON(ch)))
1421 break;
1423 if (signal_pending(current)) {
1424 retval = -ERESTARTSYS;
1425 break;
1427 schedule();
1429 set_current_state(TASK_RUNNING);
1430 remove_wait_queue(&ch->open_wait, &wait);
1432 spin_lock_irqsave(&moxa_lock, flags);
1433 if (!tty_hung_up_p(filp))
1434 ch->count++;
1435 ch->blocked_open--;
1436 spin_unlock_irqrestore(&moxa_lock, flags);
1437 pr_debug("block_til_ready after blocking: ttys%d, count = %d\n",
1438 tty->index, ch->count);
1439 if (retval)
1440 return (retval);
1441 /* FIXME: review to see if we need to use set_bit on these */
1442 ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
1443 return 0;
1446 static void moxa_setup_empty_event(struct tty_struct *tty)
1448 struct moxa_port *ch = tty->driver_data;
1449 unsigned long flags;
1451 spin_lock_irqsave(&moxa_lock, flags);
1452 ch->statusflags |= EMPTYWAIT;
1453 mod_timer(&ch->emptyTimer, jiffies + HZ);
1454 spin_unlock_irqrestore(&moxa_lock, flags);
1457 static void moxa_check_xmit_empty(unsigned long data)
1459 struct moxa_port *ch;
1461 ch = (struct moxa_port *) data;
1462 if (ch->tty && (ch->statusflags & EMPTYWAIT)) {
1463 if (MoxaPortTxQueue(ch) == 0) {
1464 ch->statusflags &= ~EMPTYWAIT;
1465 tty_wakeup(ch->tty);
1466 return;
1468 mod_timer(&ch->emptyTimer, round_jiffies(jiffies + HZ));
1469 } else
1470 ch->statusflags &= ~EMPTYWAIT;
1473 static void moxa_shut_down(struct moxa_port *ch)
1475 struct tty_struct *tp;
1477 if (!(ch->asyncflags & ASYNC_INITIALIZED))
1478 return;
1480 tp = ch->tty;
1482 MoxaPortDisable(ch);
1485 * If we're a modem control device and HUPCL is on, drop RTS & DTR.
1487 if (tp->termios->c_cflag & HUPCL)
1488 MoxaPortLineCtrl(ch, 0, 0);
1490 ch->asyncflags &= ~ASYNC_INITIALIZED;
1493 static void moxa_receive_data(struct moxa_port *ch)
1495 struct tty_struct *tp;
1496 struct ktermios *ts;
1497 unsigned long flags;
1499 ts = NULL;
1500 tp = ch->tty;
1501 if (tp)
1502 ts = tp->termios;
1503 /**************************************************
1504 if ( !tp || !ts || !(ts->c_cflag & CREAD) ) {
1505 *****************************************************/
1506 if (!tp || !ts) {
1507 MoxaPortFlushData(ch, 0);
1508 return;
1510 spin_lock_irqsave(&moxa_lock, flags);
1511 MoxaPortReadData(ch, tp);
1512 spin_unlock_irqrestore(&moxa_lock, flags);
1513 tty_schedule_flip(tp);
1517 * Query
1520 struct mon_str {
1521 int tick;
1522 int rxcnt[MAX_PORTS];
1523 int txcnt[MAX_PORTS];
1526 #define DCD_changed 0x01
1527 #define DCD_oldstate 0x80
1529 static int moxaLowWaterChk;
1530 static struct mon_str moxaLog;
1531 static int moxaFuncTout = HZ / 2;
1533 static void moxafunc(void __iomem *, int, ushort);
1534 static void moxa_wait_finish(void __iomem *);
1535 static void moxa_low_water_check(void __iomem *);
1537 /*****************************************************************************
1538 * Driver level functions: *
1539 * 2. MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port); *
1540 * 3. MoxaDriverPoll(void); *
1541 *****************************************************************************/
1542 #define MOXA 0x400
1543 #define MOXA_GET_IQUEUE (MOXA + 1) /* get input buffered count */
1544 #define MOXA_GET_OQUEUE (MOXA + 2) /* get output buffered count */
1545 #define MOXA_GETDATACOUNT (MOXA + 23)
1546 #define MOXA_GET_IOQUEUE (MOXA + 27)
1547 #define MOXA_FLUSH_QUEUE (MOXA + 28)
1548 #define MOXA_GET_CONF (MOXA + 35) /* configuration */
1549 #define MOXA_GET_MAJOR (MOXA + 63)
1550 #define MOXA_GET_CUMAJOR (MOXA + 64)
1551 #define MOXA_GETMSTATUS (MOXA + 65)
1553 static void MoxaPortFlushData(struct moxa_port *port, int mode)
1555 void __iomem *ofsAddr;
1556 if ((mode < 0) || (mode > 2))
1557 return;
1558 ofsAddr = port->tableAddr;
1559 moxafunc(ofsAddr, FC_FlushQueue, mode);
1560 if (mode != 1) {
1561 port->lowChkFlag = 0;
1562 moxa_low_water_check(ofsAddr);
1566 static int MoxaDriverIoctl(struct tty_struct *tty, unsigned int cmd,
1567 unsigned long arg)
1569 struct moxa_port *port = tty->driver_data;
1570 int i;
1571 int status;
1572 void __user *argp = (void __user *)arg;
1574 if (tty->index == MAX_PORTS) {
1575 if ((cmd != MOXA_GET_CONF) && (cmd != MOXA_GETDATACOUNT) &&
1576 (cmd != MOXA_GET_IOQUEUE) && (cmd != MOXA_GET_MAJOR) &&
1577 (cmd != MOXA_GET_CUMAJOR) && (cmd != MOXA_GETMSTATUS))
1578 return (-EINVAL);
1580 switch (cmd) {
1581 case MOXA_GETDATACOUNT:
1582 moxaLog.tick = jiffies;
1583 if(copy_to_user(argp, &moxaLog, sizeof(struct mon_str)))
1584 return -EFAULT;
1585 return (0);
1586 case MOXA_FLUSH_QUEUE:
1587 MoxaPortFlushData(port, arg);
1588 return (0);
1589 case MOXA_GET_IOQUEUE: {
1590 struct moxaq_str __user *argm = argp;
1591 struct moxaq_str tmp;
1593 for (i = 0; i < MAX_PORTS; i++, argm++) {
1594 memset(&tmp, 0, sizeof(tmp));
1595 if (moxa_ports[i].chkPort) {
1596 tmp.inq = MoxaPortRxQueue(&moxa_ports[i]);
1597 tmp.outq = MoxaPortTxQueue(&moxa_ports[i]);
1599 if (copy_to_user(argm, &tmp, sizeof(tmp)))
1600 return -EFAULT;
1602 return (0);
1603 } case MOXA_GET_OQUEUE:
1604 i = MoxaPortTxQueue(port);
1605 return put_user(i, (unsigned long __user *)argp);
1606 case MOXA_GET_IQUEUE:
1607 i = MoxaPortRxQueue(port);
1608 return put_user(i, (unsigned long __user *)argp);
1609 case MOXA_GET_MAJOR:
1610 if(copy_to_user(argp, &ttymajor, sizeof(int)))
1611 return -EFAULT;
1612 return 0;
1613 case MOXA_GET_CUMAJOR:
1614 i = 0;
1615 if(copy_to_user(argp, &i, sizeof(int)))
1616 return -EFAULT;
1617 return 0;
1618 case MOXA_GETMSTATUS: {
1619 struct mxser_mstatus __user *argm = argp;
1620 struct mxser_mstatus tmp;
1621 struct moxa_port *p;
1623 for (i = 0; i < MAX_PORTS; i++, argm++) {
1624 p = &moxa_ports[i];
1625 memset(&tmp, 0, sizeof(tmp));
1626 if (!p->chkPort) {
1627 goto copy;
1628 } else {
1629 status = MoxaPortLineStatus(p);
1630 if (status & 1)
1631 tmp.cts = 1;
1632 if (status & 2)
1633 tmp.dsr = 1;
1634 if (status & 4)
1635 tmp.dcd = 1;
1638 if (!p->tty || !p->tty->termios)
1639 tmp.cflag = p->cflag;
1640 else
1641 tmp.cflag = p->tty->termios->c_cflag;
1642 copy:
1643 if (copy_to_user(argm, &tmp, sizeof(tmp)))
1644 return -EFAULT;
1646 return 0;
1650 return -ENOIOCTLCMD;
1653 int MoxaDriverPoll(void)
1655 struct moxa_board_conf *brd;
1656 register ushort temp;
1657 register int card;
1658 void __iomem *ofsAddr;
1659 void __iomem *ip;
1660 int port, p, ports;
1662 if (moxaCard == 0)
1663 return (-1);
1664 for (card = 0; card < MAX_BOARDS; card++) {
1665 brd = &moxa_boards[card];
1666 if (brd->loadstat == 0)
1667 continue;
1668 if ((ports = brd->numPorts) == 0)
1669 continue;
1670 if (readb(brd->intPend) == 0xff) {
1671 ip = brd->intTable + readb(brd->intNdx);
1672 p = card * MAX_PORTS_PER_BOARD;
1673 ports <<= 1;
1674 for (port = 0; port < ports; port += 2, p++) {
1675 if ((temp = readw(ip + port)) != 0) {
1676 writew(0, ip + port);
1677 ofsAddr = moxa_ports[p].tableAddr;
1678 if (temp & IntrTx)
1679 writew(readw(ofsAddr + HostStat) & ~WakeupTx, ofsAddr + HostStat);
1680 if (temp & IntrBreak) {
1681 moxa_ports[p].breakCnt++;
1683 if (temp & IntrLine) {
1684 if (readb(ofsAddr + FlagStat) & DCD_state) {
1685 if ((moxa_ports[p].DCDState & DCD_oldstate) == 0)
1686 moxa_ports[p].DCDState = (DCD_oldstate |
1687 DCD_changed);
1688 } else {
1689 if (moxa_ports[p].DCDState & DCD_oldstate)
1690 moxa_ports[p].DCDState = DCD_changed;
1695 writeb(0, brd->intPend);
1697 if (moxaLowWaterChk) {
1698 p = card * MAX_PORTS_PER_BOARD;
1699 for (port = 0; port < ports; port++, p++) {
1700 if (moxa_ports[p].lowChkFlag) {
1701 moxa_ports[p].lowChkFlag = 0;
1702 ofsAddr = moxa_ports[p].tableAddr;
1703 moxa_low_water_check(ofsAddr);
1708 moxaLowWaterChk = 0;
1709 return (0);
1712 /*****************************************************************************
1713 * Card level function: *
1714 * 1. MoxaPortsOfCard(int cardno); *
1715 *****************************************************************************/
1716 int MoxaPortsOfCard(int cardno)
1719 if (moxa_boards[cardno].boardType == 0)
1720 return (0);
1721 return (moxa_boards[cardno].numPorts);
1724 /*****************************************************************************
1725 * Port level functions: *
1726 * 1. MoxaPortIsValid(int port); *
1727 * 2. MoxaPortEnable(int port); *
1728 * 3. MoxaPortDisable(int port); *
1729 * 4. MoxaPortGetMaxBaud(int port); *
1730 * 6. MoxaPortSetBaud(int port, long baud); *
1731 * 8. MoxaPortSetTermio(int port, unsigned char *termio); *
1732 * 9. MoxaPortGetLineOut(int port, int *dtrState, int *rtsState); *
1733 * 10. MoxaPortLineCtrl(int port, int dtrState, int rtsState); *
1734 * 11. MoxaPortFlowCtrl(int port, int rts, int cts, int rx, int tx,int xany); *
1735 * 12. MoxaPortLineStatus(int port); *
1736 * 13. MoxaPortDCDChange(int port); *
1737 * 14. MoxaPortDCDON(int port); *
1738 * 15. MoxaPortFlushData(int port, int mode); *
1739 * 16. MoxaPortWriteData(int port, unsigned char * buffer, int length); *
1740 * 17. MoxaPortReadData(int port, struct tty_struct *tty); *
1741 * 20. MoxaPortTxQueue(int port); *
1742 * 21. MoxaPortTxFree(int port); *
1743 * 22. MoxaPortRxQueue(int port); *
1744 * 24. MoxaPortTxDisable(int port); *
1745 * 25. MoxaPortTxEnable(int port); *
1746 * 27. MoxaPortResetBrkCnt(int port); *
1747 * 30. MoxaPortSendBreak(int port, int ticks); *
1748 *****************************************************************************/
1750 * Moxa Port Number Description:
1752 * MOXA serial driver supports up to 4 MOXA-C218/C320 boards. And,
1753 * the port number using in MOXA driver functions will be 0 to 31 for
1754 * first MOXA board, 32 to 63 for second, 64 to 95 for third and 96
1755 * to 127 for fourth. For example, if you setup three MOXA boards,
1756 * first board is C218, second board is C320-16 and third board is
1757 * C320-32. The port number of first board (C218 - 8 ports) is from
1758 * 0 to 7. The port number of second board (C320 - 16 ports) is form
1759 * 32 to 47. The port number of third board (C320 - 32 ports) is from
1760 * 64 to 95. And those port numbers form 8 to 31, 48 to 63 and 96 to
1761 * 127 will be invalid.
1764 * Moxa Functions Description:
1766 * Function 1: Driver initialization routine, this routine must be
1767 * called when initialized driver.
1768 * Syntax:
1769 * void MoxaDriverInit();
1772 * Function 2: Moxa driver private IOCTL command processing.
1773 * Syntax:
1774 * int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port);
1776 * unsigned int cmd : IOCTL command
1777 * unsigned long arg : IOCTL argument
1778 * int port : port number (0 - 127)
1780 * return: 0 (OK)
1781 * -EINVAL
1782 * -ENOIOCTLCMD
1785 * Function 3: Moxa driver polling process routine.
1786 * Syntax:
1787 * int MoxaDriverPoll(void);
1789 * return: 0 ; polling O.K.
1790 * -1 : no any Moxa card.
1793 * Function 4: Get the ports of this card.
1794 * Syntax:
1795 * int MoxaPortsOfCard(int cardno);
1797 * int cardno : card number (0 - 3)
1799 * return: 0 : this card is invalid
1800 * 8/16/24/32
1803 * Function 5: Check this port is valid or invalid
1804 * Syntax:
1805 * int MoxaPortIsValid(int port);
1806 * int port : port number (0 - 127, ref port description)
1808 * return: 0 : this port is invalid
1809 * 1 : this port is valid
1812 * Function 6: Enable this port to start Tx/Rx data.
1813 * Syntax:
1814 * void MoxaPortEnable(int port);
1815 * int port : port number (0 - 127)
1818 * Function 7: Disable this port
1819 * Syntax:
1820 * void MoxaPortDisable(int port);
1821 * int port : port number (0 - 127)
1824 * Function 8: Get the maximun available baud rate of this port.
1825 * Syntax:
1826 * long MoxaPortGetMaxBaud(int port);
1827 * int port : port number (0 - 127)
1829 * return: 0 : this port is invalid
1830 * 38400/57600/115200 bps
1833 * Function 10: Setting baud rate of this port.
1834 * Syntax:
1835 * long MoxaPortSetBaud(int port, long baud);
1836 * int port : port number (0 - 127)
1837 * long baud : baud rate (50 - 115200)
1839 * return: 0 : this port is invalid or baud < 50
1840 * 50 - 115200 : the real baud rate set to the port, if
1841 * the argument baud is large than maximun
1842 * available baud rate, the real setting
1843 * baud rate will be the maximun baud rate.
1846 * Function 12: Configure the port.
1847 * Syntax:
1848 * int MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud);
1849 * int port : port number (0 - 127)
1850 * struct ktermios * termio : termio structure pointer
1851 * speed_t baud : baud rate
1853 * return: -1 : this port is invalid or termio == NULL
1854 * 0 : setting O.K.
1857 * Function 13: Get the DTR/RTS state of this port.
1858 * Syntax:
1859 * int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState);
1860 * int port : port number (0 - 127)
1861 * int * dtrState : pointer to INT to receive the current DTR
1862 * state. (if NULL, this function will not
1863 * write to this address)
1864 * int * rtsState : pointer to INT to receive the current RTS
1865 * state. (if NULL, this function will not
1866 * write to this address)
1868 * return: -1 : this port is invalid
1869 * 0 : O.K.
1872 * Function 14: Setting the DTR/RTS output state of this port.
1873 * Syntax:
1874 * void MoxaPortLineCtrl(int port, int dtrState, int rtsState);
1875 * int port : port number (0 - 127)
1876 * int dtrState : DTR output state (0: off, 1: on)
1877 * int rtsState : RTS output state (0: off, 1: on)
1880 * Function 15: Setting the flow control of this port.
1881 * Syntax:
1882 * void MoxaPortFlowCtrl(int port, int rtsFlow, int ctsFlow, int rxFlow,
1883 * int txFlow,int xany);
1884 * int port : port number (0 - 127)
1885 * int rtsFlow : H/W RTS flow control (0: no, 1: yes)
1886 * int ctsFlow : H/W CTS flow control (0: no, 1: yes)
1887 * int rxFlow : S/W Rx XON/XOFF flow control (0: no, 1: yes)
1888 * int txFlow : S/W Tx XON/XOFF flow control (0: no, 1: yes)
1889 * int xany : S/W XANY flow control (0: no, 1: yes)
1892 * Function 16: Get ths line status of this port
1893 * Syntax:
1894 * int MoxaPortLineStatus(int port);
1895 * int port : port number (0 - 127)
1897 * return: Bit 0 - CTS state (0: off, 1: on)
1898 * Bit 1 - DSR state (0: off, 1: on)
1899 * Bit 2 - DCD state (0: off, 1: on)
1902 * Function 17: Check the DCD state has changed since the last read
1903 * of this function.
1904 * Syntax:
1905 * int MoxaPortDCDChange(int port);
1906 * int port : port number (0 - 127)
1908 * return: 0 : no changed
1909 * 1 : DCD has changed
1912 * Function 18: Check ths current DCD state is ON or not.
1913 * Syntax:
1914 * int MoxaPortDCDON(int port);
1915 * int port : port number (0 - 127)
1917 * return: 0 : DCD off
1918 * 1 : DCD on
1921 * Function 19: Flush the Rx/Tx buffer data of this port.
1922 * Syntax:
1923 * void MoxaPortFlushData(int port, int mode);
1924 * int port : port number (0 - 127)
1925 * int mode
1926 * 0 : flush the Rx buffer
1927 * 1 : flush the Tx buffer
1928 * 2 : flush the Rx and Tx buffer
1931 * Function 20: Write data.
1932 * Syntax:
1933 * int MoxaPortWriteData(int port, unsigned char * buffer, int length);
1934 * int port : port number (0 - 127)
1935 * unsigned char * buffer : pointer to write data buffer.
1936 * int length : write data length
1938 * return: 0 - length : real write data length
1941 * Function 21: Read data.
1942 * Syntax:
1943 * int MoxaPortReadData(int port, struct tty_struct *tty);
1944 * int port : port number (0 - 127)
1945 * struct tty_struct *tty : tty for data
1947 * return: 0 - length : real read data length
1950 * Function 24: Get the Tx buffer current queued data bytes
1951 * Syntax:
1952 * int MoxaPortTxQueue(int port);
1953 * int port : port number (0 - 127)
1955 * return: .. : Tx buffer current queued data bytes
1958 * Function 25: Get the Tx buffer current free space
1959 * Syntax:
1960 * int MoxaPortTxFree(int port);
1961 * int port : port number (0 - 127)
1963 * return: .. : Tx buffer current free space
1966 * Function 26: Get the Rx buffer current queued data bytes
1967 * Syntax:
1968 * int MoxaPortRxQueue(int port);
1969 * int port : port number (0 - 127)
1971 * return: .. : Rx buffer current queued data bytes
1974 * Function 28: Disable port data transmission.
1975 * Syntax:
1976 * void MoxaPortTxDisable(int port);
1977 * int port : port number (0 - 127)
1980 * Function 29: Enable port data transmission.
1981 * Syntax:
1982 * void MoxaPortTxEnable(int port);
1983 * int port : port number (0 - 127)
1986 * Function 31: Get the received BREAK signal count and reset it.
1987 * Syntax:
1988 * int MoxaPortResetBrkCnt(int port);
1989 * int port : port number (0 - 127)
1991 * return: 0 - .. : BREAK signal count
1994 * Function 34: Send out a BREAK signal.
1995 * Syntax:
1996 * void MoxaPortSendBreak(int port, int ms100);
1997 * int port : port number (0 - 127)
1998 * int ms100 : break signal time interval.
1999 * unit: 100 mini-second. if ms100 == 0, it will
2000 * send out a about 250 ms BREAK signal.
2003 static int MoxaPortIsValid(int port)
2005 if (moxaCard == 0)
2006 return (0);
2007 if (moxa_ports[port].chkPort == 0)
2008 return (0);
2009 return (1);
2012 static void MoxaPortEnable(struct moxa_port *port)
2014 void __iomem *ofsAddr;
2015 short lowwater = 512;
2017 ofsAddr = port->tableAddr;
2018 writew(lowwater, ofsAddr + Low_water);
2019 port->breakCnt = 0;
2020 if (port->board->boardType == MOXA_BOARD_C320_ISA ||
2021 port->board->boardType == MOXA_BOARD_C320_PCI) {
2022 moxafunc(ofsAddr, FC_SetBreakIrq, 0);
2023 } else {
2024 writew(readw(ofsAddr + HostStat) | WakeupBreak, ofsAddr + HostStat);
2027 moxafunc(ofsAddr, FC_SetLineIrq, Magic_code);
2028 moxafunc(ofsAddr, FC_FlushQueue, 2);
2030 moxafunc(ofsAddr, FC_EnableCH, Magic_code);
2031 MoxaPortLineStatus(port);
2034 static void MoxaPortDisable(struct moxa_port *port)
2036 void __iomem *ofsAddr = port->tableAddr;
2038 moxafunc(ofsAddr, FC_SetFlowCtl, 0); /* disable flow control */
2039 moxafunc(ofsAddr, FC_ClrLineIrq, Magic_code);
2040 writew(0, ofsAddr + HostStat);
2041 moxafunc(ofsAddr, FC_DisableCH, Magic_code);
2044 static long MoxaPortGetMaxBaud(struct moxa_port *port)
2046 if (port->board->boardType == MOXA_BOARD_C320_ISA ||
2047 port->board->boardType == MOXA_BOARD_C320_PCI)
2048 return (460800L);
2049 else
2050 return (921600L);
2054 static long MoxaPortSetBaud(struct moxa_port *port, long baud)
2056 void __iomem *ofsAddr;
2057 long max, clock;
2058 unsigned int val;
2060 if ((baud < 50L) || ((max = MoxaPortGetMaxBaud(port)) == 0))
2061 return (0);
2062 ofsAddr = port->tableAddr;
2063 if (baud > max)
2064 baud = max;
2065 if (max == 38400L)
2066 clock = 614400L; /* for 9.8304 Mhz : max. 38400 bps */
2067 else if (max == 57600L)
2068 clock = 691200L; /* for 11.0592 Mhz : max. 57600 bps */
2069 else
2070 clock = 921600L; /* for 14.7456 Mhz : max. 115200 bps */
2071 val = clock / baud;
2072 moxafunc(ofsAddr, FC_SetBaud, val);
2073 baud = clock / val;
2074 return (baud);
2077 static int MoxaPortSetTermio(struct moxa_port *port, struct ktermios *termio,
2078 speed_t baud)
2080 void __iomem *ofsAddr;
2081 tcflag_t cflag;
2082 tcflag_t mode = 0;
2084 if (port->chkPort == 0 || termio == 0)
2085 return (-1);
2086 ofsAddr = port->tableAddr;
2087 cflag = termio->c_cflag; /* termio->c_cflag */
2089 mode = termio->c_cflag & CSIZE;
2090 if (mode == CS5)
2091 mode = MX_CS5;
2092 else if (mode == CS6)
2093 mode = MX_CS6;
2094 else if (mode == CS7)
2095 mode = MX_CS7;
2096 else if (mode == CS8)
2097 mode = MX_CS8;
2099 if (termio->c_cflag & CSTOPB) {
2100 if (mode == MX_CS5)
2101 mode |= MX_STOP15;
2102 else
2103 mode |= MX_STOP2;
2104 } else
2105 mode |= MX_STOP1;
2107 if (termio->c_cflag & PARENB) {
2108 if (termio->c_cflag & PARODD)
2109 mode |= MX_PARODD;
2110 else
2111 mode |= MX_PAREVEN;
2112 } else
2113 mode |= MX_PARNONE;
2115 moxafunc(ofsAddr, FC_SetDataMode, (ushort) mode);
2117 if (port->board->boardType == MOXA_BOARD_C320_ISA ||
2118 port->board->boardType == MOXA_BOARD_C320_PCI) {
2119 if (baud >= 921600L)
2120 return (-1);
2122 baud = MoxaPortSetBaud(port, baud);
2124 if (termio->c_iflag & (IXON | IXOFF | IXANY)) {
2125 writeb(termio->c_cc[VSTART], ofsAddr + FuncArg);
2126 writeb(termio->c_cc[VSTOP], ofsAddr + FuncArg1);
2127 writeb(FC_SetXonXoff, ofsAddr + FuncCode);
2128 moxa_wait_finish(ofsAddr);
2131 return (baud);
2134 static int MoxaPortGetLineOut(struct moxa_port *port, int *dtrState,
2135 int *rtsState)
2138 if (!MoxaPortIsValid(port->tty->index))
2139 return (-1);
2140 if (dtrState)
2141 *dtrState = !!(port->lineCtrl & DTR_ON);
2142 if (rtsState)
2143 *rtsState = !!(port->lineCtrl & RTS_ON);
2145 return (0);
2148 static void MoxaPortLineCtrl(struct moxa_port *port, int dtr, int rts)
2150 int mode = 0;
2152 if (dtr)
2153 mode |= DTR_ON;
2154 if (rts)
2155 mode |= RTS_ON;
2156 port->lineCtrl = mode;
2157 moxafunc(port->tableAddr, FC_LineControl, mode);
2160 static void MoxaPortFlowCtrl(struct moxa_port *port, int rts, int cts,
2161 int txflow, int rxflow, int txany)
2163 int mode = 0;
2165 if (rts)
2166 mode |= RTS_FlowCtl;
2167 if (cts)
2168 mode |= CTS_FlowCtl;
2169 if (txflow)
2170 mode |= Tx_FlowCtl;
2171 if (rxflow)
2172 mode |= Rx_FlowCtl;
2173 if (txany)
2174 mode |= IXM_IXANY;
2175 moxafunc(port->tableAddr, FC_SetFlowCtl, mode);
2178 static int MoxaPortLineStatus(struct moxa_port *port)
2180 void __iomem *ofsAddr;
2181 int val;
2183 ofsAddr = port->tableAddr;
2184 if (port->board->boardType == MOXA_BOARD_C320_ISA ||
2185 port->board->boardType == MOXA_BOARD_C320_PCI) {
2186 moxafunc(ofsAddr, FC_LineStatus, 0);
2187 val = readw(ofsAddr + FuncArg);
2188 } else {
2189 val = readw(ofsAddr + FlagStat) >> 4;
2191 val &= 0x0B;
2192 if (val & 8) {
2193 val |= 4;
2194 if ((port->DCDState & DCD_oldstate) == 0)
2195 port->DCDState = (DCD_oldstate | DCD_changed);
2196 } else {
2197 if (port->DCDState & DCD_oldstate)
2198 port->DCDState = DCD_changed;
2200 val &= 7;
2201 return (val);
2204 static int MoxaPortDCDChange(struct moxa_port *port)
2206 int n;
2208 if (port->chkPort == 0)
2209 return (0);
2210 n = port->DCDState;
2211 port->DCDState &= ~DCD_changed;
2212 n &= DCD_changed;
2213 return (n);
2216 static int MoxaPortDCDON(struct moxa_port *port)
2218 int n;
2220 if (port->chkPort == 0)
2221 return (0);
2222 if (port->DCDState & DCD_oldstate)
2223 n = 1;
2224 else
2225 n = 0;
2226 return (n);
2229 static int MoxaPortWriteData(struct moxa_port *port, unsigned char *buffer,
2230 int len)
2232 int c, total, i;
2233 ushort tail;
2234 int cnt;
2235 ushort head, tx_mask, spage, epage;
2236 ushort pageno, pageofs, bufhead;
2237 void __iomem *baseAddr, *ofsAddr, *ofs;
2239 ofsAddr = port->tableAddr;
2240 baseAddr = port->board->basemem;
2241 tx_mask = readw(ofsAddr + TX_mask);
2242 spage = readw(ofsAddr + Page_txb);
2243 epage = readw(ofsAddr + EndPage_txb);
2244 tail = readw(ofsAddr + TXwptr);
2245 head = readw(ofsAddr + TXrptr);
2246 c = (head > tail) ? (head - tail - 1)
2247 : (head - tail + tx_mask);
2248 if (c > len)
2249 c = len;
2250 moxaLog.txcnt[port->tty->index] += c;
2251 total = c;
2252 if (spage == epage) {
2253 bufhead = readw(ofsAddr + Ofs_txb);
2254 writew(spage, baseAddr + Control_reg);
2255 while (c > 0) {
2256 if (head > tail)
2257 len = head - tail - 1;
2258 else
2259 len = tx_mask + 1 - tail;
2260 len = (c > len) ? len : c;
2261 ofs = baseAddr + DynPage_addr + bufhead + tail;
2262 for (i = 0; i < len; i++)
2263 writeb(*buffer++, ofs + i);
2264 tail = (tail + len) & tx_mask;
2265 c -= len;
2267 writew(tail, ofsAddr + TXwptr);
2268 } else {
2269 len = c;
2270 pageno = spage + (tail >> 13);
2271 pageofs = tail & Page_mask;
2272 do {
2273 cnt = Page_size - pageofs;
2274 if (cnt > c)
2275 cnt = c;
2276 c -= cnt;
2277 writeb(pageno, baseAddr + Control_reg);
2278 ofs = baseAddr + DynPage_addr + pageofs;
2279 for (i = 0; i < cnt; i++)
2280 writeb(*buffer++, ofs + i);
2281 if (c == 0) {
2282 writew((tail + len) & tx_mask, ofsAddr + TXwptr);
2283 break;
2285 if (++pageno == epage)
2286 pageno = spage;
2287 pageofs = 0;
2288 } while (1);
2290 writeb(1, ofsAddr + CD180TXirq); /* start to send */
2291 return (total);
2294 static int MoxaPortReadData(struct moxa_port *port, struct tty_struct *tty)
2296 register ushort head, pageofs;
2297 int i, count, cnt, len, total, remain;
2298 ushort tail, rx_mask, spage, epage;
2299 ushort pageno, bufhead;
2300 void __iomem *baseAddr, *ofsAddr, *ofs;
2302 ofsAddr = port->tableAddr;
2303 baseAddr = port->board->basemem;
2304 head = readw(ofsAddr + RXrptr);
2305 tail = readw(ofsAddr + RXwptr);
2306 rx_mask = readw(ofsAddr + RX_mask);
2307 spage = readw(ofsAddr + Page_rxb);
2308 epage = readw(ofsAddr + EndPage_rxb);
2309 count = (tail >= head) ? (tail - head)
2310 : (tail - head + rx_mask + 1);
2311 if (count == 0)
2312 return 0;
2314 total = count;
2315 remain = count - total;
2316 moxaLog.rxcnt[port->tty->index] += total;
2317 count = total;
2318 if (spage == epage) {
2319 bufhead = readw(ofsAddr + Ofs_rxb);
2320 writew(spage, baseAddr + Control_reg);
2321 while (count > 0) {
2322 if (tail >= head)
2323 len = tail - head;
2324 else
2325 len = rx_mask + 1 - head;
2326 len = (count > len) ? len : count;
2327 ofs = baseAddr + DynPage_addr + bufhead + head;
2328 for (i = 0; i < len; i++)
2329 tty_insert_flip_char(tty, readb(ofs + i), TTY_NORMAL);
2330 head = (head + len) & rx_mask;
2331 count -= len;
2333 writew(head, ofsAddr + RXrptr);
2334 } else {
2335 len = count;
2336 pageno = spage + (head >> 13);
2337 pageofs = head & Page_mask;
2338 do {
2339 cnt = Page_size - pageofs;
2340 if (cnt > count)
2341 cnt = count;
2342 count -= cnt;
2343 writew(pageno, baseAddr + Control_reg);
2344 ofs = baseAddr + DynPage_addr + pageofs;
2345 for (i = 0; i < cnt; i++)
2346 tty_insert_flip_char(tty, readb(ofs + i), TTY_NORMAL);
2347 if (count == 0) {
2348 writew((head + len) & rx_mask, ofsAddr + RXrptr);
2349 break;
2351 if (++pageno == epage)
2352 pageno = spage;
2353 pageofs = 0;
2354 } while (1);
2356 if ((readb(ofsAddr + FlagStat) & Xoff_state) && (remain < LowWater)) {
2357 moxaLowWaterChk = 1;
2358 port->lowChkFlag = 1;
2360 return (total);
2364 static int MoxaPortTxQueue(struct moxa_port *port)
2366 void __iomem *ofsAddr = port->tableAddr;
2367 ushort rptr, wptr, mask;
2368 int len;
2370 rptr = readw(ofsAddr + TXrptr);
2371 wptr = readw(ofsAddr + TXwptr);
2372 mask = readw(ofsAddr + TX_mask);
2373 len = (wptr - rptr) & mask;
2374 return (len);
2377 static int MoxaPortTxFree(struct moxa_port *port)
2379 void __iomem *ofsAddr = port->tableAddr;
2380 ushort rptr, wptr, mask;
2381 int len;
2383 rptr = readw(ofsAddr + TXrptr);
2384 wptr = readw(ofsAddr + TXwptr);
2385 mask = readw(ofsAddr + TX_mask);
2386 len = mask - ((wptr - rptr) & mask);
2387 return (len);
2390 static int MoxaPortRxQueue(struct moxa_port *port)
2392 void __iomem *ofsAddr = port->tableAddr;
2393 ushort rptr, wptr, mask;
2394 int len;
2396 rptr = readw(ofsAddr + RXrptr);
2397 wptr = readw(ofsAddr + RXwptr);
2398 mask = readw(ofsAddr + RX_mask);
2399 len = (wptr - rptr) & mask;
2400 return (len);
2404 static void MoxaPortTxDisable(struct moxa_port *port)
2406 moxafunc(port->tableAddr, FC_SetXoffState, Magic_code);
2409 static void MoxaPortTxEnable(struct moxa_port *port)
2411 moxafunc(port->tableAddr, FC_SetXonState, Magic_code);
2415 static int MoxaPortResetBrkCnt(struct moxa_port *port)
2417 ushort cnt;
2418 cnt = port->breakCnt;
2419 port->breakCnt = 0;
2420 return (cnt);
2424 static void MoxaPortSendBreak(struct moxa_port *port, int ms100)
2426 void __iomem *ofsAddr = port->tableAddr;
2428 if (ms100) {
2429 moxafunc(ofsAddr, FC_SendBreak, Magic_code);
2430 msleep(ms100 * 10);
2431 } else {
2432 moxafunc(ofsAddr, FC_SendBreak, Magic_code);
2433 msleep(250);
2435 moxafunc(ofsAddr, FC_StopBreak, Magic_code);
2438 static int moxa_get_serial_info(struct moxa_port *info,
2439 struct serial_struct __user *retinfo)
2441 struct serial_struct tmp;
2443 memset(&tmp, 0, sizeof(tmp));
2444 tmp.type = info->type;
2445 tmp.line = info->tty->index;
2446 tmp.port = 0;
2447 tmp.irq = 0;
2448 tmp.flags = info->asyncflags;
2449 tmp.baud_base = 921600;
2450 tmp.close_delay = info->close_delay;
2451 tmp.custom_divisor = 0;
2452 tmp.hub6 = 0;
2453 if(copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2454 return -EFAULT;
2455 return (0);
2459 static int moxa_set_serial_info(struct moxa_port *info,
2460 struct serial_struct __user *new_info)
2462 struct serial_struct new_serial;
2464 if(copy_from_user(&new_serial, new_info, sizeof(new_serial)))
2465 return -EFAULT;
2467 if ((new_serial.irq != 0) ||
2468 (new_serial.port != 0) ||
2469 // (new_serial.type != info->type) ||
2470 (new_serial.custom_divisor != 0) ||
2471 (new_serial.baud_base != 921600))
2472 return (-EPERM);
2474 if (!capable(CAP_SYS_ADMIN)) {
2475 if (((new_serial.flags & ~ASYNC_USR_MASK) !=
2476 (info->asyncflags & ~ASYNC_USR_MASK)))
2477 return (-EPERM);
2478 } else {
2479 info->close_delay = new_serial.close_delay * HZ / 100;
2482 new_serial.flags = (new_serial.flags & ~ASYNC_FLAGS);
2483 new_serial.flags |= (info->asyncflags & ASYNC_FLAGS);
2485 if (new_serial.type == PORT_16550A) {
2486 MoxaSetFifo(info, 1);
2487 } else {
2488 MoxaSetFifo(info, 0);
2491 info->type = new_serial.type;
2492 return (0);
2497 /*****************************************************************************
2498 * Static local functions: *
2499 *****************************************************************************/
2500 static void moxafunc(void __iomem *ofsAddr, int cmd, ushort arg)
2503 writew(arg, ofsAddr + FuncArg);
2504 writew(cmd, ofsAddr + FuncCode);
2505 moxa_wait_finish(ofsAddr);
2508 static void moxa_wait_finish(void __iomem *ofsAddr)
2510 unsigned long i, j;
2512 i = jiffies;
2513 while (readw(ofsAddr + FuncCode) != 0) {
2514 j = jiffies;
2515 if ((j - i) > moxaFuncTout) {
2516 return;
2521 static void moxa_low_water_check(void __iomem *ofsAddr)
2523 int len;
2524 ushort rptr, wptr, mask;
2526 if (readb(ofsAddr + FlagStat) & Xoff_state) {
2527 rptr = readw(ofsAddr + RXrptr);
2528 wptr = readw(ofsAddr + RXwptr);
2529 mask = readw(ofsAddr + RX_mask);
2530 len = (wptr - rptr) & mask;
2531 if (len <= Low_water)
2532 moxafunc(ofsAddr, FC_SendXon, 0);
2536 static void MoxaSetFifo(struct moxa_port *port, int enable)
2538 void __iomem *ofsAddr = port->tableAddr;
2540 if (!enable) {
2541 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 0);
2542 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 1);
2543 } else {
2544 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 3);
2545 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 16);