162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * spcp8x5 USB to serial adaptor driver
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Copyright (C) 2010-2013 Johan Hovold (jhovold@gmail.com)
662306a36Sopenharmony_ci * Copyright (C) 2006 Linxb (xubin.lin@worldplus.com.cn)
762306a36Sopenharmony_ci * Copyright (C) 2006 S1 Corp.
862306a36Sopenharmony_ci *
962306a36Sopenharmony_ci * Original driver for 2.6.10 pl2303 driver by
1062306a36Sopenharmony_ci *   Greg Kroah-Hartman (greg@kroah.com)
1162306a36Sopenharmony_ci * Changes for 2.6.20 by Harald Klein <hari@vt100.at>
1262306a36Sopenharmony_ci */
1362306a36Sopenharmony_ci#include <linux/kernel.h>
1462306a36Sopenharmony_ci#include <linux/errno.h>
1562306a36Sopenharmony_ci#include <linux/slab.h>
1662306a36Sopenharmony_ci#include <linux/tty.h>
1762306a36Sopenharmony_ci#include <linux/tty_driver.h>
1862306a36Sopenharmony_ci#include <linux/tty_flip.h>
1962306a36Sopenharmony_ci#include <linux/module.h>
2062306a36Sopenharmony_ci#include <linux/spinlock.h>
2162306a36Sopenharmony_ci#include <linux/usb.h>
2262306a36Sopenharmony_ci#include <linux/usb/serial.h>
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_ci#define DRIVER_DESC	"SPCP8x5 USB to serial adaptor driver"
2562306a36Sopenharmony_ci
2662306a36Sopenharmony_ci#define SPCP825_QUIRK_NO_UART_STATUS	0x01
2762306a36Sopenharmony_ci#define SPCP825_QUIRK_NO_WORK_MODE	0x02
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_ci#define SPCP8x5_007_VID		0x04FC
3062306a36Sopenharmony_ci#define SPCP8x5_007_PID		0x0201
3162306a36Sopenharmony_ci#define SPCP8x5_008_VID		0x04fc
3262306a36Sopenharmony_ci#define SPCP8x5_008_PID		0x0235
3362306a36Sopenharmony_ci#define SPCP8x5_PHILIPS_VID	0x0471
3462306a36Sopenharmony_ci#define SPCP8x5_PHILIPS_PID	0x081e
3562306a36Sopenharmony_ci#define SPCP8x5_INTERMATIC_VID	0x04FC
3662306a36Sopenharmony_ci#define SPCP8x5_INTERMATIC_PID	0x0204
3762306a36Sopenharmony_ci#define SPCP8x5_835_VID		0x04fc
3862306a36Sopenharmony_ci#define SPCP8x5_835_PID		0x0231
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_cistatic const struct usb_device_id id_table[] = {
4162306a36Sopenharmony_ci	{ USB_DEVICE(SPCP8x5_PHILIPS_VID , SPCP8x5_PHILIPS_PID)},
4262306a36Sopenharmony_ci	{ USB_DEVICE(SPCP8x5_INTERMATIC_VID, SPCP8x5_INTERMATIC_PID)},
4362306a36Sopenharmony_ci	{ USB_DEVICE(SPCP8x5_835_VID, SPCP8x5_835_PID)},
4462306a36Sopenharmony_ci	{ USB_DEVICE(SPCP8x5_008_VID, SPCP8x5_008_PID)},
4562306a36Sopenharmony_ci	{ USB_DEVICE(SPCP8x5_007_VID, SPCP8x5_007_PID),
4662306a36Sopenharmony_ci	  .driver_info = SPCP825_QUIRK_NO_UART_STATUS |
4762306a36Sopenharmony_ci				SPCP825_QUIRK_NO_WORK_MODE },
4862306a36Sopenharmony_ci	{ }					/* Terminating entry */
4962306a36Sopenharmony_ci};
5062306a36Sopenharmony_ciMODULE_DEVICE_TABLE(usb, id_table);
5162306a36Sopenharmony_ci
5262306a36Sopenharmony_cistruct spcp8x5_usb_ctrl_arg {
5362306a36Sopenharmony_ci	u8	type;
5462306a36Sopenharmony_ci	u8	cmd;
5562306a36Sopenharmony_ci	u8	cmd_type;
5662306a36Sopenharmony_ci	u16	value;
5762306a36Sopenharmony_ci	u16	index;
5862306a36Sopenharmony_ci	u16	length;
5962306a36Sopenharmony_ci};
6062306a36Sopenharmony_ci
6162306a36Sopenharmony_ci
6262306a36Sopenharmony_ci/* spcp8x5 spec register define */
6362306a36Sopenharmony_ci#define MCR_CONTROL_LINE_RTS		0x02
6462306a36Sopenharmony_ci#define MCR_CONTROL_LINE_DTR		0x01
6562306a36Sopenharmony_ci#define MCR_DTR				0x01
6662306a36Sopenharmony_ci#define MCR_RTS				0x02
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_ci#define MSR_STATUS_LINE_DCD		0x80
6962306a36Sopenharmony_ci#define MSR_STATUS_LINE_RI		0x40
7062306a36Sopenharmony_ci#define MSR_STATUS_LINE_DSR		0x20
7162306a36Sopenharmony_ci#define MSR_STATUS_LINE_CTS		0x10
7262306a36Sopenharmony_ci
7362306a36Sopenharmony_ci/* verdor command here , we should define myself */
7462306a36Sopenharmony_ci#define SET_DEFAULT			0x40
7562306a36Sopenharmony_ci#define SET_DEFAULT_TYPE		0x20
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ci#define SET_UART_FORMAT			0x40
7862306a36Sopenharmony_ci#define SET_UART_FORMAT_TYPE		0x21
7962306a36Sopenharmony_ci#define SET_UART_FORMAT_SIZE_5		0x00
8062306a36Sopenharmony_ci#define SET_UART_FORMAT_SIZE_6		0x01
8162306a36Sopenharmony_ci#define SET_UART_FORMAT_SIZE_7		0x02
8262306a36Sopenharmony_ci#define SET_UART_FORMAT_SIZE_8		0x03
8362306a36Sopenharmony_ci#define SET_UART_FORMAT_STOP_1		0x00
8462306a36Sopenharmony_ci#define SET_UART_FORMAT_STOP_2		0x04
8562306a36Sopenharmony_ci#define SET_UART_FORMAT_PAR_NONE	0x00
8662306a36Sopenharmony_ci#define SET_UART_FORMAT_PAR_ODD		0x10
8762306a36Sopenharmony_ci#define SET_UART_FORMAT_PAR_EVEN	0x30
8862306a36Sopenharmony_ci#define SET_UART_FORMAT_PAR_MASK	0xD0
8962306a36Sopenharmony_ci#define SET_UART_FORMAT_PAR_SPACE	0x90
9062306a36Sopenharmony_ci
9162306a36Sopenharmony_ci#define GET_UART_STATUS_TYPE		0xc0
9262306a36Sopenharmony_ci#define GET_UART_STATUS			0x22
9362306a36Sopenharmony_ci#define GET_UART_STATUS_MSR		0x06
9462306a36Sopenharmony_ci
9562306a36Sopenharmony_ci#define SET_UART_STATUS			0x40
9662306a36Sopenharmony_ci#define SET_UART_STATUS_TYPE		0x23
9762306a36Sopenharmony_ci#define SET_UART_STATUS_MCR		0x0004
9862306a36Sopenharmony_ci#define SET_UART_STATUS_MCR_DTR		0x01
9962306a36Sopenharmony_ci#define SET_UART_STATUS_MCR_RTS		0x02
10062306a36Sopenharmony_ci#define SET_UART_STATUS_MCR_LOOP	0x10
10162306a36Sopenharmony_ci
10262306a36Sopenharmony_ci#define SET_WORKING_MODE		0x40
10362306a36Sopenharmony_ci#define SET_WORKING_MODE_TYPE		0x24
10462306a36Sopenharmony_ci#define SET_WORKING_MODE_U2C		0x00
10562306a36Sopenharmony_ci#define SET_WORKING_MODE_RS485		0x01
10662306a36Sopenharmony_ci#define SET_WORKING_MODE_PDMA		0x02
10762306a36Sopenharmony_ci#define SET_WORKING_MODE_SPP		0x03
10862306a36Sopenharmony_ci
10962306a36Sopenharmony_ci#define SET_FLOWCTL_CHAR		0x40
11062306a36Sopenharmony_ci#define SET_FLOWCTL_CHAR_TYPE		0x25
11162306a36Sopenharmony_ci
11262306a36Sopenharmony_ci#define GET_VERSION			0xc0
11362306a36Sopenharmony_ci#define GET_VERSION_TYPE		0x26
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_ci#define SET_REGISTER			0x40
11662306a36Sopenharmony_ci#define SET_REGISTER_TYPE		0x27
11762306a36Sopenharmony_ci
11862306a36Sopenharmony_ci#define	GET_REGISTER			0xc0
11962306a36Sopenharmony_ci#define GET_REGISTER_TYPE		0x28
12062306a36Sopenharmony_ci
12162306a36Sopenharmony_ci#define SET_RAM				0x40
12262306a36Sopenharmony_ci#define SET_RAM_TYPE			0x31
12362306a36Sopenharmony_ci
12462306a36Sopenharmony_ci#define GET_RAM				0xc0
12562306a36Sopenharmony_ci#define GET_RAM_TYPE			0x32
12662306a36Sopenharmony_ci
12762306a36Sopenharmony_ci/* how come ??? */
12862306a36Sopenharmony_ci#define UART_STATE			0x08
12962306a36Sopenharmony_ci#define UART_STATE_TRANSIENT_MASK	0x75
13062306a36Sopenharmony_ci#define UART_DCD			0x01
13162306a36Sopenharmony_ci#define UART_DSR			0x02
13262306a36Sopenharmony_ci#define UART_BREAK_ERROR		0x04
13362306a36Sopenharmony_ci#define UART_RING			0x08
13462306a36Sopenharmony_ci#define UART_FRAME_ERROR		0x10
13562306a36Sopenharmony_ci#define UART_PARITY_ERROR		0x20
13662306a36Sopenharmony_ci#define UART_OVERRUN_ERROR		0x40
13762306a36Sopenharmony_ci#define UART_CTS			0x80
13862306a36Sopenharmony_ci
13962306a36Sopenharmony_cistruct spcp8x5_private {
14062306a36Sopenharmony_ci	unsigned		quirks;
14162306a36Sopenharmony_ci	spinlock_t		lock;
14262306a36Sopenharmony_ci	u8			line_control;
14362306a36Sopenharmony_ci};
14462306a36Sopenharmony_ci
14562306a36Sopenharmony_cistatic int spcp8x5_probe(struct usb_serial *serial,
14662306a36Sopenharmony_ci						const struct usb_device_id *id)
14762306a36Sopenharmony_ci{
14862306a36Sopenharmony_ci	usb_set_serial_data(serial, (void *)id);
14962306a36Sopenharmony_ci
15062306a36Sopenharmony_ci	return 0;
15162306a36Sopenharmony_ci}
15262306a36Sopenharmony_ci
15362306a36Sopenharmony_cistatic int spcp8x5_port_probe(struct usb_serial_port *port)
15462306a36Sopenharmony_ci{
15562306a36Sopenharmony_ci	const struct usb_device_id *id = usb_get_serial_data(port->serial);
15662306a36Sopenharmony_ci	struct spcp8x5_private *priv;
15762306a36Sopenharmony_ci
15862306a36Sopenharmony_ci	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
15962306a36Sopenharmony_ci	if (!priv)
16062306a36Sopenharmony_ci		return -ENOMEM;
16162306a36Sopenharmony_ci
16262306a36Sopenharmony_ci	spin_lock_init(&priv->lock);
16362306a36Sopenharmony_ci	priv->quirks = id->driver_info;
16462306a36Sopenharmony_ci
16562306a36Sopenharmony_ci	usb_set_serial_port_data(port, priv);
16662306a36Sopenharmony_ci
16762306a36Sopenharmony_ci	port->port.drain_delay = 256;
16862306a36Sopenharmony_ci
16962306a36Sopenharmony_ci	return 0;
17062306a36Sopenharmony_ci}
17162306a36Sopenharmony_ci
17262306a36Sopenharmony_cistatic void spcp8x5_port_remove(struct usb_serial_port *port)
17362306a36Sopenharmony_ci{
17462306a36Sopenharmony_ci	struct spcp8x5_private *priv;
17562306a36Sopenharmony_ci
17662306a36Sopenharmony_ci	priv = usb_get_serial_port_data(port);
17762306a36Sopenharmony_ci	kfree(priv);
17862306a36Sopenharmony_ci}
17962306a36Sopenharmony_ci
18062306a36Sopenharmony_cistatic int spcp8x5_set_ctrl_line(struct usb_serial_port *port, u8 mcr)
18162306a36Sopenharmony_ci{
18262306a36Sopenharmony_ci	struct spcp8x5_private *priv = usb_get_serial_port_data(port);
18362306a36Sopenharmony_ci	struct usb_device *dev = port->serial->dev;
18462306a36Sopenharmony_ci	int retval;
18562306a36Sopenharmony_ci
18662306a36Sopenharmony_ci	if (priv->quirks & SPCP825_QUIRK_NO_UART_STATUS)
18762306a36Sopenharmony_ci		return -EPERM;
18862306a36Sopenharmony_ci
18962306a36Sopenharmony_ci	retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
19062306a36Sopenharmony_ci				 SET_UART_STATUS_TYPE, SET_UART_STATUS,
19162306a36Sopenharmony_ci				 mcr, 0x04, NULL, 0, 100);
19262306a36Sopenharmony_ci	if (retval != 0) {
19362306a36Sopenharmony_ci		dev_err(&port->dev, "failed to set control lines: %d\n",
19462306a36Sopenharmony_ci								retval);
19562306a36Sopenharmony_ci	}
19662306a36Sopenharmony_ci	return retval;
19762306a36Sopenharmony_ci}
19862306a36Sopenharmony_ci
19962306a36Sopenharmony_cistatic int spcp8x5_get_msr(struct usb_serial_port *port, u8 *status)
20062306a36Sopenharmony_ci{
20162306a36Sopenharmony_ci	struct spcp8x5_private *priv = usb_get_serial_port_data(port);
20262306a36Sopenharmony_ci	struct usb_device *dev = port->serial->dev;
20362306a36Sopenharmony_ci	u8 *buf;
20462306a36Sopenharmony_ci	int ret;
20562306a36Sopenharmony_ci
20662306a36Sopenharmony_ci	if (priv->quirks & SPCP825_QUIRK_NO_UART_STATUS)
20762306a36Sopenharmony_ci		return -EPERM;
20862306a36Sopenharmony_ci
20962306a36Sopenharmony_ci	buf = kzalloc(1, GFP_KERNEL);
21062306a36Sopenharmony_ci	if (!buf)
21162306a36Sopenharmony_ci		return -ENOMEM;
21262306a36Sopenharmony_ci
21362306a36Sopenharmony_ci	ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
21462306a36Sopenharmony_ci			      GET_UART_STATUS, GET_UART_STATUS_TYPE,
21562306a36Sopenharmony_ci			      0, GET_UART_STATUS_MSR, buf, 1, 100);
21662306a36Sopenharmony_ci	if (ret < 1) {
21762306a36Sopenharmony_ci		dev_err(&port->dev, "failed to get modem status: %d\n", ret);
21862306a36Sopenharmony_ci		if (ret >= 0)
21962306a36Sopenharmony_ci			ret = -EIO;
22062306a36Sopenharmony_ci		goto out;
22162306a36Sopenharmony_ci	}
22262306a36Sopenharmony_ci
22362306a36Sopenharmony_ci	dev_dbg(&port->dev, "0xc0:0x22:0:6  %d - 0x02%x\n", ret, *buf);
22462306a36Sopenharmony_ci	*status = *buf;
22562306a36Sopenharmony_ci	ret = 0;
22662306a36Sopenharmony_ciout:
22762306a36Sopenharmony_ci	kfree(buf);
22862306a36Sopenharmony_ci
22962306a36Sopenharmony_ci	return ret;
23062306a36Sopenharmony_ci}
23162306a36Sopenharmony_ci
23262306a36Sopenharmony_cistatic void spcp8x5_set_work_mode(struct usb_serial_port *port, u16 value,
23362306a36Sopenharmony_ci								 u16 index)
23462306a36Sopenharmony_ci{
23562306a36Sopenharmony_ci	struct spcp8x5_private *priv = usb_get_serial_port_data(port);
23662306a36Sopenharmony_ci	struct usb_device *dev = port->serial->dev;
23762306a36Sopenharmony_ci	int ret;
23862306a36Sopenharmony_ci
23962306a36Sopenharmony_ci	if (priv->quirks & SPCP825_QUIRK_NO_WORK_MODE)
24062306a36Sopenharmony_ci		return;
24162306a36Sopenharmony_ci
24262306a36Sopenharmony_ci	ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
24362306a36Sopenharmony_ci			      SET_WORKING_MODE_TYPE, SET_WORKING_MODE,
24462306a36Sopenharmony_ci			      value, index, NULL, 0, 100);
24562306a36Sopenharmony_ci	dev_dbg(&port->dev, "value = %#x , index = %#x\n", value, index);
24662306a36Sopenharmony_ci	if (ret < 0)
24762306a36Sopenharmony_ci		dev_err(&port->dev, "failed to set work mode: %d\n", ret);
24862306a36Sopenharmony_ci}
24962306a36Sopenharmony_ci
25062306a36Sopenharmony_cistatic int spcp8x5_carrier_raised(struct usb_serial_port *port)
25162306a36Sopenharmony_ci{
25262306a36Sopenharmony_ci	u8 msr;
25362306a36Sopenharmony_ci	int ret;
25462306a36Sopenharmony_ci
25562306a36Sopenharmony_ci	ret = spcp8x5_get_msr(port, &msr);
25662306a36Sopenharmony_ci	if (ret || msr & MSR_STATUS_LINE_DCD)
25762306a36Sopenharmony_ci		return 1;
25862306a36Sopenharmony_ci
25962306a36Sopenharmony_ci	return 0;
26062306a36Sopenharmony_ci}
26162306a36Sopenharmony_ci
26262306a36Sopenharmony_cistatic void spcp8x5_dtr_rts(struct usb_serial_port *port, int on)
26362306a36Sopenharmony_ci{
26462306a36Sopenharmony_ci	struct spcp8x5_private *priv = usb_get_serial_port_data(port);
26562306a36Sopenharmony_ci	unsigned long flags;
26662306a36Sopenharmony_ci	u8 control;
26762306a36Sopenharmony_ci
26862306a36Sopenharmony_ci	spin_lock_irqsave(&priv->lock, flags);
26962306a36Sopenharmony_ci	if (on)
27062306a36Sopenharmony_ci		priv->line_control = MCR_CONTROL_LINE_DTR
27162306a36Sopenharmony_ci						| MCR_CONTROL_LINE_RTS;
27262306a36Sopenharmony_ci	else
27362306a36Sopenharmony_ci		priv->line_control &= ~ (MCR_CONTROL_LINE_DTR
27462306a36Sopenharmony_ci						| MCR_CONTROL_LINE_RTS);
27562306a36Sopenharmony_ci	control = priv->line_control;
27662306a36Sopenharmony_ci	spin_unlock_irqrestore(&priv->lock, flags);
27762306a36Sopenharmony_ci	spcp8x5_set_ctrl_line(port, control);
27862306a36Sopenharmony_ci}
27962306a36Sopenharmony_ci
28062306a36Sopenharmony_cistatic void spcp8x5_init_termios(struct tty_struct *tty)
28162306a36Sopenharmony_ci{
28262306a36Sopenharmony_ci	tty_encode_baud_rate(tty, 115200, 115200);
28362306a36Sopenharmony_ci}
28462306a36Sopenharmony_ci
28562306a36Sopenharmony_cistatic void spcp8x5_set_termios(struct tty_struct *tty,
28662306a36Sopenharmony_ci				struct usb_serial_port *port,
28762306a36Sopenharmony_ci				const struct ktermios *old_termios)
28862306a36Sopenharmony_ci{
28962306a36Sopenharmony_ci	struct usb_serial *serial = port->serial;
29062306a36Sopenharmony_ci	struct spcp8x5_private *priv = usb_get_serial_port_data(port);
29162306a36Sopenharmony_ci	unsigned long flags;
29262306a36Sopenharmony_ci	unsigned int cflag = tty->termios.c_cflag;
29362306a36Sopenharmony_ci	unsigned short uartdata;
29462306a36Sopenharmony_ci	unsigned char buf[2] = {0, 0};
29562306a36Sopenharmony_ci	int baud;
29662306a36Sopenharmony_ci	int i;
29762306a36Sopenharmony_ci	u8 control;
29862306a36Sopenharmony_ci
29962306a36Sopenharmony_ci	/* check that they really want us to change something */
30062306a36Sopenharmony_ci	if (old_termios && !tty_termios_hw_change(&tty->termios, old_termios))
30162306a36Sopenharmony_ci		return;
30262306a36Sopenharmony_ci
30362306a36Sopenharmony_ci	/* set DTR/RTS active */
30462306a36Sopenharmony_ci	spin_lock_irqsave(&priv->lock, flags);
30562306a36Sopenharmony_ci	control = priv->line_control;
30662306a36Sopenharmony_ci	if (old_termios && (old_termios->c_cflag & CBAUD) == B0) {
30762306a36Sopenharmony_ci		priv->line_control |= MCR_DTR;
30862306a36Sopenharmony_ci		if (!(old_termios->c_cflag & CRTSCTS))
30962306a36Sopenharmony_ci			priv->line_control |= MCR_RTS;
31062306a36Sopenharmony_ci	}
31162306a36Sopenharmony_ci	if (control != priv->line_control) {
31262306a36Sopenharmony_ci		control = priv->line_control;
31362306a36Sopenharmony_ci		spin_unlock_irqrestore(&priv->lock, flags);
31462306a36Sopenharmony_ci		spcp8x5_set_ctrl_line(port, control);
31562306a36Sopenharmony_ci	} else {
31662306a36Sopenharmony_ci		spin_unlock_irqrestore(&priv->lock, flags);
31762306a36Sopenharmony_ci	}
31862306a36Sopenharmony_ci
31962306a36Sopenharmony_ci	/* Set Baud Rate */
32062306a36Sopenharmony_ci	baud = tty_get_baud_rate(tty);
32162306a36Sopenharmony_ci	switch (baud) {
32262306a36Sopenharmony_ci	case 300:	buf[0] = 0x00;	break;
32362306a36Sopenharmony_ci	case 600:	buf[0] = 0x01;	break;
32462306a36Sopenharmony_ci	case 1200:	buf[0] = 0x02;	break;
32562306a36Sopenharmony_ci	case 2400:	buf[0] = 0x03;	break;
32662306a36Sopenharmony_ci	case 4800:	buf[0] = 0x04;	break;
32762306a36Sopenharmony_ci	case 9600:	buf[0] = 0x05;	break;
32862306a36Sopenharmony_ci	case 19200:	buf[0] = 0x07;	break;
32962306a36Sopenharmony_ci	case 38400:	buf[0] = 0x09;	break;
33062306a36Sopenharmony_ci	case 57600:	buf[0] = 0x0a;	break;
33162306a36Sopenharmony_ci	case 115200:	buf[0] = 0x0b;	break;
33262306a36Sopenharmony_ci	case 230400:	buf[0] = 0x0c;	break;
33362306a36Sopenharmony_ci	case 460800:	buf[0] = 0x0d;	break;
33462306a36Sopenharmony_ci	case 921600:	buf[0] = 0x0e;	break;
33562306a36Sopenharmony_ci/*	case 1200000:	buf[0] = 0x0f;	break; */
33662306a36Sopenharmony_ci/*	case 2400000:	buf[0] = 0x10;	break; */
33762306a36Sopenharmony_ci	case 3000000:	buf[0] = 0x11;	break;
33862306a36Sopenharmony_ci/*	case 6000000:	buf[0] = 0x12;	break; */
33962306a36Sopenharmony_ci	case 0:
34062306a36Sopenharmony_ci	case 1000000:
34162306a36Sopenharmony_ci			buf[0] = 0x0b;	break;
34262306a36Sopenharmony_ci	default:
34362306a36Sopenharmony_ci		dev_err(&port->dev, "unsupported baudrate, using 9600\n");
34462306a36Sopenharmony_ci	}
34562306a36Sopenharmony_ci
34662306a36Sopenharmony_ci	/* Set Data Length : 00:5bit, 01:6bit, 10:7bit, 11:8bit */
34762306a36Sopenharmony_ci	switch (cflag & CSIZE) {
34862306a36Sopenharmony_ci	case CS5:
34962306a36Sopenharmony_ci		buf[1] |= SET_UART_FORMAT_SIZE_5;
35062306a36Sopenharmony_ci		break;
35162306a36Sopenharmony_ci	case CS6:
35262306a36Sopenharmony_ci		buf[1] |= SET_UART_FORMAT_SIZE_6;
35362306a36Sopenharmony_ci		break;
35462306a36Sopenharmony_ci	case CS7:
35562306a36Sopenharmony_ci		buf[1] |= SET_UART_FORMAT_SIZE_7;
35662306a36Sopenharmony_ci		break;
35762306a36Sopenharmony_ci	default:
35862306a36Sopenharmony_ci	case CS8:
35962306a36Sopenharmony_ci		buf[1] |= SET_UART_FORMAT_SIZE_8;
36062306a36Sopenharmony_ci		break;
36162306a36Sopenharmony_ci	}
36262306a36Sopenharmony_ci
36362306a36Sopenharmony_ci	/* Set Stop bit2 : 0:1bit 1:2bit */
36462306a36Sopenharmony_ci	buf[1] |= (cflag & CSTOPB) ? SET_UART_FORMAT_STOP_2 :
36562306a36Sopenharmony_ci				     SET_UART_FORMAT_STOP_1;
36662306a36Sopenharmony_ci
36762306a36Sopenharmony_ci	/* Set Parity bit3-4 01:Odd 11:Even */
36862306a36Sopenharmony_ci	if (cflag & PARENB) {
36962306a36Sopenharmony_ci		buf[1] |= (cflag & PARODD) ?
37062306a36Sopenharmony_ci		SET_UART_FORMAT_PAR_ODD : SET_UART_FORMAT_PAR_EVEN ;
37162306a36Sopenharmony_ci	} else {
37262306a36Sopenharmony_ci		buf[1] |= SET_UART_FORMAT_PAR_NONE;
37362306a36Sopenharmony_ci	}
37462306a36Sopenharmony_ci	uartdata = buf[0] | buf[1]<<8;
37562306a36Sopenharmony_ci
37662306a36Sopenharmony_ci	i = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
37762306a36Sopenharmony_ci			    SET_UART_FORMAT_TYPE, SET_UART_FORMAT,
37862306a36Sopenharmony_ci			    uartdata, 0, NULL, 0, 100);
37962306a36Sopenharmony_ci	if (i < 0)
38062306a36Sopenharmony_ci		dev_err(&port->dev, "Set UART format %#x failed (error = %d)\n",
38162306a36Sopenharmony_ci			uartdata, i);
38262306a36Sopenharmony_ci	dev_dbg(&port->dev, "0x21:0x40:0:0  %d\n", i);
38362306a36Sopenharmony_ci
38462306a36Sopenharmony_ci	if (cflag & CRTSCTS) {
38562306a36Sopenharmony_ci		/* enable hardware flow control */
38662306a36Sopenharmony_ci		spcp8x5_set_work_mode(port, 0x000a, SET_WORKING_MODE_U2C);
38762306a36Sopenharmony_ci	}
38862306a36Sopenharmony_ci}
38962306a36Sopenharmony_ci
39062306a36Sopenharmony_cistatic int spcp8x5_open(struct tty_struct *tty, struct usb_serial_port *port)
39162306a36Sopenharmony_ci{
39262306a36Sopenharmony_ci	struct usb_serial *serial = port->serial;
39362306a36Sopenharmony_ci	struct spcp8x5_private *priv = usb_get_serial_port_data(port);
39462306a36Sopenharmony_ci	int ret;
39562306a36Sopenharmony_ci
39662306a36Sopenharmony_ci	usb_clear_halt(serial->dev, port->write_urb->pipe);
39762306a36Sopenharmony_ci	usb_clear_halt(serial->dev, port->read_urb->pipe);
39862306a36Sopenharmony_ci
39962306a36Sopenharmony_ci	ret = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
40062306a36Sopenharmony_ci			      0x09, 0x00,
40162306a36Sopenharmony_ci			      0x01, 0x00, NULL, 0x00, 100);
40262306a36Sopenharmony_ci	if (ret)
40362306a36Sopenharmony_ci		return ret;
40462306a36Sopenharmony_ci
40562306a36Sopenharmony_ci	spcp8x5_set_ctrl_line(port, priv->line_control);
40662306a36Sopenharmony_ci
40762306a36Sopenharmony_ci	if (tty)
40862306a36Sopenharmony_ci		spcp8x5_set_termios(tty, port, NULL);
40962306a36Sopenharmony_ci
41062306a36Sopenharmony_ci	return usb_serial_generic_open(tty, port);
41162306a36Sopenharmony_ci}
41262306a36Sopenharmony_ci
41362306a36Sopenharmony_cistatic int spcp8x5_tiocmset(struct tty_struct *tty,
41462306a36Sopenharmony_ci			    unsigned int set, unsigned int clear)
41562306a36Sopenharmony_ci{
41662306a36Sopenharmony_ci	struct usb_serial_port *port = tty->driver_data;
41762306a36Sopenharmony_ci	struct spcp8x5_private *priv = usb_get_serial_port_data(port);
41862306a36Sopenharmony_ci	unsigned long flags;
41962306a36Sopenharmony_ci	u8 control;
42062306a36Sopenharmony_ci
42162306a36Sopenharmony_ci	spin_lock_irqsave(&priv->lock, flags);
42262306a36Sopenharmony_ci	if (set & TIOCM_RTS)
42362306a36Sopenharmony_ci		priv->line_control |= MCR_RTS;
42462306a36Sopenharmony_ci	if (set & TIOCM_DTR)
42562306a36Sopenharmony_ci		priv->line_control |= MCR_DTR;
42662306a36Sopenharmony_ci	if (clear & TIOCM_RTS)
42762306a36Sopenharmony_ci		priv->line_control &= ~MCR_RTS;
42862306a36Sopenharmony_ci	if (clear & TIOCM_DTR)
42962306a36Sopenharmony_ci		priv->line_control &= ~MCR_DTR;
43062306a36Sopenharmony_ci	control = priv->line_control;
43162306a36Sopenharmony_ci	spin_unlock_irqrestore(&priv->lock, flags);
43262306a36Sopenharmony_ci
43362306a36Sopenharmony_ci	return spcp8x5_set_ctrl_line(port, control);
43462306a36Sopenharmony_ci}
43562306a36Sopenharmony_ci
43662306a36Sopenharmony_cistatic int spcp8x5_tiocmget(struct tty_struct *tty)
43762306a36Sopenharmony_ci{
43862306a36Sopenharmony_ci	struct usb_serial_port *port = tty->driver_data;
43962306a36Sopenharmony_ci	struct spcp8x5_private *priv = usb_get_serial_port_data(port);
44062306a36Sopenharmony_ci	unsigned long flags;
44162306a36Sopenharmony_ci	unsigned int mcr;
44262306a36Sopenharmony_ci	u8 status;
44362306a36Sopenharmony_ci	unsigned int result;
44462306a36Sopenharmony_ci
44562306a36Sopenharmony_ci	result = spcp8x5_get_msr(port, &status);
44662306a36Sopenharmony_ci	if (result)
44762306a36Sopenharmony_ci		return result;
44862306a36Sopenharmony_ci
44962306a36Sopenharmony_ci	spin_lock_irqsave(&priv->lock, flags);
45062306a36Sopenharmony_ci	mcr = priv->line_control;
45162306a36Sopenharmony_ci	spin_unlock_irqrestore(&priv->lock, flags);
45262306a36Sopenharmony_ci
45362306a36Sopenharmony_ci	result = ((mcr & MCR_DTR)			? TIOCM_DTR : 0)
45462306a36Sopenharmony_ci		  | ((mcr & MCR_RTS)			? TIOCM_RTS : 0)
45562306a36Sopenharmony_ci		  | ((status & MSR_STATUS_LINE_CTS)	? TIOCM_CTS : 0)
45662306a36Sopenharmony_ci		  | ((status & MSR_STATUS_LINE_DSR)	? TIOCM_DSR : 0)
45762306a36Sopenharmony_ci		  | ((status & MSR_STATUS_LINE_RI)	? TIOCM_RI  : 0)
45862306a36Sopenharmony_ci		  | ((status & MSR_STATUS_LINE_DCD)	? TIOCM_CD  : 0);
45962306a36Sopenharmony_ci
46062306a36Sopenharmony_ci	return result;
46162306a36Sopenharmony_ci}
46262306a36Sopenharmony_ci
46362306a36Sopenharmony_cistatic struct usb_serial_driver spcp8x5_device = {
46462306a36Sopenharmony_ci	.driver = {
46562306a36Sopenharmony_ci		.owner =	THIS_MODULE,
46662306a36Sopenharmony_ci		.name =		"SPCP8x5",
46762306a36Sopenharmony_ci	},
46862306a36Sopenharmony_ci	.id_table		= id_table,
46962306a36Sopenharmony_ci	.num_ports		= 1,
47062306a36Sopenharmony_ci	.num_bulk_in		= 1,
47162306a36Sopenharmony_ci	.num_bulk_out		= 1,
47262306a36Sopenharmony_ci	.open			= spcp8x5_open,
47362306a36Sopenharmony_ci	.dtr_rts		= spcp8x5_dtr_rts,
47462306a36Sopenharmony_ci	.carrier_raised		= spcp8x5_carrier_raised,
47562306a36Sopenharmony_ci	.set_termios		= spcp8x5_set_termios,
47662306a36Sopenharmony_ci	.init_termios		= spcp8x5_init_termios,
47762306a36Sopenharmony_ci	.tiocmget		= spcp8x5_tiocmget,
47862306a36Sopenharmony_ci	.tiocmset		= spcp8x5_tiocmset,
47962306a36Sopenharmony_ci	.probe			= spcp8x5_probe,
48062306a36Sopenharmony_ci	.port_probe		= spcp8x5_port_probe,
48162306a36Sopenharmony_ci	.port_remove		= spcp8x5_port_remove,
48262306a36Sopenharmony_ci};
48362306a36Sopenharmony_ci
48462306a36Sopenharmony_cistatic struct usb_serial_driver * const serial_drivers[] = {
48562306a36Sopenharmony_ci	&spcp8x5_device, NULL
48662306a36Sopenharmony_ci};
48762306a36Sopenharmony_ci
48862306a36Sopenharmony_cimodule_usb_serial_driver(serial_drivers, id_table);
48962306a36Sopenharmony_ci
49062306a36Sopenharmony_ciMODULE_DESCRIPTION(DRIVER_DESC);
49162306a36Sopenharmony_ciMODULE_LICENSE("GPL");
492