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