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