blob: 6f4303cef0d8e44c9e4eb022bef0caa6673f15c1 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#define DRIVER_AUTHOR "Wolfgang Grandegger <wolfgang@ces.ch>"
42#define DRIVER_DESC "Magic Control Technology USB-RS232 converter driver"
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044/*
45 * Function prototypes
46 */
Alan Coxe19b2562008-07-22 11:14:30 +010047static int mct_u232_startup(struct usb_serial *serial);
Johan Hovolda8f2ae72012-10-25 10:29:13 +020048static int mct_u232_port_probe(struct usb_serial_port *port);
49static int mct_u232_port_remove(struct usb_serial_port *remove);
Alan Coxa509a7e2009-09-19 13:13:26 -070050static int mct_u232_open(struct tty_struct *tty, struct usb_serial_port *port);
Alan Cox335f8512009-06-11 12:26:29 +010051static void mct_u232_close(struct usb_serial_port *port);
52static void mct_u232_dtr_rts(struct usb_serial_port *port, int on);
Alan Coxe19b2562008-07-22 11:14:30 +010053static void mct_u232_read_int_callback(struct urb *urb);
54static void mct_u232_set_termios(struct tty_struct *tty,
55 struct usb_serial_port *port, struct ktermios *old);
56static void mct_u232_break_ctl(struct tty_struct *tty, int break_state);
Alan Cox60b33c12011-02-14 16:26:14 +000057static int mct_u232_tiocmget(struct tty_struct *tty);
Alan Cox20b9d172011-02-14 16:26:50 +000058static int mct_u232_tiocmset(struct tty_struct *tty,
Alan Coxe19b2562008-07-22 11:14:30 +010059 unsigned int set, unsigned int clear);
Randy Dunlap4acfaf82011-04-03 11:42:00 -070060static int mct_u232_ioctl(struct tty_struct *tty,
Vadim Tsozik7af75af2011-01-09 01:00:11 -050061 unsigned int cmd, unsigned long arg);
Alan Coxe19b2562008-07-22 11:14:30 +010062static void mct_u232_throttle(struct tty_struct *tty);
63static void mct_u232_unthrottle(struct tty_struct *tty);
Dave Platt45b844d2007-05-08 11:00:12 -070064
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066/*
67 * All of the device info needed for the MCT USB-RS232 converter.
68 */
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -070069static const struct usb_device_id id_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 { USB_DEVICE(MCT_U232_VID, MCT_U232_PID) },
71 { USB_DEVICE(MCT_U232_VID, MCT_U232_SITECOM_PID) },
72 { USB_DEVICE(MCT_U232_VID, MCT_U232_DU_H3SP_PID) },
73 { USB_DEVICE(MCT_U232_BELKIN_F5U109_VID, MCT_U232_BELKIN_F5U109_PID) },
74 { } /* Terminating entry */
75};
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -070076MODULE_DEVICE_TABLE(usb, id_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Greg Kroah-Hartmanea653702005-06-20 21:15:16 -070078static struct usb_serial_driver mct_u232_device = {
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -070079 .driver = {
80 .owner = THIS_MODULE,
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -070081 .name = "mct_u232",
Greg Kroah-Hartman18fcac32005-06-20 21:15:16 -070082 },
Greg Kroah-Hartman269bda12005-06-20 21:15:16 -070083 .description = "MCT U232",
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -070084 .id_table = id_table,
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 .num_ports = 1,
86 .open = mct_u232_open,
87 .close = mct_u232_close,
Alan Cox335f8512009-06-11 12:26:29 +010088 .dtr_rts = mct_u232_dtr_rts,
Dave Platt45b844d2007-05-08 11:00:12 -070089 .throttle = mct_u232_throttle,
90 .unthrottle = mct_u232_unthrottle,
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 .read_int_callback = mct_u232_read_int_callback,
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 .set_termios = mct_u232_set_termios,
93 .break_ctl = mct_u232_break_ctl,
94 .tiocmget = mct_u232_tiocmget,
95 .tiocmset = mct_u232_tiocmset,
96 .attach = mct_u232_startup,
Johan Hovolda8f2ae72012-10-25 10:29:13 +020097 .port_probe = mct_u232_port_probe,
98 .port_remove = mct_u232_port_remove,
Vadim Tsozik7af75af2011-01-09 01:00:11 -050099 .ioctl = mct_u232_ioctl,
Johan Hovold468c7402013-03-21 12:37:12 +0100100 .get_icount = usb_serial_generic_get_icount,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101};
102
Alan Stern4d2a7af2012-02-23 14:57:09 -0500103static struct usb_serial_driver * const serial_drivers[] = {
104 &mct_u232_device, NULL
105};
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107struct mct_u232_private {
108 spinlock_t lock;
109 unsigned int control_state; /* Modem Line Setting (TIOCM) */
110 unsigned char last_lcr; /* Line Control Register */
111 unsigned char last_lsr; /* Line Status Register */
112 unsigned char last_msr; /* Modem Status Register */
Dave Platt45b844d2007-05-08 11:00:12 -0700113 unsigned int rx_flags; /* Throttling flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114};
115
Dave Platt45b844d2007-05-08 11:00:12 -0700116#define THROTTLED 0x01
117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118/*
119 * Handle vendor specific USB requests
120 */
121
122#define WDR_TIMEOUT 5000 /* default urb timeout */
123
124/*
125 * Later day 2.6.0-test kernels have new baud rates like B230400 which
126 * we do not know how to support. We ignore them for the moment.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 */
Alan Coxe19b2562008-07-22 11:14:30 +0100128static int mct_u232_calculate_baud_rate(struct usb_serial *serial,
129 speed_t value, speed_t *result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
Alan Coxd0fab0d2007-12-13 16:15:29 -0800131 *result = value;
132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 if (le16_to_cpu(serial->dev->descriptor.idProduct) == MCT_U232_SITECOM_PID
Alan Coxe19b2562008-07-22 11:14:30 +0100134 || le16_to_cpu(serial->dev->descriptor.idProduct) == MCT_U232_BELKIN_F5U109_PID) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 switch (value) {
Alan Coxe19b2562008-07-22 11:14:30 +0100136 case 300:
137 return 0x01;
138 case 600:
139 return 0x02; /* this one not tested */
140 case 1200:
141 return 0x03;
142 case 2400:
143 return 0x04;
144 case 4800:
145 return 0x06;
146 case 9600:
147 return 0x08;
148 case 19200:
149 return 0x09;
150 case 38400:
151 return 0x0a;
152 case 57600:
153 return 0x0b;
154 case 115200:
155 return 0x0c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 default:
Alan Coxd0fab0d2007-12-13 16:15:29 -0800157 *result = 9600;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 return 0x08;
159 }
160 } else {
Alan Coxd0fab0d2007-12-13 16:15:29 -0800161 /* FIXME: Can we use any divider - should we do
162 divider = 115200/value;
163 real baud = 115200/divider */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 switch (value) {
Andrew Mortonb3aceb22007-08-10 14:53:35 -0700165 case 300: break;
166 case 600: break;
167 case 1200: break;
168 case 2400: break;
169 case 4800: break;
170 case 9600: break;
171 case 19200: break;
172 case 38400: break;
173 case 57600: break;
174 case 115200: break;
175 default:
Andrew Mortonb3aceb22007-08-10 14:53:35 -0700176 value = 9600;
Alan Coxd0fab0d2007-12-13 16:15:29 -0800177 *result = 9600;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 }
179 return 115200/value;
180 }
181}
182
Alan Cox95da3102008-07-22 11:09:07 +0100183static int mct_u232_set_baud_rate(struct tty_struct *tty,
184 struct usb_serial *serial, struct usb_serial_port *port, speed_t value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
Pete Zaitcevaf2ac1a2009-12-07 20:29:05 -0700186 unsigned int divisor;
Alan Coxe19b2562008-07-22 11:14:30 +0100187 int rc;
Pete Zaitcevaf2ac1a2009-12-07 20:29:05 -0700188 unsigned char *buf;
Alan Coxe19b2562008-07-22 11:14:30 +0100189 unsigned char cts_enable_byte = 0;
190 speed_t speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Pete Zaitcevaf2ac1a2009-12-07 20:29:05 -0700192 buf = kmalloc(MCT_U232_MAX_SIZE, GFP_KERNEL);
193 if (buf == NULL)
194 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Pete Zaitcevaf2ac1a2009-12-07 20:29:05 -0700196 divisor = mct_u232_calculate_baud_rate(serial, value, &speed);
197 put_unaligned_le32(cpu_to_le32(divisor), buf);
Alan Coxe19b2562008-07-22 11:14:30 +0100198 rc = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
199 MCT_U232_SET_BAUD_RATE_REQUEST,
200 MCT_U232_SET_REQUEST_TYPE,
Pete Zaitcevaf2ac1a2009-12-07 20:29:05 -0700201 0, 0, buf, MCT_U232_SET_BAUD_RATE_SIZE,
Alan Coxe19b2562008-07-22 11:14:30 +0100202 WDR_TIMEOUT);
Alan Coxd0fab0d2007-12-13 16:15:29 -0800203 if (rc < 0) /*FIXME: What value speed results */
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700204 dev_err(&port->dev, "Set BAUD RATE %d failed (error = %d)\n",
205 value, rc);
Alan Coxd0fab0d2007-12-13 16:15:29 -0800206 else
Alan Cox95da3102008-07-22 11:09:07 +0100207 tty_encode_baud_rate(tty, speed, speed);
Greg Kroah-Hartman4a770cc2012-09-14 15:08:30 -0700208 dev_dbg(&port->dev, "set_baud_rate: value: 0x%x, divisor: 0x%x\n", value, divisor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210 /* Mimic the MCT-supplied Windows driver (version 1.21P.0104), which
211 always sends two extra USB 'device request' messages after the
212 'baud rate change' message. The actual functionality of the
213 request codes in these messages is not fully understood but these
214 particular codes are never seen in any operation besides a baud
Dave Platt45b844d2007-05-08 11:00:12 -0700215 rate change. Both of these messages send a single byte of data.
216 In the first message, the value of this byte is always zero.
217
218 The second message has been determined experimentally to control
219 whether data will be transmitted to a device which is not asserting
220 the 'CTS' signal. If the second message's data byte is zero, data
221 will be transmitted even if 'CTS' is not asserted (i.e. no hardware
Alan Coxe19b2562008-07-22 11:14:30 +0100222 flow control). if the second message's data byte is nonzero (a
223 value of 1 is used by this driver), data will not be transmitted to
224 a device which is not asserting 'CTS'.
Dave Platt45b844d2007-05-08 11:00:12 -0700225 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Pete Zaitcevaf2ac1a2009-12-07 20:29:05 -0700227 buf[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 rc = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
Alan Coxe19b2562008-07-22 11:14:30 +0100229 MCT_U232_SET_UNKNOWN1_REQUEST,
230 MCT_U232_SET_REQUEST_TYPE,
Pete Zaitcevaf2ac1a2009-12-07 20:29:05 -0700231 0, 0, buf, MCT_U232_SET_UNKNOWN1_SIZE,
Alan Coxe19b2562008-07-22 11:14:30 +0100232 WDR_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 if (rc < 0)
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700234 dev_err(&port->dev, "Sending USB device request code %d "
235 "failed (error = %d)\n", MCT_U232_SET_UNKNOWN1_REQUEST,
236 rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Alan Coxe19b2562008-07-22 11:14:30 +0100238 if (port && C_CRTSCTS(tty))
Dave Platt45b844d2007-05-08 11:00:12 -0700239 cts_enable_byte = 1;
Dave Platt45b844d2007-05-08 11:00:12 -0700240
Greg Kroah-Hartman4a770cc2012-09-14 15:08:30 -0700241 dev_dbg(&port->dev, "set_baud_rate: send second control message, data = %02X\n",
242 cts_enable_byte);
Pete Zaitcevaf2ac1a2009-12-07 20:29:05 -0700243 buf[0] = cts_enable_byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 rc = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
Alan Coxe19b2562008-07-22 11:14:30 +0100245 MCT_U232_SET_CTS_REQUEST,
246 MCT_U232_SET_REQUEST_TYPE,
Pete Zaitcevaf2ac1a2009-12-07 20:29:05 -0700247 0, 0, buf, MCT_U232_SET_CTS_SIZE,
Alan Coxe19b2562008-07-22 11:14:30 +0100248 WDR_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 if (rc < 0)
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700250 dev_err(&port->dev, "Sending USB device request code %d "
251 "failed (error = %d)\n", MCT_U232_SET_CTS_REQUEST, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Pete Zaitcevaf2ac1a2009-12-07 20:29:05 -0700253 kfree(buf);
Alan Coxe19b2562008-07-22 11:14:30 +0100254 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255} /* mct_u232_set_baud_rate */
256
Greg Kroah-Hartman4a770cc2012-09-14 15:08:30 -0700257static int mct_u232_set_line_ctrl(struct usb_serial_port *port,
258 unsigned char lcr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
Alan Coxe19b2562008-07-22 11:14:30 +0100260 int rc;
Pete Zaitcevaf2ac1a2009-12-07 20:29:05 -0700261 unsigned char *buf;
262
263 buf = kmalloc(MCT_U232_MAX_SIZE, GFP_KERNEL);
264 if (buf == NULL)
265 return -ENOMEM;
266
267 buf[0] = lcr;
Greg Kroah-Hartman4a770cc2012-09-14 15:08:30 -0700268 rc = usb_control_msg(port->serial->dev, usb_sndctrlpipe(port->serial->dev, 0),
Alan Coxe19b2562008-07-22 11:14:30 +0100269 MCT_U232_SET_LINE_CTRL_REQUEST,
270 MCT_U232_SET_REQUEST_TYPE,
Pete Zaitcevaf2ac1a2009-12-07 20:29:05 -0700271 0, 0, buf, MCT_U232_SET_LINE_CTRL_SIZE,
Alan Coxe19b2562008-07-22 11:14:30 +0100272 WDR_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 if (rc < 0)
Greg Kroah-Hartman4a770cc2012-09-14 15:08:30 -0700274 dev_err(&port->dev, "Set LINE CTRL 0x%x failed (error = %d)\n", lcr, rc);
275 dev_dbg(&port->dev, "set_line_ctrl: 0x%x\n", lcr);
Pete Zaitcevaf2ac1a2009-12-07 20:29:05 -0700276 kfree(buf);
Alan Coxe19b2562008-07-22 11:14:30 +0100277 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278} /* mct_u232_set_line_ctrl */
279
Greg Kroah-Hartman4a770cc2012-09-14 15:08:30 -0700280static int mct_u232_set_modem_ctrl(struct usb_serial_port *port,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 unsigned int control_state)
282{
Alan Coxe19b2562008-07-22 11:14:30 +0100283 int rc;
Pete Zaitcevaf2ac1a2009-12-07 20:29:05 -0700284 unsigned char mcr;
285 unsigned char *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Pete Zaitcevaf2ac1a2009-12-07 20:29:05 -0700287 buf = kmalloc(MCT_U232_MAX_SIZE, GFP_KERNEL);
288 if (buf == NULL)
289 return -ENOMEM;
290
291 mcr = MCT_U232_MCR_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 if (control_state & TIOCM_DTR)
293 mcr |= MCT_U232_MCR_DTR;
294 if (control_state & TIOCM_RTS)
295 mcr |= MCT_U232_MCR_RTS;
296
Pete Zaitcevaf2ac1a2009-12-07 20:29:05 -0700297 buf[0] = mcr;
Greg Kroah-Hartman4a770cc2012-09-14 15:08:30 -0700298 rc = usb_control_msg(port->serial->dev, usb_sndctrlpipe(port->serial->dev, 0),
Alan Coxe19b2562008-07-22 11:14:30 +0100299 MCT_U232_SET_MODEM_CTRL_REQUEST,
300 MCT_U232_SET_REQUEST_TYPE,
Pete Zaitcevaf2ac1a2009-12-07 20:29:05 -0700301 0, 0, buf, MCT_U232_SET_MODEM_CTRL_SIZE,
Alan Coxe19b2562008-07-22 11:14:30 +0100302 WDR_TIMEOUT);
Alan Cox1aa3c632012-05-22 20:45:13 +0100303 kfree(buf);
304
Greg Kroah-Hartman4a770cc2012-09-14 15:08:30 -0700305 dev_dbg(&port->dev, "set_modem_ctrl: state=0x%x ==> mcr=0x%x\n", control_state, mcr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Alan Cox1aa3c632012-05-22 20:45:13 +0100307 if (rc < 0) {
Greg Kroah-Hartman4a770cc2012-09-14 15:08:30 -0700308 dev_err(&port->dev, "Set MODEM CTRL 0x%x failed (error = %d)\n", mcr, rc);
Alan Cox1aa3c632012-05-22 20:45:13 +0100309 return rc;
310 }
311 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312} /* mct_u232_set_modem_ctrl */
313
Greg Kroah-Hartman4a770cc2012-09-14 15:08:30 -0700314static int mct_u232_get_modem_stat(struct usb_serial_port *port,
315 unsigned char *msr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316{
Alan Coxe19b2562008-07-22 11:14:30 +0100317 int rc;
Pete Zaitcevaf2ac1a2009-12-07 20:29:05 -0700318 unsigned char *buf;
319
320 buf = kmalloc(MCT_U232_MAX_SIZE, GFP_KERNEL);
321 if (buf == NULL) {
322 *msr = 0;
323 return -ENOMEM;
324 }
Greg Kroah-Hartman4a770cc2012-09-14 15:08:30 -0700325 rc = usb_control_msg(port->serial->dev, usb_rcvctrlpipe(port->serial->dev, 0),
Alan Coxe19b2562008-07-22 11:14:30 +0100326 MCT_U232_GET_MODEM_STAT_REQUEST,
327 MCT_U232_GET_REQUEST_TYPE,
Pete Zaitcevaf2ac1a2009-12-07 20:29:05 -0700328 0, 0, buf, MCT_U232_GET_MODEM_STAT_SIZE,
Alan Coxe19b2562008-07-22 11:14:30 +0100329 WDR_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 if (rc < 0) {
Greg Kroah-Hartman4a770cc2012-09-14 15:08:30 -0700331 dev_err(&port->dev, "Get MODEM STATus failed (error = %d)\n", rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 *msr = 0;
Pete Zaitcevaf2ac1a2009-12-07 20:29:05 -0700333 } else {
334 *msr = buf[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 }
Greg Kroah-Hartman4a770cc2012-09-14 15:08:30 -0700336 dev_dbg(&port->dev, "get_modem_stat: 0x%x\n", *msr);
Pete Zaitcevaf2ac1a2009-12-07 20:29:05 -0700337 kfree(buf);
Alan Coxe19b2562008-07-22 11:14:30 +0100338 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339} /* mct_u232_get_modem_stat */
340
Vadim Tsozik7af75af2011-01-09 01:00:11 -0500341static void mct_u232_msr_to_icount(struct async_icount *icount,
342 unsigned char msr)
343{
344 /* Translate Control Line states */
345 if (msr & MCT_U232_MSR_DDSR)
346 icount->dsr++;
347 if (msr & MCT_U232_MSR_DCTS)
348 icount->cts++;
349 if (msr & MCT_U232_MSR_DRI)
350 icount->rng++;
351 if (msr & MCT_U232_MSR_DCD)
352 icount->dcd++;
353} /* mct_u232_msr_to_icount */
354
Greg Kroah-Hartman4a770cc2012-09-14 15:08:30 -0700355static void mct_u232_msr_to_state(struct usb_serial_port *port,
356 unsigned int *control_state, unsigned char msr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
Alan Coxe19b2562008-07-22 11:14:30 +0100358 /* Translate Control Line states */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 if (msr & MCT_U232_MSR_DSR)
360 *control_state |= TIOCM_DSR;
361 else
362 *control_state &= ~TIOCM_DSR;
363 if (msr & MCT_U232_MSR_CTS)
364 *control_state |= TIOCM_CTS;
365 else
366 *control_state &= ~TIOCM_CTS;
367 if (msr & MCT_U232_MSR_RI)
368 *control_state |= TIOCM_RI;
369 else
370 *control_state &= ~TIOCM_RI;
371 if (msr & MCT_U232_MSR_CD)
372 *control_state |= TIOCM_CD;
373 else
374 *control_state &= ~TIOCM_CD;
Greg Kroah-Hartman4a770cc2012-09-14 15:08:30 -0700375 dev_dbg(&port->dev, "msr_to_state: msr=0x%x ==> state=0x%x\n", msr, *control_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376} /* mct_u232_msr_to_state */
377
378/*
379 * Driver's tty interface functions
380 */
381
Alan Coxe19b2562008-07-22 11:14:30 +0100382static int mct_u232_startup(struct usb_serial *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 struct usb_serial_port *port, *rport;
385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 /* Puh, that's dirty */
387 port = serial->port[0];
388 rport = serial->port[1];
Mariusz Kozlowski73135bb2006-11-08 15:36:42 +0100389 /* No unlinking, it wasn't submitted yet. */
390 usb_free_urb(port->read_urb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 port->read_urb = rport->interrupt_in_urb;
392 rport->interrupt_in_urb = NULL;
393 port->read_urb->context = port;
394
Alan Coxe19b2562008-07-22 11:14:30 +0100395 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396} /* mct_u232_startup */
397
Johan Hovolda8f2ae72012-10-25 10:29:13 +0200398static int mct_u232_port_probe(struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
400 struct mct_u232_private *priv;
Alan Coxe19b2562008-07-22 11:14:30 +0100401
Johan Hovolda8f2ae72012-10-25 10:29:13 +0200402 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
403 if (!priv)
404 return -ENOMEM;
405
406 spin_lock_init(&priv->lock);
Johan Hovolda8f2ae72012-10-25 10:29:13 +0200407
408 usb_set_serial_port_data(port, priv);
409
410 return 0;
411}
412
413static int mct_u232_port_remove(struct usb_serial_port *port)
414{
415 struct mct_u232_private *priv;
416
417 priv = usb_get_serial_port_data(port);
418 kfree(priv);
419
420 return 0;
421}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Alan Coxa509a7e2009-09-19 13:13:26 -0700423static int mct_u232_open(struct tty_struct *tty, struct usb_serial_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
425 struct usb_serial *serial = port->serial;
426 struct mct_u232_private *priv = usb_get_serial_port_data(port);
427 int retval = 0;
428 unsigned int control_state;
429 unsigned long flags;
430 unsigned char last_lcr;
431 unsigned char last_msr;
432
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 /* Compensate for a hardware bug: although the Sitecom U232-P25
434 * device reports a maximum output packet size of 32 bytes,
435 * it seems to be able to accept only 16 bytes (and that's what
436 * SniffUSB says too...)
437 */
Alan Coxe19b2562008-07-22 11:14:30 +0100438 if (le16_to_cpu(serial->dev->descriptor.idProduct)
439 == MCT_U232_SITECOM_PID)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 port->bulk_out_size = 16;
441
Alan Coxe19b2562008-07-22 11:14:30 +0100442 /* Do a defined restart: the normal serial device seems to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 * always turn on DTR and RTS here, so do the same. I'm not
444 * sure if this is really necessary. But it should not harm
445 * either.
446 */
447 spin_lock_irqsave(&priv->lock, flags);
Alan Coxadc8d742012-07-14 15:31:47 +0100448 if (tty && (tty->termios.c_cflag & CBAUD))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 priv->control_state = TIOCM_DTR | TIOCM_RTS;
450 else
451 priv->control_state = 0;
Alan Coxe19b2562008-07-22 11:14:30 +0100452
453 priv->last_lcr = (MCT_U232_DATA_BITS_8 |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 MCT_U232_PARITY_NONE |
455 MCT_U232_STOP_BITS_1);
456 control_state = priv->control_state;
457 last_lcr = priv->last_lcr;
458 spin_unlock_irqrestore(&priv->lock, flags);
Greg Kroah-Hartman4a770cc2012-09-14 15:08:30 -0700459 mct_u232_set_modem_ctrl(port, control_state);
460 mct_u232_set_line_ctrl(port, last_lcr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
462 /* Read modem status and update control state */
Greg Kroah-Hartman4a770cc2012-09-14 15:08:30 -0700463 mct_u232_get_modem_stat(port, &last_msr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 spin_lock_irqsave(&priv->lock, flags);
465 priv->last_msr = last_msr;
Greg Kroah-Hartman4a770cc2012-09-14 15:08:30 -0700466 mct_u232_msr_to_state(port, &priv->control_state, priv->last_msr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 spin_unlock_irqrestore(&priv->lock, flags);
468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 retval = usb_submit_urb(port->read_urb, GFP_KERNEL);
470 if (retval) {
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700471 dev_err(&port->dev,
472 "usb_submit_urb(read bulk) failed pipe 0x%x err %d\n",
473 port->read_urb->pipe, retval);
Oliver Neukum2f007de2007-03-29 10:45:17 +0200474 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 }
476
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 retval = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
Oliver Neukum2f007de2007-03-29 10:45:17 +0200478 if (retval) {
479 usb_kill_urb(port->read_urb);
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700480 dev_err(&port->dev,
481 "usb_submit_urb(read int) failed pipe 0x%x err %d",
482 port->interrupt_in_urb->pipe, retval);
Oliver Neukum2f007de2007-03-29 10:45:17 +0200483 goto error;
484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 return 0;
Oliver Neukum2f007de2007-03-29 10:45:17 +0200486
487error:
488 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489} /* mct_u232_open */
490
Alan Cox335f8512009-06-11 12:26:29 +0100491static void mct_u232_dtr_rts(struct usb_serial_port *port, int on)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
Dave Platt45b844d2007-05-08 11:00:12 -0700493 unsigned int control_state;
494 struct mct_u232_private *priv = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Johan Hovoldb2ca6992013-02-13 17:53:28 +0100496 spin_lock_irq(&priv->lock);
497 if (on)
498 priv->control_state |= TIOCM_DTR | TIOCM_RTS;
499 else
500 priv->control_state &= ~(TIOCM_DTR | TIOCM_RTS);
501 control_state = priv->control_state;
502 spin_unlock_irq(&priv->lock);
503
504 mct_u232_set_modem_ctrl(port, control_state);
Alan Cox335f8512009-06-11 12:26:29 +0100505}
Dave Platt45b844d2007-05-08 11:00:12 -0700506
Alan Cox335f8512009-06-11 12:26:29 +0100507static void mct_u232_close(struct usb_serial_port *port)
508{
Johan Hovold5260e452012-10-25 10:29:14 +0200509 /*
510 * Must kill the read urb as it is actually an interrupt urb, which
511 * generic close thus fails to kill.
512 */
513 usb_kill_urb(port->read_urb);
514 usb_kill_urb(port->interrupt_in_urb);
515
516 usb_serial_generic_close(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517} /* mct_u232_close */
518
519
Alan Coxe19b2562008-07-22 11:14:30 +0100520static void mct_u232_read_int_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521{
Ming Leicdc97792008-02-24 18:41:47 +0800522 struct usb_serial_port *port = urb->context;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 struct mct_u232_private *priv = usb_get_serial_port_data(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 unsigned char *data = urb->transfer_buffer;
Greg Kroah-Hartmane96da392007-06-15 15:44:13 -0700525 int retval;
526 int status = urb->status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 unsigned long flags;
528
Greg Kroah-Hartmane96da392007-06-15 15:44:13 -0700529 switch (status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 case 0:
531 /* success */
532 break;
533 case -ECONNRESET:
534 case -ENOENT:
535 case -ESHUTDOWN:
536 /* this urb is terminated, clean up */
Greg Kroah-Hartman4a770cc2012-09-14 15:08:30 -0700537 dev_dbg(&port->dev, "%s - urb shutting down with status: %d\n",
538 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 return;
540 default:
Greg Kroah-Hartman4a770cc2012-09-14 15:08:30 -0700541 dev_dbg(&port->dev, "%s - nonzero urb status received: %d\n",
542 __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 goto exit;
544 }
545
Greg Kroah-Hartman59d33f22012-09-18 09:58:57 +0100546 usb_serial_debug_data(&port->dev, __func__, urb->actual_length, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
548 /*
549 * Work-a-round: handle the 'usual' bulk-in pipe here
550 */
551 if (urb->transfer_buffer_length > 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 if (urb->actual_length) {
Jiri Slaby2e124b42013-01-03 15:53:06 +0100553 tty_insert_flip_string(&port->port, data,
554 urb->actual_length);
555 tty_flip_buffer_push(&port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 }
557 goto exit;
558 }
Alan Coxe19b2562008-07-22 11:14:30 +0100559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 /*
561 * The interrupt-in pipe signals exceptional conditions (modem line
562 * signal changes and errors). data[0] holds MSR, data[1] holds LSR.
563 */
564 spin_lock_irqsave(&priv->lock, flags);
565 priv->last_msr = data[MCT_U232_MSR_INDEX];
Alan Coxe19b2562008-07-22 11:14:30 +0100566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 /* Record Control Line states */
Greg Kroah-Hartman4a770cc2012-09-14 15:08:30 -0700568 mct_u232_msr_to_state(port, &priv->control_state, priv->last_msr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Johan Hovold468c7402013-03-21 12:37:12 +0100570 mct_u232_msr_to_icount(&port->icount, priv->last_msr);
Vadim Tsozik7af75af2011-01-09 01:00:11 -0500571
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572#if 0
Alan Coxe19b2562008-07-22 11:14:30 +0100573 /* Not yet handled. See belkin_sa.c for further information */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 /* Now to report any errors */
575 priv->last_lsr = data[MCT_U232_LSR_INDEX];
576 /*
577 * fill in the flip buffer here, but I do not know the relation
578 * to the current/next receive buffer or characters. I need
579 * to look in to this before committing any code.
580 */
581 if (priv->last_lsr & MCT_U232_LSR_ERR) {
Alan Cox4a90f092008-10-13 10:39:46 +0100582 tty = tty_port_tty_get(&port->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 /* Overrun Error */
584 if (priv->last_lsr & MCT_U232_LSR_OE) {
585 }
586 /* Parity Error */
587 if (priv->last_lsr & MCT_U232_LSR_PE) {
588 }
589 /* Framing Error */
590 if (priv->last_lsr & MCT_U232_LSR_FE) {
591 }
592 /* Break Indicator */
593 if (priv->last_lsr & MCT_U232_LSR_BI) {
594 }
Alan Cox4a90f092008-10-13 10:39:46 +0100595 tty_kref_put(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 }
597#endif
Johan Hovoldcf1d2442013-03-19 09:21:18 +0100598 wake_up_interruptible(&port->delta_msr_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 spin_unlock_irqrestore(&priv->lock, flags);
600exit:
Alan Coxe19b2562008-07-22 11:14:30 +0100601 retval = usb_submit_urb(urb, GFP_ATOMIC);
Greg Kroah-Hartmane96da392007-06-15 15:44:13 -0700602 if (retval)
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700603 dev_err(&port->dev,
604 "%s - usb_submit_urb failed with result %d\n",
605 __func__, retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606} /* mct_u232_read_int_callback */
607
Alan Coxe19b2562008-07-22 11:14:30 +0100608static void mct_u232_set_termios(struct tty_struct *tty,
609 struct usb_serial_port *port,
610 struct ktermios *old_termios)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
612 struct usb_serial *serial = port->serial;
613 struct mct_u232_private *priv = usb_get_serial_port_data(port);
Alan Coxadc8d742012-07-14 15:31:47 +0100614 struct ktermios *termios = &tty->termios;
Alan Coxd0fab0d2007-12-13 16:15:29 -0800615 unsigned int cflag = termios->c_cflag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 unsigned int old_cflag = old_termios->c_cflag;
617 unsigned long flags;
Dave Platt45b844d2007-05-08 11:00:12 -0700618 unsigned int control_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 unsigned char last_lcr;
620
621 /* get a local copy of the current port settings */
622 spin_lock_irqsave(&priv->lock, flags);
623 control_state = priv->control_state;
624 spin_unlock_irqrestore(&priv->lock, flags);
625 last_lcr = 0;
626
627 /*
628 * Update baud rate.
629 * Do not attempt to cache old rates and skip settings,
630 * disconnects screw such tricks up completely.
631 * Premature optimization is the root of all evil.
632 */
633
Alan Coxe19b2562008-07-22 11:14:30 +0100634 /* reassert DTR and RTS on transition from B0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 if ((old_cflag & CBAUD) == B0) {
Greg Kroah-Hartman4a770cc2012-09-14 15:08:30 -0700636 dev_dbg(&port->dev, "%s: baud was B0\n", __func__);
Dave Platt45b844d2007-05-08 11:00:12 -0700637 control_state |= TIOCM_DTR | TIOCM_RTS;
Greg Kroah-Hartman4a770cc2012-09-14 15:08:30 -0700638 mct_u232_set_modem_ctrl(port, control_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 }
640
Alan Cox95da3102008-07-22 11:09:07 +0100641 mct_u232_set_baud_rate(tty, serial, port, tty_get_baud_rate(tty));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Alan Coxe19b2562008-07-22 11:14:30 +0100643 if ((cflag & CBAUD) == B0) {
Greg Kroah-Hartman4a770cc2012-09-14 15:08:30 -0700644 dev_dbg(&port->dev, "%s: baud is B0\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 /* Drop RTS and DTR */
646 control_state &= ~(TIOCM_DTR | TIOCM_RTS);
Greg Kroah-Hartman4a770cc2012-09-14 15:08:30 -0700647 mct_u232_set_modem_ctrl(port, control_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 }
649
650 /*
651 * Update line control register (LCR)
652 */
653
654 /* set the parity */
655 if (cflag & PARENB)
656 last_lcr |= (cflag & PARODD) ?
657 MCT_U232_PARITY_ODD : MCT_U232_PARITY_EVEN;
658 else
659 last_lcr |= MCT_U232_PARITY_NONE;
660
661 /* set the number of data bits */
662 switch (cflag & CSIZE) {
663 case CS5:
664 last_lcr |= MCT_U232_DATA_BITS_5; break;
665 case CS6:
666 last_lcr |= MCT_U232_DATA_BITS_6; break;
667 case CS7:
668 last_lcr |= MCT_U232_DATA_BITS_7; break;
669 case CS8:
670 last_lcr |= MCT_U232_DATA_BITS_8; break;
671 default:
Greg Kroah-Hartman194343d2008-08-20 16:56:34 -0700672 dev_err(&port->dev,
673 "CSIZE was not CS5-CS8, using default of 8\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 last_lcr |= MCT_U232_DATA_BITS_8;
675 break;
676 }
677
Alan Coxd0fab0d2007-12-13 16:15:29 -0800678 termios->c_cflag &= ~CMSPAR;
679
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 /* set the number of stop bits */
681 last_lcr |= (cflag & CSTOPB) ?
682 MCT_U232_STOP_BITS_2 : MCT_U232_STOP_BITS_1;
683
Greg Kroah-Hartman4a770cc2012-09-14 15:08:30 -0700684 mct_u232_set_line_ctrl(port, last_lcr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 /* save off the modified port settings */
687 spin_lock_irqsave(&priv->lock, flags);
688 priv->control_state = control_state;
689 priv->last_lcr = last_lcr;
690 spin_unlock_irqrestore(&priv->lock, flags);
691} /* mct_u232_set_termios */
692
Alan Cox95da3102008-07-22 11:09:07 +0100693static void mct_u232_break_ctl(struct tty_struct *tty, int break_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694{
Alan Cox95da3102008-07-22 11:09:07 +0100695 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 struct mct_u232_private *priv = usb_get_serial_port_data(port);
697 unsigned char lcr;
698 unsigned long flags;
699
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 spin_lock_irqsave(&priv->lock, flags);
701 lcr = priv->last_lcr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
703 if (break_state)
704 lcr |= MCT_U232_SET_BREAK;
Alan Cox6b447f042009-01-02 13:48:56 +0000705 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Greg Kroah-Hartman4a770cc2012-09-14 15:08:30 -0700707 mct_u232_set_line_ctrl(port, lcr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708} /* mct_u232_break_ctl */
709
710
Alan Cox60b33c12011-02-14 16:26:14 +0000711static int mct_u232_tiocmget(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
Alan Cox95da3102008-07-22 11:09:07 +0100713 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 struct mct_u232_private *priv = usb_get_serial_port_data(port);
715 unsigned int control_state;
716 unsigned long flags;
Alan Coxe19b2562008-07-22 11:14:30 +0100717
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 spin_lock_irqsave(&priv->lock, flags);
719 control_state = priv->control_state;
720 spin_unlock_irqrestore(&priv->lock, flags);
721
722 return control_state;
723}
724
Alan Cox20b9d172011-02-14 16:26:50 +0000725static int mct_u232_tiocmset(struct tty_struct *tty,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 unsigned int set, unsigned int clear)
727{
Alan Cox95da3102008-07-22 11:09:07 +0100728 struct usb_serial_port *port = tty->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 struct mct_u232_private *priv = usb_get_serial_port_data(port);
730 unsigned int control_state;
731 unsigned long flags;
Alan Coxe19b2562008-07-22 11:14:30 +0100732
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 spin_lock_irqsave(&priv->lock, flags);
734 control_state = priv->control_state;
735
736 if (set & TIOCM_RTS)
737 control_state |= TIOCM_RTS;
738 if (set & TIOCM_DTR)
739 control_state |= TIOCM_DTR;
740 if (clear & TIOCM_RTS)
741 control_state &= ~TIOCM_RTS;
742 if (clear & TIOCM_DTR)
743 control_state &= ~TIOCM_DTR;
744
745 priv->control_state = control_state;
746 spin_unlock_irqrestore(&priv->lock, flags);
Greg Kroah-Hartman4a770cc2012-09-14 15:08:30 -0700747 return mct_u232_set_modem_ctrl(port, control_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748}
749
Alan Cox95da3102008-07-22 11:09:07 +0100750static void mct_u232_throttle(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751{
Alan Cox95da3102008-07-22 11:09:07 +0100752 struct usb_serial_port *port = tty->driver_data;
Dave Platt45b844d2007-05-08 11:00:12 -0700753 struct mct_u232_private *priv = usb_get_serial_port_data(port);
Dave Platt45b844d2007-05-08 11:00:12 -0700754 unsigned int control_state;
Dave Platt45b844d2007-05-08 11:00:12 -0700755
Oliver Neukum63832512009-10-07 10:50:23 +0200756 spin_lock_irq(&priv->lock);
Dave Platt45b844d2007-05-08 11:00:12 -0700757 priv->rx_flags |= THROTTLED;
758 if (C_CRTSCTS(tty)) {
Alan Cox95da3102008-07-22 11:09:07 +0100759 priv->control_state &= ~TIOCM_RTS;
760 control_state = priv->control_state;
Oliver Neukum63832512009-10-07 10:50:23 +0200761 spin_unlock_irq(&priv->lock);
Greg Kroah-Hartman4a770cc2012-09-14 15:08:30 -0700762 mct_u232_set_modem_ctrl(port, control_state);
Dave Platt45b844d2007-05-08 11:00:12 -0700763 } else {
Oliver Neukum63832512009-10-07 10:50:23 +0200764 spin_unlock_irq(&priv->lock);
Dave Platt45b844d2007-05-08 11:00:12 -0700765 }
766}
767
Alan Cox95da3102008-07-22 11:09:07 +0100768static void mct_u232_unthrottle(struct tty_struct *tty)
Dave Platt45b844d2007-05-08 11:00:12 -0700769{
Alan Cox95da3102008-07-22 11:09:07 +0100770 struct usb_serial_port *port = tty->driver_data;
Dave Platt45b844d2007-05-08 11:00:12 -0700771 struct mct_u232_private *priv = usb_get_serial_port_data(port);
Dave Platt45b844d2007-05-08 11:00:12 -0700772 unsigned int control_state;
Dave Platt45b844d2007-05-08 11:00:12 -0700773
Oliver Neukum63832512009-10-07 10:50:23 +0200774 spin_lock_irq(&priv->lock);
Dave Platt45b844d2007-05-08 11:00:12 -0700775 if ((priv->rx_flags & THROTTLED) && C_CRTSCTS(tty)) {
Alan Cox95da3102008-07-22 11:09:07 +0100776 priv->rx_flags &= ~THROTTLED;
777 priv->control_state |= TIOCM_RTS;
778 control_state = priv->control_state;
Oliver Neukum63832512009-10-07 10:50:23 +0200779 spin_unlock_irq(&priv->lock);
Greg Kroah-Hartman4a770cc2012-09-14 15:08:30 -0700780 mct_u232_set_modem_ctrl(port, control_state);
Dave Platt45b844d2007-05-08 11:00:12 -0700781 } else {
Oliver Neukum63832512009-10-07 10:50:23 +0200782 spin_unlock_irq(&priv->lock);
Dave Platt45b844d2007-05-08 11:00:12 -0700783 }
784}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Randy Dunlap4acfaf82011-04-03 11:42:00 -0700786static int mct_u232_ioctl(struct tty_struct *tty,
Vadim Tsozik7af75af2011-01-09 01:00:11 -0500787 unsigned int cmd, unsigned long arg)
788{
789 DEFINE_WAIT(wait);
790 struct usb_serial_port *port = tty->driver_data;
791 struct mct_u232_private *mct_u232_port = usb_get_serial_port_data(port);
792 struct async_icount cnow, cprev;
793 unsigned long flags;
794
Greg Kroah-Hartman4a770cc2012-09-14 15:08:30 -0700795 dev_dbg(&port->dev, "%s - cmd = 0x%x\n", __func__, cmd);
Vadim Tsozik7af75af2011-01-09 01:00:11 -0500796
797 switch (cmd) {
798
799 case TIOCMIWAIT:
800
Greg Kroah-Hartman4a770cc2012-09-14 15:08:30 -0700801 dev_dbg(&port->dev, "%s TIOCMIWAIT", __func__);
Vadim Tsozik7af75af2011-01-09 01:00:11 -0500802
803 spin_lock_irqsave(&mct_u232_port->lock, flags);
Johan Hovold468c7402013-03-21 12:37:12 +0100804 cprev = port->icount;
Vadim Tsozik7af75af2011-01-09 01:00:11 -0500805 spin_unlock_irqrestore(&mct_u232_port->lock, flags);
806 for ( ; ; ) {
Johan Hovoldcf1d2442013-03-19 09:21:18 +0100807 prepare_to_wait(&port->delta_msr_wait,
Vadim Tsozik7af75af2011-01-09 01:00:11 -0500808 &wait, TASK_INTERRUPTIBLE);
809 schedule();
Johan Hovoldcf1d2442013-03-19 09:21:18 +0100810 finish_wait(&port->delta_msr_wait, &wait);
Vadim Tsozik7af75af2011-01-09 01:00:11 -0500811 /* see if a signal did it */
812 if (signal_pending(current))
813 return -ERESTARTSYS;
Johan Hovoldcf1d2442013-03-19 09:21:18 +0100814
815 if (port->serial->disconnected)
816 return -EIO;
817
Vadim Tsozik7af75af2011-01-09 01:00:11 -0500818 spin_lock_irqsave(&mct_u232_port->lock, flags);
Johan Hovold468c7402013-03-21 12:37:12 +0100819 cnow = port->icount;
Vadim Tsozik7af75af2011-01-09 01:00:11 -0500820 spin_unlock_irqrestore(&mct_u232_port->lock, flags);
821 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
822 cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
823 return -EIO; /* no change => error */
824 if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
825 ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
826 ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
827 ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
828 return 0;
829 }
830 cprev = cnow;
831 }
832
833 }
834 return -ENOIOCTLCMD;
835}
836
Greg Kroah-Hartman68e24112012-05-08 15:46:14 -0700837module_usb_serial_driver(serial_drivers, id_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
Alan Coxe19b2562008-07-22 11:14:30 +0100839MODULE_AUTHOR(DRIVER_AUTHOR);
840MODULE_DESCRIPTION(DRIVER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841MODULE_LICENSE("GPL");