blob: fa58fa14b4cf46e6b5223b9a785362e936368411 [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
107 int loadstat;
108
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 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 int type;
132 int port;
133 int close_delay;
134 unsigned short closing_wait;
135 int count;
136 int blocked_open;
137 long event; /* long req'd for set_bit --RR */
138 int asyncflags;
139 unsigned long statusflags;
140 struct tty_struct *tty;
141 int cflag;
142 wait_queue_head_t open_wait;
Jiri Slaby95e07912007-10-18 03:06:25 -0700143 struct completion close_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800145 struct timer_list emptyTimer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800147 char chkPort;
148 char lineCtrl;
149 void __iomem *tableAddr;
150 long curBaud;
151 char DCDState;
152 char lowChkFlag;
153
154 ushort breakCnt;
155};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157/* statusflags */
158#define TXSTOPPED 0x1
159#define LOWWAIT 0x2
160#define EMPTYWAIT 0x4
161#define THROTTLE 0x8
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163#define SERIAL_DO_RESTART
164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165#define WAKEUP_CHARS 256
166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167static int ttymajor = MOXAMAJOR;
Jiri Slaby03718232008-04-30 00:53:39 -0700168static int moxaCard;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169/* Variables for insmod */
170#ifdef MODULE
Jiri Slabyd353eca2008-04-30 00:53:37 -0700171static unsigned long baseaddr[MAX_BOARDS];
172static unsigned int type[MAX_BOARDS];
173static unsigned int numports[MAX_BOARDS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174#endif
175
176MODULE_AUTHOR("William Chen");
177MODULE_DESCRIPTION("MOXA Intellio Family Multiport Board Device Driver");
178MODULE_LICENSE("GPL");
179#ifdef MODULE
Jiri Slabyd353eca2008-04-30 00:53:37 -0700180module_param_array(type, uint, NULL, 0);
181MODULE_PARM_DESC(type, "card type: C218=2, C320=4");
182module_param_array(baseaddr, ulong, NULL, 0);
183MODULE_PARM_DESC(baseaddr, "base address");
184module_param_array(numports, uint, NULL, 0);
185MODULE_PARM_DESC(numports, "numports (ignored for C218)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186#endif
187module_param(ttymajor, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189/*
190 * static functions:
191 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192static int moxa_open(struct tty_struct *, struct file *);
193static void moxa_close(struct tty_struct *, struct file *);
194static int moxa_write(struct tty_struct *, const unsigned char *, int);
195static int moxa_write_room(struct tty_struct *);
196static void moxa_flush_buffer(struct tty_struct *);
197static int moxa_chars_in_buffer(struct tty_struct *);
198static void moxa_flush_chars(struct tty_struct *);
199static void moxa_put_char(struct tty_struct *, unsigned char);
200static int moxa_ioctl(struct tty_struct *, struct file *, unsigned int, unsigned long);
201static void moxa_throttle(struct tty_struct *);
202static void moxa_unthrottle(struct tty_struct *);
Alan Cox606d0992006-12-08 02:38:45 -0800203static void moxa_set_termios(struct tty_struct *, struct ktermios *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204static void moxa_stop(struct tty_struct *);
205static void moxa_start(struct tty_struct *);
206static void moxa_hangup(struct tty_struct *);
207static int moxa_tiocmget(struct tty_struct *tty, struct file *file);
208static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
209 unsigned int set, unsigned int clear);
210static void moxa_poll(unsigned long);
Alan Coxdb1acaa2008-02-08 04:18:43 -0800211static void moxa_set_tty_param(struct tty_struct *, struct ktermios *);
Jiri Slaby6f56b6582007-10-18 03:06:24 -0700212static int moxa_block_till_ready(struct tty_struct *, struct file *,
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800213 struct moxa_port *);
Jiri Slaby6f56b6582007-10-18 03:06:24 -0700214static void moxa_setup_empty_event(struct tty_struct *);
215static void moxa_check_xmit_empty(unsigned long);
216static void moxa_shut_down(struct moxa_port *);
217static void moxa_receive_data(struct moxa_port *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218/*
219 * moxa board interface functions:
220 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221static int MoxaDriverIoctl(unsigned int, unsigned long, int);
222static int MoxaDriverPoll(void);
223static int MoxaPortsOfCard(int);
224static int MoxaPortIsValid(int);
225static void MoxaPortEnable(int);
226static void MoxaPortDisable(int);
227static long MoxaPortGetMaxBaud(int);
228static long MoxaPortSetBaud(int, long);
Alan Cox606d0992006-12-08 02:38:45 -0800229static int MoxaPortSetTermio(int, struct ktermios *, speed_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230static int MoxaPortGetLineOut(int, int *, int *);
231static void MoxaPortLineCtrl(int, int, int);
232static void MoxaPortFlowCtrl(int, int, int, int, int, int);
233static int MoxaPortLineStatus(int);
234static int MoxaPortDCDChange(int);
235static int MoxaPortDCDON(int);
236static void MoxaPortFlushData(int, int);
237static int MoxaPortWriteData(int, unsigned char *, int);
Alan Cox33f0f882006-01-09 20:54:13 -0800238static int MoxaPortReadData(int, struct tty_struct *tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239static int MoxaPortTxQueue(int);
240static int MoxaPortRxQueue(int);
241static int MoxaPortTxFree(int);
242static void MoxaPortTxDisable(int);
243static void MoxaPortTxEnable(int);
244static int MoxaPortResetBrkCnt(int);
245static void MoxaPortSendBreak(int, int);
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800246static int moxa_get_serial_info(struct moxa_port *, struct serial_struct __user *);
247static int moxa_set_serial_info(struct moxa_port *, struct serial_struct __user *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248static void MoxaSetFifo(int port, int enable);
249
Jeff Dikeb68e31d2006-10-02 02:17:18 -0700250static const struct tty_operations moxa_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 .open = moxa_open,
252 .close = moxa_close,
253 .write = moxa_write,
254 .write_room = moxa_write_room,
255 .flush_buffer = moxa_flush_buffer,
256 .chars_in_buffer = moxa_chars_in_buffer,
257 .flush_chars = moxa_flush_chars,
258 .put_char = moxa_put_char,
259 .ioctl = moxa_ioctl,
260 .throttle = moxa_throttle,
261 .unthrottle = moxa_unthrottle,
262 .set_termios = moxa_set_termios,
263 .stop = moxa_stop,
264 .start = moxa_start,
265 .hangup = moxa_hangup,
266 .tiocmget = moxa_tiocmget,
267 .tiocmset = moxa_tiocmset,
268};
269
Jiri Slabyaa7e5222007-02-10 01:45:27 -0800270static struct tty_driver *moxaDriver;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800271static struct moxa_port moxa_ports[MAX_PORTS];
Jiri Slabyaa7e5222007-02-10 01:45:27 -0800272static DEFINE_TIMER(moxaTimer, moxa_poll, 0, 0);
Ingo Molnar34af9462006-06-27 02:53:55 -0700273static DEFINE_SPINLOCK(moxa_lock);
Alan Cox33f0f882006-01-09 20:54:13 -0800274
Jiri Slaby03718232008-04-30 00:53:39 -0700275static int moxa_check_fw_model(struct moxa_board_conf *brd, u8 model)
276{
277 switch (brd->boardType) {
278 case MOXA_BOARD_C218_ISA:
279 case MOXA_BOARD_C218_PCI:
280 if (model != 1)
281 goto err;
282 break;
283 case MOXA_BOARD_CP204J:
284 if (model != 3)
285 goto err;
286 break;
287 default:
288 if (model != 2)
289 goto err;
290 break;
291 }
292 return 0;
293err:
294 return -EINVAL;
295}
296
297static int moxa_check_fw(const void *ptr)
298{
299 const __le16 *lptr = ptr;
300
301 if (*lptr != cpu_to_le16(0x7980))
302 return -EINVAL;
303
304 return 0;
305}
306
307static int moxa_load_bios(struct moxa_board_conf *brd, const u8 *buf,
308 size_t len)
309{
310 void __iomem *baseAddr = brd->basemem;
311 u16 tmp;
312
313 writeb(HW_reset, baseAddr + Control_reg); /* reset */
314 msleep(10);
315 memset_io(baseAddr, 0, 4096);
316 memcpy_toio(baseAddr, buf, len); /* download BIOS */
317 writeb(0, baseAddr + Control_reg); /* restart */
318
319 msleep(2000);
320
321 switch (brd->boardType) {
322 case MOXA_BOARD_C218_ISA:
323 case MOXA_BOARD_C218_PCI:
324 tmp = readw(baseAddr + C218_key);
325 if (tmp != C218_KeyCode)
326 goto err;
327 break;
328 case MOXA_BOARD_CP204J:
329 tmp = readw(baseAddr + C218_key);
330 if (tmp != CP204J_KeyCode)
331 goto err;
332 break;
333 default:
334 tmp = readw(baseAddr + C320_key);
335 if (tmp != C320_KeyCode)
336 goto err;
337 tmp = readw(baseAddr + C320_status);
338 if (tmp != STS_init) {
339 printk(KERN_ERR "moxa: bios upload failed -- CPU/Basic "
340 "module not found\n");
341 return -EIO;
342 }
343 break;
344 }
345
346 return 0;
347err:
348 printk(KERN_ERR "moxa: bios upload failed -- board not found\n");
349 return -EIO;
350}
351
352static int moxa_load_320b(struct moxa_board_conf *brd, const u8 *ptr,
353 size_t len)
354{
355 void __iomem *baseAddr = brd->basemem;
356
357 if (len < 7168) {
358 printk(KERN_ERR "moxa: invalid 320 bios -- too short\n");
359 return -EINVAL;
360 }
361
362 writew(len - 7168 - 2, baseAddr + C320bapi_len);
363 writeb(1, baseAddr + Control_reg); /* Select Page 1 */
364 memcpy_toio(baseAddr + DynPage_addr, ptr, 7168);
365 writeb(2, baseAddr + Control_reg); /* Select Page 2 */
366 memcpy_toio(baseAddr + DynPage_addr, ptr + 7168, len - 7168);
367
368 return 0;
369}
370
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700371static int moxa_real_load_code(struct moxa_board_conf *brd, const void *ptr,
Jiri Slaby03718232008-04-30 00:53:39 -0700372 size_t len)
373{
374 void __iomem *baseAddr = brd->basemem;
375 const u16 *uptr = ptr;
376 size_t wlen, len2, j;
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700377 unsigned long key, loadbuf, loadlen, checksum, checksum_ok;
378 unsigned int i, retry, c320;
Jiri Slaby03718232008-04-30 00:53:39 -0700379 u16 usum, keycode;
380
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700381 c320 = brd->boardType == MOXA_BOARD_C320_PCI ||
382 brd->boardType == MOXA_BOARD_C320_ISA;
383 keycode = (brd->boardType == MOXA_BOARD_CP204J) ? CP204J_KeyCode :
384 C218_KeyCode;
385
386 switch (brd->boardType) {
387 case MOXA_BOARD_CP204J:
388 case MOXA_BOARD_C218_ISA:
389 case MOXA_BOARD_C218_PCI:
390 key = C218_key;
391 loadbuf = C218_LoadBuf;
392 loadlen = C218DLoad_len;
393 checksum = C218check_sum;
394 checksum_ok = C218chksum_ok;
395 break;
396 default:
397 key = C320_key;
398 keycode = C320_KeyCode;
399 loadbuf = C320_LoadBuf;
400 loadlen = C320DLoad_len;
401 checksum = C320check_sum;
402 checksum_ok = C320chksum_ok;
403 break;
404 }
405
Jiri Slaby03718232008-04-30 00:53:39 -0700406 usum = 0;
407 wlen = len >> 1;
408 for (i = 0; i < wlen; i++)
409 usum += le16_to_cpu(uptr[i]);
410 retry = 0;
411 do {
412 wlen = len >> 1;
413 j = 0;
414 while (wlen) {
415 len2 = (wlen > 2048) ? 2048 : wlen;
416 wlen -= len2;
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700417 memcpy_toio(baseAddr + loadbuf, ptr + j, len2 << 1);
Jiri Slaby03718232008-04-30 00:53:39 -0700418 j += len2 << 1;
419
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700420 writew(len2, baseAddr + loadlen);
421 writew(0, baseAddr + key);
Jiri Slaby03718232008-04-30 00:53:39 -0700422 for (i = 0; i < 100; i++) {
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700423 if (readw(baseAddr + key) == keycode)
Jiri Slaby03718232008-04-30 00:53:39 -0700424 break;
425 msleep(10);
426 }
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700427 if (readw(baseAddr + key) != keycode)
Jiri Slaby03718232008-04-30 00:53:39 -0700428 return -EIO;
429 }
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700430 writew(0, baseAddr + loadlen);
431 writew(usum, baseAddr + checksum);
432 writew(0, baseAddr + key);
Jiri Slaby03718232008-04-30 00:53:39 -0700433 for (i = 0; i < 100; i++) {
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700434 if (readw(baseAddr + key) == keycode)
Jiri Slaby03718232008-04-30 00:53:39 -0700435 break;
436 msleep(10);
437 }
438 retry++;
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700439 } while ((readb(baseAddr + checksum_ok) != 1) && (retry < 3));
440 if (readb(baseAddr + checksum_ok) != 1)
Jiri Slaby03718232008-04-30 00:53:39 -0700441 return -EIO;
442
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700443 writew(0, baseAddr + key);
Jiri Slaby03718232008-04-30 00:53:39 -0700444 for (i = 0; i < 600; i++) {
445 if (readw(baseAddr + Magic_no) == Magic_code)
446 break;
447 msleep(10);
448 }
449 if (readw(baseAddr + Magic_no) != Magic_code)
450 return -EIO;
451
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700452 if (c320) {
453 if (brd->busType == MOXA_BUS_TYPE_PCI) { /* ASIC board */
454 writew(0x3800, baseAddr + TMS320_PORT1);
455 writew(0x3900, baseAddr + TMS320_PORT2);
456 writew(28499, baseAddr + TMS320_CLOCK);
457 } else {
458 writew(0x3200, baseAddr + TMS320_PORT1);
459 writew(0x3400, baseAddr + TMS320_PORT2);
460 writew(19999, baseAddr + TMS320_CLOCK);
461 }
Jiri Slaby03718232008-04-30 00:53:39 -0700462 }
463 writew(1, baseAddr + Disable_IRQ);
464 writew(0, baseAddr + Magic_no);
465 for (i = 0; i < 500; i++) {
466 if (readw(baseAddr + Magic_no) == Magic_code)
467 break;
468 msleep(10);
469 }
470 if (readw(baseAddr + Magic_no) != Magic_code)
471 return -EIO;
472
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700473 if (c320) {
474 j = readw(baseAddr + Module_cnt);
475 if (j <= 0)
476 return -EIO;
477 brd->numPorts = j * 8;
478 writew(j, baseAddr + Module_no);
479 writew(0, baseAddr + Magic_no);
480 for (i = 0; i < 600; i++) {
481 if (readw(baseAddr + Magic_no) == Magic_code)
482 break;
483 msleep(10);
484 }
485 if (readw(baseAddr + Magic_no) != Magic_code)
486 return -EIO;
Jiri Slaby03718232008-04-30 00:53:39 -0700487 }
Jiri Slaby03718232008-04-30 00:53:39 -0700488 moxaCard = 1;
489 brd->intNdx = baseAddr + IRQindex;
490 brd->intPend = baseAddr + IRQpending;
491 brd->intTable = baseAddr + IRQtable;
492
493 return 0;
494}
495
496static int moxa_load_code(struct moxa_board_conf *brd, const void *ptr,
497 size_t len)
498{
499 void __iomem *ofsAddr, *baseAddr = brd->basemem;
500 struct moxa_port *port;
501 int retval, i;
502
503 if (len % 2) {
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700504 printk(KERN_ERR "moxa: bios length is not even\n");
Jiri Slaby03718232008-04-30 00:53:39 -0700505 return -EINVAL;
506 }
507
Jiri Slaby5292bcd2008-04-30 00:53:39 -0700508 retval = moxa_real_load_code(brd, ptr, len); /* may change numPorts */
509 if (retval)
510 return retval;
511
Jiri Slaby03718232008-04-30 00:53:39 -0700512 switch (brd->boardType) {
513 case MOXA_BOARD_C218_ISA:
514 case MOXA_BOARD_C218_PCI:
515 case MOXA_BOARD_CP204J:
Jiri Slaby03718232008-04-30 00:53:39 -0700516 port = brd->ports;
517 for (i = 0; i < brd->numPorts; i++, port++) {
518 port->chkPort = 1;
519 port->curBaud = 9600L;
520 port->DCDState = 0;
521 port->tableAddr = baseAddr + Extern_table +
522 Extern_size * i;
523 ofsAddr = port->tableAddr;
524 writew(C218rx_mask, ofsAddr + RX_mask);
525 writew(C218tx_mask, ofsAddr + TX_mask);
526 writew(C218rx_spage + i * C218buf_pageno, ofsAddr + Page_rxb);
527 writew(readw(ofsAddr + Page_rxb) + C218rx_pageno, ofsAddr + EndPage_rxb);
528
529 writew(C218tx_spage + i * C218buf_pageno, ofsAddr + Page_txb);
530 writew(readw(ofsAddr + Page_txb) + C218tx_pageno, ofsAddr + EndPage_txb);
531
532 }
533 break;
534 default:
Jiri Slaby03718232008-04-30 00:53:39 -0700535 port = brd->ports;
536 for (i = 0; i < brd->numPorts; i++, port++) {
537 port->chkPort = 1;
538 port->curBaud = 9600L;
539 port->DCDState = 0;
540 port->tableAddr = baseAddr + Extern_table +
541 Extern_size * i;
542 ofsAddr = port->tableAddr;
543 switch (brd->numPorts) {
544 case 8:
545 writew(C320p8rx_mask, ofsAddr + RX_mask);
546 writew(C320p8tx_mask, ofsAddr + TX_mask);
547 writew(C320p8rx_spage + i * C320p8buf_pgno, ofsAddr + Page_rxb);
548 writew(readw(ofsAddr + Page_rxb) + C320p8rx_pgno, ofsAddr + EndPage_rxb);
549 writew(C320p8tx_spage + i * C320p8buf_pgno, ofsAddr + Page_txb);
550 writew(readw(ofsAddr + Page_txb) + C320p8tx_pgno, ofsAddr + EndPage_txb);
551
552 break;
553 case 16:
554 writew(C320p16rx_mask, ofsAddr + RX_mask);
555 writew(C320p16tx_mask, ofsAddr + TX_mask);
556 writew(C320p16rx_spage + i * C320p16buf_pgno, ofsAddr + Page_rxb);
557 writew(readw(ofsAddr + Page_rxb) + C320p16rx_pgno, ofsAddr + EndPage_rxb);
558 writew(C320p16tx_spage + i * C320p16buf_pgno, ofsAddr + Page_txb);
559 writew(readw(ofsAddr + Page_txb) + C320p16tx_pgno, ofsAddr + EndPage_txb);
560 break;
561
562 case 24:
563 writew(C320p24rx_mask, ofsAddr + RX_mask);
564 writew(C320p24tx_mask, ofsAddr + TX_mask);
565 writew(C320p24rx_spage + i * C320p24buf_pgno, ofsAddr + Page_rxb);
566 writew(readw(ofsAddr + Page_rxb) + C320p24rx_pgno, ofsAddr + EndPage_rxb);
567 writew(C320p24tx_spage + i * C320p24buf_pgno, ofsAddr + Page_txb);
568 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
569 break;
570 case 32:
571 writew(C320p32rx_mask, ofsAddr + RX_mask);
572 writew(C320p32tx_mask, ofsAddr + TX_mask);
573 writew(C320p32tx_ofs, ofsAddr + Ofs_txb);
574 writew(C320p32rx_spage + i * C320p32buf_pgno, ofsAddr + Page_rxb);
575 writew(readb(ofsAddr + Page_rxb), ofsAddr + EndPage_rxb);
576 writew(C320p32tx_spage + i * C320p32buf_pgno, ofsAddr + Page_txb);
577 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
578 break;
579 }
580 }
581 break;
582 }
583 brd->loadstat = 1;
584 return 0;
585}
586
587static int moxa_load_fw(struct moxa_board_conf *brd, const struct firmware *fw)
588{
589 void *ptr = fw->data;
590 char rsn[64];
591 u16 lens[5];
592 size_t len;
593 unsigned int a, lenp, lencnt;
594 int ret = -EINVAL;
595 struct {
596 __le32 magic; /* 0x34303430 */
597 u8 reserved1[2];
598 u8 type; /* UNIX = 3 */
599 u8 model; /* C218T=1, C320T=2, CP204=3 */
600 u8 reserved2[8];
601 __le16 len[5];
602 } *hdr = ptr;
603
604 BUILD_BUG_ON(ARRAY_SIZE(hdr->len) != ARRAY_SIZE(lens));
605
606 if (fw->size < MOXA_FW_HDRLEN) {
607 strcpy(rsn, "too short (even header won't fit)");
608 goto err;
609 }
610 if (hdr->magic != cpu_to_le32(0x30343034)) {
611 sprintf(rsn, "bad magic: %.8x", le32_to_cpu(hdr->magic));
612 goto err;
613 }
614 if (hdr->type != 3) {
615 sprintf(rsn, "not for linux, type is %u", hdr->type);
616 goto err;
617 }
618 if (moxa_check_fw_model(brd, hdr->model)) {
619 sprintf(rsn, "not for this card, model is %u", hdr->model);
620 goto err;
621 }
622
623 len = MOXA_FW_HDRLEN;
624 lencnt = hdr->model == 2 ? 5 : 3;
625 for (a = 0; a < ARRAY_SIZE(lens); a++) {
626 lens[a] = le16_to_cpu(hdr->len[a]);
627 if (lens[a] && len + lens[a] <= fw->size &&
628 moxa_check_fw(&fw->data[len]))
629 printk(KERN_WARNING "moxa firmware: unexpected input "
630 "at offset %u, but going on\n", (u32)len);
631 if (!lens[a] && a < lencnt) {
632 sprintf(rsn, "too few entries in fw file");
633 goto err;
634 }
635 len += lens[a];
636 }
637
638 if (len != fw->size) {
639 sprintf(rsn, "bad length: %u (should be %u)", (u32)fw->size,
640 (u32)len);
641 goto err;
642 }
643
644 ptr += MOXA_FW_HDRLEN;
645 lenp = 0; /* bios */
646
647 strcpy(rsn, "read above");
648
649 ret = moxa_load_bios(brd, ptr, lens[lenp]);
650 if (ret)
651 goto err;
652
653 /* we skip the tty section (lens[1]), since we don't need it */
654 ptr += lens[lenp] + lens[lenp + 1];
655 lenp += 2; /* comm */
656
657 if (hdr->model == 2) {
658 ret = moxa_load_320b(brd, ptr, lens[lenp]);
659 if (ret)
660 goto err;
661 /* skip another tty */
662 ptr += lens[lenp] + lens[lenp + 1];
663 lenp += 2;
664 }
665
666 ret = moxa_load_code(brd, ptr, lens[lenp]);
667 if (ret)
668 goto err;
669
670 return 0;
671err:
672 printk(KERN_ERR "firmware failed to load, reason: %s\n", rsn);
673 return ret;
674}
675
676static int moxa_init_board(struct moxa_board_conf *brd, struct device *dev)
677{
678 const struct firmware *fw;
679 const char *file;
680 int ret;
681
682 switch (brd->boardType) {
683 case MOXA_BOARD_C218_ISA:
684 case MOXA_BOARD_C218_PCI:
685 file = "c218tunx.cod";
686 break;
687 case MOXA_BOARD_CP204J:
688 file = "cp204unx.cod";
689 break;
690 default:
691 file = "c320tunx.cod";
692 break;
693 }
694
695 ret = request_firmware(&fw, file, dev);
696 if (ret) {
697 printk(KERN_ERR "request_firmware failed\n");
698 goto end;
699 }
700
701 ret = moxa_load_fw(brd, fw);
702
703 release_firmware(fw);
704end:
705 return ret;
706}
707
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708#ifdef CONFIG_PCI
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800709static int __devinit moxa_pci_probe(struct pci_dev *pdev,
710 const struct pci_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800712 struct moxa_board_conf *board;
713 unsigned int i;
714 int board_type = ent->driver_data;
715 int retval;
716
717 retval = pci_enable_device(pdev);
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700718 if (retval) {
719 dev_err(&pdev->dev, "can't enable pci device\n");
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800720 goto err;
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700721 }
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800722
723 for (i = 0; i < MAX_BOARDS; i++)
724 if (moxa_boards[i].basemem == NULL)
725 break;
726
727 retval = -ENODEV;
728 if (i >= MAX_BOARDS) {
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700729 dev_warn(&pdev->dev, "more than %u MOXA Intellio family boards "
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800730 "found. Board is ignored.\n", MAX_BOARDS);
731 goto err;
732 }
733
734 board = &moxa_boards[i];
Jiri Slaby03718232008-04-30 00:53:39 -0700735 board->ports = &moxa_ports[i * MAX_PORTS_PER_BOARD];
Jiri Slabye46a5e32008-04-30 00:53:37 -0700736
737 retval = pci_request_region(pdev, 2, "moxa-base");
738 if (retval) {
739 dev_err(&pdev->dev, "can't request pci region 2\n");
740 goto err;
741 }
742
743 board->basemem = ioremap(pci_resource_start(pdev, 2), 0x4000);
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700744 if (board->basemem == NULL) {
745 dev_err(&pdev->dev, "can't remap io space 2\n");
Jiri Slabye46a5e32008-04-30 00:53:37 -0700746 goto err_reg;
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700747 }
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800748
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 board->boardType = board_type;
750 switch (board_type) {
751 case MOXA_BOARD_C218_ISA:
752 case MOXA_BOARD_C218_PCI:
753 board->numPorts = 8;
754 break;
755
756 case MOXA_BOARD_CP204J:
757 board->numPorts = 4;
758 break;
759 default:
760 board->numPorts = 0;
761 break;
762 }
763 board->busType = MOXA_BUS_TYPE_PCI;
Jiri Slabya784bf72007-02-10 01:45:36 -0800764
Jiri Slaby03718232008-04-30 00:53:39 -0700765 retval = moxa_init_board(board, &pdev->dev);
766 if (retval)
767 goto err_base;
768
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800769 pci_set_drvdata(pdev, board);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
771 return (0);
Jiri Slaby03718232008-04-30 00:53:39 -0700772err_base:
773 iounmap(board->basemem);
774 board->basemem = NULL;
Jiri Slabye46a5e32008-04-30 00:53:37 -0700775err_reg:
776 pci_release_region(pdev, 2);
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800777err:
778 return retval;
779}
780
781static void __devexit moxa_pci_remove(struct pci_dev *pdev)
782{
783 struct moxa_board_conf *brd = pci_get_drvdata(pdev);
784
Jiri Slabye46a5e32008-04-30 00:53:37 -0700785 iounmap(brd->basemem);
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800786 brd->basemem = NULL;
Jiri Slabye46a5e32008-04-30 00:53:37 -0700787 pci_release_region(pdev, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788}
Jiri Slabya784bf72007-02-10 01:45:36 -0800789
790static struct pci_driver moxa_pci_driver = {
791 .name = "moxa",
792 .id_table = moxa_pcibrds,
793 .probe = moxa_pci_probe,
794 .remove = __devexit_p(moxa_pci_remove)
795};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796#endif /* CONFIG_PCI */
797
798static int __init moxa_init(void)
799{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800800 struct moxa_port *ch;
Jiri Slabyd353eca2008-04-30 00:53:37 -0700801 unsigned int i, isabrds = 0;
802 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700804 printk(KERN_INFO "MOXA Intellio family driver version %s\n",
805 MOXA_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 moxaDriver = alloc_tty_driver(MAX_PORTS + 1);
807 if (!moxaDriver)
808 return -ENOMEM;
809
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 moxaDriver->owner = THIS_MODULE;
Sergey Vlasov9b4e3b12005-09-03 16:26:49 +0100811 moxaDriver->name = "ttyMX";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 moxaDriver->major = ttymajor;
813 moxaDriver->minor_start = 0;
814 moxaDriver->type = TTY_DRIVER_TYPE_SERIAL;
815 moxaDriver->subtype = SERIAL_TYPE_NORMAL;
816 moxaDriver->init_termios = tty_std_termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 moxaDriver->init_termios.c_cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
Alan Cox606d0992006-12-08 02:38:45 -0800818 moxaDriver->init_termios.c_ispeed = 9600;
819 moxaDriver->init_termios.c_ospeed = 9600;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 moxaDriver->flags = TTY_DRIVER_REAL_RAW;
821 tty_set_operations(moxaDriver, &moxa_ops);
822
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800823 for (i = 0, ch = moxa_ports; i < MAX_PORTS; i++, ch++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 ch->type = PORT_16550A;
825 ch->port = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 ch->close_delay = 5 * HZ / 10;
827 ch->closing_wait = 30 * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 ch->cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
829 init_waitqueue_head(&ch->open_wait);
Jiri Slaby95e07912007-10-18 03:06:25 -0700830 init_completion(&ch->close_wait);
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800831
Jiri Slaby6f56b6582007-10-18 03:06:24 -0700832 setup_timer(&ch->emptyTimer, moxa_check_xmit_empty,
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800833 (unsigned long)ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 }
835
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700836 pr_debug("Moxa tty devices major number = %d\n", ttymajor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
838 if (tty_register_driver(moxaDriver)) {
839 printk(KERN_ERR "Couldn't install MOXA Smartio family driver !\n");
840 put_tty_driver(moxaDriver);
841 return -1;
842 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Jiri Slabyaa7e5222007-02-10 01:45:27 -0800844 mod_timer(&moxaTimer, jiffies + HZ / 50);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Jiri Slabyd353eca2008-04-30 00:53:37 -0700846 /* Find the boards defined from module args. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847#ifdef MODULE
Jiri Slabyd353eca2008-04-30 00:53:37 -0700848 {
849 struct moxa_board_conf *brd = moxa_boards;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 for (i = 0; i < MAX_BOARDS; i++) {
Jiri Slabyd353eca2008-04-30 00:53:37 -0700851 if (!baseaddr[i])
852 break;
853 if (type[i] == MOXA_BOARD_C218_ISA ||
854 type[i] == MOXA_BOARD_C320_ISA) {
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700855 pr_debug("Moxa board %2d: %s board(baseAddr=%lx)\n",
Jiri Slabyd353eca2008-04-30 00:53:37 -0700856 isabrds + 1, moxa_brdname[type[i] - 1],
857 baseaddr[i]);
858 brd->boardType = type[i];
Jiri Slaby03718232008-04-30 00:53:39 -0700859 brd->ports = &moxa_ports[isabrds * MAX_PORTS_PER_BOARD];
Jiri Slabyd353eca2008-04-30 00:53:37 -0700860 brd->numPorts = type[i] == MOXA_BOARD_C218_ISA ? 8 :
861 numports[i];
862 brd->busType = MOXA_BUS_TYPE_ISA;
863 brd->basemem = ioremap(baseaddr[i], 0x4000);
864 if (!brd->basemem) {
865 printk(KERN_ERR "moxa: can't remap %lx\n",
866 baseaddr[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 continue;
868 }
Jiri Slaby03718232008-04-30 00:53:39 -0700869 if (moxa_init_board(brd, NULL)) {
870 iounmap(brd->basemem);
871 brd->basemem = NULL;
872 continue;
873 }
Jiri Slabyd353eca2008-04-30 00:53:37 -0700874
875 brd++;
876 isabrds++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 }
878 }
Jiri Slabyd353eca2008-04-30 00:53:37 -0700879 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880#endif
Jiri Slabya784bf72007-02-10 01:45:36 -0800881
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882#ifdef CONFIG_PCI
Jiri Slabya784bf72007-02-10 01:45:36 -0800883 retval = pci_register_driver(&moxa_pci_driver);
884 if (retval) {
885 printk(KERN_ERR "Can't register moxa pci driver!\n");
Jiri Slabyd353eca2008-04-30 00:53:37 -0700886 if (isabrds)
Jiri Slabya784bf72007-02-10 01:45:36 -0800887 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 }
889#endif
Jiri Slabya784bf72007-02-10 01:45:36 -0800890
Jiri Slabya784bf72007-02-10 01:45:36 -0800891 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892}
893
894static void __exit moxa_exit(void)
895{
896 int i;
897
Jiri Slabyc251ae02007-02-10 01:45:32 -0800898 del_timer_sync(&moxaTimer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
900 for (i = 0; i < MAX_PORTS; i++)
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800901 del_timer_sync(&moxa_ports[i].emptyTimer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
903 if (tty_unregister_driver(moxaDriver))
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700904 printk(KERN_ERR "Couldn't unregister MOXA Intellio family "
905 "serial driver\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 put_tty_driver(moxaDriver);
Jiri Slaby86fbf142006-10-21 10:24:01 -0700907
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800908#ifdef CONFIG_PCI
Jiri Slabya784bf72007-02-10 01:45:36 -0800909 pci_unregister_driver(&moxa_pci_driver);
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800910#endif
Jiri Slabya784bf72007-02-10 01:45:36 -0800911
912 for (i = 0; i < MAX_BOARDS; i++)
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800913 if (moxa_boards[i].basemem)
914 iounmap(moxa_boards[i].basemem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915}
916
917module_init(moxa_init);
918module_exit(moxa_exit);
919
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920static int moxa_open(struct tty_struct *tty, struct file *filp)
921{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800922 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 int port;
924 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
Jiri Slaby11324ed2007-02-10 01:45:31 -0800926 port = tty->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 if (port == MAX_PORTS) {
928 return (0);
929 }
930 if (!MoxaPortIsValid(port)) {
931 tty->driver_data = NULL;
932 return (-ENODEV);
933 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800935 ch = &moxa_ports[port];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 ch->count++;
937 tty->driver_data = ch;
938 ch->tty = tty;
939 if (!(ch->asyncflags & ASYNC_INITIALIZED)) {
940 ch->statusflags = 0;
Alan Coxdb1acaa2008-02-08 04:18:43 -0800941 moxa_set_tty_param(tty, tty->termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 MoxaPortLineCtrl(ch->port, 1, 1);
943 MoxaPortEnable(ch->port);
944 ch->asyncflags |= ASYNC_INITIALIZED;
945 }
Jiri Slaby6f56b6582007-10-18 03:06:24 -0700946 retval = moxa_block_till_ready(tty, filp, ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
948 moxa_unthrottle(tty);
949
950 if (ch->type == PORT_16550A) {
951 MoxaSetFifo(ch->port, 1);
952 } else {
953 MoxaSetFifo(ch->port, 0);
954 }
955
956 return (retval);
957}
958
959static void moxa_close(struct tty_struct *tty, struct file *filp)
960{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800961 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 int port;
963
Jiri Slaby11324ed2007-02-10 01:45:31 -0800964 port = tty->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 if (port == MAX_PORTS) {
966 return;
967 }
968 if (!MoxaPortIsValid(port)) {
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700969 pr_debug("Invalid portno in moxa_close\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 tty->driver_data = NULL;
971 return;
972 }
973 if (tty->driver_data == NULL) {
974 return;
975 }
976 if (tty_hung_up_p(filp)) {
977 return;
978 }
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800979 ch = (struct moxa_port *) tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
981 if ((tty->count == 1) && (ch->count != 1)) {
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700982 printk(KERN_WARNING "moxa_close: bad serial port count; "
983 "tty->count is 1, ch->count is %d\n", ch->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 ch->count = 1;
985 }
986 if (--ch->count < 0) {
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700987 printk(KERN_WARNING "moxa_close: bad serial port count, "
988 "device=%s\n", tty->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 ch->count = 0;
990 }
991 if (ch->count) {
992 return;
993 }
994 ch->asyncflags |= ASYNC_CLOSING;
995
996 ch->cflag = tty->termios->c_cflag;
997 if (ch->asyncflags & ASYNC_INITIALIZED) {
Jiri Slaby6f56b6582007-10-18 03:06:24 -0700998 moxa_setup_empty_event(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 tty_wait_until_sent(tty, 30 * HZ); /* 30 seconds timeout */
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001000 del_timer_sync(&moxa_ports[ch->port].emptyTimer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 }
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001002 moxa_shut_down(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 MoxaPortFlushData(port, 2);
1004
1005 if (tty->driver->flush_buffer)
1006 tty->driver->flush_buffer(tty);
1007 tty_ldisc_flush(tty);
1008
1009 tty->closing = 0;
1010 ch->event = 0;
1011 ch->tty = NULL;
1012 if (ch->blocked_open) {
1013 if (ch->close_delay) {
1014 msleep_interruptible(jiffies_to_msecs(ch->close_delay));
1015 }
1016 wake_up_interruptible(&ch->open_wait);
1017 }
1018 ch->asyncflags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
Jiri Slaby95e07912007-10-18 03:06:25 -07001019 complete_all(&ch->close_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020}
1021
1022static int moxa_write(struct tty_struct *tty,
1023 const unsigned char *buf, int count)
1024{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001025 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 int len, port;
1027 unsigned long flags;
1028
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001029 ch = (struct moxa_port *) tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 if (ch == NULL)
1031 return (0);
1032 port = ch->port;
Alan Cox33f0f882006-01-09 20:54:13 -08001033
1034 spin_lock_irqsave(&moxa_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 len = MoxaPortWriteData(port, (unsigned char *) buf, count);
Alan Cox33f0f882006-01-09 20:54:13 -08001036 spin_unlock_irqrestore(&moxa_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
1038 /*********************************************
1039 if ( !(ch->statusflags & LOWWAIT) &&
1040 ((len != count) || (MoxaPortTxFree(port) <= 100)) )
1041 ************************************************/
1042 ch->statusflags |= LOWWAIT;
1043 return (len);
1044}
1045
1046static int moxa_write_room(struct tty_struct *tty)
1047{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001048 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
1050 if (tty->stopped)
1051 return (0);
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001052 ch = (struct moxa_port *) tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 if (ch == NULL)
1054 return (0);
1055 return (MoxaPortTxFree(ch->port));
1056}
1057
1058static void moxa_flush_buffer(struct tty_struct *tty)
1059{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001060 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
1062 if (ch == NULL)
1063 return;
1064 MoxaPortFlushData(ch->port, 1);
1065 tty_wakeup(tty);
1066}
1067
1068static int moxa_chars_in_buffer(struct tty_struct *tty)
1069{
1070 int chars;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001071 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
1073 /*
1074 * Sigh...I have to check if driver_data is NULL here, because
1075 * if an open() fails, the TTY subsystem eventually calls
1076 * tty_wait_until_sent(), which calls the driver's chars_in_buffer()
1077 * routine. And since the open() failed, we return 0 here. TDJ
1078 */
1079 if (ch == NULL)
1080 return (0);
1081 chars = MoxaPortTxQueue(ch->port);
1082 if (chars) {
1083 /*
1084 * Make it possible to wakeup anything waiting for output
1085 * in tty_ioctl.c, etc.
1086 */
1087 if (!(ch->statusflags & EMPTYWAIT))
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001088 moxa_setup_empty_event(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 }
1090 return (chars);
1091}
1092
1093static void moxa_flush_chars(struct tty_struct *tty)
1094{
1095 /*
1096 * Don't think I need this, because this is called to empty the TX
1097 * buffer for the 16450, 16550, etc.
1098 */
1099}
1100
1101static void moxa_put_char(struct tty_struct *tty, unsigned char c)
1102{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001103 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 int port;
1105 unsigned long flags;
1106
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001107 ch = (struct moxa_port *) tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 if (ch == NULL)
1109 return;
1110 port = ch->port;
Alan Cox33f0f882006-01-09 20:54:13 -08001111 spin_lock_irqsave(&moxa_lock, flags);
Jiri Slabyf204d262007-02-10 01:45:25 -08001112 MoxaPortWriteData(port, &c, 1);
Alan Cox33f0f882006-01-09 20:54:13 -08001113 spin_unlock_irqrestore(&moxa_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 /************************************************
1115 if ( !(ch->statusflags & LOWWAIT) && (MoxaPortTxFree(port) <= 100) )
1116 *************************************************/
1117 ch->statusflags |= LOWWAIT;
1118}
1119
1120static int moxa_tiocmget(struct tty_struct *tty, struct file *file)
1121{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001122 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 int port;
1124 int flag = 0, dtr, rts;
1125
Jiri Slaby11324ed2007-02-10 01:45:31 -08001126 port = tty->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 if ((port != MAX_PORTS) && (!ch))
1128 return (-EINVAL);
1129
1130 MoxaPortGetLineOut(ch->port, &dtr, &rts);
1131 if (dtr)
1132 flag |= TIOCM_DTR;
1133 if (rts)
1134 flag |= TIOCM_RTS;
1135 dtr = MoxaPortLineStatus(ch->port);
1136 if (dtr & 1)
1137 flag |= TIOCM_CTS;
1138 if (dtr & 2)
1139 flag |= TIOCM_DSR;
1140 if (dtr & 4)
1141 flag |= TIOCM_CD;
1142 return flag;
1143}
1144
1145static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
1146 unsigned int set, unsigned int clear)
1147{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001148 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 int port;
1150 int dtr, rts;
1151
Jiri Slaby11324ed2007-02-10 01:45:31 -08001152 port = tty->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 if ((port != MAX_PORTS) && (!ch))
1154 return (-EINVAL);
1155
1156 MoxaPortGetLineOut(ch->port, &dtr, &rts);
1157 if (set & TIOCM_RTS)
1158 rts = 1;
1159 if (set & TIOCM_DTR)
1160 dtr = 1;
1161 if (clear & TIOCM_RTS)
1162 rts = 0;
1163 if (clear & TIOCM_DTR)
1164 dtr = 0;
1165 MoxaPortLineCtrl(ch->port, dtr, rts);
1166 return 0;
1167}
1168
1169static int moxa_ioctl(struct tty_struct *tty, struct file *file,
1170 unsigned int cmd, unsigned long arg)
1171{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001172 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 register int port;
1174 void __user *argp = (void __user *)arg;
1175 int retval;
1176
Jiri Slaby11324ed2007-02-10 01:45:31 -08001177 port = tty->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 if ((port != MAX_PORTS) && (!ch))
1179 return (-EINVAL);
1180
1181 switch (cmd) {
1182 case TCSBRK: /* SVID version: non-zero arg --> no break */
1183 retval = tty_check_change(tty);
1184 if (retval)
1185 return (retval);
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001186 moxa_setup_empty_event(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 tty_wait_until_sent(tty, 0);
1188 if (!arg)
1189 MoxaPortSendBreak(ch->port, 0);
1190 return (0);
1191 case TCSBRKP: /* support for POSIX tcsendbreak() */
1192 retval = tty_check_change(tty);
1193 if (retval)
1194 return (retval);
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001195 moxa_setup_empty_event(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 tty_wait_until_sent(tty, 0);
1197 MoxaPortSendBreak(ch->port, arg);
1198 return (0);
1199 case TIOCGSOFTCAR:
Jiri Slaby9e9fc312008-04-30 00:53:38 -07001200 return put_user(C_CLOCAL(tty) ? 1 : 0, (int __user *)argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 case TIOCSSOFTCAR:
Jiri Slaby9e9fc312008-04-30 00:53:38 -07001202 if (get_user(retval, (int __user *)argp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 return -EFAULT;
1204 arg = retval;
1205 tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) |
1206 (arg ? CLOCAL : 0));
1207 if (C_CLOCAL(tty))
1208 ch->asyncflags &= ~ASYNC_CHECK_CD;
1209 else
1210 ch->asyncflags |= ASYNC_CHECK_CD;
1211 return (0);
1212 case TIOCGSERIAL:
1213 return moxa_get_serial_info(ch, argp);
1214
1215 case TIOCSSERIAL:
1216 return moxa_set_serial_info(ch, argp);
1217 default:
1218 retval = MoxaDriverIoctl(cmd, arg, port);
1219 }
1220 return (retval);
1221}
1222
1223static void moxa_throttle(struct tty_struct *tty)
1224{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001225 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
1227 ch->statusflags |= THROTTLE;
1228}
1229
1230static void moxa_unthrottle(struct tty_struct *tty)
1231{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001232 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
1234 ch->statusflags &= ~THROTTLE;
1235}
1236
1237static void moxa_set_termios(struct tty_struct *tty,
Alan Cox606d0992006-12-08 02:38:45 -08001238 struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001240 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
1242 if (ch == NULL)
1243 return;
Alan Coxdb1acaa2008-02-08 04:18:43 -08001244 moxa_set_tty_param(tty, old_termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 if (!(old_termios->c_cflag & CLOCAL) &&
1246 (tty->termios->c_cflag & CLOCAL))
1247 wake_up_interruptible(&ch->open_wait);
1248}
1249
1250static void moxa_stop(struct tty_struct *tty)
1251{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001252 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
1254 if (ch == NULL)
1255 return;
1256 MoxaPortTxDisable(ch->port);
1257 ch->statusflags |= TXSTOPPED;
1258}
1259
1260
1261static void moxa_start(struct tty_struct *tty)
1262{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001263 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
1265 if (ch == NULL)
1266 return;
1267
1268 if (!(ch->statusflags & TXSTOPPED))
1269 return;
1270
1271 MoxaPortTxEnable(ch->port);
1272 ch->statusflags &= ~TXSTOPPED;
1273}
1274
1275static void moxa_hangup(struct tty_struct *tty)
1276{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001277 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
1279 moxa_flush_buffer(tty);
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001280 moxa_shut_down(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 ch->event = 0;
1282 ch->count = 0;
1283 ch->asyncflags &= ~ASYNC_NORMAL_ACTIVE;
1284 ch->tty = NULL;
1285 wake_up_interruptible(&ch->open_wait);
1286}
1287
1288static void moxa_poll(unsigned long ignored)
1289{
1290 register int card;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001291 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 struct tty_struct *tp;
1293 int i, ports;
1294
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 del_timer(&moxaTimer);
1296
1297 if (MoxaDriverPoll() < 0) {
Jiri Slabyaa7e5222007-02-10 01:45:27 -08001298 mod_timer(&moxaTimer, jiffies + HZ / 50);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 return;
1300 }
1301 for (card = 0; card < MAX_BOARDS; card++) {
1302 if ((ports = MoxaPortsOfCard(card)) <= 0)
1303 continue;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001304 ch = &moxa_ports[card * MAX_PORTS_PER_BOARD];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 for (i = 0; i < ports; i++, ch++) {
1306 if ((ch->asyncflags & ASYNC_INITIALIZED) == 0)
1307 continue;
1308 if (!(ch->statusflags & THROTTLE) &&
1309 (MoxaPortRxQueue(ch->port) > 0))
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001310 moxa_receive_data(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 if ((tp = ch->tty) == 0)
1312 continue;
1313 if (ch->statusflags & LOWWAIT) {
1314 if (MoxaPortTxQueue(ch->port) <= WAKEUP_CHARS) {
1315 if (!tp->stopped) {
1316 ch->statusflags &= ~LOWWAIT;
1317 tty_wakeup(tp);
1318 }
1319 }
1320 }
1321 if (!I_IGNBRK(tp) && (MoxaPortResetBrkCnt(ch->port) > 0)) {
1322 tty_insert_flip_char(tp, 0, TTY_BREAK);
1323 tty_schedule_flip(tp);
1324 }
1325 if (MoxaPortDCDChange(ch->port)) {
1326 if (ch->asyncflags & ASYNC_CHECK_CD) {
1327 if (MoxaPortDCDON(ch->port))
1328 wake_up_interruptible(&ch->open_wait);
1329 else {
Jiri Slabyba196df2007-02-10 01:45:28 -08001330 tty_hangup(tp);
1331 wake_up_interruptible(&ch->open_wait);
1332 ch->asyncflags &= ~ASYNC_NORMAL_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 }
1334 }
1335 }
1336 }
1337 }
1338
Jiri Slabyaa7e5222007-02-10 01:45:27 -08001339 mod_timer(&moxaTimer, jiffies + HZ / 50);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340}
1341
1342/******************************************************************************/
1343
Alan Coxdb1acaa2008-02-08 04:18:43 -08001344static void moxa_set_tty_param(struct tty_struct *tty, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345{
Alan Cox606d0992006-12-08 02:38:45 -08001346 register struct ktermios *ts;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001347 struct moxa_port *ch;
Alan Coxdb1acaa2008-02-08 04:18:43 -08001348 int rts, cts, txflow, rxflow, xany, baud;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001350 ch = (struct moxa_port *) tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 ts = tty->termios;
1352 if (ts->c_cflag & CLOCAL)
1353 ch->asyncflags &= ~ASYNC_CHECK_CD;
1354 else
1355 ch->asyncflags |= ASYNC_CHECK_CD;
1356 rts = cts = txflow = rxflow = xany = 0;
1357 if (ts->c_cflag & CRTSCTS)
1358 rts = cts = 1;
1359 if (ts->c_iflag & IXON)
1360 txflow = 1;
1361 if (ts->c_iflag & IXOFF)
1362 rxflow = 1;
1363 if (ts->c_iflag & IXANY)
1364 xany = 1;
Alan Coxdb1acaa2008-02-08 04:18:43 -08001365
1366 /* Clear the features we don't support */
1367 ts->c_cflag &= ~CMSPAR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 MoxaPortFlowCtrl(ch->port, rts, cts, txflow, rxflow, xany);
Alan Coxdb1acaa2008-02-08 04:18:43 -08001369 baud = MoxaPortSetTermio(ch->port, ts, tty_get_baud_rate(tty));
1370 if (baud == -1)
1371 baud = tty_termios_baud_rate(old_termios);
1372 /* Not put the baud rate into the termios data */
1373 tty_encode_baud_rate(tty, baud, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374}
1375
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001376static int moxa_block_till_ready(struct tty_struct *tty, struct file *filp,
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001377 struct moxa_port *ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378{
1379 DECLARE_WAITQUEUE(wait,current);
1380 unsigned long flags;
1381 int retval;
1382 int do_clocal = C_CLOCAL(tty);
1383
1384 /*
1385 * If the device is in the middle of being closed, then block
1386 * until it's done, and then try again.
1387 */
1388 if (tty_hung_up_p(filp) || (ch->asyncflags & ASYNC_CLOSING)) {
1389 if (ch->asyncflags & ASYNC_CLOSING)
Jiri Slaby95e07912007-10-18 03:06:25 -07001390 wait_for_completion_interruptible(&ch->close_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391#ifdef SERIAL_DO_RESTART
1392 if (ch->asyncflags & ASYNC_HUP_NOTIFY)
1393 return (-EAGAIN);
1394 else
1395 return (-ERESTARTSYS);
1396#else
1397 return (-EAGAIN);
1398#endif
1399 }
1400 /*
1401 * If non-blocking mode is set, then make the check up front
1402 * and then exit.
1403 */
1404 if (filp->f_flags & O_NONBLOCK) {
1405 ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
1406 return (0);
1407 }
1408 /*
1409 * Block waiting for the carrier detect and the line to become free
1410 */
1411 retval = 0;
1412 add_wait_queue(&ch->open_wait, &wait);
Jiri Slaby7aeb95d2007-10-18 03:06:24 -07001413 pr_debug("block_til_ready before block: ttys%d, count = %d\n",
1414 ch->port, ch->count);
Alan Cox33f0f882006-01-09 20:54:13 -08001415 spin_lock_irqsave(&moxa_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 if (!tty_hung_up_p(filp))
1417 ch->count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 ch->blocked_open++;
Alan Cox33f0f882006-01-09 20:54:13 -08001419 spin_unlock_irqrestore(&moxa_lock, flags);
1420
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 while (1) {
1422 set_current_state(TASK_INTERRUPTIBLE);
1423 if (tty_hung_up_p(filp) ||
1424 !(ch->asyncflags & ASYNC_INITIALIZED)) {
1425#ifdef SERIAL_DO_RESTART
1426 if (ch->asyncflags & ASYNC_HUP_NOTIFY)
1427 retval = -EAGAIN;
1428 else
1429 retval = -ERESTARTSYS;
1430#else
1431 retval = -EAGAIN;
1432#endif
1433 break;
1434 }
1435 if (!(ch->asyncflags & ASYNC_CLOSING) && (do_clocal ||
1436 MoxaPortDCDON(ch->port)))
1437 break;
1438
1439 if (signal_pending(current)) {
1440 retval = -ERESTARTSYS;
1441 break;
1442 }
1443 schedule();
1444 }
1445 set_current_state(TASK_RUNNING);
1446 remove_wait_queue(&ch->open_wait, &wait);
Alan Cox33f0f882006-01-09 20:54:13 -08001447
1448 spin_lock_irqsave(&moxa_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 if (!tty_hung_up_p(filp))
1450 ch->count++;
1451 ch->blocked_open--;
Alan Cox33f0f882006-01-09 20:54:13 -08001452 spin_unlock_irqrestore(&moxa_lock, flags);
Jiri Slaby7aeb95d2007-10-18 03:06:24 -07001453 pr_debug("block_til_ready after blocking: ttys%d, count = %d\n",
1454 ch->port, ch->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 if (retval)
1456 return (retval);
Alan Cox33f0f882006-01-09 20:54:13 -08001457 /* FIXME: review to see if we need to use set_bit on these */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
Alan Cox33f0f882006-01-09 20:54:13 -08001459 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460}
1461
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001462static void moxa_setup_empty_event(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001464 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 unsigned long flags;
1466
Alan Cox33f0f882006-01-09 20:54:13 -08001467 spin_lock_irqsave(&moxa_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 ch->statusflags |= EMPTYWAIT;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001469 mod_timer(&moxa_ports[ch->port].emptyTimer, jiffies + HZ);
Alan Cox33f0f882006-01-09 20:54:13 -08001470 spin_unlock_irqrestore(&moxa_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471}
1472
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001473static void moxa_check_xmit_empty(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001475 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001477 ch = (struct moxa_port *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 if (ch->tty && (ch->statusflags & EMPTYWAIT)) {
1479 if (MoxaPortTxQueue(ch->port) == 0) {
1480 ch->statusflags &= ~EMPTYWAIT;
1481 tty_wakeup(ch->tty);
1482 return;
1483 }
Jiri Slabyc4342202007-10-18 03:06:19 -07001484 mod_timer(&moxa_ports[ch->port].emptyTimer,
1485 round_jiffies(jiffies + HZ));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 } else
1487 ch->statusflags &= ~EMPTYWAIT;
1488}
1489
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001490static void moxa_shut_down(struct moxa_port *ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491{
1492 struct tty_struct *tp;
1493
1494 if (!(ch->asyncflags & ASYNC_INITIALIZED))
1495 return;
1496
1497 tp = ch->tty;
1498
1499 MoxaPortDisable(ch->port);
1500
1501 /*
1502 * If we're a modem control device and HUPCL is on, drop RTS & DTR.
1503 */
1504 if (tp->termios->c_cflag & HUPCL)
1505 MoxaPortLineCtrl(ch->port, 0, 0);
1506
1507 ch->asyncflags &= ~ASYNC_INITIALIZED;
1508}
1509
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001510static void moxa_receive_data(struct moxa_port *ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511{
1512 struct tty_struct *tp;
Alan Cox606d0992006-12-08 02:38:45 -08001513 struct ktermios *ts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 unsigned long flags;
1515
1516 ts = NULL;
1517 tp = ch->tty;
1518 if (tp)
1519 ts = tp->termios;
1520 /**************************************************
1521 if ( !tp || !ts || !(ts->c_cflag & CREAD) ) {
1522 *****************************************************/
1523 if (!tp || !ts) {
1524 MoxaPortFlushData(ch->port, 0);
1525 return;
1526 }
Alan Cox33f0f882006-01-09 20:54:13 -08001527 spin_lock_irqsave(&moxa_lock, flags);
1528 MoxaPortReadData(ch->port, tp);
1529 spin_unlock_irqrestore(&moxa_lock, flags);
1530 tty_schedule_flip(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531}
1532
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533/*
1534 * Query
1535 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536
1537struct mon_str {
1538 int tick;
1539 int rxcnt[MAX_PORTS];
1540 int txcnt[MAX_PORTS];
1541};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
1543#define DCD_changed 0x01
1544#define DCD_oldstate 0x80
1545
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546static int moxaLowWaterChk;
Jiri Slaby9dff89c2007-02-10 01:45:30 -08001547static struct mon_str moxaLog;
Jiri Slaby9fa372a2007-02-10 01:45:26 -08001548static int moxaFuncTout = HZ / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550static void moxafunc(void __iomem *, int, ushort);
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001551static void moxa_wait_finish(void __iomem *);
1552static void moxa_low_water_check(void __iomem *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
1554/*****************************************************************************
1555 * Driver level functions: *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 * 2. MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port); *
1557 * 3. MoxaDriverPoll(void); *
1558 *****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559#define MOXA 0x400
1560#define MOXA_GET_IQUEUE (MOXA + 1) /* get input buffered count */
1561#define MOXA_GET_OQUEUE (MOXA + 2) /* get output buffered count */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562#define MOXA_GETDATACOUNT (MOXA + 23)
1563#define MOXA_GET_IOQUEUE (MOXA + 27)
1564#define MOXA_FLUSH_QUEUE (MOXA + 28)
1565#define MOXA_GET_CONF (MOXA + 35) /* configuration */
1566#define MOXA_GET_MAJOR (MOXA + 63)
1567#define MOXA_GET_CUMAJOR (MOXA + 64)
1568#define MOXA_GETMSTATUS (MOXA + 65)
1569
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570void MoxaPortFlushData(int port, int mode)
1571{
1572 void __iomem *ofsAddr;
1573 if ((mode < 0) || (mode > 2))
1574 return;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001575 ofsAddr = moxa_ports[port].tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 moxafunc(ofsAddr, FC_FlushQueue, mode);
1577 if (mode != 1) {
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001578 moxa_ports[port].lowChkFlag = 0;
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001579 moxa_low_water_check(ofsAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 }
1581}
1582
1583int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port)
1584{
1585 int i;
1586 int status;
1587 int MoxaPortTxQueue(int), MoxaPortRxQueue(int);
1588 void __user *argp = (void __user *)arg;
1589
Jiri Slaby11324ed2007-02-10 01:45:31 -08001590 if (port == MAX_PORTS) {
Jiri Slaby03718232008-04-30 00:53:39 -07001591 if ((cmd != MOXA_GET_CONF) && (cmd != MOXA_GETDATACOUNT) &&
1592 (cmd != MOXA_GET_IOQUEUE) && (cmd != MOXA_GET_MAJOR) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 (cmd != MOXA_GET_CUMAJOR) && (cmd != MOXA_GETMSTATUS))
1594 return (-EINVAL);
1595 }
1596 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 case MOXA_GETDATACOUNT:
1598 moxaLog.tick = jiffies;
Jiri Slaby9dff89c2007-02-10 01:45:30 -08001599 if(copy_to_user(argp, &moxaLog, sizeof(struct mon_str)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 return -EFAULT;
1601 return (0);
1602 case MOXA_FLUSH_QUEUE:
1603 MoxaPortFlushData(port, arg);
1604 return (0);
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001605 case MOXA_GET_IOQUEUE: {
1606 struct moxaq_str __user *argm = argp;
Jiri Slaby181d6f42007-02-10 01:45:34 -08001607 struct moxaq_str tmp;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001608
1609 for (i = 0; i < MAX_PORTS; i++, argm++) {
Jiri Slaby181d6f42007-02-10 01:45:34 -08001610 memset(&tmp, 0, sizeof(tmp));
1611 if (moxa_ports[i].chkPort) {
1612 tmp.inq = MoxaPortRxQueue(i);
1613 tmp.outq = MoxaPortTxQueue(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 }
Jiri Slaby181d6f42007-02-10 01:45:34 -08001615 if (copy_to_user(argm, &tmp, sizeof(tmp)))
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001616 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 return (0);
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001619 } case MOXA_GET_OQUEUE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 i = MoxaPortTxQueue(port);
1621 return put_user(i, (unsigned long __user *)argp);
1622 case MOXA_GET_IQUEUE:
1623 i = MoxaPortRxQueue(port);
1624 return put_user(i, (unsigned long __user *)argp);
1625 case MOXA_GET_MAJOR:
1626 if(copy_to_user(argp, &ttymajor, sizeof(int)))
1627 return -EFAULT;
1628 return 0;
1629 case MOXA_GET_CUMAJOR:
1630 i = 0;
1631 if(copy_to_user(argp, &i, sizeof(int)))
1632 return -EFAULT;
1633 return 0;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001634 case MOXA_GETMSTATUS: {
1635 struct mxser_mstatus __user *argm = argp;
Jiri Slaby181d6f42007-02-10 01:45:34 -08001636 struct mxser_mstatus tmp;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001637 struct moxa_port *p;
1638
1639 for (i = 0; i < MAX_PORTS; i++, argm++) {
1640 p = &moxa_ports[i];
Jiri Slaby181d6f42007-02-10 01:45:34 -08001641 memset(&tmp, 0, sizeof(tmp));
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001642 if (!p->chkPort) {
1643 goto copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 } else {
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001645 status = MoxaPortLineStatus(p->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 if (status & 1)
Jiri Slaby181d6f42007-02-10 01:45:34 -08001647 tmp.cts = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 if (status & 2)
Jiri Slaby181d6f42007-02-10 01:45:34 -08001649 tmp.dsr = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 if (status & 4)
Jiri Slaby181d6f42007-02-10 01:45:34 -08001651 tmp.dcd = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 }
1653
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001654 if (!p->tty || !p->tty->termios)
Jiri Slaby181d6f42007-02-10 01:45:34 -08001655 tmp.cflag = p->cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 else
Jiri Slaby181d6f42007-02-10 01:45:34 -08001657 tmp.cflag = p->tty->termios->c_cflag;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001658copy:
Jiri Slaby181d6f42007-02-10 01:45:34 -08001659 if (copy_to_user(argm, &tmp, sizeof(tmp)))
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001660 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 return 0;
Jiri Slaby03718232008-04-30 00:53:39 -07001663 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 }
1665
Jiri Slaby03718232008-04-30 00:53:39 -07001666 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667}
1668
1669int MoxaDriverPoll(void)
1670{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001671 struct moxa_board_conf *brd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 register ushort temp;
1673 register int card;
1674 void __iomem *ofsAddr;
1675 void __iomem *ip;
1676 int port, p, ports;
1677
1678 if (moxaCard == 0)
1679 return (-1);
1680 for (card = 0; card < MAX_BOARDS; card++) {
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001681 brd = &moxa_boards[card];
1682 if (brd->loadstat == 0)
Dirk Eibach01cfaf02006-08-27 01:23:36 -07001683 continue;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001684 if ((ports = brd->numPorts) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 continue;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001686 if (readb(brd->intPend) == 0xff) {
1687 ip = brd->intTable + readb(brd->intNdx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 p = card * MAX_PORTS_PER_BOARD;
1689 ports <<= 1;
1690 for (port = 0; port < ports; port += 2, p++) {
1691 if ((temp = readw(ip + port)) != 0) {
1692 writew(0, ip + port);
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001693 ofsAddr = moxa_ports[p].tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 if (temp & IntrTx)
1695 writew(readw(ofsAddr + HostStat) & ~WakeupTx, ofsAddr + HostStat);
1696 if (temp & IntrBreak) {
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001697 moxa_ports[p].breakCnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 }
1699 if (temp & IntrLine) {
1700 if (readb(ofsAddr + FlagStat) & DCD_state) {
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001701 if ((moxa_ports[p].DCDState & DCD_oldstate) == 0)
1702 moxa_ports[p].DCDState = (DCD_oldstate |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 DCD_changed);
1704 } else {
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001705 if (moxa_ports[p].DCDState & DCD_oldstate)
1706 moxa_ports[p].DCDState = DCD_changed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 }
1708 }
1709 }
1710 }
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001711 writeb(0, brd->intPend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 }
1713 if (moxaLowWaterChk) {
1714 p = card * MAX_PORTS_PER_BOARD;
1715 for (port = 0; port < ports; port++, p++) {
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001716 if (moxa_ports[p].lowChkFlag) {
1717 moxa_ports[p].lowChkFlag = 0;
1718 ofsAddr = moxa_ports[p].tableAddr;
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001719 moxa_low_water_check(ofsAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 }
1721 }
1722 }
1723 }
1724 moxaLowWaterChk = 0;
1725 return (0);
1726}
1727
1728/*****************************************************************************
1729 * Card level function: *
1730 * 1. MoxaPortsOfCard(int cardno); *
1731 *****************************************************************************/
1732int MoxaPortsOfCard(int cardno)
1733{
1734
1735 if (moxa_boards[cardno].boardType == 0)
1736 return (0);
1737 return (moxa_boards[cardno].numPorts);
1738}
1739
1740/*****************************************************************************
1741 * Port level functions: *
1742 * 1. MoxaPortIsValid(int port); *
1743 * 2. MoxaPortEnable(int port); *
1744 * 3. MoxaPortDisable(int port); *
1745 * 4. MoxaPortGetMaxBaud(int port); *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 * 6. MoxaPortSetBaud(int port, long baud); *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 * 8. MoxaPortSetTermio(int port, unsigned char *termio); *
1748 * 9. MoxaPortGetLineOut(int port, int *dtrState, int *rtsState); *
1749 * 10. MoxaPortLineCtrl(int port, int dtrState, int rtsState); *
1750 * 11. MoxaPortFlowCtrl(int port, int rts, int cts, int rx, int tx,int xany); *
1751 * 12. MoxaPortLineStatus(int port); *
1752 * 13. MoxaPortDCDChange(int port); *
1753 * 14. MoxaPortDCDON(int port); *
1754 * 15. MoxaPortFlushData(int port, int mode); *
1755 * 16. MoxaPortWriteData(int port, unsigned char * buffer, int length); *
Alan Cox33f0f882006-01-09 20:54:13 -08001756 * 17. MoxaPortReadData(int port, struct tty_struct *tty); *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 * 20. MoxaPortTxQueue(int port); *
1758 * 21. MoxaPortTxFree(int port); *
1759 * 22. MoxaPortRxQueue(int port); *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 * 24. MoxaPortTxDisable(int port); *
1761 * 25. MoxaPortTxEnable(int port); *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 * 27. MoxaPortResetBrkCnt(int port); *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 * 30. MoxaPortSendBreak(int port, int ticks); *
1764 *****************************************************************************/
1765/*
1766 * Moxa Port Number Description:
1767 *
1768 * MOXA serial driver supports up to 4 MOXA-C218/C320 boards. And,
1769 * the port number using in MOXA driver functions will be 0 to 31 for
1770 * first MOXA board, 32 to 63 for second, 64 to 95 for third and 96
1771 * to 127 for fourth. For example, if you setup three MOXA boards,
1772 * first board is C218, second board is C320-16 and third board is
1773 * C320-32. The port number of first board (C218 - 8 ports) is from
1774 * 0 to 7. The port number of second board (C320 - 16 ports) is form
1775 * 32 to 47. The port number of third board (C320 - 32 ports) is from
1776 * 64 to 95. And those port numbers form 8 to 31, 48 to 63 and 96 to
1777 * 127 will be invalid.
1778 *
1779 *
1780 * Moxa Functions Description:
1781 *
1782 * Function 1: Driver initialization routine, this routine must be
1783 * called when initialized driver.
1784 * Syntax:
1785 * void MoxaDriverInit();
1786 *
1787 *
1788 * Function 2: Moxa driver private IOCTL command processing.
1789 * Syntax:
1790 * int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port);
1791 *
1792 * unsigned int cmd : IOCTL command
1793 * unsigned long arg : IOCTL argument
1794 * int port : port number (0 - 127)
1795 *
1796 * return: 0 (OK)
1797 * -EINVAL
1798 * -ENOIOCTLCMD
1799 *
1800 *
1801 * Function 3: Moxa driver polling process routine.
1802 * Syntax:
1803 * int MoxaDriverPoll(void);
1804 *
1805 * return: 0 ; polling O.K.
1806 * -1 : no any Moxa card.
1807 *
1808 *
1809 * Function 4: Get the ports of this card.
1810 * Syntax:
1811 * int MoxaPortsOfCard(int cardno);
1812 *
1813 * int cardno : card number (0 - 3)
1814 *
1815 * return: 0 : this card is invalid
1816 * 8/16/24/32
1817 *
1818 *
1819 * Function 5: Check this port is valid or invalid
1820 * Syntax:
1821 * int MoxaPortIsValid(int port);
1822 * int port : port number (0 - 127, ref port description)
1823 *
1824 * return: 0 : this port is invalid
1825 * 1 : this port is valid
1826 *
1827 *
1828 * Function 6: Enable this port to start Tx/Rx data.
1829 * Syntax:
1830 * void MoxaPortEnable(int port);
1831 * int port : port number (0 - 127)
1832 *
1833 *
1834 * Function 7: Disable this port
1835 * Syntax:
1836 * void MoxaPortDisable(int port);
1837 * int port : port number (0 - 127)
1838 *
1839 *
1840 * Function 8: Get the maximun available baud rate of this port.
1841 * Syntax:
1842 * long MoxaPortGetMaxBaud(int port);
1843 * int port : port number (0 - 127)
1844 *
1845 * return: 0 : this port is invalid
1846 * 38400/57600/115200 bps
1847 *
1848 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 * Function 10: Setting baud rate of this port.
1850 * Syntax:
1851 * long MoxaPortSetBaud(int port, long baud);
1852 * int port : port number (0 - 127)
1853 * long baud : baud rate (50 - 115200)
1854 *
1855 * return: 0 : this port is invalid or baud < 50
1856 * 50 - 115200 : the real baud rate set to the port, if
1857 * the argument baud is large than maximun
1858 * available baud rate, the real setting
1859 * baud rate will be the maximun baud rate.
1860 *
1861 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 * Function 12: Configure the port.
1863 * Syntax:
Alan Cox606d0992006-12-08 02:38:45 -08001864 * int MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 * int port : port number (0 - 127)
Alan Cox606d0992006-12-08 02:38:45 -08001866 * struct ktermios * termio : termio structure pointer
Alan Coxc7bce302006-09-30 23:27:24 -07001867 * speed_t baud : baud rate
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 *
1869 * return: -1 : this port is invalid or termio == NULL
1870 * 0 : setting O.K.
1871 *
1872 *
1873 * Function 13: Get the DTR/RTS state of this port.
1874 * Syntax:
1875 * int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState);
1876 * int port : port number (0 - 127)
1877 * int * dtrState : pointer to INT to receive the current DTR
1878 * state. (if NULL, this function will not
1879 * write to this address)
1880 * int * rtsState : pointer to INT to receive the current RTS
1881 * state. (if NULL, this function will not
1882 * write to this address)
1883 *
1884 * return: -1 : this port is invalid
1885 * 0 : O.K.
1886 *
1887 *
1888 * Function 14: Setting the DTR/RTS output state of this port.
1889 * Syntax:
1890 * void MoxaPortLineCtrl(int port, int dtrState, int rtsState);
1891 * int port : port number (0 - 127)
1892 * int dtrState : DTR output state (0: off, 1: on)
1893 * int rtsState : RTS output state (0: off, 1: on)
1894 *
1895 *
1896 * Function 15: Setting the flow control of this port.
1897 * Syntax:
1898 * void MoxaPortFlowCtrl(int port, int rtsFlow, int ctsFlow, int rxFlow,
1899 * int txFlow,int xany);
1900 * int port : port number (0 - 127)
1901 * int rtsFlow : H/W RTS flow control (0: no, 1: yes)
1902 * int ctsFlow : H/W CTS flow control (0: no, 1: yes)
1903 * int rxFlow : S/W Rx XON/XOFF flow control (0: no, 1: yes)
1904 * int txFlow : S/W Tx XON/XOFF flow control (0: no, 1: yes)
1905 * int xany : S/W XANY flow control (0: no, 1: yes)
1906 *
1907 *
1908 * Function 16: Get ths line status of this port
1909 * Syntax:
1910 * int MoxaPortLineStatus(int port);
1911 * int port : port number (0 - 127)
1912 *
1913 * return: Bit 0 - CTS state (0: off, 1: on)
1914 * Bit 1 - DSR state (0: off, 1: on)
1915 * Bit 2 - DCD state (0: off, 1: on)
1916 *
1917 *
1918 * Function 17: Check the DCD state has changed since the last read
1919 * of this function.
1920 * Syntax:
1921 * int MoxaPortDCDChange(int port);
1922 * int port : port number (0 - 127)
1923 *
1924 * return: 0 : no changed
1925 * 1 : DCD has changed
1926 *
1927 *
1928 * Function 18: Check ths current DCD state is ON or not.
1929 * Syntax:
1930 * int MoxaPortDCDON(int port);
1931 * int port : port number (0 - 127)
1932 *
1933 * return: 0 : DCD off
1934 * 1 : DCD on
1935 *
1936 *
1937 * Function 19: Flush the Rx/Tx buffer data of this port.
1938 * Syntax:
1939 * void MoxaPortFlushData(int port, int mode);
1940 * int port : port number (0 - 127)
1941 * int mode
1942 * 0 : flush the Rx buffer
1943 * 1 : flush the Tx buffer
1944 * 2 : flush the Rx and Tx buffer
1945 *
1946 *
1947 * Function 20: Write data.
1948 * Syntax:
1949 * int MoxaPortWriteData(int port, unsigned char * buffer, int length);
1950 * int port : port number (0 - 127)
1951 * unsigned char * buffer : pointer to write data buffer.
1952 * int length : write data length
1953 *
1954 * return: 0 - length : real write data length
1955 *
1956 *
1957 * Function 21: Read data.
1958 * Syntax:
Alan Cox33f0f882006-01-09 20:54:13 -08001959 * int MoxaPortReadData(int port, struct tty_struct *tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 * int port : port number (0 - 127)
Alan Cox33f0f882006-01-09 20:54:13 -08001961 * struct tty_struct *tty : tty for data
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 *
1963 * return: 0 - length : real read data length
1964 *
1965 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 * Function 24: Get the Tx buffer current queued data bytes
1967 * Syntax:
1968 * int MoxaPortTxQueue(int port);
1969 * int port : port number (0 - 127)
1970 *
1971 * return: .. : Tx buffer current queued data bytes
1972 *
1973 *
1974 * Function 25: Get the Tx buffer current free space
1975 * Syntax:
1976 * int MoxaPortTxFree(int port);
1977 * int port : port number (0 - 127)
1978 *
1979 * return: .. : Tx buffer current free space
1980 *
1981 *
1982 * Function 26: Get the Rx buffer current queued data bytes
1983 * Syntax:
1984 * int MoxaPortRxQueue(int port);
1985 * int port : port number (0 - 127)
1986 *
1987 * return: .. : Rx buffer current queued data bytes
1988 *
1989 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 * Function 28: Disable port data transmission.
1991 * Syntax:
1992 * void MoxaPortTxDisable(int port);
1993 * int port : port number (0 - 127)
1994 *
1995 *
1996 * Function 29: Enable port data transmission.
1997 * Syntax:
1998 * void MoxaPortTxEnable(int port);
1999 * int port : port number (0 - 127)
2000 *
2001 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 * Function 31: Get the received BREAK signal count and reset it.
2003 * Syntax:
2004 * int MoxaPortResetBrkCnt(int port);
2005 * int port : port number (0 - 127)
2006 *
2007 * return: 0 - .. : BREAK signal count
2008 *
2009 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 * Function 34: Send out a BREAK signal.
2011 * Syntax:
2012 * void MoxaPortSendBreak(int port, int ms100);
2013 * int port : port number (0 - 127)
2014 * int ms100 : break signal time interval.
2015 * unit: 100 mini-second. if ms100 == 0, it will
2016 * send out a about 250 ms BREAK signal.
2017 *
2018 */
2019int MoxaPortIsValid(int port)
2020{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 if (moxaCard == 0)
2022 return (0);
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002023 if (moxa_ports[port].chkPort == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 return (0);
2025 return (1);
2026}
2027
2028void MoxaPortEnable(int port)
2029{
2030 void __iomem *ofsAddr;
2031 int MoxaPortLineStatus(int);
2032 short lowwater = 512;
2033
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002034 ofsAddr = moxa_ports[port].tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 writew(lowwater, ofsAddr + Low_water);
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002036 moxa_ports[port].breakCnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
2038 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
2039 moxafunc(ofsAddr, FC_SetBreakIrq, 0);
2040 } else {
2041 writew(readw(ofsAddr + HostStat) | WakeupBreak, ofsAddr + HostStat);
2042 }
2043
2044 moxafunc(ofsAddr, FC_SetLineIrq, Magic_code);
2045 moxafunc(ofsAddr, FC_FlushQueue, 2);
2046
2047 moxafunc(ofsAddr, FC_EnableCH, Magic_code);
2048 MoxaPortLineStatus(port);
2049}
2050
2051void MoxaPortDisable(int port)
2052{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002053 void __iomem *ofsAddr = moxa_ports[port].tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054
2055 moxafunc(ofsAddr, FC_SetFlowCtl, 0); /* disable flow control */
2056 moxafunc(ofsAddr, FC_ClrLineIrq, Magic_code);
2057 writew(0, ofsAddr + HostStat);
2058 moxafunc(ofsAddr, FC_DisableCH, Magic_code);
2059}
2060
2061long MoxaPortGetMaxBaud(int port)
2062{
2063 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
2064 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI))
2065 return (460800L);
2066 else
2067 return (921600L);
2068}
2069
2070
2071long MoxaPortSetBaud(int port, long baud)
2072{
2073 void __iomem *ofsAddr;
2074 long max, clock;
2075 unsigned int val;
2076
2077 if ((baud < 50L) || ((max = MoxaPortGetMaxBaud(port)) == 0))
2078 return (0);
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002079 ofsAddr = moxa_ports[port].tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 if (baud > max)
2081 baud = max;
2082 if (max == 38400L)
2083 clock = 614400L; /* for 9.8304 Mhz : max. 38400 bps */
2084 else if (max == 57600L)
2085 clock = 691200L; /* for 11.0592 Mhz : max. 57600 bps */
2086 else
2087 clock = 921600L; /* for 14.7456 Mhz : max. 115200 bps */
2088 val = clock / baud;
2089 moxafunc(ofsAddr, FC_SetBaud, val);
2090 baud = clock / val;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002091 moxa_ports[port].curBaud = baud;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 return (baud);
2093}
2094
Alan Cox606d0992006-12-08 02:38:45 -08002095int MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096{
2097 void __iomem *ofsAddr;
2098 tcflag_t cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 tcflag_t mode = 0;
2100
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002101 if (moxa_ports[port].chkPort == 0 || termio == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 return (-1);
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002103 ofsAddr = moxa_ports[port].tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 cflag = termio->c_cflag; /* termio->c_cflag */
2105
2106 mode = termio->c_cflag & CSIZE;
2107 if (mode == CS5)
2108 mode = MX_CS5;
2109 else if (mode == CS6)
2110 mode = MX_CS6;
2111 else if (mode == CS7)
2112 mode = MX_CS7;
2113 else if (mode == CS8)
2114 mode = MX_CS8;
2115
2116 if (termio->c_cflag & CSTOPB) {
2117 if (mode == MX_CS5)
2118 mode |= MX_STOP15;
2119 else
2120 mode |= MX_STOP2;
2121 } else
2122 mode |= MX_STOP1;
2123
2124 if (termio->c_cflag & PARENB) {
2125 if (termio->c_cflag & PARODD)
2126 mode |= MX_PARODD;
2127 else
2128 mode |= MX_PAREVEN;
2129 } else
2130 mode |= MX_PARNONE;
2131
2132 moxafunc(ofsAddr, FC_SetDataMode, (ushort) mode);
2133
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
2135 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
Alan Coxc7bce302006-09-30 23:27:24 -07002136 if (baud >= 921600L)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 return (-1);
2138 }
Alan Coxdb1acaa2008-02-08 04:18:43 -08002139 baud = MoxaPortSetBaud(port, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140
2141 if (termio->c_iflag & (IXON | IXOFF | IXANY)) {
2142 writeb(termio->c_cc[VSTART], ofsAddr + FuncArg);
2143 writeb(termio->c_cc[VSTOP], ofsAddr + FuncArg1);
2144 writeb(FC_SetXonXoff, ofsAddr + FuncCode);
Jiri Slaby6f56b6582007-10-18 03:06:24 -07002145 moxa_wait_finish(ofsAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146
2147 }
Alan Coxdb1acaa2008-02-08 04:18:43 -08002148 return (baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149}
2150
2151int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState)
2152{
2153
2154 if (!MoxaPortIsValid(port))
2155 return (-1);
2156 if (dtrState) {
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002157 if (moxa_ports[port].lineCtrl & DTR_ON)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 *dtrState = 1;
2159 else
2160 *dtrState = 0;
2161 }
2162 if (rtsState) {
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002163 if (moxa_ports[port].lineCtrl & RTS_ON)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 *rtsState = 1;
2165 else
2166 *rtsState = 0;
2167 }
2168 return (0);
2169}
2170
2171void MoxaPortLineCtrl(int port, int dtr, int rts)
2172{
2173 void __iomem *ofsAddr;
2174 int mode;
2175
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002176 ofsAddr = moxa_ports[port].tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 mode = 0;
2178 if (dtr)
2179 mode |= DTR_ON;
2180 if (rts)
2181 mode |= RTS_ON;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002182 moxa_ports[port].lineCtrl = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 moxafunc(ofsAddr, FC_LineControl, mode);
2184}
2185
2186void MoxaPortFlowCtrl(int port, int rts, int cts, int txflow, int rxflow, int txany)
2187{
2188 void __iomem *ofsAddr;
2189 int mode;
2190
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002191 ofsAddr = moxa_ports[port].tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 mode = 0;
2193 if (rts)
2194 mode |= RTS_FlowCtl;
2195 if (cts)
2196 mode |= CTS_FlowCtl;
2197 if (txflow)
2198 mode |= Tx_FlowCtl;
2199 if (rxflow)
2200 mode |= Rx_FlowCtl;
2201 if (txany)
2202 mode |= IXM_IXANY;
2203 moxafunc(ofsAddr, FC_SetFlowCtl, mode);
2204}
2205
2206int MoxaPortLineStatus(int port)
2207{
2208 void __iomem *ofsAddr;
2209 int val;
2210
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002211 ofsAddr = moxa_ports[port].tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
2213 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
2214 moxafunc(ofsAddr, FC_LineStatus, 0);
2215 val = readw(ofsAddr + FuncArg);
2216 } else {
2217 val = readw(ofsAddr + FlagStat) >> 4;
2218 }
2219 val &= 0x0B;
2220 if (val & 8) {
2221 val |= 4;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002222 if ((moxa_ports[port].DCDState & DCD_oldstate) == 0)
2223 moxa_ports[port].DCDState = (DCD_oldstate | DCD_changed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 } else {
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002225 if (moxa_ports[port].DCDState & DCD_oldstate)
2226 moxa_ports[port].DCDState = DCD_changed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 }
2228 val &= 7;
2229 return (val);
2230}
2231
2232int MoxaPortDCDChange(int port)
2233{
2234 int n;
2235
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002236 if (moxa_ports[port].chkPort == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 return (0);
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002238 n = moxa_ports[port].DCDState;
2239 moxa_ports[port].DCDState &= ~DCD_changed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 n &= DCD_changed;
2241 return (n);
2242}
2243
2244int MoxaPortDCDON(int port)
2245{
2246 int n;
2247
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002248 if (moxa_ports[port].chkPort == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 return (0);
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002250 if (moxa_ports[port].DCDState & DCD_oldstate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 n = 1;
2252 else
2253 n = 0;
2254 return (n);
2255}
2256
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257int MoxaPortWriteData(int port, unsigned char * buffer, int len)
2258{
2259 int c, total, i;
2260 ushort tail;
2261 int cnt;
2262 ushort head, tx_mask, spage, epage;
2263 ushort pageno, pageofs, bufhead;
2264 void __iomem *baseAddr, *ofsAddr, *ofs;
2265
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002266 ofsAddr = moxa_ports[port].tableAddr;
2267 baseAddr = moxa_boards[port / MAX_PORTS_PER_BOARD].basemem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 tx_mask = readw(ofsAddr + TX_mask);
2269 spage = readw(ofsAddr + Page_txb);
2270 epage = readw(ofsAddr + EndPage_txb);
2271 tail = readw(ofsAddr + TXwptr);
2272 head = readw(ofsAddr + TXrptr);
2273 c = (head > tail) ? (head - tail - 1)
2274 : (head - tail + tx_mask);
2275 if (c > len)
2276 c = len;
2277 moxaLog.txcnt[port] += c;
2278 total = c;
2279 if (spage == epage) {
2280 bufhead = readw(ofsAddr + Ofs_txb);
2281 writew(spage, baseAddr + Control_reg);
2282 while (c > 0) {
2283 if (head > tail)
2284 len = head - tail - 1;
2285 else
2286 len = tx_mask + 1 - tail;
2287 len = (c > len) ? len : c;
2288 ofs = baseAddr + DynPage_addr + bufhead + tail;
2289 for (i = 0; i < len; i++)
2290 writeb(*buffer++, ofs + i);
2291 tail = (tail + len) & tx_mask;
2292 c -= len;
2293 }
2294 writew(tail, ofsAddr + TXwptr);
2295 } else {
2296 len = c;
2297 pageno = spage + (tail >> 13);
2298 pageofs = tail & Page_mask;
2299 do {
2300 cnt = Page_size - pageofs;
2301 if (cnt > c)
2302 cnt = c;
2303 c -= cnt;
2304 writeb(pageno, baseAddr + Control_reg);
2305 ofs = baseAddr + DynPage_addr + pageofs;
2306 for (i = 0; i < cnt; i++)
2307 writeb(*buffer++, ofs + i);
2308 if (c == 0) {
2309 writew((tail + len) & tx_mask, ofsAddr + TXwptr);
2310 break;
2311 }
2312 if (++pageno == epage)
2313 pageno = spage;
2314 pageofs = 0;
2315 } while (1);
2316 }
2317 writeb(1, ofsAddr + CD180TXirq); /* start to send */
2318 return (total);
2319}
2320
Alan Cox33f0f882006-01-09 20:54:13 -08002321int MoxaPortReadData(int port, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322{
2323 register ushort head, pageofs;
2324 int i, count, cnt, len, total, remain;
2325 ushort tail, rx_mask, spage, epage;
2326 ushort pageno, bufhead;
2327 void __iomem *baseAddr, *ofsAddr, *ofs;
2328
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002329 ofsAddr = moxa_ports[port].tableAddr;
2330 baseAddr = moxa_boards[port / MAX_PORTS_PER_BOARD].basemem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 head = readw(ofsAddr + RXrptr);
2332 tail = readw(ofsAddr + RXwptr);
2333 rx_mask = readw(ofsAddr + RX_mask);
2334 spage = readw(ofsAddr + Page_rxb);
2335 epage = readw(ofsAddr + EndPage_rxb);
2336 count = (tail >= head) ? (tail - head)
2337 : (tail - head + rx_mask + 1);
2338 if (count == 0)
Alan Cox33f0f882006-01-09 20:54:13 -08002339 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340
Alan Cox33f0f882006-01-09 20:54:13 -08002341 total = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 remain = count - total;
2343 moxaLog.rxcnt[port] += total;
2344 count = total;
2345 if (spage == epage) {
2346 bufhead = readw(ofsAddr + Ofs_rxb);
2347 writew(spage, baseAddr + Control_reg);
2348 while (count > 0) {
2349 if (tail >= head)
2350 len = tail - head;
2351 else
2352 len = rx_mask + 1 - head;
2353 len = (count > len) ? len : count;
2354 ofs = baseAddr + DynPage_addr + bufhead + head;
2355 for (i = 0; i < len; i++)
Alan Cox33f0f882006-01-09 20:54:13 -08002356 tty_insert_flip_char(tty, readb(ofs + i), TTY_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 head = (head + len) & rx_mask;
2358 count -= len;
2359 }
2360 writew(head, ofsAddr + RXrptr);
2361 } else {
2362 len = count;
2363 pageno = spage + (head >> 13);
2364 pageofs = head & Page_mask;
2365 do {
2366 cnt = Page_size - pageofs;
2367 if (cnt > count)
2368 cnt = count;
2369 count -= cnt;
2370 writew(pageno, baseAddr + Control_reg);
2371 ofs = baseAddr + DynPage_addr + pageofs;
2372 for (i = 0; i < cnt; i++)
Alan Cox33f0f882006-01-09 20:54:13 -08002373 tty_insert_flip_char(tty, readb(ofs + i), TTY_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 if (count == 0) {
2375 writew((head + len) & rx_mask, ofsAddr + RXrptr);
2376 break;
2377 }
2378 if (++pageno == epage)
2379 pageno = spage;
2380 pageofs = 0;
2381 } while (1);
2382 }
2383 if ((readb(ofsAddr + FlagStat) & Xoff_state) && (remain < LowWater)) {
2384 moxaLowWaterChk = 1;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002385 moxa_ports[port].lowChkFlag = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 }
2387 return (total);
2388}
2389
2390
2391int MoxaPortTxQueue(int port)
2392{
2393 void __iomem *ofsAddr;
2394 ushort rptr, wptr, mask;
2395 int len;
2396
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002397 ofsAddr = moxa_ports[port].tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 rptr = readw(ofsAddr + TXrptr);
2399 wptr = readw(ofsAddr + TXwptr);
2400 mask = readw(ofsAddr + TX_mask);
2401 len = (wptr - rptr) & mask;
2402 return (len);
2403}
2404
2405int MoxaPortTxFree(int port)
2406{
2407 void __iomem *ofsAddr;
2408 ushort rptr, wptr, mask;
2409 int len;
2410
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002411 ofsAddr = moxa_ports[port].tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 rptr = readw(ofsAddr + TXrptr);
2413 wptr = readw(ofsAddr + TXwptr);
2414 mask = readw(ofsAddr + TX_mask);
2415 len = mask - ((wptr - rptr) & mask);
2416 return (len);
2417}
2418
2419int MoxaPortRxQueue(int port)
2420{
2421 void __iomem *ofsAddr;
2422 ushort rptr, wptr, mask;
2423 int len;
2424
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002425 ofsAddr = moxa_ports[port].tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 rptr = readw(ofsAddr + RXrptr);
2427 wptr = readw(ofsAddr + RXwptr);
2428 mask = readw(ofsAddr + RX_mask);
2429 len = (wptr - rptr) & mask;
2430 return (len);
2431}
2432
2433
2434void MoxaPortTxDisable(int port)
2435{
2436 void __iomem *ofsAddr;
2437
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002438 ofsAddr = moxa_ports[port].tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 moxafunc(ofsAddr, FC_SetXoffState, Magic_code);
2440}
2441
2442void MoxaPortTxEnable(int port)
2443{
2444 void __iomem *ofsAddr;
2445
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002446 ofsAddr = moxa_ports[port].tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 moxafunc(ofsAddr, FC_SetXonState, Magic_code);
2448}
2449
2450
2451int MoxaPortResetBrkCnt(int port)
2452{
2453 ushort cnt;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002454 cnt = moxa_ports[port].breakCnt;
2455 moxa_ports[port].breakCnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 return (cnt);
2457}
2458
2459
2460void MoxaPortSendBreak(int port, int ms100)
2461{
2462 void __iomem *ofsAddr;
2463
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002464 ofsAddr = moxa_ports[port].tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 if (ms100) {
2466 moxafunc(ofsAddr, FC_SendBreak, Magic_code);
Jiri Slaby24c032f2007-07-17 04:05:19 -07002467 msleep(ms100 * 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 } else {
2469 moxafunc(ofsAddr, FC_SendBreak, Magic_code);
Jiri Slaby24c032f2007-07-17 04:05:19 -07002470 msleep(250);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 }
2472 moxafunc(ofsAddr, FC_StopBreak, Magic_code);
2473}
2474
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002475static int moxa_get_serial_info(struct moxa_port *info,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 struct serial_struct __user *retinfo)
2477{
2478 struct serial_struct tmp;
2479
2480 memset(&tmp, 0, sizeof(tmp));
2481 tmp.type = info->type;
2482 tmp.line = info->port;
2483 tmp.port = 0;
2484 tmp.irq = 0;
2485 tmp.flags = info->asyncflags;
2486 tmp.baud_base = 921600;
2487 tmp.close_delay = info->close_delay;
2488 tmp.closing_wait = info->closing_wait;
2489 tmp.custom_divisor = 0;
2490 tmp.hub6 = 0;
2491 if(copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2492 return -EFAULT;
2493 return (0);
2494}
2495
2496
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002497static int moxa_set_serial_info(struct moxa_port *info,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 struct serial_struct __user *new_info)
2499{
2500 struct serial_struct new_serial;
2501
2502 if(copy_from_user(&new_serial, new_info, sizeof(new_serial)))
2503 return -EFAULT;
2504
2505 if ((new_serial.irq != 0) ||
2506 (new_serial.port != 0) ||
2507// (new_serial.type != info->type) ||
2508 (new_serial.custom_divisor != 0) ||
2509 (new_serial.baud_base != 921600))
2510 return (-EPERM);
2511
2512 if (!capable(CAP_SYS_ADMIN)) {
2513 if (((new_serial.flags & ~ASYNC_USR_MASK) !=
2514 (info->asyncflags & ~ASYNC_USR_MASK)))
2515 return (-EPERM);
2516 } else {
2517 info->close_delay = new_serial.close_delay * HZ / 100;
2518 info->closing_wait = new_serial.closing_wait * HZ / 100;
2519 }
2520
2521 new_serial.flags = (new_serial.flags & ~ASYNC_FLAGS);
2522 new_serial.flags |= (info->asyncflags & ASYNC_FLAGS);
2523
2524 if (new_serial.type == PORT_16550A) {
2525 MoxaSetFifo(info->port, 1);
2526 } else {
2527 MoxaSetFifo(info->port, 0);
2528 }
2529
2530 info->type = new_serial.type;
2531 return (0);
2532}
2533
2534
2535
2536/*****************************************************************************
2537 * Static local functions: *
2538 *****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539static void moxafunc(void __iomem *ofsAddr, int cmd, ushort arg)
2540{
2541
2542 writew(arg, ofsAddr + FuncArg);
2543 writew(cmd, ofsAddr + FuncCode);
Jiri Slaby6f56b6582007-10-18 03:06:24 -07002544 moxa_wait_finish(ofsAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545}
2546
Jiri Slaby6f56b6582007-10-18 03:06:24 -07002547static void moxa_wait_finish(void __iomem *ofsAddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548{
2549 unsigned long i, j;
2550
2551 i = jiffies;
2552 while (readw(ofsAddr + FuncCode) != 0) {
2553 j = jiffies;
2554 if ((j - i) > moxaFuncTout) {
2555 return;
2556 }
2557 }
2558}
2559
Jiri Slaby6f56b6582007-10-18 03:06:24 -07002560static void moxa_low_water_check(void __iomem *ofsAddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561{
2562 int len;
2563 ushort rptr, wptr, mask;
2564
2565 if (readb(ofsAddr + FlagStat) & Xoff_state) {
2566 rptr = readw(ofsAddr + RXrptr);
2567 wptr = readw(ofsAddr + RXwptr);
2568 mask = readw(ofsAddr + RX_mask);
2569 len = (wptr - rptr) & mask;
2570 if (len <= Low_water)
2571 moxafunc(ofsAddr, FC_SendXon, 0);
2572 }
2573}
2574
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575static void MoxaSetFifo(int port, int enable)
2576{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002577 void __iomem *ofsAddr = moxa_ports[port].tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578
2579 if (!enable) {
2580 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 0);
2581 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 1);
2582 } else {
2583 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 3);
2584 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 16);
2585 }
2586}