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