162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Ours Technology Inc. OTi-6858 USB to serial adapter driver.
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Copyleft  (C) 2007 Kees Lemmens (adapted for kernel 2.6.20)
662306a36Sopenharmony_ci * Copyright (C) 2006 Tomasz Michal Lukaszewski (FIXME: add e-mail)
762306a36Sopenharmony_ci * Copyright (C) 2001-2004 Greg Kroah-Hartman (greg@kroah.com)
862306a36Sopenharmony_ci * Copyright (C) 2003 IBM Corp.
962306a36Sopenharmony_ci *
1062306a36Sopenharmony_ci * Many thanks to the authors of pl2303 driver: all functions in this file
1162306a36Sopenharmony_ci * are heavily based on pl2303 code, buffering code is a 1-to-1 copy.
1262306a36Sopenharmony_ci *
1362306a36Sopenharmony_ci * Warning! You use this driver on your own risk! The only official
1462306a36Sopenharmony_ci * description of this device I have is datasheet from manufacturer,
1562306a36Sopenharmony_ci * and it doesn't contain almost any information needed to write a driver.
1662306a36Sopenharmony_ci * Almost all knowlegde used while writing this driver was gathered by:
1762306a36Sopenharmony_ci *  - analyzing traffic between device and the M$ Windows 2000 driver,
1862306a36Sopenharmony_ci *  - trying different bit combinations and checking pin states
1962306a36Sopenharmony_ci *    with a voltmeter,
2062306a36Sopenharmony_ci *  - receiving malformed frames and producing buffer overflows
2162306a36Sopenharmony_ci *    to learn how errors are reported,
2262306a36Sopenharmony_ci * So, THIS CODE CAN DESTROY OTi-6858 AND ANY OTHER DEVICES, THAT ARE
2362306a36Sopenharmony_ci * CONNECTED TO IT!
2462306a36Sopenharmony_ci *
2562306a36Sopenharmony_ci * See Documentation/usb/usb-serial.rst for more information on using this
2662306a36Sopenharmony_ci * driver
2762306a36Sopenharmony_ci *
2862306a36Sopenharmony_ci * TODO:
2962306a36Sopenharmony_ci *  - implement correct flushing for ioctls and oti6858_close()
3062306a36Sopenharmony_ci *  - check how errors (rx overflow, parity error, framing error) are reported
3162306a36Sopenharmony_ci *  - implement oti6858_break_ctl()
3262306a36Sopenharmony_ci *  - implement more ioctls
3362306a36Sopenharmony_ci *  - test/implement flow control
3462306a36Sopenharmony_ci *  - allow setting custom baud rates
3562306a36Sopenharmony_ci */
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_ci#include <linux/kernel.h>
3862306a36Sopenharmony_ci#include <linux/errno.h>
3962306a36Sopenharmony_ci#include <linux/slab.h>
4062306a36Sopenharmony_ci#include <linux/tty.h>
4162306a36Sopenharmony_ci#include <linux/tty_driver.h>
4262306a36Sopenharmony_ci#include <linux/tty_flip.h>
4362306a36Sopenharmony_ci#include <linux/serial.h>
4462306a36Sopenharmony_ci#include <linux/module.h>
4562306a36Sopenharmony_ci#include <linux/moduleparam.h>
4662306a36Sopenharmony_ci#include <linux/spinlock.h>
4762306a36Sopenharmony_ci#include <linux/usb.h>
4862306a36Sopenharmony_ci#include <linux/usb/serial.h>
4962306a36Sopenharmony_ci#include <linux/uaccess.h>
5062306a36Sopenharmony_ci#include <linux/kfifo.h>
5162306a36Sopenharmony_ci#include "oti6858.h"
5262306a36Sopenharmony_ci
5362306a36Sopenharmony_ci#define OTI6858_DESCRIPTION \
5462306a36Sopenharmony_ci	"Ours Technology Inc. OTi-6858 USB to serial adapter driver"
5562306a36Sopenharmony_ci#define OTI6858_AUTHOR "Tomasz Michal Lukaszewski <FIXME@FIXME>"
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_cistatic const struct usb_device_id id_table[] = {
5862306a36Sopenharmony_ci	{ USB_DEVICE(OTI6858_VENDOR_ID, OTI6858_PRODUCT_ID) },
5962306a36Sopenharmony_ci	{ }
6062306a36Sopenharmony_ci};
6162306a36Sopenharmony_ci
6262306a36Sopenharmony_ciMODULE_DEVICE_TABLE(usb, id_table);
6362306a36Sopenharmony_ci
6462306a36Sopenharmony_ci/* requests */
6562306a36Sopenharmony_ci#define	OTI6858_REQ_GET_STATUS		(USB_DIR_IN | USB_TYPE_VENDOR | 0x00)
6662306a36Sopenharmony_ci#define	OTI6858_REQ_T_GET_STATUS	0x01
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_ci#define	OTI6858_REQ_SET_LINE		(USB_DIR_OUT | USB_TYPE_VENDOR | 0x00)
6962306a36Sopenharmony_ci#define	OTI6858_REQ_T_SET_LINE		0x00
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_ci#define	OTI6858_REQ_CHECK_TXBUFF	(USB_DIR_IN | USB_TYPE_VENDOR | 0x01)
7262306a36Sopenharmony_ci#define	OTI6858_REQ_T_CHECK_TXBUFF	0x00
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_ci/* format of the control packet */
7562306a36Sopenharmony_cistruct oti6858_control_pkt {
7662306a36Sopenharmony_ci	__le16	divisor;	/* baud rate = 96000000 / (16 * divisor), LE */
7762306a36Sopenharmony_ci#define OTI6858_MAX_BAUD_RATE	3000000
7862306a36Sopenharmony_ci	u8	frame_fmt;
7962306a36Sopenharmony_ci#define FMT_STOP_BITS_MASK	0xc0
8062306a36Sopenharmony_ci#define FMT_STOP_BITS_1		0x00
8162306a36Sopenharmony_ci#define FMT_STOP_BITS_2		0x40	/* 1.5 stop bits if FMT_DATA_BITS_5 */
8262306a36Sopenharmony_ci#define FMT_PARITY_MASK		0x38
8362306a36Sopenharmony_ci#define FMT_PARITY_NONE		0x00
8462306a36Sopenharmony_ci#define FMT_PARITY_ODD		0x08
8562306a36Sopenharmony_ci#define FMT_PARITY_EVEN		0x18
8662306a36Sopenharmony_ci#define FMT_PARITY_MARK		0x28
8762306a36Sopenharmony_ci#define FMT_PARITY_SPACE	0x38
8862306a36Sopenharmony_ci#define FMT_DATA_BITS_MASK	0x03
8962306a36Sopenharmony_ci#define FMT_DATA_BITS_5		0x00
9062306a36Sopenharmony_ci#define FMT_DATA_BITS_6		0x01
9162306a36Sopenharmony_ci#define FMT_DATA_BITS_7		0x02
9262306a36Sopenharmony_ci#define FMT_DATA_BITS_8		0x03
9362306a36Sopenharmony_ci	u8	something;	/* always equals 0x43 */
9462306a36Sopenharmony_ci	u8	control;	/* settings of flow control lines */
9562306a36Sopenharmony_ci#define CONTROL_MASK		0x0c
9662306a36Sopenharmony_ci#define CONTROL_DTR_HIGH	0x08
9762306a36Sopenharmony_ci#define CONTROL_RTS_HIGH	0x04
9862306a36Sopenharmony_ci	u8	tx_status;
9962306a36Sopenharmony_ci#define	TX_BUFFER_EMPTIED	0x09
10062306a36Sopenharmony_ci	u8	pin_state;
10162306a36Sopenharmony_ci#define PIN_MASK		0x3f
10262306a36Sopenharmony_ci#define PIN_MSR_MASK		0x1b
10362306a36Sopenharmony_ci#define PIN_RTS			0x20	/* output pin */
10462306a36Sopenharmony_ci#define PIN_CTS			0x10	/* input pin, active low */
10562306a36Sopenharmony_ci#define PIN_DSR			0x08	/* input pin, active low */
10662306a36Sopenharmony_ci#define PIN_DTR			0x04	/* output pin */
10762306a36Sopenharmony_ci#define PIN_RI			0x02	/* input pin, active low */
10862306a36Sopenharmony_ci#define PIN_DCD			0x01	/* input pin, active low */
10962306a36Sopenharmony_ci	u8	rx_bytes_avail;		/* number of bytes in rx buffer */;
11062306a36Sopenharmony_ci};
11162306a36Sopenharmony_ci
11262306a36Sopenharmony_ci#define OTI6858_CTRL_PKT_SIZE	sizeof(struct oti6858_control_pkt)
11362306a36Sopenharmony_ci#define OTI6858_CTRL_EQUALS_PENDING(a, priv) \
11462306a36Sopenharmony_ci	(((a)->divisor == (priv)->pending_setup.divisor) \
11562306a36Sopenharmony_ci	  && ((a)->control == (priv)->pending_setup.control) \
11662306a36Sopenharmony_ci	  && ((a)->frame_fmt == (priv)->pending_setup.frame_fmt))
11762306a36Sopenharmony_ci
11862306a36Sopenharmony_ci/* function prototypes */
11962306a36Sopenharmony_cistatic int oti6858_open(struct tty_struct *tty, struct usb_serial_port *port);
12062306a36Sopenharmony_cistatic void oti6858_close(struct usb_serial_port *port);
12162306a36Sopenharmony_cistatic void oti6858_set_termios(struct tty_struct *tty,
12262306a36Sopenharmony_ci				struct usb_serial_port *port,
12362306a36Sopenharmony_ci				const struct ktermios *old_termios);
12462306a36Sopenharmony_cistatic void oti6858_init_termios(struct tty_struct *tty);
12562306a36Sopenharmony_cistatic void oti6858_read_int_callback(struct urb *urb);
12662306a36Sopenharmony_cistatic void oti6858_read_bulk_callback(struct urb *urb);
12762306a36Sopenharmony_cistatic void oti6858_write_bulk_callback(struct urb *urb);
12862306a36Sopenharmony_cistatic int oti6858_write(struct tty_struct *tty, struct usb_serial_port *port,
12962306a36Sopenharmony_ci			const unsigned char *buf, int count);
13062306a36Sopenharmony_cistatic unsigned int oti6858_write_room(struct tty_struct *tty);
13162306a36Sopenharmony_cistatic unsigned int oti6858_chars_in_buffer(struct tty_struct *tty);
13262306a36Sopenharmony_cistatic int oti6858_tiocmget(struct tty_struct *tty);
13362306a36Sopenharmony_cistatic int oti6858_tiocmset(struct tty_struct *tty,
13462306a36Sopenharmony_ci				unsigned int set, unsigned int clear);
13562306a36Sopenharmony_cistatic int oti6858_port_probe(struct usb_serial_port *port);
13662306a36Sopenharmony_cistatic void oti6858_port_remove(struct usb_serial_port *port);
13762306a36Sopenharmony_ci
13862306a36Sopenharmony_ci/* device info */
13962306a36Sopenharmony_cistatic struct usb_serial_driver oti6858_device = {
14062306a36Sopenharmony_ci	.driver = {
14162306a36Sopenharmony_ci		.owner =	THIS_MODULE,
14262306a36Sopenharmony_ci		.name =		"oti6858",
14362306a36Sopenharmony_ci	},
14462306a36Sopenharmony_ci	.id_table =		id_table,
14562306a36Sopenharmony_ci	.num_ports =		1,
14662306a36Sopenharmony_ci	.num_bulk_in =		1,
14762306a36Sopenharmony_ci	.num_bulk_out =		1,
14862306a36Sopenharmony_ci	.num_interrupt_in =	1,
14962306a36Sopenharmony_ci	.open =			oti6858_open,
15062306a36Sopenharmony_ci	.close =		oti6858_close,
15162306a36Sopenharmony_ci	.write =		oti6858_write,
15262306a36Sopenharmony_ci	.set_termios =		oti6858_set_termios,
15362306a36Sopenharmony_ci	.init_termios = 	oti6858_init_termios,
15462306a36Sopenharmony_ci	.tiocmget =		oti6858_tiocmget,
15562306a36Sopenharmony_ci	.tiocmset =		oti6858_tiocmset,
15662306a36Sopenharmony_ci	.tiocmiwait =		usb_serial_generic_tiocmiwait,
15762306a36Sopenharmony_ci	.read_bulk_callback =	oti6858_read_bulk_callback,
15862306a36Sopenharmony_ci	.read_int_callback =	oti6858_read_int_callback,
15962306a36Sopenharmony_ci	.write_bulk_callback =	oti6858_write_bulk_callback,
16062306a36Sopenharmony_ci	.write_room =		oti6858_write_room,
16162306a36Sopenharmony_ci	.chars_in_buffer =	oti6858_chars_in_buffer,
16262306a36Sopenharmony_ci	.port_probe =		oti6858_port_probe,
16362306a36Sopenharmony_ci	.port_remove =		oti6858_port_remove,
16462306a36Sopenharmony_ci};
16562306a36Sopenharmony_ci
16662306a36Sopenharmony_cistatic struct usb_serial_driver * const serial_drivers[] = {
16762306a36Sopenharmony_ci	&oti6858_device, NULL
16862306a36Sopenharmony_ci};
16962306a36Sopenharmony_ci
17062306a36Sopenharmony_cistruct oti6858_private {
17162306a36Sopenharmony_ci	spinlock_t lock;
17262306a36Sopenharmony_ci
17362306a36Sopenharmony_ci	struct oti6858_control_pkt status;
17462306a36Sopenharmony_ci
17562306a36Sopenharmony_ci	struct {
17662306a36Sopenharmony_ci		u8 read_urb_in_use;
17762306a36Sopenharmony_ci		u8 write_urb_in_use;
17862306a36Sopenharmony_ci	} flags;
17962306a36Sopenharmony_ci	struct delayed_work delayed_write_work;
18062306a36Sopenharmony_ci
18162306a36Sopenharmony_ci	struct {
18262306a36Sopenharmony_ci		__le16 divisor;
18362306a36Sopenharmony_ci		u8 frame_fmt;
18462306a36Sopenharmony_ci		u8 control;
18562306a36Sopenharmony_ci	} pending_setup;
18662306a36Sopenharmony_ci	u8 transient;
18762306a36Sopenharmony_ci	u8 setup_done;
18862306a36Sopenharmony_ci	struct delayed_work delayed_setup_work;
18962306a36Sopenharmony_ci
19062306a36Sopenharmony_ci	struct usb_serial_port *port;   /* USB port with which associated */
19162306a36Sopenharmony_ci};
19262306a36Sopenharmony_ci
19362306a36Sopenharmony_cistatic void setup_line(struct work_struct *work)
19462306a36Sopenharmony_ci{
19562306a36Sopenharmony_ci	struct oti6858_private *priv = container_of(work,
19662306a36Sopenharmony_ci			struct oti6858_private, delayed_setup_work.work);
19762306a36Sopenharmony_ci	struct usb_serial_port *port = priv->port;
19862306a36Sopenharmony_ci	struct oti6858_control_pkt *new_setup;
19962306a36Sopenharmony_ci	unsigned long flags;
20062306a36Sopenharmony_ci	int result;
20162306a36Sopenharmony_ci
20262306a36Sopenharmony_ci	new_setup = kmalloc(OTI6858_CTRL_PKT_SIZE, GFP_KERNEL);
20362306a36Sopenharmony_ci	if (!new_setup) {
20462306a36Sopenharmony_ci		/* we will try again */
20562306a36Sopenharmony_ci		schedule_delayed_work(&priv->delayed_setup_work,
20662306a36Sopenharmony_ci						msecs_to_jiffies(2));
20762306a36Sopenharmony_ci		return;
20862306a36Sopenharmony_ci	}
20962306a36Sopenharmony_ci
21062306a36Sopenharmony_ci	result = usb_control_msg(port->serial->dev,
21162306a36Sopenharmony_ci				usb_rcvctrlpipe(port->serial->dev, 0),
21262306a36Sopenharmony_ci				OTI6858_REQ_T_GET_STATUS,
21362306a36Sopenharmony_ci				OTI6858_REQ_GET_STATUS,
21462306a36Sopenharmony_ci				0, 0,
21562306a36Sopenharmony_ci				new_setup, OTI6858_CTRL_PKT_SIZE,
21662306a36Sopenharmony_ci				100);
21762306a36Sopenharmony_ci
21862306a36Sopenharmony_ci	if (result != OTI6858_CTRL_PKT_SIZE) {
21962306a36Sopenharmony_ci		dev_err(&port->dev, "%s(): error reading status\n", __func__);
22062306a36Sopenharmony_ci		kfree(new_setup);
22162306a36Sopenharmony_ci		/* we will try again */
22262306a36Sopenharmony_ci		schedule_delayed_work(&priv->delayed_setup_work,
22362306a36Sopenharmony_ci							msecs_to_jiffies(2));
22462306a36Sopenharmony_ci		return;
22562306a36Sopenharmony_ci	}
22662306a36Sopenharmony_ci
22762306a36Sopenharmony_ci	spin_lock_irqsave(&priv->lock, flags);
22862306a36Sopenharmony_ci	if (!OTI6858_CTRL_EQUALS_PENDING(new_setup, priv)) {
22962306a36Sopenharmony_ci		new_setup->divisor = priv->pending_setup.divisor;
23062306a36Sopenharmony_ci		new_setup->control = priv->pending_setup.control;
23162306a36Sopenharmony_ci		new_setup->frame_fmt = priv->pending_setup.frame_fmt;
23262306a36Sopenharmony_ci
23362306a36Sopenharmony_ci		spin_unlock_irqrestore(&priv->lock, flags);
23462306a36Sopenharmony_ci		result = usb_control_msg(port->serial->dev,
23562306a36Sopenharmony_ci					usb_sndctrlpipe(port->serial->dev, 0),
23662306a36Sopenharmony_ci					OTI6858_REQ_T_SET_LINE,
23762306a36Sopenharmony_ci					OTI6858_REQ_SET_LINE,
23862306a36Sopenharmony_ci					0, 0,
23962306a36Sopenharmony_ci					new_setup, OTI6858_CTRL_PKT_SIZE,
24062306a36Sopenharmony_ci					100);
24162306a36Sopenharmony_ci	} else {
24262306a36Sopenharmony_ci		spin_unlock_irqrestore(&priv->lock, flags);
24362306a36Sopenharmony_ci		result = 0;
24462306a36Sopenharmony_ci	}
24562306a36Sopenharmony_ci	kfree(new_setup);
24662306a36Sopenharmony_ci
24762306a36Sopenharmony_ci	spin_lock_irqsave(&priv->lock, flags);
24862306a36Sopenharmony_ci	if (result != OTI6858_CTRL_PKT_SIZE)
24962306a36Sopenharmony_ci		priv->transient = 0;
25062306a36Sopenharmony_ci	priv->setup_done = 1;
25162306a36Sopenharmony_ci	spin_unlock_irqrestore(&priv->lock, flags);
25262306a36Sopenharmony_ci
25362306a36Sopenharmony_ci	dev_dbg(&port->dev, "%s(): submitting interrupt urb\n", __func__);
25462306a36Sopenharmony_ci	result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
25562306a36Sopenharmony_ci	if (result != 0) {
25662306a36Sopenharmony_ci		dev_err(&port->dev, "%s(): usb_submit_urb() failed with error %d\n",
25762306a36Sopenharmony_ci			__func__, result);
25862306a36Sopenharmony_ci	}
25962306a36Sopenharmony_ci}
26062306a36Sopenharmony_ci
26162306a36Sopenharmony_cistatic void send_data(struct work_struct *work)
26262306a36Sopenharmony_ci{
26362306a36Sopenharmony_ci	struct oti6858_private *priv = container_of(work,
26462306a36Sopenharmony_ci			struct oti6858_private, delayed_write_work.work);
26562306a36Sopenharmony_ci	struct usb_serial_port *port = priv->port;
26662306a36Sopenharmony_ci	int count = 0, result;
26762306a36Sopenharmony_ci	unsigned long flags;
26862306a36Sopenharmony_ci	u8 *allow;
26962306a36Sopenharmony_ci
27062306a36Sopenharmony_ci	spin_lock_irqsave(&priv->lock, flags);
27162306a36Sopenharmony_ci	if (priv->flags.write_urb_in_use) {
27262306a36Sopenharmony_ci		spin_unlock_irqrestore(&priv->lock, flags);
27362306a36Sopenharmony_ci		schedule_delayed_work(&priv->delayed_write_work,
27462306a36Sopenharmony_ci						msecs_to_jiffies(2));
27562306a36Sopenharmony_ci		return;
27662306a36Sopenharmony_ci	}
27762306a36Sopenharmony_ci	priv->flags.write_urb_in_use = 1;
27862306a36Sopenharmony_ci	spin_unlock_irqrestore(&priv->lock, flags);
27962306a36Sopenharmony_ci
28062306a36Sopenharmony_ci	spin_lock_irqsave(&port->lock, flags);
28162306a36Sopenharmony_ci	count = kfifo_len(&port->write_fifo);
28262306a36Sopenharmony_ci	spin_unlock_irqrestore(&port->lock, flags);
28362306a36Sopenharmony_ci
28462306a36Sopenharmony_ci	if (count > port->bulk_out_size)
28562306a36Sopenharmony_ci		count = port->bulk_out_size;
28662306a36Sopenharmony_ci
28762306a36Sopenharmony_ci	if (count != 0) {
28862306a36Sopenharmony_ci		allow = kmalloc(1, GFP_KERNEL);
28962306a36Sopenharmony_ci		if (!allow)
29062306a36Sopenharmony_ci			return;
29162306a36Sopenharmony_ci
29262306a36Sopenharmony_ci		result = usb_control_msg(port->serial->dev,
29362306a36Sopenharmony_ci				usb_rcvctrlpipe(port->serial->dev, 0),
29462306a36Sopenharmony_ci				OTI6858_REQ_T_CHECK_TXBUFF,
29562306a36Sopenharmony_ci				OTI6858_REQ_CHECK_TXBUFF,
29662306a36Sopenharmony_ci				count, 0, allow, 1, 100);
29762306a36Sopenharmony_ci		if (result != 1 || *allow != 0)
29862306a36Sopenharmony_ci			count = 0;
29962306a36Sopenharmony_ci		kfree(allow);
30062306a36Sopenharmony_ci	}
30162306a36Sopenharmony_ci
30262306a36Sopenharmony_ci	if (count == 0) {
30362306a36Sopenharmony_ci		priv->flags.write_urb_in_use = 0;
30462306a36Sopenharmony_ci
30562306a36Sopenharmony_ci		dev_dbg(&port->dev, "%s(): submitting interrupt urb\n", __func__);
30662306a36Sopenharmony_ci		result = usb_submit_urb(port->interrupt_in_urb, GFP_NOIO);
30762306a36Sopenharmony_ci		if (result != 0) {
30862306a36Sopenharmony_ci			dev_err(&port->dev, "%s(): usb_submit_urb() failed with error %d\n",
30962306a36Sopenharmony_ci				__func__, result);
31062306a36Sopenharmony_ci		}
31162306a36Sopenharmony_ci		return;
31262306a36Sopenharmony_ci	}
31362306a36Sopenharmony_ci
31462306a36Sopenharmony_ci	count = kfifo_out_locked(&port->write_fifo,
31562306a36Sopenharmony_ci					port->write_urb->transfer_buffer,
31662306a36Sopenharmony_ci					count, &port->lock);
31762306a36Sopenharmony_ci	port->write_urb->transfer_buffer_length = count;
31862306a36Sopenharmony_ci	result = usb_submit_urb(port->write_urb, GFP_NOIO);
31962306a36Sopenharmony_ci	if (result != 0) {
32062306a36Sopenharmony_ci		dev_err_console(port, "%s(): usb_submit_urb() failed with error %d\n",
32162306a36Sopenharmony_ci				__func__, result);
32262306a36Sopenharmony_ci		priv->flags.write_urb_in_use = 0;
32362306a36Sopenharmony_ci	}
32462306a36Sopenharmony_ci
32562306a36Sopenharmony_ci	usb_serial_port_softint(port);
32662306a36Sopenharmony_ci}
32762306a36Sopenharmony_ci
32862306a36Sopenharmony_cistatic int oti6858_port_probe(struct usb_serial_port *port)
32962306a36Sopenharmony_ci{
33062306a36Sopenharmony_ci	struct oti6858_private *priv;
33162306a36Sopenharmony_ci
33262306a36Sopenharmony_ci	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
33362306a36Sopenharmony_ci	if (!priv)
33462306a36Sopenharmony_ci		return -ENOMEM;
33562306a36Sopenharmony_ci
33662306a36Sopenharmony_ci	spin_lock_init(&priv->lock);
33762306a36Sopenharmony_ci	priv->port = port;
33862306a36Sopenharmony_ci	INIT_DELAYED_WORK(&priv->delayed_setup_work, setup_line);
33962306a36Sopenharmony_ci	INIT_DELAYED_WORK(&priv->delayed_write_work, send_data);
34062306a36Sopenharmony_ci
34162306a36Sopenharmony_ci	usb_set_serial_port_data(port, priv);
34262306a36Sopenharmony_ci
34362306a36Sopenharmony_ci	port->port.drain_delay = 256;	/* FIXME: check the FIFO length */
34462306a36Sopenharmony_ci
34562306a36Sopenharmony_ci	return 0;
34662306a36Sopenharmony_ci}
34762306a36Sopenharmony_ci
34862306a36Sopenharmony_cistatic void oti6858_port_remove(struct usb_serial_port *port)
34962306a36Sopenharmony_ci{
35062306a36Sopenharmony_ci	struct oti6858_private *priv;
35162306a36Sopenharmony_ci
35262306a36Sopenharmony_ci	priv = usb_get_serial_port_data(port);
35362306a36Sopenharmony_ci	kfree(priv);
35462306a36Sopenharmony_ci}
35562306a36Sopenharmony_ci
35662306a36Sopenharmony_cistatic int oti6858_write(struct tty_struct *tty, struct usb_serial_port *port,
35762306a36Sopenharmony_ci			const unsigned char *buf, int count)
35862306a36Sopenharmony_ci{
35962306a36Sopenharmony_ci	if (!count)
36062306a36Sopenharmony_ci		return count;
36162306a36Sopenharmony_ci
36262306a36Sopenharmony_ci	count = kfifo_in_locked(&port->write_fifo, buf, count, &port->lock);
36362306a36Sopenharmony_ci
36462306a36Sopenharmony_ci	return count;
36562306a36Sopenharmony_ci}
36662306a36Sopenharmony_ci
36762306a36Sopenharmony_cistatic unsigned int oti6858_write_room(struct tty_struct *tty)
36862306a36Sopenharmony_ci{
36962306a36Sopenharmony_ci	struct usb_serial_port *port = tty->driver_data;
37062306a36Sopenharmony_ci	unsigned int room;
37162306a36Sopenharmony_ci	unsigned long flags;
37262306a36Sopenharmony_ci
37362306a36Sopenharmony_ci	spin_lock_irqsave(&port->lock, flags);
37462306a36Sopenharmony_ci	room = kfifo_avail(&port->write_fifo);
37562306a36Sopenharmony_ci	spin_unlock_irqrestore(&port->lock, flags);
37662306a36Sopenharmony_ci
37762306a36Sopenharmony_ci	return room;
37862306a36Sopenharmony_ci}
37962306a36Sopenharmony_ci
38062306a36Sopenharmony_cistatic unsigned int oti6858_chars_in_buffer(struct tty_struct *tty)
38162306a36Sopenharmony_ci{
38262306a36Sopenharmony_ci	struct usb_serial_port *port = tty->driver_data;
38362306a36Sopenharmony_ci	unsigned int chars;
38462306a36Sopenharmony_ci	unsigned long flags;
38562306a36Sopenharmony_ci
38662306a36Sopenharmony_ci	spin_lock_irqsave(&port->lock, flags);
38762306a36Sopenharmony_ci	chars = kfifo_len(&port->write_fifo);
38862306a36Sopenharmony_ci	spin_unlock_irqrestore(&port->lock, flags);
38962306a36Sopenharmony_ci
39062306a36Sopenharmony_ci	return chars;
39162306a36Sopenharmony_ci}
39262306a36Sopenharmony_ci
39362306a36Sopenharmony_cistatic void oti6858_init_termios(struct tty_struct *tty)
39462306a36Sopenharmony_ci{
39562306a36Sopenharmony_ci	tty_encode_baud_rate(tty, 38400, 38400);
39662306a36Sopenharmony_ci}
39762306a36Sopenharmony_ci
39862306a36Sopenharmony_cistatic void oti6858_set_termios(struct tty_struct *tty,
39962306a36Sopenharmony_ci				struct usb_serial_port *port,
40062306a36Sopenharmony_ci				const struct ktermios *old_termios)
40162306a36Sopenharmony_ci{
40262306a36Sopenharmony_ci	struct oti6858_private *priv = usb_get_serial_port_data(port);
40362306a36Sopenharmony_ci	unsigned long flags;
40462306a36Sopenharmony_ci	unsigned int cflag;
40562306a36Sopenharmony_ci	u8 frame_fmt, control;
40662306a36Sopenharmony_ci	__le16 divisor;
40762306a36Sopenharmony_ci	int br;
40862306a36Sopenharmony_ci
40962306a36Sopenharmony_ci	cflag = tty->termios.c_cflag;
41062306a36Sopenharmony_ci
41162306a36Sopenharmony_ci	spin_lock_irqsave(&priv->lock, flags);
41262306a36Sopenharmony_ci	divisor = priv->pending_setup.divisor;
41362306a36Sopenharmony_ci	frame_fmt = priv->pending_setup.frame_fmt;
41462306a36Sopenharmony_ci	control = priv->pending_setup.control;
41562306a36Sopenharmony_ci	spin_unlock_irqrestore(&priv->lock, flags);
41662306a36Sopenharmony_ci
41762306a36Sopenharmony_ci	frame_fmt &= ~FMT_DATA_BITS_MASK;
41862306a36Sopenharmony_ci	switch (cflag & CSIZE) {
41962306a36Sopenharmony_ci	case CS5:
42062306a36Sopenharmony_ci		frame_fmt |= FMT_DATA_BITS_5;
42162306a36Sopenharmony_ci		break;
42262306a36Sopenharmony_ci	case CS6:
42362306a36Sopenharmony_ci		frame_fmt |= FMT_DATA_BITS_6;
42462306a36Sopenharmony_ci		break;
42562306a36Sopenharmony_ci	case CS7:
42662306a36Sopenharmony_ci		frame_fmt |= FMT_DATA_BITS_7;
42762306a36Sopenharmony_ci		break;
42862306a36Sopenharmony_ci	default:
42962306a36Sopenharmony_ci	case CS8:
43062306a36Sopenharmony_ci		frame_fmt |= FMT_DATA_BITS_8;
43162306a36Sopenharmony_ci		break;
43262306a36Sopenharmony_ci	}
43362306a36Sopenharmony_ci
43462306a36Sopenharmony_ci	/* manufacturer claims that this device can work with baud rates
43562306a36Sopenharmony_ci	 * up to 3 Mbps; I've tested it only on 115200 bps, so I can't
43662306a36Sopenharmony_ci	 * guarantee that any other baud rate will work (especially
43762306a36Sopenharmony_ci	 * the higher ones)
43862306a36Sopenharmony_ci	 */
43962306a36Sopenharmony_ci	br = tty_get_baud_rate(tty);
44062306a36Sopenharmony_ci	if (br == 0) {
44162306a36Sopenharmony_ci		divisor = 0;
44262306a36Sopenharmony_ci	} else {
44362306a36Sopenharmony_ci		int real_br;
44462306a36Sopenharmony_ci		int new_divisor;
44562306a36Sopenharmony_ci		br = min(br, OTI6858_MAX_BAUD_RATE);
44662306a36Sopenharmony_ci
44762306a36Sopenharmony_ci		new_divisor = (96000000 + 8 * br) / (16 * br);
44862306a36Sopenharmony_ci		real_br = 96000000 / (16 * new_divisor);
44962306a36Sopenharmony_ci		divisor = cpu_to_le16(new_divisor);
45062306a36Sopenharmony_ci		tty_encode_baud_rate(tty, real_br, real_br);
45162306a36Sopenharmony_ci	}
45262306a36Sopenharmony_ci
45362306a36Sopenharmony_ci	frame_fmt &= ~FMT_STOP_BITS_MASK;
45462306a36Sopenharmony_ci	if ((cflag & CSTOPB) != 0)
45562306a36Sopenharmony_ci		frame_fmt |= FMT_STOP_BITS_2;
45662306a36Sopenharmony_ci	else
45762306a36Sopenharmony_ci		frame_fmt |= FMT_STOP_BITS_1;
45862306a36Sopenharmony_ci
45962306a36Sopenharmony_ci	frame_fmt &= ~FMT_PARITY_MASK;
46062306a36Sopenharmony_ci	if ((cflag & PARENB) != 0) {
46162306a36Sopenharmony_ci		if ((cflag & PARODD) != 0)
46262306a36Sopenharmony_ci			frame_fmt |= FMT_PARITY_ODD;
46362306a36Sopenharmony_ci		else
46462306a36Sopenharmony_ci			frame_fmt |= FMT_PARITY_EVEN;
46562306a36Sopenharmony_ci	} else {
46662306a36Sopenharmony_ci		frame_fmt |= FMT_PARITY_NONE;
46762306a36Sopenharmony_ci	}
46862306a36Sopenharmony_ci
46962306a36Sopenharmony_ci	control &= ~CONTROL_MASK;
47062306a36Sopenharmony_ci	if ((cflag & CRTSCTS) != 0)
47162306a36Sopenharmony_ci		control |= (CONTROL_DTR_HIGH | CONTROL_RTS_HIGH);
47262306a36Sopenharmony_ci
47362306a36Sopenharmony_ci	/* change control lines if we are switching to or from B0 */
47462306a36Sopenharmony_ci	/* FIXME:
47562306a36Sopenharmony_ci	spin_lock_irqsave(&priv->lock, flags);
47662306a36Sopenharmony_ci	control = priv->line_control;
47762306a36Sopenharmony_ci	if ((cflag & CBAUD) == B0)
47862306a36Sopenharmony_ci		priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
47962306a36Sopenharmony_ci	else
48062306a36Sopenharmony_ci		priv->line_control |= (CONTROL_DTR | CONTROL_RTS);
48162306a36Sopenharmony_ci	if (control != priv->line_control) {
48262306a36Sopenharmony_ci		control = priv->line_control;
48362306a36Sopenharmony_ci		spin_unlock_irqrestore(&priv->lock, flags);
48462306a36Sopenharmony_ci		set_control_lines(serial->dev, control);
48562306a36Sopenharmony_ci	} else {
48662306a36Sopenharmony_ci		spin_unlock_irqrestore(&priv->lock, flags);
48762306a36Sopenharmony_ci	}
48862306a36Sopenharmony_ci	*/
48962306a36Sopenharmony_ci
49062306a36Sopenharmony_ci	spin_lock_irqsave(&priv->lock, flags);
49162306a36Sopenharmony_ci	if (divisor != priv->pending_setup.divisor
49262306a36Sopenharmony_ci			|| control != priv->pending_setup.control
49362306a36Sopenharmony_ci			|| frame_fmt != priv->pending_setup.frame_fmt) {
49462306a36Sopenharmony_ci		priv->pending_setup.divisor = divisor;
49562306a36Sopenharmony_ci		priv->pending_setup.control = control;
49662306a36Sopenharmony_ci		priv->pending_setup.frame_fmt = frame_fmt;
49762306a36Sopenharmony_ci	}
49862306a36Sopenharmony_ci	spin_unlock_irqrestore(&priv->lock, flags);
49962306a36Sopenharmony_ci}
50062306a36Sopenharmony_ci
50162306a36Sopenharmony_cistatic int oti6858_open(struct tty_struct *tty, struct usb_serial_port *port)
50262306a36Sopenharmony_ci{
50362306a36Sopenharmony_ci	struct oti6858_private *priv = usb_get_serial_port_data(port);
50462306a36Sopenharmony_ci	struct usb_serial *serial = port->serial;
50562306a36Sopenharmony_ci	struct oti6858_control_pkt *buf;
50662306a36Sopenharmony_ci	unsigned long flags;
50762306a36Sopenharmony_ci	int result;
50862306a36Sopenharmony_ci
50962306a36Sopenharmony_ci	usb_clear_halt(serial->dev, port->write_urb->pipe);
51062306a36Sopenharmony_ci	usb_clear_halt(serial->dev, port->read_urb->pipe);
51162306a36Sopenharmony_ci
51262306a36Sopenharmony_ci	buf = kmalloc(OTI6858_CTRL_PKT_SIZE, GFP_KERNEL);
51362306a36Sopenharmony_ci	if (!buf)
51462306a36Sopenharmony_ci		return -ENOMEM;
51562306a36Sopenharmony_ci
51662306a36Sopenharmony_ci	result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
51762306a36Sopenharmony_ci				OTI6858_REQ_T_GET_STATUS,
51862306a36Sopenharmony_ci				OTI6858_REQ_GET_STATUS,
51962306a36Sopenharmony_ci				0, 0,
52062306a36Sopenharmony_ci				buf, OTI6858_CTRL_PKT_SIZE,
52162306a36Sopenharmony_ci				100);
52262306a36Sopenharmony_ci	if (result != OTI6858_CTRL_PKT_SIZE) {
52362306a36Sopenharmony_ci		/* assume default (after power-on reset) values */
52462306a36Sopenharmony_ci		buf->divisor = cpu_to_le16(0x009c);	/* 38400 bps */
52562306a36Sopenharmony_ci		buf->frame_fmt = 0x03;	/* 8N1 */
52662306a36Sopenharmony_ci		buf->something = 0x43;
52762306a36Sopenharmony_ci		buf->control = 0x4c;	/* DTR, RTS */
52862306a36Sopenharmony_ci		buf->tx_status = 0x00;
52962306a36Sopenharmony_ci		buf->pin_state = 0x5b;	/* RTS, CTS, DSR, DTR, RI, DCD */
53062306a36Sopenharmony_ci		buf->rx_bytes_avail = 0x00;
53162306a36Sopenharmony_ci	}
53262306a36Sopenharmony_ci
53362306a36Sopenharmony_ci	spin_lock_irqsave(&priv->lock, flags);
53462306a36Sopenharmony_ci	memcpy(&priv->status, buf, OTI6858_CTRL_PKT_SIZE);
53562306a36Sopenharmony_ci	priv->pending_setup.divisor = buf->divisor;
53662306a36Sopenharmony_ci	priv->pending_setup.frame_fmt = buf->frame_fmt;
53762306a36Sopenharmony_ci	priv->pending_setup.control = buf->control;
53862306a36Sopenharmony_ci	spin_unlock_irqrestore(&priv->lock, flags);
53962306a36Sopenharmony_ci	kfree(buf);
54062306a36Sopenharmony_ci
54162306a36Sopenharmony_ci	dev_dbg(&port->dev, "%s(): submitting interrupt urb\n", __func__);
54262306a36Sopenharmony_ci	result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
54362306a36Sopenharmony_ci	if (result != 0) {
54462306a36Sopenharmony_ci		dev_err(&port->dev, "%s(): usb_submit_urb() failed with error %d\n",
54562306a36Sopenharmony_ci			__func__, result);
54662306a36Sopenharmony_ci		oti6858_close(port);
54762306a36Sopenharmony_ci		return result;
54862306a36Sopenharmony_ci	}
54962306a36Sopenharmony_ci
55062306a36Sopenharmony_ci	/* setup termios */
55162306a36Sopenharmony_ci	if (tty)
55262306a36Sopenharmony_ci		oti6858_set_termios(tty, port, NULL);
55362306a36Sopenharmony_ci
55462306a36Sopenharmony_ci	return 0;
55562306a36Sopenharmony_ci}
55662306a36Sopenharmony_ci
55762306a36Sopenharmony_cistatic void oti6858_close(struct usb_serial_port *port)
55862306a36Sopenharmony_ci{
55962306a36Sopenharmony_ci	struct oti6858_private *priv = usb_get_serial_port_data(port);
56062306a36Sopenharmony_ci	unsigned long flags;
56162306a36Sopenharmony_ci
56262306a36Sopenharmony_ci	spin_lock_irqsave(&port->lock, flags);
56362306a36Sopenharmony_ci	/* clear out any remaining data in the buffer */
56462306a36Sopenharmony_ci	kfifo_reset_out(&port->write_fifo);
56562306a36Sopenharmony_ci	spin_unlock_irqrestore(&port->lock, flags);
56662306a36Sopenharmony_ci
56762306a36Sopenharmony_ci	dev_dbg(&port->dev, "%s(): after buf_clear()\n", __func__);
56862306a36Sopenharmony_ci
56962306a36Sopenharmony_ci	/* cancel scheduled setup */
57062306a36Sopenharmony_ci	cancel_delayed_work_sync(&priv->delayed_setup_work);
57162306a36Sopenharmony_ci	cancel_delayed_work_sync(&priv->delayed_write_work);
57262306a36Sopenharmony_ci
57362306a36Sopenharmony_ci	/* shutdown our urbs */
57462306a36Sopenharmony_ci	dev_dbg(&port->dev, "%s(): shutting down urbs\n", __func__);
57562306a36Sopenharmony_ci	usb_kill_urb(port->write_urb);
57662306a36Sopenharmony_ci	usb_kill_urb(port->read_urb);
57762306a36Sopenharmony_ci	usb_kill_urb(port->interrupt_in_urb);
57862306a36Sopenharmony_ci}
57962306a36Sopenharmony_ci
58062306a36Sopenharmony_cistatic int oti6858_tiocmset(struct tty_struct *tty,
58162306a36Sopenharmony_ci				unsigned int set, unsigned int clear)
58262306a36Sopenharmony_ci{
58362306a36Sopenharmony_ci	struct usb_serial_port *port = tty->driver_data;
58462306a36Sopenharmony_ci	struct oti6858_private *priv = usb_get_serial_port_data(port);
58562306a36Sopenharmony_ci	unsigned long flags;
58662306a36Sopenharmony_ci	u8 control;
58762306a36Sopenharmony_ci
58862306a36Sopenharmony_ci	dev_dbg(&port->dev, "%s(set = 0x%08x, clear = 0x%08x)\n",
58962306a36Sopenharmony_ci		__func__, set, clear);
59062306a36Sopenharmony_ci
59162306a36Sopenharmony_ci	/* FIXME: check if this is correct (active high/low) */
59262306a36Sopenharmony_ci	spin_lock_irqsave(&priv->lock, flags);
59362306a36Sopenharmony_ci	control = priv->pending_setup.control;
59462306a36Sopenharmony_ci	if ((set & TIOCM_RTS) != 0)
59562306a36Sopenharmony_ci		control |= CONTROL_RTS_HIGH;
59662306a36Sopenharmony_ci	if ((set & TIOCM_DTR) != 0)
59762306a36Sopenharmony_ci		control |= CONTROL_DTR_HIGH;
59862306a36Sopenharmony_ci	if ((clear & TIOCM_RTS) != 0)
59962306a36Sopenharmony_ci		control &= ~CONTROL_RTS_HIGH;
60062306a36Sopenharmony_ci	if ((clear & TIOCM_DTR) != 0)
60162306a36Sopenharmony_ci		control &= ~CONTROL_DTR_HIGH;
60262306a36Sopenharmony_ci
60362306a36Sopenharmony_ci	if (control != priv->pending_setup.control)
60462306a36Sopenharmony_ci		priv->pending_setup.control = control;
60562306a36Sopenharmony_ci
60662306a36Sopenharmony_ci	spin_unlock_irqrestore(&priv->lock, flags);
60762306a36Sopenharmony_ci	return 0;
60862306a36Sopenharmony_ci}
60962306a36Sopenharmony_ci
61062306a36Sopenharmony_cistatic int oti6858_tiocmget(struct tty_struct *tty)
61162306a36Sopenharmony_ci{
61262306a36Sopenharmony_ci	struct usb_serial_port *port = tty->driver_data;
61362306a36Sopenharmony_ci	struct oti6858_private *priv = usb_get_serial_port_data(port);
61462306a36Sopenharmony_ci	unsigned long flags;
61562306a36Sopenharmony_ci	unsigned pin_state;
61662306a36Sopenharmony_ci	unsigned result = 0;
61762306a36Sopenharmony_ci
61862306a36Sopenharmony_ci	spin_lock_irqsave(&priv->lock, flags);
61962306a36Sopenharmony_ci	pin_state = priv->status.pin_state & PIN_MASK;
62062306a36Sopenharmony_ci	spin_unlock_irqrestore(&priv->lock, flags);
62162306a36Sopenharmony_ci
62262306a36Sopenharmony_ci	/* FIXME: check if this is correct (active high/low) */
62362306a36Sopenharmony_ci	if ((pin_state & PIN_RTS) != 0)
62462306a36Sopenharmony_ci		result |= TIOCM_RTS;
62562306a36Sopenharmony_ci	if ((pin_state & PIN_CTS) != 0)
62662306a36Sopenharmony_ci		result |= TIOCM_CTS;
62762306a36Sopenharmony_ci	if ((pin_state & PIN_DSR) != 0)
62862306a36Sopenharmony_ci		result |= TIOCM_DSR;
62962306a36Sopenharmony_ci	if ((pin_state & PIN_DTR) != 0)
63062306a36Sopenharmony_ci		result |= TIOCM_DTR;
63162306a36Sopenharmony_ci	if ((pin_state & PIN_RI) != 0)
63262306a36Sopenharmony_ci		result |= TIOCM_RI;
63362306a36Sopenharmony_ci	if ((pin_state & PIN_DCD) != 0)
63462306a36Sopenharmony_ci		result |= TIOCM_CD;
63562306a36Sopenharmony_ci
63662306a36Sopenharmony_ci	dev_dbg(&port->dev, "%s() = 0x%08x\n", __func__, result);
63762306a36Sopenharmony_ci
63862306a36Sopenharmony_ci	return result;
63962306a36Sopenharmony_ci}
64062306a36Sopenharmony_ci
64162306a36Sopenharmony_cistatic void oti6858_read_int_callback(struct urb *urb)
64262306a36Sopenharmony_ci{
64362306a36Sopenharmony_ci	struct usb_serial_port *port =  urb->context;
64462306a36Sopenharmony_ci	struct oti6858_private *priv = usb_get_serial_port_data(port);
64562306a36Sopenharmony_ci	int transient = 0, can_recv = 0, resubmit = 1;
64662306a36Sopenharmony_ci	int status = urb->status;
64762306a36Sopenharmony_ci
64862306a36Sopenharmony_ci	switch (status) {
64962306a36Sopenharmony_ci	case 0:
65062306a36Sopenharmony_ci		/* success */
65162306a36Sopenharmony_ci		break;
65262306a36Sopenharmony_ci	case -ECONNRESET:
65362306a36Sopenharmony_ci	case -ENOENT:
65462306a36Sopenharmony_ci	case -ESHUTDOWN:
65562306a36Sopenharmony_ci		/* this urb is terminated, clean up */
65662306a36Sopenharmony_ci		dev_dbg(&urb->dev->dev, "%s(): urb shutting down with status: %d\n",
65762306a36Sopenharmony_ci			__func__, status);
65862306a36Sopenharmony_ci		return;
65962306a36Sopenharmony_ci	default:
66062306a36Sopenharmony_ci		dev_dbg(&urb->dev->dev, "%s(): nonzero urb status received: %d\n",
66162306a36Sopenharmony_ci			__func__, status);
66262306a36Sopenharmony_ci		break;
66362306a36Sopenharmony_ci	}
66462306a36Sopenharmony_ci
66562306a36Sopenharmony_ci	if (status == 0 && urb->actual_length == OTI6858_CTRL_PKT_SIZE) {
66662306a36Sopenharmony_ci		struct oti6858_control_pkt *xs = urb->transfer_buffer;
66762306a36Sopenharmony_ci		unsigned long flags;
66862306a36Sopenharmony_ci
66962306a36Sopenharmony_ci		spin_lock_irqsave(&priv->lock, flags);
67062306a36Sopenharmony_ci
67162306a36Sopenharmony_ci		if (!priv->transient) {
67262306a36Sopenharmony_ci			if (!OTI6858_CTRL_EQUALS_PENDING(xs, priv)) {
67362306a36Sopenharmony_ci				if (xs->rx_bytes_avail == 0) {
67462306a36Sopenharmony_ci					priv->transient = 4;
67562306a36Sopenharmony_ci					priv->setup_done = 0;
67662306a36Sopenharmony_ci					resubmit = 0;
67762306a36Sopenharmony_ci					dev_dbg(&port->dev, "%s(): scheduling setup_line()\n", __func__);
67862306a36Sopenharmony_ci					schedule_delayed_work(&priv->delayed_setup_work, 0);
67962306a36Sopenharmony_ci				}
68062306a36Sopenharmony_ci			}
68162306a36Sopenharmony_ci		} else {
68262306a36Sopenharmony_ci			if (OTI6858_CTRL_EQUALS_PENDING(xs, priv)) {
68362306a36Sopenharmony_ci				priv->transient = 0;
68462306a36Sopenharmony_ci			} else if (!priv->setup_done) {
68562306a36Sopenharmony_ci				resubmit = 0;
68662306a36Sopenharmony_ci			} else if (--priv->transient == 0) {
68762306a36Sopenharmony_ci				if (xs->rx_bytes_avail == 0) {
68862306a36Sopenharmony_ci					priv->transient = 4;
68962306a36Sopenharmony_ci					priv->setup_done = 0;
69062306a36Sopenharmony_ci					resubmit = 0;
69162306a36Sopenharmony_ci					dev_dbg(&port->dev, "%s(): scheduling setup_line()\n", __func__);
69262306a36Sopenharmony_ci					schedule_delayed_work(&priv->delayed_setup_work, 0);
69362306a36Sopenharmony_ci				}
69462306a36Sopenharmony_ci			}
69562306a36Sopenharmony_ci		}
69662306a36Sopenharmony_ci
69762306a36Sopenharmony_ci		if (!priv->transient) {
69862306a36Sopenharmony_ci			u8 delta = xs->pin_state ^ priv->status.pin_state;
69962306a36Sopenharmony_ci
70062306a36Sopenharmony_ci			if (delta & PIN_MSR_MASK) {
70162306a36Sopenharmony_ci				if (delta & PIN_CTS)
70262306a36Sopenharmony_ci					port->icount.cts++;
70362306a36Sopenharmony_ci				if (delta & PIN_DSR)
70462306a36Sopenharmony_ci					port->icount.dsr++;
70562306a36Sopenharmony_ci				if (delta & PIN_RI)
70662306a36Sopenharmony_ci					port->icount.rng++;
70762306a36Sopenharmony_ci				if (delta & PIN_DCD)
70862306a36Sopenharmony_ci					port->icount.dcd++;
70962306a36Sopenharmony_ci
71062306a36Sopenharmony_ci				wake_up_interruptible(&port->port.delta_msr_wait);
71162306a36Sopenharmony_ci			}
71262306a36Sopenharmony_ci
71362306a36Sopenharmony_ci			memcpy(&priv->status, xs, OTI6858_CTRL_PKT_SIZE);
71462306a36Sopenharmony_ci		}
71562306a36Sopenharmony_ci
71662306a36Sopenharmony_ci		if (!priv->transient && xs->rx_bytes_avail != 0) {
71762306a36Sopenharmony_ci			can_recv = xs->rx_bytes_avail;
71862306a36Sopenharmony_ci			priv->flags.read_urb_in_use = 1;
71962306a36Sopenharmony_ci		}
72062306a36Sopenharmony_ci
72162306a36Sopenharmony_ci		transient = priv->transient;
72262306a36Sopenharmony_ci		spin_unlock_irqrestore(&priv->lock, flags);
72362306a36Sopenharmony_ci	}
72462306a36Sopenharmony_ci
72562306a36Sopenharmony_ci	if (can_recv) {
72662306a36Sopenharmony_ci		int result;
72762306a36Sopenharmony_ci
72862306a36Sopenharmony_ci		result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
72962306a36Sopenharmony_ci		if (result != 0) {
73062306a36Sopenharmony_ci			priv->flags.read_urb_in_use = 0;
73162306a36Sopenharmony_ci			dev_err(&port->dev, "%s(): usb_submit_urb() failed,"
73262306a36Sopenharmony_ci					" error %d\n", __func__, result);
73362306a36Sopenharmony_ci		} else {
73462306a36Sopenharmony_ci			resubmit = 0;
73562306a36Sopenharmony_ci		}
73662306a36Sopenharmony_ci	} else if (!transient) {
73762306a36Sopenharmony_ci		unsigned long flags;
73862306a36Sopenharmony_ci		int count;
73962306a36Sopenharmony_ci
74062306a36Sopenharmony_ci		spin_lock_irqsave(&port->lock, flags);
74162306a36Sopenharmony_ci		count = kfifo_len(&port->write_fifo);
74262306a36Sopenharmony_ci		spin_unlock_irqrestore(&port->lock, flags);
74362306a36Sopenharmony_ci
74462306a36Sopenharmony_ci		spin_lock_irqsave(&priv->lock, flags);
74562306a36Sopenharmony_ci		if (priv->flags.write_urb_in_use == 0 && count != 0) {
74662306a36Sopenharmony_ci			schedule_delayed_work(&priv->delayed_write_work, 0);
74762306a36Sopenharmony_ci			resubmit = 0;
74862306a36Sopenharmony_ci		}
74962306a36Sopenharmony_ci		spin_unlock_irqrestore(&priv->lock, flags);
75062306a36Sopenharmony_ci	}
75162306a36Sopenharmony_ci
75262306a36Sopenharmony_ci	if (resubmit) {
75362306a36Sopenharmony_ci		int result;
75462306a36Sopenharmony_ci
75562306a36Sopenharmony_ci/*		dev_dbg(&urb->dev->dev, "%s(): submitting interrupt urb\n", __func__); */
75662306a36Sopenharmony_ci		result = usb_submit_urb(urb, GFP_ATOMIC);
75762306a36Sopenharmony_ci		if (result != 0) {
75862306a36Sopenharmony_ci			dev_err(&urb->dev->dev,
75962306a36Sopenharmony_ci					"%s(): usb_submit_urb() failed with"
76062306a36Sopenharmony_ci					" error %d\n", __func__, result);
76162306a36Sopenharmony_ci		}
76262306a36Sopenharmony_ci	}
76362306a36Sopenharmony_ci}
76462306a36Sopenharmony_ci
76562306a36Sopenharmony_cistatic void oti6858_read_bulk_callback(struct urb *urb)
76662306a36Sopenharmony_ci{
76762306a36Sopenharmony_ci	struct usb_serial_port *port =  urb->context;
76862306a36Sopenharmony_ci	struct oti6858_private *priv = usb_get_serial_port_data(port);
76962306a36Sopenharmony_ci	unsigned char *data = urb->transfer_buffer;
77062306a36Sopenharmony_ci	unsigned long flags;
77162306a36Sopenharmony_ci	int status = urb->status;
77262306a36Sopenharmony_ci	int result;
77362306a36Sopenharmony_ci
77462306a36Sopenharmony_ci	spin_lock_irqsave(&priv->lock, flags);
77562306a36Sopenharmony_ci	priv->flags.read_urb_in_use = 0;
77662306a36Sopenharmony_ci	spin_unlock_irqrestore(&priv->lock, flags);
77762306a36Sopenharmony_ci
77862306a36Sopenharmony_ci	if (status != 0) {
77962306a36Sopenharmony_ci		dev_dbg(&urb->dev->dev, "%s(): unable to handle the error, exiting\n", __func__);
78062306a36Sopenharmony_ci		return;
78162306a36Sopenharmony_ci	}
78262306a36Sopenharmony_ci
78362306a36Sopenharmony_ci	if (urb->actual_length > 0) {
78462306a36Sopenharmony_ci		tty_insert_flip_string(&port->port, data, urb->actual_length);
78562306a36Sopenharmony_ci		tty_flip_buffer_push(&port->port);
78662306a36Sopenharmony_ci	}
78762306a36Sopenharmony_ci
78862306a36Sopenharmony_ci	/* schedule the interrupt urb */
78962306a36Sopenharmony_ci	result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
79062306a36Sopenharmony_ci	if (result != 0 && result != -EPERM) {
79162306a36Sopenharmony_ci		dev_err(&port->dev, "%s(): usb_submit_urb() failed,"
79262306a36Sopenharmony_ci				" error %d\n", __func__, result);
79362306a36Sopenharmony_ci	}
79462306a36Sopenharmony_ci}
79562306a36Sopenharmony_ci
79662306a36Sopenharmony_cistatic void oti6858_write_bulk_callback(struct urb *urb)
79762306a36Sopenharmony_ci{
79862306a36Sopenharmony_ci	struct usb_serial_port *port =  urb->context;
79962306a36Sopenharmony_ci	struct oti6858_private *priv = usb_get_serial_port_data(port);
80062306a36Sopenharmony_ci	int status = urb->status;
80162306a36Sopenharmony_ci	int result;
80262306a36Sopenharmony_ci
80362306a36Sopenharmony_ci	switch (status) {
80462306a36Sopenharmony_ci	case 0:
80562306a36Sopenharmony_ci		/* success */
80662306a36Sopenharmony_ci		break;
80762306a36Sopenharmony_ci	case -ECONNRESET:
80862306a36Sopenharmony_ci	case -ENOENT:
80962306a36Sopenharmony_ci	case -ESHUTDOWN:
81062306a36Sopenharmony_ci		/* this urb is terminated, clean up */
81162306a36Sopenharmony_ci		dev_dbg(&urb->dev->dev, "%s(): urb shutting down with status: %d\n", __func__, status);
81262306a36Sopenharmony_ci		priv->flags.write_urb_in_use = 0;
81362306a36Sopenharmony_ci		return;
81462306a36Sopenharmony_ci	default:
81562306a36Sopenharmony_ci		/* error in the urb, so we have to resubmit it */
81662306a36Sopenharmony_ci		dev_dbg(&urb->dev->dev, "%s(): nonzero write bulk status received: %d\n", __func__, status);
81762306a36Sopenharmony_ci		dev_dbg(&urb->dev->dev, "%s(): overflow in write\n", __func__);
81862306a36Sopenharmony_ci
81962306a36Sopenharmony_ci		port->write_urb->transfer_buffer_length = 1;
82062306a36Sopenharmony_ci		result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
82162306a36Sopenharmony_ci		if (result) {
82262306a36Sopenharmony_ci			dev_err_console(port, "%s(): usb_submit_urb() failed,"
82362306a36Sopenharmony_ci					" error %d\n", __func__, result);
82462306a36Sopenharmony_ci		} else {
82562306a36Sopenharmony_ci			return;
82662306a36Sopenharmony_ci		}
82762306a36Sopenharmony_ci	}
82862306a36Sopenharmony_ci
82962306a36Sopenharmony_ci	priv->flags.write_urb_in_use = 0;
83062306a36Sopenharmony_ci
83162306a36Sopenharmony_ci	/* schedule the interrupt urb if we are still open */
83262306a36Sopenharmony_ci	dev_dbg(&port->dev, "%s(): submitting interrupt urb\n", __func__);
83362306a36Sopenharmony_ci	result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
83462306a36Sopenharmony_ci	if (result != 0) {
83562306a36Sopenharmony_ci		dev_err(&port->dev, "%s(): failed submitting int urb,"
83662306a36Sopenharmony_ci					" error %d\n", __func__, result);
83762306a36Sopenharmony_ci	}
83862306a36Sopenharmony_ci}
83962306a36Sopenharmony_ci
84062306a36Sopenharmony_cimodule_usb_serial_driver(serial_drivers, id_table);
84162306a36Sopenharmony_ci
84262306a36Sopenharmony_ciMODULE_DESCRIPTION(OTI6858_DESCRIPTION);
84362306a36Sopenharmony_ciMODULE_AUTHOR(OTI6858_AUTHOR);
84462306a36Sopenharmony_ciMODULE_LICENSE("GPL v2");
845