xref: /openbmc/linux/drivers/usb/serial/ssu100.c (revision eb0c68ea4246252ba56951c6cf5e5d544a342e9e)
15fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
252af9545SBill Pemberton /*
352af9545SBill Pemberton  * usb-serial driver for Quatech SSU-100
452af9545SBill Pemberton  *
552af9545SBill Pemberton  * based on ftdi_sio.c and the original serqt_usb.c from Quatech
652af9545SBill Pemberton  *
752af9545SBill Pemberton  */
852af9545SBill Pemberton 
952af9545SBill Pemberton #include <linux/errno.h>
1052af9545SBill Pemberton #include <linux/slab.h>
1152af9545SBill Pemberton #include <linux/tty.h>
1252af9545SBill Pemberton #include <linux/tty_driver.h>
1352af9545SBill Pemberton #include <linux/tty_flip.h>
1452af9545SBill Pemberton #include <linux/module.h>
1552af9545SBill Pemberton #include <linux/serial.h>
1652af9545SBill Pemberton #include <linux/usb.h>
1752af9545SBill Pemberton #include <linux/usb/serial.h>
1879f203a2SBill Pemberton #include <linux/serial_reg.h>
1952af9545SBill Pemberton #include <linux/uaccess.h>
2052af9545SBill Pemberton 
2152af9545SBill Pemberton #define QT_OPEN_CLOSE_CHANNEL       0xca
2252af9545SBill Pemberton #define QT_SET_GET_DEVICE           0xc2
2352af9545SBill Pemberton #define QT_SET_GET_REGISTER         0xc0
2452af9545SBill Pemberton #define QT_GET_SET_PREBUF_TRIG_LVL  0xcc
2552af9545SBill Pemberton #define QT_SET_ATF                  0xcd
2652af9545SBill Pemberton #define QT_GET_SET_UART             0xc1
2752af9545SBill Pemberton #define QT_TRANSFER_IN              0xc0
2852af9545SBill Pemberton #define QT_HW_FLOW_CONTROL_MASK     0xc5
2952af9545SBill Pemberton #define QT_SW_FLOW_CONTROL_MASK     0xc6
3052af9545SBill Pemberton 
3152af9545SBill Pemberton #define  SERIAL_MSR_MASK            0xf0
3252af9545SBill Pemberton 
3379f203a2SBill Pemberton #define  SERIAL_CRTSCTS ((UART_MCR_RTS << 8) | UART_MSR_CTS)
3452af9545SBill Pemberton 
3579f203a2SBill Pemberton #define  SERIAL_EVEN_PARITY         (UART_LCR_PARITY | UART_LCR_EPAR)
3652af9545SBill Pemberton 
3752af9545SBill Pemberton #define  MAX_BAUD_RATE              460800
3852af9545SBill Pemberton 
3952af9545SBill Pemberton #define ATC_DISABLED                0x00
4052af9545SBill Pemberton #define DUPMODE_BITS        0xc0
4152af9545SBill Pemberton #define RR_BITS             0x03
4252af9545SBill Pemberton #define LOOPMODE_BITS       0x41
4352af9545SBill Pemberton #define RS232_MODE          0x00
4452af9545SBill Pemberton #define RTSCTS_TO_CONNECTOR 0x40
4552af9545SBill Pemberton #define CLKS_X4             0x02
4652af9545SBill Pemberton #define FULLPWRBIT          0x00000080
4752af9545SBill Pemberton #define NEXT_BOARD_POWER_BIT        0x00000004
4852af9545SBill Pemberton 
4952af9545SBill Pemberton #define DRIVER_DESC "Quatech SSU-100 USB to Serial Driver"
5052af9545SBill Pemberton 
5152af9545SBill Pemberton #define	USB_VENDOR_ID_QUATECH	0x061d	/* Quatech VID */
5252af9545SBill Pemberton #define QUATECH_SSU100	0xC020	/* SSU100 */
5352af9545SBill Pemberton 
5452af9545SBill Pemberton static const struct usb_device_id id_table[] = {
5552af9545SBill Pemberton 	{USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_SSU100)},
5652af9545SBill Pemberton 	{}			/* Terminating entry */
5752af9545SBill Pemberton };
5852af9545SBill Pemberton MODULE_DEVICE_TABLE(usb, id_table);
5952af9545SBill Pemberton 
6052af9545SBill Pemberton struct ssu100_port_private {
6117523058SBill Pemberton 	spinlock_t status_lock;
6252af9545SBill Pemberton 	u8 shadowLSR;
6352af9545SBill Pemberton 	u8 shadowMSR;
6452af9545SBill Pemberton };
6552af9545SBill Pemberton 
6652af9545SBill Pemberton static inline int ssu100_control_msg(struct usb_device *dev,
6752af9545SBill Pemberton 				     u8 request, u16 data, u16 index)
6852af9545SBill Pemberton {
6952af9545SBill Pemberton 	return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
7052af9545SBill Pemberton 			       request, 0x40, data, index,
7152af9545SBill Pemberton 			       NULL, 0, 300);
7252af9545SBill Pemberton }
7352af9545SBill Pemberton 
7452af9545SBill Pemberton static inline int ssu100_setdevice(struct usb_device *dev, u8 *data)
7552af9545SBill Pemberton {
7652af9545SBill Pemberton 	u16 x = ((u16)(data[1] << 8) | (u16)(data[0]));
7752af9545SBill Pemberton 
7852af9545SBill Pemberton 	return ssu100_control_msg(dev, QT_SET_GET_DEVICE, x, 0);
7952af9545SBill Pemberton }
8052af9545SBill Pemberton 
8152af9545SBill Pemberton 
8252af9545SBill Pemberton static inline int ssu100_getdevice(struct usb_device *dev, u8 *data)
8352af9545SBill Pemberton {
841eac5c24SJohan Hovold 	int ret;
851eac5c24SJohan Hovold 
861eac5c24SJohan Hovold 	ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
8752af9545SBill Pemberton 			      QT_SET_GET_DEVICE, 0xc0, 0, 0,
8852af9545SBill Pemberton 			      data, 3, 300);
891eac5c24SJohan Hovold 	if (ret < 3) {
901eac5c24SJohan Hovold 		if (ret >= 0)
911eac5c24SJohan Hovold 			ret = -EIO;
921eac5c24SJohan Hovold 	}
931eac5c24SJohan Hovold 
941eac5c24SJohan Hovold 	return ret;
9552af9545SBill Pemberton }
9652af9545SBill Pemberton 
9752af9545SBill Pemberton static inline int ssu100_getregister(struct usb_device *dev,
9852af9545SBill Pemberton 				     unsigned short uart,
9952af9545SBill Pemberton 				     unsigned short reg,
10052af9545SBill Pemberton 				     u8 *data)
10152af9545SBill Pemberton {
1021eac5c24SJohan Hovold 	int ret;
1031eac5c24SJohan Hovold 
1041eac5c24SJohan Hovold 	ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
10552af9545SBill Pemberton 			      QT_SET_GET_REGISTER, 0xc0, reg,
10652af9545SBill Pemberton 			      uart, data, sizeof(*data), 300);
1073391ca1dSChengguang Xu 	if (ret < (int)sizeof(*data)) {
1081eac5c24SJohan Hovold 		if (ret >= 0)
1091eac5c24SJohan Hovold 			ret = -EIO;
1101eac5c24SJohan Hovold 	}
11152af9545SBill Pemberton 
1121eac5c24SJohan Hovold 	return ret;
11352af9545SBill Pemberton }
11452af9545SBill Pemberton 
11552af9545SBill Pemberton 
11652af9545SBill Pemberton static inline int ssu100_setregister(struct usb_device *dev,
11752af9545SBill Pemberton 				     unsigned short uart,
118556f1a0eSBill Pemberton 				     unsigned short reg,
11952af9545SBill Pemberton 				     u16 data)
12052af9545SBill Pemberton {
121556f1a0eSBill Pemberton 	u16 value = (data << 8) | reg;
12252af9545SBill Pemberton 
12352af9545SBill Pemberton 	return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
12452af9545SBill Pemberton 			       QT_SET_GET_REGISTER, 0x40, value, uart,
12552af9545SBill Pemberton 			       NULL, 0, 300);
12652af9545SBill Pemberton 
12752af9545SBill Pemberton }
12852af9545SBill Pemberton 
12952af9545SBill Pemberton #define set_mctrl(dev, set)		update_mctrl((dev), (set), 0)
13052af9545SBill Pemberton #define clear_mctrl(dev, clear)	update_mctrl((dev), 0, (clear))
13152af9545SBill Pemberton 
13252af9545SBill Pemberton /* these do not deal with device that have more than 1 port */
13352af9545SBill Pemberton static inline int update_mctrl(struct usb_device *dev, unsigned int set,
13452af9545SBill Pemberton 			       unsigned int clear)
13552af9545SBill Pemberton {
13652af9545SBill Pemberton 	unsigned urb_value;
13752af9545SBill Pemberton 	int result;
13852af9545SBill Pemberton 
13952af9545SBill Pemberton 	if (((set | clear) & (TIOCM_DTR | TIOCM_RTS)) == 0) {
1404f0c6412SGreg Kroah-Hartman 		dev_dbg(&dev->dev, "%s - DTR|RTS not being set|cleared\n", __func__);
14152af9545SBill Pemberton 		return 0;	/* no change */
14252af9545SBill Pemberton 	}
14352af9545SBill Pemberton 
14452af9545SBill Pemberton 	clear &= ~set;	/* 'set' takes precedence over 'clear' */
14552af9545SBill Pemberton 	urb_value = 0;
14652af9545SBill Pemberton 	if (set & TIOCM_DTR)
14779f203a2SBill Pemberton 		urb_value |= UART_MCR_DTR;
14852af9545SBill Pemberton 	if (set & TIOCM_RTS)
14979f203a2SBill Pemberton 		urb_value |= UART_MCR_RTS;
15052af9545SBill Pemberton 
151556f1a0eSBill Pemberton 	result = ssu100_setregister(dev, 0, UART_MCR, urb_value);
15252af9545SBill Pemberton 	if (result < 0)
1534f0c6412SGreg Kroah-Hartman 		dev_dbg(&dev->dev, "%s Error from MODEM_CTRL urb\n", __func__);
15452af9545SBill Pemberton 
15552af9545SBill Pemberton 	return result;
15652af9545SBill Pemberton }
15752af9545SBill Pemberton 
15852af9545SBill Pemberton static int ssu100_initdevice(struct usb_device *dev)
15952af9545SBill Pemberton {
16052af9545SBill Pemberton 	u8 *data;
16152af9545SBill Pemberton 	int result = 0;
16252af9545SBill Pemberton 
16352af9545SBill Pemberton 	data = kzalloc(3, GFP_KERNEL);
16452af9545SBill Pemberton 	if (!data)
16552af9545SBill Pemberton 		return -ENOMEM;
16652af9545SBill Pemberton 
16752af9545SBill Pemberton 	result = ssu100_getdevice(dev, data);
16852af9545SBill Pemberton 	if (result < 0) {
1694f0c6412SGreg Kroah-Hartman 		dev_dbg(&dev->dev, "%s - get_device failed %i\n", __func__, result);
17052af9545SBill Pemberton 		goto out;
17152af9545SBill Pemberton 	}
17252af9545SBill Pemberton 
17352af9545SBill Pemberton 	data[1] &= ~FULLPWRBIT;
17452af9545SBill Pemberton 
17552af9545SBill Pemberton 	result = ssu100_setdevice(dev, data);
17652af9545SBill Pemberton 	if (result < 0) {
1774f0c6412SGreg Kroah-Hartman 		dev_dbg(&dev->dev, "%s - setdevice failed %i\n", __func__, result);
17852af9545SBill Pemberton 		goto out;
17952af9545SBill Pemberton 	}
18052af9545SBill Pemberton 
18152af9545SBill Pemberton 	result = ssu100_control_msg(dev, QT_GET_SET_PREBUF_TRIG_LVL, 128, 0);
18252af9545SBill Pemberton 	if (result < 0) {
1834f0c6412SGreg Kroah-Hartman 		dev_dbg(&dev->dev, "%s - set prebuffer level failed %i\n", __func__, result);
18452af9545SBill Pemberton 		goto out;
18552af9545SBill Pemberton 	}
18652af9545SBill Pemberton 
18752af9545SBill Pemberton 	result = ssu100_control_msg(dev, QT_SET_ATF, ATC_DISABLED, 0);
18852af9545SBill Pemberton 	if (result < 0) {
1894f0c6412SGreg Kroah-Hartman 		dev_dbg(&dev->dev, "%s - set ATFprebuffer level failed %i\n", __func__, result);
19052af9545SBill Pemberton 		goto out;
19152af9545SBill Pemberton 	}
19252af9545SBill Pemberton 
19352af9545SBill Pemberton 	result = ssu100_getdevice(dev, data);
19452af9545SBill Pemberton 	if (result < 0) {
1954f0c6412SGreg Kroah-Hartman 		dev_dbg(&dev->dev, "%s - get_device failed %i\n", __func__, result);
19652af9545SBill Pemberton 		goto out;
19752af9545SBill Pemberton 	}
19852af9545SBill Pemberton 
19952af9545SBill Pemberton 	data[0] &= ~(RR_BITS | DUPMODE_BITS);
20052af9545SBill Pemberton 	data[0] |= CLKS_X4;
20152af9545SBill Pemberton 	data[1] &= ~(LOOPMODE_BITS);
20252af9545SBill Pemberton 	data[1] |= RS232_MODE;
20352af9545SBill Pemberton 
20452af9545SBill Pemberton 	result = ssu100_setdevice(dev, data);
20552af9545SBill Pemberton 	if (result < 0) {
2064f0c6412SGreg Kroah-Hartman 		dev_dbg(&dev->dev, "%s - setdevice failed %i\n", __func__, result);
20752af9545SBill Pemberton 		goto out;
20852af9545SBill Pemberton 	}
20952af9545SBill Pemberton 
21052af9545SBill Pemberton out:	kfree(data);
21152af9545SBill Pemberton 	return result;
21252af9545SBill Pemberton 
21352af9545SBill Pemberton }
21452af9545SBill Pemberton 
21552af9545SBill Pemberton 
21652af9545SBill Pemberton static void ssu100_set_termios(struct tty_struct *tty,
21752af9545SBill Pemberton 			       struct usb_serial_port *port,
21852af9545SBill Pemberton 			       struct ktermios *old_termios)
21952af9545SBill Pemberton {
22052af9545SBill Pemberton 	struct usb_device *dev = port->serial->dev;
221adc8d746SAlan Cox 	struct ktermios *termios = &tty->termios;
22252af9545SBill Pemberton 	u16 baud, divisor, remainder;
22352af9545SBill Pemberton 	unsigned int cflag = termios->c_cflag;
22452af9545SBill Pemberton 	u16 urb_value = 0; /* will hold the new flags */
22552af9545SBill Pemberton 	int result;
22652af9545SBill Pemberton 
22752af9545SBill Pemberton 	if (cflag & PARENB) {
22852af9545SBill Pemberton 		if (cflag & PARODD)
22979f203a2SBill Pemberton 			urb_value |= UART_LCR_PARITY;
23052af9545SBill Pemberton 		else
23152af9545SBill Pemberton 			urb_value |= SERIAL_EVEN_PARITY;
23252af9545SBill Pemberton 	}
23352af9545SBill Pemberton 
23452af9545SBill Pemberton 	switch (cflag & CSIZE) {
23552af9545SBill Pemberton 	case CS5:
23679f203a2SBill Pemberton 		urb_value |= UART_LCR_WLEN5;
23752af9545SBill Pemberton 		break;
23852af9545SBill Pemberton 	case CS6:
23979f203a2SBill Pemberton 		urb_value |= UART_LCR_WLEN6;
24052af9545SBill Pemberton 		break;
24152af9545SBill Pemberton 	case CS7:
24279f203a2SBill Pemberton 		urb_value |= UART_LCR_WLEN7;
24352af9545SBill Pemberton 		break;
24452af9545SBill Pemberton 	default:
24552af9545SBill Pemberton 	case CS8:
24679f203a2SBill Pemberton 		urb_value |= UART_LCR_WLEN8;
24752af9545SBill Pemberton 		break;
24852af9545SBill Pemberton 	}
24952af9545SBill Pemberton 
25052af9545SBill Pemberton 	baud = tty_get_baud_rate(tty);
25152af9545SBill Pemberton 	if (!baud)
25252af9545SBill Pemberton 		baud = 9600;
25352af9545SBill Pemberton 
2544f0c6412SGreg Kroah-Hartman 	dev_dbg(&port->dev, "%s - got baud = %d\n", __func__, baud);
25552af9545SBill Pemberton 
25652af9545SBill Pemberton 
25752af9545SBill Pemberton 	divisor = MAX_BAUD_RATE / baud;
25852af9545SBill Pemberton 	remainder = MAX_BAUD_RATE % baud;
25952af9545SBill Pemberton 	if (((remainder * 2) >= baud) && (baud != 110))
26052af9545SBill Pemberton 		divisor++;
26152af9545SBill Pemberton 
26252af9545SBill Pemberton 	urb_value = urb_value << 8;
26352af9545SBill Pemberton 
26452af9545SBill Pemberton 	result = ssu100_control_msg(dev, QT_GET_SET_UART, divisor, urb_value);
26552af9545SBill Pemberton 	if (result < 0)
2664f0c6412SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s - set uart failed\n", __func__);
26752af9545SBill Pemberton 
26852af9545SBill Pemberton 	if (cflag & CRTSCTS)
26952af9545SBill Pemberton 		result = ssu100_control_msg(dev, QT_HW_FLOW_CONTROL_MASK,
27052af9545SBill Pemberton 					    SERIAL_CRTSCTS, 0);
27152af9545SBill Pemberton 	else
27252af9545SBill Pemberton 		result = ssu100_control_msg(dev, QT_HW_FLOW_CONTROL_MASK,
27352af9545SBill Pemberton 					    0, 0);
27452af9545SBill Pemberton 	if (result < 0)
2754f0c6412SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s - set HW flow control failed\n", __func__);
27652af9545SBill Pemberton 
27752af9545SBill Pemberton 	if (I_IXOFF(tty) || I_IXON(tty)) {
27852af9545SBill Pemberton 		u16 x = ((u16)(START_CHAR(tty) << 8) | (u16)(STOP_CHAR(tty)));
27952af9545SBill Pemberton 
28052af9545SBill Pemberton 		result = ssu100_control_msg(dev, QT_SW_FLOW_CONTROL_MASK,
28152af9545SBill Pemberton 					    x, 0);
28252af9545SBill Pemberton 	} else
28352af9545SBill Pemberton 		result = ssu100_control_msg(dev, QT_SW_FLOW_CONTROL_MASK,
28452af9545SBill Pemberton 					    0, 0);
28552af9545SBill Pemberton 
28652af9545SBill Pemberton 	if (result < 0)
2874f0c6412SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s - set SW flow control failed\n", __func__);
28852af9545SBill Pemberton 
28952af9545SBill Pemberton }
29052af9545SBill Pemberton 
29152af9545SBill Pemberton 
29252af9545SBill Pemberton static int ssu100_open(struct tty_struct *tty, struct usb_serial_port *port)
29352af9545SBill Pemberton {
29452af9545SBill Pemberton 	struct usb_device *dev = port->serial->dev;
29552af9545SBill Pemberton 	struct ssu100_port_private *priv = usb_get_serial_port_data(port);
29652af9545SBill Pemberton 	u8 *data;
29752af9545SBill Pemberton 	int result;
29817523058SBill Pemberton 	unsigned long flags;
29952af9545SBill Pemberton 
30052af9545SBill Pemberton 	data = kzalloc(2, GFP_KERNEL);
30152af9545SBill Pemberton 	if (!data)
30252af9545SBill Pemberton 		return -ENOMEM;
30352af9545SBill Pemberton 
30452af9545SBill Pemberton 	result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
30552af9545SBill Pemberton 				 QT_OPEN_CLOSE_CHANNEL,
30652af9545SBill Pemberton 				 QT_TRANSFER_IN, 0x01,
30752af9545SBill Pemberton 				 0, data, 2, 300);
3081eac5c24SJohan Hovold 	if (result < 2) {
3094f0c6412SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s - open failed %i\n", __func__, result);
3101eac5c24SJohan Hovold 		if (result >= 0)
3111eac5c24SJohan Hovold 			result = -EIO;
31252af9545SBill Pemberton 		kfree(data);
31352af9545SBill Pemberton 		return result;
31452af9545SBill Pemberton 	}
31552af9545SBill Pemberton 
31617523058SBill Pemberton 	spin_lock_irqsave(&priv->status_lock, flags);
317f81c83dbSBill Pemberton 	priv->shadowLSR = data[0];
318f81c83dbSBill Pemberton 	priv->shadowMSR = data[1];
31917523058SBill Pemberton 	spin_unlock_irqrestore(&priv->status_lock, flags);
32052af9545SBill Pemberton 
32152af9545SBill Pemberton 	kfree(data);
32252af9545SBill Pemberton 
32352af9545SBill Pemberton /* set to 9600 */
32452af9545SBill Pemberton 	result = ssu100_control_msg(dev, QT_GET_SET_UART, 0x30, 0x0300);
32552af9545SBill Pemberton 	if (result < 0)
3264f0c6412SGreg Kroah-Hartman 		dev_dbg(&port->dev, "%s - set uart failed\n", __func__);
32752af9545SBill Pemberton 
32852af9545SBill Pemberton 	if (tty)
329adc8d746SAlan Cox 		ssu100_set_termios(tty, port, &tty->termios);
33052af9545SBill Pemberton 
33152af9545SBill Pemberton 	return usb_serial_generic_open(tty, port);
33252af9545SBill Pemberton }
33352af9545SBill Pemberton 
334ee08cefbSAl Viro static int get_serial_info(struct tty_struct *tty,
335ee08cefbSAl Viro 			   struct serial_struct *ss)
33652af9545SBill Pemberton {
33752af9545SBill Pemberton 	struct usb_serial_port *port = tty->driver_data;
33852af9545SBill Pemberton 
339ee08cefbSAl Viro 	ss->line		= port->minor;
340ee08cefbSAl Viro 	ss->port		= 0;
341ee08cefbSAl Viro 	ss->irq			= 0;
342ee08cefbSAl Viro 	ss->xmit_fifo_size	= port->bulk_out_size;
343ee08cefbSAl Viro 	ss->baud_base		= 9600;
344ee08cefbSAl Viro 	ss->close_delay		= 5*HZ;
345ee08cefbSAl Viro 	ss->closing_wait	= 30*HZ;
346ee08cefbSAl Viro 	return 0;
34752af9545SBill Pemberton }
34852af9545SBill Pemberton 
34952af9545SBill Pemberton static int ssu100_attach(struct usb_serial *serial)
35052af9545SBill Pemberton {
351638b9e15SJohan Hovold 	return ssu100_initdevice(serial->dev);
352638b9e15SJohan Hovold }
353638b9e15SJohan Hovold 
354638b9e15SJohan Hovold static int ssu100_port_probe(struct usb_serial_port *port)
355638b9e15SJohan Hovold {
35652af9545SBill Pemberton 	struct ssu100_port_private *priv;
35752af9545SBill Pemberton 
35852af9545SBill Pemberton 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
359638b9e15SJohan Hovold 	if (!priv)
36052af9545SBill Pemberton 		return -ENOMEM;
36152af9545SBill Pemberton 
36217523058SBill Pemberton 	spin_lock_init(&priv->status_lock);
363638b9e15SJohan Hovold 
36452af9545SBill Pemberton 	usb_set_serial_port_data(port, priv);
36552af9545SBill Pemberton 
366638b9e15SJohan Hovold 	return 0;
367638b9e15SJohan Hovold }
368638b9e15SJohan Hovold 
369638b9e15SJohan Hovold static int ssu100_port_remove(struct usb_serial_port *port)
370638b9e15SJohan Hovold {
371638b9e15SJohan Hovold 	struct ssu100_port_private *priv;
372638b9e15SJohan Hovold 
373638b9e15SJohan Hovold 	priv = usb_get_serial_port_data(port);
374638b9e15SJohan Hovold 	kfree(priv);
375638b9e15SJohan Hovold 
376638b9e15SJohan Hovold 	return 0;
37752af9545SBill Pemberton }
37852af9545SBill Pemberton 
37960b33c13SAlan Cox static int ssu100_tiocmget(struct tty_struct *tty)
38052af9545SBill Pemberton {
38152af9545SBill Pemberton 	struct usb_serial_port *port = tty->driver_data;
38252af9545SBill Pemberton 	struct usb_device *dev = port->serial->dev;
38352af9545SBill Pemberton 	u8 *d;
38452af9545SBill Pemberton 	int r;
38552af9545SBill Pemberton 
38652af9545SBill Pemberton 	d = kzalloc(2, GFP_KERNEL);
38752af9545SBill Pemberton 	if (!d)
38852af9545SBill Pemberton 		return -ENOMEM;
38952af9545SBill Pemberton 
39079f203a2SBill Pemberton 	r = ssu100_getregister(dev, 0, UART_MCR, d);
39152af9545SBill Pemberton 	if (r < 0)
39252af9545SBill Pemberton 		goto mget_out;
39352af9545SBill Pemberton 
39479f203a2SBill Pemberton 	r = ssu100_getregister(dev, 0, UART_MSR, d+1);
39552af9545SBill Pemberton 	if (r < 0)
39652af9545SBill Pemberton 		goto mget_out;
39752af9545SBill Pemberton 
39879f203a2SBill Pemberton 	r = (d[0] & UART_MCR_DTR ? TIOCM_DTR : 0) |
39979f203a2SBill Pemberton 		(d[0] & UART_MCR_RTS ? TIOCM_RTS : 0) |
40079f203a2SBill Pemberton 		(d[1] & UART_MSR_CTS ? TIOCM_CTS : 0) |
40179f203a2SBill Pemberton 		(d[1] & UART_MSR_DCD ? TIOCM_CAR : 0) |
40279f203a2SBill Pemberton 		(d[1] & UART_MSR_RI ? TIOCM_RI : 0) |
40379f203a2SBill Pemberton 		(d[1] & UART_MSR_DSR ? TIOCM_DSR : 0);
40452af9545SBill Pemberton 
40552af9545SBill Pemberton mget_out:
40652af9545SBill Pemberton 	kfree(d);
40752af9545SBill Pemberton 	return r;
40852af9545SBill Pemberton }
40952af9545SBill Pemberton 
41020b9d177SAlan Cox static int ssu100_tiocmset(struct tty_struct *tty,
41152af9545SBill Pemberton 			   unsigned int set, unsigned int clear)
41252af9545SBill Pemberton {
41352af9545SBill Pemberton 	struct usb_serial_port *port = tty->driver_data;
41452af9545SBill Pemberton 	struct usb_device *dev = port->serial->dev;
41552af9545SBill Pemberton 
41652af9545SBill Pemberton 	return update_mctrl(dev, set, clear);
41752af9545SBill Pemberton }
41852af9545SBill Pemberton 
41952af9545SBill Pemberton static void ssu100_dtr_rts(struct usb_serial_port *port, int on)
42052af9545SBill Pemberton {
42152af9545SBill Pemberton 	struct usb_device *dev = port->serial->dev;
42252af9545SBill Pemberton 
42352af9545SBill Pemberton 	/* Disable flow control */
424b2ca6990SJohan Hovold 	if (!on) {
425b2ca6990SJohan Hovold 		if (ssu100_setregister(dev, 0, UART_MCR, 0) < 0)
42652af9545SBill Pemberton 			dev_err(&port->dev, "error from flowcontrol urb\n");
427b2ca6990SJohan Hovold 	}
42852af9545SBill Pemberton 	/* drop RTS and DTR */
42952af9545SBill Pemberton 	if (on)
43052af9545SBill Pemberton 		set_mctrl(dev, TIOCM_DTR | TIOCM_RTS);
43152af9545SBill Pemberton 	else
43252af9545SBill Pemberton 		clear_mctrl(dev, TIOCM_DTR | TIOCM_RTS);
43352af9545SBill Pemberton }
43452af9545SBill Pemberton 
435f81c83dbSBill Pemberton static void ssu100_update_msr(struct usb_serial_port *port, u8 msr)
436f81c83dbSBill Pemberton {
437f81c83dbSBill Pemberton 	struct ssu100_port_private *priv = usb_get_serial_port_data(port);
438f81c83dbSBill Pemberton 	unsigned long flags;
439f81c83dbSBill Pemberton 
440f81c83dbSBill Pemberton 	spin_lock_irqsave(&priv->status_lock, flags);
441f81c83dbSBill Pemberton 	priv->shadowMSR = msr;
442f81c83dbSBill Pemberton 	spin_unlock_irqrestore(&priv->status_lock, flags);
443f81c83dbSBill Pemberton 
444f81c83dbSBill Pemberton 	if (msr & UART_MSR_ANY_DELTA) {
445f81c83dbSBill Pemberton 		/* update input line counters */
446f81c83dbSBill Pemberton 		if (msr & UART_MSR_DCTS)
44731ecdb6bSJohan Hovold 			port->icount.cts++;
448f81c83dbSBill Pemberton 		if (msr & UART_MSR_DDSR)
44931ecdb6bSJohan Hovold 			port->icount.dsr++;
450f81c83dbSBill Pemberton 		if (msr & UART_MSR_DDCD)
45131ecdb6bSJohan Hovold 			port->icount.dcd++;
452f81c83dbSBill Pemberton 		if (msr & UART_MSR_TERI)
45331ecdb6bSJohan Hovold 			port->icount.rng++;
454c24c838eSJohan Hovold 		wake_up_interruptible(&port->port.delta_msr_wait);
455f81c83dbSBill Pemberton 	}
456f81c83dbSBill Pemberton }
457f81c83dbSBill Pemberton 
4586b8f1ca5SBill Pemberton static void ssu100_update_lsr(struct usb_serial_port *port, u8 lsr,
4596b8f1ca5SBill Pemberton 			      char *tty_flag)
460f81c83dbSBill Pemberton {
461f81c83dbSBill Pemberton 	struct ssu100_port_private *priv = usb_get_serial_port_data(port);
462f81c83dbSBill Pemberton 	unsigned long flags;
463f81c83dbSBill Pemberton 
464f81c83dbSBill Pemberton 	spin_lock_irqsave(&priv->status_lock, flags);
465f81c83dbSBill Pemberton 	priv->shadowLSR = lsr;
466f81c83dbSBill Pemberton 	spin_unlock_irqrestore(&priv->status_lock, flags);
467f81c83dbSBill Pemberton 
4686b8f1ca5SBill Pemberton 	*tty_flag = TTY_NORMAL;
469f81c83dbSBill Pemberton 	if (lsr & UART_LSR_BRK_ERROR_BITS) {
4706b8f1ca5SBill Pemberton 		/* we always want to update icount, but we only want to
4716b8f1ca5SBill Pemberton 		 * update tty_flag for one case */
4726b8f1ca5SBill Pemberton 		if (lsr & UART_LSR_BI) {
47331ecdb6bSJohan Hovold 			port->icount.brk++;
4746b8f1ca5SBill Pemberton 			*tty_flag = TTY_BREAK;
4756b8f1ca5SBill Pemberton 			usb_serial_handle_break(port);
476f81c83dbSBill Pemberton 		}
4776b8f1ca5SBill Pemberton 		if (lsr & UART_LSR_PE) {
47831ecdb6bSJohan Hovold 			port->icount.parity++;
4796b8f1ca5SBill Pemberton 			if (*tty_flag == TTY_NORMAL)
4806b8f1ca5SBill Pemberton 				*tty_flag = TTY_PARITY;
4816b8f1ca5SBill Pemberton 		}
4826b8f1ca5SBill Pemberton 		if (lsr & UART_LSR_FE) {
48331ecdb6bSJohan Hovold 			port->icount.frame++;
4846b8f1ca5SBill Pemberton 			if (*tty_flag == TTY_NORMAL)
4856b8f1ca5SBill Pemberton 				*tty_flag = TTY_FRAME;
4866b8f1ca5SBill Pemberton 		}
4876b8f1ca5SBill Pemberton 		if (lsr & UART_LSR_OE) {
48831ecdb6bSJohan Hovold 			port->icount.overrun++;
48975bcbf29SJohan Hovold 			tty_insert_flip_char(&port->port, 0, TTY_OVERRUN);
4906b8f1ca5SBill Pemberton 		}
4916b8f1ca5SBill Pemberton 	}
4926b8f1ca5SBill Pemberton 
493f81c83dbSBill Pemberton }
494f81c83dbSBill Pemberton 
4952e124b4aSJiri Slaby static void ssu100_process_read_urb(struct urb *urb)
49652af9545SBill Pemberton {
497f7043ecbSBill Pemberton 	struct usb_serial_port *port = urb->context;
498*eb0c68eaSJohan Hovold 	char *packet = urb->transfer_buffer;
4996b8f1ca5SBill Pemberton 	char flag = TTY_NORMAL;
500f7043ecbSBill Pemberton 	u32 len = urb->actual_length;
501f7043ecbSBill Pemberton 	int i;
50252af9545SBill Pemberton 	char *ch;
50352af9545SBill Pemberton 
5049b2cef31SBill Pemberton 	if ((len >= 4) &&
5059b2cef31SBill Pemberton 	    (packet[0] == 0x1b) && (packet[1] == 0x1b) &&
50652af9545SBill Pemberton 	    ((packet[2] == 0x00) || (packet[2] == 0x01))) {
50775bcbf29SJohan Hovold 		if (packet[2] == 0x00)
5086b8f1ca5SBill Pemberton 			ssu100_update_lsr(port, packet[3], &flag);
509f81c83dbSBill Pemberton 		if (packet[2] == 0x01)
510f81c83dbSBill Pemberton 			ssu100_update_msr(port, packet[3]);
51152af9545SBill Pemberton 
51252af9545SBill Pemberton 		len -= 4;
51352af9545SBill Pemberton 		ch = packet + 4;
51452af9545SBill Pemberton 	} else
51552af9545SBill Pemberton 		ch = packet;
51652af9545SBill Pemberton 
51752af9545SBill Pemberton 	if (!len)
5182e124b4aSJiri Slaby 		return;	/* status only */
51952af9545SBill Pemberton 
52037ae2315SJohan Hovold 	if (port->sysrq) {
52152af9545SBill Pemberton 		for (i = 0; i < len; i++, ch++) {
5226ee9f4b4SDmitry Torokhov 			if (!usb_serial_handle_sysrq_char(port, *ch))
52392a19f9cSJiri Slaby 				tty_insert_flip_char(&port->port, *ch, flag);
52452af9545SBill Pemberton 		}
52537ae2315SJohan Hovold 	} else {
5262f693357SJiri Slaby 		tty_insert_flip_string_fixed_flag(&port->port, ch, flag, len);
52737ae2315SJohan Hovold 	}
52852af9545SBill Pemberton 
5292e124b4aSJiri Slaby 	tty_flip_buffer_push(&port->port);
53052af9545SBill Pemberton }
53152af9545SBill Pemberton 
53252af9545SBill Pemberton static struct usb_serial_driver ssu100_device = {
53352af9545SBill Pemberton 	.driver = {
53452af9545SBill Pemberton 		.owner = THIS_MODULE,
53552af9545SBill Pemberton 		.name = "ssu100",
53652af9545SBill Pemberton 	},
53752af9545SBill Pemberton 	.description	     = DRIVER_DESC,
53852af9545SBill Pemberton 	.id_table	     = id_table,
53952af9545SBill Pemberton 	.num_ports	     = 1,
54052af9545SBill Pemberton 	.open		     = ssu100_open,
54152af9545SBill Pemberton 	.attach              = ssu100_attach,
542638b9e15SJohan Hovold 	.port_probe          = ssu100_port_probe,
543638b9e15SJohan Hovold 	.port_remove         = ssu100_port_remove,
54452af9545SBill Pemberton 	.dtr_rts             = ssu100_dtr_rts,
54552af9545SBill Pemberton 	.process_read_urb    = ssu100_process_read_urb,
54652af9545SBill Pemberton 	.tiocmget            = ssu100_tiocmget,
54752af9545SBill Pemberton 	.tiocmset            = ssu100_tiocmset,
548c24c838eSJohan Hovold 	.tiocmiwait          = usb_serial_generic_tiocmiwait,
54931ecdb6bSJohan Hovold 	.get_icount	     = usb_serial_generic_get_icount,
550ee08cefbSAl Viro 	.get_serial          = get_serial_info,
55152af9545SBill Pemberton 	.set_termios         = ssu100_set_termios,
55252af9545SBill Pemberton };
55352af9545SBill Pemberton 
554d860322fSAlan Stern static struct usb_serial_driver * const serial_drivers[] = {
555d860322fSAlan Stern 	&ssu100_device, NULL
556d860322fSAlan Stern };
557d860322fSAlan Stern 
55868e24113SGreg Kroah-Hartman module_usb_serial_driver(serial_drivers, id_table);
55952af9545SBill Pemberton 
56052af9545SBill Pemberton MODULE_DESCRIPTION(DRIVER_DESC);
561627cfa89SJohan Hovold MODULE_LICENSE("GPL v2");
562