digi_acceleport.c 43.8 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
/*
*  Digi AccelePort USB-4 and USB-2 Serial Converters
*
*  Copyright 2000 by Digi International
*
*  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.
*
*  Shamelessly based on Brian Warner's keyspan_pda.c and Greg Kroah-Hartman's
*  usb-serial driver.
*
*  Peter Berger (pberger@brimson.com)
*  Al Borchers (borchers@steinerpoint.com)
*/

#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>
#include <linux/workqueue.h>
A
Alan Cox 已提交
28
#include <linux/uaccess.h>
L
Linus Torvalds 已提交
29 30
#include <linux/usb.h>
#include <linux/wait.h>
31
#include <linux/usb/serial.h>
L
Linus Torvalds 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210

/* Defines */

/*
 * Version Information
 */
#define DRIVER_VERSION "v1.80.1.2"
#define DRIVER_AUTHOR "Peter Berger <pberger@brimson.com>, Al Borchers <borchers@steinerpoint.com>"
#define DRIVER_DESC "Digi AccelePort USB-2/USB-4 Serial Converter driver"

/* port output buffer length -- must be <= transfer buffer length - 2 */
/* so we can be sure to send the full buffer in one urb */
#define DIGI_OUT_BUF_SIZE		8

/* port input buffer length -- must be >= transfer buffer length - 3 */
/* so we can be sure to hold at least one full buffer from one urb */
#define DIGI_IN_BUF_SIZE		64

/* retry timeout while sleeping */
#define DIGI_RETRY_TIMEOUT		(HZ/10)

/* timeout while waiting for tty output to drain in close */
/* this delay is used twice in close, so the total delay could */
/* be twice this value */
#define DIGI_CLOSE_TIMEOUT		(5*HZ)


/* AccelePort USB Defines */

/* ids */
#define DIGI_VENDOR_ID			0x05c5
#define DIGI_2_ID			0x0002	/* USB-2 */
#define DIGI_4_ID			0x0004	/* USB-4 */

/* commands
 * "INB": can be used on the in-band endpoint
 * "OOB": can be used on the out-of-band endpoint
 */
#define DIGI_CMD_SET_BAUD_RATE			0	/* INB, OOB */
#define DIGI_CMD_SET_WORD_SIZE			1	/* INB, OOB */
#define DIGI_CMD_SET_PARITY			2	/* INB, OOB */
#define DIGI_CMD_SET_STOP_BITS			3	/* INB, OOB */
#define DIGI_CMD_SET_INPUT_FLOW_CONTROL		4	/* INB, OOB */
#define DIGI_CMD_SET_OUTPUT_FLOW_CONTROL	5	/* INB, OOB */
#define DIGI_CMD_SET_DTR_SIGNAL			6	/* INB, OOB */
#define DIGI_CMD_SET_RTS_SIGNAL			7	/* INB, OOB */
#define DIGI_CMD_READ_INPUT_SIGNALS		8	/*      OOB */
#define DIGI_CMD_IFLUSH_FIFO			9	/*      OOB */
#define DIGI_CMD_RECEIVE_ENABLE			10	/* INB, OOB */
#define DIGI_CMD_BREAK_CONTROL			11	/* INB, OOB */
#define DIGI_CMD_LOCAL_LOOPBACK			12	/* INB, OOB */
#define DIGI_CMD_TRANSMIT_IDLE			13	/* INB, OOB */
#define DIGI_CMD_READ_UART_REGISTER		14	/*      OOB */
#define DIGI_CMD_WRITE_UART_REGISTER		15	/* INB, OOB */
#define DIGI_CMD_AND_UART_REGISTER		16	/* INB, OOB */
#define DIGI_CMD_OR_UART_REGISTER		17	/* INB, OOB */
#define DIGI_CMD_SEND_DATA			18	/* INB      */
#define DIGI_CMD_RECEIVE_DATA			19	/* INB      */
#define DIGI_CMD_RECEIVE_DISABLE		20	/* INB      */
#define DIGI_CMD_GET_PORT_TYPE			21	/*      OOB */

/* baud rates */
#define DIGI_BAUD_50				0
#define DIGI_BAUD_75				1
#define DIGI_BAUD_110				2
#define DIGI_BAUD_150				3
#define DIGI_BAUD_200				4
#define DIGI_BAUD_300				5
#define DIGI_BAUD_600				6
#define DIGI_BAUD_1200				7
#define DIGI_BAUD_1800				8
#define DIGI_BAUD_2400				9
#define DIGI_BAUD_4800				10
#define DIGI_BAUD_7200				11
#define DIGI_BAUD_9600				12
#define DIGI_BAUD_14400				13
#define DIGI_BAUD_19200				14
#define DIGI_BAUD_28800				15
#define DIGI_BAUD_38400				16
#define DIGI_BAUD_57600				17
#define DIGI_BAUD_76800				18
#define DIGI_BAUD_115200			19
#define DIGI_BAUD_153600			20
#define DIGI_BAUD_230400			21
#define DIGI_BAUD_460800			22

/* arguments */
#define DIGI_WORD_SIZE_5			0
#define DIGI_WORD_SIZE_6			1
#define DIGI_WORD_SIZE_7			2
#define DIGI_WORD_SIZE_8			3

#define DIGI_PARITY_NONE			0
#define DIGI_PARITY_ODD				1
#define DIGI_PARITY_EVEN			2
#define DIGI_PARITY_MARK			3
#define DIGI_PARITY_SPACE			4

#define DIGI_STOP_BITS_1			0
#define DIGI_STOP_BITS_2			1

#define DIGI_INPUT_FLOW_CONTROL_XON_XOFF	1
#define DIGI_INPUT_FLOW_CONTROL_RTS		2
#define DIGI_INPUT_FLOW_CONTROL_DTR		4

#define DIGI_OUTPUT_FLOW_CONTROL_XON_XOFF	1
#define DIGI_OUTPUT_FLOW_CONTROL_CTS		2
#define DIGI_OUTPUT_FLOW_CONTROL_DSR		4

#define DIGI_DTR_INACTIVE			0
#define DIGI_DTR_ACTIVE				1
#define DIGI_DTR_INPUT_FLOW_CONTROL		2

#define DIGI_RTS_INACTIVE			0
#define DIGI_RTS_ACTIVE				1
#define DIGI_RTS_INPUT_FLOW_CONTROL		2
#define DIGI_RTS_TOGGLE				3

#define DIGI_FLUSH_TX				1
#define DIGI_FLUSH_RX				2
#define DIGI_RESUME_TX				4 /* clears xoff condition */

#define DIGI_TRANSMIT_NOT_IDLE			0
#define DIGI_TRANSMIT_IDLE			1

#define DIGI_DISABLE				0
#define DIGI_ENABLE				1

#define DIGI_DEASSERT				0
#define DIGI_ASSERT				1

/* in band status codes */
#define DIGI_OVERRUN_ERROR			4
#define DIGI_PARITY_ERROR			8
#define DIGI_FRAMING_ERROR			16
#define DIGI_BREAK_ERROR			32

/* out of band status */
#define DIGI_NO_ERROR				0
#define DIGI_BAD_FIRST_PARAMETER		1
#define DIGI_BAD_SECOND_PARAMETER		2
#define DIGI_INVALID_LINE			3
#define DIGI_INVALID_OPCODE			4

/* input signals */
#define DIGI_READ_INPUT_SIGNALS_SLOT		1
#define DIGI_READ_INPUT_SIGNALS_ERR		2
#define DIGI_READ_INPUT_SIGNALS_BUSY		4
#define DIGI_READ_INPUT_SIGNALS_PE		8
#define DIGI_READ_INPUT_SIGNALS_CTS		16
#define DIGI_READ_INPUT_SIGNALS_DSR		32
#define DIGI_READ_INPUT_SIGNALS_RI		64
#define DIGI_READ_INPUT_SIGNALS_DCD		128


/* Structures */

struct digi_serial {
	spinlock_t ds_serial_lock;
	struct usb_serial_port *ds_oob_port;	/* out-of-band port */
	int ds_oob_port_num;			/* index of out-of-band port */
	int ds_device_started;
};

struct digi_port {
	spinlock_t dp_port_lock;
	int dp_port_num;
	int dp_out_buf_len;
	unsigned char dp_out_buf[DIGI_OUT_BUF_SIZE];
	int dp_write_urb_in_use;
	unsigned int dp_modem_signals;
	wait_queue_head_t dp_modem_change_wait;
	int dp_transmit_idle;
	wait_queue_head_t dp_transmit_idle_wait;
	int dp_throttled;
	int dp_throttle_restart;
	wait_queue_head_t dp_flush_wait;
	wait_queue_head_t dp_close_wait;	/* wait queue for close */
	struct work_struct dp_wakeup_work;
D
David Howells 已提交
211
	struct usb_serial_port *dp_port;
L
Linus Torvalds 已提交
212 213 214 215 216
};


/* Local Function Declarations */

217
static void digi_wakeup_write(struct usb_serial_port *port);
D
David Howells 已提交
218
static void digi_wakeup_write_lock(struct work_struct *work);
219 220 221 222 223 224 225 226
static int digi_write_oob_command(struct usb_serial_port *port,
	unsigned char *buf, int count, int interruptible);
static int digi_write_inb_command(struct usb_serial_port *port,
	unsigned char *buf, int count, unsigned long timeout);
static int digi_set_modem_signals(struct usb_serial_port *port,
	unsigned int modem_signals, int interruptible);
static int digi_transmit_idle(struct usb_serial_port *port,
	unsigned long timeout);
A
Alan Cox 已提交
227 228
static void digi_rx_throttle(struct tty_struct *tty);
static void digi_rx_unthrottle(struct tty_struct *tty);
A
Alan Cox 已提交
229 230 231
static void digi_set_termios(struct tty_struct *tty,
		struct usb_serial_port *port, struct ktermios *old_termios);
static void digi_break_ctl(struct tty_struct *tty, int break_state);
232
static int digi_tiocmget(struct tty_struct *tty);
233 234
static int digi_tiocmset(struct tty_struct *tty, unsigned int set,
		unsigned int clear);
A
Alan Cox 已提交
235
static int digi_write(struct tty_struct *tty, struct usb_serial_port *port,
236
		const unsigned char *buf, int count);
237
static void digi_write_bulk_callback(struct urb *urb);
A
Alan Cox 已提交
238 239
static int digi_write_room(struct tty_struct *tty);
static int digi_chars_in_buffer(struct tty_struct *tty);
240
static int digi_open(struct tty_struct *tty, struct usb_serial_port *port);
241 242
static void digi_close(struct usb_serial_port *port);
static void digi_dtr_rts(struct usb_serial_port *port, int on);
243 244
static int digi_startup_device(struct usb_serial *serial);
static int digi_startup(struct usb_serial *serial);
245 246
static void digi_disconnect(struct usb_serial *serial);
static void digi_release(struct usb_serial *serial);
247 248
static int digi_port_probe(struct usb_serial_port *port);
static int digi_port_remove(struct usb_serial_port *port);
249 250 251
static void digi_read_bulk_callback(struct urb *urb);
static int digi_read_inb_callback(struct urb *urb);
static int digi_read_oob_callback(struct urb *urb);
L
Linus Torvalds 已提交
252 253


254
static const struct usb_device_id id_table_combined[] = {
L
Linus Torvalds 已提交
255 256 257 258 259
	{ USB_DEVICE(DIGI_VENDOR_ID, DIGI_2_ID) },
	{ USB_DEVICE(DIGI_VENDOR_ID, DIGI_4_ID) },
	{ }						/* Terminating entry */
};

260
static const struct usb_device_id id_table_2[] = {
L
Linus Torvalds 已提交
261 262 263 264
	{ USB_DEVICE(DIGI_VENDOR_ID, DIGI_2_ID) },
	{ }						/* Terminating entry */
};

265
static const struct usb_device_id id_table_4[] = {
L
Linus Torvalds 已提交
266 267 268 269
	{ USB_DEVICE(DIGI_VENDOR_ID, DIGI_4_ID) },
	{ }						/* Terminating entry */
};

A
Alan Cox 已提交
270
MODULE_DEVICE_TABLE(usb, id_table_combined);
L
Linus Torvalds 已提交
271 272 273

/* device info needed for the Digi serial converter */

274
static struct usb_serial_driver digi_acceleport_2_device = {
275
	.driver = {
276 277
		.owner =		THIS_MODULE,
		.name =			"digi_2",
278
	},
279
	.description =			"Digi 2 port USB adapter",
L
Linus Torvalds 已提交
280 281 282 283
	.id_table =			id_table_2,
	.num_ports =			3,
	.open =				digi_open,
	.close =			digi_close,
284
	.dtr_rts =			digi_dtr_rts,
L
Linus Torvalds 已提交
285 286 287 288 289 290 291 292 293 294 295 296
	.write =			digi_write,
	.write_room =			digi_write_room,
	.write_bulk_callback = 		digi_write_bulk_callback,
	.read_bulk_callback =		digi_read_bulk_callback,
	.chars_in_buffer =		digi_chars_in_buffer,
	.throttle =			digi_rx_throttle,
	.unthrottle =			digi_rx_unthrottle,
	.set_termios =			digi_set_termios,
	.break_ctl =			digi_break_ctl,
	.tiocmget =			digi_tiocmget,
	.tiocmset =			digi_tiocmset,
	.attach =			digi_startup,
297 298
	.disconnect =			digi_disconnect,
	.release =			digi_release,
299 300
	.port_probe =			digi_port_probe,
	.port_remove =			digi_port_remove,
L
Linus Torvalds 已提交
301 302
};

303
static struct usb_serial_driver digi_acceleport_4_device = {
304
	.driver = {
305 306
		.owner =		THIS_MODULE,
		.name =			"digi_4",
307
	},
308
	.description =			"Digi 4 port USB adapter",
L
Linus Torvalds 已提交
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
	.id_table =			id_table_4,
	.num_ports =			4,
	.open =				digi_open,
	.close =			digi_close,
	.write =			digi_write,
	.write_room =			digi_write_room,
	.write_bulk_callback = 		digi_write_bulk_callback,
	.read_bulk_callback =		digi_read_bulk_callback,
	.chars_in_buffer =		digi_chars_in_buffer,
	.throttle =			digi_rx_throttle,
	.unthrottle =			digi_rx_unthrottle,
	.set_termios =			digi_set_termios,
	.break_ctl =			digi_break_ctl,
	.tiocmget =			digi_tiocmget,
	.tiocmset =			digi_tiocmset,
	.attach =			digi_startup,
325 326
	.disconnect =			digi_disconnect,
	.release =			digi_release,
327 328
	.port_probe =			digi_port_probe,
	.port_remove =			digi_port_remove,
L
Linus Torvalds 已提交
329 330
};

331 332 333
static struct usb_serial_driver * const serial_drivers[] = {
	&digi_acceleport_2_device, &digi_acceleport_4_device, NULL
};
L
Linus Torvalds 已提交
334 335 336 337

/* Functions */

/*
A
Alan Cox 已提交
338 339 340 341 342 343 344 345 346 347 348
 *  Cond Wait Interruptible Timeout Irqrestore
 *
 *  Do spin_unlock_irqrestore and interruptible_sleep_on_timeout
 *  so that wake ups are not lost if they occur between the unlock
 *  and the sleep.  In other words, spin_unlock_irqrestore and
 *  interruptible_sleep_on_timeout are "atomic" with respect to
 *  wake ups.  This is used to implement condition variables.
 *
 *  interruptible_sleep_on_timeout is deprecated and has been replaced
 *  with the equivalent code.
 */
L
Linus Torvalds 已提交
349

350
static long cond_wait_interruptible_timeout_irqrestore(
L
Linus Torvalds 已提交
351
	wait_queue_head_t *q, long timeout,
352
	spinlock_t *lock, unsigned long flags)
353
__releases(lock)
L
Linus Torvalds 已提交
354 355 356
{
	DEFINE_WAIT(wait);

357
	prepare_to_wait(q, &wait, TASK_INTERRUPTIBLE);
L
Linus Torvalds 已提交
358 359 360 361 362 363 364 365 366
	spin_unlock_irqrestore(lock, flags);
	timeout = schedule_timeout(timeout);
	finish_wait(q, &wait);

	return timeout;
}


/*
A
Alan Cox 已提交
367 368 369 370 371
 *  Digi Wakeup Write
 *
 *  Wake up port, line discipline, and tty processes sleeping
 *  on writes.
 */
L
Linus Torvalds 已提交
372

D
David Howells 已提交
373
static void digi_wakeup_write_lock(struct work_struct *work)
L
Linus Torvalds 已提交
374
{
A
Alan Cox 已提交
375 376
	struct digi_port *priv =
			container_of(work, struct digi_port, dp_wakeup_work);
D
David Howells 已提交
377
	struct usb_serial_port *port = priv->dp_port;
L
Linus Torvalds 已提交
378 379
	unsigned long flags;

380 381 382
	spin_lock_irqsave(&priv->dp_port_lock, flags);
	digi_wakeup_write(port);
	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
L
Linus Torvalds 已提交
383 384
}

385
static void digi_wakeup_write(struct usb_serial_port *port)
L
Linus Torvalds 已提交
386
{
A
Alan Cox 已提交
387
	struct tty_struct *tty = tty_port_tty_get(&port->port);
388 389 390 391
	if (tty) {
		tty_wakeup(tty);
		tty_kref_put(tty);
	}
L
Linus Torvalds 已提交
392 393 394 395
}


/*
A
Alan Cox 已提交
396 397 398 399 400 401 402 403 404
 *  Digi Write OOB Command
 *
 *  Write commands on the out of band port.  Commands are 4
 *  bytes each, multiple commands can be sent at once, and
 *  no command will be split across USB packets.  Returns 0
 *  if successful, -EINTR if interrupted while sleeping and
 *  the interruptible flag is true, or a negative error
 *  returned by usb_submit_urb.
 */
L
Linus Torvalds 已提交
405

406 407
static int digi_write_oob_command(struct usb_serial_port *port,
	unsigned char *buf, int count, int interruptible)
L
Linus Torvalds 已提交
408 409 410 411 412 413 414
{
	int ret = 0;
	int len;
	struct usb_serial_port *oob_port = (struct usb_serial_port *)((struct digi_serial *)(usb_get_serial_data(port->serial)))->ds_oob_port;
	struct digi_port *oob_priv = usb_get_serial_port_data(oob_port);
	unsigned long flags = 0;

415 416 417
	dev_dbg(&port->dev,
		"digi_write_oob_command: TOP: port=%d, count=%d\n",
		oob_priv->dp_port_num, count);
L
Linus Torvalds 已提交
418

419
	spin_lock_irqsave(&oob_priv->dp_port_lock, flags);
A
Alan Cox 已提交
420
	while (count > 0) {
421
		while (oob_priv->dp_write_urb_in_use) {
L
Linus Torvalds 已提交
422 423
			cond_wait_interruptible_timeout_irqrestore(
				&oob_port->write_wait, DIGI_RETRY_TIMEOUT,
424 425 426 427
				&oob_priv->dp_port_lock, flags);
			if (interruptible && signal_pending(current))
				return -EINTR;
			spin_lock_irqsave(&oob_priv->dp_port_lock, flags);
L
Linus Torvalds 已提交
428 429 430
		}

		/* len must be a multiple of 4, so commands are not split */
431 432
		len = min(count, oob_port->bulk_out_size);
		if (len > 4)
L
Linus Torvalds 已提交
433
			len &= ~3;
434
		memcpy(oob_port->write_urb->transfer_buffer, buf, len);
L
Linus Torvalds 已提交
435
		oob_port->write_urb->transfer_buffer_length = len;
A
Alan Cox 已提交
436 437
		ret = usb_submit_urb(oob_port->write_urb, GFP_ATOMIC);
		if (ret == 0) {
L
Linus Torvalds 已提交
438 439 440 441 442
			oob_priv->dp_write_urb_in_use = 1;
			count -= len;
			buf += len;
		}
	}
443 444
	spin_unlock_irqrestore(&oob_priv->dp_port_lock, flags);
	if (ret)
445 446
		dev_err(&port->dev, "%s: usb_submit_urb failed, ret=%d\n",
			__func__, ret);
447
	return ret;
L
Linus Torvalds 已提交
448 449 450 451 452

}


/*
A
Alan Cox 已提交
453 454 455 456 457 458 459 460 461 462
 *  Digi Write In Band Command
 *
 *  Write commands on the given port.  Commands are 4
 *  bytes each, multiple commands can be sent at once, and
 *  no command will be split across USB packets.  If timeout
 *  is non-zero, write in band command will return after
 *  waiting unsuccessfully for the URB status to clear for
 *  timeout ticks.  Returns 0 if successful, or a negative
 *  error returned by digi_write.
 */
L
Linus Torvalds 已提交
463

464 465
static int digi_write_inb_command(struct usb_serial_port *port,
	unsigned char *buf, int count, unsigned long timeout)
L
Linus Torvalds 已提交
466 467 468 469 470 471 472
{
	int ret = 0;
	int len;
	struct digi_port *priv = usb_get_serial_port_data(port);
	unsigned char *data = port->write_urb->transfer_buffer;
	unsigned long flags = 0;

473
	dev_dbg(&port->dev, "digi_write_inb_command: TOP: port=%d, count=%d\n",
474
		priv->dp_port_num, count);
L
Linus Torvalds 已提交
475

476
	if (timeout)
L
Linus Torvalds 已提交
477 478 479 480
		timeout += jiffies;
	else
		timeout = ULONG_MAX;

481
	spin_lock_irqsave(&priv->dp_port_lock, flags);
A
Alan Cox 已提交
482
	while (count > 0 && ret == 0) {
483 484
		while (priv->dp_write_urb_in_use &&
		       time_before(jiffies, timeout)) {
L
Linus Torvalds 已提交
485 486
			cond_wait_interruptible_timeout_irqrestore(
				&port->write_wait, DIGI_RETRY_TIMEOUT,
487 488 489 490
				&priv->dp_port_lock, flags);
			if (signal_pending(current))
				return -EINTR;
			spin_lock_irqsave(&priv->dp_port_lock, flags);
L
Linus Torvalds 已提交
491 492 493 494 495
		}

		/* len must be a multiple of 4 and small enough to */
		/* guarantee the write will send buffered data first, */
		/* so commands are in order with data and not split */
496 497
		len = min(count, port->bulk_out_size-2-priv->dp_out_buf_len);
		if (len > 4)
L
Linus Torvalds 已提交
498 499 500
			len &= ~3;

		/* write any buffered data first */
501
		if (priv->dp_out_buf_len > 0) {
L
Linus Torvalds 已提交
502 503
			data[0] = DIGI_CMD_SEND_DATA;
			data[1] = priv->dp_out_buf_len;
504 505 506
			memcpy(data + 2, priv->dp_out_buf,
				priv->dp_out_buf_len);
			memcpy(data + 2 + priv->dp_out_buf_len, buf, len);
L
Linus Torvalds 已提交
507
			port->write_urb->transfer_buffer_length
508
				= priv->dp_out_buf_len + 2 + len;
L
Linus Torvalds 已提交
509
		} else {
510
			memcpy(data, buf, len);
L
Linus Torvalds 已提交
511 512 513
			port->write_urb->transfer_buffer_length = len;
		}

A
Alan Cox 已提交
514 515
		ret = usb_submit_urb(port->write_urb, GFP_ATOMIC);
		if (ret == 0) {
L
Linus Torvalds 已提交
516 517 518 519 520 521 522
			priv->dp_write_urb_in_use = 1;
			priv->dp_out_buf_len = 0;
			count -= len;
			buf += len;
		}

	}
523
	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
L
Linus Torvalds 已提交
524

525
	if (ret)
526 527
		dev_err(&port->dev,
			"%s: usb_submit_urb failed, ret=%d, port=%d\n",
528
			__func__, ret, priv->dp_port_num);
529
	return ret;
L
Linus Torvalds 已提交
530 531 532 533
}


/*
A
Alan Cox 已提交
534 535 536 537 538 539 540 541
 *  Digi Set Modem Signals
 *
 *  Sets or clears DTR and RTS on the port, according to the
 *  modem_signals argument.  Use TIOCM_DTR and TIOCM_RTS flags
 *  for the modem_signals argument.  Returns 0 if successful,
 *  -EINTR if interrupted while sleeping, or a non-zero error
 *  returned by usb_submit_urb.
 */
L
Linus Torvalds 已提交
542

543 544
static int digi_set_modem_signals(struct usb_serial_port *port,
	unsigned int modem_signals, int interruptible)
L
Linus Torvalds 已提交
545 546 547 548
{

	int ret;
	struct digi_port *port_priv = usb_get_serial_port_data(port);
A
Alan Cox 已提交
549
	struct usb_serial_port *oob_port = (struct usb_serial_port *) ((struct digi_serial *)(usb_get_serial_data(port->serial)))->ds_oob_port;
L
Linus Torvalds 已提交
550 551 552 553 554
	struct digi_port *oob_priv = usb_get_serial_port_data(oob_port);
	unsigned char *data = oob_port->write_urb->transfer_buffer;
	unsigned long flags = 0;


555 556
	dev_dbg(&port->dev,
		"digi_set_modem_signals: TOP: port=%d, modem_signals=0x%x\n",
557
		port_priv->dp_port_num, modem_signals);
L
Linus Torvalds 已提交
558

559 560
	spin_lock_irqsave(&oob_priv->dp_port_lock, flags);
	spin_lock(&port_priv->dp_port_lock);
L
Linus Torvalds 已提交
561

562
	while (oob_priv->dp_write_urb_in_use) {
563
		spin_unlock(&port_priv->dp_port_lock);
L
Linus Torvalds 已提交
564 565
		cond_wait_interruptible_timeout_irqrestore(
			&oob_port->write_wait, DIGI_RETRY_TIMEOUT,
566 567 568 569 570
			&oob_priv->dp_port_lock, flags);
		if (interruptible && signal_pending(current))
			return -EINTR;
		spin_lock_irqsave(&oob_priv->dp_port_lock, flags);
		spin_lock(&port_priv->dp_port_lock);
L
Linus Torvalds 已提交
571 572 573
	}
	data[0] = DIGI_CMD_SET_DTR_SIGNAL;
	data[1] = port_priv->dp_port_num;
A
Alan Cox 已提交
574 575
	data[2] = (modem_signals & TIOCM_DTR) ?
					DIGI_DTR_ACTIVE : DIGI_DTR_INACTIVE;
L
Linus Torvalds 已提交
576 577 578
	data[3] = 0;
	data[4] = DIGI_CMD_SET_RTS_SIGNAL;
	data[5] = port_priv->dp_port_num;
A
Alan Cox 已提交
579 580
	data[6] = (modem_signals & TIOCM_RTS) ?
					DIGI_RTS_ACTIVE : DIGI_RTS_INACTIVE;
L
Linus Torvalds 已提交
581 582 583 584
	data[7] = 0;

	oob_port->write_urb->transfer_buffer_length = 8;

A
Alan Cox 已提交
585 586
	ret = usb_submit_urb(oob_port->write_urb, GFP_ATOMIC);
	if (ret == 0) {
L
Linus Torvalds 已提交
587 588 589 590 591
		oob_priv->dp_write_urb_in_use = 1;
		port_priv->dp_modem_signals =
			(port_priv->dp_modem_signals&~(TIOCM_DTR|TIOCM_RTS))
			| (modem_signals&(TIOCM_DTR|TIOCM_RTS));
	}
592 593 594
	spin_unlock(&port_priv->dp_port_lock);
	spin_unlock_irqrestore(&oob_priv->dp_port_lock, flags);
	if (ret)
595 596
		dev_err(&port->dev, "%s: usb_submit_urb failed, ret=%d\n",
			__func__, ret);
597
	return ret;
L
Linus Torvalds 已提交
598 599 600
}

/*
A
Alan Cox 已提交
601 602 603 604 605 606 607 608 609 610
 *  Digi Transmit Idle
 *
 *  Digi transmit idle waits, up to timeout ticks, for the transmitter
 *  to go idle.  It returns 0 if successful or a negative error.
 *
 *  There are race conditions here if more than one process is calling
 *  digi_transmit_idle on the same port at the same time.  However, this
 *  is only called from close, and only one process can be in close on a
 *  port at a time, so its ok.
 */
L
Linus Torvalds 已提交
611

612 613
static int digi_transmit_idle(struct usb_serial_port *port,
	unsigned long timeout)
L
Linus Torvalds 已提交
614 615 616 617 618 619
{
	int ret;
	unsigned char buf[2];
	struct digi_port *priv = usb_get_serial_port_data(port);
	unsigned long flags = 0;

620
	spin_lock_irqsave(&priv->dp_port_lock, flags);
L
Linus Torvalds 已提交
621
	priv->dp_transmit_idle = 0;
622
	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
L
Linus Torvalds 已提交
623 624 625 626 627 628

	buf[0] = DIGI_CMD_TRANSMIT_IDLE;
	buf[1] = 0;

	timeout += jiffies;

A
Alan Cox 已提交
629 630
	ret = digi_write_inb_command(port, buf, 2, timeout - jiffies);
	if (ret != 0)
631
		return ret;
L
Linus Torvalds 已提交
632

633
	spin_lock_irqsave(&priv->dp_port_lock, flags);
L
Linus Torvalds 已提交
634

A
Alan Cox 已提交
635
	while (time_before(jiffies, timeout) && !priv->dp_transmit_idle) {
L
Linus Torvalds 已提交
636 637
		cond_wait_interruptible_timeout_irqrestore(
			&priv->dp_transmit_idle_wait, DIGI_RETRY_TIMEOUT,
638 639 640 641
			&priv->dp_port_lock, flags);
		if (signal_pending(current))
			return -EINTR;
		spin_lock_irqsave(&priv->dp_port_lock, flags);
L
Linus Torvalds 已提交
642 643
	}
	priv->dp_transmit_idle = 0;
644 645
	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
	return 0;
L
Linus Torvalds 已提交
646 647 648 649

}


A
Alan Cox 已提交
650
static void digi_rx_throttle(struct tty_struct *tty)
L
Linus Torvalds 已提交
651 652
{
	unsigned long flags;
A
Alan Cox 已提交
653
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
654 655 656
	struct digi_port *priv = usb_get_serial_port_data(port);

	/* stop receiving characters by not resubmitting the read urb */
657
	spin_lock_irqsave(&priv->dp_port_lock, flags);
L
Linus Torvalds 已提交
658 659
	priv->dp_throttled = 1;
	priv->dp_throttle_restart = 0;
660
	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
L
Linus Torvalds 已提交
661 662 663
}


A
Alan Cox 已提交
664
static void digi_rx_unthrottle(struct tty_struct *tty)
L
Linus Torvalds 已提交
665 666 667
{
	int ret = 0;
	unsigned long flags;
A
Alan Cox 已提交
668
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
669 670
	struct digi_port *priv = usb_get_serial_port_data(port);

671
	spin_lock_irqsave(&priv->dp_port_lock, flags);
L
Linus Torvalds 已提交
672 673

	/* restart read chain */
674
	if (priv->dp_throttle_restart)
675
		ret = usb_submit_urb(port->read_urb, GFP_ATOMIC);
L
Linus Torvalds 已提交
676

677 678 679 680
	/* turn throttle off */
	priv->dp_throttled = 0;
	priv->dp_throttle_restart = 0;

681
	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
L
Linus Torvalds 已提交
682

683
	if (ret)
684 685
		dev_err(&port->dev,
			"%s: usb_submit_urb failed, ret=%d, port=%d\n",
686
			__func__, ret, priv->dp_port_num);
L
Linus Torvalds 已提交
687 688 689
}


A
Alan Cox 已提交
690
static void digi_set_termios(struct tty_struct *tty,
A
Alan Cox 已提交
691
		struct usb_serial_port *port, struct ktermios *old_termios)
L
Linus Torvalds 已提交
692 693
{
	struct digi_port *priv = usb_get_serial_port_data(port);
694
	struct device *dev = &port->dev;
695 696
	unsigned int iflag = tty->termios.c_iflag;
	unsigned int cflag = tty->termios.c_cflag;
L
Linus Torvalds 已提交
697 698 699 700
	unsigned int old_iflag = old_termios->c_iflag;
	unsigned int old_cflag = old_termios->c_cflag;
	unsigned char buf[32];
	unsigned int modem_signals;
A
Alan Cox 已提交
701
	int arg, ret;
L
Linus Torvalds 已提交
702
	int i = 0;
703
	speed_t baud;
L
Linus Torvalds 已提交
704

705 706 707
	dev_dbg(dev,
		"digi_set_termios: TOP: port=%d, iflag=0x%x, old_iflag=0x%x, cflag=0x%x, old_cflag=0x%x\n",
		priv->dp_port_num, iflag, old_iflag, cflag, old_cflag);
L
Linus Torvalds 已提交
708 709

	/* set baud rate */
A
Alan Cox 已提交
710 711
	baud = tty_get_baud_rate(tty);
	if (baud != tty_termios_baud_rate(old_termios)) {
L
Linus Torvalds 已提交
712 713 714
		arg = -1;

		/* reassert DTR and (maybe) RTS on transition from B0 */
715
		if ((old_cflag&CBAUD) == B0) {
L
Linus Torvalds 已提交
716 717 718
			/* don't set RTS if using hardware flow control */
			/* and throttling input */
			modem_signals = TIOCM_DTR;
719
			if (!(tty->termios.c_cflag & CRTSCTS) ||
720
			    !test_bit(TTY_THROTTLED, &tty->flags))
L
Linus Torvalds 已提交
721
				modem_signals |= TIOCM_RTS;
722
			digi_set_modem_signals(port, modem_signals, 1);
L
Linus Torvalds 已提交
723
		}
724
		switch (baud) {
A
Alan Cox 已提交
725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748
		/* drop DTR and RTS on transition to B0 */
		case 0: digi_set_modem_signals(port, 0, 1); break;
		case 50: arg = DIGI_BAUD_50; break;
		case 75: arg = DIGI_BAUD_75; break;
		case 110: arg = DIGI_BAUD_110; break;
		case 150: arg = DIGI_BAUD_150; break;
		case 200: arg = DIGI_BAUD_200; break;
		case 300: arg = DIGI_BAUD_300; break;
		case 600: arg = DIGI_BAUD_600; break;
		case 1200: arg = DIGI_BAUD_1200; break;
		case 1800: arg = DIGI_BAUD_1800; break;
		case 2400: arg = DIGI_BAUD_2400; break;
		case 4800: arg = DIGI_BAUD_4800; break;
		case 9600: arg = DIGI_BAUD_9600; break;
		case 19200: arg = DIGI_BAUD_19200; break;
		case 38400: arg = DIGI_BAUD_38400; break;
		case 57600: arg = DIGI_BAUD_57600; break;
		case 115200: arg = DIGI_BAUD_115200; break;
		case 230400: arg = DIGI_BAUD_230400; break;
		case 460800: arg = DIGI_BAUD_460800; break;
		default:
			arg = DIGI_BAUD_9600;
			baud = 9600;
			break;
L
Linus Torvalds 已提交
749
		}
750
		if (arg != -1) {
L
Linus Torvalds 已提交
751 752 753 754 755 756 757
			buf[i++] = DIGI_CMD_SET_BAUD_RATE;
			buf[i++] = priv->dp_port_num;
			buf[i++] = arg;
			buf[i++] = 0;
		}
	}
	/* set parity */
758
	tty->termios.c_cflag &= ~CMSPAR;
759

760 761 762
	if ((cflag&(PARENB|PARODD)) != (old_cflag&(PARENB|PARODD))) {
		if (cflag&PARENB) {
			if (cflag&PARODD)
L
Linus Torvalds 已提交
763 764 765 766 767 768 769 770 771 772 773 774
				arg = DIGI_PARITY_ODD;
			else
				arg = DIGI_PARITY_EVEN;
		} else {
			arg = DIGI_PARITY_NONE;
		}
		buf[i++] = DIGI_CMD_SET_PARITY;
		buf[i++] = priv->dp_port_num;
		buf[i++] = arg;
		buf[i++] = 0;
	}
	/* set word size */
775
	if ((cflag&CSIZE) != (old_cflag&CSIZE)) {
L
Linus Torvalds 已提交
776
		arg = -1;
777
		switch (cflag&CSIZE) {
L
Linus Torvalds 已提交
778 779 780 781 782
		case CS5: arg = DIGI_WORD_SIZE_5; break;
		case CS6: arg = DIGI_WORD_SIZE_6; break;
		case CS7: arg = DIGI_WORD_SIZE_7; break;
		case CS8: arg = DIGI_WORD_SIZE_8; break;
		default:
783 784
			dev_dbg(dev,
				"digi_set_termios: can't handle word size %d\n",
785
				(cflag&CSIZE));
L
Linus Torvalds 已提交
786 787 788
			break;
		}

789
		if (arg != -1) {
L
Linus Torvalds 已提交
790 791 792 793 794 795 796 797 798
			buf[i++] = DIGI_CMD_SET_WORD_SIZE;
			buf[i++] = priv->dp_port_num;
			buf[i++] = arg;
			buf[i++] = 0;
		}

	}

	/* set stop bits */
799
	if ((cflag&CSTOPB) != (old_cflag&CSTOPB)) {
L
Linus Torvalds 已提交
800

801
		if ((cflag&CSTOPB))
L
Linus Torvalds 已提交
802 803 804 805 806 807 808 809 810 811 812 813
			arg = DIGI_STOP_BITS_2;
		else
			arg = DIGI_STOP_BITS_1;

		buf[i++] = DIGI_CMD_SET_STOP_BITS;
		buf[i++] = priv->dp_port_num;
		buf[i++] = arg;
		buf[i++] = 0;

	}

	/* set input flow control */
814 815
	if ((iflag&IXOFF) != (old_iflag&IXOFF)
	    || (cflag&CRTSCTS) != (old_cflag&CRTSCTS)) {
L
Linus Torvalds 已提交
816
		arg = 0;
817
		if (iflag&IXOFF)
L
Linus Torvalds 已提交
818 819 820 821
			arg |= DIGI_INPUT_FLOW_CONTROL_XON_XOFF;
		else
			arg &= ~DIGI_INPUT_FLOW_CONTROL_XON_XOFF;

822
		if (cflag&CRTSCTS) {
L
Linus Torvalds 已提交
823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841
			arg |= DIGI_INPUT_FLOW_CONTROL_RTS;

			/* On USB-4 it is necessary to assert RTS prior */
			/* to selecting RTS input flow control.  */
			buf[i++] = DIGI_CMD_SET_RTS_SIGNAL;
			buf[i++] = priv->dp_port_num;
			buf[i++] = DIGI_RTS_ACTIVE;
			buf[i++] = 0;

		} else {
			arg &= ~DIGI_INPUT_FLOW_CONTROL_RTS;
		}
		buf[i++] = DIGI_CMD_SET_INPUT_FLOW_CONTROL;
		buf[i++] = priv->dp_port_num;
		buf[i++] = arg;
		buf[i++] = 0;
	}

	/* set output flow control */
842 843
	if ((iflag & IXON) != (old_iflag & IXON)
	    || (cflag & CRTSCTS) != (old_cflag & CRTSCTS)) {
L
Linus Torvalds 已提交
844
		arg = 0;
845
		if (iflag & IXON)
L
Linus Torvalds 已提交
846 847 848 849
			arg |= DIGI_OUTPUT_FLOW_CONTROL_XON_XOFF;
		else
			arg &= ~DIGI_OUTPUT_FLOW_CONTROL_XON_XOFF;

850
		if (cflag & CRTSCTS) {
L
Linus Torvalds 已提交
851 852 853
			arg |= DIGI_OUTPUT_FLOW_CONTROL_CTS;
		} else {
			arg &= ~DIGI_OUTPUT_FLOW_CONTROL_CTS;
854
			tty->hw_stopped = 0;
L
Linus Torvalds 已提交
855 856 857 858 859 860 861 862 863
		}

		buf[i++] = DIGI_CMD_SET_OUTPUT_FLOW_CONTROL;
		buf[i++] = priv->dp_port_num;
		buf[i++] = arg;
		buf[i++] = 0;
	}

	/* set receive enable/disable */
864 865
	if ((cflag & CREAD) != (old_cflag & CREAD)) {
		if (cflag & CREAD)
L
Linus Torvalds 已提交
866 867 868 869 870 871 872 873 874
			arg = DIGI_ENABLE;
		else
			arg = DIGI_DISABLE;

		buf[i++] = DIGI_CMD_RECEIVE_ENABLE;
		buf[i++] = priv->dp_port_num;
		buf[i++] = arg;
		buf[i++] = 0;
	}
A
Alan Cox 已提交
875 876
	ret = digi_write_oob_command(port, buf, i, 1);
	if (ret != 0)
877
		dev_dbg(dev, "digi_set_termios: write oob failed, ret=%d\n", ret);
878
	tty_encode_baud_rate(tty, baud, baud);
L
Linus Torvalds 已提交
879 880 881
}


A
Alan Cox 已提交
882
static void digi_break_ctl(struct tty_struct *tty, int break_state)
L
Linus Torvalds 已提交
883
{
A
Alan Cox 已提交
884
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
885 886 887 888 889 890
	unsigned char buf[4];

	buf[0] = DIGI_CMD_BREAK_CONTROL;
	buf[1] = 2;				/* length */
	buf[2] = break_state ? 1 : 0;
	buf[3] = 0;				/* pad */
891
	digi_write_inb_command(port, buf, 4, 0);
L
Linus Torvalds 已提交
892 893 894
}


895
static int digi_tiocmget(struct tty_struct *tty)
L
Linus Torvalds 已提交
896
{
A
Alan Cox 已提交
897
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
898 899 900 901
	struct digi_port *priv = usb_get_serial_port_data(port);
	unsigned int val;
	unsigned long flags;

902
	spin_lock_irqsave(&priv->dp_port_lock, flags);
L
Linus Torvalds 已提交
903
	val = priv->dp_modem_signals;
904
	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
L
Linus Torvalds 已提交
905 906 907 908
	return val;
}


909 910
static int digi_tiocmset(struct tty_struct *tty,
					unsigned int set, unsigned int clear)
L
Linus Torvalds 已提交
911
{
A
Alan Cox 已提交
912
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
913 914 915 916
	struct digi_port *priv = usb_get_serial_port_data(port);
	unsigned int val;
	unsigned long flags;

917
	spin_lock_irqsave(&priv->dp_port_lock, flags);
L
Linus Torvalds 已提交
918
	val = (priv->dp_modem_signals & ~clear) | set;
919 920
	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
	return digi_set_modem_signals(port, val, 1);
L
Linus Torvalds 已提交
921 922 923
}


A
Alan Cox 已提交
924 925
static int digi_write(struct tty_struct *tty, struct usb_serial_port *port,
					const unsigned char *buf, int count)
L
Linus Torvalds 已提交
926 927
{

A
Alan Cox 已提交
928
	int ret, data_len, new_len;
L
Linus Torvalds 已提交
929 930 931 932
	struct digi_port *priv = usb_get_serial_port_data(port);
	unsigned char *data = port->write_urb->transfer_buffer;
	unsigned long flags = 0;

933 934
	dev_dbg(&port->dev,
		"digi_write: TOP: port=%d, count=%d, in_interrupt=%ld\n",
935
		priv->dp_port_num, count, in_interrupt());
L
Linus Torvalds 已提交
936 937

	/* copy user data (which can sleep) before getting spin lock */
938 939
	count = min(count, port->bulk_out_size-2);
	count = min(64, count);
L
Linus Torvalds 已提交
940 941 942

	/* be sure only one write proceeds at a time */
	/* there are races on the port private buffer */
943
	spin_lock_irqsave(&priv->dp_port_lock, flags);
L
Linus Torvalds 已提交
944 945

	/* wait for urb status clear to submit another urb */
946
	if (priv->dp_write_urb_in_use) {
L
Linus Torvalds 已提交
947
		/* buffer data if count is 1 (probably put_char) if possible */
948
		if (count == 1 && priv->dp_out_buf_len < DIGI_OUT_BUF_SIZE) {
L
Linus Torvalds 已提交
949 950 951 952 953
			priv->dp_out_buf[priv->dp_out_buf_len++] = *buf;
			new_len = 1;
		} else {
			new_len = 0;
		}
954 955
		spin_unlock_irqrestore(&priv->dp_port_lock, flags);
		return new_len;
L
Linus Torvalds 已提交
956 957 958 959 960 961 962
	}

	/* allow space for any buffered data and for new data, up to */
	/* transfer buffer size - 2 (for command and length bytes) */
	new_len = min(count, port->bulk_out_size-2-priv->dp_out_buf_len);
	data_len = new_len + priv->dp_out_buf_len;

963 964 965
	if (data_len == 0) {
		spin_unlock_irqrestore(&priv->dp_port_lock, flags);
		return 0;
L
Linus Torvalds 已提交
966 967 968 969 970 971 972 973
	}

	port->write_urb->transfer_buffer_length = data_len+2;

	*data++ = DIGI_CMD_SEND_DATA;
	*data++ = data_len;

	/* copy in buffered data first */
974
	memcpy(data, priv->dp_out_buf, priv->dp_out_buf_len);
L
Linus Torvalds 已提交
975 976 977
	data += priv->dp_out_buf_len;

	/* copy in new data */
978
	memcpy(data, buf, new_len);
L
Linus Torvalds 已提交
979

A
Alan Cox 已提交
980 981
	ret = usb_submit_urb(port->write_urb, GFP_ATOMIC);
	if (ret == 0) {
L
Linus Torvalds 已提交
982 983 984 985 986 987
		priv->dp_write_urb_in_use = 1;
		ret = new_len;
		priv->dp_out_buf_len = 0;
	}

	/* return length of new data written, or error */
988 989
	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
	if (ret < 0)
990
		dev_err_console(port,
991
			"%s: usb_submit_urb failed, ret=%d, port=%d\n",
992
			__func__, ret, priv->dp_port_num);
993
	dev_dbg(&port->dev, "digi_write: returning %d\n", ret);
994
	return ret;
L
Linus Torvalds 已提交
995

A
Alan Cox 已提交
996
}
L
Linus Torvalds 已提交
997

998
static void digi_write_bulk_callback(struct urb *urb)
L
Linus Torvalds 已提交
999 1000
{

1001
	struct usb_serial_port *port = urb->context;
L
Linus Torvalds 已提交
1002 1003 1004 1005
	struct usb_serial *serial;
	struct digi_port *priv;
	struct digi_serial *serial_priv;
	int ret = 0;
1006
	int status = urb->status;
L
Linus Torvalds 已提交
1007 1008

	/* port and serial sanity check */
A
Alan Cox 已提交
1009
	if (port == NULL || (priv = usb_get_serial_port_data(port)) == NULL) {
1010
		pr_err("%s: port or port->private is NULL, status=%d\n",
1011
			__func__, status);
L
Linus Torvalds 已提交
1012 1013 1014
		return;
	}
	serial = port->serial;
A
Alan Cox 已提交
1015
	if (serial == NULL || (serial_priv = usb_get_serial_data(serial)) == NULL) {
1016 1017 1018
		dev_err(&port->dev,
			"%s: serial or serial->private is NULL, status=%d\n",
			__func__, status);
L
Linus Torvalds 已提交
1019 1020 1021 1022
		return;
	}

	/* handle oob callback */
1023
	if (priv->dp_port_num == serial_priv->ds_oob_port_num) {
1024
		dev_dbg(&port->dev, "digi_write_bulk_callback: oob callback\n");
1025
		spin_lock(&priv->dp_port_lock);
L
Linus Torvalds 已提交
1026
		priv->dp_write_urb_in_use = 0;
1027 1028
		wake_up_interruptible(&port->write_wait);
		spin_unlock(&priv->dp_port_lock);
L
Linus Torvalds 已提交
1029 1030 1031
		return;
	}

1032
	/* try to send any buffered data on this port */
1033
	spin_lock(&priv->dp_port_lock);
L
Linus Torvalds 已提交
1034
	priv->dp_write_urb_in_use = 0;
1035
	if (priv->dp_out_buf_len > 0) {
L
Linus Torvalds 已提交
1036 1037
		*((unsigned char *)(port->write_urb->transfer_buffer))
			= (unsigned char)DIGI_CMD_SEND_DATA;
A
Alan Cox 已提交
1038
		*((unsigned char *)(port->write_urb->transfer_buffer) + 1)
L
Linus Torvalds 已提交
1039
			= (unsigned char)priv->dp_out_buf_len;
A
Alan Cox 已提交
1040 1041 1042
		port->write_urb->transfer_buffer_length =
						priv->dp_out_buf_len + 2;
		memcpy(port->write_urb->transfer_buffer + 2, priv->dp_out_buf,
1043
			priv->dp_out_buf_len);
A
Alan Cox 已提交
1044 1045
		ret = usb_submit_urb(port->write_urb, GFP_ATOMIC);
		if (ret == 0) {
L
Linus Torvalds 已提交
1046 1047 1048 1049 1050
			priv->dp_write_urb_in_use = 1;
			priv->dp_out_buf_len = 0;
		}
	}
	/* wake up processes sleeping on writes immediately */
1051
	digi_wakeup_write(port);
L
Linus Torvalds 已提交
1052 1053 1054 1055
	/* also queue up a wakeup at scheduler time, in case we */
	/* lost the race in write_chan(). */
	schedule_work(&priv->dp_wakeup_work);

1056
	spin_unlock(&priv->dp_port_lock);
1057
	if (ret && ret != -EPERM)
1058
		dev_err_console(port,
1059
			"%s: usb_submit_urb failed, ret=%d, port=%d\n",
1060
			__func__, ret, priv->dp_port_num);
L
Linus Torvalds 已提交
1061 1062
}

A
Alan Cox 已提交
1063
static int digi_write_room(struct tty_struct *tty)
L
Linus Torvalds 已提交
1064
{
A
Alan Cox 已提交
1065
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
1066
	struct digi_port *priv = usb_get_serial_port_data(port);
A
Alan Cox 已提交
1067
	int room;
L
Linus Torvalds 已提交
1068 1069
	unsigned long flags = 0;

1070
	spin_lock_irqsave(&priv->dp_port_lock, flags);
L
Linus Torvalds 已提交
1071

1072
	if (priv->dp_write_urb_in_use)
L
Linus Torvalds 已提交
1073 1074 1075 1076
		room = 0;
	else
		room = port->bulk_out_size - 2 - priv->dp_out_buf_len;

1077
	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
1078
	dev_dbg(&port->dev, "digi_write_room: port=%d, room=%d\n", priv->dp_port_num, room);
1079
	return room;
L
Linus Torvalds 已提交
1080 1081 1082

}

A
Alan Cox 已提交
1083
static int digi_chars_in_buffer(struct tty_struct *tty)
L
Linus Torvalds 已提交
1084
{
A
Alan Cox 已提交
1085
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
1086 1087
	struct digi_port *priv = usb_get_serial_port_data(port);

1088
	if (priv->dp_write_urb_in_use) {
1089
		dev_dbg(&port->dev, "digi_chars_in_buffer: port=%d, chars=%d\n",
1090 1091 1092
			priv->dp_port_num, port->bulk_out_size - 2);
		/* return(port->bulk_out_size - 2); */
		return 256;
L
Linus Torvalds 已提交
1093
	} else {
1094
		dev_dbg(&port->dev, "digi_chars_in_buffer: port=%d, chars=%d\n",
1095 1096
			priv->dp_port_num, priv->dp_out_buf_len);
		return priv->dp_out_buf_len;
L
Linus Torvalds 已提交
1097 1098 1099 1100
	}

}

1101 1102 1103 1104 1105 1106
static void digi_dtr_rts(struct usb_serial_port *port, int on)
{
	/* Adjust DTR and RTS */
	digi_set_modem_signals(port, on * (TIOCM_DTR|TIOCM_RTS), 1);
}

1107
static int digi_open(struct tty_struct *tty, struct usb_serial_port *port)
L
Linus Torvalds 已提交
1108 1109 1110 1111
{
	int ret;
	unsigned char buf[32];
	struct digi_port *priv = usb_get_serial_port_data(port);
A
Alan Cox 已提交
1112
	struct ktermios not_termios;
L
Linus Torvalds 已提交
1113 1114

	/* be sure the device is started up */
1115 1116
	if (digi_startup_device(port->serial) != 0)
		return -ENXIO;
L
Linus Torvalds 已提交
1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129

	/* read modem signals automatically whenever they change */
	buf[0] = DIGI_CMD_READ_INPUT_SIGNALS;
	buf[1] = priv->dp_port_num;
	buf[2] = DIGI_ENABLE;
	buf[3] = 0;

	/* flush fifos */
	buf[4] = DIGI_CMD_IFLUSH_FIFO;
	buf[5] = priv->dp_port_num;
	buf[6] = DIGI_FLUSH_TX | DIGI_FLUSH_RX;
	buf[7] = 0;

A
Alan Cox 已提交
1130 1131
	ret = digi_write_oob_command(port, buf, 8, 1);
	if (ret != 0)
1132
		dev_dbg(&port->dev, "digi_open: write oob failed, ret=%d\n", ret);
L
Linus Torvalds 已提交
1133 1134

	/* set termios settings */
A
Alan Cox 已提交
1135
	if (tty) {
1136 1137
		not_termios.c_cflag = ~tty->termios.c_cflag;
		not_termios.c_iflag = ~tty->termios.c_iflag;
A
Alan Cox 已提交
1138 1139
		digi_set_termios(tty, port, &not_termios);
	}
1140
	return 0;
L
Linus Torvalds 已提交
1141 1142 1143
}


1144
static void digi_close(struct usb_serial_port *port)
L
Linus Torvalds 已提交
1145 1146 1147 1148 1149 1150
{
	DEFINE_WAIT(wait);
	int ret;
	unsigned char buf[32];
	struct digi_port *priv = usb_get_serial_port_data(port);

1151
	mutex_lock(&port->serial->disc_mutex);
L
Linus Torvalds 已提交
1152
	/* if disconnected, just clear flags */
1153
	if (port->serial->disconnected)
L
Linus Torvalds 已提交
1154 1155 1156
		goto exit;

	if (port->serial->dev) {
1157 1158
		/* FIXME: Transmit idle belongs in the wait_unti_sent path */
		digi_transmit_idle(port, DIGI_CLOSE_TIMEOUT);
L
Linus Torvalds 已提交
1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189

		/* disable input flow control */
		buf[0] = DIGI_CMD_SET_INPUT_FLOW_CONTROL;
		buf[1] = priv->dp_port_num;
		buf[2] = DIGI_DISABLE;
		buf[3] = 0;

		/* disable output flow control */
		buf[4] = DIGI_CMD_SET_OUTPUT_FLOW_CONTROL;
		buf[5] = priv->dp_port_num;
		buf[6] = DIGI_DISABLE;
		buf[7] = 0;

		/* disable reading modem signals automatically */
		buf[8] = DIGI_CMD_READ_INPUT_SIGNALS;
		buf[9] = priv->dp_port_num;
		buf[10] = DIGI_DISABLE;
		buf[11] = 0;

		/* disable receive */
		buf[12] = DIGI_CMD_RECEIVE_ENABLE;
		buf[13] = priv->dp_port_num;
		buf[14] = DIGI_DISABLE;
		buf[15] = 0;

		/* flush fifos */
		buf[16] = DIGI_CMD_IFLUSH_FIFO;
		buf[17] = priv->dp_port_num;
		buf[18] = DIGI_FLUSH_TX | DIGI_FLUSH_RX;
		buf[19] = 0;

A
Alan Cox 已提交
1190 1191
		ret = digi_write_oob_command(port, buf, 20, 0);
		if (ret != 0)
1192
			dev_dbg(&port->dev, "digi_close: write oob failed, ret=%d\n", ret);
L
Linus Torvalds 已提交
1193 1194

		/* wait for final commands on oob port to complete */
A
Alan Cox 已提交
1195 1196
		prepare_to_wait(&priv->dp_flush_wait, &wait,
							TASK_INTERRUPTIBLE);
L
Linus Torvalds 已提交
1197 1198 1199 1200 1201 1202 1203
		schedule_timeout(DIGI_CLOSE_TIMEOUT);
		finish_wait(&priv->dp_flush_wait, &wait);

		/* shutdown any outstanding bulk writes */
		usb_kill_urb(port->write_urb);
	}
exit:
1204
	spin_lock_irq(&priv->dp_port_lock);
L
Linus Torvalds 已提交
1205
	priv->dp_write_urb_in_use = 0;
1206
	wake_up_interruptible(&priv->dp_close_wait);
1207 1208
	spin_unlock_irq(&priv->dp_port_lock);
	mutex_unlock(&port->serial->disc_mutex);
L
Linus Torvalds 已提交
1209 1210 1211 1212
}


/*
A
Alan Cox 已提交
1213 1214 1215 1216 1217
 *  Digi Startup Device
 *
 *  Starts reads on all ports.  Must be called AFTER startup, with
 *  urbs initialized.  Returns 0 if successful, non-zero error otherwise.
 */
L
Linus Torvalds 已提交
1218

1219
static int digi_startup_device(struct usb_serial *serial)
L
Linus Torvalds 已提交
1220
{
A
Alan Cox 已提交
1221
	int i, ret = 0;
L
Linus Torvalds 已提交
1222 1223 1224 1225
	struct digi_serial *serial_priv = usb_get_serial_data(serial);
	struct usb_serial_port *port;

	/* be sure this happens exactly once */
1226 1227 1228 1229
	spin_lock(&serial_priv->ds_serial_lock);
	if (serial_priv->ds_device_started) {
		spin_unlock(&serial_priv->ds_serial_lock);
		return 0;
L
Linus Torvalds 已提交
1230 1231
	}
	serial_priv->ds_device_started = 1;
1232
	spin_unlock(&serial_priv->ds_serial_lock);
L
Linus Torvalds 已提交
1233 1234 1235

	/* start reading from each bulk in endpoint for the device */
	/* set USB_DISABLE_SPD flag for write bulk urbs */
1236
	for (i = 0; i < serial->type->num_ports + 1; i++) {
L
Linus Torvalds 已提交
1237
		port = serial->port[i];
A
Alan Cox 已提交
1238 1239
		ret = usb_submit_urb(port->read_urb, GFP_KERNEL);
		if (ret != 0) {
1240 1241 1242
			dev_err(&port->dev,
				"%s: usb_submit_urb failed, ret=%d, port=%d\n",
				__func__, ret, i);
L
Linus Torvalds 已提交
1243 1244 1245
			break;
		}
	}
1246
	return ret;
L
Linus Torvalds 已提交
1247 1248
}

1249
static int digi_port_init(struct usb_serial_port *port, unsigned port_num)
L
Linus Torvalds 已提交
1250 1251 1252
{
	struct digi_port *priv;

1253 1254 1255
	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
	if (!priv)
		return -ENOMEM;
L
Linus Torvalds 已提交
1256

1257 1258 1259 1260 1261 1262 1263 1264
	spin_lock_init(&priv->dp_port_lock);
	priv->dp_port_num = port_num;
	init_waitqueue_head(&priv->dp_modem_change_wait);
	init_waitqueue_head(&priv->dp_transmit_idle_wait);
	init_waitqueue_head(&priv->dp_flush_wait);
	init_waitqueue_head(&priv->dp_close_wait);
	INIT_WORK(&priv->dp_wakeup_work, digi_wakeup_write_lock);
	priv->dp_port = port;
L
Linus Torvalds 已提交
1265

1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280
	init_waitqueue_head(&port->write_wait);

	usb_set_serial_port_data(port, priv);

	return 0;
}

static int digi_startup(struct usb_serial *serial)
{
	struct digi_serial *serial_priv;
	int ret;

	serial_priv = kzalloc(sizeof(*serial_priv), GFP_KERNEL);
	if (!serial_priv)
		return -ENOMEM;
L
Linus Torvalds 已提交
1281

1282
	spin_lock_init(&serial_priv->ds_serial_lock);
L
Linus Torvalds 已提交
1283 1284
	serial_priv->ds_oob_port_num = serial->type->num_ports;
	serial_priv->ds_oob_port = serial->port[serial_priv->ds_oob_port_num];
1285 1286 1287 1288 1289 1290 1291 1292

	ret = digi_port_init(serial_priv->ds_oob_port,
						serial_priv->ds_oob_port_num);
	if (ret) {
		kfree(serial_priv);
		return ret;
	}

L
Linus Torvalds 已提交
1293 1294
	usb_set_serial_data(serial, serial_priv);

1295
	return 0;
L
Linus Torvalds 已提交
1296 1297 1298
}


1299
static void digi_disconnect(struct usb_serial *serial)
L
Linus Torvalds 已提交
1300 1301 1302 1303
{
	int i;

	/* stop reads and writes on all ports */
1304
	for (i = 0; i < serial->type->num_ports + 1; i++) {
L
Linus Torvalds 已提交
1305 1306 1307
		usb_kill_urb(serial->port[i]->read_urb);
		usb_kill_urb(serial->port[i]->write_urb);
	}
1308 1309 1310 1311 1312
}


static void digi_release(struct usb_serial *serial)
{
1313 1314 1315 1316 1317 1318 1319
	struct digi_serial *serial_priv;
	struct digi_port *priv;

	serial_priv = usb_get_serial_data(serial);

	priv = usb_get_serial_port_data(serial_priv->ds_oob_port);
	kfree(priv);
L
Linus Torvalds 已提交
1320

1321
	kfree(serial_priv);
L
Linus Torvalds 已提交
1322 1323
}

1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341
static int digi_port_probe(struct usb_serial_port *port)
{
	unsigned port_num;

	port_num = port->number - port->serial->minor;

	return digi_port_init(port, port_num);
}

static int digi_port_remove(struct usb_serial_port *port)
{
	struct digi_port *priv;

	priv = usb_get_serial_port_data(port);
	kfree(priv);

	return 0;
}
L
Linus Torvalds 已提交
1342

1343
static void digi_read_bulk_callback(struct urb *urb)
L
Linus Torvalds 已提交
1344
{
1345
	struct usb_serial_port *port = urb->context;
L
Linus Torvalds 已提交
1346 1347 1348
	struct digi_port *priv;
	struct digi_serial *serial_priv;
	int ret;
1349
	int status = urb->status;
L
Linus Torvalds 已提交
1350 1351

	/* port sanity check, do not resubmit if port is not valid */
1352 1353 1354 1355 1356 1357
	if (port == NULL)
		return;
	priv = usb_get_serial_port_data(port);
	if (priv == NULL) {
		dev_err(&port->dev, "%s: port->private is NULL, status=%d\n",
			__func__, status);
L
Linus Torvalds 已提交
1358 1359
		return;
	}
1360
	if (port->serial == NULL ||
A
Alan Cox 已提交
1361
		(serial_priv = usb_get_serial_data(port->serial)) == NULL) {
1362 1363
		dev_err(&port->dev, "%s: serial is bad or serial->private "
			"is NULL, status=%d\n", __func__, status);
L
Linus Torvalds 已提交
1364 1365 1366 1367
		return;
	}

	/* do not resubmit urb if it has any status error */
1368
	if (status) {
1369 1370 1371
		dev_err(&port->dev,
			"%s: nonzero read bulk status: status=%d, port=%d\n",
			__func__, status, priv->dp_port_num);
L
Linus Torvalds 已提交
1372 1373 1374 1375
		return;
	}

	/* handle oob or inb callback, do not resubmit if error */
1376 1377
	if (priv->dp_port_num == serial_priv->ds_oob_port_num) {
		if (digi_read_oob_callback(urb) != 0)
L
Linus Torvalds 已提交
1378 1379
			return;
	} else {
1380
		if (digi_read_inb_callback(urb) != 0)
L
Linus Torvalds 已提交
1381 1382 1383 1384
			return;
	}

	/* continue read */
A
Alan Cox 已提交
1385
	ret = usb_submit_urb(urb, GFP_ATOMIC);
1386
	if (ret != 0 && ret != -EPERM) {
1387 1388 1389
		dev_err(&port->dev,
			"%s: failed resubmitting urb, ret=%d, port=%d\n",
			__func__, ret, priv->dp_port_num);
L
Linus Torvalds 已提交
1390 1391 1392 1393
	}

}

A
Alan Cox 已提交
1394 1395 1396 1397 1398 1399 1400 1401 1402
/*
 *  Digi Read INB Callback
 *
 *  Digi Read INB Callback handles reads on the in band ports, sending
 *  the data on to the tty subsystem.  When called we know port and
 *  port->private are not NULL and port->serial has been validated.
 *  It returns 0 if successful, 1 if successful but the port is
 *  throttled, and -1 if the sanity checks failed.
 */
L
Linus Torvalds 已提交
1403

1404
static int digi_read_inb_callback(struct urb *urb)
L
Linus Torvalds 已提交
1405 1406
{

1407
	struct usb_serial_port *port = urb->context;
A
Alan Cox 已提交
1408
	struct tty_struct *tty;
L
Linus Torvalds 已提交
1409 1410 1411
	struct digi_port *priv = usb_get_serial_port_data(port);
	int opcode = ((unsigned char *)urb->transfer_buffer)[0];
	int len = ((unsigned char *)urb->transfer_buffer)[1];
1412
	int port_status = ((unsigned char *)urb->transfer_buffer)[2];
A
Alan Cox 已提交
1413 1414
	unsigned char *data = ((unsigned char *)urb->transfer_buffer) + 3;
	int flag, throttled;
1415
	int status = urb->status;
L
Linus Torvalds 已提交
1416 1417 1418

	/* do not process callbacks on closed ports */
	/* but do continue the read chain */
1419
	if (urb->status == -ENOENT)
1420
		return 0;
L
Linus Torvalds 已提交
1421 1422

	/* short/multiple packet check */
1423
	if (urb->actual_length != len + 2) {
1424
		dev_err(&port->dev, "%s: INCOMPLETE OR MULTIPLE PACKET, "
1425
			"status=%d, port=%d, opcode=%d, len=%d, "
1426 1427 1428
			"actual_length=%d, status=%d\n", __func__, status,
			priv->dp_port_num, opcode, len, urb->actual_length,
			port_status);
1429
		return -1;
L
Linus Torvalds 已提交
1430 1431
	}

A
Alan Cox 已提交
1432
	tty = tty_port_tty_get(&port->port);
1433
	spin_lock(&priv->dp_port_lock);
L
Linus Torvalds 已提交
1434 1435 1436 1437

	/* check for throttle; if set, do not resubmit read urb */
	/* indicate the read chain needs to be restarted on unthrottle */
	throttled = priv->dp_throttled;
1438
	if (throttled)
L
Linus Torvalds 已提交
1439 1440 1441
		priv->dp_throttle_restart = 1;

	/* receive data */
1442
	if (tty && opcode == DIGI_CMD_RECEIVE_DATA) {
1443
		/* get flag from port_status */
L
Linus Torvalds 已提交
1444 1445 1446
		flag = 0;

		/* overrun is special, not associated with a char */
1447 1448
		if (port_status & DIGI_OVERRUN_ERROR)
			tty_insert_flip_char(tty, 0, TTY_OVERRUN);
L
Linus Torvalds 已提交
1449 1450 1451

		/* break takes precedence over parity, */
		/* which takes precedence over framing errors */
1452
		if (port_status & DIGI_BREAK_ERROR)
L
Linus Torvalds 已提交
1453
			flag = TTY_BREAK;
1454
		else if (port_status & DIGI_PARITY_ERROR)
L
Linus Torvalds 已提交
1455
			flag = TTY_PARITY;
1456
		else if (port_status & DIGI_FRAMING_ERROR)
L
Linus Torvalds 已提交
1457 1458
			flag = TTY_FRAME;

1459
		/* data length is len-1 (one byte of len is port_status) */
L
Linus Torvalds 已提交
1460
		--len;
1461
		if (len > 0) {
1462 1463
			tty_insert_flip_string_fixed_flag(tty, data, flag,
									len);
1464
			tty_flip_buffer_push(tty);
L
Linus Torvalds 已提交
1465 1466
		}
	}
1467
	spin_unlock(&priv->dp_port_lock);
A
Alan Cox 已提交
1468
	tty_kref_put(tty);
L
Linus Torvalds 已提交
1469

1470
	if (opcode == DIGI_CMD_RECEIVE_DISABLE)
1471
		dev_dbg(&port->dev, "%s: got RECEIVE_DISABLE\n", __func__);
1472
	else if (opcode != DIGI_CMD_RECEIVE_DATA)
1473
		dev_dbg(&port->dev, "%s: unknown opcode: %d\n", __func__, opcode);
L
Linus Torvalds 已提交
1474

A
Alan Cox 已提交
1475
	return throttled ? 1 : 0;
L
Linus Torvalds 已提交
1476 1477 1478 1479

}


A
Alan Cox 已提交
1480 1481 1482 1483 1484 1485 1486 1487
/*
 *  Digi Read OOB Callback
 *
 *  Digi Read OOB Callback handles reads on the out of band port.
 *  When called we know port and port->private are not NULL and
 *  the port->serial is valid.  It returns 0 if successful, and
 *  -1 if the sanity checks failed.
 */
L
Linus Torvalds 已提交
1488

1489
static int digi_read_oob_callback(struct urb *urb)
L
Linus Torvalds 已提交
1490 1491
{

1492
	struct usb_serial_port *port = urb->context;
L
Linus Torvalds 已提交
1493
	struct usb_serial *serial = port->serial;
A
Alan Cox 已提交
1494
	struct tty_struct *tty;
L
Linus Torvalds 已提交
1495 1496 1497
	struct digi_port *priv = usb_get_serial_port_data(port);
	int opcode, line, status, val;
	int i;
A
Alan Cox 已提交
1498
	unsigned int rts;
L
Linus Torvalds 已提交
1499 1500

	/* handle each oob command */
A
Alan Cox 已提交
1501
	for (i = 0; i < urb->actual_length - 3;) {
L
Linus Torvalds 已提交
1502 1503 1504 1505 1506
		opcode = ((unsigned char *)urb->transfer_buffer)[i++];
		line = ((unsigned char *)urb->transfer_buffer)[i++];
		status = ((unsigned char *)urb->transfer_buffer)[i++];
		val = ((unsigned char *)urb->transfer_buffer)[i++];

1507
		dev_dbg(&port->dev, "digi_read_oob_callback: opcode=%d, line=%d, status=%d, val=%d\n",
1508
			opcode, line, status, val);
L
Linus Torvalds 已提交
1509

1510
		if (status != 0 || line >= serial->type->num_ports)
L
Linus Torvalds 已提交
1511 1512 1513 1514
			continue;

		port = serial->port[line];

A
Alan Cox 已提交
1515 1516
		priv = usb_get_serial_port_data(port);
		if (priv == NULL)
L
Linus Torvalds 已提交
1517 1518
			return -1;

A
Alan Cox 已提交
1519
		tty = tty_port_tty_get(&port->port);
1520

A
Alan Cox 已提交
1521
		rts = 0;
1522
		if (tty)
1523
			rts = tty->termios.c_cflag & CRTSCTS;
A
Alan Cox 已提交
1524
		
1525
		if (tty && opcode == DIGI_CMD_READ_INPUT_SIGNALS) {
1526
			spin_lock(&priv->dp_port_lock);
L
Linus Torvalds 已提交
1527
			/* convert from digi flags to termiox flags */
1528
			if (val & DIGI_READ_INPUT_SIGNALS_CTS) {
L
Linus Torvalds 已提交
1529 1530
				priv->dp_modem_signals |= TIOCM_CTS;
				/* port must be open to use tty struct */
A
Alan Cox 已提交
1531
				if (rts) {
A
Alan Cox 已提交
1532
					tty->hw_stopped = 0;
1533
					digi_wakeup_write(port);
L
Linus Torvalds 已提交
1534 1535 1536 1537
				}
			} else {
				priv->dp_modem_signals &= ~TIOCM_CTS;
				/* port must be open to use tty struct */
A
Alan Cox 已提交
1538
				if (rts)
A
Alan Cox 已提交
1539
					tty->hw_stopped = 1;
L
Linus Torvalds 已提交
1540
			}
1541
			if (val & DIGI_READ_INPUT_SIGNALS_DSR)
L
Linus Torvalds 已提交
1542 1543 1544
				priv->dp_modem_signals |= TIOCM_DSR;
			else
				priv->dp_modem_signals &= ~TIOCM_DSR;
1545
			if (val & DIGI_READ_INPUT_SIGNALS_RI)
L
Linus Torvalds 已提交
1546 1547 1548
				priv->dp_modem_signals |= TIOCM_RI;
			else
				priv->dp_modem_signals &= ~TIOCM_RI;
1549
			if (val & DIGI_READ_INPUT_SIGNALS_DCD)
L
Linus Torvalds 已提交
1550 1551 1552 1553
				priv->dp_modem_signals |= TIOCM_CD;
			else
				priv->dp_modem_signals &= ~TIOCM_CD;

1554 1555 1556 1557
			wake_up_interruptible(&priv->dp_modem_change_wait);
			spin_unlock(&priv->dp_port_lock);
		} else if (opcode == DIGI_CMD_TRANSMIT_IDLE) {
			spin_lock(&priv->dp_port_lock);
L
Linus Torvalds 已提交
1558
			priv->dp_transmit_idle = 1;
1559 1560 1561 1562
			wake_up_interruptible(&priv->dp_transmit_idle_wait);
			spin_unlock(&priv->dp_port_lock);
		} else if (opcode == DIGI_CMD_IFLUSH_FIFO) {
			wake_up_interruptible(&priv->dp_flush_wait);
L
Linus Torvalds 已提交
1563
		}
A
Alan Cox 已提交
1564
		tty_kref_put(tty);
L
Linus Torvalds 已提交
1565
	}
1566
	return 0;
L
Linus Torvalds 已提交
1567 1568 1569

}

1570
module_usb_serial_driver(serial_drivers, id_table_combined);
L
Linus Torvalds 已提交
1571

1572 1573
MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_DESCRIPTION(DRIVER_DESC);
L
Linus Torvalds 已提交
1574
MODULE_LICENSE("GPL");