MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / char / mxupcie.c
blob28c7f21f06b0746d7ea62e483ea3c703ce2a7132
1 /*
2 * mxupcie.c -- MOXA Smartio/Industio family multiport serial driver.
4 * Copyright (C) 1999-2008 Moxa Inc. (support@moxa.com).
6 * This code is loosely based on the Linux serial driver, written by
7 * Linus Torvalds, Theodore T'so and others.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #ifdef MODVERSIONS
26 #ifndef MODULE
27 #define MODULE
28 #endif
29 #endif
31 #include <linux/version.h>
32 #define VERSION_CODE(ver,rel,seq) ((ver << 16) | (rel << 8) | seq)
34 #ifdef MODULE
35 #if (LINUX_VERSION_CODE < VERSION_CODE(2,6,18))
36 #include <linux/config.h>
37 #endif
38 #ifdef MODVERSIONS
39 #include <linux/modversions.h>
40 #endif
41 #include <linux/module.h>
42 #else
43 //#define MOD_INC_USE_COUNT
44 //#define MOD_DEC_USE_COUNT
45 #endif
46 #include <linux/autoconf.h>
47 #include <linux/errno.h>
48 #include <linux/signal.h>
49 #include <linux/sched.h>
50 #include <linux/timer.h>
51 #include <linux/interrupt.h>
52 #include <linux/tty.h>
53 #include <linux/tty_flip.h>
54 #include <linux/serial.h>
55 #include <linux/serial_reg.h>
56 #include <linux/major.h>
57 #include <linux/string.h>
58 #include <linux/fcntl.h>
59 #include <linux/ptrace.h>
60 #include <linux/ioport.h>
61 #include <linux/mm.h>
62 #include <linux/smp_lock.h>
63 #include <linux/delay.h>
65 #include <asm/system.h>
66 #include <asm/io.h>
67 #include <asm/irq.h>
68 #include <asm/segment.h>
69 #include <asm/bitops.h>
72 #include "mxupcie.h"
73 #ifdef CONFIG_PCI
74 #include <linux/pci.h>
75 #endif
77 #define MXUPCIE_VERSION "1.13"
78 #define MXUPCIEMAJOR 31
79 #define MXUPCIECUMAJOR 34
81 #ifdef CONFIG_PCI
82 #include <linux/pci.h>
83 #endif /* ENABLE_PCI */
86 enum {
87 MXUPCIE_BOARD_CP102E = 1,
88 MXUPCIE_BOARD_CP102EL,
89 MXUPCIE_BOARD_CP132EL,
90 MXUPCIE_BOARD_CP114EL
93 static char *mxupcie_brdname[] = {
94 "CP-102E series",
95 "CP-102EL series",
96 "CP-132EL series",
97 "CP-114EL series"
100 static int mxupcie_numports[] = {
101 2, //CP-102E
102 2, //CP-102EL
103 2, //CP-132EL
104 4 //CP-114EL
108 #if (LINUX_VERSION_CODE < VERSION_CODE(2,6,0))
109 typedef struct {
110 unsigned short vendor;
111 unsigned short device;
112 unsigned short driver_data;
113 } mxupcie_pciinfo;
115 static mxupcie_pciinfo mxupcie_pcibrds[] = {
116 {PCI_VENDOR_ID_MOXA,PCI_DEVICE_ID_CP102E ,MXUPCIE_BOARD_CP102E},
117 {PCI_VENDOR_ID_MOXA,PCI_DEVICE_ID_CP102EL ,MXUPCIE_BOARD_CP102EL},
118 {PCI_VENDOR_ID_MOXA,PCI_DEVICE_ID_CP132EL ,MXUPCIE_BOARD_CP132EL},
119 {PCI_VENDOR_ID_MOXA,PCI_DEVICE_ID_CP114EL ,MXUPCIE_BOARD_CP114EL},
123 #else
125 static struct pci_device_id mxupcie_pcibrds[] = {
126 {PCI_VENDOR_ID_MOXA,PCI_DEVICE_ID_CP102E ,PCI_ANY_ID, PCI_ANY_ID, 0, 0,MXUPCIE_BOARD_CP102E},
127 {PCI_VENDOR_ID_MOXA,PCI_DEVICE_ID_CP102EL ,PCI_ANY_ID, PCI_ANY_ID, 0, 0,MXUPCIE_BOARD_CP102EL},
128 {PCI_VENDOR_ID_MOXA,PCI_DEVICE_ID_CP132EL ,PCI_ANY_ID, PCI_ANY_ID, 0, 0,MXUPCIE_BOARD_CP132EL},
129 {PCI_VENDOR_ID_MOXA,PCI_DEVICE_ID_CP114EL ,PCI_ANY_ID, PCI_ANY_ID, 0, 0,MXUPCIE_BOARD_CP114EL},
133 MODULE_DEVICE_TABLE(pci, mxupcie_pcibrds);
135 #endif
137 typedef struct _moxa_pci_info {
138 unsigned short busNum;
139 unsigned short devNum;
140 struct pci_dev *pdev;
141 } moxa_pci_info;
143 static int ttymajor=MXUPCIEMAJOR;
144 static int calloutmajor=MXUPCIECUMAJOR;
145 static unsigned char interface=0;
146 static unsigned char terminator=0;
149 #ifdef MODULE
150 /* Variables for insmod */
152 MODULE_AUTHOR("Eric Lo");
153 MODULE_DESCRIPTION("MOXA Smartio/Industio Family Multiport Board Device Driver");
154 MODULE_LICENSE("GPL");
156 #if defined SP2 || (LINUX_VERSION_CODE > VERSION_CODE(2,6,9))
157 int mx_ioaddr_array_num;
158 module_param(ttymajor, int, 0);
159 module_param(calloutmajor, int, 0);
160 module_param(interface, byte, 0);
161 module_param(terminator, byte, 0);
162 #else
163 MODULE_PARM(ttymajor, "i");
164 MODULE_PARM(calloutmajor, "i");
165 MODULE_PARM(interface, "b");
166 MODULE_PARM(terminator, "b");
167 #endif
169 #endif /* MODULE */
172 struct mxupcie_log {
173 int tick;
174 unsigned long rxcnt[MXUPCIE_PORTS];
175 unsigned long txcnt[MXUPCIE_PORTS];
179 struct mxupcie_mon{
180 unsigned long rxcnt;
181 unsigned long txcnt;
182 unsigned long up_rxcnt;
183 unsigned long up_txcnt;
184 int modem_status;
185 unsigned char hold_reason;
189 struct mxupcie_hwconf {
190 int board_type;
191 int ports;
192 int irq;
193 unsigned long iobar3_addr;
194 unsigned long vector_mask;
195 int uart_type;
196 unsigned char *ioaddr[MXUPCIE_PORTS_PER_BOARD];
197 int baud_base[MXUPCIE_PORTS_PER_BOARD];
198 moxa_pci_info pciInfo;
199 int IsMoxaMustChipFlag; // add by Victor Yu. 08-30-2002
200 int MaxCanSetBaudRate[MXUPCIE_PORTS_PER_BOARD]; // add by Victor Yu. 09-04-2002
201 unsigned long *opmode_ioaddr[MXUPCIE_PORTS_PER_BOARD]; // add by Victor Yu. 01-05-2004
205 struct mxupcie_struct {
206 int port;
207 unsigned char *base; /* port base address */
208 int irq; /* port using irq no. */
209 int baud_base; /* max. speed */
210 int flags; /* defined in tty.h */
211 int type; /* UART type */
212 struct tty_struct * tty;
213 int read_status_mask;
214 int ignore_status_mask;
215 int xmit_fifo_size;
216 int custom_divisor;
217 int close_delay;
218 unsigned short closing_wait;
219 int IER; /* Interrupt Enable Register */
220 int MCR; /* Modem control register */
221 unsigned long event;
222 int count; /* # of fd on device */
223 int blocked_open; /* # of blocked opens */
224 unsigned char *xmit_buf;
225 int xmit_head;
226 int xmit_tail;
227 int xmit_cnt;
228 #if (LINUX_VERSION_CODE < VERSION_CODE(2,6,0))
229 struct tq_struct tqueue;
230 #else
231 struct work_struct tqueue;
232 #endif
233 #if (LINUX_VERSION_CODE > VERSION_CODE(2,6,19))
234 struct ktermios normal_termios;
235 struct ktermios callout_termios;
236 #else
237 struct termios normal_termios;
238 struct termios callout_termios;
239 #endif
240 wait_queue_head_t open_wait;
241 wait_queue_head_t close_wait;
242 wait_queue_head_t delta_msr_wait;
243 struct async_icount icount; /* kernel counters for the 4 input interrupts */
244 int timeout;
245 int MaxCanSetBaudRate; // add by Victor Yu. 09-04-2002
246 long realbaud;
247 struct mxupcie_mon mon_data;
248 unsigned char err_shadow;
249 spinlock_t slock;
250 int speed;
251 int custom_baud_rate;
252 unsigned long iobar3_addr;
253 unsigned char UIR;
254 unsigned long UIR_addr;
255 unsigned char terminator_flag;
256 int board_type;
260 struct mxupcie_mstatus{
261 tcflag_t cflag;
262 int cts;
263 int dsr;
264 int ri;
265 int dcd;
269 static struct mxupcie_mstatus GMStatus[MXUPCIE_PORTS];
271 #if (LINUX_VERSION_CODE < VERSION_CODE(2,6,0))
272 static struct tty_driver mxvar_sdriver;
273 static struct tty_driver mxvar_cdriver;
274 #else
275 static struct tty_driver *mxvar_sdriver;
276 #endif
277 #if (LINUX_VERSION_CODE < VERSION_CODE(2,6,0))
278 static int mxvar_refcount;
279 #endif
280 static struct mxupcie_struct mxvar_table[MXUPCIE_PORTS];
281 static struct tty_struct * mxvar_tty[MXUPCIE_PORTS+1];
282 #if (LINUX_VERSION_CODE > VERSION_CODE(2,6,19))
283 static struct ktermios * mxvar_termios[MXUPCIE_PORTS+1];
284 static struct ktermios * mxvar_termios_locked[MXUPCIE_PORTS+1];
285 #else
286 static struct termios * mxvar_termios[MXUPCIE_PORTS+1];
287 static struct termios * mxvar_termios_locked[MXUPCIE_PORTS+1];
288 #endif
289 static struct mxupcie_log mxvar_log;
290 static int mxvar_diagflag;
291 static unsigned char mxupcie_msr[MXUPCIE_PORTS+1];
292 static int mxupcie_set_baud_method[MXUPCIE_PORTS+1];
293 static spinlock_t gm_lock;
296 * This is used to figure out the divisor speeds and the timeouts
298 static int mxvar_baud_table[] = {
299 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
300 9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600, 0 };
301 #define BAUD_TABLE_NO (sizeof(mxvar_baud_table)/sizeof(int))
303 struct mxupcie_hwconf mxupciecfg[MXUPCIE_BOARDS];
306 * static functions:
309 #ifdef MODULE
310 int init_module(void);
311 void cleanup_module(void);
312 #endif
314 static int mxupcie_open(struct tty_struct *, struct file *);
315 static void mxupcie_close(struct tty_struct *, struct file *);
317 #if (LINUX_VERSION_CODE < VERSION_CODE(2,6,10))
318 static int mxupcie_write(struct tty_struct *, int from_user, const unsigned char *, int);
319 #else
320 static int mxupcie_write(struct tty_struct *, const unsigned char *, int);
321 #endif
322 static void mxupcie_put_char(struct tty_struct *, unsigned char);
323 static void mxupcie_flush_chars(struct tty_struct *);
324 static int mxupcie_write_room(struct tty_struct *);
325 static int mxupcie_chars_in_buffer(struct tty_struct *);
326 static void mxupcie_flush_buffer(struct tty_struct *);
327 static int mxupcie_ioctl(struct tty_struct *, struct file *, uint, ulong);
328 static void mxupcie_throttle(struct tty_struct *);
329 static void mxupcie_unthrottle(struct tty_struct *);
330 #if (LINUX_VERSION_CODE > VERSION_CODE(2,6,19))
331 static void mxupcie_set_termios(struct tty_struct *, struct ktermios *);
332 #else
333 static void mxupcie_set_termios(struct tty_struct *, struct termios *);
334 #endif
335 static void mxupcie_stop(struct tty_struct *);
336 static void mxupcie_start(struct tty_struct *);
337 static void mxupcie_hangup(struct tty_struct *);
338 #if (LINUX_VERSION_CODE >= VERSION_CODE(2,6,0))
339 static int mxupcie_tiocmget(struct tty_struct *, struct file *);
340 static int mxupcie_tiocmset(struct tty_struct *, struct file *, unsigned int, unsigned int);
341 #else
342 static int mx_get_modem_info(struct mxupcie_struct *, unsigned int *);
343 static int mx_set_modem_info(struct mxupcie_struct *, unsigned int, unsigned int *);
344 #endif
345 static void mxupcie_rs_break(struct tty_struct *, int);
346 static void mxupcie_wait_until_sent(struct tty_struct *tty, int timeout);
348 static void mx_getcfg(int board,struct mxupcie_hwconf *hwconf);
349 int mx_init(void);
350 #ifdef CONFIG_PCI
351 static int mx_get_PCI_conf(int ,int ,int ,struct mxupcie_hwconf *);
352 #endif
353 #if (LINUX_VERSION_CODE > VERSION_CODE(2,6,19))
354 static void mx_do_softint(struct work_struct *work);
355 #else
356 static void mx_do_softint(void *);
357 #endif
358 static int mx_ioctl_special(unsigned int, unsigned long);
359 #if (LINUX_VERSION_CODE > VERSION_CODE(2,6,19))
360 static irqreturn_t mx_interrupt(int irq, void *dev_id);
361 #else
362 static IRQ_RET mx_interrupt(int, void *, struct pt_regs *);
363 #endif
364 static void mx_receive_chars(struct mxupcie_struct *, int *);
365 static void mx_transmit_chars(struct mxupcie_struct *);
366 static void mx_check_modem_status(struct mxupcie_struct *, int);
367 static int mx_block_til_ready(struct tty_struct *, struct file *, struct mxupcie_struct *);
368 static int mx_startup(struct mxupcie_struct *);
369 static void mx_shutdown(struct mxupcie_struct *);
370 #if (LINUX_VERSION_CODE > VERSION_CODE(2,6,19))
371 static int mx_change_speed(struct mxupcie_struct *, struct ktermios *old_termios);
372 #else
373 static int mx_change_speed(struct mxupcie_struct *, struct termios *old_termios);
374 #endif
375 static int mx_get_serial_info(struct mxupcie_struct *, struct serial_struct *);
376 static int mx_set_serial_info(struct mxupcie_struct *, struct serial_struct *);
377 static int mx_get_lsr_info(struct mxupcie_struct *, unsigned int *);
378 static void mx_send_break(struct mxupcie_struct *, int);
379 static int mx_set_baud(struct mxupcie_struct *info, long newspd);
380 static void mx_startrx(struct tty_struct * tty);
381 static void mx_stoprx(struct tty_struct * tty);
383 static int mx_set_interface(struct mxupcie_struct *info, unsigned char val);
384 static int mx_set_terminator(struct mxupcie_struct *info, unsigned char val);
385 static void mx_software_break_signal(struct mxupcie_struct *info, unsigned char state);
387 #if (LINUX_VERSION_CODE >= VERSION_CODE(2,6,0))
388 static struct tty_operations mxupcie_ops = {
389 .open = mxupcie_open,
390 .close = mxupcie_close,
391 .write = mxupcie_write,
392 .put_char = mxupcie_put_char,
393 .flush_chars = mxupcie_flush_chars,
394 .write_room = mxupcie_write_room,
395 .chars_in_buffer = mxupcie_chars_in_buffer,
396 .flush_buffer = mxupcie_flush_buffer,
397 .ioctl = mxupcie_ioctl,
398 .throttle = mxupcie_throttle,
399 .unthrottle = mxupcie_unthrottle,
400 .set_termios = mxupcie_set_termios,
401 .stop = mxupcie_stop,
402 .start = mxupcie_start,
403 .hangup = mxupcie_hangup,
404 .tiocmget = mxupcie_tiocmget,
405 .tiocmset = mxupcie_tiocmset,
407 #endif
411 * The MOXA Smartio/Industio serial driver boot-time initialization code!
413 INIT_FUNC_RET INIT_FUNC(void)
415 int ret;
417 ret = mx_init();
419 return ret;
422 CLEAR_FUNC_RET CLEAR_FUNC(void)
424 int i, err=0;
426 #if (LINUX_VERSION_CODE < VERSION_CODE(2,6,0))
427 if ((err |= tty_unregister_driver(&mxvar_cdriver)))
428 printk("Couldn't unregister MOXA Smartio/Industio family callout driver\n");
429 #endif
430 if ((err |= tty_unregister_driver(DRV_VAR)))
431 printk("Couldn't unregister MOXA Smartio/Industio family serial driver\n");
433 for(i=0; i<MXUPCIE_BOARDS; i++){
434 struct pci_dev *pdev;
436 if(mxupciecfg[i].board_type == -1){
437 continue;
439 else{
440 pdev = mxupciecfg[i].pciInfo.pdev;
441 free_irq(mxupciecfg[i].irq, &mxvar_table[i*MXUPCIE_PORTS_PER_BOARD]);
443 if(pdev!=NULL){ //PCI
444 iounmap(mxupciecfg[i].ioaddr[1]);
445 release_mem_region(pci_resource_start(pdev, 1), pci_resource_len(pdev, 1));
446 release_region(pci_resource_start(pdev,2), pci_resource_len(pdev,2));
453 int mxupcie_initbrd(int board,struct mxupcie_hwconf *hwconf)
455 struct mxupcie_struct * info;
456 int retval;
457 int i,n;
459 n = board*MXUPCIE_PORTS_PER_BOARD;
460 info = &mxvar_table[n];
461 #if (LINUX_VERSION_CODE < VERSION_CODE(2,6,0))
462 printk(" ttyMUE%d/cumue%d - ttyMUE%d/cumue%d ",
463 n, n, n+hwconf->ports-1, n+hwconf->ports-1);
464 #else
465 printk(" ttyMUE%d - ttyMUE%d ", n, n+hwconf->ports-1);
466 #endif
467 printk(" max. baud rate = %d bps.\n", hwconf->MaxCanSetBaudRate[0]);
469 for ( i=0; i<hwconf->ports; i++, n++, info++ ) {
470 info->port = n;
471 info->base = hwconf->ioaddr[i];
472 info->irq = hwconf->irq;
473 info->UIR = 0;
474 info->terminator_flag = 0;
475 info->iobar3_addr = hwconf->iobar3_addr;
476 info->UIR_addr = hwconf->iobar3_addr + MOXA_UIR_OFFSET + i/2;
477 info->board_type = hwconf->board_type;
479 if(i == MX_PORT4){
480 switch(hwconf->board_type){
481 case MXUPCIE_BOARD_CP114EL:
482 info->UIR_addr = hwconf->iobar3_addr + MOXA_UIR_OFFSET + 3;
483 break;
484 default:
485 break;
489 info->flags = ASYNC_SHARE_IRQ;
490 info->type = hwconf->uart_type;
491 info->baud_base = hwconf->baud_base[i];
492 info->MaxCanSetBaudRate = hwconf->MaxCanSetBaudRate[i];
493 info->xmit_fifo_size = MX_TX_FIFO_SIZE;
494 info->custom_divisor = hwconf->baud_base[i] * 16;
495 info->close_delay = 5*HZ/10;
496 info->closing_wait = 30*HZ;
497 #if (LINUX_VERSION_CODE > VERSION_CODE(2,6,19))
498 INIT_WORK(&info->tqueue, mx_do_softint);
499 #else
500 INIT_WORK(&info->tqueue, mx_do_softint, info);
501 #endif
502 #if (LINUX_VERSION_CODE < VERSION_CODE(2,6,0))
503 info->callout_termios = mxvar_cdriver.init_termios;
504 #endif
505 info->normal_termios = DRV_VAR_P(init_termios);
506 init_waitqueue_head(&info->open_wait);
507 init_waitqueue_head(&info->close_wait);
508 init_waitqueue_head(&info->delta_msr_wait);
509 info->speed = 9600;
510 memset(&info->mon_data, 0, sizeof(struct mxupcie_mon));
511 info->err_shadow = 0;
512 spin_lock_init(&info->slock);
514 MX_WRITE_IOBAR3_REG(MOXA_GPIO_SET_ALL_OUTPUT, info->iobar3_addr + MOXA_PUART_GPIO_EN);
515 mx_set_terminator(info, terminator);
517 if(!interface){
518 switch(info->board_type){
519 case MXUPCIE_BOARD_CP132EL:
520 interface = MOXA_UIR_RS485_2W;
521 break;
522 default:
523 interface = MOXA_UIR_RS232;
524 break;
528 mx_set_interface(info, interface);
533 * Allocate the IRQ if necessary
537 /* before set INT ISR, disable all int */
538 for(i=0; i<hwconf->ports; i++)
539 MX_WRITE_REG(MX_READ_REG(hwconf->ioaddr[i] + UART_IER) & 0xf0, hwconf->ioaddr[i]+UART_IER);
541 n = board*MXUPCIE_PORTS_PER_BOARD;
542 info = &mxvar_table[n];
543 retval = request_irq(hwconf->irq, mx_interrupt, IRQ_T(info), "mxupcie", info);
545 if ( retval ) {
546 printk("Board %d: %s", board, mxupcie_brdname[hwconf->board_type-1]);
547 printk(" Request irq fail,IRQ (%d) may be conflit with another device.\n",info->irq);
549 return retval;
552 return 0;
556 static void mx_getcfg(int board,struct mxupcie_hwconf *hwconf)
558 mxupciecfg[board] = *hwconf;
562 #ifdef CONFIG_PCI
563 static int mx_get_PCI_conf(int busnum,int devnum,int board_type,struct mxupcie_hwconf *hwconf)
565 int i;
566 unsigned char *ioaddress;
567 struct pci_dev *pdev=hwconf->pciInfo.pdev;
568 unsigned long iobar_addr;
570 //io address
571 hwconf->board_type = board_type;
572 hwconf->ports = mxupcie_numports[board_type-1];
574 request_mem_region(pci_resource_start(pdev, 1),
575 pci_resource_len(pdev, 1),
576 "mxupcie(MEM)");
578 ioaddress = ioremap(pci_resource_start(pdev,1),
579 pci_resource_len(pdev,1));
581 iobar_addr = pci_resource_start(pdev, 2);
582 request_region(pci_resource_start(pdev, 2),
583 pci_resource_len(pdev, 2),
584 "mxupcie(IOBAR3)");
586 for (i = 0; i < hwconf->ports; i++) {
587 hwconf->ioaddr[i] = ioaddress + (i * MX_PUART_SIZE);
588 if(i == 3){
589 switch(board_type){
590 case MXUPCIE_BOARD_CP114EL:
591 hwconf->ioaddr[i] = ioaddress + (MX_PORT8 * MX_PUART_SIZE);
592 break;
593 default:
594 break;
599 //irq
600 hwconf->irq = hwconf->pciInfo.pdev->irq;
601 hwconf->iobar3_addr = iobar_addr;
603 hwconf->uart_type = PORT_16550A;
604 hwconf->IsMoxaMustChipFlag = MOXA_PUART_HWID;
606 for (i = 0; i < hwconf->ports; i++) {
607 hwconf->baud_base[i] = 921600;
608 hwconf->MaxCanSetBaudRate[i] = 921600;
611 return 0;
613 #endif
615 int mx_init(void)
617 int i, m, retval, b;
618 int ret1, ret2;
619 #ifdef CONFIG_PCI
620 struct pci_dev *pdev=NULL;
621 int index;
622 unsigned char busnum,devnum;
623 #endif
624 struct mxupcie_hwconf hwconf;
626 #if (LINUX_VERSION_CODE >= VERSION_CODE(2,6,0))
627 mxvar_sdriver = alloc_tty_driver(MXUPCIE_PORTS + 1);
628 if (!mxvar_sdriver)
629 return -ENOMEM;
630 #endif
631 spin_lock_init(&gm_lock);
633 for(i=0; i<MXUPCIE_BOARDS; i++)
634 mxupciecfg[i].board_type = -1;
636 printk("MOXA Smartio/Industio family driver version %s\n",MXUPCIE_VERSION);
638 /* Initialize the tty_driver structure */
639 memset(DRV_VAR, 0, sizeof(struct tty_driver));
640 DRV_VAR_P(magic) = TTY_DRIVER_MAGIC;
641 DRV_VAR_P(name) = "ttyMUE";
642 DRV_VAR_P(major) = ttymajor;
643 DRV_VAR_P(minor_start) = 0;
644 DRV_VAR_P(num) = MXUPCIE_PORTS + 1;
645 DRV_VAR_P(type) = TTY_DRIVER_TYPE_SERIAL;
646 DRV_VAR_P(subtype) = SERIAL_TYPE_NORMAL;
647 DRV_VAR_P(init_termios) = tty_std_termios;
648 DRV_VAR_P(init_termios.c_cflag) = B9600|CS8|CREAD|HUPCL|CLOCAL;
649 DRV_VAR_P(flags) = TTY_DRIVER_REAL_RAW;
650 #if (LINUX_VERSION_CODE >= VERSION_CODE(2,6,0))
651 tty_set_operations(DRV_VAR, &mxupcie_ops);
652 DRV_VAR_P(ttys) = mxvar_tty;
653 #else
654 DRV_VAR_P(refcount) = &mxvar_refcount;
655 DRV_VAR_P(table) = mxvar_tty;
656 #endif
657 DRV_VAR_P(termios) = mxvar_termios;
658 DRV_VAR_P(termios_locked) = mxvar_termios_locked;
660 DRV_VAR_P(open) = mxupcie_open;
661 DRV_VAR_P(close) = mxupcie_close;
662 DRV_VAR_P(write) = mxupcie_write;
663 DRV_VAR_P(put_char) = mxupcie_put_char;
664 DRV_VAR_P(flush_chars) = mxupcie_flush_chars;
665 DRV_VAR_P(write_room) = mxupcie_write_room;
666 DRV_VAR_P(chars_in_buffer) = mxupcie_chars_in_buffer;
667 DRV_VAR_P(flush_buffer) = mxupcie_flush_buffer;
668 DRV_VAR_P(ioctl) = mxupcie_ioctl;
669 DRV_VAR_P(throttle) = mxupcie_throttle;
670 DRV_VAR_P(unthrottle) = mxupcie_unthrottle;
671 DRV_VAR_P(set_termios) = mxupcie_set_termios;
672 DRV_VAR_P(stop) = mxupcie_stop;
673 DRV_VAR_P(start) = mxupcie_start;
674 DRV_VAR_P(hangup) = mxupcie_hangup;
675 DRV_VAR_P(break_ctl) = mxupcie_rs_break;
676 DRV_VAR_P(wait_until_sent) = mxupcie_wait_until_sent;
678 #if (LINUX_VERSION_CODE < VERSION_CODE(2,6,0))
680 * The callout device is just like normal device except for
681 * major number and the subtype code.
683 mxvar_cdriver = mxvar_sdriver;
684 mxvar_cdriver.name = "cumue";
685 mxvar_cdriver.major = calloutmajor;
686 mxvar_cdriver.subtype = SERIAL_TYPE_CALLOUT;
688 printk("Tty devices major number = %d, callout devices major number = %d\n",ttymajor,calloutmajor);
689 #endif
690 mxvar_diagflag = 0;
691 memset(mxvar_table, 0, MXUPCIE_PORTS * sizeof(struct mxupcie_struct));
692 memset(&mxvar_log, 0, sizeof(struct mxupcie_log));
694 memset(&mxupcie_msr, 0, sizeof(unsigned char) * (MXUPCIE_PORTS+1));
695 memset(&mxupcie_set_baud_method, 0, sizeof(int) * (MXUPCIE_PORTS+1));
696 memset(&hwconf, 0, sizeof(struct mxupcie_hwconf));
699 /* start finding PCI board here */
700 #ifdef CONFIG_PCI
701 #if (LINUX_VERSION_CODE < VERSION_CODE(2,6,0))
702 if (pci_present()) {
703 #else
705 #endif
706 int n = (sizeof(mxupcie_pcibrds) / sizeof(mxupcie_pcibrds[0])) - 1;
707 index = 0;
708 b = 0;
709 m = 0;
711 while (b < n) {
712 #if (LINUX_VERSION_CODE < VERSION_CODE(2,6,21))
713 pdev = pci_find_device(mxupcie_pcibrds[b].vendor,
714 mxupcie_pcibrds[b].device, pdev);
715 #else
716 pdev = pci_get_device(mxupcie_pcibrds[b].vendor,
717 mxupcie_pcibrds[b].device, pdev);
718 #endif
720 if(pdev==NULL){
721 b++;
722 continue;
725 hwconf.pciInfo.busNum = busnum = pdev->bus->number;
726 hwconf.pciInfo.devNum = devnum = PCI_SLOT(pdev->devfn)<<3;
727 hwconf.pciInfo.pdev = pdev;
728 printk("Found MOXA %s board(BusNo=%d,DevNo=%d)\n",mxupcie_brdname[(int)(mxupcie_pcibrds[b].driver_data)-1],busnum,devnum >> 3);
729 index++;
731 if ( m >= MXUPCIE_BOARDS) {
732 printk("Too many Smartio/Industio family boards find (maximum %d),board not configured\n",MXUPCIE_BOARDS);
734 else {
735 if ( pci_enable_device(pdev) ) {
736 printk("Moxa SmartI/O PCI enable fail !\n");
737 continue;
739 retval = mx_get_PCI_conf(busnum,devnum,
740 (int)mxupcie_pcibrds[b].driver_data,&hwconf);
741 if (retval < 0) {
742 if (retval == MXUPCIE_ERR_IRQ)
743 printk("Invalid interrupt number,board not configured\n");
744 else if (retval == MXUPCIE_ERR_IRQ_CONFLIT)
745 printk("Invalid interrupt number,board not configured\n");
746 else if (retval == MXUPCIE_ERR_VECTOR)
747 printk("Invalid interrupt vector,board not configured\n");
748 else if (retval == MXUPCIE_ERR_IOADDR)
749 printk("Invalid I/O address,board not configured\n");
750 continue;
754 mx_getcfg(m,&hwconf);
755 //init mxupciecfg first, or mxupciecfg data is not correct on ISR.
756 //mxupcie_initbrd will hook ISR.
757 if(mxupcie_initbrd(m,&hwconf)<0)
758 continue;
759 m++;
763 #endif
767 ret1 = 0;
768 ret2 = 0;
770 if ( !(ret1=tty_register_driver(DRV_VAR)) ){
771 #if (LINUX_VERSION_CODE < VERSION_CODE(2,6,0))
772 if ( !(ret2=tty_register_driver(&mxvar_cdriver)) ){
774 return 0;
775 }else{
776 tty_unregister_driver(DRV_VAR);
777 printk("Couldn't install MOXA Smartio/Industio family callout driver !\n");
779 #else
780 return 0;
781 #endif
782 }else
783 printk("Couldn't install MOXA Smartio/Industio family driver !\n");
786 if(ret1 || ret2){
787 for(i=0; i<MXUPCIE_BOARDS; i++){
788 if(mxupciecfg[i].board_type == -1)
789 continue;
790 else{
791 free_irq(mxupciecfg[i].irq, &mxvar_table[i*MXUPCIE_PORTS_PER_BOARD]);
792 //todo: release io, vector
796 return -1;
799 return 0;
803 #if (LINUX_VERSION_CODE > VERSION_CODE(2,6,19))
804 static void mx_do_softint(struct work_struct *work)
806 struct mxupcie_struct *info = container_of(work,
807 struct mxupcie_struct, tqueue);
808 #else
809 static void mx_do_softint(void *private_)
811 struct mxupcie_struct * info = (struct mxupcie_struct *)private_;
812 #endif
813 struct tty_struct * tty;
815 tty = info->tty;
817 #if (LINUX_VERSION_CODE < VERSION_CODE(2,6,9))
818 if ( test_and_clear_bit(MXUPCIE_EVENT_TXLOW, &info->event) ) {
819 if ( (tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) && tty->ldisc.write_wakeup )
820 (tty->ldisc.write_wakeup)(tty);
821 wake_up_interruptible(&tty->write_wait);
823 #else
824 if ( test_and_clear_bit(MXUPCIE_EVENT_TXLOW, &info->event) )
825 tty_wakeup(tty);
826 #endif
827 if ( test_and_clear_bit(MXUPCIE_EVENT_HANGUP, &info->event) )
828 tty_hangup(tty);
830 #if (LINUX_VERSION_CODE < VERSION_CODE(2,6,0))
831 MX_MOD_DEC;
832 #endif
836 #if (LINUX_VERSION_CODE < VERSION_CODE(2,6,0))
837 static unsigned char mxupcie_get_msr(unsigned char *baseaddr, int mode, int port, struct mxupcie_struct *info)
839 unsigned char status=0;
841 status = MX_READ_REG(baseaddr + UART_MSR);
842 mxupcie_msr[port] &= 0x0F;
843 mxupcie_msr[port] |= status;
844 status = mxupcie_msr[port];
846 if( mode )
847 mxupcie_msr[port] = 0;
849 return status;
851 #endif
855 * This routine is called whenever a serial port is opened. It
856 * enables interrupts for a serial port, linking in its async structure into
857 * the IRQ chain. It also performs the serial-specific
858 * initialization for the tty structure.
860 static int mxupcie_open(struct tty_struct * tty, struct file * filp)
862 struct mxupcie_struct * info;
863 int retval, line;
865 MX_LOCK_INIT();
867 line = PORTNO(tty);
869 if ( line == MXUPCIE_PORTS )
870 return 0;
872 MX_MOD_INC;
874 if ( (line < 0) || (line > MXUPCIE_PORTS) )
875 return -ENODEV;
877 info = mxvar_table + line;
879 if ( !info->base )
880 return -ENODEV;
882 tty->driver_data = info;
883 info->tty = tty;
885 //printk("port %d, mxupcie_open\r\n", info->port);
887 * Start up serial port
889 info->count++;
891 retval = mx_startup(info);
893 if ( retval )
894 return retval;
896 retval = mx_block_til_ready(tty, filp, info);
898 if ( retval )
899 return retval;
901 if ( (info->count == 1) && (info->flags & ASYNC_SPLIT_TERMIOS) ) {
902 if ( MX_TTY_DRV(subtype) == SERIAL_TYPE_NORMAL )
903 *tty->termios = info->normal_termios;
904 else
905 *tty->termios = info->callout_termios;
907 MX_LOCK(&info->slock);
908 mx_change_speed(info, 0);
909 MX_UNLOCK(&info->slock);
912 #ifdef TTY_DONT_FLIP
913 clear_bit(TTY_DONT_FLIP, &tty->flags); // since VERSION_CODE >= 2.6.18
914 #endif
916 /* unmark here for very high baud rate (ex. 921600 bps) used
918 #if (LINUX_VERSION_CODE >= VERSION_CODE(2,1,0))
919 tty->low_latency = 1;
920 #endif
921 return 0;
926 * This routine is called when the serial port gets closed. First, we
927 * wait for the last remaining data to be sent. Then, we unlink its
928 * async structure from the interrupt chain if necessary, and we free
929 * that IRQ if nothing is left in the chain.
931 static void mxupcie_close(struct tty_struct * tty, struct file * filp)
933 struct mxupcie_struct * info = (struct mxupcie_struct *)tty->driver_data;
934 #if (LINUX_VERSION_CODE > VERSION_CODE(2,6,9))
935 struct tty_ldisc *ld;
936 #endif
937 unsigned long timeout;
938 unsigned char reg_flag;
939 MX_LOCK_INIT();
941 if ( PORTNO(tty) == MXUPCIE_PORTS )
942 return;
944 if ( !info ){
945 MX_MOD_DEC;
947 return;
950 MX_LOCK(&info->slock);
952 if ( tty_hung_up_p(filp) ) {
953 MX_UNLOCK(&info->slock);
954 MX_MOD_DEC;
956 return;
959 #ifndef SP1
960 if ( (tty->count == 1) && (info->count != 1) ) {
961 #else
962 #if (LINUX_VERSION_CODE < VERSION_CODE(2,4,21))
963 if ( (tty->count == 1) && (info->count != 1) ) {
964 #else
965 if ((atomic_read(&tty->count) == 1) && (info->count != 1)) {
966 #endif
967 #endif
969 * Uh, oh. tty->count is 1, which means that the tty
970 * structure will be freed. Info->count should always
971 * be one in these conditions. If it's greater than
972 * one, we've got real problems, since it means the
973 * serial port won't be shutdown.
975 printk("mxupcie_close: bad serial port count; tty->count is 1, "
976 "info->count is %d\n", info->count);
977 info->count = 1;
980 if ( --info->count < 0 ) {
981 printk("mxupcie_close: bad serial port count for ttys%d: %d\n",
982 info->port, info->count);
983 info->count = 0;
986 if ( info->count ) {
987 MX_UNLOCK(&info->slock);
988 MX_MOD_DEC;
990 return;
993 info->flags |= ASYNC_CLOSING;
994 MX_UNLOCK(&info->slock);
996 * Save the termios structure, since this port may have
997 * separate termios for callout and dialin.
999 if ( info->flags & ASYNC_NORMAL_ACTIVE )
1000 info->normal_termios = *tty->termios;
1002 if ( info->flags & ASYNC_CALLOUT_ACTIVE )
1003 info->callout_termios = *tty->termios;
1005 * Now we wait for the transmit buffer to clear; and we notify
1006 * the line discipline to only process XON/XOFF characters.
1009 reg_flag = MX_READ_REG(info->base + MOXA_PUART_EFR);
1010 reg_flag &= ~MOXA_EFR_AUTO_RTS;
1011 MX_WRITE_REG(reg_flag, info->base + MOXA_PUART_EFR);
1012 tty->closing = 1;
1014 if ( info->closing_wait != ASYNC_CLOSING_WAIT_NONE )
1015 tty_wait_until_sent(tty, info->closing_wait);
1017 * At this point we stop accepting input. To do this, we
1018 * disable the receive line status interrupts, and tell the
1019 * interrupt driver to stop checking the data ready bit in the
1020 * line status register.
1022 info->IER &= ~UART_IER_RLSI;
1024 if ( info->flags & ASYNC_INITIALIZED ) {
1025 MX_WRITE_REG(info->IER, info->base + UART_IER);
1027 * Before we drop DTR, make sure the UART transmitter
1028 * has completely drained; this is especially
1029 * important if there is a transmit FIFO!
1031 timeout = jiffies + HZ;
1032 while ( mxupcie_chars_in_buffer(tty) ) {
1033 set_current_state(TASK_INTERRUPTIBLE);
1034 schedule_timeout(5);
1035 set_current_state(TASK_RUNNING);
1037 if ( time_after(jiffies, timeout) )
1038 break;
1042 mx_shutdown(info);
1044 if ( MX_TTY_DRV(flush_buffer) )
1045 MX_TTY_DRV(flush_buffer)(tty);
1047 #if (LINUX_VERSION_CODE < VERSION_CODE(2,6,10))
1048 if ( tty->ldisc.flush_buffer )
1049 tty->ldisc.flush_buffer(tty);
1050 #else
1051 ld = tty_ldisc_ref(tty);
1052 if (ld) {
1053 if(ld->flush_buffer)
1054 ld->flush_buffer(tty);
1056 tty_ldisc_deref(ld);
1058 #endif
1060 tty->closing = 0;
1061 info->event = 0;
1062 info->tty = 0;
1064 if ( info->blocked_open ) {
1065 if ( info->close_delay ) {
1066 set_current_state(TASK_INTERRUPTIBLE);
1067 schedule_timeout(info->close_delay);
1068 set_current_state(TASK_RUNNING);
1071 wake_up_interruptible(&info->open_wait);
1074 info->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CALLOUT_ACTIVE | ASYNC_CLOSING);
1075 wake_up_interruptible(&info->close_wait);
1077 MX_MOD_DEC;
1080 #if (LINUX_VERSION_CODE < VERSION_CODE(2,6,10))
1081 static int mxupcie_write(struct tty_struct * tty, int from_user,
1082 const unsigned char * buf, int count)
1083 #else
1084 static int mxupcie_write(struct tty_struct * tty,
1085 const unsigned char * buf, int count)
1086 #endif
1088 int c, total = 0;
1089 struct mxupcie_struct *info = (struct mxupcie_struct *)tty->driver_data;
1090 MX_LOCK_INIT();
1092 if ( !tty || !info->xmit_buf )
1093 return(0);
1096 while ( 1 ) {
1097 c = MIN(count, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1098 SERIAL_XMIT_SIZE - info->xmit_head));
1099 if ( c <= 0 )
1100 break;
1102 #if (LINUX_VERSION_CODE < VERSION_CODE(2,6,10))
1103 if ( from_user ) {
1104 if(copy_from_user(info->xmit_buf+info->xmit_head, buf, c)==c){
1105 total = -EFAULT;
1106 break;
1109 } else
1110 memcpy(info->xmit_buf + info->xmit_head, buf, c);
1111 #else
1112 memcpy(info->xmit_buf + info->xmit_head, buf, c);
1113 #endif
1114 MX_LOCK(&info->slock);
1115 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE - 1);
1116 info->xmit_cnt += c;
1117 MX_UNLOCK(&info->slock);
1119 buf += c;
1120 count -= c;
1121 total += c;
1124 if ( info->xmit_cnt && !tty->stopped ) {
1125 MX_LOCK(&info->slock);
1126 info->IER &= ~UART_IER_THRI ;
1127 MX_WRITE_REG(info->IER, info->base + UART_IER);
1128 info->IER |= UART_IER_THRI;
1129 MX_WRITE_REG(info->IER, info->base + UART_IER);
1130 MX_UNLOCK(&info->slock);
1133 //printk("%lu, mxupcie_write=%d\n", jiffies, total);
1135 return total;
1138 static void mxupcie_put_char(struct tty_struct * tty, unsigned char ch)
1140 struct mxupcie_struct *info = (struct mxupcie_struct *)tty->driver_data;
1141 MX_LOCK_INIT();
1143 if ( !tty || !info->xmit_buf )
1144 return;
1147 if ( info->xmit_cnt >= SERIAL_XMIT_SIZE - 1 )
1148 return;
1150 MX_LOCK(&info->slock);
1151 info->xmit_buf[info->xmit_head++] = ch;
1152 info->xmit_head &= SERIAL_XMIT_SIZE - 1;
1153 info->xmit_cnt++;
1154 MX_UNLOCK(&info->slock);
1156 if ( !tty->stopped ) {
1157 if (!tty->hw_stopped) {
1158 MX_LOCK(&info->slock);
1159 info->IER &= ~UART_IER_THRI ;
1160 MX_WRITE_REG(info->IER, info->base + UART_IER);
1161 info->IER |= UART_IER_THRI;
1162 MX_WRITE_REG(info->IER, info->base + UART_IER);
1163 MX_UNLOCK(&info->slock);
1166 //printk("%lu, mxupcie_put_char=%x\n", jiffies, ch);
1170 static void mxupcie_flush_chars(struct tty_struct * tty)
1172 struct mxupcie_struct *info = (struct mxupcie_struct *)tty->driver_data;
1173 MX_LOCK_INIT();
1175 //printk("%lu, mxupcie_flush_chars\n", jiffies);
1177 if ( info->xmit_cnt <= 0 || tty->stopped || !info->xmit_buf)
1178 return;
1180 MX_LOCK(&info->slock);
1181 info->IER &= ~UART_IER_THRI;
1182 MX_WRITE_REG(info->IER, info->base + UART_IER);
1183 info->IER |= UART_IER_THRI;
1184 MX_WRITE_REG(info->IER, info->base + UART_IER);
1185 MX_UNLOCK(&info->slock);
1189 static int mxupcie_write_room(struct tty_struct * tty)
1191 struct mxupcie_struct *info = (struct mxupcie_struct *)tty->driver_data;
1192 int ret;
1194 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1196 if ( ret < 0 )
1197 ret = 0;
1199 return ret;
1203 static int mxupcie_chars_in_buffer(struct tty_struct * tty)
1205 int len;
1206 struct mxupcie_struct *info = (struct mxupcie_struct *)tty->driver_data;
1207 unsigned char t_cnt;
1209 len = info->xmit_cnt;
1210 t_cnt = MX_READ_REG(info->base + MOXA_PUART_TCNT);
1212 if(t_cnt)
1213 len+=(int)t_cnt;
1214 //printk("%lu, mxupcie_chars_in_buffer=%d\n", jiffies, len);
1215 return len;
1219 static void mxupcie_flush_buffer(struct tty_struct * tty)
1221 struct mxupcie_struct *info = (struct mxupcie_struct *)tty->driver_data;
1222 char fcr;
1223 MX_LOCK_INIT();
1225 //printk("%lu, flush_buffer\n", jiffies);
1227 MX_LOCK(&info->slock);
1228 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1229 MX_UNLOCK(&info->slock);
1231 fcr = MX_READ_REG(info->base + UART_FCR);
1232 MX_WRITE_REG((fcr | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),
1233 info->base + UART_FCR);
1234 MX_WRITE_REG(fcr, info->base+UART_FCR);
1236 wake_up_interruptible(&tty->write_wait);
1238 if ( (tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) && tty->ldisc.write_wakeup )
1239 (tty->ldisc.write_wakeup)(tty);
1243 static int mxupcie_ioctl(struct tty_struct * tty, struct file * file,
1244 unsigned int cmd, unsigned long arg)
1246 int error;
1247 struct mxupcie_struct * info = (struct mxupcie_struct *)tty->driver_data;
1248 int retval;
1249 struct async_icount cprev, cnow; /* kernel counter temps */
1250 struct serial_icounter_struct *p_cuser; /* user space */
1251 unsigned long templ;
1252 MX_LOCK_INIT();
1254 //printk("%lu, mxupcie_ioctl=%x\n", jiffies, cmd);
1256 if ( PORTNO(tty) == MXUPCIE_PORTS )
1257 return(mx_ioctl_special(cmd, arg));
1259 if ( cmd == MOXA_SET_SPECIAL_BAUD_RATE || cmd == MOXA_GET_SPECIAL_BAUD_RATE || cmd == SMARTIO_SET_SPECIAL_BAUD_RATE || cmd == SMARTIO_GET_SPECIAL_BAUD_RATE ) {
1260 int speed, i;
1261 if ( cmd == MOXA_SET_SPECIAL_BAUD_RATE || cmd == SMARTIO_SET_SPECIAL_BAUD_RATE ) {
1262 error = MX_ACCESS_CHK(VERIFY_READ, (void *)arg, sizeof(int));
1264 if ( MX_ERR(error) )
1265 return(error);
1267 get_from_user(speed,(int *)arg);
1269 if ( speed <= 0 || speed > info->MaxCanSetBaudRate )
1270 return -EFAULT;
1272 if ( !info->tty || !info->tty->termios || !info->base )
1273 return 0;
1275 info->tty->termios->c_cflag &= ~(CBAUD | CBAUDEX);
1277 for ( i=0; i<BAUD_TABLE_NO && speed != mxvar_baud_table[i]; i++ );
1279 if ( i == BAUD_TABLE_NO ) {
1280 info->tty->termios->c_cflag |= B4000000;
1281 } else {
1282 switch ( mxvar_baud_table[i] ) {
1283 case 921600 : info->tty->termios->c_cflag |= B921600; break;
1284 case 460800 : info->tty->termios->c_cflag |= B460800; break;
1285 case 230400 : info->tty->termios->c_cflag |= B230400; break;
1286 case 115200 : info->tty->termios->c_cflag |= B115200; break;
1287 case 57600 : info->tty->termios->c_cflag |= B57600; break;
1288 case 38400 : info->tty->termios->c_cflag |= B38400; break;
1289 case 19200 : info->tty->termios->c_cflag |= B19200; break;
1290 case 9600 : info->tty->termios->c_cflag |= B9600; break;
1291 case 4800 : info->tty->termios->c_cflag |= B4800; break;
1292 case 2400 : info->tty->termios->c_cflag |= B2400; break;
1293 case 1800 : info->tty->termios->c_cflag |= B1800; break;
1294 case 1200 : info->tty->termios->c_cflag |= B1200; break;
1295 case 600 : info->tty->termios->c_cflag |= B600; break;
1296 case 300 : info->tty->termios->c_cflag |= B300; break;
1297 case 200 : info->tty->termios->c_cflag |= B200; break;
1298 case 150 : info->tty->termios->c_cflag |= B150; break;
1299 case 134 : info->tty->termios->c_cflag |= B134; break;
1300 case 110 : info->tty->termios->c_cflag |= B110; break;
1301 case 75 : info->tty->termios->c_cflag |= B75; break;
1302 case 50 : info->tty->termios->c_cflag |= B50; break;
1306 info->speed = speed;
1307 MX_LOCK(&info->slock);
1308 mx_change_speed(info, 0);
1309 MX_UNLOCK(&info->slock);
1310 } else {
1311 error = MX_ACCESS_CHK(VERIFY_WRITE, (void *)arg, sizeof(int));
1313 if ( MX_ERR(error) )
1314 return(error);
1316 if(copy_to_user((int*)arg, &info->speed, sizeof(int)))
1317 return -EFAULT;
1320 return 0;
1323 if ( (cmd != TIOCGSERIAL) && (cmd != TIOCMIWAIT) &&
1324 (cmd != TIOCGICOUNT) ) {
1326 if ( tty->flags & (1 << TTY_IO_ERROR) ){
1327 return -EIO;
1331 switch ( cmd ) {
1332 case TCSBRK: /* SVID version: non-zero arg --> no break */
1333 retval = tty_check_change(tty);
1335 if ( retval )
1336 return retval;
1338 tty_wait_until_sent(tty, 0);
1340 if ( !arg )
1341 mx_send_break(info, HZ/4); /* 1/4 second */
1343 return 0;
1344 case TCSBRKP: /* support for POSIX tcsendbreak() */
1345 retval = tty_check_change(tty);
1347 if ( retval )
1348 return retval;
1350 tty_wait_until_sent(tty, 0);
1351 mx_send_break(info, arg ? arg*(HZ/10) : HZ/4);
1353 return 0;
1354 case TIOCGSOFTCAR:
1355 error = MX_ACCESS_CHK(VERIFY_WRITE, (void *)arg, sizeof(long));
1357 if ( MX_ERR(error) )
1358 return error;
1360 put_to_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long *)arg);
1362 return 0;
1363 case TIOCSSOFTCAR:
1364 error = MX_ACCESS_CHK(VERIFY_READ, (void *)arg, sizeof(long));
1366 if ( MX_ERR(error) )
1367 return error;
1369 get_from_user(templ,(unsigned long *)arg);
1370 arg = templ;
1371 tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) | (arg ? CLOCAL : 0));
1373 return 0;
1374 #if (LINUX_VERSION_CODE < VERSION_CODE(2,6,0))
1375 case TIOCMGET:
1376 error = MX_ACCESS_CHK(VERIFY_WRITE, (void *)arg, sizeof(unsigned int));
1378 if ( MX_ERR(error) )
1379 return error;
1381 return mx_get_modem_info(info, (unsigned int *)arg);
1382 case TIOCMBIS:
1383 case TIOCMBIC:
1384 case TIOCMSET:
1385 return mx_set_modem_info(info, cmd, (unsigned int *)arg);
1386 #endif
1387 case TIOCGSERIAL:
1388 error = MX_ACCESS_CHK(VERIFY_WRITE, (void *)arg,sizeof(struct serial_struct));
1390 if ( MX_ERR(error) )
1391 return error;
1393 return mx_get_serial_info(info, (struct serial_struct *)arg);
1394 case TIOCSSERIAL:
1395 error = MX_ACCESS_CHK(VERIFY_READ, (void *)arg,sizeof(struct serial_struct));
1397 if ( MX_ERR(error) )
1398 return error;
1400 return mx_set_serial_info(info, (struct serial_struct *)arg);
1401 case TIOCSERGETLSR: /* Get line status register */
1402 error = MX_ACCESS_CHK(VERIFY_WRITE, (void *)arg,sizeof(unsigned int));
1404 if ( MX_ERR(error) )
1405 return error;
1406 else
1407 return mx_get_lsr_info(info, (unsigned int *)arg);
1409 * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1410 * - mask passed in arg for lines of interest
1411 * (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1412 * Caller should use TIOCGICOUNT to see which one it was
1414 case TIOCMIWAIT:{
1415 DECLARE_WAITQUEUE(wait, current);
1416 int ret;
1418 MX_LOCK(&info->slock);
1419 cprev = info->icount; /* note the counters on entry */
1420 MX_UNLOCK(&info->slock);
1422 add_wait_queue(&info->delta_msr_wait, &wait);
1423 while ( 1 ) {
1424 MX_LOCK(&info->slock);
1425 cnow = info->icount; /* atomic copy */
1426 MX_UNLOCK(&info->slock);
1427 set_current_state(TASK_INTERRUPTIBLE);
1429 if ( ((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1430 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1431 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
1432 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
1433 ret = 0;
1434 break;
1436 /* see if a signal did it */
1437 if ( signal_pending(current) ){
1438 ret = -ERESTARTSYS;
1439 break;
1442 cprev = cnow;
1445 set_current_state(TASK_RUNNING);
1446 remove_wait_queue(&info->delta_msr_wait, &wait);
1447 break;
1449 /* NOTREACHED */
1451 * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1452 * Return: write counters to the user passed counter struct
1453 * NB: both 1->0 and 0->1 transitions are counted except for
1454 * RI where only 0->1 is counted.
1456 case TIOCGICOUNT:
1457 error = MX_ACCESS_CHK(VERIFY_WRITE, (void *)arg,sizeof(struct serial_icounter_struct));
1459 if ( MX_ERR(error) )
1460 return error;
1462 MX_LOCK(&info->slock);
1463 cnow = info->icount;
1464 MX_UNLOCK(&info->slock);
1465 p_cuser = (struct serial_icounter_struct *)arg;
1467 if (put_user(cnow.frame, &p_cuser->frame))
1468 return -EFAULT;
1470 if (put_user(cnow.brk, &p_cuser->brk))
1471 return -EFAULT;
1473 if (put_user(cnow.overrun, &p_cuser->overrun))
1474 return -EFAULT;
1476 if (put_user(cnow.buf_overrun, &p_cuser->buf_overrun))
1477 return -EFAULT;
1479 if (put_user(cnow.parity, &p_cuser->parity))
1480 return -EFAULT;
1482 if (put_user(cnow.rx, &p_cuser->rx))
1483 return -EFAULT;
1485 if (put_user(cnow.tx, &p_cuser->tx))
1486 return -EFAULT;
1488 put_to_user(cnow.cts, &p_cuser->cts);
1489 put_to_user(cnow.dsr, &p_cuser->dsr);
1490 put_to_user(cnow.rng, &p_cuser->rng);
1491 put_to_user(cnow.dcd, &p_cuser->dcd);
1493 return 0;
1495 case SMARTIO_PUART_SET_INTERFACE:
1497 return mx_set_interface(info, (unsigned char)arg);
1499 case SMARTIO_PUART_GET_INTERFACE:
1501 if(copy_to_user((void*)arg,&info->UIR,sizeof(info->UIR)))
1502 return -EFAULT;
1504 return 0;
1506 case SMARTIO_PUART_SET_TERMINATOR:
1508 return mx_set_terminator(info, (unsigned char)arg);
1510 case SMARTIO_PUART_GET_TERMINATOR:
1512 if(copy_to_user((void*)arg,&info->terminator_flag,sizeof(info->terminator_flag)))
1513 return -EFAULT;
1515 return 0;
1517 default:
1518 return(-ENOIOCTLCMD);
1521 return 0;
1524 static int mx_ioctl_special(unsigned int cmd, unsigned long arg)
1526 int error, i, result, status;
1528 switch ( cmd ) {
1529 case MOXA_GET_CONF:
1530 error = MX_ACCESS_CHK(VERIFY_WRITE, (void *)arg,sizeof(struct mxupcie_hwconf)*4);
1532 if ( MX_ERR(error) )
1533 return error;
1535 if(copy_to_user((struct mxupcie_hwconf *)arg, mxupciecfg,sizeof(struct mxupcie_hwconf)*4))
1536 return -EFAULT;
1538 return 0;
1540 case MOXA_GET_MAJOR:
1541 error = MX_ACCESS_CHK(VERIFY_WRITE, (void *)arg, sizeof(int));
1543 if ( MX_ERR(error) )
1544 return error;
1546 if(copy_to_user((int*)arg, &ttymajor, sizeof(int)))
1547 return -EFAULT;
1549 return 0;
1551 case MOXA_GET_CUMAJOR:
1552 error = MX_ACCESS_CHK(VERIFY_WRITE, (void *)arg, sizeof(int));
1554 if ( MX_ERR(error) )
1555 return error;
1557 if(copy_to_user((int*)arg, &calloutmajor, sizeof(int)))
1558 return -EFAULT;
1560 return 0;
1562 case MOXA_CHKPORTENABLE:
1563 error = MX_ACCESS_CHK(VERIFY_WRITE, (void *)arg, sizeof(long));
1565 if ( MX_ERR(error) )
1566 return error;
1568 result = 0;
1570 for ( i=0; i<MXUPCIE_PORTS; i++ ) {
1571 if ( mxvar_table[i].base )
1572 result |= (1 << i);
1575 put_to_user(result, (unsigned long *)arg);
1577 return 0;
1579 case MOXA_GETDATACOUNT:
1580 error = MX_ACCESS_CHK(VERIFY_WRITE, (void *)arg,sizeof(struct mxupcie_log));
1582 if ( MX_ERR(error) )
1583 return error;
1585 if(copy_to_user((struct mxupcie_log *)arg, &mxvar_log, sizeof(mxvar_log)))
1586 return -EFAULT;
1588 return 0;
1590 case MOXA_GETMSTATUS:
1591 error = MX_ACCESS_CHK(VERIFY_WRITE, (void *)arg,sizeof(struct mxupcie_mstatus) * MXUPCIE_PORTS);
1593 if ( MX_ERR(error) )
1594 return error;
1596 for(i=0; i<MXUPCIE_PORTS; i++){
1597 GMStatus[i].ri = 0;
1599 if ( !mxvar_table[i].base ){
1600 GMStatus[i].dcd = 0;
1601 GMStatus[i].dsr = 0;
1602 GMStatus[i].cts = 0;
1604 continue;
1607 if ( !mxvar_table[i].tty || !mxvar_table[i].tty->termios )
1608 GMStatus[i].cflag=mxvar_table[i].normal_termios.c_cflag;
1609 else
1610 GMStatus[i].cflag = mxvar_table[i].tty->termios->c_cflag;
1612 status = MX_READ_REG(mxvar_table[i].base + UART_MSR);
1614 if(status & 0x80/*UART_MSR_DCD*/)
1615 GMStatus[i].dcd = 1;
1616 else
1617 GMStatus[i].dcd = 0;
1619 if(status & 0x20/*UART_MSR_DSR*/)
1620 GMStatus[i].dsr = 1;
1621 else
1622 GMStatus[i].dsr = 0;
1624 if(status & 0x10/*UART_MSR_CTS*/)
1625 GMStatus[i].cts = 1;
1626 else
1627 GMStatus[i].cts = 0;
1630 if(copy_to_user((struct mxupcie_mstatus *)arg, GMStatus,sizeof(struct mxupcie_mstatus) * MXUPCIE_PORTS))
1631 return -EFAULT;
1633 return 0;
1635 default:
1636 return -ENOIOCTLCMD;
1639 return 0;
1643 static void mx_stoprx(struct tty_struct * tty)
1645 struct mxupcie_struct *info = (struct mxupcie_struct *)tty->driver_data;
1646 MX_LOCK_INIT();
1648 MX_LOCK(&info->slock);
1649 info->IER &= ~UART_IER_RDI;
1650 MX_WRITE_REG(info->IER, info->base + UART_IER);
1651 MX_UNLOCK(&info->slock);
1655 static void mx_startrx(struct tty_struct * tty)
1657 struct mxupcie_struct *info = (struct mxupcie_struct *)tty->driver_data;
1658 MX_LOCK_INIT();
1660 MX_LOCK(&info->slock);
1661 info->IER |= UART_IER_RDI;
1662 MX_WRITE_REG(info->IER, info->base + UART_IER);
1663 MX_UNLOCK(&info->slock);
1668 * This routine is called by the upper-layer tty layer to signal that
1669 * incoming characters should be throttled.
1671 static void mxupcie_throttle(struct tty_struct * tty)
1673 mx_stoprx(tty);
1677 static void mxupcie_unthrottle(struct tty_struct * tty)
1679 mx_startrx(tty);
1683 #if (LINUX_VERSION_CODE > VERSION_CODE(2,6,19))
1684 static void mxupcie_set_termios(struct tty_struct * tty,
1685 struct ktermios * old_termios)
1686 #else
1687 static void mxupcie_set_termios(struct tty_struct * tty,
1688 struct termios * old_termios)
1689 #endif
1691 struct mxupcie_struct *info = (struct mxupcie_struct *)tty->driver_data;
1692 MX_LOCK_INIT();
1694 if ( (tty->termios->c_cflag != old_termios->c_cflag) ||
1695 (RELEVANT_IFLAG(tty->termios->c_iflag) !=
1696 RELEVANT_IFLAG(old_termios->c_iflag)) ) {
1697 MX_LOCK(&info->slock);
1698 mx_change_speed(info, old_termios);
1699 MX_UNLOCK(&info->slock);
1701 if ( (old_termios->c_cflag & CRTSCTS) &&
1702 !(tty->termios->c_cflag & CRTSCTS) ) {
1703 tty->hw_stopped = 0;
1704 mxupcie_start(tty);
1708 /* Handle sw stopped */
1709 if ( (old_termios->c_iflag & IXON) &&
1710 !(tty->termios->c_iflag & IXON) ) {
1711 tty->stopped = 0;
1712 mxupcie_start(tty);
1718 * mxupcie_stop() and mxupcie_start()
1720 * This routines are called before setting or resetting tty->stopped.
1721 * They enable or disable transmitter interrupts, as necessary.
1723 static void mxupcie_stop(struct tty_struct * tty)
1725 struct mxupcie_struct *info = (struct mxupcie_struct *)tty->driver_data;
1726 MX_LOCK_INIT();
1728 MX_LOCK(&info->slock);
1729 if ( info->IER & UART_IER_THRI ) {
1730 info->IER &= ~UART_IER_THRI;
1731 MX_WRITE_REG(info->IER, info->base + UART_IER);
1733 MX_UNLOCK(&info->slock);
1737 static void mxupcie_start(struct tty_struct * tty)
1739 struct mxupcie_struct *info = (struct mxupcie_struct *)tty->driver_data;
1740 MX_LOCK_INIT();
1742 MX_LOCK(&info->slock);
1743 if ( info->xmit_cnt && info->xmit_buf ) {
1744 info->IER &= ~UART_IER_THRI;
1745 MX_WRITE_REG(info->IER, info->base + UART_IER);
1746 info->IER |= UART_IER_THRI;
1747 MX_WRITE_REG(info->IER, info->base + UART_IER);
1749 MX_UNLOCK(&info->slock);
1753 * mxupcie_wait_until_sent() --- wait until the transmitter is empty
1755 static void mxupcie_wait_until_sent(struct tty_struct *tty, int timeout)
1757 struct mxupcie_struct * info = (struct mxupcie_struct *)tty->driver_data;
1758 unsigned long orig_jiffies, char_time;
1759 int lsr;
1761 if (info->type == PORT_UNKNOWN)
1762 return;
1764 if (info->xmit_fifo_size == 0)
1765 return; /* Just in case.... */
1767 orig_jiffies = jiffies;
1769 * Set the check interval to be 1/5 of the estimated time to
1770 * send a single character, and make it at least 1. The check
1771 * interval should also be less than the timeout.
1773 * Note: we have to use pretty tight timings here to satisfy
1774 * the NIST-PCTS.
1776 char_time = (info->timeout - HZ/50) / info->xmit_fifo_size;
1777 char_time = char_time / 5;
1779 if (char_time == 0)
1780 char_time = 1;
1782 if (timeout && timeout < char_time)
1783 char_time = timeout;
1785 * If the transmitter hasn't cleared in twice the approximate
1786 * amount of time to send the entire FIFO, it probably won't
1787 * ever clear. This assumes the UART isn't doing flow
1788 * control, which is currently the case. Hence, if it ever
1789 * takes longer than info->timeout, this is probably due to a
1790 * UART bug of some kind. So, we clamp the timeout parameter at
1791 * 2*info->timeout.
1794 if (!timeout || timeout > 2*info->timeout)
1795 timeout = 2*info->timeout;
1796 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1797 printk("In rs_wait_until_sent(%d) check=%lu...", timeout, char_time);
1798 printk("jiff=%lu...", jiffies);
1799 #endif
1800 while (!((lsr = MX_READ_REG(info->base+ UART_LSR)) & UART_LSR_TEMT)) {
1801 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1802 printk("lsr = %d (jiff=%lu)...", lsr, jiffies);
1803 #endif
1804 set_current_state(TASK_INTERRUPTIBLE);
1805 schedule_timeout(char_time);
1807 if (signal_pending(current))
1808 break;
1810 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1811 break;
1813 set_current_state(TASK_RUNNING);
1815 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1816 printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
1817 #endif
1822 * This routine is called by tty_hangup() when a hangup is signaled.
1824 void mxupcie_hangup(struct tty_struct * tty)
1826 struct mxupcie_struct * info = (struct mxupcie_struct *)tty->driver_data;
1828 mxupcie_flush_buffer(tty);
1829 mx_shutdown(info);
1830 info->event = 0;
1831 info->count = 0;
1832 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE);
1833 info->tty = 0;
1834 wake_up_interruptible(&info->open_wait);
1839 * mxupcie_rs_break() --- routine which turns the break handling on or off
1841 static void mxupcie_rs_break(struct tty_struct *tty, int break_state)
1843 struct mxupcie_struct * info = (struct mxupcie_struct *)tty->driver_data;
1844 MX_LOCK_INIT();
1846 MX_LOCK(&info->slock);
1847 if (break_state == -1){
1848 switch(info->UIR){
1849 case MOXA_UIR_RS485_4W:
1850 case MOXA_UIR_RS485_2W:
1851 mx_software_break_signal(info, MX_BREAK_ON);
1852 break;
1853 case MOXA_UIR_RS232:
1854 case MOXA_UIR_RS422:
1855 MX_WRITE_REG(MX_READ_REG(info->base + UART_LCR) | UART_LCR_SBC, info->base + UART_LCR);
1856 break;
1859 else{
1860 switch(info->UIR){
1861 case MOXA_UIR_RS485_4W:
1862 case MOXA_UIR_RS485_2W:
1863 mx_software_break_signal(info, MX_BREAK_OFF);
1864 break;
1865 case MOXA_UIR_RS232:
1866 case MOXA_UIR_RS422:
1867 MX_WRITE_REG(MX_READ_REG(info->base + UART_LCR) & ~UART_LCR_SBC, info->base + UART_LCR);
1868 break;
1872 MX_UNLOCK(&info->slock);
1877 * This is the serial driver's generic interrupt routine
1879 #if (LINUX_VERSION_CODE > VERSION_CODE(2,6,19))
1880 static irqreturn_t mx_interrupt(int irq, void *dev_id)
1881 #else
1882 static IRQ_RET mx_interrupt(int irq, void *dev_id, struct pt_regs * regs)
1883 #endif
1885 int status, iir, i;
1886 struct mxupcie_struct * info;
1887 struct mxupcie_struct * port;
1888 int max, msr;
1889 int pass_counter = 0;
1890 int int_cnt;
1891 int handled=0;
1892 int vect_flag;
1894 port = 0;
1896 for(i=0; i<MXUPCIE_BOARDS; i++){
1897 if(dev_id == &(mxvar_table[i*MXUPCIE_PORTS_PER_BOARD])){
1898 port = dev_id;
1899 break;
1903 if(i==MXUPCIE_BOARDS)
1904 goto irq_stop;
1906 if(port==0)
1907 goto irq_stop;
1909 max = mxupcie_numports[mxupciecfg[i].board_type-1];
1910 pass_counter = 0;
1913 vect_flag = 0;
1915 for(i=0;i<max;i++){
1916 info = port + i;
1917 int_cnt=0;
1920 iir = MX_READ_REG(info->base+UART_IIR);
1922 if ( iir == MOXA_IIR_NO_INT){
1923 vect_flag++;
1924 break;
1927 if ( !info->tty ) {
1928 status = MX_READ_REG(info->base+UART_LSR);
1929 MX_WRITE_REG(0x27, info->base+UART_FCR);
1930 MX_READ_REG(info->base+UART_MSR);
1931 break;
1934 handled = 1;
1936 spin_lock(&info->slock);
1937 status = MX_READ_REG(info->base+UART_LSR);
1939 if(iir == MOXA_IIR_RLSI){
1940 if(status & UART_LSR_PE)
1941 info->err_shadow |= NPPI_NOTIFY_PARITY;
1942 if(status & UART_LSR_FE)
1943 info->err_shadow |= NPPI_NOTIFY_FRAMING;
1944 if(status & UART_LSR_OE)
1945 info->err_shadow |= NPPI_NOTIFY_HW_OVERRUN;
1946 if(status & UART_LSR_BI)
1947 info->err_shadow |= NPPI_NOTIFY_BREAK;
1950 if(iir & MOXA_IIR_RDI){
1951 status &= info->read_status_mask;
1952 if ( status & UART_LSR_DR )
1953 mx_receive_chars(info, &status);
1956 msr = MX_READ_REG(info->base + UART_MSR);
1957 if ( msr & UART_MSR_ANY_DELTA ) {
1958 mx_check_modem_status(info, msr);
1961 if(iir & MOXA_IIR_THRI){
1962 if ( status & UART_LSR_THRE ) {
1963 mx_transmit_chars(info);
1967 spin_unlock(&info->slock);
1968 }while(int_cnt++ < MXUPCIE_ISR_PASS_LIMIT);
1971 if(vect_flag == max)
1972 break;
1974 }while(pass_counter++ < MXUPCIE_ISR_PASS_LIMIT);
1976 irq_stop:
1977 return IRQ_RETVAL(handled);
1980 #if defined(_SCREEN_INFO_H) || (LINUX_VERSION_CODE >= VERSION_CODE(2,6,16))
1981 static void mx_receive_chars(struct mxupcie_struct *info,
1982 int *status)
1984 struct tty_struct * tty = info->tty;
1985 unsigned char ch, gdl=0;
1986 int cnt = 0;
1987 int recv_room;
1988 int max = 256;
1989 unsigned long flags;
1991 if ( *status & UART_LSR_SPECIAL )
1992 goto intr_old;
1994 recv_room = tty_buffer_request_room(tty,MX_RX_FIFO_SIZE);
1996 if(recv_room){
1997 gdl = MX_READ_REG(info->base + MOXA_PUART_RCNT);
1999 if(gdl > recv_room)
2000 gdl = recv_room;
2002 if(gdl){
2003 tty_insert_flip_string(tty, info->base + MOXA_PUART_MEMRBR, gdl);
2004 cnt = gdl;
2007 else{
2008 set_bit(TTY_THROTTLED, &tty->flags);
2011 goto end_intr;
2013 intr_old:
2015 do {
2016 if(max-- <0)
2017 break;
2019 ch = MX_READ_REG(info->base + UART_RX);
2021 if ( *status & UART_LSR_SPECIAL ) {
2022 if ( *status & UART_LSR_BI ) {
2023 flags = TTY_BREAK;
2024 info->icount.brk++;
2026 if ( info->flags & ASYNC_SAK )
2027 do_SAK(tty);
2028 } else if ( *status & UART_LSR_PE ) {
2029 flags = TTY_PARITY;
2030 info->icount.parity++;
2031 } else if ( *status & UART_LSR_FE ) {
2032 flags = TTY_FRAME;
2033 info->icount.frame++;
2034 } else if ( *status & UART_LSR_OE ) {
2035 flags = TTY_OVERRUN;
2036 info->icount.overrun++;
2037 } else
2038 flags = TTY_BREAK;
2040 } else
2041 flags = 0;
2043 tty_insert_flip_char(tty, ch, flags);
2044 cnt++;
2046 *status = MX_READ_REG(info->base+UART_LSR);
2047 } while ( *status & UART_LSR_DR );
2049 end_intr: // add by Victor Yu. 09-02-2002
2051 tty_flip_buffer_push(tty);
2053 mxvar_log.rxcnt[info->port] += cnt;
2054 info->mon_data.rxcnt += cnt;
2055 info->mon_data.up_rxcnt += cnt;
2057 #else
2058 static void mx_receive_chars(struct mxupcie_struct *info,
2059 int *status)
2061 struct tty_struct * tty = info->tty;
2062 unsigned char ch, gdl;
2063 int cnt = 0;
2064 unsigned char *cp;
2065 char *fp;
2066 int recv_room;
2067 int max = 256;
2069 cp = tty->flip.char_buf;
2070 fp = tty->flip.flag_buf;
2072 if ( *status & UART_LSR_SPECIAL )
2073 goto intr_old;
2075 recv_room = tty->ldisc.receive_room(tty);
2077 if(recv_room){
2078 gdl = MX_READ_REG(info->base + MOXA_PUART_RCNT);
2080 if(gdl > recv_room)
2081 gdl = recv_room;
2083 if(gdl){
2084 cnt = gdl;
2085 memcpy(cp,info->base + MOXA_PUART_MEMRBR,cnt);
2086 memset(fp, TTY_NORMAL, cnt);
2087 cp+=cnt;
2088 fp+=cnt;
2091 else{
2092 set_bit(TTY_THROTTLED, &tty->flags);
2095 goto end_intr;
2097 intr_old:
2098 do {
2099 if(max-- <0)
2100 break;
2102 ch = MX_READ_REG(info->base + UART_RX);
2104 if ( *status & UART_LSR_SPECIAL ) {
2105 if ( *status & UART_LSR_BI ) {
2106 *fp++ = TTY_BREAK;
2107 info->icount.brk++;
2108 if ( info->flags & ASYNC_SAK )
2109 do_SAK(tty);
2110 } else if ( *status & UART_LSR_PE ) {
2111 *fp++ = TTY_PARITY;
2112 info->icount.parity++;
2113 } else if ( *status & UART_LSR_FE ) {
2114 *fp++ = TTY_FRAME;
2115 info->icount.frame++;
2116 } else if ( *status & UART_LSR_OE ) {
2117 *fp++ = TTY_OVERRUN;
2118 info->icount.overrun++;
2119 } else
2120 *fp++ = 0;
2121 } else
2122 *fp++ = 0;
2124 *cp++ = ch;
2125 cnt++;
2127 *status = MX_READ_REG(info->base+UART_LSR);
2128 } while ( *status & UART_LSR_DR );
2130 end_intr: // add by Victor Yu. 09-02-2002
2132 mxvar_log.rxcnt[info->port] += cnt;
2133 info->mon_data.rxcnt += cnt;
2134 info->mon_data.up_rxcnt += cnt;
2136 tty->ldisc.receive_buf(tty, tty->flip.char_buf, tty->flip.flag_buf, cnt);
2137 //printk("%lu, mxsesr_receive_chars=%d, [%x]\n",
2138 // jiffies, count, tty->flip.char_buf[0]);
2140 #endif
2143 static void mx_transmit_chars(struct mxupcie_struct *info)
2145 int count, cnt;
2146 int tx_cnt;
2148 if ( info->xmit_buf == 0 ){
2149 return;
2152 if(info->xmit_cnt==0){
2153 if ( info->xmit_cnt < WAKEUP_CHARS ) {
2154 set_bit(MXUPCIE_EVENT_TXLOW,&info->event);
2155 MXQ_TASK();
2157 return;
2160 if (info->tty->stopped){
2161 info->IER &= ~UART_IER_THRI;
2162 MX_WRITE_REG(info->IER, info->base + UART_IER);
2163 return;
2166 cnt = info->xmit_cnt;
2167 count = info->xmit_fifo_size;
2169 tx_cnt = MX_TX_FIFO_SIZE - MX_READ_REG(info->base + MOXA_PUART_TCNT);
2171 cnt = MIN(info->xmit_cnt, MIN(tx_cnt,
2172 SERIAL_XMIT_SIZE - info->xmit_tail));
2174 if(cnt){
2175 memcpy(info->base + MOXA_PUART_MEMTHR,info->xmit_buf+info->xmit_tail,cnt);
2176 info->xmit_tail += cnt;
2177 info->xmit_tail &= (SERIAL_XMIT_SIZE - 1);
2178 info->xmit_cnt -= cnt;
2181 mxvar_log.txcnt[info->port] += cnt;
2182 info->mon_data.txcnt += cnt;
2183 info->mon_data.up_txcnt += cnt;
2184 info->icount.tx += cnt;
2186 if ( info->xmit_cnt < WAKEUP_CHARS ) {
2187 set_bit(MXUPCIE_EVENT_TXLOW,&info->event);
2188 MXQ_TASK();
2192 static void mx_check_modem_status(struct mxupcie_struct *info,
2193 int status)
2195 /* update input line counters */
2196 if ( status & UART_MSR_TERI )
2197 info->icount.rng++;
2198 if ( status & UART_MSR_DDSR )
2199 info->icount.dsr++;
2200 if ( status & UART_MSR_DDCD )
2201 info->icount.dcd++;
2202 if ( status & UART_MSR_DCTS )
2203 info->icount.cts++;
2205 info->mon_data.modem_status = status;
2206 wake_up_interruptible(&info->delta_msr_wait);
2208 if ( (info->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD) ) {
2209 if ( status & UART_MSR_DCD )
2210 wake_up_interruptible(&info->open_wait);
2211 else if ( !((info->flags & ASYNC_CALLOUT_ACTIVE) &&
2212 (info->flags & ASYNC_CALLOUT_NOHUP)) )
2214 set_bit(MXUPCIE_EVENT_HANGUP,&info->event);
2215 MXQ_TASK();
2218 if ( info->flags & ASYNC_CTS_FLOW ) {
2219 if ( info->tty->hw_stopped ) {
2220 if (status & UART_MSR_CTS ){
2221 info->tty->hw_stopped = 0;
2222 set_bit(MXUPCIE_EVENT_TXLOW,&info->event);
2223 MXQ_TASK();
2225 } else {
2226 if ( !(status & UART_MSR_CTS) ){
2227 info->tty->hw_stopped = 1;
2233 static int mx_block_til_ready(struct tty_struct *tty, struct file * filp,
2234 struct mxupcie_struct *info)
2236 DECLARE_WAITQUEUE(wait, current);
2237 int retval;
2238 int do_clocal = 0;
2239 MX_LOCK_INIT();
2242 * If non-blocking mode is set, or the port is not enabled,
2243 * then make the check up front and then exit.
2245 if ( (filp->f_flags & O_NONBLOCK) || (tty->flags & (1 << TTY_IO_ERROR)) ) {
2246 if ( info->flags & ASYNC_CALLOUT_ACTIVE )
2247 return -EBUSY;
2249 info->flags |= ASYNC_NORMAL_ACTIVE;
2251 return 0;
2254 if ( info->flags & ASYNC_CALLOUT_ACTIVE ) {
2255 if ( info->normal_termios.c_cflag & CLOCAL )
2256 do_clocal = 1;
2257 } else {
2258 if ( tty->termios->c_cflag & CLOCAL )
2259 do_clocal = 1;
2263 * Block waiting for the carrier detect and the line to become
2264 * free (i.e., not in use by the callout). While we are in
2265 * this loop, info->count is dropped by one, so that
2266 * mxupcie_close() knows when to free things. We restore it upon
2267 * exit, either normal or abnormal.
2269 retval = 0;
2270 add_wait_queue(&info->open_wait, &wait);
2272 MX_LOCK(&info->slock);
2273 if ( !tty_hung_up_p(filp) )
2274 info->count--;
2275 MX_UNLOCK(&info->slock);
2277 info->blocked_open++;
2279 while ( 1 ) {
2280 MX_LOCK(&info->slock);
2281 if ( !(info->flags & ASYNC_CALLOUT_ACTIVE) )
2282 MX_WRITE_REG(MX_READ_REG(info->base + UART_MCR) | UART_MCR_DTR | UART_MCR_RTS,
2283 info->base + UART_MCR);
2284 MX_UNLOCK(&info->slock);
2285 set_current_state(TASK_INTERRUPTIBLE);
2287 if ( tty_hung_up_p(filp) || !(info->flags & ASYNC_INITIALIZED) ) {
2288 #ifdef SERIAL_DO_RESTART
2289 if ( info->flags & ASYNC_HUP_NOTIFY )
2290 retval = -EAGAIN;
2291 else
2292 retval = -ERESTARTSYS;
2293 #else
2294 retval = -EAGAIN;
2295 #endif
2296 break;
2299 if ( !(info->flags & ASYNC_CALLOUT_ACTIVE) && !(info->flags & ASYNC_CLOSING) &&
2300 (do_clocal || (MX_READ_REG(info->base + UART_MSR) & UART_MSR_DCD)) )
2302 break;
2303 if ( signal_pending(current) ) {
2304 retval = -ERESTARTSYS;
2305 break;
2308 schedule();
2311 set_current_state(TASK_RUNNING);
2312 remove_wait_queue(&info->open_wait, &wait);
2314 if ( !tty_hung_up_p(filp) )
2315 info->count++;
2317 info->blocked_open--;
2319 if ( retval )
2320 return retval;
2322 info->flags |= ASYNC_NORMAL_ACTIVE;
2324 return 0;
2327 static int mx_startup(struct mxupcie_struct * info)
2330 unsigned long page;
2331 unsigned char reg_flag;
2332 MX_LOCK_INIT();
2334 page = GET_FPAGE(GFP_KERNEL);
2335 if ( !page )
2336 return -ENOMEM;
2338 MX_LOCK(&info->slock);
2340 if ( info->flags & ASYNC_INITIALIZED ) {
2341 free_page(page);
2342 MX_UNLOCK(&info->slock);
2344 return 0;
2347 if ( !info->base || !info->type ) {
2348 if ( info->tty )
2349 set_bit(TTY_IO_ERROR, &info->tty->flags);
2350 free_page(page);
2351 MX_UNLOCK(&info->slock);
2353 return 0;
2356 if ( info->xmit_buf )
2357 free_page(page);
2358 else
2359 info->xmit_buf = (unsigned char *)page;
2361 mx_change_speed(info, 0);
2364 * Clear the FIFO buffers and disable them
2365 * (they will be reenabled in mx_change_speed())
2367 MX_WRITE_REG((UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT),info->base + UART_FCR);
2370 * At this point there's no way the LSR could still be 0xFF;
2371 * if it is, then bail out, because there's likely no UART
2372 * here.
2375 if ( MX_READ_REG(info->base + UART_LSR) == 0xff ) {
2376 MX_UNLOCK(&info->slock);
2378 if (capable(CAP_SYS_ADMIN)) {
2379 if ( info->tty )
2380 set_bit(TTY_IO_ERROR, &info->tty->flags);
2382 return(0);
2383 } else
2384 return -ENODEV;
2388 * Clear the interrupt registers.
2390 (void)MX_READ_REG(info->base + UART_LSR);
2391 (void)MX_READ_REG(info->base + UART_RX);
2392 (void)MX_READ_REG(info->base + UART_IIR);
2393 (void)MX_READ_REG(info->base + UART_MSR);
2396 * Now, initialize the UART
2398 MX_WRITE_REG(UART_LCR_WLEN8, info->base + UART_LCR); /* reset DLAB */
2399 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
2400 MX_WRITE_REG(info->MCR, info->base + UART_MCR);
2403 * Initialize enhance mode register
2405 reg_flag = MOXA_EFR_ENHANCE;
2406 MX_WRITE_REG(reg_flag, info->base + MOXA_PUART_EFR);
2408 reg_flag = MOXA_SFR_950 | MOXA_SFR_ENABLE_TCNT;
2409 MX_WRITE_REG(reg_flag, info->base + MOXA_PUART_SFR);
2411 MX_WRITE_REG(MOXA_TTL_1, info->base + MOXA_PUART_TTL);
2412 MX_WRITE_REG(MOXA_RTL_96, info->base + MOXA_PUART_RTL);
2413 MX_WRITE_REG(MOXA_FCL_16, info->base + MOXA_PUART_FCL);
2414 MX_WRITE_REG(MOXA_FCH_110, info->base + MOXA_PUART_FCH);
2417 * Finally, enable interrupts
2419 info->IER = UART_IER_MSI | UART_IER_RLSI | UART_IER_RDI;
2420 MX_WRITE_REG(info->IER, info->base + UART_IER); /* enable interrupts */
2423 * And clear the interrupt registers again for luck.
2425 (void)MX_READ_REG(info->base + UART_LSR);
2426 (void)MX_READ_REG(info->base + UART_RX);
2427 (void)MX_READ_REG(info->base + UART_IIR);
2428 (void)MX_READ_REG(info->base + UART_MSR);
2430 if ( info->tty )
2431 test_and_clear_bit(TTY_IO_ERROR, &info->tty->flags);
2433 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
2436 * and set the speed of the serial port
2438 mx_change_speed(info, 0);
2439 MX_UNLOCK(&info->slock);
2441 info->flags |= ASYNC_INITIALIZED;
2443 return 0;
2447 * This routine will shutdown a serial port; interrupts maybe disabled, and
2448 * DTR is dropped if the hangup on close termio flag is on.
2450 static void mx_shutdown(struct mxupcie_struct * info)
2452 unsigned char reg_flag;
2453 MX_LOCK_INIT();
2455 if ( !(info->flags & ASYNC_INITIALIZED) )
2456 return;
2458 MX_LOCK(&info->slock);
2461 * clear delta_msr_wait queue to avoid mem leaks: we may free the irq
2462 * here so the queue might never be waken up
2464 wake_up_interruptible(&info->delta_msr_wait);
2467 * Free the IRQ, if necessary
2469 if ( info->xmit_buf ) {
2470 free_page((unsigned long)info->xmit_buf);
2471 info->xmit_buf = 0;
2474 reg_flag = 0;
2475 MX_WRITE_REG(reg_flag, info->base + MOXA_PUART_EFR);
2476 MX_WRITE_REG(reg_flag, info->base + MOXA_PUART_SFR);
2478 info->IER = 0;
2479 MX_WRITE_REG(0x00, info->base + UART_IER);
2481 if ( !info->tty || (info->tty->termios->c_cflag & HUPCL) )
2482 info->MCR &= ~(UART_MCR_DTR | UART_MCR_RTS);
2483 MX_WRITE_REG(info->MCR, info->base + UART_MCR);
2485 /* clear Rx/Tx FIFO's */
2486 MX_WRITE_REG((UART_FCR_CLEAR_RCVR|UART_FCR_CLEAR_XMIT), info->base + UART_FCR);
2488 /* read data port to reset things */
2489 (void)MX_READ_REG(info->base + UART_RX);
2491 if ( info->tty )
2492 set_bit(TTY_IO_ERROR, &info->tty->flags);
2494 info->flags &= ~ASYNC_INITIALIZED;
2496 MX_UNLOCK(&info->slock);
2500 * This routine is called to set the UART divisor registers to match
2501 * the specified baud rate for a serial port.
2503 #if (LINUX_VERSION_CODE > VERSION_CODE(2,6,19))
2504 static int mx_change_speed(struct mxupcie_struct *info,
2505 struct ktermios *old_termios)
2506 #else
2507 static int mx_change_speed(struct mxupcie_struct *info,
2508 struct termios *old_termios)
2509 #endif
2511 unsigned cflag, cval, fcr;
2512 int ret = 0;
2513 long baud;
2514 int reg_flag;
2517 if ( !info->tty || !info->tty->termios )
2518 return ret;
2520 cflag = info->tty->termios->c_cflag;
2522 if ( !(info->base) )
2523 return ret;
2525 #ifndef B921600
2526 #define B921600 (B460800 +1)
2527 #endif
2528 switch( cflag & (CBAUD | CBAUDEX) ){
2529 case B4000000 : baud = info->speed; break;
2530 case B921600 : info->speed = baud = 921600; break;
2531 case B460800 : info->speed = baud = 460800; break;
2532 case B230400 : info->speed = baud = 230400; break;
2533 case B115200 : info->speed = baud = 115200; break;
2534 case B57600 : info->speed = baud = 57600; break;
2535 case B38400 : info->speed = baud = 38400;
2536 if((info->flags & ASYNC_SPD_MASK)==ASYNC_SPD_HI)
2537 info->speed = baud = 57600;
2538 else if((info->flags & ASYNC_SPD_MASK)==ASYNC_SPD_VHI)
2539 info->speed = baud = 115200;
2540 #ifdef ASYNC_SPD_SHI
2541 else if((info->flags & ASYNC_SPD_MASK)==ASYNC_SPD_SHI)
2542 info->speed = baud = 230400;
2543 #endif
2544 #ifdef ASYNC_SPD_WARP
2545 else if((info->flags & ASYNC_SPD_MASK)==ASYNC_SPD_WARP)
2546 info->speed = baud = 460800;
2547 #endif
2548 #ifdef ASYNC_SPD_CUST
2549 else if((info->flags & ASYNC_SPD_MASK)==ASYNC_SPD_CUST){
2550 info->speed = baud = info->custom_baud_rate;
2552 #endif
2553 break;
2554 case B19200 : info->speed = baud = 19200; break;
2555 case B9600 : info->speed = baud = 9600; break;
2556 case B4800 : info->speed = baud = 4800; break;
2557 case B2400 : info->speed = baud = 2400; break;
2558 case B1800 : info->speed = baud = 1800; break;
2559 case B1200 : info->speed = baud = 1200; break;
2560 case B600 : info->speed = baud = 600; break;
2561 case B300 : info->speed = baud = 300; break;
2562 case B200 : info->speed = baud = 200; break;
2563 case B150 : info->speed = baud = 150; break;
2564 case B134 : info->speed = baud = 134; break;
2565 case B110 : info->speed = baud = 110; break;
2566 case B75 : info->speed = baud = 75; break;
2567 case B50 : info->speed = baud = 50; break;
2568 default: info->speed = baud = 0; break;
2571 mx_set_baud(info, baud);
2573 /* byte size and parity */
2574 switch ( cflag & CSIZE ) {
2575 case CS5: cval = 0x00; break;
2576 case CS6: cval = 0x01; break;
2577 case CS7: cval = 0x02; break;
2578 case CS8: cval = 0x03; break;
2579 default: cval = 0x00; break; /* too keep GCC shut... */
2582 if ( cflag & CSTOPB )
2583 cval |= 0x04;
2585 if ( cflag & PARENB )
2586 cval |= UART_LCR_PARITY;
2588 #ifndef CMSPAR
2589 #define CMSPAR 010000000000
2590 #endif
2591 if ( !(cflag & PARODD) )
2592 cval |= UART_LCR_EPAR;
2594 if ( cflag & CMSPAR )
2595 cval |= UART_LCR_SPAR;
2597 fcr = UART_FCR_ENABLE_FIFO;
2599 /* CTS flow control flag and modem status interrupts */
2600 info->IER &= ~UART_IER_MSI;
2601 // info->MCR &= ~UART_MCR_AFE;
2603 reg_flag = MX_READ_REG(info->base + MOXA_PUART_EFR);
2605 if ( cflag & CRTSCTS ) {
2606 info->flags |= ASYNC_CTS_FLOW;
2607 info->IER |= UART_IER_MSI;
2608 reg_flag |= (MOXA_EFR_AUTO_RTS | MOXA_EFR_AUTO_CTS);
2609 } else {
2610 info->flags &= ~ASYNC_CTS_FLOW;
2611 reg_flag &= ~(MOXA_EFR_AUTO_RTS | MOXA_EFR_AUTO_CTS);
2614 MX_WRITE_REG(info->MCR, info->base + UART_MCR);
2616 if ( cflag & CLOCAL ){
2617 info->flags &= ~ASYNC_CHECK_CD;
2618 }else {
2619 info->flags |= ASYNC_CHECK_CD;
2620 info->IER |= UART_IER_MSI;
2623 MX_WRITE_REG(info->IER, info->base + UART_IER);
2626 * Set up parity check flag
2628 info->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
2630 if ( I_INPCK(info->tty) )
2631 info->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
2633 if ( I_BRKINT(info->tty) || I_PARMRK(info->tty) )
2634 info->read_status_mask |= UART_LSR_BI;
2636 info->ignore_status_mask = 0;
2638 if ( I_IGNBRK(info->tty) ) {
2639 info->ignore_status_mask |= UART_LSR_BI;
2640 info->read_status_mask |= UART_LSR_BI;
2642 * If we're ignore parity and break indicators, ignore
2643 * overruns too. (For real raw support).
2645 if ( I_IGNPAR(info->tty) ) {
2646 info->ignore_status_mask |= UART_LSR_OE|UART_LSR_PE|UART_LSR_FE;
2647 info->read_status_mask |= UART_LSR_OE|UART_LSR_PE|UART_LSR_FE;
2651 MX_WRITE_REG(START_CHAR(info->tty), info->base + MOXA_PUART_XON1);
2652 MX_WRITE_REG(START_CHAR(info->tty), info->base + MOXA_PUART_XON2);
2653 MX_WRITE_REG(STOP_CHAR(info->tty), info->base + MOXA_PUART_XOFF1);
2654 MX_WRITE_REG(STOP_CHAR(info->tty), info->base + MOXA_PUART_XOFF2);
2656 if ( I_IXON(info->tty) )
2657 reg_flag |= MOXA_EFR_TX_SW;
2658 else
2659 reg_flag &= ~MOXA_EFR_TX_SW;
2661 if ( I_IXOFF(info->tty) )
2662 reg_flag |= MOXA_EFR_RX_SW;
2663 else
2664 reg_flag &= ~MOXA_EFR_RX_SW;
2666 MX_WRITE_REG(reg_flag, info->base + MOXA_PUART_EFR);
2667 MX_WRITE_REG(fcr, info->base + UART_FCR); /* set fcr */
2668 MX_WRITE_REG(cval, info->base + UART_LCR);
2670 return ret;
2674 static int mx_set_baud(struct mxupcie_struct *info, long newspd)
2676 int i;
2677 int quot = 0;
2678 unsigned char cval;
2679 int ret = 0;
2681 if ( !info->tty || !info->tty->termios )
2682 return ret;
2684 if ( !(info->base) )
2685 return ret;
2687 if ( newspd > info->MaxCanSetBaudRate )
2688 return 0;
2690 info->realbaud = newspd;
2692 for ( i=0; i<BAUD_TABLE_NO && newspd != mxvar_baud_table[i]; i++ );
2694 if ( i == BAUD_TABLE_NO ){
2695 quot = info->baud_base / info->speed;
2696 if ( info->speed <= 0 || info->speed > info->MaxCanSetBaudRate )
2697 quot = 0;
2698 }else{
2699 if ( newspd == 134 ) {
2700 quot = (2 * info->baud_base / 269);
2701 info->speed = 134;
2702 } else if ( newspd ) {
2703 quot = info->baud_base / newspd;
2705 if(quot==0)
2706 quot = 1;
2707 } else {
2708 quot = 0;
2712 info->timeout = (int)((unsigned int)(info->xmit_fifo_size*HZ*10*quot) / (unsigned int)info->baud_base);
2713 info->timeout += HZ/50; /* Add .02 seconds of slop */
2715 if ( quot ) {
2716 info->MCR |= UART_MCR_DTR;
2717 MX_WRITE_REG(info->MCR, info->base + UART_MCR);
2718 } else {
2719 info->MCR &= ~UART_MCR_DTR;
2720 MX_WRITE_REG(info->MCR, info->base + UART_MCR);
2722 return ret;
2725 cval = MX_READ_REG(info->base + UART_LCR);
2726 MX_WRITE_REG(cval | UART_LCR_DLAB, info->base + UART_LCR); /* set DLAB */
2727 MX_WRITE_REG(quot & 0xff, info->base + UART_DLL); /* LS of divisor */
2728 MX_WRITE_REG(quot >> 8, info->base + UART_DLM); /* MS of divisor */
2729 MX_WRITE_REG(cval, info->base + UART_LCR); /* reset DLAB */
2731 if ( i == BAUD_TABLE_NO ){
2732 quot = info->baud_base % info->speed;
2733 quot *= 8;
2734 if ( (quot % info->speed) > (info->speed / 2) ) {
2735 quot /= info->speed;
2736 quot++;
2737 } else {
2738 quot /= info->speed;
2742 return ret;
2748 * ------------------------------------------------------------
2749 * friends of mxupcie_ioctl()
2750 * ------------------------------------------------------------
2752 static int mx_get_serial_info(struct mxupcie_struct * info,
2753 struct serial_struct * retinfo)
2755 struct serial_struct tmp;
2757 if ( !retinfo )
2758 return(-EFAULT);
2760 memset(&tmp, 0, sizeof(tmp));
2761 tmp.type = info->type;
2762 tmp.line = info->port;
2763 tmp.port = *info->base;
2764 tmp.irq = info->irq;
2765 tmp.flags = info->flags;
2766 tmp.baud_base = info->baud_base;
2767 tmp.close_delay = info->close_delay;
2768 tmp.closing_wait = info->closing_wait;
2769 tmp.custom_divisor = info->custom_divisor;
2770 tmp.hub6 = 0;
2772 if(copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
2773 return -EFAULT;
2775 return 0;
2778 static int mx_set_serial_info(struct mxupcie_struct * info,
2779 struct serial_struct * new_info)
2781 struct serial_struct new_serial;
2782 unsigned int flags;
2783 int retval = 0;
2785 MX_LOCK_INIT();
2787 if ( !new_info || !info->base )
2788 return -EFAULT;
2790 if(copy_from_user(&new_serial, new_info, sizeof(new_serial)))
2791 return -EFAULT;
2793 if ( (new_serial.irq != info->irq) ||
2794 (new_serial.port != *info->base) )
2795 return -EPERM;
2797 flags = info->flags & ASYNC_SPD_MASK;
2799 if ( !capable(CAP_SYS_ADMIN)) {
2800 if ( (new_serial.baud_base != info->baud_base) ||
2801 (new_serial.close_delay != info->close_delay) ||
2802 ((new_serial.flags & ~ASYNC_USR_MASK) !=
2803 (info->flags & ~ASYNC_USR_MASK)) )
2804 return(-EPERM);
2806 info->flags = ((info->flags & ~ASYNC_USR_MASK) | (new_serial.flags & ASYNC_USR_MASK));
2807 } else {
2809 * OK, past this point, all the error checking has been done.
2810 * At this point, we start making changes.....
2812 info->flags = ((info->flags & ~ASYNC_FLAGS) | (new_serial.flags & ASYNC_FLAGS));
2813 info->close_delay = new_serial.close_delay * HZ/100;
2814 info->closing_wait = new_serial.closing_wait * HZ/100;
2815 info->tty->low_latency = (info->flags & ASYNC_LOW_LATENCY) ? 1 : 0;
2817 if( (new_serial.baud_base != info->baud_base) ||
2818 (new_serial.custom_divisor != info->custom_divisor) )
2819 info->custom_baud_rate = new_serial.baud_base/new_serial.custom_divisor;
2822 info->type = new_serial.type;
2823 info->xmit_fifo_size = MX_TX_FIFO_SIZE;
2825 if ( info->flags & ASYNC_INITIALIZED ) {
2826 if ( flags != (info->flags & ASYNC_SPD_MASK) ){
2827 MX_LOCK(&info->slock);
2828 mx_change_speed(info,0);
2829 MX_UNLOCK(&info->slock);
2831 } else{
2832 retval = mx_startup(info);
2835 return retval;
2839 * mx_get_lsr_info - get line status register info
2841 * Purpose: Let user call ioctl() to get info when the UART physically
2842 * is emptied. On bus types like RS485, the transmitter must
2843 * release the bus after transmitting. This must be done when
2844 * the transmit shift register is empty, not be done when the
2845 * transmit holding register is empty. This functionality
2846 * allows an RS485 driver to be written in user space.
2848 static int mx_get_lsr_info(struct mxupcie_struct * info, unsigned int *value)
2850 unsigned char status;
2851 unsigned int result;
2852 MX_LOCK_INIT();
2854 MX_LOCK(&info->slock);
2855 status = MX_READ_REG(info->base + UART_LSR);
2856 MX_UNLOCK(&info->slock);
2857 result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
2858 put_to_user(result, value);
2860 return 0;
2864 * This routine sends a break character out the serial port.
2866 static void mx_send_break(struct mxupcie_struct * info, int duration)
2868 MX_LOCK_INIT();
2870 if ( !info->base )
2871 return;
2873 set_current_state(TASK_INTERRUPTIBLE);
2875 MX_LOCK(&info->slock);
2876 switch(info->UIR){
2877 case MOXA_UIR_RS485_4W:
2878 case MOXA_UIR_RS485_2W:
2879 mx_software_break_signal(info, MX_BREAK_ON);
2880 break;
2881 case MOXA_UIR_RS232:
2882 case MOXA_UIR_RS422:
2883 MX_WRITE_REG(MX_READ_REG(info->base + UART_LCR) | UART_LCR_SBC, info->base + UART_LCR);
2884 break;
2886 MX_UNLOCK(&info->slock);
2888 schedule_timeout(duration);
2890 MX_LOCK(&info->slock);
2891 switch(info->UIR){
2892 case MOXA_UIR_RS485_4W:
2893 case MOXA_UIR_RS485_2W:
2894 mx_software_break_signal(info, MX_BREAK_OFF);
2895 break;
2896 case MOXA_UIR_RS232:
2897 case MOXA_UIR_RS422:
2898 MX_WRITE_REG(MX_READ_REG(info->base + UART_LCR) & ~UART_LCR_SBC, info->base + UART_LCR);
2899 break;
2901 MX_UNLOCK(&info->slock);
2903 set_current_state(TASK_RUNNING);
2906 #if (LINUX_VERSION_CODE >= VERSION_CODE(2,6,0))
2907 static int mxupcie_tiocmget(struct tty_struct *tty, struct file *file)
2909 struct mxupcie_struct *info = (struct mxupcie_struct *) tty->driver_data;
2910 unsigned char control, status;
2911 MX_LOCK_INIT();
2912 //printk("%lu,tiocmget\n", jiffies);
2914 if (PORTNO(tty) == MXUPCIE_PORTS)
2915 return -ENOIOCTLCMD;
2917 if (tty->flags & (1 << TTY_IO_ERROR))
2918 return -EIO;
2920 control = info->MCR;
2922 MX_LOCK(&info->slock);
2923 status = MX_READ_REG(info->base + UART_MSR);
2925 if (status & UART_MSR_ANY_DELTA)
2926 mx_check_modem_status(info, status);
2928 MX_UNLOCK(&info->slock);
2930 return ((control & UART_MCR_RTS) ? TIOCM_RTS : 0) |
2931 ((control & UART_MCR_DTR) ? TIOCM_DTR : 0) |
2932 ((status & UART_MSR_DCD) ? TIOCM_CAR : 0) |
2933 ((status & UART_MSR_RI) ? TIOCM_RNG : 0) |
2934 ((status & UART_MSR_DSR) ? TIOCM_DSR : 0) |
2935 ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
2939 static int mxupcie_tiocmset(struct tty_struct *tty, struct file *file,
2940 unsigned int set, unsigned int clear)
2942 struct mxupcie_struct *info = (struct mxupcie_struct *) tty->driver_data;
2943 MX_LOCK_INIT();
2946 //printk("%lu,tiocmset\n", jiffies);
2948 if (PORTNO(tty) == MXUPCIE_PORTS)
2949 return (-ENOIOCTLCMD);
2951 if (tty->flags & (1 << TTY_IO_ERROR))
2952 return (-EIO);
2954 MX_LOCK(&info->slock);
2956 if (set & TIOCM_RTS)
2957 info->MCR |= UART_MCR_RTS;
2958 if (set & TIOCM_DTR)
2959 info->MCR |= UART_MCR_DTR;
2960 if (clear & TIOCM_RTS)
2961 info->MCR &= ~UART_MCR_RTS;
2962 if (clear & TIOCM_DTR)
2963 info->MCR &= ~UART_MCR_DTR;
2965 MX_WRITE_REG(info->MCR, info->base + UART_MCR);
2966 MX_UNLOCK(&info->slock);
2968 return 0;
2971 #else
2972 static int mx_get_modem_info(struct mxupcie_struct * info,
2973 unsigned int *value)
2975 unsigned char control, status;
2976 unsigned int result;
2977 MX_LOCK_INIT();
2979 MX_LOCK(&info->slock);
2980 control = info->MCR;
2981 status = mxupcie_get_msr(info->base, 0, info->port, info);
2982 MX_UNLOCK(&info->slock);
2984 if ( status & UART_MSR_ANY_DELTA )
2985 mx_check_modem_status(info, status);
2987 result = ((control & UART_MCR_RTS) ? TIOCM_RTS : 0) |
2988 ((control & UART_MCR_DTR) ? TIOCM_DTR : 0) |
2989 ((status & UART_MSR_DCD) ? TIOCM_CAR : 0) |
2990 ((status & UART_MSR_RI) ? TIOCM_RNG : 0) |
2991 ((status & UART_MSR_DSR) ? TIOCM_DSR : 0) |
2992 ((status & UART_MSR_CTS) ? TIOCM_CTS : 0);
2993 put_to_user(result, value);
2995 return 0;
2999 static int mx_set_modem_info(struct mxupcie_struct * info, unsigned int cmd,
3000 unsigned int *value)
3002 int error;
3003 unsigned int arg;
3005 error = MX_ACCESS_CHK(VERIFY_READ, value, sizeof(int));
3007 if ( MX_ERR(error) )
3008 return(error);
3010 get_from_user(arg,value);
3012 switch ( cmd ) {
3013 case TIOCMBIS:
3014 if ( arg & TIOCM_RTS )
3015 info->MCR |= UART_MCR_RTS;
3016 if ( arg & TIOCM_DTR )
3017 info->MCR |= UART_MCR_DTR;
3018 break;
3019 case TIOCMBIC:
3020 if ( arg & TIOCM_RTS )
3021 info->MCR &= ~UART_MCR_RTS;
3022 if ( arg & TIOCM_DTR )
3023 info->MCR &= ~UART_MCR_DTR;
3024 break;
3025 case TIOCMSET:
3026 info->MCR = ((info->MCR & ~(UART_MCR_RTS | UART_MCR_DTR)) |
3027 ((arg & TIOCM_RTS) ? UART_MCR_RTS : 0) |
3028 ((arg & TIOCM_DTR) ? UART_MCR_DTR : 0));
3029 break;
3030 default:
3031 return -EINVAL;
3034 MX_WRITE_REG(info->MCR, info->base + UART_MCR);
3036 return 0;
3038 #endif
3040 static int mx_set_interface(struct mxupcie_struct *info, unsigned char val)
3042 unsigned char intf = 0, chip_val = 0;
3044 switch(val){
3045 case MOXA_UIR_RS232:
3046 switch(info->board_type){
3047 case MXUPCIE_BOARD_CP132EL:
3048 return -EINVAL;
3049 default:
3050 break;
3052 case MOXA_UIR_RS422:
3053 case MOXA_UIR_RS485_4W:
3054 case MOXA_UIR_RS485_2W:
3055 switch(info->board_type){
3056 case MXUPCIE_BOARD_CP102E:
3057 case MXUPCIE_BOARD_CP102EL:
3058 return -EINVAL;
3059 default:
3060 break;
3063 info->UIR = val;
3065 chip_val = MX_READ_IOBAR3_REG(info->UIR_addr);
3067 if(info->port % 2){
3068 intf = val << MOXA_UIR_EVEN_PORT_VALUE_OFFSET;
3069 chip_val &= 0x0F;
3070 chip_val |= intf;
3073 else{
3074 intf = val;
3075 chip_val &= 0xF0;
3076 chip_val |= intf;
3080 MX_WRITE_IOBAR3_REG(chip_val, info->UIR_addr);
3082 break;
3083 default:
3084 return -EINVAL;
3087 return 0;
3091 static int mx_set_terminator(struct mxupcie_struct *info, unsigned char val)
3093 unsigned char chip_val = 0;
3095 if(info->UIR == MOXA_UIR_RS232)
3096 return -EINVAL;
3098 switch(val){
3099 case MX_TERM_NONE:
3100 case MX_TERM_120:
3102 info->terminator_flag = val;
3103 chip_val = MX_READ_IOBAR3_REG(info->iobar3_addr + MOXA_PUART_GPIO_IN);
3105 switch(info->board_type){
3106 case MXUPCIE_BOARD_CP132EL:
3107 chip_val &= ~(1<<(info->port+2));
3108 chip_val |= (val << (info->port+2));
3109 break;
3110 case MXUPCIE_BOARD_CP114EL:
3111 chip_val &= ~(1<<info->port);
3112 chip_val |= (val << info->port);
3113 break;
3114 case MXUPCIE_BOARD_CP102E:
3115 case MXUPCIE_BOARD_CP102EL:
3116 default:
3117 return -EINVAL;
3120 MX_WRITE_IOBAR3_REG(chip_val, info->iobar3_addr + MOXA_PUART_GPIO_OUT);
3122 break;
3123 default:
3124 return -EINVAL;
3127 return 0;
3131 static void mx_software_break_signal(struct mxupcie_struct *info, unsigned char state)
3133 unsigned char cval,reg_flag;
3134 unsigned char tx_byte=0x01;
3135 int origin_speed;
3137 origin_speed = info->speed;
3139 if(state == MX_BREAK_ON){
3140 cval = MX_READ_REG(info->base + UART_LCR);
3141 MX_WRITE_REG(cval | UART_LCR_DLAB, info->base + UART_LCR);
3142 MX_WRITE_REG(0, info->base + UART_DLL);
3143 MX_WRITE_REG(0, info->base + UART_DLM);
3144 MX_WRITE_REG(cval, info->base + UART_LCR);
3146 memcpy(info->base + MOXA_PUART_MEMTHR,&tx_byte,1);
3148 reg_flag = MX_READ_REG(info->base + MOXA_PUART_SFR);
3149 reg_flag |= MOXA_SFR_FORCE_TX;
3150 MX_WRITE_REG(reg_flag, info->base + MOXA_PUART_SFR);
3152 MX_WRITE_REG(MX_READ_REG(info->base + UART_LCR) | UART_LCR_SBC, info->base + UART_LCR);
3155 if(state == MX_BREAK_OFF){
3156 MX_WRITE_REG(MX_READ_REG(info->base + UART_LCR) & ~UART_LCR_SBC, info->base + UART_LCR);
3158 reg_flag = MX_READ_REG(info->base + MOXA_PUART_SFR);
3159 reg_flag &= ~MOXA_SFR_FORCE_TX;
3160 MX_WRITE_REG(reg_flag, info->base + MOXA_PUART_SFR);
3162 MX_WRITE_REG(UART_FCR_CLEAR_XMIT, info->base + UART_FCR);
3164 mx_set_baud(info, origin_speed);
3169 module_init(mxupcie_module_init);
3170 module_exit(mxupcie_module_exit);