xref: /linux/drivers/usb/serial/oti6858.c (revision 3a39d672e7f48b8d6b91a09afa4b55352773b4b5)
15fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
249cdee0eSKees Lemmens /*
349cdee0eSKees Lemmens  * Ours Technology Inc. OTi-6858 USB to serial adapter driver.
449cdee0eSKees Lemmens  *
549cdee0eSKees Lemmens  * Copyleft  (C) 2007 Kees Lemmens (adapted for kernel 2.6.20)
649cdee0eSKees Lemmens  * Copyright (C) 2006 Tomasz Michal Lukaszewski (FIXME: add e-mail)
749cdee0eSKees Lemmens  * Copyright (C) 2001-2004 Greg Kroah-Hartman (greg@kroah.com)
849cdee0eSKees Lemmens  * Copyright (C) 2003 IBM Corp.
949cdee0eSKees Lemmens  *
1049cdee0eSKees Lemmens  * Many thanks to the authors of pl2303 driver: all functions in this file
1149cdee0eSKees Lemmens  * are heavily based on pl2303 code, buffering code is a 1-to-1 copy.
1249cdee0eSKees Lemmens  *
1349cdee0eSKees Lemmens  * Warning! You use this driver on your own risk! The only official
1449cdee0eSKees Lemmens  * description of this device I have is datasheet from manufacturer,
1549cdee0eSKees Lemmens  * and it doesn't contain almost any information needed to write a driver.
1649cdee0eSKees Lemmens  * Almost all knowlegde used while writing this driver was gathered by:
1749cdee0eSKees Lemmens  *  - analyzing traffic between device and the M$ Windows 2000 driver,
1849cdee0eSKees Lemmens  *  - trying different bit combinations and checking pin states
1949cdee0eSKees Lemmens  *    with a voltmeter,
2049cdee0eSKees Lemmens  *  - receiving malformed frames and producing buffer overflows
2149cdee0eSKees Lemmens  *    to learn how errors are reported,
2249cdee0eSKees Lemmens  * So, THIS CODE CAN DESTROY OTi-6858 AND ANY OTHER DEVICES, THAT ARE
2349cdee0eSKees Lemmens  * CONNECTED TO IT!
2449cdee0eSKees Lemmens  *
25ecefae6dSMauro Carvalho Chehab  * See Documentation/usb/usb-serial.rst for more information on using this
262a77c814SAlan Cox  * driver
2749cdee0eSKees Lemmens  *
2849cdee0eSKees Lemmens  * TODO:
2949cdee0eSKees Lemmens  *  - implement correct flushing for ioctls and oti6858_close()
3049cdee0eSKees Lemmens  *  - check how errors (rx overflow, parity error, framing error) are reported
3149cdee0eSKees Lemmens  *  - implement oti6858_break_ctl()
3249cdee0eSKees Lemmens  *  - implement more ioctls
3349cdee0eSKees Lemmens  *  - test/implement flow control
3449cdee0eSKees Lemmens  *  - allow setting custom baud rates
3549cdee0eSKees Lemmens  */
3649cdee0eSKees Lemmens 
3749cdee0eSKees Lemmens #include <linux/kernel.h>
3849cdee0eSKees Lemmens #include <linux/errno.h>
3949cdee0eSKees Lemmens #include <linux/slab.h>
4049cdee0eSKees Lemmens #include <linux/tty.h>
4149cdee0eSKees Lemmens #include <linux/tty_driver.h>
4249cdee0eSKees Lemmens #include <linux/tty_flip.h>
4349cdee0eSKees Lemmens #include <linux/serial.h>
4449cdee0eSKees Lemmens #include <linux/module.h>
4549cdee0eSKees Lemmens #include <linux/moduleparam.h>
4649cdee0eSKees Lemmens #include <linux/spinlock.h>
4749cdee0eSKees Lemmens #include <linux/usb.h>
4849cdee0eSKees Lemmens #include <linux/usb/serial.h>
492a77c814SAlan Cox #include <linux/uaccess.h>
50e3c1803fSJohan Hovold #include <linux/kfifo.h>
5149cdee0eSKees Lemmens #include "oti6858.h"
5249cdee0eSKees Lemmens 
5349cdee0eSKees Lemmens #define OTI6858_DESCRIPTION \
5449cdee0eSKees Lemmens 	"Ours Technology Inc. OTi-6858 USB to serial adapter driver"
5549cdee0eSKees Lemmens #define OTI6858_AUTHOR "Tomasz Michal Lukaszewski <FIXME@FIXME>"
5649cdee0eSKees Lemmens 
577d40d7e8SNémeth Márton static const struct usb_device_id id_table[] = {
5849cdee0eSKees Lemmens 	{ USB_DEVICE(OTI6858_VENDOR_ID, OTI6858_PRODUCT_ID) },
5949cdee0eSKees Lemmens 	{ }
6049cdee0eSKees Lemmens };
6149cdee0eSKees Lemmens 
6249cdee0eSKees Lemmens MODULE_DEVICE_TABLE(usb, id_table);
6349cdee0eSKees Lemmens 
6449cdee0eSKees Lemmens /* requests */
6549cdee0eSKees Lemmens #define	OTI6858_REQ_GET_STATUS		(USB_DIR_IN | USB_TYPE_VENDOR | 0x00)
6649cdee0eSKees Lemmens #define	OTI6858_REQ_T_GET_STATUS	0x01
6749cdee0eSKees Lemmens 
6849cdee0eSKees Lemmens #define	OTI6858_REQ_SET_LINE		(USB_DIR_OUT | USB_TYPE_VENDOR | 0x00)
6949cdee0eSKees Lemmens #define	OTI6858_REQ_T_SET_LINE		0x00
7049cdee0eSKees Lemmens 
7149cdee0eSKees Lemmens #define	OTI6858_REQ_CHECK_TXBUFF	(USB_DIR_IN | USB_TYPE_VENDOR | 0x01)
7249cdee0eSKees Lemmens #define	OTI6858_REQ_T_CHECK_TXBUFF	0x00
7349cdee0eSKees Lemmens 
7449cdee0eSKees Lemmens /* format of the control packet */
7549cdee0eSKees Lemmens struct oti6858_control_pkt {
76fd05e720SAl Viro 	__le16	divisor;	/* baud rate = 96000000 / (16 * divisor), LE */
7749cdee0eSKees Lemmens #define OTI6858_MAX_BAUD_RATE	3000000
7849cdee0eSKees Lemmens 	u8	frame_fmt;
7949cdee0eSKees Lemmens #define FMT_STOP_BITS_MASK	0xc0
8049cdee0eSKees Lemmens #define FMT_STOP_BITS_1		0x00
8149cdee0eSKees Lemmens #define FMT_STOP_BITS_2		0x40	/* 1.5 stop bits if FMT_DATA_BITS_5 */
8249cdee0eSKees Lemmens #define FMT_PARITY_MASK		0x38
8349cdee0eSKees Lemmens #define FMT_PARITY_NONE		0x00
8449cdee0eSKees Lemmens #define FMT_PARITY_ODD		0x08
8549cdee0eSKees Lemmens #define FMT_PARITY_EVEN		0x18
8649cdee0eSKees Lemmens #define FMT_PARITY_MARK		0x28
8749cdee0eSKees Lemmens #define FMT_PARITY_SPACE	0x38
8849cdee0eSKees Lemmens #define FMT_DATA_BITS_MASK	0x03
8949cdee0eSKees Lemmens #define FMT_DATA_BITS_5		0x00
9049cdee0eSKees Lemmens #define FMT_DATA_BITS_6		0x01
9149cdee0eSKees Lemmens #define FMT_DATA_BITS_7		0x02
9249cdee0eSKees Lemmens #define FMT_DATA_BITS_8		0x03
9349cdee0eSKees Lemmens 	u8	something;	/* always equals 0x43 */
9449cdee0eSKees Lemmens 	u8	control;	/* settings of flow control lines */
9549cdee0eSKees Lemmens #define CONTROL_MASK		0x0c
9649cdee0eSKees Lemmens #define CONTROL_DTR_HIGH	0x08
9749cdee0eSKees Lemmens #define CONTROL_RTS_HIGH	0x04
9849cdee0eSKees Lemmens 	u8	tx_status;
9949cdee0eSKees Lemmens #define	TX_BUFFER_EMPTIED	0x09
10049cdee0eSKees Lemmens 	u8	pin_state;
10149cdee0eSKees Lemmens #define PIN_MASK		0x3f
1021acc36e9SJohan Hovold #define PIN_MSR_MASK		0x1b
10349cdee0eSKees Lemmens #define PIN_RTS			0x20	/* output pin */
10449cdee0eSKees Lemmens #define PIN_CTS			0x10	/* input pin, active low */
10549cdee0eSKees Lemmens #define PIN_DSR			0x08	/* input pin, active low */
10649cdee0eSKees Lemmens #define PIN_DTR			0x04	/* output pin */
10749cdee0eSKees Lemmens #define PIN_RI			0x02	/* input pin, active low */
10849cdee0eSKees Lemmens #define PIN_DCD			0x01	/* input pin, active low */
10949cdee0eSKees Lemmens 	u8	rx_bytes_avail;		/* number of bytes in rx buffer */;
11049cdee0eSKees Lemmens };
11149cdee0eSKees Lemmens 
11249cdee0eSKees Lemmens #define OTI6858_CTRL_PKT_SIZE	sizeof(struct oti6858_control_pkt)
11349cdee0eSKees Lemmens #define OTI6858_CTRL_EQUALS_PENDING(a, priv) \
11449cdee0eSKees Lemmens 	(((a)->divisor == (priv)->pending_setup.divisor) \
11549cdee0eSKees Lemmens 	  && ((a)->control == (priv)->pending_setup.control) \
11649cdee0eSKees Lemmens 	  && ((a)->frame_fmt == (priv)->pending_setup.frame_fmt))
11749cdee0eSKees Lemmens 
11849cdee0eSKees Lemmens /* function prototypes */
119a509a7e4SAlan Cox static int oti6858_open(struct tty_struct *tty, struct usb_serial_port *port);
120335f8514SAlan Cox static void oti6858_close(struct usb_serial_port *port);
12195da310eSAlan Cox static void oti6858_set_termios(struct tty_struct *tty,
122*f6d47fe5SIlpo Järvinen 				struct usb_serial_port *port,
123*f6d47fe5SIlpo Järvinen 				const struct ktermios *old_termios);
124fe1ae7fdSAlan Cox static void oti6858_init_termios(struct tty_struct *tty);
12549cdee0eSKees Lemmens static void oti6858_read_int_callback(struct urb *urb);
12649cdee0eSKees Lemmens static void oti6858_read_bulk_callback(struct urb *urb);
12749cdee0eSKees Lemmens static void oti6858_write_bulk_callback(struct urb *urb);
12895da310eSAlan Cox static int oti6858_write(struct tty_struct *tty, struct usb_serial_port *port,
12949cdee0eSKees Lemmens 			const unsigned char *buf, int count);
13094cc7aeaSJiri Slaby static unsigned int oti6858_write_room(struct tty_struct *tty);
131155591d3SJiri Slaby static unsigned int oti6858_chars_in_buffer(struct tty_struct *tty);
13260b33c13SAlan Cox static int oti6858_tiocmget(struct tty_struct *tty);
13320b9d177SAlan Cox static int oti6858_tiocmset(struct tty_struct *tty,
13449cdee0eSKees Lemmens 				unsigned int set, unsigned int clear);
135289b076fSJohan Hovold static int oti6858_port_probe(struct usb_serial_port *port);
136c5d1448fSUwe Kleine-König static void oti6858_port_remove(struct usb_serial_port *port);
13749cdee0eSKees Lemmens 
13849cdee0eSKees Lemmens /* device info */
13949cdee0eSKees Lemmens static struct usb_serial_driver oti6858_device = {
14049cdee0eSKees Lemmens 	.driver = {
14149cdee0eSKees Lemmens 		.name =		"oti6858",
14249cdee0eSKees Lemmens 	},
14349cdee0eSKees Lemmens 	.id_table =		id_table,
14449cdee0eSKees Lemmens 	.num_ports =		1,
14532814c87SJohan Hovold 	.num_bulk_in =		1,
14632814c87SJohan Hovold 	.num_bulk_out =		1,
14732814c87SJohan Hovold 	.num_interrupt_in =	1,
14849cdee0eSKees Lemmens 	.open =			oti6858_open,
14949cdee0eSKees Lemmens 	.close =		oti6858_close,
15049cdee0eSKees Lemmens 	.write =		oti6858_write,
15149cdee0eSKees Lemmens 	.set_termios =		oti6858_set_termios,
152fe1ae7fdSAlan Cox 	.init_termios = 	oti6858_init_termios,
15349cdee0eSKees Lemmens 	.tiocmget =		oti6858_tiocmget,
15449cdee0eSKees Lemmens 	.tiocmset =		oti6858_tiocmset,
155bd6383c8SJohan Hovold 	.tiocmiwait =		usb_serial_generic_tiocmiwait,
15649cdee0eSKees Lemmens 	.read_bulk_callback =	oti6858_read_bulk_callback,
15749cdee0eSKees Lemmens 	.read_int_callback =	oti6858_read_int_callback,
15849cdee0eSKees Lemmens 	.write_bulk_callback =	oti6858_write_bulk_callback,
15949cdee0eSKees Lemmens 	.write_room =		oti6858_write_room,
16049cdee0eSKees Lemmens 	.chars_in_buffer =	oti6858_chars_in_buffer,
161289b076fSJohan Hovold 	.port_probe =		oti6858_port_probe,
162289b076fSJohan Hovold 	.port_remove =		oti6858_port_remove,
16349cdee0eSKees Lemmens };
16449cdee0eSKees Lemmens 
165f667ddadSAlan Stern static struct usb_serial_driver * const serial_drivers[] = {
166f667ddadSAlan Stern 	&oti6858_device, NULL
167f667ddadSAlan Stern };
168f667ddadSAlan Stern 
16949cdee0eSKees Lemmens struct oti6858_private {
17049cdee0eSKees Lemmens 	spinlock_t lock;
17149cdee0eSKees Lemmens 
17249cdee0eSKees Lemmens 	struct oti6858_control_pkt status;
17349cdee0eSKees Lemmens 
17449cdee0eSKees Lemmens 	struct {
17549cdee0eSKees Lemmens 		u8 read_urb_in_use;
17649cdee0eSKees Lemmens 		u8 write_urb_in_use;
17749cdee0eSKees Lemmens 	} flags;
17849cdee0eSKees Lemmens 	struct delayed_work delayed_write_work;
17949cdee0eSKees Lemmens 
18049cdee0eSKees Lemmens 	struct {
181fd05e720SAl Viro 		__le16 divisor;
18249cdee0eSKees Lemmens 		u8 frame_fmt;
18349cdee0eSKees Lemmens 		u8 control;
18449cdee0eSKees Lemmens 	} pending_setup;
18549cdee0eSKees Lemmens 	u8 transient;
18649cdee0eSKees Lemmens 	u8 setup_done;
18749cdee0eSKees Lemmens 	struct delayed_work delayed_setup_work;
18849cdee0eSKees Lemmens 
18949cdee0eSKees Lemmens 	struct usb_serial_port *port;   /* USB port with which associated */
19049cdee0eSKees Lemmens };
19149cdee0eSKees Lemmens 
setup_line(struct work_struct * work)19249cdee0eSKees Lemmens static void setup_line(struct work_struct *work)
19349cdee0eSKees Lemmens {
1942a77c814SAlan Cox 	struct oti6858_private *priv = container_of(work,
1952a77c814SAlan Cox 			struct oti6858_private, delayed_setup_work.work);
19649cdee0eSKees Lemmens 	struct usb_serial_port *port = priv->port;
19749cdee0eSKees Lemmens 	struct oti6858_control_pkt *new_setup;
19849cdee0eSKees Lemmens 	unsigned long flags;
19949cdee0eSKees Lemmens 	int result;
20049cdee0eSKees Lemmens 
2012a77c814SAlan Cox 	new_setup = kmalloc(OTI6858_CTRL_PKT_SIZE, GFP_KERNEL);
20210c642d0SJohan Hovold 	if (!new_setup) {
20349cdee0eSKees Lemmens 		/* we will try again */
2042a77c814SAlan Cox 		schedule_delayed_work(&priv->delayed_setup_work,
2052a77c814SAlan Cox 						msecs_to_jiffies(2));
20649cdee0eSKees Lemmens 		return;
20749cdee0eSKees Lemmens 	}
20849cdee0eSKees Lemmens 
20949cdee0eSKees Lemmens 	result = usb_control_msg(port->serial->dev,
21049cdee0eSKees Lemmens 				usb_rcvctrlpipe(port->serial->dev, 0),
21149cdee0eSKees Lemmens 				OTI6858_REQ_T_GET_STATUS,
21249cdee0eSKees Lemmens 				OTI6858_REQ_GET_STATUS,
21349cdee0eSKees Lemmens 				0, 0,
21449cdee0eSKees Lemmens 				new_setup, OTI6858_CTRL_PKT_SIZE,
21549cdee0eSKees Lemmens 				100);
21649cdee0eSKees Lemmens 
21749cdee0eSKees Lemmens 	if (result != OTI6858_CTRL_PKT_SIZE) {
218441b62c1SHarvey Harrison 		dev_err(&port->dev, "%s(): error reading status\n", __func__);
21949cdee0eSKees Lemmens 		kfree(new_setup);
22049cdee0eSKees Lemmens 		/* we will try again */
2212a77c814SAlan Cox 		schedule_delayed_work(&priv->delayed_setup_work,
2222a77c814SAlan Cox 							msecs_to_jiffies(2));
22349cdee0eSKees Lemmens 		return;
22449cdee0eSKees Lemmens 	}
22549cdee0eSKees Lemmens 
22649cdee0eSKees Lemmens 	spin_lock_irqsave(&priv->lock, flags);
22749cdee0eSKees Lemmens 	if (!OTI6858_CTRL_EQUALS_PENDING(new_setup, priv)) {
22849cdee0eSKees Lemmens 		new_setup->divisor = priv->pending_setup.divisor;
22949cdee0eSKees Lemmens 		new_setup->control = priv->pending_setup.control;
23049cdee0eSKees Lemmens 		new_setup->frame_fmt = priv->pending_setup.frame_fmt;
23149cdee0eSKees Lemmens 
23249cdee0eSKees Lemmens 		spin_unlock_irqrestore(&priv->lock, flags);
23349cdee0eSKees Lemmens 		result = usb_control_msg(port->serial->dev,
23449cdee0eSKees Lemmens 					usb_sndctrlpipe(port->serial->dev, 0),
23549cdee0eSKees Lemmens 					OTI6858_REQ_T_SET_LINE,
23649cdee0eSKees Lemmens 					OTI6858_REQ_SET_LINE,
23749cdee0eSKees Lemmens 					0, 0,
23849cdee0eSKees Lemmens 					new_setup, OTI6858_CTRL_PKT_SIZE,
23949cdee0eSKees Lemmens 					100);
24049cdee0eSKees Lemmens 	} else {
24149cdee0eSKees Lemmens 		spin_unlock_irqrestore(&priv->lock, flags);
24249cdee0eSKees Lemmens 		result = 0;
24349cdee0eSKees Lemmens 	}
24449cdee0eSKees Lemmens 	kfree(new_setup);
24549cdee0eSKees Lemmens 
24649cdee0eSKees Lemmens 	spin_lock_irqsave(&priv->lock, flags);
24749cdee0eSKees Lemmens 	if (result != OTI6858_CTRL_PKT_SIZE)
24849cdee0eSKees Lemmens 		priv->transient = 0;
24949cdee0eSKees Lemmens 	priv->setup_done = 1;
25049cdee0eSKees Lemmens 	spin_unlock_irqrestore(&priv->lock, flags);
25149cdee0eSKees Lemmens 
2528c8e87bcSGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s(): submitting interrupt urb\n", __func__);
25340d28582SOliver Neukum 	result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
25449cdee0eSKees Lemmens 	if (result != 0) {
2558c8e87bcSGreg Kroah-Hartman 		dev_err(&port->dev, "%s(): usb_submit_urb() failed with error %d\n",
2568c8e87bcSGreg Kroah-Hartman 			__func__, result);
25749cdee0eSKees Lemmens 	}
25849cdee0eSKees Lemmens }
25949cdee0eSKees Lemmens 
send_data(struct work_struct * work)2607d7917bcSBill Pemberton static void send_data(struct work_struct *work)
26149cdee0eSKees Lemmens {
2622a77c814SAlan Cox 	struct oti6858_private *priv = container_of(work,
2632a77c814SAlan Cox 			struct oti6858_private, delayed_write_work.work);
26449cdee0eSKees Lemmens 	struct usb_serial_port *port = priv->port;
26549cdee0eSKees Lemmens 	int count = 0, result;
26649cdee0eSKees Lemmens 	unsigned long flags;
267d2126326SJohan Hovold 	u8 *allow;
26849cdee0eSKees Lemmens 
26949cdee0eSKees Lemmens 	spin_lock_irqsave(&priv->lock, flags);
27049cdee0eSKees Lemmens 	if (priv->flags.write_urb_in_use) {
27149cdee0eSKees Lemmens 		spin_unlock_irqrestore(&priv->lock, flags);
2722a77c814SAlan Cox 		schedule_delayed_work(&priv->delayed_write_work,
2732a77c814SAlan Cox 						msecs_to_jiffies(2));
27449cdee0eSKees Lemmens 		return;
27549cdee0eSKees Lemmens 	}
27649cdee0eSKees Lemmens 	priv->flags.write_urb_in_use = 1;
27749cdee0eSKees Lemmens 	spin_unlock_irqrestore(&priv->lock, flags);
27828f27dcbSJohan Hovold 
27928f27dcbSJohan Hovold 	spin_lock_irqsave(&port->lock, flags);
28028f27dcbSJohan Hovold 	count = kfifo_len(&port->write_fifo);
28128f27dcbSJohan Hovold 	spin_unlock_irqrestore(&port->lock, flags);
28228f27dcbSJohan Hovold 
28349cdee0eSKees Lemmens 	if (count > port->bulk_out_size)
28449cdee0eSKees Lemmens 		count = port->bulk_out_size;
28549cdee0eSKees Lemmens 
28649cdee0eSKees Lemmens 	if (count != 0) {
287d2126326SJohan Hovold 		allow = kmalloc(1, GFP_KERNEL);
28810c642d0SJohan Hovold 		if (!allow)
289d2126326SJohan Hovold 			return;
29010c642d0SJohan Hovold 
29149cdee0eSKees Lemmens 		result = usb_control_msg(port->serial->dev,
29249cdee0eSKees Lemmens 				usb_rcvctrlpipe(port->serial->dev, 0),
29349cdee0eSKees Lemmens 				OTI6858_REQ_T_CHECK_TXBUFF,
29449cdee0eSKees Lemmens 				OTI6858_REQ_CHECK_TXBUFF,
295d2126326SJohan Hovold 				count, 0, allow, 1, 100);
296d2126326SJohan Hovold 		if (result != 1 || *allow != 0)
29749cdee0eSKees Lemmens 			count = 0;
298d2126326SJohan Hovold 		kfree(allow);
29949cdee0eSKees Lemmens 	}
30049cdee0eSKees Lemmens 
30149cdee0eSKees Lemmens 	if (count == 0) {
30249cdee0eSKees Lemmens 		priv->flags.write_urb_in_use = 0;
30349cdee0eSKees Lemmens 
3048c8e87bcSGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s(): submitting interrupt urb\n", __func__);
30540d28582SOliver Neukum 		result = usb_submit_urb(port->interrupt_in_urb, GFP_NOIO);
30649cdee0eSKees Lemmens 		if (result != 0) {
3078c8e87bcSGreg Kroah-Hartman 			dev_err(&port->dev, "%s(): usb_submit_urb() failed with error %d\n",
3088c8e87bcSGreg Kroah-Hartman 				__func__, result);
30949cdee0eSKees Lemmens 		}
31049cdee0eSKees Lemmens 		return;
31149cdee0eSKees Lemmens 	}
31249cdee0eSKees Lemmens 
31328f27dcbSJohan Hovold 	count = kfifo_out_locked(&port->write_fifo,
314e3c1803fSJohan Hovold 					port->write_urb->transfer_buffer,
31528f27dcbSJohan Hovold 					count, &port->lock);
31649cdee0eSKees Lemmens 	port->write_urb->transfer_buffer_length = count;
31740d28582SOliver Neukum 	result = usb_submit_urb(port->write_urb, GFP_NOIO);
31849cdee0eSKees Lemmens 	if (result != 0) {
3198c8e87bcSGreg Kroah-Hartman 		dev_err_console(port, "%s(): usb_submit_urb() failed with error %d\n",
3208c8e87bcSGreg Kroah-Hartman 				__func__, result);
32149cdee0eSKees Lemmens 		priv->flags.write_urb_in_use = 0;
32249cdee0eSKees Lemmens 	}
32349cdee0eSKees Lemmens 
32449cdee0eSKees Lemmens 	usb_serial_port_softint(port);
32549cdee0eSKees Lemmens }
32649cdee0eSKees Lemmens 
oti6858_port_probe(struct usb_serial_port * port)327289b076fSJohan Hovold static int oti6858_port_probe(struct usb_serial_port *port)
32849cdee0eSKees Lemmens {
32949cdee0eSKees Lemmens 	struct oti6858_private *priv;
33049cdee0eSKees Lemmens 
331289b076fSJohan Hovold 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
33249cdee0eSKees Lemmens 	if (!priv)
333289b076fSJohan Hovold 		return -ENOMEM;
33449cdee0eSKees Lemmens 
33549cdee0eSKees Lemmens 	spin_lock_init(&priv->lock);
33649cdee0eSKees Lemmens 	priv->port = port;
33749cdee0eSKees Lemmens 	INIT_DELAYED_WORK(&priv->delayed_setup_work, setup_line);
33849cdee0eSKees Lemmens 	INIT_DELAYED_WORK(&priv->delayed_write_work, send_data);
33949cdee0eSKees Lemmens 
340289b076fSJohan Hovold 	usb_set_serial_port_data(port, priv);
34149cdee0eSKees Lemmens 
342d7be6221SJohan Hovold 	port->port.drain_delay = 256;	/* FIXME: check the FIFO length */
343d7be6221SJohan Hovold 
344289b076fSJohan Hovold 	return 0;
34549cdee0eSKees Lemmens }
346289b076fSJohan Hovold 
oti6858_port_remove(struct usb_serial_port * port)347c5d1448fSUwe Kleine-König static void oti6858_port_remove(struct usb_serial_port *port)
348289b076fSJohan Hovold {
349289b076fSJohan Hovold 	struct oti6858_private *priv;
350289b076fSJohan Hovold 
351289b076fSJohan Hovold 	priv = usb_get_serial_port_data(port);
352289b076fSJohan Hovold 	kfree(priv);
35349cdee0eSKees Lemmens }
35449cdee0eSKees Lemmens 
oti6858_write(struct tty_struct * tty,struct usb_serial_port * port,const unsigned char * buf,int count)35595da310eSAlan Cox static int oti6858_write(struct tty_struct *tty, struct usb_serial_port *port,
35649cdee0eSKees Lemmens 			const unsigned char *buf, int count)
35749cdee0eSKees Lemmens {
35849cdee0eSKees Lemmens 	if (!count)
35949cdee0eSKees Lemmens 		return count;
36049cdee0eSKees Lemmens 
36128f27dcbSJohan Hovold 	count = kfifo_in_locked(&port->write_fifo, buf, count, &port->lock);
36249cdee0eSKees Lemmens 
36349cdee0eSKees Lemmens 	return count;
36449cdee0eSKees Lemmens }
36549cdee0eSKees Lemmens 
oti6858_write_room(struct tty_struct * tty)36694cc7aeaSJiri Slaby static unsigned int oti6858_write_room(struct tty_struct *tty)
36749cdee0eSKees Lemmens {
36895da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
36994cc7aeaSJiri Slaby 	unsigned int room;
37049cdee0eSKees Lemmens 	unsigned long flags;
37149cdee0eSKees Lemmens 
37228f27dcbSJohan Hovold 	spin_lock_irqsave(&port->lock, flags);
37328f27dcbSJohan Hovold 	room = kfifo_avail(&port->write_fifo);
37428f27dcbSJohan Hovold 	spin_unlock_irqrestore(&port->lock, flags);
37549cdee0eSKees Lemmens 
37649cdee0eSKees Lemmens 	return room;
37749cdee0eSKees Lemmens }
37849cdee0eSKees Lemmens 
oti6858_chars_in_buffer(struct tty_struct * tty)379155591d3SJiri Slaby static unsigned int oti6858_chars_in_buffer(struct tty_struct *tty)
38049cdee0eSKees Lemmens {
38195da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
382155591d3SJiri Slaby 	unsigned int chars;
38349cdee0eSKees Lemmens 	unsigned long flags;
38449cdee0eSKees Lemmens 
38528f27dcbSJohan Hovold 	spin_lock_irqsave(&port->lock, flags);
38628f27dcbSJohan Hovold 	chars = kfifo_len(&port->write_fifo);
38728f27dcbSJohan Hovold 	spin_unlock_irqrestore(&port->lock, flags);
38849cdee0eSKees Lemmens 
38949cdee0eSKees Lemmens 	return chars;
39049cdee0eSKees Lemmens }
39149cdee0eSKees Lemmens 
oti6858_init_termios(struct tty_struct * tty)392fe1ae7fdSAlan Cox static void oti6858_init_termios(struct tty_struct *tty)
393fe1ae7fdSAlan Cox {
394d8a7f23cSJohan Hovold 	tty_encode_baud_rate(tty, 38400, 38400);
395fe1ae7fdSAlan Cox }
396fe1ae7fdSAlan Cox 
oti6858_set_termios(struct tty_struct * tty,struct usb_serial_port * port,const struct ktermios * old_termios)39795da310eSAlan Cox static void oti6858_set_termios(struct tty_struct *tty,
398*f6d47fe5SIlpo Järvinen 				struct usb_serial_port *port,
399*f6d47fe5SIlpo Järvinen 				const struct ktermios *old_termios)
40049cdee0eSKees Lemmens {
40149cdee0eSKees Lemmens 	struct oti6858_private *priv = usb_get_serial_port_data(port);
40249cdee0eSKees Lemmens 	unsigned long flags;
40349cdee0eSKees Lemmens 	unsigned int cflag;
40449cdee0eSKees Lemmens 	u8 frame_fmt, control;
405fd05e720SAl Viro 	__le16 divisor;
40649cdee0eSKees Lemmens 	int br;
40749cdee0eSKees Lemmens 
408adc8d746SAlan Cox 	cflag = tty->termios.c_cflag;
40949cdee0eSKees Lemmens 
41049cdee0eSKees Lemmens 	spin_lock_irqsave(&priv->lock, flags);
41149cdee0eSKees Lemmens 	frame_fmt = priv->pending_setup.frame_fmt;
41249cdee0eSKees Lemmens 	control = priv->pending_setup.control;
41349cdee0eSKees Lemmens 	spin_unlock_irqrestore(&priv->lock, flags);
41449cdee0eSKees Lemmens 
41549cdee0eSKees Lemmens 	frame_fmt &= ~FMT_DATA_BITS_MASK;
41649cdee0eSKees Lemmens 	switch (cflag & CSIZE) {
41749cdee0eSKees Lemmens 	case CS5:
41849cdee0eSKees Lemmens 		frame_fmt |= FMT_DATA_BITS_5;
41949cdee0eSKees Lemmens 		break;
42049cdee0eSKees Lemmens 	case CS6:
42149cdee0eSKees Lemmens 		frame_fmt |= FMT_DATA_BITS_6;
42249cdee0eSKees Lemmens 		break;
42349cdee0eSKees Lemmens 	case CS7:
42449cdee0eSKees Lemmens 		frame_fmt |= FMT_DATA_BITS_7;
42549cdee0eSKees Lemmens 		break;
42649cdee0eSKees Lemmens 	default:
42749cdee0eSKees Lemmens 	case CS8:
42849cdee0eSKees Lemmens 		frame_fmt |= FMT_DATA_BITS_8;
42949cdee0eSKees Lemmens 		break;
43049cdee0eSKees Lemmens 	}
43149cdee0eSKees Lemmens 
43249cdee0eSKees Lemmens 	/* manufacturer claims that this device can work with baud rates
43349cdee0eSKees Lemmens 	 * up to 3 Mbps; I've tested it only on 115200 bps, so I can't
43449cdee0eSKees Lemmens 	 * guarantee that any other baud rate will work (especially
43549cdee0eSKees Lemmens 	 * the higher ones)
43649cdee0eSKees Lemmens 	 */
43795da310eSAlan Cox 	br = tty_get_baud_rate(tty);
43849cdee0eSKees Lemmens 	if (br == 0) {
43949cdee0eSKees Lemmens 		divisor = 0;
440b0a239daSAlan Cox 	} else {
44149cdee0eSKees Lemmens 		int real_br;
442fd05e720SAl Viro 		int new_divisor;
443b0a239daSAlan Cox 		br = min(br, OTI6858_MAX_BAUD_RATE);
44449cdee0eSKees Lemmens 
445fd05e720SAl Viro 		new_divisor = (96000000 + 8 * br) / (16 * br);
446fd05e720SAl Viro 		real_br = 96000000 / (16 * new_divisor);
447fd05e720SAl Viro 		divisor = cpu_to_le16(new_divisor);
44895da310eSAlan Cox 		tty_encode_baud_rate(tty, real_br, real_br);
44949cdee0eSKees Lemmens 	}
45049cdee0eSKees Lemmens 
45149cdee0eSKees Lemmens 	frame_fmt &= ~FMT_STOP_BITS_MASK;
4522a77c814SAlan Cox 	if ((cflag & CSTOPB) != 0)
45349cdee0eSKees Lemmens 		frame_fmt |= FMT_STOP_BITS_2;
4542a77c814SAlan Cox 	else
45549cdee0eSKees Lemmens 		frame_fmt |= FMT_STOP_BITS_1;
45649cdee0eSKees Lemmens 
45749cdee0eSKees Lemmens 	frame_fmt &= ~FMT_PARITY_MASK;
45849cdee0eSKees Lemmens 	if ((cflag & PARENB) != 0) {
4592a77c814SAlan Cox 		if ((cflag & PARODD) != 0)
46049cdee0eSKees Lemmens 			frame_fmt |= FMT_PARITY_ODD;
4612a77c814SAlan Cox 		else
46249cdee0eSKees Lemmens 			frame_fmt |= FMT_PARITY_EVEN;
46349cdee0eSKees Lemmens 	} else {
46449cdee0eSKees Lemmens 		frame_fmt |= FMT_PARITY_NONE;
46549cdee0eSKees Lemmens 	}
46649cdee0eSKees Lemmens 
46749cdee0eSKees Lemmens 	control &= ~CONTROL_MASK;
46849cdee0eSKees Lemmens 	if ((cflag & CRTSCTS) != 0)
46949cdee0eSKees Lemmens 		control |= (CONTROL_DTR_HIGH | CONTROL_RTS_HIGH);
47049cdee0eSKees Lemmens 
47149cdee0eSKees Lemmens 	/* change control lines if we are switching to or from B0 */
47249cdee0eSKees Lemmens 	/* FIXME:
47349cdee0eSKees Lemmens 	spin_lock_irqsave(&priv->lock, flags);
47449cdee0eSKees Lemmens 	control = priv->line_control;
47549cdee0eSKees Lemmens 	if ((cflag & CBAUD) == B0)
47649cdee0eSKees Lemmens 		priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
47749cdee0eSKees Lemmens 	else
47849cdee0eSKees Lemmens 		priv->line_control |= (CONTROL_DTR | CONTROL_RTS);
47949cdee0eSKees Lemmens 	if (control != priv->line_control) {
48049cdee0eSKees Lemmens 		control = priv->line_control;
48149cdee0eSKees Lemmens 		spin_unlock_irqrestore(&priv->lock, flags);
48249cdee0eSKees Lemmens 		set_control_lines(serial->dev, control);
48349cdee0eSKees Lemmens 	} else {
48449cdee0eSKees Lemmens 		spin_unlock_irqrestore(&priv->lock, flags);
48549cdee0eSKees Lemmens 	}
48649cdee0eSKees Lemmens 	*/
48749cdee0eSKees Lemmens 
48849cdee0eSKees Lemmens 	spin_lock_irqsave(&priv->lock, flags);
48949cdee0eSKees Lemmens 	if (divisor != priv->pending_setup.divisor
49049cdee0eSKees Lemmens 			|| control != priv->pending_setup.control
49149cdee0eSKees Lemmens 			|| frame_fmt != priv->pending_setup.frame_fmt) {
49249cdee0eSKees Lemmens 		priv->pending_setup.divisor = divisor;
49349cdee0eSKees Lemmens 		priv->pending_setup.control = control;
49449cdee0eSKees Lemmens 		priv->pending_setup.frame_fmt = frame_fmt;
49549cdee0eSKees Lemmens 	}
49649cdee0eSKees Lemmens 	spin_unlock_irqrestore(&priv->lock, flags);
49749cdee0eSKees Lemmens }
49849cdee0eSKees Lemmens 
oti6858_open(struct tty_struct * tty,struct usb_serial_port * port)499a509a7e4SAlan Cox static int oti6858_open(struct tty_struct *tty, struct usb_serial_port *port)
50049cdee0eSKees Lemmens {
50149cdee0eSKees Lemmens 	struct oti6858_private *priv = usb_get_serial_port_data(port);
50249cdee0eSKees Lemmens 	struct usb_serial *serial = port->serial;
50349cdee0eSKees Lemmens 	struct oti6858_control_pkt *buf;
50449cdee0eSKees Lemmens 	unsigned long flags;
50549cdee0eSKees Lemmens 	int result;
50649cdee0eSKees Lemmens 
50749cdee0eSKees Lemmens 	usb_clear_halt(serial->dev, port->write_urb->pipe);
50849cdee0eSKees Lemmens 	usb_clear_halt(serial->dev, port->read_urb->pipe);
50949cdee0eSKees Lemmens 
5102a77c814SAlan Cox 	buf = kmalloc(OTI6858_CTRL_PKT_SIZE, GFP_KERNEL);
51110c642d0SJohan Hovold 	if (!buf)
51249cdee0eSKees Lemmens 		return -ENOMEM;
51349cdee0eSKees Lemmens 
51449cdee0eSKees Lemmens 	result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
51549cdee0eSKees Lemmens 				OTI6858_REQ_T_GET_STATUS,
51649cdee0eSKees Lemmens 				OTI6858_REQ_GET_STATUS,
51749cdee0eSKees Lemmens 				0, 0,
51849cdee0eSKees Lemmens 				buf, OTI6858_CTRL_PKT_SIZE,
51949cdee0eSKees Lemmens 				100);
52049cdee0eSKees Lemmens 	if (result != OTI6858_CTRL_PKT_SIZE) {
52149cdee0eSKees Lemmens 		/* assume default (after power-on reset) values */
52249cdee0eSKees Lemmens 		buf->divisor = cpu_to_le16(0x009c);	/* 38400 bps */
52349cdee0eSKees Lemmens 		buf->frame_fmt = 0x03;	/* 8N1 */
52449cdee0eSKees Lemmens 		buf->something = 0x43;
52549cdee0eSKees Lemmens 		buf->control = 0x4c;	/* DTR, RTS */
52649cdee0eSKees Lemmens 		buf->tx_status = 0x00;
52749cdee0eSKees Lemmens 		buf->pin_state = 0x5b;	/* RTS, CTS, DSR, DTR, RI, DCD */
52849cdee0eSKees Lemmens 		buf->rx_bytes_avail = 0x00;
52949cdee0eSKees Lemmens 	}
53049cdee0eSKees Lemmens 
53149cdee0eSKees Lemmens 	spin_lock_irqsave(&priv->lock, flags);
53249cdee0eSKees Lemmens 	memcpy(&priv->status, buf, OTI6858_CTRL_PKT_SIZE);
53349cdee0eSKees Lemmens 	priv->pending_setup.divisor = buf->divisor;
53449cdee0eSKees Lemmens 	priv->pending_setup.frame_fmt = buf->frame_fmt;
53549cdee0eSKees Lemmens 	priv->pending_setup.control = buf->control;
53649cdee0eSKees Lemmens 	spin_unlock_irqrestore(&priv->lock, flags);
53749cdee0eSKees Lemmens 	kfree(buf);
53849cdee0eSKees Lemmens 
5398c8e87bcSGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s(): submitting interrupt urb\n", __func__);
54049cdee0eSKees Lemmens 	result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
54149cdee0eSKees Lemmens 	if (result != 0) {
5428c8e87bcSGreg Kroah-Hartman 		dev_err(&port->dev, "%s(): usb_submit_urb() failed with error %d\n",
5438c8e87bcSGreg Kroah-Hartman 			__func__, result);
544335f8514SAlan Cox 		oti6858_close(port);
545d5e450eeSJohan Hovold 		return result;
54649cdee0eSKees Lemmens 	}
54749cdee0eSKees Lemmens 
54849cdee0eSKees Lemmens 	/* setup termios */
54995da310eSAlan Cox 	if (tty)
550dc43ff92SJohan Hovold 		oti6858_set_termios(tty, port, NULL);
551d7be6221SJohan Hovold 
55249cdee0eSKees Lemmens 	return 0;
55349cdee0eSKees Lemmens }
55449cdee0eSKees Lemmens 
oti6858_close(struct usb_serial_port * port)555335f8514SAlan Cox static void oti6858_close(struct usb_serial_port *port)
55649cdee0eSKees Lemmens {
55749cdee0eSKees Lemmens 	struct oti6858_private *priv = usb_get_serial_port_data(port);
55849cdee0eSKees Lemmens 	unsigned long flags;
55949cdee0eSKees Lemmens 
56028f27dcbSJohan Hovold 	spin_lock_irqsave(&port->lock, flags);
56149cdee0eSKees Lemmens 	/* clear out any remaining data in the buffer */
56228f27dcbSJohan Hovold 	kfifo_reset_out(&port->write_fifo);
56328f27dcbSJohan Hovold 	spin_unlock_irqrestore(&port->lock, flags);
56449cdee0eSKees Lemmens 
5658c8e87bcSGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s(): after buf_clear()\n", __func__);
56649cdee0eSKees Lemmens 
56749cdee0eSKees Lemmens 	/* cancel scheduled setup */
568569ff2deSTejun Heo 	cancel_delayed_work_sync(&priv->delayed_setup_work);
569569ff2deSTejun Heo 	cancel_delayed_work_sync(&priv->delayed_write_work);
57049cdee0eSKees Lemmens 
57149cdee0eSKees Lemmens 	/* shutdown our urbs */
5728c8e87bcSGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s(): shutting down urbs\n", __func__);
57349cdee0eSKees Lemmens 	usb_kill_urb(port->write_urb);
57449cdee0eSKees Lemmens 	usb_kill_urb(port->read_urb);
57549cdee0eSKees Lemmens 	usb_kill_urb(port->interrupt_in_urb);
57649cdee0eSKees Lemmens }
57749cdee0eSKees Lemmens 
oti6858_tiocmset(struct tty_struct * tty,unsigned int set,unsigned int clear)57820b9d177SAlan Cox static int oti6858_tiocmset(struct tty_struct *tty,
57949cdee0eSKees Lemmens 				unsigned int set, unsigned int clear)
58049cdee0eSKees Lemmens {
58195da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
58249cdee0eSKees Lemmens 	struct oti6858_private *priv = usb_get_serial_port_data(port);
58349cdee0eSKees Lemmens 	unsigned long flags;
58449cdee0eSKees Lemmens 	u8 control;
58549cdee0eSKees Lemmens 
5868c8e87bcSGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s(set = 0x%08x, clear = 0x%08x)\n",
5878c8e87bcSGreg Kroah-Hartman 		__func__, set, clear);
58849cdee0eSKees Lemmens 
58949cdee0eSKees Lemmens 	/* FIXME: check if this is correct (active high/low) */
59049cdee0eSKees Lemmens 	spin_lock_irqsave(&priv->lock, flags);
59149cdee0eSKees Lemmens 	control = priv->pending_setup.control;
59249cdee0eSKees Lemmens 	if ((set & TIOCM_RTS) != 0)
59349cdee0eSKees Lemmens 		control |= CONTROL_RTS_HIGH;
59449cdee0eSKees Lemmens 	if ((set & TIOCM_DTR) != 0)
59549cdee0eSKees Lemmens 		control |= CONTROL_DTR_HIGH;
59649cdee0eSKees Lemmens 	if ((clear & TIOCM_RTS) != 0)
59749cdee0eSKees Lemmens 		control &= ~CONTROL_RTS_HIGH;
59849cdee0eSKees Lemmens 	if ((clear & TIOCM_DTR) != 0)
59949cdee0eSKees Lemmens 		control &= ~CONTROL_DTR_HIGH;
60049cdee0eSKees Lemmens 
6012a77c814SAlan Cox 	if (control != priv->pending_setup.control)
60249cdee0eSKees Lemmens 		priv->pending_setup.control = control;
60349cdee0eSKees Lemmens 
6042a77c814SAlan Cox 	spin_unlock_irqrestore(&priv->lock, flags);
60549cdee0eSKees Lemmens 	return 0;
60649cdee0eSKees Lemmens }
60749cdee0eSKees Lemmens 
oti6858_tiocmget(struct tty_struct * tty)60860b33c13SAlan Cox static int oti6858_tiocmget(struct tty_struct *tty)
60949cdee0eSKees Lemmens {
61095da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
61149cdee0eSKees Lemmens 	struct oti6858_private *priv = usb_get_serial_port_data(port);
61249cdee0eSKees Lemmens 	unsigned long flags;
61349cdee0eSKees Lemmens 	unsigned pin_state;
61449cdee0eSKees Lemmens 	unsigned result = 0;
61549cdee0eSKees Lemmens 
61649cdee0eSKees Lemmens 	spin_lock_irqsave(&priv->lock, flags);
61749cdee0eSKees Lemmens 	pin_state = priv->status.pin_state & PIN_MASK;
61849cdee0eSKees Lemmens 	spin_unlock_irqrestore(&priv->lock, flags);
61949cdee0eSKees Lemmens 
62049cdee0eSKees Lemmens 	/* FIXME: check if this is correct (active high/low) */
62149cdee0eSKees Lemmens 	if ((pin_state & PIN_RTS) != 0)
62249cdee0eSKees Lemmens 		result |= TIOCM_RTS;
62349cdee0eSKees Lemmens 	if ((pin_state & PIN_CTS) != 0)
62449cdee0eSKees Lemmens 		result |= TIOCM_CTS;
62549cdee0eSKees Lemmens 	if ((pin_state & PIN_DSR) != 0)
62649cdee0eSKees Lemmens 		result |= TIOCM_DSR;
62749cdee0eSKees Lemmens 	if ((pin_state & PIN_DTR) != 0)
62849cdee0eSKees Lemmens 		result |= TIOCM_DTR;
62949cdee0eSKees Lemmens 	if ((pin_state & PIN_RI) != 0)
63049cdee0eSKees Lemmens 		result |= TIOCM_RI;
63149cdee0eSKees Lemmens 	if ((pin_state & PIN_DCD) != 0)
63249cdee0eSKees Lemmens 		result |= TIOCM_CD;
63349cdee0eSKees Lemmens 
6348c8e87bcSGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s() = 0x%08x\n", __func__, result);
63549cdee0eSKees Lemmens 
63649cdee0eSKees Lemmens 	return result;
63749cdee0eSKees Lemmens }
63849cdee0eSKees Lemmens 
oti6858_read_int_callback(struct urb * urb)63949cdee0eSKees Lemmens static void oti6858_read_int_callback(struct urb *urb)
64049cdee0eSKees Lemmens {
641cdc97792SMing Lei 	struct usb_serial_port *port =  urb->context;
64249cdee0eSKees Lemmens 	struct oti6858_private *priv = usb_get_serial_port_data(port);
64349cdee0eSKees Lemmens 	int transient = 0, can_recv = 0, resubmit = 1;
64478c26aebSGreg Kroah-Hartman 	int status = urb->status;
64549cdee0eSKees Lemmens 
64678c26aebSGreg Kroah-Hartman 	switch (status) {
64749cdee0eSKees Lemmens 	case 0:
64849cdee0eSKees Lemmens 		/* success */
64949cdee0eSKees Lemmens 		break;
65049cdee0eSKees Lemmens 	case -ECONNRESET:
65149cdee0eSKees Lemmens 	case -ENOENT:
65249cdee0eSKees Lemmens 	case -ESHUTDOWN:
65349cdee0eSKees Lemmens 		/* this urb is terminated, clean up */
6548c8e87bcSGreg Kroah-Hartman 		dev_dbg(&urb->dev->dev, "%s(): urb shutting down with status: %d\n",
655441b62c1SHarvey Harrison 			__func__, status);
65649cdee0eSKees Lemmens 		return;
65749cdee0eSKees Lemmens 	default:
6588c8e87bcSGreg Kroah-Hartman 		dev_dbg(&urb->dev->dev, "%s(): nonzero urb status received: %d\n",
659441b62c1SHarvey Harrison 			__func__, status);
66049cdee0eSKees Lemmens 		break;
66149cdee0eSKees Lemmens 	}
66249cdee0eSKees Lemmens 
66378c26aebSGreg Kroah-Hartman 	if (status == 0 && urb->actual_length == OTI6858_CTRL_PKT_SIZE) {
66449cdee0eSKees Lemmens 		struct oti6858_control_pkt *xs = urb->transfer_buffer;
66549cdee0eSKees Lemmens 		unsigned long flags;
66649cdee0eSKees Lemmens 
66749cdee0eSKees Lemmens 		spin_lock_irqsave(&priv->lock, flags);
66849cdee0eSKees Lemmens 
66949cdee0eSKees Lemmens 		if (!priv->transient) {
67049cdee0eSKees Lemmens 			if (!OTI6858_CTRL_EQUALS_PENDING(xs, priv)) {
67149cdee0eSKees Lemmens 				if (xs->rx_bytes_avail == 0) {
67249cdee0eSKees Lemmens 					priv->transient = 4;
67349cdee0eSKees Lemmens 					priv->setup_done = 0;
67449cdee0eSKees Lemmens 					resubmit = 0;
6758c8e87bcSGreg Kroah-Hartman 					dev_dbg(&port->dev, "%s(): scheduling setup_line()\n", __func__);
67649cdee0eSKees Lemmens 					schedule_delayed_work(&priv->delayed_setup_work, 0);
67749cdee0eSKees Lemmens 				}
67849cdee0eSKees Lemmens 			}
67949cdee0eSKees Lemmens 		} else {
68049cdee0eSKees Lemmens 			if (OTI6858_CTRL_EQUALS_PENDING(xs, priv)) {
68149cdee0eSKees Lemmens 				priv->transient = 0;
68249cdee0eSKees Lemmens 			} else if (!priv->setup_done) {
68349cdee0eSKees Lemmens 				resubmit = 0;
68449cdee0eSKees Lemmens 			} else if (--priv->transient == 0) {
68549cdee0eSKees Lemmens 				if (xs->rx_bytes_avail == 0) {
68649cdee0eSKees Lemmens 					priv->transient = 4;
68749cdee0eSKees Lemmens 					priv->setup_done = 0;
68849cdee0eSKees Lemmens 					resubmit = 0;
6898c8e87bcSGreg Kroah-Hartman 					dev_dbg(&port->dev, "%s(): scheduling setup_line()\n", __func__);
69049cdee0eSKees Lemmens 					schedule_delayed_work(&priv->delayed_setup_work, 0);
69149cdee0eSKees Lemmens 				}
69249cdee0eSKees Lemmens 			}
69349cdee0eSKees Lemmens 		}
69449cdee0eSKees Lemmens 
69549cdee0eSKees Lemmens 		if (!priv->transient) {
6961acc36e9SJohan Hovold 			u8 delta = xs->pin_state ^ priv->status.pin_state;
6971acc36e9SJohan Hovold 
698bd6383c8SJohan Hovold 			if (delta & PIN_MSR_MASK) {
699bd6383c8SJohan Hovold 				if (delta & PIN_CTS)
700bd6383c8SJohan Hovold 					port->icount.cts++;
701bd6383c8SJohan Hovold 				if (delta & PIN_DSR)
702bd6383c8SJohan Hovold 					port->icount.dsr++;
703bd6383c8SJohan Hovold 				if (delta & PIN_RI)
704bd6383c8SJohan Hovold 					port->icount.rng++;
705bd6383c8SJohan Hovold 				if (delta & PIN_DCD)
706bd6383c8SJohan Hovold 					port->icount.dcd++;
707bd6383c8SJohan Hovold 
708215f6f04SJohan Hovold 				wake_up_interruptible(&port->port.delta_msr_wait);
709bd6383c8SJohan Hovold 			}
7101acc36e9SJohan Hovold 
71149cdee0eSKees Lemmens 			memcpy(&priv->status, xs, OTI6858_CTRL_PKT_SIZE);
71249cdee0eSKees Lemmens 		}
71349cdee0eSKees Lemmens 
71449cdee0eSKees Lemmens 		if (!priv->transient && xs->rx_bytes_avail != 0) {
71549cdee0eSKees Lemmens 			can_recv = xs->rx_bytes_avail;
71649cdee0eSKees Lemmens 			priv->flags.read_urb_in_use = 1;
71749cdee0eSKees Lemmens 		}
71849cdee0eSKees Lemmens 
71949cdee0eSKees Lemmens 		transient = priv->transient;
72049cdee0eSKees Lemmens 		spin_unlock_irqrestore(&priv->lock, flags);
72149cdee0eSKees Lemmens 	}
72249cdee0eSKees Lemmens 
72349cdee0eSKees Lemmens 	if (can_recv) {
72449cdee0eSKees Lemmens 		int result;
72549cdee0eSKees Lemmens 
72649cdee0eSKees Lemmens 		result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
72749cdee0eSKees Lemmens 		if (result != 0) {
72849cdee0eSKees Lemmens 			priv->flags.read_urb_in_use = 0;
72949cdee0eSKees Lemmens 			dev_err(&port->dev, "%s(): usb_submit_urb() failed,"
730441b62c1SHarvey Harrison 					" error %d\n", __func__, result);
73149cdee0eSKees Lemmens 		} else {
73249cdee0eSKees Lemmens 			resubmit = 0;
73349cdee0eSKees Lemmens 		}
73449cdee0eSKees Lemmens 	} else if (!transient) {
73549cdee0eSKees Lemmens 		unsigned long flags;
73628f27dcbSJohan Hovold 		int count;
73728f27dcbSJohan Hovold 
73828f27dcbSJohan Hovold 		spin_lock_irqsave(&port->lock, flags);
73928f27dcbSJohan Hovold 		count = kfifo_len(&port->write_fifo);
74028f27dcbSJohan Hovold 		spin_unlock_irqrestore(&port->lock, flags);
74149cdee0eSKees Lemmens 
74249cdee0eSKees Lemmens 		spin_lock_irqsave(&priv->lock, flags);
74328f27dcbSJohan Hovold 		if (priv->flags.write_urb_in_use == 0 && count != 0) {
74449cdee0eSKees Lemmens 			schedule_delayed_work(&priv->delayed_write_work, 0);
74549cdee0eSKees Lemmens 			resubmit = 0;
74649cdee0eSKees Lemmens 		}
74749cdee0eSKees Lemmens 		spin_unlock_irqrestore(&priv->lock, flags);
74849cdee0eSKees Lemmens 	}
74949cdee0eSKees Lemmens 
75049cdee0eSKees Lemmens 	if (resubmit) {
75149cdee0eSKees Lemmens 		int result;
75249cdee0eSKees Lemmens 
7538c8e87bcSGreg Kroah-Hartman /*		dev_dbg(&urb->dev->dev, "%s(): submitting interrupt urb\n", __func__); */
75449cdee0eSKees Lemmens 		result = usb_submit_urb(urb, GFP_ATOMIC);
75549cdee0eSKees Lemmens 		if (result != 0) {
75649cdee0eSKees Lemmens 			dev_err(&urb->dev->dev,
75749cdee0eSKees Lemmens 					"%s(): usb_submit_urb() failed with"
758441b62c1SHarvey Harrison 					" error %d\n", __func__, result);
75949cdee0eSKees Lemmens 		}
76049cdee0eSKees Lemmens 	}
76149cdee0eSKees Lemmens }
76249cdee0eSKees Lemmens 
oti6858_read_bulk_callback(struct urb * urb)76349cdee0eSKees Lemmens static void oti6858_read_bulk_callback(struct urb *urb)
76449cdee0eSKees Lemmens {
765cdc97792SMing Lei 	struct usb_serial_port *port =  urb->context;
76649cdee0eSKees Lemmens 	struct oti6858_private *priv = usb_get_serial_port_data(port);
76749cdee0eSKees Lemmens 	unsigned char *data = urb->transfer_buffer;
76849cdee0eSKees Lemmens 	unsigned long flags;
76978c26aebSGreg Kroah-Hartman 	int status = urb->status;
770b0a239daSAlan Cox 	int result;
77149cdee0eSKees Lemmens 
77249cdee0eSKees Lemmens 	spin_lock_irqsave(&priv->lock, flags);
77349cdee0eSKees Lemmens 	priv->flags.read_urb_in_use = 0;
77449cdee0eSKees Lemmens 	spin_unlock_irqrestore(&priv->lock, flags);
77549cdee0eSKees Lemmens 
77678c26aebSGreg Kroah-Hartman 	if (status != 0) {
7778c8e87bcSGreg Kroah-Hartman 		dev_dbg(&urb->dev->dev, "%s(): unable to handle the error, exiting\n", __func__);
77849cdee0eSKees Lemmens 		return;
77949cdee0eSKees Lemmens 	}
78049cdee0eSKees Lemmens 
7812e124b4aSJiri Slaby 	if (urb->actual_length > 0) {
78205c7cd39SJiri Slaby 		tty_insert_flip_string(&port->port, data, urb->actual_length);
7832e124b4aSJiri Slaby 		tty_flip_buffer_push(&port->port);
78449cdee0eSKees Lemmens 	}
78549cdee0eSKees Lemmens 
7861f87158eSAlan Stern 	/* schedule the interrupt urb */
78749cdee0eSKees Lemmens 	result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
7881f87158eSAlan Stern 	if (result != 0 && result != -EPERM) {
78949cdee0eSKees Lemmens 		dev_err(&port->dev, "%s(): usb_submit_urb() failed,"
790441b62c1SHarvey Harrison 				" error %d\n", __func__, result);
79149cdee0eSKees Lemmens 	}
79249cdee0eSKees Lemmens }
79349cdee0eSKees Lemmens 
oti6858_write_bulk_callback(struct urb * urb)79449cdee0eSKees Lemmens static void oti6858_write_bulk_callback(struct urb *urb)
79549cdee0eSKees Lemmens {
796cdc97792SMing Lei 	struct usb_serial_port *port =  urb->context;
79749cdee0eSKees Lemmens 	struct oti6858_private *priv = usb_get_serial_port_data(port);
79878c26aebSGreg Kroah-Hartman 	int status = urb->status;
79949cdee0eSKees Lemmens 	int result;
80049cdee0eSKees Lemmens 
80178c26aebSGreg Kroah-Hartman 	switch (status) {
80249cdee0eSKees Lemmens 	case 0:
80349cdee0eSKees Lemmens 		/* success */
80449cdee0eSKees Lemmens 		break;
80549cdee0eSKees Lemmens 	case -ECONNRESET:
80649cdee0eSKees Lemmens 	case -ENOENT:
80749cdee0eSKees Lemmens 	case -ESHUTDOWN:
80849cdee0eSKees Lemmens 		/* this urb is terminated, clean up */
8098c8e87bcSGreg Kroah-Hartman 		dev_dbg(&urb->dev->dev, "%s(): urb shutting down with status: %d\n", __func__, status);
81049cdee0eSKees Lemmens 		priv->flags.write_urb_in_use = 0;
81149cdee0eSKees Lemmens 		return;
81249cdee0eSKees Lemmens 	default:
81349cdee0eSKees Lemmens 		/* error in the urb, so we have to resubmit it */
8148c8e87bcSGreg Kroah-Hartman 		dev_dbg(&urb->dev->dev, "%s(): nonzero write bulk status received: %d\n", __func__, status);
8158c8e87bcSGreg Kroah-Hartman 		dev_dbg(&urb->dev->dev, "%s(): overflow in write\n", __func__);
81649cdee0eSKees Lemmens 
81749cdee0eSKees Lemmens 		port->write_urb->transfer_buffer_length = 1;
81849cdee0eSKees Lemmens 		result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
81949cdee0eSKees Lemmens 		if (result) {
82022a416c4SJohan Hovold 			dev_err_console(port, "%s(): usb_submit_urb() failed,"
821441b62c1SHarvey Harrison 					" error %d\n", __func__, result);
82249cdee0eSKees Lemmens 		} else {
82349cdee0eSKees Lemmens 			return;
82449cdee0eSKees Lemmens 		}
82549cdee0eSKees Lemmens 	}
82649cdee0eSKees Lemmens 
82749cdee0eSKees Lemmens 	priv->flags.write_urb_in_use = 0;
82849cdee0eSKees Lemmens 
8292a77c814SAlan Cox 	/* schedule the interrupt urb if we are still open */
8308c8e87bcSGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s(): submitting interrupt urb\n", __func__);
83149cdee0eSKees Lemmens 	result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
83249cdee0eSKees Lemmens 	if (result != 0) {
83349cdee0eSKees Lemmens 		dev_err(&port->dev, "%s(): failed submitting int urb,"
834441b62c1SHarvey Harrison 					" error %d\n", __func__, result);
83549cdee0eSKees Lemmens 	}
83649cdee0eSKees Lemmens }
83749cdee0eSKees Lemmens 
83868e24113SGreg Kroah-Hartman module_usb_serial_driver(serial_drivers, id_table);
83949cdee0eSKees Lemmens 
84049cdee0eSKees Lemmens MODULE_DESCRIPTION(OTI6858_DESCRIPTION);
84149cdee0eSKees Lemmens MODULE_AUTHOR(OTI6858_AUTHOR);
842627cfa89SJohan Hovold MODULE_LICENSE("GPL v2");
843