| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 1 | /* | 
 | 2 |  * usb-serial driver for Quatech SSU-100 | 
 | 3 |  * | 
 | 4 |  * based on ftdi_sio.c and the original serqt_usb.c from Quatech | 
 | 5 |  * | 
 | 6 |  */ | 
 | 7 |  | 
 | 8 | #include <linux/errno.h> | 
 | 9 | #include <linux/init.h> | 
 | 10 | #include <linux/slab.h> | 
 | 11 | #include <linux/tty.h> | 
 | 12 | #include <linux/tty_driver.h> | 
 | 13 | #include <linux/tty_flip.h> | 
 | 14 | #include <linux/module.h> | 
 | 15 | #include <linux/serial.h> | 
 | 16 | #include <linux/usb.h> | 
 | 17 | #include <linux/usb/serial.h> | 
| Bill Pemberton | 79f203a | 2010-08-05 17:01:07 -0400 | [diff] [blame] | 18 | #include <linux/serial_reg.h> | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 19 | #include <linux/uaccess.h> | 
 | 20 |  | 
 | 21 | #define QT_OPEN_CLOSE_CHANNEL       0xca | 
 | 22 | #define QT_SET_GET_DEVICE           0xc2 | 
 | 23 | #define QT_SET_GET_REGISTER         0xc0 | 
 | 24 | #define QT_GET_SET_PREBUF_TRIG_LVL  0xcc | 
 | 25 | #define QT_SET_ATF                  0xcd | 
 | 26 | #define QT_GET_SET_UART             0xc1 | 
 | 27 | #define QT_TRANSFER_IN              0xc0 | 
 | 28 | #define QT_HW_FLOW_CONTROL_MASK     0xc5 | 
 | 29 | #define QT_SW_FLOW_CONTROL_MASK     0xc6 | 
 | 30 |  | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 31 | #define  SERIAL_MSR_MASK            0xf0 | 
 | 32 |  | 
| Bill Pemberton | 79f203a | 2010-08-05 17:01:07 -0400 | [diff] [blame] | 33 | #define  SERIAL_CRTSCTS ((UART_MCR_RTS << 8) | UART_MSR_CTS) | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 34 |  | 
| Bill Pemberton | 79f203a | 2010-08-05 17:01:07 -0400 | [diff] [blame] | 35 | #define  SERIAL_EVEN_PARITY         (UART_LCR_PARITY | UART_LCR_EPAR) | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 36 |  | 
 | 37 | #define  MAX_BAUD_RATE              460800 | 
 | 38 |  | 
 | 39 | #define ATC_DISABLED                0x00 | 
 | 40 | #define DUPMODE_BITS        0xc0 | 
 | 41 | #define RR_BITS             0x03 | 
 | 42 | #define LOOPMODE_BITS       0x41 | 
 | 43 | #define RS232_MODE          0x00 | 
 | 44 | #define RTSCTS_TO_CONNECTOR 0x40 | 
 | 45 | #define CLKS_X4             0x02 | 
 | 46 | #define FULLPWRBIT          0x00000080 | 
 | 47 | #define NEXT_BOARD_POWER_BIT        0x00000004 | 
 | 48 |  | 
| Rusty Russell | 90ab5ee | 2012-01-13 09:32:20 +1030 | [diff] [blame] | 49 | static bool debug; | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 50 |  | 
 | 51 | /* Version Information */ | 
 | 52 | #define DRIVER_VERSION "v0.1" | 
 | 53 | #define DRIVER_DESC "Quatech SSU-100 USB to Serial Driver" | 
 | 54 |  | 
 | 55 | #define	USB_VENDOR_ID_QUATECH	0x061d	/* Quatech VID */ | 
 | 56 | #define QUATECH_SSU100	0xC020	/* SSU100 */ | 
 | 57 |  | 
 | 58 | static const struct usb_device_id id_table[] = { | 
 | 59 | 	{USB_DEVICE(USB_VENDOR_ID_QUATECH, QUATECH_SSU100)}, | 
 | 60 | 	{}			/* Terminating entry */ | 
 | 61 | }; | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 62 | MODULE_DEVICE_TABLE(usb, id_table); | 
 | 63 |  | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 64 | struct ssu100_port_private { | 
| Bill Pemberton | 1752305 | 2010-08-05 17:01:05 -0400 | [diff] [blame] | 65 | 	spinlock_t status_lock; | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 66 | 	u8 shadowLSR; | 
 | 67 | 	u8 shadowMSR; | 
 | 68 | 	wait_queue_head_t delta_msr_wait; /* Used for TIOCMIWAIT */ | 
| Bill Pemberton | f81c83d | 2010-08-05 17:01:09 -0400 | [diff] [blame] | 69 | 	struct async_icount icount; | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 70 | }; | 
 | 71 |  | 
 | 72 | static void ssu100_release(struct usb_serial *serial) | 
 | 73 | { | 
 | 74 | 	struct ssu100_port_private *priv = usb_get_serial_port_data(*serial->port); | 
 | 75 |  | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 76 | 	kfree(priv); | 
 | 77 | } | 
 | 78 |  | 
 | 79 | static inline int ssu100_control_msg(struct usb_device *dev, | 
 | 80 | 				     u8 request, u16 data, u16 index) | 
 | 81 | { | 
 | 82 | 	return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), | 
 | 83 | 			       request, 0x40, data, index, | 
 | 84 | 			       NULL, 0, 300); | 
 | 85 | } | 
 | 86 |  | 
 | 87 | static inline int ssu100_setdevice(struct usb_device *dev, u8 *data) | 
 | 88 | { | 
 | 89 | 	u16 x = ((u16)(data[1] << 8) | (u16)(data[0])); | 
 | 90 |  | 
 | 91 | 	return ssu100_control_msg(dev, QT_SET_GET_DEVICE, x, 0); | 
 | 92 | } | 
 | 93 |  | 
 | 94 |  | 
 | 95 | static inline int ssu100_getdevice(struct usb_device *dev, u8 *data) | 
 | 96 | { | 
 | 97 | 	return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), | 
 | 98 | 			       QT_SET_GET_DEVICE, 0xc0, 0, 0, | 
 | 99 | 			       data, 3, 300); | 
 | 100 | } | 
 | 101 |  | 
 | 102 | static inline int ssu100_getregister(struct usb_device *dev, | 
 | 103 | 				     unsigned short uart, | 
 | 104 | 				     unsigned short reg, | 
 | 105 | 				     u8 *data) | 
 | 106 | { | 
 | 107 | 	return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), | 
 | 108 | 			       QT_SET_GET_REGISTER, 0xc0, reg, | 
 | 109 | 			       uart, data, sizeof(*data), 300); | 
 | 110 |  | 
 | 111 | } | 
 | 112 |  | 
 | 113 |  | 
 | 114 | static inline int ssu100_setregister(struct usb_device *dev, | 
 | 115 | 				     unsigned short uart, | 
| Bill Pemberton | 556f1a0 | 2010-08-05 17:01:08 -0400 | [diff] [blame] | 116 | 				     unsigned short reg, | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 117 | 				     u16 data) | 
 | 118 | { | 
| Bill Pemberton | 556f1a0 | 2010-08-05 17:01:08 -0400 | [diff] [blame] | 119 | 	u16 value = (data << 8) | reg; | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 120 |  | 
 | 121 | 	return usb_control_msg(dev, usb_sndctrlpipe(dev, 0), | 
 | 122 | 			       QT_SET_GET_REGISTER, 0x40, value, uart, | 
 | 123 | 			       NULL, 0, 300); | 
 | 124 |  | 
 | 125 | } | 
 | 126 |  | 
 | 127 | #define set_mctrl(dev, set)		update_mctrl((dev), (set), 0) | 
 | 128 | #define clear_mctrl(dev, clear)	update_mctrl((dev), 0, (clear)) | 
 | 129 |  | 
 | 130 | /* these do not deal with device that have more than 1 port */ | 
 | 131 | static inline int update_mctrl(struct usb_device *dev, unsigned int set, | 
 | 132 | 			       unsigned int clear) | 
 | 133 | { | 
 | 134 | 	unsigned urb_value; | 
 | 135 | 	int result; | 
 | 136 |  | 
 | 137 | 	if (((set | clear) & (TIOCM_DTR | TIOCM_RTS)) == 0) { | 
 | 138 | 		dbg("%s - DTR|RTS not being set|cleared", __func__); | 
 | 139 | 		return 0;	/* no change */ | 
 | 140 | 	} | 
 | 141 |  | 
 | 142 | 	clear &= ~set;	/* 'set' takes precedence over 'clear' */ | 
 | 143 | 	urb_value = 0; | 
 | 144 | 	if (set & TIOCM_DTR) | 
| Bill Pemberton | 79f203a | 2010-08-05 17:01:07 -0400 | [diff] [blame] | 145 | 		urb_value |= UART_MCR_DTR; | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 146 | 	if (set & TIOCM_RTS) | 
| Bill Pemberton | 79f203a | 2010-08-05 17:01:07 -0400 | [diff] [blame] | 147 | 		urb_value |= UART_MCR_RTS; | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 148 |  | 
| Bill Pemberton | 556f1a0 | 2010-08-05 17:01:08 -0400 | [diff] [blame] | 149 | 	result = ssu100_setregister(dev, 0, UART_MCR, urb_value); | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 150 | 	if (result < 0) | 
 | 151 | 		dbg("%s Error from MODEM_CTRL urb", __func__); | 
 | 152 |  | 
 | 153 | 	return result; | 
 | 154 | } | 
 | 155 |  | 
 | 156 | static int ssu100_initdevice(struct usb_device *dev) | 
 | 157 | { | 
 | 158 | 	u8 *data; | 
 | 159 | 	int result = 0; | 
 | 160 |  | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 161 | 	data = kzalloc(3, GFP_KERNEL); | 
 | 162 | 	if (!data) | 
 | 163 | 		return -ENOMEM; | 
 | 164 |  | 
 | 165 | 	result = ssu100_getdevice(dev, data); | 
 | 166 | 	if (result < 0) { | 
 | 167 | 		dbg("%s - get_device failed %i", __func__, result); | 
 | 168 | 		goto out; | 
 | 169 | 	} | 
 | 170 |  | 
 | 171 | 	data[1] &= ~FULLPWRBIT; | 
 | 172 |  | 
 | 173 | 	result = ssu100_setdevice(dev, data); | 
 | 174 | 	if (result < 0) { | 
 | 175 | 		dbg("%s - setdevice failed %i", __func__, result); | 
 | 176 | 		goto out; | 
 | 177 | 	} | 
 | 178 |  | 
 | 179 | 	result = ssu100_control_msg(dev, QT_GET_SET_PREBUF_TRIG_LVL, 128, 0); | 
 | 180 | 	if (result < 0) { | 
 | 181 | 		dbg("%s - set prebuffer level failed %i", __func__, result); | 
 | 182 | 		goto out; | 
 | 183 | 	} | 
 | 184 |  | 
 | 185 | 	result = ssu100_control_msg(dev, QT_SET_ATF, ATC_DISABLED, 0); | 
 | 186 | 	if (result < 0) { | 
 | 187 | 		dbg("%s - set ATFprebuffer level failed %i", __func__, result); | 
 | 188 | 		goto out; | 
 | 189 | 	} | 
 | 190 |  | 
 | 191 | 	result = ssu100_getdevice(dev, data); | 
 | 192 | 	if (result < 0) { | 
 | 193 | 		dbg("%s - get_device failed %i", __func__, result); | 
 | 194 | 		goto out; | 
 | 195 | 	} | 
 | 196 |  | 
 | 197 | 	data[0] &= ~(RR_BITS | DUPMODE_BITS); | 
 | 198 | 	data[0] |= CLKS_X4; | 
 | 199 | 	data[1] &= ~(LOOPMODE_BITS); | 
 | 200 | 	data[1] |= RS232_MODE; | 
 | 201 |  | 
 | 202 | 	result = ssu100_setdevice(dev, data); | 
 | 203 | 	if (result < 0) { | 
 | 204 | 		dbg("%s - setdevice failed %i", __func__, result); | 
 | 205 | 		goto out; | 
 | 206 | 	} | 
 | 207 |  | 
 | 208 | out:	kfree(data); | 
 | 209 | 	return result; | 
 | 210 |  | 
 | 211 | } | 
 | 212 |  | 
 | 213 |  | 
 | 214 | static void ssu100_set_termios(struct tty_struct *tty, | 
 | 215 | 			       struct usb_serial_port *port, | 
 | 216 | 			       struct ktermios *old_termios) | 
 | 217 | { | 
 | 218 | 	struct usb_device *dev = port->serial->dev; | 
 | 219 | 	struct ktermios *termios = tty->termios; | 
 | 220 | 	u16 baud, divisor, remainder; | 
 | 221 | 	unsigned int cflag = termios->c_cflag; | 
 | 222 | 	u16 urb_value = 0; /* will hold the new flags */ | 
 | 223 | 	int result; | 
 | 224 |  | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 225 | 	if (cflag & PARENB) { | 
 | 226 | 		if (cflag & PARODD) | 
| Bill Pemberton | 79f203a | 2010-08-05 17:01:07 -0400 | [diff] [blame] | 227 | 			urb_value |= UART_LCR_PARITY; | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 228 | 		else | 
 | 229 | 			urb_value |= SERIAL_EVEN_PARITY; | 
 | 230 | 	} | 
 | 231 |  | 
 | 232 | 	switch (cflag & CSIZE) { | 
 | 233 | 	case CS5: | 
| Bill Pemberton | 79f203a | 2010-08-05 17:01:07 -0400 | [diff] [blame] | 234 | 		urb_value |= UART_LCR_WLEN5; | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 235 | 		break; | 
 | 236 | 	case CS6: | 
| Bill Pemberton | 79f203a | 2010-08-05 17:01:07 -0400 | [diff] [blame] | 237 | 		urb_value |= UART_LCR_WLEN6; | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 238 | 		break; | 
 | 239 | 	case CS7: | 
| Bill Pemberton | 79f203a | 2010-08-05 17:01:07 -0400 | [diff] [blame] | 240 | 		urb_value |= UART_LCR_WLEN7; | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 241 | 		break; | 
 | 242 | 	default: | 
 | 243 | 	case CS8: | 
| Bill Pemberton | 79f203a | 2010-08-05 17:01:07 -0400 | [diff] [blame] | 244 | 		urb_value |= UART_LCR_WLEN8; | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 245 | 		break; | 
 | 246 | 	} | 
 | 247 |  | 
 | 248 | 	baud = tty_get_baud_rate(tty); | 
 | 249 | 	if (!baud) | 
 | 250 | 		baud = 9600; | 
 | 251 |  | 
 | 252 | 	dbg("%s - got baud = %d\n", __func__, baud); | 
 | 253 |  | 
 | 254 |  | 
 | 255 | 	divisor = MAX_BAUD_RATE / baud; | 
 | 256 | 	remainder = MAX_BAUD_RATE % baud; | 
 | 257 | 	if (((remainder * 2) >= baud) && (baud != 110)) | 
 | 258 | 		divisor++; | 
 | 259 |  | 
 | 260 | 	urb_value = urb_value << 8; | 
 | 261 |  | 
 | 262 | 	result = ssu100_control_msg(dev, QT_GET_SET_UART, divisor, urb_value); | 
 | 263 | 	if (result < 0) | 
 | 264 | 		dbg("%s - set uart failed", __func__); | 
 | 265 |  | 
 | 266 | 	if (cflag & CRTSCTS) | 
 | 267 | 		result = ssu100_control_msg(dev, QT_HW_FLOW_CONTROL_MASK, | 
 | 268 | 					    SERIAL_CRTSCTS, 0); | 
 | 269 | 	else | 
 | 270 | 		result = ssu100_control_msg(dev, QT_HW_FLOW_CONTROL_MASK, | 
 | 271 | 					    0, 0); | 
 | 272 | 	if (result < 0) | 
 | 273 | 		dbg("%s - set HW flow control failed", __func__); | 
 | 274 |  | 
 | 275 | 	if (I_IXOFF(tty) || I_IXON(tty)) { | 
 | 276 | 		u16 x = ((u16)(START_CHAR(tty) << 8) | (u16)(STOP_CHAR(tty))); | 
 | 277 |  | 
 | 278 | 		result = ssu100_control_msg(dev, QT_SW_FLOW_CONTROL_MASK, | 
 | 279 | 					    x, 0); | 
 | 280 | 	} else | 
 | 281 | 		result = ssu100_control_msg(dev, QT_SW_FLOW_CONTROL_MASK, | 
 | 282 | 					    0, 0); | 
 | 283 |  | 
 | 284 | 	if (result < 0) | 
 | 285 | 		dbg("%s - set SW flow control failed", __func__); | 
 | 286 |  | 
 | 287 | } | 
 | 288 |  | 
 | 289 |  | 
 | 290 | static int ssu100_open(struct tty_struct *tty, struct usb_serial_port *port) | 
 | 291 | { | 
 | 292 | 	struct usb_device *dev = port->serial->dev; | 
 | 293 | 	struct ssu100_port_private *priv = usb_get_serial_port_data(port); | 
 | 294 | 	u8 *data; | 
 | 295 | 	int result; | 
| Bill Pemberton | 1752305 | 2010-08-05 17:01:05 -0400 | [diff] [blame] | 296 | 	unsigned long flags; | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 297 |  | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 298 | 	data = kzalloc(2, GFP_KERNEL); | 
 | 299 | 	if (!data) | 
 | 300 | 		return -ENOMEM; | 
 | 301 |  | 
 | 302 | 	result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), | 
 | 303 | 				 QT_OPEN_CLOSE_CHANNEL, | 
 | 304 | 				 QT_TRANSFER_IN, 0x01, | 
 | 305 | 				 0, data, 2, 300); | 
 | 306 | 	if (result < 0) { | 
 | 307 | 		dbg("%s - open failed %i", __func__, result); | 
 | 308 | 		kfree(data); | 
 | 309 | 		return result; | 
 | 310 | 	} | 
 | 311 |  | 
| Bill Pemberton | 1752305 | 2010-08-05 17:01:05 -0400 | [diff] [blame] | 312 | 	spin_lock_irqsave(&priv->status_lock, flags); | 
| Bill Pemberton | f81c83d | 2010-08-05 17:01:09 -0400 | [diff] [blame] | 313 | 	priv->shadowLSR = data[0]; | 
 | 314 | 	priv->shadowMSR = data[1]; | 
| Bill Pemberton | 1752305 | 2010-08-05 17:01:05 -0400 | [diff] [blame] | 315 | 	spin_unlock_irqrestore(&priv->status_lock, flags); | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 316 |  | 
 | 317 | 	kfree(data); | 
 | 318 |  | 
 | 319 | /* set to 9600 */ | 
 | 320 | 	result = ssu100_control_msg(dev, QT_GET_SET_UART, 0x30, 0x0300); | 
 | 321 | 	if (result < 0) | 
 | 322 | 		dbg("%s - set uart failed", __func__); | 
 | 323 |  | 
 | 324 | 	if (tty) | 
 | 325 | 		ssu100_set_termios(tty, port, tty->termios); | 
 | 326 |  | 
 | 327 | 	return usb_serial_generic_open(tty, port); | 
 | 328 | } | 
 | 329 |  | 
 | 330 | static void ssu100_close(struct usb_serial_port *port) | 
 | 331 | { | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 332 | 	usb_serial_generic_close(port); | 
 | 333 | } | 
 | 334 |  | 
 | 335 | static int get_serial_info(struct usb_serial_port *port, | 
 | 336 | 			   struct serial_struct __user *retinfo) | 
 | 337 | { | 
 | 338 | 	struct serial_struct tmp; | 
 | 339 |  | 
 | 340 | 	if (!retinfo) | 
 | 341 | 		return -EFAULT; | 
 | 342 |  | 
 | 343 | 	memset(&tmp, 0, sizeof(tmp)); | 
 | 344 | 	tmp.line		= port->serial->minor; | 
 | 345 | 	tmp.port		= 0; | 
 | 346 | 	tmp.irq			= 0; | 
 | 347 | 	tmp.flags		= ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ; | 
 | 348 | 	tmp.xmit_fifo_size	= port->bulk_out_size; | 
 | 349 | 	tmp.baud_base		= 9600; | 
 | 350 | 	tmp.close_delay		= 5*HZ; | 
 | 351 | 	tmp.closing_wait	= 30*HZ; | 
 | 352 |  | 
 | 353 | 	if (copy_to_user(retinfo, &tmp, sizeof(*retinfo))) | 
 | 354 | 		return -EFAULT; | 
 | 355 | 	return 0; | 
 | 356 | } | 
 | 357 |  | 
| Bill Pemberton | f81c83d | 2010-08-05 17:01:09 -0400 | [diff] [blame] | 358 | static int wait_modem_info(struct usb_serial_port *port, unsigned int arg) | 
 | 359 | { | 
 | 360 | 	struct ssu100_port_private *priv = usb_get_serial_port_data(port); | 
 | 361 | 	struct async_icount prev, cur; | 
 | 362 | 	unsigned long flags; | 
 | 363 |  | 
 | 364 | 	spin_lock_irqsave(&priv->status_lock, flags); | 
 | 365 | 	prev = priv->icount; | 
 | 366 | 	spin_unlock_irqrestore(&priv->status_lock, flags); | 
 | 367 |  | 
 | 368 | 	while (1) { | 
 | 369 | 		wait_event_interruptible(priv->delta_msr_wait, | 
 | 370 | 					 ((priv->icount.rng != prev.rng) || | 
 | 371 | 					  (priv->icount.dsr != prev.dsr) || | 
 | 372 | 					  (priv->icount.dcd != prev.dcd) || | 
 | 373 | 					  (priv->icount.cts != prev.cts))); | 
 | 374 |  | 
 | 375 | 		if (signal_pending(current)) | 
 | 376 | 			return -ERESTARTSYS; | 
 | 377 |  | 
 | 378 | 		spin_lock_irqsave(&priv->status_lock, flags); | 
 | 379 | 		cur = priv->icount; | 
 | 380 | 		spin_unlock_irqrestore(&priv->status_lock, flags); | 
 | 381 |  | 
 | 382 | 		if ((prev.rng == cur.rng) && | 
 | 383 | 		    (prev.dsr == cur.dsr) && | 
 | 384 | 		    (prev.dcd == cur.dcd) && | 
 | 385 | 		    (prev.cts == cur.cts)) | 
 | 386 | 			return -EIO; | 
 | 387 |  | 
 | 388 | 		if ((arg & TIOCM_RNG && (prev.rng != cur.rng)) || | 
 | 389 | 		    (arg & TIOCM_DSR && (prev.dsr != cur.dsr)) || | 
 | 390 | 		    (arg & TIOCM_CD  && (prev.dcd != cur.dcd)) || | 
 | 391 | 		    (arg & TIOCM_CTS && (prev.cts != cur.cts))) | 
 | 392 | 			return 0; | 
 | 393 | 	} | 
 | 394 | 	return 0; | 
 | 395 | } | 
 | 396 |  | 
| Alan Cox | 0bca1b9 | 2010-09-16 18:21:40 +0100 | [diff] [blame] | 397 | static int ssu100_get_icount(struct tty_struct *tty, | 
 | 398 | 			struct serial_icounter_struct *icount) | 
 | 399 | { | 
 | 400 | 	struct usb_serial_port *port = tty->driver_data; | 
 | 401 | 	struct ssu100_port_private *priv = usb_get_serial_port_data(port); | 
 | 402 | 	struct async_icount cnow = priv->icount; | 
 | 403 |  | 
 | 404 | 	icount->cts = cnow.cts; | 
 | 405 | 	icount->dsr = cnow.dsr; | 
 | 406 | 	icount->rng = cnow.rng; | 
 | 407 | 	icount->dcd = cnow.dcd; | 
 | 408 | 	icount->rx = cnow.rx; | 
 | 409 | 	icount->tx = cnow.tx; | 
 | 410 | 	icount->frame = cnow.frame; | 
 | 411 | 	icount->overrun = cnow.overrun; | 
 | 412 | 	icount->parity = cnow.parity; | 
 | 413 | 	icount->brk = cnow.brk; | 
 | 414 | 	icount->buf_overrun = cnow.buf_overrun; | 
 | 415 |  | 
 | 416 | 	return 0; | 
 | 417 | } | 
 | 418 |  | 
 | 419 |  | 
 | 420 |  | 
| Alan Cox | 00a0d0d | 2011-02-14 16:27:06 +0000 | [diff] [blame] | 421 | static int ssu100_ioctl(struct tty_struct *tty, | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 422 | 		    unsigned int cmd, unsigned long arg) | 
 | 423 | { | 
 | 424 | 	struct usb_serial_port *port = tty->driver_data; | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 425 |  | 
 | 426 | 	dbg("%s cmd 0x%04x", __func__, cmd); | 
 | 427 |  | 
 | 428 | 	switch (cmd) { | 
 | 429 | 	case TIOCGSERIAL: | 
 | 430 | 		return get_serial_info(port, | 
 | 431 | 				       (struct serial_struct __user *) arg); | 
 | 432 |  | 
 | 433 | 	case TIOCMIWAIT: | 
| Bill Pemberton | f81c83d | 2010-08-05 17:01:09 -0400 | [diff] [blame] | 434 | 		return wait_modem_info(port, arg); | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 435 |  | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 436 | 	default: | 
 | 437 | 		break; | 
 | 438 | 	} | 
 | 439 |  | 
 | 440 | 	dbg("%s arg not supported", __func__); | 
 | 441 |  | 
 | 442 | 	return -ENOIOCTLCMD; | 
 | 443 | } | 
 | 444 |  | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 445 | static int ssu100_attach(struct usb_serial *serial) | 
 | 446 | { | 
 | 447 | 	struct ssu100_port_private *priv; | 
 | 448 | 	struct usb_serial_port *port = *serial->port; | 
 | 449 |  | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 450 | 	priv = kzalloc(sizeof(*priv), GFP_KERNEL); | 
 | 451 | 	if (!priv) { | 
 | 452 | 		dev_err(&port->dev, "%s- kmalloc(%Zd) failed.\n", __func__, | 
 | 453 | 			sizeof(*priv)); | 
 | 454 | 		return -ENOMEM; | 
 | 455 | 	} | 
 | 456 |  | 
| Bill Pemberton | 1752305 | 2010-08-05 17:01:05 -0400 | [diff] [blame] | 457 | 	spin_lock_init(&priv->status_lock); | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 458 | 	init_waitqueue_head(&priv->delta_msr_wait); | 
 | 459 | 	usb_set_serial_port_data(port, priv); | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 460 |  | 
 | 461 | 	return ssu100_initdevice(serial->dev); | 
 | 462 | } | 
 | 463 |  | 
| Alan Cox | 60b33c1 | 2011-02-14 16:26:14 +0000 | [diff] [blame] | 464 | static int ssu100_tiocmget(struct tty_struct *tty) | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 465 | { | 
 | 466 | 	struct usb_serial_port *port = tty->driver_data; | 
 | 467 | 	struct usb_device *dev = port->serial->dev; | 
 | 468 | 	u8 *d; | 
 | 469 | 	int r; | 
 | 470 |  | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 471 | 	d = kzalloc(2, GFP_KERNEL); | 
 | 472 | 	if (!d) | 
 | 473 | 		return -ENOMEM; | 
 | 474 |  | 
| Bill Pemberton | 79f203a | 2010-08-05 17:01:07 -0400 | [diff] [blame] | 475 | 	r = ssu100_getregister(dev, 0, UART_MCR, d); | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 476 | 	if (r < 0) | 
 | 477 | 		goto mget_out; | 
 | 478 |  | 
| Bill Pemberton | 79f203a | 2010-08-05 17:01:07 -0400 | [diff] [blame] | 479 | 	r = ssu100_getregister(dev, 0, UART_MSR, d+1); | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 480 | 	if (r < 0) | 
 | 481 | 		goto mget_out; | 
 | 482 |  | 
| Bill Pemberton | 79f203a | 2010-08-05 17:01:07 -0400 | [diff] [blame] | 483 | 	r = (d[0] & UART_MCR_DTR ? TIOCM_DTR : 0) | | 
 | 484 | 		(d[0] & UART_MCR_RTS ? TIOCM_RTS : 0) | | 
 | 485 | 		(d[1] & UART_MSR_CTS ? TIOCM_CTS : 0) | | 
 | 486 | 		(d[1] & UART_MSR_DCD ? TIOCM_CAR : 0) | | 
 | 487 | 		(d[1] & UART_MSR_RI ? TIOCM_RI : 0) | | 
 | 488 | 		(d[1] & UART_MSR_DSR ? TIOCM_DSR : 0); | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 489 |  | 
 | 490 | mget_out: | 
 | 491 | 	kfree(d); | 
 | 492 | 	return r; | 
 | 493 | } | 
 | 494 |  | 
| Alan Cox | 20b9d17 | 2011-02-14 16:26:50 +0000 | [diff] [blame] | 495 | static int ssu100_tiocmset(struct tty_struct *tty, | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 496 | 			   unsigned int set, unsigned int clear) | 
 | 497 | { | 
 | 498 | 	struct usb_serial_port *port = tty->driver_data; | 
 | 499 | 	struct usb_device *dev = port->serial->dev; | 
 | 500 |  | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 501 | 	return update_mctrl(dev, set, clear); | 
 | 502 | } | 
 | 503 |  | 
 | 504 | static void ssu100_dtr_rts(struct usb_serial_port *port, int on) | 
 | 505 | { | 
 | 506 | 	struct usb_device *dev = port->serial->dev; | 
 | 507 |  | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 508 | 	mutex_lock(&port->serial->disc_mutex); | 
 | 509 | 	if (!port->serial->disconnected) { | 
 | 510 | 		/* Disable flow control */ | 
 | 511 | 		if (!on && | 
| Bill Pemberton | 556f1a0 | 2010-08-05 17:01:08 -0400 | [diff] [blame] | 512 | 		    ssu100_setregister(dev, 0, UART_MCR, 0) < 0) | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 513 | 			dev_err(&port->dev, "error from flowcontrol urb\n"); | 
 | 514 | 		/* drop RTS and DTR */ | 
 | 515 | 		if (on) | 
 | 516 | 			set_mctrl(dev, TIOCM_DTR | TIOCM_RTS); | 
 | 517 | 		else | 
 | 518 | 			clear_mctrl(dev, TIOCM_DTR | TIOCM_RTS); | 
 | 519 | 	} | 
 | 520 | 	mutex_unlock(&port->serial->disc_mutex); | 
 | 521 | } | 
 | 522 |  | 
| Bill Pemberton | f81c83d | 2010-08-05 17:01:09 -0400 | [diff] [blame] | 523 | static void ssu100_update_msr(struct usb_serial_port *port, u8 msr) | 
 | 524 | { | 
 | 525 | 	struct ssu100_port_private *priv = usb_get_serial_port_data(port); | 
 | 526 | 	unsigned long flags; | 
 | 527 |  | 
 | 528 | 	spin_lock_irqsave(&priv->status_lock, flags); | 
 | 529 | 	priv->shadowMSR = msr; | 
 | 530 | 	spin_unlock_irqrestore(&priv->status_lock, flags); | 
 | 531 |  | 
 | 532 | 	if (msr & UART_MSR_ANY_DELTA) { | 
 | 533 | 		/* update input line counters */ | 
 | 534 | 		if (msr & UART_MSR_DCTS) | 
 | 535 | 			priv->icount.cts++; | 
 | 536 | 		if (msr & UART_MSR_DDSR) | 
 | 537 | 			priv->icount.dsr++; | 
 | 538 | 		if (msr & UART_MSR_DDCD) | 
 | 539 | 			priv->icount.dcd++; | 
 | 540 | 		if (msr & UART_MSR_TERI) | 
 | 541 | 			priv->icount.rng++; | 
 | 542 | 		wake_up_interruptible(&priv->delta_msr_wait); | 
 | 543 | 	} | 
 | 544 | } | 
 | 545 |  | 
| Bill Pemberton | 6b8f1ca | 2010-08-13 09:59:31 -0400 | [diff] [blame] | 546 | static void ssu100_update_lsr(struct usb_serial_port *port, u8 lsr, | 
 | 547 | 			      char *tty_flag) | 
| Bill Pemberton | f81c83d | 2010-08-05 17:01:09 -0400 | [diff] [blame] | 548 | { | 
 | 549 | 	struct ssu100_port_private *priv = usb_get_serial_port_data(port); | 
 | 550 | 	unsigned long flags; | 
 | 551 |  | 
 | 552 | 	spin_lock_irqsave(&priv->status_lock, flags); | 
 | 553 | 	priv->shadowLSR = lsr; | 
 | 554 | 	spin_unlock_irqrestore(&priv->status_lock, flags); | 
 | 555 |  | 
| Bill Pemberton | 6b8f1ca | 2010-08-13 09:59:31 -0400 | [diff] [blame] | 556 | 	*tty_flag = TTY_NORMAL; | 
| Bill Pemberton | f81c83d | 2010-08-05 17:01:09 -0400 | [diff] [blame] | 557 | 	if (lsr & UART_LSR_BRK_ERROR_BITS) { | 
| Bill Pemberton | 6b8f1ca | 2010-08-13 09:59:31 -0400 | [diff] [blame] | 558 | 		/* we always want to update icount, but we only want to | 
 | 559 | 		 * update tty_flag for one case */ | 
 | 560 | 		if (lsr & UART_LSR_BI) { | 
| Bill Pemberton | f81c83d | 2010-08-05 17:01:09 -0400 | [diff] [blame] | 561 | 			priv->icount.brk++; | 
| Bill Pemberton | 6b8f1ca | 2010-08-13 09:59:31 -0400 | [diff] [blame] | 562 | 			*tty_flag = TTY_BREAK; | 
 | 563 | 			usb_serial_handle_break(port); | 
 | 564 | 		} | 
 | 565 | 		if (lsr & UART_LSR_PE) { | 
| Bill Pemberton | f81c83d | 2010-08-05 17:01:09 -0400 | [diff] [blame] | 566 | 			priv->icount.parity++; | 
| Bill Pemberton | 6b8f1ca | 2010-08-13 09:59:31 -0400 | [diff] [blame] | 567 | 			if (*tty_flag == TTY_NORMAL) | 
 | 568 | 				*tty_flag = TTY_PARITY; | 
 | 569 | 		} | 
 | 570 | 		if (lsr & UART_LSR_FE) { | 
 | 571 | 			priv->icount.frame++; | 
 | 572 | 			if (*tty_flag == TTY_NORMAL) | 
 | 573 | 				*tty_flag = TTY_FRAME; | 
 | 574 | 		} | 
 | 575 | 		if (lsr & UART_LSR_OE){ | 
| Bill Pemberton | f81c83d | 2010-08-05 17:01:09 -0400 | [diff] [blame] | 576 | 			priv->icount.overrun++; | 
| Bill Pemberton | 6b8f1ca | 2010-08-13 09:59:31 -0400 | [diff] [blame] | 577 | 			if (*tty_flag == TTY_NORMAL) | 
 | 578 | 				*tty_flag = TTY_OVERRUN; | 
 | 579 | 		} | 
| Bill Pemberton | f81c83d | 2010-08-05 17:01:09 -0400 | [diff] [blame] | 580 | 	} | 
| Bill Pemberton | 6b8f1ca | 2010-08-13 09:59:31 -0400 | [diff] [blame] | 581 |  | 
| Bill Pemberton | f81c83d | 2010-08-05 17:01:09 -0400 | [diff] [blame] | 582 | } | 
 | 583 |  | 
| Bill Pemberton | f7043ec | 2010-10-21 14:43:05 -0400 | [diff] [blame] | 584 | static int ssu100_process_packet(struct urb *urb, | 
 | 585 | 				 struct tty_struct *tty) | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 586 | { | 
| Bill Pemberton | f7043ec | 2010-10-21 14:43:05 -0400 | [diff] [blame] | 587 | 	struct usb_serial_port *port = urb->context; | 
 | 588 | 	char *packet = (char *)urb->transfer_buffer; | 
| Bill Pemberton | 6b8f1ca | 2010-08-13 09:59:31 -0400 | [diff] [blame] | 589 | 	char flag = TTY_NORMAL; | 
| Bill Pemberton | f7043ec | 2010-10-21 14:43:05 -0400 | [diff] [blame] | 590 | 	u32 len = urb->actual_length; | 
 | 591 | 	int i; | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 592 | 	char *ch; | 
 | 593 |  | 
| Bill Pemberton | 9b2cef3 | 2010-08-05 17:01:06 -0400 | [diff] [blame] | 594 | 	if ((len >= 4) && | 
 | 595 | 	    (packet[0] == 0x1b) && (packet[1] == 0x1b) && | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 596 | 	    ((packet[2] == 0x00) || (packet[2] == 0x01))) { | 
| Bill Pemberton | 6b8f1ca | 2010-08-13 09:59:31 -0400 | [diff] [blame] | 597 | 		if (packet[2] == 0x00) { | 
 | 598 | 			ssu100_update_lsr(port, packet[3], &flag); | 
 | 599 | 			if (flag == TTY_OVERRUN) | 
 | 600 | 				tty_insert_flip_char(tty, 0, TTY_OVERRUN); | 
 | 601 | 		} | 
| Bill Pemberton | f81c83d | 2010-08-05 17:01:09 -0400 | [diff] [blame] | 602 | 		if (packet[2] == 0x01) | 
 | 603 | 			ssu100_update_msr(port, packet[3]); | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 604 |  | 
 | 605 | 		len -= 4; | 
 | 606 | 		ch = packet + 4; | 
 | 607 | 	} else | 
 | 608 | 		ch = packet; | 
 | 609 |  | 
 | 610 | 	if (!len) | 
 | 611 | 		return 0;	/* status only */ | 
 | 612 |  | 
 | 613 | 	if (port->port.console && port->sysrq) { | 
 | 614 | 		for (i = 0; i < len; i++, ch++) { | 
| Dmitry Torokhov | 6ee9f4b | 2010-08-17 21:15:47 -0700 | [diff] [blame] | 615 | 			if (!usb_serial_handle_sysrq_char(port, *ch)) | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 616 | 				tty_insert_flip_char(tty, *ch, flag); | 
 | 617 | 		} | 
 | 618 | 	} else | 
 | 619 | 		tty_insert_flip_string_fixed_flag(tty, ch, flag, len); | 
 | 620 |  | 
 | 621 | 	return len; | 
 | 622 | } | 
 | 623 |  | 
 | 624 | static void ssu100_process_read_urb(struct urb *urb) | 
 | 625 | { | 
 | 626 | 	struct usb_serial_port *port = urb->context; | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 627 | 	struct tty_struct *tty; | 
| Bill Pemberton | f7043ec | 2010-10-21 14:43:05 -0400 | [diff] [blame] | 628 | 	int count; | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 629 |  | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 630 | 	tty = tty_port_tty_get(&port->port); | 
 | 631 | 	if (!tty) | 
 | 632 | 		return; | 
 | 633 |  | 
| Bill Pemberton | f7043ec | 2010-10-21 14:43:05 -0400 | [diff] [blame] | 634 | 	count = ssu100_process_packet(urb, tty); | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 635 |  | 
 | 636 | 	if (count) | 
 | 637 | 		tty_flip_buffer_push(tty); | 
 | 638 | 	tty_kref_put(tty); | 
 | 639 | } | 
 | 640 |  | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 641 | static struct usb_serial_driver ssu100_device = { | 
 | 642 | 	.driver = { | 
 | 643 | 		.owner = THIS_MODULE, | 
 | 644 | 		.name = "ssu100", | 
 | 645 | 	}, | 
 | 646 | 	.description	     = DRIVER_DESC, | 
 | 647 | 	.id_table	     = id_table, | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 648 | 	.num_ports	     = 1, | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 649 | 	.open		     = ssu100_open, | 
 | 650 | 	.close		     = ssu100_close, | 
 | 651 | 	.attach              = ssu100_attach, | 
 | 652 | 	.release             = ssu100_release, | 
 | 653 | 	.dtr_rts             = ssu100_dtr_rts, | 
 | 654 | 	.process_read_urb    = ssu100_process_read_urb, | 
 | 655 | 	.tiocmget            = ssu100_tiocmget, | 
 | 656 | 	.tiocmset            = ssu100_tiocmset, | 
| Alan Cox | 0bca1b9 | 2010-09-16 18:21:40 +0100 | [diff] [blame] | 657 | 	.get_icount	     = ssu100_get_icount, | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 658 | 	.ioctl               = ssu100_ioctl, | 
 | 659 | 	.set_termios         = ssu100_set_termios, | 
| Bill Pemberton | 85dee13 | 2010-08-05 17:01:11 -0400 | [diff] [blame] | 660 | 	.disconnect          = usb_serial_generic_disconnect, | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 661 | }; | 
 | 662 |  | 
| Alan Stern | d860322 | 2012-02-23 14:57:25 -0500 | [diff] [blame] | 663 | static struct usb_serial_driver * const serial_drivers[] = { | 
 | 664 | 	&ssu100_device, NULL | 
 | 665 | }; | 
 | 666 |  | 
| Greg Kroah-Hartman | 68e2411 | 2012-05-08 15:46:14 -0700 | [diff] [blame] | 667 | module_usb_serial_driver(serial_drivers, id_table); | 
| Bill Pemberton | 52af954 | 2010-07-29 11:05:41 -0400 | [diff] [blame] | 668 |  | 
 | 669 | MODULE_DESCRIPTION(DRIVER_DESC); | 
 | 670 | MODULE_LICENSE("GPL"); | 
 | 671 |  | 
 | 672 | module_param(debug, bool, S_IRUGO | S_IWUSR); | 
 | 673 | MODULE_PARM_DESC(debug, "Debug enabled or not"); |