blob: f3947712e1375db3ee407a5efa20a1acad3d3c9d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * MCT (Magic Control Technology Corp.) USB RS232 Converter Driver
3 *
4 * Copyright (C) 2000 Wolfgang Grandegger (wolfgang@ces.ch)
5 *
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.
10 *
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.
14 *
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.
17 *
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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 */
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#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>
Alan Coxe19b2562008-07-22 11:14:30 +010033#include <linux/uaccess.h>
Pete Zaitcevaf2ac1a2009-12-07 20:29:05 -070034#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/usb.h>
Greg Kroah-Hartmana9698882006-07-11 21:22:58 -070036#include <linux/usb/serial.h>
Vadim Tsozik7af75af2011-01-09 01:00:11 -050037#include <linux/serial.h>
38#include <linux/ioctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include "mct_u232.h"
40
41/*
42 * Version Information
43 */
Dave Platt45b844d2007-05-08 11:00:12 -070044#define DRIVER_VERSION "z2.1" /* Linux in-kernel version */
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#define DRIVER_AUTHOR "Wolfgang Grandegger <wolfgang@ces.ch>"
46#define DRIVER_DESC "Magic Control Technology USB-RS232 converter driver"
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048/*
49 * Function prototypes
50 */
Alan Coxe19b2562008-07-22 11:14:30 +010051static int mct_u232_startup(struct usb_serial *serial);
Alan Sternf9c99bb2009-06-02 11:53:55 -040052static void mct_u232_release(struct usb_serial *serial);
Alan Coxa509a7e2009-09-19 13:13:26 -070053static int mct_u232_open(struct tty_struct *tty, struct usb_serial_port *port);
Alan Cox335f8512009-06-11 12:26:29 +010054static void mct_u232_close(struct usb_serial_port *port);
55static void mct_u232_dtr_rts(struct usb_serial_port *port, int on);
Alan Coxe19b2562008-07-22 11:14:30 +010056static void mct_u232_read_int_callback(struct urb *urb);
57static void mct_u232_set_termios(struct tty_struct *tty,
58 struct usb_serial_port *port, struct ktermios *old);
59static void mct_u232_break_ctl(struct tty_struct *tty, int break_state);
Alan Cox60b33c12011-02-14 16:26:14 +000060static int mct_u232_tiocmget(struct tty_struct *tty);
Alan Cox20b9d172011-02-14 16:26:50 +000061static int mct_u232_tiocmset(struct tty_struct *tty,
Alan Coxe19b2562008-07-22 11:14:30 +010062 unsigned int set, unsigned int clear);
Randy Dunlap4acfaf82011-04-03 11:42:00 -070063static int mct_u232_ioctl(struct tty_struct *tty,
Vadim Tsozik7af75af2011-01-09 01:00:11 -050064 unsigned int cmd, unsigned long arg);
65static int mct_u232_get_icount(struct tty_struct *tty,
66 struct serial_icounter_struct *icount);
Alan Coxe19b2562008-07-22 11:14:30 +010067static void mct_u232_throttle(struct tty_struct *tty);
68static void mct_u232_unthrottle(struct tty_struct *tty);
Dave Platt45b844d2007-05-08 11:00:12 -070069
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071/*
72 * All of the device info needed for the MCT USB-RS232 converter.
73 */
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -070074static const struct usb_device_id id_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 { USB_DEVICE(MCT_U232_VID, MCT_U232_PID) },
76 { USB_DEVICE(MCT_U232_VID, MCT_U232_SITECOM_PID) },
77 { USB_DEVICE(MCT_U232_VID, MCT_U232_DU_H3SP_PID) },
78 { USB_DEVICE(MCT_U232_BELKIN_F5U109_VID, MCT_U232_BELKIN_F5U109_PID) },
79 { } /* Terminating entry */
80};
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -070081MODULE_DEVICE_TABLE(usb, id_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -070083static struct usb_serial_driver mct_u232_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -070084 .driver = {
85 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -070086 .name = "mct_u232",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -070087 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -070088 .description = "MCT U232",
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -070089 .id_table = id_table,
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 .num_ports = 1,
91 .open = mct_u232_open,
92 .close = mct_u232_close,
Alan Cox335f8512009-06-11 12:26:29 +010093 .dtr_rts = mct_u232_dtr_rts,
Dave Platt45b844d2007-05-08 11:00:12 -070094 .throttle = mct_u232_throttle,
95 .unthrottle = mct_u232_unthrottle,
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 .read_int_callback = mct_u232_read_int_callback,
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 .set_termios = mct_u232_set_termios,
98 .break_ctl = mct_u232_break_ctl,
99 .tiocmget = mct_u232_tiocmget,
100 .tiocmset = mct_u232_tiocmset,
101 .attach = mct_u232_startup,
Alan Sternf9c99bb2009-06-02 11:53:55 -0400102 .release = mct_u232_release,
Vadim Tsozik7af75af2011-01-09 01:00:11 -0500103 .ioctl = mct_u232_ioctl,
104 .get_icount = mct_u232_get_icount,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105};
106
Alan Stern4d2a7af2012-02-23 14:57:09 -0500107static struct usb_serial_driver * const serial_drivers[] = {
108 &mct_u232_device, NULL
109};
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111struct mct_u232_private {
112 spinlock_t lock;
113 unsigned int control_state; /* Modem Line Setting (TIOCM) */
114 unsigned char last_lcr; /* Line Control Register */
115 unsigned char last_lsr; /* Line Status Register */
116 unsigned char last_msr; /* Modem Status Register */
Dave Platt45b844d2007-05-08 11:00:12 -0700117 unsigned int rx_flags; /* Throttling flags */
Vadim Tsozik7af75af2011-01-09 01:00:11 -0500118 struct async_icount icount;
119 wait_queue_head_t msr_wait; /* for handling sleeping while waiting
120 for msr change to happen */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121};
122
Dave Platt45b844d2007-05-08 11:00:12 -0700123#define THROTTLED 0x01
124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125/*
126 * Handle vendor specific USB requests
127 */
128
129#define WDR_TIMEOUT 5000 /* default urb timeout */
130
131/*
132 * Later day 2.6.0-test kernels have new baud rates like B230400 which
133 * we do not know how to support. We ignore them for the moment.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 */
Alan Coxe19b2562008-07-22 11:14:30 +0100135static int mct_u232_calculate_baud_rate(struct usb_serial *serial,
136 speed_t value, speed_t *result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
Alan Coxd0fab0d2007-12-13 16:15:29 -0800138 *result = value;
139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 if (le16_to_cpu(serial->dev->descriptor.idProduct) == MCT_U232_SITECOM_PID
Alan Coxe19b2562008-07-22 11:14:30 +0100141 || le16_to_cpu(serial->dev->descriptor.idProduct) == MCT_U232_BELKIN_F5U109_PID) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 switch (value) {
Alan Coxe19b2562008-07-22 11:14:30 +0100143 case 300:
144 return 0x01;
145 case 600:
146 return 0x02; /* this one not tested */
147 case 1200:
148 return 0x03;
149 case 2400:
150 return 0x04;
151 case 4800:
152 return 0x06;
153 case 9600:
154 return 0x08;
155 case 19200:
156 return 0x09;
157 case 38400:
158 return 0x0a;
159 case 57600:
160 return 0x0b;
161 case 115200:
162 return 0x0c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 default:
Alan Coxd0fab0d2007-12-13 16:15:29 -0800164 *result = 9600;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 return 0x08;
166 }
167 } else {
Alan Coxd0fab0d2007-12-13 16:15:29 -0800168 /* FIXME: Can we use any divider - should we do
169 divider = 115200/value;
170 real baud = 115200/divider */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 switch (value) {
Andrew Mortonb3aceb22007-08-10 14:53:35 -0700172 case 300: break;
173 case 600: break;
174 case 1200: break;
175 case 2400: break;
176 case 4800: break;
177 case 9600: break;
178 case 19200: break;
179 case 38400: break;
180 case 57600: break;
181 case 115200: break;
182 default:
Andrew Mortonb3aceb22007-08-10 14:53:35 -0700183 value = 9600;
Alan Coxd0fab0d2007-12-13 16:15:29 -0800184 *result = 9600;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 }
186 return 115200/value;
187 }
188}
189
Alan Cox95da3102008-07-22 11:09:07 +0100190static int mct_u232_set_baud_rate(struct tty_struct *tty,
191 struct usb_serial *serial, struct usb_serial_port *port, speed_t value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
Pete Zaitcevaf2ac1a2009-12-07 20:29:05 -0700193 unsigned int divisor;
Alan Coxe19b2562008-07-22 11:14:30 +0100194 int rc;
Pete Zaitcevaf2ac1a2009-12-07 20:29:05 -0700195 unsigned char *buf;
Alan Coxe19b2562008-07-22 11:14:30 +0100196 unsigned char cts_enable_byte = 0;
197 speed_t speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Pete Zaitcevaf2ac1a2009-12-07 20:29:05 -0700199 buf = kmalloc(MCT_U232_MAX_SIZE, GFP_KERNEL);
200 if (buf == NULL)
201 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Pete Zaitcevaf2ac1a2009-12-07 20:29:05 -0700203 divisor = mct_u232_calculate_baud_rate(serial, value, &speed);
204 put_unaligned_le32(cpu_to_le32(divisor), buf);
Alan Coxe19b2562008-07-22 11:14:30 +0100205 rc = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
206 MCT_U232_SET_BAUD_RATE_REQUEST,
207 MCT_U232_SET_REQUEST_TYPE,
Pete Zaitcevaf2ac1a2009-12-07 20:29:05 -0700208 0, 0, buf, MCT_U232_SET_BAUD_RATE_SIZE,
Alan Coxe19b2562008-07-22 11:14:30 +0100209 WDR_TIMEOUT);
Alan Coxd0fab0d2007-12-13 16:15:29 -0800210 if (rc < 0) /*FIXME: What value speed results */
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700211 dev_err(&port->dev, "Set BAUD RATE %d failed (error = %d)\n",
212 value, rc);
Alan Coxd0fab0d2007-12-13 16:15:29 -0800213 else
Alan Cox95da3102008-07-22 11:09:07 +0100214 tty_encode_baud_rate(tty, speed, speed);
Greg Kroah-Hartman4a770cc2012-09-14 15:08:30 -0700215 dev_dbg(&port->dev, "set_baud_rate: value: 0x%x, divisor: 0x%x\n", value, divisor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
217 /* Mimic the MCT-supplied Windows driver (version 1.21P.0104), which
218 always sends two extra USB 'device request' messages after the
219 'baud rate change' message. The actual functionality of the
220 request codes in these messages is not fully understood but these
221 particular codes are never seen in any operation besides a baud
Dave Platt45b844d2007-05-08 11:00:12 -0700222 rate change. Both of these messages send a single byte of data.
223 In the first message, the value of this byte is always zero.
224
225 The second message has been determined experimentally to control
226 whether data will be transmitted to a device which is not asserting
227 the 'CTS' signal. If the second message's data byte is zero, data
228 will be transmitted even if 'CTS' is not asserted (i.e. no hardware
Alan Coxe19b2562008-07-22 11:14:30 +0100229 flow control). if the second message's data byte is nonzero (a
230 value of 1 is used by this driver), data will not be transmitted to
231 a device which is not asserting 'CTS'.
Dave Platt45b844d2007-05-08 11:00:12 -0700232 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Pete Zaitcevaf2ac1a2009-12-07 20:29:05 -0700234 buf[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 rc = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
Alan Coxe19b2562008-07-22 11:14:30 +0100236 MCT_U232_SET_UNKNOWN1_REQUEST,
237 MCT_U232_SET_REQUEST_TYPE,
Pete Zaitcevaf2ac1a2009-12-07 20:29:05 -0700238 0, 0, buf, MCT_U232_SET_UNKNOWN1_SIZE,
Alan Coxe19b2562008-07-22 11:14:30 +0100239 WDR_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 if (rc < 0)
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700241 dev_err(&port->dev, "Sending USB device request code %d "
242 "failed (error = %d)\n", MCT_U232_SET_UNKNOWN1_REQUEST,
243 rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Alan Coxe19b2562008-07-22 11:14:30 +0100245 if (port && C_CRTSCTS(tty))
Dave Platt45b844d2007-05-08 11:00:12 -0700246 cts_enable_byte = 1;
Dave Platt45b844d2007-05-08 11:00:12 -0700247
Greg Kroah-Hartman4a770cc2012-09-14 15:08:30 -0700248 dev_dbg(&port->dev, "set_baud_rate: send second control message, data = %02X\n",
249 cts_enable_byte);
Pete Zaitcevaf2ac1a2009-12-07 20:29:05 -0700250 buf[0] = cts_enable_byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 rc = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
Alan Coxe19b2562008-07-22 11:14:30 +0100252 MCT_U232_SET_CTS_REQUEST,
253 MCT_U232_SET_REQUEST_TYPE,
Pete Zaitcevaf2ac1a2009-12-07 20:29:05 -0700254 0, 0, buf, MCT_U232_SET_CTS_SIZE,
Alan Coxe19b2562008-07-22 11:14:30 +0100255 WDR_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 if (rc < 0)
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700257 dev_err(&port->dev, "Sending USB device request code %d "
258 "failed (error = %d)\n", MCT_U232_SET_CTS_REQUEST, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Pete Zaitcevaf2ac1a2009-12-07 20:29:05 -0700260 kfree(buf);
Alan Coxe19b2562008-07-22 11:14:30 +0100261 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262} /* mct_u232_set_baud_rate */
263
Greg Kroah-Hartman4a770cc2012-09-14 15:08:30 -0700264static int mct_u232_set_line_ctrl(struct usb_serial_port *port,
265 unsigned char lcr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
Alan Coxe19b2562008-07-22 11:14:30 +0100267 int rc;
Pete Zaitcevaf2ac1a2009-12-07 20:29:05 -0700268 unsigned char *buf;
269
270 buf = kmalloc(MCT_U232_MAX_SIZE, GFP_KERNEL);
271 if (buf == NULL)
272 return -ENOMEM;
273
274 buf[0] = lcr;
Greg Kroah-Hartman4a770cc2012-09-14 15:08:30 -0700275 rc = usb_control_msg(port->serial->dev, usb_sndctrlpipe(port->serial->dev, 0),
Alan Coxe19b2562008-07-22 11:14:30 +0100276 MCT_U232_SET_LINE_CTRL_REQUEST,
277 MCT_U232_SET_REQUEST_TYPE,
Pete Zaitcevaf2ac1a2009-12-07 20:29:05 -0700278 0, 0, buf, MCT_U232_SET_LINE_CTRL_SIZE,
Alan Coxe19b2562008-07-22 11:14:30 +0100279 WDR_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 if (rc < 0)
Greg Kroah-Hartman4a770cc2012-09-14 15:08:30 -0700281 dev_err(&port->dev, "Set LINE CTRL 0x%x failed (error = %d)\n", lcr, rc);
282 dev_dbg(&port->dev, "set_line_ctrl: 0x%x\n", lcr);
Pete Zaitcevaf2ac1a2009-12-07 20:29:05 -0700283 kfree(buf);
Alan Coxe19b2562008-07-22 11:14:30 +0100284 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285} /* mct_u232_set_line_ctrl */
286
Greg Kroah-Hartman4a770cc2012-09-14 15:08:30 -0700287static int mct_u232_set_modem_ctrl(struct usb_serial_port *port,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 unsigned int control_state)
289{
Alan Coxe19b2562008-07-22 11:14:30 +0100290 int rc;
Pete Zaitcevaf2ac1a2009-12-07 20:29:05 -0700291 unsigned char mcr;
292 unsigned char *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Pete Zaitcevaf2ac1a2009-12-07 20:29:05 -0700294 buf = kmalloc(MCT_U232_MAX_SIZE, GFP_KERNEL);
295 if (buf == NULL)
296 return -ENOMEM;
297
298 mcr = MCT_U232_MCR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 if (control_state & TIOCM_DTR)
300 mcr |= MCT_U232_MCR_DTR;
301 if (control_state & TIOCM_RTS)
302 mcr |= MCT_U232_MCR_RTS;
303
Pete Zaitcevaf2ac1a2009-12-07 20:29:05 -0700304 buf[0] = mcr;
Greg Kroah-Hartman4a770cc2012-09-14 15:08:30 -0700305 rc = usb_control_msg(port->serial->dev, usb_sndctrlpipe(port->serial->dev, 0),
Alan Coxe19b2562008-07-22 11:14:30 +0100306 MCT_U232_SET_MODEM_CTRL_REQUEST,
307 MCT_U232_SET_REQUEST_TYPE,
Pete Zaitcevaf2ac1a2009-12-07 20:29:05 -0700308 0, 0, buf, MCT_U232_SET_MODEM_CTRL_SIZE,
Alan Coxe19b2562008-07-22 11:14:30 +0100309 WDR_TIMEOUT);
Alan Cox1aa3c632012-05-22 20:45:13 +0100310 kfree(buf);
311
Greg Kroah-Hartman4a770cc2012-09-14 15:08:30 -0700312 dev_dbg(&port->dev, "set_modem_ctrl: state=0x%x ==> mcr=0x%x\n", control_state, mcr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Alan Cox1aa3c632012-05-22 20:45:13 +0100314 if (rc < 0) {
Greg Kroah-Hartman4a770cc2012-09-14 15:08:30 -0700315 dev_err(&port->dev, "Set MODEM CTRL 0x%x failed (error = %d)\n", mcr, rc);
Alan Cox1aa3c632012-05-22 20:45:13 +0100316 return rc;
317 }
318 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319} /* mct_u232_set_modem_ctrl */
320
Greg Kroah-Hartman4a770cc2012-09-14 15:08:30 -0700321static int mct_u232_get_modem_stat(struct usb_serial_port *port,
322 unsigned char *msr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
Alan Coxe19b2562008-07-22 11:14:30 +0100324 int rc;
Pete Zaitcevaf2ac1a2009-12-07 20:29:05 -0700325 unsigned char *buf;
326
327 buf = kmalloc(MCT_U232_MAX_SIZE, GFP_KERNEL);
328 if (buf == NULL) {
329 *msr = 0;
330 return -ENOMEM;
331 }
Greg Kroah-Hartman4a770cc2012-09-14 15:08:30 -0700332 rc = usb_control_msg(port->serial->dev, usb_rcvctrlpipe(port->serial->dev, 0),
Alan Coxe19b2562008-07-22 11:14:30 +0100333 MCT_U232_GET_MODEM_STAT_REQUEST,
334 MCT_U232_GET_REQUEST_TYPE,
Pete Zaitcevaf2ac1a2009-12-07 20:29:05 -0700335 0, 0, buf, MCT_U232_GET_MODEM_STAT_SIZE,
Alan Coxe19b2562008-07-22 11:14:30 +0100336 WDR_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 if (rc < 0) {
Greg Kroah-Hartman4a770cc2012-09-14 15:08:30 -0700338 dev_err(&port->dev, "Get MODEM STATus failed (error = %d)\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 *msr = 0;
Pete Zaitcevaf2ac1a2009-12-07 20:29:05 -0700340 } else {
341 *msr = buf[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 }
Greg Kroah-Hartman4a770cc2012-09-14 15:08:30 -0700343 dev_dbg(&port->dev, "get_modem_stat: 0x%x\n", *msr);
Pete Zaitcevaf2ac1a2009-12-07 20:29:05 -0700344 kfree(buf);
Alan Coxe19b2562008-07-22 11:14:30 +0100345 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346} /* mct_u232_get_modem_stat */
347
Vadim Tsozik7af75af2011-01-09 01:00:11 -0500348static void mct_u232_msr_to_icount(struct async_icount *icount,
349 unsigned char msr)
350{
351 /* Translate Control Line states */
352 if (msr & MCT_U232_MSR_DDSR)
353 icount->dsr++;
354 if (msr & MCT_U232_MSR_DCTS)
355 icount->cts++;
356 if (msr & MCT_U232_MSR_DRI)
357 icount->rng++;
358 if (msr & MCT_U232_MSR_DCD)
359 icount->dcd++;
360} /* mct_u232_msr_to_icount */
361
Greg Kroah-Hartman4a770cc2012-09-14 15:08:30 -0700362static void mct_u232_msr_to_state(struct usb_serial_port *port,
363 unsigned int *control_state, unsigned char msr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364{
Alan Coxe19b2562008-07-22 11:14:30 +0100365 /* Translate Control Line states */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 if (msr & MCT_U232_MSR_DSR)
367 *control_state |= TIOCM_DSR;
368 else
369 *control_state &= ~TIOCM_DSR;
370 if (msr & MCT_U232_MSR_CTS)
371 *control_state |= TIOCM_CTS;
372 else
373 *control_state &= ~TIOCM_CTS;
374 if (msr & MCT_U232_MSR_RI)
375 *control_state |= TIOCM_RI;
376 else
377 *control_state &= ~TIOCM_RI;
378 if (msr & MCT_U232_MSR_CD)
379 *control_state |= TIOCM_CD;
380 else
381 *control_state &= ~TIOCM_CD;
Greg Kroah-Hartman4a770cc2012-09-14 15:08:30 -0700382 dev_dbg(&port->dev, "msr_to_state: msr=0x%x ==> state=0x%x\n", msr, *control_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383} /* mct_u232_msr_to_state */
384
385/*
386 * Driver's tty interface functions
387 */
388
Alan Coxe19b2562008-07-22 11:14:30 +0100389static int mct_u232_startup(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390{
391 struct mct_u232_private *priv;
392 struct usb_serial_port *port, *rport;
393
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +0100394 priv = kzalloc(sizeof(struct mct_u232_private), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 if (!priv)
396 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 spin_lock_init(&priv->lock);
Vadim Tsozik7af75af2011-01-09 01:00:11 -0500398 init_waitqueue_head(&priv->msr_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 usb_set_serial_port_data(serial->port[0], priv);
400
401 init_waitqueue_head(&serial->port[0]->write_wait);
402
403 /* Puh, that's dirty */
404 port = serial->port[0];
405 rport = serial->port[1];
Mariusz Kozlowski73135bb2006-11-08 15:36:42 +0100406 /* No unlinking, it wasn't submitted yet. */
407 usb_free_urb(port->read_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 port->read_urb = rport->interrupt_in_urb;
409 rport->interrupt_in_urb = NULL;
410 port->read_urb->context = port;
411
Alan Coxe19b2562008-07-22 11:14:30 +0100412 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413} /* mct_u232_startup */
414
415
Alan Sternf9c99bb2009-06-02 11:53:55 -0400416static void mct_u232_release(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417{
418 struct mct_u232_private *priv;
419 int i;
Alan Coxe19b2562008-07-22 11:14:30 +0100420
Alan Coxe19b2562008-07-22 11:14:30 +0100421 for (i = 0; i < serial->num_ports; ++i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 /* My special items, the standard routines free my urbs */
423 priv = usb_get_serial_port_data(serial->port[i]);
Alan Sternf9c99bb2009-06-02 11:53:55 -0400424 kfree(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 }
Alan Sternf9c99bb2009-06-02 11:53:55 -0400426} /* mct_u232_release */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Alan Coxa509a7e2009-09-19 13:13:26 -0700428static int mct_u232_open(struct tty_struct *tty, struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
430 struct usb_serial *serial = port->serial;
431 struct mct_u232_private *priv = usb_get_serial_port_data(port);
432 int retval = 0;
433 unsigned int control_state;
434 unsigned long flags;
435 unsigned char last_lcr;
436 unsigned char last_msr;
437
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 /* Compensate for a hardware bug: although the Sitecom U232-P25
439 * device reports a maximum output packet size of 32 bytes,
440 * it seems to be able to accept only 16 bytes (and that's what
441 * SniffUSB says too...)
442 */
Alan Coxe19b2562008-07-22 11:14:30 +0100443 if (le16_to_cpu(serial->dev->descriptor.idProduct)
444 == MCT_U232_SITECOM_PID)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 port->bulk_out_size = 16;
446
Alan Coxe19b2562008-07-22 11:14:30 +0100447 /* Do a defined restart: the normal serial device seems to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 * always turn on DTR and RTS here, so do the same. I'm not
449 * sure if this is really necessary. But it should not harm
450 * either.
451 */
452 spin_lock_irqsave(&priv->lock, flags);
Alan Coxadc8d742012-07-14 15:31:47 +0100453 if (tty && (tty->termios.c_cflag & CBAUD))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 priv->control_state = TIOCM_DTR | TIOCM_RTS;
455 else
456 priv->control_state = 0;
Alan Coxe19b2562008-07-22 11:14:30 +0100457
458 priv->last_lcr = (MCT_U232_DATA_BITS_8 |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 MCT_U232_PARITY_NONE |
460 MCT_U232_STOP_BITS_1);
461 control_state = priv->control_state;
462 last_lcr = priv->last_lcr;
463 spin_unlock_irqrestore(&priv->lock, flags);
Greg Kroah-Hartman4a770cc2012-09-14 15:08:30 -0700464 mct_u232_set_modem_ctrl(port, control_state);
465 mct_u232_set_line_ctrl(port, last_lcr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
467 /* Read modem status and update control state */
Greg Kroah-Hartman4a770cc2012-09-14 15:08:30 -0700468 mct_u232_get_modem_stat(port, &last_msr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 spin_lock_irqsave(&priv->lock, flags);
470 priv->last_msr = last_msr;
Greg Kroah-Hartman4a770cc2012-09-14 15:08:30 -0700471 mct_u232_msr_to_state(port, &priv->control_state, priv->last_msr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 spin_unlock_irqrestore(&priv->lock, flags);
473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 retval = usb_submit_urb(port->read_urb, GFP_KERNEL);
475 if (retval) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700476 dev_err(&port->dev,
477 "usb_submit_urb(read bulk) failed pipe 0x%x err %d\n",
478 port->read_urb->pipe, retval);
Oliver Neukum2f007de2007-03-29 10:45:17 +0200479 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 }
481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 retval = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
Oliver Neukum2f007de2007-03-29 10:45:17 +0200483 if (retval) {
484 usb_kill_urb(port->read_urb);
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700485 dev_err(&port->dev,
486 "usb_submit_urb(read int) failed pipe 0x%x err %d",
487 port->interrupt_in_urb->pipe, retval);
Oliver Neukum2f007de2007-03-29 10:45:17 +0200488 goto error;
489 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 return 0;
Oliver Neukum2f007de2007-03-29 10:45:17 +0200491
492error:
493 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494} /* mct_u232_open */
495
Alan Cox335f8512009-06-11 12:26:29 +0100496static void mct_u232_dtr_rts(struct usb_serial_port *port, int on)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
Dave Platt45b844d2007-05-08 11:00:12 -0700498 unsigned int control_state;
499 struct mct_u232_private *priv = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
Alan Cox335f8512009-06-11 12:26:29 +0100501 mutex_lock(&port->serial->disc_mutex);
502 if (!port->serial->disconnected) {
503 /* drop DTR and RTS */
504 spin_lock_irq(&priv->lock);
505 if (on)
506 priv->control_state |= TIOCM_DTR | TIOCM_RTS;
507 else
Oliver Neukume33fe4d2008-01-21 17:44:10 +0100508 priv->control_state &= ~(TIOCM_DTR | TIOCM_RTS);
Alan Cox335f8512009-06-11 12:26:29 +0100509 control_state = priv->control_state;
510 spin_unlock_irq(&priv->lock);
Greg Kroah-Hartman4a770cc2012-09-14 15:08:30 -0700511 mct_u232_set_modem_ctrl(port, control_state);
Dave Platt45b844d2007-05-08 11:00:12 -0700512 }
Alan Cox335f8512009-06-11 12:26:29 +0100513 mutex_unlock(&port->serial->disc_mutex);
514}
Dave Platt45b844d2007-05-08 11:00:12 -0700515
Alan Cox335f8512009-06-11 12:26:29 +0100516static void mct_u232_close(struct usb_serial_port *port)
517{
Johan Hovold92ca0dc2010-10-21 10:49:10 +0200518 if (port->serial->dev) {
519 /* shutdown our urbs */
520 usb_kill_urb(port->write_urb);
521 usb_kill_urb(port->read_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 usb_kill_urb(port->interrupt_in_urb);
Johan Hovold92ca0dc2010-10-21 10:49:10 +0200523 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524} /* mct_u232_close */
525
526
Alan Coxe19b2562008-07-22 11:14:30 +0100527static void mct_u232_read_int_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528{
Ming Leicdc97792008-02-24 18:41:47 +0800529 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 struct mct_u232_private *priv = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 struct tty_struct *tty;
532 unsigned char *data = urb->transfer_buffer;
Greg Kroah-Hartmane96da392007-06-15 15:44:13 -0700533 int retval;
534 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 unsigned long flags;
536
Greg Kroah-Hartmane96da392007-06-15 15:44:13 -0700537 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 case 0:
539 /* success */
540 break;
541 case -ECONNRESET:
542 case -ENOENT:
543 case -ESHUTDOWN:
544 /* this urb is terminated, clean up */
Greg Kroah-Hartman4a770cc2012-09-14 15:08:30 -0700545 dev_dbg(&port->dev, "%s - urb shutting down with status: %d\n",
546 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 return;
548 default:
Greg Kroah-Hartman4a770cc2012-09-14 15:08:30 -0700549 dev_dbg(&port->dev, "%s - nonzero urb status received: %d\n",
550 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 goto exit;
552 }
553
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +0100554 usb_serial_debug_data(&port->dev, __func__, urb->actual_length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
556 /*
557 * Work-a-round: handle the 'usual' bulk-in pipe here
558 */
559 if (urb->transfer_buffer_length > 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 if (urb->actual_length) {
Jiri Slaby054f2342009-09-06 23:10:10 +0200561 tty = tty_port_tty_get(&port->port);
562 if (tty) {
563 tty_insert_flip_string(tty, data,
564 urb->actual_length);
565 tty_flip_buffer_push(tty);
566 }
Alan Cox4a90f092008-10-13 10:39:46 +0100567 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 }
569 goto exit;
570 }
Alan Coxe19b2562008-07-22 11:14:30 +0100571
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 /*
573 * The interrupt-in pipe signals exceptional conditions (modem line
574 * signal changes and errors). data[0] holds MSR, data[1] holds LSR.
575 */
576 spin_lock_irqsave(&priv->lock, flags);
577 priv->last_msr = data[MCT_U232_MSR_INDEX];
Alan Coxe19b2562008-07-22 11:14:30 +0100578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 /* Record Control Line states */
Greg Kroah-Hartman4a770cc2012-09-14 15:08:30 -0700580 mct_u232_msr_to_state(port, &priv->control_state, priv->last_msr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Vadim Tsozik7af75af2011-01-09 01:00:11 -0500582 mct_u232_msr_to_icount(&priv->icount, priv->last_msr);
583
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584#if 0
Alan Coxe19b2562008-07-22 11:14:30 +0100585 /* Not yet handled. See belkin_sa.c for further information */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 /* Now to report any errors */
587 priv->last_lsr = data[MCT_U232_LSR_INDEX];
588 /*
589 * fill in the flip buffer here, but I do not know the relation
590 * to the current/next receive buffer or characters. I need
591 * to look in to this before committing any code.
592 */
593 if (priv->last_lsr & MCT_U232_LSR_ERR) {
Alan Cox4a90f092008-10-13 10:39:46 +0100594 tty = tty_port_tty_get(&port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 /* Overrun Error */
596 if (priv->last_lsr & MCT_U232_LSR_OE) {
597 }
598 /* Parity Error */
599 if (priv->last_lsr & MCT_U232_LSR_PE) {
600 }
601 /* Framing Error */
602 if (priv->last_lsr & MCT_U232_LSR_FE) {
603 }
604 /* Break Indicator */
605 if (priv->last_lsr & MCT_U232_LSR_BI) {
606 }
Alan Cox4a90f092008-10-13 10:39:46 +0100607 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 }
609#endif
Vadim Tsozik7af75af2011-01-09 01:00:11 -0500610 wake_up_interruptible(&priv->msr_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 spin_unlock_irqrestore(&priv->lock, flags);
612exit:
Alan Coxe19b2562008-07-22 11:14:30 +0100613 retval = usb_submit_urb(urb, GFP_ATOMIC);
Greg Kroah-Hartmane96da392007-06-15 15:44:13 -0700614 if (retval)
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700615 dev_err(&port->dev,
616 "%s - usb_submit_urb failed with result %d\n",
617 __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618} /* mct_u232_read_int_callback */
619
Alan Coxe19b2562008-07-22 11:14:30 +0100620static void mct_u232_set_termios(struct tty_struct *tty,
621 struct usb_serial_port *port,
622 struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
624 struct usb_serial *serial = port->serial;
625 struct mct_u232_private *priv = usb_get_serial_port_data(port);
Alan Coxadc8d742012-07-14 15:31:47 +0100626 struct ktermios *termios = &tty->termios;
Alan Coxd0fab0d2007-12-13 16:15:29 -0800627 unsigned int cflag = termios->c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 unsigned int old_cflag = old_termios->c_cflag;
629 unsigned long flags;
Dave Platt45b844d2007-05-08 11:00:12 -0700630 unsigned int control_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 unsigned char last_lcr;
632
633 /* get a local copy of the current port settings */
634 spin_lock_irqsave(&priv->lock, flags);
635 control_state = priv->control_state;
636 spin_unlock_irqrestore(&priv->lock, flags);
637 last_lcr = 0;
638
639 /*
640 * Update baud rate.
641 * Do not attempt to cache old rates and skip settings,
642 * disconnects screw such tricks up completely.
643 * Premature optimization is the root of all evil.
644 */
645
Alan Coxe19b2562008-07-22 11:14:30 +0100646 /* reassert DTR and RTS on transition from B0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 if ((old_cflag & CBAUD) == B0) {
Greg Kroah-Hartman4a770cc2012-09-14 15:08:30 -0700648 dev_dbg(&port->dev, "%s: baud was B0\n", __func__);
Dave Platt45b844d2007-05-08 11:00:12 -0700649 control_state |= TIOCM_DTR | TIOCM_RTS;
Greg Kroah-Hartman4a770cc2012-09-14 15:08:30 -0700650 mct_u232_set_modem_ctrl(port, control_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 }
652
Alan Cox95da3102008-07-22 11:09:07 +0100653 mct_u232_set_baud_rate(tty, serial, port, tty_get_baud_rate(tty));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Alan Coxe19b2562008-07-22 11:14:30 +0100655 if ((cflag & CBAUD) == B0) {
Greg Kroah-Hartman4a770cc2012-09-14 15:08:30 -0700656 dev_dbg(&port->dev, "%s: baud is B0\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 /* Drop RTS and DTR */
658 control_state &= ~(TIOCM_DTR | TIOCM_RTS);
Greg Kroah-Hartman4a770cc2012-09-14 15:08:30 -0700659 mct_u232_set_modem_ctrl(port, control_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 }
661
662 /*
663 * Update line control register (LCR)
664 */
665
666 /* set the parity */
667 if (cflag & PARENB)
668 last_lcr |= (cflag & PARODD) ?
669 MCT_U232_PARITY_ODD : MCT_U232_PARITY_EVEN;
670 else
671 last_lcr |= MCT_U232_PARITY_NONE;
672
673 /* set the number of data bits */
674 switch (cflag & CSIZE) {
675 case CS5:
676 last_lcr |= MCT_U232_DATA_BITS_5; break;
677 case CS6:
678 last_lcr |= MCT_U232_DATA_BITS_6; break;
679 case CS7:
680 last_lcr |= MCT_U232_DATA_BITS_7; break;
681 case CS8:
682 last_lcr |= MCT_U232_DATA_BITS_8; break;
683 default:
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700684 dev_err(&port->dev,
685 "CSIZE was not CS5-CS8, using default of 8\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 last_lcr |= MCT_U232_DATA_BITS_8;
687 break;
688 }
689
Alan Coxd0fab0d2007-12-13 16:15:29 -0800690 termios->c_cflag &= ~CMSPAR;
691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 /* set the number of stop bits */
693 last_lcr |= (cflag & CSTOPB) ?
694 MCT_U232_STOP_BITS_2 : MCT_U232_STOP_BITS_1;
695
Greg Kroah-Hartman4a770cc2012-09-14 15:08:30 -0700696 mct_u232_set_line_ctrl(port, last_lcr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 /* save off the modified port settings */
699 spin_lock_irqsave(&priv->lock, flags);
700 priv->control_state = control_state;
701 priv->last_lcr = last_lcr;
702 spin_unlock_irqrestore(&priv->lock, flags);
703} /* mct_u232_set_termios */
704
Alan Cox95da3102008-07-22 11:09:07 +0100705static void mct_u232_break_ctl(struct tty_struct *tty, int break_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706{
Alan Cox95da3102008-07-22 11:09:07 +0100707 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 struct mct_u232_private *priv = usb_get_serial_port_data(port);
709 unsigned char lcr;
710 unsigned long flags;
711
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 spin_lock_irqsave(&priv->lock, flags);
713 lcr = priv->last_lcr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
715 if (break_state)
716 lcr |= MCT_U232_SET_BREAK;
Alan Cox6b447f042009-01-02 13:48:56 +0000717 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
Greg Kroah-Hartman4a770cc2012-09-14 15:08:30 -0700719 mct_u232_set_line_ctrl(port, lcr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720} /* mct_u232_break_ctl */
721
722
Alan Cox60b33c12011-02-14 16:26:14 +0000723static int mct_u232_tiocmget(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724{
Alan Cox95da3102008-07-22 11:09:07 +0100725 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 struct mct_u232_private *priv = usb_get_serial_port_data(port);
727 unsigned int control_state;
728 unsigned long flags;
Alan Coxe19b2562008-07-22 11:14:30 +0100729
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 spin_lock_irqsave(&priv->lock, flags);
731 control_state = priv->control_state;
732 spin_unlock_irqrestore(&priv->lock, flags);
733
734 return control_state;
735}
736
Alan Cox20b9d172011-02-14 16:26:50 +0000737static int mct_u232_tiocmset(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 unsigned int set, unsigned int clear)
739{
Alan Cox95da3102008-07-22 11:09:07 +0100740 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 struct mct_u232_private *priv = usb_get_serial_port_data(port);
742 unsigned int control_state;
743 unsigned long flags;
Alan Coxe19b2562008-07-22 11:14:30 +0100744
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 spin_lock_irqsave(&priv->lock, flags);
746 control_state = priv->control_state;
747
748 if (set & TIOCM_RTS)
749 control_state |= TIOCM_RTS;
750 if (set & TIOCM_DTR)
751 control_state |= TIOCM_DTR;
752 if (clear & TIOCM_RTS)
753 control_state &= ~TIOCM_RTS;
754 if (clear & TIOCM_DTR)
755 control_state &= ~TIOCM_DTR;
756
757 priv->control_state = control_state;
758 spin_unlock_irqrestore(&priv->lock, flags);
Greg Kroah-Hartman4a770cc2012-09-14 15:08:30 -0700759 return mct_u232_set_modem_ctrl(port, control_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760}
761
Alan Cox95da3102008-07-22 11:09:07 +0100762static void mct_u232_throttle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763{
Alan Cox95da3102008-07-22 11:09:07 +0100764 struct usb_serial_port *port = tty->driver_data;
Dave Platt45b844d2007-05-08 11:00:12 -0700765 struct mct_u232_private *priv = usb_get_serial_port_data(port);
Dave Platt45b844d2007-05-08 11:00:12 -0700766 unsigned int control_state;
Dave Platt45b844d2007-05-08 11:00:12 -0700767
Oliver Neukum63832512009-10-07 10:50:23 +0200768 spin_lock_irq(&priv->lock);
Dave Platt45b844d2007-05-08 11:00:12 -0700769 priv->rx_flags |= THROTTLED;
770 if (C_CRTSCTS(tty)) {
Alan Cox95da3102008-07-22 11:09:07 +0100771 priv->control_state &= ~TIOCM_RTS;
772 control_state = priv->control_state;
Oliver Neukum63832512009-10-07 10:50:23 +0200773 spin_unlock_irq(&priv->lock);
Greg Kroah-Hartman4a770cc2012-09-14 15:08:30 -0700774 mct_u232_set_modem_ctrl(port, control_state);
Dave Platt45b844d2007-05-08 11:00:12 -0700775 } else {
Oliver Neukum63832512009-10-07 10:50:23 +0200776 spin_unlock_irq(&priv->lock);
Dave Platt45b844d2007-05-08 11:00:12 -0700777 }
778}
779
Alan Cox95da3102008-07-22 11:09:07 +0100780static void mct_u232_unthrottle(struct tty_struct *tty)
Dave Platt45b844d2007-05-08 11:00:12 -0700781{
Alan Cox95da3102008-07-22 11:09:07 +0100782 struct usb_serial_port *port = tty->driver_data;
Dave Platt45b844d2007-05-08 11:00:12 -0700783 struct mct_u232_private *priv = usb_get_serial_port_data(port);
Dave Platt45b844d2007-05-08 11:00:12 -0700784 unsigned int control_state;
Dave Platt45b844d2007-05-08 11:00:12 -0700785
Oliver Neukum63832512009-10-07 10:50:23 +0200786 spin_lock_irq(&priv->lock);
Dave Platt45b844d2007-05-08 11:00:12 -0700787 if ((priv->rx_flags & THROTTLED) && C_CRTSCTS(tty)) {
Alan Cox95da3102008-07-22 11:09:07 +0100788 priv->rx_flags &= ~THROTTLED;
789 priv->control_state |= TIOCM_RTS;
790 control_state = priv->control_state;
Oliver Neukum63832512009-10-07 10:50:23 +0200791 spin_unlock_irq(&priv->lock);
Greg Kroah-Hartman4a770cc2012-09-14 15:08:30 -0700792 mct_u232_set_modem_ctrl(port, control_state);
Dave Platt45b844d2007-05-08 11:00:12 -0700793 } else {
Oliver Neukum63832512009-10-07 10:50:23 +0200794 spin_unlock_irq(&priv->lock);
Dave Platt45b844d2007-05-08 11:00:12 -0700795 }
796}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
Randy Dunlap4acfaf82011-04-03 11:42:00 -0700798static int mct_u232_ioctl(struct tty_struct *tty,
Vadim Tsozik7af75af2011-01-09 01:00:11 -0500799 unsigned int cmd, unsigned long arg)
800{
801 DEFINE_WAIT(wait);
802 struct usb_serial_port *port = tty->driver_data;
803 struct mct_u232_private *mct_u232_port = usb_get_serial_port_data(port);
804 struct async_icount cnow, cprev;
805 unsigned long flags;
806
Greg Kroah-Hartman4a770cc2012-09-14 15:08:30 -0700807 dev_dbg(&port->dev, "%s - cmd = 0x%x\n", __func__, cmd);
Vadim Tsozik7af75af2011-01-09 01:00:11 -0500808
809 switch (cmd) {
810
811 case TIOCMIWAIT:
812
Greg Kroah-Hartman4a770cc2012-09-14 15:08:30 -0700813 dev_dbg(&port->dev, "%s TIOCMIWAIT", __func__);
Vadim Tsozik7af75af2011-01-09 01:00:11 -0500814
815 spin_lock_irqsave(&mct_u232_port->lock, flags);
816 cprev = mct_u232_port->icount;
817 spin_unlock_irqrestore(&mct_u232_port->lock, flags);
818 for ( ; ; ) {
819 prepare_to_wait(&mct_u232_port->msr_wait,
820 &wait, TASK_INTERRUPTIBLE);
821 schedule();
822 finish_wait(&mct_u232_port->msr_wait, &wait);
823 /* see if a signal did it */
824 if (signal_pending(current))
825 return -ERESTARTSYS;
826 spin_lock_irqsave(&mct_u232_port->lock, flags);
827 cnow = mct_u232_port->icount;
828 spin_unlock_irqrestore(&mct_u232_port->lock, flags);
829 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
830 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
831 return -EIO; /* no change => error */
832 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
833 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
834 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
835 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
836 return 0;
837 }
838 cprev = cnow;
839 }
840
841 }
842 return -ENOIOCTLCMD;
843}
844
845static int mct_u232_get_icount(struct tty_struct *tty,
846 struct serial_icounter_struct *icount)
847{
848 struct usb_serial_port *port = tty->driver_data;
849 struct mct_u232_private *mct_u232_port = usb_get_serial_port_data(port);
850 struct async_icount *ic = &mct_u232_port->icount;
851 unsigned long flags;
852
853 spin_lock_irqsave(&mct_u232_port->lock, flags);
854
855 icount->cts = ic->cts;
856 icount->dsr = ic->dsr;
857 icount->rng = ic->rng;
858 icount->dcd = ic->dcd;
859 icount->rx = ic->rx;
860 icount->tx = ic->tx;
861 icount->frame = ic->frame;
862 icount->overrun = ic->overrun;
863 icount->parity = ic->parity;
864 icount->brk = ic->brk;
865 icount->buf_overrun = ic->buf_overrun;
866
867 spin_unlock_irqrestore(&mct_u232_port->lock, flags);
868
Greg Kroah-Hartman4a770cc2012-09-14 15:08:30 -0700869 dev_dbg(&port->dev, "%s TIOCGICOUNT RX=%d, TX=%d\n",
870 __func__, icount->rx, icount->tx);
Vadim Tsozik7af75af2011-01-09 01:00:11 -0500871 return 0;
872}
873
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -0700874module_usb_serial_driver(serial_drivers, id_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Alan Coxe19b2562008-07-22 11:14:30 +0100876MODULE_AUTHOR(DRIVER_AUTHOR);
877MODULE_DESCRIPTION(DRIVER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878MODULE_LICENSE("GPL");