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