24 #include <linux/kernel.h>
25 #include <linux/errno.h>
27 #include <linux/slab.h>
28 #include <linux/tty.h>
31 #include <linux/module.h>
34 #include <asm/unaligned.h>
37 #include <linux/serial.h>
44 #define DRIVER_VERSION "z2.1"
46 #define DRIVER_DESC "Magic Control Technology USB-RS232 converter driver"
57 static void mct_u232_read_int_callback(
struct urb *
urb);
58 static void mct_u232_set_termios(
struct tty_struct *tty,
60 static void mct_u232_break_ctl(
struct tty_struct *tty,
int break_state);
61 static int mct_u232_tiocmget(
struct tty_struct *tty);
62 static int mct_u232_tiocmset(
struct tty_struct *tty,
63 unsigned int set,
unsigned int clear);
64 static int mct_u232_ioctl(
struct tty_struct *tty,
65 unsigned int cmd,
unsigned long arg);
66 static int mct_u232_get_icount(
struct tty_struct *tty,
68 static void mct_u232_throttle(
struct tty_struct *tty);
69 static void mct_u232_unthrottle(
struct tty_struct *tty);
89 .description =
"MCT U232",
92 .open = mct_u232_open,
93 .close = mct_u232_close,
94 .dtr_rts = mct_u232_dtr_rts,
95 .throttle = mct_u232_throttle,
96 .unthrottle = mct_u232_unthrottle,
97 .read_int_callback = mct_u232_read_int_callback,
98 .set_termios = mct_u232_set_termios,
99 .break_ctl = mct_u232_break_ctl,
100 .tiocmget = mct_u232_tiocmget,
101 .tiocmset = mct_u232_tiocmset,
102 .attach = mct_u232_startup,
103 .port_probe = mct_u232_port_probe,
104 .port_remove = mct_u232_port_remove,
105 .ioctl = mct_u232_ioctl,
106 .get_icount = mct_u232_get_icount,
110 &mct_u232_device,
NULL
125 #define THROTTLED 0x01
131 #define WDR_TIMEOUT 5000
192 static int mct_u232_set_baud_rate(
struct tty_struct *tty,
198 unsigned char cts_enable_byte = 0;
205 divisor = mct_u232_calculate_baud_rate(serial, value, &speed);
213 dev_err(&port->
dev,
"Set BAUD RATE %d failed (error = %d)\n",
217 dev_dbg(&port->
dev,
"set_baud_rate: value: 0x%x, divisor: 0x%x\n", value, divisor);
243 dev_err(&port->
dev,
"Sending USB device request code %d "
250 dev_dbg(&port->
dev,
"set_baud_rate: send second control message, data = %02X\n",
252 buf[0] = cts_enable_byte;
259 dev_err(&port->
dev,
"Sending USB device request code %d "
283 dev_err(&port->
dev,
"Set LINE CTRL 0x%x failed (error = %d)\n", lcr, rc);
284 dev_dbg(&port->
dev,
"set_line_ctrl: 0x%x\n", lcr);
290 unsigned int control_state)
314 dev_dbg(&port->
dev,
"set_modem_ctrl: state=0x%x ==> mcr=0x%x\n", control_state, mcr);
317 dev_err(&port->
dev,
"Set MODEM CTRL 0x%x failed (error = %d)\n", mcr, rc);
340 dev_err(&port->
dev,
"Get MODEM STATus failed (error = %d)\n", rc);
345 dev_dbg(&port->
dev,
"get_modem_stat: 0x%x\n", *msr);
350 static void mct_u232_msr_to_icount(
struct async_icount *icount,
365 unsigned int *control_state,
unsigned char msr)
384 dev_dbg(&port->
dev,
"msr_to_state: msr=0x%x ==> state=0x%x\n", msr, *control_state);
391 static int mct_u232_startup(
struct usb_serial *serial)
396 port = serial->
port[0];
397 rport = serial->
port[1];
418 usb_set_serial_port_data(port, priv);
427 priv = usb_get_serial_port_data(port);
458 if (tty && (tty->termios.c_cflag &
CBAUD))
468 spin_unlock_irqrestore(&priv->
lock, flags);
469 mct_u232_set_modem_ctrl(port, control_state);
470 mct_u232_set_line_ctrl(port, last_lcr);
473 mct_u232_get_modem_stat(port, &last_msr);
477 spin_unlock_irqrestore(&priv->
lock, flags);
482 "usb_submit_urb(read bulk) failed pipe 0x%x err %d\n",
491 "usb_submit_urb(read int) failed pipe 0x%x err %d",
507 if (!port->
serial->disconnected) {
509 spin_lock_irq(&priv->
lock);
515 spin_unlock_irq(&priv->
lock);
516 mct_u232_set_modem_ctrl(port, control_state);
534 static void mct_u232_read_int_callback(
struct urb *
urb)
539 unsigned char *
data = urb->transfer_buffer;
552 dev_dbg(&port->
dev,
"%s - urb shutting down with status: %d\n",
556 dev_dbg(&port->
dev,
"%s - nonzero urb status received: %d\n",
561 usb_serial_debug_data(&port->
dev, __func__, urb->actual_length, data);
566 if (urb->transfer_buffer_length > 2) {
567 if (urb->actual_length) {
570 tty_insert_flip_string(tty, data,
618 spin_unlock_irqrestore(&priv->
lock, flags);
623 "%s - usb_submit_urb failed with result %d\n",
627 static void mct_u232_set_termios(
struct tty_struct *tty,
635 unsigned int old_cflag = old_termios->
c_cflag;
637 unsigned int control_state;
638 unsigned char last_lcr;
643 spin_unlock_irqrestore(&priv->
lock, flags);
654 if ((old_cflag &
CBAUD) ==
B0) {
655 dev_dbg(&port->
dev,
"%s: baud was B0\n", __func__);
657 mct_u232_set_modem_ctrl(port, control_state);
662 if ((cflag & CBAUD) ==
B0) {
663 dev_dbg(&port->
dev,
"%s: baud is B0\n", __func__);
665 control_state &= ~(TIOCM_DTR |
TIOCM_RTS);
666 mct_u232_set_modem_ctrl(port, control_state);
675 last_lcr |= (cflag &
PARODD) ?
681 switch (cflag &
CSIZE) {
692 "CSIZE was not CS5-CS8, using default of 8\n");
700 last_lcr |= (cflag &
CSTOPB) ?
703 mct_u232_set_line_ctrl(port, last_lcr);
709 spin_unlock_irqrestore(&priv->
lock, flags);
712 static void mct_u232_break_ctl(
struct tty_struct *tty,
int break_state)
724 spin_unlock_irqrestore(&priv->
lock, flags);
726 mct_u232_set_line_ctrl(port, lcr);
730 static int mct_u232_tiocmget(
struct tty_struct *tty)
739 spin_unlock_irqrestore(&priv->
lock, flags);
744 static int mct_u232_tiocmset(
struct tty_struct *tty,
745 unsigned int set,
unsigned int clear)
759 if (clear & TIOCM_RTS)
760 control_state &= ~TIOCM_RTS;
761 if (clear & TIOCM_DTR)
762 control_state &= ~TIOCM_DTR;
765 spin_unlock_irqrestore(&priv->
lock, flags);
766 return mct_u232_set_modem_ctrl(port, control_state);
769 static void mct_u232_throttle(
struct tty_struct *tty)
775 spin_lock_irq(&priv->
lock);
780 spin_unlock_irq(&priv->
lock);
781 mct_u232_set_modem_ctrl(port, control_state);
783 spin_unlock_irq(&priv->
lock);
787 static void mct_u232_unthrottle(
struct tty_struct *tty)
793 spin_lock_irq(&priv->
lock);
798 spin_unlock_irq(&priv->
lock);
799 mct_u232_set_modem_ctrl(port, control_state);
801 spin_unlock_irq(&priv->
lock);
805 static int mct_u232_ioctl(
struct tty_struct *tty,
806 unsigned int cmd,
unsigned long arg)
814 dev_dbg(&port->
dev,
"%s - cmd = 0x%x\n", __func__, cmd);
820 dev_dbg(&port->
dev,
"%s TIOCMIWAIT", __func__);
823 cprev = mct_u232_port->
icount;
824 spin_unlock_irqrestore(&mct_u232_port->
lock, flags);
834 cnow = mct_u232_port->
icount;
835 spin_unlock_irqrestore(&mct_u232_port->
lock, flags);
836 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
837 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
839 if (((arg &
TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
840 ((arg &
TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
841 ((arg &
TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
842 ((arg &
TIOCM_CTS) && (cnow.cts != cprev.cts))) {
852 static int mct_u232_get_icount(
struct tty_struct *tty,
874 spin_unlock_irqrestore(&mct_u232_port->
lock, flags);
876 dev_dbg(&port->
dev,
"%s TIOCGICOUNT RX=%d, TX=%d\n",
877 __func__, icount->
rx, icount->
tx);