xref: /linux/drivers/usb/serial/mct_u232.c (revision 08ec212c0f92cbf30e3ecc7349f18151714041d6)
1 /*
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.
22  */
23 
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>
39 #include "mct_u232.h"
40 
41 /*
42  * Version Information
43  */
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"
47 
48 /*
49  * Function prototypes
50  */
51 static int  mct_u232_startup(struct usb_serial *serial);
52 static void mct_u232_release(struct usb_serial *serial);
53 static int  mct_u232_open(struct tty_struct *tty, struct usb_serial_port *port);
54 static void mct_u232_close(struct usb_serial_port *port);
55 static void mct_u232_dtr_rts(struct usb_serial_port *port, int on);
56 static void mct_u232_read_int_callback(struct urb *urb);
57 static void mct_u232_set_termios(struct tty_struct *tty,
58 			struct usb_serial_port *port, struct ktermios *old);
59 static void mct_u232_break_ctl(struct tty_struct *tty, int break_state);
60 static int  mct_u232_tiocmget(struct tty_struct *tty);
61 static int  mct_u232_tiocmset(struct tty_struct *tty,
62 			unsigned int set, unsigned int clear);
63 static int  mct_u232_ioctl(struct tty_struct *tty,
64 			unsigned int cmd, unsigned long arg);
65 static int  mct_u232_get_icount(struct tty_struct *tty,
66 			struct serial_icounter_struct *icount);
67 static void mct_u232_throttle(struct tty_struct *tty);
68 static void mct_u232_unthrottle(struct tty_struct *tty);
69 
70 
71 /*
72  * All of the device info needed for the MCT USB-RS232 converter.
73  */
74 static const struct usb_device_id id_table[] = {
75 	{ 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 };
81 MODULE_DEVICE_TABLE(usb, id_table);
82 
83 static struct usb_serial_driver mct_u232_device = {
84 	.driver = {
85 		.owner =	THIS_MODULE,
86 		.name =		"mct_u232",
87 	},
88 	.description =	     "MCT U232",
89 	.id_table =	     id_table,
90 	.num_ports =	     1,
91 	.open =		     mct_u232_open,
92 	.close =	     mct_u232_close,
93 	.dtr_rts =	     mct_u232_dtr_rts,
94 	.throttle =	     mct_u232_throttle,
95 	.unthrottle =	     mct_u232_unthrottle,
96 	.read_int_callback = mct_u232_read_int_callback,
97 	.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,
102 	.release =	     mct_u232_release,
103 	.ioctl =             mct_u232_ioctl,
104 	.get_icount =        mct_u232_get_icount,
105 };
106 
107 static struct usb_serial_driver * const serial_drivers[] = {
108 	&mct_u232_device, NULL
109 };
110 
111 struct 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 */
117 	unsigned int	     rx_flags;      /* Throttling flags */
118 	struct async_icount  icount;
119 	wait_queue_head_t    msr_wait;	/* for handling sleeping while waiting
120 						for msr change to happen */
121 };
122 
123 #define THROTTLED		0x01
124 
125 /*
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.
134  */
135 static int mct_u232_calculate_baud_rate(struct usb_serial *serial,
136 					speed_t value, speed_t *result)
137 {
138 	*result = value;
139 
140 	if (le16_to_cpu(serial->dev->descriptor.idProduct) == MCT_U232_SITECOM_PID
141 		|| le16_to_cpu(serial->dev->descriptor.idProduct) == MCT_U232_BELKIN_F5U109_PID) {
142 		switch (value) {
143 		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;
163 		default:
164 			*result = 9600;
165 			return 0x08;
166 		}
167 	} else {
168 		/* FIXME: Can we use any divider - should we do
169 		   divider = 115200/value;
170 		   real baud = 115200/divider */
171 		switch (value) {
172 		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:
183 			value = 9600;
184 			*result = 9600;
185 		}
186 		return 115200/value;
187 	}
188 }
189 
190 static int mct_u232_set_baud_rate(struct tty_struct *tty,
191 	struct usb_serial *serial, struct usb_serial_port *port, speed_t value)
192 {
193 	unsigned int divisor;
194 	int rc;
195 	unsigned char *buf;
196 	unsigned char cts_enable_byte = 0;
197 	speed_t speed;
198 
199 	buf = kmalloc(MCT_U232_MAX_SIZE, GFP_KERNEL);
200 	if (buf == NULL)
201 		return -ENOMEM;
202 
203 	divisor = mct_u232_calculate_baud_rate(serial, value, &speed);
204 	put_unaligned_le32(cpu_to_le32(divisor), buf);
205 	rc = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
206 				MCT_U232_SET_BAUD_RATE_REQUEST,
207 				MCT_U232_SET_REQUEST_TYPE,
208 				0, 0, buf, MCT_U232_SET_BAUD_RATE_SIZE,
209 				WDR_TIMEOUT);
210 	if (rc < 0)	/*FIXME: What value speed results */
211 		dev_err(&port->dev, "Set BAUD RATE %d failed (error = %d)\n",
212 			value, rc);
213 	else
214 		tty_encode_baud_rate(tty, speed, speed);
215 	dev_dbg(&port->dev, "set_baud_rate: value: 0x%x, divisor: 0x%x\n", value, divisor);
216 
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
222 	   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
229 	   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'.
232 	*/
233 
234 	buf[0] = 0;
235 	rc = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
236 				MCT_U232_SET_UNKNOWN1_REQUEST,
237 				MCT_U232_SET_REQUEST_TYPE,
238 				0, 0, buf, MCT_U232_SET_UNKNOWN1_SIZE,
239 				WDR_TIMEOUT);
240 	if (rc < 0)
241 		dev_err(&port->dev, "Sending USB device request code %d "
242 			"failed (error = %d)\n", MCT_U232_SET_UNKNOWN1_REQUEST,
243 			rc);
244 
245 	if (port && C_CRTSCTS(tty))
246 	   cts_enable_byte = 1;
247 
248 	dev_dbg(&port->dev, "set_baud_rate: send second control message, data = %02X\n",
249 		cts_enable_byte);
250 	buf[0] = cts_enable_byte;
251 	rc = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
252 			MCT_U232_SET_CTS_REQUEST,
253 			MCT_U232_SET_REQUEST_TYPE,
254 			0, 0, buf, MCT_U232_SET_CTS_SIZE,
255 			WDR_TIMEOUT);
256 	if (rc < 0)
257 		dev_err(&port->dev, "Sending USB device request code %d "
258 			"failed (error = %d)\n", MCT_U232_SET_CTS_REQUEST, rc);
259 
260 	kfree(buf);
261 	return rc;
262 } /* mct_u232_set_baud_rate */
263 
264 static int mct_u232_set_line_ctrl(struct usb_serial_port *port,
265 				  unsigned char lcr)
266 {
267 	int rc;
268 	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;
275 	rc = usb_control_msg(port->serial->dev, usb_sndctrlpipe(port->serial->dev, 0),
276 			MCT_U232_SET_LINE_CTRL_REQUEST,
277 			MCT_U232_SET_REQUEST_TYPE,
278 			0, 0, buf, MCT_U232_SET_LINE_CTRL_SIZE,
279 			WDR_TIMEOUT);
280 	if (rc < 0)
281 		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);
283 	kfree(buf);
284 	return rc;
285 } /* mct_u232_set_line_ctrl */
286 
287 static int mct_u232_set_modem_ctrl(struct usb_serial_port *port,
288 				   unsigned int control_state)
289 {
290 	int rc;
291 	unsigned char mcr;
292 	unsigned char *buf;
293 
294 	buf = kmalloc(MCT_U232_MAX_SIZE, GFP_KERNEL);
295 	if (buf == NULL)
296 		return -ENOMEM;
297 
298 	mcr = MCT_U232_MCR_NONE;
299 	if (control_state & TIOCM_DTR)
300 		mcr |= MCT_U232_MCR_DTR;
301 	if (control_state & TIOCM_RTS)
302 		mcr |= MCT_U232_MCR_RTS;
303 
304 	buf[0] = mcr;
305 	rc = usb_control_msg(port->serial->dev, usb_sndctrlpipe(port->serial->dev, 0),
306 			MCT_U232_SET_MODEM_CTRL_REQUEST,
307 			MCT_U232_SET_REQUEST_TYPE,
308 			0, 0, buf, MCT_U232_SET_MODEM_CTRL_SIZE,
309 			WDR_TIMEOUT);
310 	kfree(buf);
311 
312 	dev_dbg(&port->dev, "set_modem_ctrl: state=0x%x ==> mcr=0x%x\n", control_state, mcr);
313 
314 	if (rc < 0) {
315 		dev_err(&port->dev, "Set MODEM CTRL 0x%x failed (error = %d)\n", mcr, rc);
316 		return rc;
317 	}
318 	return 0;
319 } /* mct_u232_set_modem_ctrl */
320 
321 static int mct_u232_get_modem_stat(struct usb_serial_port *port,
322 				   unsigned char *msr)
323 {
324 	int rc;
325 	unsigned char *buf;
326 
327 	buf = kmalloc(MCT_U232_MAX_SIZE, GFP_KERNEL);
328 	if (buf == NULL) {
329 		*msr = 0;
330 		return -ENOMEM;
331 	}
332 	rc = usb_control_msg(port->serial->dev, usb_rcvctrlpipe(port->serial->dev, 0),
333 			MCT_U232_GET_MODEM_STAT_REQUEST,
334 			MCT_U232_GET_REQUEST_TYPE,
335 			0, 0, buf, MCT_U232_GET_MODEM_STAT_SIZE,
336 			WDR_TIMEOUT);
337 	if (rc < 0) {
338 		dev_err(&port->dev, "Get MODEM STATus failed (error = %d)\n", rc);
339 		*msr = 0;
340 	} else {
341 		*msr = buf[0];
342 	}
343 	dev_dbg(&port->dev, "get_modem_stat: 0x%x\n", *msr);
344 	kfree(buf);
345 	return rc;
346 } /* mct_u232_get_modem_stat */
347 
348 static 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 
362 static void mct_u232_msr_to_state(struct usb_serial_port *port,
363 				  unsigned int *control_state, unsigned char msr)
364 {
365 	/* Translate Control Line states */
366 	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;
382 	dev_dbg(&port->dev, "msr_to_state: msr=0x%x ==> state=0x%x\n", msr, *control_state);
383 } /* mct_u232_msr_to_state */
384 
385 /*
386  * Driver's tty interface functions
387  */
388 
389 static int mct_u232_startup(struct usb_serial *serial)
390 {
391 	struct mct_u232_private *priv;
392 	struct usb_serial_port *port, *rport;
393 
394 	priv = kzalloc(sizeof(struct mct_u232_private), GFP_KERNEL);
395 	if (!priv)
396 		return -ENOMEM;
397 	spin_lock_init(&priv->lock);
398 	init_waitqueue_head(&priv->msr_wait);
399 	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];
406 	/* No unlinking, it wasn't submitted yet. */
407 	usb_free_urb(port->read_urb);
408 	port->read_urb = rport->interrupt_in_urb;
409 	rport->interrupt_in_urb = NULL;
410 	port->read_urb->context = port;
411 
412 	return 0;
413 } /* mct_u232_startup */
414 
415 
416 static void mct_u232_release(struct usb_serial *serial)
417 {
418 	struct mct_u232_private *priv;
419 	int i;
420 
421 	for (i = 0; i < serial->num_ports; ++i) {
422 		/* My special items, the standard routines free my urbs */
423 		priv = usb_get_serial_port_data(serial->port[i]);
424 		kfree(priv);
425 	}
426 } /* mct_u232_release */
427 
428 static int  mct_u232_open(struct tty_struct *tty, struct usb_serial_port *port)
429 {
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 
438 	/* 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 	 */
443 	if (le16_to_cpu(serial->dev->descriptor.idProduct)
444 						== MCT_U232_SITECOM_PID)
445 		port->bulk_out_size = 16;
446 
447 	/* Do a defined restart: the normal serial device seems to
448 	 * 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);
453 	if (tty && (tty->termios.c_cflag & CBAUD))
454 		priv->control_state = TIOCM_DTR | TIOCM_RTS;
455 	else
456 		priv->control_state = 0;
457 
458 	priv->last_lcr = (MCT_U232_DATA_BITS_8 |
459 			  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);
464 	mct_u232_set_modem_ctrl(port, control_state);
465 	mct_u232_set_line_ctrl(port, last_lcr);
466 
467 	/* Read modem status and update control state */
468 	mct_u232_get_modem_stat(port, &last_msr);
469 	spin_lock_irqsave(&priv->lock, flags);
470 	priv->last_msr = last_msr;
471 	mct_u232_msr_to_state(port, &priv->control_state, priv->last_msr);
472 	spin_unlock_irqrestore(&priv->lock, flags);
473 
474 	retval = usb_submit_urb(port->read_urb, GFP_KERNEL);
475 	if (retval) {
476 		dev_err(&port->dev,
477 			"usb_submit_urb(read bulk) failed pipe 0x%x err %d\n",
478 			port->read_urb->pipe, retval);
479 		goto error;
480 	}
481 
482 	retval = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
483 	if (retval) {
484 		usb_kill_urb(port->read_urb);
485 		dev_err(&port->dev,
486 			"usb_submit_urb(read int) failed pipe 0x%x err %d",
487 			port->interrupt_in_urb->pipe, retval);
488 		goto error;
489 	}
490 	return 0;
491 
492 error:
493 	return retval;
494 } /* mct_u232_open */
495 
496 static void mct_u232_dtr_rts(struct usb_serial_port *port, int on)
497 {
498 	unsigned int control_state;
499 	struct mct_u232_private *priv = usb_get_serial_port_data(port);
500 
501 	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
508 			priv->control_state &= ~(TIOCM_DTR | TIOCM_RTS);
509 		control_state = priv->control_state;
510 		spin_unlock_irq(&priv->lock);
511 		mct_u232_set_modem_ctrl(port, control_state);
512 	}
513 	mutex_unlock(&port->serial->disc_mutex);
514 }
515 
516 static void mct_u232_close(struct usb_serial_port *port)
517 {
518 	if (port->serial->dev) {
519 		/* shutdown our urbs */
520 		usb_kill_urb(port->write_urb);
521 		usb_kill_urb(port->read_urb);
522 		usb_kill_urb(port->interrupt_in_urb);
523 	}
524 } /* mct_u232_close */
525 
526 
527 static void mct_u232_read_int_callback(struct urb *urb)
528 {
529 	struct usb_serial_port *port = urb->context;
530 	struct mct_u232_private *priv = usb_get_serial_port_data(port);
531 	struct tty_struct *tty;
532 	unsigned char *data = urb->transfer_buffer;
533 	int retval;
534 	int status = urb->status;
535 	unsigned long flags;
536 
537 	switch (status) {
538 	case 0:
539 		/* success */
540 		break;
541 	case -ECONNRESET:
542 	case -ENOENT:
543 	case -ESHUTDOWN:
544 		/* this urb is terminated, clean up */
545 		dev_dbg(&port->dev, "%s - urb shutting down with status: %d\n",
546 			__func__, status);
547 		return;
548 	default:
549 		dev_dbg(&port->dev, "%s - nonzero urb status received: %d\n",
550 			__func__, status);
551 		goto exit;
552 	}
553 
554 	usb_serial_debug_data(&port->dev, __func__, urb->actual_length, data);
555 
556 	/*
557 	 * Work-a-round: handle the 'usual' bulk-in pipe here
558 	 */
559 	if (urb->transfer_buffer_length > 2) {
560 		if (urb->actual_length) {
561 			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 			}
567 			tty_kref_put(tty);
568 		}
569 		goto exit;
570 	}
571 
572 	/*
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];
578 
579 	/* Record Control Line states */
580 	mct_u232_msr_to_state(port, &priv->control_state, priv->last_msr);
581 
582 	mct_u232_msr_to_icount(&priv->icount, priv->last_msr);
583 
584 #if 0
585 	/* Not yet handled. See belkin_sa.c for further information */
586 	/* 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) {
594 		tty = tty_port_tty_get(&port->port);
595 		/* 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 		}
607 		tty_kref_put(tty);
608 	}
609 #endif
610 	wake_up_interruptible(&priv->msr_wait);
611 	spin_unlock_irqrestore(&priv->lock, flags);
612 exit:
613 	retval = usb_submit_urb(urb, GFP_ATOMIC);
614 	if (retval)
615 		dev_err(&port->dev,
616 			"%s - usb_submit_urb failed with result %d\n",
617 			__func__, retval);
618 } /* mct_u232_read_int_callback */
619 
620 static void mct_u232_set_termios(struct tty_struct *tty,
621 				 struct usb_serial_port *port,
622 				 struct ktermios *old_termios)
623 {
624 	struct usb_serial *serial = port->serial;
625 	struct mct_u232_private *priv = usb_get_serial_port_data(port);
626 	struct ktermios *termios = &tty->termios;
627 	unsigned int cflag = termios->c_cflag;
628 	unsigned int old_cflag = old_termios->c_cflag;
629 	unsigned long flags;
630 	unsigned int control_state;
631 	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 
646 	/* reassert DTR and RTS on transition from B0 */
647 	if ((old_cflag & CBAUD) == B0) {
648 		dev_dbg(&port->dev, "%s: baud was B0\n", __func__);
649 		control_state |= TIOCM_DTR | TIOCM_RTS;
650 		mct_u232_set_modem_ctrl(port, control_state);
651 	}
652 
653 	mct_u232_set_baud_rate(tty, serial, port, tty_get_baud_rate(tty));
654 
655 	if ((cflag & CBAUD) == B0) {
656 		dev_dbg(&port->dev, "%s: baud is B0\n", __func__);
657 		/* Drop RTS and DTR */
658 		control_state &= ~(TIOCM_DTR | TIOCM_RTS);
659 		mct_u232_set_modem_ctrl(port, control_state);
660 	}
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:
684 		dev_err(&port->dev,
685 			"CSIZE was not CS5-CS8, using default of 8\n");
686 		last_lcr |= MCT_U232_DATA_BITS_8;
687 		break;
688 	}
689 
690 	termios->c_cflag &= ~CMSPAR;
691 
692 	/* set the number of stop bits */
693 	last_lcr |= (cflag & CSTOPB) ?
694 		MCT_U232_STOP_BITS_2 : MCT_U232_STOP_BITS_1;
695 
696 	mct_u232_set_line_ctrl(port, last_lcr);
697 
698 	/* 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 
705 static void mct_u232_break_ctl(struct tty_struct *tty, int break_state)
706 {
707 	struct usb_serial_port *port = tty->driver_data;
708 	struct mct_u232_private *priv = usb_get_serial_port_data(port);
709 	unsigned char lcr;
710 	unsigned long flags;
711 
712 	spin_lock_irqsave(&priv->lock, flags);
713 	lcr = priv->last_lcr;
714 
715 	if (break_state)
716 		lcr |= MCT_U232_SET_BREAK;
717 	spin_unlock_irqrestore(&priv->lock, flags);
718 
719 	mct_u232_set_line_ctrl(port, lcr);
720 } /* mct_u232_break_ctl */
721 
722 
723 static int mct_u232_tiocmget(struct tty_struct *tty)
724 {
725 	struct usb_serial_port *port = tty->driver_data;
726 	struct mct_u232_private *priv = usb_get_serial_port_data(port);
727 	unsigned int control_state;
728 	unsigned long flags;
729 
730 	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 
737 static int mct_u232_tiocmset(struct tty_struct *tty,
738 			      unsigned int set, unsigned int clear)
739 {
740 	struct usb_serial_port *port = tty->driver_data;
741 	struct mct_u232_private *priv = usb_get_serial_port_data(port);
742 	unsigned int control_state;
743 	unsigned long flags;
744 
745 	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);
759 	return mct_u232_set_modem_ctrl(port, control_state);
760 }
761 
762 static void mct_u232_throttle(struct tty_struct *tty)
763 {
764 	struct usb_serial_port *port = tty->driver_data;
765 	struct mct_u232_private *priv = usb_get_serial_port_data(port);
766 	unsigned int control_state;
767 
768 	spin_lock_irq(&priv->lock);
769 	priv->rx_flags |= THROTTLED;
770 	if (C_CRTSCTS(tty)) {
771 		priv->control_state &= ~TIOCM_RTS;
772 		control_state = priv->control_state;
773 		spin_unlock_irq(&priv->lock);
774 		mct_u232_set_modem_ctrl(port, control_state);
775 	} else {
776 		spin_unlock_irq(&priv->lock);
777 	}
778 }
779 
780 static void mct_u232_unthrottle(struct tty_struct *tty)
781 {
782 	struct usb_serial_port *port = tty->driver_data;
783 	struct mct_u232_private *priv = usb_get_serial_port_data(port);
784 	unsigned int control_state;
785 
786 	spin_lock_irq(&priv->lock);
787 	if ((priv->rx_flags & THROTTLED) && C_CRTSCTS(tty)) {
788 		priv->rx_flags &= ~THROTTLED;
789 		priv->control_state |= TIOCM_RTS;
790 		control_state = priv->control_state;
791 		spin_unlock_irq(&priv->lock);
792 		mct_u232_set_modem_ctrl(port, control_state);
793 	} else {
794 		spin_unlock_irq(&priv->lock);
795 	}
796 }
797 
798 static int  mct_u232_ioctl(struct tty_struct *tty,
799 			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 
807 	dev_dbg(&port->dev, "%s - cmd = 0x%x\n", __func__, cmd);
808 
809 	switch (cmd) {
810 
811 	case TIOCMIWAIT:
812 
813 		dev_dbg(&port->dev, "%s TIOCMIWAIT", __func__);
814 
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 
845 static 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 
869 	dev_dbg(&port->dev, "%s TIOCGICOUNT RX=%d, TX=%d\n",
870 		__func__,  icount->rx, icount->tx);
871 	return 0;
872 }
873 
874 module_usb_serial_driver(serial_drivers, id_table);
875 
876 MODULE_AUTHOR(DRIVER_AUTHOR);
877 MODULE_DESCRIPTION(DRIVER_DESC);
878 MODULE_LICENSE("GPL");
879