blob: da2a1d1690bf6f02e54322835f95a9ad57e354bf [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>
28#include <linux/signal.h>
29#include <linux/sched.h>
30#include <linux/timer.h>
31#include <linux/interrupt.h>
32#include <linux/tty.h>
33#include <linux/tty_flip.h>
34#include <linux/major.h>
35#include <linux/string.h>
36#include <linux/fcntl.h>
37#include <linux/ptrace.h>
38#include <linux/serial.h>
39#include <linux/tty_driver.h>
40#include <linux/delay.h>
41#include <linux/pci.h>
42#include <linux/init.h>
43#include <linux/bitops.h>
44
45#include <asm/system.h>
46#include <asm/io.h>
47#include <asm/uaccess.h>
48
Jiri Slaby11324ed2007-02-10 01:45:31 -080049#define MOXA_VERSION "5.1k"
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Jiri Slaby11324ed2007-02-10 01:45:31 -080051#define MOXAMAJOR 172
52#define MOXACUMAJOR 173
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Jiri Slaby11324ed2007-02-10 01:45:31 -080054#define MAX_BOARDS 4 /* Don't change this value */
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#define MAX_PORTS_PER_BOARD 32 /* Don't change this value */
Jiri Slaby11324ed2007-02-10 01:45:31 -080056#define MAX_PORTS (MAX_BOARDS * MAX_PORTS_PER_BOARD)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58/*
59 * Define the Moxa PCI vendor and device IDs.
60 */
Jiri Slaby11324ed2007-02-10 01:45:31 -080061#define MOXA_BUS_TYPE_ISA 0
62#define MOXA_BUS_TYPE_PCI 1
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Linus Torvalds1da177e2005-04-16 15:20:36 -070064enum {
65 MOXA_BOARD_C218_PCI = 1,
66 MOXA_BOARD_C218_ISA,
67 MOXA_BOARD_C320_PCI,
68 MOXA_BOARD_C320_ISA,
69 MOXA_BOARD_CP204J,
70};
71
72static char *moxa_brdname[] =
73{
74 "C218 Turbo PCI series",
75 "C218 Turbo ISA series",
76 "C320 Turbo PCI series",
77 "C320 Turbo ISA series",
78 "CP-204J series",
79};
80
81#ifdef CONFIG_PCI
82static struct pci_device_id moxa_pcibrds[] = {
Jiri Slaby5ebb4072007-02-10 01:45:30 -080083 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C218),
84 .driver_data = MOXA_BOARD_C218_PCI },
85 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_C320),
86 .driver_data = MOXA_BOARD_C320_PCI },
87 { PCI_DEVICE(PCI_VENDOR_ID_MOXA, PCI_DEVICE_ID_MOXA_CP204J),
88 .driver_data = MOXA_BOARD_CP204J },
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 { 0 }
90};
91MODULE_DEVICE_TABLE(pci, moxa_pcibrds);
92#endif /* CONFIG_PCI */
93
Jiri Slaby9dff89c2007-02-10 01:45:30 -080094struct moxa_isa_board_conf {
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 int boardType;
96 int numPorts;
97 unsigned long baseAddr;
Jiri Slaby9dff89c2007-02-10 01:45:30 -080098};
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Jiri Slaby9dff89c2007-02-10 01:45:30 -0800100static struct moxa_isa_board_conf moxa_isa_boards[] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101{
102/* {MOXA_BOARD_C218_ISA,8,0xDC000}, */
103};
104
Jiri Slaby9dff89c2007-02-10 01:45:30 -0800105struct moxa_pci_devinfo {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 ushort busNum;
107 ushort devNum;
Jiri Slaby86fbf142006-10-21 10:24:01 -0700108 struct pci_dev *pdev;
Jiri Slaby9dff89c2007-02-10 01:45:30 -0800109};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Jiri Slaby9dff89c2007-02-10 01:45:30 -0800111struct moxa_board_conf {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 int boardType;
113 int numPorts;
114 unsigned long baseAddr;
115 int busType;
Jiri Slaby9dff89c2007-02-10 01:45:30 -0800116 struct moxa_pci_devinfo pciInfo;
117};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Jiri Slaby9dff89c2007-02-10 01:45:30 -0800119static struct moxa_board_conf moxa_boards[MAX_BOARDS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120static void __iomem *moxaBaseAddr[MAX_BOARDS];
Dirk Eibach01cfaf02006-08-27 01:23:36 -0700121static int loadstat[MAX_BOARDS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123struct moxa_str {
124 int type;
125 int port;
126 int close_delay;
127 unsigned short closing_wait;
128 int count;
129 int blocked_open;
130 long event; /* long req'd for set_bit --RR */
131 int asyncflags;
132 unsigned long statusflags;
133 struct tty_struct *tty;
134 int cflag;
135 wait_queue_head_t open_wait;
136 wait_queue_head_t close_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137};
138
139struct mxser_mstatus {
140 tcflag_t cflag;
141 int cts;
142 int dsr;
143 int ri;
144 int dcd;
145};
146
147static struct mxser_mstatus GMStatus[MAX_PORTS];
148
149/* statusflags */
150#define TXSTOPPED 0x1
151#define LOWWAIT 0x2
152#define EMPTYWAIT 0x4
153#define THROTTLE 0x8
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155#define SERIAL_DO_RESTART
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157#define WAKEUP_CHARS 256
158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159static int verbose = 0;
160static int ttymajor = MOXAMAJOR;
161/* Variables for insmod */
162#ifdef MODULE
Jiri Slaby9fa372a2007-02-10 01:45:26 -0800163static int baseaddr[4];
164static int type[4];
165static int numports[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166#endif
167
168MODULE_AUTHOR("William Chen");
169MODULE_DESCRIPTION("MOXA Intellio Family Multiport Board Device Driver");
170MODULE_LICENSE("GPL");
171#ifdef MODULE
172module_param_array(type, int, NULL, 0);
173module_param_array(baseaddr, int, NULL, 0);
174module_param_array(numports, int, NULL, 0);
175#endif
176module_param(ttymajor, int, 0);
177module_param(verbose, bool, 0644);
178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179/*
180 * static functions:
181 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182static int moxa_open(struct tty_struct *, struct file *);
183static void moxa_close(struct tty_struct *, struct file *);
184static int moxa_write(struct tty_struct *, const unsigned char *, int);
185static int moxa_write_room(struct tty_struct *);
186static void moxa_flush_buffer(struct tty_struct *);
187static int moxa_chars_in_buffer(struct tty_struct *);
188static void moxa_flush_chars(struct tty_struct *);
189static void moxa_put_char(struct tty_struct *, unsigned char);
190static int moxa_ioctl(struct tty_struct *, struct file *, unsigned int, unsigned long);
191static void moxa_throttle(struct tty_struct *);
192static void moxa_unthrottle(struct tty_struct *);
Alan Cox606d0992006-12-08 02:38:45 -0800193static void moxa_set_termios(struct tty_struct *, struct ktermios *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194static void moxa_stop(struct tty_struct *);
195static void moxa_start(struct tty_struct *);
196static void moxa_hangup(struct tty_struct *);
197static int moxa_tiocmget(struct tty_struct *tty, struct file *file);
198static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
199 unsigned int set, unsigned int clear);
200static void moxa_poll(unsigned long);
201static void set_tty_param(struct tty_struct *);
202static int block_till_ready(struct tty_struct *, struct file *,
203 struct moxa_str *);
204static void setup_empty_event(struct tty_struct *);
205static void check_xmit_empty(unsigned long);
206static void shut_down(struct moxa_str *);
207static void receive_data(struct moxa_str *);
208/*
209 * moxa board interface functions:
210 */
211static void MoxaDriverInit(void);
212static int MoxaDriverIoctl(unsigned int, unsigned long, int);
213static int MoxaDriverPoll(void);
214static int MoxaPortsOfCard(int);
215static int MoxaPortIsValid(int);
216static void MoxaPortEnable(int);
217static void MoxaPortDisable(int);
218static long MoxaPortGetMaxBaud(int);
219static long MoxaPortSetBaud(int, long);
Alan Cox606d0992006-12-08 02:38:45 -0800220static int MoxaPortSetTermio(int, struct ktermios *, speed_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221static int MoxaPortGetLineOut(int, int *, int *);
222static void MoxaPortLineCtrl(int, int, int);
223static void MoxaPortFlowCtrl(int, int, int, int, int, int);
224static int MoxaPortLineStatus(int);
225static int MoxaPortDCDChange(int);
226static int MoxaPortDCDON(int);
227static void MoxaPortFlushData(int, int);
228static int MoxaPortWriteData(int, unsigned char *, int);
Alan Cox33f0f882006-01-09 20:54:13 -0800229static int MoxaPortReadData(int, struct tty_struct *tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230static int MoxaPortTxQueue(int);
231static int MoxaPortRxQueue(int);
232static int MoxaPortTxFree(int);
233static void MoxaPortTxDisable(int);
234static void MoxaPortTxEnable(int);
235static int MoxaPortResetBrkCnt(int);
236static void MoxaPortSendBreak(int, int);
237static int moxa_get_serial_info(struct moxa_str *, struct serial_struct __user *);
238static int moxa_set_serial_info(struct moxa_str *, struct serial_struct __user *);
239static void MoxaSetFifo(int port, int enable);
240
Jeff Dikeb68e31d2006-10-02 02:17:18 -0700241static const struct tty_operations moxa_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 .open = moxa_open,
243 .close = moxa_close,
244 .write = moxa_write,
245 .write_room = moxa_write_room,
246 .flush_buffer = moxa_flush_buffer,
247 .chars_in_buffer = moxa_chars_in_buffer,
248 .flush_chars = moxa_flush_chars,
249 .put_char = moxa_put_char,
250 .ioctl = moxa_ioctl,
251 .throttle = moxa_throttle,
252 .unthrottle = moxa_unthrottle,
253 .set_termios = moxa_set_termios,
254 .stop = moxa_stop,
255 .start = moxa_start,
256 .hangup = moxa_hangup,
257 .tiocmget = moxa_tiocmget,
258 .tiocmset = moxa_tiocmset,
259};
260
Jiri Slabyaa7e5222007-02-10 01:45:27 -0800261static struct tty_driver *moxaDriver;
262static struct moxa_str moxaChannels[MAX_PORTS];
263static DEFINE_TIMER(moxaTimer, moxa_poll, 0, 0);
264static struct timer_list moxaEmptyTimer[MAX_PORTS];
Ingo Molnar34af9462006-06-27 02:53:55 -0700265static DEFINE_SPINLOCK(moxa_lock);
Alan Cox33f0f882006-01-09 20:54:13 -0800266
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267#ifdef CONFIG_PCI
Jiri Slaby9dff89c2007-02-10 01:45:30 -0800268static int moxa_get_PCI_conf(struct pci_dev *p, int board_type,
269 struct moxa_board_conf *board)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
271 board->baseAddr = pci_resource_start (p, 2);
272 board->boardType = board_type;
273 switch (board_type) {
274 case MOXA_BOARD_C218_ISA:
275 case MOXA_BOARD_C218_PCI:
276 board->numPorts = 8;
277 break;
278
279 case MOXA_BOARD_CP204J:
280 board->numPorts = 4;
281 break;
282 default:
283 board->numPorts = 0;
284 break;
285 }
286 board->busType = MOXA_BUS_TYPE_PCI;
287 board->pciInfo.busNum = p->bus->number;
288 board->pciInfo.devNum = p->devfn >> 3;
Jiri Slaby86fbf142006-10-21 10:24:01 -0700289 board->pciInfo.pdev = p;
290 /* don't lose the reference in the next pci_get_device iteration */
291 pci_dev_get(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
293 return (0);
294}
295#endif /* CONFIG_PCI */
296
297static int __init moxa_init(void)
298{
299 int i, numBoards;
300 struct moxa_str *ch;
301
302 printk(KERN_INFO "MOXA Intellio family driver version %s\n", MOXA_VERSION);
303 moxaDriver = alloc_tty_driver(MAX_PORTS + 1);
304 if (!moxaDriver)
305 return -ENOMEM;
306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 moxaDriver->owner = THIS_MODULE;
Sergey Vlasov9b4e3b12005-09-03 16:26:49 +0100308 moxaDriver->name = "ttyMX";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 moxaDriver->major = ttymajor;
310 moxaDriver->minor_start = 0;
311 moxaDriver->type = TTY_DRIVER_TYPE_SERIAL;
312 moxaDriver->subtype = SERIAL_TYPE_NORMAL;
313 moxaDriver->init_termios = tty_std_termios;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 moxaDriver->init_termios.c_cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
Alan Cox606d0992006-12-08 02:38:45 -0800315 moxaDriver->init_termios.c_ispeed = 9600;
316 moxaDriver->init_termios.c_ospeed = 9600;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 moxaDriver->flags = TTY_DRIVER_REAL_RAW;
318 tty_set_operations(moxaDriver, &moxa_ops);
319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 for (i = 0, ch = moxaChannels; i < MAX_PORTS; i++, ch++) {
321 ch->type = PORT_16550A;
322 ch->port = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 ch->close_delay = 5 * HZ / 10;
324 ch->closing_wait = 30 * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 ch->cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
326 init_waitqueue_head(&ch->open_wait);
327 init_waitqueue_head(&ch->close_wait);
328 }
329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 printk("Tty devices major number = %d\n", ttymajor);
331
332 if (tty_register_driver(moxaDriver)) {
333 printk(KERN_ERR "Couldn't install MOXA Smartio family driver !\n");
334 put_tty_driver(moxaDriver);
335 return -1;
336 }
Jiri Slabyaa7e5222007-02-10 01:45:27 -0800337 for (i = 0; i < MAX_PORTS; i++)
338 setup_timer(&moxaEmptyTimer[i], check_xmit_empty,
339 (unsigned long)&moxaChannels[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Jiri Slabyaa7e5222007-02-10 01:45:27 -0800341 mod_timer(&moxaTimer, jiffies + HZ / 50);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
343 /* Find the boards defined in source code */
344 numBoards = 0;
345 for (i = 0; i < MAX_BOARDS; i++) {
346 if ((moxa_isa_boards[i].boardType == MOXA_BOARD_C218_ISA) ||
347 (moxa_isa_boards[i].boardType == MOXA_BOARD_C320_ISA)) {
348 moxa_boards[numBoards].boardType = moxa_isa_boards[i].boardType;
349 if (moxa_isa_boards[i].boardType == MOXA_BOARD_C218_ISA)
350 moxa_boards[numBoards].numPorts = 8;
351 else
352 moxa_boards[numBoards].numPorts = moxa_isa_boards[i].numPorts;
353 moxa_boards[numBoards].busType = MOXA_BUS_TYPE_ISA;
354 moxa_boards[numBoards].baseAddr = moxa_isa_boards[i].baseAddr;
355 if (verbose)
356 printk("Board %2d: %s board(baseAddr=%lx)\n",
357 numBoards + 1,
358 moxa_brdname[moxa_boards[numBoards].boardType - 1],
359 moxa_boards[numBoards].baseAddr);
360 numBoards++;
361 }
362 }
363 /* Find the boards defined form module args. */
364#ifdef MODULE
365 for (i = 0; i < MAX_BOARDS; i++) {
366 if ((type[i] == MOXA_BOARD_C218_ISA) ||
367 (type[i] == MOXA_BOARD_C320_ISA)) {
368 if (verbose)
369 printk("Board %2d: %s board(baseAddr=%lx)\n",
370 numBoards + 1,
371 moxa_brdname[type[i] - 1],
372 (unsigned long) baseaddr[i]);
373 if (numBoards >= MAX_BOARDS) {
374 if (verbose)
375 printk("More than %d MOXA Intellio family boards found. Board is ignored.", MAX_BOARDS);
376 continue;
377 }
378 moxa_boards[numBoards].boardType = type[i];
379 if (moxa_isa_boards[i].boardType == MOXA_BOARD_C218_ISA)
380 moxa_boards[numBoards].numPorts = 8;
381 else
382 moxa_boards[numBoards].numPorts = numports[i];
383 moxa_boards[numBoards].busType = MOXA_BUS_TYPE_ISA;
384 moxa_boards[numBoards].baseAddr = baseaddr[i];
385 numBoards++;
386 }
387 }
388#endif
389 /* Find PCI boards here */
390#ifdef CONFIG_PCI
391 {
392 struct pci_dev *p = NULL;
Tobias Klauserfe971072006-01-09 20:54:02 -0800393 int n = ARRAY_SIZE(moxa_pcibrds) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 i = 0;
395 while (i < n) {
Amit Gud881a8c12005-04-12 19:03:33 +0530396 while ((p = pci_get_device(moxa_pcibrds[i].vendor, moxa_pcibrds[i].device, p))!=NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 {
398 if (pci_enable_device(p))
399 continue;
400 if (numBoards >= MAX_BOARDS) {
401 if (verbose)
402 printk("More than %d MOXA Intellio family boards found. Board is ignored.", MAX_BOARDS);
403 } else {
404 moxa_get_PCI_conf(p, moxa_pcibrds[i].driver_data,
405 &moxa_boards[numBoards]);
406 numBoards++;
407 }
408 }
409 i++;
410 }
411 }
412#endif
413 for (i = 0; i < numBoards; i++) {
414 moxaBaseAddr[i] = ioremap((unsigned long) moxa_boards[i].baseAddr, 0x4000);
415 }
416
417 return (0);
418}
419
420static void __exit moxa_exit(void)
421{
422 int i;
423
424 if (verbose)
425 printk("Unloading module moxa ...\n");
426
Jiri Slabyaa7e5222007-02-10 01:45:27 -0800427 del_timer(&moxaTimer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429 for (i = 0; i < MAX_PORTS; i++)
Jiri Slabyaa7e5222007-02-10 01:45:27 -0800430 del_timer(&moxaEmptyTimer[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432 if (tty_unregister_driver(moxaDriver))
433 printk("Couldn't unregister MOXA Intellio family serial driver\n");
434 put_tty_driver(moxaDriver);
Jiri Slaby86fbf142006-10-21 10:24:01 -0700435
Amol Lad41bdabb2006-12-06 20:35:21 -0800436 for (i = 0; i < MAX_BOARDS; i++) {
437 if (moxaBaseAddr[i])
438 iounmap(moxaBaseAddr[i]);
Jiri Slaby86fbf142006-10-21 10:24:01 -0700439 if (moxa_boards[i].busType == MOXA_BUS_TYPE_PCI)
440 pci_dev_put(moxa_boards[i].pciInfo.pdev);
Amol Lad41bdabb2006-12-06 20:35:21 -0800441 }
Jiri Slaby86fbf142006-10-21 10:24:01 -0700442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 if (verbose)
444 printk("Done\n");
445}
446
447module_init(moxa_init);
448module_exit(moxa_exit);
449
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450static int moxa_open(struct tty_struct *tty, struct file *filp)
451{
452 struct moxa_str *ch;
453 int port;
454 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Jiri Slaby11324ed2007-02-10 01:45:31 -0800456 port = tty->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 if (port == MAX_PORTS) {
458 return (0);
459 }
460 if (!MoxaPortIsValid(port)) {
461 tty->driver_data = NULL;
462 return (-ENODEV);
463 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
465 ch = &moxaChannels[port];
466 ch->count++;
467 tty->driver_data = ch;
468 ch->tty = tty;
469 if (!(ch->asyncflags & ASYNC_INITIALIZED)) {
470 ch->statusflags = 0;
471 set_tty_param(tty);
472 MoxaPortLineCtrl(ch->port, 1, 1);
473 MoxaPortEnable(ch->port);
474 ch->asyncflags |= ASYNC_INITIALIZED;
475 }
476 retval = block_till_ready(tty, filp, ch);
477
478 moxa_unthrottle(tty);
479
480 if (ch->type == PORT_16550A) {
481 MoxaSetFifo(ch->port, 1);
482 } else {
483 MoxaSetFifo(ch->port, 0);
484 }
485
486 return (retval);
487}
488
489static void moxa_close(struct tty_struct *tty, struct file *filp)
490{
491 struct moxa_str *ch;
492 int port;
493
Jiri Slaby11324ed2007-02-10 01:45:31 -0800494 port = tty->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 if (port == MAX_PORTS) {
496 return;
497 }
498 if (!MoxaPortIsValid(port)) {
499#ifdef SERIAL_DEBUG_CLOSE
500 printk("Invalid portno in moxa_close\n");
501#endif
502 tty->driver_data = NULL;
503 return;
504 }
505 if (tty->driver_data == NULL) {
506 return;
507 }
508 if (tty_hung_up_p(filp)) {
509 return;
510 }
511 ch = (struct moxa_str *) tty->driver_data;
512
513 if ((tty->count == 1) && (ch->count != 1)) {
514 printk("moxa_close: bad serial port count; tty->count is 1, "
515 "ch->count is %d\n", ch->count);
516 ch->count = 1;
517 }
518 if (--ch->count < 0) {
519 printk("moxa_close: bad serial port count, device=%s\n",
520 tty->name);
521 ch->count = 0;
522 }
523 if (ch->count) {
524 return;
525 }
526 ch->asyncflags |= ASYNC_CLOSING;
527
528 ch->cflag = tty->termios->c_cflag;
529 if (ch->asyncflags & ASYNC_INITIALIZED) {
530 setup_empty_event(tty);
531 tty_wait_until_sent(tty, 30 * HZ); /* 30 seconds timeout */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 del_timer(&moxaEmptyTimer[ch->port]);
533 }
534 shut_down(ch);
535 MoxaPortFlushData(port, 2);
536
537 if (tty->driver->flush_buffer)
538 tty->driver->flush_buffer(tty);
539 tty_ldisc_flush(tty);
540
541 tty->closing = 0;
542 ch->event = 0;
543 ch->tty = NULL;
544 if (ch->blocked_open) {
545 if (ch->close_delay) {
546 msleep_interruptible(jiffies_to_msecs(ch->close_delay));
547 }
548 wake_up_interruptible(&ch->open_wait);
549 }
550 ch->asyncflags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
551 wake_up_interruptible(&ch->close_wait);
552}
553
554static int moxa_write(struct tty_struct *tty,
555 const unsigned char *buf, int count)
556{
557 struct moxa_str *ch;
558 int len, port;
559 unsigned long flags;
560
561 ch = (struct moxa_str *) tty->driver_data;
562 if (ch == NULL)
563 return (0);
564 port = ch->port;
Alan Cox33f0f882006-01-09 20:54:13 -0800565
566 spin_lock_irqsave(&moxa_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 len = MoxaPortWriteData(port, (unsigned char *) buf, count);
Alan Cox33f0f882006-01-09 20:54:13 -0800568 spin_unlock_irqrestore(&moxa_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
570 /*********************************************
571 if ( !(ch->statusflags & LOWWAIT) &&
572 ((len != count) || (MoxaPortTxFree(port) <= 100)) )
573 ************************************************/
574 ch->statusflags |= LOWWAIT;
575 return (len);
576}
577
578static int moxa_write_room(struct tty_struct *tty)
579{
580 struct moxa_str *ch;
581
582 if (tty->stopped)
583 return (0);
584 ch = (struct moxa_str *) tty->driver_data;
585 if (ch == NULL)
586 return (0);
587 return (MoxaPortTxFree(ch->port));
588}
589
590static void moxa_flush_buffer(struct tty_struct *tty)
591{
592 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
593
594 if (ch == NULL)
595 return;
596 MoxaPortFlushData(ch->port, 1);
597 tty_wakeup(tty);
598}
599
600static int moxa_chars_in_buffer(struct tty_struct *tty)
601{
602 int chars;
603 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
604
605 /*
606 * Sigh...I have to check if driver_data is NULL here, because
607 * if an open() fails, the TTY subsystem eventually calls
608 * tty_wait_until_sent(), which calls the driver's chars_in_buffer()
609 * routine. And since the open() failed, we return 0 here. TDJ
610 */
611 if (ch == NULL)
612 return (0);
613 chars = MoxaPortTxQueue(ch->port);
614 if (chars) {
615 /*
616 * Make it possible to wakeup anything waiting for output
617 * in tty_ioctl.c, etc.
618 */
619 if (!(ch->statusflags & EMPTYWAIT))
620 setup_empty_event(tty);
621 }
622 return (chars);
623}
624
625static void moxa_flush_chars(struct tty_struct *tty)
626{
627 /*
628 * Don't think I need this, because this is called to empty the TX
629 * buffer for the 16450, 16550, etc.
630 */
631}
632
633static void moxa_put_char(struct tty_struct *tty, unsigned char c)
634{
635 struct moxa_str *ch;
636 int port;
637 unsigned long flags;
638
639 ch = (struct moxa_str *) tty->driver_data;
640 if (ch == NULL)
641 return;
642 port = ch->port;
Alan Cox33f0f882006-01-09 20:54:13 -0800643 spin_lock_irqsave(&moxa_lock, flags);
Jiri Slabyf204d262007-02-10 01:45:25 -0800644 MoxaPortWriteData(port, &c, 1);
Alan Cox33f0f882006-01-09 20:54:13 -0800645 spin_unlock_irqrestore(&moxa_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 /************************************************
647 if ( !(ch->statusflags & LOWWAIT) && (MoxaPortTxFree(port) <= 100) )
648 *************************************************/
649 ch->statusflags |= LOWWAIT;
650}
651
652static int moxa_tiocmget(struct tty_struct *tty, struct file *file)
653{
654 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
655 int port;
656 int flag = 0, dtr, rts;
657
Jiri Slaby11324ed2007-02-10 01:45:31 -0800658 port = tty->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 if ((port != MAX_PORTS) && (!ch))
660 return (-EINVAL);
661
662 MoxaPortGetLineOut(ch->port, &dtr, &rts);
663 if (dtr)
664 flag |= TIOCM_DTR;
665 if (rts)
666 flag |= TIOCM_RTS;
667 dtr = MoxaPortLineStatus(ch->port);
668 if (dtr & 1)
669 flag |= TIOCM_CTS;
670 if (dtr & 2)
671 flag |= TIOCM_DSR;
672 if (dtr & 4)
673 flag |= TIOCM_CD;
674 return flag;
675}
676
677static int moxa_tiocmset(struct tty_struct *tty, struct file *file,
678 unsigned int set, unsigned int clear)
679{
680 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
681 int port;
682 int dtr, rts;
683
Jiri Slaby11324ed2007-02-10 01:45:31 -0800684 port = tty->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 if ((port != MAX_PORTS) && (!ch))
686 return (-EINVAL);
687
688 MoxaPortGetLineOut(ch->port, &dtr, &rts);
689 if (set & TIOCM_RTS)
690 rts = 1;
691 if (set & TIOCM_DTR)
692 dtr = 1;
693 if (clear & TIOCM_RTS)
694 rts = 0;
695 if (clear & TIOCM_DTR)
696 dtr = 0;
697 MoxaPortLineCtrl(ch->port, dtr, rts);
698 return 0;
699}
700
701static int moxa_ioctl(struct tty_struct *tty, struct file *file,
702 unsigned int cmd, unsigned long arg)
703{
704 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
705 register int port;
706 void __user *argp = (void __user *)arg;
707 int retval;
708
Jiri Slaby11324ed2007-02-10 01:45:31 -0800709 port = tty->index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 if ((port != MAX_PORTS) && (!ch))
711 return (-EINVAL);
712
713 switch (cmd) {
714 case TCSBRK: /* SVID version: non-zero arg --> no break */
715 retval = tty_check_change(tty);
716 if (retval)
717 return (retval);
718 setup_empty_event(tty);
719 tty_wait_until_sent(tty, 0);
720 if (!arg)
721 MoxaPortSendBreak(ch->port, 0);
722 return (0);
723 case TCSBRKP: /* support for POSIX tcsendbreak() */
724 retval = tty_check_change(tty);
725 if (retval)
726 return (retval);
727 setup_empty_event(tty);
728 tty_wait_until_sent(tty, 0);
729 MoxaPortSendBreak(ch->port, arg);
730 return (0);
731 case TIOCGSOFTCAR:
732 return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long __user *) argp);
733 case TIOCSSOFTCAR:
734 if(get_user(retval, (unsigned long __user *) argp))
735 return -EFAULT;
736 arg = retval;
737 tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) |
738 (arg ? CLOCAL : 0));
739 if (C_CLOCAL(tty))
740 ch->asyncflags &= ~ASYNC_CHECK_CD;
741 else
742 ch->asyncflags |= ASYNC_CHECK_CD;
743 return (0);
744 case TIOCGSERIAL:
745 return moxa_get_serial_info(ch, argp);
746
747 case TIOCSSERIAL:
748 return moxa_set_serial_info(ch, argp);
749 default:
750 retval = MoxaDriverIoctl(cmd, arg, port);
751 }
752 return (retval);
753}
754
755static void moxa_throttle(struct tty_struct *tty)
756{
757 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
758
759 ch->statusflags |= THROTTLE;
760}
761
762static void moxa_unthrottle(struct tty_struct *tty)
763{
764 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
765
766 ch->statusflags &= ~THROTTLE;
767}
768
769static void moxa_set_termios(struct tty_struct *tty,
Alan Cox606d0992006-12-08 02:38:45 -0800770 struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771{
772 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
773
774 if (ch == NULL)
775 return;
776 set_tty_param(tty);
777 if (!(old_termios->c_cflag & CLOCAL) &&
778 (tty->termios->c_cflag & CLOCAL))
779 wake_up_interruptible(&ch->open_wait);
780}
781
782static void moxa_stop(struct tty_struct *tty)
783{
784 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
785
786 if (ch == NULL)
787 return;
788 MoxaPortTxDisable(ch->port);
789 ch->statusflags |= TXSTOPPED;
790}
791
792
793static void moxa_start(struct tty_struct *tty)
794{
795 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
796
797 if (ch == NULL)
798 return;
799
800 if (!(ch->statusflags & TXSTOPPED))
801 return;
802
803 MoxaPortTxEnable(ch->port);
804 ch->statusflags &= ~TXSTOPPED;
805}
806
807static void moxa_hangup(struct tty_struct *tty)
808{
809 struct moxa_str *ch = (struct moxa_str *) tty->driver_data;
810
811 moxa_flush_buffer(tty);
812 shut_down(ch);
813 ch->event = 0;
814 ch->count = 0;
815 ch->asyncflags &= ~ASYNC_NORMAL_ACTIVE;
816 ch->tty = NULL;
817 wake_up_interruptible(&ch->open_wait);
818}
819
820static void moxa_poll(unsigned long ignored)
821{
822 register int card;
823 struct moxa_str *ch;
824 struct tty_struct *tp;
825 int i, ports;
826
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 del_timer(&moxaTimer);
828
829 if (MoxaDriverPoll() < 0) {
Jiri Slabyaa7e5222007-02-10 01:45:27 -0800830 mod_timer(&moxaTimer, jiffies + HZ / 50);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 return;
832 }
833 for (card = 0; card < MAX_BOARDS; card++) {
834 if ((ports = MoxaPortsOfCard(card)) <= 0)
835 continue;
836 ch = &moxaChannels[card * MAX_PORTS_PER_BOARD];
837 for (i = 0; i < ports; i++, ch++) {
838 if ((ch->asyncflags & ASYNC_INITIALIZED) == 0)
839 continue;
840 if (!(ch->statusflags & THROTTLE) &&
841 (MoxaPortRxQueue(ch->port) > 0))
842 receive_data(ch);
843 if ((tp = ch->tty) == 0)
844 continue;
845 if (ch->statusflags & LOWWAIT) {
846 if (MoxaPortTxQueue(ch->port) <= WAKEUP_CHARS) {
847 if (!tp->stopped) {
848 ch->statusflags &= ~LOWWAIT;
849 tty_wakeup(tp);
850 }
851 }
852 }
853 if (!I_IGNBRK(tp) && (MoxaPortResetBrkCnt(ch->port) > 0)) {
854 tty_insert_flip_char(tp, 0, TTY_BREAK);
855 tty_schedule_flip(tp);
856 }
857 if (MoxaPortDCDChange(ch->port)) {
858 if (ch->asyncflags & ASYNC_CHECK_CD) {
859 if (MoxaPortDCDON(ch->port))
860 wake_up_interruptible(&ch->open_wait);
861 else {
Jiri Slabyba196df2007-02-10 01:45:28 -0800862 tty_hangup(tp);
863 wake_up_interruptible(&ch->open_wait);
864 ch->asyncflags &= ~ASYNC_NORMAL_ACTIVE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 }
866 }
867 }
868 }
869 }
870
Jiri Slabyaa7e5222007-02-10 01:45:27 -0800871 mod_timer(&moxaTimer, jiffies + HZ / 50);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872}
873
874/******************************************************************************/
875
876static void set_tty_param(struct tty_struct *tty)
877{
Alan Cox606d0992006-12-08 02:38:45 -0800878 register struct ktermios *ts;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 struct moxa_str *ch;
880 int rts, cts, txflow, rxflow, xany;
881
882 ch = (struct moxa_str *) tty->driver_data;
883 ts = tty->termios;
884 if (ts->c_cflag & CLOCAL)
885 ch->asyncflags &= ~ASYNC_CHECK_CD;
886 else
887 ch->asyncflags |= ASYNC_CHECK_CD;
888 rts = cts = txflow = rxflow = xany = 0;
889 if (ts->c_cflag & CRTSCTS)
890 rts = cts = 1;
891 if (ts->c_iflag & IXON)
892 txflow = 1;
893 if (ts->c_iflag & IXOFF)
894 rxflow = 1;
895 if (ts->c_iflag & IXANY)
896 xany = 1;
897 MoxaPortFlowCtrl(ch->port, rts, cts, txflow, rxflow, xany);
Alan Coxc7bce302006-09-30 23:27:24 -0700898 MoxaPortSetTermio(ch->port, ts, tty_get_baud_rate(tty));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899}
900
901static int block_till_ready(struct tty_struct *tty, struct file *filp,
902 struct moxa_str *ch)
903{
904 DECLARE_WAITQUEUE(wait,current);
905 unsigned long flags;
906 int retval;
907 int do_clocal = C_CLOCAL(tty);
908
909 /*
910 * If the device is in the middle of being closed, then block
911 * until it's done, and then try again.
912 */
913 if (tty_hung_up_p(filp) || (ch->asyncflags & ASYNC_CLOSING)) {
914 if (ch->asyncflags & ASYNC_CLOSING)
915 interruptible_sleep_on(&ch->close_wait);
916#ifdef SERIAL_DO_RESTART
917 if (ch->asyncflags & ASYNC_HUP_NOTIFY)
918 return (-EAGAIN);
919 else
920 return (-ERESTARTSYS);
921#else
922 return (-EAGAIN);
923#endif
924 }
925 /*
926 * If non-blocking mode is set, then make the check up front
927 * and then exit.
928 */
929 if (filp->f_flags & O_NONBLOCK) {
930 ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
931 return (0);
932 }
933 /*
934 * Block waiting for the carrier detect and the line to become free
935 */
936 retval = 0;
937 add_wait_queue(&ch->open_wait, &wait);
938#ifdef SERIAL_DEBUG_OPEN
939 printk("block_til_ready before block: ttys%d, count = %d\n",
940 ch->line, ch->count);
941#endif
Alan Cox33f0f882006-01-09 20:54:13 -0800942 spin_lock_irqsave(&moxa_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 if (!tty_hung_up_p(filp))
944 ch->count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 ch->blocked_open++;
Alan Cox33f0f882006-01-09 20:54:13 -0800946 spin_unlock_irqrestore(&moxa_lock, flags);
947
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 while (1) {
949 set_current_state(TASK_INTERRUPTIBLE);
950 if (tty_hung_up_p(filp) ||
951 !(ch->asyncflags & ASYNC_INITIALIZED)) {
952#ifdef SERIAL_DO_RESTART
953 if (ch->asyncflags & ASYNC_HUP_NOTIFY)
954 retval = -EAGAIN;
955 else
956 retval = -ERESTARTSYS;
957#else
958 retval = -EAGAIN;
959#endif
960 break;
961 }
962 if (!(ch->asyncflags & ASYNC_CLOSING) && (do_clocal ||
963 MoxaPortDCDON(ch->port)))
964 break;
965
966 if (signal_pending(current)) {
967 retval = -ERESTARTSYS;
968 break;
969 }
970 schedule();
971 }
972 set_current_state(TASK_RUNNING);
973 remove_wait_queue(&ch->open_wait, &wait);
Alan Cox33f0f882006-01-09 20:54:13 -0800974
975 spin_lock_irqsave(&moxa_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 if (!tty_hung_up_p(filp))
977 ch->count++;
978 ch->blocked_open--;
Alan Cox33f0f882006-01-09 20:54:13 -0800979 spin_unlock_irqrestore(&moxa_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980#ifdef SERIAL_DEBUG_OPEN
981 printk("block_til_ready after blocking: ttys%d, count = %d\n",
982 ch->line, ch->count);
983#endif
984 if (retval)
985 return (retval);
Alan Cox33f0f882006-01-09 20:54:13 -0800986 /* FIXME: review to see if we need to use set_bit on these */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 ch->asyncflags |= ASYNC_NORMAL_ACTIVE;
Alan Cox33f0f882006-01-09 20:54:13 -0800988 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989}
990
991static void setup_empty_event(struct tty_struct *tty)
992{
993 struct moxa_str *ch = tty->driver_data;
994 unsigned long flags;
995
Alan Cox33f0f882006-01-09 20:54:13 -0800996 spin_lock_irqsave(&moxa_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 ch->statusflags |= EMPTYWAIT;
Jiri Slabyaa7e5222007-02-10 01:45:27 -0800998 mod_timer(&moxaEmptyTimer[ch->port], jiffies + HZ);
Alan Cox33f0f882006-01-09 20:54:13 -0800999 spin_unlock_irqrestore(&moxa_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000}
1001
1002static void check_xmit_empty(unsigned long data)
1003{
1004 struct moxa_str *ch;
1005
1006 ch = (struct moxa_str *) data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 del_timer(&moxaEmptyTimer[ch->port]);
1008 if (ch->tty && (ch->statusflags & EMPTYWAIT)) {
1009 if (MoxaPortTxQueue(ch->port) == 0) {
1010 ch->statusflags &= ~EMPTYWAIT;
1011 tty_wakeup(ch->tty);
1012 return;
1013 }
Jiri Slabyaa7e5222007-02-10 01:45:27 -08001014 mod_timer(&moxaEmptyTimer[ch->port], jiffies + HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 } else
1016 ch->statusflags &= ~EMPTYWAIT;
1017}
1018
1019static void shut_down(struct moxa_str *ch)
1020{
1021 struct tty_struct *tp;
1022
1023 if (!(ch->asyncflags & ASYNC_INITIALIZED))
1024 return;
1025
1026 tp = ch->tty;
1027
1028 MoxaPortDisable(ch->port);
1029
1030 /*
1031 * If we're a modem control device and HUPCL is on, drop RTS & DTR.
1032 */
1033 if (tp->termios->c_cflag & HUPCL)
1034 MoxaPortLineCtrl(ch->port, 0, 0);
1035
1036 ch->asyncflags &= ~ASYNC_INITIALIZED;
1037}
1038
1039static void receive_data(struct moxa_str *ch)
1040{
1041 struct tty_struct *tp;
Alan Cox606d0992006-12-08 02:38:45 -08001042 struct ktermios *ts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 unsigned long flags;
1044
1045 ts = NULL;
1046 tp = ch->tty;
1047 if (tp)
1048 ts = tp->termios;
1049 /**************************************************
1050 if ( !tp || !ts || !(ts->c_cflag & CREAD) ) {
1051 *****************************************************/
1052 if (!tp || !ts) {
1053 MoxaPortFlushData(ch->port, 0);
1054 return;
1055 }
Alan Cox33f0f882006-01-09 20:54:13 -08001056 spin_lock_irqsave(&moxa_lock, flags);
1057 MoxaPortReadData(ch->port, tp);
1058 spin_unlock_irqrestore(&moxa_lock, flags);
1059 tty_schedule_flip(tp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060}
1061
1062#define Magic_code 0x404
1063
1064/*
1065 * System Configuration
1066 */
1067/*
1068 * for C218 BIOS initialization
1069 */
1070#define C218_ConfBase 0x800
1071#define C218_status (C218_ConfBase + 0) /* BIOS running status */
1072#define C218_diag (C218_ConfBase + 2) /* diagnostic status */
1073#define C218_key (C218_ConfBase + 4) /* WORD (0x218 for C218) */
1074#define C218DLoad_len (C218_ConfBase + 6) /* WORD */
1075#define C218check_sum (C218_ConfBase + 8) /* BYTE */
1076#define C218chksum_ok (C218_ConfBase + 0x0a) /* BYTE (1:ok) */
1077#define C218_TestRx (C218_ConfBase + 0x10) /* 8 bytes for 8 ports */
1078#define C218_TestTx (C218_ConfBase + 0x18) /* 8 bytes for 8 ports */
1079#define C218_RXerr (C218_ConfBase + 0x20) /* 8 bytes for 8 ports */
1080#define C218_ErrFlag (C218_ConfBase + 0x28) /* 8 bytes for 8 ports */
1081
1082#define C218_LoadBuf 0x0F00
1083#define C218_KeyCode 0x218
1084#define CP204J_KeyCode 0x204
1085
1086/*
1087 * for C320 BIOS initialization
1088 */
1089#define C320_ConfBase 0x800
1090#define C320_LoadBuf 0x0f00
1091#define STS_init 0x05 /* for C320_status */
1092
1093#define C320_status C320_ConfBase + 0 /* BIOS running status */
1094#define C320_diag C320_ConfBase + 2 /* diagnostic status */
1095#define C320_key C320_ConfBase + 4 /* WORD (0320H for C320) */
1096#define C320DLoad_len C320_ConfBase + 6 /* WORD */
1097#define C320check_sum C320_ConfBase + 8 /* WORD */
1098#define C320chksum_ok C320_ConfBase + 0x0a /* WORD (1:ok) */
1099#define C320bapi_len C320_ConfBase + 0x0c /* WORD */
1100#define C320UART_no C320_ConfBase + 0x0e /* WORD */
1101
1102#define C320_KeyCode 0x320
1103
1104#define FixPage_addr 0x0000 /* starting addr of static page */
1105#define DynPage_addr 0x2000 /* starting addr of dynamic page */
1106#define C218_start 0x3000 /* starting addr of C218 BIOS prg */
1107#define Control_reg 0x1ff0 /* select page and reset control */
1108#define HW_reset 0x80
1109
1110/*
1111 * Function Codes
1112 */
1113#define FC_CardReset 0x80
1114#define FC_ChannelReset 1 /* C320 firmware not supported */
1115#define FC_EnableCH 2
1116#define FC_DisableCH 3
1117#define FC_SetParam 4
1118#define FC_SetMode 5
1119#define FC_SetRate 6
1120#define FC_LineControl 7
1121#define FC_LineStatus 8
1122#define FC_XmitControl 9
1123#define FC_FlushQueue 10
1124#define FC_SendBreak 11
1125#define FC_StopBreak 12
1126#define FC_LoopbackON 13
1127#define FC_LoopbackOFF 14
1128#define FC_ClrIrqTable 15
1129#define FC_SendXon 16
1130#define FC_SetTermIrq 17 /* C320 firmware not supported */
1131#define FC_SetCntIrq 18 /* C320 firmware not supported */
1132#define FC_SetBreakIrq 19
1133#define FC_SetLineIrq 20
1134#define FC_SetFlowCtl 21
1135#define FC_GenIrq 22
1136#define FC_InCD180 23
1137#define FC_OutCD180 24
1138#define FC_InUARTreg 23
1139#define FC_OutUARTreg 24
1140#define FC_SetXonXoff 25
1141#define FC_OutCD180CCR 26
1142#define FC_ExtIQueue 27
1143#define FC_ExtOQueue 28
1144#define FC_ClrLineIrq 29
1145#define FC_HWFlowCtl 30
1146#define FC_GetClockRate 35
1147#define FC_SetBaud 36
1148#define FC_SetDataMode 41
1149#define FC_GetCCSR 43
1150#define FC_GetDataError 45
1151#define FC_RxControl 50
1152#define FC_ImmSend 51
1153#define FC_SetXonState 52
1154#define FC_SetXoffState 53
1155#define FC_SetRxFIFOTrig 54
1156#define FC_SetTxFIFOCnt 55
1157#define FC_UnixRate 56
1158#define FC_UnixResetTimer 57
1159
1160#define RxFIFOTrig1 0
1161#define RxFIFOTrig4 1
1162#define RxFIFOTrig8 2
1163#define RxFIFOTrig14 3
1164
1165/*
1166 * Dual-Ported RAM
1167 */
1168#define DRAM_global 0
1169#define INT_data (DRAM_global + 0)
1170#define Config_base (DRAM_global + 0x108)
1171
1172#define IRQindex (INT_data + 0)
1173#define IRQpending (INT_data + 4)
1174#define IRQtable (INT_data + 8)
1175
1176/*
1177 * Interrupt Status
1178 */
1179#define IntrRx 0x01 /* receiver data O.K. */
1180#define IntrTx 0x02 /* transmit buffer empty */
1181#define IntrFunc 0x04 /* function complete */
1182#define IntrBreak 0x08 /* received break */
1183#define IntrLine 0x10 /* line status change
1184 for transmitter */
1185#define IntrIntr 0x20 /* received INTR code */
1186#define IntrQuit 0x40 /* received QUIT code */
1187#define IntrEOF 0x80 /* received EOF code */
1188
1189#define IntrRxTrigger 0x100 /* rx data count reach tigger value */
1190#define IntrTxTrigger 0x200 /* tx data count below trigger value */
1191
1192#define Magic_no (Config_base + 0)
1193#define Card_model_no (Config_base + 2)
1194#define Total_ports (Config_base + 4)
1195#define Module_cnt (Config_base + 8)
1196#define Module_no (Config_base + 10)
1197#define Timer_10ms (Config_base + 14)
1198#define Disable_IRQ (Config_base + 20)
1199#define TMS320_PORT1 (Config_base + 22)
1200#define TMS320_PORT2 (Config_base + 24)
1201#define TMS320_CLOCK (Config_base + 26)
1202
1203/*
1204 * DATA BUFFER in DRAM
1205 */
1206#define Extern_table 0x400 /* Base address of the external table
1207 (24 words * 64) total 3K bytes
1208 (24 words * 128) total 6K bytes */
1209#define Extern_size 0x60 /* 96 bytes */
1210#define RXrptr 0x00 /* read pointer for RX buffer */
1211#define RXwptr 0x02 /* write pointer for RX buffer */
1212#define TXrptr 0x04 /* read pointer for TX buffer */
1213#define TXwptr 0x06 /* write pointer for TX buffer */
1214#define HostStat 0x08 /* IRQ flag and general flag */
1215#define FlagStat 0x0A
1216#define FlowControl 0x0C /* B7 B6 B5 B4 B3 B2 B1 B0 */
1217 /* x x x x | | | | */
1218 /* | | | + CTS flow */
1219 /* | | +--- RTS flow */
1220 /* | +------ TX Xon/Xoff */
1221 /* +--------- RX Xon/Xoff */
1222#define Break_cnt 0x0E /* received break count */
1223#define CD180TXirq 0x10 /* if non-0: enable TX irq */
1224#define RX_mask 0x12
1225#define TX_mask 0x14
1226#define Ofs_rxb 0x16
1227#define Ofs_txb 0x18
1228#define Page_rxb 0x1A
1229#define Page_txb 0x1C
1230#define EndPage_rxb 0x1E
1231#define EndPage_txb 0x20
1232#define Data_error 0x22
1233#define RxTrigger 0x28
1234#define TxTrigger 0x2a
1235
1236#define rRXwptr 0x34
1237#define Low_water 0x36
1238
1239#define FuncCode 0x40
1240#define FuncArg 0x42
1241#define FuncArg1 0x44
1242
1243#define C218rx_size 0x2000 /* 8K bytes */
1244#define C218tx_size 0x8000 /* 32K bytes */
1245
1246#define C218rx_mask (C218rx_size - 1)
1247#define C218tx_mask (C218tx_size - 1)
1248
1249#define C320p8rx_size 0x2000
1250#define C320p8tx_size 0x8000
1251#define C320p8rx_mask (C320p8rx_size - 1)
1252#define C320p8tx_mask (C320p8tx_size - 1)
1253
1254#define C320p16rx_size 0x2000
1255#define C320p16tx_size 0x4000
1256#define C320p16rx_mask (C320p16rx_size - 1)
1257#define C320p16tx_mask (C320p16tx_size - 1)
1258
1259#define C320p24rx_size 0x2000
1260#define C320p24tx_size 0x2000
1261#define C320p24rx_mask (C320p24rx_size - 1)
1262#define C320p24tx_mask (C320p24tx_size - 1)
1263
1264#define C320p32rx_size 0x1000
1265#define C320p32tx_size 0x1000
1266#define C320p32rx_mask (C320p32rx_size - 1)
1267#define C320p32tx_mask (C320p32tx_size - 1)
1268
1269#define Page_size 0x2000
1270#define Page_mask (Page_size - 1)
1271#define C218rx_spage 3
1272#define C218tx_spage 4
1273#define C218rx_pageno 1
1274#define C218tx_pageno 4
1275#define C218buf_pageno 5
1276
1277#define C320p8rx_spage 3
1278#define C320p8tx_spage 4
1279#define C320p8rx_pgno 1
1280#define C320p8tx_pgno 4
1281#define C320p8buf_pgno 5
1282
1283#define C320p16rx_spage 3
1284#define C320p16tx_spage 4
1285#define C320p16rx_pgno 1
1286#define C320p16tx_pgno 2
1287#define C320p16buf_pgno 3
1288
1289#define C320p24rx_spage 3
1290#define C320p24tx_spage 4
1291#define C320p24rx_pgno 1
1292#define C320p24tx_pgno 1
1293#define C320p24buf_pgno 2
1294
1295#define C320p32rx_spage 3
1296#define C320p32tx_ofs C320p32rx_size
1297#define C320p32tx_spage 3
1298#define C320p32buf_pgno 1
1299
1300/*
1301 * Host Status
1302 */
1303#define WakeupRx 0x01
1304#define WakeupTx 0x02
1305#define WakeupBreak 0x08
1306#define WakeupLine 0x10
1307#define WakeupIntr 0x20
1308#define WakeupQuit 0x40
1309#define WakeupEOF 0x80 /* used in VTIME control */
1310#define WakeupRxTrigger 0x100
1311#define WakeupTxTrigger 0x200
1312/*
1313 * Flag status
1314 */
1315#define Rx_over 0x01
1316#define Xoff_state 0x02
1317#define Tx_flowOff 0x04
1318#define Tx_enable 0x08
1319#define CTS_state 0x10
1320#define DSR_state 0x20
1321#define DCD_state 0x80
1322/*
1323 * FlowControl
1324 */
1325#define CTS_FlowCtl 1
1326#define RTS_FlowCtl 2
1327#define Tx_FlowCtl 4
1328#define Rx_FlowCtl 8
1329#define IXM_IXANY 0x10
1330
1331#define LowWater 128
1332
1333#define DTR_ON 1
1334#define RTS_ON 2
1335#define CTS_ON 1
1336#define DSR_ON 2
1337#define DCD_ON 8
1338
1339/* mode definition */
1340#define MX_CS8 0x03
1341#define MX_CS7 0x02
1342#define MX_CS6 0x01
1343#define MX_CS5 0x00
1344
1345#define MX_STOP1 0x00
1346#define MX_STOP15 0x04
1347#define MX_STOP2 0x08
1348
1349#define MX_PARNONE 0x00
1350#define MX_PAREVEN 0x40
1351#define MX_PARODD 0xC0
1352
1353/*
1354 * Query
1355 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
1357struct mon_str {
1358 int tick;
1359 int rxcnt[MAX_PORTS];
1360 int txcnt[MAX_PORTS];
1361};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362
1363#define DCD_changed 0x01
1364#define DCD_oldstate 0x80
1365
1366static unsigned char moxaBuff[10240];
1367static void __iomem *moxaIntNdx[MAX_BOARDS];
1368static void __iomem *moxaIntPend[MAX_BOARDS];
1369static void __iomem *moxaIntTable[MAX_BOARDS];
1370static char moxaChkPort[MAX_PORTS];
1371static char moxaLineCtrl[MAX_PORTS];
1372static void __iomem *moxaTableAddr[MAX_PORTS];
1373static long moxaCurBaud[MAX_PORTS];
1374static char moxaDCDState[MAX_PORTS];
1375static char moxaLowChkFlag[MAX_PORTS];
1376static int moxaLowWaterChk;
1377static int moxaCard;
Jiri Slaby9dff89c2007-02-10 01:45:30 -08001378static struct mon_str moxaLog;
Jiri Slaby9fa372a2007-02-10 01:45:26 -08001379static int moxaFuncTout = HZ / 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380static ushort moxaBreakCnt[MAX_PORTS];
1381
1382static void moxadelay(int);
1383static void moxafunc(void __iomem *, int, ushort);
1384static void wait_finish(void __iomem *);
1385static void low_water_check(void __iomem *);
1386static int moxaloadbios(int, unsigned char __user *, int);
1387static int moxafindcard(int);
1388static int moxaload320b(int, unsigned char __user *, int);
1389static int moxaloadcode(int, unsigned char __user *, int);
1390static int moxaloadc218(int, void __iomem *, int);
1391static int moxaloadc320(int, void __iomem *, int, int *);
1392
1393/*****************************************************************************
1394 * Driver level functions: *
1395 * 1. MoxaDriverInit(void); *
1396 * 2. MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port); *
1397 * 3. MoxaDriverPoll(void); *
1398 *****************************************************************************/
1399void MoxaDriverInit(void)
1400{
1401 int i;
1402
1403 moxaFuncTout = HZ / 2; /* 500 mini-seconds */
1404 moxaCard = 0;
1405 moxaLog.tick = 0;
1406 moxaLowWaterChk = 0;
1407 for (i = 0; i < MAX_PORTS; i++) {
1408 moxaChkPort[i] = 0;
1409 moxaLowChkFlag[i] = 0;
1410 moxaLineCtrl[i] = 0;
1411 moxaLog.rxcnt[i] = 0;
1412 moxaLog.txcnt[i] = 0;
1413 }
1414}
1415
1416#define MOXA 0x400
1417#define MOXA_GET_IQUEUE (MOXA + 1) /* get input buffered count */
1418#define MOXA_GET_OQUEUE (MOXA + 2) /* get output buffered count */
1419#define MOXA_INIT_DRIVER (MOXA + 6) /* moxaCard=0 */
1420#define MOXA_LOAD_BIOS (MOXA + 9) /* download BIOS */
1421#define MOXA_FIND_BOARD (MOXA + 10) /* Check if MOXA card exist? */
1422#define MOXA_LOAD_C320B (MOXA + 11) /* download 320B firmware */
1423#define MOXA_LOAD_CODE (MOXA + 12) /* download firmware */
1424#define MOXA_GETDATACOUNT (MOXA + 23)
1425#define MOXA_GET_IOQUEUE (MOXA + 27)
1426#define MOXA_FLUSH_QUEUE (MOXA + 28)
1427#define MOXA_GET_CONF (MOXA + 35) /* configuration */
1428#define MOXA_GET_MAJOR (MOXA + 63)
1429#define MOXA_GET_CUMAJOR (MOXA + 64)
1430#define MOXA_GETMSTATUS (MOXA + 65)
1431
1432
1433struct moxaq_str {
1434 int inq;
1435 int outq;
1436};
1437
1438struct dl_str {
1439 char __user *buf;
1440 int len;
1441 int cardno;
1442};
1443
1444static struct moxaq_str temp_queue[MAX_PORTS];
1445static struct dl_str dltmp;
1446
1447void MoxaPortFlushData(int port, int mode)
1448{
1449 void __iomem *ofsAddr;
1450 if ((mode < 0) || (mode > 2))
1451 return;
1452 ofsAddr = moxaTableAddr[port];
1453 moxafunc(ofsAddr, FC_FlushQueue, mode);
1454 if (mode != 1) {
1455 moxaLowChkFlag[port] = 0;
1456 low_water_check(ofsAddr);
1457 }
1458}
1459
1460int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port)
1461{
1462 int i;
1463 int status;
1464 int MoxaPortTxQueue(int), MoxaPortRxQueue(int);
1465 void __user *argp = (void __user *)arg;
1466
Jiri Slaby11324ed2007-02-10 01:45:31 -08001467 if (port == MAX_PORTS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 if ((cmd != MOXA_GET_CONF) && (cmd != MOXA_INIT_DRIVER) &&
1469 (cmd != MOXA_LOAD_BIOS) && (cmd != MOXA_FIND_BOARD) && (cmd != MOXA_LOAD_C320B) &&
1470 (cmd != MOXA_LOAD_CODE) && (cmd != MOXA_GETDATACOUNT) &&
1471 (cmd != MOXA_GET_IOQUEUE) && (cmd != MOXA_GET_MAJOR) &&
1472 (cmd != MOXA_GET_CUMAJOR) && (cmd != MOXA_GETMSTATUS))
1473 return (-EINVAL);
1474 }
1475 switch (cmd) {
1476 case MOXA_GET_CONF:
Jiri Slaby9dff89c2007-02-10 01:45:30 -08001477 if(copy_to_user(argp, &moxa_boards, MAX_BOARDS *
1478 sizeof(struct moxa_board_conf)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 return -EFAULT;
1480 return (0);
1481 case MOXA_INIT_DRIVER:
1482 if ((int) arg == 0x404)
1483 MoxaDriverInit();
1484 return (0);
1485 case MOXA_GETDATACOUNT:
1486 moxaLog.tick = jiffies;
Jiri Slaby9dff89c2007-02-10 01:45:30 -08001487 if(copy_to_user(argp, &moxaLog, sizeof(struct mon_str)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 return -EFAULT;
1489 return (0);
1490 case MOXA_FLUSH_QUEUE:
1491 MoxaPortFlushData(port, arg);
1492 return (0);
1493 case MOXA_GET_IOQUEUE:
1494 for (i = 0; i < MAX_PORTS; i++) {
1495 if (moxaChkPort[i]) {
1496 temp_queue[i].inq = MoxaPortRxQueue(i);
1497 temp_queue[i].outq = MoxaPortTxQueue(i);
1498 }
1499 }
1500 if(copy_to_user(argp, temp_queue, sizeof(struct moxaq_str) * MAX_PORTS))
1501 return -EFAULT;
1502 return (0);
1503 case MOXA_GET_OQUEUE:
1504 i = MoxaPortTxQueue(port);
1505 return put_user(i, (unsigned long __user *)argp);
1506 case MOXA_GET_IQUEUE:
1507 i = MoxaPortRxQueue(port);
1508 return put_user(i, (unsigned long __user *)argp);
1509 case MOXA_GET_MAJOR:
1510 if(copy_to_user(argp, &ttymajor, sizeof(int)))
1511 return -EFAULT;
1512 return 0;
1513 case MOXA_GET_CUMAJOR:
1514 i = 0;
1515 if(copy_to_user(argp, &i, sizeof(int)))
1516 return -EFAULT;
1517 return 0;
1518 case MOXA_GETMSTATUS:
1519 for (i = 0; i < MAX_PORTS; i++) {
1520 GMStatus[i].ri = 0;
1521 GMStatus[i].dcd = 0;
1522 GMStatus[i].dsr = 0;
1523 GMStatus[i].cts = 0;
1524 if (!moxaChkPort[i]) {
1525 continue;
1526 } else {
1527 status = MoxaPortLineStatus(moxaChannels[i].port);
1528 if (status & 1)
1529 GMStatus[i].cts = 1;
1530 if (status & 2)
1531 GMStatus[i].dsr = 1;
1532 if (status & 4)
1533 GMStatus[i].dcd = 1;
1534 }
1535
1536 if (!moxaChannels[i].tty || !moxaChannels[i].tty->termios)
1537 GMStatus[i].cflag = moxaChannels[i].cflag;
1538 else
1539 GMStatus[i].cflag = moxaChannels[i].tty->termios->c_cflag;
1540 }
1541 if(copy_to_user(argp, GMStatus, sizeof(struct mxser_mstatus) * MAX_PORTS))
1542 return -EFAULT;
1543 return 0;
1544 default:
1545 return (-ENOIOCTLCMD);
1546 case MOXA_LOAD_BIOS:
1547 case MOXA_FIND_BOARD:
1548 case MOXA_LOAD_C320B:
1549 case MOXA_LOAD_CODE:
Alan Cox49cd6192006-01-09 09:35:28 -05001550 if (!capable(CAP_SYS_RAWIO))
1551 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 break;
1553 }
1554
1555 if(copy_from_user(&dltmp, argp, sizeof(struct dl_str)))
1556 return -EFAULT;
1557 if(dltmp.cardno < 0 || dltmp.cardno >= MAX_BOARDS)
1558 return -EINVAL;
1559
1560 switch(cmd)
1561 {
1562 case MOXA_LOAD_BIOS:
1563 i = moxaloadbios(dltmp.cardno, dltmp.buf, dltmp.len);
1564 return (i);
1565 case MOXA_FIND_BOARD:
1566 return moxafindcard(dltmp.cardno);
1567 case MOXA_LOAD_C320B:
1568 moxaload320b(dltmp.cardno, dltmp.buf, dltmp.len);
1569 default: /* to keep gcc happy */
1570 return (0);
1571 case MOXA_LOAD_CODE:
1572 i = moxaloadcode(dltmp.cardno, dltmp.buf, dltmp.len);
1573 if (i == -1)
1574 return (-EFAULT);
1575 return (i);
1576
1577 }
1578}
1579
1580int MoxaDriverPoll(void)
1581{
1582 register ushort temp;
1583 register int card;
1584 void __iomem *ofsAddr;
1585 void __iomem *ip;
1586 int port, p, ports;
1587
1588 if (moxaCard == 0)
1589 return (-1);
1590 for (card = 0; card < MAX_BOARDS; card++) {
Dirk Eibach01cfaf02006-08-27 01:23:36 -07001591 if (loadstat[card] == 0)
1592 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 if ((ports = moxa_boards[card].numPorts) == 0)
1594 continue;
1595 if (readb(moxaIntPend[card]) == 0xff) {
1596 ip = moxaIntTable[card] + readb(moxaIntNdx[card]);
1597 p = card * MAX_PORTS_PER_BOARD;
1598 ports <<= 1;
1599 for (port = 0; port < ports; port += 2, p++) {
1600 if ((temp = readw(ip + port)) != 0) {
1601 writew(0, ip + port);
1602 ofsAddr = moxaTableAddr[p];
1603 if (temp & IntrTx)
1604 writew(readw(ofsAddr + HostStat) & ~WakeupTx, ofsAddr + HostStat);
1605 if (temp & IntrBreak) {
1606 moxaBreakCnt[p]++;
1607 }
1608 if (temp & IntrLine) {
1609 if (readb(ofsAddr + FlagStat) & DCD_state) {
1610 if ((moxaDCDState[p] & DCD_oldstate) == 0)
1611 moxaDCDState[p] = (DCD_oldstate |
1612 DCD_changed);
1613 } else {
1614 if (moxaDCDState[p] & DCD_oldstate)
1615 moxaDCDState[p] = DCD_changed;
1616 }
1617 }
1618 }
1619 }
1620 writeb(0, moxaIntPend[card]);
1621 }
1622 if (moxaLowWaterChk) {
1623 p = card * MAX_PORTS_PER_BOARD;
1624 for (port = 0; port < ports; port++, p++) {
1625 if (moxaLowChkFlag[p]) {
1626 moxaLowChkFlag[p] = 0;
1627 ofsAddr = moxaTableAddr[p];
1628 low_water_check(ofsAddr);
1629 }
1630 }
1631 }
1632 }
1633 moxaLowWaterChk = 0;
1634 return (0);
1635}
1636
1637/*****************************************************************************
1638 * Card level function: *
1639 * 1. MoxaPortsOfCard(int cardno); *
1640 *****************************************************************************/
1641int MoxaPortsOfCard(int cardno)
1642{
1643
1644 if (moxa_boards[cardno].boardType == 0)
1645 return (0);
1646 return (moxa_boards[cardno].numPorts);
1647}
1648
1649/*****************************************************************************
1650 * Port level functions: *
1651 * 1. MoxaPortIsValid(int port); *
1652 * 2. MoxaPortEnable(int port); *
1653 * 3. MoxaPortDisable(int port); *
1654 * 4. MoxaPortGetMaxBaud(int port); *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 * 6. MoxaPortSetBaud(int port, long baud); *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 * 8. MoxaPortSetTermio(int port, unsigned char *termio); *
1657 * 9. MoxaPortGetLineOut(int port, int *dtrState, int *rtsState); *
1658 * 10. MoxaPortLineCtrl(int port, int dtrState, int rtsState); *
1659 * 11. MoxaPortFlowCtrl(int port, int rts, int cts, int rx, int tx,int xany); *
1660 * 12. MoxaPortLineStatus(int port); *
1661 * 13. MoxaPortDCDChange(int port); *
1662 * 14. MoxaPortDCDON(int port); *
1663 * 15. MoxaPortFlushData(int port, int mode); *
1664 * 16. MoxaPortWriteData(int port, unsigned char * buffer, int length); *
Alan Cox33f0f882006-01-09 20:54:13 -08001665 * 17. MoxaPortReadData(int port, struct tty_struct *tty); *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 * 20. MoxaPortTxQueue(int port); *
1667 * 21. MoxaPortTxFree(int port); *
1668 * 22. MoxaPortRxQueue(int port); *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 * 24. MoxaPortTxDisable(int port); *
1670 * 25. MoxaPortTxEnable(int port); *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 * 27. MoxaPortResetBrkCnt(int port); *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 * 30. MoxaPortSendBreak(int port, int ticks); *
1673 *****************************************************************************/
1674/*
1675 * Moxa Port Number Description:
1676 *
1677 * MOXA serial driver supports up to 4 MOXA-C218/C320 boards. And,
1678 * the port number using in MOXA driver functions will be 0 to 31 for
1679 * first MOXA board, 32 to 63 for second, 64 to 95 for third and 96
1680 * to 127 for fourth. For example, if you setup three MOXA boards,
1681 * first board is C218, second board is C320-16 and third board is
1682 * C320-32. The port number of first board (C218 - 8 ports) is from
1683 * 0 to 7. The port number of second board (C320 - 16 ports) is form
1684 * 32 to 47. The port number of third board (C320 - 32 ports) is from
1685 * 64 to 95. And those port numbers form 8 to 31, 48 to 63 and 96 to
1686 * 127 will be invalid.
1687 *
1688 *
1689 * Moxa Functions Description:
1690 *
1691 * Function 1: Driver initialization routine, this routine must be
1692 * called when initialized driver.
1693 * Syntax:
1694 * void MoxaDriverInit();
1695 *
1696 *
1697 * Function 2: Moxa driver private IOCTL command processing.
1698 * Syntax:
1699 * int MoxaDriverIoctl(unsigned int cmd, unsigned long arg, int port);
1700 *
1701 * unsigned int cmd : IOCTL command
1702 * unsigned long arg : IOCTL argument
1703 * int port : port number (0 - 127)
1704 *
1705 * return: 0 (OK)
1706 * -EINVAL
1707 * -ENOIOCTLCMD
1708 *
1709 *
1710 * Function 3: Moxa driver polling process routine.
1711 * Syntax:
1712 * int MoxaDriverPoll(void);
1713 *
1714 * return: 0 ; polling O.K.
1715 * -1 : no any Moxa card.
1716 *
1717 *
1718 * Function 4: Get the ports of this card.
1719 * Syntax:
1720 * int MoxaPortsOfCard(int cardno);
1721 *
1722 * int cardno : card number (0 - 3)
1723 *
1724 * return: 0 : this card is invalid
1725 * 8/16/24/32
1726 *
1727 *
1728 * Function 5: Check this port is valid or invalid
1729 * Syntax:
1730 * int MoxaPortIsValid(int port);
1731 * int port : port number (0 - 127, ref port description)
1732 *
1733 * return: 0 : this port is invalid
1734 * 1 : this port is valid
1735 *
1736 *
1737 * Function 6: Enable this port to start Tx/Rx data.
1738 * Syntax:
1739 * void MoxaPortEnable(int port);
1740 * int port : port number (0 - 127)
1741 *
1742 *
1743 * Function 7: Disable this port
1744 * Syntax:
1745 * void MoxaPortDisable(int port);
1746 * int port : port number (0 - 127)
1747 *
1748 *
1749 * Function 8: Get the maximun available baud rate of this port.
1750 * Syntax:
1751 * long MoxaPortGetMaxBaud(int port);
1752 * int port : port number (0 - 127)
1753 *
1754 * return: 0 : this port is invalid
1755 * 38400/57600/115200 bps
1756 *
1757 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 * Function 10: Setting baud rate of this port.
1759 * Syntax:
1760 * long MoxaPortSetBaud(int port, long baud);
1761 * int port : port number (0 - 127)
1762 * long baud : baud rate (50 - 115200)
1763 *
1764 * return: 0 : this port is invalid or baud < 50
1765 * 50 - 115200 : the real baud rate set to the port, if
1766 * the argument baud is large than maximun
1767 * available baud rate, the real setting
1768 * baud rate will be the maximun baud rate.
1769 *
1770 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 * Function 12: Configure the port.
1772 * Syntax:
Alan Cox606d0992006-12-08 02:38:45 -08001773 * int MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 * int port : port number (0 - 127)
Alan Cox606d0992006-12-08 02:38:45 -08001775 * struct ktermios * termio : termio structure pointer
Alan Coxc7bce302006-09-30 23:27:24 -07001776 * speed_t baud : baud rate
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 *
1778 * return: -1 : this port is invalid or termio == NULL
1779 * 0 : setting O.K.
1780 *
1781 *
1782 * Function 13: Get the DTR/RTS state of this port.
1783 * Syntax:
1784 * int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState);
1785 * int port : port number (0 - 127)
1786 * int * dtrState : pointer to INT to receive the current DTR
1787 * state. (if NULL, this function will not
1788 * write to this address)
1789 * int * rtsState : pointer to INT to receive the current RTS
1790 * state. (if NULL, this function will not
1791 * write to this address)
1792 *
1793 * return: -1 : this port is invalid
1794 * 0 : O.K.
1795 *
1796 *
1797 * Function 14: Setting the DTR/RTS output state of this port.
1798 * Syntax:
1799 * void MoxaPortLineCtrl(int port, int dtrState, int rtsState);
1800 * int port : port number (0 - 127)
1801 * int dtrState : DTR output state (0: off, 1: on)
1802 * int rtsState : RTS output state (0: off, 1: on)
1803 *
1804 *
1805 * Function 15: Setting the flow control of this port.
1806 * Syntax:
1807 * void MoxaPortFlowCtrl(int port, int rtsFlow, int ctsFlow, int rxFlow,
1808 * int txFlow,int xany);
1809 * int port : port number (0 - 127)
1810 * int rtsFlow : H/W RTS flow control (0: no, 1: yes)
1811 * int ctsFlow : H/W CTS flow control (0: no, 1: yes)
1812 * int rxFlow : S/W Rx XON/XOFF flow control (0: no, 1: yes)
1813 * int txFlow : S/W Tx XON/XOFF flow control (0: no, 1: yes)
1814 * int xany : S/W XANY flow control (0: no, 1: yes)
1815 *
1816 *
1817 * Function 16: Get ths line status of this port
1818 * Syntax:
1819 * int MoxaPortLineStatus(int port);
1820 * int port : port number (0 - 127)
1821 *
1822 * return: Bit 0 - CTS state (0: off, 1: on)
1823 * Bit 1 - DSR state (0: off, 1: on)
1824 * Bit 2 - DCD state (0: off, 1: on)
1825 *
1826 *
1827 * Function 17: Check the DCD state has changed since the last read
1828 * of this function.
1829 * Syntax:
1830 * int MoxaPortDCDChange(int port);
1831 * int port : port number (0 - 127)
1832 *
1833 * return: 0 : no changed
1834 * 1 : DCD has changed
1835 *
1836 *
1837 * Function 18: Check ths current DCD state is ON or not.
1838 * Syntax:
1839 * int MoxaPortDCDON(int port);
1840 * int port : port number (0 - 127)
1841 *
1842 * return: 0 : DCD off
1843 * 1 : DCD on
1844 *
1845 *
1846 * Function 19: Flush the Rx/Tx buffer data of this port.
1847 * Syntax:
1848 * void MoxaPortFlushData(int port, int mode);
1849 * int port : port number (0 - 127)
1850 * int mode
1851 * 0 : flush the Rx buffer
1852 * 1 : flush the Tx buffer
1853 * 2 : flush the Rx and Tx buffer
1854 *
1855 *
1856 * Function 20: Write data.
1857 * Syntax:
1858 * int MoxaPortWriteData(int port, unsigned char * buffer, int length);
1859 * int port : port number (0 - 127)
1860 * unsigned char * buffer : pointer to write data buffer.
1861 * int length : write data length
1862 *
1863 * return: 0 - length : real write data length
1864 *
1865 *
1866 * Function 21: Read data.
1867 * Syntax:
Alan Cox33f0f882006-01-09 20:54:13 -08001868 * int MoxaPortReadData(int port, struct tty_struct *tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 * int port : port number (0 - 127)
Alan Cox33f0f882006-01-09 20:54:13 -08001870 * struct tty_struct *tty : tty for data
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 *
1872 * return: 0 - length : real read data length
1873 *
1874 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 * Function 24: Get the Tx buffer current queued data bytes
1876 * Syntax:
1877 * int MoxaPortTxQueue(int port);
1878 * int port : port number (0 - 127)
1879 *
1880 * return: .. : Tx buffer current queued data bytes
1881 *
1882 *
1883 * Function 25: Get the Tx buffer current free space
1884 * Syntax:
1885 * int MoxaPortTxFree(int port);
1886 * int port : port number (0 - 127)
1887 *
1888 * return: .. : Tx buffer current free space
1889 *
1890 *
1891 * Function 26: Get the Rx buffer current queued data bytes
1892 * Syntax:
1893 * int MoxaPortRxQueue(int port);
1894 * int port : port number (0 - 127)
1895 *
1896 * return: .. : Rx buffer current queued data bytes
1897 *
1898 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 * Function 28: Disable port data transmission.
1900 * Syntax:
1901 * void MoxaPortTxDisable(int port);
1902 * int port : port number (0 - 127)
1903 *
1904 *
1905 * Function 29: Enable port data transmission.
1906 * Syntax:
1907 * void MoxaPortTxEnable(int port);
1908 * int port : port number (0 - 127)
1909 *
1910 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 * Function 31: Get the received BREAK signal count and reset it.
1912 * Syntax:
1913 * int MoxaPortResetBrkCnt(int port);
1914 * int port : port number (0 - 127)
1915 *
1916 * return: 0 - .. : BREAK signal count
1917 *
1918 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 * Function 34: Send out a BREAK signal.
1920 * Syntax:
1921 * void MoxaPortSendBreak(int port, int ms100);
1922 * int port : port number (0 - 127)
1923 * int ms100 : break signal time interval.
1924 * unit: 100 mini-second. if ms100 == 0, it will
1925 * send out a about 250 ms BREAK signal.
1926 *
1927 */
1928int MoxaPortIsValid(int port)
1929{
1930
1931 if (moxaCard == 0)
1932 return (0);
1933 if (moxaChkPort[port] == 0)
1934 return (0);
1935 return (1);
1936}
1937
1938void MoxaPortEnable(int port)
1939{
1940 void __iomem *ofsAddr;
1941 int MoxaPortLineStatus(int);
1942 short lowwater = 512;
1943
1944 ofsAddr = moxaTableAddr[port];
1945 writew(lowwater, ofsAddr + Low_water);
1946 moxaBreakCnt[port] = 0;
1947 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
1948 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
1949 moxafunc(ofsAddr, FC_SetBreakIrq, 0);
1950 } else {
1951 writew(readw(ofsAddr + HostStat) | WakeupBreak, ofsAddr + HostStat);
1952 }
1953
1954 moxafunc(ofsAddr, FC_SetLineIrq, Magic_code);
1955 moxafunc(ofsAddr, FC_FlushQueue, 2);
1956
1957 moxafunc(ofsAddr, FC_EnableCH, Magic_code);
1958 MoxaPortLineStatus(port);
1959}
1960
1961void MoxaPortDisable(int port)
1962{
1963 void __iomem *ofsAddr = moxaTableAddr[port];
1964
1965 moxafunc(ofsAddr, FC_SetFlowCtl, 0); /* disable flow control */
1966 moxafunc(ofsAddr, FC_ClrLineIrq, Magic_code);
1967 writew(0, ofsAddr + HostStat);
1968 moxafunc(ofsAddr, FC_DisableCH, Magic_code);
1969}
1970
1971long MoxaPortGetMaxBaud(int port)
1972{
1973 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
1974 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI))
1975 return (460800L);
1976 else
1977 return (921600L);
1978}
1979
1980
1981long MoxaPortSetBaud(int port, long baud)
1982{
1983 void __iomem *ofsAddr;
1984 long max, clock;
1985 unsigned int val;
1986
1987 if ((baud < 50L) || ((max = MoxaPortGetMaxBaud(port)) == 0))
1988 return (0);
1989 ofsAddr = moxaTableAddr[port];
1990 if (baud > max)
1991 baud = max;
1992 if (max == 38400L)
1993 clock = 614400L; /* for 9.8304 Mhz : max. 38400 bps */
1994 else if (max == 57600L)
1995 clock = 691200L; /* for 11.0592 Mhz : max. 57600 bps */
1996 else
1997 clock = 921600L; /* for 14.7456 Mhz : max. 115200 bps */
1998 val = clock / baud;
1999 moxafunc(ofsAddr, FC_SetBaud, val);
2000 baud = clock / val;
2001 moxaCurBaud[port] = baud;
2002 return (baud);
2003}
2004
Alan Cox606d0992006-12-08 02:38:45 -08002005int MoxaPortSetTermio(int port, struct ktermios *termio, speed_t baud)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006{
2007 void __iomem *ofsAddr;
2008 tcflag_t cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 tcflag_t mode = 0;
2010
2011 if (moxaChkPort[port] == 0 || termio == 0)
2012 return (-1);
2013 ofsAddr = moxaTableAddr[port];
2014 cflag = termio->c_cflag; /* termio->c_cflag */
2015
2016 mode = termio->c_cflag & CSIZE;
2017 if (mode == CS5)
2018 mode = MX_CS5;
2019 else if (mode == CS6)
2020 mode = MX_CS6;
2021 else if (mode == CS7)
2022 mode = MX_CS7;
2023 else if (mode == CS8)
2024 mode = MX_CS8;
2025
2026 if (termio->c_cflag & CSTOPB) {
2027 if (mode == MX_CS5)
2028 mode |= MX_STOP15;
2029 else
2030 mode |= MX_STOP2;
2031 } else
2032 mode |= MX_STOP1;
2033
2034 if (termio->c_cflag & PARENB) {
2035 if (termio->c_cflag & PARODD)
2036 mode |= MX_PARODD;
2037 else
2038 mode |= MX_PAREVEN;
2039 } else
2040 mode |= MX_PARNONE;
2041
2042 moxafunc(ofsAddr, FC_SetDataMode, (ushort) mode);
2043
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
2045 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
Alan Coxc7bce302006-09-30 23:27:24 -07002046 if (baud >= 921600L)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 return (-1);
2048 }
2049 MoxaPortSetBaud(port, baud);
2050
2051 if (termio->c_iflag & (IXON | IXOFF | IXANY)) {
2052 writeb(termio->c_cc[VSTART], ofsAddr + FuncArg);
2053 writeb(termio->c_cc[VSTOP], ofsAddr + FuncArg1);
2054 writeb(FC_SetXonXoff, ofsAddr + FuncCode);
2055 wait_finish(ofsAddr);
2056
2057 }
2058 return (0);
2059}
2060
2061int MoxaPortGetLineOut(int port, int *dtrState, int *rtsState)
2062{
2063
2064 if (!MoxaPortIsValid(port))
2065 return (-1);
2066 if (dtrState) {
2067 if (moxaLineCtrl[port] & DTR_ON)
2068 *dtrState = 1;
2069 else
2070 *dtrState = 0;
2071 }
2072 if (rtsState) {
2073 if (moxaLineCtrl[port] & RTS_ON)
2074 *rtsState = 1;
2075 else
2076 *rtsState = 0;
2077 }
2078 return (0);
2079}
2080
2081void MoxaPortLineCtrl(int port, int dtr, int rts)
2082{
2083 void __iomem *ofsAddr;
2084 int mode;
2085
2086 ofsAddr = moxaTableAddr[port];
2087 mode = 0;
2088 if (dtr)
2089 mode |= DTR_ON;
2090 if (rts)
2091 mode |= RTS_ON;
2092 moxaLineCtrl[port] = mode;
2093 moxafunc(ofsAddr, FC_LineControl, mode);
2094}
2095
2096void MoxaPortFlowCtrl(int port, int rts, int cts, int txflow, int rxflow, int txany)
2097{
2098 void __iomem *ofsAddr;
2099 int mode;
2100
2101 ofsAddr = moxaTableAddr[port];
2102 mode = 0;
2103 if (rts)
2104 mode |= RTS_FlowCtl;
2105 if (cts)
2106 mode |= CTS_FlowCtl;
2107 if (txflow)
2108 mode |= Tx_FlowCtl;
2109 if (rxflow)
2110 mode |= Rx_FlowCtl;
2111 if (txany)
2112 mode |= IXM_IXANY;
2113 moxafunc(ofsAddr, FC_SetFlowCtl, mode);
2114}
2115
2116int MoxaPortLineStatus(int port)
2117{
2118 void __iomem *ofsAddr;
2119 int val;
2120
2121 ofsAddr = moxaTableAddr[port];
2122 if ((moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_ISA) ||
2123 (moxa_boards[port / MAX_PORTS_PER_BOARD].boardType == MOXA_BOARD_C320_PCI)) {
2124 moxafunc(ofsAddr, FC_LineStatus, 0);
2125 val = readw(ofsAddr + FuncArg);
2126 } else {
2127 val = readw(ofsAddr + FlagStat) >> 4;
2128 }
2129 val &= 0x0B;
2130 if (val & 8) {
2131 val |= 4;
2132 if ((moxaDCDState[port] & DCD_oldstate) == 0)
2133 moxaDCDState[port] = (DCD_oldstate | DCD_changed);
2134 } else {
2135 if (moxaDCDState[port] & DCD_oldstate)
2136 moxaDCDState[port] = DCD_changed;
2137 }
2138 val &= 7;
2139 return (val);
2140}
2141
2142int MoxaPortDCDChange(int port)
2143{
2144 int n;
2145
2146 if (moxaChkPort[port] == 0)
2147 return (0);
2148 n = moxaDCDState[port];
2149 moxaDCDState[port] &= ~DCD_changed;
2150 n &= DCD_changed;
2151 return (n);
2152}
2153
2154int MoxaPortDCDON(int port)
2155{
2156 int n;
2157
2158 if (moxaChkPort[port] == 0)
2159 return (0);
2160 if (moxaDCDState[port] & DCD_oldstate)
2161 n = 1;
2162 else
2163 n = 0;
2164 return (n);
2165}
2166
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167int MoxaPortWriteData(int port, unsigned char * buffer, int len)
2168{
2169 int c, total, i;
2170 ushort tail;
2171 int cnt;
2172 ushort head, tx_mask, spage, epage;
2173 ushort pageno, pageofs, bufhead;
2174 void __iomem *baseAddr, *ofsAddr, *ofs;
2175
2176 ofsAddr = moxaTableAddr[port];
2177 baseAddr = moxaBaseAddr[port / MAX_PORTS_PER_BOARD];
2178 tx_mask = readw(ofsAddr + TX_mask);
2179 spage = readw(ofsAddr + Page_txb);
2180 epage = readw(ofsAddr + EndPage_txb);
2181 tail = readw(ofsAddr + TXwptr);
2182 head = readw(ofsAddr + TXrptr);
2183 c = (head > tail) ? (head - tail - 1)
2184 : (head - tail + tx_mask);
2185 if (c > len)
2186 c = len;
2187 moxaLog.txcnt[port] += c;
2188 total = c;
2189 if (spage == epage) {
2190 bufhead = readw(ofsAddr + Ofs_txb);
2191 writew(spage, baseAddr + Control_reg);
2192 while (c > 0) {
2193 if (head > tail)
2194 len = head - tail - 1;
2195 else
2196 len = tx_mask + 1 - tail;
2197 len = (c > len) ? len : c;
2198 ofs = baseAddr + DynPage_addr + bufhead + tail;
2199 for (i = 0; i < len; i++)
2200 writeb(*buffer++, ofs + i);
2201 tail = (tail + len) & tx_mask;
2202 c -= len;
2203 }
2204 writew(tail, ofsAddr + TXwptr);
2205 } else {
2206 len = c;
2207 pageno = spage + (tail >> 13);
2208 pageofs = tail & Page_mask;
2209 do {
2210 cnt = Page_size - pageofs;
2211 if (cnt > c)
2212 cnt = c;
2213 c -= cnt;
2214 writeb(pageno, baseAddr + Control_reg);
2215 ofs = baseAddr + DynPage_addr + pageofs;
2216 for (i = 0; i < cnt; i++)
2217 writeb(*buffer++, ofs + i);
2218 if (c == 0) {
2219 writew((tail + len) & tx_mask, ofsAddr + TXwptr);
2220 break;
2221 }
2222 if (++pageno == epage)
2223 pageno = spage;
2224 pageofs = 0;
2225 } while (1);
2226 }
2227 writeb(1, ofsAddr + CD180TXirq); /* start to send */
2228 return (total);
2229}
2230
Alan Cox33f0f882006-01-09 20:54:13 -08002231int MoxaPortReadData(int port, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232{
2233 register ushort head, pageofs;
2234 int i, count, cnt, len, total, remain;
2235 ushort tail, rx_mask, spage, epage;
2236 ushort pageno, bufhead;
2237 void __iomem *baseAddr, *ofsAddr, *ofs;
2238
2239 ofsAddr = moxaTableAddr[port];
2240 baseAddr = moxaBaseAddr[port / MAX_PORTS_PER_BOARD];
2241 head = readw(ofsAddr + RXrptr);
2242 tail = readw(ofsAddr + RXwptr);
2243 rx_mask = readw(ofsAddr + RX_mask);
2244 spage = readw(ofsAddr + Page_rxb);
2245 epage = readw(ofsAddr + EndPage_rxb);
2246 count = (tail >= head) ? (tail - head)
2247 : (tail - head + rx_mask + 1);
2248 if (count == 0)
Alan Cox33f0f882006-01-09 20:54:13 -08002249 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250
Alan Cox33f0f882006-01-09 20:54:13 -08002251 total = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 remain = count - total;
2253 moxaLog.rxcnt[port] += total;
2254 count = total;
2255 if (spage == epage) {
2256 bufhead = readw(ofsAddr + Ofs_rxb);
2257 writew(spage, baseAddr + Control_reg);
2258 while (count > 0) {
2259 if (tail >= head)
2260 len = tail - head;
2261 else
2262 len = rx_mask + 1 - head;
2263 len = (count > len) ? len : count;
2264 ofs = baseAddr + DynPage_addr + bufhead + head;
2265 for (i = 0; i < len; i++)
Alan Cox33f0f882006-01-09 20:54:13 -08002266 tty_insert_flip_char(tty, readb(ofs + i), TTY_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 head = (head + len) & rx_mask;
2268 count -= len;
2269 }
2270 writew(head, ofsAddr + RXrptr);
2271 } else {
2272 len = count;
2273 pageno = spage + (head >> 13);
2274 pageofs = head & Page_mask;
2275 do {
2276 cnt = Page_size - pageofs;
2277 if (cnt > count)
2278 cnt = count;
2279 count -= cnt;
2280 writew(pageno, baseAddr + Control_reg);
2281 ofs = baseAddr + DynPage_addr + pageofs;
2282 for (i = 0; i < cnt; i++)
Alan Cox33f0f882006-01-09 20:54:13 -08002283 tty_insert_flip_char(tty, readb(ofs + i), TTY_NORMAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 if (count == 0) {
2285 writew((head + len) & rx_mask, ofsAddr + RXrptr);
2286 break;
2287 }
2288 if (++pageno == epage)
2289 pageno = spage;
2290 pageofs = 0;
2291 } while (1);
2292 }
2293 if ((readb(ofsAddr + FlagStat) & Xoff_state) && (remain < LowWater)) {
2294 moxaLowWaterChk = 1;
2295 moxaLowChkFlag[port] = 1;
2296 }
2297 return (total);
2298}
2299
2300
2301int MoxaPortTxQueue(int port)
2302{
2303 void __iomem *ofsAddr;
2304 ushort rptr, wptr, mask;
2305 int len;
2306
2307 ofsAddr = moxaTableAddr[port];
2308 rptr = readw(ofsAddr + TXrptr);
2309 wptr = readw(ofsAddr + TXwptr);
2310 mask = readw(ofsAddr + TX_mask);
2311 len = (wptr - rptr) & mask;
2312 return (len);
2313}
2314
2315int MoxaPortTxFree(int port)
2316{
2317 void __iomem *ofsAddr;
2318 ushort rptr, wptr, mask;
2319 int len;
2320
2321 ofsAddr = moxaTableAddr[port];
2322 rptr = readw(ofsAddr + TXrptr);
2323 wptr = readw(ofsAddr + TXwptr);
2324 mask = readw(ofsAddr + TX_mask);
2325 len = mask - ((wptr - rptr) & mask);
2326 return (len);
2327}
2328
2329int MoxaPortRxQueue(int port)
2330{
2331 void __iomem *ofsAddr;
2332 ushort rptr, wptr, mask;
2333 int len;
2334
2335 ofsAddr = moxaTableAddr[port];
2336 rptr = readw(ofsAddr + RXrptr);
2337 wptr = readw(ofsAddr + RXwptr);
2338 mask = readw(ofsAddr + RX_mask);
2339 len = (wptr - rptr) & mask;
2340 return (len);
2341}
2342
2343
2344void MoxaPortTxDisable(int port)
2345{
2346 void __iomem *ofsAddr;
2347
2348 ofsAddr = moxaTableAddr[port];
2349 moxafunc(ofsAddr, FC_SetXoffState, Magic_code);
2350}
2351
2352void MoxaPortTxEnable(int port)
2353{
2354 void __iomem *ofsAddr;
2355
2356 ofsAddr = moxaTableAddr[port];
2357 moxafunc(ofsAddr, FC_SetXonState, Magic_code);
2358}
2359
2360
2361int MoxaPortResetBrkCnt(int port)
2362{
2363 ushort cnt;
2364 cnt = moxaBreakCnt[port];
2365 moxaBreakCnt[port] = 0;
2366 return (cnt);
2367}
2368
2369
2370void MoxaPortSendBreak(int port, int ms100)
2371{
2372 void __iomem *ofsAddr;
2373
2374 ofsAddr = moxaTableAddr[port];
2375 if (ms100) {
2376 moxafunc(ofsAddr, FC_SendBreak, Magic_code);
2377 moxadelay(ms100 * (HZ / 10));
2378 } else {
2379 moxafunc(ofsAddr, FC_SendBreak, Magic_code);
2380 moxadelay(HZ / 4); /* 250 ms */
2381 }
2382 moxafunc(ofsAddr, FC_StopBreak, Magic_code);
2383}
2384
2385static int moxa_get_serial_info(struct moxa_str *info,
2386 struct serial_struct __user *retinfo)
2387{
2388 struct serial_struct tmp;
2389
2390 memset(&tmp, 0, sizeof(tmp));
2391 tmp.type = info->type;
2392 tmp.line = info->port;
2393 tmp.port = 0;
2394 tmp.irq = 0;
2395 tmp.flags = info->asyncflags;
2396 tmp.baud_base = 921600;
2397 tmp.close_delay = info->close_delay;
2398 tmp.closing_wait = info->closing_wait;
2399 tmp.custom_divisor = 0;
2400 tmp.hub6 = 0;
2401 if(copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2402 return -EFAULT;
2403 return (0);
2404}
2405
2406
2407static int moxa_set_serial_info(struct moxa_str *info,
2408 struct serial_struct __user *new_info)
2409{
2410 struct serial_struct new_serial;
2411
2412 if(copy_from_user(&new_serial, new_info, sizeof(new_serial)))
2413 return -EFAULT;
2414
2415 if ((new_serial.irq != 0) ||
2416 (new_serial.port != 0) ||
2417// (new_serial.type != info->type) ||
2418 (new_serial.custom_divisor != 0) ||
2419 (new_serial.baud_base != 921600))
2420 return (-EPERM);
2421
2422 if (!capable(CAP_SYS_ADMIN)) {
2423 if (((new_serial.flags & ~ASYNC_USR_MASK) !=
2424 (info->asyncflags & ~ASYNC_USR_MASK)))
2425 return (-EPERM);
2426 } else {
2427 info->close_delay = new_serial.close_delay * HZ / 100;
2428 info->closing_wait = new_serial.closing_wait * HZ / 100;
2429 }
2430
2431 new_serial.flags = (new_serial.flags & ~ASYNC_FLAGS);
2432 new_serial.flags |= (info->asyncflags & ASYNC_FLAGS);
2433
2434 if (new_serial.type == PORT_16550A) {
2435 MoxaSetFifo(info->port, 1);
2436 } else {
2437 MoxaSetFifo(info->port, 0);
2438 }
2439
2440 info->type = new_serial.type;
2441 return (0);
2442}
2443
2444
2445
2446/*****************************************************************************
2447 * Static local functions: *
2448 *****************************************************************************/
2449/*
2450 * moxadelay - delays a specified number ticks
2451 */
2452static void moxadelay(int tick)
2453{
2454 unsigned long st, et;
2455
2456 st = jiffies;
2457 et = st + tick;
2458 while (time_before(jiffies, et));
2459}
2460
2461static void moxafunc(void __iomem *ofsAddr, int cmd, ushort arg)
2462{
2463
2464 writew(arg, ofsAddr + FuncArg);
2465 writew(cmd, ofsAddr + FuncCode);
2466 wait_finish(ofsAddr);
2467}
2468
2469static void wait_finish(void __iomem *ofsAddr)
2470{
2471 unsigned long i, j;
2472
2473 i = jiffies;
2474 while (readw(ofsAddr + FuncCode) != 0) {
2475 j = jiffies;
2476 if ((j - i) > moxaFuncTout) {
2477 return;
2478 }
2479 }
2480}
2481
2482static void low_water_check(void __iomem *ofsAddr)
2483{
2484 int len;
2485 ushort rptr, wptr, mask;
2486
2487 if (readb(ofsAddr + FlagStat) & Xoff_state) {
2488 rptr = readw(ofsAddr + RXrptr);
2489 wptr = readw(ofsAddr + RXwptr);
2490 mask = readw(ofsAddr + RX_mask);
2491 len = (wptr - rptr) & mask;
2492 if (len <= Low_water)
2493 moxafunc(ofsAddr, FC_SendXon, 0);
2494 }
2495}
2496
2497static int moxaloadbios(int cardno, unsigned char __user *tmp, int len)
2498{
2499 void __iomem *baseAddr;
2500 int i;
2501
2502 if(copy_from_user(moxaBuff, tmp, len))
2503 return -EFAULT;
2504 baseAddr = moxaBaseAddr[cardno];
2505 writeb(HW_reset, baseAddr + Control_reg); /* reset */
2506 moxadelay(1); /* delay 10 ms */
2507 for (i = 0; i < 4096; i++)
2508 writeb(0, baseAddr + i); /* clear fix page */
2509 for (i = 0; i < len; i++)
2510 writeb(moxaBuff[i], baseAddr + i); /* download BIOS */
2511 writeb(0, baseAddr + Control_reg); /* restart */
2512 return (0);
2513}
2514
2515static int moxafindcard(int cardno)
2516{
2517 void __iomem *baseAddr;
2518 ushort tmp;
2519
2520 baseAddr = moxaBaseAddr[cardno];
2521 switch (moxa_boards[cardno].boardType) {
2522 case MOXA_BOARD_C218_ISA:
2523 case MOXA_BOARD_C218_PCI:
2524 if ((tmp = readw(baseAddr + C218_key)) != C218_KeyCode) {
2525 return (-1);
2526 }
2527 break;
2528 case MOXA_BOARD_CP204J:
2529 if ((tmp = readw(baseAddr + C218_key)) != CP204J_KeyCode) {
2530 return (-1);
2531 }
2532 break;
2533 default:
2534 if ((tmp = readw(baseAddr + C320_key)) != C320_KeyCode) {
2535 return (-1);
2536 }
2537 if ((tmp = readw(baseAddr + C320_status)) != STS_init) {
2538 return (-2);
2539 }
2540 }
2541 return (0);
2542}
2543
2544static int moxaload320b(int cardno, unsigned char __user *tmp, int len)
2545{
2546 void __iomem *baseAddr;
2547 int i;
2548
2549 if(len > sizeof(moxaBuff))
2550 return -EINVAL;
2551 if(copy_from_user(moxaBuff, tmp, len))
2552 return -EFAULT;
2553 baseAddr = moxaBaseAddr[cardno];
2554 writew(len - 7168 - 2, baseAddr + C320bapi_len);
2555 writeb(1, baseAddr + Control_reg); /* Select Page 1 */
2556 for (i = 0; i < 7168; i++)
2557 writeb(moxaBuff[i], baseAddr + DynPage_addr + i);
2558 writeb(2, baseAddr + Control_reg); /* Select Page 2 */
2559 for (i = 0; i < (len - 7168); i++)
2560 writeb(moxaBuff[i + 7168], baseAddr + DynPage_addr + i);
2561 return (0);
2562}
2563
2564static int moxaloadcode(int cardno, unsigned char __user *tmp, int len)
2565{
2566 void __iomem *baseAddr, *ofsAddr;
2567 int retval, port, i;
2568
2569 if(copy_from_user(moxaBuff, tmp, len))
2570 return -EFAULT;
2571 baseAddr = moxaBaseAddr[cardno];
2572 switch (moxa_boards[cardno].boardType) {
2573 case MOXA_BOARD_C218_ISA:
2574 case MOXA_BOARD_C218_PCI:
2575 case MOXA_BOARD_CP204J:
2576 retval = moxaloadc218(cardno, baseAddr, len);
2577 if (retval)
2578 return (retval);
2579 port = cardno * MAX_PORTS_PER_BOARD;
2580 for (i = 0; i < moxa_boards[cardno].numPorts; i++, port++) {
2581 moxaChkPort[port] = 1;
2582 moxaCurBaud[port] = 9600L;
2583 moxaDCDState[port] = 0;
2584 moxaTableAddr[port] = baseAddr + Extern_table + Extern_size * i;
2585 ofsAddr = moxaTableAddr[port];
2586 writew(C218rx_mask, ofsAddr + RX_mask);
2587 writew(C218tx_mask, ofsAddr + TX_mask);
2588 writew(C218rx_spage + i * C218buf_pageno, ofsAddr + Page_rxb);
2589 writew(readw(ofsAddr + Page_rxb) + C218rx_pageno, ofsAddr + EndPage_rxb);
2590
2591 writew(C218tx_spage + i * C218buf_pageno, ofsAddr + Page_txb);
2592 writew(readw(ofsAddr + Page_txb) + C218tx_pageno, ofsAddr + EndPage_txb);
2593
2594 }
2595 break;
2596 default:
2597 retval = moxaloadc320(cardno, baseAddr, len,
2598 &moxa_boards[cardno].numPorts);
2599 if (retval)
2600 return (retval);
2601 port = cardno * MAX_PORTS_PER_BOARD;
2602 for (i = 0; i < moxa_boards[cardno].numPorts; i++, port++) {
2603 moxaChkPort[port] = 1;
2604 moxaCurBaud[port] = 9600L;
2605 moxaDCDState[port] = 0;
2606 moxaTableAddr[port] = baseAddr + Extern_table + Extern_size * i;
2607 ofsAddr = moxaTableAddr[port];
2608 if (moxa_boards[cardno].numPorts == 8) {
2609 writew(C320p8rx_mask, ofsAddr + RX_mask);
2610 writew(C320p8tx_mask, ofsAddr + TX_mask);
2611 writew(C320p8rx_spage + i * C320p8buf_pgno, ofsAddr + Page_rxb);
2612 writew(readw(ofsAddr + Page_rxb) + C320p8rx_pgno, ofsAddr + EndPage_rxb);
2613 writew(C320p8tx_spage + i * C320p8buf_pgno, ofsAddr + Page_txb);
2614 writew(readw(ofsAddr + Page_txb) + C320p8tx_pgno, ofsAddr + EndPage_txb);
2615
2616 } else if (moxa_boards[cardno].numPorts == 16) {
2617 writew(C320p16rx_mask, ofsAddr + RX_mask);
2618 writew(C320p16tx_mask, ofsAddr + TX_mask);
2619 writew(C320p16rx_spage + i * C320p16buf_pgno, ofsAddr + Page_rxb);
2620 writew(readw(ofsAddr + Page_rxb) + C320p16rx_pgno, ofsAddr + EndPage_rxb);
2621 writew(C320p16tx_spage + i * C320p16buf_pgno, ofsAddr + Page_txb);
2622 writew(readw(ofsAddr + Page_txb) + C320p16tx_pgno, ofsAddr + EndPage_txb);
2623
2624 } else if (moxa_boards[cardno].numPorts == 24) {
2625 writew(C320p24rx_mask, ofsAddr + RX_mask);
2626 writew(C320p24tx_mask, ofsAddr + TX_mask);
2627 writew(C320p24rx_spage + i * C320p24buf_pgno, ofsAddr + Page_rxb);
2628 writew(readw(ofsAddr + Page_rxb) + C320p24rx_pgno, ofsAddr + EndPage_rxb);
2629 writew(C320p24tx_spage + i * C320p24buf_pgno, ofsAddr + Page_txb);
2630 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
2631 } else if (moxa_boards[cardno].numPorts == 32) {
2632 writew(C320p32rx_mask, ofsAddr + RX_mask);
2633 writew(C320p32tx_mask, ofsAddr + TX_mask);
2634 writew(C320p32tx_ofs, ofsAddr + Ofs_txb);
2635 writew(C320p32rx_spage + i * C320p32buf_pgno, ofsAddr + Page_rxb);
2636 writew(readb(ofsAddr + Page_rxb), ofsAddr + EndPage_rxb);
2637 writew(C320p32tx_spage + i * C320p32buf_pgno, ofsAddr + Page_txb);
2638 writew(readw(ofsAddr + Page_txb), ofsAddr + EndPage_txb);
2639 }
2640 }
2641 break;
2642 }
Dirk Eibach01cfaf02006-08-27 01:23:36 -07002643 loadstat[cardno] = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 return (0);
2645}
2646
2647static int moxaloadc218(int cardno, void __iomem *baseAddr, int len)
2648{
2649 char retry;
2650 int i, j, len1, len2;
2651 ushort usum, *ptr, keycode;
2652
2653 if (moxa_boards[cardno].boardType == MOXA_BOARD_CP204J)
2654 keycode = CP204J_KeyCode;
2655 else
2656 keycode = C218_KeyCode;
2657 usum = 0;
2658 len1 = len >> 1;
2659 ptr = (ushort *) moxaBuff;
2660 for (i = 0; i < len1; i++)
Dirk Eibach01cfaf02006-08-27 01:23:36 -07002661 usum += le16_to_cpu(*(ptr + i));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 retry = 0;
2663 do {
2664 len1 = len >> 1;
2665 j = 0;
2666 while (len1) {
2667 len2 = (len1 > 2048) ? 2048 : len1;
2668 len1 -= len2;
2669 for (i = 0; i < len2 << 1; i++)
2670 writeb(moxaBuff[i + j], baseAddr + C218_LoadBuf + i);
2671 j += i;
2672
2673 writew(len2, baseAddr + C218DLoad_len);
2674 writew(0, baseAddr + C218_key);
2675 for (i = 0; i < 100; i++) {
2676 if (readw(baseAddr + C218_key) == keycode)
2677 break;
2678 moxadelay(1); /* delay 10 ms */
2679 }
2680 if (readw(baseAddr + C218_key) != keycode) {
2681 return (-1);
2682 }
2683 }
2684 writew(0, baseAddr + C218DLoad_len);
2685 writew(usum, baseAddr + C218check_sum);
2686 writew(0, baseAddr + C218_key);
2687 for (i = 0; i < 100; i++) {
2688 if (readw(baseAddr + C218_key) == keycode)
2689 break;
2690 moxadelay(1); /* delay 10 ms */
2691 }
2692 retry++;
2693 } while ((readb(baseAddr + C218chksum_ok) != 1) && (retry < 3));
2694 if (readb(baseAddr + C218chksum_ok) != 1) {
2695 return (-1);
2696 }
2697 writew(0, baseAddr + C218_key);
2698 for (i = 0; i < 100; i++) {
2699 if (readw(baseAddr + Magic_no) == Magic_code)
2700 break;
2701 moxadelay(1); /* delay 10 ms */
2702 }
2703 if (readw(baseAddr + Magic_no) != Magic_code) {
2704 return (-1);
2705 }
2706 writew(1, baseAddr + Disable_IRQ);
2707 writew(0, baseAddr + Magic_no);
2708 for (i = 0; i < 100; i++) {
2709 if (readw(baseAddr + Magic_no) == Magic_code)
2710 break;
2711 moxadelay(1); /* delay 10 ms */
2712 }
2713 if (readw(baseAddr + Magic_no) != Magic_code) {
2714 return (-1);
2715 }
2716 moxaCard = 1;
2717 moxaIntNdx[cardno] = baseAddr + IRQindex;
2718 moxaIntPend[cardno] = baseAddr + IRQpending;
2719 moxaIntTable[cardno] = baseAddr + IRQtable;
2720 return (0);
2721}
2722
2723static int moxaloadc320(int cardno, void __iomem *baseAddr, int len, int *numPorts)
2724{
2725 ushort usum;
2726 int i, j, wlen, len2, retry;
2727 ushort *uptr;
2728
2729 usum = 0;
2730 wlen = len >> 1;
2731 uptr = (ushort *) moxaBuff;
2732 for (i = 0; i < wlen; i++)
Dirk Eibach01cfaf02006-08-27 01:23:36 -07002733 usum += le16_to_cpu(uptr[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 retry = 0;
2735 j = 0;
2736 do {
2737 while (wlen) {
2738 if (wlen > 2048)
2739 len2 = 2048;
2740 else
2741 len2 = wlen;
2742 wlen -= len2;
2743 len2 <<= 1;
2744 for (i = 0; i < len2; i++)
2745 writeb(moxaBuff[j + i], baseAddr + C320_LoadBuf + i);
2746 len2 >>= 1;
2747 j += i;
2748 writew(len2, baseAddr + C320DLoad_len);
2749 writew(0, baseAddr + C320_key);
2750 for (i = 0; i < 10; i++) {
2751 if (readw(baseAddr + C320_key) == C320_KeyCode)
2752 break;
2753 moxadelay(1);
2754 }
2755 if (readw(baseAddr + C320_key) != C320_KeyCode)
2756 return (-1);
2757 }
2758 writew(0, baseAddr + C320DLoad_len);
2759 writew(usum, baseAddr + C320check_sum);
2760 writew(0, baseAddr + C320_key);
2761 for (i = 0; i < 10; i++) {
2762 if (readw(baseAddr + C320_key) == C320_KeyCode)
2763 break;
2764 moxadelay(1);
2765 }
2766 retry++;
2767 } while ((readb(baseAddr + C320chksum_ok) != 1) && (retry < 3));
2768 if (readb(baseAddr + C320chksum_ok) != 1)
2769 return (-1);
2770 writew(0, baseAddr + C320_key);
2771 for (i = 0; i < 600; i++) {
2772 if (readw(baseAddr + Magic_no) == Magic_code)
2773 break;
2774 moxadelay(1);
2775 }
2776 if (readw(baseAddr + Magic_no) != Magic_code)
2777 return (-100);
2778
2779 if (moxa_boards[cardno].busType == MOXA_BUS_TYPE_PCI) { /* ASIC board */
2780 writew(0x3800, baseAddr + TMS320_PORT1);
2781 writew(0x3900, baseAddr + TMS320_PORT2);
2782 writew(28499, baseAddr + TMS320_CLOCK);
2783 } else {
2784 writew(0x3200, baseAddr + TMS320_PORT1);
2785 writew(0x3400, baseAddr + TMS320_PORT2);
2786 writew(19999, baseAddr + TMS320_CLOCK);
2787 }
2788 writew(1, baseAddr + Disable_IRQ);
2789 writew(0, baseAddr + Magic_no);
2790 for (i = 0; i < 500; i++) {
2791 if (readw(baseAddr + Magic_no) == Magic_code)
2792 break;
2793 moxadelay(1);
2794 }
2795 if (readw(baseAddr + Magic_no) != Magic_code)
2796 return (-102);
2797
2798 j = readw(baseAddr + Module_cnt);
2799 if (j <= 0)
2800 return (-101);
2801 *numPorts = j * 8;
2802 writew(j, baseAddr + Module_no);
2803 writew(0, baseAddr + Magic_no);
2804 for (i = 0; i < 600; i++) {
2805 if (readw(baseAddr + Magic_no) == Magic_code)
2806 break;
2807 moxadelay(1);
2808 }
2809 if (readw(baseAddr + Magic_no) != Magic_code)
2810 return (-102);
2811 moxaCard = 1;
2812 moxaIntNdx[cardno] = baseAddr + IRQindex;
2813 moxaIntPend[cardno] = baseAddr + IRQpending;
2814 moxaIntTable[cardno] = baseAddr + IRQtable;
2815 return (0);
2816}
2817
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818static void MoxaSetFifo(int port, int enable)
2819{
2820 void __iomem *ofsAddr = moxaTableAddr[port];
2821
2822 if (!enable) {
2823 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 0);
2824 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 1);
2825 } else {
2826 moxafunc(ofsAddr, FC_SetRxFIFOTrig, 3);
2827 moxafunc(ofsAddr, FC_SetTxFIFOCnt, 16);
2828 }
2829}