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