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