blob: 133819cf2c5c6e1daf3804ce4b6612e5851ed207 [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
371static int moxa_load_c218(struct moxa_board_conf *brd, const void *ptr,
372 size_t len)
373{
374 void __iomem *baseAddr = brd->basemem;
375 const u16 *uptr = ptr;
376 size_t wlen, len2, j;
377 unsigned int i, retry;
378 u16 usum, keycode;
379
380 if (brd->boardType == MOXA_BOARD_CP204J)
381 keycode = CP204J_KeyCode;
382 else
383 keycode = C218_KeyCode;
384 usum = 0;
385 wlen = len >> 1;
386 for (i = 0; i < wlen; i++)
387 usum += le16_to_cpu(uptr[i]);
388 retry = 0;
389 do {
390 wlen = len >> 1;
391 j = 0;
392 while (wlen) {
393 len2 = (wlen > 2048) ? 2048 : wlen;
394 wlen -= len2;
395 memcpy_toio(baseAddr + C218_LoadBuf, ptr + j,
396 len2 << 1);
397 j += len2 << 1;
398
399 writew(len2, baseAddr + C218DLoad_len);
400 writew(0, baseAddr + C218_key);
401 for (i = 0; i < 100; i++) {
402 if (readw(baseAddr + C218_key) == keycode)
403 break;
404 msleep(10);
405 }
406 if (readw(baseAddr + C218_key) != keycode)
407 return -EIO;
408 }
409 writew(0, baseAddr + C218DLoad_len);
410 writew(usum, baseAddr + C218check_sum);
411 writew(0, baseAddr + C218_key);
412 for (i = 0; i < 100; i++) {
413 if (readw(baseAddr + C218_key) == keycode)
414 break;
415 msleep(10);
416 }
417 retry++;
418 } while ((readb(baseAddr + C218chksum_ok) != 1) && (retry < 3));
419 if (readb(baseAddr + C218chksum_ok) != 1)
420 return -EIO;
421
422 writew(0, baseAddr + C218_key);
423 for (i = 0; i < 100; i++) {
424 if (readw(baseAddr + Magic_no) == Magic_code)
425 break;
426 msleep(10);
427 }
428 if (readw(baseAddr + Magic_no) != Magic_code)
429 return -EIO;
430
431 writew(1, baseAddr + Disable_IRQ);
432 writew(0, baseAddr + Magic_no);
433 for (i = 0; i < 100; i++) {
434 if (readw(baseAddr + Magic_no) == Magic_code)
435 break;
436 msleep(10);
437 }
438 if (readw(baseAddr + Magic_no) != Magic_code)
439 return -EIO;
440
441 moxaCard = 1;
442 brd->intNdx = baseAddr + IRQindex;
443 brd->intPend = baseAddr + IRQpending;
444 brd->intTable = baseAddr + IRQtable;
445
446 return 0;
447}
448
449static int moxa_load_c320(struct moxa_board_conf *brd, const void *ptr,
450 size_t len)
451{
452 void __iomem *baseAddr = brd->basemem;
453 const u16 *uptr = ptr;
454 size_t wlen, len2, j;
455 unsigned int i, retry;
456 u16 usum;
457
458 usum = 0;
459 wlen = len >> 1;
460 for (i = 0; i < wlen; i++)
461 usum += le16_to_cpu(uptr[i]);
462 retry = 0;
463 do {
464 wlen = len >> 1;
465 j = 0;
466 while (wlen) {
467 len2 = (wlen > 2048) ? 2048 : wlen;
468 wlen -= len2;
469 memcpy_toio(baseAddr + C320_LoadBuf, ptr + j,
470 len2 << 1);
471 j += len2 << 1;
472 writew(len2, baseAddr + C320DLoad_len);
473 writew(0, baseAddr + C320_key);
474 for (i = 0; i < 10; i++) {
475 if (readw(baseAddr + C320_key) == C320_KeyCode)
476 break;
477 msleep(10);
478 }
479 if (readw(baseAddr + C320_key) != C320_KeyCode)
480 return -EIO;
481 }
482 writew(0, baseAddr + C320DLoad_len);
483 writew(usum, baseAddr + C320check_sum);
484 writew(0, baseAddr + C320_key);
485 for (i = 0; i < 10; i++) {
486 if (readw(baseAddr + C320_key) == C320_KeyCode)
487 break;
488 msleep(10);
489 }
490 retry++;
491 } while ((readb(baseAddr + C320chksum_ok) != 1) && (retry < 3));
492 if (readb(baseAddr + C320chksum_ok) != 1)
493 return -EIO;
494
495 writew(0, baseAddr + C320_key);
496 for (i = 0; i < 600; i++) {
497 if (readw(baseAddr + Magic_no) == Magic_code)
498 break;
499 msleep(10);
500 }
501 if (readw(baseAddr + Magic_no) != Magic_code)
502 return -EIO;
503
504 if (brd->busType == MOXA_BUS_TYPE_PCI) { /* ASIC board */
505 writew(0x3800, baseAddr + TMS320_PORT1);
506 writew(0x3900, baseAddr + TMS320_PORT2);
507 writew(28499, baseAddr + TMS320_CLOCK);
508 } else {
509 writew(0x3200, baseAddr + TMS320_PORT1);
510 writew(0x3400, baseAddr + TMS320_PORT2);
511 writew(19999, baseAddr + TMS320_CLOCK);
512 }
513 writew(1, baseAddr + Disable_IRQ);
514 writew(0, baseAddr + Magic_no);
515 for (i = 0; i < 500; i++) {
516 if (readw(baseAddr + Magic_no) == Magic_code)
517 break;
518 msleep(10);
519 }
520 if (readw(baseAddr + Magic_no) != Magic_code)
521 return -EIO;
522
523 j = readw(baseAddr + Module_cnt);
524 if (j <= 0)
525 return -EIO;
526 brd->numPorts = j * 8;
527 writew(j, baseAddr + Module_no);
528 writew(0, baseAddr + Magic_no);
529 for (i = 0; i < 600; i++) {
530 if (readw(baseAddr + Magic_no) == Magic_code)
531 break;
532 msleep(10);
533 }
534 if (readw(baseAddr + Magic_no) != Magic_code)
535 return -EIO;
536 moxaCard = 1;
537 brd->intNdx = baseAddr + IRQindex;
538 brd->intPend = baseAddr + IRQpending;
539 brd->intTable = baseAddr + IRQtable;
540
541 return 0;
542}
543
544static int moxa_load_code(struct moxa_board_conf *brd, const void *ptr,
545 size_t len)
546{
547 void __iomem *ofsAddr, *baseAddr = brd->basemem;
548 struct moxa_port *port;
549 int retval, i;
550
551 if (len % 2) {
552 printk(KERN_ERR "moxa: C2XX bios length is not even\n");
553 return -EINVAL;
554 }
555
556 switch (brd->boardType) {
557 case MOXA_BOARD_C218_ISA:
558 case MOXA_BOARD_C218_PCI:
559 case MOXA_BOARD_CP204J:
560 retval = moxa_load_c218(brd, ptr, len);
561 if (retval)
562 return retval;
563 port = brd->ports;
564 for (i = 0; i < brd->numPorts; i++, port++) {
565 port->chkPort = 1;
566 port->curBaud = 9600L;
567 port->DCDState = 0;
568 port->tableAddr = baseAddr + Extern_table +
569 Extern_size * i;
570 ofsAddr = port->tableAddr;
571 writew(C218rx_mask, ofsAddr + RX_mask);
572 writew(C218tx_mask, ofsAddr + TX_mask);
573 writew(C218rx_spage + i * C218buf_pageno, ofsAddr + Page_rxb);
574 writew(readw(ofsAddr + Page_rxb) + C218rx_pageno, ofsAddr + EndPage_rxb);
575
576 writew(C218tx_spage + i * C218buf_pageno, ofsAddr + Page_txb);
577 writew(readw(ofsAddr + Page_txb) + C218tx_pageno, ofsAddr + EndPage_txb);
578
579 }
580 break;
581 default:
582 retval = moxa_load_c320(brd, ptr, len); /* fills in numPorts */
583 if (retval)
584 return retval;
585 port = brd->ports;
586 for (i = 0; i < brd->numPorts; i++, port++) {
587 port->chkPort = 1;
588 port->curBaud = 9600L;
589 port->DCDState = 0;
590 port->tableAddr = baseAddr + Extern_table +
591 Extern_size * i;
592 ofsAddr = port->tableAddr;
593 switch (brd->numPorts) {
594 case 8:
595 writew(C320p8rx_mask, ofsAddr + RX_mask);
596 writew(C320p8tx_mask, ofsAddr + TX_mask);
597 writew(C320p8rx_spage + i * C320p8buf_pgno, ofsAddr + Page_rxb);
598 writew(readw(ofsAddr + Page_rxb) + C320p8rx_pgno, ofsAddr + EndPage_rxb);
599 writew(C320p8tx_spage + i * C320p8buf_pgno, ofsAddr + Page_txb);
600 writew(readw(ofsAddr + Page_txb) + C320p8tx_pgno, ofsAddr + EndPage_txb);
601
602 break;
603 case 16:
604 writew(C320p16rx_mask, ofsAddr + RX_mask);
605 writew(C320p16tx_mask, ofsAddr + TX_mask);
606 writew(C320p16rx_spage + i * C320p16buf_pgno, ofsAddr + Page_rxb);
607 writew(readw(ofsAddr + Page_rxb) + C320p16rx_pgno, ofsAddr + EndPage_rxb);
608 writew(C320p16tx_spage + i * C320p16buf_pgno, ofsAddr + Page_txb);
609 writew(readw(ofsAddr + Page_txb) + C320p16tx_pgno, ofsAddr + EndPage_txb);
610 break;
611
612 case 24:
613 writew(C320p24rx_mask, ofsAddr + RX_mask);
614 writew(C320p24tx_mask, ofsAddr + TX_mask);
615 writew(C320p24rx_spage + i * C320p24buf_pgno, ofsAddr + Page_rxb);
616 writew(readw(ofsAddr + Page_rxb) + C320p24rx_pgno, ofsAddr + EndPage_rxb);
617 writew(C320p24tx_spage + i * C320p24buf_pgno, ofsAddr + Page_txb);
618 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
619 break;
620 case 32:
621 writew(C320p32rx_mask, ofsAddr + RX_mask);
622 writew(C320p32tx_mask, ofsAddr + TX_mask);
623 writew(C320p32tx_ofs, ofsAddr + Ofs_txb);
624 writew(C320p32rx_spage + i * C320p32buf_pgno, ofsAddr + Page_rxb);
625 writew(readb(ofsAddr + Page_rxb), ofsAddr + EndPage_rxb);
626 writew(C320p32tx_spage + i * C320p32buf_pgno, ofsAddr + Page_txb);
627 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
628 break;
629 }
630 }
631 break;
632 }
633 brd->loadstat = 1;
634 return 0;
635}
636
637static int moxa_load_fw(struct moxa_board_conf *brd, const struct firmware *fw)
638{
639 void *ptr = fw->data;
640 char rsn[64];
641 u16 lens[5];
642 size_t len;
643 unsigned int a, lenp, lencnt;
644 int ret = -EINVAL;
645 struct {
646 __le32 magic; /* 0x34303430 */
647 u8 reserved1[2];
648 u8 type; /* UNIX = 3 */
649 u8 model; /* C218T=1, C320T=2, CP204=3 */
650 u8 reserved2[8];
651 __le16 len[5];
652 } *hdr = ptr;
653
654 BUILD_BUG_ON(ARRAY_SIZE(hdr->len) != ARRAY_SIZE(lens));
655
656 if (fw->size < MOXA_FW_HDRLEN) {
657 strcpy(rsn, "too short (even header won't fit)");
658 goto err;
659 }
660 if (hdr->magic != cpu_to_le32(0x30343034)) {
661 sprintf(rsn, "bad magic: %.8x", le32_to_cpu(hdr->magic));
662 goto err;
663 }
664 if (hdr->type != 3) {
665 sprintf(rsn, "not for linux, type is %u", hdr->type);
666 goto err;
667 }
668 if (moxa_check_fw_model(brd, hdr->model)) {
669 sprintf(rsn, "not for this card, model is %u", hdr->model);
670 goto err;
671 }
672
673 len = MOXA_FW_HDRLEN;
674 lencnt = hdr->model == 2 ? 5 : 3;
675 for (a = 0; a < ARRAY_SIZE(lens); a++) {
676 lens[a] = le16_to_cpu(hdr->len[a]);
677 if (lens[a] && len + lens[a] <= fw->size &&
678 moxa_check_fw(&fw->data[len]))
679 printk(KERN_WARNING "moxa firmware: unexpected input "
680 "at offset %u, but going on\n", (u32)len);
681 if (!lens[a] && a < lencnt) {
682 sprintf(rsn, "too few entries in fw file");
683 goto err;
684 }
685 len += lens[a];
686 }
687
688 if (len != fw->size) {
689 sprintf(rsn, "bad length: %u (should be %u)", (u32)fw->size,
690 (u32)len);
691 goto err;
692 }
693
694 ptr += MOXA_FW_HDRLEN;
695 lenp = 0; /* bios */
696
697 strcpy(rsn, "read above");
698
699 ret = moxa_load_bios(brd, ptr, lens[lenp]);
700 if (ret)
701 goto err;
702
703 /* we skip the tty section (lens[1]), since we don't need it */
704 ptr += lens[lenp] + lens[lenp + 1];
705 lenp += 2; /* comm */
706
707 if (hdr->model == 2) {
708 ret = moxa_load_320b(brd, ptr, lens[lenp]);
709 if (ret)
710 goto err;
711 /* skip another tty */
712 ptr += lens[lenp] + lens[lenp + 1];
713 lenp += 2;
714 }
715
716 ret = moxa_load_code(brd, ptr, lens[lenp]);
717 if (ret)
718 goto err;
719
720 return 0;
721err:
722 printk(KERN_ERR "firmware failed to load, reason: %s\n", rsn);
723 return ret;
724}
725
726static int moxa_init_board(struct moxa_board_conf *brd, struct device *dev)
727{
728 const struct firmware *fw;
729 const char *file;
730 int ret;
731
732 switch (brd->boardType) {
733 case MOXA_BOARD_C218_ISA:
734 case MOXA_BOARD_C218_PCI:
735 file = "c218tunx.cod";
736 break;
737 case MOXA_BOARD_CP204J:
738 file = "cp204unx.cod";
739 break;
740 default:
741 file = "c320tunx.cod";
742 break;
743 }
744
745 ret = request_firmware(&fw, file, dev);
746 if (ret) {
747 printk(KERN_ERR "request_firmware failed\n");
748 goto end;
749 }
750
751 ret = moxa_load_fw(brd, fw);
752
753 release_firmware(fw);
754end:
755 return ret;
756}
757
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758#ifdef CONFIG_PCI
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800759static int __devinit moxa_pci_probe(struct pci_dev *pdev,
760 const struct pci_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761{
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800762 struct moxa_board_conf *board;
763 unsigned int i;
764 int board_type = ent->driver_data;
765 int retval;
766
767 retval = pci_enable_device(pdev);
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700768 if (retval) {
769 dev_err(&pdev->dev, "can't enable pci device\n");
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800770 goto err;
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700771 }
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800772
773 for (i = 0; i < MAX_BOARDS; i++)
774 if (moxa_boards[i].basemem == NULL)
775 break;
776
777 retval = -ENODEV;
778 if (i >= MAX_BOARDS) {
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700779 dev_warn(&pdev->dev, "more than %u MOXA Intellio family boards "
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800780 "found. Board is ignored.\n", MAX_BOARDS);
781 goto err;
782 }
783
784 board = &moxa_boards[i];
Jiri Slaby03718232008-04-30 00:53:39 -0700785 board->ports = &moxa_ports[i * MAX_PORTS_PER_BOARD];
Jiri Slabye46a5e32008-04-30 00:53:37 -0700786
787 retval = pci_request_region(pdev, 2, "moxa-base");
788 if (retval) {
789 dev_err(&pdev->dev, "can't request pci region 2\n");
790 goto err;
791 }
792
793 board->basemem = ioremap(pci_resource_start(pdev, 2), 0x4000);
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700794 if (board->basemem == NULL) {
795 dev_err(&pdev->dev, "can't remap io space 2\n");
Jiri Slabye46a5e32008-04-30 00:53:37 -0700796 goto err_reg;
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700797 }
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800798
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 board->boardType = board_type;
800 switch (board_type) {
801 case MOXA_BOARD_C218_ISA:
802 case MOXA_BOARD_C218_PCI:
803 board->numPorts = 8;
804 break;
805
806 case MOXA_BOARD_CP204J:
807 board->numPorts = 4;
808 break;
809 default:
810 board->numPorts = 0;
811 break;
812 }
813 board->busType = MOXA_BUS_TYPE_PCI;
Jiri Slabya784bf72007-02-10 01:45:36 -0800814
Jiri Slaby03718232008-04-30 00:53:39 -0700815 retval = moxa_init_board(board, &pdev->dev);
816 if (retval)
817 goto err_base;
818
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800819 pci_set_drvdata(pdev, board);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
821 return (0);
Jiri Slaby03718232008-04-30 00:53:39 -0700822err_base:
823 iounmap(board->basemem);
824 board->basemem = NULL;
Jiri Slabye46a5e32008-04-30 00:53:37 -0700825err_reg:
826 pci_release_region(pdev, 2);
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800827err:
828 return retval;
829}
830
831static void __devexit moxa_pci_remove(struct pci_dev *pdev)
832{
833 struct moxa_board_conf *brd = pci_get_drvdata(pdev);
834
Jiri Slabye46a5e32008-04-30 00:53:37 -0700835 iounmap(brd->basemem);
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800836 brd->basemem = NULL;
Jiri Slabye46a5e32008-04-30 00:53:37 -0700837 pci_release_region(pdev, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838}
Jiri Slabya784bf72007-02-10 01:45:36 -0800839
840static struct pci_driver moxa_pci_driver = {
841 .name = "moxa",
842 .id_table = moxa_pcibrds,
843 .probe = moxa_pci_probe,
844 .remove = __devexit_p(moxa_pci_remove)
845};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846#endif /* CONFIG_PCI */
847
848static int __init moxa_init(void)
849{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800850 struct moxa_port *ch;
Jiri Slabyd353eca2008-04-30 00:53:37 -0700851 unsigned int i, isabrds = 0;
852 int retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700854 printk(KERN_INFO "MOXA Intellio family driver version %s\n",
855 MOXA_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 moxaDriver = alloc_tty_driver(MAX_PORTS + 1);
857 if (!moxaDriver)
858 return -ENOMEM;
859
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 moxaDriver->owner = THIS_MODULE;
Sergey Vlasov9b4e3b12005-09-03 16:26:49 +0100861 moxaDriver->name = "ttyMX";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 moxaDriver->major = ttymajor;
863 moxaDriver->minor_start = 0;
864 moxaDriver->type = TTY_DRIVER_TYPE_SERIAL;
865 moxaDriver->subtype = SERIAL_TYPE_NORMAL;
866 moxaDriver->init_termios = tty_std_termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 moxaDriver->init_termios.c_cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
Alan Cox606d0992006-12-08 02:38:45 -0800868 moxaDriver->init_termios.c_ispeed = 9600;
869 moxaDriver->init_termios.c_ospeed = 9600;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 moxaDriver->flags = TTY_DRIVER_REAL_RAW;
871 tty_set_operations(moxaDriver, &moxa_ops);
872
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800873 for (i = 0, ch = moxa_ports; i < MAX_PORTS; i++, ch++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 ch->type = PORT_16550A;
875 ch->port = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 ch->close_delay = 5 * HZ / 10;
877 ch->closing_wait = 30 * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 ch->cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
879 init_waitqueue_head(&ch->open_wait);
Jiri Slaby95e07912007-10-18 03:06:25 -0700880 init_completion(&ch->close_wait);
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800881
Jiri Slaby6f56b6582007-10-18 03:06:24 -0700882 setup_timer(&ch->emptyTimer, moxa_check_xmit_empty,
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800883 (unsigned long)ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 }
885
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700886 pr_debug("Moxa tty devices major number = %d\n", ttymajor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
888 if (tty_register_driver(moxaDriver)) {
889 printk(KERN_ERR "Couldn't install MOXA Smartio family driver !\n");
890 put_tty_driver(moxaDriver);
891 return -1;
892 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
Jiri Slabyaa7e5222007-02-10 01:45:27 -0800894 mod_timer(&moxaTimer, jiffies + HZ / 50);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
Jiri Slabyd353eca2008-04-30 00:53:37 -0700896 /* Find the boards defined from module args. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897#ifdef MODULE
Jiri Slabyd353eca2008-04-30 00:53:37 -0700898 {
899 struct moxa_board_conf *brd = moxa_boards;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 for (i = 0; i < MAX_BOARDS; i++) {
Jiri Slabyd353eca2008-04-30 00:53:37 -0700901 if (!baseaddr[i])
902 break;
903 if (type[i] == MOXA_BOARD_C218_ISA ||
904 type[i] == MOXA_BOARD_C320_ISA) {
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700905 pr_debug("Moxa board %2d: %s board(baseAddr=%lx)\n",
Jiri Slabyd353eca2008-04-30 00:53:37 -0700906 isabrds + 1, moxa_brdname[type[i] - 1],
907 baseaddr[i]);
908 brd->boardType = type[i];
Jiri Slaby03718232008-04-30 00:53:39 -0700909 brd->ports = &moxa_ports[isabrds * MAX_PORTS_PER_BOARD];
Jiri Slabyd353eca2008-04-30 00:53:37 -0700910 brd->numPorts = type[i] == MOXA_BOARD_C218_ISA ? 8 :
911 numports[i];
912 brd->busType = MOXA_BUS_TYPE_ISA;
913 brd->basemem = ioremap(baseaddr[i], 0x4000);
914 if (!brd->basemem) {
915 printk(KERN_ERR "moxa: can't remap %lx\n",
916 baseaddr[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 continue;
918 }
Jiri Slaby03718232008-04-30 00:53:39 -0700919 if (moxa_init_board(brd, NULL)) {
920 iounmap(brd->basemem);
921 brd->basemem = NULL;
922 continue;
923 }
Jiri Slabyd353eca2008-04-30 00:53:37 -0700924
925 brd++;
926 isabrds++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 }
928 }
Jiri Slabyd353eca2008-04-30 00:53:37 -0700929 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930#endif
Jiri Slabya784bf72007-02-10 01:45:36 -0800931
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932#ifdef CONFIG_PCI
Jiri Slabya784bf72007-02-10 01:45:36 -0800933 retval = pci_register_driver(&moxa_pci_driver);
934 if (retval) {
935 printk(KERN_ERR "Can't register moxa pci driver!\n");
Jiri Slabyd353eca2008-04-30 00:53:37 -0700936 if (isabrds)
Jiri Slabya784bf72007-02-10 01:45:36 -0800937 retval = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 }
939#endif
Jiri Slabya784bf72007-02-10 01:45:36 -0800940
Jiri Slabya784bf72007-02-10 01:45:36 -0800941 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942}
943
944static void __exit moxa_exit(void)
945{
946 int i;
947
Jiri Slabyc251ae02007-02-10 01:45:32 -0800948 del_timer_sync(&moxaTimer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
950 for (i = 0; i < MAX_PORTS; i++)
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800951 del_timer_sync(&moxa_ports[i].emptyTimer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
953 if (tty_unregister_driver(moxaDriver))
Jiri Slaby7aeb95d2007-10-18 03:06:24 -0700954 printk(KERN_ERR "Couldn't unregister MOXA Intellio family "
955 "serial driver\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 put_tty_driver(moxaDriver);
Jiri Slaby86fbf142006-10-21 10:24:01 -0700957
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800958#ifdef CONFIG_PCI
Jiri Slabya784bf72007-02-10 01:45:36 -0800959 pci_unregister_driver(&moxa_pci_driver);
Jiri Slaby9cde5bf2007-02-10 01:45:35 -0800960#endif
Jiri Slabya784bf72007-02-10 01:45:36 -0800961
962 for (i = 0; i < MAX_BOARDS; i++)
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800963 if (moxa_boards[i].basemem)
964 iounmap(moxa_boards[i].basemem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965}
966
967module_init(moxa_init);
968module_exit(moxa_exit);
969
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970static int moxa_open(struct tty_struct *tty, struct file *filp)
971{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800972 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 int port;
974 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
Jiri Slaby11324ed2007-02-10 01:45:31 -0800976 port = tty->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 if (port == MAX_PORTS) {
978 return (0);
979 }
980 if (!MoxaPortIsValid(port)) {
981 tty->driver_data = NULL;
982 return (-ENODEV);
983 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Jiri Slaby8f8ecba2007-02-10 01:45:33 -0800985 ch = &moxa_ports[port];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 ch->count++;
987 tty->driver_data = ch;
988 ch->tty = tty;
989 if (!(ch->asyncflags & ASYNC_INITIALIZED)) {
990 ch->statusflags = 0;
Alan Coxdb1acaa2008-02-08 04:18:43 -0800991 moxa_set_tty_param(tty, tty->termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 MoxaPortLineCtrl(ch->port, 1, 1);
993 MoxaPortEnable(ch->port);
994 ch->asyncflags |= ASYNC_INITIALIZED;
995 }
Jiri Slaby6f56b6582007-10-18 03:06:24 -0700996 retval = moxa_block_till_ready(tty, filp, ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
998 moxa_unthrottle(tty);
999
1000 if (ch->type == PORT_16550A) {
1001 MoxaSetFifo(ch->port, 1);
1002 } else {
1003 MoxaSetFifo(ch->port, 0);
1004 }
1005
1006 return (retval);
1007}
1008
1009static void moxa_close(struct tty_struct *tty, struct file *filp)
1010{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001011 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 int port;
1013
Jiri Slaby11324ed2007-02-10 01:45:31 -08001014 port = tty->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 if (port == MAX_PORTS) {
1016 return;
1017 }
1018 if (!MoxaPortIsValid(port)) {
Jiri Slaby7aeb95d2007-10-18 03:06:24 -07001019 pr_debug("Invalid portno in moxa_close\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 tty->driver_data = NULL;
1021 return;
1022 }
1023 if (tty->driver_data == NULL) {
1024 return;
1025 }
1026 if (tty_hung_up_p(filp)) {
1027 return;
1028 }
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001029 ch = (struct moxa_port *) tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
1031 if ((tty->count == 1) && (ch->count != 1)) {
Jiri Slaby7aeb95d2007-10-18 03:06:24 -07001032 printk(KERN_WARNING "moxa_close: bad serial port count; "
1033 "tty->count is 1, ch->count is %d\n", ch->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 ch->count = 1;
1035 }
1036 if (--ch->count < 0) {
Jiri Slaby7aeb95d2007-10-18 03:06:24 -07001037 printk(KERN_WARNING "moxa_close: bad serial port count, "
1038 "device=%s\n", tty->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 ch->count = 0;
1040 }
1041 if (ch->count) {
1042 return;
1043 }
1044 ch->asyncflags |= ASYNC_CLOSING;
1045
1046 ch->cflag = tty->termios->c_cflag;
1047 if (ch->asyncflags & ASYNC_INITIALIZED) {
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001048 moxa_setup_empty_event(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 tty_wait_until_sent(tty, 30 * HZ); /* 30 seconds timeout */
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001050 del_timer_sync(&moxa_ports[ch->port].emptyTimer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 }
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001052 moxa_shut_down(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 MoxaPortFlushData(port, 2);
1054
1055 if (tty->driver->flush_buffer)
1056 tty->driver->flush_buffer(tty);
1057 tty_ldisc_flush(tty);
1058
1059 tty->closing = 0;
1060 ch->event = 0;
1061 ch->tty = NULL;
1062 if (ch->blocked_open) {
1063 if (ch->close_delay) {
1064 msleep_interruptible(jiffies_to_msecs(ch->close_delay));
1065 }
1066 wake_up_interruptible(&ch->open_wait);
1067 }
1068 ch->asyncflags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
Jiri Slaby95e07912007-10-18 03:06:25 -07001069 complete_all(&ch->close_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070}
1071
1072static int moxa_write(struct tty_struct *tty,
1073 const unsigned char *buf, int count)
1074{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001075 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 int len, port;
1077 unsigned long flags;
1078
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001079 ch = (struct moxa_port *) tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 if (ch == NULL)
1081 return (0);
1082 port = ch->port;
Alan Cox33f0f882006-01-09 20:54:13 -08001083
1084 spin_lock_irqsave(&moxa_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 len = MoxaPortWriteData(port, (unsigned char *) buf, count);
Alan Cox33f0f882006-01-09 20:54:13 -08001086 spin_unlock_irqrestore(&moxa_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
1088 /*********************************************
1089 if ( !(ch->statusflags & LOWWAIT) &&
1090 ((len != count) || (MoxaPortTxFree(port) <= 100)) )
1091 ************************************************/
1092 ch->statusflags |= LOWWAIT;
1093 return (len);
1094}
1095
1096static int moxa_write_room(struct tty_struct *tty)
1097{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001098 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
1100 if (tty->stopped)
1101 return (0);
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001102 ch = (struct moxa_port *) tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 if (ch == NULL)
1104 return (0);
1105 return (MoxaPortTxFree(ch->port));
1106}
1107
1108static void moxa_flush_buffer(struct tty_struct *tty)
1109{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001110 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
1112 if (ch == NULL)
1113 return;
1114 MoxaPortFlushData(ch->port, 1);
1115 tty_wakeup(tty);
1116}
1117
1118static int moxa_chars_in_buffer(struct tty_struct *tty)
1119{
1120 int chars;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001121 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
1123 /*
1124 * Sigh...I have to check if driver_data is NULL here, because
1125 * if an open() fails, the TTY subsystem eventually calls
1126 * tty_wait_until_sent(), which calls the driver's chars_in_buffer()
1127 * routine. And since the open() failed, we return 0 here. TDJ
1128 */
1129 if (ch == NULL)
1130 return (0);
1131 chars = MoxaPortTxQueue(ch->port);
1132 if (chars) {
1133 /*
1134 * Make it possible to wakeup anything waiting for output
1135 * in tty_ioctl.c, etc.
1136 */
1137 if (!(ch->statusflags & EMPTYWAIT))
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001138 moxa_setup_empty_event(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 }
1140 return (chars);
1141}
1142
1143static void moxa_flush_chars(struct tty_struct *tty)
1144{
1145 /*
1146 * Don't think I need this, because this is called to empty the TX
1147 * buffer for the 16450, 16550, etc.
1148 */
1149}
1150
1151static void moxa_put_char(struct tty_struct *tty, unsigned char c)
1152{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001153 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 int port;
1155 unsigned long flags;
1156
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001157 ch = (struct moxa_port *) tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 if (ch == NULL)
1159 return;
1160 port = ch->port;
Alan Cox33f0f882006-01-09 20:54:13 -08001161 spin_lock_irqsave(&moxa_lock, flags);
Jiri Slabyf204d262007-02-10 01:45:25 -08001162 MoxaPortWriteData(port, &c, 1);
Alan Cox33f0f882006-01-09 20:54:13 -08001163 spin_unlock_irqrestore(&moxa_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 /************************************************
1165 if ( !(ch->statusflags & LOWWAIT) && (MoxaPortTxFree(port) <= 100) )
1166 *************************************************/
1167 ch->statusflags |= LOWWAIT;
1168}
1169
1170static int moxa_tiocmget(struct tty_struct *tty, struct file *file)
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 int port;
1174 int flag = 0, dtr, rts;
1175
Jiri Slaby11324ed2007-02-10 01:45:31 -08001176 port = tty->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 if ((port != MAX_PORTS) && (!ch))
1178 return (-EINVAL);
1179
1180 MoxaPortGetLineOut(ch->port, &dtr, &rts);
1181 if (dtr)
1182 flag |= TIOCM_DTR;
1183 if (rts)
1184 flag |= TIOCM_RTS;
1185 dtr = MoxaPortLineStatus(ch->port);
1186 if (dtr & 1)
1187 flag |= TIOCM_CTS;
1188 if (dtr & 2)
1189 flag |= TIOCM_DSR;
1190 if (dtr & 4)
1191 flag |= TIOCM_CD;
1192 return flag;
1193}
1194
1195static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
1196 unsigned int set, unsigned int clear)
1197{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001198 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 int port;
1200 int dtr, rts;
1201
Jiri Slaby11324ed2007-02-10 01:45:31 -08001202 port = tty->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 if ((port != MAX_PORTS) && (!ch))
1204 return (-EINVAL);
1205
1206 MoxaPortGetLineOut(ch->port, &dtr, &rts);
1207 if (set & TIOCM_RTS)
1208 rts = 1;
1209 if (set & TIOCM_DTR)
1210 dtr = 1;
1211 if (clear & TIOCM_RTS)
1212 rts = 0;
1213 if (clear & TIOCM_DTR)
1214 dtr = 0;
1215 MoxaPortLineCtrl(ch->port, dtr, rts);
1216 return 0;
1217}
1218
1219static int moxa_ioctl(struct tty_struct *tty, struct file *file,
1220 unsigned int cmd, unsigned long arg)
1221{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001222 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 register int port;
1224 void __user *argp = (void __user *)arg;
1225 int retval;
1226
Jiri Slaby11324ed2007-02-10 01:45:31 -08001227 port = tty->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 if ((port != MAX_PORTS) && (!ch))
1229 return (-EINVAL);
1230
1231 switch (cmd) {
1232 case TCSBRK: /* SVID version: non-zero arg --> no break */
1233 retval = tty_check_change(tty);
1234 if (retval)
1235 return (retval);
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001236 moxa_setup_empty_event(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 tty_wait_until_sent(tty, 0);
1238 if (!arg)
1239 MoxaPortSendBreak(ch->port, 0);
1240 return (0);
1241 case TCSBRKP: /* support for POSIX tcsendbreak() */
1242 retval = tty_check_change(tty);
1243 if (retval)
1244 return (retval);
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001245 moxa_setup_empty_event(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 tty_wait_until_sent(tty, 0);
1247 MoxaPortSendBreak(ch->port, arg);
1248 return (0);
1249 case TIOCGSOFTCAR:
Jiri Slaby9e9fc312008-04-30 00:53:38 -07001250 return put_user(C_CLOCAL(tty) ? 1 : 0, (int __user *)argp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 case TIOCSSOFTCAR:
Jiri Slaby9e9fc312008-04-30 00:53:38 -07001252 if (get_user(retval, (int __user *)argp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 return -EFAULT;
1254 arg = retval;
1255 tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) |
1256 (arg ? CLOCAL : 0));
1257 if (C_CLOCAL(tty))
1258 ch->asyncflags &= ~ASYNC_CHECK_CD;
1259 else
1260 ch->asyncflags |= ASYNC_CHECK_CD;
1261 return (0);
1262 case TIOCGSERIAL:
1263 return moxa_get_serial_info(ch, argp);
1264
1265 case TIOCSSERIAL:
1266 return moxa_set_serial_info(ch, argp);
1267 default:
1268 retval = MoxaDriverIoctl(cmd, arg, port);
1269 }
1270 return (retval);
1271}
1272
1273static void moxa_throttle(struct tty_struct *tty)
1274{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001275 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
1277 ch->statusflags |= THROTTLE;
1278}
1279
1280static void moxa_unthrottle(struct tty_struct *tty)
1281{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001282 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
1284 ch->statusflags &= ~THROTTLE;
1285}
1286
1287static void moxa_set_termios(struct tty_struct *tty,
Alan Cox606d0992006-12-08 02:38:45 -08001288 struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001290 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
1292 if (ch == NULL)
1293 return;
Alan Coxdb1acaa2008-02-08 04:18:43 -08001294 moxa_set_tty_param(tty, old_termios);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 if (!(old_termios->c_cflag & CLOCAL) &&
1296 (tty->termios->c_cflag & CLOCAL))
1297 wake_up_interruptible(&ch->open_wait);
1298}
1299
1300static void moxa_stop(struct tty_struct *tty)
1301{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001302 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
1304 if (ch == NULL)
1305 return;
1306 MoxaPortTxDisable(ch->port);
1307 ch->statusflags |= TXSTOPPED;
1308}
1309
1310
1311static void moxa_start(struct tty_struct *tty)
1312{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001313 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
1315 if (ch == NULL)
1316 return;
1317
1318 if (!(ch->statusflags & TXSTOPPED))
1319 return;
1320
1321 MoxaPortTxEnable(ch->port);
1322 ch->statusflags &= ~TXSTOPPED;
1323}
1324
1325static void moxa_hangup(struct tty_struct *tty)
1326{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001327 struct moxa_port *ch = (struct moxa_port *) tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
1329 moxa_flush_buffer(tty);
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001330 moxa_shut_down(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 ch->event = 0;
1332 ch->count = 0;
1333 ch->asyncflags &= ~ASYNC_NORMAL_ACTIVE;
1334 ch->tty = NULL;
1335 wake_up_interruptible(&ch->open_wait);
1336}
1337
1338static void moxa_poll(unsigned long ignored)
1339{
1340 register int card;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001341 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 struct tty_struct *tp;
1343 int i, ports;
1344
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 del_timer(&moxaTimer);
1346
1347 if (MoxaDriverPoll() < 0) {
Jiri Slabyaa7e5222007-02-10 01:45:27 -08001348 mod_timer(&moxaTimer, jiffies + HZ / 50);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 return;
1350 }
1351 for (card = 0; card < MAX_BOARDS; card++) {
1352 if ((ports = MoxaPortsOfCard(card)) <= 0)
1353 continue;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001354 ch = &moxa_ports[card * MAX_PORTS_PER_BOARD];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 for (i = 0; i < ports; i++, ch++) {
1356 if ((ch->asyncflags & ASYNC_INITIALIZED) == 0)
1357 continue;
1358 if (!(ch->statusflags & THROTTLE) &&
1359 (MoxaPortRxQueue(ch->port) > 0))
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001360 moxa_receive_data(ch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 if ((tp = ch->tty) == 0)
1362 continue;
1363 if (ch->statusflags & LOWWAIT) {
1364 if (MoxaPortTxQueue(ch->port) <= WAKEUP_CHARS) {
1365 if (!tp->stopped) {
1366 ch->statusflags &= ~LOWWAIT;
1367 tty_wakeup(tp);
1368 }
1369 }
1370 }
1371 if (!I_IGNBRK(tp) && (MoxaPortResetBrkCnt(ch->port) > 0)) {
1372 tty_insert_flip_char(tp, 0, TTY_BREAK);
1373 tty_schedule_flip(tp);
1374 }
1375 if (MoxaPortDCDChange(ch->port)) {
1376 if (ch->asyncflags & ASYNC_CHECK_CD) {
1377 if (MoxaPortDCDON(ch->port))
1378 wake_up_interruptible(&ch->open_wait);
1379 else {
Jiri Slabyba196df2007-02-10 01:45:28 -08001380 tty_hangup(tp);
1381 wake_up_interruptible(&ch->open_wait);
1382 ch->asyncflags &= ~ASYNC_NORMAL_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 }
1384 }
1385 }
1386 }
1387 }
1388
Jiri Slabyaa7e5222007-02-10 01:45:27 -08001389 mod_timer(&moxaTimer, jiffies + HZ / 50);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390}
1391
1392/******************************************************************************/
1393
Alan Coxdb1acaa2008-02-08 04:18:43 -08001394static void moxa_set_tty_param(struct tty_struct *tty, struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395{
Alan Cox606d0992006-12-08 02:38:45 -08001396 register struct ktermios *ts;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001397 struct moxa_port *ch;
Alan Coxdb1acaa2008-02-08 04:18:43 -08001398 int rts, cts, txflow, rxflow, xany, baud;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001400 ch = (struct moxa_port *) tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 ts = tty->termios;
1402 if (ts->c_cflag & CLOCAL)
1403 ch->asyncflags &= ~ASYNC_CHECK_CD;
1404 else
1405 ch->asyncflags |= ASYNC_CHECK_CD;
1406 rts = cts = txflow = rxflow = xany = 0;
1407 if (ts->c_cflag & CRTSCTS)
1408 rts = cts = 1;
1409 if (ts->c_iflag & IXON)
1410 txflow = 1;
1411 if (ts->c_iflag & IXOFF)
1412 rxflow = 1;
1413 if (ts->c_iflag & IXANY)
1414 xany = 1;
Alan Coxdb1acaa2008-02-08 04:18:43 -08001415
1416 /* Clear the features we don't support */
1417 ts->c_cflag &= ~CMSPAR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 MoxaPortFlowCtrl(ch->port, rts, cts, txflow, rxflow, xany);
Alan Coxdb1acaa2008-02-08 04:18:43 -08001419 baud = MoxaPortSetTermio(ch->port, ts, tty_get_baud_rate(tty));
1420 if (baud == -1)
1421 baud = tty_termios_baud_rate(old_termios);
1422 /* Not put the baud rate into the termios data */
1423 tty_encode_baud_rate(tty, baud, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424}
1425
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001426static int moxa_block_till_ready(struct tty_struct *tty, struct file *filp,
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001427 struct moxa_port *ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428{
1429 DECLARE_WAITQUEUE(wait,current);
1430 unsigned long flags;
1431 int retval;
1432 int do_clocal = C_CLOCAL(tty);
1433
1434 /*
1435 * If the device is in the middle of being closed, then block
1436 * until it's done, and then try again.
1437 */
1438 if (tty_hung_up_p(filp) || (ch->asyncflags & ASYNC_CLOSING)) {
1439 if (ch->asyncflags & ASYNC_CLOSING)
Jiri Slaby95e07912007-10-18 03:06:25 -07001440 wait_for_completion_interruptible(&ch->close_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441#ifdef SERIAL_DO_RESTART
1442 if (ch->asyncflags & ASYNC_HUP_NOTIFY)
1443 return (-EAGAIN);
1444 else
1445 return (-ERESTARTSYS);
1446#else
1447 return (-EAGAIN);
1448#endif
1449 }
1450 /*
1451 * If non-blocking mode is set, then make the check up front
1452 * and then exit.
1453 */
1454 if (filp->f_flags & O_NONBLOCK) {
1455 ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
1456 return (0);
1457 }
1458 /*
1459 * Block waiting for the carrier detect and the line to become free
1460 */
1461 retval = 0;
1462 add_wait_queue(&ch->open_wait, &wait);
Jiri Slaby7aeb95d2007-10-18 03:06:24 -07001463 pr_debug("block_til_ready before block: ttys%d, count = %d\n",
1464 ch->port, ch->count);
Alan Cox33f0f882006-01-09 20:54:13 -08001465 spin_lock_irqsave(&moxa_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 if (!tty_hung_up_p(filp))
1467 ch->count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 ch->blocked_open++;
Alan Cox33f0f882006-01-09 20:54:13 -08001469 spin_unlock_irqrestore(&moxa_lock, flags);
1470
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 while (1) {
1472 set_current_state(TASK_INTERRUPTIBLE);
1473 if (tty_hung_up_p(filp) ||
1474 !(ch->asyncflags & ASYNC_INITIALIZED)) {
1475#ifdef SERIAL_DO_RESTART
1476 if (ch->asyncflags & ASYNC_HUP_NOTIFY)
1477 retval = -EAGAIN;
1478 else
1479 retval = -ERESTARTSYS;
1480#else
1481 retval = -EAGAIN;
1482#endif
1483 break;
1484 }
1485 if (!(ch->asyncflags & ASYNC_CLOSING) && (do_clocal ||
1486 MoxaPortDCDON(ch->port)))
1487 break;
1488
1489 if (signal_pending(current)) {
1490 retval = -ERESTARTSYS;
1491 break;
1492 }
1493 schedule();
1494 }
1495 set_current_state(TASK_RUNNING);
1496 remove_wait_queue(&ch->open_wait, &wait);
Alan Cox33f0f882006-01-09 20:54:13 -08001497
1498 spin_lock_irqsave(&moxa_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 if (!tty_hung_up_p(filp))
1500 ch->count++;
1501 ch->blocked_open--;
Alan Cox33f0f882006-01-09 20:54:13 -08001502 spin_unlock_irqrestore(&moxa_lock, flags);
Jiri Slaby7aeb95d2007-10-18 03:06:24 -07001503 pr_debug("block_til_ready after blocking: ttys%d, count = %d\n",
1504 ch->port, ch->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 if (retval)
1506 return (retval);
Alan Cox33f0f882006-01-09 20:54:13 -08001507 /* FIXME: review to see if we need to use set_bit on these */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
Alan Cox33f0f882006-01-09 20:54:13 -08001509 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510}
1511
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001512static void moxa_setup_empty_event(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001514 struct moxa_port *ch = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 unsigned long flags;
1516
Alan Cox33f0f882006-01-09 20:54:13 -08001517 spin_lock_irqsave(&moxa_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 ch->statusflags |= EMPTYWAIT;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001519 mod_timer(&moxa_ports[ch->port].emptyTimer, jiffies + HZ);
Alan Cox33f0f882006-01-09 20:54:13 -08001520 spin_unlock_irqrestore(&moxa_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521}
1522
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001523static void moxa_check_xmit_empty(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001525 struct moxa_port *ch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001527 ch = (struct moxa_port *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 if (ch->tty && (ch->statusflags & EMPTYWAIT)) {
1529 if (MoxaPortTxQueue(ch->port) == 0) {
1530 ch->statusflags &= ~EMPTYWAIT;
1531 tty_wakeup(ch->tty);
1532 return;
1533 }
Jiri Slabyc4342202007-10-18 03:06:19 -07001534 mod_timer(&moxa_ports[ch->port].emptyTimer,
1535 round_jiffies(jiffies + HZ));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 } else
1537 ch->statusflags &= ~EMPTYWAIT;
1538}
1539
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001540static void moxa_shut_down(struct moxa_port *ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541{
1542 struct tty_struct *tp;
1543
1544 if (!(ch->asyncflags & ASYNC_INITIALIZED))
1545 return;
1546
1547 tp = ch->tty;
1548
1549 MoxaPortDisable(ch->port);
1550
1551 /*
1552 * If we're a modem control device and HUPCL is on, drop RTS & DTR.
1553 */
1554 if (tp->termios->c_cflag & HUPCL)
1555 MoxaPortLineCtrl(ch->port, 0, 0);
1556
1557 ch->asyncflags &= ~ASYNC_INITIALIZED;
1558}
1559
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001560static void moxa_receive_data(struct moxa_port *ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561{
1562 struct tty_struct *tp;
Alan Cox606d0992006-12-08 02:38:45 -08001563 struct ktermios *ts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 unsigned long flags;
1565
1566 ts = NULL;
1567 tp = ch->tty;
1568 if (tp)
1569 ts = tp->termios;
1570 /**************************************************
1571 if ( !tp || !ts || !(ts->c_cflag & CREAD) ) {
1572 *****************************************************/
1573 if (!tp || !ts) {
1574 MoxaPortFlushData(ch->port, 0);
1575 return;
1576 }
Alan Cox33f0f882006-01-09 20:54:13 -08001577 spin_lock_irqsave(&moxa_lock, flags);
1578 MoxaPortReadData(ch->port, tp);
1579 spin_unlock_irqrestore(&moxa_lock, flags);
1580 tty_schedule_flip(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581}
1582
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583/*
1584 * Query
1585 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
1587struct mon_str {
1588 int tick;
1589 int rxcnt[MAX_PORTS];
1590 int txcnt[MAX_PORTS];
1591};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592
1593#define DCD_changed 0x01
1594#define DCD_oldstate 0x80
1595
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596static int moxaLowWaterChk;
Jiri Slaby9dff89c2007-02-10 01:45:30 -08001597static struct mon_str moxaLog;
Jiri Slaby9fa372a2007-02-10 01:45:26 -08001598static int moxaFuncTout = HZ / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600static void moxafunc(void __iomem *, int, ushort);
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001601static void moxa_wait_finish(void __iomem *);
1602static void moxa_low_water_check(void __iomem *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
1604/*****************************************************************************
1605 * Driver level functions: *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 * 2. MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port); *
1607 * 3. MoxaDriverPoll(void); *
1608 *****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609#define MOXA 0x400
1610#define MOXA_GET_IQUEUE (MOXA + 1) /* get input buffered count */
1611#define MOXA_GET_OQUEUE (MOXA + 2) /* get output buffered count */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612#define MOXA_GETDATACOUNT (MOXA + 23)
1613#define MOXA_GET_IOQUEUE (MOXA + 27)
1614#define MOXA_FLUSH_QUEUE (MOXA + 28)
1615#define MOXA_GET_CONF (MOXA + 35) /* configuration */
1616#define MOXA_GET_MAJOR (MOXA + 63)
1617#define MOXA_GET_CUMAJOR (MOXA + 64)
1618#define MOXA_GETMSTATUS (MOXA + 65)
1619
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620void MoxaPortFlushData(int port, int mode)
1621{
1622 void __iomem *ofsAddr;
1623 if ((mode < 0) || (mode > 2))
1624 return;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001625 ofsAddr = moxa_ports[port].tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 moxafunc(ofsAddr, FC_FlushQueue, mode);
1627 if (mode != 1) {
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001628 moxa_ports[port].lowChkFlag = 0;
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001629 moxa_low_water_check(ofsAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 }
1631}
1632
1633int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port)
1634{
1635 int i;
1636 int status;
1637 int MoxaPortTxQueue(int), MoxaPortRxQueue(int);
1638 void __user *argp = (void __user *)arg;
1639
Jiri Slaby11324ed2007-02-10 01:45:31 -08001640 if (port == MAX_PORTS) {
Jiri Slaby03718232008-04-30 00:53:39 -07001641 if ((cmd != MOXA_GET_CONF) && (cmd != MOXA_GETDATACOUNT) &&
1642 (cmd != MOXA_GET_IOQUEUE) && (cmd != MOXA_GET_MAJOR) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 (cmd != MOXA_GET_CUMAJOR) && (cmd != MOXA_GETMSTATUS))
1644 return (-EINVAL);
1645 }
1646 switch (cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 case MOXA_GETDATACOUNT:
1648 moxaLog.tick = jiffies;
Jiri Slaby9dff89c2007-02-10 01:45:30 -08001649 if(copy_to_user(argp, &moxaLog, sizeof(struct mon_str)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 return -EFAULT;
1651 return (0);
1652 case MOXA_FLUSH_QUEUE:
1653 MoxaPortFlushData(port, arg);
1654 return (0);
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001655 case MOXA_GET_IOQUEUE: {
1656 struct moxaq_str __user *argm = argp;
Jiri Slaby181d6f42007-02-10 01:45:34 -08001657 struct moxaq_str tmp;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001658
1659 for (i = 0; i < MAX_PORTS; i++, argm++) {
Jiri Slaby181d6f42007-02-10 01:45:34 -08001660 memset(&tmp, 0, sizeof(tmp));
1661 if (moxa_ports[i].chkPort) {
1662 tmp.inq = MoxaPortRxQueue(i);
1663 tmp.outq = MoxaPortTxQueue(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 }
Jiri Slaby181d6f42007-02-10 01:45:34 -08001665 if (copy_to_user(argm, &tmp, sizeof(tmp)))
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001666 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 return (0);
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001669 } case MOXA_GET_OQUEUE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 i = MoxaPortTxQueue(port);
1671 return put_user(i, (unsigned long __user *)argp);
1672 case MOXA_GET_IQUEUE:
1673 i = MoxaPortRxQueue(port);
1674 return put_user(i, (unsigned long __user *)argp);
1675 case MOXA_GET_MAJOR:
1676 if(copy_to_user(argp, &ttymajor, sizeof(int)))
1677 return -EFAULT;
1678 return 0;
1679 case MOXA_GET_CUMAJOR:
1680 i = 0;
1681 if(copy_to_user(argp, &i, sizeof(int)))
1682 return -EFAULT;
1683 return 0;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001684 case MOXA_GETMSTATUS: {
1685 struct mxser_mstatus __user *argm = argp;
Jiri Slaby181d6f42007-02-10 01:45:34 -08001686 struct mxser_mstatus tmp;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001687 struct moxa_port *p;
1688
1689 for (i = 0; i < MAX_PORTS; i++, argm++) {
1690 p = &moxa_ports[i];
Jiri Slaby181d6f42007-02-10 01:45:34 -08001691 memset(&tmp, 0, sizeof(tmp));
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001692 if (!p->chkPort) {
1693 goto copy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 } else {
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001695 status = MoxaPortLineStatus(p->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 if (status & 1)
Jiri Slaby181d6f42007-02-10 01:45:34 -08001697 tmp.cts = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 if (status & 2)
Jiri Slaby181d6f42007-02-10 01:45:34 -08001699 tmp.dsr = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 if (status & 4)
Jiri Slaby181d6f42007-02-10 01:45:34 -08001701 tmp.dcd = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 }
1703
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001704 if (!p->tty || !p->tty->termios)
Jiri Slaby181d6f42007-02-10 01:45:34 -08001705 tmp.cflag = p->cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 else
Jiri Slaby181d6f42007-02-10 01:45:34 -08001707 tmp.cflag = p->tty->termios->c_cflag;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001708copy:
Jiri Slaby181d6f42007-02-10 01:45:34 -08001709 if (copy_to_user(argm, &tmp, sizeof(tmp)))
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001710 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 return 0;
Jiri Slaby03718232008-04-30 00:53:39 -07001713 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 }
1715
Jiri Slaby03718232008-04-30 00:53:39 -07001716 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717}
1718
1719int MoxaDriverPoll(void)
1720{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001721 struct moxa_board_conf *brd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 register ushort temp;
1723 register int card;
1724 void __iomem *ofsAddr;
1725 void __iomem *ip;
1726 int port, p, ports;
1727
1728 if (moxaCard == 0)
1729 return (-1);
1730 for (card = 0; card < MAX_BOARDS; card++) {
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001731 brd = &moxa_boards[card];
1732 if (brd->loadstat == 0)
Dirk Eibach01cfaf02006-08-27 01:23:36 -07001733 continue;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001734 if ((ports = brd->numPorts) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 continue;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001736 if (readb(brd->intPend) == 0xff) {
1737 ip = brd->intTable + readb(brd->intNdx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 p = card * MAX_PORTS_PER_BOARD;
1739 ports <<= 1;
1740 for (port = 0; port < ports; port += 2, p++) {
1741 if ((temp = readw(ip + port)) != 0) {
1742 writew(0, ip + port);
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001743 ofsAddr = moxa_ports[p].tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 if (temp & IntrTx)
1745 writew(readw(ofsAddr + HostStat) & ~WakeupTx, ofsAddr + HostStat);
1746 if (temp & IntrBreak) {
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001747 moxa_ports[p].breakCnt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 }
1749 if (temp & IntrLine) {
1750 if (readb(ofsAddr + FlagStat) & DCD_state) {
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001751 if ((moxa_ports[p].DCDState & DCD_oldstate) == 0)
1752 moxa_ports[p].DCDState = (DCD_oldstate |
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 DCD_changed);
1754 } else {
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001755 if (moxa_ports[p].DCDState & DCD_oldstate)
1756 moxa_ports[p].DCDState = DCD_changed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 }
1758 }
1759 }
1760 }
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001761 writeb(0, brd->intPend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 }
1763 if (moxaLowWaterChk) {
1764 p = card * MAX_PORTS_PER_BOARD;
1765 for (port = 0; port < ports; port++, p++) {
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08001766 if (moxa_ports[p].lowChkFlag) {
1767 moxa_ports[p].lowChkFlag = 0;
1768 ofsAddr = moxa_ports[p].tableAddr;
Jiri Slaby6f56b6582007-10-18 03:06:24 -07001769 moxa_low_water_check(ofsAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 }
1771 }
1772 }
1773 }
1774 moxaLowWaterChk = 0;
1775 return (0);
1776}
1777
1778/*****************************************************************************
1779 * Card level function: *
1780 * 1. MoxaPortsOfCard(int cardno); *
1781 *****************************************************************************/
1782int MoxaPortsOfCard(int cardno)
1783{
1784
1785 if (moxa_boards[cardno].boardType == 0)
1786 return (0);
1787 return (moxa_boards[cardno].numPorts);
1788}
1789
1790/*****************************************************************************
1791 * Port level functions: *
1792 * 1. MoxaPortIsValid(int port); *
1793 * 2. MoxaPortEnable(int port); *
1794 * 3. MoxaPortDisable(int port); *
1795 * 4. MoxaPortGetMaxBaud(int port); *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 * 6. MoxaPortSetBaud(int port, long baud); *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 * 8. MoxaPortSetTermio(int port, unsigned char *termio); *
1798 * 9. MoxaPortGetLineOut(int port, int *dtrState, int *rtsState); *
1799 * 10. MoxaPortLineCtrl(int port, int dtrState, int rtsState); *
1800 * 11. MoxaPortFlowCtrl(int port, int rts, int cts, int rx, int tx,int xany); *
1801 * 12. MoxaPortLineStatus(int port); *
1802 * 13. MoxaPortDCDChange(int port); *
1803 * 14. MoxaPortDCDON(int port); *
1804 * 15. MoxaPortFlushData(int port, int mode); *
1805 * 16. MoxaPortWriteData(int port, unsigned char * buffer, int length); *
Alan Cox33f0f882006-01-09 20:54:13 -08001806 * 17. MoxaPortReadData(int port, struct tty_struct *tty); *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 * 20. MoxaPortTxQueue(int port); *
1808 * 21. MoxaPortTxFree(int port); *
1809 * 22. MoxaPortRxQueue(int port); *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 * 24. MoxaPortTxDisable(int port); *
1811 * 25. MoxaPortTxEnable(int port); *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 * 27. MoxaPortResetBrkCnt(int port); *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 * 30. MoxaPortSendBreak(int port, int ticks); *
1814 *****************************************************************************/
1815/*
1816 * Moxa Port Number Description:
1817 *
1818 * MOXA serial driver supports up to 4 MOXA-C218/C320 boards. And,
1819 * the port number using in MOXA driver functions will be 0 to 31 for
1820 * first MOXA board, 32 to 63 for second, 64 to 95 for third and 96
1821 * to 127 for fourth. For example, if you setup three MOXA boards,
1822 * first board is C218, second board is C320-16 and third board is
1823 * C320-32. The port number of first board (C218 - 8 ports) is from
1824 * 0 to 7. The port number of second board (C320 - 16 ports) is form
1825 * 32 to 47. The port number of third board (C320 - 32 ports) is from
1826 * 64 to 95. And those port numbers form 8 to 31, 48 to 63 and 96 to
1827 * 127 will be invalid.
1828 *
1829 *
1830 * Moxa Functions Description:
1831 *
1832 * Function 1: Driver initialization routine, this routine must be
1833 * called when initialized driver.
1834 * Syntax:
1835 * void MoxaDriverInit();
1836 *
1837 *
1838 * Function 2: Moxa driver private IOCTL command processing.
1839 * Syntax:
1840 * int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port);
1841 *
1842 * unsigned int cmd : IOCTL command
1843 * unsigned long arg : IOCTL argument
1844 * int port : port number (0 - 127)
1845 *
1846 * return: 0 (OK)
1847 * -EINVAL
1848 * -ENOIOCTLCMD
1849 *
1850 *
1851 * Function 3: Moxa driver polling process routine.
1852 * Syntax:
1853 * int MoxaDriverPoll(void);
1854 *
1855 * return: 0 ; polling O.K.
1856 * -1 : no any Moxa card.
1857 *
1858 *
1859 * Function 4: Get the ports of this card.
1860 * Syntax:
1861 * int MoxaPortsOfCard(int cardno);
1862 *
1863 * int cardno : card number (0 - 3)
1864 *
1865 * return: 0 : this card is invalid
1866 * 8/16/24/32
1867 *
1868 *
1869 * Function 5: Check this port is valid or invalid
1870 * Syntax:
1871 * int MoxaPortIsValid(int port);
1872 * int port : port number (0 - 127, ref port description)
1873 *
1874 * return: 0 : this port is invalid
1875 * 1 : this port is valid
1876 *
1877 *
1878 * Function 6: Enable this port to start Tx/Rx data.
1879 * Syntax:
1880 * void MoxaPortEnable(int port);
1881 * int port : port number (0 - 127)
1882 *
1883 *
1884 * Function 7: Disable this port
1885 * Syntax:
1886 * void MoxaPortDisable(int port);
1887 * int port : port number (0 - 127)
1888 *
1889 *
1890 * Function 8: Get the maximun available baud rate of this port.
1891 * Syntax:
1892 * long MoxaPortGetMaxBaud(int port);
1893 * int port : port number (0 - 127)
1894 *
1895 * return: 0 : this port is invalid
1896 * 38400/57600/115200 bps
1897 *
1898 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 * Function 10: Setting baud rate of this port.
1900 * Syntax:
1901 * long MoxaPortSetBaud(int port, long baud);
1902 * int port : port number (0 - 127)
1903 * long baud : baud rate (50 - 115200)
1904 *
1905 * return: 0 : this port is invalid or baud < 50
1906 * 50 - 115200 : the real baud rate set to the port, if
1907 * the argument baud is large than maximun
1908 * available baud rate, the real setting
1909 * baud rate will be the maximun baud rate.
1910 *
1911 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 * Function 12: Configure the port.
1913 * Syntax:
Alan Cox606d0992006-12-08 02:38:45 -08001914 * int MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 * int port : port number (0 - 127)
Alan Cox606d0992006-12-08 02:38:45 -08001916 * struct ktermios * termio : termio structure pointer
Alan Coxc7bce302006-09-30 23:27:24 -07001917 * speed_t baud : baud rate
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 *
1919 * return: -1 : this port is invalid or termio == NULL
1920 * 0 : setting O.K.
1921 *
1922 *
1923 * Function 13: Get the DTR/RTS state of this port.
1924 * Syntax:
1925 * int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState);
1926 * int port : port number (0 - 127)
1927 * int * dtrState : pointer to INT to receive the current DTR
1928 * state. (if NULL, this function will not
1929 * write to this address)
1930 * int * rtsState : pointer to INT to receive the current RTS
1931 * state. (if NULL, this function will not
1932 * write to this address)
1933 *
1934 * return: -1 : this port is invalid
1935 * 0 : O.K.
1936 *
1937 *
1938 * Function 14: Setting the DTR/RTS output state of this port.
1939 * Syntax:
1940 * void MoxaPortLineCtrl(int port, int dtrState, int rtsState);
1941 * int port : port number (0 - 127)
1942 * int dtrState : DTR output state (0: off, 1: on)
1943 * int rtsState : RTS output state (0: off, 1: on)
1944 *
1945 *
1946 * Function 15: Setting the flow control of this port.
1947 * Syntax:
1948 * void MoxaPortFlowCtrl(int port, int rtsFlow, int ctsFlow, int rxFlow,
1949 * int txFlow,int xany);
1950 * int port : port number (0 - 127)
1951 * int rtsFlow : H/W RTS flow control (0: no, 1: yes)
1952 * int ctsFlow : H/W CTS flow control (0: no, 1: yes)
1953 * int rxFlow : S/W Rx XON/XOFF flow control (0: no, 1: yes)
1954 * int txFlow : S/W Tx XON/XOFF flow control (0: no, 1: yes)
1955 * int xany : S/W XANY flow control (0: no, 1: yes)
1956 *
1957 *
1958 * Function 16: Get ths line status of this port
1959 * Syntax:
1960 * int MoxaPortLineStatus(int port);
1961 * int port : port number (0 - 127)
1962 *
1963 * return: Bit 0 - CTS state (0: off, 1: on)
1964 * Bit 1 - DSR state (0: off, 1: on)
1965 * Bit 2 - DCD state (0: off, 1: on)
1966 *
1967 *
1968 * Function 17: Check the DCD state has changed since the last read
1969 * of this function.
1970 * Syntax:
1971 * int MoxaPortDCDChange(int port);
1972 * int port : port number (0 - 127)
1973 *
1974 * return: 0 : no changed
1975 * 1 : DCD has changed
1976 *
1977 *
1978 * Function 18: Check ths current DCD state is ON or not.
1979 * Syntax:
1980 * int MoxaPortDCDON(int port);
1981 * int port : port number (0 - 127)
1982 *
1983 * return: 0 : DCD off
1984 * 1 : DCD on
1985 *
1986 *
1987 * Function 19: Flush the Rx/Tx buffer data of this port.
1988 * Syntax:
1989 * void MoxaPortFlushData(int port, int mode);
1990 * int port : port number (0 - 127)
1991 * int mode
1992 * 0 : flush the Rx buffer
1993 * 1 : flush the Tx buffer
1994 * 2 : flush the Rx and Tx buffer
1995 *
1996 *
1997 * Function 20: Write data.
1998 * Syntax:
1999 * int MoxaPortWriteData(int port, unsigned char * buffer, int length);
2000 * int port : port number (0 - 127)
2001 * unsigned char * buffer : pointer to write data buffer.
2002 * int length : write data length
2003 *
2004 * return: 0 - length : real write data length
2005 *
2006 *
2007 * Function 21: Read data.
2008 * Syntax:
Alan Cox33f0f882006-01-09 20:54:13 -08002009 * int MoxaPortReadData(int port, struct tty_struct *tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 * int port : port number (0 - 127)
Alan Cox33f0f882006-01-09 20:54:13 -08002011 * struct tty_struct *tty : tty for data
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 *
2013 * return: 0 - length : real read data length
2014 *
2015 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 * Function 24: Get the Tx buffer current queued data bytes
2017 * Syntax:
2018 * int MoxaPortTxQueue(int port);
2019 * int port : port number (0 - 127)
2020 *
2021 * return: .. : Tx buffer current queued data bytes
2022 *
2023 *
2024 * Function 25: Get the Tx buffer current free space
2025 * Syntax:
2026 * int MoxaPortTxFree(int port);
2027 * int port : port number (0 - 127)
2028 *
2029 * return: .. : Tx buffer current free space
2030 *
2031 *
2032 * Function 26: Get the Rx buffer current queued data bytes
2033 * Syntax:
2034 * int MoxaPortRxQueue(int port);
2035 * int port : port number (0 - 127)
2036 *
2037 * return: .. : Rx buffer current queued data bytes
2038 *
2039 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 * Function 28: Disable port data transmission.
2041 * Syntax:
2042 * void MoxaPortTxDisable(int port);
2043 * int port : port number (0 - 127)
2044 *
2045 *
2046 * Function 29: Enable port data transmission.
2047 * Syntax:
2048 * void MoxaPortTxEnable(int port);
2049 * int port : port number (0 - 127)
2050 *
2051 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 * Function 31: Get the received BREAK signal count and reset it.
2053 * Syntax:
2054 * int MoxaPortResetBrkCnt(int port);
2055 * int port : port number (0 - 127)
2056 *
2057 * return: 0 - .. : BREAK signal count
2058 *
2059 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 * Function 34: Send out a BREAK signal.
2061 * Syntax:
2062 * void MoxaPortSendBreak(int port, int ms100);
2063 * int port : port number (0 - 127)
2064 * int ms100 : break signal time interval.
2065 * unit: 100 mini-second. if ms100 == 0, it will
2066 * send out a about 250 ms BREAK signal.
2067 *
2068 */
2069int MoxaPortIsValid(int port)
2070{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 if (moxaCard == 0)
2072 return (0);
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002073 if (moxa_ports[port].chkPort == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 return (0);
2075 return (1);
2076}
2077
2078void MoxaPortEnable(int port)
2079{
2080 void __iomem *ofsAddr;
2081 int MoxaPortLineStatus(int);
2082 short lowwater = 512;
2083
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002084 ofsAddr = moxa_ports[port].tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 writew(lowwater, ofsAddr + Low_water);
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002086 moxa_ports[port].breakCnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
2088 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
2089 moxafunc(ofsAddr, FC_SetBreakIrq, 0);
2090 } else {
2091 writew(readw(ofsAddr + HostStat) | WakeupBreak, ofsAddr + HostStat);
2092 }
2093
2094 moxafunc(ofsAddr, FC_SetLineIrq, Magic_code);
2095 moxafunc(ofsAddr, FC_FlushQueue, 2);
2096
2097 moxafunc(ofsAddr, FC_EnableCH, Magic_code);
2098 MoxaPortLineStatus(port);
2099}
2100
2101void MoxaPortDisable(int port)
2102{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002103 void __iomem *ofsAddr = moxa_ports[port].tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104
2105 moxafunc(ofsAddr, FC_SetFlowCtl, 0); /* disable flow control */
2106 moxafunc(ofsAddr, FC_ClrLineIrq, Magic_code);
2107 writew(0, ofsAddr + HostStat);
2108 moxafunc(ofsAddr, FC_DisableCH, Magic_code);
2109}
2110
2111long MoxaPortGetMaxBaud(int port)
2112{
2113 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
2114 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI))
2115 return (460800L);
2116 else
2117 return (921600L);
2118}
2119
2120
2121long MoxaPortSetBaud(int port, long baud)
2122{
2123 void __iomem *ofsAddr;
2124 long max, clock;
2125 unsigned int val;
2126
2127 if ((baud < 50L) || ((max = MoxaPortGetMaxBaud(port)) == 0))
2128 return (0);
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002129 ofsAddr = moxa_ports[port].tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 if (baud > max)
2131 baud = max;
2132 if (max == 38400L)
2133 clock = 614400L; /* for 9.8304 Mhz : max. 38400 bps */
2134 else if (max == 57600L)
2135 clock = 691200L; /* for 11.0592 Mhz : max. 57600 bps */
2136 else
2137 clock = 921600L; /* for 14.7456 Mhz : max. 115200 bps */
2138 val = clock / baud;
2139 moxafunc(ofsAddr, FC_SetBaud, val);
2140 baud = clock / val;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002141 moxa_ports[port].curBaud = baud;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 return (baud);
2143}
2144
Alan Cox606d0992006-12-08 02:38:45 -08002145int MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146{
2147 void __iomem *ofsAddr;
2148 tcflag_t cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 tcflag_t mode = 0;
2150
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002151 if (moxa_ports[port].chkPort == 0 || termio == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 return (-1);
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002153 ofsAddr = moxa_ports[port].tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 cflag = termio->c_cflag; /* termio->c_cflag */
2155
2156 mode = termio->c_cflag & CSIZE;
2157 if (mode == CS5)
2158 mode = MX_CS5;
2159 else if (mode == CS6)
2160 mode = MX_CS6;
2161 else if (mode == CS7)
2162 mode = MX_CS7;
2163 else if (mode == CS8)
2164 mode = MX_CS8;
2165
2166 if (termio->c_cflag & CSTOPB) {
2167 if (mode == MX_CS5)
2168 mode |= MX_STOP15;
2169 else
2170 mode |= MX_STOP2;
2171 } else
2172 mode |= MX_STOP1;
2173
2174 if (termio->c_cflag & PARENB) {
2175 if (termio->c_cflag & PARODD)
2176 mode |= MX_PARODD;
2177 else
2178 mode |= MX_PAREVEN;
2179 } else
2180 mode |= MX_PARNONE;
2181
2182 moxafunc(ofsAddr, FC_SetDataMode, (ushort) mode);
2183
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
2185 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
Alan Coxc7bce302006-09-30 23:27:24 -07002186 if (baud >= 921600L)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 return (-1);
2188 }
Alan Coxdb1acaa2008-02-08 04:18:43 -08002189 baud = MoxaPortSetBaud(port, baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190
2191 if (termio->c_iflag & (IXON | IXOFF | IXANY)) {
2192 writeb(termio->c_cc[VSTART], ofsAddr + FuncArg);
2193 writeb(termio->c_cc[VSTOP], ofsAddr + FuncArg1);
2194 writeb(FC_SetXonXoff, ofsAddr + FuncCode);
Jiri Slaby6f56b6582007-10-18 03:06:24 -07002195 moxa_wait_finish(ofsAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196
2197 }
Alan Coxdb1acaa2008-02-08 04:18:43 -08002198 return (baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199}
2200
2201int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState)
2202{
2203
2204 if (!MoxaPortIsValid(port))
2205 return (-1);
2206 if (dtrState) {
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002207 if (moxa_ports[port].lineCtrl & DTR_ON)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 *dtrState = 1;
2209 else
2210 *dtrState = 0;
2211 }
2212 if (rtsState) {
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002213 if (moxa_ports[port].lineCtrl & RTS_ON)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 *rtsState = 1;
2215 else
2216 *rtsState = 0;
2217 }
2218 return (0);
2219}
2220
2221void MoxaPortLineCtrl(int port, int dtr, int rts)
2222{
2223 void __iomem *ofsAddr;
2224 int mode;
2225
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002226 ofsAddr = moxa_ports[port].tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 mode = 0;
2228 if (dtr)
2229 mode |= DTR_ON;
2230 if (rts)
2231 mode |= RTS_ON;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002232 moxa_ports[port].lineCtrl = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 moxafunc(ofsAddr, FC_LineControl, mode);
2234}
2235
2236void MoxaPortFlowCtrl(int port, int rts, int cts, int txflow, int rxflow, int txany)
2237{
2238 void __iomem *ofsAddr;
2239 int mode;
2240
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002241 ofsAddr = moxa_ports[port].tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 mode = 0;
2243 if (rts)
2244 mode |= RTS_FlowCtl;
2245 if (cts)
2246 mode |= CTS_FlowCtl;
2247 if (txflow)
2248 mode |= Tx_FlowCtl;
2249 if (rxflow)
2250 mode |= Rx_FlowCtl;
2251 if (txany)
2252 mode |= IXM_IXANY;
2253 moxafunc(ofsAddr, FC_SetFlowCtl, mode);
2254}
2255
2256int MoxaPortLineStatus(int port)
2257{
2258 void __iomem *ofsAddr;
2259 int val;
2260
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002261 ofsAddr = moxa_ports[port].tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
2263 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
2264 moxafunc(ofsAddr, FC_LineStatus, 0);
2265 val = readw(ofsAddr + FuncArg);
2266 } else {
2267 val = readw(ofsAddr + FlagStat) >> 4;
2268 }
2269 val &= 0x0B;
2270 if (val & 8) {
2271 val |= 4;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002272 if ((moxa_ports[port].DCDState & DCD_oldstate) == 0)
2273 moxa_ports[port].DCDState = (DCD_oldstate | DCD_changed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 } else {
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002275 if (moxa_ports[port].DCDState & DCD_oldstate)
2276 moxa_ports[port].DCDState = DCD_changed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 }
2278 val &= 7;
2279 return (val);
2280}
2281
2282int MoxaPortDCDChange(int port)
2283{
2284 int n;
2285
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002286 if (moxa_ports[port].chkPort == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 return (0);
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002288 n = moxa_ports[port].DCDState;
2289 moxa_ports[port].DCDState &= ~DCD_changed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 n &= DCD_changed;
2291 return (n);
2292}
2293
2294int MoxaPortDCDON(int port)
2295{
2296 int n;
2297
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002298 if (moxa_ports[port].chkPort == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 return (0);
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002300 if (moxa_ports[port].DCDState & DCD_oldstate)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 n = 1;
2302 else
2303 n = 0;
2304 return (n);
2305}
2306
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307int MoxaPortWriteData(int port, unsigned char * buffer, int len)
2308{
2309 int c, total, i;
2310 ushort tail;
2311 int cnt;
2312 ushort head, tx_mask, spage, epage;
2313 ushort pageno, pageofs, bufhead;
2314 void __iomem *baseAddr, *ofsAddr, *ofs;
2315
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002316 ofsAddr = moxa_ports[port].tableAddr;
2317 baseAddr = moxa_boards[port / MAX_PORTS_PER_BOARD].basemem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 tx_mask = readw(ofsAddr + TX_mask);
2319 spage = readw(ofsAddr + Page_txb);
2320 epage = readw(ofsAddr + EndPage_txb);
2321 tail = readw(ofsAddr + TXwptr);
2322 head = readw(ofsAddr + TXrptr);
2323 c = (head > tail) ? (head - tail - 1)
2324 : (head - tail + tx_mask);
2325 if (c > len)
2326 c = len;
2327 moxaLog.txcnt[port] += c;
2328 total = c;
2329 if (spage == epage) {
2330 bufhead = readw(ofsAddr + Ofs_txb);
2331 writew(spage, baseAddr + Control_reg);
2332 while (c > 0) {
2333 if (head > tail)
2334 len = head - tail - 1;
2335 else
2336 len = tx_mask + 1 - tail;
2337 len = (c > len) ? len : c;
2338 ofs = baseAddr + DynPage_addr + bufhead + tail;
2339 for (i = 0; i < len; i++)
2340 writeb(*buffer++, ofs + i);
2341 tail = (tail + len) & tx_mask;
2342 c -= len;
2343 }
2344 writew(tail, ofsAddr + TXwptr);
2345 } else {
2346 len = c;
2347 pageno = spage + (tail >> 13);
2348 pageofs = tail & Page_mask;
2349 do {
2350 cnt = Page_size - pageofs;
2351 if (cnt > c)
2352 cnt = c;
2353 c -= cnt;
2354 writeb(pageno, baseAddr + Control_reg);
2355 ofs = baseAddr + DynPage_addr + pageofs;
2356 for (i = 0; i < cnt; i++)
2357 writeb(*buffer++, ofs + i);
2358 if (c == 0) {
2359 writew((tail + len) & tx_mask, ofsAddr + TXwptr);
2360 break;
2361 }
2362 if (++pageno == epage)
2363 pageno = spage;
2364 pageofs = 0;
2365 } while (1);
2366 }
2367 writeb(1, ofsAddr + CD180TXirq); /* start to send */
2368 return (total);
2369}
2370
Alan Cox33f0f882006-01-09 20:54:13 -08002371int MoxaPortReadData(int port, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372{
2373 register ushort head, pageofs;
2374 int i, count, cnt, len, total, remain;
2375 ushort tail, rx_mask, spage, epage;
2376 ushort pageno, bufhead;
2377 void __iomem *baseAddr, *ofsAddr, *ofs;
2378
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002379 ofsAddr = moxa_ports[port].tableAddr;
2380 baseAddr = moxa_boards[port / MAX_PORTS_PER_BOARD].basemem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 head = readw(ofsAddr + RXrptr);
2382 tail = readw(ofsAddr + RXwptr);
2383 rx_mask = readw(ofsAddr + RX_mask);
2384 spage = readw(ofsAddr + Page_rxb);
2385 epage = readw(ofsAddr + EndPage_rxb);
2386 count = (tail >= head) ? (tail - head)
2387 : (tail - head + rx_mask + 1);
2388 if (count == 0)
Alan Cox33f0f882006-01-09 20:54:13 -08002389 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390
Alan Cox33f0f882006-01-09 20:54:13 -08002391 total = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 remain = count - total;
2393 moxaLog.rxcnt[port] += total;
2394 count = total;
2395 if (spage == epage) {
2396 bufhead = readw(ofsAddr + Ofs_rxb);
2397 writew(spage, baseAddr + Control_reg);
2398 while (count > 0) {
2399 if (tail >= head)
2400 len = tail - head;
2401 else
2402 len = rx_mask + 1 - head;
2403 len = (count > len) ? len : count;
2404 ofs = baseAddr + DynPage_addr + bufhead + head;
2405 for (i = 0; i < len; i++)
Alan Cox33f0f882006-01-09 20:54:13 -08002406 tty_insert_flip_char(tty, readb(ofs + i), TTY_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 head = (head + len) & rx_mask;
2408 count -= len;
2409 }
2410 writew(head, ofsAddr + RXrptr);
2411 } else {
2412 len = count;
2413 pageno = spage + (head >> 13);
2414 pageofs = head & Page_mask;
2415 do {
2416 cnt = Page_size - pageofs;
2417 if (cnt > count)
2418 cnt = count;
2419 count -= cnt;
2420 writew(pageno, baseAddr + Control_reg);
2421 ofs = baseAddr + DynPage_addr + pageofs;
2422 for (i = 0; i < cnt; i++)
Alan Cox33f0f882006-01-09 20:54:13 -08002423 tty_insert_flip_char(tty, readb(ofs + i), TTY_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 if (count == 0) {
2425 writew((head + len) & rx_mask, ofsAddr + RXrptr);
2426 break;
2427 }
2428 if (++pageno == epage)
2429 pageno = spage;
2430 pageofs = 0;
2431 } while (1);
2432 }
2433 if ((readb(ofsAddr + FlagStat) & Xoff_state) && (remain < LowWater)) {
2434 moxaLowWaterChk = 1;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002435 moxa_ports[port].lowChkFlag = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 }
2437 return (total);
2438}
2439
2440
2441int MoxaPortTxQueue(int port)
2442{
2443 void __iomem *ofsAddr;
2444 ushort rptr, wptr, mask;
2445 int len;
2446
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002447 ofsAddr = moxa_ports[port].tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 rptr = readw(ofsAddr + TXrptr);
2449 wptr = readw(ofsAddr + TXwptr);
2450 mask = readw(ofsAddr + TX_mask);
2451 len = (wptr - rptr) & mask;
2452 return (len);
2453}
2454
2455int MoxaPortTxFree(int port)
2456{
2457 void __iomem *ofsAddr;
2458 ushort rptr, wptr, mask;
2459 int len;
2460
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002461 ofsAddr = moxa_ports[port].tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 rptr = readw(ofsAddr + TXrptr);
2463 wptr = readw(ofsAddr + TXwptr);
2464 mask = readw(ofsAddr + TX_mask);
2465 len = mask - ((wptr - rptr) & mask);
2466 return (len);
2467}
2468
2469int MoxaPortRxQueue(int port)
2470{
2471 void __iomem *ofsAddr;
2472 ushort rptr, wptr, mask;
2473 int len;
2474
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002475 ofsAddr = moxa_ports[port].tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 rptr = readw(ofsAddr + RXrptr);
2477 wptr = readw(ofsAddr + RXwptr);
2478 mask = readw(ofsAddr + RX_mask);
2479 len = (wptr - rptr) & mask;
2480 return (len);
2481}
2482
2483
2484void MoxaPortTxDisable(int port)
2485{
2486 void __iomem *ofsAddr;
2487
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002488 ofsAddr = moxa_ports[port].tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 moxafunc(ofsAddr, FC_SetXoffState, Magic_code);
2490}
2491
2492void MoxaPortTxEnable(int port)
2493{
2494 void __iomem *ofsAddr;
2495
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002496 ofsAddr = moxa_ports[port].tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 moxafunc(ofsAddr, FC_SetXonState, Magic_code);
2498}
2499
2500
2501int MoxaPortResetBrkCnt(int port)
2502{
2503 ushort cnt;
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002504 cnt = moxa_ports[port].breakCnt;
2505 moxa_ports[port].breakCnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 return (cnt);
2507}
2508
2509
2510void MoxaPortSendBreak(int port, int ms100)
2511{
2512 void __iomem *ofsAddr;
2513
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002514 ofsAddr = moxa_ports[port].tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 if (ms100) {
2516 moxafunc(ofsAddr, FC_SendBreak, Magic_code);
Jiri Slaby24c032f2007-07-17 04:05:19 -07002517 msleep(ms100 * 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 } else {
2519 moxafunc(ofsAddr, FC_SendBreak, Magic_code);
Jiri Slaby24c032f2007-07-17 04:05:19 -07002520 msleep(250);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 }
2522 moxafunc(ofsAddr, FC_StopBreak, Magic_code);
2523}
2524
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002525static int moxa_get_serial_info(struct moxa_port *info,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 struct serial_struct __user *retinfo)
2527{
2528 struct serial_struct tmp;
2529
2530 memset(&tmp, 0, sizeof(tmp));
2531 tmp.type = info->type;
2532 tmp.line = info->port;
2533 tmp.port = 0;
2534 tmp.irq = 0;
2535 tmp.flags = info->asyncflags;
2536 tmp.baud_base = 921600;
2537 tmp.close_delay = info->close_delay;
2538 tmp.closing_wait = info->closing_wait;
2539 tmp.custom_divisor = 0;
2540 tmp.hub6 = 0;
2541 if(copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2542 return -EFAULT;
2543 return (0);
2544}
2545
2546
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002547static int moxa_set_serial_info(struct moxa_port *info,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 struct serial_struct __user *new_info)
2549{
2550 struct serial_struct new_serial;
2551
2552 if(copy_from_user(&new_serial, new_info, sizeof(new_serial)))
2553 return -EFAULT;
2554
2555 if ((new_serial.irq != 0) ||
2556 (new_serial.port != 0) ||
2557// (new_serial.type != info->type) ||
2558 (new_serial.custom_divisor != 0) ||
2559 (new_serial.baud_base != 921600))
2560 return (-EPERM);
2561
2562 if (!capable(CAP_SYS_ADMIN)) {
2563 if (((new_serial.flags & ~ASYNC_USR_MASK) !=
2564 (info->asyncflags & ~ASYNC_USR_MASK)))
2565 return (-EPERM);
2566 } else {
2567 info->close_delay = new_serial.close_delay * HZ / 100;
2568 info->closing_wait = new_serial.closing_wait * HZ / 100;
2569 }
2570
2571 new_serial.flags = (new_serial.flags & ~ASYNC_FLAGS);
2572 new_serial.flags |= (info->asyncflags & ASYNC_FLAGS);
2573
2574 if (new_serial.type == PORT_16550A) {
2575 MoxaSetFifo(info->port, 1);
2576 } else {
2577 MoxaSetFifo(info->port, 0);
2578 }
2579
2580 info->type = new_serial.type;
2581 return (0);
2582}
2583
2584
2585
2586/*****************************************************************************
2587 * Static local functions: *
2588 *****************************************************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589static void moxafunc(void __iomem *ofsAddr, int cmd, ushort arg)
2590{
2591
2592 writew(arg, ofsAddr + FuncArg);
2593 writew(cmd, ofsAddr + FuncCode);
Jiri Slaby6f56b6582007-10-18 03:06:24 -07002594 moxa_wait_finish(ofsAddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595}
2596
Jiri Slaby6f56b6582007-10-18 03:06:24 -07002597static void moxa_wait_finish(void __iomem *ofsAddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598{
2599 unsigned long i, j;
2600
2601 i = jiffies;
2602 while (readw(ofsAddr + FuncCode) != 0) {
2603 j = jiffies;
2604 if ((j - i) > moxaFuncTout) {
2605 return;
2606 }
2607 }
2608}
2609
Jiri Slaby6f56b6582007-10-18 03:06:24 -07002610static void moxa_low_water_check(void __iomem *ofsAddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611{
2612 int len;
2613 ushort rptr, wptr, mask;
2614
2615 if (readb(ofsAddr + FlagStat) & Xoff_state) {
2616 rptr = readw(ofsAddr + RXrptr);
2617 wptr = readw(ofsAddr + RXwptr);
2618 mask = readw(ofsAddr + RX_mask);
2619 len = (wptr - rptr) & mask;
2620 if (len <= Low_water)
2621 moxafunc(ofsAddr, FC_SendXon, 0);
2622 }
2623}
2624
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625static void MoxaSetFifo(int port, int enable)
2626{
Jiri Slaby8f8ecba2007-02-10 01:45:33 -08002627 void __iomem *ofsAddr = moxa_ports[port].tableAddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628
2629 if (!enable) {
2630 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 0);
2631 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 1);
2632 } else {
2633 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 3);
2634 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 16);
2635 }
2636}