xref: /linux/drivers/usb/serial/oti6858.c (revision d5e450ee4f6d88711879592e30c0fb1cf14bf504)
149cdee0eSKees Lemmens /*
249cdee0eSKees Lemmens  * Ours Technology Inc. OTi-6858 USB to serial adapter driver.
349cdee0eSKees Lemmens  *
449cdee0eSKees Lemmens  * Copyleft  (C) 2007 Kees Lemmens (adapted for kernel 2.6.20)
549cdee0eSKees Lemmens  * Copyright (C) 2006 Tomasz Michal Lukaszewski (FIXME: add e-mail)
649cdee0eSKees Lemmens  * Copyright (C) 2001-2004 Greg Kroah-Hartman (greg@kroah.com)
749cdee0eSKees Lemmens  * Copyright (C) 2003 IBM Corp.
849cdee0eSKees Lemmens  *
949cdee0eSKees Lemmens  * Many thanks to the authors of pl2303 driver: all functions in this file
1049cdee0eSKees Lemmens  * are heavily based on pl2303 code, buffering code is a 1-to-1 copy.
1149cdee0eSKees Lemmens  *
1249cdee0eSKees Lemmens  * Warning! You use this driver on your own risk! The only official
1349cdee0eSKees Lemmens  * description of this device I have is datasheet from manufacturer,
1449cdee0eSKees Lemmens  * and it doesn't contain almost any information needed to write a driver.
1549cdee0eSKees Lemmens  * Almost all knowlegde used while writing this driver was gathered by:
1649cdee0eSKees Lemmens  *  - analyzing traffic between device and the M$ Windows 2000 driver,
1749cdee0eSKees Lemmens  *  - trying different bit combinations and checking pin states
1849cdee0eSKees Lemmens  *    with a voltmeter,
1949cdee0eSKees Lemmens  *  - receiving malformed frames and producing buffer overflows
2049cdee0eSKees Lemmens  *    to learn how errors are reported,
2149cdee0eSKees Lemmens  * So, THIS CODE CAN DESTROY OTi-6858 AND ANY OTHER DEVICES, THAT ARE
2249cdee0eSKees Lemmens  * CONNECTED TO IT!
2349cdee0eSKees Lemmens  *
2449cdee0eSKees Lemmens  * This program is free software; you can redistribute it and/or modify
2549cdee0eSKees Lemmens  * it under the terms of the GNU General Public License as published by
2649cdee0eSKees Lemmens  * the Free Software Foundation; either version 2 of the License.
2749cdee0eSKees Lemmens  *
282a77c814SAlan Cox  * See Documentation/usb/usb-serial.txt for more information on using this
292a77c814SAlan Cox  * driver
3049cdee0eSKees Lemmens  *
3149cdee0eSKees Lemmens  * TODO:
3249cdee0eSKees Lemmens  *  - implement correct flushing for ioctls and oti6858_close()
3349cdee0eSKees Lemmens  *  - check how errors (rx overflow, parity error, framing error) are reported
3449cdee0eSKees Lemmens  *  - implement oti6858_break_ctl()
3549cdee0eSKees Lemmens  *  - implement more ioctls
3649cdee0eSKees Lemmens  *  - test/implement flow control
3749cdee0eSKees Lemmens  *  - allow setting custom baud rates
3849cdee0eSKees Lemmens  */
3949cdee0eSKees Lemmens 
4049cdee0eSKees Lemmens #include <linux/kernel.h>
4149cdee0eSKees Lemmens #include <linux/errno.h>
4249cdee0eSKees Lemmens #include <linux/init.h>
4349cdee0eSKees Lemmens #include <linux/slab.h>
4449cdee0eSKees Lemmens #include <linux/tty.h>
4549cdee0eSKees Lemmens #include <linux/tty_driver.h>
4649cdee0eSKees Lemmens #include <linux/tty_flip.h>
4749cdee0eSKees Lemmens #include <linux/serial.h>
4849cdee0eSKees Lemmens #include <linux/module.h>
4949cdee0eSKees Lemmens #include <linux/moduleparam.h>
5049cdee0eSKees Lemmens #include <linux/spinlock.h>
5149cdee0eSKees Lemmens #include <linux/usb.h>
5249cdee0eSKees Lemmens #include <linux/usb/serial.h>
532a77c814SAlan Cox #include <linux/uaccess.h>
54e3c1803fSJohan Hovold #include <linux/kfifo.h>
5549cdee0eSKees Lemmens #include "oti6858.h"
5649cdee0eSKees Lemmens 
5749cdee0eSKees Lemmens #define OTI6858_DESCRIPTION \
5849cdee0eSKees Lemmens 	"Ours Technology Inc. OTi-6858 USB to serial adapter driver"
5949cdee0eSKees Lemmens #define OTI6858_AUTHOR "Tomasz Michal Lukaszewski <FIXME@FIXME>"
60e3c1803fSJohan Hovold #define OTI6858_VERSION "0.2"
6149cdee0eSKees Lemmens 
627d40d7e8SNémeth Márton static const struct usb_device_id id_table[] = {
6349cdee0eSKees Lemmens 	{ USB_DEVICE(OTI6858_VENDOR_ID, OTI6858_PRODUCT_ID) },
6449cdee0eSKees Lemmens 	{ }
6549cdee0eSKees Lemmens };
6649cdee0eSKees Lemmens 
6749cdee0eSKees Lemmens MODULE_DEVICE_TABLE(usb, id_table);
6849cdee0eSKees Lemmens 
6949cdee0eSKees Lemmens static struct usb_driver oti6858_driver = {
7049cdee0eSKees Lemmens 	.name =		"oti6858",
7149cdee0eSKees Lemmens 	.probe =	usb_serial_probe,
7249cdee0eSKees Lemmens 	.disconnect =	usb_serial_disconnect,
7349cdee0eSKees Lemmens 	.id_table =	id_table,
7449cdee0eSKees Lemmens 	.no_dynamic_id = 	1,
7549cdee0eSKees Lemmens };
7649cdee0eSKees Lemmens 
7749cdee0eSKees Lemmens static int debug;
7849cdee0eSKees Lemmens 
7949cdee0eSKees Lemmens /* requests */
8049cdee0eSKees Lemmens #define	OTI6858_REQ_GET_STATUS		(USB_DIR_IN | USB_TYPE_VENDOR | 0x00)
8149cdee0eSKees Lemmens #define	OTI6858_REQ_T_GET_STATUS	0x01
8249cdee0eSKees Lemmens 
8349cdee0eSKees Lemmens #define	OTI6858_REQ_SET_LINE		(USB_DIR_OUT | USB_TYPE_VENDOR | 0x00)
8449cdee0eSKees Lemmens #define	OTI6858_REQ_T_SET_LINE		0x00
8549cdee0eSKees Lemmens 
8649cdee0eSKees Lemmens #define	OTI6858_REQ_CHECK_TXBUFF	(USB_DIR_IN | USB_TYPE_VENDOR | 0x01)
8749cdee0eSKees Lemmens #define	OTI6858_REQ_T_CHECK_TXBUFF	0x00
8849cdee0eSKees Lemmens 
8949cdee0eSKees Lemmens /* format of the control packet */
9049cdee0eSKees Lemmens struct oti6858_control_pkt {
91fd05e720SAl Viro 	__le16	divisor;	/* baud rate = 96000000 / (16 * divisor), LE */
9249cdee0eSKees Lemmens #define OTI6858_MAX_BAUD_RATE	3000000
9349cdee0eSKees Lemmens 	u8	frame_fmt;
9449cdee0eSKees Lemmens #define FMT_STOP_BITS_MASK	0xc0
9549cdee0eSKees Lemmens #define FMT_STOP_BITS_1		0x00
9649cdee0eSKees Lemmens #define FMT_STOP_BITS_2		0x40	/* 1.5 stop bits if FMT_DATA_BITS_5 */
9749cdee0eSKees Lemmens #define FMT_PARITY_MASK		0x38
9849cdee0eSKees Lemmens #define FMT_PARITY_NONE		0x00
9949cdee0eSKees Lemmens #define FMT_PARITY_ODD		0x08
10049cdee0eSKees Lemmens #define FMT_PARITY_EVEN		0x18
10149cdee0eSKees Lemmens #define FMT_PARITY_MARK		0x28
10249cdee0eSKees Lemmens #define FMT_PARITY_SPACE	0x38
10349cdee0eSKees Lemmens #define FMT_DATA_BITS_MASK	0x03
10449cdee0eSKees Lemmens #define FMT_DATA_BITS_5		0x00
10549cdee0eSKees Lemmens #define FMT_DATA_BITS_6		0x01
10649cdee0eSKees Lemmens #define FMT_DATA_BITS_7		0x02
10749cdee0eSKees Lemmens #define FMT_DATA_BITS_8		0x03
10849cdee0eSKees Lemmens 	u8	something;	/* always equals 0x43 */
10949cdee0eSKees Lemmens 	u8	control;	/* settings of flow control lines */
11049cdee0eSKees Lemmens #define CONTROL_MASK		0x0c
11149cdee0eSKees Lemmens #define CONTROL_DTR_HIGH	0x08
11249cdee0eSKees Lemmens #define CONTROL_RTS_HIGH	0x04
11349cdee0eSKees Lemmens 	u8	tx_status;
11449cdee0eSKees Lemmens #define	TX_BUFFER_EMPTIED	0x09
11549cdee0eSKees Lemmens 	u8	pin_state;
11649cdee0eSKees Lemmens #define PIN_MASK		0x3f
11749cdee0eSKees Lemmens #define PIN_RTS			0x20	/* output pin */
11849cdee0eSKees Lemmens #define PIN_CTS			0x10	/* input pin, active low */
11949cdee0eSKees Lemmens #define PIN_DSR			0x08	/* input pin, active low */
12049cdee0eSKees Lemmens #define PIN_DTR			0x04	/* output pin */
12149cdee0eSKees Lemmens #define PIN_RI			0x02	/* input pin, active low */
12249cdee0eSKees Lemmens #define PIN_DCD			0x01	/* input pin, active low */
12349cdee0eSKees Lemmens 	u8	rx_bytes_avail;		/* number of bytes in rx buffer */;
12449cdee0eSKees Lemmens };
12549cdee0eSKees Lemmens 
12649cdee0eSKees Lemmens #define OTI6858_CTRL_PKT_SIZE	sizeof(struct oti6858_control_pkt)
12749cdee0eSKees Lemmens #define OTI6858_CTRL_EQUALS_PENDING(a, priv) \
12849cdee0eSKees Lemmens 	(((a)->divisor == (priv)->pending_setup.divisor) \
12949cdee0eSKees Lemmens 	  && ((a)->control == (priv)->pending_setup.control) \
13049cdee0eSKees Lemmens 	  && ((a)->frame_fmt == (priv)->pending_setup.frame_fmt))
13149cdee0eSKees Lemmens 
13249cdee0eSKees Lemmens /* function prototypes */
133a509a7e4SAlan Cox static int oti6858_open(struct tty_struct *tty, struct usb_serial_port *port);
134335f8514SAlan Cox static void oti6858_close(struct usb_serial_port *port);
13595da310eSAlan Cox static void oti6858_set_termios(struct tty_struct *tty,
13695da310eSAlan Cox 			struct usb_serial_port *port, struct ktermios *old);
137fe1ae7fdSAlan Cox static void oti6858_init_termios(struct tty_struct *tty);
13800a0d0d6SAlan Cox static int oti6858_ioctl(struct tty_struct *tty,
13949cdee0eSKees Lemmens 			unsigned int cmd, unsigned long arg);
14049cdee0eSKees Lemmens static void oti6858_read_int_callback(struct urb *urb);
14149cdee0eSKees Lemmens static void oti6858_read_bulk_callback(struct urb *urb);
14249cdee0eSKees Lemmens static void oti6858_write_bulk_callback(struct urb *urb);
14395da310eSAlan Cox static int oti6858_write(struct tty_struct *tty, struct usb_serial_port *port,
14449cdee0eSKees Lemmens 			const unsigned char *buf, int count);
14595da310eSAlan Cox static int oti6858_write_room(struct tty_struct *tty);
14695da310eSAlan Cox static int oti6858_chars_in_buffer(struct tty_struct *tty);
14760b33c13SAlan Cox static int oti6858_tiocmget(struct tty_struct *tty);
14820b9d177SAlan Cox static int oti6858_tiocmset(struct tty_struct *tty,
14949cdee0eSKees Lemmens 				unsigned int set, unsigned int clear);
15049cdee0eSKees Lemmens static int oti6858_startup(struct usb_serial *serial);
151f9c99bb8SAlan Stern static void oti6858_release(struct usb_serial *serial);
15249cdee0eSKees Lemmens 
15349cdee0eSKees Lemmens /* device info */
15449cdee0eSKees Lemmens static struct usb_serial_driver oti6858_device = {
15549cdee0eSKees Lemmens 	.driver = {
15649cdee0eSKees Lemmens 		.owner =	THIS_MODULE,
15749cdee0eSKees Lemmens 		.name =		"oti6858",
15849cdee0eSKees Lemmens 	},
15949cdee0eSKees Lemmens 	.id_table =		id_table,
1605620b5f7SAlan Stern 	.usb_driver =		&oti6858_driver,
16149cdee0eSKees Lemmens 	.num_ports =		1,
16249cdee0eSKees Lemmens 	.open =			oti6858_open,
16349cdee0eSKees Lemmens 	.close =		oti6858_close,
16449cdee0eSKees Lemmens 	.write =		oti6858_write,
16549cdee0eSKees Lemmens 	.ioctl =		oti6858_ioctl,
16649cdee0eSKees Lemmens 	.set_termios =		oti6858_set_termios,
167fe1ae7fdSAlan Cox 	.init_termios = 	oti6858_init_termios,
16849cdee0eSKees Lemmens 	.tiocmget =		oti6858_tiocmget,
16949cdee0eSKees Lemmens 	.tiocmset =		oti6858_tiocmset,
17049cdee0eSKees Lemmens 	.read_bulk_callback =	oti6858_read_bulk_callback,
17149cdee0eSKees Lemmens 	.read_int_callback =	oti6858_read_int_callback,
17249cdee0eSKees Lemmens 	.write_bulk_callback =	oti6858_write_bulk_callback,
17349cdee0eSKees Lemmens 	.write_room =		oti6858_write_room,
17449cdee0eSKees Lemmens 	.chars_in_buffer =	oti6858_chars_in_buffer,
17549cdee0eSKees Lemmens 	.attach =		oti6858_startup,
176f9c99bb8SAlan Stern 	.release =		oti6858_release,
17749cdee0eSKees Lemmens };
17849cdee0eSKees Lemmens 
17949cdee0eSKees Lemmens struct oti6858_private {
18049cdee0eSKees Lemmens 	spinlock_t lock;
18149cdee0eSKees Lemmens 
18249cdee0eSKees Lemmens 	struct oti6858_control_pkt status;
18349cdee0eSKees Lemmens 
18449cdee0eSKees Lemmens 	struct {
18549cdee0eSKees Lemmens 		u8 read_urb_in_use;
18649cdee0eSKees Lemmens 		u8 write_urb_in_use;
18749cdee0eSKees Lemmens 	} flags;
18849cdee0eSKees Lemmens 	struct delayed_work delayed_write_work;
18949cdee0eSKees Lemmens 
19049cdee0eSKees Lemmens 	struct {
191fd05e720SAl Viro 		__le16 divisor;
19249cdee0eSKees Lemmens 		u8 frame_fmt;
19349cdee0eSKees Lemmens 		u8 control;
19449cdee0eSKees Lemmens 	} pending_setup;
19549cdee0eSKees Lemmens 	u8 transient;
19649cdee0eSKees Lemmens 	u8 setup_done;
19749cdee0eSKees Lemmens 	struct delayed_work delayed_setup_work;
19849cdee0eSKees Lemmens 
19949cdee0eSKees Lemmens 	wait_queue_head_t intr_wait;
20049cdee0eSKees Lemmens 	struct usb_serial_port *port;   /* USB port with which associated */
20149cdee0eSKees Lemmens };
20249cdee0eSKees Lemmens 
20349cdee0eSKees Lemmens static void setup_line(struct work_struct *work)
20449cdee0eSKees Lemmens {
2052a77c814SAlan Cox 	struct oti6858_private *priv = container_of(work,
2062a77c814SAlan Cox 			struct oti6858_private, delayed_setup_work.work);
20749cdee0eSKees Lemmens 	struct usb_serial_port *port = priv->port;
20849cdee0eSKees Lemmens 	struct oti6858_control_pkt *new_setup;
20949cdee0eSKees Lemmens 	unsigned long flags;
21049cdee0eSKees Lemmens 	int result;
21149cdee0eSKees Lemmens 
212441b62c1SHarvey Harrison 	dbg("%s(port = %d)", __func__, port->number);
21349cdee0eSKees Lemmens 
2142a77c814SAlan Cox 	new_setup = kmalloc(OTI6858_CTRL_PKT_SIZE, GFP_KERNEL);
2152a77c814SAlan Cox 	if (new_setup == NULL) {
216441b62c1SHarvey Harrison 		dev_err(&port->dev, "%s(): out of memory!\n", __func__);
21749cdee0eSKees Lemmens 		/* we will try again */
2182a77c814SAlan Cox 		schedule_delayed_work(&priv->delayed_setup_work,
2192a77c814SAlan Cox 						msecs_to_jiffies(2));
22049cdee0eSKees Lemmens 		return;
22149cdee0eSKees Lemmens 	}
22249cdee0eSKees Lemmens 
22349cdee0eSKees Lemmens 	result = usb_control_msg(port->serial->dev,
22449cdee0eSKees Lemmens 				usb_rcvctrlpipe(port->serial->dev, 0),
22549cdee0eSKees Lemmens 				OTI6858_REQ_T_GET_STATUS,
22649cdee0eSKees Lemmens 				OTI6858_REQ_GET_STATUS,
22749cdee0eSKees Lemmens 				0, 0,
22849cdee0eSKees Lemmens 				new_setup, OTI6858_CTRL_PKT_SIZE,
22949cdee0eSKees Lemmens 				100);
23049cdee0eSKees Lemmens 
23149cdee0eSKees Lemmens 	if (result != OTI6858_CTRL_PKT_SIZE) {
232441b62c1SHarvey Harrison 		dev_err(&port->dev, "%s(): error reading status\n", __func__);
23349cdee0eSKees Lemmens 		kfree(new_setup);
23449cdee0eSKees Lemmens 		/* we will try again */
2352a77c814SAlan Cox 		schedule_delayed_work(&priv->delayed_setup_work,
2362a77c814SAlan Cox 							msecs_to_jiffies(2));
23749cdee0eSKees Lemmens 		return;
23849cdee0eSKees Lemmens 	}
23949cdee0eSKees Lemmens 
24049cdee0eSKees Lemmens 	spin_lock_irqsave(&priv->lock, flags);
24149cdee0eSKees Lemmens 	if (!OTI6858_CTRL_EQUALS_PENDING(new_setup, priv)) {
24249cdee0eSKees Lemmens 		new_setup->divisor = priv->pending_setup.divisor;
24349cdee0eSKees Lemmens 		new_setup->control = priv->pending_setup.control;
24449cdee0eSKees Lemmens 		new_setup->frame_fmt = priv->pending_setup.frame_fmt;
24549cdee0eSKees Lemmens 
24649cdee0eSKees Lemmens 		spin_unlock_irqrestore(&priv->lock, flags);
24749cdee0eSKees Lemmens 		result = usb_control_msg(port->serial->dev,
24849cdee0eSKees Lemmens 					usb_sndctrlpipe(port->serial->dev, 0),
24949cdee0eSKees Lemmens 					OTI6858_REQ_T_SET_LINE,
25049cdee0eSKees Lemmens 					OTI6858_REQ_SET_LINE,
25149cdee0eSKees Lemmens 					0, 0,
25249cdee0eSKees Lemmens 					new_setup, OTI6858_CTRL_PKT_SIZE,
25349cdee0eSKees Lemmens 					100);
25449cdee0eSKees Lemmens 	} else {
25549cdee0eSKees Lemmens 		spin_unlock_irqrestore(&priv->lock, flags);
25649cdee0eSKees Lemmens 		result = 0;
25749cdee0eSKees Lemmens 	}
25849cdee0eSKees Lemmens 	kfree(new_setup);
25949cdee0eSKees Lemmens 
26049cdee0eSKees Lemmens 	spin_lock_irqsave(&priv->lock, flags);
26149cdee0eSKees Lemmens 	if (result != OTI6858_CTRL_PKT_SIZE)
26249cdee0eSKees Lemmens 		priv->transient = 0;
26349cdee0eSKees Lemmens 	priv->setup_done = 1;
26449cdee0eSKees Lemmens 	spin_unlock_irqrestore(&priv->lock, flags);
26549cdee0eSKees Lemmens 
266441b62c1SHarvey Harrison 	dbg("%s(): submitting interrupt urb", __func__);
26740d28582SOliver Neukum 	result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
26849cdee0eSKees Lemmens 	if (result != 0) {
26949cdee0eSKees Lemmens 		dev_err(&port->dev, "%s(): usb_submit_urb() failed"
270441b62c1SHarvey Harrison 				" with error %d\n", __func__, result);
27149cdee0eSKees Lemmens 	}
27249cdee0eSKees Lemmens }
27349cdee0eSKees Lemmens 
2747d7917bcSBill Pemberton static void send_data(struct work_struct *work)
27549cdee0eSKees Lemmens {
2762a77c814SAlan Cox 	struct oti6858_private *priv = container_of(work,
2772a77c814SAlan Cox 			struct oti6858_private, delayed_write_work.work);
27849cdee0eSKees Lemmens 	struct usb_serial_port *port = priv->port;
27949cdee0eSKees Lemmens 	int count = 0, result;
28049cdee0eSKees Lemmens 	unsigned long flags;
281d2126326SJohan Hovold 	u8 *allow;
28249cdee0eSKees Lemmens 
283441b62c1SHarvey Harrison 	dbg("%s(port = %d)", __func__, port->number);
28449cdee0eSKees Lemmens 
28549cdee0eSKees Lemmens 	spin_lock_irqsave(&priv->lock, flags);
28649cdee0eSKees Lemmens 	if (priv->flags.write_urb_in_use) {
28749cdee0eSKees Lemmens 		spin_unlock_irqrestore(&priv->lock, flags);
2882a77c814SAlan Cox 		schedule_delayed_work(&priv->delayed_write_work,
2892a77c814SAlan Cox 						msecs_to_jiffies(2));
29049cdee0eSKees Lemmens 		return;
29149cdee0eSKees Lemmens 	}
29249cdee0eSKees Lemmens 	priv->flags.write_urb_in_use = 1;
29349cdee0eSKees Lemmens 	spin_unlock_irqrestore(&priv->lock, flags);
29428f27dcbSJohan Hovold 
29528f27dcbSJohan Hovold 	spin_lock_irqsave(&port->lock, flags);
29628f27dcbSJohan Hovold 	count = kfifo_len(&port->write_fifo);
29728f27dcbSJohan Hovold 	spin_unlock_irqrestore(&port->lock, flags);
29828f27dcbSJohan Hovold 
29949cdee0eSKees Lemmens 	if (count > port->bulk_out_size)
30049cdee0eSKees Lemmens 		count = port->bulk_out_size;
30149cdee0eSKees Lemmens 
30249cdee0eSKees Lemmens 	if (count != 0) {
303d2126326SJohan Hovold 		allow = kmalloc(1, GFP_KERNEL);
304d2126326SJohan Hovold 		if (!allow) {
305d2126326SJohan Hovold 			dev_err(&port->dev, "%s(): kmalloc failed\n",
306d2126326SJohan Hovold 					__func__);
307d2126326SJohan Hovold 			return;
308d2126326SJohan Hovold 		}
30949cdee0eSKees Lemmens 		result = usb_control_msg(port->serial->dev,
31049cdee0eSKees Lemmens 				usb_rcvctrlpipe(port->serial->dev, 0),
31149cdee0eSKees Lemmens 				OTI6858_REQ_T_CHECK_TXBUFF,
31249cdee0eSKees Lemmens 				OTI6858_REQ_CHECK_TXBUFF,
313d2126326SJohan Hovold 				count, 0, allow, 1, 100);
314d2126326SJohan Hovold 		if (result != 1 || *allow != 0)
31549cdee0eSKees Lemmens 			count = 0;
316d2126326SJohan Hovold 		kfree(allow);
31749cdee0eSKees Lemmens 	}
31849cdee0eSKees Lemmens 
31949cdee0eSKees Lemmens 	if (count == 0) {
32049cdee0eSKees Lemmens 		priv->flags.write_urb_in_use = 0;
32149cdee0eSKees Lemmens 
322441b62c1SHarvey Harrison 		dbg("%s(): submitting interrupt urb", __func__);
32340d28582SOliver Neukum 		result = usb_submit_urb(port->interrupt_in_urb, GFP_NOIO);
32449cdee0eSKees Lemmens 		if (result != 0) {
32549cdee0eSKees Lemmens 			dev_err(&port->dev, "%s(): usb_submit_urb() failed"
326441b62c1SHarvey Harrison 				" with error %d\n", __func__, result);
32749cdee0eSKees Lemmens 		}
32849cdee0eSKees Lemmens 		return;
32949cdee0eSKees Lemmens 	}
33049cdee0eSKees Lemmens 
33128f27dcbSJohan Hovold 	count = kfifo_out_locked(&port->write_fifo,
332e3c1803fSJohan Hovold 					port->write_urb->transfer_buffer,
33328f27dcbSJohan Hovold 					count, &port->lock);
33449cdee0eSKees Lemmens 	port->write_urb->transfer_buffer_length = count;
33540d28582SOliver Neukum 	result = usb_submit_urb(port->write_urb, GFP_NOIO);
33649cdee0eSKees Lemmens 	if (result != 0) {
33749cdee0eSKees Lemmens 		dev_err(&port->dev, "%s(): usb_submit_urb() failed"
338441b62c1SHarvey Harrison 			       " with error %d\n", __func__, result);
33949cdee0eSKees Lemmens 		priv->flags.write_urb_in_use = 0;
34049cdee0eSKees Lemmens 	}
34149cdee0eSKees Lemmens 
34249cdee0eSKees Lemmens 	usb_serial_port_softint(port);
34349cdee0eSKees Lemmens }
34449cdee0eSKees Lemmens 
34549cdee0eSKees Lemmens static int oti6858_startup(struct usb_serial *serial)
34649cdee0eSKees Lemmens {
34749cdee0eSKees Lemmens 	struct usb_serial_port *port = serial->port[0];
34849cdee0eSKees Lemmens 	struct oti6858_private *priv;
34949cdee0eSKees Lemmens 	int i;
35049cdee0eSKees Lemmens 
35149cdee0eSKees Lemmens 	for (i = 0; i < serial->num_ports; ++i) {
35249cdee0eSKees Lemmens 		priv = kzalloc(sizeof(struct oti6858_private), GFP_KERNEL);
35349cdee0eSKees Lemmens 		if (!priv)
35449cdee0eSKees Lemmens 			break;
35549cdee0eSKees Lemmens 
35649cdee0eSKees Lemmens 		spin_lock_init(&priv->lock);
35749cdee0eSKees Lemmens 		init_waitqueue_head(&priv->intr_wait);
3582a77c814SAlan Cox /*		INIT_WORK(&priv->setup_work, setup_line, serial->port[i]); */
3592a77c814SAlan Cox /*		INIT_WORK(&priv->write_work, send_data, serial->port[i]); */
36049cdee0eSKees Lemmens 		priv->port = port;
36149cdee0eSKees Lemmens 		INIT_DELAYED_WORK(&priv->delayed_setup_work, setup_line);
36249cdee0eSKees Lemmens 		INIT_DELAYED_WORK(&priv->delayed_write_work, send_data);
36349cdee0eSKees Lemmens 
36449cdee0eSKees Lemmens 		usb_set_serial_port_data(serial->port[i], priv);
36549cdee0eSKees Lemmens 	}
36649cdee0eSKees Lemmens 	if (i == serial->num_ports)
36749cdee0eSKees Lemmens 		return 0;
36849cdee0eSKees Lemmens 
36949cdee0eSKees Lemmens 	for (--i; i >= 0; --i) {
37049cdee0eSKees Lemmens 		priv = usb_get_serial_port_data(serial->port[i]);
37149cdee0eSKees Lemmens 		kfree(priv);
37249cdee0eSKees Lemmens 		usb_set_serial_port_data(serial->port[i], NULL);
37349cdee0eSKees Lemmens 	}
37449cdee0eSKees Lemmens 	return -ENOMEM;
37549cdee0eSKees Lemmens }
37649cdee0eSKees Lemmens 
37795da310eSAlan Cox static int oti6858_write(struct tty_struct *tty, struct usb_serial_port *port,
37849cdee0eSKees Lemmens 			const unsigned char *buf, int count)
37949cdee0eSKees Lemmens {
380441b62c1SHarvey Harrison 	dbg("%s(port = %d, count = %d)", __func__, port->number, count);
38149cdee0eSKees Lemmens 
38249cdee0eSKees Lemmens 	if (!count)
38349cdee0eSKees Lemmens 		return count;
38449cdee0eSKees Lemmens 
38528f27dcbSJohan Hovold 	count = kfifo_in_locked(&port->write_fifo, buf, count, &port->lock);
38649cdee0eSKees Lemmens 
38749cdee0eSKees Lemmens 	return count;
38849cdee0eSKees Lemmens }
38949cdee0eSKees Lemmens 
39095da310eSAlan Cox static int oti6858_write_room(struct tty_struct *tty)
39149cdee0eSKees Lemmens {
39295da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
39349cdee0eSKees Lemmens 	int room = 0;
39449cdee0eSKees Lemmens 	unsigned long flags;
39549cdee0eSKees Lemmens 
396441b62c1SHarvey Harrison 	dbg("%s(port = %d)", __func__, port->number);
39749cdee0eSKees Lemmens 
39828f27dcbSJohan Hovold 	spin_lock_irqsave(&port->lock, flags);
39928f27dcbSJohan Hovold 	room = kfifo_avail(&port->write_fifo);
40028f27dcbSJohan Hovold 	spin_unlock_irqrestore(&port->lock, flags);
40149cdee0eSKees Lemmens 
40249cdee0eSKees Lemmens 	return room;
40349cdee0eSKees Lemmens }
40449cdee0eSKees Lemmens 
40595da310eSAlan Cox static int oti6858_chars_in_buffer(struct tty_struct *tty)
40649cdee0eSKees Lemmens {
40795da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
40849cdee0eSKees Lemmens 	int chars = 0;
40949cdee0eSKees Lemmens 	unsigned long flags;
41049cdee0eSKees Lemmens 
411441b62c1SHarvey Harrison 	dbg("%s(port = %d)", __func__, port->number);
41249cdee0eSKees Lemmens 
41328f27dcbSJohan Hovold 	spin_lock_irqsave(&port->lock, flags);
41428f27dcbSJohan Hovold 	chars = kfifo_len(&port->write_fifo);
41528f27dcbSJohan Hovold 	spin_unlock_irqrestore(&port->lock, flags);
41649cdee0eSKees Lemmens 
41749cdee0eSKees Lemmens 	return chars;
41849cdee0eSKees Lemmens }
41949cdee0eSKees Lemmens 
420fe1ae7fdSAlan Cox static void oti6858_init_termios(struct tty_struct *tty)
421fe1ae7fdSAlan Cox {
422fe1ae7fdSAlan Cox 	*(tty->termios) = tty_std_termios;
423fe1ae7fdSAlan Cox 	tty->termios->c_cflag = B38400 | CS8 | CREAD | HUPCL | CLOCAL;
424fe1ae7fdSAlan Cox 	tty->termios->c_ispeed = 38400;
425fe1ae7fdSAlan Cox 	tty->termios->c_ospeed = 38400;
426fe1ae7fdSAlan Cox }
427fe1ae7fdSAlan Cox 
42895da310eSAlan Cox static void oti6858_set_termios(struct tty_struct *tty,
42995da310eSAlan Cox 		struct usb_serial_port *port, struct ktermios *old_termios)
43049cdee0eSKees Lemmens {
43149cdee0eSKees Lemmens 	struct oti6858_private *priv = usb_get_serial_port_data(port);
43249cdee0eSKees Lemmens 	unsigned long flags;
43349cdee0eSKees Lemmens 	unsigned int cflag;
43449cdee0eSKees Lemmens 	u8 frame_fmt, control;
435fd05e720SAl Viro 	__le16 divisor;
43649cdee0eSKees Lemmens 	int br;
43749cdee0eSKees Lemmens 
438441b62c1SHarvey Harrison 	dbg("%s(port = %d)", __func__, port->number);
43949cdee0eSKees Lemmens 
44095da310eSAlan Cox 	if (!tty) {
441441b62c1SHarvey Harrison 		dbg("%s(): no tty structures", __func__);
44249cdee0eSKees Lemmens 		return;
44349cdee0eSKees Lemmens 	}
44449cdee0eSKees Lemmens 
44595da310eSAlan Cox 	cflag = tty->termios->c_cflag;
44649cdee0eSKees Lemmens 
44749cdee0eSKees Lemmens 	spin_lock_irqsave(&priv->lock, flags);
44849cdee0eSKees Lemmens 	divisor = priv->pending_setup.divisor;
44949cdee0eSKees Lemmens 	frame_fmt = priv->pending_setup.frame_fmt;
45049cdee0eSKees Lemmens 	control = priv->pending_setup.control;
45149cdee0eSKees Lemmens 	spin_unlock_irqrestore(&priv->lock, flags);
45249cdee0eSKees Lemmens 
45349cdee0eSKees Lemmens 	frame_fmt &= ~FMT_DATA_BITS_MASK;
45449cdee0eSKees Lemmens 	switch (cflag & CSIZE) {
45549cdee0eSKees Lemmens 	case CS5:
45649cdee0eSKees Lemmens 		frame_fmt |= FMT_DATA_BITS_5;
45749cdee0eSKees Lemmens 		break;
45849cdee0eSKees Lemmens 	case CS6:
45949cdee0eSKees Lemmens 		frame_fmt |= FMT_DATA_BITS_6;
46049cdee0eSKees Lemmens 		break;
46149cdee0eSKees Lemmens 	case CS7:
46249cdee0eSKees Lemmens 		frame_fmt |= FMT_DATA_BITS_7;
46349cdee0eSKees Lemmens 		break;
46449cdee0eSKees Lemmens 	default:
46549cdee0eSKees Lemmens 	case CS8:
46649cdee0eSKees Lemmens 		frame_fmt |= FMT_DATA_BITS_8;
46749cdee0eSKees Lemmens 		break;
46849cdee0eSKees Lemmens 	}
46949cdee0eSKees Lemmens 
47049cdee0eSKees Lemmens 	/* manufacturer claims that this device can work with baud rates
47149cdee0eSKees Lemmens 	 * up to 3 Mbps; I've tested it only on 115200 bps, so I can't
47249cdee0eSKees Lemmens 	 * guarantee that any other baud rate will work (especially
47349cdee0eSKees Lemmens 	 * the higher ones)
47449cdee0eSKees Lemmens 	 */
47595da310eSAlan Cox 	br = tty_get_baud_rate(tty);
47649cdee0eSKees Lemmens 	if (br == 0) {
47749cdee0eSKees Lemmens 		divisor = 0;
478b0a239daSAlan Cox 	} else {
47949cdee0eSKees Lemmens 		int real_br;
480fd05e720SAl Viro 		int new_divisor;
481b0a239daSAlan Cox 		br = min(br, OTI6858_MAX_BAUD_RATE);
48249cdee0eSKees Lemmens 
483fd05e720SAl Viro 		new_divisor = (96000000 + 8 * br) / (16 * br);
484fd05e720SAl Viro 		real_br = 96000000 / (16 * new_divisor);
485fd05e720SAl Viro 		divisor = cpu_to_le16(new_divisor);
48695da310eSAlan Cox 		tty_encode_baud_rate(tty, real_br, real_br);
48749cdee0eSKees Lemmens 	}
48849cdee0eSKees Lemmens 
48949cdee0eSKees Lemmens 	frame_fmt &= ~FMT_STOP_BITS_MASK;
4902a77c814SAlan Cox 	if ((cflag & CSTOPB) != 0)
49149cdee0eSKees Lemmens 		frame_fmt |= FMT_STOP_BITS_2;
4922a77c814SAlan Cox 	else
49349cdee0eSKees Lemmens 		frame_fmt |= FMT_STOP_BITS_1;
49449cdee0eSKees Lemmens 
49549cdee0eSKees Lemmens 	frame_fmt &= ~FMT_PARITY_MASK;
49649cdee0eSKees Lemmens 	if ((cflag & PARENB) != 0) {
4972a77c814SAlan Cox 		if ((cflag & PARODD) != 0)
49849cdee0eSKees Lemmens 			frame_fmt |= FMT_PARITY_ODD;
4992a77c814SAlan Cox 		else
50049cdee0eSKees Lemmens 			frame_fmt |= FMT_PARITY_EVEN;
50149cdee0eSKees Lemmens 	} else {
50249cdee0eSKees Lemmens 		frame_fmt |= FMT_PARITY_NONE;
50349cdee0eSKees Lemmens 	}
50449cdee0eSKees Lemmens 
50549cdee0eSKees Lemmens 	control &= ~CONTROL_MASK;
50649cdee0eSKees Lemmens 	if ((cflag & CRTSCTS) != 0)
50749cdee0eSKees Lemmens 		control |= (CONTROL_DTR_HIGH | CONTROL_RTS_HIGH);
50849cdee0eSKees Lemmens 
50949cdee0eSKees Lemmens 	/* change control lines if we are switching to or from B0 */
51049cdee0eSKees Lemmens 	/* FIXME:
51149cdee0eSKees Lemmens 	spin_lock_irqsave(&priv->lock, flags);
51249cdee0eSKees Lemmens 	control = priv->line_control;
51349cdee0eSKees Lemmens 	if ((cflag & CBAUD) == B0)
51449cdee0eSKees Lemmens 		priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
51549cdee0eSKees Lemmens 	else
51649cdee0eSKees Lemmens 		priv->line_control |= (CONTROL_DTR | CONTROL_RTS);
51749cdee0eSKees Lemmens 	if (control != priv->line_control) {
51849cdee0eSKees Lemmens 		control = priv->line_control;
51949cdee0eSKees Lemmens 		spin_unlock_irqrestore(&priv->lock, flags);
52049cdee0eSKees Lemmens 		set_control_lines(serial->dev, control);
52149cdee0eSKees Lemmens 	} else {
52249cdee0eSKees Lemmens 		spin_unlock_irqrestore(&priv->lock, flags);
52349cdee0eSKees Lemmens 	}
52449cdee0eSKees Lemmens 	*/
52549cdee0eSKees Lemmens 
52649cdee0eSKees Lemmens 	spin_lock_irqsave(&priv->lock, flags);
52749cdee0eSKees Lemmens 	if (divisor != priv->pending_setup.divisor
52849cdee0eSKees Lemmens 			|| control != priv->pending_setup.control
52949cdee0eSKees Lemmens 			|| frame_fmt != priv->pending_setup.frame_fmt) {
53049cdee0eSKees Lemmens 		priv->pending_setup.divisor = divisor;
53149cdee0eSKees Lemmens 		priv->pending_setup.control = control;
53249cdee0eSKees Lemmens 		priv->pending_setup.frame_fmt = frame_fmt;
53349cdee0eSKees Lemmens 	}
53449cdee0eSKees Lemmens 	spin_unlock_irqrestore(&priv->lock, flags);
53549cdee0eSKees Lemmens }
53649cdee0eSKees Lemmens 
537a509a7e4SAlan Cox static int oti6858_open(struct tty_struct *tty, struct usb_serial_port *port)
53849cdee0eSKees Lemmens {
53949cdee0eSKees Lemmens 	struct oti6858_private *priv = usb_get_serial_port_data(port);
54049cdee0eSKees Lemmens 	struct ktermios tmp_termios;
54149cdee0eSKees Lemmens 	struct usb_serial *serial = port->serial;
54249cdee0eSKees Lemmens 	struct oti6858_control_pkt *buf;
54349cdee0eSKees Lemmens 	unsigned long flags;
54449cdee0eSKees Lemmens 	int result;
54549cdee0eSKees Lemmens 
546441b62c1SHarvey Harrison 	dbg("%s(port = %d)", __func__, port->number);
54749cdee0eSKees Lemmens 
54849cdee0eSKees Lemmens 	usb_clear_halt(serial->dev, port->write_urb->pipe);
54949cdee0eSKees Lemmens 	usb_clear_halt(serial->dev, port->read_urb->pipe);
55049cdee0eSKees Lemmens 
5512a77c814SAlan Cox 	buf = kmalloc(OTI6858_CTRL_PKT_SIZE, GFP_KERNEL);
5522a77c814SAlan Cox 	if (buf == NULL) {
553441b62c1SHarvey Harrison 		dev_err(&port->dev, "%s(): out of memory!\n", __func__);
55449cdee0eSKees Lemmens 		return -ENOMEM;
55549cdee0eSKees Lemmens 	}
55649cdee0eSKees Lemmens 
55749cdee0eSKees Lemmens 	result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
55849cdee0eSKees Lemmens 				OTI6858_REQ_T_GET_STATUS,
55949cdee0eSKees Lemmens 				OTI6858_REQ_GET_STATUS,
56049cdee0eSKees Lemmens 				0, 0,
56149cdee0eSKees Lemmens 				buf, OTI6858_CTRL_PKT_SIZE,
56249cdee0eSKees Lemmens 				100);
56349cdee0eSKees Lemmens 	if (result != OTI6858_CTRL_PKT_SIZE) {
56449cdee0eSKees Lemmens 		/* assume default (after power-on reset) values */
56549cdee0eSKees Lemmens 		buf->divisor = cpu_to_le16(0x009c);	/* 38400 bps */
56649cdee0eSKees Lemmens 		buf->frame_fmt = 0x03;	/* 8N1 */
56749cdee0eSKees Lemmens 		buf->something = 0x43;
56849cdee0eSKees Lemmens 		buf->control = 0x4c;	/* DTR, RTS */
56949cdee0eSKees Lemmens 		buf->tx_status = 0x00;
57049cdee0eSKees Lemmens 		buf->pin_state = 0x5b;	/* RTS, CTS, DSR, DTR, RI, DCD */
57149cdee0eSKees Lemmens 		buf->rx_bytes_avail = 0x00;
57249cdee0eSKees Lemmens 	}
57349cdee0eSKees Lemmens 
57449cdee0eSKees Lemmens 	spin_lock_irqsave(&priv->lock, flags);
57549cdee0eSKees Lemmens 	memcpy(&priv->status, buf, OTI6858_CTRL_PKT_SIZE);
57649cdee0eSKees Lemmens 	priv->pending_setup.divisor = buf->divisor;
57749cdee0eSKees Lemmens 	priv->pending_setup.frame_fmt = buf->frame_fmt;
57849cdee0eSKees Lemmens 	priv->pending_setup.control = buf->control;
57949cdee0eSKees Lemmens 	spin_unlock_irqrestore(&priv->lock, flags);
58049cdee0eSKees Lemmens 	kfree(buf);
58149cdee0eSKees Lemmens 
582441b62c1SHarvey Harrison 	dbg("%s(): submitting interrupt urb", __func__);
58349cdee0eSKees Lemmens 	result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
58449cdee0eSKees Lemmens 	if (result != 0) {
58549cdee0eSKees Lemmens 		dev_err(&port->dev, "%s(): usb_submit_urb() failed"
586441b62c1SHarvey Harrison 			       " with error %d\n", __func__, result);
587335f8514SAlan Cox 		oti6858_close(port);
588*d5e450eeSJohan Hovold 		return result;
58949cdee0eSKees Lemmens 	}
59049cdee0eSKees Lemmens 
59149cdee0eSKees Lemmens 	/* setup termios */
59295da310eSAlan Cox 	if (tty)
59395da310eSAlan Cox 		oti6858_set_termios(tty, port, &tmp_termios);
594335f8514SAlan Cox 	port->port.drain_delay = 256;	/* FIXME: check the FIFO length */
59549cdee0eSKees Lemmens 	return 0;
59649cdee0eSKees Lemmens }
59749cdee0eSKees Lemmens 
598335f8514SAlan Cox static void oti6858_close(struct usb_serial_port *port)
59949cdee0eSKees Lemmens {
60049cdee0eSKees Lemmens 	struct oti6858_private *priv = usb_get_serial_port_data(port);
60149cdee0eSKees Lemmens 	unsigned long flags;
60249cdee0eSKees Lemmens 
603441b62c1SHarvey Harrison 	dbg("%s(port = %d)", __func__, port->number);
60449cdee0eSKees Lemmens 
60528f27dcbSJohan Hovold 	spin_lock_irqsave(&port->lock, flags);
60649cdee0eSKees Lemmens 	/* clear out any remaining data in the buffer */
60728f27dcbSJohan Hovold 	kfifo_reset_out(&port->write_fifo);
60828f27dcbSJohan Hovold 	spin_unlock_irqrestore(&port->lock, flags);
60949cdee0eSKees Lemmens 
610335f8514SAlan Cox 	dbg("%s(): after buf_clear()", __func__);
61149cdee0eSKees Lemmens 
61249cdee0eSKees Lemmens 	/* cancel scheduled setup */
613569ff2deSTejun Heo 	cancel_delayed_work_sync(&priv->delayed_setup_work);
614569ff2deSTejun Heo 	cancel_delayed_work_sync(&priv->delayed_write_work);
61549cdee0eSKees Lemmens 
61649cdee0eSKees Lemmens 	/* shutdown our urbs */
617441b62c1SHarvey Harrison 	dbg("%s(): shutting down urbs", __func__);
61849cdee0eSKees Lemmens 	usb_kill_urb(port->write_urb);
61949cdee0eSKees Lemmens 	usb_kill_urb(port->read_urb);
62049cdee0eSKees Lemmens 	usb_kill_urb(port->interrupt_in_urb);
62149cdee0eSKees Lemmens }
62249cdee0eSKees Lemmens 
62320b9d177SAlan Cox static int oti6858_tiocmset(struct tty_struct *tty,
62449cdee0eSKees Lemmens 				unsigned int set, unsigned int clear)
62549cdee0eSKees Lemmens {
62695da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
62749cdee0eSKees Lemmens 	struct oti6858_private *priv = usb_get_serial_port_data(port);
62849cdee0eSKees Lemmens 	unsigned long flags;
62949cdee0eSKees Lemmens 	u8 control;
63049cdee0eSKees Lemmens 
63149cdee0eSKees Lemmens 	dbg("%s(port = %d, set = 0x%08x, clear = 0x%08x)",
632441b62c1SHarvey Harrison 				__func__, port->number, set, clear);
63349cdee0eSKees Lemmens 
63449cdee0eSKees Lemmens 	if (!usb_get_intfdata(port->serial->interface))
63549cdee0eSKees Lemmens 		return -ENODEV;
63649cdee0eSKees Lemmens 
63749cdee0eSKees Lemmens 	/* FIXME: check if this is correct (active high/low) */
63849cdee0eSKees Lemmens 	spin_lock_irqsave(&priv->lock, flags);
63949cdee0eSKees Lemmens 	control = priv->pending_setup.control;
64049cdee0eSKees Lemmens 	if ((set & TIOCM_RTS) != 0)
64149cdee0eSKees Lemmens 		control |= CONTROL_RTS_HIGH;
64249cdee0eSKees Lemmens 	if ((set & TIOCM_DTR) != 0)
64349cdee0eSKees Lemmens 		control |= CONTROL_DTR_HIGH;
64449cdee0eSKees Lemmens 	if ((clear & TIOCM_RTS) != 0)
64549cdee0eSKees Lemmens 		control &= ~CONTROL_RTS_HIGH;
64649cdee0eSKees Lemmens 	if ((clear & TIOCM_DTR) != 0)
64749cdee0eSKees Lemmens 		control &= ~CONTROL_DTR_HIGH;
64849cdee0eSKees Lemmens 
6492a77c814SAlan Cox 	if (control != priv->pending_setup.control)
65049cdee0eSKees Lemmens 		priv->pending_setup.control = control;
65149cdee0eSKees Lemmens 
6522a77c814SAlan Cox 	spin_unlock_irqrestore(&priv->lock, flags);
65349cdee0eSKees Lemmens 	return 0;
65449cdee0eSKees Lemmens }
65549cdee0eSKees Lemmens 
65660b33c13SAlan Cox static int oti6858_tiocmget(struct tty_struct *tty)
65749cdee0eSKees Lemmens {
65895da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
65949cdee0eSKees Lemmens 	struct oti6858_private *priv = usb_get_serial_port_data(port);
66049cdee0eSKees Lemmens 	unsigned long flags;
66149cdee0eSKees Lemmens 	unsigned pin_state;
66249cdee0eSKees Lemmens 	unsigned result = 0;
66349cdee0eSKees Lemmens 
664441b62c1SHarvey Harrison 	dbg("%s(port = %d)", __func__, port->number);
66549cdee0eSKees Lemmens 
66649cdee0eSKees Lemmens 	if (!usb_get_intfdata(port->serial->interface))
66749cdee0eSKees Lemmens 		return -ENODEV;
66849cdee0eSKees Lemmens 
66949cdee0eSKees Lemmens 	spin_lock_irqsave(&priv->lock, flags);
67049cdee0eSKees Lemmens 	pin_state = priv->status.pin_state & PIN_MASK;
67149cdee0eSKees Lemmens 	spin_unlock_irqrestore(&priv->lock, flags);
67249cdee0eSKees Lemmens 
67349cdee0eSKees Lemmens 	/* FIXME: check if this is correct (active high/low) */
67449cdee0eSKees Lemmens 	if ((pin_state & PIN_RTS) != 0)
67549cdee0eSKees Lemmens 		result |= TIOCM_RTS;
67649cdee0eSKees Lemmens 	if ((pin_state & PIN_CTS) != 0)
67749cdee0eSKees Lemmens 		result |= TIOCM_CTS;
67849cdee0eSKees Lemmens 	if ((pin_state & PIN_DSR) != 0)
67949cdee0eSKees Lemmens 		result |= TIOCM_DSR;
68049cdee0eSKees Lemmens 	if ((pin_state & PIN_DTR) != 0)
68149cdee0eSKees Lemmens 		result |= TIOCM_DTR;
68249cdee0eSKees Lemmens 	if ((pin_state & PIN_RI) != 0)
68349cdee0eSKees Lemmens 		result |= TIOCM_RI;
68449cdee0eSKees Lemmens 	if ((pin_state & PIN_DCD) != 0)
68549cdee0eSKees Lemmens 		result |= TIOCM_CD;
68649cdee0eSKees Lemmens 
687441b62c1SHarvey Harrison 	dbg("%s() = 0x%08x", __func__, result);
68849cdee0eSKees Lemmens 
68949cdee0eSKees Lemmens 	return result;
69049cdee0eSKees Lemmens }
69149cdee0eSKees Lemmens 
69249cdee0eSKees Lemmens static int wait_modem_info(struct usb_serial_port *port, unsigned int arg)
69349cdee0eSKees Lemmens {
69449cdee0eSKees Lemmens 	struct oti6858_private *priv = usb_get_serial_port_data(port);
69549cdee0eSKees Lemmens 	unsigned long flags;
69649cdee0eSKees Lemmens 	unsigned int prev, status;
69749cdee0eSKees Lemmens 	unsigned int changed;
69849cdee0eSKees Lemmens 
69949cdee0eSKees Lemmens 	spin_lock_irqsave(&priv->lock, flags);
70049cdee0eSKees Lemmens 	prev = priv->status.pin_state;
70149cdee0eSKees Lemmens 	spin_unlock_irqrestore(&priv->lock, flags);
70249cdee0eSKees Lemmens 
70349cdee0eSKees Lemmens 	while (1) {
7042a77c814SAlan Cox 		wait_event_interruptible(priv->intr_wait,
7052a77c814SAlan Cox 					priv->status.pin_state != prev);
70649cdee0eSKees Lemmens 		if (signal_pending(current))
70749cdee0eSKees Lemmens 			return -ERESTARTSYS;
70849cdee0eSKees Lemmens 
70949cdee0eSKees Lemmens 		spin_lock_irqsave(&priv->lock, flags);
71049cdee0eSKees Lemmens 		status = priv->status.pin_state & PIN_MASK;
71149cdee0eSKees Lemmens 		spin_unlock_irqrestore(&priv->lock, flags);
71249cdee0eSKees Lemmens 
71349cdee0eSKees Lemmens 		changed = prev ^ status;
71449cdee0eSKees Lemmens 		/* FIXME: check if this is correct (active high/low) */
71549cdee0eSKees Lemmens 		if (((arg & TIOCM_RNG) && (changed & PIN_RI)) ||
71649cdee0eSKees Lemmens 		    ((arg & TIOCM_DSR) && (changed & PIN_DSR)) ||
71749cdee0eSKees Lemmens 		    ((arg & TIOCM_CD)  && (changed & PIN_DCD)) ||
7182a77c814SAlan Cox 		    ((arg & TIOCM_CTS) && (changed & PIN_CTS)))
71949cdee0eSKees Lemmens 			return 0;
72049cdee0eSKees Lemmens 		prev = status;
72149cdee0eSKees Lemmens 	}
72249cdee0eSKees Lemmens 
72349cdee0eSKees Lemmens 	/* NOTREACHED */
72449cdee0eSKees Lemmens 	return 0;
72549cdee0eSKees Lemmens }
72649cdee0eSKees Lemmens 
72700a0d0d6SAlan Cox static int oti6858_ioctl(struct tty_struct *tty,
72849cdee0eSKees Lemmens 			unsigned int cmd, unsigned long arg)
72949cdee0eSKees Lemmens {
73095da310eSAlan Cox 	struct usb_serial_port *port = tty->driver_data;
73149cdee0eSKees Lemmens 
73249cdee0eSKees Lemmens 	dbg("%s(port = %d, cmd = 0x%04x, arg = 0x%08lx)",
733441b62c1SHarvey Harrison 				__func__, port->number, cmd, arg);
73449cdee0eSKees Lemmens 
73549cdee0eSKees Lemmens 	switch (cmd) {
73649cdee0eSKees Lemmens 	case TIOCMIWAIT:
737441b62c1SHarvey Harrison 		dbg("%s(): TIOCMIWAIT", __func__);
73849cdee0eSKees Lemmens 		return wait_modem_info(port, arg);
73949cdee0eSKees Lemmens 	default:
740441b62c1SHarvey Harrison 		dbg("%s(): 0x%04x not supported", __func__, cmd);
74149cdee0eSKees Lemmens 		break;
74249cdee0eSKees Lemmens 	}
74349cdee0eSKees Lemmens 	return -ENOIOCTLCMD;
74449cdee0eSKees Lemmens }
74549cdee0eSKees Lemmens 
74649cdee0eSKees Lemmens 
747f9c99bb8SAlan Stern static void oti6858_release(struct usb_serial *serial)
74849cdee0eSKees Lemmens {
74949cdee0eSKees Lemmens 	int i;
75049cdee0eSKees Lemmens 
751441b62c1SHarvey Harrison 	dbg("%s()", __func__);
75249cdee0eSKees Lemmens 
75328f27dcbSJohan Hovold 	for (i = 0; i < serial->num_ports; ++i)
75428f27dcbSJohan Hovold 		kfree(usb_get_serial_port_data(serial->port[i]));
75549cdee0eSKees Lemmens }
75649cdee0eSKees Lemmens 
75749cdee0eSKees Lemmens static void oti6858_read_int_callback(struct urb *urb)
75849cdee0eSKees Lemmens {
759cdc97792SMing Lei 	struct usb_serial_port *port =  urb->context;
76049cdee0eSKees Lemmens 	struct oti6858_private *priv = usb_get_serial_port_data(port);
76149cdee0eSKees Lemmens 	int transient = 0, can_recv = 0, resubmit = 1;
76278c26aebSGreg Kroah-Hartman 	int status = urb->status;
76349cdee0eSKees Lemmens 
76478c26aebSGreg Kroah-Hartman 	dbg("%s(port = %d, status = %d)",
765441b62c1SHarvey Harrison 				__func__, port->number, status);
76649cdee0eSKees Lemmens 
76778c26aebSGreg Kroah-Hartman 	switch (status) {
76849cdee0eSKees Lemmens 	case 0:
76949cdee0eSKees Lemmens 		/* success */
77049cdee0eSKees Lemmens 		break;
77149cdee0eSKees Lemmens 	case -ECONNRESET:
77249cdee0eSKees Lemmens 	case -ENOENT:
77349cdee0eSKees Lemmens 	case -ESHUTDOWN:
77449cdee0eSKees Lemmens 		/* this urb is terminated, clean up */
77549cdee0eSKees Lemmens 		dbg("%s(): urb shutting down with status: %d",
776441b62c1SHarvey Harrison 					__func__, status);
77749cdee0eSKees Lemmens 		return;
77849cdee0eSKees Lemmens 	default:
77949cdee0eSKees Lemmens 		dbg("%s(): nonzero urb status received: %d",
780441b62c1SHarvey Harrison 					__func__, status);
78149cdee0eSKees Lemmens 		break;
78249cdee0eSKees Lemmens 	}
78349cdee0eSKees Lemmens 
78478c26aebSGreg Kroah-Hartman 	if (status == 0 && urb->actual_length == OTI6858_CTRL_PKT_SIZE) {
78549cdee0eSKees Lemmens 		struct oti6858_control_pkt *xs = urb->transfer_buffer;
78649cdee0eSKees Lemmens 		unsigned long flags;
78749cdee0eSKees Lemmens 
78849cdee0eSKees Lemmens 		spin_lock_irqsave(&priv->lock, flags);
78949cdee0eSKees Lemmens 
79049cdee0eSKees Lemmens 		if (!priv->transient) {
79149cdee0eSKees Lemmens 			if (!OTI6858_CTRL_EQUALS_PENDING(xs, priv)) {
79249cdee0eSKees Lemmens 				if (xs->rx_bytes_avail == 0) {
79349cdee0eSKees Lemmens 					priv->transient = 4;
79449cdee0eSKees Lemmens 					priv->setup_done = 0;
79549cdee0eSKees Lemmens 					resubmit = 0;
79649cdee0eSKees Lemmens 					dbg("%s(): scheduling setup_line()",
797441b62c1SHarvey Harrison 					    __func__);
79849cdee0eSKees Lemmens 					schedule_delayed_work(&priv->delayed_setup_work, 0);
79949cdee0eSKees Lemmens 				}
80049cdee0eSKees Lemmens 			}
80149cdee0eSKees Lemmens 		} else {
80249cdee0eSKees Lemmens 			if (OTI6858_CTRL_EQUALS_PENDING(xs, priv)) {
80349cdee0eSKees Lemmens 				priv->transient = 0;
80449cdee0eSKees Lemmens 			} else if (!priv->setup_done) {
80549cdee0eSKees Lemmens 				resubmit = 0;
80649cdee0eSKees Lemmens 			} else if (--priv->transient == 0) {
80749cdee0eSKees Lemmens 				if (xs->rx_bytes_avail == 0) {
80849cdee0eSKees Lemmens 					priv->transient = 4;
80949cdee0eSKees Lemmens 					priv->setup_done = 0;
81049cdee0eSKees Lemmens 					resubmit = 0;
81149cdee0eSKees Lemmens 					dbg("%s(): scheduling setup_line()",
812441b62c1SHarvey Harrison 					    __func__);
81349cdee0eSKees Lemmens 					schedule_delayed_work(&priv->delayed_setup_work, 0);
81449cdee0eSKees Lemmens 				}
81549cdee0eSKees Lemmens 			}
81649cdee0eSKees Lemmens 		}
81749cdee0eSKees Lemmens 
81849cdee0eSKees Lemmens 		if (!priv->transient) {
81949cdee0eSKees Lemmens 			if (xs->pin_state != priv->status.pin_state)
82049cdee0eSKees Lemmens 				wake_up_interruptible(&priv->intr_wait);
82149cdee0eSKees Lemmens 			memcpy(&priv->status, xs, OTI6858_CTRL_PKT_SIZE);
82249cdee0eSKees Lemmens 		}
82349cdee0eSKees Lemmens 
82449cdee0eSKees Lemmens 		if (!priv->transient && xs->rx_bytes_avail != 0) {
82549cdee0eSKees Lemmens 			can_recv = xs->rx_bytes_avail;
82649cdee0eSKees Lemmens 			priv->flags.read_urb_in_use = 1;
82749cdee0eSKees Lemmens 		}
82849cdee0eSKees Lemmens 
82949cdee0eSKees Lemmens 		transient = priv->transient;
83049cdee0eSKees Lemmens 		spin_unlock_irqrestore(&priv->lock, flags);
83149cdee0eSKees Lemmens 	}
83249cdee0eSKees Lemmens 
83349cdee0eSKees Lemmens 	if (can_recv) {
83449cdee0eSKees Lemmens 		int result;
83549cdee0eSKees Lemmens 
83649cdee0eSKees Lemmens 		result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
83749cdee0eSKees Lemmens 		if (result != 0) {
83849cdee0eSKees Lemmens 			priv->flags.read_urb_in_use = 0;
83949cdee0eSKees Lemmens 			dev_err(&port->dev, "%s(): usb_submit_urb() failed,"
840441b62c1SHarvey Harrison 					" error %d\n", __func__, result);
84149cdee0eSKees Lemmens 		} else {
84249cdee0eSKees Lemmens 			resubmit = 0;
84349cdee0eSKees Lemmens 		}
84449cdee0eSKees Lemmens 	} else if (!transient) {
84549cdee0eSKees Lemmens 		unsigned long flags;
84628f27dcbSJohan Hovold 		int count;
84728f27dcbSJohan Hovold 
84828f27dcbSJohan Hovold 		spin_lock_irqsave(&port->lock, flags);
84928f27dcbSJohan Hovold 		count = kfifo_len(&port->write_fifo);
85028f27dcbSJohan Hovold 		spin_unlock_irqrestore(&port->lock, flags);
85149cdee0eSKees Lemmens 
85249cdee0eSKees Lemmens 		spin_lock_irqsave(&priv->lock, flags);
85328f27dcbSJohan Hovold 		if (priv->flags.write_urb_in_use == 0 && count != 0) {
85449cdee0eSKees Lemmens 			schedule_delayed_work(&priv->delayed_write_work, 0);
85549cdee0eSKees Lemmens 			resubmit = 0;
85649cdee0eSKees Lemmens 		}
85749cdee0eSKees Lemmens 		spin_unlock_irqrestore(&priv->lock, flags);
85849cdee0eSKees Lemmens 	}
85949cdee0eSKees Lemmens 
86049cdee0eSKees Lemmens 	if (resubmit) {
86149cdee0eSKees Lemmens 		int result;
86249cdee0eSKees Lemmens 
8632a77c814SAlan Cox /*		dbg("%s(): submitting interrupt urb", __func__); */
86449cdee0eSKees Lemmens 		result = usb_submit_urb(urb, GFP_ATOMIC);
86549cdee0eSKees Lemmens 		if (result != 0) {
86649cdee0eSKees Lemmens 			dev_err(&urb->dev->dev,
86749cdee0eSKees Lemmens 					"%s(): usb_submit_urb() failed with"
868441b62c1SHarvey Harrison 					" error %d\n", __func__, result);
86949cdee0eSKees Lemmens 		}
87049cdee0eSKees Lemmens 	}
87149cdee0eSKees Lemmens }
87249cdee0eSKees Lemmens 
87349cdee0eSKees Lemmens static void oti6858_read_bulk_callback(struct urb *urb)
87449cdee0eSKees Lemmens {
875cdc97792SMing Lei 	struct usb_serial_port *port =  urb->context;
87649cdee0eSKees Lemmens 	struct oti6858_private *priv = usb_get_serial_port_data(port);
87749cdee0eSKees Lemmens 	struct tty_struct *tty;
87849cdee0eSKees Lemmens 	unsigned char *data = urb->transfer_buffer;
87949cdee0eSKees Lemmens 	unsigned long flags;
88078c26aebSGreg Kroah-Hartman 	int status = urb->status;
881b0a239daSAlan Cox 	int result;
88249cdee0eSKees Lemmens 
88378c26aebSGreg Kroah-Hartman 	dbg("%s(port = %d, status = %d)",
884441b62c1SHarvey Harrison 				__func__, port->number, status);
88549cdee0eSKees Lemmens 
88649cdee0eSKees Lemmens 	spin_lock_irqsave(&priv->lock, flags);
88749cdee0eSKees Lemmens 	priv->flags.read_urb_in_use = 0;
88849cdee0eSKees Lemmens 	spin_unlock_irqrestore(&priv->lock, flags);
88949cdee0eSKees Lemmens 
89078c26aebSGreg Kroah-Hartman 	if (status != 0) {
891441b62c1SHarvey Harrison 		dbg("%s(): unable to handle the error, exiting", __func__);
89249cdee0eSKees Lemmens 		return;
89349cdee0eSKees Lemmens 	}
89449cdee0eSKees Lemmens 
8954a90f09bSAlan Cox 	tty = tty_port_tty_get(&port->port);
89649cdee0eSKees Lemmens 	if (tty != NULL && urb->actual_length > 0) {
897b0a239daSAlan Cox 		tty_insert_flip_string(tty, data, urb->actual_length);
89849cdee0eSKees Lemmens 		tty_flip_buffer_push(tty);
89949cdee0eSKees Lemmens 	}
9004a90f09bSAlan Cox 	tty_kref_put(tty);
90149cdee0eSKees Lemmens 
9021f87158eSAlan Stern 	/* schedule the interrupt urb */
90349cdee0eSKees Lemmens 	result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
9041f87158eSAlan Stern 	if (result != 0 && result != -EPERM) {
90549cdee0eSKees Lemmens 		dev_err(&port->dev, "%s(): usb_submit_urb() failed,"
906441b62c1SHarvey Harrison 				" error %d\n", __func__, result);
90749cdee0eSKees Lemmens 	}
90849cdee0eSKees Lemmens }
90949cdee0eSKees Lemmens 
91049cdee0eSKees Lemmens static void oti6858_write_bulk_callback(struct urb *urb)
91149cdee0eSKees Lemmens {
912cdc97792SMing Lei 	struct usb_serial_port *port =  urb->context;
91349cdee0eSKees Lemmens 	struct oti6858_private *priv = usb_get_serial_port_data(port);
91478c26aebSGreg Kroah-Hartman 	int status = urb->status;
91549cdee0eSKees Lemmens 	int result;
91649cdee0eSKees Lemmens 
91778c26aebSGreg Kroah-Hartman 	dbg("%s(port = %d, status = %d)",
918441b62c1SHarvey Harrison 				__func__, port->number, status);
91949cdee0eSKees Lemmens 
92078c26aebSGreg Kroah-Hartman 	switch (status) {
92149cdee0eSKees Lemmens 	case 0:
92249cdee0eSKees Lemmens 		/* success */
92349cdee0eSKees Lemmens 		break;
92449cdee0eSKees Lemmens 	case -ECONNRESET:
92549cdee0eSKees Lemmens 	case -ENOENT:
92649cdee0eSKees Lemmens 	case -ESHUTDOWN:
92749cdee0eSKees Lemmens 		/* this urb is terminated, clean up */
92849cdee0eSKees Lemmens 		dbg("%s(): urb shutting down with status: %d",
929441b62c1SHarvey Harrison 					__func__, status);
93049cdee0eSKees Lemmens 		priv->flags.write_urb_in_use = 0;
93149cdee0eSKees Lemmens 		return;
93249cdee0eSKees Lemmens 	default:
93349cdee0eSKees Lemmens 		/* error in the urb, so we have to resubmit it */
93449cdee0eSKees Lemmens 		dbg("%s(): nonzero write bulk status received: %d",
935441b62c1SHarvey Harrison 					__func__, status);
936441b62c1SHarvey Harrison 		dbg("%s(): overflow in write", __func__);
93749cdee0eSKees Lemmens 
93849cdee0eSKees Lemmens 		port->write_urb->transfer_buffer_length = 1;
93949cdee0eSKees Lemmens 		result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
94049cdee0eSKees Lemmens 		if (result) {
94149cdee0eSKees Lemmens 			dev_err(&port->dev, "%s(): usb_submit_urb() failed,"
942441b62c1SHarvey Harrison 					" error %d\n", __func__, result);
94349cdee0eSKees Lemmens 		} else {
94449cdee0eSKees Lemmens 			return;
94549cdee0eSKees Lemmens 		}
94649cdee0eSKees Lemmens 	}
94749cdee0eSKees Lemmens 
94849cdee0eSKees Lemmens 	priv->flags.write_urb_in_use = 0;
94949cdee0eSKees Lemmens 
9502a77c814SAlan Cox 	/* schedule the interrupt urb if we are still open */
951441b62c1SHarvey Harrison 	dbg("%s(): submitting interrupt urb", __func__);
95249cdee0eSKees Lemmens 	result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
95349cdee0eSKees Lemmens 	if (result != 0) {
95449cdee0eSKees Lemmens 		dev_err(&port->dev, "%s(): failed submitting int urb,"
955441b62c1SHarvey Harrison 					" error %d\n", __func__, result);
95649cdee0eSKees Lemmens 	}
95749cdee0eSKees Lemmens }
95849cdee0eSKees Lemmens 
95949cdee0eSKees Lemmens /* module description and (de)initialization */
96049cdee0eSKees Lemmens 
96149cdee0eSKees Lemmens static int __init oti6858_init(void)
96249cdee0eSKees Lemmens {
96349cdee0eSKees Lemmens 	int retval;
96449cdee0eSKees Lemmens 
9652a77c814SAlan Cox 	retval = usb_serial_register(&oti6858_device);
9662a77c814SAlan Cox 	if (retval == 0) {
9672a77c814SAlan Cox 		retval = usb_register(&oti6858_driver);
9682a77c814SAlan Cox 		if (retval)
96949cdee0eSKees Lemmens 			usb_serial_deregister(&oti6858_device);
97049cdee0eSKees Lemmens 	}
97149cdee0eSKees Lemmens 	return retval;
97249cdee0eSKees Lemmens }
97349cdee0eSKees Lemmens 
97449cdee0eSKees Lemmens static void __exit oti6858_exit(void)
97549cdee0eSKees Lemmens {
97649cdee0eSKees Lemmens 	usb_deregister(&oti6858_driver);
97749cdee0eSKees Lemmens 	usb_serial_deregister(&oti6858_device);
97849cdee0eSKees Lemmens }
97949cdee0eSKees Lemmens 
98049cdee0eSKees Lemmens module_init(oti6858_init);
98149cdee0eSKees Lemmens module_exit(oti6858_exit);
98249cdee0eSKees Lemmens 
98349cdee0eSKees Lemmens MODULE_DESCRIPTION(OTI6858_DESCRIPTION);
98449cdee0eSKees Lemmens MODULE_AUTHOR(OTI6858_AUTHOR);
98549cdee0eSKees Lemmens MODULE_VERSION(OTI6858_VERSION);
98649cdee0eSKees Lemmens MODULE_LICENSE("GPL");
98749cdee0eSKees Lemmens 
98849cdee0eSKees Lemmens module_param(debug, bool, S_IRUGO | S_IWUSR);
98949cdee0eSKees Lemmens MODULE_PARM_DESC(debug, "enable debug output");
99049cdee0eSKees Lemmens 
991