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