2 * MCT (Magic Control Technology Corp.) USB RS232 Converter Driver
4 * Copyright (C) 2000 Wolfgang Grandegger (wolfgang@ces.ch)
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is largely derived from the Belkin USB Serial Adapter Driver
12 * (see belkin_sa.[ch]). All of the information about the device was acquired
13 * by using SniffUSB on Windows98. For technical details see mct_u232.h.
15 * William G. Greathouse and Greg Kroah-Hartman provided great help on how to
16 * do the reverse engineering and how to write a USB serial device driver.
18 * TO BE DONE, TO BE CHECKED:
19 * DTR/RTS signal handling may be incomplete or incorrect. I have mainly
20 * implemented what I have seen with SniffUSB or found in belkin_sa.c.
21 * For further TODOs check also belkin_sa.c.
24 #include <linux/kernel.h>
25 #include <linux/errno.h>
26 #include <linux/init.h>
27 #include <linux/slab.h>
28 #include <linux/tty.h>
29 #include <linux/tty_driver.h>
30 #include <linux/tty_flip.h>
31 #include <linux/module.h>
32 #include <linux/spinlock.h>
33 #include <linux/uaccess.h>
34 #include <asm/unaligned.h>
35 #include <linux/usb.h>
36 #include <linux/usb/serial.h>
37 #include <linux/serial.h>
38 #include <linux/ioctl.h>
44 #define DRIVER_VERSION "z2.1" /* Linux in-kernel version */
45 #define DRIVER_AUTHOR "Wolfgang Grandegger <wolfgang@ces.ch>"
46 #define DRIVER_DESC "Magic Control Technology USB-RS232 converter driver"
53 static int mct_u232_startup(struct usb_serial *serial);
54 static void mct_u232_release(struct usb_serial *serial);
55 static int mct_u232_open(struct tty_struct *tty, struct usb_serial_port *port);
56 static void mct_u232_close(struct usb_serial_port *port);
57 static void mct_u232_dtr_rts(struct usb_serial_port *port, int on);
58 static void mct_u232_read_int_callback(struct urb *urb);
59 static void mct_u232_set_termios(struct tty_struct *tty,
60 struct usb_serial_port *port, struct ktermios *old);
61 static void mct_u232_break_ctl(struct tty_struct *tty, int break_state);
62 static int mct_u232_tiocmget(struct tty_struct *tty);
63 static int mct_u232_tiocmset(struct tty_struct *tty,
64 unsigned int set, unsigned int clear);
65 static int mct_u232_ioctl(struct tty_struct *tty,
66 unsigned int cmd, unsigned long arg);
67 static int mct_u232_get_icount(struct tty_struct *tty,
68 struct serial_icounter_struct *icount);
69 static void mct_u232_throttle(struct tty_struct *tty);
70 static void mct_u232_unthrottle(struct tty_struct *tty);
74 * All of the device info needed for the MCT USB-RS232 converter.
76 static const struct usb_device_id id_table_combined[] = {
77 { USB_DEVICE(MCT_U232_VID, MCT_U232_PID) },
78 { USB_DEVICE(MCT_U232_VID, MCT_U232_SITECOM_PID) },
79 { USB_DEVICE(MCT_U232_VID, MCT_U232_DU_H3SP_PID) },
80 { USB_DEVICE(MCT_U232_BELKIN_F5U109_VID, MCT_U232_BELKIN_F5U109_PID) },
81 { } /* Terminating entry */
84 MODULE_DEVICE_TABLE(usb, id_table_combined);
86 static struct usb_driver mct_u232_driver = {
88 .disconnect = usb_serial_disconnect,
89 .id_table = id_table_combined,
92 static struct usb_serial_driver mct_u232_device = {
97 .description = "MCT U232",
98 .id_table = id_table_combined,
100 .open = mct_u232_open,
101 .close = mct_u232_close,
102 .dtr_rts = mct_u232_dtr_rts,
103 .throttle = mct_u232_throttle,
104 .unthrottle = mct_u232_unthrottle,
105 .read_int_callback = mct_u232_read_int_callback,
106 .set_termios = mct_u232_set_termios,
107 .break_ctl = mct_u232_break_ctl,
108 .tiocmget = mct_u232_tiocmget,
109 .tiocmset = mct_u232_tiocmset,
110 .attach = mct_u232_startup,
111 .release = mct_u232_release,
112 .ioctl = mct_u232_ioctl,
113 .get_icount = mct_u232_get_icount,
116 static struct usb_serial_driver * const serial_drivers[] = {
117 &mct_u232_device, NULL
120 struct mct_u232_private {
122 unsigned int control_state; /* Modem Line Setting (TIOCM) */
123 unsigned char last_lcr; /* Line Control Register */
124 unsigned char last_lsr; /* Line Status Register */
125 unsigned char last_msr; /* Modem Status Register */
126 unsigned int rx_flags; /* Throttling flags */
127 struct async_icount icount;
128 wait_queue_head_t msr_wait; /* for handling sleeping while waiting
129 for msr change to happen */
132 #define THROTTLED 0x01
135 * Handle vendor specific USB requests
138 #define WDR_TIMEOUT 5000 /* default urb timeout */
141 * Later day 2.6.0-test kernels have new baud rates like B230400 which
142 * we do not know how to support. We ignore them for the moment.
144 static int mct_u232_calculate_baud_rate(struct usb_serial *serial,
145 speed_t value, speed_t *result)
149 if (le16_to_cpu(serial->dev->descriptor.idProduct) == MCT_U232_SITECOM_PID
150 || le16_to_cpu(serial->dev->descriptor.idProduct) == MCT_U232_BELKIN_F5U109_PID) {
155 return 0x02; /* this one not tested */
177 /* FIXME: Can we use any divider - should we do
178 divider = 115200/value;
179 real baud = 115200/divider */
199 static int mct_u232_set_baud_rate(struct tty_struct *tty,
200 struct usb_serial *serial, struct usb_serial_port *port, speed_t value)
202 unsigned int divisor;
205 unsigned char cts_enable_byte = 0;
208 buf = kmalloc(MCT_U232_MAX_SIZE, GFP_KERNEL);
212 divisor = mct_u232_calculate_baud_rate(serial, value, &speed);
213 put_unaligned_le32(cpu_to_le32(divisor), buf);
214 rc = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
215 MCT_U232_SET_BAUD_RATE_REQUEST,
216 MCT_U232_SET_REQUEST_TYPE,
217 0, 0, buf, MCT_U232_SET_BAUD_RATE_SIZE,
219 if (rc < 0) /*FIXME: What value speed results */
220 dev_err(&port->dev, "Set BAUD RATE %d failed (error = %d)\n",
223 tty_encode_baud_rate(tty, speed, speed);
224 dbg("set_baud_rate: value: 0x%x, divisor: 0x%x", value, divisor);
226 /* Mimic the MCT-supplied Windows driver (version 1.21P.0104), which
227 always sends two extra USB 'device request' messages after the
228 'baud rate change' message. The actual functionality of the
229 request codes in these messages is not fully understood but these
230 particular codes are never seen in any operation besides a baud
231 rate change. Both of these messages send a single byte of data.
232 In the first message, the value of this byte is always zero.
234 The second message has been determined experimentally to control
235 whether data will be transmitted to a device which is not asserting
236 the 'CTS' signal. If the second message's data byte is zero, data
237 will be transmitted even if 'CTS' is not asserted (i.e. no hardware
238 flow control). if the second message's data byte is nonzero (a
239 value of 1 is used by this driver), data will not be transmitted to
240 a device which is not asserting 'CTS'.
244 rc = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
245 MCT_U232_SET_UNKNOWN1_REQUEST,
246 MCT_U232_SET_REQUEST_TYPE,
247 0, 0, buf, MCT_U232_SET_UNKNOWN1_SIZE,
250 dev_err(&port->dev, "Sending USB device request code %d "
251 "failed (error = %d)\n", MCT_U232_SET_UNKNOWN1_REQUEST,
254 if (port && C_CRTSCTS(tty))
257 dbg("set_baud_rate: send second control message, data = %02X",
259 buf[0] = cts_enable_byte;
260 rc = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
261 MCT_U232_SET_CTS_REQUEST,
262 MCT_U232_SET_REQUEST_TYPE,
263 0, 0, buf, MCT_U232_SET_CTS_SIZE,
266 dev_err(&port->dev, "Sending USB device request code %d "
267 "failed (error = %d)\n", MCT_U232_SET_CTS_REQUEST, rc);
271 } /* mct_u232_set_baud_rate */
273 static int mct_u232_set_line_ctrl(struct usb_serial *serial, unsigned char lcr)
278 buf = kmalloc(MCT_U232_MAX_SIZE, GFP_KERNEL);
283 rc = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
284 MCT_U232_SET_LINE_CTRL_REQUEST,
285 MCT_U232_SET_REQUEST_TYPE,
286 0, 0, buf, MCT_U232_SET_LINE_CTRL_SIZE,
289 dev_err(&serial->dev->dev,
290 "Set LINE CTRL 0x%x failed (error = %d)\n", lcr, rc);
291 dbg("set_line_ctrl: 0x%x", lcr);
294 } /* mct_u232_set_line_ctrl */
296 static int mct_u232_set_modem_ctrl(struct usb_serial *serial,
297 unsigned int control_state)
303 buf = kmalloc(MCT_U232_MAX_SIZE, GFP_KERNEL);
307 mcr = MCT_U232_MCR_NONE;
308 if (control_state & TIOCM_DTR)
309 mcr |= MCT_U232_MCR_DTR;
310 if (control_state & TIOCM_RTS)
311 mcr |= MCT_U232_MCR_RTS;
314 rc = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
315 MCT_U232_SET_MODEM_CTRL_REQUEST,
316 MCT_U232_SET_REQUEST_TYPE,
317 0, 0, buf, MCT_U232_SET_MODEM_CTRL_SIZE,
320 dev_err(&serial->dev->dev,
321 "Set MODEM CTRL 0x%x failed (error = %d)\n", mcr, rc);
322 dbg("set_modem_ctrl: state=0x%x ==> mcr=0x%x", control_state, mcr);
326 } /* mct_u232_set_modem_ctrl */
328 static int mct_u232_get_modem_stat(struct usb_serial *serial,
334 buf = kmalloc(MCT_U232_MAX_SIZE, GFP_KERNEL);
339 rc = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
340 MCT_U232_GET_MODEM_STAT_REQUEST,
341 MCT_U232_GET_REQUEST_TYPE,
342 0, 0, buf, MCT_U232_GET_MODEM_STAT_SIZE,
345 dev_err(&serial->dev->dev,
346 "Get MODEM STATus failed (error = %d)\n", rc);
351 dbg("get_modem_stat: 0x%x", *msr);
354 } /* mct_u232_get_modem_stat */
356 static void mct_u232_msr_to_icount(struct async_icount *icount,
359 /* Translate Control Line states */
360 if (msr & MCT_U232_MSR_DDSR)
362 if (msr & MCT_U232_MSR_DCTS)
364 if (msr & MCT_U232_MSR_DRI)
366 if (msr & MCT_U232_MSR_DCD)
368 } /* mct_u232_msr_to_icount */
370 static void mct_u232_msr_to_state(unsigned int *control_state,
373 /* Translate Control Line states */
374 if (msr & MCT_U232_MSR_DSR)
375 *control_state |= TIOCM_DSR;
377 *control_state &= ~TIOCM_DSR;
378 if (msr & MCT_U232_MSR_CTS)
379 *control_state |= TIOCM_CTS;
381 *control_state &= ~TIOCM_CTS;
382 if (msr & MCT_U232_MSR_RI)
383 *control_state |= TIOCM_RI;
385 *control_state &= ~TIOCM_RI;
386 if (msr & MCT_U232_MSR_CD)
387 *control_state |= TIOCM_CD;
389 *control_state &= ~TIOCM_CD;
390 dbg("msr_to_state: msr=0x%x ==> state=0x%x", msr, *control_state);
391 } /* mct_u232_msr_to_state */
394 * Driver's tty interface functions
397 static int mct_u232_startup(struct usb_serial *serial)
399 struct mct_u232_private *priv;
400 struct usb_serial_port *port, *rport;
402 priv = kzalloc(sizeof(struct mct_u232_private), GFP_KERNEL);
405 spin_lock_init(&priv->lock);
406 init_waitqueue_head(&priv->msr_wait);
407 usb_set_serial_port_data(serial->port[0], priv);
409 init_waitqueue_head(&serial->port[0]->write_wait);
411 /* Puh, that's dirty */
412 port = serial->port[0];
413 rport = serial->port[1];
414 /* No unlinking, it wasn't submitted yet. */
415 usb_free_urb(port->read_urb);
416 port->read_urb = rport->interrupt_in_urb;
417 rport->interrupt_in_urb = NULL;
418 port->read_urb->context = port;
421 } /* mct_u232_startup */
424 static void mct_u232_release(struct usb_serial *serial)
426 struct mct_u232_private *priv;
429 for (i = 0; i < serial->num_ports; ++i) {
430 /* My special items, the standard routines free my urbs */
431 priv = usb_get_serial_port_data(serial->port[i]);
434 } /* mct_u232_release */
436 static int mct_u232_open(struct tty_struct *tty, struct usb_serial_port *port)
438 struct usb_serial *serial = port->serial;
439 struct mct_u232_private *priv = usb_get_serial_port_data(port);
441 unsigned int control_state;
443 unsigned char last_lcr;
444 unsigned char last_msr;
446 /* Compensate for a hardware bug: although the Sitecom U232-P25
447 * device reports a maximum output packet size of 32 bytes,
448 * it seems to be able to accept only 16 bytes (and that's what
449 * SniffUSB says too...)
451 if (le16_to_cpu(serial->dev->descriptor.idProduct)
452 == MCT_U232_SITECOM_PID)
453 port->bulk_out_size = 16;
455 /* Do a defined restart: the normal serial device seems to
456 * always turn on DTR and RTS here, so do the same. I'm not
457 * sure if this is really necessary. But it should not harm
460 spin_lock_irqsave(&priv->lock, flags);
461 if (tty && (tty->termios->c_cflag & CBAUD))
462 priv->control_state = TIOCM_DTR | TIOCM_RTS;
464 priv->control_state = 0;
466 priv->last_lcr = (MCT_U232_DATA_BITS_8 |
467 MCT_U232_PARITY_NONE |
468 MCT_U232_STOP_BITS_1);
469 control_state = priv->control_state;
470 last_lcr = priv->last_lcr;
471 spin_unlock_irqrestore(&priv->lock, flags);
472 mct_u232_set_modem_ctrl(serial, control_state);
473 mct_u232_set_line_ctrl(serial, last_lcr);
475 /* Read modem status and update control state */
476 mct_u232_get_modem_stat(serial, &last_msr);
477 spin_lock_irqsave(&priv->lock, flags);
478 priv->last_msr = last_msr;
479 mct_u232_msr_to_state(&priv->control_state, priv->last_msr);
480 spin_unlock_irqrestore(&priv->lock, flags);
482 retval = usb_submit_urb(port->read_urb, GFP_KERNEL);
485 "usb_submit_urb(read bulk) failed pipe 0x%x err %d\n",
486 port->read_urb->pipe, retval);
490 retval = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
492 usb_kill_urb(port->read_urb);
494 "usb_submit_urb(read int) failed pipe 0x%x err %d",
495 port->interrupt_in_urb->pipe, retval);
502 } /* mct_u232_open */
504 static void mct_u232_dtr_rts(struct usb_serial_port *port, int on)
506 unsigned int control_state;
507 struct mct_u232_private *priv = usb_get_serial_port_data(port);
509 mutex_lock(&port->serial->disc_mutex);
510 if (!port->serial->disconnected) {
511 /* drop DTR and RTS */
512 spin_lock_irq(&priv->lock);
514 priv->control_state |= TIOCM_DTR | TIOCM_RTS;
516 priv->control_state &= ~(TIOCM_DTR | TIOCM_RTS);
517 control_state = priv->control_state;
518 spin_unlock_irq(&priv->lock);
519 mct_u232_set_modem_ctrl(port->serial, control_state);
521 mutex_unlock(&port->serial->disc_mutex);
524 static void mct_u232_close(struct usb_serial_port *port)
526 if (port->serial->dev) {
527 /* shutdown our urbs */
528 usb_kill_urb(port->write_urb);
529 usb_kill_urb(port->read_urb);
530 usb_kill_urb(port->interrupt_in_urb);
532 } /* mct_u232_close */
535 static void mct_u232_read_int_callback(struct urb *urb)
537 struct usb_serial_port *port = urb->context;
538 struct mct_u232_private *priv = usb_get_serial_port_data(port);
539 struct usb_serial *serial = port->serial;
540 struct tty_struct *tty;
541 unsigned char *data = urb->transfer_buffer;
543 int status = urb->status;
553 /* this urb is terminated, clean up */
554 dbg("%s - urb shutting down with status: %d",
558 dbg("%s - nonzero urb status received: %d",
564 dbg("%s - bad serial pointer, exiting", __func__);
568 usb_serial_debug_data(debug, &port->dev, __func__,
569 urb->actual_length, data);
572 * Work-a-round: handle the 'usual' bulk-in pipe here
574 if (urb->transfer_buffer_length > 2) {
575 if (urb->actual_length) {
576 tty = tty_port_tty_get(&port->port);
578 tty_insert_flip_string(tty, data,
580 tty_flip_buffer_push(tty);
588 * The interrupt-in pipe signals exceptional conditions (modem line
589 * signal changes and errors). data[0] holds MSR, data[1] holds LSR.
591 spin_lock_irqsave(&priv->lock, flags);
592 priv->last_msr = data[MCT_U232_MSR_INDEX];
594 /* Record Control Line states */
595 mct_u232_msr_to_state(&priv->control_state, priv->last_msr);
597 mct_u232_msr_to_icount(&priv->icount, priv->last_msr);
600 /* Not yet handled. See belkin_sa.c for further information */
601 /* Now to report any errors */
602 priv->last_lsr = data[MCT_U232_LSR_INDEX];
604 * fill in the flip buffer here, but I do not know the relation
605 * to the current/next receive buffer or characters. I need
606 * to look in to this before committing any code.
608 if (priv->last_lsr & MCT_U232_LSR_ERR) {
609 tty = tty_port_tty_get(&port->port);
611 if (priv->last_lsr & MCT_U232_LSR_OE) {
614 if (priv->last_lsr & MCT_U232_LSR_PE) {
617 if (priv->last_lsr & MCT_U232_LSR_FE) {
619 /* Break Indicator */
620 if (priv->last_lsr & MCT_U232_LSR_BI) {
625 wake_up_interruptible(&priv->msr_wait);
626 spin_unlock_irqrestore(&priv->lock, flags);
628 retval = usb_submit_urb(urb, GFP_ATOMIC);
631 "%s - usb_submit_urb failed with result %d\n",
633 } /* mct_u232_read_int_callback */
635 static void mct_u232_set_termios(struct tty_struct *tty,
636 struct usb_serial_port *port,
637 struct ktermios *old_termios)
639 struct usb_serial *serial = port->serial;
640 struct mct_u232_private *priv = usb_get_serial_port_data(port);
641 struct ktermios *termios = tty->termios;
642 unsigned int cflag = termios->c_cflag;
643 unsigned int old_cflag = old_termios->c_cflag;
645 unsigned int control_state;
646 unsigned char last_lcr;
648 /* get a local copy of the current port settings */
649 spin_lock_irqsave(&priv->lock, flags);
650 control_state = priv->control_state;
651 spin_unlock_irqrestore(&priv->lock, flags);
656 * Do not attempt to cache old rates and skip settings,
657 * disconnects screw such tricks up completely.
658 * Premature optimization is the root of all evil.
661 /* reassert DTR and RTS on transition from B0 */
662 if ((old_cflag & CBAUD) == B0) {
663 dbg("%s: baud was B0", __func__);
664 control_state |= TIOCM_DTR | TIOCM_RTS;
665 mct_u232_set_modem_ctrl(serial, control_state);
668 mct_u232_set_baud_rate(tty, serial, port, tty_get_baud_rate(tty));
670 if ((cflag & CBAUD) == B0) {
671 dbg("%s: baud is B0", __func__);
672 /* Drop RTS and DTR */
673 control_state &= ~(TIOCM_DTR | TIOCM_RTS);
674 mct_u232_set_modem_ctrl(serial, control_state);
678 * Update line control register (LCR)
683 last_lcr |= (cflag & PARODD) ?
684 MCT_U232_PARITY_ODD : MCT_U232_PARITY_EVEN;
686 last_lcr |= MCT_U232_PARITY_NONE;
688 /* set the number of data bits */
689 switch (cflag & CSIZE) {
691 last_lcr |= MCT_U232_DATA_BITS_5; break;
693 last_lcr |= MCT_U232_DATA_BITS_6; break;
695 last_lcr |= MCT_U232_DATA_BITS_7; break;
697 last_lcr |= MCT_U232_DATA_BITS_8; break;
700 "CSIZE was not CS5-CS8, using default of 8\n");
701 last_lcr |= MCT_U232_DATA_BITS_8;
705 termios->c_cflag &= ~CMSPAR;
707 /* set the number of stop bits */
708 last_lcr |= (cflag & CSTOPB) ?
709 MCT_U232_STOP_BITS_2 : MCT_U232_STOP_BITS_1;
711 mct_u232_set_line_ctrl(serial, last_lcr);
713 /* save off the modified port settings */
714 spin_lock_irqsave(&priv->lock, flags);
715 priv->control_state = control_state;
716 priv->last_lcr = last_lcr;
717 spin_unlock_irqrestore(&priv->lock, flags);
718 } /* mct_u232_set_termios */
720 static void mct_u232_break_ctl(struct tty_struct *tty, int break_state)
722 struct usb_serial_port *port = tty->driver_data;
723 struct usb_serial *serial = port->serial;
724 struct mct_u232_private *priv = usb_get_serial_port_data(port);
728 spin_lock_irqsave(&priv->lock, flags);
729 lcr = priv->last_lcr;
732 lcr |= MCT_U232_SET_BREAK;
733 spin_unlock_irqrestore(&priv->lock, flags);
735 mct_u232_set_line_ctrl(serial, lcr);
736 } /* mct_u232_break_ctl */
739 static int mct_u232_tiocmget(struct tty_struct *tty)
741 struct usb_serial_port *port = tty->driver_data;
742 struct mct_u232_private *priv = usb_get_serial_port_data(port);
743 unsigned int control_state;
746 spin_lock_irqsave(&priv->lock, flags);
747 control_state = priv->control_state;
748 spin_unlock_irqrestore(&priv->lock, flags);
750 return control_state;
753 static int mct_u232_tiocmset(struct tty_struct *tty,
754 unsigned int set, unsigned int clear)
756 struct usb_serial_port *port = tty->driver_data;
757 struct usb_serial *serial = port->serial;
758 struct mct_u232_private *priv = usb_get_serial_port_data(port);
759 unsigned int control_state;
762 spin_lock_irqsave(&priv->lock, flags);
763 control_state = priv->control_state;
766 control_state |= TIOCM_RTS;
768 control_state |= TIOCM_DTR;
769 if (clear & TIOCM_RTS)
770 control_state &= ~TIOCM_RTS;
771 if (clear & TIOCM_DTR)
772 control_state &= ~TIOCM_DTR;
774 priv->control_state = control_state;
775 spin_unlock_irqrestore(&priv->lock, flags);
776 return mct_u232_set_modem_ctrl(serial, control_state);
779 static void mct_u232_throttle(struct tty_struct *tty)
781 struct usb_serial_port *port = tty->driver_data;
782 struct mct_u232_private *priv = usb_get_serial_port_data(port);
783 unsigned int control_state;
785 spin_lock_irq(&priv->lock);
786 priv->rx_flags |= THROTTLED;
787 if (C_CRTSCTS(tty)) {
788 priv->control_state &= ~TIOCM_RTS;
789 control_state = priv->control_state;
790 spin_unlock_irq(&priv->lock);
791 (void) mct_u232_set_modem_ctrl(port->serial, control_state);
793 spin_unlock_irq(&priv->lock);
797 static void mct_u232_unthrottle(struct tty_struct *tty)
799 struct usb_serial_port *port = tty->driver_data;
800 struct mct_u232_private *priv = usb_get_serial_port_data(port);
801 unsigned int control_state;
803 spin_lock_irq(&priv->lock);
804 if ((priv->rx_flags & THROTTLED) && C_CRTSCTS(tty)) {
805 priv->rx_flags &= ~THROTTLED;
806 priv->control_state |= TIOCM_RTS;
807 control_state = priv->control_state;
808 spin_unlock_irq(&priv->lock);
809 (void) mct_u232_set_modem_ctrl(port->serial, control_state);
811 spin_unlock_irq(&priv->lock);
815 static int mct_u232_ioctl(struct tty_struct *tty,
816 unsigned int cmd, unsigned long arg)
819 struct usb_serial_port *port = tty->driver_data;
820 struct mct_u232_private *mct_u232_port = usb_get_serial_port_data(port);
821 struct async_icount cnow, cprev;
824 dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
830 dbg("%s (%d) TIOCMIWAIT", __func__, port->number);
832 spin_lock_irqsave(&mct_u232_port->lock, flags);
833 cprev = mct_u232_port->icount;
834 spin_unlock_irqrestore(&mct_u232_port->lock, flags);
836 prepare_to_wait(&mct_u232_port->msr_wait,
837 &wait, TASK_INTERRUPTIBLE);
839 finish_wait(&mct_u232_port->msr_wait, &wait);
840 /* see if a signal did it */
841 if (signal_pending(current))
843 spin_lock_irqsave(&mct_u232_port->lock, flags);
844 cnow = mct_u232_port->icount;
845 spin_unlock_irqrestore(&mct_u232_port->lock, flags);
846 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
847 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
848 return -EIO; /* no change => error */
849 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
850 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
851 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
852 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
862 static int mct_u232_get_icount(struct tty_struct *tty,
863 struct serial_icounter_struct *icount)
865 struct usb_serial_port *port = tty->driver_data;
866 struct mct_u232_private *mct_u232_port = usb_get_serial_port_data(port);
867 struct async_icount *ic = &mct_u232_port->icount;
870 spin_lock_irqsave(&mct_u232_port->lock, flags);
872 icount->cts = ic->cts;
873 icount->dsr = ic->dsr;
874 icount->rng = ic->rng;
875 icount->dcd = ic->dcd;
878 icount->frame = ic->frame;
879 icount->overrun = ic->overrun;
880 icount->parity = ic->parity;
881 icount->brk = ic->brk;
882 icount->buf_overrun = ic->buf_overrun;
884 spin_unlock_irqrestore(&mct_u232_port->lock, flags);
886 dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d",
887 __func__, port->number, icount->rx, icount->tx);
891 module_usb_serial_driver(mct_u232_driver, serial_drivers);
893 MODULE_AUTHOR(DRIVER_AUTHOR);
894 MODULE_DESCRIPTION(DRIVER_DESC);
895 MODULE_LICENSE("GPL");
897 module_param(debug, bool, S_IRUGO | S_IWUSR);
898 MODULE_PARM_DESC(debug, "Debug enabled or not");