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