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