digi_acceleport.c 58.0 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
*  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)
* 
* (12/03/2001) gkh
A
Alan Cox 已提交
18
*	switched to using port->port.count instead of private version.
L
Linus Torvalds 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 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
*	Removed port->active
*
* (04/08/2001) gb
*	Identify version on module load.
*
* (11/01/2000) Adam J. Richter
*	usb_device_id table support
* 
* (11/01/2000) pberger and borchers
*    -- Turned off the USB_DISABLE_SPD flag for write bulk urbs--it caused
*       USB 4 ports to hang on startup.
*    -- Serialized access to write urbs by adding the dp_write_urb_in_use
*       flag; otherwise, the driver caused SMP system hangs.  Watching the
*       urb status is not sufficient.
*
* (10/05/2000) gkh
*    -- Fixed bug with urb->dev not being set properly, now that the usb
*	core needs it.
* 
*  (8/8/2000) pberger and borchers
*    -- Fixed close so that 
*       - it can timeout while waiting for transmit idle, if needed;
*       - it ignores interrupts when flushing the port, turning
*         of modem signalling, and so on;
*       - it waits for the flush to really complete before returning.
*    -- Read_bulk_callback and write_bulk_callback check for a closed
*       port before using the tty struct or writing to the port.
*    -- The two changes above fix the oops caused by interrupted closes.
*    -- Added interruptible args to write_oob_command and set_modem_signals
*       and added a timeout arg to transmit_idle; needed for fixes to
*       close.
*    -- Added code for rx_throttle and rx_unthrottle so that input flow
*       control works.
*    -- Added code to set overrun, parity, framing, and break errors
*       (untested).
*    -- Set USB_DISABLE_SPD flag for write bulk urbs, so no 0 length
*       bulk writes are done.  These hung the Digi USB device.  The
*       0 length bulk writes were a new feature of usb-uhci added in
*       the 2.4.0-test6 kernels.
*    -- Fixed mod inc race in open; do mod inc before sleeping to wait
*       for a close to finish.
*
*  (7/31/2000) pberger
*    -- Fixed bugs with hardware handshaking:
*       - Added code to set/clear tty->hw_stopped in digi_read_oob_callback()
*         and digi_set_termios()
*    -- Added code in digi_set_termios() to
*       - add conditional in code handling transition from B0 to only
*         set RTS if RTS/CTS flow control is either not in use or if
*         the port is not currently throttled.
*       - handle turning off CRTSCTS.
*
*  (7/30/2000) borchers
*    -- Added support for more than one Digi USB device by moving
*       globals to a private structure in the pointed to from the
*       usb_serial structure.
*    -- Moved the modem change and transmit idle wait queues into
*       the port private structure, so each port has its own queue
*       rather than sharing global queues.
*    -- Added support for break signals.
*
*  (7/25/2000) pberger
*    -- Added USB-2 support.  Note: the USB-2 supports 3 devices: two
*       serial and a parallel port.  The parallel port is implemented
*       as a serial-to-parallel converter.  That is, the driver actually
*       presents all three USB-2 interfaces as serial ports, but the third
*       one physically connects to a parallel device.  Thus, for example,
*       one could plug a parallel printer into the USB-2's third port,
*       but from the kernel's (and userland's) point of view what's
*       actually out there is a serial device.
*
*  (7/15/2000) borchers
*    -- Fixed race in open when a close is in progress.
*    -- Keep count of opens and dec the module use count for each
*       outstanding open when shutdown is called (on disconnect).
*    -- Fixed sanity checks in read_bulk_callback and write_bulk_callback
*       so pointers are checked before use.
*    -- Split read bulk callback into in band and out of band
*       callbacks, and no longer restart read chains if there is
*       a status error or a sanity error.  This fixed the seg
*       faults and other errors we used to get on disconnect.
*    -- Port->active is once again a flag as usb-serial intended it
*       to be, not a count.  Since it was only a char it would
*       have been limited to 256 simultaneous opens.  Now the open
*       count is kept in the port private structure in dp_open_count.
*    -- Added code for modularization of the digi_acceleport driver.
*
*  (6/27/2000) pberger and borchers
*    -- Zeroed out sync field in the wakeup_task before first use;
*       otherwise the uninitialized value might prevent the task from
*       being scheduled.
*    -- Initialized ret value to 0 in write_bulk_callback, otherwise
*       the uninitialized value could cause a spurious debugging message.
*
*  (6/22/2000) pberger and borchers
*    -- Made cond_wait_... inline--apparently on SPARC the flags arg
*       to spin_lock_irqsave cannot be passed to another function
*       to call spin_unlock_irqrestore.  Thanks to Pauline Middelink.
*    -- In digi_set_modem_signals the inner nested spin locks use just
*       spin_lock() rather than spin_lock_irqsave().  The old code
*       mistakenly left interrupts off.  Thanks to Pauline Middelink.
*    -- copy_from_user (which can sleep) is no longer called while a
*       spinlock is held.  We copy to a local buffer before getting
*       the spinlock--don't like the extra copy but the code is simpler.
*    -- Printk and dbg are no longer called while a spin lock is held.
*
*  (6/4/2000) pberger and borchers
*    -- Replaced separate calls to spin_unlock_irqrestore and
*       interruptible_sleep_on_timeout with a new function
*       cond_wait_interruptible_timeout_irqrestore.  This eliminates
*       the race condition where the wake up could happen after
*       the unlock and before the sleep.
*    -- Close now waits for output to drain.
*    -- Open waits until any close in progress is finished.
*    -- All out of band responses are now processed, not just the
*       first in a USB packet.
*    -- Fixed a bug that prevented the driver from working when the
*       first Digi port was not the first USB serial port--the driver
*       was mistakenly using the external USB serial port number to
*       try to index into its internal ports.
*    -- Fixed an SMP bug -- write_bulk_callback is called directly from
*       an interrupt, so spin_lock_irqsave/spin_unlock_irqrestore are
*       needed for locks outside write_bulk_callback that are also
*       acquired by write_bulk_callback to prevent deadlocks.
*    -- Fixed support for select() by making digi_chars_in_buffer()
*       return 256 when -EINPROGRESS is set, as the line discipline
*       code in n_tty.c expects.
*    -- Fixed an include file ordering problem that prevented debugging
*       messages from working.
*    -- Fixed an intermittent timeout problem that caused writes to
*       sometimes get stuck on some machines on some kernels.  It turns
*       out in these circumstances write_chan() (in n_tty.c) was
*       asleep waiting for our wakeup call.  Even though we call
*       wake_up_interruptible() in digi_write_bulk_callback(), there is
*       a race condition that could cause the wakeup to fail: if our
*       wake_up_interruptible() call occurs between the time that our
*       driver write routine finishes and write_chan() sets current->state
*       to TASK_INTERRUPTIBLE, the effect of our wakeup setting the state
*       to TASK_RUNNING will be lost and write_chan's subsequent call to
*       schedule() will never return (unless it catches a signal).
*       This race condition occurs because write_bulk_callback() (and thus
M
Matt LaPlante 已提交
160
*       the wakeup) are called asynchronously from an interrupt, rather than
L
Linus Torvalds 已提交
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 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
*       from the scheduler.  We can avoid the race by calling the wakeup
*       from the scheduler queue and that's our fix:  Now, at the end of
*       write_bulk_callback() we queue up a wakeup call on the scheduler
*       task queue.  We still also invoke the wakeup directly since that
*       squeezes a bit more performance out of the driver, and any lost
*       race conditions will get cleaned up at the next scheduler run.
*
*       NOTE:  The problem also goes away if you comment out
*       the two code lines in write_chan() where current->state
*       is set to TASK_RUNNING just before calling driver.write() and to
*       TASK_INTERRUPTIBLE immediately afterwards.  This is why the
*       problem did not show up with the 2.2 kernels -- they do not
*       include that code.
*
*  (5/16/2000) pberger and borchers
*    -- Added timeouts to sleeps, to defend against lost wake ups.
*    -- Handle transition to/from B0 baud rate in digi_set_termios.
*
*  (5/13/2000) pberger and borchers
*    -- All commands now sent on out of band port, using
*       digi_write_oob_command.
*    -- Get modem control signals whenever they change, support TIOCMGET/
*       SET/BIS/BIC ioctls.
*    -- digi_set_termios now supports parity, word size, stop bits, and
*       receive enable.
*    -- Cleaned up open and close, use digi_set_termios and
*       digi_write_oob_command to set port parameters.
*    -- Added digi_startup_device to start read chains on all ports.
*    -- Write buffer is only used when count==1, to be sure put_char can
*       write a char (unless the buffer is full).
*
*  (5/10/2000) pberger and borchers
*    -- Added MOD_INC_USE_COUNT/MOD_DEC_USE_COUNT calls on open/close.
*    -- Fixed problem where the first incoming character is lost on
*       port opens after the first close on that port.  Now we keep
*       the read_urb chain open until shutdown.
*    -- Added more port conditioning calls in digi_open and digi_close.
*    -- Convert port->active to a use count so that we can deal with multiple
*       opens and closes properly.
*    -- Fixed some problems with the locking code.
*
*  (5/3/2000) pberger and borchers
*    -- First alpha version of the driver--many known limitations and bugs.
*
*
*  Locking and SMP
*
*  - Each port, including the out-of-band port, has a lock used to
*    serialize all access to the port's private structure.
*  - The port lock is also used to serialize all writes and access to
*    the port's URB.
*  - The port lock is also used for the port write_wait condition
*    variable.  Holding the port lock will prevent a wake up on the
*    port's write_wait; this can be used with cond_wait_... to be sure
*    the wake up is not lost in a race when dropping the lock and
*    sleeping waiting for the wakeup.
*  - digi_write() does not sleep, since it is sometimes called on
*    interrupt time.
*  - digi_write_bulk_callback() and digi_read_bulk_callback() are
*    called directly from interrupts.  Hence spin_lock_irqsave()
*    and spin_unlock_irqrestore() are used in the rest of the code
*    for any locks they acquire.
*  - digi_write_bulk_callback() gets the port lock before waking up
*    processes sleeping on the port write_wait.  It also schedules
*    wake ups so they happen from the scheduler, because the tty
*    system can miss wake ups from interrupts.
*  - All sleeps use a timeout of DIGI_RETRY_TIMEOUT before looping to
*    recheck the condition they are sleeping on.  This is defensive,
*    in case a wake up is lost.
*  - Following Documentation/DocBook/kernel-locking.pdf no spin locks
*    are held when calling copy_to/from_user or printk.
*/

#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 已提交
244
#include <linux/uaccess.h>
L
Linus Torvalds 已提交
245 246
#include <linux/usb.h>
#include <linux/wait.h>
247
#include <linux/usb/serial.h>
L
Linus Torvalds 已提交
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427

/* 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;
	int dp_in_close;			/* close in progress */
	wait_queue_head_t dp_close_wait;	/* wait queue for close */
	struct work_struct dp_wakeup_work;
D
David Howells 已提交
428
	struct usb_serial_port *dp_port;
L
Linus Torvalds 已提交
429 430 431 432 433
};


/* Local Function Declarations */

434
static void digi_wakeup_write(struct usb_serial_port *port);
D
David Howells 已提交
435
static void digi_wakeup_write_lock(struct work_struct *work);
436 437 438 439 440 441 442 443
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 已提交
444 445
static void digi_rx_throttle(struct tty_struct *tty);
static void digi_rx_unthrottle(struct tty_struct *tty);
A
Alan Cox 已提交
446 447 448 449 450
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);
static int digi_tiocmget(struct tty_struct *tty, struct file *file);
static int digi_tiocmset(struct tty_struct *tty, struct file *file,
451
	unsigned int set, unsigned int clear);
A
Alan Cox 已提交
452 453
static int digi_write(struct tty_struct *tty, struct usb_serial_port *port,
	const unsigned char *buf, int count);
454
static void digi_write_bulk_callback(struct urb *urb);
A
Alan Cox 已提交
455 456 457 458 459 460
static int digi_write_room(struct tty_struct *tty);
static int digi_chars_in_buffer(struct tty_struct *tty);
static int digi_open(struct tty_struct *tty, struct usb_serial_port *port,
	struct file *filp);
static void digi_close(struct tty_struct *tty, struct usb_serial_port *port,
	struct file *filp);
461 462 463 464 465 466
static int digi_startup_device(struct usb_serial *serial);
static int digi_startup(struct usb_serial *serial);
static void digi_shutdown(struct usb_serial *serial);
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 已提交
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488


/* Statics */

static int debug;

static struct usb_device_id id_table_combined [] = {
	{ USB_DEVICE(DIGI_VENDOR_ID, DIGI_2_ID) },
	{ USB_DEVICE(DIGI_VENDOR_ID, DIGI_4_ID) },
	{ }						/* Terminating entry */
};

static struct usb_device_id id_table_2 [] = {
	{ USB_DEVICE(DIGI_VENDOR_ID, DIGI_2_ID) },
	{ }						/* Terminating entry */
};

static struct usb_device_id id_table_4 [] = {
	{ USB_DEVICE(DIGI_VENDOR_ID, DIGI_4_ID) },
	{ }						/* Terminating entry */
};

A
Alan Cox 已提交
489
MODULE_DEVICE_TABLE(usb, id_table_combined);
L
Linus Torvalds 已提交
490 491 492 493 494 495

static struct usb_driver digi_driver = {
	.name =		"digi_acceleport",
	.probe =	usb_serial_probe,
	.disconnect =	usb_serial_disconnect,
	.id_table =	id_table_combined,
496
	.no_dynamic_id = 	1,
L
Linus Torvalds 已提交
497 498 499 500 501
};


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

502
static struct usb_serial_driver digi_acceleport_2_device = {
503
	.driver = {
504 505
		.owner =		THIS_MODULE,
		.name =			"digi_2",
506
	},
507
	.description =			"Digi 2 port USB adapter",
508
	.usb_driver = 			&digi_driver,
L
Linus Torvalds 已提交
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
	.id_table =			id_table_2,
	.num_ports =			3,
	.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,
	.shutdown =			digi_shutdown,
};

528
static struct usb_serial_driver digi_acceleport_4_device = {
529
	.driver = {
530 531
		.owner =		THIS_MODULE,
		.name =			"digi_4",
532
	},
533
	.description =			"Digi 4 port USB adapter",
534
	.usb_driver = 			&digi_driver,
L
Linus Torvalds 已提交
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
	.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,
	.shutdown =			digi_shutdown,
};


/* Functions */

/*
A
Alan Cox 已提交
558 559 560 561 562 563 564 565 566 567 568
 *  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 已提交
569

570
static long cond_wait_interruptible_timeout_irqrestore(
L
Linus Torvalds 已提交
571
	wait_queue_head_t *q, long timeout,
572
	spinlock_t *lock, unsigned long flags)
573
__releases(lock)
L
Linus Torvalds 已提交
574 575 576
{
	DEFINE_WAIT(wait);

577
	prepare_to_wait(q, &wait, TASK_INTERRUPTIBLE);
L
Linus Torvalds 已提交
578 579 580 581 582 583 584 585 586
	spin_unlock_irqrestore(lock, flags);
	timeout = schedule_timeout(timeout);
	finish_wait(q, &wait);

	return timeout;
}


/*
A
Alan Cox 已提交
587 588 589 590 591
 *  Digi Wakeup Write
 *
 *  Wake up port, line discipline, and tty processes sleeping
 *  on writes.
 */
L
Linus Torvalds 已提交
592

D
David Howells 已提交
593
static void digi_wakeup_write_lock(struct work_struct *work)
L
Linus Torvalds 已提交
594
{
A
Alan Cox 已提交
595 596
	struct digi_port *priv =
			container_of(work, struct digi_port, dp_wakeup_work);
D
David Howells 已提交
597
	struct usb_serial_port *port = priv->dp_port;
L
Linus Torvalds 已提交
598 599
	unsigned long flags;

600 601 602
	spin_lock_irqsave(&priv->dp_port_lock, flags);
	digi_wakeup_write(port);
	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
L
Linus Torvalds 已提交
603 604
}

605
static void digi_wakeup_write(struct usb_serial_port *port)
L
Linus Torvalds 已提交
606
{
A
Alan Cox 已提交
607 608 609
	struct tty_struct *tty = tty_port_tty_get(&port->port);
	tty_wakeup(tty);
	tty_kref_put(tty);
L
Linus Torvalds 已提交
610 611 612 613
}


/*
A
Alan Cox 已提交
614 615 616 617 618 619 620 621 622
 *  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 已提交
623

624 625
static int digi_write_oob_command(struct usb_serial_port *port,
	unsigned char *buf, int count, int interruptible)
L
Linus Torvalds 已提交
626 627 628 629 630 631 632 633
{

	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;

634
	dbg("digi_write_oob_command: TOP: port=%d, count=%d", oob_priv->dp_port_num, count);
L
Linus Torvalds 已提交
635

636
	spin_lock_irqsave(&oob_priv->dp_port_lock, flags);
A
Alan Cox 已提交
637 638
	while (count > 0) {
		while (oob_port->write_urb->status == -EINPROGRESS
639
			|| oob_priv->dp_write_urb_in_use) {
L
Linus Torvalds 已提交
640 641
			cond_wait_interruptible_timeout_irqrestore(
				&oob_port->write_wait, DIGI_RETRY_TIMEOUT,
642 643 644 645
				&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 已提交
646 647 648
		}

		/* len must be a multiple of 4, so commands are not split */
649 650
		len = min(count, oob_port->bulk_out_size);
		if (len > 4)
L
Linus Torvalds 已提交
651
			len &= ~3;
652
		memcpy(oob_port->write_urb->transfer_buffer, buf, len);
L
Linus Torvalds 已提交
653 654
		oob_port->write_urb->transfer_buffer_length = len;
		oob_port->write_urb->dev = port->serial->dev;
A
Alan Cox 已提交
655 656
		ret = usb_submit_urb(oob_port->write_urb, GFP_ATOMIC);
		if (ret == 0) {
L
Linus Torvalds 已提交
657 658 659 660 661
			oob_priv->dp_write_urb_in_use = 1;
			count -= len;
			buf += len;
		}
	}
662 663
	spin_unlock_irqrestore(&oob_priv->dp_port_lock, flags);
	if (ret)
664
		err("%s: usb_submit_urb failed, ret=%d", __func__, ret);
665
	return ret;
L
Linus Torvalds 已提交
666 667 668 669 670

}


/*
A
Alan Cox 已提交
671 672 673 674 675 676 677 678 679 680
 *  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 已提交
681

682 683
static int digi_write_inb_command(struct usb_serial_port *port,
	unsigned char *buf, int count, unsigned long timeout)
L
Linus Torvalds 已提交
684 685 686 687 688 689 690
{
	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;

691 692
	dbg("digi_write_inb_command: TOP: port=%d, count=%d",
		priv->dp_port_num, count);
L
Linus Torvalds 已提交
693

694
	if (timeout)
L
Linus Torvalds 已提交
695 696 697 698
		timeout += jiffies;
	else
		timeout = ULONG_MAX;

699
	spin_lock_irqsave(&priv->dp_port_lock, flags);
A
Alan Cox 已提交
700 701 702 703
	while (count > 0 && ret == 0) {
		while ((port->write_urb->status == -EINPROGRESS
				|| priv->dp_write_urb_in_use)
					&& time_before(jiffies, timeout)) {
L
Linus Torvalds 已提交
704 705
			cond_wait_interruptible_timeout_irqrestore(
				&port->write_wait, DIGI_RETRY_TIMEOUT,
706 707 708 709
				&priv->dp_port_lock, flags);
			if (signal_pending(current))
				return -EINTR;
			spin_lock_irqsave(&priv->dp_port_lock, flags);
L
Linus Torvalds 已提交
710 711 712 713 714
		}

		/* 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 */
715 716
		len = min(count, port->bulk_out_size-2-priv->dp_out_buf_len);
		if (len > 4)
L
Linus Torvalds 已提交
717 718 719
			len &= ~3;

		/* write any buffered data first */
720
		if (priv->dp_out_buf_len > 0) {
L
Linus Torvalds 已提交
721 722
			data[0] = DIGI_CMD_SEND_DATA;
			data[1] = priv->dp_out_buf_len;
723 724 725
			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 已提交
726
			port->write_urb->transfer_buffer_length
727
				= priv->dp_out_buf_len + 2 + len;
L
Linus Torvalds 已提交
728
		} else {
729
			memcpy(data, buf, len);
L
Linus Torvalds 已提交
730 731 732 733
			port->write_urb->transfer_buffer_length = len;
		}
		port->write_urb->dev = port->serial->dev;

A
Alan Cox 已提交
734 735
		ret = usb_submit_urb(port->write_urb, GFP_ATOMIC);
		if (ret == 0) {
L
Linus Torvalds 已提交
736 737 738 739 740 741 742
			priv->dp_write_urb_in_use = 1;
			priv->dp_out_buf_len = 0;
			count -= len;
			buf += len;
		}

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

745 746
	if (ret)
		err("%s: usb_submit_urb failed, ret=%d, port=%d",
747
			__func__, ret, priv->dp_port_num);
748
	return ret;
L
Linus Torvalds 已提交
749 750 751 752
}


/*
A
Alan Cox 已提交
753 754 755 756 757 758 759 760
 *  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 已提交
761

762 763
static int digi_set_modem_signals(struct usb_serial_port *port,
	unsigned int modem_signals, int interruptible)
L
Linus Torvalds 已提交
764 765 766 767
{

	int ret;
	struct digi_port *port_priv = usb_get_serial_port_data(port);
A
Alan Cox 已提交
768
	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 已提交
769 770 771 772 773
	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;


774 775
	dbg("digi_set_modem_signals: TOP: port=%d, modem_signals=0x%x",
		port_priv->dp_port_num, modem_signals);
L
Linus Torvalds 已提交
776

777 778
	spin_lock_irqsave(&oob_priv->dp_port_lock, flags);
	spin_lock(&port_priv->dp_port_lock);
L
Linus Torvalds 已提交
779

A
Alan Cox 已提交
780 781
	while (oob_port->write_urb->status == -EINPROGRESS ||
					oob_priv->dp_write_urb_in_use) {
782
		spin_unlock(&port_priv->dp_port_lock);
L
Linus Torvalds 已提交
783 784
		cond_wait_interruptible_timeout_irqrestore(
			&oob_port->write_wait, DIGI_RETRY_TIMEOUT,
785 786 787 788 789
			&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 已提交
790 791 792
	}
	data[0] = DIGI_CMD_SET_DTR_SIGNAL;
	data[1] = port_priv->dp_port_num;
A
Alan Cox 已提交
793 794
	data[2] = (modem_signals & TIOCM_DTR) ?
					DIGI_DTR_ACTIVE : DIGI_DTR_INACTIVE;
L
Linus Torvalds 已提交
795 796 797
	data[3] = 0;
	data[4] = DIGI_CMD_SET_RTS_SIGNAL;
	data[5] = port_priv->dp_port_num;
A
Alan Cox 已提交
798 799
	data[6] = (modem_signals & TIOCM_RTS) ?
					DIGI_RTS_ACTIVE : DIGI_RTS_INACTIVE;
L
Linus Torvalds 已提交
800 801 802 803 804
	data[7] = 0;

	oob_port->write_urb->transfer_buffer_length = 8;
	oob_port->write_urb->dev = port->serial->dev;

A
Alan Cox 已提交
805 806
	ret = usb_submit_urb(oob_port->write_urb, GFP_ATOMIC);
	if (ret == 0) {
L
Linus Torvalds 已提交
807 808 809 810 811
		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));
	}
812 813 814
	spin_unlock(&port_priv->dp_port_lock);
	spin_unlock_irqrestore(&oob_priv->dp_port_lock, flags);
	if (ret)
815
		err("%s: usb_submit_urb failed, ret=%d", __func__, ret);
816
	return ret;
L
Linus Torvalds 已提交
817 818 819
}

/*
A
Alan Cox 已提交
820 821 822 823 824 825 826 827 828 829
 *  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 已提交
830

831 832
static int digi_transmit_idle(struct usb_serial_port *port,
	unsigned long timeout)
L
Linus Torvalds 已提交
833 834 835 836 837 838
{
	int ret;
	unsigned char buf[2];
	struct digi_port *priv = usb_get_serial_port_data(port);
	unsigned long flags = 0;

839
	spin_lock_irqsave(&priv->dp_port_lock, flags);
L
Linus Torvalds 已提交
840
	priv->dp_transmit_idle = 0;
841
	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
L
Linus Torvalds 已提交
842 843 844 845 846 847

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

	timeout += jiffies;

A
Alan Cox 已提交
848 849
	ret = digi_write_inb_command(port, buf, 2, timeout - jiffies);
	if (ret != 0)
850
		return ret;
L
Linus Torvalds 已提交
851

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

A
Alan Cox 已提交
854
	while (time_before(jiffies, timeout) && !priv->dp_transmit_idle) {
L
Linus Torvalds 已提交
855 856
		cond_wait_interruptible_timeout_irqrestore(
			&priv->dp_transmit_idle_wait, DIGI_RETRY_TIMEOUT,
857 858 859 860
			&priv->dp_port_lock, flags);
		if (signal_pending(current))
			return -EINTR;
		spin_lock_irqsave(&priv->dp_port_lock, flags);
L
Linus Torvalds 已提交
861 862
	}
	priv->dp_transmit_idle = 0;
863 864
	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
	return 0;
L
Linus Torvalds 已提交
865 866 867 868

}


A
Alan Cox 已提交
869
static void digi_rx_throttle(struct tty_struct *tty)
L
Linus Torvalds 已提交
870 871
{
	unsigned long flags;
A
Alan Cox 已提交
872
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
873 874 875
	struct digi_port *priv = usb_get_serial_port_data(port);


876
	dbg("digi_rx_throttle: TOP: port=%d", priv->dp_port_num);
L
Linus Torvalds 已提交
877 878

	/* stop receiving characters by not resubmitting the read urb */
879
	spin_lock_irqsave(&priv->dp_port_lock, flags);
L
Linus Torvalds 已提交
880 881
	priv->dp_throttled = 1;
	priv->dp_throttle_restart = 0;
882
	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
L
Linus Torvalds 已提交
883 884 885
}


A
Alan Cox 已提交
886
static void digi_rx_unthrottle(struct tty_struct *tty)
L
Linus Torvalds 已提交
887 888 889
{
	int ret = 0;
	unsigned long flags;
A
Alan Cox 已提交
890
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
891 892
	struct digi_port *priv = usb_get_serial_port_data(port);

893
	dbg("digi_rx_unthrottle: TOP: port=%d", priv->dp_port_num);
L
Linus Torvalds 已提交
894

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

897 898 899
	/* turn throttle off */
	priv->dp_throttled = 0;
	priv->dp_throttle_restart = 0;
L
Linus Torvalds 已提交
900 901

	/* restart read chain */
902
	if (priv->dp_throttle_restart) {
L
Linus Torvalds 已提交
903
		port->read_urb->dev = port->serial->dev;
904
		ret = usb_submit_urb(port->read_urb, GFP_ATOMIC);
L
Linus Torvalds 已提交
905 906
	}

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

909 910
	if (ret)
		err("%s: usb_submit_urb failed, ret=%d, port=%d",
911
			__func__, ret, priv->dp_port_num);
L
Linus Torvalds 已提交
912 913 914
}


A
Alan Cox 已提交
915
static void digi_set_termios(struct tty_struct *tty,
A
Alan Cox 已提交
916
		struct usb_serial_port *port, struct ktermios *old_termios)
L
Linus Torvalds 已提交
917 918
{
	struct digi_port *priv = usb_get_serial_port_data(port);
919 920
	unsigned int iflag = tty->termios->c_iflag;
	unsigned int cflag = tty->termios->c_cflag;
L
Linus Torvalds 已提交
921 922 923 924
	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 已提交
925
	int arg, ret;
L
Linus Torvalds 已提交
926
	int i = 0;
927
	speed_t baud;
L
Linus Torvalds 已提交
928

929
	dbg("digi_set_termios: TOP: port=%d, iflag=0x%x, old_iflag=0x%x, cflag=0x%x, old_cflag=0x%x", priv->dp_port_num, iflag, old_iflag, cflag, old_cflag);
L
Linus Torvalds 已提交
930 931

	/* set baud rate */
A
Alan Cox 已提交
932 933
	baud = tty_get_baud_rate(tty);
	if (baud != tty_termios_baud_rate(old_termios)) {
L
Linus Torvalds 已提交
934 935 936
		arg = -1;

		/* reassert DTR and (maybe) RTS on transition from B0 */
937
		if ((old_cflag&CBAUD) == B0) {
L
Linus Torvalds 已提交
938 939 940
			/* don't set RTS if using hardware flow control */
			/* and throttling input */
			modem_signals = TIOCM_DTR;
941 942
			if (!(tty->termios->c_cflag & CRTSCTS) ||
			    !test_bit(TTY_THROTTLED, &tty->flags))
L
Linus Torvalds 已提交
943
				modem_signals |= TIOCM_RTS;
944
			digi_set_modem_signals(port, modem_signals, 1);
L
Linus Torvalds 已提交
945
		}
946
		switch (baud) {
A
Alan Cox 已提交
947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970
		/* 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 已提交
971
		}
972
		if (arg != -1) {
L
Linus Torvalds 已提交
973 974 975 976 977 978 979
			buf[i++] = DIGI_CMD_SET_BAUD_RATE;
			buf[i++] = priv->dp_port_num;
			buf[i++] = arg;
			buf[i++] = 0;
		}
	}
	/* set parity */
980 981
	tty->termios->c_cflag &= ~CMSPAR;

982 983 984
	if ((cflag&(PARENB|PARODD)) != (old_cflag&(PARENB|PARODD))) {
		if (cflag&PARENB) {
			if (cflag&PARODD)
L
Linus Torvalds 已提交
985 986 987 988 989 990 991 992 993 994 995 996
				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 */
997
	if ((cflag&CSIZE) != (old_cflag&CSIZE)) {
L
Linus Torvalds 已提交
998
		arg = -1;
999
		switch (cflag&CSIZE) {
L
Linus Torvalds 已提交
1000 1001 1002 1003 1004
		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:
1005 1006
			dbg("digi_set_termios: can't handle word size %d",
				(cflag&CSIZE));
L
Linus Torvalds 已提交
1007 1008 1009
			break;
		}

1010
		if (arg != -1) {
L
Linus Torvalds 已提交
1011 1012 1013 1014 1015 1016 1017 1018 1019
			buf[i++] = DIGI_CMD_SET_WORD_SIZE;
			buf[i++] = priv->dp_port_num;
			buf[i++] = arg;
			buf[i++] = 0;
		}

	}

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

1022
		if ((cflag&CSTOPB))
L
Linus Torvalds 已提交
1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034
			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 */
1035 1036
	if ((iflag&IXOFF) != (old_iflag&IXOFF)
	    || (cflag&CRTSCTS) != (old_cflag&CRTSCTS)) {
L
Linus Torvalds 已提交
1037
		arg = 0;
1038
		if (iflag&IXOFF)
L
Linus Torvalds 已提交
1039 1040 1041 1042
			arg |= DIGI_INPUT_FLOW_CONTROL_XON_XOFF;
		else
			arg &= ~DIGI_INPUT_FLOW_CONTROL_XON_XOFF;

1043
		if (cflag&CRTSCTS) {
L
Linus Torvalds 已提交
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062
			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 */
1063 1064
	if ((iflag & IXON) != (old_iflag & IXON)
	    || (cflag & CRTSCTS) != (old_cflag & CRTSCTS)) {
L
Linus Torvalds 已提交
1065
		arg = 0;
1066
		if (iflag & IXON)
L
Linus Torvalds 已提交
1067 1068 1069 1070
			arg |= DIGI_OUTPUT_FLOW_CONTROL_XON_XOFF;
		else
			arg &= ~DIGI_OUTPUT_FLOW_CONTROL_XON_XOFF;

1071
		if (cflag & CRTSCTS) {
L
Linus Torvalds 已提交
1072 1073 1074
			arg |= DIGI_OUTPUT_FLOW_CONTROL_CTS;
		} else {
			arg &= ~DIGI_OUTPUT_FLOW_CONTROL_CTS;
1075
			tty->hw_stopped = 0;
L
Linus Torvalds 已提交
1076 1077 1078 1079 1080 1081 1082 1083 1084
		}

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

	/* set receive enable/disable */
1085 1086
	if ((cflag & CREAD) != (old_cflag & CREAD)) {
		if (cflag & CREAD)
L
Linus Torvalds 已提交
1087 1088 1089 1090 1091 1092 1093 1094 1095
			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 已提交
1096 1097
	ret = digi_write_oob_command(port, buf, i, 1);
	if (ret != 0)
1098
		dbg("digi_set_termios: write oob failed, ret=%d", ret);
1099
	tty_encode_baud_rate(tty, baud, baud);
L
Linus Torvalds 已提交
1100 1101 1102
}


A
Alan Cox 已提交
1103
static void digi_break_ctl(struct tty_struct *tty, int break_state)
L
Linus Torvalds 已提交
1104
{
A
Alan Cox 已提交
1105
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
1106 1107 1108 1109 1110 1111
	unsigned char buf[4];

	buf[0] = DIGI_CMD_BREAK_CONTROL;
	buf[1] = 2;				/* length */
	buf[2] = break_state ? 1 : 0;
	buf[3] = 0;				/* pad */
1112
	digi_write_inb_command(port, buf, 4, 0);
L
Linus Torvalds 已提交
1113 1114 1115
}


A
Alan Cox 已提交
1116
static int digi_tiocmget(struct tty_struct *tty, struct file *file)
L
Linus Torvalds 已提交
1117
{
A
Alan Cox 已提交
1118
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
1119 1120 1121 1122
	struct digi_port *priv = usb_get_serial_port_data(port);
	unsigned int val;
	unsigned long flags;

1123
	dbg("%s: TOP: port=%d", __func__, priv->dp_port_num);
L
Linus Torvalds 已提交
1124

1125
	spin_lock_irqsave(&priv->dp_port_lock, flags);
L
Linus Torvalds 已提交
1126
	val = priv->dp_modem_signals;
1127
	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
L
Linus Torvalds 已提交
1128 1129 1130 1131
	return val;
}


A
Alan Cox 已提交
1132
static int digi_tiocmset(struct tty_struct *tty, struct file *file,
1133
	unsigned int set, unsigned int clear)
L
Linus Torvalds 已提交
1134
{
A
Alan Cox 已提交
1135
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
1136 1137 1138 1139
	struct digi_port *priv = usb_get_serial_port_data(port);
	unsigned int val;
	unsigned long flags;

1140
	dbg("%s: TOP: port=%d", __func__, priv->dp_port_num);
L
Linus Torvalds 已提交
1141

1142
	spin_lock_irqsave(&priv->dp_port_lock, flags);
L
Linus Torvalds 已提交
1143
	val = (priv->dp_modem_signals & ~clear) | set;
1144 1145
	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
	return digi_set_modem_signals(port, val, 1);
L
Linus Torvalds 已提交
1146 1147 1148
}


A
Alan Cox 已提交
1149 1150
static int digi_write(struct tty_struct *tty, struct usb_serial_port *port,
					const unsigned char *buf, int count)
L
Linus Torvalds 已提交
1151 1152
{

A
Alan Cox 已提交
1153
	int ret, data_len, new_len;
L
Linus Torvalds 已提交
1154 1155 1156 1157
	struct digi_port *priv = usb_get_serial_port_data(port);
	unsigned char *data = port->write_urb->transfer_buffer;
	unsigned long flags = 0;

1158 1159
	dbg("digi_write: TOP: port=%d, count=%d, in_interrupt=%ld",
		priv->dp_port_num, count, in_interrupt());
L
Linus Torvalds 已提交
1160 1161

	/* copy user data (which can sleep) before getting spin lock */
1162 1163
	count = min(count, port->bulk_out_size-2);
	count = min(64, count);
L
Linus Torvalds 已提交
1164 1165 1166 1167

	/* be sure only one write proceeds at a time */
	/* there are races on the port private buffer */
	/* and races to check write_urb->status */
1168
	spin_lock_irqsave(&priv->dp_port_lock, flags);
L
Linus Torvalds 已提交
1169 1170

	/* wait for urb status clear to submit another urb */
A
Alan Cox 已提交
1171 1172
	if (port->write_urb->status == -EINPROGRESS ||
					priv->dp_write_urb_in_use) {
L
Linus Torvalds 已提交
1173
		/* buffer data if count is 1 (probably put_char) if possible */
1174
		if (count == 1 && priv->dp_out_buf_len < DIGI_OUT_BUF_SIZE) {
L
Linus Torvalds 已提交
1175 1176 1177 1178 1179
			priv->dp_out_buf[priv->dp_out_buf_len++] = *buf;
			new_len = 1;
		} else {
			new_len = 0;
		}
1180 1181
		spin_unlock_irqrestore(&priv->dp_port_lock, flags);
		return new_len;
L
Linus Torvalds 已提交
1182 1183 1184 1185 1186 1187 1188
	}

	/* 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;

1189 1190 1191
	if (data_len == 0) {
		spin_unlock_irqrestore(&priv->dp_port_lock, flags);
		return 0;
L
Linus Torvalds 已提交
1192 1193 1194 1195 1196 1197 1198 1199 1200
	}

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

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

	/* copy in buffered data first */
1201
	memcpy(data, priv->dp_out_buf, priv->dp_out_buf_len);
L
Linus Torvalds 已提交
1202 1203 1204
	data += priv->dp_out_buf_len;

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

A
Alan Cox 已提交
1207 1208
	ret = usb_submit_urb(port->write_urb, GFP_ATOMIC);
	if (ret == 0) {
L
Linus Torvalds 已提交
1209 1210 1211 1212 1213 1214
		priv->dp_write_urb_in_use = 1;
		ret = new_len;
		priv->dp_out_buf_len = 0;
	}

	/* return length of new data written, or error */
1215 1216 1217
	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
	if (ret < 0)
		err("%s: usb_submit_urb failed, ret=%d, port=%d",
1218
			__func__, ret, priv->dp_port_num);
1219 1220
	dbg("digi_write: returning %d", ret);
	return ret;
L
Linus Torvalds 已提交
1221

A
Alan Cox 已提交
1222
}
L
Linus Torvalds 已提交
1223

1224
static void digi_write_bulk_callback(struct urb *urb)
L
Linus Torvalds 已提交
1225 1226
{

1227
	struct usb_serial_port *port = urb->context;
L
Linus Torvalds 已提交
1228 1229 1230 1231
	struct usb_serial *serial;
	struct digi_port *priv;
	struct digi_serial *serial_priv;
	int ret = 0;
1232
	int status = urb->status;
L
Linus Torvalds 已提交
1233

1234
	dbg("digi_write_bulk_callback: TOP, urb->status=%d", status);
L
Linus Torvalds 已提交
1235 1236

	/* port and serial sanity check */
A
Alan Cox 已提交
1237
	if (port == NULL || (priv = usb_get_serial_port_data(port)) == NULL) {
1238
		err("%s: port or port->private is NULL, status=%d",
1239
		    __func__, status);
L
Linus Torvalds 已提交
1240 1241 1242
		return;
	}
	serial = port->serial;
A
Alan Cox 已提交
1243
	if (serial == NULL || (serial_priv = usb_get_serial_data(serial)) == NULL) {
1244
		err("%s: serial or serial->private is NULL, status=%d",
1245
		    __func__, status);
L
Linus Torvalds 已提交
1246 1247 1248 1249
		return;
	}

	/* handle oob callback */
1250 1251 1252
	if (priv->dp_port_num == serial_priv->ds_oob_port_num) {
		dbg("digi_write_bulk_callback: oob callback");
		spin_lock(&priv->dp_port_lock);
L
Linus Torvalds 已提交
1253
		priv->dp_write_urb_in_use = 0;
1254 1255
		wake_up_interruptible(&port->write_wait);
		spin_unlock(&priv->dp_port_lock);
L
Linus Torvalds 已提交
1256 1257 1258 1259
		return;
	}

	/* try to send any buffered data on this port, if it is open */
1260
	spin_lock(&priv->dp_port_lock);
L
Linus Torvalds 已提交
1261
	priv->dp_write_urb_in_use = 0;
A
Alan Cox 已提交
1262
	if (port->port.count && port->write_urb->status != -EINPROGRESS
1263
	    && priv->dp_out_buf_len > 0) {
L
Linus Torvalds 已提交
1264 1265
		*((unsigned char *)(port->write_urb->transfer_buffer))
			= (unsigned char)DIGI_CMD_SEND_DATA;
A
Alan Cox 已提交
1266
		*((unsigned char *)(port->write_urb->transfer_buffer) + 1)
L
Linus Torvalds 已提交
1267
			= (unsigned char)priv->dp_out_buf_len;
A
Alan Cox 已提交
1268 1269
		port->write_urb->transfer_buffer_length =
						priv->dp_out_buf_len + 2;
L
Linus Torvalds 已提交
1270
		port->write_urb->dev = serial->dev;
A
Alan Cox 已提交
1271
		memcpy(port->write_urb->transfer_buffer + 2, priv->dp_out_buf,
1272
			priv->dp_out_buf_len);
A
Alan Cox 已提交
1273 1274
		ret = usb_submit_urb(port->write_urb, GFP_ATOMIC);
		if (ret == 0) {
L
Linus Torvalds 已提交
1275 1276 1277 1278 1279
			priv->dp_write_urb_in_use = 1;
			priv->dp_out_buf_len = 0;
		}
	}
	/* wake up processes sleeping on writes immediately */
1280
	digi_wakeup_write(port);
L
Linus Torvalds 已提交
1281 1282 1283 1284
	/* also queue up a wakeup at scheduler time, in case we */
	/* lost the race in write_chan(). */
	schedule_work(&priv->dp_wakeup_work);

1285 1286 1287
	spin_unlock(&priv->dp_port_lock);
	if (ret)
		err("%s: usb_submit_urb failed, ret=%d, port=%d",
1288
			__func__, ret, priv->dp_port_num);
L
Linus Torvalds 已提交
1289 1290
}

A
Alan Cox 已提交
1291
static int digi_write_room(struct tty_struct *tty)
L
Linus Torvalds 已提交
1292
{
A
Alan Cox 已提交
1293
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
1294
	struct digi_port *priv = usb_get_serial_port_data(port);
A
Alan Cox 已提交
1295
	int room;
L
Linus Torvalds 已提交
1296 1297
	unsigned long flags = 0;

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

A
Alan Cox 已提交
1300 1301
	if (port->write_urb->status == -EINPROGRESS ||
					priv->dp_write_urb_in_use)
L
Linus Torvalds 已提交
1302 1303 1304 1305
		room = 0;
	else
		room = port->bulk_out_size - 2 - priv->dp_out_buf_len;

1306 1307 1308
	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
	dbg("digi_write_room: port=%d, room=%d", priv->dp_port_num, room);
	return room;
L
Linus Torvalds 已提交
1309 1310 1311

}

A
Alan Cox 已提交
1312
static int digi_chars_in_buffer(struct tty_struct *tty)
L
Linus Torvalds 已提交
1313
{
A
Alan Cox 已提交
1314
	struct usb_serial_port *port = tty->driver_data;
L
Linus Torvalds 已提交
1315 1316
	struct digi_port *priv = usb_get_serial_port_data(port);

1317 1318 1319 1320 1321 1322
	if (port->write_urb->status == -EINPROGRESS
	    || priv->dp_write_urb_in_use) {
		dbg("digi_chars_in_buffer: port=%d, chars=%d",
			priv->dp_port_num, port->bulk_out_size - 2);
		/* return(port->bulk_out_size - 2); */
		return 256;
L
Linus Torvalds 已提交
1323
	} else {
1324 1325 1326
		dbg("digi_chars_in_buffer: port=%d, chars=%d",
			priv->dp_port_num, priv->dp_out_buf_len);
		return priv->dp_out_buf_len;
L
Linus Torvalds 已提交
1327 1328 1329 1330 1331
	}

}


A
Alan Cox 已提交
1332 1333
static int digi_open(struct tty_struct *tty, struct usb_serial_port *port,
				struct file *filp)
L
Linus Torvalds 已提交
1334 1335 1336 1337
{
	int ret;
	unsigned char buf[32];
	struct digi_port *priv = usb_get_serial_port_data(port);
A
Alan Cox 已提交
1338
	struct ktermios not_termios;
L
Linus Torvalds 已提交
1339 1340
	unsigned long flags = 0;

1341
	dbg("digi_open: TOP: port=%d, open_count=%d",
A
Alan Cox 已提交
1342
		priv->dp_port_num, port->port.count);
L
Linus Torvalds 已提交
1343 1344

	/* be sure the device is started up */
1345 1346
	if (digi_startup_device(port->serial) != 0)
		return -ENXIO;
L
Linus Torvalds 已提交
1347

1348
	spin_lock_irqsave(&priv->dp_port_lock, flags);
L
Linus Torvalds 已提交
1349 1350

	/* don't wait on a close in progress for non-blocking opens */
1351 1352 1353
	if (priv->dp_in_close && (filp->f_flags&(O_NDELAY|O_NONBLOCK)) == 0) {
		spin_unlock_irqrestore(&priv->dp_port_lock, flags);
		return -EAGAIN;
L
Linus Torvalds 已提交
1354 1355 1356
	}

	/* wait for a close in progress to finish */
A
Alan Cox 已提交
1357
	while (priv->dp_in_close) {
L
Linus Torvalds 已提交
1358 1359
		cond_wait_interruptible_timeout_irqrestore(
			&priv->dp_close_wait, DIGI_RETRY_TIMEOUT,
1360 1361 1362 1363
			&priv->dp_port_lock, flags);
		if (signal_pending(current))
			return -EINTR;
		spin_lock_irqsave(&priv->dp_port_lock, flags);
L
Linus Torvalds 已提交
1364 1365
	}

1366
	spin_unlock_irqrestore(&priv->dp_port_lock, flags);
A
Alan Cox 已提交
1367

L
Linus Torvalds 已提交
1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379
	/* 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 已提交
1380 1381
	ret = digi_write_oob_command(port, buf, 8, 1);
	if (ret != 0)
1382
		dbg("digi_open: write oob failed, ret=%d", ret);
L
Linus Torvalds 已提交
1383 1384

	/* set termios settings */
A
Alan Cox 已提交
1385 1386 1387 1388 1389
	if (tty) {
		not_termios.c_cflag = ~tty->termios->c_cflag;
		not_termios.c_iflag = ~tty->termios->c_iflag;
		digi_set_termios(tty, port, &not_termios);
	}
L
Linus Torvalds 已提交
1390 1391

	/* set DTR and RTS */
1392
	digi_set_modem_signals(port, TIOCM_DTR|TIOCM_RTS, 1);
L
Linus Torvalds 已提交
1393

1394
	return 0;
L
Linus Torvalds 已提交
1395 1396 1397
}


A
Alan Cox 已提交
1398 1399
static void digi_close(struct tty_struct *tty, struct usb_serial_port *port,
				struct file *filp)
L
Linus Torvalds 已提交
1400 1401 1402 1403 1404 1405
{
	DEFINE_WAIT(wait);
	int ret;
	unsigned char buf[32];
	struct digi_port *priv = usb_get_serial_port_data(port);

1406
	dbg("digi_close: TOP: port=%d, open_count=%d",
A
Alan Cox 已提交
1407
		priv->dp_port_num, port->port.count);
L
Linus Torvalds 已提交
1408

1409
	mutex_lock(&port->serial->disc_mutex);
L
Linus Torvalds 已提交
1410
	/* if disconnected, just clear flags */
1411
	if (port->serial->disconnected)
L
Linus Torvalds 已提交
1412 1413 1414
		goto exit;

	/* do cleanup only after final close on this port */
1415
	spin_lock_irq(&priv->dp_port_lock);
L
Linus Torvalds 已提交
1416
	priv->dp_in_close = 1;
1417
	spin_unlock_irq(&priv->dp_port_lock);
L
Linus Torvalds 已提交
1418 1419 1420 1421 1422

	/* tell line discipline to process only XON/XOFF */
	tty->closing = 1;

	/* wait for output to drain */
1423 1424
	if ((filp->f_flags&(O_NDELAY|O_NONBLOCK)) == 0)
		tty_wait_until_sent(tty, DIGI_CLOSE_TIMEOUT);
L
Linus Torvalds 已提交
1425 1426

	/* flush driver and line discipline buffers */
A
Alan Cox 已提交
1427
	tty_driver_flush_buffer(tty);
L
Linus Torvalds 已提交
1428 1429 1430 1431
	tty_ldisc_flush(tty);

	if (port->serial->dev) {
		/* wait for transmit idle */
A
Alan Cox 已提交
1432
		if ((filp->f_flags&(O_NDELAY|O_NONBLOCK)) == 0)
1433
			digi_transmit_idle(port, DIGI_CLOSE_TIMEOUT);
L
Linus Torvalds 已提交
1434
		/* drop DTR and RTS */
1435
		digi_set_modem_signals(port, 0, 0);
L
Linus Torvalds 已提交
1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466

		/* 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 已提交
1467 1468
		ret = digi_write_oob_command(port, buf, 20, 0);
		if (ret != 0)
1469
			dbg("digi_close: write oob failed, ret=%d", ret);
L
Linus Torvalds 已提交
1470 1471

		/* wait for final commands on oob port to complete */
A
Alan Cox 已提交
1472 1473
		prepare_to_wait(&priv->dp_flush_wait, &wait,
							TASK_INTERRUPTIBLE);
L
Linus Torvalds 已提交
1474 1475 1476 1477 1478 1479 1480 1481
		schedule_timeout(DIGI_CLOSE_TIMEOUT);
		finish_wait(&priv->dp_flush_wait, &wait);

		/* shutdown any outstanding bulk writes */
		usb_kill_urb(port->write_urb);
	}
	tty->closing = 0;
exit:
1482
	spin_lock_irq(&priv->dp_port_lock);
L
Linus Torvalds 已提交
1483 1484
	priv->dp_write_urb_in_use = 0;
	priv->dp_in_close = 0;
1485
	wake_up_interruptible(&priv->dp_close_wait);
1486 1487
	spin_unlock_irq(&priv->dp_port_lock);
	mutex_unlock(&port->serial->disc_mutex);
1488
	dbg("digi_close: done");
L
Linus Torvalds 已提交
1489 1490 1491 1492
}


/*
A
Alan Cox 已提交
1493 1494 1495 1496 1497
 *  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 已提交
1498

1499
static int digi_startup_device(struct usb_serial *serial)
L
Linus Torvalds 已提交
1500
{
A
Alan Cox 已提交
1501
	int i, ret = 0;
L
Linus Torvalds 已提交
1502 1503 1504 1505
	struct digi_serial *serial_priv = usb_get_serial_data(serial);
	struct usb_serial_port *port;

	/* be sure this happens exactly once */
1506 1507 1508 1509
	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 已提交
1510 1511
	}
	serial_priv->ds_device_started = 1;
1512
	spin_unlock(&serial_priv->ds_serial_lock);
L
Linus Torvalds 已提交
1513 1514 1515

	/* start reading from each bulk in endpoint for the device */
	/* set USB_DISABLE_SPD flag for write bulk urbs */
1516
	for (i = 0; i < serial->type->num_ports + 1; i++) {
L
Linus Torvalds 已提交
1517 1518
		port = serial->port[i];
		port->write_urb->dev = port->serial->dev;
A
Alan Cox 已提交
1519 1520
		ret = usb_submit_urb(port->read_urb, GFP_KERNEL);
		if (ret != 0) {
1521
			err("%s: usb_submit_urb failed, ret=%d, port=%d",
1522
					__func__, ret, i);
L
Linus Torvalds 已提交
1523 1524 1525
			break;
		}
	}
1526
	return ret;
L
Linus Torvalds 已提交
1527 1528 1529
}


1530
static int digi_startup(struct usb_serial *serial)
L
Linus Torvalds 已提交
1531 1532 1533 1534 1535 1536
{

	int i;
	struct digi_port *priv;
	struct digi_serial *serial_priv;

1537
	dbg("digi_startup: TOP");
L
Linus Torvalds 已提交
1538 1539 1540

	/* allocate the private data structures for all ports */
	/* number of regular ports + 1 for the out-of-band port */
A
Alan Cox 已提交
1541
	for (i = 0; i < serial->type->num_ports + 1; i++) {
L
Linus Torvalds 已提交
1542
		/* allocate port private structure */
1543 1544 1545 1546 1547
		priv = kmalloc(sizeof(struct digi_port), GFP_KERNEL);
		if (priv == NULL) {
			while (--i >= 0)
				kfree(usb_get_serial_port_data(serial->port[i]));
			return 1;			/* error */
L
Linus Torvalds 已提交
1548 1549 1550
		}

		/* initialize port private structure */
1551
		spin_lock_init(&priv->dp_port_lock);
L
Linus Torvalds 已提交
1552 1553 1554 1555
		priv->dp_port_num = i;
		priv->dp_out_buf_len = 0;
		priv->dp_write_urb_in_use = 0;
		priv->dp_modem_signals = 0;
1556
		init_waitqueue_head(&priv->dp_modem_change_wait);
L
Linus Torvalds 已提交
1557
		priv->dp_transmit_idle = 0;
1558
		init_waitqueue_head(&priv->dp_transmit_idle_wait);
L
Linus Torvalds 已提交
1559 1560
		priv->dp_throttled = 0;
		priv->dp_throttle_restart = 0;
1561
		init_waitqueue_head(&priv->dp_flush_wait);
L
Linus Torvalds 已提交
1562
		priv->dp_in_close = 0;
1563
		init_waitqueue_head(&priv->dp_close_wait);
D
David Howells 已提交
1564 1565
		INIT_WORK(&priv->dp_wakeup_work, digi_wakeup_write_lock);
		priv->dp_port = serial->port[i];
L
Linus Torvalds 已提交
1566
		/* initialize write wait queue for this port */
1567
		init_waitqueue_head(&serial->port[i]->write_wait);
L
Linus Torvalds 已提交
1568 1569 1570 1571 1572

		usb_set_serial_port_data(serial->port[i], priv);
	}

	/* allocate serial private structure */
1573 1574 1575 1576 1577
	serial_priv = kmalloc(sizeof(struct digi_serial), GFP_KERNEL);
	if (serial_priv == NULL) {
		for (i = 0; i < serial->type->num_ports + 1; i++)
			kfree(usb_get_serial_port_data(serial->port[i]));
		return 1;			/* error */
L
Linus Torvalds 已提交
1578 1579 1580
	}

	/* initialize serial private structure */
1581
	spin_lock_init(&serial_priv->ds_serial_lock);
L
Linus Torvalds 已提交
1582 1583 1584 1585 1586
	serial_priv->ds_oob_port_num = serial->type->num_ports;
	serial_priv->ds_oob_port = serial->port[serial_priv->ds_oob_port_num];
	serial_priv->ds_device_started = 0;
	usb_set_serial_data(serial, serial_priv);

1587
	return 0;
L
Linus Torvalds 已提交
1588 1589 1590
}


1591
static void digi_shutdown(struct usb_serial *serial)
L
Linus Torvalds 已提交
1592 1593
{
	int i;
1594
	dbg("digi_shutdown: TOP, in_interrupt()=%ld", in_interrupt());
L
Linus Torvalds 已提交
1595 1596

	/* stop reads and writes on all ports */
1597
	for (i = 0; i < serial->type->num_ports + 1; i++) {
L
Linus Torvalds 已提交
1598 1599 1600 1601 1602 1603
		usb_kill_urb(serial->port[i]->read_urb);
		usb_kill_urb(serial->port[i]->write_urb);
	}

	/* free the private data structures for all ports */
	/* number of regular ports + 1 for the out-of-band port */
A
Alan Cox 已提交
1604
	for (i = 0; i < serial->type->num_ports + 1; i++)
1605 1606
		kfree(usb_get_serial_port_data(serial->port[i]));
	kfree(usb_get_serial_data(serial));
L
Linus Torvalds 已提交
1607 1608 1609
}


1610
static void digi_read_bulk_callback(struct urb *urb)
L
Linus Torvalds 已提交
1611
{
1612
	struct usb_serial_port *port = urb->context;
L
Linus Torvalds 已提交
1613 1614 1615
	struct digi_port *priv;
	struct digi_serial *serial_priv;
	int ret;
1616
	int status = urb->status;
L
Linus Torvalds 已提交
1617

1618
	dbg("digi_read_bulk_callback: TOP");
L
Linus Torvalds 已提交
1619 1620

	/* port sanity check, do not resubmit if port is not valid */
1621
	if (port == NULL || (priv = usb_get_serial_port_data(port)) == NULL) {
1622
		err("%s: port or port->private is NULL, status=%d",
1623
		    __func__, status);
L
Linus Torvalds 已提交
1624 1625
		return;
	}
1626
	if (port->serial == NULL ||
A
Alan Cox 已提交
1627
		(serial_priv = usb_get_serial_data(port->serial)) == NULL) {
1628
		err("%s: serial is bad or serial->private is NULL, status=%d",
1629
			__func__, status);
L
Linus Torvalds 已提交
1630 1631 1632 1633
		return;
	}

	/* do not resubmit urb if it has any status error */
1634 1635
	if (status) {
		err("%s: nonzero read bulk status: status=%d, port=%d",
1636
		    __func__, status, priv->dp_port_num);
L
Linus Torvalds 已提交
1637 1638 1639 1640
		return;
	}

	/* handle oob or inb callback, do not resubmit if error */
1641 1642
	if (priv->dp_port_num == serial_priv->ds_oob_port_num) {
		if (digi_read_oob_callback(urb) != 0)
L
Linus Torvalds 已提交
1643 1644
			return;
	} else {
1645
		if (digi_read_inb_callback(urb) != 0)
L
Linus Torvalds 已提交
1646 1647 1648 1649 1650
			return;
	}

	/* continue read */
	urb->dev = port->serial->dev;
A
Alan Cox 已提交
1651 1652
	ret = usb_submit_urb(urb, GFP_ATOMIC);
	if (ret != 0) {
1653
		err("%s: failed resubmitting urb, ret=%d, port=%d",
1654
		    __func__, ret, priv->dp_port_num);
L
Linus Torvalds 已提交
1655 1656 1657 1658
	}

}

A
Alan Cox 已提交
1659 1660 1661 1662 1663 1664 1665 1666 1667
/*
 *  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 已提交
1668

1669
static int digi_read_inb_callback(struct urb *urb)
L
Linus Torvalds 已提交
1670 1671
{

1672
	struct usb_serial_port *port = urb->context;
A
Alan Cox 已提交
1673
	struct tty_struct *tty;
L
Linus Torvalds 已提交
1674 1675 1676
	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];
1677
	int port_status = ((unsigned char *)urb->transfer_buffer)[2];
A
Alan Cox 已提交
1678 1679
	unsigned char *data = ((unsigned char *)urb->transfer_buffer) + 3;
	int flag, throttled;
A
Alan Cox 已提交
1680
	int i;
1681
	int status = urb->status;
L
Linus Torvalds 已提交
1682 1683 1684

	/* do not process callbacks on closed ports */
	/* but do continue the read chain */
A
Alan Cox 已提交
1685
	if (port->port.count == 0)
1686
		return 0;
L
Linus Torvalds 已提交
1687 1688

	/* short/multiple packet check */
1689
	if (urb->actual_length != len + 2) {
A
Alan Cox 已提交
1690
		err("%s: INCOMPLETE OR MULTIPLE PACKET, urb->status=%d, "
1691
		    "port=%d, opcode=%d, len=%d, actual_length=%d, "
1692
		    "status=%d", __func__, status, priv->dp_port_num,
1693
		    opcode, len, urb->actual_length, port_status);
1694
		return -1;
L
Linus Torvalds 已提交
1695 1696
	}

A
Alan Cox 已提交
1697
	tty = tty_port_tty_get(&port->port);
1698
	spin_lock(&priv->dp_port_lock);
L
Linus Torvalds 已提交
1699 1700 1701 1702

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

	/* receive data */
1707
	if (opcode == DIGI_CMD_RECEIVE_DATA) {
1708
		/* get flag from port_status */
L
Linus Torvalds 已提交
1709 1710 1711
		flag = 0;

		/* overrun is special, not associated with a char */
1712 1713
		if (port_status & DIGI_OVERRUN_ERROR)
			tty_insert_flip_char(tty, 0, TTY_OVERRUN);
L
Linus Torvalds 已提交
1714 1715 1716

		/* break takes precedence over parity, */
		/* which takes precedence over framing errors */
1717
		if (port_status & DIGI_BREAK_ERROR)
L
Linus Torvalds 已提交
1718
			flag = TTY_BREAK;
1719
		else if (port_status & DIGI_PARITY_ERROR)
L
Linus Torvalds 已提交
1720
			flag = TTY_PARITY;
1721
		else if (port_status & DIGI_FRAMING_ERROR)
L
Linus Torvalds 已提交
1722 1723
			flag = TTY_FRAME;

1724
		/* data length is len-1 (one byte of len is port_status) */
L
Linus Torvalds 已提交
1725 1726
		--len;

1727
		len = tty_buffer_request_room(tty, len);
1728
		if (len > 0) {
1729
			/* Hot path */
1730
			if (flag == TTY_NORMAL)
1731 1732
				tty_insert_flip_string(tty, data, len);
			else {
A
Alan Cox 已提交
1733 1734 1735
				for (i = 0; i < len; i++)
					tty_insert_flip_char(tty,
								data[i], flag);
L
Linus Torvalds 已提交
1736
			}
1737
			tty_flip_buffer_push(tty);
L
Linus Torvalds 已提交
1738 1739
		}
	}
1740
	spin_unlock(&priv->dp_port_lock);
A
Alan Cox 已提交
1741
	tty_kref_put(tty);
L
Linus Torvalds 已提交
1742

1743
	if (opcode == DIGI_CMD_RECEIVE_DISABLE)
1744
		dbg("%s: got RECEIVE_DISABLE", __func__);
1745
	else if (opcode != DIGI_CMD_RECEIVE_DATA)
1746
		dbg("%s: unknown opcode: %d", __func__, opcode);
L
Linus Torvalds 已提交
1747

A
Alan Cox 已提交
1748
	return throttled ? 1 : 0;
L
Linus Torvalds 已提交
1749 1750 1751 1752

}


A
Alan Cox 已提交
1753 1754 1755 1756 1757 1758 1759 1760
/*
 *  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 已提交
1761

1762
static int digi_read_oob_callback(struct urb *urb)
L
Linus Torvalds 已提交
1763 1764
{

1765
	struct usb_serial_port *port = urb->context;
L
Linus Torvalds 已提交
1766
	struct usb_serial *serial = port->serial;
A
Alan Cox 已提交
1767
	struct tty_struct *tty;
L
Linus Torvalds 已提交
1768 1769 1770
	struct digi_port *priv = usb_get_serial_port_data(port);
	int opcode, line, status, val;
	int i;
A
Alan Cox 已提交
1771
	unsigned int rts;
L
Linus Torvalds 已提交
1772

1773 1774
	dbg("digi_read_oob_callback: port=%d, len=%d",
			priv->dp_port_num, urb->actual_length);
L
Linus Torvalds 已提交
1775 1776

	/* handle each oob command */
A
Alan Cox 已提交
1777
	for (i = 0; i < urb->actual_length - 3;) {
L
Linus Torvalds 已提交
1778 1779 1780 1781 1782
		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++];

1783 1784
		dbg("digi_read_oob_callback: opcode=%d, line=%d, status=%d, val=%d",
			opcode, line, status, val);
L
Linus Torvalds 已提交
1785

1786
		if (status != 0 || line >= serial->type->num_ports)
L
Linus Torvalds 已提交
1787 1788 1789 1790
			continue;

		port = serial->port[line];

A
Alan Cox 已提交
1791 1792
		priv = usb_get_serial_port_data(port);
		if (priv == NULL)
L
Linus Torvalds 已提交
1793 1794
			return -1;

A
Alan Cox 已提交
1795
		tty = tty_port_tty_get(&port->port);
A
Alan Cox 已提交
1796 1797
		rts = 0;
		if (port->port.count)
A
Alan Cox 已提交
1798 1799
			rts = tty->termios->c_cflag & CRTSCTS;
		
1800 1801
		if (opcode == DIGI_CMD_READ_INPUT_SIGNALS) {
			spin_lock(&priv->dp_port_lock);
L
Linus Torvalds 已提交
1802
			/* convert from digi flags to termiox flags */
1803
			if (val & DIGI_READ_INPUT_SIGNALS_CTS) {
L
Linus Torvalds 已提交
1804 1805
				priv->dp_modem_signals |= TIOCM_CTS;
				/* port must be open to use tty struct */
A
Alan Cox 已提交
1806
				if (rts) {
A
Alan Cox 已提交
1807
					tty->hw_stopped = 0;
1808
					digi_wakeup_write(port);
L
Linus Torvalds 已提交
1809 1810 1811 1812
				}
			} else {
				priv->dp_modem_signals &= ~TIOCM_CTS;
				/* port must be open to use tty struct */
A
Alan Cox 已提交
1813
				if (rts)
A
Alan Cox 已提交
1814
					tty->hw_stopped = 1;
L
Linus Torvalds 已提交
1815
			}
1816
			if (val & DIGI_READ_INPUT_SIGNALS_DSR)
L
Linus Torvalds 已提交
1817 1818 1819
				priv->dp_modem_signals |= TIOCM_DSR;
			else
				priv->dp_modem_signals &= ~TIOCM_DSR;
1820
			if (val & DIGI_READ_INPUT_SIGNALS_RI)
L
Linus Torvalds 已提交
1821 1822 1823
				priv->dp_modem_signals |= TIOCM_RI;
			else
				priv->dp_modem_signals &= ~TIOCM_RI;
1824
			if (val & DIGI_READ_INPUT_SIGNALS_DCD)
L
Linus Torvalds 已提交
1825 1826 1827 1828
				priv->dp_modem_signals |= TIOCM_CD;
			else
				priv->dp_modem_signals &= ~TIOCM_CD;

1829 1830 1831 1832
			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 已提交
1833
			priv->dp_transmit_idle = 1;
1834 1835 1836 1837
			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 已提交
1838
		}
A
Alan Cox 已提交
1839
		tty_kref_put(tty);
L
Linus Torvalds 已提交
1840
	}
1841
	return 0;
L
Linus Torvalds 已提交
1842 1843 1844

}

1845
static int __init digi_init(void)
L
Linus Torvalds 已提交
1846 1847 1848 1849 1850 1851
{
	int retval;
	retval = usb_serial_register(&digi_acceleport_2_device);
	if (retval)
		goto failed_acceleport_2_device;
	retval = usb_serial_register(&digi_acceleport_4_device);
A
Alan Cox 已提交
1852
	if (retval)
L
Linus Torvalds 已提交
1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868
		goto failed_acceleport_4_device;
	retval = usb_register(&digi_driver);
	if (retval)
		goto failed_usb_register;
	info(DRIVER_VERSION ":" DRIVER_DESC);
	return 0;
failed_usb_register:
	usb_serial_deregister(&digi_acceleport_4_device);
failed_acceleport_4_device:
	usb_serial_deregister(&digi_acceleport_2_device);
failed_acceleport_2_device:
	return retval;
}

static void __exit digi_exit (void)
{
1869 1870 1871
	usb_deregister(&digi_driver);
	usb_serial_deregister(&digi_acceleport_2_device);
	usb_serial_deregister(&digi_acceleport_4_device);
L
Linus Torvalds 已提交
1872 1873 1874 1875 1876 1877 1878
}


module_init(digi_init);
module_exit(digi_exit);


1879 1880
MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_DESCRIPTION(DRIVER_DESC);
L
Linus Torvalds 已提交
1881 1882 1883 1884
MODULE_LICENSE("GPL");

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