moxa.c revision 2a5413416b6b2fd8a5a38601a4fe3b56a52cfb86
1/*****************************************************************************/ 2/* 3 * moxa.c -- MOXA Intellio family multiport serial driver. 4 * 5 * Copyright (C) 1999-2000 Moxa Technologies (support@moxa.com.tw). 6 * 7 * This code is loosely based on the Linux serial driver, written by 8 * Linus Torvalds, Theodore T'so and others. 9 * 10 * This program is free software; you can redistribute it and/or modify 11 * it under the terms of the GNU General Public License as published by 12 * the Free Software Foundation; either version 2 of the License, or 13 * (at your option) any later version. 14 */ 15 16/* 17 * MOXA Intellio Series Driver 18 * for : LINUX 19 * date : 1999/1/7 20 * version : 5.1 21 */ 22 23#include <linux/module.h> 24#include <linux/types.h> 25#include <linux/mm.h> 26#include <linux/ioport.h> 27#include <linux/errno.h> 28#include <linux/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> 46 47#include <asm/system.h> 48#include <asm/io.h> 49#include <asm/uaccess.h> 50 51#include "moxa.h" 52 53#define MOXA_VERSION "5.1k" 54 55#define MOXA_FW_HDRLEN 32 56 57#define MOXAMAJOR 172 58#define MOXACUMAJOR 173 59 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) 63 64/* 65 * Define the Moxa PCI vendor and device IDs. 66 */ 67#define MOXA_BUS_TYPE_ISA 0 68#define MOXA_BUS_TYPE_PCI 1 69 70enum { 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, 76}; 77 78static char *moxa_brdname[] = 79{ 80 "C218 Turbo PCI series", 81 "C218 Turbo ISA series", 82 "C320 Turbo PCI series", 83 "C320 Turbo ISA series", 84 "CP-204J series", 85}; 86 87#ifdef CONFIG_PCI 88static 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 } 96}; 97MODULE_DEVICE_TABLE(pci, moxa_pcibrds); 98#endif /* CONFIG_PCI */ 99 100struct moxa_port; 101 102static struct moxa_board_conf { 103 int boardType; 104 int numPorts; 105 int busType; 106 107 unsigned int ready; 108 109 struct moxa_port *ports; 110 111 void __iomem *basemem; 112 void __iomem *intNdx; 113 void __iomem *intPend; 114 void __iomem *intTable; 115} moxa_boards[MAX_BOARDS]; 116 117struct mxser_mstatus { 118 tcflag_t cflag; 119 int cts; 120 int dsr; 121 int ri; 122 int dcd; 123}; 124 125struct moxaq_str { 126 int inq; 127 int outq; 128}; 129 130struct moxa_port { 131 struct moxa_board_conf *board; 132 struct tty_struct *tty; 133 void __iomem *tableAddr; 134 135 int type; 136 int close_delay; 137 int count; 138 int blocked_open; 139 int asyncflags; 140 int cflag; 141 unsigned long statusflags; 142 wait_queue_head_t open_wait; 143 struct completion close_wait; 144 145 u8 DCDState; 146 u8 lineCtrl; 147 u8 lowChkFlag; 148}; 149 150struct mon_str { 151 int tick; 152 int rxcnt[MAX_PORTS]; 153 int txcnt[MAX_PORTS]; 154}; 155 156/* statusflags */ 157#define TXSTOPPED 0x1 158#define LOWWAIT 0x2 159#define EMPTYWAIT 0x4 160#define THROTTLE 0x8 161 162#define SERIAL_DO_RESTART 163 164#define WAKEUP_CHARS 256 165 166static int ttymajor = MOXAMAJOR; 167static struct mon_str moxaLog; 168static unsigned int moxaFuncTout = HZ / 2; 169static unsigned int moxaLowWaterChk; 170/* Variables for insmod */ 171#ifdef MODULE 172static unsigned long baseaddr[MAX_BOARDS]; 173static unsigned int type[MAX_BOARDS]; 174static unsigned int numports[MAX_BOARDS]; 175#endif 176 177MODULE_AUTHOR("William Chen"); 178MODULE_DESCRIPTION("MOXA Intellio Family Multiport Board Device Driver"); 179MODULE_LICENSE("GPL"); 180#ifdef MODULE 181module_param_array(type, uint, NULL, 0); 182MODULE_PARM_DESC(type, "card type: C218=2, C320=4"); 183module_param_array(baseaddr, ulong, NULL, 0); 184MODULE_PARM_DESC(baseaddr, "base address"); 185module_param_array(numports, uint, NULL, 0); 186MODULE_PARM_DESC(numports, "numports (ignored for C218)"); 187#endif 188module_param(ttymajor, int, 0); 189 190/* 191 * static functions: 192 */ 193static int moxa_open(struct tty_struct *, struct file *); 194static void moxa_close(struct tty_struct *, struct file *); 195static int moxa_write(struct tty_struct *, const unsigned char *, int); 196static int moxa_write_room(struct tty_struct *); 197static void moxa_flush_buffer(struct tty_struct *); 198static int moxa_chars_in_buffer(struct tty_struct *); 199static void moxa_flush_chars(struct tty_struct *); 200static void moxa_put_char(struct tty_struct *, unsigned char); 201static void moxa_throttle(struct tty_struct *); 202static void moxa_unthrottle(struct tty_struct *); 203static void moxa_set_termios(struct tty_struct *, struct ktermios *); 204static void moxa_stop(struct tty_struct *); 205static void moxa_start(struct tty_struct *); 206static void moxa_hangup(struct tty_struct *); 207static int moxa_tiocmget(struct tty_struct *tty, struct file *file); 208static int moxa_tiocmset(struct tty_struct *tty, struct file *file, 209 unsigned int set, unsigned int clear); 210static void moxa_poll(unsigned long); 211static void moxa_set_tty_param(struct tty_struct *, struct ktermios *); 212static int moxa_block_till_ready(struct tty_struct *, struct file *, 213 struct moxa_port *); 214static void moxa_setup_empty_event(struct tty_struct *); 215static void moxa_shut_down(struct moxa_port *); 216/* 217 * moxa board interface functions: 218 */ 219static void MoxaPortEnable(struct moxa_port *); 220static void MoxaPortDisable(struct moxa_port *); 221static int MoxaPortSetTermio(struct moxa_port *, struct ktermios *, speed_t); 222static int MoxaPortGetLineOut(struct moxa_port *, int *, int *); 223static void MoxaPortLineCtrl(struct moxa_port *, int, int); 224static void MoxaPortFlowCtrl(struct moxa_port *, int, int, int, int, int); 225static int MoxaPortLineStatus(struct moxa_port *); 226static void MoxaPortFlushData(struct moxa_port *, int); 227static int MoxaPortWriteData(struct moxa_port *, const unsigned char *, int); 228static int MoxaPortReadData(struct moxa_port *); 229static int MoxaPortTxQueue(struct moxa_port *); 230static int MoxaPortRxQueue(struct moxa_port *); 231static int MoxaPortTxFree(struct moxa_port *); 232static void MoxaPortTxDisable(struct moxa_port *); 233static void MoxaPortTxEnable(struct moxa_port *); 234static int moxa_get_serial_info(struct moxa_port *, struct serial_struct __user *); 235static int moxa_set_serial_info(struct moxa_port *, struct serial_struct __user *); 236static void MoxaSetFifo(struct moxa_port *port, int enable); 237 238/* 239 * I/O functions 240 */ 241 242static void moxa_wait_finish(void __iomem *ofsAddr) 243{ 244 unsigned long end = jiffies + moxaFuncTout; 245 246 while (readw(ofsAddr + FuncCode) != 0) 247 if (time_after(jiffies, end)) 248 return; 249 if (readw(ofsAddr + FuncCode) != 0 && printk_ratelimit()) 250 printk(KERN_WARNING "moxa function expired\n"); 251} 252 253static void moxafunc(void __iomem *ofsAddr, int cmd, ushort arg) 254{ 255 writew(arg, ofsAddr + FuncArg); 256 writew(cmd, ofsAddr + FuncCode); 257 moxa_wait_finish(ofsAddr); 258} 259 260static void moxa_low_water_check(void __iomem *ofsAddr) 261{ 262 u16 rptr, wptr, mask, len; 263 264 if (readb(ofsAddr + FlagStat) & Xoff_state) { 265 rptr = readw(ofsAddr + RXrptr); 266 wptr = readw(ofsAddr + RXwptr); 267 mask = readw(ofsAddr + RX_mask); 268 len = (wptr - rptr) & mask; 269 if (len <= Low_water) 270 moxafunc(ofsAddr, FC_SendXon, 0); 271 } 272} 273 274/* 275 * TTY operations 276 */ 277 278static int moxa_ioctl(struct tty_struct *tty, struct file *file, 279 unsigned int cmd, unsigned long arg) 280{ 281 struct moxa_port *ch = tty->driver_data; 282 void __user *argp = (void __user *)arg; 283 int status; 284 285 if (tty->index == MAX_PORTS) { 286 if (cmd != MOXA_GETDATACOUNT && cmd != MOXA_GET_IOQUEUE && 287 cmd != MOXA_GETMSTATUS) 288 return -EINVAL; 289 } else if (!ch) 290 return -ENODEV; 291 292 switch (cmd) { 293 case MOXA_GETDATACOUNT: 294 moxaLog.tick = jiffies; 295 return copy_to_user(argp, &moxaLog, sizeof(moxaLog)) ? 296 -EFAULT : 0; 297 case MOXA_FLUSH_QUEUE: 298 MoxaPortFlushData(ch, arg); 299 return 0; 300 case MOXA_GET_IOQUEUE: { 301 struct moxaq_str __user *argm = argp; 302 struct moxaq_str tmp; 303 struct moxa_port *p; 304 unsigned int i, j; 305 306 for (i = 0; i < MAX_BOARDS; i++) { 307 p = moxa_boards[i].ports; 308 for (j = 0; j < MAX_PORTS_PER_BOARD; j++, p++, argm++) { 309 memset(&tmp, 0, sizeof(tmp)); 310 if (moxa_boards[i].ready) { 311 tmp.inq = MoxaPortRxQueue(p); 312 tmp.outq = MoxaPortTxQueue(p); 313 } 314 if (copy_to_user(argm, &tmp, sizeof(tmp))) 315 return -EFAULT; 316 } 317 } 318 return 0; 319 } case MOXA_GET_OQUEUE: 320 status = MoxaPortTxQueue(ch); 321 return put_user(status, (unsigned long __user *)argp); 322 case MOXA_GET_IQUEUE: 323 status = MoxaPortRxQueue(ch); 324 return put_user(status, (unsigned long __user *)argp); 325 case MOXA_GETMSTATUS: { 326 struct mxser_mstatus __user *argm = argp; 327 struct mxser_mstatus tmp; 328 struct moxa_port *p; 329 unsigned int i, j; 330 331 for (i = 0; i < MAX_BOARDS; i++) { 332 p = moxa_boards[i].ports; 333 for (j = 0; j < MAX_PORTS_PER_BOARD; j++, p++, argm++) { 334 memset(&tmp, 0, sizeof(tmp)); 335 if (!moxa_boards[i].ready) 336 goto copy; 337 338 status = MoxaPortLineStatus(p); 339 if (status & 1) 340 tmp.cts = 1; 341 if (status & 2) 342 tmp.dsr = 1; 343 if (status & 4) 344 tmp.dcd = 1; 345 346 if (!p->tty || !p->tty->termios) 347 tmp.cflag = p->cflag; 348 else 349 tmp.cflag = p->tty->termios->c_cflag; 350copy: 351 if (copy_to_user(argm, &tmp, sizeof(tmp))) 352 return -EFAULT; 353 } 354 } 355 return 0; 356 } 357 case TIOCGSERIAL: 358 return moxa_get_serial_info(ch, argp); 359 case TIOCSSERIAL: 360 return moxa_set_serial_info(ch, argp); 361 } 362 return -ENOIOCTLCMD; 363} 364 365static void moxa_break_ctl(struct tty_struct *tty, int state) 366{ 367 struct moxa_port *port = tty->driver_data; 368 369 moxafunc(port->tableAddr, state ? FC_SendBreak : FC_StopBreak, 370 Magic_code); 371} 372 373static const struct tty_operations moxa_ops = { 374 .open = moxa_open, 375 .close = moxa_close, 376 .write = moxa_write, 377 .write_room = moxa_write_room, 378 .flush_buffer = moxa_flush_buffer, 379 .chars_in_buffer = moxa_chars_in_buffer, 380 .flush_chars = moxa_flush_chars, 381 .put_char = moxa_put_char, 382 .ioctl = moxa_ioctl, 383 .throttle = moxa_throttle, 384 .unthrottle = moxa_unthrottle, 385 .set_termios = moxa_set_termios, 386 .stop = moxa_stop, 387 .start = moxa_start, 388 .hangup = moxa_hangup, 389 .break_ctl = moxa_break_ctl, 390 .tiocmget = moxa_tiocmget, 391 .tiocmset = moxa_tiocmset, 392}; 393 394static struct tty_driver *moxaDriver; 395static DEFINE_TIMER(moxaTimer, moxa_poll, 0, 0); 396static DEFINE_SPINLOCK(moxa_lock); 397 398/* 399 * HW init 400 */ 401 402static int moxa_check_fw_model(struct moxa_board_conf *brd, u8 model) 403{ 404 switch (brd->boardType) { 405 case MOXA_BOARD_C218_ISA: 406 case MOXA_BOARD_C218_PCI: 407 if (model != 1) 408 goto err; 409 break; 410 case MOXA_BOARD_CP204J: 411 if (model != 3) 412 goto err; 413 break; 414 default: 415 if (model != 2) 416 goto err; 417 break; 418 } 419 return 0; 420err: 421 return -EINVAL; 422} 423 424static int moxa_check_fw(const void *ptr) 425{ 426 const __le16 *lptr = ptr; 427 428 if (*lptr != cpu_to_le16(0x7980)) 429 return -EINVAL; 430 431 return 0; 432} 433 434static int moxa_load_bios(struct moxa_board_conf *brd, const u8 *buf, 435 size_t len) 436{ 437 void __iomem *baseAddr = brd->basemem; 438 u16 tmp; 439 440 writeb(HW_reset, baseAddr + Control_reg); /* reset */ 441 msleep(10); 442 memset_io(baseAddr, 0, 4096); 443 memcpy_toio(baseAddr, buf, len); /* download BIOS */ 444 writeb(0, baseAddr + Control_reg); /* restart */ 445 446 msleep(2000); 447 448 switch (brd->boardType) { 449 case MOXA_BOARD_C218_ISA: 450 case MOXA_BOARD_C218_PCI: 451 tmp = readw(baseAddr + C218_key); 452 if (tmp != C218_KeyCode) 453 goto err; 454 break; 455 case MOXA_BOARD_CP204J: 456 tmp = readw(baseAddr + C218_key); 457 if (tmp != CP204J_KeyCode) 458 goto err; 459 break; 460 default: 461 tmp = readw(baseAddr + C320_key); 462 if (tmp != C320_KeyCode) 463 goto err; 464 tmp = readw(baseAddr + C320_status); 465 if (tmp != STS_init) { 466 printk(KERN_ERR "moxa: bios upload failed -- CPU/Basic " 467 "module not found\n"); 468 return -EIO; 469 } 470 break; 471 } 472 473 return 0; 474err: 475 printk(KERN_ERR "moxa: bios upload failed -- board not found\n"); 476 return -EIO; 477} 478 479static int moxa_load_320b(struct moxa_board_conf *brd, const u8 *ptr, 480 size_t len) 481{ 482 void __iomem *baseAddr = brd->basemem; 483 484 if (len < 7168) { 485 printk(KERN_ERR "moxa: invalid 320 bios -- too short\n"); 486 return -EINVAL; 487 } 488 489 writew(len - 7168 - 2, baseAddr + C320bapi_len); 490 writeb(1, baseAddr + Control_reg); /* Select Page 1 */ 491 memcpy_toio(baseAddr + DynPage_addr, ptr, 7168); 492 writeb(2, baseAddr + Control_reg); /* Select Page 2 */ 493 memcpy_toio(baseAddr + DynPage_addr, ptr + 7168, len - 7168); 494 495 return 0; 496} 497 498static int moxa_real_load_code(struct moxa_board_conf *brd, const void *ptr, 499 size_t len) 500{ 501 void __iomem *baseAddr = brd->basemem; 502 const u16 *uptr = ptr; 503 size_t wlen, len2, j; 504 unsigned long key, loadbuf, loadlen, checksum, checksum_ok; 505 unsigned int i, retry, c320; 506 u16 usum, keycode; 507 508 c320 = brd->boardType == MOXA_BOARD_C320_PCI || 509 brd->boardType == MOXA_BOARD_C320_ISA; 510 keycode = (brd->boardType == MOXA_BOARD_CP204J) ? CP204J_KeyCode : 511 C218_KeyCode; 512 513 switch (brd->boardType) { 514 case MOXA_BOARD_CP204J: 515 case MOXA_BOARD_C218_ISA: 516 case MOXA_BOARD_C218_PCI: 517 key = C218_key; 518 loadbuf = C218_LoadBuf; 519 loadlen = C218DLoad_len; 520 checksum = C218check_sum; 521 checksum_ok = C218chksum_ok; 522 break; 523 default: 524 key = C320_key; 525 keycode = C320_KeyCode; 526 loadbuf = C320_LoadBuf; 527 loadlen = C320DLoad_len; 528 checksum = C320check_sum; 529 checksum_ok = C320chksum_ok; 530 break; 531 } 532 533 usum = 0; 534 wlen = len >> 1; 535 for (i = 0; i < wlen; i++) 536 usum += le16_to_cpu(uptr[i]); 537 retry = 0; 538 do { 539 wlen = len >> 1; 540 j = 0; 541 while (wlen) { 542 len2 = (wlen > 2048) ? 2048 : wlen; 543 wlen -= len2; 544 memcpy_toio(baseAddr + loadbuf, ptr + j, len2 << 1); 545 j += len2 << 1; 546 547 writew(len2, baseAddr + loadlen); 548 writew(0, baseAddr + key); 549 for (i = 0; i < 100; i++) { 550 if (readw(baseAddr + key) == keycode) 551 break; 552 msleep(10); 553 } 554 if (readw(baseAddr + key) != keycode) 555 return -EIO; 556 } 557 writew(0, baseAddr + loadlen); 558 writew(usum, baseAddr + checksum); 559 writew(0, baseAddr + key); 560 for (i = 0; i < 100; i++) { 561 if (readw(baseAddr + key) == keycode) 562 break; 563 msleep(10); 564 } 565 retry++; 566 } while ((readb(baseAddr + checksum_ok) != 1) && (retry < 3)); 567 if (readb(baseAddr + checksum_ok) != 1) 568 return -EIO; 569 570 writew(0, baseAddr + key); 571 for (i = 0; i < 600; i++) { 572 if (readw(baseAddr + Magic_no) == Magic_code) 573 break; 574 msleep(10); 575 } 576 if (readw(baseAddr + Magic_no) != Magic_code) 577 return -EIO; 578 579 if (c320) { 580 if (brd->busType == MOXA_BUS_TYPE_PCI) { /* ASIC board */ 581 writew(0x3800, baseAddr + TMS320_PORT1); 582 writew(0x3900, baseAddr + TMS320_PORT2); 583 writew(28499, baseAddr + TMS320_CLOCK); 584 } else { 585 writew(0x3200, baseAddr + TMS320_PORT1); 586 writew(0x3400, baseAddr + TMS320_PORT2); 587 writew(19999, baseAddr + TMS320_CLOCK); 588 } 589 } 590 writew(1, baseAddr + Disable_IRQ); 591 writew(0, baseAddr + Magic_no); 592 for (i = 0; i < 500; i++) { 593 if (readw(baseAddr + Magic_no) == Magic_code) 594 break; 595 msleep(10); 596 } 597 if (readw(baseAddr + Magic_no) != Magic_code) 598 return -EIO; 599 600 if (c320) { 601 j = readw(baseAddr + Module_cnt); 602 if (j <= 0) 603 return -EIO; 604 brd->numPorts = j * 8; 605 writew(j, baseAddr + Module_no); 606 writew(0, baseAddr + Magic_no); 607 for (i = 0; i < 600; i++) { 608 if (readw(baseAddr + Magic_no) == Magic_code) 609 break; 610 msleep(10); 611 } 612 if (readw(baseAddr + Magic_no) != Magic_code) 613 return -EIO; 614 } 615 brd->intNdx = baseAddr + IRQindex; 616 brd->intPend = baseAddr + IRQpending; 617 brd->intTable = baseAddr + IRQtable; 618 619 return 0; 620} 621 622static int moxa_load_code(struct moxa_board_conf *brd, const void *ptr, 623 size_t len) 624{ 625 void __iomem *ofsAddr, *baseAddr = brd->basemem; 626 struct moxa_port *port; 627 int retval, i; 628 629 if (len % 2) { 630 printk(KERN_ERR "moxa: bios length is not even\n"); 631 return -EINVAL; 632 } 633 634 retval = moxa_real_load_code(brd, ptr, len); /* may change numPorts */ 635 if (retval) 636 return retval; 637 638 switch (brd->boardType) { 639 case MOXA_BOARD_C218_ISA: 640 case MOXA_BOARD_C218_PCI: 641 case MOXA_BOARD_CP204J: 642 port = brd->ports; 643 for (i = 0; i < brd->numPorts; i++, port++) { 644 port->board = brd; 645 port->DCDState = 0; 646 port->tableAddr = baseAddr + Extern_table + 647 Extern_size * i; 648 ofsAddr = port->tableAddr; 649 writew(C218rx_mask, ofsAddr + RX_mask); 650 writew(C218tx_mask, ofsAddr + TX_mask); 651 writew(C218rx_spage + i * C218buf_pageno, ofsAddr + Page_rxb); 652 writew(readw(ofsAddr + Page_rxb) + C218rx_pageno, ofsAddr + EndPage_rxb); 653 654 writew(C218tx_spage + i * C218buf_pageno, ofsAddr + Page_txb); 655 writew(readw(ofsAddr + Page_txb) + C218tx_pageno, ofsAddr + EndPage_txb); 656 657 } 658 break; 659 default: 660 port = brd->ports; 661 for (i = 0; i < brd->numPorts; i++, port++) { 662 port->board = brd; 663 port->DCDState = 0; 664 port->tableAddr = baseAddr + Extern_table + 665 Extern_size * i; 666 ofsAddr = port->tableAddr; 667 switch (brd->numPorts) { 668 case 8: 669 writew(C320p8rx_mask, ofsAddr + RX_mask); 670 writew(C320p8tx_mask, ofsAddr + TX_mask); 671 writew(C320p8rx_spage + i * C320p8buf_pgno, ofsAddr + Page_rxb); 672 writew(readw(ofsAddr + Page_rxb) + C320p8rx_pgno, ofsAddr + EndPage_rxb); 673 writew(C320p8tx_spage + i * C320p8buf_pgno, ofsAddr + Page_txb); 674 writew(readw(ofsAddr + Page_txb) + C320p8tx_pgno, ofsAddr + EndPage_txb); 675 676 break; 677 case 16: 678 writew(C320p16rx_mask, ofsAddr + RX_mask); 679 writew(C320p16tx_mask, ofsAddr + TX_mask); 680 writew(C320p16rx_spage + i * C320p16buf_pgno, ofsAddr + Page_rxb); 681 writew(readw(ofsAddr + Page_rxb) + C320p16rx_pgno, ofsAddr + EndPage_rxb); 682 writew(C320p16tx_spage + i * C320p16buf_pgno, ofsAddr + Page_txb); 683 writew(readw(ofsAddr + Page_txb) + C320p16tx_pgno, ofsAddr + EndPage_txb); 684 break; 685 686 case 24: 687 writew(C320p24rx_mask, ofsAddr + RX_mask); 688 writew(C320p24tx_mask, ofsAddr + TX_mask); 689 writew(C320p24rx_spage + i * C320p24buf_pgno, ofsAddr + Page_rxb); 690 writew(readw(ofsAddr + Page_rxb) + C320p24rx_pgno, ofsAddr + EndPage_rxb); 691 writew(C320p24tx_spage + i * C320p24buf_pgno, ofsAddr + Page_txb); 692 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb); 693 break; 694 case 32: 695 writew(C320p32rx_mask, ofsAddr + RX_mask); 696 writew(C320p32tx_mask, ofsAddr + TX_mask); 697 writew(C320p32tx_ofs, ofsAddr + Ofs_txb); 698 writew(C320p32rx_spage + i * C320p32buf_pgno, ofsAddr + Page_rxb); 699 writew(readb(ofsAddr + Page_rxb), ofsAddr + EndPage_rxb); 700 writew(C320p32tx_spage + i * C320p32buf_pgno, ofsAddr + Page_txb); 701 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb); 702 break; 703 } 704 } 705 break; 706 } 707 return 0; 708} 709 710static int moxa_load_fw(struct moxa_board_conf *brd, const struct firmware *fw) 711{ 712 void *ptr = fw->data; 713 char rsn[64]; 714 u16 lens[5]; 715 size_t len; 716 unsigned int a, lenp, lencnt; 717 int ret = -EINVAL; 718 struct { 719 __le32 magic; /* 0x34303430 */ 720 u8 reserved1[2]; 721 u8 type; /* UNIX = 3 */ 722 u8 model; /* C218T=1, C320T=2, CP204=3 */ 723 u8 reserved2[8]; 724 __le16 len[5]; 725 } *hdr = ptr; 726 727 BUILD_BUG_ON(ARRAY_SIZE(hdr->len) != ARRAY_SIZE(lens)); 728 729 if (fw->size < MOXA_FW_HDRLEN) { 730 strcpy(rsn, "too short (even header won't fit)"); 731 goto err; 732 } 733 if (hdr->magic != cpu_to_le32(0x30343034)) { 734 sprintf(rsn, "bad magic: %.8x", le32_to_cpu(hdr->magic)); 735 goto err; 736 } 737 if (hdr->type != 3) { 738 sprintf(rsn, "not for linux, type is %u", hdr->type); 739 goto err; 740 } 741 if (moxa_check_fw_model(brd, hdr->model)) { 742 sprintf(rsn, "not for this card, model is %u", hdr->model); 743 goto err; 744 } 745 746 len = MOXA_FW_HDRLEN; 747 lencnt = hdr->model == 2 ? 5 : 3; 748 for (a = 0; a < ARRAY_SIZE(lens); a++) { 749 lens[a] = le16_to_cpu(hdr->len[a]); 750 if (lens[a] && len + lens[a] <= fw->size && 751 moxa_check_fw(&fw->data[len])) 752 printk(KERN_WARNING "moxa firmware: unexpected input " 753 "at offset %u, but going on\n", (u32)len); 754 if (!lens[a] && a < lencnt) { 755 sprintf(rsn, "too few entries in fw file"); 756 goto err; 757 } 758 len += lens[a]; 759 } 760 761 if (len != fw->size) { 762 sprintf(rsn, "bad length: %u (should be %u)", (u32)fw->size, 763 (u32)len); 764 goto err; 765 } 766 767 ptr += MOXA_FW_HDRLEN; 768 lenp = 0; /* bios */ 769 770 strcpy(rsn, "read above"); 771 772 ret = moxa_load_bios(brd, ptr, lens[lenp]); 773 if (ret) 774 goto err; 775 776 /* we skip the tty section (lens[1]), since we don't need it */ 777 ptr += lens[lenp] + lens[lenp + 1]; 778 lenp += 2; /* comm */ 779 780 if (hdr->model == 2) { 781 ret = moxa_load_320b(brd, ptr, lens[lenp]); 782 if (ret) 783 goto err; 784 /* skip another tty */ 785 ptr += lens[lenp] + lens[lenp + 1]; 786 lenp += 2; 787 } 788 789 ret = moxa_load_code(brd, ptr, lens[lenp]); 790 if (ret) 791 goto err; 792 793 return 0; 794err: 795 printk(KERN_ERR "firmware failed to load, reason: %s\n", rsn); 796 return ret; 797} 798 799static int moxa_init_board(struct moxa_board_conf *brd, struct device *dev) 800{ 801 const struct firmware *fw; 802 const char *file; 803 struct moxa_port *p; 804 unsigned int i; 805 int ret; 806 807 brd->ports = kcalloc(MAX_PORTS_PER_BOARD, sizeof(*brd->ports), 808 GFP_KERNEL); 809 if (brd->ports == NULL) { 810 printk(KERN_ERR "cannot allocate memory for ports\n"); 811 ret = -ENOMEM; 812 goto err; 813 } 814 815 for (i = 0, p = brd->ports; i < MAX_PORTS_PER_BOARD; i++, p++) { 816 p->type = PORT_16550A; 817 p->close_delay = 5 * HZ / 10; 818 p->cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL; 819 init_waitqueue_head(&p->open_wait); 820 init_completion(&p->close_wait); 821 } 822 823 switch (brd->boardType) { 824 case MOXA_BOARD_C218_ISA: 825 case MOXA_BOARD_C218_PCI: 826 file = "c218tunx.cod"; 827 break; 828 case MOXA_BOARD_CP204J: 829 file = "cp204unx.cod"; 830 break; 831 default: 832 file = "c320tunx.cod"; 833 break; 834 } 835 836 ret = request_firmware(&fw, file, dev); 837 if (ret) { 838 printk(KERN_ERR "request_firmware failed\n"); 839 goto err_free; 840 } 841 842 ret = moxa_load_fw(brd, fw); 843 844 release_firmware(fw); 845 846 if (ret) 847 goto err_free; 848 849 spin_lock_bh(&moxa_lock); 850 brd->ready = 1; 851 if (!timer_pending(&moxaTimer)) 852 mod_timer(&moxaTimer, jiffies + HZ / 50); 853 spin_unlock_bh(&moxa_lock); 854 855 return 0; 856err_free: 857 kfree(brd->ports); 858err: 859 return ret; 860} 861 862static void moxa_board_deinit(struct moxa_board_conf *brd) 863{ 864 spin_lock_bh(&moxa_lock); 865 brd->ready = 0; 866 spin_unlock_bh(&moxa_lock); 867 iounmap(brd->basemem); 868 brd->basemem = NULL; 869 kfree(brd->ports); 870} 871 872#ifdef CONFIG_PCI 873static int __devinit moxa_pci_probe(struct pci_dev *pdev, 874 const struct pci_device_id *ent) 875{ 876 struct moxa_board_conf *board; 877 unsigned int i; 878 int board_type = ent->driver_data; 879 int retval; 880 881 retval = pci_enable_device(pdev); 882 if (retval) { 883 dev_err(&pdev->dev, "can't enable pci device\n"); 884 goto err; 885 } 886 887 for (i = 0; i < MAX_BOARDS; i++) 888 if (moxa_boards[i].basemem == NULL) 889 break; 890 891 retval = -ENODEV; 892 if (i >= MAX_BOARDS) { 893 dev_warn(&pdev->dev, "more than %u MOXA Intellio family boards " 894 "found. Board is ignored.\n", MAX_BOARDS); 895 goto err; 896 } 897 898 board = &moxa_boards[i]; 899 900 retval = pci_request_region(pdev, 2, "moxa-base"); 901 if (retval) { 902 dev_err(&pdev->dev, "can't request pci region 2\n"); 903 goto err; 904 } 905 906 board->basemem = ioremap(pci_resource_start(pdev, 2), 0x4000); 907 if (board->basemem == NULL) { 908 dev_err(&pdev->dev, "can't remap io space 2\n"); 909 goto err_reg; 910 } 911 912 board->boardType = board_type; 913 switch (board_type) { 914 case MOXA_BOARD_C218_ISA: 915 case MOXA_BOARD_C218_PCI: 916 board->numPorts = 8; 917 break; 918 919 case MOXA_BOARD_CP204J: 920 board->numPorts = 4; 921 break; 922 default: 923 board->numPorts = 0; 924 break; 925 } 926 board->busType = MOXA_BUS_TYPE_PCI; 927 928 retval = moxa_init_board(board, &pdev->dev); 929 if (retval) 930 goto err_base; 931 932 pci_set_drvdata(pdev, board); 933 934 return (0); 935err_base: 936 iounmap(board->basemem); 937 board->basemem = NULL; 938err_reg: 939 pci_release_region(pdev, 2); 940err: 941 return retval; 942} 943 944static void __devexit moxa_pci_remove(struct pci_dev *pdev) 945{ 946 struct moxa_board_conf *brd = pci_get_drvdata(pdev); 947 948 moxa_board_deinit(brd); 949 950 pci_release_region(pdev, 2); 951} 952 953static struct pci_driver moxa_pci_driver = { 954 .name = "moxa", 955 .id_table = moxa_pcibrds, 956 .probe = moxa_pci_probe, 957 .remove = __devexit_p(moxa_pci_remove) 958}; 959#endif /* CONFIG_PCI */ 960 961static int __init moxa_init(void) 962{ 963 unsigned int isabrds = 0; 964 int retval = 0; 965 966 printk(KERN_INFO "MOXA Intellio family driver version %s\n", 967 MOXA_VERSION); 968 moxaDriver = alloc_tty_driver(MAX_PORTS + 1); 969 if (!moxaDriver) 970 return -ENOMEM; 971 972 moxaDriver->owner = THIS_MODULE; 973 moxaDriver->name = "ttyMX"; 974 moxaDriver->major = ttymajor; 975 moxaDriver->minor_start = 0; 976 moxaDriver->type = TTY_DRIVER_TYPE_SERIAL; 977 moxaDriver->subtype = SERIAL_TYPE_NORMAL; 978 moxaDriver->init_termios = tty_std_termios; 979 moxaDriver->init_termios.c_cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL; 980 moxaDriver->init_termios.c_ispeed = 9600; 981 moxaDriver->init_termios.c_ospeed = 9600; 982 moxaDriver->flags = TTY_DRIVER_REAL_RAW; 983 tty_set_operations(moxaDriver, &moxa_ops); 984 985 pr_debug("Moxa tty devices major number = %d\n", ttymajor); 986 987 if (tty_register_driver(moxaDriver)) { 988 printk(KERN_ERR "Couldn't install MOXA Smartio family driver !\n"); 989 put_tty_driver(moxaDriver); 990 return -1; 991 } 992 993 /* Find the boards defined from module args. */ 994#ifdef MODULE 995 { 996 struct moxa_board_conf *brd = moxa_boards; 997 unsigned int i; 998 for (i = 0; i < MAX_BOARDS; i++) { 999 if (!baseaddr[i]) 1000 break; 1001 if (type[i] == MOXA_BOARD_C218_ISA || 1002 type[i] == MOXA_BOARD_C320_ISA) { 1003 pr_debug("Moxa board %2d: %s board(baseAddr=%lx)\n", 1004 isabrds + 1, moxa_brdname[type[i] - 1], 1005 baseaddr[i]); 1006 brd->boardType = type[i]; 1007 brd->numPorts = type[i] == MOXA_BOARD_C218_ISA ? 8 : 1008 numports[i]; 1009 brd->busType = MOXA_BUS_TYPE_ISA; 1010 brd->basemem = ioremap(baseaddr[i], 0x4000); 1011 if (!brd->basemem) { 1012 printk(KERN_ERR "moxa: can't remap %lx\n", 1013 baseaddr[i]); 1014 continue; 1015 } 1016 if (moxa_init_board(brd, NULL)) { 1017 iounmap(brd->basemem); 1018 brd->basemem = NULL; 1019 continue; 1020 } 1021 1022 brd++; 1023 isabrds++; 1024 } 1025 } 1026 } 1027#endif 1028 1029#ifdef CONFIG_PCI 1030 retval = pci_register_driver(&moxa_pci_driver); 1031 if (retval) { 1032 printk(KERN_ERR "Can't register moxa pci driver!\n"); 1033 if (isabrds) 1034 retval = 0; 1035 } 1036#endif 1037 1038 return retval; 1039} 1040 1041static void __exit moxa_exit(void) 1042{ 1043 int i; 1044 1045#ifdef CONFIG_PCI 1046 pci_unregister_driver(&moxa_pci_driver); 1047#endif 1048 1049 for (i = 0; i < MAX_BOARDS; i++) /* ISA boards */ 1050 if (moxa_boards[i].ready) 1051 moxa_board_deinit(&moxa_boards[i]); 1052 1053 del_timer_sync(&moxaTimer); 1054 1055 if (tty_unregister_driver(moxaDriver)) 1056 printk(KERN_ERR "Couldn't unregister MOXA Intellio family " 1057 "serial driver\n"); 1058 put_tty_driver(moxaDriver); 1059} 1060 1061module_init(moxa_init); 1062module_exit(moxa_exit); 1063 1064static int moxa_open(struct tty_struct *tty, struct file *filp) 1065{ 1066 struct moxa_board_conf *brd; 1067 struct moxa_port *ch; 1068 int port; 1069 int retval; 1070 1071 port = tty->index; 1072 if (port == MAX_PORTS) { 1073 return capable(CAP_SYS_ADMIN) ? 0 : -EPERM; 1074 } 1075 brd = &moxa_boards[port / MAX_PORTS_PER_BOARD]; 1076 if (!brd->ready) 1077 return -ENODEV; 1078 1079 ch = &brd->ports[port % MAX_PORTS_PER_BOARD]; 1080 ch->count++; 1081 tty->driver_data = ch; 1082 ch->tty = tty; 1083 if (!(ch->asyncflags & ASYNC_INITIALIZED)) { 1084 ch->statusflags = 0; 1085 moxa_set_tty_param(tty, tty->termios); 1086 MoxaPortLineCtrl(ch, 1, 1); 1087 MoxaPortEnable(ch); 1088 ch->asyncflags |= ASYNC_INITIALIZED; 1089 } 1090 retval = moxa_block_till_ready(tty, filp, ch); 1091 1092 moxa_unthrottle(tty); 1093 1094 if (ch->type == PORT_16550A) { 1095 MoxaSetFifo(ch, 1); 1096 } else { 1097 MoxaSetFifo(ch, 0); 1098 } 1099 1100 return (retval); 1101} 1102 1103static void moxa_close(struct tty_struct *tty, struct file *filp) 1104{ 1105 struct moxa_port *ch; 1106 int port; 1107 1108 port = tty->index; 1109 if (port == MAX_PORTS) { 1110 return; 1111 } 1112 if (tty->driver_data == NULL) { 1113 return; 1114 } 1115 if (tty_hung_up_p(filp)) { 1116 return; 1117 } 1118 ch = (struct moxa_port *) tty->driver_data; 1119 1120 if ((tty->count == 1) && (ch->count != 1)) { 1121 printk(KERN_WARNING "moxa_close: bad serial port count; " 1122 "tty->count is 1, ch->count is %d\n", ch->count); 1123 ch->count = 1; 1124 } 1125 if (--ch->count < 0) { 1126 printk(KERN_WARNING "moxa_close: bad serial port count, " 1127 "device=%s\n", tty->name); 1128 ch->count = 0; 1129 } 1130 if (ch->count) { 1131 return; 1132 } 1133 ch->asyncflags |= ASYNC_CLOSING; 1134 1135 ch->cflag = tty->termios->c_cflag; 1136 if (ch->asyncflags & ASYNC_INITIALIZED) { 1137 moxa_setup_empty_event(tty); 1138 tty_wait_until_sent(tty, 30 * HZ); /* 30 seconds timeout */ 1139 } 1140 moxa_shut_down(ch); 1141 MoxaPortFlushData(ch, 2); 1142 1143 if (tty->driver->flush_buffer) 1144 tty->driver->flush_buffer(tty); 1145 tty_ldisc_flush(tty); 1146 1147 tty->closing = 0; 1148 ch->tty = NULL; 1149 if (ch->blocked_open) { 1150 if (ch->close_delay) { 1151 msleep_interruptible(jiffies_to_msecs(ch->close_delay)); 1152 } 1153 wake_up_interruptible(&ch->open_wait); 1154 } 1155 ch->asyncflags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING); 1156 complete_all(&ch->close_wait); 1157} 1158 1159static int moxa_write(struct tty_struct *tty, 1160 const unsigned char *buf, int count) 1161{ 1162 struct moxa_port *ch = tty->driver_data; 1163 int len; 1164 1165 if (ch == NULL) 1166 return 0; 1167 1168 spin_lock_bh(&moxa_lock); 1169 len = MoxaPortWriteData(ch, buf, count); 1170 spin_unlock_bh(&moxa_lock); 1171 1172 /********************************************* 1173 if ( !(ch->statusflags & LOWWAIT) && 1174 ((len != count) || (MoxaPortTxFree(port) <= 100)) ) 1175 ************************************************/ 1176 ch->statusflags |= LOWWAIT; 1177 return (len); 1178} 1179 1180static int moxa_write_room(struct tty_struct *tty) 1181{ 1182 struct moxa_port *ch; 1183 1184 if (tty->stopped) 1185 return (0); 1186 ch = tty->driver_data; 1187 if (ch == NULL) 1188 return (0); 1189 return MoxaPortTxFree(ch); 1190} 1191 1192static void moxa_flush_buffer(struct tty_struct *tty) 1193{ 1194 struct moxa_port *ch = tty->driver_data; 1195 1196 if (ch == NULL) 1197 return; 1198 MoxaPortFlushData(ch, 1); 1199 tty_wakeup(tty); 1200} 1201 1202static int moxa_chars_in_buffer(struct tty_struct *tty) 1203{ 1204 struct moxa_port *ch = tty->driver_data; 1205 int chars; 1206 1207 /* 1208 * Sigh...I have to check if driver_data is NULL here, because 1209 * if an open() fails, the TTY subsystem eventually calls 1210 * tty_wait_until_sent(), which calls the driver's chars_in_buffer() 1211 * routine. And since the open() failed, we return 0 here. TDJ 1212 */ 1213 if (ch == NULL) 1214 return (0); 1215 chars = MoxaPortTxQueue(ch); 1216 if (chars) { 1217 /* 1218 * Make it possible to wakeup anything waiting for output 1219 * in tty_ioctl.c, etc. 1220 */ 1221 if (!(ch->statusflags & EMPTYWAIT)) 1222 moxa_setup_empty_event(tty); 1223 } 1224 return (chars); 1225} 1226 1227static void moxa_flush_chars(struct tty_struct *tty) 1228{ 1229 /* 1230 * Don't think I need this, because this is called to empty the TX 1231 * buffer for the 16450, 16550, etc. 1232 */ 1233} 1234 1235static void moxa_put_char(struct tty_struct *tty, unsigned char c) 1236{ 1237 struct moxa_port *ch = tty->driver_data; 1238 1239 if (ch == NULL) 1240 return; 1241 spin_lock_bh(&moxa_lock); 1242 MoxaPortWriteData(ch, &c, 1); 1243 spin_unlock_bh(&moxa_lock); 1244 /************************************************ 1245 if ( !(ch->statusflags & LOWWAIT) && (MoxaPortTxFree(port) <= 100) ) 1246 *************************************************/ 1247 ch->statusflags |= LOWWAIT; 1248} 1249 1250static int moxa_tiocmget(struct tty_struct *tty, struct file *file) 1251{ 1252 struct moxa_port *ch = tty->driver_data; 1253 int flag = 0, dtr, rts; 1254 1255 if (!ch) 1256 return -EINVAL; 1257 1258 MoxaPortGetLineOut(ch, &dtr, &rts); 1259 if (dtr) 1260 flag |= TIOCM_DTR; 1261 if (rts) 1262 flag |= TIOCM_RTS; 1263 dtr = MoxaPortLineStatus(ch); 1264 if (dtr & 1) 1265 flag |= TIOCM_CTS; 1266 if (dtr & 2) 1267 flag |= TIOCM_DSR; 1268 if (dtr & 4) 1269 flag |= TIOCM_CD; 1270 return flag; 1271} 1272 1273static int moxa_tiocmset(struct tty_struct *tty, struct file *file, 1274 unsigned int set, unsigned int clear) 1275{ 1276 struct moxa_port *ch = tty->driver_data; 1277 int port; 1278 int dtr, rts; 1279 1280 port = tty->index; 1281 if (!ch) 1282 return -EINVAL; 1283 1284 MoxaPortGetLineOut(ch, &dtr, &rts); 1285 if (set & TIOCM_RTS) 1286 rts = 1; 1287 if (set & TIOCM_DTR) 1288 dtr = 1; 1289 if (clear & TIOCM_RTS) 1290 rts = 0; 1291 if (clear & TIOCM_DTR) 1292 dtr = 0; 1293 MoxaPortLineCtrl(ch, dtr, rts); 1294 return 0; 1295} 1296 1297static void moxa_throttle(struct tty_struct *tty) 1298{ 1299 struct moxa_port *ch = (struct moxa_port *) tty->driver_data; 1300 1301 ch->statusflags |= THROTTLE; 1302} 1303 1304static void moxa_unthrottle(struct tty_struct *tty) 1305{ 1306 struct moxa_port *ch = (struct moxa_port *) tty->driver_data; 1307 1308 ch->statusflags &= ~THROTTLE; 1309} 1310 1311static void moxa_set_termios(struct tty_struct *tty, 1312 struct ktermios *old_termios) 1313{ 1314 struct moxa_port *ch = (struct moxa_port *) tty->driver_data; 1315 1316 if (ch == NULL) 1317 return; 1318 moxa_set_tty_param(tty, old_termios); 1319 if (!(old_termios->c_cflag & CLOCAL) && 1320 (tty->termios->c_cflag & CLOCAL)) 1321 wake_up_interruptible(&ch->open_wait); 1322} 1323 1324static void moxa_stop(struct tty_struct *tty) 1325{ 1326 struct moxa_port *ch = (struct moxa_port *) tty->driver_data; 1327 1328 if (ch == NULL) 1329 return; 1330 MoxaPortTxDisable(ch); 1331 ch->statusflags |= TXSTOPPED; 1332} 1333 1334 1335static void moxa_start(struct tty_struct *tty) 1336{ 1337 struct moxa_port *ch = (struct moxa_port *) tty->driver_data; 1338 1339 if (ch == NULL) 1340 return; 1341 1342 if (!(ch->statusflags & TXSTOPPED)) 1343 return; 1344 1345 MoxaPortTxEnable(ch); 1346 ch->statusflags &= ~TXSTOPPED; 1347} 1348 1349static void moxa_hangup(struct tty_struct *tty) 1350{ 1351 struct moxa_port *ch = (struct moxa_port *) tty->driver_data; 1352 1353 moxa_flush_buffer(tty); 1354 moxa_shut_down(ch); 1355 ch->count = 0; 1356 ch->asyncflags &= ~ASYNC_NORMAL_ACTIVE; 1357 ch->tty = NULL; 1358 wake_up_interruptible(&ch->open_wait); 1359} 1360 1361static void moxa_new_dcdstate(struct moxa_port *p, u8 dcd) 1362{ 1363 dcd = !!dcd; 1364 1365 if ((dcd != p->DCDState) && p->tty && C_CLOCAL(p->tty)) { 1366 if (!dcd) { 1367 tty_hangup(p->tty); 1368 p->asyncflags &= ~ASYNC_NORMAL_ACTIVE; 1369 } 1370 wake_up_interruptible(&p->open_wait); 1371 } 1372 p->DCDState = dcd; 1373} 1374 1375static int moxa_poll_port(struct moxa_port *p, unsigned int handle, 1376 u16 __iomem *ip) 1377{ 1378 struct tty_struct *tty = p->tty; 1379 void __iomem *ofsAddr; 1380 unsigned int inited = p->asyncflags & ASYNC_INITIALIZED; 1381 u16 intr; 1382 1383 if (tty) { 1384 if ((p->statusflags & EMPTYWAIT) && 1385 MoxaPortTxQueue(p) == 0) { 1386 p->statusflags &= ~EMPTYWAIT; 1387 tty_wakeup(tty); 1388 } 1389 if ((p->statusflags & LOWWAIT) && !tty->stopped && 1390 MoxaPortTxQueue(p) <= WAKEUP_CHARS) { 1391 p->statusflags &= ~LOWWAIT; 1392 tty_wakeup(tty); 1393 } 1394 1395 if (inited && !(p->statusflags & THROTTLE) && 1396 MoxaPortRxQueue(p) > 0) { /* RX */ 1397 MoxaPortReadData(p); 1398 tty_schedule_flip(tty); 1399 } 1400 } else { 1401 p->statusflags &= ~EMPTYWAIT; 1402 MoxaPortFlushData(p, 0); /* flush RX */ 1403 } 1404 1405 if (!handle) /* nothing else to do */ 1406 return 0; 1407 1408 intr = readw(ip); /* port irq status */ 1409 if (intr == 0) 1410 return 0; 1411 1412 writew(0, ip); /* ACK port */ 1413 ofsAddr = p->tableAddr; 1414 if (intr & IntrTx) /* disable tx intr */ 1415 writew(readw(ofsAddr + HostStat) & ~WakeupTx, 1416 ofsAddr + HostStat); 1417 1418 if (!inited) 1419 return 0; 1420 1421 if (tty && (intr & IntrBreak) && !I_IGNBRK(tty)) { /* BREAK */ 1422 tty_insert_flip_char(tty, 0, TTY_BREAK); 1423 tty_schedule_flip(tty); 1424 } 1425 1426 if (intr & IntrLine) 1427 moxa_new_dcdstate(p, readb(ofsAddr + FlagStat) & DCD_state); 1428 1429 return 0; 1430} 1431 1432static void moxa_poll(unsigned long ignored) 1433{ 1434 struct moxa_board_conf *brd; 1435 u16 __iomem *ip; 1436 unsigned int card, port, served = 0; 1437 1438 spin_lock(&moxa_lock); 1439 for (card = 0; card < MAX_BOARDS; card++) { 1440 brd = &moxa_boards[card]; 1441 if (!brd->ready) 1442 continue; 1443 1444 served++; 1445 1446 ip = NULL; 1447 if (readb(brd->intPend) == 0xff) 1448 ip = brd->intTable + readb(brd->intNdx); 1449 1450 for (port = 0; port < brd->numPorts; port++) 1451 moxa_poll_port(&brd->ports[port], !!ip, ip + port); 1452 1453 if (ip) 1454 writeb(0, brd->intPend); /* ACK */ 1455 1456 if (moxaLowWaterChk) { 1457 struct moxa_port *p = brd->ports; 1458 for (port = 0; port < brd->numPorts; port++, p++) 1459 if (p->lowChkFlag) { 1460 p->lowChkFlag = 0; 1461 moxa_low_water_check(p->tableAddr); 1462 } 1463 } 1464 } 1465 moxaLowWaterChk = 0; 1466 1467 if (served) 1468 mod_timer(&moxaTimer, jiffies + HZ / 50); 1469 spin_unlock(&moxa_lock); 1470} 1471 1472/******************************************************************************/ 1473 1474static void moxa_set_tty_param(struct tty_struct *tty, struct ktermios *old_termios) 1475{ 1476 register struct ktermios *ts; 1477 struct moxa_port *ch; 1478 int rts, cts, txflow, rxflow, xany, baud; 1479 1480 ch = (struct moxa_port *) tty->driver_data; 1481 ts = tty->termios; 1482 rts = cts = txflow = rxflow = xany = 0; 1483 if (ts->c_cflag & CRTSCTS) 1484 rts = cts = 1; 1485 if (ts->c_iflag & IXON) 1486 txflow = 1; 1487 if (ts->c_iflag & IXOFF) 1488 rxflow = 1; 1489 if (ts->c_iflag & IXANY) 1490 xany = 1; 1491 1492 /* Clear the features we don't support */ 1493 ts->c_cflag &= ~CMSPAR; 1494 MoxaPortFlowCtrl(ch, rts, cts, txflow, rxflow, xany); 1495 baud = MoxaPortSetTermio(ch, ts, tty_get_baud_rate(tty)); 1496 if (baud == -1) 1497 baud = tty_termios_baud_rate(old_termios); 1498 /* Not put the baud rate into the termios data */ 1499 tty_encode_baud_rate(tty, baud, baud); 1500} 1501 1502static int moxa_block_till_ready(struct tty_struct *tty, struct file *filp, 1503 struct moxa_port *ch) 1504{ 1505 DECLARE_WAITQUEUE(wait,current); 1506 int retval; 1507 int do_clocal = C_CLOCAL(tty); 1508 1509 /* 1510 * If the device is in the middle of being closed, then block 1511 * until it's done, and then try again. 1512 */ 1513 if (tty_hung_up_p(filp) || (ch->asyncflags & ASYNC_CLOSING)) { 1514 if (ch->asyncflags & ASYNC_CLOSING) 1515 wait_for_completion_interruptible(&ch->close_wait); 1516#ifdef SERIAL_DO_RESTART 1517 if (ch->asyncflags & ASYNC_HUP_NOTIFY) 1518 return (-EAGAIN); 1519 else 1520 return (-ERESTARTSYS); 1521#else 1522 return (-EAGAIN); 1523#endif 1524 } 1525 /* 1526 * If non-blocking mode is set, then make the check up front 1527 * and then exit. 1528 */ 1529 if (filp->f_flags & O_NONBLOCK) { 1530 ch->asyncflags |= ASYNC_NORMAL_ACTIVE; 1531 return (0); 1532 } 1533 /* 1534 * Block waiting for the carrier detect and the line to become free 1535 */ 1536 retval = 0; 1537 add_wait_queue(&ch->open_wait, &wait); 1538 pr_debug("block_til_ready before block: ttys%d, count = %d\n", 1539 tty->index, ch->count); 1540 spin_lock_bh(&moxa_lock); 1541 if (!tty_hung_up_p(filp)) 1542 ch->count--; 1543 ch->blocked_open++; 1544 spin_unlock_bh(&moxa_lock); 1545 1546 while (1) { 1547 set_current_state(TASK_INTERRUPTIBLE); 1548 if (tty_hung_up_p(filp) || 1549 !(ch->asyncflags & ASYNC_INITIALIZED)) { 1550#ifdef SERIAL_DO_RESTART 1551 if (ch->asyncflags & ASYNC_HUP_NOTIFY) 1552 retval = -EAGAIN; 1553 else 1554 retval = -ERESTARTSYS; 1555#else 1556 retval = -EAGAIN; 1557#endif 1558 break; 1559 } 1560 if (!(ch->asyncflags & ASYNC_CLOSING) && (do_clocal || 1561 ch->DCDState)) 1562 break; 1563 1564 if (signal_pending(current)) { 1565 retval = -ERESTARTSYS; 1566 break; 1567 } 1568 schedule(); 1569 } 1570 set_current_state(TASK_RUNNING); 1571 remove_wait_queue(&ch->open_wait, &wait); 1572 1573 spin_lock_bh(&moxa_lock); 1574 if (!tty_hung_up_p(filp)) 1575 ch->count++; 1576 ch->blocked_open--; 1577 spin_unlock_bh(&moxa_lock); 1578 pr_debug("block_til_ready after blocking: ttys%d, count = %d\n", 1579 tty->index, ch->count); 1580 if (retval) 1581 return (retval); 1582 /* FIXME: review to see if we need to use set_bit on these */ 1583 ch->asyncflags |= ASYNC_NORMAL_ACTIVE; 1584 return 0; 1585} 1586 1587static void moxa_setup_empty_event(struct tty_struct *tty) 1588{ 1589 struct moxa_port *ch = tty->driver_data; 1590 1591 spin_lock_bh(&moxa_lock); 1592 ch->statusflags |= EMPTYWAIT; 1593 spin_unlock_bh(&moxa_lock); 1594} 1595 1596static void moxa_shut_down(struct moxa_port *ch) 1597{ 1598 struct tty_struct *tp; 1599 1600 if (!(ch->asyncflags & ASYNC_INITIALIZED)) 1601 return; 1602 1603 tp = ch->tty; 1604 1605 MoxaPortDisable(ch); 1606 1607 /* 1608 * If we're a modem control device and HUPCL is on, drop RTS & DTR. 1609 */ 1610 if (tp->termios->c_cflag & HUPCL) 1611 MoxaPortLineCtrl(ch, 0, 0); 1612 1613 ch->asyncflags &= ~ASYNC_INITIALIZED; 1614} 1615 1616/***************************************************************************** 1617 * Driver level functions: * 1618 *****************************************************************************/ 1619 1620static void MoxaPortFlushData(struct moxa_port *port, int mode) 1621{ 1622 void __iomem *ofsAddr; 1623 if ((mode < 0) || (mode > 2)) 1624 return; 1625 ofsAddr = port->tableAddr; 1626 moxafunc(ofsAddr, FC_FlushQueue, mode); 1627 if (mode != 1) { 1628 port->lowChkFlag = 0; 1629 moxa_low_water_check(ofsAddr); 1630 } 1631} 1632 1633/***************************************************************************** 1634 * Port level functions: * 1635 * 2. MoxaPortEnable(int port); * 1636 * 3. MoxaPortDisable(int port); * 1637 * 4. MoxaPortGetMaxBaud(int port); * 1638 * 6. MoxaPortSetBaud(int port, long baud); * 1639 * 8. MoxaPortSetTermio(int port, unsigned char *termio); * 1640 * 9. MoxaPortGetLineOut(int port, int *dtrState, int *rtsState); * 1641 * 10. MoxaPortLineCtrl(int port, int dtrState, int rtsState); * 1642 * 11. MoxaPortFlowCtrl(int port, int rts, int cts, int rx, int tx,int xany); * 1643 * 12. MoxaPortLineStatus(int port); * 1644 * 15. MoxaPortFlushData(int port, int mode); * 1645 * 16. MoxaPortWriteData(int port, unsigned char * buffer, int length); * 1646 * 17. MoxaPortReadData(int port, struct tty_struct *tty); * 1647 * 20. MoxaPortTxQueue(int port); * 1648 * 21. MoxaPortTxFree(int port); * 1649 * 22. MoxaPortRxQueue(int port); * 1650 * 24. MoxaPortTxDisable(int port); * 1651 * 25. MoxaPortTxEnable(int port); * 1652 * 27. MoxaPortResetBrkCnt(int port); * 1653 *****************************************************************************/ 1654/* 1655 * Moxa Port Number Description: 1656 * 1657 * MOXA serial driver supports up to 4 MOXA-C218/C320 boards. And, 1658 * the port number using in MOXA driver functions will be 0 to 31 for 1659 * first MOXA board, 32 to 63 for second, 64 to 95 for third and 96 1660 * to 127 for fourth. For example, if you setup three MOXA boards, 1661 * first board is C218, second board is C320-16 and third board is 1662 * C320-32. The port number of first board (C218 - 8 ports) is from 1663 * 0 to 7. The port number of second board (C320 - 16 ports) is form 1664 * 32 to 47. The port number of third board (C320 - 32 ports) is from 1665 * 64 to 95. And those port numbers form 8 to 31, 48 to 63 and 96 to 1666 * 127 will be invalid. 1667 * 1668 * 1669 * Moxa Functions Description: 1670 * 1671 * Function 1: Driver initialization routine, this routine must be 1672 * called when initialized driver. 1673 * Syntax: 1674 * void MoxaDriverInit(); 1675 * 1676 * 1677 * Function 2: Moxa driver private IOCTL command processing. 1678 * Syntax: 1679 * int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port); 1680 * 1681 * unsigned int cmd : IOCTL command 1682 * unsigned long arg : IOCTL argument 1683 * int port : port number (0 - 127) 1684 * 1685 * return: 0 (OK) 1686 * -EINVAL 1687 * -ENOIOCTLCMD 1688 * 1689 * 1690 * Function 6: Enable this port to start Tx/Rx data. 1691 * Syntax: 1692 * void MoxaPortEnable(int port); 1693 * int port : port number (0 - 127) 1694 * 1695 * 1696 * Function 7: Disable this port 1697 * Syntax: 1698 * void MoxaPortDisable(int port); 1699 * int port : port number (0 - 127) 1700 * 1701 * 1702 * Function 8: Get the maximun available baud rate of this port. 1703 * Syntax: 1704 * long MoxaPortGetMaxBaud(int port); 1705 * int port : port number (0 - 127) 1706 * 1707 * return: 0 : this port is invalid 1708 * 38400/57600/115200 bps 1709 * 1710 * 1711 * Function 10: Setting baud rate of this port. 1712 * Syntax: 1713 * long MoxaPortSetBaud(int port, long baud); 1714 * int port : port number (0 - 127) 1715 * long baud : baud rate (50 - 115200) 1716 * 1717 * return: 0 : this port is invalid or baud < 50 1718 * 50 - 115200 : the real baud rate set to the port, if 1719 * the argument baud is large than maximun 1720 * available baud rate, the real setting 1721 * baud rate will be the maximun baud rate. 1722 * 1723 * 1724 * Function 12: Configure the port. 1725 * Syntax: 1726 * int MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud); 1727 * int port : port number (0 - 127) 1728 * struct ktermios * termio : termio structure pointer 1729 * speed_t baud : baud rate 1730 * 1731 * return: -1 : this port is invalid or termio == NULL 1732 * 0 : setting O.K. 1733 * 1734 * 1735 * Function 13: Get the DTR/RTS state of this port. 1736 * Syntax: 1737 * int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState); 1738 * int port : port number (0 - 127) 1739 * int * dtrState : pointer to INT to receive the current DTR 1740 * state. (if NULL, this function will not 1741 * write to this address) 1742 * int * rtsState : pointer to INT to receive the current RTS 1743 * state. (if NULL, this function will not 1744 * write to this address) 1745 * 1746 * return: -1 : this port is invalid 1747 * 0 : O.K. 1748 * 1749 * 1750 * Function 14: Setting the DTR/RTS output state of this port. 1751 * Syntax: 1752 * void MoxaPortLineCtrl(int port, int dtrState, int rtsState); 1753 * int port : port number (0 - 127) 1754 * int dtrState : DTR output state (0: off, 1: on) 1755 * int rtsState : RTS output state (0: off, 1: on) 1756 * 1757 * 1758 * Function 15: Setting the flow control of this port. 1759 * Syntax: 1760 * void MoxaPortFlowCtrl(int port, int rtsFlow, int ctsFlow, int rxFlow, 1761 * int txFlow,int xany); 1762 * int port : port number (0 - 127) 1763 * int rtsFlow : H/W RTS flow control (0: no, 1: yes) 1764 * int ctsFlow : H/W CTS flow control (0: no, 1: yes) 1765 * int rxFlow : S/W Rx XON/XOFF flow control (0: no, 1: yes) 1766 * int txFlow : S/W Tx XON/XOFF flow control (0: no, 1: yes) 1767 * int xany : S/W XANY flow control (0: no, 1: yes) 1768 * 1769 * 1770 * Function 16: Get ths line status of this port 1771 * Syntax: 1772 * int MoxaPortLineStatus(int port); 1773 * int port : port number (0 - 127) 1774 * 1775 * return: Bit 0 - CTS state (0: off, 1: on) 1776 * Bit 1 - DSR state (0: off, 1: on) 1777 * Bit 2 - DCD state (0: off, 1: on) 1778 * 1779 * 1780 * Function 19: Flush the Rx/Tx buffer data of this port. 1781 * Syntax: 1782 * void MoxaPortFlushData(int port, int mode); 1783 * int port : port number (0 - 127) 1784 * int mode 1785 * 0 : flush the Rx buffer 1786 * 1 : flush the Tx buffer 1787 * 2 : flush the Rx and Tx buffer 1788 * 1789 * 1790 * Function 20: Write data. 1791 * Syntax: 1792 * int MoxaPortWriteData(int port, unsigned char * buffer, int length); 1793 * int port : port number (0 - 127) 1794 * unsigned char * buffer : pointer to write data buffer. 1795 * int length : write data length 1796 * 1797 * return: 0 - length : real write data length 1798 * 1799 * 1800 * Function 21: Read data. 1801 * Syntax: 1802 * int MoxaPortReadData(int port, struct tty_struct *tty); 1803 * int port : port number (0 - 127) 1804 * struct tty_struct *tty : tty for data 1805 * 1806 * return: 0 - length : real read data length 1807 * 1808 * 1809 * Function 24: Get the Tx buffer current queued data bytes 1810 * Syntax: 1811 * int MoxaPortTxQueue(int port); 1812 * int port : port number (0 - 127) 1813 * 1814 * return: .. : Tx buffer current queued data bytes 1815 * 1816 * 1817 * Function 25: Get the Tx buffer current free space 1818 * Syntax: 1819 * int MoxaPortTxFree(int port); 1820 * int port : port number (0 - 127) 1821 * 1822 * return: .. : Tx buffer current free space 1823 * 1824 * 1825 * Function 26: Get the Rx buffer current queued data bytes 1826 * Syntax: 1827 * int MoxaPortRxQueue(int port); 1828 * int port : port number (0 - 127) 1829 * 1830 * return: .. : Rx buffer current queued data bytes 1831 * 1832 * 1833 * Function 28: Disable port data transmission. 1834 * Syntax: 1835 * void MoxaPortTxDisable(int port); 1836 * int port : port number (0 - 127) 1837 * 1838 * 1839 * Function 29: Enable port data transmission. 1840 * Syntax: 1841 * void MoxaPortTxEnable(int port); 1842 * int port : port number (0 - 127) 1843 * 1844 * 1845 * Function 31: Get the received BREAK signal count and reset it. 1846 * Syntax: 1847 * int MoxaPortResetBrkCnt(int port); 1848 * int port : port number (0 - 127) 1849 * 1850 * return: 0 - .. : BREAK signal count 1851 * 1852 * 1853 */ 1854 1855static void MoxaPortEnable(struct moxa_port *port) 1856{ 1857 void __iomem *ofsAddr; 1858 short lowwater = 512; 1859 1860 ofsAddr = port->tableAddr; 1861 writew(lowwater, ofsAddr + Low_water); 1862 if (port->board->boardType == MOXA_BOARD_C320_ISA || 1863 port->board->boardType == MOXA_BOARD_C320_PCI) { 1864 moxafunc(ofsAddr, FC_SetBreakIrq, 0); 1865 } else { 1866 writew(readw(ofsAddr + HostStat) | WakeupBreak, ofsAddr + HostStat); 1867 } 1868 1869 moxafunc(ofsAddr, FC_SetLineIrq, Magic_code); 1870 moxafunc(ofsAddr, FC_FlushQueue, 2); 1871 1872 moxafunc(ofsAddr, FC_EnableCH, Magic_code); 1873 MoxaPortLineStatus(port); 1874} 1875 1876static void MoxaPortDisable(struct moxa_port *port) 1877{ 1878 void __iomem *ofsAddr = port->tableAddr; 1879 1880 moxafunc(ofsAddr, FC_SetFlowCtl, 0); /* disable flow control */ 1881 moxafunc(ofsAddr, FC_ClrLineIrq, Magic_code); 1882 writew(0, ofsAddr + HostStat); 1883 moxafunc(ofsAddr, FC_DisableCH, Magic_code); 1884} 1885 1886static long MoxaPortGetMaxBaud(struct moxa_port *port) 1887{ 1888 if (port->board->boardType == MOXA_BOARD_C320_ISA || 1889 port->board->boardType == MOXA_BOARD_C320_PCI) 1890 return (460800L); 1891 else 1892 return (921600L); 1893} 1894 1895 1896static long MoxaPortSetBaud(struct moxa_port *port, long baud) 1897{ 1898 void __iomem *ofsAddr; 1899 long max, clock; 1900 unsigned int val; 1901 1902 if ((baud < 50L) || ((max = MoxaPortGetMaxBaud(port)) == 0)) 1903 return (0); 1904 ofsAddr = port->tableAddr; 1905 if (baud > max) 1906 baud = max; 1907 if (max == 38400L) 1908 clock = 614400L; /* for 9.8304 Mhz : max. 38400 bps */ 1909 else if (max == 57600L) 1910 clock = 691200L; /* for 11.0592 Mhz : max. 57600 bps */ 1911 else 1912 clock = 921600L; /* for 14.7456 Mhz : max. 115200 bps */ 1913 val = clock / baud; 1914 moxafunc(ofsAddr, FC_SetBaud, val); 1915 baud = clock / val; 1916 return (baud); 1917} 1918 1919static int MoxaPortSetTermio(struct moxa_port *port, struct ktermios *termio, 1920 speed_t baud) 1921{ 1922 void __iomem *ofsAddr; 1923 tcflag_t cflag; 1924 tcflag_t mode = 0; 1925 1926 ofsAddr = port->tableAddr; 1927 cflag = termio->c_cflag; /* termio->c_cflag */ 1928 1929 mode = termio->c_cflag & CSIZE; 1930 if (mode == CS5) 1931 mode = MX_CS5; 1932 else if (mode == CS6) 1933 mode = MX_CS6; 1934 else if (mode == CS7) 1935 mode = MX_CS7; 1936 else if (mode == CS8) 1937 mode = MX_CS8; 1938 1939 if (termio->c_cflag & CSTOPB) { 1940 if (mode == MX_CS5) 1941 mode |= MX_STOP15; 1942 else 1943 mode |= MX_STOP2; 1944 } else 1945 mode |= MX_STOP1; 1946 1947 if (termio->c_cflag & PARENB) { 1948 if (termio->c_cflag & PARODD) 1949 mode |= MX_PARODD; 1950 else 1951 mode |= MX_PAREVEN; 1952 } else 1953 mode |= MX_PARNONE; 1954 1955 moxafunc(ofsAddr, FC_SetDataMode, (ushort) mode); 1956 1957 if (port->board->boardType == MOXA_BOARD_C320_ISA || 1958 port->board->boardType == MOXA_BOARD_C320_PCI) { 1959 if (baud >= 921600L) 1960 return (-1); 1961 } 1962 baud = MoxaPortSetBaud(port, baud); 1963 1964 if (termio->c_iflag & (IXON | IXOFF | IXANY)) { 1965 writeb(termio->c_cc[VSTART], ofsAddr + FuncArg); 1966 writeb(termio->c_cc[VSTOP], ofsAddr + FuncArg1); 1967 writeb(FC_SetXonXoff, ofsAddr + FuncCode); 1968 moxa_wait_finish(ofsAddr); 1969 1970 } 1971 return (baud); 1972} 1973 1974static int MoxaPortGetLineOut(struct moxa_port *port, int *dtrState, 1975 int *rtsState) 1976{ 1977 1978 if (dtrState) 1979 *dtrState = !!(port->lineCtrl & DTR_ON); 1980 if (rtsState) 1981 *rtsState = !!(port->lineCtrl & RTS_ON); 1982 1983 return (0); 1984} 1985 1986static void MoxaPortLineCtrl(struct moxa_port *port, int dtr, int rts) 1987{ 1988 int mode = 0; 1989 1990 if (dtr) 1991 mode |= DTR_ON; 1992 if (rts) 1993 mode |= RTS_ON; 1994 port->lineCtrl = mode; 1995 moxafunc(port->tableAddr, FC_LineControl, mode); 1996} 1997 1998static void MoxaPortFlowCtrl(struct moxa_port *port, int rts, int cts, 1999 int txflow, int rxflow, int txany) 2000{ 2001 int mode = 0; 2002 2003 if (rts) 2004 mode |= RTS_FlowCtl; 2005 if (cts) 2006 mode |= CTS_FlowCtl; 2007 if (txflow) 2008 mode |= Tx_FlowCtl; 2009 if (rxflow) 2010 mode |= Rx_FlowCtl; 2011 if (txany) 2012 mode |= IXM_IXANY; 2013 moxafunc(port->tableAddr, FC_SetFlowCtl, mode); 2014} 2015 2016static int MoxaPortLineStatus(struct moxa_port *port) 2017{ 2018 void __iomem *ofsAddr; 2019 int val; 2020 2021 ofsAddr = port->tableAddr; 2022 if (port->board->boardType == MOXA_BOARD_C320_ISA || 2023 port->board->boardType == MOXA_BOARD_C320_PCI) { 2024 moxafunc(ofsAddr, FC_LineStatus, 0); 2025 val = readw(ofsAddr + FuncArg); 2026 } else { 2027 val = readw(ofsAddr + FlagStat) >> 4; 2028 } 2029 val &= 0x0B; 2030 if (val & 8) 2031 val |= 4; 2032 moxa_new_dcdstate(port, val & 8); 2033 val &= 7; 2034 return val; 2035} 2036 2037static int MoxaPortWriteData(struct moxa_port *port, 2038 const unsigned char *buffer, int len) 2039{ 2040 void __iomem *baseAddr, *ofsAddr, *ofs; 2041 unsigned int c, total; 2042 u16 head, tail, tx_mask, spage, epage; 2043 u16 pageno, pageofs, bufhead; 2044 2045 ofsAddr = port->tableAddr; 2046 baseAddr = port->board->basemem; 2047 tx_mask = readw(ofsAddr + TX_mask); 2048 spage = readw(ofsAddr + Page_txb); 2049 epage = readw(ofsAddr + EndPage_txb); 2050 tail = readw(ofsAddr + TXwptr); 2051 head = readw(ofsAddr + TXrptr); 2052 c = (head > tail) ? (head - tail - 1) : (head - tail + tx_mask); 2053 if (c > len) 2054 c = len; 2055 moxaLog.txcnt[port->tty->index] += c; 2056 total = c; 2057 if (spage == epage) { 2058 bufhead = readw(ofsAddr + Ofs_txb); 2059 writew(spage, baseAddr + Control_reg); 2060 while (c > 0) { 2061 if (head > tail) 2062 len = head - tail - 1; 2063 else 2064 len = tx_mask + 1 - tail; 2065 len = (c > len) ? len : c; 2066 ofs = baseAddr + DynPage_addr + bufhead + tail; 2067 memcpy_toio(ofs, buffer, len); 2068 buffer += len; 2069 tail = (tail + len) & tx_mask; 2070 c -= len; 2071 } 2072 } else { 2073 pageno = spage + (tail >> 13); 2074 pageofs = tail & Page_mask; 2075 while (c > 0) { 2076 len = Page_size - pageofs; 2077 if (len > c) 2078 len = c; 2079 writeb(pageno, baseAddr + Control_reg); 2080 ofs = baseAddr + DynPage_addr + pageofs; 2081 memcpy_toio(ofs, buffer, len); 2082 buffer += len; 2083 if (++pageno == epage) 2084 pageno = spage; 2085 pageofs = 0; 2086 c -= len; 2087 } 2088 tail = (tail + total) & tx_mask; 2089 } 2090 writew(tail, ofsAddr + TXwptr); 2091 writeb(1, ofsAddr + CD180TXirq); /* start to send */ 2092 return total; 2093} 2094 2095static int MoxaPortReadData(struct moxa_port *port) 2096{ 2097 struct tty_struct *tty = port->tty; 2098 unsigned char *dst; 2099 void __iomem *baseAddr, *ofsAddr, *ofs; 2100 unsigned int count, len, total; 2101 u16 tail, rx_mask, spage, epage; 2102 u16 pageno, pageofs, bufhead, head; 2103 2104 ofsAddr = port->tableAddr; 2105 baseAddr = port->board->basemem; 2106 head = readw(ofsAddr + RXrptr); 2107 tail = readw(ofsAddr + RXwptr); 2108 rx_mask = readw(ofsAddr + RX_mask); 2109 spage = readw(ofsAddr + Page_rxb); 2110 epage = readw(ofsAddr + EndPage_rxb); 2111 count = (tail >= head) ? (tail - head) : (tail - head + rx_mask + 1); 2112 if (count == 0) 2113 return 0; 2114 2115 total = count; 2116 moxaLog.rxcnt[tty->index] += total; 2117 if (spage == epage) { 2118 bufhead = readw(ofsAddr + Ofs_rxb); 2119 writew(spage, baseAddr + Control_reg); 2120 while (count > 0) { 2121 ofs = baseAddr + DynPage_addr + bufhead + head; 2122 len = (tail >= head) ? (tail - head) : 2123 (rx_mask + 1 - head); 2124 len = tty_prepare_flip_string(tty, &dst, 2125 min(len, count)); 2126 memcpy_fromio(dst, ofs, len); 2127 head = (head + len) & rx_mask; 2128 count -= len; 2129 } 2130 } else { 2131 pageno = spage + (head >> 13); 2132 pageofs = head & Page_mask; 2133 while (count > 0) { 2134 writew(pageno, baseAddr + Control_reg); 2135 ofs = baseAddr + DynPage_addr + pageofs; 2136 len = tty_prepare_flip_string(tty, &dst, 2137 min(Page_size - pageofs, count)); 2138 memcpy_fromio(dst, ofs, len); 2139 2140 count -= len; 2141 pageofs = (pageofs + len) & Page_mask; 2142 if (pageofs == 0 && ++pageno == epage) 2143 pageno = spage; 2144 } 2145 head = (head + total) & rx_mask; 2146 } 2147 writew(head, ofsAddr + RXrptr); 2148 if (readb(ofsAddr + FlagStat) & Xoff_state) { 2149 moxaLowWaterChk = 1; 2150 port->lowChkFlag = 1; 2151 } 2152 return total; 2153} 2154 2155 2156static int MoxaPortTxQueue(struct moxa_port *port) 2157{ 2158 void __iomem *ofsAddr = port->tableAddr; 2159 u16 rptr, wptr, mask; 2160 2161 rptr = readw(ofsAddr + TXrptr); 2162 wptr = readw(ofsAddr + TXwptr); 2163 mask = readw(ofsAddr + TX_mask); 2164 return (wptr - rptr) & mask; 2165} 2166 2167static int MoxaPortTxFree(struct moxa_port *port) 2168{ 2169 void __iomem *ofsAddr = port->tableAddr; 2170 u16 rptr, wptr, mask; 2171 2172 rptr = readw(ofsAddr + TXrptr); 2173 wptr = readw(ofsAddr + TXwptr); 2174 mask = readw(ofsAddr + TX_mask); 2175 return mask - ((wptr - rptr) & mask); 2176} 2177 2178static int MoxaPortRxQueue(struct moxa_port *port) 2179{ 2180 void __iomem *ofsAddr = port->tableAddr; 2181 u16 rptr, wptr, mask; 2182 2183 rptr = readw(ofsAddr + RXrptr); 2184 wptr = readw(ofsAddr + RXwptr); 2185 mask = readw(ofsAddr + RX_mask); 2186 return (wptr - rptr) & mask; 2187} 2188 2189static void MoxaPortTxDisable(struct moxa_port *port) 2190{ 2191 moxafunc(port->tableAddr, FC_SetXoffState, Magic_code); 2192} 2193 2194static void MoxaPortTxEnable(struct moxa_port *port) 2195{ 2196 moxafunc(port->tableAddr, FC_SetXonState, Magic_code); 2197} 2198 2199static int moxa_get_serial_info(struct moxa_port *info, 2200 struct serial_struct __user *retinfo) 2201{ 2202 struct serial_struct tmp; 2203 2204 memset(&tmp, 0, sizeof(tmp)); 2205 tmp.type = info->type; 2206 tmp.line = info->tty->index; 2207 tmp.port = 0; 2208 tmp.irq = 0; 2209 tmp.flags = info->asyncflags; 2210 tmp.baud_base = 921600; 2211 tmp.close_delay = info->close_delay; 2212 tmp.custom_divisor = 0; 2213 tmp.hub6 = 0; 2214 if(copy_to_user(retinfo, &tmp, sizeof(*retinfo))) 2215 return -EFAULT; 2216 return (0); 2217} 2218 2219 2220static int moxa_set_serial_info(struct moxa_port *info, 2221 struct serial_struct __user *new_info) 2222{ 2223 struct serial_struct new_serial; 2224 2225 if(copy_from_user(&new_serial, new_info, sizeof(new_serial))) 2226 return -EFAULT; 2227 2228 if ((new_serial.irq != 0) || 2229 (new_serial.port != 0) || 2230// (new_serial.type != info->type) || 2231 (new_serial.custom_divisor != 0) || 2232 (new_serial.baud_base != 921600)) 2233 return (-EPERM); 2234 2235 if (!capable(CAP_SYS_ADMIN)) { 2236 if (((new_serial.flags & ~ASYNC_USR_MASK) != 2237 (info->asyncflags & ~ASYNC_USR_MASK))) 2238 return (-EPERM); 2239 } else { 2240 info->close_delay = new_serial.close_delay * HZ / 100; 2241 } 2242 2243 new_serial.flags = (new_serial.flags & ~ASYNC_FLAGS); 2244 new_serial.flags |= (info->asyncflags & ASYNC_FLAGS); 2245 2246 if (new_serial.type == PORT_16550A) { 2247 MoxaSetFifo(info, 1); 2248 } else { 2249 MoxaSetFifo(info, 0); 2250 } 2251 2252 info->type = new_serial.type; 2253 return (0); 2254} 2255 2256 2257 2258/***************************************************************************** 2259 * Static local functions: * 2260 *****************************************************************************/ 2261 2262static void MoxaSetFifo(struct moxa_port *port, int enable) 2263{ 2264 void __iomem *ofsAddr = port->tableAddr; 2265 2266 if (!enable) { 2267 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 0); 2268 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 1); 2269 } else { 2270 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 3); 2271 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 16); 2272 } 2273} 2274