mct_u232.c 24.9 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/*
 * MCT (Magic Control Technology Corp.) USB RS232 Converter Driver
 *
 *   Copyright (C) 2000 Wolfgang Grandegger (wolfgang@ces.ch)
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 * This program is largely derived from the Belkin USB Serial Adapter Driver
 * (see belkin_sa.[ch]). All of the information about the device was acquired
 * by using SniffUSB on Windows98. For technical details see mct_u232.h.
 *
 * William G. Greathouse and Greg Kroah-Hartman provided great help on how to
 * do the reverse engineering and how to write a USB serial device driver.
 *
 * TO BE DONE, TO BE CHECKED:
 *   DTR/RTS signal handling may be incomplete or incorrect. I have mainly
 *   implemented what I have seen with SniffUSB or found in belkin_sa.c.
 *   For further TODOs check also belkin_sa.c.
 */

#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/tty.h>
#include <linux/tty_driver.h>
#include <linux/tty_flip.h>
#include <linux/module.h>
#include <linux/spinlock.h>
A
Alan Cox 已提交
33
#include <linux/uaccess.h>
34
#include <asm/unaligned.h>
L
Linus Torvalds 已提交
35
#include <linux/usb.h>
36
#include <linux/usb/serial.h>
37 38
#include <linux/serial.h>
#include <linux/ioctl.h>
L
Linus Torvalds 已提交
39 40 41 42 43
#include "mct_u232.h"

/*
 * Version Information
 */
44
#define DRIVER_VERSION "z2.1"		/* Linux in-kernel version */
L
Linus Torvalds 已提交
45 46 47
#define DRIVER_AUTHOR "Wolfgang Grandegger <wolfgang@ces.ch>"
#define DRIVER_DESC "Magic Control Technology USB-RS232 converter driver"

48
static bool debug;
L
Linus Torvalds 已提交
49 50 51 52

/*
 * Function prototypes
 */
A
Alan Cox 已提交
53
static int  mct_u232_startup(struct usb_serial *serial);
54
static void mct_u232_release(struct usb_serial *serial);
55
static int  mct_u232_open(struct tty_struct *tty, struct usb_serial_port *port);
56 57
static void mct_u232_close(struct usb_serial_port *port);
static void mct_u232_dtr_rts(struct usb_serial_port *port, int on);
A
Alan Cox 已提交
58 59 60 61
static void mct_u232_read_int_callback(struct urb *urb);
static void mct_u232_set_termios(struct tty_struct *tty,
			struct usb_serial_port *port, struct ktermios *old);
static void mct_u232_break_ctl(struct tty_struct *tty, int break_state);
62
static int  mct_u232_tiocmget(struct tty_struct *tty);
63
static int  mct_u232_tiocmset(struct tty_struct *tty,
A
Alan Cox 已提交
64
			unsigned int set, unsigned int clear);
65
static int  mct_u232_ioctl(struct tty_struct *tty,
66 67 68
			unsigned int cmd, unsigned long arg);
static int  mct_u232_get_icount(struct tty_struct *tty,
			struct serial_icounter_struct *icount);
A
Alan Cox 已提交
69 70
static void mct_u232_throttle(struct tty_struct *tty);
static void mct_u232_unthrottle(struct tty_struct *tty);
71 72


L
Linus Torvalds 已提交
73 74 75
/*
 * All of the device info needed for the MCT USB-RS232 converter.
 */
76
static const struct usb_device_id id_table[] = {
L
Linus Torvalds 已提交
77 78 79 80 81 82
	{ USB_DEVICE(MCT_U232_VID, MCT_U232_PID) },
	{ USB_DEVICE(MCT_U232_VID, MCT_U232_SITECOM_PID) },
	{ USB_DEVICE(MCT_U232_VID, MCT_U232_DU_H3SP_PID) },
	{ USB_DEVICE(MCT_U232_BELKIN_F5U109_VID, MCT_U232_BELKIN_F5U109_PID) },
	{ }		/* Terminating entry */
};
83
MODULE_DEVICE_TABLE(usb, id_table);
L
Linus Torvalds 已提交
84

85
static struct usb_serial_driver mct_u232_device = {
86 87
	.driver = {
		.owner =	THIS_MODULE,
88
		.name =		"mct_u232",
89
	},
90
	.description =	     "MCT U232",
91
	.id_table =	     id_table,
L
Linus Torvalds 已提交
92 93 94
	.num_ports =	     1,
	.open =		     mct_u232_open,
	.close =	     mct_u232_close,
95
	.dtr_rts =	     mct_u232_dtr_rts,
96 97
	.throttle =	     mct_u232_throttle,
	.unthrottle =	     mct_u232_unthrottle,
L
Linus Torvalds 已提交
98 99 100 101 102 103
	.read_int_callback = mct_u232_read_int_callback,
	.set_termios =	     mct_u232_set_termios,
	.break_ctl =	     mct_u232_break_ctl,
	.tiocmget =	     mct_u232_tiocmget,
	.tiocmset =	     mct_u232_tiocmset,
	.attach =	     mct_u232_startup,
104
	.release =	     mct_u232_release,
105 106
	.ioctl =             mct_u232_ioctl,
	.get_icount =        mct_u232_get_icount,
L
Linus Torvalds 已提交
107 108
};

109 110 111 112
static struct usb_serial_driver * const serial_drivers[] = {
	&mct_u232_device, NULL
};

L
Linus Torvalds 已提交
113 114 115 116 117 118
struct mct_u232_private {
	spinlock_t lock;
	unsigned int	     control_state; /* Modem Line Setting (TIOCM) */
	unsigned char        last_lcr;      /* Line Control Register */
	unsigned char	     last_lsr;      /* Line Status Register */
	unsigned char	     last_msr;      /* Modem Status Register */
119
	unsigned int	     rx_flags;      /* Throttling flags */
120 121 122
	struct async_icount  icount;
	wait_queue_head_t    msr_wait;	/* for handling sleeping while waiting
						for msr change to happen */
L
Linus Torvalds 已提交
123 124
};

125 126
#define THROTTLED		0x01

L
Linus Torvalds 已提交
127 128 129 130 131 132 133 134 135 136
/*
 * Handle vendor specific USB requests
 */

#define WDR_TIMEOUT 5000 /* default urb timeout */

/*
 * Later day 2.6.0-test kernels have new baud rates like B230400 which
 * we do not know how to support. We ignore them for the moment.
 */
A
Alan Cox 已提交
137 138
static int mct_u232_calculate_baud_rate(struct usb_serial *serial,
					speed_t value, speed_t *result)
L
Linus Torvalds 已提交
139
{
140 141
	*result = value;

L
Linus Torvalds 已提交
142
	if (le16_to_cpu(serial->dev->descriptor.idProduct) == MCT_U232_SITECOM_PID
A
Alan Cox 已提交
143
		|| le16_to_cpu(serial->dev->descriptor.idProduct) == MCT_U232_BELKIN_F5U109_PID) {
L
Linus Torvalds 已提交
144
		switch (value) {
A
Alan Cox 已提交
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
		case 300:
			return 0x01;
		case 600:
			return 0x02; /* this one not tested */
		case 1200:
			return 0x03;
		case 2400:
			return 0x04;
		case 4800:
			return 0x06;
		case 9600:
			return 0x08;
		case 19200:
			return 0x09;
		case 38400:
			return 0x0a;
		case 57600:
			return 0x0b;
		case 115200:
			return 0x0c;
L
Linus Torvalds 已提交
165
		default:
166
			*result = 9600;
L
Linus Torvalds 已提交
167 168 169
			return 0x08;
		}
	} else {
170 171 172
		/* FIXME: Can we use any divider - should we do
		   divider = 115200/value;
		   real baud = 115200/divider */
L
Linus Torvalds 已提交
173
		switch (value) {
174 175 176 177 178 179 180 181 182 183 184 185
		case 300: break;
		case 600: break;
		case 1200: break;
		case 2400: break;
		case 4800: break;
		case 9600: break;
		case 19200: break;
		case 38400: break;
		case 57600: break;
		case 115200: break;
		default:
			value = 9600;
186
			*result = 9600;
L
Linus Torvalds 已提交
187 188 189 190 191
		}
		return 115200/value;
	}
}

A
Alan Cox 已提交
192 193
static int mct_u232_set_baud_rate(struct tty_struct *tty,
	struct usb_serial *serial, struct usb_serial_port *port, speed_t value)
L
Linus Torvalds 已提交
194
{
195
	unsigned int divisor;
A
Alan Cox 已提交
196
	int rc;
197
	unsigned char *buf;
A
Alan Cox 已提交
198 199 200
	unsigned char cts_enable_byte = 0;
	speed_t speed;

201 202 203
	buf = kmalloc(MCT_U232_MAX_SIZE, GFP_KERNEL);
	if (buf == NULL)
		return -ENOMEM;
A
Alan Cox 已提交
204

205 206
	divisor = mct_u232_calculate_baud_rate(serial, value, &speed);
	put_unaligned_le32(cpu_to_le32(divisor), buf);
A
Alan Cox 已提交
207 208 209
	rc = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
				MCT_U232_SET_BAUD_RATE_REQUEST,
				MCT_U232_SET_REQUEST_TYPE,
210
				0, 0, buf, MCT_U232_SET_BAUD_RATE_SIZE,
A
Alan Cox 已提交
211
				WDR_TIMEOUT);
212
	if (rc < 0)	/*FIXME: What value speed results */
213 214
		dev_err(&port->dev, "Set BAUD RATE %d failed (error = %d)\n",
			value, rc);
215
	else
A
Alan Cox 已提交
216
		tty_encode_baud_rate(tty, speed, speed);
217
	dev_dbg(&port->dev, "set_baud_rate: value: 0x%x, divisor: 0x%x\n", value, divisor);
L
Linus Torvalds 已提交
218 219 220 221 222 223

	/* Mimic the MCT-supplied Windows driver (version 1.21P.0104), which
	   always sends two extra USB 'device request' messages after the
	   'baud rate change' message.  The actual functionality of the
	   request codes in these messages is not fully understood but these
	   particular codes are never seen in any operation besides a baud
224 225 226 227 228 229 230
	   rate change.  Both of these messages send a single byte of data.
	   In the first message, the value of this byte is always zero.

	   The second message has been determined experimentally to control
	   whether data will be transmitted to a device which is not asserting
	   the 'CTS' signal.  If the second message's data byte is zero, data
	   will be transmitted even if 'CTS' is not asserted (i.e. no hardware
A
Alan Cox 已提交
231 232 233
	   flow control).  if the second message's data byte is nonzero (a
	   value of 1 is used by this driver), data will not be transmitted to
	   a device which is not asserting 'CTS'.
234
	*/
L
Linus Torvalds 已提交
235

236
	buf[0] = 0;
L
Linus Torvalds 已提交
237
	rc = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
A
Alan Cox 已提交
238 239
				MCT_U232_SET_UNKNOWN1_REQUEST,
				MCT_U232_SET_REQUEST_TYPE,
240
				0, 0, buf, MCT_U232_SET_UNKNOWN1_SIZE,
A
Alan Cox 已提交
241
				WDR_TIMEOUT);
L
Linus Torvalds 已提交
242
	if (rc < 0)
243 244 245
		dev_err(&port->dev, "Sending USB device request code %d "
			"failed (error = %d)\n", MCT_U232_SET_UNKNOWN1_REQUEST,
			rc);
L
Linus Torvalds 已提交
246

A
Alan Cox 已提交
247
	if (port && C_CRTSCTS(tty))
248 249
	   cts_enable_byte = 1;

250 251
	dev_dbg(&port->dev, "set_baud_rate: send second control message, data = %02X\n",
		cts_enable_byte);
252
	buf[0] = cts_enable_byte;
L
Linus Torvalds 已提交
253
	rc = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
A
Alan Cox 已提交
254 255
			MCT_U232_SET_CTS_REQUEST,
			MCT_U232_SET_REQUEST_TYPE,
256
			0, 0, buf, MCT_U232_SET_CTS_SIZE,
A
Alan Cox 已提交
257
			WDR_TIMEOUT);
L
Linus Torvalds 已提交
258
	if (rc < 0)
259 260
		dev_err(&port->dev, "Sending USB device request code %d "
			"failed (error = %d)\n", MCT_U232_SET_CTS_REQUEST, rc);
L
Linus Torvalds 已提交
261

262
	kfree(buf);
A
Alan Cox 已提交
263
	return rc;
L
Linus Torvalds 已提交
264 265
} /* mct_u232_set_baud_rate */

266 267
static int mct_u232_set_line_ctrl(struct usb_serial_port *port,
				  unsigned char lcr)
L
Linus Torvalds 已提交
268
{
A
Alan Cox 已提交
269
	int rc;
270 271 272 273 274 275 276
	unsigned char *buf;

	buf = kmalloc(MCT_U232_MAX_SIZE, GFP_KERNEL);
	if (buf == NULL)
		return -ENOMEM;

	buf[0] = lcr;
277
	rc = usb_control_msg(port->serial->dev, usb_sndctrlpipe(port->serial->dev, 0),
A
Alan Cox 已提交
278 279
			MCT_U232_SET_LINE_CTRL_REQUEST,
			MCT_U232_SET_REQUEST_TYPE,
280
			0, 0, buf, MCT_U232_SET_LINE_CTRL_SIZE,
A
Alan Cox 已提交
281
			WDR_TIMEOUT);
L
Linus Torvalds 已提交
282
	if (rc < 0)
283 284
		dev_err(&port->dev, "Set LINE CTRL 0x%x failed (error = %d)\n", lcr, rc);
	dev_dbg(&port->dev, "set_line_ctrl: 0x%x\n", lcr);
285
	kfree(buf);
A
Alan Cox 已提交
286
	return rc;
L
Linus Torvalds 已提交
287 288
} /* mct_u232_set_line_ctrl */

289
static int mct_u232_set_modem_ctrl(struct usb_serial_port *port,
L
Linus Torvalds 已提交
290 291
				   unsigned int control_state)
{
A
Alan Cox 已提交
292
	int rc;
293 294 295 296 297 298
	unsigned char mcr;
	unsigned char *buf;

	buf = kmalloc(MCT_U232_MAX_SIZE, GFP_KERNEL);
	if (buf == NULL)
		return -ENOMEM;
L
Linus Torvalds 已提交
299

300
	mcr = MCT_U232_MCR_NONE;
L
Linus Torvalds 已提交
301 302 303 304 305
	if (control_state & TIOCM_DTR)
		mcr |= MCT_U232_MCR_DTR;
	if (control_state & TIOCM_RTS)
		mcr |= MCT_U232_MCR_RTS;

306
	buf[0] = mcr;
307
	rc = usb_control_msg(port->serial->dev, usb_sndctrlpipe(port->serial->dev, 0),
A
Alan Cox 已提交
308 309
			MCT_U232_SET_MODEM_CTRL_REQUEST,
			MCT_U232_SET_REQUEST_TYPE,
310
			0, 0, buf, MCT_U232_SET_MODEM_CTRL_SIZE,
A
Alan Cox 已提交
311
			WDR_TIMEOUT);
312 313
	kfree(buf);

314
	dev_dbg(&port->dev, "set_modem_ctrl: state=0x%x ==> mcr=0x%x\n", control_state, mcr);
L
Linus Torvalds 已提交
315

316
	if (rc < 0) {
317
		dev_err(&port->dev, "Set MODEM CTRL 0x%x failed (error = %d)\n", mcr, rc);
318 319 320
		return rc;
	}
	return 0;
L
Linus Torvalds 已提交
321 322
} /* mct_u232_set_modem_ctrl */

323 324
static int mct_u232_get_modem_stat(struct usb_serial_port *port,
				   unsigned char *msr)
L
Linus Torvalds 已提交
325
{
A
Alan Cox 已提交
326
	int rc;
327 328 329 330 331 332 333
	unsigned char *buf;

	buf = kmalloc(MCT_U232_MAX_SIZE, GFP_KERNEL);
	if (buf == NULL) {
		*msr = 0;
		return -ENOMEM;
	}
334
	rc = usb_control_msg(port->serial->dev, usb_rcvctrlpipe(port->serial->dev, 0),
A
Alan Cox 已提交
335 336
			MCT_U232_GET_MODEM_STAT_REQUEST,
			MCT_U232_GET_REQUEST_TYPE,
337
			0, 0, buf, MCT_U232_GET_MODEM_STAT_SIZE,
A
Alan Cox 已提交
338
			WDR_TIMEOUT);
L
Linus Torvalds 已提交
339
	if (rc < 0) {
340
		dev_err(&port->dev, "Get MODEM STATus failed (error = %d)\n", rc);
L
Linus Torvalds 已提交
341
		*msr = 0;
342 343
	} else {
		*msr = buf[0];
L
Linus Torvalds 已提交
344
	}
345
	dev_dbg(&port->dev, "get_modem_stat: 0x%x\n", *msr);
346
	kfree(buf);
A
Alan Cox 已提交
347
	return rc;
L
Linus Torvalds 已提交
348 349
} /* mct_u232_get_modem_stat */

350 351 352 353 354 355 356 357 358 359 360 361 362 363
static void mct_u232_msr_to_icount(struct async_icount *icount,
						unsigned char msr)
{
	/* Translate Control Line states */
	if (msr & MCT_U232_MSR_DDSR)
		icount->dsr++;
	if (msr & MCT_U232_MSR_DCTS)
		icount->cts++;
	if (msr & MCT_U232_MSR_DRI)
		icount->rng++;
	if (msr & MCT_U232_MSR_DCD)
		icount->dcd++;
} /* mct_u232_msr_to_icount */

364 365
static void mct_u232_msr_to_state(struct usb_serial_port *port,
				  unsigned int *control_state, unsigned char msr)
L
Linus Torvalds 已提交
366
{
A
Alan Cox 已提交
367
	/* Translate Control Line states */
L
Linus Torvalds 已提交
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
	if (msr & MCT_U232_MSR_DSR)
		*control_state |=  TIOCM_DSR;
	else
		*control_state &= ~TIOCM_DSR;
	if (msr & MCT_U232_MSR_CTS)
		*control_state |=  TIOCM_CTS;
	else
		*control_state &= ~TIOCM_CTS;
	if (msr & MCT_U232_MSR_RI)
		*control_state |=  TIOCM_RI;
	else
		*control_state &= ~TIOCM_RI;
	if (msr & MCT_U232_MSR_CD)
		*control_state |=  TIOCM_CD;
	else
		*control_state &= ~TIOCM_CD;
384
	dev_dbg(&port->dev, "msr_to_state: msr=0x%x ==> state=0x%x\n", msr, *control_state);
L
Linus Torvalds 已提交
385 386 387 388 389 390
} /* mct_u232_msr_to_state */

/*
 * Driver's tty interface functions
 */

A
Alan Cox 已提交
391
static int mct_u232_startup(struct usb_serial *serial)
L
Linus Torvalds 已提交
392 393 394 395
{
	struct mct_u232_private *priv;
	struct usb_serial_port *port, *rport;

396
	priv = kzalloc(sizeof(struct mct_u232_private), GFP_KERNEL);
L
Linus Torvalds 已提交
397 398 399
	if (!priv)
		return -ENOMEM;
	spin_lock_init(&priv->lock);
400
	init_waitqueue_head(&priv->msr_wait);
L
Linus Torvalds 已提交
401 402 403 404 405 406 407
	usb_set_serial_port_data(serial->port[0], priv);

	init_waitqueue_head(&serial->port[0]->write_wait);

	/* Puh, that's dirty */
	port = serial->port[0];
	rport = serial->port[1];
M
Mariusz Kozlowski 已提交
408 409
	/* No unlinking, it wasn't submitted yet. */
	usb_free_urb(port->read_urb);
L
Linus Torvalds 已提交
410 411 412 413
	port->read_urb = rport->interrupt_in_urb;
	rport->interrupt_in_urb = NULL;
	port->read_urb->context = port;

A
Alan Cox 已提交
414
	return 0;
L
Linus Torvalds 已提交
415 416 417
} /* mct_u232_startup */


418
static void mct_u232_release(struct usb_serial *serial)
L
Linus Torvalds 已提交
419 420 421
{
	struct mct_u232_private *priv;
	int i;
A
Alan Cox 已提交
422 423

	for (i = 0; i < serial->num_ports; ++i) {
L
Linus Torvalds 已提交
424 425
		/* My special items, the standard routines free my urbs */
		priv = usb_get_serial_port_data(serial->port[i]);
426
		kfree(priv);
L
Linus Torvalds 已提交
427
	}
428
} /* mct_u232_release */
L
Linus Torvalds 已提交
429

430
static int  mct_u232_open(struct tty_struct *tty, struct usb_serial_port *port)
L
Linus Torvalds 已提交
431 432 433 434 435 436 437 438 439 440 441 442 443 444
{
	struct usb_serial *serial = port->serial;
	struct mct_u232_private *priv = usb_get_serial_port_data(port);
	int retval = 0;
	unsigned int control_state;
	unsigned long flags;
	unsigned char last_lcr;
	unsigned char last_msr;

	/* Compensate for a hardware bug: although the Sitecom U232-P25
	 * device reports a maximum output packet size of 32 bytes,
	 * it seems to be able to accept only 16 bytes (and that's what
	 * SniffUSB says too...)
	 */
A
Alan Cox 已提交
445 446
	if (le16_to_cpu(serial->dev->descriptor.idProduct)
						== MCT_U232_SITECOM_PID)
L
Linus Torvalds 已提交
447 448
		port->bulk_out_size = 16;

A
Alan Cox 已提交
449
	/* Do a defined restart: the normal serial device seems to
L
Linus Torvalds 已提交
450 451 452 453 454
	 * always turn on DTR and RTS here, so do the same. I'm not
	 * sure if this is really necessary. But it should not harm
	 * either.
	 */
	spin_lock_irqsave(&priv->lock, flags);
A
Alan Cox 已提交
455
	if (tty && (tty->termios->c_cflag & CBAUD))
L
Linus Torvalds 已提交
456 457 458
		priv->control_state = TIOCM_DTR | TIOCM_RTS;
	else
		priv->control_state = 0;
A
Alan Cox 已提交
459 460

	priv->last_lcr = (MCT_U232_DATA_BITS_8 |
L
Linus Torvalds 已提交
461 462 463 464 465
			  MCT_U232_PARITY_NONE |
			  MCT_U232_STOP_BITS_1);
	control_state = priv->control_state;
	last_lcr = priv->last_lcr;
	spin_unlock_irqrestore(&priv->lock, flags);
466 467
	mct_u232_set_modem_ctrl(port, control_state);
	mct_u232_set_line_ctrl(port, last_lcr);
L
Linus Torvalds 已提交
468 469

	/* Read modem status and update control state */
470
	mct_u232_get_modem_stat(port, &last_msr);
L
Linus Torvalds 已提交
471 472
	spin_lock_irqsave(&priv->lock, flags);
	priv->last_msr = last_msr;
473
	mct_u232_msr_to_state(port, &priv->control_state, priv->last_msr);
L
Linus Torvalds 已提交
474 475 476 477
	spin_unlock_irqrestore(&priv->lock, flags);

	retval = usb_submit_urb(port->read_urb, GFP_KERNEL);
	if (retval) {
478 479 480
		dev_err(&port->dev,
			"usb_submit_urb(read bulk) failed pipe 0x%x err %d\n",
			port->read_urb->pipe, retval);
481
		goto error;
L
Linus Torvalds 已提交
482 483 484
	}

	retval = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
485 486
	if (retval) {
		usb_kill_urb(port->read_urb);
487 488 489
		dev_err(&port->dev,
			"usb_submit_urb(read int) failed pipe 0x%x err %d",
			port->interrupt_in_urb->pipe, retval);
490 491
		goto error;
	}
L
Linus Torvalds 已提交
492
	return 0;
493 494 495

error:
	return retval;
L
Linus Torvalds 已提交
496 497
} /* mct_u232_open */

498
static void mct_u232_dtr_rts(struct usb_serial_port *port, int on)
L
Linus Torvalds 已提交
499
{
500 501
	unsigned int control_state;
	struct mct_u232_private *priv = usb_get_serial_port_data(port);
L
Linus Torvalds 已提交
502

503 504 505 506 507 508 509
	mutex_lock(&port->serial->disc_mutex);
	if (!port->serial->disconnected) {
		/* drop DTR and RTS */
		spin_lock_irq(&priv->lock);
		if (on)
			priv->control_state |= TIOCM_DTR | TIOCM_RTS;
		else
510
			priv->control_state &= ~(TIOCM_DTR | TIOCM_RTS);
511 512
		control_state = priv->control_state;
		spin_unlock_irq(&priv->lock);
513
		mct_u232_set_modem_ctrl(port, control_state);
514
	}
515 516
	mutex_unlock(&port->serial->disc_mutex);
}
517

518 519
static void mct_u232_close(struct usb_serial_port *port)
{
J
Johan Hovold 已提交
520 521 522 523
	if (port->serial->dev) {
		/* shutdown our urbs */
		usb_kill_urb(port->write_urb);
		usb_kill_urb(port->read_urb);
L
Linus Torvalds 已提交
524
		usb_kill_urb(port->interrupt_in_urb);
J
Johan Hovold 已提交
525
	}
L
Linus Torvalds 已提交
526 527 528
} /* mct_u232_close */


A
Alan Cox 已提交
529
static void mct_u232_read_int_callback(struct urb *urb)
L
Linus Torvalds 已提交
530
{
531
	struct usb_serial_port *port = urb->context;
L
Linus Torvalds 已提交
532 533 534
	struct mct_u232_private *priv = usb_get_serial_port_data(port);
	struct tty_struct *tty;
	unsigned char *data = urb->transfer_buffer;
535 536
	int retval;
	int status = urb->status;
L
Linus Torvalds 已提交
537 538
	unsigned long flags;

539
	switch (status) {
L
Linus Torvalds 已提交
540 541 542 543 544 545 546
	case 0:
		/* success */
		break;
	case -ECONNRESET:
	case -ENOENT:
	case -ESHUTDOWN:
		/* this urb is terminated, clean up */
547 548
		dev_dbg(&port->dev, "%s - urb shutting down with status: %d\n",
			__func__, status);
L
Linus Torvalds 已提交
549 550
		return;
	default:
551 552
		dev_dbg(&port->dev, "%s - nonzero urb status received: %d\n",
			__func__, status);
L
Linus Torvalds 已提交
553 554 555
		goto exit;
	}

A
Alan Cox 已提交
556 557
	usb_serial_debug_data(debug, &port->dev, __func__,
					urb->actual_length, data);
L
Linus Torvalds 已提交
558 559 560 561 562 563

	/*
	 * Work-a-round: handle the 'usual' bulk-in pipe here
	 */
	if (urb->transfer_buffer_length > 2) {
		if (urb->actual_length) {
564 565 566 567 568 569
			tty = tty_port_tty_get(&port->port);
			if (tty) {
				tty_insert_flip_string(tty, data,
						urb->actual_length);
				tty_flip_buffer_push(tty);
			}
A
Alan Cox 已提交
570
			tty_kref_put(tty);
L
Linus Torvalds 已提交
571 572 573
		}
		goto exit;
	}
A
Alan Cox 已提交
574

L
Linus Torvalds 已提交
575 576 577 578 579 580
	/*
	 * The interrupt-in pipe signals exceptional conditions (modem line
	 * signal changes and errors). data[0] holds MSR, data[1] holds LSR.
	 */
	spin_lock_irqsave(&priv->lock, flags);
	priv->last_msr = data[MCT_U232_MSR_INDEX];
A
Alan Cox 已提交
581

L
Linus Torvalds 已提交
582
	/* Record Control Line states */
583
	mct_u232_msr_to_state(port, &priv->control_state, priv->last_msr);
L
Linus Torvalds 已提交
584

585 586
	mct_u232_msr_to_icount(&priv->icount, priv->last_msr);

L
Linus Torvalds 已提交
587
#if 0
A
Alan Cox 已提交
588
	/* Not yet handled. See belkin_sa.c for further information */
L
Linus Torvalds 已提交
589 590 591 592 593 594 595 596
	/* Now to report any errors */
	priv->last_lsr = data[MCT_U232_LSR_INDEX];
	/*
	 * fill in the flip buffer here, but I do not know the relation
	 * to the current/next receive buffer or characters.  I need
	 * to look in to this before committing any code.
	 */
	if (priv->last_lsr & MCT_U232_LSR_ERR) {
A
Alan Cox 已提交
597
		tty = tty_port_tty_get(&port->port);
L
Linus Torvalds 已提交
598 599 600 601 602 603 604 605 606 607 608 609
		/* Overrun Error */
		if (priv->last_lsr & MCT_U232_LSR_OE) {
		}
		/* Parity Error */
		if (priv->last_lsr & MCT_U232_LSR_PE) {
		}
		/* Framing Error */
		if (priv->last_lsr & MCT_U232_LSR_FE) {
		}
		/* Break Indicator */
		if (priv->last_lsr & MCT_U232_LSR_BI) {
		}
A
Alan Cox 已提交
610
		tty_kref_put(tty);
L
Linus Torvalds 已提交
611 612
	}
#endif
613
	wake_up_interruptible(&priv->msr_wait);
L
Linus Torvalds 已提交
614 615
	spin_unlock_irqrestore(&priv->lock, flags);
exit:
A
Alan Cox 已提交
616
	retval = usb_submit_urb(urb, GFP_ATOMIC);
617
	if (retval)
618 619 620
		dev_err(&port->dev,
			"%s - usb_submit_urb failed with result %d\n",
			__func__, retval);
L
Linus Torvalds 已提交
621 622
} /* mct_u232_read_int_callback */

A
Alan Cox 已提交
623 624 625
static void mct_u232_set_termios(struct tty_struct *tty,
				 struct usb_serial_port *port,
				 struct ktermios *old_termios)
L
Linus Torvalds 已提交
626 627 628
{
	struct usb_serial *serial = port->serial;
	struct mct_u232_private *priv = usb_get_serial_port_data(port);
A
Alan Cox 已提交
629
	struct ktermios *termios = tty->termios;
630
	unsigned int cflag = termios->c_cflag;
L
Linus Torvalds 已提交
631 632
	unsigned int old_cflag = old_termios->c_cflag;
	unsigned long flags;
633
	unsigned int control_state;
L
Linus Torvalds 已提交
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
	unsigned char last_lcr;

	/* get a local copy of the current port settings */
	spin_lock_irqsave(&priv->lock, flags);
	control_state = priv->control_state;
	spin_unlock_irqrestore(&priv->lock, flags);
	last_lcr = 0;

	/*
	 * Update baud rate.
	 * Do not attempt to cache old rates and skip settings,
	 * disconnects screw such tricks up completely.
	 * Premature optimization is the root of all evil.
	 */

A
Alan Cox 已提交
649
	/* reassert DTR and RTS on transition from B0 */
L
Linus Torvalds 已提交
650
	if ((old_cflag & CBAUD) == B0) {
651
		dev_dbg(&port->dev, "%s: baud was B0\n", __func__);
652
		control_state |= TIOCM_DTR | TIOCM_RTS;
653
		mct_u232_set_modem_ctrl(port, control_state);
L
Linus Torvalds 已提交
654 655
	}

A
Alan Cox 已提交
656
	mct_u232_set_baud_rate(tty, serial, port, tty_get_baud_rate(tty));
L
Linus Torvalds 已提交
657

A
Alan Cox 已提交
658
	if ((cflag & CBAUD) == B0) {
659
		dev_dbg(&port->dev, "%s: baud is B0\n", __func__);
L
Linus Torvalds 已提交
660 661
		/* Drop RTS and DTR */
		control_state &= ~(TIOCM_DTR | TIOCM_RTS);
662
		mct_u232_set_modem_ctrl(port, control_state);
L
Linus Torvalds 已提交
663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686
	}

	/*
	 * Update line control register (LCR)
	 */

	/* set the parity */
	if (cflag & PARENB)
		last_lcr |= (cflag & PARODD) ?
			MCT_U232_PARITY_ODD : MCT_U232_PARITY_EVEN;
	else
		last_lcr |= MCT_U232_PARITY_NONE;

	/* set the number of data bits */
	switch (cflag & CSIZE) {
	case CS5:
		last_lcr |= MCT_U232_DATA_BITS_5; break;
	case CS6:
		last_lcr |= MCT_U232_DATA_BITS_6; break;
	case CS7:
		last_lcr |= MCT_U232_DATA_BITS_7; break;
	case CS8:
		last_lcr |= MCT_U232_DATA_BITS_8; break;
	default:
687 688
		dev_err(&port->dev,
			"CSIZE was not CS5-CS8, using default of 8\n");
L
Linus Torvalds 已提交
689 690 691 692
		last_lcr |= MCT_U232_DATA_BITS_8;
		break;
	}

693 694
	termios->c_cflag &= ~CMSPAR;

L
Linus Torvalds 已提交
695 696 697 698
	/* set the number of stop bits */
	last_lcr |= (cflag & CSTOPB) ?
		MCT_U232_STOP_BITS_2 : MCT_U232_STOP_BITS_1;

699
	mct_u232_set_line_ctrl(port, last_lcr);
L
Linus Torvalds 已提交
700 701 702 703 704 705 706 707

	/* save off the modified port settings */
	spin_lock_irqsave(&priv->lock, flags);
	priv->control_state = control_state;
	priv->last_lcr = last_lcr;
	spin_unlock_irqrestore(&priv->lock, flags);
} /* mct_u232_set_termios */

A
Alan Cox 已提交
708
static void mct_u232_break_ctl(struct tty_struct *tty, int break_state)
L
Linus Torvalds 已提交
709
{
A
Alan Cox 已提交
710
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
711 712 713 714 715 716 717 718 719
	struct mct_u232_private *priv = usb_get_serial_port_data(port);
	unsigned char lcr;
	unsigned long flags;

	spin_lock_irqsave(&priv->lock, flags);
	lcr = priv->last_lcr;

	if (break_state)
		lcr |= MCT_U232_SET_BREAK;
720
	spin_unlock_irqrestore(&priv->lock, flags);
L
Linus Torvalds 已提交
721

722
	mct_u232_set_line_ctrl(port, lcr);
L
Linus Torvalds 已提交
723 724 725
} /* mct_u232_break_ctl */


726
static int mct_u232_tiocmget(struct tty_struct *tty)
L
Linus Torvalds 已提交
727
{
A
Alan Cox 已提交
728
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
729 730 731
	struct mct_u232_private *priv = usb_get_serial_port_data(port);
	unsigned int control_state;
	unsigned long flags;
A
Alan Cox 已提交
732

L
Linus Torvalds 已提交
733 734 735 736 737 738 739
	spin_lock_irqsave(&priv->lock, flags);
	control_state = priv->control_state;
	spin_unlock_irqrestore(&priv->lock, flags);

	return control_state;
}

740
static int mct_u232_tiocmset(struct tty_struct *tty,
L
Linus Torvalds 已提交
741 742
			      unsigned int set, unsigned int clear)
{
A
Alan Cox 已提交
743
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
744 745 746
	struct mct_u232_private *priv = usb_get_serial_port_data(port);
	unsigned int control_state;
	unsigned long flags;
A
Alan Cox 已提交
747

L
Linus Torvalds 已提交
748 749 750 751 752 753 754 755 756 757 758 759 760 761
	spin_lock_irqsave(&priv->lock, flags);
	control_state = priv->control_state;

	if (set & TIOCM_RTS)
		control_state |= TIOCM_RTS;
	if (set & TIOCM_DTR)
		control_state |= TIOCM_DTR;
	if (clear & TIOCM_RTS)
		control_state &= ~TIOCM_RTS;
	if (clear & TIOCM_DTR)
		control_state &= ~TIOCM_DTR;

	priv->control_state = control_state;
	spin_unlock_irqrestore(&priv->lock, flags);
762
	return mct_u232_set_modem_ctrl(port, control_state);
L
Linus Torvalds 已提交
763 764
}

A
Alan Cox 已提交
765
static void mct_u232_throttle(struct tty_struct *tty)
766
{
A
Alan Cox 已提交
767
	struct usb_serial_port *port = tty->driver_data;
768 769 770
	struct mct_u232_private *priv = usb_get_serial_port_data(port);
	unsigned int control_state;

771
	spin_lock_irq(&priv->lock);
772 773
	priv->rx_flags |= THROTTLED;
	if (C_CRTSCTS(tty)) {
A
Alan Cox 已提交
774 775
		priv->control_state &= ~TIOCM_RTS;
		control_state = priv->control_state;
776
		spin_unlock_irq(&priv->lock);
777
		mct_u232_set_modem_ctrl(port, control_state);
778
	} else {
779
		spin_unlock_irq(&priv->lock);
780 781 782
	}
}

A
Alan Cox 已提交
783
static void mct_u232_unthrottle(struct tty_struct *tty)
784
{
A
Alan Cox 已提交
785
	struct usb_serial_port *port = tty->driver_data;
786 787 788
	struct mct_u232_private *priv = usb_get_serial_port_data(port);
	unsigned int control_state;

789
	spin_lock_irq(&priv->lock);
790
	if ((priv->rx_flags & THROTTLED) && C_CRTSCTS(tty)) {
A
Alan Cox 已提交
791 792 793
		priv->rx_flags &= ~THROTTLED;
		priv->control_state |= TIOCM_RTS;
		control_state = priv->control_state;
794
		spin_unlock_irq(&priv->lock);
795
		mct_u232_set_modem_ctrl(port, control_state);
796
	} else {
797
		spin_unlock_irq(&priv->lock);
798 799
	}
}
L
Linus Torvalds 已提交
800

801
static int  mct_u232_ioctl(struct tty_struct *tty,
802 803 804 805 806 807 808 809
			unsigned int cmd, unsigned long arg)
{
	DEFINE_WAIT(wait);
	struct usb_serial_port *port = tty->driver_data;
	struct mct_u232_private *mct_u232_port = usb_get_serial_port_data(port);
	struct async_icount cnow, cprev;
	unsigned long flags;

810
	dev_dbg(&port->dev, "%s - cmd = 0x%x\n", __func__, cmd);
811 812 813 814 815

	switch (cmd) {

	case TIOCMIWAIT:

816
		dev_dbg(&port->dev, "%s TIOCMIWAIT", __func__);
817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871

		spin_lock_irqsave(&mct_u232_port->lock, flags);
		cprev = mct_u232_port->icount;
		spin_unlock_irqrestore(&mct_u232_port->lock, flags);
		for ( ; ; ) {
			prepare_to_wait(&mct_u232_port->msr_wait,
					&wait, TASK_INTERRUPTIBLE);
			schedule();
			finish_wait(&mct_u232_port->msr_wait, &wait);
			/* see if a signal did it */
			if (signal_pending(current))
				return -ERESTARTSYS;
			spin_lock_irqsave(&mct_u232_port->lock, flags);
			cnow = mct_u232_port->icount;
			spin_unlock_irqrestore(&mct_u232_port->lock, flags);
			if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
			    cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
				return -EIO; /* no change => error */
			if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
			    ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
			    ((arg & TIOCM_CD)  && (cnow.dcd != cprev.dcd)) ||
			    ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
				return 0;
			}
			cprev = cnow;
		}

	}
	return -ENOIOCTLCMD;
}

static int  mct_u232_get_icount(struct tty_struct *tty,
			struct serial_icounter_struct *icount)
{
	struct usb_serial_port *port = tty->driver_data;
	struct mct_u232_private *mct_u232_port = usb_get_serial_port_data(port);
	struct async_icount *ic = &mct_u232_port->icount;
	unsigned long flags;

	spin_lock_irqsave(&mct_u232_port->lock, flags);

	icount->cts = ic->cts;
	icount->dsr = ic->dsr;
	icount->rng = ic->rng;
	icount->dcd = ic->dcd;
	icount->rx = ic->rx;
	icount->tx = ic->tx;
	icount->frame = ic->frame;
	icount->overrun = ic->overrun;
	icount->parity = ic->parity;
	icount->brk = ic->brk;
	icount->buf_overrun = ic->buf_overrun;

	spin_unlock_irqrestore(&mct_u232_port->lock, flags);

872 873
	dev_dbg(&port->dev, "%s TIOCGICOUNT RX=%d, TX=%d\n",
		__func__,  icount->rx, icount->tx);
874 875 876
	return 0;
}

877
module_usb_serial_driver(serial_drivers, id_table);
L
Linus Torvalds 已提交
878

A
Alan Cox 已提交
879 880
MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_DESCRIPTION(DRIVER_DESC);
L
Linus Torvalds 已提交
881 882 883 884
MODULE_LICENSE("GPL");

module_param(debug, bool, S_IRUGO | S_IWUSR);
MODULE_PARM_DESC(debug, "Debug enabled or not");