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 * TEST STATUS:
24 *   Basic tests have been performed with minicom/zmodem transfers and
25 *   modem dialing under Linux 2.4.0-test10 (for me it works fine).
26 *
27 * 04-Nov-2003 Bill Marr <marr at flex dot com>
28 *   - Mimic Windows driver by sending 2 USB 'device request' messages
29 *     following normal 'baud rate change' message.  This allows data to be
30 *     transmitted to RS-232 devices which don't assert the 'CTS' signal.
31 *
32 * 10-Nov-2001 Wolfgang Grandegger
33 *   - Fixed an endianess problem with the baudrate selection for PowerPC.
34 *
35 * 06-Dec-2001 Martin Hamilton <martinh@gnu.org>
36 *	Added support for the Belkin F5U109 DB9 adaptor
37 *
38 * 30-May-2001 Greg Kroah-Hartman
39 *	switched from using spinlock to a semaphore, which fixes lots of problems.
40 *
41 * 04-May-2001 Stelian Pop
42 *   - Set the maximum bulk output size for Sitecom U232-P25 model to 16 bytes
43 *     instead of the device reported 32 (using 32 bytes causes many data
44 *     loss, Windows driver uses 16 too).
45 *
46 * 02-May-2001 Stelian Pop
47 *   - Fixed the baud calculation for Sitecom U232-P25 model
48 *
49 * 08-Apr-2001 gb
50 *   - Identify version on module load.
51 *
52 * 06-Jan-2001 Cornel Ciocirlan
53 *   - Added support for Sitecom U232-P25 model (Product Id 0x0230)
54 *   - Added support for D-Link DU-H3SP USB BAY (Product Id 0x0200)
55 *
56 * 29-Nov-2000 Greg Kroah-Hartman
57 *   - Added device id table to fit with 2.4.0-test11 structure.
58 *   - took out DEAL_WITH_TWO_INT_IN_ENDPOINTS #define as it's not needed
59 *     (lots of things will change if/when the usb-serial core changes to
60 *     handle these issues.
61 *
62 * 27-Nov-2000 Wolfgang Grandegger
63 *   A version for kernel 2.4.0-test10 released to the Linux community
64 *   (via linux-usb-devel).
65 */
66
67#include <linux/kernel.h>
68#include <linux/errno.h>
69#include <linux/init.h>
70#include <linux/slab.h>
71#include <linux/tty.h>
72#include <linux/tty_driver.h>
73#include <linux/tty_flip.h>
74#include <linux/module.h>
75#include <linux/spinlock.h>
76#include <asm/uaccess.h>
77#include <linux/usb.h>
78#include <linux/usb/serial.h>
79#include "mct_u232.h"
80
81/*
82 * Version Information
83 */
84#define DRIVER_VERSION "z2.0"		/* Linux in-kernel version */
85#define DRIVER_AUTHOR "Wolfgang Grandegger <wolfgang@ces.ch>"
86#define DRIVER_DESC "Magic Control Technology USB-RS232 converter driver"
87
88static int debug;
89
90/*
91 * Function prototypes
92 */
93static int  mct_u232_startup	         (struct usb_serial *serial);
94static void mct_u232_shutdown	         (struct usb_serial *serial);
95static int  mct_u232_open	         (struct usb_serial_port *port,
96					  struct file *filp);
97static void mct_u232_close	         (struct usb_serial_port *port,
98					  struct file *filp);
99static void mct_u232_read_int_callback   (struct urb *urb);
100static void mct_u232_set_termios         (struct usb_serial_port *port,
101					  struct ktermios * old);
102static int  mct_u232_ioctl	         (struct usb_serial_port *port,
103					  struct file * file,
104					  unsigned int cmd,
105					  unsigned long arg);
106static void mct_u232_break_ctl	         (struct usb_serial_port *port,
107					  int break_state );
108static int  mct_u232_tiocmget		 (struct usb_serial_port *port,
109					  struct file *file);
110static int  mct_u232_tiocmset		 (struct usb_serial_port *port,
111					  struct file *file, unsigned int set,
112					  unsigned int clear);
113/*
114 * All of the device info needed for the MCT USB-RS232 converter.
115 */
116static struct usb_device_id id_table_combined [] = {
117	{ USB_DEVICE(MCT_U232_VID, MCT_U232_PID) },
118	{ USB_DEVICE(MCT_U232_VID, MCT_U232_SITECOM_PID) },
119	{ USB_DEVICE(MCT_U232_VID, MCT_U232_DU_H3SP_PID) },
120	{ USB_DEVICE(MCT_U232_BELKIN_F5U109_VID, MCT_U232_BELKIN_F5U109_PID) },
121	{ }		/* Terminating entry */
122};
123
124MODULE_DEVICE_TABLE (usb, id_table_combined);
125
126static struct usb_driver mct_u232_driver = {
127	.name =		"mct_u232",
128	.probe =	usb_serial_probe,
129	.disconnect =	usb_serial_disconnect,
130	.id_table =	id_table_combined,
131	.no_dynamic_id = 	1,
132};
133
134static struct usb_serial_driver mct_u232_device = {
135	.driver = {
136		.owner =	THIS_MODULE,
137		.name =		"mct_u232",
138	},
139	.description =	     "MCT U232",
140	.usb_driver = 	     &mct_u232_driver,
141	.id_table =	     id_table_combined,
142	.num_interrupt_in =  2,
143	.num_bulk_in =	     0,
144	.num_bulk_out =	     1,
145	.num_ports =	     1,
146	.open =		     mct_u232_open,
147	.close =	     mct_u232_close,
148	.read_int_callback = mct_u232_read_int_callback,
149	.ioctl =	     mct_u232_ioctl,
150	.set_termios =	     mct_u232_set_termios,
151	.break_ctl =	     mct_u232_break_ctl,
152	.tiocmget =	     mct_u232_tiocmget,
153	.tiocmset =	     mct_u232_tiocmset,
154	.attach =	     mct_u232_startup,
155	.shutdown =	     mct_u232_shutdown,
156};
157
158
159struct mct_u232_private {
160	spinlock_t lock;
161	unsigned int	     control_state; /* Modem Line Setting (TIOCM) */
162	unsigned char        last_lcr;      /* Line Control Register */
163	unsigned char	     last_lsr;      /* Line Status Register */
164	unsigned char	     last_msr;      /* Modem Status Register */
165};
166
167/*
168 * Handle vendor specific USB requests
169 */
170
171#define WDR_TIMEOUT 5000 /* default urb timeout */
172
173static int mct_u232_calculate_baud_rate(struct usb_serial *serial, int value)
174{
175	if (le16_to_cpu(serial->dev->descriptor.idProduct) == MCT_U232_SITECOM_PID
176	  || le16_to_cpu(serial->dev->descriptor.idProduct) == MCT_U232_BELKIN_F5U109_PID) {
177		switch (value) {
178		case    B300: return 0x01;
179		case    B600: return 0x02; /* this one not tested */
180		case   B1200: return 0x03;
181		case   B2400: return 0x04;
182		case   B4800: return 0x06;
183		case   B9600: return 0x08;
184		case  B19200: return 0x09;
185		case  B38400: return 0x0a;
186		case  B57600: return 0x0b;
187		case B115200: return 0x0c;
188		default:
189			err("MCT USB-RS232: unsupported baudrate request 0x%x,"
190			    " using default of B9600", value);
191			return 0x08;
192		}
193	} else {
194		switch (value) {
195		case    B300: value =     300; break;
196		case    B600: value =     600; break;
197		case   B1200: value =    1200; break;
198		case   B2400: value =    2400; break;
199		case   B4800: value =    4800; break;
200		case   B9600: value =    9600; break;
201		case  B19200: value =   19200; break;
202		case  B38400: value =   38400; break;
203		case  B57600: value =   57600; break;
204		case B115200: value =  115200; break;
205		default:
206			err("MCT USB-RS232: unsupported baudrate request 0x%x,"
207			    " using default of B9600", value);
208			value = 9600;
209		}
210		return 115200/value;
211	}
212}
213
214static int mct_u232_set_baud_rate(struct usb_serial *serial, int value)
215{
216	__le32 divisor;
217        int rc;
218        unsigned char zero_byte = 0;
219
220	divisor = cpu_to_le32(mct_u232_calculate_baud_rate(serial, value));
221
222        rc = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
223                             MCT_U232_SET_BAUD_RATE_REQUEST,
224			     MCT_U232_SET_REQUEST_TYPE,
225                             0, 0, &divisor, MCT_U232_SET_BAUD_RATE_SIZE,
226			     WDR_TIMEOUT);
227	if (rc < 0)
228		err("Set BAUD RATE %d failed (error = %d)", value, rc);
229	dbg("set_baud_rate: value: 0x%x, divisor: 0x%x", value, divisor);
230
231	/* Mimic the MCT-supplied Windows driver (version 1.21P.0104), which
232	   always sends two extra USB 'device request' messages after the
233	   'baud rate change' message.  The actual functionality of the
234	   request codes in these messages is not fully understood but these
235	   particular codes are never seen in any operation besides a baud
236	   rate change.  Both of these messages send a single byte of data
237	   whose value is always zero.  The second of these two extra messages
238	   is required in order for data to be properly written to an RS-232
239	   device which does not assert the 'CTS' signal. */
240
241	rc = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
242			     MCT_U232_SET_UNKNOWN1_REQUEST,
243			     MCT_U232_SET_REQUEST_TYPE,
244			     0, 0, &zero_byte, MCT_U232_SET_UNKNOWN1_SIZE,
245			     WDR_TIMEOUT);
246	if (rc < 0)
247		err("Sending USB device request code %d failed (error = %d)",
248		    MCT_U232_SET_UNKNOWN1_REQUEST, rc);
249
250	rc = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
251			     MCT_U232_SET_UNKNOWN2_REQUEST,
252			     MCT_U232_SET_REQUEST_TYPE,
253			     0, 0, &zero_byte, MCT_U232_SET_UNKNOWN2_SIZE,
254			     WDR_TIMEOUT);
255	if (rc < 0)
256		err("Sending USB device request code %d failed (error = %d)",
257		    MCT_U232_SET_UNKNOWN2_REQUEST, rc);
258
259        return rc;
260} /* mct_u232_set_baud_rate */
261
262static int mct_u232_set_line_ctrl(struct usb_serial *serial, unsigned char lcr)
263{
264        int rc;
265        rc = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
266                             MCT_U232_SET_LINE_CTRL_REQUEST,
267			     MCT_U232_SET_REQUEST_TYPE,
268                             0, 0, &lcr, MCT_U232_SET_LINE_CTRL_SIZE,
269			     WDR_TIMEOUT);
270	if (rc < 0)
271		err("Set LINE CTRL 0x%x failed (error = %d)", lcr, rc);
272	dbg("set_line_ctrl: 0x%x", lcr);
273        return rc;
274} /* mct_u232_set_line_ctrl */
275
276static int mct_u232_set_modem_ctrl(struct usb_serial *serial,
277				   unsigned int control_state)
278{
279        int rc;
280	unsigned char mcr = MCT_U232_MCR_NONE;
281
282	if (control_state & TIOCM_DTR)
283		mcr |= MCT_U232_MCR_DTR;
284	if (control_state & TIOCM_RTS)
285		mcr |= MCT_U232_MCR_RTS;
286
287        rc = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
288                             MCT_U232_SET_MODEM_CTRL_REQUEST,
289			     MCT_U232_SET_REQUEST_TYPE,
290                             0, 0, &mcr, MCT_U232_SET_MODEM_CTRL_SIZE,
291			     WDR_TIMEOUT);
292	if (rc < 0)
293		err("Set MODEM CTRL 0x%x failed (error = %d)", mcr, rc);
294	dbg("set_modem_ctrl: state=0x%x ==> mcr=0x%x", control_state, mcr);
295
296        return rc;
297} /* mct_u232_set_modem_ctrl */
298
299static int mct_u232_get_modem_stat(struct usb_serial *serial, unsigned char *msr)
300{
301        int rc;
302        rc = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
303                             MCT_U232_GET_MODEM_STAT_REQUEST,
304			     MCT_U232_GET_REQUEST_TYPE,
305                             0, 0, msr, MCT_U232_GET_MODEM_STAT_SIZE,
306			     WDR_TIMEOUT);
307	if (rc < 0) {
308		err("Get MODEM STATus failed (error = %d)", rc);
309		*msr = 0;
310	}
311	dbg("get_modem_stat: 0x%x", *msr);
312        return rc;
313} /* mct_u232_get_modem_stat */
314
315static void mct_u232_msr_to_state(unsigned int *control_state, unsigned char msr)
316{
317 	/* Translate Control Line states */
318	if (msr & MCT_U232_MSR_DSR)
319		*control_state |=  TIOCM_DSR;
320	else
321		*control_state &= ~TIOCM_DSR;
322	if (msr & MCT_U232_MSR_CTS)
323		*control_state |=  TIOCM_CTS;
324	else
325		*control_state &= ~TIOCM_CTS;
326	if (msr & MCT_U232_MSR_RI)
327		*control_state |=  TIOCM_RI;
328	else
329		*control_state &= ~TIOCM_RI;
330	if (msr & MCT_U232_MSR_CD)
331		*control_state |=  TIOCM_CD;
332	else
333		*control_state &= ~TIOCM_CD;
334 	dbg("msr_to_state: msr=0x%x ==> state=0x%x", msr, *control_state);
335} /* mct_u232_msr_to_state */
336
337/*
338 * Driver's tty interface functions
339 */
340
341static int mct_u232_startup (struct usb_serial *serial)
342{
343	struct mct_u232_private *priv;
344	struct usb_serial_port *port, *rport;
345
346	priv = kzalloc(sizeof(struct mct_u232_private), GFP_KERNEL);
347	if (!priv)
348		return -ENOMEM;
349	spin_lock_init(&priv->lock);
350	usb_set_serial_port_data(serial->port[0], priv);
351
352	init_waitqueue_head(&serial->port[0]->write_wait);
353
354	/* Puh, that's dirty */
355	port = serial->port[0];
356	rport = serial->port[1];
357	/* No unlinking, it wasn't submitted yet. */
358	usb_free_urb(port->read_urb);
359	port->read_urb = rport->interrupt_in_urb;
360	rport->interrupt_in_urb = NULL;
361	port->read_urb->context = port;
362
363	return (0);
364} /* mct_u232_startup */
365
366
367static void mct_u232_shutdown (struct usb_serial *serial)
368{
369	struct mct_u232_private *priv;
370	int i;
371
372	dbg("%s", __FUNCTION__);
373
374	for (i=0; i < serial->num_ports; ++i) {
375		/* My special items, the standard routines free my urbs */
376		priv = usb_get_serial_port_data(serial->port[i]);
377		if (priv) {
378			usb_set_serial_port_data(serial->port[i], NULL);
379			kfree(priv);
380		}
381	}
382} /* mct_u232_shutdown */
383
384static int  mct_u232_open (struct usb_serial_port *port, struct file *filp)
385{
386	struct usb_serial *serial = port->serial;
387	struct mct_u232_private *priv = usb_get_serial_port_data(port);
388	int retval = 0;
389	unsigned int control_state;
390	unsigned long flags;
391	unsigned char last_lcr;
392	unsigned char last_msr;
393
394	dbg("%s port %d", __FUNCTION__, port->number);
395
396	/* Compensate for a hardware bug: although the Sitecom U232-P25
397	 * device reports a maximum output packet size of 32 bytes,
398	 * it seems to be able to accept only 16 bytes (and that's what
399	 * SniffUSB says too...)
400	 */
401	if (le16_to_cpu(serial->dev->descriptor.idProduct) == MCT_U232_SITECOM_PID)
402		port->bulk_out_size = 16;
403
404	/* Do a defined restart: the normal serial device seems to
405	 * always turn on DTR and RTS here, so do the same. I'm not
406	 * sure if this is really necessary. But it should not harm
407	 * either.
408	 */
409	spin_lock_irqsave(&priv->lock, flags);
410	if (port->tty->termios->c_cflag & CBAUD)
411		priv->control_state = TIOCM_DTR | TIOCM_RTS;
412	else
413		priv->control_state = 0;
414
415	priv->last_lcr = (MCT_U232_DATA_BITS_8 |
416			  MCT_U232_PARITY_NONE |
417			  MCT_U232_STOP_BITS_1);
418	control_state = priv->control_state;
419	last_lcr = priv->last_lcr;
420	spin_unlock_irqrestore(&priv->lock, flags);
421	mct_u232_set_modem_ctrl(serial, control_state);
422	mct_u232_set_line_ctrl(serial, last_lcr);
423
424	/* Read modem status and update control state */
425	mct_u232_get_modem_stat(serial, &last_msr);
426	spin_lock_irqsave(&priv->lock, flags);
427	priv->last_msr = last_msr;
428	mct_u232_msr_to_state(&priv->control_state, priv->last_msr);
429	spin_unlock_irqrestore(&priv->lock, flags);
430
431	port->read_urb->dev = port->serial->dev;
432	retval = usb_submit_urb(port->read_urb, GFP_KERNEL);
433	if (retval) {
434		err("usb_submit_urb(read bulk) failed pipe 0x%x err %d",
435		    port->read_urb->pipe, retval);
436		goto error;
437	}
438
439	port->interrupt_in_urb->dev = port->serial->dev;
440	retval = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
441	if (retval) {
442		usb_kill_urb(port->read_urb);
443		err(" usb_submit_urb(read int) failed pipe 0x%x err %d",
444		    port->interrupt_in_urb->pipe, retval);
445		goto error;
446	}
447	return 0;
448
449error:
450	return retval;
451} /* mct_u232_open */
452
453
454static void mct_u232_close (struct usb_serial_port *port, struct file *filp)
455{
456	dbg("%s port %d", __FUNCTION__, port->number);
457
458	if (port->serial->dev) {
459		/* shutdown our urbs */
460		usb_kill_urb(port->write_urb);
461		usb_kill_urb(port->read_urb);
462		usb_kill_urb(port->interrupt_in_urb);
463	}
464} /* mct_u232_close */
465
466
467static void mct_u232_read_int_callback (struct urb *urb)
468{
469	struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
470	struct mct_u232_private *priv = usb_get_serial_port_data(port);
471	struct usb_serial *serial = port->serial;
472	struct tty_struct *tty;
473	unsigned char *data = urb->transfer_buffer;
474	int status;
475	unsigned long flags;
476
477	switch (urb->status) {
478	case 0:
479		/* success */
480		break;
481	case -ECONNRESET:
482	case -ENOENT:
483	case -ESHUTDOWN:
484		/* this urb is terminated, clean up */
485		dbg("%s - urb shutting down with status: %d", __FUNCTION__, urb->status);
486		return;
487	default:
488		dbg("%s - nonzero urb status received: %d", __FUNCTION__, urb->status);
489		goto exit;
490	}
491
492	if (!serial) {
493		dbg("%s - bad serial pointer, exiting", __FUNCTION__);
494		return;
495	}
496
497        dbg("%s - port %d", __FUNCTION__, port->number);
498	usb_serial_debug_data(debug, &port->dev, __FUNCTION__, urb->actual_length, data);
499
500	/*
501	 * Work-a-round: handle the 'usual' bulk-in pipe here
502	 */
503	if (urb->transfer_buffer_length > 2) {
504		int i;
505		tty = port->tty;
506		if (urb->actual_length) {
507			for (i = 0; i < urb->actual_length ; ++i) {
508				tty_insert_flip_char(tty, data[i], 0);
509			}
510			tty_flip_buffer_push(tty);
511		}
512		goto exit;
513	}
514
515	/*
516	 * The interrupt-in pipe signals exceptional conditions (modem line
517	 * signal changes and errors). data[0] holds MSR, data[1] holds LSR.
518	 */
519	spin_lock_irqsave(&priv->lock, flags);
520	priv->last_msr = data[MCT_U232_MSR_INDEX];
521
522	/* Record Control Line states */
523	mct_u232_msr_to_state(&priv->control_state, priv->last_msr);
524
525	spin_unlock_irqrestore(&priv->lock, flags);
526exit:
527	status = usb_submit_urb (urb, GFP_ATOMIC);
528	if (status)
529		err ("%s - usb_submit_urb failed with result %d",
530		     __FUNCTION__, status);
531} /* mct_u232_read_int_callback */
532
533static void mct_u232_set_termios (struct usb_serial_port *port,
534				  struct ktermios *old_termios)
535{
536	struct usb_serial *serial = port->serial;
537	struct mct_u232_private *priv = usb_get_serial_port_data(port);
538	unsigned int iflag = port->tty->termios->c_iflag;
539	unsigned int cflag = port->tty->termios->c_cflag;
540	unsigned int old_cflag = old_termios->c_cflag;
541	unsigned long flags;
542	unsigned int control_state, new_state;
543	unsigned char last_lcr;
544
545	/* get a local copy of the current port settings */
546	spin_lock_irqsave(&priv->lock, flags);
547	control_state = priv->control_state;
548	spin_unlock_irqrestore(&priv->lock, flags);
549	last_lcr = 0;
550
551	/*
552	 * Update baud rate.
553	 * Do not attempt to cache old rates and skip settings,
554	 * disconnects screw such tricks up completely.
555	 * Premature optimization is the root of all evil.
556	 */
557
558        /* reassert DTR and (maybe) RTS on transition from B0 */
559	if ((old_cflag & CBAUD) == B0) {
560		dbg("%s: baud was B0", __FUNCTION__);
561		control_state |= TIOCM_DTR;
562		/* don't set RTS if using hardware flow control */
563		if (!(old_cflag & CRTSCTS)) {
564			control_state |= TIOCM_RTS;
565		}
566		mct_u232_set_modem_ctrl(serial, control_state);
567	}
568
569	mct_u232_set_baud_rate(serial, cflag & CBAUD);
570
571	if ((cflag & CBAUD) == B0 ) {
572		dbg("%s: baud is B0", __FUNCTION__);
573		/* Drop RTS and DTR */
574		control_state &= ~(TIOCM_DTR | TIOCM_RTS);
575       		mct_u232_set_modem_ctrl(serial, control_state);
576	}
577
578	/*
579	 * Update line control register (LCR)
580	 */
581
582	/* set the parity */
583	if (cflag & PARENB)
584		last_lcr |= (cflag & PARODD) ?
585			MCT_U232_PARITY_ODD : MCT_U232_PARITY_EVEN;
586	else
587		last_lcr |= MCT_U232_PARITY_NONE;
588
589	/* set the number of data bits */
590	switch (cflag & CSIZE) {
591	case CS5:
592		last_lcr |= MCT_U232_DATA_BITS_5; break;
593	case CS6:
594		last_lcr |= MCT_U232_DATA_BITS_6; break;
595	case CS7:
596		last_lcr |= MCT_U232_DATA_BITS_7; break;
597	case CS8:
598		last_lcr |= MCT_U232_DATA_BITS_8; break;
599	default:
600		err("CSIZE was not CS5-CS8, using default of 8");
601		last_lcr |= MCT_U232_DATA_BITS_8;
602		break;
603	}
604
605	/* set the number of stop bits */
606	last_lcr |= (cflag & CSTOPB) ?
607		MCT_U232_STOP_BITS_2 : MCT_U232_STOP_BITS_1;
608
609	mct_u232_set_line_ctrl(serial, last_lcr);
610
611	/*
612	 * Set flow control: well, I do not really now how to handle DTR/RTS.
613	 * Just do what we have seen with SniffUSB on Win98.
614	 */
615	/* Drop DTR/RTS if no flow control otherwise assert */
616	new_state = control_state;
617	if ((iflag & IXOFF) || (iflag & IXON) || (cflag & CRTSCTS))
618		new_state |= TIOCM_DTR | TIOCM_RTS;
619	else
620		new_state &= ~(TIOCM_DTR | TIOCM_RTS);
621	if (new_state != control_state) {
622		mct_u232_set_modem_ctrl(serial, new_state);
623		control_state = new_state;
624	}
625
626	/* save off the modified port settings */
627	spin_lock_irqsave(&priv->lock, flags);
628	priv->control_state = control_state;
629	priv->last_lcr = last_lcr;
630	spin_unlock_irqrestore(&priv->lock, flags);
631} /* mct_u232_set_termios */
632
633static void mct_u232_break_ctl( struct usb_serial_port *port, int break_state )
634{
635	struct usb_serial *serial = port->serial;
636	struct mct_u232_private *priv = usb_get_serial_port_data(port);
637	unsigned char lcr;
638	unsigned long flags;
639
640	dbg("%sstate=%d", __FUNCTION__, break_state);
641
642	spin_lock_irqsave(&priv->lock, flags);
643	lcr = priv->last_lcr;
644	spin_unlock_irqrestore(&priv->lock, flags);
645
646	if (break_state)
647		lcr |= MCT_U232_SET_BREAK;
648
649	mct_u232_set_line_ctrl(serial, lcr);
650} /* mct_u232_break_ctl */
651
652
653static int mct_u232_tiocmget (struct usb_serial_port *port, struct file *file)
654{
655	struct mct_u232_private *priv = usb_get_serial_port_data(port);
656	unsigned int control_state;
657	unsigned long flags;
658
659	dbg("%s", __FUNCTION__);
660
661	spin_lock_irqsave(&priv->lock, flags);
662	control_state = priv->control_state;
663	spin_unlock_irqrestore(&priv->lock, flags);
664
665	return control_state;
666}
667
668static int mct_u232_tiocmset (struct usb_serial_port *port, struct file *file,
669			      unsigned int set, unsigned int clear)
670{
671	struct usb_serial *serial = port->serial;
672	struct mct_u232_private *priv = usb_get_serial_port_data(port);
673	unsigned int control_state;
674	unsigned long flags;
675
676	dbg("%s", __FUNCTION__);
677
678	spin_lock_irqsave(&priv->lock, flags);
679	control_state = priv->control_state;
680
681	if (set & TIOCM_RTS)
682		control_state |= TIOCM_RTS;
683	if (set & TIOCM_DTR)
684		control_state |= TIOCM_DTR;
685	if (clear & TIOCM_RTS)
686		control_state &= ~TIOCM_RTS;
687	if (clear & TIOCM_DTR)
688		control_state &= ~TIOCM_DTR;
689
690	priv->control_state = control_state;
691	spin_unlock_irqrestore(&priv->lock, flags);
692	return mct_u232_set_modem_ctrl(serial, control_state);
693}
694
695static int mct_u232_ioctl (struct usb_serial_port *port, struct file * file,
696			   unsigned int cmd, unsigned long arg)
697{
698	dbg("%scmd=0x%x", __FUNCTION__, cmd);
699
700	/* Based on code from acm.c and others */
701	switch (cmd) {
702	case TIOCMIWAIT:
703		/* wait for any of the 4 modem inputs (DCD,RI,DSR,CTS)*/
704		/* TODO */
705		return( 0 );
706
707	case TIOCGICOUNT:
708		/* return count of modemline transitions */
709		/* TODO */
710		return 0;
711
712	default:
713		dbg("%s: arg not supported - 0x%04x", __FUNCTION__,cmd);
714		return(-ENOIOCTLCMD);
715		break;
716	}
717	return 0;
718} /* mct_u232_ioctl */
719
720
721static int __init mct_u232_init (void)
722{
723	int retval;
724	retval = usb_serial_register(&mct_u232_device);
725	if (retval)
726		goto failed_usb_serial_register;
727	retval = usb_register(&mct_u232_driver);
728	if (retval)
729		goto failed_usb_register;
730	info(DRIVER_DESC " " DRIVER_VERSION);
731	return 0;
732failed_usb_register:
733	usb_serial_deregister(&mct_u232_device);
734failed_usb_serial_register:
735	return retval;
736}
737
738
739static void __exit mct_u232_exit (void)
740{
741	usb_deregister (&mct_u232_driver);
742	usb_serial_deregister (&mct_u232_device);
743}
744
745
746module_init (mct_u232_init);
747module_exit(mct_u232_exit);
748
749MODULE_AUTHOR( DRIVER_AUTHOR );
750MODULE_DESCRIPTION( DRIVER_DESC );
751MODULE_LICENSE("GPL");
752
753module_param(debug, bool, S_IRUGO | S_IWUSR);
754MODULE_PARM_DESC(debug, "Debug enabled or not");
755