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