PageRenderTime 72ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/drivers/char/mxser.c

https://bitbucket.org/evzijst/gittest
C | 3170 lines | 2345 code | 422 blank | 403 comment | 619 complexity | 5f4faaf6c629796ead1d3f62fb60ccd8 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, GPL-2.0, LGPL-2.0
  1. /*
  2. * mxser.c -- MOXA Smartio/Industio family multiport serial driver.
  3. *
  4. * Copyright (C) 1999-2001 Moxa Technologies (support@moxa.com.tw).
  5. *
  6. * This code is loosely based on the Linux serial driver, written by
  7. * Linus Torvalds, Theodore T'so and others.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22. *
  23. * Original release 10/26/00
  24. *
  25. * 02/06/01 Support MOXA Industio family boards.
  26. * 02/06/01 Support TIOCGICOUNT.
  27. * 02/06/01 Fix the problem for connecting to serial mouse.
  28. * 02/06/01 Fix the problem for H/W flow control.
  29. * 02/06/01 Fix the compling warning when CONFIG_PCI
  30. * don't be defined.
  31. *
  32. * Fed through a cleanup, indent and remove of non 2.6 code by Alan Cox
  33. * <alan@redhat.com>. The original 1.8 code is available on www.moxa.com.
  34. * - Fixed x86_64 cleanness
  35. * - Fixed sleep with spinlock held in mxser_send_break
  36. */
  37. #include <linux/config.h>
  38. #include <linux/module.h>
  39. #include <linux/version.h>
  40. #include <linux/autoconf.h>
  41. #include <linux/errno.h>
  42. #include <linux/signal.h>
  43. #include <linux/sched.h>
  44. #include <linux/timer.h>
  45. #include <linux/interrupt.h>
  46. #include <linux/tty.h>
  47. #include <linux/tty_flip.h>
  48. #include <linux/serial.h>
  49. #include <linux/serial_reg.h>
  50. #include <linux/major.h>
  51. #include <linux/string.h>
  52. #include <linux/fcntl.h>
  53. #include <linux/ptrace.h>
  54. #include <linux/gfp.h>
  55. #include <linux/ioport.h>
  56. #include <linux/mm.h>
  57. #include <linux/smp_lock.h>
  58. #include <linux/delay.h>
  59. #include <linux/pci.h>
  60. #include <asm/system.h>
  61. #include <asm/io.h>
  62. #include <asm/irq.h>
  63. #include <asm/segment.h>
  64. #include <asm/bitops.h>
  65. #include <asm/uaccess.h>
  66. #include "mxser.h"
  67. #define MXSER_VERSION "1.8"
  68. #define MXSERMAJOR 174
  69. #define MXSERCUMAJOR 175
  70. #define MXSER_EVENT_TXLOW 1
  71. #define MXSER_EVENT_HANGUP 2
  72. #define MXSER_BOARDS 4 /* Max. boards */
  73. #define MXSER_PORTS 32 /* Max. ports */
  74. #define MXSER_PORTS_PER_BOARD 8 /* Max. ports per board */
  75. #define MXSER_ISR_PASS_LIMIT 256
  76. #define MXSER_ERR_IOADDR -1
  77. #define MXSER_ERR_IRQ -2
  78. #define MXSER_ERR_IRQ_CONFLIT -3
  79. #define MXSER_ERR_VECTOR -4
  80. #define SERIAL_TYPE_NORMAL 1
  81. #define SERIAL_TYPE_CALLOUT 2
  82. #define WAKEUP_CHARS 256
  83. #define UART_MCR_AFE 0x20
  84. #define UART_LSR_SPECIAL 0x1E
  85. #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK|IXON|IXOFF))
  86. #define IRQ_T(info) ((info->flags & ASYNC_SHARE_IRQ) ? SA_SHIRQ : SA_INTERRUPT)
  87. #define C168_ASIC_ID 1
  88. #define C104_ASIC_ID 2
  89. #define C102_ASIC_ID 0xB
  90. #define CI132_ASIC_ID 4
  91. #define CI134_ASIC_ID 3
  92. #define CI104J_ASIC_ID 5
  93. enum {
  94. MXSER_BOARD_C168_ISA = 1,
  95. MXSER_BOARD_C104_ISA,
  96. MXSER_BOARD_CI104J,
  97. MXSER_BOARD_C168_PCI,
  98. MXSER_BOARD_C104_PCI,
  99. MXSER_BOARD_C102_ISA,
  100. MXSER_BOARD_CI132,
  101. MXSER_BOARD_CI134,
  102. MXSER_BOARD_CP132,
  103. MXSER_BOARD_CP114,
  104. MXSER_BOARD_CT114,
  105. MXSER_BOARD_CP102,
  106. MXSER_BOARD_CP104U,
  107. MXSER_BOARD_CP168U,
  108. MXSER_BOARD_CP132U,
  109. MXSER_BOARD_CP134U,
  110. MXSER_BOARD_CP104JU,
  111. MXSER_BOARD_RC7000,
  112. MXSER_BOARD_CP118U,
  113. MXSER_BOARD_CP102UL,
  114. MXSER_BOARD_CP102U,
  115. };
  116. static char *mxser_brdname[] = {
  117. "C168 series",
  118. "C104 series",
  119. "CI-104J series",
  120. "C168H/PCI series",
  121. "C104H/PCI series",
  122. "C102 series",
  123. "CI-132 series",
  124. "CI-134 series",
  125. "CP-132 series",
  126. "CP-114 series",
  127. "CT-114 series",
  128. "CP-102 series",
  129. "CP-104U series",
  130. "CP-168U series",
  131. "CP-132U series",
  132. "CP-134U series",
  133. "CP-104JU series",
  134. "Moxa UC7000 Serial",
  135. "CP-118U series",
  136. "CP-102UL series",
  137. "CP-102U series",
  138. };
  139. static int mxser_numports[] = {
  140. 8, // C168-ISA
  141. 4, // C104-ISA
  142. 4, // CI104J
  143. 8, // C168-PCI
  144. 4, // C104-PCI
  145. 2, // C102-ISA
  146. 2, // CI132
  147. 4, // CI134
  148. 2, // CP132
  149. 4, // CP114
  150. 4, // CT114
  151. 2, // CP102
  152. 4, // CP104U
  153. 8, // CP168U
  154. 2, // CP132U
  155. 4, // CP134U
  156. 4, // CP104JU
  157. 8, // RC7000
  158. 8, // CP118U
  159. 2, // CP102UL
  160. 2, // CP102U
  161. };
  162. #define UART_TYPE_NUM 2
  163. static const unsigned int Gmoxa_uart_id[UART_TYPE_NUM] = {
  164. MOXA_MUST_MU150_HWID,
  165. MOXA_MUST_MU860_HWID
  166. };
  167. // This is only for PCI
  168. #define UART_INFO_NUM 3
  169. struct mxpciuart_info {
  170. int type;
  171. int tx_fifo;
  172. int rx_fifo;
  173. int xmit_fifo_size;
  174. int rx_high_water;
  175. int rx_trigger;
  176. int rx_low_water;
  177. long max_baud;
  178. };
  179. static const struct mxpciuart_info Gpci_uart_info[UART_INFO_NUM] = {
  180. {MOXA_OTHER_UART, 16, 16, 16, 14, 14, 1, 921600L},
  181. {MOXA_MUST_MU150_HWID, 64, 64, 64, 48, 48, 16, 230400L},
  182. {MOXA_MUST_MU860_HWID, 128, 128, 128, 96, 96, 32, 921600L}
  183. };
  184. #ifdef CONFIG_PCI
  185. static struct pci_device_id mxser_pcibrds[] = {
  186. {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C168, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_C168_PCI},
  187. {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C104, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_C104_PCI},
  188. {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP132, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_CP132},
  189. {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP114, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_CP114},
  190. {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CT114, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_CT114},
  191. {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP102, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_CP102},
  192. {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP104U, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_CP104U},
  193. {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP168U, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_CP168U},
  194. {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP132U, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_CP132U},
  195. {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP134U, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_CP134U},
  196. {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP104JU, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_CP104JU},
  197. {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_RC7000, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_RC7000},
  198. {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP118U, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_CP118U},
  199. {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP102UL, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_CP102UL},
  200. {PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP102U, PCI_ANY_ID, PCI_ANY_ID, 0, 0, MXSER_BOARD_CP102U},
  201. {0}
  202. };
  203. MODULE_DEVICE_TABLE(pci, mxser_pcibrds);
  204. #endif
  205. typedef struct _moxa_pci_info {
  206. unsigned short busNum;
  207. unsigned short devNum;
  208. struct pci_dev *pdev; // add by Victor Yu. 06-23-2003
  209. } moxa_pci_info;
  210. static int ioaddr[MXSER_BOARDS] = { 0, 0, 0, 0 };
  211. static int ttymajor = MXSERMAJOR;
  212. static int calloutmajor = MXSERCUMAJOR;
  213. static int verbose = 0;
  214. /* Variables for insmod */
  215. MODULE_AUTHOR("Casper Yang");
  216. MODULE_DESCRIPTION("MOXA Smartio/Industio Family Multiport Board Device Driver");
  217. MODULE_PARM(ioaddr, "1-4i");
  218. MODULE_PARM(ttymajor, "i");
  219. MODULE_PARM(calloutmajor, "i");
  220. MODULE_PARM(verbose, "i");
  221. MODULE_LICENSE("GPL");
  222. struct mxser_log {
  223. int tick;
  224. unsigned long rxcnt[MXSER_PORTS];
  225. unsigned long txcnt[MXSER_PORTS];
  226. };
  227. struct mxser_mon {
  228. unsigned long rxcnt;
  229. unsigned long txcnt;
  230. unsigned long up_rxcnt;
  231. unsigned long up_txcnt;
  232. int modem_status;
  233. unsigned char hold_reason;
  234. };
  235. struct mxser_mon_ext {
  236. unsigned long rx_cnt[32];
  237. unsigned long tx_cnt[32];
  238. unsigned long up_rxcnt[32];
  239. unsigned long up_txcnt[32];
  240. int modem_status[32];
  241. long baudrate[32];
  242. int databits[32];
  243. int stopbits[32];
  244. int parity[32];
  245. int flowctrl[32];
  246. int fifo[32];
  247. int iftype[32];
  248. };
  249. struct mxser_hwconf {
  250. int board_type;
  251. int ports;
  252. int irq;
  253. int vector;
  254. int vector_mask;
  255. int uart_type;
  256. int ioaddr[MXSER_PORTS_PER_BOARD];
  257. int baud_base[MXSER_PORTS_PER_BOARD];
  258. moxa_pci_info pciInfo;
  259. int IsMoxaMustChipFlag; // add by Victor Yu. 08-30-2002
  260. int MaxCanSetBaudRate[MXSER_PORTS_PER_BOARD]; // add by Victor Yu. 09-04-2002
  261. int opmode_ioaddr[MXSER_PORTS_PER_BOARD]; // add by Victor Yu. 01-05-2004
  262. };
  263. struct mxser_struct {
  264. int port;
  265. int base; /* port base address */
  266. int irq; /* port using irq no. */
  267. int vector; /* port irq vector */
  268. int vectormask; /* port vector mask */
  269. int rx_high_water;
  270. int rx_trigger; /* Rx fifo trigger level */
  271. int rx_low_water;
  272. int baud_base; /* max. speed */
  273. int flags; /* defined in tty.h */
  274. int type; /* UART type */
  275. struct tty_struct *tty;
  276. int read_status_mask;
  277. int ignore_status_mask;
  278. int xmit_fifo_size;
  279. int custom_divisor;
  280. int x_char; /* xon/xoff character */
  281. int close_delay;
  282. unsigned short closing_wait;
  283. int IER; /* Interrupt Enable Register */
  284. int MCR; /* Modem control register */
  285. unsigned long event;
  286. int count; /* # of fd on device */
  287. int blocked_open; /* # of blocked opens */
  288. long session; /* Session of opening process */
  289. long pgrp; /* pgrp of opening process */
  290. unsigned char *xmit_buf;
  291. int xmit_head;
  292. int xmit_tail;
  293. int xmit_cnt;
  294. struct work_struct tqueue;
  295. struct termios normal_termios;
  296. struct termios callout_termios;
  297. wait_queue_head_t open_wait;
  298. wait_queue_head_t close_wait;
  299. wait_queue_head_t delta_msr_wait;
  300. struct async_icount icount; /* kernel counters for the 4 input interrupts */
  301. int timeout;
  302. int IsMoxaMustChipFlag; // add by Victor Yu. 08-30-2002
  303. int MaxCanSetBaudRate; // add by Victor Yu. 09-04-2002
  304. int opmode_ioaddr; // add by Victor Yu. 01-05-2004
  305. unsigned char stop_rx;
  306. unsigned char ldisc_stop_rx;
  307. long realbaud;
  308. struct mxser_mon mon_data;
  309. unsigned char err_shadow;
  310. spinlock_t slock;
  311. };
  312. struct mxser_mstatus {
  313. tcflag_t cflag;
  314. int cts;
  315. int dsr;
  316. int ri;
  317. int dcd;
  318. };
  319. static struct mxser_mstatus GMStatus[MXSER_PORTS];
  320. static int mxserBoardCAP[MXSER_BOARDS] = {
  321. 0, 0, 0, 0
  322. /* 0x180, 0x280, 0x200, 0x320 */
  323. };
  324. static struct tty_driver *mxvar_sdriver;
  325. static struct mxser_struct mxvar_table[MXSER_PORTS];
  326. static struct tty_struct *mxvar_tty[MXSER_PORTS + 1];
  327. static struct termios *mxvar_termios[MXSER_PORTS + 1];
  328. static struct termios *mxvar_termios_locked[MXSER_PORTS + 1];
  329. static struct mxser_log mxvar_log;
  330. static int mxvar_diagflag;
  331. static unsigned char mxser_msr[MXSER_PORTS + 1];
  332. static struct mxser_mon_ext mon_data_ext;
  333. static int mxser_set_baud_method[MXSER_PORTS + 1];
  334. static spinlock_t gm_lock;
  335. /*
  336. * This is used to figure out the divisor speeds and the timeouts
  337. */
  338. static struct mxser_hwconf mxsercfg[MXSER_BOARDS];
  339. /*
  340. * static functions:
  341. */
  342. static void mxser_getcfg(int board, struct mxser_hwconf *hwconf);
  343. static int mxser_init(void);
  344. //static void mxser_poll(unsigned long);
  345. static int mxser_get_ISA_conf(int, struct mxser_hwconf *);
  346. static int mxser_get_PCI_conf(int, int, int, struct mxser_hwconf *);
  347. static void mxser_do_softint(void *);
  348. static int mxser_open(struct tty_struct *, struct file *);
  349. static void mxser_close(struct tty_struct *, struct file *);
  350. static int mxser_write(struct tty_struct *, const unsigned char *, int);
  351. static int mxser_write_room(struct tty_struct *);
  352. static void mxser_flush_buffer(struct tty_struct *);
  353. static int mxser_chars_in_buffer(struct tty_struct *);
  354. static void mxser_flush_chars(struct tty_struct *);
  355. static void mxser_put_char(struct tty_struct *, unsigned char);
  356. static int mxser_ioctl(struct tty_struct *, struct file *, uint, ulong);
  357. static int mxser_ioctl_special(unsigned int, void __user *);
  358. static void mxser_throttle(struct tty_struct *);
  359. static void mxser_unthrottle(struct tty_struct *);
  360. static void mxser_set_termios(struct tty_struct *, struct termios *);
  361. static void mxser_stop(struct tty_struct *);
  362. static void mxser_start(struct tty_struct *);
  363. static void mxser_hangup(struct tty_struct *);
  364. static void mxser_rs_break(struct tty_struct *, int);
  365. static irqreturn_t mxser_interrupt(int, void *, struct pt_regs *);
  366. static void mxser_receive_chars(struct mxser_struct *, int *);
  367. static void mxser_transmit_chars(struct mxser_struct *);
  368. static void mxser_check_modem_status(struct mxser_struct *, int);
  369. static int mxser_block_til_ready(struct tty_struct *, struct file *, struct mxser_struct *);
  370. static int mxser_startup(struct mxser_struct *);
  371. static void mxser_shutdown(struct mxser_struct *);
  372. static int mxser_change_speed(struct mxser_struct *, struct termios *old_termios);
  373. static int mxser_get_serial_info(struct mxser_struct *, struct serial_struct __user *);
  374. static int mxser_set_serial_info(struct mxser_struct *, struct serial_struct __user *);
  375. static int mxser_get_lsr_info(struct mxser_struct *, unsigned int __user *);
  376. static void mxser_send_break(struct mxser_struct *, int);
  377. static int mxser_tiocmget(struct tty_struct *, struct file *);
  378. static int mxser_tiocmset(struct tty_struct *, struct file *, unsigned int, unsigned int);
  379. static int mxser_set_baud(struct mxser_struct *info, long newspd);
  380. static void mxser_wait_until_sent(struct tty_struct *tty, int timeout);
  381. static void mxser_startrx(struct tty_struct *tty);
  382. static void mxser_stoprx(struct tty_struct *tty);
  383. static int CheckIsMoxaMust(int io)
  384. {
  385. u8 oldmcr, hwid;
  386. int i;
  387. outb(0, io + UART_LCR);
  388. DISABLE_MOXA_MUST_ENCHANCE_MODE(io);
  389. oldmcr = inb(io + UART_MCR);
  390. outb(0, io + UART_MCR);
  391. SET_MOXA_MUST_XON1_VALUE(io, 0x11);
  392. if ((hwid = inb(io + UART_MCR)) != 0) {
  393. outb(oldmcr, io + UART_MCR);
  394. return (MOXA_OTHER_UART);
  395. }
  396. GET_MOXA_MUST_HARDWARE_ID(io, &hwid);
  397. for (i = 0; i < UART_TYPE_NUM; i++) {
  398. if (hwid == Gmoxa_uart_id[i])
  399. return (int) hwid;
  400. }
  401. return MOXA_OTHER_UART;
  402. }
  403. // above is modified by Victor Yu. 08-15-2002
  404. static struct tty_operations mxser_ops = {
  405. .open = mxser_open,
  406. .close = mxser_close,
  407. .write = mxser_write,
  408. .put_char = mxser_put_char,
  409. .flush_chars = mxser_flush_chars,
  410. .write_room = mxser_write_room,
  411. .chars_in_buffer = mxser_chars_in_buffer,
  412. .flush_buffer = mxser_flush_buffer,
  413. .ioctl = mxser_ioctl,
  414. .throttle = mxser_throttle,
  415. .unthrottle = mxser_unthrottle,
  416. .set_termios = mxser_set_termios,
  417. .stop = mxser_stop,
  418. .start = mxser_start,
  419. .hangup = mxser_hangup,
  420. .tiocmget = mxser_tiocmget,
  421. .tiocmset = mxser_tiocmset,
  422. };
  423. /*
  424. * The MOXA Smartio/Industio serial driver boot-time initialization code!
  425. */
  426. static int __init mxser_module_init(void)
  427. {
  428. int ret;
  429. if (verbose)
  430. printk(KERN_DEBUG "Loading module mxser ...\n");
  431. ret = mxser_init();
  432. if (verbose)
  433. printk(KERN_DEBUG "Done.\n");
  434. return ret;
  435. }
  436. static void __exit mxser_module_exit(void)
  437. {
  438. int i, err = 0;
  439. if (verbose)
  440. printk(KERN_DEBUG "Unloading module mxser ...\n");
  441. if ((err |= tty_unregister_driver(mxvar_sdriver)))
  442. printk(KERN_ERR "Couldn't unregister MOXA Smartio/Industio family serial driver\n");
  443. for (i = 0; i < MXSER_BOARDS; i++) {
  444. struct pci_dev *pdev;
  445. if (mxsercfg[i].board_type == -1)
  446. continue;
  447. else {
  448. pdev = mxsercfg[i].pciInfo.pdev;
  449. free_irq(mxsercfg[i].irq, &mxvar_table[i * MXSER_PORTS_PER_BOARD]);
  450. if (pdev != NULL) { //PCI
  451. release_region(pci_resource_start(pdev, 2), pci_resource_len(pdev, 2));
  452. release_region(pci_resource_start(pdev, 3), pci_resource_len(pdev, 3));
  453. } else {
  454. release_region(mxsercfg[i].ioaddr[0], 8 * mxsercfg[i].ports);
  455. release_region(mxsercfg[i].vector, 1);
  456. }
  457. }
  458. }
  459. if (verbose)
  460. printk(KERN_DEBUG "Done.\n");
  461. }
  462. static void process_txrx_fifo(struct mxser_struct *info)
  463. {
  464. int i;
  465. if ((info->type == PORT_16450) || (info->type == PORT_8250)) {
  466. info->rx_trigger = 1;
  467. info->rx_high_water = 1;
  468. info->rx_low_water = 1;
  469. info->xmit_fifo_size = 1;
  470. } else {
  471. for (i = 0; i < UART_INFO_NUM; i++) {
  472. if (info->IsMoxaMustChipFlag == Gpci_uart_info[i].type) {
  473. info->rx_trigger = Gpci_uart_info[i].rx_trigger;
  474. info->rx_low_water = Gpci_uart_info[i].rx_low_water;
  475. info->rx_high_water = Gpci_uart_info[i].rx_high_water;
  476. info->xmit_fifo_size = Gpci_uart_info[i].xmit_fifo_size;
  477. break;
  478. }
  479. }
  480. }
  481. }
  482. static int mxser_initbrd(int board, struct mxser_hwconf *hwconf)
  483. {
  484. struct mxser_struct *info;
  485. int retval;
  486. int i, n;
  487. n = board * MXSER_PORTS_PER_BOARD;
  488. info = &mxvar_table[n];
  489. /*if (verbose) */ {
  490. printk(KERN_DEBUG " ttyM%d - ttyM%d ", n, n + hwconf->ports - 1);
  491. printk(" max. baud rate = %d bps.\n", hwconf->MaxCanSetBaudRate[0]);
  492. }
  493. for (i = 0; i < hwconf->ports; i++, n++, info++) {
  494. info->port = n;
  495. info->base = hwconf->ioaddr[i];
  496. info->irq = hwconf->irq;
  497. info->vector = hwconf->vector;
  498. info->vectormask = hwconf->vector_mask;
  499. info->opmode_ioaddr = hwconf->opmode_ioaddr[i]; // add by Victor Yu. 01-05-2004
  500. info->stop_rx = 0;
  501. info->ldisc_stop_rx = 0;
  502. info->IsMoxaMustChipFlag = hwconf->IsMoxaMustChipFlag;
  503. //Enhance mode enabled here
  504. if (info->IsMoxaMustChipFlag != MOXA_OTHER_UART) {
  505. ENABLE_MOXA_MUST_ENCHANCE_MODE(info->base);
  506. }
  507. info->flags = ASYNC_SHARE_IRQ;
  508. info->type = hwconf->uart_type;
  509. info->baud_base = hwconf->baud_base[i];
  510. info->MaxCanSetBaudRate = hwconf->MaxCanSetBaudRate[i];
  511. process_txrx_fifo(info);
  512. info->custom_divisor = hwconf->baud_base[i] * 16;
  513. info->close_delay = 5 * HZ / 10;
  514. info->closing_wait = 30 * HZ;
  515. INIT_WORK(&info->tqueue, mxser_do_softint, info);
  516. info->normal_termios = mxvar_sdriver->init_termios;
  517. init_waitqueue_head(&info->open_wait);
  518. init_waitqueue_head(&info->close_wait);
  519. init_waitqueue_head(&info->delta_msr_wait);
  520. memset(&info->mon_data, 0, sizeof(struct mxser_mon));
  521. info->err_shadow = 0;
  522. spin_lock_init(&info->slock);
  523. }
  524. /*
  525. * Allocate the IRQ if necessary
  526. */
  527. /* before set INT ISR, disable all int */
  528. for (i = 0; i < hwconf->ports; i++) {
  529. outb(inb(hwconf->ioaddr[i] + UART_IER) & 0xf0, hwconf->ioaddr[i] + UART_IER);
  530. }
  531. n = board * MXSER_PORTS_PER_BOARD;
  532. info = &mxvar_table[n];
  533. retval = request_irq(hwconf->irq, mxser_interrupt, IRQ_T(info), "mxser", info);
  534. if (retval) {
  535. printk(KERN_ERR "Board %d: %s", board, mxser_brdname[hwconf->board_type - 1]);
  536. printk(" Request irq fail,IRQ (%d) may be conflit with another device.\n", info->irq);
  537. return retval;
  538. }
  539. return 0;
  540. }
  541. static void mxser_getcfg(int board, struct mxser_hwconf *hwconf)
  542. {
  543. mxsercfg[board] = *hwconf;
  544. }
  545. #ifdef CONFIG_PCI
  546. static int mxser_get_PCI_conf(int busnum, int devnum, int board_type, struct mxser_hwconf *hwconf)
  547. {
  548. int i, j;
  549. // unsigned int val;
  550. unsigned int ioaddress;
  551. struct pci_dev *pdev = hwconf->pciInfo.pdev;
  552. //io address
  553. hwconf->board_type = board_type;
  554. hwconf->ports = mxser_numports[board_type - 1];
  555. ioaddress = pci_resource_start(pdev, 2);
  556. request_region(pci_resource_start(pdev, 2), pci_resource_len(pdev, 2), "mxser(IO)");
  557. for (i = 0; i < hwconf->ports; i++) {
  558. hwconf->ioaddr[i] = ioaddress + 8 * i;
  559. }
  560. //vector
  561. ioaddress = pci_resource_start(pdev, 3);
  562. request_region(pci_resource_start(pdev, 3), pci_resource_len(pdev, 3), "mxser(vector)");
  563. hwconf->vector = ioaddress;
  564. //irq
  565. hwconf->irq = hwconf->pciInfo.pdev->irq;
  566. hwconf->IsMoxaMustChipFlag = CheckIsMoxaMust(hwconf->ioaddr[0]);
  567. hwconf->uart_type = PORT_16550A;
  568. hwconf->vector_mask = 0;
  569. for (i = 0; i < hwconf->ports; i++) {
  570. for (j = 0; j < UART_INFO_NUM; j++) {
  571. if (Gpci_uart_info[j].type == hwconf->IsMoxaMustChipFlag) {
  572. hwconf->MaxCanSetBaudRate[i] = Gpci_uart_info[j].max_baud;
  573. //exception....CP-102
  574. if (board_type == MXSER_BOARD_CP102)
  575. hwconf->MaxCanSetBaudRate[i] = 921600;
  576. break;
  577. }
  578. }
  579. }
  580. if (hwconf->IsMoxaMustChipFlag == MOXA_MUST_MU860_HWID) {
  581. for (i = 0; i < hwconf->ports; i++) {
  582. if (i < 4)
  583. hwconf->opmode_ioaddr[i] = ioaddress + 4;
  584. else
  585. hwconf->opmode_ioaddr[i] = ioaddress + 0x0c;
  586. }
  587. outb(0, ioaddress + 4); // default set to RS232 mode
  588. outb(0, ioaddress + 0x0c); //default set to RS232 mode
  589. }
  590. for (i = 0; i < hwconf->ports; i++) {
  591. hwconf->vector_mask |= (1 << i);
  592. hwconf->baud_base[i] = 921600;
  593. }
  594. return (0);
  595. }
  596. #endif
  597. static int mxser_init(void)
  598. {
  599. int i, m, retval, b, n;
  600. int ret1;
  601. struct pci_dev *pdev = NULL;
  602. int index;
  603. unsigned char busnum, devnum;
  604. struct mxser_hwconf hwconf;
  605. mxvar_sdriver = alloc_tty_driver(MXSER_PORTS + 1);
  606. if (!mxvar_sdriver)
  607. return -ENOMEM;
  608. spin_lock_init(&gm_lock);
  609. for (i = 0; i < MXSER_BOARDS; i++) {
  610. mxsercfg[i].board_type = -1;
  611. }
  612. printk(KERN_INFO "MOXA Smartio/Industio family driver version %s\n", MXSER_VERSION);
  613. /* Initialize the tty_driver structure */
  614. memset(mxvar_sdriver, 0, sizeof(struct tty_driver));
  615. mxvar_sdriver->magic = TTY_DRIVER_MAGIC;
  616. mxvar_sdriver->name = "ttyM";
  617. mxvar_sdriver->major = ttymajor;
  618. mxvar_sdriver->minor_start = 0;
  619. mxvar_sdriver->num = MXSER_PORTS + 1;
  620. mxvar_sdriver->type = TTY_DRIVER_TYPE_SERIAL;
  621. mxvar_sdriver->subtype = SERIAL_TYPE_NORMAL;
  622. mxvar_sdriver->init_termios = tty_std_termios;
  623. mxvar_sdriver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;
  624. mxvar_sdriver->flags = TTY_DRIVER_REAL_RAW;
  625. tty_set_operations(mxvar_sdriver, &mxser_ops);
  626. mxvar_sdriver->ttys = mxvar_tty;
  627. mxvar_sdriver->termios = mxvar_termios;
  628. mxvar_sdriver->termios_locked = mxvar_termios_locked;
  629. mxvar_sdriver->open = mxser_open;
  630. mxvar_sdriver->close = mxser_close;
  631. mxvar_sdriver->write = mxser_write;
  632. mxvar_sdriver->put_char = mxser_put_char;
  633. mxvar_sdriver->flush_chars = mxser_flush_chars;
  634. mxvar_sdriver->write_room = mxser_write_room;
  635. mxvar_sdriver->chars_in_buffer = mxser_chars_in_buffer;
  636. mxvar_sdriver->flush_buffer = mxser_flush_buffer;
  637. mxvar_sdriver->ioctl = mxser_ioctl;
  638. mxvar_sdriver->throttle = mxser_throttle;
  639. mxvar_sdriver->unthrottle = mxser_unthrottle;
  640. mxvar_sdriver->set_termios = mxser_set_termios;
  641. mxvar_sdriver->stop = mxser_stop;
  642. mxvar_sdriver->start = mxser_start;
  643. mxvar_sdriver->hangup = mxser_hangup;
  644. mxvar_sdriver->break_ctl = mxser_rs_break;
  645. mxvar_sdriver->wait_until_sent = mxser_wait_until_sent;
  646. mxvar_diagflag = 0;
  647. memset(mxvar_table, 0, MXSER_PORTS * sizeof(struct mxser_struct));
  648. memset(&mxvar_log, 0, sizeof(struct mxser_log));
  649. memset(&mxser_msr, 0, sizeof(unsigned char) * (MXSER_PORTS + 1));
  650. memset(&mon_data_ext, 0, sizeof(struct mxser_mon_ext));
  651. memset(&mxser_set_baud_method, 0, sizeof(int) * (MXSER_PORTS + 1));
  652. memset(&hwconf, 0, sizeof(struct mxser_hwconf));
  653. m = 0;
  654. /* Start finding ISA boards here */
  655. for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
  656. int cap;
  657. if (!(cap = mxserBoardCAP[b]))
  658. continue;
  659. retval = mxser_get_ISA_conf(cap, &hwconf);
  660. if (retval != 0)
  661. printk(KERN_INFO "Found MOXA %s board (CAP=0x%x)\n", mxser_brdname[hwconf.board_type - 1], ioaddr[b]);
  662. if (retval <= 0) {
  663. if (retval == MXSER_ERR_IRQ)
  664. printk(KERN_ERR "Invalid interrupt number,board not configured\n");
  665. else if (retval == MXSER_ERR_IRQ_CONFLIT)
  666. printk(KERN_ERR "Invalid interrupt number,board not configured\n");
  667. else if (retval == MXSER_ERR_VECTOR)
  668. printk(KERN_ERR "Invalid interrupt vector,board not configured\n");
  669. else if (retval == MXSER_ERR_IOADDR)
  670. printk(KERN_ERR "Invalid I/O address,board not configured\n");
  671. continue;
  672. }
  673. hwconf.pciInfo.busNum = 0;
  674. hwconf.pciInfo.devNum = 0;
  675. hwconf.pciInfo.pdev = NULL;
  676. mxser_getcfg(m, &hwconf);
  677. //init mxsercfg first, or mxsercfg data is not correct on ISR.
  678. //mxser_initbrd will hook ISR.
  679. if (mxser_initbrd(m, &hwconf) < 0)
  680. continue;
  681. m++;
  682. }
  683. /* Start finding ISA boards from module arg */
  684. for (b = 0; b < MXSER_BOARDS && m < MXSER_BOARDS; b++) {
  685. int cap;
  686. if (!(cap = ioaddr[b]))
  687. continue;
  688. retval = mxser_get_ISA_conf(cap, &hwconf);
  689. if (retval != 0)
  690. printk(KERN_INFO "Found MOXA %s board (CAP=0x%x)\n", mxser_brdname[hwconf.board_type - 1], ioaddr[b]);
  691. if (retval <= 0) {
  692. if (retval == MXSER_ERR_IRQ)
  693. printk(KERN_ERR "Invalid interrupt number,board not configured\n");
  694. else if (retval == MXSER_ERR_IRQ_CONFLIT)
  695. printk(KERN_ERR "Invalid interrupt number,board not configured\n");
  696. else if (retval == MXSER_ERR_VECTOR)
  697. printk(KERN_ERR "Invalid interrupt vector,board not configured\n");
  698. else if (retval == MXSER_ERR_IOADDR)
  699. printk(KERN_ERR "Invalid I/O address,board not configured\n");
  700. continue;
  701. }
  702. hwconf.pciInfo.busNum = 0;
  703. hwconf.pciInfo.devNum = 0;
  704. hwconf.pciInfo.pdev = NULL;
  705. mxser_getcfg(m, &hwconf);
  706. //init mxsercfg first, or mxsercfg data is not correct on ISR.
  707. //mxser_initbrd will hook ISR.
  708. if (mxser_initbrd(m, &hwconf) < 0)
  709. continue;
  710. m++;
  711. }
  712. /* start finding PCI board here */
  713. #ifdef CONFIG_PCI
  714. n = (sizeof(mxser_pcibrds) / sizeof(mxser_pcibrds[0])) - 1;
  715. index = 0;
  716. b = 0;
  717. while (b < n) {
  718. pdev = pci_find_device(mxser_pcibrds[b].vendor, mxser_pcibrds[b].device, pdev);
  719. if (pdev == NULL) {
  720. b++;
  721. continue;
  722. }
  723. hwconf.pciInfo.busNum = busnum = pdev->bus->number;
  724. hwconf.pciInfo.devNum = devnum = PCI_SLOT(pdev->devfn) << 3;
  725. hwconf.pciInfo.pdev = pdev;
  726. printk(KERN_INFO "Found MOXA %s board(BusNo=%d,DevNo=%d)\n", mxser_brdname[(int) (mxser_pcibrds[b].driver_data) - 1], busnum, devnum >> 3);
  727. index++;
  728. if (m >= MXSER_BOARDS) {
  729. printk(KERN_ERR "Too many Smartio/Industio family boards find (maximum %d),board not configured\n", MXSER_BOARDS);
  730. } else {
  731. if (pci_enable_device(pdev)) {
  732. printk(KERN_ERR "Moxa SmartI/O PCI enable fail !\n");
  733. continue;
  734. }
  735. retval = mxser_get_PCI_conf(busnum, devnum, (int) mxser_pcibrds[b].driver_data, &hwconf);
  736. if (retval < 0) {
  737. if (retval == MXSER_ERR_IRQ)
  738. printk(KERN_ERR "Invalid interrupt number,board not configured\n");
  739. else if (retval == MXSER_ERR_IRQ_CONFLIT)
  740. printk(KERN_ERR "Invalid interrupt number,board not configured\n");
  741. else if (retval == MXSER_ERR_VECTOR)
  742. printk(KERN_ERR "Invalid interrupt vector,board not configured\n");
  743. else if (retval == MXSER_ERR_IOADDR)
  744. printk(KERN_ERR "Invalid I/O address,board not configured\n");
  745. continue;
  746. }
  747. mxser_getcfg(m, &hwconf);
  748. //init mxsercfg first, or mxsercfg data is not correct on ISR.
  749. //mxser_initbrd will hook ISR.
  750. if (mxser_initbrd(m, &hwconf) < 0)
  751. continue;
  752. m++;
  753. }
  754. }
  755. #endif
  756. ret1 = 0;
  757. if (!(ret1 = tty_register_driver(mxvar_sdriver))) {
  758. return 0;
  759. } else
  760. printk(KERN_ERR "Couldn't install MOXA Smartio/Industio family driver !\n");
  761. if (ret1) {
  762. for (i = 0; i < MXSER_BOARDS; i++) {
  763. if (mxsercfg[i].board_type == -1)
  764. continue;
  765. else {
  766. free_irq(mxsercfg[i].irq, &mxvar_table[i * MXSER_PORTS_PER_BOARD]);
  767. //todo: release io, vector
  768. }
  769. }
  770. return -1;
  771. }
  772. return (0);
  773. }
  774. static void mxser_do_softint(void *private_)
  775. {
  776. struct mxser_struct *info = (struct mxser_struct *) private_;
  777. struct tty_struct *tty;
  778. tty = info->tty;
  779. if (tty) {
  780. if (test_and_clear_bit(MXSER_EVENT_TXLOW, &info->event))
  781. tty_wakeup(tty);
  782. if (test_and_clear_bit(MXSER_EVENT_HANGUP, &info->event))
  783. tty_hangup(tty);
  784. }
  785. }
  786. static unsigned char mxser_get_msr(int baseaddr, int mode, int port, struct mxser_struct *info)
  787. {
  788. unsigned char status = 0;
  789. status = inb(baseaddr + UART_MSR);
  790. mxser_msr[port] &= 0x0F;
  791. mxser_msr[port] |= status;
  792. status = mxser_msr[port];
  793. if (mode)
  794. mxser_msr[port] = 0;
  795. return status;
  796. }
  797. /*
  798. * This routine is called whenever a serial port is opened. It
  799. * enables interrupts for a serial port, linking in its async structure into
  800. * the IRQ chain. It also performs the serial-specific
  801. * initialization for the tty structure.
  802. */
  803. static int mxser_open(struct tty_struct *tty, struct file *filp)
  804. {
  805. struct mxser_struct *info;
  806. int retval, line;
  807. line = tty->index;
  808. if (line == MXSER_PORTS)
  809. return 0;
  810. if (line < 0 || line > MXSER_PORTS)
  811. return -ENODEV;
  812. info = mxvar_table + line;
  813. if (!info->base)
  814. return (-ENODEV);
  815. tty->driver_data = info;
  816. info->tty = tty;
  817. /*
  818. * Start up serial port
  819. */
  820. retval = mxser_startup(info);
  821. if (retval)
  822. return (retval);
  823. retval = mxser_block_til_ready(tty, filp, info);
  824. if (retval)
  825. return (retval);
  826. info->count++;
  827. if ((info->count == 1) && (info->flags & ASYNC_SPLIT_TERMIOS)) {
  828. if (tty->driver->subtype == SERIAL_TYPE_NORMAL)
  829. *tty->termios = info->normal_termios;
  830. else
  831. *tty->termios = info->callout_termios;
  832. mxser_change_speed(info, NULL);
  833. }
  834. info->session = current->signal->session;
  835. info->pgrp = process_group(current);
  836. clear_bit(TTY_DONT_FLIP, &tty->flags);
  837. //status = mxser_get_msr(info->base, 0, info->port);
  838. //mxser_check_modem_status(info, status);
  839. /* unmark here for very high baud rate (ex. 921600 bps) used
  840. */
  841. tty->low_latency = 1;
  842. return 0;
  843. }
  844. /*
  845. * This routine is called when the serial port gets closed. First, we
  846. * wait for the last remaining data to be sent. Then, we unlink its
  847. * async structure from the interrupt chain if necessary, and we free
  848. * that IRQ if nothing is left in the chain.
  849. */
  850. static void mxser_close(struct tty_struct *tty, struct file *filp)
  851. {
  852. struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
  853. unsigned long timeout;
  854. unsigned long flags;
  855. struct tty_ldisc *ld;
  856. if (tty->index == MXSER_PORTS)
  857. return;
  858. if (!info)
  859. BUG();
  860. spin_lock_irqsave(&info->slock, flags);
  861. if (tty_hung_up_p(filp)) {
  862. spin_unlock_irqrestore(&info->slock, flags);
  863. return;
  864. }
  865. if ((tty->count == 1) && (info->count != 1)) {
  866. /*
  867. * Uh, oh. tty->count is 1, which means that the tty
  868. * structure will be freed. Info->count should always
  869. * be one in these conditions. If it's greater than
  870. * one, we've got real problems, since it means the
  871. * serial port won't be shutdown.
  872. */
  873. printk(KERN_ERR "mxser_close: bad serial port count; tty->count is 1, " "info->count is %d\n", info->count);
  874. info->count = 1;
  875. }
  876. if (--info->count < 0) {
  877. printk(KERN_ERR "mxser_close: bad serial port count for ttys%d: %d\n", info->port, info->count);
  878. info->count = 0;
  879. }
  880. if (info->count) {
  881. spin_unlock_irqrestore(&info->slock, flags);
  882. return;
  883. }
  884. info->flags |= ASYNC_CLOSING;
  885. spin_unlock_irqrestore(&info->slock, flags);
  886. /*
  887. * Save the termios structure, since this port may have
  888. * separate termios for callout and dialin.
  889. */
  890. if (info->flags & ASYNC_NORMAL_ACTIVE)
  891. info->normal_termios = *tty->termios;
  892. /*
  893. * Now we wait for the transmit buffer to clear; and we notify
  894. * the line discipline to only process XON/XOFF characters.
  895. */
  896. tty->closing = 1;
  897. if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
  898. tty_wait_until_sent(tty, info->closing_wait);
  899. /*
  900. * At this point we stop accepting input. To do this, we
  901. * disable the receive line status interrupts, and tell the
  902. * interrupt driver to stop checking the data ready bit in the
  903. * line status register.
  904. */
  905. info->IER &= ~UART_IER_RLSI;
  906. if (info->IsMoxaMustChipFlag)
  907. info->IER &= ~MOXA_MUST_RECV_ISR;
  908. /* by William
  909. info->read_status_mask &= ~UART_LSR_DR;
  910. */
  911. if (info->flags & ASYNC_INITIALIZED) {
  912. outb(info->IER, info->base + UART_IER);
  913. /*
  914. * Before we drop DTR, make sure the UART transmitter
  915. * has completely drained; this is especially
  916. * important if there is a transmit FIFO!
  917. */
  918. timeout = jiffies + HZ;
  919. while (!(inb(info->base + UART_LSR) & UART_LSR_TEMT)) {
  920. set_current_state(TASK_INTERRUPTIBLE);
  921. schedule_timeout(5);
  922. if (time_after(jiffies, timeout))
  923. break;
  924. }
  925. }
  926. mxser_shutdown(info);
  927. if (tty->driver->flush_buffer)
  928. tty->driver->flush_buffer(tty);
  929. ld = tty_ldisc_ref(tty);
  930. if (ld) {
  931. if(ld->flush_buffer)
  932. ld->flush_buffer(tty);
  933. tty_ldisc_deref(ld);
  934. }
  935. tty->closing = 0;
  936. info->event = 0;
  937. info->tty = NULL;
  938. if (info->blocked_open) {
  939. if (info->close_delay) {
  940. set_current_state(TASK_INTERRUPTIBLE);
  941. schedule_timeout(info->close_delay);
  942. }
  943. wake_up_interruptible(&info->open_wait);
  944. }
  945. info->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
  946. wake_up_interruptible(&info->close_wait);
  947. }
  948. static int mxser_write(struct tty_struct *tty, const unsigned char *buf, int count)
  949. {
  950. int c, total = 0;
  951. struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
  952. unsigned long flags;
  953. if (!tty || !info->xmit_buf)
  954. return (0);
  955. while (1) {
  956. c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1, SERIAL_XMIT_SIZE - info->xmit_head));
  957. if (c <= 0)
  958. break;
  959. memcpy(info->xmit_buf + info->xmit_head, buf, c);
  960. spin_lock_irqsave(&info->slock, flags);
  961. info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE - 1);
  962. info->xmit_cnt += c;
  963. spin_unlock_irqrestore(&info->slock, flags);
  964. buf += c;
  965. count -= c;
  966. total += c;
  967. }
  968. if (info->xmit_cnt && !tty->stopped && !(info->IER & UART_IER_THRI)) {
  969. if (!tty->hw_stopped || (info->type == PORT_16550A) || (info->IsMoxaMustChipFlag)) {
  970. spin_lock_irqsave(&info->slock, flags);
  971. info->IER |= UART_IER_THRI;
  972. outb(info->IER, info->base + UART_IER);
  973. spin_unlock_irqrestore(&info->slock, flags);
  974. }
  975. }
  976. return total;
  977. }
  978. static void mxser_put_char(struct tty_struct *tty, unsigned char ch)
  979. {
  980. struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
  981. unsigned long flags;
  982. if (!tty || !info->xmit_buf)
  983. return;
  984. if (info->xmit_cnt >= SERIAL_XMIT_SIZE - 1)
  985. return;
  986. spin_lock_irqsave(&info->slock, flags);
  987. info->xmit_buf[info->xmit_head++] = ch;
  988. info->xmit_head &= SERIAL_XMIT_SIZE - 1;
  989. info->xmit_cnt++;
  990. spin_unlock_irqrestore(&info->slock, flags);
  991. if (!tty->stopped && !(info->IER & UART_IER_THRI)) {
  992. if (!tty->hw_stopped || (info->type == PORT_16550A) || info->IsMoxaMustChipFlag) {
  993. spin_lock_irqsave(&info->slock, flags);
  994. info->IER |= UART_IER_THRI;
  995. outb(info->IER, info->base + UART_IER);
  996. spin_unlock_irqrestore(&info->slock, flags);
  997. }
  998. }
  999. }
  1000. static void mxser_flush_chars(struct tty_struct *tty)
  1001. {
  1002. struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
  1003. unsigned long flags;
  1004. if (info->xmit_cnt <= 0 || tty->stopped || !info->xmit_buf || (tty->hw_stopped && (info->type != PORT_16550A) && (!info->IsMoxaMustChipFlag)))
  1005. return;
  1006. spin_lock_irqsave(&info->slock, flags);
  1007. info->IER |= UART_IER_THRI;
  1008. outb(info->IER, info->base + UART_IER);
  1009. spin_unlock_irqrestore(&info->slock, flags);
  1010. }
  1011. static int mxser_write_room(struct tty_struct *tty)
  1012. {
  1013. struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
  1014. int ret;
  1015. ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
  1016. if (ret < 0)
  1017. ret = 0;
  1018. return (ret);
  1019. }
  1020. static int mxser_chars_in_buffer(struct tty_struct *tty)
  1021. {
  1022. struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
  1023. return info->xmit_cnt;
  1024. }
  1025. static void mxser_flush_buffer(struct tty_struct *tty)
  1026. {
  1027. struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
  1028. char fcr;
  1029. unsigned long flags;
  1030. spin_lock_irqsave(&info->slock, flags);
  1031. info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
  1032. /* below added by shinhay */
  1033. fcr = inb(info->base + UART_FCR);
  1034. outb((fcr | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT), info->base + UART_FCR);
  1035. outb(fcr, info->base + UART_FCR);
  1036. spin_unlock_irqrestore(&info->slock, flags);
  1037. /* above added by shinhay */
  1038. wake_up_interruptible(&tty->write_wait);
  1039. if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) && tty->ldisc.write_wakeup)
  1040. (tty->ldisc.write_wakeup) (tty);
  1041. }
  1042. static int mxser_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg)
  1043. {
  1044. struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
  1045. int retval;
  1046. struct async_icount cprev, cnow; /* kernel counter temps */
  1047. struct serial_icounter_struct __user *p_cuser;
  1048. unsigned long templ;
  1049. unsigned long flags;
  1050. void __user *argp = (void __user *)arg;
  1051. if (tty->index == MXSER_PORTS)
  1052. return (mxser_ioctl_special(cmd, argp));
  1053. // following add by Victor Yu. 01-05-2004
  1054. if (cmd == MOXA_SET_OP_MODE || cmd == MOXA_GET_OP_MODE) {
  1055. int opmode, p;
  1056. static unsigned char ModeMask[] = { 0xfc, 0xf3, 0xcf, 0x3f };
  1057. int shiftbit;
  1058. unsigned char val, mask;
  1059. p = info->port % 4;
  1060. if (cmd == MOXA_SET_OP_MODE) {
  1061. if (get_user(opmode, (int __user *) argp))
  1062. return -EFAULT;
  1063. if (opmode != RS232_MODE && opmode != RS485_2WIRE_MODE && opmode != RS422_MODE && opmode != RS485_4WIRE_MODE)
  1064. return -EFAULT;
  1065. mask = ModeMask[p];
  1066. shiftbit = p * 2;
  1067. val = inb(info->opmode_ioaddr);
  1068. val &= mask;
  1069. val |= (opmode << shiftbit);
  1070. outb(val, info->opmode_ioaddr);
  1071. } else {
  1072. shiftbit = p * 2;
  1073. opmode = inb(info->opmode_ioaddr) >> shiftbit;
  1074. opmode &= OP_MODE_MASK;
  1075. if (copy_to_user(argp, &opmode, sizeof(int)))
  1076. return -EFAULT;
  1077. }
  1078. return 0;
  1079. }
  1080. // above add by Victor Yu. 01-05-2004
  1081. if ((cmd != TIOCGSERIAL) && (cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
  1082. if (tty->flags & (1 << TTY_IO_ERROR))
  1083. return (-EIO);
  1084. }
  1085. switch (cmd) {
  1086. case TCSBRK: /* SVID version: non-zero arg --> no break */
  1087. retval = tty_check_change(tty);
  1088. if (retval)
  1089. return (retval);
  1090. tty_wait_until_sent(tty, 0);
  1091. if (!arg)
  1092. mxser_send_break(info, HZ / 4); /* 1/4 second */
  1093. return (0);
  1094. case TCSBRKP: /* support for POSIX tcsendbreak() */
  1095. retval = tty_check_change(tty);
  1096. if (retval)
  1097. return (retval);
  1098. tty_wait_until_sent(tty, 0);
  1099. mxser_send_break(info, arg ? arg * (HZ / 10) : HZ / 4);
  1100. return (0);
  1101. case TIOCGSOFTCAR:
  1102. return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *) argp);
  1103. case TIOCSSOFTCAR:
  1104. if (get_user(templ, (unsigned long __user *) argp))
  1105. return -EFAULT;
  1106. arg = templ;
  1107. tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) | (arg ? CLOCAL : 0));
  1108. return (0);
  1109. case TIOCGSERIAL:
  1110. return mxser_get_serial_info(info, argp);
  1111. case TIOCSSERIAL:
  1112. return mxser_set_serial_info(info, argp);
  1113. case TIOCSERGETLSR: /* Get line status register */
  1114. return mxser_get_lsr_info(info, argp);
  1115. /*
  1116. * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
  1117. * - mask passed in arg for lines of interest
  1118. * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
  1119. * Caller should use TIOCGICOUNT to see which one it was
  1120. */
  1121. case TIOCMIWAIT:{
  1122. DECLARE_WAITQUEUE(wait, current);
  1123. int ret;
  1124. spin_lock_irqsave(&info->slock, flags);
  1125. cprev = info->icount; /* note the counters on entry */
  1126. spin_unlock_irqrestore(&info->slock, flags);
  1127. add_wait_queue(&info->delta_msr_wait, &wait);
  1128. while (1) {
  1129. spin_lock_irqsave(&info->slock, flags);
  1130. cnow = info->icount; /* atomic copy */
  1131. spin_unlock_irqrestore(&info->slock, flags);
  1132. set_current_state(TASK_INTERRUPTIBLE);
  1133. if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) || ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) || ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) || ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
  1134. ret = 0;
  1135. break;
  1136. }
  1137. /* see if a signal did it */
  1138. if (signal_pending(current)) {
  1139. ret = -ERESTARTSYS;
  1140. break;
  1141. }
  1142. cprev = cnow;
  1143. }
  1144. current->state = TASK_RUNNING;
  1145. remove_wait_queue(&info->delta_msr_wait, &wait);
  1146. break;
  1147. }
  1148. /* NOTREACHED */
  1149. /*
  1150. * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
  1151. * Return: write counters to the user passed counter struct
  1152. * NB: both 1->0 and 0->1 transitions are counted except for
  1153. * RI where only 0->1 is counted.
  1154. */
  1155. case TIOCGICOUNT:
  1156. spin_lock_irqsave(&info->slock, flags);
  1157. cnow = info->icount;
  1158. spin_unlock_irqrestore(&info->slock, flags);
  1159. p_cuser = argp;
  1160. /* modified by casper 1/11/2000 */
  1161. if (put_user(cnow.frame, &p_cuser->frame))
  1162. return -EFAULT;
  1163. if (put_user(cnow.brk, &p_cuser->brk))
  1164. return -EFAULT;
  1165. if (put_user(cnow.overrun, &p_cuser->overrun))
  1166. return -EFAULT;
  1167. if (put_user(cnow.buf_overrun, &p_cuser->buf_overrun))
  1168. return -EFAULT;
  1169. if (put_user(cnow.parity, &p_cuser->parity))
  1170. return -EFAULT;
  1171. if (put_user(cnow.rx, &p_cuser->rx))
  1172. return -EFAULT;
  1173. if (put_user(cnow.tx, &p_cuser->tx))
  1174. return -EFAULT;
  1175. put_user(cnow.cts, &p_cuser->cts);
  1176. put_user(cnow.dsr, &p_cuser->dsr);
  1177. put_user(cnow.rng, &p_cuser->rng);
  1178. put_user(cnow.dcd, &p_cuser->dcd);
  1179. /* */
  1180. return 0;
  1181. case MOXA_HighSpeedOn:
  1182. return put_user(info->baud_base != 115200 ? 1 : 0, (int __user *) argp);
  1183. case MOXA_SDS_RSTICOUNTER:{
  1184. info->mon_data.rxcnt = 0;
  1185. info->mon_data.txcnt = 0;
  1186. return 0;
  1187. }
  1188. // (above) added by James.
  1189. case MOXA_ASPP_SETBAUD:{
  1190. long baud;
  1191. if (get_user(baud, (long __user *) argp))
  1192. return -EFAULT;
  1193. mxser_set_baud(info, baud);
  1194. return 0;
  1195. }
  1196. case MOXA_ASPP_GETBAUD:
  1197. if (copy_to_user(argp, &info->realbaud, sizeof(long)))
  1198. return -EFAULT;
  1199. return 0;
  1200. case MOXA_ASPP_OQUEUE:{
  1201. int len, lsr;
  1202. len = mxser_chars_in_buffer(tty);
  1203. lsr = inb(info->base + UART_LSR) & UART_LSR_TEMT;
  1204. len += (lsr ? 0 : 1);
  1205. if (copy_to_user(argp, &len, sizeof(int)))
  1206. return -EFAULT;
  1207. return 0;
  1208. }
  1209. case MOXA_ASPP_MON:{
  1210. int mcr, status;
  1211. // info->mon_data.ser_param = tty->termios->c_cflag;
  1212. status = mxser_get_msr(info->base, 1, info->port, info);
  1213. mxser_check_modem_status(info, status);
  1214. mcr = inb(info->base + UART_MCR);
  1215. if (mcr & MOXA_MUST_MCR_XON_FLAG)
  1216. info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFHOLD;
  1217. else
  1218. info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFHOLD;
  1219. if (mcr & MOXA_MUST_MCR_TX_XON)
  1220. info->mon_data.hold_reason &= ~NPPI_NOTIFY_XOFFXENT;
  1221. else
  1222. info->mon_data.hold_reason |= NPPI_NOTIFY_XOFFXENT;
  1223. if (info->tty->hw_stopped)
  1224. info->mon_data.hold_reason |= NPPI_NOTIFY_CTSHOLD;
  1225. else
  1226. info->mon_data.hold_reason &= ~NPPI_NOTIFY_CTSHOLD;
  1227. if (copy_to_user(argp, &info->mon_data, sizeof(struct mxser_mon)))
  1228. return -EFAULT;
  1229. return 0;
  1230. }
  1231. case MOXA_ASPP_LSTATUS:{
  1232. if (copy_to_user(argp, &info->err_shadow, sizeof(unsigned char)))
  1233. return -EFAULT;
  1234. info->err_shadow = 0;
  1235. return 0;
  1236. }
  1237. case MOXA_SET_BAUD_METHOD:{
  1238. int method;
  1239. if (get_user(method, (int __user *) argp))
  1240. return -EFAULT;
  1241. mxser_set_baud_method[info->port] = method;
  1242. if (copy_to_user(argp, &method, sizeof(int)))
  1243. return -EFAULT;
  1244. return 0;
  1245. }
  1246. default:
  1247. return -ENOIOCTLCMD;
  1248. }
  1249. return 0;
  1250. }
  1251. #ifndef CMSPAR
  1252. #define CMSPAR 010000000000
  1253. #endif
  1254. static int mxser_ioctl_special(unsigned int cmd, void __user *argp)
  1255. {
  1256. int i, result, status;
  1257. switch (cmd) {
  1258. case MOXA_GET_CONF:
  1259. if (copy_to_user(argp, mxsercfg, sizeof(struct mxser_hwconf) * 4))
  1260. return -EFAULT;
  1261. return 0;
  1262. case MOXA_GET_MAJOR:
  1263. if (copy_to_user(argp, &ttymajor, sizeof(int)))
  1264. return -EFAULT;
  1265. return 0;
  1266. case MOXA_GET_CUMAJOR:
  1267. if (copy_to_user(argp, &calloutmajor, sizeof(int)))
  1268. return -EFAULT;
  1269. return 0;
  1270. case MOXA_CHKPORTENABLE:
  1271. result = 0;
  1272. for (i = 0; i < MXSER_PORTS; i++) {
  1273. if (mxvar_table[i].base)
  1274. result |= (1 << i);
  1275. }
  1276. return put_user(result, (unsigned long __user *) argp);
  1277. case MOXA_GETDATACOUNT:
  1278. if (copy_to_user(argp, &mxvar_log, sizeof(mxvar_log)))
  1279. return -EFAULT;
  1280. return (0);
  1281. case MOXA_GETMSTATUS:
  1282. for (i = 0; i < MXSER_PORTS; i++) {
  1283. GMStatus[i].ri = 0;
  1284. if (!mxvar_table[i].base) {
  1285. GMStatus[i].dcd = 0;
  1286. GMStatus[i].dsr = 0;
  1287. GMStatus[i].cts = 0;
  1288. continue;
  1289. }
  1290. if (!mxvar_table[i].tty || !mxvar_table[i].tty->termios)
  1291. GMStatus[i].cflag = mxvar_table[i].normal_termios.c_cflag;
  1292. else
  1293. GMStatus[i].cflag = mxvar_table[i].tty->termios->c_cflag;
  1294. status = inb(mxvar_table[i].base + UART_MSR);
  1295. if (status & 0x80 /*UART_MSR_DCD */ )
  1296. GMStatus[i].dcd = 1;
  1297. else
  1298. GMStatus[i].dcd = 0;
  1299. if (status & 0x20 /*UART_MSR_DSR */ )
  1300. GMStatus[i].dsr = 1;
  1301. else
  1302. GMStatus[i].dsr = 0;
  1303. if (status & 0x10 /*UART_MSR_CTS */ )
  1304. GMStatus[i].cts = 1;
  1305. else
  1306. GMStatus[i].cts = 0;
  1307. }
  1308. if (copy_to_user(argp, GMStatus, sizeof(struct mxser_mstatus) * MXSER_PORTS))
  1309. return -EFAULT;
  1310. return 0;
  1311. case MOXA_ASPP_MON_EXT:{
  1312. int status;
  1313. int opmode, p;
  1314. int shiftbit;
  1315. unsigned cflag, iflag;
  1316. for (i = 0; i < MXSER_PORTS; i++) {
  1317. if (!mxvar_table[i].base)
  1318. continue;
  1319. status = mxser_get_msr(mxvar_table[i].base, 0, i, &(mxvar_table[i]));
  1320. // mxser_check_modem_status(&mxvar_table[i], status);
  1321. if (status & UART_MSR_TERI)
  1322. mxvar_table[i].icount.rng++;
  1323. if (status & UART_MSR_DDSR)
  1324. mxvar_table[i].icount.dsr++;
  1325. if (status & UART_MSR_DDCD)
  1326. mxvar_table[i].icount.dcd++;
  1327. if (status & UART_MSR_DCTS)
  1328. mxvar_table[i].icount.cts++;
  1329. mxvar_table[i].mon_data.modem_status = status;
  1330. mon_data_ext.rx_cnt[i] = mxvar_table[i].mon_data.rxcnt;
  1331. mon_data_ext.tx_cnt[i] = mxvar_table[i].mon_data.txcnt;
  1332. mon_data_ext.up_rxcnt[i] = mxvar_table[i].mon_data.up_rxcnt;
  1333. mon_data_ext.up_txcnt[i] = mxvar_table[i].mon_data.up_txcnt;
  1334. mon_data_ext.modem_status[i] = mxvar_table[i].mon_data.modem_status;
  1335. mon_data_ext.baudrate[i] = mxvar_table[i].realbaud;
  1336. if (!mxvar_table[i].tty || !mxvar_table[i].tty->termios) {
  1337. cflag = mxvar_table[i].normal_termios.c_cflag;
  1338. iflag = mxvar_table[i].normal_termios.c_iflag;
  1339. } else {
  1340. cflag = mxvar_table[i].tty->termios->c_cflag;
  1341. iflag = mxvar_table[i].tty->termios->c_iflag;
  1342. }
  1343. mon_data_ext.databits[i] = cflag & CSIZE;
  1344. mon_data_ext.stopbits[i] = cflag & CSTOPB;
  1345. mon_data_ext.parity[i] = cflag & (PARENB | PARODD | CMSPAR);
  1346. mon_data_ext.flowctrl[i] = 0x00;
  1347. if (cflag & CRTSCTS)
  1348. mon_data_ext.flowctrl[i] |= 0x03;
  1349. if (iflag & (IXON | IXOFF))
  1350. mon_data_ext.flowctrl[i] |= 0x0C;
  1351. if (mxvar_table[i].type == PORT_16550A)
  1352. mon_data_ext.fifo[i] = 1;
  1353. else
  1354. mon_data_ext.fifo[i] = 0;
  1355. p = i % 4;
  1356. shiftbit = p * 2;
  1357. opmode = inb(mxvar_table[i].opmode_ioaddr) >> shiftbit;
  1358. opmode &= OP_MODE_MASK;
  1359. mon_data_ext.iftype[i] = opmode;
  1360. }
  1361. if (copy_to_user(argp, &mon_data_ext, sizeof(struct mxser_mon_ext)))
  1362. return -EFAULT;
  1363. return 0;
  1364. }
  1365. default:
  1366. return -ENOIOCTLCMD;
  1367. }
  1368. return 0;
  1369. }
  1370. static void mxser_stoprx(struct tty_struct *tty)
  1371. {
  1372. struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
  1373. //unsigned long flags;
  1374. info->ldisc_stop_rx = 1;
  1375. if (I_IXOFF(tty)) {
  1376. //MX_LOCK(&info->slock);
  1377. // following add by Victor Yu. 09-02-2002
  1378. if (info->IsMoxaMustChipFlag) {
  1379. info->IER &= ~MOXA_MUST_RECV_ISR;
  1380. outb(info->IER, info->base + UART_IER);
  1381. } else {
  1382. // above add by Victor Yu. 09-02-2002
  1383. info->x_char = STOP_CHAR(tty);
  1384. // outb(info->IER, 0); // mask by Victor Yu. 09-02-2002
  1385. outb(0, info->base + UART_IER);
  1386. info->IER |= UART_IER_THRI;
  1387. outb(info->IER, info->base + UART_IER); /* force Tx interrupt */
  1388. } // add by Victor Yu. 09-02-2002
  1389. //MX_UNLOCK(&info->slock);
  1390. }
  1391. if (info->tty->termios->c_cflag & CRTSCTS) {
  1392. //MX_LOCK(&info->slock);
  1393. info->MCR &= ~UART_MCR_RTS;
  1394. outb(info->MCR, info->base + UART_MCR);
  1395. //MX_UNLOCK(&info->slock);
  1396. }
  1397. }
  1398. static void mxser_startrx(struct tty_struct *tty)
  1399. {
  1400. struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
  1401. //unsigned long flags;
  1402. info->ldisc_stop_rx = 0;
  1403. if (I_IXOFF(tty)) {
  1404. if (info->x_char)
  1405. info->x_char = 0;
  1406. else {
  1407. //MX_LOCK(&info->slock);
  1408. // following add by Victor Yu. 09-02-2002
  1409. if (info->IsMoxaMustChipFlag) {
  1410. info->IER |= MOXA_MUST_RECV_ISR;
  1411. outb(info->IER, info->base + UART_IER);
  1412. } else {
  1413. // above add by Victor Yu. 09-02-2002
  1414. info->x_char = START_CHAR(tty);
  1415. // outb(info->IER, 0); // mask by Victor Yu. 09-02-2002
  1416. outb(0, info->base + UART_IER); // add by Victor Yu. 09-02-2002
  1417. info->IER |= UART_IER_THRI; /* force Tx interrupt */
  1418. outb(info->IER, info->base + UART_IER);
  1419. } // add by Victor Yu. 09-02-2002
  1420. //MX_UNLOCK(&info->slock);
  1421. }
  1422. }
  1423. if (info->tty->termios->c_cflag & CRTSCTS) {
  1424. //MX_LOCK(&info->slock);
  1425. info->MCR |= UART_MCR_RTS;
  1426. outb(info->MCR, info->base + UART_MCR);
  1427. //MX_UNLOCK(&info->slock);
  1428. }
  1429. }
  1430. /*
  1431. * This routine is called by the upper-layer tty layer to signal that
  1432. * incoming characters should be throttled.
  1433. */
  1434. static void mxser_throttle(struct tty_struct *tty)
  1435. {
  1436. //struct mxser_struct *info = (struct mxser_struct *)tty->driver_data;
  1437. //unsigned long flags;
  1438. //MX_LOCK(&info->slock);
  1439. mxser_stoprx(tty);
  1440. //MX_UNLOCK(&info->slock);
  1441. }
  1442. static void mxser_unthrottle(struct tty_struct *tty)
  1443. {
  1444. //struct mxser_struct *info = (struct mxser_struct *)tty->driver_data;
  1445. //unsigned long flags;
  1446. //MX_LOCK(&info->slock);
  1447. mxser_startrx(tty);
  1448. //MX_UNLOCK(&info->slock);
  1449. }
  1450. static void mxser_set_termios(struct tty_struct *tty, struct termios *old_termios)
  1451. {
  1452. struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
  1453. unsigned long flags;
  1454. if ((tty->termios->c_cflag != old_termios->c_cflag) || (RELEVANT_IFLAG(tty->termios->c_iflag) != RELEVANT_IFLAG(old_termios->c_iflag))) {
  1455. mxser_change_speed(info, old_termios);
  1456. if ((old_termios->c_cflag & CRTSCTS) && !(tty->termios->c_cflag & CRTSCTS)) {
  1457. tty->hw_stopped = 0;
  1458. mxser_start(tty);
  1459. }
  1460. }
  1461. /* Handle sw stopped */
  1462. if ((old_termios->c_iflag & IXON) && !(tty->termios->c_iflag & IXON)) {
  1463. tty->stopped = 0;
  1464. // following add by Victor Yu. 09-02-2002
  1465. if (info->IsMoxaMustChipFlag) {
  1466. spin_lock_irqsave(&info->slock, flags);
  1467. DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->base);
  1468. spin_unlock_irqrestore(&info->slock, flags);
  1469. }
  1470. // above add by Victor Yu. 09-02-2002
  1471. mxser_start(tty);
  1472. }
  1473. }
  1474. /*
  1475. * mxser_stop() and mxser_start()
  1476. *
  1477. * This routines are called before setting or resetting tty->stopped.
  1478. * They enable or disable transmitter interrupts, as necessary.
  1479. */
  1480. static void mxser_stop(struct tty_struct *tty)
  1481. {
  1482. struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
  1483. unsigned long flags;
  1484. spin_lock_irqsave(&info->slock, flags);
  1485. if (info->IER & UART_IER_THRI) {
  1486. info->IER &= ~UART_IER_THRI;
  1487. outb(info->IER, info->base + UART_IER);
  1488. }
  1489. spin_unlock_irqrestore(&info->slock, flags);
  1490. }
  1491. static void mxser_start(struct tty_struct *tty)
  1492. {
  1493. struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
  1494. unsigned long flags;
  1495. spin_lock_irqsave(&info->slock, flags);
  1496. if (info->xmit_cnt && info->xmit_buf && !(info->IER & UART_IER_THRI)) {
  1497. info->IER |= UART_IER_THRI;
  1498. outb(info->IER, info->base + UART_IER);
  1499. }
  1500. spin_unlock_irqrestore(&info->slock, flags);
  1501. }
  1502. /*
  1503. * mxser_wait_until_sent() --- wait until the transmitter is empty
  1504. */
  1505. static void mxser_wait_until_sent(struct tty_struct *tty, int timeout)
  1506. {
  1507. struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
  1508. unsigned long orig_jiffies, char_time;
  1509. int lsr;
  1510. if (info->type == PORT_UNKNOWN)
  1511. return;
  1512. if (info->xmit_fifo_size == 0)
  1513. return; /* Just in case.... */
  1514. orig_jiffies = jiffies;
  1515. /*
  1516. * Set the check interval to be 1/5 of the estimated time to
  1517. * send a single character, and make it at least 1. The check
  1518. * interval should also be less than the timeout.
  1519. *
  1520. * Note: we have to use pretty tight timings here to satisfy
  1521. * the NIST-PCTS.
  1522. */
  1523. char_time = (info->timeout - HZ / 50) / info->xmit_fifo_size;
  1524. char_time = char_time / 5;
  1525. if (char_time == 0)
  1526. char_time = 1;
  1527. if (timeout && timeout < char_time)
  1528. char_time = timeout;
  1529. /*
  1530. * If the transmitter hasn't cleared in twice the approximate
  1531. * amount of time to send the entire FIFO, it probably won't
  1532. * ever clear. This assumes the UART isn't doing flow
  1533. * control, which is currently the case. Hence, if it ever
  1534. * takes longer than info->timeout, this is probably due to a
  1535. * UART bug of some kind. So, we clamp the timeout parameter at
  1536. * 2*info->timeout.
  1537. */
  1538. if (!timeout || timeout > 2 * info->timeout)
  1539. timeout = 2 * info->timeout;
  1540. #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
  1541. printk(KERN_DEBUG "In rs_wait_until_sent(%d) check=%lu...", timeout, char_time);
  1542. printk("jiff=%lu...", jiffies);
  1543. #endif
  1544. while (!((lsr = inb(info->base + UART_LSR)) & UART_LSR_TEMT)) {
  1545. #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
  1546. printk("lsr = %d (jiff=%lu)...", lsr, jiffies);
  1547. #endif
  1548. set_current_state(TASK_INTERRUPTIBLE);
  1549. schedule_timeout(char_time);
  1550. if (signal_pending(current))
  1551. break;
  1552. if (timeout && time_after(jiffies, orig_jiffies + timeout))
  1553. break;
  1554. }
  1555. set_current_state(TASK_RUNNING);
  1556. #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
  1557. printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
  1558. #endif
  1559. }
  1560. /*
  1561. * This routine is called by tty_hangup() when a hangup is signaled.
  1562. */
  1563. void mxser_hangup(struct tty_struct *tty)
  1564. {
  1565. struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
  1566. mxser_flush_buffer(tty);
  1567. mxser_shutdown(info);
  1568. info->event = 0;
  1569. info->count = 0;
  1570. info->flags &= ~ASYNC_NORMAL_ACTIVE;
  1571. info->tty = NULL;
  1572. wake_up_interruptible(&info->open_wait);
  1573. }
  1574. // added by James 03-12-2004.
  1575. /*
  1576. * mxser_rs_break() --- routine which turns the break handling on or off
  1577. */
  1578. static void mxser_rs_break(struct tty_struct *tty, int break_state)
  1579. {
  1580. struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
  1581. unsigned long flags;
  1582. spin_lock_irqsave(&info->slock, flags);
  1583. if (break_state == -1)
  1584. outb(inb(info->base + UART_LCR) | UART_LCR_SBC, info->base + UART_LCR);
  1585. else
  1586. outb(inb(info->base + UART_LCR) & ~UART_LCR_SBC, info->base + UART_LCR);
  1587. spin_unlock_irqrestore(&info->slock, flags);
  1588. }
  1589. // (above) added by James.
  1590. /*
  1591. * This is the serial driver's generic interrupt routine
  1592. */
  1593. static irqreturn_t mxser_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  1594. {
  1595. int status, iir, i;
  1596. struct mxser_struct *info;
  1597. struct mxser_struct *port;
  1598. int max, irqbits, bits, msr;
  1599. int pass_counter = 0;
  1600. int handled = IRQ_NONE;
  1601. port = NULL;
  1602. //spin_lock(&gm_lock);
  1603. for (i = 0; i < MXSER_BOARDS; i++) {
  1604. if (dev_id == &(mxvar_table[i * MXSER_PORTS_PER_BOARD])) {
  1605. port = dev_id;
  1606. break;
  1607. }
  1608. }
  1609. if (i == MXSER_BOARDS) {
  1610. goto irq_stop;
  1611. }
  1612. if (port == 0) {
  1613. goto irq_stop;
  1614. }
  1615. max = mxser_numports[mxsercfg[i].board_type - 1];
  1616. while (1) {
  1617. irqbits = inb(port->vector) & port->vectormask;
  1618. if (irqbits == port->vectormask) {
  1619. break;
  1620. }
  1621. handled = IRQ_HANDLED;
  1622. for (i = 0, bits = 1; i < max; i++, irqbits |= bits, bits <<= 1) {
  1623. if (irqbits == port->vectormask) {
  1624. break;
  1625. }
  1626. if (bits & irqbits)
  1627. continue;
  1628. info = port + i;
  1629. // following add by Victor Yu. 09-13-2002
  1630. iir = inb(info->base + UART_IIR);
  1631. if (iir & UART_IIR_NO_INT)
  1632. continue;
  1633. iir &= MOXA_MUST_IIR_MASK;
  1634. if (!info->tty) {
  1635. status = inb(info->base + UART_LSR);
  1636. outb(0x27, info->base + UART_FCR);
  1637. inb(info->base + UART_MSR);
  1638. continue;
  1639. }
  1640. // above add by Victor Yu. 09-13-2002
  1641. /*
  1642. if ( info->tty->flip.count < TTY_FLIPBUF_SIZE/4 ){
  1643. info->IER |= MOXA_MUST_RECV_ISR;
  1644. outb(info->IER, info->base + UART_IER);
  1645. }
  1646. */
  1647. /* mask by Victor Yu. 09-13-2002
  1648. if ( !info->tty ||
  1649. (inb(info->base + UART_IIR) & UART_IIR_NO_INT) )
  1650. continue;
  1651. */
  1652. /* mask by Victor Yu. 09-02-2002
  1653. status = inb(info->base + UART_LSR) & info->read_status_mask;
  1654. */
  1655. // following add by Victor Yu. 09-02-2002
  1656. status = inb(info->base + UART_LSR);
  1657. if (status & UART_LSR_PE) {
  1658. info->err_shadow |= NPPI_NOTIFY_PARITY;
  1659. }
  1660. if (status & UART_LSR_FE) {
  1661. info->err_shadow |= NPPI_NOTIFY_FRAMING;
  1662. }
  1663. if (status & UART_LSR_OE) {
  1664. info->err_shadow |= NPPI_NOTIFY_HW_OVERRUN;
  1665. }
  1666. if (status & UART_LSR_BI)
  1667. info->err_shadow |= NPPI_NOTIFY_BREAK;
  1668. if (info->IsMoxaMustChipFlag) {
  1669. /*
  1670. if ( (status & 0x02) && !(status & 0x01) ) {
  1671. outb(info->base+UART_FCR, 0x23);
  1672. continue;
  1673. }
  1674. */
  1675. if (iir == MOXA_MUST_IIR_GDA || iir == MOXA_MUST_IIR_RDA || iir == MOXA_MUST_IIR_RTO || iir == MOXA_MUST_IIR_LSR)
  1676. mxser_receive_chars(info, &status);
  1677. } else {
  1678. // above add by Victor Yu. 09-02-2002
  1679. status &= info->read_status_mask;
  1680. if (status & UART_LSR_DR)
  1681. mxser_receive_chars(info, &status);
  1682. }
  1683. msr = inb(info->base + UART_MSR);
  1684. if (msr & UART_MSR_ANY_DELTA) {
  1685. mxser_check_modem_status(info, msr);
  1686. }
  1687. // following add by Victor Yu. 09-13-2002
  1688. if (info->IsMoxaMustChipFlag) {
  1689. if ((iir == 0x02) && (status & UART_LSR_THRE)) {
  1690. mxser_transmit_chars(info);
  1691. }
  1692. } else {
  1693. // above add by Victor Yu. 09-13-2002
  1694. if (status & UART_LSR_THRE) {
  1695. /* 8-2-99 by William
  1696. if ( info->x_char || (info->xmit_cnt > 0) )
  1697. */
  1698. mxser_transmit_chars(info);
  1699. }
  1700. }
  1701. }
  1702. if (pass_counter++ > MXSER_ISR_PASS_LIMIT) {
  1703. break; /* Prevent infinite loops */
  1704. }
  1705. }
  1706. irq_stop:
  1707. //spin_unlock(&gm_lock);
  1708. return handled;
  1709. }
  1710. static void mxser_receive_chars(struct mxser_struct *info, int *status)
  1711. {
  1712. struct tty_struct *tty = info->tty;
  1713. unsigned char ch, gdl;
  1714. int ignored = 0;
  1715. int cnt = 0;
  1716. unsigned char *cp;
  1717. char *fp;
  1718. int count;
  1719. int recv_room;
  1720. int max = 256;
  1721. unsigned long flags;
  1722. spin_lock_irqsave(&info->slock, flags);
  1723. recv_room = tty->ldisc.receive_room(tty);
  1724. if ((recv_room == 0) && (!info->ldisc_stop_rx)) {
  1725. //mxser_throttle(tty);
  1726. mxser_stoprx(tty);
  1727. //return;
  1728. }
  1729. cp = tty->flip.char_buf;
  1730. fp = tty->flip.flag_buf;
  1731. count = 0;
  1732. // following add by Victor Yu. 09-02-2002
  1733. if (info->IsMoxaMustChipFlag != MOXA_OTHER_UART) {
  1734. if (*status & UART_LSR_SPECIAL) {
  1735. goto intr_old;
  1736. }
  1737. // following add by Victor Yu. 02-11-2004
  1738. if (info->IsMoxaMustChipFlag == MOXA_MUST_MU860_HWID && (*status & MOXA_MUST_LSR_RERR))
  1739. goto intr_old;
  1740. // above add by Victor Yu. 02-14-2004
  1741. if (*status & MOXA_MUST_LSR_RERR)
  1742. goto intr_old;
  1743. gdl = inb(info->base + MOXA_MUST_GDL_REGISTER);
  1744. if (info->IsMoxaMustChipFlag == MOXA_MUST_MU150_HWID) // add by Victor Yu. 02-11-2004
  1745. gdl &= MOXA_MUST_GDL_MASK;
  1746. if (gdl >= recv_room) {
  1747. if (!info->ldisc_stop_rx) {
  1748. //mxser_throttle(tty);
  1749. mxser_stoprx(tty);
  1750. }
  1751. //return;
  1752. }
  1753. while (gdl--) {
  1754. ch = inb(info->base + UART_RX);
  1755. count++;
  1756. *cp++ = ch;
  1757. *fp++ = 0;
  1758. cnt++;
  1759. /*
  1760. if((count>=HI_WATER) && (info->stop_rx==0)){
  1761. mxser_stoprx(tty);
  1762. info->stop_rx=1;
  1763. break;
  1764. } */
  1765. }
  1766. goto end_intr;
  1767. }
  1768. intr_old:
  1769. // above add by Victor Yu. 09-02-2002
  1770. do {
  1771. if (max-- < 0)
  1772. break;
  1773. /*
  1774. if((count>=HI_WATER) && (info->stop_rx==0)){
  1775. mxser_stoprx(tty);
  1776. info->stop_rx=1;
  1777. break;
  1778. }
  1779. */
  1780. ch = inb(info->base + UART_RX);
  1781. // following add by Victor Yu. 09-02-2002
  1782. if (info->IsMoxaMustChipFlag && (*status & UART_LSR_OE) /*&& !(*status&UART_LSR_DR) */ )
  1783. outb(0x23, info->base + UART_FCR);
  1784. *status &= info->read_status_mask;
  1785. // above add by Victor Yu. 09-02-2002
  1786. if (*status & info->ignore_status_mask) {
  1787. if (++ignored > 100)
  1788. break;
  1789. } else {
  1790. count++;
  1791. if (*status & UART_LSR_SPECIAL) {
  1792. if (*status & UART_LSR_BI) {
  1793. *fp++ = TTY_BREAK;
  1794. /* added by casper 1/11/2000 */
  1795. info->icount.brk++;
  1796. /* */
  1797. if (info->flags & ASYNC_SAK)
  1798. do_SAK(tty);
  1799. } else if (*status & UART_LSR_PE) {
  1800. *fp++ = TTY_PARITY;
  1801. /* added by casper 1/11/2000 */
  1802. info->icount.parity++;
  1803. /* */
  1804. } else if (*status & UART_LSR_FE) {
  1805. *fp++ = TTY_FRAME;
  1806. /* added by casper 1/11/2000 */
  1807. info->icount.frame++;
  1808. /* */
  1809. } else if (*status & UART_LSR_OE) {
  1810. *fp++ = TTY_OVERRUN;
  1811. /* added by casper 1/11/2000 */
  1812. info->icount.overrun++;
  1813. /* */
  1814. } else
  1815. *fp++ = 0;
  1816. } else
  1817. *fp++ = 0;
  1818. *cp++ = ch;
  1819. cnt++;
  1820. if (cnt >= recv_room) {
  1821. if (!info->ldisc_stop_rx) {
  1822. //mxser_throttle(tty);
  1823. mxser_stoprx(tty);
  1824. }
  1825. break;
  1826. }
  1827. }
  1828. // following add by Victor Yu. 09-02-2002
  1829. if (info->IsMoxaMustChipFlag)
  1830. break;
  1831. // above add by Victor Yu. 09-02-2002
  1832. /* mask by Victor Yu. 09-02-2002
  1833. *status = inb(info->base + UART_LSR) & info->read_status_mask;
  1834. */
  1835. // following add by Victor Yu. 09-02-2002
  1836. *status = inb(info->base + UART_LSR);
  1837. // above add by Victor Yu. 09-02-2002
  1838. } while (*status & UART_LSR_DR);
  1839. end_intr: // add by Victor Yu. 09-02-2002
  1840. mxvar_log.rxcnt[info->port] += cnt;
  1841. info->mon_data.rxcnt += cnt;
  1842. info->mon_data.up_rxcnt += cnt;
  1843. spin_unlock_irqrestore(&info->slock, flags);
  1844. tty_flip_buffer_push(tty);
  1845. }
  1846. static void mxser_transmit_chars(struct mxser_struct *info)
  1847. {
  1848. int count, cnt;
  1849. unsigned long flags;
  1850. spin_lock_irqsave(&info->slock, flags);
  1851. if (info->x_char) {
  1852. outb(info->x_char, info->base + UART_TX);
  1853. info->x_char = 0;
  1854. mxvar_log.txcnt[info->port]++;
  1855. info->mon_data.txcnt++;
  1856. info->mon_data.up_txcnt++;
  1857. /* added by casper 1/11/2000 */
  1858. info->icount.tx++;
  1859. /* */
  1860. spin_unlock_irqrestore(&info->slock, flags);
  1861. return;
  1862. }
  1863. if (info->xmit_buf == 0) {
  1864. spin_unlock_irqrestore(&info->slock, flags);
  1865. return;
  1866. }
  1867. if ((info->xmit_cnt <= 0) || info->tty->stopped || (info->tty->hw_stopped && (info->type != PORT_16550A) && (!info->IsMoxaMustChipFlag))) {
  1868. info->IER &= ~UART_IER_THRI;
  1869. outb(info->IER, info->base + UART_IER);
  1870. spin_unlock_irqrestore(&info->slock, flags);
  1871. return;
  1872. }
  1873. cnt = info->xmit_cnt;
  1874. count = info->xmit_fifo_size;
  1875. do {
  1876. outb(info->xmit_buf[info->xmit_tail++], info->base + UART_TX);
  1877. info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE - 1);
  1878. if (--info->xmit_cnt <= 0)
  1879. break;
  1880. } while (--count > 0);
  1881. mxvar_log.txcnt[info->port] += (cnt - info->xmit_cnt);
  1882. // added by James 03-12-2004.
  1883. info->mon_data.txcnt += (cnt - info->xmit_cnt);
  1884. info->mon_data.up_txcnt += (cnt - info->xmit_cnt);
  1885. // (above) added by James.
  1886. /* added by casper 1/11/2000 */
  1887. info->icount.tx += (cnt - info->xmit_cnt);
  1888. /* */
  1889. if (info->xmit_cnt < WAKEUP_CHARS) {
  1890. set_bit(MXSER_EVENT_TXLOW, &info->event);
  1891. schedule_work(&info->tqueue);
  1892. }
  1893. if (info->xmit_cnt <= 0) {
  1894. info->IER &= ~UART_IER_THRI;
  1895. outb(info->IER, info->base + UART_IER);
  1896. }
  1897. spin_unlock_irqrestore(&info->slock, flags);
  1898. }
  1899. static void mxser_check_modem_status(struct mxser_struct *info, int status)
  1900. {
  1901. /* update input line counters */
  1902. if (status & UART_MSR_TERI)
  1903. info->icount.rng++;
  1904. if (status & UART_MSR_DDSR)
  1905. info->icount.dsr++;
  1906. if (status & UART_MSR_DDCD)
  1907. info->icount.dcd++;
  1908. if (status & UART_MSR_DCTS)
  1909. info->icount.cts++;
  1910. info->mon_data.modem_status = status;
  1911. wake_up_interruptible(&info->delta_msr_wait);
  1912. if ((info->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
  1913. if (status & UART_MSR_DCD)
  1914. wake_up_interruptible(&info->open_wait);
  1915. schedule_work(&info->tqueue);
  1916. }
  1917. if (info->flags & ASYNC_CTS_FLOW) {
  1918. if (info->tty->hw_stopped) {
  1919. if (status & UART_MSR_CTS) {
  1920. info->tty->hw_stopped = 0;
  1921. if ((info->type != PORT_16550A) && (!info->IsMoxaMustChipFlag)) {
  1922. info->IER |= UART_IER_THRI;
  1923. outb(info->IER, info->base + UART_IER);
  1924. }
  1925. set_bit(MXSER_EVENT_TXLOW, &info->event);
  1926. schedule_work(&info->tqueue); }
  1927. } else {
  1928. if (!(status & UART_MSR_CTS)) {
  1929. info->tty->hw_stopped = 1;
  1930. if ((info->type != PORT_16550A) && (!info->IsMoxaMustChipFlag)) {
  1931. info->IER &= ~UART_IER_THRI;
  1932. outb(info->IER, info->base + UART_IER);
  1933. }
  1934. }
  1935. }
  1936. }
  1937. }
  1938. static int mxser_block_til_ready(struct tty_struct *tty, struct file *filp, struct mxser_struct *info)
  1939. {
  1940. DECLARE_WAITQUEUE(wait, current);
  1941. int retval;
  1942. int do_clocal = 0;
  1943. unsigned long flags;
  1944. /*
  1945. * If non-blocking mode is set, or the port is not enabled,
  1946. * then make the check up front and then exit.
  1947. */
  1948. if ((filp->f_flags & O_NONBLOCK) || (tty->flags & (1 << TTY_IO_ERROR))) {
  1949. info->flags |= ASYNC_NORMAL_ACTIVE;
  1950. return (0);
  1951. }
  1952. if (tty->termios->c_cflag & CLOCAL)
  1953. do_clocal = 1;
  1954. /*
  1955. * Block waiting for the carrier detect and the line to become
  1956. * free (i.e., not in use by the callout). While we are in
  1957. * this loop, info->count is dropped by one, so that
  1958. * mxser_close() knows when to free things. We restore it upon
  1959. * exit, either normal or abnormal.
  1960. */
  1961. retval = 0;
  1962. add_wait_queue(&info->open_wait, &wait);
  1963. spin_lock_irqsave(&info->slock, flags);
  1964. if (!tty_hung_up_p(filp))
  1965. info->count--;
  1966. spin_unlock_irqrestore(&info->slock, flags);
  1967. info->blocked_open++;
  1968. while (1) {
  1969. spin_lock_irqsave(&info->slock, flags);
  1970. outb(inb(info->base + UART_MCR) | UART_MCR_DTR | UART_MCR_RTS, info->base + UART_MCR);
  1971. spin_unlock_irqrestore(&info->slock, flags);
  1972. set_current_state(TASK_INTERRUPTIBLE);
  1973. if (tty_hung_up_p(filp) || !(info->flags & ASYNC_INITIALIZED)) {
  1974. if (info->flags & ASYNC_HUP_NOTIFY)
  1975. retval = -EAGAIN;
  1976. else
  1977. retval = -ERESTARTSYS;
  1978. break;
  1979. }
  1980. if (!(info->flags & ASYNC_CLOSING) && (do_clocal || (inb(info->base + UART_MSR) & UART_MSR_DCD)))
  1981. break;
  1982. if (signal_pending(current)) {
  1983. retval = -ERESTARTSYS;
  1984. break;
  1985. }
  1986. schedule();
  1987. }
  1988. set_current_state(TASK_RUNNING);
  1989. remove_wait_queue(&info->open_wait, &wait);
  1990. if (!tty_hung_up_p(filp))
  1991. info->count++;
  1992. info->blocked_open--;
  1993. if (retval)
  1994. return (retval);
  1995. info->flags |= ASYNC_NORMAL_ACTIVE;
  1996. return (0);
  1997. }
  1998. static int mxser_startup(struct mxser_struct *info)
  1999. {
  2000. unsigned long page;
  2001. unsigned long flags;
  2002. page = __get_free_page(GFP_KERNEL);
  2003. if (!page)
  2004. return (-ENOMEM);
  2005. spin_lock_irqsave(&info->slock, flags);
  2006. if (info->flags & ASYNC_INITIALIZED) {
  2007. free_page(page);
  2008. spin_unlock_irqrestore(&info->slock, flags);
  2009. return (0);
  2010. }
  2011. if (!info->base || !info->type) {
  2012. if (info->tty)
  2013. set_bit(TTY_IO_ERROR, &info->tty->flags);
  2014. free_page(page);
  2015. spin_unlock_irqrestore(&info->slock, flags);
  2016. return (0);
  2017. }
  2018. if (info->xmit_buf)
  2019. free_page(page);
  2020. else
  2021. info->xmit_buf = (unsigned char *) page;
  2022. /*
  2023. * Clear the FIFO buffers and disable them
  2024. * (they will be reenabled in mxser_change_speed())
  2025. */
  2026. if (info->IsMoxaMustChipFlag)
  2027. outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT | MOXA_MUST_FCR_GDA_MODE_ENABLE), info->base + UART_FCR);
  2028. else
  2029. outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT), info->base + UART_FCR);
  2030. /*
  2031. * At this point there's no way the LSR could still be 0xFF;
  2032. * if it is, then bail out, because there's likely no UART
  2033. * here.
  2034. */
  2035. if (inb(info->base + UART_LSR) == 0xff) {
  2036. spin_unlock_irqrestore(&info->slock, flags);
  2037. if (capable(CAP_SYS_ADMIN)) {
  2038. if (info->tty)
  2039. set_bit(TTY_IO_ERROR, &info->tty->flags);
  2040. return (0);
  2041. } else
  2042. return (-ENODEV);
  2043. }
  2044. /*
  2045. * Clear the interrupt registers.
  2046. */
  2047. (void) inb(info->base + UART_LSR);
  2048. (void) inb(info->base + UART_RX);
  2049. (void) inb(info->base + UART_IIR);
  2050. (void) inb(info->base + UART_MSR);
  2051. /*
  2052. * Now, initialize the UART
  2053. */
  2054. outb(UART_LCR_WLEN8, info->base + UART_LCR); /* reset DLAB */
  2055. info->MCR = UART_MCR_DTR | UART_MCR_RTS;
  2056. outb(info->MCR, info->base + UART_MCR);
  2057. /*
  2058. * Finally, enable interrupts
  2059. */
  2060. info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
  2061. // info->IER = UART_IER_RLSI | UART_IER_RDI;
  2062. // following add by Victor Yu. 08-30-2002
  2063. if (info->IsMoxaMustChipFlag)
  2064. info->IER |= MOXA_MUST_IER_EGDAI;
  2065. // above add by Victor Yu. 08-30-2002
  2066. outb(info->IER, info->base + UART_IER); /* enable interrupts */
  2067. /*
  2068. * And clear the interrupt registers again for luck.
  2069. */
  2070. (void) inb(info->base + UART_LSR);
  2071. (void) inb(info->base + UART_RX);
  2072. (void) inb(info->base + UART_IIR);
  2073. (void) inb(info->base + UART_MSR);
  2074. if (info->tty)
  2075. clear_bit(TTY_IO_ERROR, &info->tty->flags);
  2076. info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
  2077. /*
  2078. * and set the speed of the serial port
  2079. */
  2080. spin_unlock_irqrestore(&info->slock, flags);
  2081. mxser_change_speed(info, NULL);
  2082. info->flags |= ASYNC_INITIALIZED;
  2083. return (0);
  2084. }
  2085. /*
  2086. * This routine will shutdown a serial port; interrupts maybe disabled, and
  2087. * DTR is dropped if the hangup on close termio flag is on.
  2088. */
  2089. static void mxser_shutdown(struct mxser_struct *info)
  2090. {
  2091. unsigned long flags;
  2092. if (!(info->flags & ASYNC_INITIALIZED))
  2093. return;
  2094. spin_lock_irqsave(&info->slock, flags);
  2095. /*
  2096. * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
  2097. * here so the queue might never be waken up
  2098. */
  2099. wake_up_interruptible(&info->delta_msr_wait);
  2100. /*
  2101. * Free the IRQ, if necessary
  2102. */
  2103. if (info->xmit_buf) {
  2104. free_page((unsigned long) info->xmit_buf);
  2105. info->xmit_buf = NULL;
  2106. }
  2107. info->IER = 0;
  2108. outb(0x00, info->base + UART_IER);
  2109. if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
  2110. info->MCR &= ~(UART_MCR_DTR | UART_MCR_RTS);
  2111. outb(info->MCR, info->base + UART_MCR);
  2112. /* clear Rx/Tx FIFO's */
  2113. // following add by Victor Yu. 08-30-2002
  2114. if (info->IsMoxaMustChipFlag)
  2115. outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT | MOXA_MUST_FCR_GDA_MODE_ENABLE), info->base + UART_FCR);
  2116. else
  2117. // above add by Victor Yu. 08-30-2002
  2118. outb((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT), info->base + UART_FCR);
  2119. /* read data port to reset things */
  2120. (void) inb(info->base + UART_RX);
  2121. if (info->tty)
  2122. set_bit(TTY_IO_ERROR, &info->tty->flags);
  2123. info->flags &= ~ASYNC_INITIALIZED;
  2124. // following add by Victor Yu. 09-23-2002
  2125. if (info->IsMoxaMustChipFlag) {
  2126. SET_MOXA_MUST_NO_SOFTWARE_FLOW_CONTROL(info->base);
  2127. }
  2128. // above add by Victor Yu. 09-23-2002
  2129. spin_unlock_irqrestore(&info->slock, flags);
  2130. }
  2131. /*
  2132. * This routine is called to set the UART divisor registers to match
  2133. * the specified baud rate for a serial port.
  2134. */
  2135. static int mxser_change_speed(struct mxser_struct *info, struct termios *old_termios)
  2136. {
  2137. unsigned cflag, cval, fcr;
  2138. int ret = 0;
  2139. unsigned char status;
  2140. long baud;
  2141. unsigned long flags;
  2142. if (!info->tty || !info->tty->termios)
  2143. return ret;
  2144. cflag = info->tty->termios->c_cflag;
  2145. if (!(info->base))
  2146. return ret;
  2147. #ifndef B921600
  2148. #define B921600 (B460800 +1)
  2149. #endif
  2150. if (mxser_set_baud_method[info->port] == 0) {
  2151. switch (cflag & (CBAUD | CBAUDEX)) {
  2152. case B921600:
  2153. baud = 921600;
  2154. break;
  2155. case B460800:
  2156. baud = 460800;
  2157. break;
  2158. case B230400:
  2159. baud = 230400;
  2160. break;
  2161. case B115200:
  2162. baud = 115200;
  2163. break;
  2164. case B57600:
  2165. baud = 57600;
  2166. break;
  2167. case B38400:
  2168. baud = 38400;
  2169. break;
  2170. case B19200:
  2171. baud = 19200;
  2172. break;
  2173. case B9600:
  2174. baud = 9600;
  2175. break;
  2176. case B4800:
  2177. baud = 4800;
  2178. break;
  2179. case B2400:
  2180. baud = 2400;
  2181. break;
  2182. case B1800:
  2183. baud = 1800;
  2184. break;
  2185. case B1200:
  2186. baud = 1200;
  2187. break;
  2188. case B600:
  2189. baud = 600;
  2190. break;
  2191. case B300:
  2192. baud = 300;
  2193. break;
  2194. case B200:
  2195. baud = 200;
  2196. break;
  2197. case B150:
  2198. baud = 150;
  2199. break;
  2200. case B134:
  2201. baud = 134;
  2202. break;
  2203. case B110:
  2204. baud = 110;
  2205. break;
  2206. case B75:
  2207. baud = 75;
  2208. break;
  2209. case B50:
  2210. baud = 50;
  2211. break;
  2212. default:
  2213. baud = 0;
  2214. break;
  2215. }
  2216. mxser_set_baud(info, baud);
  2217. }
  2218. /* byte size and parity */
  2219. switch (cflag & CSIZE) {
  2220. case CS5:
  2221. cval = 0x00;
  2222. break;
  2223. case CS6:
  2224. cval = 0x01;
  2225. break;
  2226. case CS7:
  2227. cval = 0x02;
  2228. break;
  2229. case CS8:
  2230. cval = 0x03;
  2231. break;
  2232. default:
  2233. cval = 0x00;
  2234. break; /* too keep GCC shut... */
  2235. }
  2236. if (cflag & CSTOPB)
  2237. cval |= 0x04;
  2238. if (cflag & PARENB)
  2239. cval |= UART_LCR_PARITY;
  2240. if (!(cflag & PARODD)) {
  2241. cval |= UART_LCR_EPAR;
  2242. }
  2243. if (cflag & CMSPAR)
  2244. cval |= UART_LCR_SPAR;
  2245. if ((info->type == PORT_8250) || (info->type == PORT_16450)) {
  2246. if (info->IsMoxaMustChipFlag) {
  2247. fcr = UART_FCR_ENABLE_FIFO;
  2248. fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
  2249. SET_MOXA_MUST_FIFO_VALUE(info);
  2250. } else
  2251. fcr = 0;
  2252. } else {
  2253. fcr = UART_FCR_ENABLE_FIFO;
  2254. // following add by Victor Yu. 08-30-2002
  2255. if (info->IsMoxaMustChipFlag) {
  2256. fcr |= MOXA_MUST_FCR_GDA_MODE_ENABLE;
  2257. SET_MOXA_MUST_FIFO_VALUE(info);
  2258. } else {
  2259. // above add by Victor Yu. 08-30-2002
  2260. switch (info->rx_trigger) {
  2261. case 1:
  2262. fcr |= UART_FCR_TRIGGER_1;
  2263. break;
  2264. case 4:
  2265. fcr |= UART_FCR_TRIGGER_4;
  2266. break;
  2267. case 8:
  2268. fcr |= UART_FCR_TRIGGER_8;
  2269. break;
  2270. default:
  2271. fcr |= UART_FCR_TRIGGER_14;
  2272. break;
  2273. }
  2274. }
  2275. }
  2276. /* CTS flow control flag and modem status interrupts */
  2277. info->IER &= ~UART_IER_MSI;
  2278. info->MCR &= ~UART_MCR_AFE;
  2279. if (cflag & CRTSCTS) {
  2280. info->flags |= ASYNC_CTS_FLOW;
  2281. info->IER |= UART_IER_MSI;
  2282. if ((info->type == PORT_16550A) || (info->IsMoxaMustChipFlag)) {
  2283. info->MCR |= UART_MCR_AFE;
  2284. //status = mxser_get_msr(info->base, 0, info->port);
  2285. /* save_flags(flags);
  2286. cli();
  2287. status = inb(baseaddr + UART_MSR);
  2288. restore_flags(flags);*/
  2289. //mxser_check_modem_status(info, status);
  2290. } else {
  2291. //status = mxser_get_msr(info->base, 0, info->port);
  2292. //MX_LOCK(&info->slock);
  2293. status = inb(info->base + UART_MSR);
  2294. //MX_UNLOCK(&info->slock);
  2295. if (info->tty->hw_stopped) {
  2296. if (status & UART_MSR_CTS) {
  2297. info->tty->hw_stopped = 0;
  2298. if ((info->type != PORT_16550A) && (!info->IsMoxaMustChipFlag)) {
  2299. info->IER |= UART_IER_THRI;
  2300. outb(info->IER, info->base + UART_IER);
  2301. }
  2302. set_bit(MXSER_EVENT_TXLOW, &info->event);
  2303. schedule_work(&info->tqueue); }
  2304. } else {
  2305. if (!(status & UART_MSR_CTS)) {
  2306. info->tty->hw_stopped = 1;
  2307. if ((info->type != PORT_16550A) && (!info->IsMoxaMustChipFlag)) {
  2308. info->IER &= ~UART_IER_THRI;
  2309. outb(info->IER, info->base + UART_IER);
  2310. }
  2311. }
  2312. }
  2313. }
  2314. } else {
  2315. info->flags &= ~ASYNC_CTS_FLOW;
  2316. }
  2317. outb(info->MCR, info->base + UART_MCR);
  2318. if (cflag & CLOCAL) {
  2319. info->flags &= ~ASYNC_CHECK_CD;
  2320. } else {
  2321. info->flags |= ASYNC_CHECK_CD;
  2322. info->IER |= UART_IER_MSI;
  2323. }
  2324. outb(info->IER, info->base + UART_IER);
  2325. /*
  2326. * Set up parity check flag
  2327. */
  2328. info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
  2329. if (I_INPCK(info->tty))
  2330. info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
  2331. if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
  2332. info->read_status_mask |= UART_LSR_BI;
  2333. info->ignore_status_mask = 0;
  2334. if (I_IGNBRK(info->tty)) {
  2335. info->ignore_status_mask |= UART_LSR_BI;
  2336. info->read_status_mask |= UART_LSR_BI;
  2337. /*
  2338. * If we're ignore parity and break indicators, ignore
  2339. * overruns too. (For real raw support).
  2340. */
  2341. if (I_IGNPAR(info->tty)) {
  2342. info->ignore_status_mask |= UART_LSR_OE | UART_LSR_PE | UART_LSR_FE;
  2343. info->read_status_mask |= UART_LSR_OE | UART_LSR_PE | UART_LSR_FE;
  2344. }
  2345. }
  2346. // following add by Victor Yu. 09-02-2002
  2347. if (info->IsMoxaMustChipFlag) {
  2348. spin_lock_irqsave(&info->slock, flags);
  2349. SET_MOXA_MUST_XON1_VALUE(info->base, START_CHAR(info->tty));
  2350. SET_MOXA_MUST_XOFF1_VALUE(info->base, STOP_CHAR(info->tty));
  2351. if (I_IXON(info->tty)) {
  2352. ENABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->base);
  2353. } else {
  2354. DISABLE_MOXA_MUST_RX_SOFTWARE_FLOW_CONTROL(info->base);
  2355. }
  2356. if (I_IXOFF(info->tty)) {
  2357. ENABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->base);
  2358. } else {
  2359. DISABLE_MOXA_MUST_TX_SOFTWARE_FLOW_CONTROL(info->base);
  2360. }
  2361. /*
  2362. if ( I_IXANY(info->tty) ) {
  2363. info->MCR |= MOXA_MUST_MCR_XON_ANY;
  2364. ENABLE_MOXA_MUST_XON_ANY_FLOW_CONTROL(info->base);
  2365. } else {
  2366. info->MCR &= ~MOXA_MUST_MCR_XON_ANY;
  2367. DISABLE_MOXA_MUST_XON_ANY_FLOW_CONTROL(info->base);
  2368. }
  2369. */
  2370. spin_unlock_irqrestore(&info->slock, flags);
  2371. }
  2372. // above add by Victor Yu. 09-02-2002
  2373. outb(fcr, info->base + UART_FCR); /* set fcr */
  2374. outb(cval, info->base + UART_LCR);
  2375. return ret;
  2376. }
  2377. static int mxser_set_baud(struct mxser_struct *info, long newspd)
  2378. {
  2379. int quot = 0;
  2380. unsigned char cval;
  2381. int ret = 0;
  2382. unsigned long flags;
  2383. if (!info->tty || !info->tty->termios)
  2384. return ret;
  2385. if (!(info->base))
  2386. return ret;
  2387. if (newspd > info->MaxCanSetBaudRate)
  2388. return 0;
  2389. info->realbaud = newspd;
  2390. if (newspd == 134) {
  2391. quot = (2 * info->baud_base / 269);
  2392. } else if (newspd) {
  2393. quot = info->baud_base / newspd;
  2394. if (quot == 0)
  2395. quot = 1;
  2396. } else {
  2397. quot = 0;
  2398. }
  2399. info->timeout = ((info->xmit_fifo_size * HZ * 10 * quot) / info->baud_base);
  2400. info->timeout += HZ / 50; /* Add .02 seconds of slop */
  2401. if (quot) {
  2402. spin_lock_irqsave(&info->slock, flags);
  2403. info->MCR |= UART_MCR_DTR;
  2404. outb(info->MCR, info->base + UART_MCR);
  2405. spin_unlock_irqrestore(&info->slock, flags);
  2406. } else {
  2407. spin_lock_irqsave(&info->slock, flags);
  2408. info->MCR &= ~UART_MCR_DTR;
  2409. outb(info->MCR, info->base + UART_MCR);
  2410. spin_unlock_irqrestore(&info->slock, flags);
  2411. return ret;
  2412. }
  2413. cval = inb(info->base + UART_LCR);
  2414. outb(cval | UART_LCR_DLAB, info->base + UART_LCR); /* set DLAB */
  2415. outb(quot & 0xff, info->base + UART_DLL); /* LS of divisor */
  2416. outb(quot >> 8, info->base + UART_DLM); /* MS of divisor */
  2417. outb(cval, info->base + UART_LCR); /* reset DLAB */
  2418. return ret;
  2419. }
  2420. /*
  2421. * ------------------------------------------------------------
  2422. * friends of mxser_ioctl()
  2423. * ------------------------------------------------------------
  2424. */
  2425. static int mxser_get_serial_info(struct mxser_struct *info, struct serial_struct __user *retinfo)
  2426. {
  2427. struct serial_struct tmp;
  2428. if (!retinfo)
  2429. return (-EFAULT);
  2430. memset(&tmp, 0, sizeof(tmp));
  2431. tmp.type = info->type;
  2432. tmp.line = info->port;
  2433. tmp.port = info->base;
  2434. tmp.irq = info->irq;
  2435. tmp.flags = info->flags;
  2436. tmp.baud_base = info->baud_base;
  2437. tmp.close_delay = info->close_delay;
  2438. tmp.closing_wait = info->closing_wait;
  2439. tmp.custom_divisor = info->custom_divisor;
  2440. tmp.hub6 = 0;
  2441. if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
  2442. return -EFAULT;
  2443. return (0);
  2444. }
  2445. static int mxser_set_serial_info(struct mxser_struct *info, struct serial_struct __user *new_info)
  2446. {
  2447. struct serial_struct new_serial;
  2448. unsigned int flags;
  2449. int retval = 0;
  2450. if (!new_info || !info->base)
  2451. return (-EFAULT);
  2452. if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
  2453. return -EFAULT;
  2454. if ((new_serial.irq != info->irq) || (new_serial.port != info->base) || (new_serial.custom_divisor != info->custom_divisor) || (new_serial.baud_base != info->baud_base))
  2455. return (-EPERM);
  2456. flags = info->flags & ASYNC_SPD_MASK;
  2457. if (!capable(CAP_SYS_ADMIN)) {
  2458. if ((new_serial.baud_base != info->baud_base) || (new_serial.close_delay != info->close_delay) || ((new_serial.flags & ~ASYNC_USR_MASK) != (info->flags & ~ASYNC_USR_MASK)))
  2459. return (-EPERM);
  2460. info->flags = ((info->flags & ~ASYNC_USR_MASK) | (new_serial.flags & ASYNC_USR_MASK));
  2461. } else {
  2462. /*
  2463. * OK, past this point, all the error checking has been done.
  2464. * At this point, we start making changes.....
  2465. */
  2466. info->flags = ((info->flags & ~ASYNC_FLAGS) | (new_serial.flags & ASYNC_FLAGS));
  2467. info->close_delay = new_serial.close_delay * HZ / 100;
  2468. info->closing_wait = new_serial.closing_wait * HZ / 100;
  2469. info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
  2470. info->tty->low_latency = 0; //(info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
  2471. }
  2472. /* added by casper, 3/17/2000, for mouse */
  2473. info->type = new_serial.type;
  2474. process_txrx_fifo(info);
  2475. /* */
  2476. if (info->flags & ASYNC_INITIALIZED) {
  2477. if (flags != (info->flags & ASYNC_SPD_MASK)) {
  2478. mxser_change_speed(info, NULL);
  2479. }
  2480. } else {
  2481. retval = mxser_startup(info);
  2482. }
  2483. return (retval);
  2484. }
  2485. /*
  2486. * mxser_get_lsr_info - get line status register info
  2487. *
  2488. * Purpose: Let user call ioctl() to get info when the UART physically
  2489. * is emptied. On bus types like RS485, the transmitter must
  2490. * release the bus after transmitting. This must be done when
  2491. * the transmit shift register is empty, not be done when the
  2492. * transmit holding register is empty. This functionality
  2493. * allows an RS485 driver to be written in user space.
  2494. */
  2495. static int mxser_get_lsr_info(struct mxser_struct *info, unsigned int __user *value)
  2496. {
  2497. unsigned char status;
  2498. unsigned int result;
  2499. unsigned long flags;
  2500. spin_lock_irqsave(&info->slock, flags);
  2501. status = inb(info->base + UART_LSR);
  2502. spin_unlock_irqrestore(&info->slock, flags);
  2503. result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
  2504. return put_user(result, value);
  2505. }
  2506. /*
  2507. * This routine sends a break character out the serial port.
  2508. */
  2509. static void mxser_send_break(struct mxser_struct *info, int duration)
  2510. {
  2511. unsigned long flags;
  2512. if (!info->base)
  2513. return;
  2514. set_current_state(TASK_INTERRUPTIBLE);
  2515. spin_lock_irqsave(&info->slock, flags);
  2516. outb(inb(info->base + UART_LCR) | UART_LCR_SBC, info->base + UART_LCR);
  2517. spin_unlock_irqrestore(&info->slock, flags);
  2518. schedule_timeout(duration);
  2519. spin_lock_irqsave(&info->slock, flags);
  2520. outb(inb(info->base + UART_LCR) & ~UART_LCR_SBC, info->base + UART_LCR);
  2521. spin_unlock_irqrestore(&info->slock, flags);
  2522. }
  2523. static int mxser_tiocmget(struct tty_struct *tty, struct file *file)
  2524. {
  2525. struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
  2526. unsigned char control, status;
  2527. unsigned long flags;
  2528. if (tty->index == MXSER_PORTS)
  2529. return (-ENOIOCTLCMD);
  2530. if (tty->flags & (1 << TTY_IO_ERROR))
  2531. return (-EIO);
  2532. control = info->MCR;
  2533. spin_lock_irqsave(&info->slock, flags);
  2534. status = inb(info->base + UART_MSR);
  2535. if (status & UART_MSR_ANY_DELTA)
  2536. mxser_check_modem_status(info, status);
  2537. spin_unlock_irqrestore(&info->slock, flags);
  2538. return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0) |
  2539. ((control & UART_MCR_DTR) ? TIOCM_DTR : 0) | ((status & UART_MSR_DCD) ? TIOCM_CAR : 0) | ((status & UART_MSR_RI) ? TIOCM_RNG : 0) | ((status & UART_MSR_DSR) ? TIOCM_DSR : 0) | ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
  2540. }
  2541. static int mxser_tiocmset(struct tty_struct *tty, struct file *file, unsigned int set, unsigned int clear)
  2542. {
  2543. struct mxser_struct *info = (struct mxser_struct *) tty->driver_data;
  2544. unsigned long flags;
  2545. if (tty->index == MXSER_PORTS)
  2546. return -ENOIOCTLCMD;
  2547. if (tty->flags & (1 << TTY_IO_ERROR))
  2548. return -EIO;
  2549. spin_lock_irqsave(&info->slock, flags);
  2550. if (set & TIOCM_RTS)
  2551. info->MCR |= UART_MCR_RTS;
  2552. if (set & TIOCM_DTR)
  2553. info->MCR |= UART_MCR_DTR;
  2554. if (clear & TIOCM_RTS)
  2555. info->MCR &= ~UART_MCR_RTS;
  2556. if (clear & TIOCM_DTR)
  2557. info->MCR &= ~UART_MCR_DTR;
  2558. outb(info->MCR, info->base + UART_MCR);
  2559. spin_unlock_irqrestore(&info->slock, flags);
  2560. return 0;
  2561. }
  2562. static int mxser_read_register(int, unsigned short *);
  2563. static int mxser_program_mode(int);
  2564. static void mxser_normal_mode(int);
  2565. static int mxser_get_ISA_conf(int cap, struct mxser_hwconf *hwconf)
  2566. {
  2567. int id, i, bits;
  2568. unsigned short regs[16], irq;
  2569. unsigned char scratch, scratch2;
  2570. hwconf->IsMoxaMustChipFlag = MOXA_OTHER_UART;
  2571. id = mxser_read_register(cap, regs);
  2572. if (id == C168_ASIC_ID) {
  2573. hwconf->board_type = MXSER_BOARD_C168_ISA;
  2574. hwconf->ports = 8;
  2575. } else if (id == C104_ASIC_ID) {
  2576. hwconf->board_type = MXSER_BOARD_C104_ISA;
  2577. hwconf->ports = 4;
  2578. } else if (id == C102_ASIC_ID) {
  2579. hwconf->board_type = MXSER_BOARD_C102_ISA;
  2580. hwconf->ports = 2;
  2581. } else if (id == CI132_ASIC_ID) {
  2582. hwconf->board_type = MXSER_BOARD_CI132;
  2583. hwconf->ports = 2;
  2584. } else if (id == CI134_ASIC_ID) {
  2585. hwconf->board_type = MXSER_BOARD_CI134;
  2586. hwconf->ports = 4;
  2587. } else if (id == CI104J_ASIC_ID) {
  2588. hwconf->board_type = MXSER_BOARD_CI104J;
  2589. hwconf->ports = 4;
  2590. } else
  2591. return (0);
  2592. irq = 0;
  2593. if (hwconf->ports == 2) {
  2594. irq = regs[9] & 0xF000;
  2595. irq = irq | (irq >> 4);
  2596. if (irq != (regs[9] & 0xFF00))
  2597. return (MXSER_ERR_IRQ_CONFLIT);
  2598. } else if (hwconf->ports == 4) {
  2599. irq = regs[9] & 0xF000;
  2600. irq = irq | (irq >> 4);
  2601. irq = irq | (irq >> 8);
  2602. if (irq != regs[9])
  2603. return (MXSER_ERR_IRQ_CONFLIT);
  2604. } else if (hwconf->ports == 8) {
  2605. irq = regs[9] & 0xF000;
  2606. irq = irq | (irq >> 4);
  2607. irq = irq | (irq >> 8);
  2608. if ((irq != regs[9]) || (irq != regs[10]))
  2609. return (MXSER_ERR_IRQ_CONFLIT);
  2610. }
  2611. if (!irq) {
  2612. return (MXSER_ERR_IRQ);
  2613. }
  2614. hwconf->irq = ((int) (irq & 0xF000) >> 12);
  2615. for (i = 0; i < 8; i++)
  2616. hwconf->ioaddr[i] = (int) regs[i + 1] & 0xFFF8;
  2617. if ((regs[12] & 0x80) == 0) {
  2618. return (MXSER_ERR_VECTOR);
  2619. }
  2620. hwconf->vector = (int) regs[11]; /* interrupt vector */
  2621. if (id == 1)
  2622. hwconf->vector_mask = 0x00FF;
  2623. else
  2624. hwconf->vector_mask = 0x000F;
  2625. for (i = 7, bits = 0x0100; i >= 0; i--, bits <<= 1) {
  2626. if (regs[12] & bits) {
  2627. hwconf->baud_base[i] = 921600;
  2628. hwconf->MaxCanSetBaudRate[i] = 921600; // add by Victor Yu. 09-04-2002
  2629. } else {
  2630. hwconf->baud_base[i] = 115200;
  2631. hwconf->MaxCanSetBaudRate[i] = 115200; // add by Victor Yu. 09-04-2002
  2632. }
  2633. }
  2634. scratch2 = inb(cap + UART_LCR) & (~UART_LCR_DLAB);
  2635. outb(scratch2 | UART_LCR_DLAB, cap + UART_LCR);
  2636. outb(0, cap + UART_EFR); /* EFR is the same as FCR */
  2637. outb(scratch2, cap + UART_LCR);
  2638. outb(UART_FCR_ENABLE_FIFO, cap + UART_FCR);
  2639. scratch = inb(cap + UART_IIR);
  2640. if (scratch & 0xC0)
  2641. hwconf->uart_type = PORT_16550A;
  2642. else
  2643. hwconf->uart_type = PORT_16450;
  2644. if (id == 1)
  2645. hwconf->ports = 8;
  2646. else
  2647. hwconf->ports = 4;
  2648. request_region(hwconf->ioaddr[0], 8 * hwconf->ports, "mxser(IO)");
  2649. request_region(hwconf->vector, 1, "mxser(vector)");
  2650. return (hwconf->ports);
  2651. }
  2652. #define CHIP_SK 0x01 /* Serial Data Clock in Eprom */
  2653. #define CHIP_DO 0x02 /* Serial Data Output in Eprom */
  2654. #define CHIP_CS 0x04 /* Serial Chip Select in Eprom */
  2655. #define CHIP_DI 0x08 /* Serial Data Input in Eprom */
  2656. #define EN_CCMD 0x000 /* Chip's command register */
  2657. #define EN0_RSARLO 0x008 /* Remote start address reg 0 */
  2658. #define EN0_RSARHI 0x009 /* Remote start address reg 1 */
  2659. #define EN0_RCNTLO 0x00A /* Remote byte count reg WR */
  2660. #define EN0_RCNTHI 0x00B /* Remote byte count reg WR */
  2661. #define EN0_DCFG 0x00E /* Data configuration reg WR */
  2662. #define EN0_PORT 0x010 /* Rcv missed frame error counter RD */
  2663. #define ENC_PAGE0 0x000 /* Select page 0 of chip registers */
  2664. #define ENC_PAGE3 0x0C0 /* Select page 3 of chip registers */
  2665. static int mxser_read_register(int port, unsigned short *regs)
  2666. {
  2667. int i, k, value, id;
  2668. unsigned int j;
  2669. id = mxser_program_mode(port);
  2670. if (id < 0)
  2671. return (id);
  2672. for (i = 0; i < 14; i++) {
  2673. k = (i & 0x3F) | 0x180;
  2674. for (j = 0x100; j > 0; j >>= 1) {
  2675. outb(CHIP_CS, port);
  2676. if (k & j) {
  2677. outb(CHIP_CS | CHIP_DO, port);
  2678. outb(CHIP_CS | CHIP_DO | CHIP_SK, port); /* A? bit of read */
  2679. } else {
  2680. outb(CHIP_CS, port);
  2681. outb(CHIP_CS | CHIP_SK, port); /* A? bit of read */
  2682. }
  2683. }
  2684. (void) inb(port);
  2685. value = 0;
  2686. for (k = 0, j = 0x8000; k < 16; k++, j >>= 1) {
  2687. outb(CHIP_CS, port);
  2688. outb(CHIP_CS | CHIP_SK, port);
  2689. if (inb(port) & CHIP_DI)
  2690. value |= j;
  2691. }
  2692. regs[i] = value;
  2693. outb(0, port);
  2694. }
  2695. mxser_normal_mode(port);
  2696. return (id);
  2697. }
  2698. static int mxser_program_mode(int port)
  2699. {
  2700. int id, i, j, n;
  2701. //unsigned long flags;
  2702. spin_lock(&gm_lock);
  2703. outb(0, port);
  2704. outb(0, port);
  2705. outb(0, port);
  2706. (void) inb(port);
  2707. (void) inb(port);
  2708. outb(0, port);
  2709. (void) inb(port);
  2710. //restore_flags(flags);
  2711. spin_unlock(&gm_lock);
  2712. id = inb(port + 1) & 0x1F;
  2713. if ((id != C168_ASIC_ID) && (id != C104_ASIC_ID) && (id != C102_ASIC_ID) && (id != CI132_ASIC_ID) && (id != CI134_ASIC_ID) && (id != CI104J_ASIC_ID))
  2714. return (-1);
  2715. for (i = 0, j = 0; i < 4; i++) {
  2716. n = inb(port + 2);
  2717. if (n == 'M') {
  2718. j = 1;
  2719. } else if ((j == 1) && (n == 1)) {
  2720. j = 2;
  2721. break;
  2722. } else
  2723. j = 0;
  2724. }
  2725. if (j != 2)
  2726. id = -2;
  2727. return (id);
  2728. }
  2729. static void mxser_normal_mode(int port)
  2730. {
  2731. int i, n;
  2732. outb(0xA5, port + 1);
  2733. outb(0x80, port + 3);
  2734. outb(12, port + 0); /* 9600 bps */
  2735. outb(0, port + 1);
  2736. outb(0x03, port + 3); /* 8 data bits */
  2737. outb(0x13, port + 4); /* loop back mode */
  2738. for (i = 0; i < 16; i++) {
  2739. n = inb(port + 5);
  2740. if ((n & 0x61) == 0x60)
  2741. break;
  2742. if ((n & 1) == 1)
  2743. (void) inb(port);
  2744. }
  2745. outb(0x00, port + 4);
  2746. }
  2747. module_init(mxser_module_init);
  2748. module_exit(mxser_module_exit);