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.
31 #include <linux/version.h>
32 #define VERSION_CODE(ver,rel,seq) ((ver << 16) | (rel << 8) | seq)
35 #if (LINUX_VERSION_CODE < VERSION_CODE(2,6,18))
36 #include <linux/config.h>
39 #include <linux/modversions.h>
41 #include <linux/module.h>
43 //#define MOD_INC_USE_COUNT
44 //#define MOD_DEC_USE_COUNT
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>
62 #include <linux/smp_lock.h>
63 #include <linux/delay.h>
65 #include <asm/system.h>
68 #include <asm/segment.h>
69 #include <asm/bitops.h>
74 #include <linux/pci.h>
77 #define MXUPCIE_VERSION "1.13"
78 #define MXUPCIEMAJOR 31
79 #define MXUPCIECUMAJOR 34
82 #include <linux/pci.h>
83 #endif /* ENABLE_PCI */
87 MXUPCIE_BOARD_CP102E
= 1,
88 MXUPCIE_BOARD_CP102EL
,
89 MXUPCIE_BOARD_CP132EL
,
93 static char *mxupcie_brdname
[] = {
100 static int mxupcie_numports
[] = {
108 #if (LINUX_VERSION_CODE < VERSION_CODE(2,6,0))
110 unsigned short vendor
;
111 unsigned short device
;
112 unsigned short driver_data
;
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
},
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
);
137 typedef struct _moxa_pci_info
{
138 unsigned short busNum
;
139 unsigned short devNum
;
140 struct pci_dev
*pdev
;
143 static int ttymajor
=MXUPCIEMAJOR
;
144 static int calloutmajor
=MXUPCIECUMAJOR
;
145 static unsigned char interface
=0;
146 static unsigned char terminator
=0;
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);
163 MODULE_PARM(ttymajor
, "i");
164 MODULE_PARM(calloutmajor
, "i");
165 MODULE_PARM(interface
, "b");
166 MODULE_PARM(terminator
, "b");
174 unsigned long rxcnt
[MXUPCIE_PORTS
];
175 unsigned long txcnt
[MXUPCIE_PORTS
];
182 unsigned long up_rxcnt
;
183 unsigned long up_txcnt
;
185 unsigned char hold_reason
;
189 struct mxupcie_hwconf
{
193 unsigned long iobar3_addr
;
194 unsigned long vector_mask
;
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
{
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
;
218 unsigned short closing_wait
;
219 int IER
; /* Interrupt Enable Register */
220 int MCR
; /* Modem control register */
222 int count
; /* # of fd on device */
223 int blocked_open
; /* # of blocked opens */
224 unsigned char *xmit_buf
;
228 #if (LINUX_VERSION_CODE < VERSION_CODE(2,6,0))
229 struct tq_struct tqueue
;
231 struct work_struct tqueue
;
233 #if (LINUX_VERSION_CODE > VERSION_CODE(2,6,19))
234 struct ktermios normal_termios
;
235 struct ktermios callout_termios
;
237 struct termios normal_termios
;
238 struct termios callout_termios
;
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 */
245 int MaxCanSetBaudRate
; // add by Victor Yu. 09-04-2002
247 struct mxupcie_mon mon_data
;
248 unsigned char err_shadow
;
251 int custom_baud_rate
;
252 unsigned long iobar3_addr
;
254 unsigned long UIR_addr
;
255 unsigned char terminator_flag
;
260 struct mxupcie_mstatus
{
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
;
275 static struct tty_driver
*mxvar_sdriver
;
277 #if (LINUX_VERSION_CODE < VERSION_CODE(2,6,0))
278 static int mxvar_refcount
;
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];
286 static struct termios
* mxvar_termios
[MXUPCIE_PORTS
+1];
287 static struct termios
* mxvar_termios_locked
[MXUPCIE_PORTS
+1];
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
];
310 int init_module(void);
311 void cleanup_module(void);
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);
320 static int mxupcie_write(struct tty_struct
*, const unsigned char *, int);
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
*);
333 static void mxupcie_set_termios(struct tty_struct
*, struct termios
*);
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);
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 *);
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
);
351 static int mx_get_PCI_conf(int ,int ,int ,struct mxupcie_hwconf
*);
353 #if (LINUX_VERSION_CODE > VERSION_CODE(2,6,19))
354 static void mx_do_softint(struct work_struct
*work
);
356 static void mx_do_softint(void *);
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
);
362 static IRQ_RET
mx_interrupt(int, void *, struct pt_regs
*);
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
);
373 static int mx_change_speed(struct mxupcie_struct
*, struct termios
*old_termios
);
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
,
411 * The MOXA Smartio/Industio serial driver boot-time initialization code!
413 INIT_FUNC_RET
INIT_FUNC(void)
422 CLEAR_FUNC_RET
CLEAR_FUNC(void)
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");
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){
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
;
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);
465 printk(" ttyMUE%d - ttyMUE%d ", n
, n
+hwconf
->ports
-1);
467 printk(" max. baud rate = %d bps.\n", hwconf
->MaxCanSetBaudRate
[0]);
469 for ( i
=0; i
<hwconf
->ports
; i
++, n
++, info
++ ) {
471 info
->base
= hwconf
->ioaddr
[i
];
472 info
->irq
= hwconf
->irq
;
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
;
480 switch(hwconf
->board_type
){
481 case MXUPCIE_BOARD_CP114EL
:
482 info
->UIR_addr
= hwconf
->iobar3_addr
+ MOXA_UIR_OFFSET
+ 3;
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
);
500 INIT_WORK(&info
->tqueue
, mx_do_softint
, info
);
502 #if (LINUX_VERSION_CODE < VERSION_CODE(2,6,0))
503 info
->callout_termios
= mxvar_cdriver
.init_termios
;
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
);
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
);
518 switch(info
->board_type
){
519 case MXUPCIE_BOARD_CP132EL
:
520 interface
= MOXA_UIR_RS485_2W
;
523 interface
= MOXA_UIR_RS232
;
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
);
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
);
556 static void mx_getcfg(int board
,struct mxupcie_hwconf
*hwconf
)
558 mxupciecfg
[board
] = *hwconf
;
563 static int mx_get_PCI_conf(int busnum
,int devnum
,int board_type
,struct mxupcie_hwconf
*hwconf
)
566 unsigned char *ioaddress
;
567 struct pci_dev
*pdev
=hwconf
->pciInfo
.pdev
;
568 unsigned long iobar_addr
;
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),
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),
586 for (i
= 0; i
< hwconf
->ports
; i
++) {
587 hwconf
->ioaddr
[i
] = ioaddress
+ (i
* MX_PUART_SIZE
);
590 case MXUPCIE_BOARD_CP114EL
:
591 hwconf
->ioaddr
[i
] = ioaddress
+ (MX_PORT8
* MX_PUART_SIZE
);
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;
620 struct pci_dev
*pdev
=NULL
;
622 unsigned char busnum
,devnum
;
624 struct mxupcie_hwconf hwconf
;
626 #if (LINUX_VERSION_CODE >= VERSION_CODE(2,6,0))
627 mxvar_sdriver
= alloc_tty_driver(MXUPCIE_PORTS
+ 1);
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
;
654 DRV_VAR_P(refcount
) = &mxvar_refcount
;
655 DRV_VAR_P(table
) = mxvar_tty
;
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
);
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 */
701 #if (LINUX_VERSION_CODE < VERSION_CODE(2,6,0))
706 int n
= (sizeof(mxupcie_pcibrds
) / sizeof(mxupcie_pcibrds
[0])) - 1;
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
);
716 pdev
= pci_get_device(mxupcie_pcibrds
[b
].vendor
,
717 mxupcie_pcibrds
[b
].device
, pdev
);
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);
731 if ( m
>= MXUPCIE_BOARDS
) {
732 printk("Too many Smartio/Industio family boards find (maximum %d),board not configured\n",MXUPCIE_BOARDS
);
735 if ( pci_enable_device(pdev
) ) {
736 printk("Moxa SmartI/O PCI enable fail !\n");
739 retval
= mx_get_PCI_conf(busnum
,devnum
,
740 (int)mxupcie_pcibrds
[b
].driver_data
,&hwconf
);
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");
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)
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
)) ){
776 tty_unregister_driver(DRV_VAR
);
777 printk("Couldn't install MOXA Smartio/Industio family callout driver !\n");
783 printk("Couldn't install MOXA Smartio/Industio family driver !\n");
787 for(i
=0; i
<MXUPCIE_BOARDS
; i
++){
788 if(mxupciecfg
[i
].board_type
== -1)
791 free_irq(mxupciecfg
[i
].irq
, &mxvar_table
[i
*MXUPCIE_PORTS_PER_BOARD
]);
792 //todo: release io, vector
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
);
809 static void mx_do_softint(void *private_
)
811 struct mxupcie_struct
* info
= (struct mxupcie_struct
*)private_
;
813 struct tty_struct
* 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
);
824 if ( test_and_clear_bit(MXUPCIE_EVENT_TXLOW
, &info
->event
) )
827 if ( test_and_clear_bit(MXUPCIE_EVENT_HANGUP
, &info
->event
) )
830 #if (LINUX_VERSION_CODE < VERSION_CODE(2,6,0))
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
];
847 mxupcie_msr
[port
] = 0;
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
;
869 if ( line
== MXUPCIE_PORTS
)
874 if ( (line
< 0) || (line
> MXUPCIE_PORTS
) )
877 info
= mxvar_table
+ line
;
882 tty
->driver_data
= info
;
885 //printk("port %d, mxupcie_open\r\n", info->port);
887 * Start up serial port
891 retval
= mx_startup(info
);
896 retval
= mx_block_til_ready(tty
, filp
, info
);
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
;
905 *tty
->termios
= info
->callout_termios
;
907 MX_LOCK(&info
->slock
);
908 mx_change_speed(info
, 0);
909 MX_UNLOCK(&info
->slock
);
913 clear_bit(TTY_DONT_FLIP
, &tty
->flags
); // since VERSION_CODE >= 2.6.18
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;
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
;
937 unsigned long timeout
;
938 unsigned char reg_flag
;
941 if ( PORTNO(tty
) == MXUPCIE_PORTS
)
950 MX_LOCK(&info
->slock
);
952 if ( tty_hung_up_p(filp
) ) {
953 MX_UNLOCK(&info
->slock
);
960 if ( (tty
->count
== 1) && (info
->count
!= 1) ) {
962 #if (LINUX_VERSION_CODE < VERSION_CODE(2,4,21))
963 if ( (tty
->count
== 1) && (info
->count
!= 1) ) {
965 if ((atomic_read(&tty
->count
) == 1) && (info
->count
!= 1)) {
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
);
980 if ( --info
->count
< 0 ) {
981 printk("mxupcie_close: bad serial port count for ttys%d: %d\n",
982 info
->port
, info
->count
);
987 MX_UNLOCK(&info
->slock
);
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
);
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
) )
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
);
1051 ld
= tty_ldisc_ref(tty
);
1053 if(ld
->flush_buffer
)
1054 ld
->flush_buffer(tty
);
1056 tty_ldisc_deref(ld
);
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
);
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
)
1084 static int mxupcie_write(struct tty_struct
* tty
,
1085 const unsigned char * buf
, int count
)
1089 struct mxupcie_struct
*info
= (struct mxupcie_struct
*)tty
->driver_data
;
1092 if ( !tty
|| !info
->xmit_buf
)
1097 c
= MIN(count
, MIN(SERIAL_XMIT_SIZE
- info
->xmit_cnt
- 1,
1098 SERIAL_XMIT_SIZE
- info
->xmit_head
));
1102 #if (LINUX_VERSION_CODE < VERSION_CODE(2,6,10))
1104 if(copy_from_user(info
->xmit_buf
+info
->xmit_head
, buf
, c
)==c
){
1110 memcpy(info
->xmit_buf
+ info
->xmit_head
, buf
, c
);
1112 memcpy(info
->xmit_buf
+ info
->xmit_head
, buf
, c
);
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
);
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);
1138 static void mxupcie_put_char(struct tty_struct
* tty
, unsigned char ch
)
1140 struct mxupcie_struct
*info
= (struct mxupcie_struct
*)tty
->driver_data
;
1143 if ( !tty
|| !info
->xmit_buf
)
1147 if ( info
->xmit_cnt
>= SERIAL_XMIT_SIZE
- 1 )
1150 MX_LOCK(&info
->slock
);
1151 info
->xmit_buf
[info
->xmit_head
++] = ch
;
1152 info
->xmit_head
&= SERIAL_XMIT_SIZE
- 1;
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
;
1175 //printk("%lu, mxupcie_flush_chars\n", jiffies);
1177 if ( info
->xmit_cnt
<= 0 || tty
->stopped
|| !info
->xmit_buf
)
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
;
1194 ret
= SERIAL_XMIT_SIZE
- info
->xmit_cnt
- 1;
1203 static int mxupcie_chars_in_buffer(struct tty_struct
* tty
)
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
);
1214 //printk("%lu, mxupcie_chars_in_buffer=%d\n", jiffies, len);
1219 static void mxupcie_flush_buffer(struct tty_struct
* tty
)
1221 struct mxupcie_struct
*info
= (struct mxupcie_struct
*)tty
->driver_data
;
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
)
1247 struct mxupcie_struct
* info
= (struct mxupcie_struct
*)tty
->driver_data
;
1249 struct async_icount cprev
, cnow
; /* kernel counter temps */
1250 struct serial_icounter_struct
*p_cuser
; /* user space */
1251 unsigned long templ
;
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
) {
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
) )
1267 get_from_user(speed
,(int *)arg
);
1269 if ( speed
<= 0 || speed
> info
->MaxCanSetBaudRate
)
1272 if ( !info
->tty
|| !info
->tty
->termios
|| !info
->base
)
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
;
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
);
1311 error
= MX_ACCESS_CHK(VERIFY_WRITE
, (void *)arg
, sizeof(int));
1313 if ( MX_ERR(error
) )
1316 if(copy_to_user((int*)arg
, &info
->speed
, sizeof(int)))
1323 if ( (cmd
!= TIOCGSERIAL
) && (cmd
!= TIOCMIWAIT
) &&
1324 (cmd
!= TIOCGICOUNT
) ) {
1326 if ( tty
->flags
& (1 << TTY_IO_ERROR
) ){
1332 case TCSBRK
: /* SVID version: non-zero arg --> no break */
1333 retval
= tty_check_change(tty
);
1338 tty_wait_until_sent(tty
, 0);
1341 mx_send_break(info
, HZ
/4); /* 1/4 second */
1344 case TCSBRKP
: /* support for POSIX tcsendbreak() */
1345 retval
= tty_check_change(tty
);
1350 tty_wait_until_sent(tty
, 0);
1351 mx_send_break(info
, arg
? arg
*(HZ
/10) : HZ
/4);
1355 error
= MX_ACCESS_CHK(VERIFY_WRITE
, (void *)arg
, sizeof(long));
1357 if ( MX_ERR(error
) )
1360 put_to_user(C_CLOCAL(tty
) ? 1 : 0, (unsigned long *)arg
);
1364 error
= MX_ACCESS_CHK(VERIFY_READ
, (void *)arg
, sizeof(long));
1366 if ( MX_ERR(error
) )
1369 get_from_user(templ
,(unsigned long *)arg
);
1371 tty
->termios
->c_cflag
= ((tty
->termios
->c_cflag
& ~CLOCAL
) | (arg
? CLOCAL
: 0));
1374 #if (LINUX_VERSION_CODE < VERSION_CODE(2,6,0))
1376 error
= MX_ACCESS_CHK(VERIFY_WRITE
, (void *)arg
, sizeof(unsigned int));
1378 if ( MX_ERR(error
) )
1381 return mx_get_modem_info(info
, (unsigned int *)arg
);
1385 return mx_set_modem_info(info
, cmd
, (unsigned int *)arg
);
1388 error
= MX_ACCESS_CHK(VERIFY_WRITE
, (void *)arg
,sizeof(struct serial_struct
));
1390 if ( MX_ERR(error
) )
1393 return mx_get_serial_info(info
, (struct serial_struct
*)arg
);
1395 error
= MX_ACCESS_CHK(VERIFY_READ
, (void *)arg
,sizeof(struct serial_struct
));
1397 if ( MX_ERR(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
) )
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
1415 DECLARE_WAITQUEUE(wait
, current
);
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
);
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
)) ) {
1436 /* see if a signal did it */
1437 if ( signal_pending(current
) ){
1445 set_current_state(TASK_RUNNING
);
1446 remove_wait_queue(&info
->delta_msr_wait
, &wait
);
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.
1457 error
= MX_ACCESS_CHK(VERIFY_WRITE
, (void *)arg
,sizeof(struct serial_icounter_struct
));
1459 if ( MX_ERR(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
))
1470 if (put_user(cnow
.brk
, &p_cuser
->brk
))
1473 if (put_user(cnow
.overrun
, &p_cuser
->overrun
))
1476 if (put_user(cnow
.buf_overrun
, &p_cuser
->buf_overrun
))
1479 if (put_user(cnow
.parity
, &p_cuser
->parity
))
1482 if (put_user(cnow
.rx
, &p_cuser
->rx
))
1485 if (put_user(cnow
.tx
, &p_cuser
->tx
))
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
);
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
)))
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
)))
1518 return(-ENOIOCTLCMD
);
1524 static int mx_ioctl_special(unsigned int cmd
, unsigned long arg
)
1526 int error
, i
, result
, status
;
1530 error
= MX_ACCESS_CHK(VERIFY_WRITE
, (void *)arg
,sizeof(struct mxupcie_hwconf
)*4);
1532 if ( MX_ERR(error
) )
1535 if(copy_to_user((struct mxupcie_hwconf
*)arg
, mxupciecfg
,sizeof(struct mxupcie_hwconf
)*4))
1540 case MOXA_GET_MAJOR
:
1541 error
= MX_ACCESS_CHK(VERIFY_WRITE
, (void *)arg
, sizeof(int));
1543 if ( MX_ERR(error
) )
1546 if(copy_to_user((int*)arg
, &ttymajor
, sizeof(int)))
1551 case MOXA_GET_CUMAJOR
:
1552 error
= MX_ACCESS_CHK(VERIFY_WRITE
, (void *)arg
, sizeof(int));
1554 if ( MX_ERR(error
) )
1557 if(copy_to_user((int*)arg
, &calloutmajor
, sizeof(int)))
1562 case MOXA_CHKPORTENABLE
:
1563 error
= MX_ACCESS_CHK(VERIFY_WRITE
, (void *)arg
, sizeof(long));
1565 if ( MX_ERR(error
) )
1570 for ( i
=0; i
<MXUPCIE_PORTS
; i
++ ) {
1571 if ( mxvar_table
[i
].base
)
1575 put_to_user(result
, (unsigned long *)arg
);
1579 case MOXA_GETDATACOUNT
:
1580 error
= MX_ACCESS_CHK(VERIFY_WRITE
, (void *)arg
,sizeof(struct mxupcie_log
));
1582 if ( MX_ERR(error
) )
1585 if(copy_to_user((struct mxupcie_log
*)arg
, &mxvar_log
, sizeof(mxvar_log
)))
1590 case MOXA_GETMSTATUS
:
1591 error
= MX_ACCESS_CHK(VERIFY_WRITE
, (void *)arg
,sizeof(struct mxupcie_mstatus
) * MXUPCIE_PORTS
);
1593 if ( MX_ERR(error
) )
1596 for(i
=0; i
<MXUPCIE_PORTS
; i
++){
1599 if ( !mxvar_table
[i
].base
){
1600 GMStatus
[i
].dcd
= 0;
1601 GMStatus
[i
].dsr
= 0;
1602 GMStatus
[i
].cts
= 0;
1607 if ( !mxvar_table
[i
].tty
|| !mxvar_table
[i
].tty
->termios
)
1608 GMStatus
[i
].cflag
=mxvar_table
[i
].normal_termios
.c_cflag
;
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;
1617 GMStatus
[i
].dcd
= 0;
1619 if(status
& 0x20/*UART_MSR_DSR*/)
1620 GMStatus
[i
].dsr
= 1;
1622 GMStatus
[i
].dsr
= 0;
1624 if(status
& 0x10/*UART_MSR_CTS*/)
1625 GMStatus
[i
].cts
= 1;
1627 GMStatus
[i
].cts
= 0;
1630 if(copy_to_user((struct mxupcie_mstatus
*)arg
, GMStatus
,sizeof(struct mxupcie_mstatus
) * MXUPCIE_PORTS
))
1636 return -ENOIOCTLCMD
;
1643 static void mx_stoprx(struct tty_struct
* tty
)
1645 struct mxupcie_struct
*info
= (struct mxupcie_struct
*)tty
->driver_data
;
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
;
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
)
1677 static void mxupcie_unthrottle(struct tty_struct
* 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
)
1687 static void mxupcie_set_termios(struct tty_struct
* tty
,
1688 struct termios
* old_termios
)
1691 struct mxupcie_struct
*info
= (struct mxupcie_struct
*)tty
->driver_data
;
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;
1708 /* Handle sw stopped */
1709 if ( (old_termios
->c_iflag
& IXON
) &&
1710 !(tty
->termios
->c_iflag
& IXON
) ) {
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
;
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
;
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
;
1761 if (info
->type
== PORT_UNKNOWN
)
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
1776 char_time
= (info
->timeout
- HZ
/50) / info
->xmit_fifo_size
;
1777 char_time
= char_time
/ 5;
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
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
);
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
);
1804 set_current_state(TASK_INTERRUPTIBLE
);
1805 schedule_timeout(char_time
);
1807 if (signal_pending(current
))
1810 if (timeout
&& time_after(jiffies
, orig_jiffies
+ timeout
))
1813 set_current_state(TASK_RUNNING
);
1815 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1816 printk("lsr = %d (jiff=%lu)...done\n", lsr
, jiffies
);
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
);
1832 info
->flags
&= ~(ASYNC_NORMAL_ACTIVE
|ASYNC_CALLOUT_ACTIVE
);
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
;
1846 MX_LOCK(&info
->slock
);
1847 if (break_state
== -1){
1849 case MOXA_UIR_RS485_4W
:
1850 case MOXA_UIR_RS485_2W
:
1851 mx_software_break_signal(info
, MX_BREAK_ON
);
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
);
1861 case MOXA_UIR_RS485_4W
:
1862 case MOXA_UIR_RS485_2W
:
1863 mx_software_break_signal(info
, MX_BREAK_OFF
);
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
);
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
)
1882 static IRQ_RET
mx_interrupt(int irq
, void *dev_id
, struct pt_regs
* regs
)
1886 struct mxupcie_struct
* info
;
1887 struct mxupcie_struct
* port
;
1889 int pass_counter
= 0;
1896 for(i
=0; i
<MXUPCIE_BOARDS
; i
++){
1897 if(dev_id
== &(mxvar_table
[i
*MXUPCIE_PORTS_PER_BOARD
])){
1903 if(i
==MXUPCIE_BOARDS
)
1909 max
= mxupcie_numports
[mxupciecfg
[i
].board_type
-1];
1920 iir
= MX_READ_REG(info
->base
+UART_IIR
);
1922 if ( iir
== MOXA_IIR_NO_INT
){
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
);
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
)
1974 }while(pass_counter
++ < MXUPCIE_ISR_PASS_LIMIT
);
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
,
1984 struct tty_struct
* tty
= info
->tty
;
1985 unsigned char ch
, gdl
=0;
1989 unsigned long flags
;
1991 if ( *status
& UART_LSR_SPECIAL
)
1994 recv_room
= tty_buffer_request_room(tty
,MX_RX_FIFO_SIZE
);
1997 gdl
= MX_READ_REG(info
->base
+ MOXA_PUART_RCNT
);
2003 tty_insert_flip_string(tty
, info
->base
+ MOXA_PUART_MEMRBR
, gdl
);
2008 set_bit(TTY_THROTTLED
, &tty
->flags
);
2019 ch
= MX_READ_REG(info
->base
+ UART_RX
);
2021 if ( *status
& UART_LSR_SPECIAL
) {
2022 if ( *status
& UART_LSR_BI
) {
2026 if ( info
->flags
& ASYNC_SAK
)
2028 } else if ( *status
& UART_LSR_PE
) {
2030 info
->icount
.parity
++;
2031 } else if ( *status
& UART_LSR_FE
) {
2033 info
->icount
.frame
++;
2034 } else if ( *status
& UART_LSR_OE
) {
2035 flags
= TTY_OVERRUN
;
2036 info
->icount
.overrun
++;
2043 tty_insert_flip_char(tty
, ch
, flags
);
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
;
2058 static void mx_receive_chars(struct mxupcie_struct
*info
,
2061 struct tty_struct
* tty
= info
->tty
;
2062 unsigned char ch
, gdl
;
2069 cp
= tty
->flip
.char_buf
;
2070 fp
= tty
->flip
.flag_buf
;
2072 if ( *status
& UART_LSR_SPECIAL
)
2075 recv_room
= tty
->ldisc
.receive_room(tty
);
2078 gdl
= MX_READ_REG(info
->base
+ MOXA_PUART_RCNT
);
2085 memcpy(cp
,info
->base
+ MOXA_PUART_MEMRBR
,cnt
);
2086 memset(fp
, TTY_NORMAL
, cnt
);
2092 set_bit(TTY_THROTTLED
, &tty
->flags
);
2102 ch
= MX_READ_REG(info
->base
+ UART_RX
);
2104 if ( *status
& UART_LSR_SPECIAL
) {
2105 if ( *status
& UART_LSR_BI
) {
2108 if ( info
->flags
& ASYNC_SAK
)
2110 } else if ( *status
& UART_LSR_PE
) {
2112 info
->icount
.parity
++;
2113 } else if ( *status
& UART_LSR_FE
) {
2115 info
->icount
.frame
++;
2116 } else if ( *status
& UART_LSR_OE
) {
2117 *fp
++ = TTY_OVERRUN
;
2118 info
->icount
.overrun
++;
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]);
2143 static void mx_transmit_chars(struct mxupcie_struct
*info
)
2148 if ( info
->xmit_buf
== 0 ){
2152 if(info
->xmit_cnt
==0){
2153 if ( info
->xmit_cnt
< WAKEUP_CHARS
) {
2154 set_bit(MXUPCIE_EVENT_TXLOW
,&info
->event
);
2160 if (info
->tty
->stopped
){
2161 info
->IER
&= ~UART_IER_THRI
;
2162 MX_WRITE_REG(info
->IER
, info
->base
+ UART_IER
);
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
));
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
);
2192 static void mx_check_modem_status(struct mxupcie_struct
*info
,
2195 /* update input line counters */
2196 if ( status
& UART_MSR_TERI
)
2198 if ( status
& UART_MSR_DDSR
)
2200 if ( status
& UART_MSR_DDCD
)
2202 if ( status
& UART_MSR_DCTS
)
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
);
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
);
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
);
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
)
2249 info
->flags
|= ASYNC_NORMAL_ACTIVE
;
2254 if ( info
->flags
& ASYNC_CALLOUT_ACTIVE
) {
2255 if ( info
->normal_termios
.c_cflag
& CLOCAL
)
2258 if ( tty
->termios
->c_cflag
& CLOCAL
)
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.
2270 add_wait_queue(&info
->open_wait
, &wait
);
2272 MX_LOCK(&info
->slock
);
2273 if ( !tty_hung_up_p(filp
) )
2275 MX_UNLOCK(&info
->slock
);
2277 info
->blocked_open
++;
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
)
2292 retval
= -ERESTARTSYS
;
2299 if ( !(info
->flags
& ASYNC_CALLOUT_ACTIVE
) && !(info
->flags
& ASYNC_CLOSING
) &&
2300 (do_clocal
|| (MX_READ_REG(info
->base
+ UART_MSR
) & UART_MSR_DCD
)) )
2303 if ( signal_pending(current
) ) {
2304 retval
= -ERESTARTSYS
;
2311 set_current_state(TASK_RUNNING
);
2312 remove_wait_queue(&info
->open_wait
, &wait
);
2314 if ( !tty_hung_up_p(filp
) )
2317 info
->blocked_open
--;
2322 info
->flags
|= ASYNC_NORMAL_ACTIVE
;
2327 static int mx_startup(struct mxupcie_struct
* info
)
2331 unsigned char reg_flag
;
2334 page
= GET_FPAGE(GFP_KERNEL
);
2338 MX_LOCK(&info
->slock
);
2340 if ( info
->flags
& ASYNC_INITIALIZED
) {
2342 MX_UNLOCK(&info
->slock
);
2347 if ( !info
->base
|| !info
->type
) {
2349 set_bit(TTY_IO_ERROR
, &info
->tty
->flags
);
2351 MX_UNLOCK(&info
->slock
);
2356 if ( info
->xmit_buf
)
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
2375 if ( MX_READ_REG(info
->base
+ UART_LSR
) == 0xff ) {
2376 MX_UNLOCK(&info
->slock
);
2378 if (capable(CAP_SYS_ADMIN
)) {
2380 set_bit(TTY_IO_ERROR
, &info
->tty
->flags
);
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
);
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
;
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
;
2455 if ( !(info
->flags
& ASYNC_INITIALIZED
) )
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
);
2475 MX_WRITE_REG(reg_flag
, info
->base
+ MOXA_PUART_EFR
);
2476 MX_WRITE_REG(reg_flag
, info
->base
+ MOXA_PUART_SFR
);
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
);
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
)
2507 static int mx_change_speed(struct mxupcie_struct
*info
,
2508 struct termios
*old_termios
)
2511 unsigned cflag
, cval
, fcr
;
2517 if ( !info
->tty
|| !info
->tty
->termios
)
2520 cflag
= info
->tty
->termios
->c_cflag
;
2522 if ( !(info
->base
) )
2526 #define B921600 (B460800 +1)
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;
2544 #ifdef ASYNC_SPD_WARP
2545 else if((info
->flags
& ASYNC_SPD_MASK
)==ASYNC_SPD_WARP
)
2546 info
->speed
= baud
= 460800;
2548 #ifdef ASYNC_SPD_CUST
2549 else if((info
->flags
& ASYNC_SPD_MASK
)==ASYNC_SPD_CUST
){
2550 info
->speed
= baud
= info
->custom_baud_rate
;
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
)
2585 if ( cflag
& PARENB
)
2586 cval
|= UART_LCR_PARITY
;
2589 #define CMSPAR 010000000000
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
);
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
;
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
;
2659 reg_flag
&= ~MOXA_EFR_TX_SW
;
2661 if ( I_IXOFF(info
->tty
) )
2662 reg_flag
|= MOXA_EFR_RX_SW
;
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
);
2674 static int mx_set_baud(struct mxupcie_struct
*info
, long newspd
)
2681 if ( !info
->tty
|| !info
->tty
->termios
)
2684 if ( !(info
->base
) )
2687 if ( newspd
> info
->MaxCanSetBaudRate
)
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
)
2699 if ( newspd
== 134 ) {
2700 quot
= (2 * info
->baud_base
/ 269);
2702 } else if ( newspd
) {
2703 quot
= info
->baud_base
/ newspd
;
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 */
2716 info
->MCR
|= UART_MCR_DTR
;
2717 MX_WRITE_REG(info
->MCR
, info
->base
+ UART_MCR
);
2719 info
->MCR
&= ~UART_MCR_DTR
;
2720 MX_WRITE_REG(info
->MCR
, info
->base
+ UART_MCR
);
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
;
2734 if ( (quot
% info
->speed
) > (info
->speed
/ 2) ) {
2735 quot
/= info
->speed
;
2738 quot
/= info
->speed
;
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
;
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
;
2772 if(copy_to_user(retinfo
, &tmp
, sizeof(*retinfo
)))
2778 static int mx_set_serial_info(struct mxupcie_struct
* info
,
2779 struct serial_struct
* new_info
)
2781 struct serial_struct new_serial
;
2787 if ( !new_info
|| !info
->base
)
2790 if(copy_from_user(&new_serial
, new_info
, sizeof(new_serial
)))
2793 if ( (new_serial
.irq
!= info
->irq
) ||
2794 (new_serial
.port
!= *info
->base
) )
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
)) )
2806 info
->flags
= ((info
->flags
& ~ASYNC_USR_MASK
) | (new_serial
.flags
& ASYNC_USR_MASK
));
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
);
2832 retval
= mx_startup(info
);
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
;
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
);
2864 * This routine sends a break character out the serial port.
2866 static void mx_send_break(struct mxupcie_struct
* info
, int duration
)
2873 set_current_state(TASK_INTERRUPTIBLE
);
2875 MX_LOCK(&info
->slock
);
2877 case MOXA_UIR_RS485_4W
:
2878 case MOXA_UIR_RS485_2W
:
2879 mx_software_break_signal(info
, MX_BREAK_ON
);
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
);
2886 MX_UNLOCK(&info
->slock
);
2888 schedule_timeout(duration
);
2890 MX_LOCK(&info
->slock
);
2892 case MOXA_UIR_RS485_4W
:
2893 case MOXA_UIR_RS485_2W
:
2894 mx_software_break_signal(info
, MX_BREAK_OFF
);
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
);
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
;
2912 //printk("%lu,tiocmget\n", jiffies);
2914 if (PORTNO(tty
) == MXUPCIE_PORTS
)
2915 return -ENOIOCTLCMD
;
2917 if (tty
->flags
& (1 << TTY_IO_ERROR
))
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
;
2946 //printk("%lu,tiocmset\n", jiffies);
2948 if (PORTNO(tty
) == MXUPCIE_PORTS
)
2949 return (-ENOIOCTLCMD
);
2951 if (tty
->flags
& (1 << TTY_IO_ERROR
))
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
);
2972 static int mx_get_modem_info(struct mxupcie_struct
* info
,
2973 unsigned int *value
)
2975 unsigned char control
, status
;
2976 unsigned int result
;
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
);
2999 static int mx_set_modem_info(struct mxupcie_struct
* info
, unsigned int cmd
,
3000 unsigned int *value
)
3005 error
= MX_ACCESS_CHK(VERIFY_READ
, value
, sizeof(int));
3007 if ( MX_ERR(error
) )
3010 get_from_user(arg
,value
);
3014 if ( arg
& TIOCM_RTS
)
3015 info
->MCR
|= UART_MCR_RTS
;
3016 if ( arg
& TIOCM_DTR
)
3017 info
->MCR
|= UART_MCR_DTR
;
3020 if ( arg
& TIOCM_RTS
)
3021 info
->MCR
&= ~UART_MCR_RTS
;
3022 if ( arg
& TIOCM_DTR
)
3023 info
->MCR
&= ~UART_MCR_DTR
;
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));
3034 MX_WRITE_REG(info
->MCR
, info
->base
+ UART_MCR
);
3040 static int mx_set_interface(struct mxupcie_struct
*info
, unsigned char val
)
3042 unsigned char intf
= 0, chip_val
= 0;
3045 case MOXA_UIR_RS232
:
3046 switch(info
->board_type
){
3047 case MXUPCIE_BOARD_CP132EL
:
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
:
3065 chip_val
= MX_READ_IOBAR3_REG(info
->UIR_addr
);
3068 intf
= val
<< MOXA_UIR_EVEN_PORT_VALUE_OFFSET
;
3080 MX_WRITE_IOBAR3_REG(chip_val
, info
->UIR_addr
);
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
)
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));
3110 case MXUPCIE_BOARD_CP114EL
:
3111 chip_val
&= ~(1<<info
->port
);
3112 chip_val
|= (val
<< info
->port
);
3114 case MXUPCIE_BOARD_CP102E
:
3115 case MXUPCIE_BOARD_CP102EL
:
3120 MX_WRITE_IOBAR3_REG(chip_val
, info
->iobar3_addr
+ MOXA_PUART_GPIO_OUT
);
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;
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
);