blob: b2f3de0195c9635f6a8426a9e80c3dd3fe5364cb [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*****************************************************************************/
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 Torvalds1da177e2005-04-16 15:20:36 -070014 */
15
16/*
17 * MOXA Intellio Series Driver
18 * for : LINUX
19 * date : 1999/1/7
20 * version : 5.1
21 */
22
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#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 Slaby03718232008-04-30 00:53:39 -070028#include <linux/firmware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#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>
Jiri Slaby95e07912007-10-18 03:06:25 -070045#include <linux/completion.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47#include <asm/system.h>
48#include <asm/io.h>
49#include <asm/uaccess.h>
50
Jiri Slaby03718232008-04-30 00:53:39 -070051#include "moxa.h"
52
Jiri Slaby11324ed2007-02-10 01:45:31 -080053#define MOXA_VERSION "5.1k"
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Jiri Slaby03718232008-04-30 00:53:39 -070055#define MOXA_FW_HDRLEN 32
56
Jiri Slaby11324ed2007-02-10 01:45:31 -080057#define MOXAMAJOR 172
58#define MOXACUMAJOR 173
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Jiri Slaby11324ed2007-02-10 01:45:31 -080060#define MAX_BOARDS 4 /* Don't change this value */
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#define MAX_PORTS_PER_BOARD 32 /* Don't change this value */
Jiri Slaby11324ed2007-02-10 01:45:31 -080062#define MAX_PORTS (MAX_BOARDS * MAX_PORTS_PER_BOARD)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64/*
65 * Define the Moxa PCI vendor and device IDs.
66 */
Jiri Slaby11324ed2007-02-10 01:45:31 -080067#define MOXA_BUS_TYPE_ISA 0
68#define MOXA_BUS_TYPE_PCI 1
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Linus Torvalds1da177e2005-04-16 15:20:36 -070070enum {
71 MOXA_BOARD_C218_PCI = 1,
72 MOXA_BOARD_C218_ISA,
73 MOXA_BOARD_C320_PCI,
74 MOXA_BOARD_C320_ISA,
75 MOXA_BOARD_CP204J,
76};
77
78static char *moxa_brdname[] =
79{
80 "C218 Turbo PCI series",
81 "C218 Turbo ISA series",
82 "C320 Turbo PCI series",
83 "C320 Turbo ISA series",
84 "CP-204J series",
85};
86
87#ifdef CONFIG_PCI
88static struct pci_device_id moxa_pcibrds[] = {
Jiri Slaby5ebb4072007-02-10 01:45:30 -080089 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C218),
90 .driver_data = MOXA_BOARD_C218_PCI },
91 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C320),
92 .driver_data = MOXA_BOARD_C320_PCI },
93 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP204J),
94 .driver_data = MOXA_BOARD_CP204J },
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 { 0 }
96};
97MODULE_DEVICE_TABLE(pci, moxa_pcibrds);
98#endif /* CONFIG_PCI */
99
Jiri Slaby03718232008-04-30 00:53:39 -0700100struct moxa_port;
101
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800102static struct moxa_board_conf {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 int boardType;
104 int numPorts;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 int busType;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800106
Jiri Slaby810ab092008-04-30 00:53:41 -0700107 unsigned int ready;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800108
Jiri Slaby03718232008-04-30 00:53:39 -0700109 struct moxa_port *ports;
110
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800111 void __iomem *basemem;
112 void __iomem *intNdx;
113 void __iomem *intPend;
114 void __iomem *intTable;
115} moxa_boards[MAX_BOARDS];
116
117struct mxser_mstatus {
118 tcflag_t cflag;
119 int cts;
120 int dsr;
121 int ri;
122 int dcd;
Jiri Slaby9dff89c2007-02-10 01:45:30 -0800123};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800125struct moxaq_str {
126 int inq;
127 int outq;
128};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800130struct moxa_port {
Jiri Slabyb4173f42008-04-30 00:53:40 -0700131 struct moxa_board_conf *board;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 int close_delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 int count;
135 int blocked_open;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 int asyncflags;
137 unsigned long statusflags;
138 struct tty_struct *tty;
139 int cflag;
140 wait_queue_head_t open_wait;
Jiri Slaby95e07912007-10-18 03:06:25 -0700141 struct completion close_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800143 struct timer_list emptyTimer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800145 char lineCtrl;
146 void __iomem *tableAddr;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800147 char DCDState;
148 char lowChkFlag;
149
150 ushort breakCnt;
151};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153/* statusflags */
154#define TXSTOPPED 0x1
155#define LOWWAIT 0x2
156#define EMPTYWAIT 0x4
157#define THROTTLE 0x8
158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159#define SERIAL_DO_RESTART
160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161#define WAKEUP_CHARS 256
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163static int ttymajor = MOXAMAJOR;
164/* Variables for insmod */
165#ifdef MODULE
Jiri Slabyd353eca2008-04-30 00:53:37 -0700166static unsigned long baseaddr[MAX_BOARDS];
167static unsigned int type[MAX_BOARDS];
168static unsigned int numports[MAX_BOARDS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169#endif
170
171MODULE_AUTHOR("William Chen");
172MODULE_DESCRIPTION("MOXA Intellio Family Multiport Board Device Driver");
173MODULE_LICENSE("GPL");
174#ifdef MODULE
Jiri Slabyd353eca2008-04-30 00:53:37 -0700175module_param_array(type, uint, NULL, 0);
176MODULE_PARM_DESC(type, "card type: C218=2, C320=4");
177module_param_array(baseaddr, ulong, NULL, 0);
178MODULE_PARM_DESC(baseaddr, "base address");
179module_param_array(numports, uint, NULL, 0);
180MODULE_PARM_DESC(numports, "numports (ignored for C218)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181#endif
182module_param(ttymajor, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184/*
185 * static functions:
186 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187static int moxa_open(struct tty_struct *, struct file *);
188static void moxa_close(struct tty_struct *, struct file *);
189static int moxa_write(struct tty_struct *, const unsigned char *, int);
190static int moxa_write_room(struct tty_struct *);
191static void moxa_flush_buffer(struct tty_struct *);
192static int moxa_chars_in_buffer(struct tty_struct *);
193static void moxa_flush_chars(struct tty_struct *);
194static void moxa_put_char(struct tty_struct *, unsigned char);
195static int moxa_ioctl(struct tty_struct *, struct file *, unsigned int, unsigned long);
196static void moxa_throttle(struct tty_struct *);
197static void moxa_unthrottle(struct tty_struct *);
Alan Cox606d0992006-12-08 02:38:45 -0800198static void moxa_set_termios(struct tty_struct *, struct ktermios *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199static void moxa_stop(struct tty_struct *);
200static void moxa_start(struct tty_struct *);
201static void moxa_hangup(struct tty_struct *);
202static int moxa_tiocmget(struct tty_struct *tty, struct file *file);
203static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
204 unsigned int set, unsigned int clear);
205static void moxa_poll(unsigned long);
Alan Coxdb1acaa2008-02-08 04:18:43 -0800206static void moxa_set_tty_param(struct tty_struct *, struct ktermios *);
Jiri Slaby6f56b6582007-10-18 03:06:24 -0700207static int moxa_block_till_ready(struct tty_struct *, struct file *,
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800208 struct moxa_port *);
Jiri Slaby6f56b6582007-10-18 03:06:24 -0700209static void moxa_setup_empty_event(struct tty_struct *);
210static void moxa_check_xmit_empty(unsigned long);
211static void moxa_shut_down(struct moxa_port *);
212static void moxa_receive_data(struct moxa_port *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213/*
214 * moxa board interface functions:
215 */
Jiri Slabyb4173f42008-04-30 00:53:40 -0700216static int MoxaDriverIoctl(struct tty_struct *, unsigned int, unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217static int MoxaDriverPoll(void);
218static int MoxaPortsOfCard(int);
Jiri Slabyb4173f42008-04-30 00:53:40 -0700219static void MoxaPortEnable(struct moxa_port *);
220static void MoxaPortDisable(struct moxa_port *);
221static int MoxaPortSetTermio(struct moxa_port *, struct ktermios *, speed_t);
222static int MoxaPortGetLineOut(struct moxa_port *, int *, int *);
223static void MoxaPortLineCtrl(struct moxa_port *, int, int);
224static void MoxaPortFlowCtrl(struct moxa_port *, int, int, int, int, int);
225static int MoxaPortLineStatus(struct moxa_port *);
226static int MoxaPortDCDChange(struct moxa_port *);
227static int MoxaPortDCDON(struct moxa_port *);
228static void MoxaPortFlushData(struct moxa_port *, int);
229static int MoxaPortWriteData(struct moxa_port *, unsigned char *, int);
230static int MoxaPortReadData(struct moxa_port *, struct tty_struct *tty);
231static int MoxaPortTxQueue(struct moxa_port *);
232static int MoxaPortRxQueue(struct moxa_port *);
233static int MoxaPortTxFree(struct moxa_port *);
234static void MoxaPortTxDisable(struct moxa_port *);
235static void MoxaPortTxEnable(struct moxa_port *);
236static int MoxaPortResetBrkCnt(struct moxa_port *);
237static void MoxaPortSendBreak(struct moxa_port *, int);
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800238static int moxa_get_serial_info(struct moxa_port *, struct serial_struct __user *);
239static int moxa_set_serial_info(struct moxa_port *, struct serial_struct __user *);
Jiri Slabyb4173f42008-04-30 00:53:40 -0700240static void MoxaSetFifo(struct moxa_port *port, int enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Jeff Dikeb68e31d2006-10-02 02:17:18 -0700242static const struct tty_operations moxa_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 .open = moxa_open,
244 .close = moxa_close,
245 .write = moxa_write,
246 .write_room = moxa_write_room,
247 .flush_buffer = moxa_flush_buffer,
248 .chars_in_buffer = moxa_chars_in_buffer,
249 .flush_chars = moxa_flush_chars,
250 .put_char = moxa_put_char,
251 .ioctl = moxa_ioctl,
252 .throttle = moxa_throttle,
253 .unthrottle = moxa_unthrottle,
254 .set_termios = moxa_set_termios,
255 .stop = moxa_stop,
256 .start = moxa_start,
257 .hangup = moxa_hangup,
258 .tiocmget = moxa_tiocmget,
259 .tiocmset = moxa_tiocmset,
260};
261
Jiri Slabyaa7e5222007-02-10 01:45:27 -0800262static struct tty_driver *moxaDriver;
Jiri Slabyaa7e5222007-02-10 01:45:27 -0800263static DEFINE_TIMER(moxaTimer, moxa_poll, 0, 0);
Ingo Molnar34af9462006-06-27 02:53:55 -0700264static DEFINE_SPINLOCK(moxa_lock);
Alan Cox33f0f882006-01-09 20:54:13 -0800265
Jiri Slaby03718232008-04-30 00:53:39 -0700266static int moxa_check_fw_model(struct moxa_board_conf *brd, u8 model)
267{
268 switch (brd->boardType) {
269 case MOXA_BOARD_C218_ISA:
270 case MOXA_BOARD_C218_PCI:
271 if (model != 1)
272 goto err;
273 break;
274 case MOXA_BOARD_CP204J:
275 if (model != 3)
276 goto err;
277 break;
278 default:
279 if (model != 2)
280 goto err;
281 break;
282 }
283 return 0;
284err:
285 return -EINVAL;
286}
287
288static int moxa_check_fw(const void *ptr)
289{
290 const __le16 *lptr = ptr;
291
292 if (*lptr != cpu_to_le16(0x7980))
293 return -EINVAL;
294
295 return 0;
296}
297
298static int moxa_load_bios(struct moxa_board_conf *brd, const u8 *buf,
299 size_t len)
300{
301 void __iomem *baseAddr = brd->basemem;
302 u16 tmp;
303
304 writeb(HW_reset, baseAddr + Control_reg); /* reset */
305 msleep(10);
306 memset_io(baseAddr, 0, 4096);
307 memcpy_toio(baseAddr, buf, len); /* download BIOS */
308 writeb(0, baseAddr + Control_reg); /* restart */
309
310 msleep(2000);
311
312 switch (brd->boardType) {
313 case MOXA_BOARD_C218_ISA:
314 case MOXA_BOARD_C218_PCI:
315 tmp = readw(baseAddr + C218_key);
316 if (tmp != C218_KeyCode)
317 goto err;
318 break;
319 case MOXA_BOARD_CP204J:
320 tmp = readw(baseAddr + C218_key);
321 if (tmp != CP204J_KeyCode)
322 goto err;
323 break;
324 default:
325 tmp = readw(baseAddr + C320_key);
326 if (tmp != C320_KeyCode)
327 goto err;
328 tmp = readw(baseAddr + C320_status);
329 if (tmp != STS_init) {
330 printk(KERN_ERR "moxa: bios upload failed -- CPU/Basic "
331 "module not found\n");
332 return -EIO;
333 }
334 break;
335 }
336
337 return 0;
338err:
339 printk(KERN_ERR "moxa: bios upload failed -- board not found\n");
340 return -EIO;
341}
342
343static int moxa_load_320b(struct moxa_board_conf *brd, const u8 *ptr,
344 size_t len)
345{
346 void __iomem *baseAddr = brd->basemem;
347
348 if (len < 7168) {
349 printk(KERN_ERR "moxa: invalid 320 bios -- too short\n");
350 return -EINVAL;
351 }
352
353 writew(len - 7168 - 2, baseAddr + C320bapi_len);
354 writeb(1, baseAddr + Control_reg); /* Select Page 1 */
355 memcpy_toio(baseAddr + DynPage_addr, ptr, 7168);
356 writeb(2, baseAddr + Control_reg); /* Select Page 2 */
357 memcpy_toio(baseAddr + DynPage_addr, ptr + 7168, len - 7168);
358
359 return 0;
360}
361
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700362static int moxa_real_load_code(struct moxa_board_conf *brd, const void *ptr,
Jiri Slaby03718232008-04-30 00:53:39 -0700363 size_t len)
364{
365 void __iomem *baseAddr = brd->basemem;
366 const u16 *uptr = ptr;
367 size_t wlen, len2, j;
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700368 unsigned long key, loadbuf, loadlen, checksum, checksum_ok;
369 unsigned int i, retry, c320;
Jiri Slaby03718232008-04-30 00:53:39 -0700370 u16 usum, keycode;
371
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700372 c320 = brd->boardType == MOXA_BOARD_C320_PCI ||
373 brd->boardType == MOXA_BOARD_C320_ISA;
374 keycode = (brd->boardType == MOXA_BOARD_CP204J) ? CP204J_KeyCode :
375 C218_KeyCode;
376
377 switch (brd->boardType) {
378 case MOXA_BOARD_CP204J:
379 case MOXA_BOARD_C218_ISA:
380 case MOXA_BOARD_C218_PCI:
381 key = C218_key;
382 loadbuf = C218_LoadBuf;
383 loadlen = C218DLoad_len;
384 checksum = C218check_sum;
385 checksum_ok = C218chksum_ok;
386 break;
387 default:
388 key = C320_key;
389 keycode = C320_KeyCode;
390 loadbuf = C320_LoadBuf;
391 loadlen = C320DLoad_len;
392 checksum = C320check_sum;
393 checksum_ok = C320chksum_ok;
394 break;
395 }
396
Jiri Slaby03718232008-04-30 00:53:39 -0700397 usum = 0;
398 wlen = len >> 1;
399 for (i = 0; i < wlen; i++)
400 usum += le16_to_cpu(uptr[i]);
401 retry = 0;
402 do {
403 wlen = len >> 1;
404 j = 0;
405 while (wlen) {
406 len2 = (wlen > 2048) ? 2048 : wlen;
407 wlen -= len2;
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700408 memcpy_toio(baseAddr + loadbuf, ptr + j, len2 << 1);
Jiri Slaby03718232008-04-30 00:53:39 -0700409 j += len2 << 1;
410
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700411 writew(len2, baseAddr + loadlen);
412 writew(0, baseAddr + key);
Jiri Slaby03718232008-04-30 00:53:39 -0700413 for (i = 0; i < 100; i++) {
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700414 if (readw(baseAddr + key) == keycode)
Jiri Slaby03718232008-04-30 00:53:39 -0700415 break;
416 msleep(10);
417 }
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700418 if (readw(baseAddr + key) != keycode)
Jiri Slaby03718232008-04-30 00:53:39 -0700419 return -EIO;
420 }
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700421 writew(0, baseAddr + loadlen);
422 writew(usum, baseAddr + checksum);
423 writew(0, baseAddr + key);
Jiri Slaby03718232008-04-30 00:53:39 -0700424 for (i = 0; i < 100; i++) {
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700425 if (readw(baseAddr + key) == keycode)
Jiri Slaby03718232008-04-30 00:53:39 -0700426 break;
427 msleep(10);
428 }
429 retry++;
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700430 } while ((readb(baseAddr + checksum_ok) != 1) && (retry < 3));
431 if (readb(baseAddr + checksum_ok) != 1)
Jiri Slaby03718232008-04-30 00:53:39 -0700432 return -EIO;
433
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700434 writew(0, baseAddr + key);
Jiri Slaby03718232008-04-30 00:53:39 -0700435 for (i = 0; i < 600; i++) {
436 if (readw(baseAddr + Magic_no) == Magic_code)
437 break;
438 msleep(10);
439 }
440 if (readw(baseAddr + Magic_no) != Magic_code)
441 return -EIO;
442
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700443 if (c320) {
444 if (brd->busType == MOXA_BUS_TYPE_PCI) { /* ASIC board */
445 writew(0x3800, baseAddr + TMS320_PORT1);
446 writew(0x3900, baseAddr + TMS320_PORT2);
447 writew(28499, baseAddr + TMS320_CLOCK);
448 } else {
449 writew(0x3200, baseAddr + TMS320_PORT1);
450 writew(0x3400, baseAddr + TMS320_PORT2);
451 writew(19999, baseAddr + TMS320_CLOCK);
452 }
Jiri Slaby03718232008-04-30 00:53:39 -0700453 }
454 writew(1, baseAddr + Disable_IRQ);
455 writew(0, baseAddr + Magic_no);
456 for (i = 0; i < 500; i++) {
457 if (readw(baseAddr + Magic_no) == Magic_code)
458 break;
459 msleep(10);
460 }
461 if (readw(baseAddr + Magic_no) != Magic_code)
462 return -EIO;
463
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700464 if (c320) {
465 j = readw(baseAddr + Module_cnt);
466 if (j <= 0)
467 return -EIO;
468 brd->numPorts = j * 8;
469 writew(j, baseAddr + Module_no);
470 writew(0, baseAddr + Magic_no);
471 for (i = 0; i < 600; i++) {
472 if (readw(baseAddr + Magic_no) == Magic_code)
473 break;
474 msleep(10);
475 }
476 if (readw(baseAddr + Magic_no) != Magic_code)
477 return -EIO;
Jiri Slaby03718232008-04-30 00:53:39 -0700478 }
Jiri Slaby03718232008-04-30 00:53:39 -0700479 brd->intNdx = baseAddr + IRQindex;
480 brd->intPend = baseAddr + IRQpending;
481 brd->intTable = baseAddr + IRQtable;
482
483 return 0;
484}
485
486static int moxa_load_code(struct moxa_board_conf *brd, const void *ptr,
487 size_t len)
488{
489 void __iomem *ofsAddr, *baseAddr = brd->basemem;
490 struct moxa_port *port;
491 int retval, i;
492
493 if (len % 2) {
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700494 printk(KERN_ERR "moxa: bios length is not even\n");
Jiri Slaby03718232008-04-30 00:53:39 -0700495 return -EINVAL;
496 }
497
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700498 retval = moxa_real_load_code(brd, ptr, len); /* may change numPorts */
499 if (retval)
500 return retval;
501
Jiri Slaby03718232008-04-30 00:53:39 -0700502 switch (brd->boardType) {
503 case MOXA_BOARD_C218_ISA:
504 case MOXA_BOARD_C218_PCI:
505 case MOXA_BOARD_CP204J:
Jiri Slaby03718232008-04-30 00:53:39 -0700506 port = brd->ports;
507 for (i = 0; i < brd->numPorts; i++, port++) {
Jiri Slabyb4173f42008-04-30 00:53:40 -0700508 port->board = brd;
Jiri Slaby03718232008-04-30 00:53:39 -0700509 port->DCDState = 0;
510 port->tableAddr = baseAddr + Extern_table +
511 Extern_size * i;
512 ofsAddr = port->tableAddr;
513 writew(C218rx_mask, ofsAddr + RX_mask);
514 writew(C218tx_mask, ofsAddr + TX_mask);
515 writew(C218rx_spage + i * C218buf_pageno, ofsAddr + Page_rxb);
516 writew(readw(ofsAddr + Page_rxb) + C218rx_pageno, ofsAddr + EndPage_rxb);
517
518 writew(C218tx_spage + i * C218buf_pageno, ofsAddr + Page_txb);
519 writew(readw(ofsAddr + Page_txb) + C218tx_pageno, ofsAddr + EndPage_txb);
520
521 }
522 break;
523 default:
Jiri Slaby03718232008-04-30 00:53:39 -0700524 port = brd->ports;
525 for (i = 0; i < brd->numPorts; i++, port++) {
Jiri Slabyb4173f42008-04-30 00:53:40 -0700526 port->board = brd;
Jiri Slaby03718232008-04-30 00:53:39 -0700527 port->DCDState = 0;
528 port->tableAddr = baseAddr + Extern_table +
529 Extern_size * i;
530 ofsAddr = port->tableAddr;
531 switch (brd->numPorts) {
532 case 8:
533 writew(C320p8rx_mask, ofsAddr + RX_mask);
534 writew(C320p8tx_mask, ofsAddr + TX_mask);
535 writew(C320p8rx_spage + i * C320p8buf_pgno, ofsAddr + Page_rxb);
536 writew(readw(ofsAddr + Page_rxb) + C320p8rx_pgno, ofsAddr + EndPage_rxb);
537 writew(C320p8tx_spage + i * C320p8buf_pgno, ofsAddr + Page_txb);
538 writew(readw(ofsAddr + Page_txb) + C320p8tx_pgno, ofsAddr + EndPage_txb);
539
540 break;
541 case 16:
542 writew(C320p16rx_mask, ofsAddr + RX_mask);
543 writew(C320p16tx_mask, ofsAddr + TX_mask);
544 writew(C320p16rx_spage + i * C320p16buf_pgno, ofsAddr + Page_rxb);
545 writew(readw(ofsAddr + Page_rxb) + C320p16rx_pgno, ofsAddr + EndPage_rxb);
546 writew(C320p16tx_spage + i * C320p16buf_pgno, ofsAddr + Page_txb);
547 writew(readw(ofsAddr + Page_txb) + C320p16tx_pgno, ofsAddr + EndPage_txb);
548 break;
549
550 case 24:
551 writew(C320p24rx_mask, ofsAddr + RX_mask);
552 writew(C320p24tx_mask, ofsAddr + TX_mask);
553 writew(C320p24rx_spage + i * C320p24buf_pgno, ofsAddr + Page_rxb);
554 writew(readw(ofsAddr + Page_rxb) + C320p24rx_pgno, ofsAddr + EndPage_rxb);
555 writew(C320p24tx_spage + i * C320p24buf_pgno, ofsAddr + Page_txb);
556 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
557 break;
558 case 32:
559 writew(C320p32rx_mask, ofsAddr + RX_mask);
560 writew(C320p32tx_mask, ofsAddr + TX_mask);
561 writew(C320p32tx_ofs, ofsAddr + Ofs_txb);
562 writew(C320p32rx_spage + i * C320p32buf_pgno, ofsAddr + Page_rxb);
563 writew(readb(ofsAddr + Page_rxb), ofsAddr + EndPage_rxb);
564 writew(C320p32tx_spage + i * C320p32buf_pgno, ofsAddr + Page_txb);
565 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
566 break;
567 }
568 }
569 break;
570 }
Jiri Slaby03718232008-04-30 00:53:39 -0700571 return 0;
572}
573
574static int moxa_load_fw(struct moxa_board_conf *brd, const struct firmware *fw)
575{
576 void *ptr = fw->data;
577 char rsn[64];
578 u16 lens[5];
579 size_t len;
580 unsigned int a, lenp, lencnt;
581 int ret = -EINVAL;
582 struct {
583 __le32 magic; /* 0x34303430 */
584 u8 reserved1[2];
585 u8 type; /* UNIX = 3 */
586 u8 model; /* C218T=1, C320T=2, CP204=3 */
587 u8 reserved2[8];
588 __le16 len[5];
589 } *hdr = ptr;
590
591 BUILD_BUG_ON(ARRAY_SIZE(hdr->len) != ARRAY_SIZE(lens));
592
593 if (fw->size < MOXA_FW_HDRLEN) {
594 strcpy(rsn, "too short (even header won't fit)");
595 goto err;
596 }
597 if (hdr->magic != cpu_to_le32(0x30343034)) {
598 sprintf(rsn, "bad magic: %.8x", le32_to_cpu(hdr->magic));
599 goto err;
600 }
601 if (hdr->type != 3) {
602 sprintf(rsn, "not for linux, type is %u", hdr->type);
603 goto err;
604 }
605 if (moxa_check_fw_model(brd, hdr->model)) {
606 sprintf(rsn, "not for this card, model is %u", hdr->model);
607 goto err;
608 }
609
610 len = MOXA_FW_HDRLEN;
611 lencnt = hdr->model == 2 ? 5 : 3;
612 for (a = 0; a < ARRAY_SIZE(lens); a++) {
613 lens[a] = le16_to_cpu(hdr->len[a]);
614 if (lens[a] && len + lens[a] <= fw->size &&
615 moxa_check_fw(&fw->data[len]))
616 printk(KERN_WARNING "moxa firmware: unexpected input "
617 "at offset %u, but going on\n", (u32)len);
618 if (!lens[a] && a < lencnt) {
619 sprintf(rsn, "too few entries in fw file");
620 goto err;
621 }
622 len += lens[a];
623 }
624
625 if (len != fw->size) {
626 sprintf(rsn, "bad length: %u (should be %u)", (u32)fw->size,
627 (u32)len);
628 goto err;
629 }
630
631 ptr += MOXA_FW_HDRLEN;
632 lenp = 0; /* bios */
633
634 strcpy(rsn, "read above");
635
636 ret = moxa_load_bios(brd, ptr, lens[lenp]);
637 if (ret)
638 goto err;
639
640 /* we skip the tty section (lens[1]), since we don't need it */
641 ptr += lens[lenp] + lens[lenp + 1];
642 lenp += 2; /* comm */
643
644 if (hdr->model == 2) {
645 ret = moxa_load_320b(brd, ptr, lens[lenp]);
646 if (ret)
647 goto err;
648 /* skip another tty */
649 ptr += lens[lenp] + lens[lenp + 1];
650 lenp += 2;
651 }
652
653 ret = moxa_load_code(brd, ptr, lens[lenp]);
654 if (ret)
655 goto err;
656
657 return 0;
658err:
659 printk(KERN_ERR "firmware failed to load, reason: %s\n", rsn);
660 return ret;
661}
662
663static int moxa_init_board(struct moxa_board_conf *brd, struct device *dev)
664{
665 const struct firmware *fw;
666 const char *file;
Jiri Slaby810ab092008-04-30 00:53:41 -0700667 struct moxa_port *p;
668 unsigned int i;
Jiri Slaby03718232008-04-30 00:53:39 -0700669 int ret;
670
Jiri Slaby810ab092008-04-30 00:53:41 -0700671 brd->ports = kcalloc(MAX_PORTS_PER_BOARD, sizeof(*brd->ports),
672 GFP_KERNEL);
673 if (brd->ports == NULL) {
674 printk(KERN_ERR "cannot allocate memory for ports\n");
675 ret = -ENOMEM;
676 goto err;
677 }
678
679 for (i = 0, p = brd->ports; i < MAX_PORTS_PER_BOARD; i++, p++) {
680 p->type = PORT_16550A;
681 p->close_delay = 5 * HZ / 10;
682 p->cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
683 init_waitqueue_head(&p->open_wait);
684 init_completion(&p->close_wait);
685
686 setup_timer(&p->emptyTimer, moxa_check_xmit_empty,
687 (unsigned long)p);
688 }
689
Jiri Slaby03718232008-04-30 00:53:39 -0700690 switch (brd->boardType) {
691 case MOXA_BOARD_C218_ISA:
692 case MOXA_BOARD_C218_PCI:
693 file = "c218tunx.cod";
694 break;
695 case MOXA_BOARD_CP204J:
696 file = "cp204unx.cod";
697 break;
698 default:
699 file = "c320tunx.cod";
700 break;
701 }
702
703 ret = request_firmware(&fw, file, dev);
704 if (ret) {
705 printk(KERN_ERR "request_firmware failed\n");
Jiri Slaby810ab092008-04-30 00:53:41 -0700706 goto err_free;
Jiri Slaby03718232008-04-30 00:53:39 -0700707 }
708
709 ret = moxa_load_fw(brd, fw);
710
711 release_firmware(fw);
Jiri Slaby810ab092008-04-30 00:53:41 -0700712
713 if (ret)
714 goto err_free;
715
716 brd->ready = 1;
717
718 return 0;
719err_free:
720 kfree(brd->ports);
721err:
Jiri Slaby03718232008-04-30 00:53:39 -0700722 return ret;
723}
724
Jiri Slaby810ab092008-04-30 00:53:41 -0700725static void moxa_board_deinit(struct moxa_board_conf *brd)
726{
727 unsigned int i;
728
729 brd->ready = 0;
730 for (i = 0; i < MAX_PORTS_PER_BOARD; i++)
731 del_timer_sync(&brd->ports[i].emptyTimer);
732
733 iounmap(brd->basemem);
734 brd->basemem = NULL;
735 kfree(brd->ports);
736}
737
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738#ifdef CONFIG_PCI
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800739static int __devinit moxa_pci_probe(struct pci_dev *pdev,
740 const struct pci_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741{
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800742 struct moxa_board_conf *board;
743 unsigned int i;
744 int board_type = ent->driver_data;
745 int retval;
746
747 retval = pci_enable_device(pdev);
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700748 if (retval) {
749 dev_err(&pdev->dev, "can't enable pci device\n");
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800750 goto err;
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700751 }
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800752
753 for (i = 0; i < MAX_BOARDS; i++)
754 if (moxa_boards[i].basemem == NULL)
755 break;
756
757 retval = -ENODEV;
758 if (i >= MAX_BOARDS) {
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700759 dev_warn(&pdev->dev, "more than %u MOXA Intellio family boards "
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800760 "found. Board is ignored.\n", MAX_BOARDS);
761 goto err;
762 }
763
764 board = &moxa_boards[i];
Jiri Slabye46a5e32008-04-30 00:53:37 -0700765
766 retval = pci_request_region(pdev, 2, "moxa-base");
767 if (retval) {
768 dev_err(&pdev->dev, "can't request pci region 2\n");
769 goto err;
770 }
771
772 board->basemem = ioremap(pci_resource_start(pdev, 2), 0x4000);
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700773 if (board->basemem == NULL) {
774 dev_err(&pdev->dev, "can't remap io space 2\n");
Jiri Slabye46a5e32008-04-30 00:53:37 -0700775 goto err_reg;
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700776 }
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800777
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 board->boardType = board_type;
779 switch (board_type) {
780 case MOXA_BOARD_C218_ISA:
781 case MOXA_BOARD_C218_PCI:
782 board->numPorts = 8;
783 break;
784
785 case MOXA_BOARD_CP204J:
786 board->numPorts = 4;
787 break;
788 default:
789 board->numPorts = 0;
790 break;
791 }
792 board->busType = MOXA_BUS_TYPE_PCI;
Jiri Slabya784bf72007-02-10 01:45:36 -0800793
Jiri Slaby03718232008-04-30 00:53:39 -0700794 retval = moxa_init_board(board, &pdev->dev);
795 if (retval)
796 goto err_base;
797
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800798 pci_set_drvdata(pdev, board);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
800 return (0);
Jiri Slaby03718232008-04-30 00:53:39 -0700801err_base:
802 iounmap(board->basemem);
803 board->basemem = NULL;
Jiri Slabye46a5e32008-04-30 00:53:37 -0700804err_reg:
805 pci_release_region(pdev, 2);
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800806err:
807 return retval;
808}
809
810static void __devexit moxa_pci_remove(struct pci_dev *pdev)
811{
812 struct moxa_board_conf *brd = pci_get_drvdata(pdev);
813
Jiri Slaby810ab092008-04-30 00:53:41 -0700814 moxa_board_deinit(brd);
815
Jiri Slabye46a5e32008-04-30 00:53:37 -0700816 pci_release_region(pdev, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817}
Jiri Slabya784bf72007-02-10 01:45:36 -0800818
819static struct pci_driver moxa_pci_driver = {
820 .name = "moxa",
821 .id_table = moxa_pcibrds,
822 .probe = moxa_pci_probe,
823 .remove = __devexit_p(moxa_pci_remove)
824};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825#endif /* CONFIG_PCI */
826
827static int __init moxa_init(void)
828{
Jiri Slaby810ab092008-04-30 00:53:41 -0700829 unsigned int isabrds = 0;
Jiri Slabyd353eca2008-04-30 00:53:37 -0700830 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700832 printk(KERN_INFO "MOXA Intellio family driver version %s\n",
833 MOXA_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 moxaDriver = alloc_tty_driver(MAX_PORTS + 1);
835 if (!moxaDriver)
836 return -ENOMEM;
837
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 moxaDriver->owner = THIS_MODULE;
Sergey Vlasov9b4e3b12005-09-03 16:26:49 +0100839 moxaDriver->name = "ttyMX";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 moxaDriver->major = ttymajor;
841 moxaDriver->minor_start = 0;
842 moxaDriver->type = TTY_DRIVER_TYPE_SERIAL;
843 moxaDriver->subtype = SERIAL_TYPE_NORMAL;
844 moxaDriver->init_termios = tty_std_termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 moxaDriver->init_termios.c_cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
Alan Cox606d0992006-12-08 02:38:45 -0800846 moxaDriver->init_termios.c_ispeed = 9600;
847 moxaDriver->init_termios.c_ospeed = 9600;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 moxaDriver->flags = TTY_DRIVER_REAL_RAW;
849 tty_set_operations(moxaDriver, &moxa_ops);
850
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700851 pr_debug("Moxa tty devices major number = %d\n", ttymajor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
853 if (tty_register_driver(moxaDriver)) {
854 printk(KERN_ERR "Couldn't install MOXA Smartio family driver !\n");
855 put_tty_driver(moxaDriver);
856 return -1;
857 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
Jiri Slabyaa7e5222007-02-10 01:45:27 -0800859 mod_timer(&moxaTimer, jiffies + HZ / 50);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
Jiri Slabyd353eca2008-04-30 00:53:37 -0700861 /* Find the boards defined from module args. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862#ifdef MODULE
Jiri Slabyd353eca2008-04-30 00:53:37 -0700863 {
864 struct moxa_board_conf *brd = moxa_boards;
Jiri Slaby810ab092008-04-30 00:53:41 -0700865 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 for (i = 0; i < MAX_BOARDS; i++) {
Jiri Slabyd353eca2008-04-30 00:53:37 -0700867 if (!baseaddr[i])
868 break;
869 if (type[i] == MOXA_BOARD_C218_ISA ||
870 type[i] == MOXA_BOARD_C320_ISA) {
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700871 pr_debug("Moxa board %2d: %s board(baseAddr=%lx)\n",
Jiri Slabyd353eca2008-04-30 00:53:37 -0700872 isabrds + 1, moxa_brdname[type[i] - 1],
873 baseaddr[i]);
874 brd->boardType = type[i];
875 brd->numPorts = type[i] == MOXA_BOARD_C218_ISA ? 8 :
876 numports[i];
877 brd->busType = MOXA_BUS_TYPE_ISA;
878 brd->basemem = ioremap(baseaddr[i], 0x4000);
879 if (!brd->basemem) {
880 printk(KERN_ERR "moxa: can't remap %lx\n",
881 baseaddr[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 continue;
883 }
Jiri Slaby03718232008-04-30 00:53:39 -0700884 if (moxa_init_board(brd, NULL)) {
885 iounmap(brd->basemem);
886 brd->basemem = NULL;
887 continue;
888 }
Jiri Slabyd353eca2008-04-30 00:53:37 -0700889
890 brd++;
891 isabrds++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 }
893 }
Jiri Slabyd353eca2008-04-30 00:53:37 -0700894 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895#endif
Jiri Slabya784bf72007-02-10 01:45:36 -0800896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897#ifdef CONFIG_PCI
Jiri Slabya784bf72007-02-10 01:45:36 -0800898 retval = pci_register_driver(&moxa_pci_driver);
899 if (retval) {
900 printk(KERN_ERR "Can't register moxa pci driver!\n");
Jiri Slabyd353eca2008-04-30 00:53:37 -0700901 if (isabrds)
Jiri Slabya784bf72007-02-10 01:45:36 -0800902 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 }
904#endif
Jiri Slabya784bf72007-02-10 01:45:36 -0800905
Jiri Slabya784bf72007-02-10 01:45:36 -0800906 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907}
908
909static void __exit moxa_exit(void)
910{
911 int i;
912
Jiri Slabyc251ae02007-02-10 01:45:32 -0800913 del_timer_sync(&moxaTimer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 if (tty_unregister_driver(moxaDriver))
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700916 printk(KERN_ERR "Couldn't unregister MOXA Intellio family "
917 "serial driver\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 put_tty_driver(moxaDriver);
Jiri Slaby86fbf142006-10-21 10:24:01 -0700919
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800920#ifdef CONFIG_PCI
Jiri Slabya784bf72007-02-10 01:45:36 -0800921 pci_unregister_driver(&moxa_pci_driver);
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800922#endif
Jiri Slabya784bf72007-02-10 01:45:36 -0800923
Jiri Slaby810ab092008-04-30 00:53:41 -0700924 for (i = 0; i < MAX_BOARDS; i++) /* ISA boards */
925 if (moxa_boards[i].ready)
926 moxa_board_deinit(&moxa_boards[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927}
928
929module_init(moxa_init);
930module_exit(moxa_exit);
931
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932static int moxa_open(struct tty_struct *tty, struct file *filp)
933{
Jiri Slaby810ab092008-04-30 00:53:41 -0700934 struct moxa_board_conf *brd;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800935 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 int port;
937 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Jiri Slaby11324ed2007-02-10 01:45:31 -0800939 port = tty->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 if (port == MAX_PORTS) {
941 return (0);
942 }
Jiri Slaby810ab092008-04-30 00:53:41 -0700943 brd = &moxa_boards[port / MAX_PORTS_PER_BOARD];
944 if (!brd->ready)
945 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
Jiri Slaby810ab092008-04-30 00:53:41 -0700947 ch = &brd->ports[port % MAX_PORTS_PER_BOARD];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 ch->count++;
949 tty->driver_data = ch;
950 ch->tty = tty;
951 if (!(ch->asyncflags & ASYNC_INITIALIZED)) {
952 ch->statusflags = 0;
Alan Coxdb1acaa2008-02-08 04:18:43 -0800953 moxa_set_tty_param(tty, tty->termios);
Jiri Slabyb4173f42008-04-30 00:53:40 -0700954 MoxaPortLineCtrl(ch, 1, 1);
955 MoxaPortEnable(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 ch->asyncflags |= ASYNC_INITIALIZED;
957 }
Jiri Slaby6f56b6582007-10-18 03:06:24 -0700958 retval = moxa_block_till_ready(tty, filp, ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
960 moxa_unthrottle(tty);
961
962 if (ch->type == PORT_16550A) {
Jiri Slabyb4173f42008-04-30 00:53:40 -0700963 MoxaSetFifo(ch, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 } else {
Jiri Slabyb4173f42008-04-30 00:53:40 -0700965 MoxaSetFifo(ch, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 }
967
968 return (retval);
969}
970
971static void moxa_close(struct tty_struct *tty, struct file *filp)
972{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800973 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 int port;
975
Jiri Slaby11324ed2007-02-10 01:45:31 -0800976 port = tty->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 if (port == MAX_PORTS) {
978 return;
979 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 if (tty->driver_data == NULL) {
981 return;
982 }
983 if (tty_hung_up_p(filp)) {
984 return;
985 }
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800986 ch = (struct moxa_port *) tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
988 if ((tty->count == 1) && (ch->count != 1)) {
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700989 printk(KERN_WARNING "moxa_close: bad serial port count; "
990 "tty->count is 1, ch->count is %d\n", ch->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 ch->count = 1;
992 }
993 if (--ch->count < 0) {
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700994 printk(KERN_WARNING "moxa_close: bad serial port count, "
995 "device=%s\n", tty->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 ch->count = 0;
997 }
998 if (ch->count) {
999 return;
1000 }
1001 ch->asyncflags |= ASYNC_CLOSING;
1002
1003 ch->cflag = tty->termios->c_cflag;
1004 if (ch->asyncflags & ASYNC_INITIALIZED) {
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001005 moxa_setup_empty_event(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 tty_wait_until_sent(tty, 30 * HZ); /* 30 seconds timeout */
Jiri Slabyb4173f42008-04-30 00:53:40 -07001007 del_timer_sync(&ch->emptyTimer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 }
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001009 moxa_shut_down(ch);
Jiri Slabyb4173f42008-04-30 00:53:40 -07001010 MoxaPortFlushData(ch, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
1012 if (tty->driver->flush_buffer)
1013 tty->driver->flush_buffer(tty);
1014 tty_ldisc_flush(tty);
1015
1016 tty->closing = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 ch->tty = NULL;
1018 if (ch->blocked_open) {
1019 if (ch->close_delay) {
1020 msleep_interruptible(jiffies_to_msecs(ch->close_delay));
1021 }
1022 wake_up_interruptible(&ch->open_wait);
1023 }
1024 ch->asyncflags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
Jiri Slaby95e07912007-10-18 03:06:25 -07001025 complete_all(&ch->close_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026}
1027
1028static int moxa_write(struct tty_struct *tty,
1029 const unsigned char *buf, int count)
1030{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001031 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 unsigned long flags;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001033 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 if (ch == NULL)
Jiri Slabyb4173f42008-04-30 00:53:40 -07001036 return 0;
Alan Cox33f0f882006-01-09 20:54:13 -08001037
1038 spin_lock_irqsave(&moxa_lock, flags);
Jiri Slabyb4173f42008-04-30 00:53:40 -07001039 len = MoxaPortWriteData(ch, (unsigned char *) buf, count);
Alan Cox33f0f882006-01-09 20:54:13 -08001040 spin_unlock_irqrestore(&moxa_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
1042 /*********************************************
1043 if ( !(ch->statusflags & LOWWAIT) &&
1044 ((len != count) || (MoxaPortTxFree(port) <= 100)) )
1045 ************************************************/
1046 ch->statusflags |= LOWWAIT;
1047 return (len);
1048}
1049
1050static int moxa_write_room(struct tty_struct *tty)
1051{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001052 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
1054 if (tty->stopped)
1055 return (0);
Jiri Slabyb4173f42008-04-30 00:53:40 -07001056 ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 if (ch == NULL)
1058 return (0);
Jiri Slabyb4173f42008-04-30 00:53:40 -07001059 return MoxaPortTxFree(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060}
1061
1062static void moxa_flush_buffer(struct tty_struct *tty)
1063{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001064 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
1066 if (ch == NULL)
1067 return;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001068 MoxaPortFlushData(ch, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 tty_wakeup(tty);
1070}
1071
1072static int moxa_chars_in_buffer(struct tty_struct *tty)
1073{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001074 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 int chars;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
1077 /*
1078 * Sigh...I have to check if driver_data is NULL here, because
1079 * if an open() fails, the TTY subsystem eventually calls
1080 * tty_wait_until_sent(), which calls the driver's chars_in_buffer()
1081 * routine. And since the open() failed, we return 0 here. TDJ
1082 */
1083 if (ch == NULL)
1084 return (0);
Jiri Slabyb4173f42008-04-30 00:53:40 -07001085 chars = MoxaPortTxQueue(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 if (chars) {
1087 /*
1088 * Make it possible to wakeup anything waiting for output
1089 * in tty_ioctl.c, etc.
1090 */
1091 if (!(ch->statusflags & EMPTYWAIT))
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001092 moxa_setup_empty_event(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 }
1094 return (chars);
1095}
1096
1097static void moxa_flush_chars(struct tty_struct *tty)
1098{
1099 /*
1100 * Don't think I need this, because this is called to empty the TX
1101 * buffer for the 16450, 16550, etc.
1102 */
1103}
1104
1105static void moxa_put_char(struct tty_struct *tty, unsigned char c)
1106{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001107 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 unsigned long flags;
1109
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 if (ch == NULL)
1111 return;
Alan Cox33f0f882006-01-09 20:54:13 -08001112 spin_lock_irqsave(&moxa_lock, flags);
Jiri Slabyb4173f42008-04-30 00:53:40 -07001113 MoxaPortWriteData(ch, &c, 1);
Alan Cox33f0f882006-01-09 20:54:13 -08001114 spin_unlock_irqrestore(&moxa_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 /************************************************
1116 if ( !(ch->statusflags & LOWWAIT) && (MoxaPortTxFree(port) <= 100) )
1117 *************************************************/
1118 ch->statusflags |= LOWWAIT;
1119}
1120
1121static int moxa_tiocmget(struct tty_struct *tty, struct file *file)
1122{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001123 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 int flag = 0, dtr, rts;
1125
Jiri Slabyb4173f42008-04-30 00:53:40 -07001126 if ((tty->index != MAX_PORTS) && (!ch))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 return (-EINVAL);
1128
Jiri Slabyb4173f42008-04-30 00:53:40 -07001129 MoxaPortGetLineOut(ch, &dtr, &rts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 if (dtr)
1131 flag |= TIOCM_DTR;
1132 if (rts)
1133 flag |= TIOCM_RTS;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001134 dtr = MoxaPortLineStatus(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 if (dtr & 1)
1136 flag |= TIOCM_CTS;
1137 if (dtr & 2)
1138 flag |= TIOCM_DSR;
1139 if (dtr & 4)
1140 flag |= TIOCM_CD;
1141 return flag;
1142}
1143
1144static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
1145 unsigned int set, unsigned int clear)
1146{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001147 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 int port;
1149 int dtr, rts;
1150
Jiri Slaby11324ed2007-02-10 01:45:31 -08001151 port = tty->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 if ((port != MAX_PORTS) && (!ch))
1153 return (-EINVAL);
1154
Jiri Slabyb4173f42008-04-30 00:53:40 -07001155 MoxaPortGetLineOut(ch, &dtr, &rts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 if (set & TIOCM_RTS)
1157 rts = 1;
1158 if (set & TIOCM_DTR)
1159 dtr = 1;
1160 if (clear & TIOCM_RTS)
1161 rts = 0;
1162 if (clear & TIOCM_DTR)
1163 dtr = 0;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001164 MoxaPortLineCtrl(ch, dtr, rts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 return 0;
1166}
1167
1168static int moxa_ioctl(struct tty_struct *tty, struct file *file,
1169 unsigned int cmd, unsigned long arg)
1170{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001171 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 register int port;
1173 void __user *argp = (void __user *)arg;
1174 int retval;
1175
Jiri Slaby11324ed2007-02-10 01:45:31 -08001176 port = tty->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 if ((port != MAX_PORTS) && (!ch))
1178 return (-EINVAL);
1179
1180 switch (cmd) {
1181 case TCSBRK: /* SVID version: non-zero arg --> no break */
1182 retval = tty_check_change(tty);
1183 if (retval)
1184 return (retval);
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001185 moxa_setup_empty_event(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 tty_wait_until_sent(tty, 0);
1187 if (!arg)
Jiri Slabyb4173f42008-04-30 00:53:40 -07001188 MoxaPortSendBreak(ch, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 return (0);
1190 case TCSBRKP: /* support for POSIX tcsendbreak() */
1191 retval = tty_check_change(tty);
1192 if (retval)
1193 return (retval);
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001194 moxa_setup_empty_event(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 tty_wait_until_sent(tty, 0);
Jiri Slabyb4173f42008-04-30 00:53:40 -07001196 MoxaPortSendBreak(ch, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 return (0);
1198 case TIOCGSOFTCAR:
Jiri Slaby9e9fc312008-04-30 00:53:38 -07001199 return put_user(C_CLOCAL(tty) ? 1 : 0, (int __user *)argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 case TIOCSSOFTCAR:
Jiri Slaby9e9fc312008-04-30 00:53:38 -07001201 if (get_user(retval, (int __user *)argp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 return -EFAULT;
1203 arg = retval;
1204 tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) |
1205 (arg ? CLOCAL : 0));
1206 if (C_CLOCAL(tty))
1207 ch->asyncflags &= ~ASYNC_CHECK_CD;
1208 else
1209 ch->asyncflags |= ASYNC_CHECK_CD;
1210 return (0);
1211 case TIOCGSERIAL:
1212 return moxa_get_serial_info(ch, argp);
1213
1214 case TIOCSSERIAL:
1215 return moxa_set_serial_info(ch, argp);
1216 default:
Jiri Slabyb4173f42008-04-30 00:53:40 -07001217 retval = MoxaDriverIoctl(tty, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 }
1219 return (retval);
1220}
1221
1222static void moxa_throttle(struct tty_struct *tty)
1223{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001224 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
1226 ch->statusflags |= THROTTLE;
1227}
1228
1229static void moxa_unthrottle(struct tty_struct *tty)
1230{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001231 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
1233 ch->statusflags &= ~THROTTLE;
1234}
1235
1236static void moxa_set_termios(struct tty_struct *tty,
Alan Cox606d0992006-12-08 02:38:45 -08001237 struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001239 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
1241 if (ch == NULL)
1242 return;
Alan Coxdb1acaa2008-02-08 04:18:43 -08001243 moxa_set_tty_param(tty, old_termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 if (!(old_termios->c_cflag & CLOCAL) &&
1245 (tty->termios->c_cflag & CLOCAL))
1246 wake_up_interruptible(&ch->open_wait);
1247}
1248
1249static void moxa_stop(struct tty_struct *tty)
1250{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001251 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
1253 if (ch == NULL)
1254 return;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001255 MoxaPortTxDisable(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 ch->statusflags |= TXSTOPPED;
1257}
1258
1259
1260static void moxa_start(struct tty_struct *tty)
1261{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001262 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
1264 if (ch == NULL)
1265 return;
1266
1267 if (!(ch->statusflags & TXSTOPPED))
1268 return;
1269
Jiri Slabyb4173f42008-04-30 00:53:40 -07001270 MoxaPortTxEnable(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 ch->statusflags &= ~TXSTOPPED;
1272}
1273
1274static void moxa_hangup(struct tty_struct *tty)
1275{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001276 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
1278 moxa_flush_buffer(tty);
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001279 moxa_shut_down(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 ch->count = 0;
1281 ch->asyncflags &= ~ASYNC_NORMAL_ACTIVE;
1282 ch->tty = NULL;
1283 wake_up_interruptible(&ch->open_wait);
1284}
1285
1286static void moxa_poll(unsigned long ignored)
1287{
1288 register int card;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001289 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 struct tty_struct *tp;
1291 int i, ports;
1292
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 del_timer(&moxaTimer);
1294
1295 if (MoxaDriverPoll() < 0) {
Jiri Slabyaa7e5222007-02-10 01:45:27 -08001296 mod_timer(&moxaTimer, jiffies + HZ / 50);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 return;
1298 }
1299 for (card = 0; card < MAX_BOARDS; card++) {
1300 if ((ports = MoxaPortsOfCard(card)) <= 0)
1301 continue;
Jiri Slaby810ab092008-04-30 00:53:41 -07001302 ch = moxa_boards[card].ports;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 for (i = 0; i < ports; i++, ch++) {
1304 if ((ch->asyncflags & ASYNC_INITIALIZED) == 0)
1305 continue;
1306 if (!(ch->statusflags & THROTTLE) &&
Jiri Slabyb4173f42008-04-30 00:53:40 -07001307 (MoxaPortRxQueue(ch) > 0))
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001308 moxa_receive_data(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 if ((tp = ch->tty) == 0)
1310 continue;
1311 if (ch->statusflags & LOWWAIT) {
Jiri Slabyb4173f42008-04-30 00:53:40 -07001312 if (MoxaPortTxQueue(ch) <= WAKEUP_CHARS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 if (!tp->stopped) {
1314 ch->statusflags &= ~LOWWAIT;
1315 tty_wakeup(tp);
1316 }
1317 }
1318 }
Jiri Slabyb4173f42008-04-30 00:53:40 -07001319 if (!I_IGNBRK(tp) && (MoxaPortResetBrkCnt(ch) > 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 tty_insert_flip_char(tp, 0, TTY_BREAK);
1321 tty_schedule_flip(tp);
1322 }
Jiri Slabyb4173f42008-04-30 00:53:40 -07001323 if (MoxaPortDCDChange(ch)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 if (ch->asyncflags & ASYNC_CHECK_CD) {
Jiri Slabyb4173f42008-04-30 00:53:40 -07001325 if (MoxaPortDCDON(ch))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 wake_up_interruptible(&ch->open_wait);
1327 else {
Jiri Slabyba196df2007-02-10 01:45:28 -08001328 tty_hangup(tp);
1329 wake_up_interruptible(&ch->open_wait);
1330 ch->asyncflags &= ~ASYNC_NORMAL_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 }
1332 }
1333 }
1334 }
1335 }
1336
Jiri Slabyaa7e5222007-02-10 01:45:27 -08001337 mod_timer(&moxaTimer, jiffies + HZ / 50);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338}
1339
1340/******************************************************************************/
1341
Alan Coxdb1acaa2008-02-08 04:18:43 -08001342static void moxa_set_tty_param(struct tty_struct *tty, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343{
Alan Cox606d0992006-12-08 02:38:45 -08001344 register struct ktermios *ts;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001345 struct moxa_port *ch;
Alan Coxdb1acaa2008-02-08 04:18:43 -08001346 int rts, cts, txflow, rxflow, xany, baud;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001348 ch = (struct moxa_port *) tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 ts = tty->termios;
1350 if (ts->c_cflag & CLOCAL)
1351 ch->asyncflags &= ~ASYNC_CHECK_CD;
1352 else
1353 ch->asyncflags |= ASYNC_CHECK_CD;
1354 rts = cts = txflow = rxflow = xany = 0;
1355 if (ts->c_cflag & CRTSCTS)
1356 rts = cts = 1;
1357 if (ts->c_iflag & IXON)
1358 txflow = 1;
1359 if (ts->c_iflag & IXOFF)
1360 rxflow = 1;
1361 if (ts->c_iflag & IXANY)
1362 xany = 1;
Alan Coxdb1acaa2008-02-08 04:18:43 -08001363
1364 /* Clear the features we don't support */
1365 ts->c_cflag &= ~CMSPAR;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001366 MoxaPortFlowCtrl(ch, rts, cts, txflow, rxflow, xany);
1367 baud = MoxaPortSetTermio(ch, ts, tty_get_baud_rate(tty));
Alan Coxdb1acaa2008-02-08 04:18:43 -08001368 if (baud == -1)
1369 baud = tty_termios_baud_rate(old_termios);
1370 /* Not put the baud rate into the termios data */
1371 tty_encode_baud_rate(tty, baud, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372}
1373
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001374static int moxa_block_till_ready(struct tty_struct *tty, struct file *filp,
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001375 struct moxa_port *ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376{
1377 DECLARE_WAITQUEUE(wait,current);
1378 unsigned long flags;
1379 int retval;
1380 int do_clocal = C_CLOCAL(tty);
1381
1382 /*
1383 * If the device is in the middle of being closed, then block
1384 * until it's done, and then try again.
1385 */
1386 if (tty_hung_up_p(filp) || (ch->asyncflags & ASYNC_CLOSING)) {
1387 if (ch->asyncflags & ASYNC_CLOSING)
Jiri Slaby95e07912007-10-18 03:06:25 -07001388 wait_for_completion_interruptible(&ch->close_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389#ifdef SERIAL_DO_RESTART
1390 if (ch->asyncflags & ASYNC_HUP_NOTIFY)
1391 return (-EAGAIN);
1392 else
1393 return (-ERESTARTSYS);
1394#else
1395 return (-EAGAIN);
1396#endif
1397 }
1398 /*
1399 * If non-blocking mode is set, then make the check up front
1400 * and then exit.
1401 */
1402 if (filp->f_flags & O_NONBLOCK) {
1403 ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
1404 return (0);
1405 }
1406 /*
1407 * Block waiting for the carrier detect and the line to become free
1408 */
1409 retval = 0;
1410 add_wait_queue(&ch->open_wait, &wait);
Jiri Slaby7aeb95d2007-10-18 03:06:24 -07001411 pr_debug("block_til_ready before block: ttys%d, count = %d\n",
Jiri Slabyb4173f42008-04-30 00:53:40 -07001412 tty->index, ch->count);
Alan Cox33f0f882006-01-09 20:54:13 -08001413 spin_lock_irqsave(&moxa_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 if (!tty_hung_up_p(filp))
1415 ch->count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 ch->blocked_open++;
Alan Cox33f0f882006-01-09 20:54:13 -08001417 spin_unlock_irqrestore(&moxa_lock, flags);
1418
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 while (1) {
1420 set_current_state(TASK_INTERRUPTIBLE);
1421 if (tty_hung_up_p(filp) ||
1422 !(ch->asyncflags & ASYNC_INITIALIZED)) {
1423#ifdef SERIAL_DO_RESTART
1424 if (ch->asyncflags & ASYNC_HUP_NOTIFY)
1425 retval = -EAGAIN;
1426 else
1427 retval = -ERESTARTSYS;
1428#else
1429 retval = -EAGAIN;
1430#endif
1431 break;
1432 }
1433 if (!(ch->asyncflags & ASYNC_CLOSING) && (do_clocal ||
Jiri Slabyb4173f42008-04-30 00:53:40 -07001434 MoxaPortDCDON(ch)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 break;
1436
1437 if (signal_pending(current)) {
1438 retval = -ERESTARTSYS;
1439 break;
1440 }
1441 schedule();
1442 }
1443 set_current_state(TASK_RUNNING);
1444 remove_wait_queue(&ch->open_wait, &wait);
Alan Cox33f0f882006-01-09 20:54:13 -08001445
1446 spin_lock_irqsave(&moxa_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 if (!tty_hung_up_p(filp))
1448 ch->count++;
1449 ch->blocked_open--;
Alan Cox33f0f882006-01-09 20:54:13 -08001450 spin_unlock_irqrestore(&moxa_lock, flags);
Jiri Slaby7aeb95d2007-10-18 03:06:24 -07001451 pr_debug("block_til_ready after blocking: ttys%d, count = %d\n",
Jiri Slabyb4173f42008-04-30 00:53:40 -07001452 tty->index, ch->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 if (retval)
1454 return (retval);
Alan Cox33f0f882006-01-09 20:54:13 -08001455 /* FIXME: review to see if we need to use set_bit on these */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
Alan Cox33f0f882006-01-09 20:54:13 -08001457 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458}
1459
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001460static void moxa_setup_empty_event(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001462 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 unsigned long flags;
1464
Alan Cox33f0f882006-01-09 20:54:13 -08001465 spin_lock_irqsave(&moxa_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 ch->statusflags |= EMPTYWAIT;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001467 mod_timer(&ch->emptyTimer, jiffies + HZ);
Alan Cox33f0f882006-01-09 20:54:13 -08001468 spin_unlock_irqrestore(&moxa_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469}
1470
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001471static void moxa_check_xmit_empty(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001473 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001475 ch = (struct moxa_port *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 if (ch->tty && (ch->statusflags & EMPTYWAIT)) {
Jiri Slabyb4173f42008-04-30 00:53:40 -07001477 if (MoxaPortTxQueue(ch) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 ch->statusflags &= ~EMPTYWAIT;
1479 tty_wakeup(ch->tty);
1480 return;
1481 }
Jiri Slabyb4173f42008-04-30 00:53:40 -07001482 mod_timer(&ch->emptyTimer, round_jiffies(jiffies + HZ));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 } else
1484 ch->statusflags &= ~EMPTYWAIT;
1485}
1486
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001487static void moxa_shut_down(struct moxa_port *ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488{
1489 struct tty_struct *tp;
1490
1491 if (!(ch->asyncflags & ASYNC_INITIALIZED))
1492 return;
1493
1494 tp = ch->tty;
1495
Jiri Slabyb4173f42008-04-30 00:53:40 -07001496 MoxaPortDisable(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497
1498 /*
1499 * If we're a modem control device and HUPCL is on, drop RTS & DTR.
1500 */
1501 if (tp->termios->c_cflag & HUPCL)
Jiri Slabyb4173f42008-04-30 00:53:40 -07001502 MoxaPortLineCtrl(ch, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
1504 ch->asyncflags &= ~ASYNC_INITIALIZED;
1505}
1506
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001507static void moxa_receive_data(struct moxa_port *ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508{
1509 struct tty_struct *tp;
Alan Cox606d0992006-12-08 02:38:45 -08001510 struct ktermios *ts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 unsigned long flags;
1512
1513 ts = NULL;
1514 tp = ch->tty;
1515 if (tp)
1516 ts = tp->termios;
1517 /**************************************************
1518 if ( !tp || !ts || !(ts->c_cflag & CREAD) ) {
1519 *****************************************************/
1520 if (!tp || !ts) {
Jiri Slabyb4173f42008-04-30 00:53:40 -07001521 MoxaPortFlushData(ch, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 return;
1523 }
Alan Cox33f0f882006-01-09 20:54:13 -08001524 spin_lock_irqsave(&moxa_lock, flags);
Jiri Slabyb4173f42008-04-30 00:53:40 -07001525 MoxaPortReadData(ch, tp);
Alan Cox33f0f882006-01-09 20:54:13 -08001526 spin_unlock_irqrestore(&moxa_lock, flags);
1527 tty_schedule_flip(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528}
1529
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530/*
1531 * Query
1532 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533
1534struct mon_str {
1535 int tick;
1536 int rxcnt[MAX_PORTS];
1537 int txcnt[MAX_PORTS];
1538};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539
1540#define DCD_changed 0x01
1541#define DCD_oldstate 0x80
1542
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543static int moxaLowWaterChk;
Jiri Slaby9dff89c2007-02-10 01:45:30 -08001544static struct mon_str moxaLog;
Jiri Slaby9fa372a2007-02-10 01:45:26 -08001545static int moxaFuncTout = HZ / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547static void moxafunc(void __iomem *, int, ushort);
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001548static void moxa_wait_finish(void __iomem *);
1549static void moxa_low_water_check(void __iomem *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
1551/*****************************************************************************
1552 * Driver level functions: *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 * 2. MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port); *
1554 * 3. MoxaDriverPoll(void); *
1555 *****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556#define MOXA 0x400
1557#define MOXA_GET_IQUEUE (MOXA + 1) /* get input buffered count */
1558#define MOXA_GET_OQUEUE (MOXA + 2) /* get output buffered count */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559#define MOXA_GETDATACOUNT (MOXA + 23)
1560#define MOXA_GET_IOQUEUE (MOXA + 27)
1561#define MOXA_FLUSH_QUEUE (MOXA + 28)
1562#define MOXA_GET_CONF (MOXA + 35) /* configuration */
1563#define MOXA_GET_MAJOR (MOXA + 63)
1564#define MOXA_GET_CUMAJOR (MOXA + 64)
1565#define MOXA_GETMSTATUS (MOXA + 65)
1566
Jiri Slabyb4173f42008-04-30 00:53:40 -07001567static void MoxaPortFlushData(struct moxa_port *port, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568{
1569 void __iomem *ofsAddr;
1570 if ((mode < 0) || (mode > 2))
1571 return;
Jiri Slabyb4173f42008-04-30 00:53:40 -07001572 ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 moxafunc(ofsAddr, FC_FlushQueue, mode);
1574 if (mode != 1) {
Jiri Slabyb4173f42008-04-30 00:53:40 -07001575 port->lowChkFlag = 0;
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001576 moxa_low_water_check(ofsAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 }
1578}
1579
Jiri Slabyb4173f42008-04-30 00:53:40 -07001580static int MoxaDriverIoctl(struct tty_struct *tty, unsigned int cmd,
1581 unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582{
Jiri Slabyb4173f42008-04-30 00:53:40 -07001583 struct moxa_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 int i;
1585 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 void __user *argp = (void __user *)arg;
1587
Jiri Slabyb4173f42008-04-30 00:53:40 -07001588 if (tty->index == MAX_PORTS) {
Jiri Slaby03718232008-04-30 00:53:39 -07001589 if ((cmd != MOXA_GET_CONF) && (cmd != MOXA_GETDATACOUNT) &&
1590 (cmd != MOXA_GET_IOQUEUE) && (cmd != MOXA_GET_MAJOR) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 (cmd != MOXA_GET_CUMAJOR) && (cmd != MOXA_GETMSTATUS))
1592 return (-EINVAL);
1593 }
1594 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 case MOXA_GETDATACOUNT:
1596 moxaLog.tick = jiffies;
Jiri Slaby9dff89c2007-02-10 01:45:30 -08001597 if(copy_to_user(argp, &moxaLog, sizeof(struct mon_str)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 return -EFAULT;
1599 return (0);
1600 case MOXA_FLUSH_QUEUE:
1601 MoxaPortFlushData(port, arg);
1602 return (0);
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001603 case MOXA_GET_IOQUEUE: {
1604 struct moxaq_str __user *argm = argp;
Jiri Slaby181d6f42007-02-10 01:45:34 -08001605 struct moxaq_str tmp;
Jiri Slaby810ab092008-04-30 00:53:41 -07001606 struct moxa_port *p;
1607 unsigned int j;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001608
Jiri Slaby810ab092008-04-30 00:53:41 -07001609 for (i = 0; i < MAX_BOARDS; i++) {
1610 p = moxa_boards[i].ports;
1611 for (j = 0; j < MAX_PORTS_PER_BOARD; j++, p++, argm++) {
1612 memset(&tmp, 0, sizeof(tmp));
1613 if (moxa_boards[i].ready) {
1614 tmp.inq = MoxaPortRxQueue(p);
1615 tmp.outq = MoxaPortTxQueue(p);
1616 }
1617 if (copy_to_user(argm, &tmp, sizeof(tmp)))
1618 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 }
1620 }
Jiri Slaby810ab092008-04-30 00:53:41 -07001621 return 0;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001622 } case MOXA_GET_OQUEUE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 i = MoxaPortTxQueue(port);
1624 return put_user(i, (unsigned long __user *)argp);
1625 case MOXA_GET_IQUEUE:
1626 i = MoxaPortRxQueue(port);
1627 return put_user(i, (unsigned long __user *)argp);
1628 case MOXA_GET_MAJOR:
1629 if(copy_to_user(argp, &ttymajor, sizeof(int)))
1630 return -EFAULT;
1631 return 0;
1632 case MOXA_GET_CUMAJOR:
1633 i = 0;
1634 if(copy_to_user(argp, &i, sizeof(int)))
1635 return -EFAULT;
1636 return 0;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001637 case MOXA_GETMSTATUS: {
1638 struct mxser_mstatus __user *argm = argp;
Jiri Slaby181d6f42007-02-10 01:45:34 -08001639 struct mxser_mstatus tmp;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001640 struct moxa_port *p;
Jiri Slaby810ab092008-04-30 00:53:41 -07001641 unsigned int j;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001642
Jiri Slaby810ab092008-04-30 00:53:41 -07001643 for (i = 0; i < MAX_BOARDS; i++) {
1644 p = moxa_boards[i].ports;
1645 for (j = 0; j < MAX_PORTS_PER_BOARD; j++, p++, argm++) {
1646 memset(&tmp, 0, sizeof(tmp));
1647 if (!moxa_boards[i].ready)
1648 goto copy;
1649
Jiri Slabyb4173f42008-04-30 00:53:40 -07001650 status = MoxaPortLineStatus(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 if (status & 1)
Jiri Slaby181d6f42007-02-10 01:45:34 -08001652 tmp.cts = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 if (status & 2)
Jiri Slaby181d6f42007-02-10 01:45:34 -08001654 tmp.dsr = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 if (status & 4)
Jiri Slaby181d6f42007-02-10 01:45:34 -08001656 tmp.dcd = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
Jiri Slaby810ab092008-04-30 00:53:41 -07001658 if (!p->tty || !p->tty->termios)
1659 tmp.cflag = p->cflag;
1660 else
1661 tmp.cflag = p->tty->termios->c_cflag;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001662copy:
Jiri Slaby810ab092008-04-30 00:53:41 -07001663 if (copy_to_user(argm, &tmp, sizeof(tmp)))
1664 return -EFAULT;
1665 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 return 0;
Jiri Slaby03718232008-04-30 00:53:39 -07001668 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 }
1670
Jiri Slaby03718232008-04-30 00:53:39 -07001671 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672}
1673
1674int MoxaDriverPoll(void)
1675{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001676 struct moxa_board_conf *brd;
Jiri Slaby810ab092008-04-30 00:53:41 -07001677 struct moxa_port *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 register ushort temp;
1679 register int card;
1680 void __iomem *ofsAddr;
1681 void __iomem *ip;
Jiri Slaby810ab092008-04-30 00:53:41 -07001682 int port, ports;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 for (card = 0; card < MAX_BOARDS; card++) {
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001685 brd = &moxa_boards[card];
Jiri Slaby810ab092008-04-30 00:53:41 -07001686 if (brd->ready == 0)
Dirk Eibach01cfaf02006-08-27 01:23:36 -07001687 continue;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001688 if ((ports = brd->numPorts) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 continue;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001690 if (readb(brd->intPend) == 0xff) {
1691 ip = brd->intTable + readb(brd->intNdx);
Jiri Slaby810ab092008-04-30 00:53:41 -07001692 p = brd->ports;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 ports <<= 1;
1694 for (port = 0; port < ports; port += 2, p++) {
Jiri Slaby810ab092008-04-30 00:53:41 -07001695 temp = readw(ip + port);
1696 if (temp == 0)
1697 continue;
1698
1699 writew(0, ip + port);
1700 ofsAddr = p->tableAddr;
1701 if (temp & IntrTx)
1702 writew(readw(ofsAddr + HostStat) &
1703 ~WakeupTx, ofsAddr + HostStat);
1704 if (temp & IntrBreak)
1705 p->breakCnt++;
1706
1707 if (temp & IntrLine) {
1708 if (readb(ofsAddr + FlagStat) & DCD_state) {
1709 if ((p->DCDState & DCD_oldstate) == 0)
1710 p->DCDState = (DCD_oldstate |
1711 DCD_changed);
1712 } else {
1713 if (p->DCDState & DCD_oldstate)
1714 p->DCDState = DCD_changed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 }
1716 }
1717 }
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001718 writeb(0, brd->intPend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 }
1720 if (moxaLowWaterChk) {
Jiri Slaby810ab092008-04-30 00:53:41 -07001721 p = brd->ports;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 for (port = 0; port < ports; port++, p++) {
Jiri Slaby810ab092008-04-30 00:53:41 -07001723 if (p->lowChkFlag) {
1724 p->lowChkFlag = 0;
1725 ofsAddr = p->tableAddr;
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001726 moxa_low_water_check(ofsAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 }
1728 }
1729 }
1730 }
1731 moxaLowWaterChk = 0;
1732 return (0);
1733}
1734
1735/*****************************************************************************
1736 * Card level function: *
1737 * 1. MoxaPortsOfCard(int cardno); *
1738 *****************************************************************************/
1739int MoxaPortsOfCard(int cardno)
1740{
1741
1742 if (moxa_boards[cardno].boardType == 0)
1743 return (0);
1744 return (moxa_boards[cardno].numPorts);
1745}
1746
1747/*****************************************************************************
1748 * Port level functions: *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 * 2. MoxaPortEnable(int port); *
1750 * 3. MoxaPortDisable(int port); *
1751 * 4. MoxaPortGetMaxBaud(int port); *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 * 6. MoxaPortSetBaud(int port, long baud); *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 * 8. MoxaPortSetTermio(int port, unsigned char *termio); *
1754 * 9. MoxaPortGetLineOut(int port, int *dtrState, int *rtsState); *
1755 * 10. MoxaPortLineCtrl(int port, int dtrState, int rtsState); *
1756 * 11. MoxaPortFlowCtrl(int port, int rts, int cts, int rx, int tx,int xany); *
1757 * 12. MoxaPortLineStatus(int port); *
1758 * 13. MoxaPortDCDChange(int port); *
1759 * 14. MoxaPortDCDON(int port); *
1760 * 15. MoxaPortFlushData(int port, int mode); *
1761 * 16. MoxaPortWriteData(int port, unsigned char * buffer, int length); *
Alan Cox33f0f882006-01-09 20:54:13 -08001762 * 17. MoxaPortReadData(int port, struct tty_struct *tty); *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 * 20. MoxaPortTxQueue(int port); *
1764 * 21. MoxaPortTxFree(int port); *
1765 * 22. MoxaPortRxQueue(int port); *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 * 24. MoxaPortTxDisable(int port); *
1767 * 25. MoxaPortTxEnable(int port); *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 * 27. MoxaPortResetBrkCnt(int port); *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 * 30. MoxaPortSendBreak(int port, int ticks); *
1770 *****************************************************************************/
1771/*
1772 * Moxa Port Number Description:
1773 *
1774 * MOXA serial driver supports up to 4 MOXA-C218/C320 boards. And,
1775 * the port number using in MOXA driver functions will be 0 to 31 for
1776 * first MOXA board, 32 to 63 for second, 64 to 95 for third and 96
1777 * to 127 for fourth. For example, if you setup three MOXA boards,
1778 * first board is C218, second board is C320-16 and third board is
1779 * C320-32. The port number of first board (C218 - 8 ports) is from
1780 * 0 to 7. The port number of second board (C320 - 16 ports) is form
1781 * 32 to 47. The port number of third board (C320 - 32 ports) is from
1782 * 64 to 95. And those port numbers form 8 to 31, 48 to 63 and 96 to
1783 * 127 will be invalid.
1784 *
1785 *
1786 * Moxa Functions Description:
1787 *
1788 * Function 1: Driver initialization routine, this routine must be
1789 * called when initialized driver.
1790 * Syntax:
1791 * void MoxaDriverInit();
1792 *
1793 *
1794 * Function 2: Moxa driver private IOCTL command processing.
1795 * Syntax:
1796 * int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port);
1797 *
1798 * unsigned int cmd : IOCTL command
1799 * unsigned long arg : IOCTL argument
1800 * int port : port number (0 - 127)
1801 *
1802 * return: 0 (OK)
1803 * -EINVAL
1804 * -ENOIOCTLCMD
1805 *
1806 *
1807 * Function 3: Moxa driver polling process routine.
1808 * Syntax:
1809 * int MoxaDriverPoll(void);
1810 *
1811 * return: 0 ; polling O.K.
1812 * -1 : no any Moxa card.
1813 *
1814 *
1815 * Function 4: Get the ports of this card.
1816 * Syntax:
1817 * int MoxaPortsOfCard(int cardno);
1818 *
1819 * int cardno : card number (0 - 3)
1820 *
1821 * return: 0 : this card is invalid
1822 * 8/16/24/32
1823 *
1824 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 * Function 6: Enable this port to start Tx/Rx data.
1826 * Syntax:
1827 * void MoxaPortEnable(int port);
1828 * int port : port number (0 - 127)
1829 *
1830 *
1831 * Function 7: Disable this port
1832 * Syntax:
1833 * void MoxaPortDisable(int port);
1834 * int port : port number (0 - 127)
1835 *
1836 *
1837 * Function 8: Get the maximun available baud rate of this port.
1838 * Syntax:
1839 * long MoxaPortGetMaxBaud(int port);
1840 * int port : port number (0 - 127)
1841 *
1842 * return: 0 : this port is invalid
1843 * 38400/57600/115200 bps
1844 *
1845 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 * Function 10: Setting baud rate of this port.
1847 * Syntax:
1848 * long MoxaPortSetBaud(int port, long baud);
1849 * int port : port number (0 - 127)
1850 * long baud : baud rate (50 - 115200)
1851 *
1852 * return: 0 : this port is invalid or baud < 50
1853 * 50 - 115200 : the real baud rate set to the port, if
1854 * the argument baud is large than maximun
1855 * available baud rate, the real setting
1856 * baud rate will be the maximun baud rate.
1857 *
1858 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 * Function 12: Configure the port.
1860 * Syntax:
Alan Cox606d0992006-12-08 02:38:45 -08001861 * int MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 * int port : port number (0 - 127)
Alan Cox606d0992006-12-08 02:38:45 -08001863 * struct ktermios * termio : termio structure pointer
Alan Coxc7bce302006-09-30 23:27:24 -07001864 * speed_t baud : baud rate
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 *
1866 * return: -1 : this port is invalid or termio == NULL
1867 * 0 : setting O.K.
1868 *
1869 *
1870 * Function 13: Get the DTR/RTS state of this port.
1871 * Syntax:
1872 * int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState);
1873 * int port : port number (0 - 127)
1874 * int * dtrState : pointer to INT to receive the current DTR
1875 * state. (if NULL, this function will not
1876 * write to this address)
1877 * int * rtsState : pointer to INT to receive the current RTS
1878 * state. (if NULL, this function will not
1879 * write to this address)
1880 *
1881 * return: -1 : this port is invalid
1882 * 0 : O.K.
1883 *
1884 *
1885 * Function 14: Setting the DTR/RTS output state of this port.
1886 * Syntax:
1887 * void MoxaPortLineCtrl(int port, int dtrState, int rtsState);
1888 * int port : port number (0 - 127)
1889 * int dtrState : DTR output state (0: off, 1: on)
1890 * int rtsState : RTS output state (0: off, 1: on)
1891 *
1892 *
1893 * Function 15: Setting the flow control of this port.
1894 * Syntax:
1895 * void MoxaPortFlowCtrl(int port, int rtsFlow, int ctsFlow, int rxFlow,
1896 * int txFlow,int xany);
1897 * int port : port number (0 - 127)
1898 * int rtsFlow : H/W RTS flow control (0: no, 1: yes)
1899 * int ctsFlow : H/W CTS flow control (0: no, 1: yes)
1900 * int rxFlow : S/W Rx XON/XOFF flow control (0: no, 1: yes)
1901 * int txFlow : S/W Tx XON/XOFF flow control (0: no, 1: yes)
1902 * int xany : S/W XANY flow control (0: no, 1: yes)
1903 *
1904 *
1905 * Function 16: Get ths line status of this port
1906 * Syntax:
1907 * int MoxaPortLineStatus(int port);
1908 * int port : port number (0 - 127)
1909 *
1910 * return: Bit 0 - CTS state (0: off, 1: on)
1911 * Bit 1 - DSR state (0: off, 1: on)
1912 * Bit 2 - DCD state (0: off, 1: on)
1913 *
1914 *
1915 * Function 17: Check the DCD state has changed since the last read
1916 * of this function.
1917 * Syntax:
1918 * int MoxaPortDCDChange(int port);
1919 * int port : port number (0 - 127)
1920 *
1921 * return: 0 : no changed
1922 * 1 : DCD has changed
1923 *
1924 *
1925 * Function 18: Check ths current DCD state is ON or not.
1926 * Syntax:
1927 * int MoxaPortDCDON(int port);
1928 * int port : port number (0 - 127)
1929 *
1930 * return: 0 : DCD off
1931 * 1 : DCD on
1932 *
1933 *
1934 * Function 19: Flush the Rx/Tx buffer data of this port.
1935 * Syntax:
1936 * void MoxaPortFlushData(int port, int mode);
1937 * int port : port number (0 - 127)
1938 * int mode
1939 * 0 : flush the Rx buffer
1940 * 1 : flush the Tx buffer
1941 * 2 : flush the Rx and Tx buffer
1942 *
1943 *
1944 * Function 20: Write data.
1945 * Syntax:
1946 * int MoxaPortWriteData(int port, unsigned char * buffer, int length);
1947 * int port : port number (0 - 127)
1948 * unsigned char * buffer : pointer to write data buffer.
1949 * int length : write data length
1950 *
1951 * return: 0 - length : real write data length
1952 *
1953 *
1954 * Function 21: Read data.
1955 * Syntax:
Alan Cox33f0f882006-01-09 20:54:13 -08001956 * int MoxaPortReadData(int port, struct tty_struct *tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 * int port : port number (0 - 127)
Alan Cox33f0f882006-01-09 20:54:13 -08001958 * struct tty_struct *tty : tty for data
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 *
1960 * return: 0 - length : real read data length
1961 *
1962 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 * Function 24: Get the Tx buffer current queued data bytes
1964 * Syntax:
1965 * int MoxaPortTxQueue(int port);
1966 * int port : port number (0 - 127)
1967 *
1968 * return: .. : Tx buffer current queued data bytes
1969 *
1970 *
1971 * Function 25: Get the Tx buffer current free space
1972 * Syntax:
1973 * int MoxaPortTxFree(int port);
1974 * int port : port number (0 - 127)
1975 *
1976 * return: .. : Tx buffer current free space
1977 *
1978 *
1979 * Function 26: Get the Rx buffer current queued data bytes
1980 * Syntax:
1981 * int MoxaPortRxQueue(int port);
1982 * int port : port number (0 - 127)
1983 *
1984 * return: .. : Rx buffer current queued data bytes
1985 *
1986 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 * Function 28: Disable port data transmission.
1988 * Syntax:
1989 * void MoxaPortTxDisable(int port);
1990 * int port : port number (0 - 127)
1991 *
1992 *
1993 * Function 29: Enable port data transmission.
1994 * Syntax:
1995 * void MoxaPortTxEnable(int port);
1996 * int port : port number (0 - 127)
1997 *
1998 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 * Function 31: Get the received BREAK signal count and reset it.
2000 * Syntax:
2001 * int MoxaPortResetBrkCnt(int port);
2002 * int port : port number (0 - 127)
2003 *
2004 * return: 0 - .. : BREAK signal count
2005 *
2006 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 * Function 34: Send out a BREAK signal.
2008 * Syntax:
2009 * void MoxaPortSendBreak(int port, int ms100);
2010 * int port : port number (0 - 127)
2011 * int ms100 : break signal time interval.
2012 * unit: 100 mini-second. if ms100 == 0, it will
2013 * send out a about 250 ms BREAK signal.
2014 *
2015 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016
Jiri Slabyb4173f42008-04-30 00:53:40 -07002017static void MoxaPortEnable(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018{
2019 void __iomem *ofsAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 short lowwater = 512;
2021
Jiri Slabyb4173f42008-04-30 00:53:40 -07002022 ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 writew(lowwater, ofsAddr + Low_water);
Jiri Slabyb4173f42008-04-30 00:53:40 -07002024 port->breakCnt = 0;
2025 if (port->board->boardType == MOXA_BOARD_C320_ISA ||
2026 port->board->boardType == MOXA_BOARD_C320_PCI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 moxafunc(ofsAddr, FC_SetBreakIrq, 0);
2028 } else {
2029 writew(readw(ofsAddr + HostStat) | WakeupBreak, ofsAddr + HostStat);
2030 }
2031
2032 moxafunc(ofsAddr, FC_SetLineIrq, Magic_code);
2033 moxafunc(ofsAddr, FC_FlushQueue, 2);
2034
2035 moxafunc(ofsAddr, FC_EnableCH, Magic_code);
2036 MoxaPortLineStatus(port);
2037}
2038
Jiri Slabyb4173f42008-04-30 00:53:40 -07002039static void MoxaPortDisable(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002041 void __iomem *ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042
2043 moxafunc(ofsAddr, FC_SetFlowCtl, 0); /* disable flow control */
2044 moxafunc(ofsAddr, FC_ClrLineIrq, Magic_code);
2045 writew(0, ofsAddr + HostStat);
2046 moxafunc(ofsAddr, FC_DisableCH, Magic_code);
2047}
2048
Jiri Slabyb4173f42008-04-30 00:53:40 -07002049static long MoxaPortGetMaxBaud(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002051 if (port->board->boardType == MOXA_BOARD_C320_ISA ||
2052 port->board->boardType == MOXA_BOARD_C320_PCI)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 return (460800L);
2054 else
2055 return (921600L);
2056}
2057
2058
Jiri Slabyb4173f42008-04-30 00:53:40 -07002059static long MoxaPortSetBaud(struct moxa_port *port, long baud)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060{
2061 void __iomem *ofsAddr;
2062 long max, clock;
2063 unsigned int val;
2064
2065 if ((baud < 50L) || ((max = MoxaPortGetMaxBaud(port)) == 0))
2066 return (0);
Jiri Slabyb4173f42008-04-30 00:53:40 -07002067 ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 if (baud > max)
2069 baud = max;
2070 if (max == 38400L)
2071 clock = 614400L; /* for 9.8304 Mhz : max. 38400 bps */
2072 else if (max == 57600L)
2073 clock = 691200L; /* for 11.0592 Mhz : max. 57600 bps */
2074 else
2075 clock = 921600L; /* for 14.7456 Mhz : max. 115200 bps */
2076 val = clock / baud;
2077 moxafunc(ofsAddr, FC_SetBaud, val);
2078 baud = clock / val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 return (baud);
2080}
2081
Jiri Slabyb4173f42008-04-30 00:53:40 -07002082static int MoxaPortSetTermio(struct moxa_port *port, struct ktermios *termio,
2083 speed_t baud)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084{
2085 void __iomem *ofsAddr;
2086 tcflag_t cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 tcflag_t mode = 0;
2088
Jiri Slabyb4173f42008-04-30 00:53:40 -07002089 ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 cflag = termio->c_cflag; /* termio->c_cflag */
2091
2092 mode = termio->c_cflag & CSIZE;
2093 if (mode == CS5)
2094 mode = MX_CS5;
2095 else if (mode == CS6)
2096 mode = MX_CS6;
2097 else if (mode == CS7)
2098 mode = MX_CS7;
2099 else if (mode == CS8)
2100 mode = MX_CS8;
2101
2102 if (termio->c_cflag & CSTOPB) {
2103 if (mode == MX_CS5)
2104 mode |= MX_STOP15;
2105 else
2106 mode |= MX_STOP2;
2107 } else
2108 mode |= MX_STOP1;
2109
2110 if (termio->c_cflag & PARENB) {
2111 if (termio->c_cflag & PARODD)
2112 mode |= MX_PARODD;
2113 else
2114 mode |= MX_PAREVEN;
2115 } else
2116 mode |= MX_PARNONE;
2117
2118 moxafunc(ofsAddr, FC_SetDataMode, (ushort) mode);
2119
Jiri Slabyb4173f42008-04-30 00:53:40 -07002120 if (port->board->boardType == MOXA_BOARD_C320_ISA ||
2121 port->board->boardType == MOXA_BOARD_C320_PCI) {
Alan Coxc7bce302006-09-30 23:27:24 -07002122 if (baud >= 921600L)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 return (-1);
2124 }
Alan Coxdb1acaa2008-02-08 04:18:43 -08002125 baud = MoxaPortSetBaud(port, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126
2127 if (termio->c_iflag & (IXON | IXOFF | IXANY)) {
2128 writeb(termio->c_cc[VSTART], ofsAddr + FuncArg);
2129 writeb(termio->c_cc[VSTOP], ofsAddr + FuncArg1);
2130 writeb(FC_SetXonXoff, ofsAddr + FuncCode);
Jiri Slaby6f56b6582007-10-18 03:06:24 -07002131 moxa_wait_finish(ofsAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132
2133 }
Alan Coxdb1acaa2008-02-08 04:18:43 -08002134 return (baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135}
2136
Jiri Slabyb4173f42008-04-30 00:53:40 -07002137static int MoxaPortGetLineOut(struct moxa_port *port, int *dtrState,
2138 int *rtsState)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139{
2140
Jiri Slabyb4173f42008-04-30 00:53:40 -07002141 if (dtrState)
2142 *dtrState = !!(port->lineCtrl & DTR_ON);
2143 if (rtsState)
2144 *rtsState = !!(port->lineCtrl & RTS_ON);
2145
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 return (0);
2147}
2148
Jiri Slabyb4173f42008-04-30 00:53:40 -07002149static void MoxaPortLineCtrl(struct moxa_port *port, int dtr, int rts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002151 int mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 if (dtr)
2154 mode |= DTR_ON;
2155 if (rts)
2156 mode |= RTS_ON;
Jiri Slabyb4173f42008-04-30 00:53:40 -07002157 port->lineCtrl = mode;
2158 moxafunc(port->tableAddr, FC_LineControl, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159}
2160
Jiri Slabyb4173f42008-04-30 00:53:40 -07002161static void MoxaPortFlowCtrl(struct moxa_port *port, int rts, int cts,
2162 int txflow, int rxflow, int txany)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002164 int mode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 if (rts)
2167 mode |= RTS_FlowCtl;
2168 if (cts)
2169 mode |= CTS_FlowCtl;
2170 if (txflow)
2171 mode |= Tx_FlowCtl;
2172 if (rxflow)
2173 mode |= Rx_FlowCtl;
2174 if (txany)
2175 mode |= IXM_IXANY;
Jiri Slabyb4173f42008-04-30 00:53:40 -07002176 moxafunc(port->tableAddr, FC_SetFlowCtl, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177}
2178
Jiri Slabyb4173f42008-04-30 00:53:40 -07002179static int MoxaPortLineStatus(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180{
2181 void __iomem *ofsAddr;
2182 int val;
2183
Jiri Slabyb4173f42008-04-30 00:53:40 -07002184 ofsAddr = port->tableAddr;
2185 if (port->board->boardType == MOXA_BOARD_C320_ISA ||
2186 port->board->boardType == MOXA_BOARD_C320_PCI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 moxafunc(ofsAddr, FC_LineStatus, 0);
2188 val = readw(ofsAddr + FuncArg);
2189 } else {
2190 val = readw(ofsAddr + FlagStat) >> 4;
2191 }
2192 val &= 0x0B;
2193 if (val & 8) {
2194 val |= 4;
Jiri Slabyb4173f42008-04-30 00:53:40 -07002195 if ((port->DCDState & DCD_oldstate) == 0)
2196 port->DCDState = (DCD_oldstate | DCD_changed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 } else {
Jiri Slabyb4173f42008-04-30 00:53:40 -07002198 if (port->DCDState & DCD_oldstate)
2199 port->DCDState = DCD_changed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 }
2201 val &= 7;
2202 return (val);
2203}
2204
Jiri Slabyb4173f42008-04-30 00:53:40 -07002205static int MoxaPortDCDChange(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206{
2207 int n;
2208
Jiri Slabyb4173f42008-04-30 00:53:40 -07002209 n = port->DCDState;
2210 port->DCDState &= ~DCD_changed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 n &= DCD_changed;
2212 return (n);
2213}
2214
Jiri Slabyb4173f42008-04-30 00:53:40 -07002215static int MoxaPortDCDON(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216{
2217 int n;
2218
Jiri Slabyb4173f42008-04-30 00:53:40 -07002219 if (port->DCDState & DCD_oldstate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 n = 1;
2221 else
2222 n = 0;
2223 return (n);
2224}
2225
Jiri Slabyb4173f42008-04-30 00:53:40 -07002226static int MoxaPortWriteData(struct moxa_port *port, unsigned char *buffer,
2227 int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228{
2229 int c, total, i;
2230 ushort tail;
2231 int cnt;
2232 ushort head, tx_mask, spage, epage;
2233 ushort pageno, pageofs, bufhead;
2234 void __iomem *baseAddr, *ofsAddr, *ofs;
2235
Jiri Slabyb4173f42008-04-30 00:53:40 -07002236 ofsAddr = port->tableAddr;
2237 baseAddr = port->board->basemem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 tx_mask = readw(ofsAddr + TX_mask);
2239 spage = readw(ofsAddr + Page_txb);
2240 epage = readw(ofsAddr + EndPage_txb);
2241 tail = readw(ofsAddr + TXwptr);
2242 head = readw(ofsAddr + TXrptr);
2243 c = (head > tail) ? (head - tail - 1)
2244 : (head - tail + tx_mask);
2245 if (c > len)
2246 c = len;
Jiri Slabyb4173f42008-04-30 00:53:40 -07002247 moxaLog.txcnt[port->tty->index] += c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 total = c;
2249 if (spage == epage) {
2250 bufhead = readw(ofsAddr + Ofs_txb);
2251 writew(spage, baseAddr + Control_reg);
2252 while (c > 0) {
2253 if (head > tail)
2254 len = head - tail - 1;
2255 else
2256 len = tx_mask + 1 - tail;
2257 len = (c > len) ? len : c;
2258 ofs = baseAddr + DynPage_addr + bufhead + tail;
2259 for (i = 0; i < len; i++)
2260 writeb(*buffer++, ofs + i);
2261 tail = (tail + len) & tx_mask;
2262 c -= len;
2263 }
2264 writew(tail, ofsAddr + TXwptr);
2265 } else {
2266 len = c;
2267 pageno = spage + (tail >> 13);
2268 pageofs = tail & Page_mask;
2269 do {
2270 cnt = Page_size - pageofs;
2271 if (cnt > c)
2272 cnt = c;
2273 c -= cnt;
2274 writeb(pageno, baseAddr + Control_reg);
2275 ofs = baseAddr + DynPage_addr + pageofs;
2276 for (i = 0; i < cnt; i++)
2277 writeb(*buffer++, ofs + i);
2278 if (c == 0) {
2279 writew((tail + len) & tx_mask, ofsAddr + TXwptr);
2280 break;
2281 }
2282 if (++pageno == epage)
2283 pageno = spage;
2284 pageofs = 0;
2285 } while (1);
2286 }
2287 writeb(1, ofsAddr + CD180TXirq); /* start to send */
2288 return (total);
2289}
2290
Jiri Slabyb4173f42008-04-30 00:53:40 -07002291static int MoxaPortReadData(struct moxa_port *port, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292{
2293 register ushort head, pageofs;
2294 int i, count, cnt, len, total, remain;
2295 ushort tail, rx_mask, spage, epage;
2296 ushort pageno, bufhead;
2297 void __iomem *baseAddr, *ofsAddr, *ofs;
2298
Jiri Slabyb4173f42008-04-30 00:53:40 -07002299 ofsAddr = port->tableAddr;
2300 baseAddr = port->board->basemem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 head = readw(ofsAddr + RXrptr);
2302 tail = readw(ofsAddr + RXwptr);
2303 rx_mask = readw(ofsAddr + RX_mask);
2304 spage = readw(ofsAddr + Page_rxb);
2305 epage = readw(ofsAddr + EndPage_rxb);
2306 count = (tail >= head) ? (tail - head)
2307 : (tail - head + rx_mask + 1);
2308 if (count == 0)
Alan Cox33f0f882006-01-09 20:54:13 -08002309 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310
Alan Cox33f0f882006-01-09 20:54:13 -08002311 total = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 remain = count - total;
Jiri Slabyb4173f42008-04-30 00:53:40 -07002313 moxaLog.rxcnt[port->tty->index] += total;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 count = total;
2315 if (spage == epage) {
2316 bufhead = readw(ofsAddr + Ofs_rxb);
2317 writew(spage, baseAddr + Control_reg);
2318 while (count > 0) {
2319 if (tail >= head)
2320 len = tail - head;
2321 else
2322 len = rx_mask + 1 - head;
2323 len = (count > len) ? len : count;
2324 ofs = baseAddr + DynPage_addr + bufhead + head;
2325 for (i = 0; i < len; i++)
Alan Cox33f0f882006-01-09 20:54:13 -08002326 tty_insert_flip_char(tty, readb(ofs + i), TTY_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 head = (head + len) & rx_mask;
2328 count -= len;
2329 }
2330 writew(head, ofsAddr + RXrptr);
2331 } else {
2332 len = count;
2333 pageno = spage + (head >> 13);
2334 pageofs = head & Page_mask;
2335 do {
2336 cnt = Page_size - pageofs;
2337 if (cnt > count)
2338 cnt = count;
2339 count -= cnt;
2340 writew(pageno, baseAddr + Control_reg);
2341 ofs = baseAddr + DynPage_addr + pageofs;
2342 for (i = 0; i < cnt; i++)
Alan Cox33f0f882006-01-09 20:54:13 -08002343 tty_insert_flip_char(tty, readb(ofs + i), TTY_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 if (count == 0) {
2345 writew((head + len) & rx_mask, ofsAddr + RXrptr);
2346 break;
2347 }
2348 if (++pageno == epage)
2349 pageno = spage;
2350 pageofs = 0;
2351 } while (1);
2352 }
2353 if ((readb(ofsAddr + FlagStat) & Xoff_state) && (remain < LowWater)) {
2354 moxaLowWaterChk = 1;
Jiri Slabyb4173f42008-04-30 00:53:40 -07002355 port->lowChkFlag = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 }
2357 return (total);
2358}
2359
2360
Jiri Slabyb4173f42008-04-30 00:53:40 -07002361static int MoxaPortTxQueue(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002363 void __iomem *ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 ushort rptr, wptr, mask;
2365 int len;
2366
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 rptr = readw(ofsAddr + TXrptr);
2368 wptr = readw(ofsAddr + TXwptr);
2369 mask = readw(ofsAddr + TX_mask);
2370 len = (wptr - rptr) & mask;
2371 return (len);
2372}
2373
Jiri Slabyb4173f42008-04-30 00:53:40 -07002374static int MoxaPortTxFree(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002376 void __iomem *ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 ushort rptr, wptr, mask;
2378 int len;
2379
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 rptr = readw(ofsAddr + TXrptr);
2381 wptr = readw(ofsAddr + TXwptr);
2382 mask = readw(ofsAddr + TX_mask);
2383 len = mask - ((wptr - rptr) & mask);
2384 return (len);
2385}
2386
Jiri Slabyb4173f42008-04-30 00:53:40 -07002387static int MoxaPortRxQueue(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002389 void __iomem *ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 ushort rptr, wptr, mask;
2391 int len;
2392
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 rptr = readw(ofsAddr + RXrptr);
2394 wptr = readw(ofsAddr + RXwptr);
2395 mask = readw(ofsAddr + RX_mask);
2396 len = (wptr - rptr) & mask;
2397 return (len);
2398}
2399
2400
Jiri Slabyb4173f42008-04-30 00:53:40 -07002401static void MoxaPortTxDisable(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002403 moxafunc(port->tableAddr, FC_SetXoffState, Magic_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404}
2405
Jiri Slabyb4173f42008-04-30 00:53:40 -07002406static void MoxaPortTxEnable(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002408 moxafunc(port->tableAddr, FC_SetXonState, Magic_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409}
2410
2411
Jiri Slabyb4173f42008-04-30 00:53:40 -07002412static int MoxaPortResetBrkCnt(struct moxa_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413{
2414 ushort cnt;
Jiri Slabyb4173f42008-04-30 00:53:40 -07002415 cnt = port->breakCnt;
2416 port->breakCnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 return (cnt);
2418}
2419
2420
Jiri Slabyb4173f42008-04-30 00:53:40 -07002421static void MoxaPortSendBreak(struct moxa_port *port, int ms100)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002423 void __iomem *ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 if (ms100) {
2426 moxafunc(ofsAddr, FC_SendBreak, Magic_code);
Jiri Slaby24c032f2007-07-17 04:05:19 -07002427 msleep(ms100 * 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 } else {
2429 moxafunc(ofsAddr, FC_SendBreak, Magic_code);
Jiri Slaby24c032f2007-07-17 04:05:19 -07002430 msleep(250);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 }
2432 moxafunc(ofsAddr, FC_StopBreak, Magic_code);
2433}
2434
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002435static int moxa_get_serial_info(struct moxa_port *info,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 struct serial_struct __user *retinfo)
2437{
2438 struct serial_struct tmp;
2439
2440 memset(&tmp, 0, sizeof(tmp));
2441 tmp.type = info->type;
Jiri Slabyb4173f42008-04-30 00:53:40 -07002442 tmp.line = info->tty->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 tmp.port = 0;
2444 tmp.irq = 0;
2445 tmp.flags = info->asyncflags;
2446 tmp.baud_base = 921600;
2447 tmp.close_delay = info->close_delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 tmp.custom_divisor = 0;
2449 tmp.hub6 = 0;
2450 if(copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2451 return -EFAULT;
2452 return (0);
2453}
2454
2455
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002456static int moxa_set_serial_info(struct moxa_port *info,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 struct serial_struct __user *new_info)
2458{
2459 struct serial_struct new_serial;
2460
2461 if(copy_from_user(&new_serial, new_info, sizeof(new_serial)))
2462 return -EFAULT;
2463
2464 if ((new_serial.irq != 0) ||
2465 (new_serial.port != 0) ||
2466// (new_serial.type != info->type) ||
2467 (new_serial.custom_divisor != 0) ||
2468 (new_serial.baud_base != 921600))
2469 return (-EPERM);
2470
2471 if (!capable(CAP_SYS_ADMIN)) {
2472 if (((new_serial.flags & ~ASYNC_USR_MASK) !=
2473 (info->asyncflags & ~ASYNC_USR_MASK)))
2474 return (-EPERM);
2475 } else {
2476 info->close_delay = new_serial.close_delay * HZ / 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 }
2478
2479 new_serial.flags = (new_serial.flags & ~ASYNC_FLAGS);
2480 new_serial.flags |= (info->asyncflags & ASYNC_FLAGS);
2481
2482 if (new_serial.type == PORT_16550A) {
Jiri Slabyb4173f42008-04-30 00:53:40 -07002483 MoxaSetFifo(info, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 } else {
Jiri Slabyb4173f42008-04-30 00:53:40 -07002485 MoxaSetFifo(info, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 }
2487
2488 info->type = new_serial.type;
2489 return (0);
2490}
2491
2492
2493
2494/*****************************************************************************
2495 * Static local functions: *
2496 *****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497static void moxafunc(void __iomem *ofsAddr, int cmd, ushort arg)
2498{
2499
2500 writew(arg, ofsAddr + FuncArg);
2501 writew(cmd, ofsAddr + FuncCode);
Jiri Slaby6f56b6582007-10-18 03:06:24 -07002502 moxa_wait_finish(ofsAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503}
2504
Jiri Slaby6f56b6582007-10-18 03:06:24 -07002505static void moxa_wait_finish(void __iomem *ofsAddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506{
2507 unsigned long i, j;
2508
2509 i = jiffies;
2510 while (readw(ofsAddr + FuncCode) != 0) {
2511 j = jiffies;
2512 if ((j - i) > moxaFuncTout) {
2513 return;
2514 }
2515 }
2516}
2517
Jiri Slaby6f56b6582007-10-18 03:06:24 -07002518static void moxa_low_water_check(void __iomem *ofsAddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519{
2520 int len;
2521 ushort rptr, wptr, mask;
2522
2523 if (readb(ofsAddr + FlagStat) & Xoff_state) {
2524 rptr = readw(ofsAddr + RXrptr);
2525 wptr = readw(ofsAddr + RXwptr);
2526 mask = readw(ofsAddr + RX_mask);
2527 len = (wptr - rptr) & mask;
2528 if (len <= Low_water)
2529 moxafunc(ofsAddr, FC_SendXon, 0);
2530 }
2531}
2532
Jiri Slabyb4173f42008-04-30 00:53:40 -07002533static void MoxaSetFifo(struct moxa_port *port, int enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534{
Jiri Slabyb4173f42008-04-30 00:53:40 -07002535 void __iomem *ofsAddr = port->tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536
2537 if (!enable) {
2538 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 0);
2539 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 1);
2540 } else {
2541 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 3);
2542 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 16);
2543 }
2544}